]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
Up to 6.8.0 (in progress)
[packages/kernel.git] / kernel-aufs.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs6.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index 89fdbefd1075..0038eb501478 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -338,6 +338,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 c09016257f05..3899fae66d3f 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -130,3 +130,4 @@ obj-$(CONFIG_EFIVAR_FS)             += efivarfs/
21  obj-$(CONFIG_EROFS_FS)         += erofs/
22  obj-$(CONFIG_VBOXSF_FS)                += vboxsf/
23  obj-$(CONFIG_ZONEFS_FS)                += zonefs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs6.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 9ed4d3868539..c0f45969bcf8 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3365,6 +3365,19 @@ F:       kernel/audit*
33  F:     lib/*audit.c
34  K:     \baudit_[a-z_0-9]\+\b
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
40 +S:     Supported
41 +W:     http://aufs.sourceforge.net
42 +T:     git://github.com/sfjro/aufs4-linux.git
43 +F:     Documentation/ABI/testing/debugfs-aufs
44 +F:     Documentation/ABI/testing/sysfs-aufs
45 +F:     Documentation/filesystems/aufs/
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY BUS DRIVER
50  M:     Greg Kroah-Hartman <gregkh@linuxfoundation.org>
51  R:     Dave Ertman <david.m.ertman@intel.com>
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index f8145499da38..333b3915fe86 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -641,6 +641,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 b813528fb147..78e322b59b8d 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1219,7 +1219,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, *dentry;
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index c80a6acad742..96a562b48ec7 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -34,7 +34,7 @@
99  
100  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101  
102 -static int setfl(int fd, struct file * filp, unsigned int arg)
103 +int setfl(int fd, struct file * filp, unsigned int arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned int arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/namespace.c b/fs/namespace.c
117 index 5a51315c6678..a4b6ce51848e 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -860,6 +860,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 218e24b1ac40..7210ddc5aa81 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -933,7 +933,7 @@ static int warn_unsupported(struct file *file, const char *op)
138  /*
139   * Attempt to initiate a splice from pipe to file.
140   */
141 -static ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142 +ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
143                               loff_t *ppos, size_t len, unsigned int flags)
144  {
145         if (unlikely(!out->f_op->splice_write))
146 diff --git a/include/linux/fs.h b/include/linux/fs.h
147 index 023f37c60709..96ca249ee1e6 100644
148 --- a/include/linux/fs.h
149 +++ b/include/linux/fs.h
150 @@ -1104,6 +1104,7 @@ extern void fasync_free(struct fasync_struct *);
151  /* can be called from interrupts */
152  extern void kill_fasync(struct fasync_struct **, int, int);
153  
154 +extern int setfl(int fd, struct file *filp, unsigned int arg);
155  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
156  extern int f_setown(struct file *filp, int who, int force);
157  extern void f_delown(struct file *filp);
158 @@ -2001,6 +2002,7 @@ struct file_operations {
159         int (*lock) (struct file *, int, struct file_lock *);
160         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
161         int (*check_flags)(int);
162 +       int (*setfl)(struct file *, unsigned long);
163         int (*flock) (struct file *, int, struct file_lock *);
164         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
165         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
166 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
167 index 08b0d1d9d78b..009f69c4e09d 100644
168 --- a/include/linux/lockdep.h
169 +++ b/include/linux/lockdep.h
170 @@ -192,6 +192,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
171         return lock->key == key;
172  }
173  
174 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
175 +
176  /*
177   * Acquire a lock.
178   *
179 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
180 index 8f882f5881e8..6b9808f09843 100644
181 --- a/include/linux/mnt_namespace.h
182 +++ b/include/linux/mnt_namespace.h
183 @@ -7,12 +7,15 @@ struct mnt_namespace;
184  struct fs_struct;
185  struct user_namespace;
186  struct ns_common;
187 +struct vfsmount;
188  
189  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
190                 struct user_namespace *, struct fs_struct *);
191  extern void put_mnt_ns(struct mnt_namespace *ns);
192  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
193  
194 +extern int is_current_mnt_ns(struct vfsmount *mnt);
195 +
196  extern const struct file_operations proc_mounts_operations;
197  extern const struct file_operations proc_mountinfo_operations;
198  extern const struct file_operations proc_mountstats_operations;
199 diff --git a/include/linux/splice.h b/include/linux/splice.h
200 index 9dec4861d09f..14583d846864 100644
201 --- a/include/linux/splice.h
202 +++ b/include/linux/splice.h
203 @@ -108,4 +108,7 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
204  
205  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
206  extern const struct pipe_buf_operations default_pipe_buf_ops;
207 +
208 +extern ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
209 +                             loff_t *ppos, size_t len, unsigned int flags);
210  #endif
211 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
212 index 151bd3de5936..2223ee909c10 100644
213 --- a/kernel/locking/lockdep.c
214 +++ b/kernel/locking/lockdep.c
215 @@ -218,7 +218,7 @@ unsigned long max_lock_class_idx;
216  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
217  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
218  
219 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
220 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
221  {
222         unsigned int class_idx = hlock->class_idx;
223  
224 @@ -239,6 +239,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
225          */
226         return lock_classes + class_idx;
227  }
228 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
229  
230  #ifdef CONFIG_LOCK_STAT
231  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
232 SPDX-License-Identifier: GPL-2.0
233 aufs6.x-rcN mmap patch
234
235 diff --git a/fs/proc/base.c b/fs/proc/base.c
236 index 98a031ac2648..a7a2ff2a7f61 100644
237 --- a/fs/proc/base.c
238 +++ b/fs/proc/base.c
239 @@ -2215,7 +2215,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
240         rc = -ENOENT;
241         vma = find_exact_vma(mm, vm_start, vm_end);
242         if (vma && vma->vm_file) {
243 -               *path = *file_user_path(vma->vm_file);
244 +               *path = vma_pr_or_file(vma)->f_path;
245                 path_get(path);
246                 rc = 0;
247         }
248 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
249 index c6e7ebc63756..d7ccfd909764 100644
250 --- a/fs/proc/nommu.c
251 +++ b/fs/proc/nommu.c
252 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
253         file = region->vm_file;
254  
255         if (file) {
256 -               struct inode *inode = file_inode(region->vm_file);
257 +               struct inode *inode;
258 +
259 +               file = vmr_pr_or_file(region);
260 +               inode = file_inode(file);
261                 dev = inode->i_sb->s_dev;
262                 ino = inode->i_ino;
263         }
264 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
265 index 3f78ebbb795f..f96ec77db39e 100644
266 --- a/fs/proc/task_mmu.c
267 +++ b/fs/proc/task_mmu.c
268 @@ -273,7 +273,8 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
269         const char *name = NULL;
270  
271         if (file) {
272 -               const struct inode *inode = file_user_inode(vma->vm_file);
273 +               const struct inode *inode
274 +                       = file_inode(file = vma_pr_or_file(vma));
275  
276                 dev = inode->i_sb->s_dev;
277                 ino = inode->i_ino;
278 @@ -2701,7 +2702,7 @@ static int show_numa_map(struct seq_file *m, void *v)
279         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
280         struct vm_area_struct *vma = v;
281         struct numa_maps *md = &numa_priv->md;
282 -       struct file *file = vma->vm_file;
283 +       struct file *file = vma_pr_or_file(vma);
284         struct mm_struct *mm = vma->vm_mm;
285         char buffer[64];
286         struct mempolicy *pol;
287 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
288 index bce674533000..b12b5a75c799 100644
289 --- a/fs/proc/task_nommu.c
290 +++ b/fs/proc/task_nommu.c
291 @@ -137,7 +137,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
292         file = vma->vm_file;
293  
294         if (file) {
295 -               struct inode *inode = file_inode(vma->vm_file);
296 +               struct inode *inode;
297 +
298 +               file = vma_pr_or_file(vma);
299 +               inode = file_inode(file);
300                 dev = inode->i_sb->s_dev;
301                 ino = inode->i_ino;
302                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
303 diff --git a/include/linux/mm.h b/include/linux/mm.h
304 index f5a97dec5169..a5b0edc8e290 100644
305 --- a/include/linux/mm.h
306 +++ b/include/linux/mm.h
307 @@ -2432,6 +2432,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
308  static inline struct vm_area_struct *vma_lookup(struct mm_struct *mm,
309                                                 unsigned long addr);
310  
311 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
312 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
313 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
314 +                                     int);
315 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
316 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
317 +
318 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
319 +                                                               __LINE__)
320 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
321 +                                                         __LINE__)
322 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
323 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
324 +
325 +#ifndef CONFIG_MMU
326 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
327 +extern void vmr_do_fput(struct vm_region *, const char[], int);
328 +
329 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
330 +                                                         __LINE__)
331 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
332 +#endif /* !CONFIG_MMU */
333 +
334 +#else
335 +
336 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
337 +#define vma_pr_or_file(vma)            (vma)->vm_file
338 +#define vma_get_file(vma)              get_file((vma)->vm_file)
339 +#define vma_fput(vma)                  fput((vma)->vm_file)
340 +
341 +#ifndef CONFIG_MMU
342 +#define vmr_pr_or_file(region)         (region)->vm_file
343 +#define vmr_fput(region)               fput((region)->vm_file)
344 +#endif /* !CONFIG_MMU */
345 +
346 +#endif /* CONFIG_AUFS_FS */
347 +
348  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
349                 void *buf, int len, unsigned int gup_flags);
350  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
351 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
352 index 8b611e13153e..838af328941a 100644
353 --- a/include/linux/mm_types.h
354 +++ b/include/linux/mm_types.h
355 @@ -527,6 +527,9 @@ struct vm_region {
356         unsigned long   vm_top;         /* region allocated to here */
357         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
358         struct file     *vm_file;       /* the backing file or NULL */
359 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
360 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
361 +#endif
362  
363         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
364         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
365 @@ -691,6 +694,9 @@ struct vm_area_struct {
366         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
367                                            units */
368         struct file * vm_file;          /* File we map to (can be NULL). */
369 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
370 +       struct file *vm_prfile;         /* shadow of vm_file */
371 +#endif
372         void * vm_private_data;         /* was vm_pte (shared mem) */
373  
374  #ifdef CONFIG_ANON_VMA_NAME
375 diff --git a/kernel/fork.c b/kernel/fork.c
376 index 0d944e92a43f..bc073aaca2dc 100644
377 --- a/kernel/fork.c
378 +++ b/kernel/fork.c
379 @@ -716,7 +716,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
380                 if (file) {
381                         struct address_space *mapping = file->f_mapping;
382  
383 -                       get_file(file);
384 +                       vma_get_file(tmp);
385                         i_mmap_lock_write(mapping);
386                         if (vma_is_shared_maywrite(tmp))
387                                 mapping_allow_writable(mapping);
388 diff --git a/mm/Makefile b/mm/Makefile
389 index e4b5b75aaec9..2ec2544009fb 100644
390 --- a/mm/Makefile
391 +++ b/mm/Makefile
392 @@ -134,3 +134,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
393  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
394  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
395  obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
396 +obj-y += prfile.o
397 diff --git a/mm/filemap.c b/mm/filemap.c
398 index 750e779c23db..b84a752ff676 100644
399 --- a/mm/filemap.c
400 +++ b/mm/filemap.c
401 @@ -3587,7 +3587,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
402         vm_fault_t ret = VM_FAULT_LOCKED;
403  
404         sb_start_pagefault(mapping->host->i_sb);
405 -       file_update_time(vmf->vma->vm_file);
406 +       vma_file_update_time(vmf->vma);
407         folio_lock(folio);
408         if (folio->mapping != mapping) {
409                 folio_unlock(folio);
410 diff --git a/mm/mmap.c b/mm/mmap.c
411 index d89770eaab6b..6bfba84b5ff0 100644
412 --- a/mm/mmap.c
413 +++ b/mm/mmap.c
414 @@ -140,7 +140,7 @@ static void remove_vma(struct vm_area_struct *vma, bool unreachable)
415         if (vma->vm_ops && vma->vm_ops->close)
416                 vma->vm_ops->close(vma);
417         if (vma->vm_file)
418 -               fput(vma->vm_file);
419 +               vma_fput(vma);
420         mpol_put(vma_policy(vma));
421         if (unreachable)
422                 __vm_area_free(vma);
423 @@ -554,7 +554,7 @@ static inline void vma_complete(struct vma_prepare *vp,
424                 if (vp->file) {
425                         uprobe_munmap(vp->remove, vp->remove->vm_start,
426                                       vp->remove->vm_end);
427 -                       fput(vp->file);
428 +                       vma_fput(vp->vma);
429                 }
430                 if (vp->remove->anon_vma)
431                         anon_vma_merge(vp->vma, vp->remove);
432 @@ -2357,7 +2357,7 @@ static int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma,
433                 goto out_free_mpol;
434  
435         if (new->vm_file)
436 -               get_file(new->vm_file);
437 +               vma_get_file(new);
438  
439         if (new->vm_ops && new->vm_ops->open)
440                 new->vm_ops->open(new);
441 @@ -2855,7 +2855,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
442                                  * and cause general protection fault
443                                  * ultimately.
444                                  */
445 -                               fput(vma->vm_file);
446 +                               vma_fput(vma);
447                                 vm_area_free(vma);
448                                 vma = merge;
449                                 /* Update vm_flags to pick up the change. */
450 @@ -2950,7 +2950,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
451  
452         if (file || vma->vm_file) {
453  unmap_and_free_vma:
454 -               fput(vma->vm_file);
455 +               vma_fput(vma);
456                 vma->vm_file = NULL;
457  
458                 vma_iter_set(&vmi, vma->vm_end);
459 @@ -3012,6 +3012,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
460         unsigned long populate = 0;
461         unsigned long ret = -EINVAL;
462         struct file *file;
463 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
464 +       struct file *prfile;
465 +#endif
466  
467         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
468                      current->comm, current->pid);
469 @@ -3070,10 +3073,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
470         if (vma->vm_flags & VM_LOCKED)
471                 flags |= MAP_LOCKED;
472  
473 +#if 1 /* IS_ENABLED(CONFIG_AUFS_FS) */
474 +       vma_get_file(vma);
475 +       file = vma->vm_file;
476 +       prfile = vma->vm_prfile;
477 +       ret = do_mmap(vma->vm_file, start, size,
478 +                       prot, flags, /*vm_flags*/0, pgoff, &populate, NULL);
479 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
480 +               struct vm_area_struct *new_vma;
481 +
482 +               new_vma = find_vma(mm, ret);
483 +               if (!new_vma->vm_prfile)
484 +                       new_vma->vm_prfile = prfile;
485 +               if (prfile)
486 +                       get_file(prfile);
487 +       }
488 +       /*
489 +        * two fput()s instead of vma_fput(vma),
490 +        * coz vma may not be available anymore.
491 +        */
492 +       fput(file);
493 +       if (prfile)
494 +               fput(prfile);
495 +#else
496         file = get_file(vma->vm_file);
497         ret = do_mmap(vma->vm_file, start, size,
498                         prot, flags, 0, pgoff, &populate, NULL);
499         fput(file);
500 +#endif /* CONFIG_AUFS_FS */
501  out:
502         mmap_write_unlock(mm);
503         if (populate)
504 @@ -3420,7 +3447,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
505                 if (anon_vma_clone(new_vma, vma))
506                         goto out_free_mempol;
507                 if (new_vma->vm_file)
508 -                       get_file(new_vma->vm_file);
509 +                       vma_get_file(new_vma);
510                 if (new_vma->vm_ops && new_vma->vm_ops->open)
511                         new_vma->vm_ops->open(new_vma);
512                 if (vma_link(mm, new_vma))
513 @@ -3434,7 +3461,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
514                 new_vma->vm_ops->close(new_vma);
515  
516         if (new_vma->vm_file)
517 -               fput(new_vma->vm_file);
518 +               vma_fput(new_vma);
519  
520         unlink_anon_vmas(new_vma);
521  out_free_mempol:
522 diff --git a/mm/nommu.c b/mm/nommu.c
523 index b6dc558d3144..d96655486468 100644
524 --- a/mm/nommu.c
525 +++ b/mm/nommu.c
526 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
527                 up_write(&nommu_region_sem);
528  
529                 if (region->vm_file)
530 -                       fput(region->vm_file);
531 +                       vmr_fput(region);
532  
533                 /* IO memory and memory shared directly out of the pagecache
534                  * from ramfs/tmpfs mustn't be released here */
535 @@ -603,7 +603,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
536         if (vma->vm_ops && vma->vm_ops->close)
537                 vma->vm_ops->close(vma);
538         if (vma->vm_file)
539 -               fput(vma->vm_file);
540 +               vma_fput(vma);
541         put_nommu_region(vma->vm_region);
542         vm_area_free(vma);
543  }
544 @@ -1135,7 +1135,7 @@ unsigned long do_mmap(struct file *file,
545                                         goto error_just_free;
546                                 }
547                         }
548 -                       fput(region->vm_file);
549 +                       vmr_fput(region);
550                         kmem_cache_free(vm_region_jar, region);
551                         region = pregion;
552                         result = start;
553 @@ -1221,10 +1221,10 @@ unsigned long do_mmap(struct file *file,
554  error:
555         vma_iter_free(&vmi);
556         if (region->vm_file)
557 -               fput(region->vm_file);
558 +               vmr_fput(region);
559         kmem_cache_free(vm_region_jar, region);
560         if (vma->vm_file)
561 -               fput(vma->vm_file);
562 +               vma_fput(vma);
563         vm_area_free(vma);
564         return ret;
565  
566 diff --git a/mm/prfile.c b/mm/prfile.c
567 new file mode 100644
568 index 000000000000..8f820a235364
569 --- /dev/null
570 +++ b/mm/prfile.c
571 @@ -0,0 +1,86 @@
572 +// SPDX-License-Identifier: GPL-2.0
573 +/*
574 + * Mainly for aufs which mmap(2) different file and wants to print different
575 + * path in /proc/PID/maps.
576 + * Call these functions via macros defined in linux/mm.h.
577 + *
578 + * See Documentation/filesystems/aufs/design/06mmap.txt
579 + *
580 + * Copyright (c) 2014-2022 Junjro R. Okajima
581 + * Copyright (c) 2014 Ian Campbell
582 + */
583 +
584 +#include <linux/mm.h>
585 +#include <linux/file.h>
586 +#include <linux/fs.h>
587 +
588 +/* #define PRFILE_TRACE */
589 +static inline void prfile_trace(struct file *f, struct file *pr,
590 +                             const char func[], int line, const char func2[])
591 +{
592 +#ifdef PRFILE_TRACE
593 +       if (pr)
594 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
595 +#endif
596 +}
597 +
598 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
599 +                            int line)
600 +{
601 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
602 +
603 +       prfile_trace(f, pr, func, line, __func__);
604 +       file_update_time(f);
605 +       if (f && pr)
606 +               file_update_time(pr);
607 +}
608 +
609 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
610 +                              int line)
611 +{
612 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
613 +
614 +       prfile_trace(f, pr, func, line, __func__);
615 +       return (f && pr) ? pr : f;
616 +}
617 +
618 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
619 +{
620 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
621 +
622 +       prfile_trace(f, pr, func, line, __func__);
623 +       get_file(f);
624 +       if (f && pr)
625 +               get_file(pr);
626 +}
627 +
628 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
629 +{
630 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
631 +
632 +       prfile_trace(f, pr, func, line, __func__);
633 +       fput(f);
634 +       if (f && pr)
635 +               fput(pr);
636 +}
637 +
638 +#ifndef CONFIG_MMU
639 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
640 +                              int line)
641 +{
642 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
643 +
644 +       prfile_trace(f, pr, func, line, __func__);
645 +       return (f && pr) ? pr : f;
646 +}
647 +
648 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
649 +{
650 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
651 +
652 +       prfile_trace(f, pr, func, line, __func__);
653 +       fput(f);
654 +       if (f && pr)
655 +               fput(pr);
656 +}
657 +#endif /* !CONFIG_MMU */
658 SPDX-License-Identifier: GPL-2.0
659 aufs6.x-rcN standalone patch
660
661 diff --git a/fs/dcache.c b/fs/dcache.c
662 index 78e322b59b8d..7d8fbe5b7ce5 100644
663 --- a/fs/dcache.c
664 +++ b/fs/dcache.c
665 @@ -1318,6 +1318,7 @@ void d_walk(struct dentry *parent, void *data,
666         seq = 1;
667         goto again;
668  }
669 +EXPORT_SYMBOL_GPL(d_walk);
670  
671  struct check_mount {
672         struct vfsmount *mnt;
673 @@ -2880,6 +2881,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
674  
675         write_sequnlock(&rename_lock);
676  }
677 +EXPORT_SYMBOL_GPL(d_exchange);
678  
679  /**
680   * d_ancestor - search for an ancestor
681 diff --git a/fs/exec.c b/fs/exec.c
682 index af4fbb61cd53..aa5161ad2c1f 100644
683 --- a/fs/exec.c
684 +++ b/fs/exec.c
685 @@ -113,6 +113,7 @@ bool path_noexec(const struct path *path)
686         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
687                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
688  }
689 +EXPORT_SYMBOL_GPL(path_noexec);
690  
691  #ifdef CONFIG_USELIB
692  /*
693 diff --git a/fs/fcntl.c b/fs/fcntl.c
694 index 96a562b48ec7..e21a0fcff538 100644
695 --- a/fs/fcntl.c
696 +++ b/fs/fcntl.c
697 @@ -87,6 +87,7 @@ int setfl(int fd, struct file * filp, unsigned int arg)
698   out:
699         return error;
700  }
701 +EXPORT_SYMBOL_GPL(setfl);
702  
703  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
704                       int force)
705 diff --git a/fs/file_table.c b/fs/file_table.c
706 index b991f90571b4..a3ba5726194a 100644
707 --- a/fs/file_table.c
708 +++ b/fs/file_table.c
709 @@ -222,6 +222,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
710         }
711         return ERR_PTR(-ENFILE);
712  }
713 +EXPORT_SYMBOL_GPL(alloc_empty_file);
714  
715  /*
716   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
717 diff --git a/fs/namespace.c b/fs/namespace.c
718 index a4b6ce51848e..c7415f75ab4c 100644
719 --- a/fs/namespace.c
720 +++ b/fs/namespace.c
721 @@ -865,6 +865,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
722  {
723         return check_mnt(real_mount(mnt));
724  }
725 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
726  
727  /*
728   * vfsmount lock must be held for write
729 @@ -2053,6 +2054,7 @@ struct vfsmount *collect_mounts(const struct path *path)
730                 return ERR_CAST(tree);
731         return &tree->mnt;
732  }
733 +EXPORT_SYMBOL_GPL(collect_mounts);
734  
735  static void free_mnt_ns(struct mnt_namespace *);
736  static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *, bool);
737 @@ -2083,6 +2085,7 @@ void drop_collected_mounts(struct vfsmount *mnt)
738         unlock_mount_hash();
739         namespace_unlock();
740  }
741 +EXPORT_SYMBOL_GPL(drop_collected_mounts);
742  
743  static bool has_locked_children(struct mount *mnt, struct dentry *dentry)
744  {
745 @@ -2154,6 +2157,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
746         }
747         return 0;
748  }
749 +EXPORT_SYMBOL_GPL(iterate_mounts);
750  
751  static void lock_mnt_tree(struct mount *mnt)
752  {
753 diff --git a/fs/notify/group.c b/fs/notify/group.c
754 index 1de6631a3925..3008eb37a18d 100644
755 --- a/fs/notify/group.c
756 +++ b/fs/notify/group.c
757 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
758  {
759         refcount_inc(&group->refcnt);
760  }
761 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
762  
763  /*
764   * Drop a reference to a group.  Free it if it's through.
765 diff --git a/fs/open.c b/fs/open.c
766 index a84d21e55c39..35183a95dafd 100644
767 --- a/fs/open.c
768 +++ b/fs/open.c
769 @@ -67,6 +67,7 @@ int do_truncate(struct mnt_idmap *idmap, struct dentry *dentry,
770         inode_unlock(dentry->d_inode);
771         return ret;
772  }
773 +EXPORT_SYMBOL_GPL(do_truncate);
774  
775  long vfs_truncate(const struct path *path, loff_t length)
776  {
777 diff --git a/fs/read_write.c b/fs/read_write.c
778 index d4c036e82b6c..c61361ff5e67 100644
779 --- a/fs/read_write.c
780 +++ b/fs/read_write.c
781 @@ -483,6 +483,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
782         inc_syscr(current);
783         return ret;
784  }
785 +EXPORT_SYMBOL_GPL(vfs_read);
786  
787  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
788  {
789 @@ -598,6 +599,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
790         file_end_write(file);
791         return ret;
792  }
793 +EXPORT_SYMBOL_GPL(vfs_write);
794  
795  /* file_ppos returns &file->f_pos or NULL if file is stream */
796  static inline loff_t *file_ppos(struct file *file)
797 diff --git a/fs/splice.c b/fs/splice.c
798 index 7210ddc5aa81..9e83f46eacc8 100644
799 --- a/fs/splice.c
800 +++ b/fs/splice.c
801 @@ -940,6 +940,7 @@ ssize_t do_splice_from(struct pipe_inode_info *pipe, struct file *out,
802                 return warn_unsupported(out, "write");
803         return out->f_op->splice_write(pipe, out, ppos, len, flags);
804  }
805 +EXPORT_SYMBOL_GPL(do_splice_from);
806  
807  /*
808   * Indicate to the caller that there was a premature EOF when reading from the
809 diff --git a/fs/xattr.c b/fs/xattr.c
810 index 09d927603433..890daee95a7b 100644
811 --- a/fs/xattr.c
812 +++ b/fs/xattr.c
813 @@ -406,6 +406,7 @@ vfs_getxattr_alloc(struct mnt_idmap *idmap, struct dentry *dentry,
814         *xattr_value = value;
815         return error;
816  }
817 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
818  
819  ssize_t
820  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
821 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
822 index 2223ee909c10..d0ac4fc69a31 100644
823 --- a/kernel/locking/lockdep.c
824 +++ b/kernel/locking/lockdep.c
825 @@ -239,6 +239,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
826          */
827         return lock_classes + class_idx;
828  }
829 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
830  #define hlock_class(hlock) lockdep_hlock_class(hlock)
831  
832  #ifdef CONFIG_LOCK_STAT
833 diff --git a/kernel/task_work.c b/kernel/task_work.c
834 index 95a7e1b7f1da..5053670775d3 100644
835 --- a/kernel/task_work.c
836 +++ b/kernel/task_work.c
837 @@ -183,3 +183,4 @@ void task_work_run(void)
838                 } while (work);
839         }
840  }
841 +EXPORT_SYMBOL_GPL(task_work_run);
842 diff --git a/security/security.c b/security/security.c
843 index 7035ee35a393..eb461a68073f 100644
844 --- a/security/security.c
845 +++ b/security/security.c
846 @@ -1835,6 +1835,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
847                 return 0;
848         return call_int_hook(path_rmdir, 0, dir, dentry);
849  }
850 +EXPORT_SYMBOL_GPL(security_path_rmdir);
851  
852  /**
853   * security_path_unlink() - Check if removing a hard link is allowed
854 @@ -1870,6 +1871,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
855                 return 0;
856         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
857  }
858 +EXPORT_SYMBOL_GPL(security_path_symlink);
859  
860  /**
861   * security_path_link - Check if creating a hard link is allowed
862 @@ -1888,6 +1890,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
863                 return 0;
864         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
865  }
866 +EXPORT_SYMBOL_GPL(security_path_link);
867  
868  /**
869   * security_path_rename() - Check if renaming a file is allowed
870 @@ -1949,6 +1952,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
871                 return 0;
872         return call_int_hook(path_chmod, 0, path, mode);
873  }
874 +EXPORT_SYMBOL_GPL(security_path_chmod);
875  
876  /**
877   * security_path_chown() - Check if changing the file's owner/group is allowed
878 @@ -1966,6 +1970,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
879                 return 0;
880         return call_int_hook(path_chown, 0, path, uid, gid);
881  }
882 +EXPORT_SYMBOL_GPL(security_path_chown);
883  
884  /**
885   * security_path_chroot() - Check if changing the root directory is allowed
886 @@ -2195,6 +2200,7 @@ int security_inode_permission(struct inode *inode, int mask)
887                 return 0;
888         return call_int_hook(inode_permission, 0, inode, mask);
889  }
890 +EXPORT_SYMBOL_GPL(security_inode_permission);
891  
892  /**
893   * security_inode_setattr() - Check if setting file attributes is allowed
894 @@ -2667,6 +2673,7 @@ int security_file_permission(struct file *file, int mask)
895  {
896         return call_int_hook(file_permission, 0, file, mask);
897  }
898 +EXPORT_SYMBOL_GPL(security_file_permission);
899  
900  /**
901   * security_file_alloc() - Allocate and init a file's LSM blob
902 @@ -2951,6 +2958,7 @@ int security_file_truncate(struct file *file)
903  {
904         return call_int_hook(file_truncate, 0, file);
905  }
906 +EXPORT_SYMBOL_GPL(security_file_truncate);
907  
908  /**
909   * security_task_alloc() - Allocate a task's LSM blob
910 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
911 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
912 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-11-05 23:02:18.955889283 +0100
913 @@ -0,0 +1,55 @@
914 +What:          /debug/aufs/si_<id>/
915 +Date:          March 2009
916 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
917 +Description:
918 +               Under /debug/aufs, a directory named si_<id> is created
919 +               per aufs mount, where <id> is a unique id generated
920 +               internally.
921 +
922 +What:          /debug/aufs/si_<id>/plink
923 +Date:          Apr 2013
924 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
925 +Description:
926 +               It has three lines and shows the information about the
927 +               pseudo-link. The first line is a single number
928 +               representing a number of buckets. The second line is a
929 +               number of pseudo-links per buckets (separated by a
930 +               blank). The last line is a single number representing a
931 +               total number of psedo-links.
932 +               When the aufs mount option 'noplink' is specified, it
933 +               will show "1\n0\n0\n".
934 +
935 +What:          /debug/aufs/si_<id>/xib
936 +Date:          March 2009
937 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
938 +Description:
939 +               It shows the consumed blocks by xib (External Inode Number
940 +               Bitmap), its block size and file size.
941 +               When the aufs mount option 'noxino' is specified, it
942 +               will be empty. About XINO files, see the aufs manual.
943 +
944 +What:          /debug/aufs/si_<id>/xi<branch-index>
945 +Date:          March 2009
946 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
947 +Description:
948 +               It shows the consumed blocks by xino (External Inode Number
949 +               Translation Table), its link count, block size and file
950 +               size.
951 +               Due to the file size limit, there may exist multiple
952 +               xino files per branch.  In this case, "-N" is added to
953 +               the filename and it corresponds to the index of the
954 +               internal xino array.  "-0" is omitted.
955 +               When the aufs mount option 'noxino' is specified, Those
956 +               entries won't exist.  About XINO files, see the aufs
957 +               manual.
958 +
959 +What:          /debug/aufs/si_<id>/xigen
960 +Date:          March 2009
961 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
962 +Description:
963 +               It shows the consumed blocks by xigen (External Inode
964 +               Generation Table), its block size and file size.
965 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
966 +               be created.
967 +               When the aufs mount option 'noxino' is specified, it
968 +               will be empty. About XINO files, see the aufs manual.
969 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
970 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
971 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-11-05 23:02:18.955889283 +0100
972 @@ -0,0 +1,31 @@
973 +What:          /sys/fs/aufs/si_<id>/
974 +Date:          March 2009
975 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
976 +Description:
977 +               Under /sys/fs/aufs, a directory named si_<id> is created
978 +               per aufs mount, where <id> is a unique id generated
979 +               internally.
980 +
981 +What:          /sys/fs/aufs/si_<id>/br<idx>
982 +Date:          March 2009
983 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
984 +Description:
985 +               It shows the abolute path of a member directory (which
986 +               is called branch) in aufs, and its permission.
987 +
988 +What:          /sys/fs/aufs/si_<id>/brid<idx>
989 +Date:          July 2013
990 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
991 +Description:
992 +               It shows the id of a member directory (which is called
993 +               branch) in aufs.
994 +
995 +What:          /sys/fs/aufs/si_<id>/xi_path
996 +Date:          March 2009
997 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
998 +Description:
999 +               It shows the abolute path of XINO (External Inode Number
1000 +               Bitmap, Translation Table and Generation Table) file
1001 +               even if it is the default path.
1002 +               When the aufs mount option 'noxino' is specified, it
1003 +               will be empty. About XINO files, see the aufs manual.
1004 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1005 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1006 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-11-05 23:02:18.955889283 +0100
1007 @@ -0,0 +1,171 @@
1008 +
1009 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1010 +#
1011 +# This program is free software; you can redistribute it and/or modify
1012 +# it under the terms of the GNU General Public License as published by
1013 +# the Free Software Foundation; either version 2 of the License, or
1014 +# (at your option) any later version.
1015 +#
1016 +# This program is distributed in the hope that it will be useful,
1017 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1018 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1019 +# GNU General Public License for more details.
1020 +#
1021 +# You should have received a copy of the GNU General Public License
1022 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1023 +
1024 +Introduction
1025 +----------------------------------------
1026 +
1027 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1028 +1. abbrev. for "advanced multi-layered unification filesystem".
1029 +2. abbrev. for "another unionfs".
1030 +3. abbrev. for "auf das" in German which means "on the" in English.
1031 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1032 +   But "Filesystem aufs Filesystem" is hard to understand.
1033 +4. abbrev. for "African Urban Fashion Show".
1034 +
1035 +AUFS is a filesystem with features:
1036 +- multi layered stackable unification filesystem, the member directory
1037 +  is called as a branch.
1038 +- branch permission and attribute, 'readonly', 'real-readonly',
1039 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1040 +  combination.
1041 +- internal "file copy-on-write".
1042 +- logical deletion, whiteout.
1043 +- dynamic branch manipulation, adding, deleting and changing permission.
1044 +- allow bypassing aufs, user's direct branch access.
1045 +- external inode number translation table and bitmap which maintains the
1046 +  persistent aufs inode number.
1047 +- seekable directory, including NFS readdir.
1048 +- file mapping, mmap and sharing pages.
1049 +- pseudo-link, hardlink over branches.
1050 +- loopback mounted filesystem as a branch.
1051 +- several policies to select one among multiple writable branches.
1052 +- revert a single systemcall when an error occurs in aufs.
1053 +- and more...
1054 +
1055 +
1056 +Multi Layered Stackable Unification Filesystem
1057 +----------------------------------------------------------------------
1058 +Most people already knows what it is.
1059 +It is a filesystem which unifies several directories and provides a
1060 +merged single directory. When users access a file, the access will be
1061 +passed/re-directed/converted (sorry, I am not sure which English word is
1062 +correct) to the real file on the member filesystem. The member
1063 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1064 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1065 +readonly branch is handled by creating 'whiteout' on the upper writable
1066 +branch.
1067 +
1068 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1069 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1070 +different approaches to implement the merged-view.
1071 +The former tries putting it into VFS, and the latter implements as a
1072 +separate filesystem.
1073 +(If I misunderstand about these implementations, please let me know and
1074 +I shall correct it. Because it is a long time ago when I read their
1075 +source files last time).
1076 +
1077 +UnionMount's approach will be able to small, but may be hard to share
1078 +branches between several UnionMount since the whiteout in it is
1079 +implemented in the inode on branch filesystem and always
1080 +shared. According to Bharata's post, readdir does not seems to be
1081 +finished yet.
1082 +There are several missing features known in this implementations such as
1083 +- for users, the inode number may change silently. eg. copy-up.
1084 +- link(2) may break by copy-up.
1085 +- read(2) may get an obsoleted filedata (fstat(2) too).
1086 +- fcntl(F_SETLK) may be broken by copy-up.
1087 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1088 +  open(O_RDWR).
1089 +
1090 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1091 +merged into mainline. This is another implementation of UnionMount as a
1092 +separated filesystem. All the limitations and known problems which
1093 +UnionMount are equally inherited to "overlay" filesystem.
1094 +
1095 +Unionfs has a longer history. When I started implementing a stackable
1096 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1097 +inode, dentry and file objects and they have an array pointing lower
1098 +same kind objects. After contributing many patches for Unionfs, I
1099 +re-started my project AUFS (Jun 2006).
1100 +
1101 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1102 +implemented my own ideas, approaches and enhancements and it became
1103 +totally different one.
1104 +
1105 +Comparing DM snapshot and fs based implementation
1106 +- the number of bytes to be copied between devices is much smaller.
1107 +- the type of filesystem must be one and only.
1108 +- the fs must be writable, no readonly fs, even for the lower original
1109 +  device. so the compression fs will not be usable. but if we use
1110 +  loopback mount, we may address this issue.
1111 +  for instance,
1112 +       mount /cdrom/squashfs.img /sq
1113 +       losetup /sq/ext2.img
1114 +       losetup /somewhere/cow
1115 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1116 +- it will be difficult (or needs more operations) to extract the
1117 +  difference between the original device and COW.
1118 +- DM snapshot-merge may help a lot when users try merging. in the
1119 +  fs-layer union, users will use rsync(1).
1120 +
1121 +You may want to read my old paper "Filesystems in LiveCD"
1122 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1123 +
1124 +
1125 +Several characters/aspects/persona of aufs
1126 +----------------------------------------------------------------------
1127 +
1128 +Aufs has several characters, aspects or persona.
1129 +1. a filesystem, callee of VFS helper
1130 +2. sub-VFS, caller of VFS helper for branches
1131 +3. a virtual filesystem which maintains persistent inode number
1132 +4. reader/writer of files on branches such like an application
1133 +
1134 +1. Callee of VFS Helper
1135 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1136 +unlink(2) from an application reaches sys_unlink() kernel function and
1137 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1138 +calls filesystem specific unlink operation. Actually aufs implements the
1139 +unlink operation but it behaves like a redirector.
1140 +
1141 +2. Caller of VFS Helper for Branches
1142 +aufs_unlink() passes the unlink request to the branch filesystem as if
1143 +it were called from VFS. So the called unlink operation of the branch
1144 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1145 +every necessary pre/post operation for the branch filesystem.
1146 +- acquire the lock for the parent dir on a branch
1147 +- lookup in a branch
1148 +- revalidate dentry on a branch
1149 +- mnt_want_write() for a branch
1150 +- vfs_unlink() for a branch
1151 +- mnt_drop_write() for a branch
1152 +- release the lock on a branch
1153 +
1154 +3. Persistent Inode Number
1155 +One of the most important issue for a filesystem is to maintain inode
1156 +numbers. This is particularly important to support exporting a
1157 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1158 +backend block device for its own. But some storage is necessary to
1159 +keep and maintain the inode numbers. It may be a large space and may not
1160 +suit to keep in memory. Aufs rents some space from its first writable
1161 +branch filesystem (by default) and creates file(s) on it. These files
1162 +are created by aufs internally and removed soon (currently) keeping
1163 +opened.
1164 +Note: Because these files are removed, they are totally gone after
1165 +      unmounting aufs. It means the inode numbers are not persistent
1166 +      across unmount or reboot. I have a plan to make them really
1167 +      persistent which will be important for aufs on NFS server.
1168 +
1169 +4. Read/Write Files Internally (copy-on-write)
1170 +Because a branch can be readonly, when you write a file on it, aufs will
1171 +"copy-up" it to the upper writable branch internally. And then write the
1172 +originally requested thing to the file. Generally kernel doesn't
1173 +open/read/write file actively. In aufs, even a single write may cause a
1174 +internal "file copy". This behaviour is very similar to cp(1) command.
1175 +
1176 +Some people may think it is better to pass such work to user space
1177 +helper, instead of doing in kernel space. Actually I am still thinking
1178 +about it. But currently I have implemented it in kernel space.
1179 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1180 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1181 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-11-05 23:02:18.955889283 +0100
1182 @@ -0,0 +1,258 @@
1183 +
1184 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1185 +#
1186 +# This program is free software; you can redistribute it and/or modify
1187 +# it under the terms of the GNU General Public License as published by
1188 +# the Free Software Foundation; either version 2 of the License, or
1189 +# (at your option) any later version.
1190 +#
1191 +# This program is distributed in the hope that it will be useful,
1192 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1193 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1194 +# GNU General Public License for more details.
1195 +#
1196 +# You should have received a copy of the GNU General Public License
1197 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1198 +
1199 +Basic Aufs Internal Structure
1200 +
1201 +Superblock/Inode/Dentry/File Objects
1202 +----------------------------------------------------------------------
1203 +As like an ordinary filesystem, aufs has its own
1204 +superblock/inode/dentry/file objects. All these objects have a
1205 +dynamically allocated array and store the same kind of pointers to the
1206 +lower filesystem, branch.
1207 +For example, when you build a union with one readwrite branch and one
1208 +readonly, mounted /au, /rw and /ro respectively.
1209 +- /au = /rw + /ro
1210 +- /ro/fileA exists but /rw/fileA
1211 +
1212 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1213 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1214 +- [0] = NULL (because /rw/fileA doesn't exist)
1215 +- [1] = /ro/fileA
1216 +
1217 +This style of an array is essentially same to the aufs
1218 +superblock/inode/dentry/file objects.
1219 +
1220 +Because aufs supports manipulating branches, ie. add/delete/change
1221 +branches dynamically, these objects has its own generation. When
1222 +branches are changed, the generation in aufs superblock is
1223 +incremented. And a generation in other object are compared when it is
1224 +accessed. When a generation in other objects are obsoleted, aufs
1225 +refreshes the internal array.
1226 +
1227 +
1228 +Superblock
1229 +----------------------------------------------------------------------
1230 +Additionally aufs superblock has some data for policies to select one
1231 +among multiple writable branches, XIB files, pseudo-links and kobject.
1232 +See below in detail.
1233 +About the policies which supports copy-down a directory, see
1234 +wbr_policy.txt too.
1235 +
1236 +
1237 +Branch and XINO(External Inode Number Translation Table)
1238 +----------------------------------------------------------------------
1239 +Every branch has its own xino (external inode number translation table)
1240 +file. The xino file is created and unlinked by aufs internally. When two
1241 +members of a union exist on the same filesystem, they share the single
1242 +xino file.
1243 +The struct of a xino file is simple, just a sequence of aufs inode
1244 +numbers which is indexed by the lower inode number.
1245 +In the above sample, assume the inode number of /ro/fileA is i111 and
1246 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1247 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1248 +
1249 +When the inode numbers are not contiguous, the xino file will be sparse
1250 +which has a hole in it and doesn't consume as much disk space as it
1251 +might appear. If your branch filesystem consumes disk space for such
1252 +holes, then you should specify 'xino=' option at mounting aufs.
1253 +
1254 +Aufs has a mount option to free the disk blocks for such holes in XINO
1255 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1256 +meet a problem of disk shortage due to XINO files, then you should try
1257 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1258 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1259 +the holes in XINO files.
1260 +
1261 +Also a writable branch has three kinds of "whiteout bases". All these
1262 +are existed when the branch is joined to aufs, and their names are
1263 +whiteout-ed doubly, so that users will never see their names in aufs
1264 +hierarchy.
1265 +1. a regular file which will be hardlinked to all whiteouts.
1266 +2. a directory to store a pseudo-link.
1267 +3. a directory to store an "orphan"-ed file temporary.
1268 +
1269 +1. Whiteout Base
1270 +   When you remove a file on a readonly branch, aufs handles it as a
1271 +   logical deletion and creates a whiteout on the upper writable branch
1272 +   as a hardlink of this file in order not to consume inode on the
1273 +   writable branch.
1274 +2. Pseudo-link Dir
1275 +   See below, Pseudo-link.
1276 +3. Step-Parent Dir
1277 +   When "fileC" exists on the lower readonly branch only and it is
1278 +   opened and removed with its parent dir, and then user writes
1279 +   something into it, then aufs copies-up fileC to this
1280 +   directory. Because there is no other dir to store fileC. After
1281 +   creating a file under this dir, the file is unlinked.
1282 +
1283 +Because aufs supports manipulating branches, ie. add/delete/change
1284 +dynamically, a branch has its own id. When the branch order changes,
1285 +aufs finds the new index by searching the branch id.
1286 +
1287 +
1288 +Pseudo-link
1289 +----------------------------------------------------------------------
1290 +Assume "fileA" exists on the lower readonly branch only and it is
1291 +hardlinked to "fileB" on the branch. When you write something to fileA,
1292 +aufs copies-up it to the upper writable branch. Additionally aufs
1293 +creates a hardlink under the Pseudo-link Directory of the writable
1294 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1295 +simple list. If fileB is read after unlinking fileA, aufs returns
1296 +filedata from the pseudo-link instead of the lower readonly
1297 +branch. Because the pseudo-link is based upon the inode, to keep the
1298 +inode number by xino (see above) is essentially necessary.
1299 +
1300 +All the hardlinks under the Pseudo-link Directory of the writable branch
1301 +should be restored in a proper location later. Aufs provides a utility
1302 +to do this. The userspace helpers executed at remounting and unmounting
1303 +aufs by default.
1304 +During this utility is running, it puts aufs into the pseudo-link
1305 +maintenance mode. In this mode, only the process which began the
1306 +maintenance mode (and its child processes) is allowed to operate in
1307 +aufs. Some other processes which are not related to the pseudo-link will
1308 +be allowed to run too, but the rest have to return an error or wait
1309 +until the maintenance mode ends. If a process already acquires an inode
1310 +mutex (in VFS), it has to return an error.
1311 +
1312 +
1313 +XIB(external inode number bitmap)
1314 +----------------------------------------------------------------------
1315 +Addition to the xino file per a branch, aufs has an external inode number
1316 +bitmap in a superblock object. It is also an internal file such like a
1317 +xino file.
1318 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1319 +not.
1320 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1321 +
1322 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1323 +reduce the number of consumed disk blocks for these files.
1324 +
1325 +
1326 +Virtual or Vertical Dir, and Readdir in Userspace
1327 +----------------------------------------------------------------------
1328 +In order to support multiple layers (branches), aufs readdir operation
1329 +constructs a virtual dir block on memory. For readdir, aufs calls
1330 +vfs_readdir() internally for each dir on branches, merges their entries
1331 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1332 +object. So the file object has its entry list until it is closed. The
1333 +entry list will be updated when the file position is zero and becomes
1334 +obsoleted. This decision is made in aufs automatically.
1335 +
1336 +The dynamically allocated memory block for the name of entries has a
1337 +unit of 512 bytes (by default) and stores the names contiguously (no
1338 +padding). Another block for each entry is handled by kmem_cache too.
1339 +During building dir blocks, aufs creates hash list and judging whether
1340 +the entry is whiteouted by its upper branch or already listed.
1341 +The merged result is cached in the corresponding inode object and
1342 +maintained by a customizable life-time option.
1343 +
1344 +Some people may call it can be a security hole or invite DoS attack
1345 +since the opened and once readdir-ed dir (file object) holds its entry
1346 +list and becomes a pressure for system memory. But I'd say it is similar
1347 +to files under /proc or /sys. The virtual files in them also holds a
1348 +memory page (generally) while they are opened. When an idea to reduce
1349 +memory for them is introduced, it will be applied to aufs too.
1350 +For those who really hate this situation, I've developed readdir(3)
1351 +library which operates this merging in userspace. You just need to set
1352 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1353 +kernel space for readdir(3).
1354 +
1355 +
1356 +Workqueue
1357 +----------------------------------------------------------------------
1358 +Aufs sometimes requires privilege access to a branch. For instance,
1359 +in copy-up/down operation. When a user process is going to make changes
1360 +to a file which exists in the lower readonly branch only, and the mode
1361 +of one of ancestor directories may not be writable by a user
1362 +process. Here aufs copy-up the file with its ancestors and they may
1363 +require privilege to set its owner/group/mode/etc.
1364 +This is a typical case of a application character of aufs (see
1365 +Introduction).
1366 +
1367 +Aufs uses workqueue synchronously for this case. It creates its own
1368 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1369 +passes the request to call mkdir or write (for example), and wait for
1370 +its completion. This approach solves a problem of a signal handler
1371 +simply.
1372 +If aufs didn't adopt the workqueue and changed the privilege of the
1373 +process, then the process may receive the unexpected SIGXFSZ or other
1374 +signals.
1375 +
1376 +Also aufs uses the system global workqueue ("events" kernel thread) too
1377 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1378 +whiteout base and etc. This is unrelated to a privilege.
1379 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1380 +superblock at the beginning, at the same time waits for the completion
1381 +of all queued asynchronous tasks.
1382 +
1383 +
1384 +Whiteout
1385 +----------------------------------------------------------------------
1386 +The whiteout in aufs is very similar to Unionfs's. That is represented
1387 +by its filename. UnionMount takes an approach of a file mode, but I am
1388 +afraid several utilities (find(1) or something) will have to support it.
1389 +
1390 +Basically the whiteout represents "logical deletion" which stops aufs to
1391 +lookup further, but also it represents "dir is opaque" which also stop
1392 +further lookup.
1393 +
1394 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1395 +In order to make several functions in a single systemcall to be
1396 +revertible, aufs adopts an approach to rename a directory to a temporary
1397 +unique whiteouted name.
1398 +For example, in rename(2) dir where the target dir already existed, aufs
1399 +renames the target dir to a temporary unique whiteouted name before the
1400 +actual rename on a branch, and then handles other actions (make it opaque,
1401 +update the attributes, etc). If an error happens in these actions, aufs
1402 +simply renames the whiteouted name back and returns an error. If all are
1403 +succeeded, aufs registers a function to remove the whiteouted unique
1404 +temporary name completely and asynchronously to the system global
1405 +workqueue.
1406 +
1407 +
1408 +Copy-up
1409 +----------------------------------------------------------------------
1410 +It is a well-known feature or concept.
1411 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1412 +internally and makes change to the new file on the upper writable branch.
1413 +When the trigger systemcall does not update the timestamps of the parent
1414 +dir, aufs reverts it after copy-up.
1415 +
1416 +
1417 +Move-down (aufs3.9 and later)
1418 +----------------------------------------------------------------------
1419 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1420 +the lower readonly branch to the upper writable branch when a user
1421 +changes something about the file.
1422 +"Move-down" is an opposite action of copy-up. Basically this action is
1423 +ran manually instead of automatically and internally.
1424 +For desgin and implementation, aufs has to consider these issues.
1425 +- whiteout for the file may exist on the lower branch.
1426 +- ancestor directories may not exist on the lower branch.
1427 +- diropq for the ancestor directories may exist on the upper branch.
1428 +- free space on the lower branch will reduce.
1429 +- another access to the file may happen during moving-down, including
1430 +  UDBA (see "Revalidate Dentry and UDBA").
1431 +- the file should not be hard-linked nor pseudo-linked. they should be
1432 +  handled by auplink utility later.
1433 +
1434 +Sometimes users want to move-down a file from the upper writable branch
1435 +to the lower readonly or writable branch. For instance,
1436 +- the free space of the upper writable branch is going to run out.
1437 +- create a new intermediate branch between the upper and lower branch.
1438 +- etc.
1439 +
1440 +For this purpose, use "aumvdown" command in aufs-util.git.
1441 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1442 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1443 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-11-05 23:02:18.955889283 +0100
1444 @@ -0,0 +1,85 @@
1445 +
1446 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1447 +#
1448 +# This program is free software; you can redistribute it and/or modify
1449 +# it under the terms of the GNU General Public License as published by
1450 +# the Free Software Foundation; either version 2 of the License, or
1451 +# (at your option) any later version.
1452 +#
1453 +# This program is distributed in the hope that it will be useful,
1454 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1455 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1456 +# GNU General Public License for more details.
1457 +#
1458 +# You should have received a copy of the GNU General Public License
1459 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1460 +
1461 +Support for a branch who has its ->atomic_open()
1462 +----------------------------------------------------------------------
1463 +The filesystems who implement its ->atomic_open() are not majority. For
1464 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1465 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1466 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1467 +sure whether all filesystems who have ->atomic_open() behave like this,
1468 +but NFSv4 surely returns the error.
1469 +
1470 +In order to support ->atomic_open() for aufs, there are a few
1471 +approaches.
1472 +
1473 +A. Introduce aufs_atomic_open()
1474 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1475 +     branch fs.
1476 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1477 +   an aufs user Pip Cet's approach
1478 +   - calls aufs_create(), VFS finish_open() and notify_change().
1479 +   - pass fake-mode to finish_open(), and then correct the mode by
1480 +     notify_change().
1481 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1482 +   - no aufs_atomic_open().
1483 +   - aufs_lookup() registers the TID to an aufs internal object.
1484 +   - aufs_create() does nothing when the matching TID is registered, but
1485 +     registers the mode.
1486 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1487 +     TID is registered.
1488 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1489 +   credential
1490 +   - no aufs_atomic_open().
1491 +   - aufs_create() registers the TID to an internal object. this info
1492 +     represents "this process created this file just now."
1493 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1494 +     registered TID and re-try open() with superuser's credential.
1495 +
1496 +Pros and cons for each approach.
1497 +
1498 +A.
1499 +   - straightforward but highly depends upon VFS internal.
1500 +   - the atomic behavaiour is kept.
1501 +   - some of parameters such as nameidata are hard to reproduce for
1502 +     branch fs.
1503 +   - large overhead.
1504 +B.
1505 +   - easy to implement.
1506 +   - the atomic behavaiour is lost.
1507 +C.
1508 +   - the atomic behavaiour is kept.
1509 +   - dirty and tricky.
1510 +   - VFS checks whether the file is created correctly after calling
1511 +     ->create(), which means this approach doesn't work.
1512 +D.
1513 +   - easy to implement.
1514 +   - the atomic behavaiour is lost.
1515 +   - to open a file with superuser's credential and give it to a user
1516 +     process is a bad idea, since the file object keeps the credential
1517 +     in it. It may affect LSM or something. This approach doesn't work
1518 +     either.
1519 +
1520 +The approach A is ideal, but it hard to implement. So here is a
1521 +variation of A, which is to be implemented.
1522 +
1523 +A-1. Introduce aufs_atomic_open()
1524 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1525 +       vfs_create() and finish_open().
1526 +     - the demerit is that the several checks after branch fs
1527 +       ->atomic_open() are lost. in the ordinary case, the checks are
1528 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1529 +       be implemented in aufs, but not all I am afraid.
1530 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1531 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1532 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-11-05 23:02:18.959222617 +0100
1533 @@ -0,0 +1,113 @@
1534 +
1535 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1536 +#
1537 +# This program is free software; you can redistribute it and/or modify
1538 +# it under the terms of the GNU General Public License as published by
1539 +# the Free Software Foundation; either version 2 of the License, or
1540 +# (at your option) any later version.
1541 +#
1542 +# This program is distributed in the hope that it will be useful,
1543 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1544 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1545 +# GNU General Public License for more details.
1546 +#
1547 +# You should have received a copy of the GNU General Public License
1548 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1549 +
1550 +Lookup in a Branch
1551 +----------------------------------------------------------------------
1552 +Since aufs has a character of sub-VFS (see Introduction), it operates
1553 +lookup for branches as VFS does. It may be a heavy work. But almost all
1554 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1555 +directly connected to its parent. Digging down the directory hierarchy
1556 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1557 +aufs calls it.
1558 +
1559 +When a branch is a remote filesystem, aufs basically relies upon its
1560 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1561 +them.
1562 +For d_revalidate, aufs implements three levels of revalidate tests. See
1563 +"Revalidate Dentry and UDBA" in detail.
1564 +
1565 +
1566 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1567 +----------------------------------------------------------------------
1568 +Let's try case study.
1569 +- aufs has two branches, upper readwrite and lower readonly.
1570 +  /au = /rw + /ro
1571 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1572 +- user invoked "chmod a+rx /au/dirA"
1573 +- the internal copy-up is activated and "/rw/dirA" is created and its
1574 +  permission bits are set to world readable.
1575 +- then "/au/dirA" becomes world readable?
1576 +
1577 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1578 +or it may be a natively readonly filesystem. If aufs respects the lower
1579 +branch, it should not respond readdir request from other users. But user
1580 +allowed it by chmod. Should really aufs rejects showing the entries
1581 +under /ro/dirA?
1582 +
1583 +To be honest, I don't have a good solution for this case. So aufs
1584 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1585 +users.
1586 +When dirperm1 is specified, aufs checks only the highest one for the
1587 +directory permission, and shows the entries. Otherwise, as usual, checks
1588 +every dir existing on all branches and rejects the request.
1589 +
1590 +As a side effect, dirperm1 option improves the performance of aufs
1591 +because the number of permission check is reduced when the number of
1592 +branch is many.
1593 +
1594 +
1595 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1596 +----------------------------------------------------------------------
1597 +Generally VFS helpers re-validate a dentry as a part of lookup.
1598 +0. digging down the directory hierarchy.
1599 +1. lock the parent dir by its i_mutex.
1600 +2. lookup the final (child) entry.
1601 +3. revalidate it.
1602 +4. call the actual operation (create, unlink, etc.)
1603 +5. unlock the parent dir
1604 +
1605 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1606 +called. Actually aufs implements it and checks the dentry on a branch is
1607 +still valid.
1608 +But it is not enough. Because aufs has to release the lock for the
1609 +parent dir on a branch at the end of ->lookup() (step 2) and
1610 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1611 +held by VFS.
1612 +If the file on a branch is changed directly, eg. bypassing aufs, after
1613 +aufs released the lock, then the subsequent operation may cause
1614 +something unpleasant result.
1615 +
1616 +This situation is a result of VFS architecture, ->lookup() and
1617 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1618 +design from VFS's point of view. It is just not suitable for sub-VFS
1619 +character in aufs.
1620 +
1621 +Aufs supports such case by three level of revalidation which is
1622 +selectable by user.
1623 +1. Simple Revalidate
1624 +   Addition to the native flow in VFS's, confirm the child-parent
1625 +   relationship on the branch just after locking the parent dir on the
1626 +   branch in the "actual operation" (step 4). When this validation
1627 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1628 +   checks the validation of the dentry on branches.
1629 +2. Monitor Changes Internally by Inotify/Fsnotify
1630 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1631 +   the dentry on the branch, and returns EBUSY if it finds different
1632 +   dentry.
1633 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1634 +   during it is in cache. When the event is notified, aufs registers a
1635 +   function to kernel 'events' thread by schedule_work(). And the
1636 +   function sets some special status to the cached aufs dentry and inode
1637 +   private data. If they are not cached, then aufs has nothing to
1638 +   do. When the same file is accessed through aufs (step 0-3) later,
1639 +   aufs will detect the status and refresh all necessary data.
1640 +   In this mode, aufs has to ignore the event which is fired by aufs
1641 +   itself.
1642 +3. No Extra Validation
1643 +   This is the simplest test and doesn't add any additional revalidation
1644 +   test, and skip the revalidation in step 4. It is useful and improves
1645 +   aufs performance when system surely hide the aufs branches from user,
1646 +   by over-mounting something (or another method).
1647 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1648 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1649 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-11-05 23:02:18.959222617 +0100
1650 @@ -0,0 +1,74 @@
1651 +
1652 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1653 +#
1654 +# This program is free software; you can redistribute it and/or modify
1655 +# it under the terms of the GNU General Public License as published by
1656 +# the Free Software Foundation; either version 2 of the License, or
1657 +# (at your option) any later version.
1658 +#
1659 +# This program is distributed in the hope that it will be useful,
1660 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1661 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1662 +# GNU General Public License for more details.
1663 +#
1664 +# You should have received a copy of the GNU General Public License
1665 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1666 +
1667 +Branch Manipulation
1668 +
1669 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1670 +and changing its permission/attribute, there are a lot of works to do.
1671 +
1672 +
1673 +Add a Branch
1674 +----------------------------------------------------------------------
1675 +o Confirm the adding dir exists outside of aufs, including loopback
1676 +  mount, and its various attributes.
1677 +o Initialize the xino file and whiteout bases if necessary.
1678 +  See struct.txt.
1679 +
1680 +o Check the owner/group/mode of the directory
1681 +  When the owner/group/mode of the adding directory differs from the
1682 +  existing branch, aufs issues a warning because it may impose a
1683 +  security risk.
1684 +  For example, when a upper writable branch has a world writable empty
1685 +  top directory, a malicious user can create any files on the writable
1686 +  branch directly, like copy-up and modify manually. If something like
1687 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1688 +  writable branch, and the writable branch is world-writable, then a
1689 +  malicious guy may create /etc/passwd on the writable branch directly
1690 +  and the infected file will be valid in aufs.
1691 +  I am afraid it can be a security issue, but aufs can do nothing except
1692 +  producing a warning.
1693 +
1694 +
1695 +Delete a Branch
1696 +----------------------------------------------------------------------
1697 +o Confirm the deleting branch is not busy
1698 +  To be general, there is one merit to adopt "remount" interface to
1699 +  manipulate branches. It is to discard caches. At deleting a branch,
1700 +  aufs checks the still cached (and connected) dentries and inodes. If
1701 +  there are any, then they are all in-use. An inode without its
1702 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1703 +
1704 +  For the cached one, aufs checks whether the same named entry exists on
1705 +  other branches.
1706 +  If the cached one is a directory, because aufs provides a merged view
1707 +  to users, as long as one dir is left on any branch aufs can show the
1708 +  dir to users. In this case, the branch can be removed from aufs.
1709 +  Otherwise aufs rejects deleting the branch.
1710 +
1711 +  If any file on the deleting branch is opened by aufs, then aufs
1712 +  rejects deleting.
1713 +
1714 +
1715 +Modify the Permission of a Branch
1716 +----------------------------------------------------------------------
1717 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1718 +  See struct.txt.
1719 +
1720 +o rw --> ro: Confirm the modifying branch is not busy
1721 +  Aufs rejects the request if any of these conditions are true.
1722 +  - a file on the branch is mmap-ed.
1723 +  - a regular file on the branch is opened for write and there is no
1724 +    same named entry on the upper branch.
1725 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1726 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1727 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-11-05 23:02:18.959222617 +0100
1728 @@ -0,0 +1,64 @@
1729 +
1730 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1731 +#
1732 +# This program is free software; you can redistribute it and/or modify
1733 +# it under the terms of the GNU General Public License as published by
1734 +# the Free Software Foundation; either version 2 of the License, or
1735 +# (at your option) any later version.
1736 +#
1737 +# This program is distributed in the hope that it will be useful,
1738 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1739 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1740 +# GNU General Public License for more details.
1741 +#
1742 +# You should have received a copy of the GNU General Public License
1743 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1744 +
1745 +Policies to Select One among Multiple Writable Branches
1746 +----------------------------------------------------------------------
1747 +When the number of writable branch is more than one, aufs has to decide
1748 +the target branch for file creation or copy-up. By default, the highest
1749 +writable branch which has the parent (or ancestor) dir of the target
1750 +file is chosen (top-down-parent policy).
1751 +By user's request, aufs implements some other policies to select the
1752 +writable branch, for file creation several policies, round-robin,
1753 +most-free-space, and other policies. For copy-up, top-down-parent,
1754 +bottom-up-parent, bottom-up and others.
1755 +
1756 +As expected, the round-robin policy selects the branch in circular. When
1757 +you have two writable branches and creates 10 new files, 5 files will be
1758 +created for each branch. mkdir(2) systemcall is an exception. When you
1759 +create 10 new directories, all will be created on the same branch.
1760 +And the most-free-space policy selects the one which has most free
1761 +space among the writable branches. The amount of free space will be
1762 +checked by aufs internally, and users can specify its time interval.
1763 +
1764 +The policies for copy-up is more simple,
1765 +top-down-parent is equivalent to the same named on in create policy,
1766 +bottom-up-parent selects the writable branch where the parent dir
1767 +exists and the nearest upper one from the copyup-source,
1768 +bottom-up selects the nearest upper writable branch from the
1769 +copyup-source, regardless the existence of the parent dir.
1770 +
1771 +There are some rules or exceptions to apply these policies.
1772 +- If there is a readonly branch above the policy-selected branch and
1773 +  the parent dir is marked as opaque (a variation of whiteout), or the
1774 +  target (creating) file is whiteout-ed on the upper readonly branch,
1775 +  then the result of the policy is ignored and the target file will be
1776 +  created on the nearest upper writable branch than the readonly branch.
1777 +- If there is a writable branch above the policy-selected branch and
1778 +  the parent dir is marked as opaque or the target file is whiteouted
1779 +  on the branch, then the result of the policy is ignored and the target
1780 +  file will be created on the highest one among the upper writable
1781 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1782 +  it as usual.
1783 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1784 +  They try selecting the branch where the source exists as possible
1785 +  since copyup a large file will take long time. If it can't be,
1786 +  ie. the branch where the source exists is readonly, then they will
1787 +  follow the copyup policy.
1788 +- There is an exception for rename(2) when the target exists.
1789 +  If the rename target exists, aufs compares the index of the branches
1790 +  where the source and the target exists and selects the higher
1791 +  one. If the selected branch is readonly, then aufs follows the
1792 +  copyup policy.
1793 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1794 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1795 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-11-05 23:02:18.959222617 +0100
1796 @@ -0,0 +1,44 @@
1797 +
1798 +// to view this graph, run dot(1) command in GRAPHVIZ.
1799 +//
1800 +// This program is free software; you can redistribute it and/or modify
1801 +// it under the terms of the GNU General Public License as published by
1802 +// the Free Software Foundation; either version 2 of the License, or
1803 +// (at your option) any later version.
1804 +//
1805 +// This program is distributed in the hope that it will be useful,
1806 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1807 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1808 +// GNU General Public License for more details.
1809 +//
1810 +// You should have received a copy of the GNU General Public License
1811 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1812 +
1813 +digraph G {
1814 +node [shape=box];
1815 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1816 +
1817 +node [shape=oval];
1818 +
1819 +aufs_rename -> whinfo [label="store/remove"];
1820 +
1821 +node [shape=oval];
1822 +inode_list [label="h_inum list in branch\ncache"];
1823 +
1824 +node [shape=box];
1825 +whinode [label="h_inum list file"];
1826 +
1827 +node [shape=oval];
1828 +brmgmt [label="br_add/del/mod/umount"];
1829 +
1830 +brmgmt -> inode_list [label="create/remove"];
1831 +brmgmt -> whinode [label="load/store"];
1832 +
1833 +inode_list -> whinode [style=dashed,dir=both];
1834 +
1835 +aufs_rename -> inode_list [label="add/del"];
1836 +
1837 +aufs_lookup -> inode_list [label="search"];
1838 +
1839 +aufs_lookup -> whinfo [label="load/remove"];
1840 +}
1841 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1842 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1843 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-11-05 23:02:18.959222617 +0100
1844 @@ -0,0 +1,102 @@
1845 +
1846 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1847 +#
1848 +# This program is free software; you can redistribute it and/or modify
1849 +# it under the terms of the GNU General Public License as published by
1850 +# the Free Software Foundation; either version 2 of the License, or
1851 +# (at your option) any later version.
1852 +#
1853 +# This program is distributed in the hope that it will be useful,
1854 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1855 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1856 +# GNU General Public License for more details.
1857 +#
1858 +# You should have received a copy of the GNU General Public License
1859 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1860 +
1861 +Special handling for renaming a directory (DIRREN)
1862 +----------------------------------------------------------------------
1863 +First, let's assume we have a simple usecase.
1864 +
1865 +- /u = /rw + /ro
1866 +- /rw/dirA exists
1867 +- /ro/dirA and /ro/dirA/file exist too
1868 +- there is no dirB on both branches
1869 +- a user issues rename("dirA", "dirB")
1870 +
1871 +Now, what should aufs behave against this rename(2)?
1872 +There are a few possible cases.
1873 +
1874 +A. returns EROFS.
1875 +   since dirA exists on a readonly branch which cannot be renamed.
1876 +B. returns EXDEV.
1877 +   it is possible to copy-up dirA (only the dir itself), but the child
1878 +   entries ("file" in this case) should not be. it must be a bad
1879 +   approach to copy-up recursively.
1880 +C. returns a success.
1881 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1882 +   is a violation of aufs' policy.
1883 +D. construct an extra information which indicates that /ro/dirA should
1884 +   be handled as the name of dirB.
1885 +   overlayfs has a similar feature called REDIRECT.
1886 +
1887 +Until now, aufs implements the case B only which returns EXDEV, and
1888 +expects the userspace application behaves like mv(1) which tries
1889 +issueing rename(2) recursively.
1890 +
1891 +A new aufs feature called DIRREN is introduced which implements the case
1892 +D. There are several "extra information" added.
1893 +
1894 +1. detailed info per renamed directory
1895 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1896 +2. the inode-number list of directories on a branch
1897 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1898 +
1899 +The filename of "detailed info per directory" represents the lower
1900 +branch, and its format is
1901 +- a type of the branch id
1902 +  one of these.
1903 +  + uuid (not implemented yet)
1904 +  + fsid
1905 +  + dev
1906 +- the inode-number of the branch root dir
1907 +
1908 +And it contains these info in a single regular file.
1909 +- magic number
1910 +- branch's inode-number of the logically renamed dir
1911 +- the name of the before-renamed dir
1912 +
1913 +The "detailed info per directory" file is created in aufs rename(2), and
1914 +loaded in any lookup.
1915 +The info is considered in lookup for the matching case only. Here
1916 +"matching" means that the root of branch (in the info filename) is same
1917 +to the current looking-up branch. After looking-up the before-renamed
1918 +name, the inode-number is compared. And the matched dentry is used.
1919 +
1920 +The "inode-number list of directories" is a regular file which contains
1921 +simply the inode-numbers on the branch. The file is created or updated
1922 +in removing the branch, and loaded in adding the branch. Its lifetime is
1923 +equal to the branch.
1924 +The list is referred in lookup, and when the current target inode is
1925 +found in the list, the aufs tries loading the "detailed info per
1926 +directory" and get the changed and valid name of the dir.
1927 +
1928 +Theoretically these "extra informaiton" may be able to be put into XATTR
1929 +in the dir inode. But aufs doesn't choose this way because
1930 +1. XATTR may not be supported by the branch (or its configuration)
1931 +2. XATTR may have its size limit.
1932 +3. XATTR may be less easy to convert than a regular file, when the
1933 +   format of the info is changed in the future.
1934 +At the same time, I agree that the regular file approach is much slower
1935 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1936 +better approach.
1937 +
1938 +This DIRREN feature is enabled by aufs configuration, and is activated
1939 +by a new mount option.
1940 +
1941 +For the more complicated case, there is a work with UDBA option, which
1942 +is to dected the direct access to the branches (by-passing aufs) and to
1943 +maintain the cashes in aufs. Since a single cached aufs dentry may
1944 +contains two names, before- and after-rename, the name comparision in
1945 +UDBA handler may not work correctly. In this case, the behaviour will be
1946 +equivalen to udba=reval case.
1947 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1948 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1949 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-11-05 23:02:18.959222617 +0100
1950 @@ -0,0 +1,118 @@
1951 +
1952 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1953 +#
1954 +# This program is free software; you can redistribute it and/or modify
1955 +# it under the terms of the GNU General Public License as published by
1956 +# the Free Software Foundation; either version 2 of the License, or
1957 +# (at your option) any later version.
1958 +#
1959 +# This program is distributed in the hope that it will be useful,
1960 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1961 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1962 +# GNU General Public License for more details.
1963 +#
1964 +# You should have received a copy of the GNU General Public License
1965 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1966 +
1967 +File-based Hierarchical Storage Management (FHSM)
1968 +----------------------------------------------------------------------
1969 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1970 +storage world. Aufs provides this feature as file-based with multiple
1971 +writable branches, based upon the principle of "Colder, the Lower".
1972 +Here the word "colder" means that the less used files, and "lower" means
1973 +that the position in the order of the stacked branches vertically.
1974 +These multiple writable branches are prioritized, ie. the topmost one
1975 +should be the fastest drive and be used heavily.
1976 +
1977 +o Characters in aufs FHSM story
1978 +- aufs itself and a new branch attribute.
1979 +- a new ioctl interface to move-down and to establish a connection with
1980 +  the daemon ("move-down" is a converse of "copy-up").
1981 +- userspace tool and daemon.
1982 +
1983 +The userspace daemon establishes a connection with aufs and waits for
1984 +the notification. The notified information is very similar to struct
1985 +statfs containing the number of consumed blocks and inodes.
1986 +When the consumed blocks/inodes of a branch exceeds the user-specified
1987 +upper watermark, the daemon activates its move-down process until the
1988 +consumed blocks/inodes reaches the user-specified lower watermark.
1989 +
1990 +The actual move-down is done by aufs based upon the request from
1991 +user-space since we need to maintain the inode number and the internal
1992 +pointer arrays in aufs.
1993 +
1994 +Currently aufs FHSM handles the regular files only. Additionally they
1995 +must not be hard-linked nor pseudo-linked.
1996 +
1997 +
1998 +o Cowork of aufs and the user-space daemon
1999 +  During the userspace daemon established the connection, aufs sends a
2000 +  small notification to it whenever aufs writes something into the
2001 +  writable branch. But it may cost high since aufs issues statfs(2)
2002 +  internally. So user can specify a new option to cache the
2003 +  info. Actually the notification is controlled by these factors.
2004 +  + the specified cache time.
2005 +  + classified as "force" by aufs internally.
2006 +  Until the specified time expires, aufs doesn't send the info
2007 +  except the forced cases. When aufs decide forcing, the info is always
2008 +  notified to userspace.
2009 +  For example, the number of free inodes is generally large enough and
2010 +  the shortage of it happens rarely. So aufs doesn't force the
2011 +  notification when creating a new file, directory and others. This is
2012 +  the typical case which aufs doesn't force.
2013 +  When aufs writes the actual filedata and the files consumes any of new
2014 +  blocks, the aufs forces notifying.
2015 +
2016 +
2017 +o Interfaces in aufs
2018 +- New branch attribute.
2019 +  + fhsm
2020 +    Specifies that the branch is managed by FHSM feature. In other word,
2021 +    participant in the FHSM.
2022 +    When nofhsm is set to the branch, it will not be the source/target
2023 +    branch of the move-down operation. This attribute is set
2024 +    independently from coo and moo attributes, and if you want full
2025 +    FHSM, you should specify them as well.
2026 +- New mount option.
2027 +  + fhsm_sec
2028 +    Specifies a second to suppress many less important info to be
2029 +    notified.
2030 +- New ioctl.
2031 +  + AUFS_CTL_FHSM_FD
2032 +    create a new file descriptor which userspace can read the notification
2033 +    (a subset of struct statfs) from aufs.
2034 +- Module parameter 'brs'
2035 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2036 +  be set.
2037 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2038 +  When there are two or more branches with fhsm attributes,
2039 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2040 +  terminates it. As a result of remounting and branch-manipulation, the
2041 +  number of branches with fhsm attribute can be one. In this case,
2042 +  /sbin/mount.aufs will terminate the user-space daemon.
2043 +
2044 +
2045 +Finally the operation is done as these steps in kernel-space.
2046 +- make sure that,
2047 +  + no one else is using the file.
2048 +  + the file is not hard-linked.
2049 +  + the file is not pseudo-linked.
2050 +  + the file is a regular file.
2051 +  + the parent dir is not opaqued.
2052 +- find the target writable branch.
2053 +- make sure the file is not whiteout-ed by the upper (than the target)
2054 +  branch.
2055 +- make the parent dir on the target branch.
2056 +- mutex lock the inode on the branch.
2057 +- unlink the whiteout on the target branch (if exists).
2058 +- lookup and create the whiteout-ed temporary name on the target branch.
2059 +- copy the file as the whiteout-ed temporary name on the target branch.
2060 +- rename the whiteout-ed temporary name to the original name.
2061 +- unlink the file on the source branch.
2062 +- maintain the internal pointer array and the external inode number
2063 +  table (XINO).
2064 +- maintain the timestamps and other attributes of the parent dir and the
2065 +  file.
2066 +
2067 +And of course, in every step, an error may happen. So the operation
2068 +should restore the original file state after an error happens.
2069 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2070 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2071 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-11-05 23:02:18.959222617 +0100
2072 @@ -0,0 +1,72 @@
2073 +
2074 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2075 +#
2076 +# This program is free software; you can redistribute it and/or modify
2077 +# it under the terms of the GNU General Public License as published by
2078 +# the Free Software Foundation; either version 2 of the License, or
2079 +# (at your option) any later version.
2080 +#
2081 +# This program is distributed in the hope that it will be useful,
2082 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2083 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2084 +# GNU General Public License for more details.
2085 +#
2086 +# You should have received a copy of the GNU General Public License
2087 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2088 +
2089 +mmap(2) -- File Memory Mapping
2090 +----------------------------------------------------------------------
2091 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2092 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2093 +->mmap().
2094 +This approach is simple and good, but there is one problem.
2095 +Under /proc, several entries show the mmapped files by its path (with
2096 +device and inode number), and the printed path will be the path on the
2097 +branch fs's instead of virtual aufs's.
2098 +This is not a problem in most cases, but some utilities lsof(1) (and its
2099 +user) may expect the path on aufs.
2100 +
2101 +To address this issue, aufs adds a new member called vm_prfile in struct
2102 +vm_area_struct (and struct vm_region). The original vm_file points to
2103 +the file on the branch fs in order to handle everything correctly as
2104 +usual. The new vm_prfile points to a virtual file in aufs, and the
2105 +show-functions in procfs refers to vm_prfile if it is set.
2106 +Also we need to maintain several other places where touching vm_file
2107 +such like
2108 +- fork()/clone() copies vma and the reference count of vm_file is
2109 +  incremented.
2110 +- merging vma maintains the ref count too.
2111 +
2112 +This is not a good approach. It just fakes the printed path. But it
2113 +leaves all behaviour around f_mapping unchanged. This is surely an
2114 +advantage.
2115 +Actually aufs had adopted another complicated approach which calls
2116 +generic_file_mmap() and handles struct vm_operations_struct. In this
2117 +approach, aufs met a hard problem and I could not solve it without
2118 +switching the approach.
2119 +
2120 +There may be one more another approach which is
2121 +- bind-mount the branch-root onto the aufs-root internally
2122 +- grab the new vfsmount (ie. struct mount)
2123 +- lazy-umount the branch-root internally
2124 +- in open(2) the aufs-file, open the branch-file with the hidden
2125 +  vfsmount (instead of the original branch's vfsmount)
2126 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2127 +  but it may be possible from userspace by the mount helper.
2128 +
2129 +Adding the internal hidden vfsmount and using it in opening a file, the
2130 +file path under /proc will be printed correctly. This approach looks
2131 +smarter, but is not possible I am afraid.
2132 +- aufs-root may be bind-mount later. when it happens, another hidden
2133 +  vfsmount will be required.
2134 +- it is hard to get the chance to bind-mount and lazy-umount
2135 +  + in kernel-space, FS can have vfsmount in open(2) via
2136 +    file->f_path, and aufs can know its vfsmount. But several locks are
2137 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2138 +    here, then it may cause a deadlock.
2139 +  + in user-space, bind-mount doesn't invoke the mount helper.
2140 +- since /proc shows dev and ino, aufs has to give vma these info. it
2141 +  means a new member vm_prinode will be necessary. this is essentially
2142 +  equivalent to vm_prfile described above.
2143 +
2144 +I have to give up this "looks-smater" approach.
2145 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2146 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2147 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-11-05 23:02:18.959222617 +0100
2148 @@ -0,0 +1,94 @@
2149 +
2150 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2151 +#
2152 +# This program is free software; you can redistribute it and/or modify
2153 +# it under the terms of the GNU General Public License as published by
2154 +# the Free Software Foundation; either version 2 of the License, or
2155 +# (at your option) any later version.
2156 +#
2157 +# This program is distributed in the hope that it will be useful,
2158 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2159 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2160 +# GNU General Public License for more details.
2161 +#
2162 +# You should have received a copy of the GNU General Public License
2163 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2164 +
2165 +Listing XATTR/EA and getting the value
2166 +----------------------------------------------------------------------
2167 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2168 +shows the values from the topmost existing file. This behaviour is good
2169 +for the non-dir entries since the bahaviour exactly matches the shown
2170 +information. But for the directories, aufs considers all the same named
2171 +entries on the lower branches. Which means, if one of the lower entry
2172 +rejects readdir call, then aufs returns an error even if the topmost
2173 +entry allows it. This behaviour is necessary to respect the branch fs's
2174 +security, but can make users confused since the user-visible standard
2175 +attributes don't match the behaviour.
2176 +To address this issue, aufs has a mount option called dirperm1 which
2177 +checks the permission for the topmost entry only, and ignores the lower
2178 +entry's permission.
2179 +
2180 +A similar issue can happen around XATTR.
2181 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2182 +always set. Otherwise these very unpleasant situation would happen.
2183 +- listxattr(2) may return the duplicated entries.
2184 +- users may not be able to remove or reset the XATTR forever,
2185 +
2186 +
2187 +XATTR/EA support in the internal (copy,move)-(up,down)
2188 +----------------------------------------------------------------------
2189 +Generally the extended attributes of inode are categorized as these.
2190 +- "security" for LSM and capability.
2191 +- "system" for posix ACL, 'acl' mount option is required for the branch
2192 +  fs generally.
2193 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2194 +- "user" for userspace, 'user_xattr' mount option is required for the
2195 +  branch fs generally.
2196 +
2197 +Moreover there are some other categories. Aufs handles these rather
2198 +unpopular categories as the ordinary ones, ie. there is no special
2199 +condition nor exception.
2200 +
2201 +In copy-up, the support for XATTR on the dst branch may differ from the
2202 +src branch. In this case, the copy-up operation will get an error and
2203 +the original user operation which triggered the copy-up will fail. It
2204 +can happen that even all copy-up will fail.
2205 +When both of src and dst branches support XATTR and if an error occurs
2206 +during copying XATTR, then the copy-up should fail obviously. That is a
2207 +good reason and aufs should return an error to userspace. But when only
2208 +the src branch support that XATTR, aufs should not return an error.
2209 +For example, the src branch supports ACL but the dst branch doesn't
2210 +because the dst branch may natively un-support it or temporary
2211 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2212 +may NOT return an error even if the XATTR is not supported. It is
2213 +totally up to the branch fs.
2214 +
2215 +Anyway when the aufs internal copy-up gets an error from the dst branch
2216 +fs, then aufs tries removing the just copied entry and returns the error
2217 +to the userspace. The worst case of this situation will be all copy-up
2218 +will fail.
2219 +
2220 +For the copy-up operation, there two basic approaches.
2221 +- copy the specified XATTR only (by category above), and return the
2222 +  error unconditionally if it happens.
2223 +- copy all XATTR, and ignore the error on the specified category only.
2224 +
2225 +In order to support XATTR and to implement the correct behaviour, aufs
2226 +chooses the latter approach and introduces some new branch attributes,
2227 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2228 +They correspond to the XATTR namespaces (see above). Additionally, to be
2229 +convenient, "icex" is also provided which means all "icex*" attributes
2230 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2231 +
2232 +The meaning of these attributes is to ignore the error from setting
2233 +XATTR on that branch.
2234 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2235 +error from the dst branch according to the specified attributes.
2236 +
2237 +Some XATTR may have its default value. The default value may come from
2238 +the parent dir or the environment. If the default value is set at the
2239 +file creating-time, it will be overwritten by copy-up.
2240 +Some contradiction may happen I am afraid.
2241 +Do we need another attribute to stop copying XATTR? I am unsure. For
2242 +now, aufs implements the branch attributes to ignore the error.
2243 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2244 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2245 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-11-05 23:02:18.959222617 +0100
2246 @@ -0,0 +1,58 @@
2247 +
2248 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2249 +#
2250 +# This program is free software; you can redistribute it and/or modify
2251 +# it under the terms of the GNU General Public License as published by
2252 +# the Free Software Foundation; either version 2 of the License, or
2253 +# (at your option) any later version.
2254 +#
2255 +# This program is distributed in the hope that it will be useful,
2256 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2257 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2258 +# GNU General Public License for more details.
2259 +#
2260 +# You should have received a copy of the GNU General Public License
2261 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2262 +
2263 +Export Aufs via NFS
2264 +----------------------------------------------------------------------
2265 +Here is an approach.
2266 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2267 +  generation.
2268 +- iget_locked(): initialize aufs inode generation for a new inode, and
2269 +  store it in xigen file.
2270 +- destroy_inode(): increment aufs inode generation and store it in xigen
2271 +  file. it is necessary even if it is not unlinked, because any data of
2272 +  inode may be changed by UDBA.
2273 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2274 +  build file handle by
2275 +  + branch id (4 bytes)
2276 +  + superblock generation (4 bytes)
2277 +  + inode number (4 or 8 bytes)
2278 +  + parent dir inode number (4 or 8 bytes)
2279 +  + inode generation (4 bytes))
2280 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2281 +    bytes)
2282 +  + file handle for a branch (by exportfs_encode_fh())
2283 +- fh_to_dentry():
2284 +  + find the index of a branch from its id in handle, and check it is
2285 +    still exist in aufs.
2286 +  + 1st level: get the inode number from handle and search it in cache.
2287 +  + 2nd level: if not found in cache, get the parent inode number from
2288 +    the handle and search it in cache. and then open the found parent
2289 +    dir, find the matching inode number by vfs_readdir() and get its
2290 +    name, and call lookup_one_len() for the target dentry.
2291 +  + 3rd level: if the parent dir is not cached, call
2292 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2293 +    build a pathname of it, convert it a pathname in aufs, call
2294 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2295 +    the 2nd level.
2296 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2297 +    for every branch, but not itself. to get this, (currently) aufs
2298 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2299 +    idea, but I didn't get other approach.
2300 +  + test the generation of the gotten inode.
2301 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2302 +  convert it into ESTALE for NFSD.
2303 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2304 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2305 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2306 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2307 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-11-05 23:02:18.959222617 +0100
2308 @@ -0,0 +1,52 @@
2309 +
2310 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2311 +#
2312 +# This program is free software; you can redistribute it and/or modify
2313 +# it under the terms of the GNU General Public License as published by
2314 +# the Free Software Foundation; either version 2 of the License, or
2315 +# (at your option) any later version.
2316 +#
2317 +# This program is distributed in the hope that it will be useful,
2318 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2319 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2320 +# GNU General Public License for more details.
2321 +#
2322 +# You should have received a copy of the GNU General Public License
2323 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2324 +
2325 +Show Whiteout Mode (shwh)
2326 +----------------------------------------------------------------------
2327 +Generally aufs hides the name of whiteouts. But in some cases, to show
2328 +them is very useful for users. For instance, creating a new middle layer
2329 +(branch) by merging existing layers.
2330 +
2331 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2332 +When you have three branches,
2333 +- Bottom: 'system', squashfs (underlying base system), read-only
2334 +- Middle: 'mods', squashfs, read-only
2335 +- Top: 'overlay', ram (tmpfs), read-write
2336 +
2337 +The top layer is loaded at boot time and saved at shutdown, to preserve
2338 +the changes made to the system during the session.
2339 +When larger changes have been made, or smaller changes have accumulated,
2340 +the size of the saved top layer data grows. At this point, it would be
2341 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2342 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2343 +restoring save and load speed.
2344 +
2345 +This merging is simplified by the use of another aufs mount, of just the
2346 +two overlay branches using the 'shwh' option.
2347 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2348 +       aufs /livesys/merge_union
2349 +
2350 +A merged view of these two branches is then available at
2351 +/livesys/merge_union, and the new feature is that the whiteouts are
2352 +visible!
2353 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2354 +writing to all branches. Also the default mode for all branches is 'ro'.
2355 +It is now possible to save the combined contents of the two overlay
2356 +branches to a new squashfs, e.g.:
2357 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2358 +
2359 +This new squashfs archive can be stored on the boot device and the
2360 +initramfs will use it to replace the old one at the next boot.
2361 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2362 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2363 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-11-05 23:02:18.959222617 +0100
2364 @@ -0,0 +1,47 @@
2365 +
2366 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2367 +#
2368 +# This program is free software; you can redistribute it and/or modify
2369 +# it under the terms of the GNU General Public License as published by
2370 +# the Free Software Foundation; either version 2 of the License, or
2371 +# (at your option) any later version.
2372 +#
2373 +# This program is distributed in the hope that it will be useful,
2374 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2375 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2376 +# GNU General Public License for more details.
2377 +#
2378 +# You should have received a copy of the GNU General Public License
2379 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2380 +
2381 +Dynamically customizable FS operations
2382 +----------------------------------------------------------------------
2383 +Generally FS operations (struct inode_operations, struct
2384 +address_space_operations, struct file_operations, etc.) are defined as
2385 +"static const", but it never means that FS have only one set of
2386 +operation. Some FS have multiple sets of them. For instance, ext2 has
2387 +three sets, one for XIP, for NOBH, and for normal.
2388 +Since aufs overrides and redirects these operations, sometimes aufs has
2389 +to change its behaviour according to the branch FS type. More importantly
2390 +VFS acts differently if a function (member in the struct) is set or
2391 +not. It means aufs should have several sets of operations and select one
2392 +among them according to the branch FS definition.
2393 +
2394 +In order to solve this problem and not to affect the behaviour of VFS,
2395 +aufs defines these operations dynamically. For instance, aufs defines
2396 +dummy direct_IO function for struct address_space_operations, but it may
2397 +not be set to the address_space_operations actually. When the branch FS
2398 +doesn't have it, aufs doesn't set it to its address_space_operations
2399 +while the function definition itself is still alive. So the behaviour
2400 +itself will not change, and it will return an error when direct_IO is
2401 +not set.
2402 +
2403 +The lifetime of these dynamically generated operation object is
2404 +maintained by aufs branch object. When the branch is removed from aufs,
2405 +the reference counter of the object is decremented. When it reaches
2406 +zero, the dynamically generated operation object will be freed.
2407 +
2408 +This approach is designed to support AIO (io_submit), Direct I/O and
2409 +XIP (DAX) mainly.
2410 +Currently this approach is applied to address_space_operations for
2411 +regular files only.
2412 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2413 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2414 +++ linux/Documentation/filesystems/aufs/README 2023-09-02 12:00:06.376642958 +0200
2415 @@ -0,0 +1,409 @@
2416 +
2417 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2418 +http://aufs.sf.net
2419 +Junjiro R. Okajima
2420 +
2421 +
2422 +0. Introduction
2423 +----------------------------------------
2424 +In the early days, aufs was entirely re-designed and re-implemented
2425 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2426 +improvements and implementations, it became totally different from
2427 +Unionfs while keeping the basic features.
2428 +Later, Unionfs Version 2.x series began taking some of the same
2429 +approaches to aufs1's.
2430 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2431 +University and his team.
2432 +
2433 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2434 +aufs-linux.git or aufs-standalone.git.
2435 +If you want older kernel version support,
2436 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2437 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2438 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2439 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2440 +  or aufs1 from CVS on SourceForge.
2441 +
2442 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2443 +      were changed. Now they are aufs-linux.git and
2444 +      aufs-standalone.git and they contain aufs5 and later branches.
2445 +
2446 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2447 +      According to Christoph Hellwig, linux rejects all union-type
2448 +      filesystems but UnionMount.
2449 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2450 +
2451 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2452 +    UnionMount, and he pointed out an issue around a directory mutex
2453 +    lock and aufs addressed it. But it is still unsure whether aufs will
2454 +    be merged (or any other union solution).
2455 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2456 +
2457 +
2458 +1. Features
2459 +----------------------------------------
2460 +- unite several directories into a single virtual filesystem. The member
2461 +  directory is called as a branch.
2462 +- you can specify the permission flags to the branch, which are 'readonly',
2463 +  'readwrite' and 'whiteout-able.'
2464 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2465 +  readonly branch are modifiable logically.
2466 +- dynamic branch manipulation, add, del.
2467 +- etc...
2468 +
2469 +Also there are many enhancements in aufs, such as:
2470 +- test only the highest one for the directory permission (dirperm1)
2471 +- copyup on open (coo=)
2472 +- 'move' policy for copy-up between two writable branches, after
2473 +  checking free space.
2474 +- xattr, acl
2475 +- readdir(3) in userspace.
2476 +- keep inode number by external inode number table
2477 +- keep the timestamps of file/dir in internal copyup operation
2478 +- seekable directory, supporting NFS readdir.
2479 +- whiteout is hardlinked in order to reduce the consumption of inodes
2480 +  on branch
2481 +- do not copyup, nor create a whiteout when it is unnecessary
2482 +- revert a single systemcall when an error occurs in aufs
2483 +- remount interface instead of ioctl
2484 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2485 +- loopback mounted filesystem as a branch
2486 +- kernel thread for removing the dir who has a plenty of whiteouts
2487 +- support copyup sparse file (a file which has a 'hole' in it)
2488 +- default permission flags for branches
2489 +- selectable permission flags for ro branch, whether whiteout can
2490 +  exist or not
2491 +- export via NFS.
2492 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2493 +- support multiple writable branches, some policies to select one
2494 +  among multiple writable branches.
2495 +- a new semantics for link(2) and rename(2) to support multiple
2496 +  writable branches.
2497 +- no glibc changes are required.
2498 +- pseudo hardlink (hardlink over branches)
2499 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2500 +  including NFS or remote filesystem branch.
2501 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2502 +- and more...
2503 +
2504 +Currently these features are dropped temporary from aufs6.
2505 +See design/08plan.txt in detail.
2506 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2507 +  (robr)
2508 +- statistics of aufs thread (/sys/fs/aufs/stat)
2509 +
2510 +Features or just an idea in the future (see also design/*.txt),
2511 +- reorder the branch index without del/re-add.
2512 +- permanent xino files for NFSD
2513 +- an option for refreshing the opened files after add/del branches
2514 +- light version, without branch manipulation. (unnecessary?)
2515 +- copyup in userspace
2516 +- inotify in userspace
2517 +- readv/writev
2518 +
2519 +
2520 +2. Download
2521 +----------------------------------------
2522 +There are three GIT trees for aufs6, aufs-linux.git,
2523 +aufs-standalone.git, and aufs-util.git.
2524 +While the aufs-util is always necessary, you need either of aufs-linux
2525 +or aufs-standalone.
2526 +
2527 +The aufs-linux tree includes the whole linux mainline GIT tree,
2528 +git://git.kernel.org/.../torvalds/linux.git.
2529 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2530 +build aufs6 as an external kernel module.
2531 +Several extra patches are not included in this tree. Only
2532 +aufs-standalone tree contains them. They are described in the later
2533 +section "Configuration and Compilation."
2534 +
2535 +On the other hand, the aufs-standalone tree has only aufs source files
2536 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2537 +But you need to apply all aufs patches manually.
2538 +
2539 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2540 +represents the linux kernel version, "linux-6.x". For instance,
2541 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2542 +"aufs6.x-rcN" branch.
2543 +
2544 +o aufs-linux tree
2545 +$ git clone --reference /your/linux/git/tree \
2546 +       git://github.com/sfjro/aufs-linux.git aufs-linux.git
2547 +- if you don't have linux GIT tree, then remove "--reference ..."
2548 +$ cd aufs-linux.git
2549 +$ git checkout origin/aufs6.0
2550 +
2551 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2552 +leave the patch-work to GIT.
2553 +$ cd /your/linux/git/tree
2554 +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
2555 +$ git fetch aufs
2556 +$ git checkout -b my6.0 v6.0
2557 +$ (add your local change...)
2558 +$ git pull aufs aufs6.0
2559 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2560 +- you may need to solve some conflicts between your_changes and
2561 +  aufs6.0. in this case, git-rerere is recommended so that you can
2562 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2563 +  later in the future.
2564 +
2565 +o aufs-standalone tree
2566 +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
2567 +$ cd aufs-standalone.git
2568 +$ git checkout origin/aufs6.0
2569 +
2570 +o aufs-util tree
2571 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2572 +- note that the public aufs-util.git is on SourceForge instead of
2573 +  GitHUB.
2574 +$ cd aufs-util.git
2575 +$ git checkout origin/aufs6.0
2576 +
2577 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2578 +The minor version number, 'x' in '6.x', of aufs may not always
2579 +follow the minor version number of the kernel.
2580 +Because changes in the kernel that cause the use of a new
2581 +minor version number do not always require changes to aufs-util.
2582 +
2583 +Since aufs-util has its own minor version number, you may not be
2584 +able to find a GIT branch in aufs-util for your kernel's
2585 +exact minor version number.
2586 +In this case, you should git-checkout the branch for the
2587 +nearest lower number.
2588 +
2589 +For (an unreleased) example:
2590 +If you are using "linux-6.10" and the "aufs6.10" branch
2591 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2592 +or something numerically smaller is the branch for your kernel.
2593 +
2594 +Also you can view all branches by
2595 +       $ git branch -a
2596 +
2597 +
2598 +3. Configuration and Compilation
2599 +----------------------------------------
2600 +Make sure you have git-checkout'ed the correct branch.
2601 +
2602 +For aufs-linux tree,
2603 +- enable CONFIG_AUFS_FS.
2604 +- set other aufs configurations if necessary.
2605 +- for aufs5.13 and later
2606 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2607 +  also a caller of VFS functions for branch filesystems, subclassing of
2608 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2609 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2610 +  need to customize some LOCKDEP numbers. Here are what I use on my
2611 +  test environment.
2612 +       CONFIG_LOCKDEP_BITS=21
2613 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2614 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2615 +  Also you will need to expand some constant values in LOCKDEP. Refer
2616 +  to lockdep-debug.patch in aufs-standalone.git.
2617 +
2618 +For aufs-standalone tree,
2619 +There are several ways to build.
2620 +
2621 +1.
2622 +- apply ./aufs6-kbuild.patch to your kernel source files.
2623 +- apply ./aufs6-base.patch too.
2624 +- apply ./aufs6-mmap.patch too.
2625 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2626 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs-standalone.patch.
2627 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2628 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2629 +- enable CONFIG_AUFS_FS, you can select either
2630 +  =m or =y.
2631 +- and build your kernel as usual.
2632 +- install the built kernel.
2633 +- install the header files too by "make headers_install" to the
2634 +  directory where you specify. By default, it is $PWD/usr.
2635 +  "make help" shows a brief note for headers_install.
2636 +- and reboot your system.
2637 +
2638 +2.
2639 +- module only (CONFIG_AUFS_FS=m).
2640 +- apply ./aufs6-base.patch to your kernel source files.
2641 +- apply ./aufs6-mmap.patch too.
2642 +- apply ./aufs6-standalone.patch too.
2643 +- build your kernel, don't forget "make headers_install", and reboot.
2644 +- edit ./config.mk and set other aufs configurations if necessary.
2645 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2646 +  every aufs configurations.
2647 +- build the module by simple "make".
2648 +- you can specify ${KDIR} make variable which points to your kernel
2649 +  source tree.
2650 +- install the files
2651 +  + run "make install" to install the aufs module, or copy the built
2652 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2653 +  + run "make install_headers" (instead of headers_install) to install
2654 +    the modified aufs header file (you can specify DESTDIR which is
2655 +    available in aufs standalone version's Makefile only), or copy
2656 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2657 +    you like manually. By default, the target directory is $PWD/usr.
2658 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2659 +  kernel source tree.
2660 +
2661 +Note: The header file aufs_type.h is necessary to build aufs-util
2662 +      as well as "make headers_install" in the kernel source tree.
2663 +      headers_install is subject to be forgotten, but it is essentially
2664 +      necessary, not only for building aufs-util.
2665 +      You may not meet problems without headers_install in some older
2666 +      version though.
2667 +
2668 +And then,
2669 +- read README in aufs-util, build and install it
2670 +- note that your distribution may contain an obsoleted version of
2671 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2672 +  utilities, make sure that your compiler refers the correct aufs header
2673 +  file which is built by "make headers_install."
2674 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2675 +  then run "make install_ulib" too. And refer to the aufs manual in
2676 +  detail.
2677 +
2678 +There several other patches in aufs-standalone.git. They are all
2679 +optional. When you meet some problems, they will help you.
2680 +- aufs6-loopback.patch
2681 +  Supports a nested loopback mount in a branch-fs. This patch is
2682 +  unnecessary until aufs produces a message like "you may want to try
2683 +  another patch for loopback file".
2684 +- vfs-ino.patch
2685 +  Modifies a system global kernel internal function get_next_ino() in
2686 +  order to stop assigning 0 for an inode-number. Not directly related to
2687 +  aufs, but recommended generally.
2688 +- tmpfs-idr.patch
2689 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2690 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2691 +  duplication of inode number, which is important for backup tools and
2692 +  other utilities. When you find aufs XINO files for tmpfs branch
2693 +  growing too much, try this patch.
2694 +- lockdep-debug.patch
2695 +  Similar to some kernel configurations for LOCKDEP (see the top of
2696 +  this section), you will need expand some constants in LOCKDEP for
2697 +  aufs if you enable CONFIG_LOCKDEP.
2698 +
2699 +
2700 +4. Usage
2701 +----------------------------------------
2702 +At first, make sure aufs-util are installed, and please read the aufs
2703 +manual, aufs.5 in aufs-util.git tree.
2704 +$ man -l aufs.5
2705 +
2706 +And then,
2707 +$ mkdir /tmp/rw /tmp/aufs
2708 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2709 +
2710 +Here is another example. The result is equivalent.
2711 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2712 +  Or
2713 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2714 +# mount -o remount,append:${HOME} /tmp/aufs
2715 +
2716 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2717 +you modify a file under /tmp/aufs, the one on your home directory is
2718 +not affected, instead the same named file will be newly created under
2719 +/tmp/rw. And all of your modification to a file will be applied to
2720 +the one under /tmp/rw. This is called the file based Copy on Write
2721 +(COW) method.
2722 +Aufs mount options are described in aufs.5.
2723 +If you run chroot or something and make your aufs as a root directory,
2724 +then you need to customize the shutdown script. See the aufs manual in
2725 +detail.
2726 +
2727 +Additionally, there are some sample usages of aufs which are a
2728 +diskless system with network booting, and LiveCD over NFS.
2729 +See sample dir in CVS tree on SourceForge.
2730 +
2731 +
2732 +5. Contact
2733 +----------------------------------------
2734 +When you have any problems or strange behaviour in aufs, please let me
2735 +know with:
2736 +- /proc/mounts (instead of the output of mount(8))
2737 +- /sys/module/aufs/*
2738 +- /sys/fs/aufs/* (if you have them)
2739 +- /debug/aufs/* (if you have them)
2740 +- linux kernel version
2741 +  if your kernel is not plain, for example modified by distributor,
2742 +  the url where i can download its source is necessary too.
2743 +- aufs version which was printed at loading the module or booting the
2744 +  system, instead of the date you downloaded.
2745 +- configuration (define/undefine CONFIG_AUFS_xxx)
2746 +- kernel configuration or /proc/config.gz (if you have it)
2747 +- LSM (linux security module, if you are using)
2748 +- behaviour which you think to be incorrect
2749 +- actual operation, reproducible one is better
2750 +- mailto: aufs-users at lists.sourceforge.net
2751 +
2752 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2753 +and Feature Requests) on SourceForge. Please join and write to
2754 +aufs-users ML.
2755 +
2756 +
2757 +6. Acknowledgements
2758 +----------------------------------------
2759 +Thanks to everyone who have tried and are using aufs, whoever
2760 +have reported a bug or any feedback.
2761 +
2762 +Especially donators:
2763 +Tomas Matejicek(slax.org) made a donation (much more than once).
2764 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2765 +       scripts) is making "doubling" donations.
2766 +       Unfortunately I cannot list all of the donators, but I really
2767 +       appreciate.
2768 +       It ends Aug 2010, but the ordinary donation URL is still available.
2769 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2770 +Dai Itasaka made a donation (2007/8).
2771 +Chuck Smith made a donation (2008/4, 10 and 12).
2772 +Henk Schoneveld made a donation (2008/9).
2773 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2774 +Francois Dupoux made a donation (2008/11).
2775 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2776 +       aufs2 GIT tree (2009/2).
2777 +William Grant made a donation (2009/3).
2778 +Patrick Lane made a donation (2009/4).
2779 +The Mail Archive (mail-archive.com) made donations (2009/5).
2780 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2781 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2782 +Pavel Pronskiy made a donation (2011/2).
2783 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2784 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2785 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2786 +11).
2787 +Sam Liddicott made a donation (2011/9).
2788 +Era Scarecrow made a donation (2013/4).
2789 +Bor Ratajc made a donation (2013/4).
2790 +Alessandro Gorreta made a donation (2013/4).
2791 +POIRETTE Marc made a donation (2013/4).
2792 +Alessandro Gorreta made a donation (2013/4).
2793 +lauri kasvandik made a donation (2013/5).
2794 +"pemasu from Finland" made a donation (2013/7).
2795 +The Parted Magic Project made a donation (2013/9 and 11).
2796 +Pavel Barta made a donation (2013/10).
2797 +Nikolay Pertsev made a donation (2014/5).
2798 +James B made a donation (2014/7, 2015/7, and 2021/12).
2799 +Stefano Di Biase made a donation (2014/8).
2800 +Daniel Epellei made a donation (2015/1).
2801 +OmegaPhil made a donation (2016/1, 2018/4).
2802 +Tomasz Szewczyk made a donation (2016/4).
2803 +James Burry made a donation (2016/12).
2804 +Carsten Rose made a donation (2018/9).
2805 +Porteus Kiosk made a donation (2018/10).
2806 +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
2807 +Vasily Mikhaylichenko made a donation (2023/5).
2808 +
2809 +Thank you very much.
2810 +Donations are always, including future donations, very important and
2811 +helpful for me to keep on developing aufs.
2812 +
2813 +
2814 +7.
2815 +----------------------------------------
2816 +If you are an experienced user, no explanation is needed. Aufs is
2817 +just a linux filesystem.
2818 +
2819 +
2820 +Enjoy!
2821 +
2822 +# Local variables: ;
2823 +# mode: text;
2824 +# End: ;
2825 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2826 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2827 +++ linux/fs/aufs/aufs.h        2022-11-05 23:02:18.959222617 +0100
2828 @@ -0,0 +1,62 @@
2829 +/* SPDX-License-Identifier: GPL-2.0 */
2830 +/*
2831 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2832 + *
2833 + * This program is free software; you can redistribute it and/or modify
2834 + * it under the terms of the GNU General Public License as published by
2835 + * the Free Software Foundation; either version 2 of the License, or
2836 + * (at your option) any later version.
2837 + *
2838 + * This program is distributed in the hope that it will be useful,
2839 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2840 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2841 + * GNU General Public License for more details.
2842 + *
2843 + * You should have received a copy of the GNU General Public License
2844 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2845 + */
2846 +
2847 +/*
2848 + * all header files
2849 + */
2850 +
2851 +#ifndef __AUFS_H__
2852 +#define __AUFS_H__
2853 +
2854 +#ifdef __KERNEL__
2855 +
2856 +#define AuStub(type, name, body, ...) \
2857 +       static inline type name(__VA_ARGS__) { body; }
2858 +
2859 +#define AuStubVoid(name, ...) \
2860 +       AuStub(void, name, , __VA_ARGS__)
2861 +#define AuStubInt0(name, ...) \
2862 +       AuStub(int, name, return 0, __VA_ARGS__)
2863 +
2864 +#include "debug.h"
2865 +
2866 +#include "branch.h"
2867 +#include "cpup.h"
2868 +#include "dcsub.h"
2869 +#include "dbgaufs.h"
2870 +#include "dentry.h"
2871 +#include "dir.h"
2872 +#include "dirren.h"
2873 +#include "dynop.h"
2874 +#include "file.h"
2875 +#include "fstype.h"
2876 +#include "hbl.h"
2877 +#include "inode.h"
2878 +#include "lcnt.h"
2879 +#include "loop.h"
2880 +#include "module.h"
2881 +#include "opts.h"
2882 +#include "rwsem.h"
2883 +#include "super.h"
2884 +#include "sysaufs.h"
2885 +#include "vfsub.h"
2886 +#include "whout.h"
2887 +#include "wkq.h"
2888 +
2889 +#endif /* __KERNEL__ */
2890 +#endif /* __AUFS_H__ */
2891 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2892 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2893 +++ linux/fs/aufs/branch.c      2024-03-10 23:40:47.083143450 +0100
2894 @@ -0,0 +1,1427 @@
2895 +// SPDX-License-Identifier: GPL-2.0
2896 +/*
2897 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2898 + *
2899 + * This program is free software; you can redistribute it and/or modify
2900 + * it under the terms of the GNU General Public License as published by
2901 + * the Free Software Foundation; either version 2 of the License, or
2902 + * (at your option) any later version.
2903 + *
2904 + * This program is distributed in the hope that it will be useful,
2905 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2906 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2907 + * GNU General Public License for more details.
2908 + *
2909 + * You should have received a copy of the GNU General Public License
2910 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2911 + */
2912 +
2913 +/*
2914 + * branch management
2915 + */
2916 +
2917 +#include <linux/compat.h>
2918 +#include <linux/statfs.h>
2919 +#include "aufs.h"
2920 +
2921 +/*
2922 + * free a single branch
2923 + */
2924 +static void au_br_do_free(struct au_branch *br)
2925 +{
2926 +       int i;
2927 +       struct au_wbr *wbr;
2928 +       struct au_dykey **key;
2929 +
2930 +       au_hnotify_fin_br(br);
2931 +       /* always, regardless the mount option */
2932 +       au_dr_hino_free(&br->br_dirren);
2933 +       au_xino_put(br);
2934 +
2935 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2936 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2937 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2938 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2939 +
2940 +       wbr = br->br_wbr;
2941 +       if (wbr) {
2942 +               for (i = 0; i < AuBrWh_Last; i++)
2943 +                       dput(wbr->wbr_wh[i]);
2944 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2945 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2946 +       }
2947 +
2948 +       if (br->br_fhsm) {
2949 +               au_br_fhsm_fin(br->br_fhsm);
2950 +               au_kfree_try_rcu(br->br_fhsm);
2951 +       }
2952 +
2953 +       key = br->br_dykey;
2954 +       for (i = 0; i < AuBrDynOp; i++, key++)
2955 +               if (*key)
2956 +                       au_dy_put(*key);
2957 +               else
2958 +                       break;
2959 +
2960 +       /* recursive lock, s_umount of branch's */
2961 +       /* synchronize_rcu(); */ /* why? */
2962 +       lockdep_off();
2963 +       path_put(&br->br_path);
2964 +       lockdep_on();
2965 +       au_kfree_rcu(wbr);
2966 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2967 +       au_lcnt_wait_for_fin(&br->br_count);
2968 +       /* I don't know why, but percpu_refcount requires this */
2969 +       /* synchronize_rcu(); */
2970 +       au_kfree_rcu(br);
2971 +}
2972 +
2973 +/*
2974 + * frees all branches
2975 + */
2976 +void au_br_free(struct au_sbinfo *sbinfo)
2977 +{
2978 +       aufs_bindex_t bmax;
2979 +       struct au_branch **br;
2980 +
2981 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2982 +
2983 +       bmax = sbinfo->si_bbot + 1;
2984 +       br = sbinfo->si_branch;
2985 +       while (bmax--)
2986 +               au_br_do_free(*br++);
2987 +}
2988 +
2989 +/*
2990 + * find the index of a branch which is specified by @br_id.
2991 + */
2992 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2993 +{
2994 +       aufs_bindex_t bindex, bbot;
2995 +
2996 +       bbot = au_sbbot(sb);
2997 +       for (bindex = 0; bindex <= bbot; bindex++)
2998 +               if (au_sbr_id(sb, bindex) == br_id)
2999 +                       return bindex;
3000 +       return -1;
3001 +}
3002 +
3003 +/* ---------------------------------------------------------------------- */
3004 +
3005 +/*
3006 + * add a branch
3007 + */
3008 +
3009 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3010 +                       struct dentry *h_root)
3011 +{
3012 +       if (unlikely(h_adding == h_root
3013 +                    || au_test_loopback_overlap(sb, h_adding)))
3014 +               return 1;
3015 +       if (h_adding->d_sb != h_root->d_sb)
3016 +               return 0;
3017 +       return au_test_subdir(h_adding, h_root)
3018 +               || au_test_subdir(h_root, h_adding);
3019 +}
3020 +
3021 +/*
3022 + * returns a newly allocated branch. @new_nbranch is a number of branches
3023 + * after adding a branch.
3024 + */
3025 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3026 +                                    int perm)
3027 +{
3028 +       struct au_branch *add_branch;
3029 +       struct dentry *root;
3030 +       struct inode *inode;
3031 +       int err;
3032 +
3033 +       err = -ENOMEM;
3034 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3035 +       if (unlikely(!add_branch))
3036 +               goto out;
3037 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3038 +       if (unlikely(!add_branch->br_xino))
3039 +               goto out_br;
3040 +       err = au_hnotify_init_br(add_branch, perm);
3041 +       if (unlikely(err))
3042 +               goto out_xino;
3043 +
3044 +       if (au_br_writable(perm)) {
3045 +               /* may be freed separately at changing the branch permission */
3046 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3047 +                                            GFP_NOFS);
3048 +               if (unlikely(!add_branch->br_wbr))
3049 +                       goto out_hnotify;
3050 +       }
3051 +
3052 +       if (au_br_fhsm(perm)) {
3053 +               err = au_fhsm_br_alloc(add_branch);
3054 +               if (unlikely(err))
3055 +                       goto out_wbr;
3056 +       }
3057 +
3058 +       root = sb->s_root;
3059 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3060 +       if (!err)
3061 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3062 +       if (!err) {
3063 +               inode = d_inode(root);
3064 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3065 +                                       /*may_shrink*/0);
3066 +       }
3067 +       if (!err)
3068 +               return add_branch; /* success */
3069 +
3070 +out_wbr:
3071 +       au_kfree_rcu(add_branch->br_wbr);
3072 +out_hnotify:
3073 +       au_hnotify_fin_br(add_branch);
3074 +out_xino:
3075 +       au_xino_put(add_branch);
3076 +out_br:
3077 +       au_kfree_rcu(add_branch);
3078 +out:
3079 +       return ERR_PTR(err);
3080 +}
3081 +
3082 +/*
3083 + * test if the branch permission is legal or not.
3084 + */
3085 +static int test_br(struct inode *inode, int brperm, char *path)
3086 +{
3087 +       int err;
3088 +
3089 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3090 +       if (!err)
3091 +               goto out;
3092 +
3093 +       err = -EINVAL;
3094 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3095 +
3096 +out:
3097 +       return err;
3098 +}
3099 +
3100 +/*
3101 + * returns:
3102 + * 0: success, the caller will add it
3103 + * plus: success, it is already unified, the caller should ignore it
3104 + * minus: error
3105 + */
3106 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3107 +{
3108 +       int err;
3109 +       aufs_bindex_t bbot, bindex;
3110 +       struct dentry *root, *h_dentry;
3111 +       struct inode *inode, *h_inode;
3112 +
3113 +       root = sb->s_root;
3114 +       bbot = au_sbbot(sb);
3115 +       if (unlikely(bbot >= 0
3116 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3117 +               err = 1;
3118 +               if (!remount) {
3119 +                       err = -EINVAL;
3120 +                       pr_err("%s duplicated\n", add->pathname);
3121 +               }
3122 +               goto out;
3123 +       }
3124 +
3125 +       err = -ENOSPC; /* -E2BIG; */
3126 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3127 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3128 +               pr_err("number of branches exceeded %s\n", add->pathname);
3129 +               goto out;
3130 +       }
3131 +
3132 +       err = -EDOM;
3133 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3134 +               pr_err("bad index %d\n", add->bindex);
3135 +               goto out;
3136 +       }
3137 +
3138 +       inode = d_inode(add->path.dentry);
3139 +       err = -ENOENT;
3140 +       if (unlikely(!inode->i_nlink)) {
3141 +               pr_err("no existence %s\n", add->pathname);
3142 +               goto out;
3143 +       }
3144 +
3145 +       err = -EINVAL;
3146 +       if (unlikely(inode->i_sb == sb)) {
3147 +               pr_err("%s must be outside\n", add->pathname);
3148 +               goto out;
3149 +       }
3150 +
3151 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3152 +               pr_err("unsupported filesystem, %s (%s)\n",
3153 +                      add->pathname, au_sbtype(inode->i_sb));
3154 +               goto out;
3155 +       }
3156 +
3157 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3158 +               pr_err("already stacked, %s (%s)\n",
3159 +                      add->pathname, au_sbtype(inode->i_sb));
3160 +               goto out;
3161 +       }
3162 +
3163 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3164 +       if (unlikely(err))
3165 +               goto out;
3166 +
3167 +       if (bbot < 0)
3168 +               return 0; /* success */
3169 +
3170 +       err = -EINVAL;
3171 +       for (bindex = 0; bindex <= bbot; bindex++)
3172 +               if (unlikely(test_overlap(sb, add->path.dentry,
3173 +                                         au_h_dptr(root, bindex)))) {
3174 +                       pr_err("%s is overlapped\n", add->pathname);
3175 +                       goto out;
3176 +               }
3177 +
3178 +       err = 0;
3179 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3180 +               h_dentry = au_h_dptr(root, 0);
3181 +               h_inode = d_inode(h_dentry);
3182 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3183 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3184 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3185 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3186 +                               add->pathname,
3187 +                               i_uid_read(inode), i_gid_read(inode),
3188 +                               (inode->i_mode & S_IALLUGO),
3189 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3190 +                               (h_inode->i_mode & S_IALLUGO));
3191 +       }
3192 +
3193 +out:
3194 +       return err;
3195 +}
3196 +
3197 +/*
3198 + * initialize or clean the whiteouts for an adding branch
3199 + */
3200 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3201 +                        int new_perm)
3202 +{
3203 +       int err, old_perm;
3204 +       aufs_bindex_t bindex;
3205 +       struct inode *h_inode;
3206 +       struct au_wbr *wbr;
3207 +       struct au_hinode *hdir;
3208 +       struct dentry *h_dentry;
3209 +
3210 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3211 +       if (unlikely(err))
3212 +               goto out;
3213 +
3214 +       wbr = br->br_wbr;
3215 +       old_perm = br->br_perm;
3216 +       br->br_perm = new_perm;
3217 +       hdir = NULL;
3218 +       h_inode = NULL;
3219 +       bindex = au_br_index(sb, br->br_id);
3220 +       if (0 <= bindex) {
3221 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3222 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3223 +       } else {
3224 +               h_dentry = au_br_dentry(br);
3225 +               h_inode = d_inode(h_dentry);
3226 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3227 +       }
3228 +       if (!wbr)
3229 +               err = au_wh_init(br, sb);
3230 +       else {
3231 +               wbr_wh_write_lock(wbr);
3232 +               err = au_wh_init(br, sb);
3233 +               wbr_wh_write_unlock(wbr);
3234 +       }
3235 +       if (hdir)
3236 +               au_hn_inode_unlock(hdir);
3237 +       else
3238 +               inode_unlock(h_inode);
3239 +       vfsub_mnt_drop_write(au_br_mnt(br));
3240 +       br->br_perm = old_perm;
3241 +
3242 +       if (!err && wbr && !au_br_writable(new_perm)) {
3243 +               au_kfree_rcu(wbr);
3244 +               br->br_wbr = NULL;
3245 +       }
3246 +
3247 +out:
3248 +       return err;
3249 +}
3250 +
3251 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3252 +                      int perm)
3253 +{
3254 +       int err;
3255 +       struct kstatfs kst;
3256 +       struct au_wbr *wbr;
3257 +
3258 +       wbr = br->br_wbr;
3259 +       au_rw_init(&wbr->wbr_wh_rwsem);
3260 +       atomic_set(&wbr->wbr_wh_running, 0);
3261 +
3262 +       /*
3263 +        * a limit for rmdir/rename a dir
3264 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3265 +        */
3266 +       err = vfs_statfs(&br->br_path, &kst);
3267 +       if (unlikely(err))
3268 +               goto out;
3269 +       err = -EINVAL;
3270 +       if (kst.f_namelen >= NAME_MAX)
3271 +               err = au_br_init_wh(sb, br, perm);
3272 +       else
3273 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3274 +                      au_br_dentry(br),
3275 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3276 +
3277 +out:
3278 +       return err;
3279 +}
3280 +
3281 +/* initialize a new branch */
3282 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3283 +                     struct au_opt_add *add)
3284 +{
3285 +       int err;
3286 +       struct au_branch *brbase;
3287 +       struct file *xf;
3288 +       struct inode *h_inode;
3289 +
3290 +       err = 0;
3291 +       br->br_perm = add->perm;
3292 +       br->br_path = add->path; /* set first, path_get() later */
3293 +       spin_lock_init(&br->br_dykey_lock);
3294 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3295 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3296 +       br->br_id = au_new_br_id(sb);
3297 +       AuDebugOn(br->br_id < 0);
3298 +
3299 +       /* always, regardless the given option */
3300 +       err = au_dr_br_init(sb, br, &add->path);
3301 +       if (unlikely(err))
3302 +               goto out_err;
3303 +
3304 +       if (au_br_writable(add->perm)) {
3305 +               err = au_wbr_init(br, sb, add->perm);
3306 +               if (unlikely(err))
3307 +                       goto out_err;
3308 +       }
3309 +
3310 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3311 +               brbase = au_sbr(sb, 0);
3312 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3313 +               AuDebugOn(!xf);
3314 +               h_inode = d_inode(add->path.dentry);
3315 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3316 +               if (unlikely(err)) {
3317 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3318 +                       goto out_err;
3319 +               }
3320 +       }
3321 +
3322 +       sysaufs_br_init(br);
3323 +       path_get(&br->br_path);
3324 +       goto out; /* success */
3325 +
3326 +out_err:
3327 +       memset(&br->br_path, 0, sizeof(br->br_path));
3328 +out:
3329 +       return err;
3330 +}
3331 +
3332 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3333 +                            struct au_branch *br, aufs_bindex_t bbot,
3334 +                            aufs_bindex_t amount)
3335 +{
3336 +       struct au_branch **brp;
3337 +
3338 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3339 +
3340 +       brp = sbinfo->si_branch + bindex;
3341 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3342 +       *brp = br;
3343 +       sbinfo->si_bbot++;
3344 +       if (unlikely(bbot < 0))
3345 +               sbinfo->si_bbot = 0;
3346 +}
3347 +
3348 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3349 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3350 +{
3351 +       struct au_hdentry *hdp;
3352 +
3353 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3354 +
3355 +       hdp = au_hdentry(dinfo, bindex);
3356 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3357 +       au_h_dentry_init(hdp);
3358 +       dinfo->di_bbot++;
3359 +       if (unlikely(bbot < 0))
3360 +               dinfo->di_btop = 0;
3361 +}
3362 +
3363 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3364 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3365 +{
3366 +       struct au_hinode *hip;
3367 +
3368 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3369 +
3370 +       hip = au_hinode(iinfo, bindex);
3371 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3372 +       au_hinode_init(hip);
3373 +       iinfo->ii_bbot++;
3374 +       if (unlikely(bbot < 0))
3375 +               iinfo->ii_btop = 0;
3376 +}
3377 +
3378 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3379 +                        aufs_bindex_t bindex)
3380 +{
3381 +       struct dentry *root, *h_dentry;
3382 +       struct inode *root_inode, *h_inode;
3383 +       aufs_bindex_t bbot, amount;
3384 +
3385 +       root = sb->s_root;
3386 +       root_inode = d_inode(root);
3387 +       bbot = au_sbbot(sb);
3388 +       amount = bbot + 1 - bindex;
3389 +       h_dentry = au_br_dentry(br);
3390 +       au_sbilist_lock();
3391 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3392 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3393 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3394 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3395 +       h_inode = d_inode(h_dentry);
3396 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3397 +       au_sbilist_unlock();
3398 +}
3399 +
3400 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3401 +{
3402 +       int err;
3403 +       aufs_bindex_t bbot, add_bindex;
3404 +       struct dentry *root, *h_dentry;
3405 +       struct inode *root_inode;
3406 +       struct au_branch *add_branch;
3407 +
3408 +       root = sb->s_root;
3409 +       root_inode = d_inode(root);
3410 +       IMustLock(root_inode);
3411 +       IiMustWriteLock(root_inode);
3412 +       err = test_add(sb, add, remount);
3413 +       if (unlikely(err < 0))
3414 +               goto out;
3415 +       if (err) {
3416 +               err = 0;
3417 +               goto out; /* success */
3418 +       }
3419 +
3420 +       bbot = au_sbbot(sb);
3421 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3422 +       err = PTR_ERR(add_branch);
3423 +       if (IS_ERR(add_branch))
3424 +               goto out;
3425 +
3426 +       err = au_br_init(add_branch, sb, add);
3427 +       if (unlikely(err)) {
3428 +               au_br_do_free(add_branch);
3429 +               goto out;
3430 +       }
3431 +
3432 +       add_bindex = add->bindex;
3433 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3434 +       au_br_do_add(sb, add_branch, add_bindex);
3435 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3436 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3437 +
3438 +       h_dentry = add->path.dentry;
3439 +       if (!add_bindex) {
3440 +               au_cpup_attr_all(root_inode, /*force*/1);
3441 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3442 +       } else
3443 +               au_add_nlink(root_inode, d_inode(h_dentry));
3444 +
3445 +out:
3446 +       return err;
3447 +}
3448 +
3449 +/* ---------------------------------------------------------------------- */
3450 +
3451 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3452 +                                      unsigned long long max __maybe_unused,
3453 +                                      void *arg)
3454 +{
3455 +       unsigned long long n;
3456 +       struct file **p, *f;
3457 +       struct hlist_bl_head *files;
3458 +       struct hlist_bl_node *pos;
3459 +       struct au_finfo *finfo;
3460 +
3461 +       n = 0;
3462 +       p = a;
3463 +       files = &au_sbi(sb)->si_files;
3464 +       hlist_bl_lock(files);
3465 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3466 +               f = finfo->fi_file;
3467 +               if (file_count(f)
3468 +                   && !special_file(file_inode(f)->i_mode)) {
3469 +                       get_file(f);
3470 +                       *p++ = f;
3471 +                       n++;
3472 +                       AuDebugOn(n > max);
3473 +               }
3474 +       }
3475 +       hlist_bl_unlock(files);
3476 +
3477 +       return n;
3478 +}
3479 +
3480 +static struct file **au_farray_alloc(struct super_block *sb,
3481 +                                    unsigned long long *max)
3482 +{
3483 +       struct au_sbinfo *sbi;
3484 +
3485 +       sbi = au_sbi(sb);
3486 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3487 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3488 +}
3489 +
3490 +static void au_farray_free(struct file **a, unsigned long long max)
3491 +{
3492 +       unsigned long long ull;
3493 +
3494 +       for (ull = 0; ull < max; ull++)
3495 +               if (a[ull])
3496 +                       fput(a[ull]);
3497 +       kvfree(a);
3498 +}
3499 +
3500 +/* ---------------------------------------------------------------------- */
3501 +
3502 +/*
3503 + * delete a branch
3504 + */
3505 +
3506 +/* to show the line number, do not make it inlined function */
3507 +#define AuVerbose(do_info, fmt, ...) do { \
3508 +       if (do_info) \
3509 +               pr_info(fmt, ##__VA_ARGS__); \
3510 +} while (0)
3511 +
3512 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3513 +                        aufs_bindex_t bbot)
3514 +{
3515 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3516 +}
3517 +
3518 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3519 +                        aufs_bindex_t bbot)
3520 +{
3521 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3522 +}
3523 +
3524 +/*
3525 + * test if the branch is deletable or not.
3526 + */
3527 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3528 +                           unsigned int sigen, const unsigned int verbose)
3529 +{
3530 +       int err, i, j, ndentry;
3531 +       aufs_bindex_t btop, bbot;
3532 +       struct au_dcsub_pages dpages;
3533 +       struct au_dpage *dpage;
3534 +       struct dentry *d;
3535 +
3536 +       err = au_dpages_init(&dpages, GFP_NOFS);
3537 +       if (unlikely(err))
3538 +               goto out;
3539 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3540 +       if (unlikely(err))
3541 +               goto out_dpages;
3542 +
3543 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3544 +               dpage = dpages.dpages + i;
3545 +               ndentry = dpage->ndentry;
3546 +               for (j = 0; !err && j < ndentry; j++) {
3547 +                       d = dpage->dentries[j];
3548 +                       AuDebugOn(au_dcount(d) <= 0);
3549 +                       if (!au_digen_test(d, sigen)) {
3550 +                               di_read_lock_child(d, AuLock_IR);
3551 +                               if (unlikely(au_dbrange_test(d))) {
3552 +                                       di_read_unlock(d, AuLock_IR);
3553 +                                       continue;
3554 +                               }
3555 +                       } else {
3556 +                               di_write_lock_child(d);
3557 +                               if (unlikely(au_dbrange_test(d))) {
3558 +                                       di_write_unlock(d);
3559 +                                       continue;
3560 +                               }
3561 +                               err = au_reval_dpath(d, sigen);
3562 +                               if (!err)
3563 +                                       di_downgrade_lock(d, AuLock_IR);
3564 +                               else {
3565 +                                       di_write_unlock(d);
3566 +                                       break;
3567 +                               }
3568 +                       }
3569 +
3570 +                       /* AuDbgDentry(d); */
3571 +                       btop = au_dbtop(d);
3572 +                       bbot = au_dbbot(d);
3573 +                       if (btop <= bindex
3574 +                           && bindex <= bbot
3575 +                           && au_h_dptr(d, bindex)
3576 +                           && au_test_dbusy(d, btop, bbot)) {
3577 +                               err = -EBUSY;
3578 +                               AuVerbose(verbose, "busy %pd\n", d);
3579 +                               AuDbgDentry(d);
3580 +                       }
3581 +                       di_read_unlock(d, AuLock_IR);
3582 +               }
3583 +       }
3584 +
3585 +out_dpages:
3586 +       au_dpages_free(&dpages);
3587 +out:
3588 +       return err;
3589 +}
3590 +
3591 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3592 +                          unsigned int sigen, const unsigned int verbose)
3593 +{
3594 +       int err;
3595 +       unsigned long long max, ull;
3596 +       struct inode *i, **array;
3597 +       aufs_bindex_t btop, bbot;
3598 +
3599 +       array = au_iarray_alloc(sb, &max);
3600 +       err = PTR_ERR(array);
3601 +       if (IS_ERR(array))
3602 +               goto out;
3603 +
3604 +       err = 0;
3605 +       AuDbg("b%d\n", bindex);
3606 +       for (ull = 0; !err && ull < max; ull++) {
3607 +               i = array[ull];
3608 +               if (unlikely(!i))
3609 +                       break;
3610 +               if (i->i_ino == AUFS_ROOT_INO)
3611 +                       continue;
3612 +
3613 +               /* AuDbgInode(i); */
3614 +               if (au_iigen(i, NULL) == sigen)
3615 +                       ii_read_lock_child(i);
3616 +               else {
3617 +                       ii_write_lock_child(i);
3618 +                       err = au_refresh_hinode_self(i);
3619 +                       au_iigen_dec(i);
3620 +                       if (!err)
3621 +                               ii_downgrade_lock(i);
3622 +                       else {
3623 +                               ii_write_unlock(i);
3624 +                               break;
3625 +                       }
3626 +               }
3627 +
3628 +               btop = au_ibtop(i);
3629 +               bbot = au_ibbot(i);
3630 +               if (btop <= bindex
3631 +                   && bindex <= bbot
3632 +                   && au_h_iptr(i, bindex)
3633 +                   && au_test_ibusy(i, btop, bbot)) {
3634 +                       err = -EBUSY;
3635 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3636 +                       AuDbgInode(i);
3637 +               }
3638 +               ii_read_unlock(i);
3639 +       }
3640 +       au_iarray_free(array, max);
3641 +
3642 +out:
3643 +       return err;
3644 +}
3645 +
3646 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3647 +                             const unsigned int verbose)
3648 +{
3649 +       int err;
3650 +       unsigned int sigen;
3651 +
3652 +       sigen = au_sigen(root->d_sb);
3653 +       DiMustNoWaiters(root);
3654 +       IiMustNoWaiters(d_inode(root));
3655 +       di_write_unlock(root);
3656 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3657 +       if (!err)
3658 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3659 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3660 +
3661 +       return err;
3662 +}
3663 +
3664 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3665 +                        struct file **to_free, int *idx)
3666 +{
3667 +       int err;
3668 +       unsigned char matched, root;
3669 +       aufs_bindex_t bindex, bbot;
3670 +       struct au_fidir *fidir;
3671 +       struct au_hfile *hfile;
3672 +
3673 +       err = 0;
3674 +       root = IS_ROOT(file->f_path.dentry);
3675 +       if (root) {
3676 +               get_file(file);
3677 +               to_free[*idx] = file;
3678 +               (*idx)++;
3679 +               goto out;
3680 +       }
3681 +
3682 +       matched = 0;
3683 +       fidir = au_fi(file)->fi_hdir;
3684 +       AuDebugOn(!fidir);
3685 +       bbot = au_fbbot_dir(file);
3686 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3687 +               hfile = fidir->fd_hfile + bindex;
3688 +               if (!hfile->hf_file)
3689 +                       continue;
3690 +
3691 +               if (hfile->hf_br->br_id == br_id) {
3692 +                       matched = 1;
3693 +                       break;
3694 +               }
3695 +       }
3696 +       if (matched)
3697 +               err = -EBUSY;
3698 +
3699 +out:
3700 +       return err;
3701 +}
3702 +
3703 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3704 +                         struct file **to_free, int opened)
3705 +{
3706 +       int err, idx;
3707 +       unsigned long long ull, max;
3708 +       aufs_bindex_t btop;
3709 +       struct file *file, **array;
3710 +       struct dentry *root;
3711 +       struct au_hfile *hfile;
3712 +
3713 +       array = au_farray_alloc(sb, &max);
3714 +       err = PTR_ERR(array);
3715 +       if (IS_ERR(array))
3716 +               goto out;
3717 +
3718 +       err = 0;
3719 +       idx = 0;
3720 +       root = sb->s_root;
3721 +       di_write_unlock(root);
3722 +       for (ull = 0; ull < max; ull++) {
3723 +               file = array[ull];
3724 +               if (unlikely(!file))
3725 +                       break;
3726 +
3727 +               /* AuDbg("%pD\n", file); */
3728 +               fi_read_lock(file);
3729 +               btop = au_fbtop(file);
3730 +               if (!d_is_dir(file->f_path.dentry)) {
3731 +                       hfile = &au_fi(file)->fi_htop;
3732 +                       if (hfile->hf_br->br_id == br_id)
3733 +                               err = -EBUSY;
3734 +               } else
3735 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3736 +               fi_read_unlock(file);
3737 +               if (unlikely(err))
3738 +                       break;
3739 +       }
3740 +       di_write_lock_child(root);
3741 +       au_farray_free(array, max);
3742 +       AuDebugOn(idx > opened);
3743 +
3744 +out:
3745 +       return err;
3746 +}
3747 +
3748 +static void br_del_file(struct file **to_free, unsigned long long opened,
3749 +                       aufs_bindex_t br_id)
3750 +{
3751 +       unsigned long long ull;
3752 +       aufs_bindex_t bindex, btop, bbot, bfound;
3753 +       struct file *file;
3754 +       struct au_fidir *fidir;
3755 +       struct au_hfile *hfile;
3756 +
3757 +       for (ull = 0; ull < opened; ull++) {
3758 +               file = to_free[ull];
3759 +               if (unlikely(!file))
3760 +                       break;
3761 +
3762 +               /* AuDbg("%pD\n", file); */
3763 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3764 +               bfound = -1;
3765 +               fidir = au_fi(file)->fi_hdir;
3766 +               AuDebugOn(!fidir);
3767 +               fi_write_lock(file);
3768 +               btop = au_fbtop(file);
3769 +               bbot = au_fbbot_dir(file);
3770 +               for (bindex = btop; bindex <= bbot; bindex++) {
3771 +                       hfile = fidir->fd_hfile + bindex;
3772 +                       if (!hfile->hf_file)
3773 +                               continue;
3774 +
3775 +                       if (hfile->hf_br->br_id == br_id) {
3776 +                               bfound = bindex;
3777 +                               break;
3778 +                       }
3779 +               }
3780 +               AuDebugOn(bfound < 0);
3781 +               au_set_h_fptr(file, bfound, NULL);
3782 +               if (bfound == btop) {
3783 +                       for (btop++; btop <= bbot; btop++)
3784 +                               if (au_hf_dir(file, btop)) {
3785 +                                       au_set_fbtop(file, btop);
3786 +                                       break;
3787 +                               }
3788 +               }
3789 +               fi_write_unlock(file);
3790 +       }
3791 +}
3792 +
3793 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3794 +                            const aufs_bindex_t bindex,
3795 +                            const aufs_bindex_t bbot)
3796 +{
3797 +       struct au_branch **brp, **p;
3798 +
3799 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3800 +
3801 +       brp = sbinfo->si_branch + bindex;
3802 +       if (bindex < bbot)
3803 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3804 +       sbinfo->si_branch[0 + bbot] = NULL;
3805 +       sbinfo->si_bbot--;
3806 +
3807 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3808 +                       /*may_shrink*/1);
3809 +       if (p)
3810 +               sbinfo->si_branch = p;
3811 +       /* harmless error */
3812 +}
3813 +
3814 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3815 +                            const aufs_bindex_t bbot)
3816 +{
3817 +       struct au_hdentry *hdp, *p;
3818 +
3819 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3820 +
3821 +       hdp = au_hdentry(dinfo, bindex);
3822 +       if (bindex < bbot)
3823 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3824 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3825 +       dinfo->di_bbot--;
3826 +
3827 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3828 +                       /*may_shrink*/1);
3829 +       if (p)
3830 +               dinfo->di_hdentry = p;
3831 +       /* harmless error */
3832 +}
3833 +
3834 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3835 +                            const aufs_bindex_t bbot)
3836 +{
3837 +       struct au_hinode *hip, *p;
3838 +
3839 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3840 +
3841 +       hip = au_hinode(iinfo, bindex);
3842 +       if (bindex < bbot)
3843 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3844 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3845 +       iinfo->ii_bbot--;
3846 +
3847 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3848 +                       /*may_shrink*/1);
3849 +       if (p)
3850 +               iinfo->ii_hinode = p;
3851 +       /* harmless error */
3852 +}
3853 +
3854 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3855 +                        struct au_branch *br)
3856 +{
3857 +       aufs_bindex_t bbot;
3858 +       struct au_sbinfo *sbinfo;
3859 +       struct dentry *root, *h_root;
3860 +       struct inode *inode, *h_inode;
3861 +       struct au_hinode *hinode;
3862 +
3863 +       SiMustWriteLock(sb);
3864 +
3865 +       root = sb->s_root;
3866 +       inode = d_inode(root);
3867 +       sbinfo = au_sbi(sb);
3868 +       bbot = sbinfo->si_bbot;
3869 +
3870 +       h_root = au_h_dptr(root, bindex);
3871 +       hinode = au_hi(inode, bindex);
3872 +       h_inode = au_igrab(hinode->hi_inode);
3873 +       au_hiput(hinode);
3874 +
3875 +       au_sbilist_lock();
3876 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3877 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3878 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3879 +       au_sbilist_unlock();
3880 +
3881 +       /* ignore an error */
3882 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3883 +
3884 +       dput(h_root);
3885 +       iput(h_inode);
3886 +       au_br_do_free(br);
3887 +}
3888 +
3889 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3890 +                                  unsigned long long max, void *arg)
3891 +{
3892 +       return max;
3893 +}
3894 +
3895 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3896 +{
3897 +       int err, rerr, i;
3898 +       unsigned long long opened;
3899 +       unsigned int mnt_flags;
3900 +       aufs_bindex_t bindex, bbot, br_id;
3901 +       unsigned char do_wh, verbose;
3902 +       struct au_branch *br;
3903 +       struct au_wbr *wbr;
3904 +       struct dentry *root;
3905 +       struct file **to_free;
3906 +
3907 +       err = 0;
3908 +       opened = 0;
3909 +       to_free = NULL;
3910 +       root = sb->s_root;
3911 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3912 +       if (bindex < 0) {
3913 +               if (remount)
3914 +                       goto out; /* success */
3915 +               err = -ENOENT;
3916 +               pr_err("%s no such branch\n", del->pathname);
3917 +               goto out;
3918 +       }
3919 +       AuDbg("bindex b%d\n", bindex);
3920 +
3921 +       err = -EBUSY;
3922 +       mnt_flags = au_mntflags(sb);
3923 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3924 +       bbot = au_sbbot(sb);
3925 +       if (unlikely(!bbot)) {
3926 +               AuVerbose(verbose, "no more branches left\n");
3927 +               goto out;
3928 +       }
3929 +
3930 +       br = au_sbr(sb, bindex);
3931 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3932 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3933 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3934 +               goto out;
3935 +       }
3936 +
3937 +       br_id = br->br_id;
3938 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3939 +       if (unlikely(opened)) {
3940 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3941 +               err = PTR_ERR(to_free);
3942 +               if (IS_ERR(to_free))
3943 +                       goto out;
3944 +
3945 +               err = test_file_busy(sb, br_id, to_free, opened);
3946 +               if (unlikely(err)) {
3947 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3948 +                       goto out;
3949 +               }
3950 +       }
3951 +
3952 +       wbr = br->br_wbr;
3953 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3954 +       if (do_wh) {
3955 +               /* instead of WbrWhMustWriteLock(wbr) */
3956 +               SiMustWriteLock(sb);
3957 +               for (i = 0; i < AuBrWh_Last; i++) {
3958 +                       dput(wbr->wbr_wh[i]);
3959 +                       wbr->wbr_wh[i] = NULL;
3960 +               }
3961 +       }
3962 +
3963 +       err = test_children_busy(root, bindex, verbose);
3964 +       if (unlikely(err)) {
3965 +               if (do_wh)
3966 +                       goto out_wh;
3967 +               goto out;
3968 +       }
3969 +
3970 +       err = 0;
3971 +       if (to_free) {
3972 +               /*
3973 +                * now we confirmed the branch is deletable.
3974 +                * let's free the remaining opened dirs on the branch.
3975 +                */
3976 +               di_write_unlock(root);
3977 +               br_del_file(to_free, opened, br_id);
3978 +               di_write_lock_child(root);
3979 +       }
3980 +
3981 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3982 +       dbgaufs_xino_del(br);           /* remove one */
3983 +       au_br_do_del(sb, bindex, br);
3984 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3985 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3986 +
3987 +       if (!bindex) {
3988 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3989 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3990 +       } else
3991 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3992 +       if (au_opt_test(mnt_flags, PLINK))
3993 +               au_plink_half_refresh(sb, br_id);
3994 +
3995 +       goto out; /* success */
3996 +
3997 +out_wh:
3998 +       /* revert */
3999 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4000 +       if (rerr)
4001 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4002 +                       del->pathname, rerr);
4003 +out:
4004 +       if (to_free)
4005 +               au_farray_free(to_free, opened);
4006 +       return err;
4007 +}
4008 +
4009 +/* ---------------------------------------------------------------------- */
4010 +
4011 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4012 +{
4013 +       int err;
4014 +       aufs_bindex_t btop, bbot;
4015 +       struct aufs_ibusy ibusy;
4016 +       struct inode *inode, *h_inode;
4017 +
4018 +       err = -EPERM;
4019 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4020 +               goto out;
4021 +
4022 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4023 +       if (!err)
4024 +               /* VERIFY_WRITE */
4025 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4026 +       if (unlikely(err)) {
4027 +               err = -EFAULT;
4028 +               AuTraceErr(err);
4029 +               goto out;
4030 +       }
4031 +
4032 +       err = -EINVAL;
4033 +       si_read_lock(sb, AuLock_FLUSH);
4034 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4035 +               goto out_unlock;
4036 +
4037 +       err = 0;
4038 +       ibusy.h_ino = 0; /* invalid */
4039 +       inode = ilookup(sb, ibusy.ino);
4040 +       if (!inode
4041 +           || inode->i_ino == AUFS_ROOT_INO
4042 +           || au_is_bad_inode(inode))
4043 +               goto out_unlock;
4044 +
4045 +       ii_read_lock_child(inode);
4046 +       btop = au_ibtop(inode);
4047 +       bbot = au_ibbot(inode);
4048 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4049 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4050 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4051 +                       ibusy.h_ino = h_inode->i_ino;
4052 +       }
4053 +       ii_read_unlock(inode);
4054 +       iput(inode);
4055 +
4056 +out_unlock:
4057 +       si_read_unlock(sb);
4058 +       if (!err) {
4059 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4060 +               if (unlikely(err)) {
4061 +                       err = -EFAULT;
4062 +                       AuTraceErr(err);
4063 +               }
4064 +       }
4065 +out:
4066 +       return err;
4067 +}
4068 +
4069 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4070 +{
4071 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4072 +}
4073 +
4074 +#ifdef CONFIG_COMPAT
4075 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4076 +{
4077 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4078 +}
4079 +#endif
4080 +
4081 +/* ---------------------------------------------------------------------- */
4082 +
4083 +/*
4084 + * change a branch permission
4085 + */
4086 +
4087 +static void au_warn_ima(void)
4088 +{
4089 +#ifdef CONFIG_IMA
4090 +       /* since it doesn't support mark_files_ro() */
4091 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4092 +#endif
4093 +}
4094 +
4095 +static int do_need_sigen_inc(int a, int b)
4096 +{
4097 +       return au_br_whable(a) && !au_br_whable(b);
4098 +}
4099 +
4100 +static int need_sigen_inc(int old, int new)
4101 +{
4102 +       return do_need_sigen_inc(old, new)
4103 +               || do_need_sigen_inc(new, old);
4104 +}
4105 +
4106 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4107 +{
4108 +       int err, do_warn;
4109 +       unsigned int mnt_flags;
4110 +       unsigned long long ull, max;
4111 +       aufs_bindex_t br_id;
4112 +       unsigned char verbose, writer;
4113 +       struct file *file, *hf, **array;
4114 +       struct au_hfile *hfile;
4115 +       struct inode *h_inode;
4116 +
4117 +       mnt_flags = au_mntflags(sb);
4118 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4119 +
4120 +       array = au_farray_alloc(sb, &max);
4121 +       err = PTR_ERR(array);
4122 +       if (IS_ERR(array))
4123 +               goto out;
4124 +
4125 +       do_warn = 0;
4126 +       br_id = au_sbr_id(sb, bindex);
4127 +       for (ull = 0; ull < max; ull++) {
4128 +               file = array[ull];
4129 +               if (unlikely(!file))
4130 +                       break;
4131 +
4132 +               /* AuDbg("%pD\n", file); */
4133 +               fi_read_lock(file);
4134 +               if (unlikely(au_test_mmapped(file))) {
4135 +                       err = -EBUSY;
4136 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4137 +                       AuDbgFile(file);
4138 +                       FiMustNoWaiters(file);
4139 +                       fi_read_unlock(file);
4140 +                       goto out_array;
4141 +               }
4142 +
4143 +               hfile = &au_fi(file)->fi_htop;
4144 +               hf = hfile->hf_file;
4145 +               if (!d_is_reg(file->f_path.dentry)
4146 +                   || !(file->f_mode & FMODE_WRITE)
4147 +                   || hfile->hf_br->br_id != br_id
4148 +                   || !(hf->f_mode & FMODE_WRITE))
4149 +                       array[ull] = NULL;
4150 +               else {
4151 +                       do_warn = 1;
4152 +                       get_file(file);
4153 +               }
4154 +
4155 +               FiMustNoWaiters(file);
4156 +               fi_read_unlock(file);
4157 +               fput(file);
4158 +       }
4159 +
4160 +       err = 0;
4161 +       if (do_warn)
4162 +               au_warn_ima();
4163 +
4164 +       for (ull = 0; ull < max; ull++) {
4165 +               file = array[ull];
4166 +               if (!file)
4167 +                       continue;
4168 +
4169 +               /* todo: already flushed? */
4170 +               /*
4171 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4172 +                * approach which resets f_mode and calls mnt_drop_write() and
4173 +                * file_release_write() for each file, because the branch
4174 +                * attribute in aufs world is totally different from the native
4175 +                * fs rw/ro mode.
4176 +                */
4177 +               /* fi_read_lock(file); */
4178 +               hfile = &au_fi(file)->fi_htop;
4179 +               hf = hfile->hf_file;
4180 +               /* fi_read_unlock(file); */
4181 +               spin_lock(&hf->f_lock);
4182 +               writer = !!(hf->f_mode & FMODE_WRITER);
4183 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4184 +               spin_unlock(&hf->f_lock);
4185 +               if (writer) {
4186 +                       h_inode = file_inode(hf);
4187 +                       if (hf->f_mode & FMODE_READ)
4188 +                               i_readcount_inc(h_inode);
4189 +                       put_write_access(h_inode);
4190 +                       mnt_put_write_access(hf->f_path.mnt);
4191 +               }
4192 +       }
4193 +
4194 +out_array:
4195 +       au_farray_free(array, max);
4196 +out:
4197 +       AuTraceErr(err);
4198 +       return err;
4199 +}
4200 +
4201 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4202 +             int *do_refresh)
4203 +{
4204 +       int err, rerr;
4205 +       aufs_bindex_t bindex;
4206 +       struct dentry *root;
4207 +       struct au_branch *br;
4208 +       struct au_br_fhsm *bf;
4209 +
4210 +       root = sb->s_root;
4211 +       bindex = au_find_dbindex(root, mod->h_root);
4212 +       if (bindex < 0) {
4213 +               if (remount)
4214 +                       return 0; /* success */
4215 +               err = -ENOENT;
4216 +               pr_err("%s no such branch\n", mod->path);
4217 +               goto out;
4218 +       }
4219 +       AuDbg("bindex b%d\n", bindex);
4220 +
4221 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4222 +       if (unlikely(err))
4223 +               goto out;
4224 +
4225 +       br = au_sbr(sb, bindex);
4226 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4227 +       if (br->br_perm == mod->perm)
4228 +               return 0; /* success */
4229 +
4230 +       /* pre-allocate for non-fhsm --> fhsm */
4231 +       bf = NULL;
4232 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4233 +               err = au_fhsm_br_alloc(br);
4234 +               if (unlikely(err))
4235 +                       goto out;
4236 +               bf = br->br_fhsm;
4237 +               br->br_fhsm = NULL;
4238 +       }
4239 +
4240 +       if (au_br_writable(br->br_perm)) {
4241 +               /* remove whiteout base */
4242 +               err = au_br_init_wh(sb, br, mod->perm);
4243 +               if (unlikely(err))
4244 +                       goto out_bf;
4245 +
4246 +               if (!au_br_writable(mod->perm)) {
4247 +                       /* rw --> ro, file might be mmapped */
4248 +                       DiMustNoWaiters(root);
4249 +                       IiMustNoWaiters(d_inode(root));
4250 +                       di_write_unlock(root);
4251 +                       err = au_br_mod_files_ro(sb, bindex);
4252 +                       /* aufs_write_lock() calls ..._child() */
4253 +                       di_write_lock_child(root);
4254 +
4255 +                       if (unlikely(err)) {
4256 +                               rerr = -ENOMEM;
4257 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4258 +                                                    GFP_NOFS);
4259 +                               if (br->br_wbr)
4260 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4261 +                               if (unlikely(rerr)) {
4262 +                                       AuIOErr("nested error %d (%d)\n",
4263 +                                               rerr, err);
4264 +                                       br->br_perm = mod->perm;
4265 +                               }
4266 +                       }
4267 +               }
4268 +       } else if (au_br_writable(mod->perm)) {
4269 +               /* ro --> rw */
4270 +               err = -ENOMEM;
4271 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4272 +               if (br->br_wbr) {
4273 +                       err = au_wbr_init(br, sb, mod->perm);
4274 +                       if (unlikely(err)) {
4275 +                               au_kfree_rcu(br->br_wbr);
4276 +                               br->br_wbr = NULL;
4277 +                       }
4278 +               }
4279 +       }
4280 +       if (unlikely(err))
4281 +               goto out_bf;
4282 +
4283 +       if (au_br_fhsm(br->br_perm)) {
4284 +               if (!au_br_fhsm(mod->perm)) {
4285 +                       /* fhsm --> non-fhsm */
4286 +                       au_br_fhsm_fin(br->br_fhsm);
4287 +                       au_kfree_rcu(br->br_fhsm);
4288 +                       br->br_fhsm = NULL;
4289 +               }
4290 +       } else if (au_br_fhsm(mod->perm))
4291 +               /* non-fhsm --> fhsm */
4292 +               br->br_fhsm = bf;
4293 +
4294 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4295 +       br->br_perm = mod->perm;
4296 +       goto out; /* success */
4297 +
4298 +out_bf:
4299 +       au_kfree_try_rcu(bf);
4300 +out:
4301 +       AuTraceErr(err);
4302 +       return err;
4303 +}
4304 +
4305 +/* ---------------------------------------------------------------------- */
4306 +
4307 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4308 +{
4309 +       int err;
4310 +       struct kstatfs kstfs;
4311 +
4312 +       err = vfs_statfs(&br->br_path, &kstfs);
4313 +       if (!err) {
4314 +               stfs->f_blocks = kstfs.f_blocks;
4315 +               stfs->f_bavail = kstfs.f_bavail;
4316 +               stfs->f_files = kstfs.f_files;
4317 +               stfs->f_ffree = kstfs.f_ffree;
4318 +       }
4319 +
4320 +       return err;
4321 +}
4322 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4323 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4324 +++ linux/fs/aufs/branch.h      2023-10-31 09:31:04.196547417 +0100
4325 @@ -0,0 +1,375 @@
4326 +/* SPDX-License-Identifier: GPL-2.0 */
4327 +/*
4328 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4329 + *
4330 + * This program is free software; you can redistribute it and/or modify
4331 + * it under the terms of the GNU General Public License as published by
4332 + * the Free Software Foundation; either version 2 of the License, or
4333 + * (at your option) any later version.
4334 + *
4335 + * This program is distributed in the hope that it will be useful,
4336 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4337 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4338 + * GNU General Public License for more details.
4339 + *
4340 + * You should have received a copy of the GNU General Public License
4341 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4342 + */
4343 +
4344 +/*
4345 + * branch filesystems and xino for them
4346 + */
4347 +
4348 +#ifndef __AUFS_BRANCH_H__
4349 +#define __AUFS_BRANCH_H__
4350 +
4351 +#ifdef __KERNEL__
4352 +
4353 +#include <linux/mount.h>
4354 +#include "dirren.h"
4355 +#include "dynop.h"
4356 +#include "lcnt.h"
4357 +#include "rwsem.h"
4358 +#include "super.h"
4359 +
4360 +/* ---------------------------------------------------------------------- */
4361 +
4362 +/* a xino file */
4363 +struct au_xino {
4364 +       struct file             **xi_file;
4365 +       unsigned int            xi_nfile;
4366 +
4367 +       struct {
4368 +               spinlock_t              spin;
4369 +               ino_t                   *array;
4370 +               int                     total;
4371 +               /* reserved for future use */
4372 +               /* unsigned long        *bitmap; */
4373 +               wait_queue_head_t       wqh;
4374 +       } xi_nondir;
4375 +
4376 +       struct mutex            xi_mtx; /* protects xi_file array */
4377 +       struct hlist_bl_head    xi_writing;
4378 +
4379 +       atomic_t                xi_truncating;
4380 +
4381 +       struct kref             xi_kref;
4382 +};
4383 +
4384 +/* File-based Hierarchical Storage Management */
4385 +struct au_br_fhsm {
4386 +#ifdef CONFIG_AUFS_FHSM
4387 +       struct mutex            bf_lock;
4388 +       unsigned long           bf_jiffy;
4389 +       struct aufs_stfs        bf_stfs;
4390 +       int                     bf_readable;
4391 +#endif
4392 +};
4393 +
4394 +/* members for writable branch only */
4395 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4396 +struct au_wbr {
4397 +       struct au_rwsem         wbr_wh_rwsem;
4398 +       struct dentry           *wbr_wh[AuBrWh_Last];
4399 +       atomic_t                wbr_wh_running;
4400 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4401 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4402 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4403 +
4404 +       /* mfs mode */
4405 +       unsigned long long      wbr_bytes;
4406 +};
4407 +
4408 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4409 +#define AuBrDynOp (AuDyLast * 4)
4410 +
4411 +#ifdef CONFIG_AUFS_HFSNOTIFY
4412 +/* support for asynchronous destruction */
4413 +struct au_br_hfsnotify {
4414 +       struct fsnotify_group   *hfsn_group;
4415 +};
4416 +#endif
4417 +
4418 +/* sysfs entries */
4419 +struct au_brsysfs {
4420 +       char                    name[16];
4421 +       struct attribute        attr;
4422 +};
4423 +
4424 +enum {
4425 +       AuBrSysfs_BR,
4426 +       AuBrSysfs_BRID,
4427 +       AuBrSysfs_Last
4428 +};
4429 +
4430 +/* protected by superblock rwsem */
4431 +struct au_branch {
4432 +       struct au_xino          *br_xino;
4433 +
4434 +       aufs_bindex_t           br_id;
4435 +
4436 +       int                     br_perm;
4437 +       struct path             br_path;
4438 +       spinlock_t              br_dykey_lock;
4439 +       struct au_dykey         *br_dykey[AuBrDynOp];
4440 +       au_lcnt_t               br_nfiles;      /* opened files */
4441 +       au_lcnt_t               br_count;       /* in-use for other */
4442 +
4443 +       struct au_wbr           *br_wbr;
4444 +       struct au_br_fhsm       *br_fhsm;
4445 +
4446 +#ifdef CONFIG_AUFS_HFSNOTIFY
4447 +       struct au_br_hfsnotify  *br_hfsn;
4448 +#endif
4449 +
4450 +#ifdef CONFIG_SYSFS
4451 +       /* entries under sysfs per mount-point */
4452 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4453 +#endif
4454 +
4455 +#ifdef CONFIG_DEBUG_FS
4456 +       struct dentry            *br_dbgaufs; /* xino */
4457 +#endif
4458 +
4459 +       struct au_dr_br         br_dirren;
4460 +};
4461 +
4462 +/* ---------------------------------------------------------------------- */
4463 +
4464 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4465 +{
4466 +       return br->br_path.mnt;
4467 +}
4468 +
4469 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4470 +{
4471 +       return br->br_path.dentry;
4472 +}
4473 +
4474 +static inline struct mnt_idmap *au_br_idmap(struct au_branch *br)
4475 +{
4476 +       return mnt_idmap(br->br_path.mnt);
4477 +}
4478 +
4479 +static inline struct super_block *au_br_sb(struct au_branch *br)
4480 +{
4481 +       return au_br_mnt(br)->mnt_sb;
4482 +}
4483 +
4484 +static inline int au_br_rdonly(struct au_branch *br)
4485 +{
4486 +       return (sb_rdonly(au_br_sb(br))
4487 +               || !au_br_writable(br->br_perm))
4488 +               ? -EROFS : 0;
4489 +}
4490 +
4491 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4492 +{
4493 +#ifdef CONFIG_AUFS_HNOTIFY
4494 +       return !(brperm & AuBrPerm_RR);
4495 +#else
4496 +       return 0;
4497 +#endif
4498 +}
4499 +
4500 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4501 +{
4502 +       int err, exec_flag;
4503 +
4504 +       err = 0;
4505 +       exec_flag = oflag & __FMODE_EXEC;
4506 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4507 +               err = -EACCES;
4508 +
4509 +       return err;
4510 +}
4511 +
4512 +static inline void au_xino_get(struct au_branch *br)
4513 +{
4514 +       struct au_xino *xi;
4515 +
4516 +       xi = br->br_xino;
4517 +       if (xi)
4518 +               kref_get(&xi->xi_kref);
4519 +}
4520 +
4521 +static inline int au_xino_count(struct au_branch *br)
4522 +{
4523 +       int v;
4524 +       struct au_xino *xi;
4525 +
4526 +       v = 0;
4527 +       xi = br->br_xino;
4528 +       if (xi)
4529 +               v = kref_read(&xi->xi_kref);
4530 +
4531 +       return v;
4532 +}
4533 +
4534 +/* ---------------------------------------------------------------------- */
4535 +
4536 +/* branch.c */
4537 +struct au_sbinfo;
4538 +void au_br_free(struct au_sbinfo *sinfo);
4539 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4540 +struct au_opt_add;
4541 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4542 +struct au_opt_del;
4543 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4544 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4545 +#ifdef CONFIG_COMPAT
4546 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4547 +#endif
4548 +struct au_opt_mod;
4549 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4550 +             int *do_refresh);
4551 +struct aufs_stfs;
4552 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4553 +
4554 +/* xino.c */
4555 +static const loff_t au_loff_max = LLONG_MAX;
4556 +
4557 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4558 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4559 +                           int wbrtop);
4560 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4561 +                            struct file *copy_src);
4562 +struct au_xi_new {
4563 +       struct au_xino *xi;     /* switch between xino and xigen */
4564 +       int idx;
4565 +       struct path *base;
4566 +       struct file *copy_src;
4567 +};
4568 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4569 +
4570 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4571 +                ino_t *ino);
4572 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4573 +                 ino_t ino);
4574 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4575 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4576 +
4577 +int au_xib_trunc(struct super_block *sb);
4578 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4579 +
4580 +struct au_xino *au_xino_alloc(unsigned int nfile);
4581 +int au_xino_put(struct au_branch *br);
4582 +struct file *au_xino_file1(struct au_xino *xi);
4583 +
4584 +struct au_opt_xino;
4585 +void au_xino_clr(struct super_block *sb);
4586 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4587 +struct file *au_xino_def(struct super_block *sb);
4588 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4589 +                   struct path *base);
4590 +
4591 +ino_t au_xino_new_ino(struct super_block *sb);
4592 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4593 +
4594 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4595 +                      ino_t h_ino, int idx);
4596 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4597 +                     int *idx);
4598 +
4599 +int au_xino_path(struct seq_file *seq, struct file *file);
4600 +
4601 +/* ---------------------------------------------------------------------- */
4602 +
4603 +/* @idx is signed to accept -1 meaning the first file */
4604 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4605 +{
4606 +       struct file *file;
4607 +
4608 +       file = NULL;
4609 +       if (!xi)
4610 +               goto out;
4611 +
4612 +       if (idx >= 0) {
4613 +               if (idx < xi->xi_nfile)
4614 +                       file = xi->xi_file[idx];
4615 +       } else
4616 +               file = au_xino_file1(xi);
4617 +
4618 +out:
4619 +       return file;
4620 +}
4621 +
4622 +/* ---------------------------------------------------------------------- */
4623 +
4624 +/* Superblock to branch */
4625 +static inline
4626 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4627 +{
4628 +       return au_sbr(sb, bindex)->br_id;
4629 +}
4630 +
4631 +static inline
4632 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4633 +{
4634 +       return au_br_mnt(au_sbr(sb, bindex));
4635 +}
4636 +
4637 +static inline
4638 +struct mnt_idmap *au_sbr_idmap(struct super_block *sb, aufs_bindex_t bindex)
4639 +{
4640 +       return au_br_idmap(au_sbr(sb, bindex));
4641 +}
4642 +
4643 +static inline
4644 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4645 +{
4646 +       return au_br_sb(au_sbr(sb, bindex));
4647 +}
4648 +
4649 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4650 +{
4651 +       return au_sbr(sb, bindex)->br_perm;
4652 +}
4653 +
4654 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4655 +{
4656 +       return au_br_whable(au_sbr_perm(sb, bindex));
4657 +}
4658 +
4659 +/* ---------------------------------------------------------------------- */
4660 +
4661 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4662 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4663 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4664 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4665 +/*
4666 +#define wbr_wh_read_trylock_nested(wbr) \
4667 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4668 +#define wbr_wh_write_trylock_nested(wbr) \
4669 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4670 +*/
4671 +
4672 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4673 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4674 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4675 +
4676 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4677 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4678 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4679 +
4680 +/* ---------------------------------------------------------------------- */
4681 +
4682 +#ifdef CONFIG_AUFS_FHSM
4683 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4684 +{
4685 +       mutex_init(&brfhsm->bf_lock);
4686 +       brfhsm->bf_jiffy = 0;
4687 +       brfhsm->bf_readable = 0;
4688 +}
4689 +
4690 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4691 +{
4692 +       mutex_destroy(&brfhsm->bf_lock);
4693 +}
4694 +#else
4695 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4696 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4697 +#endif
4698 +
4699 +#endif /* __KERNEL__ */
4700 +#endif /* __AUFS_BRANCH_H__ */
4701 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4702 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4703 +++ linux/fs/aufs/conf.mk       2022-11-05 23:02:18.959222617 +0100
4704 @@ -0,0 +1,40 @@
4705 +# SPDX-License-Identifier: GPL-2.0
4706 +
4707 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4708 +
4709 +define AuConf
4710 +ifdef ${1}
4711 +AuConfStr += ${1}=${${1}}
4712 +endif
4713 +endef
4714 +
4715 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4716 +       SBILIST \
4717 +       HNOTIFY HFSNOTIFY \
4718 +       EXPORT INO_T_64 \
4719 +       XATTR \
4720 +       FHSM \
4721 +       RDU \
4722 +       DIRREN \
4723 +       SHWH \
4724 +       BR_RAMFS \
4725 +       BR_FUSE POLL \
4726 +       BR_HFSPLUS \
4727 +       BDEV_LOOP \
4728 +       DEBUG MAGIC_SYSRQ
4729 +$(foreach i, ${AuConfAll}, \
4730 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4731 +
4732 +AuConfName = ${obj}/conf.str
4733 +${AuConfName}.tmp: FORCE
4734 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4735 +${AuConfName}: ${AuConfName}.tmp
4736 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4737 +       echo '  GEN    ' $@; \
4738 +       cp -p $< $@; \
4739 +       }
4740 +FORCE:
4741 +clean-files += ${AuConfName} ${AuConfName}.tmp
4742 +${obj}/sysfs.o: ${AuConfName}
4743 +
4744 +-include ${srctree}/${src}/conf_priv.mk
4745 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4746 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4747 +++ linux/fs/aufs/cpup.c        2024-03-10 23:40:47.083143450 +0100
4748 @@ -0,0 +1,1459 @@
4749 +// SPDX-License-Identifier: GPL-2.0
4750 +/*
4751 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4752 + *
4753 + * This program is free software; you can redistribute it and/or modify
4754 + * it under the terms of the GNU General Public License as published by
4755 + * the Free Software Foundation; either version 2 of the License, or
4756 + * (at your option) any later version.
4757 + *
4758 + * This program is distributed in the hope that it will be useful,
4759 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4760 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4761 + * GNU General Public License for more details.
4762 + *
4763 + * You should have received a copy of the GNU General Public License
4764 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4765 + */
4766 +
4767 +/*
4768 + * copy-up functions, see wbr_policy.c for copy-down
4769 + */
4770 +
4771 +#include <linux/fs_stack.h>
4772 +#include <linux/mm.h>
4773 +#include <linux/task_work.h>
4774 +#include "aufs.h"
4775 +
4776 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4777 +{
4778 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4779 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4780 +
4781 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4782 +
4783 +       dst->i_flags |= iflags & ~mask;
4784 +       if (au_test_fs_notime(dst->i_sb))
4785 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4786 +}
4787 +
4788 +void au_cpup_attr_timesizes(struct inode *inode)
4789 +{
4790 +       struct inode *h_inode;
4791 +
4792 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4793 +       fsstack_copy_attr_times(inode, h_inode);
4794 +       fsstack_copy_inode_size(inode, h_inode);
4795 +}
4796 +
4797 +void au_cpup_attr_nlink(struct inode *inode, int force)
4798 +{
4799 +       struct inode *h_inode;
4800 +       struct super_block *sb;
4801 +       aufs_bindex_t bindex, bbot;
4802 +
4803 +       sb = inode->i_sb;
4804 +       bindex = au_ibtop(inode);
4805 +       h_inode = au_h_iptr(inode, bindex);
4806 +       if (!force
4807 +           && !S_ISDIR(h_inode->i_mode)
4808 +           && au_opt_test(au_mntflags(sb), PLINK)
4809 +           && au_plink_test(inode))
4810 +               return;
4811 +
4812 +       /*
4813 +        * 0 can happen in revalidating.
4814 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4815 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4816 +        * case.
4817 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4818 +        *       the incorrect link count.
4819 +        */
4820 +       set_nlink(inode, h_inode->i_nlink);
4821 +
4822 +       /*
4823 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4824 +        * it may includes whplink directory.
4825 +        */
4826 +       if (S_ISDIR(h_inode->i_mode)) {
4827 +               bbot = au_ibbot(inode);
4828 +               for (bindex++; bindex <= bbot; bindex++) {
4829 +                       h_inode = au_h_iptr(inode, bindex);
4830 +                       if (h_inode)
4831 +                               au_add_nlink(inode, h_inode);
4832 +               }
4833 +       }
4834 +}
4835 +
4836 +void au_cpup_attr_changeable(struct inode *inode)
4837 +{
4838 +       struct inode *h_inode;
4839 +
4840 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4841 +       inode->i_mode = h_inode->i_mode;
4842 +       inode->i_uid = h_inode->i_uid;
4843 +       inode->i_gid = h_inode->i_gid;
4844 +       au_cpup_attr_timesizes(inode);
4845 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4846 +}
4847 +
4848 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4849 +{
4850 +       struct au_iinfo *iinfo = au_ii(inode);
4851 +
4852 +       IiMustWriteLock(inode);
4853 +
4854 +       iinfo->ii_higen = h_inode->i_generation;
4855 +       iinfo->ii_hsb1 = h_inode->i_sb;
4856 +}
4857 +
4858 +void au_cpup_attr_all(struct inode *inode, int force)
4859 +{
4860 +       struct inode *h_inode;
4861 +
4862 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4863 +       au_cpup_attr_changeable(inode);
4864 +       if (inode->i_nlink > 0)
4865 +               au_cpup_attr_nlink(inode, force);
4866 +       inode->i_rdev = h_inode->i_rdev;
4867 +       inode->i_blkbits = h_inode->i_blkbits;
4868 +       au_cpup_igen(inode, h_inode);
4869 +}
4870 +
4871 +/* ---------------------------------------------------------------------- */
4872 +
4873 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4874 +
4875 +/* keep the timestamps of the parent dir when cpup */
4876 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4877 +                   struct path *h_path)
4878 +{
4879 +       struct inode *h_inode;
4880 +
4881 +       dt->dt_dentry = dentry;
4882 +       dt->dt_h_path = *h_path;
4883 +       h_inode = d_inode(h_path->dentry);
4884 +       dt->dt_atime = inode_get_atime(h_inode);
4885 +       dt->dt_mtime = inode_get_mtime(h_inode);
4886 +       /* smp_mb(); */
4887 +}
4888 +
4889 +void au_dtime_revert(struct au_dtime *dt)
4890 +{
4891 +       struct iattr attr;
4892 +       int err;
4893 +
4894 +       attr.ia_atime = dt->dt_atime;
4895 +       attr.ia_mtime = dt->dt_mtime;
4896 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4897 +               | ATTR_ATIME | ATTR_ATIME_SET;
4898 +
4899 +       /* no delegation since this is a directory */
4900 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4901 +       if (unlikely(err))
4902 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4903 +}
4904 +
4905 +/* ---------------------------------------------------------------------- */
4906 +
4907 +/* internal use only */
4908 +struct au_cpup_reg_attr {
4909 +       int             valid;
4910 +       struct kstat    st;
4911 +       unsigned int    iflags; /* inode->i_flags */
4912 +};
4913 +
4914 +static noinline_for_stack
4915 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4916 +              struct au_cpup_reg_attr *h_src_attr)
4917 +{
4918 +       int err, sbits, icex;
4919 +       unsigned int mnt_flags;
4920 +       unsigned char verbose;
4921 +       struct iattr ia;
4922 +       struct path h_path;
4923 +       struct inode *h_isrc, *h_idst;
4924 +       struct kstat *h_st;
4925 +       struct au_branch *br;
4926 +
4927 +       br = au_sbr(dst->d_sb, bindex);
4928 +       h_path.mnt = au_br_mnt(br);
4929 +       h_path.dentry = au_h_dptr(dst, bindex);
4930 +       h_idst = d_inode(h_path.dentry);
4931 +       h_isrc = d_inode(h_src->dentry);
4932 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4933 +               | ATTR_ATIME | ATTR_MTIME
4934 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4935 +       if (h_src_attr && h_src_attr->valid) {
4936 +               h_st = &h_src_attr->st;
4937 +               ia.ia_uid = h_st->uid;
4938 +               ia.ia_gid = h_st->gid;
4939 +               ia.ia_atime = h_st->atime;
4940 +               ia.ia_mtime = h_st->mtime;
4941 +               if (h_idst->i_mode != h_st->mode
4942 +                   && !S_ISLNK(h_idst->i_mode)) {
4943 +                       ia.ia_valid |= ATTR_MODE;
4944 +                       ia.ia_mode = h_st->mode;
4945 +               }
4946 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4947 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4948 +       } else {
4949 +               ia.ia_uid = h_isrc->i_uid;
4950 +               ia.ia_gid = h_isrc->i_gid;
4951 +               ia.ia_atime = inode_get_atime(h_isrc);
4952 +               ia.ia_mtime = inode_get_mtime(h_isrc);
4953 +               if (h_idst->i_mode != h_isrc->i_mode
4954 +                   && !S_ISLNK(h_idst->i_mode)) {
4955 +                       ia.ia_valid |= ATTR_MODE;
4956 +                       ia.ia_mode = h_isrc->i_mode;
4957 +               }
4958 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4959 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4960 +       }
4961 +       /* no delegation since it is just created */
4962 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4963 +
4964 +       /* is this nfs only? */
4965 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4966 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4967 +               ia.ia_mode = h_isrc->i_mode;
4968 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4969 +       }
4970 +
4971 +       icex = br->br_perm & AuBrAttr_ICEX;
4972 +       if (!err) {
4973 +               mnt_flags = au_mntflags(dst->d_sb);
4974 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4975 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4976 +       }
4977 +
4978 +       return err;
4979 +}
4980 +
4981 +/* ---------------------------------------------------------------------- */
4982 +
4983 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4984 +                          char *buf, unsigned long blksize)
4985 +{
4986 +       int err;
4987 +       size_t sz, rbytes, wbytes;
4988 +       unsigned char all_zero;
4989 +       char *p, *zp;
4990 +       struct inode *h_inode;
4991 +       /* reduce stack usage */
4992 +       struct iattr *ia;
4993 +
4994 +       zp = page_address(ZERO_PAGE(0));
4995 +       if (unlikely(!zp))
4996 +               return -ENOMEM; /* possible? */
4997 +
4998 +       err = 0;
4999 +       all_zero = 0;
5000 +       while (len) {
5001 +               AuDbg("len %lld\n", len);
5002 +               sz = blksize;
5003 +               if (len < blksize)
5004 +                       sz = len;
5005 +
5006 +               rbytes = 0;
5007 +               /* todo: signal_pending? */
5008 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5009 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5010 +                       err = rbytes;
5011 +               }
5012 +               if (unlikely(err < 0))
5013 +                       break;
5014 +
5015 +               all_zero = 0;
5016 +               if (len >= rbytes && rbytes == blksize)
5017 +                       all_zero = !memcmp(buf, zp, rbytes);
5018 +               if (!all_zero) {
5019 +                       wbytes = rbytes;
5020 +                       p = buf;
5021 +                       while (wbytes) {
5022 +                               size_t b;
5023 +
5024 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5025 +                               err = b;
5026 +                               /* todo: signal_pending? */
5027 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5028 +                                       continue;
5029 +                               if (unlikely(err < 0))
5030 +                                       break;
5031 +                               wbytes -= b;
5032 +                               p += b;
5033 +                       }
5034 +                       if (unlikely(err < 0))
5035 +                               break;
5036 +               } else {
5037 +                       loff_t res;
5038 +
5039 +                       AuLabel(hole);
5040 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5041 +                       err = res;
5042 +                       if (unlikely(res < 0))
5043 +                               break;
5044 +               }
5045 +               len -= rbytes;
5046 +               err = 0;
5047 +       }
5048 +
5049 +       /* the last block may be a hole */
5050 +       if (!err && all_zero) {
5051 +               AuLabel(last hole);
5052 +
5053 +               err = 1;
5054 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5055 +                       /* nfs requires this step to make last hole */
5056 +                       /* is this only nfs? */
5057 +                       do {
5058 +                               /* todo: signal_pending? */
5059 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5060 +                       } while (err == -EAGAIN || err == -EINTR);
5061 +                       if (err == 1)
5062 +                               dst->f_pos--;
5063 +               }
5064 +
5065 +               if (err == 1) {
5066 +                       ia = (void *)buf;
5067 +                       ia->ia_size = dst->f_pos;
5068 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5069 +                       ia->ia_file = dst;
5070 +                       h_inode = file_inode(dst);
5071 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5072 +                       /* no delegation since it is just created */
5073 +                       err = vfsub_notify_change(&dst->f_path, ia,
5074 +                                                 /*delegated*/NULL);
5075 +                       inode_unlock(h_inode);
5076 +               }
5077 +       }
5078 +
5079 +       return err;
5080 +}
5081 +
5082 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5083 +{
5084 +       int err;
5085 +       unsigned long blksize;
5086 +       unsigned char do_kfree;
5087 +       char *buf;
5088 +       struct super_block *h_sb;
5089 +
5090 +       err = -ENOMEM;
5091 +       h_sb = file_inode(dst)->i_sb;
5092 +       blksize = h_sb->s_blocksize;
5093 +       if (!blksize || PAGE_SIZE < blksize)
5094 +               blksize = PAGE_SIZE;
5095 +       AuDbg("blksize %lu\n", blksize);
5096 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5097 +       if (do_kfree)
5098 +               buf = kmalloc(blksize, GFP_NOFS);
5099 +       else
5100 +               buf = (void *)__get_free_page(GFP_NOFS);
5101 +       if (unlikely(!buf))
5102 +               goto out;
5103 +
5104 +       if (len > (1 << 22))
5105 +               AuDbg("copying a large file %lld\n", (long long)len);
5106 +
5107 +       src->f_pos = 0;
5108 +       dst->f_pos = 0;
5109 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5110 +       if (do_kfree) {
5111 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5112 +               au_kfree_do_rcu(buf);
5113 +       } else
5114 +               free_page((unsigned long)buf);
5115 +
5116 +out:
5117 +       return err;
5118 +}
5119 +
5120 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5121 +{
5122 +       int err;
5123 +       struct super_block *h_src_sb;
5124 +       struct inode *h_src_inode;
5125 +
5126 +       h_src_inode = file_inode(src);
5127 +       h_src_sb = h_src_inode->i_sb;
5128 +
5129 +       /* XFS acquires inode_lock */
5130 +       if (!au_test_xfs(h_src_sb))
5131 +               err = au_copy_file(dst, src, len);
5132 +       else {
5133 +               inode_unlock_shared(h_src_inode);
5134 +               err = au_copy_file(dst, src, len);
5135 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5136 +       }
5137 +
5138 +       return err;
5139 +}
5140 +
5141 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5142 +{
5143 +       int err;
5144 +       loff_t lo;
5145 +       struct super_block *h_src_sb;
5146 +       struct inode *h_src_inode;
5147 +
5148 +       h_src_inode = file_inode(src);
5149 +       h_src_sb = h_src_inode->i_sb;
5150 +       if (h_src_sb != file_inode(dst)->i_sb
5151 +           || !dst->f_op->remap_file_range) {
5152 +               err = au_do_copy(dst, src, len);
5153 +               goto out;
5154 +       }
5155 +
5156 +       if (!au_test_nfs(h_src_sb)) {
5157 +               inode_unlock_shared(h_src_inode);
5158 +               lo = vfsub_clone_file_range(src, dst, len);
5159 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5160 +       } else
5161 +               lo = vfsub_clone_file_range(src, dst, len);
5162 +       if (lo == len) {
5163 +               err = 0;
5164 +               goto out; /* success */
5165 +       } else if (lo >= 0)
5166 +               /* todo: possible? */
5167 +               /* paritially succeeded */
5168 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5169 +       else if (lo != -EOPNOTSUPP) {
5170 +               /* older XFS has a condition in cloning */
5171 +               err = lo;
5172 +               goto out;
5173 +       }
5174 +
5175 +       /* the backend fs on NFS may not support cloning */
5176 +       err = au_do_copy(dst, src, len);
5177 +
5178 +out:
5179 +       AuTraceErr(err);
5180 +       return err;
5181 +}
5182 +
5183 +/*
5184 + * to support a sparse file which is opened with O_APPEND,
5185 + * we need to close the file.
5186 + */
5187 +static int au_cp_regular(struct au_cp_generic *cpg)
5188 +{
5189 +       int err, i;
5190 +       enum { SRC, DST };
5191 +       struct {
5192 +               aufs_bindex_t bindex;
5193 +               unsigned int flags;
5194 +               struct dentry *dentry;
5195 +               int force_wr;
5196 +               struct file *file;
5197 +       } *f, file[] = {
5198 +               {
5199 +                       .bindex = cpg->bsrc,
5200 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5201 +               },
5202 +               {
5203 +                       .bindex = cpg->bdst,
5204 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5205 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5206 +               }
5207 +       };
5208 +       struct au_branch *br;
5209 +       struct super_block *sb, *h_src_sb;
5210 +       struct inode *h_src_inode;
5211 +       struct task_struct *tsk = current;
5212 +
5213 +       /* bsrc branch can be ro/rw. */
5214 +       sb = cpg->dentry->d_sb;
5215 +       f = file;
5216 +       for (i = 0; i < 2; i++, f++) {
5217 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5218 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5219 +                                   /*file*/NULL, f->force_wr);
5220 +               if (IS_ERR(f->file)) {
5221 +                       err = PTR_ERR(f->file);
5222 +                       if (i == SRC)
5223 +                               goto out;
5224 +                       else
5225 +                               goto out_src;
5226 +               }
5227 +       }
5228 +
5229 +       /* try stopping to update while we copyup */
5230 +       h_src_inode = d_inode(file[SRC].dentry);
5231 +       h_src_sb = h_src_inode->i_sb;
5232 +       if (!au_test_nfs(h_src_sb))
5233 +               IMustLock(h_src_inode);
5234 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5235 +
5236 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5237 +       if (tsk->flags & PF_KTHREAD)
5238 +               __fput_sync(file[DST].file);
5239 +       else {
5240 +               /* it happened actually */
5241 +               fput(file[DST].file);
5242 +               /*
5243 +                * too bad.
5244 +                * we have to call both since we don't know which place the file
5245 +                * was added to.
5246 +                */
5247 +               task_work_run();
5248 +               flush_delayed_fput();
5249 +       }
5250 +       br = au_sbr(sb, file[DST].bindex);
5251 +       au_lcnt_dec(&br->br_nfiles);
5252 +
5253 +out_src:
5254 +       fput(file[SRC].file);
5255 +       br = au_sbr(sb, file[SRC].bindex);
5256 +       au_lcnt_dec(&br->br_nfiles);
5257 +out:
5258 +       return err;
5259 +}
5260 +
5261 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5262 +                             struct au_cpup_reg_attr *h_src_attr)
5263 +{
5264 +       int err, rerr;
5265 +       loff_t l;
5266 +       struct path h_path;
5267 +       struct inode *h_src_inode, *h_dst_inode;
5268 +
5269 +       err = 0;
5270 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5271 +       l = i_size_read(h_src_inode);
5272 +       if (cpg->len == -1 || l < cpg->len)
5273 +               cpg->len = l;
5274 +       if (cpg->len) {
5275 +               /* try stopping to update while we are referencing */
5276 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5277 +               au_pin_hdir_unlock(cpg->pin);
5278 +
5279 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5280 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5281 +               h_src_attr->iflags = h_src_inode->i_flags;
5282 +               if (!au_test_nfs(h_src_inode->i_sb))
5283 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5284 +               else {
5285 +                       inode_unlock_shared(h_src_inode);
5286 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5287 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5288 +               }
5289 +               if (unlikely(err)) {
5290 +                       inode_unlock_shared(h_src_inode);
5291 +                       goto out;
5292 +               }
5293 +               h_src_attr->valid = 1;
5294 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5295 +                       err = au_cp_regular(cpg);
5296 +                       inode_unlock_shared(h_src_inode);
5297 +               } else {
5298 +                       inode_unlock_shared(h_src_inode);
5299 +                       err = au_cp_regular(cpg);
5300 +               }
5301 +               rerr = au_pin_hdir_relock(cpg->pin);
5302 +               if (!err && rerr)
5303 +                       err = rerr;
5304 +       }
5305 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5306 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5307 +               h_dst_inode = d_inode(h_path.dentry);
5308 +               spin_lock(&h_dst_inode->i_lock);
5309 +               h_dst_inode->i_state |= I_LINKABLE;
5310 +               spin_unlock(&h_dst_inode->i_lock);
5311 +       }
5312 +
5313 +out:
5314 +       return err;
5315 +}
5316 +
5317 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5318 +                             struct inode *h_dir)
5319 +{
5320 +       int err;
5321 +       DEFINE_DELAYED_CALL(done);
5322 +       const char *sym;
5323 +
5324 +       sym = vfs_get_link(h_src, &done);
5325 +       err = PTR_ERR(sym);
5326 +       if (IS_ERR(sym))
5327 +               goto out;
5328 +
5329 +       err = vfsub_symlink(h_dir, h_path, sym);
5330 +
5331 +out:
5332 +       do_delayed_call(&done);
5333 +       return err;
5334 +}
5335 +
5336 +/*
5337 + * regardless 'acl' option, reset all ACL.
5338 + * All ACL will be copied up later from the original entry on the lower branch.
5339 + */
5340 +static int au_reset_acl(struct path *h_path, umode_t mode)
5341 +{
5342 +       int err;
5343 +       struct dentry *h_dentry;
5344 +       /* struct inode *h_inode; */
5345 +       struct mnt_idmap *h_idmap;
5346 +
5347 +       h_idmap = mnt_idmap(h_path->mnt);
5348 +       h_dentry = h_path->dentry;
5349 +       /* h_inode = d_inode(h_dentry); */
5350 +       /* forget_all_cached_acls(h_inode)); */
5351 +       err = vfsub_remove_acl(h_idmap, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5352 +       AuTraceErr(err);
5353 +       if (err == -EOPNOTSUPP)
5354 +               err = 0;
5355 +       if (!err)
5356 +               err = vfsub_acl_chmod(h_idmap, h_dentry, mode);
5357 +
5358 +       AuTraceErr(err);
5359 +       return err;
5360 +}
5361 +
5362 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5363 +                         struct inode *h_dir, struct path *h_path)
5364 +{
5365 +       int err;
5366 +       struct inode *dir, *inode;
5367 +       struct mnt_idmap *h_idmap;
5368 +
5369 +       h_idmap = mnt_idmap(h_path->mnt);
5370 +       err = vfsub_remove_acl(h_idmap, h_path->dentry,
5371 +                              XATTR_NAME_POSIX_ACL_DEFAULT);
5372 +       AuTraceErr(err);
5373 +       if (err == -EOPNOTSUPP)
5374 +               err = 0;
5375 +       if (unlikely(err))
5376 +               goto out;
5377 +
5378 +       /*
5379 +        * strange behaviour from the users view,
5380 +        * particularly setattr case
5381 +        */
5382 +       dir = d_inode(dst_parent);
5383 +       if (au_ibtop(dir) == cpg->bdst)
5384 +               au_cpup_attr_nlink(dir, /*force*/1);
5385 +       inode = d_inode(cpg->dentry);
5386 +       au_cpup_attr_nlink(inode, /*force*/1);
5387 +
5388 +out:
5389 +       return err;
5390 +}
5391 +
5392 +static noinline_for_stack
5393 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5394 +              struct au_cpup_reg_attr *h_src_attr)
5395 +{
5396 +       int err;
5397 +       umode_t mode;
5398 +       unsigned int mnt_flags;
5399 +       unsigned char isdir, isreg, force;
5400 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5401 +       struct au_dtime dt;
5402 +       struct path h_path;
5403 +       struct dentry *h_src, *h_dst, *h_parent;
5404 +       struct inode *h_inode, *h_dir;
5405 +       struct super_block *sb;
5406 +
5407 +       /* bsrc branch can be ro/rw. */
5408 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5409 +       h_inode = d_inode(h_src);
5410 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5411 +
5412 +       /* try stopping to be referenced while we are creating */
5413 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5414 +       if (au_ftest_cpup(cpg->flags, RENAME))
5415 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5416 +                                 AUFS_WH_PFX_LEN));
5417 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5418 +       h_dir = d_inode(h_parent);
5419 +       IMustLock(h_dir);
5420 +       AuDebugOn(h_parent != h_dst->d_parent);
5421 +
5422 +       sb = cpg->dentry->d_sb;
5423 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5424 +       if (do_dt) {
5425 +               h_path.dentry = h_parent;
5426 +               au_dtime_store(&dt, dst_parent, &h_path);
5427 +       }
5428 +       h_path.dentry = h_dst;
5429 +
5430 +       isreg = 0;
5431 +       isdir = 0;
5432 +       mode = h_inode->i_mode;
5433 +       switch (mode & S_IFMT) {
5434 +       case S_IFREG:
5435 +               isreg = 1;
5436 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5437 +               if (!err)
5438 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5439 +               break;
5440 +       case S_IFDIR:
5441 +               isdir = 1;
5442 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5443 +               if (!err)
5444 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5445 +               break;
5446 +       case S_IFLNK:
5447 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5448 +               break;
5449 +       case S_IFCHR:
5450 +       case S_IFBLK:
5451 +               AuDebugOn(!capable(CAP_MKNOD));
5452 +               fallthrough;
5453 +       case S_IFIFO:
5454 +       case S_IFSOCK:
5455 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5456 +               break;
5457 +       default:
5458 +               AuIOErr("Unknown inode type 0%o\n", mode);
5459 +               err = -EIO;
5460 +       }
5461 +       if (!err)
5462 +               err = au_reset_acl(&h_path, mode);
5463 +
5464 +       mnt_flags = au_mntflags(sb);
5465 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5466 +           && !isdir
5467 +           && au_opt_test(mnt_flags, XINO)
5468 +           && (h_inode->i_nlink == 1
5469 +               || (h_inode->i_state & I_LINKABLE))
5470 +           /* todo: unnecessary? */
5471 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5472 +           && cpg->bdst < cpg->bsrc
5473 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5474 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5475 +               /* ignore this error */
5476 +
5477 +       if (!err) {
5478 +               force = 0;
5479 +               if (isreg) {
5480 +                       force = !!cpg->len;
5481 +                       if (cpg->len == -1)
5482 +                               force = !!i_size_read(h_inode);
5483 +               }
5484 +               au_fhsm_wrote(sb, cpg->bdst, force);
5485 +       }
5486 +
5487 +       if (do_dt)
5488 +               au_dtime_revert(&dt);
5489 +       return err;
5490 +}
5491 +
5492 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5493 +{
5494 +       int err;
5495 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5496 +       struct path h_ppath;
5497 +       struct inode *h_dir;
5498 +       aufs_bindex_t bdst;
5499 +
5500 +       dentry = cpg->dentry;
5501 +       bdst = cpg->bdst;
5502 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5503 +       h_dentry = au_h_dptr(dentry, bdst);
5504 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5505 +               dget(h_dentry);
5506 +               au_set_h_dptr(dentry, bdst, NULL);
5507 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5508 +               if (!err)
5509 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5510 +               au_set_h_dptr(dentry, bdst, h_dentry);
5511 +       } else {
5512 +               err = 0;
5513 +               parent = dget_parent(dentry);
5514 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5515 +               dput(parent);
5516 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5517 +               if (IS_ERR(h_path->dentry))
5518 +                       err = PTR_ERR(h_path->dentry);
5519 +       }
5520 +       if (unlikely(err))
5521 +               goto out;
5522 +
5523 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5524 +       h_dir = d_inode(h_parent);
5525 +       IMustLock(h_dir);
5526 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5527 +       /* no delegation since it is just created */
5528 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5529 +                          /*flags*/0);
5530 +       dput(h_path->dentry);
5531 +
5532 +out:
5533 +       return err;
5534 +}
5535 +
5536 +/*
5537 + * copyup the @dentry from @bsrc to @bdst.
5538 + * the caller must set the both of lower dentries.
5539 + * @len is for truncating when it is -1 copyup the entire file.
5540 + * in link/rename cases, @dst_parent may be different from the real one.
5541 + * basic->bsrc can be larger than basic->bdst.
5542 + * aufs doesn't touch the credential so
5543 + * security_inode_copy_up{,_xattr}() are unnecessary.
5544 + */
5545 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5546 +{
5547 +       int err, rerr;
5548 +       aufs_bindex_t old_ibtop;
5549 +       unsigned char isdir, plink;
5550 +       struct dentry *h_src, *h_dst, *h_parent;
5551 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5552 +       struct super_block *sb;
5553 +       struct au_branch *br;
5554 +       struct path h_src_path;
5555 +       /* to reduce stack size */
5556 +       struct {
5557 +               struct au_dtime dt;
5558 +               struct path h_path;
5559 +               struct au_cpup_reg_attr h_src_attr;
5560 +       } *a;
5561 +
5562 +       err = -ENOMEM;
5563 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5564 +       if (unlikely(!a))
5565 +               goto out;
5566 +       a->h_src_attr.valid = 0;
5567 +
5568 +       sb = cpg->dentry->d_sb;
5569 +       br = au_sbr(sb, cpg->bdst);
5570 +       a->h_path.mnt = au_br_mnt(br);
5571 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5572 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5573 +       h_dir = d_inode(h_parent);
5574 +       IMustLock(h_dir);
5575 +
5576 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5577 +       inode = d_inode(cpg->dentry);
5578 +
5579 +       if (!dst_parent)
5580 +               dst_parent = dget_parent(cpg->dentry);
5581 +       else
5582 +               dget(dst_parent);
5583 +
5584 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5585 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5586 +       if (dst_inode) {
5587 +               if (unlikely(!plink)) {
5588 +                       err = -EIO;
5589 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5590 +                               "but plink is disabled\n",
5591 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5592 +                       goto out_parent;
5593 +               }
5594 +
5595 +               if (dst_inode->i_nlink) {
5596 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5597 +
5598 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5599 +                       err = PTR_ERR(h_src);
5600 +                       if (IS_ERR(h_src))
5601 +                               goto out_parent;
5602 +                       if (unlikely(d_is_negative(h_src))) {
5603 +                               err = -EIO;
5604 +                               AuIOErr("i%lu exists on b%d "
5605 +                                       "but not pseudo-linked\n",
5606 +                                       inode->i_ino, cpg->bdst);
5607 +                               dput(h_src);
5608 +                               goto out_parent;
5609 +                       }
5610 +
5611 +                       if (do_dt) {
5612 +                               a->h_path.dentry = h_parent;
5613 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5614 +                       }
5615 +
5616 +                       a->h_path.dentry = h_dst;
5617 +                       delegated = NULL;
5618 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5619 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5620 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5621 +                       if (do_dt)
5622 +                               au_dtime_revert(&a->dt);
5623 +                       if (unlikely(err == -EWOULDBLOCK)) {
5624 +                               pr_warn("cannot retry for NFSv4 delegation"
5625 +                                       " for an internal link\n");
5626 +                               iput(delegated);
5627 +                       }
5628 +                       dput(h_src);
5629 +                       goto out_parent;
5630 +               } else
5631 +                       /* todo: cpup_wh_file? */
5632 +                       /* udba work */
5633 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5634 +       }
5635 +
5636 +       isdir = S_ISDIR(inode->i_mode);
5637 +       old_ibtop = au_ibtop(inode);
5638 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5639 +       if (unlikely(err))
5640 +               goto out_rev;
5641 +       dst_inode = d_inode(h_dst);
5642 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5643 +       /* todo: necessary? */
5644 +       /* au_pin_hdir_unlock(cpg->pin); */
5645 +
5646 +       h_src_path.dentry = h_src;
5647 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5648 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5649 +       if (unlikely(err)) {
5650 +               /* todo: necessary? */
5651 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5652 +               inode_unlock(dst_inode);
5653 +               goto out_rev;
5654 +       }
5655 +
5656 +       if (cpg->bdst < old_ibtop) {
5657 +               if (S_ISREG(inode->i_mode)) {
5658 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5659 +                       if (unlikely(err)) {
5660 +                               /* ignore an error */
5661 +                               /* au_pin_hdir_relock(cpg->pin); */
5662 +                               inode_unlock(dst_inode);
5663 +                               goto out_rev;
5664 +                       }
5665 +               }
5666 +               au_set_ibtop(inode, cpg->bdst);
5667 +       } else
5668 +               au_set_ibbot(inode, cpg->bdst);
5669 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5670 +                     au_hi_flags(inode, isdir));
5671 +
5672 +       /* todo: necessary? */
5673 +       /* err = au_pin_hdir_relock(cpg->pin); */
5674 +       inode_unlock(dst_inode);
5675 +       if (unlikely(err))
5676 +               goto out_rev;
5677 +
5678 +       src_inode = d_inode(h_src);
5679 +       if (!isdir
5680 +           && (src_inode->i_nlink > 1
5681 +               || src_inode->i_state & I_LINKABLE)
5682 +           && plink)
5683 +               au_plink_append(inode, cpg->bdst, h_dst);
5684 +
5685 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5686 +               a->h_path.dentry = h_dst;
5687 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5688 +       }
5689 +       if (!err)
5690 +               goto out_parent; /* success */
5691 +
5692 +       /* revert */
5693 +out_rev:
5694 +       a->h_path.dentry = h_parent;
5695 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5696 +       a->h_path.dentry = h_dst;
5697 +       rerr = 0;
5698 +       if (d_is_positive(h_dst)) {
5699 +               if (!isdir) {
5700 +                       /* no delegation since it is just created */
5701 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5702 +                                           /*delegated*/NULL, /*force*/0);
5703 +               } else
5704 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5705 +       }
5706 +       au_dtime_revert(&a->dt);
5707 +       if (rerr) {
5708 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5709 +               err = -EIO;
5710 +       }
5711 +out_parent:
5712 +       dput(dst_parent);
5713 +       au_kfree_rcu(a);
5714 +out:
5715 +       return err;
5716 +}
5717 +
5718 +#if 0 /* reserved */
5719 +struct au_cpup_single_args {
5720 +       int *errp;
5721 +       struct au_cp_generic *cpg;
5722 +       struct dentry *dst_parent;
5723 +};
5724 +
5725 +static void au_call_cpup_single(void *args)
5726 +{
5727 +       struct au_cpup_single_args *a = args;
5728 +
5729 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5730 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5731 +       au_pin_hdir_release(a->cpg->pin);
5732 +}
5733 +#endif
5734 +
5735 +/*
5736 + * prevent SIGXFSZ in copy-up.
5737 + * testing CAP_MKNOD is for generic fs,
5738 + * but CAP_FSETID is for xfs only, currently.
5739 + */
5740 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5741 +{
5742 +       int do_sio;
5743 +       struct super_block *sb;
5744 +       struct inode *h_dir;
5745 +
5746 +       do_sio = 0;
5747 +       sb = au_pinned_parent(pin)->d_sb;
5748 +       if (!au_wkq_test()
5749 +           && (!au_sbi(sb)->si_plink_maint_pid
5750 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5751 +               switch (mode & S_IFMT) {
5752 +               case S_IFREG:
5753 +                       /* no condition about RLIMIT_FSIZE and the file size */
5754 +                       do_sio = 1;
5755 +                       break;
5756 +               case S_IFCHR:
5757 +               case S_IFBLK:
5758 +                       do_sio = !capable(CAP_MKNOD);
5759 +                       break;
5760 +               }
5761 +               if (!do_sio)
5762 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5763 +                                 && !capable(CAP_FSETID));
5764 +               /* this workaround may be removed in the future */
5765 +               if (!do_sio) {
5766 +                       h_dir = au_pinned_h_dir(pin);
5767 +                       do_sio = h_dir->i_mode & S_ISVTX;
5768 +               }
5769 +       }
5770 +
5771 +       return do_sio;
5772 +}
5773 +
5774 +#if 0 /* reserved */
5775 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5776 +{
5777 +       int err, wkq_err;
5778 +       struct dentry *h_dentry;
5779 +
5780 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5781 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5782 +               err = au_cpup_single(cpg, dst_parent);
5783 +       else {
5784 +               struct au_cpup_single_args args = {
5785 +                       .errp           = &err,
5786 +                       .cpg            = cpg,
5787 +                       .dst_parent     = dst_parent
5788 +               };
5789 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5790 +               if (unlikely(wkq_err))
5791 +                       err = wkq_err;
5792 +       }
5793 +
5794 +       return err;
5795 +}
5796 +#endif
5797 +
5798 +/*
5799 + * copyup the @dentry from the first active lower branch to @bdst,
5800 + * using au_cpup_single().
5801 + */
5802 +static int au_cpup_simple(struct au_cp_generic *cpg)
5803 +{
5804 +       int err;
5805 +       unsigned int flags_orig;
5806 +       struct dentry *dentry;
5807 +
5808 +       AuDebugOn(cpg->bsrc < 0);
5809 +
5810 +       dentry = cpg->dentry;
5811 +       DiMustWriteLock(dentry);
5812 +
5813 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5814 +       if (!err) {
5815 +               flags_orig = cpg->flags;
5816 +               au_fset_cpup(cpg->flags, RENAME);
5817 +               err = au_cpup_single(cpg, NULL);
5818 +               cpg->flags = flags_orig;
5819 +               if (!err)
5820 +                       return 0; /* success */
5821 +
5822 +               /* revert */
5823 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5824 +               au_set_dbtop(dentry, cpg->bsrc);
5825 +       }
5826 +
5827 +       return err;
5828 +}
5829 +
5830 +struct au_cpup_simple_args {
5831 +       int *errp;
5832 +       struct au_cp_generic *cpg;
5833 +};
5834 +
5835 +static void au_call_cpup_simple(void *args)
5836 +{
5837 +       struct au_cpup_simple_args *a = args;
5838 +
5839 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5840 +       *a->errp = au_cpup_simple(a->cpg);
5841 +       au_pin_hdir_release(a->cpg->pin);
5842 +}
5843 +
5844 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5845 +{
5846 +       int err, wkq_err;
5847 +       struct dentry *dentry, *parent;
5848 +       struct file *h_file;
5849 +       struct inode *h_dir;
5850 +       struct mnt_idmap *h_idmap;
5851 +
5852 +       dentry = cpg->dentry;
5853 +       h_file = NULL;
5854 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5855 +               AuDebugOn(cpg->bsrc < 0);
5856 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5857 +               err = PTR_ERR(h_file);
5858 +               if (IS_ERR(h_file))
5859 +                       goto out;
5860 +       }
5861 +
5862 +       parent = dget_parent(dentry);
5863 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5864 +       h_idmap = au_sbr_idmap(dentry->d_sb, cpg->bdst);
5865 +       if (!au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC | MAY_WRITE)
5866 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5867 +               err = au_cpup_simple(cpg);
5868 +       else {
5869 +               struct au_cpup_simple_args args = {
5870 +                       .errp           = &err,
5871 +                       .cpg            = cpg
5872 +               };
5873 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5874 +               if (unlikely(wkq_err))
5875 +                       err = wkq_err;
5876 +       }
5877 +
5878 +       dput(parent);
5879 +       if (h_file)
5880 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5881 +
5882 +out:
5883 +       return err;
5884 +}
5885 +
5886 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5887 +{
5888 +       aufs_bindex_t bsrc, bbot;
5889 +       struct dentry *dentry, *h_dentry;
5890 +
5891 +       if (cpg->bsrc < 0) {
5892 +               dentry = cpg->dentry;
5893 +               bbot = au_dbbot(dentry);
5894 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5895 +                       h_dentry = au_h_dptr(dentry, bsrc);
5896 +                       if (h_dentry) {
5897 +                               AuDebugOn(d_is_negative(h_dentry));
5898 +                               break;
5899 +                       }
5900 +               }
5901 +               AuDebugOn(bsrc > bbot);
5902 +               cpg->bsrc = bsrc;
5903 +       }
5904 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5905 +       return au_do_sio_cpup_simple(cpg);
5906 +}
5907 +
5908 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5909 +{
5910 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5911 +       return au_do_sio_cpup_simple(cpg);
5912 +}
5913 +
5914 +/* ---------------------------------------------------------------------- */
5915 +
5916 +/*
5917 + * copyup the deleted file for writing.
5918 + */
5919 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5920 +                        struct file *file)
5921 +{
5922 +       int err;
5923 +       unsigned int flags_orig;
5924 +       aufs_bindex_t bsrc_orig;
5925 +       struct au_dinfo *dinfo;
5926 +       struct {
5927 +               struct au_hdentry *hd;
5928 +               struct dentry *h_dentry;
5929 +       } hdst, hsrc;
5930 +
5931 +       dinfo = au_di(cpg->dentry);
5932 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5933 +
5934 +       bsrc_orig = cpg->bsrc;
5935 +       cpg->bsrc = dinfo->di_btop;
5936 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5937 +       hdst.h_dentry = hdst.hd->hd_dentry;
5938 +       hdst.hd->hd_dentry = wh_dentry;
5939 +       dinfo->di_btop = cpg->bdst;
5940 +
5941 +       hsrc.h_dentry = NULL;
5942 +       if (file) {
5943 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5944 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5945 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5946 +       }
5947 +       flags_orig = cpg->flags;
5948 +       cpg->flags = !AuCpup_DTIME;
5949 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5950 +       cpg->flags = flags_orig;
5951 +       if (file) {
5952 +               if (!err)
5953 +                       err = au_reopen_nondir(file);
5954 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5955 +       }
5956 +       hdst.hd->hd_dentry = hdst.h_dentry;
5957 +       dinfo->di_btop = cpg->bsrc;
5958 +       cpg->bsrc = bsrc_orig;
5959 +
5960 +       return err;
5961 +}
5962 +
5963 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5964 +{
5965 +       int err;
5966 +       aufs_bindex_t bdst;
5967 +       struct au_dtime dt;
5968 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5969 +       struct au_branch *br;
5970 +       struct path h_path;
5971 +
5972 +       dentry = cpg->dentry;
5973 +       bdst = cpg->bdst;
5974 +       br = au_sbr(dentry->d_sb, bdst);
5975 +       parent = dget_parent(dentry);
5976 +       h_parent = au_h_dptr(parent, bdst);
5977 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5978 +       err = PTR_ERR(wh_dentry);
5979 +       if (IS_ERR(wh_dentry))
5980 +               goto out;
5981 +
5982 +       h_path.dentry = h_parent;
5983 +       h_path.mnt = au_br_mnt(br);
5984 +       au_dtime_store(&dt, parent, &h_path);
5985 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5986 +       if (unlikely(err))
5987 +               goto out_wh;
5988 +
5989 +       dget(wh_dentry);
5990 +       h_path.dentry = wh_dentry;
5991 +       if (!d_is_dir(wh_dentry)) {
5992 +               /* no delegation since it is just created */
5993 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5994 +                                  /*delegated*/NULL, /*force*/0);
5995 +       } else
5996 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5997 +       if (unlikely(err)) {
5998 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5999 +                       wh_dentry, err);
6000 +               err = -EIO;
6001 +       }
6002 +       au_dtime_revert(&dt);
6003 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6004 +
6005 +out_wh:
6006 +       dput(wh_dentry);
6007 +out:
6008 +       dput(parent);
6009 +       return err;
6010 +}
6011 +
6012 +struct au_cpup_wh_args {
6013 +       int *errp;
6014 +       struct au_cp_generic *cpg;
6015 +       struct file *file;
6016 +};
6017 +
6018 +static void au_call_cpup_wh(void *args)
6019 +{
6020 +       struct au_cpup_wh_args *a = args;
6021 +
6022 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6023 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6024 +       au_pin_hdir_release(a->cpg->pin);
6025 +}
6026 +
6027 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6028 +{
6029 +       int err, wkq_err;
6030 +       aufs_bindex_t bdst;
6031 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6032 +       struct inode *dir, *h_dir, *h_tmpdir;
6033 +       struct au_wbr *wbr;
6034 +       struct au_pin wh_pin, *pin_orig;
6035 +       struct mnt_idmap *h_idmap;
6036 +
6037 +       dentry = cpg->dentry;
6038 +       bdst = cpg->bdst;
6039 +       parent = dget_parent(dentry);
6040 +       dir = d_inode(parent);
6041 +       h_orph = NULL;
6042 +       h_parent = NULL;
6043 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6044 +       h_tmpdir = h_dir;
6045 +       pin_orig = NULL;
6046 +       if (!h_dir->i_nlink) {
6047 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6048 +               h_orph = wbr->wbr_orph;
6049 +
6050 +               h_parent = dget(au_h_dptr(parent, bdst));
6051 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6052 +               h_tmpdir = d_inode(h_orph);
6053 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6054 +
6055 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6056 +               /* todo: au_h_open_pre()? */
6057 +
6058 +               pin_orig = cpg->pin;
6059 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6060 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6061 +               cpg->pin = &wh_pin;
6062 +       }
6063 +
6064 +       h_idmap = au_sbr_idmap(dentry->d_sb, bdst);
6065 +       if (!au_test_h_perm_sio(h_idmap, h_tmpdir, MAY_EXEC | MAY_WRITE)
6066 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6067 +               err = au_cpup_wh(cpg, file);
6068 +       else {
6069 +               struct au_cpup_wh_args args = {
6070 +                       .errp   = &err,
6071 +                       .cpg    = cpg,
6072 +                       .file   = file
6073 +               };
6074 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6075 +               if (unlikely(wkq_err))
6076 +                       err = wkq_err;
6077 +       }
6078 +
6079 +       if (h_orph) {
6080 +               inode_unlock(h_tmpdir);
6081 +               /* todo: au_h_open_post()? */
6082 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6083 +               au_set_h_dptr(parent, bdst, h_parent);
6084 +               AuDebugOn(!pin_orig);
6085 +               cpg->pin = pin_orig;
6086 +       }
6087 +       iput(h_dir);
6088 +       dput(parent);
6089 +
6090 +       return err;
6091 +}
6092 +
6093 +/* ---------------------------------------------------------------------- */
6094 +
6095 +/*
6096 + * generic routine for both of copy-up and copy-down.
6097 + */
6098 +/* cf. revalidate function in file.c */
6099 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6100 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6101 +                        struct au_pin *pin,
6102 +                        struct dentry *h_parent, void *arg),
6103 +              void *arg)
6104 +{
6105 +       int err;
6106 +       struct au_pin pin;
6107 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6108 +
6109 +       err = 0;
6110 +       parent = dget_parent(dentry);
6111 +       if (IS_ROOT(parent))
6112 +               goto out;
6113 +
6114 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6115 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6116 +
6117 +       /* do not use au_dpage */
6118 +       real_parent = parent;
6119 +       while (1) {
6120 +               dput(parent);
6121 +               parent = dget_parent(dentry);
6122 +               h_parent = au_h_dptr(parent, bdst);
6123 +               if (h_parent)
6124 +                       goto out; /* success */
6125 +
6126 +               /* find top dir which is necessary to cpup */
6127 +               do {
6128 +                       d = parent;
6129 +                       dput(parent);
6130 +                       parent = dget_parent(d);
6131 +                       di_read_lock_parent3(parent, !AuLock_IR);
6132 +                       h_parent = au_h_dptr(parent, bdst);
6133 +                       di_read_unlock(parent, !AuLock_IR);
6134 +               } while (!h_parent);
6135 +
6136 +               if (d != real_parent)
6137 +                       di_write_lock_child3(d);
6138 +
6139 +               /* somebody else might create while we were sleeping */
6140 +               h_dentry = au_h_dptr(d, bdst);
6141 +               if (!h_dentry || d_is_negative(h_dentry)) {
6142 +                       if (h_dentry)
6143 +                               au_update_dbtop(d);
6144 +
6145 +                       au_pin_set_dentry(&pin, d);
6146 +                       err = au_do_pin(&pin);
6147 +                       if (!err) {
6148 +                               err = cp(d, bdst, &pin, h_parent, arg);
6149 +                               au_unpin(&pin);
6150 +                       }
6151 +               }
6152 +
6153 +               if (d != real_parent)
6154 +                       di_write_unlock(d);
6155 +               if (unlikely(err))
6156 +                       break;
6157 +       }
6158 +
6159 +out:
6160 +       dput(parent);
6161 +       return err;
6162 +}
6163 +
6164 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6165 +                      struct au_pin *pin,
6166 +                      struct dentry *h_parent __maybe_unused,
6167 +                      void *arg __maybe_unused)
6168 +{
6169 +       struct au_cp_generic cpg = {
6170 +               .dentry = dentry,
6171 +               .bdst   = bdst,
6172 +               .bsrc   = -1,
6173 +               .len    = 0,
6174 +               .pin    = pin,
6175 +               .flags  = AuCpup_DTIME
6176 +       };
6177 +       return au_sio_cpup_simple(&cpg);
6178 +}
6179 +
6180 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6181 +{
6182 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6183 +}
6184 +
6185 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6186 +{
6187 +       int err;
6188 +       struct dentry *parent;
6189 +       struct inode *dir;
6190 +
6191 +       parent = dget_parent(dentry);
6192 +       dir = d_inode(parent);
6193 +       err = 0;
6194 +       if (au_h_iptr(dir, bdst))
6195 +               goto out;
6196 +
6197 +       di_read_unlock(parent, AuLock_IR);
6198 +       di_write_lock_parent(parent);
6199 +       /* someone else might change our inode while we were sleeping */
6200 +       if (!au_h_iptr(dir, bdst))
6201 +               err = au_cpup_dirs(dentry, bdst);
6202 +       di_downgrade_lock(parent, AuLock_IR);
6203 +
6204 +out:
6205 +       dput(parent);
6206 +       return err;
6207 +}
6208 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6209 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6210 +++ linux/fs/aufs/cpup.h        2024-03-10 23:40:47.083143450 +0100
6211 @@ -0,0 +1,100 @@
6212 +/* SPDX-License-Identifier: GPL-2.0 */
6213 +/*
6214 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6215 + *
6216 + * This program is free software; you can redistribute it and/or modify
6217 + * it under the terms of the GNU General Public License as published by
6218 + * the Free Software Foundation; either version 2 of the License, or
6219 + * (at your option) any later version.
6220 + *
6221 + * This program is distributed in the hope that it will be useful,
6222 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6223 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6224 + * GNU General Public License for more details.
6225 + *
6226 + * You should have received a copy of the GNU General Public License
6227 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6228 + */
6229 +
6230 +/*
6231 + * copy-up/down functions
6232 + */
6233 +
6234 +#ifndef __AUFS_CPUP_H__
6235 +#define __AUFS_CPUP_H__
6236 +
6237 +#ifdef __KERNEL__
6238 +
6239 +#include <linux/path.h>
6240 +
6241 +struct inode;
6242 +struct file;
6243 +struct au_pin;
6244 +
6245 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6246 +void au_cpup_attr_timesizes(struct inode *inode);
6247 +void au_cpup_attr_nlink(struct inode *inode, int force);
6248 +void au_cpup_attr_changeable(struct inode *inode);
6249 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6250 +void au_cpup_attr_all(struct inode *inode, int force);
6251 +
6252 +/* ---------------------------------------------------------------------- */
6253 +
6254 +struct au_cp_generic {
6255 +       struct dentry   *dentry;
6256 +       aufs_bindex_t   bdst, bsrc;
6257 +       loff_t          len;
6258 +       struct au_pin   *pin;
6259 +       unsigned int    flags;
6260 +};
6261 +
6262 +/* cpup flags */
6263 +#define AuCpup_DTIME           BIT(0)          /* do dtime_store/revert */
6264 +#define AuCpup_KEEPLINO                BIT(1)          /* do not clear the lower xino,
6265 +                                                  for link(2) */
6266 +#define AuCpup_RENAME          BIT(2)          /* rename after cpup */
6267 +#define AuCpup_HOPEN           BIT(3)          /* call h_open_pre/post() in
6268 +                                                  cpup */
6269 +#define AuCpup_OVERWRITE       BIT(4)          /* allow overwriting the
6270 +                                                  existing entry */
6271 +#define AuCpup_RWDST           BIT(5)          /* force write target even if
6272 +                                                  the branch is marked as RO */
6273 +
6274 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6275 +#undef AuCpup_HOPEN
6276 +#define AuCpup_HOPEN           0
6277 +#endif
6278 +
6279 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6280 +#define au_fset_cpup(flags, name) \
6281 +       do { (flags) |= AuCpup_##name; } while (0)
6282 +#define au_fclr_cpup(flags, name) \
6283 +       do { (flags) &= ~AuCpup_##name; } while (0)
6284 +
6285 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6286 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6287 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6288 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6289 +
6290 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6291 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6292 +                        struct au_pin *pin,
6293 +                        struct dentry *h_parent, void *arg),
6294 +              void *arg);
6295 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6296 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6297 +
6298 +/* ---------------------------------------------------------------------- */
6299 +
6300 +/* keep timestamps when copyup */
6301 +struct au_dtime {
6302 +       struct dentry *dt_dentry;
6303 +       struct path dt_h_path;
6304 +       struct timespec64 dt_atime, dt_mtime;
6305 +};
6306 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6307 +                   struct path *h_path);
6308 +void au_dtime_revert(struct au_dtime *dt);
6309 +
6310 +#endif /* __KERNEL__ */
6311 +#endif /* __AUFS_CPUP_H__ */
6312 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6313 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6314 +++ linux/fs/aufs/dbgaufs.c     2023-10-10 22:51:18.033248030 +0200
6315 @@ -0,0 +1,526 @@
6316 +// SPDX-License-Identifier: GPL-2.0
6317 +/*
6318 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6319 + *
6320 + * This program is free software; you can redistribute it and/or modify
6321 + * it under the terms of the GNU General Public License as published by
6322 + * the Free Software Foundation; either version 2 of the License, or
6323 + * (at your option) any later version.
6324 + *
6325 + * This program is distributed in the hope that it will be useful,
6326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6328 + * GNU General Public License for more details.
6329 + *
6330 + * You should have received a copy of the GNU General Public License
6331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6332 + */
6333 +
6334 +/*
6335 + * debugfs interface
6336 + */
6337 +
6338 +#include <linux/debugfs.h>
6339 +#include "aufs.h"
6340 +
6341 +#ifndef CONFIG_SYSFS
6342 +#error DEBUG_FS depends upon SYSFS
6343 +#endif
6344 +
6345 +static struct dentry *dbgaufs;
6346 +static const mode_t dbgaufs_mode = 0444;
6347 +
6348 +/* 20 is max digits length of ulong 64 */
6349 +struct dbgaufs_arg {
6350 +       int n;
6351 +       char a[20 * 4];
6352 +};
6353 +
6354 +/*
6355 + * common function for all XINO files
6356 + */
6357 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6358 +                             struct file *file)
6359 +{
6360 +       void *p;
6361 +
6362 +       p = file->private_data;
6363 +       if (p) {
6364 +               /* this is struct dbgaufs_arg */
6365 +               AuDebugOn(!au_kfree_sz_test(p));
6366 +               au_kfree_do_rcu(p);
6367 +       }
6368 +       return 0;
6369 +}
6370 +
6371 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6372 +                          int cnt)
6373 +{
6374 +       int err;
6375 +       struct kstat st;
6376 +       struct dbgaufs_arg *p;
6377 +
6378 +       err = -ENOMEM;
6379 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6380 +       if (unlikely(!p))
6381 +               goto out;
6382 +
6383 +       err = 0;
6384 +       p->n = 0;
6385 +       file->private_data = p;
6386 +       if (!xf)
6387 +               goto out;
6388 +
6389 +       err = vfsub_getattr(&xf->f_path, &st);
6390 +       if (!err) {
6391 +               if (do_fcnt)
6392 +                       p->n = snprintf
6393 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6394 +                                cnt, st.blocks, st.blksize,
6395 +                                (long long)st.size);
6396 +               else
6397 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6398 +                                       st.blocks, st.blksize,
6399 +                                       (long long)st.size);
6400 +               AuDebugOn(p->n >= sizeof(p->a));
6401 +       } else {
6402 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6403 +               err = 0;
6404 +       }
6405 +
6406 +out:
6407 +       return err;
6408 +}
6409 +
6410 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6411 +                              size_t count, loff_t *ppos)
6412 +{
6413 +       struct dbgaufs_arg *p;
6414 +
6415 +       p = file->private_data;
6416 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6417 +}
6418 +
6419 +/* ---------------------------------------------------------------------- */
6420 +
6421 +struct dbgaufs_plink_arg {
6422 +       int n;
6423 +       char a[];
6424 +};
6425 +
6426 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6427 +                                struct file *file)
6428 +{
6429 +       free_page((unsigned long)file->private_data);
6430 +       return 0;
6431 +}
6432 +
6433 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6434 +{
6435 +       int err, i, limit;
6436 +       unsigned long n, sum;
6437 +       struct dbgaufs_plink_arg *p;
6438 +       struct au_sbinfo *sbinfo;
6439 +       struct super_block *sb;
6440 +       struct hlist_bl_head *hbl;
6441 +
6442 +       err = -ENOMEM;
6443 +       p = (void *)get_zeroed_page(GFP_NOFS);
6444 +       if (unlikely(!p))
6445 +               goto out;
6446 +
6447 +       err = -EFBIG;
6448 +       sbinfo = inode->i_private;
6449 +       sb = sbinfo->si_sb;
6450 +       si_noflush_read_lock(sb);
6451 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6452 +               limit = PAGE_SIZE - sizeof(p->n);
6453 +
6454 +               /* the number of buckets */
6455 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6456 +               p->n += n;
6457 +               limit -= n;
6458 +
6459 +               sum = 0;
6460 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6461 +                    i++, hbl++) {
6462 +                       n = au_hbl_count(hbl);
6463 +                       sum += n;
6464 +
6465 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6466 +                       p->n += n;
6467 +                       limit -= n;
6468 +                       if (unlikely(limit <= 0))
6469 +                               goto out_free;
6470 +               }
6471 +               p->a[p->n - 1] = '\n';
6472 +
6473 +               /* the sum of plinks */
6474 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6475 +               p->n += n;
6476 +               limit -= n;
6477 +               if (unlikely(limit <= 0))
6478 +                       goto out_free;
6479 +       } else {
6480 +#define str "1\n0\n0\n"
6481 +               p->n = sizeof(str) - 1;
6482 +               strscpy(p->a, str, sizeof(str));
6483 +#undef str
6484 +       }
6485 +       si_read_unlock(sb);
6486 +
6487 +       err = 0;
6488 +       file->private_data = p;
6489 +       goto out; /* success */
6490 +
6491 +out_free:
6492 +       free_page((unsigned long)p);
6493 +out:
6494 +       return err;
6495 +}
6496 +
6497 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6498 +                                 size_t count, loff_t *ppos)
6499 +{
6500 +       struct dbgaufs_plink_arg *p;
6501 +
6502 +       p = file->private_data;
6503 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6504 +}
6505 +
6506 +static const struct file_operations dbgaufs_plink_fop = {
6507 +       .owner          = THIS_MODULE,
6508 +       .open           = dbgaufs_plink_open,
6509 +       .release        = dbgaufs_plink_release,
6510 +       .read           = dbgaufs_plink_read
6511 +};
6512 +
6513 +/* ---------------------------------------------------------------------- */
6514 +
6515 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6516 +{
6517 +       int err;
6518 +       struct au_sbinfo *sbinfo;
6519 +       struct super_block *sb;
6520 +
6521 +       sbinfo = inode->i_private;
6522 +       sb = sbinfo->si_sb;
6523 +       si_noflush_read_lock(sb);
6524 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6525 +       si_read_unlock(sb);
6526 +       return err;
6527 +}
6528 +
6529 +static const struct file_operations dbgaufs_xib_fop = {
6530 +       .owner          = THIS_MODULE,
6531 +       .open           = dbgaufs_xib_open,
6532 +       .release        = dbgaufs_xi_release,
6533 +       .read           = dbgaufs_xi_read
6534 +};
6535 +
6536 +/* ---------------------------------------------------------------------- */
6537 +
6538 +#define DbgaufsXi_PREFIX "xi"
6539 +
6540 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6541 +{
6542 +       int err, idx;
6543 +       long l;
6544 +       aufs_bindex_t bindex;
6545 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6546 +       struct au_sbinfo *sbinfo;
6547 +       struct super_block *sb;
6548 +       struct au_xino *xi;
6549 +       struct file *xf;
6550 +       struct qstr *name;
6551 +       struct au_branch *br;
6552 +
6553 +       err = -ENOENT;
6554 +       name = &file->f_path.dentry->d_name;
6555 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6556 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6557 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6558 +               goto out;
6559 +
6560 +       AuDebugOn(name->len >= sizeof(a));
6561 +       memcpy(a, name->name, name->len);
6562 +       a[name->len] = '\0';
6563 +       p = strchr(a, '-');
6564 +       if (p)
6565 +               *p = '\0';
6566 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6567 +       if (unlikely(err))
6568 +               goto out;
6569 +       bindex = l;
6570 +       idx = 0;
6571 +       if (p) {
6572 +               err = kstrtol(p + 1, 10, &l);
6573 +               if (unlikely(err))
6574 +                       goto out;
6575 +               idx = l;
6576 +       }
6577 +
6578 +       err = -ENOENT;
6579 +       sbinfo = inode->i_private;
6580 +       sb = sbinfo->si_sb;
6581 +       si_noflush_read_lock(sb);
6582 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6583 +               goto out_si;
6584 +       br = au_sbr(sb, bindex);
6585 +       xi = br->br_xino;
6586 +       if (unlikely(idx >= xi->xi_nfile))
6587 +               goto out_si;
6588 +       xf = au_xino_file(xi, idx);
6589 +       if (xf)
6590 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6591 +                                     au_xino_count(br));
6592 +
6593 +out_si:
6594 +       si_read_unlock(sb);
6595 +out:
6596 +       AuTraceErr(err);
6597 +       return err;
6598 +}
6599 +
6600 +static const struct file_operations dbgaufs_xino_fop = {
6601 +       .owner          = THIS_MODULE,
6602 +       .open           = dbgaufs_xino_open,
6603 +       .release        = dbgaufs_xi_release,
6604 +       .read           = dbgaufs_xi_read
6605 +};
6606 +
6607 +void dbgaufs_xino_del(struct au_branch *br)
6608 +{
6609 +       struct dentry *dbgaufs;
6610 +
6611 +       dbgaufs = br->br_dbgaufs;
6612 +       if (!dbgaufs)
6613 +               return;
6614 +
6615 +       br->br_dbgaufs = NULL;
6616 +       /* debugfs acquires the parent i_mutex */
6617 +       lockdep_off();
6618 +       debugfs_remove(dbgaufs);
6619 +       lockdep_on();
6620 +}
6621 +
6622 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6623 +{
6624 +       aufs_bindex_t bbot;
6625 +       struct au_branch *br;
6626 +
6627 +       if (!au_sbi(sb)->si_dbgaufs)
6628 +               return;
6629 +
6630 +       bbot = au_sbbot(sb);
6631 +       for (; bindex <= bbot; bindex++) {
6632 +               br = au_sbr(sb, bindex);
6633 +               dbgaufs_xino_del(br);
6634 +       }
6635 +}
6636 +
6637 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6638 +                             unsigned int idx, struct dentry *parent,
6639 +                             struct au_sbinfo *sbinfo)
6640 +{
6641 +       struct au_branch *br;
6642 +       struct dentry *d;
6643 +       /* "xi" bindex(5) "-" idx(2) NULL */
6644 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6645 +
6646 +       if (!idx)
6647 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6648 +       else
6649 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6650 +                        bindex, idx);
6651 +       br = au_sbr(sb, bindex);
6652 +       if (br->br_dbgaufs) {
6653 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6654 +
6655 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6656 +                       /* debugfs acquires the parent i_mutex */
6657 +                       lockdep_off();
6658 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6659 +                                          name);
6660 +                       lockdep_on();
6661 +                       if (unlikely(!d))
6662 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6663 +                                       parent, name);
6664 +               }
6665 +       } else {
6666 +               lockdep_off();
6667 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6668 +                                                    sbinfo, &dbgaufs_xino_fop);
6669 +               lockdep_on();
6670 +               if (unlikely(!br->br_dbgaufs))
6671 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6672 +                               parent, name);
6673 +       }
6674 +}
6675 +
6676 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6677 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6678 +{
6679 +       struct au_branch *br;
6680 +       struct au_xino *xi;
6681 +       unsigned int u;
6682 +
6683 +       br = au_sbr(sb, bindex);
6684 +       xi = br->br_xino;
6685 +       for (u = 0; u < xi->xi_nfile; u++)
6686 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6687 +}
6688 +
6689 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6690 +{
6691 +       struct au_sbinfo *sbinfo;
6692 +       struct dentry *parent;
6693 +       aufs_bindex_t bbot;
6694 +
6695 +       if (!au_opt_test(au_mntflags(sb), XINO))
6696 +               return;
6697 +
6698 +       sbinfo = au_sbi(sb);
6699 +       parent = sbinfo->si_dbgaufs;
6700 +       if (!parent)
6701 +               return;
6702 +
6703 +       bbot = au_sbbot(sb);
6704 +       if (topdown)
6705 +               for (; bindex <= bbot; bindex++)
6706 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6707 +       else
6708 +               for (; bbot >= bindex; bbot--)
6709 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6710 +}
6711 +
6712 +/* ---------------------------------------------------------------------- */
6713 +
6714 +#ifdef CONFIG_AUFS_EXPORT
6715 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6716 +{
6717 +       int err;
6718 +       struct au_sbinfo *sbinfo;
6719 +       struct super_block *sb;
6720 +
6721 +       sbinfo = inode->i_private;
6722 +       sb = sbinfo->si_sb;
6723 +       si_noflush_read_lock(sb);
6724 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6725 +       si_read_unlock(sb);
6726 +       return err;
6727 +}
6728 +
6729 +static const struct file_operations dbgaufs_xigen_fop = {
6730 +       .owner          = THIS_MODULE,
6731 +       .open           = dbgaufs_xigen_open,
6732 +       .release        = dbgaufs_xi_release,
6733 +       .read           = dbgaufs_xi_read
6734 +};
6735 +
6736 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6737 +{
6738 +       int err;
6739 +
6740 +       /*
6741 +        * This function is a dynamic '__init' function actually,
6742 +        * so the tiny check for si_rwsem is unnecessary.
6743 +        */
6744 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6745 +
6746 +       err = -EIO;
6747 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6748 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6749 +                &dbgaufs_xigen_fop);
6750 +       if (sbinfo->si_dbgaufs_xigen)
6751 +               err = 0;
6752 +
6753 +       return err;
6754 +}
6755 +#else
6756 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6757 +{
6758 +       return 0;
6759 +}
6760 +#endif /* CONFIG_AUFS_EXPORT */
6761 +
6762 +/* ---------------------------------------------------------------------- */
6763 +
6764 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6765 +{
6766 +       /*
6767 +        * This function is a dynamic '__fin' function actually,
6768 +        * so the tiny check for si_rwsem is unnecessary.
6769 +        */
6770 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6771 +
6772 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6773 +       sbinfo->si_dbgaufs = NULL;
6774 +}
6775 +
6776 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6777 +{
6778 +       int err;
6779 +       char name[SysaufsSiNameLen];
6780 +
6781 +       /*
6782 +        * This function is a dynamic '__init' function actually,
6783 +        * so the tiny check for si_rwsem is unnecessary.
6784 +        */
6785 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6786 +
6787 +       err = -ENOENT;
6788 +       if (!dbgaufs) {
6789 +               AuErr1("/debug/aufs is uninitialized\n");
6790 +               goto out;
6791 +       }
6792 +
6793 +       err = -EIO;
6794 +       sysaufs_name(sbinfo, name);
6795 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6796 +       if (unlikely(!sbinfo->si_dbgaufs))
6797 +               goto out;
6798 +
6799 +       /* regardless plink/noplink option */
6800 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6801 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6802 +                &dbgaufs_plink_fop);
6803 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6804 +               goto out_dir;
6805 +
6806 +       /* regardless xino/noxino option */
6807 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6808 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6809 +                &dbgaufs_xib_fop);
6810 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6811 +               goto out_dir;
6812 +
6813 +       err = dbgaufs_xigen_init(sbinfo);
6814 +       if (!err)
6815 +               goto out; /* success */
6816 +
6817 +out_dir:
6818 +       dbgaufs_si_fin(sbinfo);
6819 +out:
6820 +       if (unlikely(err))
6821 +               pr_err("debugfs/aufs failed\n");
6822 +       return err;
6823 +}
6824 +
6825 +/* ---------------------------------------------------------------------- */
6826 +
6827 +void dbgaufs_fin(void)
6828 +{
6829 +       debugfs_remove(dbgaufs);
6830 +}
6831 +
6832 +int __init dbgaufs_init(void)
6833 +{
6834 +       int err;
6835 +
6836 +       err = -EIO;
6837 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6838 +       if (dbgaufs)
6839 +               err = 0;
6840 +       return err;
6841 +}
6842 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6843 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6844 +++ linux/fs/aufs/dbgaufs.h     2022-11-05 23:02:18.962555950 +0100
6845 @@ -0,0 +1,53 @@
6846 +/* SPDX-License-Identifier: GPL-2.0 */
6847 +/*
6848 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6849 + *
6850 + * This program is free software; you can redistribute it and/or modify
6851 + * it under the terms of the GNU General Public License as published by
6852 + * the Free Software Foundation; either version 2 of the License, or
6853 + * (at your option) any later version.
6854 + *
6855 + * This program is distributed in the hope that it will be useful,
6856 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6857 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6858 + * GNU General Public License for more details.
6859 + *
6860 + * You should have received a copy of the GNU General Public License
6861 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6862 + */
6863 +
6864 +/*
6865 + * debugfs interface
6866 + */
6867 +
6868 +#ifndef __DBGAUFS_H__
6869 +#define __DBGAUFS_H__
6870 +
6871 +#ifdef __KERNEL__
6872 +
6873 +struct super_block;
6874 +struct au_sbinfo;
6875 +struct au_branch;
6876 +
6877 +#ifdef CONFIG_DEBUG_FS
6878 +/* dbgaufs.c */
6879 +void dbgaufs_xino_del(struct au_branch *br);
6880 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6881 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6882 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6883 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6884 +void dbgaufs_fin(void);
6885 +int __init dbgaufs_init(void);
6886 +#else
6887 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6888 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6889 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6890 +          int topdown)
6891 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6892 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6893 +AuStubVoid(dbgaufs_fin, void)
6894 +AuStubInt0(__init dbgaufs_init, void)
6895 +#endif /* CONFIG_DEBUG_FS */
6896 +
6897 +#endif /* __KERNEL__ */
6898 +#endif /* __DBGAUFS_H__ */
6899 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6900 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6901 +++ linux/fs/aufs/dcsub.c       2022-11-05 23:02:18.962555950 +0100
6902 @@ -0,0 +1,225 @@
6903 +// SPDX-License-Identifier: GPL-2.0
6904 +/*
6905 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6906 + *
6907 + * This program is free software; you can redistribute it and/or modify
6908 + * it under the terms of the GNU General Public License as published by
6909 + * the Free Software Foundation; either version 2 of the License, or
6910 + * (at your option) any later version.
6911 + *
6912 + * This program is distributed in the hope that it will be useful,
6913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6915 + * GNU General Public License for more details.
6916 + *
6917 + * You should have received a copy of the GNU General Public License
6918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6919 + */
6920 +
6921 +/*
6922 + * sub-routines for dentry cache
6923 + */
6924 +
6925 +#include "aufs.h"
6926 +
6927 +static void au_dpage_free(struct au_dpage *dpage)
6928 +{
6929 +       int i;
6930 +       struct dentry **p;
6931 +
6932 +       p = dpage->dentries;
6933 +       for (i = 0; i < dpage->ndentry; i++)
6934 +               dput(*p++);
6935 +       free_page((unsigned long)dpage->dentries);
6936 +}
6937 +
6938 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6939 +{
6940 +       int err;
6941 +       void *p;
6942 +
6943 +       err = -ENOMEM;
6944 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6945 +       if (unlikely(!dpages->dpages))
6946 +               goto out;
6947 +
6948 +       p = (void *)__get_free_page(gfp);
6949 +       if (unlikely(!p))
6950 +               goto out_dpages;
6951 +
6952 +       dpages->dpages[0].ndentry = 0;
6953 +       dpages->dpages[0].dentries = p;
6954 +       dpages->ndpage = 1;
6955 +       return 0; /* success */
6956 +
6957 +out_dpages:
6958 +       au_kfree_try_rcu(dpages->dpages);
6959 +out:
6960 +       return err;
6961 +}
6962 +
6963 +void au_dpages_free(struct au_dcsub_pages *dpages)
6964 +{
6965 +       int i;
6966 +       struct au_dpage *p;
6967 +
6968 +       p = dpages->dpages;
6969 +       for (i = 0; i < dpages->ndpage; i++)
6970 +               au_dpage_free(p++);
6971 +       au_kfree_try_rcu(dpages->dpages);
6972 +}
6973 +
6974 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6975 +                           struct dentry *dentry, gfp_t gfp)
6976 +{
6977 +       int err, sz;
6978 +       struct au_dpage *dpage;
6979 +       void *p;
6980 +
6981 +       dpage = dpages->dpages + dpages->ndpage - 1;
6982 +       sz = PAGE_SIZE / sizeof(dentry);
6983 +       if (unlikely(dpage->ndentry >= sz)) {
6984 +               AuLabel(new dpage);
6985 +               err = -ENOMEM;
6986 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6987 +               p = au_kzrealloc(dpages->dpages, sz,
6988 +                                sz + sizeof(*dpages->dpages), gfp,
6989 +                                /*may_shrink*/0);
6990 +               if (unlikely(!p))
6991 +                       goto out;
6992 +
6993 +               dpages->dpages = p;
6994 +               dpage = dpages->dpages + dpages->ndpage;
6995 +               p = (void *)__get_free_page(gfp);
6996 +               if (unlikely(!p))
6997 +                       goto out;
6998 +
6999 +               dpage->ndentry = 0;
7000 +               dpage->dentries = p;
7001 +               dpages->ndpage++;
7002 +       }
7003 +
7004 +       AuDebugOn(au_dcount(dentry) <= 0);
7005 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7006 +       return 0; /* success */
7007 +
7008 +out:
7009 +       return err;
7010 +}
7011 +
7012 +/* todo: BAD approach */
7013 +/* copied from linux/fs/dcache.c */
7014 +enum d_walk_ret {
7015 +       D_WALK_CONTINUE,
7016 +       D_WALK_QUIT,
7017 +       D_WALK_NORETRY,
7018 +       D_WALK_SKIP,
7019 +};
7020 +
7021 +extern void d_walk(struct dentry *parent, void *data,
7022 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7023 +
7024 +struct ac_dpages_arg {
7025 +       int err;
7026 +       struct au_dcsub_pages *dpages;
7027 +       struct super_block *sb;
7028 +       au_dpages_test test;
7029 +       void *arg;
7030 +};
7031 +
7032 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7033 +{
7034 +       enum d_walk_ret ret;
7035 +       struct ac_dpages_arg *arg = _arg;
7036 +
7037 +       ret = D_WALK_CONTINUE;
7038 +       if (dentry->d_sb == arg->sb
7039 +           && !IS_ROOT(dentry)
7040 +           && au_dcount(dentry) > 0
7041 +           && au_di(dentry)
7042 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7043 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7044 +               if (unlikely(arg->err))
7045 +                       ret = D_WALK_QUIT;
7046 +       }
7047 +
7048 +       return ret;
7049 +}
7050 +
7051 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7052 +                  au_dpages_test test, void *arg)
7053 +{
7054 +       struct ac_dpages_arg args = {
7055 +               .err    = 0,
7056 +               .dpages = dpages,
7057 +               .sb     = root->d_sb,
7058 +               .test   = test,
7059 +               .arg    = arg
7060 +       };
7061 +
7062 +       d_walk(root, &args, au_call_dpages_append);
7063 +
7064 +       return args.err;
7065 +}
7066 +
7067 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7068 +                      int do_include, au_dpages_test test, void *arg)
7069 +{
7070 +       int err;
7071 +
7072 +       err = 0;
7073 +       write_seqlock(&rename_lock);
7074 +       spin_lock(&dentry->d_lock);
7075 +       if (do_include
7076 +           && au_dcount(dentry) > 0
7077 +           && (!test || test(dentry, arg)))
7078 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7079 +       spin_unlock(&dentry->d_lock);
7080 +       if (unlikely(err))
7081 +               goto out;
7082 +
7083 +       /*
7084 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7085 +        * mount
7086 +        */
7087 +       while (!IS_ROOT(dentry)) {
7088 +               dentry = dentry->d_parent; /* rename_lock is locked */
7089 +               spin_lock(&dentry->d_lock);
7090 +               if (au_dcount(dentry) > 0
7091 +                   && (!test || test(dentry, arg)))
7092 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7093 +               spin_unlock(&dentry->d_lock);
7094 +               if (unlikely(err))
7095 +                       break;
7096 +       }
7097 +
7098 +out:
7099 +       write_sequnlock(&rename_lock);
7100 +       return err;
7101 +}
7102 +
7103 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7104 +{
7105 +       return au_di(dentry) && dentry->d_sb == arg;
7106 +}
7107 +
7108 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7109 +                           struct dentry *dentry, int do_include)
7110 +{
7111 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7112 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7113 +}
7114 +
7115 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7116 +{
7117 +       struct path path[2] = {
7118 +               {
7119 +                       .dentry = d1
7120 +               },
7121 +               {
7122 +                       .dentry = d2
7123 +               }
7124 +       };
7125 +
7126 +       return path_is_under(path + 0, path + 1);
7127 +}
7128 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7129 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7130 +++ linux/fs/aufs/dcsub.h       2022-11-05 23:02:18.962555950 +0100
7131 @@ -0,0 +1,137 @@
7132 +/* SPDX-License-Identifier: GPL-2.0 */
7133 +/*
7134 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7135 + *
7136 + * This program is free software; you can redistribute it and/or modify
7137 + * it under the terms of the GNU General Public License as published by
7138 + * the Free Software Foundation; either version 2 of the License, or
7139 + * (at your option) any later version.
7140 + *
7141 + * This program is distributed in the hope that it will be useful,
7142 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7143 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7144 + * GNU General Public License for more details.
7145 + *
7146 + * You should have received a copy of the GNU General Public License
7147 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7148 + */
7149 +
7150 +/*
7151 + * sub-routines for dentry cache
7152 + */
7153 +
7154 +#ifndef __AUFS_DCSUB_H__
7155 +#define __AUFS_DCSUB_H__
7156 +
7157 +#ifdef __KERNEL__
7158 +
7159 +#include <linux/dcache.h>
7160 +#include <linux/fs.h>
7161 +
7162 +struct au_dpage {
7163 +       int ndentry;
7164 +       struct dentry **dentries;
7165 +};
7166 +
7167 +struct au_dcsub_pages {
7168 +       int ndpage;
7169 +       struct au_dpage *dpages;
7170 +};
7171 +
7172 +/* ---------------------------------------------------------------------- */
7173 +
7174 +/* dcsub.c */
7175 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7176 +void au_dpages_free(struct au_dcsub_pages *dpages);
7177 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7178 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7179 +                  au_dpages_test test, void *arg);
7180 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7181 +                      int do_include, au_dpages_test test, void *arg);
7182 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7183 +                           struct dentry *dentry, int do_include);
7184 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7185 +
7186 +/* ---------------------------------------------------------------------- */
7187 +
7188 +/*
7189 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7190 + * include/linux/dcache.h. Try them (in the future).
7191 + */
7192 +
7193 +static inline int au_d_hashed_positive(struct dentry *d)
7194 +{
7195 +       int err;
7196 +       struct inode *inode = d_inode(d);
7197 +
7198 +       err = 0;
7199 +       if (unlikely(d_unhashed(d)
7200 +                    || d_is_negative(d)
7201 +                    || !inode->i_nlink))
7202 +               err = -ENOENT;
7203 +       return err;
7204 +}
7205 +
7206 +static inline int au_d_linkable(struct dentry *d)
7207 +{
7208 +       int err;
7209 +       struct inode *inode = d_inode(d);
7210 +
7211 +       err = au_d_hashed_positive(d);
7212 +       if (err
7213 +           && d_is_positive(d)
7214 +           && (inode->i_state & I_LINKABLE))
7215 +               err = 0;
7216 +       return err;
7217 +}
7218 +
7219 +static inline int au_d_alive(struct dentry *d)
7220 +{
7221 +       int err;
7222 +       struct inode *inode;
7223 +
7224 +       err = 0;
7225 +       if (!IS_ROOT(d))
7226 +               err = au_d_hashed_positive(d);
7227 +       else {
7228 +               inode = d_inode(d);
7229 +               if (unlikely(d_unlinked(d)
7230 +                            || d_is_negative(d)
7231 +                            || !inode->i_nlink))
7232 +                       err = -ENOENT;
7233 +       }
7234 +       return err;
7235 +}
7236 +
7237 +static inline int au_alive_dir(struct dentry *d)
7238 +{
7239 +       int err;
7240 +
7241 +       err = au_d_alive(d);
7242 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7243 +               err = -ENOENT;
7244 +       return err;
7245 +}
7246 +
7247 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7248 +{
7249 +       return a->len == b->len
7250 +               && !memcmp(a->name, b->name, a->len);
7251 +}
7252 +
7253 +/*
7254 + * by the commit
7255 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7256 + *                     taking d_lock
7257 + * the type of d_lockref.count became int, but the inlined function d_count()
7258 + * still returns unsigned int.
7259 + * I don't know why. Maybe it is for every d_count() users?
7260 + * Anyway au_dcount() lives on.
7261 + */
7262 +static inline int au_dcount(struct dentry *d)
7263 +{
7264 +       return (int)d_count(d);
7265 +}
7266 +
7267 +#endif /* __KERNEL__ */
7268 +#endif /* __AUFS_DCSUB_H__ */
7269 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7270 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7271 +++ linux/fs/aufs/debug.c       2023-10-31 09:31:04.196547417 +0100
7272 @@ -0,0 +1,448 @@
7273 +// SPDX-License-Identifier: GPL-2.0
7274 +/*
7275 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7276 + *
7277 + * This program is free software; you can redistribute it and/or modify
7278 + * it under the terms of the GNU General Public License as published by
7279 + * the Free Software Foundation; either version 2 of the License, or
7280 + * (at your option) any later version.
7281 + *
7282 + * This program is distributed in the hope that it will be useful,
7283 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7284 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7285 + * GNU General Public License for more details.
7286 + *
7287 + * You should have received a copy of the GNU General Public License
7288 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7289 + */
7290 +
7291 +/*
7292 + * debug print functions
7293 + */
7294 +
7295 +#include <linux/iversion.h>
7296 +#include "aufs.h"
7297 +
7298 +/* Returns 0, or -errno.  arg is in kp->arg. */
7299 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7300 +{
7301 +       int err, n;
7302 +
7303 +       err = kstrtoint(val, 0, &n);
7304 +       if (!err) {
7305 +               if (n > 0)
7306 +                       au_debug_on();
7307 +               else
7308 +                       au_debug_off();
7309 +       }
7310 +       return err;
7311 +}
7312 +
7313 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7314 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7315 +{
7316 +       atomic_t *a;
7317 +
7318 +       a = kp->arg;
7319 +       return sprintf(buffer, "%d", atomic_read(a));
7320 +}
7321 +
7322 +static const struct kernel_param_ops param_ops_atomic_t = {
7323 +       .set = param_atomic_t_set,
7324 +       .get = param_atomic_t_get
7325 +       /* void (*free)(void *arg) */
7326 +};
7327 +
7328 +atomic_t aufs_debug = ATOMIC_INIT(0);
7329 +MODULE_PARM_DESC(debug, "debug print");
7330 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7331 +
7332 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7333 +char *au_plevel = KERN_DEBUG;
7334 +#define dpri(fmt, ...) do {                                    \
7335 +       if ((au_plevel                                          \
7336 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7337 +           || au_debug_test())                                 \
7338 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7339 +} while (0)
7340 +
7341 +/* ---------------------------------------------------------------------- */
7342 +
7343 +void au_dpri_whlist(struct au_nhash *whlist)
7344 +{
7345 +       unsigned long ul, n;
7346 +       struct hlist_head *head;
7347 +       struct au_vdir_wh *pos;
7348 +
7349 +       n = whlist->nh_num;
7350 +       head = whlist->nh_head;
7351 +       for (ul = 0; ul < n; ul++) {
7352 +               hlist_for_each_entry(pos, head, wh_hash)
7353 +                       dpri("b%d, %.*s, %d\n",
7354 +                            pos->wh_bindex,
7355 +                            pos->wh_str.len, pos->wh_str.name,
7356 +                            pos->wh_str.len);
7357 +               head++;
7358 +       }
7359 +}
7360 +
7361 +void au_dpri_vdir(struct au_vdir *vdir)
7362 +{
7363 +       unsigned long ul;
7364 +       union au_vdir_deblk_p p;
7365 +       unsigned char *o;
7366 +
7367 +       if (!vdir || IS_ERR(vdir)) {
7368 +               dpri("err %ld\n", PTR_ERR(vdir));
7369 +               return;
7370 +       }
7371 +
7372 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7373 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7374 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7375 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7376 +               p.deblk = vdir->vd_deblk[ul];
7377 +               o = p.deblk;
7378 +               dpri("[%lu]: %p\n", ul, o);
7379 +       }
7380 +}
7381 +
7382 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7383 +                       struct dentry *wh)
7384 +{
7385 +       char *n = NULL;
7386 +       int l = 0;
7387 +       struct timespec64 ctime;
7388 +
7389 +       if (!inode || IS_ERR(inode)) {
7390 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7391 +               return -1;
7392 +       }
7393 +
7394 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7395 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7396 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7397 +       if (wh) {
7398 +               n = (void *)wh->d_name.name;
7399 +               l = wh->d_name.len;
7400 +       }
7401 +
7402 +       ctime = inode_get_ctime(inode);
7403 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7404 +            " acl %p, def_acl %p,"
7405 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7406 +            bindex, inode,
7407 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7408 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7409 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7410 +            inode->i_acl, inode->i_default_acl,
7411 +            hn, (long long)timespec64_to_ns(&ctime) & 0x0ffff,
7412 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7413 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7414 +            inode->i_generation,
7415 +            l ? ", wh " : "", l, n);
7416 +       return 0;
7417 +}
7418 +
7419 +void au_dpri_inode(struct inode *inode)
7420 +{
7421 +       struct au_iinfo *iinfo;
7422 +       struct au_hinode *hi;
7423 +       aufs_bindex_t bindex;
7424 +       int err, hn;
7425 +
7426 +       err = do_pri_inode(-1, inode, -1, NULL);
7427 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7428 +               return;
7429 +
7430 +       iinfo = au_ii(inode);
7431 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7432 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7433 +       if (iinfo->ii_btop < 0)
7434 +               return;
7435 +       hn = 0;
7436 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7437 +               hi = au_hinode(iinfo, bindex);
7438 +               hn = !!au_hn(hi);
7439 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7440 +       }
7441 +}
7442 +
7443 +void au_dpri_dalias(struct inode *inode)
7444 +{
7445 +       struct dentry *d;
7446 +
7447 +       spin_lock(&inode->i_lock);
7448 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7449 +               au_dpri_dentry(d);
7450 +       spin_unlock(&inode->i_lock);
7451 +}
7452 +
7453 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7454 +{
7455 +       struct dentry *wh = NULL;
7456 +       int hn;
7457 +       struct inode *inode;
7458 +       struct au_iinfo *iinfo;
7459 +       struct au_hinode *hi;
7460 +
7461 +       if (!dentry || IS_ERR(dentry)) {
7462 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7463 +               return -1;
7464 +       }
7465 +       /* do not call dget_parent() here */
7466 +       /* note: access d_xxx without d_lock */
7467 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7468 +            bindex, dentry, dentry,
7469 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7470 +            au_dcount(dentry), dentry->d_flags,
7471 +            d_unhashed(dentry) ? "un" : "");
7472 +       hn = -1;
7473 +       inode = NULL;
7474 +       if (d_is_positive(dentry))
7475 +               inode = d_inode(dentry);
7476 +       if (inode
7477 +           && au_test_aufs(dentry->d_sb)
7478 +           && bindex >= 0
7479 +           && !au_is_bad_inode(inode)) {
7480 +               iinfo = au_ii(inode);
7481 +               hi = au_hinode(iinfo, bindex);
7482 +               hn = !!au_hn(hi);
7483 +               wh = hi->hi_whdentry;
7484 +       }
7485 +       do_pri_inode(bindex, inode, hn, wh);
7486 +       return 0;
7487 +}
7488 +
7489 +void au_dpri_dentry(struct dentry *dentry)
7490 +{
7491 +       struct au_dinfo *dinfo;
7492 +       aufs_bindex_t bindex;
7493 +       int err;
7494 +
7495 +       err = do_pri_dentry(-1, dentry);
7496 +       if (err || !au_test_aufs(dentry->d_sb))
7497 +               return;
7498 +
7499 +       dinfo = au_di(dentry);
7500 +       if (!dinfo)
7501 +               return;
7502 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7503 +            dinfo->di_btop, dinfo->di_bbot,
7504 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7505 +            dinfo->di_tmpfile);
7506 +       if (dinfo->di_btop < 0)
7507 +               return;
7508 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7509 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7510 +}
7511 +
7512 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7513 +{
7514 +       char a[32];
7515 +
7516 +       if (!file || IS_ERR(file)) {
7517 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7518 +               return -1;
7519 +       }
7520 +       a[0] = 0;
7521 +       if (bindex < 0
7522 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7523 +           && au_test_aufs(file->f_path.dentry->d_sb)
7524 +           && au_fi(file))
7525 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7526 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7527 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7528 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7529 +            file->f_version, file->f_pos, a);
7530 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7531 +               do_pri_dentry(bindex, file->f_path.dentry);
7532 +       return 0;
7533 +}
7534 +
7535 +void au_dpri_file(struct file *file)
7536 +{
7537 +       struct au_finfo *finfo;
7538 +       struct au_fidir *fidir;
7539 +       struct au_hfile *hfile;
7540 +       aufs_bindex_t bindex;
7541 +       int err;
7542 +
7543 +       err = do_pri_file(-1, file);
7544 +       if (err
7545 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7546 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7547 +               return;
7548 +
7549 +       finfo = au_fi(file);
7550 +       if (!finfo)
7551 +               return;
7552 +       if (finfo->fi_btop < 0)
7553 +               return;
7554 +       fidir = finfo->fi_hdir;
7555 +       if (!fidir)
7556 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7557 +       else
7558 +               for (bindex = finfo->fi_btop;
7559 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7560 +                    bindex++) {
7561 +                       hfile = fidir->fd_hfile + bindex;
7562 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7563 +               }
7564 +}
7565 +
7566 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7567 +{
7568 +       struct vfsmount *mnt;
7569 +       struct super_block *sb;
7570 +
7571 +       if (!br || IS_ERR(br))
7572 +               goto out;
7573 +       mnt = au_br_mnt(br);
7574 +       if (!mnt || IS_ERR(mnt))
7575 +               goto out;
7576 +       sb = mnt->mnt_sb;
7577 +       if (!sb || IS_ERR(sb))
7578 +               goto out;
7579 +
7580 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7581 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7582 +            "xino %d\n",
7583 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7584 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7585 +            sb->s_flags, sb->s_count,
7586 +            atomic_read(&sb->s_active),
7587 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7588 +       return 0;
7589 +
7590 +out:
7591 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7592 +       return -1;
7593 +}
7594 +
7595 +void au_dpri_sb(struct super_block *sb)
7596 +{
7597 +       struct au_sbinfo *sbinfo;
7598 +       aufs_bindex_t bindex;
7599 +       int err;
7600 +       /* to reduce stack size */
7601 +       struct {
7602 +               struct vfsmount mnt;
7603 +               struct au_branch fake;
7604 +       } *a;
7605 +
7606 +       /* this function can be called from magic sysrq */
7607 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7608 +       if (unlikely(!a)) {
7609 +               dpri("no memory\n");
7610 +               return;
7611 +       }
7612 +
7613 +       a->mnt.mnt_sb = sb;
7614 +       a->fake.br_path.mnt = &a->mnt;
7615 +       err = do_pri_br(-1, &a->fake);
7616 +       au_kfree_rcu(a);
7617 +       dpri("dev 0x%x\n", sb->s_dev);
7618 +       if (err || !au_test_aufs(sb))
7619 +               return;
7620 +
7621 +       sbinfo = au_sbi(sb);
7622 +       if (!sbinfo)
7623 +               return;
7624 +       dpri("nw %d, gen %u, kobj %d\n",
7625 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7626 +            kref_read(&sbinfo->si_kobj.kref));
7627 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7628 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7629 +}
7630 +
7631 +/* ---------------------------------------------------------------------- */
7632 +
7633 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7634 +{
7635 +       struct inode *h_inode, *inode = d_inode(dentry);
7636 +       struct dentry *h_dentry;
7637 +       aufs_bindex_t bindex, bbot, bi;
7638 +
7639 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7640 +               return;
7641 +
7642 +       bbot = au_dbbot(dentry);
7643 +       bi = au_ibbot(inode);
7644 +       if (bi < bbot)
7645 +               bbot = bi;
7646 +       bindex = au_dbtop(dentry);
7647 +       bi = au_ibtop(inode);
7648 +       if (bi > bindex)
7649 +               bindex = bi;
7650 +
7651 +       for (; bindex <= bbot; bindex++) {
7652 +               h_dentry = au_h_dptr(dentry, bindex);
7653 +               if (!h_dentry)
7654 +                       continue;
7655 +               h_inode = au_h_iptr(inode, bindex);
7656 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7657 +                       au_debug_on();
7658 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7659 +                       AuDbgDentry(dentry);
7660 +                       AuDbgInode(inode);
7661 +                       au_debug_off();
7662 +                       if (au_test_fuse(h_inode->i_sb))
7663 +                               WARN_ON_ONCE(1);
7664 +                       else
7665 +                               BUG();
7666 +               }
7667 +       }
7668 +}
7669 +
7670 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7671 +{
7672 +       int err, i, j;
7673 +       struct au_dcsub_pages dpages;
7674 +       struct au_dpage *dpage;
7675 +       struct dentry **dentries;
7676 +
7677 +       err = au_dpages_init(&dpages, GFP_NOFS);
7678 +       AuDebugOn(err);
7679 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7680 +       AuDebugOn(err);
7681 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7682 +               dpage = dpages.dpages + i;
7683 +               dentries = dpage->dentries;
7684 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7685 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7686 +       }
7687 +       au_dpages_free(&dpages);
7688 +}
7689 +
7690 +void au_dbg_verify_kthread(void)
7691 +{
7692 +       if (au_wkq_test()) {
7693 +               au_dbg_blocked();
7694 +               /*
7695 +                * It may be recursive, but udba=notify between two aufs mounts,
7696 +                * where a single ro branch is shared, is not a problem.
7697 +                */
7698 +               /* WARN_ON(1); */
7699 +       }
7700 +}
7701 +
7702 +/* ---------------------------------------------------------------------- */
7703 +
7704 +int __init au_debug_init(void)
7705 +{
7706 +       aufs_bindex_t bindex;
7707 +       struct au_vdir_destr destr;
7708 +
7709 +       bindex = -1;
7710 +       AuDebugOn(bindex >= 0);
7711 +
7712 +       destr.len = -1;
7713 +       AuDebugOn(destr.len < NAME_MAX);
7714 +
7715 +#ifdef CONFIG_4KSTACKS
7716 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7717 +#endif
7718 +
7719 +       return 0;
7720 +}
7721 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7722 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7723 +++ linux/fs/aufs/debug.h       2022-11-05 23:02:18.962555950 +0100
7724 @@ -0,0 +1,226 @@
7725 +/* SPDX-License-Identifier: GPL-2.0 */
7726 +/*
7727 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7728 + *
7729 + * This program is free software; you can redistribute it and/or modify
7730 + * it under the terms of the GNU General Public License as published by
7731 + * the Free Software Foundation; either version 2 of the License, or
7732 + * (at your option) any later version.
7733 + *
7734 + * This program is distributed in the hope that it will be useful,
7735 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7736 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7737 + * GNU General Public License for more details.
7738 + *
7739 + * You should have received a copy of the GNU General Public License
7740 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7741 + */
7742 +
7743 +/*
7744 + * debug print functions
7745 + */
7746 +
7747 +#ifndef __AUFS_DEBUG_H__
7748 +#define __AUFS_DEBUG_H__
7749 +
7750 +#ifdef __KERNEL__
7751 +
7752 +#include <linux/atomic.h>
7753 +#include <linux/module.h>
7754 +#include <linux/kallsyms.h>
7755 +#include <linux/sysrq.h>
7756 +
7757 +#ifdef CONFIG_AUFS_DEBUG
7758 +#define AuDebugOn(a)           BUG_ON(a)
7759 +
7760 +/* module parameter */
7761 +extern atomic_t aufs_debug;
7762 +static inline void au_debug_on(void)
7763 +{
7764 +       atomic_inc(&aufs_debug);
7765 +}
7766 +static inline void au_debug_off(void)
7767 +{
7768 +       atomic_dec_if_positive(&aufs_debug);
7769 +}
7770 +
7771 +static inline int au_debug_test(void)
7772 +{
7773 +       return atomic_read(&aufs_debug) > 0;
7774 +}
7775 +#else
7776 +#define AuDebugOn(a)           do {} while (0)
7777 +AuStubVoid(au_debug_on, void)
7778 +AuStubVoid(au_debug_off, void)
7779 +AuStubInt0(au_debug_test, void)
7780 +#endif /* CONFIG_AUFS_DEBUG */
7781 +
7782 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7783 +
7784 +/* ---------------------------------------------------------------------- */
7785 +
7786 +/* debug print */
7787 +
7788 +#define AuDbg(fmt, ...) do { \
7789 +       if (au_debug_test()) \
7790 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7791 +} while (0)
7792 +#define AuLabel(l)             AuDbg(#l "\n")
7793 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7794 +#define AuWarn1(fmt, ...) do { \
7795 +       static unsigned char _c; \
7796 +       if (!_c++) \
7797 +               pr_warn(fmt, ##__VA_ARGS__); \
7798 +} while (0)
7799 +
7800 +#define AuErr1(fmt, ...) do { \
7801 +       static unsigned char _c; \
7802 +       if (!_c++) \
7803 +               pr_err(fmt, ##__VA_ARGS__); \
7804 +} while (0)
7805 +
7806 +#define AuIOErr1(fmt, ...) do { \
7807 +       static unsigned char _c; \
7808 +       if (!_c++) \
7809 +               AuIOErr(fmt, ##__VA_ARGS__); \
7810 +} while (0)
7811 +
7812 +#define AuUnsupportMsg "This operation is not supported." \
7813 +                       " Please report this application to aufs-users ML."
7814 +#define AuUnsupport(fmt, ...) do { \
7815 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7816 +       dump_stack(); \
7817 +} while (0)
7818 +
7819 +#define AuTraceErr(e) do { \
7820 +       if (unlikely((e) < 0)) \
7821 +               AuDbg("err %d\n", (int)(e)); \
7822 +} while (0)
7823 +
7824 +#define AuTraceErrPtr(p) do { \
7825 +       if (IS_ERR(p)) \
7826 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7827 +} while (0)
7828 +
7829 +/* dirty macros for debug print, use with "%.*s" and caution */
7830 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7831 +
7832 +/* ---------------------------------------------------------------------- */
7833 +
7834 +struct dentry;
7835 +#ifdef CONFIG_AUFS_DEBUG
7836 +extern struct mutex au_dbg_mtx;
7837 +extern char *au_plevel;
7838 +struct au_nhash;
7839 +void au_dpri_whlist(struct au_nhash *whlist);
7840 +struct au_vdir;
7841 +void au_dpri_vdir(struct au_vdir *vdir);
7842 +struct inode;
7843 +void au_dpri_inode(struct inode *inode);
7844 +void au_dpri_dalias(struct inode *inode);
7845 +void au_dpri_dentry(struct dentry *dentry);
7846 +struct file;
7847 +void au_dpri_file(struct file *filp);
7848 +struct super_block;
7849 +void au_dpri_sb(struct super_block *sb);
7850 +
7851 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7852 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7853 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7854 +void au_dbg_verify_kthread(void);
7855 +
7856 +int __init au_debug_init(void);
7857 +
7858 +#define AuDbgWhlist(w) do { \
7859 +       mutex_lock(&au_dbg_mtx); \
7860 +       AuDbg(#w "\n"); \
7861 +       au_dpri_whlist(w); \
7862 +       mutex_unlock(&au_dbg_mtx); \
7863 +} while (0)
7864 +
7865 +#define AuDbgVdir(v) do { \
7866 +       mutex_lock(&au_dbg_mtx); \
7867 +       AuDbg(#v "\n"); \
7868 +       au_dpri_vdir(v); \
7869 +       mutex_unlock(&au_dbg_mtx); \
7870 +} while (0)
7871 +
7872 +#define AuDbgInode(i) do { \
7873 +       mutex_lock(&au_dbg_mtx); \
7874 +       AuDbg(#i "\n"); \
7875 +       au_dpri_inode(i); \
7876 +       mutex_unlock(&au_dbg_mtx); \
7877 +} while (0)
7878 +
7879 +#define AuDbgDAlias(i) do { \
7880 +       mutex_lock(&au_dbg_mtx); \
7881 +       AuDbg(#i "\n"); \
7882 +       au_dpri_dalias(i); \
7883 +       mutex_unlock(&au_dbg_mtx); \
7884 +} while (0)
7885 +
7886 +#define AuDbgDentry(d) do { \
7887 +       mutex_lock(&au_dbg_mtx); \
7888 +       AuDbg(#d "\n"); \
7889 +       au_dpri_dentry(d); \
7890 +       mutex_unlock(&au_dbg_mtx); \
7891 +} while (0)
7892 +
7893 +#define AuDbgFile(f) do { \
7894 +       mutex_lock(&au_dbg_mtx); \
7895 +       AuDbg(#f "\n"); \
7896 +       au_dpri_file(f); \
7897 +       mutex_unlock(&au_dbg_mtx); \
7898 +} while (0)
7899 +
7900 +#define AuDbgSb(sb) do { \
7901 +       mutex_lock(&au_dbg_mtx); \
7902 +       AuDbg(#sb "\n"); \
7903 +       au_dpri_sb(sb); \
7904 +       mutex_unlock(&au_dbg_mtx); \
7905 +} while (0)
7906 +
7907 +#define AuDbgSym(addr) do {                            \
7908 +       char sym[KSYM_SYMBOL_LEN];                      \
7909 +       sprint_symbol(sym, (unsigned long)addr);        \
7910 +       AuDbg("%s\n", sym);                             \
7911 +} while (0)
7912 +#else
7913 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7914 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7915 +AuStubVoid(au_dbg_verify_kthread, void)
7916 +AuStubInt0(__init au_debug_init, void)
7917 +
7918 +#define AuDbgWhlist(w)         do {} while (0)
7919 +#define AuDbgVdir(v)           do {} while (0)
7920 +#define AuDbgInode(i)          do {} while (0)
7921 +#define AuDbgDAlias(i)         do {} while (0)
7922 +#define AuDbgDentry(d)         do {} while (0)
7923 +#define AuDbgFile(f)           do {} while (0)
7924 +#define AuDbgSb(sb)            do {} while (0)
7925 +#define AuDbgSym(addr)         do {} while (0)
7926 +#endif /* CONFIG_AUFS_DEBUG */
7927 +
7928 +/* ---------------------------------------------------------------------- */
7929 +
7930 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7931 +int __init au_sysrq_init(void);
7932 +void au_sysrq_fin(void);
7933 +
7934 +#ifdef CONFIG_HW_CONSOLE
7935 +#define au_dbg_blocked() do { \
7936 +       WARN_ON(1); \
7937 +       handle_sysrq('w'); \
7938 +} while (0)
7939 +#else
7940 +AuStubVoid(au_dbg_blocked, void)
7941 +#endif
7942 +
7943 +#else
7944 +AuStubInt0(__init au_sysrq_init, void)
7945 +AuStubVoid(au_sysrq_fin, void)
7946 +AuStubVoid(au_dbg_blocked, void)
7947 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7948 +
7949 +#endif /* __KERNEL__ */
7950 +#endif /* __AUFS_DEBUG_H__ */
7951 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7952 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7953 +++ linux/fs/aufs/dentry.c      2023-10-31 09:31:04.196547417 +0100
7954 @@ -0,0 +1,1168 @@
7955 +// SPDX-License-Identifier: GPL-2.0
7956 +/*
7957 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7958 + *
7959 + * This program is free software; you can redistribute it and/or modify
7960 + * it under the terms of the GNU General Public License as published by
7961 + * the Free Software Foundation; either version 2 of the License, or
7962 + * (at your option) any later version.
7963 + *
7964 + * This program is distributed in the hope that it will be useful,
7965 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7966 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7967 + * GNU General Public License for more details.
7968 + *
7969 + * You should have received a copy of the GNU General Public License
7970 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7971 + */
7972 +
7973 +/*
7974 + * lookup and dentry operations
7975 + */
7976 +
7977 +#include <linux/iversion.h>
7978 +#include "aufs.h"
7979 +
7980 +/*
7981 + * returns positive/negative dentry, NULL or an error.
7982 + * NULL means whiteout-ed or not-found.
7983 + */
7984 +static struct dentry*
7985 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7986 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7987 +{
7988 +       struct dentry *h_dentry;
7989 +       struct inode *h_inode;
7990 +       struct au_branch *br;
7991 +       struct mnt_idmap *h_idmap;
7992 +       struct path h_path;
7993 +       int wh_found, opq;
7994 +       unsigned char wh_able;
7995 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7996 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7997 +                                                         IGNORE_PERM);
7998 +
7999 +       wh_found = 0;
8000 +       br = au_sbr(dentry->d_sb, bindex);
8001 +       h_path.dentry = h_parent;
8002 +       h_path.mnt = au_br_mnt(br);
8003 +       h_idmap = au_br_idmap(br);
8004 +       wh_able = !!au_br_whable(br->br_perm);
8005 +       if (wh_able)
8006 +               wh_found = au_wh_test(h_idmap, &h_path, &args->whname,
8007 +                                     ignore_perm);
8008 +       h_dentry = ERR_PTR(wh_found);
8009 +       if (!wh_found)
8010 +               goto real_lookup;
8011 +       if (unlikely(wh_found < 0))
8012 +               goto out;
8013 +
8014 +       /* We found a whiteout */
8015 +       /* au_set_dbbot(dentry, bindex); */
8016 +       au_set_dbwh(dentry, bindex);
8017 +       if (!allow_neg)
8018 +               return NULL; /* success */
8019 +
8020 +real_lookup:
8021 +       if (!ignore_perm)
8022 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8023 +       else
8024 +               h_dentry = au_sio_lkup_one(h_idmap, args->name, &h_path);
8025 +       if (IS_ERR(h_dentry)) {
8026 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8027 +                   && !allow_neg)
8028 +                       h_dentry = NULL;
8029 +               goto out;
8030 +       }
8031 +
8032 +       h_inode = d_inode(h_dentry);
8033 +       if (d_is_negative(h_dentry)) {
8034 +               if (!allow_neg)
8035 +                       goto out_neg;
8036 +       } else if (wh_found
8037 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8038 +               goto out_neg;
8039 +       else if (au_ftest_lkup(args->flags, DIRREN)
8040 +                /* && h_inode */
8041 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8042 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8043 +                     (unsigned long long)h_inode->i_ino);
8044 +               goto out_neg;
8045 +       }
8046 +
8047 +       if (au_dbbot(dentry) <= bindex)
8048 +               au_set_dbbot(dentry, bindex);
8049 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8050 +               au_set_dbtop(dentry, bindex);
8051 +       au_set_h_dptr(dentry, bindex, h_dentry);
8052 +
8053 +       if (!d_is_dir(h_dentry)
8054 +           || !wh_able
8055 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8056 +               goto out; /* success */
8057 +
8058 +       h_path.dentry = h_dentry;
8059 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8060 +       opq = au_diropq_test(h_idmap, &h_path);
8061 +       inode_unlock_shared(h_inode);
8062 +       if (opq > 0)
8063 +               au_set_dbdiropq(dentry, bindex);
8064 +       else if (unlikely(opq < 0)) {
8065 +               au_set_h_dptr(dentry, bindex, NULL);
8066 +               h_dentry = ERR_PTR(opq);
8067 +       }
8068 +       goto out;
8069 +
8070 +out_neg:
8071 +       dput(h_dentry);
8072 +       h_dentry = NULL;
8073 +out:
8074 +       return h_dentry;
8075 +}
8076 +
8077 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8078 +{
8079 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8080 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8081 +               return -EPERM;
8082 +       return 0;
8083 +}
8084 +
8085 +/*
8086 + * returns the number of lower positive dentries,
8087 + * otherwise an error.
8088 + * can be called at unlinking with @type is zero.
8089 + */
8090 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8091 +                  unsigned int flags)
8092 +{
8093 +       int npositive, err;
8094 +       aufs_bindex_t bindex, btail, bdiropq;
8095 +       unsigned char isdir, dirperm1, dirren;
8096 +       struct au_do_lookup_args args = {
8097 +               .flags          = flags,
8098 +               .name           = &dentry->d_name
8099 +       };
8100 +       struct dentry *parent;
8101 +       struct super_block *sb;
8102 +
8103 +       sb = dentry->d_sb;
8104 +       err = au_test_shwh(sb, args.name);
8105 +       if (unlikely(err))
8106 +               goto out;
8107 +
8108 +       err = au_wh_name_alloc(&args.whname, args.name);
8109 +       if (unlikely(err))
8110 +               goto out;
8111 +
8112 +       isdir = !!d_is_dir(dentry);
8113 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8114 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8115 +       if (dirren)
8116 +               au_fset_lkup(args.flags, DIRREN);
8117 +
8118 +       npositive = 0;
8119 +       parent = dget_parent(dentry);
8120 +       btail = au_dbtaildir(parent);
8121 +       for (bindex = btop; bindex <= btail; bindex++) {
8122 +               struct dentry *h_parent, *h_dentry;
8123 +               struct inode *h_inode, *h_dir;
8124 +               struct au_branch *br;
8125 +
8126 +               h_dentry = au_h_dptr(dentry, bindex);
8127 +               if (h_dentry) {
8128 +                       if (d_is_positive(h_dentry))
8129 +                               npositive++;
8130 +                       break;
8131 +               }
8132 +               h_parent = au_h_dptr(parent, bindex);
8133 +               if (!h_parent || !d_is_dir(h_parent))
8134 +                       continue;
8135 +
8136 +               if (dirren) {
8137 +                       /* if the inum matches, then use the prepared name */
8138 +                       err = au_dr_lkup_name(&args, bindex);
8139 +                       if (unlikely(err))
8140 +                               goto out_parent;
8141 +               }
8142 +
8143 +               h_dir = d_inode(h_parent);
8144 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8145 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8146 +               inode_unlock_shared(h_dir);
8147 +               err = PTR_ERR(h_dentry);
8148 +               if (IS_ERR(h_dentry))
8149 +                       goto out_parent;
8150 +               if (h_dentry)
8151 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8152 +               if (dirperm1)
8153 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8154 +
8155 +               if (au_dbwh(dentry) == bindex)
8156 +                       break;
8157 +               if (!h_dentry)
8158 +                       continue;
8159 +               if (d_is_negative(h_dentry))
8160 +                       continue;
8161 +               h_inode = d_inode(h_dentry);
8162 +               npositive++;
8163 +               if (!args.type)
8164 +                       args.type = h_inode->i_mode & S_IFMT;
8165 +               if (args.type != S_IFDIR)
8166 +                       break;
8167 +               else if (isdir) {
8168 +                       /* the type of lower may be different */
8169 +                       bdiropq = au_dbdiropq(dentry);
8170 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8171 +                               break;
8172 +               }
8173 +               br = au_sbr(sb, bindex);
8174 +               if (dirren
8175 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8176 +                                          /*add_ent*/NULL)) {
8177 +                       /* prepare next name to lookup */
8178 +                       err = au_dr_lkup(&args, dentry, bindex);
8179 +                       if (unlikely(err))
8180 +                               goto out_parent;
8181 +               }
8182 +       }
8183 +
8184 +       if (npositive) {
8185 +               AuLabel(positive);
8186 +               au_update_dbtop(dentry);
8187 +       }
8188 +       err = npositive;
8189 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8190 +                    && au_dbtop(dentry) < 0)) {
8191 +               err = -EIO;
8192 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8193 +                       dentry, err);
8194 +       }
8195 +
8196 +out_parent:
8197 +       dput(parent);
8198 +       au_kfree_try_rcu(args.whname.name);
8199 +       if (dirren)
8200 +               au_dr_lkup_fin(&args);
8201 +out:
8202 +       return err;
8203 +}
8204 +
8205 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
8206 +                              struct path *ppath)
8207 +{
8208 +       struct dentry *dentry;
8209 +       int wkq_err;
8210 +
8211 +       if (!au_test_h_perm_sio(idmap, d_inode(ppath->dentry), MAY_EXEC))
8212 +               dentry = vfsub_lkup_one(name, ppath);
8213 +       else {
8214 +               struct vfsub_lkup_one_args args = {
8215 +                       .errp   = &dentry,
8216 +                       .name   = name,
8217 +                       .ppath  = ppath
8218 +               };
8219 +
8220 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8221 +               if (unlikely(wkq_err))
8222 +                       dentry = ERR_PTR(wkq_err);
8223 +       }
8224 +
8225 +       return dentry;
8226 +}
8227 +
8228 +/*
8229 + * lookup @dentry on @bindex which should be negative.
8230 + */
8231 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8232 +{
8233 +       int err;
8234 +       struct dentry *parent, *h_dentry;
8235 +       struct au_branch *br;
8236 +       struct mnt_idmap *h_idmap;
8237 +       struct path h_ppath;
8238 +
8239 +       parent = dget_parent(dentry);
8240 +       br = au_sbr(dentry->d_sb, bindex);
8241 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8242 +       h_ppath.mnt = au_br_mnt(br);
8243 +       h_idmap = au_br_idmap(br);
8244 +       if (wh)
8245 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8246 +       else
8247 +               h_dentry = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
8248 +       err = PTR_ERR(h_dentry);
8249 +       if (IS_ERR(h_dentry))
8250 +               goto out;
8251 +       if (unlikely(d_is_positive(h_dentry))) {
8252 +               err = -EIO;
8253 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8254 +               dput(h_dentry);
8255 +               goto out;
8256 +       }
8257 +
8258 +       err = 0;
8259 +       if (bindex < au_dbtop(dentry))
8260 +               au_set_dbtop(dentry, bindex);
8261 +       if (au_dbbot(dentry) < bindex)
8262 +               au_set_dbbot(dentry, bindex);
8263 +       au_set_h_dptr(dentry, bindex, h_dentry);
8264 +
8265 +out:
8266 +       dput(parent);
8267 +       return err;
8268 +}
8269 +
8270 +/* ---------------------------------------------------------------------- */
8271 +
8272 +/* subset of struct inode */
8273 +struct au_iattr {
8274 +       unsigned long           i_ino;
8275 +       /* unsigned int         i_nlink; */
8276 +       kuid_t                  i_uid;
8277 +       kgid_t                  i_gid;
8278 +       u64                     i_version;
8279 +/*
8280 +       loff_t                  i_size;
8281 +       blkcnt_t                i_blocks;
8282 +*/
8283 +       umode_t                 i_mode;
8284 +};
8285 +
8286 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8287 +{
8288 +       ia->i_ino = h_inode->i_ino;
8289 +       /* ia->i_nlink = h_inode->i_nlink; */
8290 +       ia->i_uid = h_inode->i_uid;
8291 +       ia->i_gid = h_inode->i_gid;
8292 +       ia->i_version = inode_query_iversion(h_inode);
8293 +/*
8294 +       ia->i_size = h_inode->i_size;
8295 +       ia->i_blocks = h_inode->i_blocks;
8296 +*/
8297 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8298 +}
8299 +
8300 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8301 +{
8302 +       return ia->i_ino != h_inode->i_ino
8303 +               /* || ia->i_nlink != h_inode->i_nlink */
8304 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8305 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8306 +               || !inode_eq_iversion(h_inode, ia->i_version)
8307 +/*
8308 +               || ia->i_size != h_inode->i_size
8309 +               || ia->i_blocks != h_inode->i_blocks
8310 +*/
8311 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8312 +}
8313 +
8314 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8315 +                             struct au_branch *br)
8316 +{
8317 +       int err;
8318 +       struct au_iattr ia;
8319 +       struct inode *h_inode;
8320 +       struct dentry *h_d;
8321 +       struct super_block *h_sb;
8322 +       struct path h_ppath;
8323 +
8324 +       err = 0;
8325 +       memset(&ia, -1, sizeof(ia));
8326 +       h_sb = h_dentry->d_sb;
8327 +       h_inode = NULL;
8328 +       if (d_is_positive(h_dentry)) {
8329 +               h_inode = d_inode(h_dentry);
8330 +               au_iattr_save(&ia, h_inode);
8331 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8332 +               /* nfs d_revalidate may return 0 for negative dentry */
8333 +               /* fuse d_revalidate always return 0 for negative dentry */
8334 +               goto out;
8335 +
8336 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8337 +       h_ppath.dentry = h_parent;
8338 +       h_ppath.mnt = au_br_mnt(br);
8339 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8340 +       err = PTR_ERR(h_d);
8341 +       if (IS_ERR(h_d))
8342 +               goto out;
8343 +
8344 +       err = 0;
8345 +       if (unlikely(h_d != h_dentry
8346 +                    || d_inode(h_d) != h_inode
8347 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8348 +               err = au_busy_or_stale();
8349 +       dput(h_d);
8350 +
8351 +out:
8352 +       AuTraceErr(err);
8353 +       return err;
8354 +}
8355 +
8356 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8357 +               struct dentry *h_parent, struct au_branch *br)
8358 +{
8359 +       int err;
8360 +
8361 +       err = 0;
8362 +       if (udba == AuOpt_UDBA_REVAL
8363 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8364 +               IMustLock(h_dir);
8365 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8366 +       } else if (udba != AuOpt_UDBA_NONE)
8367 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8368 +
8369 +       return err;
8370 +}
8371 +
8372 +/* ---------------------------------------------------------------------- */
8373 +
8374 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8375 +{
8376 +       int err;
8377 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8378 +       struct au_hdentry tmp, *p, *q;
8379 +       struct au_dinfo *dinfo;
8380 +       struct super_block *sb;
8381 +
8382 +       DiMustWriteLock(dentry);
8383 +
8384 +       sb = dentry->d_sb;
8385 +       dinfo = au_di(dentry);
8386 +       bbot = dinfo->di_bbot;
8387 +       bwh = dinfo->di_bwh;
8388 +       bdiropq = dinfo->di_bdiropq;
8389 +       bindex = dinfo->di_btop;
8390 +       p = au_hdentry(dinfo, bindex);
8391 +       for (; bindex <= bbot; bindex++, p++) {
8392 +               if (!p->hd_dentry)
8393 +                       continue;
8394 +
8395 +               new_bindex = au_br_index(sb, p->hd_id);
8396 +               if (new_bindex == bindex)
8397 +                       continue;
8398 +
8399 +               if (dinfo->di_bwh == bindex)
8400 +                       bwh = new_bindex;
8401 +               if (dinfo->di_bdiropq == bindex)
8402 +                       bdiropq = new_bindex;
8403 +               if (new_bindex < 0) {
8404 +                       au_hdput(p);
8405 +                       p->hd_dentry = NULL;
8406 +                       continue;
8407 +               }
8408 +
8409 +               /* swap two lower dentries, and loop again */
8410 +               q = au_hdentry(dinfo, new_bindex);
8411 +               tmp = *q;
8412 +               *q = *p;
8413 +               *p = tmp;
8414 +               if (tmp.hd_dentry) {
8415 +                       bindex--;
8416 +                       p--;
8417 +               }
8418 +       }
8419 +
8420 +       dinfo->di_bwh = -1;
8421 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8422 +               dinfo->di_bwh = bwh;
8423 +
8424 +       dinfo->di_bdiropq = -1;
8425 +       if (bdiropq >= 0
8426 +           && bdiropq <= au_sbbot(sb)
8427 +           && au_sbr_whable(sb, bdiropq))
8428 +               dinfo->di_bdiropq = bdiropq;
8429 +
8430 +       err = -EIO;
8431 +       dinfo->di_btop = -1;
8432 +       dinfo->di_bbot = -1;
8433 +       bbot = au_dbbot(parent);
8434 +       bindex = 0;
8435 +       p = au_hdentry(dinfo, bindex);
8436 +       for (; bindex <= bbot; bindex++, p++)
8437 +               if (p->hd_dentry) {
8438 +                       dinfo->di_btop = bindex;
8439 +                       break;
8440 +               }
8441 +
8442 +       if (dinfo->di_btop >= 0) {
8443 +               bindex = bbot;
8444 +               p = au_hdentry(dinfo, bindex);
8445 +               for (; bindex >= 0; bindex--, p--)
8446 +                       if (p->hd_dentry) {
8447 +                               dinfo->di_bbot = bindex;
8448 +                               err = 0;
8449 +                               break;
8450 +                       }
8451 +       }
8452 +
8453 +       return err;
8454 +}
8455 +
8456 +static void au_do_hide(struct dentry *dentry)
8457 +{
8458 +       struct inode *inode;
8459 +
8460 +       if (d_really_is_positive(dentry)) {
8461 +               inode = d_inode(dentry);
8462 +               if (!d_is_dir(dentry)) {
8463 +                       if (inode->i_nlink && !d_unhashed(dentry))
8464 +                               drop_nlink(inode);
8465 +               } else {
8466 +                       clear_nlink(inode);
8467 +                       /* stop next lookup */
8468 +                       inode->i_flags |= S_DEAD;
8469 +               }
8470 +               smp_mb(); /* necessary? */
8471 +       }
8472 +       d_drop(dentry);
8473 +}
8474 +
8475 +static int au_hide_children(struct dentry *parent)
8476 +{
8477 +       int err, i, j, ndentry;
8478 +       struct au_dcsub_pages dpages;
8479 +       struct au_dpage *dpage;
8480 +       struct dentry *dentry;
8481 +
8482 +       err = au_dpages_init(&dpages, GFP_NOFS);
8483 +       if (unlikely(err))
8484 +               goto out;
8485 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8486 +       if (unlikely(err))
8487 +               goto out_dpages;
8488 +
8489 +       /* in reverse order */
8490 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8491 +               dpage = dpages.dpages + i;
8492 +               ndentry = dpage->ndentry;
8493 +               for (j = ndentry - 1; j >= 0; j--) {
8494 +                       dentry = dpage->dentries[j];
8495 +                       if (dentry != parent)
8496 +                               au_do_hide(dentry);
8497 +               }
8498 +       }
8499 +
8500 +out_dpages:
8501 +       au_dpages_free(&dpages);
8502 +out:
8503 +       return err;
8504 +}
8505 +
8506 +static void au_hide(struct dentry *dentry)
8507 +{
8508 +       int err;
8509 +
8510 +       AuDbgDentry(dentry);
8511 +       if (d_is_dir(dentry)) {
8512 +               /* shrink_dcache_parent(dentry); */
8513 +               err = au_hide_children(dentry);
8514 +               if (unlikely(err))
8515 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8516 +                               dentry, err);
8517 +       }
8518 +       au_do_hide(dentry);
8519 +}
8520 +
8521 +/*
8522 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8523 + *
8524 + * a dirty branch is added
8525 + * - on the top of layers
8526 + * - in the middle of layers
8527 + * - to the bottom of layers
8528 + *
8529 + * on the added branch there exists
8530 + * - a whiteout
8531 + * - a diropq
8532 + * - a same named entry
8533 + *   + exist
8534 + *     * negative --> positive
8535 + *     * positive --> positive
8536 + *      - type is unchanged
8537 + *      - type is changed
8538 + *   + doesn't exist
8539 + *     * negative --> negative
8540 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8541 + * - none
8542 + */
8543 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8544 +                              struct au_dinfo *tmp)
8545 +{
8546 +       int err;
8547 +       aufs_bindex_t bindex, bbot;
8548 +       struct {
8549 +               struct dentry *dentry;
8550 +               struct inode *inode;
8551 +               mode_t mode;
8552 +       } orig_h, tmp_h = {
8553 +               .dentry = NULL
8554 +       };
8555 +       struct au_hdentry *hd;
8556 +       struct inode *inode, *h_inode;
8557 +       struct dentry *h_dentry;
8558 +
8559 +       err = 0;
8560 +       AuDebugOn(dinfo->di_btop < 0);
8561 +       orig_h.mode = 0;
8562 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8563 +       orig_h.inode = NULL;
8564 +       if (d_is_positive(orig_h.dentry)) {
8565 +               orig_h.inode = d_inode(orig_h.dentry);
8566 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8567 +       }
8568 +       if (tmp->di_btop >= 0) {
8569 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8570 +               if (d_is_positive(tmp_h.dentry)) {
8571 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8572 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8573 +               }
8574 +       }
8575 +
8576 +       inode = NULL;
8577 +       if (d_really_is_positive(dentry))
8578 +               inode = d_inode(dentry);
8579 +       if (!orig_h.inode) {
8580 +               AuDbg("negative originally\n");
8581 +               if (inode) {
8582 +                       au_hide(dentry);
8583 +                       goto out;
8584 +               }
8585 +               AuDebugOn(inode);
8586 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8587 +               AuDebugOn(dinfo->di_bdiropq != -1);
8588 +
8589 +               if (!tmp_h.inode) {
8590 +                       AuDbg("negative --> negative\n");
8591 +                       /* should have only one negative lower */
8592 +                       if (tmp->di_btop >= 0
8593 +                           && tmp->di_btop < dinfo->di_btop) {
8594 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8595 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8596 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8597 +                               au_di_cp(dinfo, tmp);
8598 +                               hd = au_hdentry(tmp, tmp->di_btop);
8599 +                               au_set_h_dptr(dentry, tmp->di_btop,
8600 +                                             dget(hd->hd_dentry));
8601 +                       }
8602 +                       au_dbg_verify_dinode(dentry);
8603 +               } else {
8604 +                       AuDbg("negative --> positive\n");
8605 +                       /*
8606 +                        * similar to the behaviour of creating with bypassing
8607 +                        * aufs.
8608 +                        * unhash it in order to force an error in the
8609 +                        * succeeding create operation.
8610 +                        * we should not set S_DEAD here.
8611 +                        */
8612 +                       d_drop(dentry);
8613 +                       /* au_di_swap(tmp, dinfo); */
8614 +                       au_dbg_verify_dinode(dentry);
8615 +               }
8616 +       } else {
8617 +               AuDbg("positive originally\n");
8618 +               /* inode may be NULL */
8619 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8620 +               if (!tmp_h.inode) {
8621 +                       AuDbg("positive --> negative\n");
8622 +                       /* or bypassing aufs */
8623 +                       au_hide(dentry);
8624 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8625 +                               dinfo->di_bwh = tmp->di_bwh;
8626 +                       if (inode)
8627 +                               err = au_refresh_hinode_self(inode);
8628 +                       au_dbg_verify_dinode(dentry);
8629 +               } else if (orig_h.mode == tmp_h.mode) {
8630 +                       AuDbg("positive --> positive, same type\n");
8631 +                       if (!S_ISDIR(orig_h.mode)
8632 +                           && dinfo->di_btop > tmp->di_btop) {
8633 +                               /*
8634 +                                * similar to the behaviour of removing and
8635 +                                * creating.
8636 +                                */
8637 +                               au_hide(dentry);
8638 +                               if (inode)
8639 +                                       err = au_refresh_hinode_self(inode);
8640 +                               au_dbg_verify_dinode(dentry);
8641 +                       } else {
8642 +                               /* fill empty slots */
8643 +                               if (dinfo->di_btop > tmp->di_btop)
8644 +                                       dinfo->di_btop = tmp->di_btop;
8645 +                               if (dinfo->di_bbot < tmp->di_bbot)
8646 +                                       dinfo->di_bbot = tmp->di_bbot;
8647 +                               dinfo->di_bwh = tmp->di_bwh;
8648 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8649 +                               bbot = dinfo->di_bbot;
8650 +                               bindex = tmp->di_btop;
8651 +                               hd = au_hdentry(tmp, bindex);
8652 +                               for (; bindex <= bbot; bindex++, hd++) {
8653 +                                       if (au_h_dptr(dentry, bindex))
8654 +                                               continue;
8655 +                                       h_dentry = hd->hd_dentry;
8656 +                                       if (!h_dentry)
8657 +                                               continue;
8658 +                                       AuDebugOn(d_is_negative(h_dentry));
8659 +                                       h_inode = d_inode(h_dentry);
8660 +                                       AuDebugOn(orig_h.mode
8661 +                                                 != (h_inode->i_mode
8662 +                                                     & S_IFMT));
8663 +                                       au_set_h_dptr(dentry, bindex,
8664 +                                                     dget(h_dentry));
8665 +                               }
8666 +                               if (inode)
8667 +                                       err = au_refresh_hinode(inode, dentry);
8668 +                               au_dbg_verify_dinode(dentry);
8669 +                       }
8670 +               } else {
8671 +                       AuDbg("positive --> positive, different type\n");
8672 +                       /* similar to the behaviour of removing and creating */
8673 +                       au_hide(dentry);
8674 +                       if (inode)
8675 +                               err = au_refresh_hinode_self(inode);
8676 +                       au_dbg_verify_dinode(dentry);
8677 +               }
8678 +       }
8679 +
8680 +out:
8681 +       return err;
8682 +}
8683 +
8684 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8685 +{
8686 +       const struct dentry_operations *dop
8687 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8688 +       static const unsigned int mask
8689 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8690 +
8691 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8692 +
8693 +       if (dentry->d_op == dop)
8694 +               return;
8695 +
8696 +       AuDbg("%pd\n", dentry);
8697 +       spin_lock(&dentry->d_lock);
8698 +       if (dop == &aufs_dop)
8699 +               dentry->d_flags |= mask;
8700 +       else
8701 +               dentry->d_flags &= ~mask;
8702 +       dentry->d_op = dop;
8703 +       spin_unlock(&dentry->d_lock);
8704 +}
8705 +
8706 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8707 +{
8708 +       int err, ebrange, nbr;
8709 +       unsigned int sigen;
8710 +       struct au_dinfo *dinfo, *tmp;
8711 +       struct super_block *sb;
8712 +       struct inode *inode;
8713 +
8714 +       DiMustWriteLock(dentry);
8715 +       AuDebugOn(IS_ROOT(dentry));
8716 +       AuDebugOn(d_really_is_negative(parent));
8717 +
8718 +       sb = dentry->d_sb;
8719 +       sigen = au_sigen(sb);
8720 +       err = au_digen_test(parent, sigen);
8721 +       if (unlikely(err))
8722 +               goto out;
8723 +
8724 +       nbr = au_sbbot(sb) + 1;
8725 +       dinfo = au_di(dentry);
8726 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8727 +       if (unlikely(err))
8728 +               goto out;
8729 +       ebrange = au_dbrange_test(dentry);
8730 +       if (!ebrange)
8731 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8732 +
8733 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8734 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8735 +               if (d_really_is_positive(dentry)) {
8736 +                       inode = d_inode(dentry);
8737 +                       err = au_refresh_hinode_self(inode);
8738 +               }
8739 +               au_dbg_verify_dinode(dentry);
8740 +               if (!err)
8741 +                       goto out_dgen; /* success */
8742 +               goto out;
8743 +       }
8744 +
8745 +       /* temporary dinfo */
8746 +       AuDbgDentry(dentry);
8747 +       err = -ENOMEM;
8748 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8749 +       if (unlikely(!tmp))
8750 +               goto out;
8751 +       au_di_swap(tmp, dinfo);
8752 +       /* returns the number of positive dentries */
8753 +       /*
8754 +        * if current working dir is removed, it returns an error.
8755 +        * but the dentry is legal.
8756 +        */
8757 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8758 +       AuDbgDentry(dentry);
8759 +       au_di_swap(tmp, dinfo);
8760 +       if (err == -ENOENT)
8761 +               err = 0;
8762 +       if (err >= 0) {
8763 +               /* compare/refresh by dinfo */
8764 +               AuDbgDentry(dentry);
8765 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8766 +               au_dbg_verify_dinode(dentry);
8767 +               AuTraceErr(err);
8768 +       }
8769 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8770 +       au_rw_write_unlock(&tmp->di_rwsem);
8771 +       au_di_free(tmp);
8772 +       if (unlikely(err))
8773 +               goto out;
8774 +
8775 +out_dgen:
8776 +       au_update_digen(dentry);
8777 +out:
8778 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8779 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8780 +               AuDbgDentry(dentry);
8781 +       }
8782 +       AuTraceErr(err);
8783 +       return err;
8784 +}
8785 +
8786 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8787 +                          struct dentry *dentry, aufs_bindex_t bindex)
8788 +{
8789 +       int err, valid;
8790 +
8791 +       err = 0;
8792 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8793 +               goto out;
8794 +
8795 +       AuDbg("b%d\n", bindex);
8796 +       /*
8797 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8798 +        * due to whiteout and branch permission.
8799 +        */
8800 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8801 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8802 +       /* it may return tri-state */
8803 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8804 +
8805 +       if (unlikely(valid < 0))
8806 +               err = valid;
8807 +       else if (!valid)
8808 +               err = -EINVAL;
8809 +
8810 +out:
8811 +       AuTraceErr(err);
8812 +       return err;
8813 +}
8814 +
8815 +/* todo: remove this */
8816 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8817 +                         unsigned int flags, int do_udba, int dirren)
8818 +{
8819 +       int err;
8820 +       umode_t mode, h_mode;
8821 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8822 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8823 +       struct inode *h_inode, *h_cached_inode;
8824 +       struct dentry *h_dentry;
8825 +       struct qstr *name, *h_name;
8826 +
8827 +       err = 0;
8828 +       plus = 0;
8829 +       mode = 0;
8830 +       ibs = -1;
8831 +       ibe = -1;
8832 +       unhashed = !!d_unhashed(dentry);
8833 +       is_root = !!IS_ROOT(dentry);
8834 +       name = &dentry->d_name;
8835 +       tmpfile = au_di(dentry)->di_tmpfile;
8836 +
8837 +       /*
8838 +        * Theoretically, REVAL test should be unnecessary in case of
8839 +        * {FS,I}NOTIFY.
8840 +        * But {fs,i}notify doesn't fire some necessary events,
8841 +        *      IN_ATTRIB for atime/nlink/pageio
8842 +        * Let's do REVAL test too.
8843 +        */
8844 +       if (do_udba && inode) {
8845 +               mode = (inode->i_mode & S_IFMT);
8846 +               plus = (inode->i_nlink > 0);
8847 +               ibs = au_ibtop(inode);
8848 +               ibe = au_ibbot(inode);
8849 +       }
8850 +
8851 +       btop = au_dbtop(dentry);
8852 +       btail = btop;
8853 +       if (inode && S_ISDIR(inode->i_mode))
8854 +               btail = au_dbtaildir(dentry);
8855 +       for (bindex = btop; bindex <= btail; bindex++) {
8856 +               h_dentry = au_h_dptr(dentry, bindex);
8857 +               if (!h_dentry)
8858 +                       continue;
8859 +
8860 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8861 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8862 +               spin_lock(&h_dentry->d_lock);
8863 +               h_name = &h_dentry->d_name;
8864 +               if (unlikely(do_udba
8865 +                            && !is_root
8866 +                            && ((!h_nfs
8867 +                                 && (unhashed != !!d_unhashed(h_dentry)
8868 +                                     || (!tmpfile && !dirren
8869 +                                         && !au_qstreq(name, h_name))
8870 +                                         ))
8871 +                                || (h_nfs
8872 +                                    && !(flags & LOOKUP_OPEN)
8873 +                                    && (h_dentry->d_flags
8874 +                                        & DCACHE_NFSFS_RENAMED)))
8875 +                           )) {
8876 +                       int h_unhashed;
8877 +
8878 +                       h_unhashed = d_unhashed(h_dentry);
8879 +                       spin_unlock(&h_dentry->d_lock);
8880 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8881 +                             unhashed, h_unhashed, dentry, h_dentry);
8882 +                       goto err;
8883 +               }
8884 +               spin_unlock(&h_dentry->d_lock);
8885 +
8886 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8887 +               if (unlikely(err))
8888 +                       /* do not goto err, to keep the errno */
8889 +                       break;
8890 +
8891 +               /* todo: plink too? */
8892 +               if (!do_udba)
8893 +                       continue;
8894 +
8895 +               /* UDBA tests */
8896 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8897 +                       goto err;
8898 +
8899 +               h_inode = NULL;
8900 +               if (d_is_positive(h_dentry))
8901 +                       h_inode = d_inode(h_dentry);
8902 +               h_plus = plus;
8903 +               h_mode = mode;
8904 +               h_cached_inode = h_inode;
8905 +               if (h_inode) {
8906 +                       h_mode = (h_inode->i_mode & S_IFMT);
8907 +                       h_plus = (h_inode->i_nlink > 0);
8908 +               }
8909 +               if (inode && ibs <= bindex && bindex <= ibe)
8910 +                       h_cached_inode = au_h_iptr(inode, bindex);
8911 +
8912 +               if (!h_nfs) {
8913 +                       if (unlikely(plus != h_plus && !tmpfile))
8914 +                               goto err;
8915 +               } else {
8916 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8917 +                                    && !is_root
8918 +                                    && !IS_ROOT(h_dentry)
8919 +                                    && unhashed != d_unhashed(h_dentry)))
8920 +                               goto err;
8921 +               }
8922 +               if (unlikely(mode != h_mode
8923 +                            || h_cached_inode != h_inode))
8924 +                       goto err;
8925 +               continue;
8926 +
8927 +err:
8928 +               err = -EINVAL;
8929 +               break;
8930 +       }
8931 +
8932 +       AuTraceErr(err);
8933 +       return err;
8934 +}
8935 +
8936 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8937 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8938 +{
8939 +       int err;
8940 +       struct dentry *parent;
8941 +
8942 +       if (!au_digen_test(dentry, sigen))
8943 +               return 0;
8944 +
8945 +       parent = dget_parent(dentry);
8946 +       di_read_lock_parent(parent, AuLock_IR);
8947 +       AuDebugOn(au_digen_test(parent, sigen));
8948 +       au_dbg_verify_gen(parent, sigen);
8949 +       err = au_refresh_dentry(dentry, parent);
8950 +       di_read_unlock(parent, AuLock_IR);
8951 +       dput(parent);
8952 +       AuTraceErr(err);
8953 +       return err;
8954 +}
8955 +
8956 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8957 +{
8958 +       int err;
8959 +       struct dentry *d, *parent;
8960 +
8961 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8962 +               return simple_reval_dpath(dentry, sigen);
8963 +
8964 +       /* slow loop, keep it simple and stupid */
8965 +       /* cf: au_cpup_dirs() */
8966 +       err = 0;
8967 +       parent = NULL;
8968 +       while (au_digen_test(dentry, sigen)) {
8969 +               d = dentry;
8970 +               while (1) {
8971 +                       dput(parent);
8972 +                       parent = dget_parent(d);
8973 +                       if (!au_digen_test(parent, sigen))
8974 +                               break;
8975 +                       d = parent;
8976 +               }
8977 +
8978 +               if (d != dentry)
8979 +                       di_write_lock_child2(d);
8980 +
8981 +               /* someone might update our dentry while we were sleeping */
8982 +               if (au_digen_test(d, sigen)) {
8983 +                       /*
8984 +                        * todo: consolidate with simple_reval_dpath(),
8985 +                        * do_refresh() and au_reval_for_attr().
8986 +                        */
8987 +                       di_read_lock_parent(parent, AuLock_IR);
8988 +                       err = au_refresh_dentry(d, parent);
8989 +                       di_read_unlock(parent, AuLock_IR);
8990 +               }
8991 +
8992 +               if (d != dentry)
8993 +                       di_write_unlock(d);
8994 +               dput(parent);
8995 +               if (unlikely(err))
8996 +                       break;
8997 +       }
8998 +
8999 +       return err;
9000 +}
9001 +
9002 +/*
9003 + * if valid returns 1, otherwise 0.
9004 + */
9005 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9006 +{
9007 +       int valid, err;
9008 +       unsigned int sigen;
9009 +       unsigned char do_udba, dirren;
9010 +       struct super_block *sb;
9011 +       struct inode *inode;
9012 +
9013 +       /* todo: support rcu-walk? */
9014 +       if (flags & LOOKUP_RCU)
9015 +               return -ECHILD;
9016 +
9017 +       valid = 0;
9018 +       if (unlikely(!au_di(dentry)))
9019 +               goto out;
9020 +
9021 +       valid = 1;
9022 +       sb = dentry->d_sb;
9023 +       /*
9024 +        * todo: very ugly
9025 +        * i_mutex of parent dir may be held,
9026 +        * but we should not return 'invalid' due to busy.
9027 +        */
9028 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9029 +       if (unlikely(err)) {
9030 +               valid = err;
9031 +               AuTraceErr(err);
9032 +               goto out;
9033 +       }
9034 +       inode = NULL;
9035 +       if (d_really_is_positive(dentry))
9036 +               inode = d_inode(dentry);
9037 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9038 +               err = -EINVAL;
9039 +               AuTraceErr(err);
9040 +               goto out_dgrade;
9041 +       }
9042 +       if (unlikely(au_dbrange_test(dentry))) {
9043 +               err = -EINVAL;
9044 +               AuTraceErr(err);
9045 +               goto out_dgrade;
9046 +       }
9047 +
9048 +       sigen = au_sigen(sb);
9049 +       if (au_digen_test(dentry, sigen)) {
9050 +               AuDebugOn(IS_ROOT(dentry));
9051 +               err = au_reval_dpath(dentry, sigen);
9052 +               if (unlikely(err)) {
9053 +                       AuTraceErr(err);
9054 +                       goto out_dgrade;
9055 +               }
9056 +       }
9057 +       di_downgrade_lock(dentry, AuLock_IR);
9058 +
9059 +       err = -EINVAL;
9060 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9061 +           && inode
9062 +           && !(inode->i_state && I_LINKABLE)
9063 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9064 +               AuTraceErr(err);
9065 +               goto out_inval;
9066 +       }
9067 +
9068 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9069 +       if (do_udba && inode) {
9070 +               aufs_bindex_t btop = au_ibtop(inode);
9071 +               struct inode *h_inode;
9072 +
9073 +               if (btop >= 0) {
9074 +                       h_inode = au_h_iptr(inode, btop);
9075 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9076 +                               AuTraceErr(err);
9077 +                               goto out_inval;
9078 +                       }
9079 +               }
9080 +       }
9081 +
9082 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9083 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9084 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9085 +               err = -EIO;
9086 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9087 +                     dentry, err);
9088 +       }
9089 +       goto out_inval;
9090 +
9091 +out_dgrade:
9092 +       di_downgrade_lock(dentry, AuLock_IR);
9093 +out_inval:
9094 +       aufs_read_unlock(dentry, AuLock_IR);
9095 +       AuTraceErr(err);
9096 +       valid = !err;
9097 +out:
9098 +       if (!valid) {
9099 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9100 +               d_drop(dentry);
9101 +       }
9102 +       return valid;
9103 +}
9104 +
9105 +static void aufs_d_release(struct dentry *dentry)
9106 +{
9107 +       if (au_di(dentry)) {
9108 +               au_di_fin(dentry);
9109 +               au_hn_di_reinit(dentry);
9110 +       }
9111 +}
9112 +
9113 +const struct dentry_operations aufs_dop = {
9114 +       .d_revalidate           = aufs_d_revalidate,
9115 +       .d_weak_revalidate      = aufs_d_revalidate,
9116 +       .d_release              = aufs_d_release
9117 +};
9118 +
9119 +/* aufs_dop without d_revalidate */
9120 +const struct dentry_operations aufs_dop_noreval = {
9121 +       .d_release              = aufs_d_release
9122 +};
9123 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9124 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9125 +++ linux/fs/aufs/dentry.h      2024-03-10 23:40:47.083143450 +0100
9126 @@ -0,0 +1,270 @@
9127 +/* SPDX-License-Identifier: GPL-2.0 */
9128 +/*
9129 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9130 + *
9131 + * This program is free software; you can redistribute it and/or modify
9132 + * it under the terms of the GNU General Public License as published by
9133 + * the Free Software Foundation; either version 2 of the License, or
9134 + * (at your option) any later version.
9135 + *
9136 + * This program is distributed in the hope that it will be useful,
9137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9139 + * GNU General Public License for more details.
9140 + *
9141 + * You should have received a copy of the GNU General Public License
9142 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9143 + */
9144 +
9145 +/*
9146 + * lookup and dentry operations
9147 + */
9148 +
9149 +#ifndef __AUFS_DENTRY_H__
9150 +#define __AUFS_DENTRY_H__
9151 +
9152 +#ifdef __KERNEL__
9153 +
9154 +#include <linux/dcache.h>
9155 +#include "dirren.h"
9156 +#include "rwsem.h"
9157 +
9158 +struct au_hdentry {
9159 +       struct dentry           *hd_dentry;
9160 +       aufs_bindex_t           hd_id;
9161 +};
9162 +
9163 +struct au_dinfo {
9164 +       atomic_t                di_generation;
9165 +
9166 +       struct au_rwsem         di_rwsem;
9167 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9168 +       unsigned char           di_tmpfile; /* to allow the different name */
9169 +       struct au_hdentry       *di_hdentry;
9170 +       struct file             *di_htmpfile;
9171 +       struct rcu_head         rcu;
9172 +} ____cacheline_aligned_in_smp;
9173 +
9174 +/* ---------------------------------------------------------------------- */
9175 +
9176 +/* flags for au_lkup_dentry() */
9177 +#define AuLkup_ALLOW_NEG       BIT(0)
9178 +#define AuLkup_IGNORE_PERM     BIT(1)
9179 +#define AuLkup_DIRREN          BIT(2)
9180 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9181 +#define au_fset_lkup(flags, name) \
9182 +       do { (flags) |= AuLkup_##name; } while (0)
9183 +#define au_fclr_lkup(flags, name) \
9184 +       do { (flags) &= ~AuLkup_##name; } while (0)
9185 +
9186 +#ifndef CONFIG_AUFS_DIRREN
9187 +#undef AuLkup_DIRREN
9188 +#define AuLkup_DIRREN 0
9189 +#endif
9190 +
9191 +struct au_do_lookup_args {
9192 +       unsigned int            flags;
9193 +       mode_t                  type;
9194 +       struct qstr             whname, *name;
9195 +       struct au_dr_lookup     dirren;
9196 +};
9197 +
9198 +/* ---------------------------------------------------------------------- */
9199 +
9200 +/* dentry.c */
9201 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9202 +struct au_branch;
9203 +struct dentry *au_sio_lkup_one(struct mnt_idmap *idmap, struct qstr *name,
9204 +                              struct path *ppath);
9205 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9206 +               struct dentry *h_parent, struct au_branch *br);
9207 +
9208 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9209 +                  unsigned int flags);
9210 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9211 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9212 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9213 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9214 +
9215 +/* dinfo.c */
9216 +void au_di_init_once(void *_di);
9217 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9218 +void au_di_free(struct au_dinfo *dinfo);
9219 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9220 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9221 +int au_di_init(struct dentry *dentry);
9222 +void au_di_fin(struct dentry *dentry);
9223 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9224 +
9225 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9226 +void di_read_unlock(struct dentry *d, int flags);
9227 +void di_downgrade_lock(struct dentry *d, int flags);
9228 +void di_write_lock(struct dentry *d, unsigned int lsc);
9229 +void di_write_unlock(struct dentry *d);
9230 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9231 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9232 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9233 +
9234 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9235 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9236 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9237 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9238 +
9239 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9240 +                  struct dentry *h_dentry);
9241 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9242 +int au_dbrange_test(struct dentry *dentry);
9243 +void au_update_digen(struct dentry *dentry);
9244 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9245 +void au_update_dbtop(struct dentry *dentry);
9246 +void au_update_dbbot(struct dentry *dentry);
9247 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9248 +
9249 +/* ---------------------------------------------------------------------- */
9250 +
9251 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9252 +{
9253 +       return dentry->d_fsdata;
9254 +}
9255 +
9256 +/* ---------------------------------------------------------------------- */
9257 +
9258 +/* lock subclass for dinfo */
9259 +enum {
9260 +       AuLsc_DI_CHILD,         /* child first */
9261 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9262 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9263 +       AuLsc_DI_PARENT,
9264 +       AuLsc_DI_PARENT2,
9265 +       AuLsc_DI_PARENT3,
9266 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9267 +};
9268 +
9269 +/*
9270 + * di_read_lock_child, di_write_lock_child,
9271 + * di_read_lock_child2, di_write_lock_child2,
9272 + * di_read_lock_child3, di_write_lock_child3,
9273 + * di_read_lock_parent, di_write_lock_parent,
9274 + * di_read_lock_parent2, di_write_lock_parent2,
9275 + * di_read_lock_parent3, di_write_lock_parent3,
9276 + */
9277 +#define AuReadLockFunc(name, lsc) \
9278 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9279 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9280 +
9281 +#define AuWriteLockFunc(name, lsc) \
9282 +static inline void di_write_lock_##name(struct dentry *d) \
9283 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9284 +
9285 +#define AuRWLockFuncs(name, lsc) \
9286 +       AuReadLockFunc(name, lsc) \
9287 +       AuWriteLockFunc(name, lsc)
9288 +
9289 +AuRWLockFuncs(child, CHILD);
9290 +AuRWLockFuncs(child2, CHILD2);
9291 +AuRWLockFuncs(child3, CHILD3);
9292 +AuRWLockFuncs(parent, PARENT);
9293 +AuRWLockFuncs(parent2, PARENT2);
9294 +AuRWLockFuncs(parent3, PARENT3);
9295 +
9296 +#undef AuReadLockFunc
9297 +#undef AuWriteLockFunc
9298 +#undef AuRWLockFuncs
9299 +
9300 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9301 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9302 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9303 +
9304 +/* ---------------------------------------------------------------------- */
9305 +
9306 +/* todo: memory barrier? */
9307 +static inline unsigned int au_digen(struct dentry *d)
9308 +{
9309 +       return atomic_read(&au_di(d)->di_generation);
9310 +}
9311 +
9312 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9313 +{
9314 +       hdentry->hd_dentry = NULL;
9315 +}
9316 +
9317 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9318 +                                           aufs_bindex_t bindex)
9319 +{
9320 +       return di->di_hdentry + bindex;
9321 +}
9322 +
9323 +static inline void au_hdput(struct au_hdentry *hd)
9324 +{
9325 +       if (hd)
9326 +               dput(hd->hd_dentry);
9327 +}
9328 +
9329 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9330 +{
9331 +       DiMustAnyLock(dentry);
9332 +       return au_di(dentry)->di_btop;
9333 +}
9334 +
9335 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9336 +{
9337 +       DiMustAnyLock(dentry);
9338 +       return au_di(dentry)->di_bbot;
9339 +}
9340 +
9341 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9342 +{
9343 +       DiMustAnyLock(dentry);
9344 +       return au_di(dentry)->di_bwh;
9345 +}
9346 +
9347 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9348 +{
9349 +       DiMustAnyLock(dentry);
9350 +       return au_di(dentry)->di_bdiropq;
9351 +}
9352 +
9353 +/* todo: hard/soft set? */
9354 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9355 +{
9356 +       DiMustWriteLock(dentry);
9357 +       au_di(dentry)->di_btop = bindex;
9358 +}
9359 +
9360 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9361 +{
9362 +       DiMustWriteLock(dentry);
9363 +       au_di(dentry)->di_bbot = bindex;
9364 +}
9365 +
9366 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9367 +{
9368 +       DiMustWriteLock(dentry);
9369 +       /* dbwh can be outside of btop - bbot range */
9370 +       au_di(dentry)->di_bwh = bindex;
9371 +}
9372 +
9373 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9374 +{
9375 +       DiMustWriteLock(dentry);
9376 +       au_di(dentry)->di_bdiropq = bindex;
9377 +}
9378 +
9379 +/* ---------------------------------------------------------------------- */
9380 +
9381 +#ifdef CONFIG_AUFS_HNOTIFY
9382 +static inline void au_digen_dec(struct dentry *d)
9383 +{
9384 +       atomic_dec(&au_di(d)->di_generation);
9385 +}
9386 +
9387 +static inline void au_hn_di_reinit(struct dentry *dentry)
9388 +{
9389 +       dentry->d_fsdata = NULL;
9390 +}
9391 +#else
9392 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9393 +#endif /* CONFIG_AUFS_HNOTIFY */
9394 +
9395 +#endif /* __KERNEL__ */
9396 +#endif /* __AUFS_DENTRY_H__ */
9397 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9398 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9399 +++ linux/fs/aufs/dinfo.c       2022-12-17 09:21:34.796521861 +0100
9400 @@ -0,0 +1,555 @@
9401 +// SPDX-License-Identifier: GPL-2.0
9402 +/*
9403 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9404 + *
9405 + * This program is free software; you can redistribute it and/or modify
9406 + * it under the terms of the GNU General Public License as published by
9407 + * the Free Software Foundation; either version 2 of the License, or
9408 + * (at your option) any later version.
9409 + *
9410 + * This program is distributed in the hope that it will be useful,
9411 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9412 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9413 + * GNU General Public License for more details.
9414 + *
9415 + * You should have received a copy of the GNU General Public License
9416 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9417 + */
9418 +
9419 +/*
9420 + * dentry private data
9421 + */
9422 +
9423 +#include "aufs.h"
9424 +
9425 +void au_di_init_once(void *_dinfo)
9426 +{
9427 +       struct au_dinfo *dinfo = _dinfo;
9428 +
9429 +       au_rw_init(&dinfo->di_rwsem);
9430 +}
9431 +
9432 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9433 +{
9434 +       struct au_dinfo *dinfo;
9435 +       int nbr, i;
9436 +
9437 +       dinfo = au_cache_alloc_dinfo();
9438 +       if (unlikely(!dinfo))
9439 +               goto out;
9440 +
9441 +       nbr = au_sbbot(sb) + 1;
9442 +       if (nbr <= 0)
9443 +               nbr = 1;
9444 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9445 +       if (dinfo->di_hdentry) {
9446 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9447 +               dinfo->di_btop = -1;
9448 +               dinfo->di_bbot = -1;
9449 +               dinfo->di_bwh = -1;
9450 +               dinfo->di_bdiropq = -1;
9451 +               dinfo->di_tmpfile = 0;
9452 +               for (i = 0; i < nbr; i++)
9453 +                       dinfo->di_hdentry[i].hd_id = -1;
9454 +               dinfo->di_htmpfile = NULL;
9455 +               goto out;
9456 +       }
9457 +
9458 +       au_cache_free_dinfo(dinfo);
9459 +       dinfo = NULL;
9460 +
9461 +out:
9462 +       return dinfo;
9463 +}
9464 +
9465 +void au_di_free(struct au_dinfo *dinfo)
9466 +{
9467 +       struct au_hdentry *p;
9468 +       aufs_bindex_t bbot, bindex;
9469 +
9470 +       /* dentry may not be revalidated */
9471 +       bindex = dinfo->di_btop;
9472 +       if (bindex >= 0) {
9473 +               bbot = dinfo->di_bbot;
9474 +               p = au_hdentry(dinfo, bindex);
9475 +               while (bindex++ <= bbot)
9476 +                       au_hdput(p++);
9477 +       }
9478 +       au_kfree_try_rcu(dinfo->di_hdentry);
9479 +       au_cache_free_dinfo(dinfo);
9480 +}
9481 +
9482 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9483 +{
9484 +       struct au_hdentry *p;
9485 +       aufs_bindex_t bi;
9486 +
9487 +       AuRwMustWriteLock(&a->di_rwsem);
9488 +       AuRwMustWriteLock(&b->di_rwsem);
9489 +
9490 +#define DiSwap(v, name)                                \
9491 +       do {                                    \
9492 +               v = a->di_##name;               \
9493 +               a->di_##name = b->di_##name;    \
9494 +               b->di_##name = v;               \
9495 +       } while (0)
9496 +
9497 +       DiSwap(p, hdentry);
9498 +       DiSwap(bi, btop);
9499 +       DiSwap(bi, bbot);
9500 +       DiSwap(bi, bwh);
9501 +       DiSwap(bi, bdiropq);
9502 +       /* smp_mb(); */
9503 +
9504 +#undef DiSwap
9505 +}
9506 +
9507 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9508 +{
9509 +       AuRwMustWriteLock(&dst->di_rwsem);
9510 +       AuRwMustWriteLock(&src->di_rwsem);
9511 +
9512 +       dst->di_btop = src->di_btop;
9513 +       dst->di_bbot = src->di_bbot;
9514 +       dst->di_bwh = src->di_bwh;
9515 +       dst->di_bdiropq = src->di_bdiropq;
9516 +       /* smp_mb(); */
9517 +}
9518 +
9519 +int au_di_init(struct dentry *dentry)
9520 +{
9521 +       int err;
9522 +       struct super_block *sb;
9523 +       struct au_dinfo *dinfo;
9524 +
9525 +       err = 0;
9526 +       sb = dentry->d_sb;
9527 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9528 +       if (dinfo) {
9529 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9530 +               /* smp_mb(); */ /* atomic_set */
9531 +               dentry->d_fsdata = dinfo;
9532 +       } else
9533 +               err = -ENOMEM;
9534 +
9535 +       return err;
9536 +}
9537 +
9538 +void au_di_fin(struct dentry *dentry)
9539 +{
9540 +       struct au_dinfo *dinfo;
9541 +
9542 +       dinfo = au_di(dentry);
9543 +       AuRwDestroy(&dinfo->di_rwsem);
9544 +       au_di_free(dinfo);
9545 +}
9546 +
9547 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9548 +{
9549 +       int err, sz;
9550 +       struct au_hdentry *hdp;
9551 +
9552 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9553 +
9554 +       err = -ENOMEM;
9555 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9556 +       if (!sz)
9557 +               sz = sizeof(*hdp);
9558 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9559 +                          may_shrink);
9560 +       if (hdp) {
9561 +               dinfo->di_hdentry = hdp;
9562 +               err = 0;
9563 +       }
9564 +
9565 +       return err;
9566 +}
9567 +
9568 +/* ---------------------------------------------------------------------- */
9569 +
9570 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9571 +{
9572 +       switch (lsc) {
9573 +       case AuLsc_DI_CHILD:
9574 +               ii_write_lock_child(inode);
9575 +               break;
9576 +       case AuLsc_DI_CHILD2:
9577 +               ii_write_lock_child2(inode);
9578 +               break;
9579 +       case AuLsc_DI_CHILD3:
9580 +               ii_write_lock_child3(inode);
9581 +               break;
9582 +       case AuLsc_DI_PARENT:
9583 +               ii_write_lock_parent(inode);
9584 +               break;
9585 +       case AuLsc_DI_PARENT2:
9586 +               ii_write_lock_parent2(inode);
9587 +               break;
9588 +       case AuLsc_DI_PARENT3:
9589 +               ii_write_lock_parent3(inode);
9590 +               break;
9591 +       default:
9592 +               BUG();
9593 +       }
9594 +}
9595 +
9596 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9597 +{
9598 +       switch (lsc) {
9599 +       case AuLsc_DI_CHILD:
9600 +               ii_read_lock_child(inode);
9601 +               break;
9602 +       case AuLsc_DI_CHILD2:
9603 +               ii_read_lock_child2(inode);
9604 +               break;
9605 +       case AuLsc_DI_CHILD3:
9606 +               ii_read_lock_child3(inode);
9607 +               break;
9608 +       case AuLsc_DI_PARENT:
9609 +               ii_read_lock_parent(inode);
9610 +               break;
9611 +       case AuLsc_DI_PARENT2:
9612 +               ii_read_lock_parent2(inode);
9613 +               break;
9614 +       case AuLsc_DI_PARENT3:
9615 +               ii_read_lock_parent3(inode);
9616 +               break;
9617 +       default:
9618 +               BUG();
9619 +       }
9620 +}
9621 +
9622 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9623 +{
9624 +       struct inode *inode;
9625 +
9626 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9627 +       if (d_really_is_positive(d)) {
9628 +               inode = d_inode(d);
9629 +               if (au_ftest_lock(flags, IW))
9630 +                       do_ii_write_lock(inode, lsc);
9631 +               else if (au_ftest_lock(flags, IR))
9632 +                       do_ii_read_lock(inode, lsc);
9633 +       }
9634 +}
9635 +
9636 +void di_read_unlock(struct dentry *d, int flags)
9637 +{
9638 +       struct inode *inode;
9639 +
9640 +       if (d_really_is_positive(d)) {
9641 +               inode = d_inode(d);
9642 +               if (au_ftest_lock(flags, IW)) {
9643 +                       au_dbg_verify_dinode(d);
9644 +                       ii_write_unlock(inode);
9645 +               } else if (au_ftest_lock(flags, IR)) {
9646 +                       au_dbg_verify_dinode(d);
9647 +                       ii_read_unlock(inode);
9648 +               }
9649 +       }
9650 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9651 +}
9652 +
9653 +void di_downgrade_lock(struct dentry *d, int flags)
9654 +{
9655 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9656 +               ii_downgrade_lock(d_inode(d));
9657 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9658 +}
9659 +
9660 +void di_write_lock(struct dentry *d, unsigned int lsc)
9661 +{
9662 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9663 +       if (d_really_is_positive(d))
9664 +               do_ii_write_lock(d_inode(d), lsc);
9665 +}
9666 +
9667 +void di_write_unlock(struct dentry *d)
9668 +{
9669 +       au_dbg_verify_dinode(d);
9670 +       if (d_really_is_positive(d))
9671 +               ii_write_unlock(d_inode(d));
9672 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9673 +}
9674 +
9675 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9676 +{
9677 +       AuDebugOn(d1 == d2
9678 +                 || d_inode(d1) == d_inode(d2)
9679 +                 || d1->d_sb != d2->d_sb);
9680 +
9681 +       if ((isdir && au_test_subdir(d1, d2))
9682 +           || d1 < d2) {
9683 +               di_write_lock_child(d1);
9684 +               di_write_lock_child2(d2);
9685 +       } else {
9686 +               di_write_lock_child(d2);
9687 +               di_write_lock_child2(d1);
9688 +       }
9689 +}
9690 +
9691 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9692 +{
9693 +       AuDebugOn(d1 == d2
9694 +                 || d_inode(d1) == d_inode(d2)
9695 +                 || d1->d_sb != d2->d_sb);
9696 +
9697 +       if ((isdir && au_test_subdir(d1, d2))
9698 +           || d1 < d2) {
9699 +               di_write_lock_parent(d1);
9700 +               di_write_lock_parent2(d2);
9701 +       } else {
9702 +               di_write_lock_parent(d2);
9703 +               di_write_lock_parent2(d1);
9704 +       }
9705 +}
9706 +
9707 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9708 +{
9709 +       di_write_unlock(d1);
9710 +       if (d_inode(d1) == d_inode(d2))
9711 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9712 +       else
9713 +               di_write_unlock(d2);
9714 +}
9715 +
9716 +/* ---------------------------------------------------------------------- */
9717 +
9718 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9719 +{
9720 +       struct dentry *d;
9721 +
9722 +       DiMustAnyLock(dentry);
9723 +
9724 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9725 +               return NULL;
9726 +       AuDebugOn(bindex < 0);
9727 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9728 +       AuDebugOn(d && au_dcount(d) <= 0);
9729 +       return d;
9730 +}
9731 +
9732 +/*
9733 + * extended version of au_h_dptr().
9734 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9735 + * error.
9736 + */
9737 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9738 +{
9739 +       struct dentry *h_dentry;
9740 +       struct inode *inode, *h_inode;
9741 +
9742 +       AuDebugOn(d_really_is_negative(dentry));
9743 +
9744 +       h_dentry = NULL;
9745 +       if (au_dbtop(dentry) <= bindex
9746 +           && bindex <= au_dbbot(dentry))
9747 +               h_dentry = au_h_dptr(dentry, bindex);
9748 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9749 +               dget(h_dentry);
9750 +               goto out; /* success */
9751 +       }
9752 +
9753 +       inode = d_inode(dentry);
9754 +       AuDebugOn(bindex < au_ibtop(inode));
9755 +       AuDebugOn(au_ibbot(inode) < bindex);
9756 +       h_inode = au_h_iptr(inode, bindex);
9757 +       h_dentry = d_find_alias(h_inode);
9758 +       if (h_dentry) {
9759 +               if (!IS_ERR(h_dentry)) {
9760 +                       if (!au_d_linkable(h_dentry))
9761 +                               goto out; /* success */
9762 +                       dput(h_dentry);
9763 +               } else
9764 +                       goto out;
9765 +       }
9766 +
9767 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9768 +               h_dentry = au_plink_lkup(inode, bindex);
9769 +               AuDebugOn(!h_dentry);
9770 +               if (!IS_ERR(h_dentry)) {
9771 +                       if (!au_d_hashed_positive(h_dentry))
9772 +                               goto out; /* success */
9773 +                       dput(h_dentry);
9774 +                       h_dentry = NULL;
9775 +               }
9776 +       }
9777 +
9778 +out:
9779 +       AuDbgDentry(h_dentry);
9780 +       return h_dentry;
9781 +}
9782 +
9783 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9784 +{
9785 +       aufs_bindex_t bbot, bwh;
9786 +
9787 +       bbot = au_dbbot(dentry);
9788 +       if (0 <= bbot) {
9789 +               bwh = au_dbwh(dentry);
9790 +               if (!bwh)
9791 +                       return bwh;
9792 +               if (0 < bwh && bwh < bbot)
9793 +                       return bwh - 1;
9794 +       }
9795 +       return bbot;
9796 +}
9797 +
9798 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9799 +{
9800 +       aufs_bindex_t bbot, bopq;
9801 +
9802 +       bbot = au_dbtail(dentry);
9803 +       if (0 <= bbot) {
9804 +               bopq = au_dbdiropq(dentry);
9805 +               if (0 <= bopq && bopq < bbot)
9806 +                       bbot = bopq;
9807 +       }
9808 +       return bbot;
9809 +}
9810 +
9811 +/* ---------------------------------------------------------------------- */
9812 +
9813 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9814 +                  struct dentry *h_dentry)
9815 +{
9816 +       struct au_dinfo *dinfo;
9817 +       struct au_hdentry *hd;
9818 +       struct au_branch *br;
9819 +
9820 +       DiMustWriteLock(dentry);
9821 +
9822 +       dinfo = au_di(dentry);
9823 +       hd = au_hdentry(dinfo, bindex);
9824 +       au_hdput(hd);
9825 +       hd->hd_dentry = h_dentry;
9826 +       if (h_dentry) {
9827 +               br = au_sbr(dentry->d_sb, bindex);
9828 +               hd->hd_id = br->br_id;
9829 +       }
9830 +}
9831 +
9832 +int au_dbrange_test(struct dentry *dentry)
9833 +{
9834 +       int err;
9835 +       aufs_bindex_t btop, bbot;
9836 +
9837 +       err = 0;
9838 +       btop = au_dbtop(dentry);
9839 +       bbot = au_dbbot(dentry);
9840 +       if (btop >= 0)
9841 +               AuDebugOn(bbot < 0 && btop > bbot);
9842 +       else {
9843 +               err = -EIO;
9844 +               AuDebugOn(bbot >= 0);
9845 +       }
9846 +
9847 +       return err;
9848 +}
9849 +
9850 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9851 +{
9852 +       int err;
9853 +
9854 +       err = 0;
9855 +       if (unlikely(au_digen(dentry) != sigen
9856 +                    || au_iigen_test(d_inode(dentry), sigen)))
9857 +               err = -EIO;
9858 +
9859 +       return err;
9860 +}
9861 +
9862 +void au_update_digen(struct dentry *dentry)
9863 +{
9864 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9865 +       /* smp_mb(); */ /* atomic_set */
9866 +}
9867 +
9868 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9869 +{
9870 +       struct au_dinfo *dinfo;
9871 +       struct dentry *h_d;
9872 +       struct au_hdentry *hdp;
9873 +       aufs_bindex_t bindex, bbot;
9874 +
9875 +       DiMustWriteLock(dentry);
9876 +
9877 +       dinfo = au_di(dentry);
9878 +       if (!dinfo || dinfo->di_btop < 0)
9879 +               return;
9880 +
9881 +       if (do_put_zero) {
9882 +               bbot = dinfo->di_bbot;
9883 +               bindex = dinfo->di_btop;
9884 +               hdp = au_hdentry(dinfo, bindex);
9885 +               for (; bindex <= bbot; bindex++, hdp++) {
9886 +                       h_d = hdp->hd_dentry;
9887 +                       if (h_d && d_is_negative(h_d))
9888 +                               au_set_h_dptr(dentry, bindex, NULL);
9889 +               }
9890 +       }
9891 +
9892 +       dinfo->di_btop = 0;
9893 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9894 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9895 +               if (hdp->hd_dentry)
9896 +                       break;
9897 +       if (dinfo->di_btop > dinfo->di_bbot) {
9898 +               dinfo->di_btop = -1;
9899 +               dinfo->di_bbot = -1;
9900 +               return;
9901 +       }
9902 +
9903 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9904 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9905 +               if (hdp->hd_dentry)
9906 +                       break;
9907 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9908 +}
9909 +
9910 +void au_update_dbtop(struct dentry *dentry)
9911 +{
9912 +       aufs_bindex_t bindex, bbot;
9913 +       struct dentry *h_dentry;
9914 +
9915 +       bbot = au_dbbot(dentry);
9916 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9917 +               h_dentry = au_h_dptr(dentry, bindex);
9918 +               if (!h_dentry)
9919 +                       continue;
9920 +               if (d_is_positive(h_dentry)) {
9921 +                       au_set_dbtop(dentry, bindex);
9922 +                       return;
9923 +               }
9924 +               au_set_h_dptr(dentry, bindex, NULL);
9925 +       }
9926 +}
9927 +
9928 +void au_update_dbbot(struct dentry *dentry)
9929 +{
9930 +       aufs_bindex_t bindex, btop;
9931 +       struct dentry *h_dentry;
9932 +
9933 +       btop = au_dbtop(dentry);
9934 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9935 +               h_dentry = au_h_dptr(dentry, bindex);
9936 +               if (!h_dentry)
9937 +                       continue;
9938 +               if (d_is_positive(h_dentry)) {
9939 +                       au_set_dbbot(dentry, bindex);
9940 +                       return;
9941 +               }
9942 +               au_set_h_dptr(dentry, bindex, NULL);
9943 +       }
9944 +}
9945 +
9946 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9947 +{
9948 +       aufs_bindex_t bindex, bbot;
9949 +
9950 +       bbot = au_dbbot(dentry);
9951 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9952 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9953 +                       return bindex;
9954 +       return -1;
9955 +}
9956 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9957 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9958 +++ linux/fs/aufs/dir.c 2024-03-10 23:40:47.083143450 +0100
9959 @@ -0,0 +1,767 @@
9960 +// SPDX-License-Identifier: GPL-2.0
9961 +/*
9962 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9963 + *
9964 + * This program is free software; you can redistribute it and/or modify
9965 + * it under the terms of the GNU General Public License as published by
9966 + * the Free Software Foundation; either version 2 of the License, or
9967 + * (at your option) any later version.
9968 + *
9969 + * This program is distributed in the hope that it will be useful,
9970 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9971 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9972 + * GNU General Public License for more details.
9973 + *
9974 + * You should have received a copy of the GNU General Public License
9975 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9976 + */
9977 +
9978 +/*
9979 + * directory operations
9980 + */
9981 +
9982 +#include <linux/fs_stack.h>
9983 +#include <linux/iversion.h>
9984 +#include "aufs.h"
9985 +
9986 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9987 +{
9988 +       unsigned int nlink;
9989 +
9990 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9991 +
9992 +       nlink = dir->i_nlink;
9993 +       nlink += h_dir->i_nlink - 2;
9994 +       if (h_dir->i_nlink < 2)
9995 +               nlink += 2;
9996 +       smp_mb(); /* for i_nlink */
9997 +       /* 0 can happen in revaliding */
9998 +       set_nlink(dir, nlink);
9999 +}
10000 +
10001 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10002 +{
10003 +       unsigned int nlink;
10004 +
10005 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10006 +
10007 +       nlink = dir->i_nlink;
10008 +       nlink -= h_dir->i_nlink - 2;
10009 +       if (h_dir->i_nlink < 2)
10010 +               nlink -= 2;
10011 +       smp_mb(); /* for i_nlink */
10012 +       /* nlink == 0 means the branch-fs is broken */
10013 +       set_nlink(dir, nlink);
10014 +}
10015 +
10016 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10017 +{
10018 +       loff_t sz;
10019 +       aufs_bindex_t bindex, bbot;
10020 +       struct file *h_file;
10021 +       struct dentry *h_dentry;
10022 +
10023 +       sz = 0;
10024 +       if (file) {
10025 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10026 +
10027 +               bbot = au_fbbot_dir(file);
10028 +               for (bindex = au_fbtop(file);
10029 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10030 +                    bindex++) {
10031 +                       h_file = au_hf_dir(file, bindex);
10032 +                       if (h_file && file_inode(h_file))
10033 +                               sz += vfsub_f_size_read(h_file);
10034 +               }
10035 +       } else {
10036 +               AuDebugOn(!dentry);
10037 +               AuDebugOn(!d_is_dir(dentry));
10038 +
10039 +               bbot = au_dbtaildir(dentry);
10040 +               for (bindex = au_dbtop(dentry);
10041 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10042 +                    bindex++) {
10043 +                       h_dentry = au_h_dptr(dentry, bindex);
10044 +                       if (h_dentry && d_is_positive(h_dentry))
10045 +                               sz += i_size_read(d_inode(h_dentry));
10046 +               }
10047 +       }
10048 +       if (sz < KMALLOC_MAX_SIZE)
10049 +               sz = roundup_pow_of_two(sz);
10050 +       if (sz > KMALLOC_MAX_SIZE)
10051 +               sz = KMALLOC_MAX_SIZE;
10052 +       else if (sz < NAME_MAX) {
10053 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10054 +               sz = AUFS_RDBLK_DEF;
10055 +       }
10056 +       return sz;
10057 +}
10058 +
10059 +struct au_dir_ts_arg {
10060 +       struct dentry *dentry;
10061 +       aufs_bindex_t brid;
10062 +};
10063 +
10064 +static void au_do_dir_ts(void *arg)
10065 +{
10066 +       struct au_dir_ts_arg *a = arg;
10067 +       struct au_dtime dt;
10068 +       struct path h_path;
10069 +       struct timespec64 ts;
10070 +       struct inode *dir, *h_dir;
10071 +       struct super_block *sb;
10072 +       struct au_branch *br;
10073 +       struct au_hinode *hdir;
10074 +       int err;
10075 +       aufs_bindex_t btop, bindex;
10076 +
10077 +       sb = a->dentry->d_sb;
10078 +       if (d_really_is_negative(a->dentry))
10079 +               goto out;
10080 +       /* no dir->i_mutex lock */
10081 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10082 +
10083 +       dir = d_inode(a->dentry);
10084 +       btop = au_ibtop(dir);
10085 +       bindex = au_br_index(sb, a->brid);
10086 +       if (bindex < btop)
10087 +               goto out_unlock;
10088 +
10089 +       br = au_sbr(sb, bindex);
10090 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10091 +       if (!h_path.dentry)
10092 +               goto out_unlock;
10093 +       h_path.mnt = au_br_mnt(br);
10094 +       au_dtime_store(&dt, a->dentry, &h_path);
10095 +
10096 +       br = au_sbr(sb, btop);
10097 +       if (!au_br_writable(br->br_perm))
10098 +               goto out_unlock;
10099 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10100 +       h_path.mnt = au_br_mnt(br);
10101 +       err = vfsub_mnt_want_write(h_path.mnt);
10102 +       if (err)
10103 +               goto out_unlock;
10104 +       hdir = au_hi(dir, btop);
10105 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10106 +       h_dir = au_h_iptr(dir, btop);
10107 +       ts = inode_get_mtime(h_dir);
10108 +       if (h_dir->i_nlink
10109 +           && timespec64_compare(&ts, &dt.dt_mtime) < 0) {
10110 +               dt.dt_h_path = h_path;
10111 +               au_dtime_revert(&dt);
10112 +       }
10113 +       au_hn_inode_unlock(hdir);
10114 +       vfsub_mnt_drop_write(h_path.mnt);
10115 +       au_cpup_attr_timesizes(dir);
10116 +
10117 +out_unlock:
10118 +       aufs_read_unlock(a->dentry, AuLock_DW);
10119 +out:
10120 +       dput(a->dentry);
10121 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10122 +       au_kfree_try_rcu(arg);
10123 +}
10124 +
10125 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10126 +{
10127 +       int perm, wkq_err;
10128 +       aufs_bindex_t btop;
10129 +       struct au_dir_ts_arg *arg;
10130 +       struct dentry *dentry;
10131 +       struct super_block *sb;
10132 +
10133 +       IMustLock(dir);
10134 +
10135 +       dentry = d_find_any_alias(dir);
10136 +       AuDebugOn(!dentry);
10137 +       sb = dentry->d_sb;
10138 +       btop = au_ibtop(dir);
10139 +       if (btop == bindex) {
10140 +               au_cpup_attr_timesizes(dir);
10141 +               goto out;
10142 +       }
10143 +
10144 +       perm = au_sbr_perm(sb, btop);
10145 +       if (!au_br_writable(perm))
10146 +               goto out;
10147 +
10148 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10149 +       if (!arg)
10150 +               goto out;
10151 +
10152 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10153 +       arg->brid = au_sbr_id(sb, bindex);
10154 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10155 +       if (unlikely(wkq_err)) {
10156 +               pr_err("wkq %d\n", wkq_err);
10157 +               dput(dentry);
10158 +               au_kfree_try_rcu(arg);
10159 +       }
10160 +
10161 +out:
10162 +       dput(dentry);
10163 +}
10164 +
10165 +/* ---------------------------------------------------------------------- */
10166 +
10167 +static int reopen_dir(struct file *file)
10168 +{
10169 +       int err;
10170 +       unsigned int flags;
10171 +       aufs_bindex_t bindex, btail, btop;
10172 +       struct dentry *dentry, *h_dentry;
10173 +       struct file *h_file;
10174 +
10175 +       /* open all lower dirs */
10176 +       dentry = file->f_path.dentry;
10177 +       btop = au_dbtop(dentry);
10178 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10179 +               au_set_h_fptr(file, bindex, NULL);
10180 +       au_set_fbtop(file, btop);
10181 +
10182 +       btail = au_dbtaildir(dentry);
10183 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10184 +               au_set_h_fptr(file, bindex, NULL);
10185 +       au_set_fbbot_dir(file, btail);
10186 +
10187 +       flags = vfsub_file_flags(file);
10188 +       for (bindex = btop; bindex <= btail; bindex++) {
10189 +               h_dentry = au_h_dptr(dentry, bindex);
10190 +               if (!h_dentry)
10191 +                       continue;
10192 +               h_file = au_hf_dir(file, bindex);
10193 +               if (h_file)
10194 +                       continue;
10195 +
10196 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10197 +               err = PTR_ERR(h_file);
10198 +               if (IS_ERR(h_file))
10199 +                       goto out; /* close all? */
10200 +               au_set_h_fptr(file, bindex, h_file);
10201 +       }
10202 +       au_update_figen(file);
10203 +       /* todo: necessary? */
10204 +       /* file->f_ra = h_file->f_ra; */
10205 +       err = 0;
10206 +
10207 +out:
10208 +       return err;
10209 +}
10210 +
10211 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10212 +{
10213 +       int err;
10214 +       aufs_bindex_t bindex, btail;
10215 +       struct dentry *dentry, *h_dentry;
10216 +       struct vfsmount *mnt;
10217 +
10218 +       FiMustWriteLock(file);
10219 +       AuDebugOn(h_file);
10220 +
10221 +       err = 0;
10222 +       mnt = file->f_path.mnt;
10223 +       dentry = file->f_path.dentry;
10224 +       file->f_version = inode_query_iversion(d_inode(dentry));
10225 +       bindex = au_dbtop(dentry);
10226 +       au_set_fbtop(file, bindex);
10227 +       btail = au_dbtaildir(dentry);
10228 +       au_set_fbbot_dir(file, btail);
10229 +       for (; !err && bindex <= btail; bindex++) {
10230 +               h_dentry = au_h_dptr(dentry, bindex);
10231 +               if (!h_dentry)
10232 +                       continue;
10233 +
10234 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10235 +               if (unlikely(err))
10236 +                       break;
10237 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10238 +               if (IS_ERR(h_file)) {
10239 +                       err = PTR_ERR(h_file);
10240 +                       break;
10241 +               }
10242 +               au_set_h_fptr(file, bindex, h_file);
10243 +       }
10244 +       au_update_figen(file);
10245 +       /* todo: necessary? */
10246 +       /* file->f_ra = h_file->f_ra; */
10247 +       if (!err)
10248 +               return 0; /* success */
10249 +
10250 +       /* close all */
10251 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10252 +               au_set_h_fptr(file, bindex, NULL);
10253 +       au_set_fbtop(file, -1);
10254 +       au_set_fbbot_dir(file, -1);
10255 +
10256 +       return err;
10257 +}
10258 +
10259 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10260 +                        struct file *file)
10261 +{
10262 +       int err;
10263 +       struct super_block *sb;
10264 +       struct au_fidir *fidir;
10265 +
10266 +       err = -ENOMEM;
10267 +       sb = file->f_path.dentry->d_sb;
10268 +       si_read_lock(sb, AuLock_FLUSH);
10269 +       fidir = au_fidir_alloc(sb);
10270 +       if (fidir) {
10271 +               struct au_do_open_args args = {
10272 +                       .open   = do_open_dir,
10273 +                       .fidir  = fidir
10274 +               };
10275 +               err = au_do_open(file, &args);
10276 +               if (unlikely(err))
10277 +                       au_kfree_rcu(fidir);
10278 +       }
10279 +       si_read_unlock(sb);
10280 +       return err;
10281 +}
10282 +
10283 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10284 +                           struct file *file)
10285 +{
10286 +       struct au_vdir *vdir_cache;
10287 +       struct au_finfo *finfo;
10288 +       struct au_fidir *fidir;
10289 +       struct au_hfile *hf;
10290 +       aufs_bindex_t bindex, bbot;
10291 +
10292 +       finfo = au_fi(file);
10293 +       fidir = finfo->fi_hdir;
10294 +       if (fidir) {
10295 +               au_hbl_del(&finfo->fi_hlist,
10296 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10297 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10298 +               if (vdir_cache)
10299 +                       au_vdir_free(vdir_cache);
10300 +
10301 +               bindex = finfo->fi_btop;
10302 +               if (bindex >= 0) {
10303 +                       hf = fidir->fd_hfile + bindex;
10304 +                       /*
10305 +                        * calls fput() instead of filp_close(),
10306 +                        * since no dnotify or lock for the lower file.
10307 +                        */
10308 +                       bbot = fidir->fd_bbot;
10309 +                       for (; bindex <= bbot; bindex++, hf++)
10310 +                               if (hf->hf_file)
10311 +                                       au_hfput(hf, /*execed*/0);
10312 +               }
10313 +               au_kfree_rcu(fidir);
10314 +               finfo->fi_hdir = NULL;
10315 +       }
10316 +       au_finfo_fin(file);
10317 +       return 0;
10318 +}
10319 +
10320 +/* ---------------------------------------------------------------------- */
10321 +
10322 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10323 +{
10324 +       int err;
10325 +       aufs_bindex_t bindex, bbot;
10326 +       struct file *h_file;
10327 +
10328 +       err = 0;
10329 +       bbot = au_fbbot_dir(file);
10330 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10331 +               h_file = au_hf_dir(file, bindex);
10332 +               if (h_file)
10333 +                       err = vfsub_flush(h_file, id);
10334 +       }
10335 +       return err;
10336 +}
10337 +
10338 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10339 +{
10340 +       return au_do_flush(file, id, au_do_flush_dir);
10341 +}
10342 +
10343 +/* ---------------------------------------------------------------------- */
10344 +
10345 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10346 +{
10347 +       int err;
10348 +       aufs_bindex_t bbot, bindex;
10349 +       struct inode *inode;
10350 +       struct super_block *sb;
10351 +
10352 +       err = 0;
10353 +       sb = dentry->d_sb;
10354 +       inode = d_inode(dentry);
10355 +       IMustLock(inode);
10356 +       bbot = au_dbbot(dentry);
10357 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10358 +               struct path h_path;
10359 +
10360 +               if (au_test_ro(sb, bindex, inode))
10361 +                       continue;
10362 +               h_path.dentry = au_h_dptr(dentry, bindex);
10363 +               if (!h_path.dentry)
10364 +                       continue;
10365 +
10366 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10367 +               err = vfsub_fsync(NULL, &h_path, datasync);
10368 +       }
10369 +
10370 +       return err;
10371 +}
10372 +
10373 +static int au_do_fsync_dir(struct file *file, int datasync)
10374 +{
10375 +       int err;
10376 +       aufs_bindex_t bbot, bindex;
10377 +       struct file *h_file;
10378 +       struct super_block *sb;
10379 +       struct inode *inode;
10380 +
10381 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10382 +       if (unlikely(err))
10383 +               goto out;
10384 +
10385 +       inode = file_inode(file);
10386 +       sb = inode->i_sb;
10387 +       bbot = au_fbbot_dir(file);
10388 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10389 +               h_file = au_hf_dir(file, bindex);
10390 +               if (!h_file || au_test_ro(sb, bindex, inode))
10391 +                       continue;
10392 +
10393 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10394 +       }
10395 +
10396 +out:
10397 +       return err;
10398 +}
10399 +
10400 +/*
10401 + * @file may be NULL
10402 + */
10403 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10404 +                         int datasync)
10405 +{
10406 +       int err;
10407 +       struct dentry *dentry;
10408 +       struct inode *inode;
10409 +       struct super_block *sb;
10410 +
10411 +       err = 0;
10412 +       dentry = file->f_path.dentry;
10413 +       inode = d_inode(dentry);
10414 +       inode_lock(inode);
10415 +       sb = dentry->d_sb;
10416 +       si_noflush_read_lock(sb);
10417 +       if (file)
10418 +               err = au_do_fsync_dir(file, datasync);
10419 +       else {
10420 +               di_write_lock_child(dentry);
10421 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10422 +       }
10423 +       au_cpup_attr_timesizes(inode);
10424 +       di_write_unlock(dentry);
10425 +       if (file)
10426 +               fi_write_unlock(file);
10427 +
10428 +       si_read_unlock(sb);
10429 +       inode_unlock(inode);
10430 +       return err;
10431 +}
10432 +
10433 +/* ---------------------------------------------------------------------- */
10434 +
10435 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10436 +{
10437 +       int err;
10438 +       struct dentry *dentry;
10439 +       struct inode *inode, *h_inode;
10440 +       struct super_block *sb;
10441 +
10442 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10443 +
10444 +       dentry = file->f_path.dentry;
10445 +       inode = d_inode(dentry);
10446 +       IMustLock(inode);
10447 +
10448 +       sb = dentry->d_sb;
10449 +       si_read_lock(sb, AuLock_FLUSH);
10450 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10451 +       if (unlikely(err))
10452 +               goto out;
10453 +       err = au_alive_dir(dentry);
10454 +       if (!err)
10455 +               err = au_vdir_init(file);
10456 +       di_downgrade_lock(dentry, AuLock_IR);
10457 +       if (unlikely(err))
10458 +               goto out_unlock;
10459 +
10460 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10461 +       if (!au_test_nfsd()) {
10462 +               err = au_vdir_fill_de(file, ctx);
10463 +               fsstack_copy_attr_atime(inode, h_inode);
10464 +       } else {
10465 +               /*
10466 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10467 +                * encode_fh() and others.
10468 +                */
10469 +               atomic_inc(&h_inode->i_count);
10470 +               di_read_unlock(dentry, AuLock_IR);
10471 +               si_read_unlock(sb);
10472 +               err = au_vdir_fill_de(file, ctx);
10473 +               fsstack_copy_attr_atime(inode, h_inode);
10474 +               fi_write_unlock(file);
10475 +               iput(h_inode);
10476 +
10477 +               AuTraceErr(err);
10478 +               return err;
10479 +       }
10480 +
10481 +out_unlock:
10482 +       di_read_unlock(dentry, AuLock_IR);
10483 +       fi_write_unlock(file);
10484 +out:
10485 +       si_read_unlock(sb);
10486 +       return err;
10487 +}
10488 +
10489 +/* ---------------------------------------------------------------------- */
10490 +
10491 +#define AuTestEmpty_WHONLY     BIT(0)
10492 +#define AuTestEmpty_CALLED     BIT(1)
10493 +#define AuTestEmpty_SHWH       BIT(2)
10494 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10495 +#define au_fset_testempty(flags, name) \
10496 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10497 +#define au_fclr_testempty(flags, name) \
10498 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10499 +
10500 +#ifndef CONFIG_AUFS_SHWH
10501 +#undef AuTestEmpty_SHWH
10502 +#define AuTestEmpty_SHWH       0
10503 +#endif
10504 +
10505 +struct test_empty_arg {
10506 +       struct dir_context ctx;
10507 +       struct au_nhash *whlist;
10508 +       unsigned int flags;
10509 +       int err;
10510 +       aufs_bindex_t bindex;
10511 +};
10512 +
10513 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10514 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10515 +                         unsigned int d_type)
10516 +{
10517 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10518 +                                                 ctx);
10519 +       char *name = (void *)__name;
10520 +
10521 +       arg->err = 0;
10522 +       au_fset_testempty(arg->flags, CALLED);
10523 +       /* smp_mb(); */
10524 +       if (name[0] == '.'
10525 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10526 +               goto out; /* success */
10527 +
10528 +       if (namelen <= AUFS_WH_PFX_LEN
10529 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10530 +               if (au_ftest_testempty(arg->flags, WHONLY)
10531 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10532 +                       arg->err = -ENOTEMPTY;
10533 +               goto out;
10534 +       }
10535 +
10536 +       name += AUFS_WH_PFX_LEN;
10537 +       namelen -= AUFS_WH_PFX_LEN;
10538 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10539 +               arg->err = au_nhash_append_wh
10540 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10541 +                        au_ftest_testempty(arg->flags, SHWH));
10542 +
10543 +out:
10544 +       /* smp_mb(); */
10545 +       AuTraceErr(arg->err);
10546 +       return !arg->err;
10547 +}
10548 +
10549 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10550 +{
10551 +       int err;
10552 +       struct file *h_file;
10553 +       struct au_branch *br;
10554 +
10555 +       h_file = au_h_open(dentry, arg->bindex,
10556 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10557 +                          /*file*/NULL, /*force_wr*/0);
10558 +       err = PTR_ERR(h_file);
10559 +       if (IS_ERR(h_file))
10560 +               goto out;
10561 +
10562 +       err = 0;
10563 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10564 +           && !file_inode(h_file)->i_nlink)
10565 +               goto out_put;
10566 +
10567 +       do {
10568 +               arg->err = 0;
10569 +               au_fclr_testempty(arg->flags, CALLED);
10570 +               /* smp_mb(); */
10571 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10572 +               if (err >= 0)
10573 +                       err = arg->err;
10574 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10575 +
10576 +out_put:
10577 +       fput(h_file);
10578 +       br = au_sbr(dentry->d_sb, arg->bindex);
10579 +       au_lcnt_dec(&br->br_nfiles);
10580 +out:
10581 +       return err;
10582 +}
10583 +
10584 +struct do_test_empty_args {
10585 +       int *errp;
10586 +       struct dentry *dentry;
10587 +       struct test_empty_arg *arg;
10588 +};
10589 +
10590 +static void call_do_test_empty(void *args)
10591 +{
10592 +       struct do_test_empty_args *a = args;
10593 +       *a->errp = do_test_empty(a->dentry, a->arg);
10594 +}
10595 +
10596 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10597 +{
10598 +       int err, wkq_err;
10599 +       struct dentry *h_dentry;
10600 +       struct inode *h_inode;
10601 +       struct mnt_idmap *h_idmap;
10602 +
10603 +       h_idmap = au_sbr_idmap(dentry->d_sb, arg->bindex);
10604 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10605 +       h_inode = d_inode(h_dentry);
10606 +       /* todo: i_mode changes anytime? */
10607 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10608 +       err = au_test_h_perm_sio(h_idmap, h_inode, MAY_EXEC | MAY_READ);
10609 +       inode_unlock_shared(h_inode);
10610 +       if (!err)
10611 +               err = do_test_empty(dentry, arg);
10612 +       else {
10613 +               struct do_test_empty_args args = {
10614 +                       .errp   = &err,
10615 +                       .dentry = dentry,
10616 +                       .arg    = arg
10617 +               };
10618 +               unsigned int flags = arg->flags;
10619 +
10620 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10621 +               if (unlikely(wkq_err))
10622 +                       err = wkq_err;
10623 +               arg->flags = flags;
10624 +       }
10625 +
10626 +       return err;
10627 +}
10628 +
10629 +int au_test_empty_lower(struct dentry *dentry)
10630 +{
10631 +       int err;
10632 +       unsigned int rdhash;
10633 +       aufs_bindex_t bindex, btop, btail;
10634 +       struct au_nhash whlist;
10635 +       struct test_empty_arg arg = {
10636 +               .ctx = {
10637 +                       .actor = test_empty_cb
10638 +               }
10639 +       };
10640 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10641 +
10642 +       SiMustAnyLock(dentry->d_sb);
10643 +
10644 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10645 +       if (!rdhash)
10646 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10647 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10648 +       if (unlikely(err))
10649 +               goto out;
10650 +
10651 +       arg.flags = 0;
10652 +       arg.whlist = &whlist;
10653 +       btop = au_dbtop(dentry);
10654 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10655 +               au_fset_testempty(arg.flags, SHWH);
10656 +       test_empty = do_test_empty;
10657 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10658 +               test_empty = sio_test_empty;
10659 +       arg.bindex = btop;
10660 +       err = test_empty(dentry, &arg);
10661 +       if (unlikely(err))
10662 +               goto out_whlist;
10663 +
10664 +       au_fset_testempty(arg.flags, WHONLY);
10665 +       btail = au_dbtaildir(dentry);
10666 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10667 +               struct dentry *h_dentry;
10668 +
10669 +               h_dentry = au_h_dptr(dentry, bindex);
10670 +               if (h_dentry && d_is_positive(h_dentry)) {
10671 +                       arg.bindex = bindex;
10672 +                       err = test_empty(dentry, &arg);
10673 +               }
10674 +       }
10675 +
10676 +out_whlist:
10677 +       au_nhash_wh_free(&whlist);
10678 +out:
10679 +       return err;
10680 +}
10681 +
10682 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10683 +{
10684 +       int err;
10685 +       struct test_empty_arg arg = {
10686 +               .ctx = {
10687 +                       .actor = test_empty_cb
10688 +               }
10689 +       };
10690 +       aufs_bindex_t bindex, btail;
10691 +
10692 +       err = 0;
10693 +       arg.whlist = whlist;
10694 +       arg.flags = AuTestEmpty_WHONLY;
10695 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10696 +               au_fset_testempty(arg.flags, SHWH);
10697 +       btail = au_dbtaildir(dentry);
10698 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10699 +               struct dentry *h_dentry;
10700 +
10701 +               h_dentry = au_h_dptr(dentry, bindex);
10702 +               if (h_dentry && d_is_positive(h_dentry)) {
10703 +                       arg.bindex = bindex;
10704 +                       err = sio_test_empty(dentry, &arg);
10705 +               }
10706 +       }
10707 +
10708 +       return err;
10709 +}
10710 +
10711 +/* ---------------------------------------------------------------------- */
10712 +
10713 +const struct file_operations aufs_dir_fop = {
10714 +       .owner          = THIS_MODULE,
10715 +       .llseek         = default_llseek,
10716 +       .read           = generic_read_dir,
10717 +       .iterate_shared = aufs_iterate_shared,
10718 +       .unlocked_ioctl = aufs_ioctl_dir,
10719 +#ifdef CONFIG_COMPAT
10720 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10721 +#endif
10722 +       .open           = aufs_open_dir,
10723 +       .release        = aufs_release_dir,
10724 +       .flush          = aufs_flush_dir,
10725 +       .fsync          = aufs_fsync_dir
10726 +};
10727 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10728 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10729 +++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
10730 @@ -0,0 +1,134 @@
10731 +/* SPDX-License-Identifier: GPL-2.0 */
10732 +/*
10733 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10734 + *
10735 + * This program is free software; you can redistribute it and/or modify
10736 + * it under the terms of the GNU General Public License as published by
10737 + * the Free Software Foundation; either version 2 of the License, or
10738 + * (at your option) any later version.
10739 + *
10740 + * This program is distributed in the hope that it will be useful,
10741 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10742 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10743 + * GNU General Public License for more details.
10744 + *
10745 + * You should have received a copy of the GNU General Public License
10746 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10747 + */
10748 +
10749 +/*
10750 + * directory operations
10751 + */
10752 +
10753 +#ifndef __AUFS_DIR_H__
10754 +#define __AUFS_DIR_H__
10755 +
10756 +#ifdef __KERNEL__
10757 +
10758 +#include <linux/fs.h>
10759 +
10760 +/* ---------------------------------------------------------------------- */
10761 +
10762 +/* need to be faster and smaller */
10763 +
10764 +struct au_nhash {
10765 +       unsigned int            nh_num;
10766 +       struct hlist_head       *nh_head;
10767 +};
10768 +
10769 +struct au_vdir_destr {
10770 +       unsigned char   len;
10771 +       unsigned char   name[];
10772 +} __packed;
10773 +
10774 +struct au_vdir_dehstr {
10775 +       struct hlist_node       hash;
10776 +       struct au_vdir_destr    *str;
10777 +       struct rcu_head         rcu;
10778 +} ____cacheline_aligned_in_smp;
10779 +
10780 +struct au_vdir_de {
10781 +       ino_t                   de_ino;
10782 +       unsigned char           de_type;
10783 +       /* caution: packed */
10784 +       struct au_vdir_destr    de_str;
10785 +} __packed;
10786 +
10787 +struct au_vdir_wh {
10788 +       struct hlist_node       wh_hash;
10789 +#ifdef CONFIG_AUFS_SHWH
10790 +       ino_t                   wh_ino;
10791 +       aufs_bindex_t           wh_bindex;
10792 +       unsigned char           wh_type;
10793 +#else
10794 +       aufs_bindex_t           wh_bindex;
10795 +#endif
10796 +       /* caution: packed */
10797 +       struct au_vdir_destr    wh_str;
10798 +} __packed;
10799 +
10800 +union au_vdir_deblk_p {
10801 +       unsigned char           *deblk;
10802 +       struct au_vdir_de       *de;
10803 +};
10804 +
10805 +struct au_vdir {
10806 +       unsigned char   **vd_deblk;
10807 +       unsigned long   vd_nblk;
10808 +       struct {
10809 +               unsigned long           ul;
10810 +               union au_vdir_deblk_p   p;
10811 +       } vd_last;
10812 +
10813 +       u64             vd_version;
10814 +       unsigned int    vd_deblk_sz;
10815 +       unsigned long   vd_jiffy;
10816 +       struct rcu_head rcu;
10817 +} ____cacheline_aligned_in_smp;
10818 +
10819 +/* ---------------------------------------------------------------------- */
10820 +
10821 +/* dir.c */
10822 +extern const struct file_operations aufs_dir_fop;
10823 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10824 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10825 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10826 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10827 +int au_test_empty_lower(struct dentry *dentry);
10828 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10829 +
10830 +/* vdir.c */
10831 +unsigned int au_rdhash_est(loff_t sz);
10832 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10833 +void au_nhash_wh_free(struct au_nhash *whlist);
10834 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10835 +                           int limit);
10836 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10837 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10838 +                      unsigned int d_type, aufs_bindex_t bindex,
10839 +                      unsigned char shwh);
10840 +void au_vdir_free(struct au_vdir *vdir);
10841 +int au_vdir_init(struct file *file);
10842 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10843 +
10844 +/* ioctl.c */
10845 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10846 +
10847 +#ifdef CONFIG_AUFS_RDU
10848 +/* rdu.c */
10849 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10850 +#ifdef CONFIG_COMPAT
10851 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10852 +                        unsigned long arg);
10853 +#endif
10854 +#else
10855 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10856 +       unsigned int cmd, unsigned long arg)
10857 +#ifdef CONFIG_COMPAT
10858 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10859 +       unsigned int cmd, unsigned long arg)
10860 +#endif
10861 +#endif
10862 +
10863 +#endif /* __KERNEL__ */
10864 +#endif /* __AUFS_DIR_H__ */
10865 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10866 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10867 +++ linux/fs/aufs/dirren.c      2023-10-10 22:51:18.033248030 +0200
10868 @@ -0,0 +1,1315 @@
10869 +// SPDX-License-Identifier: GPL-2.0
10870 +/*
10871 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10872 + *
10873 + * This program is free software; you can redistribute it and/or modify
10874 + * it under the terms of the GNU General Public License as published by
10875 + * the Free Software Foundation; either version 2 of the License, or
10876 + * (at your option) any later version.
10877 + *
10878 + * This program is distributed in the hope that it will be useful,
10879 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10880 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10881 + * GNU General Public License for more details.
10882 + *
10883 + * You should have received a copy of the GNU General Public License
10884 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10885 + */
10886 +
10887 +/*
10888 + * special handling in renaming a directory
10889 + * in order to support looking-up the before-renamed name on the lower readonly
10890 + * branches
10891 + */
10892 +
10893 +#include <linux/byteorder/generic.h>
10894 +#include "aufs.h"
10895 +
10896 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10897 +{
10898 +       int idx;
10899 +
10900 +       idx = au_dr_ihash(ent->dr_h_ino);
10901 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10902 +}
10903 +
10904 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10905 +{
10906 +       int ret, i;
10907 +       struct hlist_bl_head *hbl;
10908 +
10909 +       ret = 1;
10910 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10911 +               hbl = dr->dr_h_ino + i;
10912 +               hlist_bl_lock(hbl);
10913 +               ret &= hlist_bl_empty(hbl);
10914 +               hlist_bl_unlock(hbl);
10915 +       }
10916 +
10917 +       return ret;
10918 +}
10919 +
10920 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10921 +{
10922 +       struct au_dr_hino *found, *ent;
10923 +       struct hlist_bl_head *hbl;
10924 +       struct hlist_bl_node *pos;
10925 +       int idx;
10926 +
10927 +       found = NULL;
10928 +       idx = au_dr_ihash(ino);
10929 +       hbl = dr->dr_h_ino + idx;
10930 +       hlist_bl_lock(hbl);
10931 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10932 +               if (ent->dr_h_ino == ino) {
10933 +                       found = ent;
10934 +                       break;
10935 +               }
10936 +       hlist_bl_unlock(hbl);
10937 +
10938 +       return found;
10939 +}
10940 +
10941 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10942 +                       struct au_dr_hino *add_ent)
10943 +{
10944 +       int found, idx;
10945 +       struct hlist_bl_head *hbl;
10946 +       struct hlist_bl_node *pos;
10947 +       struct au_dr_hino *ent;
10948 +
10949 +       found = 0;
10950 +       idx = au_dr_ihash(ino);
10951 +       hbl = dr->dr_h_ino + idx;
10952 +#if 0 /* debug print */
10953 +       {
10954 +               struct hlist_bl_node *tmp;
10955 +
10956 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10957 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10958 +       }
10959 +#endif
10960 +       hlist_bl_lock(hbl);
10961 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10962 +               if (ent->dr_h_ino == ino) {
10963 +                       found = 1;
10964 +                       break;
10965 +               }
10966 +       if (!found && add_ent)
10967 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10968 +       hlist_bl_unlock(hbl);
10969 +
10970 +       if (!found && add_ent)
10971 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10972 +
10973 +       return found;
10974 +}
10975 +
10976 +void au_dr_hino_free(struct au_dr_br *dr)
10977 +{
10978 +       int i;
10979 +       struct hlist_bl_head *hbl;
10980 +       struct hlist_bl_node *pos, *tmp;
10981 +       struct au_dr_hino *ent;
10982 +
10983 +       /* SiMustWriteLock(sb); */
10984 +
10985 +       for (i = 0; i < AuDirren_NHASH; i++) {
10986 +               hbl = dr->dr_h_ino + i;
10987 +               /* no spinlock since sbinfo must be write-locked */
10988 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10989 +                       au_kfree_rcu(ent);
10990 +               INIT_HLIST_BL_HEAD(hbl);
10991 +       }
10992 +}
10993 +
10994 +/* returns the number of inodes or an error */
10995 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10996 +                           struct file *hinofile)
10997 +{
10998 +       int err, i;
10999 +       ssize_t ssz;
11000 +       loff_t pos, oldsize;
11001 +       __be64 u64;
11002 +       struct inode *hinoinode;
11003 +       struct hlist_bl_head *hbl;
11004 +       struct hlist_bl_node *n1, *n2;
11005 +       struct au_dr_hino *ent;
11006 +
11007 +       SiMustWriteLock(sb);
11008 +       AuDebugOn(!au_br_writable(br->br_perm));
11009 +
11010 +       hinoinode = file_inode(hinofile);
11011 +       oldsize = i_size_read(hinoinode);
11012 +
11013 +       err = 0;
11014 +       pos = 0;
11015 +       hbl = br->br_dirren.dr_h_ino;
11016 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11017 +               /* no bit-lock since sbinfo must be write-locked */
11018 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11019 +                       AuDbg("hi%llu, %pD2\n",
11020 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11021 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11022 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11023 +                       if (ssz == sizeof(u64))
11024 +                               continue;
11025 +
11026 +                       /* write error */
11027 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11028 +                       err = -ENOSPC;
11029 +                       if (ssz < 0)
11030 +                               err = ssz;
11031 +                       break;
11032 +               }
11033 +       }
11034 +       /* regardless the error */
11035 +       if (pos < oldsize) {
11036 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11037 +               AuTraceErr(err);
11038 +       }
11039 +
11040 +       AuTraceErr(err);
11041 +       return err;
11042 +}
11043 +
11044 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11045 +{
11046 +       int err, hidx;
11047 +       ssize_t ssz;
11048 +       size_t sz, n;
11049 +       loff_t pos;
11050 +       uint64_t u64;
11051 +       struct au_dr_hino *ent;
11052 +       struct inode *hinoinode;
11053 +       struct hlist_bl_head *hbl;
11054 +
11055 +       err = 0;
11056 +       pos = 0;
11057 +       hbl = dr->dr_h_ino;
11058 +       hinoinode = file_inode(hinofile);
11059 +       sz = i_size_read(hinoinode);
11060 +       AuDebugOn(sz % sizeof(u64));
11061 +       n = sz / sizeof(u64);
11062 +       while (n--) {
11063 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11064 +               if (unlikely(ssz != sizeof(u64))) {
11065 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11066 +                       err = -EINVAL;
11067 +                       if (ssz < 0)
11068 +                               err = ssz;
11069 +                       goto out_free;
11070 +               }
11071 +
11072 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11073 +               if (!ent) {
11074 +                       err = -ENOMEM;
11075 +                       AuTraceErr(err);
11076 +                       goto out_free;
11077 +               }
11078 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11079 +               AuDbg("hi%llu, %pD2\n",
11080 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11081 +               hidx = au_dr_ihash(ent->dr_h_ino);
11082 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11083 +       }
11084 +       goto out; /* success */
11085 +
11086 +out_free:
11087 +       au_dr_hino_free(dr);
11088 +out:
11089 +       AuTraceErr(err);
11090 +       return err;
11091 +}
11092 +
11093 +/*
11094 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11095 + * @path is a switch to distinguish load and store.
11096 + */
11097 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11098 +                     struct au_branch *br, const struct path *path)
11099 +{
11100 +       int err, flags;
11101 +       unsigned char load, suspend;
11102 +       struct file *hinofile;
11103 +       struct au_hinode *hdir;
11104 +       struct inode *dir, *delegated;
11105 +       struct path hinopath;
11106 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11107 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11108 +
11109 +       AuDebugOn(bindex < 0 && !br);
11110 +       AuDebugOn(bindex >= 0 && br);
11111 +
11112 +       err = -EINVAL;
11113 +       suspend = !br;
11114 +       if (suspend)
11115 +               br = au_sbr(sb, bindex);
11116 +       load = !!path;
11117 +       if (!load) {
11118 +               path = &br->br_path;
11119 +               AuDebugOn(!au_br_writable(br->br_perm));
11120 +               if (unlikely(!au_br_writable(br->br_perm)))
11121 +                       goto out;
11122 +       }
11123 +
11124 +       hdir = NULL;
11125 +       if (suspend) {
11126 +               dir = d_inode(sb->s_root);
11127 +               hdir = au_hinode(au_ii(dir), bindex);
11128 +               dir = hdir->hi_inode;
11129 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11130 +       } else {
11131 +               dir = d_inode(path->dentry);
11132 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11133 +       }
11134 +       hinopath.mnt = path->mnt;
11135 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11136 +       err = PTR_ERR(hinopath.dentry);
11137 +       if (IS_ERR(hinopath.dentry))
11138 +               goto out_unlock;
11139 +
11140 +       err = 0;
11141 +       flags = O_RDONLY;
11142 +       if (load) {
11143 +               if (d_is_negative(hinopath.dentry))
11144 +                       goto out_dput; /* success */
11145 +       } else {
11146 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11147 +                       if (d_is_positive(hinopath.dentry)) {
11148 +                               delegated = NULL;
11149 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11150 +                                                  /*force*/0);
11151 +                               AuTraceErr(err);
11152 +                               if (unlikely(err))
11153 +                                       pr_err("ignored err %d, %pd2\n",
11154 +                                              err, hinopath.dentry);
11155 +                               if (unlikely(err == -EWOULDBLOCK))
11156 +                                       iput(delegated);
11157 +                               err = 0;
11158 +                       }
11159 +                       goto out_dput;
11160 +               } else if (!d_is_positive(hinopath.dentry)) {
11161 +                       err = vfsub_create(dir, &hinopath, 0600,
11162 +                                          /*want_excl*/false);
11163 +                       AuTraceErr(err);
11164 +                       if (unlikely(err))
11165 +                               goto out_dput;
11166 +               }
11167 +               flags = O_WRONLY;
11168 +       }
11169 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11170 +       if (suspend)
11171 +               au_hn_inode_unlock(hdir);
11172 +       else
11173 +               inode_unlock(dir);
11174 +       dput(hinopath.dentry);
11175 +       AuTraceErrPtr(hinofile);
11176 +       if (IS_ERR(hinofile)) {
11177 +               err = PTR_ERR(hinofile);
11178 +               goto out;
11179 +       }
11180 +
11181 +       if (load)
11182 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11183 +       else
11184 +               err = au_dr_hino_store(sb, br, hinofile);
11185 +       fput(hinofile);
11186 +       goto out;
11187 +
11188 +out_dput:
11189 +       dput(hinopath.dentry);
11190 +out_unlock:
11191 +       if (suspend)
11192 +               au_hn_inode_unlock(hdir);
11193 +       else
11194 +               inode_unlock(dir);
11195 +out:
11196 +       AuTraceErr(err);
11197 +       return err;
11198 +}
11199 +
11200 +/* ---------------------------------------------------------------------- */
11201 +
11202 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11203 +{
11204 +       int err;
11205 +       struct kstatfs kstfs;
11206 +       dev_t dev;
11207 +       struct dentry *dentry;
11208 +       struct super_block *sb;
11209 +
11210 +       err = vfs_statfs((void *)path, &kstfs);
11211 +       AuTraceErr(err);
11212 +       if (unlikely(err))
11213 +               goto out;
11214 +
11215 +       /* todo: support for UUID */
11216 +
11217 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11218 +               brid->type = AuBrid_FSID;
11219 +               brid->fsid = kstfs.f_fsid;
11220 +       } else {
11221 +               dentry = path->dentry;
11222 +               sb = dentry->d_sb;
11223 +               dev = sb->s_dev;
11224 +               if (dev) {
11225 +                       brid->type = AuBrid_DEV;
11226 +                       brid->dev = dev;
11227 +               }
11228 +       }
11229 +
11230 +out:
11231 +       return err;
11232 +}
11233 +
11234 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11235 +                 const struct path *path)
11236 +{
11237 +       int err, i;
11238 +       struct au_dr_br *dr;
11239 +       struct hlist_bl_head *hbl;
11240 +
11241 +       dr = &br->br_dirren;
11242 +       hbl = dr->dr_h_ino;
11243 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11244 +               INIT_HLIST_BL_HEAD(hbl);
11245 +
11246 +       err = au_dr_brid_init(&dr->dr_brid, path);
11247 +       if (unlikely(err))
11248 +               goto out;
11249 +
11250 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11251 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11252 +
11253 +out:
11254 +       AuTraceErr(err);
11255 +       return err;
11256 +}
11257 +
11258 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11259 +{
11260 +       int err;
11261 +
11262 +       err = 0;
11263 +       if (au_br_writable(br->br_perm))
11264 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11265 +       if (!err)
11266 +               au_dr_hino_free(&br->br_dirren);
11267 +
11268 +       return err;
11269 +}
11270 +
11271 +/* ---------------------------------------------------------------------- */
11272 +
11273 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11274 +                      char *buf, size_t sz)
11275 +{
11276 +       int err;
11277 +       unsigned int major, minor;
11278 +       char *p;
11279 +
11280 +       p = buf;
11281 +       err = snprintf(p, sz, "%d_", brid->type);
11282 +       AuDebugOn(err > sz);
11283 +       p += err;
11284 +       sz -= err;
11285 +       switch (brid->type) {
11286 +       case AuBrid_Unset:
11287 +               return -EINVAL;
11288 +       case AuBrid_UUID:
11289 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11290 +               break;
11291 +       case AuBrid_FSID:
11292 +               err = snprintf(p, sz, "%08x-%08x",
11293 +                              brid->fsid.val[0], brid->fsid.val[1]);
11294 +               break;
11295 +       case AuBrid_DEV:
11296 +               major = MAJOR(brid->dev);
11297 +               minor = MINOR(brid->dev);
11298 +               if (major <= 0xff && minor <= 0xff)
11299 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11300 +               else
11301 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11302 +               break;
11303 +       }
11304 +       AuDebugOn(err > sz);
11305 +       p += err;
11306 +       sz -= err;
11307 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11308 +       AuDebugOn(err > sz);
11309 +       p += err;
11310 +       sz -= err;
11311 +
11312 +       return p - buf;
11313 +}
11314 +
11315 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11316 +{
11317 +       int rlen;
11318 +       struct dentry *br_dentry;
11319 +       struct inode *br_inode;
11320 +
11321 +       br_dentry = au_br_dentry(br);
11322 +       br_inode = d_inode(br_dentry);
11323 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11324 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11325 +       AuDebugOn(rlen > len);
11326 +
11327 +       return rlen;
11328 +}
11329 +
11330 +/* ---------------------------------------------------------------------- */
11331 +
11332 +/*
11333 + * from the given @h_dentry, construct drinfo at @*fdata.
11334 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11335 + * @allocated.
11336 + */
11337 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11338 +                              struct dentry *h_dentry,
11339 +                              unsigned char *allocated)
11340 +{
11341 +       int err, v;
11342 +       struct au_drinfo_fdata *f, *p;
11343 +       struct au_drinfo *drinfo;
11344 +       struct inode *h_inode;
11345 +       struct qstr *qname;
11346 +
11347 +       err = 0;
11348 +       f = *fdata;
11349 +       h_inode = d_inode(h_dentry);
11350 +       qname = &h_dentry->d_name;
11351 +       drinfo = &f->drinfo;
11352 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11353 +       drinfo->oldnamelen = qname->len;
11354 +       if (*allocated < sizeof(*f) + qname->len) {
11355 +               v = roundup_pow_of_two(*allocated + qname->len);
11356 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11357 +               if (unlikely(!p)) {
11358 +                       err = -ENOMEM;
11359 +                       AuTraceErr(err);
11360 +                       goto out;
11361 +               }
11362 +               f = p;
11363 +               *fdata = f;
11364 +               *allocated = v;
11365 +               drinfo = &f->drinfo;
11366 +       }
11367 +       memcpy(drinfo->oldname, qname->name, qname->len);
11368 +       AuDbg("i%llu, %.*s\n",
11369 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11370 +             drinfo->oldname);
11371 +
11372 +out:
11373 +       AuTraceErr(err);
11374 +       return err;
11375 +}
11376 +
11377 +/* callers have to free the return value */
11378 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11379 +{
11380 +       struct au_drinfo *ret, *drinfo;
11381 +       struct au_drinfo_fdata fdata;
11382 +       int len;
11383 +       loff_t pos;
11384 +       ssize_t ssz;
11385 +
11386 +       ret = ERR_PTR(-EIO);
11387 +       pos = 0;
11388 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11389 +       if (unlikely(ssz != sizeof(fdata))) {
11390 +               AuIOErr("ssz %zd, %u, %pD2\n",
11391 +                       ssz, (unsigned int)sizeof(fdata), file);
11392 +               goto out;
11393 +       }
11394 +
11395 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11396 +       switch (fdata.magic) {
11397 +       case AUFS_DRINFO_MAGIC_V1:
11398 +               break;
11399 +       default:
11400 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11401 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11402 +               goto out;
11403 +       }
11404 +
11405 +       drinfo = &fdata.drinfo;
11406 +       len = drinfo->oldnamelen;
11407 +       if (!len) {
11408 +               AuIOErr("broken drinfo %pD2\n", file);
11409 +               goto out;
11410 +       }
11411 +
11412 +       ret = NULL;
11413 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11414 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11415 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11416 +                     (unsigned long long)drinfo->ino,
11417 +                     (unsigned long long)h_ino, file);
11418 +               goto out; /* success */
11419 +       }
11420 +
11421 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11422 +       if (unlikely(!ret)) {
11423 +               ret = ERR_PTR(-ENOMEM);
11424 +               AuTraceErrPtr(ret);
11425 +               goto out;
11426 +       }
11427 +
11428 +       *ret = *drinfo;
11429 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11430 +       if (unlikely(ssz != len)) {
11431 +               au_kfree_rcu(ret);
11432 +               ret = ERR_PTR(-EIO);
11433 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11434 +               goto out;
11435 +       }
11436 +
11437 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11438 +
11439 +out:
11440 +       return ret;
11441 +}
11442 +
11443 +/* ---------------------------------------------------------------------- */
11444 +
11445 +/* in order to be revertible */
11446 +struct au_drinfo_rev_elm {
11447 +       int                     created;
11448 +       struct dentry           *info_dentry;
11449 +       struct au_drinfo        *info_last;
11450 +};
11451 +
11452 +struct au_drinfo_rev {
11453 +       unsigned char                   already;
11454 +       aufs_bindex_t                   nelm;
11455 +       struct au_drinfo_rev_elm        elm[];
11456 +};
11457 +
11458 +/* todo: isn't it too large? */
11459 +struct au_drinfo_store {
11460 +       struct path h_ppath;
11461 +       struct dentry *h_dentry;
11462 +       struct au_drinfo_fdata *fdata;
11463 +       char *infoname;                 /* inside of whname, just after PFX */
11464 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11465 +       aufs_bindex_t btgt, btail;
11466 +       unsigned char no_sio,
11467 +               allocated,              /* current size of *fdata */
11468 +               infonamelen,            /* room size for p */
11469 +               whnamelen,              /* length of the generated name */
11470 +               renameback;             /* renamed back */
11471 +};
11472 +
11473 +/* on rename(2) error, the caller should revert it using @elm */
11474 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11475 +                             struct au_drinfo_rev_elm *elm)
11476 +{
11477 +       int err, len;
11478 +       ssize_t ssz;
11479 +       loff_t pos;
11480 +       struct path infopath = {
11481 +               .mnt = w->h_ppath.mnt
11482 +       };
11483 +       struct inode *h_dir, *h_inode, *delegated;
11484 +       struct file *infofile;
11485 +       struct qstr *qname;
11486 +
11487 +       AuDebugOn(elm
11488 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11489 +
11490 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11491 +                                              w->whnamelen);
11492 +       AuTraceErrPtr(infopath.dentry);
11493 +       if (IS_ERR(infopath.dentry)) {
11494 +               err = PTR_ERR(infopath.dentry);
11495 +               goto out;
11496 +       }
11497 +
11498 +       err = 0;
11499 +       h_dir = d_inode(w->h_ppath.dentry);
11500 +       if (elm && d_is_negative(infopath.dentry)) {
11501 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11502 +               AuTraceErr(err);
11503 +               if (unlikely(err))
11504 +                       goto out_dput;
11505 +               elm->created = 1;
11506 +               elm->info_dentry = dget(infopath.dentry);
11507 +       }
11508 +
11509 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11510 +       AuTraceErrPtr(infofile);
11511 +       if (IS_ERR(infofile)) {
11512 +               err = PTR_ERR(infofile);
11513 +               goto out_dput;
11514 +       }
11515 +
11516 +       h_inode = d_inode(infopath.dentry);
11517 +       if (elm && i_size_read(h_inode)) {
11518 +               h_inode = d_inode(w->h_dentry);
11519 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11520 +               AuTraceErrPtr(elm->info_last);
11521 +               if (IS_ERR(elm->info_last)) {
11522 +                       err = PTR_ERR(elm->info_last);
11523 +                       elm->info_last = NULL;
11524 +                       AuDebugOn(elm->info_dentry);
11525 +                       goto out_fput;
11526 +               }
11527 +       }
11528 +
11529 +       if (elm && w->renameback) {
11530 +               delegated = NULL;
11531 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11532 +               AuTraceErr(err);
11533 +               if (unlikely(err == -EWOULDBLOCK))
11534 +                       iput(delegated);
11535 +               goto out_fput;
11536 +       }
11537 +
11538 +       pos = 0;
11539 +       qname = &w->h_dentry->d_name;
11540 +       len = sizeof(*w->fdata) + qname->len;
11541 +       if (!elm)
11542 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11543 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11544 +       if (ssz == len) {
11545 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11546 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11547 +               goto out_fput; /* success */
11548 +       } else {
11549 +               err = -EIO;
11550 +               if (ssz < 0)
11551 +                       err = ssz;
11552 +               /* the caller should revert it using @elm */
11553 +       }
11554 +
11555 +out_fput:
11556 +       fput(infofile);
11557 +out_dput:
11558 +       dput(infopath.dentry);
11559 +out:
11560 +       AuTraceErr(err);
11561 +       return err;
11562 +}
11563 +
11564 +struct au_call_drinfo_do_store_args {
11565 +       int *errp;
11566 +       struct au_drinfo_store *w;
11567 +       struct au_drinfo_rev_elm *elm;
11568 +};
11569 +
11570 +static void au_call_drinfo_do_store(void *args)
11571 +{
11572 +       struct au_call_drinfo_do_store_args *a = args;
11573 +
11574 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11575 +}
11576 +
11577 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11578 +                              struct au_drinfo_rev_elm *elm)
11579 +{
11580 +       int err, wkq_err;
11581 +
11582 +       if (w->no_sio)
11583 +               err = au_drinfo_do_store(w, elm);
11584 +       else {
11585 +               struct au_call_drinfo_do_store_args a = {
11586 +                       .errp   = &err,
11587 +                       .w      = w,
11588 +                       .elm    = elm
11589 +               };
11590 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11591 +               if (unlikely(wkq_err))
11592 +                       err = wkq_err;
11593 +       }
11594 +       AuTraceErr(err);
11595 +
11596 +       return err;
11597 +}
11598 +
11599 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11600 +                                    aufs_bindex_t btgt)
11601 +{
11602 +       int err;
11603 +
11604 +       memset(w, 0, sizeof(*w));
11605 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11606 +       strscpy(w->whname, AUFS_WH_DR_INFO_PFX, sizeof(AUFS_WH_DR_INFO_PFX));
11607 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11608 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11609 +       w->btgt = btgt;
11610 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11611 +
11612 +       err = -ENOMEM;
11613 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11614 +       if (unlikely(!w->fdata)) {
11615 +               AuTraceErr(err);
11616 +               goto out;
11617 +       }
11618 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11619 +       err = 0;
11620 +
11621 +out:
11622 +       return err;
11623 +}
11624 +
11625 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11626 +{
11627 +       au_kfree_rcu(w->fdata);
11628 +}
11629 +
11630 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11631 +                               struct au_drinfo_store *w)
11632 +{
11633 +       struct au_drinfo_rev_elm *elm;
11634 +       struct inode *h_dir, *delegated;
11635 +       int err, nelm;
11636 +       struct path infopath = {
11637 +               .mnt = w->h_ppath.mnt
11638 +       };
11639 +
11640 +       h_dir = d_inode(w->h_ppath.dentry);
11641 +       IMustLock(h_dir);
11642 +
11643 +       err = 0;
11644 +       elm = rev->elm;
11645 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11646 +               AuDebugOn(elm->created && elm->info_last);
11647 +               if (elm->created) {
11648 +                       AuDbg("here\n");
11649 +                       delegated = NULL;
11650 +                       infopath.dentry = elm->info_dentry;
11651 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11652 +                                          !w->no_sio);
11653 +                       AuTraceErr(err);
11654 +                       if (unlikely(err == -EWOULDBLOCK))
11655 +                               iput(delegated);
11656 +                       dput(elm->info_dentry);
11657 +               } else if (elm->info_last) {
11658 +                       AuDbg("here\n");
11659 +                       w->fdata->drinfo = *elm->info_last;
11660 +                       memcpy(w->fdata->drinfo.oldname,
11661 +                              elm->info_last->oldname,
11662 +                              elm->info_last->oldnamelen);
11663 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11664 +                       au_kfree_rcu(elm->info_last);
11665 +               }
11666 +               if (unlikely(err))
11667 +                       AuIOErr("%d, %s\n", err, w->whname);
11668 +               /* go on even if err */
11669 +       }
11670 +}
11671 +
11672 +/* caller has to call au_dr_rename_fin() later */
11673 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11674 +                          struct qstr *dst_name, void *_rev)
11675 +{
11676 +       int err, sz, nelm;
11677 +       aufs_bindex_t bindex, btail;
11678 +       struct au_drinfo_store work;
11679 +       struct au_drinfo_rev *rev, **p;
11680 +       struct au_drinfo_rev_elm *elm;
11681 +       struct super_block *sb;
11682 +       struct au_branch *br;
11683 +       struct au_hinode *hdir;
11684 +
11685 +       err = au_drinfo_store_work_init(&work, btgt);
11686 +       AuTraceErr(err);
11687 +       if (unlikely(err))
11688 +               goto out;
11689 +
11690 +       err = -ENOMEM;
11691 +       btail = au_dbtaildir(dentry);
11692 +       nelm = btail - btgt;
11693 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11694 +       rev = kcalloc(1, sz, GFP_NOFS);
11695 +       if (unlikely(!rev)) {
11696 +               AuTraceErr(err);
11697 +               goto out_args;
11698 +       }
11699 +       rev->nelm = nelm;
11700 +       elm = rev->elm;
11701 +       p = _rev;
11702 +       *p = rev;
11703 +
11704 +       err = 0;
11705 +       sb = dentry->d_sb;
11706 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11707 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11708 +       hdir = au_hi(d_inode(dentry), btgt);
11709 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11710 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11711 +               work.h_dentry = au_h_dptr(dentry, bindex);
11712 +               if (!work.h_dentry)
11713 +                       continue;
11714 +
11715 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11716 +                                         &work.allocated);
11717 +               AuTraceErr(err);
11718 +               if (unlikely(err))
11719 +                       break;
11720 +
11721 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11722 +               br = au_sbr(sb, bindex);
11723 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11724 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11725 +                                                work.infonamelen);
11726 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11727 +                     work.whnamelen, work.whname,
11728 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11729 +                     work.fdata->drinfo.oldnamelen,
11730 +                     work.fdata->drinfo.oldname);
11731 +
11732 +               err = au_drinfo_store_sio(&work, elm);
11733 +               AuTraceErr(err);
11734 +               if (unlikely(err))
11735 +                       break;
11736 +       }
11737 +       if (unlikely(err)) {
11738 +               /* revert all drinfo */
11739 +               au_drinfo_store_rev(rev, &work);
11740 +               au_kfree_try_rcu(rev);
11741 +               *p = NULL;
11742 +       }
11743 +       au_hn_inode_unlock(hdir);
11744 +
11745 +out_args:
11746 +       au_drinfo_store_work_fin(&work);
11747 +out:
11748 +       return err;
11749 +}
11750 +
11751 +/* ---------------------------------------------------------------------- */
11752 +
11753 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11754 +                struct qstr *dst_name, void *_rev)
11755 +{
11756 +       int err, already;
11757 +       ino_t ino;
11758 +       struct super_block *sb;
11759 +       struct au_branch *br;
11760 +       struct au_dr_br *dr;
11761 +       struct dentry *h_dentry;
11762 +       struct inode *h_inode;
11763 +       struct au_dr_hino *ent;
11764 +       struct au_drinfo_rev *rev, **p;
11765 +
11766 +       AuDbg("bindex %d\n", bindex);
11767 +
11768 +       err = -ENOMEM;
11769 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11770 +       if (unlikely(!ent))
11771 +               goto out;
11772 +
11773 +       sb = src->d_sb;
11774 +       br = au_sbr(sb, bindex);
11775 +       dr = &br->br_dirren;
11776 +       h_dentry = au_h_dptr(src, bindex);
11777 +       h_inode = d_inode(h_dentry);
11778 +       ino = h_inode->i_ino;
11779 +       ent->dr_h_ino = ino;
11780 +       already = au_dr_hino_test_add(dr, ino, ent);
11781 +       AuDbg("b%d, hi%llu, already %d\n",
11782 +             bindex, (unsigned long long)ino, already);
11783 +
11784 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11785 +       AuTraceErr(err);
11786 +       if (!err) {
11787 +               p = _rev;
11788 +               rev = *p;
11789 +               rev->already = already;
11790 +               goto out; /* success */
11791 +       }
11792 +
11793 +       /* revert */
11794 +       if (!already)
11795 +               au_dr_hino_del(dr, ent);
11796 +       au_kfree_rcu(ent);
11797 +
11798 +out:
11799 +       AuTraceErr(err);
11800 +       return err;
11801 +}
11802 +
11803 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11804 +{
11805 +       struct au_drinfo_rev *rev;
11806 +       struct au_drinfo_rev_elm *elm;
11807 +       int nelm;
11808 +
11809 +       rev = _rev;
11810 +       elm = rev->elm;
11811 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11812 +               dput(elm->info_dentry);
11813 +               au_kfree_rcu(elm->info_last);
11814 +       }
11815 +       au_kfree_try_rcu(rev);
11816 +}
11817 +
11818 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11819 +{
11820 +       int err;
11821 +       struct au_drinfo_store work;
11822 +       struct au_drinfo_rev *rev = _rev;
11823 +       struct super_block *sb;
11824 +       struct au_branch *br;
11825 +       struct inode *h_inode;
11826 +       struct au_dr_br *dr;
11827 +       struct au_dr_hino *ent;
11828 +
11829 +       err = au_drinfo_store_work_init(&work, btgt);
11830 +       if (unlikely(err))
11831 +               goto out;
11832 +
11833 +       sb = src->d_sb;
11834 +       br = au_sbr(sb, btgt);
11835 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11836 +       work.h_ppath.mnt = au_br_mnt(br);
11837 +       au_drinfo_store_rev(rev, &work);
11838 +       au_drinfo_store_work_fin(&work);
11839 +       if (rev->already)
11840 +               goto out;
11841 +
11842 +       dr = &br->br_dirren;
11843 +       h_inode = d_inode(work.h_ppath.dentry);
11844 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11845 +       BUG_ON(!ent);
11846 +       au_dr_hino_del(dr, ent);
11847 +       au_kfree_rcu(ent);
11848 +
11849 +out:
11850 +       au_kfree_try_rcu(rev);
11851 +       if (unlikely(err))
11852 +               pr_err("failed to remove dirren info\n");
11853 +}
11854 +
11855 +/* ---------------------------------------------------------------------- */
11856 +
11857 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11858 +                                          char *whname, int whnamelen,
11859 +                                          struct dentry **info_dentry)
11860 +{
11861 +       struct au_drinfo *drinfo;
11862 +       struct file *f;
11863 +       struct inode *h_dir;
11864 +       struct path infopath;
11865 +       int unlocked;
11866 +
11867 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11868 +
11869 +       *info_dentry = NULL;
11870 +       drinfo = NULL;
11871 +       unlocked = 0;
11872 +       h_dir = d_inode(h_ppath->dentry);
11873 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11874 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11875 +       if (IS_ERR(infopath.dentry)) {
11876 +               drinfo = (void *)infopath.dentry;
11877 +               goto out;
11878 +       }
11879 +
11880 +       if (d_is_negative(infopath.dentry))
11881 +               goto out_dput; /* success */
11882 +
11883 +       infopath.mnt = h_ppath->mnt;
11884 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11885 +       inode_unlock_shared(h_dir);
11886 +       unlocked = 1;
11887 +       if (IS_ERR(f)) {
11888 +               drinfo = (void *)f;
11889 +               goto out_dput;
11890 +       }
11891 +
11892 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11893 +       if (IS_ERR_OR_NULL(drinfo))
11894 +               goto out_fput;
11895 +
11896 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11897 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11898 +
11899 +out_fput:
11900 +       fput(f);
11901 +out_dput:
11902 +       dput(infopath.dentry);
11903 +out:
11904 +       if (!unlocked)
11905 +               inode_unlock_shared(h_dir);
11906 +       AuTraceErrPtr(drinfo);
11907 +       return drinfo;
11908 +}
11909 +
11910 +struct au_drinfo_do_load_args {
11911 +       struct au_drinfo **drinfop;
11912 +       struct path *h_ppath;
11913 +       char *whname;
11914 +       int whnamelen;
11915 +       struct dentry **info_dentry;
11916 +};
11917 +
11918 +static void au_call_drinfo_do_load(void *args)
11919 +{
11920 +       struct au_drinfo_do_load_args *a = args;
11921 +
11922 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11923 +                                       a->info_dentry);
11924 +}
11925 +
11926 +struct au_drinfo_load {
11927 +       struct path h_ppath;
11928 +       struct qstr *qname;
11929 +       unsigned char no_sio;
11930 +
11931 +       aufs_bindex_t ninfo;
11932 +       struct au_drinfo **drinfo;
11933 +};
11934 +
11935 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11936 +                         struct au_branch *br)
11937 +{
11938 +       int err, wkq_err, whnamelen, e;
11939 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11940 +               = AUFS_WH_DR_INFO_PFX;
11941 +       struct au_drinfo *drinfo;
11942 +       struct qstr oldname;
11943 +       struct inode *h_dir, *delegated;
11944 +       struct dentry *info_dentry;
11945 +       struct path infopath;
11946 +
11947 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11948 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11949 +                                   sizeof(whname) - whnamelen);
11950 +       if (w->no_sio)
11951 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11952 +                                          &info_dentry);
11953 +       else {
11954 +               struct au_drinfo_do_load_args args = {
11955 +                       .drinfop        = &drinfo,
11956 +                       .h_ppath        = &w->h_ppath,
11957 +                       .whname         = whname,
11958 +                       .whnamelen      = whnamelen,
11959 +                       .info_dentry    = &info_dentry
11960 +               };
11961 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11962 +               if (unlikely(wkq_err))
11963 +                       drinfo = ERR_PTR(wkq_err);
11964 +       }
11965 +       err = PTR_ERR(drinfo);
11966 +       if (IS_ERR_OR_NULL(drinfo))
11967 +               goto out;
11968 +
11969 +       err = 0;
11970 +       oldname.len = drinfo->oldnamelen;
11971 +       oldname.name = drinfo->oldname;
11972 +       if (au_qstreq(w->qname, &oldname)) {
11973 +               /* the name is renamed back */
11974 +               au_kfree_rcu(drinfo);
11975 +               drinfo = NULL;
11976 +
11977 +               infopath.dentry = info_dentry;
11978 +               infopath.mnt = w->h_ppath.mnt;
11979 +               h_dir = d_inode(w->h_ppath.dentry);
11980 +               delegated = NULL;
11981 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11982 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11983 +               inode_unlock(h_dir);
11984 +               if (unlikely(e))
11985 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11986 +               if (unlikely(e == -EWOULDBLOCK))
11987 +                       iput(delegated);
11988 +       }
11989 +       au_kfree_rcu(w->drinfo[bindex]);
11990 +       w->drinfo[bindex] = drinfo;
11991 +       dput(info_dentry);
11992 +
11993 +out:
11994 +       AuTraceErr(err);
11995 +       return err;
11996 +}
11997 +
11998 +/* ---------------------------------------------------------------------- */
11999 +
12000 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12001 +{
12002 +       struct au_drinfo **p = drinfo;
12003 +
12004 +       while (n-- > 0)
12005 +               au_kfree_rcu(*drinfo++);
12006 +       au_kfree_try_rcu(p);
12007 +}
12008 +
12009 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12010 +              aufs_bindex_t btgt)
12011 +{
12012 +       int err, ninfo;
12013 +       struct au_drinfo_load w;
12014 +       aufs_bindex_t bindex, bbot;
12015 +       struct au_branch *br;
12016 +       struct inode *h_dir;
12017 +       struct au_dr_hino *ent;
12018 +       struct super_block *sb;
12019 +
12020 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12021 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12022 +             AuLNPair(&lkup->whname), btgt);
12023 +
12024 +       sb = dentry->d_sb;
12025 +       bbot = au_sbbot(sb);
12026 +       w.ninfo = bbot + 1;
12027 +       if (!lkup->dirren.drinfo) {
12028 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12029 +                                             sizeof(*lkup->dirren.drinfo),
12030 +                                             GFP_NOFS);
12031 +               if (unlikely(!lkup->dirren.drinfo)) {
12032 +                       err = -ENOMEM;
12033 +                       goto out;
12034 +               }
12035 +               lkup->dirren.ninfo = w.ninfo;
12036 +       }
12037 +       w.drinfo = lkup->dirren.drinfo;
12038 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12039 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12040 +       AuDebugOn(!w.h_ppath.dentry);
12041 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12042 +       w.qname = &dentry->d_name;
12043 +
12044 +       ninfo = 0;
12045 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12046 +               br = au_sbr(sb, bindex);
12047 +               err = au_drinfo_load(&w, bindex, br);
12048 +               if (unlikely(err))
12049 +                       goto out_free;
12050 +               if (w.drinfo[bindex])
12051 +                       ninfo++;
12052 +       }
12053 +       if (!ninfo) {
12054 +               br = au_sbr(sb, btgt);
12055 +               h_dir = d_inode(w.h_ppath.dentry);
12056 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12057 +               AuDebugOn(!ent);
12058 +               au_dr_hino_del(&br->br_dirren, ent);
12059 +               au_kfree_rcu(ent);
12060 +       }
12061 +       goto out; /* success */
12062 +
12063 +out_free:
12064 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12065 +       lkup->dirren.ninfo = 0;
12066 +       lkup->dirren.drinfo = NULL;
12067 +out:
12068 +       AuTraceErr(err);
12069 +       return err;
12070 +}
12071 +
12072 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12073 +{
12074 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12075 +}
12076 +
12077 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12078 +{
12079 +       int err;
12080 +       struct au_drinfo *drinfo;
12081 +
12082 +       err = 0;
12083 +       if (!lkup->dirren.drinfo)
12084 +               goto out;
12085 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12086 +       drinfo = lkup->dirren.drinfo[btgt];
12087 +       if (!drinfo)
12088 +               goto out;
12089 +
12090 +       au_kfree_try_rcu(lkup->whname.name);
12091 +       lkup->whname.name = NULL;
12092 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12093 +       lkup->dirren.dr_name.name = drinfo->oldname;
12094 +       lkup->name = &lkup->dirren.dr_name;
12095 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12096 +       if (!err)
12097 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12098 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12099 +                     btgt);
12100 +
12101 +out:
12102 +       AuTraceErr(err);
12103 +       return err;
12104 +}
12105 +
12106 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12107 +                    ino_t h_ino)
12108 +{
12109 +       int match;
12110 +       struct au_drinfo *drinfo;
12111 +
12112 +       match = 1;
12113 +       if (!lkup->dirren.drinfo)
12114 +               goto out;
12115 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12116 +       drinfo = lkup->dirren.drinfo[bindex];
12117 +       if (!drinfo)
12118 +               goto out;
12119 +
12120 +       match = (drinfo->ino == h_ino);
12121 +       AuDbg("match %d\n", match);
12122 +
12123 +out:
12124 +       return match;
12125 +}
12126 +
12127 +/* ---------------------------------------------------------------------- */
12128 +
12129 +int au_dr_opt_set(struct super_block *sb)
12130 +{
12131 +       int err;
12132 +       aufs_bindex_t bindex, bbot;
12133 +       struct au_branch *br;
12134 +
12135 +       err = 0;
12136 +       bbot = au_sbbot(sb);
12137 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12138 +               br = au_sbr(sb, bindex);
12139 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12140 +       }
12141 +
12142 +       return err;
12143 +}
12144 +
12145 +int au_dr_opt_flush(struct super_block *sb)
12146 +{
12147 +       int err;
12148 +       aufs_bindex_t bindex, bbot;
12149 +       struct au_branch *br;
12150 +
12151 +       err = 0;
12152 +       bbot = au_sbbot(sb);
12153 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12154 +               br = au_sbr(sb, bindex);
12155 +               if (au_br_writable(br->br_perm))
12156 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12157 +       }
12158 +
12159 +       return err;
12160 +}
12161 +
12162 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12163 +{
12164 +       int err;
12165 +       aufs_bindex_t bindex, bbot;
12166 +       struct au_branch *br;
12167 +
12168 +       err = 0;
12169 +       if (!no_flush) {
12170 +               err = au_dr_opt_flush(sb);
12171 +               if (unlikely(err))
12172 +                       goto out;
12173 +       }
12174 +
12175 +       bbot = au_sbbot(sb);
12176 +       for (bindex = 0; bindex <= bbot; bindex++) {
12177 +               br = au_sbr(sb, bindex);
12178 +               au_dr_hino_free(&br->br_dirren);
12179 +       }
12180 +
12181 +out:
12182 +       return err;
12183 +}
12184 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12185 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12186 +++ linux/fs/aufs/dirren.h      2022-11-05 23:02:18.962555950 +0100
12187 @@ -0,0 +1,140 @@
12188 +/* SPDX-License-Identifier: GPL-2.0 */
12189 +/*
12190 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12191 + *
12192 + * This program is free software; you can redistribute it and/or modify
12193 + * it under the terms of the GNU General Public License as published by
12194 + * the Free Software Foundation; either version 2 of the License, or
12195 + * (at your option) any later version.
12196 + *
12197 + * This program is distributed in the hope that it will be useful,
12198 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12199 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12200 + * GNU General Public License for more details.
12201 + *
12202 + * You should have received a copy of the GNU General Public License
12203 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12204 + */
12205 +
12206 +/*
12207 + * renamed dir info
12208 + */
12209 +
12210 +#ifndef __AUFS_DIRREN_H__
12211 +#define __AUFS_DIRREN_H__
12212 +
12213 +#ifdef __KERNEL__
12214 +
12215 +#include <linux/dcache.h>
12216 +#include <linux/statfs.h>
12217 +#include <linux/uuid.h>
12218 +#include "hbl.h"
12219 +
12220 +#define AuDirren_NHASH 100
12221 +
12222 +#ifdef CONFIG_AUFS_DIRREN
12223 +enum au_brid_type {
12224 +       AuBrid_Unset,
12225 +       AuBrid_UUID,
12226 +       AuBrid_FSID,
12227 +       AuBrid_DEV
12228 +};
12229 +
12230 +struct au_dr_brid {
12231 +       enum au_brid_type       type;
12232 +       union {
12233 +               uuid_t  uuid;   /* unimplemented yet */
12234 +               fsid_t  fsid;
12235 +               dev_t   dev;
12236 +       };
12237 +};
12238 +
12239 +/* 20 is the max digits length of ulong 64 */
12240 +/* brid-type "_" uuid "_" inum */
12241 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12242 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12243 +
12244 +struct au_dr_hino {
12245 +       struct hlist_bl_node    dr_hnode;
12246 +       ino_t                   dr_h_ino;
12247 +};
12248 +
12249 +struct au_dr_br {
12250 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12251 +       struct au_dr_brid       dr_brid;
12252 +};
12253 +
12254 +struct au_dr_lookup {
12255 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12256 +       struct qstr             dr_name; /* subset of dr_info */
12257 +       aufs_bindex_t           ninfo;
12258 +       struct au_drinfo        **drinfo;
12259 +};
12260 +#else
12261 +struct au_dr_hino;
12262 +/* empty */
12263 +struct au_dr_br { };
12264 +struct au_dr_lookup { };
12265 +#endif
12266 +
12267 +/* ---------------------------------------------------------------------- */
12268 +
12269 +struct au_branch;
12270 +struct au_do_lookup_args;
12271 +struct au_hinode;
12272 +#ifdef CONFIG_AUFS_DIRREN
12273 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12274 +                       struct au_dr_hino *add_ent);
12275 +void au_dr_hino_free(struct au_dr_br *dr);
12276 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12277 +                 const struct path *path);
12278 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12279 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12280 +                struct qstr *dst_name, void *_rev);
12281 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12282 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12283 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12284 +              aufs_bindex_t bindex);
12285 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12286 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12287 +                    ino_t h_ino);
12288 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12289 +int au_dr_opt_set(struct super_block *sb);
12290 +int au_dr_opt_flush(struct super_block *sb);
12291 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12292 +#else
12293 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12294 +          struct au_dr_hino *add_ent);
12295 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12296 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12297 +          const struct path *path);
12298 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12299 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12300 +          struct qstr *dst_name, void *_rev);
12301 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12302 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12303 +          void *rev);
12304 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12305 +          aufs_bindex_t bindex);
12306 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12307 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12308 +          aufs_bindex_t bindex, ino_t h_ino);
12309 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12310 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12311 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12312 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12313 +#endif
12314 +
12315 +/* ---------------------------------------------------------------------- */
12316 +
12317 +#ifdef CONFIG_AUFS_DIRREN
12318 +static inline int au_dr_ihash(ino_t h_ino)
12319 +{
12320 +       return h_ino % AuDirren_NHASH;
12321 +}
12322 +#else
12323 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12324 +#endif
12325 +
12326 +#endif /* __KERNEL__ */
12327 +#endif /* __AUFS_DIRREN_H__ */
12328 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12329 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12330 +++ linux/fs/aufs/dynop.c       2024-03-10 23:40:47.083143450 +0100
12331 @@ -0,0 +1,366 @@
12332 +// SPDX-License-Identifier: GPL-2.0
12333 +/*
12334 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12335 + *
12336 + * This program is free software; you can redistribute it and/or modify
12337 + * it under the terms of the GNU General Public License as published by
12338 + * the Free Software Foundation; either version 2 of the License, or
12339 + * (at your option) any later version.
12340 + *
12341 + * This program is distributed in the hope that it will be useful,
12342 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12343 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12344 + * GNU General Public License for more details.
12345 + *
12346 + * You should have received a copy of the GNU General Public License
12347 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12348 + */
12349 +
12350 +/*
12351 + * dynamically customizable operations for regular files
12352 + */
12353 +
12354 +#include "aufs.h"
12355 +
12356 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12357 +
12358 +/*
12359 + * How large will these lists be?
12360 + * Usually just a few elements, 20-30 at most for each, I guess.
12361 + */
12362 +static struct hlist_bl_head dynop[AuDyLast];
12363 +
12364 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12365 +                                    const void *h_op)
12366 +{
12367 +       struct au_dykey *key, *tmp;
12368 +       struct hlist_bl_node *pos;
12369 +
12370 +       key = NULL;
12371 +       hlist_bl_lock(hbl);
12372 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12373 +               if (tmp->dk_op.dy_hop == h_op) {
12374 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12375 +                               key = tmp;
12376 +                       break;
12377 +               }
12378 +       hlist_bl_unlock(hbl);
12379 +
12380 +       return key;
12381 +}
12382 +
12383 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12384 +{
12385 +       struct au_dykey **k, *found;
12386 +       const void *h_op = key->dk_op.dy_hop;
12387 +       int i;
12388 +
12389 +       found = NULL;
12390 +       k = br->br_dykey;
12391 +       for (i = 0; i < AuBrDynOp; i++)
12392 +               if (k[i]) {
12393 +                       if (k[i]->dk_op.dy_hop == h_op) {
12394 +                               found = k[i];
12395 +                               break;
12396 +                       }
12397 +               } else
12398 +                       break;
12399 +       if (!found) {
12400 +               spin_lock(&br->br_dykey_lock);
12401 +               for (; i < AuBrDynOp; i++)
12402 +                       if (k[i]) {
12403 +                               if (k[i]->dk_op.dy_hop == h_op) {
12404 +                                       found = k[i];
12405 +                                       break;
12406 +                               }
12407 +                       } else {
12408 +                               k[i] = key;
12409 +                               break;
12410 +                       }
12411 +               spin_unlock(&br->br_dykey_lock);
12412 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12413 +       }
12414 +
12415 +       return found;
12416 +}
12417 +
12418 +/* kref_get() if @key is already added */
12419 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12420 +{
12421 +       struct au_dykey *tmp, *found;
12422 +       struct hlist_bl_node *pos;
12423 +       const void *h_op = key->dk_op.dy_hop;
12424 +
12425 +       found = NULL;
12426 +       hlist_bl_lock(hbl);
12427 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12428 +               if (tmp->dk_op.dy_hop == h_op) {
12429 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12430 +                               found = tmp;
12431 +                       break;
12432 +               }
12433 +       if (!found)
12434 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12435 +       hlist_bl_unlock(hbl);
12436 +
12437 +       if (!found)
12438 +               DyPrSym(key);
12439 +       return found;
12440 +}
12441 +
12442 +static void dy_free_rcu(struct rcu_head *rcu)
12443 +{
12444 +       struct au_dykey *key;
12445 +
12446 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12447 +       DyPrSym(key);
12448 +       kfree(key);
12449 +}
12450 +
12451 +static void dy_free(struct kref *kref)
12452 +{
12453 +       struct au_dykey *key;
12454 +       struct hlist_bl_head *hbl;
12455 +
12456 +       key = container_of(kref, struct au_dykey, dk_kref);
12457 +       hbl = dynop + key->dk_op.dy_type;
12458 +       au_hbl_del(&key->dk_hnode, hbl);
12459 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12460 +}
12461 +
12462 +void au_dy_put(struct au_dykey *key)
12463 +{
12464 +       kref_put(&key->dk_kref, dy_free);
12465 +}
12466 +
12467 +/* ---------------------------------------------------------------------- */
12468 +
12469 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12470 +
12471 +#ifdef CONFIG_AUFS_DEBUG
12472 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12473 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12474 +#else
12475 +#define DyDbgDeclare(cnt)      do {} while (0)
12476 +#define DyDbgInc(cnt)          do {} while (0)
12477 +#endif
12478 +
12479 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12480 +       DyDbgInc(cnt);                                                  \
12481 +       if (h_op->func) {                                               \
12482 +               if (src.func)                                           \
12483 +                       dst.func = src.func;                            \
12484 +               else                                                    \
12485 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12486 +       }                                                               \
12487 +} while (0)
12488 +
12489 +#define DySetForce(func, dst, src) do {                \
12490 +       AuDebugOn(!src.func);                   \
12491 +       DyDbgInc(cnt);                          \
12492 +       dst.func = src.func;                    \
12493 +} while (0)
12494 +
12495 +#define DySetAop(func) \
12496 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12497 +#define DySetAopForce(func) \
12498 +       DySetForce(func, dyaop->da_op, aufs_aop)
12499 +
12500 +static void dy_aop(struct au_dykey *key, const void *h_op,
12501 +                  struct super_block *h_sb __maybe_unused)
12502 +{
12503 +       struct au_dyaop *dyaop = (void *)key;
12504 +       const struct address_space_operations *h_aop = h_op;
12505 +       DyDbgDeclare(cnt);
12506 +
12507 +       AuDbg("%s\n", au_sbtype(h_sb));
12508 +
12509 +       DySetAop(writepage);
12510 +       DySetAopForce(read_folio);      /* force */
12511 +       DySetAop(writepages);
12512 +       DySetAop(dirty_folio);
12513 +       DySetAop(invalidate_folio);
12514 +       DySetAop(readahead);
12515 +       DySetAop(write_begin);
12516 +       DySetAop(write_end);
12517 +       DySetAop(bmap);
12518 +       DySetAop(release_folio);
12519 +       DySetAop(free_folio);
12520 +       /* this one will be changed according to an aufs mount option */
12521 +       DySetAop(direct_IO);
12522 +       DySetAop(migrate_folio);
12523 +       DySetAop(launder_folio);
12524 +       DySetAop(is_partially_uptodate);
12525 +       DySetAop(is_dirty_writeback);
12526 +       DySetAop(error_remove_folio);
12527 +       DySetAop(swap_activate);
12528 +       DySetAop(swap_deactivate);
12529 +       DySetAop(swap_rw);
12530 +
12531 +       DyDbgSize(cnt, *h_aop);
12532 +}
12533 +
12534 +/* ---------------------------------------------------------------------- */
12535 +
12536 +static void dy_bug(struct kref *kref)
12537 +{
12538 +       BUG();
12539 +}
12540 +
12541 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12542 +{
12543 +       struct au_dykey *key, *old;
12544 +       struct hlist_bl_head *hbl;
12545 +       struct op {
12546 +               unsigned int sz;
12547 +               void (*set)(struct au_dykey *key, const void *h_op,
12548 +                           struct super_block *h_sb __maybe_unused);
12549 +       };
12550 +       static const struct op a[] = {
12551 +               [AuDy_AOP] = {
12552 +                       .sz     = sizeof(struct au_dyaop),
12553 +                       .set    = dy_aop
12554 +               }
12555 +       };
12556 +       const struct op *p;
12557 +
12558 +       hbl = dynop + op->dy_type;
12559 +       key = dy_gfind_get(hbl, op->dy_hop);
12560 +       if (key)
12561 +               goto out_add; /* success */
12562 +
12563 +       p = a + op->dy_type;
12564 +       key = kzalloc(p->sz, GFP_NOFS);
12565 +       if (unlikely(!key)) {
12566 +               key = ERR_PTR(-ENOMEM);
12567 +               goto out;
12568 +       }
12569 +
12570 +       key->dk_op.dy_hop = op->dy_hop;
12571 +       kref_init(&key->dk_kref);
12572 +       p->set(key, op->dy_hop, au_br_sb(br));
12573 +       old = dy_gadd(hbl, key);
12574 +       if (old) {
12575 +               au_kfree_rcu(key);
12576 +               key = old;
12577 +       }
12578 +
12579 +out_add:
12580 +       old = dy_bradd(br, key);
12581 +       if (old)
12582 +               /* its ref-count should never be zero here */
12583 +               kref_put(&key->dk_kref, dy_bug);
12584 +out:
12585 +       return key;
12586 +}
12587 +
12588 +/* ---------------------------------------------------------------------- */
12589 +/*
12590 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12591 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12592 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12593 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12594 + * See the aufs manual in detail.
12595 + */
12596 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12597 +{
12598 +       if (!do_dx)
12599 +               dyaop->da_op.direct_IO = NULL;
12600 +       else
12601 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12602 +}
12603 +
12604 +static struct au_dyaop *dy_aget(struct au_branch *br,
12605 +                               const struct address_space_operations *h_aop,
12606 +                               int do_dx)
12607 +{
12608 +       struct au_dyaop *dyaop;
12609 +       struct au_dynop op;
12610 +
12611 +       op.dy_type = AuDy_AOP;
12612 +       op.dy_haop = h_aop;
12613 +       dyaop = (void *)dy_get(&op, br);
12614 +       if (IS_ERR(dyaop))
12615 +               goto out;
12616 +       dy_adx(dyaop, do_dx);
12617 +
12618 +out:
12619 +       return dyaop;
12620 +}
12621 +
12622 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12623 +               struct inode *h_inode)
12624 +{
12625 +       int err, do_dx;
12626 +       struct super_block *sb;
12627 +       struct au_branch *br;
12628 +       struct au_dyaop *dyaop;
12629 +
12630 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12631 +       IiMustWriteLock(inode);
12632 +
12633 +       sb = inode->i_sb;
12634 +       br = au_sbr(sb, bindex);
12635 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12636 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12637 +       err = PTR_ERR(dyaop);
12638 +       if (IS_ERR(dyaop))
12639 +               /* unnecessary to call dy_fput() */
12640 +               goto out;
12641 +
12642 +       err = 0;
12643 +       inode->i_mapping->a_ops = &dyaop->da_op;
12644 +
12645 +out:
12646 +       return err;
12647 +}
12648 +
12649 +/*
12650 + * Is it safe to replace a_ops during the inode/file is in operation?
12651 + * Yes, I hope so.
12652 + */
12653 +int au_dy_irefresh(struct inode *inode)
12654 +{
12655 +       int err;
12656 +       aufs_bindex_t btop;
12657 +       struct inode *h_inode;
12658 +
12659 +       err = 0;
12660 +       if (S_ISREG(inode->i_mode)) {
12661 +               btop = au_ibtop(inode);
12662 +               h_inode = au_h_iptr(inode, btop);
12663 +               err = au_dy_iaop(inode, btop, h_inode);
12664 +       }
12665 +       return err;
12666 +}
12667 +
12668 +void au_dy_arefresh(int do_dx)
12669 +{
12670 +       struct hlist_bl_head *hbl;
12671 +       struct hlist_bl_node *pos;
12672 +       struct au_dykey *key;
12673 +
12674 +       hbl = dynop + AuDy_AOP;
12675 +       hlist_bl_lock(hbl);
12676 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12677 +               dy_adx((void *)key, do_dx);
12678 +       hlist_bl_unlock(hbl);
12679 +}
12680 +
12681 +/* ---------------------------------------------------------------------- */
12682 +
12683 +void __init au_dy_init(void)
12684 +{
12685 +       int i;
12686 +
12687 +       for (i = 0; i < AuDyLast; i++)
12688 +               INIT_HLIST_BL_HEAD(dynop + i);
12689 +}
12690 +
12691 +void au_dy_fin(void)
12692 +{
12693 +       int i;
12694 +
12695 +       for (i = 0; i < AuDyLast; i++)
12696 +               WARN_ON(!hlist_bl_empty(dynop + i));
12697 +}
12698 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12699 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12700 +++ linux/fs/aufs/dynop.h       2022-11-05 23:02:18.962555950 +0100
12701 @@ -0,0 +1,77 @@
12702 +/* SPDX-License-Identifier: GPL-2.0 */
12703 +/*
12704 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12705 + *
12706 + * This program is free software; you can redistribute it and/or modify
12707 + * it under the terms of the GNU General Public License as published by
12708 + * the Free Software Foundation; either version 2 of the License, or
12709 + * (at your option) any later version.
12710 + *
12711 + * This program is distributed in the hope that it will be useful,
12712 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12713 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12714 + * GNU General Public License for more details.
12715 + *
12716 + * You should have received a copy of the GNU General Public License
12717 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12718 + */
12719 +
12720 +/*
12721 + * dynamically customizable operations (for regular files only)
12722 + */
12723 +
12724 +#ifndef __AUFS_DYNOP_H__
12725 +#define __AUFS_DYNOP_H__
12726 +
12727 +#ifdef __KERNEL__
12728 +
12729 +#include <linux/fs.h>
12730 +#include <linux/kref.h>
12731 +
12732 +enum {AuDy_AOP, AuDyLast};
12733 +
12734 +struct au_dynop {
12735 +       int                                             dy_type;
12736 +       union {
12737 +               const void                              *dy_hop;
12738 +               const struct address_space_operations   *dy_haop;
12739 +       };
12740 +};
12741 +
12742 +struct au_dykey {
12743 +       union {
12744 +               struct hlist_bl_node    dk_hnode;
12745 +               struct rcu_head         dk_rcu;
12746 +       };
12747 +       struct au_dynop         dk_op;
12748 +
12749 +       /*
12750 +        * during I am in the branch local array, kref is gotten. when the
12751 +        * branch is removed, kref is put.
12752 +        */
12753 +       struct kref             dk_kref;
12754 +};
12755 +
12756 +/* stop unioning since their sizes are very different from each other */
12757 +struct au_dyaop {
12758 +       struct au_dykey                 da_key;
12759 +       struct address_space_operations da_op; /* not const */
12760 +};
12761 +/* make sure that 'struct au_dykey *' can be any type */
12762 +static_assert(!offsetof(struct au_dyaop, da_key));
12763 +
12764 +/* ---------------------------------------------------------------------- */
12765 +
12766 +/* dynop.c */
12767 +struct au_branch;
12768 +void au_dy_put(struct au_dykey *key);
12769 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12770 +               struct inode *h_inode);
12771 +int au_dy_irefresh(struct inode *inode);
12772 +void au_dy_arefresh(int do_dio);
12773 +
12774 +void __init au_dy_init(void);
12775 +void au_dy_fin(void);
12776 +
12777 +#endif /* __KERNEL__ */
12778 +#endif /* __AUFS_DYNOP_H__ */
12779 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12780 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12781 +++ linux/fs/aufs/export.c      2024-03-10 23:40:47.083143450 +0100
12782 @@ -0,0 +1,846 @@
12783 +// SPDX-License-Identifier: GPL-2.0
12784 +/*
12785 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12786 + *
12787 + * This program is free software; you can redistribute it and/or modify
12788 + * it under the terms of the GNU General Public License as published by
12789 + * the Free Software Foundation; either version 2 of the License, or
12790 + * (at your option) any later version.
12791 + *
12792 + * This program is distributed in the hope that it will be useful,
12793 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12794 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12795 + * GNU General Public License for more details.
12796 + *
12797 + * You should have received a copy of the GNU General Public License
12798 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12799 + */
12800 +
12801 +/*
12802 + * export via nfs
12803 + */
12804 +
12805 +#include <linux/exportfs.h>
12806 +#include <linux/fs_struct.h>
12807 +#include <linux/nsproxy.h>
12808 +#include <linux/random.h>
12809 +#include <linux/writeback.h>
12810 +#include "aufs.h"
12811 +
12812 +union conv {
12813 +#ifdef CONFIG_AUFS_INO_T_64
12814 +       __u32 a[2];
12815 +#else
12816 +       __u32 a[1];
12817 +#endif
12818 +       ino_t ino;
12819 +};
12820 +
12821 +static ino_t decode_ino(__u32 *a)
12822 +{
12823 +       union conv u;
12824 +
12825 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12826 +       u.a[0] = a[0];
12827 +#ifdef CONFIG_AUFS_INO_T_64
12828 +       u.a[1] = a[1];
12829 +#endif
12830 +       return u.ino;
12831 +}
12832 +
12833 +static void encode_ino(__u32 *a, ino_t ino)
12834 +{
12835 +       union conv u;
12836 +
12837 +       u.ino = ino;
12838 +       a[0] = u.a[0];
12839 +#ifdef CONFIG_AUFS_INO_T_64
12840 +       a[1] = u.a[1];
12841 +#endif
12842 +}
12843 +
12844 +/* NFS file handle */
12845 +enum {
12846 +       Fh_br_id,
12847 +       Fh_sigen,
12848 +#ifdef CONFIG_AUFS_INO_T_64
12849 +       /* support 64bit inode number */
12850 +       Fh_ino1,
12851 +       Fh_ino2,
12852 +       Fh_dir_ino1,
12853 +       Fh_dir_ino2,
12854 +#else
12855 +       Fh_ino1,
12856 +       Fh_dir_ino1,
12857 +#endif
12858 +       Fh_igen,
12859 +       Fh_h_type,
12860 +       Fh_tail,
12861 +
12862 +       Fh_ino = Fh_ino1,
12863 +       Fh_dir_ino = Fh_dir_ino1
12864 +};
12865 +
12866 +static int au_test_anon(struct dentry *dentry)
12867 +{
12868 +       /* note: read d_flags without d_lock */
12869 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12870 +}
12871 +
12872 +int au_test_nfsd(void)
12873 +{
12874 +       int ret;
12875 +       struct task_struct *tsk = current;
12876 +       char comm[sizeof(tsk->comm)];
12877 +
12878 +       ret = 0;
12879 +       if (tsk->flags & PF_KTHREAD) {
12880 +               get_task_comm(comm, tsk);
12881 +               ret = !strcmp(comm, "nfsd");
12882 +       }
12883 +
12884 +       return ret;
12885 +}
12886 +
12887 +/* ---------------------------------------------------------------------- */
12888 +/* inode generation external table */
12889 +
12890 +void au_xigen_inc(struct inode *inode)
12891 +{
12892 +       loff_t pos;
12893 +       ssize_t sz;
12894 +       __u32 igen;
12895 +       struct super_block *sb;
12896 +       struct au_sbinfo *sbinfo;
12897 +
12898 +       sb = inode->i_sb;
12899 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12900 +
12901 +       sbinfo = au_sbi(sb);
12902 +       pos = inode->i_ino;
12903 +       pos *= sizeof(igen);
12904 +       igen = inode->i_generation + 1;
12905 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12906 +       if (sz == sizeof(igen))
12907 +               return; /* success */
12908 +
12909 +       if (unlikely(sz >= 0))
12910 +               AuIOErr("xigen error (%zd)\n", sz);
12911 +}
12912 +
12913 +int au_xigen_new(struct inode *inode)
12914 +{
12915 +       int err;
12916 +       loff_t pos;
12917 +       ssize_t sz;
12918 +       struct super_block *sb;
12919 +       struct au_sbinfo *sbinfo;
12920 +       struct file *file;
12921 +
12922 +       err = 0;
12923 +       /* todo: dirty, at mount time */
12924 +       if (inode->i_ino == AUFS_ROOT_INO)
12925 +               goto out;
12926 +       sb = inode->i_sb;
12927 +       SiMustAnyLock(sb);
12928 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12929 +               goto out;
12930 +
12931 +       err = -EFBIG;
12932 +       pos = inode->i_ino;
12933 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12934 +               AuIOErr1("too large i%lld\n", pos);
12935 +               goto out;
12936 +       }
12937 +       pos *= sizeof(inode->i_generation);
12938 +
12939 +       err = 0;
12940 +       sbinfo = au_sbi(sb);
12941 +       file = sbinfo->si_xigen;
12942 +       BUG_ON(!file);
12943 +
12944 +       if (vfsub_f_size_read(file)
12945 +           < pos + sizeof(inode->i_generation)) {
12946 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12947 +               sz = xino_fwrite(file, &inode->i_generation,
12948 +                                sizeof(inode->i_generation), &pos);
12949 +       } else
12950 +               sz = xino_fread(file, &inode->i_generation,
12951 +                               sizeof(inode->i_generation), &pos);
12952 +       if (sz == sizeof(inode->i_generation))
12953 +               goto out; /* success */
12954 +
12955 +       err = sz;
12956 +       if (unlikely(sz >= 0)) {
12957 +               err = -EIO;
12958 +               AuIOErr("xigen error (%zd)\n", sz);
12959 +       }
12960 +
12961 +out:
12962 +       return err;
12963 +}
12964 +
12965 +int au_xigen_set(struct super_block *sb, struct path *path)
12966 +{
12967 +       int err;
12968 +       struct au_sbinfo *sbinfo;
12969 +       struct file *file;
12970 +
12971 +       SiMustWriteLock(sb);
12972 +
12973 +       sbinfo = au_sbi(sb);
12974 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12975 +       err = PTR_ERR(file);
12976 +       if (IS_ERR(file))
12977 +               goto out;
12978 +       err = 0;
12979 +       if (sbinfo->si_xigen)
12980 +               fput(sbinfo->si_xigen);
12981 +       sbinfo->si_xigen = file;
12982 +
12983 +out:
12984 +       AuTraceErr(err);
12985 +       return err;
12986 +}
12987 +
12988 +void au_xigen_clr(struct super_block *sb)
12989 +{
12990 +       struct au_sbinfo *sbinfo;
12991 +
12992 +       SiMustWriteLock(sb);
12993 +
12994 +       sbinfo = au_sbi(sb);
12995 +       if (sbinfo->si_xigen) {
12996 +               fput(sbinfo->si_xigen);
12997 +               sbinfo->si_xigen = NULL;
12998 +       }
12999 +}
13000 +
13001 +/* ---------------------------------------------------------------------- */
13002 +
13003 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13004 +                                   ino_t dir_ino)
13005 +{
13006 +       struct dentry *dentry, *d;
13007 +       struct inode *inode;
13008 +       unsigned int sigen;
13009 +
13010 +       dentry = NULL;
13011 +       inode = ilookup(sb, ino);
13012 +       if (!inode)
13013 +               goto out;
13014 +
13015 +       dentry = ERR_PTR(-ESTALE);
13016 +       sigen = au_sigen(sb);
13017 +       if (unlikely(au_is_bad_inode(inode)
13018 +                    || IS_DEADDIR(inode)
13019 +                    || sigen != au_iigen(inode, NULL)))
13020 +               goto out_iput;
13021 +
13022 +       dentry = NULL;
13023 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13024 +               dentry = d_find_alias(inode);
13025 +       else {
13026 +               spin_lock(&inode->i_lock);
13027 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13028 +                       spin_lock(&d->d_lock);
13029 +                       if (!au_test_anon(d)
13030 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13031 +                               dentry = dget_dlock(d);
13032 +                               spin_unlock(&d->d_lock);
13033 +                               break;
13034 +                       }
13035 +                       spin_unlock(&d->d_lock);
13036 +               }
13037 +               spin_unlock(&inode->i_lock);
13038 +       }
13039 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13040 +               /* need to refresh */
13041 +               dput(dentry);
13042 +               dentry = NULL;
13043 +       }
13044 +
13045 +out_iput:
13046 +       iput(inode);
13047 +out:
13048 +       AuTraceErrPtr(dentry);
13049 +       return dentry;
13050 +}
13051 +
13052 +/* ---------------------------------------------------------------------- */
13053 +
13054 +/* todo: dirty? */
13055 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13056 +
13057 +struct au_compare_mnt_args {
13058 +       /* input */
13059 +       struct super_block *sb;
13060 +
13061 +       /* output */
13062 +       struct vfsmount *mnt;
13063 +};
13064 +
13065 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13066 +{
13067 +       struct au_compare_mnt_args *a = arg;
13068 +
13069 +       if (mnt->mnt_sb != a->sb)
13070 +               return 0;
13071 +       a->mnt = mntget(mnt);
13072 +       return 1;
13073 +}
13074 +
13075 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13076 +{
13077 +       int err;
13078 +       struct path root;
13079 +       struct vfsmount *mnt;
13080 +       struct au_compare_mnt_args args = {
13081 +               .sb = sb
13082 +       };
13083 +
13084 +       get_fs_root(current->fs, &root);
13085 +       /*
13086 +        * as long as this sb is alive, this temporary unlock is safe.
13087 +        * Really?
13088 +        */
13089 +       si_read_unlock(sb);
13090 +       mnt = collect_mounts(&root);
13091 +       if (IS_ERR(mnt)) {
13092 +               args.mnt = mnt;
13093 +               goto out;
13094 +       }
13095 +
13096 +       rcu_read_lock();
13097 +       err = iterate_mounts(au_compare_mnt, &args, mnt);
13098 +       rcu_read_unlock();
13099 +       drop_collected_mounts(mnt);
13100 +       AuDebugOn(!err);
13101 +
13102 +out:
13103 +       si_noflush_read_lock(sb);
13104 +       AuDebugOn(!args.mnt);
13105 +       path_put(&root);
13106 +       return args.mnt;
13107 +}
13108 +
13109 +struct au_nfsd_si_lock {
13110 +       unsigned int sigen;
13111 +       aufs_bindex_t bindex, br_id;
13112 +       unsigned char force_lock;
13113 +};
13114 +
13115 +static int si_nfsd_read_lock(struct super_block *sb,
13116 +                            struct au_nfsd_si_lock *nsi_lock)
13117 +{
13118 +       int err;
13119 +       aufs_bindex_t bindex;
13120 +
13121 +       si_read_lock(sb, AuLock_FLUSH);
13122 +
13123 +       /* branch id may be wrapped around */
13124 +       err = 0;
13125 +       bindex = au_br_index(sb, nsi_lock->br_id);
13126 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13127 +               goto out; /* success */
13128 +
13129 +       err = -ESTALE;
13130 +       bindex = -1;
13131 +       if (!nsi_lock->force_lock)
13132 +               si_read_unlock(sb);
13133 +
13134 +out:
13135 +       nsi_lock->bindex = bindex;
13136 +       return err;
13137 +}
13138 +
13139 +struct find_name_by_ino {
13140 +       struct dir_context ctx;
13141 +       int called, found;
13142 +       ino_t ino;
13143 +       char *name;
13144 +       int namelen;
13145 +};
13146 +
13147 +static bool
13148 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13149 +                loff_t offset, u64 ino, unsigned int d_type)
13150 +{
13151 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13152 +                                                 ctx);
13153 +
13154 +       a->called++;
13155 +       if (a->ino != ino)
13156 +               return true;
13157 +
13158 +       memcpy(a->name, name, namelen);
13159 +       a->namelen = namelen;
13160 +       a->found = 1;
13161 +       return false;
13162 +}
13163 +
13164 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13165 +                                    struct au_nfsd_si_lock *nsi_lock)
13166 +{
13167 +       struct dentry *dentry, *parent;
13168 +       struct file *file;
13169 +       struct inode *dir;
13170 +       struct find_name_by_ino arg = {
13171 +               .ctx = {
13172 +                       .actor = find_name_by_ino
13173 +               }
13174 +       };
13175 +       int err;
13176 +
13177 +       parent = path->dentry;
13178 +       if (nsi_lock)
13179 +               si_read_unlock(parent->d_sb);
13180 +       file = vfsub_dentry_open(path, au_dir_roflags);
13181 +       dentry = (void *)file;
13182 +       if (IS_ERR(file))
13183 +               goto out;
13184 +
13185 +       dentry = ERR_PTR(-ENOMEM);
13186 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13187 +       if (unlikely(!arg.name))
13188 +               goto out_file;
13189 +       arg.ino = ino;
13190 +       arg.found = 0;
13191 +       do {
13192 +               arg.called = 0;
13193 +               /* smp_mb(); */
13194 +               err = vfsub_iterate_dir(file, &arg.ctx);
13195 +       } while (!err && !arg.found && arg.called);
13196 +       dentry = ERR_PTR(err);
13197 +       if (unlikely(err))
13198 +               goto out_name;
13199 +       /* instead of ENOENT */
13200 +       dentry = ERR_PTR(-ESTALE);
13201 +       if (!arg.found)
13202 +               goto out_name;
13203 +
13204 +       /* do not call vfsub_lkup_one() */
13205 +       dir = d_inode(parent);
13206 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13207 +       AuTraceErrPtr(dentry);
13208 +       if (IS_ERR(dentry))
13209 +               goto out_name;
13210 +       AuDebugOn(au_test_anon(dentry));
13211 +       if (unlikely(d_really_is_negative(dentry))) {
13212 +               dput(dentry);
13213 +               dentry = ERR_PTR(-ENOENT);
13214 +       }
13215 +
13216 +out_name:
13217 +       free_page((unsigned long)arg.name);
13218 +out_file:
13219 +       fput(file);
13220 +out:
13221 +       if (unlikely(nsi_lock
13222 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13223 +               if (!IS_ERR(dentry)) {
13224 +                       dput(dentry);
13225 +                       dentry = ERR_PTR(-ESTALE);
13226 +               }
13227 +       AuTraceErrPtr(dentry);
13228 +       return dentry;
13229 +}
13230 +
13231 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13232 +                                       ino_t dir_ino,
13233 +                                       struct au_nfsd_si_lock *nsi_lock)
13234 +{
13235 +       struct dentry *dentry;
13236 +       struct path path;
13237 +
13238 +       if (dir_ino != AUFS_ROOT_INO) {
13239 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13240 +               dentry = path.dentry;
13241 +               if (!path.dentry || IS_ERR(path.dentry))
13242 +                       goto out;
13243 +               AuDebugOn(au_test_anon(path.dentry));
13244 +       } else
13245 +               path.dentry = dget(sb->s_root);
13246 +
13247 +       path.mnt = au_mnt_get(sb);
13248 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13249 +       path_put(&path);
13250 +
13251 +out:
13252 +       AuTraceErrPtr(dentry);
13253 +       return dentry;
13254 +}
13255 +
13256 +/* ---------------------------------------------------------------------- */
13257 +
13258 +static int h_acceptable(void *expv, struct dentry *dentry)
13259 +{
13260 +       return 1;
13261 +}
13262 +
13263 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13264 +                          char *buf, int len, struct super_block *sb)
13265 +{
13266 +       char *p;
13267 +       int n;
13268 +       struct path path;
13269 +
13270 +       p = d_path(h_rootpath, buf, len);
13271 +       if (IS_ERR(p))
13272 +               goto out;
13273 +       n = strlen(p);
13274 +
13275 +       path.mnt = h_rootpath->mnt;
13276 +       path.dentry = h_parent;
13277 +       p = d_path(&path, buf, len);
13278 +       if (IS_ERR(p))
13279 +               goto out;
13280 +       if (n != 1)
13281 +               p += n;
13282 +
13283 +       path.mnt = au_mnt_get(sb);
13284 +       path.dentry = sb->s_root;
13285 +       p = d_path(&path, buf, len - strlen(p));
13286 +       mntput(path.mnt);
13287 +       if (IS_ERR(p))
13288 +               goto out;
13289 +       if (n != 1)
13290 +               p[strlen(p)] = '/';
13291 +
13292 +out:
13293 +       AuTraceErrPtr(p);
13294 +       return p;
13295 +}
13296 +
13297 +static
13298 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13299 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13300 +{
13301 +       struct dentry *dentry, *h_parent, *root;
13302 +       struct super_block *h_sb;
13303 +       char *pathname, *p;
13304 +       struct vfsmount *h_mnt;
13305 +       struct au_branch *br;
13306 +       int err;
13307 +       struct path path;
13308 +
13309 +       br = au_sbr(sb, nsi_lock->bindex);
13310 +       h_mnt = au_br_mnt(br);
13311 +       h_sb = h_mnt->mnt_sb;
13312 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13313 +       lockdep_off();
13314 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13315 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13316 +                                     h_acceptable, /*context*/NULL);
13317 +       lockdep_on();
13318 +       dentry = h_parent;
13319 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13320 +               AuWarn1("%s decode_fh failed, %ld\n",
13321 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13322 +               goto out;
13323 +       }
13324 +       dentry = NULL;
13325 +       if (unlikely(au_test_anon(h_parent))) {
13326 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13327 +                       au_sbtype(h_sb));
13328 +               goto out_h_parent;
13329 +       }
13330 +
13331 +       dentry = ERR_PTR(-ENOMEM);
13332 +       pathname = (void *)__get_free_page(GFP_NOFS);
13333 +       if (unlikely(!pathname))
13334 +               goto out_h_parent;
13335 +
13336 +       root = sb->s_root;
13337 +       path.mnt = h_mnt;
13338 +       di_read_lock_parent(root, !AuLock_IR);
13339 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13340 +       di_read_unlock(root, !AuLock_IR);
13341 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13342 +       dentry = (void *)p;
13343 +       if (IS_ERR(p))
13344 +               goto out_pathname;
13345 +
13346 +       si_read_unlock(sb);
13347 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13348 +       dentry = ERR_PTR(err);
13349 +       if (unlikely(err))
13350 +               goto out_relock;
13351 +
13352 +       dentry = ERR_PTR(-ENOENT);
13353 +       AuDebugOn(au_test_anon(path.dentry));
13354 +       if (unlikely(d_really_is_negative(path.dentry)))
13355 +               goto out_path;
13356 +
13357 +       if (ino != d_inode(path.dentry)->i_ino)
13358 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13359 +       else
13360 +               dentry = dget(path.dentry);
13361 +
13362 +out_path:
13363 +       path_put(&path);
13364 +out_relock:
13365 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13366 +               if (!IS_ERR(dentry)) {
13367 +                       dput(dentry);
13368 +                       dentry = ERR_PTR(-ESTALE);
13369 +               }
13370 +out_pathname:
13371 +       free_page((unsigned long)pathname);
13372 +out_h_parent:
13373 +       dput(h_parent);
13374 +out:
13375 +       AuTraceErrPtr(dentry);
13376 +       return dentry;
13377 +}
13378 +
13379 +/* ---------------------------------------------------------------------- */
13380 +
13381 +static struct dentry *
13382 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13383 +                 int fh_type)
13384 +{
13385 +       struct dentry *dentry;
13386 +       __u32 *fh = fid->raw;
13387 +       struct au_branch *br;
13388 +       ino_t ino, dir_ino;
13389 +       struct au_nfsd_si_lock nsi_lock = {
13390 +               .force_lock     = 0
13391 +       };
13392 +
13393 +       dentry = ERR_PTR(-ESTALE);
13394 +       /* it should never happen, but the file handle is unreliable */
13395 +       if (unlikely(fh_len < Fh_tail))
13396 +               goto out;
13397 +       nsi_lock.sigen = fh[Fh_sigen];
13398 +       nsi_lock.br_id = fh[Fh_br_id];
13399 +
13400 +       /* branch id may be wrapped around */
13401 +       br = NULL;
13402 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13403 +               goto out;
13404 +       nsi_lock.force_lock = 1;
13405 +
13406 +       /* is this inode still cached? */
13407 +       ino = decode_ino(fh + Fh_ino);
13408 +       /* it should never happen */
13409 +       if (unlikely(ino == AUFS_ROOT_INO))
13410 +               goto out_unlock;
13411 +
13412 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13413 +       dentry = decode_by_ino(sb, ino, dir_ino);
13414 +       if (IS_ERR(dentry))
13415 +               goto out_unlock;
13416 +       if (dentry)
13417 +               goto accept;
13418 +
13419 +       /* is the parent dir cached? */
13420 +       br = au_sbr(sb, nsi_lock.bindex);
13421 +       au_lcnt_inc(&br->br_nfiles);
13422 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13423 +       if (IS_ERR(dentry))
13424 +               goto out_unlock;
13425 +       if (dentry)
13426 +               goto accept;
13427 +
13428 +       /* lookup path */
13429 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13430 +       if (IS_ERR(dentry))
13431 +               goto out_unlock;
13432 +       if (unlikely(!dentry))
13433 +               /* todo?: make it ESTALE */
13434 +               goto out_unlock;
13435 +
13436 +accept:
13437 +       if (!au_digen_test(dentry, au_sigen(sb))
13438 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13439 +               goto out_unlock; /* success */
13440 +
13441 +       dput(dentry);
13442 +       dentry = ERR_PTR(-ESTALE);
13443 +out_unlock:
13444 +       if (br)
13445 +               au_lcnt_dec(&br->br_nfiles);
13446 +       si_read_unlock(sb);
13447 +out:
13448 +       AuTraceErrPtr(dentry);
13449 +       return dentry;
13450 +}
13451 +
13452 +#if 0 /* reserved for future use */
13453 +/* support subtreecheck option */
13454 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13455 +                                       int fh_len, int fh_type)
13456 +{
13457 +       struct dentry *parent;
13458 +       __u32 *fh = fid->raw;
13459 +       ino_t dir_ino;
13460 +
13461 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13462 +       parent = decode_by_ino(sb, dir_ino, 0);
13463 +       if (IS_ERR(parent))
13464 +               goto out;
13465 +       if (!parent)
13466 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13467 +                                       dir_ino, fh, fh_len);
13468 +
13469 +out:
13470 +       AuTraceErrPtr(parent);
13471 +       return parent;
13472 +}
13473 +#endif
13474 +
13475 +/* ---------------------------------------------------------------------- */
13476 +
13477 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13478 +                         struct inode *dir)
13479 +{
13480 +       int err;
13481 +       aufs_bindex_t bindex;
13482 +       struct super_block *sb, *h_sb;
13483 +       struct dentry *dentry, *parent, *h_parent;
13484 +       struct inode *h_dir;
13485 +       struct au_branch *br;
13486 +
13487 +       err = -ENOSPC;
13488 +       if (unlikely(*max_len <= Fh_tail)) {
13489 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13490 +               goto out;
13491 +       }
13492 +
13493 +       err = FILEID_ROOT;
13494 +       if (inode->i_ino == AUFS_ROOT_INO) {
13495 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13496 +               goto out;
13497 +       }
13498 +
13499 +       h_parent = NULL;
13500 +       sb = inode->i_sb;
13501 +       err = si_read_lock(sb, AuLock_FLUSH);
13502 +       if (unlikely(err))
13503 +               goto out;
13504 +
13505 +#ifdef CONFIG_AUFS_DEBUG
13506 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13507 +               AuWarn1("NFS-exporting requires xino\n");
13508 +#endif
13509 +       err = -EIO;
13510 +       parent = NULL;
13511 +       ii_read_lock_child(inode);
13512 +       bindex = au_ibtop(inode);
13513 +       if (!dir) {
13514 +               dentry = d_find_any_alias(inode);
13515 +               if (unlikely(!dentry))
13516 +                       goto out_unlock;
13517 +               AuDebugOn(au_test_anon(dentry));
13518 +               parent = dget_parent(dentry);
13519 +               dput(dentry);
13520 +               if (unlikely(!parent))
13521 +                       goto out_unlock;
13522 +               if (d_really_is_positive(parent))
13523 +                       dir = d_inode(parent);
13524 +       }
13525 +
13526 +       ii_read_lock_parent(dir);
13527 +       h_dir = au_h_iptr(dir, bindex);
13528 +       ii_read_unlock(dir);
13529 +       if (unlikely(!h_dir))
13530 +               goto out_parent;
13531 +       h_parent = d_find_any_alias(h_dir);
13532 +       if (unlikely(!h_parent))
13533 +               goto out_hparent;
13534 +
13535 +       err = -EPERM;
13536 +       br = au_sbr(sb, bindex);
13537 +       h_sb = au_br_sb(br);
13538 +       if (unlikely(!h_sb->s_export_op)) {
13539 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13540 +               goto out_hparent;
13541 +       }
13542 +
13543 +       fh[Fh_br_id] = br->br_id;
13544 +       fh[Fh_sigen] = au_sigen(sb);
13545 +       encode_ino(fh + Fh_ino, inode->i_ino);
13546 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13547 +       fh[Fh_igen] = inode->i_generation;
13548 +
13549 +       *max_len -= Fh_tail;
13550 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13551 +                                          max_len,
13552 +                                          /*connectable or subtreecheck*/0);
13553 +       err = fh[Fh_h_type];
13554 +       *max_len += Fh_tail;
13555 +       /* todo: macros? */
13556 +       if (err != FILEID_INVALID)
13557 +               err = 99;
13558 +       else
13559 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13560 +
13561 +out_hparent:
13562 +       dput(h_parent);
13563 +out_parent:
13564 +       dput(parent);
13565 +out_unlock:
13566 +       ii_read_unlock(inode);
13567 +       si_read_unlock(sb);
13568 +out:
13569 +       if (unlikely(err < 0))
13570 +               err = FILEID_INVALID;
13571 +       return err;
13572 +}
13573 +
13574 +/* ---------------------------------------------------------------------- */
13575 +
13576 +static int aufs_commit_metadata(struct inode *inode)
13577 +{
13578 +       int err;
13579 +       aufs_bindex_t bindex;
13580 +       struct super_block *sb;
13581 +       struct inode *h_inode;
13582 +       int (*f)(struct inode *inode);
13583 +
13584 +       sb = inode->i_sb;
13585 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13586 +       ii_write_lock_child(inode);
13587 +       bindex = au_ibtop(inode);
13588 +       AuDebugOn(bindex < 0);
13589 +       h_inode = au_h_iptr(inode, bindex);
13590 +
13591 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13592 +       if (f)
13593 +               err = f(h_inode);
13594 +       else
13595 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13596 +
13597 +       au_cpup_attr_timesizes(inode);
13598 +       ii_write_unlock(inode);
13599 +       si_read_unlock(sb);
13600 +       return err;
13601 +}
13602 +
13603 +/* ---------------------------------------------------------------------- */
13604 +
13605 +static struct export_operations aufs_export_op = {
13606 +       .fh_to_dentry           = aufs_fh_to_dentry,
13607 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13608 +       .encode_fh              = aufs_encode_fh,
13609 +       .commit_metadata        = aufs_commit_metadata
13610 +};
13611 +
13612 +void au_export_init(struct super_block *sb)
13613 +{
13614 +       struct au_sbinfo *sbinfo;
13615 +       __u32 u;
13616 +
13617 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13618 +                        && IS_MODULE(CONFIG_EXPORTFS),
13619 +                        AUFS_NAME ": unsupported configuration "
13620 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13621 +
13622 +       sb->s_export_op = &aufs_export_op;
13623 +       sbinfo = au_sbi(sb);
13624 +       sbinfo->si_xigen = NULL;
13625 +       get_random_bytes(&u, sizeof(u));
13626 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13627 +       atomic_set(&sbinfo->si_xigen_next, u);
13628 +}
13629 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13630 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13631 +++ linux/fs/aufs/fhsm.c        2022-11-05 23:02:18.962555950 +0100
13632 @@ -0,0 +1,426 @@
13633 +// SPDX-License-Identifier: GPL-2.0
13634 +/*
13635 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13636 + *
13637 + * This program is free software; you can redistribute it and/or modify
13638 + * it under the terms of the GNU General Public License as published by
13639 + * the Free Software Foundation; either version 2 of the License, or
13640 + * (at your option) any later version.
13641 + *
13642 + * This program is distributed in the hope that it will be useful,
13643 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13644 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13645 + * GNU General Public License for more details.
13646 + *
13647 + * You should have received a copy of the GNU General Public License
13648 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13649 + */
13650 +
13651 +/*
13652 + * File-based Hierarchy Storage Management
13653 + */
13654 +
13655 +#include <linux/anon_inodes.h>
13656 +#include <linux/poll.h>
13657 +#include <linux/seq_file.h>
13658 +#include <linux/statfs.h>
13659 +#include "aufs.h"
13660 +
13661 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13662 +{
13663 +       struct au_sbinfo *sbinfo;
13664 +       struct au_fhsm *fhsm;
13665 +
13666 +       SiMustAnyLock(sb);
13667 +
13668 +       sbinfo = au_sbi(sb);
13669 +       fhsm = &sbinfo->si_fhsm;
13670 +       AuDebugOn(!fhsm);
13671 +       return fhsm->fhsm_bottom;
13672 +}
13673 +
13674 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13675 +{
13676 +       struct au_sbinfo *sbinfo;
13677 +       struct au_fhsm *fhsm;
13678 +
13679 +       SiMustWriteLock(sb);
13680 +
13681 +       sbinfo = au_sbi(sb);
13682 +       fhsm = &sbinfo->si_fhsm;
13683 +       AuDebugOn(!fhsm);
13684 +       fhsm->fhsm_bottom = bindex;
13685 +}
13686 +
13687 +/* ---------------------------------------------------------------------- */
13688 +
13689 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13690 +{
13691 +       struct au_br_fhsm *bf;
13692 +
13693 +       bf = br->br_fhsm;
13694 +       MtxMustLock(&bf->bf_lock);
13695 +
13696 +       return !bf->bf_readable
13697 +               || time_after(jiffies,
13698 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13699 +}
13700 +
13701 +/* ---------------------------------------------------------------------- */
13702 +
13703 +static void au_fhsm_notify(struct super_block *sb, int val)
13704 +{
13705 +       struct au_sbinfo *sbinfo;
13706 +       struct au_fhsm *fhsm;
13707 +
13708 +       SiMustAnyLock(sb);
13709 +
13710 +       sbinfo = au_sbi(sb);
13711 +       fhsm = &sbinfo->si_fhsm;
13712 +       if (au_fhsm_pid(fhsm)
13713 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13714 +               atomic_set(&fhsm->fhsm_readable, val);
13715 +               if (val)
13716 +                       wake_up(&fhsm->fhsm_wqh);
13717 +       }
13718 +}
13719 +
13720 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13721 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13722 +{
13723 +       int err;
13724 +       struct au_branch *br;
13725 +       struct au_br_fhsm *bf;
13726 +
13727 +       br = au_sbr(sb, bindex);
13728 +       AuDebugOn(au_br_rdonly(br));
13729 +       bf = br->br_fhsm;
13730 +       AuDebugOn(!bf);
13731 +
13732 +       if (do_lock)
13733 +               mutex_lock(&bf->bf_lock);
13734 +       else
13735 +               MtxMustLock(&bf->bf_lock);
13736 +
13737 +       /* sb->s_root for NFS is unreliable */
13738 +       err = au_br_stfs(br, &bf->bf_stfs);
13739 +       if (unlikely(err)) {
13740 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13741 +               goto out;
13742 +       }
13743 +
13744 +       bf->bf_jiffy = jiffies;
13745 +       bf->bf_readable = 1;
13746 +       if (do_notify)
13747 +               au_fhsm_notify(sb, /*val*/1);
13748 +       if (rstfs)
13749 +               *rstfs = bf->bf_stfs;
13750 +
13751 +out:
13752 +       if (do_lock)
13753 +               mutex_unlock(&bf->bf_lock);
13754 +       au_fhsm_notify(sb, /*val*/1);
13755 +
13756 +       return err;
13757 +}
13758 +
13759 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13760 +{
13761 +       int err;
13762 +       struct au_sbinfo *sbinfo;
13763 +       struct au_fhsm *fhsm;
13764 +       struct au_branch *br;
13765 +       struct au_br_fhsm *bf;
13766 +
13767 +       AuDbg("b%d, force %d\n", bindex, force);
13768 +       SiMustAnyLock(sb);
13769 +
13770 +       sbinfo = au_sbi(sb);
13771 +       fhsm = &sbinfo->si_fhsm;
13772 +       if (!au_ftest_si(sbinfo, FHSM)
13773 +           || fhsm->fhsm_bottom == bindex)
13774 +               return;
13775 +
13776 +       br = au_sbr(sb, bindex);
13777 +       bf = br->br_fhsm;
13778 +       AuDebugOn(!bf);
13779 +       mutex_lock(&bf->bf_lock);
13780 +       if (force
13781 +           || au_fhsm_pid(fhsm)
13782 +           || au_fhsm_test_jiffy(sbinfo, br))
13783 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13784 +                                 /*do_notify*/1);
13785 +       mutex_unlock(&bf->bf_lock);
13786 +}
13787 +
13788 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13789 +{
13790 +       aufs_bindex_t bindex, bbot;
13791 +       struct au_branch *br;
13792 +
13793 +       /* exclude the bottom */
13794 +       bbot = au_fhsm_bottom(sb);
13795 +       for (bindex = 0; bindex < bbot; bindex++) {
13796 +               br = au_sbr(sb, bindex);
13797 +               if (au_br_fhsm(br->br_perm))
13798 +                       au_fhsm_wrote(sb, bindex, force);
13799 +       }
13800 +}
13801 +
13802 +/* ---------------------------------------------------------------------- */
13803 +
13804 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13805 +{
13806 +       __poll_t mask;
13807 +       struct au_sbinfo *sbinfo;
13808 +       struct au_fhsm *fhsm;
13809 +
13810 +       mask = 0;
13811 +       sbinfo = file->private_data;
13812 +       fhsm = &sbinfo->si_fhsm;
13813 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13814 +       if (atomic_read(&fhsm->fhsm_readable))
13815 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13816 +
13817 +       if (!mask)
13818 +               AuDbg("mask 0x%x\n", mask);
13819 +       return mask;
13820 +}
13821 +
13822 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13823 +                             struct aufs_stfs *stfs, __s16 brid)
13824 +{
13825 +       int err;
13826 +
13827 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13828 +       if (!err)
13829 +               err = __put_user(brid, &stbr->brid);
13830 +       if (unlikely(err))
13831 +               err = -EFAULT;
13832 +
13833 +       return err;
13834 +}
13835 +
13836 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13837 +                              struct aufs_stbr __user *stbr, size_t count)
13838 +{
13839 +       ssize_t err;
13840 +       int nstbr;
13841 +       aufs_bindex_t bindex, bbot;
13842 +       struct au_branch *br;
13843 +       struct au_br_fhsm *bf;
13844 +
13845 +       /* except the bottom branch */
13846 +       err = 0;
13847 +       nstbr = 0;
13848 +       bbot = au_fhsm_bottom(sb);
13849 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13850 +               br = au_sbr(sb, bindex);
13851 +               if (!au_br_fhsm(br->br_perm))
13852 +                       continue;
13853 +
13854 +               bf = br->br_fhsm;
13855 +               mutex_lock(&bf->bf_lock);
13856 +               if (bf->bf_readable) {
13857 +                       err = -EFAULT;
13858 +                       if (count >= sizeof(*stbr))
13859 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13860 +                                                         br->br_id);
13861 +                       if (!err) {
13862 +                               bf->bf_readable = 0;
13863 +                               count -= sizeof(*stbr);
13864 +                               nstbr++;
13865 +                       }
13866 +               }
13867 +               mutex_unlock(&bf->bf_lock);
13868 +       }
13869 +       if (!err)
13870 +               err = sizeof(*stbr) * nstbr;
13871 +
13872 +       return err;
13873 +}
13874 +
13875 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13876 +                          loff_t *pos)
13877 +{
13878 +       ssize_t err;
13879 +       int readable;
13880 +       aufs_bindex_t nfhsm, bindex, bbot;
13881 +       struct au_sbinfo *sbinfo;
13882 +       struct au_fhsm *fhsm;
13883 +       struct au_branch *br;
13884 +       struct super_block *sb;
13885 +
13886 +       err = 0;
13887 +       sbinfo = file->private_data;
13888 +       fhsm = &sbinfo->si_fhsm;
13889 +need_data:
13890 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13891 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13892 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13893 +                       err = -EAGAIN;
13894 +               else
13895 +                       err = wait_event_interruptible_locked_irq
13896 +                               (fhsm->fhsm_wqh,
13897 +                                atomic_read(&fhsm->fhsm_readable));
13898 +       }
13899 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13900 +       if (unlikely(err))
13901 +               goto out;
13902 +
13903 +       /* sb may already be dead */
13904 +       au_rw_read_lock(&sbinfo->si_rwsem);
13905 +       readable = atomic_read(&fhsm->fhsm_readable);
13906 +       if (readable > 0) {
13907 +               sb = sbinfo->si_sb;
13908 +               AuDebugOn(!sb);
13909 +               /* exclude the bottom branch */
13910 +               nfhsm = 0;
13911 +               bbot = au_fhsm_bottom(sb);
13912 +               for (bindex = 0; bindex < bbot; bindex++) {
13913 +                       br = au_sbr(sb, bindex);
13914 +                       if (au_br_fhsm(br->br_perm))
13915 +                               nfhsm++;
13916 +               }
13917 +               err = -EMSGSIZE;
13918 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13919 +                       atomic_set(&fhsm->fhsm_readable, 0);
13920 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13921 +                                            count);
13922 +               }
13923 +       }
13924 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13925 +       if (!readable)
13926 +               goto need_data;
13927 +
13928 +out:
13929 +       return err;
13930 +}
13931 +
13932 +static int au_fhsm_release(struct inode *inode, struct file *file)
13933 +{
13934 +       struct au_sbinfo *sbinfo;
13935 +       struct au_fhsm *fhsm;
13936 +
13937 +       /* sb may already be dead */
13938 +       sbinfo = file->private_data;
13939 +       fhsm = &sbinfo->si_fhsm;
13940 +       spin_lock(&fhsm->fhsm_spin);
13941 +       fhsm->fhsm_pid = 0;
13942 +       spin_unlock(&fhsm->fhsm_spin);
13943 +       kobject_put(&sbinfo->si_kobj);
13944 +
13945 +       return 0;
13946 +}
13947 +
13948 +static const struct file_operations au_fhsm_fops = {
13949 +       .owner          = THIS_MODULE,
13950 +       .llseek         = noop_llseek,
13951 +       .read           = au_fhsm_read,
13952 +       .poll           = au_fhsm_poll,
13953 +       .release        = au_fhsm_release
13954 +};
13955 +
13956 +int au_fhsm_fd(struct super_block *sb, int oflags)
13957 +{
13958 +       int err, fd;
13959 +       struct au_sbinfo *sbinfo;
13960 +       struct au_fhsm *fhsm;
13961 +
13962 +       err = -EPERM;
13963 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13964 +               goto out;
13965 +
13966 +       err = -EINVAL;
13967 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13968 +               goto out;
13969 +
13970 +       err = 0;
13971 +       sbinfo = au_sbi(sb);
13972 +       fhsm = &sbinfo->si_fhsm;
13973 +       spin_lock(&fhsm->fhsm_spin);
13974 +       if (!fhsm->fhsm_pid)
13975 +               fhsm->fhsm_pid = current->pid;
13976 +       else
13977 +               err = -EBUSY;
13978 +       spin_unlock(&fhsm->fhsm_spin);
13979 +       if (unlikely(err))
13980 +               goto out;
13981 +
13982 +       oflags |= O_RDONLY;
13983 +       /* oflags |= FMODE_NONOTIFY; */
13984 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13985 +       err = fd;
13986 +       if (unlikely(fd < 0))
13987 +               goto out_pid;
13988 +
13989 +       /* succeed regardless 'fhsm' status */
13990 +       kobject_get(&sbinfo->si_kobj);
13991 +       si_noflush_read_lock(sb);
13992 +       if (au_ftest_si(sbinfo, FHSM))
13993 +               au_fhsm_wrote_all(sb, /*force*/0);
13994 +       si_read_unlock(sb);
13995 +       goto out; /* success */
13996 +
13997 +out_pid:
13998 +       spin_lock(&fhsm->fhsm_spin);
13999 +       fhsm->fhsm_pid = 0;
14000 +       spin_unlock(&fhsm->fhsm_spin);
14001 +out:
14002 +       AuTraceErr(err);
14003 +       return err;
14004 +}
14005 +
14006 +/* ---------------------------------------------------------------------- */
14007 +
14008 +int au_fhsm_br_alloc(struct au_branch *br)
14009 +{
14010 +       int err;
14011 +
14012 +       err = 0;
14013 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14014 +       if (br->br_fhsm)
14015 +               au_br_fhsm_init(br->br_fhsm);
14016 +       else
14017 +               err = -ENOMEM;
14018 +
14019 +       return err;
14020 +}
14021 +
14022 +/* ---------------------------------------------------------------------- */
14023 +
14024 +void au_fhsm_fin(struct super_block *sb)
14025 +{
14026 +       au_fhsm_notify(sb, /*val*/-1);
14027 +}
14028 +
14029 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14030 +{
14031 +       struct au_fhsm *fhsm;
14032 +
14033 +       fhsm = &sbinfo->si_fhsm;
14034 +       spin_lock_init(&fhsm->fhsm_spin);
14035 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14036 +       atomic_set(&fhsm->fhsm_readable, 0);
14037 +       fhsm->fhsm_expire
14038 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14039 +       fhsm->fhsm_bottom = -1;
14040 +}
14041 +
14042 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14043 +{
14044 +       sbinfo->si_fhsm.fhsm_expire
14045 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14046 +}
14047 +
14048 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14049 +{
14050 +       unsigned int u;
14051 +
14052 +       if (!au_ftest_si(sbinfo, FHSM))
14053 +               return;
14054 +
14055 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14056 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14057 +               seq_printf(seq, ",fhsm_sec=%u", u);
14058 +}
14059 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14060 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14061 +++ linux/fs/aufs/file.c        2024-03-10 23:40:47.083143450 +0100
14062 @@ -0,0 +1,865 @@
14063 +// SPDX-License-Identifier: GPL-2.0
14064 +/*
14065 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14066 + *
14067 + * This program is free software; you can redistribute it and/or modify
14068 + * it under the terms of the GNU General Public License as published by
14069 + * the Free Software Foundation; either version 2 of the License, or
14070 + * (at your option) any later version.
14071 + *
14072 + * This program is distributed in the hope that it will be useful,
14073 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14074 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14075 + * GNU General Public License for more details.
14076 + *
14077 + * You should have received a copy of the GNU General Public License
14078 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14079 + */
14080 +
14081 +/*
14082 + * handling file/dir, and address_space operation
14083 + */
14084 +
14085 +#ifdef CONFIG_AUFS_DEBUG
14086 +#include <linux/migrate.h>
14087 +#endif
14088 +#include <linux/pagemap.h>
14089 +#include "aufs.h"
14090 +
14091 +/* drop flags for writing */
14092 +unsigned int au_file_roflags(unsigned int flags)
14093 +{
14094 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14095 +       flags |= O_RDONLY | O_NOATIME;
14096 +       return flags;
14097 +}
14098 +
14099 +/* common functions to regular file and dir */
14100 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14101 +                      struct file *file, int force_wr)
14102 +{
14103 +       struct file *h_file;
14104 +       struct dentry *h_dentry;
14105 +       struct inode *h_inode;
14106 +       struct super_block *sb;
14107 +       struct au_branch *br;
14108 +       struct path h_path;
14109 +       int err;
14110 +
14111 +       /* a race condition can happen between open and unlink/rmdir */
14112 +       h_file = ERR_PTR(-ENOENT);
14113 +       h_dentry = au_h_dptr(dentry, bindex);
14114 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14115 +               goto out;
14116 +       h_inode = d_inode(h_dentry);
14117 +       spin_lock(&h_dentry->d_lock);
14118 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14119 +               /* || !d_inode(dentry)->i_nlink */
14120 +               ;
14121 +       spin_unlock(&h_dentry->d_lock);
14122 +       if (unlikely(err))
14123 +               goto out;
14124 +
14125 +       sb = dentry->d_sb;
14126 +       br = au_sbr(sb, bindex);
14127 +       err = au_br_test_oflag(flags, br);
14128 +       h_file = ERR_PTR(err);
14129 +       if (unlikely(err))
14130 +               goto out;
14131 +
14132 +       /* drop flags for writing */
14133 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14134 +               if (force_wr && !(flags & O_WRONLY))
14135 +                       force_wr = 0;
14136 +               flags = au_file_roflags(flags);
14137 +               if (force_wr) {
14138 +                       h_file = ERR_PTR(-EROFS);
14139 +                       flags = au_file_roflags(flags);
14140 +                       if (unlikely(vfsub_native_ro(h_inode)
14141 +                                    || IS_APPEND(h_inode)))
14142 +                               goto out;
14143 +                       flags &= ~O_ACCMODE;
14144 +                       flags |= O_WRONLY;
14145 +               }
14146 +       }
14147 +       flags &= ~O_CREAT;
14148 +       au_lcnt_inc(&br->br_nfiles);
14149 +       h_path.dentry = h_dentry;
14150 +       h_path.mnt = au_br_mnt(br);
14151 +       /*
14152 +        * vfs::backing_file_open() looks promising since it can get rid of
14153 +        * mm::vm_prfile approach from my mind.
14154 +        * but I keep current approach for a while.
14155 +        */
14156 +       h_file = vfsub_dentry_open(&h_path, flags);
14157 +       if (IS_ERR(h_file))
14158 +               goto out_br;
14159 +
14160 +       if (flags & __FMODE_EXEC) {
14161 +               err = deny_write_access(h_file);
14162 +               if (unlikely(err)) {
14163 +                       fput(h_file);
14164 +                       h_file = ERR_PTR(err);
14165 +                       goto out_br;
14166 +               }
14167 +       }
14168 +       fsnotify_open(h_file);
14169 +       goto out; /* success */
14170 +
14171 +out_br:
14172 +       au_lcnt_dec(&br->br_nfiles);
14173 +out:
14174 +       return h_file;
14175 +}
14176 +
14177 +static int au_cmoo(struct dentry *dentry)
14178 +{
14179 +       int err, cmoo, matched;
14180 +       unsigned int udba;
14181 +       struct path h_path;
14182 +       struct au_pin pin;
14183 +       struct au_cp_generic cpg = {
14184 +               .dentry = dentry,
14185 +               .bdst   = -1,
14186 +               .bsrc   = -1,
14187 +               .len    = -1,
14188 +               .pin    = &pin,
14189 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14190 +       };
14191 +       struct inode *delegated;
14192 +       struct super_block *sb;
14193 +       struct au_sbinfo *sbinfo;
14194 +       struct au_fhsm *fhsm;
14195 +       pid_t pid;
14196 +       struct au_branch *br;
14197 +       struct dentry *parent;
14198 +       struct au_hinode *hdir;
14199 +
14200 +       DiMustWriteLock(dentry);
14201 +       IiMustWriteLock(d_inode(dentry));
14202 +
14203 +       err = 0;
14204 +       if (IS_ROOT(dentry))
14205 +               goto out;
14206 +       cpg.bsrc = au_dbtop(dentry);
14207 +       if (!cpg.bsrc)
14208 +               goto out;
14209 +
14210 +       sb = dentry->d_sb;
14211 +       sbinfo = au_sbi(sb);
14212 +       fhsm = &sbinfo->si_fhsm;
14213 +       pid = au_fhsm_pid(fhsm);
14214 +       rcu_read_lock();
14215 +       matched = (pid
14216 +                  && (current->pid == pid
14217 +                      || rcu_dereference(current->real_parent)->pid == pid));
14218 +       rcu_read_unlock();
14219 +       if (matched)
14220 +               goto out;
14221 +
14222 +       br = au_sbr(sb, cpg.bsrc);
14223 +       cmoo = au_br_cmoo(br->br_perm);
14224 +       if (!cmoo)
14225 +               goto out;
14226 +       if (!d_is_reg(dentry))
14227 +               cmoo &= AuBrAttr_COO_ALL;
14228 +       if (!cmoo)
14229 +               goto out;
14230 +
14231 +       parent = dget_parent(dentry);
14232 +       di_write_lock_parent(parent);
14233 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14234 +       cpg.bdst = err;
14235 +       if (unlikely(err < 0)) {
14236 +               err = 0;        /* there is no upper writable branch */
14237 +               goto out_dgrade;
14238 +       }
14239 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14240 +
14241 +       /* do not respect the coo attrib for the target branch */
14242 +       err = au_cpup_dirs(dentry, cpg.bdst);
14243 +       if (unlikely(err))
14244 +               goto out_dgrade;
14245 +
14246 +       di_downgrade_lock(parent, AuLock_IR);
14247 +       udba = au_opt_udba(sb);
14248 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14249 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14250 +       if (unlikely(err))
14251 +               goto out_parent;
14252 +
14253 +       err = au_sio_cpup_simple(&cpg);
14254 +       au_unpin(&pin);
14255 +       if (unlikely(err))
14256 +               goto out_parent;
14257 +       if (!(cmoo & AuBrWAttr_MOO))
14258 +               goto out_parent; /* success */
14259 +
14260 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14261 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14262 +       if (unlikely(err))
14263 +               goto out_parent;
14264 +
14265 +       h_path.mnt = au_br_mnt(br);
14266 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14267 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14268 +       delegated = NULL;
14269 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14270 +       au_unpin(&pin);
14271 +       /* todo: keep h_dentry or not? */
14272 +       if (unlikely(err == -EWOULDBLOCK)) {
14273 +               pr_warn("cannot retry for NFSv4 delegation"
14274 +                       " for an internal unlink\n");
14275 +               iput(delegated);
14276 +       }
14277 +       if (unlikely(err)) {
14278 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14279 +                      dentry, err);
14280 +               err = 0;
14281 +       }
14282 +       goto out_parent; /* success */
14283 +
14284 +out_dgrade:
14285 +       di_downgrade_lock(parent, AuLock_IR);
14286 +out_parent:
14287 +       di_read_unlock(parent, AuLock_IR);
14288 +       dput(parent);
14289 +out:
14290 +       AuTraceErr(err);
14291 +       return err;
14292 +}
14293 +
14294 +int au_do_open(struct file *file, struct au_do_open_args *args)
14295 +{
14296 +       int err, aopen = args->aopen;
14297 +       struct dentry *dentry;
14298 +       struct au_finfo *finfo;
14299 +
14300 +       if (!aopen)
14301 +               err = au_finfo_init(file, args->fidir);
14302 +       else {
14303 +               lockdep_off();
14304 +               err = au_finfo_init(file, args->fidir);
14305 +               lockdep_on();
14306 +       }
14307 +       if (unlikely(err))
14308 +               goto out;
14309 +
14310 +       dentry = file->f_path.dentry;
14311 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14312 +       di_write_lock_child(dentry);
14313 +       err = au_cmoo(dentry);
14314 +       if (!err) {
14315 +               if (!aopen) {
14316 +                       err = args->open(file, vfsub_file_flags(file),
14317 +                                        au_di(dentry)->di_htmpfile);
14318 +                       di_write_unlock(dentry);
14319 +               } else {
14320 +                       di_downgrade_lock(dentry, AuLock_IR);
14321 +                       lockdep_off();
14322 +                       err = args->open(file, vfsub_file_flags(file),
14323 +                                        args->h_file);
14324 +                       lockdep_on();
14325 +                       di_read_unlock(dentry, AuLock_IR);
14326 +               }
14327 +       }
14328 +
14329 +       finfo = au_fi(file);
14330 +       if (!err) {
14331 +               finfo->fi_file = file;
14332 +               au_hbl_add(&finfo->fi_hlist,
14333 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14334 +       }
14335 +       if (!aopen)
14336 +               fi_write_unlock(file);
14337 +       else {
14338 +               lockdep_off();
14339 +               fi_write_unlock(file);
14340 +               lockdep_on();
14341 +       }
14342 +       if (unlikely(err)) {
14343 +               finfo->fi_hdir = NULL;
14344 +               au_finfo_fin(file);
14345 +       }
14346 +
14347 +out:
14348 +       AuTraceErr(err);
14349 +       return err;
14350 +}
14351 +
14352 +int au_reopen_nondir(struct file *file)
14353 +{
14354 +       int err;
14355 +       aufs_bindex_t btop;
14356 +       struct dentry *dentry;
14357 +       struct au_branch *br;
14358 +       struct file *h_file, *h_file_tmp;
14359 +
14360 +       dentry = file->f_path.dentry;
14361 +       btop = au_dbtop(dentry);
14362 +       br = au_sbr(dentry->d_sb, btop);
14363 +       h_file_tmp = NULL;
14364 +       if (au_fbtop(file) == btop) {
14365 +               h_file = au_hf_top(file);
14366 +               if (file->f_mode == h_file->f_mode)
14367 +                       return 0; /* success */
14368 +               h_file_tmp = h_file;
14369 +               get_file(h_file_tmp);
14370 +               au_lcnt_inc(&br->br_nfiles);
14371 +               au_set_h_fptr(file, btop, NULL);
14372 +       }
14373 +       AuDebugOn(au_fi(file)->fi_hdir);
14374 +       /*
14375 +        * it can happen
14376 +        * file exists on both of rw and ro
14377 +        * open --> dbtop and fbtop are both 0
14378 +        * prepend a branch as rw, "rw" become ro
14379 +        * remove rw/file
14380 +        * delete the top branch, "rw" becomes rw again
14381 +        *      --> dbtop is 1, fbtop is still 0
14382 +        * write --> fbtop is 0 but dbtop is 1
14383 +        */
14384 +       /* AuDebugOn(au_fbtop(file) < btop); */
14385 +
14386 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14387 +                          file, /*force_wr*/0);
14388 +       err = PTR_ERR(h_file);
14389 +       if (IS_ERR(h_file)) {
14390 +               if (h_file_tmp) {
14391 +                       /* revert */
14392 +                       au_set_h_fptr(file, btop, h_file_tmp);
14393 +                       h_file_tmp = NULL;
14394 +               }
14395 +               goto out; /* todo: close all? */
14396 +       }
14397 +
14398 +       err = 0;
14399 +       au_set_fbtop(file, btop);
14400 +       au_set_h_fptr(file, btop, h_file);
14401 +       au_update_figen(file);
14402 +       /* todo: necessary? */
14403 +       /* file->f_ra = h_file->f_ra; */
14404 +
14405 +out:
14406 +       if (h_file_tmp) {
14407 +               fput(h_file_tmp);
14408 +               au_lcnt_dec(&br->br_nfiles);
14409 +       }
14410 +       return err;
14411 +}
14412 +
14413 +/* ---------------------------------------------------------------------- */
14414 +
14415 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14416 +                       struct dentry *hi_wh)
14417 +{
14418 +       int err;
14419 +       aufs_bindex_t btop;
14420 +       struct au_dinfo *dinfo;
14421 +       struct dentry *h_dentry;
14422 +       struct au_hdentry *hdp;
14423 +
14424 +       dinfo = au_di(file->f_path.dentry);
14425 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14426 +
14427 +       btop = dinfo->di_btop;
14428 +       dinfo->di_btop = btgt;
14429 +       hdp = au_hdentry(dinfo, btgt);
14430 +       h_dentry = hdp->hd_dentry;
14431 +       hdp->hd_dentry = hi_wh;
14432 +       err = au_reopen_nondir(file);
14433 +       hdp->hd_dentry = h_dentry;
14434 +       dinfo->di_btop = btop;
14435 +
14436 +       return err;
14437 +}
14438 +
14439 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14440 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14441 +{
14442 +       int err;
14443 +       struct inode *inode, *h_inode;
14444 +       struct dentry *h_dentry, *hi_wh;
14445 +       struct au_cp_generic cpg = {
14446 +               .dentry = file->f_path.dentry,
14447 +               .bdst   = bcpup,
14448 +               .bsrc   = -1,
14449 +               .len    = len,
14450 +               .pin    = pin
14451 +       };
14452 +
14453 +       au_update_dbtop(cpg.dentry);
14454 +       inode = d_inode(cpg.dentry);
14455 +       h_inode = NULL;
14456 +       if (au_dbtop(cpg.dentry) <= bcpup
14457 +           && au_dbbot(cpg.dentry) >= bcpup) {
14458 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14459 +               if (h_dentry && d_is_positive(h_dentry))
14460 +                       h_inode = d_inode(h_dentry);
14461 +       }
14462 +       hi_wh = au_hi_wh(inode, bcpup);
14463 +       if (!hi_wh && !h_inode)
14464 +               err = au_sio_cpup_wh(&cpg, file);
14465 +       else
14466 +               /* already copied-up after unlink */
14467 +               err = au_reopen_wh(file, bcpup, hi_wh);
14468 +
14469 +       if (!err
14470 +           && (inode->i_nlink > 1
14471 +               || (inode->i_state & I_LINKABLE))
14472 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14473 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14474 +
14475 +       return err;
14476 +}
14477 +
14478 +/*
14479 + * prepare the @file for writing.
14480 + */
14481 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14482 +{
14483 +       int err;
14484 +       aufs_bindex_t dbtop;
14485 +       struct dentry *parent;
14486 +       struct inode *inode;
14487 +       struct super_block *sb;
14488 +       struct file *h_file;
14489 +       struct au_cp_generic cpg = {
14490 +               .dentry = file->f_path.dentry,
14491 +               .bdst   = -1,
14492 +               .bsrc   = -1,
14493 +               .len    = len,
14494 +               .pin    = pin,
14495 +               .flags  = AuCpup_DTIME
14496 +       };
14497 +
14498 +       sb = cpg.dentry->d_sb;
14499 +       inode = d_inode(cpg.dentry);
14500 +       cpg.bsrc = au_fbtop(file);
14501 +       err = au_test_ro(sb, cpg.bsrc, inode);
14502 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14503 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14504 +                            /*flags*/0);
14505 +               goto out;
14506 +       }
14507 +
14508 +       /* need to cpup or reopen */
14509 +       parent = dget_parent(cpg.dentry);
14510 +       di_write_lock_parent(parent);
14511 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14512 +       cpg.bdst = err;
14513 +       if (unlikely(err < 0))
14514 +               goto out_dgrade;
14515 +       err = 0;
14516 +
14517 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14518 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14519 +               if (unlikely(err))
14520 +                       goto out_dgrade;
14521 +       }
14522 +
14523 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14524 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14525 +       if (unlikely(err))
14526 +               goto out_dgrade;
14527 +
14528 +       dbtop = au_dbtop(cpg.dentry);
14529 +       if (dbtop <= cpg.bdst)
14530 +               cpg.bsrc = cpg.bdst;
14531 +
14532 +       if (dbtop <= cpg.bdst           /* just reopen */
14533 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14534 +               ) {
14535 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14536 +               if (IS_ERR(h_file))
14537 +                       err = PTR_ERR(h_file);
14538 +               else {
14539 +                       di_downgrade_lock(parent, AuLock_IR);
14540 +                       if (dbtop > cpg.bdst)
14541 +                               err = au_sio_cpup_simple(&cpg);
14542 +                       if (!err)
14543 +                               err = au_reopen_nondir(file);
14544 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14545 +               }
14546 +       } else {                        /* copyup as wh and reopen */
14547 +               /*
14548 +                * since writable hfsplus branch is not supported,
14549 +                * h_open_pre/post() are unnecessary.
14550 +                */
14551 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14552 +               di_downgrade_lock(parent, AuLock_IR);
14553 +       }
14554 +
14555 +       if (!err) {
14556 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14557 +               goto out_dput; /* success */
14558 +       }
14559 +       au_unpin(pin);
14560 +       goto out_unlock;
14561 +
14562 +out_dgrade:
14563 +       di_downgrade_lock(parent, AuLock_IR);
14564 +out_unlock:
14565 +       di_read_unlock(parent, AuLock_IR);
14566 +out_dput:
14567 +       dput(parent);
14568 +out:
14569 +       return err;
14570 +}
14571 +
14572 +/* ---------------------------------------------------------------------- */
14573 +
14574 +int au_do_flush(struct file *file, fl_owner_t id,
14575 +               int (*flush)(struct file *file, fl_owner_t id))
14576 +{
14577 +       int err;
14578 +       struct super_block *sb;
14579 +       struct inode *inode;
14580 +
14581 +       inode = file_inode(file);
14582 +       sb = inode->i_sb;
14583 +       si_noflush_read_lock(sb);
14584 +       fi_read_lock(file);
14585 +       ii_read_lock_child(inode);
14586 +
14587 +       err = flush(file, id);
14588 +       au_cpup_attr_timesizes(inode);
14589 +
14590 +       ii_read_unlock(inode);
14591 +       fi_read_unlock(file);
14592 +       si_read_unlock(sb);
14593 +       return err;
14594 +}
14595 +
14596 +/* ---------------------------------------------------------------------- */
14597 +
14598 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14599 +{
14600 +       int err;
14601 +       struct au_pin pin;
14602 +       struct au_finfo *finfo;
14603 +       struct dentry *parent, *hi_wh;
14604 +       struct inode *inode;
14605 +       struct super_block *sb;
14606 +       struct au_cp_generic cpg = {
14607 +               .dentry = file->f_path.dentry,
14608 +               .bdst   = -1,
14609 +               .bsrc   = -1,
14610 +               .len    = -1,
14611 +               .pin    = &pin,
14612 +               .flags  = AuCpup_DTIME
14613 +       };
14614 +
14615 +       FiMustWriteLock(file);
14616 +
14617 +       err = 0;
14618 +       finfo = au_fi(file);
14619 +       sb = cpg.dentry->d_sb;
14620 +       inode = d_inode(cpg.dentry);
14621 +       cpg.bdst = au_ibtop(inode);
14622 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14623 +               goto out;
14624 +
14625 +       parent = dget_parent(cpg.dentry);
14626 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14627 +               di_read_lock_parent(parent, !AuLock_IR);
14628 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14629 +               cpg.bdst = err;
14630 +               di_read_unlock(parent, !AuLock_IR);
14631 +               if (unlikely(err < 0))
14632 +                       goto out_parent;
14633 +               err = 0;
14634 +       }
14635 +
14636 +       di_read_lock_parent(parent, AuLock_IR);
14637 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14638 +       if (!S_ISDIR(inode->i_mode)
14639 +           && au_opt_test(au_mntflags(sb), PLINK)
14640 +           && au_plink_test(inode)
14641 +           && !d_unhashed(cpg.dentry)
14642 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14643 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14644 +               if (unlikely(err))
14645 +                       goto out_unlock;
14646 +
14647 +               /* always superio. */
14648 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14649 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14650 +               if (!err) {
14651 +                       err = au_sio_cpup_simple(&cpg);
14652 +                       au_unpin(&pin);
14653 +               }
14654 +       } else if (hi_wh) {
14655 +               /* already copied-up after unlink */
14656 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14657 +               *need_reopen = 0;
14658 +       }
14659 +
14660 +out_unlock:
14661 +       di_read_unlock(parent, AuLock_IR);
14662 +out_parent:
14663 +       dput(parent);
14664 +out:
14665 +       return err;
14666 +}
14667 +
14668 +static void au_do_refresh_dir(struct file *file)
14669 +{
14670 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14671 +       struct au_hfile *p, tmp, *q;
14672 +       struct au_finfo *finfo;
14673 +       struct super_block *sb;
14674 +       struct au_fidir *fidir;
14675 +
14676 +       FiMustWriteLock(file);
14677 +
14678 +       sb = file->f_path.dentry->d_sb;
14679 +       finfo = au_fi(file);
14680 +       fidir = finfo->fi_hdir;
14681 +       AuDebugOn(!fidir);
14682 +       p = fidir->fd_hfile + finfo->fi_btop;
14683 +       brid = p->hf_br->br_id;
14684 +       bbot = fidir->fd_bbot;
14685 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14686 +               if (!p->hf_file)
14687 +                       continue;
14688 +
14689 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14690 +               if (new_bindex == bindex)
14691 +                       continue;
14692 +               if (new_bindex < 0) {
14693 +                       au_set_h_fptr(file, bindex, NULL);
14694 +                       continue;
14695 +               }
14696 +
14697 +               /* swap two lower inode, and loop again */
14698 +               q = fidir->fd_hfile + new_bindex;
14699 +               tmp = *q;
14700 +               *q = *p;
14701 +               *p = tmp;
14702 +               if (tmp.hf_file) {
14703 +                       bindex--;
14704 +                       p--;
14705 +               }
14706 +       }
14707 +
14708 +       p = fidir->fd_hfile;
14709 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14710 +               bbot = au_sbbot(sb);
14711 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14712 +                    finfo->fi_btop++, p++)
14713 +                       if (p->hf_file) {
14714 +                               if (file_inode(p->hf_file))
14715 +                                       break;
14716 +                               au_hfput(p, /*execed*/0);
14717 +                       }
14718 +       } else {
14719 +               bbot = au_br_index(sb, brid);
14720 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14721 +                    finfo->fi_btop++, p++)
14722 +                       if (p->hf_file)
14723 +                               au_hfput(p, /*execed*/0);
14724 +               bbot = au_sbbot(sb);
14725 +       }
14726 +
14727 +       p = fidir->fd_hfile + bbot;
14728 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14729 +            fidir->fd_bbot--, p--)
14730 +               if (p->hf_file) {
14731 +                       if (file_inode(p->hf_file))
14732 +                               break;
14733 +                       au_hfput(p, /*execed*/0);
14734 +               }
14735 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14736 +}
14737 +
14738 +/*
14739 + * after branch manipulating, refresh the file.
14740 + */
14741 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14742 +{
14743 +       int err, need_reopen, nbr;
14744 +       aufs_bindex_t bbot, bindex;
14745 +       struct dentry *dentry;
14746 +       struct super_block *sb;
14747 +       struct au_finfo *finfo;
14748 +       struct au_hfile *hfile;
14749 +
14750 +       dentry = file->f_path.dentry;
14751 +       sb = dentry->d_sb;
14752 +       nbr = au_sbbot(sb) + 1;
14753 +       finfo = au_fi(file);
14754 +       if (!finfo->fi_hdir) {
14755 +               hfile = &finfo->fi_htop;
14756 +               AuDebugOn(!hfile->hf_file);
14757 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14758 +               AuDebugOn(bindex < 0);
14759 +               if (bindex != finfo->fi_btop)
14760 +                       au_set_fbtop(file, bindex);
14761 +       } else {
14762 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14763 +               if (unlikely(err))
14764 +                       goto out;
14765 +               au_do_refresh_dir(file);
14766 +       }
14767 +
14768 +       err = 0;
14769 +       need_reopen = 1;
14770 +       if (!au_test_mmapped(file))
14771 +               err = au_file_refresh_by_inode(file, &need_reopen);
14772 +       if (finfo->fi_hdir)
14773 +               /* harmless if err */
14774 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14775 +       if (!err && need_reopen && !d_unlinked(dentry))
14776 +               err = reopen(file);
14777 +       if (!err) {
14778 +               au_update_figen(file);
14779 +               goto out; /* success */
14780 +       }
14781 +
14782 +       /* error, close all lower files */
14783 +       if (finfo->fi_hdir) {
14784 +               bbot = au_fbbot_dir(file);
14785 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14786 +                       au_set_h_fptr(file, bindex, NULL);
14787 +       }
14788 +
14789 +out:
14790 +       return err;
14791 +}
14792 +
14793 +/* common function to regular file and dir */
14794 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14795 +                         int wlock, unsigned int fi_lsc)
14796 +{
14797 +       int err;
14798 +       unsigned int sigen, figen;
14799 +       aufs_bindex_t btop;
14800 +       unsigned char pseudo_link;
14801 +       struct dentry *dentry;
14802 +       struct inode *inode;
14803 +
14804 +       err = 0;
14805 +       dentry = file->f_path.dentry;
14806 +       inode = d_inode(dentry);
14807 +       sigen = au_sigen(dentry->d_sb);
14808 +       fi_write_lock_nested(file, fi_lsc);
14809 +       figen = au_figen(file);
14810 +       if (!fi_lsc)
14811 +               di_write_lock_child(dentry);
14812 +       else
14813 +               di_write_lock_child2(dentry);
14814 +       btop = au_dbtop(dentry);
14815 +       pseudo_link = (btop != au_ibtop(inode));
14816 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14817 +               if (!wlock) {
14818 +                       di_downgrade_lock(dentry, AuLock_IR);
14819 +                       fi_downgrade_lock(file);
14820 +               }
14821 +               goto out; /* success */
14822 +       }
14823 +
14824 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14825 +       if (au_digen_test(dentry, sigen)) {
14826 +               err = au_reval_dpath(dentry, sigen);
14827 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14828 +       }
14829 +
14830 +       if (!err)
14831 +               err = refresh_file(file, reopen);
14832 +       if (!err) {
14833 +               if (!wlock) {
14834 +                       di_downgrade_lock(dentry, AuLock_IR);
14835 +                       fi_downgrade_lock(file);
14836 +               }
14837 +       } else {
14838 +               di_write_unlock(dentry);
14839 +               fi_write_unlock(file);
14840 +       }
14841 +
14842 +out:
14843 +       return err;
14844 +}
14845 +
14846 +/* ---------------------------------------------------------------------- */
14847 +
14848 +/* cf. aufs_nopage() */
14849 +/* for madvise(2) */
14850 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14851 +{
14852 +       folio_unlock(folio);
14853 +       return 0;
14854 +}
14855 +
14856 +/* it will never be called, but necessary to support O_DIRECT */
14857 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14858 +{ BUG(); return 0; }
14859 +
14860 +/* they will never be called. */
14861 +#ifdef CONFIG_AUFS_DEBUG
14862 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14863 +                           loff_t pos, unsigned len,
14864 +                           struct page **pagep, void **fsdata)
14865 +{ AuUnsupport(); return 0; }
14866 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14867 +                         loff_t pos, unsigned len, unsigned copied,
14868 +                         struct page *page, void *fsdata)
14869 +{ AuUnsupport(); return 0; }
14870 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14871 +{ AuUnsupport(); return 0; }
14872 +
14873 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14874 +{ AuUnsupport(); return true; }
14875 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14876 +{ AuUnsupport(); }
14877 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14878 +{ AuUnsupport(); return true; }
14879 +#if 0 /* called by memory compaction regardless file */
14880 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14881 +                             struct folio *src, enum migrate_mode mode)
14882 +{ AuUnsupport(); return 0; }
14883 +#endif
14884 +static int aufs_launder_folio(struct folio *folio)
14885 +{ AuUnsupport(); return 0; }
14886 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14887 +                                     size_t count)
14888 +{ AuUnsupport(); return true; }
14889 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14890 +                                   bool *writeback)
14891 +{ AuUnsupport(); }
14892 +static int aufs_error_remove_folio(struct address_space *mapping,
14893 +                                  struct folio *folio)
14894 +{ AuUnsupport(); return 0; }
14895 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14896 +                             sector_t *span)
14897 +{ AuUnsupport(); return 0; }
14898 +static void aufs_swap_deactivate(struct file *file)
14899 +{ AuUnsupport(); }
14900 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14901 +{ AuUnsupport(); return 0; }
14902 +#endif /* CONFIG_AUFS_DEBUG */
14903 +
14904 +const struct address_space_operations aufs_aop = {
14905 +       .read_folio             = aufs_read_folio,
14906 +       .direct_IO              = aufs_direct_IO,
14907 +#ifdef CONFIG_AUFS_DEBUG
14908 +       .writepage              = aufs_writepage,
14909 +       /* no writepages, because of writepage */
14910 +       .dirty_folio            = aufs_dirty_folio,
14911 +       /* no readpages, because of readpage */
14912 +       .write_begin            = aufs_write_begin,
14913 +       .write_end              = aufs_write_end,
14914 +       /* no bmap, no block device */
14915 +       .invalidate_folio       = aufs_invalidate_folio,
14916 +       .release_folio          = aufs_release_folio,
14917 +       /* is fallback_migrate_page ok? */
14918 +       /* .migrate_folio       = aufs_migrate_folio, */
14919 +       .launder_folio          = aufs_launder_folio,
14920 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14921 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14922 +       .error_remove_folio     = aufs_error_remove_folio,
14923 +       .swap_activate          = aufs_swap_activate,
14924 +       .swap_deactivate        = aufs_swap_deactivate,
14925 +       .swap_rw                = aufs_swap_rw
14926 +#endif /* CONFIG_AUFS_DEBUG */
14927 +};
14928 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14929 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14930 +++ linux/fs/aufs/file.h        2022-11-05 23:02:18.965889284 +0100
14931 @@ -0,0 +1,342 @@
14932 +/* SPDX-License-Identifier: GPL-2.0 */
14933 +/*
14934 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14935 + *
14936 + * This program is free software; you can redistribute it and/or modify
14937 + * it under the terms of the GNU General Public License as published by
14938 + * the Free Software Foundation; either version 2 of the License, or
14939 + * (at your option) any later version.
14940 + *
14941 + * This program is distributed in the hope that it will be useful,
14942 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14943 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14944 + * GNU General Public License for more details.
14945 + *
14946 + * You should have received a copy of the GNU General Public License
14947 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14948 + */
14949 +
14950 +/*
14951 + * file operations
14952 + */
14953 +
14954 +#ifndef __AUFS_FILE_H__
14955 +#define __AUFS_FILE_H__
14956 +
14957 +#ifdef __KERNEL__
14958 +
14959 +#include <linux/file.h>
14960 +#include <linux/fs.h>
14961 +#include <linux/mm_types.h>
14962 +#include <linux/poll.h>
14963 +#include "rwsem.h"
14964 +
14965 +struct au_branch;
14966 +struct au_hfile {
14967 +       struct file             *hf_file;
14968 +       struct au_branch        *hf_br;
14969 +};
14970 +
14971 +struct au_vdir;
14972 +struct au_fidir {
14973 +       aufs_bindex_t           fd_bbot;
14974 +       aufs_bindex_t           fd_nent;
14975 +       struct au_vdir          *fd_vdir_cache;
14976 +       struct au_hfile         fd_hfile[];
14977 +};
14978 +
14979 +static inline int au_fidir_sz(int nent)
14980 +{
14981 +       AuDebugOn(nent < 0);
14982 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14983 +}
14984 +
14985 +struct au_finfo {
14986 +       atomic_t                fi_generation;
14987 +
14988 +       struct au_rwsem         fi_rwsem;
14989 +       aufs_bindex_t           fi_btop;
14990 +
14991 +       /* do not union them */
14992 +       struct {                                /* for non-dir */
14993 +               struct au_hfile                 fi_htop;
14994 +               atomic_t                        fi_mmapped;
14995 +       };
14996 +       struct au_fidir         *fi_hdir;       /* for dir only */
14997 +
14998 +       struct hlist_bl_node    fi_hlist;
14999 +       struct file             *fi_file;       /* very ugly */
15000 +       struct rcu_head         rcu;
15001 +} ____cacheline_aligned_in_smp;
15002 +
15003 +/* ---------------------------------------------------------------------- */
15004 +
15005 +/* file.c */
15006 +extern const struct address_space_operations aufs_aop;
15007 +unsigned int au_file_roflags(unsigned int flags);
15008 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15009 +                      struct file *file, int force_wr);
15010 +struct au_do_open_args {
15011 +       int             aopen;
15012 +       int             (*open)(struct file *file, int flags,
15013 +                               struct file *h_file);
15014 +       struct au_fidir *fidir;
15015 +       struct file     *h_file;
15016 +};
15017 +int au_do_open(struct file *file, struct au_do_open_args *args);
15018 +int au_reopen_nondir(struct file *file);
15019 +struct au_pin;
15020 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15021 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15022 +                         int wlock, unsigned int fi_lsc);
15023 +int au_do_flush(struct file *file, fl_owner_t id,
15024 +               int (*flush)(struct file *file, fl_owner_t id));
15025 +
15026 +/* poll.c */
15027 +#ifdef CONFIG_AUFS_POLL
15028 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15029 +#endif
15030 +
15031 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15032 +/* hfsplus.c */
15033 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15034 +                          int force_wr);
15035 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15036 +                   struct file *h_file);
15037 +#else
15038 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15039 +       aufs_bindex_t bindex, int force_wr)
15040 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15041 +          struct file *h_file);
15042 +#endif
15043 +
15044 +/* f_op.c */
15045 +extern const struct file_operations aufs_file_fop;
15046 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15047 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15048 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15049 +
15050 +/* finfo.c */
15051 +void au_hfput(struct au_hfile *hf, int execed);
15052 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15053 +                  struct file *h_file);
15054 +
15055 +void au_update_figen(struct file *file);
15056 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15057 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15058 +
15059 +void au_fi_init_once(void *_fi);
15060 +void au_finfo_fin(struct file *file);
15061 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15062 +
15063 +/* ioctl.c */
15064 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15065 +#ifdef CONFIG_COMPAT
15066 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15067 +                          unsigned long arg);
15068 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15069 +                             unsigned long arg);
15070 +#endif
15071 +
15072 +/* ---------------------------------------------------------------------- */
15073 +
15074 +static inline struct au_finfo *au_fi(struct file *file)
15075 +{
15076 +       return file->private_data;
15077 +}
15078 +
15079 +/* ---------------------------------------------------------------------- */
15080 +
15081 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15082 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15083 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15084 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15085 +/*
15086 +#define fi_read_trylock_nested(f) \
15087 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15088 +#define fi_write_trylock_nested(f) \
15089 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15090 +*/
15091 +
15092 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15093 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15094 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15095 +
15096 +/* lock subclass for finfo */
15097 +enum {
15098 +       AuLsc_FI_1,
15099 +       AuLsc_FI_2
15100 +};
15101 +
15102 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15103 +{
15104 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15105 +}
15106 +
15107 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15108 +{
15109 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15110 +}
15111 +
15112 +/*
15113 + * fi_read_lock_1, fi_write_lock_1,
15114 + * fi_read_lock_2, fi_write_lock_2
15115 + */
15116 +#define AuReadLockFunc(name) \
15117 +static inline void fi_read_lock_##name(struct file *f) \
15118 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15119 +
15120 +#define AuWriteLockFunc(name) \
15121 +static inline void fi_write_lock_##name(struct file *f) \
15122 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15123 +
15124 +#define AuRWLockFuncs(name) \
15125 +       AuReadLockFunc(name) \
15126 +       AuWriteLockFunc(name)
15127 +
15128 +AuRWLockFuncs(1);
15129 +AuRWLockFuncs(2);
15130 +
15131 +#undef AuReadLockFunc
15132 +#undef AuWriteLockFunc
15133 +#undef AuRWLockFuncs
15134 +
15135 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15136 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15137 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15138 +
15139 +/* ---------------------------------------------------------------------- */
15140 +
15141 +/* todo: hard/soft set? */
15142 +static inline aufs_bindex_t au_fbtop(struct file *file)
15143 +{
15144 +       FiMustAnyLock(file);
15145 +       return au_fi(file)->fi_btop;
15146 +}
15147 +
15148 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15149 +{
15150 +       FiMustAnyLock(file);
15151 +       AuDebugOn(!au_fi(file)->fi_hdir);
15152 +       return au_fi(file)->fi_hdir->fd_bbot;
15153 +}
15154 +
15155 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15156 +{
15157 +       FiMustAnyLock(file);
15158 +       AuDebugOn(!au_fi(file)->fi_hdir);
15159 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15160 +}
15161 +
15162 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15163 +{
15164 +       FiMustWriteLock(file);
15165 +       au_fi(file)->fi_btop = bindex;
15166 +}
15167 +
15168 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15169 +{
15170 +       FiMustWriteLock(file);
15171 +       AuDebugOn(!au_fi(file)->fi_hdir);
15172 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15173 +}
15174 +
15175 +static inline void au_set_fvdir_cache(struct file *file,
15176 +                                     struct au_vdir *vdir_cache)
15177 +{
15178 +       FiMustWriteLock(file);
15179 +       AuDebugOn(!au_fi(file)->fi_hdir);
15180 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15181 +}
15182 +
15183 +static inline struct file *au_hf_top(struct file *file)
15184 +{
15185 +       FiMustAnyLock(file);
15186 +       AuDebugOn(au_fi(file)->fi_hdir);
15187 +       return au_fi(file)->fi_htop.hf_file;
15188 +}
15189 +
15190 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15191 +{
15192 +       FiMustAnyLock(file);
15193 +       AuDebugOn(!au_fi(file)->fi_hdir);
15194 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15195 +}
15196 +
15197 +/* todo: memory barrier? */
15198 +static inline unsigned int au_figen(struct file *f)
15199 +{
15200 +       return atomic_read(&au_fi(f)->fi_generation);
15201 +}
15202 +
15203 +static inline void au_set_mmapped(struct file *f)
15204 +{
15205 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15206 +               return;
15207 +       pr_warn("fi_mmapped wrapped around\n");
15208 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15209 +               ;
15210 +}
15211 +
15212 +static inline void au_unset_mmapped(struct file *f)
15213 +{
15214 +       atomic_dec(&au_fi(f)->fi_mmapped);
15215 +}
15216 +
15217 +static inline int au_test_mmapped(struct file *f)
15218 +{
15219 +       return atomic_read(&au_fi(f)->fi_mmapped);
15220 +}
15221 +
15222 +/* customize vma->vm_file */
15223 +
15224 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15225 +                                      struct file *file)
15226 +{
15227 +       struct file *f;
15228 +
15229 +       f = vma->vm_file;
15230 +       get_file(file);
15231 +       vma->vm_file = file;
15232 +       fput(f);
15233 +}
15234 +
15235 +#ifdef CONFIG_MMU
15236 +#define AuDbgVmRegion(file, vma) do {} while (0)
15237 +
15238 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15239 +                                   struct file *file)
15240 +{
15241 +       au_do_vm_file_reset(vma, file);
15242 +}
15243 +#else
15244 +#define AuDbgVmRegion(file, vma) \
15245 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15246 +
15247 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15248 +                                   struct file *file)
15249 +{
15250 +       struct file *f;
15251 +
15252 +       au_do_vm_file_reset(vma, file);
15253 +       f = vma->vm_region->vm_file;
15254 +       get_file(file);
15255 +       vma->vm_region->vm_file = file;
15256 +       fput(f);
15257 +}
15258 +#endif /* CONFIG_MMU */
15259 +
15260 +/* handle vma->vm_prfile */
15261 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15262 +                                   struct file *file)
15263 +{
15264 +       get_file(file);
15265 +       vma->vm_prfile = file;
15266 +#ifndef CONFIG_MMU
15267 +       get_file(file);
15268 +       vma->vm_region->vm_prfile = file;
15269 +#endif
15270 +}
15271 +
15272 +#endif /* __KERNEL__ */
15273 +#endif /* __AUFS_FILE_H__ */
15274 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15275 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15276 +++ linux/fs/aufs/finfo.c       2022-11-05 23:02:18.965889284 +0100
15277 @@ -0,0 +1,149 @@
15278 +// SPDX-License-Identifier: GPL-2.0
15279 +/*
15280 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15281 + *
15282 + * This program is free software; you can redistribute it and/or modify
15283 + * it under the terms of the GNU General Public License as published by
15284 + * the Free Software Foundation; either version 2 of the License, or
15285 + * (at your option) any later version.
15286 + *
15287 + * This program is distributed in the hope that it will be useful,
15288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15290 + * GNU General Public License for more details.
15291 + *
15292 + * You should have received a copy of the GNU General Public License
15293 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15294 + */
15295 +
15296 +/*
15297 + * file private data
15298 + */
15299 +
15300 +#include "aufs.h"
15301 +
15302 +void au_hfput(struct au_hfile *hf, int execed)
15303 +{
15304 +       if (execed)
15305 +               allow_write_access(hf->hf_file);
15306 +       fput(hf->hf_file);
15307 +       hf->hf_file = NULL;
15308 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15309 +       hf->hf_br = NULL;
15310 +}
15311 +
15312 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15313 +{
15314 +       struct au_finfo *finfo = au_fi(file);
15315 +       struct au_hfile *hf;
15316 +       struct au_fidir *fidir;
15317 +
15318 +       fidir = finfo->fi_hdir;
15319 +       if (!fidir) {
15320 +               AuDebugOn(finfo->fi_btop != bindex);
15321 +               hf = &finfo->fi_htop;
15322 +       } else
15323 +               hf = fidir->fd_hfile + bindex;
15324 +
15325 +       if (hf && hf->hf_file)
15326 +               au_hfput(hf, vfsub_file_execed(file));
15327 +       if (val) {
15328 +               FiMustWriteLock(file);
15329 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15330 +               hf->hf_file = val;
15331 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15332 +       }
15333 +}
15334 +
15335 +void au_update_figen(struct file *file)
15336 +{
15337 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15338 +       /* smp_mb(); */ /* atomic_set */
15339 +}
15340 +
15341 +/* ---------------------------------------------------------------------- */
15342 +
15343 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15344 +{
15345 +       struct au_fidir *fidir;
15346 +       int nbr;
15347 +
15348 +       nbr = au_sbbot(sb) + 1;
15349 +       if (nbr < 2)
15350 +               nbr = 2; /* initial allocate for 2 branches */
15351 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15352 +       if (fidir) {
15353 +               fidir->fd_bbot = -1;
15354 +               fidir->fd_nent = nbr;
15355 +       }
15356 +
15357 +       return fidir;
15358 +}
15359 +
15360 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15361 +{
15362 +       int err;
15363 +       struct au_fidir *fidir, *p;
15364 +
15365 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15366 +       fidir = finfo->fi_hdir;
15367 +       AuDebugOn(!fidir);
15368 +
15369 +       err = -ENOMEM;
15370 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15371 +                        GFP_NOFS, may_shrink);
15372 +       if (p) {
15373 +               p->fd_nent = nbr;
15374 +               finfo->fi_hdir = p;
15375 +               err = 0;
15376 +       }
15377 +
15378 +       return err;
15379 +}
15380 +
15381 +/* ---------------------------------------------------------------------- */
15382 +
15383 +void au_finfo_fin(struct file *file)
15384 +{
15385 +       struct au_finfo *finfo;
15386 +
15387 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15388 +
15389 +       finfo = au_fi(file);
15390 +       AuDebugOn(finfo->fi_hdir);
15391 +       AuRwDestroy(&finfo->fi_rwsem);
15392 +       au_cache_free_finfo(finfo);
15393 +}
15394 +
15395 +void au_fi_init_once(void *_finfo)
15396 +{
15397 +       struct au_finfo *finfo = _finfo;
15398 +
15399 +       au_rw_init(&finfo->fi_rwsem);
15400 +}
15401 +
15402 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15403 +{
15404 +       int err;
15405 +       struct au_finfo *finfo;
15406 +       struct dentry *dentry;
15407 +
15408 +       err = -ENOMEM;
15409 +       dentry = file->f_path.dentry;
15410 +       finfo = au_cache_alloc_finfo();
15411 +       if (unlikely(!finfo))
15412 +               goto out;
15413 +
15414 +       err = 0;
15415 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15416 +       au_rw_write_lock(&finfo->fi_rwsem);
15417 +       finfo->fi_btop = -1;
15418 +       finfo->fi_hdir = fidir;
15419 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15420 +       /* smp_mb(); */ /* atomic_set */
15421 +
15422 +       file->private_data = finfo;
15423 +
15424 +out:
15425 +       return err;
15426 +}
15427 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15428 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15429 +++ linux/fs/aufs/f_op.c        2023-10-31 09:31:04.196547417 +0100
15430 @@ -0,0 +1,771 @@
15431 +// SPDX-License-Identifier: GPL-2.0
15432 +/*
15433 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15434 + *
15435 + * This program is free software; you can redistribute it and/or modify
15436 + * it under the terms of the GNU General Public License as published by
15437 + * the Free Software Foundation; either version 2 of the License, or
15438 + * (at your option) any later version.
15439 + *
15440 + * This program is distributed in the hope that it will be useful,
15441 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15442 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15443 + * GNU General Public License for more details.
15444 + *
15445 + * You should have received a copy of the GNU General Public License
15446 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15447 + */
15448 +
15449 +/*
15450 + * file and vm operations
15451 + */
15452 +
15453 +#include <linux/aio.h>
15454 +#include <linux/fs_stack.h>
15455 +#include <linux/mman.h>
15456 +#include <linux/security.h>
15457 +#include "aufs.h"
15458 +
15459 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15460 +{
15461 +       int err;
15462 +       aufs_bindex_t bindex;
15463 +       struct dentry *dentry, *h_dentry;
15464 +       struct au_finfo *finfo;
15465 +       struct inode *h_inode;
15466 +
15467 +       FiMustWriteLock(file);
15468 +
15469 +       err = 0;
15470 +       dentry = file->f_path.dentry;
15471 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15472 +       finfo = au_fi(file);
15473 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15474 +       atomic_set(&finfo->fi_mmapped, 0);
15475 +       bindex = au_dbtop(dentry);
15476 +       if (!h_file) {
15477 +               h_dentry = au_h_dptr(dentry, bindex);
15478 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15479 +               if (unlikely(err))
15480 +                       goto out;
15481 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15482 +               if (IS_ERR(h_file)) {
15483 +                       err = PTR_ERR(h_file);
15484 +                       goto out;
15485 +               }
15486 +       } else {
15487 +               h_dentry = h_file->f_path.dentry;
15488 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15489 +               if (unlikely(err))
15490 +                       goto out;
15491 +               /* br ref is already inc-ed */
15492 +       }
15493 +
15494 +       if (flags & __O_TMPFILE) {
15495 +               AuDebugOn(!h_file);
15496 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15497 +               au_di(dentry)->di_htmpfile = NULL;
15498 +
15499 +               if (!(flags & O_EXCL)) {
15500 +                       h_inode = file_inode(h_file);
15501 +                       spin_lock(&h_inode->i_lock);
15502 +                       h_inode->i_state |= I_LINKABLE;
15503 +                       spin_unlock(&h_inode->i_lock);
15504 +               }
15505 +       }
15506 +       au_set_fbtop(file, bindex);
15507 +       au_set_h_fptr(file, bindex, h_file);
15508 +       au_update_figen(file);
15509 +       /* todo: necessary? */
15510 +       /* file->f_ra = h_file->f_ra; */
15511 +
15512 +out:
15513 +       return err;
15514 +}
15515 +
15516 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15517 +                           struct file *file)
15518 +{
15519 +       int err;
15520 +       struct super_block *sb;
15521 +       struct au_do_open_args args = {
15522 +               .open   = au_do_open_nondir
15523 +       };
15524 +
15525 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15526 +             file, vfsub_file_flags(file), file->f_mode);
15527 +
15528 +       sb = file->f_path.dentry->d_sb;
15529 +       si_read_lock(sb, AuLock_FLUSH);
15530 +       err = au_do_open(file, &args);
15531 +       si_read_unlock(sb);
15532 +       return err;
15533 +}
15534 +
15535 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15536 +{
15537 +       struct au_finfo *finfo;
15538 +       aufs_bindex_t bindex;
15539 +
15540 +       finfo = au_fi(file);
15541 +       au_hbl_del(&finfo->fi_hlist,
15542 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15543 +       bindex = finfo->fi_btop;
15544 +       if (bindex >= 0)
15545 +               au_set_h_fptr(file, bindex, NULL);
15546 +
15547 +       au_finfo_fin(file);
15548 +       return 0;
15549 +}
15550 +
15551 +/* ---------------------------------------------------------------------- */
15552 +
15553 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15554 +{
15555 +       int err;
15556 +       struct file *h_file;
15557 +
15558 +       err = 0;
15559 +       h_file = au_hf_top(file);
15560 +       if (h_file)
15561 +               err = vfsub_flush(h_file, id);
15562 +       return err;
15563 +}
15564 +
15565 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15566 +{
15567 +       return au_do_flush(file, id, au_do_flush_nondir);
15568 +}
15569 +
15570 +/* ---------------------------------------------------------------------- */
15571 +/*
15572 + * read and write functions acquire [fdi]_rwsem once, but release before
15573 + * mmap_sem. This is because to stop a race condition between mmap(2).
15574 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15575 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15576 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15577 + */
15578 +
15579 +/* Callers should call au_read_post() or fput() in the end */
15580 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15581 +{
15582 +       struct file *h_file;
15583 +       int err;
15584 +
15585 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15586 +       if (!err) {
15587 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15588 +               h_file = au_hf_top(file);
15589 +               get_file(h_file);
15590 +               if (!keep_fi)
15591 +                       fi_read_unlock(file);
15592 +       } else
15593 +               h_file = ERR_PTR(err);
15594 +
15595 +       return h_file;
15596 +}
15597 +
15598 +static void au_read_post(struct inode *inode, struct file *h_file)
15599 +{
15600 +       /* update without lock, I don't think it a problem */
15601 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15602 +       fput(h_file);
15603 +}
15604 +
15605 +struct au_write_pre {
15606 +       /* input */
15607 +       unsigned int lsc;
15608 +
15609 +       /* output */
15610 +       blkcnt_t blks;
15611 +       aufs_bindex_t btop;
15612 +};
15613 +
15614 +/*
15615 + * return with iinfo is write-locked
15616 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15617 + * end
15618 + */
15619 +static struct file *au_write_pre(struct file *file, int do_ready,
15620 +                                struct au_write_pre *wpre)
15621 +{
15622 +       struct file *h_file;
15623 +       struct dentry *dentry;
15624 +       int err;
15625 +       unsigned int lsc;
15626 +       struct au_pin pin;
15627 +
15628 +       lsc = 0;
15629 +       if (wpre)
15630 +               lsc = wpre->lsc;
15631 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15632 +       h_file = ERR_PTR(err);
15633 +       if (unlikely(err))
15634 +               goto out;
15635 +
15636 +       dentry = file->f_path.dentry;
15637 +       if (do_ready) {
15638 +               err = au_ready_to_write(file, -1, &pin);
15639 +               if (unlikely(err)) {
15640 +                       h_file = ERR_PTR(err);
15641 +                       di_write_unlock(dentry);
15642 +                       goto out_fi;
15643 +               }
15644 +       }
15645 +
15646 +       di_downgrade_lock(dentry, /*flags*/0);
15647 +       if (wpre)
15648 +               wpre->btop = au_fbtop(file);
15649 +       h_file = au_hf_top(file);
15650 +       get_file(h_file);
15651 +       if (wpre)
15652 +               wpre->blks = file_inode(h_file)->i_blocks;
15653 +       if (do_ready)
15654 +               au_unpin(&pin);
15655 +       di_read_unlock(dentry, /*flags*/0);
15656 +       vfsub_file_start_write(h_file);
15657 +
15658 +out_fi:
15659 +       fi_write_unlock(file);
15660 +out:
15661 +       return h_file;
15662 +}
15663 +
15664 +static void au_write_post(struct inode *inode, struct file *h_file,
15665 +                         struct au_write_pre *wpre, ssize_t written)
15666 +{
15667 +       struct inode *h_inode;
15668 +
15669 +       vfsub_file_end_write(h_file);
15670 +       au_cpup_attr_timesizes(inode);
15671 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15672 +       h_inode = file_inode(h_file);
15673 +       inode->i_mode = h_inode->i_mode;
15674 +       ii_write_unlock(inode);
15675 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15676 +       if (written > 0)
15677 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15678 +                             /*force*/h_inode->i_blocks > wpre->blks);
15679 +       fput(h_file);
15680 +}
15681 +
15682 +/*
15683 + * todo: very ugly
15684 + * it locks both of i_mutex and si_rwsem for read in safe.
15685 + * if the plink maintenance mode continues forever (that is the problem),
15686 + * may loop forever.
15687 + */
15688 +static void au_mtx_and_read_lock(struct inode *inode)
15689 +{
15690 +       int err;
15691 +       struct super_block *sb = inode->i_sb;
15692 +
15693 +       while (1) {
15694 +               inode_lock(inode);
15695 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15696 +               if (!err)
15697 +                       break;
15698 +               inode_unlock(inode);
15699 +               si_read_lock(sb, AuLock_NOPLMW);
15700 +               si_read_unlock(sb);
15701 +       }
15702 +}
15703 +
15704 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15705 +                         struct iov_iter *iov_iter)
15706 +{
15707 +       ssize_t err;
15708 +       struct file *file;
15709 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15710 +
15711 +       err = security_file_permission(h_file, rw);
15712 +       if (unlikely(err))
15713 +               goto out;
15714 +
15715 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15716 +       iter = NULL;
15717 +       if (rw == MAY_READ)
15718 +               iter = h_file->f_op->read_iter;
15719 +       else if (rw == MAY_WRITE)
15720 +               iter = h_file->f_op->write_iter;
15721 +
15722 +       file = kio->ki_filp;
15723 +       kio->ki_filp = h_file;
15724 +       if (iter) {
15725 +               lockdep_off();
15726 +               err = iter(kio, iov_iter);
15727 +               lockdep_on();
15728 +       } else
15729 +               /* currently there is no such fs */
15730 +               WARN_ON_ONCE(1);
15731 +       kio->ki_filp = file;
15732 +
15733 +out:
15734 +       return err;
15735 +}
15736 +
15737 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15738 +{
15739 +       ssize_t err;
15740 +       struct file *file, *h_file;
15741 +       struct inode *inode;
15742 +       struct super_block *sb;
15743 +
15744 +       file = kio->ki_filp;
15745 +       inode = file_inode(file);
15746 +       sb = inode->i_sb;
15747 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15748 +
15749 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15750 +       err = PTR_ERR(h_file);
15751 +       if (IS_ERR(h_file))
15752 +               goto out;
15753 +
15754 +       if (au_test_loopback_kthread()) {
15755 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15756 +               if (file->f_mapping != h_file->f_mapping) {
15757 +                       file->f_mapping = h_file->f_mapping;
15758 +                       smp_mb(); /* unnecessary? */
15759 +               }
15760 +       }
15761 +       fi_read_unlock(file);
15762 +
15763 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15764 +       /* todo: necessary? */
15765 +       /* file->f_ra = h_file->f_ra; */
15766 +       au_read_post(inode, h_file);
15767 +
15768 +out:
15769 +       si_read_unlock(sb);
15770 +       return err;
15771 +}
15772 +
15773 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15774 +{
15775 +       ssize_t err;
15776 +       struct au_write_pre wpre;
15777 +       struct inode *inode;
15778 +       struct file *file, *h_file;
15779 +
15780 +       file = kio->ki_filp;
15781 +       inode = file_inode(file);
15782 +       au_mtx_and_read_lock(inode);
15783 +
15784 +       wpre.lsc = 0;
15785 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15786 +       err = PTR_ERR(h_file);
15787 +       if (IS_ERR(h_file))
15788 +               goto out;
15789 +
15790 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15791 +       au_write_post(inode, h_file, &wpre, err);
15792 +
15793 +out:
15794 +       si_read_unlock(inode->i_sb);
15795 +       inode_unlock(inode);
15796 +       return err;
15797 +}
15798 +
15799 +/*
15800 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15801 + * don't have their own .splice_{read,write} implimentations, and they use
15802 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15803 + * simple converters to f_op->iter_read() and ->iter_write().
15804 + * But we keep our own implementations because some non-mainlined FSes may have
15805 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15806 + * away an opportunity to co-work with aufs from them.
15807 + */
15808 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15809 +                               struct pipe_inode_info *pipe, size_t len,
15810 +                               unsigned int flags)
15811 +{
15812 +       ssize_t err;
15813 +       struct file *h_file;
15814 +       struct inode *inode;
15815 +       struct super_block *sb;
15816 +
15817 +       inode = file_inode(file);
15818 +       sb = inode->i_sb;
15819 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15820 +
15821 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15822 +       err = PTR_ERR(h_file);
15823 +       if (IS_ERR(h_file))
15824 +               goto out;
15825 +
15826 +       err = vfsub_splice_read(h_file, ppos, pipe, len, flags);
15827 +       /* todo: necessary? */
15828 +       /* file->f_ra = h_file->f_ra; */
15829 +       au_read_post(inode, h_file);
15830 +
15831 +out:
15832 +       si_read_unlock(sb);
15833 +       return err;
15834 +}
15835 +
15836 +static ssize_t
15837 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15838 +                 size_t len, unsigned int flags)
15839 +{
15840 +       ssize_t err;
15841 +       struct au_write_pre wpre;
15842 +       struct inode *inode;
15843 +       struct file *h_file;
15844 +
15845 +       inode = file_inode(file);
15846 +       au_mtx_and_read_lock(inode);
15847 +
15848 +       wpre.lsc = 0;
15849 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15850 +       err = PTR_ERR(h_file);
15851 +       if (IS_ERR(h_file))
15852 +               goto out;
15853 +
15854 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15855 +       au_write_post(inode, h_file, &wpre, err);
15856 +
15857 +out:
15858 +       si_read_unlock(inode->i_sb);
15859 +       inode_unlock(inode);
15860 +       return err;
15861 +}
15862 +
15863 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15864 +                          loff_t len)
15865 +{
15866 +       long err;
15867 +       struct au_write_pre wpre;
15868 +       struct inode *inode;
15869 +       struct file *h_file;
15870 +
15871 +       inode = file_inode(file);
15872 +       au_mtx_and_read_lock(inode);
15873 +
15874 +       wpre.lsc = 0;
15875 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15876 +       err = PTR_ERR(h_file);
15877 +       if (IS_ERR(h_file))
15878 +               goto out;
15879 +
15880 +       lockdep_off();
15881 +       err = vfs_fallocate(h_file, mode, offset, len);
15882 +       lockdep_on();
15883 +       /*
15884 +        * we don't need to call file_modifed() here since au_write_post()
15885 +        * is equivalent and copies-up all timestamps and permission bits.
15886 +        */
15887 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15888 +
15889 +out:
15890 +       si_read_unlock(inode->i_sb);
15891 +       inode_unlock(inode);
15892 +       return err;
15893 +}
15894 +
15895 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15896 +                                   struct file *dst, loff_t dst_pos,
15897 +                                   size_t len, unsigned int flags)
15898 +{
15899 +       ssize_t err;
15900 +       struct au_write_pre wpre;
15901 +       enum { SRC, DST };
15902 +       struct {
15903 +               struct inode *inode;
15904 +               struct file *h_file;
15905 +               struct super_block *h_sb;
15906 +       } a[2];
15907 +#define a_src  a[SRC]
15908 +#define a_dst  a[DST]
15909 +
15910 +       err = -EINVAL;
15911 +       a_src.inode = file_inode(src);
15912 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15913 +               goto out;
15914 +       a_dst.inode = file_inode(dst);
15915 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15916 +               goto out;
15917 +
15918 +       au_mtx_and_read_lock(a_dst.inode);
15919 +       /*
15920 +        * in order to match the order in di_write_lock2_{child,parent}(),
15921 +        * use f_path.dentry for this comparison.
15922 +        */
15923 +       if (src->f_path.dentry < dst->f_path.dentry) {
15924 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15925 +               err = PTR_ERR(a_src.h_file);
15926 +               if (IS_ERR(a_src.h_file))
15927 +                       goto out_si;
15928 +
15929 +               wpre.lsc = AuLsc_FI_2;
15930 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15931 +               err = PTR_ERR(a_dst.h_file);
15932 +               if (IS_ERR(a_dst.h_file)) {
15933 +                       au_read_post(a_src.inode, a_src.h_file);
15934 +                       goto out_si;
15935 +               }
15936 +       } else {
15937 +               wpre.lsc = AuLsc_FI_1;
15938 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15939 +               err = PTR_ERR(a_dst.h_file);
15940 +               if (IS_ERR(a_dst.h_file))
15941 +                       goto out_si;
15942 +
15943 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15944 +               err = PTR_ERR(a_src.h_file);
15945 +               if (IS_ERR(a_src.h_file)) {
15946 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15947 +                                     /*written*/0);
15948 +                       goto out_si;
15949 +               }
15950 +       }
15951 +
15952 +       err = -EXDEV;
15953 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15954 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15955 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15956 +               AuDbgFile(src);
15957 +               AuDbgFile(dst);
15958 +               goto out_file;
15959 +       }
15960 +
15961 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15962 +                                   dst_pos, len, flags);
15963 +
15964 +out_file:
15965 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15966 +       fi_read_unlock(src);
15967 +       au_read_post(a_src.inode, a_src.h_file);
15968 +out_si:
15969 +       si_read_unlock(a_dst.inode->i_sb);
15970 +       inode_unlock(a_dst.inode);
15971 +out:
15972 +       return err;
15973 +#undef a_src
15974 +#undef a_dst
15975 +}
15976 +
15977 +/* ---------------------------------------------------------------------- */
15978 +
15979 +/*
15980 + * The locking order around current->mmap_sem.
15981 + * - in most and regular cases
15982 + *   file I/O syscall -- aufs_read() or something
15983 + *     -- si_rwsem for read -- mmap_sem
15984 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15985 + * - in mmap case
15986 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15987 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15988 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15989 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15990 + * It means that when aufs acquires si_rwsem for write, the process should never
15991 + * acquire mmap_sem.
15992 + *
15993 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15994 + * problem either since any directory is not able to be mmap-ed.
15995 + * The similar scenario is applied to aufs_readlink() too.
15996 + */
15997 +
15998 +#if 0 /* stop calling security_file_mmap() */
15999 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16000 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16001 +
16002 +static unsigned long au_arch_prot_conv(unsigned long flags)
16003 +{
16004 +       /* currently ppc64 only */
16005 +#ifdef CONFIG_PPC64
16006 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16007 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16008 +       return AuConv_VM_PROT(flags, SAO);
16009 +#else
16010 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16011 +       return 0;
16012 +#endif
16013 +}
16014 +
16015 +static unsigned long au_prot_conv(unsigned long flags)
16016 +{
16017 +       return AuConv_VM_PROT(flags, READ)
16018 +               | AuConv_VM_PROT(flags, WRITE)
16019 +               | AuConv_VM_PROT(flags, EXEC)
16020 +               | au_arch_prot_conv(flags);
16021 +}
16022 +
16023 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16024 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16025 +
16026 +static unsigned long au_flag_conv(unsigned long flags)
16027 +{
16028 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16029 +               | AuConv_VM_MAP(flags, DENYWRITE)
16030 +               | AuConv_VM_MAP(flags, LOCKED);
16031 +}
16032 +#endif
16033 +
16034 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16035 +{
16036 +       int err;
16037 +       const unsigned char wlock
16038 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16039 +       struct super_block *sb;
16040 +       struct file *h_file;
16041 +       struct inode *inode;
16042 +
16043 +       AuDbgVmRegion(file, vma);
16044 +
16045 +       inode = file_inode(file);
16046 +       sb = inode->i_sb;
16047 +       lockdep_off();
16048 +       si_read_lock(sb, AuLock_NOPLMW);
16049 +
16050 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16051 +       lockdep_on();
16052 +       err = PTR_ERR(h_file);
16053 +       if (IS_ERR(h_file))
16054 +               goto out;
16055 +
16056 +       err = 0;
16057 +       au_set_mmapped(file);
16058 +       au_vm_file_reset(vma, h_file);
16059 +       /*
16060 +        * we cannot call security_mmap_file() here since it may acquire
16061 +        * mmap_sem or i_mutex.
16062 +        *
16063 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16064 +        *                       au_flag_conv(vma->vm_flags));
16065 +        */
16066 +       if (!err)
16067 +               err = call_mmap(h_file, vma);
16068 +       if (!err) {
16069 +               au_vm_prfile_set(vma, file);
16070 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16071 +               goto out_fput; /* success */
16072 +       }
16073 +       au_unset_mmapped(file);
16074 +       au_vm_file_reset(vma, file);
16075 +
16076 +out_fput:
16077 +       lockdep_off();
16078 +       ii_write_unlock(inode);
16079 +       lockdep_on();
16080 +       fput(h_file);
16081 +out:
16082 +       lockdep_off();
16083 +       si_read_unlock(sb);
16084 +       lockdep_on();
16085 +       AuTraceErr(err);
16086 +       return err;
16087 +}
16088 +
16089 +/* ---------------------------------------------------------------------- */
16090 +
16091 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16092 +                            int datasync)
16093 +{
16094 +       int err;
16095 +       struct au_write_pre wpre;
16096 +       struct inode *inode;
16097 +       struct file *h_file;
16098 +
16099 +       err = 0; /* -EBADF; */ /* posix? */
16100 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16101 +               goto out;
16102 +
16103 +       inode = file_inode(file);
16104 +       au_mtx_and_read_lock(inode);
16105 +
16106 +       wpre.lsc = 0;
16107 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16108 +       err = PTR_ERR(h_file);
16109 +       if (IS_ERR(h_file))
16110 +               goto out_unlock;
16111 +
16112 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16113 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16114 +
16115 +out_unlock:
16116 +       si_read_unlock(inode->i_sb);
16117 +       inode_unlock(inode);
16118 +out:
16119 +       return err;
16120 +}
16121 +
16122 +static int aufs_fasync(int fd, struct file *file, int flag)
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 +       if (h_file->f_op->fasync)
16137 +               err = h_file->f_op->fasync(fd, h_file, flag);
16138 +       fput(h_file); /* instead of au_read_post() */
16139 +
16140 +out:
16141 +       si_read_unlock(sb);
16142 +       return err;
16143 +}
16144 +
16145 +static int aufs_setfl(struct file *file, unsigned long arg)
16146 +{
16147 +       int err;
16148 +       struct file *h_file;
16149 +       struct super_block *sb;
16150 +
16151 +       sb = file->f_path.dentry->d_sb;
16152 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16153 +
16154 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16155 +       err = PTR_ERR(h_file);
16156 +       if (IS_ERR(h_file))
16157 +               goto out;
16158 +
16159 +       /* stop calling h_file->fasync */
16160 +       arg |= vfsub_file_flags(file) & FASYNC;
16161 +       err = setfl(/*unused fd*/-1, h_file, arg);
16162 +       fput(h_file); /* instead of au_read_post() */
16163 +
16164 +out:
16165 +       si_read_unlock(sb);
16166 +       return err;
16167 +}
16168 +
16169 +/* ---------------------------------------------------------------------- */
16170 +
16171 +const struct file_operations aufs_file_fop = {
16172 +       .owner          = THIS_MODULE,
16173 +
16174 +       .llseek         = default_llseek,
16175 +
16176 +       .read_iter      = aufs_read_iter,
16177 +       .write_iter     = aufs_write_iter,
16178 +
16179 +#ifdef CONFIG_AUFS_POLL
16180 +       .poll           = aufs_poll,
16181 +#endif
16182 +       .unlocked_ioctl = aufs_ioctl_nondir,
16183 +#ifdef CONFIG_COMPAT
16184 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16185 +#endif
16186 +       .mmap           = aufs_mmap,
16187 +       .open           = aufs_open_nondir,
16188 +       .flush          = aufs_flush_nondir,
16189 +       .release        = aufs_release_nondir,
16190 +       .fsync          = aufs_fsync_nondir,
16191 +       .fasync         = aufs_fasync,
16192 +       .setfl          = aufs_setfl,
16193 +       .splice_write   = aufs_splice_write,
16194 +       .splice_read    = aufs_splice_read,
16195 +#if 0 /* reserved for future use */
16196 +       .aio_splice_write = aufs_aio_splice_write,
16197 +       .aio_splice_read  = aufs_aio_splice_read,
16198 +#endif
16199 +       .fallocate      = aufs_fallocate,
16200 +       .copy_file_range = aufs_copy_file_range
16201 +};
16202 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16203 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16204 +++ linux/fs/aufs/fsctx.c       2022-11-05 23:02:18.965889284 +0100
16205 @@ -0,0 +1,1242 @@
16206 +// SPDX-License-Identifier: GPL-2.0
16207 +/*
16208 + * Copyright (C) 2022 Junjiro R. Okajima
16209 + *
16210 + * This program is free software; you can redistribute it and/or modify
16211 + * it under the terms of the GNU General Public License as published by
16212 + * the Free Software Foundation; either version 2 of the License, or
16213 + * (at your option) any later version.
16214 + *
16215 + * This program is distributed in the hope that it will be useful,
16216 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16217 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16218 + * GNU General Public License for more details.
16219 + *
16220 + * You should have received a copy of the GNU General Public License
16221 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16222 + */
16223 +
16224 +/*
16225 + * fs context, aka new mount api
16226 + */
16227 +
16228 +#include <linux/fs_context.h>
16229 +#include "aufs.h"
16230 +
16231 +struct au_fsctx_opts {
16232 +       aufs_bindex_t bindex;
16233 +       unsigned char skipped;
16234 +       struct au_opt *opt, *opt_tail;
16235 +       struct super_block *sb;
16236 +       struct au_sbinfo *sbinfo;
16237 +       struct au_opts opts;
16238 +};
16239 +
16240 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16241 +static int cvt_err(int err)
16242 +{
16243 +       AuTraceErr(err);
16244 +
16245 +       switch (err) {
16246 +       case -ENOENT:
16247 +       case -ENOTDIR:
16248 +       case -EEXIST:
16249 +       case -EIO:
16250 +               err = -EINVAL;
16251 +       }
16252 +       return err;
16253 +}
16254 +
16255 +static int au_fsctx_reconfigure(struct fs_context *fc)
16256 +{
16257 +       int err, do_dx;
16258 +       unsigned int mntflags;
16259 +       struct dentry *root;
16260 +       struct super_block *sb;
16261 +       struct inode *inode;
16262 +       struct au_fsctx_opts *a = fc->fs_private;
16263 +
16264 +       AuDbg("fc %p\n", fc);
16265 +
16266 +       root = fc->root;
16267 +       sb = root->d_sb;
16268 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16269 +       if (!err) {
16270 +               di_write_lock_child(root);
16271 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16272 +               aufs_write_unlock(root);
16273 +       }
16274 +
16275 +       inode = d_inode(root);
16276 +       inode_lock(inode);
16277 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16278 +       if (unlikely(err))
16279 +               goto out;
16280 +       di_write_lock_child(root);
16281 +
16282 +       /* au_opts_remount() may return an error */
16283 +       err = au_opts_remount(sb, &a->opts);
16284 +
16285 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16286 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16287 +                                                    REFRESH_IDOP));
16288 +
16289 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16290 +               mntflags = au_mntflags(sb);
16291 +               do_dx = !!au_opt_test(mntflags, DIO);
16292 +               au_dy_arefresh(do_dx);
16293 +       }
16294 +
16295 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16296 +       aufs_write_unlock(root);
16297 +
16298 +out:
16299 +       inode_unlock(inode);
16300 +       err = cvt_err(err);
16301 +       AuTraceErr(err);
16302 +
16303 +       return err;
16304 +}
16305 +
16306 +/* ---------------------------------------------------------------------- */
16307 +
16308 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16309 +{
16310 +       int err;
16311 +       struct au_fsctx_opts *a = fc->fs_private;
16312 +       struct au_sbinfo *sbinfo = a->sbinfo;
16313 +       struct dentry *root;
16314 +       struct inode *inode;
16315 +
16316 +       sbinfo->si_sb = sb;
16317 +       sb->s_fs_info = sbinfo;
16318 +       kobject_get(&sbinfo->si_kobj);
16319 +
16320 +       __si_write_lock(sb);
16321 +       si_pid_set(sb);
16322 +       au_sbilist_add(sb);
16323 +
16324 +       /* all timestamps always follow the ones on the branch */
16325 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16326 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16327 +       sb->s_op = &aufs_sop;
16328 +       sb->s_d_op = &aufs_dop;
16329 +       sb->s_magic = AUFS_SUPER_MAGIC;
16330 +       sb->s_maxbytes = 0;
16331 +       sb->s_stack_depth = 1;
16332 +       au_export_init(sb);
16333 +       au_xattr_init(sb);
16334 +
16335 +       err = au_alloc_root(sb);
16336 +       if (unlikely(err)) {
16337 +               si_write_unlock(sb);
16338 +               goto out;
16339 +       }
16340 +       root = sb->s_root;
16341 +       inode = d_inode(root);
16342 +       ii_write_lock_parent(inode);
16343 +       aufs_write_unlock(root);
16344 +
16345 +       /* lock vfs_inode first, then aufs. */
16346 +       inode_lock(inode);
16347 +       aufs_write_lock(root);
16348 +       err = au_opts_mount(sb, &a->opts);
16349 +       AuTraceErr(err);
16350 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16351 +               sb->s_d_op = &aufs_dop_noreval;
16352 +               /* infofc(fc, "%ps", sb->s_d_op); */
16353 +               pr_info("%ps\n", sb->s_d_op);
16354 +               au_refresh_dop(root, /*force_reval*/0);
16355 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16356 +               au_refresh_iop(inode, /*force_getattr*/0);
16357 +       }
16358 +       aufs_write_unlock(root);
16359 +       inode_unlock(inode);
16360 +       if (!err)
16361 +               goto out; /* success */
16362 +
16363 +       dput(root);
16364 +       sb->s_root = NULL;
16365 +
16366 +out:
16367 +       if (unlikely(err))
16368 +               kobject_put(&sbinfo->si_kobj);
16369 +       AuTraceErr(err);
16370 +       err = cvt_err(err);
16371 +       AuTraceErr(err);
16372 +       return err;
16373 +}
16374 +
16375 +static int au_fsctx_get_tree(struct fs_context *fc)
16376 +{
16377 +       int err;
16378 +
16379 +       AuDbg("fc %p\n", fc);
16380 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16381 +
16382 +       AuTraceErr(err);
16383 +       return err;
16384 +}
16385 +
16386 +/* ---------------------------------------------------------------------- */
16387 +
16388 +static void au_fsctx_dump(struct au_opts *opts)
16389 +{
16390 +#ifdef CONFIG_AUFS_DEBUG
16391 +       /* reduce stack space */
16392 +       union {
16393 +               struct au_opt_add *add;
16394 +               struct au_opt_del *del;
16395 +               struct au_opt_mod *mod;
16396 +               struct au_opt_xino *xino;
16397 +               struct au_opt_xino_itrunc *xino_itrunc;
16398 +               struct au_opt_wbr_create *create;
16399 +       } u;
16400 +       struct au_opt *opt;
16401 +
16402 +       opt = opts->opt;
16403 +       while (opt->type != Opt_tail) {
16404 +               switch (opt->type) {
16405 +               case Opt_add:
16406 +                       u.add = &opt->add;
16407 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16408 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16409 +                                 u.add->path.dentry);
16410 +                       break;
16411 +               case Opt_del:
16412 +                       fallthrough;
16413 +               case Opt_idel:
16414 +                       u.del = &opt->del;
16415 +                       AuDbg("del {%s, %p}\n",
16416 +                             u.del->pathname, u.del->h_path.dentry);
16417 +                       break;
16418 +               case Opt_mod:
16419 +                       fallthrough;
16420 +               case Opt_imod:
16421 +                       u.mod = &opt->mod;
16422 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16423 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16424 +                       break;
16425 +               case Opt_append:
16426 +                       u.add = &opt->add;
16427 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16428 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16429 +                                 u.add->path.dentry);
16430 +                       break;
16431 +               case Opt_prepend:
16432 +                       u.add = &opt->add;
16433 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16434 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16435 +                                 u.add->path.dentry);
16436 +                       break;
16437 +
16438 +               case Opt_dirwh:
16439 +                       AuDbg("dirwh %d\n", opt->dirwh);
16440 +                       break;
16441 +               case Opt_rdcache:
16442 +                       AuDbg("rdcache %d\n", opt->rdcache);
16443 +                       break;
16444 +               case Opt_rdblk:
16445 +                       AuDbg("rdblk %d\n", opt->rdblk);
16446 +                       break;
16447 +               case Opt_rdhash:
16448 +                       AuDbg("rdhash %u\n", opt->rdhash);
16449 +                       break;
16450 +
16451 +               case Opt_xino:
16452 +                       u.xino = &opt->xino;
16453 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16454 +                       break;
16455 +
16456 +#define au_fsctx_TF(name)                                        \
16457 +                       case Opt_##name:                          \
16458 +                               if (opt->tf)                      \
16459 +                                       AuLabel(name);            \
16460 +                               else                              \
16461 +                                       AuLabel(no##name);        \
16462 +                               break;
16463 +
16464 +               /* simple true/false flag */
16465 +               au_fsctx_TF(trunc_xino);
16466 +               au_fsctx_TF(trunc_xib);
16467 +               au_fsctx_TF(dirperm1);
16468 +               au_fsctx_TF(plink);
16469 +               au_fsctx_TF(shwh);
16470 +               au_fsctx_TF(dio);
16471 +               au_fsctx_TF(warn_perm);
16472 +               au_fsctx_TF(verbose);
16473 +               au_fsctx_TF(sum);
16474 +               au_fsctx_TF(dirren);
16475 +               au_fsctx_TF(acl);
16476 +#undef au_fsctx_TF
16477 +
16478 +               case Opt_trunc_xino_path:
16479 +                       fallthrough;
16480 +               case Opt_itrunc_xino:
16481 +                       u.xino_itrunc = &opt->xino_itrunc;
16482 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16483 +                       break;
16484 +               case Opt_noxino:
16485 +                       AuLabel(noxino);
16486 +                       break;
16487 +
16488 +               case Opt_list_plink:
16489 +                       AuLabel(list_plink);
16490 +                       break;
16491 +               case Opt_udba:
16492 +                       AuDbg("udba %d, %s\n",
16493 +                                 opt->udba, au_optstr_udba(opt->udba));
16494 +                       break;
16495 +               case Opt_diropq_a:
16496 +                       AuLabel(diropq_a);
16497 +                       break;
16498 +               case Opt_diropq_w:
16499 +                       AuLabel(diropq_w);
16500 +                       break;
16501 +               case Opt_wsum:
16502 +                       AuLabel(wsum);
16503 +                       break;
16504 +               case Opt_wbr_create:
16505 +                       u.create = &opt->wbr_create;
16506 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16507 +                                 au_optstr_wbr_create(u.create->wbr_create));
16508 +                       switch (u.create->wbr_create) {
16509 +                       case AuWbrCreate_MFSV:
16510 +                               fallthrough;
16511 +                       case AuWbrCreate_PMFSV:
16512 +                               AuDbg("%d sec\n", u.create->mfs_second);
16513 +                               break;
16514 +                       case AuWbrCreate_MFSRR:
16515 +                               fallthrough;
16516 +                       case AuWbrCreate_TDMFS:
16517 +                               AuDbg("%llu watermark\n",
16518 +                                         u.create->mfsrr_watermark);
16519 +                               break;
16520 +                       case AuWbrCreate_MFSRRV:
16521 +                               fallthrough;
16522 +                       case AuWbrCreate_TDMFSV:
16523 +                               fallthrough;
16524 +                       case AuWbrCreate_PMFSRRV:
16525 +                               AuDbg("%llu watermark, %d sec\n",
16526 +                                         u.create->mfsrr_watermark,
16527 +                                         u.create->mfs_second);
16528 +                               break;
16529 +                       }
16530 +                       break;
16531 +               case Opt_wbr_copyup:
16532 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16533 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16534 +                       break;
16535 +               case Opt_fhsm_sec:
16536 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16537 +                       break;
16538 +
16539 +               default:
16540 +                       AuDbg("type %d\n", opt->type);
16541 +                       BUG();
16542 +               }
16543 +               opt++;
16544 +       }
16545 +#endif
16546 +}
16547 +
16548 +/* ---------------------------------------------------------------------- */
16549 +
16550 +/*
16551 + * For conditionally compiled mount options.
16552 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16553 + */
16554 +#define au_ignore_flag(name, action)           \
16555 +       fsparam_flag(name, action),             \
16556 +       fsparam_flag("no" name, Opt_ignore_silent)
16557 +
16558 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16559 +       fsparam_string("br", Opt_br),
16560 +
16561 +       /* "add=%d:%s" or "ins=%d:%s" */
16562 +       fsparam_string("add", Opt_add),
16563 +       fsparam_string("ins", Opt_add),
16564 +       fsparam_path("append", Opt_append),
16565 +       fsparam_path("prepend", Opt_prepend),
16566 +
16567 +       fsparam_path("del", Opt_del),
16568 +       /* fsparam_s32("idel", Opt_idel), */
16569 +       fsparam_path("mod", Opt_mod),
16570 +       /* fsparam_string("imod", Opt_imod), */
16571 +
16572 +       fsparam_s32("dirwh", Opt_dirwh),
16573 +
16574 +       fsparam_path("xino", Opt_xino),
16575 +       fsparam_flag("noxino", Opt_noxino),
16576 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16577 +       /* "trunc_xino_v=%d:%d" */
16578 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16579 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16580 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16581 +       /* fsparam_path("zxino", Opt_zxino), */
16582 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16583 +
16584 +#ifdef CONFIG_PROC_FS
16585 +       fsparam_flag_no("plink", Opt_plink),
16586 +#else
16587 +       au_ignore_flag("plink", Opt_ignore),
16588 +#endif
16589 +
16590 +#ifdef CONFIG_AUFS_DEBUG
16591 +       fsparam_flag("list_plink", Opt_list_plink),
16592 +#endif
16593 +
16594 +       fsparam_string("udba", Opt_udba),
16595 +
16596 +       fsparam_flag_no("dio", Opt_dio),
16597 +
16598 +#ifdef CONFIG_AUFS_DIRREN
16599 +       fsparam_flag_no("dirren", Opt_dirren),
16600 +#else
16601 +       au_ignore_flag("dirren", Opt_ignore),
16602 +#endif
16603 +
16604 +#ifdef CONFIG_AUFS_FHSM
16605 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16606 +#else
16607 +       fsparam_s32("fhsm_sec", Opt_ignore),
16608 +#endif
16609 +
16610 +       /* always | a | whiteouted | w */
16611 +       fsparam_string("diropq", Opt_diropq),
16612 +
16613 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16614 +
16615 +#ifdef CONFIG_AUFS_SHWH
16616 +       fsparam_flag_no("shwh", Opt_shwh),
16617 +#else
16618 +       au_ignore_flag("shwh", Opt_err),
16619 +#endif
16620 +
16621 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16622 +
16623 +       fsparam_flag_no("verbose", Opt_verbose),
16624 +       fsparam_flag("v", Opt_verbose),
16625 +       fsparam_flag("quiet", Opt_noverbose),
16626 +       fsparam_flag("q", Opt_noverbose),
16627 +       /* user-space may handle this */
16628 +       fsparam_flag("silent", Opt_noverbose),
16629 +
16630 +       fsparam_flag_no("sum", Opt_sum),
16631 +       fsparam_flag("wsum", Opt_wsum),
16632 +
16633 +       fsparam_s32("rdcache", Opt_rdcache),
16634 +       /* "def" or s32 */
16635 +       fsparam_string("rdblk", Opt_rdblk),
16636 +       /* "def" or s32 */
16637 +       fsparam_string("rdhash", Opt_rdhash),
16638 +
16639 +       fsparam_string("create", Opt_wbr_create),
16640 +       fsparam_string("create_policy", Opt_wbr_create),
16641 +       fsparam_string("cpup", Opt_wbr_copyup),
16642 +       fsparam_string("copyup", Opt_wbr_copyup),
16643 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16644 +
16645 +       /* generic VFS flag */
16646 +#ifdef CONFIG_FS_POSIX_ACL
16647 +       fsparam_flag_no("acl", Opt_acl),
16648 +#else
16649 +       au_ignore_flag("acl", Opt_ignore),
16650 +#endif
16651 +
16652 +       /* internal use for the scripts */
16653 +       fsparam_string("si", Opt_ignore_silent),
16654 +
16655 +       /* obsoleted, keep them temporary */
16656 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16657 +       fsparam_flag("clean_plink", Opt_ignore),
16658 +       fsparam_string("dirs", Opt_br),
16659 +       fsparam_u32("debug", Opt_ignore),
16660 +       /* "whiteout" or "all" */
16661 +       fsparam_string("delete", Opt_ignore),
16662 +       fsparam_string("imap", Opt_ignore),
16663 +
16664 +       /* temporary workaround, due to old mount(8)? */
16665 +       fsparam_flag("relatime", Opt_ignore_silent),
16666 +
16667 +       {}
16668 +};
16669 +
16670 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16671 +                                char *brspec, size_t speclen,
16672 +                                aufs_bindex_t bindex)
16673 +{
16674 +       int err;
16675 +       char *p;
16676 +
16677 +       AuDbg("brspec %s\n", brspec);
16678 +
16679 +       err = -ENOMEM;
16680 +       if (!speclen)
16681 +               speclen = strlen(brspec);
16682 +       /* will be freed by au_fsctx_free() */
16683 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16684 +       if (unlikely(!p)) {
16685 +               errorfc(fc, "failed in %s", brspec);
16686 +               goto out;
16687 +       }
16688 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16689 +
16690 +out:
16691 +       AuTraceErr(err);
16692 +       return err;
16693 +}
16694 +
16695 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16696 +{
16697 +       int err;
16698 +       char *p;
16699 +       struct au_fsctx_opts *a = fc->fs_private;
16700 +       struct au_opt *opt = a->opt;
16701 +       aufs_bindex_t bindex = a->bindex;
16702 +
16703 +       AuDbg("brspec %s\n", brspec);
16704 +
16705 +       err = -EINVAL;
16706 +       while ((p = strsep(&brspec, ":")) && *p) {
16707 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16708 +               AuTraceErr(err);
16709 +               if (unlikely(err))
16710 +                       break;
16711 +               bindex++;
16712 +               opt++;
16713 +               if (unlikely(opt > a->opt_tail)) {
16714 +                       err = -E2BIG;
16715 +                       bindex--;
16716 +                       opt--;
16717 +                       break;
16718 +               }
16719 +               opt->type = Opt_tail;
16720 +               a->skipped = 1;
16721 +       }
16722 +       a->bindex = bindex;
16723 +       a->opt = opt;
16724 +
16725 +       AuTraceErr(err);
16726 +       return err;
16727 +}
16728 +
16729 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16730 +{
16731 +       int err, n;
16732 +       char *p;
16733 +       struct au_fsctx_opts *a = fc->fs_private;
16734 +       struct au_opt *opt = a->opt;
16735 +
16736 +       err = -EINVAL;
16737 +       p = strchr(addspec, ':');
16738 +       if (unlikely(!p)) {
16739 +               errorfc(fc, "bad arg in %s", addspec);
16740 +               goto out;
16741 +       }
16742 +       *p++ = '\0';
16743 +       err = kstrtoint(addspec, 0, &n);
16744 +       if (unlikely(err)) {
16745 +               errorfc(fc, "bad integer in %s", addspec);
16746 +               goto out;
16747 +       }
16748 +       AuDbg("n %d\n", n);
16749 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16750 +
16751 +out:
16752 +       AuTraceErr(err);
16753 +       return err;
16754 +}
16755 +
16756 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16757 +                             struct fs_parameter *param)
16758 +{
16759 +       int err;
16760 +
16761 +       err = -ENOMEM;
16762 +       /* will be freed by au_fsctx_free() */
16763 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16764 +       if (unlikely(!del->pathname))
16765 +               goto out;
16766 +       AuDbg("del %s\n", del->pathname);
16767 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16768 +       if (unlikely(err))
16769 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16770 +
16771 +out:
16772 +       AuTraceErr(err);
16773 +       return err;
16774 +}
16775 +
16776 +#if 0 /* reserved for future use */
16777 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16778 +                              aufs_bindex_t bindex)
16779 +{
16780 +       int err;
16781 +       struct super_block *sb;
16782 +       struct dentry *root;
16783 +       struct au_fsctx_opts *a = fc->fs_private;
16784 +
16785 +       sb = a->sb;
16786 +       AuDebugOn(!sb);
16787 +
16788 +       err = -EINVAL;
16789 +       root = sb->s_root;
16790 +       aufs_read_lock(root, AuLock_FLUSH);
16791 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16792 +               errorfc(fc, "out of bounds, %d", bindex);
16793 +               goto out;
16794 +       }
16795 +
16796 +       err = 0;
16797 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16798 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16799 +
16800 +out:
16801 +       aufs_read_unlock(root, !AuLock_IR);
16802 +       AuTraceErr(err);
16803 +       return err;
16804 +}
16805 +#endif
16806 +
16807 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16808 +                             struct fs_parameter *param)
16809 +{
16810 +       int err;
16811 +       struct path path;
16812 +       char *p;
16813 +
16814 +       err = -ENOMEM;
16815 +       /* will be freed by au_fsctx_free() */
16816 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16817 +       if (unlikely(!mod->path))
16818 +               goto out;
16819 +
16820 +       err = -EINVAL;
16821 +       p = strchr(mod->path, '=');
16822 +       if (unlikely(!p)) {
16823 +               errorfc(fc, "no permission %s", mod->path);
16824 +               goto out;
16825 +       }
16826 +
16827 +       *p++ = 0;
16828 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16829 +       if (unlikely(err)) {
16830 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16831 +               goto out;
16832 +       }
16833 +
16834 +       mod->perm = au_br_perm_val(p);
16835 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16836 +       mod->h_root = dget(path.dentry);
16837 +       path_put(&path);
16838 +
16839 +out:
16840 +       AuTraceErr(err);
16841 +       return err;
16842 +}
16843 +
16844 +#if 0 /* reserved for future use */
16845 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16846 +                              char *ibrspec)
16847 +{
16848 +       int err, n;
16849 +       char *p;
16850 +       struct super_block *sb;
16851 +       struct dentry *root;
16852 +       struct au_fsctx_opts *a = fc->fs_private;
16853 +
16854 +       sb = a->sb;
16855 +       AuDebugOn(!sb);
16856 +
16857 +       err = -EINVAL;
16858 +       p = strchr(ibrspec, ':');
16859 +       if (unlikely(!p)) {
16860 +               errorfc(fc, "no index, %s", ibrspec);
16861 +               goto out;
16862 +       }
16863 +       *p++ = '\0';
16864 +       err = kstrtoint(ibrspec, 0, &n);
16865 +       if (unlikely(err)) {
16866 +               errorfc(fc, "bad integer in %s", ibrspec);
16867 +               goto out;
16868 +       }
16869 +       AuDbg("n %d\n", n);
16870 +
16871 +       root = sb->s_root;
16872 +       aufs_read_lock(root, AuLock_FLUSH);
16873 +       if (n < 0 || au_sbbot(sb) < n) {
16874 +               errorfc(fc, "out of bounds, %d", bindex);
16875 +               goto out_root;
16876 +       }
16877 +
16878 +       err = 0;
16879 +       mod->perm = au_br_perm_val(p);
16880 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16881 +             mod->path, mod->perm, p);
16882 +       mod->h_root = dget(au_h_dptr(root, bindex));
16883 +
16884 +out_root:
16885 +       aufs_read_unlock(root, !AuLock_IR);
16886 +out:
16887 +       AuTraceErr(err);
16888 +       return err;
16889 +}
16890 +#endif
16891 +
16892 +static int au_fsctx_parse_xino(struct fs_context *fc,
16893 +                              struct au_opt_xino *xino,
16894 +                              struct fs_parameter *param)
16895 +{
16896 +       int err;
16897 +       struct au_fsctx_opts *a = fc->fs_private;
16898 +
16899 +       err = -ENOMEM;
16900 +       /* will be freed by au_opts_free() */
16901 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16902 +       if (unlikely(!xino->path))
16903 +               goto out;
16904 +       AuDbg("path %s\n", xino->path);
16905 +
16906 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16907 +                                   /*wbrtop*/0);
16908 +       err = PTR_ERR(xino->file);
16909 +       if (IS_ERR(xino->file)) {
16910 +               xino->file = NULL;
16911 +               goto out;
16912 +       }
16913 +
16914 +       err = 0;
16915 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16916 +               err = -EINVAL;
16917 +               errorfc(fc, "%s must be outside", xino->path);
16918 +       }
16919 +
16920 +out:
16921 +       AuTraceErr(err);
16922 +       return err;
16923 +}
16924 +
16925 +static
16926 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16927 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16928 +                                   char *pathname)
16929 +{
16930 +       int err;
16931 +       aufs_bindex_t bbot, bindex;
16932 +       struct path path;
16933 +       struct dentry *root;
16934 +       struct au_fsctx_opts *a = fc->fs_private;
16935 +
16936 +       AuDebugOn(!a->sb);
16937 +
16938 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16939 +       if (unlikely(err)) {
16940 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16941 +               goto out;
16942 +       }
16943 +
16944 +       xino_itrunc->bindex = -1;
16945 +       root = a->sb->s_root;
16946 +       aufs_read_lock(root, AuLock_FLUSH);
16947 +       bbot = au_sbbot(a->sb);
16948 +       for (bindex = 0; bindex <= bbot; bindex++) {
16949 +               if (au_h_dptr(root, bindex) == path.dentry) {
16950 +                       xino_itrunc->bindex = bindex;
16951 +                       break;
16952 +               }
16953 +       }
16954 +       aufs_read_unlock(root, !AuLock_IR);
16955 +       path_put(&path);
16956 +
16957 +       if (unlikely(xino_itrunc->bindex < 0)) {
16958 +               err = -EINVAL;
16959 +               errorfc(fc, "no such branch %s", pathname);
16960 +       }
16961 +
16962 +out:
16963 +       AuTraceErr(err);
16964 +       return err;
16965 +}
16966 +
16967 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16968 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16969 +                                     unsigned int bindex)
16970 +{
16971 +       int err;
16972 +       aufs_bindex_t bbot;
16973 +       struct super_block *sb;
16974 +       struct au_fsctx_opts *a = fc->fs_private;
16975 +
16976 +       sb = a->sb;
16977 +       AuDebugOn(!sb);
16978 +
16979 +       err = 0;
16980 +       si_noflush_read_lock(sb);
16981 +       bbot = au_sbbot(sb);
16982 +       si_read_unlock(sb);
16983 +       if (bindex <= bbot)
16984 +               xino_itrunc->bindex = bindex;
16985 +       else {
16986 +               err = -EINVAL;
16987 +               errorfc(fc, "out of bounds, %u", bindex);
16988 +       }
16989 +
16990 +       AuTraceErr(err);
16991 +       return err;
16992 +}
16993 +
16994 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16995 +{
16996 +       int err, token;
16997 +       struct fs_parse_result result;
16998 +       struct au_fsctx_opts *a = fc->fs_private;
16999 +       struct au_opt *opt = a->opt;
17000 +
17001 +       AuDbg("fc %p, param {key %s, string %s}\n",
17002 +             fc, param->key, param->string);
17003 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
17004 +       if (unlikely(err < 0))
17005 +               goto out;
17006 +       token = err;
17007 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
17008 +             token, result.negated, result.uint_64);
17009 +
17010 +       err = -EINVAL;
17011 +       a->skipped = 0;
17012 +       switch (token) {
17013 +       case Opt_br:
17014 +               err = au_fsctx_parse_br(fc, param->string);
17015 +               break;
17016 +       case Opt_add:
17017 +               err = au_fsctx_parse_add(fc, param->string);
17018 +               break;
17019 +       case Opt_append:
17020 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17021 +                                           /*dummy bindex*/1);
17022 +               break;
17023 +       case Opt_prepend:
17024 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17025 +                                           /*bindex*/0);
17026 +               break;
17027 +
17028 +       case Opt_del:
17029 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17030 +               break;
17031 +#if 0 /* reserved for future use */
17032 +       case Opt_idel:
17033 +               if (!a->sb) {
17034 +                       err = 0;
17035 +                       a->skipped = 1;
17036 +                       break;
17037 +               }
17038 +               del->pathname = "(indexed)";
17039 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17040 +               break;
17041 +#endif
17042 +
17043 +       case Opt_mod:
17044 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17045 +               break;
17046 +#ifdef IMOD /* reserved for future use */
17047 +       case Opt_imod:
17048 +               if (!a->sb) {
17049 +                       err = 0;
17050 +                       a->skipped = 1;
17051 +                       break;
17052 +               }
17053 +               u.mod->path = "(indexed)";
17054 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17055 +               break;
17056 +#endif
17057 +
17058 +       case Opt_xino:
17059 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17060 +               break;
17061 +       case Opt_trunc_xino_path:
17062 +               if (!a->sb) {
17063 +                       errorfc(fc, "no such branch %s", param->string);
17064 +                       break;
17065 +               }
17066 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17067 +                                                     param->string);
17068 +               break;
17069 +#if 0
17070 +       case Opt_trunc_xino_v:
17071 +               if (!a->sb) {
17072 +                       err = 0;
17073 +                       a->skipped = 1;
17074 +                       break;
17075 +               }
17076 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17077 +                                                     param->string);
17078 +               break;
17079 +#endif
17080 +       case Opt_itrunc_xino:
17081 +               if (!a->sb) {
17082 +                       errorfc(fc, "out of bounds %s", param->string);
17083 +                       break;
17084 +               }
17085 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17086 +                                                result.int_32);
17087 +               break;
17088 +
17089 +       case Opt_dirwh:
17090 +               err = 0;
17091 +               opt->dirwh = result.int_32;
17092 +               break;
17093 +
17094 +       case Opt_rdcache:
17095 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17096 +                       errorfc(fc, "rdcache must be smaller than %d",
17097 +                               AUFS_RDCACHE_MAX);
17098 +                       break;
17099 +               }
17100 +               err = 0;
17101 +               opt->rdcache = result.int_32;
17102 +               break;
17103 +
17104 +       case Opt_rdblk:
17105 +               err = 0;
17106 +               opt->rdblk = AUFS_RDBLK_DEF;
17107 +               if (!strcmp(param->string, "def"))
17108 +                       break;
17109 +
17110 +               err = kstrtoint(param->string, 0, &result.int_32);
17111 +               if (unlikely(err)) {
17112 +                       errorfc(fc, "bad value in %s", param->key);
17113 +                       break;
17114 +               }
17115 +               err = -EINVAL;
17116 +               if (unlikely(result.int_32 < 0
17117 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17118 +                       errorfc(fc, "bad value in %s", param->key);
17119 +                       break;
17120 +               }
17121 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17122 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17123 +                       break;
17124 +               }
17125 +               err = 0;
17126 +               opt->rdblk = result.int_32;
17127 +               break;
17128 +
17129 +       case Opt_rdhash:
17130 +               err = 0;
17131 +               opt->rdhash = AUFS_RDHASH_DEF;
17132 +               if (!strcmp(param->string, "def"))
17133 +                       break;
17134 +
17135 +               err = kstrtoint(param->string, 0, &result.int_32);
17136 +               if (unlikely(err)) {
17137 +                       errorfc(fc, "bad value in %s", param->key);
17138 +                       break;
17139 +               }
17140 +               /* how about zero? */
17141 +               if (result.int_32 < 0
17142 +                   || result.int_32 * sizeof(struct hlist_head)
17143 +                   > KMALLOC_MAX_SIZE) {
17144 +                       err = -EINVAL;
17145 +                       errorfc(fc, "bad integer in %s", param->key);
17146 +                       break;
17147 +               }
17148 +               opt->rdhash = result.int_32;
17149 +               break;
17150 +
17151 +       case Opt_diropq:
17152 +               /*
17153 +                * As other options, fs/aufs/opts.c can handle these strings by
17154 +                * match_token().  But "diropq=" is deprecated now and will
17155 +                * never have other value.  So simple strcmp() is enough here.
17156 +                */
17157 +               if (!strcmp(param->string, "a") ||
17158 +                   !strcmp(param->string, "always")) {
17159 +                       err = 0;
17160 +                       opt->type = Opt_diropq_a;
17161 +               } else if (!strcmp(param->string, "w") ||
17162 +                          !strcmp(param->string, "whiteouted")) {
17163 +                       err = 0;
17164 +                       opt->type = Opt_diropq_w;
17165 +               } else
17166 +                       errorfc(fc, "unknown value %s", param->string);
17167 +               break;
17168 +
17169 +       case Opt_udba:
17170 +               opt->udba = au_udba_val(param->string);
17171 +               if (opt->udba >= 0)
17172 +                       err = 0;
17173 +               else
17174 +                       errorf(fc, "wrong value, %s", param->string);
17175 +               break;
17176 +
17177 +       case Opt_wbr_create:
17178 +               opt->wbr_create.wbr_create
17179 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17180 +               if (opt->wbr_create.wbr_create >= 0)
17181 +                       err = 0;
17182 +               else
17183 +                       errorf(fc, "wrong value, %s", param->key);
17184 +               break;
17185 +
17186 +       case Opt_wbr_copyup:
17187 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17188 +               if (opt->wbr_copyup >= 0)
17189 +                       err = 0;
17190 +               else
17191 +                       errorfc(fc, "wrong value, %s", param->key);
17192 +               break;
17193 +
17194 +       case Opt_fhsm_sec:
17195 +               if (unlikely(result.int_32 < 0)) {
17196 +                       errorfc(fc, "bad integer in %s\n", param->key);
17197 +                       break;
17198 +               }
17199 +               err = 0;
17200 +               if (sysaufs_brs)
17201 +                       opt->fhsm_second = result.int_32;
17202 +               else
17203 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17204 +               break;
17205 +
17206 +       /* simple true/false flag */
17207 +#define au_fsctx_TF(name)                              \
17208 +               case Opt_##name:                        \
17209 +                       err = 0;                        \
17210 +                       opt->tf = !result.negated;      \
17211 +                       break
17212 +       au_fsctx_TF(trunc_xino);
17213 +       au_fsctx_TF(trunc_xib);
17214 +       au_fsctx_TF(dirperm1);
17215 +       au_fsctx_TF(plink);
17216 +       au_fsctx_TF(shwh);
17217 +       au_fsctx_TF(dio);
17218 +       au_fsctx_TF(warn_perm);
17219 +       au_fsctx_TF(verbose);
17220 +       au_fsctx_TF(sum);
17221 +       au_fsctx_TF(dirren);
17222 +       au_fsctx_TF(acl);
17223 +#undef au_fsctx_TF
17224 +
17225 +       case Opt_noverbose:
17226 +               err = 0;
17227 +               opt->type = Opt_verbose;
17228 +               opt->tf = false;
17229 +               break;
17230 +
17231 +       case Opt_noxino:
17232 +               fallthrough;
17233 +       case Opt_list_plink:
17234 +               fallthrough;
17235 +       case Opt_wsum:
17236 +               err = 0;
17237 +               break;
17238 +
17239 +       case Opt_ignore:
17240 +               warnfc(fc, "ignored %s", param->key);
17241 +               fallthrough;
17242 +       case Opt_ignore_silent:
17243 +               a->skipped = 1;
17244 +               err = 0;
17245 +               break;
17246 +       default:
17247 +               a->skipped = 1;
17248 +               err = -ENOPARAM;
17249 +               break;
17250 +       }
17251 +       if (unlikely(err))
17252 +               goto out;
17253 +       if (a->skipped)
17254 +               goto out;
17255 +
17256 +       switch (token) {
17257 +       case Opt_br:
17258 +               fallthrough;
17259 +       case Opt_noverbose:
17260 +               fallthrough;
17261 +       case Opt_diropq:
17262 +               break;
17263 +       default:
17264 +               opt->type = token;
17265 +               break;
17266 +       }
17267 +       opt++;
17268 +       if (unlikely(opt > a->opt_tail)) {
17269 +               err = -E2BIG;
17270 +               opt--;
17271 +       }
17272 +       opt->type = Opt_tail;
17273 +       a->opt = opt;
17274 +
17275 +out:
17276 +       return err;
17277 +}
17278 +
17279 +/*
17280 + * these options accept both 'name=val' and 'name:val' form.
17281 + * some accept optional '=' in its value.
17282 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17283 + */
17284 +static inline unsigned int is_colonopt(char *str)
17285 +{
17286 +#define do_test(name)                                  \
17287 +       if (!strncmp(str, name ":", sizeof(name)))      \
17288 +               return sizeof(name) - 1
17289 +       do_test("br");
17290 +       do_test("add");
17291 +       do_test("ins");
17292 +       do_test("append");
17293 +       do_test("prepend");
17294 +       do_test("del");
17295 +       /* do_test("idel"); */
17296 +       do_test("mod");
17297 +       /* do_test("imod"); */
17298 +#undef do_test
17299 +
17300 +       return 0;
17301 +}
17302 +
17303 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17304 +{
17305 +       int err;
17306 +       unsigned int u;
17307 +       char *str;
17308 +       struct au_fsctx_opts *a = fc->fs_private;
17309 +
17310 +       str = data;
17311 +       AuDbg("str %s\n", str);
17312 +       while (str) {
17313 +               u = is_colonopt(str);
17314 +               if (u)
17315 +                       str[u] = '=';
17316 +               str = strchr(str, ',');
17317 +               if (!str)
17318 +                       break;
17319 +               str++;
17320 +       }
17321 +       str = data;
17322 +       AuDbg("str %s\n", str);
17323 +
17324 +       err = generic_parse_monolithic(fc, str);
17325 +       AuTraceErr(err);
17326 +       au_fsctx_dump(&a->opts);
17327 +
17328 +       return err;
17329 +}
17330 +
17331 +/* ---------------------------------------------------------------------- */
17332 +
17333 +static void au_fsctx_opts_free(struct au_opts *opts)
17334 +{
17335 +       struct au_opt *opt;
17336 +
17337 +       opt = opts->opt;
17338 +       while (opt->type != Opt_tail) {
17339 +               switch (opt->type) {
17340 +               case Opt_add:
17341 +                       fallthrough;
17342 +               case Opt_append:
17343 +                       fallthrough;
17344 +               case Opt_prepend:
17345 +                       kfree(opt->add.pathname);
17346 +                       path_put(&opt->add.path);
17347 +                       break;
17348 +               case Opt_del:
17349 +                       kfree(opt->del.pathname);
17350 +                       fallthrough;
17351 +               case Opt_idel:
17352 +                       path_put(&opt->del.h_path);
17353 +                       break;
17354 +               case Opt_mod:
17355 +                       kfree(opt->mod.path);
17356 +                       fallthrough;
17357 +               case Opt_imod:
17358 +                       dput(opt->mod.h_root);
17359 +                       break;
17360 +               case Opt_xino:
17361 +                       kfree(opt->xino.path);
17362 +                       fput(opt->xino.file);
17363 +                       break;
17364 +               }
17365 +               opt++;
17366 +       }
17367 +}
17368 +
17369 +static void au_fsctx_free(struct fs_context *fc)
17370 +{
17371 +       struct au_fsctx_opts *a = fc->fs_private;
17372 +
17373 +       /* fs_type=%p, root=%pD */
17374 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17375 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17376 +
17377 +       kobject_put(&a->sbinfo->si_kobj);
17378 +       au_fsctx_opts_free(&a->opts);
17379 +       free_page((unsigned long)a->opts.opt);
17380 +       au_kfree_rcu(a);
17381 +}
17382 +
17383 +static const struct fs_context_operations au_fsctx_ops = {
17384 +       .free                   = au_fsctx_free,
17385 +       .parse_param            = au_fsctx_parse_param,
17386 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17387 +       .get_tree               = au_fsctx_get_tree,
17388 +       .reconfigure            = au_fsctx_reconfigure
17389 +       /*
17390 +        * nfs4 requires ->dup()? No.
17391 +        * I don't know what is this ->dup() for.
17392 +        */
17393 +};
17394 +
17395 +int aufs_fsctx_init(struct fs_context *fc)
17396 +{
17397 +       int err;
17398 +       struct au_fsctx_opts *a;
17399 +
17400 +       /* fs_type=%p, root=%pD */
17401 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17402 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17403 +
17404 +       /* they will be freed by au_fsctx_free() */
17405 +       err = -ENOMEM;
17406 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17407 +       if (unlikely(!a))
17408 +               goto out;
17409 +       a->bindex = 0;
17410 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17411 +       if (unlikely(!a->opts.opt))
17412 +               goto out_a;
17413 +       a->opt = a->opts.opt;
17414 +       a->opt->type = Opt_tail;
17415 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17416 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17417 +       a->opts.sb_flags = fc->sb_flags;
17418 +
17419 +       a->sb = NULL;
17420 +       if (fc->root) {
17421 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17422 +               a->opts.flags = AuOpts_REMOUNT;
17423 +               a->sb = fc->root->d_sb;
17424 +               a->sbinfo = au_sbi(a->sb);
17425 +               kobject_get(&a->sbinfo->si_kobj);
17426 +       } else {
17427 +               a->sbinfo = au_si_alloc(a->sb);
17428 +               AuDebugOn(!a->sbinfo);
17429 +               err = PTR_ERR(a->sbinfo);
17430 +               if (IS_ERR(a->sbinfo))
17431 +                       goto out_opt;
17432 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17433 +       }
17434 +
17435 +       err = 0;
17436 +       fc->fs_private = a;
17437 +       fc->ops = &au_fsctx_ops;
17438 +       goto out; /* success */
17439 +
17440 +out_opt:
17441 +       free_page((unsigned long)a->opts.opt);
17442 +out_a:
17443 +       au_kfree_rcu(a);
17444 +out:
17445 +       AuTraceErr(err);
17446 +       return err;
17447 +}
17448 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17449 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17450 +++ linux/fs/aufs/fstype.h      2022-11-05 23:02:18.965889284 +0100
17451 @@ -0,0 +1,401 @@
17452 +/* SPDX-License-Identifier: GPL-2.0 */
17453 +/*
17454 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17455 + *
17456 + * This program is free software; you can redistribute it and/or modify
17457 + * it under the terms of the GNU General Public License as published by
17458 + * the Free Software Foundation; either version 2 of the License, or
17459 + * (at your option) any later version.
17460 + *
17461 + * This program is distributed in the hope that it will be useful,
17462 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17463 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17464 + * GNU General Public License for more details.
17465 + *
17466 + * You should have received a copy of the GNU General Public License
17467 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17468 + */
17469 +
17470 +/*
17471 + * judging filesystem type
17472 + */
17473 +
17474 +#ifndef __AUFS_FSTYPE_H__
17475 +#define __AUFS_FSTYPE_H__
17476 +
17477 +#ifdef __KERNEL__
17478 +
17479 +#include <linux/fs.h>
17480 +#include <linux/magic.h>
17481 +#include <linux/nfs_fs.h>
17482 +#include <linux/romfs_fs.h>
17483 +
17484 +static inline int au_test_aufs(struct super_block *sb)
17485 +{
17486 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17487 +}
17488 +
17489 +static inline const char *au_sbtype(struct super_block *sb)
17490 +{
17491 +       return sb->s_type->name;
17492 +}
17493 +
17494 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17495 +{
17496 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17497 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17498 +#else
17499 +       return 0;
17500 +#endif
17501 +}
17502 +
17503 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17504 +{
17505 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17506 +       return sb->s_magic == ROMFS_MAGIC;
17507 +#else
17508 +       return 0;
17509 +#endif
17510 +}
17511 +
17512 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17513 +{
17514 +#if IS_ENABLED(CONFIG_CRAMFS)
17515 +       return sb->s_magic == CRAMFS_MAGIC;
17516 +#endif
17517 +       return 0;
17518 +}
17519 +
17520 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17521 +{
17522 +#if IS_ENABLED(CONFIG_NFS_FS)
17523 +       return sb->s_magic == NFS_SUPER_MAGIC;
17524 +#else
17525 +       return 0;
17526 +#endif
17527 +}
17528 +
17529 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17530 +{
17531 +#if IS_ENABLED(CONFIG_FUSE_FS)
17532 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17533 +#else
17534 +       return 0;
17535 +#endif
17536 +}
17537 +
17538 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17539 +{
17540 +#if IS_ENABLED(CONFIG_XFS_FS)
17541 +       return sb->s_magic == XFS_SB_MAGIC;
17542 +#else
17543 +       return 0;
17544 +#endif
17545 +}
17546 +
17547 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17548 +{
17549 +#ifdef CONFIG_TMPFS
17550 +       return sb->s_magic == TMPFS_MAGIC;
17551 +#else
17552 +       return 0;
17553 +#endif
17554 +}
17555 +
17556 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17557 +{
17558 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17559 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17560 +#else
17561 +       return 0;
17562 +#endif
17563 +}
17564 +
17565 +static inline int au_test_ramfs(struct super_block *sb)
17566 +{
17567 +       return sb->s_magic == RAMFS_MAGIC;
17568 +}
17569 +
17570 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17571 +{
17572 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17573 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17574 +#else
17575 +       return 0;
17576 +#endif
17577 +}
17578 +
17579 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17580 +{
17581 +#ifdef CONFIG_PROC_FS
17582 +       return sb->s_magic == PROC_SUPER_MAGIC;
17583 +#else
17584 +       return 0;
17585 +#endif
17586 +}
17587 +
17588 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17589 +{
17590 +#ifdef CONFIG_SYSFS
17591 +       return sb->s_magic == SYSFS_MAGIC;
17592 +#else
17593 +       return 0;
17594 +#endif
17595 +}
17596 +
17597 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17598 +{
17599 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17600 +       return sb->s_magic == CONFIGFS_MAGIC;
17601 +#else
17602 +       return 0;
17603 +#endif
17604 +}
17605 +
17606 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17607 +{
17608 +#if IS_ENABLED(CONFIG_MINIX_FS)
17609 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17610 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17611 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17612 +               || sb->s_magic == MINIX_SUPER_MAGIC
17613 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17614 +#else
17615 +       return 0;
17616 +#endif
17617 +}
17618 +
17619 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17620 +{
17621 +#if IS_ENABLED(CONFIG_FAT_FS)
17622 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17623 +#else
17624 +       return 0;
17625 +#endif
17626 +}
17627 +
17628 +static inline int au_test_msdos(struct super_block *sb)
17629 +{
17630 +       return au_test_fat(sb);
17631 +}
17632 +
17633 +static inline int au_test_vfat(struct super_block *sb)
17634 +{
17635 +       return au_test_fat(sb);
17636 +}
17637 +
17638 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17639 +{
17640 +#ifdef CONFIG_SECURITYFS
17641 +       return sb->s_magic == SECURITYFS_MAGIC;
17642 +#else
17643 +       return 0;
17644 +#endif
17645 +}
17646 +
17647 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17648 +{
17649 +#if IS_ENABLED(CONFIG_SQUASHFS)
17650 +       return sb->s_magic == SQUASHFS_MAGIC;
17651 +#else
17652 +       return 0;
17653 +#endif
17654 +}
17655 +
17656 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17657 +{
17658 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17659 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17660 +#else
17661 +       return 0;
17662 +#endif
17663 +}
17664 +
17665 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17666 +{
17667 +#if IS_ENABLED(CONFIG_XENFS)
17668 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17669 +#else
17670 +       return 0;
17671 +#endif
17672 +}
17673 +
17674 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17675 +{
17676 +#ifdef CONFIG_DEBUG_FS
17677 +       return sb->s_magic == DEBUGFS_MAGIC;
17678 +#else
17679 +       return 0;
17680 +#endif
17681 +}
17682 +
17683 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17684 +{
17685 +#if IS_ENABLED(CONFIG_NILFS)
17686 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17687 +#else
17688 +       return 0;
17689 +#endif
17690 +}
17691 +
17692 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17693 +{
17694 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17695 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17696 +#else
17697 +       return 0;
17698 +#endif
17699 +}
17700 +
17701 +/* ---------------------------------------------------------------------- */
17702 +/*
17703 + * they can't be an aufs branch.
17704 + */
17705 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17706 +{
17707 +       return
17708 +#ifndef CONFIG_AUFS_BR_RAMFS
17709 +               au_test_ramfs(sb) ||
17710 +#endif
17711 +               au_test_procfs(sb)
17712 +               || au_test_sysfs(sb)
17713 +               || au_test_configfs(sb)
17714 +               || au_test_debugfs(sb)
17715 +               || au_test_securityfs(sb)
17716 +               || au_test_xenfs(sb)
17717 +               || au_test_ecryptfs(sb)
17718 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17719 +               || au_test_aufs(sb); /* will be supported in next version */
17720 +}
17721 +
17722 +static inline int au_test_fs_remote(struct super_block *sb)
17723 +{
17724 +       return !au_test_tmpfs(sb)
17725 +#ifdef CONFIG_AUFS_BR_RAMFS
17726 +               && !au_test_ramfs(sb)
17727 +#endif
17728 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17729 +}
17730 +
17731 +/* ---------------------------------------------------------------------- */
17732 +
17733 +/*
17734 + * Note: these functions (below) are created after reading ->getattr() in all
17735 + * filesystems under linux/fs. it means we have to do so in every update...
17736 + */
17737 +
17738 +/*
17739 + * some filesystems require getattr to refresh the inode attributes before
17740 + * referencing.
17741 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17742 + * and leave the work for d_revalidate()
17743 + */
17744 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17745 +{
17746 +       return au_test_nfs(sb)
17747 +               || au_test_fuse(sb)
17748 +               /* || au_test_btrfs(sb) */      /* untested */
17749 +               ;
17750 +}
17751 +
17752 +/*
17753 + * filesystems which don't maintain i_size or i_blocks.
17754 + */
17755 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17756 +{
17757 +       return au_test_xfs(sb)
17758 +               || au_test_btrfs(sb)
17759 +               || au_test_ubifs(sb)
17760 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17761 +               /* || au_test_minix(sb) */      /* untested */
17762 +               ;
17763 +}
17764 +
17765 +/*
17766 + * filesystems which don't store the correct value in some of their inode
17767 + * attributes.
17768 + */
17769 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17770 +{
17771 +       return au_test_fs_bad_iattr_size(sb)
17772 +               || au_test_fat(sb)
17773 +               || au_test_msdos(sb)
17774 +               || au_test_vfat(sb);
17775 +}
17776 +
17777 +/* they don't check i_nlink in link(2) */
17778 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17779 +{
17780 +       return au_test_tmpfs(sb)
17781 +#ifdef CONFIG_AUFS_BR_RAMFS
17782 +               || au_test_ramfs(sb)
17783 +#endif
17784 +               || au_test_ubifs(sb)
17785 +               || au_test_hfsplus(sb);
17786 +}
17787 +
17788 +/*
17789 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17790 + */
17791 +static inline int au_test_fs_notime(struct super_block *sb)
17792 +{
17793 +       return au_test_nfs(sb)
17794 +               || au_test_fuse(sb)
17795 +               || au_test_ubifs(sb)
17796 +               ;
17797 +}
17798 +
17799 +/* temporary support for i#1 in cramfs */
17800 +static inline int au_test_fs_unique_ino(struct inode *inode)
17801 +{
17802 +       if (au_test_cramfs(inode->i_sb))
17803 +               return inode->i_ino != 1;
17804 +       return 1;
17805 +}
17806 +
17807 +/* ---------------------------------------------------------------------- */
17808 +
17809 +/*
17810 + * the filesystem where the xino files placed must support i/o after unlink and
17811 + * maintain i_size and i_blocks.
17812 + */
17813 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17814 +{
17815 +       return au_test_fs_remote(sb)
17816 +               || au_test_fs_bad_iattr_size(sb)
17817 +               /* don't want unnecessary work for xino */
17818 +               || au_test_aufs(sb)
17819 +               || au_test_ecryptfs(sb)
17820 +               || au_test_nilfs(sb);
17821 +}
17822 +
17823 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17824 +{
17825 +       return au_test_tmpfs(sb)
17826 +               || au_test_ramfs(sb);
17827 +}
17828 +
17829 +/*
17830 + * test if the @sb is real-readonly.
17831 + */
17832 +static inline int au_test_fs_rr(struct super_block *sb)
17833 +{
17834 +       return au_test_squashfs(sb)
17835 +               || au_test_iso9660(sb)
17836 +               || au_test_cramfs(sb)
17837 +               || au_test_romfs(sb);
17838 +}
17839 +
17840 +/*
17841 + * test if the @inode is nfs with 'noacl' option
17842 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17843 + */
17844 +static inline int au_test_nfs_noacl(struct inode *inode)
17845 +{
17846 +       return au_test_nfs(inode->i_sb)
17847 +               /* && IS_POSIXACL(inode) */
17848 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17849 +}
17850 +
17851 +#endif /* __KERNEL__ */
17852 +#endif /* __AUFS_FSTYPE_H__ */
17853 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17854 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17855 +++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
17856 @@ -0,0 +1,65 @@
17857 +/* SPDX-License-Identifier: GPL-2.0 */
17858 +/*
17859 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17860 + *
17861 + * This program is free software; you can redistribute it and/or modify
17862 + * it under the terms of the GNU General Public License as published by
17863 + * the Free Software Foundation; either version 2 of the License, or
17864 + * (at your option) any later version.
17865 + *
17866 + * This program is distributed in the hope that it will be useful,
17867 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17868 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17869 + * GNU General Public License for more details.
17870 + *
17871 + * You should have received a copy of the GNU General Public License
17872 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17873 + */
17874 +
17875 +/*
17876 + * helpers for hlist_bl.h
17877 + */
17878 +
17879 +#ifndef __AUFS_HBL_H__
17880 +#define __AUFS_HBL_H__
17881 +
17882 +#ifdef __KERNEL__
17883 +
17884 +#include <linux/list_bl.h>
17885 +
17886 +static inline void au_hbl_add(struct hlist_bl_node *node,
17887 +                             struct hlist_bl_head *hbl)
17888 +{
17889 +       hlist_bl_lock(hbl);
17890 +       hlist_bl_add_head(node, hbl);
17891 +       hlist_bl_unlock(hbl);
17892 +}
17893 +
17894 +static inline void au_hbl_del(struct hlist_bl_node *node,
17895 +                             struct hlist_bl_head *hbl)
17896 +{
17897 +       hlist_bl_lock(hbl);
17898 +       hlist_bl_del(node);
17899 +       hlist_bl_unlock(hbl);
17900 +}
17901 +
17902 +#define au_hbl_for_each(pos, head)                                     \
17903 +       for (pos = hlist_bl_first(head);                                \
17904 +            pos;                                                       \
17905 +            pos = pos->next)
17906 +
17907 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17908 +{
17909 +       unsigned long cnt;
17910 +       struct hlist_bl_node *pos;
17911 +
17912 +       cnt = 0;
17913 +       hlist_bl_lock(hbl);
17914 +       au_hbl_for_each(pos, hbl)
17915 +               cnt++;
17916 +       hlist_bl_unlock(hbl);
17917 +       return cnt;
17918 +}
17919 +
17920 +#endif /* __KERNEL__ */
17921 +#endif /* __AUFS_HBL_H__ */
17922 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17923 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17924 +++ linux/fs/aufs/hfsnotify.c   2022-11-05 23:02:18.965889284 +0100
17925 @@ -0,0 +1,290 @@
17926 +// SPDX-License-Identifier: GPL-2.0
17927 +/*
17928 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17929 + *
17930 + * This program is free software; you can redistribute it and/or modify
17931 + * it under the terms of the GNU General Public License as published by
17932 + * the Free Software Foundation; either version 2 of the License, or
17933 + * (at your option) any later version.
17934 + *
17935 + * This program is distributed in the hope that it will be useful,
17936 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17937 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17938 + * GNU General Public License for more details.
17939 + *
17940 + * You should have received a copy of the GNU General Public License
17941 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17942 + */
17943 +
17944 +/*
17945 + * fsnotify for the lower directories
17946 + */
17947 +
17948 +#include "aufs.h"
17949 +
17950 +/* FS_IN_IGNORED is unnecessary */
17951 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17952 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17953 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17954 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17955 +
17956 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17957 +{
17958 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17959 +                                            hn_mark);
17960 +       /* AuDbg("here\n"); */
17961 +       au_cache_free_hnotify(hn);
17962 +       smp_mb__before_atomic(); /* for atomic64_dec */
17963 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17964 +               wake_up(&au_hfsn_wq);
17965 +}
17966 +
17967 +static int au_hfsn_alloc(struct au_hinode *hinode)
17968 +{
17969 +       int err;
17970 +       struct au_hnotify *hn;
17971 +       struct super_block *sb;
17972 +       struct au_branch *br;
17973 +       struct fsnotify_mark *mark;
17974 +       aufs_bindex_t bindex;
17975 +
17976 +       hn = hinode->hi_notify;
17977 +       sb = hn->hn_aufs_inode->i_sb;
17978 +       bindex = au_br_index(sb, hinode->hi_id);
17979 +       br = au_sbr(sb, bindex);
17980 +       AuDebugOn(!br->br_hfsn);
17981 +
17982 +       mark = &hn->hn_mark;
17983 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17984 +       mark->mask = AuHfsnMask;
17985 +       /*
17986 +        * by udba rename or rmdir, aufs assign a new inode to the known
17987 +        * h_inode, so specify 1 to allow dups.
17988 +        */
17989 +       lockdep_off();
17990 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17991 +       lockdep_on();
17992 +
17993 +       return err;
17994 +}
17995 +
17996 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17997 +{
17998 +       struct fsnotify_mark *mark;
17999 +       unsigned long long ull;
18000 +       struct fsnotify_group *group;
18001 +
18002 +       ull = atomic64_inc_return(&au_hfsn_ifree);
18003 +       BUG_ON(!ull);
18004 +
18005 +       mark = &hn->hn_mark;
18006 +       spin_lock(&mark->lock);
18007 +       group = mark->group;
18008 +       fsnotify_get_group(group);
18009 +       spin_unlock(&mark->lock);
18010 +       lockdep_off();
18011 +       fsnotify_destroy_mark(mark, group);
18012 +       fsnotify_put_mark(mark);
18013 +       fsnotify_put_group(group);
18014 +       lockdep_on();
18015 +
18016 +       /* free hn by myself */
18017 +       return 0;
18018 +}
18019 +
18020 +/* ---------------------------------------------------------------------- */
18021 +
18022 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18023 +{
18024 +       struct fsnotify_mark *mark;
18025 +
18026 +       mark = &hinode->hi_notify->hn_mark;
18027 +       spin_lock(&mark->lock);
18028 +       if (do_set) {
18029 +               AuDebugOn(mark->mask & AuHfsnMask);
18030 +               mark->mask |= AuHfsnMask;
18031 +       } else {
18032 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18033 +               mark->mask &= ~AuHfsnMask;
18034 +       }
18035 +       spin_unlock(&mark->lock);
18036 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18037 +}
18038 +
18039 +/* ---------------------------------------------------------------------- */
18040 +
18041 +/* #define AuDbgHnotify */
18042 +#ifdef AuDbgHnotify
18043 +static char *au_hfsn_name(u32 mask)
18044 +{
18045 +#ifdef CONFIG_AUFS_DEBUG
18046 +#define test_ret(flag)                         \
18047 +       do {                                    \
18048 +               if (mask & flag)                \
18049 +                       return #flag;           \
18050 +       } while (0)
18051 +       test_ret(FS_ACCESS);
18052 +       test_ret(FS_MODIFY);
18053 +       test_ret(FS_ATTRIB);
18054 +       test_ret(FS_CLOSE_WRITE);
18055 +       test_ret(FS_CLOSE_NOWRITE);
18056 +       test_ret(FS_OPEN);
18057 +       test_ret(FS_MOVED_FROM);
18058 +       test_ret(FS_MOVED_TO);
18059 +       test_ret(FS_CREATE);
18060 +       test_ret(FS_DELETE);
18061 +       test_ret(FS_DELETE_SELF);
18062 +       test_ret(FS_MOVE_SELF);
18063 +       test_ret(FS_UNMOUNT);
18064 +       test_ret(FS_Q_OVERFLOW);
18065 +       test_ret(FS_IN_IGNORED);
18066 +       test_ret(FS_ISDIR);
18067 +       test_ret(FS_IN_ONESHOT);
18068 +       test_ret(FS_EVENT_ON_CHILD);
18069 +       return "";
18070 +#undef test_ret
18071 +#else
18072 +       return "??";
18073 +#endif
18074 +}
18075 +#endif
18076 +
18077 +/* ---------------------------------------------------------------------- */
18078 +
18079 +static void au_hfsn_free_group(struct fsnotify_group *group)
18080 +{
18081 +       struct au_br_hfsnotify *hfsn = group->private;
18082 +
18083 +       /* AuDbg("here\n"); */
18084 +       au_kfree_try_rcu(hfsn);
18085 +}
18086 +
18087 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18088 +                               u32 mask, const void *data, int data_type,
18089 +                               struct inode *dir,
18090 +                               const struct qstr *file_name, u32 cookie,
18091 +                               struct fsnotify_iter_info *iter_info)
18092 +{
18093 +       int err;
18094 +       struct au_hnotify *hnotify;
18095 +       struct inode *h_dir, *h_inode;
18096 +       struct fsnotify_mark *inode_mark;
18097 +
18098 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18099 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18100 +
18101 +       err = 0;
18102 +       /* if FS_UNMOUNT happens, there must be another bug */
18103 +       AuDebugOn(mask & FS_UNMOUNT);
18104 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18105 +               goto out;
18106 +
18107 +       h_dir = dir;
18108 +       h_inode = NULL;
18109 +#ifdef AuDbgHnotify
18110 +       au_debug_on();
18111 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18112 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18113 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18114 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18115 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18116 +               /* WARN_ON(1); */
18117 +       }
18118 +       au_debug_off();
18119 +#endif
18120 +
18121 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18122 +       AuDebugOn(!inode_mark);
18123 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18124 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18125 +
18126 +out:
18127 +       return err;
18128 +}
18129 +
18130 +static struct fsnotify_ops au_hfsn_ops = {
18131 +       .handle_event           = au_hfsn_handle_event,
18132 +       .free_group_priv        = au_hfsn_free_group,
18133 +       .free_mark              = au_hfsn_free_mark
18134 +};
18135 +
18136 +/* ---------------------------------------------------------------------- */
18137 +
18138 +static void au_hfsn_fin_br(struct au_branch *br)
18139 +{
18140 +       struct au_br_hfsnotify *hfsn;
18141 +
18142 +       hfsn = br->br_hfsn;
18143 +       if (hfsn) {
18144 +               lockdep_off();
18145 +               fsnotify_put_group(hfsn->hfsn_group);
18146 +               lockdep_on();
18147 +       }
18148 +}
18149 +
18150 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18151 +{
18152 +       int err;
18153 +       struct fsnotify_group *group;
18154 +       struct au_br_hfsnotify *hfsn;
18155 +
18156 +       err = 0;
18157 +       br->br_hfsn = NULL;
18158 +       if (!au_br_hnotifyable(perm))
18159 +               goto out;
18160 +
18161 +       err = -ENOMEM;
18162 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18163 +       if (unlikely(!hfsn))
18164 +               goto out;
18165 +
18166 +       err = 0;
18167 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18168 +                                    /*flags - not for userspace*/0);
18169 +       if (IS_ERR(group)) {
18170 +               err = PTR_ERR(group);
18171 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18172 +               goto out_hfsn;
18173 +       }
18174 +
18175 +       group->private = hfsn;
18176 +       hfsn->hfsn_group = group;
18177 +       br->br_hfsn = hfsn;
18178 +       goto out; /* success */
18179 +
18180 +out_hfsn:
18181 +       au_kfree_try_rcu(hfsn);
18182 +out:
18183 +       return err;
18184 +}
18185 +
18186 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18187 +{
18188 +       int err;
18189 +
18190 +       err = 0;
18191 +       if (!br->br_hfsn)
18192 +               err = au_hfsn_init_br(br, perm);
18193 +
18194 +       return err;
18195 +}
18196 +
18197 +/* ---------------------------------------------------------------------- */
18198 +
18199 +static void au_hfsn_fin(void)
18200 +{
18201 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18202 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18203 +}
18204 +
18205 +const struct au_hnotify_op au_hnotify_op = {
18206 +       .ctl            = au_hfsn_ctl,
18207 +       .alloc          = au_hfsn_alloc,
18208 +       .free           = au_hfsn_free,
18209 +
18210 +       .fin            = au_hfsn_fin,
18211 +
18212 +       .reset_br       = au_hfsn_reset_br,
18213 +       .fin_br         = au_hfsn_fin_br,
18214 +       .init_br        = au_hfsn_init_br
18215 +};
18216 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18217 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18218 +++ linux/fs/aufs/hfsplus.c     2022-11-05 23:02:18.965889284 +0100
18219 @@ -0,0 +1,60 @@
18220 +// SPDX-License-Identifier: GPL-2.0
18221 +/*
18222 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18223 + *
18224 + * This program is free software; you can redistribute it and/or modify
18225 + * it under the terms of the GNU General Public License as published by
18226 + * the Free Software Foundation; either version 2 of the License, or
18227 + * (at your option) any later version.
18228 + *
18229 + * This program is distributed in the hope that it will be useful,
18230 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18231 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18232 + * GNU General Public License for more details.
18233 + *
18234 + * You should have received a copy of the GNU General Public License
18235 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18236 + */
18237 +
18238 +/*
18239 + * special support for filesystems which acquires an inode mutex
18240 + * at final closing a file, eg, hfsplus.
18241 + *
18242 + * This trick is very simple and stupid, just to open the file before really
18243 + * necessary open to tell hfsplus that this is not the final closing.
18244 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18245 + * and au_h_open_post() after releasing it.
18246 + */
18247 +
18248 +#include "aufs.h"
18249 +
18250 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18251 +                          int force_wr)
18252 +{
18253 +       struct file *h_file;
18254 +       struct dentry *h_dentry;
18255 +
18256 +       h_dentry = au_h_dptr(dentry, bindex);
18257 +       AuDebugOn(!h_dentry);
18258 +       AuDebugOn(d_is_negative(h_dentry));
18259 +
18260 +       h_file = NULL;
18261 +       if (au_test_hfsplus(h_dentry->d_sb)
18262 +           && d_is_reg(h_dentry))
18263 +               h_file = au_h_open(dentry, bindex,
18264 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18265 +                                  /*file*/NULL, force_wr);
18266 +       return h_file;
18267 +}
18268 +
18269 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18270 +                   struct file *h_file)
18271 +{
18272 +       struct au_branch *br;
18273 +
18274 +       if (h_file) {
18275 +               fput(h_file);
18276 +               br = au_sbr(dentry->d_sb, bindex);
18277 +               au_lcnt_dec(&br->br_nfiles);
18278 +       }
18279 +}
18280 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18281 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18282 +++ linux/fs/aufs/hnotify.c     2024-03-10 23:40:47.083143450 +0100
18283 @@ -0,0 +1,715 @@
18284 +// SPDX-License-Identifier: GPL-2.0
18285 +/*
18286 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18287 + *
18288 + * This program is free software; you can redistribute it and/or modify
18289 + * it under the terms of the GNU General Public License as published by
18290 + * the Free Software Foundation; either version 2 of the License, or
18291 + * (at your option) any later version.
18292 + *
18293 + * This program is distributed in the hope that it will be useful,
18294 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18295 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18296 + * GNU General Public License for more details.
18297 + *
18298 + * You should have received a copy of the GNU General Public License
18299 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18300 + */
18301 +
18302 +/*
18303 + * abstraction to notify the direct changes on lower directories
18304 + */
18305 +
18306 +/* #include <linux/iversion.h> */
18307 +#include "aufs.h"
18308 +
18309 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18310 +{
18311 +       int err;
18312 +       struct au_hnotify *hn;
18313 +
18314 +       err = -ENOMEM;
18315 +       hn = au_cache_alloc_hnotify();
18316 +       if (hn) {
18317 +               hn->hn_aufs_inode = inode;
18318 +               hinode->hi_notify = hn;
18319 +               err = au_hnotify_op.alloc(hinode);
18320 +               AuTraceErr(err);
18321 +               if (unlikely(err)) {
18322 +                       hinode->hi_notify = NULL;
18323 +                       au_cache_free_hnotify(hn);
18324 +                       /*
18325 +                        * The upper dir was removed by udba, but the same named
18326 +                        * dir left. In this case, aufs assigns a new inode
18327 +                        * number and set the monitor again.
18328 +                        * For the lower dir, the old monitor is still left.
18329 +                        */
18330 +                       if (err == -EEXIST)
18331 +                               err = 0;
18332 +               }
18333 +       }
18334 +
18335 +       AuTraceErr(err);
18336 +       return err;
18337 +}
18338 +
18339 +void au_hn_free(struct au_hinode *hinode)
18340 +{
18341 +       struct au_hnotify *hn;
18342 +
18343 +       hn = hinode->hi_notify;
18344 +       if (hn) {
18345 +               hinode->hi_notify = NULL;
18346 +               if (au_hnotify_op.free(hinode, hn))
18347 +                       au_cache_free_hnotify(hn);
18348 +       }
18349 +}
18350 +
18351 +/* ---------------------------------------------------------------------- */
18352 +
18353 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18354 +{
18355 +       if (hinode->hi_notify)
18356 +               au_hnotify_op.ctl(hinode, do_set);
18357 +}
18358 +
18359 +void au_hn_reset(struct inode *inode, unsigned int flags)
18360 +{
18361 +       aufs_bindex_t bindex, bbot;
18362 +       struct inode *hi;
18363 +       struct dentry *iwhdentry;
18364 +
18365 +       bbot = au_ibbot(inode);
18366 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18367 +               hi = au_h_iptr(inode, bindex);
18368 +               if (!hi)
18369 +                       continue;
18370 +
18371 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18372 +               iwhdentry = au_hi_wh(inode, bindex);
18373 +               if (iwhdentry)
18374 +                       dget(iwhdentry);
18375 +               au_igrab(hi);
18376 +               au_set_h_iptr(inode, bindex, NULL, 0);
18377 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18378 +                             flags & ~AuHi_XINO);
18379 +               iput(hi);
18380 +               dput(iwhdentry);
18381 +               /* inode_unlock(hi); */
18382 +       }
18383 +}
18384 +
18385 +/* ---------------------------------------------------------------------- */
18386 +
18387 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18388 +{
18389 +       int err;
18390 +       aufs_bindex_t bindex, bbot, bfound, btop;
18391 +       struct inode *h_i;
18392 +
18393 +       err = 0;
18394 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18395 +               pr_warn("branch root dir was changed\n");
18396 +               goto out;
18397 +       }
18398 +
18399 +       bfound = -1;
18400 +       bbot = au_ibbot(inode);
18401 +       btop = au_ibtop(inode);
18402 +#if 0 /* reserved for future use */
18403 +       if (bindex == bbot) {
18404 +               /* keep this ino in rename case */
18405 +               goto out;
18406 +       }
18407 +#endif
18408 +       for (bindex = btop; bindex <= bbot; bindex++)
18409 +               if (au_h_iptr(inode, bindex) == h_inode) {
18410 +                       bfound = bindex;
18411 +                       break;
18412 +               }
18413 +       if (bfound < 0)
18414 +               goto out;
18415 +
18416 +       for (bindex = btop; bindex <= bbot; bindex++) {
18417 +               h_i = au_h_iptr(inode, bindex);
18418 +               if (!h_i)
18419 +                       continue;
18420 +
18421 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18422 +               /* ignore this error */
18423 +               /* bad action? */
18424 +       }
18425 +
18426 +       /* children inode number will be broken */
18427 +
18428 +out:
18429 +       AuTraceErr(err);
18430 +       return err;
18431 +}
18432 +
18433 +static int hn_gen_tree(struct dentry *dentry)
18434 +{
18435 +       int err, i, j, ndentry;
18436 +       struct au_dcsub_pages dpages;
18437 +       struct au_dpage *dpage;
18438 +       struct dentry **dentries;
18439 +
18440 +       err = au_dpages_init(&dpages, GFP_NOFS);
18441 +       if (unlikely(err))
18442 +               goto out;
18443 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18444 +       if (unlikely(err))
18445 +               goto out_dpages;
18446 +
18447 +       for (i = 0; i < dpages.ndpage; i++) {
18448 +               dpage = dpages.dpages + i;
18449 +               dentries = dpage->dentries;
18450 +               ndentry = dpage->ndentry;
18451 +               for (j = 0; j < ndentry; j++) {
18452 +                       struct dentry *d;
18453 +
18454 +                       d = dentries[j];
18455 +                       if (IS_ROOT(d))
18456 +                               continue;
18457 +
18458 +                       au_digen_dec(d);
18459 +                       if (d_really_is_positive(d))
18460 +                               /* todo: reset children xino?
18461 +                                  cached children only? */
18462 +                               au_iigen_dec(d_inode(d));
18463 +               }
18464 +       }
18465 +
18466 +out_dpages:
18467 +       au_dpages_free(&dpages);
18468 +out:
18469 +       return err;
18470 +}
18471 +
18472 +/*
18473 + * return 0 if processed.
18474 + */
18475 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18476 +                          const unsigned int isdir)
18477 +{
18478 +       int err;
18479 +       struct dentry *d;
18480 +       struct qstr *dname;
18481 +
18482 +       err = 1;
18483 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18484 +               pr_warn("branch root dir was changed\n");
18485 +               err = 0;
18486 +               goto out;
18487 +       }
18488 +
18489 +       if (!isdir) {
18490 +               AuDebugOn(!name);
18491 +               au_iigen_dec(inode);
18492 +               spin_lock(&inode->i_lock);
18493 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18494 +                       spin_lock(&d->d_lock);
18495 +                       dname = &d->d_name;
18496 +                       if (dname->len != nlen
18497 +                           && memcmp(dname->name, name, nlen)) {
18498 +                               spin_unlock(&d->d_lock);
18499 +                               continue;
18500 +                       }
18501 +                       err = 0;
18502 +                       au_digen_dec(d);
18503 +                       spin_unlock(&d->d_lock);
18504 +                       break;
18505 +               }
18506 +               spin_unlock(&inode->i_lock);
18507 +       } else {
18508 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18509 +               d = d_find_any_alias(inode);
18510 +               if (!d) {
18511 +                       au_iigen_dec(inode);
18512 +                       goto out;
18513 +               }
18514 +
18515 +               spin_lock(&d->d_lock);
18516 +               dname = &d->d_name;
18517 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18518 +                       spin_unlock(&d->d_lock);
18519 +                       err = hn_gen_tree(d);
18520 +                       spin_lock(&d->d_lock);
18521 +               }
18522 +               spin_unlock(&d->d_lock);
18523 +               dput(d);
18524 +       }
18525 +
18526 +out:
18527 +       AuTraceErr(err);
18528 +       return err;
18529 +}
18530 +
18531 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18532 +{
18533 +       int err;
18534 +
18535 +       if (IS_ROOT(dentry)) {
18536 +               pr_warn("branch root dir was changed\n");
18537 +               return 0;
18538 +       }
18539 +
18540 +       err = 0;
18541 +       if (!isdir) {
18542 +               au_digen_dec(dentry);
18543 +               if (d_really_is_positive(dentry))
18544 +                       au_iigen_dec(d_inode(dentry));
18545 +       } else {
18546 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18547 +               if (d_really_is_positive(dentry))
18548 +                       err = hn_gen_tree(dentry);
18549 +       }
18550 +
18551 +       AuTraceErr(err);
18552 +       return err;
18553 +}
18554 +
18555 +/* ---------------------------------------------------------------------- */
18556 +
18557 +/* hnotify job flags */
18558 +#define AuHnJob_XINO0          BIT(0)
18559 +#define AuHnJob_GEN            BIT(1)
18560 +#define AuHnJob_DIRENT         BIT(2)
18561 +#define AuHnJob_ISDIR          BIT(3)
18562 +#define AuHnJob_TRYXINO0       BIT(4)
18563 +#define AuHnJob_MNTPNT         BIT(5)
18564 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18565 +#define au_fset_hnjob(flags, name) \
18566 +       do { (flags) |= AuHnJob_##name; } while (0)
18567 +#define au_fclr_hnjob(flags, name) \
18568 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18569 +
18570 +enum {
18571 +       AuHn_CHILD,
18572 +       AuHn_PARENT,
18573 +       AuHnLast
18574 +};
18575 +
18576 +struct au_hnotify_args {
18577 +       struct inode *h_dir, *dir, *h_child_inode;
18578 +       u32 mask;
18579 +       unsigned int flags[AuHnLast];
18580 +       unsigned int h_child_nlen;
18581 +       char h_child_name[];
18582 +};
18583 +
18584 +struct hn_job_args {
18585 +       unsigned int flags;
18586 +       struct inode *inode, *h_inode, *dir, *h_dir;
18587 +       struct dentry *dentry;
18588 +       char *h_name;
18589 +       int h_nlen;
18590 +};
18591 +
18592 +static int hn_job(struct hn_job_args *a)
18593 +{
18594 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18595 +       int e;
18596 +
18597 +       /* reset xino */
18598 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18599 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18600 +
18601 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18602 +           && a->inode
18603 +           && a->h_inode) {
18604 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18605 +               if (!a->h_inode->i_nlink
18606 +                   && !(a->h_inode->i_state & I_LINKABLE))
18607 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18608 +               inode_unlock_shared(a->h_inode);
18609 +       }
18610 +
18611 +       /* make the generation obsolete */
18612 +       if (au_ftest_hnjob(a->flags, GEN)) {
18613 +               e = -1;
18614 +               if (a->inode)
18615 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18616 +                                             isdir);
18617 +               if (e && a->dentry)
18618 +                       hn_gen_by_name(a->dentry, isdir);
18619 +               /* ignore this error */
18620 +       }
18621 +
18622 +       /* make dir entries obsolete */
18623 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18624 +               struct au_vdir *vdir;
18625 +
18626 +               vdir = au_ivdir(a->inode);
18627 +               if (vdir)
18628 +                       vdir->vd_jiffy = 0;
18629 +               /* IMustLock(a->inode); */
18630 +               /* inode_inc_iversion(a->inode); */
18631 +       }
18632 +
18633 +       /* can do nothing but warn */
18634 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18635 +           && a->dentry
18636 +           && d_mountpoint(a->dentry))
18637 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18638 +
18639 +       return 0;
18640 +}
18641 +
18642 +/* ---------------------------------------------------------------------- */
18643 +
18644 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18645 +                                          struct inode *dir)
18646 +{
18647 +       struct dentry *dentry, *d, *parent;
18648 +       struct qstr *dname;
18649 +
18650 +       parent = d_find_any_alias(dir);
18651 +       if (!parent)
18652 +               return NULL;
18653 +
18654 +       dentry = NULL;
18655 +       spin_lock(&parent->d_lock);
18656 +       hlist_for_each_entry(d, &parent->d_children, d_sib) {
18657 +               /* AuDbg("%pd\n", d); */
18658 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18659 +               dname = &d->d_name;
18660 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18661 +                       goto cont_unlock;
18662 +               if (au_di(d))
18663 +                       au_digen_dec(d);
18664 +               else
18665 +                       goto cont_unlock;
18666 +               if (au_dcount(d) > 0) {
18667 +                       dentry = dget_dlock(d);
18668 +                       spin_unlock(&d->d_lock);
18669 +                       break;
18670 +               }
18671 +
18672 +cont_unlock:
18673 +               spin_unlock(&d->d_lock);
18674 +       }
18675 +       spin_unlock(&parent->d_lock);
18676 +       dput(parent);
18677 +
18678 +       if (dentry)
18679 +               di_write_lock_child(dentry);
18680 +
18681 +       return dentry;
18682 +}
18683 +
18684 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18685 +                                        aufs_bindex_t bindex, ino_t h_ino)
18686 +{
18687 +       struct inode *inode;
18688 +       ino_t ino;
18689 +       int err;
18690 +
18691 +       inode = NULL;
18692 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18693 +       if (!err && ino)
18694 +               inode = ilookup(sb, ino);
18695 +       if (!inode)
18696 +               goto out;
18697 +
18698 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18699 +               pr_warn("wrong root branch\n");
18700 +               iput(inode);
18701 +               inode = NULL;
18702 +               goto out;
18703 +       }
18704 +
18705 +       ii_write_lock_child(inode);
18706 +
18707 +out:
18708 +       return inode;
18709 +}
18710 +
18711 +static void au_hn_bh(void *_args)
18712 +{
18713 +       struct au_hnotify_args *a = _args;
18714 +       struct super_block *sb;
18715 +       aufs_bindex_t bindex, bbot, bfound;
18716 +       unsigned char xino, try_iput;
18717 +       int err;
18718 +       struct inode *inode;
18719 +       ino_t h_ino;
18720 +       struct hn_job_args args;
18721 +       struct dentry *dentry;
18722 +       struct au_sbinfo *sbinfo;
18723 +
18724 +       AuDebugOn(!_args);
18725 +       AuDebugOn(!a->h_dir);
18726 +       AuDebugOn(!a->dir);
18727 +       AuDebugOn(!a->mask);
18728 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18729 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18730 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18731 +
18732 +       inode = NULL;
18733 +       dentry = NULL;
18734 +       /*
18735 +        * do not lock a->dir->i_mutex here
18736 +        * because of d_revalidate() may cause a deadlock.
18737 +        */
18738 +       sb = a->dir->i_sb;
18739 +       AuDebugOn(!sb);
18740 +       sbinfo = au_sbi(sb);
18741 +       AuDebugOn(!sbinfo);
18742 +       si_write_lock(sb, AuLock_NOPLMW);
18743 +
18744 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18745 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18746 +               case FS_MOVED_FROM:
18747 +               case FS_MOVED_TO:
18748 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18749 +                               "for the direct rename(2)\n");
18750 +               }
18751 +
18752 +       ii_read_lock_parent(a->dir);
18753 +       bfound = -1;
18754 +       bbot = au_ibbot(a->dir);
18755 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18756 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18757 +                       bfound = bindex;
18758 +                       break;
18759 +               }
18760 +       ii_read_unlock(a->dir);
18761 +       if (unlikely(bfound < 0))
18762 +               goto out;
18763 +
18764 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18765 +       h_ino = 0;
18766 +       if (a->h_child_inode)
18767 +               h_ino = a->h_child_inode->i_ino;
18768 +
18769 +       if (a->h_child_nlen
18770 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18771 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18772 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18773 +                                             a->dir);
18774 +       try_iput = 0;
18775 +       if (dentry && d_really_is_positive(dentry))
18776 +               inode = d_inode(dentry);
18777 +       if (xino && !inode && h_ino
18778 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18779 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18780 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18781 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18782 +               try_iput = 1;
18783 +       }
18784 +
18785 +       args.flags = a->flags[AuHn_CHILD];
18786 +       args.dentry = dentry;
18787 +       args.inode = inode;
18788 +       args.h_inode = a->h_child_inode;
18789 +       args.dir = a->dir;
18790 +       args.h_dir = a->h_dir;
18791 +       args.h_name = a->h_child_name;
18792 +       args.h_nlen = a->h_child_nlen;
18793 +       err = hn_job(&args);
18794 +       if (dentry) {
18795 +               if (au_di(dentry))
18796 +                       di_write_unlock(dentry);
18797 +               dput(dentry);
18798 +       }
18799 +       if (inode && try_iput) {
18800 +               ii_write_unlock(inode);
18801 +               iput(inode);
18802 +       }
18803 +
18804 +       ii_write_lock_parent(a->dir);
18805 +       args.flags = a->flags[AuHn_PARENT];
18806 +       args.dentry = NULL;
18807 +       args.inode = a->dir;
18808 +       args.h_inode = a->h_dir;
18809 +       args.dir = NULL;
18810 +       args.h_dir = NULL;
18811 +       args.h_name = NULL;
18812 +       args.h_nlen = 0;
18813 +       err = hn_job(&args);
18814 +       ii_write_unlock(a->dir);
18815 +
18816 +out:
18817 +       iput(a->h_child_inode);
18818 +       iput(a->h_dir);
18819 +       iput(a->dir);
18820 +       si_write_unlock(sb);
18821 +       au_nwt_done(&sbinfo->si_nowait);
18822 +       au_kfree_rcu(a);
18823 +}
18824 +
18825 +/* ---------------------------------------------------------------------- */
18826 +
18827 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18828 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18829 +{
18830 +       int err, len;
18831 +       unsigned int flags[AuHnLast], f;
18832 +       unsigned char isdir, isroot, wh;
18833 +       struct inode *dir;
18834 +       struct au_hnotify_args *args;
18835 +       char *p, *h_child_name;
18836 +
18837 +       err = 0;
18838 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18839 +       dir = igrab(hnotify->hn_aufs_inode);
18840 +       if (!dir)
18841 +               goto out;
18842 +
18843 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18844 +       wh = 0;
18845 +       h_child_name = (void *)h_child_qstr->name;
18846 +       len = h_child_qstr->len;
18847 +       if (h_child_name) {
18848 +               if (len > AUFS_WH_PFX_LEN
18849 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18850 +                       h_child_name += AUFS_WH_PFX_LEN;
18851 +                       len -= AUFS_WH_PFX_LEN;
18852 +                       wh = 1;
18853 +               }
18854 +       }
18855 +
18856 +       isdir = 0;
18857 +       if (h_child_inode)
18858 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18859 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18860 +       flags[AuHn_CHILD] = 0;
18861 +       if (isdir)
18862 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18863 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18864 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18865 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18866 +       case FS_MOVED_FROM:
18867 +       case FS_MOVED_TO:
18868 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18869 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18870 +               fallthrough;
18871 +       case FS_CREATE:
18872 +               AuDebugOn(!h_child_name);
18873 +               break;
18874 +
18875 +       case FS_DELETE:
18876 +               /*
18877 +                * aufs never be able to get this child inode.
18878 +                * revalidation should be in d_revalidate()
18879 +                * by checking i_nlink, i_generation or d_unhashed().
18880 +                */
18881 +               AuDebugOn(!h_child_name);
18882 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18883 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18884 +               break;
18885 +
18886 +       default:
18887 +               AuDebugOn(1);
18888 +       }
18889 +
18890 +       if (wh)
18891 +               h_child_inode = NULL;
18892 +
18893 +       err = -ENOMEM;
18894 +       /* iput() and kfree() will be called in au_hnotify() */
18895 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18896 +       if (unlikely(!args)) {
18897 +               AuErr1("no memory\n");
18898 +               iput(dir);
18899 +               goto out;
18900 +       }
18901 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18902 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18903 +       args->mask = mask;
18904 +       args->dir = dir;
18905 +       args->h_dir = igrab(h_dir);
18906 +       if (h_child_inode)
18907 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18908 +       args->h_child_inode = h_child_inode;
18909 +       args->h_child_nlen = len;
18910 +       if (len) {
18911 +               p = (void *)args;
18912 +               p += sizeof(*args);
18913 +               memcpy(p, h_child_name, len);
18914 +               p[len] = 0;
18915 +       }
18916 +
18917 +       /* NFS fires the event for silly-renamed one from kworker */
18918 +       f = 0;
18919 +       if (!dir->i_nlink
18920 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18921 +               f = AuWkq_NEST;
18922 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18923 +       if (unlikely(err)) {
18924 +               pr_err("wkq %d\n", err);
18925 +               iput(args->h_child_inode);
18926 +               iput(args->h_dir);
18927 +               iput(args->dir);
18928 +               au_kfree_rcu(args);
18929 +       }
18930 +
18931 +out:
18932 +       return err;
18933 +}
18934 +
18935 +/* ---------------------------------------------------------------------- */
18936 +
18937 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18938 +{
18939 +       int err;
18940 +
18941 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18942 +
18943 +       err = 0;
18944 +       if (au_hnotify_op.reset_br)
18945 +               err = au_hnotify_op.reset_br(udba, br, perm);
18946 +
18947 +       return err;
18948 +}
18949 +
18950 +int au_hnotify_init_br(struct au_branch *br, int perm)
18951 +{
18952 +       int err;
18953 +
18954 +       err = 0;
18955 +       if (au_hnotify_op.init_br)
18956 +               err = au_hnotify_op.init_br(br, perm);
18957 +
18958 +       return err;
18959 +}
18960 +
18961 +void au_hnotify_fin_br(struct au_branch *br)
18962 +{
18963 +       if (au_hnotify_op.fin_br)
18964 +               au_hnotify_op.fin_br(br);
18965 +}
18966 +
18967 +static void au_hn_destroy_cache(void)
18968 +{
18969 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18970 +       au_cache[AuCache_HNOTIFY] = NULL;
18971 +}
18972 +
18973 +int __init au_hnotify_init(void)
18974 +{
18975 +       int err;
18976 +
18977 +       err = -ENOMEM;
18978 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18979 +       if (au_cache[AuCache_HNOTIFY]) {
18980 +               err = 0;
18981 +               if (au_hnotify_op.init)
18982 +                       err = au_hnotify_op.init();
18983 +               if (unlikely(err))
18984 +                       au_hn_destroy_cache();
18985 +       }
18986 +       AuTraceErr(err);
18987 +       return err;
18988 +}
18989 +
18990 +void au_hnotify_fin(void)
18991 +{
18992 +       if (au_hnotify_op.fin)
18993 +               au_hnotify_op.fin();
18994 +
18995 +       /* cf. au_cache_fin() */
18996 +       if (au_cache[AuCache_HNOTIFY])
18997 +               au_hn_destroy_cache();
18998 +}
18999 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
19000 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
19001 +++ linux/fs/aufs/iinfo.c       2022-11-05 23:02:18.965889284 +0100
19002 @@ -0,0 +1,286 @@
19003 +// SPDX-License-Identifier: GPL-2.0
19004 +/*
19005 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19006 + *
19007 + * This program is free software; you can redistribute it and/or modify
19008 + * it under the terms of the GNU General Public License as published by
19009 + * the Free Software Foundation; either version 2 of the License, or
19010 + * (at your option) any later version.
19011 + *
19012 + * This program is distributed in the hope that it will be useful,
19013 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19014 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19015 + * GNU General Public License for more details.
19016 + *
19017 + * You should have received a copy of the GNU General Public License
19018 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19019 + */
19020 +
19021 +/*
19022 + * inode private data
19023 + */
19024 +
19025 +#include "aufs.h"
19026 +
19027 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19028 +{
19029 +       struct inode *h_inode;
19030 +       struct au_hinode *hinode;
19031 +
19032 +       IiMustAnyLock(inode);
19033 +
19034 +       hinode = au_hinode(au_ii(inode), bindex);
19035 +       h_inode = hinode->hi_inode;
19036 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19037 +       return h_inode;
19038 +}
19039 +
19040 +/* todo: hard/soft set? */
19041 +void au_hiput(struct au_hinode *hinode)
19042 +{
19043 +       au_hn_free(hinode);
19044 +       dput(hinode->hi_whdentry);
19045 +       iput(hinode->hi_inode);
19046 +}
19047 +
19048 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19049 +{
19050 +       unsigned int flags;
19051 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19052 +
19053 +       flags = 0;
19054 +       if (au_opt_test(mnt_flags, XINO))
19055 +               au_fset_hi(flags, XINO);
19056 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19057 +               au_fset_hi(flags, HNOTIFY);
19058 +       return flags;
19059 +}
19060 +
19061 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19062 +                  struct inode *h_inode, unsigned int flags)
19063 +{
19064 +       struct au_hinode *hinode;
19065 +       struct inode *hi;
19066 +       struct au_iinfo *iinfo = au_ii(inode);
19067 +
19068 +       IiMustWriteLock(inode);
19069 +
19070 +       hinode = au_hinode(iinfo, bindex);
19071 +       hi = hinode->hi_inode;
19072 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19073 +
19074 +       if (hi)
19075 +               au_hiput(hinode);
19076 +       hinode->hi_inode = h_inode;
19077 +       if (h_inode) {
19078 +               int err;
19079 +               struct super_block *sb = inode->i_sb;
19080 +               struct au_branch *br;
19081 +
19082 +               AuDebugOn(inode->i_mode
19083 +                         && (h_inode->i_mode & S_IFMT)
19084 +                         != (inode->i_mode & S_IFMT));
19085 +               if (bindex == iinfo->ii_btop)
19086 +                       au_cpup_igen(inode, h_inode);
19087 +               br = au_sbr(sb, bindex);
19088 +               hinode->hi_id = br->br_id;
19089 +               if (au_ftest_hi(flags, XINO)) {
19090 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19091 +                                           inode->i_ino);
19092 +                       if (unlikely(err))
19093 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19094 +               }
19095 +
19096 +               if (au_ftest_hi(flags, HNOTIFY)
19097 +                   && au_br_hnotifyable(br->br_perm)) {
19098 +                       err = au_hn_alloc(hinode, inode);
19099 +                       if (unlikely(err))
19100 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19101 +               }
19102 +       }
19103 +}
19104 +
19105 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19106 +                 struct dentry *h_wh)
19107 +{
19108 +       struct au_hinode *hinode;
19109 +
19110 +       IiMustWriteLock(inode);
19111 +
19112 +       hinode = au_hinode(au_ii(inode), bindex);
19113 +       AuDebugOn(hinode->hi_whdentry);
19114 +       hinode->hi_whdentry = h_wh;
19115 +}
19116 +
19117 +void au_update_iigen(struct inode *inode, int half)
19118 +{
19119 +       struct au_iinfo *iinfo;
19120 +       struct au_iigen *iigen;
19121 +       unsigned int sigen;
19122 +
19123 +       sigen = au_sigen(inode->i_sb);
19124 +       iinfo = au_ii(inode);
19125 +       iigen = &iinfo->ii_generation;
19126 +       spin_lock(&iigen->ig_spin);
19127 +       iigen->ig_generation = sigen;
19128 +       if (half)
19129 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19130 +       else
19131 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19132 +       spin_unlock(&iigen->ig_spin);
19133 +}
19134 +
19135 +/* it may be called at remount time, too */
19136 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19137 +{
19138 +       struct au_iinfo *iinfo;
19139 +       aufs_bindex_t bindex, bbot;
19140 +
19141 +       AuDebugOn(au_is_bad_inode(inode));
19142 +       IiMustWriteLock(inode);
19143 +
19144 +       iinfo = au_ii(inode);
19145 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19146 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19147 +                    bindex++) {
19148 +                       struct inode *h_i;
19149 +
19150 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19151 +                       if (h_i
19152 +                           && !h_i->i_nlink
19153 +                           && !(h_i->i_state & I_LINKABLE))
19154 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19155 +               }
19156 +       }
19157 +
19158 +       iinfo->ii_btop = -1;
19159 +       iinfo->ii_bbot = -1;
19160 +       bbot = au_sbbot(inode->i_sb);
19161 +       for (bindex = 0; bindex <= bbot; bindex++)
19162 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19163 +                       iinfo->ii_btop = bindex;
19164 +                       break;
19165 +               }
19166 +       if (iinfo->ii_btop >= 0)
19167 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19168 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19169 +                               iinfo->ii_bbot = bindex;
19170 +                               break;
19171 +                       }
19172 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19173 +}
19174 +
19175 +/* ---------------------------------------------------------------------- */
19176 +
19177 +void au_icntnr_init_once(void *_c)
19178 +{
19179 +       struct au_icntnr *c = _c;
19180 +       struct au_iinfo *iinfo = &c->iinfo;
19181 +
19182 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19183 +       au_rw_init(&iinfo->ii_rwsem);
19184 +       inode_init_once(&c->vfs_inode);
19185 +}
19186 +
19187 +void au_hinode_init(struct au_hinode *hinode)
19188 +{
19189 +       hinode->hi_inode = NULL;
19190 +       hinode->hi_id = -1;
19191 +       au_hn_init(hinode);
19192 +       hinode->hi_whdentry = NULL;
19193 +}
19194 +
19195 +int au_iinfo_init(struct inode *inode)
19196 +{
19197 +       struct au_iinfo *iinfo;
19198 +       struct super_block *sb;
19199 +       struct au_hinode *hi;
19200 +       int nbr, i;
19201 +
19202 +       sb = inode->i_sb;
19203 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19204 +       nbr = au_sbbot(sb) + 1;
19205 +       if (unlikely(nbr <= 0))
19206 +               nbr = 1;
19207 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19208 +       if (hi) {
19209 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19210 +
19211 +               iinfo->ii_hinode = hi;
19212 +               for (i = 0; i < nbr; i++, hi++)
19213 +                       au_hinode_init(hi);
19214 +
19215 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19216 +               iinfo->ii_btop = -1;
19217 +               iinfo->ii_bbot = -1;
19218 +               iinfo->ii_vdir = NULL;
19219 +               return 0;
19220 +       }
19221 +       return -ENOMEM;
19222 +}
19223 +
19224 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19225 +{
19226 +       int err, i;
19227 +       struct au_hinode *hip;
19228 +
19229 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19230 +
19231 +       err = -ENOMEM;
19232 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19233 +                         may_shrink);
19234 +       if (hip) {
19235 +               iinfo->ii_hinode = hip;
19236 +               i = iinfo->ii_bbot + 1;
19237 +               hip += i;
19238 +               for (; i < nbr; i++, hip++)
19239 +                       au_hinode_init(hip);
19240 +               err = 0;
19241 +       }
19242 +
19243 +       return err;
19244 +}
19245 +
19246 +void au_iinfo_fin(struct inode *inode)
19247 +{
19248 +       struct au_iinfo *iinfo;
19249 +       struct au_hinode *hi;
19250 +       struct super_block *sb;
19251 +       aufs_bindex_t bindex, bbot;
19252 +       const unsigned char unlinked = !inode->i_nlink;
19253 +
19254 +       AuDebugOn(au_is_bad_inode(inode));
19255 +
19256 +       sb = inode->i_sb;
19257 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19258 +       if (si_pid_test(sb))
19259 +               au_xino_delete_inode(inode, unlinked);
19260 +       else {
19261 +               /*
19262 +                * it is safe to hide the dependency between sbinfo and
19263 +                * sb->s_umount.
19264 +                */
19265 +               lockdep_off();
19266 +               si_noflush_read_lock(sb);
19267 +               au_xino_delete_inode(inode, unlinked);
19268 +               si_read_unlock(sb);
19269 +               lockdep_on();
19270 +       }
19271 +
19272 +       iinfo = au_ii(inode);
19273 +       if (iinfo->ii_vdir)
19274 +               au_vdir_free(iinfo->ii_vdir);
19275 +
19276 +       bindex = iinfo->ii_btop;
19277 +       if (bindex >= 0) {
19278 +               hi = au_hinode(iinfo, bindex);
19279 +               bbot = iinfo->ii_bbot;
19280 +               while (bindex++ <= bbot) {
19281 +                       if (hi->hi_inode)
19282 +                               au_hiput(hi);
19283 +                       hi++;
19284 +               }
19285 +       }
19286 +       au_kfree_rcu(iinfo->ii_hinode);
19287 +       AuRwDestroy(&iinfo->ii_rwsem);
19288 +}
19289 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19290 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19291 +++ linux/fs/aufs/inode.c       2023-10-31 09:31:04.199880750 +0100
19292 @@ -0,0 +1,531 @@
19293 +// SPDX-License-Identifier: GPL-2.0
19294 +/*
19295 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19296 + *
19297 + * This program is free software; you can redistribute it and/or modify
19298 + * it under the terms of the GNU General Public License as published by
19299 + * the Free Software Foundation; either version 2 of the License, or
19300 + * (at your option) any later version.
19301 + *
19302 + * This program is distributed in the hope that it will be useful,
19303 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19304 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19305 + * GNU General Public License for more details.
19306 + *
19307 + * You should have received a copy of the GNU General Public License
19308 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19309 + */
19310 +
19311 +/*
19312 + * inode functions
19313 + */
19314 +
19315 +#include <linux/iversion.h>
19316 +#include "aufs.h"
19317 +
19318 +struct inode *au_igrab(struct inode *inode)
19319 +{
19320 +       if (inode) {
19321 +               AuDebugOn(!atomic_read(&inode->i_count));
19322 +               ihold(inode);
19323 +       }
19324 +       return inode;
19325 +}
19326 +
19327 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19328 +{
19329 +       au_cpup_attr_all(inode, /*force*/0);
19330 +       au_update_iigen(inode, /*half*/1);
19331 +       if (do_version)
19332 +               inode_inc_iversion(inode);
19333 +}
19334 +
19335 +static int au_ii_refresh(struct inode *inode, int *update)
19336 +{
19337 +       int err, e, nbr;
19338 +       umode_t type;
19339 +       aufs_bindex_t bindex, new_bindex;
19340 +       struct super_block *sb;
19341 +       struct au_iinfo *iinfo;
19342 +       struct au_hinode *p, *q, tmp;
19343 +
19344 +       AuDebugOn(au_is_bad_inode(inode));
19345 +       IiMustWriteLock(inode);
19346 +
19347 +       *update = 0;
19348 +       sb = inode->i_sb;
19349 +       nbr = au_sbbot(sb) + 1;
19350 +       type = inode->i_mode & S_IFMT;
19351 +       iinfo = au_ii(inode);
19352 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19353 +       if (unlikely(err))
19354 +               goto out;
19355 +
19356 +       AuDebugOn(iinfo->ii_btop < 0);
19357 +       p = au_hinode(iinfo, iinfo->ii_btop);
19358 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19359 +            bindex++, p++) {
19360 +               if (!p->hi_inode)
19361 +                       continue;
19362 +
19363 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19364 +               new_bindex = au_br_index(sb, p->hi_id);
19365 +               if (new_bindex == bindex)
19366 +                       continue;
19367 +
19368 +               if (new_bindex < 0) {
19369 +                       *update = 1;
19370 +                       au_hiput(p);
19371 +                       p->hi_inode = NULL;
19372 +                       continue;
19373 +               }
19374 +
19375 +               if (new_bindex < iinfo->ii_btop)
19376 +                       iinfo->ii_btop = new_bindex;
19377 +               if (iinfo->ii_bbot < new_bindex)
19378 +                       iinfo->ii_bbot = new_bindex;
19379 +               /* swap two lower inode, and loop again */
19380 +               q = au_hinode(iinfo, new_bindex);
19381 +               tmp = *q;
19382 +               *q = *p;
19383 +               *p = tmp;
19384 +               if (tmp.hi_inode) {
19385 +                       bindex--;
19386 +                       p--;
19387 +               }
19388 +       }
19389 +       au_update_ibrange(inode, /*do_put_zero*/0);
19390 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19391 +       e = au_dy_irefresh(inode);
19392 +       if (unlikely(e && !err))
19393 +               err = e;
19394 +
19395 +out:
19396 +       AuTraceErr(err);
19397 +       return err;
19398 +}
19399 +
19400 +void au_refresh_iop(struct inode *inode, int force_getattr)
19401 +{
19402 +       int type;
19403 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19404 +       const struct inode_operations *iop
19405 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19406 +
19407 +       if (inode->i_op == iop)
19408 +               return;
19409 +
19410 +       switch (inode->i_mode & S_IFMT) {
19411 +       case S_IFDIR:
19412 +               type = AuIop_DIR;
19413 +               break;
19414 +       case S_IFLNK:
19415 +               type = AuIop_SYMLINK;
19416 +               break;
19417 +       default:
19418 +               type = AuIop_OTHER;
19419 +               break;
19420 +       }
19421 +
19422 +       inode->i_op = iop + type;
19423 +       /* unnecessary smp_wmb() */
19424 +}
19425 +
19426 +int au_refresh_hinode_self(struct inode *inode)
19427 +{
19428 +       int err, update;
19429 +
19430 +       err = au_ii_refresh(inode, &update);
19431 +       if (!err)
19432 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19433 +
19434 +       AuTraceErr(err);
19435 +       return err;
19436 +}
19437 +
19438 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19439 +{
19440 +       int err, e, update;
19441 +       unsigned int flags;
19442 +       umode_t mode;
19443 +       aufs_bindex_t bindex, bbot;
19444 +       unsigned char isdir;
19445 +       struct au_hinode *p;
19446 +       struct au_iinfo *iinfo;
19447 +
19448 +       err = au_ii_refresh(inode, &update);
19449 +       if (unlikely(err))
19450 +               goto out;
19451 +
19452 +       update = 0;
19453 +       iinfo = au_ii(inode);
19454 +       p = au_hinode(iinfo, iinfo->ii_btop);
19455 +       mode = (inode->i_mode & S_IFMT);
19456 +       isdir = S_ISDIR(mode);
19457 +       flags = au_hi_flags(inode, isdir);
19458 +       bbot = au_dbbot(dentry);
19459 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19460 +               struct inode *h_i, *h_inode;
19461 +               struct dentry *h_d;
19462 +
19463 +               h_d = au_h_dptr(dentry, bindex);
19464 +               if (!h_d || d_is_negative(h_d))
19465 +                       continue;
19466 +
19467 +               h_inode = d_inode(h_d);
19468 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19469 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19470 +                       h_i = au_h_iptr(inode, bindex);
19471 +                       if (h_i) {
19472 +                               if (h_i == h_inode)
19473 +                                       continue;
19474 +                               err = -EIO;
19475 +                               break;
19476 +                       }
19477 +               }
19478 +               if (bindex < iinfo->ii_btop)
19479 +                       iinfo->ii_btop = bindex;
19480 +               if (iinfo->ii_bbot < bindex)
19481 +                       iinfo->ii_bbot = bindex;
19482 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19483 +               update = 1;
19484 +       }
19485 +       au_update_ibrange(inode, /*do_put_zero*/0);
19486 +       e = au_dy_irefresh(inode);
19487 +       if (unlikely(e && !err))
19488 +               err = e;
19489 +       if (!err)
19490 +               au_refresh_hinode_attr(inode, update && isdir);
19491 +
19492 +out:
19493 +       AuTraceErr(err);
19494 +       return err;
19495 +}
19496 +
19497 +static int set_inode(struct inode *inode, struct dentry *dentry)
19498 +{
19499 +       int err;
19500 +       unsigned int flags;
19501 +       umode_t mode;
19502 +       aufs_bindex_t bindex, btop, btail;
19503 +       unsigned char isdir;
19504 +       struct dentry *h_dentry;
19505 +       struct inode *h_inode;
19506 +       struct au_iinfo *iinfo;
19507 +       const struct inode_operations *iop;
19508 +
19509 +       IiMustWriteLock(inode);
19510 +
19511 +       err = 0;
19512 +       isdir = 0;
19513 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19514 +       btop = au_dbtop(dentry);
19515 +       h_dentry = au_h_dptr(dentry, btop);
19516 +       h_inode = d_inode(h_dentry);
19517 +       mode = h_inode->i_mode;
19518 +       switch (mode & S_IFMT) {
19519 +       case S_IFREG:
19520 +               btail = au_dbtail(dentry);
19521 +               inode->i_op = iop + AuIop_OTHER;
19522 +               inode->i_fop = &aufs_file_fop;
19523 +               err = au_dy_iaop(inode, btop, h_inode);
19524 +               if (unlikely(err))
19525 +                       goto out;
19526 +               break;
19527 +       case S_IFDIR:
19528 +               isdir = 1;
19529 +               btail = au_dbtaildir(dentry);
19530 +               inode->i_op = iop + AuIop_DIR;
19531 +               inode->i_fop = &aufs_dir_fop;
19532 +               break;
19533 +       case S_IFLNK:
19534 +               btail = au_dbtail(dentry);
19535 +               inode->i_op = iop + AuIop_SYMLINK;
19536 +               break;
19537 +       case S_IFBLK:
19538 +       case S_IFCHR:
19539 +       case S_IFIFO:
19540 +       case S_IFSOCK:
19541 +               btail = au_dbtail(dentry);
19542 +               inode->i_op = iop + AuIop_OTHER;
19543 +               init_special_inode(inode, mode, h_inode->i_rdev);
19544 +               break;
19545 +       default:
19546 +               AuIOErr("Unknown file type 0%o\n", mode);
19547 +               err = -EIO;
19548 +               goto out;
19549 +       }
19550 +
19551 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19552 +       flags = au_hi_flags(inode, isdir);
19553 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19554 +           && au_ftest_hi(flags, HNOTIFY)
19555 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19556 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19557 +               au_fclr_hi(flags, HNOTIFY);
19558 +       iinfo = au_ii(inode);
19559 +       iinfo->ii_btop = btop;
19560 +       iinfo->ii_bbot = btail;
19561 +       for (bindex = btop; bindex <= btail; bindex++) {
19562 +               h_dentry = au_h_dptr(dentry, bindex);
19563 +               if (h_dentry)
19564 +                       au_set_h_iptr(inode, bindex,
19565 +                                     au_igrab(d_inode(h_dentry)), flags);
19566 +       }
19567 +       au_cpup_attr_all(inode, /*force*/1);
19568 +       /*
19569 +        * to force calling aufs_get_inode_acl() every time,
19570 +        * do not call cache_no_acl() for aufs inode.
19571 +        */
19572 +
19573 +out:
19574 +       return err;
19575 +}
19576 +
19577 +/*
19578 + * successful returns with iinfo write_locked
19579 + * minus: errno
19580 + * zero: success, matched
19581 + * plus: no error, but unmatched
19582 + */
19583 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19584 +{
19585 +       int err;
19586 +       unsigned int gen, igflags;
19587 +       aufs_bindex_t bindex, bbot;
19588 +       struct inode *h_inode, *h_dinode;
19589 +       struct dentry *h_dentry;
19590 +
19591 +       /*
19592 +        * before this function, if aufs got any iinfo lock, it must be only
19593 +        * one, the parent dir.
19594 +        * it can happen by UDBA and the obsoleted inode number.
19595 +        */
19596 +       err = -EIO;
19597 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19598 +               goto out;
19599 +
19600 +       err = 1;
19601 +       ii_write_lock_new_child(inode);
19602 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19603 +       h_dinode = d_inode(h_dentry);
19604 +       bbot = au_ibbot(inode);
19605 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19606 +               h_inode = au_h_iptr(inode, bindex);
19607 +               if (!h_inode || h_inode != h_dinode)
19608 +                       continue;
19609 +
19610 +               err = 0;
19611 +               gen = au_iigen(inode, &igflags);
19612 +               if (gen == au_digen(dentry)
19613 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19614 +                       break;
19615 +
19616 +               /* fully refresh inode using dentry */
19617 +               err = au_refresh_hinode(inode, dentry);
19618 +               if (!err)
19619 +                       au_update_iigen(inode, /*half*/0);
19620 +               break;
19621 +       }
19622 +
19623 +       if (unlikely(err))
19624 +               ii_write_unlock(inode);
19625 +out:
19626 +       return err;
19627 +}
19628 +
19629 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19630 +          unsigned int d_type, ino_t *ino)
19631 +{
19632 +       int err, idx;
19633 +       const int isnondir = d_type != DT_DIR;
19634 +
19635 +       /* prevent hardlinked inode number from race condition */
19636 +       if (isnondir) {
19637 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19638 +               if (unlikely(err))
19639 +                       goto out;
19640 +       }
19641 +
19642 +       err = au_xino_read(sb, bindex, h_ino, ino);
19643 +       if (unlikely(err))
19644 +               goto out_xinondir;
19645 +
19646 +       if (!*ino) {
19647 +               err = -EIO;
19648 +               *ino = au_xino_new_ino(sb);
19649 +               if (unlikely(!*ino))
19650 +                       goto out_xinondir;
19651 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19652 +               if (unlikely(err))
19653 +                       goto out_xinondir;
19654 +       }
19655 +
19656 +out_xinondir:
19657 +       if (isnondir && idx >= 0)
19658 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19659 +out:
19660 +       return err;
19661 +}
19662 +
19663 +/* successful returns with iinfo write_locked */
19664 +/* todo: return with unlocked? */
19665 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19666 +{
19667 +       struct inode *inode, *h_inode;
19668 +       struct dentry *h_dentry;
19669 +       struct super_block *sb;
19670 +       ino_t h_ino, ino;
19671 +       int err, idx, hlinked;
19672 +       aufs_bindex_t btop;
19673 +
19674 +       sb = dentry->d_sb;
19675 +       btop = au_dbtop(dentry);
19676 +       h_dentry = au_h_dptr(dentry, btop);
19677 +       h_inode = d_inode(h_dentry);
19678 +       h_ino = h_inode->i_ino;
19679 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19680 +
19681 +new_ino:
19682 +       /*
19683 +        * stop 'race'-ing between hardlinks under different
19684 +        * parents.
19685 +        */
19686 +       if (hlinked) {
19687 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19688 +               inode = ERR_PTR(err);
19689 +               if (unlikely(err))
19690 +                       goto out;
19691 +       }
19692 +
19693 +       err = au_xino_read(sb, btop, h_ino, &ino);
19694 +       inode = ERR_PTR(err);
19695 +       if (unlikely(err))
19696 +               goto out_xinondir;
19697 +
19698 +       if (!ino) {
19699 +               ino = au_xino_new_ino(sb);
19700 +               if (unlikely(!ino)) {
19701 +                       inode = ERR_PTR(-EIO);
19702 +                       goto out_xinondir;
19703 +               }
19704 +       }
19705 +
19706 +       AuDbg("i%lu\n", (unsigned long)ino);
19707 +       inode = au_iget_locked(sb, ino);
19708 +       err = PTR_ERR(inode);
19709 +       if (IS_ERR(inode))
19710 +               goto out_xinondir;
19711 +
19712 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19713 +       if (inode->i_state & I_NEW) {
19714 +               ii_write_lock_new_child(inode);
19715 +               err = set_inode(inode, dentry);
19716 +               if (!err) {
19717 +                       unlock_new_inode(inode);
19718 +                       goto out_xinondir; /* success */
19719 +               }
19720 +
19721 +               /*
19722 +                * iget_failed() calls iput(), but we need to call
19723 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19724 +                * i_count.
19725 +                */
19726 +               atomic_inc(&inode->i_count);
19727 +               iget_failed(inode);
19728 +               ii_write_unlock(inode);
19729 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19730 +               /* ignore this error */
19731 +               goto out_iput;
19732 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19733 +               /*
19734 +                * horrible race condition between lookup, readdir and copyup
19735 +                * (or something).
19736 +                */
19737 +               if (hlinked && idx >= 0)
19738 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19739 +               err = reval_inode(inode, dentry);
19740 +               if (unlikely(err < 0)) {
19741 +                       hlinked = 0;
19742 +                       goto out_iput;
19743 +               }
19744 +               if (!err)
19745 +                       goto out; /* success */
19746 +               else if (hlinked && idx >= 0) {
19747 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19748 +                       if (unlikely(err)) {
19749 +                               iput(inode);
19750 +                               inode = ERR_PTR(err);
19751 +                               goto out;
19752 +                       }
19753 +               }
19754 +       }
19755 +
19756 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19757 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19758 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19759 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19760 +                       (unsigned long)h_ino, (unsigned long)ino);
19761 +       ino = 0;
19762 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19763 +       if (!err) {
19764 +               iput(inode);
19765 +               if (hlinked && idx >= 0)
19766 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19767 +               goto new_ino;
19768 +       }
19769 +
19770 +out_iput:
19771 +       iput(inode);
19772 +       inode = ERR_PTR(err);
19773 +out_xinondir:
19774 +       if (hlinked && idx >= 0)
19775 +               au_xinondir_leave(sb, btop, h_ino, idx);
19776 +out:
19777 +       return inode;
19778 +}
19779 +
19780 +/* ---------------------------------------------------------------------- */
19781 +
19782 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19783 +              struct inode *inode)
19784 +{
19785 +       int err;
19786 +       struct inode *hi;
19787 +
19788 +       err = au_br_rdonly(au_sbr(sb, bindex));
19789 +
19790 +       /* pseudo-link after flushed may happen out of bounds */
19791 +       if (!err
19792 +           && inode
19793 +           && au_ibtop(inode) <= bindex
19794 +           && bindex <= au_ibbot(inode)) {
19795 +               /*
19796 +                * permission check is unnecessary since vfsub routine
19797 +                * will be called later
19798 +                */
19799 +               hi = au_h_iptr(inode, bindex);
19800 +               if (hi)
19801 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19802 +       }
19803 +
19804 +       return err;
19805 +}
19806 +
19807 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19808 +                  int mask)
19809 +{
19810 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19811 +               return 0;
19812 +       return inode_permission(h_idmap, h_inode, mask);
19813 +}
19814 +
19815 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19816 +                      int mask)
19817 +{
19818 +       if (au_test_nfs(h_inode->i_sb)
19819 +           && (mask & MAY_WRITE)
19820 +           && S_ISDIR(h_inode->i_mode))
19821 +               mask |= MAY_READ; /* force permission check */
19822 +       return au_test_h_perm(h_idmap, h_inode, mask);
19823 +}
19824 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19825 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19826 +++ linux/fs/aufs/inode.h       2024-03-10 23:40:47.086476782 +0100
19827 @@ -0,0 +1,707 @@
19828 +/* SPDX-License-Identifier: GPL-2.0 */
19829 +/*
19830 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19831 + *
19832 + * This program is free software; you can redistribute it and/or modify
19833 + * it under the terms of the GNU General Public License as published by
19834 + * the Free Software Foundation; either version 2 of the License, or
19835 + * (at your option) any later version.
19836 + *
19837 + * This program is distributed in the hope that it will be useful,
19838 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19839 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19840 + * GNU General Public License for more details.
19841 + *
19842 + * You should have received a copy of the GNU General Public License
19843 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19844 + */
19845 +
19846 +/*
19847 + * inode operations
19848 + */
19849 +
19850 +#ifndef __AUFS_INODE_H__
19851 +#define __AUFS_INODE_H__
19852 +
19853 +#ifdef __KERNEL__
19854 +
19855 +#include <linux/fsnotify.h>
19856 +#include "rwsem.h"
19857 +
19858 +struct vfsmount;
19859 +
19860 +struct au_hnotify {
19861 +#ifdef CONFIG_AUFS_HNOTIFY
19862 +#ifdef CONFIG_AUFS_HFSNOTIFY
19863 +       /* never use fsnotify_add_vfsmount_mark() */
19864 +       struct fsnotify_mark            hn_mark;
19865 +#endif
19866 +       struct inode            *hn_aufs_inode; /* no get/put */
19867 +       struct rcu_head         rcu;
19868 +#endif
19869 +} ____cacheline_aligned_in_smp;
19870 +
19871 +struct au_hinode {
19872 +       struct inode            *hi_inode;
19873 +       aufs_bindex_t           hi_id;
19874 +#ifdef CONFIG_AUFS_HNOTIFY
19875 +       struct au_hnotify       *hi_notify;
19876 +#endif
19877 +
19878 +       /* reference to the copied-up whiteout with get/put */
19879 +       struct dentry           *hi_whdentry;
19880 +};
19881 +
19882 +/* ig_flags */
19883 +#define AuIG_HALF_REFRESHED            1
19884 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19885 +#define au_ig_fset(flags, name) \
19886 +       do { (flags) |= AuIG_##name; } while (0)
19887 +#define au_ig_fclr(flags, name) \
19888 +       do { (flags) &= ~AuIG_##name; } while (0)
19889 +
19890 +struct au_iigen {
19891 +       spinlock_t      ig_spin;
19892 +       __u32           ig_generation, ig_flags;
19893 +};
19894 +
19895 +struct au_vdir;
19896 +struct au_iinfo {
19897 +       struct au_iigen         ii_generation;
19898 +       struct super_block      *ii_hsb1;       /* no get/put */
19899 +
19900 +       struct au_rwsem         ii_rwsem;
19901 +       aufs_bindex_t           ii_btop, ii_bbot;
19902 +       __u32                   ii_higen;
19903 +       struct au_hinode        *ii_hinode;
19904 +       struct au_vdir          *ii_vdir;
19905 +};
19906 +
19907 +struct au_icntnr {
19908 +       struct au_iinfo         iinfo;
19909 +       struct inode            vfs_inode;
19910 +       struct hlist_bl_node    plink;
19911 +       struct rcu_head         rcu;
19912 +} ____cacheline_aligned_in_smp;
19913 +
19914 +/* au_pin flags */
19915 +#define AuPin_DI_LOCKED                BIT(0)
19916 +#define AuPin_MNT_WRITE                BIT(1)
19917 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19918 +#define au_fset_pin(flags, name) \
19919 +       do { (flags) |= AuPin_##name; } while (0)
19920 +#define au_fclr_pin(flags, name) \
19921 +       do { (flags) &= ~AuPin_##name; } while (0)
19922 +
19923 +struct au_pin {
19924 +       /* input */
19925 +       struct dentry *dentry;
19926 +       unsigned int udba;
19927 +       unsigned char lsc_di, lsc_hi, flags;
19928 +       aufs_bindex_t bindex;
19929 +
19930 +       /* output */
19931 +       struct dentry *parent;
19932 +       struct au_hinode *hdir;
19933 +       struct vfsmount *h_mnt;
19934 +
19935 +       /* temporary unlock/relock for copyup */
19936 +       struct dentry *h_dentry, *h_parent;
19937 +       struct au_branch *br;
19938 +       struct task_struct *task;
19939 +};
19940 +
19941 +void au_pin_hdir_unlock(struct au_pin *p);
19942 +int au_pin_hdir_lock(struct au_pin *p);
19943 +int au_pin_hdir_relock(struct au_pin *p);
19944 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19945 +void au_pin_hdir_release(struct au_pin *p);
19946 +
19947 +/* ---------------------------------------------------------------------- */
19948 +
19949 +static inline struct au_iinfo *au_ii(struct inode *inode)
19950 +{
19951 +       BUG_ON(is_bad_inode(inode));
19952 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19953 +}
19954 +
19955 +/* ---------------------------------------------------------------------- */
19956 +
19957 +/* inode.c */
19958 +struct inode *au_igrab(struct inode *inode);
19959 +void au_refresh_iop(struct inode *inode, int force_getattr);
19960 +int au_refresh_hinode_self(struct inode *inode);
19961 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19962 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19963 +          unsigned int d_type, ino_t *ino);
19964 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19965 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19966 +              struct inode *inode);
19967 +int au_test_h_perm(struct mnt_idmap *h_idmap, struct inode *h_inode,
19968 +                  int mask);
19969 +int au_test_h_perm_sio(struct mnt_idmap *h_idmap, struct inode *h_inode,
19970 +                      int mask);
19971 +
19972 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19973 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19974 +{
19975 +#ifdef CONFIG_AUFS_SHWH
19976 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19977 +#else
19978 +       return 0;
19979 +#endif
19980 +}
19981 +
19982 +/* i_op.c */
19983 +enum {
19984 +       AuIop_SYMLINK,
19985 +       AuIop_DIR,
19986 +       AuIop_OTHER,
19987 +       AuIop_Last
19988 +};
19989 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19990 +       aufs_iop_nogetattr[AuIop_Last];
19991 +
19992 +/* au_wr_dir flags */
19993 +#define AuWrDir_ADD_ENTRY      BIT(0)
19994 +#define AuWrDir_ISDIR          BIT(1)
19995 +#define AuWrDir_TMPFILE                BIT(2)
19996 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19997 +#define au_fset_wrdir(flags, name) \
19998 +       do { (flags) |= AuWrDir_##name; } while (0)
19999 +#define au_fclr_wrdir(flags, name) \
20000 +       do { (flags) &= ~AuWrDir_##name; } while (0)
20001 +
20002 +struct au_wr_dir_args {
20003 +       aufs_bindex_t force_btgt;
20004 +       unsigned char flags;
20005 +};
20006 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20007 +             struct au_wr_dir_args *args);
20008 +
20009 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
20010 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
20011 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
20012 +                unsigned int udba, unsigned char flags);
20013 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
20014 +          unsigned int udba, unsigned char flags) __must_check;
20015 +int au_do_pin(struct au_pin *pin) __must_check;
20016 +void au_unpin(struct au_pin *pin);
20017 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20018 +
20019 +#define AuIcpup_DID_CPUP       1
20020 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20021 +#define au_fset_icpup(flags, name) \
20022 +       do { (flags) |= AuIcpup_##name; } while (0)
20023 +#define au_fclr_icpup(flags, name) \
20024 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20025 +
20026 +struct au_icpup_args {
20027 +       unsigned char flags;
20028 +       unsigned char pin_flags;
20029 +       aufs_bindex_t btgt;
20030 +       unsigned int udba;
20031 +       struct au_pin pin;
20032 +       struct path h_path;
20033 +       struct inode *h_inode;
20034 +};
20035 +
20036 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20037 +                    struct au_icpup_args *a);
20038 +
20039 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20040 +                     struct path *h_path, int locked);
20041 +
20042 +/* i_op_add.c */
20043 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20044 +              struct dentry *h_parent, int isdir);
20045 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
20046 +              struct dentry *dentry, umode_t mode, dev_t dev);
20047 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
20048 +                struct dentry *dentry, const char *symname);
20049 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
20050 +               struct dentry *dentry, umode_t mode, bool want_excl);
20051 +struct vfsub_aopen_args;
20052 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20053 +                      struct vfsub_aopen_args *args);
20054 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
20055 +                struct file *file, umode_t mode);
20056 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20057 +             struct dentry *dentry);
20058 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
20059 +              struct dentry *dentry, umode_t mode);
20060 +
20061 +/* i_op_del.c */
20062 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20063 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20064 +              struct dentry *h_parent, int isdir);
20065 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20066 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20067 +
20068 +/* i_op_ren.c */
20069 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20070 +int aufs_rename(struct mnt_idmap *idmap,
20071 +               struct inode *_src_dir, struct dentry *_src_dentry,
20072 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20073 +               unsigned int _flags);
20074 +
20075 +/* iinfo.c */
20076 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20077 +void au_hiput(struct au_hinode *hinode);
20078 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20079 +                 struct dentry *h_wh);
20080 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20081 +
20082 +/* hinode flags */
20083 +#define AuHi_XINO      BIT(0)
20084 +#define AuHi_HNOTIFY   BIT(1)
20085 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20086 +#define au_fset_hi(flags, name) \
20087 +       do { (flags) |= AuHi_##name; } while (0)
20088 +#define au_fclr_hi(flags, name) \
20089 +       do { (flags) &= ~AuHi_##name; } while (0)
20090 +
20091 +#ifndef CONFIG_AUFS_HNOTIFY
20092 +#undef AuHi_HNOTIFY
20093 +#define AuHi_HNOTIFY   0
20094 +#endif
20095 +
20096 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20097 +                  struct inode *h_inode, unsigned int flags);
20098 +
20099 +void au_update_iigen(struct inode *inode, int half);
20100 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20101 +
20102 +void au_icntnr_init_once(void *_c);
20103 +void au_hinode_init(struct au_hinode *hinode);
20104 +int au_iinfo_init(struct inode *inode);
20105 +void au_iinfo_fin(struct inode *inode);
20106 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20107 +
20108 +#ifdef CONFIG_PROC_FS
20109 +/* plink.c */
20110 +int au_plink_maint(struct super_block *sb, int flags);
20111 +struct au_sbinfo;
20112 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20113 +int au_plink_maint_enter(struct super_block *sb);
20114 +#ifdef CONFIG_AUFS_DEBUG
20115 +void au_plink_list(struct super_block *sb);
20116 +#else
20117 +AuStubVoid(au_plink_list, struct super_block *sb)
20118 +#endif
20119 +int au_plink_test(struct inode *inode);
20120 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20121 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20122 +                    struct dentry *h_dentry);
20123 +void au_plink_put(struct super_block *sb, int verbose);
20124 +void au_plink_clean(struct super_block *sb, int verbose);
20125 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20126 +#else
20127 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20128 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20129 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20130 +AuStubVoid(au_plink_list, struct super_block *sb);
20131 +AuStubInt0(au_plink_test, struct inode *inode);
20132 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20133 +       struct inode *inode, aufs_bindex_t bindex);
20134 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20135 +          struct dentry *h_dentry);
20136 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20137 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20138 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20139 +#endif /* CONFIG_PROC_FS */
20140 +
20141 +#ifdef CONFIG_AUFS_XATTR
20142 +/* xattr.c */
20143 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20144 +                 unsigned int verbose);
20145 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20146 +void au_xattr_init(struct super_block *sb);
20147 +#else
20148 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20149 +          int ignore_flags, unsigned int verbose);
20150 +AuStubVoid(au_xattr_init, struct super_block *sb);
20151 +#endif
20152 +
20153 +#ifdef CONFIG_FS_POSIX_ACL
20154 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu);
20155 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
20156 +                              struct dentry *dentry, int type);
20157 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
20158 +                struct posix_acl *acl, int type);
20159 +#endif
20160 +
20161 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20162 +enum {
20163 +       AU_XATTR_SET,
20164 +       AU_ACL_SET
20165 +};
20166 +
20167 +struct au_sxattr {
20168 +       int type;
20169 +       union {
20170 +               struct {
20171 +                       const char      *name;
20172 +                       const void      *value;
20173 +                       size_t          size;
20174 +                       int             flags;
20175 +               } set;
20176 +               struct {
20177 +                       struct posix_acl *acl;
20178 +                       int             type;
20179 +               } acl_set;
20180 +       } u;
20181 +};
20182 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20183 +                 struct au_sxattr *arg);
20184 +#endif
20185 +
20186 +/* ---------------------------------------------------------------------- */
20187 +
20188 +/* lock subclass for iinfo */
20189 +enum {
20190 +       AuLsc_II_CHILD,         /* child first */
20191 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20192 +       AuLsc_II_CHILD3,        /* copyup dirs */
20193 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20194 +       AuLsc_II_PARENT2,
20195 +       AuLsc_II_PARENT3,       /* copyup dirs */
20196 +       AuLsc_II_NEW_CHILD
20197 +};
20198 +
20199 +/*
20200 + * ii_read_lock_child, ii_write_lock_child,
20201 + * ii_read_lock_child2, ii_write_lock_child2,
20202 + * ii_read_lock_child3, ii_write_lock_child3,
20203 + * ii_read_lock_parent, ii_write_lock_parent,
20204 + * ii_read_lock_parent2, ii_write_lock_parent2,
20205 + * ii_read_lock_parent3, ii_write_lock_parent3,
20206 + * ii_read_lock_new_child, ii_write_lock_new_child,
20207 + */
20208 +#define AuReadLockFunc(name, lsc) \
20209 +static inline void ii_read_lock_##name(struct inode *i) \
20210 +{ \
20211 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20212 +}
20213 +
20214 +#define AuWriteLockFunc(name, lsc) \
20215 +static inline void ii_write_lock_##name(struct inode *i) \
20216 +{ \
20217 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20218 +}
20219 +
20220 +#define AuRWLockFuncs(name, lsc) \
20221 +       AuReadLockFunc(name, lsc) \
20222 +       AuWriteLockFunc(name, lsc)
20223 +
20224 +AuRWLockFuncs(child, CHILD);
20225 +AuRWLockFuncs(child2, CHILD2);
20226 +AuRWLockFuncs(child3, CHILD3);
20227 +AuRWLockFuncs(parent, PARENT);
20228 +AuRWLockFuncs(parent2, PARENT2);
20229 +AuRWLockFuncs(parent3, PARENT3);
20230 +AuRWLockFuncs(new_child, NEW_CHILD);
20231 +
20232 +#undef AuReadLockFunc
20233 +#undef AuWriteLockFunc
20234 +#undef AuRWLockFuncs
20235 +
20236 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20237 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20238 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20239 +
20240 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20241 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20242 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20243 +
20244 +/* ---------------------------------------------------------------------- */
20245 +
20246 +static inline void au_icntnr_init(struct au_icntnr *c)
20247 +{
20248 +#ifdef CONFIG_AUFS_DEBUG
20249 +       c->vfs_inode.i_mode = 0;
20250 +#endif
20251 +}
20252 +
20253 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20254 +{
20255 +       unsigned int gen;
20256 +       struct au_iinfo *iinfo;
20257 +       struct au_iigen *iigen;
20258 +
20259 +       iinfo = au_ii(inode);
20260 +       iigen = &iinfo->ii_generation;
20261 +       spin_lock(&iigen->ig_spin);
20262 +       if (igflags)
20263 +               *igflags = iigen->ig_flags;
20264 +       gen = iigen->ig_generation;
20265 +       spin_unlock(&iigen->ig_spin);
20266 +
20267 +       return gen;
20268 +}
20269 +
20270 +/* tiny test for inode number */
20271 +/* tmpfs generation is too rough */
20272 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20273 +{
20274 +       struct au_iinfo *iinfo;
20275 +
20276 +       iinfo = au_ii(inode);
20277 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20278 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20279 +                && iinfo->ii_higen == h_inode->i_generation);
20280 +}
20281 +
20282 +static inline void au_iigen_dec(struct inode *inode)
20283 +{
20284 +       struct au_iinfo *iinfo;
20285 +       struct au_iigen *iigen;
20286 +
20287 +       iinfo = au_ii(inode);
20288 +       iigen = &iinfo->ii_generation;
20289 +       spin_lock(&iigen->ig_spin);
20290 +       iigen->ig_generation--;
20291 +       spin_unlock(&iigen->ig_spin);
20292 +}
20293 +
20294 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20295 +{
20296 +       int err;
20297 +
20298 +       err = 0;
20299 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20300 +               err = -EIO;
20301 +
20302 +       return err;
20303 +}
20304 +
20305 +/* ---------------------------------------------------------------------- */
20306 +
20307 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20308 +                                         aufs_bindex_t bindex)
20309 +{
20310 +       return iinfo->ii_hinode + bindex;
20311 +}
20312 +
20313 +static inline int au_is_bad_inode(struct inode *inode)
20314 +{
20315 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20316 +}
20317 +
20318 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20319 +                                       aufs_bindex_t bindex)
20320 +{
20321 +       IiMustAnyLock(inode);
20322 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20323 +}
20324 +
20325 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20326 +{
20327 +       IiMustAnyLock(inode);
20328 +       return au_ii(inode)->ii_btop;
20329 +}
20330 +
20331 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20332 +{
20333 +       IiMustAnyLock(inode);
20334 +       return au_ii(inode)->ii_bbot;
20335 +}
20336 +
20337 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20338 +{
20339 +       IiMustAnyLock(inode);
20340 +       return au_ii(inode)->ii_vdir;
20341 +}
20342 +
20343 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20344 +{
20345 +       IiMustAnyLock(inode);
20346 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20347 +}
20348 +
20349 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20350 +{
20351 +       IiMustWriteLock(inode);
20352 +       au_ii(inode)->ii_btop = bindex;
20353 +}
20354 +
20355 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20356 +{
20357 +       IiMustWriteLock(inode);
20358 +       au_ii(inode)->ii_bbot = bindex;
20359 +}
20360 +
20361 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20362 +{
20363 +       IiMustWriteLock(inode);
20364 +       au_ii(inode)->ii_vdir = vdir;
20365 +}
20366 +
20367 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20368 +{
20369 +       IiMustAnyLock(inode);
20370 +       return au_hinode(au_ii(inode), bindex);
20371 +}
20372 +
20373 +/* ---------------------------------------------------------------------- */
20374 +
20375 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20376 +{
20377 +       if (pin)
20378 +               return pin->parent;
20379 +       return NULL;
20380 +}
20381 +
20382 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20383 +{
20384 +       if (pin && pin->hdir)
20385 +               return pin->hdir->hi_inode;
20386 +       return NULL;
20387 +}
20388 +
20389 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20390 +{
20391 +       if (pin)
20392 +               return pin->hdir;
20393 +       return NULL;
20394 +}
20395 +
20396 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20397 +{
20398 +       if (pin)
20399 +               pin->dentry = dentry;
20400 +}
20401 +
20402 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20403 +                                          unsigned char lflag)
20404 +{
20405 +       if (pin) {
20406 +               if (lflag)
20407 +                       au_fset_pin(pin->flags, DI_LOCKED);
20408 +               else
20409 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20410 +       }
20411 +}
20412 +
20413 +#if 0 /* reserved */
20414 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20415 +{
20416 +       if (pin) {
20417 +               dput(pin->parent);
20418 +               pin->parent = dget(parent);
20419 +       }
20420 +}
20421 +#endif
20422 +
20423 +/* ---------------------------------------------------------------------- */
20424 +
20425 +struct au_branch;
20426 +#ifdef CONFIG_AUFS_HNOTIFY
20427 +struct au_hnotify_op {
20428 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20429 +       int (*alloc)(struct au_hinode *hinode);
20430 +
20431 +       /*
20432 +        * if it returns true, the caller should free hinode->hi_notify,
20433 +        * otherwise ->free() frees it.
20434 +        */
20435 +       int (*free)(struct au_hinode *hinode,
20436 +                   struct au_hnotify *hn) __must_check;
20437 +
20438 +       void (*fin)(void);
20439 +       int (*init)(void);
20440 +
20441 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20442 +       void (*fin_br)(struct au_branch *br);
20443 +       int (*init_br)(struct au_branch *br, int perm);
20444 +};
20445 +
20446 +/* hnotify.c */
20447 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20448 +void au_hn_free(struct au_hinode *hinode);
20449 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20450 +void au_hn_reset(struct inode *inode, unsigned int flags);
20451 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20452 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20453 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20454 +int au_hnotify_init_br(struct au_branch *br, int perm);
20455 +void au_hnotify_fin_br(struct au_branch *br);
20456 +int __init au_hnotify_init(void);
20457 +void au_hnotify_fin(void);
20458 +
20459 +/* hfsnotify.c */
20460 +extern const struct au_hnotify_op au_hnotify_op;
20461 +
20462 +static inline
20463 +void au_hn_init(struct au_hinode *hinode)
20464 +{
20465 +       hinode->hi_notify = NULL;
20466 +}
20467 +
20468 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20469 +{
20470 +       return hinode->hi_notify;
20471 +}
20472 +
20473 +#else
20474 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20475 +       struct au_hinode *hinode __maybe_unused,
20476 +       struct inode *inode __maybe_unused)
20477 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20478 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20479 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20480 +          int do_set __maybe_unused)
20481 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20482 +          unsigned int flags __maybe_unused)
20483 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20484 +          struct au_branch *br __maybe_unused,
20485 +          int perm __maybe_unused)
20486 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20487 +          int perm __maybe_unused)
20488 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20489 +AuStubInt0(__init au_hnotify_init, void)
20490 +AuStubVoid(au_hnotify_fin, void)
20491 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20492 +#endif /* CONFIG_AUFS_HNOTIFY */
20493 +
20494 +static inline void au_hn_suspend(struct au_hinode *hdir)
20495 +{
20496 +       au_hn_ctl(hdir, /*do_set*/0);
20497 +}
20498 +
20499 +static inline void au_hn_resume(struct au_hinode *hdir)
20500 +{
20501 +       au_hn_ctl(hdir, /*do_set*/1);
20502 +}
20503 +
20504 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20505 +{
20506 +       inode_lock(hdir->hi_inode);
20507 +       au_hn_suspend(hdir);
20508 +}
20509 +
20510 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20511 +                                         unsigned int sc __maybe_unused)
20512 +{
20513 +       inode_lock_nested(hdir->hi_inode, sc);
20514 +       au_hn_suspend(hdir);
20515 +}
20516 +
20517 +#if 0 /* unused */
20518 +#include "vfsub.h"
20519 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20520 +                                                 unsigned int sc)
20521 +{
20522 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20523 +       au_hn_suspend(hdir);
20524 +}
20525 +#endif
20526 +
20527 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20528 +{
20529 +       au_hn_resume(hdir);
20530 +       inode_unlock(hdir->hi_inode);
20531 +}
20532 +
20533 +#endif /* __KERNEL__ */
20534 +#endif /* __AUFS_INODE_H__ */
20535 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20536 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20537 +++ linux/fs/aufs/ioctl.c       2022-11-05 23:02:18.965889284 +0100
20538 @@ -0,0 +1,220 @@
20539 +// SPDX-License-Identifier: GPL-2.0
20540 +/*
20541 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20542 + *
20543 + * This program is free software; you can redistribute it and/or modify
20544 + * it under the terms of the GNU General Public License as published by
20545 + * the Free Software Foundation; either version 2 of the License, or
20546 + * (at your option) any later version.
20547 + *
20548 + * This program is distributed in the hope that it will be useful,
20549 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20550 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20551 + * GNU General Public License for more details.
20552 + *
20553 + * You should have received a copy of the GNU General Public License
20554 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20555 + */
20556 +
20557 +/*
20558 + * ioctl
20559 + * plink-management and readdir in userspace.
20560 + * assist the pathconf(3) wrapper library.
20561 + * move-down
20562 + * File-based Hierarchical Storage Management.
20563 + */
20564 +
20565 +#include <linux/compat.h>
20566 +#include <linux/file.h>
20567 +#include "aufs.h"
20568 +
20569 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20570 +{
20571 +       int err, fd;
20572 +       aufs_bindex_t wbi, bindex, bbot;
20573 +       struct file *h_file;
20574 +       struct super_block *sb;
20575 +       struct dentry *root;
20576 +       struct au_branch *br;
20577 +       struct aufs_wbr_fd wbrfd = {
20578 +               .oflags = au_dir_roflags,
20579 +               .brid   = -1
20580 +       };
20581 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20582 +               | O_NOATIME | O_CLOEXEC;
20583 +
20584 +       AuDebugOn(wbrfd.oflags & ~valid);
20585 +
20586 +       if (arg) {
20587 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20588 +               if (unlikely(err)) {
20589 +                       err = -EFAULT;
20590 +                       goto out;
20591 +               }
20592 +
20593 +               err = -EINVAL;
20594 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20595 +               wbrfd.oflags |= au_dir_roflags;
20596 +               AuDbg("0%o\n", wbrfd.oflags);
20597 +               if (unlikely(wbrfd.oflags & ~valid))
20598 +                       goto out;
20599 +       }
20600 +
20601 +       fd = get_unused_fd_flags(0);
20602 +       err = fd;
20603 +       if (unlikely(fd < 0))
20604 +               goto out;
20605 +
20606 +       h_file = ERR_PTR(-EINVAL);
20607 +       wbi = 0;
20608 +       br = NULL;
20609 +       sb = path->dentry->d_sb;
20610 +       root = sb->s_root;
20611 +       aufs_read_lock(root, AuLock_IR);
20612 +       bbot = au_sbbot(sb);
20613 +       if (wbrfd.brid >= 0) {
20614 +               wbi = au_br_index(sb, wbrfd.brid);
20615 +               if (unlikely(wbi < 0 || wbi > bbot))
20616 +                       goto out_unlock;
20617 +       }
20618 +
20619 +       h_file = ERR_PTR(-ENOENT);
20620 +       br = au_sbr(sb, wbi);
20621 +       if (!au_br_writable(br->br_perm)) {
20622 +               if (arg)
20623 +                       goto out_unlock;
20624 +
20625 +               bindex = wbi + 1;
20626 +               wbi = -1;
20627 +               for (; bindex <= bbot; bindex++) {
20628 +                       br = au_sbr(sb, bindex);
20629 +                       if (au_br_writable(br->br_perm)) {
20630 +                               wbi = bindex;
20631 +                               br = au_sbr(sb, wbi);
20632 +                               break;
20633 +                       }
20634 +               }
20635 +       }
20636 +       AuDbg("wbi %d\n", wbi);
20637 +       if (wbi >= 0)
20638 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20639 +                                  /*force_wr*/0);
20640 +
20641 +out_unlock:
20642 +       aufs_read_unlock(root, AuLock_IR);
20643 +       err = PTR_ERR(h_file);
20644 +       if (IS_ERR(h_file))
20645 +               goto out_fd;
20646 +
20647 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20648 +       fd_install(fd, h_file);
20649 +       err = fd;
20650 +       goto out; /* success */
20651 +
20652 +out_fd:
20653 +       put_unused_fd(fd);
20654 +out:
20655 +       AuTraceErr(err);
20656 +       return err;
20657 +}
20658 +
20659 +/* ---------------------------------------------------------------------- */
20660 +
20661 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20662 +{
20663 +       long err;
20664 +       struct dentry *dentry;
20665 +
20666 +       switch (cmd) {
20667 +       case AUFS_CTL_RDU:
20668 +       case AUFS_CTL_RDU_INO:
20669 +               err = au_rdu_ioctl(file, cmd, arg);
20670 +               break;
20671 +
20672 +       case AUFS_CTL_WBR_FD:
20673 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20674 +               break;
20675 +
20676 +       case AUFS_CTL_IBUSY:
20677 +               err = au_ibusy_ioctl(file, arg);
20678 +               break;
20679 +
20680 +       case AUFS_CTL_BRINFO:
20681 +               err = au_brinfo_ioctl(file, arg);
20682 +               break;
20683 +
20684 +       case AUFS_CTL_FHSM_FD:
20685 +               dentry = file->f_path.dentry;
20686 +               if (IS_ROOT(dentry))
20687 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20688 +               else
20689 +                       err = -ENOTTY;
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 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20703 +{
20704 +       long err;
20705 +
20706 +       switch (cmd) {
20707 +       case AUFS_CTL_MVDOWN:
20708 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20709 +               break;
20710 +
20711 +       case AUFS_CTL_WBR_FD:
20712 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20713 +               break;
20714 +
20715 +       default:
20716 +               /* do not call the lower */
20717 +               AuDbg("0x%x\n", cmd);
20718 +               err = -ENOTTY;
20719 +       }
20720 +
20721 +       AuTraceErr(err);
20722 +       return err;
20723 +}
20724 +
20725 +#ifdef CONFIG_COMPAT
20726 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20727 +                          unsigned long arg)
20728 +{
20729 +       long err;
20730 +
20731 +       switch (cmd) {
20732 +       case AUFS_CTL_RDU:
20733 +       case AUFS_CTL_RDU_INO:
20734 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20735 +               break;
20736 +
20737 +       case AUFS_CTL_IBUSY:
20738 +               err = au_ibusy_compat_ioctl(file, arg);
20739 +               break;
20740 +
20741 +       case AUFS_CTL_BRINFO:
20742 +               err = au_brinfo_compat_ioctl(file, arg);
20743 +               break;
20744 +
20745 +       default:
20746 +               err = aufs_ioctl_dir(file, cmd, arg);
20747 +       }
20748 +
20749 +       AuTraceErr(err);
20750 +       return err;
20751 +}
20752 +
20753 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20754 +                             unsigned long arg)
20755 +{
20756 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20757 +}
20758 +#endif
20759 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20760 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20761 +++ linux/fs/aufs/i_op_add.c    2023-10-31 09:31:04.199880750 +0100
20762 @@ -0,0 +1,972 @@
20763 +// SPDX-License-Identifier: GPL-2.0
20764 +/*
20765 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20766 + *
20767 + * This program is free software; you can redistribute it and/or modify
20768 + * it under the terms of the GNU General Public License as published by
20769 + * the Free Software Foundation; either version 2 of the License, or
20770 + * (at your option) any later version.
20771 + *
20772 + * This program is distributed in the hope that it will be useful,
20773 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20774 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20775 + * GNU General Public License for more details.
20776 + *
20777 + * You should have received a copy of the GNU General Public License
20778 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20779 + */
20780 +
20781 +/*
20782 + * inode operations (add entry)
20783 + */
20784 +
20785 +#include <linux/iversion.h>
20786 +#include "aufs.h"
20787 +
20788 +/*
20789 + * final procedure of adding a new entry, except link(2).
20790 + * remove whiteout, instantiate, copyup the parent dir's times and size
20791 + * and update version.
20792 + * if it failed, re-create the removed whiteout.
20793 + */
20794 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20795 +                 struct dentry *wh_dentry, struct dentry *dentry)
20796 +{
20797 +       int err, rerr;
20798 +       aufs_bindex_t bwh;
20799 +       struct path h_path;
20800 +       struct super_block *sb;
20801 +       struct inode *inode, *h_dir;
20802 +       struct dentry *wh;
20803 +
20804 +       bwh = -1;
20805 +       sb = dir->i_sb;
20806 +       if (wh_dentry) {
20807 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20808 +               IMustLock(h_dir);
20809 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20810 +               bwh = au_dbwh(dentry);
20811 +               h_path.dentry = wh_dentry;
20812 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20813 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20814 +                                         dentry);
20815 +               if (unlikely(err))
20816 +                       goto out;
20817 +       }
20818 +
20819 +       inode = au_new_inode(dentry, /*must_new*/1);
20820 +       if (!IS_ERR(inode)) {
20821 +               d_instantiate(dentry, inode);
20822 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20823 +               IMustLock(dir);
20824 +               au_dir_ts(dir, bindex);
20825 +               inode_inc_iversion(dir);
20826 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20827 +               return 0; /* success */
20828 +       }
20829 +
20830 +       err = PTR_ERR(inode);
20831 +       if (!wh_dentry)
20832 +               goto out;
20833 +
20834 +       /* revert */
20835 +       /* dir inode is locked */
20836 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20837 +       rerr = PTR_ERR(wh);
20838 +       if (IS_ERR(wh)) {
20839 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20840 +                       dentry, err, rerr);
20841 +               err = -EIO;
20842 +       } else
20843 +               dput(wh);
20844 +
20845 +out:
20846 +       return err;
20847 +}
20848 +
20849 +static int au_d_may_add(struct dentry *dentry)
20850 +{
20851 +       int err;
20852 +
20853 +       err = 0;
20854 +       if (unlikely(d_unhashed(dentry)))
20855 +               err = -ENOENT;
20856 +       if (unlikely(d_really_is_positive(dentry)))
20857 +               err = -EEXIST;
20858 +       return err;
20859 +}
20860 +
20861 +/*
20862 + * simple tests for the adding inode operations.
20863 + * following the checks in vfs, plus the parent-child relationship.
20864 + */
20865 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20866 +              struct dentry *h_parent, int isdir)
20867 +{
20868 +       int err;
20869 +       umode_t h_mode;
20870 +       struct dentry *h_dentry;
20871 +       struct inode *h_inode;
20872 +
20873 +       err = -ENAMETOOLONG;
20874 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20875 +               goto out;
20876 +
20877 +       h_dentry = au_h_dptr(dentry, bindex);
20878 +       if (d_really_is_negative(dentry)) {
20879 +               err = -EEXIST;
20880 +               if (unlikely(d_is_positive(h_dentry)))
20881 +                       goto out;
20882 +       } else {
20883 +               /* rename(2) case */
20884 +               err = -EIO;
20885 +               if (unlikely(d_is_negative(h_dentry)))
20886 +                       goto out;
20887 +               h_inode = d_inode(h_dentry);
20888 +               if (unlikely(!h_inode->i_nlink))
20889 +                       goto out;
20890 +
20891 +               h_mode = h_inode->i_mode;
20892 +               if (!isdir) {
20893 +                       err = -EISDIR;
20894 +                       if (unlikely(S_ISDIR(h_mode)))
20895 +                               goto out;
20896 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20897 +                       err = -ENOTDIR;
20898 +                       goto out;
20899 +               }
20900 +       }
20901 +
20902 +       err = 0;
20903 +       /* expected parent dir is locked */
20904 +       if (unlikely(h_parent != h_dentry->d_parent))
20905 +               err = -EIO;
20906 +
20907 +out:
20908 +       AuTraceErr(err);
20909 +       return err;
20910 +}
20911 +
20912 +/*
20913 + * initial procedure of adding a new entry.
20914 + * prepare writable branch and the parent dir, lock it,
20915 + * and lookup whiteout for the new entry.
20916 + */
20917 +static struct dentry*
20918 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20919 +                 struct dentry *src_dentry, struct au_pin *pin,
20920 +                 struct au_wr_dir_args *wr_dir_args)
20921 +{
20922 +       struct dentry *wh_dentry, *h_parent;
20923 +       struct super_block *sb;
20924 +       struct au_branch *br;
20925 +       int err;
20926 +       unsigned int udba;
20927 +       aufs_bindex_t bcpup;
20928 +
20929 +       AuDbg("%pd\n", dentry);
20930 +
20931 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20932 +       bcpup = err;
20933 +       wh_dentry = ERR_PTR(err);
20934 +       if (unlikely(err < 0))
20935 +               goto out;
20936 +
20937 +       sb = dentry->d_sb;
20938 +       udba = au_opt_udba(sb);
20939 +       err = au_pin(pin, dentry, bcpup, udba,
20940 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20941 +       wh_dentry = ERR_PTR(err);
20942 +       if (unlikely(err))
20943 +               goto out;
20944 +
20945 +       h_parent = au_pinned_h_parent(pin);
20946 +       if (udba != AuOpt_UDBA_NONE
20947 +           && au_dbtop(dentry) == bcpup)
20948 +               err = au_may_add(dentry, bcpup, h_parent,
20949 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20950 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20951 +               err = -ENAMETOOLONG;
20952 +       wh_dentry = ERR_PTR(err);
20953 +       if (unlikely(err))
20954 +               goto out_unpin;
20955 +
20956 +       br = au_sbr(sb, bcpup);
20957 +       if (dt) {
20958 +               struct path tmp = {
20959 +                       .dentry = h_parent,
20960 +                       .mnt    = au_br_mnt(br)
20961 +               };
20962 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20963 +       }
20964 +
20965 +       wh_dentry = NULL;
20966 +       if (bcpup != au_dbwh(dentry))
20967 +               goto out; /* success */
20968 +
20969 +       /*
20970 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20971 +        * would not be able to removed in the future. So we don't allow such
20972 +        * name here and we don't handle ENAMETOOLONG differently here.
20973 +        */
20974 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20975 +
20976 +out_unpin:
20977 +       if (IS_ERR(wh_dentry))
20978 +               au_unpin(pin);
20979 +out:
20980 +       return wh_dentry;
20981 +}
20982 +
20983 +/* ---------------------------------------------------------------------- */
20984 +
20985 +enum { Mknod, Symlink, Creat };
20986 +struct simple_arg {
20987 +       int type;
20988 +       union {
20989 +               struct {
20990 +                       umode_t                 mode;
20991 +                       bool                    want_excl;
20992 +                       bool                    try_aopen;
20993 +                       struct vfsub_aopen_args *aopen;
20994 +               } c;
20995 +               struct {
20996 +                       const char *symname;
20997 +               } s;
20998 +               struct {
20999 +                       umode_t mode;
21000 +                       dev_t dev;
21001 +               } m;
21002 +       } u;
21003 +};
21004 +
21005 +static int add_simple(struct inode *dir, struct dentry *dentry,
21006 +                     struct simple_arg *arg)
21007 +{
21008 +       int err, rerr;
21009 +       aufs_bindex_t btop;
21010 +       unsigned char created;
21011 +       const unsigned char try_aopen
21012 +               = (arg->type == Creat && arg->u.c.try_aopen);
21013 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
21014 +       struct dentry *wh_dentry, *parent;
21015 +       struct inode *h_dir;
21016 +       struct super_block *sb;
21017 +       struct au_branch *br;
21018 +       /* to reduce stack size */
21019 +       struct {
21020 +               struct au_dtime dt;
21021 +               struct au_pin pin;
21022 +               struct path h_path;
21023 +               struct au_wr_dir_args wr_dir_args;
21024 +       } *a;
21025 +
21026 +       AuDbg("%pd\n", dentry);
21027 +       IMustLock(dir);
21028 +
21029 +       err = -ENOMEM;
21030 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21031 +       if (unlikely(!a))
21032 +               goto out;
21033 +       a->wr_dir_args.force_btgt = -1;
21034 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21035 +
21036 +       parent = dentry->d_parent; /* dir inode is locked */
21037 +       if (!try_aopen) {
21038 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21039 +               if (unlikely(err))
21040 +                       goto out_free;
21041 +       }
21042 +       err = au_d_may_add(dentry);
21043 +       if (unlikely(err))
21044 +               goto out_unlock;
21045 +       if (!try_aopen)
21046 +               di_write_lock_parent(parent);
21047 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21048 +                                     &a->pin, &a->wr_dir_args);
21049 +       err = PTR_ERR(wh_dentry);
21050 +       if (IS_ERR(wh_dentry))
21051 +               goto out_parent;
21052 +
21053 +       btop = au_dbtop(dentry);
21054 +       sb = dentry->d_sb;
21055 +       br = au_sbr(sb, btop);
21056 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21057 +       a->h_path.mnt = au_br_mnt(br);
21058 +       h_dir = au_pinned_h_dir(&a->pin);
21059 +       switch (arg->type) {
21060 +       case Creat:
21061 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21062 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21063 +                                          arg->u.c.want_excl);
21064 +                       created = !err;
21065 +                       if (!err && try_aopen)
21066 +                               aopen->file->f_mode |= FMODE_CREATED;
21067 +               } else {
21068 +                       aopen->br = br;
21069 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21070 +                       AuDbg("err %d\n", err);
21071 +                       AuDbgFile(aopen->file);
21072 +                       created = err >= 0
21073 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21074 +               }
21075 +               break;
21076 +       case Symlink:
21077 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21078 +               created = !err;
21079 +               break;
21080 +       case Mknod:
21081 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21082 +                                 arg->u.m.dev);
21083 +               created = !err;
21084 +               break;
21085 +       default:
21086 +               BUG();
21087 +       }
21088 +       if (unlikely(err < 0))
21089 +               goto out_unpin;
21090 +
21091 +       err = epilog(dir, btop, wh_dentry, dentry);
21092 +       if (!err)
21093 +               goto out_unpin; /* success */
21094 +
21095 +       /* revert */
21096 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21097 +               /* no delegation since it is just created */
21098 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21099 +                                   /*force*/0);
21100 +               if (rerr) {
21101 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21102 +                               dentry, err, rerr);
21103 +                       err = -EIO;
21104 +               }
21105 +               au_dtime_revert(&a->dt);
21106 +       }
21107 +       if (try_aopen && h_dir->i_op->atomic_open
21108 +           && (aopen->file->f_mode & FMODE_OPENED))
21109 +               /* aopen->file is still opened */
21110 +               au_lcnt_dec(&aopen->br->br_nfiles);
21111 +
21112 +out_unpin:
21113 +       au_unpin(&a->pin);
21114 +       dput(wh_dentry);
21115 +out_parent:
21116 +       if (!try_aopen)
21117 +               di_write_unlock(parent);
21118 +out_unlock:
21119 +       if (unlikely(err)) {
21120 +               au_update_dbtop(dentry);
21121 +               d_drop(dentry);
21122 +       }
21123 +       if (!try_aopen)
21124 +               aufs_read_unlock(dentry, AuLock_DW);
21125 +out_free:
21126 +       au_kfree_rcu(a);
21127 +out:
21128 +       return err;
21129 +}
21130 +
21131 +int aufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
21132 +              struct dentry *dentry, umode_t mode, dev_t dev)
21133 +{
21134 +       struct simple_arg arg = {
21135 +               .type = Mknod,
21136 +               .u.m = {
21137 +                       .mode   = mode,
21138 +                       .dev    = dev
21139 +               }
21140 +       };
21141 +       return add_simple(dir, dentry, &arg);
21142 +}
21143 +
21144 +int aufs_symlink(struct mnt_idmap *idmap, struct inode *dir,
21145 +                struct dentry *dentry, const char *symname)
21146 +{
21147 +       struct simple_arg arg = {
21148 +               .type = Symlink,
21149 +               .u.s.symname = symname
21150 +       };
21151 +       return add_simple(dir, dentry, &arg);
21152 +}
21153 +
21154 +int aufs_create(struct mnt_idmap *idmap, struct inode *dir,
21155 +               struct dentry *dentry, umode_t mode, bool want_excl)
21156 +{
21157 +       struct simple_arg arg = {
21158 +               .type = Creat,
21159 +               .u.c = {
21160 +                       .mode           = mode,
21161 +                       .want_excl      = want_excl
21162 +               }
21163 +       };
21164 +       return add_simple(dir, dentry, &arg);
21165 +}
21166 +
21167 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21168 +                      struct vfsub_aopen_args *aopen_args)
21169 +{
21170 +       struct simple_arg arg = {
21171 +               .type = Creat,
21172 +               .u.c = {
21173 +                       .mode           = aopen_args->create_mode,
21174 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21175 +                       .try_aopen      = true,
21176 +                       .aopen          = aopen_args
21177 +               }
21178 +       };
21179 +       return add_simple(dir, dentry, &arg);
21180 +}
21181 +
21182 +int aufs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
21183 +                struct file *file, umode_t mode)
21184 +{
21185 +       int err;
21186 +       aufs_bindex_t bindex;
21187 +       struct path h_ppath;
21188 +       struct super_block *sb;
21189 +       struct au_branch *br;
21190 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21191 +       struct inode *h_dir, *inode;
21192 +       struct vfsmount *h_mnt;
21193 +       struct mnt_idmap *h_idmap;
21194 +       struct file *h_file;
21195 +       struct au_wr_dir_args wr_dir_args = {
21196 +               .force_btgt     = -1,
21197 +               .flags          = AuWrDir_TMPFILE
21198 +       };
21199 +
21200 +       /* copy-up may happen */
21201 +       inode_lock(dir);
21202 +
21203 +       h_file = NULL;
21204 +       sb = dir->i_sb;
21205 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21206 +       if (unlikely(err))
21207 +               goto out;
21208 +
21209 +       dentry = file->f_path.dentry;
21210 +       err = au_di_init(dentry);
21211 +       if (unlikely(err))
21212 +               goto out_si;
21213 +
21214 +       err = -EBUSY;
21215 +       parent = d_find_any_alias(dir);
21216 +       AuDebugOn(!parent);
21217 +       di_write_lock_parent(parent);
21218 +       if (unlikely(d_inode(parent) != dir))
21219 +               goto out_parent;
21220 +
21221 +       err = au_digen_test(parent, au_sigen(sb));
21222 +       if (unlikely(err))
21223 +               goto out_parent;
21224 +
21225 +       bindex = au_dbtop(parent);
21226 +       au_set_dbtop(dentry, bindex);
21227 +       au_set_dbbot(dentry, bindex);
21228 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21229 +       bindex = err;
21230 +       if (unlikely(err < 0))
21231 +               goto out_parent;
21232 +
21233 +       err = -EOPNOTSUPP;
21234 +       h_dir = au_h_iptr(dir, bindex);
21235 +       if (unlikely(!h_dir->i_op->tmpfile))
21236 +               goto out_parent;
21237 +
21238 +       br = au_sbr(sb, bindex);
21239 +       h_mnt = au_br_mnt(br);
21240 +       err = vfsub_mnt_want_write(h_mnt);
21241 +       if (unlikely(err))
21242 +               goto out_parent;
21243 +
21244 +       h_idmap = mnt_idmap(h_mnt);
21245 +       h_parent = au_h_dptr(parent, bindex);
21246 +       h_ppath.mnt = h_mnt;
21247 +       h_ppath.dentry = h_parent;
21248 +       h_file = kernel_tmpfile_open(h_idmap, &h_ppath, mode, /*open_flag*/0,
21249 +                                    current_cred());
21250 +       if (IS_ERR(h_file)) {
21251 +               err = PTR_ERR(h_file);
21252 +               h_file = NULL;
21253 +               goto out_mnt;
21254 +       }
21255 +
21256 +       h_dentry = h_file->f_path.dentry;
21257 +       au_set_dbtop(dentry, bindex);
21258 +       au_set_dbbot(dentry, bindex);
21259 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21260 +       inode = au_new_inode(dentry, /*must_new*/1);
21261 +       if (IS_ERR(inode)) {
21262 +               err = PTR_ERR(inode);
21263 +               au_set_h_dptr(dentry, bindex, NULL);
21264 +               au_set_dbtop(dentry, -1);
21265 +               au_set_dbbot(dentry, -1);
21266 +               goto out_h_file;
21267 +       }
21268 +
21269 +       if (!inode->i_nlink)
21270 +               set_nlink(inode, 1);
21271 +       d_tmpfile(file, inode);
21272 +       au_di(dentry)->di_tmpfile = 1;
21273 +       get_file(h_file);
21274 +       au_di(dentry)->di_htmpfile = h_file;
21275 +
21276 +       /* update without i_mutex */
21277 +       if (au_ibtop(dir) == au_dbtop(dentry))
21278 +               au_cpup_attr_timesizes(dir);
21279 +
21280 +out_h_file:
21281 +       fput(h_file);
21282 +out_mnt:
21283 +       vfsub_mnt_drop_write(h_mnt);
21284 +out_parent:
21285 +       di_write_unlock(parent);
21286 +       dput(parent);
21287 +       di_write_unlock(dentry);
21288 +       if (!err)
21289 +               goto out_si;
21290 +       if (h_file)
21291 +               fput(h_file);
21292 +       au_di(dentry)->di_htmpfile = NULL;
21293 +       au_di_fin(dentry);
21294 +       dentry->d_fsdata = NULL;
21295 +out_si:
21296 +       si_read_unlock(sb);
21297 +       if (!err && h_file) {
21298 +               /* finally... */
21299 +               err = finish_open_simple(file, err);
21300 +               if (!err)
21301 +                       au_lcnt_inc(&br->br_nfiles);
21302 +               else {
21303 +                       fput(h_file);
21304 +                       au_di(dentry)->di_htmpfile = NULL;
21305 +                       au_di_fin(dentry);
21306 +                       dentry->d_fsdata = NULL;
21307 +               }
21308 +       }
21309 +out:
21310 +       inode_unlock(dir);
21311 +       AuTraceErr(err);
21312 +       return err;
21313 +}
21314 +
21315 +/* ---------------------------------------------------------------------- */
21316 +
21317 +struct au_link_args {
21318 +       aufs_bindex_t bdst, bsrc;
21319 +       struct au_pin pin;
21320 +       struct path h_path;
21321 +       struct dentry *src_parent, *parent;
21322 +};
21323 +
21324 +static int au_cpup_before_link(struct dentry *src_dentry,
21325 +                              struct au_link_args *a)
21326 +{
21327 +       int err;
21328 +       struct dentry *h_src_dentry;
21329 +       struct au_cp_generic cpg = {
21330 +               .dentry = src_dentry,
21331 +               .bdst   = a->bdst,
21332 +               .bsrc   = a->bsrc,
21333 +               .len    = -1,
21334 +               .pin    = &a->pin,
21335 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21336 +       };
21337 +
21338 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21339 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21340 +       if (unlikely(err))
21341 +               goto out;
21342 +
21343 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21344 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21345 +                    au_opt_udba(src_dentry->d_sb),
21346 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21347 +       if (unlikely(err))
21348 +               goto out;
21349 +
21350 +       err = au_sio_cpup_simple(&cpg);
21351 +       au_unpin(&a->pin);
21352 +
21353 +out:
21354 +       di_read_unlock(a->src_parent, AuLock_IR);
21355 +       return err;
21356 +}
21357 +
21358 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21359 +                          struct au_link_args *a)
21360 +{
21361 +       int err;
21362 +       unsigned char plink;
21363 +       aufs_bindex_t bbot;
21364 +       struct dentry *h_src_dentry;
21365 +       struct inode *h_inode, *inode, *delegated;
21366 +       struct super_block *sb;
21367 +       struct file *h_file;
21368 +
21369 +       plink = 0;
21370 +       h_inode = NULL;
21371 +       sb = src_dentry->d_sb;
21372 +       inode = d_inode(src_dentry);
21373 +       if (au_ibtop(inode) <= a->bdst)
21374 +               h_inode = au_h_iptr(inode, a->bdst);
21375 +       if (!h_inode || !h_inode->i_nlink) {
21376 +               /* copyup src_dentry as the name of dentry. */
21377 +               bbot = au_dbbot(dentry);
21378 +               if (bbot < a->bsrc)
21379 +                       au_set_dbbot(dentry, a->bsrc);
21380 +               au_set_h_dptr(dentry, a->bsrc,
21381 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21382 +               dget(a->h_path.dentry);
21383 +               au_set_h_dptr(dentry, a->bdst, NULL);
21384 +               AuDbg("temporary d_inode...\n");
21385 +               spin_lock(&dentry->d_lock);
21386 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21387 +               spin_unlock(&dentry->d_lock);
21388 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21389 +               if (IS_ERR(h_file))
21390 +                       err = PTR_ERR(h_file);
21391 +               else {
21392 +                       struct au_cp_generic cpg = {
21393 +                               .dentry = dentry,
21394 +                               .bdst   = a->bdst,
21395 +                               .bsrc   = -1,
21396 +                               .len    = -1,
21397 +                               .pin    = &a->pin,
21398 +                               .flags  = AuCpup_KEEPLINO
21399 +                       };
21400 +                       err = au_sio_cpup_simple(&cpg);
21401 +                       au_h_open_post(dentry, a->bsrc, h_file);
21402 +                       if (!err) {
21403 +                               dput(a->h_path.dentry);
21404 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21405 +                       } else
21406 +                               au_set_h_dptr(dentry, a->bdst,
21407 +                                             a->h_path.dentry);
21408 +               }
21409 +               spin_lock(&dentry->d_lock);
21410 +               dentry->d_inode = NULL; /* restore */
21411 +               spin_unlock(&dentry->d_lock);
21412 +               AuDbg("temporary d_inode...done\n");
21413 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21414 +               au_set_dbbot(dentry, bbot);
21415 +       } else {
21416 +               /* the inode of src_dentry already exists on a.bdst branch */
21417 +               h_src_dentry = d_find_alias(h_inode);
21418 +               if (!h_src_dentry && au_plink_test(inode)) {
21419 +                       plink = 1;
21420 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21421 +                       err = PTR_ERR(h_src_dentry);
21422 +                       if (IS_ERR(h_src_dentry))
21423 +                               goto out;
21424 +
21425 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21426 +                               dput(h_src_dentry);
21427 +                               h_src_dentry = NULL;
21428 +                       }
21429 +
21430 +               }
21431 +               if (h_src_dentry) {
21432 +                       delegated = NULL;
21433 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21434 +                                        &a->h_path, &delegated);
21435 +                       if (unlikely(err == -EWOULDBLOCK)) {
21436 +                               pr_warn("cannot retry for NFSv4 delegation"
21437 +                                       " for an internal link\n");
21438 +                               iput(delegated);
21439 +                       }
21440 +                       dput(h_src_dentry);
21441 +               } else {
21442 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21443 +                               h_inode->i_ino, a->bdst);
21444 +                       err = -EIO;
21445 +               }
21446 +       }
21447 +
21448 +       if (!err && !plink)
21449 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21450 +
21451 +out:
21452 +       AuTraceErr(err);
21453 +       return err;
21454 +}
21455 +
21456 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21457 +             struct dentry *dentry)
21458 +{
21459 +       int err, rerr;
21460 +       struct au_dtime dt;
21461 +       struct au_link_args *a;
21462 +       struct dentry *wh_dentry, *h_src_dentry;
21463 +       struct inode *inode, *delegated;
21464 +       struct super_block *sb;
21465 +       struct au_wr_dir_args wr_dir_args = {
21466 +               /* .force_btgt  = -1, */
21467 +               .flags          = AuWrDir_ADD_ENTRY
21468 +       };
21469 +
21470 +       IMustLock(dir);
21471 +       inode = d_inode(src_dentry);
21472 +       IMustLock(inode);
21473 +
21474 +       err = -ENOMEM;
21475 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21476 +       if (unlikely(!a))
21477 +               goto out;
21478 +
21479 +       a->parent = dentry->d_parent; /* dir inode is locked */
21480 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21481 +                                       AuLock_NOPLM | AuLock_GEN);
21482 +       if (unlikely(err))
21483 +               goto out_kfree;
21484 +       err = au_d_linkable(src_dentry);
21485 +       if (unlikely(err))
21486 +               goto out_unlock;
21487 +       err = au_d_may_add(dentry);
21488 +       if (unlikely(err))
21489 +               goto out_unlock;
21490 +
21491 +       a->src_parent = dget_parent(src_dentry);
21492 +       wr_dir_args.force_btgt = au_ibtop(inode);
21493 +
21494 +       di_write_lock_parent(a->parent);
21495 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21496 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21497 +                                     &wr_dir_args);
21498 +       err = PTR_ERR(wh_dentry);
21499 +       if (IS_ERR(wh_dentry))
21500 +               goto out_parent;
21501 +
21502 +       err = 0;
21503 +       sb = dentry->d_sb;
21504 +       a->bdst = au_dbtop(dentry);
21505 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21506 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21507 +       a->bsrc = au_ibtop(inode);
21508 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21509 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21510 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21511 +       if (!h_src_dentry) {
21512 +               a->bsrc = au_dbtop(src_dentry);
21513 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21514 +               AuDebugOn(!h_src_dentry);
21515 +       } else if (IS_ERR(h_src_dentry)) {
21516 +               err = PTR_ERR(h_src_dentry);
21517 +               goto out_parent;
21518 +       }
21519 +
21520 +       /*
21521 +        * aufs doesn't touch the credential so
21522 +        * security_dentry_create_files_as() is unnecessary.
21523 +        */
21524 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21525 +               if (a->bdst < a->bsrc
21526 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21527 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21528 +               else {
21529 +                       delegated = NULL;
21530 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21531 +                                        &a->h_path, &delegated);
21532 +                       if (unlikely(err == -EWOULDBLOCK)) {
21533 +                               pr_warn("cannot retry for NFSv4 delegation"
21534 +                                       " for an internal link\n");
21535 +                               iput(delegated);
21536 +                       }
21537 +               }
21538 +               dput(h_src_dentry);
21539 +       } else {
21540 +               /*
21541 +                * copyup src_dentry to the branch we process,
21542 +                * and then link(2) to it.
21543 +                */
21544 +               dput(h_src_dentry);
21545 +               if (a->bdst < a->bsrc
21546 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21547 +                       au_unpin(&a->pin);
21548 +                       di_write_unlock(a->parent);
21549 +                       err = au_cpup_before_link(src_dentry, a);
21550 +                       di_write_lock_parent(a->parent);
21551 +                       if (!err)
21552 +                               err = au_pin(&a->pin, dentry, a->bdst,
21553 +                                            au_opt_udba(sb),
21554 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21555 +                       if (unlikely(err))
21556 +                               goto out_wh;
21557 +               }
21558 +               if (!err) {
21559 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21560 +                       err = -ENOENT;
21561 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21562 +                               delegated = NULL;
21563 +                               err = vfsub_link(h_src_dentry,
21564 +                                                au_pinned_h_dir(&a->pin),
21565 +                                                &a->h_path, &delegated);
21566 +                               if (unlikely(err == -EWOULDBLOCK)) {
21567 +                                       pr_warn("cannot retry"
21568 +                                               " for NFSv4 delegation"
21569 +                                               " for an internal link\n");
21570 +                                       iput(delegated);
21571 +                               }
21572 +                       }
21573 +               }
21574 +       }
21575 +       if (unlikely(err))
21576 +               goto out_unpin;
21577 +
21578 +       if (wh_dentry) {
21579 +               a->h_path.dentry = wh_dentry;
21580 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21581 +                                         dentry);
21582 +               if (unlikely(err))
21583 +                       goto out_revert;
21584 +       }
21585 +
21586 +       au_dir_ts(dir, a->bdst);
21587 +       inode_inc_iversion(dir);
21588 +       inc_nlink(inode);
21589 +       inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
21590 +       d_instantiate(dentry, au_igrab(inode));
21591 +       if (d_unhashed(a->h_path.dentry))
21592 +               /* some filesystem calls d_drop() */
21593 +               d_drop(dentry);
21594 +       /* some filesystems consume an inode even hardlink */
21595 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21596 +       goto out_unpin; /* success */
21597 +
21598 +out_revert:
21599 +       /* no delegation since it is just created */
21600 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21601 +                           /*delegated*/NULL, /*force*/0);
21602 +       if (unlikely(rerr)) {
21603 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21604 +               err = -EIO;
21605 +       }
21606 +       au_dtime_revert(&dt);
21607 +out_unpin:
21608 +       au_unpin(&a->pin);
21609 +out_wh:
21610 +       dput(wh_dentry);
21611 +out_parent:
21612 +       di_write_unlock(a->parent);
21613 +       dput(a->src_parent);
21614 +out_unlock:
21615 +       if (unlikely(err)) {
21616 +               au_update_dbtop(dentry);
21617 +               d_drop(dentry);
21618 +       }
21619 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21620 +out_kfree:
21621 +       au_kfree_rcu(a);
21622 +out:
21623 +       AuTraceErr(err);
21624 +       return err;
21625 +}
21626 +
21627 +int aufs_mkdir(struct mnt_idmap *idmap, struct inode *dir,
21628 +              struct dentry *dentry, umode_t mode)
21629 +{
21630 +       int err, rerr;
21631 +       aufs_bindex_t bindex;
21632 +       unsigned char diropq;
21633 +       struct path h_path;
21634 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21635 +       struct inode *h_inode;
21636 +       struct super_block *sb;
21637 +       struct {
21638 +               struct au_pin pin;
21639 +               struct au_dtime dt;
21640 +       } *a; /* reduce the stack usage */
21641 +       struct au_wr_dir_args wr_dir_args = {
21642 +               .force_btgt     = -1,
21643 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21644 +       };
21645 +
21646 +       IMustLock(dir);
21647 +
21648 +       err = -ENOMEM;
21649 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21650 +       if (unlikely(!a))
21651 +               goto out;
21652 +
21653 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21654 +       if (unlikely(err))
21655 +               goto out_free;
21656 +       err = au_d_may_add(dentry);
21657 +       if (unlikely(err))
21658 +               goto out_unlock;
21659 +
21660 +       parent = dentry->d_parent; /* dir inode is locked */
21661 +       di_write_lock_parent(parent);
21662 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21663 +                                     &a->pin, &wr_dir_args);
21664 +       err = PTR_ERR(wh_dentry);
21665 +       if (IS_ERR(wh_dentry))
21666 +               goto out_parent;
21667 +
21668 +       sb = dentry->d_sb;
21669 +       bindex = au_dbtop(dentry);
21670 +       h_path.dentry = au_h_dptr(dentry, bindex);
21671 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21672 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21673 +       if (unlikely(err))
21674 +               goto out_unpin;
21675 +
21676 +       /* make the dir opaque */
21677 +       diropq = 0;
21678 +       h_inode = d_inode(h_path.dentry);
21679 +       if (wh_dentry
21680 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21681 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21682 +               opq_dentry = au_diropq_create(dentry, bindex);
21683 +               inode_unlock(h_inode);
21684 +               err = PTR_ERR(opq_dentry);
21685 +               if (IS_ERR(opq_dentry))
21686 +                       goto out_dir;
21687 +               dput(opq_dentry);
21688 +               diropq = 1;
21689 +       }
21690 +
21691 +       err = epilog(dir, bindex, wh_dentry, dentry);
21692 +       if (!err) {
21693 +               inc_nlink(dir);
21694 +               goto out_unpin; /* success */
21695 +       }
21696 +
21697 +       /* revert */
21698 +       if (diropq) {
21699 +               AuLabel(revert opq);
21700 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21701 +               rerr = au_diropq_remove(dentry, bindex);
21702 +               inode_unlock(h_inode);
21703 +               if (rerr) {
21704 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21705 +                               dentry, err, rerr);
21706 +                       err = -EIO;
21707 +               }
21708 +       }
21709 +
21710 +out_dir:
21711 +       AuLabel(revert dir);
21712 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21713 +       if (rerr) {
21714 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21715 +                       dentry, err, rerr);
21716 +               err = -EIO;
21717 +       }
21718 +       au_dtime_revert(&a->dt);
21719 +out_unpin:
21720 +       au_unpin(&a->pin);
21721 +       dput(wh_dentry);
21722 +out_parent:
21723 +       di_write_unlock(parent);
21724 +out_unlock:
21725 +       if (unlikely(err)) {
21726 +               au_update_dbtop(dentry);
21727 +               d_drop(dentry);
21728 +       }
21729 +       aufs_read_unlock(dentry, AuLock_DW);
21730 +out_free:
21731 +       au_kfree_rcu(a);
21732 +out:
21733 +       return err;
21734 +}
21735 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21736 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21737 +++ linux/fs/aufs/i_op.c        2024-03-10 23:40:47.083143450 +0100
21738 @@ -0,0 +1,1520 @@
21739 +// SPDX-License-Identifier: GPL-2.0
21740 +/*
21741 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21742 + *
21743 + * This program is free software; you can redistribute it and/or modify
21744 + * it under the terms of the GNU General Public License as published by
21745 + * the Free Software Foundation; either version 2 of the License, or
21746 + * (at your option) any later version.
21747 + *
21748 + * This program is distributed in the hope that it will be useful,
21749 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21750 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21751 + * GNU General Public License for more details.
21752 + *
21753 + * You should have received a copy of the GNU General Public License
21754 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21755 + */
21756 +
21757 +/*
21758 + * inode operations (except add/del/rename)
21759 + */
21760 +
21761 +#include <linux/device_cgroup.h>
21762 +#include <linux/filelock.h>
21763 +#include <linux/fs_stack.h>
21764 +#include <linux/iversion.h>
21765 +#include <linux/security.h>
21766 +#include "aufs.h"
21767 +
21768 +static int h_permission(struct inode *h_inode, int mask,
21769 +                       struct path *h_path, int brperm)
21770 +{
21771 +       int err;
21772 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21773 +       struct mnt_idmap *h_idmap;
21774 +
21775 +       err = -EPERM;
21776 +       if (write_mask && IS_IMMUTABLE(h_inode))
21777 +               goto out;
21778 +
21779 +       err = -EACCES;
21780 +       if (((mask & MAY_EXEC)
21781 +            && S_ISREG(h_inode->i_mode)
21782 +            && (path_noexec(h_path)
21783 +                || !(h_inode->i_mode & 0111))))
21784 +               goto out;
21785 +
21786 +       /*
21787 +        * - skip the lower fs test in the case of write to ro branch.
21788 +        * - nfs dir permission write check is optimized, but a policy for
21789 +        *   link/rename requires a real check.
21790 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21791 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21792 +        */
21793 +       h_idmap = mnt_idmap(h_path->mnt);
21794 +       if ((write_mask && !au_br_writable(brperm))
21795 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21796 +               && write_mask && !(mask & MAY_READ))
21797 +           || !h_inode->i_op->permission) {
21798 +               /* AuLabel(generic_permission); */
21799 +               /* AuDbg("get_inode_acl %ps\n",
21800 +                  h_inode->i_op->get_inode_acl); */
21801 +               err = generic_permission(h_idmap, h_inode, mask);
21802 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21803 +                       err = h_inode->i_op->permission(h_idmap, h_inode,
21804 +                                                       mask);
21805 +               AuTraceErr(err);
21806 +       } else {
21807 +               /* AuLabel(h_inode->permission); */
21808 +               err = h_inode->i_op->permission(h_idmap, h_inode, mask);
21809 +               AuTraceErr(err);
21810 +       }
21811 +
21812 +       if (!err)
21813 +               err = devcgroup_inode_permission(h_inode, mask);
21814 +       if (!err)
21815 +               err = security_inode_permission(h_inode, mask);
21816 +
21817 +out:
21818 +       return err;
21819 +}
21820 +
21821 +static int aufs_permission(struct mnt_idmap *idmap, struct inode *inode,
21822 +                          int mask)
21823 +{
21824 +       int err;
21825 +       aufs_bindex_t bindex, bbot;
21826 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21827 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21828 +       struct inode *h_inode;
21829 +       struct super_block *sb;
21830 +       struct au_branch *br;
21831 +
21832 +       /* todo: support rcu-walk? */
21833 +       if (mask & MAY_NOT_BLOCK)
21834 +               return -ECHILD;
21835 +
21836 +       sb = inode->i_sb;
21837 +       si_read_lock(sb, AuLock_FLUSH);
21838 +       ii_read_lock_child(inode);
21839 +#if 0 /* reserved for future use */
21840 +       /*
21841 +        * This test may be rather 'too much' since the test is essentially done
21842 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21843 +        * generation doesn't match to the superblock's here.  But it isn't a
21844 +        * big deal I suppose.
21845 +        */
21846 +       err = au_iigen_test(inode, au_sigen(sb));
21847 +       if (unlikely(err))
21848 +               goto out;
21849 +#endif
21850 +
21851 +       if (!isdir
21852 +           || write_mask
21853 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21854 +               err = au_busy_or_stale();
21855 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21856 +               if (unlikely(!h_inode
21857 +                            || (h_inode->i_mode & S_IFMT)
21858 +                            != (inode->i_mode & S_IFMT)))
21859 +                       goto out;
21860 +
21861 +               err = 0;
21862 +               bindex = au_ibtop(inode);
21863 +               br = au_sbr(sb, bindex);
21864 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21865 +               if (write_mask
21866 +                   && !err
21867 +                   && !special_file(h_inode->i_mode)) {
21868 +                       /* test whether the upper writable branch exists */
21869 +                       err = -EROFS;
21870 +                       for (; bindex >= 0; bindex--)
21871 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21872 +                                       err = 0;
21873 +                                       break;
21874 +                               }
21875 +               }
21876 +               goto out;
21877 +       }
21878 +
21879 +       /* non-write to dir */
21880 +       err = 0;
21881 +       bbot = au_ibbot(inode);
21882 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21883 +               h_inode = au_h_iptr(inode, bindex);
21884 +               if (h_inode) {
21885 +                       err = au_busy_or_stale();
21886 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21887 +                               break;
21888 +
21889 +                       br = au_sbr(sb, bindex);
21890 +                       err = h_permission(h_inode, mask, &br->br_path,
21891 +                                          br->br_perm);
21892 +               }
21893 +       }
21894 +
21895 +out:
21896 +       ii_read_unlock(inode);
21897 +       si_read_unlock(sb);
21898 +       return err;
21899 +}
21900 +
21901 +/* ---------------------------------------------------------------------- */
21902 +
21903 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21904 +                                 unsigned int flags)
21905 +{
21906 +       struct dentry *ret, *parent;
21907 +       struct inode *inode;
21908 +       struct super_block *sb;
21909 +       int err, npositive;
21910 +
21911 +       IMustLock(dir);
21912 +
21913 +       /* todo: support rcu-walk? */
21914 +       ret = ERR_PTR(-ECHILD);
21915 +       if (flags & LOOKUP_RCU)
21916 +               goto out;
21917 +
21918 +       ret = ERR_PTR(-ENAMETOOLONG);
21919 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21920 +               goto out;
21921 +
21922 +       sb = dir->i_sb;
21923 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21924 +       ret = ERR_PTR(err);
21925 +       if (unlikely(err))
21926 +               goto out;
21927 +
21928 +       err = au_di_init(dentry);
21929 +       ret = ERR_PTR(err);
21930 +       if (unlikely(err))
21931 +               goto out_si;
21932 +
21933 +       inode = NULL;
21934 +       npositive = 0; /* suppress a warning */
21935 +       parent = dentry->d_parent; /* dir inode is locked */
21936 +       di_read_lock_parent(parent, AuLock_IR);
21937 +       err = au_alive_dir(parent);
21938 +       if (!err)
21939 +               err = au_digen_test(parent, au_sigen(sb));
21940 +       if (!err) {
21941 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21942 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21943 +                                          AuLkup_ALLOW_NEG);
21944 +               err = npositive;
21945 +       }
21946 +       di_read_unlock(parent, AuLock_IR);
21947 +       ret = ERR_PTR(err);
21948 +       if (unlikely(err < 0))
21949 +               goto out_unlock;
21950 +
21951 +       if (npositive) {
21952 +               inode = au_new_inode(dentry, /*must_new*/0);
21953 +               if (IS_ERR(inode)) {
21954 +                       ret = (void *)inode;
21955 +                       inode = NULL;
21956 +                       goto out_unlock;
21957 +               }
21958 +       }
21959 +
21960 +       if (inode)
21961 +               atomic_inc(&inode->i_count);
21962 +       ret = d_splice_alias(inode, dentry);
21963 +#if 0 /* reserved for future use */
21964 +       if (unlikely(d_need_lookup(dentry))) {
21965 +               spin_lock(&dentry->d_lock);
21966 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21967 +               spin_unlock(&dentry->d_lock);
21968 +       } else
21969 +#endif
21970 +       if (inode) {
21971 +               if (!IS_ERR(ret)) {
21972 +                       iput(inode);
21973 +                       if (ret && ret != dentry)
21974 +                               ii_write_unlock(inode);
21975 +               } else {
21976 +                       ii_write_unlock(inode);
21977 +                       iput(inode);
21978 +                       inode = NULL;
21979 +               }
21980 +       }
21981 +
21982 +out_unlock:
21983 +       di_write_unlock(dentry);
21984 +out_si:
21985 +       si_read_unlock(sb);
21986 +out:
21987 +       return ret;
21988 +}
21989 +
21990 +/* ---------------------------------------------------------------------- */
21991 +
21992 +/*
21993 + * very dirty and complicated aufs ->atomic_open().
21994 + * aufs_atomic_open()
21995 + * + au_aopen_or_create()
21996 + *   + add_simple()
21997 + *     + vfsub_atomic_open()
21998 + *       + branch fs ->atomic_open()
21999 + *        may call the actual 'open' for h_file
22000 + *       + inc br_nfiles only if opened
22001 + * + au_aopen_no_open() or au_aopen_do_open()
22002 + *
22003 + * au_aopen_do_open()
22004 + * + finish_open()
22005 + *   + au_do_aopen()
22006 + *     + au_do_open() the body of all 'open'
22007 + *       + au_do_open_nondir()
22008 + *        set the passed h_file
22009 + *
22010 + * au_aopen_no_open()
22011 + * + finish_no_open()
22012 + */
22013 +
22014 +struct aopen_node {
22015 +       struct hlist_bl_node hblist;
22016 +       struct file *file, *h_file;
22017 +};
22018 +
22019 +static int au_do_aopen(struct inode *inode, struct file *file)
22020 +{
22021 +       struct hlist_bl_head *aopen;
22022 +       struct hlist_bl_node *pos;
22023 +       struct aopen_node *node;
22024 +       struct au_do_open_args args = {
22025 +               .aopen  = 1,
22026 +               .open   = au_do_open_nondir
22027 +       };
22028 +
22029 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22030 +       hlist_bl_lock(aopen);
22031 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22032 +               if (node->file == file) {
22033 +                       args.h_file = node->h_file;
22034 +                       break;
22035 +               }
22036 +       hlist_bl_unlock(aopen);
22037 +       /* AuDebugOn(!args.h_file); */
22038 +
22039 +       return au_do_open(file, &args);
22040 +}
22041 +
22042 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22043 +                           struct aopen_node *aopen_node)
22044 +{
22045 +       int err;
22046 +       struct hlist_bl_head *aopen;
22047 +
22048 +       AuLabel(here);
22049 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22050 +       au_hbl_add(&aopen_node->hblist, aopen);
22051 +       err = finish_open(file, dentry, au_do_aopen);
22052 +       au_hbl_del(&aopen_node->hblist, aopen);
22053 +       /* AuDbgFile(file); */
22054 +       AuDbg("%pd%s%s\n", dentry,
22055 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22056 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22057 +
22058 +       AuTraceErr(err);
22059 +       return err;
22060 +}
22061 +
22062 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22063 +{
22064 +       int err;
22065 +
22066 +       AuLabel(here);
22067 +       dget(dentry);
22068 +       err = finish_no_open(file, dentry);
22069 +
22070 +       AuTraceErr(err);
22071 +       return err;
22072 +}
22073 +
22074 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22075 +                           struct file *file, unsigned int open_flag,
22076 +                           umode_t create_mode)
22077 +{
22078 +       int err, did_open;
22079 +       unsigned int lkup_flags;
22080 +       aufs_bindex_t bindex;
22081 +       struct super_block *sb;
22082 +       struct dentry *parent, *d;
22083 +       struct vfsub_aopen_args args = {
22084 +               .open_flag      = open_flag,
22085 +               .create_mode    = create_mode
22086 +       };
22087 +       struct aopen_node aopen_node = {
22088 +               .file   = file
22089 +       };
22090 +
22091 +       IMustLock(dir);
22092 +       AuDbg("open_flag 0%o\n", open_flag);
22093 +       AuDbgDentry(dentry);
22094 +
22095 +       err = 0;
22096 +       if (!au_di(dentry)) {
22097 +               lkup_flags = LOOKUP_OPEN;
22098 +               if (open_flag & O_CREAT)
22099 +                       lkup_flags |= LOOKUP_CREATE;
22100 +               d = aufs_lookup(dir, dentry, lkup_flags);
22101 +               if (IS_ERR(d)) {
22102 +                       err = PTR_ERR(d);
22103 +                       AuTraceErr(err);
22104 +                       goto out;
22105 +               } else if (d) {
22106 +                       /*
22107 +                        * obsoleted dentry found.
22108 +                        * another error will be returned later.
22109 +                        */
22110 +                       d_drop(d);
22111 +                       AuDbgDentry(d);
22112 +                       dput(d);
22113 +               }
22114 +               AuDbgDentry(dentry);
22115 +       }
22116 +
22117 +       if (d_is_positive(dentry)
22118 +           || d_unhashed(dentry)
22119 +           || d_unlinked(dentry)
22120 +           || !(open_flag & O_CREAT)) {
22121 +               err = au_aopen_no_open(file, dentry);
22122 +               goto out; /* success */
22123 +       }
22124 +
22125 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22126 +       if (unlikely(err))
22127 +               goto out;
22128 +
22129 +       sb = dentry->d_sb;
22130 +       parent = dentry->d_parent;      /* dir is locked */
22131 +       di_write_lock_parent(parent);
22132 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22133 +       if (unlikely(err < 0))
22134 +               goto out_parent;
22135 +
22136 +       AuDbgDentry(dentry);
22137 +       if (d_is_positive(dentry)) {
22138 +               err = au_aopen_no_open(file, dentry);
22139 +               goto out_parent; /* success */
22140 +       }
22141 +
22142 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22143 +       err = PTR_ERR(args.file);
22144 +       if (IS_ERR(args.file))
22145 +               goto out_parent;
22146 +
22147 +       bindex = au_dbtop(dentry);
22148 +       err = au_aopen_or_create(dir, dentry, &args);
22149 +       AuTraceErr(err);
22150 +       AuDbgFile(args.file);
22151 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22152 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22153 +       if (!did_open) {
22154 +               fput(args.file);
22155 +               args.file = NULL;
22156 +       }
22157 +       di_write_unlock(parent);
22158 +       di_write_unlock(dentry);
22159 +       if (unlikely(err < 0)) {
22160 +               if (args.file)
22161 +                       fput(args.file);
22162 +               goto out_sb;
22163 +       }
22164 +
22165 +       if (!did_open)
22166 +               err = au_aopen_no_open(file, dentry);
22167 +       else {
22168 +               aopen_node.h_file = args.file;
22169 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22170 +       }
22171 +       if (unlikely(err < 0)) {
22172 +               if (args.file)
22173 +                       fput(args.file);
22174 +               if (did_open)
22175 +                       au_lcnt_dec(&args.br->br_nfiles);
22176 +       }
22177 +       goto out_sb; /* success */
22178 +
22179 +out_parent:
22180 +       di_write_unlock(parent);
22181 +       di_write_unlock(dentry);
22182 +out_sb:
22183 +       si_read_unlock(sb);
22184 +out:
22185 +       AuTraceErr(err);
22186 +       AuDbgFile(file);
22187 +       return err;
22188 +}
22189 +
22190 +
22191 +/* ---------------------------------------------------------------------- */
22192 +
22193 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22194 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22195 +                         aufs_bindex_t btop)
22196 +{
22197 +       int err;
22198 +       struct dentry *h_parent;
22199 +       struct inode *h_dir;
22200 +
22201 +       if (add_entry)
22202 +               IMustLock(d_inode(parent));
22203 +       else
22204 +               di_write_lock_parent(parent);
22205 +
22206 +       err = 0;
22207 +       if (!au_h_dptr(parent, bcpup)) {
22208 +               if (btop > bcpup)
22209 +                       err = au_cpup_dirs(dentry, bcpup);
22210 +               else if (btop < bcpup)
22211 +                       err = au_cpdown_dirs(dentry, bcpup);
22212 +               else
22213 +                       BUG();
22214 +       }
22215 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22216 +               h_parent = au_h_dptr(parent, bcpup);
22217 +               h_dir = d_inode(h_parent);
22218 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22219 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22220 +               /* todo: no unlock here */
22221 +               inode_unlock_shared(h_dir);
22222 +
22223 +               AuDbg("bcpup %d\n", bcpup);
22224 +               if (!err) {
22225 +                       if (d_really_is_negative(dentry))
22226 +                               au_set_h_dptr(dentry, btop, NULL);
22227 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22228 +               }
22229 +       }
22230 +
22231 +       if (!add_entry)
22232 +               di_write_unlock(parent);
22233 +       if (!err)
22234 +               err = bcpup; /* success */
22235 +
22236 +       AuTraceErr(err);
22237 +       return err;
22238 +}
22239 +
22240 +/*
22241 + * decide the branch and the parent dir where we will create a new entry.
22242 + * returns new bindex or an error.
22243 + * copyup the parent dir if needed.
22244 + */
22245 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22246 +             struct au_wr_dir_args *args)
22247 +{
22248 +       int err;
22249 +       unsigned int flags;
22250 +       aufs_bindex_t bcpup, btop, src_btop;
22251 +       const unsigned char add_entry
22252 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22253 +               | au_ftest_wrdir(args->flags, TMPFILE);
22254 +       struct super_block *sb;
22255 +       struct dentry *parent;
22256 +       struct au_sbinfo *sbinfo;
22257 +
22258 +       sb = dentry->d_sb;
22259 +       sbinfo = au_sbi(sb);
22260 +       parent = dget_parent(dentry);
22261 +       btop = au_dbtop(dentry);
22262 +       bcpup = btop;
22263 +       if (args->force_btgt < 0) {
22264 +               if (src_dentry) {
22265 +                       src_btop = au_dbtop(src_dentry);
22266 +                       if (src_btop < btop)
22267 +                               bcpup = src_btop;
22268 +               } else if (add_entry) {
22269 +                       flags = 0;
22270 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22271 +                               au_fset_wbr(flags, DIR);
22272 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22273 +                       bcpup = err;
22274 +               }
22275 +
22276 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22277 +                       if (add_entry)
22278 +                               err = AuWbrCopyup(sbinfo, dentry);
22279 +                       else {
22280 +                               if (!IS_ROOT(dentry)) {
22281 +                                       di_read_lock_parent(parent, !AuLock_IR);
22282 +                                       err = AuWbrCopyup(sbinfo, dentry);
22283 +                                       di_read_unlock(parent, !AuLock_IR);
22284 +                               } else
22285 +                                       err = AuWbrCopyup(sbinfo, dentry);
22286 +                       }
22287 +                       bcpup = err;
22288 +                       if (unlikely(err < 0))
22289 +                               goto out;
22290 +               }
22291 +       } else {
22292 +               bcpup = args->force_btgt;
22293 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22294 +       }
22295 +
22296 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22297 +       err = bcpup;
22298 +       if (bcpup == btop)
22299 +               goto out; /* success */
22300 +
22301 +       /* copyup the new parent into the branch we process */
22302 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22303 +       if (err >= 0) {
22304 +               if (d_really_is_negative(dentry)) {
22305 +                       au_set_h_dptr(dentry, btop, NULL);
22306 +                       au_set_dbtop(dentry, bcpup);
22307 +                       au_set_dbbot(dentry, bcpup);
22308 +               }
22309 +               AuDebugOn(add_entry
22310 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22311 +                         && !au_h_dptr(dentry, bcpup));
22312 +       }
22313 +
22314 +out:
22315 +       dput(parent);
22316 +       return err;
22317 +}
22318 +
22319 +/* ---------------------------------------------------------------------- */
22320 +
22321 +void au_pin_hdir_unlock(struct au_pin *p)
22322 +{
22323 +       if (p->hdir)
22324 +               au_hn_inode_unlock(p->hdir);
22325 +}
22326 +
22327 +int au_pin_hdir_lock(struct au_pin *p)
22328 +{
22329 +       int err;
22330 +
22331 +       err = 0;
22332 +       if (!p->hdir)
22333 +               goto out;
22334 +
22335 +       /* even if an error happens later, keep this lock */
22336 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22337 +
22338 +       err = -EBUSY;
22339 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22340 +               goto out;
22341 +
22342 +       err = 0;
22343 +       if (p->h_dentry)
22344 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22345 +                                 p->h_parent, p->br);
22346 +
22347 +out:
22348 +       return err;
22349 +}
22350 +
22351 +int au_pin_hdir_relock(struct au_pin *p)
22352 +{
22353 +       int err, i;
22354 +       struct inode *h_i;
22355 +       struct dentry *h_d[] = {
22356 +               p->h_dentry,
22357 +               p->h_parent
22358 +       };
22359 +
22360 +       err = au_pin_hdir_lock(p);
22361 +       if (unlikely(err))
22362 +               goto out;
22363 +
22364 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22365 +               if (!h_d[i])
22366 +                       continue;
22367 +               if (d_is_positive(h_d[i])) {
22368 +                       h_i = d_inode(h_d[i]);
22369 +                       err = !h_i->i_nlink;
22370 +               }
22371 +       }
22372 +
22373 +out:
22374 +       return err;
22375 +}
22376 +
22377 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22378 +{
22379 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22380 +}
22381 +
22382 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22383 +{
22384 +       if (p->hdir) {
22385 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22386 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22387 +               au_pin_hdir_set_owner(p, current);
22388 +       }
22389 +}
22390 +
22391 +void au_pin_hdir_release(struct au_pin *p)
22392 +{
22393 +       if (p->hdir) {
22394 +               au_pin_hdir_set_owner(p, p->task);
22395 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22396 +       }
22397 +}
22398 +
22399 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22400 +{
22401 +       if (pin && pin->parent)
22402 +               return au_h_dptr(pin->parent, pin->bindex);
22403 +       return NULL;
22404 +}
22405 +
22406 +void au_unpin(struct au_pin *p)
22407 +{
22408 +       if (p->hdir)
22409 +               au_pin_hdir_unlock(p);
22410 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22411 +               vfsub_mnt_drop_write(p->h_mnt);
22412 +       if (!p->hdir)
22413 +               return;
22414 +
22415 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22416 +               di_read_unlock(p->parent, AuLock_IR);
22417 +       iput(p->hdir->hi_inode);
22418 +       dput(p->parent);
22419 +       p->parent = NULL;
22420 +       p->hdir = NULL;
22421 +       p->h_mnt = NULL;
22422 +       /* do not clear p->task */
22423 +}
22424 +
22425 +int au_do_pin(struct au_pin *p)
22426 +{
22427 +       int err;
22428 +       struct super_block *sb;
22429 +       struct inode *h_dir;
22430 +
22431 +       err = 0;
22432 +       sb = p->dentry->d_sb;
22433 +       p->br = au_sbr(sb, p->bindex);
22434 +       if (IS_ROOT(p->dentry)) {
22435 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22436 +                       p->h_mnt = au_br_mnt(p->br);
22437 +                       err = vfsub_mnt_want_write(p->h_mnt);
22438 +                       if (unlikely(err)) {
22439 +                               au_fclr_pin(p->flags, MNT_WRITE);
22440 +                               goto out_err;
22441 +                       }
22442 +               }
22443 +               goto out;
22444 +       }
22445 +
22446 +       p->h_dentry = NULL;
22447 +       if (p->bindex <= au_dbbot(p->dentry))
22448 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22449 +
22450 +       p->parent = dget_parent(p->dentry);
22451 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22452 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22453 +
22454 +       h_dir = NULL;
22455 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22456 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22457 +       if (p->hdir)
22458 +               h_dir = p->hdir->hi_inode;
22459 +
22460 +       /*
22461 +        * udba case, or
22462 +        * if DI_LOCKED is not set, then p->parent may be different
22463 +        * and h_parent can be NULL.
22464 +        */
22465 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22466 +               err = -EBUSY;
22467 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22468 +                       di_read_unlock(p->parent, AuLock_IR);
22469 +               dput(p->parent);
22470 +               p->parent = NULL;
22471 +               goto out_err;
22472 +       }
22473 +
22474 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22475 +               p->h_mnt = au_br_mnt(p->br);
22476 +               err = vfsub_mnt_want_write(p->h_mnt);
22477 +               if (unlikely(err)) {
22478 +                       au_fclr_pin(p->flags, MNT_WRITE);
22479 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22480 +                               di_read_unlock(p->parent, AuLock_IR);
22481 +                       dput(p->parent);
22482 +                       p->parent = NULL;
22483 +                       goto out_err;
22484 +               }
22485 +       }
22486 +
22487 +       au_igrab(h_dir);
22488 +       err = au_pin_hdir_lock(p);
22489 +       if (!err)
22490 +               goto out; /* success */
22491 +
22492 +       au_unpin(p);
22493 +
22494 +out_err:
22495 +       pr_err("err %d\n", err);
22496 +       err = au_busy_or_stale();
22497 +out:
22498 +       return err;
22499 +}
22500 +
22501 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22502 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22503 +                unsigned int udba, unsigned char flags)
22504 +{
22505 +       p->dentry = dentry;
22506 +       p->udba = udba;
22507 +       p->lsc_di = lsc_di;
22508 +       p->lsc_hi = lsc_hi;
22509 +       p->flags = flags;
22510 +       p->bindex = bindex;
22511 +
22512 +       p->parent = NULL;
22513 +       p->hdir = NULL;
22514 +       p->h_mnt = NULL;
22515 +
22516 +       p->h_dentry = NULL;
22517 +       p->h_parent = NULL;
22518 +       p->br = NULL;
22519 +       p->task = current;
22520 +}
22521 +
22522 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22523 +          unsigned int udba, unsigned char flags)
22524 +{
22525 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22526 +                   udba, flags);
22527 +       return au_do_pin(pin);
22528 +}
22529 +
22530 +/* ---------------------------------------------------------------------- */
22531 +
22532 +/*
22533 + * ->setattr() and ->getattr() are called in various cases.
22534 + * chmod, stat: dentry is revalidated.
22535 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22536 + *               unhashed.
22537 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22538 + */
22539 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22540 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22541 +{
22542 +       int err;
22543 +       struct dentry *parent;
22544 +
22545 +       err = 0;
22546 +       if (au_digen_test(dentry, sigen)) {
22547 +               parent = dget_parent(dentry);
22548 +               di_read_lock_parent(parent, AuLock_IR);
22549 +               err = au_refresh_dentry(dentry, parent);
22550 +               di_read_unlock(parent, AuLock_IR);
22551 +               dput(parent);
22552 +       }
22553 +
22554 +       AuTraceErr(err);
22555 +       return err;
22556 +}
22557 +
22558 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22559 +                    struct au_icpup_args *a)
22560 +{
22561 +       int err;
22562 +       loff_t sz;
22563 +       aufs_bindex_t btop, ibtop;
22564 +       struct dentry *hi_wh, *parent;
22565 +       struct inode *inode;
22566 +       struct au_wr_dir_args wr_dir_args = {
22567 +               .force_btgt     = -1,
22568 +               .flags          = 0
22569 +       };
22570 +
22571 +       if (d_is_dir(dentry))
22572 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22573 +       /* plink or hi_wh() case */
22574 +       btop = au_dbtop(dentry);
22575 +       inode = d_inode(dentry);
22576 +       ibtop = au_ibtop(inode);
22577 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22578 +               wr_dir_args.force_btgt = ibtop;
22579 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22580 +       if (unlikely(err < 0))
22581 +               goto out;
22582 +       a->btgt = err;
22583 +       if (err != btop)
22584 +               au_fset_icpup(a->flags, DID_CPUP);
22585 +
22586 +       err = 0;
22587 +       a->pin_flags = AuPin_MNT_WRITE;
22588 +       parent = NULL;
22589 +       if (!IS_ROOT(dentry)) {
22590 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22591 +               parent = dget_parent(dentry);
22592 +               di_write_lock_parent(parent);
22593 +       }
22594 +
22595 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22596 +       if (unlikely(err))
22597 +               goto out_parent;
22598 +
22599 +       sz = -1;
22600 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22601 +       a->h_inode = d_inode(a->h_path.dentry);
22602 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22603 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22604 +               if (ia->ia_size < i_size_read(a->h_inode))
22605 +                       sz = ia->ia_size;
22606 +               inode_unlock_shared(a->h_inode);
22607 +       }
22608 +
22609 +       hi_wh = NULL;
22610 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22611 +               hi_wh = au_hi_wh(inode, a->btgt);
22612 +               if (!hi_wh) {
22613 +                       struct au_cp_generic cpg = {
22614 +                               .dentry = dentry,
22615 +                               .bdst   = a->btgt,
22616 +                               .bsrc   = -1,
22617 +                               .len    = sz,
22618 +                               .pin    = &a->pin
22619 +                       };
22620 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22621 +                       if (unlikely(err))
22622 +                               goto out_unlock;
22623 +                       hi_wh = au_hi_wh(inode, a->btgt);
22624 +                       /* todo: revalidate hi_wh? */
22625 +               }
22626 +       }
22627 +
22628 +       if (parent) {
22629 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22630 +               di_downgrade_lock(parent, AuLock_IR);
22631 +               dput(parent);
22632 +               parent = NULL;
22633 +       }
22634 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22635 +               goto out; /* success */
22636 +
22637 +       if (!d_unhashed(dentry)) {
22638 +               struct au_cp_generic cpg = {
22639 +                       .dentry = dentry,
22640 +                       .bdst   = a->btgt,
22641 +                       .bsrc   = btop,
22642 +                       .len    = sz,
22643 +                       .pin    = &a->pin,
22644 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22645 +               };
22646 +               err = au_sio_cpup_simple(&cpg);
22647 +               if (!err)
22648 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22649 +       } else if (!hi_wh)
22650 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22651 +       else
22652 +               a->h_path.dentry = hi_wh; /* do not dget here */
22653 +
22654 +out_unlock:
22655 +       a->h_inode = d_inode(a->h_path.dentry);
22656 +       if (!err)
22657 +               goto out; /* success */
22658 +       au_unpin(&a->pin);
22659 +out_parent:
22660 +       if (parent) {
22661 +               di_write_unlock(parent);
22662 +               dput(parent);
22663 +       }
22664 +out:
22665 +       if (!err)
22666 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22667 +       return err;
22668 +}
22669 +
22670 +static int aufs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
22671 +                       struct iattr *ia)
22672 +{
22673 +       int err;
22674 +       struct inode *inode, *delegated;
22675 +       struct super_block *sb;
22676 +       struct file *file;
22677 +       struct au_icpup_args *a;
22678 +       struct mnt_idmap *h_idmap;
22679 +
22680 +       inode = d_inode(dentry);
22681 +       IMustLock(inode);
22682 +
22683 +       err = setattr_prepare(idmap, dentry, ia);
22684 +       if (unlikely(err))
22685 +               goto out;
22686 +
22687 +       err = -ENOMEM;
22688 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22689 +       if (unlikely(!a))
22690 +               goto out;
22691 +
22692 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22693 +               ia->ia_valid &= ~ATTR_MODE;
22694 +
22695 +       file = NULL;
22696 +       sb = dentry->d_sb;
22697 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22698 +       if (unlikely(err))
22699 +               goto out_kfree;
22700 +
22701 +       if (ia->ia_valid & ATTR_FILE) {
22702 +               /* currently ftruncate(2) only */
22703 +               AuDebugOn(!d_is_reg(dentry));
22704 +               file = ia->ia_file;
22705 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22706 +                                           /*fi_lsc*/0);
22707 +               if (unlikely(err))
22708 +                       goto out_si;
22709 +               ia->ia_file = au_hf_top(file);
22710 +               a->udba = AuOpt_UDBA_NONE;
22711 +       } else {
22712 +               /* fchmod() doesn't pass ia_file */
22713 +               a->udba = au_opt_udba(sb);
22714 +               di_write_lock_child(dentry);
22715 +               /* no d_unlinked(), to set UDBA_NONE for root */
22716 +               if (d_unhashed(dentry))
22717 +                       a->udba = AuOpt_UDBA_NONE;
22718 +               if (a->udba != AuOpt_UDBA_NONE) {
22719 +                       AuDebugOn(IS_ROOT(dentry));
22720 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22721 +                       if (unlikely(err))
22722 +                               goto out_dentry;
22723 +               }
22724 +       }
22725 +
22726 +       err = au_pin_and_icpup(dentry, ia, a);
22727 +       if (unlikely(err < 0))
22728 +               goto out_dentry;
22729 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22730 +               ia->ia_file = NULL;
22731 +               ia->ia_valid &= ~ATTR_FILE;
22732 +       }
22733 +
22734 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22735 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22736 +           == (ATTR_MODE | ATTR_CTIME)) {
22737 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22738 +               if (unlikely(err))
22739 +                       goto out_unlock;
22740 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22741 +                  && (ia->ia_valid & ATTR_CTIME)) {
22742 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22743 +               if (unlikely(err))
22744 +                       goto out_unlock;
22745 +       }
22746 +
22747 +       if (ia->ia_valid & ATTR_SIZE) {
22748 +               struct file *f;
22749 +
22750 +               if (ia->ia_size < i_size_read(inode))
22751 +                       /* unmap only */
22752 +                       truncate_setsize(inode, ia->ia_size);
22753 +
22754 +               f = NULL;
22755 +               if (ia->ia_valid & ATTR_FILE)
22756 +                       f = ia->ia_file;
22757 +               inode_unlock(a->h_inode);
22758 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22759 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22760 +       } else {
22761 +               delegated = NULL;
22762 +               while (1) {
22763 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22764 +                       if (delegated) {
22765 +                               err = break_deleg_wait(&delegated);
22766 +                               if (!err)
22767 +                                       continue;
22768 +                       }
22769 +                       break;
22770 +               }
22771 +       }
22772 +       /*
22773 +        * regardless aufs 'acl' option setting.
22774 +        * why don't all acl-aware fs call this func from their ->setattr()?
22775 +        */
22776 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22777 +               h_idmap = mnt_idmap(a->h_path.mnt);
22778 +               err = vfsub_acl_chmod(h_idmap, a->h_path.dentry, ia->ia_mode);
22779 +       }
22780 +       if (!err)
22781 +               au_cpup_attr_changeable(inode);
22782 +
22783 +out_unlock:
22784 +       inode_unlock(a->h_inode);
22785 +       au_unpin(&a->pin);
22786 +       if (unlikely(err))
22787 +               au_update_dbtop(dentry);
22788 +out_dentry:
22789 +       di_write_unlock(dentry);
22790 +       if (file) {
22791 +               fi_write_unlock(file);
22792 +               ia->ia_file = file;
22793 +               ia->ia_valid |= ATTR_FILE;
22794 +       }
22795 +out_si:
22796 +       si_read_unlock(sb);
22797 +out_kfree:
22798 +       au_kfree_rcu(a);
22799 +out:
22800 +       AuTraceErr(err);
22801 +       return err;
22802 +}
22803 +
22804 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22805 +static int au_h_path_to_set_attr(struct dentry *dentry,
22806 +                                struct au_icpup_args *a, struct path *h_path)
22807 +{
22808 +       int err;
22809 +       struct super_block *sb;
22810 +
22811 +       sb = dentry->d_sb;
22812 +       a->udba = au_opt_udba(sb);
22813 +       /* no d_unlinked(), to set UDBA_NONE for root */
22814 +       if (d_unhashed(dentry))
22815 +               a->udba = AuOpt_UDBA_NONE;
22816 +       if (a->udba != AuOpt_UDBA_NONE) {
22817 +               AuDebugOn(IS_ROOT(dentry));
22818 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22819 +               if (unlikely(err))
22820 +                       goto out;
22821 +       }
22822 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22823 +       if (unlikely(err < 0))
22824 +               goto out;
22825 +
22826 +       h_path->dentry = a->h_path.dentry;
22827 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22828 +
22829 +out:
22830 +       return err;
22831 +}
22832 +
22833 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22834 +                 struct au_sxattr *arg)
22835 +{
22836 +       int err;
22837 +       struct path h_path;
22838 +       struct super_block *sb;
22839 +       struct au_icpup_args *a;
22840 +       struct inode *h_inode;
22841 +       struct mnt_idmap *h_idmap;
22842 +
22843 +       IMustLock(inode);
22844 +
22845 +       err = -ENOMEM;
22846 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22847 +       if (unlikely(!a))
22848 +               goto out;
22849 +
22850 +       sb = dentry->d_sb;
22851 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22852 +       if (unlikely(err))
22853 +               goto out_kfree;
22854 +
22855 +       h_path.dentry = NULL;   /* silence gcc */
22856 +       di_write_lock_child(dentry);
22857 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22858 +       if (unlikely(err))
22859 +               goto out_di;
22860 +       h_idmap = mnt_idmap(h_path.mnt);
22861 +
22862 +       inode_unlock(a->h_inode);
22863 +       switch (arg->type) {
22864 +       case AU_XATTR_SET:
22865 +               AuDebugOn(d_is_negative(h_path.dentry));
22866 +               err = vfsub_setxattr(h_idmap, h_path.dentry,
22867 +                                    arg->u.set.name, arg->u.set.value,
22868 +                                    arg->u.set.size, arg->u.set.flags);
22869 +               break;
22870 +       case AU_ACL_SET:
22871 +               err = -EOPNOTSUPP;
22872 +               h_inode = d_inode(h_path.dentry);
22873 +               if (h_inode->i_op->set_acl) {
22874 +                       /* this will call posix_acl_update_mode */
22875 +                       err = h_inode->i_op->set_acl(h_idmap, h_path.dentry,
22876 +                                                    arg->u.acl_set.acl,
22877 +                                                    arg->u.acl_set.type);
22878 +               }
22879 +               break;
22880 +       }
22881 +       if (!err)
22882 +               au_cpup_attr_timesizes(inode);
22883 +
22884 +       au_unpin(&a->pin);
22885 +       if (unlikely(err))
22886 +               au_update_dbtop(dentry);
22887 +
22888 +out_di:
22889 +       di_write_unlock(dentry);
22890 +       si_read_unlock(sb);
22891 +out_kfree:
22892 +       au_kfree_rcu(a);
22893 +out:
22894 +       AuTraceErr(err);
22895 +       return err;
22896 +}
22897 +#endif
22898 +
22899 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22900 +                            unsigned int nlink)
22901 +{
22902 +       unsigned int n;
22903 +
22904 +       inode->i_mode = st->mode;
22905 +       /* don't i_[ug]id_write() here */
22906 +       inode->i_uid = st->uid;
22907 +       inode->i_gid = st->gid;
22908 +       inode_set_atime_to_ts(inode, st->atime);
22909 +       inode_set_mtime_to_ts(inode, st->mtime);
22910 +       inode_set_ctime_to_ts(inode, st->ctime);
22911 +
22912 +       au_cpup_attr_nlink(inode, /*force*/0);
22913 +       if (S_ISDIR(inode->i_mode)) {
22914 +               n = inode->i_nlink;
22915 +               n -= nlink;
22916 +               n += st->nlink;
22917 +               smp_mb(); /* for i_nlink */
22918 +               /* 0 can happen */
22919 +               set_nlink(inode, n);
22920 +       }
22921 +
22922 +       spin_lock(&inode->i_lock);
22923 +       inode->i_blocks = st->blocks;
22924 +       i_size_write(inode, st->size);
22925 +       spin_unlock(&inode->i_lock);
22926 +}
22927 +
22928 +/*
22929 + * common routine for aufs_getattr() and au_getxattr().
22930 + * returns zero or negative (an error).
22931 + * @dentry will be read-locked in success.
22932 + */
22933 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22934 +                     struct path *h_path, int locked)
22935 +{
22936 +       int err;
22937 +       unsigned int mnt_flags, sigen;
22938 +       unsigned char udba_none;
22939 +       aufs_bindex_t bindex;
22940 +       struct super_block *sb, *h_sb;
22941 +
22942 +       h_path->mnt = NULL;
22943 +       h_path->dentry = NULL;
22944 +
22945 +       err = 0;
22946 +       sb = dentry->d_sb;
22947 +       mnt_flags = au_mntflags(sb);
22948 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22949 +
22950 +       if (unlikely(locked))
22951 +               goto body; /* skip locking dinfo */
22952 +
22953 +       /* support fstat(2) */
22954 +       if (!d_unlinked(dentry) && !udba_none) {
22955 +               sigen = au_sigen(sb);
22956 +               err = au_digen_test(dentry, sigen);
22957 +               if (!err) {
22958 +                       di_read_lock_child(dentry, AuLock_IR);
22959 +                       err = au_dbrange_test(dentry);
22960 +                       if (unlikely(err)) {
22961 +                               di_read_unlock(dentry, AuLock_IR);
22962 +                               goto out;
22963 +                       }
22964 +               } else {
22965 +                       AuDebugOn(IS_ROOT(dentry));
22966 +                       di_write_lock_child(dentry);
22967 +                       err = au_dbrange_test(dentry);
22968 +                       if (!err)
22969 +                               err = au_reval_for_attr(dentry, sigen);
22970 +                       if (!err)
22971 +                               di_downgrade_lock(dentry, AuLock_IR);
22972 +                       else {
22973 +                               di_write_unlock(dentry);
22974 +                               goto out;
22975 +                       }
22976 +               }
22977 +       } else
22978 +               di_read_lock_child(dentry, AuLock_IR);
22979 +
22980 +body:
22981 +       if (!inode) {
22982 +               inode = d_inode(dentry);
22983 +               if (unlikely(!inode))
22984 +                       goto out;
22985 +       }
22986 +       bindex = au_ibtop(inode);
22987 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22988 +       h_sb = h_path->mnt->mnt_sb;
22989 +       if (!force
22990 +           && !au_test_fs_bad_iattr(h_sb)
22991 +           && udba_none)
22992 +               goto out; /* success */
22993 +
22994 +       if (au_dbtop(dentry) == bindex)
22995 +               h_path->dentry = au_h_dptr(dentry, bindex);
22996 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22997 +               h_path->dentry = au_plink_lkup(inode, bindex);
22998 +               if (IS_ERR(h_path->dentry))
22999 +                       /* pretending success */
23000 +                       h_path->dentry = NULL;
23001 +               else
23002 +                       dput(h_path->dentry);
23003 +       }
23004 +
23005 +out:
23006 +       return err;
23007 +}
23008 +
23009 +static int aufs_getattr(struct mnt_idmap *idmap, const struct path *path,
23010 +                       struct kstat *st, u32 request, unsigned int query)
23011 +{
23012 +       int err;
23013 +       unsigned char positive;
23014 +       struct path h_path;
23015 +       struct dentry *dentry;
23016 +       struct inode *inode;
23017 +       struct super_block *sb;
23018 +
23019 +       dentry = path->dentry;
23020 +       inode = d_inode(dentry);
23021 +       sb = dentry->d_sb;
23022 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23023 +       if (unlikely(err))
23024 +               goto out;
23025 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23026 +                               /*locked*/0);
23027 +       if (unlikely(err))
23028 +               goto out_si;
23029 +       if (unlikely(!h_path.dentry))
23030 +               /* illegally overlapped or something */
23031 +               goto out_fill; /* pretending success */
23032 +
23033 +       positive = d_is_positive(h_path.dentry);
23034 +       if (positive) {
23035 +               /* no vfsub version */
23036 +               if (query & AT_GETATTR_NOSEC)
23037 +                       err = vfs_getattr_nosec(&h_path, st, request, query);
23038 +               else
23039 +                       err = vfs_getattr(&h_path, st, request, query);
23040 +       }
23041 +       if (!err) {
23042 +               if (positive)
23043 +                       au_refresh_iattr(inode, st,
23044 +                                        d_inode(h_path.dentry)->i_nlink);
23045 +               goto out_fill; /* success */
23046 +       }
23047 +       AuTraceErr(err);
23048 +       goto out_di;
23049 +
23050 +out_fill:
23051 +       generic_fillattr(idmap, request, inode, st);
23052 +out_di:
23053 +       di_read_unlock(dentry, AuLock_IR);
23054 +out_si:
23055 +       si_read_unlock(sb);
23056 +out:
23057 +       AuTraceErr(err);
23058 +       return err;
23059 +}
23060 +
23061 +/* ---------------------------------------------------------------------- */
23062 +
23063 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23064 +                                struct delayed_call *done)
23065 +{
23066 +       const char *ret;
23067 +       struct dentry *h_dentry;
23068 +       struct inode *h_inode;
23069 +       int err;
23070 +       aufs_bindex_t bindex;
23071 +
23072 +       ret = NULL; /* suppress a warning */
23073 +       err = -ECHILD;
23074 +       if (!dentry)
23075 +               goto out;
23076 +
23077 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23078 +       if (unlikely(err))
23079 +               goto out;
23080 +
23081 +       err = au_d_hashed_positive(dentry);
23082 +       if (unlikely(err))
23083 +               goto out_unlock;
23084 +
23085 +       err = -EINVAL;
23086 +       inode = d_inode(dentry);
23087 +       bindex = au_ibtop(inode);
23088 +       h_inode = au_h_iptr(inode, bindex);
23089 +       if (unlikely(!h_inode->i_op->get_link))
23090 +               goto out_unlock;
23091 +
23092 +       err = -EBUSY;
23093 +       h_dentry = NULL;
23094 +       if (au_dbtop(dentry) <= bindex) {
23095 +               h_dentry = au_h_dptr(dentry, bindex);
23096 +               if (h_dentry)
23097 +                       dget(h_dentry);
23098 +       }
23099 +       if (!h_dentry) {
23100 +               h_dentry = d_find_any_alias(h_inode);
23101 +               if (IS_ERR(h_dentry)) {
23102 +                       err = PTR_ERR(h_dentry);
23103 +                       goto out_unlock;
23104 +               }
23105 +       }
23106 +       if (unlikely(!h_dentry))
23107 +               goto out_unlock;
23108 +
23109 +       err = 0;
23110 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23111 +       AuDbgDentry(h_dentry);
23112 +       ret = vfs_get_link(h_dentry, done);
23113 +       dput(h_dentry);
23114 +       if (IS_ERR(ret))
23115 +               err = PTR_ERR(ret);
23116 +
23117 +out_unlock:
23118 +       aufs_read_unlock(dentry, AuLock_IR);
23119 +out:
23120 +       if (unlikely(err))
23121 +               ret = ERR_PTR(err);
23122 +       AuTraceErrPtr(ret);
23123 +       return ret;
23124 +}
23125 +
23126 +/* ---------------------------------------------------------------------- */
23127 +
23128 +static int au_is_special(struct inode *inode)
23129 +{
23130 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23131 +}
23132 +
23133 +static int aufs_update_time(struct inode *inode, int flags)
23134 +{
23135 +       int err;
23136 +       aufs_bindex_t bindex;
23137 +       struct super_block *sb;
23138 +       struct inode *h_inode;
23139 +       struct vfsmount *h_mnt;
23140 +
23141 +       sb = inode->i_sb;
23142 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23143 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23144 +
23145 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23146 +       lockdep_off();
23147 +       si_read_lock(sb, AuLock_FLUSH);
23148 +       ii_write_lock_child(inode);
23149 +
23150 +       err = 0;
23151 +       bindex = au_ibtop(inode);
23152 +       h_inode = au_h_iptr(inode, bindex);
23153 +       if (!au_test_ro(sb, bindex, inode)) {
23154 +               h_mnt = au_sbr_mnt(sb, bindex);
23155 +               err = vfsub_mnt_want_write(h_mnt);
23156 +               if (!err) {
23157 +                       err = vfsub_update_time(h_inode, flags);
23158 +                       vfsub_mnt_drop_write(h_mnt);
23159 +               }
23160 +       } else if (au_is_special(h_inode)) {
23161 +               /*
23162 +                * Never copy-up here.
23163 +                * These special files may already be opened and used for
23164 +                * communicating. If we copied it up, then the communication
23165 +                * would be corrupted.
23166 +                */
23167 +               AuWarn1("timestamps for i%lu are ignored "
23168 +                       "since it is on readonly branch (hi%lu).\n",
23169 +                       inode->i_ino, h_inode->i_ino);
23170 +       } else if (flags & ~S_ATIME) {
23171 +               err = -EIO;
23172 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23173 +               AuDebugOn(1);
23174 +       }
23175 +
23176 +       if (!err)
23177 +               au_cpup_attr_timesizes(inode);
23178 +       ii_write_unlock(inode);
23179 +       si_read_unlock(sb);
23180 +       lockdep_on();
23181 +
23182 +       if (!err && (flags & S_VERSION))
23183 +               inode_inc_iversion(inode);
23184 +
23185 +       return err;
23186 +}
23187 +
23188 +/* ---------------------------------------------------------------------- */
23189 +
23190 +/* no getattr version will be set by module.c:aufs_init() */
23191 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23192 +       aufs_iop[] = {
23193 +       [AuIop_SYMLINK] = {
23194 +               .permission     = aufs_permission,
23195 +#ifdef CONFIG_FS_POSIX_ACL
23196 +               .get_inode_acl  = aufs_get_inode_acl,
23197 +               .get_acl        = aufs_get_acl,
23198 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23199 +#endif
23200 +
23201 +               .setattr        = aufs_setattr,
23202 +               .getattr        = aufs_getattr,
23203 +
23204 +#ifdef CONFIG_AUFS_XATTR
23205 +               .listxattr      = aufs_listxattr,
23206 +#endif
23207 +
23208 +               .get_link       = aufs_get_link
23209 +
23210 +               /* .update_time = aufs_update_time */
23211 +       },
23212 +       [AuIop_DIR] = {
23213 +               .create         = aufs_create,
23214 +               .lookup         = aufs_lookup,
23215 +               .link           = aufs_link,
23216 +               .unlink         = aufs_unlink,
23217 +               .symlink        = aufs_symlink,
23218 +               .mkdir          = aufs_mkdir,
23219 +               .rmdir          = aufs_rmdir,
23220 +               .mknod          = aufs_mknod,
23221 +               .rename         = aufs_rename,
23222 +
23223 +               .permission     = aufs_permission,
23224 +#ifdef CONFIG_FS_POSIX_ACL
23225 +               .get_inode_acl  = aufs_get_inode_acl,
23226 +               .get_acl        = aufs_get_acl,
23227 +               .set_acl        = aufs_set_acl,
23228 +#endif
23229 +
23230 +               .setattr        = aufs_setattr,
23231 +               .getattr        = aufs_getattr,
23232 +
23233 +#ifdef CONFIG_AUFS_XATTR
23234 +               .listxattr      = aufs_listxattr,
23235 +#endif
23236 +
23237 +               .update_time    = aufs_update_time,
23238 +               .atomic_open    = aufs_atomic_open,
23239 +               .tmpfile        = aufs_tmpfile
23240 +       },
23241 +       [AuIop_OTHER] = {
23242 +               .permission     = aufs_permission,
23243 +#ifdef CONFIG_FS_POSIX_ACL
23244 +               .get_inode_acl  = aufs_get_inode_acl,
23245 +               .get_acl        = aufs_get_acl,
23246 +               .set_acl        = aufs_set_acl,
23247 +#endif
23248 +
23249 +               .setattr        = aufs_setattr,
23250 +               .getattr        = aufs_getattr,
23251 +
23252 +#ifdef CONFIG_AUFS_XATTR
23253 +               .listxattr      = aufs_listxattr,
23254 +#endif
23255 +
23256 +               .update_time    = aufs_update_time
23257 +       }
23258 +};
23259 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23260 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23261 +++ linux/fs/aufs/i_op_del.c    2023-10-31 09:31:04.199880750 +0100
23262 @@ -0,0 +1,523 @@
23263 +// SPDX-License-Identifier: GPL-2.0
23264 +/*
23265 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23266 + *
23267 + * This program is free software; you can redistribute it and/or modify
23268 + * it under the terms of the GNU General Public License as published by
23269 + * the Free Software Foundation; either version 2 of the License, or
23270 + * (at your option) any later version.
23271 + *
23272 + * This program is distributed in the hope that it will be useful,
23273 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23274 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23275 + * GNU General Public License for more details.
23276 + *
23277 + * You should have received a copy of the GNU General Public License
23278 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23279 + */
23280 +
23281 +/*
23282 + * inode operations (del entry)
23283 + */
23284 +
23285 +#include <linux/iversion.h>
23286 +#include "aufs.h"
23287 +
23288 +/*
23289 + * decide if a new whiteout for @dentry is necessary or not.
23290 + * when it is necessary, prepare the parent dir for the upper branch whose
23291 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23292 + * be done by caller.
23293 + * return value:
23294 + * 0: wh is unnecessary
23295 + * plus: wh is necessary
23296 + * minus: error
23297 + */
23298 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23299 +{
23300 +       int need_wh, err;
23301 +       aufs_bindex_t btop;
23302 +       struct super_block *sb;
23303 +
23304 +       sb = dentry->d_sb;
23305 +       btop = au_dbtop(dentry);
23306 +       if (*bcpup < 0) {
23307 +               *bcpup = btop;
23308 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23309 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23310 +                       *bcpup = err;
23311 +                       if (unlikely(err < 0))
23312 +                               goto out;
23313 +               }
23314 +       } else
23315 +               AuDebugOn(btop < *bcpup
23316 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23317 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23318 +
23319 +       if (*bcpup != btop) {
23320 +               err = au_cpup_dirs(dentry, *bcpup);
23321 +               if (unlikely(err))
23322 +                       goto out;
23323 +               need_wh = 1;
23324 +       } else {
23325 +               struct au_dinfo *dinfo, *tmp;
23326 +
23327 +               need_wh = -ENOMEM;
23328 +               dinfo = au_di(dentry);
23329 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23330 +               if (tmp) {
23331 +                       au_di_cp(tmp, dinfo);
23332 +                       au_di_swap(tmp, dinfo);
23333 +                       /* returns the number of positive dentries */
23334 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23335 +                                                /* AuLkup_IGNORE_PERM */ 0);
23336 +                       au_di_swap(tmp, dinfo);
23337 +                       au_rw_write_unlock(&tmp->di_rwsem);
23338 +                       au_di_free(tmp);
23339 +               }
23340 +       }
23341 +       AuDbg("need_wh %d\n", need_wh);
23342 +       err = need_wh;
23343 +
23344 +out:
23345 +       return err;
23346 +}
23347 +
23348 +/*
23349 + * simple tests for the del-entry operations.
23350 + * following the checks in vfs, plus the parent-child relationship.
23351 + */
23352 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23353 +              struct dentry *h_parent, int isdir)
23354 +{
23355 +       int err;
23356 +       umode_t h_mode;
23357 +       struct dentry *h_dentry, *h_latest;
23358 +       struct inode *h_inode;
23359 +       struct path h_ppath;
23360 +       struct super_block *sb;
23361 +       struct au_branch *br;
23362 +       struct mnt_idmap *h_idmap;
23363 +
23364 +       h_dentry = au_h_dptr(dentry, bindex);
23365 +       if (d_really_is_positive(dentry)) {
23366 +               err = -ENOENT;
23367 +               if (unlikely(d_is_negative(h_dentry)))
23368 +                       goto out;
23369 +               h_inode = d_inode(h_dentry);
23370 +               if (unlikely(!h_inode->i_nlink))
23371 +                       goto out;
23372 +
23373 +               h_mode = h_inode->i_mode;
23374 +               if (!isdir) {
23375 +                       err = -EISDIR;
23376 +                       if (unlikely(S_ISDIR(h_mode)))
23377 +                               goto out;
23378 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23379 +                       err = -ENOTDIR;
23380 +                       goto out;
23381 +               }
23382 +       } else {
23383 +               /* rename(2) case */
23384 +               err = -EIO;
23385 +               if (unlikely(d_is_positive(h_dentry)))
23386 +                       goto out;
23387 +       }
23388 +
23389 +       err = -ENOENT;
23390 +       /* expected parent dir is locked */
23391 +       if (unlikely(h_parent != h_dentry->d_parent))
23392 +               goto out;
23393 +       err = 0;
23394 +
23395 +       /*
23396 +        * rmdir a dir may break the consistency on some filesystem.
23397 +        * let's try heavy test.
23398 +        */
23399 +       err = -EACCES;
23400 +       sb = dentry->d_sb;
23401 +       br = au_sbr(sb, bindex);
23402 +       h_idmap = au_br_idmap(br);
23403 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23404 +                    && au_test_h_perm(h_idmap, d_inode(h_parent),
23405 +                                      MAY_EXEC | MAY_WRITE)))
23406 +               goto out;
23407 +
23408 +       h_ppath.dentry = h_parent;
23409 +       h_ppath.mnt = au_br_mnt(br);
23410 +       h_latest = au_sio_lkup_one(h_idmap, &dentry->d_name, &h_ppath);
23411 +       err = -EIO;
23412 +       if (IS_ERR(h_latest))
23413 +               goto out;
23414 +       if (h_latest == h_dentry)
23415 +               err = 0;
23416 +       dput(h_latest);
23417 +
23418 +out:
23419 +       return err;
23420 +}
23421 +
23422 +/*
23423 + * decide the branch where we operate for @dentry. the branch index will be set
23424 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23425 + * dir for reverting.
23426 + * when a new whiteout is necessary, create it.
23427 + */
23428 +static struct dentry*
23429 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23430 +                   struct au_dtime *dt, struct au_pin *pin)
23431 +{
23432 +       struct dentry *wh_dentry;
23433 +       struct super_block *sb;
23434 +       struct path h_path;
23435 +       int err, need_wh;
23436 +       unsigned int udba;
23437 +       aufs_bindex_t bcpup;
23438 +
23439 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23440 +       wh_dentry = ERR_PTR(need_wh);
23441 +       if (unlikely(need_wh < 0))
23442 +               goto out;
23443 +
23444 +       sb = dentry->d_sb;
23445 +       udba = au_opt_udba(sb);
23446 +       bcpup = *rbcpup;
23447 +       err = au_pin(pin, dentry, bcpup, udba,
23448 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23449 +       wh_dentry = ERR_PTR(err);
23450 +       if (unlikely(err))
23451 +               goto out;
23452 +
23453 +       h_path.dentry = au_pinned_h_parent(pin);
23454 +       if (udba != AuOpt_UDBA_NONE
23455 +           && au_dbtop(dentry) == bcpup) {
23456 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23457 +               wh_dentry = ERR_PTR(err);
23458 +               if (unlikely(err))
23459 +                       goto out_unpin;
23460 +       }
23461 +
23462 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23463 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23464 +       wh_dentry = NULL;
23465 +       if (!need_wh)
23466 +               goto out; /* success, no need to create whiteout */
23467 +
23468 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23469 +       if (IS_ERR(wh_dentry))
23470 +               goto out_unpin;
23471 +
23472 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23473 +       goto out; /* success */
23474 +
23475 +out_unpin:
23476 +       au_unpin(pin);
23477 +out:
23478 +       return wh_dentry;
23479 +}
23480 +
23481 +/*
23482 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23483 + * in order to be revertible and save time for removing many child whiteouts
23484 + * under the dir.
23485 + * returns 1 when there are too many child whiteout and caller should remove
23486 + * them asynchronously. returns 0 when the number of children is enough small to
23487 + * remove now or the branch fs is a remote fs.
23488 + * otherwise return an error.
23489 + */
23490 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23491 +                          struct au_nhash *whlist, struct inode *dir)
23492 +{
23493 +       int rmdir_later, err, dirwh;
23494 +       struct dentry *h_dentry;
23495 +       struct super_block *sb;
23496 +       struct inode *inode;
23497 +
23498 +       sb = dentry->d_sb;
23499 +       SiMustAnyLock(sb);
23500 +       h_dentry = au_h_dptr(dentry, bindex);
23501 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23502 +       if (unlikely(err))
23503 +               goto out;
23504 +
23505 +       /* stop monitoring */
23506 +       inode = d_inode(dentry);
23507 +       au_hn_free(au_hi(inode, bindex));
23508 +
23509 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23510 +               dirwh = au_sbi(sb)->si_dirwh;
23511 +               rmdir_later = (dirwh <= 1);
23512 +               if (!rmdir_later)
23513 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23514 +                                                             dirwh);
23515 +               if (rmdir_later)
23516 +                       return rmdir_later;
23517 +       }
23518 +
23519 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23520 +       if (unlikely(err)) {
23521 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23522 +                       h_dentry, bindex, err);
23523 +               err = 0;
23524 +       }
23525 +
23526 +out:
23527 +       AuTraceErr(err);
23528 +       return err;
23529 +}
23530 +
23531 +/*
23532 + * final procedure for deleting a entry.
23533 + * maintain dentry and iattr.
23534 + */
23535 +static void epilog(struct inode *dir, struct dentry *dentry,
23536 +                  aufs_bindex_t bindex)
23537 +{
23538 +       struct inode *inode;
23539 +
23540 +       inode = d_inode(dentry);
23541 +       d_drop(dentry);
23542 +       inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
23543 +
23544 +       au_dir_ts(dir, bindex);
23545 +       inode_inc_iversion(dir);
23546 +}
23547 +
23548 +/*
23549 + * when an error happened, remove the created whiteout and revert everything.
23550 + */
23551 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23552 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23553 +                    struct dentry *dentry, struct au_dtime *dt)
23554 +{
23555 +       int rerr;
23556 +       struct path h_path = {
23557 +               .dentry = wh_dentry,
23558 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23559 +       };
23560 +
23561 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23562 +       if (!rerr) {
23563 +               au_set_dbwh(dentry, bwh);
23564 +               au_dtime_revert(dt);
23565 +               return 0;
23566 +       }
23567 +
23568 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23569 +       return -EIO;
23570 +}
23571 +
23572 +/* ---------------------------------------------------------------------- */
23573 +
23574 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23575 +{
23576 +       int err;
23577 +       aufs_bindex_t bwh, bindex, btop;
23578 +       struct inode *inode, *h_dir, *delegated, *h_inode;
23579 +       struct dentry *parent, *wh_dentry;
23580 +       /* to reduce stack size */
23581 +       struct {
23582 +               struct au_dtime dt;
23583 +               struct au_pin pin;
23584 +               struct path h_path;
23585 +       } *a;
23586 +
23587 +       IMustLock(dir);
23588 +
23589 +       err = -ENOMEM;
23590 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23591 +       if (unlikely(!a))
23592 +               goto out;
23593 +
23594 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23595 +       if (unlikely(err))
23596 +               goto out_free;
23597 +       err = au_d_hashed_positive(dentry);
23598 +       if (unlikely(err))
23599 +               goto out_unlock;
23600 +       inode = d_inode(dentry);
23601 +       IMustLock(inode);
23602 +       err = -EISDIR;
23603 +       if (unlikely(d_is_dir(dentry)))
23604 +               goto out_unlock; /* possible? */
23605 +
23606 +       btop = au_dbtop(dentry);
23607 +       bwh = au_dbwh(dentry);
23608 +       bindex = -1;
23609 +       parent = dentry->d_parent; /* dir inode is locked */
23610 +       di_write_lock_parent(parent);
23611 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23612 +                                       &a->pin);
23613 +       err = PTR_ERR(wh_dentry);
23614 +       if (IS_ERR(wh_dentry))
23615 +               goto out_parent;
23616 +
23617 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23618 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23619 +       dget(a->h_path.dentry);
23620 +       if (bindex == btop) {
23621 +               h_dir = au_pinned_h_dir(&a->pin);
23622 +               delegated = NULL;
23623 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23624 +               if (unlikely(err == -EWOULDBLOCK)) {
23625 +                       pr_warn("cannot retry for NFSv4 delegation"
23626 +                               " for an internal unlink\n");
23627 +                       iput(delegated);
23628 +               }
23629 +       } else {
23630 +               /* dir inode is locked */
23631 +               h_dir = d_inode(wh_dentry->d_parent);
23632 +               IMustLock(h_dir);
23633 +               err = 0;
23634 +       }
23635 +
23636 +       if (!err) {
23637 +               vfsub_drop_nlink(inode);
23638 +               epilog(dir, dentry, bindex);
23639 +
23640 +               /* update target timestamps */
23641 +               if (bindex == btop) {
23642 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23643 +                       /*ignore*/
23644 +                       h_inode = d_inode(a->h_path.dentry);
23645 +                       inode_set_ctime_to_ts(inode, inode_get_ctime(h_inode));
23646 +               } else
23647 +                       /* todo: this timestamp may be reverted later */
23648 +                       inode_set_ctime_to_ts(inode, inode_get_ctime(h_dir));
23649 +               goto out_unpin; /* success */
23650 +       }
23651 +
23652 +       /* revert */
23653 +       if (wh_dentry) {
23654 +               int rerr;
23655 +
23656 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23657 +                                &a->dt);
23658 +               if (rerr)
23659 +                       err = rerr;
23660 +       }
23661 +
23662 +out_unpin:
23663 +       au_unpin(&a->pin);
23664 +       dput(wh_dentry);
23665 +       dput(a->h_path.dentry);
23666 +out_parent:
23667 +       di_write_unlock(parent);
23668 +out_unlock:
23669 +       aufs_read_unlock(dentry, AuLock_DW);
23670 +out_free:
23671 +       au_kfree_rcu(a);
23672 +out:
23673 +       return err;
23674 +}
23675 +
23676 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23677 +{
23678 +       int err, rmdir_later;
23679 +       aufs_bindex_t bwh, bindex, btop;
23680 +       struct inode *inode;
23681 +       struct dentry *parent, *wh_dentry, *h_dentry;
23682 +       struct au_whtmp_rmdir *args;
23683 +       /* to reduce stack size */
23684 +       struct {
23685 +               struct au_dtime dt;
23686 +               struct au_pin pin;
23687 +       } *a;
23688 +
23689 +       IMustLock(dir);
23690 +
23691 +       err = -ENOMEM;
23692 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23693 +       if (unlikely(!a))
23694 +               goto out;
23695 +
23696 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23697 +       if (unlikely(err))
23698 +               goto out_free;
23699 +       err = au_alive_dir(dentry);
23700 +       if (unlikely(err))
23701 +               goto out_unlock;
23702 +       inode = d_inode(dentry);
23703 +       IMustLock(inode);
23704 +       err = -ENOTDIR;
23705 +       if (unlikely(!d_is_dir(dentry)))
23706 +               goto out_unlock; /* possible? */
23707 +
23708 +       err = -ENOMEM;
23709 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23710 +       if (unlikely(!args))
23711 +               goto out_unlock;
23712 +
23713 +       parent = dentry->d_parent; /* dir inode is locked */
23714 +       di_write_lock_parent(parent);
23715 +       err = au_test_empty(dentry, &args->whlist);
23716 +       if (unlikely(err))
23717 +               goto out_parent;
23718 +
23719 +       btop = au_dbtop(dentry);
23720 +       bwh = au_dbwh(dentry);
23721 +       bindex = -1;
23722 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23723 +                                       &a->pin);
23724 +       err = PTR_ERR(wh_dentry);
23725 +       if (IS_ERR(wh_dentry))
23726 +               goto out_parent;
23727 +
23728 +       h_dentry = au_h_dptr(dentry, btop);
23729 +       dget(h_dentry);
23730 +       rmdir_later = 0;
23731 +       if (bindex == btop) {
23732 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23733 +               if (err > 0) {
23734 +                       rmdir_later = err;
23735 +                       err = 0;
23736 +               }
23737 +       } else {
23738 +               /* stop monitoring */
23739 +               au_hn_free(au_hi(inode, btop));
23740 +
23741 +               /* dir inode is locked */
23742 +               IMustLock(d_inode(wh_dentry->d_parent));
23743 +               err = 0;
23744 +       }
23745 +
23746 +       if (!err) {
23747 +               vfsub_dead_dir(inode);
23748 +               au_set_dbdiropq(dentry, -1);
23749 +               epilog(dir, dentry, bindex);
23750 +
23751 +               if (rmdir_later) {
23752 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23753 +                       args = NULL;
23754 +               }
23755 +
23756 +               goto out_unpin; /* success */
23757 +       }
23758 +
23759 +       /* revert */
23760 +       AuLabel(revert);
23761 +       if (wh_dentry) {
23762 +               int rerr;
23763 +
23764 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23765 +                                &a->dt);
23766 +               if (rerr)
23767 +                       err = rerr;
23768 +       }
23769 +
23770 +out_unpin:
23771 +       au_unpin(&a->pin);
23772 +       dput(wh_dentry);
23773 +       dput(h_dentry);
23774 +out_parent:
23775 +       di_write_unlock(parent);
23776 +       if (args)
23777 +               au_whtmp_rmdir_free(args);
23778 +out_unlock:
23779 +       aufs_read_unlock(dentry, AuLock_DW);
23780 +out_free:
23781 +       au_kfree_rcu(a);
23782 +out:
23783 +       AuTraceErr(err);
23784 +       return err;
23785 +}
23786 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23787 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23788 +++ linux/fs/aufs/i_op_ren.c    2024-03-10 23:40:47.083143450 +0100
23789 @@ -0,0 +1,1264 @@
23790 +// SPDX-License-Identifier: GPL-2.0
23791 +/*
23792 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23793 + *
23794 + * This program is free software; you can redistribute it and/or modify
23795 + * it under the terms of the GNU General Public License as published by
23796 + * the Free Software Foundation; either version 2 of the License, or
23797 + * (at your option) any later version.
23798 + *
23799 + * This program is distributed in the hope that it will be useful,
23800 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23801 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23802 + * GNU General Public License for more details.
23803 + *
23804 + * You should have received a copy of the GNU General Public License
23805 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23806 + */
23807 +
23808 +/*
23809 + * inode operation (rename entry)
23810 + * todo: this is crazy monster
23811 + */
23812 +
23813 +#include <linux/iversion.h>
23814 +#include "aufs.h"
23815 +
23816 +enum { AuSRC, AuDST, AuSrcDst };
23817 +enum { AuPARENT, AuCHILD, AuParentChild };
23818 +
23819 +#define AuRen_ISDIR_SRC                BIT(0)
23820 +#define AuRen_ISDIR_DST                BIT(1)
23821 +#define AuRen_ISSAMEDIR                BIT(2)
23822 +#define AuRen_WHSRC            BIT(3)
23823 +#define AuRen_WHDST            BIT(4)
23824 +#define AuRen_MNT_WRITE                BIT(5)
23825 +#define AuRen_DT_DSTDIR                BIT(6)
23826 +#define AuRen_DIROPQ_SRC       BIT(7)
23827 +#define AuRen_DIROPQ_DST       BIT(8)
23828 +#define AuRen_DIRREN           BIT(9)
23829 +#define AuRen_DROPPED_SRC      BIT(10)
23830 +#define AuRen_DROPPED_DST      BIT(11)
23831 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23832 +#define au_fset_ren(flags, name) \
23833 +       do { (flags) |= AuRen_##name; } while (0)
23834 +#define au_fclr_ren(flags, name) \
23835 +       do { (flags) &= ~AuRen_##name; } while (0)
23836 +
23837 +#ifndef CONFIG_AUFS_DIRREN
23838 +#undef AuRen_DIRREN
23839 +#define AuRen_DIRREN           0
23840 +#endif
23841 +
23842 +struct au_ren_args {
23843 +       struct {
23844 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23845 +                       *wh_dentry;
23846 +               struct inode *dir, *inode;
23847 +               struct au_hinode *hdir, *hinode;
23848 +               struct au_dtime dt[AuParentChild];
23849 +               aufs_bindex_t btop, bdiropq;
23850 +       } sd[AuSrcDst];
23851 +
23852 +#define src_dentry     sd[AuSRC].dentry
23853 +#define src_dir                sd[AuSRC].dir
23854 +#define src_inode      sd[AuSRC].inode
23855 +#define src_h_dentry   sd[AuSRC].h_dentry
23856 +#define src_parent     sd[AuSRC].parent
23857 +#define src_h_parent   sd[AuSRC].h_parent
23858 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23859 +#define src_hdir       sd[AuSRC].hdir
23860 +#define src_hinode     sd[AuSRC].hinode
23861 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23862 +#define src_dt         sd[AuSRC].dt
23863 +#define src_btop       sd[AuSRC].btop
23864 +#define src_bdiropq    sd[AuSRC].bdiropq
23865 +
23866 +#define dst_dentry     sd[AuDST].dentry
23867 +#define dst_dir                sd[AuDST].dir
23868 +#define dst_inode      sd[AuDST].inode
23869 +#define dst_h_dentry   sd[AuDST].h_dentry
23870 +#define dst_parent     sd[AuDST].parent
23871 +#define dst_h_parent   sd[AuDST].h_parent
23872 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23873 +#define dst_hdir       sd[AuDST].hdir
23874 +#define dst_hinode     sd[AuDST].hinode
23875 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23876 +#define dst_dt         sd[AuDST].dt
23877 +#define dst_btop       sd[AuDST].btop
23878 +#define dst_bdiropq    sd[AuDST].bdiropq
23879 +
23880 +       struct dentry *h_trap;
23881 +       struct au_branch *br;
23882 +       struct path h_path;
23883 +       struct au_nhash whlist;
23884 +       aufs_bindex_t btgt, src_bwh;
23885 +
23886 +       struct {
23887 +               unsigned short auren_flags;
23888 +               unsigned char flags;    /* syscall parameter */
23889 +               unsigned char exchange;
23890 +       } __packed;
23891 +
23892 +       struct au_whtmp_rmdir *thargs;
23893 +       struct dentry *h_dst;
23894 +       struct au_hinode *h_root;
23895 +};
23896 +
23897 +/* ---------------------------------------------------------------------- */
23898 +
23899 +/*
23900 + * functions for reverting.
23901 + * when an error happened in a single rename systemcall, we should revert
23902 + * everything as if nothing happened.
23903 + * we don't need to revert the copied-up/down the parent dir since they are
23904 + * harmless.
23905 + */
23906 +
23907 +#define RevertFailure(fmt, ...) do { \
23908 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23909 +               ##__VA_ARGS__, err, rerr); \
23910 +       err = -EIO; \
23911 +} while (0)
23912 +
23913 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23914 +{
23915 +       int rerr;
23916 +       struct dentry *d;
23917 +#define src_or_dst(member) a->sd[idx].member
23918 +
23919 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23920 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23921 +       rerr = au_diropq_remove(d, a->btgt);
23922 +       au_hn_inode_unlock(src_or_dst(hinode));
23923 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23924 +       if (rerr)
23925 +               RevertFailure("remove diropq %pd", d);
23926 +
23927 +#undef src_or_dst_
23928 +}
23929 +
23930 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23931 +{
23932 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23933 +               au_ren_do_rev_diropq(err, a, AuSRC);
23934 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23935 +               au_ren_do_rev_diropq(err, a, AuDST);
23936 +}
23937 +
23938 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23939 +{
23940 +       int rerr;
23941 +       struct inode *delegated;
23942 +       struct path h_ppath = {
23943 +               .dentry = a->src_h_parent,
23944 +               .mnt    = a->h_path.mnt
23945 +       };
23946 +
23947 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23948 +       rerr = PTR_ERR(a->h_path.dentry);
23949 +       if (IS_ERR(a->h_path.dentry)) {
23950 +               RevertFailure("lkup one %pd", a->src_dentry);
23951 +               return;
23952 +       }
23953 +
23954 +       delegated = NULL;
23955 +       rerr = vfsub_rename(a->dst_h_dir,
23956 +                           au_h_dptr(a->src_dentry, a->btgt),
23957 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23958 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23959 +               pr_warn("cannot retry for NFSv4 delegation"
23960 +                       " for an internal rename\n");
23961 +               iput(delegated);
23962 +       }
23963 +       d_drop(a->h_path.dentry);
23964 +       dput(a->h_path.dentry);
23965 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23966 +       if (rerr)
23967 +               RevertFailure("rename %pd", a->src_dentry);
23968 +}
23969 +
23970 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23971 +{
23972 +       int rerr;
23973 +       struct inode *delegated;
23974 +       struct path h_ppath = {
23975 +               .dentry = a->dst_h_parent,
23976 +               .mnt    = a->h_path.mnt
23977 +       };
23978 +
23979 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23980 +       rerr = PTR_ERR(a->h_path.dentry);
23981 +       if (IS_ERR(a->h_path.dentry)) {
23982 +               RevertFailure("lkup one %pd", a->dst_dentry);
23983 +               return;
23984 +       }
23985 +       if (d_is_positive(a->h_path.dentry)) {
23986 +               d_drop(a->h_path.dentry);
23987 +               dput(a->h_path.dentry);
23988 +               return;
23989 +       }
23990 +
23991 +       delegated = NULL;
23992 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23993 +                           &delegated, a->flags);
23994 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23995 +               pr_warn("cannot retry for NFSv4 delegation"
23996 +                       " for an internal rename\n");
23997 +               iput(delegated);
23998 +       }
23999 +       d_drop(a->h_path.dentry);
24000 +       dput(a->h_path.dentry);
24001 +       if (!rerr)
24002 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
24003 +       else
24004 +               RevertFailure("rename %pd", a->h_dst);
24005 +}
24006 +
24007 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
24008 +{
24009 +       int rerr;
24010 +
24011 +       a->h_path.dentry = a->src_wh_dentry;
24012 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
24013 +       au_set_dbwh(a->src_dentry, a->src_bwh);
24014 +       if (rerr)
24015 +               RevertFailure("unlink %pd", a->src_wh_dentry);
24016 +}
24017 +#undef RevertFailure
24018 +
24019 +/* ---------------------------------------------------------------------- */
24020 +
24021 +/*
24022 + * when we have to copyup the renaming entry, do it with the rename-target name
24023 + * in order to minimize the cost (the later actual rename is unnecessary).
24024 + * otherwise rename it on the target branch.
24025 + */
24026 +static int au_ren_or_cpup(struct au_ren_args *a)
24027 +{
24028 +       int err;
24029 +       struct dentry *d;
24030 +       struct inode *delegated;
24031 +
24032 +       d = a->src_dentry;
24033 +       if (au_dbtop(d) == a->btgt) {
24034 +               a->h_path.dentry = a->dst_h_dentry;
24035 +               AuDebugOn(au_dbtop(d) != a->btgt);
24036 +               delegated = NULL;
24037 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24038 +                                  a->dst_h_dir, &a->h_path, &delegated,
24039 +                                  a->flags);
24040 +               if (unlikely(err == -EWOULDBLOCK)) {
24041 +                       pr_warn("cannot retry for NFSv4 delegation"
24042 +                               " for an internal rename\n");
24043 +                       iput(delegated);
24044 +               }
24045 +       } else
24046 +               BUG();
24047 +
24048 +       if (!err && a->h_dst)
24049 +               /* it will be set to dinfo later */
24050 +               dget(a->h_dst);
24051 +
24052 +       return err;
24053 +}
24054 +
24055 +/* cf. aufs_rmdir() */
24056 +static int au_ren_del_whtmp(struct au_ren_args *a)
24057 +{
24058 +       int err;
24059 +       struct inode *dir;
24060 +
24061 +       dir = a->dst_dir;
24062 +       SiMustAnyLock(dir->i_sb);
24063 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24064 +                                    au_sbi(dir->i_sb)->si_dirwh)
24065 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24066 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24067 +               if (unlikely(err))
24068 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24069 +                               "ignored.\n", a->h_dst, err);
24070 +       } else {
24071 +               au_nhash_wh_free(&a->thargs->whlist);
24072 +               a->thargs->whlist = a->whlist;
24073 +               a->whlist.nh_num = 0;
24074 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24075 +               dput(a->h_dst);
24076 +               a->thargs = NULL;
24077 +       }
24078 +
24079 +       return 0;
24080 +}
24081 +
24082 +/* make it 'opaque' dir. */
24083 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24084 +{
24085 +       int err;
24086 +       struct dentry *d, *diropq;
24087 +#define src_or_dst(member) a->sd[idx].member
24088 +
24089 +       err = 0;
24090 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24091 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24092 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24093 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24094 +       diropq = au_diropq_create(d, a->btgt);
24095 +       au_hn_inode_unlock(src_or_dst(hinode));
24096 +       if (IS_ERR(diropq))
24097 +               err = PTR_ERR(diropq);
24098 +       else
24099 +               dput(diropq);
24100 +
24101 +#undef src_or_dst_
24102 +       return err;
24103 +}
24104 +
24105 +static int au_ren_diropq(struct au_ren_args *a)
24106 +{
24107 +       int err;
24108 +       unsigned char always;
24109 +       struct dentry *d;
24110 +
24111 +       err = 0;
24112 +       d = a->dst_dentry; /* already renamed on the branch */
24113 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24114 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24115 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24116 +           && a->btgt != au_dbdiropq(a->src_dentry)
24117 +           && (a->dst_wh_dentry
24118 +               || a->btgt <= au_dbdiropq(d)
24119 +               /* hide the lower to keep xino */
24120 +               /* the lowers may not be a dir, but we hide them anyway */
24121 +               || a->btgt < au_dbbot(d)
24122 +               || always)) {
24123 +               AuDbg("here\n");
24124 +               err = au_ren_do_diropq(a, AuSRC);
24125 +               if (unlikely(err))
24126 +                       goto out;
24127 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24128 +       }
24129 +       if (!a->exchange)
24130 +               goto out; /* success */
24131 +
24132 +       d = a->src_dentry; /* already renamed on the branch */
24133 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24134 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24135 +           && (a->btgt < au_dbdiropq(d)
24136 +               || a->btgt < au_dbbot(d)
24137 +               || always)) {
24138 +               AuDbgDentry(a->src_dentry);
24139 +               AuDbgDentry(a->dst_dentry);
24140 +               err = au_ren_do_diropq(a, AuDST);
24141 +               if (unlikely(err))
24142 +                       goto out_rev_src;
24143 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24144 +       }
24145 +       goto out; /* success */
24146 +
24147 +out_rev_src:
24148 +       AuDbg("err %d, reverting src\n", err);
24149 +       au_ren_rev_diropq(err, a);
24150 +out:
24151 +       return err;
24152 +}
24153 +
24154 +static int do_rename(struct au_ren_args *a)
24155 +{
24156 +       int err;
24157 +       struct dentry *d, *h_d;
24158 +       struct inode *h_inode;
24159 +
24160 +       if (!a->exchange) {
24161 +               /* prepare workqueue args for asynchronous rmdir */
24162 +               h_d = a->dst_h_dentry;
24163 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24164 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24165 +                   && d_is_positive(h_d)) {
24166 +                       err = -ENOMEM;
24167 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24168 +                                                        GFP_NOFS);
24169 +                       if (unlikely(!a->thargs))
24170 +                               goto out;
24171 +                       a->h_dst = dget(h_d);
24172 +               }
24173 +
24174 +               /* create whiteout for src_dentry */
24175 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24176 +                       a->src_bwh = au_dbwh(a->src_dentry);
24177 +                       AuDebugOn(a->src_bwh >= 0);
24178 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24179 +                                                       a->src_h_parent);
24180 +                       err = PTR_ERR(a->src_wh_dentry);
24181 +                       if (IS_ERR(a->src_wh_dentry))
24182 +                               goto out_thargs;
24183 +               }
24184 +
24185 +               /* lookup whiteout for dentry */
24186 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24187 +                       h_d = au_wh_lkup(a->dst_h_parent,
24188 +                                        &a->dst_dentry->d_name, a->br);
24189 +                       err = PTR_ERR(h_d);
24190 +                       if (IS_ERR(h_d))
24191 +                               goto out_whsrc;
24192 +                       if (d_is_negative(h_d))
24193 +                               dput(h_d);
24194 +                       else
24195 +                               a->dst_wh_dentry = h_d;
24196 +               }
24197 +
24198 +               /* rename dentry to tmpwh */
24199 +               if (a->thargs) {
24200 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24201 +                       if (unlikely(err))
24202 +                               goto out_whdst;
24203 +
24204 +                       d = a->dst_dentry;
24205 +                       au_set_h_dptr(d, a->btgt, NULL);
24206 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24207 +                       if (unlikely(err))
24208 +                               goto out_whtmp;
24209 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24210 +               }
24211 +       }
24212 +
24213 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24214 +#if 0 /* debugging */
24215 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24216 +              && d_is_positive(a->dst_h_dentry)
24217 +              && a->src_btop != a->btgt);
24218 +#endif
24219 +
24220 +       /* rename by vfs_rename or cpup */
24221 +       err = au_ren_or_cpup(a);
24222 +       if (unlikely(err))
24223 +               /* leave the copied-up one */
24224 +               goto out_whtmp;
24225 +
24226 +       /* make dir opaque */
24227 +       err = au_ren_diropq(a);
24228 +       if (unlikely(err))
24229 +               goto out_rename;
24230 +
24231 +       /* update target timestamps */
24232 +       if (a->exchange) {
24233 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24234 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24235 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24236 +               h_inode = d_inode(a->h_path.dentry);
24237 +               inode_set_ctime_to_ts(a->dst_inode, inode_get_ctime(h_inode));
24238 +       }
24239 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24240 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24241 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24242 +       h_inode = d_inode(a->h_path.dentry);
24243 +       inode_set_ctime_to_ts(a->src_inode, inode_get_ctime(h_inode));
24244 +
24245 +       if (!a->exchange) {
24246 +               /* remove whiteout for dentry */
24247 +               if (a->dst_wh_dentry) {
24248 +                       a->h_path.dentry = a->dst_wh_dentry;
24249 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24250 +                                                 a->dst_dentry);
24251 +                       if (unlikely(err))
24252 +                               goto out_diropq;
24253 +               }
24254 +
24255 +               /* remove whtmp */
24256 +               if (a->thargs)
24257 +                       au_ren_del_whtmp(a); /* ignore this error */
24258 +
24259 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24260 +       }
24261 +       err = 0;
24262 +       goto out_success;
24263 +
24264 +out_diropq:
24265 +       au_ren_rev_diropq(err, a);
24266 +out_rename:
24267 +       au_ren_rev_rename(err, a);
24268 +       dput(a->h_dst);
24269 +out_whtmp:
24270 +       if (a->thargs)
24271 +               au_ren_rev_whtmp(err, a);
24272 +out_whdst:
24273 +       dput(a->dst_wh_dentry);
24274 +       a->dst_wh_dentry = NULL;
24275 +out_whsrc:
24276 +       if (a->src_wh_dentry)
24277 +               au_ren_rev_whsrc(err, a);
24278 +out_success:
24279 +       dput(a->src_wh_dentry);
24280 +       dput(a->dst_wh_dentry);
24281 +out_thargs:
24282 +       if (a->thargs) {
24283 +               dput(a->h_dst);
24284 +               au_whtmp_rmdir_free(a->thargs);
24285 +               a->thargs = NULL;
24286 +       }
24287 +out:
24288 +       return err;
24289 +}
24290 +
24291 +/* ---------------------------------------------------------------------- */
24292 +
24293 +/*
24294 + * test if @dentry dir can be rename destination or not.
24295 + * success means, it is a logically empty dir.
24296 + */
24297 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24298 +{
24299 +       return au_test_empty(dentry, whlist);
24300 +}
24301 +
24302 +/*
24303 + * test if @a->src_dentry dir can be rename source or not.
24304 + * if it can, return 0.
24305 + * success means,
24306 + * - it is a logically empty dir.
24307 + * - or, it exists on writable branch and has no children including whiteouts
24308 + *   on the lower branch unless DIRREN is on.
24309 + */
24310 +static int may_rename_srcdir(struct au_ren_args *a)
24311 +{
24312 +       int err;
24313 +       unsigned int rdhash;
24314 +       aufs_bindex_t btop, btgt;
24315 +       struct dentry *dentry;
24316 +       struct super_block *sb;
24317 +       struct au_sbinfo *sbinfo;
24318 +
24319 +       dentry = a->src_dentry;
24320 +       sb = dentry->d_sb;
24321 +       sbinfo = au_sbi(sb);
24322 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24323 +               au_fset_ren(a->auren_flags, DIRREN);
24324 +
24325 +       btgt = a->btgt;
24326 +       btop = au_dbtop(dentry);
24327 +       if (btop != btgt) {
24328 +               struct au_nhash whlist;
24329 +
24330 +               SiMustAnyLock(sb);
24331 +               rdhash = sbinfo->si_rdhash;
24332 +               if (!rdhash)
24333 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24334 +                                                          dentry));
24335 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24336 +               if (unlikely(err))
24337 +                       goto out;
24338 +               err = au_test_empty(dentry, &whlist);
24339 +               au_nhash_wh_free(&whlist);
24340 +               goto out;
24341 +       }
24342 +
24343 +       if (btop == au_dbtaildir(dentry))
24344 +               return 0; /* success */
24345 +
24346 +       err = au_test_empty_lower(dentry);
24347 +
24348 +out:
24349 +       if (err == -ENOTEMPTY) {
24350 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24351 +                       err = 0;
24352 +               } else {
24353 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24354 +                               "branches, is not supported\n");
24355 +                       err = -EXDEV;
24356 +               }
24357 +       }
24358 +       return err;
24359 +}
24360 +
24361 +/* side effect: sets whlist and h_dentry */
24362 +static int au_ren_may_dir(struct au_ren_args *a)
24363 +{
24364 +       int err;
24365 +       unsigned int rdhash;
24366 +       struct dentry *d;
24367 +
24368 +       d = a->dst_dentry;
24369 +       SiMustAnyLock(d->d_sb);
24370 +
24371 +       err = 0;
24372 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24373 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24374 +               if (!rdhash)
24375 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24376 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24377 +               if (unlikely(err))
24378 +                       goto out;
24379 +
24380 +               if (!a->exchange) {
24381 +                       au_set_dbtop(d, a->dst_btop);
24382 +                       err = may_rename_dstdir(d, &a->whlist);
24383 +                       au_set_dbtop(d, a->btgt);
24384 +               } else
24385 +                       err = may_rename_srcdir(a);
24386 +       }
24387 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24388 +       if (unlikely(err))
24389 +               goto out;
24390 +
24391 +       d = a->src_dentry;
24392 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24393 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24394 +               err = may_rename_srcdir(a);
24395 +               if (unlikely(err)) {
24396 +                       au_nhash_wh_free(&a->whlist);
24397 +                       a->whlist.nh_num = 0;
24398 +               }
24399 +       }
24400 +out:
24401 +       return err;
24402 +}
24403 +
24404 +/* ---------------------------------------------------------------------- */
24405 +
24406 +/*
24407 + * simple tests for rename.
24408 + * following the checks in vfs, plus the parent-child relationship.
24409 + */
24410 +static int au_may_ren(struct au_ren_args *a)
24411 +{
24412 +       int err, isdir;
24413 +       struct inode *h_inode;
24414 +
24415 +       if (a->src_btop == a->btgt) {
24416 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24417 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24418 +               if (unlikely(err))
24419 +                       goto out;
24420 +               err = -EINVAL;
24421 +               if (unlikely(a->src_h_dentry == a->h_trap))
24422 +                       goto out;
24423 +       }
24424 +
24425 +       err = 0;
24426 +       if (a->dst_btop != a->btgt)
24427 +               goto out;
24428 +
24429 +       err = -ENOTEMPTY;
24430 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24431 +               goto out;
24432 +
24433 +       err = -EIO;
24434 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24435 +       if (d_really_is_negative(a->dst_dentry)) {
24436 +               if (d_is_negative(a->dst_h_dentry))
24437 +                       err = au_may_add(a->dst_dentry, a->btgt,
24438 +                                        a->dst_h_parent, isdir);
24439 +       } else {
24440 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24441 +                       goto out;
24442 +               h_inode = d_inode(a->dst_h_dentry);
24443 +               if (h_inode->i_nlink)
24444 +                       err = au_may_del(a->dst_dentry, a->btgt,
24445 +                                        a->dst_h_parent, isdir);
24446 +       }
24447 +
24448 +out:
24449 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24450 +               err = -EIO;
24451 +       AuTraceErr(err);
24452 +       return err;
24453 +}
24454 +
24455 +/* ---------------------------------------------------------------------- */
24456 +
24457 +/*
24458 + * locking order
24459 + * (VFS)
24460 + * - src_dir and dir by lock_rename()
24461 + * - inode if exists
24462 + * (aufs)
24463 + * - lock all
24464 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24465 + *     + si_read_lock
24466 + *     + di_write_lock2_child()
24467 + *       + di_write_lock_child()
24468 + *        + ii_write_lock_child()
24469 + *       + di_write_lock_child2()
24470 + *        + ii_write_lock_child2()
24471 + *     + src_parent and parent
24472 + *       + di_write_lock_parent()
24473 + *        + ii_write_lock_parent()
24474 + *       + di_write_lock_parent2()
24475 + *        + ii_write_lock_parent2()
24476 + *   + lower src_dir and dir by vfsub_lock_rename()
24477 + *   + verify the every relationships between child and parent. if any
24478 + *     of them failed, unlock all and return -EBUSY.
24479 + */
24480 +static void au_ren_unlock(struct au_ren_args *a)
24481 +{
24482 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24483 +                           a->dst_h_parent, a->dst_hdir);
24484 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24485 +           && a->h_root)
24486 +               au_hn_inode_unlock(a->h_root);
24487 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24488 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24489 +}
24490 +
24491 +static int au_ren_lock(struct au_ren_args *a)
24492 +{
24493 +       int err;
24494 +       unsigned int udba;
24495 +
24496 +       err = 0;
24497 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24498 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24499 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24500 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24501 +
24502 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24503 +       if (unlikely(err))
24504 +               goto out;
24505 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24506 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24507 +               struct dentry *root;
24508 +               struct inode *dir;
24509 +
24510 +               /*
24511 +                * sbinfo is already locked, so this ii_read_lock is
24512 +                * unnecessary. but our debugging feature checks it.
24513 +                */
24514 +               root = a->src_inode->i_sb->s_root;
24515 +               if (root != a->src_parent && root != a->dst_parent) {
24516 +                       dir = d_inode(root);
24517 +                       ii_read_lock_parent3(dir);
24518 +                       a->h_root = au_hi(dir, a->btgt);
24519 +                       ii_read_unlock(dir);
24520 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24521 +               }
24522 +       }
24523 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24524 +                                     a->dst_h_parent, a->dst_hdir);
24525 +       if (IS_ERR(a->h_trap)) {
24526 +               err = PTR_ERR(a->h_trap);
24527 +               goto out;
24528 +       }
24529 +       udba = au_opt_udba(a->src_dentry->d_sb);
24530 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24531 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24532 +               err = au_busy_or_stale();
24533 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24534 +               err = au_h_verify(a->src_h_dentry, udba,
24535 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24536 +                                 a->br);
24537 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24538 +               err = au_h_verify(a->dst_h_dentry, udba,
24539 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24540 +                                 a->br);
24541 +       if (!err)
24542 +               goto out; /* success */
24543 +
24544 +       err = au_busy_or_stale();
24545 +       au_ren_unlock(a);
24546 +
24547 +out:
24548 +       return err;
24549 +}
24550 +
24551 +/* ---------------------------------------------------------------------- */
24552 +
24553 +static void au_ren_refresh_dir(struct au_ren_args *a)
24554 +{
24555 +       struct inode *dir;
24556 +
24557 +       dir = a->dst_dir;
24558 +       inode_inc_iversion(dir);
24559 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24560 +               /* is this updating defined in POSIX? */
24561 +               au_cpup_attr_timesizes(a->src_inode);
24562 +               au_cpup_attr_nlink(dir, /*force*/1);
24563 +       }
24564 +       au_dir_ts(dir, a->btgt);
24565 +
24566 +       if (a->exchange) {
24567 +               dir = a->src_dir;
24568 +               inode_inc_iversion(dir);
24569 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24570 +                       /* is this updating defined in POSIX? */
24571 +                       au_cpup_attr_timesizes(a->dst_inode);
24572 +                       au_cpup_attr_nlink(dir, /*force*/1);
24573 +               }
24574 +               au_dir_ts(dir, a->btgt);
24575 +       }
24576 +
24577 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24578 +               return;
24579 +
24580 +       dir = a->src_dir;
24581 +       inode_inc_iversion(dir);
24582 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24583 +               au_cpup_attr_nlink(dir, /*force*/1);
24584 +       au_dir_ts(dir, a->btgt);
24585 +}
24586 +
24587 +static void au_ren_refresh(struct au_ren_args *a)
24588 +{
24589 +       aufs_bindex_t bbot, bindex;
24590 +       struct dentry *d, *h_d;
24591 +       struct inode *i, *h_i;
24592 +       struct super_block *sb;
24593 +
24594 +       d = a->dst_dentry;
24595 +       d_drop(d);
24596 +       if (a->h_dst)
24597 +               /* already dget-ed by au_ren_or_cpup() */
24598 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24599 +
24600 +       i = a->dst_inode;
24601 +       if (i) {
24602 +               if (!a->exchange) {
24603 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24604 +                               vfsub_drop_nlink(i);
24605 +                       else {
24606 +                               vfsub_dead_dir(i);
24607 +                               au_cpup_attr_timesizes(i);
24608 +                       }
24609 +                       au_update_dbrange(d, /*do_put_zero*/1);
24610 +               } else
24611 +                       au_cpup_attr_nlink(i, /*force*/1);
24612 +       } else {
24613 +               bbot = a->btgt;
24614 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24615 +                       au_set_h_dptr(d, bindex, NULL);
24616 +               bbot = au_dbbot(d);
24617 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24618 +                       au_set_h_dptr(d, bindex, NULL);
24619 +               au_update_dbrange(d, /*do_put_zero*/0);
24620 +       }
24621 +
24622 +       if (a->exchange
24623 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24624 +               d_drop(a->src_dentry);
24625 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24626 +                       au_set_dbwh(a->src_dentry, -1);
24627 +               return;
24628 +       }
24629 +
24630 +       d = a->src_dentry;
24631 +       au_set_dbwh(d, -1);
24632 +       bbot = au_dbbot(d);
24633 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24634 +               h_d = au_h_dptr(d, bindex);
24635 +               if (h_d)
24636 +                       au_set_h_dptr(d, bindex, NULL);
24637 +       }
24638 +       au_set_dbbot(d, a->btgt);
24639 +
24640 +       sb = d->d_sb;
24641 +       i = a->src_inode;
24642 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24643 +               return; /* success */
24644 +
24645 +       bbot = au_ibbot(i);
24646 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24647 +               h_i = au_h_iptr(i, bindex);
24648 +               if (h_i) {
24649 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24650 +                       /* ignore this error */
24651 +                       au_set_h_iptr(i, bindex, NULL, 0);
24652 +               }
24653 +       }
24654 +       au_set_ibbot(i, a->btgt);
24655 +}
24656 +
24657 +/* ---------------------------------------------------------------------- */
24658 +
24659 +/* mainly for link(2) and rename(2) */
24660 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24661 +{
24662 +       aufs_bindex_t bdiropq, bwh;
24663 +       struct dentry *parent;
24664 +       struct au_branch *br;
24665 +
24666 +       parent = dentry->d_parent;
24667 +       IMustLock(d_inode(parent)); /* dir is locked */
24668 +
24669 +       bdiropq = au_dbdiropq(parent);
24670 +       bwh = au_dbwh(dentry);
24671 +       br = au_sbr(dentry->d_sb, btgt);
24672 +       if (au_br_rdonly(br)
24673 +           || (0 <= bdiropq && bdiropq < btgt)
24674 +           || (0 <= bwh && bwh < btgt))
24675 +               btgt = -1;
24676 +
24677 +       AuDbg("btgt %d\n", btgt);
24678 +       return btgt;
24679 +}
24680 +
24681 +/* sets src_btop, dst_btop and btgt */
24682 +static int au_ren_wbr(struct au_ren_args *a)
24683 +{
24684 +       int err;
24685 +       struct au_wr_dir_args wr_dir_args = {
24686 +               /* .force_btgt  = -1, */
24687 +               .flags          = AuWrDir_ADD_ENTRY
24688 +       };
24689 +
24690 +       a->src_btop = au_dbtop(a->src_dentry);
24691 +       a->dst_btop = au_dbtop(a->dst_dentry);
24692 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24693 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24694 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24695 +       wr_dir_args.force_btgt = a->src_btop;
24696 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24697 +               wr_dir_args.force_btgt = a->dst_btop;
24698 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24699 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24700 +       a->btgt = err;
24701 +       if (a->exchange)
24702 +               au_update_dbtop(a->dst_dentry);
24703 +
24704 +       return err;
24705 +}
24706 +
24707 +static void au_ren_dt(struct au_ren_args *a)
24708 +{
24709 +       a->h_path.dentry = a->src_h_parent;
24710 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24711 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24712 +               a->h_path.dentry = a->dst_h_parent;
24713 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24714 +       }
24715 +
24716 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24717 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24718 +           && !a->exchange)
24719 +               return;
24720 +
24721 +       a->h_path.dentry = a->src_h_dentry;
24722 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24723 +       if (d_is_positive(a->dst_h_dentry)) {
24724 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24725 +               a->h_path.dentry = a->dst_h_dentry;
24726 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24727 +       }
24728 +}
24729 +
24730 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24731 +{
24732 +       struct dentry *h_d;
24733 +       struct inode *h_inode;
24734 +
24735 +       au_dtime_revert(a->src_dt + AuPARENT);
24736 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24737 +               au_dtime_revert(a->dst_dt + AuPARENT);
24738 +
24739 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24740 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24741 +               h_inode = d_inode(h_d);
24742 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24743 +               au_dtime_revert(a->src_dt + AuCHILD);
24744 +               inode_unlock(h_inode);
24745 +
24746 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24747 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24748 +                       h_inode = d_inode(h_d);
24749 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24750 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24751 +                       inode_unlock(h_inode);
24752 +               }
24753 +       }
24754 +}
24755 +
24756 +/* ---------------------------------------------------------------------- */
24757 +
24758 +int aufs_rename(struct mnt_idmap *idmap,
24759 +               struct inode *_src_dir, struct dentry *_src_dentry,
24760 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24761 +               unsigned int _flags)
24762 +{
24763 +       int err, lock_flags;
24764 +       void *rev;
24765 +       /* reduce stack space */
24766 +       struct au_ren_args *a;
24767 +       struct au_pin pin;
24768 +
24769 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24770 +       IMustLock(_src_dir);
24771 +       IMustLock(_dst_dir);
24772 +
24773 +       err = -EINVAL;
24774 +       if (unlikely(_flags & RENAME_WHITEOUT))
24775 +               goto out;
24776 +
24777 +       err = -ENOMEM;
24778 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24779 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24780 +       if (unlikely(!a))
24781 +               goto out;
24782 +
24783 +       a->flags = _flags;
24784 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24785 +                    && RENAME_EXCHANGE > U8_MAX);
24786 +       a->exchange = _flags & RENAME_EXCHANGE;
24787 +       a->src_dir = _src_dir;
24788 +       a->src_dentry = _src_dentry;
24789 +       a->src_inode = NULL;
24790 +       if (d_really_is_positive(a->src_dentry))
24791 +               a->src_inode = d_inode(a->src_dentry);
24792 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24793 +       a->dst_dir = _dst_dir;
24794 +       a->dst_dentry = _dst_dentry;
24795 +       a->dst_inode = NULL;
24796 +       if (d_really_is_positive(a->dst_dentry))
24797 +               a->dst_inode = d_inode(a->dst_dentry);
24798 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24799 +       if (a->dst_inode) {
24800 +               /*
24801 +                * if EXCHANGE && src is non-dir && dst is dir,
24802 +                * dst is not locked.
24803 +                */
24804 +               /* IMustLock(a->dst_inode); */
24805 +               au_igrab(a->dst_inode);
24806 +       }
24807 +
24808 +       err = -ENOTDIR;
24809 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24810 +       if (d_is_dir(a->src_dentry)) {
24811 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24812 +               if (unlikely(!a->exchange
24813 +                            && d_really_is_positive(a->dst_dentry)
24814 +                            && !d_is_dir(a->dst_dentry)))
24815 +                       goto out_free;
24816 +               lock_flags |= AuLock_DIRS;
24817 +       }
24818 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24819 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24820 +               if (unlikely(!a->exchange
24821 +                            && d_really_is_positive(a->src_dentry)
24822 +                            && !d_is_dir(a->src_dentry)))
24823 +                       goto out_free;
24824 +               lock_flags |= AuLock_DIRS;
24825 +       }
24826 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24827 +                                       lock_flags);
24828 +       if (unlikely(err))
24829 +               goto out_free;
24830 +
24831 +       err = au_d_hashed_positive(a->src_dentry);
24832 +       if (unlikely(err))
24833 +               goto out_unlock;
24834 +       err = -ENOENT;
24835 +       if (a->dst_inode) {
24836 +               /*
24837 +                * If it is a dir, VFS unhash it before this
24838 +                * function. It means we cannot rely upon d_unhashed().
24839 +                */
24840 +               if (unlikely(!a->dst_inode->i_nlink))
24841 +                       goto out_unlock;
24842 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24843 +                       err = au_d_hashed_positive(a->dst_dentry);
24844 +                       if (unlikely(err && !a->exchange))
24845 +                               goto out_unlock;
24846 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24847 +                       goto out_unlock;
24848 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24849 +               goto out_unlock;
24850 +
24851 +       /*
24852 +        * is it possible?
24853 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24854 +        * there may exist a problem somewhere else.
24855 +        */
24856 +       err = -EINVAL;
24857 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24858 +               goto out_unlock;
24859 +
24860 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24861 +       di_write_lock_parent(a->dst_parent);
24862 +
24863 +       /* which branch we process */
24864 +       err = au_ren_wbr(a);
24865 +       if (unlikely(err < 0))
24866 +               goto out_parent;
24867 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24868 +       a->h_path.mnt = au_br_mnt(a->br);
24869 +
24870 +       /* are they available to be renamed */
24871 +       err = au_ren_may_dir(a);
24872 +       if (unlikely(err))
24873 +               goto out_children;
24874 +
24875 +       /* prepare the writable parent dir on the same branch */
24876 +       if (a->dst_btop == a->btgt) {
24877 +               au_fset_ren(a->auren_flags, WHDST);
24878 +       } else {
24879 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24880 +               if (unlikely(err))
24881 +                       goto out_children;
24882 +       }
24883 +
24884 +       err = 0;
24885 +       if (!a->exchange) {
24886 +               if (a->src_dir != a->dst_dir) {
24887 +                       /*
24888 +                        * this temporary unlock is safe,
24889 +                        * because both dir->i_mutex are locked.
24890 +                        */
24891 +                       di_write_unlock(a->dst_parent);
24892 +                       di_write_lock_parent(a->src_parent);
24893 +                       err = au_wr_dir_need_wh(a->src_dentry,
24894 +                                               au_ftest_ren(a->auren_flags,
24895 +                                                            ISDIR_SRC),
24896 +                                               &a->btgt);
24897 +                       di_write_unlock(a->src_parent);
24898 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24899 +                                             /*isdir*/1);
24900 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24901 +               } else
24902 +                       err = au_wr_dir_need_wh(a->src_dentry,
24903 +                                               au_ftest_ren(a->auren_flags,
24904 +                                                            ISDIR_SRC),
24905 +                                               &a->btgt);
24906 +       }
24907 +       if (unlikely(err < 0))
24908 +               goto out_children;
24909 +       if (err)
24910 +               au_fset_ren(a->auren_flags, WHSRC);
24911 +
24912 +       /* cpup src */
24913 +       if (a->src_btop != a->btgt) {
24914 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24915 +                            au_opt_udba(a->src_dentry->d_sb),
24916 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24917 +               if (!err) {
24918 +                       struct au_cp_generic cpg = {
24919 +                               .dentry = a->src_dentry,
24920 +                               .bdst   = a->btgt,
24921 +                               .bsrc   = a->src_btop,
24922 +                               .len    = -1,
24923 +                               .pin    = &pin,
24924 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24925 +                       };
24926 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24927 +                       err = au_sio_cpup_simple(&cpg);
24928 +                       au_unpin(&pin);
24929 +               }
24930 +               if (unlikely(err))
24931 +                       goto out_children;
24932 +               a->src_btop = a->btgt;
24933 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24934 +               if (!a->exchange)
24935 +                       au_fset_ren(a->auren_flags, WHSRC);
24936 +       }
24937 +
24938 +       /* cpup dst */
24939 +       if (a->exchange && a->dst_inode
24940 +           && a->dst_btop != a->btgt) {
24941 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24942 +                            au_opt_udba(a->dst_dentry->d_sb),
24943 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24944 +               if (!err) {
24945 +                       struct au_cp_generic cpg = {
24946 +                               .dentry = a->dst_dentry,
24947 +                               .bdst   = a->btgt,
24948 +                               .bsrc   = a->dst_btop,
24949 +                               .len    = -1,
24950 +                               .pin    = &pin,
24951 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24952 +                       };
24953 +                       err = au_sio_cpup_simple(&cpg);
24954 +                       au_unpin(&pin);
24955 +               }
24956 +               if (unlikely(err))
24957 +                       goto out_children;
24958 +               a->dst_btop = a->btgt;
24959 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24960 +       }
24961 +
24962 +       /* lock them all */
24963 +       err = au_ren_lock(a);
24964 +       if (unlikely(err))
24965 +               /* leave the copied-up one */
24966 +               goto out_children;
24967 +
24968 +       if (!a->exchange) {
24969 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24970 +                       err = au_may_ren(a);
24971 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24972 +                       err = -ENAMETOOLONG;
24973 +               if (unlikely(err))
24974 +                       goto out_hdir;
24975 +       }
24976 +
24977 +       /* store timestamps to be revertible */
24978 +       au_ren_dt(a);
24979 +
24980 +       /* store dirren info */
24981 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24982 +               err = au_dr_rename(a->src_dentry, a->btgt,
24983 +                                  &a->dst_dentry->d_name, &rev);
24984 +               AuTraceErr(err);
24985 +               if (unlikely(err))
24986 +                       goto out_dt;
24987 +       }
24988 +
24989 +       /* here we go */
24990 +       err = do_rename(a);
24991 +       if (unlikely(err))
24992 +               goto out_dirren;
24993 +
24994 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24995 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24996 +
24997 +       /* update dir attributes */
24998 +       au_ren_refresh_dir(a);
24999 +
25000 +       /* dput/iput all lower dentries */
25001 +       au_ren_refresh(a);
25002 +
25003 +       goto out_hdir; /* success */
25004 +
25005 +out_dirren:
25006 +       if (au_ftest_ren(a->auren_flags, DIRREN))
25007 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
25008 +out_dt:
25009 +       au_ren_rev_dt(err, a);
25010 +out_hdir:
25011 +       au_ren_unlock(a);
25012 +out_children:
25013 +       au_nhash_wh_free(&a->whlist);
25014 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
25015 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
25016 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
25017 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
25018 +       }
25019 +out_parent:
25020 +       if (!err) {
25021 +               if (d_unhashed(a->src_dentry))
25022 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
25023 +               if (d_unhashed(a->dst_dentry))
25024 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
25025 +               if (!a->exchange)
25026 +                       d_move(a->src_dentry, a->dst_dentry);
25027 +               else {
25028 +                       d_exchange(a->src_dentry, a->dst_dentry);
25029 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25030 +                               d_drop(a->dst_dentry);
25031 +               }
25032 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25033 +                       d_drop(a->src_dentry);
25034 +       } else {
25035 +               au_update_dbtop(a->dst_dentry);
25036 +               if (!a->dst_inode)
25037 +                       d_drop(a->dst_dentry);
25038 +       }
25039 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25040 +               di_write_unlock(a->dst_parent);
25041 +       else
25042 +               di_write_unlock2(a->src_parent, a->dst_parent);
25043 +out_unlock:
25044 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25045 +out_free:
25046 +       iput(a->dst_inode);
25047 +       if (a->thargs)
25048 +               au_whtmp_rmdir_free(a->thargs);
25049 +       au_kfree_rcu(a);
25050 +out:
25051 +       AuTraceErr(err);
25052 +       return err;
25053 +}
25054 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25055 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25056 +++ linux/fs/aufs/Kconfig       2022-11-05 23:02:18.959222617 +0100
25057 @@ -0,0 +1,199 @@
25058 +# SPDX-License-Identifier: GPL-2.0
25059 +config AUFS_FS
25060 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25061 +       help
25062 +       Aufs is a stackable unification filesystem such as Unionfs,
25063 +       which unifies several directories and provides a merged single
25064 +       directory.
25065 +       In the early days, aufs was entirely re-designed and
25066 +       re-implemented Unionfs Version 1.x series. Introducing many
25067 +       original ideas, approaches and improvements, it becomes totally
25068 +       different from Unionfs while keeping the basic features.
25069 +
25070 +if AUFS_FS
25071 +choice
25072 +       prompt "Maximum number of branches"
25073 +       default AUFS_BRANCH_MAX_127
25074 +       help
25075 +       Specifies the maximum number of branches (or member directories)
25076 +       in a single aufs. The larger value consumes more system
25077 +       resources and has a minor impact to performance.
25078 +config AUFS_BRANCH_MAX_127
25079 +       bool "127"
25080 +       help
25081 +       Specifies the maximum number of branches (or member directories)
25082 +       in a single aufs. The larger value consumes more system
25083 +       resources and has a minor impact to performance.
25084 +config AUFS_BRANCH_MAX_511
25085 +       bool "511"
25086 +       help
25087 +       Specifies the maximum number of branches (or member directories)
25088 +       in a single aufs. The larger value consumes more system
25089 +       resources and has a minor impact to performance.
25090 +config AUFS_BRANCH_MAX_1023
25091 +       bool "1023"
25092 +       help
25093 +       Specifies the maximum number of branches (or member directories)
25094 +       in a single aufs. The larger value consumes more system
25095 +       resources and has a minor impact to performance.
25096 +config AUFS_BRANCH_MAX_32767
25097 +       bool "32767"
25098 +       help
25099 +       Specifies the maximum number of branches (or member directories)
25100 +       in a single aufs. The larger value consumes more system
25101 +       resources and has a minor impact to performance.
25102 +endchoice
25103 +
25104 +config AUFS_SBILIST
25105 +       bool
25106 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25107 +       default y
25108 +       help
25109 +       Automatic configuration for internal use.
25110 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25111 +
25112 +config AUFS_HNOTIFY
25113 +       bool "Detect direct branch access (bypassing aufs)"
25114 +       help
25115 +       If you want to modify files on branches directly, eg. bypassing aufs,
25116 +       and want aufs to detect the changes of them fully, then enable this
25117 +       option and use 'udba=notify' mount option.
25118 +       Currently there is only one available configuration, "fsnotify".
25119 +       It will have a negative impact to the performance.
25120 +       See detail in aufs.5.
25121 +
25122 +choice
25123 +       prompt "method" if AUFS_HNOTIFY
25124 +       default AUFS_HFSNOTIFY
25125 +config AUFS_HFSNOTIFY
25126 +       bool "fsnotify"
25127 +       select FSNOTIFY
25128 +endchoice
25129 +
25130 +config AUFS_EXPORT
25131 +       bool "NFS-exportable aufs"
25132 +       depends on EXPORTFS
25133 +       help
25134 +       If you want to export your mounted aufs via NFS, then enable this
25135 +       option. There are several requirements for this configuration.
25136 +       See detail in aufs.5.
25137 +
25138 +config AUFS_INO_T_64
25139 +       bool
25140 +       depends on AUFS_EXPORT
25141 +       depends on 64BIT && !(ALPHA || S390)
25142 +       default y
25143 +       help
25144 +       Automatic configuration for internal use.
25145 +       /* typedef unsigned long/int __kernel_ino_t */
25146 +       /* alpha and s390x are int */
25147 +
25148 +config AUFS_XATTR
25149 +       bool "support for XATTR/EA (including Security Labels)"
25150 +       help
25151 +       If your branch fs supports XATTR/EA and you want to make them
25152 +       available in aufs too, then enable this opsion and specify the
25153 +       branch attributes for EA.
25154 +       See detail in aufs.5.
25155 +
25156 +config AUFS_FHSM
25157 +       bool "File-based Hierarchical Storage Management"
25158 +       help
25159 +       Hierarchical Storage Management (or HSM) is a well-known feature
25160 +       in the storage world. Aufs provides this feature as file-based.
25161 +       with multiple branches.
25162 +       These multiple branches are prioritized, ie. the topmost one
25163 +       should be the fastest drive and be used heavily.
25164 +
25165 +config AUFS_RDU
25166 +       bool "Readdir in userspace"
25167 +       help
25168 +       Aufs has two methods to provide a merged view for a directory,
25169 +       by a user-space library and by kernel-space natively. The latter
25170 +       is always enabled but sometimes large and slow.
25171 +       If you enable this option, install the library in aufs2-util
25172 +       package, and set some environment variables for your readdir(3),
25173 +       then the work will be handled in user-space which generally
25174 +       shows better performance in most cases.
25175 +       See detail in aufs.5.
25176 +
25177 +config AUFS_DIRREN
25178 +       bool "Workaround for rename(2)-ing a directory"
25179 +       help
25180 +       By default, aufs returns EXDEV error in renameing a dir who has
25181 +       his child on the lower branch, since it is a bad idea to issue
25182 +       rename(2) internally for every lower branch. But user may not
25183 +       accept this behaviour. So here is a workaround to allow such
25184 +       rename(2) and store some extra information on the writable
25185 +       branch. Obviously this costs high (and I don't like it).
25186 +       To use this feature, you need to enable this configuration AND
25187 +       to specify the mount option `dirren.'
25188 +       See details in aufs.5 and the design documents.
25189 +
25190 +config AUFS_SHWH
25191 +       bool "Show whiteouts"
25192 +       help
25193 +       If you want to make the whiteouts in aufs visible, then enable
25194 +       this option and specify 'shwh' mount option. Although it may
25195 +       sounds like philosophy or something, but in technically it
25196 +       simply shows the name of whiteout with keeping its behaviour.
25197 +
25198 +config AUFS_BR_RAMFS
25199 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25200 +       help
25201 +       If you want to use ramfs as an aufs branch fs, then enable this
25202 +       option. Generally tmpfs is recommended.
25203 +       Aufs prohibited them to be a branch fs by default, because
25204 +       initramfs becomes unusable after switch_root or something
25205 +       generally. If you sets initramfs as an aufs branch and boot your
25206 +       system by switch_root, you will meet a problem easily since the
25207 +       files in initramfs may be inaccessible.
25208 +       Unless you are going to use ramfs as an aufs branch fs without
25209 +       switch_root or something, leave it N.
25210 +
25211 +config AUFS_BR_FUSE
25212 +       bool "Fuse fs as an aufs branch"
25213 +       depends on FUSE_FS
25214 +       select AUFS_POLL
25215 +       help
25216 +       If you want to use fuse-based userspace filesystem as an aufs
25217 +       branch fs, then enable this option.
25218 +       It implements the internal poll(2) operation which is
25219 +       implemented by fuse only (curretnly).
25220 +
25221 +config AUFS_POLL
25222 +       bool
25223 +       help
25224 +       Automatic configuration for internal use.
25225 +
25226 +config AUFS_BR_HFSPLUS
25227 +       bool "Hfsplus as an aufs branch"
25228 +       depends on HFSPLUS_FS
25229 +       default y
25230 +       help
25231 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25232 +       this option. This option introduces a small overhead at
25233 +       copying-up a file on hfsplus.
25234 +
25235 +config AUFS_BDEV_LOOP
25236 +       bool
25237 +       depends on BLK_DEV_LOOP
25238 +       default y
25239 +       help
25240 +       Automatic configuration for internal use.
25241 +       Convert =[ym] into =y.
25242 +
25243 +config AUFS_DEBUG
25244 +       bool "Debug aufs"
25245 +       help
25246 +       Enable this to compile aufs internal debug code.
25247 +       It will have a negative impact to the performance.
25248 +
25249 +config AUFS_MAGIC_SYSRQ
25250 +       bool
25251 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25252 +       default y
25253 +       help
25254 +       Automatic configuration for internal use.
25255 +       When aufs supports Magic SysRq, enabled automatically.
25256 +endif
25257 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25258 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25259 +++ linux/fs/aufs/lcnt.h        2022-11-05 23:02:18.965889284 +0100
25260 @@ -0,0 +1,186 @@
25261 +/* SPDX-License-Identifier: GPL-2.0 */
25262 +/*
25263 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25264 + *
25265 + * This program is free software; you can redistribute it and/or modify
25266 + * it under the terms of the GNU General Public License as published by
25267 + * the Free Software Foundation; either version 2 of the License, or
25268 + * (at your option) any later version.
25269 + *
25270 + * This program is distributed in the hope that it will be useful,
25271 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25272 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25273 + * GNU General Public License for more details.
25274 + *
25275 + * You should have received a copy of the GNU General Public License
25276 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25277 + */
25278 +
25279 +/*
25280 + * simple long counter wrapper
25281 + */
25282 +
25283 +#ifndef __AUFS_LCNT_H__
25284 +#define __AUFS_LCNT_H__
25285 +
25286 +#ifdef __KERNEL__
25287 +
25288 +#include "debug.h"
25289 +
25290 +#define AuLCntATOMIC   1
25291 +#define AuLCntPCPUCNT  2
25292 +/*
25293 + * why does percpu_refcount require extra synchronize_rcu()s in
25294 + * au_br_do_free()
25295 + */
25296 +#define AuLCntPCPUREF  3
25297 +
25298 +/* #define AuLCntChosen        AuLCntATOMIC */
25299 +#define AuLCntChosen   AuLCntPCPUCNT
25300 +/* #define AuLCntChosen        AuLCntPCPUREF */
25301 +
25302 +#if AuLCntChosen == AuLCntATOMIC
25303 +#include <linux/atomic.h>
25304 +
25305 +typedef atomic_long_t au_lcnt_t;
25306 +
25307 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25308 +{
25309 +       atomic_long_set(cnt, 0);
25310 +       return 0;
25311 +}
25312 +
25313 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25314 +{
25315 +       /* empty */
25316 +}
25317 +
25318 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25319 +                              int do_sync __maybe_unused)
25320 +{
25321 +       /* empty */
25322 +}
25323 +
25324 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25325 +{
25326 +       atomic_long_inc(cnt);
25327 +}
25328 +
25329 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25330 +{
25331 +       atomic_long_dec(cnt);
25332 +}
25333 +
25334 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25335 +{
25336 +       return atomic_long_read(cnt);
25337 +}
25338 +#endif
25339 +
25340 +#if AuLCntChosen == AuLCntPCPUCNT
25341 +#include <linux/percpu_counter.h>
25342 +
25343 +typedef struct percpu_counter au_lcnt_t;
25344 +
25345 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25346 +{
25347 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25348 +}
25349 +
25350 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25351 +{
25352 +       /* empty */
25353 +}
25354 +
25355 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25356 +{
25357 +       percpu_counter_destroy(cnt);
25358 +}
25359 +
25360 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25361 +{
25362 +       percpu_counter_inc(cnt);
25363 +}
25364 +
25365 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25366 +{
25367 +       percpu_counter_dec(cnt);
25368 +}
25369 +
25370 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25371 +{
25372 +       s64 n;
25373 +
25374 +       n = percpu_counter_sum(cnt);
25375 +       BUG_ON(n < 0);
25376 +       if (LONG_MAX != LLONG_MAX
25377 +           && n > LONG_MAX)
25378 +               AuWarn1("%s\n", "wrap-around");
25379 +
25380 +       return n;
25381 +}
25382 +#endif
25383 +
25384 +#if AuLCntChosen == AuLCntPCPUREF
25385 +#include <linux/percpu-refcount.h>
25386 +
25387 +typedef struct percpu_ref au_lcnt_t;
25388 +
25389 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25390 +{
25391 +       if (!release)
25392 +               release = percpu_ref_exit;
25393 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25394 +}
25395 +
25396 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25397 +{
25398 +       synchronize_rcu();
25399 +}
25400 +
25401 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25402 +{
25403 +       percpu_ref_kill(cnt);
25404 +       if (do_sync)
25405 +               au_lcnt_wait_for_fin(cnt);
25406 +}
25407 +
25408 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25409 +{
25410 +       percpu_ref_get(cnt);
25411 +}
25412 +
25413 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25414 +{
25415 +       percpu_ref_put(cnt);
25416 +}
25417 +
25418 +/*
25419 + * avoid calling this func as possible.
25420 + */
25421 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25422 +{
25423 +       long l;
25424 +
25425 +       percpu_ref_switch_to_atomic_sync(cnt);
25426 +       l = atomic_long_read(&cnt->count);
25427 +       if (do_rev)
25428 +               percpu_ref_switch_to_percpu(cnt);
25429 +
25430 +       /* percpu_ref is initialized by 1 instead of 0 */
25431 +       return l - 1;
25432 +}
25433 +#endif
25434 +
25435 +#ifdef CONFIG_AUFS_DEBUG
25436 +#define AuLCntZero(val) do {                   \
25437 +       long l = val;                           \
25438 +       if (l)                                  \
25439 +               AuDbg("%s = %ld\n", #val, l);   \
25440 +} while (0)
25441 +#else
25442 +#define AuLCntZero(val)                do {} while (0)
25443 +#endif
25444 +
25445 +#endif /* __KERNEL__ */
25446 +#endif /* __AUFS_LCNT_H__ */
25447 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25448 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25449 +++ linux/fs/aufs/loop.c        2022-11-05 23:02:18.965889284 +0100
25450 @@ -0,0 +1,148 @@
25451 +// SPDX-License-Identifier: GPL-2.0
25452 +/*
25453 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25454 + *
25455 + * This program is free software; you can redistribute it and/or modify
25456 + * it under the terms of the GNU General Public License as published by
25457 + * the Free Software Foundation; either version 2 of the License, or
25458 + * (at your option) any later version.
25459 + *
25460 + * This program is distributed in the hope that it will be useful,
25461 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25462 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25463 + * GNU General Public License for more details.
25464 + *
25465 + * You should have received a copy of the GNU General Public License
25466 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25467 + */
25468 +
25469 +/*
25470 + * support for loopback block device as a branch
25471 + */
25472 +
25473 +#include "aufs.h"
25474 +
25475 +/* added into drivers/block/loop.c */
25476 +static struct file *(*backing_file_func)(struct super_block *sb);
25477 +
25478 +/*
25479 + * test if two lower dentries have overlapping branches.
25480 + */
25481 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25482 +{
25483 +       struct super_block *h_sb;
25484 +       struct file *backing_file;
25485 +
25486 +       if (unlikely(!backing_file_func)) {
25487 +               /* don't load "loop" module here */
25488 +               backing_file_func = symbol_get(loop_backing_file);
25489 +               if (unlikely(!backing_file_func))
25490 +                       /* "loop" module is not loaded */
25491 +                       return 0;
25492 +       }
25493 +
25494 +       h_sb = h_adding->d_sb;
25495 +       backing_file = backing_file_func(h_sb);
25496 +       if (!backing_file)
25497 +               return 0;
25498 +
25499 +       h_adding = backing_file->f_path.dentry;
25500 +       /*
25501 +        * h_adding can be local NFS.
25502 +        * in this case aufs cannot detect the loop.
25503 +        */
25504 +       if (unlikely(h_adding->d_sb == sb))
25505 +               return 1;
25506 +       return !!au_test_subdir(h_adding, sb->s_root);
25507 +}
25508 +
25509 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25510 +int au_test_loopback_kthread(void)
25511 +{
25512 +       int ret;
25513 +       struct task_struct *tsk = current;
25514 +       char c, comm[sizeof(tsk->comm)];
25515 +
25516 +       ret = 0;
25517 +       if (tsk->flags & PF_KTHREAD) {
25518 +               get_task_comm(comm, tsk);
25519 +               c = comm[4];
25520 +               ret = ('0' <= c && c <= '9'
25521 +                      && !strncmp(comm, "loop", 4));
25522 +       }
25523 +
25524 +       return ret;
25525 +}
25526 +
25527 +/* ---------------------------------------------------------------------- */
25528 +
25529 +#define au_warn_loopback_step  16
25530 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25531 +static unsigned long *au_warn_loopback_array;
25532 +
25533 +void au_warn_loopback(struct super_block *h_sb)
25534 +{
25535 +       int i, new_nelem;
25536 +       unsigned long *a, magic;
25537 +       static DEFINE_SPINLOCK(spin);
25538 +
25539 +       magic = h_sb->s_magic;
25540 +       spin_lock(&spin);
25541 +       a = au_warn_loopback_array;
25542 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25543 +               if (a[i] == magic) {
25544 +                       spin_unlock(&spin);
25545 +                       return;
25546 +               }
25547 +
25548 +       /* h_sb is new to us, print it */
25549 +       if (i < au_warn_loopback_nelem) {
25550 +               a[i] = magic;
25551 +               goto pr;
25552 +       }
25553 +
25554 +       /* expand the array */
25555 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25556 +       a = au_kzrealloc(au_warn_loopback_array,
25557 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25558 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25559 +                        /*may_shrink*/0);
25560 +       if (a) {
25561 +               au_warn_loopback_nelem = new_nelem;
25562 +               au_warn_loopback_array = a;
25563 +               a[i] = magic;
25564 +               goto pr;
25565 +       }
25566 +
25567 +       spin_unlock(&spin);
25568 +       AuWarn1("realloc failed, ignored\n");
25569 +       return;
25570 +
25571 +pr:
25572 +       spin_unlock(&spin);
25573 +       pr_warn("you may want to try another patch for loopback file "
25574 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25575 +}
25576 +
25577 +int au_loopback_init(void)
25578 +{
25579 +       int err;
25580 +       struct super_block *sb __maybe_unused;
25581 +
25582 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25583 +
25584 +       err = 0;
25585 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25586 +                                        sizeof(unsigned long), GFP_NOFS);
25587 +       if (unlikely(!au_warn_loopback_array))
25588 +               err = -ENOMEM;
25589 +
25590 +       return err;
25591 +}
25592 +
25593 +void au_loopback_fin(void)
25594 +{
25595 +       if (backing_file_func)
25596 +               symbol_put(loop_backing_file);
25597 +       au_kfree_try_rcu(au_warn_loopback_array);
25598 +}
25599 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25600 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25601 +++ linux/fs/aufs/loop.h        2022-11-05 23:02:18.965889284 +0100
25602 @@ -0,0 +1,55 @@
25603 +/* SPDX-License-Identifier: GPL-2.0 */
25604 +/*
25605 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25606 + *
25607 + * This program is free software; you can redistribute it and/or modify
25608 + * it under the terms of the GNU General Public License as published by
25609 + * the Free Software Foundation; either version 2 of the License, or
25610 + * (at your option) any later version.
25611 + *
25612 + * This program is distributed in the hope that it will be useful,
25613 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25614 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25615 + * GNU General Public License for more details.
25616 + *
25617 + * You should have received a copy of the GNU General Public License
25618 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25619 + */
25620 +
25621 +/*
25622 + * support for loopback mount as a branch
25623 + */
25624 +
25625 +#ifndef __AUFS_LOOP_H__
25626 +#define __AUFS_LOOP_H__
25627 +
25628 +#ifdef __KERNEL__
25629 +
25630 +struct dentry;
25631 +struct super_block;
25632 +
25633 +#ifdef CONFIG_AUFS_BDEV_LOOP
25634 +/* drivers/block/loop.c */
25635 +struct file *loop_backing_file(struct super_block *sb);
25636 +
25637 +/* loop.c */
25638 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25639 +int au_test_loopback_kthread(void);
25640 +void au_warn_loopback(struct super_block *h_sb);
25641 +
25642 +int au_loopback_init(void);
25643 +void au_loopback_fin(void);
25644 +#else
25645 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25646 +
25647 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25648 +          struct dentry *h_adding)
25649 +AuStubInt0(au_test_loopback_kthread, void)
25650 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25651 +
25652 +AuStubInt0(au_loopback_init, void)
25653 +AuStubVoid(au_loopback_fin, void)
25654 +#endif /* BLK_DEV_LOOP */
25655 +
25656 +#endif /* __KERNEL__ */
25657 +#endif /* __AUFS_LOOP_H__ */
25658 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25659 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25660 +++ linux/fs/aufs/magic.mk      2022-11-05 23:02:18.965889284 +0100
25661 @@ -0,0 +1,31 @@
25662 +# SPDX-License-Identifier: GPL-2.0
25663 +
25664 +# defined in ${srctree}/fs/fuse/inode.c
25665 +# tristate
25666 +ifdef CONFIG_FUSE_FS
25667 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25668 +endif
25669 +
25670 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25671 +# tristate
25672 +ifdef CONFIG_XFS_FS
25673 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25674 +endif
25675 +
25676 +# defined in ${srctree}/fs/configfs/mount.c
25677 +# tristate
25678 +ifdef CONFIG_CONFIGFS_FS
25679 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25680 +endif
25681 +
25682 +# defined in ${srctree}/fs/ubifs/ubifs.h
25683 +# tristate
25684 +ifdef CONFIG_UBIFS_FS
25685 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25686 +endif
25687 +
25688 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25689 +# tristate
25690 +ifdef CONFIG_HFSPLUS_FS
25691 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25692 +endif
25693 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25694 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25695 +++ linux/fs/aufs/Makefile      2022-11-05 23:02:18.959222617 +0100
25696 @@ -0,0 +1,46 @@
25697 +# SPDX-License-Identifier: GPL-2.0
25698 +
25699 +include ${src}/magic.mk
25700 +ifeq (${CONFIG_AUFS_FS},m)
25701 +include ${src}/conf.mk
25702 +endif
25703 +-include ${src}/priv_def.mk
25704 +
25705 +# cf. include/linux/kernel.h
25706 +# enable pr_debug
25707 +ccflags-y += -DDEBUG
25708 +# sparse requires the full pathname
25709 +ifdef M
25710 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25711 +else
25712 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25713 +endif
25714 +
25715 +obj-$(CONFIG_AUFS_FS) += aufs.o
25716 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25717 +       wkq.o vfsub.o dcsub.o \
25718 +       cpup.o whout.o wbr_policy.o \
25719 +       dinfo.o dentry.o \
25720 +       dynop.o \
25721 +       finfo.o file.o f_op.o \
25722 +       dir.o vdir.o \
25723 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25724 +       mvdown.o ioctl.o
25725 +
25726 +# all are boolean
25727 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25728 +aufs-$(CONFIG_SYSFS) += sysfs.o
25729 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25730 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25731 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25732 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25733 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25734 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25735 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25736 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25737 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25738 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25739 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25740 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25741 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25742 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25743 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25744 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25745 +++ linux/fs/aufs/module.c      2022-11-05 23:02:18.965889284 +0100
25746 @@ -0,0 +1,273 @@
25747 +// SPDX-License-Identifier: GPL-2.0
25748 +/*
25749 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25750 + *
25751 + * This program is free software; you can redistribute it and/or modify
25752 + * it under the terms of the GNU General Public License as published by
25753 + * the Free Software Foundation; either version 2 of the License, or
25754 + * (at your option) any later version.
25755 + *
25756 + * This program is distributed in the hope that it will be useful,
25757 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25758 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25759 + * GNU General Public License for more details.
25760 + *
25761 + * You should have received a copy of the GNU General Public License
25762 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25763 + */
25764 +
25765 +/*
25766 + * module global variables and operations
25767 + */
25768 +
25769 +#include <linux/module.h>
25770 +#include <linux/seq_file.h>
25771 +#include "aufs.h"
25772 +
25773 +/* shrinkable realloc */
25774 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25775 +{
25776 +       size_t sz;
25777 +       int diff;
25778 +
25779 +       sz = 0;
25780 +       diff = -1;
25781 +       if (p) {
25782 +#if 0 /* unused */
25783 +               if (!new_sz) {
25784 +                       au_kfree_rcu(p);
25785 +                       p = NULL;
25786 +                       goto out;
25787 +               }
25788 +#else
25789 +               AuDebugOn(!new_sz);
25790 +#endif
25791 +               sz = ksize(p);
25792 +               diff = au_kmidx_sub(sz, new_sz);
25793 +       }
25794 +       if (sz && !diff)
25795 +               goto out;
25796 +
25797 +       if (sz < new_sz)
25798 +               /* expand or SLOB */
25799 +               p = krealloc(p, new_sz, gfp);
25800 +       else if (new_sz < sz && may_shrink) {
25801 +               /* shrink */
25802 +               void *q;
25803 +
25804 +               q = kmalloc(new_sz, gfp);
25805 +               if (q) {
25806 +                       if (p) {
25807 +                               memcpy(q, p, new_sz);
25808 +                               au_kfree_try_rcu(p);
25809 +                       }
25810 +                       p = q;
25811 +               } else
25812 +                       p = NULL;
25813 +       }
25814 +
25815 +out:
25816 +       return p;
25817 +}
25818 +
25819 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25820 +                  int may_shrink)
25821 +{
25822 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25823 +       if (p && new_sz > nused)
25824 +               memset(p + nused, 0, new_sz - nused);
25825 +       return p;
25826 +}
25827 +
25828 +/* ---------------------------------------------------------------------- */
25829 +/*
25830 + * aufs caches
25831 + */
25832 +struct kmem_cache *au_cache[AuCache_Last];
25833 +
25834 +static void au_cache_fin(void)
25835 +{
25836 +       int i;
25837 +
25838 +       /*
25839 +        * Make sure all delayed rcu free inodes are flushed before we
25840 +        * destroy cache.
25841 +        */
25842 +       rcu_barrier();
25843 +
25844 +       /* excluding AuCache_HNOTIFY */
25845 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25846 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25847 +               kmem_cache_destroy(au_cache[i]);
25848 +               au_cache[i] = NULL;
25849 +       }
25850 +}
25851 +
25852 +static int __init au_cache_init(void)
25853 +{
25854 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25855 +       if (au_cache[AuCache_DINFO])
25856 +               /* SLAB_DESTROY_BY_RCU */
25857 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25858 +                                                      au_icntnr_init_once);
25859 +       if (au_cache[AuCache_ICNTNR])
25860 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25861 +                                                     au_fi_init_once);
25862 +       if (au_cache[AuCache_FINFO])
25863 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25864 +       if (au_cache[AuCache_VDIR])
25865 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25866 +       if (au_cache[AuCache_DEHSTR])
25867 +               return 0;
25868 +
25869 +       au_cache_fin();
25870 +       return -ENOMEM;
25871 +}
25872 +
25873 +/* ---------------------------------------------------------------------- */
25874 +
25875 +int au_dir_roflags;
25876 +
25877 +#ifdef CONFIG_AUFS_SBILIST
25878 +/*
25879 + * iterate_supers_type() doesn't protect us from
25880 + * remounting (branch management)
25881 + */
25882 +struct hlist_bl_head au_sbilist;
25883 +#endif
25884 +
25885 +/*
25886 + * functions for module interface.
25887 + */
25888 +MODULE_LICENSE("GPL");
25889 +/* MODULE_LICENSE("GPL v2"); */
25890 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25891 +MODULE_DESCRIPTION(AUFS_NAME
25892 +       " -- Advanced multi layered unification filesystem");
25893 +MODULE_VERSION(AUFS_VERSION);
25894 +MODULE_ALIAS_FS(AUFS_NAME);
25895 +
25896 +/* this module parameter has no meaning when SYSFS is disabled */
25897 +int sysaufs_brs = 1;
25898 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25899 +module_param_named(brs, sysaufs_brs, int, 0444);
25900 +
25901 +/* this module parameter has no meaning when USER_NS is disabled */
25902 +bool au_userns;
25903 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25904 +module_param_named(allow_userns, au_userns, bool, 0444);
25905 +
25906 +/* ---------------------------------------------------------------------- */
25907 +
25908 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25909 +
25910 +int au_seq_path(struct seq_file *seq, struct path *path)
25911 +{
25912 +       int err;
25913 +
25914 +       err = seq_path(seq, path, au_esc_chars);
25915 +       if (err >= 0)
25916 +               err = 0;
25917 +       else
25918 +               err = -ENOMEM;
25919 +
25920 +       return err;
25921 +}
25922 +
25923 +/* ---------------------------------------------------------------------- */
25924 +
25925 +static int __init aufs_init(void)
25926 +{
25927 +       int err, i;
25928 +       char *p;
25929 +
25930 +       p = au_esc_chars;
25931 +       for (i = 1; i <= ' '; i++)
25932 +               *p++ = i;
25933 +       *p++ = '\\';
25934 +       *p++ = '\x7f';
25935 +       *p = 0;
25936 +
25937 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25938 +
25939 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25940 +       for (i = 0; i < AuIop_Last; i++)
25941 +               aufs_iop_nogetattr[i].getattr = NULL;
25942 +
25943 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25944 +
25945 +       au_sbilist_init();
25946 +       sysaufs_brs_init();
25947 +       au_debug_init();
25948 +       au_dy_init();
25949 +       err = sysaufs_init();
25950 +       if (unlikely(err))
25951 +               goto out;
25952 +       err = dbgaufs_init();
25953 +       if (unlikely(err))
25954 +               goto out_sysaufs;
25955 +       err = au_procfs_init();
25956 +       if (unlikely(err))
25957 +               goto out_dbgaufs;
25958 +       err = au_wkq_init();
25959 +       if (unlikely(err))
25960 +               goto out_procfs;
25961 +       err = au_loopback_init();
25962 +       if (unlikely(err))
25963 +               goto out_wkq;
25964 +       err = au_hnotify_init();
25965 +       if (unlikely(err))
25966 +               goto out_loopback;
25967 +       err = au_sysrq_init();
25968 +       if (unlikely(err))
25969 +               goto out_hin;
25970 +       err = au_cache_init();
25971 +       if (unlikely(err))
25972 +               goto out_sysrq;
25973 +
25974 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25975 +       err = register_filesystem(&aufs_fs_type);
25976 +       if (unlikely(err))
25977 +               goto out_cache;
25978 +
25979 +       /* since we define pr_fmt, call printk directly */
25980 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25981 +       goto out; /* success */
25982 +
25983 +out_cache:
25984 +       au_cache_fin();
25985 +out_sysrq:
25986 +       au_sysrq_fin();
25987 +out_hin:
25988 +       au_hnotify_fin();
25989 +out_loopback:
25990 +       au_loopback_fin();
25991 +out_wkq:
25992 +       au_wkq_fin();
25993 +out_procfs:
25994 +       au_procfs_fin();
25995 +out_dbgaufs:
25996 +       dbgaufs_fin();
25997 +out_sysaufs:
25998 +       sysaufs_fin();
25999 +       au_dy_fin();
26000 +out:
26001 +       return err;
26002 +}
26003 +
26004 +static void __exit aufs_exit(void)
26005 +{
26006 +       unregister_filesystem(&aufs_fs_type);
26007 +       au_cache_fin();
26008 +       au_sysrq_fin();
26009 +       au_hnotify_fin();
26010 +       au_loopback_fin();
26011 +       au_wkq_fin();
26012 +       au_procfs_fin();
26013 +       dbgaufs_fin();
26014 +       sysaufs_fin();
26015 +       au_dy_fin();
26016 +}
26017 +
26018 +module_init(aufs_init);
26019 +module_exit(aufs_exit);
26020 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
26021 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
26022 +++ linux/fs/aufs/module.h      2022-11-05 23:02:18.969222617 +0100
26023 @@ -0,0 +1,180 @@
26024 +/* SPDX-License-Identifier: GPL-2.0 */
26025 +/*
26026 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26027 + *
26028 + * This program is free software; you can redistribute it and/or modify
26029 + * it under the terms of the GNU General Public License as published by
26030 + * the Free Software Foundation; either version 2 of the License, or
26031 + * (at your option) any later version.
26032 + *
26033 + * This program is distributed in the hope that it will be useful,
26034 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26035 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26036 + * GNU General Public License for more details.
26037 + *
26038 + * You should have received a copy of the GNU General Public License
26039 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26040 + */
26041 +
26042 +/*
26043 + * module initialization and module-global
26044 + */
26045 +
26046 +#ifndef __AUFS_MODULE_H__
26047 +#define __AUFS_MODULE_H__
26048 +
26049 +#ifdef __KERNEL__
26050 +
26051 +#include <linux/slab.h>
26052 +#include "debug.h"
26053 +#include "dentry.h"
26054 +#include "dir.h"
26055 +#include "file.h"
26056 +#include "inode.h"
26057 +
26058 +struct path;
26059 +struct seq_file;
26060 +
26061 +/* module parameters */
26062 +extern int sysaufs_brs;
26063 +extern bool au_userns;
26064 +
26065 +/* ---------------------------------------------------------------------- */
26066 +
26067 +extern int au_dir_roflags;
26068 +
26069 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26070 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26071 +                  int may_shrink);
26072 +
26073 +/*
26074 + * Comparing the size of the object with sizeof(struct rcu_head)
26075 + * case 1: object is always larger
26076 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26077 + * case 2: object is always smaller
26078 + *     --> au_kfree_small()
26079 + * case 3: object can be any size
26080 + *     --> au_kfree_try_rcu()
26081 + */
26082 +
26083 +static inline void au_kfree_do_rcu(const void *p)
26084 +{
26085 +       struct {
26086 +               struct rcu_head rcu;
26087 +       } *a = (void *)p;
26088 +
26089 +       kfree_rcu(a, rcu);
26090 +}
26091 +
26092 +#define au_kfree_rcu(_p) do {                                          \
26093 +               typeof(_p) p = (_p);                                    \
26094 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26095 +               if (p)                                                  \
26096 +                       au_kfree_do_rcu(p);                             \
26097 +       } while (0)
26098 +
26099 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26100 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26101 +
26102 +static inline void au_kfree_try_rcu(const void *p)
26103 +{
26104 +       if (!p)
26105 +               return;
26106 +       if (au_kfree_sz_test(p))
26107 +               au_kfree_do_rcu(p);
26108 +       else
26109 +               kfree(p);
26110 +}
26111 +
26112 +static inline void au_kfree_small(const void *p)
26113 +{
26114 +       if (!p)
26115 +               return;
26116 +       AuDebugOn(au_kfree_sz_test(p));
26117 +       kfree(p);
26118 +}
26119 +
26120 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26121 +{
26122 +#ifndef CONFIG_SLOB
26123 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26124 +#else
26125 +       return -1; /* SLOB is untested */
26126 +#endif
26127 +}
26128 +
26129 +int au_seq_path(struct seq_file *seq, struct path *path);
26130 +
26131 +#ifdef CONFIG_PROC_FS
26132 +/* procfs.c */
26133 +int __init au_procfs_init(void);
26134 +void au_procfs_fin(void);
26135 +#else
26136 +AuStubInt0(au_procfs_init, void);
26137 +AuStubVoid(au_procfs_fin, void);
26138 +#endif
26139 +
26140 +/* ---------------------------------------------------------------------- */
26141 +
26142 +/* kmem cache */
26143 +enum {
26144 +       AuCache_DINFO,
26145 +       AuCache_ICNTNR,
26146 +       AuCache_FINFO,
26147 +       AuCache_VDIR,
26148 +       AuCache_DEHSTR,
26149 +       AuCache_HNOTIFY, /* must be last */
26150 +       AuCache_Last
26151 +};
26152 +
26153 +extern struct kmem_cache *au_cache[AuCache_Last];
26154 +
26155 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26156 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26157 +#define AuCacheCtor(type, ctor)        \
26158 +       kmem_cache_create(#type, sizeof(struct type), \
26159 +                         __alignof__(struct type), AuCacheFlags, ctor)
26160 +
26161 +#define AuCacheFuncAlloc(name, index)                                  \
26162 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26163 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26164 +
26165 +#define AuCacheFuncs(name, index)                                      \
26166 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26167 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26168 +                                                                       \
26169 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26170 +       { void *p = rcu;                                                \
26171 +               p -= offsetof(struct au_##name, rcu);                   \
26172 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26173 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26174 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26175 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26176 +                                                                       \
26177 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26178 +       { /* au_cache_free_##name##_norcu(p); */                        \
26179 +               au_cache_free_##name##_rcu(p); }
26180 +
26181 +AuCacheFuncs(dinfo, DINFO);
26182 +AuCacheFuncAlloc(dinfo, DINFO);
26183 +
26184 +AuCacheFuncs(icntnr, ICNTNR);
26185 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26186 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26187 +
26188 +AuCacheFuncs(finfo, FINFO);
26189 +AuCacheFuncAlloc(finfo, FINFO);
26190 +
26191 +AuCacheFuncs(vdir, VDIR);
26192 +AuCacheFuncAlloc(vdir, VDIR);
26193 +
26194 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26195 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26196 +
26197 +#ifdef CONFIG_AUFS_HNOTIFY
26198 +AuCacheFuncs(hnotify, HNOTIFY);
26199 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26200 +#endif
26201 +
26202 +#endif /* __KERNEL__ */
26203 +#endif /* __AUFS_MODULE_H__ */
26204 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26205 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26206 +++ linux/fs/aufs/mvdown.c      2024-03-10 23:40:47.086476782 +0100
26207 @@ -0,0 +1,713 @@
26208 +// SPDX-License-Identifier: GPL-2.0
26209 +/*
26210 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26211 + *
26212 + * This program is free software; you can redistribute it and/or modify
26213 + * it under the terms of the GNU General Public License as published by
26214 + * the Free Software Foundation; either version 2 of the License, or
26215 + * (at your option) any later version.
26216 + *
26217 + * This program is distributed in the hope that it will be useful,
26218 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26219 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26220 + * GNU General Public License for more details.
26221 + *
26222 + * You should have received a copy of the GNU General Public License
26223 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26224 + */
26225 +
26226 +/*
26227 + * move-down, opposite of copy-up
26228 + */
26229 +
26230 +#include "aufs.h"
26231 +
26232 +struct au_mvd_args {
26233 +       struct {
26234 +               struct super_block *h_sb;
26235 +               struct dentry *h_parent;
26236 +               struct au_hinode *hdir;
26237 +               struct inode *h_dir, *h_inode;
26238 +               struct au_pin pin;
26239 +       } info[AUFS_MVDOWN_NARRAY];
26240 +
26241 +       struct aufs_mvdown mvdown;
26242 +       struct dentry *dentry, *parent;
26243 +       struct inode *inode, *dir;
26244 +       struct super_block *sb;
26245 +       aufs_bindex_t bopq, bwh, bfound;
26246 +       unsigned char rename_lock;
26247 +};
26248 +
26249 +#define mvd_errno              mvdown.au_errno
26250 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26251 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26252 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26253 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26254 +
26255 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26256 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26257 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26258 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26259 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26260 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26261 +
26262 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26263 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26264 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26265 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26266 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26267 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26268 +
26269 +#define AU_MVD_PR(flag, ...) do {                      \
26270 +               if (flag)                               \
26271 +                       pr_err(__VA_ARGS__);            \
26272 +       } while (0)
26273 +
26274 +static int find_lower_writable(struct au_mvd_args *a)
26275 +{
26276 +       struct super_block *sb;
26277 +       aufs_bindex_t bindex, bbot;
26278 +       struct au_branch *br;
26279 +
26280 +       sb = a->sb;
26281 +       bindex = a->mvd_bsrc;
26282 +       bbot = au_sbbot(sb);
26283 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26284 +               for (bindex++; bindex <= bbot; bindex++) {
26285 +                       br = au_sbr(sb, bindex);
26286 +                       if (au_br_fhsm(br->br_perm)
26287 +                           && !sb_rdonly(au_br_sb(br)))
26288 +                               return bindex;
26289 +               }
26290 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26291 +               for (bindex++; bindex <= bbot; bindex++) {
26292 +                       br = au_sbr(sb, bindex);
26293 +                       if (!au_br_rdonly(br))
26294 +                               return bindex;
26295 +               }
26296 +       else
26297 +               for (bindex++; bindex <= bbot; bindex++) {
26298 +                       br = au_sbr(sb, bindex);
26299 +                       if (!sb_rdonly(au_br_sb(br))) {
26300 +                               if (au_br_rdonly(br))
26301 +                                       a->mvdown.flags
26302 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26303 +                               return bindex;
26304 +                       }
26305 +               }
26306 +
26307 +       return -1;
26308 +}
26309 +
26310 +/* make the parent dir on bdst */
26311 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26312 +{
26313 +       int err;
26314 +
26315 +       err = 0;
26316 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26317 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26318 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26319 +       a->mvd_h_dst_parent = NULL;
26320 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26321 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26322 +       if (!a->mvd_h_dst_parent) {
26323 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26324 +               if (unlikely(err)) {
26325 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26326 +                       goto out;
26327 +               }
26328 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26329 +       }
26330 +
26331 +out:
26332 +       AuTraceErr(err);
26333 +       return err;
26334 +}
26335 +
26336 +/* lock them all */
26337 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26338 +{
26339 +       int err;
26340 +       struct dentry *h_trap;
26341 +
26342 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26343 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26344 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26345 +                    au_opt_udba(a->sb),
26346 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26347 +       AuTraceErr(err);
26348 +       if (unlikely(err)) {
26349 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26350 +               goto out;
26351 +       }
26352 +
26353 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26354 +               a->rename_lock = 0;
26355 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26356 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26357 +                           au_opt_udba(a->sb),
26358 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26359 +               err = au_do_pin(&a->mvd_pin_src);
26360 +               AuTraceErr(err);
26361 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26362 +               if (unlikely(err)) {
26363 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26364 +                       goto out_dst;
26365 +               }
26366 +               goto out; /* success */
26367 +       }
26368 +
26369 +       a->rename_lock = 1;
26370 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26371 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26372 +                    au_opt_udba(a->sb),
26373 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26374 +       AuTraceErr(err);
26375 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26376 +       if (unlikely(err)) {
26377 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26378 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26379 +               goto out_dst;
26380 +       }
26381 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26382 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26383 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26384 +       if (IS_ERR(h_trap)) {
26385 +               err = PTR_ERR(h_trap);
26386 +               au_pin_hdir_lock(&a->mvd_pin_src);
26387 +               au_unpin(&a->mvd_pin_src);
26388 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26389 +               goto out_dst;
26390 +       }
26391 +       if (h_trap) {
26392 +               err = (h_trap != a->mvd_h_src_parent);
26393 +               if (err)
26394 +                       err = (h_trap != a->mvd_h_dst_parent);
26395 +       }
26396 +       BUG_ON(err); /* it should never happen */
26397 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26398 +               err = -EBUSY;
26399 +               AuTraceErr(err);
26400 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26401 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26402 +               au_pin_hdir_lock(&a->mvd_pin_src);
26403 +               au_unpin(&a->mvd_pin_src);
26404 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26405 +               goto out_dst;
26406 +       }
26407 +       goto out; /* success */
26408 +
26409 +out_dst:
26410 +       au_unpin(&a->mvd_pin_dst);
26411 +out:
26412 +       AuTraceErr(err);
26413 +       return err;
26414 +}
26415 +
26416 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26417 +{
26418 +       if (!a->rename_lock)
26419 +               au_unpin(&a->mvd_pin_src);
26420 +       else {
26421 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26422 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26423 +               au_pin_hdir_lock(&a->mvd_pin_src);
26424 +               au_unpin(&a->mvd_pin_src);
26425 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26426 +       }
26427 +       au_unpin(&a->mvd_pin_dst);
26428 +}
26429 +
26430 +/* copy-down the file */
26431 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26432 +{
26433 +       int err;
26434 +       struct au_cp_generic cpg = {
26435 +               .dentry = a->dentry,
26436 +               .bdst   = a->mvd_bdst,
26437 +               .bsrc   = a->mvd_bsrc,
26438 +               .len    = -1,
26439 +               .pin    = &a->mvd_pin_dst,
26440 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26441 +       };
26442 +
26443 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26444 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26445 +               au_fset_cpup(cpg.flags, OVERWRITE);
26446 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26447 +               au_fset_cpup(cpg.flags, RWDST);
26448 +       err = au_sio_cpdown_simple(&cpg);
26449 +       if (unlikely(err))
26450 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26451 +
26452 +       AuTraceErr(err);
26453 +       return err;
26454 +}
26455 +
26456 +/*
26457 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26458 + * were sleeping
26459 + */
26460 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26461 +{
26462 +       int err;
26463 +       struct path h_path;
26464 +       struct au_branch *br;
26465 +       struct inode *delegated;
26466 +
26467 +       br = au_sbr(a->sb, a->mvd_bdst);
26468 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26469 +       err = PTR_ERR(h_path.dentry);
26470 +       if (IS_ERR(h_path.dentry)) {
26471 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26472 +               goto out;
26473 +       }
26474 +
26475 +       err = 0;
26476 +       if (d_is_positive(h_path.dentry)) {
26477 +               h_path.mnt = au_br_mnt(br);
26478 +               delegated = NULL;
26479 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26480 +                                  &delegated, /*force*/0);
26481 +               if (unlikely(err == -EWOULDBLOCK)) {
26482 +                       pr_warn("cannot retry for NFSv4 delegation"
26483 +                               " for an internal unlink\n");
26484 +                       iput(delegated);
26485 +               }
26486 +               if (unlikely(err))
26487 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26488 +       }
26489 +       dput(h_path.dentry);
26490 +
26491 +out:
26492 +       AuTraceErr(err);
26493 +       return err;
26494 +}
26495 +
26496 +/*
26497 + * unlink the topmost h_dentry
26498 + */
26499 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26500 +{
26501 +       int err;
26502 +       struct path h_path;
26503 +       struct inode *delegated;
26504 +
26505 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26506 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26507 +       delegated = NULL;
26508 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26509 +       if (unlikely(err == -EWOULDBLOCK)) {
26510 +               pr_warn("cannot retry for NFSv4 delegation"
26511 +                       " for an internal unlink\n");
26512 +               iput(delegated);
26513 +       }
26514 +       if (unlikely(err))
26515 +               AU_MVD_PR(dmsg, "unlink failed\n");
26516 +
26517 +       AuTraceErr(err);
26518 +       return err;
26519 +}
26520 +
26521 +/* Since mvdown succeeded, we ignore an error of this function */
26522 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26523 +{
26524 +       int err;
26525 +       struct au_branch *br;
26526 +
26527 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26528 +       br = au_sbr(a->sb, a->mvd_bsrc);
26529 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26530 +       if (!err) {
26531 +               br = au_sbr(a->sb, a->mvd_bdst);
26532 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26533 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26534 +       }
26535 +       if (!err)
26536 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26537 +       else
26538 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26539 +}
26540 +
26541 +/*
26542 + * copy-down the file and unlink the bsrc file.
26543 + * - unlink the bdst whout if exist
26544 + * - copy-down the file (with whtmp name and rename)
26545 + * - unlink the bsrc file
26546 + */
26547 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26548 +{
26549 +       int err;
26550 +
26551 +       err = au_do_mkdir(dmsg, a);
26552 +       if (!err)
26553 +               err = au_do_lock(dmsg, a);
26554 +       if (unlikely(err))
26555 +               goto out;
26556 +
26557 +       /*
26558 +        * do not revert the activities we made on bdst since they should be
26559 +        * harmless in aufs.
26560 +        */
26561 +
26562 +       err = au_do_cpdown(dmsg, a);
26563 +       if (!err)
26564 +               err = au_do_unlink_wh(dmsg, a);
26565 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26566 +               err = au_do_unlink(dmsg, a);
26567 +       if (unlikely(err))
26568 +               goto out_unlock;
26569 +
26570 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26571 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26572 +       if (find_lower_writable(a) < 0)
26573 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26574 +
26575 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26576 +               au_do_stfs(dmsg, a);
26577 +
26578 +       /* maintain internal array */
26579 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26580 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26581 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26582 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26583 +               au_set_ibtop(a->inode, a->mvd_bdst);
26584 +       } else {
26585 +               /* hide the lower */
26586 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26587 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26588 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26589 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26590 +       }
26591 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26592 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26593 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26594 +               au_set_ibbot(a->inode, a->mvd_bdst);
26595 +
26596 +out_unlock:
26597 +       au_do_unlock(dmsg, a);
26598 +out:
26599 +       AuTraceErr(err);
26600 +       return err;
26601 +}
26602 +
26603 +/* ---------------------------------------------------------------------- */
26604 +
26605 +/* make sure the file is idle */
26606 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26607 +{
26608 +       int err, plinked;
26609 +
26610 +       err = 0;
26611 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26612 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26613 +           && au_dcount(a->dentry) == 1
26614 +           && atomic_read(&a->inode->i_count) == 1
26615 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26616 +           && (!plinked || !au_plink_test(a->inode))
26617 +           && a->inode->i_nlink == 1)
26618 +               goto out;
26619 +
26620 +       err = -EBUSY;
26621 +       AU_MVD_PR(dmsg,
26622 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26623 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26624 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26625 +                 a->mvd_h_src_inode->i_nlink,
26626 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26627 +
26628 +out:
26629 +       AuTraceErr(err);
26630 +       return err;
26631 +}
26632 +
26633 +/* make sure the parent dir is fine */
26634 +static int au_mvd_args_parent(const unsigned char dmsg,
26635 +                             struct au_mvd_args *a)
26636 +{
26637 +       int err;
26638 +       aufs_bindex_t bindex;
26639 +
26640 +       err = 0;
26641 +       if (unlikely(au_alive_dir(a->parent))) {
26642 +               err = -ENOENT;
26643 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26644 +               goto out;
26645 +       }
26646 +
26647 +       a->bopq = au_dbdiropq(a->parent);
26648 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26649 +       AuDbg("b%d\n", bindex);
26650 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26651 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26652 +               err = -EINVAL;
26653 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26654 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26655 +                         a->bopq, a->mvd_bdst);
26656 +       }
26657 +
26658 +out:
26659 +       AuTraceErr(err);
26660 +       return err;
26661 +}
26662 +
26663 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26664 +                                   struct au_mvd_args *a)
26665 +{
26666 +       int err;
26667 +       struct au_dinfo *dinfo, *tmp;
26668 +
26669 +       /* lookup the next lower positive entry */
26670 +       err = -ENOMEM;
26671 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26672 +       if (unlikely(!tmp))
26673 +               goto out;
26674 +
26675 +       a->bfound = -1;
26676 +       a->bwh = -1;
26677 +       dinfo = au_di(a->dentry);
26678 +       au_di_cp(tmp, dinfo);
26679 +       au_di_swap(tmp, dinfo);
26680 +
26681 +       /* returns the number of positive dentries */
26682 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26683 +                            /* AuLkup_IGNORE_PERM */ 0);
26684 +       if (!err)
26685 +               a->bwh = au_dbwh(a->dentry);
26686 +       else if (err > 0)
26687 +               a->bfound = au_dbtop(a->dentry);
26688 +
26689 +       au_di_swap(tmp, dinfo);
26690 +       au_rw_write_unlock(&tmp->di_rwsem);
26691 +       au_di_free(tmp);
26692 +       if (unlikely(err < 0))
26693 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26694 +
26695 +       /*
26696 +        * here, we have these cases.
26697 +        * bfound == -1
26698 +        *      no positive dentry under bsrc. there are more sub-cases.
26699 +        *      bwh < 0
26700 +        *              there no whiteout, we can safely move-down.
26701 +        *      bwh <= bsrc
26702 +        *              impossible
26703 +        *      bsrc < bwh && bwh < bdst
26704 +        *              there is a whiteout on RO branch. cannot proceed.
26705 +        *      bwh == bdst
26706 +        *              there is a whiteout on the RW target branch. it should
26707 +        *              be removed.
26708 +        *      bdst < bwh
26709 +        *              there is a whiteout somewhere unrelated branch.
26710 +        * -1 < bfound && bfound <= bsrc
26711 +        *      impossible.
26712 +        * bfound < bdst
26713 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26714 +        *      proceed.
26715 +        * bfound == bdst
26716 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26717 +        *      error.
26718 +        * bdst < bfound
26719 +        *      found, after we create the file on bdst, it will be hidden.
26720 +        */
26721 +
26722 +       AuDebugOn(a->bfound == -1
26723 +                 && a->bwh != -1
26724 +                 && a->bwh <= a->mvd_bsrc);
26725 +       AuDebugOn(-1 < a->bfound
26726 +                 && a->bfound <= a->mvd_bsrc);
26727 +
26728 +       err = -EINVAL;
26729 +       if (a->bfound == -1
26730 +           && a->mvd_bsrc < a->bwh
26731 +           && a->bwh != -1
26732 +           && a->bwh < a->mvd_bdst) {
26733 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26734 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26735 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26736 +               goto out;
26737 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26738 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26739 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26740 +                         a->mvd_bdst, a->bfound);
26741 +               goto out;
26742 +       }
26743 +
26744 +       err = 0; /* success */
26745 +
26746 +out:
26747 +       AuTraceErr(err);
26748 +       return err;
26749 +}
26750 +
26751 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26752 +{
26753 +       int err;
26754 +
26755 +       err = 0;
26756 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26757 +           && a->bfound == a->mvd_bdst)
26758 +               err = -EEXIST;
26759 +       AuTraceErr(err);
26760 +       return err;
26761 +}
26762 +
26763 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26764 +{
26765 +       int err;
26766 +       struct au_branch *br;
26767 +
26768 +       err = -EISDIR;
26769 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26770 +               goto out;
26771 +
26772 +       err = -EINVAL;
26773 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26774 +               a->mvd_bsrc = au_ibtop(a->inode);
26775 +       else {
26776 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26777 +               if (unlikely(a->mvd_bsrc < 0
26778 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26779 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26780 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26781 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26782 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26783 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26784 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26785 +                       AU_MVD_PR(dmsg, "no upper\n");
26786 +                       goto out;
26787 +               }
26788 +       }
26789 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26790 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26791 +               AU_MVD_PR(dmsg, "on the bottom\n");
26792 +               goto out;
26793 +       }
26794 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26795 +       br = au_sbr(a->sb, a->mvd_bsrc);
26796 +       err = au_br_rdonly(br);
26797 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26798 +               if (unlikely(err))
26799 +                       goto out;
26800 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26801 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26802 +               if (err)
26803 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26804 +               /* go on */
26805 +       } else
26806 +               goto out;
26807 +
26808 +       err = -EINVAL;
26809 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26810 +               a->mvd_bdst = find_lower_writable(a);
26811 +               if (unlikely(a->mvd_bdst < 0)) {
26812 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26813 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26814 +                       goto out;
26815 +               }
26816 +       } else {
26817 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26818 +               if (unlikely(a->mvd_bdst < 0
26819 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26820 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26821 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26822 +                       goto out;
26823 +               }
26824 +       }
26825 +
26826 +       err = au_mvd_args_busy(dmsg, a);
26827 +       if (!err)
26828 +               err = au_mvd_args_parent(dmsg, a);
26829 +       if (!err)
26830 +               err = au_mvd_args_intermediate(dmsg, a);
26831 +       if (!err)
26832 +               err = au_mvd_args_exist(dmsg, a);
26833 +       if (!err)
26834 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26835 +
26836 +out:
26837 +       AuTraceErr(err);
26838 +       return err;
26839 +}
26840 +
26841 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26842 +{
26843 +       int err, e;
26844 +       unsigned char dmsg;
26845 +       struct au_mvd_args *args;
26846 +       struct inode *inode;
26847 +
26848 +       inode = d_inode(dentry);
26849 +       err = -EPERM;
26850 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26851 +               goto out;
26852 +
26853 +       err = -ENOMEM;
26854 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26855 +       if (unlikely(!args))
26856 +               goto out;
26857 +
26858 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26859 +       if (!err)
26860 +               /* VERIFY_WRITE */
26861 +               err = !access_ok(uarg, sizeof(*uarg));
26862 +       if (unlikely(err)) {
26863 +               err = -EFAULT;
26864 +               AuTraceErr(err);
26865 +               goto out_free;
26866 +       }
26867 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26868 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26869 +       args->mvdown.au_errno = 0;
26870 +       args->dentry = dentry;
26871 +       args->inode = inode;
26872 +       args->sb = dentry->d_sb;
26873 +
26874 +       err = -ENOENT;
26875 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26876 +       args->parent = dget_parent(dentry);
26877 +       args->dir = d_inode(args->parent);
26878 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26879 +       dput(args->parent);
26880 +       if (unlikely(args->parent != dentry->d_parent)) {
26881 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26882 +               goto out_dir;
26883 +       }
26884 +
26885 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26886 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26887 +       if (unlikely(err))
26888 +               goto out_inode;
26889 +
26890 +       di_write_lock_parent(args->parent);
26891 +       err = au_mvd_args(dmsg, args);
26892 +       if (unlikely(err))
26893 +               goto out_parent;
26894 +
26895 +       err = au_do_mvdown(dmsg, args);
26896 +       if (unlikely(err))
26897 +               goto out_parent;
26898 +
26899 +       au_cpup_attr_timesizes(args->dir);
26900 +       au_cpup_attr_timesizes(inode);
26901 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26902 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26903 +       /* au_digen_dec(dentry); */
26904 +
26905 +out_parent:
26906 +       di_write_unlock(args->parent);
26907 +       aufs_read_unlock(dentry, AuLock_DW);
26908 +out_inode:
26909 +       inode_unlock(inode);
26910 +out_dir:
26911 +       inode_unlock(args->dir);
26912 +out_free:
26913 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26914 +       if (unlikely(e))
26915 +               err = -EFAULT;
26916 +       au_kfree_rcu(args);
26917 +out:
26918 +       AuTraceErr(err);
26919 +       return err;
26920 +}
26921 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26922 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26923 +++ linux/fs/aufs/opts.c        2024-03-10 23:40:47.086476782 +0100
26924 @@ -0,0 +1,1032 @@
26925 +// SPDX-License-Identifier: GPL-2.0
26926 +/*
26927 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26928 + *
26929 + * This program is free software; you can redistribute it and/or modify
26930 + * it under the terms of the GNU General Public License as published by
26931 + * the Free Software Foundation; either version 2 of the License, or
26932 + * (at your option) any later version.
26933 + *
26934 + * This program is distributed in the hope that it will be useful,
26935 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26936 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26937 + * GNU General Public License for more details.
26938 + *
26939 + * You should have received a copy of the GNU General Public License
26940 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26941 + */
26942 +
26943 +/*
26944 + * mount options/flags
26945 + */
26946 +
26947 +#include <linux/types.h> /* a distribution requires */
26948 +#include <linux/parser.h>
26949 +#include "aufs.h"
26950 +
26951 +/* ---------------------------------------------------------------------- */
26952 +
26953 +static const char *au_parser_pattern(int val, match_table_t tbl)
26954 +{
26955 +       struct match_token *p;
26956 +
26957 +       p = tbl;
26958 +       while (p->pattern) {
26959 +               if (p->token == val)
26960 +                       return p->pattern;
26961 +               p++;
26962 +       }
26963 +       BUG();
26964 +       return "??";
26965 +}
26966 +
26967 +static const char *au_optstr(int *val, match_table_t tbl)
26968 +{
26969 +       struct match_token *p;
26970 +       int v;
26971 +
26972 +       v = *val;
26973 +       if (!v)
26974 +               goto out;
26975 +       p = tbl;
26976 +       while (p->pattern) {
26977 +               if (p->token
26978 +                   && (v & p->token) == p->token) {
26979 +                       *val &= ~p->token;
26980 +                       return p->pattern;
26981 +               }
26982 +               p++;
26983 +       }
26984 +
26985 +out:
26986 +       return NULL;
26987 +}
26988 +
26989 +/* ---------------------------------------------------------------------- */
26990 +
26991 +static match_table_t brperm = {
26992 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26993 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26994 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26995 +       {0, NULL}
26996 +};
26997 +
26998 +static match_table_t brattr = {
26999 +       /* general */
27000 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
27001 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
27002 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
27003 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
27004 +#ifdef CONFIG_AUFS_FHSM
27005 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
27006 +#endif
27007 +#ifdef CONFIG_AUFS_XATTR
27008 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
27009 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
27010 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
27011 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
27012 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
27013 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
27014 +#endif
27015 +
27016 +       /* ro/rr branch */
27017 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
27018 +
27019 +       /* rw branch */
27020 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
27021 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
27022 +
27023 +       {0, NULL}
27024 +};
27025 +
27026 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
27027 +{
27028 +       int attr, v;
27029 +       char *p;
27030 +
27031 +       attr = 0;
27032 +       do {
27033 +               p = strchr(str, '+');
27034 +               if (p)
27035 +                       *p = 0;
27036 +               v = match_token(str, table, args);
27037 +               if (v) {
27038 +                       if (v & AuBrAttr_CMOO_Mask)
27039 +                               attr &= ~AuBrAttr_CMOO_Mask;
27040 +                       attr |= v;
27041 +               } else {
27042 +                       if (p)
27043 +                               *p = '+';
27044 +                       pr_warn("ignored branch attribute %s\n", str);
27045 +                       break;
27046 +               }
27047 +               if (p)
27048 +                       str = p + 1;
27049 +       } while (p);
27050 +
27051 +       return attr;
27052 +}
27053 +
27054 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27055 +{
27056 +       int sz;
27057 +       const char *p;
27058 +       char *q;
27059 +
27060 +       q = str->a;
27061 +       *q = 0;
27062 +       p = au_optstr(&perm, brattr);
27063 +       if (p) {
27064 +               sz = strlen(p);
27065 +               memcpy(q, p, sz + 1);
27066 +               q += sz;
27067 +       } else
27068 +               goto out;
27069 +
27070 +       do {
27071 +               p = au_optstr(&perm, brattr);
27072 +               if (p) {
27073 +                       *q++ = '+';
27074 +                       sz = strlen(p);
27075 +                       memcpy(q, p, sz + 1);
27076 +                       q += sz;
27077 +               }
27078 +       } while (p);
27079 +
27080 +out:
27081 +       return q - str->a;
27082 +}
27083 +
27084 +int au_br_perm_val(char *perm)
27085 +{
27086 +       int val, bad, sz;
27087 +       char *p;
27088 +       substring_t args[MAX_OPT_ARGS];
27089 +       au_br_perm_str_t attr;
27090 +
27091 +       p = strchr(perm, '+');
27092 +       if (p)
27093 +               *p = 0;
27094 +       val = match_token(perm, brperm, args);
27095 +       if (!val) {
27096 +               if (p)
27097 +                       *p = '+';
27098 +               pr_warn("ignored branch permission %s\n", perm);
27099 +               val = AuBrPerm_RO;
27100 +               goto out;
27101 +       }
27102 +       if (!p)
27103 +               goto out;
27104 +
27105 +       val |= br_attr_val(p + 1, brattr, args);
27106 +
27107 +       bad = 0;
27108 +       switch (val & AuBrPerm_Mask) {
27109 +       case AuBrPerm_RO:
27110 +       case AuBrPerm_RR:
27111 +               bad = val & AuBrWAttr_Mask;
27112 +               val &= ~AuBrWAttr_Mask;
27113 +               break;
27114 +       case AuBrPerm_RW:
27115 +               bad = val & AuBrRAttr_Mask;
27116 +               val &= ~AuBrRAttr_Mask;
27117 +               break;
27118 +       }
27119 +
27120 +       /*
27121 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27122 +        * does not treat it as an error, just warning.
27123 +        * this is a tiny guard for the user operation.
27124 +        */
27125 +       if (val & AuBrAttr_UNPIN) {
27126 +               bad |= AuBrAttr_UNPIN;
27127 +               val &= ~AuBrAttr_UNPIN;
27128 +       }
27129 +
27130 +       if (unlikely(bad)) {
27131 +               sz = au_do_optstr_br_attr(&attr, bad);
27132 +               AuDebugOn(!sz);
27133 +               pr_warn("ignored branch attribute %s\n", attr.a);
27134 +       }
27135 +
27136 +out:
27137 +       return val;
27138 +}
27139 +
27140 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27141 +{
27142 +       au_br_perm_str_t attr;
27143 +       const char *p;
27144 +       char *q;
27145 +       int sz;
27146 +
27147 +       q = str->a;
27148 +       p = au_optstr(&perm, brperm);
27149 +       AuDebugOn(!p || !*p);
27150 +       sz = strlen(p);
27151 +       memcpy(q, p, sz + 1);
27152 +       q += sz;
27153 +
27154 +       sz = au_do_optstr_br_attr(&attr, perm);
27155 +       if (sz) {
27156 +               *q++ = '+';
27157 +               memcpy(q, attr.a, sz + 1);
27158 +       }
27159 +
27160 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27161 +}
27162 +
27163 +/* ---------------------------------------------------------------------- */
27164 +
27165 +static match_table_t udbalevel = {
27166 +       {AuOpt_UDBA_REVAL, "reval"},
27167 +       {AuOpt_UDBA_NONE, "none"},
27168 +#ifdef CONFIG_AUFS_HNOTIFY
27169 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27170 +#ifdef CONFIG_AUFS_HFSNOTIFY
27171 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27172 +#endif
27173 +#endif
27174 +       {-1, NULL}
27175 +};
27176 +
27177 +int au_udba_val(char *str)
27178 +{
27179 +       substring_t args[MAX_OPT_ARGS];
27180 +
27181 +       return match_token(str, udbalevel, args);
27182 +}
27183 +
27184 +const char *au_optstr_udba(int udba)
27185 +{
27186 +       return au_parser_pattern(udba, udbalevel);
27187 +}
27188 +
27189 +/* ---------------------------------------------------------------------- */
27190 +
27191 +static match_table_t au_wbr_create_policy = {
27192 +       {AuWbrCreate_TDP, "tdp"},
27193 +       {AuWbrCreate_TDP, "top-down-parent"},
27194 +       {AuWbrCreate_RR, "rr"},
27195 +       {AuWbrCreate_RR, "round-robin"},
27196 +       {AuWbrCreate_MFS, "mfs"},
27197 +       {AuWbrCreate_MFS, "most-free-space"},
27198 +       {AuWbrCreate_MFSV, "mfs:%d"},
27199 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27200 +
27201 +       /* top-down regardless the parent, and then mfs */
27202 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27203 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27204 +
27205 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27206 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27207 +       {AuWbrCreate_PMFS, "pmfs"},
27208 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27209 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27210 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27211 +
27212 +       {-1, NULL}
27213 +};
27214 +
27215 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27216 +                           struct au_opt_wbr_create *create)
27217 +{
27218 +       int err;
27219 +       unsigned long long ull;
27220 +
27221 +       err = 0;
27222 +       if (!match_u64(arg, &ull))
27223 +               create->mfsrr_watermark = ull;
27224 +       else {
27225 +               pr_err("bad integer in %s\n", str);
27226 +               err = -EINVAL;
27227 +       }
27228 +
27229 +       return err;
27230 +}
27231 +
27232 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27233 +                         struct au_opt_wbr_create *create)
27234 +{
27235 +       int n, err;
27236 +
27237 +       err = 0;
27238 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27239 +               create->mfs_second = n;
27240 +       else {
27241 +               pr_err("bad integer in %s\n", str);
27242 +               err = -EINVAL;
27243 +       }
27244 +
27245 +       return err;
27246 +}
27247 +
27248 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27249 +{
27250 +       int err, e;
27251 +       substring_t args[MAX_OPT_ARGS];
27252 +
27253 +       err = match_token(str, au_wbr_create_policy, args);
27254 +       create->wbr_create = err;
27255 +       switch (err) {
27256 +       case AuWbrCreate_MFSRRV:
27257 +       case AuWbrCreate_TDMFSV:
27258 +       case AuWbrCreate_PMFSRRV:
27259 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27260 +               if (!e)
27261 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27262 +               if (unlikely(e))
27263 +                       err = e;
27264 +               break;
27265 +       case AuWbrCreate_MFSRR:
27266 +       case AuWbrCreate_TDMFS:
27267 +       case AuWbrCreate_PMFSRR:
27268 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27269 +               if (unlikely(e)) {
27270 +                       err = e;
27271 +                       break;
27272 +               }
27273 +               fallthrough;
27274 +       case AuWbrCreate_MFS:
27275 +       case AuWbrCreate_PMFS:
27276 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27277 +               break;
27278 +       case AuWbrCreate_MFSV:
27279 +       case AuWbrCreate_PMFSV:
27280 +               e = au_wbr_mfs_sec(&args[0], str, create);
27281 +               if (unlikely(e))
27282 +                       err = e;
27283 +               break;
27284 +       }
27285 +
27286 +       return err;
27287 +}
27288 +
27289 +const char *au_optstr_wbr_create(int wbr_create)
27290 +{
27291 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27292 +}
27293 +
27294 +static match_table_t au_wbr_copyup_policy = {
27295 +       {AuWbrCopyup_TDP, "tdp"},
27296 +       {AuWbrCopyup_TDP, "top-down-parent"},
27297 +       {AuWbrCopyup_BUP, "bup"},
27298 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27299 +       {AuWbrCopyup_BU, "bu"},
27300 +       {AuWbrCopyup_BU, "bottom-up"},
27301 +       {-1, NULL}
27302 +};
27303 +
27304 +int au_wbr_copyup_val(char *str)
27305 +{
27306 +       substring_t args[MAX_OPT_ARGS];
27307 +
27308 +       return match_token(str, au_wbr_copyup_policy, args);
27309 +}
27310 +
27311 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27312 +{
27313 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27314 +}
27315 +
27316 +/* ---------------------------------------------------------------------- */
27317 +
27318 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27319 +              aufs_bindex_t bindex)
27320 +{
27321 +       int err;
27322 +       struct au_opt_add *add = &opt->add;
27323 +       char *p;
27324 +
27325 +       add->bindex = bindex;
27326 +       add->perm = AuBrPerm_RO;
27327 +       add->pathname = opt_str;
27328 +       p = strchr(opt_str, '=');
27329 +       if (p) {
27330 +               *p++ = 0;
27331 +               if (*p)
27332 +                       add->perm = au_br_perm_val(p);
27333 +       }
27334 +
27335 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27336 +       if (!err) {
27337 +               if (!p) {
27338 +                       add->perm = AuBrPerm_RO;
27339 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27340 +                               add->perm = AuBrPerm_RR;
27341 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27342 +                               add->perm = AuBrPerm_RW;
27343 +               }
27344 +               opt->type = Opt_add;
27345 +               goto out;
27346 +       }
27347 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27348 +       err = -EINVAL;
27349 +
27350 +out:
27351 +       return err;
27352 +}
27353 +
27354 +static int au_opt_wbr_create(struct super_block *sb,
27355 +                            struct au_opt_wbr_create *create)
27356 +{
27357 +       int err;
27358 +       struct au_sbinfo *sbinfo;
27359 +
27360 +       SiMustWriteLock(sb);
27361 +
27362 +       err = 1; /* handled */
27363 +       sbinfo = au_sbi(sb);
27364 +       if (sbinfo->si_wbr_create_ops->fin) {
27365 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27366 +               if (!err)
27367 +                       err = 1;
27368 +       }
27369 +
27370 +       sbinfo->si_wbr_create = create->wbr_create;
27371 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27372 +       switch (create->wbr_create) {
27373 +       case AuWbrCreate_MFSRRV:
27374 +       case AuWbrCreate_MFSRR:
27375 +       case AuWbrCreate_TDMFS:
27376 +       case AuWbrCreate_TDMFSV:
27377 +       case AuWbrCreate_PMFSRR:
27378 +       case AuWbrCreate_PMFSRRV:
27379 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27380 +               fallthrough;
27381 +       case AuWbrCreate_MFS:
27382 +       case AuWbrCreate_MFSV:
27383 +       case AuWbrCreate_PMFS:
27384 +       case AuWbrCreate_PMFSV:
27385 +               sbinfo->si_wbr_mfs.mfs_expire
27386 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27387 +               break;
27388 +       }
27389 +
27390 +       if (sbinfo->si_wbr_create_ops->init)
27391 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27392 +
27393 +       return err;
27394 +}
27395 +
27396 +/*
27397 + * returns,
27398 + * plus: processed without an error
27399 + * zero: unprocessed
27400 + */
27401 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27402 +                        struct au_opts *opts)
27403 +{
27404 +       int err;
27405 +       struct au_sbinfo *sbinfo;
27406 +
27407 +       SiMustWriteLock(sb);
27408 +
27409 +       err = 1; /* handled */
27410 +       sbinfo = au_sbi(sb);
27411 +       switch (opt->type) {
27412 +       case Opt_udba:
27413 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27414 +               sbinfo->si_mntflags |= opt->udba;
27415 +               opts->given_udba |= opt->udba;
27416 +               break;
27417 +
27418 +       case Opt_plink:
27419 +               if (opt->tf)
27420 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27421 +               else {
27422 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27423 +                               au_plink_put(sb, /*verbose*/1);
27424 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27425 +               }
27426 +               break;
27427 +       case Opt_list_plink:
27428 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27429 +                       au_plink_list(sb);
27430 +               break;
27431 +
27432 +       case Opt_dio:
27433 +               if (opt->tf) {
27434 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27435 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27436 +               } else {
27437 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27438 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27439 +               }
27440 +               break;
27441 +
27442 +       case Opt_fhsm_sec:
27443 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27444 +               break;
27445 +
27446 +       case Opt_diropq_a:
27447 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27448 +               break;
27449 +       case Opt_diropq_w:
27450 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27451 +               break;
27452 +
27453 +       case Opt_warn_perm:
27454 +               if (opt->tf)
27455 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27456 +               else
27457 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27458 +               break;
27459 +
27460 +       case Opt_verbose:
27461 +               if (opt->tf)
27462 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27463 +               else
27464 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27465 +               break;
27466 +
27467 +       case Opt_sum:
27468 +               if (opt->tf)
27469 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27470 +               else {
27471 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27472 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27473 +               }
27474 +               break;
27475 +       case Opt_wsum:
27476 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27477 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27478 +               break;
27479 +
27480 +       case Opt_wbr_create:
27481 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27482 +               break;
27483 +       case Opt_wbr_copyup:
27484 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27485 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27486 +               break;
27487 +
27488 +       case Opt_dirwh:
27489 +               sbinfo->si_dirwh = opt->dirwh;
27490 +               break;
27491 +
27492 +       case Opt_rdcache:
27493 +               sbinfo->si_rdcache
27494 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27495 +               break;
27496 +       case Opt_rdblk:
27497 +               sbinfo->si_rdblk = opt->rdblk;
27498 +               break;
27499 +       case Opt_rdhash:
27500 +               sbinfo->si_rdhash = opt->rdhash;
27501 +               break;
27502 +
27503 +       case Opt_shwh:
27504 +               if (opt->tf)
27505 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27506 +               else
27507 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27508 +               break;
27509 +
27510 +       case Opt_dirperm1:
27511 +               if (opt->tf)
27512 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27513 +               else
27514 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27515 +               break;
27516 +
27517 +       case Opt_trunc_xino:
27518 +               if (opt->tf)
27519 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27520 +               else
27521 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27522 +               break;
27523 +
27524 +       case Opt_trunc_xino_path:
27525 +       case Opt_itrunc_xino:
27526 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27527 +                                   /*idx_begin*/0);
27528 +               if (!err)
27529 +                       err = 1;
27530 +               break;
27531 +
27532 +       case Opt_trunc_xib:
27533 +               if (opt->tf)
27534 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27535 +               else
27536 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27537 +               break;
27538 +
27539 +       case Opt_dirren:
27540 +               err = 1;
27541 +               if (opt->tf) {
27542 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27543 +                               err = au_dr_opt_set(sb);
27544 +                               if (!err)
27545 +                                       err = 1;
27546 +                       }
27547 +                       if (err == 1)
27548 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27549 +               } else {
27550 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27551 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27552 +                                                                     DR_FLUSHED));
27553 +                               if (!err)
27554 +                                       err = 1;
27555 +                       }
27556 +                       if (err == 1)
27557 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27558 +               }
27559 +               break;
27560 +
27561 +       case Opt_acl:
27562 +               if (opt->tf)
27563 +                       sb->s_flags |= SB_POSIXACL;
27564 +               else
27565 +                       sb->s_flags &= ~SB_POSIXACL;
27566 +               break;
27567 +
27568 +       default:
27569 +               err = 0;
27570 +               break;
27571 +       }
27572 +
27573 +       return err;
27574 +}
27575 +
27576 +/*
27577 + * returns tri-state.
27578 + * plus: processed without an error
27579 + * zero: unprocessed
27580 + * minus: error
27581 + */
27582 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27583 +                    struct au_opts *opts)
27584 +{
27585 +       int err, do_refresh;
27586 +
27587 +       err = 0;
27588 +       switch (opt->type) {
27589 +       case Opt_append:
27590 +               opt->add.bindex = au_sbbot(sb) + 1;
27591 +               if (opt->add.bindex < 0)
27592 +                       opt->add.bindex = 0;
27593 +               goto add;
27594 +               /* Always goto add, not fallthrough */
27595 +       case Opt_prepend:
27596 +               opt->add.bindex = 0;
27597 +               fallthrough;
27598 +       case Opt_add:
27599 +       add: /* indented label */
27600 +               err = au_br_add(sb, &opt->add,
27601 +                               au_ftest_opts(opts->flags, REMOUNT));
27602 +               if (!err) {
27603 +                       err = 1;
27604 +                       au_fset_opts(opts->flags, REFRESH);
27605 +               }
27606 +               break;
27607 +
27608 +       case Opt_del:
27609 +       case Opt_idel:
27610 +               err = au_br_del(sb, &opt->del,
27611 +                               au_ftest_opts(opts->flags, REMOUNT));
27612 +               if (!err) {
27613 +                       err = 1;
27614 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27615 +                       au_fset_opts(opts->flags, REFRESH);
27616 +               }
27617 +               break;
27618 +
27619 +       case Opt_mod:
27620 +       case Opt_imod:
27621 +               err = au_br_mod(sb, &opt->mod,
27622 +                               au_ftest_opts(opts->flags, REMOUNT),
27623 +                               &do_refresh);
27624 +               if (!err) {
27625 +                       err = 1;
27626 +                       if (do_refresh)
27627 +                               au_fset_opts(opts->flags, REFRESH);
27628 +               }
27629 +               break;
27630 +       }
27631 +       return err;
27632 +}
27633 +
27634 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27635 +                      struct au_opt_xino **opt_xino,
27636 +                      struct au_opts *opts)
27637 +{
27638 +       int err;
27639 +
27640 +       err = 0;
27641 +       switch (opt->type) {
27642 +       case Opt_xino:
27643 +               err = au_xino_set(sb, &opt->xino,
27644 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27645 +               if (!err)
27646 +                       *opt_xino = &opt->xino;
27647 +               break;
27648 +       case Opt_noxino:
27649 +               au_xino_clr(sb);
27650 +               *opt_xino = (void *)-1;
27651 +               break;
27652 +       }
27653 +
27654 +       return err;
27655 +}
27656 +
27657 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27658 +                  unsigned int pending)
27659 +{
27660 +       int err, fhsm;
27661 +       aufs_bindex_t bindex, bbot;
27662 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27663 +       struct au_branch *br;
27664 +       struct au_wbr *wbr;
27665 +       struct dentry *root, *dentry;
27666 +       struct inode *dir, *h_dir;
27667 +       struct au_sbinfo *sbinfo;
27668 +       struct au_hinode *hdir;
27669 +
27670 +       SiMustAnyLock(sb);
27671 +
27672 +       sbinfo = au_sbi(sb);
27673 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27674 +
27675 +       if (!(sb_flags & SB_RDONLY)) {
27676 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27677 +                       pr_warn("first branch should be rw\n");
27678 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27679 +                       pr_warn_once("shwh should be used with ro\n");
27680 +       }
27681 +
27682 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27683 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27684 +               pr_warn_once("udba=*notify requires xino\n");
27685 +
27686 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27687 +               pr_warn_once("dirperm1 breaks the protection"
27688 +                            " by the permission bits on the lower branch\n");
27689 +
27690 +       err = 0;
27691 +       fhsm = 0;
27692 +       root = sb->s_root;
27693 +       dir = d_inode(root);
27694 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27695 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27696 +                                     UDBA_NONE);
27697 +       bbot = au_sbbot(sb);
27698 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27699 +               skip = 0;
27700 +               h_dir = au_h_iptr(dir, bindex);
27701 +               br = au_sbr(sb, bindex);
27702 +
27703 +               if ((br->br_perm & AuBrAttr_ICEX)
27704 +                   && !h_dir->i_op->listxattr)
27705 +                       br->br_perm &= ~AuBrAttr_ICEX;
27706 +#if 0 /* untested */
27707 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27708 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27709 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27710 +#endif
27711 +
27712 +               do_free = 0;
27713 +               wbr = br->br_wbr;
27714 +               if (wbr)
27715 +                       wbr_wh_read_lock(wbr);
27716 +
27717 +               if (!au_br_writable(br->br_perm)) {
27718 +                       do_free = !!wbr;
27719 +                       skip = (!wbr
27720 +                               || (!wbr->wbr_whbase
27721 +                                   && !wbr->wbr_plink
27722 +                                   && !wbr->wbr_orph));
27723 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27724 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27725 +                       skip = (!wbr || !wbr->wbr_whbase);
27726 +                       if (skip && wbr) {
27727 +                               if (do_plink)
27728 +                                       skip = !!wbr->wbr_plink;
27729 +                               else
27730 +                                       skip = !wbr->wbr_plink;
27731 +                       }
27732 +               } else {
27733 +                       /* skip = (br->br_whbase && br->br_ohph); */
27734 +                       skip = (wbr && wbr->wbr_whbase);
27735 +                       if (skip) {
27736 +                               if (do_plink)
27737 +                                       skip = !!wbr->wbr_plink;
27738 +                               else
27739 +                                       skip = !wbr->wbr_plink;
27740 +                       }
27741 +               }
27742 +               if (wbr)
27743 +                       wbr_wh_read_unlock(wbr);
27744 +
27745 +               if (can_no_dreval) {
27746 +                       dentry = br->br_path.dentry;
27747 +                       spin_lock(&dentry->d_lock);
27748 +                       if (dentry->d_flags &
27749 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27750 +                               can_no_dreval = 0;
27751 +                       spin_unlock(&dentry->d_lock);
27752 +               }
27753 +
27754 +               if (au_br_fhsm(br->br_perm)) {
27755 +                       fhsm++;
27756 +                       AuDebugOn(!br->br_fhsm);
27757 +               }
27758 +
27759 +               if (skip)
27760 +                       continue;
27761 +
27762 +               hdir = au_hi(dir, bindex);
27763 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27764 +               if (wbr)
27765 +                       wbr_wh_write_lock(wbr);
27766 +               err = au_wh_init(br, sb);
27767 +               if (wbr)
27768 +                       wbr_wh_write_unlock(wbr);
27769 +               au_hn_inode_unlock(hdir);
27770 +
27771 +               if (!err && do_free) {
27772 +                       au_kfree_rcu(wbr);
27773 +                       br->br_wbr = NULL;
27774 +               }
27775 +       }
27776 +
27777 +       if (can_no_dreval)
27778 +               au_fset_si(sbinfo, NO_DREVAL);
27779 +       else
27780 +               au_fclr_si(sbinfo, NO_DREVAL);
27781 +
27782 +       if (fhsm >= 2) {
27783 +               au_fset_si(sbinfo, FHSM);
27784 +               for (bindex = bbot; bindex >= 0; bindex--) {
27785 +                       br = au_sbr(sb, bindex);
27786 +                       if (au_br_fhsm(br->br_perm)) {
27787 +                               au_fhsm_set_bottom(sb, bindex);
27788 +                               break;
27789 +                       }
27790 +               }
27791 +       } else {
27792 +               au_fclr_si(sbinfo, FHSM);
27793 +               au_fhsm_set_bottom(sb, -1);
27794 +       }
27795 +
27796 +       return err;
27797 +}
27798 +
27799 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27800 +{
27801 +       int err;
27802 +       unsigned int tmp;
27803 +       aufs_bindex_t bindex, bbot;
27804 +       struct au_opt *opt;
27805 +       struct au_opt_xino *opt_xino, xino;
27806 +       struct au_sbinfo *sbinfo;
27807 +       struct au_branch *br;
27808 +       struct inode *dir;
27809 +
27810 +       SiMustWriteLock(sb);
27811 +
27812 +       err = 0;
27813 +       opt_xino = NULL;
27814 +       opt = opts->opt;
27815 +       while (err >= 0 && opt->type != Opt_tail)
27816 +               err = au_opt_simple(sb, opt++, opts);
27817 +       if (err > 0)
27818 +               err = 0;
27819 +       else if (unlikely(err < 0))
27820 +               goto out;
27821 +
27822 +       /* disable xino and udba temporary */
27823 +       sbinfo = au_sbi(sb);
27824 +       tmp = sbinfo->si_mntflags;
27825 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27826 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27827 +
27828 +       opt = opts->opt;
27829 +       while (err >= 0 && opt->type != Opt_tail)
27830 +               err = au_opt_br(sb, opt++, opts);
27831 +       if (err > 0)
27832 +               err = 0;
27833 +       else if (unlikely(err < 0))
27834 +               goto out;
27835 +
27836 +       bbot = au_sbbot(sb);
27837 +       if (unlikely(bbot < 0)) {
27838 +               err = -EINVAL;
27839 +               pr_err("no branches\n");
27840 +               goto out;
27841 +       }
27842 +
27843 +       if (au_opt_test(tmp, XINO))
27844 +               au_opt_set(sbinfo->si_mntflags, XINO);
27845 +       opt = opts->opt;
27846 +       while (!err && opt->type != Opt_tail)
27847 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27848 +       if (unlikely(err))
27849 +               goto out;
27850 +
27851 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27852 +       if (unlikely(err))
27853 +               goto out;
27854 +
27855 +       /* restore xino */
27856 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27857 +               xino.file = au_xino_def(sb);
27858 +               err = PTR_ERR(xino.file);
27859 +               if (IS_ERR(xino.file))
27860 +                       goto out;
27861 +
27862 +               err = au_xino_set(sb, &xino, /*remount*/0);
27863 +               fput(xino.file);
27864 +               if (unlikely(err))
27865 +                       goto out;
27866 +       }
27867 +
27868 +       /* restore udba */
27869 +       tmp &= AuOptMask_UDBA;
27870 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27871 +       sbinfo->si_mntflags |= tmp;
27872 +       bbot = au_sbbot(sb);
27873 +       for (bindex = 0; bindex <= bbot; bindex++) {
27874 +               br = au_sbr(sb, bindex);
27875 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27876 +               if (unlikely(err))
27877 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27878 +                               bindex, err);
27879 +               /* go on even if err */
27880 +       }
27881 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27882 +               dir = d_inode(sb->s_root);
27883 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27884 +       }
27885 +
27886 +out:
27887 +       return err;
27888 +}
27889 +
27890 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27891 +{
27892 +       int err, rerr;
27893 +       unsigned char no_dreval;
27894 +       struct inode *dir;
27895 +       struct au_opt_xino *opt_xino;
27896 +       struct au_opt *opt;
27897 +       struct au_sbinfo *sbinfo;
27898 +
27899 +       SiMustWriteLock(sb);
27900 +
27901 +       err = au_dr_opt_flush(sb);
27902 +       if (unlikely(err))
27903 +               goto out;
27904 +       au_fset_opts(opts->flags, DR_FLUSHED);
27905 +
27906 +       dir = d_inode(sb->s_root);
27907 +       sbinfo = au_sbi(sb);
27908 +       opt_xino = NULL;
27909 +       opt = opts->opt;
27910 +       while (err >= 0 && opt->type != Opt_tail) {
27911 +               err = au_opt_simple(sb, opt, opts);
27912 +               if (!err)
27913 +                       err = au_opt_br(sb, opt, opts);
27914 +               if (!err)
27915 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27916 +               opt++;
27917 +       }
27918 +       if (err > 0)
27919 +               err = 0;
27920 +       AuTraceErr(err);
27921 +       /* go on even err */
27922 +
27923 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27924 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27925 +       if (unlikely(rerr && !err))
27926 +               err = rerr;
27927 +
27928 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27929 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27930 +
27931 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27932 +               rerr = au_xib_trunc(sb);
27933 +               if (unlikely(rerr && !err))
27934 +                       err = rerr;
27935 +       }
27936 +
27937 +       /* will be handled by the caller */
27938 +       if (!au_ftest_opts(opts->flags, REFRESH)
27939 +           && (opts->given_udba
27940 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27941 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27942 +                   ))
27943 +               au_fset_opts(opts->flags, REFRESH);
27944 +
27945 +       AuDbg("status 0x%x\n", opts->flags);
27946 +
27947 +out:
27948 +       return err;
27949 +}
27950 +
27951 +/* ---------------------------------------------------------------------- */
27952 +
27953 +unsigned int au_opt_udba(struct super_block *sb)
27954 +{
27955 +       return au_mntflags(sb) & AuOptMask_UDBA;
27956 +}
27957 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27958 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27959 +++ linux/fs/aufs/opts.h        2024-03-10 23:40:47.086476782 +0100
27960 @@ -0,0 +1,263 @@
27961 +/* SPDX-License-Identifier: GPL-2.0 */
27962 +/*
27963 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27964 + *
27965 + * This program is free software; you can redistribute it and/or modify
27966 + * it under the terms of the GNU General Public License as published by
27967 + * the Free Software Foundation; either version 2 of the License, or
27968 + * (at your option) any later version.
27969 + *
27970 + * This program is distributed in the hope that it will be useful,
27971 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27972 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27973 + * GNU General Public License for more details.
27974 + *
27975 + * You should have received a copy of the GNU General Public License
27976 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27977 + */
27978 +
27979 +/*
27980 + * mount options/flags
27981 + */
27982 +
27983 +#ifndef __AUFS_OPTS_H__
27984 +#define __AUFS_OPTS_H__
27985 +
27986 +#ifdef __KERNEL__
27987 +
27988 +#include <linux/fs_parser.h>
27989 +#include <linux/namei.h>
27990 +#include <linux/path.h>
27991 +
27992 +enum {
27993 +       Opt_br,
27994 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27995 +       Opt_idel, Opt_imod,
27996 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27997 +       Opt_xino, Opt_noxino,
27998 +       Opt_trunc_xino, Opt_trunc_xino_v,
27999 +       Opt_trunc_xino_path, Opt_itrunc_xino,
28000 +       Opt_trunc_xib,
28001 +       Opt_shwh,
28002 +       Opt_plink, Opt_list_plink,
28003 +       Opt_udba,
28004 +       Opt_dio,
28005 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
28006 +       Opt_warn_perm,
28007 +       Opt_wbr_copyup, Opt_wbr_create,
28008 +       Opt_fhsm_sec,
28009 +       Opt_verbose, Opt_noverbose,
28010 +       Opt_sum, Opt_wsum,
28011 +       Opt_dirperm1,
28012 +       Opt_dirren,
28013 +       Opt_acl,
28014 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
28015 +};
28016 +
28017 +/* ---------------------------------------------------------------------- */
28018 +
28019 +/* mount flags */
28020 +#define AuOpt_XINO             BIT(0)          /* external inode number bitmap
28021 +                                                  and translation table */
28022 +#define AuOpt_TRUNC_XINO       BIT(1)          /* truncate xino files */
28023 +#define AuOpt_UDBA_NONE                BIT(2)          /* users direct branch access */
28024 +#define AuOpt_UDBA_REVAL       BIT(3)
28025 +#define AuOpt_UDBA_HNOTIFY     BIT(4)
28026 +#define AuOpt_SHWH             BIT(5)          /* show whiteout */
28027 +#define AuOpt_PLINK            BIT(6)          /* pseudo-link */
28028 +#define AuOpt_DIRPERM1         BIT(7)          /* ignore the lower dir's perm
28029 +                                                  bits */
28030 +#define AuOpt_ALWAYS_DIROPQ    BIT(8)          /* policy to creating diropq */
28031 +#define AuOpt_SUM              BIT(9)          /* summation for statfs(2) */
28032 +#define AuOpt_SUM_W            BIT(10)         /* unimplemented */
28033 +#define AuOpt_WARN_PERM                BIT(11)         /* warn when add-branch */
28034 +#define AuOpt_VERBOSE          BIT(12)         /* print the cause of error */
28035 +#define AuOpt_DIO              BIT(13)         /* direct io */
28036 +#define AuOpt_DIRREN           BIT(14)         /* directory rename */
28037 +
28038 +#ifndef CONFIG_AUFS_HNOTIFY
28039 +#undef AuOpt_UDBA_HNOTIFY
28040 +#define AuOpt_UDBA_HNOTIFY     0
28041 +#endif
28042 +#ifndef CONFIG_AUFS_DIRREN
28043 +#undef AuOpt_DIRREN
28044 +#define AuOpt_DIRREN           0
28045 +#endif
28046 +#ifndef CONFIG_AUFS_SHWH
28047 +#undef AuOpt_SHWH
28048 +#define AuOpt_SHWH             0
28049 +#endif
28050 +
28051 +#define AuOpt_Def      (AuOpt_XINO \
28052 +                        | AuOpt_UDBA_REVAL \
28053 +                        | AuOpt_PLINK \
28054 +                        /* | AuOpt_DIRPERM1 */ \
28055 +                        | AuOpt_WARN_PERM)
28056 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28057 +                        | AuOpt_UDBA_REVAL \
28058 +                        | AuOpt_UDBA_HNOTIFY)
28059 +
28060 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28061 +
28062 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28063 +#define au_opt_set(flags, name) do { \
28064 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28065 +       ((flags) |= AuOpt_##name); \
28066 +} while (0)
28067 +#define au_opt_set_udba(flags, name) do { \
28068 +       (flags) &= ~AuOptMask_UDBA; \
28069 +       ((flags) |= AuOpt_##name); \
28070 +} while (0)
28071 +#define au_opt_clr(flags, name) do { \
28072 +       ((flags) &= ~AuOpt_##name); \
28073 +} while (0)
28074 +
28075 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28076 +{
28077 +#ifdef CONFIG_PROC_FS
28078 +       return mntflags;
28079 +#else
28080 +       return mntflags & ~AuOpt_PLINK;
28081 +#endif
28082 +}
28083 +
28084 +/* ---------------------------------------------------------------------- */
28085 +
28086 +/* policies to select one among multiple writable branches */
28087 +enum {
28088 +       AuWbrCreate_TDP,        /* top down parent */
28089 +       AuWbrCreate_RR,         /* round robin */
28090 +       AuWbrCreate_MFS,        /* most free space */
28091 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28092 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28093 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28094 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28095 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28096 +       AuWbrCreate_PMFS,       /* parent and mfs */
28097 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28098 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28099 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28100 +
28101 +       AuWbrCreate_Def = AuWbrCreate_TDP
28102 +};
28103 +
28104 +enum {
28105 +       AuWbrCopyup_TDP,        /* top down parent */
28106 +       AuWbrCopyup_BUP,        /* bottom up parent */
28107 +       AuWbrCopyup_BU,         /* bottom up */
28108 +
28109 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28110 +};
28111 +
28112 +/* ---------------------------------------------------------------------- */
28113 +
28114 +struct file;
28115 +
28116 +struct au_opt_add {
28117 +       aufs_bindex_t   bindex;
28118 +       char            *pathname;
28119 +       int             perm;
28120 +       struct path     path;
28121 +};
28122 +
28123 +struct au_opt_del {
28124 +       char            *pathname;
28125 +       struct path     h_path;
28126 +};
28127 +
28128 +struct au_opt_mod {
28129 +       char            *path;
28130 +       int             perm;
28131 +       struct dentry   *h_root;
28132 +};
28133 +
28134 +struct au_opt_xino {
28135 +       char            *path;
28136 +       struct file     *file;
28137 +};
28138 +
28139 +struct au_opt_xino_itrunc {
28140 +       aufs_bindex_t   bindex;
28141 +};
28142 +
28143 +struct au_opt_wbr_create {
28144 +       int                     wbr_create;
28145 +       int                     mfs_second;
28146 +       unsigned long long      mfsrr_watermark;
28147 +};
28148 +
28149 +struct au_opt {
28150 +       int type;
28151 +       union {
28152 +               struct au_opt_xino      xino;
28153 +               struct au_opt_xino_itrunc xino_itrunc;
28154 +               struct au_opt_add       add;
28155 +               struct au_opt_del       del;
28156 +               struct au_opt_mod       mod;
28157 +               int                     dirwh;
28158 +               int                     rdcache;
28159 +               unsigned int            rdblk;
28160 +               unsigned int            rdhash;
28161 +               int                     udba;
28162 +               struct au_opt_wbr_create wbr_create;
28163 +               int                     wbr_copyup;
28164 +               unsigned int            fhsm_second;
28165 +               bool                    tf; /* generic flag, true or false */
28166 +       };
28167 +};
28168 +
28169 +/* opts flags */
28170 +#define AuOpts_REMOUNT         BIT(0)
28171 +#define AuOpts_REFRESH         BIT(1)
28172 +#define AuOpts_TRUNC_XIB       BIT(2)
28173 +#define AuOpts_REFRESH_DYAOP   BIT(3)
28174 +#define AuOpts_REFRESH_IDOP    BIT(4)
28175 +#define AuOpts_DR_FLUSHED      BIT(5)
28176 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28177 +#define au_fset_opts(flags, name) \
28178 +       do { (flags) |= AuOpts_##name; } while (0)
28179 +#define au_fclr_opts(flags, name) \
28180 +       do { (flags) &= ~AuOpts_##name; } while (0)
28181 +
28182 +#ifndef CONFIG_AUFS_DIRREN
28183 +#undef AuOpts_DR_FLUSHED
28184 +#define AuOpts_DR_FLUSHED      0
28185 +#endif
28186 +
28187 +struct au_opts {
28188 +       struct au_opt   *opt;
28189 +       int             max_opt;
28190 +
28191 +       unsigned int    given_udba;
28192 +       unsigned int    flags;
28193 +       unsigned long   sb_flags;
28194 +};
28195 +
28196 +/* ---------------------------------------------------------------------- */
28197 +
28198 +/* opts.c */
28199 +int au_br_perm_val(char *perm);
28200 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28201 +int au_udba_val(char *str);
28202 +const char *au_optstr_udba(int udba);
28203 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28204 +const char *au_optstr_wbr_create(int wbr_create);
28205 +int au_wbr_copyup_val(char *str);
28206 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28207 +
28208 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28209 +              aufs_bindex_t bindex);
28210 +struct super_block;
28211 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28212 +                  unsigned int pending);
28213 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28214 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28215 +
28216 +unsigned int au_opt_udba(struct super_block *sb);
28217 +
28218 +/* fsctx.c */
28219 +int aufs_fsctx_init(struct fs_context *fc);
28220 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28221 +
28222 +#endif /* __KERNEL__ */
28223 +#endif /* __AUFS_OPTS_H__ */
28224 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28225 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28226 +++ linux/fs/aufs/plink.c       2022-11-05 23:02:18.969222617 +0100
28227 @@ -0,0 +1,516 @@
28228 +// SPDX-License-Identifier: GPL-2.0
28229 +/*
28230 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28231 + *
28232 + * This program is free software; you can redistribute it and/or modify
28233 + * it under the terms of the GNU General Public License as published by
28234 + * the Free Software Foundation; either version 2 of the License, or
28235 + * (at your option) any later version.
28236 + *
28237 + * This program is distributed in the hope that it will be useful,
28238 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28239 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28240 + * GNU General Public License for more details.
28241 + *
28242 + * You should have received a copy of the GNU General Public License
28243 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28244 + */
28245 +
28246 +/*
28247 + * pseudo-link
28248 + */
28249 +
28250 +#include "aufs.h"
28251 +
28252 +/*
28253 + * the pseudo-link maintenance mode.
28254 + * during a user process maintains the pseudo-links,
28255 + * prohibit adding a new plink and branch manipulation.
28256 + *
28257 + * Flags
28258 + * NOPLM:
28259 + *     For entry functions which will handle plink, and i_mutex is already held
28260 + *     in VFS.
28261 + *     They cannot wait and should return an error at once.
28262 + *     Callers has to check the error.
28263 + * NOPLMW:
28264 + *     For entry functions which will handle plink, but i_mutex is not held
28265 + *     in VFS.
28266 + *     They can wait the plink maintenance mode to finish.
28267 + *
28268 + * They behave like F_SETLK and F_SETLKW.
28269 + * If the caller never handle plink, then both flags are unnecessary.
28270 + */
28271 +
28272 +int au_plink_maint(struct super_block *sb, int flags)
28273 +{
28274 +       int err;
28275 +       pid_t pid, ppid;
28276 +       struct task_struct *parent, *prev;
28277 +       struct au_sbinfo *sbi;
28278 +
28279 +       SiMustAnyLock(sb);
28280 +
28281 +       err = 0;
28282 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28283 +               goto out;
28284 +
28285 +       sbi = au_sbi(sb);
28286 +       pid = sbi->si_plink_maint_pid;
28287 +       if (!pid || pid == current->pid)
28288 +               goto out;
28289 +
28290 +       /* todo: it highly depends upon /sbin/mount.aufs */
28291 +       prev = NULL;
28292 +       parent = current;
28293 +       ppid = 0;
28294 +       rcu_read_lock();
28295 +       while (1) {
28296 +               parent = rcu_dereference(parent->real_parent);
28297 +               if (parent == prev)
28298 +                       break;
28299 +               ppid = task_pid_vnr(parent);
28300 +               if (pid == ppid) {
28301 +                       rcu_read_unlock();
28302 +                       goto out;
28303 +               }
28304 +               prev = parent;
28305 +       }
28306 +       rcu_read_unlock();
28307 +
28308 +       if (au_ftest_lock(flags, NOPLMW)) {
28309 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28310 +               /* AuDebugOn(!lockdep_depth(current)); */
28311 +               while (sbi->si_plink_maint_pid) {
28312 +                       si_read_unlock(sb);
28313 +                       /* gave up wake_up_bit() */
28314 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28315 +
28316 +                       if (au_ftest_lock(flags, FLUSH))
28317 +                               au_nwt_flush(&sbi->si_nowait);
28318 +                       si_noflush_read_lock(sb);
28319 +               }
28320 +       } else if (au_ftest_lock(flags, NOPLM)) {
28321 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28322 +               err = -EAGAIN;
28323 +       }
28324 +
28325 +out:
28326 +       return err;
28327 +}
28328 +
28329 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28330 +{
28331 +       spin_lock(&sbinfo->si_plink_maint_lock);
28332 +       sbinfo->si_plink_maint_pid = 0;
28333 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28334 +       wake_up_all(&sbinfo->si_plink_wq);
28335 +}
28336 +
28337 +int au_plink_maint_enter(struct super_block *sb)
28338 +{
28339 +       int err;
28340 +       struct au_sbinfo *sbinfo;
28341 +
28342 +       err = 0;
28343 +       sbinfo = au_sbi(sb);
28344 +       /* make sure i am the only one in this fs */
28345 +       si_write_lock(sb, AuLock_FLUSH);
28346 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28347 +               spin_lock(&sbinfo->si_plink_maint_lock);
28348 +               if (!sbinfo->si_plink_maint_pid)
28349 +                       sbinfo->si_plink_maint_pid = current->pid;
28350 +               else
28351 +                       err = -EBUSY;
28352 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28353 +       }
28354 +       si_write_unlock(sb);
28355 +
28356 +       return err;
28357 +}
28358 +
28359 +/* ---------------------------------------------------------------------- */
28360 +
28361 +#ifdef CONFIG_AUFS_DEBUG
28362 +void au_plink_list(struct super_block *sb)
28363 +{
28364 +       int i;
28365 +       struct au_sbinfo *sbinfo;
28366 +       struct hlist_bl_head *hbl;
28367 +       struct hlist_bl_node *pos;
28368 +       struct au_icntnr *icntnr;
28369 +
28370 +       SiMustAnyLock(sb);
28371 +
28372 +       sbinfo = au_sbi(sb);
28373 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28374 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28375 +
28376 +       for (i = 0; i < AuPlink_NHASH; i++) {
28377 +               hbl = sbinfo->si_plink + i;
28378 +               hlist_bl_lock(hbl);
28379 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28380 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28381 +               hlist_bl_unlock(hbl);
28382 +       }
28383 +}
28384 +#endif
28385 +
28386 +/* is the inode pseudo-linked? */
28387 +int au_plink_test(struct inode *inode)
28388 +{
28389 +       int found, i;
28390 +       struct au_sbinfo *sbinfo;
28391 +       struct hlist_bl_head *hbl;
28392 +       struct hlist_bl_node *pos;
28393 +       struct au_icntnr *icntnr;
28394 +
28395 +       sbinfo = au_sbi(inode->i_sb);
28396 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28397 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28398 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28399 +
28400 +       found = 0;
28401 +       i = au_plink_hash(inode->i_ino);
28402 +       hbl =  sbinfo->si_plink + i;
28403 +       hlist_bl_lock(hbl);
28404 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28405 +               if (&icntnr->vfs_inode == inode) {
28406 +                       found = 1;
28407 +                       break;
28408 +               }
28409 +       hlist_bl_unlock(hbl);
28410 +       return found;
28411 +}
28412 +
28413 +/* ---------------------------------------------------------------------- */
28414 +
28415 +/*
28416 + * generate a name for plink.
28417 + * the file will be stored under AUFS_WH_PLINKDIR.
28418 + */
28419 +/* 20 is max digits length of ulong 64 */
28420 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28421 +
28422 +static int plink_name(char *name, int len, struct inode *inode,
28423 +                     aufs_bindex_t bindex)
28424 +{
28425 +       int rlen;
28426 +       struct inode *h_inode;
28427 +
28428 +       h_inode = au_h_iptr(inode, bindex);
28429 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28430 +       return rlen;
28431 +}
28432 +
28433 +struct au_do_plink_lkup_args {
28434 +       struct dentry **errp;
28435 +       struct qstr *tgtname;
28436 +       struct path *h_ppath;
28437 +};
28438 +
28439 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28440 +                                      struct path *h_ppath)
28441 +{
28442 +       struct dentry *h_dentry;
28443 +       struct inode *h_inode;
28444 +
28445 +       h_inode = d_inode(h_ppath->dentry);
28446 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28447 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28448 +       inode_unlock_shared(h_inode);
28449 +
28450 +       return h_dentry;
28451 +}
28452 +
28453 +static void au_call_do_plink_lkup(void *args)
28454 +{
28455 +       struct au_do_plink_lkup_args *a = args;
28456 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28457 +}
28458 +
28459 +/* lookup the plink-ed @inode under the branch at @bindex */
28460 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28461 +{
28462 +       struct dentry *h_dentry;
28463 +       struct au_branch *br;
28464 +       struct path h_ppath;
28465 +       int wkq_err;
28466 +       char a[PLINK_NAME_LEN];
28467 +       struct qstr tgtname = QSTR_INIT(a, 0);
28468 +
28469 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28470 +
28471 +       br = au_sbr(inode->i_sb, bindex);
28472 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28473 +       h_ppath.mnt = au_br_mnt(br);
28474 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28475 +
28476 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28477 +               struct au_do_plink_lkup_args args = {
28478 +                       .errp           = &h_dentry,
28479 +                       .tgtname        = &tgtname,
28480 +                       .h_ppath        = &h_ppath
28481 +               };
28482 +
28483 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28484 +               if (unlikely(wkq_err))
28485 +                       h_dentry = ERR_PTR(wkq_err);
28486 +       } else
28487 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28488 +
28489 +       return h_dentry;
28490 +}
28491 +
28492 +/* create a pseudo-link */
28493 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28494 +                     struct dentry *h_dentry)
28495 +{
28496 +       int err;
28497 +       struct path h_path;
28498 +       struct inode *h_dir, *delegated;
28499 +
28500 +       h_dir = d_inode(h_ppath->dentry);
28501 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28502 +       h_path.mnt = h_ppath->mnt;
28503 +again:
28504 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28505 +       err = PTR_ERR(h_path.dentry);
28506 +       if (IS_ERR(h_path.dentry))
28507 +               goto out;
28508 +
28509 +       err = 0;
28510 +       /* wh.plink dir is not monitored */
28511 +       /* todo: is it really safe? */
28512 +       if (d_is_positive(h_path.dentry)
28513 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28514 +               delegated = NULL;
28515 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28516 +               if (unlikely(err == -EWOULDBLOCK)) {
28517 +                       pr_warn("cannot retry for NFSv4 delegation"
28518 +                               " for an internal unlink\n");
28519 +                       iput(delegated);
28520 +               }
28521 +               dput(h_path.dentry);
28522 +               h_path.dentry = NULL;
28523 +               if (!err)
28524 +                       goto again;
28525 +       }
28526 +       if (!err && d_is_negative(h_path.dentry)) {
28527 +               delegated = NULL;
28528 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28529 +               if (unlikely(err == -EWOULDBLOCK)) {
28530 +                       pr_warn("cannot retry for NFSv4 delegation"
28531 +                               " for an internal link\n");
28532 +                       iput(delegated);
28533 +               }
28534 +       }
28535 +       dput(h_path.dentry);
28536 +
28537 +out:
28538 +       inode_unlock(h_dir);
28539 +       return err;
28540 +}
28541 +
28542 +struct do_whplink_args {
28543 +       int *errp;
28544 +       struct qstr *tgt;
28545 +       struct path *h_ppath;
28546 +       struct dentry *h_dentry;
28547 +};
28548 +
28549 +static void call_do_whplink(void *args)
28550 +{
28551 +       struct do_whplink_args *a = args;
28552 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28553 +}
28554 +
28555 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28556 +                  aufs_bindex_t bindex)
28557 +{
28558 +       int err, wkq_err;
28559 +       struct au_branch *br;
28560 +       struct au_wbr *wbr;
28561 +       struct path h_ppath;
28562 +       char a[PLINK_NAME_LEN];
28563 +       struct qstr tgtname = QSTR_INIT(a, 0);
28564 +
28565 +       br = au_sbr(inode->i_sb, bindex);
28566 +       wbr = br->br_wbr;
28567 +       h_ppath.dentry = wbr->wbr_plink;
28568 +       h_ppath.mnt = au_br_mnt(br);
28569 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28570 +
28571 +       /* always superio. */
28572 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28573 +               struct do_whplink_args args = {
28574 +                       .errp           = &err,
28575 +                       .tgt            = &tgtname,
28576 +                       .h_ppath        = &h_ppath,
28577 +                       .h_dentry       = h_dentry
28578 +               };
28579 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28580 +               if (unlikely(wkq_err))
28581 +                       err = wkq_err;
28582 +       } else
28583 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28584 +
28585 +       return err;
28586 +}
28587 +
28588 +/*
28589 + * create a new pseudo-link for @h_dentry on @bindex.
28590 + * the linked inode is held in aufs @inode.
28591 + */
28592 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28593 +                    struct dentry *h_dentry)
28594 +{
28595 +       struct super_block *sb;
28596 +       struct au_sbinfo *sbinfo;
28597 +       struct hlist_bl_head *hbl;
28598 +       struct hlist_bl_node *pos;
28599 +       struct au_icntnr *icntnr;
28600 +       int found, err, cnt, i;
28601 +
28602 +       sb = inode->i_sb;
28603 +       sbinfo = au_sbi(sb);
28604 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28605 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28606 +
28607 +       found = au_plink_test(inode);
28608 +       if (found)
28609 +               return;
28610 +
28611 +       i = au_plink_hash(inode->i_ino);
28612 +       hbl = sbinfo->si_plink + i;
28613 +       au_igrab(inode);
28614 +
28615 +       hlist_bl_lock(hbl);
28616 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28617 +               if (&icntnr->vfs_inode == inode) {
28618 +                       found = 1;
28619 +                       break;
28620 +               }
28621 +       }
28622 +       if (!found) {
28623 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28624 +               hlist_bl_add_head(&icntnr->plink, hbl);
28625 +       }
28626 +       hlist_bl_unlock(hbl);
28627 +       if (!found) {
28628 +               cnt = au_hbl_count(hbl);
28629 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28630 +               if (cnt > AUFS_PLINK_WARN)
28631 +                       AuWarn1(msg ", %d\n", cnt);
28632 +#undef msg
28633 +               err = whplink(h_dentry, inode, bindex);
28634 +               if (unlikely(err)) {
28635 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28636 +                       au_hbl_del(&icntnr->plink, hbl);
28637 +                       iput(&icntnr->vfs_inode);
28638 +               }
28639 +       } else
28640 +               iput(&icntnr->vfs_inode);
28641 +}
28642 +
28643 +/* free all plinks */
28644 +void au_plink_put(struct super_block *sb, int verbose)
28645 +{
28646 +       int i, warned;
28647 +       struct au_sbinfo *sbinfo;
28648 +       struct hlist_bl_head *hbl;
28649 +       struct hlist_bl_node *pos, *tmp;
28650 +       struct au_icntnr *icntnr;
28651 +
28652 +       SiMustWriteLock(sb);
28653 +
28654 +       sbinfo = au_sbi(sb);
28655 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28656 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28657 +
28658 +       /* no spin_lock since sbinfo is write-locked */
28659 +       warned = 0;
28660 +       for (i = 0; i < AuPlink_NHASH; i++) {
28661 +               hbl = sbinfo->si_plink + i;
28662 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28663 +                       pr_warn("pseudo-link is not flushed");
28664 +                       warned = 1;
28665 +               }
28666 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28667 +                       iput(&icntnr->vfs_inode);
28668 +               INIT_HLIST_BL_HEAD(hbl);
28669 +       }
28670 +}
28671 +
28672 +void au_plink_clean(struct super_block *sb, int verbose)
28673 +{
28674 +       struct dentry *root;
28675 +
28676 +       root = sb->s_root;
28677 +       aufs_write_lock(root);
28678 +       if (au_opt_test(au_mntflags(sb), PLINK))
28679 +               au_plink_put(sb, verbose);
28680 +       aufs_write_unlock(root);
28681 +}
28682 +
28683 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28684 +{
28685 +       int do_put;
28686 +       aufs_bindex_t btop, bbot, bindex;
28687 +
28688 +       do_put = 0;
28689 +       btop = au_ibtop(inode);
28690 +       bbot = au_ibbot(inode);
28691 +       if (btop >= 0) {
28692 +               for (bindex = btop; bindex <= bbot; bindex++) {
28693 +                       if (!au_h_iptr(inode, bindex)
28694 +                           || au_ii_br_id(inode, bindex) != br_id)
28695 +                               continue;
28696 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28697 +                       do_put = 1;
28698 +                       break;
28699 +               }
28700 +               if (do_put)
28701 +                       for (bindex = btop; bindex <= bbot; bindex++)
28702 +                               if (au_h_iptr(inode, bindex)) {
28703 +                                       do_put = 0;
28704 +                                       break;
28705 +                               }
28706 +       } else
28707 +               do_put = 1;
28708 +
28709 +       return do_put;
28710 +}
28711 +
28712 +/* free the plinks on a branch specified by @br_id */
28713 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28714 +{
28715 +       struct au_sbinfo *sbinfo;
28716 +       struct hlist_bl_head *hbl;
28717 +       struct hlist_bl_node *pos, *tmp;
28718 +       struct au_icntnr *icntnr;
28719 +       struct inode *inode;
28720 +       int i, do_put;
28721 +
28722 +       SiMustWriteLock(sb);
28723 +
28724 +       sbinfo = au_sbi(sb);
28725 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28726 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28727 +
28728 +       /* no bit_lock since sbinfo is write-locked */
28729 +       for (i = 0; i < AuPlink_NHASH; i++) {
28730 +               hbl = sbinfo->si_plink + i;
28731 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28732 +                       inode = au_igrab(&icntnr->vfs_inode);
28733 +                       ii_write_lock_child(inode);
28734 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28735 +                       if (do_put) {
28736 +                               hlist_bl_del(&icntnr->plink);
28737 +                               iput(inode);
28738 +                       }
28739 +                       ii_write_unlock(inode);
28740 +                       iput(inode);
28741 +               }
28742 +       }
28743 +}
28744 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28745 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28746 +++ linux/fs/aufs/poll.c        2022-11-05 23:02:18.969222617 +0100
28747 @@ -0,0 +1,51 @@
28748 +// SPDX-License-Identifier: GPL-2.0
28749 +/*
28750 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28751 + *
28752 + * This program is free software; you can redistribute it and/or modify
28753 + * it under the terms of the GNU General Public License as published by
28754 + * the Free Software Foundation; either version 2 of the License, or
28755 + * (at your option) any later version.
28756 + *
28757 + * This program is distributed in the hope that it will be useful,
28758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28760 + * GNU General Public License for more details.
28761 + *
28762 + * You should have received a copy of the GNU General Public License
28763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28764 + */
28765 +
28766 +/*
28767 + * poll operation
28768 + * There is only one filesystem which implements ->poll operation, currently.
28769 + */
28770 +
28771 +#include "aufs.h"
28772 +
28773 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28774 +{
28775 +       __poll_t mask;
28776 +       struct file *h_file;
28777 +       struct super_block *sb;
28778 +
28779 +       /* We should pretend an error happened. */
28780 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28781 +       sb = file->f_path.dentry->d_sb;
28782 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28783 +
28784 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28785 +       if (IS_ERR(h_file)) {
28786 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28787 +               goto out;
28788 +       }
28789 +
28790 +       mask = vfs_poll(h_file, pt);
28791 +       fput(h_file); /* instead of au_read_post() */
28792 +
28793 +out:
28794 +       si_read_unlock(sb);
28795 +       if (mask & EPOLLERR)
28796 +               AuDbg("mask 0x%x\n", mask);
28797 +       return mask;
28798 +}
28799 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28800 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28801 +++ linux/fs/aufs/posix_acl.c   2023-10-31 09:31:04.199880750 +0100
28802 @@ -0,0 +1,108 @@
28803 +// SPDX-License-Identifier: GPL-2.0
28804 +/*
28805 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28806 + *
28807 + * This program is free software; you can redistribute it and/or modify
28808 + * it under the terms of the GNU General Public License as published by
28809 + * the Free Software Foundation; either version 2 of the License, or
28810 + * (at your option) any later version.
28811 + *
28812 + * This program is distributed in the hope that it will be useful,
28813 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28814 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28815 + * GNU General Public License for more details.
28816 + *
28817 + * You should have received a copy of the GNU General Public License
28818 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28819 + */
28820 +
28821 +/*
28822 + * posix acl operations
28823 + */
28824 +
28825 +#include <linux/fs.h>
28826 +#include "aufs.h"
28827 +
28828 +struct posix_acl *aufs_get_inode_acl(struct inode *inode, int type, bool rcu)
28829 +{
28830 +       struct posix_acl *acl;
28831 +       int err;
28832 +       aufs_bindex_t bindex;
28833 +       struct inode *h_inode;
28834 +       struct super_block *sb;
28835 +
28836 +       acl = ERR_PTR(-ECHILD);
28837 +       if (rcu)
28838 +               goto out;
28839 +
28840 +       acl = NULL;
28841 +       sb = inode->i_sb;
28842 +       si_read_lock(sb, AuLock_FLUSH);
28843 +       ii_read_lock_child(inode);
28844 +       if (!(sb->s_flags & SB_POSIXACL))
28845 +               goto unlock;
28846 +
28847 +       bindex = au_ibtop(inode);
28848 +       h_inode = au_h_iptr(inode, bindex);
28849 +       if (unlikely(!h_inode
28850 +                    || ((h_inode->i_mode & S_IFMT)
28851 +                        != (inode->i_mode & S_IFMT)))) {
28852 +               err = au_busy_or_stale();
28853 +               acl = ERR_PTR(err);
28854 +               goto unlock;
28855 +       }
28856 +
28857 +       /* always topmost only */
28858 +       acl = get_inode_acl(h_inode, type);
28859 +       if (IS_ERR(acl))
28860 +               forget_cached_acl(inode, type);
28861 +       else
28862 +               set_cached_acl(inode, type, acl);
28863 +
28864 +unlock:
28865 +       ii_read_unlock(inode);
28866 +       si_read_unlock(sb);
28867 +
28868 +out:
28869 +       AuTraceErrPtr(acl);
28870 +       return acl;
28871 +}
28872 +
28873 +struct posix_acl *aufs_get_acl(struct mnt_idmap *idmap,
28874 +                              struct dentry *dentry, int type)
28875 +{
28876 +       struct posix_acl *acl;
28877 +       struct inode *inode;
28878 +
28879 +       inode = d_inode(dentry);
28880 +       acl = aufs_get_inode_acl(inode, type, /*rcu*/false);
28881 +
28882 +       return acl;
28883 +}
28884 +
28885 +int aufs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
28886 +                struct posix_acl *acl, int type)
28887 +{
28888 +       int err;
28889 +       ssize_t ssz;
28890 +       struct inode *inode;
28891 +       struct au_sxattr arg = {
28892 +               .type = AU_ACL_SET,
28893 +               .u.acl_set = {
28894 +                       .acl    = acl,
28895 +                       .type   = type
28896 +               },
28897 +       };
28898 +
28899 +       inode = d_inode(dentry);
28900 +       IMustLock(inode);
28901 +
28902 +       ssz = au_sxattr(dentry, inode, &arg);
28903 +       /* forget even it if succeeds since the branch might set differently */
28904 +       forget_cached_acl(inode, type);
28905 +       err = ssz;
28906 +       if (ssz >= 0)
28907 +               err = 0;
28908 +
28909 +       return err;
28910 +}
28911 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28912 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28913 +++ linux/fs/aufs/procfs.c      2022-11-05 23:02:18.969222617 +0100
28914 @@ -0,0 +1,170 @@
28915 +// SPDX-License-Identifier: GPL-2.0
28916 +/*
28917 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28918 + *
28919 + * This program is free software; you can redistribute it and/or modify
28920 + * it under the terms of the GNU General Public License as published by
28921 + * the Free Software Foundation; either version 2 of the License, or
28922 + * (at your option) any later version.
28923 + *
28924 + * This program is distributed in the hope that it will be useful,
28925 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28926 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28927 + * GNU General Public License for more details.
28928 + *
28929 + * You should have received a copy of the GNU General Public License
28930 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28931 + */
28932 +
28933 +/*
28934 + * procfs interfaces
28935 + */
28936 +
28937 +#include <linux/proc_fs.h>
28938 +#include "aufs.h"
28939 +
28940 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28941 +{
28942 +       struct au_sbinfo *sbinfo;
28943 +
28944 +       sbinfo = file->private_data;
28945 +       if (sbinfo) {
28946 +               au_plink_maint_leave(sbinfo);
28947 +               kobject_put(&sbinfo->si_kobj);
28948 +       }
28949 +
28950 +       return 0;
28951 +}
28952 +
28953 +static void au_procfs_plm_write_clean(struct file *file)
28954 +{
28955 +       struct au_sbinfo *sbinfo;
28956 +
28957 +       sbinfo = file->private_data;
28958 +       if (sbinfo)
28959 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28960 +}
28961 +
28962 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28963 +{
28964 +       int err;
28965 +       struct super_block *sb;
28966 +       struct au_sbinfo *sbinfo;
28967 +       struct hlist_bl_node *pos;
28968 +
28969 +       err = -EBUSY;
28970 +       if (unlikely(file->private_data))
28971 +               goto out;
28972 +
28973 +       sb = NULL;
28974 +       /* don't use au_sbilist_lock() here */
28975 +       hlist_bl_lock(&au_sbilist);
28976 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28977 +               if (id == sysaufs_si_id(sbinfo)) {
28978 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28979 +                               sb = sbinfo->si_sb;
28980 +                       break;
28981 +               }
28982 +       hlist_bl_unlock(&au_sbilist);
28983 +
28984 +       err = -EINVAL;
28985 +       if (unlikely(!sb))
28986 +               goto out;
28987 +
28988 +       err = au_plink_maint_enter(sb);
28989 +       if (!err)
28990 +               /* keep kobject_get() */
28991 +               file->private_data = sbinfo;
28992 +       else
28993 +               kobject_put(&sbinfo->si_kobj);
28994 +out:
28995 +       return err;
28996 +}
28997 +
28998 +/*
28999 + * Accept a valid "si=xxxx" only.
29000 + * Once it is accepted successfully, accept "clean" too.
29001 + */
29002 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
29003 +                                  size_t count, loff_t *ppos)
29004 +{
29005 +       ssize_t err;
29006 +       unsigned long id;
29007 +       /* last newline is allowed */
29008 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
29009 +
29010 +       err = -EACCES;
29011 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
29012 +               goto out;
29013 +
29014 +       err = -EINVAL;
29015 +       if (unlikely(count > sizeof(buf)))
29016 +               goto out;
29017 +
29018 +       err = copy_from_user(buf, ubuf, count);
29019 +       if (unlikely(err)) {
29020 +               err = -EFAULT;
29021 +               goto out;
29022 +       }
29023 +       buf[count] = 0;
29024 +
29025 +       err = -EINVAL;
29026 +       if (!strcmp("clean", buf)) {
29027 +               au_procfs_plm_write_clean(file);
29028 +               goto out_success;
29029 +       } else if (unlikely(strncmp("si=", buf, 3)))
29030 +               goto out;
29031 +
29032 +       err = kstrtoul(buf + 3, 16, &id);
29033 +       if (unlikely(err))
29034 +               goto out;
29035 +
29036 +       err = au_procfs_plm_write_si(file, id);
29037 +       if (unlikely(err))
29038 +               goto out;
29039 +
29040 +out_success:
29041 +       err = count; /* success */
29042 +out:
29043 +       return err;
29044 +}
29045 +
29046 +static const struct proc_ops au_procfs_plm_op = {
29047 +       .proc_write     = au_procfs_plm_write,
29048 +       .proc_release   = au_procfs_plm_release
29049 +};
29050 +
29051 +/* ---------------------------------------------------------------------- */
29052 +
29053 +static struct proc_dir_entry *au_procfs_dir;
29054 +
29055 +void au_procfs_fin(void)
29056 +{
29057 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29058 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29059 +}
29060 +
29061 +int __init au_procfs_init(void)
29062 +{
29063 +       int err;
29064 +       struct proc_dir_entry *entry;
29065 +
29066 +       err = -ENOMEM;
29067 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29068 +       if (unlikely(!au_procfs_dir))
29069 +               goto out;
29070 +
29071 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29072 +                           au_procfs_dir, &au_procfs_plm_op);
29073 +       if (unlikely(!entry))
29074 +               goto out_dir;
29075 +
29076 +       err = 0;
29077 +       goto out; /* success */
29078 +
29079 +
29080 +out_dir:
29081 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29082 +out:
29083 +       return err;
29084 +}
29085 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29086 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29087 +++ linux/fs/aufs/rdu.c 2024-03-10 23:40:47.086476782 +0100
29088 @@ -0,0 +1,384 @@
29089 +// SPDX-License-Identifier: GPL-2.0
29090 +/*
29091 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29092 + *
29093 + * This program is free software; you can redistribute it and/or modify
29094 + * it under the terms of the GNU General Public License as published by
29095 + * the Free Software Foundation; either version 2 of the License, or
29096 + * (at your option) any later version.
29097 + *
29098 + * This program is distributed in the hope that it will be useful,
29099 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29100 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29101 + * GNU General Public License for more details.
29102 + *
29103 + * You should have received a copy of the GNU General Public License
29104 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29105 + */
29106 +
29107 +/*
29108 + * readdir in userspace.
29109 + */
29110 +
29111 +#include <linux/compat.h>
29112 +#include <linux/fs_stack.h>
29113 +#include <linux/security.h>
29114 +#include "aufs.h"
29115 +
29116 +/* bits for struct aufs_rdu.flags */
29117 +#define        AuRdu_CALLED    BIT(0)
29118 +#define        AuRdu_CONT      BIT(1)
29119 +#define        AuRdu_FULL      BIT(2)
29120 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29121 +#define au_fset_rdu(flags, name) \
29122 +       do { (flags) |= AuRdu_##name; } while (0)
29123 +#define au_fclr_rdu(flags, name) \
29124 +       do { (flags) &= ~AuRdu_##name; } while (0)
29125 +
29126 +struct au_rdu_arg {
29127 +       struct dir_context              ctx;
29128 +       struct aufs_rdu                 *rdu;
29129 +       union au_rdu_ent_ul             ent;
29130 +       unsigned long                   end;
29131 +
29132 +       struct super_block              *sb;
29133 +       int                             err;
29134 +};
29135 +
29136 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29137 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29138 +{
29139 +       int err, len;
29140 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29141 +       struct aufs_rdu *rdu = arg->rdu;
29142 +       struct au_rdu_ent ent;
29143 +
29144 +       err = 0;
29145 +       arg->err = 0;
29146 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29147 +       len = au_rdu_len(nlen);
29148 +       if (arg->ent.ul + len  < arg->end) {
29149 +               ent.ino = h_ino;
29150 +               ent.bindex = rdu->cookie.bindex;
29151 +               ent.type = d_type;
29152 +               ent.nlen = nlen;
29153 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29154 +                       ent.type = DT_UNKNOWN;
29155 +
29156 +               /* unnecessary to support mmap_sem since this is a dir */
29157 +               err = -EFAULT;
29158 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29159 +                       goto out;
29160 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29161 +                       goto out;
29162 +               /* the terminating NULL */
29163 +               if (__put_user(0, arg->ent.e->name + nlen))
29164 +                       goto out;
29165 +               err = 0;
29166 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29167 +               arg->ent.ul += len;
29168 +               rdu->rent++;
29169 +       } else {
29170 +               err = -EFAULT;
29171 +               au_fset_rdu(rdu->cookie.flags, FULL);
29172 +               rdu->full = 1;
29173 +               rdu->tail = arg->ent;
29174 +       }
29175 +
29176 +out:
29177 +       /* AuTraceErr(err); */
29178 +       return !err;
29179 +}
29180 +
29181 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29182 +{
29183 +       int err;
29184 +       loff_t offset;
29185 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29186 +
29187 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29188 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29189 +       err = offset;
29190 +       if (unlikely(offset != cookie->h_pos))
29191 +               goto out;
29192 +
29193 +       err = 0;
29194 +       do {
29195 +               arg->err = 0;
29196 +               au_fclr_rdu(cookie->flags, CALLED);
29197 +               /* smp_mb(); */
29198 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29199 +               if (err >= 0)
29200 +                       err = arg->err;
29201 +       } while (!err
29202 +                && au_ftest_rdu(cookie->flags, CALLED)
29203 +                && !au_ftest_rdu(cookie->flags, FULL));
29204 +       cookie->h_pos = h_file->f_pos;
29205 +
29206 +out:
29207 +       AuTraceErr(err);
29208 +       return err;
29209 +}
29210 +
29211 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29212 +{
29213 +       int err;
29214 +       aufs_bindex_t bbot;
29215 +       struct au_rdu_arg arg = {
29216 +               .ctx = {
29217 +                       .actor = au_rdu_fill
29218 +               }
29219 +       };
29220 +       struct dentry *dentry;
29221 +       struct inode *inode;
29222 +       struct file *h_file;
29223 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29224 +
29225 +       /* VERIFY_WRITE */
29226 +       err = !access_ok(rdu->ent.e, rdu->sz);
29227 +       if (unlikely(err)) {
29228 +               err = -EFAULT;
29229 +               AuTraceErr(err);
29230 +               goto out;
29231 +       }
29232 +       rdu->rent = 0;
29233 +       rdu->tail = rdu->ent;
29234 +       rdu->full = 0;
29235 +       arg.rdu = rdu;
29236 +       arg.ent = rdu->ent;
29237 +       arg.end = arg.ent.ul;
29238 +       arg.end += rdu->sz;
29239 +
29240 +       err = -ENOTDIR;
29241 +       if (unlikely(!file->f_op->iterate_shared))
29242 +               goto out;
29243 +
29244 +       err = security_file_permission(file, MAY_READ);
29245 +       AuTraceErr(err);
29246 +       if (unlikely(err))
29247 +               goto out;
29248 +
29249 +       dentry = file->f_path.dentry;
29250 +       inode = d_inode(dentry);
29251 +       inode_lock_shared(inode);
29252 +
29253 +       arg.sb = inode->i_sb;
29254 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29255 +       if (unlikely(err))
29256 +               goto out_mtx;
29257 +       err = au_alive_dir(dentry);
29258 +       if (unlikely(err))
29259 +               goto out_si;
29260 +       /* todo: reval? */
29261 +       fi_read_lock(file);
29262 +
29263 +       err = -EAGAIN;
29264 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29265 +                    && cookie->generation != au_figen(file)))
29266 +               goto out_unlock;
29267 +
29268 +       err = 0;
29269 +       if (!rdu->blk) {
29270 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29271 +               if (!rdu->blk)
29272 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29273 +       }
29274 +       bbot = au_fbtop(file);
29275 +       if (cookie->bindex < bbot)
29276 +               cookie->bindex = bbot;
29277 +       bbot = au_fbbot_dir(file);
29278 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29279 +       for (; !err && cookie->bindex <= bbot;
29280 +            cookie->bindex++, cookie->h_pos = 0) {
29281 +               h_file = au_hf_dir(file, cookie->bindex);
29282 +               if (!h_file)
29283 +                       continue;
29284 +
29285 +               au_fclr_rdu(cookie->flags, FULL);
29286 +               err = au_rdu_do(h_file, &arg);
29287 +               AuTraceErr(err);
29288 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29289 +                       break;
29290 +       }
29291 +       AuDbg("rent %llu\n", rdu->rent);
29292 +
29293 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29294 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29295 +               au_fset_rdu(cookie->flags, CONT);
29296 +               cookie->generation = au_figen(file);
29297 +       }
29298 +
29299 +       ii_read_lock_child(inode);
29300 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29301 +       ii_read_unlock(inode);
29302 +
29303 +out_unlock:
29304 +       fi_read_unlock(file);
29305 +out_si:
29306 +       si_read_unlock(arg.sb);
29307 +out_mtx:
29308 +       inode_unlock_shared(inode);
29309 +out:
29310 +       AuTraceErr(err);
29311 +       return err;
29312 +}
29313 +
29314 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29315 +{
29316 +       int err;
29317 +       ino_t ino;
29318 +       unsigned long long nent;
29319 +       union au_rdu_ent_ul *u;
29320 +       struct au_rdu_ent ent;
29321 +       struct super_block *sb;
29322 +
29323 +       err = 0;
29324 +       nent = rdu->nent;
29325 +       u = &rdu->ent;
29326 +       sb = file->f_path.dentry->d_sb;
29327 +       si_read_lock(sb, AuLock_FLUSH);
29328 +       while (nent-- > 0) {
29329 +               /* unnecessary to support mmap_sem since this is a dir */
29330 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29331 +               if (!err)
29332 +                       /* VERIFY_WRITE */
29333 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29334 +               if (unlikely(err)) {
29335 +                       err = -EFAULT;
29336 +                       AuTraceErr(err);
29337 +                       break;
29338 +               }
29339 +
29340 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29341 +               if (!ent.wh)
29342 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29343 +               else
29344 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29345 +                                       &ino);
29346 +               if (unlikely(err)) {
29347 +                       AuTraceErr(err);
29348 +                       break;
29349 +               }
29350 +
29351 +               err = __put_user(ino, &u->e->ino);
29352 +               if (unlikely(err)) {
29353 +                       err = -EFAULT;
29354 +                       AuTraceErr(err);
29355 +                       break;
29356 +               }
29357 +               u->ul += au_rdu_len(ent.nlen);
29358 +       }
29359 +       si_read_unlock(sb);
29360 +
29361 +       return err;
29362 +}
29363 +
29364 +/* ---------------------------------------------------------------------- */
29365 +
29366 +static int au_rdu_verify(struct aufs_rdu *rdu)
29367 +{
29368 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29369 +             "%llu, b%d, 0x%x, g%u}\n",
29370 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29371 +             rdu->blk,
29372 +             rdu->rent, rdu->shwh, rdu->full,
29373 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29374 +             rdu->cookie.generation);
29375 +
29376 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29377 +               return 0;
29378 +
29379 +       AuDbg("%u:%u\n",
29380 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29381 +       return -EINVAL;
29382 +}
29383 +
29384 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29385 +{
29386 +       long err, e;
29387 +       struct aufs_rdu rdu;
29388 +       void __user *p = (void __user *)arg;
29389 +
29390 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29391 +       if (unlikely(err)) {
29392 +               err = -EFAULT;
29393 +               AuTraceErr(err);
29394 +               goto out;
29395 +       }
29396 +       err = au_rdu_verify(&rdu);
29397 +       if (unlikely(err))
29398 +               goto out;
29399 +
29400 +       switch (cmd) {
29401 +       case AUFS_CTL_RDU:
29402 +               err = au_rdu(file, &rdu);
29403 +               if (unlikely(err))
29404 +                       break;
29405 +
29406 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29407 +               if (unlikely(e)) {
29408 +                       err = -EFAULT;
29409 +                       AuTraceErr(err);
29410 +               }
29411 +               break;
29412 +       case AUFS_CTL_RDU_INO:
29413 +               err = au_rdu_ino(file, &rdu);
29414 +               break;
29415 +
29416 +       default:
29417 +               /* err = -ENOTTY; */
29418 +               err = -EINVAL;
29419 +       }
29420 +
29421 +out:
29422 +       AuTraceErr(err);
29423 +       return err;
29424 +}
29425 +
29426 +#ifdef CONFIG_COMPAT
29427 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29428 +{
29429 +       long err, e;
29430 +       struct aufs_rdu rdu;
29431 +       void __user *p = compat_ptr(arg);
29432 +
29433 +       /* todo: get_user()? */
29434 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29435 +       if (unlikely(err)) {
29436 +               err = -EFAULT;
29437 +               AuTraceErr(err);
29438 +               goto out;
29439 +       }
29440 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29441 +       err = au_rdu_verify(&rdu);
29442 +       if (unlikely(err))
29443 +               goto out;
29444 +
29445 +       switch (cmd) {
29446 +       case AUFS_CTL_RDU:
29447 +               err = au_rdu(file, &rdu);
29448 +               if (unlikely(err))
29449 +                       break;
29450 +
29451 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29452 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29453 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29454 +               if (unlikely(e)) {
29455 +                       err = -EFAULT;
29456 +                       AuTraceErr(err);
29457 +               }
29458 +               break;
29459 +       case AUFS_CTL_RDU_INO:
29460 +               err = au_rdu_ino(file, &rdu);
29461 +               break;
29462 +
29463 +       default:
29464 +               /* err = -ENOTTY; */
29465 +               err = -EINVAL;
29466 +       }
29467 +
29468 +out:
29469 +       AuTraceErr(err);
29470 +       return err;
29471 +}
29472 +#endif
29473 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29474 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29475 +++ linux/fs/aufs/rwsem.h       2022-11-05 23:02:18.969222617 +0100
29476 @@ -0,0 +1,85 @@
29477 +/* SPDX-License-Identifier: GPL-2.0 */
29478 +/*
29479 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29480 + *
29481 + * This program is free software; you can redistribute it and/or modify
29482 + * it under the terms of the GNU General Public License as published by
29483 + * the Free Software Foundation; either version 2 of the License, or
29484 + * (at your option) any later version.
29485 + *
29486 + * This program is distributed in the hope that it will be useful,
29487 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29488 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29489 + * GNU General Public License for more details.
29490 + *
29491 + * You should have received a copy of the GNU General Public License
29492 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29493 + */
29494 +
29495 +/*
29496 + * simple read-write semaphore wrappers
29497 + */
29498 +
29499 +#ifndef __AUFS_RWSEM_H__
29500 +#define __AUFS_RWSEM_H__
29501 +
29502 +#ifdef __KERNEL__
29503 +
29504 +#include "debug.h"
29505 +
29506 +/* in the future, the name 'au_rwsem' will be totally gone */
29507 +#define au_rwsem       rw_semaphore
29508 +
29509 +/* to debug easier, do not make them inlined functions */
29510 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29511 +
29512 +#ifdef CONFIG_LOCKDEP
29513 +/* rwsem_is_locked() is unusable */
29514 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29515 +                                         && !lockdep_recursing(current) \
29516 +                                         && debug_locks                \
29517 +                                         && !lockdep_is_held_type(rw, 1))
29518 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29519 +                                         && !lockdep_recursing(current) \
29520 +                                         && debug_locks                \
29521 +                                         && !lockdep_is_held_type(rw, 0))
29522 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29523 +                                         && !lockdep_recursing(current) \
29524 +                                         && debug_locks                \
29525 +                                         && !lockdep_is_held(rw))
29526 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29527 +                                         && !lockdep_recursing(current) \
29528 +                                         && debug_locks                \
29529 +                                         && lockdep_is_held(rw))
29530 +#else
29531 +#define AuRwMustReadLock(rw)   do {} while (0)
29532 +#define AuRwMustWriteLock(rw)  do {} while (0)
29533 +#define AuRwMustAnyLock(rw)    do {} while (0)
29534 +#define AuRwDestroy(rw)                do {} while (0)
29535 +#endif
29536 +
29537 +#define au_rw_init(rw) init_rwsem(rw)
29538 +
29539 +#define au_rw_init_wlock(rw) do {              \
29540 +               au_rw_init(rw);                 \
29541 +               down_write(rw);                 \
29542 +       } while (0)
29543 +
29544 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29545 +               au_rw_init(rw);                 \
29546 +               down_write_nested(rw, lsc);     \
29547 +       } while (0)
29548 +
29549 +#define au_rw_read_lock(rw)            down_read(rw)
29550 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29551 +#define au_rw_read_unlock(rw)          up_read(rw)
29552 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29553 +#define au_rw_write_lock(rw)           down_write(rw)
29554 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29555 +#define au_rw_write_unlock(rw)         up_write(rw)
29556 +/* why is not _nested version defined? */
29557 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29558 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29559 +
29560 +#endif /* __KERNEL__ */
29561 +#endif /* __AUFS_RWSEM_H__ */
29562 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29563 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29564 +++ linux/fs/aufs/sbinfo.c      2022-11-05 23:02:18.969222617 +0100
29565 @@ -0,0 +1,316 @@
29566 +// SPDX-License-Identifier: GPL-2.0
29567 +/*
29568 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29569 + *
29570 + * This program is free software; you can redistribute it and/or modify
29571 + * it under the terms of the GNU General Public License as published by
29572 + * the Free Software Foundation; either version 2 of the License, or
29573 + * (at your option) any later version.
29574 + *
29575 + * This program is distributed in the hope that it will be useful,
29576 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29577 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29578 + * GNU General Public License for more details.
29579 + *
29580 + * You should have received a copy of the GNU General Public License
29581 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29582 + */
29583 +
29584 +/*
29585 + * superblock private data
29586 + */
29587 +
29588 +#include <linux/iversion.h>
29589 +#include "aufs.h"
29590 +
29591 +/*
29592 + * they are necessary regardless sysfs is disabled.
29593 + */
29594 +void au_si_free(struct kobject *kobj)
29595 +{
29596 +       int i;
29597 +       struct au_sbinfo *sbinfo;
29598 +       char *locked __maybe_unused; /* debug only */
29599 +
29600 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29601 +       for (i = 0; i < AuPlink_NHASH; i++)
29602 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29603 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29604 +
29605 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29606 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29607 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29608 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29609 +
29610 +       dbgaufs_si_fin(sbinfo);
29611 +       au_rw_write_lock(&sbinfo->si_rwsem);
29612 +       au_br_free(sbinfo);
29613 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29614 +
29615 +       au_kfree_try_rcu(sbinfo->si_branch);
29616 +       mutex_destroy(&sbinfo->si_xib_mtx);
29617 +       AuRwDestroy(&sbinfo->si_rwsem);
29618 +
29619 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29620 +       /* si_nfiles is waited too */
29621 +       au_kfree_rcu(sbinfo);
29622 +}
29623 +
29624 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29625 +{
29626 +       struct au_sbinfo *sbinfo;
29627 +       int err, i;
29628 +
29629 +       err = -ENOMEM;
29630 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29631 +       if (unlikely(!sbinfo))
29632 +               goto out;
29633 +
29634 +       /* will be reallocated separately */
29635 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29636 +       if (unlikely(!sbinfo->si_branch))
29637 +               goto out_sbinfo;
29638 +
29639 +       err = sysaufs_si_init(sbinfo);
29640 +       if (!err) {
29641 +               dbgaufs_si_null(sbinfo);
29642 +               err = dbgaufs_si_init(sbinfo);
29643 +               if (unlikely(err))
29644 +                       kobject_put(&sbinfo->si_kobj);
29645 +       }
29646 +       if (unlikely(err))
29647 +               goto out_br;
29648 +
29649 +       au_nwt_init(&sbinfo->si_nowait);
29650 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29651 +
29652 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29653 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29654 +
29655 +       sbinfo->si_bbot = -1;
29656 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29657 +
29658 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29659 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29660 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29661 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29662 +
29663 +       au_fhsm_init(sbinfo);
29664 +
29665 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29666 +
29667 +       sbinfo->si_xino_jiffy = jiffies;
29668 +       sbinfo->si_xino_expire
29669 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29670 +       mutex_init(&sbinfo->si_xib_mtx);
29671 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29672 +
29673 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29674 +
29675 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29676 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29677 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29678 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29679 +
29680 +       for (i = 0; i < AuPlink_NHASH; i++)
29681 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29682 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29683 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29684 +
29685 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29686 +
29687 +       /* with getattr by default */
29688 +       sbinfo->si_iop_array = aufs_iop;
29689 +
29690 +       /* leave other members for sysaufs and si_mnt. */
29691 +       sbinfo->si_sb = sb;
29692 +       if (sb) {
29693 +               sb->s_fs_info = sbinfo;
29694 +               si_pid_set(sb);
29695 +       }
29696 +       return sbinfo; /* success */
29697 +
29698 +out_br:
29699 +       au_kfree_try_rcu(sbinfo->si_branch);
29700 +out_sbinfo:
29701 +       au_kfree_rcu(sbinfo);
29702 +out:
29703 +       return ERR_PTR(err);
29704 +}
29705 +
29706 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29707 +{
29708 +       int err, sz;
29709 +       struct au_branch **brp;
29710 +
29711 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29712 +
29713 +       err = -ENOMEM;
29714 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29715 +       if (unlikely(!sz))
29716 +               sz = sizeof(*brp);
29717 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29718 +                          may_shrink);
29719 +       if (brp) {
29720 +               sbinfo->si_branch = brp;
29721 +               err = 0;
29722 +       }
29723 +
29724 +       return err;
29725 +}
29726 +
29727 +/* ---------------------------------------------------------------------- */
29728 +
29729 +unsigned int au_sigen_inc(struct super_block *sb)
29730 +{
29731 +       unsigned int gen;
29732 +       struct inode *inode;
29733 +
29734 +       SiMustWriteLock(sb);
29735 +
29736 +       gen = ++au_sbi(sb)->si_generation;
29737 +       au_update_digen(sb->s_root);
29738 +       inode = d_inode(sb->s_root);
29739 +       au_update_iigen(inode, /*half*/0);
29740 +       inode_inc_iversion(inode);
29741 +       return gen;
29742 +}
29743 +
29744 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29745 +{
29746 +       aufs_bindex_t br_id;
29747 +       int i;
29748 +       struct au_sbinfo *sbinfo;
29749 +
29750 +       SiMustWriteLock(sb);
29751 +
29752 +       sbinfo = au_sbi(sb);
29753 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29754 +               br_id = ++sbinfo->si_last_br_id;
29755 +               AuDebugOn(br_id < 0);
29756 +               if (br_id && au_br_index(sb, br_id) < 0)
29757 +                       return br_id;
29758 +       }
29759 +
29760 +       return -1;
29761 +}
29762 +
29763 +/* ---------------------------------------------------------------------- */
29764 +
29765 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29766 +int si_read_lock(struct super_block *sb, int flags)
29767 +{
29768 +       int err;
29769 +
29770 +       err = 0;
29771 +       if (au_ftest_lock(flags, FLUSH))
29772 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29773 +
29774 +       si_noflush_read_lock(sb);
29775 +       err = au_plink_maint(sb, flags);
29776 +       if (unlikely(err))
29777 +               si_read_unlock(sb);
29778 +
29779 +       return err;
29780 +}
29781 +
29782 +int si_write_lock(struct super_block *sb, int flags)
29783 +{
29784 +       int err;
29785 +
29786 +       if (au_ftest_lock(flags, FLUSH))
29787 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29788 +
29789 +       si_noflush_write_lock(sb);
29790 +       err = au_plink_maint(sb, flags);
29791 +       if (unlikely(err))
29792 +               si_write_unlock(sb);
29793 +
29794 +       return err;
29795 +}
29796 +
29797 +/* dentry and super_block lock. call at entry point */
29798 +int aufs_read_lock(struct dentry *dentry, int flags)
29799 +{
29800 +       int err;
29801 +       struct super_block *sb;
29802 +
29803 +       sb = dentry->d_sb;
29804 +       err = si_read_lock(sb, flags);
29805 +       if (unlikely(err))
29806 +               goto out;
29807 +
29808 +       if (au_ftest_lock(flags, DW))
29809 +               di_write_lock_child(dentry);
29810 +       else
29811 +               di_read_lock_child(dentry, flags);
29812 +
29813 +       if (au_ftest_lock(flags, GEN)) {
29814 +               err = au_digen_test(dentry, au_sigen(sb));
29815 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29816 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29817 +               else if (!err)
29818 +                       err = au_dbrange_test(dentry);
29819 +               if (unlikely(err))
29820 +                       aufs_read_unlock(dentry, flags);
29821 +       }
29822 +
29823 +out:
29824 +       return err;
29825 +}
29826 +
29827 +void aufs_read_unlock(struct dentry *dentry, int flags)
29828 +{
29829 +       if (au_ftest_lock(flags, DW))
29830 +               di_write_unlock(dentry);
29831 +       else
29832 +               di_read_unlock(dentry, flags);
29833 +       si_read_unlock(dentry->d_sb);
29834 +}
29835 +
29836 +void aufs_write_lock(struct dentry *dentry)
29837 +{
29838 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29839 +       di_write_lock_child(dentry);
29840 +}
29841 +
29842 +void aufs_write_unlock(struct dentry *dentry)
29843 +{
29844 +       di_write_unlock(dentry);
29845 +       si_write_unlock(dentry->d_sb);
29846 +}
29847 +
29848 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29849 +{
29850 +       int err;
29851 +       unsigned int sigen;
29852 +       struct super_block *sb;
29853 +
29854 +       sb = d1->d_sb;
29855 +       err = si_read_lock(sb, flags);
29856 +       if (unlikely(err))
29857 +               goto out;
29858 +
29859 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29860 +
29861 +       if (au_ftest_lock(flags, GEN)) {
29862 +               sigen = au_sigen(sb);
29863 +               err = au_digen_test(d1, sigen);
29864 +               AuDebugOn(!err && au_dbrange_test(d1));
29865 +               if (!err) {
29866 +                       err = au_digen_test(d2, sigen);
29867 +                       AuDebugOn(!err && au_dbrange_test(d2));
29868 +               }
29869 +               if (unlikely(err))
29870 +                       aufs_read_and_write_unlock2(d1, d2);
29871 +       }
29872 +
29873 +out:
29874 +       return err;
29875 +}
29876 +
29877 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29878 +{
29879 +       di_write_unlock2(d1, d2);
29880 +       si_read_unlock(d1->d_sb);
29881 +}
29882 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29883 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29884 +++ linux/fs/aufs/super.c       2023-10-10 22:51:18.033248030 +0200
29885 @@ -0,0 +1,871 @@
29886 +// SPDX-License-Identifier: GPL-2.0
29887 +/*
29888 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29889 + *
29890 + * This program is free software; you can redistribute it and/or modify
29891 + * it under the terms of the GNU General Public License as published by
29892 + * the Free Software Foundation; either version 2 of the License, or
29893 + * (at your option) any later version.
29894 + *
29895 + * This program is distributed in the hope that it will be useful,
29896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29898 + * GNU General Public License for more details.
29899 + *
29900 + * You should have received a copy of the GNU General Public License
29901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29902 + */
29903 +
29904 +/*
29905 + * mount and super_block operations
29906 + */
29907 +
29908 +#include <linux/iversion.h>
29909 +#include <linux/mm.h>
29910 +#include <linux/seq_file.h>
29911 +#include <linux/statfs.h>
29912 +#include <linux/vmalloc.h>
29913 +#include "aufs.h"
29914 +
29915 +/*
29916 + * super_operations
29917 + */
29918 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29919 +{
29920 +       struct au_icntnr *c;
29921 +
29922 +       c = au_cache_alloc_icntnr(sb);
29923 +       if (c) {
29924 +               au_icntnr_init(c);
29925 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29926 +               c->iinfo.ii_hinode = NULL;
29927 +               return &c->vfs_inode;
29928 +       }
29929 +       return NULL;
29930 +}
29931 +
29932 +static void aufs_destroy_inode(struct inode *inode)
29933 +{
29934 +       if (!au_is_bad_inode(inode))
29935 +               au_iinfo_fin(inode);
29936 +}
29937 +
29938 +static void aufs_free_inode(struct inode *inode)
29939 +{
29940 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29941 +}
29942 +
29943 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29944 +{
29945 +       struct inode *inode;
29946 +       int err;
29947 +
29948 +       inode = iget_locked(sb, ino);
29949 +       if (unlikely(!inode)) {
29950 +               inode = ERR_PTR(-ENOMEM);
29951 +               goto out;
29952 +       }
29953 +       if (!(inode->i_state & I_NEW))
29954 +               goto out;
29955 +
29956 +       err = au_xigen_new(inode);
29957 +       if (!err)
29958 +               err = au_iinfo_init(inode);
29959 +       if (!err)
29960 +               inode_inc_iversion(inode);
29961 +       else {
29962 +               iget_failed(inode);
29963 +               inode = ERR_PTR(err);
29964 +       }
29965 +
29966 +out:
29967 +       /* never return NULL */
29968 +       AuDebugOn(!inode);
29969 +       AuTraceErrPtr(inode);
29970 +       return inode;
29971 +}
29972 +
29973 +/* lock free root dinfo */
29974 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29975 +{
29976 +       int err;
29977 +       aufs_bindex_t bindex, bbot;
29978 +       struct path path;
29979 +       struct au_hdentry *hdp;
29980 +       struct au_branch *br;
29981 +       au_br_perm_str_t perm;
29982 +
29983 +       err = 0;
29984 +       bbot = au_sbbot(sb);
29985 +       bindex = 0;
29986 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29987 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29988 +               br = au_sbr(sb, bindex);
29989 +               path.mnt = au_br_mnt(br);
29990 +               path.dentry = hdp->hd_dentry;
29991 +               err = au_seq_path(seq, &path);
29992 +               if (!err) {
29993 +                       au_optstr_br_perm(&perm, br->br_perm);
29994 +                       seq_printf(seq, "=%s", perm.a);
29995 +                       if (bindex != bbot)
29996 +                               seq_putc(seq, ':');
29997 +               }
29998 +       }
29999 +       if (unlikely(err || seq_has_overflowed(seq)))
30000 +               err = -E2BIG;
30001 +
30002 +       return err;
30003 +}
30004 +
30005 +static void au_gen_fmt(char *fmt, int len, const char *pat,
30006 +                      const char *append)
30007 +{
30008 +       char *p;
30009 +
30010 +       p = fmt;
30011 +       while (*pat != ':')
30012 +               *p++ = *pat++;
30013 +       *p++ = *pat++;
30014 +       strscpy(p, append, len - (p - fmt));
30015 +       AuDebugOn(strlen(fmt) >= len);
30016 +}
30017 +
30018 +static void au_show_wbr_create(struct seq_file *m, int v,
30019 +                              struct au_sbinfo *sbinfo)
30020 +{
30021 +       const char *pat;
30022 +       char fmt[32];
30023 +       struct au_wbr_mfs *mfs;
30024 +
30025 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30026 +
30027 +       seq_puts(m, ",create=");
30028 +       pat = au_optstr_wbr_create(v);
30029 +       mfs = &sbinfo->si_wbr_mfs;
30030 +       switch (v) {
30031 +       case AuWbrCreate_TDP:
30032 +       case AuWbrCreate_RR:
30033 +       case AuWbrCreate_MFS:
30034 +       case AuWbrCreate_PMFS:
30035 +               seq_puts(m, pat);
30036 +               break;
30037 +       case AuWbrCreate_MFSRR:
30038 +       case AuWbrCreate_TDMFS:
30039 +       case AuWbrCreate_PMFSRR:
30040 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30041 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30042 +               break;
30043 +       case AuWbrCreate_MFSV:
30044 +       case AuWbrCreate_PMFSV:
30045 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30046 +               seq_printf(m, fmt,
30047 +                          jiffies_to_msecs(mfs->mfs_expire)
30048 +                          / MSEC_PER_SEC);
30049 +               break;
30050 +       case AuWbrCreate_MFSRRV:
30051 +       case AuWbrCreate_TDMFSV:
30052 +       case AuWbrCreate_PMFSRRV:
30053 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30054 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30055 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30056 +               break;
30057 +       default:
30058 +               BUG();
30059 +       }
30060 +}
30061 +
30062 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30063 +{
30064 +#ifdef CONFIG_SYSFS
30065 +       return 0;
30066 +#else
30067 +       int err;
30068 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30069 +       aufs_bindex_t bindex, brid;
30070 +       struct qstr *name;
30071 +       struct file *f;
30072 +       struct dentry *d, *h_root;
30073 +       struct au_branch *br;
30074 +
30075 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30076 +
30077 +       err = 0;
30078 +       f = au_sbi(sb)->si_xib;
30079 +       if (!f)
30080 +               goto out;
30081 +
30082 +       /* stop printing the default xino path on the first writable branch */
30083 +       h_root = NULL;
30084 +       bindex = au_xi_root(sb, f->f_path.dentry);
30085 +       if (bindex >= 0) {
30086 +               br = au_sbr_sb(sb, bindex);
30087 +               h_root = au_br_dentry(br);
30088 +       }
30089 +
30090 +       d = f->f_path.dentry;
30091 +       name = &d->d_name;
30092 +       /* safe ->d_parent because the file is unlinked */
30093 +       if (d->d_parent == h_root
30094 +           && name->len == len
30095 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30096 +               goto out;
30097 +
30098 +       seq_puts(seq, ",xino=");
30099 +       err = au_xino_path(seq, f);
30100 +
30101 +out:
30102 +       return err;
30103 +#endif
30104 +}
30105 +
30106 +/* seq_file will re-call me in case of too long string */
30107 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30108 +{
30109 +       int err;
30110 +       unsigned int mnt_flags, v;
30111 +       struct super_block *sb;
30112 +       struct au_sbinfo *sbinfo;
30113 +
30114 +#define AuBool(name, str) do { \
30115 +       v = au_opt_test(mnt_flags, name); \
30116 +       if (v != au_opt_test(AuOpt_Def, name)) \
30117 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30118 +} while (0)
30119 +
30120 +#define AuStr(name, str) do { \
30121 +       v = mnt_flags & AuOptMask_##name; \
30122 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30123 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30124 +} while (0)
30125 +
30126 +#define AuUInt(name, str, val) do { \
30127 +       if (val != AUFS_##name##_DEF) \
30128 +               seq_printf(m, "," #str "=%u", val); \
30129 +} while (0)
30130 +
30131 +       sb = dentry->d_sb;
30132 +       if (sb->s_flags & SB_POSIXACL)
30133 +               seq_puts(m, ",acl");
30134 +#if 0 /* reserved for future use */
30135 +       if (sb->s_flags & SB_I_VERSION)
30136 +               seq_puts(m, ",i_version");
30137 +#endif
30138 +
30139 +       /* lock free root dinfo */
30140 +       si_noflush_read_lock(sb);
30141 +       sbinfo = au_sbi(sb);
30142 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30143 +
30144 +       mnt_flags = au_mntflags(sb);
30145 +       if (au_opt_test(mnt_flags, XINO)) {
30146 +               err = au_show_xino(m, sb);
30147 +               if (unlikely(err))
30148 +                       goto out;
30149 +       } else
30150 +               seq_puts(m, ",noxino");
30151 +
30152 +       AuBool(TRUNC_XINO, trunc_xino);
30153 +       AuStr(UDBA, udba);
30154 +       AuBool(SHWH, shwh);
30155 +       AuBool(PLINK, plink);
30156 +       AuBool(DIO, dio);
30157 +       AuBool(DIRPERM1, dirperm1);
30158 +
30159 +       v = sbinfo->si_wbr_create;
30160 +       if (v != AuWbrCreate_Def)
30161 +               au_show_wbr_create(m, v, sbinfo);
30162 +
30163 +       v = sbinfo->si_wbr_copyup;
30164 +       if (v != AuWbrCopyup_Def)
30165 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30166 +
30167 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30168 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30169 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30170 +
30171 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30172 +
30173 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30174 +       AuUInt(RDCACHE, rdcache, v);
30175 +
30176 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30177 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30178 +
30179 +       au_fhsm_show(m, sbinfo);
30180 +
30181 +       AuBool(DIRREN, dirren);
30182 +       AuBool(SUM, sum);
30183 +       /* AuBool(SUM_W, wsum); */
30184 +       AuBool(WARN_PERM, warn_perm);
30185 +       AuBool(VERBOSE, verbose);
30186 +
30187 +out:
30188 +       /* be sure to print "br:" last */
30189 +       if (!sysaufs_brs) {
30190 +               seq_puts(m, ",br:");
30191 +               au_show_brs(m, sb);
30192 +       }
30193 +       si_read_unlock(sb);
30194 +       return 0;
30195 +
30196 +#undef AuBool
30197 +#undef AuStr
30198 +#undef AuUInt
30199 +}
30200 +
30201 +/* ---------------------------------------------------------------------- */
30202 +
30203 +/* sum mode which returns the summation for statfs(2) */
30204 +
30205 +static u64 au_add_till_max(u64 a, u64 b)
30206 +{
30207 +       u64 old;
30208 +
30209 +       old = a;
30210 +       a += b;
30211 +       if (old <= a)
30212 +               return a;
30213 +       return ULLONG_MAX;
30214 +}
30215 +
30216 +static u64 au_mul_till_max(u64 a, long mul)
30217 +{
30218 +       u64 old;
30219 +
30220 +       old = a;
30221 +       a *= mul;
30222 +       if (old <= a)
30223 +               return a;
30224 +       return ULLONG_MAX;
30225 +}
30226 +
30227 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30228 +{
30229 +       int err;
30230 +       long bsize, factor;
30231 +       u64 blocks, bfree, bavail, files, ffree;
30232 +       aufs_bindex_t bbot, bindex, i;
30233 +       unsigned char shared;
30234 +       struct path h_path;
30235 +       struct super_block *h_sb;
30236 +
30237 +       err = 0;
30238 +       bsize = LONG_MAX;
30239 +       files = 0;
30240 +       ffree = 0;
30241 +       blocks = 0;
30242 +       bfree = 0;
30243 +       bavail = 0;
30244 +       bbot = au_sbbot(sb);
30245 +       for (bindex = 0; bindex <= bbot; bindex++) {
30246 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30247 +               h_sb = h_path.mnt->mnt_sb;
30248 +               shared = 0;
30249 +               for (i = 0; !shared && i < bindex; i++)
30250 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30251 +               if (shared)
30252 +                       continue;
30253 +
30254 +               /* sb->s_root for NFS is unreliable */
30255 +               h_path.dentry = h_path.mnt->mnt_root;
30256 +               err = vfs_statfs(&h_path, buf);
30257 +               if (unlikely(err))
30258 +                       goto out;
30259 +
30260 +               if (bsize > buf->f_bsize) {
30261 +                       /*
30262 +                        * we will reduce bsize, so we have to expand blocks
30263 +                        * etc. to match them again
30264 +                        */
30265 +                       factor = (bsize / buf->f_bsize);
30266 +                       blocks = au_mul_till_max(blocks, factor);
30267 +                       bfree = au_mul_till_max(bfree, factor);
30268 +                       bavail = au_mul_till_max(bavail, factor);
30269 +                       bsize = buf->f_bsize;
30270 +               }
30271 +
30272 +               factor = (buf->f_bsize / bsize);
30273 +               blocks = au_add_till_max(blocks,
30274 +                               au_mul_till_max(buf->f_blocks, factor));
30275 +               bfree = au_add_till_max(bfree,
30276 +                               au_mul_till_max(buf->f_bfree, factor));
30277 +               bavail = au_add_till_max(bavail,
30278 +                               au_mul_till_max(buf->f_bavail, factor));
30279 +               files = au_add_till_max(files, buf->f_files);
30280 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30281 +       }
30282 +
30283 +       buf->f_bsize = bsize;
30284 +       buf->f_blocks = blocks;
30285 +       buf->f_bfree = bfree;
30286 +       buf->f_bavail = bavail;
30287 +       buf->f_files = files;
30288 +       buf->f_ffree = ffree;
30289 +       buf->f_frsize = 0;
30290 +
30291 +out:
30292 +       return err;
30293 +}
30294 +
30295 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30296 +{
30297 +       int err;
30298 +       struct path h_path;
30299 +       struct super_block *sb;
30300 +
30301 +       /* lock free root dinfo */
30302 +       sb = dentry->d_sb;
30303 +       si_noflush_read_lock(sb);
30304 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30305 +               /* sb->s_root for NFS is unreliable */
30306 +               h_path.mnt = au_sbr_mnt(sb, 0);
30307 +               h_path.dentry = h_path.mnt->mnt_root;
30308 +               err = vfs_statfs(&h_path, buf);
30309 +       } else
30310 +               err = au_statfs_sum(sb, buf);
30311 +       si_read_unlock(sb);
30312 +
30313 +       if (!err) {
30314 +               buf->f_type = AUFS_SUPER_MAGIC;
30315 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30316 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30317 +       }
30318 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30319 +
30320 +       return err;
30321 +}
30322 +
30323 +/* ---------------------------------------------------------------------- */
30324 +
30325 +static int aufs_sync_fs(struct super_block *sb, int wait)
30326 +{
30327 +       int err, e;
30328 +       aufs_bindex_t bbot, bindex;
30329 +       struct au_branch *br;
30330 +       struct super_block *h_sb;
30331 +
30332 +       err = 0;
30333 +       si_noflush_read_lock(sb);
30334 +       bbot = au_sbbot(sb);
30335 +       for (bindex = 0; bindex <= bbot; bindex++) {
30336 +               br = au_sbr(sb, bindex);
30337 +               if (!au_br_writable(br->br_perm))
30338 +                       continue;
30339 +
30340 +               h_sb = au_sbr_sb(sb, bindex);
30341 +               e = vfsub_sync_filesystem(h_sb);
30342 +               if (unlikely(e && !err))
30343 +                       err = e;
30344 +               /* go on even if an error happens */
30345 +       }
30346 +       si_read_unlock(sb);
30347 +
30348 +       return err;
30349 +}
30350 +
30351 +/* ---------------------------------------------------------------------- */
30352 +
30353 +/* final actions when unmounting a file system */
30354 +static void aufs_put_super(struct super_block *sb)
30355 +{
30356 +       struct au_sbinfo *sbinfo;
30357 +
30358 +       sbinfo = au_sbi(sb);
30359 +       if (sbinfo)
30360 +               kobject_put(&sbinfo->si_kobj);
30361 +}
30362 +
30363 +/* ---------------------------------------------------------------------- */
30364 +
30365 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30366 +                    struct super_block *sb, void *arg)
30367 +{
30368 +       void *array;
30369 +       unsigned long long n, sz;
30370 +
30371 +       array = NULL;
30372 +       n = 0;
30373 +       if (!*hint)
30374 +               goto out;
30375 +
30376 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30377 +               array = ERR_PTR(-EMFILE);
30378 +               pr_err("hint %llu\n", *hint);
30379 +               goto out;
30380 +       }
30381 +
30382 +       sz = sizeof(array) * *hint;
30383 +       array = kzalloc(sz, GFP_NOFS);
30384 +       if (unlikely(!array))
30385 +               array = vzalloc(sz);
30386 +       if (unlikely(!array)) {
30387 +               array = ERR_PTR(-ENOMEM);
30388 +               goto out;
30389 +       }
30390 +
30391 +       n = cb(sb, array, *hint, arg);
30392 +       AuDebugOn(n > *hint);
30393 +
30394 +out:
30395 +       *hint = n;
30396 +       return array;
30397 +}
30398 +
30399 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30400 +                                      unsigned long long max __maybe_unused,
30401 +                                      void *arg)
30402 +{
30403 +       unsigned long long n;
30404 +       struct inode **p, *inode;
30405 +       struct list_head *head;
30406 +
30407 +       n = 0;
30408 +       p = a;
30409 +       head = arg;
30410 +       spin_lock(&sb->s_inode_list_lock);
30411 +       list_for_each_entry(inode, head, i_sb_list) {
30412 +               if (!au_is_bad_inode(inode)
30413 +                   && au_ii(inode)->ii_btop >= 0) {
30414 +                       spin_lock(&inode->i_lock);
30415 +                       if (atomic_read(&inode->i_count)) {
30416 +                               au_igrab(inode);
30417 +                               *p++ = inode;
30418 +                               n++;
30419 +                               AuDebugOn(n > max);
30420 +                       }
30421 +                       spin_unlock(&inode->i_lock);
30422 +               }
30423 +       }
30424 +       spin_unlock(&sb->s_inode_list_lock);
30425 +
30426 +       return n;
30427 +}
30428 +
30429 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30430 +{
30431 +       struct au_sbinfo *sbi;
30432 +
30433 +       sbi = au_sbi(sb);
30434 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30435 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30436 +}
30437 +
30438 +void au_iarray_free(struct inode **a, unsigned long long max)
30439 +{
30440 +       unsigned long long ull;
30441 +
30442 +       for (ull = 0; ull < max; ull++)
30443 +               iput(a[ull]);
30444 +       kvfree(a);
30445 +}
30446 +
30447 +/* ---------------------------------------------------------------------- */
30448 +
30449 +/*
30450 + * refresh dentry and inode at remount time.
30451 + */
30452 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30453 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30454 +                     struct dentry *parent)
30455 +{
30456 +       int err;
30457 +
30458 +       di_write_lock_child(dentry);
30459 +       di_read_lock_parent(parent, AuLock_IR);
30460 +       err = au_refresh_dentry(dentry, parent);
30461 +       if (!err && dir_flags)
30462 +               au_hn_reset(d_inode(dentry), dir_flags);
30463 +       di_read_unlock(parent, AuLock_IR);
30464 +       di_write_unlock(dentry);
30465 +
30466 +       return err;
30467 +}
30468 +
30469 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30470 +                          struct au_sbinfo *sbinfo,
30471 +                          const unsigned int dir_flags, unsigned int do_idop)
30472 +{
30473 +       int err;
30474 +       struct dentry *parent;
30475 +
30476 +       err = 0;
30477 +       parent = dget_parent(dentry);
30478 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30479 +               if (d_really_is_positive(dentry)) {
30480 +                       if (!d_is_dir(dentry))
30481 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30482 +                                                parent);
30483 +                       else {
30484 +                               err = au_do_refresh(dentry, dir_flags, parent);
30485 +                               if (unlikely(err))
30486 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30487 +                       }
30488 +               } else
30489 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30490 +               AuDbgDentry(dentry);
30491 +       }
30492 +       dput(parent);
30493 +
30494 +       if (!err) {
30495 +               if (do_idop)
30496 +                       au_refresh_dop(dentry, /*force_reval*/0);
30497 +       } else
30498 +               au_refresh_dop(dentry, /*force_reval*/1);
30499 +
30500 +       AuTraceErr(err);
30501 +       return err;
30502 +}
30503 +
30504 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30505 +{
30506 +       int err, i, j, ndentry, e;
30507 +       unsigned int sigen;
30508 +       struct au_dcsub_pages dpages;
30509 +       struct au_dpage *dpage;
30510 +       struct dentry **dentries, *d;
30511 +       struct au_sbinfo *sbinfo;
30512 +       struct dentry *root = sb->s_root;
30513 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30514 +
30515 +       if (do_idop)
30516 +               au_refresh_dop(root, /*force_reval*/0);
30517 +
30518 +       err = au_dpages_init(&dpages, GFP_NOFS);
30519 +       if (unlikely(err))
30520 +               goto out;
30521 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30522 +       if (unlikely(err))
30523 +               goto out_dpages;
30524 +
30525 +       sigen = au_sigen(sb);
30526 +       sbinfo = au_sbi(sb);
30527 +       for (i = 0; i < dpages.ndpage; i++) {
30528 +               dpage = dpages.dpages + i;
30529 +               dentries = dpage->dentries;
30530 +               ndentry = dpage->ndentry;
30531 +               for (j = 0; j < ndentry; j++) {
30532 +                       d = dentries[j];
30533 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30534 +                                           do_idop);
30535 +                       if (unlikely(e && !err))
30536 +                               err = e;
30537 +                       /* go on even err */
30538 +               }
30539 +       }
30540 +
30541 +out_dpages:
30542 +       au_dpages_free(&dpages);
30543 +out:
30544 +       return err;
30545 +}
30546 +
30547 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30548 +{
30549 +       int err, e;
30550 +       unsigned int sigen;
30551 +       unsigned long long max, ull;
30552 +       struct inode *inode, **array;
30553 +
30554 +       array = au_iarray_alloc(sb, &max);
30555 +       err = PTR_ERR(array);
30556 +       if (IS_ERR(array))
30557 +               goto out;
30558 +
30559 +       err = 0;
30560 +       sigen = au_sigen(sb);
30561 +       for (ull = 0; ull < max; ull++) {
30562 +               inode = array[ull];
30563 +               if (unlikely(!inode))
30564 +                       break;
30565 +
30566 +               e = 0;
30567 +               ii_write_lock_child(inode);
30568 +               if (au_iigen(inode, NULL) != sigen) {
30569 +                       e = au_refresh_hinode_self(inode);
30570 +                       if (unlikely(e)) {
30571 +                               au_refresh_iop(inode, /*force_getattr*/1);
30572 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30573 +                               if (!err)
30574 +                                       err = e;
30575 +                               /* go on even if err */
30576 +                       }
30577 +               }
30578 +               if (!e && do_idop)
30579 +                       au_refresh_iop(inode, /*force_getattr*/0);
30580 +               ii_write_unlock(inode);
30581 +       }
30582 +
30583 +       au_iarray_free(array, max);
30584 +
30585 +out:
30586 +       return err;
30587 +}
30588 +
30589 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30590 +{
30591 +       int err, e;
30592 +       unsigned int udba;
30593 +       aufs_bindex_t bindex, bbot;
30594 +       struct dentry *root;
30595 +       struct inode *inode;
30596 +       struct au_branch *br;
30597 +       struct au_sbinfo *sbi;
30598 +
30599 +       au_sigen_inc(sb);
30600 +       sbi = au_sbi(sb);
30601 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30602 +
30603 +       root = sb->s_root;
30604 +       DiMustNoWaiters(root);
30605 +       inode = d_inode(root);
30606 +       IiMustNoWaiters(inode);
30607 +
30608 +       udba = au_opt_udba(sb);
30609 +       bbot = au_sbbot(sb);
30610 +       for (bindex = 0; bindex <= bbot; bindex++) {
30611 +               br = au_sbr(sb, bindex);
30612 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30613 +               if (unlikely(err))
30614 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30615 +                               bindex, err);
30616 +               /* go on even if err */
30617 +       }
30618 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30619 +
30620 +       if (do_idop) {
30621 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30622 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30623 +                       sb->s_d_op = &aufs_dop_noreval;
30624 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30625 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30626 +               } else {
30627 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30628 +                       sb->s_d_op = &aufs_dop;
30629 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30630 +                       sbi->si_iop_array = aufs_iop;
30631 +               }
30632 +               pr_info("reset to %ps and %ps\n",
30633 +                       sb->s_d_op, sbi->si_iop_array);
30634 +       }
30635 +
30636 +       di_write_unlock(root);
30637 +       err = au_refresh_d(sb, do_idop);
30638 +       e = au_refresh_i(sb, do_idop);
30639 +       if (unlikely(e && !err))
30640 +               err = e;
30641 +       /* aufs_write_lock() calls ..._child() */
30642 +       di_write_lock_child(root);
30643 +
30644 +       au_cpup_attr_all(inode, /*force*/1);
30645 +
30646 +       if (unlikely(err))
30647 +               AuIOErr("refresh failed, ignored, %d\n", err);
30648 +}
30649 +
30650 +const struct super_operations aufs_sop = {
30651 +       .alloc_inode    = aufs_alloc_inode,
30652 +       .destroy_inode  = aufs_destroy_inode,
30653 +       .free_inode     = aufs_free_inode,
30654 +       /* always deleting, no clearing */
30655 +       .drop_inode     = generic_delete_inode,
30656 +       .show_options   = aufs_show_options,
30657 +       .statfs         = aufs_statfs,
30658 +       .put_super      = aufs_put_super,
30659 +       .sync_fs        = aufs_sync_fs
30660 +};
30661 +
30662 +/* ---------------------------------------------------------------------- */
30663 +
30664 +int au_alloc_root(struct super_block *sb)
30665 +{
30666 +       int err;
30667 +       struct inode *inode;
30668 +       struct dentry *root;
30669 +
30670 +       err = -ENOMEM;
30671 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30672 +       err = PTR_ERR(inode);
30673 +       if (IS_ERR(inode))
30674 +               goto out;
30675 +
30676 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30677 +       inode->i_fop = &aufs_dir_fop;
30678 +       inode->i_mode = S_IFDIR;
30679 +       set_nlink(inode, 2);
30680 +       unlock_new_inode(inode);
30681 +
30682 +       root = d_make_root(inode);
30683 +       if (unlikely(!root))
30684 +               goto out;
30685 +       err = PTR_ERR(root);
30686 +       if (IS_ERR(root))
30687 +               goto out;
30688 +
30689 +       err = au_di_init(root);
30690 +       if (!err) {
30691 +               sb->s_root = root;
30692 +               return 0; /* success */
30693 +       }
30694 +       dput(root);
30695 +
30696 +out:
30697 +       return err;
30698 +}
30699 +
30700 +/* ---------------------------------------------------------------------- */
30701 +
30702 +static void aufs_kill_sb(struct super_block *sb)
30703 +{
30704 +       struct au_sbinfo *sbinfo;
30705 +       struct dentry *root;
30706 +
30707 +       sbinfo = au_sbi(sb);
30708 +       if (!sbinfo)
30709 +               goto out;
30710 +
30711 +       au_sbilist_del(sb);
30712 +
30713 +       root = sb->s_root;
30714 +       if (root)
30715 +               aufs_write_lock(root);
30716 +       else
30717 +               __si_write_lock(sb);
30718 +
30719 +       au_fhsm_fin(sb);
30720 +       if (sbinfo->si_wbr_create_ops->fin)
30721 +               sbinfo->si_wbr_create_ops->fin(sb);
30722 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30723 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30724 +               au_remount_refresh(sb, /*do_idop*/0);
30725 +       }
30726 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30727 +               au_plink_put(sb, /*verbose*/1);
30728 +       au_xino_clr(sb);
30729 +       if (root)
30730 +               au_dr_opt_flush(sb);
30731 +
30732 +       if (root)
30733 +               aufs_write_unlock(root);
30734 +       else
30735 +               __si_write_unlock(sb);
30736 +
30737 +       sbinfo->si_sb = NULL;
30738 +       au_nwt_flush(&sbinfo->si_nowait);
30739 +
30740 +out:
30741 +       kill_anon_super(sb);
30742 +}
30743 +
30744 +struct file_system_type aufs_fs_type = {
30745 +       .name           = AUFS_FSTYPE,
30746 +       /* a race between rename and others */
30747 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30748 +                               /* untested */
30749 +                               /*| FS_ALLOW_IDMAP*/
30750 +                               ,
30751 +       .init_fs_context = aufs_fsctx_init,
30752 +       .parameters     = aufs_fsctx_paramspec,
30753 +       .kill_sb        = aufs_kill_sb,
30754 +       /* no need to __module_get() and module_put(). */
30755 +       .owner          = THIS_MODULE,
30756 +};
30757 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30758 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30759 +++ linux/fs/aufs/super.h       2024-03-10 23:40:47.086476782 +0100
30760 @@ -0,0 +1,592 @@
30761 +/* SPDX-License-Identifier: GPL-2.0 */
30762 +/*
30763 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30764 + *
30765 + * This program is free software; you can redistribute it and/or modify
30766 + * it under the terms of the GNU General Public License as published by
30767 + * the Free Software Foundation; either version 2 of the License, or
30768 + * (at your option) any later version.
30769 + *
30770 + * This program is distributed in the hope that it will be useful,
30771 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30772 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30773 + * GNU General Public License for more details.
30774 + *
30775 + * You should have received a copy of the GNU General Public License
30776 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30777 + */
30778 +
30779 +/*
30780 + * super_block operations
30781 + */
30782 +
30783 +#ifndef __AUFS_SUPER_H__
30784 +#define __AUFS_SUPER_H__
30785 +
30786 +#ifdef __KERNEL__
30787 +
30788 +#include <linux/fs.h>
30789 +#include <linux/kobject.h>
30790 +#include "hbl.h"
30791 +#include "lcnt.h"
30792 +#include "rwsem.h"
30793 +#include "wkq.h"
30794 +
30795 +/* policies to select one among multiple writable branches */
30796 +struct au_wbr_copyup_operations {
30797 +       int (*copyup)(struct dentry *dentry);
30798 +};
30799 +
30800 +#define AuWbr_DIR      BIT(0)          /* target is a dir */
30801 +#define AuWbr_PARENT   BIT(1)          /* always require a parent */
30802 +
30803 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30804 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30805 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30806 +
30807 +struct au_wbr_create_operations {
30808 +       int (*create)(struct dentry *dentry, unsigned int flags);
30809 +       int (*init)(struct super_block *sb);
30810 +       int (*fin)(struct super_block *sb);
30811 +};
30812 +
30813 +struct au_wbr_mfs {
30814 +       struct mutex    mfs_lock; /* protect this structure */
30815 +       unsigned long   mfs_jiffy;
30816 +       unsigned long   mfs_expire;
30817 +       aufs_bindex_t   mfs_bindex;
30818 +
30819 +       unsigned long long      mfsrr_bytes;
30820 +       unsigned long long      mfsrr_watermark;
30821 +};
30822 +
30823 +#define AuPlink_NHASH 100
30824 +static inline int au_plink_hash(ino_t ino)
30825 +{
30826 +       return ino % AuPlink_NHASH;
30827 +}
30828 +
30829 +/* File-based Hierarchical Storage Management */
30830 +struct au_fhsm {
30831 +#ifdef CONFIG_AUFS_FHSM
30832 +       /* allow only one process who can receive the notification */
30833 +       spinlock_t              fhsm_spin;
30834 +       pid_t                   fhsm_pid;
30835 +       wait_queue_head_t       fhsm_wqh;
30836 +       atomic_t                fhsm_readable;
30837 +
30838 +       /* these are protected by si_rwsem */
30839 +       unsigned long           fhsm_expire;
30840 +       aufs_bindex_t           fhsm_bottom;
30841 +#endif
30842 +};
30843 +
30844 +struct au_branch;
30845 +struct au_sbinfo {
30846 +       /* nowait tasks in the system-wide workqueue */
30847 +       struct au_nowait_tasks  si_nowait;
30848 +
30849 +       /*
30850 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30851 +        * rwsem for au_sbinfo is necessary.
30852 +        */
30853 +       struct au_rwsem         si_rwsem;
30854 +
30855 +       /*
30856 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30857 +        * remount.
30858 +        */
30859 +       au_lcnt_t               si_ninodes, si_nfiles;
30860 +
30861 +       /* branch management */
30862 +       unsigned int            si_generation;
30863 +
30864 +       /* see AuSi_ flags */
30865 +       unsigned char           au_si_status;
30866 +
30867 +       aufs_bindex_t           si_bbot;
30868 +
30869 +       /* dirty trick to keep br_id plus */
30870 +       unsigned int            si_last_br_id :
30871 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30872 +       struct au_branch        **si_branch;
30873 +
30874 +       /* policy to select a writable branch */
30875 +       unsigned char           si_wbr_copyup;
30876 +       unsigned char           si_wbr_create;
30877 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30878 +       struct au_wbr_create_operations *si_wbr_create_ops;
30879 +
30880 +       /* round robin */
30881 +       atomic_t                si_wbr_rr_next;
30882 +
30883 +       /* most free space */
30884 +       struct au_wbr_mfs       si_wbr_mfs;
30885 +
30886 +       /* File-based Hierarchical Storage Management */
30887 +       struct au_fhsm          si_fhsm;
30888 +
30889 +       /* mount flags */
30890 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30891 +       unsigned int            si_mntflags;
30892 +
30893 +       /* external inode number (bitmap and translation table) */
30894 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30895 +
30896 +       struct file             *si_xib;
30897 +       struct mutex            si_xib_mtx; /* protect xib members */
30898 +       unsigned long           *si_xib_buf;
30899 +       unsigned long           si_xib_last_pindex;
30900 +       int                     si_xib_next_bit;
30901 +
30902 +       unsigned long           si_xino_jiffy;
30903 +       unsigned long           si_xino_expire;
30904 +       /* reserved for future use */
30905 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30906 +
30907 +#ifdef CONFIG_AUFS_EXPORT
30908 +       /* i_generation */
30909 +       /* todo: make xigen file an array to support many inode numbers */
30910 +       struct file             *si_xigen;
30911 +       atomic_t                si_xigen_next;
30912 +#endif
30913 +
30914 +       /* dirty trick to support atomic_open */
30915 +       struct hlist_bl_head    si_aopen;
30916 +
30917 +       /* vdir parameters */
30918 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30919 +       unsigned int            si_rdblk;       /* deblk size */
30920 +       unsigned int            si_rdhash;      /* hash size */
30921 +
30922 +       /*
30923 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30924 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30925 +        * future fsck.aufs or kernel thread will remove them later.
30926 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30927 +        */
30928 +       unsigned int            si_dirwh;
30929 +
30930 +       /* pseudo_link list */
30931 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30932 +       wait_queue_head_t       si_plink_wq;
30933 +       spinlock_t              si_plink_maint_lock;
30934 +       pid_t                   si_plink_maint_pid;
30935 +
30936 +       /* file list */
30937 +       struct hlist_bl_head    si_files;
30938 +
30939 +       /* with/without getattr, brother of sb->s_d_op */
30940 +       const struct inode_operations *si_iop_array;
30941 +
30942 +       /*
30943 +        * sysfs and lifetime management.
30944 +        * this is not a small structure and it may be a waste of memory in case
30945 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30946 +        * but using sysfs is majority.
30947 +        */
30948 +       struct kobject          si_kobj;
30949 +#ifdef CONFIG_DEBUG_FS
30950 +       struct dentry            *si_dbgaufs;
30951 +       struct dentry            *si_dbgaufs_plink;
30952 +       struct dentry            *si_dbgaufs_xib;
30953 +#ifdef CONFIG_AUFS_EXPORT
30954 +       struct dentry            *si_dbgaufs_xigen;
30955 +#endif
30956 +#endif
30957 +
30958 +#ifdef CONFIG_AUFS_SBILIST
30959 +       struct hlist_bl_node    si_list;
30960 +#endif
30961 +
30962 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30963 +       struct super_block      *si_sb;
30964 +};
30965 +
30966 +/* sbinfo status flags */
30967 +/*
30968 + * set true when refresh_dirs() failed at remount time.
30969 + * then try refreshing dirs at access time again.
30970 + * if it is false, refreshing dirs at access time is unnecessary
30971 + */
30972 +#define AuSi_FAILED_REFRESH_DIR        BIT(0)
30973 +#define AuSi_FHSM              BIT(1)          /* fhsm is active now */
30974 +#define AuSi_NO_DREVAL         BIT(2)          /* disable all d_revalidate */
30975 +
30976 +#ifndef CONFIG_AUFS_FHSM
30977 +#undef AuSi_FHSM
30978 +#define AuSi_FHSM              0
30979 +#endif
30980 +
30981 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30982 +                                          unsigned int flag)
30983 +{
30984 +       AuRwMustAnyLock(&sbi->si_rwsem);
30985 +       return sbi->au_si_status & flag;
30986 +}
30987 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30988 +#define au_fset_si(sbinfo, name) do { \
30989 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30990 +       (sbinfo)->au_si_status |= AuSi_##name; \
30991 +} while (0)
30992 +#define au_fclr_si(sbinfo, name) do { \
30993 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30994 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30995 +} while (0)
30996 +
30997 +/* ---------------------------------------------------------------------- */
30998 +
30999 +/* policy to select one among writable branches */
31000 +#define AuWbrCopyup(sbinfo, ...) \
31001 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
31002 +#define AuWbrCreate(sbinfo, ...) \
31003 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
31004 +
31005 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
31006 +#define AuLock_DW              BIT(0)          /* write-lock dentry */
31007 +#define AuLock_IR              BIT(1)          /* read-lock inode */
31008 +#define AuLock_IW              BIT(2)          /* write-lock inode */
31009 +#define AuLock_FLUSH           BIT(3)          /* wait for 'nowait' tasks */
31010 +#define AuLock_DIRS            BIT(4)          /* target is a pair of dirs */
31011 +                                               /* except RENAME_EXCHANGE */
31012 +#define AuLock_NOPLM           BIT(5)          /* return err in plm mode */
31013 +#define AuLock_NOPLMW          BIT(6)          /* wait for plm mode ends */
31014 +#define AuLock_GEN             BIT(7)          /* test digen/iigen */
31015 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
31016 +#define au_fset_lock(flags, name) \
31017 +       do { (flags) |= AuLock_##name; } while (0)
31018 +#define au_fclr_lock(flags, name) \
31019 +       do { (flags) &= ~AuLock_##name; } while (0)
31020 +
31021 +/* ---------------------------------------------------------------------- */
31022 +
31023 +/* super.c */
31024 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
31025 +
31026 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
31027 +                                          unsigned long long max, void *arg);
31028 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
31029 +                    struct super_block *sb, void *arg);
31030 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
31031 +void au_iarray_free(struct inode **a, unsigned long long max);
31032 +
31033 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31034 +extern const struct super_operations aufs_sop;
31035 +int au_alloc_root(struct super_block *sb);
31036 +extern struct file_system_type aufs_fs_type;
31037 +
31038 +/* sbinfo.c */
31039 +void au_si_free(struct kobject *kobj);
31040 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31041 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31042 +
31043 +unsigned int au_sigen_inc(struct super_block *sb);
31044 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31045 +
31046 +int si_read_lock(struct super_block *sb, int flags);
31047 +int si_write_lock(struct super_block *sb, int flags);
31048 +int aufs_read_lock(struct dentry *dentry, int flags);
31049 +void aufs_read_unlock(struct dentry *dentry, int flags);
31050 +void aufs_write_lock(struct dentry *dentry);
31051 +void aufs_write_unlock(struct dentry *dentry);
31052 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31053 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31054 +
31055 +/* wbr_policy.c */
31056 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31057 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31058 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31059 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31060 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31061 +
31062 +/* mvdown.c */
31063 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31064 +
31065 +#ifdef CONFIG_AUFS_FHSM
31066 +/* fhsm.c */
31067 +
31068 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31069 +{
31070 +       pid_t pid;
31071 +
31072 +       spin_lock(&fhsm->fhsm_spin);
31073 +       pid = fhsm->fhsm_pid;
31074 +       spin_unlock(&fhsm->fhsm_spin);
31075 +
31076 +       return pid;
31077 +}
31078 +
31079 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31080 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31081 +int au_fhsm_fd(struct super_block *sb, int oflags);
31082 +int au_fhsm_br_alloc(struct au_branch *br);
31083 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31084 +void au_fhsm_fin(struct super_block *sb);
31085 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31086 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31087 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31088 +#else
31089 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31090 +          int force)
31091 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31092 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31093 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31094 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31095 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31096 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31097 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31098 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31099 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31100 +#endif
31101 +
31102 +/* ---------------------------------------------------------------------- */
31103 +
31104 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31105 +{
31106 +       return sb->s_fs_info;
31107 +}
31108 +
31109 +/* ---------------------------------------------------------------------- */
31110 +
31111 +#ifdef CONFIG_AUFS_EXPORT
31112 +int au_test_nfsd(void);
31113 +void au_export_init(struct super_block *sb);
31114 +void au_xigen_inc(struct inode *inode);
31115 +int au_xigen_new(struct inode *inode);
31116 +int au_xigen_set(struct super_block *sb, struct path *path);
31117 +void au_xigen_clr(struct super_block *sb);
31118 +
31119 +static inline int au_busy_or_stale(void)
31120 +{
31121 +       if (!au_test_nfsd())
31122 +               return -EBUSY;
31123 +       return -ESTALE;
31124 +}
31125 +#else
31126 +AuStubInt0(au_test_nfsd, void)
31127 +AuStubVoid(au_export_init, struct super_block *sb)
31128 +AuStubVoid(au_xigen_inc, struct inode *inode)
31129 +AuStubInt0(au_xigen_new, struct inode *inode)
31130 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31131 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31132 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31133 +#endif /* CONFIG_AUFS_EXPORT */
31134 +
31135 +/* ---------------------------------------------------------------------- */
31136 +
31137 +#ifdef CONFIG_AUFS_SBILIST
31138 +/* module.c */
31139 +extern struct hlist_bl_head au_sbilist;
31140 +
31141 +static inline void au_sbilist_init(void)
31142 +{
31143 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31144 +}
31145 +
31146 +static inline void au_sbilist_add(struct super_block *sb)
31147 +{
31148 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31149 +}
31150 +
31151 +static inline void au_sbilist_del(struct super_block *sb)
31152 +{
31153 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31154 +}
31155 +
31156 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31157 +static inline void au_sbilist_lock(void)
31158 +{
31159 +       hlist_bl_lock(&au_sbilist);
31160 +}
31161 +
31162 +static inline void au_sbilist_unlock(void)
31163 +{
31164 +       hlist_bl_unlock(&au_sbilist);
31165 +}
31166 +#define AuGFP_SBILIST  GFP_ATOMIC
31167 +#else
31168 +AuStubVoid(au_sbilist_lock, void)
31169 +AuStubVoid(au_sbilist_unlock, void)
31170 +#define AuGFP_SBILIST  GFP_NOFS
31171 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31172 +#else
31173 +AuStubVoid(au_sbilist_init, void)
31174 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31175 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31176 +AuStubVoid(au_sbilist_lock, void)
31177 +AuStubVoid(au_sbilist_unlock, void)
31178 +#define AuGFP_SBILIST  GFP_NOFS
31179 +#endif
31180 +
31181 +/* ---------------------------------------------------------------------- */
31182 +
31183 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31184 +{
31185 +       /*
31186 +        * This function is a dynamic '__init' function actually,
31187 +        * so the tiny check for si_rwsem is unnecessary.
31188 +        */
31189 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31190 +#ifdef CONFIG_DEBUG_FS
31191 +       sbinfo->si_dbgaufs = NULL;
31192 +       sbinfo->si_dbgaufs_plink = NULL;
31193 +       sbinfo->si_dbgaufs_xib = NULL;
31194 +#ifdef CONFIG_AUFS_EXPORT
31195 +       sbinfo->si_dbgaufs_xigen = NULL;
31196 +#endif
31197 +#endif
31198 +}
31199 +
31200 +/* ---------------------------------------------------------------------- */
31201 +
31202 +/* current->atomic_flags */
31203 +/* this value should never corrupt the ones defined in linux/sched.h */
31204 +#define PFA_AUFS       0x10
31205 +
31206 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31207 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31208 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31209 +
31210 +static inline int si_pid_test(struct super_block *sb)
31211 +{
31212 +       return !!task_test_aufs(current);
31213 +}
31214 +
31215 +static inline void si_pid_clr(struct super_block *sb)
31216 +{
31217 +       AuDebugOn(!task_test_aufs(current));
31218 +       task_clear_aufs(current);
31219 +}
31220 +
31221 +static inline void si_pid_set(struct super_block *sb)
31222 +{
31223 +       AuDebugOn(task_test_aufs(current));
31224 +       task_set_aufs(current);
31225 +}
31226 +
31227 +/* ---------------------------------------------------------------------- */
31228 +
31229 +/* lock superblock. mainly for entry point functions */
31230 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31231 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31232 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31233 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31234 +/*
31235 +#define __si_read_trylock_nested(sb) \
31236 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31237 +#define __si_write_trylock_nested(sb) \
31238 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31239 +*/
31240 +
31241 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31242 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31243 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31244 +
31245 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31246 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31247 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31248 +
31249 +static inline void si_noflush_read_lock(struct super_block *sb)
31250 +{
31251 +       __si_read_lock(sb);
31252 +       si_pid_set(sb);
31253 +}
31254 +
31255 +static inline int si_noflush_read_trylock(struct super_block *sb)
31256 +{
31257 +       int locked;
31258 +
31259 +       locked = __si_read_trylock(sb);
31260 +       if (locked)
31261 +               si_pid_set(sb);
31262 +       return locked;
31263 +}
31264 +
31265 +static inline void si_noflush_write_lock(struct super_block *sb)
31266 +{
31267 +       __si_write_lock(sb);
31268 +       si_pid_set(sb);
31269 +}
31270 +
31271 +static inline int si_noflush_write_trylock(struct super_block *sb)
31272 +{
31273 +       int locked;
31274 +
31275 +       locked = __si_write_trylock(sb);
31276 +       if (locked)
31277 +               si_pid_set(sb);
31278 +       return locked;
31279 +}
31280 +
31281 +#if 0 /* reserved */
31282 +static inline int si_read_trylock(struct super_block *sb, int flags)
31283 +{
31284 +       if (au_ftest_lock(flags, FLUSH))
31285 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31286 +       return si_noflush_read_trylock(sb);
31287 +}
31288 +#endif
31289 +
31290 +static inline void si_read_unlock(struct super_block *sb)
31291 +{
31292 +       si_pid_clr(sb);
31293 +       __si_read_unlock(sb);
31294 +}
31295 +
31296 +#if 0 /* reserved */
31297 +static inline int si_write_trylock(struct super_block *sb, int flags)
31298 +{
31299 +       if (au_ftest_lock(flags, FLUSH))
31300 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31301 +       return si_noflush_write_trylock(sb);
31302 +}
31303 +#endif
31304 +
31305 +static inline void si_write_unlock(struct super_block *sb)
31306 +{
31307 +       si_pid_clr(sb);
31308 +       __si_write_unlock(sb);
31309 +}
31310 +
31311 +#if 0 /* reserved */
31312 +static inline void si_downgrade_lock(struct super_block *sb)
31313 +{
31314 +       __si_downgrade_lock(sb);
31315 +}
31316 +#endif
31317 +
31318 +/* ---------------------------------------------------------------------- */
31319 +
31320 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31321 +{
31322 +       SiMustAnyLock(sb);
31323 +       return au_sbi(sb)->si_bbot;
31324 +}
31325 +
31326 +static inline unsigned int au_mntflags(struct super_block *sb)
31327 +{
31328 +       SiMustAnyLock(sb);
31329 +       return au_sbi(sb)->si_mntflags;
31330 +}
31331 +
31332 +static inline unsigned int au_sigen(struct super_block *sb)
31333 +{
31334 +       SiMustAnyLock(sb);
31335 +       return au_sbi(sb)->si_generation;
31336 +}
31337 +
31338 +static inline struct au_branch *au_sbr(struct super_block *sb,
31339 +                                      aufs_bindex_t bindex)
31340 +{
31341 +       SiMustAnyLock(sb);
31342 +       return au_sbi(sb)->si_branch[0 + bindex];
31343 +}
31344 +
31345 +static inline loff_t au_xi_maxent(struct super_block *sb)
31346 +{
31347 +       SiMustAnyLock(sb);
31348 +       return au_sbi(sb)->si_ximaxent;
31349 +}
31350 +
31351 +#endif /* __KERNEL__ */
31352 +#endif /* __AUFS_SUPER_H__ */
31353 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31354 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31355 +++ linux/fs/aufs/sysaufs.c     2023-10-10 22:51:18.033248030 +0200
31356 @@ -0,0 +1,94 @@
31357 +// SPDX-License-Identifier: GPL-2.0
31358 +/*
31359 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31360 + *
31361 + * This program is free software; you can redistribute it and/or modify
31362 + * it under the terms of the GNU General Public License as published by
31363 + * the Free Software Foundation; either version 2 of the License, or
31364 + * (at your option) any later version.
31365 + *
31366 + * This program is distributed in the hope that it will be useful,
31367 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31368 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31369 + * GNU General Public License for more details.
31370 + *
31371 + * You should have received a copy of the GNU General Public License
31372 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31373 + */
31374 +
31375 +/*
31376 + * sysfs interface and lifetime management
31377 + * they are necessary regardless sysfs is disabled.
31378 + */
31379 +
31380 +#include <linux/random.h>
31381 +#include "aufs.h"
31382 +
31383 +unsigned long sysaufs_si_mask;
31384 +struct kset *sysaufs_kset;
31385 +
31386 +#define AuSiAttr(_name) { \
31387 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31388 +       .show   = sysaufs_si_##_name,                           \
31389 +}
31390 +
31391 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31392 +struct attribute *sysaufs_si_attrs[] = {
31393 +       &sysaufs_si_attr_xi_path.attr,
31394 +       NULL,
31395 +};
31396 +ATTRIBUTE_GROUPS(sysaufs_si);
31397 +
31398 +static const struct sysfs_ops au_sbi_ops = {
31399 +       .show   = sysaufs_si_show
31400 +};
31401 +
31402 +static const struct kobj_type au_sbi_ktype = {
31403 +       .release        = au_si_free,
31404 +       .sysfs_ops      = &au_sbi_ops,
31405 +       .default_groups = sysaufs_si_groups
31406 +};
31407 +
31408 +/* ---------------------------------------------------------------------- */
31409 +
31410 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31411 +{
31412 +       int err;
31413 +
31414 +       sbinfo->si_kobj.kset = sysaufs_kset;
31415 +       /* cf. sysaufs_name() */
31416 +       err = kobject_init_and_add
31417 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31418 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31419 +
31420 +       return err;
31421 +}
31422 +
31423 +void sysaufs_fin(void)
31424 +{
31425 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31426 +       kset_unregister(sysaufs_kset);
31427 +}
31428 +
31429 +int __init sysaufs_init(void)
31430 +{
31431 +       int err;
31432 +
31433 +       do {
31434 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31435 +       } while (!sysaufs_si_mask);
31436 +
31437 +       err = -EINVAL;
31438 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31439 +       if (unlikely(!sysaufs_kset))
31440 +               goto out;
31441 +       err = PTR_ERR(sysaufs_kset);
31442 +       if (IS_ERR(sysaufs_kset))
31443 +               goto out;
31444 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31445 +       if (unlikely(err))
31446 +               kset_unregister(sysaufs_kset);
31447 +
31448 +out:
31449 +       return err;
31450 +}
31451 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31452 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31453 +++ linux/fs/aufs/sysaufs.h     2022-11-05 23:02:18.969222617 +0100
31454 @@ -0,0 +1,102 @@
31455 +/* SPDX-License-Identifier: GPL-2.0 */
31456 +/*
31457 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31458 + *
31459 + * This program is free software; you can redistribute it and/or modify
31460 + * it under the terms of the GNU General Public License as published by
31461 + * the Free Software Foundation; either version 2 of the License, or
31462 + * (at your option) any later version.
31463 + *
31464 + * This program is distributed in the hope that it will be useful,
31465 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31466 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31467 + * GNU General Public License for more details.
31468 + *
31469 + * You should have received a copy of the GNU General Public License
31470 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31471 + */
31472 +
31473 +/*
31474 + * sysfs interface and mount lifetime management
31475 + */
31476 +
31477 +#ifndef __SYSAUFS_H__
31478 +#define __SYSAUFS_H__
31479 +
31480 +#ifdef __KERNEL__
31481 +
31482 +#include <linux/sysfs.h>
31483 +#include "module.h"
31484 +
31485 +struct super_block;
31486 +struct au_sbinfo;
31487 +
31488 +struct sysaufs_si_attr {
31489 +       struct attribute attr;
31490 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31491 +};
31492 +
31493 +/* ---------------------------------------------------------------------- */
31494 +
31495 +/* sysaufs.c */
31496 +extern unsigned long sysaufs_si_mask;
31497 +extern struct kset *sysaufs_kset;
31498 +extern struct attribute *sysaufs_si_attrs[];
31499 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31500 +int __init sysaufs_init(void);
31501 +void sysaufs_fin(void);
31502 +
31503 +/* ---------------------------------------------------------------------- */
31504 +
31505 +/* some people doesn't like to show a pointer in kernel */
31506 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31507 +{
31508 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31509 +}
31510 +
31511 +#define SysaufsSiNamePrefix    "si_"
31512 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31513 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31514 +{
31515 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31516 +                sysaufs_si_id(sbinfo));
31517 +}
31518 +
31519 +struct au_branch;
31520 +#ifdef CONFIG_SYSFS
31521 +/* sysfs.c */
31522 +extern struct attribute_group *sysaufs_attr_group;
31523 +
31524 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31525 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31526 +                        char *buf);
31527 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31528 +#ifdef CONFIG_COMPAT
31529 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31530 +#endif
31531 +
31532 +void sysaufs_br_init(struct au_branch *br);
31533 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31534 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31535 +
31536 +#define sysaufs_brs_init()     do {} while (0)
31537 +
31538 +#else
31539 +#define sysaufs_attr_group     NULL
31540 +
31541 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31542 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31543 +       struct attribute *attr, char *buf)
31544 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31545 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31546 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31547 +
31548 +static inline void sysaufs_brs_init(void)
31549 +{
31550 +       sysaufs_brs = 0;
31551 +}
31552 +
31553 +#endif /* CONFIG_SYSFS */
31554 +
31555 +#endif /* __KERNEL__ */
31556 +#endif /* __SYSAUFS_H__ */
31557 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31558 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31559 +++ linux/fs/aufs/sysfs.c       2022-11-05 23:02:18.969222617 +0100
31560 @@ -0,0 +1,374 @@
31561 +// SPDX-License-Identifier: GPL-2.0
31562 +/*
31563 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31564 + *
31565 + * This program is free software; you can redistribute it and/or modify
31566 + * it under the terms of the GNU General Public License as published by
31567 + * the Free Software Foundation; either version 2 of the License, or
31568 + * (at your option) any later version.
31569 + *
31570 + * This program is distributed in the hope that it will be useful,
31571 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31572 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31573 + * GNU General Public License for more details.
31574 + *
31575 + * You should have received a copy of the GNU General Public License
31576 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31577 + */
31578 +
31579 +/*
31580 + * sysfs interface
31581 + */
31582 +
31583 +#include <linux/compat.h>
31584 +#include <linux/seq_file.h>
31585 +#include "aufs.h"
31586 +
31587 +#ifdef CONFIG_AUFS_FS_MODULE
31588 +/* this entry violates the "one line per file" policy of sysfs */
31589 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31590 +                          char *buf)
31591 +{
31592 +       ssize_t err;
31593 +       static char *conf =
31594 +/* this file is generated at compiling */
31595 +#include "conf.str"
31596 +               ;
31597 +
31598 +       err = snprintf(buf, PAGE_SIZE, conf);
31599 +       if (unlikely(err >= PAGE_SIZE))
31600 +               err = -EFBIG;
31601 +       return err;
31602 +}
31603 +
31604 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31605 +#endif
31606 +
31607 +static struct attribute *au_attr[] = {
31608 +#ifdef CONFIG_AUFS_FS_MODULE
31609 +       &au_config_attr.attr,
31610 +#endif
31611 +       NULL,   /* need to NULL terminate the list of attributes */
31612 +};
31613 +
31614 +static struct attribute_group sysaufs_attr_group_body = {
31615 +       .attrs = au_attr
31616 +};
31617 +
31618 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31619 +
31620 +/* ---------------------------------------------------------------------- */
31621 +
31622 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31623 +{
31624 +       int err;
31625 +
31626 +       SiMustAnyLock(sb);
31627 +
31628 +       err = 0;
31629 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31630 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31631 +               seq_putc(seq, '\n');
31632 +       }
31633 +       return err;
31634 +}
31635 +
31636 +/*
31637 + * the lifetime of branch is independent from the entry under sysfs.
31638 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31639 + * unlinked.
31640 + */
31641 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31642 +                        aufs_bindex_t bindex, int idx)
31643 +{
31644 +       int err;
31645 +       struct path path;
31646 +       struct dentry *root;
31647 +       struct au_branch *br;
31648 +       au_br_perm_str_t perm;
31649 +
31650 +       AuDbg("b%d\n", bindex);
31651 +
31652 +       err = 0;
31653 +       root = sb->s_root;
31654 +       di_read_lock_parent(root, !AuLock_IR);
31655 +       br = au_sbr(sb, bindex);
31656 +
31657 +       switch (idx) {
31658 +       case AuBrSysfs_BR:
31659 +               path.mnt = au_br_mnt(br);
31660 +               path.dentry = au_h_dptr(root, bindex);
31661 +               err = au_seq_path(seq, &path);
31662 +               if (!err) {
31663 +                       au_optstr_br_perm(&perm, br->br_perm);
31664 +                       seq_printf(seq, "=%s\n", perm.a);
31665 +               }
31666 +               break;
31667 +       case AuBrSysfs_BRID:
31668 +               seq_printf(seq, "%d\n", br->br_id);
31669 +               break;
31670 +       }
31671 +       di_read_unlock(root, !AuLock_IR);
31672 +       if (unlikely(err || seq_has_overflowed(seq)))
31673 +               err = -E2BIG;
31674 +
31675 +       return err;
31676 +}
31677 +
31678 +/* ---------------------------------------------------------------------- */
31679 +
31680 +static struct seq_file *au_seq(char *p, ssize_t len)
31681 +{
31682 +       struct seq_file *seq;
31683 +
31684 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31685 +       if (seq) {
31686 +               /* mutex_init(&seq.lock); */
31687 +               seq->buf = p;
31688 +               seq->size = len;
31689 +               return seq; /* success */
31690 +       }
31691 +
31692 +       seq = ERR_PTR(-ENOMEM);
31693 +       return seq;
31694 +}
31695 +
31696 +#define SysaufsBr_PREFIX       "br"
31697 +#define SysaufsBrid_PREFIX     "brid"
31698 +
31699 +/* todo: file size may exceed PAGE_SIZE */
31700 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31701 +                       char *buf)
31702 +{
31703 +       ssize_t err;
31704 +       int idx;
31705 +       long l;
31706 +       aufs_bindex_t bbot;
31707 +       struct au_sbinfo *sbinfo;
31708 +       struct super_block *sb;
31709 +       struct seq_file *seq;
31710 +       char *name;
31711 +       struct attribute **cattr;
31712 +
31713 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31714 +       sb = sbinfo->si_sb;
31715 +
31716 +       /*
31717 +        * prevent a race condition between sysfs and aufs.
31718 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31719 +        * prohibits maintaining the sysfs entries.
31720 +        * hew we acquire read lock after sysfs_get_active_two().
31721 +        * on the other hand, the remount process may maintain the sysfs/aufs
31722 +        * entries after acquiring write lock.
31723 +        * it can cause a deadlock.
31724 +        * simply we gave up processing read here.
31725 +        */
31726 +       err = -EBUSY;
31727 +       if (unlikely(!si_noflush_read_trylock(sb)))
31728 +               goto out;
31729 +
31730 +       seq = au_seq(buf, PAGE_SIZE);
31731 +       err = PTR_ERR(seq);
31732 +       if (IS_ERR(seq))
31733 +               goto out_unlock;
31734 +
31735 +       name = (void *)attr->name;
31736 +       cattr = sysaufs_si_attrs;
31737 +       while (*cattr) {
31738 +               if (!strcmp(name, (*cattr)->name)) {
31739 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31740 +                               ->show(seq, sb);
31741 +                       goto out_seq;
31742 +               }
31743 +               cattr++;
31744 +       }
31745 +
31746 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31747 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31748 +               idx = AuBrSysfs_BRID;
31749 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31750 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31751 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31752 +               idx = AuBrSysfs_BR;
31753 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31754 +       } else
31755 +                 BUG();
31756 +
31757 +       err = kstrtol(name, 10, &l);
31758 +       if (!err) {
31759 +               bbot = au_sbbot(sb);
31760 +               if (l <= bbot)
31761 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31762 +               else
31763 +                       err = -ENOENT;
31764 +       }
31765 +
31766 +out_seq:
31767 +       if (!err) {
31768 +               err = seq->count;
31769 +               /* sysfs limit */
31770 +               if (unlikely(err == PAGE_SIZE))
31771 +                       err = -EFBIG;
31772 +       }
31773 +       au_kfree_rcu(seq);
31774 +out_unlock:
31775 +       si_read_unlock(sb);
31776 +out:
31777 +       return err;
31778 +}
31779 +
31780 +/* ---------------------------------------------------------------------- */
31781 +
31782 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31783 +{
31784 +       int err;
31785 +       int16_t brid;
31786 +       aufs_bindex_t bindex, bbot;
31787 +       size_t sz;
31788 +       char *buf;
31789 +       struct seq_file *seq;
31790 +       struct au_branch *br;
31791 +
31792 +       si_read_lock(sb, AuLock_FLUSH);
31793 +       bbot = au_sbbot(sb);
31794 +       err = bbot + 1;
31795 +       if (!arg)
31796 +               goto out;
31797 +
31798 +       err = -ENOMEM;
31799 +       buf = (void *)__get_free_page(GFP_NOFS);
31800 +       if (unlikely(!buf))
31801 +               goto out;
31802 +
31803 +       seq = au_seq(buf, PAGE_SIZE);
31804 +       err = PTR_ERR(seq);
31805 +       if (IS_ERR(seq))
31806 +               goto out_buf;
31807 +
31808 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31809 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31810 +               /* VERIFY_WRITE */
31811 +               err = !access_ok(arg, sizeof(*arg));
31812 +               if (unlikely(err))
31813 +                       break;
31814 +
31815 +               br = au_sbr(sb, bindex);
31816 +               brid = br->br_id;
31817 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31818 +               err = __put_user(brid, &arg->id);
31819 +               if (unlikely(err))
31820 +                       break;
31821 +
31822 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31823 +               err = __put_user(br->br_perm, &arg->perm);
31824 +               if (unlikely(err))
31825 +                       break;
31826 +
31827 +               err = au_seq_path(seq, &br->br_path);
31828 +               if (unlikely(err))
31829 +                       break;
31830 +               seq_putc(seq, '\0');
31831 +               if (!seq_has_overflowed(seq)) {
31832 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31833 +                       seq->count = 0;
31834 +                       if (unlikely(err))
31835 +                               break;
31836 +               } else {
31837 +                       err = -E2BIG;
31838 +                       goto out_seq;
31839 +               }
31840 +       }
31841 +       if (unlikely(err))
31842 +               err = -EFAULT;
31843 +
31844 +out_seq:
31845 +       au_kfree_rcu(seq);
31846 +out_buf:
31847 +       free_page((unsigned long)buf);
31848 +out:
31849 +       si_read_unlock(sb);
31850 +       return err;
31851 +}
31852 +
31853 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31854 +{
31855 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31856 +}
31857 +
31858 +#ifdef CONFIG_COMPAT
31859 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31860 +{
31861 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31862 +}
31863 +#endif
31864 +
31865 +/* ---------------------------------------------------------------------- */
31866 +
31867 +void sysaufs_br_init(struct au_branch *br)
31868 +{
31869 +       int i;
31870 +       struct au_brsysfs *br_sysfs;
31871 +       struct attribute *attr;
31872 +
31873 +       br_sysfs = br->br_sysfs;
31874 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31875 +               attr = &br_sysfs->attr;
31876 +               sysfs_attr_init(attr);
31877 +               attr->name = br_sysfs->name;
31878 +               attr->mode = 0444;
31879 +               br_sysfs++;
31880 +       }
31881 +}
31882 +
31883 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31884 +{
31885 +       struct au_branch *br;
31886 +       struct kobject *kobj;
31887 +       struct au_brsysfs *br_sysfs;
31888 +       int i;
31889 +       aufs_bindex_t bbot;
31890 +
31891 +       if (!sysaufs_brs)
31892 +               return;
31893 +
31894 +       kobj = &au_sbi(sb)->si_kobj;
31895 +       bbot = au_sbbot(sb);
31896 +       for (; bindex <= bbot; bindex++) {
31897 +               br = au_sbr(sb, bindex);
31898 +               br_sysfs = br->br_sysfs;
31899 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31900 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31901 +                       br_sysfs++;
31902 +               }
31903 +       }
31904 +}
31905 +
31906 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31907 +{
31908 +       int err, i;
31909 +       aufs_bindex_t bbot;
31910 +       struct kobject *kobj;
31911 +       struct au_branch *br;
31912 +       struct au_brsysfs *br_sysfs;
31913 +
31914 +       if (!sysaufs_brs)
31915 +               return;
31916 +
31917 +       kobj = &au_sbi(sb)->si_kobj;
31918 +       bbot = au_sbbot(sb);
31919 +       for (; bindex <= bbot; bindex++) {
31920 +               br = au_sbr(sb, bindex);
31921 +               br_sysfs = br->br_sysfs;
31922 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31923 +                        SysaufsBr_PREFIX "%d", bindex);
31924 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31925 +                        SysaufsBrid_PREFIX "%d", bindex);
31926 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31927 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31928 +                       if (unlikely(err))
31929 +                               pr_warn("failed %s under sysfs(%d)\n",
31930 +                                       br_sysfs->name, err);
31931 +                       br_sysfs++;
31932 +               }
31933 +       }
31934 +}
31935 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31936 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31937 +++ linux/fs/aufs/sysrq.c       2023-10-31 09:31:04.199880750 +0100
31938 @@ -0,0 +1,149 @@
31939 +// SPDX-License-Identifier: GPL-2.0
31940 +/*
31941 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31942 + *
31943 + * This program is free software; you can redistribute it and/or modify
31944 + * it under the terms of the GNU General Public License as published by
31945 + * the Free Software Foundation; either version 2 of the License, or
31946 + * (at your option) any later version.
31947 + *
31948 + * This program is distributed in the hope that it will be useful,
31949 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31950 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31951 + * GNU General Public License for more details.
31952 + *
31953 + * You should have received a copy of the GNU General Public License
31954 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31955 + */
31956 +
31957 +/*
31958 + * magic sysrq handler
31959 + */
31960 +
31961 +/* #include <linux/sysrq.h> */
31962 +#include <linux/writeback.h>
31963 +#include "aufs.h"
31964 +
31965 +/* ---------------------------------------------------------------------- */
31966 +
31967 +static void sysrq_sb(struct super_block *sb)
31968 +{
31969 +       char *plevel;
31970 +       struct au_sbinfo *sbinfo;
31971 +       struct file *file;
31972 +       struct hlist_bl_head *files;
31973 +       struct hlist_bl_node *pos;
31974 +       struct au_finfo *finfo;
31975 +       struct inode *i;
31976 +
31977 +       plevel = au_plevel;
31978 +       au_plevel = KERN_WARNING;
31979 +
31980 +       /* since we define pr_fmt, call printk directly */
31981 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31982 +
31983 +       sbinfo = au_sbi(sb);
31984 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31985 +       pr("superblock\n");
31986 +       au_dpri_sb(sb);
31987 +
31988 +#if 0 /* reserved */
31989 +       do {
31990 +               int err, i, j, ndentry;
31991 +               struct au_dcsub_pages dpages;
31992 +               struct au_dpage *dpage;
31993 +
31994 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31995 +               if (unlikely(err))
31996 +                       break;
31997 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31998 +               if (!err)
31999 +                       for (i = 0; i < dpages.ndpage; i++) {
32000 +                               dpage = dpages.dpages + i;
32001 +                               ndentry = dpage->ndentry;
32002 +                               for (j = 0; j < ndentry; j++)
32003 +                                       au_dpri_dentry(dpage->dentries[j]);
32004 +                       }
32005 +               au_dpages_free(&dpages);
32006 +       } while (0);
32007 +#endif
32008 +
32009 +       pr("isolated inode\n");
32010 +       spin_lock(&sb->s_inode_list_lock);
32011 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
32012 +               spin_lock(&i->i_lock);
32013 +               if (hlist_empty(&i->i_dentry))
32014 +                       au_dpri_inode(i);
32015 +               spin_unlock(&i->i_lock);
32016 +       }
32017 +       spin_unlock(&sb->s_inode_list_lock);
32018 +
32019 +       pr("files\n");
32020 +       files = &au_sbi(sb)->si_files;
32021 +       hlist_bl_lock(files);
32022 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
32023 +               umode_t mode;
32024 +
32025 +               file = finfo->fi_file;
32026 +               mode = file_inode(file)->i_mode;
32027 +               if (!special_file(mode))
32028 +                       au_dpri_file(file);
32029 +       }
32030 +       hlist_bl_unlock(files);
32031 +       pr("done\n");
32032 +
32033 +#undef pr
32034 +       au_plevel = plevel;
32035 +}
32036 +
32037 +/* ---------------------------------------------------------------------- */
32038 +
32039 +/* module parameter */
32040 +static char *aufs_sysrq_key = "a";
32041 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32042 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32043 +
32044 +static void au_sysrq(u8 key __maybe_unused)
32045 +{
32046 +       struct au_sbinfo *sbinfo;
32047 +       struct hlist_bl_node *pos;
32048 +
32049 +       lockdep_off();
32050 +       au_sbilist_lock();
32051 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32052 +               sysrq_sb(sbinfo->si_sb);
32053 +       au_sbilist_unlock();
32054 +       lockdep_on();
32055 +}
32056 +
32057 +static struct sysrq_key_op au_sysrq_op = {
32058 +       .handler        = au_sysrq,
32059 +       .help_msg       = "Aufs",
32060 +       .action_msg     = "Aufs",
32061 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32062 +};
32063 +
32064 +/* ---------------------------------------------------------------------- */
32065 +
32066 +int __init au_sysrq_init(void)
32067 +{
32068 +       int err;
32069 +       char key;
32070 +
32071 +       err = -1;
32072 +       key = *aufs_sysrq_key;
32073 +       if ('a' <= key && key <= 'z')
32074 +               err = register_sysrq_key(key, &au_sysrq_op);
32075 +       if (unlikely(err))
32076 +               pr_err("err %d, sysrq=%c\n", err, key);
32077 +       return err;
32078 +}
32079 +
32080 +void au_sysrq_fin(void)
32081 +{
32082 +       int err;
32083 +
32084 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32085 +       if (unlikely(err))
32086 +               pr_err("err %d (ignored)\n", err);
32087 +}
32088 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32089 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32090 +++ linux/fs/aufs/vdir.c        2024-03-10 23:40:47.086476782 +0100
32091 @@ -0,0 +1,896 @@
32092 +// SPDX-License-Identifier: GPL-2.0
32093 +/*
32094 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32095 + *
32096 + * This program is free software; you can redistribute it and/or modify
32097 + * it under the terms of the GNU General Public License as published by
32098 + * the Free Software Foundation; either version 2 of the License, or
32099 + * (at your option) any later version.
32100 + *
32101 + * This program is distributed in the hope that it will be useful,
32102 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32103 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32104 + * GNU General Public License for more details.
32105 + *
32106 + * You should have received a copy of the GNU General Public License
32107 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32108 + */
32109 +
32110 +/*
32111 + * virtual or vertical directory
32112 + */
32113 +
32114 +#include <linux/iversion.h>
32115 +#include "aufs.h"
32116 +
32117 +static unsigned int calc_size(int nlen)
32118 +{
32119 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32120 +}
32121 +
32122 +static int set_deblk_end(union au_vdir_deblk_p *p,
32123 +                        union au_vdir_deblk_p *deblk_end)
32124 +{
32125 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32126 +               p->de->de_str.len = 0;
32127 +               /* smp_mb(); */
32128 +               return 0;
32129 +       }
32130 +       return -1; /* error */
32131 +}
32132 +
32133 +/* returns true or false */
32134 +static int is_deblk_end(union au_vdir_deblk_p *p,
32135 +                       union au_vdir_deblk_p *deblk_end)
32136 +{
32137 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32138 +               return !p->de->de_str.len;
32139 +       return 1;
32140 +}
32141 +
32142 +static unsigned char *last_deblk(struct au_vdir *vdir)
32143 +{
32144 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32145 +}
32146 +
32147 +/* ---------------------------------------------------------------------- */
32148 +
32149 +/* estimate the appropriate size for name hash table */
32150 +unsigned int au_rdhash_est(loff_t sz)
32151 +{
32152 +       unsigned int n;
32153 +
32154 +       n = UINT_MAX;
32155 +       sz >>= 10;
32156 +       if (sz < n)
32157 +               n = sz;
32158 +       if (sz < AUFS_RDHASH_DEF)
32159 +               n = AUFS_RDHASH_DEF;
32160 +       /* pr_info("n %u\n", n); */
32161 +       return n;
32162 +}
32163 +
32164 +/*
32165 + * the allocated memory has to be freed by
32166 + * au_nhash_wh_free() or au_nhash_de_free().
32167 + */
32168 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32169 +{
32170 +       struct hlist_head *head;
32171 +       unsigned int u;
32172 +       size_t sz;
32173 +
32174 +       sz = sizeof(*nhash->nh_head) * num_hash;
32175 +       head = kmalloc(sz, gfp);
32176 +       if (head) {
32177 +               nhash->nh_num = num_hash;
32178 +               nhash->nh_head = head;
32179 +               for (u = 0; u < num_hash; u++)
32180 +                       INIT_HLIST_HEAD(head++);
32181 +               return 0; /* success */
32182 +       }
32183 +
32184 +       return -ENOMEM;
32185 +}
32186 +
32187 +static void nhash_count(struct hlist_head *head)
32188 +{
32189 +#if 0 /* debugging */
32190 +       unsigned long n;
32191 +       struct hlist_node *pos;
32192 +
32193 +       n = 0;
32194 +       hlist_for_each(pos, head)
32195 +               n++;
32196 +       pr_info("%lu\n", n);
32197 +#endif
32198 +}
32199 +
32200 +static void au_nhash_wh_do_free(struct hlist_head *head)
32201 +{
32202 +       struct au_vdir_wh *pos;
32203 +       struct hlist_node *node;
32204 +
32205 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32206 +               au_kfree_rcu(pos);
32207 +}
32208 +
32209 +static void au_nhash_de_do_free(struct hlist_head *head)
32210 +{
32211 +       struct au_vdir_dehstr *pos;
32212 +       struct hlist_node *node;
32213 +
32214 +       hlist_for_each_entry_safe(pos, node, head, hash)
32215 +               au_cache_free_vdir_dehstr(pos);
32216 +}
32217 +
32218 +static void au_nhash_do_free(struct au_nhash *nhash,
32219 +                            void (*free)(struct hlist_head *head))
32220 +{
32221 +       unsigned int n;
32222 +       struct hlist_head *head;
32223 +
32224 +       n = nhash->nh_num;
32225 +       if (!n)
32226 +               return;
32227 +
32228 +       head = nhash->nh_head;
32229 +       while (n-- > 0) {
32230 +               nhash_count(head);
32231 +               free(head++);
32232 +       }
32233 +       au_kfree_try_rcu(nhash->nh_head);
32234 +}
32235 +
32236 +void au_nhash_wh_free(struct au_nhash *whlist)
32237 +{
32238 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32239 +}
32240 +
32241 +static void au_nhash_de_free(struct au_nhash *delist)
32242 +{
32243 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32244 +}
32245 +
32246 +/* ---------------------------------------------------------------------- */
32247 +
32248 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32249 +                           int limit)
32250 +{
32251 +       int num;
32252 +       unsigned int u, n;
32253 +       struct hlist_head *head;
32254 +       struct au_vdir_wh *pos;
32255 +
32256 +       num = 0;
32257 +       n = whlist->nh_num;
32258 +       head = whlist->nh_head;
32259 +       for (u = 0; u < n; u++, head++)
32260 +               hlist_for_each_entry(pos, head, wh_hash)
32261 +                       if (pos->wh_bindex == btgt && ++num > limit)
32262 +                               return 1;
32263 +       return 0;
32264 +}
32265 +
32266 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32267 +                                      unsigned char *name,
32268 +                                      unsigned int len)
32269 +{
32270 +       unsigned int v;
32271 +       /* const unsigned int magic_bit = 12; */
32272 +
32273 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32274 +
32275 +       v = 0;
32276 +       if (len > 8)
32277 +               len = 8;
32278 +       while (len--)
32279 +               v += *name++;
32280 +       /* v = hash_long(v, magic_bit); */
32281 +       v %= nhash->nh_num;
32282 +       return nhash->nh_head + v;
32283 +}
32284 +
32285 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32286 +                             int nlen)
32287 +{
32288 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32289 +}
32290 +
32291 +/* returns found or not */
32292 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32293 +{
32294 +       struct hlist_head *head;
32295 +       struct au_vdir_wh *pos;
32296 +       struct au_vdir_destr *str;
32297 +
32298 +       head = au_name_hash(whlist, name, nlen);
32299 +       hlist_for_each_entry(pos, head, wh_hash) {
32300 +               str = &pos->wh_str;
32301 +               AuDbg("%.*s\n", str->len, str->name);
32302 +               if (au_nhash_test_name(str, name, nlen))
32303 +                       return 1;
32304 +       }
32305 +       return 0;
32306 +}
32307 +
32308 +/* returns found(true) or not */
32309 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32310 +{
32311 +       struct hlist_head *head;
32312 +       struct au_vdir_dehstr *pos;
32313 +       struct au_vdir_destr *str;
32314 +
32315 +       head = au_name_hash(delist, name, nlen);
32316 +       hlist_for_each_entry(pos, head, hash) {
32317 +               str = pos->str;
32318 +               AuDbg("%.*s\n", str->len, str->name);
32319 +               if (au_nhash_test_name(str, name, nlen))
32320 +                       return 1;
32321 +       }
32322 +       return 0;
32323 +}
32324 +
32325 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32326 +                           unsigned char d_type)
32327 +{
32328 +#ifdef CONFIG_AUFS_SHWH
32329 +       wh->wh_ino = ino;
32330 +       wh->wh_type = d_type;
32331 +#endif
32332 +}
32333 +
32334 +/* ---------------------------------------------------------------------- */
32335 +
32336 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32337 +                      unsigned int d_type, aufs_bindex_t bindex,
32338 +                      unsigned char shwh)
32339 +{
32340 +       int err;
32341 +       struct au_vdir_destr *str;
32342 +       struct au_vdir_wh *wh;
32343 +
32344 +       AuDbg("%.*s\n", nlen, name);
32345 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32346 +
32347 +       err = -ENOMEM;
32348 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32349 +       if (unlikely(!wh))
32350 +               goto out;
32351 +
32352 +       err = 0;
32353 +       wh->wh_bindex = bindex;
32354 +       if (shwh)
32355 +               au_shwh_init_wh(wh, ino, d_type);
32356 +       str = &wh->wh_str;
32357 +       str->len = nlen;
32358 +       memcpy(str->name, name, nlen);
32359 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32360 +       /* smp_mb(); */
32361 +
32362 +out:
32363 +       return err;
32364 +}
32365 +
32366 +static int append_deblk(struct au_vdir *vdir)
32367 +{
32368 +       int err;
32369 +       unsigned long ul;
32370 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32371 +       union au_vdir_deblk_p p, deblk_end;
32372 +       unsigned char **o;
32373 +
32374 +       err = -ENOMEM;
32375 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32376 +                       GFP_NOFS, /*may_shrink*/0);
32377 +       if (unlikely(!o))
32378 +               goto out;
32379 +
32380 +       vdir->vd_deblk = o;
32381 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32382 +       if (p.deblk) {
32383 +               ul = vdir->vd_nblk++;
32384 +               vdir->vd_deblk[ul] = p.deblk;
32385 +               vdir->vd_last.ul = ul;
32386 +               vdir->vd_last.p.deblk = p.deblk;
32387 +               deblk_end.deblk = p.deblk + deblk_sz;
32388 +               err = set_deblk_end(&p, &deblk_end);
32389 +       }
32390 +
32391 +out:
32392 +       return err;
32393 +}
32394 +
32395 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32396 +                    unsigned int d_type, struct au_nhash *delist)
32397 +{
32398 +       int err;
32399 +       unsigned int sz;
32400 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32401 +       union au_vdir_deblk_p p, *room, deblk_end;
32402 +       struct au_vdir_dehstr *dehstr;
32403 +
32404 +       p.deblk = last_deblk(vdir);
32405 +       deblk_end.deblk = p.deblk + deblk_sz;
32406 +       room = &vdir->vd_last.p;
32407 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32408 +                 || !is_deblk_end(room, &deblk_end));
32409 +
32410 +       sz = calc_size(nlen);
32411 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32412 +               err = append_deblk(vdir);
32413 +               if (unlikely(err))
32414 +                       goto out;
32415 +
32416 +               p.deblk = last_deblk(vdir);
32417 +               deblk_end.deblk = p.deblk + deblk_sz;
32418 +               /* smp_mb(); */
32419 +               AuDebugOn(room->deblk != p.deblk);
32420 +       }
32421 +
32422 +       err = -ENOMEM;
32423 +       dehstr = au_cache_alloc_vdir_dehstr();
32424 +       if (unlikely(!dehstr))
32425 +               goto out;
32426 +
32427 +       dehstr->str = &room->de->de_str;
32428 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32429 +       room->de->de_ino = ino;
32430 +       room->de->de_type = d_type;
32431 +       room->de->de_str.len = nlen;
32432 +       memcpy(room->de->de_str.name, name, nlen);
32433 +
32434 +       err = 0;
32435 +       room->deblk += sz;
32436 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32437 +               err = append_deblk(vdir);
32438 +       /* smp_mb(); */
32439 +
32440 +out:
32441 +       return err;
32442 +}
32443 +
32444 +/* ---------------------------------------------------------------------- */
32445 +
32446 +void au_vdir_free(struct au_vdir *vdir)
32447 +{
32448 +       unsigned char **deblk;
32449 +
32450 +       deblk = vdir->vd_deblk;
32451 +       while (vdir->vd_nblk--)
32452 +               au_kfree_try_rcu(*deblk++);
32453 +       au_kfree_try_rcu(vdir->vd_deblk);
32454 +       au_cache_free_vdir(vdir);
32455 +}
32456 +
32457 +static struct au_vdir *alloc_vdir(struct file *file)
32458 +{
32459 +       struct au_vdir *vdir;
32460 +       struct super_block *sb;
32461 +       int err;
32462 +
32463 +       sb = file->f_path.dentry->d_sb;
32464 +       SiMustAnyLock(sb);
32465 +
32466 +       err = -ENOMEM;
32467 +       vdir = au_cache_alloc_vdir();
32468 +       if (unlikely(!vdir))
32469 +               goto out;
32470 +
32471 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32472 +       if (unlikely(!vdir->vd_deblk))
32473 +               goto out_free;
32474 +
32475 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32476 +       if (!vdir->vd_deblk_sz) {
32477 +               /* estimate the appropriate size for deblk */
32478 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32479 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32480 +       }
32481 +       vdir->vd_nblk = 0;
32482 +       vdir->vd_version = 0;
32483 +       vdir->vd_jiffy = 0;
32484 +       err = append_deblk(vdir);
32485 +       if (!err)
32486 +               return vdir; /* success */
32487 +
32488 +       au_kfree_try_rcu(vdir->vd_deblk);
32489 +
32490 +out_free:
32491 +       au_cache_free_vdir(vdir);
32492 +out:
32493 +       vdir = ERR_PTR(err);
32494 +       return vdir;
32495 +}
32496 +
32497 +static int reinit_vdir(struct au_vdir *vdir)
32498 +{
32499 +       int err;
32500 +       union au_vdir_deblk_p p, deblk_end;
32501 +
32502 +       while (vdir->vd_nblk > 1) {
32503 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32504 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32505 +               vdir->vd_nblk--;
32506 +       }
32507 +       p.deblk = vdir->vd_deblk[0];
32508 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32509 +       err = set_deblk_end(&p, &deblk_end);
32510 +       /* keep vd_dblk_sz */
32511 +       vdir->vd_last.ul = 0;
32512 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32513 +       vdir->vd_version = 0;
32514 +       vdir->vd_jiffy = 0;
32515 +       /* smp_mb(); */
32516 +       return err;
32517 +}
32518 +
32519 +/* ---------------------------------------------------------------------- */
32520 +
32521 +#define AuFillVdir_CALLED      BIT(0)
32522 +#define AuFillVdir_WHABLE      BIT(1)
32523 +#define AuFillVdir_SHWH                BIT(2)
32524 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32525 +#define au_fset_fillvdir(flags, name) \
32526 +       do { (flags) |= AuFillVdir_##name; } while (0)
32527 +#define au_fclr_fillvdir(flags, name) \
32528 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32529 +
32530 +#ifndef CONFIG_AUFS_SHWH
32531 +#undef AuFillVdir_SHWH
32532 +#define AuFillVdir_SHWH                0
32533 +#endif
32534 +
32535 +struct fillvdir_arg {
32536 +       struct dir_context      ctx;
32537 +       struct file             *file;
32538 +       struct au_vdir          *vdir;
32539 +       struct au_nhash         delist;
32540 +       struct au_nhash         whlist;
32541 +       aufs_bindex_t           bindex;
32542 +       unsigned int            flags;
32543 +       int                     err;
32544 +};
32545 +
32546 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32547 +                   loff_t offset __maybe_unused, u64 h_ino,
32548 +                   unsigned int d_type)
32549 +{
32550 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32551 +       char *name = (void *)__name;
32552 +       struct super_block *sb;
32553 +       ino_t ino;
32554 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32555 +
32556 +       arg->err = 0;
32557 +       sb = arg->file->f_path.dentry->d_sb;
32558 +       au_fset_fillvdir(arg->flags, CALLED);
32559 +       /* smp_mb(); */
32560 +       if (nlen <= AUFS_WH_PFX_LEN
32561 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32562 +               if (test_known(&arg->delist, name, nlen)
32563 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32564 +                       goto out; /* already exists or whiteouted */
32565 +
32566 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32567 +               if (!arg->err) {
32568 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32569 +                               d_type = DT_UNKNOWN;
32570 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32571 +                                            d_type, &arg->delist);
32572 +               }
32573 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32574 +               name += AUFS_WH_PFX_LEN;
32575 +               nlen -= AUFS_WH_PFX_LEN;
32576 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32577 +                       goto out; /* already whiteouted */
32578 +
32579 +               ino = 0; /* just to suppress a warning */
32580 +               if (shwh)
32581 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32582 +                                            &ino);
32583 +               if (!arg->err) {
32584 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32585 +                               d_type = DT_UNKNOWN;
32586 +                       arg->err = au_nhash_append_wh
32587 +                               (&arg->whlist, name, nlen, ino, d_type,
32588 +                                arg->bindex, shwh);
32589 +               }
32590 +       }
32591 +
32592 +out:
32593 +       if (!arg->err)
32594 +               arg->vdir->vd_jiffy = jiffies;
32595 +       /* smp_mb(); */
32596 +       AuTraceErr(arg->err);
32597 +       return !arg->err;
32598 +}
32599 +
32600 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32601 +                         struct au_nhash *whlist, struct au_nhash *delist)
32602 +{
32603 +#ifdef CONFIG_AUFS_SHWH
32604 +       int err;
32605 +       unsigned int nh, u;
32606 +       struct hlist_head *head;
32607 +       struct au_vdir_wh *pos;
32608 +       struct hlist_node *n;
32609 +       char *p, *o;
32610 +       struct au_vdir_destr *destr;
32611 +
32612 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32613 +
32614 +       err = -ENOMEM;
32615 +       o = p = (void *)__get_free_page(GFP_NOFS);
32616 +       if (unlikely(!p))
32617 +               goto out;
32618 +
32619 +       err = 0;
32620 +       nh = whlist->nh_num;
32621 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32622 +       p += AUFS_WH_PFX_LEN;
32623 +       for (u = 0; u < nh; u++) {
32624 +               head = whlist->nh_head + u;
32625 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32626 +                       destr = &pos->wh_str;
32627 +                       memcpy(p, destr->name, destr->len);
32628 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32629 +                                       pos->wh_ino, pos->wh_type, delist);
32630 +                       if (unlikely(err))
32631 +                               break;
32632 +               }
32633 +       }
32634 +
32635 +       free_page((unsigned long)o);
32636 +
32637 +out:
32638 +       AuTraceErr(err);
32639 +       return err;
32640 +#else
32641 +       return 0;
32642 +#endif
32643 +}
32644 +
32645 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32646 +{
32647 +       int err;
32648 +       unsigned int rdhash;
32649 +       loff_t offset;
32650 +       aufs_bindex_t bbot, bindex, btop;
32651 +       unsigned char shwh;
32652 +       struct file *hf, *file;
32653 +       struct super_block *sb;
32654 +
32655 +       file = arg->file;
32656 +       sb = file->f_path.dentry->d_sb;
32657 +       SiMustAnyLock(sb);
32658 +
32659 +       rdhash = au_sbi(sb)->si_rdhash;
32660 +       if (!rdhash)
32661 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32662 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32663 +       if (unlikely(err))
32664 +               goto out;
32665 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32666 +       if (unlikely(err))
32667 +               goto out_delist;
32668 +
32669 +       err = 0;
32670 +       arg->flags = 0;
32671 +       shwh = 0;
32672 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32673 +               shwh = 1;
32674 +               au_fset_fillvdir(arg->flags, SHWH);
32675 +       }
32676 +       btop = au_fbtop(file);
32677 +       bbot = au_fbbot_dir(file);
32678 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32679 +               hf = au_hf_dir(file, bindex);
32680 +               if (!hf)
32681 +                       continue;
32682 +
32683 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32684 +               err = offset;
32685 +               if (unlikely(offset))
32686 +                       break;
32687 +
32688 +               arg->bindex = bindex;
32689 +               au_fclr_fillvdir(arg->flags, WHABLE);
32690 +               if (shwh
32691 +                   || (bindex != bbot
32692 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32693 +                       au_fset_fillvdir(arg->flags, WHABLE);
32694 +               do {
32695 +                       arg->err = 0;
32696 +                       au_fclr_fillvdir(arg->flags, CALLED);
32697 +                       /* smp_mb(); */
32698 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32699 +                       if (err >= 0)
32700 +                               err = arg->err;
32701 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32702 +
32703 +               /*
32704 +                * dir_relax() may be good for concurrency, but aufs should not
32705 +                * use it since it will cause a lockdep problem.
32706 +                */
32707 +       }
32708 +
32709 +       if (!err && shwh)
32710 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32711 +
32712 +       au_nhash_wh_free(&arg->whlist);
32713 +
32714 +out_delist:
32715 +       au_nhash_de_free(&arg->delist);
32716 +out:
32717 +       return err;
32718 +}
32719 +
32720 +static int read_vdir(struct file *file, int may_read)
32721 +{
32722 +       int err;
32723 +       unsigned long expire;
32724 +       unsigned char do_read;
32725 +       struct fillvdir_arg arg = {
32726 +               .ctx = {
32727 +                       .actor = fillvdir
32728 +               }
32729 +       };
32730 +       struct inode *inode;
32731 +       struct au_vdir *vdir, *allocated;
32732 +
32733 +       err = 0;
32734 +       inode = file_inode(file);
32735 +       IMustLock(inode);
32736 +       IiMustWriteLock(inode);
32737 +       SiMustAnyLock(inode->i_sb);
32738 +
32739 +       allocated = NULL;
32740 +       do_read = 0;
32741 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32742 +       vdir = au_ivdir(inode);
32743 +       if (!vdir) {
32744 +               do_read = 1;
32745 +               vdir = alloc_vdir(file);
32746 +               err = PTR_ERR(vdir);
32747 +               if (IS_ERR(vdir))
32748 +                       goto out;
32749 +               err = 0;
32750 +               allocated = vdir;
32751 +       } else if (may_read
32752 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32753 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32754 +               do_read = 1;
32755 +               err = reinit_vdir(vdir);
32756 +               if (unlikely(err))
32757 +                       goto out;
32758 +       }
32759 +
32760 +       if (!do_read)
32761 +               return 0; /* success */
32762 +
32763 +       arg.file = file;
32764 +       arg.vdir = vdir;
32765 +       err = au_do_read_vdir(&arg);
32766 +       if (!err) {
32767 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32768 +               vdir->vd_version = inode_query_iversion(inode);
32769 +               vdir->vd_last.ul = 0;
32770 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32771 +               if (allocated)
32772 +                       au_set_ivdir(inode, allocated);
32773 +       } else if (allocated)
32774 +               au_vdir_free(allocated);
32775 +
32776 +out:
32777 +       return err;
32778 +}
32779 +
32780 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32781 +{
32782 +       int err, rerr;
32783 +       unsigned long ul, n;
32784 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32785 +
32786 +       AuDebugOn(tgt->vd_nblk != 1);
32787 +
32788 +       err = -ENOMEM;
32789 +       if (tgt->vd_nblk < src->vd_nblk) {
32790 +               unsigned char **p;
32791 +
32792 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32793 +                               GFP_NOFS, /*may_shrink*/0);
32794 +               if (unlikely(!p))
32795 +                       goto out;
32796 +               tgt->vd_deblk = p;
32797 +       }
32798 +
32799 +       if (tgt->vd_deblk_sz != deblk_sz) {
32800 +               unsigned char *p;
32801 +
32802 +               tgt->vd_deblk_sz = deblk_sz;
32803 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32804 +                               /*may_shrink*/1);
32805 +               if (unlikely(!p))
32806 +                       goto out;
32807 +               tgt->vd_deblk[0] = p;
32808 +       }
32809 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32810 +       tgt->vd_version = src->vd_version;
32811 +       tgt->vd_jiffy = src->vd_jiffy;
32812 +
32813 +       n = src->vd_nblk;
32814 +       for (ul = 1; ul < n; ul++) {
32815 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32816 +                                           GFP_NOFS);
32817 +               if (unlikely(!tgt->vd_deblk[ul]))
32818 +                       goto out;
32819 +               tgt->vd_nblk++;
32820 +       }
32821 +       tgt->vd_nblk = n;
32822 +       tgt->vd_last.ul = tgt->vd_last.ul;
32823 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32824 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32825 +               - src->vd_deblk[src->vd_last.ul];
32826 +       /* smp_mb(); */
32827 +       return 0; /* success */
32828 +
32829 +out:
32830 +       rerr = reinit_vdir(tgt);
32831 +       BUG_ON(rerr);
32832 +       return err;
32833 +}
32834 +
32835 +int au_vdir_init(struct file *file)
32836 +{
32837 +       int err;
32838 +       struct inode *inode;
32839 +       struct au_vdir *vdir_cache, *allocated;
32840 +
32841 +       /* test file->f_pos here instead of ctx->pos */
32842 +       err = read_vdir(file, !file->f_pos);
32843 +       if (unlikely(err))
32844 +               goto out;
32845 +
32846 +       allocated = NULL;
32847 +       vdir_cache = au_fvdir_cache(file);
32848 +       if (!vdir_cache) {
32849 +               vdir_cache = alloc_vdir(file);
32850 +               err = PTR_ERR(vdir_cache);
32851 +               if (IS_ERR(vdir_cache))
32852 +                       goto out;
32853 +               allocated = vdir_cache;
32854 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32855 +               /* test file->f_pos here instead of ctx->pos */
32856 +               err = reinit_vdir(vdir_cache);
32857 +               if (unlikely(err))
32858 +                       goto out;
32859 +       } else
32860 +               return 0; /* success */
32861 +
32862 +       inode = file_inode(file);
32863 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32864 +       if (!err) {
32865 +               file->f_version = inode_query_iversion(inode);
32866 +               if (allocated)
32867 +                       au_set_fvdir_cache(file, allocated);
32868 +       } else if (allocated)
32869 +               au_vdir_free(allocated);
32870 +
32871 +out:
32872 +       return err;
32873 +}
32874 +
32875 +static loff_t calc_offset(struct au_vdir *vdir)
32876 +{
32877 +       loff_t offset;
32878 +       union au_vdir_deblk_p p;
32879 +
32880 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32881 +       offset = vdir->vd_last.p.deblk - p.deblk;
32882 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32883 +       return offset;
32884 +}
32885 +
32886 +/* returns true or false */
32887 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32888 +{
32889 +       int valid;
32890 +       unsigned int deblk_sz;
32891 +       unsigned long ul, n;
32892 +       loff_t offset;
32893 +       union au_vdir_deblk_p p, deblk_end;
32894 +       struct au_vdir *vdir_cache;
32895 +
32896 +       valid = 1;
32897 +       vdir_cache = au_fvdir_cache(file);
32898 +       offset = calc_offset(vdir_cache);
32899 +       AuDbg("offset %lld\n", offset);
32900 +       if (ctx->pos == offset)
32901 +               goto out;
32902 +
32903 +       vdir_cache->vd_last.ul = 0;
32904 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32905 +       if (!ctx->pos)
32906 +               goto out;
32907 +
32908 +       valid = 0;
32909 +       deblk_sz = vdir_cache->vd_deblk_sz;
32910 +       ul = div64_u64(ctx->pos, deblk_sz);
32911 +       AuDbg("ul %lu\n", ul);
32912 +       if (ul >= vdir_cache->vd_nblk)
32913 +               goto out;
32914 +
32915 +       n = vdir_cache->vd_nblk;
32916 +       for (; ul < n; ul++) {
32917 +               p.deblk = vdir_cache->vd_deblk[ul];
32918 +               deblk_end.deblk = p.deblk + deblk_sz;
32919 +               offset = ul;
32920 +               offset *= deblk_sz;
32921 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32922 +                       unsigned int l;
32923 +
32924 +                       l = calc_size(p.de->de_str.len);
32925 +                       offset += l;
32926 +                       p.deblk += l;
32927 +               }
32928 +               if (!is_deblk_end(&p, &deblk_end)) {
32929 +                       valid = 1;
32930 +                       vdir_cache->vd_last.ul = ul;
32931 +                       vdir_cache->vd_last.p = p;
32932 +                       break;
32933 +               }
32934 +       }
32935 +
32936 +out:
32937 +       /* smp_mb(); */
32938 +       if (!valid)
32939 +               AuDbg("valid %d\n", !valid);
32940 +       return valid;
32941 +}
32942 +
32943 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32944 +{
32945 +       unsigned int l, deblk_sz;
32946 +       union au_vdir_deblk_p deblk_end;
32947 +       struct au_vdir *vdir_cache;
32948 +       struct au_vdir_de *de;
32949 +
32950 +       if (!seek_vdir(file, ctx))
32951 +               return 0;
32952 +
32953 +       vdir_cache = au_fvdir_cache(file);
32954 +       deblk_sz = vdir_cache->vd_deblk_sz;
32955 +       while (1) {
32956 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32957 +               deblk_end.deblk += deblk_sz;
32958 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32959 +                       de = vdir_cache->vd_last.p.de;
32960 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32961 +                             de->de_str.len, de->de_str.name, ctx->pos,
32962 +                             (unsigned long)de->de_ino, de->de_type);
32963 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32964 +                                              de->de_str.len, de->de_ino,
32965 +                                              de->de_type))) {
32966 +                               /* todo: ignore the error caused by udba? */
32967 +                               /* return err; */
32968 +                               return 0;
32969 +                       }
32970 +
32971 +                       l = calc_size(de->de_str.len);
32972 +                       vdir_cache->vd_last.p.deblk += l;
32973 +                       ctx->pos += l;
32974 +               }
32975 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32976 +                       vdir_cache->vd_last.ul++;
32977 +                       vdir_cache->vd_last.p.deblk
32978 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32979 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32980 +                       continue;
32981 +               }
32982 +               break;
32983 +       }
32984 +
32985 +       /* smp_mb(); */
32986 +       return 0;
32987 +}
32988 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32989 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32990 +++ linux/fs/aufs/vfsub.c       2024-03-10 23:40:47.086476782 +0100
32991 @@ -0,0 +1,921 @@
32992 +// SPDX-License-Identifier: GPL-2.0
32993 +/*
32994 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32995 + *
32996 + * This program is free software; you can redistribute it and/or modify
32997 + * it under the terms of the GNU General Public License as published by
32998 + * the Free Software Foundation; either version 2 of the License, or
32999 + * (at your option) any later version.
33000 + *
33001 + * This program is distributed in the hope that it will be useful,
33002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33004 + * GNU General Public License for more details.
33005 + *
33006 + * You should have received a copy of the GNU General Public License
33007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33008 + */
33009 +
33010 +/*
33011 + * sub-routines for VFS
33012 + */
33013 +
33014 +#include <linux/mnt_namespace.h>
33015 +#include <linux/nsproxy.h>
33016 +#include <linux/security.h>
33017 +#include <linux/splice.h>
33018 +#include "aufs.h"
33019 +
33020 +#ifdef CONFIG_AUFS_BR_FUSE
33021 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
33022 +{
33023 +       if (!au_test_fuse(h_sb) || !au_userns)
33024 +               return 0;
33025 +
33026 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
33027 +}
33028 +#endif
33029 +
33030 +int vfsub_sync_filesystem(struct super_block *h_sb)
33031 +{
33032 +       int err;
33033 +
33034 +       lockdep_off();
33035 +       down_read(&h_sb->s_umount);
33036 +       err = sync_filesystem(h_sb);
33037 +       up_read(&h_sb->s_umount);
33038 +       lockdep_on();
33039 +
33040 +       return err;
33041 +}
33042 +
33043 +/* ---------------------------------------------------------------------- */
33044 +
33045 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33046 +{
33047 +       int err;
33048 +       struct kstat st;
33049 +       struct super_block *h_sb;
33050 +
33051 +       /*
33052 +        * Always needs h_path->mnt for LSM or FUSE branch.
33053 +        */
33054 +       AuDebugOn(!h_path->mnt);
33055 +
33056 +       /* for remote fs, leave work for its getattr or d_revalidate */
33057 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33058 +       /* still some fs may acquire i_mutex. we need to skip them */
33059 +       err = 0;
33060 +       if (!did)
33061 +               did = &err;
33062 +       h_sb = h_path->dentry->d_sb;
33063 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33064 +       if (*did)
33065 +               err = vfsub_getattr(h_path, &st);
33066 +
33067 +       return err;
33068 +}
33069 +
33070 +/* ---------------------------------------------------------------------- */
33071 +
33072 +struct file *vfsub_dentry_open(struct path *path, int flags)
33073 +{
33074 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33075 +                          current_cred());
33076 +}
33077 +
33078 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33079 +{
33080 +       struct file *file;
33081 +
33082 +       lockdep_off();
33083 +       file = filp_open(path,
33084 +                        oflags /* | __FMODE_NONOTIFY */,
33085 +                        mode);
33086 +       lockdep_on();
33087 +       if (IS_ERR(file))
33088 +               goto out;
33089 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33090 +
33091 +out:
33092 +       return file;
33093 +}
33094 +
33095 +/*
33096 + * Ideally this function should call VFS:do_last() in order to keep all its
33097 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33098 + * structure such as nameidata. This is a second (or third) best approach.
33099 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33100 + */
33101 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33102 +                     struct vfsub_aopen_args *args)
33103 +{
33104 +       int err;
33105 +       struct au_branch *br = args->br;
33106 +       struct file *file = args->file;
33107 +       /* copied from linux/fs/namei.c:atomic_open() */
33108 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33109 +
33110 +       IMustLock(dir);
33111 +       AuDebugOn(!dir->i_op->atomic_open);
33112 +
33113 +       err = au_br_test_oflag(args->open_flag, br);
33114 +       if (unlikely(err))
33115 +               goto out;
33116 +
33117 +       au_lcnt_inc(&br->br_nfiles);
33118 +       file->f_path.dentry = DENTRY_NOT_SET;
33119 +       file->f_path.mnt = au_br_mnt(br);
33120 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33121 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33122 +                                    args->create_mode);
33123 +       if (unlikely(err < 0)) {
33124 +               au_lcnt_dec(&br->br_nfiles);
33125 +               goto out;
33126 +       }
33127 +
33128 +       /* temporary workaround for nfsv4 branch */
33129 +       if (au_test_nfs(dir->i_sb))
33130 +               nfs_mark_for_revalidate(dir);
33131 +
33132 +       if (file->f_mode & FMODE_CREATED)
33133 +               fsnotify_create(dir, dentry);
33134 +       if (!(file->f_mode & FMODE_OPENED)) {
33135 +               au_lcnt_dec(&br->br_nfiles);
33136 +               goto out;
33137 +       }
33138 +
33139 +       /* todo: call VFS:may_open() here */
33140 +       /* todo: ima_file_check() too? */
33141 +       if (!err && (args->open_flag & __FMODE_EXEC))
33142 +               err = deny_write_access(file);
33143 +       if (!err)
33144 +               fsnotify_open(file);
33145 +       else
33146 +               au_lcnt_dec(&br->br_nfiles);
33147 +       /* note that the file is created and still opened */
33148 +
33149 +out:
33150 +       return err;
33151 +}
33152 +
33153 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33154 +{
33155 +       int err;
33156 +
33157 +       err = kern_path(name, flags, path);
33158 +       if (!err && d_is_positive(path->dentry))
33159 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33160 +       return err;
33161 +}
33162 +
33163 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33164 +                                            struct path *ppath, int len)
33165 +{
33166 +       struct path path;
33167 +
33168 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33169 +       if (IS_ERR(path.dentry))
33170 +               goto out;
33171 +       if (d_is_positive(path.dentry)) {
33172 +               path.mnt = ppath->mnt;
33173 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33174 +       }
33175 +
33176 +out:
33177 +       AuTraceErrPtr(path.dentry);
33178 +       return path.dentry;
33179 +}
33180 +
33181 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33182 +                                   int len)
33183 +{
33184 +       struct path path;
33185 +
33186 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33187 +       IMustLock(d_inode(ppath->dentry));
33188 +
33189 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33190 +       if (IS_ERR(path.dentry))
33191 +               goto out;
33192 +       if (d_is_positive(path.dentry)) {
33193 +               path.mnt = ppath->mnt;
33194 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33195 +       }
33196 +
33197 +out:
33198 +       AuTraceErrPtr(path.dentry);
33199 +       return path.dentry;
33200 +}
33201 +
33202 +void vfsub_call_lkup_one(void *args)
33203 +{
33204 +       struct vfsub_lkup_one_args *a = args;
33205 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33206 +}
33207 +
33208 +/* ---------------------------------------------------------------------- */
33209 +
33210 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33211 +                                struct dentry *d2, struct au_hinode *hdir2)
33212 +{
33213 +       struct dentry *d;
33214 +
33215 +       lockdep_off();
33216 +       d = lock_rename(d1, d2);
33217 +       lockdep_on();
33218 +       if (IS_ERR(d))
33219 +               goto out;
33220 +       au_hn_suspend(hdir1);
33221 +       if (hdir1 != hdir2)
33222 +               au_hn_suspend(hdir2);
33223 +
33224 +out:
33225 +       return d;
33226 +}
33227 +
33228 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33229 +                        struct dentry *d2, struct au_hinode *hdir2)
33230 +{
33231 +       au_hn_resume(hdir1);
33232 +       if (hdir1 != hdir2)
33233 +               au_hn_resume(hdir2);
33234 +       lockdep_off();
33235 +       unlock_rename(d1, d2);
33236 +       lockdep_on();
33237 +}
33238 +
33239 +/* ---------------------------------------------------------------------- */
33240 +
33241 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33242 +{
33243 +       int err;
33244 +       struct dentry *d;
33245 +       struct mnt_idmap *idmap;
33246 +
33247 +       IMustLock(dir);
33248 +
33249 +       d = path->dentry;
33250 +       path->dentry = d->d_parent;
33251 +       err = security_path_mknod(path, d, mode, 0);
33252 +       path->dentry = d;
33253 +       if (unlikely(err))
33254 +               goto out;
33255 +       idmap = mnt_idmap(path->mnt);
33256 +
33257 +       lockdep_off();
33258 +       err = vfs_create(idmap, dir, path->dentry, mode, want_excl);
33259 +       lockdep_on();
33260 +       if (!err) {
33261 +               struct path tmp = *path;
33262 +               int did;
33263 +
33264 +               vfsub_update_h_iattr(&tmp, &did);
33265 +               if (did) {
33266 +                       tmp.dentry = path->dentry->d_parent;
33267 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33268 +               }
33269 +               /*ignore*/
33270 +       }
33271 +
33272 +out:
33273 +       return err;
33274 +}
33275 +
33276 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33277 +{
33278 +       int err;
33279 +       struct dentry *d;
33280 +       struct mnt_idmap *idmap;
33281 +
33282 +       IMustLock(dir);
33283 +
33284 +       d = path->dentry;
33285 +       path->dentry = d->d_parent;
33286 +       err = security_path_symlink(path, d, symname);
33287 +       path->dentry = d;
33288 +       if (unlikely(err))
33289 +               goto out;
33290 +       idmap = mnt_idmap(path->mnt);
33291 +
33292 +       lockdep_off();
33293 +       err = vfs_symlink(idmap, dir, path->dentry, symname);
33294 +       lockdep_on();
33295 +       if (!err) {
33296 +               struct path tmp = *path;
33297 +               int did;
33298 +
33299 +               vfsub_update_h_iattr(&tmp, &did);
33300 +               if (did) {
33301 +                       tmp.dentry = path->dentry->d_parent;
33302 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33303 +               }
33304 +               /*ignore*/
33305 +       }
33306 +
33307 +out:
33308 +       return err;
33309 +}
33310 +
33311 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33312 +{
33313 +       int err;
33314 +       struct dentry *d;
33315 +       struct mnt_idmap *idmap;
33316 +
33317 +       IMustLock(dir);
33318 +
33319 +       d = path->dentry;
33320 +       path->dentry = d->d_parent;
33321 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33322 +       path->dentry = d;
33323 +       if (unlikely(err))
33324 +               goto out;
33325 +       idmap = mnt_idmap(path->mnt);
33326 +
33327 +       lockdep_off();
33328 +       err = vfs_mknod(idmap, dir, path->dentry, mode, dev);
33329 +       lockdep_on();
33330 +       if (!err) {
33331 +               struct path tmp = *path;
33332 +               int did;
33333 +
33334 +               vfsub_update_h_iattr(&tmp, &did);
33335 +               if (did) {
33336 +                       tmp.dentry = path->dentry->d_parent;
33337 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33338 +               }
33339 +               /*ignore*/
33340 +       }
33341 +
33342 +out:
33343 +       return err;
33344 +}
33345 +
33346 +static int au_test_nlink(struct inode *inode)
33347 +{
33348 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33349 +
33350 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33351 +           || inode->i_nlink < link_max)
33352 +               return 0;
33353 +       return -EMLINK;
33354 +}
33355 +
33356 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33357 +              struct inode **delegated_inode)
33358 +{
33359 +       int err;
33360 +       struct dentry *d;
33361 +       struct mnt_idmap *idmap;
33362 +
33363 +       IMustLock(dir);
33364 +
33365 +       err = au_test_nlink(d_inode(src_dentry));
33366 +       if (unlikely(err))
33367 +               return err;
33368 +
33369 +       /* we don't call may_linkat() */
33370 +       d = path->dentry;
33371 +       path->dentry = d->d_parent;
33372 +       err = security_path_link(src_dentry, path, d);
33373 +       path->dentry = d;
33374 +       if (unlikely(err))
33375 +               goto out;
33376 +       idmap = mnt_idmap(path->mnt);
33377 +
33378 +       lockdep_off();
33379 +       err = vfs_link(src_dentry, idmap, dir, path->dentry, delegated_inode);
33380 +       lockdep_on();
33381 +       if (!err) {
33382 +               struct path tmp = *path;
33383 +               int did;
33384 +
33385 +               /* fuse has different memory inode for the same inumber */
33386 +               vfsub_update_h_iattr(&tmp, &did);
33387 +               if (did) {
33388 +                       tmp.dentry = path->dentry->d_parent;
33389 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33390 +                       tmp.dentry = src_dentry;
33391 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33392 +               }
33393 +               /*ignore*/
33394 +       }
33395 +
33396 +out:
33397 +       return err;
33398 +}
33399 +
33400 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33401 +                struct inode *dir, struct path *path,
33402 +                struct inode **delegated_inode, unsigned int flags)
33403 +{
33404 +       int err;
33405 +       struct renamedata rd;
33406 +       struct path tmp = {
33407 +               .mnt    = path->mnt
33408 +       };
33409 +       struct dentry *d;
33410 +
33411 +       IMustLock(dir);
33412 +       IMustLock(src_dir);
33413 +
33414 +       d = path->dentry;
33415 +       path->dentry = d->d_parent;
33416 +       tmp.dentry = src_dentry->d_parent;
33417 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33418 +       path->dentry = d;
33419 +       if (unlikely(err))
33420 +               goto out;
33421 +
33422 +       rd.old_mnt_idmap = mnt_idmap(path->mnt);
33423 +       rd.old_dir = src_dir;
33424 +       rd.old_dentry = src_dentry;
33425 +       rd.new_mnt_idmap = rd.old_mnt_idmap;
33426 +       rd.new_dir = dir;
33427 +       rd.new_dentry = path->dentry;
33428 +       rd.delegated_inode = delegated_inode;
33429 +       rd.flags = flags;
33430 +       lockdep_off();
33431 +       err = vfs_rename(&rd);
33432 +       lockdep_on();
33433 +       if (!err) {
33434 +               int did;
33435 +
33436 +               tmp.dentry = d->d_parent;
33437 +               vfsub_update_h_iattr(&tmp, &did);
33438 +               if (did) {
33439 +                       tmp.dentry = src_dentry;
33440 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33441 +                       tmp.dentry = src_dentry->d_parent;
33442 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33443 +               }
33444 +               /*ignore*/
33445 +       }
33446 +
33447 +out:
33448 +       return err;
33449 +}
33450 +
33451 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33452 +{
33453 +       int err;
33454 +       struct dentry *d;
33455 +       struct mnt_idmap *idmap;
33456 +
33457 +       IMustLock(dir);
33458 +
33459 +       d = path->dentry;
33460 +       path->dentry = d->d_parent;
33461 +       err = security_path_mkdir(path, d, mode);
33462 +       path->dentry = d;
33463 +       if (unlikely(err))
33464 +               goto out;
33465 +       idmap = mnt_idmap(path->mnt);
33466 +
33467 +       lockdep_off();
33468 +       err = vfs_mkdir(idmap, dir, path->dentry, mode);
33469 +       lockdep_on();
33470 +       if (!err) {
33471 +               struct path tmp = *path;
33472 +               int did;
33473 +
33474 +               vfsub_update_h_iattr(&tmp, &did);
33475 +               if (did) {
33476 +                       tmp.dentry = path->dentry->d_parent;
33477 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33478 +               }
33479 +               /*ignore*/
33480 +       }
33481 +
33482 +out:
33483 +       return err;
33484 +}
33485 +
33486 +int vfsub_rmdir(struct inode *dir, struct path *path)
33487 +{
33488 +       int err;
33489 +       struct dentry *d;
33490 +       struct mnt_idmap *idmap;
33491 +
33492 +       IMustLock(dir);
33493 +
33494 +       d = path->dentry;
33495 +       path->dentry = d->d_parent;
33496 +       err = security_path_rmdir(path, d);
33497 +       path->dentry = d;
33498 +       if (unlikely(err))
33499 +               goto out;
33500 +       idmap = mnt_idmap(path->mnt);
33501 +
33502 +       lockdep_off();
33503 +       err = vfs_rmdir(idmap, dir, path->dentry);
33504 +       lockdep_on();
33505 +       if (!err) {
33506 +               struct path tmp = {
33507 +                       .dentry = path->dentry->d_parent,
33508 +                       .mnt    = path->mnt
33509 +               };
33510 +
33511 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33512 +       }
33513 +
33514 +out:
33515 +       return err;
33516 +}
33517 +
33518 +/* ---------------------------------------------------------------------- */
33519 +
33520 +/* todo: support mmap_sem? */
33521 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33522 +                    loff_t *ppos)
33523 +{
33524 +       ssize_t err;
33525 +
33526 +       lockdep_off();
33527 +       err = vfs_read(file, ubuf, count, ppos);
33528 +       lockdep_on();
33529 +       if (err >= 0)
33530 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33531 +       return err;
33532 +}
33533 +
33534 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33535 +                    loff_t *ppos)
33536 +{
33537 +       ssize_t err;
33538 +
33539 +       lockdep_off();
33540 +       err = kernel_read(file, kbuf, count, ppos);
33541 +       lockdep_on();
33542 +       AuTraceErr(err);
33543 +       if (err >= 0)
33544 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33545 +       return err;
33546 +}
33547 +
33548 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33549 +                     loff_t *ppos)
33550 +{
33551 +       ssize_t err;
33552 +
33553 +       lockdep_off();
33554 +       err = vfs_write(file, ubuf, count, ppos);
33555 +       lockdep_on();
33556 +       if (err >= 0)
33557 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33558 +       return err;
33559 +}
33560 +
33561 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33562 +{
33563 +       ssize_t err;
33564 +
33565 +       lockdep_off();
33566 +       err = kernel_write(file, kbuf, count, ppos);
33567 +       lockdep_on();
33568 +       if (err >= 0)
33569 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33570 +       return err;
33571 +}
33572 +
33573 +int vfsub_flush(struct file *file, fl_owner_t id)
33574 +{
33575 +       int err;
33576 +
33577 +       err = 0;
33578 +       if (file->f_op->flush) {
33579 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33580 +                       err = file->f_op->flush(file, id);
33581 +               else {
33582 +                       lockdep_off();
33583 +                       err = file->f_op->flush(file, id);
33584 +                       lockdep_on();
33585 +               }
33586 +               if (!err)
33587 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33588 +               /*ignore*/
33589 +       }
33590 +       return err;
33591 +}
33592 +
33593 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33594 +{
33595 +       int err;
33596 +
33597 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33598 +
33599 +       lockdep_off();
33600 +       err = iterate_dir(file, ctx);
33601 +       lockdep_on();
33602 +       if (err >= 0)
33603 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33604 +
33605 +       return err;
33606 +}
33607 +
33608 +ssize_t vfsub_splice_read(struct file *in, loff_t *ppos,
33609 +                         struct pipe_inode_info *pipe, size_t len,
33610 +                         unsigned int flags)
33611 +{
33612 +       ssize_t err;
33613 +
33614 +       lockdep_off();
33615 +       err = vfs_splice_read(in, ppos, pipe, len, flags);
33616 +       lockdep_on();
33617 +       file_accessed(in);
33618 +       if (err >= 0)
33619 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33620 +       return err;
33621 +}
33622 +
33623 +ssize_t vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33624 +                         loff_t *ppos, size_t len, unsigned int flags)
33625 +{
33626 +       ssize_t err;
33627 +
33628 +       lockdep_off();
33629 +       err = do_splice_from(pipe, out, ppos, len, flags);
33630 +       lockdep_on();
33631 +       if (err >= 0)
33632 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33633 +       return err;
33634 +}
33635 +
33636 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33637 +{
33638 +       int err;
33639 +
33640 +       /* file can be NULL */
33641 +       lockdep_off();
33642 +       err = vfs_fsync(file, datasync);
33643 +       lockdep_on();
33644 +       if (!err) {
33645 +               if (!path) {
33646 +                       AuDebugOn(!file);
33647 +                       path = &file->f_path;
33648 +               }
33649 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33650 +       }
33651 +       return err;
33652 +}
33653 +
33654 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33655 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33656 +               struct file *h_file)
33657 +{
33658 +       int err;
33659 +       struct inode *h_inode;
33660 +       struct super_block *h_sb;
33661 +       struct mnt_idmap *h_idmap;
33662 +
33663 +       if (!h_file) {
33664 +               err = vfsub_truncate(h_path, length);
33665 +               goto out;
33666 +       }
33667 +
33668 +       h_inode = d_inode(h_path->dentry);
33669 +       h_sb = h_inode->i_sb;
33670 +       lockdep_off();
33671 +       sb_start_write(h_sb);
33672 +       lockdep_on();
33673 +       err = security_file_truncate(h_file);
33674 +       if (!err) {
33675 +               h_idmap = mnt_idmap(h_path->mnt);
33676 +               lockdep_off();
33677 +               err = do_truncate(h_idmap, h_path->dentry, length, attr,
33678 +                                 h_file);
33679 +               lockdep_on();
33680 +       }
33681 +       lockdep_off();
33682 +       sb_end_write(h_sb);
33683 +       lockdep_on();
33684 +
33685 +out:
33686 +       return err;
33687 +}
33688 +
33689 +/* ---------------------------------------------------------------------- */
33690 +
33691 +struct au_vfsub_mkdir_args {
33692 +       int *errp;
33693 +       struct inode *dir;
33694 +       struct path *path;
33695 +       int mode;
33696 +};
33697 +
33698 +static void au_call_vfsub_mkdir(void *args)
33699 +{
33700 +       struct au_vfsub_mkdir_args *a = args;
33701 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33702 +}
33703 +
33704 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33705 +{
33706 +       int err, do_sio, wkq_err;
33707 +       struct mnt_idmap *idmap;
33708 +
33709 +       idmap = mnt_idmap(path->mnt);
33710 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33711 +       if (!do_sio) {
33712 +               lockdep_off();
33713 +               err = vfsub_mkdir(dir, path, mode);
33714 +               lockdep_on();
33715 +       } else {
33716 +               struct au_vfsub_mkdir_args args = {
33717 +                       .errp   = &err,
33718 +                       .dir    = dir,
33719 +                       .path   = path,
33720 +                       .mode   = mode
33721 +               };
33722 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33723 +               if (unlikely(wkq_err))
33724 +                       err = wkq_err;
33725 +       }
33726 +
33727 +       return err;
33728 +}
33729 +
33730 +struct au_vfsub_rmdir_args {
33731 +       int *errp;
33732 +       struct inode *dir;
33733 +       struct path *path;
33734 +};
33735 +
33736 +static void au_call_vfsub_rmdir(void *args)
33737 +{
33738 +       struct au_vfsub_rmdir_args *a = args;
33739 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33740 +}
33741 +
33742 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33743 +{
33744 +       int err, do_sio, wkq_err;
33745 +       struct mnt_idmap *idmap;
33746 +
33747 +       idmap = mnt_idmap(path->mnt);
33748 +       do_sio = au_test_h_perm_sio(idmap, dir, MAY_EXEC | MAY_WRITE);
33749 +       if (!do_sio) {
33750 +               lockdep_off();
33751 +               err = vfsub_rmdir(dir, path);
33752 +               lockdep_on();
33753 +       } else {
33754 +               struct au_vfsub_rmdir_args args = {
33755 +                       .errp   = &err,
33756 +                       .dir    = dir,
33757 +                       .path   = path
33758 +               };
33759 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33760 +               if (unlikely(wkq_err))
33761 +                       err = wkq_err;
33762 +       }
33763 +
33764 +       return err;
33765 +}
33766 +
33767 +/* ---------------------------------------------------------------------- */
33768 +
33769 +struct notify_change_args {
33770 +       int *errp;
33771 +       struct path *path;
33772 +       struct iattr *ia;
33773 +       struct inode **delegated_inode;
33774 +};
33775 +
33776 +static void call_notify_change(void *args)
33777 +{
33778 +       struct notify_change_args *a = args;
33779 +       struct inode *h_inode;
33780 +       struct mnt_idmap *idmap;
33781 +
33782 +       h_inode = d_inode(a->path->dentry);
33783 +       IMustLock(h_inode);
33784 +
33785 +       *a->errp = -EPERM;
33786 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33787 +               idmap = mnt_idmap(a->path->mnt);
33788 +               lockdep_off();
33789 +               *a->errp = notify_change(idmap, a->path->dentry, a->ia,
33790 +                                        a->delegated_inode);
33791 +               lockdep_on();
33792 +               if (!*a->errp)
33793 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33794 +       }
33795 +       AuTraceErr(*a->errp);
33796 +}
33797 +
33798 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33799 +                       struct inode **delegated_inode)
33800 +{
33801 +       int err;
33802 +       struct notify_change_args args = {
33803 +               .errp                   = &err,
33804 +               .path                   = path,
33805 +               .ia                     = ia,
33806 +               .delegated_inode        = delegated_inode
33807 +       };
33808 +
33809 +       call_notify_change(&args);
33810 +
33811 +       return err;
33812 +}
33813 +
33814 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33815 +                           struct inode **delegated_inode)
33816 +{
33817 +       int err, wkq_err;
33818 +       struct notify_change_args args = {
33819 +               .errp                   = &err,
33820 +               .path                   = path,
33821 +               .ia                     = ia,
33822 +               .delegated_inode        = delegated_inode
33823 +       };
33824 +
33825 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33826 +       if (unlikely(wkq_err))
33827 +               err = wkq_err;
33828 +
33829 +       return err;
33830 +}
33831 +
33832 +/* ---------------------------------------------------------------------- */
33833 +
33834 +struct unlink_args {
33835 +       int *errp;
33836 +       struct inode *dir;
33837 +       struct path *path;
33838 +       struct inode **delegated_inode;
33839 +};
33840 +
33841 +static void call_unlink(void *args)
33842 +{
33843 +       struct unlink_args *a = args;
33844 +       struct dentry *d = a->path->dentry;
33845 +       struct inode *h_inode;
33846 +       struct mnt_idmap *idmap;
33847 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33848 +                                     && au_dcount(d) == 1);
33849 +
33850 +       IMustLock(a->dir);
33851 +
33852 +       a->path->dentry = d->d_parent;
33853 +       *a->errp = security_path_unlink(a->path, d);
33854 +       a->path->dentry = d;
33855 +       if (unlikely(*a->errp))
33856 +               return;
33857 +
33858 +       if (!stop_sillyrename)
33859 +               dget(d);
33860 +       h_inode = NULL;
33861 +       if (d_is_positive(d)) {
33862 +               h_inode = d_inode(d);
33863 +               ihold(h_inode);
33864 +       }
33865 +
33866 +       idmap = mnt_idmap(a->path->mnt);
33867 +       lockdep_off();
33868 +       *a->errp = vfs_unlink(idmap, a->dir, d, a->delegated_inode);
33869 +       lockdep_on();
33870 +       if (!*a->errp) {
33871 +               struct path tmp = {
33872 +                       .dentry = d->d_parent,
33873 +                       .mnt    = a->path->mnt
33874 +               };
33875 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33876 +       }
33877 +
33878 +       if (!stop_sillyrename)
33879 +               dput(d);
33880 +       if (h_inode)
33881 +               iput(h_inode);
33882 +
33883 +       AuTraceErr(*a->errp);
33884 +}
33885 +
33886 +/*
33887 + * @dir: must be locked.
33888 + * @dentry: target dentry.
33889 + */
33890 +int vfsub_unlink(struct inode *dir, struct path *path,
33891 +                struct inode **delegated_inode, int force)
33892 +{
33893 +       int err;
33894 +       struct unlink_args args = {
33895 +               .errp                   = &err,
33896 +               .dir                    = dir,
33897 +               .path                   = path,
33898 +               .delegated_inode        = delegated_inode
33899 +       };
33900 +
33901 +       if (!force)
33902 +               call_unlink(&args);
33903 +       else {
33904 +               int wkq_err;
33905 +
33906 +               wkq_err = au_wkq_wait(call_unlink, &args);
33907 +               if (unlikely(wkq_err))
33908 +                       err = wkq_err;
33909 +       }
33910 +
33911 +       return err;
33912 +}
33913 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33914 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33915 +++ linux/fs/aufs/vfsub.h       2024-03-10 23:40:47.086476782 +0100
33916 @@ -0,0 +1,402 @@
33917 +/* SPDX-License-Identifier: GPL-2.0 */
33918 +/*
33919 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33920 + *
33921 + * This program is free software; you can redistribute it and/or modify
33922 + * it under the terms of the GNU General Public License as published by
33923 + * the Free Software Foundation; either version 2 of the License, or
33924 + * (at your option) any later version.
33925 + *
33926 + * This program is distributed in the hope that it will be useful,
33927 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33928 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33929 + * GNU General Public License for more details.
33930 + *
33931 + * You should have received a copy of the GNU General Public License
33932 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33933 + */
33934 +
33935 +/*
33936 + * sub-routines for VFS
33937 + */
33938 +
33939 +#ifndef __AUFS_VFSUB_H__
33940 +#define __AUFS_VFSUB_H__
33941 +
33942 +#ifdef __KERNEL__
33943 +
33944 +#include <linux/fs.h>
33945 +#include <linux/mount.h>
33946 +#include <linux/posix_acl.h>
33947 +#include <linux/xattr.h>
33948 +#include "debug.h"
33949 +
33950 +/* copied from linux/fs/internal.h */
33951 +/* todo: BAD approach!! */
33952 +extern struct file *alloc_empty_file(int, const struct cred *);
33953 +
33954 +/* ---------------------------------------------------------------------- */
33955 +
33956 +/* lock subclass for lower inode */
33957 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33958 +/* reduce? gave up. */
33959 +enum {
33960 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33961 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33962 +       AuLsc_I_PARENT2,        /* copyup dirs */
33963 +       AuLsc_I_PARENT3,        /* copyup wh */
33964 +       AuLsc_I_CHILD,
33965 +       AuLsc_I_CHILD2,
33966 +       AuLsc_I_End
33967 +};
33968 +
33969 +/* to debug easier, do not make them inlined functions */
33970 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33971 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33972 +
33973 +/* ---------------------------------------------------------------------- */
33974 +
33975 +static inline void vfsub_drop_nlink(struct inode *inode)
33976 +{
33977 +       AuDebugOn(!inode->i_nlink);
33978 +       drop_nlink(inode);
33979 +}
33980 +
33981 +static inline void vfsub_dead_dir(struct inode *inode)
33982 +{
33983 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33984 +       inode->i_flags |= S_DEAD;
33985 +       clear_nlink(inode);
33986 +}
33987 +
33988 +static inline int vfsub_native_ro(struct inode *inode)
33989 +{
33990 +       return sb_rdonly(inode->i_sb)
33991 +               || IS_RDONLY(inode)
33992 +               /* || IS_APPEND(inode) */
33993 +               || IS_IMMUTABLE(inode);
33994 +}
33995 +
33996 +#ifdef CONFIG_AUFS_BR_FUSE
33997 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33998 +#else
33999 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
34000 +#endif
34001 +
34002 +int vfsub_sync_filesystem(struct super_block *h_sb);
34003 +
34004 +/* ---------------------------------------------------------------------- */
34005 +
34006 +int vfsub_update_h_iattr(struct path *h_path, int *did);
34007 +struct file *vfsub_dentry_open(struct path *path, int flags);
34008 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
34009 +struct au_branch;
34010 +struct vfsub_aopen_args {
34011 +       struct file             *file;
34012 +       unsigned int            open_flag;
34013 +       umode_t                 create_mode;
34014 +       struct au_branch        *br;
34015 +};
34016 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
34017 +                     struct vfsub_aopen_args *args);
34018 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
34019 +
34020 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
34021 +                                            struct path *ppath, int len);
34022 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
34023 +                                   int len);
34024 +
34025 +struct vfsub_lkup_one_args {
34026 +       struct dentry **errp;
34027 +       struct qstr *name;
34028 +       struct path *ppath;
34029 +};
34030 +
34031 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
34032 +                                           struct path *ppath)
34033 +{
34034 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
34035 +}
34036 +
34037 +void vfsub_call_lkup_one(void *args);
34038 +
34039 +/* ---------------------------------------------------------------------- */
34040 +
34041 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34042 +{
34043 +       int err;
34044 +
34045 +       lockdep_off();
34046 +       err = mnt_want_write(mnt);
34047 +       lockdep_on();
34048 +       return err;
34049 +}
34050 +
34051 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34052 +{
34053 +       lockdep_off();
34054 +       mnt_drop_write(mnt);
34055 +       lockdep_on();
34056 +}
34057 +
34058 +#if 0 /* reserved */
34059 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34060 +{
34061 +       lockdep_off();
34062 +       mnt_drop_write_file(file);
34063 +       lockdep_on();
34064 +}
34065 +#endif
34066 +
34067 +static inline void vfsub_file_start_write(struct file *file)
34068 +{
34069 +       lockdep_off();
34070 +       file_start_write(file);
34071 +       lockdep_on();
34072 +}
34073 +
34074 +static inline void vfsub_file_end_write(struct file *file)
34075 +{
34076 +       lockdep_off();
34077 +       file_end_write(file);
34078 +       lockdep_on();
34079 +}
34080 +
34081 +/* ---------------------------------------------------------------------- */
34082 +
34083 +struct au_hinode;
34084 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34085 +                                struct dentry *d2, struct au_hinode *hdir2);
34086 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34087 +                        struct dentry *d2, struct au_hinode *hdir2);
34088 +
34089 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34090 +                bool want_excl);
34091 +int vfsub_symlink(struct inode *dir, struct path *path,
34092 +                 const char *symname);
34093 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34094 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34095 +              struct path *path, struct inode **delegated_inode);
34096 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34097 +                struct inode *hdir, struct path *path,
34098 +                struct inode **delegated_inode, unsigned int flags);
34099 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34100 +int vfsub_rmdir(struct inode *dir, struct path *path);
34101 +
34102 +/* ---------------------------------------------------------------------- */
34103 +
34104 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34105 +                    loff_t *ppos);
34106 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34107 +                       loff_t *ppos);
34108 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34109 +                     loff_t *ppos);
34110 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34111 +                     loff_t *ppos);
34112 +int vfsub_flush(struct file *file, fl_owner_t id);
34113 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34114 +
34115 +static inline loff_t vfsub_f_size_read(struct file *file)
34116 +{
34117 +       return i_size_read(file_inode(file));
34118 +}
34119 +
34120 +static inline unsigned int vfsub_file_flags(struct file *file)
34121 +{
34122 +       unsigned int flags;
34123 +
34124 +       spin_lock(&file->f_lock);
34125 +       flags = file->f_flags;
34126 +       spin_unlock(&file->f_lock);
34127 +
34128 +       return flags;
34129 +}
34130 +
34131 +static inline int vfsub_file_execed(struct file *file)
34132 +{
34133 +       /* todo: direct access f_flags */
34134 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34135 +}
34136 +
34137 +#if 0 /* reserved */
34138 +static inline void vfsub_file_accessed(struct file *h_file)
34139 +{
34140 +       file_accessed(h_file);
34141 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34142 +}
34143 +#endif
34144 +
34145 +#if 0 /* reserved */
34146 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34147 +                                    struct dentry *h_dentry)
34148 +{
34149 +       struct path h_path = {
34150 +               .dentry = h_dentry,
34151 +               .mnt    = h_mnt
34152 +       };
34153 +       touch_atime(&h_path);
34154 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34155 +}
34156 +#endif
34157 +
34158 +static inline int vfsub_update_time(struct inode *h_inode, int flags)
34159 +{
34160 +       return inode_update_time(h_inode, flags);
34161 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34162 +}
34163 +
34164 +#ifdef CONFIG_FS_POSIX_ACL
34165 +static inline int vfsub_acl_chmod(struct mnt_idmap *h_idmap,
34166 +                                 struct dentry *h_dentry, umode_t h_mode)
34167 +{
34168 +       int err;
34169 +
34170 +       err = posix_acl_chmod(h_idmap, h_dentry, h_mode);
34171 +       if (err == -EOPNOTSUPP)
34172 +               err = 0;
34173 +       return err;
34174 +}
34175 +#else
34176 +AuStubInt0(vfsub_acl_chmod, struct mnt_idmap *h_idmap,
34177 +          struct dentry *h_dentry, umode_t h_mode);
34178 +#endif
34179 +
34180 +ssize_t vfsub_splice_read(struct file *in, loff_t *ppos,
34181 +                         struct pipe_inode_info *pipe, size_t len,
34182 +                         unsigned int flags);
34183 +ssize_t vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34184 +                         loff_t *ppos, size_t len, unsigned int flags);
34185 +
34186 +static inline long vfsub_truncate(struct path *path, loff_t length)
34187 +{
34188 +       long err;
34189 +
34190 +       lockdep_off();
34191 +       err = vfs_truncate(path, length);
34192 +       lockdep_on();
34193 +       return err;
34194 +}
34195 +
34196 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34197 +               struct file *h_file);
34198 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34199 +
34200 +/*
34201 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34202 + * ioctl.
34203 + */
34204 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34205 +                                           loff_t len)
34206 +{
34207 +       loff_t err;
34208 +
34209 +       lockdep_off();
34210 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34211 +       lockdep_on();
34212 +
34213 +       return err;
34214 +}
34215 +
34216 +/* copy_file_range(2) is a systemcall */
34217 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34218 +                                           struct file *dst, loff_t dst_pos,
34219 +                                           size_t len, unsigned int flags)
34220 +{
34221 +       ssize_t ssz;
34222 +
34223 +       lockdep_off();
34224 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34225 +       lockdep_on();
34226 +
34227 +       return ssz;
34228 +}
34229 +
34230 +/* ---------------------------------------------------------------------- */
34231 +
34232 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34233 +{
34234 +       loff_t err;
34235 +
34236 +       lockdep_off();
34237 +       err = vfs_llseek(file, offset, origin);
34238 +       lockdep_on();
34239 +       return err;
34240 +}
34241 +
34242 +/* ---------------------------------------------------------------------- */
34243 +
34244 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34245 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34246 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34247 +                           struct inode **delegated_inode);
34248 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34249 +                       struct inode **delegated_inode);
34250 +int vfsub_unlink(struct inode *dir, struct path *path,
34251 +                struct inode **delegated_inode, int force);
34252 +
34253 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34254 +{
34255 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34256 +}
34257 +
34258 +/* ---------------------------------------------------------------------- */
34259 +
34260 +static inline int vfsub_setxattr(struct mnt_idmap *idmap,
34261 +                                struct dentry *dentry, const char *name,
34262 +                                const void *value, size_t size, int flags)
34263 +{
34264 +       int err;
34265 +
34266 +       lockdep_off();
34267 +       err = vfs_setxattr(idmap, dentry, name, value, size, flags);
34268 +       lockdep_on();
34269 +
34270 +       return err;
34271 +}
34272 +
34273 +static inline int vfsub_removexattr(struct mnt_idmap *idmap,
34274 +                                   struct dentry *dentry, const char *name)
34275 +{
34276 +       int err;
34277 +
34278 +       lockdep_off();
34279 +       err = vfs_removexattr(idmap, dentry, name);
34280 +       lockdep_on();
34281 +
34282 +       return err;
34283 +}
34284 +
34285 +#ifdef CONFIG_FS_POSIX_ACL
34286 +static inline int vfsub_set_acl(struct mnt_idmap *idmap,
34287 +                               struct dentry *dentry, const char *name,
34288 +                               struct posix_acl *acl)
34289 +{
34290 +       int err;
34291 +
34292 +       lockdep_off();
34293 +       err = vfs_set_acl(idmap, dentry, name, acl);
34294 +       lockdep_on();
34295 +
34296 +       return err;
34297 +}
34298 +
34299 +static inline int vfsub_remove_acl(struct mnt_idmap *idmap,
34300 +                                  struct dentry *dentry, const char *name)
34301 +{
34302 +       int err;
34303 +
34304 +       lockdep_off();
34305 +       err = vfs_remove_acl(idmap, dentry, name);
34306 +       lockdep_on();
34307 +
34308 +       return err;
34309 +}
34310 +#else
34311 +AuStubInt0(vfsub_set_acl, struct mnt_idmap *idmap, struct dentry *dentry,
34312 +          const char *name, struct posix_acl *acl);
34313 +AuStubInt0(vfsub_remove_acl, struct mnt_idmap *idmap,
34314 +          struct dentry *dentry, const char *name);
34315 +#endif
34316 +
34317 +#endif /* __KERNEL__ */
34318 +#endif /* __AUFS_VFSUB_H__ */
34319 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34320 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34321 +++ linux/fs/aufs/wbr_policy.c  2024-03-10 23:40:47.086476782 +0100
34322 @@ -0,0 +1,830 @@
34323 +// SPDX-License-Identifier: GPL-2.0
34324 +/*
34325 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34326 + *
34327 + * This program is free software; you can redistribute it and/or modify
34328 + * it under the terms of the GNU General Public License as published by
34329 + * the Free Software Foundation; either version 2 of the License, or
34330 + * (at your option) any later version.
34331 + *
34332 + * This program is distributed in the hope that it will be useful,
34333 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34334 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34335 + * GNU General Public License for more details.
34336 + *
34337 + * You should have received a copy of the GNU General Public License
34338 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34339 + */
34340 +
34341 +/*
34342 + * policies for selecting one among multiple writable branches
34343 + */
34344 +
34345 +#include <linux/statfs.h>
34346 +#include "aufs.h"
34347 +
34348 +/* subset of cpup_attr() */
34349 +static noinline_for_stack
34350 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34351 +{
34352 +       int err, sbits;
34353 +       struct iattr ia;
34354 +       struct inode *h_isrc;
34355 +
34356 +       h_isrc = d_inode(h_src);
34357 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34358 +       ia.ia_mode = h_isrc->i_mode;
34359 +       ia.ia_uid = h_isrc->i_uid;
34360 +       ia.ia_gid = h_isrc->i_gid;
34361 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34362 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34363 +       /* no delegation since it is just created */
34364 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34365 +
34366 +       /* is this nfs only? */
34367 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34368 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34369 +               ia.ia_mode = h_isrc->i_mode;
34370 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34371 +       }
34372 +
34373 +       return err;
34374 +}
34375 +
34376 +#define AuCpdown_PARENT_OPQ    BIT(0)
34377 +#define AuCpdown_WHED          BIT(1)
34378 +#define AuCpdown_MADE_DIR      BIT(2)
34379 +#define AuCpdown_DIROPQ                BIT(3)
34380 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34381 +#define au_fset_cpdown(flags, name) \
34382 +       do { (flags) |= AuCpdown_##name; } while (0)
34383 +#define au_fclr_cpdown(flags, name) \
34384 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34385 +
34386 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34387 +                            unsigned int *flags)
34388 +{
34389 +       int err;
34390 +       struct dentry *opq_dentry;
34391 +
34392 +       opq_dentry = au_diropq_create(dentry, bdst);
34393 +       err = PTR_ERR(opq_dentry);
34394 +       if (IS_ERR(opq_dentry))
34395 +               goto out;
34396 +       dput(opq_dentry);
34397 +       au_fset_cpdown(*flags, DIROPQ);
34398 +
34399 +out:
34400 +       return err;
34401 +}
34402 +
34403 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34404 +                           struct inode *dir, aufs_bindex_t bdst)
34405 +{
34406 +       int err;
34407 +       struct path h_path;
34408 +       struct au_branch *br;
34409 +
34410 +       br = au_sbr(dentry->d_sb, bdst);
34411 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34412 +       err = PTR_ERR(h_path.dentry);
34413 +       if (IS_ERR(h_path.dentry))
34414 +               goto out;
34415 +
34416 +       err = 0;
34417 +       if (d_is_positive(h_path.dentry)) {
34418 +               h_path.mnt = au_br_mnt(br);
34419 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34420 +                                         dentry);
34421 +       }
34422 +       dput(h_path.dentry);
34423 +
34424 +out:
34425 +       return err;
34426 +}
34427 +
34428 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34429 +                        struct au_pin *pin,
34430 +                        struct dentry *h_parent, void *arg)
34431 +{
34432 +       int err, rerr;
34433 +       aufs_bindex_t bopq, btop;
34434 +       struct path h_path;
34435 +       struct dentry *parent;
34436 +       struct inode *h_dir, *h_inode, *inode, *dir;
34437 +       unsigned int *flags = arg;
34438 +
34439 +       btop = au_dbtop(dentry);
34440 +       /* dentry is di-locked */
34441 +       parent = dget_parent(dentry);
34442 +       dir = d_inode(parent);
34443 +       h_dir = d_inode(h_parent);
34444 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34445 +       IMustLock(h_dir);
34446 +
34447 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34448 +       if (unlikely(err < 0))
34449 +               goto out;
34450 +       h_path.dentry = au_h_dptr(dentry, bdst);
34451 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34452 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34453 +       if (unlikely(err))
34454 +               goto out_put;
34455 +       au_fset_cpdown(*flags, MADE_DIR);
34456 +
34457 +       bopq = au_dbdiropq(dentry);
34458 +       au_fclr_cpdown(*flags, WHED);
34459 +       au_fclr_cpdown(*flags, DIROPQ);
34460 +       if (au_dbwh(dentry) == bdst)
34461 +               au_fset_cpdown(*flags, WHED);
34462 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34463 +               au_fset_cpdown(*flags, PARENT_OPQ);
34464 +       h_inode = d_inode(h_path.dentry);
34465 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34466 +       if (au_ftest_cpdown(*flags, WHED)) {
34467 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34468 +               if (unlikely(err)) {
34469 +                       inode_unlock(h_inode);
34470 +                       goto out_dir;
34471 +               }
34472 +       }
34473 +
34474 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34475 +       inode_unlock(h_inode);
34476 +       if (unlikely(err))
34477 +               goto out_opq;
34478 +
34479 +       if (au_ftest_cpdown(*flags, WHED)) {
34480 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34481 +               if (unlikely(err))
34482 +                       goto out_opq;
34483 +       }
34484 +
34485 +       inode = d_inode(dentry);
34486 +       if (au_ibbot(inode) < bdst)
34487 +               au_set_ibbot(inode, bdst);
34488 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34489 +                     au_hi_flags(inode, /*isdir*/1));
34490 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34491 +       goto out; /* success */
34492 +
34493 +       /* revert */
34494 +out_opq:
34495 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34496 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34497 +               rerr = au_diropq_remove(dentry, bdst);
34498 +               inode_unlock(h_inode);
34499 +               if (unlikely(rerr)) {
34500 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34501 +                               dentry, bdst, rerr);
34502 +                       err = -EIO;
34503 +                       goto out;
34504 +               }
34505 +       }
34506 +out_dir:
34507 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34508 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34509 +               if (unlikely(rerr)) {
34510 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34511 +                               dentry, bdst, rerr);
34512 +                       err = -EIO;
34513 +               }
34514 +       }
34515 +out_put:
34516 +       au_set_h_dptr(dentry, bdst, NULL);
34517 +       if (au_dbbot(dentry) == bdst)
34518 +               au_update_dbbot(dentry);
34519 +out:
34520 +       dput(parent);
34521 +       return err;
34522 +}
34523 +
34524 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34525 +{
34526 +       int err;
34527 +       unsigned int flags;
34528 +
34529 +       flags = 0;
34530 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34531 +
34532 +       return err;
34533 +}
34534 +
34535 +/* ---------------------------------------------------------------------- */
34536 +
34537 +/* policies for create */
34538 +
34539 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34540 +{
34541 +       int err, i, j, ndentry;
34542 +       aufs_bindex_t bopq;
34543 +       struct au_dcsub_pages dpages;
34544 +       struct au_dpage *dpage;
34545 +       struct dentry **dentries, *parent, *d;
34546 +
34547 +       err = au_dpages_init(&dpages, GFP_NOFS);
34548 +       if (unlikely(err))
34549 +               goto out;
34550 +       parent = dget_parent(dentry);
34551 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34552 +       if (unlikely(err))
34553 +               goto out_free;
34554 +
34555 +       err = bindex;
34556 +       for (i = 0; i < dpages.ndpage; i++) {
34557 +               dpage = dpages.dpages + i;
34558 +               dentries = dpage->dentries;
34559 +               ndentry = dpage->ndentry;
34560 +               for (j = 0; j < ndentry; j++) {
34561 +                       d = dentries[j];
34562 +                       di_read_lock_parent2(d, !AuLock_IR);
34563 +                       bopq = au_dbdiropq(d);
34564 +                       di_read_unlock(d, !AuLock_IR);
34565 +                       if (bopq >= 0 && bopq < err)
34566 +                               err = bopq;
34567 +               }
34568 +       }
34569 +
34570 +out_free:
34571 +       dput(parent);
34572 +       au_dpages_free(&dpages);
34573 +out:
34574 +       return err;
34575 +}
34576 +
34577 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34578 +{
34579 +       for (; bindex >= 0; bindex--)
34580 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34581 +                       return bindex;
34582 +       return -EROFS;
34583 +}
34584 +
34585 +/* top down parent */
34586 +static int au_wbr_create_tdp(struct dentry *dentry,
34587 +                            unsigned int flags __maybe_unused)
34588 +{
34589 +       int err;
34590 +       aufs_bindex_t btop, bindex;
34591 +       struct super_block *sb;
34592 +       struct dentry *parent, *h_parent;
34593 +
34594 +       sb = dentry->d_sb;
34595 +       btop = au_dbtop(dentry);
34596 +       err = btop;
34597 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34598 +               goto out;
34599 +
34600 +       err = -EROFS;
34601 +       parent = dget_parent(dentry);
34602 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34603 +               h_parent = au_h_dptr(parent, bindex);
34604 +               if (!h_parent || d_is_negative(h_parent))
34605 +                       continue;
34606 +
34607 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34608 +                       err = bindex;
34609 +                       break;
34610 +               }
34611 +       }
34612 +       dput(parent);
34613 +
34614 +       /* bottom up here */
34615 +       if (unlikely(err < 0)) {
34616 +               err = au_wbr_bu(sb, btop - 1);
34617 +               if (err >= 0)
34618 +                       err = au_wbr_nonopq(dentry, err);
34619 +       }
34620 +
34621 +out:
34622 +       AuDbg("b%d\n", err);
34623 +       return err;
34624 +}
34625 +
34626 +/* ---------------------------------------------------------------------- */
34627 +
34628 +/* an exception for the policy other than tdp */
34629 +static int au_wbr_create_exp(struct dentry *dentry)
34630 +{
34631 +       int err;
34632 +       aufs_bindex_t bwh, bdiropq;
34633 +       struct dentry *parent;
34634 +
34635 +       err = -1;
34636 +       bwh = au_dbwh(dentry);
34637 +       parent = dget_parent(dentry);
34638 +       bdiropq = au_dbdiropq(parent);
34639 +       if (bwh >= 0) {
34640 +               if (bdiropq >= 0)
34641 +                       err = min(bdiropq, bwh);
34642 +               else
34643 +                       err = bwh;
34644 +               AuDbg("%d\n", err);
34645 +       } else if (bdiropq >= 0) {
34646 +               err = bdiropq;
34647 +               AuDbg("%d\n", err);
34648 +       }
34649 +       dput(parent);
34650 +
34651 +       if (err >= 0)
34652 +               err = au_wbr_nonopq(dentry, err);
34653 +
34654 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34655 +               err = -1;
34656 +
34657 +       AuDbg("%d\n", err);
34658 +       return err;
34659 +}
34660 +
34661 +/* ---------------------------------------------------------------------- */
34662 +
34663 +/* round robin */
34664 +static int au_wbr_create_init_rr(struct super_block *sb)
34665 +{
34666 +       int err;
34667 +
34668 +       err = au_wbr_bu(sb, au_sbbot(sb));
34669 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34670 +       /* smp_mb(); */
34671 +
34672 +       AuDbg("b%d\n", err);
34673 +       return err;
34674 +}
34675 +
34676 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34677 +{
34678 +       int err, nbr;
34679 +       unsigned int u;
34680 +       aufs_bindex_t bindex, bbot;
34681 +       struct super_block *sb;
34682 +       atomic_t *next;
34683 +
34684 +       err = au_wbr_create_exp(dentry);
34685 +       if (err >= 0)
34686 +               goto out;
34687 +
34688 +       sb = dentry->d_sb;
34689 +       next = &au_sbi(sb)->si_wbr_rr_next;
34690 +       bbot = au_sbbot(sb);
34691 +       nbr = bbot + 1;
34692 +       for (bindex = 0; bindex <= bbot; bindex++) {
34693 +               if (!au_ftest_wbr(flags, DIR)) {
34694 +                       err = atomic_dec_return(next) + 1;
34695 +                       /* modulo for 0 is meaningless */
34696 +                       if (unlikely(!err))
34697 +                               err = atomic_dec_return(next) + 1;
34698 +               } else
34699 +                       err = atomic_read(next);
34700 +               AuDbg("%d\n", err);
34701 +               u = err;
34702 +               err = u % nbr;
34703 +               AuDbg("%d\n", err);
34704 +               if (!au_br_rdonly(au_sbr(sb, err)))
34705 +                       break;
34706 +               err = -EROFS;
34707 +       }
34708 +
34709 +       if (err >= 0)
34710 +               err = au_wbr_nonopq(dentry, err);
34711 +
34712 +out:
34713 +       AuDbg("%d\n", err);
34714 +       return err;
34715 +}
34716 +
34717 +/* ---------------------------------------------------------------------- */
34718 +
34719 +/* most free space */
34720 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34721 +{
34722 +       struct super_block *sb;
34723 +       struct au_branch *br;
34724 +       struct au_wbr_mfs *mfs;
34725 +       struct dentry *h_parent;
34726 +       aufs_bindex_t bindex, bbot;
34727 +       int err;
34728 +       unsigned long long b, bavail;
34729 +       struct path h_path;
34730 +       /* reduce the stack usage */
34731 +       struct kstatfs *st;
34732 +
34733 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34734 +       if (unlikely(!st)) {
34735 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34736 +               return;
34737 +       }
34738 +
34739 +       bavail = 0;
34740 +       sb = dentry->d_sb;
34741 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34742 +       MtxMustLock(&mfs->mfs_lock);
34743 +       mfs->mfs_bindex = -EROFS;
34744 +       mfs->mfsrr_bytes = 0;
34745 +       if (!parent) {
34746 +               bindex = 0;
34747 +               bbot = au_sbbot(sb);
34748 +       } else {
34749 +               bindex = au_dbtop(parent);
34750 +               bbot = au_dbtaildir(parent);
34751 +       }
34752 +
34753 +       for (; bindex <= bbot; bindex++) {
34754 +               if (parent) {
34755 +                       h_parent = au_h_dptr(parent, bindex);
34756 +                       if (!h_parent || d_is_negative(h_parent))
34757 +                               continue;
34758 +               }
34759 +               br = au_sbr(sb, bindex);
34760 +               if (au_br_rdonly(br))
34761 +                       continue;
34762 +
34763 +               /* sb->s_root for NFS is unreliable */
34764 +               h_path.mnt = au_br_mnt(br);
34765 +               h_path.dentry = h_path.mnt->mnt_root;
34766 +               err = vfs_statfs(&h_path, st);
34767 +               if (unlikely(err)) {
34768 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34769 +                       continue;
34770 +               }
34771 +
34772 +               /* when the available size is equal, select the lower one */
34773 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34774 +                            || sizeof(b) < sizeof(st->f_bsize));
34775 +               b = st->f_bavail * st->f_bsize;
34776 +               br->br_wbr->wbr_bytes = b;
34777 +               if (b >= bavail) {
34778 +                       bavail = b;
34779 +                       mfs->mfs_bindex = bindex;
34780 +                       mfs->mfs_jiffy = jiffies;
34781 +               }
34782 +       }
34783 +
34784 +       mfs->mfsrr_bytes = bavail;
34785 +       AuDbg("b%d\n", mfs->mfs_bindex);
34786 +       au_kfree_rcu(st);
34787 +}
34788 +
34789 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34790 +{
34791 +       int err;
34792 +       struct dentry *parent;
34793 +       struct super_block *sb;
34794 +       struct au_wbr_mfs *mfs;
34795 +
34796 +       err = au_wbr_create_exp(dentry);
34797 +       if (err >= 0)
34798 +               goto out;
34799 +
34800 +       sb = dentry->d_sb;
34801 +       parent = NULL;
34802 +       if (au_ftest_wbr(flags, PARENT))
34803 +               parent = dget_parent(dentry);
34804 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34805 +       mutex_lock(&mfs->mfs_lock);
34806 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34807 +           || mfs->mfs_bindex < 0
34808 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34809 +               au_mfs(dentry, parent);
34810 +       mutex_unlock(&mfs->mfs_lock);
34811 +       err = mfs->mfs_bindex;
34812 +       dput(parent);
34813 +
34814 +       if (err >= 0)
34815 +               err = au_wbr_nonopq(dentry, err);
34816 +
34817 +out:
34818 +       AuDbg("b%d\n", err);
34819 +       return err;
34820 +}
34821 +
34822 +static int au_wbr_create_init_mfs(struct super_block *sb)
34823 +{
34824 +       struct au_wbr_mfs *mfs;
34825 +
34826 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34827 +       mutex_init(&mfs->mfs_lock);
34828 +       mfs->mfs_jiffy = 0;
34829 +       mfs->mfs_bindex = -EROFS;
34830 +
34831 +       return 0;
34832 +}
34833 +
34834 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34835 +{
34836 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34837 +       return 0;
34838 +}
34839 +
34840 +/* ---------------------------------------------------------------------- */
34841 +
34842 +/* top down regardless parent, and then mfs */
34843 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34844 +                              unsigned int flags __maybe_unused)
34845 +{
34846 +       int err;
34847 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34848 +       unsigned long long watermark;
34849 +       struct super_block *sb;
34850 +       struct au_wbr_mfs *mfs;
34851 +       struct au_branch *br;
34852 +       struct dentry *parent;
34853 +
34854 +       sb = dentry->d_sb;
34855 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34856 +       mutex_lock(&mfs->mfs_lock);
34857 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34858 +           || mfs->mfs_bindex < 0)
34859 +               au_mfs(dentry, /*parent*/NULL);
34860 +       watermark = mfs->mfsrr_watermark;
34861 +       bmfs = mfs->mfs_bindex;
34862 +       mutex_unlock(&mfs->mfs_lock);
34863 +
34864 +       /* another style of au_wbr_create_exp() */
34865 +       bwh = au_dbwh(dentry);
34866 +       parent = dget_parent(dentry);
34867 +       btail = au_dbtaildir(parent);
34868 +       if (bwh >= 0 && bwh < btail)
34869 +               btail = bwh;
34870 +
34871 +       err = au_wbr_nonopq(dentry, btail);
34872 +       if (unlikely(err < 0))
34873 +               goto out;
34874 +       btail = err;
34875 +       bfound = -1;
34876 +       for (bindex = 0; bindex <= btail; bindex++) {
34877 +               br = au_sbr(sb, bindex);
34878 +               if (au_br_rdonly(br))
34879 +                       continue;
34880 +               if (br->br_wbr->wbr_bytes > watermark) {
34881 +                       bfound = bindex;
34882 +                       break;
34883 +               }
34884 +       }
34885 +       err = bfound;
34886 +       if (err < 0)
34887 +               err = bmfs;
34888 +
34889 +out:
34890 +       dput(parent);
34891 +       AuDbg("b%d\n", err);
34892 +       return err;
34893 +}
34894 +
34895 +/* ---------------------------------------------------------------------- */
34896 +
34897 +/* most free space and then round robin */
34898 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34899 +{
34900 +       int err;
34901 +       struct au_wbr_mfs *mfs;
34902 +
34903 +       err = au_wbr_create_mfs(dentry, flags);
34904 +       if (err >= 0) {
34905 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34906 +               mutex_lock(&mfs->mfs_lock);
34907 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34908 +                       err = au_wbr_create_rr(dentry, flags);
34909 +               mutex_unlock(&mfs->mfs_lock);
34910 +       }
34911 +
34912 +       AuDbg("b%d\n", err);
34913 +       return err;
34914 +}
34915 +
34916 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34917 +{
34918 +       int err;
34919 +
34920 +       au_wbr_create_init_mfs(sb); /* ignore */
34921 +       err = au_wbr_create_init_rr(sb);
34922 +
34923 +       return err;
34924 +}
34925 +
34926 +/* ---------------------------------------------------------------------- */
34927 +
34928 +/* top down parent and most free space */
34929 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34930 +{
34931 +       int err, e2;
34932 +       unsigned long long b;
34933 +       aufs_bindex_t bindex, btop, bbot;
34934 +       struct super_block *sb;
34935 +       struct dentry *parent, *h_parent;
34936 +       struct au_branch *br;
34937 +
34938 +       err = au_wbr_create_tdp(dentry, flags);
34939 +       if (unlikely(err < 0))
34940 +               goto out;
34941 +       parent = dget_parent(dentry);
34942 +       btop = au_dbtop(parent);
34943 +       bbot = au_dbtaildir(parent);
34944 +       if (btop == bbot)
34945 +               goto out_parent; /* success */
34946 +
34947 +       e2 = au_wbr_create_mfs(dentry, flags);
34948 +       if (e2 < 0)
34949 +               goto out_parent; /* success */
34950 +
34951 +       /* when the available size is equal, select upper one */
34952 +       sb = dentry->d_sb;
34953 +       br = au_sbr(sb, err);
34954 +       b = br->br_wbr->wbr_bytes;
34955 +       AuDbg("b%d, %llu\n", err, b);
34956 +
34957 +       for (bindex = btop; bindex <= bbot; bindex++) {
34958 +               h_parent = au_h_dptr(parent, bindex);
34959 +               if (!h_parent || d_is_negative(h_parent))
34960 +                       continue;
34961 +
34962 +               br = au_sbr(sb, bindex);
34963 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34964 +                       b = br->br_wbr->wbr_bytes;
34965 +                       err = bindex;
34966 +                       AuDbg("b%d, %llu\n", err, b);
34967 +               }
34968 +       }
34969 +
34970 +       if (err >= 0)
34971 +               err = au_wbr_nonopq(dentry, err);
34972 +
34973 +out_parent:
34974 +       dput(parent);
34975 +out:
34976 +       AuDbg("b%d\n", err);
34977 +       return err;
34978 +}
34979 +
34980 +/* ---------------------------------------------------------------------- */
34981 +
34982 +/*
34983 + * - top down parent
34984 + * - most free space with parent
34985 + * - most free space round-robin regardless parent
34986 + */
34987 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34988 +{
34989 +       int err;
34990 +       unsigned long long watermark;
34991 +       struct super_block *sb;
34992 +       struct au_branch *br;
34993 +       struct au_wbr_mfs *mfs;
34994 +
34995 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34996 +       if (unlikely(err < 0))
34997 +               goto out;
34998 +
34999 +       sb = dentry->d_sb;
35000 +       br = au_sbr(sb, err);
35001 +       mfs = &au_sbi(sb)->si_wbr_mfs;
35002 +       mutex_lock(&mfs->mfs_lock);
35003 +       watermark = mfs->mfsrr_watermark;
35004 +       mutex_unlock(&mfs->mfs_lock);
35005 +       if (br->br_wbr->wbr_bytes < watermark)
35006 +               /* regardless the parent dir */
35007 +               err = au_wbr_create_mfsrr(dentry, flags);
35008 +
35009 +out:
35010 +       AuDbg("b%d\n", err);
35011 +       return err;
35012 +}
35013 +
35014 +/* ---------------------------------------------------------------------- */
35015 +
35016 +/* policies for copyup */
35017 +
35018 +/* top down parent */
35019 +static int au_wbr_copyup_tdp(struct dentry *dentry)
35020 +{
35021 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
35022 +}
35023 +
35024 +/* bottom up parent */
35025 +static int au_wbr_copyup_bup(struct dentry *dentry)
35026 +{
35027 +       int err;
35028 +       aufs_bindex_t bindex, btop;
35029 +       struct dentry *parent, *h_parent;
35030 +       struct super_block *sb;
35031 +
35032 +       err = -EROFS;
35033 +       sb = dentry->d_sb;
35034 +       parent = dget_parent(dentry);
35035 +       btop = au_dbtop(parent);
35036 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
35037 +               h_parent = au_h_dptr(parent, bindex);
35038 +               if (!h_parent || d_is_negative(h_parent))
35039 +                       continue;
35040 +
35041 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
35042 +                       err = bindex;
35043 +                       break;
35044 +               }
35045 +       }
35046 +       dput(parent);
35047 +
35048 +       /* bottom up here */
35049 +       if (unlikely(err < 0))
35050 +               err = au_wbr_bu(sb, btop - 1);
35051 +
35052 +       AuDbg("b%d\n", err);
35053 +       return err;
35054 +}
35055 +
35056 +/* bottom up */
35057 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
35058 +{
35059 +       int err;
35060 +
35061 +       err = au_wbr_bu(dentry->d_sb, btop);
35062 +       AuDbg("b%d\n", err);
35063 +       if (err > btop)
35064 +               err = au_wbr_nonopq(dentry, err);
35065 +
35066 +       AuDbg("b%d\n", err);
35067 +       return err;
35068 +}
35069 +
35070 +static int au_wbr_copyup_bu(struct dentry *dentry)
35071 +{
35072 +       int err;
35073 +       aufs_bindex_t btop;
35074 +
35075 +       btop = au_dbtop(dentry);
35076 +       err = au_wbr_do_copyup_bu(dentry, btop);
35077 +       return err;
35078 +}
35079 +
35080 +/* ---------------------------------------------------------------------- */
35081 +
35082 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35083 +       [AuWbrCopyup_TDP] = {
35084 +               .copyup = au_wbr_copyup_tdp
35085 +       },
35086 +       [AuWbrCopyup_BUP] = {
35087 +               .copyup = au_wbr_copyup_bup
35088 +       },
35089 +       [AuWbrCopyup_BU] = {
35090 +               .copyup = au_wbr_copyup_bu
35091 +       }
35092 +};
35093 +
35094 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35095 +       [AuWbrCreate_TDP] = {
35096 +               .create = au_wbr_create_tdp
35097 +       },
35098 +       [AuWbrCreate_RR] = {
35099 +               .create = au_wbr_create_rr,
35100 +               .init   = au_wbr_create_init_rr
35101 +       },
35102 +       [AuWbrCreate_MFS] = {
35103 +               .create = au_wbr_create_mfs,
35104 +               .init   = au_wbr_create_init_mfs,
35105 +               .fin    = au_wbr_create_fin_mfs
35106 +       },
35107 +       [AuWbrCreate_MFSV] = {
35108 +               .create = au_wbr_create_mfs,
35109 +               .init   = au_wbr_create_init_mfs,
35110 +               .fin    = au_wbr_create_fin_mfs
35111 +       },
35112 +       [AuWbrCreate_MFSRR] = {
35113 +               .create = au_wbr_create_mfsrr,
35114 +               .init   = au_wbr_create_init_mfsrr,
35115 +               .fin    = au_wbr_create_fin_mfs
35116 +       },
35117 +       [AuWbrCreate_MFSRRV] = {
35118 +               .create = au_wbr_create_mfsrr,
35119 +               .init   = au_wbr_create_init_mfsrr,
35120 +               .fin    = au_wbr_create_fin_mfs
35121 +       },
35122 +       [AuWbrCreate_TDMFS] = {
35123 +               .create = au_wbr_create_tdmfs,
35124 +               .init   = au_wbr_create_init_mfs,
35125 +               .fin    = au_wbr_create_fin_mfs
35126 +       },
35127 +       [AuWbrCreate_TDMFSV] = {
35128 +               .create = au_wbr_create_tdmfs,
35129 +               .init   = au_wbr_create_init_mfs,
35130 +               .fin    = au_wbr_create_fin_mfs
35131 +       },
35132 +       [AuWbrCreate_PMFS] = {
35133 +               .create = au_wbr_create_pmfs,
35134 +               .init   = au_wbr_create_init_mfs,
35135 +               .fin    = au_wbr_create_fin_mfs
35136 +       },
35137 +       [AuWbrCreate_PMFSV] = {
35138 +               .create = au_wbr_create_pmfs,
35139 +               .init   = au_wbr_create_init_mfs,
35140 +               .fin    = au_wbr_create_fin_mfs
35141 +       },
35142 +       [AuWbrCreate_PMFSRR] = {
35143 +               .create = au_wbr_create_pmfsrr,
35144 +               .init   = au_wbr_create_init_mfsrr,
35145 +               .fin    = au_wbr_create_fin_mfs
35146 +       },
35147 +       [AuWbrCreate_PMFSRRV] = {
35148 +               .create = au_wbr_create_pmfsrr,
35149 +               .init   = au_wbr_create_init_mfsrr,
35150 +               .fin    = au_wbr_create_fin_mfs
35151 +       }
35152 +};
35153 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35154 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35155 +++ linux/fs/aufs/whout.c       2023-10-31 09:31:04.199880750 +0100
35156 @@ -0,0 +1,1072 @@
35157 +// SPDX-License-Identifier: GPL-2.0
35158 +/*
35159 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35160 + *
35161 + * This program is free software; you can redistribute it and/or modify
35162 + * it under the terms of the GNU General Public License as published by
35163 + * the Free Software Foundation; either version 2 of the License, or
35164 + * (at your option) any later version.
35165 + *
35166 + * This program is distributed in the hope that it will be useful,
35167 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35168 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35169 + * GNU General Public License for more details.
35170 + *
35171 + * You should have received a copy of the GNU General Public License
35172 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35173 + */
35174 +
35175 +/*
35176 + * whiteout for logical deletion and opaque directory
35177 + */
35178 +
35179 +#include "aufs.h"
35180 +
35181 +#define WH_MASK                        0444
35182 +
35183 +/*
35184 + * If a directory contains this file, then it is opaque.  We start with the
35185 + * .wh. flag so that it is blocked by lookup.
35186 + */
35187 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35188 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35189 +
35190 +/*
35191 + * generate whiteout name, which is NOT terminated by NULL.
35192 + * @name: original d_name.name
35193 + * @len: original d_name.len
35194 + * @wh: whiteout qstr
35195 + * returns zero when succeeds, otherwise error.
35196 + * succeeded value as wh->name should be freed by kfree().
35197 + */
35198 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35199 +{
35200 +       char *p;
35201 +
35202 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35203 +               return -ENAMETOOLONG;
35204 +
35205 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35206 +       p = kmalloc(wh->len, GFP_NOFS);
35207 +       wh->name = p;
35208 +       if (p) {
35209 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35210 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35211 +               /* smp_mb(); */
35212 +               return 0;
35213 +       }
35214 +       return -ENOMEM;
35215 +}
35216 +
35217 +/* ---------------------------------------------------------------------- */
35218 +
35219 +/*
35220 + * test if the @wh_name exists under @h_ppath.
35221 + * @try_sio specifies the necessary of super-io.
35222 + */
35223 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
35224 +              struct qstr *wh_name, int try_sio)
35225 +{
35226 +       int err;
35227 +       struct dentry *wh_dentry;
35228 +
35229 +       if (!try_sio)
35230 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35231 +       else
35232 +               wh_dentry = au_sio_lkup_one(h_idmap, wh_name, h_ppath);
35233 +       err = PTR_ERR(wh_dentry);
35234 +       if (IS_ERR(wh_dentry)) {
35235 +               if (err == -ENAMETOOLONG)
35236 +                       err = 0;
35237 +               goto out;
35238 +       }
35239 +
35240 +       err = 0;
35241 +       if (d_is_negative(wh_dentry))
35242 +               goto out_wh; /* success */
35243 +
35244 +       err = 1;
35245 +       if (d_is_reg(wh_dentry))
35246 +               goto out_wh; /* success */
35247 +
35248 +       err = -EIO;
35249 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35250 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35251 +
35252 +out_wh:
35253 +       dput(wh_dentry);
35254 +out:
35255 +       return err;
35256 +}
35257 +
35258 +/*
35259 + * test if the @h_path->dentry sets opaque or not.
35260 + */
35261 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path)
35262 +{
35263 +       int err;
35264 +       struct inode *h_dir;
35265 +
35266 +       h_dir = d_inode(h_path->dentry);
35267 +       err = au_wh_test(h_idmap, h_path, &diropq_name,
35268 +                        au_test_h_perm_sio(h_idmap, h_dir, MAY_EXEC));
35269 +       return err;
35270 +}
35271 +
35272 +/*
35273 + * returns a negative dentry whose name is unique and temporary.
35274 + */
35275 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35276 +                            struct qstr *prefix)
35277 +{
35278 +       struct dentry *dentry;
35279 +       int i;
35280 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35281 +               *name, *p;
35282 +       /* strict atomic_t is unnecessary here */
35283 +       static unsigned short cnt;
35284 +       struct qstr qs;
35285 +       struct path h_ppath;
35286 +       struct mnt_idmap *h_idmap;
35287 +
35288 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35289 +
35290 +       name = defname;
35291 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35292 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35293 +               dentry = ERR_PTR(-ENAMETOOLONG);
35294 +               if (unlikely(qs.len > NAME_MAX))
35295 +                       goto out;
35296 +               dentry = ERR_PTR(-ENOMEM);
35297 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35298 +               if (unlikely(!name))
35299 +                       goto out;
35300 +       }
35301 +
35302 +       /* doubly whiteout-ed */
35303 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35304 +       p = name + AUFS_WH_PFX_LEN * 2;
35305 +       memcpy(p, prefix->name, prefix->len);
35306 +       p += prefix->len;
35307 +       *p++ = '.';
35308 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35309 +
35310 +       h_ppath.dentry = h_parent;
35311 +       h_ppath.mnt = au_br_mnt(br);
35312 +       h_idmap = au_br_idmap(br);
35313 +       qs.name = name;
35314 +       for (i = 0; i < 3; i++) {
35315 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35316 +               dentry = au_sio_lkup_one(h_idmap, &qs, &h_ppath);
35317 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35318 +                       goto out_name;
35319 +               dput(dentry);
35320 +       }
35321 +       /* pr_warn("could not get random name\n"); */
35322 +       dentry = ERR_PTR(-EEXIST);
35323 +       AuDbg("%.*s\n", AuLNPair(&qs));
35324 +       BUG();
35325 +
35326 +out_name:
35327 +       if (name != defname)
35328 +               au_kfree_try_rcu(name);
35329 +out:
35330 +       AuTraceErrPtr(dentry);
35331 +       return dentry;
35332 +}
35333 +
35334 +/*
35335 + * rename the @h_dentry on @br to the whiteouted temporary name.
35336 + */
35337 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35338 +{
35339 +       int err;
35340 +       struct path h_path = {
35341 +               .mnt = au_br_mnt(br)
35342 +       };
35343 +       struct inode *h_dir, *delegated;
35344 +       struct dentry *h_parent;
35345 +
35346 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35347 +       h_dir = d_inode(h_parent);
35348 +       IMustLock(h_dir);
35349 +
35350 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35351 +       err = PTR_ERR(h_path.dentry);
35352 +       if (IS_ERR(h_path.dentry))
35353 +               goto out;
35354 +
35355 +       /* under the same dir, no need to lock_rename() */
35356 +       delegated = NULL;
35357 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35358 +                          /*flags*/0);
35359 +       AuTraceErr(err);
35360 +       if (unlikely(err == -EWOULDBLOCK)) {
35361 +               pr_warn("cannot retry for NFSv4 delegation"
35362 +                       " for an internal rename\n");
35363 +               iput(delegated);
35364 +       }
35365 +       dput(h_path.dentry);
35366 +
35367 +out:
35368 +       AuTraceErr(err);
35369 +       return err;
35370 +}
35371 +
35372 +/* ---------------------------------------------------------------------- */
35373 +/*
35374 + * functions for removing a whiteout
35375 + */
35376 +
35377 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35378 +{
35379 +       int err, force;
35380 +       struct inode *delegated;
35381 +
35382 +       /*
35383 +        * forces superio when the dir has a sticky bit.
35384 +        * this may be a violation of unix fs semantics.
35385 +        */
35386 +       force = (h_dir->i_mode & S_ISVTX)
35387 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35388 +       delegated = NULL;
35389 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35390 +       if (unlikely(err == -EWOULDBLOCK)) {
35391 +               pr_warn("cannot retry for NFSv4 delegation"
35392 +                       " for an internal unlink\n");
35393 +               iput(delegated);
35394 +       }
35395 +       return err;
35396 +}
35397 +
35398 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35399 +                       struct dentry *dentry)
35400 +{
35401 +       int err;
35402 +
35403 +       err = do_unlink_wh(h_dir, h_path);
35404 +       if (!err && dentry)
35405 +               au_set_dbwh(dentry, -1);
35406 +
35407 +       return err;
35408 +}
35409 +
35410 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35411 +{
35412 +       int err;
35413 +       struct path h_path;
35414 +
35415 +       err = 0;
35416 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35417 +       if (IS_ERR(h_path.dentry))
35418 +               err = PTR_ERR(h_path.dentry);
35419 +       else {
35420 +               if (d_is_reg(h_path.dentry)) {
35421 +                       h_path.mnt = h_ppath->mnt;
35422 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35423 +               }
35424 +               dput(h_path.dentry);
35425 +       }
35426 +
35427 +       return err;
35428 +}
35429 +
35430 +/* ---------------------------------------------------------------------- */
35431 +/*
35432 + * initialize/clean whiteout for a branch
35433 + */
35434 +
35435 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35436 +                       const int isdir)
35437 +{
35438 +       int err;
35439 +       struct inode *delegated;
35440 +
35441 +       if (d_is_negative(whpath->dentry))
35442 +               return;
35443 +
35444 +       if (isdir)
35445 +               err = vfsub_rmdir(h_dir, whpath);
35446 +       else {
35447 +               delegated = NULL;
35448 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35449 +               if (unlikely(err == -EWOULDBLOCK)) {
35450 +                       pr_warn("cannot retry for NFSv4 delegation"
35451 +                               " for an internal unlink\n");
35452 +                       iput(delegated);
35453 +               }
35454 +       }
35455 +       if (unlikely(err))
35456 +               pr_warn("failed removing %pd (%d), ignored.\n",
35457 +                       whpath->dentry, err);
35458 +}
35459 +
35460 +static int test_linkable(struct dentry *h_root)
35461 +{
35462 +       struct inode *h_dir = d_inode(h_root);
35463 +
35464 +       if (h_dir->i_op->link)
35465 +               return 0;
35466 +
35467 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35468 +              h_root, au_sbtype(h_root->d_sb));
35469 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35470 +}
35471 +
35472 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35473 +static int au_whdir(struct inode *h_dir, struct path *path)
35474 +{
35475 +       int err;
35476 +
35477 +       err = -EEXIST;
35478 +       if (d_is_negative(path->dentry)) {
35479 +               int mode = 0700;
35480 +
35481 +               if (au_test_nfs(path->dentry->d_sb))
35482 +                       mode |= 0111;
35483 +               err = vfsub_mkdir(h_dir, path, mode);
35484 +       } else if (d_is_dir(path->dentry))
35485 +               err = 0;
35486 +       else
35487 +               pr_err("unknown %pd exists\n", path->dentry);
35488 +
35489 +       return err;
35490 +}
35491 +
35492 +struct au_wh_base {
35493 +       const struct qstr *name;
35494 +       struct dentry *dentry;
35495 +};
35496 +
35497 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35498 +                         struct path *h_path)
35499 +{
35500 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35501 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35502 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35503 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35504 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35505 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35506 +}
35507 +
35508 +/*
35509 + * returns tri-state,
35510 + * minus: error, caller should print the message
35511 + * zero: success
35512 + * plus: error, caller should NOT print the message
35513 + */
35514 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35515 +                               int do_plink, struct au_wh_base base[],
35516 +                               struct path *h_path)
35517 +{
35518 +       int err;
35519 +       struct inode *h_dir;
35520 +
35521 +       h_dir = d_inode(h_root);
35522 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35523 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35524 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35525 +       if (do_plink) {
35526 +               err = test_linkable(h_root);
35527 +               if (unlikely(err)) {
35528 +                       err = 1;
35529 +                       goto out;
35530 +               }
35531 +
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 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35539 +       err = au_whdir(h_dir, h_path);
35540 +       if (unlikely(err))
35541 +               goto out;
35542 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35543 +
35544 +out:
35545 +       return err;
35546 +}
35547 +
35548 +/*
35549 + * for the moment, aufs supports the branch filesystem which does not support
35550 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35551 + * copyup failed. finally, such filesystem will not be used as the writable
35552 + * branch.
35553 + *
35554 + * returns tri-state, see above.
35555 + */
35556 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35557 +                        int do_plink, struct au_wh_base base[],
35558 +                        struct path *h_path)
35559 +{
35560 +       int err;
35561 +       struct inode *h_dir;
35562 +
35563 +       WbrWhMustWriteLock(wbr);
35564 +
35565 +       err = test_linkable(h_root);
35566 +       if (unlikely(err)) {
35567 +               err = 1;
35568 +               goto out;
35569 +       }
35570 +
35571 +       /*
35572 +        * todo: should this create be done in /sbin/mount.aufs helper?
35573 +        */
35574 +       err = -EEXIST;
35575 +       h_dir = d_inode(h_root);
35576 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35577 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35578 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35579 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35580 +               err = 0;
35581 +       else
35582 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35583 +       if (unlikely(err))
35584 +               goto out;
35585 +
35586 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35587 +       if (do_plink) {
35588 +               err = au_whdir(h_dir, h_path);
35589 +               if (unlikely(err))
35590 +                       goto out;
35591 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35592 +       } else
35593 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35594 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35595 +
35596 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35597 +       err = au_whdir(h_dir, h_path);
35598 +       if (unlikely(err))
35599 +               goto out;
35600 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35601 +
35602 +out:
35603 +       return err;
35604 +}
35605 +
35606 +/*
35607 + * initialize the whiteout base file/dir for @br.
35608 + */
35609 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35610 +{
35611 +       int err, i;
35612 +       const unsigned char do_plink
35613 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35614 +       struct inode *h_dir;
35615 +       struct path path = br->br_path;
35616 +       struct dentry *h_root = path.dentry;
35617 +       struct au_wbr *wbr = br->br_wbr;
35618 +       static const struct qstr base_name[] = {
35619 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35620 +                                         sizeof(AUFS_BASE_NAME) - 1),
35621 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35622 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35623 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35624 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35625 +       };
35626 +       struct au_wh_base base[] = {
35627 +               [AuBrWh_BASE] = {
35628 +                       .name   = base_name + AuBrWh_BASE,
35629 +                       .dentry = NULL
35630 +               },
35631 +               [AuBrWh_PLINK] = {
35632 +                       .name   = base_name + AuBrWh_PLINK,
35633 +                       .dentry = NULL
35634 +               },
35635 +               [AuBrWh_ORPH] = {
35636 +                       .name   = base_name + AuBrWh_ORPH,
35637 +                       .dentry = NULL
35638 +               }
35639 +       };
35640 +
35641 +       if (wbr)
35642 +               WbrWhMustWriteLock(wbr);
35643 +
35644 +       for (i = 0; i < AuBrWh_Last; i++) {
35645 +               /* doubly whiteouted */
35646 +               struct dentry *d;
35647 +
35648 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35649 +               err = PTR_ERR(d);
35650 +               if (IS_ERR(d))
35651 +                       goto out;
35652 +
35653 +               base[i].dentry = d;
35654 +               AuDebugOn(wbr
35655 +                         && wbr->wbr_wh[i]
35656 +                         && wbr->wbr_wh[i] != base[i].dentry);
35657 +       }
35658 +
35659 +       if (wbr)
35660 +               for (i = 0; i < AuBrWh_Last; i++) {
35661 +                       dput(wbr->wbr_wh[i]);
35662 +                       wbr->wbr_wh[i] = NULL;
35663 +               }
35664 +
35665 +       err = 0;
35666 +       if (!au_br_writable(br->br_perm)) {
35667 +               h_dir = d_inode(h_root);
35668 +               au_wh_init_ro(h_dir, base, &path);
35669 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35670 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35671 +               if (err > 0)
35672 +                       goto out;
35673 +               else if (err)
35674 +                       goto out_err;
35675 +       } else {
35676 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35677 +               if (err > 0)
35678 +                       goto out;
35679 +               else if (err)
35680 +                       goto out_err;
35681 +       }
35682 +       goto out; /* success */
35683 +
35684 +out_err:
35685 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35686 +              err, h_root, au_sbtype(h_root->d_sb));
35687 +out:
35688 +       for (i = 0; i < AuBrWh_Last; i++)
35689 +               dput(base[i].dentry);
35690 +       return err;
35691 +}
35692 +
35693 +/* ---------------------------------------------------------------------- */
35694 +/*
35695 + * whiteouts are all hard-linked usually.
35696 + * when its link count reaches a ceiling, we create a new whiteout base
35697 + * asynchronously.
35698 + */
35699 +
35700 +struct reinit_br_wh {
35701 +       struct super_block *sb;
35702 +       struct au_branch *br;
35703 +};
35704 +
35705 +static void reinit_br_wh(void *arg)
35706 +{
35707 +       int err;
35708 +       aufs_bindex_t bindex;
35709 +       struct path h_path;
35710 +       struct reinit_br_wh *a = arg;
35711 +       struct au_wbr *wbr;
35712 +       struct inode *dir, *delegated;
35713 +       struct dentry *h_root;
35714 +       struct au_hinode *hdir;
35715 +
35716 +       err = 0;
35717 +       wbr = a->br->br_wbr;
35718 +       /* big aufs lock */
35719 +       si_noflush_write_lock(a->sb);
35720 +       if (!au_br_writable(a->br->br_perm))
35721 +               goto out;
35722 +       bindex = au_br_index(a->sb, a->br->br_id);
35723 +       if (unlikely(bindex < 0))
35724 +               goto out;
35725 +
35726 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35727 +       dir = d_inode(a->sb->s_root);
35728 +       hdir = au_hi(dir, bindex);
35729 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35730 +       AuDebugOn(h_root != au_br_dentry(a->br));
35731 +
35732 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35733 +       wbr_wh_write_lock(wbr);
35734 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35735 +                         h_root, a->br);
35736 +       if (!err) {
35737 +               h_path.dentry = wbr->wbr_whbase;
35738 +               h_path.mnt = au_br_mnt(a->br);
35739 +               delegated = NULL;
35740 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35741 +                                  /*force*/0);
35742 +               if (unlikely(err == -EWOULDBLOCK)) {
35743 +                       pr_warn("cannot retry for NFSv4 delegation"
35744 +                               " for an internal unlink\n");
35745 +                       iput(delegated);
35746 +               }
35747 +       } else {
35748 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35749 +               err = 0;
35750 +       }
35751 +       dput(wbr->wbr_whbase);
35752 +       wbr->wbr_whbase = NULL;
35753 +       if (!err)
35754 +               err = au_wh_init(a->br, a->sb);
35755 +       wbr_wh_write_unlock(wbr);
35756 +       au_hn_inode_unlock(hdir);
35757 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35758 +       if (!err)
35759 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35760 +
35761 +out:
35762 +       if (wbr)
35763 +               atomic_dec(&wbr->wbr_wh_running);
35764 +       au_lcnt_dec(&a->br->br_count);
35765 +       si_write_unlock(a->sb);
35766 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35767 +       au_kfree_rcu(a);
35768 +       if (unlikely(err))
35769 +               AuIOErr("err %d\n", err);
35770 +}
35771 +
35772 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35773 +{
35774 +       int do_dec, wkq_err;
35775 +       struct reinit_br_wh *arg;
35776 +
35777 +       do_dec = 1;
35778 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35779 +               goto out;
35780 +
35781 +       /* ignore ENOMEM */
35782 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35783 +       if (arg) {
35784 +               /*
35785 +                * dec(wh_running), kfree(arg) and dec(br_count)
35786 +                * in reinit function
35787 +                */
35788 +               arg->sb = sb;
35789 +               arg->br = br;
35790 +               au_lcnt_inc(&br->br_count);
35791 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35792 +               if (unlikely(wkq_err)) {
35793 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35794 +                       au_lcnt_dec(&br->br_count);
35795 +                       au_kfree_rcu(arg);
35796 +               }
35797 +               do_dec = 0;
35798 +       }
35799 +
35800 +out:
35801 +       if (do_dec)
35802 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35803 +}
35804 +
35805 +/* ---------------------------------------------------------------------- */
35806 +
35807 +/*
35808 + * create the whiteout @wh.
35809 + */
35810 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35811 +                            struct dentry *wh)
35812 +{
35813 +       int err;
35814 +       struct path h_path = {
35815 +               .dentry = wh
35816 +       };
35817 +       struct au_branch *br;
35818 +       struct au_wbr *wbr;
35819 +       struct dentry *h_parent;
35820 +       struct inode *h_dir, *delegated;
35821 +
35822 +       h_parent = wh->d_parent; /* dir inode is locked */
35823 +       h_dir = d_inode(h_parent);
35824 +       IMustLock(h_dir);
35825 +
35826 +       br = au_sbr(sb, bindex);
35827 +       h_path.mnt = au_br_mnt(br);
35828 +       wbr = br->br_wbr;
35829 +       wbr_wh_read_lock(wbr);
35830 +       if (wbr->wbr_whbase) {
35831 +               delegated = NULL;
35832 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35833 +               if (unlikely(err == -EWOULDBLOCK)) {
35834 +                       pr_warn("cannot retry for NFSv4 delegation"
35835 +                               " for an internal link\n");
35836 +                       iput(delegated);
35837 +               }
35838 +               if (!err || err != -EMLINK)
35839 +                       goto out;
35840 +
35841 +               /* link count full. re-initialize br_whbase. */
35842 +               kick_reinit_br_wh(sb, br);
35843 +       }
35844 +
35845 +       /* return this error in this context */
35846 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35847 +       if (!err)
35848 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35849 +
35850 +out:
35851 +       wbr_wh_read_unlock(wbr);
35852 +       return err;
35853 +}
35854 +
35855 +/* ---------------------------------------------------------------------- */
35856 +
35857 +/*
35858 + * create or remove the diropq.
35859 + */
35860 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35861 +                               unsigned int flags)
35862 +{
35863 +       struct dentry *opq_dentry;
35864 +       struct super_block *sb;
35865 +       struct au_branch *br;
35866 +       struct path h_path;
35867 +       int err;
35868 +
35869 +       sb = dentry->d_sb;
35870 +       br = au_sbr(sb, bindex);
35871 +       h_path.dentry = au_h_dptr(dentry, bindex);
35872 +       h_path.mnt = au_br_mnt(br);
35873 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35874 +       if (IS_ERR(opq_dentry))
35875 +               goto out;
35876 +
35877 +       if (au_ftest_diropq(flags, CREATE)) {
35878 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35879 +               if (!err) {
35880 +                       au_set_dbdiropq(dentry, bindex);
35881 +                       goto out; /* success */
35882 +               }
35883 +       } else {
35884 +               h_path.dentry = opq_dentry;
35885 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35886 +               if (!err)
35887 +                       au_set_dbdiropq(dentry, -1);
35888 +       }
35889 +       dput(opq_dentry);
35890 +       opq_dentry = ERR_PTR(err);
35891 +
35892 +out:
35893 +       return opq_dentry;
35894 +}
35895 +
35896 +struct do_diropq_args {
35897 +       struct dentry **errp;
35898 +       struct dentry *dentry;
35899 +       aufs_bindex_t bindex;
35900 +       unsigned int flags;
35901 +};
35902 +
35903 +static void call_do_diropq(void *args)
35904 +{
35905 +       struct do_diropq_args *a = args;
35906 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35907 +}
35908 +
35909 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35910 +                            unsigned int flags)
35911 +{
35912 +       struct dentry *diropq, *h_dentry;
35913 +       struct mnt_idmap *h_idmap;
35914 +
35915 +       h_idmap = au_sbr_idmap(dentry->d_sb, bindex);
35916 +       h_dentry = au_h_dptr(dentry, bindex);
35917 +       if (!au_test_h_perm_sio(h_idmap, d_inode(h_dentry),
35918 +                               MAY_EXEC | MAY_WRITE))
35919 +               diropq = do_diropq(dentry, bindex, flags);
35920 +       else {
35921 +               int wkq_err;
35922 +               struct do_diropq_args args = {
35923 +                       .errp           = &diropq,
35924 +                       .dentry         = dentry,
35925 +                       .bindex         = bindex,
35926 +                       .flags          = flags
35927 +               };
35928 +
35929 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35930 +               if (unlikely(wkq_err))
35931 +                       diropq = ERR_PTR(wkq_err);
35932 +       }
35933 +
35934 +       return diropq;
35935 +}
35936 +
35937 +/* ---------------------------------------------------------------------- */
35938 +
35939 +/*
35940 + * lookup whiteout dentry.
35941 + * @h_parent: lower parent dentry which must exist and be locked
35942 + * @base_name: name of dentry which will be whiteouted
35943 + * returns dentry for whiteout.
35944 + */
35945 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35946 +                         struct au_branch *br)
35947 +{
35948 +       int err;
35949 +       struct qstr wh_name;
35950 +       struct dentry *wh_dentry;
35951 +       struct path h_path;
35952 +
35953 +       err = au_wh_name_alloc(&wh_name, base_name);
35954 +       wh_dentry = ERR_PTR(err);
35955 +       if (!err) {
35956 +               h_path.dentry = h_parent;
35957 +               h_path.mnt = au_br_mnt(br);
35958 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35959 +               au_kfree_try_rcu(wh_name.name);
35960 +       }
35961 +       return wh_dentry;
35962 +}
35963 +
35964 +/*
35965 + * link/create a whiteout for @dentry on @bindex.
35966 + */
35967 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35968 +                           struct dentry *h_parent)
35969 +{
35970 +       struct dentry *wh_dentry;
35971 +       struct super_block *sb;
35972 +       int err;
35973 +
35974 +       sb = dentry->d_sb;
35975 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35976 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35977 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35978 +               if (!err) {
35979 +                       au_set_dbwh(dentry, bindex);
35980 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35981 +               } else {
35982 +                       dput(wh_dentry);
35983 +                       wh_dentry = ERR_PTR(err);
35984 +               }
35985 +       }
35986 +
35987 +       return wh_dentry;
35988 +}
35989 +
35990 +/* ---------------------------------------------------------------------- */
35991 +
35992 +/* Delete all whiteouts in this directory on branch bindex. */
35993 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35994 +                          aufs_bindex_t bindex)
35995 +{
35996 +       int err;
35997 +       unsigned long ul, n;
35998 +       struct qstr wh_name;
35999 +       char *p;
36000 +       struct hlist_head *head;
36001 +       struct au_vdir_wh *pos;
36002 +       struct au_vdir_destr *str;
36003 +
36004 +       err = -ENOMEM;
36005 +       p = (void *)__get_free_page(GFP_NOFS);
36006 +       wh_name.name = p;
36007 +       if (unlikely(!wh_name.name))
36008 +               goto out;
36009 +
36010 +       err = 0;
36011 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
36012 +       p += AUFS_WH_PFX_LEN;
36013 +       n = whlist->nh_num;
36014 +       head = whlist->nh_head;
36015 +       for (ul = 0; !err && ul < n; ul++, head++) {
36016 +               hlist_for_each_entry(pos, head, wh_hash) {
36017 +                       if (pos->wh_bindex != bindex)
36018 +                               continue;
36019 +
36020 +                       str = &pos->wh_str;
36021 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
36022 +                               memcpy(p, str->name, str->len);
36023 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
36024 +                               err = unlink_wh_name(h_path, &wh_name);
36025 +                               if (!err)
36026 +                                       continue;
36027 +                               break;
36028 +                       }
36029 +                       AuIOErr("whiteout name too long %.*s\n",
36030 +                               str->len, str->name);
36031 +                       err = -EIO;
36032 +                       break;
36033 +               }
36034 +       }
36035 +       free_page((unsigned long)wh_name.name);
36036 +
36037 +out:
36038 +       return err;
36039 +}
36040 +
36041 +struct del_wh_children_args {
36042 +       int *errp;
36043 +       struct path *h_path;
36044 +       struct au_nhash *whlist;
36045 +       aufs_bindex_t bindex;
36046 +};
36047 +
36048 +static void call_del_wh_children(void *args)
36049 +{
36050 +       struct del_wh_children_args *a = args;
36051 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
36052 +}
36053 +
36054 +/* ---------------------------------------------------------------------- */
36055 +
36056 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
36057 +{
36058 +       struct au_whtmp_rmdir *whtmp;
36059 +       int err;
36060 +       unsigned int rdhash;
36061 +
36062 +       SiMustAnyLock(sb);
36063 +
36064 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
36065 +       if (unlikely(!whtmp)) {
36066 +               whtmp = ERR_PTR(-ENOMEM);
36067 +               goto out;
36068 +       }
36069 +
36070 +       /* no estimation for dir size */
36071 +       rdhash = au_sbi(sb)->si_rdhash;
36072 +       if (!rdhash)
36073 +               rdhash = AUFS_RDHASH_DEF;
36074 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
36075 +       if (unlikely(err)) {
36076 +               au_kfree_rcu(whtmp);
36077 +               whtmp = ERR_PTR(err);
36078 +       }
36079 +
36080 +out:
36081 +       return whtmp;
36082 +}
36083 +
36084 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36085 +{
36086 +       if (whtmp->br)
36087 +               au_lcnt_dec(&whtmp->br->br_count);
36088 +       dput(whtmp->wh_dentry);
36089 +       iput(whtmp->dir);
36090 +       au_nhash_wh_free(&whtmp->whlist);
36091 +       au_kfree_rcu(whtmp);
36092 +}
36093 +
36094 +/*
36095 + * rmdir the whiteouted temporary named dir @h_dentry.
36096 + * @whlist: whiteouted children.
36097 + */
36098 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36099 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36100 +{
36101 +       int err;
36102 +       unsigned int h_nlink;
36103 +       struct path wh_path;
36104 +       struct inode *wh_inode, *h_dir;
36105 +       struct au_branch *br;
36106 +       struct mnt_idmap *h_idmap;
36107 +
36108 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36109 +       IMustLock(h_dir);
36110 +
36111 +       br = au_sbr(dir->i_sb, bindex);
36112 +       wh_path.dentry = wh_dentry;
36113 +       wh_path.mnt = au_br_mnt(br);
36114 +       h_idmap = au_br_idmap(br);
36115 +       wh_inode = d_inode(wh_dentry);
36116 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36117 +
36118 +       /*
36119 +        * someone else might change some whiteouts while we were sleeping.
36120 +        * it means this whlist may have an obsoleted entry.
36121 +        */
36122 +       if (!au_test_h_perm_sio(h_idmap, wh_inode, MAY_EXEC | MAY_WRITE))
36123 +               err = del_wh_children(&wh_path, whlist, bindex);
36124 +       else {
36125 +               int wkq_err;
36126 +               struct del_wh_children_args args = {
36127 +                       .errp           = &err,
36128 +                       .h_path         = &wh_path,
36129 +                       .whlist         = whlist,
36130 +                       .bindex         = bindex
36131 +               };
36132 +
36133 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36134 +               if (unlikely(wkq_err))
36135 +                       err = wkq_err;
36136 +       }
36137 +       inode_unlock(wh_inode);
36138 +
36139 +       if (!err) {
36140 +               h_nlink = h_dir->i_nlink;
36141 +               err = vfsub_rmdir(h_dir, &wh_path);
36142 +               /* some fs doesn't change the parent nlink in some cases */
36143 +               h_nlink -= h_dir->i_nlink;
36144 +       }
36145 +
36146 +       if (!err) {
36147 +               if (au_ibtop(dir) == bindex) {
36148 +                       /* todo: dir->i_mutex is necessary */
36149 +                       au_cpup_attr_timesizes(dir);
36150 +                       if (h_nlink)
36151 +                               vfsub_drop_nlink(dir);
36152 +               }
36153 +               return 0; /* success */
36154 +       }
36155 +
36156 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36157 +       return err;
36158 +}
36159 +
36160 +static void call_rmdir_whtmp(void *args)
36161 +{
36162 +       int err;
36163 +       aufs_bindex_t bindex;
36164 +       struct au_whtmp_rmdir *a = args;
36165 +       struct super_block *sb;
36166 +       struct dentry *h_parent;
36167 +       struct inode *h_dir;
36168 +       struct au_hinode *hdir;
36169 +
36170 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36171 +       /* inode_lock(a->dir); */
36172 +       err = -EROFS;
36173 +       sb = a->dir->i_sb;
36174 +       si_read_lock(sb, !AuLock_FLUSH);
36175 +       if (!au_br_writable(a->br->br_perm))
36176 +               goto out;
36177 +       bindex = au_br_index(sb, a->br->br_id);
36178 +       if (unlikely(bindex < 0))
36179 +               goto out;
36180 +
36181 +       err = -EIO;
36182 +       ii_write_lock_parent(a->dir);
36183 +       h_parent = dget_parent(a->wh_dentry);
36184 +       h_dir = d_inode(h_parent);
36185 +       hdir = au_hi(a->dir, bindex);
36186 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36187 +       if (unlikely(err))
36188 +               goto out_mnt;
36189 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36190 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36191 +                         a->br);
36192 +       if (!err)
36193 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36194 +       au_hn_inode_unlock(hdir);
36195 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36196 +
36197 +out_mnt:
36198 +       dput(h_parent);
36199 +       ii_write_unlock(a->dir);
36200 +out:
36201 +       /* inode_unlock(a->dir); */
36202 +       au_whtmp_rmdir_free(a);
36203 +       si_read_unlock(sb);
36204 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36205 +       if (unlikely(err))
36206 +               AuIOErr("err %d\n", err);
36207 +}
36208 +
36209 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36210 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36211 +{
36212 +       int wkq_err;
36213 +       struct super_block *sb;
36214 +
36215 +       IMustLock(dir);
36216 +
36217 +       /* all post-process will be done in do_rmdir_whtmp(). */
36218 +       sb = dir->i_sb;
36219 +       args->dir = au_igrab(dir);
36220 +       args->br = au_sbr(sb, bindex);
36221 +       au_lcnt_inc(&args->br->br_count);
36222 +       args->wh_dentry = dget(wh_dentry);
36223 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36224 +       if (unlikely(wkq_err)) {
36225 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36226 +               au_whtmp_rmdir_free(args);
36227 +       }
36228 +}
36229 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36230 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36231 +++ linux/fs/aufs/whout.h       2023-10-31 09:31:04.199880750 +0100
36232 @@ -0,0 +1,87 @@
36233 +/* SPDX-License-Identifier: GPL-2.0 */
36234 +/*
36235 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36236 + *
36237 + * This program is free software; you can redistribute it and/or modify
36238 + * it under the terms of the GNU General Public License as published by
36239 + * the Free Software Foundation; either version 2 of the License, or
36240 + * (at your option) any later version.
36241 + *
36242 + * This program is distributed in the hope that it will be useful,
36243 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36244 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36245 + * GNU General Public License for more details.
36246 + *
36247 + * You should have received a copy of the GNU General Public License
36248 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36249 + */
36250 +
36251 +/*
36252 + * whiteout for logical deletion and opaque directory
36253 + */
36254 +
36255 +#ifndef __AUFS_WHOUT_H__
36256 +#define __AUFS_WHOUT_H__
36257 +
36258 +#ifdef __KERNEL__
36259 +
36260 +#include "dir.h"
36261 +
36262 +/* whout.c */
36263 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36264 +int au_wh_test(struct mnt_idmap *h_idmap, struct path *h_ppath,
36265 +              struct qstr *wh_name, int try_sio);
36266 +int au_diropq_test(struct mnt_idmap *h_idmap, struct path *h_path);
36267 +struct au_branch;
36268 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36269 +                            struct qstr *prefix);
36270 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36271 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36272 +                       struct dentry *dentry);
36273 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36274 +
36275 +/* diropq flags */
36276 +#define AuDiropq_CREATE        1
36277 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36278 +#define au_fset_diropq(flags, name) \
36279 +       do { (flags) |= AuDiropq_##name; } while (0)
36280 +#define au_fclr_diropq(flags, name) \
36281 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36282 +
36283 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36284 +                            unsigned int flags);
36285 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36286 +                         struct au_branch *br);
36287 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36288 +                           struct dentry *h_parent);
36289 +
36290 +/* real rmdir for the whiteout-ed dir */
36291 +struct au_whtmp_rmdir {
36292 +       struct inode *dir;
36293 +       struct au_branch *br;
36294 +       struct dentry *wh_dentry;
36295 +       struct au_nhash whlist;
36296 +};
36297 +
36298 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36299 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36300 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36301 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36302 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36303 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36304 +
36305 +/* ---------------------------------------------------------------------- */
36306 +
36307 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36308 +                                             aufs_bindex_t bindex)
36309 +{
36310 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36311 +}
36312 +
36313 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36314 +{
36315 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36316 +}
36317 +
36318 +#endif /* __KERNEL__ */
36319 +#endif /* __AUFS_WHOUT_H__ */
36320 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36321 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36322 +++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
36323 @@ -0,0 +1,372 @@
36324 +// SPDX-License-Identifier: GPL-2.0
36325 +/*
36326 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36327 + *
36328 + * This program is free software; you can redistribute it and/or modify
36329 + * it under the terms of the GNU General Public License as published by
36330 + * the Free Software Foundation; either version 2 of the License, or
36331 + * (at your option) any later version.
36332 + *
36333 + * This program is distributed in the hope that it will be useful,
36334 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36335 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36336 + * GNU General Public License for more details.
36337 + *
36338 + * You should have received a copy of the GNU General Public License
36339 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36340 + */
36341 +
36342 +/*
36343 + * workqueue for asynchronous/super-io operations
36344 + * todo: try new credential scheme
36345 + */
36346 +
36347 +#include <linux/module.h>
36348 +#include "aufs.h"
36349 +
36350 +/* internal workqueue named AUFS_WKQ_NAME */
36351 +
36352 +static struct workqueue_struct *au_wkq;
36353 +
36354 +struct au_wkinfo {
36355 +       struct work_struct wk;
36356 +       struct kobject *kobj;
36357 +
36358 +       unsigned int flags; /* see wkq.h */
36359 +
36360 +       au_wkq_func_t func;
36361 +       void *args;
36362 +
36363 +#ifdef CONFIG_LOCKDEP
36364 +       int dont_check;
36365 +       struct held_lock **hlock;
36366 +#endif
36367 +
36368 +       struct completion *comp;
36369 +};
36370 +
36371 +/* ---------------------------------------------------------------------- */
36372 +/*
36373 + * Aufs passes some operations to the workqueue such as the internal copyup.
36374 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36375 + * job run by workqueue depends upon the locks acquired in the other task.
36376 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36377 + * information too. And the job in the workqueue restores the info in order to
36378 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36379 + * correctly and expectedly.
36380 + */
36381 +
36382 +#ifndef CONFIG_LOCKDEP
36383 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36384 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36385 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36386 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36387 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36388 +#else
36389 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36390 +{
36391 +       wkinfo->hlock = NULL;
36392 +       wkinfo->dont_check = 0;
36393 +}
36394 +
36395 +/*
36396 + * 1: matched
36397 + * 0: unmatched
36398 + */
36399 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36400 +{
36401 +       static DEFINE_SPINLOCK(spin);
36402 +       static struct {
36403 +               char *name;
36404 +               struct lock_class_key *key;
36405 +       } a[] = {
36406 +               { .name = "&sbinfo->si_rwsem" },
36407 +               { .name = "&finfo->fi_rwsem" },
36408 +               { .name = "&dinfo->di_rwsem" },
36409 +               { .name = "&iinfo->ii_rwsem" }
36410 +       };
36411 +       static int set;
36412 +       int i;
36413 +
36414 +       /* lockless read from 'set.' see below */
36415 +       if (set == ARRAY_SIZE(a)) {
36416 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36417 +                       if (a[i].key == key)
36418 +                               goto match;
36419 +               goto unmatch;
36420 +       }
36421 +
36422 +       spin_lock(&spin);
36423 +       if (set)
36424 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36425 +                       if (a[i].key == key) {
36426 +                               spin_unlock(&spin);
36427 +                               goto match;
36428 +                       }
36429 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36430 +               if (a[i].key) {
36431 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36432 +                               spin_unlock(&spin);
36433 +                               goto match;
36434 +                       } else
36435 +                               continue;
36436 +               }
36437 +               if (strstr(a[i].name, name)) {
36438 +                       /*
36439 +                        * the order of these three lines is important for the
36440 +                        * lockless read above.
36441 +                        */
36442 +                       a[i].key = key;
36443 +                       spin_unlock(&spin);
36444 +                       set++;
36445 +                       /* AuDbg("%d, %s\n", set, name); */
36446 +                       goto match;
36447 +               }
36448 +       }
36449 +       spin_unlock(&spin);
36450 +       goto unmatch;
36451 +
36452 +match:
36453 +       return 1;
36454 +unmatch:
36455 +       return 0;
36456 +}
36457 +
36458 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36459 +{
36460 +       int err, n;
36461 +       struct task_struct *curr;
36462 +       struct held_lock **hl, *held_locks, *p;
36463 +
36464 +       err = 0;
36465 +       curr = current;
36466 +       wkinfo->dont_check = lockdep_recursing(curr);
36467 +       if (wkinfo->dont_check)
36468 +               goto out;
36469 +       n = curr->lockdep_depth;
36470 +       if (!n)
36471 +               goto out;
36472 +
36473 +       err = -ENOMEM;
36474 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36475 +       if (unlikely(!wkinfo->hlock))
36476 +               goto out;
36477 +
36478 +       err = 0;
36479 +#if 0 /* left for debugging */
36480 +       if (0 && au_debug_test())
36481 +               lockdep_print_held_locks(curr);
36482 +#endif
36483 +       held_locks = curr->held_locks;
36484 +       hl = wkinfo->hlock;
36485 +       while (n--) {
36486 +               p = held_locks++;
36487 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36488 +                       *hl++ = p;
36489 +       }
36490 +       *hl = NULL;
36491 +
36492 +out:
36493 +       return err;
36494 +}
36495 +
36496 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36497 +{
36498 +       au_kfree_try_rcu(wkinfo->hlock);
36499 +}
36500 +
36501 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36502 +{
36503 +       struct held_lock *p, **hl = wkinfo->hlock;
36504 +       int subclass;
36505 +
36506 +       if (wkinfo->dont_check)
36507 +               lockdep_off();
36508 +       if (!hl)
36509 +               return;
36510 +       while ((p = *hl++)) { /* assignment */
36511 +               subclass = lockdep_hlock_class(p)->subclass;
36512 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36513 +               if (p->read)
36514 +                       rwsem_acquire_read(p->instance, subclass, 0,
36515 +                                          /*p->acquire_ip*/_RET_IP_);
36516 +               else
36517 +                       rwsem_acquire(p->instance, subclass, 0,
36518 +                                     /*p->acquire_ip*/_RET_IP_);
36519 +       }
36520 +}
36521 +
36522 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36523 +{
36524 +       struct held_lock *p, **hl = wkinfo->hlock;
36525 +
36526 +       if (wkinfo->dont_check)
36527 +               lockdep_on();
36528 +       if (!hl)
36529 +               return;
36530 +       while ((p = *hl++)) /* assignment */
36531 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36532 +}
36533 +#endif
36534 +
36535 +static void wkq_func(struct work_struct *wk)
36536 +{
36537 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36538 +
36539 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36540 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36541 +
36542 +       au_wkq_lockdep_pre(wkinfo);
36543 +       wkinfo->func(wkinfo->args);
36544 +       au_wkq_lockdep_post(wkinfo);
36545 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36546 +               complete(wkinfo->comp);
36547 +       else {
36548 +               kobject_put(wkinfo->kobj);
36549 +               module_put(THIS_MODULE); /* todo: ?? */
36550 +               au_kfree_rcu(wkinfo);
36551 +       }
36552 +}
36553 +
36554 +/*
36555 + * Since struct completion is large, try allocating it dynamically.
36556 + */
36557 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36558 +
36559 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36560 +{
36561 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36562 +       if (*comp) {
36563 +               init_completion(*comp);
36564 +               wkinfo->comp = *comp;
36565 +               return 0;
36566 +       }
36567 +       return -ENOMEM;
36568 +}
36569 +
36570 +static void au_wkq_comp_free(struct completion *comp)
36571 +{
36572 +       au_kfree_rcu(comp);
36573 +}
36574 +
36575 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36576 +{
36577 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36578 +               if (au_wkq_test()) {
36579 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36580 +                               " due to a dead dir by UDBA,"
36581 +                               " or async xino write?\n");
36582 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36583 +               }
36584 +       } else
36585 +               au_dbg_verify_kthread();
36586 +
36587 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36588 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36589 +               queue_work(au_wkq, &wkinfo->wk);
36590 +       } else {
36591 +               INIT_WORK(&wkinfo->wk, wkq_func);
36592 +               schedule_work(&wkinfo->wk);
36593 +       }
36594 +}
36595 +
36596 +/*
36597 + * Be careful. It is easy to make deadlock happen.
36598 + * processA: lock, wkq and wait
36599 + * processB: wkq and wait, lock in wkq
36600 + * --> deadlock
36601 + */
36602 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36603 +{
36604 +       int err;
36605 +       AuWkqCompDeclare(comp);
36606 +       struct au_wkinfo wkinfo = {
36607 +               .flags  = flags,
36608 +               .func   = func,
36609 +               .args   = args
36610 +       };
36611 +
36612 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36613 +       if (unlikely(err))
36614 +               goto out;
36615 +       err = au_wkq_lockdep_alloc(&wkinfo);
36616 +       if (unlikely(err))
36617 +               goto out_comp;
36618 +       if (!err) {
36619 +               au_wkq_run(&wkinfo);
36620 +               /* no timeout, no interrupt */
36621 +               wait_for_completion(wkinfo.comp);
36622 +       }
36623 +       au_wkq_lockdep_free(&wkinfo);
36624 +
36625 +out_comp:
36626 +       au_wkq_comp_free(comp);
36627 +out:
36628 +       destroy_work_on_stack(&wkinfo.wk);
36629 +       return err;
36630 +}
36631 +
36632 +/*
36633 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36634 + * problem in a concurrent umounting.
36635 + */
36636 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36637 +                 unsigned int flags)
36638 +{
36639 +       int err;
36640 +       struct au_wkinfo *wkinfo;
36641 +
36642 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36643 +
36644 +       /*
36645 +        * wkq_func() must free this wkinfo.
36646 +        * it highly depends upon the implementation of workqueue.
36647 +        */
36648 +       err = 0;
36649 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36650 +       if (wkinfo) {
36651 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36652 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36653 +               wkinfo->func = func;
36654 +               wkinfo->args = args;
36655 +               wkinfo->comp = NULL;
36656 +               au_wkq_lockdep_init(wkinfo);
36657 +               kobject_get(wkinfo->kobj);
36658 +               __module_get(THIS_MODULE); /* todo: ?? */
36659 +
36660 +               au_wkq_run(wkinfo);
36661 +       } else {
36662 +               err = -ENOMEM;
36663 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36664 +       }
36665 +
36666 +       return err;
36667 +}
36668 +
36669 +/* ---------------------------------------------------------------------- */
36670 +
36671 +void au_nwt_init(struct au_nowait_tasks *nwt)
36672 +{
36673 +       atomic_set(&nwt->nw_len, 0);
36674 +       /* smp_mb(); */ /* atomic_set */
36675 +       init_waitqueue_head(&nwt->nw_wq);
36676 +}
36677 +
36678 +void au_wkq_fin(void)
36679 +{
36680 +       destroy_workqueue(au_wkq);
36681 +}
36682 +
36683 +int __init au_wkq_init(void)
36684 +{
36685 +       int err;
36686 +
36687 +       err = 0;
36688 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36689 +       if (IS_ERR(au_wkq))
36690 +               err = PTR_ERR(au_wkq);
36691 +       else if (!au_wkq)
36692 +               err = -ENOMEM;
36693 +
36694 +       return err;
36695 +}
36696 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36697 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36698 +++ linux/fs/aufs/wkq.h 2024-03-10 23:40:47.086476782 +0100
36699 @@ -0,0 +1,89 @@
36700 +/* SPDX-License-Identifier: GPL-2.0 */
36701 +/*
36702 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36703 + *
36704 + * This program is free software; you can redistribute it and/or modify
36705 + * it under the terms of the GNU General Public License as published by
36706 + * the Free Software Foundation; either version 2 of the License, or
36707 + * (at your option) any later version.
36708 + *
36709 + * This program is distributed in the hope that it will be useful,
36710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36712 + * GNU General Public License for more details.
36713 + *
36714 + * You should have received a copy of the GNU General Public License
36715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36716 + */
36717 +
36718 +/*
36719 + * workqueue for asynchronous/super-io operations
36720 + * todo: try new credentials management scheme
36721 + */
36722 +
36723 +#ifndef __AUFS_WKQ_H__
36724 +#define __AUFS_WKQ_H__
36725 +
36726 +#ifdef __KERNEL__
36727 +
36728 +#include <linux/wait.h>
36729 +
36730 +struct super_block;
36731 +
36732 +/* ---------------------------------------------------------------------- */
36733 +
36734 +/*
36735 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36736 + */
36737 +struct au_nowait_tasks {
36738 +       atomic_t                nw_len;
36739 +       wait_queue_head_t       nw_wq;
36740 +};
36741 +
36742 +/* ---------------------------------------------------------------------- */
36743 +
36744 +typedef void (*au_wkq_func_t)(void *args);
36745 +
36746 +/* wkq flags */
36747 +#define AuWkq_WAIT     BIT(0)
36748 +#define AuWkq_NEST     BIT(1)
36749 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36750 +#define au_fset_wkq(flags, name) \
36751 +       do { (flags) |= AuWkq_##name; } while (0)
36752 +#define au_fclr_wkq(flags, name) \
36753 +       do { (flags) &= ~AuWkq_##name; } while (0)
36754 +
36755 +/* wkq.c */
36756 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36757 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36758 +                 unsigned int flags);
36759 +void au_nwt_init(struct au_nowait_tasks *nwt);
36760 +int __init au_wkq_init(void);
36761 +void au_wkq_fin(void);
36762 +
36763 +/* ---------------------------------------------------------------------- */
36764 +
36765 +static inline int au_wkq_test(void)
36766 +{
36767 +       return current->flags & PF_WQ_WORKER;
36768 +}
36769 +
36770 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36771 +{
36772 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36773 +}
36774 +
36775 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36776 +{
36777 +       if (atomic_dec_and_test(&nwt->nw_len))
36778 +               wake_up_all(&nwt->nw_wq);
36779 +}
36780 +
36781 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36782 +{
36783 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36784 +       return 0;
36785 +}
36786 +
36787 +#endif /* __KERNEL__ */
36788 +#endif /* __AUFS_WKQ_H__ */
36789 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36790 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36791 +++ linux/fs/aufs/xattr.c       2024-03-10 23:40:47.086476782 +0100
36792 @@ -0,0 +1,360 @@
36793 +// SPDX-License-Identifier: GPL-2.0
36794 +/*
36795 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36796 + *
36797 + * This program is free software; you can redistribute it and/or modify
36798 + * it under the terms of the GNU General Public License as published by
36799 + * the Free Software Foundation; either version 2 of the License, or
36800 + * (at your option) any later version.
36801 + *
36802 + * This program is distributed in the hope that it will be useful,
36803 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36804 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36805 + * GNU General Public License for more details.
36806 + *
36807 + * You should have received a copy of the GNU General Public License
36808 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36809 + */
36810 +
36811 +/*
36812 + * handling xattr functions
36813 + */
36814 +
36815 +#include <linux/fs.h>
36816 +#include <linux/xattr.h>
36817 +#include "aufs.h"
36818 +
36819 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36820 +{
36821 +       if (!ignore_flags)
36822 +               goto out;
36823 +       switch (err) {
36824 +       case -ENOMEM:
36825 +       case -EDQUOT:
36826 +               goto out;
36827 +       }
36828 +
36829 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36830 +               err = 0;
36831 +               goto out;
36832 +       }
36833 +
36834 +#define cmp(brattr, prefix) do {                                       \
36835 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36836 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36837 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36838 +                               err = 0;                                \
36839 +                       goto out;                                       \
36840 +               }                                                       \
36841 +       } while (0)
36842 +
36843 +       cmp(SEC, SECURITY);
36844 +       cmp(SYS, SYSTEM);
36845 +       cmp(TR, TRUSTED);
36846 +       cmp(USR, USER);
36847 +#undef cmp
36848 +
36849 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36850 +               err = 0;
36851 +
36852 +out:
36853 +       return err;
36854 +}
36855 +
36856 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36857 +
36858 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36859 +                           char *name, char **buf, unsigned int ignore_flags,
36860 +                           unsigned int verbose)
36861 +{
36862 +       int err, is_acl;
36863 +       ssize_t ssz;
36864 +       struct inode *h_idst;
36865 +       struct dentry *h_dst_dentry, *h_src_dentry;
36866 +       struct mnt_idmap *h_dst_idmap, *h_src_idmap;
36867 +       struct posix_acl *acl;
36868 +
36869 +       is_acl = !!is_posix_acl_xattr(name);
36870 +       h_src_idmap = mnt_idmap(h_src->mnt);
36871 +       h_src_dentry = h_src->dentry;
36872 +       if (is_acl) {
36873 +               acl = vfs_get_acl(h_src_idmap, h_src_dentry, name);
36874 +               AuDebugOn(!acl);
36875 +               if (IS_ERR(acl)) {
36876 +                       err = PTR_ERR(acl);
36877 +                       if (err == -ENODATA)
36878 +                               err = 0;
36879 +                       else if (err == -EOPNOTSUPP
36880 +                                && au_test_nfs_noacl(d_inode(h_src_dentry)))
36881 +                               err = 0;
36882 +                       else if (verbose || au_debug_test())
36883 +                               pr_err("%s, err %d\n", name, err);
36884 +                       goto out;
36885 +               }
36886 +       } else {
36887 +               ssz = vfs_getxattr_alloc(h_src_idmap, h_src_dentry, name, buf,
36888 +                                        0, GFP_NOFS);
36889 +               if (unlikely(ssz <= 0)) {
36890 +                       err = ssz;
36891 +                       if (err == -ENODATA)
36892 +                               err = 0;
36893 +                       else if (err == -EOPNOTSUPP
36894 +                                && (ignore_flags & au_xattr_out_of_list))
36895 +                                err = 0;
36896 +                       else if (err && (verbose || au_debug_test()))
36897 +                               pr_err("%s, err %d\n", name, err);
36898 +                       goto out;
36899 +               }
36900 +       }
36901 +
36902 +       /* unlock it temporary */
36903 +       h_dst_idmap = mnt_idmap(h_dst->mnt);
36904 +       h_dst_dentry = h_dst->dentry;
36905 +       h_idst = d_inode(h_dst_dentry);
36906 +       inode_unlock(h_idst);
36907 +       if (is_acl) {
36908 +               err = vfsub_set_acl(h_dst_idmap, h_dst_dentry, name, acl);
36909 +               posix_acl_release(acl);
36910 +       } else
36911 +               err = vfsub_setxattr(h_dst_idmap, h_dst_dentry, name, *buf,
36912 +                                    ssz, /*flags*/0);
36913 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36914 +       if (unlikely(err)) {
36915 +               if (verbose || au_debug_test())
36916 +                       pr_err("%s, err %d\n", name, err);
36917 +               err = au_xattr_ignore(err, name, ignore_flags);
36918 +       }
36919 +
36920 +out:
36921 +       return err;
36922 +}
36923 +
36924 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36925 +                 unsigned int verbose)
36926 +{
36927 +       int err, unlocked;
36928 +       ssize_t ssz;
36929 +       struct dentry *h_dst_dentry, *h_src_dentry;
36930 +       struct inode *h_isrc, *h_idst;
36931 +       char *value, *p, *o, *e;
36932 +
36933 +       /* try stopping to update the source inode while we are referencing */
36934 +       /* there should not be the parent-child relationship between them */
36935 +       h_dst_dentry = h_dst->dentry;
36936 +       h_idst = d_inode(h_dst_dentry);
36937 +       h_src_dentry = h_src->dentry;
36938 +       h_isrc = d_inode(h_src_dentry);
36939 +       inode_unlock(h_idst);
36940 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36941 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36942 +       unlocked = 0;
36943 +
36944 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36945 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36946 +       err = ssz;
36947 +       if (unlikely(err < 0)) {
36948 +               AuTraceErr(err);
36949 +               if (err == -ENODATA
36950 +                   || err == -EOPNOTSUPP)
36951 +                       err = 0;        /* ignore */
36952 +               goto out;
36953 +       }
36954 +
36955 +       err = 0;
36956 +       p = NULL;
36957 +       o = NULL;
36958 +       if (ssz) {
36959 +               err = -ENOMEM;
36960 +               p = kmalloc(ssz, GFP_NOFS);
36961 +               o = p;
36962 +               if (unlikely(!p))
36963 +                       goto out;
36964 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36965 +       }
36966 +       inode_unlock_shared(h_isrc);
36967 +       unlocked = 1;
36968 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36969 +       if (unlikely(err < 0))
36970 +               goto out_free;
36971 +
36972 +       err = 0;
36973 +       e = p + ssz;
36974 +       value = NULL;
36975 +       ignore_flags |= au_xattr_out_of_list;
36976 +       while (!err && p < e) {
36977 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36978 +                                      verbose);
36979 +               p += strlen(p) + 1;
36980 +       }
36981 +       au_kfree_try_rcu(value);
36982 +
36983 +out_free:
36984 +       au_kfree_try_rcu(o);
36985 +out:
36986 +       if (!unlocked)
36987 +               inode_unlock_shared(h_isrc);
36988 +       AuTraceErr(err);
36989 +       return err;
36990 +}
36991 +
36992 +/* ---------------------------------------------------------------------- */
36993 +
36994 +static int au_smack_reentering(struct super_block *sb)
36995 +{
36996 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36997 +       /*
36998 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36999 +        * i_op->getxattr(). ouch.
37000 +        */
37001 +       return si_pid_test(sb);
37002 +#else
37003 +       return 0;
37004 +#endif
37005 +}
37006 +
37007 +enum {
37008 +       AU_XATTR_LIST,
37009 +       AU_XATTR_GET
37010 +};
37011 +
37012 +struct au_lgxattr {
37013 +       int type;
37014 +       union {
37015 +               struct {
37016 +                       char    *list;
37017 +                       size_t  size;
37018 +               } list;
37019 +               struct {
37020 +                       const char      *name;
37021 +                       void            *value;
37022 +                       size_t          size;
37023 +               } get;
37024 +       } u;
37025 +};
37026 +
37027 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
37028 +                         struct au_lgxattr *arg)
37029 +{
37030 +       ssize_t err;
37031 +       int reenter;
37032 +       struct path h_path;
37033 +       struct super_block *sb;
37034 +
37035 +       sb = dentry->d_sb;
37036 +       reenter = au_smack_reentering(sb);
37037 +       if (!reenter) {
37038 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
37039 +               if (unlikely(err))
37040 +                       goto out;
37041 +       }
37042 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
37043 +       if (unlikely(err))
37044 +               goto out_si;
37045 +       if (unlikely(!h_path.dentry))
37046 +               /* illegally overlapped or something */
37047 +               goto out_di; /* pretending success */
37048 +
37049 +       /* always topmost entry only */
37050 +       switch (arg->type) {
37051 +       case AU_XATTR_LIST:
37052 +               err = vfs_listxattr(h_path.dentry,
37053 +                                   arg->u.list.list, arg->u.list.size);
37054 +               break;
37055 +       case AU_XATTR_GET:
37056 +               AuDebugOn(d_is_negative(h_path.dentry));
37057 +               err = vfs_getxattr(mnt_idmap(h_path.mnt), h_path.dentry,
37058 +                                  arg->u.get.name, arg->u.get.value,
37059 +                                  arg->u.get.size);
37060 +               break;
37061 +       }
37062 +
37063 +out_di:
37064 +       if (!reenter)
37065 +               di_read_unlock(dentry, AuLock_IR);
37066 +out_si:
37067 +       if (!reenter)
37068 +               si_read_unlock(sb);
37069 +out:
37070 +       AuTraceErr(err);
37071 +       return err;
37072 +}
37073 +
37074 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37075 +{
37076 +       struct au_lgxattr arg = {
37077 +               .type = AU_XATTR_LIST,
37078 +               .u.list = {
37079 +                       .list   = list,
37080 +                       .size   = size
37081 +               },
37082 +       };
37083 +
37084 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37085 +}
37086 +
37087 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37088 +                          const char *name, void *value, size_t size)
37089 +{
37090 +       struct au_lgxattr arg = {
37091 +               .type = AU_XATTR_GET,
37092 +               .u.get = {
37093 +                       .name   = name,
37094 +                       .value  = value,
37095 +                       .size   = size
37096 +               },
37097 +       };
37098 +
37099 +       return au_lgxattr(dentry, inode, &arg);
37100 +}
37101 +
37102 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37103 +                      const char *name, const void *value, size_t size,
37104 +                      int flags)
37105 +{
37106 +       struct au_sxattr arg = {
37107 +               .type = AU_XATTR_SET,
37108 +               .u.set = {
37109 +                       .name   = name,
37110 +                       .value  = value,
37111 +                       .size   = size,
37112 +                       .flags  = flags
37113 +               },
37114 +       };
37115 +
37116 +       return au_sxattr(dentry, inode, &arg);
37117 +}
37118 +
37119 +/* ---------------------------------------------------------------------- */
37120 +
37121 +static int au_xattr_get(const struct xattr_handler *handler,
37122 +                       struct dentry *dentry, struct inode *inode,
37123 +                       const char *name, void *buffer, size_t size)
37124 +{
37125 +       return au_getxattr(dentry, inode, name, buffer, size);
37126 +}
37127 +
37128 +static int au_xattr_set(const struct xattr_handler *handler,
37129 +                       struct mnt_idmap *idmap,
37130 +                       struct dentry *dentry, struct inode *inode,
37131 +                       const char *name, const void *value, size_t size,
37132 +                       int flags)
37133 +{
37134 +       return au_setxattr(dentry, inode, name, value, size, flags);
37135 +}
37136 +
37137 +static const struct xattr_handler au_xattr_handler = {
37138 +       .name   = "",
37139 +       .prefix = "",
37140 +       .get    = au_xattr_get,
37141 +       .set    = au_xattr_set
37142 +};
37143 +
37144 +static const struct xattr_handler *au_xattr_handlers[] = {
37145 +       &au_xattr_handler, /* must be last */
37146 +       NULL
37147 +};
37148 +
37149 +void au_xattr_init(struct super_block *sb)
37150 +{
37151 +       sb->s_xattr = au_xattr_handlers;
37152 +}
37153 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37154 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37155 +++ linux/fs/aufs/xino.c        2023-10-31 09:31:04.203214083 +0100
37156 @@ -0,0 +1,1926 @@
37157 +// SPDX-License-Identifier: GPL-2.0
37158 +/*
37159 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37160 + *
37161 + * This program is free software; you can redistribute it and/or modify
37162 + * it under the terms of the GNU General Public License as published by
37163 + * the Free Software Foundation; either version 2 of the License, or
37164 + * (at your option) any later version.
37165 + *
37166 + * This program is distributed in the hope that it will be useful,
37167 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37168 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37169 + * GNU General Public License for more details.
37170 + *
37171 + * You should have received a copy of the GNU General Public License
37172 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37173 + */
37174 +
37175 +/*
37176 + * external inode number translation table and bitmap
37177 + *
37178 + * things to consider
37179 + * - the lifetime
37180 + *   + au_xino object
37181 + *   + XINO files (xino, xib, xigen)
37182 + *   + dynamic debugfs entries (xiN)
37183 + *   + static debugfs entries (xib, xigen)
37184 + *   + static sysfs entry (xi_path)
37185 + * - several entry points to handle them.
37186 + *   + mount(2) without xino option (default)
37187 + *   + mount(2) with xino option
37188 + *   + mount(2) with noxino option
37189 + *   + umount(2)
37190 + *   + remount with add/del branches
37191 + *   + remount with xino/noxino options
37192 + */
37193 +
37194 +#include <linux/seq_file.h>
37195 +#include <linux/statfs.h>
37196 +#include "aufs.h"
37197 +
37198 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37199 +                                    aufs_bindex_t bbot,
37200 +                                    struct super_block *h_sb)
37201 +{
37202 +       /* todo: try binary-search if the branches are many */
37203 +       for (; btop <= bbot; btop++)
37204 +               if (h_sb == au_sbr_sb(sb, btop))
37205 +                       return btop;
37206 +       return -1;
37207 +}
37208 +
37209 +/*
37210 + * find another branch who is on the same filesystem of the specified
37211 + * branch{@btgt}. search until @bbot.
37212 + */
37213 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37214 +                                 aufs_bindex_t bbot)
37215 +{
37216 +       aufs_bindex_t bindex;
37217 +       struct super_block *tgt_sb;
37218 +
37219 +       tgt_sb = au_sbr_sb(sb, btgt);
37220 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37221 +       if (bindex < 0)
37222 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37223 +
37224 +       return bindex;
37225 +}
37226 +
37227 +/* ---------------------------------------------------------------------- */
37228 +
37229 +/*
37230 + * stop unnecessary notify events at creating xino files
37231 + */
37232 +
37233 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37234 +{
37235 +       aufs_bindex_t bfound, bindex, bbot;
37236 +       struct dentry *parent;
37237 +       struct au_branch *br;
37238 +
37239 +       bfound = -1;
37240 +       parent = dentry->d_parent; /* safe d_parent access */
37241 +       bbot = au_sbbot(sb);
37242 +       for (bindex = 0; bindex <= bbot; bindex++) {
37243 +               br = au_sbr(sb, bindex);
37244 +               if (au_br_dentry(br) == parent) {
37245 +                       bfound = bindex;
37246 +                       break;
37247 +               }
37248 +       }
37249 +
37250 +       AuDbg("bfound b%d\n", bfound);
37251 +       return bfound;
37252 +}
37253 +
37254 +struct au_xino_lock_dir {
37255 +       struct au_hinode *hdir;
37256 +       struct dentry *parent;
37257 +       struct inode *dir;
37258 +};
37259 +
37260 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37261 +                                         unsigned int lsc)
37262 +{
37263 +       struct dentry *parent;
37264 +       struct inode *dir;
37265 +
37266 +       parent = dget_parent(dentry);
37267 +       dir = d_inode(parent);
37268 +       inode_lock_nested(dir, lsc);
37269 +#if 0 /* it should not happen */
37270 +       spin_lock(&dentry->d_lock);
37271 +       if (unlikely(dentry->d_parent != parent)) {
37272 +               spin_unlock(&dentry->d_lock);
37273 +               inode_unlock(dir);
37274 +               dput(parent);
37275 +               parent = NULL;
37276 +               goto out;
37277 +       }
37278 +       spin_unlock(&dentry->d_lock);
37279 +
37280 +out:
37281 +#endif
37282 +       return parent;
37283 +}
37284 +
37285 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37286 +                            struct au_xino_lock_dir *ldir)
37287 +{
37288 +       aufs_bindex_t bindex;
37289 +
37290 +       ldir->hdir = NULL;
37291 +       bindex = au_xi_root(sb, xipath->dentry);
37292 +       if (bindex >= 0) {
37293 +               /* rw branch root */
37294 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37295 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37296 +       } else {
37297 +               /* other */
37298 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37299 +                                                  AuLsc_I_PARENT);
37300 +               ldir->dir = d_inode(ldir->parent);
37301 +       }
37302 +}
37303 +
37304 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37305 +{
37306 +       if (ldir->hdir)
37307 +               au_hn_inode_unlock(ldir->hdir);
37308 +       else {
37309 +               inode_unlock(ldir->dir);
37310 +               dput(ldir->parent);
37311 +       }
37312 +}
37313 +
37314 +/* ---------------------------------------------------------------------- */
37315 +
37316 +/*
37317 + * create and set a new xino file
37318 + */
37319 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37320 +                           int wbrtop)
37321 +{
37322 +       struct file *file;
37323 +       struct dentry *h_parent, *d;
37324 +       struct inode *h_dir, *inode;
37325 +       int err;
37326 +       static DEFINE_MUTEX(mtx);
37327 +
37328 +       /*
37329 +        * at mount-time, and the xino file is the default path,
37330 +        * hnotify is disabled so we have no notify events to ignore.
37331 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37332 +        */
37333 +       if (!wbrtop)
37334 +               mutex_lock(&mtx);
37335 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37336 +                              /* | __FMODE_NONOTIFY */,
37337 +                              0666);
37338 +       if (IS_ERR(file)) {
37339 +               if (!wbrtop)
37340 +                       mutex_unlock(&mtx);
37341 +               if (!silent)
37342 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37343 +               return file;
37344 +       }
37345 +
37346 +       /* keep file count */
37347 +       err = 0;
37348 +       d = file->f_path.dentry;
37349 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37350 +       if (!wbrtop)
37351 +               mutex_unlock(&mtx);
37352 +       /* mnt_want_write() is unnecessary here */
37353 +       h_dir = d_inode(h_parent);
37354 +       inode = file_inode(file);
37355 +       /* no delegation since it is just created */
37356 +       if (inode->i_nlink)
37357 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37358 +                                  /*force*/0);
37359 +       inode_unlock(h_dir);
37360 +       dput(h_parent);
37361 +       if (unlikely(err)) {
37362 +               if (!silent)
37363 +                       pr_err("unlink %s(%d)\n", fpath, err);
37364 +               goto out;
37365 +       }
37366 +
37367 +       err = -EINVAL;
37368 +       if (unlikely(sb && sb == d->d_sb)) {
37369 +               if (!silent)
37370 +                       pr_err("%s must be outside\n", fpath);
37371 +               goto out;
37372 +       }
37373 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37374 +               if (!silent)
37375 +                       pr_err("xino doesn't support %s(%s)\n",
37376 +                              fpath, au_sbtype(d->d_sb));
37377 +               goto out;
37378 +       }
37379 +       return file; /* success */
37380 +
37381 +out:
37382 +       fput(file);
37383 +       file = ERR_PTR(err);
37384 +       return file;
37385 +}
37386 +
37387 +/*
37388 + * create a new xinofile at the same place/path as @base.
37389 + */
37390 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37391 +                            struct file *copy_src)
37392 +{
37393 +       struct file *file;
37394 +       struct dentry *dentry;
37395 +       struct inode *dir, *delegated;
37396 +       struct qstr *name;
37397 +       struct path ppath, path;
37398 +       int err, do_unlock;
37399 +       struct au_xino_lock_dir ldir;
37400 +
37401 +       do_unlock = 1;
37402 +       au_xino_lock_dir(sb, base, &ldir);
37403 +       dentry = base->dentry;
37404 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37405 +       ppath.mnt = base->mnt;
37406 +       dir = d_inode(ppath.dentry);
37407 +       IMustLock(dir);
37408 +
37409 +       name = &dentry->d_name;
37410 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37411 +       if (IS_ERR(path.dentry)) {
37412 +               file = (void *)path.dentry;
37413 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37414 +               goto out;
37415 +       }
37416 +
37417 +       /* no need to mnt_want_write() since we call dentry_open() later */
37418 +       err = vfs_create(mnt_idmap(base->mnt), dir, path.dentry, 0666, NULL);
37419 +       if (unlikely(err)) {
37420 +               file = ERR_PTR(err);
37421 +               pr_err("%pd create err %d\n", dentry, err);
37422 +               goto out_dput;
37423 +       }
37424 +
37425 +       path.mnt = base->mnt;
37426 +       file = vfsub_dentry_open(&path,
37427 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37428 +                                /* | __FMODE_NONOTIFY */);
37429 +       if (IS_ERR(file)) {
37430 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37431 +               goto out_dput;
37432 +       }
37433 +
37434 +       delegated = NULL;
37435 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37436 +       au_xino_unlock_dir(&ldir);
37437 +       do_unlock = 0;
37438 +       if (unlikely(err == -EWOULDBLOCK)) {
37439 +               pr_warn("cannot retry for NFSv4 delegation"
37440 +                       " for an internal unlink\n");
37441 +               iput(delegated);
37442 +       }
37443 +       if (unlikely(err)) {
37444 +               pr_err("%pd unlink err %d\n", dentry, err);
37445 +               goto out_fput;
37446 +       }
37447 +
37448 +       if (copy_src) {
37449 +               /* no one can touch copy_src xino */
37450 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37451 +               if (unlikely(err)) {
37452 +                       pr_err("%pd copy err %d\n", dentry, err);
37453 +                       goto out_fput;
37454 +               }
37455 +       }
37456 +       goto out_dput; /* success */
37457 +
37458 +out_fput:
37459 +       fput(file);
37460 +       file = ERR_PTR(err);
37461 +out_dput:
37462 +       dput(path.dentry);
37463 +out:
37464 +       if (do_unlock)
37465 +               au_xino_unlock_dir(&ldir);
37466 +       return file;
37467 +}
37468 +
37469 +struct file *au_xino_file1(struct au_xino *xi)
37470 +{
37471 +       struct file *file;
37472 +       unsigned int u, nfile;
37473 +
37474 +       file = NULL;
37475 +       nfile = xi->xi_nfile;
37476 +       for (u = 0; u < nfile; u++) {
37477 +               file = xi->xi_file[u];
37478 +               if (file)
37479 +                       break;
37480 +       }
37481 +
37482 +       return file;
37483 +}
37484 +
37485 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37486 +{
37487 +       int err;
37488 +       struct file *f;
37489 +       void *p;
37490 +
37491 +       if (file)
37492 +               get_file(file);
37493 +
37494 +       err = 0;
37495 +       f = NULL;
37496 +       if (idx < xi->xi_nfile) {
37497 +               f = xi->xi_file[idx];
37498 +               if (f)
37499 +                       fput(f);
37500 +       } else {
37501 +               p = au_kzrealloc(xi->xi_file,
37502 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37503 +                                sizeof(*xi->xi_file) * (idx + 1),
37504 +                                GFP_NOFS, /*may_shrink*/0);
37505 +               if (p) {
37506 +                       MtxMustLock(&xi->xi_mtx);
37507 +                       xi->xi_file = p;
37508 +                       xi->xi_nfile = idx + 1;
37509 +               } else {
37510 +                       err = -ENOMEM;
37511 +                       if (file)
37512 +                               fput(file);
37513 +                       goto out;
37514 +               }
37515 +       }
37516 +       xi->xi_file[idx] = file;
37517 +
37518 +out:
37519 +       return err;
37520 +}
37521 +
37522 +/*
37523 + * if @xinew->xi is not set, then create new xigen file.
37524 + */
37525 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37526 +{
37527 +       struct file *file;
37528 +       int err;
37529 +
37530 +       SiMustAnyLock(sb);
37531 +
37532 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37533 +       if (IS_ERR(file)) {
37534 +               err = PTR_ERR(file);
37535 +               pr_err("%s[%d], err %d\n",
37536 +                      xinew->xi ? "xino" : "xigen",
37537 +                      xinew->idx, err);
37538 +               goto out;
37539 +       }
37540 +
37541 +       if (xinew->xi)
37542 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37543 +       else {
37544 +               BUG();
37545 +               /* todo: make xigen file an array */
37546 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37547 +       }
37548 +       fput(file);
37549 +       if (unlikely(err))
37550 +               file = ERR_PTR(err);
37551 +
37552 +out:
37553 +       return file;
37554 +}
37555 +
37556 +/* ---------------------------------------------------------------------- */
37557 +
37558 +/*
37559 + * truncate xino files
37560 + */
37561 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37562 +                           int idx, struct kstatfs *st)
37563 +{
37564 +       int err;
37565 +       blkcnt_t blocks;
37566 +       struct file *file, *new_xino;
37567 +       struct au_xi_new xinew = {
37568 +               .idx = idx
37569 +       };
37570 +
37571 +       err = 0;
37572 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37573 +       file = au_xino_file(xinew.xi, idx);
37574 +       if (!file)
37575 +               goto out;
37576 +
37577 +       xinew.base = &file->f_path;
37578 +       err = vfs_statfs(xinew.base, st);
37579 +       if (unlikely(err)) {
37580 +               AuErr1("statfs err %d, ignored\n", err);
37581 +               err = 0;
37582 +               goto out;
37583 +       }
37584 +
37585 +       blocks = file_inode(file)->i_blocks;
37586 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37587 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37588 +
37589 +       xinew.copy_src = file;
37590 +       new_xino = au_xi_new(sb, &xinew);
37591 +       if (IS_ERR(new_xino)) {
37592 +               err = PTR_ERR(new_xino);
37593 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37594 +               goto out;
37595 +       }
37596 +
37597 +       err = vfs_statfs(&new_xino->f_path, st);
37598 +       if (!err)
37599 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37600 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37601 +                       st->f_bfree, st->f_blocks);
37602 +       else {
37603 +               AuErr1("statfs err %d, ignored\n", err);
37604 +               err = 0;
37605 +       }
37606 +
37607 +out:
37608 +       return err;
37609 +}
37610 +
37611 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37612 +{
37613 +       int err, i;
37614 +       unsigned long jiffy;
37615 +       aufs_bindex_t bbot;
37616 +       struct kstatfs *st;
37617 +       struct au_branch *br;
37618 +       struct au_xino *xi;
37619 +
37620 +       err = -ENOMEM;
37621 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37622 +       if (unlikely(!st))
37623 +               goto out;
37624 +
37625 +       err = -EINVAL;
37626 +       bbot = au_sbbot(sb);
37627 +       if (unlikely(bindex < 0 || bbot < bindex))
37628 +               goto out_st;
37629 +
37630 +       err = 0;
37631 +       jiffy = jiffies;
37632 +       br = au_sbr(sb, bindex);
37633 +       xi = br->br_xino;
37634 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37635 +               err = au_xino_do_trunc(sb, bindex, i, st);
37636 +       if (!err)
37637 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37638 +
37639 +out_st:
37640 +       au_kfree_rcu(st);
37641 +out:
37642 +       return err;
37643 +}
37644 +
37645 +struct xino_do_trunc_args {
37646 +       struct super_block *sb;
37647 +       struct au_branch *br;
37648 +       int idx;
37649 +};
37650 +
37651 +static void xino_do_trunc(void *_args)
37652 +{
37653 +       struct xino_do_trunc_args *args = _args;
37654 +       struct super_block *sb;
37655 +       struct au_branch *br;
37656 +       struct inode *dir;
37657 +       int err, idx;
37658 +       aufs_bindex_t bindex;
37659 +
37660 +       err = 0;
37661 +       sb = args->sb;
37662 +       dir = d_inode(sb->s_root);
37663 +       br = args->br;
37664 +       idx = args->idx;
37665 +
37666 +       si_noflush_write_lock(sb);
37667 +       ii_read_lock_parent(dir);
37668 +       bindex = au_br_index(sb, br->br_id);
37669 +       err = au_xino_trunc(sb, bindex, idx);
37670 +       ii_read_unlock(dir);
37671 +       if (unlikely(err))
37672 +               pr_warn("err b%d, (%d)\n", bindex, err);
37673 +       atomic_dec(&br->br_xino->xi_truncating);
37674 +       au_lcnt_dec(&br->br_count);
37675 +       si_write_unlock(sb);
37676 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37677 +       au_kfree_rcu(args);
37678 +}
37679 +
37680 +/*
37681 + * returns the index in the xi_file array whose corresponding file is necessary
37682 + * to truncate, or -1 which means no need to truncate.
37683 + */
37684 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37685 +{
37686 +       int err;
37687 +       unsigned int u;
37688 +       struct kstatfs st;
37689 +       struct au_sbinfo *sbinfo;
37690 +       struct au_xino *xi;
37691 +       struct file *file;
37692 +
37693 +       /* todo: si_xino_expire and the ratio should be customizable */
37694 +       sbinfo = au_sbi(sb);
37695 +       if (time_before(jiffies,
37696 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37697 +               return -1;
37698 +
37699 +       /* truncation border */
37700 +       xi = br->br_xino;
37701 +       for (u = 0; u < xi->xi_nfile; u++) {
37702 +               file = au_xino_file(xi, u);
37703 +               if (!file)
37704 +                       continue;
37705 +
37706 +               err = vfs_statfs(&file->f_path, &st);
37707 +               if (unlikely(err)) {
37708 +                       AuErr1("statfs err %d, ignored\n", err);
37709 +                       return -1;
37710 +               }
37711 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37712 +                   >= AUFS_XINO_DEF_TRUNC)
37713 +                       return u;
37714 +       }
37715 +
37716 +       return -1;
37717 +}
37718 +
37719 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37720 +{
37721 +       int idx;
37722 +       struct xino_do_trunc_args *args;
37723 +       int wkq_err;
37724 +
37725 +       idx = xino_trunc_test(sb, br);
37726 +       if (idx < 0)
37727 +               return;
37728 +
37729 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37730 +               goto out;
37731 +
37732 +       /* lock and kfree() will be called in trunc_xino() */
37733 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37734 +       if (unlikely(!args)) {
37735 +               AuErr1("no memory\n");
37736 +               goto out;
37737 +       }
37738 +
37739 +       au_lcnt_inc(&br->br_count);
37740 +       args->sb = sb;
37741 +       args->br = br;
37742 +       args->idx = idx;
37743 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37744 +       if (!wkq_err)
37745 +               return; /* success */
37746 +
37747 +       pr_err("wkq %d\n", wkq_err);
37748 +       au_lcnt_dec(&br->br_count);
37749 +       au_kfree_rcu(args);
37750 +
37751 +out:
37752 +       atomic_dec(&br->br_xino->xi_truncating);
37753 +}
37754 +
37755 +/* ---------------------------------------------------------------------- */
37756 +
37757 +struct au_xi_calc {
37758 +       int idx;
37759 +       loff_t pos;
37760 +};
37761 +
37762 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37763 +                      struct au_xi_calc *calc)
37764 +{
37765 +       loff_t maxent;
37766 +
37767 +       maxent = au_xi_maxent(sb);
37768 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37769 +       calc->pos *= sizeof(ino_t);
37770 +}
37771 +
37772 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37773 +                               struct au_xi_calc *calc)
37774 +{
37775 +       int err;
37776 +       struct file *file;
37777 +       struct au_xino *xi = br->br_xino;
37778 +       struct au_xi_new xinew = {
37779 +               .xi = xi
37780 +       };
37781 +
37782 +       SiMustAnyLock(sb);
37783 +
37784 +       err = 0;
37785 +       if (!xi)
37786 +               goto out;
37787 +
37788 +       mutex_lock(&xi->xi_mtx);
37789 +       file = au_xino_file(xi, calc->idx);
37790 +       if (file)
37791 +               goto out_mtx;
37792 +
37793 +       file = au_xino_file(xi, /*idx*/-1);
37794 +       AuDebugOn(!file);
37795 +       xinew.idx = calc->idx;
37796 +       xinew.base = &file->f_path;
37797 +       /* xinew.copy_src = NULL; */
37798 +       file = au_xi_new(sb, &xinew);
37799 +       if (IS_ERR(file))
37800 +               err = PTR_ERR(file);
37801 +
37802 +out_mtx:
37803 +       mutex_unlock(&xi->xi_mtx);
37804 +out:
37805 +       return err;
37806 +}
37807 +
37808 +struct au_xino_do_new_async_args {
37809 +       struct super_block *sb;
37810 +       struct au_branch *br;
37811 +       struct au_xi_calc calc;
37812 +       ino_t ino;
37813 +};
37814 +
37815 +struct au_xi_writing {
37816 +       struct hlist_bl_node node;
37817 +       ino_t h_ino, ino;
37818 +};
37819 +
37820 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37821 +                           ino_t ino);
37822 +
37823 +static void au_xino_call_do_new_async(void *args)
37824 +{
37825 +       struct au_xino_do_new_async_args *a = args;
37826 +       struct au_branch *br;
37827 +       struct super_block *sb;
37828 +       struct au_sbinfo *sbi;
37829 +       struct inode *root;
37830 +       struct file *file;
37831 +       struct au_xi_writing *del, *p;
37832 +       struct hlist_bl_head *hbl;
37833 +       struct hlist_bl_node *pos;
37834 +       int err;
37835 +
37836 +       br = a->br;
37837 +       sb = a->sb;
37838 +       sbi = au_sbi(sb);
37839 +       si_noflush_read_lock(sb);
37840 +       root = d_inode(sb->s_root);
37841 +       ii_read_lock_child(root);
37842 +       err = au_xino_do_new_async(sb, br, &a->calc);
37843 +       if (unlikely(err)) {
37844 +               AuIOErr("err %d\n", err);
37845 +               goto out;
37846 +       }
37847 +
37848 +       file = au_xino_file(br->br_xino, a->calc.idx);
37849 +       AuDebugOn(!file);
37850 +       err = au_xino_do_write(file, &a->calc, a->ino);
37851 +       if (unlikely(err)) {
37852 +               AuIOErr("err %d\n", err);
37853 +               goto out;
37854 +       }
37855 +
37856 +       del = NULL;
37857 +       hbl = &br->br_xino->xi_writing;
37858 +       hlist_bl_lock(hbl);
37859 +       au_hbl_for_each(pos, hbl) {
37860 +               p = container_of(pos, struct au_xi_writing, node);
37861 +               if (p->ino == a->ino) {
37862 +                       del = p;
37863 +                       hlist_bl_del(&p->node);
37864 +                       break;
37865 +               }
37866 +       }
37867 +       hlist_bl_unlock(hbl);
37868 +       au_kfree_rcu(del);
37869 +
37870 +out:
37871 +       au_lcnt_dec(&br->br_count);
37872 +       ii_read_unlock(root);
37873 +       si_read_unlock(sb);
37874 +       au_nwt_done(&sbi->si_nowait);
37875 +       au_kfree_rcu(a);
37876 +}
37877 +
37878 +/*
37879 + * create a new xino file asynchronously
37880 + */
37881 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37882 +                            struct au_xi_calc *calc, ino_t ino)
37883 +{
37884 +       int err;
37885 +       struct au_xino_do_new_async_args *arg;
37886 +
37887 +       err = -ENOMEM;
37888 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37889 +       if (unlikely(!arg))
37890 +               goto out;
37891 +
37892 +       arg->sb = sb;
37893 +       arg->br = br;
37894 +       arg->calc = *calc;
37895 +       arg->ino = ino;
37896 +       au_lcnt_inc(&br->br_count);
37897 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37898 +       if (unlikely(err)) {
37899 +               pr_err("wkq %d\n", err);
37900 +               au_lcnt_dec(&br->br_count);
37901 +               au_kfree_rcu(arg);
37902 +       }
37903 +
37904 +out:
37905 +       return err;
37906 +}
37907 +
37908 +/*
37909 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37910 + * at the position of @h_ino.
37911 + */
37912 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37913 +                ino_t *ino)
37914 +{
37915 +       int err;
37916 +       ssize_t sz;
37917 +       struct au_xi_calc calc;
37918 +       struct au_sbinfo *sbinfo;
37919 +       struct file *file;
37920 +       struct au_xino *xi;
37921 +       struct hlist_bl_head *hbl;
37922 +       struct hlist_bl_node *pos;
37923 +       struct au_xi_writing *p;
37924 +
37925 +       *ino = 0;
37926 +       if (!au_opt_test(au_mntflags(sb), XINO))
37927 +               return 0; /* no xino */
37928 +
37929 +       err = 0;
37930 +       au_xi_calc(sb, h_ino, &calc);
37931 +       xi = au_sbr(sb, bindex)->br_xino;
37932 +       file = au_xino_file(xi, calc.idx);
37933 +       if (!file) {
37934 +               hbl = &xi->xi_writing;
37935 +               hlist_bl_lock(hbl);
37936 +               au_hbl_for_each(pos, hbl) {
37937 +                       p = container_of(pos, struct au_xi_writing, node);
37938 +                       if (p->h_ino == h_ino) {
37939 +                               AuDbg("hi%llu, i%llu, found\n",
37940 +                                     (u64)p->h_ino, (u64)p->ino);
37941 +                               *ino = p->ino;
37942 +                               break;
37943 +                       }
37944 +               }
37945 +               hlist_bl_unlock(hbl);
37946 +               return 0;
37947 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37948 +               return 0; /* no xino */
37949 +
37950 +       sbinfo = au_sbi(sb);
37951 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37952 +       if (sz == sizeof(*ino))
37953 +               return 0; /* success */
37954 +
37955 +       err = sz;
37956 +       if (unlikely(sz >= 0)) {
37957 +               err = -EIO;
37958 +               AuIOErr("xino read error (%zd)\n", sz);
37959 +       }
37960 +       return err;
37961 +}
37962 +
37963 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37964 +                           ino_t ino)
37965 +{
37966 +       ssize_t sz;
37967 +
37968 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37969 +       if (sz == sizeof(ino))
37970 +               return 0; /* success */
37971 +
37972 +       AuIOErr("write failed (%zd)\n", sz);
37973 +       return -EIO;
37974 +}
37975 +
37976 +/*
37977 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37978 + * at the position of @h_ino.
37979 + * even if @ino is zero, it is written to the xinofile and means no entry.
37980 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37981 + * try truncating it.
37982 + */
37983 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37984 +                 ino_t ino)
37985 +{
37986 +       int err;
37987 +       unsigned int mnt_flags;
37988 +       struct au_xi_calc calc;
37989 +       struct file *file;
37990 +       struct au_branch *br;
37991 +       struct au_xino *xi;
37992 +       struct au_xi_writing *p;
37993 +
37994 +       SiMustAnyLock(sb);
37995 +
37996 +       mnt_flags = au_mntflags(sb);
37997 +       if (!au_opt_test(mnt_flags, XINO))
37998 +               return 0;
37999 +
38000 +       au_xi_calc(sb, h_ino, &calc);
38001 +       br = au_sbr(sb, bindex);
38002 +       xi = br->br_xino;
38003 +       file = au_xino_file(xi, calc.idx);
38004 +       if (!file) {
38005 +               /* store the inum pair into the list */
38006 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
38007 +               p->h_ino = h_ino;
38008 +               p->ino = ino;
38009 +               au_hbl_add(&p->node, &xi->xi_writing);
38010 +
38011 +               /* create and write a new xino file asynchronously */
38012 +               err = au_xino_new_async(sb, br, &calc, ino);
38013 +               if (!err)
38014 +                       return 0; /* success */
38015 +               goto out;
38016 +       }
38017 +
38018 +       err = au_xino_do_write(file, &calc, ino);
38019 +       if (!err) {
38020 +               br = au_sbr(sb, bindex);
38021 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
38022 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38023 +                       xino_try_trunc(sb, br);
38024 +               return 0; /* success */
38025 +       }
38026 +
38027 +out:
38028 +       AuIOErr("write failed (%d)\n", err);
38029 +       return -EIO;
38030 +}
38031 +
38032 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38033 +                             loff_t *pos);
38034 +
38035 +/* todo: unnecessary to support mmap_sem since kernel-space? */
38036 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
38037 +{
38038 +       ssize_t err;
38039 +       int i;
38040 +       const int prevent_endless = 10;
38041 +
38042 +       i = 0;
38043 +       do {
38044 +               err = vfsub_read_k(file, kbuf, size, pos);
38045 +               if (err == -EINTR
38046 +                   && !au_wkq_test()
38047 +                   && fatal_signal_pending(current)) {
38048 +                       err = xino_fread_wkq(file, kbuf, size, pos);
38049 +                       BUG_ON(err == -EINTR);
38050 +               }
38051 +       } while (i++ < prevent_endless
38052 +                && (err == -EAGAIN || err == -EINTR));
38053 +
38054 +#if 0 /* reserved for future use */
38055 +       if (err > 0)
38056 +               fsnotify_access(file->f_path.dentry);
38057 +#endif
38058 +
38059 +       return err;
38060 +}
38061 +
38062 +struct xino_fread_args {
38063 +       ssize_t *errp;
38064 +       struct file *file;
38065 +       void *buf;
38066 +       size_t size;
38067 +       loff_t *pos;
38068 +};
38069 +
38070 +static void call_xino_fread(void *args)
38071 +{
38072 +       struct xino_fread_args *a = args;
38073 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38074 +}
38075 +
38076 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38077 +                             loff_t *pos)
38078 +{
38079 +       ssize_t err;
38080 +       int wkq_err;
38081 +       struct xino_fread_args args = {
38082 +               .errp   = &err,
38083 +               .file   = file,
38084 +               .buf    = buf,
38085 +               .size   = size,
38086 +               .pos    = pos
38087 +       };
38088 +
38089 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38090 +       if (unlikely(wkq_err))
38091 +               err = wkq_err;
38092 +
38093 +       return err;
38094 +}
38095 +
38096 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38097 +                              loff_t *pos);
38098 +
38099 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38100 +                             loff_t *pos)
38101 +{
38102 +       ssize_t err;
38103 +       int i;
38104 +       const int prevent_endless = 10;
38105 +
38106 +       i = 0;
38107 +       do {
38108 +               err = vfsub_write_k(file, kbuf, size, pos);
38109 +               if (err == -EINTR
38110 +                   && !au_wkq_test()
38111 +                   && fatal_signal_pending(current)) {
38112 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38113 +                       BUG_ON(err == -EINTR);
38114 +               }
38115 +       } while (i++ < prevent_endless
38116 +                && (err == -EAGAIN || err == -EINTR));
38117 +
38118 +#if 0 /* reserved for future use */
38119 +       if (err > 0)
38120 +               fsnotify_modify(file->f_path.dentry);
38121 +#endif
38122 +
38123 +       return err;
38124 +}
38125 +
38126 +struct do_xino_fwrite_args {
38127 +       ssize_t *errp;
38128 +       struct file *file;
38129 +       void *buf;
38130 +       size_t size;
38131 +       loff_t *pos;
38132 +};
38133 +
38134 +static void call_do_xino_fwrite(void *args)
38135 +{
38136 +       struct do_xino_fwrite_args *a = args;
38137 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38138 +}
38139 +
38140 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38141 +                              loff_t *pos)
38142 +{
38143 +       ssize_t err;
38144 +       int wkq_err;
38145 +       struct do_xino_fwrite_args args = {
38146 +               .errp   = &err,
38147 +               .file   = file,
38148 +               .buf    = buf,
38149 +               .size   = size,
38150 +               .pos    = pos
38151 +       };
38152 +
38153 +       /*
38154 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38155 +        * users should care about quota and real 'filesystem full.'
38156 +        */
38157 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38158 +       if (unlikely(wkq_err))
38159 +               err = wkq_err;
38160 +
38161 +       return err;
38162 +}
38163 +
38164 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38165 +{
38166 +       ssize_t err;
38167 +
38168 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38169 +               lockdep_off();
38170 +               err = do_xino_fwrite(file, buf, size, pos);
38171 +               lockdep_on();
38172 +       } else {
38173 +               lockdep_off();
38174 +               err = xino_fwrite_wkq(file, buf, size, pos);
38175 +               lockdep_on();
38176 +       }
38177 +
38178 +       return err;
38179 +}
38180 +
38181 +/* ---------------------------------------------------------------------- */
38182 +
38183 +/*
38184 + * inode number bitmap
38185 + */
38186 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38187 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38188 +{
38189 +       ino_t ino;
38190 +
38191 +       AuDebugOn(bit < 0 || page_bits <= bit);
38192 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38193 +       return ino;
38194 +}
38195 +
38196 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38197 +{
38198 +       AuDebugOn(ino < AUFS_FIRST_INO);
38199 +       ino -= AUFS_FIRST_INO;
38200 +       *pindex = ino / page_bits;
38201 +       *bit = ino % page_bits;
38202 +}
38203 +
38204 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38205 +{
38206 +       int err;
38207 +       loff_t pos;
38208 +       ssize_t sz;
38209 +       struct au_sbinfo *sbinfo;
38210 +       struct file *xib;
38211 +       unsigned long *p;
38212 +
38213 +       sbinfo = au_sbi(sb);
38214 +       MtxMustLock(&sbinfo->si_xib_mtx);
38215 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38216 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38217 +
38218 +       if (pindex == sbinfo->si_xib_last_pindex)
38219 +               return 0;
38220 +
38221 +       xib = sbinfo->si_xib;
38222 +       p = sbinfo->si_xib_buf;
38223 +       pos = sbinfo->si_xib_last_pindex;
38224 +       pos *= PAGE_SIZE;
38225 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38226 +       if (unlikely(sz != PAGE_SIZE))
38227 +               goto out;
38228 +
38229 +       pos = pindex;
38230 +       pos *= PAGE_SIZE;
38231 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38232 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38233 +       else {
38234 +               memset(p, 0, PAGE_SIZE);
38235 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38236 +       }
38237 +       if (sz == PAGE_SIZE) {
38238 +               sbinfo->si_xib_last_pindex = pindex;
38239 +               return 0; /* success */
38240 +       }
38241 +
38242 +out:
38243 +       AuIOErr1("write failed (%zd)\n", sz);
38244 +       err = sz;
38245 +       if (sz >= 0)
38246 +               err = -EIO;
38247 +       return err;
38248 +}
38249 +
38250 +static void au_xib_clear_bit(struct inode *inode)
38251 +{
38252 +       int err, bit;
38253 +       unsigned long pindex;
38254 +       struct super_block *sb;
38255 +       struct au_sbinfo *sbinfo;
38256 +
38257 +       AuDebugOn(inode->i_nlink);
38258 +
38259 +       sb = inode->i_sb;
38260 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38261 +       AuDebugOn(page_bits <= bit);
38262 +       sbinfo = au_sbi(sb);
38263 +       mutex_lock(&sbinfo->si_xib_mtx);
38264 +       err = xib_pindex(sb, pindex);
38265 +       if (!err) {
38266 +               clear_bit(bit, sbinfo->si_xib_buf);
38267 +               sbinfo->si_xib_next_bit = bit;
38268 +       }
38269 +       mutex_unlock(&sbinfo->si_xib_mtx);
38270 +}
38271 +
38272 +/* ---------------------------------------------------------------------- */
38273 +
38274 +/*
38275 + * truncate a xino bitmap file
38276 + */
38277 +
38278 +/* todo: slow */
38279 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38280 +{
38281 +       int err, bit;
38282 +       ssize_t sz;
38283 +       unsigned long pindex;
38284 +       loff_t pos, pend;
38285 +       struct au_sbinfo *sbinfo;
38286 +       ino_t *ino;
38287 +       unsigned long *p;
38288 +
38289 +       err = 0;
38290 +       sbinfo = au_sbi(sb);
38291 +       MtxMustLock(&sbinfo->si_xib_mtx);
38292 +       p = sbinfo->si_xib_buf;
38293 +       pend = vfsub_f_size_read(file);
38294 +       pos = 0;
38295 +       while (pos < pend) {
38296 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38297 +               err = sz;
38298 +               if (unlikely(sz <= 0))
38299 +                       goto out;
38300 +
38301 +               err = 0;
38302 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38303 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38304 +                               continue;
38305 +
38306 +                       xib_calc_bit(*ino, &pindex, &bit);
38307 +                       AuDebugOn(page_bits <= bit);
38308 +                       err = xib_pindex(sb, pindex);
38309 +                       if (!err)
38310 +                               set_bit(bit, p);
38311 +                       else
38312 +                               goto out;
38313 +               }
38314 +       }
38315 +
38316 +out:
38317 +       return err;
38318 +}
38319 +
38320 +static int xib_restore(struct super_block *sb)
38321 +{
38322 +       int err, i;
38323 +       unsigned int nfile;
38324 +       aufs_bindex_t bindex, bbot;
38325 +       void *page;
38326 +       struct au_branch *br;
38327 +       struct au_xino *xi;
38328 +       struct file *file;
38329 +
38330 +       err = -ENOMEM;
38331 +       page = (void *)__get_free_page(GFP_NOFS);
38332 +       if (unlikely(!page))
38333 +               goto out;
38334 +
38335 +       err = 0;
38336 +       bbot = au_sbbot(sb);
38337 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38338 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38339 +                       br = au_sbr(sb, bindex);
38340 +                       xi = br->br_xino;
38341 +                       nfile = xi->xi_nfile;
38342 +                       for (i = 0; i < nfile; i++) {
38343 +                               file = au_xino_file(xi, i);
38344 +                               if (file)
38345 +                                       err = do_xib_restore(sb, file, page);
38346 +                       }
38347 +               } else
38348 +                       AuDbg("skip shared b%d\n", bindex);
38349 +       free_page((unsigned long)page);
38350 +
38351 +out:
38352 +       return err;
38353 +}
38354 +
38355 +int au_xib_trunc(struct super_block *sb)
38356 +{
38357 +       int err;
38358 +       ssize_t sz;
38359 +       loff_t pos;
38360 +       struct au_sbinfo *sbinfo;
38361 +       unsigned long *p;
38362 +       struct file *file;
38363 +
38364 +       SiMustWriteLock(sb);
38365 +
38366 +       err = 0;
38367 +       sbinfo = au_sbi(sb);
38368 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38369 +               goto out;
38370 +
38371 +       file = sbinfo->si_xib;
38372 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38373 +               goto out;
38374 +
38375 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38376 +       err = PTR_ERR(file);
38377 +       if (IS_ERR(file))
38378 +               goto out;
38379 +       fput(sbinfo->si_xib);
38380 +       sbinfo->si_xib = file;
38381 +
38382 +       p = sbinfo->si_xib_buf;
38383 +       memset(p, 0, PAGE_SIZE);
38384 +       pos = 0;
38385 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38386 +       if (unlikely(sz != PAGE_SIZE)) {
38387 +               err = sz;
38388 +               AuIOErr("err %d\n", err);
38389 +               if (sz >= 0)
38390 +                       err = -EIO;
38391 +               goto out;
38392 +       }
38393 +
38394 +       mutex_lock(&sbinfo->si_xib_mtx);
38395 +       /* mnt_want_write() is unnecessary here */
38396 +       err = xib_restore(sb);
38397 +       mutex_unlock(&sbinfo->si_xib_mtx);
38398 +
38399 +out:
38400 +       return err;
38401 +}
38402 +
38403 +/* ---------------------------------------------------------------------- */
38404 +
38405 +struct au_xino *au_xino_alloc(unsigned int nfile)
38406 +{
38407 +       struct au_xino *xi;
38408 +
38409 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38410 +       if (unlikely(!xi))
38411 +               goto out;
38412 +       xi->xi_nfile = nfile;
38413 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38414 +       if (unlikely(!xi->xi_file))
38415 +               goto out_free;
38416 +
38417 +       xi->xi_nondir.total = 8; /* initial size */
38418 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38419 +                                     GFP_NOFS);
38420 +       if (unlikely(!xi->xi_nondir.array))
38421 +               goto out_file;
38422 +
38423 +       spin_lock_init(&xi->xi_nondir.spin);
38424 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38425 +       mutex_init(&xi->xi_mtx);
38426 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38427 +       atomic_set(&xi->xi_truncating, 0);
38428 +       kref_init(&xi->xi_kref);
38429 +       goto out; /* success */
38430 +
38431 +out_file:
38432 +       au_kfree_try_rcu(xi->xi_file);
38433 +out_free:
38434 +       au_kfree_rcu(xi);
38435 +       xi = NULL;
38436 +out:
38437 +       return xi;
38438 +}
38439 +
38440 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38441 +{
38442 +       int err;
38443 +       struct au_xino *xi;
38444 +
38445 +       err = 0;
38446 +       xi = au_xino_alloc(idx + 1);
38447 +       if (unlikely(!xi)) {
38448 +               err = -ENOMEM;
38449 +               goto out;
38450 +       }
38451 +
38452 +       if (file)
38453 +               get_file(file);
38454 +       xi->xi_file[idx] = file;
38455 +       AuDebugOn(br->br_xino);
38456 +       br->br_xino = xi;
38457 +
38458 +out:
38459 +       return err;
38460 +}
38461 +
38462 +static void au_xino_release(struct kref *kref)
38463 +{
38464 +       struct au_xino *xi;
38465 +       int i;
38466 +       unsigned long ul;
38467 +       struct hlist_bl_head *hbl;
38468 +       struct hlist_bl_node *pos, *n;
38469 +       struct au_xi_writing *p;
38470 +
38471 +       xi = container_of(kref, struct au_xino, xi_kref);
38472 +       for (i = 0; i < xi->xi_nfile; i++)
38473 +               if (xi->xi_file[i])
38474 +                       fput(xi->xi_file[i]);
38475 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38476 +               AuDebugOn(xi->xi_nondir.array[i]);
38477 +       mutex_destroy(&xi->xi_mtx);
38478 +       hbl = &xi->xi_writing;
38479 +       ul = au_hbl_count(hbl);
38480 +       if (unlikely(ul)) {
38481 +               pr_warn("xi_writing %lu\n", ul);
38482 +               hlist_bl_lock(hbl);
38483 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38484 +                       hlist_bl_del(&p->node);
38485 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38486 +                       kfree(p);
38487 +               }
38488 +               hlist_bl_unlock(hbl);
38489 +       }
38490 +       au_kfree_try_rcu(xi->xi_file);
38491 +       au_kfree_try_rcu(xi->xi_nondir.array);
38492 +       au_kfree_rcu(xi);
38493 +}
38494 +
38495 +int au_xino_put(struct au_branch *br)
38496 +{
38497 +       int ret;
38498 +       struct au_xino *xi;
38499 +
38500 +       ret = 0;
38501 +       xi = br->br_xino;
38502 +       if (xi) {
38503 +               br->br_xino = NULL;
38504 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38505 +       }
38506 +
38507 +       return ret;
38508 +}
38509 +
38510 +/* ---------------------------------------------------------------------- */
38511 +
38512 +/*
38513 + * xino mount option handlers
38514 + */
38515 +
38516 +/* xino bitmap */
38517 +static void xino_clear_xib(struct super_block *sb)
38518 +{
38519 +       struct au_sbinfo *sbinfo;
38520 +
38521 +       SiMustWriteLock(sb);
38522 +
38523 +       sbinfo = au_sbi(sb);
38524 +       if (sbinfo->si_xib)
38525 +               fput(sbinfo->si_xib);
38526 +       sbinfo->si_xib = NULL;
38527 +       if (sbinfo->si_xib_buf)
38528 +               free_page((unsigned long)sbinfo->si_xib_buf);
38529 +       sbinfo->si_xib_buf = NULL;
38530 +}
38531 +
38532 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38533 +{
38534 +       int err;
38535 +       loff_t pos;
38536 +       struct au_sbinfo *sbinfo;
38537 +       struct file *file;
38538 +       struct super_block *xi_sb;
38539 +
38540 +       SiMustWriteLock(sb);
38541 +
38542 +       sbinfo = au_sbi(sb);
38543 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38544 +       err = PTR_ERR(file);
38545 +       if (IS_ERR(file))
38546 +               goto out;
38547 +       if (sbinfo->si_xib)
38548 +               fput(sbinfo->si_xib);
38549 +       sbinfo->si_xib = file;
38550 +       xi_sb = file_inode(file)->i_sb;
38551 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38552 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38553 +               err = -EIO;
38554 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38555 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38556 +               goto out_unset;
38557 +       }
38558 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38559 +
38560 +       err = -ENOMEM;
38561 +       if (!sbinfo->si_xib_buf)
38562 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38563 +       if (unlikely(!sbinfo->si_xib_buf))
38564 +               goto out_unset;
38565 +
38566 +       sbinfo->si_xib_last_pindex = 0;
38567 +       sbinfo->si_xib_next_bit = 0;
38568 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38569 +               pos = 0;
38570 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38571 +               if (unlikely(err != PAGE_SIZE))
38572 +                       goto out_free;
38573 +       }
38574 +       err = 0;
38575 +       goto out; /* success */
38576 +
38577 +out_free:
38578 +       if (sbinfo->si_xib_buf)
38579 +               free_page((unsigned long)sbinfo->si_xib_buf);
38580 +       sbinfo->si_xib_buf = NULL;
38581 +       if (err >= 0)
38582 +               err = -EIO;
38583 +out_unset:
38584 +       fput(sbinfo->si_xib);
38585 +       sbinfo->si_xib = NULL;
38586 +out:
38587 +       AuTraceErr(err);
38588 +       return err;
38589 +}
38590 +
38591 +/* xino for each branch */
38592 +static void xino_clear_br(struct super_block *sb)
38593 +{
38594 +       aufs_bindex_t bindex, bbot;
38595 +       struct au_branch *br;
38596 +
38597 +       bbot = au_sbbot(sb);
38598 +       for (bindex = 0; bindex <= bbot; bindex++) {
38599 +               br = au_sbr(sb, bindex);
38600 +               AuDebugOn(!br);
38601 +               au_xino_put(br);
38602 +       }
38603 +}
38604 +
38605 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38606 +                                 aufs_bindex_t bshared)
38607 +{
38608 +       struct au_branch *brshared;
38609 +
38610 +       brshared = au_sbr(sb, bshared);
38611 +       AuDebugOn(!brshared->br_xino);
38612 +       AuDebugOn(!brshared->br_xino->xi_file);
38613 +       if (br->br_xino != brshared->br_xino) {
38614 +               au_xino_get(brshared);
38615 +               au_xino_put(br);
38616 +               br->br_xino = brshared->br_xino;
38617 +       }
38618 +}
38619 +
38620 +struct au_xino_do_set_br {
38621 +       struct au_branch *br;
38622 +       ino_t h_ino;
38623 +       aufs_bindex_t bshared;
38624 +};
38625 +
38626 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38627 +                            struct au_xino_do_set_br *args)
38628 +{
38629 +       int err;
38630 +       struct au_xi_calc calc;
38631 +       struct file *file;
38632 +       struct au_branch *br;
38633 +       struct au_xi_new xinew = {
38634 +               .base = path
38635 +       };
38636 +
38637 +       br = args->br;
38638 +       xinew.xi = br->br_xino;
38639 +       au_xi_calc(sb, args->h_ino, &calc);
38640 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38641 +       if (args->bshared >= 0)
38642 +               /* shared xino */
38643 +               au_xino_set_br_shared(sb, br, args->bshared);
38644 +       else if (!xinew.xi) {
38645 +               /* new xino */
38646 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38647 +               if (unlikely(err))
38648 +                       goto out;
38649 +       }
38650 +
38651 +       /* force re-creating */
38652 +       xinew.xi = br->br_xino;
38653 +       xinew.idx = calc.idx;
38654 +       mutex_lock(&xinew.xi->xi_mtx);
38655 +       file = au_xi_new(sb, &xinew);
38656 +       mutex_unlock(&xinew.xi->xi_mtx);
38657 +       err = PTR_ERR(file);
38658 +       if (IS_ERR(file))
38659 +               goto out;
38660 +       AuDebugOn(!file);
38661 +
38662 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38663 +       if (unlikely(err))
38664 +               au_xino_put(br);
38665 +
38666 +out:
38667 +       AuTraceErr(err);
38668 +       return err;
38669 +}
38670 +
38671 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38672 +{
38673 +       int err;
38674 +       aufs_bindex_t bindex, bbot;
38675 +       struct au_xino_do_set_br args;
38676 +       struct inode *inode;
38677 +
38678 +       SiMustWriteLock(sb);
38679 +
38680 +       bbot = au_sbbot(sb);
38681 +       inode = d_inode(sb->s_root);
38682 +       for (bindex = 0; bindex <= bbot; bindex++) {
38683 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38684 +               args.br = au_sbr(sb, bindex);
38685 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38686 +               err = au_xino_do_set_br(sb, path, &args);
38687 +               if (unlikely(err))
38688 +                       break;
38689 +       }
38690 +
38691 +       AuTraceErr(err);
38692 +       return err;
38693 +}
38694 +
38695 +void au_xino_clr(struct super_block *sb)
38696 +{
38697 +       struct au_sbinfo *sbinfo;
38698 +
38699 +       au_xigen_clr(sb);
38700 +       xino_clear_xib(sb);
38701 +       xino_clear_br(sb);
38702 +       dbgaufs_brs_del(sb, 0);
38703 +       sbinfo = au_sbi(sb);
38704 +       /* lvalue, do not call au_mntflags() */
38705 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38706 +}
38707 +
38708 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38709 +{
38710 +       int err, skip;
38711 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38712 +       struct qstr *dname, *cur_name;
38713 +       struct file *cur_xino;
38714 +       struct au_sbinfo *sbinfo;
38715 +       struct path *path, *cur_path;
38716 +
38717 +       SiMustWriteLock(sb);
38718 +
38719 +       err = 0;
38720 +       sbinfo = au_sbi(sb);
38721 +       path = &xiopt->file->f_path;
38722 +       dentry = path->dentry;
38723 +       parent = dget_parent(dentry);
38724 +       if (remount) {
38725 +               skip = 0;
38726 +               cur_xino = sbinfo->si_xib;
38727 +               if (cur_xino) {
38728 +                       cur_path = &cur_xino->f_path;
38729 +                       cur_dentry = cur_path->dentry;
38730 +                       cur_parent = dget_parent(cur_dentry);
38731 +                       cur_name = &cur_dentry->d_name;
38732 +                       dname = &dentry->d_name;
38733 +                       skip = (cur_parent == parent
38734 +                               && au_qstreq(dname, cur_name));
38735 +                       dput(cur_parent);
38736 +               }
38737 +               if (skip)
38738 +                       goto out;
38739 +       }
38740 +
38741 +       au_opt_set(sbinfo->si_mntflags, XINO);
38742 +       err = au_xino_set_xib(sb, path);
38743 +       /* si_x{read,write} are set */
38744 +       if (!err)
38745 +               err = au_xigen_set(sb, path);
38746 +       if (!err)
38747 +               err = au_xino_set_br(sb, path);
38748 +       if (!err) {
38749 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38750 +               goto out; /* success */
38751 +       }
38752 +
38753 +       /* reset all */
38754 +       AuIOErr("failed setting xino(%d).\n", err);
38755 +       au_xino_clr(sb);
38756 +
38757 +out:
38758 +       dput(parent);
38759 +       return err;
38760 +}
38761 +
38762 +/*
38763 + * create a xinofile at the default place/path.
38764 + */
38765 +struct file *au_xino_def(struct super_block *sb)
38766 +{
38767 +       struct file *file;
38768 +       char *page, *p;
38769 +       struct au_branch *br;
38770 +       struct super_block *h_sb;
38771 +       struct path path;
38772 +       aufs_bindex_t bbot, bindex, bwr;
38773 +
38774 +       br = NULL;
38775 +       bbot = au_sbbot(sb);
38776 +       bwr = -1;
38777 +       for (bindex = 0; bindex <= bbot; bindex++) {
38778 +               br = au_sbr(sb, bindex);
38779 +               if (au_br_writable(br->br_perm)
38780 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38781 +                       bwr = bindex;
38782 +                       break;
38783 +               }
38784 +       }
38785 +
38786 +       if (bwr >= 0) {
38787 +               file = ERR_PTR(-ENOMEM);
38788 +               page = (void *)__get_free_page(GFP_NOFS);
38789 +               if (unlikely(!page))
38790 +                       goto out;
38791 +               path.mnt = au_br_mnt(br);
38792 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38793 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38794 +               file = (void *)p;
38795 +               if (!IS_ERR(p)) {
38796 +                       strcat(p, "/" AUFS_XINO_FNAME);
38797 +                       AuDbg("%s\n", p);
38798 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38799 +               }
38800 +               free_page((unsigned long)page);
38801 +       } else {
38802 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38803 +                                     /*wbrtop*/0);
38804 +               if (IS_ERR(file))
38805 +                       goto out;
38806 +               h_sb = file->f_path.dentry->d_sb;
38807 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38808 +                       pr_err("xino doesn't support %s(%s)\n",
38809 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38810 +                       fput(file);
38811 +                       file = ERR_PTR(-EINVAL);
38812 +               }
38813 +       }
38814 +
38815 +out:
38816 +       return file;
38817 +}
38818 +
38819 +/* ---------------------------------------------------------------------- */
38820 +
38821 +/*
38822 + * initialize the xinofile for the specified branch @br
38823 + * at the place/path where @base_file indicates.
38824 + * test whether another branch is on the same filesystem or not,
38825 + * if found then share the xinofile with another branch.
38826 + */
38827 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38828 +                   struct path *base)
38829 +{
38830 +       int err;
38831 +       struct au_xino_do_set_br args = {
38832 +               .h_ino  = h_ino,
38833 +               .br     = br
38834 +       };
38835 +
38836 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38837 +                                      au_br_sb(br));
38838 +       err = au_xino_do_set_br(sb, base, &args);
38839 +       if (unlikely(err))
38840 +               au_xino_put(br);
38841 +
38842 +       return err;
38843 +}
38844 +
38845 +/* ---------------------------------------------------------------------- */
38846 +
38847 +/*
38848 + * get an unused inode number from bitmap
38849 + */
38850 +ino_t au_xino_new_ino(struct super_block *sb)
38851 +{
38852 +       ino_t ino;
38853 +       unsigned long *p, pindex, ul, pend;
38854 +       struct au_sbinfo *sbinfo;
38855 +       struct file *file;
38856 +       int free_bit, err;
38857 +
38858 +       if (!au_opt_test(au_mntflags(sb), XINO))
38859 +               return iunique(sb, AUFS_FIRST_INO);
38860 +
38861 +       sbinfo = au_sbi(sb);
38862 +       mutex_lock(&sbinfo->si_xib_mtx);
38863 +       p = sbinfo->si_xib_buf;
38864 +       free_bit = sbinfo->si_xib_next_bit;
38865 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38866 +               goto out; /* success */
38867 +       free_bit = find_first_zero_bit(p, page_bits);
38868 +       if (free_bit < page_bits)
38869 +               goto out; /* success */
38870 +
38871 +       pindex = sbinfo->si_xib_last_pindex;
38872 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38873 +               err = xib_pindex(sb, ul);
38874 +               if (unlikely(err))
38875 +                       goto out_err;
38876 +               free_bit = find_first_zero_bit(p, page_bits);
38877 +               if (free_bit < page_bits)
38878 +                       goto out; /* success */
38879 +       }
38880 +
38881 +       file = sbinfo->si_xib;
38882 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38883 +       for (ul = pindex + 1; ul <= pend; ul++) {
38884 +               err = xib_pindex(sb, ul);
38885 +               if (unlikely(err))
38886 +                       goto out_err;
38887 +               free_bit = find_first_zero_bit(p, page_bits);
38888 +               if (free_bit < page_bits)
38889 +                       goto out; /* success */
38890 +       }
38891 +       BUG();
38892 +
38893 +out:
38894 +       set_bit(free_bit, p);
38895 +       sbinfo->si_xib_next_bit = free_bit + 1;
38896 +       pindex = sbinfo->si_xib_last_pindex;
38897 +       mutex_unlock(&sbinfo->si_xib_mtx);
38898 +       ino = xib_calc_ino(pindex, free_bit);
38899 +       AuDbg("i%lu\n", (unsigned long)ino);
38900 +       return ino;
38901 +out_err:
38902 +       mutex_unlock(&sbinfo->si_xib_mtx);
38903 +       AuDbg("i0\n");
38904 +       return 0;
38905 +}
38906 +
38907 +/* for s_op->delete_inode() */
38908 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38909 +{
38910 +       int err;
38911 +       unsigned int mnt_flags;
38912 +       aufs_bindex_t bindex, bbot, bi;
38913 +       unsigned char try_trunc;
38914 +       struct au_iinfo *iinfo;
38915 +       struct super_block *sb;
38916 +       struct au_hinode *hi;
38917 +       struct inode *h_inode;
38918 +       struct au_branch *br;
38919 +       struct au_xi_calc calc;
38920 +       struct file *file;
38921 +
38922 +       AuDebugOn(au_is_bad_inode(inode));
38923 +
38924 +       sb = inode->i_sb;
38925 +       mnt_flags = au_mntflags(sb);
38926 +       if (!au_opt_test(mnt_flags, XINO)
38927 +           || inode->i_ino == AUFS_ROOT_INO)
38928 +               return;
38929 +
38930 +       if (unlinked) {
38931 +               au_xigen_inc(inode);
38932 +               au_xib_clear_bit(inode);
38933 +       }
38934 +
38935 +       iinfo = au_ii(inode);
38936 +       bindex = iinfo->ii_btop;
38937 +       if (bindex < 0)
38938 +               return;
38939 +
38940 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38941 +       hi = au_hinode(iinfo, bindex);
38942 +       bbot = iinfo->ii_bbot;
38943 +       for (; bindex <= bbot; bindex++, hi++) {
38944 +               h_inode = hi->hi_inode;
38945 +               if (!h_inode
38946 +                   || (!unlinked && h_inode->i_nlink))
38947 +                       continue;
38948 +
38949 +               /* inode may not be revalidated */
38950 +               bi = au_br_index(sb, hi->hi_id);
38951 +               if (bi < 0)
38952 +                       continue;
38953 +
38954 +               br = au_sbr(sb, bi);
38955 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38956 +               file = au_xino_file(br->br_xino, calc.idx);
38957 +               if (IS_ERR_OR_NULL(file))
38958 +                       continue;
38959 +
38960 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38961 +               if (!err && try_trunc
38962 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38963 +                       xino_try_trunc(sb, br);
38964 +       }
38965 +}
38966 +
38967 +/* ---------------------------------------------------------------------- */
38968 +
38969 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38970 +{
38971 +       int found, total, i;
38972 +
38973 +       found = -1;
38974 +       total = xi->xi_nondir.total;
38975 +       for (i = 0; i < total; i++) {
38976 +               if (xi->xi_nondir.array[i] != h_ino)
38977 +                       continue;
38978 +               found = i;
38979 +               break;
38980 +       }
38981 +
38982 +       return found;
38983 +}
38984 +
38985 +static int au_xinondir_expand(struct au_xino *xi)
38986 +{
38987 +       int err, sz;
38988 +       ino_t *p;
38989 +
38990 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38991 +
38992 +       err = -ENOMEM;
38993 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38994 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38995 +               goto out;
38996 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38997 +                        /*may_shrink*/0);
38998 +       if (p) {
38999 +               xi->xi_nondir.array = p;
39000 +               xi->xi_nondir.total <<= 1;
39001 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
39002 +               err = 0;
39003 +       }
39004 +
39005 +out:
39006 +       return err;
39007 +}
39008 +
39009 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
39010 +                      ino_t h_ino, int idx)
39011 +{
39012 +       struct au_xino *xi;
39013 +
39014 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
39015 +       xi = au_sbr(sb, bindex)->br_xino;
39016 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
39017 +
39018 +       spin_lock(&xi->xi_nondir.spin);
39019 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
39020 +       xi->xi_nondir.array[idx] = 0;
39021 +       spin_unlock(&xi->xi_nondir.spin);
39022 +       wake_up_all(&xi->xi_nondir.wqh);
39023 +}
39024 +
39025 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
39026 +                     int *idx)
39027 +{
39028 +       int err, found, empty;
39029 +       struct au_xino *xi;
39030 +
39031 +       err = 0;
39032 +       *idx = -1;
39033 +       if (!au_opt_test(au_mntflags(sb), XINO))
39034 +               goto out; /* no xino */
39035 +
39036 +       xi = au_sbr(sb, bindex)->br_xino;
39037 +
39038 +again:
39039 +       spin_lock(&xi->xi_nondir.spin);
39040 +       found = au_xinondir_find(xi, h_ino);
39041 +       if (found == -1) {
39042 +               empty = au_xinondir_find(xi, /*h_ino*/0);
39043 +               if (empty == -1) {
39044 +                       empty = xi->xi_nondir.total;
39045 +                       err = au_xinondir_expand(xi);
39046 +                       if (unlikely(err))
39047 +                               goto out_unlock;
39048 +               }
39049 +               xi->xi_nondir.array[empty] = h_ino;
39050 +               *idx = empty;
39051 +       } else {
39052 +               spin_unlock(&xi->xi_nondir.spin);
39053 +               wait_event(xi->xi_nondir.wqh,
39054 +                          xi->xi_nondir.array[found] != h_ino);
39055 +               goto again;
39056 +       }
39057 +
39058 +out_unlock:
39059 +       spin_unlock(&xi->xi_nondir.spin);
39060 +out:
39061 +       return err;
39062 +}
39063 +
39064 +/* ---------------------------------------------------------------------- */
39065 +
39066 +int au_xino_path(struct seq_file *seq, struct file *file)
39067 +{
39068 +       int err;
39069 +
39070 +       err = au_seq_path(seq, &file->f_path);
39071 +       if (unlikely(err))
39072 +               goto out;
39073 +
39074 +#define Deleted "\\040(deleted)"
39075 +       seq->count -= sizeof(Deleted) - 1;
39076 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39077 +                        sizeof(Deleted) - 1));
39078 +#undef Deleted
39079 +
39080 +out:
39081 +       return err;
39082 +}
39083 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39084 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39085 +++ linux/include/uapi/linux/aufs_type.h        2024-03-10 23:40:47.086476782 +0100
39086 @@ -0,0 +1,452 @@
39087 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39088 +/*
39089 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39090 + *
39091 + * This program is free software; you can redistribute it and/or modify
39092 + * it under the terms of the GNU General Public License as published by
39093 + * the Free Software Foundation; either version 2 of the License, or
39094 + * (at your option) any later version.
39095 + *
39096 + * This program is distributed in the hope that it will be useful,
39097 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39098 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39099 + * GNU General Public License for more details.
39100 + *
39101 + * You should have received a copy of the GNU General Public License
39102 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39103 + */
39104 +
39105 +#ifndef __AUFS_TYPE_H__
39106 +#define __AUFS_TYPE_H__
39107 +
39108 +#define AUFS_NAME      "aufs"
39109 +
39110 +#ifdef __KERNEL__
39111 +/*
39112 + * define it before including all other headers.
39113 + * sched.h may use pr_* macros before defining "current", so define the
39114 + * no-current version first, and re-define later.
39115 + */
39116 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39117 +#include <linux/sched.h>
39118 +#undef pr_fmt
39119 +#define pr_fmt(fmt) \
39120 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39121 +               (int)sizeof(current->comm), current->comm, current->pid
39122 +#include <linux/limits.h>
39123 +#else
39124 +#include <stdint.h>
39125 +#include <sys/types.h>
39126 +#include <limits.h>
39127 +#endif /* __KERNEL__ */
39128 +
39129 +#define AUFS_VERSION   "6.x-rcN-20240226"
39130 +
39131 +/* todo? move this to linux-2.6.19/include/magic.h */
39132 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39133 +
39134 +/* ---------------------------------------------------------------------- */
39135 +
39136 +#ifdef __KERNEL__
39137 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39138 +typedef int8_t aufs_bindex_t;
39139 +#define AUFS_BRANCH_MAX 127
39140 +#else
39141 +typedef int16_t aufs_bindex_t;
39142 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39143 +#define AUFS_BRANCH_MAX 511
39144 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39145 +#define AUFS_BRANCH_MAX 1023
39146 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39147 +#define AUFS_BRANCH_MAX 32767
39148 +#endif
39149 +#endif
39150 +
39151 +#ifndef AUFS_BRANCH_MAX
39152 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39153 +#endif
39154 +#endif /* __KERNEL__ */
39155 +
39156 +/* ---------------------------------------------------------------------- */
39157 +
39158 +#define AUFS_FSTYPE            AUFS_NAME
39159 +
39160 +#define AUFS_ROOT_INO          2
39161 +#define AUFS_FIRST_INO         11
39162 +
39163 +#define AUFS_WH_PFX            ".wh."
39164 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39165 +#define AUFS_WH_TMP_LEN                4
39166 +/* a limit for rmdir/rename a dir and copyup */
39167 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39168 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39169 +                               - 1                     /* dot */\
39170 +                               - AUFS_WH_TMP_LEN)      /* hex */
39171 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39172 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39173 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39174 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39175 +#define AUFS_DIRWH_DEF         3
39176 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39177 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39178 +#define AUFS_RDBLK_DEF         512 /* bytes */
39179 +#define AUFS_RDHASH_DEF                32
39180 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39181 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39182 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39183 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39184 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39185 +
39186 +/* pseudo-link maintenace under /proc */
39187 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39188 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39189 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39190 +
39191 +/* dirren, renamed dir */
39192 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39193 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39194 +/* whiteouted doubly */
39195 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39196 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39197 +
39198 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39199 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39200 +
39201 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39202 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39203 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39204 +
39205 +/* doubly whiteouted */
39206 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39207 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39208 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39209 +
39210 +/* branch permissions and attributes */
39211 +#define AUFS_BRPERM_RW         "rw"
39212 +#define AUFS_BRPERM_RO         "ro"
39213 +#define AUFS_BRPERM_RR         "rr"
39214 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39215 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39216 +#define AUFS_BRATTR_FHSM       "fhsm"
39217 +#define AUFS_BRATTR_UNPIN      "unpin"
39218 +#define AUFS_BRATTR_ICEX       "icex"
39219 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39220 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39221 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39222 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39223 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39224 +#define AUFS_BRRATTR_WH                "wh"
39225 +#define AUFS_BRWATTR_NLWH      "nolwh"
39226 +#define AUFS_BRWATTR_MOO       "moo"
39227 +
39228 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39229 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39230 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39231 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39232 +
39233 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39234 +#define AuBrAttr_COO_ALL       (1 << 4)
39235 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39236 +
39237 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39238 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39239 +                                                  branch. meaningless since
39240 +                                                  linux-3.18-rc1 */
39241 +
39242 +/* ignore error in copying XATTR */
39243 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39244 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39245 +#define AuBrAttr_ICEX_TR       (1 << 9)
39246 +#define AuBrAttr_ICEX_USR      (1 << 10)
39247 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39248 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39249 +                                | AuBrAttr_ICEX_SYS    \
39250 +                                | AuBrAttr_ICEX_TR     \
39251 +                                | AuBrAttr_ICEX_USR    \
39252 +                                | AuBrAttr_ICEX_OTH)
39253 +
39254 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39255 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39256 +
39257 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39258 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39259 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39260 +
39261 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39262 +
39263 +/* #warning test userspace */
39264 +#ifdef __KERNEL__
39265 +#ifndef CONFIG_AUFS_FHSM
39266 +#undef AuBrAttr_FHSM
39267 +#define AuBrAttr_FHSM          0
39268 +#endif
39269 +#ifndef CONFIG_AUFS_XATTR
39270 +#undef AuBrAttr_ICEX
39271 +#define AuBrAttr_ICEX          0
39272 +#undef AuBrAttr_ICEX_SEC
39273 +#define AuBrAttr_ICEX_SEC      0
39274 +#undef AuBrAttr_ICEX_SYS
39275 +#define AuBrAttr_ICEX_SYS      0
39276 +#undef AuBrAttr_ICEX_TR
39277 +#define AuBrAttr_ICEX_TR       0
39278 +#undef AuBrAttr_ICEX_USR
39279 +#define AuBrAttr_ICEX_USR      0
39280 +#undef AuBrAttr_ICEX_OTH
39281 +#define AuBrAttr_ICEX_OTH      0
39282 +#endif
39283 +#endif
39284 +
39285 +/* the longest combination */
39286 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39287 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39288 +                              "+" AUFS_BRATTR_COO_REG          \
39289 +                              "+" AUFS_BRATTR_FHSM             \
39290 +                              "+" AUFS_BRATTR_UNPIN            \
39291 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39292 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39293 +                              "+" AUFS_BRATTR_ICEX_USR         \
39294 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39295 +                              "+" AUFS_BRWATTR_NLWH)
39296 +
39297 +typedef struct {
39298 +       char a[AuBrPermStrSz];
39299 +} au_br_perm_str_t;
39300 +
39301 +static inline int au_br_writable(int brperm)
39302 +{
39303 +       return brperm & AuBrPerm_RW;
39304 +}
39305 +
39306 +static inline int au_br_whable(int brperm)
39307 +{
39308 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39309 +}
39310 +
39311 +static inline int au_br_wh_linkable(int brperm)
39312 +{
39313 +       return !(brperm & AuBrWAttr_NoLinkWH);
39314 +}
39315 +
39316 +static inline int au_br_cmoo(int brperm)
39317 +{
39318 +       return brperm & AuBrAttr_CMOO_Mask;
39319 +}
39320 +
39321 +static inline int au_br_fhsm(int brperm)
39322 +{
39323 +       return brperm & AuBrAttr_FHSM;
39324 +}
39325 +
39326 +/* ---------------------------------------------------------------------- */
39327 +
39328 +/* ioctl */
39329 +enum {
39330 +       /* readdir in userspace */
39331 +       AuCtl_RDU,
39332 +       AuCtl_RDU_INO,
39333 +
39334 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39335 +       AuCtl_IBUSY,    /* busy inode */
39336 +       AuCtl_MVDOWN,   /* move-down */
39337 +       AuCtl_BR,       /* info about branches */
39338 +       AuCtl_FHSM_FD   /* connection for fhsm */
39339 +};
39340 +
39341 +/* borrowed from linux/include/linux/kernel.h */
39342 +#ifndef ALIGN
39343 +#ifdef _GNU_SOURCE
39344 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39345 +#else
39346 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39347 +#endif
39348 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39349 +#endif
39350 +
39351 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39352 +#ifndef __aligned
39353 +#define __aligned(x)                   __attribute__((aligned(x)))
39354 +#endif
39355 +
39356 +#ifdef __KERNEL__
39357 +#ifndef __packed
39358 +#define __packed                       __attribute__((packed))
39359 +#endif
39360 +#endif
39361 +
39362 +struct au_rdu_cookie {
39363 +       uint64_t        h_pos;
39364 +       int16_t         bindex;
39365 +       uint8_t         flags;
39366 +       uint8_t         pad;
39367 +       uint32_t        generation;
39368 +} __aligned(8);
39369 +
39370 +struct au_rdu_ent {
39371 +       uint64_t        ino;
39372 +       int16_t         bindex;
39373 +       uint8_t         type;
39374 +       uint8_t         nlen;
39375 +       uint8_t         wh;
39376 +       char            name[];
39377 +} __aligned(8);
39378 +
39379 +static inline int au_rdu_len(int nlen)
39380 +{
39381 +       /* include the terminating NULL */
39382 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39383 +                    sizeof(uint64_t));
39384 +}
39385 +
39386 +union au_rdu_ent_ul {
39387 +       struct au_rdu_ent __user        *e;
39388 +       uint64_t                        ul;
39389 +};
39390 +
39391 +enum {
39392 +       AufsCtlRduV_SZ,
39393 +       AufsCtlRduV_End
39394 +};
39395 +
39396 +struct aufs_rdu {
39397 +       /* input */
39398 +       union {
39399 +               uint64_t        sz;     /* AuCtl_RDU */
39400 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39401 +       };
39402 +       union au_rdu_ent_ul     ent;
39403 +       uint16_t                verify[AufsCtlRduV_End];
39404 +
39405 +       /* input/output */
39406 +       uint32_t                blk;
39407 +
39408 +       /* output */
39409 +       union au_rdu_ent_ul     tail;
39410 +       /* number of entries which were added in a single call */
39411 +       uint64_t                rent;
39412 +       uint8_t                 full;
39413 +       uint8_t                 shwh;
39414 +
39415 +       struct au_rdu_cookie    cookie;
39416 +} __aligned(8);
39417 +
39418 +/* ---------------------------------------------------------------------- */
39419 +
39420 +/* dirren. the branch is identified by the filename who contains this */
39421 +struct au_drinfo {
39422 +       uint64_t ino;
39423 +       union {
39424 +               uint8_t oldnamelen;
39425 +               uint64_t _padding;
39426 +       };
39427 +       uint8_t oldname[];
39428 +} __aligned(8);
39429 +
39430 +struct au_drinfo_fdata {
39431 +       uint32_t magic;
39432 +       struct au_drinfo drinfo;
39433 +} __aligned(8);
39434 +
39435 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39436 +/* future */
39437 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39438 +
39439 +/* ---------------------------------------------------------------------- */
39440 +
39441 +struct aufs_wbr_fd {
39442 +       uint32_t        oflags;
39443 +       int16_t         brid;
39444 +} __aligned(8);
39445 +
39446 +/* ---------------------------------------------------------------------- */
39447 +
39448 +struct aufs_ibusy {
39449 +       uint64_t        ino, h_ino;
39450 +       int16_t         bindex;
39451 +} __aligned(8);
39452 +
39453 +/* ---------------------------------------------------------------------- */
39454 +
39455 +/* error code for move-down */
39456 +/* the actual message strings are implemented in aufs-util.git */
39457 +enum {
39458 +       EAU_MVDOWN_OPAQUE = 1,
39459 +       EAU_MVDOWN_WHITEOUT,
39460 +       EAU_MVDOWN_UPPER,
39461 +       EAU_MVDOWN_BOTTOM,
39462 +       EAU_MVDOWN_NOUPPER,
39463 +       EAU_MVDOWN_NOLOWERBR,
39464 +       EAU_Last
39465 +};
39466 +
39467 +/* flags for move-down */
39468 +#define AUFS_MVDOWN_DMSG       1
39469 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39470 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39471 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39472 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39473 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39474 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39475 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39476 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39477 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39478 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39479 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39480 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39481 +
39482 +/* index for move-down */
39483 +enum {
39484 +       AUFS_MVDOWN_UPPER,
39485 +       AUFS_MVDOWN_LOWER,
39486 +       AUFS_MVDOWN_NARRAY
39487 +};
39488 +
39489 +/*
39490 + * additional info of move-down
39491 + * number of free blocks and inodes.
39492 + * subset of struct kstatfs, but smaller and always 64bit.
39493 + */
39494 +struct aufs_stfs {
39495 +       uint64_t        f_blocks;
39496 +       uint64_t        f_bavail;
39497 +       uint64_t        f_files;
39498 +       uint64_t        f_ffree;
39499 +};
39500 +
39501 +struct aufs_stbr {
39502 +       int16_t                 brid;   /* optional input */
39503 +       int16_t                 bindex; /* output */
39504 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39505 +} __aligned(8);
39506 +
39507 +struct aufs_mvdown {
39508 +       uint32_t                flags;                  /* input/output */
39509 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39510 +       int8_t                  au_errno;               /* output */
39511 +} __aligned(8);
39512 +
39513 +/* ---------------------------------------------------------------------- */
39514 +
39515 +union aufs_brinfo {
39516 +       /* PATH_MAX may differ between kernel-space and user-space */
39517 +       char    _spacer[4096];
39518 +       struct {
39519 +               int16_t id;
39520 +               int     perm;
39521 +               char    path[];
39522 +       };
39523 +} __aligned(8);
39524 +
39525 +/* ---------------------------------------------------------------------- */
39526 +
39527 +#define AuCtlType              'A'
39528 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39529 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39530 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39531 +                                    struct aufs_wbr_fd)
39532 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39533 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39534 +                                     struct aufs_mvdown)
39535 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39536 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39537 +
39538 +#endif /* __AUFS_TYPE_H__ */
39539 SPDX-License-Identifier: GPL-2.0
39540 aufs6.x-rcN loopback patch
39541
39542 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39543 index 333b3915fe86..1eb5bde57477 100644
39544 --- a/drivers/block/loop.c
39545 +++ b/drivers/block/loop.c
39546 @@ -54,7 +54,7 @@ struct loop_device {
39547         int             lo_flags;
39548         char            lo_file_name[LO_NAME_SIZE];
39549  
39550 -       struct file *   lo_backing_file;
39551 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39552         struct block_device *lo_device;
39553  
39554         gfp_t           old_gfp_mask;
39555 @@ -506,6 +506,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39556                                 lo->use_dio);
39557  }
39558  
39559 +static struct file *loop_real_file(struct file *file)
39560 +{
39561 +       struct file *f = NULL;
39562 +
39563 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39564 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39565 +       return f;
39566 +}
39567 +
39568  static void loop_reread_partitions(struct loop_device *lo)
39569  {
39570         int rc;
39571 @@ -563,6 +572,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39572  {
39573         struct file *file = fget(arg);
39574         struct file *old_file;
39575 +       struct file *f, *virt_file = NULL, *old_virt_file;
39576         int error;
39577         bool partscan;
39578         bool is_loop;
39579 @@ -586,11 +596,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39580         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39581                 goto out_err;
39582  
39583 +       f = loop_real_file(file);
39584 +       if (f) {
39585 +               virt_file = file;
39586 +               file = f;
39587 +               get_file(file);
39588 +       }
39589 +
39590         error = loop_validate_file(file, bdev);
39591         if (error)
39592                 goto out_err;
39593  
39594         old_file = lo->lo_backing_file;
39595 +       old_virt_file = lo->lo_backing_virt_file;
39596  
39597         error = -EINVAL;
39598  
39599 @@ -603,6 +621,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39600         blk_mq_freeze_queue(lo->lo_queue);
39601         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39602         lo->lo_backing_file = file;
39603 +       lo->lo_backing_virt_file = virt_file;
39604         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39605         mapping_set_gfp_mask(file->f_mapping,
39606                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39607 @@ -625,6 +644,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39608          * dependency.
39609          */
39610         fput(old_file);
39611 +       if (old_virt_file)
39612 +               fput(old_virt_file);
39613         if (partscan)
39614                 loop_reread_partitions(lo);
39615  
39616 @@ -638,6 +659,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39617         loop_global_unlock(lo, is_loop);
39618  out_putf:
39619         fput(file);
39620 +       if (virt_file)
39621 +               fput(virt_file);
39622         goto done;
39623  }
39624  
39625 @@ -1009,6 +1032,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39626                           const struct loop_config *config)
39627  {
39628         struct file *file = fget(config->fd);
39629 +       struct file *f, *virt_file = NULL;
39630         struct inode *inode;
39631         struct address_space *mapping;
39632         int error;
39633 @@ -1024,6 +1048,13 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39634         /* This is safe, since we have a reference from open(). */
39635         __module_get(THIS_MODULE);
39636  
39637 +       f = loop_real_file(file);
39638 +       if (f) {
39639 +               virt_file = file;
39640 +               file = f;
39641 +               get_file(file);
39642 +       }
39643 +
39644         /*
39645          * If we don't hold exclusive handle for the device, upgrade to it
39646          * here to avoid changing device under exclusive owner.
39647 @@ -1087,6 +1118,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39648         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39649         lo->lo_device = bdev;
39650         lo->lo_backing_file = file;
39651 +       lo->lo_backing_virt_file = virt_file;
39652         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39653         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39654  
39655 @@ -1142,6 +1174,8 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39656                 bd_abort_claiming(bdev, loop_configure);
39657  out_putf:
39658         fput(file);
39659 +       if (virt_file)
39660 +               fput(virt_file);
39661         /* This is safe: open() is still holding a reference. */
39662         module_put(THIS_MODULE);
39663         return error;
39664 @@ -1150,6 +1184,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
39665  static void __loop_clr_fd(struct loop_device *lo, bool release)
39666  {
39667         struct file *filp;
39668 +       struct file *virt_filp = lo->lo_backing_virt_file;
39669         gfp_t gfp = lo->old_gfp_mask;
39670  
39671         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39672 @@ -1166,6 +1201,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39673         spin_lock_irq(&lo->lo_lock);
39674         filp = lo->lo_backing_file;
39675         lo->lo_backing_file = NULL;
39676 +       lo->lo_backing_virt_file = NULL;
39677         spin_unlock_irq(&lo->lo_lock);
39678  
39679         lo->lo_device = NULL;
39680 @@ -1228,6 +1264,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39681          * fput can take open_mutex which is usually taken before lo_mutex.
39682          */
39683         fput(filp);
39684 +       if (virt_filp)
39685 +               fput(virt_filp);
39686  }
39687  
39688  static int loop_clr_fd(struct loop_device *lo)
39689 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39690 index fa8a517ffd0c..c18f7bcef81b 100644
39691 --- a/fs/aufs/f_op.c
39692 +++ b/fs/aufs/f_op.c
39693 @@ -311,7 +311,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39694         if (IS_ERR(h_file))
39695                 goto out;
39696  
39697 -       if (au_test_loopback_kthread()) {
39698 +       if (0 && au_test_loopback_kthread()) {
39699                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39700                 if (file->f_mapping != h_file->f_mapping) {
39701                         file->f_mapping = h_file->f_mapping;
39702 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39703 index 58043e31e5f3..e2bfae6f9d59 100644
39704 --- a/fs/aufs/loop.c
39705 +++ b/fs/aufs/loop.c
39706 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39707                 symbol_put(loop_backing_file);
39708         au_kfree_try_rcu(au_warn_loopback_array);
39709  }
39710 +
39711 +/* ---------------------------------------------------------------------- */
39712 +
39713 +/* support the loopback block device insude aufs */
39714 +
39715 +struct file *aufs_real_loop(struct file *file)
39716 +{
39717 +       struct file *f;
39718 +
39719 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39720 +       fi_read_lock(file);
39721 +       f = au_hf_top(file);
39722 +       fi_read_unlock(file);
39723 +       AuDebugOn(!f);
39724 +       return f;
39725 +}
39726 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39727 index 03d4908a6c03..34d356e181d5 100644
39728 --- a/fs/aufs/loop.h
39729 +++ b/fs/aufs/loop.h
39730 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39731  
39732  int au_loopback_init(void);
39733  void au_loopback_fin(void);
39734 +
39735 +struct file *aufs_real_loop(struct file *file);
39736  #else
39737  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39738  
39739 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39740  
39741  AuStubInt0(au_loopback_init, void)
39742  AuStubVoid(au_loopback_fin, void)
39743 +
39744 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39745  #endif /* BLK_DEV_LOOP */
39746  
39747  #endif /* __KERNEL__ */
39748 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39749 index 07d3412e950f..c4a00f620e57 100644
39750 --- a/fs/aufs/super.c
39751 +++ b/fs/aufs/super.c
39752 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39753         .show_options   = aufs_show_options,
39754         .statfs         = aufs_statfs,
39755         .put_super      = aufs_put_super,
39756 -       .sync_fs        = aufs_sync_fs
39757 +       .sync_fs        = aufs_sync_fs,
39758 +#ifdef CONFIG_AUFS_BDEV_LOOP
39759 +       .real_loop      = aufs_real_loop
39760 +#endif
39761  };
39762  
39763  /* ---------------------------------------------------------------------- */
39764 diff --git a/include/linux/fs.h b/include/linux/fs.h
39765 index 96ca249ee1e6..b21abb1a2134 100644
39766 --- a/include/linux/fs.h
39767 +++ b/include/linux/fs.h
39768 @@ -2165,6 +2165,11 @@ struct super_operations {
39769         long (*free_cached_objects)(struct super_block *,
39770                                     struct shrink_control *);
39771         void (*shutdown)(struct super_block *sb);
39772 +
39773 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39774 +       /* and aufs */
39775 +       struct file *(*real_loop)(struct file *);
39776 +#endif
39777  };
39778  
39779  /*
This page took 3.147995 seconds and 3 git commands to generate.