]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs.patch
- 6.1.47
[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 2685a4d0d353..a2df3d4bb3aa 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -319,6 +319,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 4dea17840761..9795cbfdd0bf 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -137,3 +137,4 @@ obj-$(CONFIG_EFIVAR_FS)             += efivarfs/
21  obj-$(CONFIG_EROFS_FS)         += erofs/
22  obj-$(CONFIG_VBOXSF_FS)                += vboxsf/
23  obj-$(CONFIG_ZONEFS_FS)                += zonefs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs6.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 379945f82a64..b3b8fbca497b 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3455,6 +3455,19 @@ F:       include/uapi/linux/audit.h
33  F:     kernel/audit*
34  F:     lib/*audit.c
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
40 +S:     Supported
41 +W:     http://aufs.sourceforge.net
42 +T:     git://github.com/sfjro/aufs4-linux.git
43 +F:     Documentation/ABI/testing/debugfs-aufs
44 +F:     Documentation/ABI/testing/sysfs-aufs
45 +F:     Documentation/filesystems/aufs/
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY DISPLAY DRIVERS
50  M:     Miguel Ojeda <ojeda@kernel.org>
51  S:     Maintained
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index ad92192c7d61..8ca56da3ca69 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -645,6 +645,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57         goto done;
58  }
59  
60 +/*
61 + * for AUFS
62 + * no get/put for file.
63 + */
64 +struct file *loop_backing_file(struct super_block *sb)
65 +{
66 +       struct file *ret;
67 +       struct loop_device *l;
68 +
69 +       ret = NULL;
70 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71 +               l = sb->s_bdev->bd_disk->private_data;
72 +               ret = l->lo_backing_file;
73 +       }
74 +       return ret;
75 +}
76 +EXPORT_SYMBOL_GPL(loop_backing_file);
77 +
78  /* loop sysfs attributes */
79  
80  static ssize_t loop_attr_show(struct device *dev, char *page,
81 diff --git a/fs/dcache.c b/fs/dcache.c
82 index 52e6d5fdab6b..519321f32f95 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1345,7 +1345,7 @@ enum d_walk_ret {
86   *
87   * The @enter() callbacks are called with d_lock held.
88   */
89 -static void d_walk(struct dentry *parent, void *data,
90 +void d_walk(struct dentry *parent, void *data,
91                    enum d_walk_ret (*enter)(void *, struct dentry *))
92  {
93         struct dentry *this_parent;
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index 146c9ab0cd4b..c980a0524118 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -33,7 +33,7 @@
99  
100  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101  
102 -static int setfl(int fd, struct file * filp, unsigned long arg)
103 +int setfl(int fd, struct file *filp, unsigned long arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/namespace.c b/fs/namespace.c
117 index df137ba19d37..faae7f8644ac 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -852,6 +852,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 0878b852b355..d83f9e5cddbc 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -756,8 +756,8 @@ static int warn_unsupported(struct file *file, const char *op)
138  /*
139   * Attempt to initiate a splice from pipe to file.
140   */
141 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142 -                          loff_t *ppos, size_t len, unsigned int flags)
143 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
144 +                   loff_t *ppos, size_t len, unsigned int flags)
145  {
146         if (unlikely(!out->f_op->splice_write))
147                 return warn_unsupported(out, "write");
148 @@ -767,9 +767,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
149  /*
150   * Attempt to initiate a splice from a file to a pipe.
151   */
152 -static long do_splice_to(struct file *in, loff_t *ppos,
153 -                        struct pipe_inode_info *pipe, size_t len,
154 -                        unsigned int flags)
155 +long do_splice_to(struct file *in, loff_t *ppos,
156 +                 struct pipe_inode_info *pipe, size_t len,
157 +                 unsigned int flags)
158  {
159         unsigned int p_space;
160         int ret;
161 diff --git a/include/linux/fs.h b/include/linux/fs.h
162 index e654435f1651..433a4f08dab4 100644
163 --- a/include/linux/fs.h
164 +++ b/include/linux/fs.h
165 @@ -1364,6 +1364,7 @@ extern void fasync_free(struct fasync_struct *);
166  /* can be called from interrupts */
167  extern void kill_fasync(struct fasync_struct **, int, int);
168  
169 +extern int setfl(int fd, struct file *filp, unsigned long arg);
170  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
171  extern int f_setown(struct file *filp, unsigned long arg, int force);
172  extern void f_delown(struct file *filp);
173 @@ -2117,6 +2118,7 @@ struct file_operations {
174         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
175         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
176         int (*check_flags)(int);
177 +       int (*setfl)(struct file *, unsigned long);
178         int (*flock) (struct file *, int, struct file_lock *);
179         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
180         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
181 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
182 index 1f1099dac3f0..81d10b8e9dc0 100644
183 --- a/include/linux/lockdep.h
184 +++ b/include/linux/lockdep.h
185 @@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
186         return lock->key == key;
187  }
188  
189 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
190 +
191  /*
192   * Acquire a lock.
193   *
194 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
195 index 8f882f5881e8..6b9808f09843 100644
196 --- a/include/linux/mnt_namespace.h
197 +++ b/include/linux/mnt_namespace.h
198 @@ -7,12 +7,15 @@ struct mnt_namespace;
199  struct fs_struct;
200  struct user_namespace;
201  struct ns_common;
202 +struct vfsmount;
203  
204  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
205                 struct user_namespace *, struct fs_struct *);
206  extern void put_mnt_ns(struct mnt_namespace *ns);
207  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
208  
209 +extern int is_current_mnt_ns(struct vfsmount *mnt);
210 +
211  extern const struct file_operations proc_mounts_operations;
212  extern const struct file_operations proc_mountinfo_operations;
213  extern const struct file_operations proc_mountstats_operations;
214 diff --git a/include/linux/splice.h b/include/linux/splice.h
215 index a55179fd60fc..8e21c53cf883 100644
216 --- a/include/linux/splice.h
217 +++ b/include/linux/splice.h
218 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
219  
220  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
221  extern const struct pipe_buf_operations default_pipe_buf_ops;
222 +
223 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
224 +                          loff_t *ppos, size_t len, unsigned int flags);
225 +extern long do_splice_to(struct file *in, loff_t *ppos,
226 +                        struct pipe_inode_info *pipe, size_t len,
227 +                        unsigned int flags);
228  #endif
229 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
230 index e3375bc40dad..c1f0aadde539 100644
231 --- a/kernel/locking/lockdep.c
232 +++ b/kernel/locking/lockdep.c
233 @@ -217,7 +217,7 @@ unsigned long max_lock_class_idx;
234  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
235  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
236  
237 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
238 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
239  {
240         unsigned int class_idx = hlock->class_idx;
241  
242 @@ -238,6 +238,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
243          */
244         return lock_classes + class_idx;
245  }
246 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
247  
248  #ifdef CONFIG_LOCK_STAT
249  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
250 SPDX-License-Identifier: GPL-2.0
251 aufs6.x-rcN mmap patch
252
253 diff --git a/fs/proc/base.c b/fs/proc/base.c
254 index 9e479d7d202b..986c2ae12f8b 100644
255 --- a/fs/proc/base.c
256 +++ b/fs/proc/base.c
257 @@ -2218,7 +2218,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
258         rc = -ENOENT;
259         vma = find_exact_vma(mm, vm_start, vm_end);
260         if (vma && vma->vm_file) {
261 -               *path = vma->vm_file->f_path;
262 +               *path = vma_pr_or_file(vma)->f_path;
263                 path_get(path);
264                 rc = 0;
265         }
266 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
267 index 4d3493579458..42edd9a42c78 100644
268 --- a/fs/proc/nommu.c
269 +++ b/fs/proc/nommu.c
270 @@ -39,7 +39,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
271         file = region->vm_file;
272  
273         if (file) {
274 -               struct inode *inode = file_inode(region->vm_file);
275 +               struct inode *inode;
276 +
277 +               file = vmr_pr_or_file(region);
278 +               inode = file_inode(file);
279                 dev = inode->i_sb->s_dev;
280                 ino = inode->i_ino;
281         }
282 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
283 index 8a74cdcc9af0..c05071b53f5b 100644
284 --- a/fs/proc/task_mmu.c
285 +++ b/fs/proc/task_mmu.c
286 @@ -284,7 +284,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
287         const char *name = NULL;
288  
289         if (file) {
290 -               struct inode *inode = file_inode(vma->vm_file);
291 +               struct inode *inode;
292 +
293 +               file = vma_pr_or_file(vma);
294 +               inode = file_inode(file);
295                 dev = inode->i_sb->s_dev;
296                 ino = inode->i_ino;
297                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
298 @@ -1934,7 +1937,7 @@ static int show_numa_map(struct seq_file *m, void *v)
299         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
300         struct vm_area_struct *vma = v;
301         struct numa_maps *md = &numa_priv->md;
302 -       struct file *file = vma->vm_file;
303 +       struct file *file = vma_pr_or_file(vma);
304         struct mm_struct *mm = vma->vm_mm;
305         struct mempolicy *pol;
306         char buffer[64];
307 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
308 index 2fd06f52b6a4..b01030da424f 100644
309 --- a/fs/proc/task_nommu.c
310 +++ b/fs/proc/task_nommu.c
311 @@ -150,7 +150,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
312         file = vma->vm_file;
313  
314         if (file) {
315 -               struct inode *inode = file_inode(vma->vm_file);
316 +               struct inode *inode;
317 +
318 +               file = vma_pr_or_file(vma);
319 +               inode = file_inode(file);
320                 dev = inode->i_sb->s_dev;
321                 ino = inode->i_ino;
322                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
323 diff --git a/include/linux/mm.h b/include/linux/mm.h
324 index 8bbcccbc5565..d5006a1f4271 100644
325 --- a/include/linux/mm.h
326 +++ b/include/linux/mm.h
327 @@ -1933,6 +1933,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
328         unmap_mapping_range(mapping, holebegin, holelen, 0);
329  }
330  
331 +#if IS_ENABLED(CONFIG_AUFS_FS)
332 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
333 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
334 +                                     int);
335 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
336 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
337 +
338 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
339 +                                                               __LINE__)
340 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
341 +                                                         __LINE__)
342 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
343 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
344 +
345 +#ifndef CONFIG_MMU
346 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
347 +extern void vmr_do_fput(struct vm_region *, const char[], int);
348 +
349 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
350 +                                                         __LINE__)
351 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
352 +#endif /* !CONFIG_MMU */
353 +
354 +#else
355 +
356 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
357 +#define vma_pr_or_file(vma)            (vma)->vm_file
358 +#define vma_get_file(vma)              get_file((vma)->vm_file)
359 +#define vma_fput(vma)                  fput((vma)->vm_file)
360 +
361 +#ifndef CONFIG_MMU
362 +#define vmr_pr_or_file(region)         (region)->vm_file
363 +#define vmr_fput(region)               fput((region)->vm_file)
364 +#endif /* !CONFIG_MMU */
365 +
366 +#endif /* CONFIG_AUFS_FS */
367 +
368  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
369                 void *buf, int len, unsigned int gup_flags);
370  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
371 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
372 index 500e536796ca..ad766e2110ca 100644
373 --- a/include/linux/mm_types.h
374 +++ b/include/linux/mm_types.h
375 @@ -413,6 +413,9 @@ struct vm_region {
376         unsigned long   vm_top;         /* region allocated to here */
377         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
378         struct file     *vm_file;       /* the backing file or NULL */
379 +#if IS_ENABLED(CONFIG_AUFS_FS)
380 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
381 +#endif
382  
383         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
384         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
385 @@ -494,6 +497,9 @@ struct vm_area_struct {
386         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
387                                            units */
388         struct file * vm_file;          /* File we map to (can be NULL). */
389 +#if IS_ENABLED(CONFIG_AUFS_FS)
390 +       struct file *vm_prfile;         /* shadow of vm_file */
391 +#endif
392         void * vm_private_data;         /* was vm_pte (shared mem) */
393  
394  #ifdef CONFIG_SWAP
395 diff --git a/kernel/fork.c b/kernel/fork.c
396 index 08969f5aa38d..61871700033a 100644
397 --- a/kernel/fork.c
398 +++ b/kernel/fork.c
399 @@ -661,7 +661,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
400                 if (file) {
401                         struct address_space *mapping = file->f_mapping;
402  
403 -                       get_file(file);
404 +                       vma_get_file(tmp);
405                         i_mmap_lock_write(mapping);
406                         if (tmp->vm_flags & VM_SHARED)
407                                 mapping_allow_writable(mapping);
408 diff --git a/mm/Makefile b/mm/Makefile
409 index 8e105e5b3e29..c218d042dbce 100644
410 --- a/mm/Makefile
411 +++ b/mm/Makefile
412 @@ -138,3 +138,4 @@ obj-$(CONFIG_IO_MAPPING) += io-mapping.o
413  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
414  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
415  obj-$(CONFIG_SHRINKER_DEBUG) += shrinker_debug.o
416 +obj-$(CONFIG_AUFS_FS:m=y) += prfile.o
417 diff --git a/mm/filemap.c b/mm/filemap.c
418 index 08341616ae7a..410ca60fbf39 100644
419 --- a/mm/filemap.c
420 +++ b/mm/filemap.c
421 @@ -3418,7 +3418,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
422         vm_fault_t ret = VM_FAULT_LOCKED;
423  
424         sb_start_pagefault(mapping->host->i_sb);
425 -       file_update_time(vmf->vma->vm_file);
426 +       vma_file_update_time(vmf->vma);
427         folio_lock(folio);
428         if (folio->mapping != mapping) {
429                 folio_unlock(folio);
430 diff --git a/mm/mmap.c b/mm/mmap.c
431 index 2def55555e05..fefc5c67e96e 100644
432 --- a/mm/mmap.c
433 +++ b/mm/mmap.c
434 @@ -139,7 +139,7 @@ static void remove_vma(struct vm_area_struct *vma)
435         if (vma->vm_ops && vma->vm_ops->close)
436                 vma->vm_ops->close(vma);
437         if (vma->vm_file)
438 -               fput(vma->vm_file);
439 +               vma_fput(vma);
440         mpol_put(vma_policy(vma));
441         vm_area_free(vma);
442  }
443 @@ -590,7 +590,7 @@ inline int vma_expand(struct ma_state *mas, struct vm_area_struct *vma,
444         if (remove_next) {
445                 if (file) {
446                         uprobe_munmap(next, next->vm_start, next->vm_end);
447 -                       fput(file);
448 +                       vma_fput(vma);
449                 }
450                 if (next->anon_vma)
451                         anon_vma_merge(vma, next);
452 @@ -831,7 +831,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
453  again:
454                 if (file) {
455                         uprobe_munmap(next, next->vm_start, next->vm_end);
456 -                       fput(file);
457 +                       vma_fput(vma);
458                 }
459                 if (next->anon_vma)
460                         anon_vma_merge(vma, next);
461 @@ -2228,7 +2228,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
462                 goto out_free_mpol;
463  
464         if (new->vm_file)
465 -               get_file(new->vm_file);
466 +               vma_get_file(new);
467  
468         if (new->vm_ops && new->vm_ops->open)
469                 new->vm_ops->open(new);
470 @@ -2250,7 +2250,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
471         if (new->vm_ops && new->vm_ops->close)
472                 new->vm_ops->close(new);
473         if (new->vm_file)
474 -               fput(new->vm_file);
475 +               vma_fput(new);
476         unlink_anon_vmas(new);
477   out_free_mpol:
478         mpol_put(vma_policy(new));
479 @@ -2746,7 +2746,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
480         if (vma->vm_ops && vma->vm_ops->close)
481                 vma->vm_ops->close(vma);
482  unmap_and_free_vma:
483 -       fput(vma->vm_file);
484 +       vma_fput(vma);
485         vma->vm_file = NULL;
486  
487         /* Undo any partial mapping done by a device driver. */
488 @@ -2813,6 +2813,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
489         unsigned long populate = 0;
490         unsigned long ret = -EINVAL;
491         struct file *file;
492 +#if IS_ENABLED(CONFIG_AUFS_FS)
493 +       struct file *prfile;
494 +#endif
495  
496         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/mm/remap_file_pages.rst.\n",
497                      current->comm, current->pid);
498 @@ -2871,10 +2874,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
499         if (vma->vm_flags & VM_LOCKED)
500                 flags |= MAP_LOCKED;
501  
502 +#if IS_ENABLED(CONFIG_AUFS_FS)
503 +       vma_get_file(vma);
504 +       file = vma->vm_file;
505 +       prfile = vma->vm_prfile;
506 +       ret = do_mmap(vma->vm_file, start, size,
507 +                       prot, flags, pgoff, &populate, NULL);
508 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
509 +               struct vm_area_struct *new_vma;
510 +
511 +               new_vma = find_vma(mm, ret);
512 +               if (!new_vma->vm_prfile)
513 +                       new_vma->vm_prfile = prfile;
514 +               if (new_vma != vma)
515 +                       get_file(prfile);
516 +       }
517 +       /*
518 +        * two fput()s instead of vma_fput(vma),
519 +        * coz vma may not be available anymore.
520 +        */
521 +       fput(file);
522 +       if (prfile)
523 +               fput(prfile);
524 +#else
525         file = get_file(vma->vm_file);
526         ret = do_mmap(vma->vm_file, start, size,
527                         prot, flags, pgoff, &populate, NULL);
528         fput(file);
529 +#endif /* CONFIG_AUFS_FS */
530  out:
531         mmap_write_unlock(mm);
532         if (populate)
533 @@ -3227,7 +3254,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
534                 if (anon_vma_clone(new_vma, vma))
535                         goto out_free_mempol;
536                 if (new_vma->vm_file)
537 -                       get_file(new_vma->vm_file);
538 +                       vma_get_file(new_vma);
539                 if (new_vma->vm_ops && new_vma->vm_ops->open)
540                         new_vma->vm_ops->open(new_vma);
541                 if (vma_link(mm, new_vma))
542 diff --git a/mm/nommu.c b/mm/nommu.c
543 index 214c70e1d059..dccbb939d2a9 100644
544 --- a/mm/nommu.c
545 +++ b/mm/nommu.c
546 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
547                 up_write(&nommu_region_sem);
548  
549                 if (region->vm_file)
550 -                       fput(region->vm_file);
551 +                       vmr_fput(region);
552  
553                 /* IO memory and memory shared directly out of the pagecache
554                  * from ramfs/tmpfs mustn't be released here */
555 @@ -653,7 +653,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
556         if (vma->vm_ops && vma->vm_ops->close)
557                 vma->vm_ops->close(vma);
558         if (vma->vm_file)
559 -               fput(vma->vm_file);
560 +               vma_fput(vma);
561         put_nommu_region(vma->vm_region);
562         vm_area_free(vma);
563  }
564 @@ -1164,7 +1164,7 @@ unsigned long do_mmap(struct file *file,
565                                         goto error_just_free;
566                                 }
567                         }
568 -                       fput(region->vm_file);
569 +                       vmr_fput(region);
570                         kmem_cache_free(vm_region_jar, region);
571                         region = pregion;
572                         result = start;
573 @@ -1241,10 +1241,10 @@ unsigned long do_mmap(struct file *file,
574  error:
575         mas_destroy(&mas);
576         if (region->vm_file)
577 -               fput(region->vm_file);
578 +               vmr_fput(region);
579         kmem_cache_free(vm_region_jar, region);
580         if (vma->vm_file)
581 -               fput(vma->vm_file);
582 +               vma_fput(vma);
583         vm_area_free(vma);
584         return ret;
585  
586 diff --git a/mm/prfile.c b/mm/prfile.c
587 new file mode 100644
588 index 000000000000..8f820a235364
589 --- /dev/null
590 +++ b/mm/prfile.c
591 @@ -0,0 +1,86 @@
592 +// SPDX-License-Identifier: GPL-2.0
593 +/*
594 + * Mainly for aufs which mmap(2) different file and wants to print different
595 + * path in /proc/PID/maps.
596 + * Call these functions via macros defined in linux/mm.h.
597 + *
598 + * See Documentation/filesystems/aufs/design/06mmap.txt
599 + *
600 + * Copyright (c) 2014-2022 Junjro R. Okajima
601 + * Copyright (c) 2014 Ian Campbell
602 + */
603 +
604 +#include <linux/mm.h>
605 +#include <linux/file.h>
606 +#include <linux/fs.h>
607 +
608 +/* #define PRFILE_TRACE */
609 +static inline void prfile_trace(struct file *f, struct file *pr,
610 +                             const char func[], int line, const char func2[])
611 +{
612 +#ifdef PRFILE_TRACE
613 +       if (pr)
614 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
615 +#endif
616 +}
617 +
618 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
619 +                            int line)
620 +{
621 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
622 +
623 +       prfile_trace(f, pr, func, line, __func__);
624 +       file_update_time(f);
625 +       if (f && pr)
626 +               file_update_time(pr);
627 +}
628 +
629 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
630 +                              int line)
631 +{
632 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       return (f && pr) ? pr : f;
636 +}
637 +
638 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
639 +{
640 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
641 +
642 +       prfile_trace(f, pr, func, line, __func__);
643 +       get_file(f);
644 +       if (f && pr)
645 +               get_file(pr);
646 +}
647 +
648 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
649 +{
650 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
651 +
652 +       prfile_trace(f, pr, func, line, __func__);
653 +       fput(f);
654 +       if (f && pr)
655 +               fput(pr);
656 +}
657 +
658 +#ifndef CONFIG_MMU
659 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
660 +                              int line)
661 +{
662 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
663 +
664 +       prfile_trace(f, pr, func, line, __func__);
665 +       return (f && pr) ? pr : f;
666 +}
667 +
668 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
669 +{
670 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
671 +
672 +       prfile_trace(f, pr, func, line, __func__);
673 +       fput(f);
674 +       if (f && pr)
675 +               fput(pr);
676 +}
677 +#endif /* !CONFIG_MMU */
678 SPDX-License-Identifier: GPL-2.0
679 aufs6.x-rcN standalone patch
680
681 diff --git a/fs/dcache.c b/fs/dcache.c
682 index 519321f32f95..267e0c65914a 100644
683 --- a/fs/dcache.c
684 +++ b/fs/dcache.c
685 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
686         seq = 1;
687         goto again;
688  }
689 +EXPORT_SYMBOL_GPL(d_walk);
690  
691  struct check_mount {
692         struct vfsmount *mnt;
693 @@ -3052,6 +3053,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
694  
695         write_sequnlock(&rename_lock);
696  }
697 +EXPORT_SYMBOL_GPL(d_exchange);
698  
699  /**
700   * d_ancestor - search for an ancestor
701 diff --git a/fs/exec.c b/fs/exec.c
702 index a0b1f0337a62..2038fdb508c9 100644
703 --- a/fs/exec.c
704 +++ b/fs/exec.c
705 @@ -110,6 +110,7 @@ bool path_noexec(const struct path *path)
706         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
707                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
708  }
709 +EXPORT_SYMBOL_GPL(path_noexec);
710  
711  #ifdef CONFIG_USELIB
712  /*
713 diff --git a/fs/fcntl.c b/fs/fcntl.c
714 index c980a0524118..40087d11a974 100644
715 --- a/fs/fcntl.c
716 +++ b/fs/fcntl.c
717 @@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
718   out:
719         return error;
720  }
721 +EXPORT_SYMBOL_GPL(setfl);
722  
723  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
724                       int force)
725 diff --git a/fs/file_table.c b/fs/file_table.c
726 index dd88701e54a9..ecdb7817b074 100644
727 --- a/fs/file_table.c
728 +++ b/fs/file_table.c
729 @@ -198,6 +198,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
730         }
731         return ERR_PTR(-ENFILE);
732  }
733 +EXPORT_SYMBOL_GPL(alloc_empty_file);
734  
735  /*
736   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
737 diff --git a/fs/namespace.c b/fs/namespace.c
738 index faae7f8644ac..0310c99a83dd 100644
739 --- a/fs/namespace.c
740 +++ b/fs/namespace.c
741 @@ -456,6 +456,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
742         mnt_dec_writers(real_mount(mnt));
743         preempt_enable();
744  }
745 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
746  
747  /**
748   * mnt_drop_write - give up write access to a mount
749 @@ -857,6 +858,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
750  {
751         return check_mnt(real_mount(mnt));
752  }
753 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
754  
755  /*
756   * vfsmount lock must be held for write
757 @@ -2060,6 +2062,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
758         }
759         return 0;
760  }
761 +EXPORT_SYMBOL_GPL(iterate_mounts);
762  
763  static void lock_mnt_tree(struct mount *mnt)
764  {
765 diff --git a/fs/notify/group.c b/fs/notify/group.c
766 index 1de6631a3925..3008eb37a18d 100644
767 --- a/fs/notify/group.c
768 +++ b/fs/notify/group.c
769 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
770  {
771         refcount_inc(&group->refcnt);
772  }
773 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
774  
775  /*
776   * Drop a reference to a group.  Free it if it's through.
777 diff --git a/fs/open.c b/fs/open.c
778 index a81319b6177f..50bb15ea3fb5 100644
779 --- a/fs/open.c
780 +++ b/fs/open.c
781 @@ -66,6 +66,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
782         inode_unlock(dentry->d_inode);
783         return ret;
784  }
785 +EXPORT_SYMBOL_GPL(do_truncate);
786  
787  long vfs_truncate(const struct path *path, loff_t length)
788  {
789 diff --git a/fs/read_write.c b/fs/read_write.c
790 index 328ce8cf9a85..59931848f794 100644
791 --- a/fs/read_write.c
792 +++ b/fs/read_write.c
793 @@ -477,6 +477,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
794         inc_syscr(current);
795         return ret;
796  }
797 +EXPORT_SYMBOL_GPL(vfs_read);
798  
799  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
800  {
801 @@ -592,6 +593,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
802         file_end_write(file);
803         return ret;
804  }
805 +EXPORT_SYMBOL_GPL(vfs_write);
806  
807  /* file_ppos returns &file->f_pos or NULL if file is stream */
808  static inline loff_t *file_ppos(struct file *file)
809 diff --git a/fs/splice.c b/fs/splice.c
810 index d83f9e5cddbc..ac355ebeb9f0 100644
811 --- a/fs/splice.c
812 +++ b/fs/splice.c
813 @@ -763,6 +763,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
814                 return warn_unsupported(out, "write");
815         return out->f_op->splice_write(pipe, out, ppos, len, flags);
816  }
817 +EXPORT_SYMBOL_GPL(do_splice_from);
818  
819  /*
820   * Attempt to initiate a splice from a file to a pipe.
821 @@ -792,6 +793,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
822                 return warn_unsupported(in, "read");
823         return in->f_op->splice_read(in, ppos, pipe, len, flags);
824  }
825 +EXPORT_SYMBOL_GPL(do_splice_to);
826  
827  /**
828   * splice_direct_to_actor - splices data directly between two non-pipes
829 diff --git a/fs/xattr.c b/fs/xattr.c
830 index 61107b6bbed2..243855024966 100644
831 --- a/fs/xattr.c
832 +++ b/fs/xattr.c
833 @@ -392,6 +392,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
834         *xattr_value = value;
835         return error;
836  }
837 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
838  
839  ssize_t
840  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
841 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
842 index c1f0aadde539..278e0a322d20 100644
843 --- a/kernel/locking/lockdep.c
844 +++ b/kernel/locking/lockdep.c
845 @@ -238,6 +238,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
846          */
847         return lock_classes + class_idx;
848  }
849 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
850  #define hlock_class(hlock) lockdep_hlock_class(hlock)
851  
852  #ifdef CONFIG_LOCK_STAT
853 diff --git a/kernel/task_work.c b/kernel/task_work.c
854 index 065e1ef8fc8d..c623c6f0c645 100644
855 --- a/kernel/task_work.c
856 +++ b/kernel/task_work.c
857 @@ -182,3 +182,4 @@ void task_work_run(void)
858                 } while (work);
859         }
860  }
861 +EXPORT_SYMBOL_GPL(task_work_run);
862 diff --git a/security/security.c b/security/security.c
863 index 79d82cb6e469..1c50e6faadcd 100644
864 --- a/security/security.c
865 +++ b/security/security.c
866 @@ -1167,6 +1167,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
867                 return 0;
868         return call_int_hook(path_rmdir, 0, dir, dentry);
869  }
870 +EXPORT_SYMBOL_GPL(security_path_rmdir);
871  
872  int security_path_unlink(const struct path *dir, struct dentry *dentry)
873  {
874 @@ -1183,6 +1184,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
875                 return 0;
876         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
877  }
878 +EXPORT_SYMBOL_GPL(security_path_symlink);
879  
880  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
881                        struct dentry *new_dentry)
882 @@ -1191,6 +1193,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
883                 return 0;
884         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
885  }
886 +EXPORT_SYMBOL_GPL(security_path_link);
887  
888  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
889                          const struct path *new_dir, struct dentry *new_dentry,
890 @@ -1211,6 +1214,7 @@ int security_path_truncate(const struct path *path)
891                 return 0;
892         return call_int_hook(path_truncate, 0, path);
893  }
894 +EXPORT_SYMBOL_GPL(security_path_truncate);
895  
896  int security_path_chmod(const struct path *path, umode_t mode)
897  {
898 @@ -1218,6 +1222,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
899                 return 0;
900         return call_int_hook(path_chmod, 0, path, mode);
901  }
902 +EXPORT_SYMBOL_GPL(security_path_chmod);
903  
904  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
905  {
906 @@ -1225,6 +1230,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
907                 return 0;
908         return call_int_hook(path_chown, 0, path, uid, gid);
909  }
910 +EXPORT_SYMBOL_GPL(security_path_chown);
911  
912  int security_path_chroot(const struct path *path)
913  {
914 @@ -1325,6 +1331,7 @@ int security_inode_permission(struct inode *inode, int mask)
915                 return 0;
916         return call_int_hook(inode_permission, 0, inode, mask);
917  }
918 +EXPORT_SYMBOL_GPL(security_inode_permission);
919  
920  int security_inode_setattr(struct user_namespace *mnt_userns,
921                            struct dentry *dentry, struct iattr *attr)
922 @@ -1523,6 +1530,7 @@ int security_file_permission(struct file *file, int mask)
923  
924         return fsnotify_perm(file, mask);
925  }
926 +EXPORT_SYMBOL_GPL(security_file_permission);
927  
928  int security_file_alloc(struct file *file)
929  {
930 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
931 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
932 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-11-05 23:02:18.955889283 +0100
933 @@ -0,0 +1,55 @@
934 +What:          /debug/aufs/si_<id>/
935 +Date:          March 2009
936 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
937 +Description:
938 +               Under /debug/aufs, a directory named si_<id> is created
939 +               per aufs mount, where <id> is a unique id generated
940 +               internally.
941 +
942 +What:          /debug/aufs/si_<id>/plink
943 +Date:          Apr 2013
944 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
945 +Description:
946 +               It has three lines and shows the information about the
947 +               pseudo-link. The first line is a single number
948 +               representing a number of buckets. The second line is a
949 +               number of pseudo-links per buckets (separated by a
950 +               blank). The last line is a single number representing a
951 +               total number of psedo-links.
952 +               When the aufs mount option 'noplink' is specified, it
953 +               will show "1\n0\n0\n".
954 +
955 +What:          /debug/aufs/si_<id>/xib
956 +Date:          March 2009
957 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
958 +Description:
959 +               It shows the consumed blocks by xib (External Inode Number
960 +               Bitmap), its block size and file size.
961 +               When the aufs mount option 'noxino' is specified, it
962 +               will be empty. About XINO files, see the aufs manual.
963 +
964 +What:          /debug/aufs/si_<id>/xi<branch-index>
965 +Date:          March 2009
966 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
967 +Description:
968 +               It shows the consumed blocks by xino (External Inode Number
969 +               Translation Table), its link count, block size and file
970 +               size.
971 +               Due to the file size limit, there may exist multiple
972 +               xino files per branch.  In this case, "-N" is added to
973 +               the filename and it corresponds to the index of the
974 +               internal xino array.  "-0" is omitted.
975 +               When the aufs mount option 'noxino' is specified, Those
976 +               entries won't exist.  About XINO files, see the aufs
977 +               manual.
978 +
979 +What:          /debug/aufs/si_<id>/xigen
980 +Date:          March 2009
981 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
982 +Description:
983 +               It shows the consumed blocks by xigen (External Inode
984 +               Generation Table), its block size and file size.
985 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
986 +               be created.
987 +               When the aufs mount option 'noxino' is specified, it
988 +               will be empty. About XINO files, see the aufs manual.
989 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
990 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
991 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-11-05 23:02:18.955889283 +0100
992 @@ -0,0 +1,31 @@
993 +What:          /sys/fs/aufs/si_<id>/
994 +Date:          March 2009
995 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
996 +Description:
997 +               Under /sys/fs/aufs, a directory named si_<id> is created
998 +               per aufs mount, where <id> is a unique id generated
999 +               internally.
1000 +
1001 +What:          /sys/fs/aufs/si_<id>/br<idx>
1002 +Date:          March 2009
1003 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1004 +Description:
1005 +               It shows the abolute path of a member directory (which
1006 +               is called branch) in aufs, and its permission.
1007 +
1008 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1009 +Date:          July 2013
1010 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1011 +Description:
1012 +               It shows the id of a member directory (which is called
1013 +               branch) in aufs.
1014 +
1015 +What:          /sys/fs/aufs/si_<id>/xi_path
1016 +Date:          March 2009
1017 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1018 +Description:
1019 +               It shows the abolute path of XINO (External Inode Number
1020 +               Bitmap, Translation Table and Generation Table) file
1021 +               even if it is the default path.
1022 +               When the aufs mount option 'noxino' is specified, it
1023 +               will be empty. About XINO files, see the aufs manual.
1024 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1025 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1026 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-11-05 23:02:18.955889283 +0100
1027 @@ -0,0 +1,171 @@
1028 +
1029 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1030 +#
1031 +# This program is free software; you can redistribute it and/or modify
1032 +# it under the terms of the GNU General Public License as published by
1033 +# the Free Software Foundation; either version 2 of the License, or
1034 +# (at your option) any later version.
1035 +#
1036 +# This program is distributed in the hope that it will be useful,
1037 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1038 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1039 +# GNU General Public License for more details.
1040 +#
1041 +# You should have received a copy of the GNU General Public License
1042 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1043 +
1044 +Introduction
1045 +----------------------------------------
1046 +
1047 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1048 +1. abbrev. for "advanced multi-layered unification filesystem".
1049 +2. abbrev. for "another unionfs".
1050 +3. abbrev. for "auf das" in German which means "on the" in English.
1051 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1052 +   But "Filesystem aufs Filesystem" is hard to understand.
1053 +4. abbrev. for "African Urban Fashion Show".
1054 +
1055 +AUFS is a filesystem with features:
1056 +- multi layered stackable unification filesystem, the member directory
1057 +  is called as a branch.
1058 +- branch permission and attribute, 'readonly', 'real-readonly',
1059 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1060 +  combination.
1061 +- internal "file copy-on-write".
1062 +- logical deletion, whiteout.
1063 +- dynamic branch manipulation, adding, deleting and changing permission.
1064 +- allow bypassing aufs, user's direct branch access.
1065 +- external inode number translation table and bitmap which maintains the
1066 +  persistent aufs inode number.
1067 +- seekable directory, including NFS readdir.
1068 +- file mapping, mmap and sharing pages.
1069 +- pseudo-link, hardlink over branches.
1070 +- loopback mounted filesystem as a branch.
1071 +- several policies to select one among multiple writable branches.
1072 +- revert a single systemcall when an error occurs in aufs.
1073 +- and more...
1074 +
1075 +
1076 +Multi Layered Stackable Unification Filesystem
1077 +----------------------------------------------------------------------
1078 +Most people already knows what it is.
1079 +It is a filesystem which unifies several directories and provides a
1080 +merged single directory. When users access a file, the access will be
1081 +passed/re-directed/converted (sorry, I am not sure which English word is
1082 +correct) to the real file on the member filesystem. The member
1083 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1084 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1085 +readonly branch is handled by creating 'whiteout' on the upper writable
1086 +branch.
1087 +
1088 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1089 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1090 +different approaches to implement the merged-view.
1091 +The former tries putting it into VFS, and the latter implements as a
1092 +separate filesystem.
1093 +(If I misunderstand about these implementations, please let me know and
1094 +I shall correct it. Because it is a long time ago when I read their
1095 +source files last time).
1096 +
1097 +UnionMount's approach will be able to small, but may be hard to share
1098 +branches between several UnionMount since the whiteout in it is
1099 +implemented in the inode on branch filesystem and always
1100 +shared. According to Bharata's post, readdir does not seems to be
1101 +finished yet.
1102 +There are several missing features known in this implementations such as
1103 +- for users, the inode number may change silently. eg. copy-up.
1104 +- link(2) may break by copy-up.
1105 +- read(2) may get an obsoleted filedata (fstat(2) too).
1106 +- fcntl(F_SETLK) may be broken by copy-up.
1107 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1108 +  open(O_RDWR).
1109 +
1110 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1111 +merged into mainline. This is another implementation of UnionMount as a
1112 +separated filesystem. All the limitations and known problems which
1113 +UnionMount are equally inherited to "overlay" filesystem.
1114 +
1115 +Unionfs has a longer history. When I started implementing a stackable
1116 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1117 +inode, dentry and file objects and they have an array pointing lower
1118 +same kind objects. After contributing many patches for Unionfs, I
1119 +re-started my project AUFS (Jun 2006).
1120 +
1121 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1122 +implemented my own ideas, approaches and enhancements and it became
1123 +totally different one.
1124 +
1125 +Comparing DM snapshot and fs based implementation
1126 +- the number of bytes to be copied between devices is much smaller.
1127 +- the type of filesystem must be one and only.
1128 +- the fs must be writable, no readonly fs, even for the lower original
1129 +  device. so the compression fs will not be usable. but if we use
1130 +  loopback mount, we may address this issue.
1131 +  for instance,
1132 +       mount /cdrom/squashfs.img /sq
1133 +       losetup /sq/ext2.img
1134 +       losetup /somewhere/cow
1135 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1136 +- it will be difficult (or needs more operations) to extract the
1137 +  difference between the original device and COW.
1138 +- DM snapshot-merge may help a lot when users try merging. in the
1139 +  fs-layer union, users will use rsync(1).
1140 +
1141 +You may want to read my old paper "Filesystems in LiveCD"
1142 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1143 +
1144 +
1145 +Several characters/aspects/persona of aufs
1146 +----------------------------------------------------------------------
1147 +
1148 +Aufs has several characters, aspects or persona.
1149 +1. a filesystem, callee of VFS helper
1150 +2. sub-VFS, caller of VFS helper for branches
1151 +3. a virtual filesystem which maintains persistent inode number
1152 +4. reader/writer of files on branches such like an application
1153 +
1154 +1. Callee of VFS Helper
1155 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1156 +unlink(2) from an application reaches sys_unlink() kernel function and
1157 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1158 +calls filesystem specific unlink operation. Actually aufs implements the
1159 +unlink operation but it behaves like a redirector.
1160 +
1161 +2. Caller of VFS Helper for Branches
1162 +aufs_unlink() passes the unlink request to the branch filesystem as if
1163 +it were called from VFS. So the called unlink operation of the branch
1164 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1165 +every necessary pre/post operation for the branch filesystem.
1166 +- acquire the lock for the parent dir on a branch
1167 +- lookup in a branch
1168 +- revalidate dentry on a branch
1169 +- mnt_want_write() for a branch
1170 +- vfs_unlink() for a branch
1171 +- mnt_drop_write() for a branch
1172 +- release the lock on a branch
1173 +
1174 +3. Persistent Inode Number
1175 +One of the most important issue for a filesystem is to maintain inode
1176 +numbers. This is particularly important to support exporting a
1177 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1178 +backend block device for its own. But some storage is necessary to
1179 +keep and maintain the inode numbers. It may be a large space and may not
1180 +suit to keep in memory. Aufs rents some space from its first writable
1181 +branch filesystem (by default) and creates file(s) on it. These files
1182 +are created by aufs internally and removed soon (currently) keeping
1183 +opened.
1184 +Note: Because these files are removed, they are totally gone after
1185 +      unmounting aufs. It means the inode numbers are not persistent
1186 +      across unmount or reboot. I have a plan to make them really
1187 +      persistent which will be important for aufs on NFS server.
1188 +
1189 +4. Read/Write Files Internally (copy-on-write)
1190 +Because a branch can be readonly, when you write a file on it, aufs will
1191 +"copy-up" it to the upper writable branch internally. And then write the
1192 +originally requested thing to the file. Generally kernel doesn't
1193 +open/read/write file actively. In aufs, even a single write may cause a
1194 +internal "file copy". This behaviour is very similar to cp(1) command.
1195 +
1196 +Some people may think it is better to pass such work to user space
1197 +helper, instead of doing in kernel space. Actually I am still thinking
1198 +about it. But currently I have implemented it in kernel space.
1199 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1200 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1201 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-11-05 23:02:18.955889283 +0100
1202 @@ -0,0 +1,258 @@
1203 +
1204 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1205 +#
1206 +# This program is free software; you can redistribute it and/or modify
1207 +# it under the terms of the GNU General Public License as published by
1208 +# the Free Software Foundation; either version 2 of the License, or
1209 +# (at your option) any later version.
1210 +#
1211 +# This program is distributed in the hope that it will be useful,
1212 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1213 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1214 +# GNU General Public License for more details.
1215 +#
1216 +# You should have received a copy of the GNU General Public License
1217 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1218 +
1219 +Basic Aufs Internal Structure
1220 +
1221 +Superblock/Inode/Dentry/File Objects
1222 +----------------------------------------------------------------------
1223 +As like an ordinary filesystem, aufs has its own
1224 +superblock/inode/dentry/file objects. All these objects have a
1225 +dynamically allocated array and store the same kind of pointers to the
1226 +lower filesystem, branch.
1227 +For example, when you build a union with one readwrite branch and one
1228 +readonly, mounted /au, /rw and /ro respectively.
1229 +- /au = /rw + /ro
1230 +- /ro/fileA exists but /rw/fileA
1231 +
1232 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1233 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1234 +- [0] = NULL (because /rw/fileA doesn't exist)
1235 +- [1] = /ro/fileA
1236 +
1237 +This style of an array is essentially same to the aufs
1238 +superblock/inode/dentry/file objects.
1239 +
1240 +Because aufs supports manipulating branches, ie. add/delete/change
1241 +branches dynamically, these objects has its own generation. When
1242 +branches are changed, the generation in aufs superblock is
1243 +incremented. And a generation in other object are compared when it is
1244 +accessed. When a generation in other objects are obsoleted, aufs
1245 +refreshes the internal array.
1246 +
1247 +
1248 +Superblock
1249 +----------------------------------------------------------------------
1250 +Additionally aufs superblock has some data for policies to select one
1251 +among multiple writable branches, XIB files, pseudo-links and kobject.
1252 +See below in detail.
1253 +About the policies which supports copy-down a directory, see
1254 +wbr_policy.txt too.
1255 +
1256 +
1257 +Branch and XINO(External Inode Number Translation Table)
1258 +----------------------------------------------------------------------
1259 +Every branch has its own xino (external inode number translation table)
1260 +file. The xino file is created and unlinked by aufs internally. When two
1261 +members of a union exist on the same filesystem, they share the single
1262 +xino file.
1263 +The struct of a xino file is simple, just a sequence of aufs inode
1264 +numbers which is indexed by the lower inode number.
1265 +In the above sample, assume the inode number of /ro/fileA is i111 and
1266 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1267 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1268 +
1269 +When the inode numbers are not contiguous, the xino file will be sparse
1270 +which has a hole in it and doesn't consume as much disk space as it
1271 +might appear. If your branch filesystem consumes disk space for such
1272 +holes, then you should specify 'xino=' option at mounting aufs.
1273 +
1274 +Aufs has a mount option to free the disk blocks for such holes in XINO
1275 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1276 +meet a problem of disk shortage due to XINO files, then you should try
1277 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1278 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1279 +the holes in XINO files.
1280 +
1281 +Also a writable branch has three kinds of "whiteout bases". All these
1282 +are existed when the branch is joined to aufs, and their names are
1283 +whiteout-ed doubly, so that users will never see their names in aufs
1284 +hierarchy.
1285 +1. a regular file which will be hardlinked to all whiteouts.
1286 +2. a directory to store a pseudo-link.
1287 +3. a directory to store an "orphan"-ed file temporary.
1288 +
1289 +1. Whiteout Base
1290 +   When you remove a file on a readonly branch, aufs handles it as a
1291 +   logical deletion and creates a whiteout on the upper writable branch
1292 +   as a hardlink of this file in order not to consume inode on the
1293 +   writable branch.
1294 +2. Pseudo-link Dir
1295 +   See below, Pseudo-link.
1296 +3. Step-Parent Dir
1297 +   When "fileC" exists on the lower readonly branch only and it is
1298 +   opened and removed with its parent dir, and then user writes
1299 +   something into it, then aufs copies-up fileC to this
1300 +   directory. Because there is no other dir to store fileC. After
1301 +   creating a file under this dir, the file is unlinked.
1302 +
1303 +Because aufs supports manipulating branches, ie. add/delete/change
1304 +dynamically, a branch has its own id. When the branch order changes,
1305 +aufs finds the new index by searching the branch id.
1306 +
1307 +
1308 +Pseudo-link
1309 +----------------------------------------------------------------------
1310 +Assume "fileA" exists on the lower readonly branch only and it is
1311 +hardlinked to "fileB" on the branch. When you write something to fileA,
1312 +aufs copies-up it to the upper writable branch. Additionally aufs
1313 +creates a hardlink under the Pseudo-link Directory of the writable
1314 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1315 +simple list. If fileB is read after unlinking fileA, aufs returns
1316 +filedata from the pseudo-link instead of the lower readonly
1317 +branch. Because the pseudo-link is based upon the inode, to keep the
1318 +inode number by xino (see above) is essentially necessary.
1319 +
1320 +All the hardlinks under the Pseudo-link Directory of the writable branch
1321 +should be restored in a proper location later. Aufs provides a utility
1322 +to do this. The userspace helpers executed at remounting and unmounting
1323 +aufs by default.
1324 +During this utility is running, it puts aufs into the pseudo-link
1325 +maintenance mode. In this mode, only the process which began the
1326 +maintenance mode (and its child processes) is allowed to operate in
1327 +aufs. Some other processes which are not related to the pseudo-link will
1328 +be allowed to run too, but the rest have to return an error or wait
1329 +until the maintenance mode ends. If a process already acquires an inode
1330 +mutex (in VFS), it has to return an error.
1331 +
1332 +
1333 +XIB(external inode number bitmap)
1334 +----------------------------------------------------------------------
1335 +Addition to the xino file per a branch, aufs has an external inode number
1336 +bitmap in a superblock object. It is also an internal file such like a
1337 +xino file.
1338 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1339 +not.
1340 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1341 +
1342 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1343 +reduce the number of consumed disk blocks for these files.
1344 +
1345 +
1346 +Virtual or Vertical Dir, and Readdir in Userspace
1347 +----------------------------------------------------------------------
1348 +In order to support multiple layers (branches), aufs readdir operation
1349 +constructs a virtual dir block on memory. For readdir, aufs calls
1350 +vfs_readdir() internally for each dir on branches, merges their entries
1351 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1352 +object. So the file object has its entry list until it is closed. The
1353 +entry list will be updated when the file position is zero and becomes
1354 +obsoleted. This decision is made in aufs automatically.
1355 +
1356 +The dynamically allocated memory block for the name of entries has a
1357 +unit of 512 bytes (by default) and stores the names contiguously (no
1358 +padding). Another block for each entry is handled by kmem_cache too.
1359 +During building dir blocks, aufs creates hash list and judging whether
1360 +the entry is whiteouted by its upper branch or already listed.
1361 +The merged result is cached in the corresponding inode object and
1362 +maintained by a customizable life-time option.
1363 +
1364 +Some people may call it can be a security hole or invite DoS attack
1365 +since the opened and once readdir-ed dir (file object) holds its entry
1366 +list and becomes a pressure for system memory. But I'd say it is similar
1367 +to files under /proc or /sys. The virtual files in them also holds a
1368 +memory page (generally) while they are opened. When an idea to reduce
1369 +memory for them is introduced, it will be applied to aufs too.
1370 +For those who really hate this situation, I've developed readdir(3)
1371 +library which operates this merging in userspace. You just need to set
1372 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1373 +kernel space for readdir(3).
1374 +
1375 +
1376 +Workqueue
1377 +----------------------------------------------------------------------
1378 +Aufs sometimes requires privilege access to a branch. For instance,
1379 +in copy-up/down operation. When a user process is going to make changes
1380 +to a file which exists in the lower readonly branch only, and the mode
1381 +of one of ancestor directories may not be writable by a user
1382 +process. Here aufs copy-up the file with its ancestors and they may
1383 +require privilege to set its owner/group/mode/etc.
1384 +This is a typical case of a application character of aufs (see
1385 +Introduction).
1386 +
1387 +Aufs uses workqueue synchronously for this case. It creates its own
1388 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1389 +passes the request to call mkdir or write (for example), and wait for
1390 +its completion. This approach solves a problem of a signal handler
1391 +simply.
1392 +If aufs didn't adopt the workqueue and changed the privilege of the
1393 +process, then the process may receive the unexpected SIGXFSZ or other
1394 +signals.
1395 +
1396 +Also aufs uses the system global workqueue ("events" kernel thread) too
1397 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1398 +whiteout base and etc. This is unrelated to a privilege.
1399 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1400 +superblock at the beginning, at the same time waits for the completion
1401 +of all queued asynchronous tasks.
1402 +
1403 +
1404 +Whiteout
1405 +----------------------------------------------------------------------
1406 +The whiteout in aufs is very similar to Unionfs's. That is represented
1407 +by its filename. UnionMount takes an approach of a file mode, but I am
1408 +afraid several utilities (find(1) or something) will have to support it.
1409 +
1410 +Basically the whiteout represents "logical deletion" which stops aufs to
1411 +lookup further, but also it represents "dir is opaque" which also stop
1412 +further lookup.
1413 +
1414 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1415 +In order to make several functions in a single systemcall to be
1416 +revertible, aufs adopts an approach to rename a directory to a temporary
1417 +unique whiteouted name.
1418 +For example, in rename(2) dir where the target dir already existed, aufs
1419 +renames the target dir to a temporary unique whiteouted name before the
1420 +actual rename on a branch, and then handles other actions (make it opaque,
1421 +update the attributes, etc). If an error happens in these actions, aufs
1422 +simply renames the whiteouted name back and returns an error. If all are
1423 +succeeded, aufs registers a function to remove the whiteouted unique
1424 +temporary name completely and asynchronously to the system global
1425 +workqueue.
1426 +
1427 +
1428 +Copy-up
1429 +----------------------------------------------------------------------
1430 +It is a well-known feature or concept.
1431 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1432 +internally and makes change to the new file on the upper writable branch.
1433 +When the trigger systemcall does not update the timestamps of the parent
1434 +dir, aufs reverts it after copy-up.
1435 +
1436 +
1437 +Move-down (aufs3.9 and later)
1438 +----------------------------------------------------------------------
1439 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1440 +the lower readonly branch to the upper writable branch when a user
1441 +changes something about the file.
1442 +"Move-down" is an opposite action of copy-up. Basically this action is
1443 +ran manually instead of automatically and internally.
1444 +For desgin and implementation, aufs has to consider these issues.
1445 +- whiteout for the file may exist on the lower branch.
1446 +- ancestor directories may not exist on the lower branch.
1447 +- diropq for the ancestor directories may exist on the upper branch.
1448 +- free space on the lower branch will reduce.
1449 +- another access to the file may happen during moving-down, including
1450 +  UDBA (see "Revalidate Dentry and UDBA").
1451 +- the file should not be hard-linked nor pseudo-linked. they should be
1452 +  handled by auplink utility later.
1453 +
1454 +Sometimes users want to move-down a file from the upper writable branch
1455 +to the lower readonly or writable branch. For instance,
1456 +- the free space of the upper writable branch is going to run out.
1457 +- create a new intermediate branch between the upper and lower branch.
1458 +- etc.
1459 +
1460 +For this purpose, use "aumvdown" command in aufs-util.git.
1461 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1462 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1463 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-11-05 23:02:18.955889283 +0100
1464 @@ -0,0 +1,85 @@
1465 +
1466 +# Copyright (C) 2015-2022 Junjiro R. Okajima
1467 +#
1468 +# This program is free software; you can redistribute it and/or modify
1469 +# it under the terms of the GNU General Public License as published by
1470 +# the Free Software Foundation; either version 2 of the License, or
1471 +# (at your option) any later version.
1472 +#
1473 +# This program is distributed in the hope that it will be useful,
1474 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1475 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1476 +# GNU General Public License for more details.
1477 +#
1478 +# You should have received a copy of the GNU General Public License
1479 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1480 +
1481 +Support for a branch who has its ->atomic_open()
1482 +----------------------------------------------------------------------
1483 +The filesystems who implement its ->atomic_open() are not majority. For
1484 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1485 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1486 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1487 +sure whether all filesystems who have ->atomic_open() behave like this,
1488 +but NFSv4 surely returns the error.
1489 +
1490 +In order to support ->atomic_open() for aufs, there are a few
1491 +approaches.
1492 +
1493 +A. Introduce aufs_atomic_open()
1494 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1495 +     branch fs.
1496 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1497 +   an aufs user Pip Cet's approach
1498 +   - calls aufs_create(), VFS finish_open() and notify_change().
1499 +   - pass fake-mode to finish_open(), and then correct the mode by
1500 +     notify_change().
1501 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1502 +   - no aufs_atomic_open().
1503 +   - aufs_lookup() registers the TID to an aufs internal object.
1504 +   - aufs_create() does nothing when the matching TID is registered, but
1505 +     registers the mode.
1506 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1507 +     TID is registered.
1508 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1509 +   credential
1510 +   - no aufs_atomic_open().
1511 +   - aufs_create() registers the TID to an internal object. this info
1512 +     represents "this process created this file just now."
1513 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1514 +     registered TID and re-try open() with superuser's credential.
1515 +
1516 +Pros and cons for each approach.
1517 +
1518 +A.
1519 +   - straightforward but highly depends upon VFS internal.
1520 +   - the atomic behavaiour is kept.
1521 +   - some of parameters such as nameidata are hard to reproduce for
1522 +     branch fs.
1523 +   - large overhead.
1524 +B.
1525 +   - easy to implement.
1526 +   - the atomic behavaiour is lost.
1527 +C.
1528 +   - the atomic behavaiour is kept.
1529 +   - dirty and tricky.
1530 +   - VFS checks whether the file is created correctly after calling
1531 +     ->create(), which means this approach doesn't work.
1532 +D.
1533 +   - easy to implement.
1534 +   - the atomic behavaiour is lost.
1535 +   - to open a file with superuser's credential and give it to a user
1536 +     process is a bad idea, since the file object keeps the credential
1537 +     in it. It may affect LSM or something. This approach doesn't work
1538 +     either.
1539 +
1540 +The approach A is ideal, but it hard to implement. So here is a
1541 +variation of A, which is to be implemented.
1542 +
1543 +A-1. Introduce aufs_atomic_open()
1544 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1545 +       vfs_create() and finish_open().
1546 +     - the demerit is that the several checks after branch fs
1547 +       ->atomic_open() are lost. in the ordinary case, the checks are
1548 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1549 +       be implemented in aufs, but not all I am afraid.
1550 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1551 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1552 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-11-05 23:02:18.959222617 +0100
1553 @@ -0,0 +1,113 @@
1554 +
1555 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1556 +#
1557 +# This program is free software; you can redistribute it and/or modify
1558 +# it under the terms of the GNU General Public License as published by
1559 +# the Free Software Foundation; either version 2 of the License, or
1560 +# (at your option) any later version.
1561 +#
1562 +# This program is distributed in the hope that it will be useful,
1563 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1564 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1565 +# GNU General Public License for more details.
1566 +#
1567 +# You should have received a copy of the GNU General Public License
1568 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1569 +
1570 +Lookup in a Branch
1571 +----------------------------------------------------------------------
1572 +Since aufs has a character of sub-VFS (see Introduction), it operates
1573 +lookup for branches as VFS does. It may be a heavy work. But almost all
1574 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1575 +directly connected to its parent. Digging down the directory hierarchy
1576 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1577 +aufs calls it.
1578 +
1579 +When a branch is a remote filesystem, aufs basically relies upon its
1580 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1581 +them.
1582 +For d_revalidate, aufs implements three levels of revalidate tests. See
1583 +"Revalidate Dentry and UDBA" in detail.
1584 +
1585 +
1586 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1587 +----------------------------------------------------------------------
1588 +Let's try case study.
1589 +- aufs has two branches, upper readwrite and lower readonly.
1590 +  /au = /rw + /ro
1591 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1592 +- user invoked "chmod a+rx /au/dirA"
1593 +- the internal copy-up is activated and "/rw/dirA" is created and its
1594 +  permission bits are set to world readable.
1595 +- then "/au/dirA" becomes world readable?
1596 +
1597 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1598 +or it may be a natively readonly filesystem. If aufs respects the lower
1599 +branch, it should not respond readdir request from other users. But user
1600 +allowed it by chmod. Should really aufs rejects showing the entries
1601 +under /ro/dirA?
1602 +
1603 +To be honest, I don't have a good solution for this case. So aufs
1604 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1605 +users.
1606 +When dirperm1 is specified, aufs checks only the highest one for the
1607 +directory permission, and shows the entries. Otherwise, as usual, checks
1608 +every dir existing on all branches and rejects the request.
1609 +
1610 +As a side effect, dirperm1 option improves the performance of aufs
1611 +because the number of permission check is reduced when the number of
1612 +branch is many.
1613 +
1614 +
1615 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1616 +----------------------------------------------------------------------
1617 +Generally VFS helpers re-validate a dentry as a part of lookup.
1618 +0. digging down the directory hierarchy.
1619 +1. lock the parent dir by its i_mutex.
1620 +2. lookup the final (child) entry.
1621 +3. revalidate it.
1622 +4. call the actual operation (create, unlink, etc.)
1623 +5. unlock the parent dir
1624 +
1625 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1626 +called. Actually aufs implements it and checks the dentry on a branch is
1627 +still valid.
1628 +But it is not enough. Because aufs has to release the lock for the
1629 +parent dir on a branch at the end of ->lookup() (step 2) and
1630 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1631 +held by VFS.
1632 +If the file on a branch is changed directly, eg. bypassing aufs, after
1633 +aufs released the lock, then the subsequent operation may cause
1634 +something unpleasant result.
1635 +
1636 +This situation is a result of VFS architecture, ->lookup() and
1637 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1638 +design from VFS's point of view. It is just not suitable for sub-VFS
1639 +character in aufs.
1640 +
1641 +Aufs supports such case by three level of revalidation which is
1642 +selectable by user.
1643 +1. Simple Revalidate
1644 +   Addition to the native flow in VFS's, confirm the child-parent
1645 +   relationship on the branch just after locking the parent dir on the
1646 +   branch in the "actual operation" (step 4). When this validation
1647 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1648 +   checks the validation of the dentry on branches.
1649 +2. Monitor Changes Internally by Inotify/Fsnotify
1650 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1651 +   the dentry on the branch, and returns EBUSY if it finds different
1652 +   dentry.
1653 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1654 +   during it is in cache. When the event is notified, aufs registers a
1655 +   function to kernel 'events' thread by schedule_work(). And the
1656 +   function sets some special status to the cached aufs dentry and inode
1657 +   private data. If they are not cached, then aufs has nothing to
1658 +   do. When the same file is accessed through aufs (step 0-3) later,
1659 +   aufs will detect the status and refresh all necessary data.
1660 +   In this mode, aufs has to ignore the event which is fired by aufs
1661 +   itself.
1662 +3. No Extra Validation
1663 +   This is the simplest test and doesn't add any additional revalidation
1664 +   test, and skip the revalidation in step 4. It is useful and improves
1665 +   aufs performance when system surely hide the aufs branches from user,
1666 +   by over-mounting something (or another method).
1667 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1668 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1669 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-11-05 23:02:18.959222617 +0100
1670 @@ -0,0 +1,74 @@
1671 +
1672 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1673 +#
1674 +# This program is free software; you can redistribute it and/or modify
1675 +# it under the terms of the GNU General Public License as published by
1676 +# the Free Software Foundation; either version 2 of the License, or
1677 +# (at your option) any later version.
1678 +#
1679 +# This program is distributed in the hope that it will be useful,
1680 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1681 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1682 +# GNU General Public License for more details.
1683 +#
1684 +# You should have received a copy of the GNU General Public License
1685 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1686 +
1687 +Branch Manipulation
1688 +
1689 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1690 +and changing its permission/attribute, there are a lot of works to do.
1691 +
1692 +
1693 +Add a Branch
1694 +----------------------------------------------------------------------
1695 +o Confirm the adding dir exists outside of aufs, including loopback
1696 +  mount, and its various attributes.
1697 +o Initialize the xino file and whiteout bases if necessary.
1698 +  See struct.txt.
1699 +
1700 +o Check the owner/group/mode of the directory
1701 +  When the owner/group/mode of the adding directory differs from the
1702 +  existing branch, aufs issues a warning because it may impose a
1703 +  security risk.
1704 +  For example, when a upper writable branch has a world writable empty
1705 +  top directory, a malicious user can create any files on the writable
1706 +  branch directly, like copy-up and modify manually. If something like
1707 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1708 +  writable branch, and the writable branch is world-writable, then a
1709 +  malicious guy may create /etc/passwd on the writable branch directly
1710 +  and the infected file will be valid in aufs.
1711 +  I am afraid it can be a security issue, but aufs can do nothing except
1712 +  producing a warning.
1713 +
1714 +
1715 +Delete a Branch
1716 +----------------------------------------------------------------------
1717 +o Confirm the deleting branch is not busy
1718 +  To be general, there is one merit to adopt "remount" interface to
1719 +  manipulate branches. It is to discard caches. At deleting a branch,
1720 +  aufs checks the still cached (and connected) dentries and inodes. If
1721 +  there are any, then they are all in-use. An inode without its
1722 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1723 +
1724 +  For the cached one, aufs checks whether the same named entry exists on
1725 +  other branches.
1726 +  If the cached one is a directory, because aufs provides a merged view
1727 +  to users, as long as one dir is left on any branch aufs can show the
1728 +  dir to users. In this case, the branch can be removed from aufs.
1729 +  Otherwise aufs rejects deleting the branch.
1730 +
1731 +  If any file on the deleting branch is opened by aufs, then aufs
1732 +  rejects deleting.
1733 +
1734 +
1735 +Modify the Permission of a Branch
1736 +----------------------------------------------------------------------
1737 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1738 +  See struct.txt.
1739 +
1740 +o rw --> ro: Confirm the modifying branch is not busy
1741 +  Aufs rejects the request if any of these conditions are true.
1742 +  - a file on the branch is mmap-ed.
1743 +  - a regular file on the branch is opened for write and there is no
1744 +    same named entry on the upper branch.
1745 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1746 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1747 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-11-05 23:02:18.959222617 +0100
1748 @@ -0,0 +1,64 @@
1749 +
1750 +# Copyright (C) 2005-2022 Junjiro R. Okajima
1751 +#
1752 +# This program is free software; you can redistribute it and/or modify
1753 +# it under the terms of the GNU General Public License as published by
1754 +# the Free Software Foundation; either version 2 of the License, or
1755 +# (at your option) any later version.
1756 +#
1757 +# This program is distributed in the hope that it will be useful,
1758 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1759 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1760 +# GNU General Public License for more details.
1761 +#
1762 +# You should have received a copy of the GNU General Public License
1763 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1764 +
1765 +Policies to Select One among Multiple Writable Branches
1766 +----------------------------------------------------------------------
1767 +When the number of writable branch is more than one, aufs has to decide
1768 +the target branch for file creation or copy-up. By default, the highest
1769 +writable branch which has the parent (or ancestor) dir of the target
1770 +file is chosen (top-down-parent policy).
1771 +By user's request, aufs implements some other policies to select the
1772 +writable branch, for file creation several policies, round-robin,
1773 +most-free-space, and other policies. For copy-up, top-down-parent,
1774 +bottom-up-parent, bottom-up and others.
1775 +
1776 +As expected, the round-robin policy selects the branch in circular. When
1777 +you have two writable branches and creates 10 new files, 5 files will be
1778 +created for each branch. mkdir(2) systemcall is an exception. When you
1779 +create 10 new directories, all will be created on the same branch.
1780 +And the most-free-space policy selects the one which has most free
1781 +space among the writable branches. The amount of free space will be
1782 +checked by aufs internally, and users can specify its time interval.
1783 +
1784 +The policies for copy-up is more simple,
1785 +top-down-parent is equivalent to the same named on in create policy,
1786 +bottom-up-parent selects the writable branch where the parent dir
1787 +exists and the nearest upper one from the copyup-source,
1788 +bottom-up selects the nearest upper writable branch from the
1789 +copyup-source, regardless the existence of the parent dir.
1790 +
1791 +There are some rules or exceptions to apply these policies.
1792 +- If there is a readonly branch above the policy-selected branch and
1793 +  the parent dir is marked as opaque (a variation of whiteout), or the
1794 +  target (creating) file is whiteout-ed on the upper readonly branch,
1795 +  then the result of the policy is ignored and the target file will be
1796 +  created on the nearest upper writable branch than the readonly branch.
1797 +- If there is a writable branch above the policy-selected branch and
1798 +  the parent dir is marked as opaque or the target file is whiteouted
1799 +  on the branch, then the result of the policy is ignored and the target
1800 +  file will be created on the highest one among the upper writable
1801 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1802 +  it as usual.
1803 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1804 +  They try selecting the branch where the source exists as possible
1805 +  since copyup a large file will take long time. If it can't be,
1806 +  ie. the branch where the source exists is readonly, then they will
1807 +  follow the copyup policy.
1808 +- There is an exception for rename(2) when the target exists.
1809 +  If the rename target exists, aufs compares the index of the branches
1810 +  where the source and the target exists and selects the higher
1811 +  one. If the selected branch is readonly, then aufs follows the
1812 +  copyup policy.
1813 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1814 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1815 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-11-05 23:02:18.959222617 +0100
1816 @@ -0,0 +1,44 @@
1817 +
1818 +// to view this graph, run dot(1) command in GRAPHVIZ.
1819 +//
1820 +// This program is free software; you can redistribute it and/or modify
1821 +// it under the terms of the GNU General Public License as published by
1822 +// the Free Software Foundation; either version 2 of the License, or
1823 +// (at your option) any later version.
1824 +//
1825 +// This program is distributed in the hope that it will be useful,
1826 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
1827 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1828 +// GNU General Public License for more details.
1829 +//
1830 +// You should have received a copy of the GNU General Public License
1831 +// along with this program.  If not, see <http://www.gnu.org/licenses/>.
1832 +
1833 +digraph G {
1834 +node [shape=box];
1835 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1836 +
1837 +node [shape=oval];
1838 +
1839 +aufs_rename -> whinfo [label="store/remove"];
1840 +
1841 +node [shape=oval];
1842 +inode_list [label="h_inum list in branch\ncache"];
1843 +
1844 +node [shape=box];
1845 +whinode [label="h_inum list file"];
1846 +
1847 +node [shape=oval];
1848 +brmgmt [label="br_add/del/mod/umount"];
1849 +
1850 +brmgmt -> inode_list [label="create/remove"];
1851 +brmgmt -> whinode [label="load/store"];
1852 +
1853 +inode_list -> whinode [style=dashed,dir=both];
1854 +
1855 +aufs_rename -> inode_list [label="add/del"];
1856 +
1857 +aufs_lookup -> inode_list [label="search"];
1858 +
1859 +aufs_lookup -> whinfo [label="load/remove"];
1860 +}
1861 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1862 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1863 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-11-05 23:02:18.959222617 +0100
1864 @@ -0,0 +1,102 @@
1865 +
1866 +# Copyright (C) 2017-2022 Junjiro R. Okajima
1867 +#
1868 +# This program is free software; you can redistribute it and/or modify
1869 +# it under the terms of the GNU General Public License as published by
1870 +# the Free Software Foundation; either version 2 of the License, or
1871 +# (at your option) any later version.
1872 +#
1873 +# This program is distributed in the hope that it will be useful,
1874 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1875 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1876 +# GNU General Public License for more details.
1877 +#
1878 +# You should have received a copy of the GNU General Public License
1879 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1880 +
1881 +Special handling for renaming a directory (DIRREN)
1882 +----------------------------------------------------------------------
1883 +First, let's assume we have a simple usecase.
1884 +
1885 +- /u = /rw + /ro
1886 +- /rw/dirA exists
1887 +- /ro/dirA and /ro/dirA/file exist too
1888 +- there is no dirB on both branches
1889 +- a user issues rename("dirA", "dirB")
1890 +
1891 +Now, what should aufs behave against this rename(2)?
1892 +There are a few possible cases.
1893 +
1894 +A. returns EROFS.
1895 +   since dirA exists on a readonly branch which cannot be renamed.
1896 +B. returns EXDEV.
1897 +   it is possible to copy-up dirA (only the dir itself), but the child
1898 +   entries ("file" in this case) should not be. it must be a bad
1899 +   approach to copy-up recursively.
1900 +C. returns a success.
1901 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1902 +   is a violation of aufs' policy.
1903 +D. construct an extra information which indicates that /ro/dirA should
1904 +   be handled as the name of dirB.
1905 +   overlayfs has a similar feature called REDIRECT.
1906 +
1907 +Until now, aufs implements the case B only which returns EXDEV, and
1908 +expects the userspace application behaves like mv(1) which tries
1909 +issueing rename(2) recursively.
1910 +
1911 +A new aufs feature called DIRREN is introduced which implements the case
1912 +D. There are several "extra information" added.
1913 +
1914 +1. detailed info per renamed directory
1915 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1916 +2. the inode-number list of directories on a branch
1917 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1918 +
1919 +The filename of "detailed info per directory" represents the lower
1920 +branch, and its format is
1921 +- a type of the branch id
1922 +  one of these.
1923 +  + uuid (not implemented yet)
1924 +  + fsid
1925 +  + dev
1926 +- the inode-number of the branch root dir
1927 +
1928 +And it contains these info in a single regular file.
1929 +- magic number
1930 +- branch's inode-number of the logically renamed dir
1931 +- the name of the before-renamed dir
1932 +
1933 +The "detailed info per directory" file is created in aufs rename(2), and
1934 +loaded in any lookup.
1935 +The info is considered in lookup for the matching case only. Here
1936 +"matching" means that the root of branch (in the info filename) is same
1937 +to the current looking-up branch. After looking-up the before-renamed
1938 +name, the inode-number is compared. And the matched dentry is used.
1939 +
1940 +The "inode-number list of directories" is a regular file which contains
1941 +simply the inode-numbers on the branch. The file is created or updated
1942 +in removing the branch, and loaded in adding the branch. Its lifetime is
1943 +equal to the branch.
1944 +The list is referred in lookup, and when the current target inode is
1945 +found in the list, the aufs tries loading the "detailed info per
1946 +directory" and get the changed and valid name of the dir.
1947 +
1948 +Theoretically these "extra informaiton" may be able to be put into XATTR
1949 +in the dir inode. But aufs doesn't choose this way because
1950 +1. XATTR may not be supported by the branch (or its configuration)
1951 +2. XATTR may have its size limit.
1952 +3. XATTR may be less easy to convert than a regular file, when the
1953 +   format of the info is changed in the future.
1954 +At the same time, I agree that the regular file approach is much slower
1955 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1956 +better approach.
1957 +
1958 +This DIRREN feature is enabled by aufs configuration, and is activated
1959 +by a new mount option.
1960 +
1961 +For the more complicated case, there is a work with UDBA option, which
1962 +is to dected the direct access to the branches (by-passing aufs) and to
1963 +maintain the cashes in aufs. Since a single cached aufs dentry may
1964 +contains two names, before- and after-rename, the name comparision in
1965 +UDBA handler may not work correctly. In this case, the behaviour will be
1966 +equivalen to udba=reval case.
1967 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1968 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1969 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-11-05 23:02:18.959222617 +0100
1970 @@ -0,0 +1,118 @@
1971 +
1972 +# Copyright (C) 2011-2022 Junjiro R. Okajima
1973 +#
1974 +# This program is free software; you can redistribute it and/or modify
1975 +# it under the terms of the GNU General Public License as published by
1976 +# the Free Software Foundation; either version 2 of the License, or
1977 +# (at your option) any later version.
1978 +#
1979 +# This program is distributed in the hope that it will be useful,
1980 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1981 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1982 +# GNU General Public License for more details.
1983 +#
1984 +# You should have received a copy of the GNU General Public License
1985 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1986 +
1987 +File-based Hierarchical Storage Management (FHSM)
1988 +----------------------------------------------------------------------
1989 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1990 +storage world. Aufs provides this feature as file-based with multiple
1991 +writable branches, based upon the principle of "Colder, the Lower".
1992 +Here the word "colder" means that the less used files, and "lower" means
1993 +that the position in the order of the stacked branches vertically.
1994 +These multiple writable branches are prioritized, ie. the topmost one
1995 +should be the fastest drive and be used heavily.
1996 +
1997 +o Characters in aufs FHSM story
1998 +- aufs itself and a new branch attribute.
1999 +- a new ioctl interface to move-down and to establish a connection with
2000 +  the daemon ("move-down" is a converse of "copy-up").
2001 +- userspace tool and daemon.
2002 +
2003 +The userspace daemon establishes a connection with aufs and waits for
2004 +the notification. The notified information is very similar to struct
2005 +statfs containing the number of consumed blocks and inodes.
2006 +When the consumed blocks/inodes of a branch exceeds the user-specified
2007 +upper watermark, the daemon activates its move-down process until the
2008 +consumed blocks/inodes reaches the user-specified lower watermark.
2009 +
2010 +The actual move-down is done by aufs based upon the request from
2011 +user-space since we need to maintain the inode number and the internal
2012 +pointer arrays in aufs.
2013 +
2014 +Currently aufs FHSM handles the regular files only. Additionally they
2015 +must not be hard-linked nor pseudo-linked.
2016 +
2017 +
2018 +o Cowork of aufs and the user-space daemon
2019 +  During the userspace daemon established the connection, aufs sends a
2020 +  small notification to it whenever aufs writes something into the
2021 +  writable branch. But it may cost high since aufs issues statfs(2)
2022 +  internally. So user can specify a new option to cache the
2023 +  info. Actually the notification is controlled by these factors.
2024 +  + the specified cache time.
2025 +  + classified as "force" by aufs internally.
2026 +  Until the specified time expires, aufs doesn't send the info
2027 +  except the forced cases. When aufs decide forcing, the info is always
2028 +  notified to userspace.
2029 +  For example, the number of free inodes is generally large enough and
2030 +  the shortage of it happens rarely. So aufs doesn't force the
2031 +  notification when creating a new file, directory and others. This is
2032 +  the typical case which aufs doesn't force.
2033 +  When aufs writes the actual filedata and the files consumes any of new
2034 +  blocks, the aufs forces notifying.
2035 +
2036 +
2037 +o Interfaces in aufs
2038 +- New branch attribute.
2039 +  + fhsm
2040 +    Specifies that the branch is managed by FHSM feature. In other word,
2041 +    participant in the FHSM.
2042 +    When nofhsm is set to the branch, it will not be the source/target
2043 +    branch of the move-down operation. This attribute is set
2044 +    independently from coo and moo attributes, and if you want full
2045 +    FHSM, you should specify them as well.
2046 +- New mount option.
2047 +  + fhsm_sec
2048 +    Specifies a second to suppress many less important info to be
2049 +    notified.
2050 +- New ioctl.
2051 +  + AUFS_CTL_FHSM_FD
2052 +    create a new file descriptor which userspace can read the notification
2053 +    (a subset of struct statfs) from aufs.
2054 +- Module parameter 'brs'
2055 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2056 +  be set.
2057 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2058 +  When there are two or more branches with fhsm attributes,
2059 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2060 +  terminates it. As a result of remounting and branch-manipulation, the
2061 +  number of branches with fhsm attribute can be one. In this case,
2062 +  /sbin/mount.aufs will terminate the user-space daemon.
2063 +
2064 +
2065 +Finally the operation is done as these steps in kernel-space.
2066 +- make sure that,
2067 +  + no one else is using the file.
2068 +  + the file is not hard-linked.
2069 +  + the file is not pseudo-linked.
2070 +  + the file is a regular file.
2071 +  + the parent dir is not opaqued.
2072 +- find the target writable branch.
2073 +- make sure the file is not whiteout-ed by the upper (than the target)
2074 +  branch.
2075 +- make the parent dir on the target branch.
2076 +- mutex lock the inode on the branch.
2077 +- unlink the whiteout on the target branch (if exists).
2078 +- lookup and create the whiteout-ed temporary name on the target branch.
2079 +- copy the file as the whiteout-ed temporary name on the target branch.
2080 +- rename the whiteout-ed temporary name to the original name.
2081 +- unlink the file on the source branch.
2082 +- maintain the internal pointer array and the external inode number
2083 +  table (XINO).
2084 +- maintain the timestamps and other attributes of the parent dir and the
2085 +  file.
2086 +
2087 +And of course, in every step, an error may happen. So the operation
2088 +should restore the original file state after an error happens.
2089 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2090 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2091 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-11-05 23:02:18.959222617 +0100
2092 @@ -0,0 +1,72 @@
2093 +
2094 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2095 +#
2096 +# This program is free software; you can redistribute it and/or modify
2097 +# it under the terms of the GNU General Public License as published by
2098 +# the Free Software Foundation; either version 2 of the License, or
2099 +# (at your option) any later version.
2100 +#
2101 +# This program is distributed in the hope that it will be useful,
2102 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2103 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2104 +# GNU General Public License for more details.
2105 +#
2106 +# You should have received a copy of the GNU General Public License
2107 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2108 +
2109 +mmap(2) -- File Memory Mapping
2110 +----------------------------------------------------------------------
2111 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2112 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2113 +->mmap().
2114 +This approach is simple and good, but there is one problem.
2115 +Under /proc, several entries show the mmapped files by its path (with
2116 +device and inode number), and the printed path will be the path on the
2117 +branch fs's instead of virtual aufs's.
2118 +This is not a problem in most cases, but some utilities lsof(1) (and its
2119 +user) may expect the path on aufs.
2120 +
2121 +To address this issue, aufs adds a new member called vm_prfile in struct
2122 +vm_area_struct (and struct vm_region). The original vm_file points to
2123 +the file on the branch fs in order to handle everything correctly as
2124 +usual. The new vm_prfile points to a virtual file in aufs, and the
2125 +show-functions in procfs refers to vm_prfile if it is set.
2126 +Also we need to maintain several other places where touching vm_file
2127 +such like
2128 +- fork()/clone() copies vma and the reference count of vm_file is
2129 +  incremented.
2130 +- merging vma maintains the ref count too.
2131 +
2132 +This is not a good approach. It just fakes the printed path. But it
2133 +leaves all behaviour around f_mapping unchanged. This is surely an
2134 +advantage.
2135 +Actually aufs had adopted another complicated approach which calls
2136 +generic_file_mmap() and handles struct vm_operations_struct. In this
2137 +approach, aufs met a hard problem and I could not solve it without
2138 +switching the approach.
2139 +
2140 +There may be one more another approach which is
2141 +- bind-mount the branch-root onto the aufs-root internally
2142 +- grab the new vfsmount (ie. struct mount)
2143 +- lazy-umount the branch-root internally
2144 +- in open(2) the aufs-file, open the branch-file with the hidden
2145 +  vfsmount (instead of the original branch's vfsmount)
2146 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2147 +  but it may be possible from userspace by the mount helper.
2148 +
2149 +Adding the internal hidden vfsmount and using it in opening a file, the
2150 +file path under /proc will be printed correctly. This approach looks
2151 +smarter, but is not possible I am afraid.
2152 +- aufs-root may be bind-mount later. when it happens, another hidden
2153 +  vfsmount will be required.
2154 +- it is hard to get the chance to bind-mount and lazy-umount
2155 +  + in kernel-space, FS can have vfsmount in open(2) via
2156 +    file->f_path, and aufs can know its vfsmount. But several locks are
2157 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2158 +    here, then it may cause a deadlock.
2159 +  + in user-space, bind-mount doesn't invoke the mount helper.
2160 +- since /proc shows dev and ino, aufs has to give vma these info. it
2161 +  means a new member vm_prinode will be necessary. this is essentially
2162 +  equivalent to vm_prfile described above.
2163 +
2164 +I have to give up this "looks-smater" approach.
2165 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2166 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2167 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-11-05 23:02:18.959222617 +0100
2168 @@ -0,0 +1,94 @@
2169 +
2170 +# Copyright (C) 2014-2022 Junjiro R. Okajima
2171 +#
2172 +# This program is free software; you can redistribute it and/or modify
2173 +# it under the terms of the GNU General Public License as published by
2174 +# the Free Software Foundation; either version 2 of the License, or
2175 +# (at your option) any later version.
2176 +#
2177 +# This program is distributed in the hope that it will be useful,
2178 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2179 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2180 +# GNU General Public License for more details.
2181 +#
2182 +# You should have received a copy of the GNU General Public License
2183 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2184 +
2185 +Listing XATTR/EA and getting the value
2186 +----------------------------------------------------------------------
2187 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2188 +shows the values from the topmost existing file. This behaviour is good
2189 +for the non-dir entries since the bahaviour exactly matches the shown
2190 +information. But for the directories, aufs considers all the same named
2191 +entries on the lower branches. Which means, if one of the lower entry
2192 +rejects readdir call, then aufs returns an error even if the topmost
2193 +entry allows it. This behaviour is necessary to respect the branch fs's
2194 +security, but can make users confused since the user-visible standard
2195 +attributes don't match the behaviour.
2196 +To address this issue, aufs has a mount option called dirperm1 which
2197 +checks the permission for the topmost entry only, and ignores the lower
2198 +entry's permission.
2199 +
2200 +A similar issue can happen around XATTR.
2201 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2202 +always set. Otherwise these very unpleasant situation would happen.
2203 +- listxattr(2) may return the duplicated entries.
2204 +- users may not be able to remove or reset the XATTR forever,
2205 +
2206 +
2207 +XATTR/EA support in the internal (copy,move)-(up,down)
2208 +----------------------------------------------------------------------
2209 +Generally the extended attributes of inode are categorized as these.
2210 +- "security" for LSM and capability.
2211 +- "system" for posix ACL, 'acl' mount option is required for the branch
2212 +  fs generally.
2213 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2214 +- "user" for userspace, 'user_xattr' mount option is required for the
2215 +  branch fs generally.
2216 +
2217 +Moreover there are some other categories. Aufs handles these rather
2218 +unpopular categories as the ordinary ones, ie. there is no special
2219 +condition nor exception.
2220 +
2221 +In copy-up, the support for XATTR on the dst branch may differ from the
2222 +src branch. In this case, the copy-up operation will get an error and
2223 +the original user operation which triggered the copy-up will fail. It
2224 +can happen that even all copy-up will fail.
2225 +When both of src and dst branches support XATTR and if an error occurs
2226 +during copying XATTR, then the copy-up should fail obviously. That is a
2227 +good reason and aufs should return an error to userspace. But when only
2228 +the src branch support that XATTR, aufs should not return an error.
2229 +For example, the src branch supports ACL but the dst branch doesn't
2230 +because the dst branch may natively un-support it or temporary
2231 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2232 +may NOT return an error even if the XATTR is not supported. It is
2233 +totally up to the branch fs.
2234 +
2235 +Anyway when the aufs internal copy-up gets an error from the dst branch
2236 +fs, then aufs tries removing the just copied entry and returns the error
2237 +to the userspace. The worst case of this situation will be all copy-up
2238 +will fail.
2239 +
2240 +For the copy-up operation, there two basic approaches.
2241 +- copy the specified XATTR only (by category above), and return the
2242 +  error unconditionally if it happens.
2243 +- copy all XATTR, and ignore the error on the specified category only.
2244 +
2245 +In order to support XATTR and to implement the correct behaviour, aufs
2246 +chooses the latter approach and introduces some new branch attributes,
2247 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2248 +They correspond to the XATTR namespaces (see above). Additionally, to be
2249 +convenient, "icex" is also provided which means all "icex*" attributes
2250 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2251 +
2252 +The meaning of these attributes is to ignore the error from setting
2253 +XATTR on that branch.
2254 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2255 +error from the dst branch according to the specified attributes.
2256 +
2257 +Some XATTR may have its default value. The default value may come from
2258 +the parent dir or the environment. If the default value is set at the
2259 +file creating-time, it will be overwritten by copy-up.
2260 +Some contradiction may happen I am afraid.
2261 +Do we need another attribute to stop copying XATTR? I am unsure. For
2262 +now, aufs implements the branch attributes to ignore the error.
2263 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2264 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2265 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-11-05 23:02:18.959222617 +0100
2266 @@ -0,0 +1,58 @@
2267 +
2268 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2269 +#
2270 +# This program is free software; you can redistribute it and/or modify
2271 +# it under the terms of the GNU General Public License as published by
2272 +# the Free Software Foundation; either version 2 of the License, or
2273 +# (at your option) any later version.
2274 +#
2275 +# This program is distributed in the hope that it will be useful,
2276 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2277 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2278 +# GNU General Public License for more details.
2279 +#
2280 +# You should have received a copy of the GNU General Public License
2281 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2282 +
2283 +Export Aufs via NFS
2284 +----------------------------------------------------------------------
2285 +Here is an approach.
2286 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2287 +  generation.
2288 +- iget_locked(): initialize aufs inode generation for a new inode, and
2289 +  store it in xigen file.
2290 +- destroy_inode(): increment aufs inode generation and store it in xigen
2291 +  file. it is necessary even if it is not unlinked, because any data of
2292 +  inode may be changed by UDBA.
2293 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2294 +  build file handle by
2295 +  + branch id (4 bytes)
2296 +  + superblock generation (4 bytes)
2297 +  + inode number (4 or 8 bytes)
2298 +  + parent dir inode number (4 or 8 bytes)
2299 +  + inode generation (4 bytes))
2300 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2301 +    bytes)
2302 +  + file handle for a branch (by exportfs_encode_fh())
2303 +- fh_to_dentry():
2304 +  + find the index of a branch from its id in handle, and check it is
2305 +    still exist in aufs.
2306 +  + 1st level: get the inode number from handle and search it in cache.
2307 +  + 2nd level: if not found in cache, get the parent inode number from
2308 +    the handle and search it in cache. and then open the found parent
2309 +    dir, find the matching inode number by vfs_readdir() and get its
2310 +    name, and call lookup_one_len() for the target dentry.
2311 +  + 3rd level: if the parent dir is not cached, call
2312 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2313 +    build a pathname of it, convert it a pathname in aufs, call
2314 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2315 +    the 2nd level.
2316 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2317 +    for every branch, but not itself. to get this, (currently) aufs
2318 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2319 +    idea, but I didn't get other approach.
2320 +  + test the generation of the gotten inode.
2321 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2322 +  convert it into ESTALE for NFSD.
2323 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2324 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2325 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2326 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2327 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-11-05 23:02:18.959222617 +0100
2328 @@ -0,0 +1,52 @@
2329 +
2330 +# Copyright (C) 2005-2022 Junjiro R. Okajima
2331 +#
2332 +# This program is free software; you can redistribute it and/or modify
2333 +# it under the terms of the GNU General Public License as published by
2334 +# the Free Software Foundation; either version 2 of the License, or
2335 +# (at your option) any later version.
2336 +#
2337 +# This program is distributed in the hope that it will be useful,
2338 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2339 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2340 +# GNU General Public License for more details.
2341 +#
2342 +# You should have received a copy of the GNU General Public License
2343 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2344 +
2345 +Show Whiteout Mode (shwh)
2346 +----------------------------------------------------------------------
2347 +Generally aufs hides the name of whiteouts. But in some cases, to show
2348 +them is very useful for users. For instance, creating a new middle layer
2349 +(branch) by merging existing layers.
2350 +
2351 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2352 +When you have three branches,
2353 +- Bottom: 'system', squashfs (underlying base system), read-only
2354 +- Middle: 'mods', squashfs, read-only
2355 +- Top: 'overlay', ram (tmpfs), read-write
2356 +
2357 +The top layer is loaded at boot time and saved at shutdown, to preserve
2358 +the changes made to the system during the session.
2359 +When larger changes have been made, or smaller changes have accumulated,
2360 +the size of the saved top layer data grows. At this point, it would be
2361 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2362 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2363 +restoring save and load speed.
2364 +
2365 +This merging is simplified by the use of another aufs mount, of just the
2366 +two overlay branches using the 'shwh' option.
2367 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2368 +       aufs /livesys/merge_union
2369 +
2370 +A merged view of these two branches is then available at
2371 +/livesys/merge_union, and the new feature is that the whiteouts are
2372 +visible!
2373 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2374 +writing to all branches. Also the default mode for all branches is 'ro'.
2375 +It is now possible to save the combined contents of the two overlay
2376 +branches to a new squashfs, e.g.:
2377 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2378 +
2379 +This new squashfs archive can be stored on the boot device and the
2380 +initramfs will use it to replace the old one at the next boot.
2381 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2382 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2383 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-11-05 23:02:18.959222617 +0100
2384 @@ -0,0 +1,47 @@
2385 +
2386 +# Copyright (C) 2010-2022 Junjiro R. Okajima
2387 +#
2388 +# This program is free software; you can redistribute it and/or modify
2389 +# it under the terms of the GNU General Public License as published by
2390 +# the Free Software Foundation; either version 2 of the License, or
2391 +# (at your option) any later version.
2392 +#
2393 +# This program is distributed in the hope that it will be useful,
2394 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2395 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2396 +# GNU General Public License for more details.
2397 +#
2398 +# You should have received a copy of the GNU General Public License
2399 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2400 +
2401 +Dynamically customizable FS operations
2402 +----------------------------------------------------------------------
2403 +Generally FS operations (struct inode_operations, struct
2404 +address_space_operations, struct file_operations, etc.) are defined as
2405 +"static const", but it never means that FS have only one set of
2406 +operation. Some FS have multiple sets of them. For instance, ext2 has
2407 +three sets, one for XIP, for NOBH, and for normal.
2408 +Since aufs overrides and redirects these operations, sometimes aufs has
2409 +to change its behaviour according to the branch FS type. More importantly
2410 +VFS acts differently if a function (member in the struct) is set or
2411 +not. It means aufs should have several sets of operations and select one
2412 +among them according to the branch FS definition.
2413 +
2414 +In order to solve this problem and not to affect the behaviour of VFS,
2415 +aufs defines these operations dynamically. For instance, aufs defines
2416 +dummy direct_IO function for struct address_space_operations, but it may
2417 +not be set to the address_space_operations actually. When the branch FS
2418 +doesn't have it, aufs doesn't set it to its address_space_operations
2419 +while the function definition itself is still alive. So the behaviour
2420 +itself will not change, and it will return an error when direct_IO is
2421 +not set.
2422 +
2423 +The lifetime of these dynamically generated operation object is
2424 +maintained by aufs branch object. When the branch is removed from aufs,
2425 +the reference counter of the object is decremented. When it reaches
2426 +zero, the dynamically generated operation object will be freed.
2427 +
2428 +This approach is designed to support AIO (io_submit), Direct I/O and
2429 +XIP (DAX) mainly.
2430 +Currently this approach is applied to address_space_operations for
2431 +regular files only.
2432 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2433 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2434 +++ linux/Documentation/filesystems/aufs/README 2022-11-05 23:02:18.955889283 +0100
2435 @@ -0,0 +1,403 @@
2436 +
2437 +Aufs6 -- advanced multi layered unification filesystem version 6.x
2438 +http://aufs.sf.net
2439 +Junjiro R. Okajima
2440 +
2441 +
2442 +0. Introduction
2443 +----------------------------------------
2444 +In the early days, aufs was entirely re-designed and re-implemented
2445 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2446 +improvements and implementations, it became totally different from
2447 +Unionfs while keeping the basic features.
2448 +Later, Unionfs Version 2.x series began taking some of the same
2449 +approaches to aufs1's.
2450 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2451 +University and his team.
2452 +
2453 +Aufs6 supports linux-v6.0 and later, try aufs6.0 branch in
2454 +aufs-linux.git or aufs-standalone.git.
2455 +If you want older kernel version support,
2456 +- for linux-v5.x series, try aufs-linux.git or aufs-standalone.git
2457 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2458 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2459 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2460 +  or aufs1 from CVS on SourceForge.
2461 +
2462 +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
2463 +      were changed. Now they are aufs-linux.git and
2464 +      aufs-standalone.git and they contain aufs5 and later branches.
2465 +
2466 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2467 +      According to Christoph Hellwig, linux rejects all union-type
2468 +      filesystems but UnionMount.
2469 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2470 +
2471 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2472 +    UnionMount, and he pointed out an issue around a directory mutex
2473 +    lock and aufs addressed it. But it is still unsure whether aufs will
2474 +    be merged (or any other union solution).
2475 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2476 +
2477 +
2478 +1. Features
2479 +----------------------------------------
2480 +- unite several directories into a single virtual filesystem. The member
2481 +  directory is called as a branch.
2482 +- you can specify the permission flags to the branch, which are 'readonly',
2483 +  'readwrite' and 'whiteout-able.'
2484 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2485 +  readonly branch are modifiable logically.
2486 +- dynamic branch manipulation, add, del.
2487 +- etc...
2488 +
2489 +Also there are many enhancements in aufs, such as:
2490 +- test only the highest one for the directory permission (dirperm1)
2491 +- copyup on open (coo=)
2492 +- 'move' policy for copy-up between two writable branches, after
2493 +  checking free space.
2494 +- xattr, acl
2495 +- readdir(3) in userspace.
2496 +- keep inode number by external inode number table
2497 +- keep the timestamps of file/dir in internal copyup operation
2498 +- seekable directory, supporting NFS readdir.
2499 +- whiteout is hardlinked in order to reduce the consumption of inodes
2500 +  on branch
2501 +- do not copyup, nor create a whiteout when it is unnecessary
2502 +- revert a single systemcall when an error occurs in aufs
2503 +- remount interface instead of ioctl
2504 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2505 +- loopback mounted filesystem as a branch
2506 +- kernel thread for removing the dir who has a plenty of whiteouts
2507 +- support copyup sparse file (a file which has a 'hole' in it)
2508 +- default permission flags for branches
2509 +- selectable permission flags for ro branch, whether whiteout can
2510 +  exist or not
2511 +- export via NFS.
2512 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2513 +- support multiple writable branches, some policies to select one
2514 +  among multiple writable branches.
2515 +- a new semantics for link(2) and rename(2) to support multiple
2516 +  writable branches.
2517 +- no glibc changes are required.
2518 +- pseudo hardlink (hardlink over branches)
2519 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2520 +  including NFS or remote filesystem branch.
2521 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2522 +- and more...
2523 +
2524 +Currently these features are dropped temporary from aufs6.
2525 +See design/08plan.txt in detail.
2526 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2527 +  (robr)
2528 +- statistics of aufs thread (/sys/fs/aufs/stat)
2529 +
2530 +Features or just an idea in the future (see also design/*.txt),
2531 +- reorder the branch index without del/re-add.
2532 +- permanent xino files for NFSD
2533 +- an option for refreshing the opened files after add/del branches
2534 +- light version, without branch manipulation. (unnecessary?)
2535 +- copyup in userspace
2536 +- inotify in userspace
2537 +- readv/writev
2538 +
2539 +
2540 +2. Download
2541 +----------------------------------------
2542 +There are three GIT trees for aufs6, aufs6-linux.git,
2543 +aufs6-standalone.git, and aufs-util.git. Note that there is no "6" in
2544 +"aufs-util.git."
2545 +While the aufs-util is always necessary, you need either of aufs6-linux
2546 +or aufs6-standalone.
2547 +
2548 +The aufs6-linux tree includes the whole linux mainline GIT tree,
2549 +git://git.kernel.org/.../torvalds/linux.git.
2550 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2551 +build aufs6 as an external kernel module.
2552 +Several extra patches are not included in this tree. Only
2553 +aufs6-standalone tree contains them. They are described in the later
2554 +section "Configuration and Compilation."
2555 +
2556 +On the other hand, the aufs6-standalone tree has only aufs source files
2557 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2558 +But you need to apply all aufs patches manually.
2559 +
2560 +You will find GIT branches whose name is in form of "aufs6.x" where "x"
2561 +represents the linux kernel version, "linux-6.x". For instance,
2562 +"aufs6.0" is for linux-6.0. For latest "linux-6.x-rcN", use
2563 +"aufs6.x-rcN" branch.
2564 +
2565 +o aufs6-linux tree
2566 +$ git clone --reference /your/linux/git/tree \
2567 +       git://github.com/sfjro/aufs6-linux.git aufs6-linux.git
2568 +- if you don't have linux GIT tree, then remove "--reference ..."
2569 +$ cd aufs6-linux.git
2570 +$ git checkout origin/aufs6.0
2571 +
2572 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2573 +leave the patch-work to GIT.
2574 +$ cd /your/linux/git/tree
2575 +$ git remote add aufs6 git://github.com/sfjro/aufs6-linux.git
2576 +$ git fetch aufs6
2577 +$ git checkout -b my6.0 v6.0
2578 +$ (add your local change...)
2579 +$ git pull aufs6 aufs6.0
2580 +- now you have v6.0 + your_changes + aufs6.0 in you my6.0 branch.
2581 +- you may need to solve some conflicts between your_changes and
2582 +  aufs6.0. in this case, git-rerere is recommended so that you can
2583 +  solve the similar conflicts automatically when you upgrade to 6.1 or
2584 +  later in the future.
2585 +
2586 +o aufs6-standalone tree
2587 +$ git clone git://github.com/sfjro/aufs6-standalone.git aufs6-standalone.git
2588 +$ cd aufs6-standalone.git
2589 +$ git checkout origin/aufs6.0
2590 +
2591 +o aufs-util tree
2592 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2593 +- note that the public aufs-util.git is on SourceForge instead of
2594 +  GitHUB.
2595 +$ cd aufs-util.git
2596 +$ git checkout origin/aufs6.0
2597 +
2598 +Note: The 6.x-rcN branch is to be used with `rc' kernel versions ONLY.
2599 +The minor version number, 'x' in '6.x', of aufs may not always
2600 +follow the minor version number of the kernel.
2601 +Because changes in the kernel that cause the use of a new
2602 +minor version number do not always require changes to aufs-util.
2603 +
2604 +Since aufs-util has its own minor version number, you may not be
2605 +able to find a GIT branch in aufs-util for your kernel's
2606 +exact minor version number.
2607 +In this case, you should git-checkout the branch for the
2608 +nearest lower number.
2609 +
2610 +For (an unreleased) example:
2611 +If you are using "linux-6.10" and the "aufs6.10" branch
2612 +does not exist in aufs-util repository, then "aufs6.9", "aufs6.8"
2613 +or something numerically smaller is the branch for your kernel.
2614 +
2615 +Also you can view all branches by
2616 +       $ git branch -a
2617 +
2618 +
2619 +3. Configuration and Compilation
2620 +----------------------------------------
2621 +Make sure you have git-checkout'ed the correct branch.
2622 +
2623 +For aufs6-linux tree,
2624 +- enable CONFIG_AUFS_FS.
2625 +- set other aufs configurations if necessary.
2626 +- for aufs5.13 and later
2627 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2628 +  also a caller of VFS functions for branch filesystems, subclassing of
2629 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2630 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2631 +  need to customize some LOCKDEP numbers. Here are what I use on my
2632 +  test environment.
2633 +       CONFIG_LOCKDEP_BITS=21
2634 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2635 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2636 +
2637 +For aufs6-standalone tree,
2638 +There are several ways to build.
2639 +
2640 +1.
2641 +- apply ./aufs6-kbuild.patch to your kernel source files.
2642 +- apply ./aufs6-base.patch too.
2643 +- apply ./aufs6-mmap.patch too.
2644 +- apply ./aufs6-standalone.patch too, if you have a plan to set
2645 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs6-standalone.patch.
2646 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2647 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2648 +- enable CONFIG_AUFS_FS, you can select either
2649 +  =m or =y.
2650 +- and build your kernel as usual.
2651 +- install the built kernel.
2652 +- install the header files too by "make headers_install" to the
2653 +  directory where you specify. By default, it is $PWD/usr.
2654 +  "make help" shows a brief note for headers_install.
2655 +- and reboot your system.
2656 +
2657 +2.
2658 +- module only (CONFIG_AUFS_FS=m).
2659 +- apply ./aufs6-base.patch to your kernel source files.
2660 +- apply ./aufs6-mmap.patch too.
2661 +- apply ./aufs6-standalone.patch too.
2662 +- build your kernel, don't forget "make headers_install", and reboot.
2663 +- edit ./config.mk and set other aufs configurations if necessary.
2664 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2665 +  every aufs configurations.
2666 +- build the module by simple "make".
2667 +- you can specify ${KDIR} make variable which points to your kernel
2668 +  source tree.
2669 +- install the files
2670 +  + run "make install" to install the aufs module, or copy the built
2671 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2672 +  + run "make install_headers" (instead of headers_install) to install
2673 +    the modified aufs header file (you can specify DESTDIR which is
2674 +    available in aufs standalone version's Makefile only), or copy
2675 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2676 +    you like manually. By default, the target directory is $PWD/usr.
2677 +- no need to apply aufs6-kbuild.patch, nor copying source files to your
2678 +  kernel source tree.
2679 +
2680 +Note: The header file aufs_type.h is necessary to build aufs-util
2681 +      as well as "make headers_install" in the kernel source tree.
2682 +      headers_install is subject to be forgotten, but it is essentially
2683 +      necessary, not only for building aufs-util.
2684 +      You may not meet problems without headers_install in some older
2685 +      version though.
2686 +
2687 +And then,
2688 +- read README in aufs-util, build and install it
2689 +- note that your distribution may contain an obsoleted version of
2690 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2691 +  utilities, make sure that your compiler refers the correct aufs header
2692 +  file which is built by "make headers_install."
2693 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2694 +  then run "make install_ulib" too. And refer to the aufs manual in
2695 +  detail.
2696 +
2697 +There several other patches in aufs6-standalone.git. They are all
2698 +optional. When you meet some problems, they will help you.
2699 +- aufs6-loopback.patch
2700 +  Supports a nested loopback mount in a branch-fs. This patch is
2701 +  unnecessary until aufs produces a message like "you may want to try
2702 +  another patch for loopback file".
2703 +- vfs-ino.patch
2704 +  Modifies a system global kernel internal function get_next_ino() in
2705 +  order to stop assigning 0 for an inode-number. Not directly related to
2706 +  aufs, but recommended generally.
2707 +- tmpfs-idr.patch
2708 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2709 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2710 +  duplication of inode number, which is important for backup tools and
2711 +  other utilities. When you find aufs XINO files for tmpfs branch
2712 +  growing too much, try this patch.
2713 +
2714 +
2715 +4. Usage
2716 +----------------------------------------
2717 +At first, make sure aufs-util are installed, and please read the aufs
2718 +manual, aufs.6 in aufs-util.git tree.
2719 +$ man -l aufs.6
2720 +
2721 +And then,
2722 +$ mkdir /tmp/rw /tmp/aufs
2723 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2724 +
2725 +Here is another example. The result is equivalent.
2726 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2727 +  Or
2728 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2729 +# mount -o remount,append:${HOME} /tmp/aufs
2730 +
2731 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2732 +you modify a file under /tmp/aufs, the one on your home directory is
2733 +not affected, instead the same named file will be newly created under
2734 +/tmp/rw. And all of your modification to a file will be applied to
2735 +the one under /tmp/rw. This is called the file based Copy on Write
2736 +(COW) method.
2737 +Aufs mount options are described in aufs.6.
2738 +If you run chroot or something and make your aufs as a root directory,
2739 +then you need to customize the shutdown script. See the aufs manual in
2740 +detail.
2741 +
2742 +Additionally, there are some sample usages of aufs which are a
2743 +diskless system with network booting, and LiveCD over NFS.
2744 +See sample dir in CVS tree on SourceForge.
2745 +
2746 +
2747 +5. Contact
2748 +----------------------------------------
2749 +When you have any problems or strange behaviour in aufs, please let me
2750 +know with:
2751 +- /proc/mounts (instead of the output of mount(8))
2752 +- /sys/module/aufs/*
2753 +- /sys/fs/aufs/* (if you have them)
2754 +- /debug/aufs/* (if you have them)
2755 +- linux kernel version
2756 +  if your kernel is not plain, for example modified by distributor,
2757 +  the url where i can download its source is necessary too.
2758 +- aufs version which was printed at loading the module or booting the
2759 +  system, instead of the date you downloaded.
2760 +- configuration (define/undefine CONFIG_AUFS_xxx)
2761 +- kernel configuration or /proc/config.gz (if you have it)
2762 +- LSM (linux security module, if you are using)
2763 +- behaviour which you think to be incorrect
2764 +- actual operation, reproducible one is better
2765 +- mailto: aufs-users at lists.sourceforge.net
2766 +
2767 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2768 +and Feature Requests) on SourceForge. Please join and write to
2769 +aufs-users ML.
2770 +
2771 +
2772 +6. Acknowledgements
2773 +----------------------------------------
2774 +Thanks to everyone who have tried and are using aufs, whoever
2775 +have reported a bug or any feedback.
2776 +
2777 +Especially donators:
2778 +Tomas Matejicek(slax.org) made a donation (much more than once).
2779 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2780 +       scripts) is making "doubling" donations.
2781 +       Unfortunately I cannot list all of the donators, but I really
2782 +       appreciate.
2783 +       It ends Aug 2010, but the ordinary donation URL is still available.
2784 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2785 +Dai Itasaka made a donation (2007/8).
2786 +Chuck Smith made a donation (2008/4, 10 and 12).
2787 +Henk Schoneveld made a donation (2008/9).
2788 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2789 +Francois Dupoux made a donation (2008/11).
2790 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2791 +       aufs2 GIT tree (2009/2).
2792 +William Grant made a donation (2009/3).
2793 +Patrick Lane made a donation (2009/4).
2794 +The Mail Archive (mail-archive.com) made donations (2009/5).
2795 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2796 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2797 +Pavel Pronskiy made a donation (2011/2).
2798 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2799 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2800 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2801 +11).
2802 +Sam Liddicott made a donation (2011/9).
2803 +Era Scarecrow made a donation (2013/4).
2804 +Bor Ratajc made a donation (2013/4).
2805 +Alessandro Gorreta made a donation (2013/4).
2806 +POIRETTE Marc made a donation (2013/4).
2807 +Alessandro Gorreta made a donation (2013/4).
2808 +lauri kasvandik made a donation (2013/5).
2809 +"pemasu from Finland" made a donation (2013/7).
2810 +The Parted Magic Project made a donation (2013/9 and 11).
2811 +Pavel Barta made a donation (2013/10).
2812 +Nikolay Pertsev made a donation (2014/5).
2813 +James B made a donation (2014/7, 2015/7, and 2021/12).
2814 +Stefano Di Biase made a donation (2014/8).
2815 +Daniel Epellei made a donation (2015/1).
2816 +OmegaPhil made a donation (2016/1, 2018/4).
2817 +Tomasz Szewczyk made a donation (2016/4).
2818 +James Burry made a donation (2016/12).
2819 +Carsten Rose made a donation (2018/9).
2820 +Porteus Kiosk made a donation (2018/10).
2821 +Enya Quetzalli Gomez Rodriguez made a donation (2022/5).
2822 +
2823 +Thank you very much.
2824 +Donations are always, including future donations, very important and
2825 +helpful for me to keep on developing aufs.
2826 +
2827 +
2828 +7.
2829 +----------------------------------------
2830 +If you are an experienced user, no explanation is needed. Aufs is
2831 +just a linux filesystem.
2832 +
2833 +
2834 +Enjoy!
2835 +
2836 +# Local variables: ;
2837 +# mode: text;
2838 +# End: ;
2839 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2840 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2841 +++ linux/fs/aufs/aufs.h        2022-11-05 23:02:18.959222617 +0100
2842 @@ -0,0 +1,62 @@
2843 +/* SPDX-License-Identifier: GPL-2.0 */
2844 +/*
2845 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2846 + *
2847 + * This program is free software; you can redistribute it and/or modify
2848 + * it under the terms of the GNU General Public License as published by
2849 + * the Free Software Foundation; either version 2 of the License, or
2850 + * (at your option) any later version.
2851 + *
2852 + * This program is distributed in the hope that it will be useful,
2853 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2854 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2855 + * GNU General Public License for more details.
2856 + *
2857 + * You should have received a copy of the GNU General Public License
2858 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2859 + */
2860 +
2861 +/*
2862 + * all header files
2863 + */
2864 +
2865 +#ifndef __AUFS_H__
2866 +#define __AUFS_H__
2867 +
2868 +#ifdef __KERNEL__
2869 +
2870 +#define AuStub(type, name, body, ...) \
2871 +       static inline type name(__VA_ARGS__) { body; }
2872 +
2873 +#define AuStubVoid(name, ...) \
2874 +       AuStub(void, name, , __VA_ARGS__)
2875 +#define AuStubInt0(name, ...) \
2876 +       AuStub(int, name, return 0, __VA_ARGS__)
2877 +
2878 +#include "debug.h"
2879 +
2880 +#include "branch.h"
2881 +#include "cpup.h"
2882 +#include "dcsub.h"
2883 +#include "dbgaufs.h"
2884 +#include "dentry.h"
2885 +#include "dir.h"
2886 +#include "dirren.h"
2887 +#include "dynop.h"
2888 +#include "file.h"
2889 +#include "fstype.h"
2890 +#include "hbl.h"
2891 +#include "inode.h"
2892 +#include "lcnt.h"
2893 +#include "loop.h"
2894 +#include "module.h"
2895 +#include "opts.h"
2896 +#include "rwsem.h"
2897 +#include "super.h"
2898 +#include "sysaufs.h"
2899 +#include "vfsub.h"
2900 +#include "whout.h"
2901 +#include "wkq.h"
2902 +
2903 +#endif /* __KERNEL__ */
2904 +#endif /* __AUFS_H__ */
2905 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2906 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2907 +++ linux/fs/aufs/branch.c      2022-11-05 23:02:18.959222617 +0100
2908 @@ -0,0 +1,1427 @@
2909 +// SPDX-License-Identifier: GPL-2.0
2910 +/*
2911 + * Copyright (C) 2005-2022 Junjiro R. Okajima
2912 + *
2913 + * This program is free software; you can redistribute it and/or modify
2914 + * it under the terms of the GNU General Public License as published by
2915 + * the Free Software Foundation; either version 2 of the License, or
2916 + * (at your option) any later version.
2917 + *
2918 + * This program is distributed in the hope that it will be useful,
2919 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2920 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2921 + * GNU General Public License for more details.
2922 + *
2923 + * You should have received a copy of the GNU General Public License
2924 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2925 + */
2926 +
2927 +/*
2928 + * branch management
2929 + */
2930 +
2931 +#include <linux/compat.h>
2932 +#include <linux/statfs.h>
2933 +#include "aufs.h"
2934 +
2935 +/*
2936 + * free a single branch
2937 + */
2938 +static void au_br_do_free(struct au_branch *br)
2939 +{
2940 +       int i;
2941 +       struct au_wbr *wbr;
2942 +       struct au_dykey **key;
2943 +
2944 +       au_hnotify_fin_br(br);
2945 +       /* always, regardless the mount option */
2946 +       au_dr_hino_free(&br->br_dirren);
2947 +       au_xino_put(br);
2948 +
2949 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2950 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2951 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2952 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2953 +
2954 +       wbr = br->br_wbr;
2955 +       if (wbr) {
2956 +               for (i = 0; i < AuBrWh_Last; i++)
2957 +                       dput(wbr->wbr_wh[i]);
2958 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2959 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2960 +       }
2961 +
2962 +       if (br->br_fhsm) {
2963 +               au_br_fhsm_fin(br->br_fhsm);
2964 +               au_kfree_try_rcu(br->br_fhsm);
2965 +       }
2966 +
2967 +       key = br->br_dykey;
2968 +       for (i = 0; i < AuBrDynOp; i++, key++)
2969 +               if (*key)
2970 +                       au_dy_put(*key);
2971 +               else
2972 +                       break;
2973 +
2974 +       /* recursive lock, s_umount of branch's */
2975 +       /* synchronize_rcu(); */ /* why? */
2976 +       lockdep_off();
2977 +       path_put(&br->br_path);
2978 +       lockdep_on();
2979 +       au_kfree_rcu(wbr);
2980 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2981 +       au_lcnt_wait_for_fin(&br->br_count);
2982 +       /* I don't know why, but percpu_refcount requires this */
2983 +       /* synchronize_rcu(); */
2984 +       au_kfree_rcu(br);
2985 +}
2986 +
2987 +/*
2988 + * frees all branches
2989 + */
2990 +void au_br_free(struct au_sbinfo *sbinfo)
2991 +{
2992 +       aufs_bindex_t bmax;
2993 +       struct au_branch **br;
2994 +
2995 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2996 +
2997 +       bmax = sbinfo->si_bbot + 1;
2998 +       br = sbinfo->si_branch;
2999 +       while (bmax--)
3000 +               au_br_do_free(*br++);
3001 +}
3002 +
3003 +/*
3004 + * find the index of a branch which is specified by @br_id.
3005 + */
3006 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3007 +{
3008 +       aufs_bindex_t bindex, bbot;
3009 +
3010 +       bbot = au_sbbot(sb);
3011 +       for (bindex = 0; bindex <= bbot; bindex++)
3012 +               if (au_sbr_id(sb, bindex) == br_id)
3013 +                       return bindex;
3014 +       return -1;
3015 +}
3016 +
3017 +/* ---------------------------------------------------------------------- */
3018 +
3019 +/*
3020 + * add a branch
3021 + */
3022 +
3023 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3024 +                       struct dentry *h_root)
3025 +{
3026 +       if (unlikely(h_adding == h_root
3027 +                    || au_test_loopback_overlap(sb, h_adding)))
3028 +               return 1;
3029 +       if (h_adding->d_sb != h_root->d_sb)
3030 +               return 0;
3031 +       return au_test_subdir(h_adding, h_root)
3032 +               || au_test_subdir(h_root, h_adding);
3033 +}
3034 +
3035 +/*
3036 + * returns a newly allocated branch. @new_nbranch is a number of branches
3037 + * after adding a branch.
3038 + */
3039 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3040 +                                    int perm)
3041 +{
3042 +       struct au_branch *add_branch;
3043 +       struct dentry *root;
3044 +       struct inode *inode;
3045 +       int err;
3046 +
3047 +       err = -ENOMEM;
3048 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3049 +       if (unlikely(!add_branch))
3050 +               goto out;
3051 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3052 +       if (unlikely(!add_branch->br_xino))
3053 +               goto out_br;
3054 +       err = au_hnotify_init_br(add_branch, perm);
3055 +       if (unlikely(err))
3056 +               goto out_xino;
3057 +
3058 +       if (au_br_writable(perm)) {
3059 +               /* may be freed separately at changing the branch permission */
3060 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3061 +                                            GFP_NOFS);
3062 +               if (unlikely(!add_branch->br_wbr))
3063 +                       goto out_hnotify;
3064 +       }
3065 +
3066 +       if (au_br_fhsm(perm)) {
3067 +               err = au_fhsm_br_alloc(add_branch);
3068 +               if (unlikely(err))
3069 +                       goto out_wbr;
3070 +       }
3071 +
3072 +       root = sb->s_root;
3073 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3074 +       if (!err)
3075 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3076 +       if (!err) {
3077 +               inode = d_inode(root);
3078 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3079 +                                       /*may_shrink*/0);
3080 +       }
3081 +       if (!err)
3082 +               return add_branch; /* success */
3083 +
3084 +out_wbr:
3085 +       au_kfree_rcu(add_branch->br_wbr);
3086 +out_hnotify:
3087 +       au_hnotify_fin_br(add_branch);
3088 +out_xino:
3089 +       au_xino_put(add_branch);
3090 +out_br:
3091 +       au_kfree_rcu(add_branch);
3092 +out:
3093 +       return ERR_PTR(err);
3094 +}
3095 +
3096 +/*
3097 + * test if the branch permission is legal or not.
3098 + */
3099 +static int test_br(struct inode *inode, int brperm, char *path)
3100 +{
3101 +       int err;
3102 +
3103 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3104 +       if (!err)
3105 +               goto out;
3106 +
3107 +       err = -EINVAL;
3108 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3109 +
3110 +out:
3111 +       return err;
3112 +}
3113 +
3114 +/*
3115 + * returns:
3116 + * 0: success, the caller will add it
3117 + * plus: success, it is already unified, the caller should ignore it
3118 + * minus: error
3119 + */
3120 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3121 +{
3122 +       int err;
3123 +       aufs_bindex_t bbot, bindex;
3124 +       struct dentry *root, *h_dentry;
3125 +       struct inode *inode, *h_inode;
3126 +
3127 +       root = sb->s_root;
3128 +       bbot = au_sbbot(sb);
3129 +       if (unlikely(bbot >= 0
3130 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3131 +               err = 1;
3132 +               if (!remount) {
3133 +                       err = -EINVAL;
3134 +                       pr_err("%s duplicated\n", add->pathname);
3135 +               }
3136 +               goto out;
3137 +       }
3138 +
3139 +       err = -ENOSPC; /* -E2BIG; */
3140 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3141 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3142 +               pr_err("number of branches exceeded %s\n", add->pathname);
3143 +               goto out;
3144 +       }
3145 +
3146 +       err = -EDOM;
3147 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3148 +               pr_err("bad index %d\n", add->bindex);
3149 +               goto out;
3150 +       }
3151 +
3152 +       inode = d_inode(add->path.dentry);
3153 +       err = -ENOENT;
3154 +       if (unlikely(!inode->i_nlink)) {
3155 +               pr_err("no existence %s\n", add->pathname);
3156 +               goto out;
3157 +       }
3158 +
3159 +       err = -EINVAL;
3160 +       if (unlikely(inode->i_sb == sb)) {
3161 +               pr_err("%s must be outside\n", add->pathname);
3162 +               goto out;
3163 +       }
3164 +
3165 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3166 +               pr_err("unsupported filesystem, %s (%s)\n",
3167 +                      add->pathname, au_sbtype(inode->i_sb));
3168 +               goto out;
3169 +       }
3170 +
3171 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3172 +               pr_err("already stacked, %s (%s)\n",
3173 +                      add->pathname, au_sbtype(inode->i_sb));
3174 +               goto out;
3175 +       }
3176 +
3177 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3178 +       if (unlikely(err))
3179 +               goto out;
3180 +
3181 +       if (bbot < 0)
3182 +               return 0; /* success */
3183 +
3184 +       err = -EINVAL;
3185 +       for (bindex = 0; bindex <= bbot; bindex++)
3186 +               if (unlikely(test_overlap(sb, add->path.dentry,
3187 +                                         au_h_dptr(root, bindex)))) {
3188 +                       pr_err("%s is overlapped\n", add->pathname);
3189 +                       goto out;
3190 +               }
3191 +
3192 +       err = 0;
3193 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3194 +               h_dentry = au_h_dptr(root, 0);
3195 +               h_inode = d_inode(h_dentry);
3196 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3197 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3198 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3199 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3200 +                               add->pathname,
3201 +                               i_uid_read(inode), i_gid_read(inode),
3202 +                               (inode->i_mode & S_IALLUGO),
3203 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3204 +                               (h_inode->i_mode & S_IALLUGO));
3205 +       }
3206 +
3207 +out:
3208 +       return err;
3209 +}
3210 +
3211 +/*
3212 + * initialize or clean the whiteouts for an adding branch
3213 + */
3214 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3215 +                        int new_perm)
3216 +{
3217 +       int err, old_perm;
3218 +       aufs_bindex_t bindex;
3219 +       struct inode *h_inode;
3220 +       struct au_wbr *wbr;
3221 +       struct au_hinode *hdir;
3222 +       struct dentry *h_dentry;
3223 +
3224 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3225 +       if (unlikely(err))
3226 +               goto out;
3227 +
3228 +       wbr = br->br_wbr;
3229 +       old_perm = br->br_perm;
3230 +       br->br_perm = new_perm;
3231 +       hdir = NULL;
3232 +       h_inode = NULL;
3233 +       bindex = au_br_index(sb, br->br_id);
3234 +       if (0 <= bindex) {
3235 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3236 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3237 +       } else {
3238 +               h_dentry = au_br_dentry(br);
3239 +               h_inode = d_inode(h_dentry);
3240 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3241 +       }
3242 +       if (!wbr)
3243 +               err = au_wh_init(br, sb);
3244 +       else {
3245 +               wbr_wh_write_lock(wbr);
3246 +               err = au_wh_init(br, sb);
3247 +               wbr_wh_write_unlock(wbr);
3248 +       }
3249 +       if (hdir)
3250 +               au_hn_inode_unlock(hdir);
3251 +       else
3252 +               inode_unlock(h_inode);
3253 +       vfsub_mnt_drop_write(au_br_mnt(br));
3254 +       br->br_perm = old_perm;
3255 +
3256 +       if (!err && wbr && !au_br_writable(new_perm)) {
3257 +               au_kfree_rcu(wbr);
3258 +               br->br_wbr = NULL;
3259 +       }
3260 +
3261 +out:
3262 +       return err;
3263 +}
3264 +
3265 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3266 +                      int perm)
3267 +{
3268 +       int err;
3269 +       struct kstatfs kst;
3270 +       struct au_wbr *wbr;
3271 +
3272 +       wbr = br->br_wbr;
3273 +       au_rw_init(&wbr->wbr_wh_rwsem);
3274 +       atomic_set(&wbr->wbr_wh_running, 0);
3275 +
3276 +       /*
3277 +        * a limit for rmdir/rename a dir
3278 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3279 +        */
3280 +       err = vfs_statfs(&br->br_path, &kst);
3281 +       if (unlikely(err))
3282 +               goto out;
3283 +       err = -EINVAL;
3284 +       if (kst.f_namelen >= NAME_MAX)
3285 +               err = au_br_init_wh(sb, br, perm);
3286 +       else
3287 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3288 +                      au_br_dentry(br),
3289 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3290 +
3291 +out:
3292 +       return err;
3293 +}
3294 +
3295 +/* initialize a new branch */
3296 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3297 +                     struct au_opt_add *add)
3298 +{
3299 +       int err;
3300 +       struct au_branch *brbase;
3301 +       struct file *xf;
3302 +       struct inode *h_inode;
3303 +
3304 +       err = 0;
3305 +       br->br_perm = add->perm;
3306 +       br->br_path = add->path; /* set first, path_get() later */
3307 +       spin_lock_init(&br->br_dykey_lock);
3308 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3309 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3310 +       br->br_id = au_new_br_id(sb);
3311 +       AuDebugOn(br->br_id < 0);
3312 +
3313 +       /* always, regardless the given option */
3314 +       err = au_dr_br_init(sb, br, &add->path);
3315 +       if (unlikely(err))
3316 +               goto out_err;
3317 +
3318 +       if (au_br_writable(add->perm)) {
3319 +               err = au_wbr_init(br, sb, add->perm);
3320 +               if (unlikely(err))
3321 +                       goto out_err;
3322 +       }
3323 +
3324 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3325 +               brbase = au_sbr(sb, 0);
3326 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3327 +               AuDebugOn(!xf);
3328 +               h_inode = d_inode(add->path.dentry);
3329 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3330 +               if (unlikely(err)) {
3331 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3332 +                       goto out_err;
3333 +               }
3334 +       }
3335 +
3336 +       sysaufs_br_init(br);
3337 +       path_get(&br->br_path);
3338 +       goto out; /* success */
3339 +
3340 +out_err:
3341 +       memset(&br->br_path, 0, sizeof(br->br_path));
3342 +out:
3343 +       return err;
3344 +}
3345 +
3346 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3347 +                            struct au_branch *br, aufs_bindex_t bbot,
3348 +                            aufs_bindex_t amount)
3349 +{
3350 +       struct au_branch **brp;
3351 +
3352 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3353 +
3354 +       brp = sbinfo->si_branch + bindex;
3355 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3356 +       *brp = br;
3357 +       sbinfo->si_bbot++;
3358 +       if (unlikely(bbot < 0))
3359 +               sbinfo->si_bbot = 0;
3360 +}
3361 +
3362 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3363 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3364 +{
3365 +       struct au_hdentry *hdp;
3366 +
3367 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3368 +
3369 +       hdp = au_hdentry(dinfo, bindex);
3370 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3371 +       au_h_dentry_init(hdp);
3372 +       dinfo->di_bbot++;
3373 +       if (unlikely(bbot < 0))
3374 +               dinfo->di_btop = 0;
3375 +}
3376 +
3377 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3378 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3379 +{
3380 +       struct au_hinode *hip;
3381 +
3382 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3383 +
3384 +       hip = au_hinode(iinfo, bindex);
3385 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3386 +       au_hinode_init(hip);
3387 +       iinfo->ii_bbot++;
3388 +       if (unlikely(bbot < 0))
3389 +               iinfo->ii_btop = 0;
3390 +}
3391 +
3392 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3393 +                        aufs_bindex_t bindex)
3394 +{
3395 +       struct dentry *root, *h_dentry;
3396 +       struct inode *root_inode, *h_inode;
3397 +       aufs_bindex_t bbot, amount;
3398 +
3399 +       root = sb->s_root;
3400 +       root_inode = d_inode(root);
3401 +       bbot = au_sbbot(sb);
3402 +       amount = bbot + 1 - bindex;
3403 +       h_dentry = au_br_dentry(br);
3404 +       au_sbilist_lock();
3405 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3406 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3407 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3408 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3409 +       h_inode = d_inode(h_dentry);
3410 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3411 +       au_sbilist_unlock();
3412 +}
3413 +
3414 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3415 +{
3416 +       int err;
3417 +       aufs_bindex_t bbot, add_bindex;
3418 +       struct dentry *root, *h_dentry;
3419 +       struct inode *root_inode;
3420 +       struct au_branch *add_branch;
3421 +
3422 +       root = sb->s_root;
3423 +       root_inode = d_inode(root);
3424 +       IMustLock(root_inode);
3425 +       IiMustWriteLock(root_inode);
3426 +       err = test_add(sb, add, remount);
3427 +       if (unlikely(err < 0))
3428 +               goto out;
3429 +       if (err) {
3430 +               err = 0;
3431 +               goto out; /* success */
3432 +       }
3433 +
3434 +       bbot = au_sbbot(sb);
3435 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3436 +       err = PTR_ERR(add_branch);
3437 +       if (IS_ERR(add_branch))
3438 +               goto out;
3439 +
3440 +       err = au_br_init(add_branch, sb, add);
3441 +       if (unlikely(err)) {
3442 +               au_br_do_free(add_branch);
3443 +               goto out;
3444 +       }
3445 +
3446 +       add_bindex = add->bindex;
3447 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3448 +       au_br_do_add(sb, add_branch, add_bindex);
3449 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3450 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3451 +
3452 +       h_dentry = add->path.dentry;
3453 +       if (!add_bindex) {
3454 +               au_cpup_attr_all(root_inode, /*force*/1);
3455 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3456 +       } else
3457 +               au_add_nlink(root_inode, d_inode(h_dentry));
3458 +
3459 +out:
3460 +       return err;
3461 +}
3462 +
3463 +/* ---------------------------------------------------------------------- */
3464 +
3465 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3466 +                                      unsigned long long max __maybe_unused,
3467 +                                      void *arg)
3468 +{
3469 +       unsigned long long n;
3470 +       struct file **p, *f;
3471 +       struct hlist_bl_head *files;
3472 +       struct hlist_bl_node *pos;
3473 +       struct au_finfo *finfo;
3474 +
3475 +       n = 0;
3476 +       p = a;
3477 +       files = &au_sbi(sb)->si_files;
3478 +       hlist_bl_lock(files);
3479 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3480 +               f = finfo->fi_file;
3481 +               if (file_count(f)
3482 +                   && !special_file(file_inode(f)->i_mode)) {
3483 +                       get_file(f);
3484 +                       *p++ = f;
3485 +                       n++;
3486 +                       AuDebugOn(n > max);
3487 +               }
3488 +       }
3489 +       hlist_bl_unlock(files);
3490 +
3491 +       return n;
3492 +}
3493 +
3494 +static struct file **au_farray_alloc(struct super_block *sb,
3495 +                                    unsigned long long *max)
3496 +{
3497 +       struct au_sbinfo *sbi;
3498 +
3499 +       sbi = au_sbi(sb);
3500 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3501 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3502 +}
3503 +
3504 +static void au_farray_free(struct file **a, unsigned long long max)
3505 +{
3506 +       unsigned long long ull;
3507 +
3508 +       for (ull = 0; ull < max; ull++)
3509 +               if (a[ull])
3510 +                       fput(a[ull]);
3511 +       kvfree(a);
3512 +}
3513 +
3514 +/* ---------------------------------------------------------------------- */
3515 +
3516 +/*
3517 + * delete a branch
3518 + */
3519 +
3520 +/* to show the line number, do not make it inlined function */
3521 +#define AuVerbose(do_info, fmt, ...) do { \
3522 +       if (do_info) \
3523 +               pr_info(fmt, ##__VA_ARGS__); \
3524 +} while (0)
3525 +
3526 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3527 +                        aufs_bindex_t bbot)
3528 +{
3529 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3530 +}
3531 +
3532 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3533 +                        aufs_bindex_t bbot)
3534 +{
3535 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3536 +}
3537 +
3538 +/*
3539 + * test if the branch is deletable or not.
3540 + */
3541 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3542 +                           unsigned int sigen, const unsigned int verbose)
3543 +{
3544 +       int err, i, j, ndentry;
3545 +       aufs_bindex_t btop, bbot;
3546 +       struct au_dcsub_pages dpages;
3547 +       struct au_dpage *dpage;
3548 +       struct dentry *d;
3549 +
3550 +       err = au_dpages_init(&dpages, GFP_NOFS);
3551 +       if (unlikely(err))
3552 +               goto out;
3553 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3554 +       if (unlikely(err))
3555 +               goto out_dpages;
3556 +
3557 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3558 +               dpage = dpages.dpages + i;
3559 +               ndentry = dpage->ndentry;
3560 +               for (j = 0; !err && j < ndentry; j++) {
3561 +                       d = dpage->dentries[j];
3562 +                       AuDebugOn(au_dcount(d) <= 0);
3563 +                       if (!au_digen_test(d, sigen)) {
3564 +                               di_read_lock_child(d, AuLock_IR);
3565 +                               if (unlikely(au_dbrange_test(d))) {
3566 +                                       di_read_unlock(d, AuLock_IR);
3567 +                                       continue;
3568 +                               }
3569 +                       } else {
3570 +                               di_write_lock_child(d);
3571 +                               if (unlikely(au_dbrange_test(d))) {
3572 +                                       di_write_unlock(d);
3573 +                                       continue;
3574 +                               }
3575 +                               err = au_reval_dpath(d, sigen);
3576 +                               if (!err)
3577 +                                       di_downgrade_lock(d, AuLock_IR);
3578 +                               else {
3579 +                                       di_write_unlock(d);
3580 +                                       break;
3581 +                               }
3582 +                       }
3583 +
3584 +                       /* AuDbgDentry(d); */
3585 +                       btop = au_dbtop(d);
3586 +                       bbot = au_dbbot(d);
3587 +                       if (btop <= bindex
3588 +                           && bindex <= bbot
3589 +                           && au_h_dptr(d, bindex)
3590 +                           && au_test_dbusy(d, btop, bbot)) {
3591 +                               err = -EBUSY;
3592 +                               AuVerbose(verbose, "busy %pd\n", d);
3593 +                               AuDbgDentry(d);
3594 +                       }
3595 +                       di_read_unlock(d, AuLock_IR);
3596 +               }
3597 +       }
3598 +
3599 +out_dpages:
3600 +       au_dpages_free(&dpages);
3601 +out:
3602 +       return err;
3603 +}
3604 +
3605 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3606 +                          unsigned int sigen, const unsigned int verbose)
3607 +{
3608 +       int err;
3609 +       unsigned long long max, ull;
3610 +       struct inode *i, **array;
3611 +       aufs_bindex_t btop, bbot;
3612 +
3613 +       array = au_iarray_alloc(sb, &max);
3614 +       err = PTR_ERR(array);
3615 +       if (IS_ERR(array))
3616 +               goto out;
3617 +
3618 +       err = 0;
3619 +       AuDbg("b%d\n", bindex);
3620 +       for (ull = 0; !err && ull < max; ull++) {
3621 +               i = array[ull];
3622 +               if (unlikely(!i))
3623 +                       break;
3624 +               if (i->i_ino == AUFS_ROOT_INO)
3625 +                       continue;
3626 +
3627 +               /* AuDbgInode(i); */
3628 +               if (au_iigen(i, NULL) == sigen)
3629 +                       ii_read_lock_child(i);
3630 +               else {
3631 +                       ii_write_lock_child(i);
3632 +                       err = au_refresh_hinode_self(i);
3633 +                       au_iigen_dec(i);
3634 +                       if (!err)
3635 +                               ii_downgrade_lock(i);
3636 +                       else {
3637 +                               ii_write_unlock(i);
3638 +                               break;
3639 +                       }
3640 +               }
3641 +
3642 +               btop = au_ibtop(i);
3643 +               bbot = au_ibbot(i);
3644 +               if (btop <= bindex
3645 +                   && bindex <= bbot
3646 +                   && au_h_iptr(i, bindex)
3647 +                   && au_test_ibusy(i, btop, bbot)) {
3648 +                       err = -EBUSY;
3649 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3650 +                       AuDbgInode(i);
3651 +               }
3652 +               ii_read_unlock(i);
3653 +       }
3654 +       au_iarray_free(array, max);
3655 +
3656 +out:
3657 +       return err;
3658 +}
3659 +
3660 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3661 +                             const unsigned int verbose)
3662 +{
3663 +       int err;
3664 +       unsigned int sigen;
3665 +
3666 +       sigen = au_sigen(root->d_sb);
3667 +       DiMustNoWaiters(root);
3668 +       IiMustNoWaiters(d_inode(root));
3669 +       di_write_unlock(root);
3670 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3671 +       if (!err)
3672 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3673 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3674 +
3675 +       return err;
3676 +}
3677 +
3678 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3679 +                        struct file **to_free, int *idx)
3680 +{
3681 +       int err;
3682 +       unsigned char matched, root;
3683 +       aufs_bindex_t bindex, bbot;
3684 +       struct au_fidir *fidir;
3685 +       struct au_hfile *hfile;
3686 +
3687 +       err = 0;
3688 +       root = IS_ROOT(file->f_path.dentry);
3689 +       if (root) {
3690 +               get_file(file);
3691 +               to_free[*idx] = file;
3692 +               (*idx)++;
3693 +               goto out;
3694 +       }
3695 +
3696 +       matched = 0;
3697 +       fidir = au_fi(file)->fi_hdir;
3698 +       AuDebugOn(!fidir);
3699 +       bbot = au_fbbot_dir(file);
3700 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3701 +               hfile = fidir->fd_hfile + bindex;
3702 +               if (!hfile->hf_file)
3703 +                       continue;
3704 +
3705 +               if (hfile->hf_br->br_id == br_id) {
3706 +                       matched = 1;
3707 +                       break;
3708 +               }
3709 +       }
3710 +       if (matched)
3711 +               err = -EBUSY;
3712 +
3713 +out:
3714 +       return err;
3715 +}
3716 +
3717 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3718 +                         struct file **to_free, int opened)
3719 +{
3720 +       int err, idx;
3721 +       unsigned long long ull, max;
3722 +       aufs_bindex_t btop;
3723 +       struct file *file, **array;
3724 +       struct dentry *root;
3725 +       struct au_hfile *hfile;
3726 +
3727 +       array = au_farray_alloc(sb, &max);
3728 +       err = PTR_ERR(array);
3729 +       if (IS_ERR(array))
3730 +               goto out;
3731 +
3732 +       err = 0;
3733 +       idx = 0;
3734 +       root = sb->s_root;
3735 +       di_write_unlock(root);
3736 +       for (ull = 0; ull < max; ull++) {
3737 +               file = array[ull];
3738 +               if (unlikely(!file))
3739 +                       break;
3740 +
3741 +               /* AuDbg("%pD\n", file); */
3742 +               fi_read_lock(file);
3743 +               btop = au_fbtop(file);
3744 +               if (!d_is_dir(file->f_path.dentry)) {
3745 +                       hfile = &au_fi(file)->fi_htop;
3746 +                       if (hfile->hf_br->br_id == br_id)
3747 +                               err = -EBUSY;
3748 +               } else
3749 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3750 +               fi_read_unlock(file);
3751 +               if (unlikely(err))
3752 +                       break;
3753 +       }
3754 +       di_write_lock_child(root);
3755 +       au_farray_free(array, max);
3756 +       AuDebugOn(idx > opened);
3757 +
3758 +out:
3759 +       return err;
3760 +}
3761 +
3762 +static void br_del_file(struct file **to_free, unsigned long long opened,
3763 +                       aufs_bindex_t br_id)
3764 +{
3765 +       unsigned long long ull;
3766 +       aufs_bindex_t bindex, btop, bbot, bfound;
3767 +       struct file *file;
3768 +       struct au_fidir *fidir;
3769 +       struct au_hfile *hfile;
3770 +
3771 +       for (ull = 0; ull < opened; ull++) {
3772 +               file = to_free[ull];
3773 +               if (unlikely(!file))
3774 +                       break;
3775 +
3776 +               /* AuDbg("%pD\n", file); */
3777 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3778 +               bfound = -1;
3779 +               fidir = au_fi(file)->fi_hdir;
3780 +               AuDebugOn(!fidir);
3781 +               fi_write_lock(file);
3782 +               btop = au_fbtop(file);
3783 +               bbot = au_fbbot_dir(file);
3784 +               for (bindex = btop; bindex <= bbot; bindex++) {
3785 +                       hfile = fidir->fd_hfile + bindex;
3786 +                       if (!hfile->hf_file)
3787 +                               continue;
3788 +
3789 +                       if (hfile->hf_br->br_id == br_id) {
3790 +                               bfound = bindex;
3791 +                               break;
3792 +                       }
3793 +               }
3794 +               AuDebugOn(bfound < 0);
3795 +               au_set_h_fptr(file, bfound, NULL);
3796 +               if (bfound == btop) {
3797 +                       for (btop++; btop <= bbot; btop++)
3798 +                               if (au_hf_dir(file, btop)) {
3799 +                                       au_set_fbtop(file, btop);
3800 +                                       break;
3801 +                               }
3802 +               }
3803 +               fi_write_unlock(file);
3804 +       }
3805 +}
3806 +
3807 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3808 +                            const aufs_bindex_t bindex,
3809 +                            const aufs_bindex_t bbot)
3810 +{
3811 +       struct au_branch **brp, **p;
3812 +
3813 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3814 +
3815 +       brp = sbinfo->si_branch + bindex;
3816 +       if (bindex < bbot)
3817 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3818 +       sbinfo->si_branch[0 + bbot] = NULL;
3819 +       sbinfo->si_bbot--;
3820 +
3821 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3822 +                       /*may_shrink*/1);
3823 +       if (p)
3824 +               sbinfo->si_branch = p;
3825 +       /* harmless error */
3826 +}
3827 +
3828 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3829 +                            const aufs_bindex_t bbot)
3830 +{
3831 +       struct au_hdentry *hdp, *p;
3832 +
3833 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3834 +
3835 +       hdp = au_hdentry(dinfo, bindex);
3836 +       if (bindex < bbot)
3837 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3838 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3839 +       dinfo->di_bbot--;
3840 +
3841 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3842 +                       /*may_shrink*/1);
3843 +       if (p)
3844 +               dinfo->di_hdentry = p;
3845 +       /* harmless error */
3846 +}
3847 +
3848 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3849 +                            const aufs_bindex_t bbot)
3850 +{
3851 +       struct au_hinode *hip, *p;
3852 +
3853 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3854 +
3855 +       hip = au_hinode(iinfo, bindex);
3856 +       if (bindex < bbot)
3857 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3858 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3859 +       iinfo->ii_bbot--;
3860 +
3861 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3862 +                       /*may_shrink*/1);
3863 +       if (p)
3864 +               iinfo->ii_hinode = p;
3865 +       /* harmless error */
3866 +}
3867 +
3868 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3869 +                        struct au_branch *br)
3870 +{
3871 +       aufs_bindex_t bbot;
3872 +       struct au_sbinfo *sbinfo;
3873 +       struct dentry *root, *h_root;
3874 +       struct inode *inode, *h_inode;
3875 +       struct au_hinode *hinode;
3876 +
3877 +       SiMustWriteLock(sb);
3878 +
3879 +       root = sb->s_root;
3880 +       inode = d_inode(root);
3881 +       sbinfo = au_sbi(sb);
3882 +       bbot = sbinfo->si_bbot;
3883 +
3884 +       h_root = au_h_dptr(root, bindex);
3885 +       hinode = au_hi(inode, bindex);
3886 +       h_inode = au_igrab(hinode->hi_inode);
3887 +       au_hiput(hinode);
3888 +
3889 +       au_sbilist_lock();
3890 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3891 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3892 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3893 +       au_sbilist_unlock();
3894 +
3895 +       /* ignore an error */
3896 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3897 +
3898 +       dput(h_root);
3899 +       iput(h_inode);
3900 +       au_br_do_free(br);
3901 +}
3902 +
3903 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3904 +                                  unsigned long long max, void *arg)
3905 +{
3906 +       return max;
3907 +}
3908 +
3909 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3910 +{
3911 +       int err, rerr, i;
3912 +       unsigned long long opened;
3913 +       unsigned int mnt_flags;
3914 +       aufs_bindex_t bindex, bbot, br_id;
3915 +       unsigned char do_wh, verbose;
3916 +       struct au_branch *br;
3917 +       struct au_wbr *wbr;
3918 +       struct dentry *root;
3919 +       struct file **to_free;
3920 +
3921 +       err = 0;
3922 +       opened = 0;
3923 +       to_free = NULL;
3924 +       root = sb->s_root;
3925 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3926 +       if (bindex < 0) {
3927 +               if (remount)
3928 +                       goto out; /* success */
3929 +               err = -ENOENT;
3930 +               pr_err("%s no such branch\n", del->pathname);
3931 +               goto out;
3932 +       }
3933 +       AuDbg("bindex b%d\n", bindex);
3934 +
3935 +       err = -EBUSY;
3936 +       mnt_flags = au_mntflags(sb);
3937 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3938 +       bbot = au_sbbot(sb);
3939 +       if (unlikely(!bbot)) {
3940 +               AuVerbose(verbose, "no more branches left\n");
3941 +               goto out;
3942 +       }
3943 +
3944 +       br = au_sbr(sb, bindex);
3945 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3946 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3947 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3948 +               goto out;
3949 +       }
3950 +
3951 +       br_id = br->br_id;
3952 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3953 +       if (unlikely(opened)) {
3954 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3955 +               err = PTR_ERR(to_free);
3956 +               if (IS_ERR(to_free))
3957 +                       goto out;
3958 +
3959 +               err = test_file_busy(sb, br_id, to_free, opened);
3960 +               if (unlikely(err)) {
3961 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3962 +                       goto out;
3963 +               }
3964 +       }
3965 +
3966 +       wbr = br->br_wbr;
3967 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3968 +       if (do_wh) {
3969 +               /* instead of WbrWhMustWriteLock(wbr) */
3970 +               SiMustWriteLock(sb);
3971 +               for (i = 0; i < AuBrWh_Last; i++) {
3972 +                       dput(wbr->wbr_wh[i]);
3973 +                       wbr->wbr_wh[i] = NULL;
3974 +               }
3975 +       }
3976 +
3977 +       err = test_children_busy(root, bindex, verbose);
3978 +       if (unlikely(err)) {
3979 +               if (do_wh)
3980 +                       goto out_wh;
3981 +               goto out;
3982 +       }
3983 +
3984 +       err = 0;
3985 +       if (to_free) {
3986 +               /*
3987 +                * now we confirmed the branch is deletable.
3988 +                * let's free the remaining opened dirs on the branch.
3989 +                */
3990 +               di_write_unlock(root);
3991 +               br_del_file(to_free, opened, br_id);
3992 +               di_write_lock_child(root);
3993 +       }
3994 +
3995 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3996 +       dbgaufs_xino_del(br);           /* remove one */
3997 +       au_br_do_del(sb, bindex, br);
3998 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3999 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4000 +
4001 +       if (!bindex) {
4002 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4003 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4004 +       } else
4005 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4006 +       if (au_opt_test(mnt_flags, PLINK))
4007 +               au_plink_half_refresh(sb, br_id);
4008 +
4009 +       goto out; /* success */
4010 +
4011 +out_wh:
4012 +       /* revert */
4013 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4014 +       if (rerr)
4015 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4016 +                       del->pathname, rerr);
4017 +out:
4018 +       if (to_free)
4019 +               au_farray_free(to_free, opened);
4020 +       return err;
4021 +}
4022 +
4023 +/* ---------------------------------------------------------------------- */
4024 +
4025 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4026 +{
4027 +       int err;
4028 +       aufs_bindex_t btop, bbot;
4029 +       struct aufs_ibusy ibusy;
4030 +       struct inode *inode, *h_inode;
4031 +
4032 +       err = -EPERM;
4033 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4034 +               goto out;
4035 +
4036 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4037 +       if (!err)
4038 +               /* VERIFY_WRITE */
4039 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4040 +       if (unlikely(err)) {
4041 +               err = -EFAULT;
4042 +               AuTraceErr(err);
4043 +               goto out;
4044 +       }
4045 +
4046 +       err = -EINVAL;
4047 +       si_read_lock(sb, AuLock_FLUSH);
4048 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4049 +               goto out_unlock;
4050 +
4051 +       err = 0;
4052 +       ibusy.h_ino = 0; /* invalid */
4053 +       inode = ilookup(sb, ibusy.ino);
4054 +       if (!inode
4055 +           || inode->i_ino == AUFS_ROOT_INO
4056 +           || au_is_bad_inode(inode))
4057 +               goto out_unlock;
4058 +
4059 +       ii_read_lock_child(inode);
4060 +       btop = au_ibtop(inode);
4061 +       bbot = au_ibbot(inode);
4062 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4063 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4064 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4065 +                       ibusy.h_ino = h_inode->i_ino;
4066 +       }
4067 +       ii_read_unlock(inode);
4068 +       iput(inode);
4069 +
4070 +out_unlock:
4071 +       si_read_unlock(sb);
4072 +       if (!err) {
4073 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4074 +               if (unlikely(err)) {
4075 +                       err = -EFAULT;
4076 +                       AuTraceErr(err);
4077 +               }
4078 +       }
4079 +out:
4080 +       return err;
4081 +}
4082 +
4083 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4084 +{
4085 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4086 +}
4087 +
4088 +#ifdef CONFIG_COMPAT
4089 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4090 +{
4091 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4092 +}
4093 +#endif
4094 +
4095 +/* ---------------------------------------------------------------------- */
4096 +
4097 +/*
4098 + * change a branch permission
4099 + */
4100 +
4101 +static void au_warn_ima(void)
4102 +{
4103 +#ifdef CONFIG_IMA
4104 +       /* since it doesn't support mark_files_ro() */
4105 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4106 +#endif
4107 +}
4108 +
4109 +static int do_need_sigen_inc(int a, int b)
4110 +{
4111 +       return au_br_whable(a) && !au_br_whable(b);
4112 +}
4113 +
4114 +static int need_sigen_inc(int old, int new)
4115 +{
4116 +       return do_need_sigen_inc(old, new)
4117 +               || do_need_sigen_inc(new, old);
4118 +}
4119 +
4120 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4121 +{
4122 +       int err, do_warn;
4123 +       unsigned int mnt_flags;
4124 +       unsigned long long ull, max;
4125 +       aufs_bindex_t br_id;
4126 +       unsigned char verbose, writer;
4127 +       struct file *file, *hf, **array;
4128 +       struct au_hfile *hfile;
4129 +       struct inode *h_inode;
4130 +
4131 +       mnt_flags = au_mntflags(sb);
4132 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4133 +
4134 +       array = au_farray_alloc(sb, &max);
4135 +       err = PTR_ERR(array);
4136 +       if (IS_ERR(array))
4137 +               goto out;
4138 +
4139 +       do_warn = 0;
4140 +       br_id = au_sbr_id(sb, bindex);
4141 +       for (ull = 0; ull < max; ull++) {
4142 +               file = array[ull];
4143 +               if (unlikely(!file))
4144 +                       break;
4145 +
4146 +               /* AuDbg("%pD\n", file); */
4147 +               fi_read_lock(file);
4148 +               if (unlikely(au_test_mmapped(file))) {
4149 +                       err = -EBUSY;
4150 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4151 +                       AuDbgFile(file);
4152 +                       FiMustNoWaiters(file);
4153 +                       fi_read_unlock(file);
4154 +                       goto out_array;
4155 +               }
4156 +
4157 +               hfile = &au_fi(file)->fi_htop;
4158 +               hf = hfile->hf_file;
4159 +               if (!d_is_reg(file->f_path.dentry)
4160 +                   || !(file->f_mode & FMODE_WRITE)
4161 +                   || hfile->hf_br->br_id != br_id
4162 +                   || !(hf->f_mode & FMODE_WRITE))
4163 +                       array[ull] = NULL;
4164 +               else {
4165 +                       do_warn = 1;
4166 +                       get_file(file);
4167 +               }
4168 +
4169 +               FiMustNoWaiters(file);
4170 +               fi_read_unlock(file);
4171 +               fput(file);
4172 +       }
4173 +
4174 +       err = 0;
4175 +       if (do_warn)
4176 +               au_warn_ima();
4177 +
4178 +       for (ull = 0; ull < max; ull++) {
4179 +               file = array[ull];
4180 +               if (!file)
4181 +                       continue;
4182 +
4183 +               /* todo: already flushed? */
4184 +               /*
4185 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4186 +                * approach which resets f_mode and calls mnt_drop_write() and
4187 +                * file_release_write() for each file, because the branch
4188 +                * attribute in aufs world is totally different from the native
4189 +                * fs rw/ro mode.
4190 +                */
4191 +               /* fi_read_lock(file); */
4192 +               hfile = &au_fi(file)->fi_htop;
4193 +               hf = hfile->hf_file;
4194 +               /* fi_read_unlock(file); */
4195 +               spin_lock(&hf->f_lock);
4196 +               writer = !!(hf->f_mode & FMODE_WRITER);
4197 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4198 +               spin_unlock(&hf->f_lock);
4199 +               if (writer) {
4200 +                       h_inode = file_inode(hf);
4201 +                       if (hf->f_mode & FMODE_READ)
4202 +                               i_readcount_inc(h_inode);
4203 +                       put_write_access(h_inode);
4204 +                       __mnt_drop_write(hf->f_path.mnt);
4205 +               }
4206 +       }
4207 +
4208 +out_array:
4209 +       au_farray_free(array, max);
4210 +out:
4211 +       AuTraceErr(err);
4212 +       return err;
4213 +}
4214 +
4215 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4216 +             int *do_refresh)
4217 +{
4218 +       int err, rerr;
4219 +       aufs_bindex_t bindex;
4220 +       struct dentry *root;
4221 +       struct au_branch *br;
4222 +       struct au_br_fhsm *bf;
4223 +
4224 +       root = sb->s_root;
4225 +       bindex = au_find_dbindex(root, mod->h_root);
4226 +       if (bindex < 0) {
4227 +               if (remount)
4228 +                       return 0; /* success */
4229 +               err = -ENOENT;
4230 +               pr_err("%s no such branch\n", mod->path);
4231 +               goto out;
4232 +       }
4233 +       AuDbg("bindex b%d\n", bindex);
4234 +
4235 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4236 +       if (unlikely(err))
4237 +               goto out;
4238 +
4239 +       br = au_sbr(sb, bindex);
4240 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4241 +       if (br->br_perm == mod->perm)
4242 +               return 0; /* success */
4243 +
4244 +       /* pre-allocate for non-fhsm --> fhsm */
4245 +       bf = NULL;
4246 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4247 +               err = au_fhsm_br_alloc(br);
4248 +               if (unlikely(err))
4249 +                       goto out;
4250 +               bf = br->br_fhsm;
4251 +               br->br_fhsm = NULL;
4252 +       }
4253 +
4254 +       if (au_br_writable(br->br_perm)) {
4255 +               /* remove whiteout base */
4256 +               err = au_br_init_wh(sb, br, mod->perm);
4257 +               if (unlikely(err))
4258 +                       goto out_bf;
4259 +
4260 +               if (!au_br_writable(mod->perm)) {
4261 +                       /* rw --> ro, file might be mmapped */
4262 +                       DiMustNoWaiters(root);
4263 +                       IiMustNoWaiters(d_inode(root));
4264 +                       di_write_unlock(root);
4265 +                       err = au_br_mod_files_ro(sb, bindex);
4266 +                       /* aufs_write_lock() calls ..._child() */
4267 +                       di_write_lock_child(root);
4268 +
4269 +                       if (unlikely(err)) {
4270 +                               rerr = -ENOMEM;
4271 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4272 +                                                    GFP_NOFS);
4273 +                               if (br->br_wbr)
4274 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4275 +                               if (unlikely(rerr)) {
4276 +                                       AuIOErr("nested error %d (%d)\n",
4277 +                                               rerr, err);
4278 +                                       br->br_perm = mod->perm;
4279 +                               }
4280 +                       }
4281 +               }
4282 +       } else if (au_br_writable(mod->perm)) {
4283 +               /* ro --> rw */
4284 +               err = -ENOMEM;
4285 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4286 +               if (br->br_wbr) {
4287 +                       err = au_wbr_init(br, sb, mod->perm);
4288 +                       if (unlikely(err)) {
4289 +                               au_kfree_rcu(br->br_wbr);
4290 +                               br->br_wbr = NULL;
4291 +                       }
4292 +               }
4293 +       }
4294 +       if (unlikely(err))
4295 +               goto out_bf;
4296 +
4297 +       if (au_br_fhsm(br->br_perm)) {
4298 +               if (!au_br_fhsm(mod->perm)) {
4299 +                       /* fhsm --> non-fhsm */
4300 +                       au_br_fhsm_fin(br->br_fhsm);
4301 +                       au_kfree_rcu(br->br_fhsm);
4302 +                       br->br_fhsm = NULL;
4303 +               }
4304 +       } else if (au_br_fhsm(mod->perm))
4305 +               /* non-fhsm --> fhsm */
4306 +               br->br_fhsm = bf;
4307 +
4308 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4309 +       br->br_perm = mod->perm;
4310 +       goto out; /* success */
4311 +
4312 +out_bf:
4313 +       au_kfree_try_rcu(bf);
4314 +out:
4315 +       AuTraceErr(err);
4316 +       return err;
4317 +}
4318 +
4319 +/* ---------------------------------------------------------------------- */
4320 +
4321 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4322 +{
4323 +       int err;
4324 +       struct kstatfs kstfs;
4325 +
4326 +       err = vfs_statfs(&br->br_path, &kstfs);
4327 +       if (!err) {
4328 +               stfs->f_blocks = kstfs.f_blocks;
4329 +               stfs->f_bavail = kstfs.f_bavail;
4330 +               stfs->f_files = kstfs.f_files;
4331 +               stfs->f_ffree = kstfs.f_ffree;
4332 +       }
4333 +
4334 +       return err;
4335 +}
4336 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4337 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4338 +++ linux/fs/aufs/branch.h      2022-11-05 23:02:18.959222617 +0100
4339 @@ -0,0 +1,375 @@
4340 +/* SPDX-License-Identifier: GPL-2.0 */
4341 +/*
4342 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4343 + *
4344 + * This program is free software; you can redistribute it and/or modify
4345 + * it under the terms of the GNU General Public License as published by
4346 + * the Free Software Foundation; either version 2 of the License, or
4347 + * (at your option) any later version.
4348 + *
4349 + * This program is distributed in the hope that it will be useful,
4350 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4351 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4352 + * GNU General Public License for more details.
4353 + *
4354 + * You should have received a copy of the GNU General Public License
4355 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4356 + */
4357 +
4358 +/*
4359 + * branch filesystems and xino for them
4360 + */
4361 +
4362 +#ifndef __AUFS_BRANCH_H__
4363 +#define __AUFS_BRANCH_H__
4364 +
4365 +#ifdef __KERNEL__
4366 +
4367 +#include <linux/mount.h>
4368 +#include "dirren.h"
4369 +#include "dynop.h"
4370 +#include "lcnt.h"
4371 +#include "rwsem.h"
4372 +#include "super.h"
4373 +
4374 +/* ---------------------------------------------------------------------- */
4375 +
4376 +/* a xino file */
4377 +struct au_xino {
4378 +       struct file             **xi_file;
4379 +       unsigned int            xi_nfile;
4380 +
4381 +       struct {
4382 +               spinlock_t              spin;
4383 +               ino_t                   *array;
4384 +               int                     total;
4385 +               /* reserved for future use */
4386 +               /* unsigned long        *bitmap; */
4387 +               wait_queue_head_t       wqh;
4388 +       } xi_nondir;
4389 +
4390 +       struct mutex            xi_mtx; /* protects xi_file array */
4391 +       struct hlist_bl_head    xi_writing;
4392 +
4393 +       atomic_t                xi_truncating;
4394 +
4395 +       struct kref             xi_kref;
4396 +};
4397 +
4398 +/* File-based Hierarchical Storage Management */
4399 +struct au_br_fhsm {
4400 +#ifdef CONFIG_AUFS_FHSM
4401 +       struct mutex            bf_lock;
4402 +       unsigned long           bf_jiffy;
4403 +       struct aufs_stfs        bf_stfs;
4404 +       int                     bf_readable;
4405 +#endif
4406 +};
4407 +
4408 +/* members for writable branch only */
4409 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4410 +struct au_wbr {
4411 +       struct au_rwsem         wbr_wh_rwsem;
4412 +       struct dentry           *wbr_wh[AuBrWh_Last];
4413 +       atomic_t                wbr_wh_running;
4414 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4415 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4416 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4417 +
4418 +       /* mfs mode */
4419 +       unsigned long long      wbr_bytes;
4420 +};
4421 +
4422 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4423 +#define AuBrDynOp (AuDyLast * 4)
4424 +
4425 +#ifdef CONFIG_AUFS_HFSNOTIFY
4426 +/* support for asynchronous destruction */
4427 +struct au_br_hfsnotify {
4428 +       struct fsnotify_group   *hfsn_group;
4429 +};
4430 +#endif
4431 +
4432 +/* sysfs entries */
4433 +struct au_brsysfs {
4434 +       char                    name[16];
4435 +       struct attribute        attr;
4436 +};
4437 +
4438 +enum {
4439 +       AuBrSysfs_BR,
4440 +       AuBrSysfs_BRID,
4441 +       AuBrSysfs_Last
4442 +};
4443 +
4444 +/* protected by superblock rwsem */
4445 +struct au_branch {
4446 +       struct au_xino          *br_xino;
4447 +
4448 +       aufs_bindex_t           br_id;
4449 +
4450 +       int                     br_perm;
4451 +       struct path             br_path;
4452 +       spinlock_t              br_dykey_lock;
4453 +       struct au_dykey         *br_dykey[AuBrDynOp];
4454 +       au_lcnt_t               br_nfiles;      /* opened files */
4455 +       au_lcnt_t               br_count;       /* in-use for other */
4456 +
4457 +       struct au_wbr           *br_wbr;
4458 +       struct au_br_fhsm       *br_fhsm;
4459 +
4460 +#ifdef CONFIG_AUFS_HFSNOTIFY
4461 +       struct au_br_hfsnotify  *br_hfsn;
4462 +#endif
4463 +
4464 +#ifdef CONFIG_SYSFS
4465 +       /* entries under sysfs per mount-point */
4466 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4467 +#endif
4468 +
4469 +#ifdef CONFIG_DEBUG_FS
4470 +       struct dentry            *br_dbgaufs; /* xino */
4471 +#endif
4472 +
4473 +       struct au_dr_br         br_dirren;
4474 +};
4475 +
4476 +/* ---------------------------------------------------------------------- */
4477 +
4478 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4479 +{
4480 +       return br->br_path.mnt;
4481 +}
4482 +
4483 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4484 +{
4485 +       return br->br_path.dentry;
4486 +}
4487 +
4488 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4489 +{
4490 +       return mnt_user_ns(br->br_path.mnt);
4491 +}
4492 +
4493 +static inline struct super_block *au_br_sb(struct au_branch *br)
4494 +{
4495 +       return au_br_mnt(br)->mnt_sb;
4496 +}
4497 +
4498 +static inline int au_br_rdonly(struct au_branch *br)
4499 +{
4500 +       return (sb_rdonly(au_br_sb(br))
4501 +               || !au_br_writable(br->br_perm))
4502 +               ? -EROFS : 0;
4503 +}
4504 +
4505 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4506 +{
4507 +#ifdef CONFIG_AUFS_HNOTIFY
4508 +       return !(brperm & AuBrPerm_RR);
4509 +#else
4510 +       return 0;
4511 +#endif
4512 +}
4513 +
4514 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4515 +{
4516 +       int err, exec_flag;
4517 +
4518 +       err = 0;
4519 +       exec_flag = oflag & __FMODE_EXEC;
4520 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4521 +               err = -EACCES;
4522 +
4523 +       return err;
4524 +}
4525 +
4526 +static inline void au_xino_get(struct au_branch *br)
4527 +{
4528 +       struct au_xino *xi;
4529 +
4530 +       xi = br->br_xino;
4531 +       if (xi)
4532 +               kref_get(&xi->xi_kref);
4533 +}
4534 +
4535 +static inline int au_xino_count(struct au_branch *br)
4536 +{
4537 +       int v;
4538 +       struct au_xino *xi;
4539 +
4540 +       v = 0;
4541 +       xi = br->br_xino;
4542 +       if (xi)
4543 +               v = kref_read(&xi->xi_kref);
4544 +
4545 +       return v;
4546 +}
4547 +
4548 +/* ---------------------------------------------------------------------- */
4549 +
4550 +/* branch.c */
4551 +struct au_sbinfo;
4552 +void au_br_free(struct au_sbinfo *sinfo);
4553 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4554 +struct au_opt_add;
4555 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4556 +struct au_opt_del;
4557 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4558 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4559 +#ifdef CONFIG_COMPAT
4560 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4561 +#endif
4562 +struct au_opt_mod;
4563 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4564 +             int *do_refresh);
4565 +struct aufs_stfs;
4566 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4567 +
4568 +/* xino.c */
4569 +static const loff_t au_loff_max = LLONG_MAX;
4570 +
4571 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4572 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4573 +                           int wbrtop);
4574 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4575 +                            struct file *copy_src);
4576 +struct au_xi_new {
4577 +       struct au_xino *xi;     /* switch between xino and xigen */
4578 +       int idx;
4579 +       struct path *base;
4580 +       struct file *copy_src;
4581 +};
4582 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4583 +
4584 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4585 +                ino_t *ino);
4586 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4587 +                 ino_t ino);
4588 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4589 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4590 +
4591 +int au_xib_trunc(struct super_block *sb);
4592 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4593 +
4594 +struct au_xino *au_xino_alloc(unsigned int nfile);
4595 +int au_xino_put(struct au_branch *br);
4596 +struct file *au_xino_file1(struct au_xino *xi);
4597 +
4598 +struct au_opt_xino;
4599 +void au_xino_clr(struct super_block *sb);
4600 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4601 +struct file *au_xino_def(struct super_block *sb);
4602 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4603 +                   struct path *base);
4604 +
4605 +ino_t au_xino_new_ino(struct super_block *sb);
4606 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4607 +
4608 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4609 +                      ino_t h_ino, int idx);
4610 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4611 +                     int *idx);
4612 +
4613 +int au_xino_path(struct seq_file *seq, struct file *file);
4614 +
4615 +/* ---------------------------------------------------------------------- */
4616 +
4617 +/* @idx is signed to accept -1 meaning the first file */
4618 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4619 +{
4620 +       struct file *file;
4621 +
4622 +       file = NULL;
4623 +       if (!xi)
4624 +               goto out;
4625 +
4626 +       if (idx >= 0) {
4627 +               if (idx < xi->xi_nfile)
4628 +                       file = xi->xi_file[idx];
4629 +       } else
4630 +               file = au_xino_file1(xi);
4631 +
4632 +out:
4633 +       return file;
4634 +}
4635 +
4636 +/* ---------------------------------------------------------------------- */
4637 +
4638 +/* Superblock to branch */
4639 +static inline
4640 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4641 +{
4642 +       return au_sbr(sb, bindex)->br_id;
4643 +}
4644 +
4645 +static inline
4646 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4647 +{
4648 +       return au_br_mnt(au_sbr(sb, bindex));
4649 +}
4650 +
4651 +static inline
4652 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4653 +{
4654 +       return au_br_userns(au_sbr(sb, bindex));
4655 +}
4656 +
4657 +static inline
4658 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4659 +{
4660 +       return au_br_sb(au_sbr(sb, bindex));
4661 +}
4662 +
4663 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4664 +{
4665 +       return au_sbr(sb, bindex)->br_perm;
4666 +}
4667 +
4668 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4669 +{
4670 +       return au_br_whable(au_sbr_perm(sb, bindex));
4671 +}
4672 +
4673 +/* ---------------------------------------------------------------------- */
4674 +
4675 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4676 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4677 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4678 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4679 +/*
4680 +#define wbr_wh_read_trylock_nested(wbr) \
4681 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4682 +#define wbr_wh_write_trylock_nested(wbr) \
4683 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4684 +*/
4685 +
4686 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4687 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4688 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4689 +
4690 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4691 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4692 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4693 +
4694 +/* ---------------------------------------------------------------------- */
4695 +
4696 +#ifdef CONFIG_AUFS_FHSM
4697 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4698 +{
4699 +       mutex_init(&brfhsm->bf_lock);
4700 +       brfhsm->bf_jiffy = 0;
4701 +       brfhsm->bf_readable = 0;
4702 +}
4703 +
4704 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4705 +{
4706 +       mutex_destroy(&brfhsm->bf_lock);
4707 +}
4708 +#else
4709 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4710 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4711 +#endif
4712 +
4713 +#endif /* __KERNEL__ */
4714 +#endif /* __AUFS_BRANCH_H__ */
4715 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4716 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4717 +++ linux/fs/aufs/conf.mk       2022-11-05 23:02:18.959222617 +0100
4718 @@ -0,0 +1,40 @@
4719 +# SPDX-License-Identifier: GPL-2.0
4720 +
4721 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4722 +
4723 +define AuConf
4724 +ifdef ${1}
4725 +AuConfStr += ${1}=${${1}}
4726 +endif
4727 +endef
4728 +
4729 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4730 +       SBILIST \
4731 +       HNOTIFY HFSNOTIFY \
4732 +       EXPORT INO_T_64 \
4733 +       XATTR \
4734 +       FHSM \
4735 +       RDU \
4736 +       DIRREN \
4737 +       SHWH \
4738 +       BR_RAMFS \
4739 +       BR_FUSE POLL \
4740 +       BR_HFSPLUS \
4741 +       BDEV_LOOP \
4742 +       DEBUG MAGIC_SYSRQ
4743 +$(foreach i, ${AuConfAll}, \
4744 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4745 +
4746 +AuConfName = ${obj}/conf.str
4747 +${AuConfName}.tmp: FORCE
4748 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4749 +${AuConfName}: ${AuConfName}.tmp
4750 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4751 +       echo '  GEN    ' $@; \
4752 +       cp -p $< $@; \
4753 +       }
4754 +FORCE:
4755 +clean-files += ${AuConfName} ${AuConfName}.tmp
4756 +${obj}/sysfs.o: ${AuConfName}
4757 +
4758 +-include ${srctree}/${src}/conf_priv.mk
4759 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4760 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4761 +++ linux/fs/aufs/cpup.c        2022-11-05 23:02:18.962555950 +0100
4762 @@ -0,0 +1,1459 @@
4763 +// SPDX-License-Identifier: GPL-2.0
4764 +/*
4765 + * Copyright (C) 2005-2022 Junjiro R. Okajima
4766 + *
4767 + * This program is free software; you can redistribute it and/or modify
4768 + * it under the terms of the GNU General Public License as published by
4769 + * the Free Software Foundation; either version 2 of the License, or
4770 + * (at your option) any later version.
4771 + *
4772 + * This program is distributed in the hope that it will be useful,
4773 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4774 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4775 + * GNU General Public License for more details.
4776 + *
4777 + * You should have received a copy of the GNU General Public License
4778 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4779 + */
4780 +
4781 +/*
4782 + * copy-up functions, see wbr_policy.c for copy-down
4783 + */
4784 +
4785 +#include <linux/fs_stack.h>
4786 +#include <linux/mm.h>
4787 +#include <linux/task_work.h>
4788 +#include "aufs.h"
4789 +
4790 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4791 +{
4792 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4793 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4794 +
4795 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4796 +
4797 +       dst->i_flags |= iflags & ~mask;
4798 +       if (au_test_fs_notime(dst->i_sb))
4799 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4800 +}
4801 +
4802 +void au_cpup_attr_timesizes(struct inode *inode)
4803 +{
4804 +       struct inode *h_inode;
4805 +
4806 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4807 +       fsstack_copy_attr_times(inode, h_inode);
4808 +       fsstack_copy_inode_size(inode, h_inode);
4809 +}
4810 +
4811 +void au_cpup_attr_nlink(struct inode *inode, int force)
4812 +{
4813 +       struct inode *h_inode;
4814 +       struct super_block *sb;
4815 +       aufs_bindex_t bindex, bbot;
4816 +
4817 +       sb = inode->i_sb;
4818 +       bindex = au_ibtop(inode);
4819 +       h_inode = au_h_iptr(inode, bindex);
4820 +       if (!force
4821 +           && !S_ISDIR(h_inode->i_mode)
4822 +           && au_opt_test(au_mntflags(sb), PLINK)
4823 +           && au_plink_test(inode))
4824 +               return;
4825 +
4826 +       /*
4827 +        * 0 can happen in revalidating.
4828 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4829 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4830 +        * case.
4831 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4832 +        *       the incorrect link count.
4833 +        */
4834 +       set_nlink(inode, h_inode->i_nlink);
4835 +
4836 +       /*
4837 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4838 +        * it may includes whplink directory.
4839 +        */
4840 +       if (S_ISDIR(h_inode->i_mode)) {
4841 +               bbot = au_ibbot(inode);
4842 +               for (bindex++; bindex <= bbot; bindex++) {
4843 +                       h_inode = au_h_iptr(inode, bindex);
4844 +                       if (h_inode)
4845 +                               au_add_nlink(inode, h_inode);
4846 +               }
4847 +       }
4848 +}
4849 +
4850 +void au_cpup_attr_changeable(struct inode *inode)
4851 +{
4852 +       struct inode *h_inode;
4853 +
4854 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4855 +       inode->i_mode = h_inode->i_mode;
4856 +       inode->i_uid = h_inode->i_uid;
4857 +       inode->i_gid = h_inode->i_gid;
4858 +       au_cpup_attr_timesizes(inode);
4859 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4860 +}
4861 +
4862 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4863 +{
4864 +       struct au_iinfo *iinfo = au_ii(inode);
4865 +
4866 +       IiMustWriteLock(inode);
4867 +
4868 +       iinfo->ii_higen = h_inode->i_generation;
4869 +       iinfo->ii_hsb1 = h_inode->i_sb;
4870 +}
4871 +
4872 +void au_cpup_attr_all(struct inode *inode, int force)
4873 +{
4874 +       struct inode *h_inode;
4875 +
4876 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4877 +       au_cpup_attr_changeable(inode);
4878 +       if (inode->i_nlink > 0)
4879 +               au_cpup_attr_nlink(inode, force);
4880 +       inode->i_rdev = h_inode->i_rdev;
4881 +       inode->i_blkbits = h_inode->i_blkbits;
4882 +       au_cpup_igen(inode, h_inode);
4883 +}
4884 +
4885 +/* ---------------------------------------------------------------------- */
4886 +
4887 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4888 +
4889 +/* keep the timestamps of the parent dir when cpup */
4890 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4891 +                   struct path *h_path)
4892 +{
4893 +       struct inode *h_inode;
4894 +
4895 +       dt->dt_dentry = dentry;
4896 +       dt->dt_h_path = *h_path;
4897 +       h_inode = d_inode(h_path->dentry);
4898 +       dt->dt_atime = h_inode->i_atime;
4899 +       dt->dt_mtime = h_inode->i_mtime;
4900 +       /* smp_mb(); */
4901 +}
4902 +
4903 +void au_dtime_revert(struct au_dtime *dt)
4904 +{
4905 +       struct iattr attr;
4906 +       int err;
4907 +
4908 +       attr.ia_atime = dt->dt_atime;
4909 +       attr.ia_mtime = dt->dt_mtime;
4910 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4911 +               | ATTR_ATIME | ATTR_ATIME_SET;
4912 +
4913 +       /* no delegation since this is a directory */
4914 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4915 +       if (unlikely(err))
4916 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4917 +}
4918 +
4919 +/* ---------------------------------------------------------------------- */
4920 +
4921 +/* internal use only */
4922 +struct au_cpup_reg_attr {
4923 +       int             valid;
4924 +       struct kstat    st;
4925 +       unsigned int    iflags; /* inode->i_flags */
4926 +};
4927 +
4928 +static noinline_for_stack
4929 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4930 +              struct au_cpup_reg_attr *h_src_attr)
4931 +{
4932 +       int err, sbits, icex;
4933 +       unsigned int mnt_flags;
4934 +       unsigned char verbose;
4935 +       struct iattr ia;
4936 +       struct path h_path;
4937 +       struct inode *h_isrc, *h_idst;
4938 +       struct kstat *h_st;
4939 +       struct au_branch *br;
4940 +
4941 +       br = au_sbr(dst->d_sb, bindex);
4942 +       h_path.mnt = au_br_mnt(br);
4943 +       h_path.dentry = au_h_dptr(dst, bindex);
4944 +       h_idst = d_inode(h_path.dentry);
4945 +       h_isrc = d_inode(h_src->dentry);
4946 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4947 +               | ATTR_ATIME | ATTR_MTIME
4948 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4949 +       if (h_src_attr && h_src_attr->valid) {
4950 +               h_st = &h_src_attr->st;
4951 +               ia.ia_uid = h_st->uid;
4952 +               ia.ia_gid = h_st->gid;
4953 +               ia.ia_atime = h_st->atime;
4954 +               ia.ia_mtime = h_st->mtime;
4955 +               if (h_idst->i_mode != h_st->mode
4956 +                   && !S_ISLNK(h_idst->i_mode)) {
4957 +                       ia.ia_valid |= ATTR_MODE;
4958 +                       ia.ia_mode = h_st->mode;
4959 +               }
4960 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4961 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4962 +       } else {
4963 +               ia.ia_uid = h_isrc->i_uid;
4964 +               ia.ia_gid = h_isrc->i_gid;
4965 +               ia.ia_atime = h_isrc->i_atime;
4966 +               ia.ia_mtime = h_isrc->i_mtime;
4967 +               if (h_idst->i_mode != h_isrc->i_mode
4968 +                   && !S_ISLNK(h_idst->i_mode)) {
4969 +                       ia.ia_valid |= ATTR_MODE;
4970 +                       ia.ia_mode = h_isrc->i_mode;
4971 +               }
4972 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4973 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4974 +       }
4975 +       /* no delegation since it is just created */
4976 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4977 +
4978 +       /* is this nfs only? */
4979 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4980 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4981 +               ia.ia_mode = h_isrc->i_mode;
4982 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4983 +       }
4984 +
4985 +       icex = br->br_perm & AuBrAttr_ICEX;
4986 +       if (!err) {
4987 +               mnt_flags = au_mntflags(dst->d_sb);
4988 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4989 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4990 +       }
4991 +
4992 +       return err;
4993 +}
4994 +
4995 +/* ---------------------------------------------------------------------- */
4996 +
4997 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4998 +                          char *buf, unsigned long blksize)
4999 +{
5000 +       int err;
5001 +       size_t sz, rbytes, wbytes;
5002 +       unsigned char all_zero;
5003 +       char *p, *zp;
5004 +       struct inode *h_inode;
5005 +       /* reduce stack usage */
5006 +       struct iattr *ia;
5007 +
5008 +       zp = page_address(ZERO_PAGE(0));
5009 +       if (unlikely(!zp))
5010 +               return -ENOMEM; /* possible? */
5011 +
5012 +       err = 0;
5013 +       all_zero = 0;
5014 +       while (len) {
5015 +               AuDbg("len %lld\n", len);
5016 +               sz = blksize;
5017 +               if (len < blksize)
5018 +                       sz = len;
5019 +
5020 +               rbytes = 0;
5021 +               /* todo: signal_pending? */
5022 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5023 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5024 +                       err = rbytes;
5025 +               }
5026 +               if (unlikely(err < 0))
5027 +                       break;
5028 +
5029 +               all_zero = 0;
5030 +               if (len >= rbytes && rbytes == blksize)
5031 +                       all_zero = !memcmp(buf, zp, rbytes);
5032 +               if (!all_zero) {
5033 +                       wbytes = rbytes;
5034 +                       p = buf;
5035 +                       while (wbytes) {
5036 +                               size_t b;
5037 +
5038 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5039 +                               err = b;
5040 +                               /* todo: signal_pending? */
5041 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5042 +                                       continue;
5043 +                               if (unlikely(err < 0))
5044 +                                       break;
5045 +                               wbytes -= b;
5046 +                               p += b;
5047 +                       }
5048 +                       if (unlikely(err < 0))
5049 +                               break;
5050 +               } else {
5051 +                       loff_t res;
5052 +
5053 +                       AuLabel(hole);
5054 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5055 +                       err = res;
5056 +                       if (unlikely(res < 0))
5057 +                               break;
5058 +               }
5059 +               len -= rbytes;
5060 +               err = 0;
5061 +       }
5062 +
5063 +       /* the last block may be a hole */
5064 +       if (!err && all_zero) {
5065 +               AuLabel(last hole);
5066 +
5067 +               err = 1;
5068 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5069 +                       /* nfs requires this step to make last hole */
5070 +                       /* is this only nfs? */
5071 +                       do {
5072 +                               /* todo: signal_pending? */
5073 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5074 +                       } while (err == -EAGAIN || err == -EINTR);
5075 +                       if (err == 1)
5076 +                               dst->f_pos--;
5077 +               }
5078 +
5079 +               if (err == 1) {
5080 +                       ia = (void *)buf;
5081 +                       ia->ia_size = dst->f_pos;
5082 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5083 +                       ia->ia_file = dst;
5084 +                       h_inode = file_inode(dst);
5085 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5086 +                       /* no delegation since it is just created */
5087 +                       err = vfsub_notify_change(&dst->f_path, ia,
5088 +                                                 /*delegated*/NULL);
5089 +                       inode_unlock(h_inode);
5090 +               }
5091 +       }
5092 +
5093 +       return err;
5094 +}
5095 +
5096 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5097 +{
5098 +       int err;
5099 +       unsigned long blksize;
5100 +       unsigned char do_kfree;
5101 +       char *buf;
5102 +       struct super_block *h_sb;
5103 +
5104 +       err = -ENOMEM;
5105 +       h_sb = file_inode(dst)->i_sb;
5106 +       blksize = h_sb->s_blocksize;
5107 +       if (!blksize || PAGE_SIZE < blksize)
5108 +               blksize = PAGE_SIZE;
5109 +       AuDbg("blksize %lu\n", blksize);
5110 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5111 +       if (do_kfree)
5112 +               buf = kmalloc(blksize, GFP_NOFS);
5113 +       else
5114 +               buf = (void *)__get_free_page(GFP_NOFS);
5115 +       if (unlikely(!buf))
5116 +               goto out;
5117 +
5118 +       if (len > (1 << 22))
5119 +               AuDbg("copying a large file %lld\n", (long long)len);
5120 +
5121 +       src->f_pos = 0;
5122 +       dst->f_pos = 0;
5123 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5124 +       if (do_kfree) {
5125 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5126 +               au_kfree_do_rcu(buf);
5127 +       } else
5128 +               free_page((unsigned long)buf);
5129 +
5130 +out:
5131 +       return err;
5132 +}
5133 +
5134 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5135 +{
5136 +       int err;
5137 +       struct super_block *h_src_sb;
5138 +       struct inode *h_src_inode;
5139 +
5140 +       h_src_inode = file_inode(src);
5141 +       h_src_sb = h_src_inode->i_sb;
5142 +
5143 +       /* XFS acquires inode_lock */
5144 +       if (!au_test_xfs(h_src_sb))
5145 +               err = au_copy_file(dst, src, len);
5146 +       else {
5147 +               inode_unlock_shared(h_src_inode);
5148 +               err = au_copy_file(dst, src, len);
5149 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5150 +       }
5151 +
5152 +       return err;
5153 +}
5154 +
5155 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5156 +{
5157 +       int err;
5158 +       loff_t lo;
5159 +       struct super_block *h_src_sb;
5160 +       struct inode *h_src_inode;
5161 +
5162 +       h_src_inode = file_inode(src);
5163 +       h_src_sb = h_src_inode->i_sb;
5164 +       if (h_src_sb != file_inode(dst)->i_sb
5165 +           || !dst->f_op->remap_file_range) {
5166 +               err = au_do_copy(dst, src, len);
5167 +               goto out;
5168 +       }
5169 +
5170 +       if (!au_test_nfs(h_src_sb)) {
5171 +               inode_unlock_shared(h_src_inode);
5172 +               lo = vfsub_clone_file_range(src, dst, len);
5173 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5174 +       } else
5175 +               lo = vfsub_clone_file_range(src, dst, len);
5176 +       if (lo == len) {
5177 +               err = 0;
5178 +               goto out; /* success */
5179 +       } else if (lo >= 0)
5180 +               /* todo: possible? */
5181 +               /* paritially succeeded */
5182 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5183 +       else if (lo != -EOPNOTSUPP) {
5184 +               /* older XFS has a condition in cloning */
5185 +               err = lo;
5186 +               goto out;
5187 +       }
5188 +
5189 +       /* the backend fs on NFS may not support cloning */
5190 +       err = au_do_copy(dst, src, len);
5191 +
5192 +out:
5193 +       AuTraceErr(err);
5194 +       return err;
5195 +}
5196 +
5197 +/*
5198 + * to support a sparse file which is opened with O_APPEND,
5199 + * we need to close the file.
5200 + */
5201 +static int au_cp_regular(struct au_cp_generic *cpg)
5202 +{
5203 +       int err, i;
5204 +       enum { SRC, DST };
5205 +       struct {
5206 +               aufs_bindex_t bindex;
5207 +               unsigned int flags;
5208 +               struct dentry *dentry;
5209 +               int force_wr;
5210 +               struct file *file;
5211 +       } *f, file[] = {
5212 +               {
5213 +                       .bindex = cpg->bsrc,
5214 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5215 +               },
5216 +               {
5217 +                       .bindex = cpg->bdst,
5218 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5219 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5220 +               }
5221 +       };
5222 +       struct au_branch *br;
5223 +       struct super_block *sb, *h_src_sb;
5224 +       struct inode *h_src_inode;
5225 +       struct task_struct *tsk = current;
5226 +
5227 +       /* bsrc branch can be ro/rw. */
5228 +       sb = cpg->dentry->d_sb;
5229 +       f = file;
5230 +       for (i = 0; i < 2; i++, f++) {
5231 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5232 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5233 +                                   /*file*/NULL, f->force_wr);
5234 +               if (IS_ERR(f->file)) {
5235 +                       err = PTR_ERR(f->file);
5236 +                       if (i == SRC)
5237 +                               goto out;
5238 +                       else
5239 +                               goto out_src;
5240 +               }
5241 +       }
5242 +
5243 +       /* try stopping to update while we copyup */
5244 +       h_src_inode = d_inode(file[SRC].dentry);
5245 +       h_src_sb = h_src_inode->i_sb;
5246 +       if (!au_test_nfs(h_src_sb))
5247 +               IMustLock(h_src_inode);
5248 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5249 +
5250 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5251 +       if (tsk->flags & PF_KTHREAD)
5252 +               __fput_sync(file[DST].file);
5253 +       else {
5254 +               /* it happened actually */
5255 +               fput(file[DST].file);
5256 +               /*
5257 +                * too bad.
5258 +                * we have to call both since we don't know which place the file
5259 +                * was added to.
5260 +                */
5261 +               task_work_run();
5262 +               flush_delayed_fput();
5263 +       }
5264 +       br = au_sbr(sb, file[DST].bindex);
5265 +       au_lcnt_dec(&br->br_nfiles);
5266 +
5267 +out_src:
5268 +       fput(file[SRC].file);
5269 +       br = au_sbr(sb, file[SRC].bindex);
5270 +       au_lcnt_dec(&br->br_nfiles);
5271 +out:
5272 +       return err;
5273 +}
5274 +
5275 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5276 +                             struct au_cpup_reg_attr *h_src_attr)
5277 +{
5278 +       int err, rerr;
5279 +       loff_t l;
5280 +       struct path h_path;
5281 +       struct inode *h_src_inode, *h_dst_inode;
5282 +
5283 +       err = 0;
5284 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5285 +       l = i_size_read(h_src_inode);
5286 +       if (cpg->len == -1 || l < cpg->len)
5287 +               cpg->len = l;
5288 +       if (cpg->len) {
5289 +               /* try stopping to update while we are referencing */
5290 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5291 +               au_pin_hdir_unlock(cpg->pin);
5292 +
5293 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5294 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5295 +               h_src_attr->iflags = h_src_inode->i_flags;
5296 +               if (!au_test_nfs(h_src_inode->i_sb))
5297 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5298 +               else {
5299 +                       inode_unlock_shared(h_src_inode);
5300 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5301 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5302 +               }
5303 +               if (unlikely(err)) {
5304 +                       inode_unlock_shared(h_src_inode);
5305 +                       goto out;
5306 +               }
5307 +               h_src_attr->valid = 1;
5308 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5309 +                       err = au_cp_regular(cpg);
5310 +                       inode_unlock_shared(h_src_inode);
5311 +               } else {
5312 +                       inode_unlock_shared(h_src_inode);
5313 +                       err = au_cp_regular(cpg);
5314 +               }
5315 +               rerr = au_pin_hdir_relock(cpg->pin);
5316 +               if (!err && rerr)
5317 +                       err = rerr;
5318 +       }
5319 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5320 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5321 +               h_dst_inode = d_inode(h_path.dentry);
5322 +               spin_lock(&h_dst_inode->i_lock);
5323 +               h_dst_inode->i_state |= I_LINKABLE;
5324 +               spin_unlock(&h_dst_inode->i_lock);
5325 +       }
5326 +
5327 +out:
5328 +       return err;
5329 +}
5330 +
5331 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5332 +                             struct inode *h_dir)
5333 +{
5334 +       int err;
5335 +       DEFINE_DELAYED_CALL(done);
5336 +       const char *sym;
5337 +
5338 +       sym = vfs_get_link(h_src, &done);
5339 +       err = PTR_ERR(sym);
5340 +       if (IS_ERR(sym))
5341 +               goto out;
5342 +
5343 +       err = vfsub_symlink(h_dir, h_path, sym);
5344 +
5345 +out:
5346 +       do_delayed_call(&done);
5347 +       return err;
5348 +}
5349 +
5350 +/*
5351 + * regardless 'acl' option, reset all ACL.
5352 + * All ACL will be copied up later from the original entry on the lower branch.
5353 + */
5354 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5355 +{
5356 +       int err;
5357 +       struct dentry *h_dentry;
5358 +       struct inode *h_inode;
5359 +       struct user_namespace *h_userns;
5360 +
5361 +       h_userns = mnt_user_ns(h_path->mnt);
5362 +       h_dentry = h_path->dentry;
5363 +       h_inode = d_inode(h_dentry);
5364 +       /* forget_all_cached_acls(h_inode)); */
5365 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5366 +       AuTraceErr(err);
5367 +       if (err == -EOPNOTSUPP)
5368 +               err = 0;
5369 +       if (!err)
5370 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5371 +
5372 +       AuTraceErr(err);
5373 +       return err;
5374 +}
5375 +
5376 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5377 +                         struct inode *h_dir, struct path *h_path)
5378 +{
5379 +       int err;
5380 +       struct inode *dir, *inode;
5381 +       struct user_namespace *h_userns;
5382 +
5383 +       h_userns = mnt_user_ns(h_path->mnt);
5384 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5385 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5386 +       AuTraceErr(err);
5387 +       if (err == -EOPNOTSUPP)
5388 +               err = 0;
5389 +       if (unlikely(err))
5390 +               goto out;
5391 +
5392 +       /*
5393 +        * strange behaviour from the users view,
5394 +        * particularly setattr case
5395 +        */
5396 +       dir = d_inode(dst_parent);
5397 +       if (au_ibtop(dir) == cpg->bdst)
5398 +               au_cpup_attr_nlink(dir, /*force*/1);
5399 +       inode = d_inode(cpg->dentry);
5400 +       au_cpup_attr_nlink(inode, /*force*/1);
5401 +
5402 +out:
5403 +       return err;
5404 +}
5405 +
5406 +static noinline_for_stack
5407 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5408 +              struct au_cpup_reg_attr *h_src_attr)
5409 +{
5410 +       int err;
5411 +       umode_t mode;
5412 +       unsigned int mnt_flags;
5413 +       unsigned char isdir, isreg, force;
5414 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5415 +       struct au_dtime dt;
5416 +       struct path h_path;
5417 +       struct dentry *h_src, *h_dst, *h_parent;
5418 +       struct inode *h_inode, *h_dir;
5419 +       struct super_block *sb;
5420 +
5421 +       /* bsrc branch can be ro/rw. */
5422 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5423 +       h_inode = d_inode(h_src);
5424 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5425 +
5426 +       /* try stopping to be referenced while we are creating */
5427 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5428 +       if (au_ftest_cpup(cpg->flags, RENAME))
5429 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5430 +                                 AUFS_WH_PFX_LEN));
5431 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5432 +       h_dir = d_inode(h_parent);
5433 +       IMustLock(h_dir);
5434 +       AuDebugOn(h_parent != h_dst->d_parent);
5435 +
5436 +       sb = cpg->dentry->d_sb;
5437 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5438 +       if (do_dt) {
5439 +               h_path.dentry = h_parent;
5440 +               au_dtime_store(&dt, dst_parent, &h_path);
5441 +       }
5442 +       h_path.dentry = h_dst;
5443 +
5444 +       isreg = 0;
5445 +       isdir = 0;
5446 +       mode = h_inode->i_mode;
5447 +       switch (mode & S_IFMT) {
5448 +       case S_IFREG:
5449 +               isreg = 1;
5450 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5451 +               if (!err)
5452 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5453 +               break;
5454 +       case S_IFDIR:
5455 +               isdir = 1;
5456 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5457 +               if (!err)
5458 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5459 +               break;
5460 +       case S_IFLNK:
5461 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5462 +               break;
5463 +       case S_IFCHR:
5464 +       case S_IFBLK:
5465 +               AuDebugOn(!capable(CAP_MKNOD));
5466 +               fallthrough;
5467 +       case S_IFIFO:
5468 +       case S_IFSOCK:
5469 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5470 +               break;
5471 +       default:
5472 +               AuIOErr("Unknown inode type 0%o\n", mode);
5473 +               err = -EIO;
5474 +       }
5475 +       if (!err)
5476 +               err = au_reset_acl(h_dir, &h_path, mode);
5477 +
5478 +       mnt_flags = au_mntflags(sb);
5479 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5480 +           && !isdir
5481 +           && au_opt_test(mnt_flags, XINO)
5482 +           && (h_inode->i_nlink == 1
5483 +               || (h_inode->i_state & I_LINKABLE))
5484 +           /* todo: unnecessary? */
5485 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5486 +           && cpg->bdst < cpg->bsrc
5487 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5488 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5489 +               /* ignore this error */
5490 +
5491 +       if (!err) {
5492 +               force = 0;
5493 +               if (isreg) {
5494 +                       force = !!cpg->len;
5495 +                       if (cpg->len == -1)
5496 +                               force = !!i_size_read(h_inode);
5497 +               }
5498 +               au_fhsm_wrote(sb, cpg->bdst, force);
5499 +       }
5500 +
5501 +       if (do_dt)
5502 +               au_dtime_revert(&dt);
5503 +       return err;
5504 +}
5505 +
5506 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5507 +{
5508 +       int err;
5509 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5510 +       struct path h_ppath;
5511 +       struct inode *h_dir;
5512 +       aufs_bindex_t bdst;
5513 +
5514 +       dentry = cpg->dentry;
5515 +       bdst = cpg->bdst;
5516 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5517 +       h_dentry = au_h_dptr(dentry, bdst);
5518 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5519 +               dget(h_dentry);
5520 +               au_set_h_dptr(dentry, bdst, NULL);
5521 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5522 +               if (!err)
5523 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5524 +               au_set_h_dptr(dentry, bdst, h_dentry);
5525 +       } else {
5526 +               err = 0;
5527 +               parent = dget_parent(dentry);
5528 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5529 +               dput(parent);
5530 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5531 +               if (IS_ERR(h_path->dentry))
5532 +                       err = PTR_ERR(h_path->dentry);
5533 +       }
5534 +       if (unlikely(err))
5535 +               goto out;
5536 +
5537 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5538 +       h_dir = d_inode(h_parent);
5539 +       IMustLock(h_dir);
5540 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5541 +       /* no delegation since it is just created */
5542 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5543 +                          /*flags*/0);
5544 +       dput(h_path->dentry);
5545 +
5546 +out:
5547 +       return err;
5548 +}
5549 +
5550 +/*
5551 + * copyup the @dentry from @bsrc to @bdst.
5552 + * the caller must set the both of lower dentries.
5553 + * @len is for truncating when it is -1 copyup the entire file.
5554 + * in link/rename cases, @dst_parent may be different from the real one.
5555 + * basic->bsrc can be larger than basic->bdst.
5556 + * aufs doesn't touch the credential so
5557 + * security_inode_copy_up{,_xattr}() are unnecessary.
5558 + */
5559 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5560 +{
5561 +       int err, rerr;
5562 +       aufs_bindex_t old_ibtop;
5563 +       unsigned char isdir, plink;
5564 +       struct dentry *h_src, *h_dst, *h_parent;
5565 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5566 +       struct super_block *sb;
5567 +       struct au_branch *br;
5568 +       struct path h_src_path;
5569 +       /* to reduce stack size */
5570 +       struct {
5571 +               struct au_dtime dt;
5572 +               struct path h_path;
5573 +               struct au_cpup_reg_attr h_src_attr;
5574 +       } *a;
5575 +
5576 +       err = -ENOMEM;
5577 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5578 +       if (unlikely(!a))
5579 +               goto out;
5580 +       a->h_src_attr.valid = 0;
5581 +
5582 +       sb = cpg->dentry->d_sb;
5583 +       br = au_sbr(sb, cpg->bdst);
5584 +       a->h_path.mnt = au_br_mnt(br);
5585 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5586 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5587 +       h_dir = d_inode(h_parent);
5588 +       IMustLock(h_dir);
5589 +
5590 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5591 +       inode = d_inode(cpg->dentry);
5592 +
5593 +       if (!dst_parent)
5594 +               dst_parent = dget_parent(cpg->dentry);
5595 +       else
5596 +               dget(dst_parent);
5597 +
5598 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5599 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5600 +       if (dst_inode) {
5601 +               if (unlikely(!plink)) {
5602 +                       err = -EIO;
5603 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5604 +                               "but plink is disabled\n",
5605 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5606 +                       goto out_parent;
5607 +               }
5608 +
5609 +               if (dst_inode->i_nlink) {
5610 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5611 +
5612 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5613 +                       err = PTR_ERR(h_src);
5614 +                       if (IS_ERR(h_src))
5615 +                               goto out_parent;
5616 +                       if (unlikely(d_is_negative(h_src))) {
5617 +                               err = -EIO;
5618 +                               AuIOErr("i%lu exists on b%d "
5619 +                                       "but not pseudo-linked\n",
5620 +                                       inode->i_ino, cpg->bdst);
5621 +                               dput(h_src);
5622 +                               goto out_parent;
5623 +                       }
5624 +
5625 +                       if (do_dt) {
5626 +                               a->h_path.dentry = h_parent;
5627 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5628 +                       }
5629 +
5630 +                       a->h_path.dentry = h_dst;
5631 +                       delegated = NULL;
5632 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5633 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5634 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5635 +                       if (do_dt)
5636 +                               au_dtime_revert(&a->dt);
5637 +                       if (unlikely(err == -EWOULDBLOCK)) {
5638 +                               pr_warn("cannot retry for NFSv4 delegation"
5639 +                                       " for an internal link\n");
5640 +                               iput(delegated);
5641 +                       }
5642 +                       dput(h_src);
5643 +                       goto out_parent;
5644 +               } else
5645 +                       /* todo: cpup_wh_file? */
5646 +                       /* udba work */
5647 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5648 +       }
5649 +
5650 +       isdir = S_ISDIR(inode->i_mode);
5651 +       old_ibtop = au_ibtop(inode);
5652 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5653 +       if (unlikely(err))
5654 +               goto out_rev;
5655 +       dst_inode = d_inode(h_dst);
5656 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5657 +       /* todo: necessary? */
5658 +       /* au_pin_hdir_unlock(cpg->pin); */
5659 +
5660 +       h_src_path.dentry = h_src;
5661 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5662 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5663 +       if (unlikely(err)) {
5664 +               /* todo: necessary? */
5665 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5666 +               inode_unlock(dst_inode);
5667 +               goto out_rev;
5668 +       }
5669 +
5670 +       if (cpg->bdst < old_ibtop) {
5671 +               if (S_ISREG(inode->i_mode)) {
5672 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5673 +                       if (unlikely(err)) {
5674 +                               /* ignore an error */
5675 +                               /* au_pin_hdir_relock(cpg->pin); */
5676 +                               inode_unlock(dst_inode);
5677 +                               goto out_rev;
5678 +                       }
5679 +               }
5680 +               au_set_ibtop(inode, cpg->bdst);
5681 +       } else
5682 +               au_set_ibbot(inode, cpg->bdst);
5683 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5684 +                     au_hi_flags(inode, isdir));
5685 +
5686 +       /* todo: necessary? */
5687 +       /* err = au_pin_hdir_relock(cpg->pin); */
5688 +       inode_unlock(dst_inode);
5689 +       if (unlikely(err))
5690 +               goto out_rev;
5691 +
5692 +       src_inode = d_inode(h_src);
5693 +       if (!isdir
5694 +           && (src_inode->i_nlink > 1
5695 +               || src_inode->i_state & I_LINKABLE)
5696 +           && plink)
5697 +               au_plink_append(inode, cpg->bdst, h_dst);
5698 +
5699 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5700 +               a->h_path.dentry = h_dst;
5701 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5702 +       }
5703 +       if (!err)
5704 +               goto out_parent; /* success */
5705 +
5706 +       /* revert */
5707 +out_rev:
5708 +       a->h_path.dentry = h_parent;
5709 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5710 +       a->h_path.dentry = h_dst;
5711 +       rerr = 0;
5712 +       if (d_is_positive(h_dst)) {
5713 +               if (!isdir) {
5714 +                       /* no delegation since it is just created */
5715 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5716 +                                           /*delegated*/NULL, /*force*/0);
5717 +               } else
5718 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5719 +       }
5720 +       au_dtime_revert(&a->dt);
5721 +       if (rerr) {
5722 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5723 +               err = -EIO;
5724 +       }
5725 +out_parent:
5726 +       dput(dst_parent);
5727 +       au_kfree_rcu(a);
5728 +out:
5729 +       return err;
5730 +}
5731 +
5732 +#if 0 /* reserved */
5733 +struct au_cpup_single_args {
5734 +       int *errp;
5735 +       struct au_cp_generic *cpg;
5736 +       struct dentry *dst_parent;
5737 +};
5738 +
5739 +static void au_call_cpup_single(void *args)
5740 +{
5741 +       struct au_cpup_single_args *a = args;
5742 +
5743 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5744 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5745 +       au_pin_hdir_release(a->cpg->pin);
5746 +}
5747 +#endif
5748 +
5749 +/*
5750 + * prevent SIGXFSZ in copy-up.
5751 + * testing CAP_MKNOD is for generic fs,
5752 + * but CAP_FSETID is for xfs only, currently.
5753 + */
5754 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5755 +{
5756 +       int do_sio;
5757 +       struct super_block *sb;
5758 +       struct inode *h_dir;
5759 +
5760 +       do_sio = 0;
5761 +       sb = au_pinned_parent(pin)->d_sb;
5762 +       if (!au_wkq_test()
5763 +           && (!au_sbi(sb)->si_plink_maint_pid
5764 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5765 +               switch (mode & S_IFMT) {
5766 +               case S_IFREG:
5767 +                       /* no condition about RLIMIT_FSIZE and the file size */
5768 +                       do_sio = 1;
5769 +                       break;
5770 +               case S_IFCHR:
5771 +               case S_IFBLK:
5772 +                       do_sio = !capable(CAP_MKNOD);
5773 +                       break;
5774 +               }
5775 +               if (!do_sio)
5776 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5777 +                                 && !capable(CAP_FSETID));
5778 +               /* this workaround may be removed in the future */
5779 +               if (!do_sio) {
5780 +                       h_dir = au_pinned_h_dir(pin);
5781 +                       do_sio = h_dir->i_mode & S_ISVTX;
5782 +               }
5783 +       }
5784 +
5785 +       return do_sio;
5786 +}
5787 +
5788 +#if 0 /* reserved */
5789 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5790 +{
5791 +       int err, wkq_err;
5792 +       struct dentry *h_dentry;
5793 +
5794 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5795 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5796 +               err = au_cpup_single(cpg, dst_parent);
5797 +       else {
5798 +               struct au_cpup_single_args args = {
5799 +                       .errp           = &err,
5800 +                       .cpg            = cpg,
5801 +                       .dst_parent     = dst_parent
5802 +               };
5803 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5804 +               if (unlikely(wkq_err))
5805 +                       err = wkq_err;
5806 +       }
5807 +
5808 +       return err;
5809 +}
5810 +#endif
5811 +
5812 +/*
5813 + * copyup the @dentry from the first active lower branch to @bdst,
5814 + * using au_cpup_single().
5815 + */
5816 +static int au_cpup_simple(struct au_cp_generic *cpg)
5817 +{
5818 +       int err;
5819 +       unsigned int flags_orig;
5820 +       struct dentry *dentry;
5821 +
5822 +       AuDebugOn(cpg->bsrc < 0);
5823 +
5824 +       dentry = cpg->dentry;
5825 +       DiMustWriteLock(dentry);
5826 +
5827 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5828 +       if (!err) {
5829 +               flags_orig = cpg->flags;
5830 +               au_fset_cpup(cpg->flags, RENAME);
5831 +               err = au_cpup_single(cpg, NULL);
5832 +               cpg->flags = flags_orig;
5833 +               if (!err)
5834 +                       return 0; /* success */
5835 +
5836 +               /* revert */
5837 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5838 +               au_set_dbtop(dentry, cpg->bsrc);
5839 +       }
5840 +
5841 +       return err;
5842 +}
5843 +
5844 +struct au_cpup_simple_args {
5845 +       int *errp;
5846 +       struct au_cp_generic *cpg;
5847 +};
5848 +
5849 +static void au_call_cpup_simple(void *args)
5850 +{
5851 +       struct au_cpup_simple_args *a = args;
5852 +
5853 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5854 +       *a->errp = au_cpup_simple(a->cpg);
5855 +       au_pin_hdir_release(a->cpg->pin);
5856 +}
5857 +
5858 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5859 +{
5860 +       int err, wkq_err;
5861 +       struct dentry *dentry, *parent;
5862 +       struct file *h_file;
5863 +       struct inode *h_dir;
5864 +       struct user_namespace *h_userns;
5865 +
5866 +       dentry = cpg->dentry;
5867 +       h_file = NULL;
5868 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5869 +               AuDebugOn(cpg->bsrc < 0);
5870 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5871 +               err = PTR_ERR(h_file);
5872 +               if (IS_ERR(h_file))
5873 +                       goto out;
5874 +       }
5875 +
5876 +       parent = dget_parent(dentry);
5877 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5878 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5879 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5880 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5881 +               err = au_cpup_simple(cpg);
5882 +       else {
5883 +               struct au_cpup_simple_args args = {
5884 +                       .errp           = &err,
5885 +                       .cpg            = cpg
5886 +               };
5887 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5888 +               if (unlikely(wkq_err))
5889 +                       err = wkq_err;
5890 +       }
5891 +
5892 +       dput(parent);
5893 +       if (h_file)
5894 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5895 +
5896 +out:
5897 +       return err;
5898 +}
5899 +
5900 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5901 +{
5902 +       aufs_bindex_t bsrc, bbot;
5903 +       struct dentry *dentry, *h_dentry;
5904 +
5905 +       if (cpg->bsrc < 0) {
5906 +               dentry = cpg->dentry;
5907 +               bbot = au_dbbot(dentry);
5908 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5909 +                       h_dentry = au_h_dptr(dentry, bsrc);
5910 +                       if (h_dentry) {
5911 +                               AuDebugOn(d_is_negative(h_dentry));
5912 +                               break;
5913 +                       }
5914 +               }
5915 +               AuDebugOn(bsrc > bbot);
5916 +               cpg->bsrc = bsrc;
5917 +       }
5918 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5919 +       return au_do_sio_cpup_simple(cpg);
5920 +}
5921 +
5922 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5923 +{
5924 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5925 +       return au_do_sio_cpup_simple(cpg);
5926 +}
5927 +
5928 +/* ---------------------------------------------------------------------- */
5929 +
5930 +/*
5931 + * copyup the deleted file for writing.
5932 + */
5933 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5934 +                        struct file *file)
5935 +{
5936 +       int err;
5937 +       unsigned int flags_orig;
5938 +       aufs_bindex_t bsrc_orig;
5939 +       struct au_dinfo *dinfo;
5940 +       struct {
5941 +               struct au_hdentry *hd;
5942 +               struct dentry *h_dentry;
5943 +       } hdst, hsrc;
5944 +
5945 +       dinfo = au_di(cpg->dentry);
5946 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5947 +
5948 +       bsrc_orig = cpg->bsrc;
5949 +       cpg->bsrc = dinfo->di_btop;
5950 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5951 +       hdst.h_dentry = hdst.hd->hd_dentry;
5952 +       hdst.hd->hd_dentry = wh_dentry;
5953 +       dinfo->di_btop = cpg->bdst;
5954 +
5955 +       hsrc.h_dentry = NULL;
5956 +       if (file) {
5957 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5958 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5959 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5960 +       }
5961 +       flags_orig = cpg->flags;
5962 +       cpg->flags = !AuCpup_DTIME;
5963 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5964 +       cpg->flags = flags_orig;
5965 +       if (file) {
5966 +               if (!err)
5967 +                       err = au_reopen_nondir(file);
5968 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5969 +       }
5970 +       hdst.hd->hd_dentry = hdst.h_dentry;
5971 +       dinfo->di_btop = cpg->bsrc;
5972 +       cpg->bsrc = bsrc_orig;
5973 +
5974 +       return err;
5975 +}
5976 +
5977 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5978 +{
5979 +       int err;
5980 +       aufs_bindex_t bdst;
5981 +       struct au_dtime dt;
5982 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5983 +       struct au_branch *br;
5984 +       struct path h_path;
5985 +
5986 +       dentry = cpg->dentry;
5987 +       bdst = cpg->bdst;
5988 +       br = au_sbr(dentry->d_sb, bdst);
5989 +       parent = dget_parent(dentry);
5990 +       h_parent = au_h_dptr(parent, bdst);
5991 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5992 +       err = PTR_ERR(wh_dentry);
5993 +       if (IS_ERR(wh_dentry))
5994 +               goto out;
5995 +
5996 +       h_path.dentry = h_parent;
5997 +       h_path.mnt = au_br_mnt(br);
5998 +       au_dtime_store(&dt, parent, &h_path);
5999 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6000 +       if (unlikely(err))
6001 +               goto out_wh;
6002 +
6003 +       dget(wh_dentry);
6004 +       h_path.dentry = wh_dentry;
6005 +       if (!d_is_dir(wh_dentry)) {
6006 +               /* no delegation since it is just created */
6007 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6008 +                                  /*delegated*/NULL, /*force*/0);
6009 +       } else
6010 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6011 +       if (unlikely(err)) {
6012 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6013 +                       wh_dentry, err);
6014 +               err = -EIO;
6015 +       }
6016 +       au_dtime_revert(&dt);
6017 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6018 +
6019 +out_wh:
6020 +       dput(wh_dentry);
6021 +out:
6022 +       dput(parent);
6023 +       return err;
6024 +}
6025 +
6026 +struct au_cpup_wh_args {
6027 +       int *errp;
6028 +       struct au_cp_generic *cpg;
6029 +       struct file *file;
6030 +};
6031 +
6032 +static void au_call_cpup_wh(void *args)
6033 +{
6034 +       struct au_cpup_wh_args *a = args;
6035 +
6036 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6037 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6038 +       au_pin_hdir_release(a->cpg->pin);
6039 +}
6040 +
6041 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6042 +{
6043 +       int err, wkq_err;
6044 +       aufs_bindex_t bdst;
6045 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6046 +       struct inode *dir, *h_dir, *h_tmpdir;
6047 +       struct au_wbr *wbr;
6048 +       struct au_pin wh_pin, *pin_orig;
6049 +       struct user_namespace *h_userns;
6050 +
6051 +       dentry = cpg->dentry;
6052 +       bdst = cpg->bdst;
6053 +       parent = dget_parent(dentry);
6054 +       dir = d_inode(parent);
6055 +       h_orph = NULL;
6056 +       h_parent = NULL;
6057 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6058 +       h_tmpdir = h_dir;
6059 +       pin_orig = NULL;
6060 +       if (!h_dir->i_nlink) {
6061 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6062 +               h_orph = wbr->wbr_orph;
6063 +
6064 +               h_parent = dget(au_h_dptr(parent, bdst));
6065 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6066 +               h_tmpdir = d_inode(h_orph);
6067 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6068 +
6069 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6070 +               /* todo: au_h_open_pre()? */
6071 +
6072 +               pin_orig = cpg->pin;
6073 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6074 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6075 +               cpg->pin = &wh_pin;
6076 +       }
6077 +
6078 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6079 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6080 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6081 +               err = au_cpup_wh(cpg, file);
6082 +       else {
6083 +               struct au_cpup_wh_args args = {
6084 +                       .errp   = &err,
6085 +                       .cpg    = cpg,
6086 +                       .file   = file
6087 +               };
6088 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6089 +               if (unlikely(wkq_err))
6090 +                       err = wkq_err;
6091 +       }
6092 +
6093 +       if (h_orph) {
6094 +               inode_unlock(h_tmpdir);
6095 +               /* todo: au_h_open_post()? */
6096 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6097 +               au_set_h_dptr(parent, bdst, h_parent);
6098 +               AuDebugOn(!pin_orig);
6099 +               cpg->pin = pin_orig;
6100 +       }
6101 +       iput(h_dir);
6102 +       dput(parent);
6103 +
6104 +       return err;
6105 +}
6106 +
6107 +/* ---------------------------------------------------------------------- */
6108 +
6109 +/*
6110 + * generic routine for both of copy-up and copy-down.
6111 + */
6112 +/* cf. revalidate function in file.c */
6113 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6114 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6115 +                        struct au_pin *pin,
6116 +                        struct dentry *h_parent, void *arg),
6117 +              void *arg)
6118 +{
6119 +       int err;
6120 +       struct au_pin pin;
6121 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6122 +
6123 +       err = 0;
6124 +       parent = dget_parent(dentry);
6125 +       if (IS_ROOT(parent))
6126 +               goto out;
6127 +
6128 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6129 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6130 +
6131 +       /* do not use au_dpage */
6132 +       real_parent = parent;
6133 +       while (1) {
6134 +               dput(parent);
6135 +               parent = dget_parent(dentry);
6136 +               h_parent = au_h_dptr(parent, bdst);
6137 +               if (h_parent)
6138 +                       goto out; /* success */
6139 +
6140 +               /* find top dir which is necessary to cpup */
6141 +               do {
6142 +                       d = parent;
6143 +                       dput(parent);
6144 +                       parent = dget_parent(d);
6145 +                       di_read_lock_parent3(parent, !AuLock_IR);
6146 +                       h_parent = au_h_dptr(parent, bdst);
6147 +                       di_read_unlock(parent, !AuLock_IR);
6148 +               } while (!h_parent);
6149 +
6150 +               if (d != real_parent)
6151 +                       di_write_lock_child3(d);
6152 +
6153 +               /* somebody else might create while we were sleeping */
6154 +               h_dentry = au_h_dptr(d, bdst);
6155 +               if (!h_dentry || d_is_negative(h_dentry)) {
6156 +                       if (h_dentry)
6157 +                               au_update_dbtop(d);
6158 +
6159 +                       au_pin_set_dentry(&pin, d);
6160 +                       err = au_do_pin(&pin);
6161 +                       if (!err) {
6162 +                               err = cp(d, bdst, &pin, h_parent, arg);
6163 +                               au_unpin(&pin);
6164 +                       }
6165 +               }
6166 +
6167 +               if (d != real_parent)
6168 +                       di_write_unlock(d);
6169 +               if (unlikely(err))
6170 +                       break;
6171 +       }
6172 +
6173 +out:
6174 +       dput(parent);
6175 +       return err;
6176 +}
6177 +
6178 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6179 +                      struct au_pin *pin,
6180 +                      struct dentry *h_parent __maybe_unused,
6181 +                      void *arg __maybe_unused)
6182 +{
6183 +       struct au_cp_generic cpg = {
6184 +               .dentry = dentry,
6185 +               .bdst   = bdst,
6186 +               .bsrc   = -1,
6187 +               .len    = 0,
6188 +               .pin    = pin,
6189 +               .flags  = AuCpup_DTIME
6190 +       };
6191 +       return au_sio_cpup_simple(&cpg);
6192 +}
6193 +
6194 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6195 +{
6196 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6197 +}
6198 +
6199 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6200 +{
6201 +       int err;
6202 +       struct dentry *parent;
6203 +       struct inode *dir;
6204 +
6205 +       parent = dget_parent(dentry);
6206 +       dir = d_inode(parent);
6207 +       err = 0;
6208 +       if (au_h_iptr(dir, bdst))
6209 +               goto out;
6210 +
6211 +       di_read_unlock(parent, AuLock_IR);
6212 +       di_write_lock_parent(parent);
6213 +       /* someone else might change our inode while we were sleeping */
6214 +       if (!au_h_iptr(dir, bdst))
6215 +               err = au_cpup_dirs(dentry, bdst);
6216 +       di_downgrade_lock(parent, AuLock_IR);
6217 +
6218 +out:
6219 +       dput(parent);
6220 +       return err;
6221 +}
6222 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6223 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6224 +++ linux/fs/aufs/cpup.h        2022-11-05 23:02:18.962555950 +0100
6225 @@ -0,0 +1,100 @@
6226 +/* SPDX-License-Identifier: GPL-2.0 */
6227 +/*
6228 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6229 + *
6230 + * This program is free software; you can redistribute it and/or modify
6231 + * it under the terms of the GNU General Public License as published by
6232 + * the Free Software Foundation; either version 2 of the License, or
6233 + * (at your option) any later version.
6234 + *
6235 + * This program is distributed in the hope that it will be useful,
6236 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6237 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6238 + * GNU General Public License for more details.
6239 + *
6240 + * You should have received a copy of the GNU General Public License
6241 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6242 + */
6243 +
6244 +/*
6245 + * copy-up/down functions
6246 + */
6247 +
6248 +#ifndef __AUFS_CPUP_H__
6249 +#define __AUFS_CPUP_H__
6250 +
6251 +#ifdef __KERNEL__
6252 +
6253 +#include <linux/path.h>
6254 +
6255 +struct inode;
6256 +struct file;
6257 +struct au_pin;
6258 +
6259 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6260 +void au_cpup_attr_timesizes(struct inode *inode);
6261 +void au_cpup_attr_nlink(struct inode *inode, int force);
6262 +void au_cpup_attr_changeable(struct inode *inode);
6263 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6264 +void au_cpup_attr_all(struct inode *inode, int force);
6265 +
6266 +/* ---------------------------------------------------------------------- */
6267 +
6268 +struct au_cp_generic {
6269 +       struct dentry   *dentry;
6270 +       aufs_bindex_t   bdst, bsrc;
6271 +       loff_t          len;
6272 +       struct au_pin   *pin;
6273 +       unsigned int    flags;
6274 +};
6275 +
6276 +/* cpup flags */
6277 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6278 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6279 +                                                  for link(2) */
6280 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6281 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6282 +                                                  cpup */
6283 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6284 +                                                  existing entry */
6285 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6286 +                                                  the branch is marked as RO */
6287 +
6288 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6289 +#undef AuCpup_HOPEN
6290 +#define AuCpup_HOPEN           0
6291 +#endif
6292 +
6293 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6294 +#define au_fset_cpup(flags, name) \
6295 +       do { (flags) |= AuCpup_##name; } while (0)
6296 +#define au_fclr_cpup(flags, name) \
6297 +       do { (flags) &= ~AuCpup_##name; } while (0)
6298 +
6299 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6300 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6301 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6302 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6303 +
6304 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6305 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6306 +                        struct au_pin *pin,
6307 +                        struct dentry *h_parent, void *arg),
6308 +              void *arg);
6309 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6310 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6311 +
6312 +/* ---------------------------------------------------------------------- */
6313 +
6314 +/* keep timestamps when copyup */
6315 +struct au_dtime {
6316 +       struct dentry *dt_dentry;
6317 +       struct path dt_h_path;
6318 +       struct timespec64 dt_atime, dt_mtime;
6319 +};
6320 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6321 +                   struct path *h_path);
6322 +void au_dtime_revert(struct au_dtime *dt);
6323 +
6324 +#endif /* __KERNEL__ */
6325 +#endif /* __AUFS_CPUP_H__ */
6326 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6327 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6328 +++ linux/fs/aufs/dbgaufs.c     2022-11-05 23:02:18.962555950 +0100
6329 @@ -0,0 +1,526 @@
6330 +// SPDX-License-Identifier: GPL-2.0
6331 +/*
6332 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6333 + *
6334 + * This program is free software; you can redistribute it and/or modify
6335 + * it under the terms of the GNU General Public License as published by
6336 + * the Free Software Foundation; either version 2 of the License, or
6337 + * (at your option) any later version.
6338 + *
6339 + * This program is distributed in the hope that it will be useful,
6340 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6341 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6342 + * GNU General Public License for more details.
6343 + *
6344 + * You should have received a copy of the GNU General Public License
6345 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6346 + */
6347 +
6348 +/*
6349 + * debugfs interface
6350 + */
6351 +
6352 +#include <linux/debugfs.h>
6353 +#include "aufs.h"
6354 +
6355 +#ifndef CONFIG_SYSFS
6356 +#error DEBUG_FS depends upon SYSFS
6357 +#endif
6358 +
6359 +static struct dentry *dbgaufs;
6360 +static const mode_t dbgaufs_mode = 0444;
6361 +
6362 +/* 20 is max digits length of ulong 64 */
6363 +struct dbgaufs_arg {
6364 +       int n;
6365 +       char a[20 * 4];
6366 +};
6367 +
6368 +/*
6369 + * common function for all XINO files
6370 + */
6371 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6372 +                             struct file *file)
6373 +{
6374 +       void *p;
6375 +
6376 +       p = file->private_data;
6377 +       if (p) {
6378 +               /* this is struct dbgaufs_arg */
6379 +               AuDebugOn(!au_kfree_sz_test(p));
6380 +               au_kfree_do_rcu(p);
6381 +       }
6382 +       return 0;
6383 +}
6384 +
6385 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6386 +                          int cnt)
6387 +{
6388 +       int err;
6389 +       struct kstat st;
6390 +       struct dbgaufs_arg *p;
6391 +
6392 +       err = -ENOMEM;
6393 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6394 +       if (unlikely(!p))
6395 +               goto out;
6396 +
6397 +       err = 0;
6398 +       p->n = 0;
6399 +       file->private_data = p;
6400 +       if (!xf)
6401 +               goto out;
6402 +
6403 +       err = vfsub_getattr(&xf->f_path, &st);
6404 +       if (!err) {
6405 +               if (do_fcnt)
6406 +                       p->n = snprintf
6407 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6408 +                                cnt, st.blocks, st.blksize,
6409 +                                (long long)st.size);
6410 +               else
6411 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6412 +                                       st.blocks, st.blksize,
6413 +                                       (long long)st.size);
6414 +               AuDebugOn(p->n >= sizeof(p->a));
6415 +       } else {
6416 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6417 +               err = 0;
6418 +       }
6419 +
6420 +out:
6421 +       return err;
6422 +}
6423 +
6424 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6425 +                              size_t count, loff_t *ppos)
6426 +{
6427 +       struct dbgaufs_arg *p;
6428 +
6429 +       p = file->private_data;
6430 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6431 +}
6432 +
6433 +/* ---------------------------------------------------------------------- */
6434 +
6435 +struct dbgaufs_plink_arg {
6436 +       int n;
6437 +       char a[];
6438 +};
6439 +
6440 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6441 +                                struct file *file)
6442 +{
6443 +       free_page((unsigned long)file->private_data);
6444 +       return 0;
6445 +}
6446 +
6447 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6448 +{
6449 +       int err, i, limit;
6450 +       unsigned long n, sum;
6451 +       struct dbgaufs_plink_arg *p;
6452 +       struct au_sbinfo *sbinfo;
6453 +       struct super_block *sb;
6454 +       struct hlist_bl_head *hbl;
6455 +
6456 +       err = -ENOMEM;
6457 +       p = (void *)get_zeroed_page(GFP_NOFS);
6458 +       if (unlikely(!p))
6459 +               goto out;
6460 +
6461 +       err = -EFBIG;
6462 +       sbinfo = inode->i_private;
6463 +       sb = sbinfo->si_sb;
6464 +       si_noflush_read_lock(sb);
6465 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6466 +               limit = PAGE_SIZE - sizeof(p->n);
6467 +
6468 +               /* the number of buckets */
6469 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6470 +               p->n += n;
6471 +               limit -= n;
6472 +
6473 +               sum = 0;
6474 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6475 +                    i++, hbl++) {
6476 +                       n = au_hbl_count(hbl);
6477 +                       sum += n;
6478 +
6479 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6480 +                       p->n += n;
6481 +                       limit -= n;
6482 +                       if (unlikely(limit <= 0))
6483 +                               goto out_free;
6484 +               }
6485 +               p->a[p->n - 1] = '\n';
6486 +
6487 +               /* the sum of plinks */
6488 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6489 +               p->n += n;
6490 +               limit -= n;
6491 +               if (unlikely(limit <= 0))
6492 +                       goto out_free;
6493 +       } else {
6494 +#define str "1\n0\n0\n"
6495 +               p->n = sizeof(str) - 1;
6496 +               strcpy(p->a, str);
6497 +#undef str
6498 +       }
6499 +       si_read_unlock(sb);
6500 +
6501 +       err = 0;
6502 +       file->private_data = p;
6503 +       goto out; /* success */
6504 +
6505 +out_free:
6506 +       free_page((unsigned long)p);
6507 +out:
6508 +       return err;
6509 +}
6510 +
6511 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6512 +                                 size_t count, loff_t *ppos)
6513 +{
6514 +       struct dbgaufs_plink_arg *p;
6515 +
6516 +       p = file->private_data;
6517 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6518 +}
6519 +
6520 +static const struct file_operations dbgaufs_plink_fop = {
6521 +       .owner          = THIS_MODULE,
6522 +       .open           = dbgaufs_plink_open,
6523 +       .release        = dbgaufs_plink_release,
6524 +       .read           = dbgaufs_plink_read
6525 +};
6526 +
6527 +/* ---------------------------------------------------------------------- */
6528 +
6529 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6530 +{
6531 +       int err;
6532 +       struct au_sbinfo *sbinfo;
6533 +       struct super_block *sb;
6534 +
6535 +       sbinfo = inode->i_private;
6536 +       sb = sbinfo->si_sb;
6537 +       si_noflush_read_lock(sb);
6538 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6539 +       si_read_unlock(sb);
6540 +       return err;
6541 +}
6542 +
6543 +static const struct file_operations dbgaufs_xib_fop = {
6544 +       .owner          = THIS_MODULE,
6545 +       .open           = dbgaufs_xib_open,
6546 +       .release        = dbgaufs_xi_release,
6547 +       .read           = dbgaufs_xi_read
6548 +};
6549 +
6550 +/* ---------------------------------------------------------------------- */
6551 +
6552 +#define DbgaufsXi_PREFIX "xi"
6553 +
6554 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6555 +{
6556 +       int err, idx;
6557 +       long l;
6558 +       aufs_bindex_t bindex;
6559 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6560 +       struct au_sbinfo *sbinfo;
6561 +       struct super_block *sb;
6562 +       struct au_xino *xi;
6563 +       struct file *xf;
6564 +       struct qstr *name;
6565 +       struct au_branch *br;
6566 +
6567 +       err = -ENOENT;
6568 +       name = &file->f_path.dentry->d_name;
6569 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6570 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6571 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6572 +               goto out;
6573 +
6574 +       AuDebugOn(name->len >= sizeof(a));
6575 +       memcpy(a, name->name, name->len);
6576 +       a[name->len] = '\0';
6577 +       p = strchr(a, '-');
6578 +       if (p)
6579 +               *p = '\0';
6580 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6581 +       if (unlikely(err))
6582 +               goto out;
6583 +       bindex = l;
6584 +       idx = 0;
6585 +       if (p) {
6586 +               err = kstrtol(p + 1, 10, &l);
6587 +               if (unlikely(err))
6588 +                       goto out;
6589 +               idx = l;
6590 +       }
6591 +
6592 +       err = -ENOENT;
6593 +       sbinfo = inode->i_private;
6594 +       sb = sbinfo->si_sb;
6595 +       si_noflush_read_lock(sb);
6596 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6597 +               goto out_si;
6598 +       br = au_sbr(sb, bindex);
6599 +       xi = br->br_xino;
6600 +       if (unlikely(idx >= xi->xi_nfile))
6601 +               goto out_si;
6602 +       xf = au_xino_file(xi, idx);
6603 +       if (xf)
6604 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6605 +                                     au_xino_count(br));
6606 +
6607 +out_si:
6608 +       si_read_unlock(sb);
6609 +out:
6610 +       AuTraceErr(err);
6611 +       return err;
6612 +}
6613 +
6614 +static const struct file_operations dbgaufs_xino_fop = {
6615 +       .owner          = THIS_MODULE,
6616 +       .open           = dbgaufs_xino_open,
6617 +       .release        = dbgaufs_xi_release,
6618 +       .read           = dbgaufs_xi_read
6619 +};
6620 +
6621 +void dbgaufs_xino_del(struct au_branch *br)
6622 +{
6623 +       struct dentry *dbgaufs;
6624 +
6625 +       dbgaufs = br->br_dbgaufs;
6626 +       if (!dbgaufs)
6627 +               return;
6628 +
6629 +       br->br_dbgaufs = NULL;
6630 +       /* debugfs acquires the parent i_mutex */
6631 +       lockdep_off();
6632 +       debugfs_remove(dbgaufs);
6633 +       lockdep_on();
6634 +}
6635 +
6636 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6637 +{
6638 +       aufs_bindex_t bbot;
6639 +       struct au_branch *br;
6640 +
6641 +       if (!au_sbi(sb)->si_dbgaufs)
6642 +               return;
6643 +
6644 +       bbot = au_sbbot(sb);
6645 +       for (; bindex <= bbot; bindex++) {
6646 +               br = au_sbr(sb, bindex);
6647 +               dbgaufs_xino_del(br);
6648 +       }
6649 +}
6650 +
6651 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6652 +                             unsigned int idx, struct dentry *parent,
6653 +                             struct au_sbinfo *sbinfo)
6654 +{
6655 +       struct au_branch *br;
6656 +       struct dentry *d;
6657 +       /* "xi" bindex(5) "-" idx(2) NULL */
6658 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6659 +
6660 +       if (!idx)
6661 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6662 +       else
6663 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6664 +                        bindex, idx);
6665 +       br = au_sbr(sb, bindex);
6666 +       if (br->br_dbgaufs) {
6667 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6668 +
6669 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6670 +                       /* debugfs acquires the parent i_mutex */
6671 +                       lockdep_off();
6672 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6673 +                                          name);
6674 +                       lockdep_on();
6675 +                       if (unlikely(!d))
6676 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6677 +                                       parent, name);
6678 +               }
6679 +       } else {
6680 +               lockdep_off();
6681 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6682 +                                                    sbinfo, &dbgaufs_xino_fop);
6683 +               lockdep_on();
6684 +               if (unlikely(!br->br_dbgaufs))
6685 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6686 +                               parent, name);
6687 +       }
6688 +}
6689 +
6690 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6691 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6692 +{
6693 +       struct au_branch *br;
6694 +       struct au_xino *xi;
6695 +       unsigned int u;
6696 +
6697 +       br = au_sbr(sb, bindex);
6698 +       xi = br->br_xino;
6699 +       for (u = 0; u < xi->xi_nfile; u++)
6700 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6701 +}
6702 +
6703 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6704 +{
6705 +       struct au_sbinfo *sbinfo;
6706 +       struct dentry *parent;
6707 +       aufs_bindex_t bbot;
6708 +
6709 +       if (!au_opt_test(au_mntflags(sb), XINO))
6710 +               return;
6711 +
6712 +       sbinfo = au_sbi(sb);
6713 +       parent = sbinfo->si_dbgaufs;
6714 +       if (!parent)
6715 +               return;
6716 +
6717 +       bbot = au_sbbot(sb);
6718 +       if (topdown)
6719 +               for (; bindex <= bbot; bindex++)
6720 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6721 +       else
6722 +               for (; bbot >= bindex; bbot--)
6723 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6724 +}
6725 +
6726 +/* ---------------------------------------------------------------------- */
6727 +
6728 +#ifdef CONFIG_AUFS_EXPORT
6729 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6730 +{
6731 +       int err;
6732 +       struct au_sbinfo *sbinfo;
6733 +       struct super_block *sb;
6734 +
6735 +       sbinfo = inode->i_private;
6736 +       sb = sbinfo->si_sb;
6737 +       si_noflush_read_lock(sb);
6738 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6739 +       si_read_unlock(sb);
6740 +       return err;
6741 +}
6742 +
6743 +static const struct file_operations dbgaufs_xigen_fop = {
6744 +       .owner          = THIS_MODULE,
6745 +       .open           = dbgaufs_xigen_open,
6746 +       .release        = dbgaufs_xi_release,
6747 +       .read           = dbgaufs_xi_read
6748 +};
6749 +
6750 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6751 +{
6752 +       int err;
6753 +
6754 +       /*
6755 +        * This function is a dynamic '__init' function actually,
6756 +        * so the tiny check for si_rwsem is unnecessary.
6757 +        */
6758 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6759 +
6760 +       err = -EIO;
6761 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6762 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6763 +                &dbgaufs_xigen_fop);
6764 +       if (sbinfo->si_dbgaufs_xigen)
6765 +               err = 0;
6766 +
6767 +       return err;
6768 +}
6769 +#else
6770 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6771 +{
6772 +       return 0;
6773 +}
6774 +#endif /* CONFIG_AUFS_EXPORT */
6775 +
6776 +/* ---------------------------------------------------------------------- */
6777 +
6778 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6779 +{
6780 +       /*
6781 +        * This function is a dynamic '__fin' function actually,
6782 +        * so the tiny check for si_rwsem is unnecessary.
6783 +        */
6784 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6785 +
6786 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6787 +       sbinfo->si_dbgaufs = NULL;
6788 +}
6789 +
6790 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6791 +{
6792 +       int err;
6793 +       char name[SysaufsSiNameLen];
6794 +
6795 +       /*
6796 +        * This function is a dynamic '__init' function actually,
6797 +        * so the tiny check for si_rwsem is unnecessary.
6798 +        */
6799 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6800 +
6801 +       err = -ENOENT;
6802 +       if (!dbgaufs) {
6803 +               AuErr1("/debug/aufs is uninitialized\n");
6804 +               goto out;
6805 +       }
6806 +
6807 +       err = -EIO;
6808 +       sysaufs_name(sbinfo, name);
6809 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6810 +       if (unlikely(!sbinfo->si_dbgaufs))
6811 +               goto out;
6812 +
6813 +       /* regardless plink/noplink option */
6814 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6815 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6816 +                &dbgaufs_plink_fop);
6817 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6818 +               goto out_dir;
6819 +
6820 +       /* regardless xino/noxino option */
6821 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6822 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6823 +                &dbgaufs_xib_fop);
6824 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6825 +               goto out_dir;
6826 +
6827 +       err = dbgaufs_xigen_init(sbinfo);
6828 +       if (!err)
6829 +               goto out; /* success */
6830 +
6831 +out_dir:
6832 +       dbgaufs_si_fin(sbinfo);
6833 +out:
6834 +       if (unlikely(err))
6835 +               pr_err("debugfs/aufs failed\n");
6836 +       return err;
6837 +}
6838 +
6839 +/* ---------------------------------------------------------------------- */
6840 +
6841 +void dbgaufs_fin(void)
6842 +{
6843 +       debugfs_remove(dbgaufs);
6844 +}
6845 +
6846 +int __init dbgaufs_init(void)
6847 +{
6848 +       int err;
6849 +
6850 +       err = -EIO;
6851 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6852 +       if (dbgaufs)
6853 +               err = 0;
6854 +       return err;
6855 +}
6856 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6857 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6858 +++ linux/fs/aufs/dbgaufs.h     2022-11-05 23:02:18.962555950 +0100
6859 @@ -0,0 +1,53 @@
6860 +/* SPDX-License-Identifier: GPL-2.0 */
6861 +/*
6862 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6863 + *
6864 + * This program is free software; you can redistribute it and/or modify
6865 + * it under the terms of the GNU General Public License as published by
6866 + * the Free Software Foundation; either version 2 of the License, or
6867 + * (at your option) any later version.
6868 + *
6869 + * This program is distributed in the hope that it will be useful,
6870 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6871 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6872 + * GNU General Public License for more details.
6873 + *
6874 + * You should have received a copy of the GNU General Public License
6875 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6876 + */
6877 +
6878 +/*
6879 + * debugfs interface
6880 + */
6881 +
6882 +#ifndef __DBGAUFS_H__
6883 +#define __DBGAUFS_H__
6884 +
6885 +#ifdef __KERNEL__
6886 +
6887 +struct super_block;
6888 +struct au_sbinfo;
6889 +struct au_branch;
6890 +
6891 +#ifdef CONFIG_DEBUG_FS
6892 +/* dbgaufs.c */
6893 +void dbgaufs_xino_del(struct au_branch *br);
6894 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6895 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6896 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6897 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6898 +void dbgaufs_fin(void);
6899 +int __init dbgaufs_init(void);
6900 +#else
6901 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6902 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6903 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6904 +          int topdown)
6905 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6906 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6907 +AuStubVoid(dbgaufs_fin, void)
6908 +AuStubInt0(__init dbgaufs_init, void)
6909 +#endif /* CONFIG_DEBUG_FS */
6910 +
6911 +#endif /* __KERNEL__ */
6912 +#endif /* __DBGAUFS_H__ */
6913 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6914 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6915 +++ linux/fs/aufs/dcsub.c       2022-11-05 23:02:18.962555950 +0100
6916 @@ -0,0 +1,225 @@
6917 +// SPDX-License-Identifier: GPL-2.0
6918 +/*
6919 + * Copyright (C) 2005-2022 Junjiro R. Okajima
6920 + *
6921 + * This program is free software; you can redistribute it and/or modify
6922 + * it under the terms of the GNU General Public License as published by
6923 + * the Free Software Foundation; either version 2 of the License, or
6924 + * (at your option) any later version.
6925 + *
6926 + * This program is distributed in the hope that it will be useful,
6927 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6928 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6929 + * GNU General Public License for more details.
6930 + *
6931 + * You should have received a copy of the GNU General Public License
6932 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6933 + */
6934 +
6935 +/*
6936 + * sub-routines for dentry cache
6937 + */
6938 +
6939 +#include "aufs.h"
6940 +
6941 +static void au_dpage_free(struct au_dpage *dpage)
6942 +{
6943 +       int i;
6944 +       struct dentry **p;
6945 +
6946 +       p = dpage->dentries;
6947 +       for (i = 0; i < dpage->ndentry; i++)
6948 +               dput(*p++);
6949 +       free_page((unsigned long)dpage->dentries);
6950 +}
6951 +
6952 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6953 +{
6954 +       int err;
6955 +       void *p;
6956 +
6957 +       err = -ENOMEM;
6958 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6959 +       if (unlikely(!dpages->dpages))
6960 +               goto out;
6961 +
6962 +       p = (void *)__get_free_page(gfp);
6963 +       if (unlikely(!p))
6964 +               goto out_dpages;
6965 +
6966 +       dpages->dpages[0].ndentry = 0;
6967 +       dpages->dpages[0].dentries = p;
6968 +       dpages->ndpage = 1;
6969 +       return 0; /* success */
6970 +
6971 +out_dpages:
6972 +       au_kfree_try_rcu(dpages->dpages);
6973 +out:
6974 +       return err;
6975 +}
6976 +
6977 +void au_dpages_free(struct au_dcsub_pages *dpages)
6978 +{
6979 +       int i;
6980 +       struct au_dpage *p;
6981 +
6982 +       p = dpages->dpages;
6983 +       for (i = 0; i < dpages->ndpage; i++)
6984 +               au_dpage_free(p++);
6985 +       au_kfree_try_rcu(dpages->dpages);
6986 +}
6987 +
6988 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6989 +                           struct dentry *dentry, gfp_t gfp)
6990 +{
6991 +       int err, sz;
6992 +       struct au_dpage *dpage;
6993 +       void *p;
6994 +
6995 +       dpage = dpages->dpages + dpages->ndpage - 1;
6996 +       sz = PAGE_SIZE / sizeof(dentry);
6997 +       if (unlikely(dpage->ndentry >= sz)) {
6998 +               AuLabel(new dpage);
6999 +               err = -ENOMEM;
7000 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7001 +               p = au_kzrealloc(dpages->dpages, sz,
7002 +                                sz + sizeof(*dpages->dpages), gfp,
7003 +                                /*may_shrink*/0);
7004 +               if (unlikely(!p))
7005 +                       goto out;
7006 +
7007 +               dpages->dpages = p;
7008 +               dpage = dpages->dpages + dpages->ndpage;
7009 +               p = (void *)__get_free_page(gfp);
7010 +               if (unlikely(!p))
7011 +                       goto out;
7012 +
7013 +               dpage->ndentry = 0;
7014 +               dpage->dentries = p;
7015 +               dpages->ndpage++;
7016 +       }
7017 +
7018 +       AuDebugOn(au_dcount(dentry) <= 0);
7019 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7020 +       return 0; /* success */
7021 +
7022 +out:
7023 +       return err;
7024 +}
7025 +
7026 +/* todo: BAD approach */
7027 +/* copied from linux/fs/dcache.c */
7028 +enum d_walk_ret {
7029 +       D_WALK_CONTINUE,
7030 +       D_WALK_QUIT,
7031 +       D_WALK_NORETRY,
7032 +       D_WALK_SKIP,
7033 +};
7034 +
7035 +extern void d_walk(struct dentry *parent, void *data,
7036 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7037 +
7038 +struct ac_dpages_arg {
7039 +       int err;
7040 +       struct au_dcsub_pages *dpages;
7041 +       struct super_block *sb;
7042 +       au_dpages_test test;
7043 +       void *arg;
7044 +};
7045 +
7046 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7047 +{
7048 +       enum d_walk_ret ret;
7049 +       struct ac_dpages_arg *arg = _arg;
7050 +
7051 +       ret = D_WALK_CONTINUE;
7052 +       if (dentry->d_sb == arg->sb
7053 +           && !IS_ROOT(dentry)
7054 +           && au_dcount(dentry) > 0
7055 +           && au_di(dentry)
7056 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7057 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7058 +               if (unlikely(arg->err))
7059 +                       ret = D_WALK_QUIT;
7060 +       }
7061 +
7062 +       return ret;
7063 +}
7064 +
7065 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7066 +                  au_dpages_test test, void *arg)
7067 +{
7068 +       struct ac_dpages_arg args = {
7069 +               .err    = 0,
7070 +               .dpages = dpages,
7071 +               .sb     = root->d_sb,
7072 +               .test   = test,
7073 +               .arg    = arg
7074 +       };
7075 +
7076 +       d_walk(root, &args, au_call_dpages_append);
7077 +
7078 +       return args.err;
7079 +}
7080 +
7081 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7082 +                      int do_include, au_dpages_test test, void *arg)
7083 +{
7084 +       int err;
7085 +
7086 +       err = 0;
7087 +       write_seqlock(&rename_lock);
7088 +       spin_lock(&dentry->d_lock);
7089 +       if (do_include
7090 +           && 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 +               goto out;
7096 +
7097 +       /*
7098 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7099 +        * mount
7100 +        */
7101 +       while (!IS_ROOT(dentry)) {
7102 +               dentry = dentry->d_parent; /* rename_lock is locked */
7103 +               spin_lock(&dentry->d_lock);
7104 +               if (au_dcount(dentry) > 0
7105 +                   && (!test || test(dentry, arg)))
7106 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7107 +               spin_unlock(&dentry->d_lock);
7108 +               if (unlikely(err))
7109 +                       break;
7110 +       }
7111 +
7112 +out:
7113 +       write_sequnlock(&rename_lock);
7114 +       return err;
7115 +}
7116 +
7117 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7118 +{
7119 +       return au_di(dentry) && dentry->d_sb == arg;
7120 +}
7121 +
7122 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7123 +                           struct dentry *dentry, int do_include)
7124 +{
7125 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7126 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7127 +}
7128 +
7129 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7130 +{
7131 +       struct path path[2] = {
7132 +               {
7133 +                       .dentry = d1
7134 +               },
7135 +               {
7136 +                       .dentry = d2
7137 +               }
7138 +       };
7139 +
7140 +       return path_is_under(path + 0, path + 1);
7141 +}
7142 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7143 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7144 +++ linux/fs/aufs/dcsub.h       2022-11-05 23:02:18.962555950 +0100
7145 @@ -0,0 +1,137 @@
7146 +/* SPDX-License-Identifier: GPL-2.0 */
7147 +/*
7148 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7149 + *
7150 + * This program is free software; you can redistribute it and/or modify
7151 + * it under the terms of the GNU General Public License as published by
7152 + * the Free Software Foundation; either version 2 of the License, or
7153 + * (at your option) any later version.
7154 + *
7155 + * This program is distributed in the hope that it will be useful,
7156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7158 + * GNU General Public License for more details.
7159 + *
7160 + * You should have received a copy of the GNU General Public License
7161 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7162 + */
7163 +
7164 +/*
7165 + * sub-routines for dentry cache
7166 + */
7167 +
7168 +#ifndef __AUFS_DCSUB_H__
7169 +#define __AUFS_DCSUB_H__
7170 +
7171 +#ifdef __KERNEL__
7172 +
7173 +#include <linux/dcache.h>
7174 +#include <linux/fs.h>
7175 +
7176 +struct au_dpage {
7177 +       int ndentry;
7178 +       struct dentry **dentries;
7179 +};
7180 +
7181 +struct au_dcsub_pages {
7182 +       int ndpage;
7183 +       struct au_dpage *dpages;
7184 +};
7185 +
7186 +/* ---------------------------------------------------------------------- */
7187 +
7188 +/* dcsub.c */
7189 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7190 +void au_dpages_free(struct au_dcsub_pages *dpages);
7191 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7192 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7193 +                  au_dpages_test test, void *arg);
7194 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7195 +                      int do_include, au_dpages_test test, void *arg);
7196 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7197 +                           struct dentry *dentry, int do_include);
7198 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7199 +
7200 +/* ---------------------------------------------------------------------- */
7201 +
7202 +/*
7203 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7204 + * include/linux/dcache.h. Try them (in the future).
7205 + */
7206 +
7207 +static inline int au_d_hashed_positive(struct dentry *d)
7208 +{
7209 +       int err;
7210 +       struct inode *inode = d_inode(d);
7211 +
7212 +       err = 0;
7213 +       if (unlikely(d_unhashed(d)
7214 +                    || d_is_negative(d)
7215 +                    || !inode->i_nlink))
7216 +               err = -ENOENT;
7217 +       return err;
7218 +}
7219 +
7220 +static inline int au_d_linkable(struct dentry *d)
7221 +{
7222 +       int err;
7223 +       struct inode *inode = d_inode(d);
7224 +
7225 +       err = au_d_hashed_positive(d);
7226 +       if (err
7227 +           && d_is_positive(d)
7228 +           && (inode->i_state & I_LINKABLE))
7229 +               err = 0;
7230 +       return err;
7231 +}
7232 +
7233 +static inline int au_d_alive(struct dentry *d)
7234 +{
7235 +       int err;
7236 +       struct inode *inode;
7237 +
7238 +       err = 0;
7239 +       if (!IS_ROOT(d))
7240 +               err = au_d_hashed_positive(d);
7241 +       else {
7242 +               inode = d_inode(d);
7243 +               if (unlikely(d_unlinked(d)
7244 +                            || d_is_negative(d)
7245 +                            || !inode->i_nlink))
7246 +                       err = -ENOENT;
7247 +       }
7248 +       return err;
7249 +}
7250 +
7251 +static inline int au_alive_dir(struct dentry *d)
7252 +{
7253 +       int err;
7254 +
7255 +       err = au_d_alive(d);
7256 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7257 +               err = -ENOENT;
7258 +       return err;
7259 +}
7260 +
7261 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7262 +{
7263 +       return a->len == b->len
7264 +               && !memcmp(a->name, b->name, a->len);
7265 +}
7266 +
7267 +/*
7268 + * by the commit
7269 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7270 + *                     taking d_lock
7271 + * the type of d_lockref.count became int, but the inlined function d_count()
7272 + * still returns unsigned int.
7273 + * I don't know why. Maybe it is for every d_count() users?
7274 + * Anyway au_dcount() lives on.
7275 + */
7276 +static inline int au_dcount(struct dentry *d)
7277 +{
7278 +       return (int)d_count(d);
7279 +}
7280 +
7281 +#endif /* __KERNEL__ */
7282 +#endif /* __AUFS_DCSUB_H__ */
7283 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7284 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7285 +++ linux/fs/aufs/debug.c       2022-11-05 23:02:18.962555950 +0100
7286 @@ -0,0 +1,444 @@
7287 +// SPDX-License-Identifier: GPL-2.0
7288 +/*
7289 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7290 + *
7291 + * This program is free software; you can redistribute it and/or modify
7292 + * it under the terms of the GNU General Public License as published by
7293 + * the Free Software Foundation; either version 2 of the License, or
7294 + * (at your option) any later version.
7295 + *
7296 + * This program is distributed in the hope that it will be useful,
7297 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7298 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7299 + * GNU General Public License for more details.
7300 + *
7301 + * You should have received a copy of the GNU General Public License
7302 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7303 + */
7304 +
7305 +/*
7306 + * debug print functions
7307 + */
7308 +
7309 +#include <linux/iversion.h>
7310 +#include "aufs.h"
7311 +
7312 +/* Returns 0, or -errno.  arg is in kp->arg. */
7313 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7314 +{
7315 +       int err, n;
7316 +
7317 +       err = kstrtoint(val, 0, &n);
7318 +       if (!err) {
7319 +               if (n > 0)
7320 +                       au_debug_on();
7321 +               else
7322 +                       au_debug_off();
7323 +       }
7324 +       return err;
7325 +}
7326 +
7327 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7328 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7329 +{
7330 +       atomic_t *a;
7331 +
7332 +       a = kp->arg;
7333 +       return sprintf(buffer, "%d", atomic_read(a));
7334 +}
7335 +
7336 +static const struct kernel_param_ops param_ops_atomic_t = {
7337 +       .set = param_atomic_t_set,
7338 +       .get = param_atomic_t_get
7339 +       /* void (*free)(void *arg) */
7340 +};
7341 +
7342 +atomic_t aufs_debug = ATOMIC_INIT(0);
7343 +MODULE_PARM_DESC(debug, "debug print");
7344 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7345 +
7346 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7347 +char *au_plevel = KERN_DEBUG;
7348 +#define dpri(fmt, ...) do {                                    \
7349 +       if ((au_plevel                                          \
7350 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7351 +           || au_debug_test())                                 \
7352 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7353 +} while (0)
7354 +
7355 +/* ---------------------------------------------------------------------- */
7356 +
7357 +void au_dpri_whlist(struct au_nhash *whlist)
7358 +{
7359 +       unsigned long ul, n;
7360 +       struct hlist_head *head;
7361 +       struct au_vdir_wh *pos;
7362 +
7363 +       n = whlist->nh_num;
7364 +       head = whlist->nh_head;
7365 +       for (ul = 0; ul < n; ul++) {
7366 +               hlist_for_each_entry(pos, head, wh_hash)
7367 +                       dpri("b%d, %.*s, %d\n",
7368 +                            pos->wh_bindex,
7369 +                            pos->wh_str.len, pos->wh_str.name,
7370 +                            pos->wh_str.len);
7371 +               head++;
7372 +       }
7373 +}
7374 +
7375 +void au_dpri_vdir(struct au_vdir *vdir)
7376 +{
7377 +       unsigned long ul;
7378 +       union au_vdir_deblk_p p;
7379 +       unsigned char *o;
7380 +
7381 +       if (!vdir || IS_ERR(vdir)) {
7382 +               dpri("err %ld\n", PTR_ERR(vdir));
7383 +               return;
7384 +       }
7385 +
7386 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7387 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7388 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7389 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7390 +               p.deblk = vdir->vd_deblk[ul];
7391 +               o = p.deblk;
7392 +               dpri("[%lu]: %p\n", ul, o);
7393 +       }
7394 +}
7395 +
7396 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7397 +                       struct dentry *wh)
7398 +{
7399 +       char *n = NULL;
7400 +       int l = 0;
7401 +
7402 +       if (!inode || IS_ERR(inode)) {
7403 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7404 +               return -1;
7405 +       }
7406 +
7407 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7408 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7409 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7410 +       if (wh) {
7411 +               n = (void *)wh->d_name.name;
7412 +               l = wh->d_name.len;
7413 +       }
7414 +
7415 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7416 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7417 +            bindex, inode,
7418 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7419 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7420 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7421 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7422 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7423 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7424 +            inode->i_generation,
7425 +            l ? ", wh " : "", l, n);
7426 +       return 0;
7427 +}
7428 +
7429 +void au_dpri_inode(struct inode *inode)
7430 +{
7431 +       struct au_iinfo *iinfo;
7432 +       struct au_hinode *hi;
7433 +       aufs_bindex_t bindex;
7434 +       int err, hn;
7435 +
7436 +       err = do_pri_inode(-1, inode, -1, NULL);
7437 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7438 +               return;
7439 +
7440 +       iinfo = au_ii(inode);
7441 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7442 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7443 +       if (iinfo->ii_btop < 0)
7444 +               return;
7445 +       hn = 0;
7446 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7447 +               hi = au_hinode(iinfo, bindex);
7448 +               hn = !!au_hn(hi);
7449 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7450 +       }
7451 +}
7452 +
7453 +void au_dpri_dalias(struct inode *inode)
7454 +{
7455 +       struct dentry *d;
7456 +
7457 +       spin_lock(&inode->i_lock);
7458 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7459 +               au_dpri_dentry(d);
7460 +       spin_unlock(&inode->i_lock);
7461 +}
7462 +
7463 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7464 +{
7465 +       struct dentry *wh = NULL;
7466 +       int hn;
7467 +       struct inode *inode;
7468 +       struct au_iinfo *iinfo;
7469 +       struct au_hinode *hi;
7470 +
7471 +       if (!dentry || IS_ERR(dentry)) {
7472 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7473 +               return -1;
7474 +       }
7475 +       /* do not call dget_parent() here */
7476 +       /* note: access d_xxx without d_lock */
7477 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7478 +            bindex, dentry, dentry,
7479 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7480 +            au_dcount(dentry), dentry->d_flags,
7481 +            d_unhashed(dentry) ? "un" : "");
7482 +       hn = -1;
7483 +       inode = NULL;
7484 +       if (d_is_positive(dentry))
7485 +               inode = d_inode(dentry);
7486 +       if (inode
7487 +           && au_test_aufs(dentry->d_sb)
7488 +           && bindex >= 0
7489 +           && !au_is_bad_inode(inode)) {
7490 +               iinfo = au_ii(inode);
7491 +               hi = au_hinode(iinfo, bindex);
7492 +               hn = !!au_hn(hi);
7493 +               wh = hi->hi_whdentry;
7494 +       }
7495 +       do_pri_inode(bindex, inode, hn, wh);
7496 +       return 0;
7497 +}
7498 +
7499 +void au_dpri_dentry(struct dentry *dentry)
7500 +{
7501 +       struct au_dinfo *dinfo;
7502 +       aufs_bindex_t bindex;
7503 +       int err;
7504 +
7505 +       err = do_pri_dentry(-1, dentry);
7506 +       if (err || !au_test_aufs(dentry->d_sb))
7507 +               return;
7508 +
7509 +       dinfo = au_di(dentry);
7510 +       if (!dinfo)
7511 +               return;
7512 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7513 +            dinfo->di_btop, dinfo->di_bbot,
7514 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7515 +            dinfo->di_tmpfile);
7516 +       if (dinfo->di_btop < 0)
7517 +               return;
7518 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7519 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7520 +}
7521 +
7522 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7523 +{
7524 +       char a[32];
7525 +
7526 +       if (!file || IS_ERR(file)) {
7527 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7528 +               return -1;
7529 +       }
7530 +       a[0] = 0;
7531 +       if (bindex < 0
7532 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7533 +           && au_test_aufs(file->f_path.dentry->d_sb)
7534 +           && au_fi(file))
7535 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7536 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7537 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7538 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7539 +            file->f_version, file->f_pos, a);
7540 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7541 +               do_pri_dentry(bindex, file->f_path.dentry);
7542 +       return 0;
7543 +}
7544 +
7545 +void au_dpri_file(struct file *file)
7546 +{
7547 +       struct au_finfo *finfo;
7548 +       struct au_fidir *fidir;
7549 +       struct au_hfile *hfile;
7550 +       aufs_bindex_t bindex;
7551 +       int err;
7552 +
7553 +       err = do_pri_file(-1, file);
7554 +       if (err
7555 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7556 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7557 +               return;
7558 +
7559 +       finfo = au_fi(file);
7560 +       if (!finfo)
7561 +               return;
7562 +       if (finfo->fi_btop < 0)
7563 +               return;
7564 +       fidir = finfo->fi_hdir;
7565 +       if (!fidir)
7566 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7567 +       else
7568 +               for (bindex = finfo->fi_btop;
7569 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7570 +                    bindex++) {
7571 +                       hfile = fidir->fd_hfile + bindex;
7572 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7573 +               }
7574 +}
7575 +
7576 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7577 +{
7578 +       struct vfsmount *mnt;
7579 +       struct super_block *sb;
7580 +
7581 +       if (!br || IS_ERR(br))
7582 +               goto out;
7583 +       mnt = au_br_mnt(br);
7584 +       if (!mnt || IS_ERR(mnt))
7585 +               goto out;
7586 +       sb = mnt->mnt_sb;
7587 +       if (!sb || IS_ERR(sb))
7588 +               goto out;
7589 +
7590 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7591 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7592 +            "xino %d\n",
7593 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7594 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7595 +            sb->s_flags, sb->s_count,
7596 +            atomic_read(&sb->s_active),
7597 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7598 +       return 0;
7599 +
7600 +out:
7601 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7602 +       return -1;
7603 +}
7604 +
7605 +void au_dpri_sb(struct super_block *sb)
7606 +{
7607 +       struct au_sbinfo *sbinfo;
7608 +       aufs_bindex_t bindex;
7609 +       int err;
7610 +       /* to reduce stack size */
7611 +       struct {
7612 +               struct vfsmount mnt;
7613 +               struct au_branch fake;
7614 +       } *a;
7615 +
7616 +       /* this function can be called from magic sysrq */
7617 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7618 +       if (unlikely(!a)) {
7619 +               dpri("no memory\n");
7620 +               return;
7621 +       }
7622 +
7623 +       a->mnt.mnt_sb = sb;
7624 +       a->fake.br_path.mnt = &a->mnt;
7625 +       err = do_pri_br(-1, &a->fake);
7626 +       au_kfree_rcu(a);
7627 +       dpri("dev 0x%x\n", sb->s_dev);
7628 +       if (err || !au_test_aufs(sb))
7629 +               return;
7630 +
7631 +       sbinfo = au_sbi(sb);
7632 +       if (!sbinfo)
7633 +               return;
7634 +       dpri("nw %d, gen %u, kobj %d\n",
7635 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7636 +            kref_read(&sbinfo->si_kobj.kref));
7637 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7638 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7639 +}
7640 +
7641 +/* ---------------------------------------------------------------------- */
7642 +
7643 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7644 +{
7645 +       struct inode *h_inode, *inode = d_inode(dentry);
7646 +       struct dentry *h_dentry;
7647 +       aufs_bindex_t bindex, bbot, bi;
7648 +
7649 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7650 +               return;
7651 +
7652 +       bbot = au_dbbot(dentry);
7653 +       bi = au_ibbot(inode);
7654 +       if (bi < bbot)
7655 +               bbot = bi;
7656 +       bindex = au_dbtop(dentry);
7657 +       bi = au_ibtop(inode);
7658 +       if (bi > bindex)
7659 +               bindex = bi;
7660 +
7661 +       for (; bindex <= bbot; bindex++) {
7662 +               h_dentry = au_h_dptr(dentry, bindex);
7663 +               if (!h_dentry)
7664 +                       continue;
7665 +               h_inode = au_h_iptr(inode, bindex);
7666 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7667 +                       au_debug_on();
7668 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7669 +                       AuDbgDentry(dentry);
7670 +                       AuDbgInode(inode);
7671 +                       au_debug_off();
7672 +                       if (au_test_fuse(h_inode->i_sb))
7673 +                               WARN_ON_ONCE(1);
7674 +                       else
7675 +                               BUG();
7676 +               }
7677 +       }
7678 +}
7679 +
7680 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7681 +{
7682 +       int err, i, j;
7683 +       struct au_dcsub_pages dpages;
7684 +       struct au_dpage *dpage;
7685 +       struct dentry **dentries;
7686 +
7687 +       err = au_dpages_init(&dpages, GFP_NOFS);
7688 +       AuDebugOn(err);
7689 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7690 +       AuDebugOn(err);
7691 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7692 +               dpage = dpages.dpages + i;
7693 +               dentries = dpage->dentries;
7694 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7695 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7696 +       }
7697 +       au_dpages_free(&dpages);
7698 +}
7699 +
7700 +void au_dbg_verify_kthread(void)
7701 +{
7702 +       if (au_wkq_test()) {
7703 +               au_dbg_blocked();
7704 +               /*
7705 +                * It may be recursive, but udba=notify between two aufs mounts,
7706 +                * where a single ro branch is shared, is not a problem.
7707 +                */
7708 +               /* WARN_ON(1); */
7709 +       }
7710 +}
7711 +
7712 +/* ---------------------------------------------------------------------- */
7713 +
7714 +int __init au_debug_init(void)
7715 +{
7716 +       aufs_bindex_t bindex;
7717 +       struct au_vdir_destr destr;
7718 +
7719 +       bindex = -1;
7720 +       AuDebugOn(bindex >= 0);
7721 +
7722 +       destr.len = -1;
7723 +       AuDebugOn(destr.len < NAME_MAX);
7724 +
7725 +#ifdef CONFIG_4KSTACKS
7726 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7727 +#endif
7728 +
7729 +       return 0;
7730 +}
7731 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7732 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7733 +++ linux/fs/aufs/debug.h       2022-11-05 23:02:18.962555950 +0100
7734 @@ -0,0 +1,226 @@
7735 +/* SPDX-License-Identifier: GPL-2.0 */
7736 +/*
7737 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7738 + *
7739 + * This program is free software; you can redistribute it and/or modify
7740 + * it under the terms of the GNU General Public License as published by
7741 + * the Free Software Foundation; either version 2 of the License, or
7742 + * (at your option) any later version.
7743 + *
7744 + * This program is distributed in the hope that it will be useful,
7745 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7746 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7747 + * GNU General Public License for more details.
7748 + *
7749 + * You should have received a copy of the GNU General Public License
7750 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7751 + */
7752 +
7753 +/*
7754 + * debug print functions
7755 + */
7756 +
7757 +#ifndef __AUFS_DEBUG_H__
7758 +#define __AUFS_DEBUG_H__
7759 +
7760 +#ifdef __KERNEL__
7761 +
7762 +#include <linux/atomic.h>
7763 +#include <linux/module.h>
7764 +#include <linux/kallsyms.h>
7765 +#include <linux/sysrq.h>
7766 +
7767 +#ifdef CONFIG_AUFS_DEBUG
7768 +#define AuDebugOn(a)           BUG_ON(a)
7769 +
7770 +/* module parameter */
7771 +extern atomic_t aufs_debug;
7772 +static inline void au_debug_on(void)
7773 +{
7774 +       atomic_inc(&aufs_debug);
7775 +}
7776 +static inline void au_debug_off(void)
7777 +{
7778 +       atomic_dec_if_positive(&aufs_debug);
7779 +}
7780 +
7781 +static inline int au_debug_test(void)
7782 +{
7783 +       return atomic_read(&aufs_debug) > 0;
7784 +}
7785 +#else
7786 +#define AuDebugOn(a)           do {} while (0)
7787 +AuStubVoid(au_debug_on, void)
7788 +AuStubVoid(au_debug_off, void)
7789 +AuStubInt0(au_debug_test, void)
7790 +#endif /* CONFIG_AUFS_DEBUG */
7791 +
7792 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7793 +
7794 +/* ---------------------------------------------------------------------- */
7795 +
7796 +/* debug print */
7797 +
7798 +#define AuDbg(fmt, ...) do { \
7799 +       if (au_debug_test()) \
7800 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7801 +} while (0)
7802 +#define AuLabel(l)             AuDbg(#l "\n")
7803 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7804 +#define AuWarn1(fmt, ...) do { \
7805 +       static unsigned char _c; \
7806 +       if (!_c++) \
7807 +               pr_warn(fmt, ##__VA_ARGS__); \
7808 +} while (0)
7809 +
7810 +#define AuErr1(fmt, ...) do { \
7811 +       static unsigned char _c; \
7812 +       if (!_c++) \
7813 +               pr_err(fmt, ##__VA_ARGS__); \
7814 +} while (0)
7815 +
7816 +#define AuIOErr1(fmt, ...) do { \
7817 +       static unsigned char _c; \
7818 +       if (!_c++) \
7819 +               AuIOErr(fmt, ##__VA_ARGS__); \
7820 +} while (0)
7821 +
7822 +#define AuUnsupportMsg "This operation is not supported." \
7823 +                       " Please report this application to aufs-users ML."
7824 +#define AuUnsupport(fmt, ...) do { \
7825 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7826 +       dump_stack(); \
7827 +} while (0)
7828 +
7829 +#define AuTraceErr(e) do { \
7830 +       if (unlikely((e) < 0)) \
7831 +               AuDbg("err %d\n", (int)(e)); \
7832 +} while (0)
7833 +
7834 +#define AuTraceErrPtr(p) do { \
7835 +       if (IS_ERR(p)) \
7836 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7837 +} while (0)
7838 +
7839 +/* dirty macros for debug print, use with "%.*s" and caution */
7840 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7841 +
7842 +/* ---------------------------------------------------------------------- */
7843 +
7844 +struct dentry;
7845 +#ifdef CONFIG_AUFS_DEBUG
7846 +extern struct mutex au_dbg_mtx;
7847 +extern char *au_plevel;
7848 +struct au_nhash;
7849 +void au_dpri_whlist(struct au_nhash *whlist);
7850 +struct au_vdir;
7851 +void au_dpri_vdir(struct au_vdir *vdir);
7852 +struct inode;
7853 +void au_dpri_inode(struct inode *inode);
7854 +void au_dpri_dalias(struct inode *inode);
7855 +void au_dpri_dentry(struct dentry *dentry);
7856 +struct file;
7857 +void au_dpri_file(struct file *filp);
7858 +struct super_block;
7859 +void au_dpri_sb(struct super_block *sb);
7860 +
7861 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7862 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7863 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7864 +void au_dbg_verify_kthread(void);
7865 +
7866 +int __init au_debug_init(void);
7867 +
7868 +#define AuDbgWhlist(w) do { \
7869 +       mutex_lock(&au_dbg_mtx); \
7870 +       AuDbg(#w "\n"); \
7871 +       au_dpri_whlist(w); \
7872 +       mutex_unlock(&au_dbg_mtx); \
7873 +} while (0)
7874 +
7875 +#define AuDbgVdir(v) do { \
7876 +       mutex_lock(&au_dbg_mtx); \
7877 +       AuDbg(#v "\n"); \
7878 +       au_dpri_vdir(v); \
7879 +       mutex_unlock(&au_dbg_mtx); \
7880 +} while (0)
7881 +
7882 +#define AuDbgInode(i) do { \
7883 +       mutex_lock(&au_dbg_mtx); \
7884 +       AuDbg(#i "\n"); \
7885 +       au_dpri_inode(i); \
7886 +       mutex_unlock(&au_dbg_mtx); \
7887 +} while (0)
7888 +
7889 +#define AuDbgDAlias(i) do { \
7890 +       mutex_lock(&au_dbg_mtx); \
7891 +       AuDbg(#i "\n"); \
7892 +       au_dpri_dalias(i); \
7893 +       mutex_unlock(&au_dbg_mtx); \
7894 +} while (0)
7895 +
7896 +#define AuDbgDentry(d) do { \
7897 +       mutex_lock(&au_dbg_mtx); \
7898 +       AuDbg(#d "\n"); \
7899 +       au_dpri_dentry(d); \
7900 +       mutex_unlock(&au_dbg_mtx); \
7901 +} while (0)
7902 +
7903 +#define AuDbgFile(f) do { \
7904 +       mutex_lock(&au_dbg_mtx); \
7905 +       AuDbg(#f "\n"); \
7906 +       au_dpri_file(f); \
7907 +       mutex_unlock(&au_dbg_mtx); \
7908 +} while (0)
7909 +
7910 +#define AuDbgSb(sb) do { \
7911 +       mutex_lock(&au_dbg_mtx); \
7912 +       AuDbg(#sb "\n"); \
7913 +       au_dpri_sb(sb); \
7914 +       mutex_unlock(&au_dbg_mtx); \
7915 +} while (0)
7916 +
7917 +#define AuDbgSym(addr) do {                            \
7918 +       char sym[KSYM_SYMBOL_LEN];                      \
7919 +       sprint_symbol(sym, (unsigned long)addr);        \
7920 +       AuDbg("%s\n", sym);                             \
7921 +} while (0)
7922 +#else
7923 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7924 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7925 +AuStubVoid(au_dbg_verify_kthread, void)
7926 +AuStubInt0(__init au_debug_init, void)
7927 +
7928 +#define AuDbgWhlist(w)         do {} while (0)
7929 +#define AuDbgVdir(v)           do {} while (0)
7930 +#define AuDbgInode(i)          do {} while (0)
7931 +#define AuDbgDAlias(i)         do {} while (0)
7932 +#define AuDbgDentry(d)         do {} while (0)
7933 +#define AuDbgFile(f)           do {} while (0)
7934 +#define AuDbgSb(sb)            do {} while (0)
7935 +#define AuDbgSym(addr)         do {} while (0)
7936 +#endif /* CONFIG_AUFS_DEBUG */
7937 +
7938 +/* ---------------------------------------------------------------------- */
7939 +
7940 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7941 +int __init au_sysrq_init(void);
7942 +void au_sysrq_fin(void);
7943 +
7944 +#ifdef CONFIG_HW_CONSOLE
7945 +#define au_dbg_blocked() do { \
7946 +       WARN_ON(1); \
7947 +       handle_sysrq('w'); \
7948 +} while (0)
7949 +#else
7950 +AuStubVoid(au_dbg_blocked, void)
7951 +#endif
7952 +
7953 +#else
7954 +AuStubInt0(__init au_sysrq_init, void)
7955 +AuStubVoid(au_sysrq_fin, void)
7956 +AuStubVoid(au_dbg_blocked, void)
7957 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7958 +
7959 +#endif /* __KERNEL__ */
7960 +#endif /* __AUFS_DEBUG_H__ */
7961 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7962 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7963 +++ linux/fs/aufs/dentry.c      2022-11-05 23:02:18.962555950 +0100
7964 @@ -0,0 +1,1168 @@
7965 +// SPDX-License-Identifier: GPL-2.0
7966 +/*
7967 + * Copyright (C) 2005-2022 Junjiro R. Okajima
7968 + *
7969 + * This program is free software; you can redistribute it and/or modify
7970 + * it under the terms of the GNU General Public License as published by
7971 + * the Free Software Foundation; either version 2 of the License, or
7972 + * (at your option) any later version.
7973 + *
7974 + * This program is distributed in the hope that it will be useful,
7975 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7976 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7977 + * GNU General Public License for more details.
7978 + *
7979 + * You should have received a copy of the GNU General Public License
7980 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7981 + */
7982 +
7983 +/*
7984 + * lookup and dentry operations
7985 + */
7986 +
7987 +#include <linux/iversion.h>
7988 +#include "aufs.h"
7989 +
7990 +/*
7991 + * returns positive/negative dentry, NULL or an error.
7992 + * NULL means whiteout-ed or not-found.
7993 + */
7994 +static struct dentry*
7995 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7996 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7997 +{
7998 +       struct dentry *h_dentry;
7999 +       struct inode *h_inode;
8000 +       struct au_branch *br;
8001 +       struct user_namespace *h_userns;
8002 +       struct path h_path;
8003 +       int wh_found, opq;
8004 +       unsigned char wh_able;
8005 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8006 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8007 +                                                         IGNORE_PERM);
8008 +
8009 +       wh_found = 0;
8010 +       br = au_sbr(dentry->d_sb, bindex);
8011 +       h_path.dentry = h_parent;
8012 +       h_path.mnt = au_br_mnt(br);
8013 +       h_userns = au_br_userns(br);
8014 +       wh_able = !!au_br_whable(br->br_perm);
8015 +       if (wh_able)
8016 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
8017 +                                     ignore_perm);
8018 +       h_dentry = ERR_PTR(wh_found);
8019 +       if (!wh_found)
8020 +               goto real_lookup;
8021 +       if (unlikely(wh_found < 0))
8022 +               goto out;
8023 +
8024 +       /* We found a whiteout */
8025 +       /* au_set_dbbot(dentry, bindex); */
8026 +       au_set_dbwh(dentry, bindex);
8027 +       if (!allow_neg)
8028 +               return NULL; /* success */
8029 +
8030 +real_lookup:
8031 +       if (!ignore_perm)
8032 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8033 +       else
8034 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8035 +       if (IS_ERR(h_dentry)) {
8036 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8037 +                   && !allow_neg)
8038 +                       h_dentry = NULL;
8039 +               goto out;
8040 +       }
8041 +
8042 +       h_inode = d_inode(h_dentry);
8043 +       if (d_is_negative(h_dentry)) {
8044 +               if (!allow_neg)
8045 +                       goto out_neg;
8046 +       } else if (wh_found
8047 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8048 +               goto out_neg;
8049 +       else if (au_ftest_lkup(args->flags, DIRREN)
8050 +                /* && h_inode */
8051 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8052 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8053 +                     (unsigned long long)h_inode->i_ino);
8054 +               goto out_neg;
8055 +       }
8056 +
8057 +       if (au_dbbot(dentry) <= bindex)
8058 +               au_set_dbbot(dentry, bindex);
8059 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8060 +               au_set_dbtop(dentry, bindex);
8061 +       au_set_h_dptr(dentry, bindex, h_dentry);
8062 +
8063 +       if (!d_is_dir(h_dentry)
8064 +           || !wh_able
8065 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8066 +               goto out; /* success */
8067 +
8068 +       h_path.dentry = h_dentry;
8069 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8070 +       opq = au_diropq_test(h_userns, &h_path);
8071 +       inode_unlock_shared(h_inode);
8072 +       if (opq > 0)
8073 +               au_set_dbdiropq(dentry, bindex);
8074 +       else if (unlikely(opq < 0)) {
8075 +               au_set_h_dptr(dentry, bindex, NULL);
8076 +               h_dentry = ERR_PTR(opq);
8077 +       }
8078 +       goto out;
8079 +
8080 +out_neg:
8081 +       dput(h_dentry);
8082 +       h_dentry = NULL;
8083 +out:
8084 +       return h_dentry;
8085 +}
8086 +
8087 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8088 +{
8089 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8090 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8091 +               return -EPERM;
8092 +       return 0;
8093 +}
8094 +
8095 +/*
8096 + * returns the number of lower positive dentries,
8097 + * otherwise an error.
8098 + * can be called at unlinking with @type is zero.
8099 + */
8100 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8101 +                  unsigned int flags)
8102 +{
8103 +       int npositive, err;
8104 +       aufs_bindex_t bindex, btail, bdiropq;
8105 +       unsigned char isdir, dirperm1, dirren;
8106 +       struct au_do_lookup_args args = {
8107 +               .flags          = flags,
8108 +               .name           = &dentry->d_name
8109 +       };
8110 +       struct dentry *parent;
8111 +       struct super_block *sb;
8112 +
8113 +       sb = dentry->d_sb;
8114 +       err = au_test_shwh(sb, args.name);
8115 +       if (unlikely(err))
8116 +               goto out;
8117 +
8118 +       err = au_wh_name_alloc(&args.whname, args.name);
8119 +       if (unlikely(err))
8120 +               goto out;
8121 +
8122 +       isdir = !!d_is_dir(dentry);
8123 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8124 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8125 +       if (dirren)
8126 +               au_fset_lkup(args.flags, DIRREN);
8127 +
8128 +       npositive = 0;
8129 +       parent = dget_parent(dentry);
8130 +       btail = au_dbtaildir(parent);
8131 +       for (bindex = btop; bindex <= btail; bindex++) {
8132 +               struct dentry *h_parent, *h_dentry;
8133 +               struct inode *h_inode, *h_dir;
8134 +               struct au_branch *br;
8135 +
8136 +               h_dentry = au_h_dptr(dentry, bindex);
8137 +               if (h_dentry) {
8138 +                       if (d_is_positive(h_dentry))
8139 +                               npositive++;
8140 +                       break;
8141 +               }
8142 +               h_parent = au_h_dptr(parent, bindex);
8143 +               if (!h_parent || !d_is_dir(h_parent))
8144 +                       continue;
8145 +
8146 +               if (dirren) {
8147 +                       /* if the inum matches, then use the prepared name */
8148 +                       err = au_dr_lkup_name(&args, bindex);
8149 +                       if (unlikely(err))
8150 +                               goto out_parent;
8151 +               }
8152 +
8153 +               h_dir = d_inode(h_parent);
8154 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8155 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8156 +               inode_unlock_shared(h_dir);
8157 +               err = PTR_ERR(h_dentry);
8158 +               if (IS_ERR(h_dentry))
8159 +                       goto out_parent;
8160 +               if (h_dentry)
8161 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8162 +               if (dirperm1)
8163 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8164 +
8165 +               if (au_dbwh(dentry) == bindex)
8166 +                       break;
8167 +               if (!h_dentry)
8168 +                       continue;
8169 +               if (d_is_negative(h_dentry))
8170 +                       continue;
8171 +               h_inode = d_inode(h_dentry);
8172 +               npositive++;
8173 +               if (!args.type)
8174 +                       args.type = h_inode->i_mode & S_IFMT;
8175 +               if (args.type != S_IFDIR)
8176 +                       break;
8177 +               else if (isdir) {
8178 +                       /* the type of lower may be different */
8179 +                       bdiropq = au_dbdiropq(dentry);
8180 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8181 +                               break;
8182 +               }
8183 +               br = au_sbr(sb, bindex);
8184 +               if (dirren
8185 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8186 +                                          /*add_ent*/NULL)) {
8187 +                       /* prepare next name to lookup */
8188 +                       err = au_dr_lkup(&args, dentry, bindex);
8189 +                       if (unlikely(err))
8190 +                               goto out_parent;
8191 +               }
8192 +       }
8193 +
8194 +       if (npositive) {
8195 +               AuLabel(positive);
8196 +               au_update_dbtop(dentry);
8197 +       }
8198 +       err = npositive;
8199 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8200 +                    && au_dbtop(dentry) < 0)) {
8201 +               err = -EIO;
8202 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8203 +                       dentry, err);
8204 +       }
8205 +
8206 +out_parent:
8207 +       dput(parent);
8208 +       au_kfree_try_rcu(args.whname.name);
8209 +       if (dirren)
8210 +               au_dr_lkup_fin(&args);
8211 +out:
8212 +       return err;
8213 +}
8214 +
8215 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8216 +                              struct path *ppath)
8217 +{
8218 +       struct dentry *dentry;
8219 +       int wkq_err;
8220 +
8221 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8222 +               dentry = vfsub_lkup_one(name, ppath);
8223 +       else {
8224 +               struct vfsub_lkup_one_args args = {
8225 +                       .errp   = &dentry,
8226 +                       .name   = name,
8227 +                       .ppath  = ppath
8228 +               };
8229 +
8230 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8231 +               if (unlikely(wkq_err))
8232 +                       dentry = ERR_PTR(wkq_err);
8233 +       }
8234 +
8235 +       return dentry;
8236 +}
8237 +
8238 +/*
8239 + * lookup @dentry on @bindex which should be negative.
8240 + */
8241 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8242 +{
8243 +       int err;
8244 +       struct dentry *parent, *h_dentry;
8245 +       struct au_branch *br;
8246 +       struct user_namespace *h_userns;
8247 +       struct path h_ppath;
8248 +
8249 +       parent = dget_parent(dentry);
8250 +       br = au_sbr(dentry->d_sb, bindex);
8251 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8252 +       h_ppath.mnt = au_br_mnt(br);
8253 +       h_userns = au_br_userns(br);
8254 +       if (wh)
8255 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8256 +       else
8257 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8258 +       err = PTR_ERR(h_dentry);
8259 +       if (IS_ERR(h_dentry))
8260 +               goto out;
8261 +       if (unlikely(d_is_positive(h_dentry))) {
8262 +               err = -EIO;
8263 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8264 +               dput(h_dentry);
8265 +               goto out;
8266 +       }
8267 +
8268 +       err = 0;
8269 +       if (bindex < au_dbtop(dentry))
8270 +               au_set_dbtop(dentry, bindex);
8271 +       if (au_dbbot(dentry) < bindex)
8272 +               au_set_dbbot(dentry, bindex);
8273 +       au_set_h_dptr(dentry, bindex, h_dentry);
8274 +
8275 +out:
8276 +       dput(parent);
8277 +       return err;
8278 +}
8279 +
8280 +/* ---------------------------------------------------------------------- */
8281 +
8282 +/* subset of struct inode */
8283 +struct au_iattr {
8284 +       unsigned long           i_ino;
8285 +       /* unsigned int         i_nlink; */
8286 +       kuid_t                  i_uid;
8287 +       kgid_t                  i_gid;
8288 +       u64                     i_version;
8289 +/*
8290 +       loff_t                  i_size;
8291 +       blkcnt_t                i_blocks;
8292 +*/
8293 +       umode_t                 i_mode;
8294 +};
8295 +
8296 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8297 +{
8298 +       ia->i_ino = h_inode->i_ino;
8299 +       /* ia->i_nlink = h_inode->i_nlink; */
8300 +       ia->i_uid = h_inode->i_uid;
8301 +       ia->i_gid = h_inode->i_gid;
8302 +       ia->i_version = inode_query_iversion(h_inode);
8303 +/*
8304 +       ia->i_size = h_inode->i_size;
8305 +       ia->i_blocks = h_inode->i_blocks;
8306 +*/
8307 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8308 +}
8309 +
8310 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8311 +{
8312 +       return ia->i_ino != h_inode->i_ino
8313 +               /* || ia->i_nlink != h_inode->i_nlink */
8314 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8315 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8316 +               || !inode_eq_iversion(h_inode, ia->i_version)
8317 +/*
8318 +               || ia->i_size != h_inode->i_size
8319 +               || ia->i_blocks != h_inode->i_blocks
8320 +*/
8321 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8322 +}
8323 +
8324 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8325 +                             struct au_branch *br)
8326 +{
8327 +       int err;
8328 +       struct au_iattr ia;
8329 +       struct inode *h_inode;
8330 +       struct dentry *h_d;
8331 +       struct super_block *h_sb;
8332 +       struct path h_ppath;
8333 +
8334 +       err = 0;
8335 +       memset(&ia, -1, sizeof(ia));
8336 +       h_sb = h_dentry->d_sb;
8337 +       h_inode = NULL;
8338 +       if (d_is_positive(h_dentry)) {
8339 +               h_inode = d_inode(h_dentry);
8340 +               au_iattr_save(&ia, h_inode);
8341 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8342 +               /* nfs d_revalidate may return 0 for negative dentry */
8343 +               /* fuse d_revalidate always return 0 for negative dentry */
8344 +               goto out;
8345 +
8346 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8347 +       h_ppath.dentry = h_parent;
8348 +       h_ppath.mnt = au_br_mnt(br);
8349 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8350 +       err = PTR_ERR(h_d);
8351 +       if (IS_ERR(h_d))
8352 +               goto out;
8353 +
8354 +       err = 0;
8355 +       if (unlikely(h_d != h_dentry
8356 +                    || d_inode(h_d) != h_inode
8357 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8358 +               err = au_busy_or_stale();
8359 +       dput(h_d);
8360 +
8361 +out:
8362 +       AuTraceErr(err);
8363 +       return err;
8364 +}
8365 +
8366 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8367 +               struct dentry *h_parent, struct au_branch *br)
8368 +{
8369 +       int err;
8370 +
8371 +       err = 0;
8372 +       if (udba == AuOpt_UDBA_REVAL
8373 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8374 +               IMustLock(h_dir);
8375 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8376 +       } else if (udba != AuOpt_UDBA_NONE)
8377 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8378 +
8379 +       return err;
8380 +}
8381 +
8382 +/* ---------------------------------------------------------------------- */
8383 +
8384 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8385 +{
8386 +       int err;
8387 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8388 +       struct au_hdentry tmp, *p, *q;
8389 +       struct au_dinfo *dinfo;
8390 +       struct super_block *sb;
8391 +
8392 +       DiMustWriteLock(dentry);
8393 +
8394 +       sb = dentry->d_sb;
8395 +       dinfo = au_di(dentry);
8396 +       bbot = dinfo->di_bbot;
8397 +       bwh = dinfo->di_bwh;
8398 +       bdiropq = dinfo->di_bdiropq;
8399 +       bindex = dinfo->di_btop;
8400 +       p = au_hdentry(dinfo, bindex);
8401 +       for (; bindex <= bbot; bindex++, p++) {
8402 +               if (!p->hd_dentry)
8403 +                       continue;
8404 +
8405 +               new_bindex = au_br_index(sb, p->hd_id);
8406 +               if (new_bindex == bindex)
8407 +                       continue;
8408 +
8409 +               if (dinfo->di_bwh == bindex)
8410 +                       bwh = new_bindex;
8411 +               if (dinfo->di_bdiropq == bindex)
8412 +                       bdiropq = new_bindex;
8413 +               if (new_bindex < 0) {
8414 +                       au_hdput(p);
8415 +                       p->hd_dentry = NULL;
8416 +                       continue;
8417 +               }
8418 +
8419 +               /* swap two lower dentries, and loop again */
8420 +               q = au_hdentry(dinfo, new_bindex);
8421 +               tmp = *q;
8422 +               *q = *p;
8423 +               *p = tmp;
8424 +               if (tmp.hd_dentry) {
8425 +                       bindex--;
8426 +                       p--;
8427 +               }
8428 +       }
8429 +
8430 +       dinfo->di_bwh = -1;
8431 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8432 +               dinfo->di_bwh = bwh;
8433 +
8434 +       dinfo->di_bdiropq = -1;
8435 +       if (bdiropq >= 0
8436 +           && bdiropq <= au_sbbot(sb)
8437 +           && au_sbr_whable(sb, bdiropq))
8438 +               dinfo->di_bdiropq = bdiropq;
8439 +
8440 +       err = -EIO;
8441 +       dinfo->di_btop = -1;
8442 +       dinfo->di_bbot = -1;
8443 +       bbot = au_dbbot(parent);
8444 +       bindex = 0;
8445 +       p = au_hdentry(dinfo, bindex);
8446 +       for (; bindex <= bbot; bindex++, p++)
8447 +               if (p->hd_dentry) {
8448 +                       dinfo->di_btop = bindex;
8449 +                       break;
8450 +               }
8451 +
8452 +       if (dinfo->di_btop >= 0) {
8453 +               bindex = bbot;
8454 +               p = au_hdentry(dinfo, bindex);
8455 +               for (; bindex >= 0; bindex--, p--)
8456 +                       if (p->hd_dentry) {
8457 +                               dinfo->di_bbot = bindex;
8458 +                               err = 0;
8459 +                               break;
8460 +                       }
8461 +       }
8462 +
8463 +       return err;
8464 +}
8465 +
8466 +static void au_do_hide(struct dentry *dentry)
8467 +{
8468 +       struct inode *inode;
8469 +
8470 +       if (d_really_is_positive(dentry)) {
8471 +               inode = d_inode(dentry);
8472 +               if (!d_is_dir(dentry)) {
8473 +                       if (inode->i_nlink && !d_unhashed(dentry))
8474 +                               drop_nlink(inode);
8475 +               } else {
8476 +                       clear_nlink(inode);
8477 +                       /* stop next lookup */
8478 +                       inode->i_flags |= S_DEAD;
8479 +               }
8480 +               smp_mb(); /* necessary? */
8481 +       }
8482 +       d_drop(dentry);
8483 +}
8484 +
8485 +static int au_hide_children(struct dentry *parent)
8486 +{
8487 +       int err, i, j, ndentry;
8488 +       struct au_dcsub_pages dpages;
8489 +       struct au_dpage *dpage;
8490 +       struct dentry *dentry;
8491 +
8492 +       err = au_dpages_init(&dpages, GFP_NOFS);
8493 +       if (unlikely(err))
8494 +               goto out;
8495 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8496 +       if (unlikely(err))
8497 +               goto out_dpages;
8498 +
8499 +       /* in reverse order */
8500 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8501 +               dpage = dpages.dpages + i;
8502 +               ndentry = dpage->ndentry;
8503 +               for (j = ndentry - 1; j >= 0; j--) {
8504 +                       dentry = dpage->dentries[j];
8505 +                       if (dentry != parent)
8506 +                               au_do_hide(dentry);
8507 +               }
8508 +       }
8509 +
8510 +out_dpages:
8511 +       au_dpages_free(&dpages);
8512 +out:
8513 +       return err;
8514 +}
8515 +
8516 +static void au_hide(struct dentry *dentry)
8517 +{
8518 +       int err;
8519 +
8520 +       AuDbgDentry(dentry);
8521 +       if (d_is_dir(dentry)) {
8522 +               /* shrink_dcache_parent(dentry); */
8523 +               err = au_hide_children(dentry);
8524 +               if (unlikely(err))
8525 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8526 +                               dentry, err);
8527 +       }
8528 +       au_do_hide(dentry);
8529 +}
8530 +
8531 +/*
8532 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8533 + *
8534 + * a dirty branch is added
8535 + * - on the top of layers
8536 + * - in the middle of layers
8537 + * - to the bottom of layers
8538 + *
8539 + * on the added branch there exists
8540 + * - a whiteout
8541 + * - a diropq
8542 + * - a same named entry
8543 + *   + exist
8544 + *     * negative --> positive
8545 + *     * positive --> positive
8546 + *      - type is unchanged
8547 + *      - type is changed
8548 + *   + doesn't exist
8549 + *     * negative --> negative
8550 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8551 + * - none
8552 + */
8553 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8554 +                              struct au_dinfo *tmp)
8555 +{
8556 +       int err;
8557 +       aufs_bindex_t bindex, bbot;
8558 +       struct {
8559 +               struct dentry *dentry;
8560 +               struct inode *inode;
8561 +               mode_t mode;
8562 +       } orig_h, tmp_h = {
8563 +               .dentry = NULL
8564 +       };
8565 +       struct au_hdentry *hd;
8566 +       struct inode *inode, *h_inode;
8567 +       struct dentry *h_dentry;
8568 +
8569 +       err = 0;
8570 +       AuDebugOn(dinfo->di_btop < 0);
8571 +       orig_h.mode = 0;
8572 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8573 +       orig_h.inode = NULL;
8574 +       if (d_is_positive(orig_h.dentry)) {
8575 +               orig_h.inode = d_inode(orig_h.dentry);
8576 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8577 +       }
8578 +       if (tmp->di_btop >= 0) {
8579 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8580 +               if (d_is_positive(tmp_h.dentry)) {
8581 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8582 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8583 +               }
8584 +       }
8585 +
8586 +       inode = NULL;
8587 +       if (d_really_is_positive(dentry))
8588 +               inode = d_inode(dentry);
8589 +       if (!orig_h.inode) {
8590 +               AuDbg("negative originally\n");
8591 +               if (inode) {
8592 +                       au_hide(dentry);
8593 +                       goto out;
8594 +               }
8595 +               AuDebugOn(inode);
8596 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8597 +               AuDebugOn(dinfo->di_bdiropq != -1);
8598 +
8599 +               if (!tmp_h.inode) {
8600 +                       AuDbg("negative --> negative\n");
8601 +                       /* should have only one negative lower */
8602 +                       if (tmp->di_btop >= 0
8603 +                           && tmp->di_btop < dinfo->di_btop) {
8604 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8605 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8606 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8607 +                               au_di_cp(dinfo, tmp);
8608 +                               hd = au_hdentry(tmp, tmp->di_btop);
8609 +                               au_set_h_dptr(dentry, tmp->di_btop,
8610 +                                             dget(hd->hd_dentry));
8611 +                       }
8612 +                       au_dbg_verify_dinode(dentry);
8613 +               } else {
8614 +                       AuDbg("negative --> positive\n");
8615 +                       /*
8616 +                        * similar to the behaviour of creating with bypassing
8617 +                        * aufs.
8618 +                        * unhash it in order to force an error in the
8619 +                        * succeeding create operation.
8620 +                        * we should not set S_DEAD here.
8621 +                        */
8622 +                       d_drop(dentry);
8623 +                       /* au_di_swap(tmp, dinfo); */
8624 +                       au_dbg_verify_dinode(dentry);
8625 +               }
8626 +       } else {
8627 +               AuDbg("positive originally\n");
8628 +               /* inode may be NULL */
8629 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8630 +               if (!tmp_h.inode) {
8631 +                       AuDbg("positive --> negative\n");
8632 +                       /* or bypassing aufs */
8633 +                       au_hide(dentry);
8634 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8635 +                               dinfo->di_bwh = tmp->di_bwh;
8636 +                       if (inode)
8637 +                               err = au_refresh_hinode_self(inode);
8638 +                       au_dbg_verify_dinode(dentry);
8639 +               } else if (orig_h.mode == tmp_h.mode) {
8640 +                       AuDbg("positive --> positive, same type\n");
8641 +                       if (!S_ISDIR(orig_h.mode)
8642 +                           && dinfo->di_btop > tmp->di_btop) {
8643 +                               /*
8644 +                                * similar to the behaviour of removing and
8645 +                                * creating.
8646 +                                */
8647 +                               au_hide(dentry);
8648 +                               if (inode)
8649 +                                       err = au_refresh_hinode_self(inode);
8650 +                               au_dbg_verify_dinode(dentry);
8651 +                       } else {
8652 +                               /* fill empty slots */
8653 +                               if (dinfo->di_btop > tmp->di_btop)
8654 +                                       dinfo->di_btop = tmp->di_btop;
8655 +                               if (dinfo->di_bbot < tmp->di_bbot)
8656 +                                       dinfo->di_bbot = tmp->di_bbot;
8657 +                               dinfo->di_bwh = tmp->di_bwh;
8658 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8659 +                               bbot = dinfo->di_bbot;
8660 +                               bindex = tmp->di_btop;
8661 +                               hd = au_hdentry(tmp, bindex);
8662 +                               for (; bindex <= bbot; bindex++, hd++) {
8663 +                                       if (au_h_dptr(dentry, bindex))
8664 +                                               continue;
8665 +                                       h_dentry = hd->hd_dentry;
8666 +                                       if (!h_dentry)
8667 +                                               continue;
8668 +                                       AuDebugOn(d_is_negative(h_dentry));
8669 +                                       h_inode = d_inode(h_dentry);
8670 +                                       AuDebugOn(orig_h.mode
8671 +                                                 != (h_inode->i_mode
8672 +                                                     & S_IFMT));
8673 +                                       au_set_h_dptr(dentry, bindex,
8674 +                                                     dget(h_dentry));
8675 +                               }
8676 +                               if (inode)
8677 +                                       err = au_refresh_hinode(inode, dentry);
8678 +                               au_dbg_verify_dinode(dentry);
8679 +                       }
8680 +               } else {
8681 +                       AuDbg("positive --> positive, different type\n");
8682 +                       /* similar to the behaviour of removing and creating */
8683 +                       au_hide(dentry);
8684 +                       if (inode)
8685 +                               err = au_refresh_hinode_self(inode);
8686 +                       au_dbg_verify_dinode(dentry);
8687 +               }
8688 +       }
8689 +
8690 +out:
8691 +       return err;
8692 +}
8693 +
8694 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8695 +{
8696 +       const struct dentry_operations *dop
8697 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8698 +       static const unsigned int mask
8699 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8700 +
8701 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8702 +
8703 +       if (dentry->d_op == dop)
8704 +               return;
8705 +
8706 +       AuDbg("%pd\n", dentry);
8707 +       spin_lock(&dentry->d_lock);
8708 +       if (dop == &aufs_dop)
8709 +               dentry->d_flags |= mask;
8710 +       else
8711 +               dentry->d_flags &= ~mask;
8712 +       dentry->d_op = dop;
8713 +       spin_unlock(&dentry->d_lock);
8714 +}
8715 +
8716 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8717 +{
8718 +       int err, ebrange, nbr;
8719 +       unsigned int sigen;
8720 +       struct au_dinfo *dinfo, *tmp;
8721 +       struct super_block *sb;
8722 +       struct inode *inode;
8723 +
8724 +       DiMustWriteLock(dentry);
8725 +       AuDebugOn(IS_ROOT(dentry));
8726 +       AuDebugOn(d_really_is_negative(parent));
8727 +
8728 +       sb = dentry->d_sb;
8729 +       sigen = au_sigen(sb);
8730 +       err = au_digen_test(parent, sigen);
8731 +       if (unlikely(err))
8732 +               goto out;
8733 +
8734 +       nbr = au_sbbot(sb) + 1;
8735 +       dinfo = au_di(dentry);
8736 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8737 +       if (unlikely(err))
8738 +               goto out;
8739 +       ebrange = au_dbrange_test(dentry);
8740 +       if (!ebrange)
8741 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8742 +
8743 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8744 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8745 +               if (d_really_is_positive(dentry)) {
8746 +                       inode = d_inode(dentry);
8747 +                       err = au_refresh_hinode_self(inode);
8748 +               }
8749 +               au_dbg_verify_dinode(dentry);
8750 +               if (!err)
8751 +                       goto out_dgen; /* success */
8752 +               goto out;
8753 +       }
8754 +
8755 +       /* temporary dinfo */
8756 +       AuDbgDentry(dentry);
8757 +       err = -ENOMEM;
8758 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8759 +       if (unlikely(!tmp))
8760 +               goto out;
8761 +       au_di_swap(tmp, dinfo);
8762 +       /* returns the number of positive dentries */
8763 +       /*
8764 +        * if current working dir is removed, it returns an error.
8765 +        * but the dentry is legal.
8766 +        */
8767 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8768 +       AuDbgDentry(dentry);
8769 +       au_di_swap(tmp, dinfo);
8770 +       if (err == -ENOENT)
8771 +               err = 0;
8772 +       if (err >= 0) {
8773 +               /* compare/refresh by dinfo */
8774 +               AuDbgDentry(dentry);
8775 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8776 +               au_dbg_verify_dinode(dentry);
8777 +               AuTraceErr(err);
8778 +       }
8779 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8780 +       au_rw_write_unlock(&tmp->di_rwsem);
8781 +       au_di_free(tmp);
8782 +       if (unlikely(err))
8783 +               goto out;
8784 +
8785 +out_dgen:
8786 +       au_update_digen(dentry);
8787 +out:
8788 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8789 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8790 +               AuDbgDentry(dentry);
8791 +       }
8792 +       AuTraceErr(err);
8793 +       return err;
8794 +}
8795 +
8796 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8797 +                          struct dentry *dentry, aufs_bindex_t bindex)
8798 +{
8799 +       int err, valid;
8800 +
8801 +       err = 0;
8802 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8803 +               goto out;
8804 +
8805 +       AuDbg("b%d\n", bindex);
8806 +       /*
8807 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8808 +        * due to whiteout and branch permission.
8809 +        */
8810 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8811 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8812 +       /* it may return tri-state */
8813 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8814 +
8815 +       if (unlikely(valid < 0))
8816 +               err = valid;
8817 +       else if (!valid)
8818 +               err = -EINVAL;
8819 +
8820 +out:
8821 +       AuTraceErr(err);
8822 +       return err;
8823 +}
8824 +
8825 +/* todo: remove this */
8826 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8827 +                         unsigned int flags, int do_udba, int dirren)
8828 +{
8829 +       int err;
8830 +       umode_t mode, h_mode;
8831 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8832 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8833 +       struct inode *h_inode, *h_cached_inode;
8834 +       struct dentry *h_dentry;
8835 +       struct qstr *name, *h_name;
8836 +
8837 +       err = 0;
8838 +       plus = 0;
8839 +       mode = 0;
8840 +       ibs = -1;
8841 +       ibe = -1;
8842 +       unhashed = !!d_unhashed(dentry);
8843 +       is_root = !!IS_ROOT(dentry);
8844 +       name = &dentry->d_name;
8845 +       tmpfile = au_di(dentry)->di_tmpfile;
8846 +
8847 +       /*
8848 +        * Theoretically, REVAL test should be unnecessary in case of
8849 +        * {FS,I}NOTIFY.
8850 +        * But {fs,i}notify doesn't fire some necessary events,
8851 +        *      IN_ATTRIB for atime/nlink/pageio
8852 +        * Let's do REVAL test too.
8853 +        */
8854 +       if (do_udba && inode) {
8855 +               mode = (inode->i_mode & S_IFMT);
8856 +               plus = (inode->i_nlink > 0);
8857 +               ibs = au_ibtop(inode);
8858 +               ibe = au_ibbot(inode);
8859 +       }
8860 +
8861 +       btop = au_dbtop(dentry);
8862 +       btail = btop;
8863 +       if (inode && S_ISDIR(inode->i_mode))
8864 +               btail = au_dbtaildir(dentry);
8865 +       for (bindex = btop; bindex <= btail; bindex++) {
8866 +               h_dentry = au_h_dptr(dentry, bindex);
8867 +               if (!h_dentry)
8868 +                       continue;
8869 +
8870 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8871 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8872 +               spin_lock(&h_dentry->d_lock);
8873 +               h_name = &h_dentry->d_name;
8874 +               if (unlikely(do_udba
8875 +                            && !is_root
8876 +                            && ((!h_nfs
8877 +                                 && (unhashed != !!d_unhashed(h_dentry)
8878 +                                     || (!tmpfile && !dirren
8879 +                                         && !au_qstreq(name, h_name))
8880 +                                         ))
8881 +                                || (h_nfs
8882 +                                    && !(flags & LOOKUP_OPEN)
8883 +                                    && (h_dentry->d_flags
8884 +                                        & DCACHE_NFSFS_RENAMED)))
8885 +                           )) {
8886 +                       int h_unhashed;
8887 +
8888 +                       h_unhashed = d_unhashed(h_dentry);
8889 +                       spin_unlock(&h_dentry->d_lock);
8890 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8891 +                             unhashed, h_unhashed, dentry, h_dentry);
8892 +                       goto err;
8893 +               }
8894 +               spin_unlock(&h_dentry->d_lock);
8895 +
8896 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8897 +               if (unlikely(err))
8898 +                       /* do not goto err, to keep the errno */
8899 +                       break;
8900 +
8901 +               /* todo: plink too? */
8902 +               if (!do_udba)
8903 +                       continue;
8904 +
8905 +               /* UDBA tests */
8906 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8907 +                       goto err;
8908 +
8909 +               h_inode = NULL;
8910 +               if (d_is_positive(h_dentry))
8911 +                       h_inode = d_inode(h_dentry);
8912 +               h_plus = plus;
8913 +               h_mode = mode;
8914 +               h_cached_inode = h_inode;
8915 +               if (h_inode) {
8916 +                       h_mode = (h_inode->i_mode & S_IFMT);
8917 +                       h_plus = (h_inode->i_nlink > 0);
8918 +               }
8919 +               if (inode && ibs <= bindex && bindex <= ibe)
8920 +                       h_cached_inode = au_h_iptr(inode, bindex);
8921 +
8922 +               if (!h_nfs) {
8923 +                       if (unlikely(plus != h_plus && !tmpfile))
8924 +                               goto err;
8925 +               } else {
8926 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8927 +                                    && !is_root
8928 +                                    && !IS_ROOT(h_dentry)
8929 +                                    && unhashed != d_unhashed(h_dentry)))
8930 +                               goto err;
8931 +               }
8932 +               if (unlikely(mode != h_mode
8933 +                            || h_cached_inode != h_inode))
8934 +                       goto err;
8935 +               continue;
8936 +
8937 +err:
8938 +               err = -EINVAL;
8939 +               break;
8940 +       }
8941 +
8942 +       AuTraceErr(err);
8943 +       return err;
8944 +}
8945 +
8946 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8947 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8948 +{
8949 +       int err;
8950 +       struct dentry *parent;
8951 +
8952 +       if (!au_digen_test(dentry, sigen))
8953 +               return 0;
8954 +
8955 +       parent = dget_parent(dentry);
8956 +       di_read_lock_parent(parent, AuLock_IR);
8957 +       AuDebugOn(au_digen_test(parent, sigen));
8958 +       au_dbg_verify_gen(parent, sigen);
8959 +       err = au_refresh_dentry(dentry, parent);
8960 +       di_read_unlock(parent, AuLock_IR);
8961 +       dput(parent);
8962 +       AuTraceErr(err);
8963 +       return err;
8964 +}
8965 +
8966 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8967 +{
8968 +       int err;
8969 +       struct dentry *d, *parent;
8970 +
8971 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8972 +               return simple_reval_dpath(dentry, sigen);
8973 +
8974 +       /* slow loop, keep it simple and stupid */
8975 +       /* cf: au_cpup_dirs() */
8976 +       err = 0;
8977 +       parent = NULL;
8978 +       while (au_digen_test(dentry, sigen)) {
8979 +               d = dentry;
8980 +               while (1) {
8981 +                       dput(parent);
8982 +                       parent = dget_parent(d);
8983 +                       if (!au_digen_test(parent, sigen))
8984 +                               break;
8985 +                       d = parent;
8986 +               }
8987 +
8988 +               if (d != dentry)
8989 +                       di_write_lock_child2(d);
8990 +
8991 +               /* someone might update our dentry while we were sleeping */
8992 +               if (au_digen_test(d, sigen)) {
8993 +                       /*
8994 +                        * todo: consolidate with simple_reval_dpath(),
8995 +                        * do_refresh() and au_reval_for_attr().
8996 +                        */
8997 +                       di_read_lock_parent(parent, AuLock_IR);
8998 +                       err = au_refresh_dentry(d, parent);
8999 +                       di_read_unlock(parent, AuLock_IR);
9000 +               }
9001 +
9002 +               if (d != dentry)
9003 +                       di_write_unlock(d);
9004 +               dput(parent);
9005 +               if (unlikely(err))
9006 +                       break;
9007 +       }
9008 +
9009 +       return err;
9010 +}
9011 +
9012 +/*
9013 + * if valid returns 1, otherwise 0.
9014 + */
9015 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9016 +{
9017 +       int valid, err;
9018 +       unsigned int sigen;
9019 +       unsigned char do_udba, dirren;
9020 +       struct super_block *sb;
9021 +       struct inode *inode;
9022 +
9023 +       /* todo: support rcu-walk? */
9024 +       if (flags & LOOKUP_RCU)
9025 +               return -ECHILD;
9026 +
9027 +       valid = 0;
9028 +       if (unlikely(!au_di(dentry)))
9029 +               goto out;
9030 +
9031 +       valid = 1;
9032 +       sb = dentry->d_sb;
9033 +       /*
9034 +        * todo: very ugly
9035 +        * i_mutex of parent dir may be held,
9036 +        * but we should not return 'invalid' due to busy.
9037 +        */
9038 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9039 +       if (unlikely(err)) {
9040 +               valid = err;
9041 +               AuTraceErr(err);
9042 +               goto out;
9043 +       }
9044 +       inode = NULL;
9045 +       if (d_really_is_positive(dentry))
9046 +               inode = d_inode(dentry);
9047 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9048 +               err = -EINVAL;
9049 +               AuTraceErr(err);
9050 +               goto out_dgrade;
9051 +       }
9052 +       if (unlikely(au_dbrange_test(dentry))) {
9053 +               err = -EINVAL;
9054 +               AuTraceErr(err);
9055 +               goto out_dgrade;
9056 +       }
9057 +
9058 +       sigen = au_sigen(sb);
9059 +       if (au_digen_test(dentry, sigen)) {
9060 +               AuDebugOn(IS_ROOT(dentry));
9061 +               err = au_reval_dpath(dentry, sigen);
9062 +               if (unlikely(err)) {
9063 +                       AuTraceErr(err);
9064 +                       goto out_dgrade;
9065 +               }
9066 +       }
9067 +       di_downgrade_lock(dentry, AuLock_IR);
9068 +
9069 +       err = -EINVAL;
9070 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9071 +           && inode
9072 +           && !(inode->i_state && I_LINKABLE)
9073 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9074 +               AuTraceErr(err);
9075 +               goto out_inval;
9076 +       }
9077 +
9078 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9079 +       if (do_udba && inode) {
9080 +               aufs_bindex_t btop = au_ibtop(inode);
9081 +               struct inode *h_inode;
9082 +
9083 +               if (btop >= 0) {
9084 +                       h_inode = au_h_iptr(inode, btop);
9085 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9086 +                               AuTraceErr(err);
9087 +                               goto out_inval;
9088 +                       }
9089 +               }
9090 +       }
9091 +
9092 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9093 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9094 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9095 +               err = -EIO;
9096 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9097 +                     dentry, err);
9098 +       }
9099 +       goto out_inval;
9100 +
9101 +out_dgrade:
9102 +       di_downgrade_lock(dentry, AuLock_IR);
9103 +out_inval:
9104 +       aufs_read_unlock(dentry, AuLock_IR);
9105 +       AuTraceErr(err);
9106 +       valid = !err;
9107 +out:
9108 +       if (!valid) {
9109 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9110 +               d_drop(dentry);
9111 +       }
9112 +       return valid;
9113 +}
9114 +
9115 +static void aufs_d_release(struct dentry *dentry)
9116 +{
9117 +       if (au_di(dentry)) {
9118 +               au_di_fin(dentry);
9119 +               au_hn_di_reinit(dentry);
9120 +       }
9121 +}
9122 +
9123 +const struct dentry_operations aufs_dop = {
9124 +       .d_revalidate           = aufs_d_revalidate,
9125 +       .d_weak_revalidate      = aufs_d_revalidate,
9126 +       .d_release              = aufs_d_release
9127 +};
9128 +
9129 +/* aufs_dop without d_revalidate */
9130 +const struct dentry_operations aufs_dop_noreval = {
9131 +       .d_release              = aufs_d_release
9132 +};
9133 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9134 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9135 +++ linux/fs/aufs/dentry.h      2022-12-17 09:21:34.796521861 +0100
9136 @@ -0,0 +1,270 @@
9137 +/* SPDX-License-Identifier: GPL-2.0 */
9138 +/*
9139 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9140 + *
9141 + * This program is free software; you can redistribute it and/or modify
9142 + * it under the terms of the GNU General Public License as published by
9143 + * the Free Software Foundation; either version 2 of the License, or
9144 + * (at your option) any later version.
9145 + *
9146 + * This program is distributed in the hope that it will be useful,
9147 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9148 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9149 + * GNU General Public License for more details.
9150 + *
9151 + * You should have received a copy of the GNU General Public License
9152 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9153 + */
9154 +
9155 +/*
9156 + * lookup and dentry operations
9157 + */
9158 +
9159 +#ifndef __AUFS_DENTRY_H__
9160 +#define __AUFS_DENTRY_H__
9161 +
9162 +#ifdef __KERNEL__
9163 +
9164 +#include <linux/dcache.h>
9165 +#include "dirren.h"
9166 +#include "rwsem.h"
9167 +
9168 +struct au_hdentry {
9169 +       struct dentry           *hd_dentry;
9170 +       aufs_bindex_t           hd_id;
9171 +};
9172 +
9173 +struct au_dinfo {
9174 +       atomic_t                di_generation;
9175 +
9176 +       struct au_rwsem         di_rwsem;
9177 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9178 +       unsigned char           di_tmpfile; /* to allow the different name */
9179 +       struct au_hdentry       *di_hdentry;
9180 +       struct file             *di_htmpfile;
9181 +       struct rcu_head         rcu;
9182 +} ____cacheline_aligned_in_smp;
9183 +
9184 +/* ---------------------------------------------------------------------- */
9185 +
9186 +/* flags for au_lkup_dentry() */
9187 +#define AuLkup_ALLOW_NEG       1
9188 +#define AuLkup_IGNORE_PERM     (1 << 1)
9189 +#define AuLkup_DIRREN          (1 << 2)
9190 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9191 +#define au_fset_lkup(flags, name) \
9192 +       do { (flags) |= AuLkup_##name; } while (0)
9193 +#define au_fclr_lkup(flags, name) \
9194 +       do { (flags) &= ~AuLkup_##name; } while (0)
9195 +
9196 +#ifndef CONFIG_AUFS_DIRREN
9197 +#undef AuLkup_DIRREN
9198 +#define AuLkup_DIRREN 0
9199 +#endif
9200 +
9201 +struct au_do_lookup_args {
9202 +       unsigned int            flags;
9203 +       mode_t                  type;
9204 +       struct qstr             whname, *name;
9205 +       struct au_dr_lookup     dirren;
9206 +};
9207 +
9208 +/* ---------------------------------------------------------------------- */
9209 +
9210 +/* dentry.c */
9211 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9212 +struct au_branch;
9213 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9214 +                              struct path *ppath);
9215 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9216 +               struct dentry *h_parent, struct au_branch *br);
9217 +
9218 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9219 +                  unsigned int flags);
9220 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9221 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9222 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9223 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9224 +
9225 +/* dinfo.c */
9226 +void au_di_init_once(void *_di);
9227 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9228 +void au_di_free(struct au_dinfo *dinfo);
9229 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9230 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9231 +int au_di_init(struct dentry *dentry);
9232 +void au_di_fin(struct dentry *dentry);
9233 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9234 +
9235 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9236 +void di_read_unlock(struct dentry *d, int flags);
9237 +void di_downgrade_lock(struct dentry *d, int flags);
9238 +void di_write_lock(struct dentry *d, unsigned int lsc);
9239 +void di_write_unlock(struct dentry *d);
9240 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9241 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9242 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9243 +
9244 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9245 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9246 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9247 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9248 +
9249 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9250 +                  struct dentry *h_dentry);
9251 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9252 +int au_dbrange_test(struct dentry *dentry);
9253 +void au_update_digen(struct dentry *dentry);
9254 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9255 +void au_update_dbtop(struct dentry *dentry);
9256 +void au_update_dbbot(struct dentry *dentry);
9257 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9258 +
9259 +/* ---------------------------------------------------------------------- */
9260 +
9261 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9262 +{
9263 +       return dentry->d_fsdata;
9264 +}
9265 +
9266 +/* ---------------------------------------------------------------------- */
9267 +
9268 +/* lock subclass for dinfo */
9269 +enum {
9270 +       AuLsc_DI_CHILD,         /* child first */
9271 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9272 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9273 +       AuLsc_DI_PARENT,
9274 +       AuLsc_DI_PARENT2,
9275 +       AuLsc_DI_PARENT3,
9276 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9277 +};
9278 +
9279 +/*
9280 + * di_read_lock_child, di_write_lock_child,
9281 + * di_read_lock_child2, di_write_lock_child2,
9282 + * di_read_lock_child3, di_write_lock_child3,
9283 + * di_read_lock_parent, di_write_lock_parent,
9284 + * di_read_lock_parent2, di_write_lock_parent2,
9285 + * di_read_lock_parent3, di_write_lock_parent3,
9286 + */
9287 +#define AuReadLockFunc(name, lsc) \
9288 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9289 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9290 +
9291 +#define AuWriteLockFunc(name, lsc) \
9292 +static inline void di_write_lock_##name(struct dentry *d) \
9293 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9294 +
9295 +#define AuRWLockFuncs(name, lsc) \
9296 +       AuReadLockFunc(name, lsc) \
9297 +       AuWriteLockFunc(name, lsc)
9298 +
9299 +AuRWLockFuncs(child, CHILD);
9300 +AuRWLockFuncs(child2, CHILD2);
9301 +AuRWLockFuncs(child3, CHILD3);
9302 +AuRWLockFuncs(parent, PARENT);
9303 +AuRWLockFuncs(parent2, PARENT2);
9304 +AuRWLockFuncs(parent3, PARENT3);
9305 +
9306 +#undef AuReadLockFunc
9307 +#undef AuWriteLockFunc
9308 +#undef AuRWLockFuncs
9309 +
9310 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9311 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9312 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9313 +
9314 +/* ---------------------------------------------------------------------- */
9315 +
9316 +/* todo: memory barrier? */
9317 +static inline unsigned int au_digen(struct dentry *d)
9318 +{
9319 +       return atomic_read(&au_di(d)->di_generation);
9320 +}
9321 +
9322 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9323 +{
9324 +       hdentry->hd_dentry = NULL;
9325 +}
9326 +
9327 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9328 +                                           aufs_bindex_t bindex)
9329 +{
9330 +       return di->di_hdentry + bindex;
9331 +}
9332 +
9333 +static inline void au_hdput(struct au_hdentry *hd)
9334 +{
9335 +       if (hd)
9336 +               dput(hd->hd_dentry);
9337 +}
9338 +
9339 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9340 +{
9341 +       DiMustAnyLock(dentry);
9342 +       return au_di(dentry)->di_btop;
9343 +}
9344 +
9345 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9346 +{
9347 +       DiMustAnyLock(dentry);
9348 +       return au_di(dentry)->di_bbot;
9349 +}
9350 +
9351 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9352 +{
9353 +       DiMustAnyLock(dentry);
9354 +       return au_di(dentry)->di_bwh;
9355 +}
9356 +
9357 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9358 +{
9359 +       DiMustAnyLock(dentry);
9360 +       return au_di(dentry)->di_bdiropq;
9361 +}
9362 +
9363 +/* todo: hard/soft set? */
9364 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9365 +{
9366 +       DiMustWriteLock(dentry);
9367 +       au_di(dentry)->di_btop = bindex;
9368 +}
9369 +
9370 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9371 +{
9372 +       DiMustWriteLock(dentry);
9373 +       au_di(dentry)->di_bbot = bindex;
9374 +}
9375 +
9376 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9377 +{
9378 +       DiMustWriteLock(dentry);
9379 +       /* dbwh can be outside of btop - bbot range */
9380 +       au_di(dentry)->di_bwh = bindex;
9381 +}
9382 +
9383 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9384 +{
9385 +       DiMustWriteLock(dentry);
9386 +       au_di(dentry)->di_bdiropq = bindex;
9387 +}
9388 +
9389 +/* ---------------------------------------------------------------------- */
9390 +
9391 +#ifdef CONFIG_AUFS_HNOTIFY
9392 +static inline void au_digen_dec(struct dentry *d)
9393 +{
9394 +       atomic_dec(&au_di(d)->di_generation);
9395 +}
9396 +
9397 +static inline void au_hn_di_reinit(struct dentry *dentry)
9398 +{
9399 +       dentry->d_fsdata = NULL;
9400 +}
9401 +#else
9402 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9403 +#endif /* CONFIG_AUFS_HNOTIFY */
9404 +
9405 +#endif /* __KERNEL__ */
9406 +#endif /* __AUFS_DENTRY_H__ */
9407 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9408 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9409 +++ linux/fs/aufs/dinfo.c       2022-12-17 09:21:34.796521861 +0100
9410 @@ -0,0 +1,555 @@
9411 +// SPDX-License-Identifier: GPL-2.0
9412 +/*
9413 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9414 + *
9415 + * This program is free software; you can redistribute it and/or modify
9416 + * it under the terms of the GNU General Public License as published by
9417 + * the Free Software Foundation; either version 2 of the License, or
9418 + * (at your option) any later version.
9419 + *
9420 + * This program is distributed in the hope that it will be useful,
9421 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9422 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9423 + * GNU General Public License for more details.
9424 + *
9425 + * You should have received a copy of the GNU General Public License
9426 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9427 + */
9428 +
9429 +/*
9430 + * dentry private data
9431 + */
9432 +
9433 +#include "aufs.h"
9434 +
9435 +void au_di_init_once(void *_dinfo)
9436 +{
9437 +       struct au_dinfo *dinfo = _dinfo;
9438 +
9439 +       au_rw_init(&dinfo->di_rwsem);
9440 +}
9441 +
9442 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9443 +{
9444 +       struct au_dinfo *dinfo;
9445 +       int nbr, i;
9446 +
9447 +       dinfo = au_cache_alloc_dinfo();
9448 +       if (unlikely(!dinfo))
9449 +               goto out;
9450 +
9451 +       nbr = au_sbbot(sb) + 1;
9452 +       if (nbr <= 0)
9453 +               nbr = 1;
9454 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9455 +       if (dinfo->di_hdentry) {
9456 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9457 +               dinfo->di_btop = -1;
9458 +               dinfo->di_bbot = -1;
9459 +               dinfo->di_bwh = -1;
9460 +               dinfo->di_bdiropq = -1;
9461 +               dinfo->di_tmpfile = 0;
9462 +               for (i = 0; i < nbr; i++)
9463 +                       dinfo->di_hdentry[i].hd_id = -1;
9464 +               dinfo->di_htmpfile = NULL;
9465 +               goto out;
9466 +       }
9467 +
9468 +       au_cache_free_dinfo(dinfo);
9469 +       dinfo = NULL;
9470 +
9471 +out:
9472 +       return dinfo;
9473 +}
9474 +
9475 +void au_di_free(struct au_dinfo *dinfo)
9476 +{
9477 +       struct au_hdentry *p;
9478 +       aufs_bindex_t bbot, bindex;
9479 +
9480 +       /* dentry may not be revalidated */
9481 +       bindex = dinfo->di_btop;
9482 +       if (bindex >= 0) {
9483 +               bbot = dinfo->di_bbot;
9484 +               p = au_hdentry(dinfo, bindex);
9485 +               while (bindex++ <= bbot)
9486 +                       au_hdput(p++);
9487 +       }
9488 +       au_kfree_try_rcu(dinfo->di_hdentry);
9489 +       au_cache_free_dinfo(dinfo);
9490 +}
9491 +
9492 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9493 +{
9494 +       struct au_hdentry *p;
9495 +       aufs_bindex_t bi;
9496 +
9497 +       AuRwMustWriteLock(&a->di_rwsem);
9498 +       AuRwMustWriteLock(&b->di_rwsem);
9499 +
9500 +#define DiSwap(v, name)                                \
9501 +       do {                                    \
9502 +               v = a->di_##name;               \
9503 +               a->di_##name = b->di_##name;    \
9504 +               b->di_##name = v;               \
9505 +       } while (0)
9506 +
9507 +       DiSwap(p, hdentry);
9508 +       DiSwap(bi, btop);
9509 +       DiSwap(bi, bbot);
9510 +       DiSwap(bi, bwh);
9511 +       DiSwap(bi, bdiropq);
9512 +       /* smp_mb(); */
9513 +
9514 +#undef DiSwap
9515 +}
9516 +
9517 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9518 +{
9519 +       AuRwMustWriteLock(&dst->di_rwsem);
9520 +       AuRwMustWriteLock(&src->di_rwsem);
9521 +
9522 +       dst->di_btop = src->di_btop;
9523 +       dst->di_bbot = src->di_bbot;
9524 +       dst->di_bwh = src->di_bwh;
9525 +       dst->di_bdiropq = src->di_bdiropq;
9526 +       /* smp_mb(); */
9527 +}
9528 +
9529 +int au_di_init(struct dentry *dentry)
9530 +{
9531 +       int err;
9532 +       struct super_block *sb;
9533 +       struct au_dinfo *dinfo;
9534 +
9535 +       err = 0;
9536 +       sb = dentry->d_sb;
9537 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9538 +       if (dinfo) {
9539 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9540 +               /* smp_mb(); */ /* atomic_set */
9541 +               dentry->d_fsdata = dinfo;
9542 +       } else
9543 +               err = -ENOMEM;
9544 +
9545 +       return err;
9546 +}
9547 +
9548 +void au_di_fin(struct dentry *dentry)
9549 +{
9550 +       struct au_dinfo *dinfo;
9551 +
9552 +       dinfo = au_di(dentry);
9553 +       AuRwDestroy(&dinfo->di_rwsem);
9554 +       au_di_free(dinfo);
9555 +}
9556 +
9557 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9558 +{
9559 +       int err, sz;
9560 +       struct au_hdentry *hdp;
9561 +
9562 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9563 +
9564 +       err = -ENOMEM;
9565 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9566 +       if (!sz)
9567 +               sz = sizeof(*hdp);
9568 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9569 +                          may_shrink);
9570 +       if (hdp) {
9571 +               dinfo->di_hdentry = hdp;
9572 +               err = 0;
9573 +       }
9574 +
9575 +       return err;
9576 +}
9577 +
9578 +/* ---------------------------------------------------------------------- */
9579 +
9580 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9581 +{
9582 +       switch (lsc) {
9583 +       case AuLsc_DI_CHILD:
9584 +               ii_write_lock_child(inode);
9585 +               break;
9586 +       case AuLsc_DI_CHILD2:
9587 +               ii_write_lock_child2(inode);
9588 +               break;
9589 +       case AuLsc_DI_CHILD3:
9590 +               ii_write_lock_child3(inode);
9591 +               break;
9592 +       case AuLsc_DI_PARENT:
9593 +               ii_write_lock_parent(inode);
9594 +               break;
9595 +       case AuLsc_DI_PARENT2:
9596 +               ii_write_lock_parent2(inode);
9597 +               break;
9598 +       case AuLsc_DI_PARENT3:
9599 +               ii_write_lock_parent3(inode);
9600 +               break;
9601 +       default:
9602 +               BUG();
9603 +       }
9604 +}
9605 +
9606 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9607 +{
9608 +       switch (lsc) {
9609 +       case AuLsc_DI_CHILD:
9610 +               ii_read_lock_child(inode);
9611 +               break;
9612 +       case AuLsc_DI_CHILD2:
9613 +               ii_read_lock_child2(inode);
9614 +               break;
9615 +       case AuLsc_DI_CHILD3:
9616 +               ii_read_lock_child3(inode);
9617 +               break;
9618 +       case AuLsc_DI_PARENT:
9619 +               ii_read_lock_parent(inode);
9620 +               break;
9621 +       case AuLsc_DI_PARENT2:
9622 +               ii_read_lock_parent2(inode);
9623 +               break;
9624 +       case AuLsc_DI_PARENT3:
9625 +               ii_read_lock_parent3(inode);
9626 +               break;
9627 +       default:
9628 +               BUG();
9629 +       }
9630 +}
9631 +
9632 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9633 +{
9634 +       struct inode *inode;
9635 +
9636 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9637 +       if (d_really_is_positive(d)) {
9638 +               inode = d_inode(d);
9639 +               if (au_ftest_lock(flags, IW))
9640 +                       do_ii_write_lock(inode, lsc);
9641 +               else if (au_ftest_lock(flags, IR))
9642 +                       do_ii_read_lock(inode, lsc);
9643 +       }
9644 +}
9645 +
9646 +void di_read_unlock(struct dentry *d, int flags)
9647 +{
9648 +       struct inode *inode;
9649 +
9650 +       if (d_really_is_positive(d)) {
9651 +               inode = d_inode(d);
9652 +               if (au_ftest_lock(flags, IW)) {
9653 +                       au_dbg_verify_dinode(d);
9654 +                       ii_write_unlock(inode);
9655 +               } else if (au_ftest_lock(flags, IR)) {
9656 +                       au_dbg_verify_dinode(d);
9657 +                       ii_read_unlock(inode);
9658 +               }
9659 +       }
9660 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9661 +}
9662 +
9663 +void di_downgrade_lock(struct dentry *d, int flags)
9664 +{
9665 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9666 +               ii_downgrade_lock(d_inode(d));
9667 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9668 +}
9669 +
9670 +void di_write_lock(struct dentry *d, unsigned int lsc)
9671 +{
9672 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9673 +       if (d_really_is_positive(d))
9674 +               do_ii_write_lock(d_inode(d), lsc);
9675 +}
9676 +
9677 +void di_write_unlock(struct dentry *d)
9678 +{
9679 +       au_dbg_verify_dinode(d);
9680 +       if (d_really_is_positive(d))
9681 +               ii_write_unlock(d_inode(d));
9682 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9683 +}
9684 +
9685 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9686 +{
9687 +       AuDebugOn(d1 == d2
9688 +                 || d_inode(d1) == d_inode(d2)
9689 +                 || d1->d_sb != d2->d_sb);
9690 +
9691 +       if ((isdir && au_test_subdir(d1, d2))
9692 +           || d1 < d2) {
9693 +               di_write_lock_child(d1);
9694 +               di_write_lock_child2(d2);
9695 +       } else {
9696 +               di_write_lock_child(d2);
9697 +               di_write_lock_child2(d1);
9698 +       }
9699 +}
9700 +
9701 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9702 +{
9703 +       AuDebugOn(d1 == d2
9704 +                 || d_inode(d1) == d_inode(d2)
9705 +                 || d1->d_sb != d2->d_sb);
9706 +
9707 +       if ((isdir && au_test_subdir(d1, d2))
9708 +           || d1 < d2) {
9709 +               di_write_lock_parent(d1);
9710 +               di_write_lock_parent2(d2);
9711 +       } else {
9712 +               di_write_lock_parent(d2);
9713 +               di_write_lock_parent2(d1);
9714 +       }
9715 +}
9716 +
9717 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9718 +{
9719 +       di_write_unlock(d1);
9720 +       if (d_inode(d1) == d_inode(d2))
9721 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9722 +       else
9723 +               di_write_unlock(d2);
9724 +}
9725 +
9726 +/* ---------------------------------------------------------------------- */
9727 +
9728 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9729 +{
9730 +       struct dentry *d;
9731 +
9732 +       DiMustAnyLock(dentry);
9733 +
9734 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9735 +               return NULL;
9736 +       AuDebugOn(bindex < 0);
9737 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9738 +       AuDebugOn(d && au_dcount(d) <= 0);
9739 +       return d;
9740 +}
9741 +
9742 +/*
9743 + * extended version of au_h_dptr().
9744 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9745 + * error.
9746 + */
9747 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9748 +{
9749 +       struct dentry *h_dentry;
9750 +       struct inode *inode, *h_inode;
9751 +
9752 +       AuDebugOn(d_really_is_negative(dentry));
9753 +
9754 +       h_dentry = NULL;
9755 +       if (au_dbtop(dentry) <= bindex
9756 +           && bindex <= au_dbbot(dentry))
9757 +               h_dentry = au_h_dptr(dentry, bindex);
9758 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9759 +               dget(h_dentry);
9760 +               goto out; /* success */
9761 +       }
9762 +
9763 +       inode = d_inode(dentry);
9764 +       AuDebugOn(bindex < au_ibtop(inode));
9765 +       AuDebugOn(au_ibbot(inode) < bindex);
9766 +       h_inode = au_h_iptr(inode, bindex);
9767 +       h_dentry = d_find_alias(h_inode);
9768 +       if (h_dentry) {
9769 +               if (!IS_ERR(h_dentry)) {
9770 +                       if (!au_d_linkable(h_dentry))
9771 +                               goto out; /* success */
9772 +                       dput(h_dentry);
9773 +               } else
9774 +                       goto out;
9775 +       }
9776 +
9777 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9778 +               h_dentry = au_plink_lkup(inode, bindex);
9779 +               AuDebugOn(!h_dentry);
9780 +               if (!IS_ERR(h_dentry)) {
9781 +                       if (!au_d_hashed_positive(h_dentry))
9782 +                               goto out; /* success */
9783 +                       dput(h_dentry);
9784 +                       h_dentry = NULL;
9785 +               }
9786 +       }
9787 +
9788 +out:
9789 +       AuDbgDentry(h_dentry);
9790 +       return h_dentry;
9791 +}
9792 +
9793 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9794 +{
9795 +       aufs_bindex_t bbot, bwh;
9796 +
9797 +       bbot = au_dbbot(dentry);
9798 +       if (0 <= bbot) {
9799 +               bwh = au_dbwh(dentry);
9800 +               if (!bwh)
9801 +                       return bwh;
9802 +               if (0 < bwh && bwh < bbot)
9803 +                       return bwh - 1;
9804 +       }
9805 +       return bbot;
9806 +}
9807 +
9808 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9809 +{
9810 +       aufs_bindex_t bbot, bopq;
9811 +
9812 +       bbot = au_dbtail(dentry);
9813 +       if (0 <= bbot) {
9814 +               bopq = au_dbdiropq(dentry);
9815 +               if (0 <= bopq && bopq < bbot)
9816 +                       bbot = bopq;
9817 +       }
9818 +       return bbot;
9819 +}
9820 +
9821 +/* ---------------------------------------------------------------------- */
9822 +
9823 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9824 +                  struct dentry *h_dentry)
9825 +{
9826 +       struct au_dinfo *dinfo;
9827 +       struct au_hdentry *hd;
9828 +       struct au_branch *br;
9829 +
9830 +       DiMustWriteLock(dentry);
9831 +
9832 +       dinfo = au_di(dentry);
9833 +       hd = au_hdentry(dinfo, bindex);
9834 +       au_hdput(hd);
9835 +       hd->hd_dentry = h_dentry;
9836 +       if (h_dentry) {
9837 +               br = au_sbr(dentry->d_sb, bindex);
9838 +               hd->hd_id = br->br_id;
9839 +       }
9840 +}
9841 +
9842 +int au_dbrange_test(struct dentry *dentry)
9843 +{
9844 +       int err;
9845 +       aufs_bindex_t btop, bbot;
9846 +
9847 +       err = 0;
9848 +       btop = au_dbtop(dentry);
9849 +       bbot = au_dbbot(dentry);
9850 +       if (btop >= 0)
9851 +               AuDebugOn(bbot < 0 && btop > bbot);
9852 +       else {
9853 +               err = -EIO;
9854 +               AuDebugOn(bbot >= 0);
9855 +       }
9856 +
9857 +       return err;
9858 +}
9859 +
9860 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9861 +{
9862 +       int err;
9863 +
9864 +       err = 0;
9865 +       if (unlikely(au_digen(dentry) != sigen
9866 +                    || au_iigen_test(d_inode(dentry), sigen)))
9867 +               err = -EIO;
9868 +
9869 +       return err;
9870 +}
9871 +
9872 +void au_update_digen(struct dentry *dentry)
9873 +{
9874 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9875 +       /* smp_mb(); */ /* atomic_set */
9876 +}
9877 +
9878 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9879 +{
9880 +       struct au_dinfo *dinfo;
9881 +       struct dentry *h_d;
9882 +       struct au_hdentry *hdp;
9883 +       aufs_bindex_t bindex, bbot;
9884 +
9885 +       DiMustWriteLock(dentry);
9886 +
9887 +       dinfo = au_di(dentry);
9888 +       if (!dinfo || dinfo->di_btop < 0)
9889 +               return;
9890 +
9891 +       if (do_put_zero) {
9892 +               bbot = dinfo->di_bbot;
9893 +               bindex = dinfo->di_btop;
9894 +               hdp = au_hdentry(dinfo, bindex);
9895 +               for (; bindex <= bbot; bindex++, hdp++) {
9896 +                       h_d = hdp->hd_dentry;
9897 +                       if (h_d && d_is_negative(h_d))
9898 +                               au_set_h_dptr(dentry, bindex, NULL);
9899 +               }
9900 +       }
9901 +
9902 +       dinfo->di_btop = 0;
9903 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9904 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9905 +               if (hdp->hd_dentry)
9906 +                       break;
9907 +       if (dinfo->di_btop > dinfo->di_bbot) {
9908 +               dinfo->di_btop = -1;
9909 +               dinfo->di_bbot = -1;
9910 +               return;
9911 +       }
9912 +
9913 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9914 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9915 +               if (hdp->hd_dentry)
9916 +                       break;
9917 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9918 +}
9919 +
9920 +void au_update_dbtop(struct dentry *dentry)
9921 +{
9922 +       aufs_bindex_t bindex, bbot;
9923 +       struct dentry *h_dentry;
9924 +
9925 +       bbot = au_dbbot(dentry);
9926 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9927 +               h_dentry = au_h_dptr(dentry, bindex);
9928 +               if (!h_dentry)
9929 +                       continue;
9930 +               if (d_is_positive(h_dentry)) {
9931 +                       au_set_dbtop(dentry, bindex);
9932 +                       return;
9933 +               }
9934 +               au_set_h_dptr(dentry, bindex, NULL);
9935 +       }
9936 +}
9937 +
9938 +void au_update_dbbot(struct dentry *dentry)
9939 +{
9940 +       aufs_bindex_t bindex, btop;
9941 +       struct dentry *h_dentry;
9942 +
9943 +       btop = au_dbtop(dentry);
9944 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9945 +               h_dentry = au_h_dptr(dentry, bindex);
9946 +               if (!h_dentry)
9947 +                       continue;
9948 +               if (d_is_positive(h_dentry)) {
9949 +                       au_set_dbbot(dentry, bindex);
9950 +                       return;
9951 +               }
9952 +               au_set_h_dptr(dentry, bindex, NULL);
9953 +       }
9954 +}
9955 +
9956 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9957 +{
9958 +       aufs_bindex_t bindex, bbot;
9959 +
9960 +       bbot = au_dbbot(dentry);
9961 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9962 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9963 +                       return bindex;
9964 +       return -1;
9965 +}
9966 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9967 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9968 +++ linux/fs/aufs/dir.c 2022-12-17 09:21:34.796521861 +0100
9969 @@ -0,0 +1,765 @@
9970 +// SPDX-License-Identifier: GPL-2.0
9971 +/*
9972 + * Copyright (C) 2005-2022 Junjiro R. Okajima
9973 + *
9974 + * This program is free software; you can redistribute it and/or modify
9975 + * it under the terms of the GNU General Public License as published by
9976 + * the Free Software Foundation; either version 2 of the License, or
9977 + * (at your option) any later version.
9978 + *
9979 + * This program is distributed in the hope that it will be useful,
9980 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9981 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9982 + * GNU General Public License for more details.
9983 + *
9984 + * You should have received a copy of the GNU General Public License
9985 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9986 + */
9987 +
9988 +/*
9989 + * directory operations
9990 + */
9991 +
9992 +#include <linux/fs_stack.h>
9993 +#include <linux/iversion.h>
9994 +#include "aufs.h"
9995 +
9996 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9997 +{
9998 +       unsigned int nlink;
9999 +
10000 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10001 +
10002 +       nlink = dir->i_nlink;
10003 +       nlink += h_dir->i_nlink - 2;
10004 +       if (h_dir->i_nlink < 2)
10005 +               nlink += 2;
10006 +       smp_mb(); /* for i_nlink */
10007 +       /* 0 can happen in revaliding */
10008 +       set_nlink(dir, nlink);
10009 +}
10010 +
10011 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10012 +{
10013 +       unsigned int nlink;
10014 +
10015 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10016 +
10017 +       nlink = dir->i_nlink;
10018 +       nlink -= h_dir->i_nlink - 2;
10019 +       if (h_dir->i_nlink < 2)
10020 +               nlink -= 2;
10021 +       smp_mb(); /* for i_nlink */
10022 +       /* nlink == 0 means the branch-fs is broken */
10023 +       set_nlink(dir, nlink);
10024 +}
10025 +
10026 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10027 +{
10028 +       loff_t sz;
10029 +       aufs_bindex_t bindex, bbot;
10030 +       struct file *h_file;
10031 +       struct dentry *h_dentry;
10032 +
10033 +       sz = 0;
10034 +       if (file) {
10035 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10036 +
10037 +               bbot = au_fbbot_dir(file);
10038 +               for (bindex = au_fbtop(file);
10039 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10040 +                    bindex++) {
10041 +                       h_file = au_hf_dir(file, bindex);
10042 +                       if (h_file && file_inode(h_file))
10043 +                               sz += vfsub_f_size_read(h_file);
10044 +               }
10045 +       } else {
10046 +               AuDebugOn(!dentry);
10047 +               AuDebugOn(!d_is_dir(dentry));
10048 +
10049 +               bbot = au_dbtaildir(dentry);
10050 +               for (bindex = au_dbtop(dentry);
10051 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10052 +                    bindex++) {
10053 +                       h_dentry = au_h_dptr(dentry, bindex);
10054 +                       if (h_dentry && d_is_positive(h_dentry))
10055 +                               sz += i_size_read(d_inode(h_dentry));
10056 +               }
10057 +       }
10058 +       if (sz < KMALLOC_MAX_SIZE)
10059 +               sz = roundup_pow_of_two(sz);
10060 +       if (sz > KMALLOC_MAX_SIZE)
10061 +               sz = KMALLOC_MAX_SIZE;
10062 +       else if (sz < NAME_MAX) {
10063 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10064 +               sz = AUFS_RDBLK_DEF;
10065 +       }
10066 +       return sz;
10067 +}
10068 +
10069 +struct au_dir_ts_arg {
10070 +       struct dentry *dentry;
10071 +       aufs_bindex_t brid;
10072 +};
10073 +
10074 +static void au_do_dir_ts(void *arg)
10075 +{
10076 +       struct au_dir_ts_arg *a = arg;
10077 +       struct au_dtime dt;
10078 +       struct path h_path;
10079 +       struct inode *dir, *h_dir;
10080 +       struct super_block *sb;
10081 +       struct au_branch *br;
10082 +       struct au_hinode *hdir;
10083 +       int err;
10084 +       aufs_bindex_t btop, bindex;
10085 +
10086 +       sb = a->dentry->d_sb;
10087 +       if (d_really_is_negative(a->dentry))
10088 +               goto out;
10089 +       /* no dir->i_mutex lock */
10090 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10091 +
10092 +       dir = d_inode(a->dentry);
10093 +       btop = au_ibtop(dir);
10094 +       bindex = au_br_index(sb, a->brid);
10095 +       if (bindex < btop)
10096 +               goto out_unlock;
10097 +
10098 +       br = au_sbr(sb, bindex);
10099 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10100 +       if (!h_path.dentry)
10101 +               goto out_unlock;
10102 +       h_path.mnt = au_br_mnt(br);
10103 +       au_dtime_store(&dt, a->dentry, &h_path);
10104 +
10105 +       br = au_sbr(sb, btop);
10106 +       if (!au_br_writable(br->br_perm))
10107 +               goto out_unlock;
10108 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10109 +       h_path.mnt = au_br_mnt(br);
10110 +       err = vfsub_mnt_want_write(h_path.mnt);
10111 +       if (err)
10112 +               goto out_unlock;
10113 +       hdir = au_hi(dir, btop);
10114 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10115 +       h_dir = au_h_iptr(dir, btop);
10116 +       if (h_dir->i_nlink
10117 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10118 +               dt.dt_h_path = h_path;
10119 +               au_dtime_revert(&dt);
10120 +       }
10121 +       au_hn_inode_unlock(hdir);
10122 +       vfsub_mnt_drop_write(h_path.mnt);
10123 +       au_cpup_attr_timesizes(dir);
10124 +
10125 +out_unlock:
10126 +       aufs_read_unlock(a->dentry, AuLock_DW);
10127 +out:
10128 +       dput(a->dentry);
10129 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10130 +       au_kfree_try_rcu(arg);
10131 +}
10132 +
10133 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10134 +{
10135 +       int perm, wkq_err;
10136 +       aufs_bindex_t btop;
10137 +       struct au_dir_ts_arg *arg;
10138 +       struct dentry *dentry;
10139 +       struct super_block *sb;
10140 +
10141 +       IMustLock(dir);
10142 +
10143 +       dentry = d_find_any_alias(dir);
10144 +       AuDebugOn(!dentry);
10145 +       sb = dentry->d_sb;
10146 +       btop = au_ibtop(dir);
10147 +       if (btop == bindex) {
10148 +               au_cpup_attr_timesizes(dir);
10149 +               goto out;
10150 +       }
10151 +
10152 +       perm = au_sbr_perm(sb, btop);
10153 +       if (!au_br_writable(perm))
10154 +               goto out;
10155 +
10156 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10157 +       if (!arg)
10158 +               goto out;
10159 +
10160 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10161 +       arg->brid = au_sbr_id(sb, bindex);
10162 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10163 +       if (unlikely(wkq_err)) {
10164 +               pr_err("wkq %d\n", wkq_err);
10165 +               dput(dentry);
10166 +               au_kfree_try_rcu(arg);
10167 +       }
10168 +
10169 +out:
10170 +       dput(dentry);
10171 +}
10172 +
10173 +/* ---------------------------------------------------------------------- */
10174 +
10175 +static int reopen_dir(struct file *file)
10176 +{
10177 +       int err;
10178 +       unsigned int flags;
10179 +       aufs_bindex_t bindex, btail, btop;
10180 +       struct dentry *dentry, *h_dentry;
10181 +       struct file *h_file;
10182 +
10183 +       /* open all lower dirs */
10184 +       dentry = file->f_path.dentry;
10185 +       btop = au_dbtop(dentry);
10186 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10187 +               au_set_h_fptr(file, bindex, NULL);
10188 +       au_set_fbtop(file, btop);
10189 +
10190 +       btail = au_dbtaildir(dentry);
10191 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10192 +               au_set_h_fptr(file, bindex, NULL);
10193 +       au_set_fbbot_dir(file, btail);
10194 +
10195 +       flags = vfsub_file_flags(file);
10196 +       for (bindex = btop; bindex <= btail; bindex++) {
10197 +               h_dentry = au_h_dptr(dentry, bindex);
10198 +               if (!h_dentry)
10199 +                       continue;
10200 +               h_file = au_hf_dir(file, bindex);
10201 +               if (h_file)
10202 +                       continue;
10203 +
10204 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10205 +               err = PTR_ERR(h_file);
10206 +               if (IS_ERR(h_file))
10207 +                       goto out; /* close all? */
10208 +               au_set_h_fptr(file, bindex, h_file);
10209 +       }
10210 +       au_update_figen(file);
10211 +       /* todo: necessary? */
10212 +       /* file->f_ra = h_file->f_ra; */
10213 +       err = 0;
10214 +
10215 +out:
10216 +       return err;
10217 +}
10218 +
10219 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10220 +{
10221 +       int err;
10222 +       aufs_bindex_t bindex, btail;
10223 +       struct dentry *dentry, *h_dentry;
10224 +       struct vfsmount *mnt;
10225 +
10226 +       FiMustWriteLock(file);
10227 +       AuDebugOn(h_file);
10228 +
10229 +       err = 0;
10230 +       mnt = file->f_path.mnt;
10231 +       dentry = file->f_path.dentry;
10232 +       file->f_version = inode_query_iversion(d_inode(dentry));
10233 +       bindex = au_dbtop(dentry);
10234 +       au_set_fbtop(file, bindex);
10235 +       btail = au_dbtaildir(dentry);
10236 +       au_set_fbbot_dir(file, btail);
10237 +       for (; !err && bindex <= btail; bindex++) {
10238 +               h_dentry = au_h_dptr(dentry, bindex);
10239 +               if (!h_dentry)
10240 +                       continue;
10241 +
10242 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10243 +               if (unlikely(err))
10244 +                       break;
10245 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10246 +               if (IS_ERR(h_file)) {
10247 +                       err = PTR_ERR(h_file);
10248 +                       break;
10249 +               }
10250 +               au_set_h_fptr(file, bindex, h_file);
10251 +       }
10252 +       au_update_figen(file);
10253 +       /* todo: necessary? */
10254 +       /* file->f_ra = h_file->f_ra; */
10255 +       if (!err)
10256 +               return 0; /* success */
10257 +
10258 +       /* close all */
10259 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10260 +               au_set_h_fptr(file, bindex, NULL);
10261 +       au_set_fbtop(file, -1);
10262 +       au_set_fbbot_dir(file, -1);
10263 +
10264 +       return err;
10265 +}
10266 +
10267 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10268 +                        struct file *file)
10269 +{
10270 +       int err;
10271 +       struct super_block *sb;
10272 +       struct au_fidir *fidir;
10273 +
10274 +       err = -ENOMEM;
10275 +       sb = file->f_path.dentry->d_sb;
10276 +       si_read_lock(sb, AuLock_FLUSH);
10277 +       fidir = au_fidir_alloc(sb);
10278 +       if (fidir) {
10279 +               struct au_do_open_args args = {
10280 +                       .open   = do_open_dir,
10281 +                       .fidir  = fidir
10282 +               };
10283 +               err = au_do_open(file, &args);
10284 +               if (unlikely(err))
10285 +                       au_kfree_rcu(fidir);
10286 +       }
10287 +       si_read_unlock(sb);
10288 +       return err;
10289 +}
10290 +
10291 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10292 +                           struct file *file)
10293 +{
10294 +       struct au_vdir *vdir_cache;
10295 +       struct au_finfo *finfo;
10296 +       struct au_fidir *fidir;
10297 +       struct au_hfile *hf;
10298 +       aufs_bindex_t bindex, bbot;
10299 +
10300 +       finfo = au_fi(file);
10301 +       fidir = finfo->fi_hdir;
10302 +       if (fidir) {
10303 +               au_hbl_del(&finfo->fi_hlist,
10304 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10305 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10306 +               if (vdir_cache)
10307 +                       au_vdir_free(vdir_cache);
10308 +
10309 +               bindex = finfo->fi_btop;
10310 +               if (bindex >= 0) {
10311 +                       hf = fidir->fd_hfile + bindex;
10312 +                       /*
10313 +                        * calls fput() instead of filp_close(),
10314 +                        * since no dnotify or lock for the lower file.
10315 +                        */
10316 +                       bbot = fidir->fd_bbot;
10317 +                       for (; bindex <= bbot; bindex++, hf++)
10318 +                               if (hf->hf_file)
10319 +                                       au_hfput(hf, /*execed*/0);
10320 +               }
10321 +               au_kfree_rcu(fidir);
10322 +               finfo->fi_hdir = NULL;
10323 +       }
10324 +       au_finfo_fin(file);
10325 +       return 0;
10326 +}
10327 +
10328 +/* ---------------------------------------------------------------------- */
10329 +
10330 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10331 +{
10332 +       int err;
10333 +       aufs_bindex_t bindex, bbot;
10334 +       struct file *h_file;
10335 +
10336 +       err = 0;
10337 +       bbot = au_fbbot_dir(file);
10338 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10339 +               h_file = au_hf_dir(file, bindex);
10340 +               if (h_file)
10341 +                       err = vfsub_flush(h_file, id);
10342 +       }
10343 +       return err;
10344 +}
10345 +
10346 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10347 +{
10348 +       return au_do_flush(file, id, au_do_flush_dir);
10349 +}
10350 +
10351 +/* ---------------------------------------------------------------------- */
10352 +
10353 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10354 +{
10355 +       int err;
10356 +       aufs_bindex_t bbot, bindex;
10357 +       struct inode *inode;
10358 +       struct super_block *sb;
10359 +
10360 +       err = 0;
10361 +       sb = dentry->d_sb;
10362 +       inode = d_inode(dentry);
10363 +       IMustLock(inode);
10364 +       bbot = au_dbbot(dentry);
10365 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10366 +               struct path h_path;
10367 +
10368 +               if (au_test_ro(sb, bindex, inode))
10369 +                       continue;
10370 +               h_path.dentry = au_h_dptr(dentry, bindex);
10371 +               if (!h_path.dentry)
10372 +                       continue;
10373 +
10374 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10375 +               err = vfsub_fsync(NULL, &h_path, datasync);
10376 +       }
10377 +
10378 +       return err;
10379 +}
10380 +
10381 +static int au_do_fsync_dir(struct file *file, int datasync)
10382 +{
10383 +       int err;
10384 +       aufs_bindex_t bbot, bindex;
10385 +       struct file *h_file;
10386 +       struct super_block *sb;
10387 +       struct inode *inode;
10388 +
10389 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10390 +       if (unlikely(err))
10391 +               goto out;
10392 +
10393 +       inode = file_inode(file);
10394 +       sb = inode->i_sb;
10395 +       bbot = au_fbbot_dir(file);
10396 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10397 +               h_file = au_hf_dir(file, bindex);
10398 +               if (!h_file || au_test_ro(sb, bindex, inode))
10399 +                       continue;
10400 +
10401 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10402 +       }
10403 +
10404 +out:
10405 +       return err;
10406 +}
10407 +
10408 +/*
10409 + * @file may be NULL
10410 + */
10411 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10412 +                         int datasync)
10413 +{
10414 +       int err;
10415 +       struct dentry *dentry;
10416 +       struct inode *inode;
10417 +       struct super_block *sb;
10418 +
10419 +       err = 0;
10420 +       dentry = file->f_path.dentry;
10421 +       inode = d_inode(dentry);
10422 +       inode_lock(inode);
10423 +       sb = dentry->d_sb;
10424 +       si_noflush_read_lock(sb);
10425 +       if (file)
10426 +               err = au_do_fsync_dir(file, datasync);
10427 +       else {
10428 +               di_write_lock_child(dentry);
10429 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10430 +       }
10431 +       au_cpup_attr_timesizes(inode);
10432 +       di_write_unlock(dentry);
10433 +       if (file)
10434 +               fi_write_unlock(file);
10435 +
10436 +       si_read_unlock(sb);
10437 +       inode_unlock(inode);
10438 +       return err;
10439 +}
10440 +
10441 +/* ---------------------------------------------------------------------- */
10442 +
10443 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10444 +{
10445 +       int err;
10446 +       struct dentry *dentry;
10447 +       struct inode *inode, *h_inode;
10448 +       struct super_block *sb;
10449 +
10450 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10451 +
10452 +       dentry = file->f_path.dentry;
10453 +       inode = d_inode(dentry);
10454 +       IMustLock(inode);
10455 +
10456 +       sb = dentry->d_sb;
10457 +       si_read_lock(sb, AuLock_FLUSH);
10458 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10459 +       if (unlikely(err))
10460 +               goto out;
10461 +       err = au_alive_dir(dentry);
10462 +       if (!err)
10463 +               err = au_vdir_init(file);
10464 +       di_downgrade_lock(dentry, AuLock_IR);
10465 +       if (unlikely(err))
10466 +               goto out_unlock;
10467 +
10468 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10469 +       if (!au_test_nfsd()) {
10470 +               err = au_vdir_fill_de(file, ctx);
10471 +               fsstack_copy_attr_atime(inode, h_inode);
10472 +       } else {
10473 +               /*
10474 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10475 +                * encode_fh() and others.
10476 +                */
10477 +               atomic_inc(&h_inode->i_count);
10478 +               di_read_unlock(dentry, AuLock_IR);
10479 +               si_read_unlock(sb);
10480 +               err = au_vdir_fill_de(file, ctx);
10481 +               fsstack_copy_attr_atime(inode, h_inode);
10482 +               fi_write_unlock(file);
10483 +               iput(h_inode);
10484 +
10485 +               AuTraceErr(err);
10486 +               return err;
10487 +       }
10488 +
10489 +out_unlock:
10490 +       di_read_unlock(dentry, AuLock_IR);
10491 +       fi_write_unlock(file);
10492 +out:
10493 +       si_read_unlock(sb);
10494 +       return err;
10495 +}
10496 +
10497 +/* ---------------------------------------------------------------------- */
10498 +
10499 +#define AuTestEmpty_WHONLY     1
10500 +#define AuTestEmpty_CALLED     (1 << 1)
10501 +#define AuTestEmpty_SHWH       (1 << 2)
10502 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10503 +#define au_fset_testempty(flags, name) \
10504 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10505 +#define au_fclr_testempty(flags, name) \
10506 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10507 +
10508 +#ifndef CONFIG_AUFS_SHWH
10509 +#undef AuTestEmpty_SHWH
10510 +#define AuTestEmpty_SHWH       0
10511 +#endif
10512 +
10513 +struct test_empty_arg {
10514 +       struct dir_context ctx;
10515 +       struct au_nhash *whlist;
10516 +       unsigned int flags;
10517 +       int err;
10518 +       aufs_bindex_t bindex;
10519 +};
10520 +
10521 +static bool test_empty_cb(struct dir_context *ctx, const char *__name,
10522 +                         int namelen, loff_t offset __maybe_unused, u64 ino,
10523 +                         unsigned int d_type)
10524 +{
10525 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10526 +                                                 ctx);
10527 +       char *name = (void *)__name;
10528 +
10529 +       arg->err = 0;
10530 +       au_fset_testempty(arg->flags, CALLED);
10531 +       /* smp_mb(); */
10532 +       if (name[0] == '.'
10533 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10534 +               goto out; /* success */
10535 +
10536 +       if (namelen <= AUFS_WH_PFX_LEN
10537 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10538 +               if (au_ftest_testempty(arg->flags, WHONLY)
10539 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10540 +                       arg->err = -ENOTEMPTY;
10541 +               goto out;
10542 +       }
10543 +
10544 +       name += AUFS_WH_PFX_LEN;
10545 +       namelen -= AUFS_WH_PFX_LEN;
10546 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10547 +               arg->err = au_nhash_append_wh
10548 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10549 +                        au_ftest_testempty(arg->flags, SHWH));
10550 +
10551 +out:
10552 +       /* smp_mb(); */
10553 +       AuTraceErr(arg->err);
10554 +       return !arg->err;
10555 +}
10556 +
10557 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10558 +{
10559 +       int err;
10560 +       struct file *h_file;
10561 +       struct au_branch *br;
10562 +
10563 +       h_file = au_h_open(dentry, arg->bindex,
10564 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10565 +                          /*file*/NULL, /*force_wr*/0);
10566 +       err = PTR_ERR(h_file);
10567 +       if (IS_ERR(h_file))
10568 +               goto out;
10569 +
10570 +       err = 0;
10571 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10572 +           && !file_inode(h_file)->i_nlink)
10573 +               goto out_put;
10574 +
10575 +       do {
10576 +               arg->err = 0;
10577 +               au_fclr_testempty(arg->flags, CALLED);
10578 +               /* smp_mb(); */
10579 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10580 +               if (err >= 0)
10581 +                       err = arg->err;
10582 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10583 +
10584 +out_put:
10585 +       fput(h_file);
10586 +       br = au_sbr(dentry->d_sb, arg->bindex);
10587 +       au_lcnt_dec(&br->br_nfiles);
10588 +out:
10589 +       return err;
10590 +}
10591 +
10592 +struct do_test_empty_args {
10593 +       int *errp;
10594 +       struct dentry *dentry;
10595 +       struct test_empty_arg *arg;
10596 +};
10597 +
10598 +static void call_do_test_empty(void *args)
10599 +{
10600 +       struct do_test_empty_args *a = args;
10601 +       *a->errp = do_test_empty(a->dentry, a->arg);
10602 +}
10603 +
10604 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10605 +{
10606 +       int err, wkq_err;
10607 +       struct dentry *h_dentry;
10608 +       struct inode *h_inode;
10609 +       struct user_namespace *h_userns;
10610 +
10611 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10612 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10613 +       h_inode = d_inode(h_dentry);
10614 +       /* todo: i_mode changes anytime? */
10615 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10616 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10617 +       inode_unlock_shared(h_inode);
10618 +       if (!err)
10619 +               err = do_test_empty(dentry, arg);
10620 +       else {
10621 +               struct do_test_empty_args args = {
10622 +                       .errp   = &err,
10623 +                       .dentry = dentry,
10624 +                       .arg    = arg
10625 +               };
10626 +               unsigned int flags = arg->flags;
10627 +
10628 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10629 +               if (unlikely(wkq_err))
10630 +                       err = wkq_err;
10631 +               arg->flags = flags;
10632 +       }
10633 +
10634 +       return err;
10635 +}
10636 +
10637 +int au_test_empty_lower(struct dentry *dentry)
10638 +{
10639 +       int err;
10640 +       unsigned int rdhash;
10641 +       aufs_bindex_t bindex, btop, btail;
10642 +       struct au_nhash whlist;
10643 +       struct test_empty_arg arg = {
10644 +               .ctx = {
10645 +                       .actor = test_empty_cb
10646 +               }
10647 +       };
10648 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10649 +
10650 +       SiMustAnyLock(dentry->d_sb);
10651 +
10652 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10653 +       if (!rdhash)
10654 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10655 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10656 +       if (unlikely(err))
10657 +               goto out;
10658 +
10659 +       arg.flags = 0;
10660 +       arg.whlist = &whlist;
10661 +       btop = au_dbtop(dentry);
10662 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10663 +               au_fset_testempty(arg.flags, SHWH);
10664 +       test_empty = do_test_empty;
10665 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10666 +               test_empty = sio_test_empty;
10667 +       arg.bindex = btop;
10668 +       err = test_empty(dentry, &arg);
10669 +       if (unlikely(err))
10670 +               goto out_whlist;
10671 +
10672 +       au_fset_testempty(arg.flags, WHONLY);
10673 +       btail = au_dbtaildir(dentry);
10674 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10675 +               struct dentry *h_dentry;
10676 +
10677 +               h_dentry = au_h_dptr(dentry, bindex);
10678 +               if (h_dentry && d_is_positive(h_dentry)) {
10679 +                       arg.bindex = bindex;
10680 +                       err = test_empty(dentry, &arg);
10681 +               }
10682 +       }
10683 +
10684 +out_whlist:
10685 +       au_nhash_wh_free(&whlist);
10686 +out:
10687 +       return err;
10688 +}
10689 +
10690 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10691 +{
10692 +       int err;
10693 +       struct test_empty_arg arg = {
10694 +               .ctx = {
10695 +                       .actor = test_empty_cb
10696 +               }
10697 +       };
10698 +       aufs_bindex_t bindex, btail;
10699 +
10700 +       err = 0;
10701 +       arg.whlist = whlist;
10702 +       arg.flags = AuTestEmpty_WHONLY;
10703 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10704 +               au_fset_testempty(arg.flags, SHWH);
10705 +       btail = au_dbtaildir(dentry);
10706 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10707 +               struct dentry *h_dentry;
10708 +
10709 +               h_dentry = au_h_dptr(dentry, bindex);
10710 +               if (h_dentry && d_is_positive(h_dentry)) {
10711 +                       arg.bindex = bindex;
10712 +                       err = sio_test_empty(dentry, &arg);
10713 +               }
10714 +       }
10715 +
10716 +       return err;
10717 +}
10718 +
10719 +/* ---------------------------------------------------------------------- */
10720 +
10721 +const struct file_operations aufs_dir_fop = {
10722 +       .owner          = THIS_MODULE,
10723 +       .llseek         = default_llseek,
10724 +       .read           = generic_read_dir,
10725 +       .iterate_shared = aufs_iterate_shared,
10726 +       .unlocked_ioctl = aufs_ioctl_dir,
10727 +#ifdef CONFIG_COMPAT
10728 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10729 +#endif
10730 +       .open           = aufs_open_dir,
10731 +       .release        = aufs_release_dir,
10732 +       .flush          = aufs_flush_dir,
10733 +       .fsync          = aufs_fsync_dir
10734 +};
10735 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10736 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10737 +++ linux/fs/aufs/dir.h 2022-11-05 23:02:18.962555950 +0100
10738 @@ -0,0 +1,134 @@
10739 +/* SPDX-License-Identifier: GPL-2.0 */
10740 +/*
10741 + * Copyright (C) 2005-2022 Junjiro R. Okajima
10742 + *
10743 + * This program is free software; you can redistribute it and/or modify
10744 + * it under the terms of the GNU General Public License as published by
10745 + * the Free Software Foundation; either version 2 of the License, or
10746 + * (at your option) any later version.
10747 + *
10748 + * This program is distributed in the hope that it will be useful,
10749 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10750 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10751 + * GNU General Public License for more details.
10752 + *
10753 + * You should have received a copy of the GNU General Public License
10754 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10755 + */
10756 +
10757 +/*
10758 + * directory operations
10759 + */
10760 +
10761 +#ifndef __AUFS_DIR_H__
10762 +#define __AUFS_DIR_H__
10763 +
10764 +#ifdef __KERNEL__
10765 +
10766 +#include <linux/fs.h>
10767 +
10768 +/* ---------------------------------------------------------------------- */
10769 +
10770 +/* need to be faster and smaller */
10771 +
10772 +struct au_nhash {
10773 +       unsigned int            nh_num;
10774 +       struct hlist_head       *nh_head;
10775 +};
10776 +
10777 +struct au_vdir_destr {
10778 +       unsigned char   len;
10779 +       unsigned char   name[];
10780 +} __packed;
10781 +
10782 +struct au_vdir_dehstr {
10783 +       struct hlist_node       hash;
10784 +       struct au_vdir_destr    *str;
10785 +       struct rcu_head         rcu;
10786 +} ____cacheline_aligned_in_smp;
10787 +
10788 +struct au_vdir_de {
10789 +       ino_t                   de_ino;
10790 +       unsigned char           de_type;
10791 +       /* caution: packed */
10792 +       struct au_vdir_destr    de_str;
10793 +} __packed;
10794 +
10795 +struct au_vdir_wh {
10796 +       struct hlist_node       wh_hash;
10797 +#ifdef CONFIG_AUFS_SHWH
10798 +       ino_t                   wh_ino;
10799 +       aufs_bindex_t           wh_bindex;
10800 +       unsigned char           wh_type;
10801 +#else
10802 +       aufs_bindex_t           wh_bindex;
10803 +#endif
10804 +       /* caution: packed */
10805 +       struct au_vdir_destr    wh_str;
10806 +} __packed;
10807 +
10808 +union au_vdir_deblk_p {
10809 +       unsigned char           *deblk;
10810 +       struct au_vdir_de       *de;
10811 +};
10812 +
10813 +struct au_vdir {
10814 +       unsigned char   **vd_deblk;
10815 +       unsigned long   vd_nblk;
10816 +       struct {
10817 +               unsigned long           ul;
10818 +               union au_vdir_deblk_p   p;
10819 +       } vd_last;
10820 +
10821 +       u64             vd_version;
10822 +       unsigned int    vd_deblk_sz;
10823 +       unsigned long   vd_jiffy;
10824 +       struct rcu_head rcu;
10825 +} ____cacheline_aligned_in_smp;
10826 +
10827 +/* ---------------------------------------------------------------------- */
10828 +
10829 +/* dir.c */
10830 +extern const struct file_operations aufs_dir_fop;
10831 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10832 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10833 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10834 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10835 +int au_test_empty_lower(struct dentry *dentry);
10836 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10837 +
10838 +/* vdir.c */
10839 +unsigned int au_rdhash_est(loff_t sz);
10840 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10841 +void au_nhash_wh_free(struct au_nhash *whlist);
10842 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10843 +                           int limit);
10844 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10845 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10846 +                      unsigned int d_type, aufs_bindex_t bindex,
10847 +                      unsigned char shwh);
10848 +void au_vdir_free(struct au_vdir *vdir);
10849 +int au_vdir_init(struct file *file);
10850 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10851 +
10852 +/* ioctl.c */
10853 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10854 +
10855 +#ifdef CONFIG_AUFS_RDU
10856 +/* rdu.c */
10857 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10858 +#ifdef CONFIG_COMPAT
10859 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10860 +                        unsigned long arg);
10861 +#endif
10862 +#else
10863 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10864 +       unsigned int cmd, unsigned long arg)
10865 +#ifdef CONFIG_COMPAT
10866 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10867 +       unsigned int cmd, unsigned long arg)
10868 +#endif
10869 +#endif
10870 +
10871 +#endif /* __KERNEL__ */
10872 +#endif /* __AUFS_DIR_H__ */
10873 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10874 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10875 +++ linux/fs/aufs/dirren.c      2022-11-05 23:02:18.962555950 +0100
10876 @@ -0,0 +1,1315 @@
10877 +// SPDX-License-Identifier: GPL-2.0
10878 +/*
10879 + * Copyright (C) 2017-2022 Junjiro R. Okajima
10880 + *
10881 + * This program is free software; you can redistribute it and/or modify
10882 + * it under the terms of the GNU General Public License as published by
10883 + * the Free Software Foundation; either version 2 of the License, or
10884 + * (at your option) any later version.
10885 + *
10886 + * This program is distributed in the hope that it will be useful,
10887 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10888 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10889 + * GNU General Public License for more details.
10890 + *
10891 + * You should have received a copy of the GNU General Public License
10892 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10893 + */
10894 +
10895 +/*
10896 + * special handling in renaming a directory
10897 + * in order to support looking-up the before-renamed name on the lower readonly
10898 + * branches
10899 + */
10900 +
10901 +#include <linux/byteorder/generic.h>
10902 +#include "aufs.h"
10903 +
10904 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10905 +{
10906 +       int idx;
10907 +
10908 +       idx = au_dr_ihash(ent->dr_h_ino);
10909 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10910 +}
10911 +
10912 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10913 +{
10914 +       int ret, i;
10915 +       struct hlist_bl_head *hbl;
10916 +
10917 +       ret = 1;
10918 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10919 +               hbl = dr->dr_h_ino + i;
10920 +               hlist_bl_lock(hbl);
10921 +               ret &= hlist_bl_empty(hbl);
10922 +               hlist_bl_unlock(hbl);
10923 +       }
10924 +
10925 +       return ret;
10926 +}
10927 +
10928 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10929 +{
10930 +       struct au_dr_hino *found, *ent;
10931 +       struct hlist_bl_head *hbl;
10932 +       struct hlist_bl_node *pos;
10933 +       int idx;
10934 +
10935 +       found = NULL;
10936 +       idx = au_dr_ihash(ino);
10937 +       hbl = dr->dr_h_ino + idx;
10938 +       hlist_bl_lock(hbl);
10939 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10940 +               if (ent->dr_h_ino == ino) {
10941 +                       found = ent;
10942 +                       break;
10943 +               }
10944 +       hlist_bl_unlock(hbl);
10945 +
10946 +       return found;
10947 +}
10948 +
10949 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10950 +                       struct au_dr_hino *add_ent)
10951 +{
10952 +       int found, idx;
10953 +       struct hlist_bl_head *hbl;
10954 +       struct hlist_bl_node *pos;
10955 +       struct au_dr_hino *ent;
10956 +
10957 +       found = 0;
10958 +       idx = au_dr_ihash(ino);
10959 +       hbl = dr->dr_h_ino + idx;
10960 +#if 0 /* debug print */
10961 +       {
10962 +               struct hlist_bl_node *tmp;
10963 +
10964 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10965 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10966 +       }
10967 +#endif
10968 +       hlist_bl_lock(hbl);
10969 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10970 +               if (ent->dr_h_ino == ino) {
10971 +                       found = 1;
10972 +                       break;
10973 +               }
10974 +       if (!found && add_ent)
10975 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10976 +       hlist_bl_unlock(hbl);
10977 +
10978 +       if (!found && add_ent)
10979 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10980 +
10981 +       return found;
10982 +}
10983 +
10984 +void au_dr_hino_free(struct au_dr_br *dr)
10985 +{
10986 +       int i;
10987 +       struct hlist_bl_head *hbl;
10988 +       struct hlist_bl_node *pos, *tmp;
10989 +       struct au_dr_hino *ent;
10990 +
10991 +       /* SiMustWriteLock(sb); */
10992 +
10993 +       for (i = 0; i < AuDirren_NHASH; i++) {
10994 +               hbl = dr->dr_h_ino + i;
10995 +               /* no spinlock since sbinfo must be write-locked */
10996 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10997 +                       au_kfree_rcu(ent);
10998 +               INIT_HLIST_BL_HEAD(hbl);
10999 +       }
11000 +}
11001 +
11002 +/* returns the number of inodes or an error */
11003 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11004 +                           struct file *hinofile)
11005 +{
11006 +       int err, i;
11007 +       ssize_t ssz;
11008 +       loff_t pos, oldsize;
11009 +       __be64 u64;
11010 +       struct inode *hinoinode;
11011 +       struct hlist_bl_head *hbl;
11012 +       struct hlist_bl_node *n1, *n2;
11013 +       struct au_dr_hino *ent;
11014 +
11015 +       SiMustWriteLock(sb);
11016 +       AuDebugOn(!au_br_writable(br->br_perm));
11017 +
11018 +       hinoinode = file_inode(hinofile);
11019 +       oldsize = i_size_read(hinoinode);
11020 +
11021 +       err = 0;
11022 +       pos = 0;
11023 +       hbl = br->br_dirren.dr_h_ino;
11024 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11025 +               /* no bit-lock since sbinfo must be write-locked */
11026 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11027 +                       AuDbg("hi%llu, %pD2\n",
11028 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11029 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11030 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11031 +                       if (ssz == sizeof(u64))
11032 +                               continue;
11033 +
11034 +                       /* write error */
11035 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11036 +                       err = -ENOSPC;
11037 +                       if (ssz < 0)
11038 +                               err = ssz;
11039 +                       break;
11040 +               }
11041 +       }
11042 +       /* regardless the error */
11043 +       if (pos < oldsize) {
11044 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11045 +               AuTraceErr(err);
11046 +       }
11047 +
11048 +       AuTraceErr(err);
11049 +       return err;
11050 +}
11051 +
11052 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11053 +{
11054 +       int err, hidx;
11055 +       ssize_t ssz;
11056 +       size_t sz, n;
11057 +       loff_t pos;
11058 +       uint64_t u64;
11059 +       struct au_dr_hino *ent;
11060 +       struct inode *hinoinode;
11061 +       struct hlist_bl_head *hbl;
11062 +
11063 +       err = 0;
11064 +       pos = 0;
11065 +       hbl = dr->dr_h_ino;
11066 +       hinoinode = file_inode(hinofile);
11067 +       sz = i_size_read(hinoinode);
11068 +       AuDebugOn(sz % sizeof(u64));
11069 +       n = sz / sizeof(u64);
11070 +       while (n--) {
11071 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11072 +               if (unlikely(ssz != sizeof(u64))) {
11073 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11074 +                       err = -EINVAL;
11075 +                       if (ssz < 0)
11076 +                               err = ssz;
11077 +                       goto out_free;
11078 +               }
11079 +
11080 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11081 +               if (!ent) {
11082 +                       err = -ENOMEM;
11083 +                       AuTraceErr(err);
11084 +                       goto out_free;
11085 +               }
11086 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11087 +               AuDbg("hi%llu, %pD2\n",
11088 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11089 +               hidx = au_dr_ihash(ent->dr_h_ino);
11090 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11091 +       }
11092 +       goto out; /* success */
11093 +
11094 +out_free:
11095 +       au_dr_hino_free(dr);
11096 +out:
11097 +       AuTraceErr(err);
11098 +       return err;
11099 +}
11100 +
11101 +/*
11102 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11103 + * @path is a switch to distinguish load and store.
11104 + */
11105 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11106 +                     struct au_branch *br, const struct path *path)
11107 +{
11108 +       int err, flags;
11109 +       unsigned char load, suspend;
11110 +       struct file *hinofile;
11111 +       struct au_hinode *hdir;
11112 +       struct inode *dir, *delegated;
11113 +       struct path hinopath;
11114 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11115 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11116 +
11117 +       AuDebugOn(bindex < 0 && !br);
11118 +       AuDebugOn(bindex >= 0 && br);
11119 +
11120 +       err = -EINVAL;
11121 +       suspend = !br;
11122 +       if (suspend)
11123 +               br = au_sbr(sb, bindex);
11124 +       load = !!path;
11125 +       if (!load) {
11126 +               path = &br->br_path;
11127 +               AuDebugOn(!au_br_writable(br->br_perm));
11128 +               if (unlikely(!au_br_writable(br->br_perm)))
11129 +                       goto out;
11130 +       }
11131 +
11132 +       hdir = NULL;
11133 +       if (suspend) {
11134 +               dir = d_inode(sb->s_root);
11135 +               hdir = au_hinode(au_ii(dir), bindex);
11136 +               dir = hdir->hi_inode;
11137 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11138 +       } else {
11139 +               dir = d_inode(path->dentry);
11140 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11141 +       }
11142 +       hinopath.mnt = path->mnt;
11143 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11144 +       err = PTR_ERR(hinopath.dentry);
11145 +       if (IS_ERR(hinopath.dentry))
11146 +               goto out_unlock;
11147 +
11148 +       err = 0;
11149 +       flags = O_RDONLY;
11150 +       if (load) {
11151 +               if (d_is_negative(hinopath.dentry))
11152 +                       goto out_dput; /* success */
11153 +       } else {
11154 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11155 +                       if (d_is_positive(hinopath.dentry)) {
11156 +                               delegated = NULL;
11157 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11158 +                                                  /*force*/0);
11159 +                               AuTraceErr(err);
11160 +                               if (unlikely(err))
11161 +                                       pr_err("ignored err %d, %pd2\n",
11162 +                                              err, hinopath.dentry);
11163 +                               if (unlikely(err == -EWOULDBLOCK))
11164 +                                       iput(delegated);
11165 +                               err = 0;
11166 +                       }
11167 +                       goto out_dput;
11168 +               } else if (!d_is_positive(hinopath.dentry)) {
11169 +                       err = vfsub_create(dir, &hinopath, 0600,
11170 +                                          /*want_excl*/false);
11171 +                       AuTraceErr(err);
11172 +                       if (unlikely(err))
11173 +                               goto out_dput;
11174 +               }
11175 +               flags = O_WRONLY;
11176 +       }
11177 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11178 +       if (suspend)
11179 +               au_hn_inode_unlock(hdir);
11180 +       else
11181 +               inode_unlock(dir);
11182 +       dput(hinopath.dentry);
11183 +       AuTraceErrPtr(hinofile);
11184 +       if (IS_ERR(hinofile)) {
11185 +               err = PTR_ERR(hinofile);
11186 +               goto out;
11187 +       }
11188 +
11189 +       if (load)
11190 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11191 +       else
11192 +               err = au_dr_hino_store(sb, br, hinofile);
11193 +       fput(hinofile);
11194 +       goto out;
11195 +
11196 +out_dput:
11197 +       dput(hinopath.dentry);
11198 +out_unlock:
11199 +       if (suspend)
11200 +               au_hn_inode_unlock(hdir);
11201 +       else
11202 +               inode_unlock(dir);
11203 +out:
11204 +       AuTraceErr(err);
11205 +       return err;
11206 +}
11207 +
11208 +/* ---------------------------------------------------------------------- */
11209 +
11210 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11211 +{
11212 +       int err;
11213 +       struct kstatfs kstfs;
11214 +       dev_t dev;
11215 +       struct dentry *dentry;
11216 +       struct super_block *sb;
11217 +
11218 +       err = vfs_statfs((void *)path, &kstfs);
11219 +       AuTraceErr(err);
11220 +       if (unlikely(err))
11221 +               goto out;
11222 +
11223 +       /* todo: support for UUID */
11224 +
11225 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11226 +               brid->type = AuBrid_FSID;
11227 +               brid->fsid = kstfs.f_fsid;
11228 +       } else {
11229 +               dentry = path->dentry;
11230 +               sb = dentry->d_sb;
11231 +               dev = sb->s_dev;
11232 +               if (dev) {
11233 +                       brid->type = AuBrid_DEV;
11234 +                       brid->dev = dev;
11235 +               }
11236 +       }
11237 +
11238 +out:
11239 +       return err;
11240 +}
11241 +
11242 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11243 +                 const struct path *path)
11244 +{
11245 +       int err, i;
11246 +       struct au_dr_br *dr;
11247 +       struct hlist_bl_head *hbl;
11248 +
11249 +       dr = &br->br_dirren;
11250 +       hbl = dr->dr_h_ino;
11251 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11252 +               INIT_HLIST_BL_HEAD(hbl);
11253 +
11254 +       err = au_dr_brid_init(&dr->dr_brid, path);
11255 +       if (unlikely(err))
11256 +               goto out;
11257 +
11258 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11259 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11260 +
11261 +out:
11262 +       AuTraceErr(err);
11263 +       return err;
11264 +}
11265 +
11266 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11267 +{
11268 +       int err;
11269 +
11270 +       err = 0;
11271 +       if (au_br_writable(br->br_perm))
11272 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11273 +       if (!err)
11274 +               au_dr_hino_free(&br->br_dirren);
11275 +
11276 +       return err;
11277 +}
11278 +
11279 +/* ---------------------------------------------------------------------- */
11280 +
11281 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11282 +                      char *buf, size_t sz)
11283 +{
11284 +       int err;
11285 +       unsigned int major, minor;
11286 +       char *p;
11287 +
11288 +       p = buf;
11289 +       err = snprintf(p, sz, "%d_", brid->type);
11290 +       AuDebugOn(err > sz);
11291 +       p += err;
11292 +       sz -= err;
11293 +       switch (brid->type) {
11294 +       case AuBrid_Unset:
11295 +               return -EINVAL;
11296 +       case AuBrid_UUID:
11297 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11298 +               break;
11299 +       case AuBrid_FSID:
11300 +               err = snprintf(p, sz, "%08x-%08x",
11301 +                              brid->fsid.val[0], brid->fsid.val[1]);
11302 +               break;
11303 +       case AuBrid_DEV:
11304 +               major = MAJOR(brid->dev);
11305 +               minor = MINOR(brid->dev);
11306 +               if (major <= 0xff && minor <= 0xff)
11307 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11308 +               else
11309 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11310 +               break;
11311 +       }
11312 +       AuDebugOn(err > sz);
11313 +       p += err;
11314 +       sz -= err;
11315 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11316 +       AuDebugOn(err > sz);
11317 +       p += err;
11318 +       sz -= err;
11319 +
11320 +       return p - buf;
11321 +}
11322 +
11323 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11324 +{
11325 +       int rlen;
11326 +       struct dentry *br_dentry;
11327 +       struct inode *br_inode;
11328 +
11329 +       br_dentry = au_br_dentry(br);
11330 +       br_inode = d_inode(br_dentry);
11331 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11332 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11333 +       AuDebugOn(rlen > len);
11334 +
11335 +       return rlen;
11336 +}
11337 +
11338 +/* ---------------------------------------------------------------------- */
11339 +
11340 +/*
11341 + * from the given @h_dentry, construct drinfo at @*fdata.
11342 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11343 + * @allocated.
11344 + */
11345 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11346 +                              struct dentry *h_dentry,
11347 +                              unsigned char *allocated)
11348 +{
11349 +       int err, v;
11350 +       struct au_drinfo_fdata *f, *p;
11351 +       struct au_drinfo *drinfo;
11352 +       struct inode *h_inode;
11353 +       struct qstr *qname;
11354 +
11355 +       err = 0;
11356 +       f = *fdata;
11357 +       h_inode = d_inode(h_dentry);
11358 +       qname = &h_dentry->d_name;
11359 +       drinfo = &f->drinfo;
11360 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11361 +       drinfo->oldnamelen = qname->len;
11362 +       if (*allocated < sizeof(*f) + qname->len) {
11363 +               v = roundup_pow_of_two(*allocated + qname->len);
11364 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11365 +               if (unlikely(!p)) {
11366 +                       err = -ENOMEM;
11367 +                       AuTraceErr(err);
11368 +                       goto out;
11369 +               }
11370 +               f = p;
11371 +               *fdata = f;
11372 +               *allocated = v;
11373 +               drinfo = &f->drinfo;
11374 +       }
11375 +       memcpy(drinfo->oldname, qname->name, qname->len);
11376 +       AuDbg("i%llu, %.*s\n",
11377 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11378 +             drinfo->oldname);
11379 +
11380 +out:
11381 +       AuTraceErr(err);
11382 +       return err;
11383 +}
11384 +
11385 +/* callers have to free the return value */
11386 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11387 +{
11388 +       struct au_drinfo *ret, *drinfo;
11389 +       struct au_drinfo_fdata fdata;
11390 +       int len;
11391 +       loff_t pos;
11392 +       ssize_t ssz;
11393 +
11394 +       ret = ERR_PTR(-EIO);
11395 +       pos = 0;
11396 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11397 +       if (unlikely(ssz != sizeof(fdata))) {
11398 +               AuIOErr("ssz %zd, %u, %pD2\n",
11399 +                       ssz, (unsigned int)sizeof(fdata), file);
11400 +               goto out;
11401 +       }
11402 +
11403 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11404 +       switch (fdata.magic) {
11405 +       case AUFS_DRINFO_MAGIC_V1:
11406 +               break;
11407 +       default:
11408 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11409 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11410 +               goto out;
11411 +       }
11412 +
11413 +       drinfo = &fdata.drinfo;
11414 +       len = drinfo->oldnamelen;
11415 +       if (!len) {
11416 +               AuIOErr("broken drinfo %pD2\n", file);
11417 +               goto out;
11418 +       }
11419 +
11420 +       ret = NULL;
11421 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11422 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11423 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11424 +                     (unsigned long long)drinfo->ino,
11425 +                     (unsigned long long)h_ino, file);
11426 +               goto out; /* success */
11427 +       }
11428 +
11429 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11430 +       if (unlikely(!ret)) {
11431 +               ret = ERR_PTR(-ENOMEM);
11432 +               AuTraceErrPtr(ret);
11433 +               goto out;
11434 +       }
11435 +
11436 +       *ret = *drinfo;
11437 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11438 +       if (unlikely(ssz != len)) {
11439 +               au_kfree_rcu(ret);
11440 +               ret = ERR_PTR(-EIO);
11441 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11442 +               goto out;
11443 +       }
11444 +
11445 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11446 +
11447 +out:
11448 +       return ret;
11449 +}
11450 +
11451 +/* ---------------------------------------------------------------------- */
11452 +
11453 +/* in order to be revertible */
11454 +struct au_drinfo_rev_elm {
11455 +       int                     created;
11456 +       struct dentry           *info_dentry;
11457 +       struct au_drinfo        *info_last;
11458 +};
11459 +
11460 +struct au_drinfo_rev {
11461 +       unsigned char                   already;
11462 +       aufs_bindex_t                   nelm;
11463 +       struct au_drinfo_rev_elm        elm[];
11464 +};
11465 +
11466 +/* todo: isn't it too large? */
11467 +struct au_drinfo_store {
11468 +       struct path h_ppath;
11469 +       struct dentry *h_dentry;
11470 +       struct au_drinfo_fdata *fdata;
11471 +       char *infoname;                 /* inside of whname, just after PFX */
11472 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11473 +       aufs_bindex_t btgt, btail;
11474 +       unsigned char no_sio,
11475 +               allocated,              /* current size of *fdata */
11476 +               infonamelen,            /* room size for p */
11477 +               whnamelen,              /* length of the generated name */
11478 +               renameback;             /* renamed back */
11479 +};
11480 +
11481 +/* on rename(2) error, the caller should revert it using @elm */
11482 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11483 +                             struct au_drinfo_rev_elm *elm)
11484 +{
11485 +       int err, len;
11486 +       ssize_t ssz;
11487 +       loff_t pos;
11488 +       struct path infopath = {
11489 +               .mnt = w->h_ppath.mnt
11490 +       };
11491 +       struct inode *h_dir, *h_inode, *delegated;
11492 +       struct file *infofile;
11493 +       struct qstr *qname;
11494 +
11495 +       AuDebugOn(elm
11496 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11497 +
11498 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11499 +                                              w->whnamelen);
11500 +       AuTraceErrPtr(infopath.dentry);
11501 +       if (IS_ERR(infopath.dentry)) {
11502 +               err = PTR_ERR(infopath.dentry);
11503 +               goto out;
11504 +       }
11505 +
11506 +       err = 0;
11507 +       h_dir = d_inode(w->h_ppath.dentry);
11508 +       if (elm && d_is_negative(infopath.dentry)) {
11509 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11510 +               AuTraceErr(err);
11511 +               if (unlikely(err))
11512 +                       goto out_dput;
11513 +               elm->created = 1;
11514 +               elm->info_dentry = dget(infopath.dentry);
11515 +       }
11516 +
11517 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11518 +       AuTraceErrPtr(infofile);
11519 +       if (IS_ERR(infofile)) {
11520 +               err = PTR_ERR(infofile);
11521 +               goto out_dput;
11522 +       }
11523 +
11524 +       h_inode = d_inode(infopath.dentry);
11525 +       if (elm && i_size_read(h_inode)) {
11526 +               h_inode = d_inode(w->h_dentry);
11527 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11528 +               AuTraceErrPtr(elm->info_last);
11529 +               if (IS_ERR(elm->info_last)) {
11530 +                       err = PTR_ERR(elm->info_last);
11531 +                       elm->info_last = NULL;
11532 +                       AuDebugOn(elm->info_dentry);
11533 +                       goto out_fput;
11534 +               }
11535 +       }
11536 +
11537 +       if (elm && w->renameback) {
11538 +               delegated = NULL;
11539 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11540 +               AuTraceErr(err);
11541 +               if (unlikely(err == -EWOULDBLOCK))
11542 +                       iput(delegated);
11543 +               goto out_fput;
11544 +       }
11545 +
11546 +       pos = 0;
11547 +       qname = &w->h_dentry->d_name;
11548 +       len = sizeof(*w->fdata) + qname->len;
11549 +       if (!elm)
11550 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11551 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11552 +       if (ssz == len) {
11553 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11554 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11555 +               goto out_fput; /* success */
11556 +       } else {
11557 +               err = -EIO;
11558 +               if (ssz < 0)
11559 +                       err = ssz;
11560 +               /* the caller should revert it using @elm */
11561 +       }
11562 +
11563 +out_fput:
11564 +       fput(infofile);
11565 +out_dput:
11566 +       dput(infopath.dentry);
11567 +out:
11568 +       AuTraceErr(err);
11569 +       return err;
11570 +}
11571 +
11572 +struct au_call_drinfo_do_store_args {
11573 +       int *errp;
11574 +       struct au_drinfo_store *w;
11575 +       struct au_drinfo_rev_elm *elm;
11576 +};
11577 +
11578 +static void au_call_drinfo_do_store(void *args)
11579 +{
11580 +       struct au_call_drinfo_do_store_args *a = args;
11581 +
11582 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11583 +}
11584 +
11585 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11586 +                              struct au_drinfo_rev_elm *elm)
11587 +{
11588 +       int err, wkq_err;
11589 +
11590 +       if (w->no_sio)
11591 +               err = au_drinfo_do_store(w, elm);
11592 +       else {
11593 +               struct au_call_drinfo_do_store_args a = {
11594 +                       .errp   = &err,
11595 +                       .w      = w,
11596 +                       .elm    = elm
11597 +               };
11598 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11599 +               if (unlikely(wkq_err))
11600 +                       err = wkq_err;
11601 +       }
11602 +       AuTraceErr(err);
11603 +
11604 +       return err;
11605 +}
11606 +
11607 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11608 +                                    aufs_bindex_t btgt)
11609 +{
11610 +       int err;
11611 +
11612 +       memset(w, 0, sizeof(*w));
11613 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11614 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11615 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11616 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11617 +       w->btgt = btgt;
11618 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11619 +
11620 +       err = -ENOMEM;
11621 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11622 +       if (unlikely(!w->fdata)) {
11623 +               AuTraceErr(err);
11624 +               goto out;
11625 +       }
11626 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11627 +       err = 0;
11628 +
11629 +out:
11630 +       return err;
11631 +}
11632 +
11633 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11634 +{
11635 +       au_kfree_rcu(w->fdata);
11636 +}
11637 +
11638 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11639 +                               struct au_drinfo_store *w)
11640 +{
11641 +       struct au_drinfo_rev_elm *elm;
11642 +       struct inode *h_dir, *delegated;
11643 +       int err, nelm;
11644 +       struct path infopath = {
11645 +               .mnt = w->h_ppath.mnt
11646 +       };
11647 +
11648 +       h_dir = d_inode(w->h_ppath.dentry);
11649 +       IMustLock(h_dir);
11650 +
11651 +       err = 0;
11652 +       elm = rev->elm;
11653 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11654 +               AuDebugOn(elm->created && elm->info_last);
11655 +               if (elm->created) {
11656 +                       AuDbg("here\n");
11657 +                       delegated = NULL;
11658 +                       infopath.dentry = elm->info_dentry;
11659 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11660 +                                          !w->no_sio);
11661 +                       AuTraceErr(err);
11662 +                       if (unlikely(err == -EWOULDBLOCK))
11663 +                               iput(delegated);
11664 +                       dput(elm->info_dentry);
11665 +               } else if (elm->info_last) {
11666 +                       AuDbg("here\n");
11667 +                       w->fdata->drinfo = *elm->info_last;
11668 +                       memcpy(w->fdata->drinfo.oldname,
11669 +                              elm->info_last->oldname,
11670 +                              elm->info_last->oldnamelen);
11671 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11672 +                       au_kfree_rcu(elm->info_last);
11673 +               }
11674 +               if (unlikely(err))
11675 +                       AuIOErr("%d, %s\n", err, w->whname);
11676 +               /* go on even if err */
11677 +       }
11678 +}
11679 +
11680 +/* caller has to call au_dr_rename_fin() later */
11681 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11682 +                          struct qstr *dst_name, void *_rev)
11683 +{
11684 +       int err, sz, nelm;
11685 +       aufs_bindex_t bindex, btail;
11686 +       struct au_drinfo_store work;
11687 +       struct au_drinfo_rev *rev, **p;
11688 +       struct au_drinfo_rev_elm *elm;
11689 +       struct super_block *sb;
11690 +       struct au_branch *br;
11691 +       struct au_hinode *hdir;
11692 +
11693 +       err = au_drinfo_store_work_init(&work, btgt);
11694 +       AuTraceErr(err);
11695 +       if (unlikely(err))
11696 +               goto out;
11697 +
11698 +       err = -ENOMEM;
11699 +       btail = au_dbtaildir(dentry);
11700 +       nelm = btail - btgt;
11701 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11702 +       rev = kcalloc(1, sz, GFP_NOFS);
11703 +       if (unlikely(!rev)) {
11704 +               AuTraceErr(err);
11705 +               goto out_args;
11706 +       }
11707 +       rev->nelm = nelm;
11708 +       elm = rev->elm;
11709 +       p = _rev;
11710 +       *p = rev;
11711 +
11712 +       err = 0;
11713 +       sb = dentry->d_sb;
11714 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11715 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11716 +       hdir = au_hi(d_inode(dentry), btgt);
11717 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11718 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11719 +               work.h_dentry = au_h_dptr(dentry, bindex);
11720 +               if (!work.h_dentry)
11721 +                       continue;
11722 +
11723 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11724 +                                         &work.allocated);
11725 +               AuTraceErr(err);
11726 +               if (unlikely(err))
11727 +                       break;
11728 +
11729 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11730 +               br = au_sbr(sb, bindex);
11731 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11732 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11733 +                                                work.infonamelen);
11734 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11735 +                     work.whnamelen, work.whname,
11736 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11737 +                     work.fdata->drinfo.oldnamelen,
11738 +                     work.fdata->drinfo.oldname);
11739 +
11740 +               err = au_drinfo_store_sio(&work, elm);
11741 +               AuTraceErr(err);
11742 +               if (unlikely(err))
11743 +                       break;
11744 +       }
11745 +       if (unlikely(err)) {
11746 +               /* revert all drinfo */
11747 +               au_drinfo_store_rev(rev, &work);
11748 +               au_kfree_try_rcu(rev);
11749 +               *p = NULL;
11750 +       }
11751 +       au_hn_inode_unlock(hdir);
11752 +
11753 +out_args:
11754 +       au_drinfo_store_work_fin(&work);
11755 +out:
11756 +       return err;
11757 +}
11758 +
11759 +/* ---------------------------------------------------------------------- */
11760 +
11761 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11762 +                struct qstr *dst_name, void *_rev)
11763 +{
11764 +       int err, already;
11765 +       ino_t ino;
11766 +       struct super_block *sb;
11767 +       struct au_branch *br;
11768 +       struct au_dr_br *dr;
11769 +       struct dentry *h_dentry;
11770 +       struct inode *h_inode;
11771 +       struct au_dr_hino *ent;
11772 +       struct au_drinfo_rev *rev, **p;
11773 +
11774 +       AuDbg("bindex %d\n", bindex);
11775 +
11776 +       err = -ENOMEM;
11777 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11778 +       if (unlikely(!ent))
11779 +               goto out;
11780 +
11781 +       sb = src->d_sb;
11782 +       br = au_sbr(sb, bindex);
11783 +       dr = &br->br_dirren;
11784 +       h_dentry = au_h_dptr(src, bindex);
11785 +       h_inode = d_inode(h_dentry);
11786 +       ino = h_inode->i_ino;
11787 +       ent->dr_h_ino = ino;
11788 +       already = au_dr_hino_test_add(dr, ino, ent);
11789 +       AuDbg("b%d, hi%llu, already %d\n",
11790 +             bindex, (unsigned long long)ino, already);
11791 +
11792 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11793 +       AuTraceErr(err);
11794 +       if (!err) {
11795 +               p = _rev;
11796 +               rev = *p;
11797 +               rev->already = already;
11798 +               goto out; /* success */
11799 +       }
11800 +
11801 +       /* revert */
11802 +       if (!already)
11803 +               au_dr_hino_del(dr, ent);
11804 +       au_kfree_rcu(ent);
11805 +
11806 +out:
11807 +       AuTraceErr(err);
11808 +       return err;
11809 +}
11810 +
11811 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11812 +{
11813 +       struct au_drinfo_rev *rev;
11814 +       struct au_drinfo_rev_elm *elm;
11815 +       int nelm;
11816 +
11817 +       rev = _rev;
11818 +       elm = rev->elm;
11819 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11820 +               dput(elm->info_dentry);
11821 +               au_kfree_rcu(elm->info_last);
11822 +       }
11823 +       au_kfree_try_rcu(rev);
11824 +}
11825 +
11826 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11827 +{
11828 +       int err;
11829 +       struct au_drinfo_store work;
11830 +       struct au_drinfo_rev *rev = _rev;
11831 +       struct super_block *sb;
11832 +       struct au_branch *br;
11833 +       struct inode *h_inode;
11834 +       struct au_dr_br *dr;
11835 +       struct au_dr_hino *ent;
11836 +
11837 +       err = au_drinfo_store_work_init(&work, btgt);
11838 +       if (unlikely(err))
11839 +               goto out;
11840 +
11841 +       sb = src->d_sb;
11842 +       br = au_sbr(sb, btgt);
11843 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11844 +       work.h_ppath.mnt = au_br_mnt(br);
11845 +       au_drinfo_store_rev(rev, &work);
11846 +       au_drinfo_store_work_fin(&work);
11847 +       if (rev->already)
11848 +               goto out;
11849 +
11850 +       dr = &br->br_dirren;
11851 +       h_inode = d_inode(work.h_ppath.dentry);
11852 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11853 +       BUG_ON(!ent);
11854 +       au_dr_hino_del(dr, ent);
11855 +       au_kfree_rcu(ent);
11856 +
11857 +out:
11858 +       au_kfree_try_rcu(rev);
11859 +       if (unlikely(err))
11860 +               pr_err("failed to remove dirren info\n");
11861 +}
11862 +
11863 +/* ---------------------------------------------------------------------- */
11864 +
11865 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11866 +                                          char *whname, int whnamelen,
11867 +                                          struct dentry **info_dentry)
11868 +{
11869 +       struct au_drinfo *drinfo;
11870 +       struct file *f;
11871 +       struct inode *h_dir;
11872 +       struct path infopath;
11873 +       int unlocked;
11874 +
11875 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11876 +
11877 +       *info_dentry = NULL;
11878 +       drinfo = NULL;
11879 +       unlocked = 0;
11880 +       h_dir = d_inode(h_ppath->dentry);
11881 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11882 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11883 +       if (IS_ERR(infopath.dentry)) {
11884 +               drinfo = (void *)infopath.dentry;
11885 +               goto out;
11886 +       }
11887 +
11888 +       if (d_is_negative(infopath.dentry))
11889 +               goto out_dput; /* success */
11890 +
11891 +       infopath.mnt = h_ppath->mnt;
11892 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11893 +       inode_unlock_shared(h_dir);
11894 +       unlocked = 1;
11895 +       if (IS_ERR(f)) {
11896 +               drinfo = (void *)f;
11897 +               goto out_dput;
11898 +       }
11899 +
11900 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11901 +       if (IS_ERR_OR_NULL(drinfo))
11902 +               goto out_fput;
11903 +
11904 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11905 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11906 +
11907 +out_fput:
11908 +       fput(f);
11909 +out_dput:
11910 +       dput(infopath.dentry);
11911 +out:
11912 +       if (!unlocked)
11913 +               inode_unlock_shared(h_dir);
11914 +       AuTraceErrPtr(drinfo);
11915 +       return drinfo;
11916 +}
11917 +
11918 +struct au_drinfo_do_load_args {
11919 +       struct au_drinfo **drinfop;
11920 +       struct path *h_ppath;
11921 +       char *whname;
11922 +       int whnamelen;
11923 +       struct dentry **info_dentry;
11924 +};
11925 +
11926 +static void au_call_drinfo_do_load(void *args)
11927 +{
11928 +       struct au_drinfo_do_load_args *a = args;
11929 +
11930 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11931 +                                       a->info_dentry);
11932 +}
11933 +
11934 +struct au_drinfo_load {
11935 +       struct path h_ppath;
11936 +       struct qstr *qname;
11937 +       unsigned char no_sio;
11938 +
11939 +       aufs_bindex_t ninfo;
11940 +       struct au_drinfo **drinfo;
11941 +};
11942 +
11943 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11944 +                         struct au_branch *br)
11945 +{
11946 +       int err, wkq_err, whnamelen, e;
11947 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11948 +               = AUFS_WH_DR_INFO_PFX;
11949 +       struct au_drinfo *drinfo;
11950 +       struct qstr oldname;
11951 +       struct inode *h_dir, *delegated;
11952 +       struct dentry *info_dentry;
11953 +       struct path infopath;
11954 +
11955 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11956 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11957 +                                   sizeof(whname) - whnamelen);
11958 +       if (w->no_sio)
11959 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11960 +                                          &info_dentry);
11961 +       else {
11962 +               struct au_drinfo_do_load_args args = {
11963 +                       .drinfop        = &drinfo,
11964 +                       .h_ppath        = &w->h_ppath,
11965 +                       .whname         = whname,
11966 +                       .whnamelen      = whnamelen,
11967 +                       .info_dentry    = &info_dentry
11968 +               };
11969 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11970 +               if (unlikely(wkq_err))
11971 +                       drinfo = ERR_PTR(wkq_err);
11972 +       }
11973 +       err = PTR_ERR(drinfo);
11974 +       if (IS_ERR_OR_NULL(drinfo))
11975 +               goto out;
11976 +
11977 +       err = 0;
11978 +       oldname.len = drinfo->oldnamelen;
11979 +       oldname.name = drinfo->oldname;
11980 +       if (au_qstreq(w->qname, &oldname)) {
11981 +               /* the name is renamed back */
11982 +               au_kfree_rcu(drinfo);
11983 +               drinfo = NULL;
11984 +
11985 +               infopath.dentry = info_dentry;
11986 +               infopath.mnt = w->h_ppath.mnt;
11987 +               h_dir = d_inode(w->h_ppath.dentry);
11988 +               delegated = NULL;
11989 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11990 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11991 +               inode_unlock(h_dir);
11992 +               if (unlikely(e))
11993 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11994 +               if (unlikely(e == -EWOULDBLOCK))
11995 +                       iput(delegated);
11996 +       }
11997 +       au_kfree_rcu(w->drinfo[bindex]);
11998 +       w->drinfo[bindex] = drinfo;
11999 +       dput(info_dentry);
12000 +
12001 +out:
12002 +       AuTraceErr(err);
12003 +       return err;
12004 +}
12005 +
12006 +/* ---------------------------------------------------------------------- */
12007 +
12008 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12009 +{
12010 +       struct au_drinfo **p = drinfo;
12011 +
12012 +       while (n-- > 0)
12013 +               au_kfree_rcu(*drinfo++);
12014 +       au_kfree_try_rcu(p);
12015 +}
12016 +
12017 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12018 +              aufs_bindex_t btgt)
12019 +{
12020 +       int err, ninfo;
12021 +       struct au_drinfo_load w;
12022 +       aufs_bindex_t bindex, bbot;
12023 +       struct au_branch *br;
12024 +       struct inode *h_dir;
12025 +       struct au_dr_hino *ent;
12026 +       struct super_block *sb;
12027 +
12028 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12029 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12030 +             AuLNPair(&lkup->whname), btgt);
12031 +
12032 +       sb = dentry->d_sb;
12033 +       bbot = au_sbbot(sb);
12034 +       w.ninfo = bbot + 1;
12035 +       if (!lkup->dirren.drinfo) {
12036 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12037 +                                             sizeof(*lkup->dirren.drinfo),
12038 +                                             GFP_NOFS);
12039 +               if (unlikely(!lkup->dirren.drinfo)) {
12040 +                       err = -ENOMEM;
12041 +                       goto out;
12042 +               }
12043 +               lkup->dirren.ninfo = w.ninfo;
12044 +       }
12045 +       w.drinfo = lkup->dirren.drinfo;
12046 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12047 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12048 +       AuDebugOn(!w.h_ppath.dentry);
12049 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12050 +       w.qname = &dentry->d_name;
12051 +
12052 +       ninfo = 0;
12053 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12054 +               br = au_sbr(sb, bindex);
12055 +               err = au_drinfo_load(&w, bindex, br);
12056 +               if (unlikely(err))
12057 +                       goto out_free;
12058 +               if (w.drinfo[bindex])
12059 +                       ninfo++;
12060 +       }
12061 +       if (!ninfo) {
12062 +               br = au_sbr(sb, btgt);
12063 +               h_dir = d_inode(w.h_ppath.dentry);
12064 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12065 +               AuDebugOn(!ent);
12066 +               au_dr_hino_del(&br->br_dirren, ent);
12067 +               au_kfree_rcu(ent);
12068 +       }
12069 +       goto out; /* success */
12070 +
12071 +out_free:
12072 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12073 +       lkup->dirren.ninfo = 0;
12074 +       lkup->dirren.drinfo = NULL;
12075 +out:
12076 +       AuTraceErr(err);
12077 +       return err;
12078 +}
12079 +
12080 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12081 +{
12082 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12083 +}
12084 +
12085 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12086 +{
12087 +       int err;
12088 +       struct au_drinfo *drinfo;
12089 +
12090 +       err = 0;
12091 +       if (!lkup->dirren.drinfo)
12092 +               goto out;
12093 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12094 +       drinfo = lkup->dirren.drinfo[btgt];
12095 +       if (!drinfo)
12096 +               goto out;
12097 +
12098 +       au_kfree_try_rcu(lkup->whname.name);
12099 +       lkup->whname.name = NULL;
12100 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12101 +       lkup->dirren.dr_name.name = drinfo->oldname;
12102 +       lkup->name = &lkup->dirren.dr_name;
12103 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12104 +       if (!err)
12105 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12106 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12107 +                     btgt);
12108 +
12109 +out:
12110 +       AuTraceErr(err);
12111 +       return err;
12112 +}
12113 +
12114 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12115 +                    ino_t h_ino)
12116 +{
12117 +       int match;
12118 +       struct au_drinfo *drinfo;
12119 +
12120 +       match = 1;
12121 +       if (!lkup->dirren.drinfo)
12122 +               goto out;
12123 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12124 +       drinfo = lkup->dirren.drinfo[bindex];
12125 +       if (!drinfo)
12126 +               goto out;
12127 +
12128 +       match = (drinfo->ino == h_ino);
12129 +       AuDbg("match %d\n", match);
12130 +
12131 +out:
12132 +       return match;
12133 +}
12134 +
12135 +/* ---------------------------------------------------------------------- */
12136 +
12137 +int au_dr_opt_set(struct super_block *sb)
12138 +{
12139 +       int err;
12140 +       aufs_bindex_t bindex, bbot;
12141 +       struct au_branch *br;
12142 +
12143 +       err = 0;
12144 +       bbot = au_sbbot(sb);
12145 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12146 +               br = au_sbr(sb, bindex);
12147 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12148 +       }
12149 +
12150 +       return err;
12151 +}
12152 +
12153 +int au_dr_opt_flush(struct super_block *sb)
12154 +{
12155 +       int err;
12156 +       aufs_bindex_t bindex, bbot;
12157 +       struct au_branch *br;
12158 +
12159 +       err = 0;
12160 +       bbot = au_sbbot(sb);
12161 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12162 +               br = au_sbr(sb, bindex);
12163 +               if (au_br_writable(br->br_perm))
12164 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12165 +       }
12166 +
12167 +       return err;
12168 +}
12169 +
12170 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12171 +{
12172 +       int err;
12173 +       aufs_bindex_t bindex, bbot;
12174 +       struct au_branch *br;
12175 +
12176 +       err = 0;
12177 +       if (!no_flush) {
12178 +               err = au_dr_opt_flush(sb);
12179 +               if (unlikely(err))
12180 +                       goto out;
12181 +       }
12182 +
12183 +       bbot = au_sbbot(sb);
12184 +       for (bindex = 0; bindex <= bbot; bindex++) {
12185 +               br = au_sbr(sb, bindex);
12186 +               au_dr_hino_free(&br->br_dirren);
12187 +       }
12188 +
12189 +out:
12190 +       return err;
12191 +}
12192 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12193 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12194 +++ linux/fs/aufs/dirren.h      2022-11-05 23:02:18.962555950 +0100
12195 @@ -0,0 +1,140 @@
12196 +/* SPDX-License-Identifier: GPL-2.0 */
12197 +/*
12198 + * Copyright (C) 2017-2022 Junjiro R. Okajima
12199 + *
12200 + * This program is free software; you can redistribute it and/or modify
12201 + * it under the terms of the GNU General Public License as published by
12202 + * the Free Software Foundation; either version 2 of the License, or
12203 + * (at your option) any later version.
12204 + *
12205 + * This program is distributed in the hope that it will be useful,
12206 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12207 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12208 + * GNU General Public License for more details.
12209 + *
12210 + * You should have received a copy of the GNU General Public License
12211 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12212 + */
12213 +
12214 +/*
12215 + * renamed dir info
12216 + */
12217 +
12218 +#ifndef __AUFS_DIRREN_H__
12219 +#define __AUFS_DIRREN_H__
12220 +
12221 +#ifdef __KERNEL__
12222 +
12223 +#include <linux/dcache.h>
12224 +#include <linux/statfs.h>
12225 +#include <linux/uuid.h>
12226 +#include "hbl.h"
12227 +
12228 +#define AuDirren_NHASH 100
12229 +
12230 +#ifdef CONFIG_AUFS_DIRREN
12231 +enum au_brid_type {
12232 +       AuBrid_Unset,
12233 +       AuBrid_UUID,
12234 +       AuBrid_FSID,
12235 +       AuBrid_DEV
12236 +};
12237 +
12238 +struct au_dr_brid {
12239 +       enum au_brid_type       type;
12240 +       union {
12241 +               uuid_t  uuid;   /* unimplemented yet */
12242 +               fsid_t  fsid;
12243 +               dev_t   dev;
12244 +       };
12245 +};
12246 +
12247 +/* 20 is the max digits length of ulong 64 */
12248 +/* brid-type "_" uuid "_" inum */
12249 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12250 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12251 +
12252 +struct au_dr_hino {
12253 +       struct hlist_bl_node    dr_hnode;
12254 +       ino_t                   dr_h_ino;
12255 +};
12256 +
12257 +struct au_dr_br {
12258 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12259 +       struct au_dr_brid       dr_brid;
12260 +};
12261 +
12262 +struct au_dr_lookup {
12263 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12264 +       struct qstr             dr_name; /* subset of dr_info */
12265 +       aufs_bindex_t           ninfo;
12266 +       struct au_drinfo        **drinfo;
12267 +};
12268 +#else
12269 +struct au_dr_hino;
12270 +/* empty */
12271 +struct au_dr_br { };
12272 +struct au_dr_lookup { };
12273 +#endif
12274 +
12275 +/* ---------------------------------------------------------------------- */
12276 +
12277 +struct au_branch;
12278 +struct au_do_lookup_args;
12279 +struct au_hinode;
12280 +#ifdef CONFIG_AUFS_DIRREN
12281 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12282 +                       struct au_dr_hino *add_ent);
12283 +void au_dr_hino_free(struct au_dr_br *dr);
12284 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12285 +                 const struct path *path);
12286 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12287 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12288 +                struct qstr *dst_name, void *_rev);
12289 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12290 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12291 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12292 +              aufs_bindex_t bindex);
12293 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12294 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12295 +                    ino_t h_ino);
12296 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12297 +int au_dr_opt_set(struct super_block *sb);
12298 +int au_dr_opt_flush(struct super_block *sb);
12299 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12300 +#else
12301 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12302 +          struct au_dr_hino *add_ent);
12303 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12304 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12305 +          const struct path *path);
12306 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12307 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12308 +          struct qstr *dst_name, void *_rev);
12309 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12310 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12311 +          void *rev);
12312 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12313 +          aufs_bindex_t bindex);
12314 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12315 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12316 +          aufs_bindex_t bindex, ino_t h_ino);
12317 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12318 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12319 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12320 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12321 +#endif
12322 +
12323 +/* ---------------------------------------------------------------------- */
12324 +
12325 +#ifdef CONFIG_AUFS_DIRREN
12326 +static inline int au_dr_ihash(ino_t h_ino)
12327 +{
12328 +       return h_ino % AuDirren_NHASH;
12329 +}
12330 +#else
12331 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12332 +#endif
12333 +
12334 +#endif /* __KERNEL__ */
12335 +#endif /* __AUFS_DIRREN_H__ */
12336 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12337 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12338 +++ linux/fs/aufs/dynop.c       2022-11-05 23:02:18.962555950 +0100
12339 @@ -0,0 +1,366 @@
12340 +// SPDX-License-Identifier: GPL-2.0
12341 +/*
12342 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12343 + *
12344 + * This program is free software; you can redistribute it and/or modify
12345 + * it under the terms of the GNU General Public License as published by
12346 + * the Free Software Foundation; either version 2 of the License, or
12347 + * (at your option) any later version.
12348 + *
12349 + * This program is distributed in the hope that it will be useful,
12350 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12351 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12352 + * GNU General Public License for more details.
12353 + *
12354 + * You should have received a copy of the GNU General Public License
12355 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12356 + */
12357 +
12358 +/*
12359 + * dynamically customizable operations for regular files
12360 + */
12361 +
12362 +#include "aufs.h"
12363 +
12364 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12365 +
12366 +/*
12367 + * How large will these lists be?
12368 + * Usually just a few elements, 20-30 at most for each, I guess.
12369 + */
12370 +static struct hlist_bl_head dynop[AuDyLast];
12371 +
12372 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12373 +                                    const void *h_op)
12374 +{
12375 +       struct au_dykey *key, *tmp;
12376 +       struct hlist_bl_node *pos;
12377 +
12378 +       key = NULL;
12379 +       hlist_bl_lock(hbl);
12380 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12381 +               if (tmp->dk_op.dy_hop == h_op) {
12382 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12383 +                               key = tmp;
12384 +                       break;
12385 +               }
12386 +       hlist_bl_unlock(hbl);
12387 +
12388 +       return key;
12389 +}
12390 +
12391 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12392 +{
12393 +       struct au_dykey **k, *found;
12394 +       const void *h_op = key->dk_op.dy_hop;
12395 +       int i;
12396 +
12397 +       found = NULL;
12398 +       k = br->br_dykey;
12399 +       for (i = 0; i < AuBrDynOp; i++)
12400 +               if (k[i]) {
12401 +                       if (k[i]->dk_op.dy_hop == h_op) {
12402 +                               found = k[i];
12403 +                               break;
12404 +                       }
12405 +               } else
12406 +                       break;
12407 +       if (!found) {
12408 +               spin_lock(&br->br_dykey_lock);
12409 +               for (; i < AuBrDynOp; i++)
12410 +                       if (k[i]) {
12411 +                               if (k[i]->dk_op.dy_hop == h_op) {
12412 +                                       found = k[i];
12413 +                                       break;
12414 +                               }
12415 +                       } else {
12416 +                               k[i] = key;
12417 +                               break;
12418 +                       }
12419 +               spin_unlock(&br->br_dykey_lock);
12420 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12421 +       }
12422 +
12423 +       return found;
12424 +}
12425 +
12426 +/* kref_get() if @key is already added */
12427 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12428 +{
12429 +       struct au_dykey *tmp, *found;
12430 +       struct hlist_bl_node *pos;
12431 +       const void *h_op = key->dk_op.dy_hop;
12432 +
12433 +       found = NULL;
12434 +       hlist_bl_lock(hbl);
12435 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12436 +               if (tmp->dk_op.dy_hop == h_op) {
12437 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12438 +                               found = tmp;
12439 +                       break;
12440 +               }
12441 +       if (!found)
12442 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12443 +       hlist_bl_unlock(hbl);
12444 +
12445 +       if (!found)
12446 +               DyPrSym(key);
12447 +       return found;
12448 +}
12449 +
12450 +static void dy_free_rcu(struct rcu_head *rcu)
12451 +{
12452 +       struct au_dykey *key;
12453 +
12454 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12455 +       DyPrSym(key);
12456 +       kfree(key);
12457 +}
12458 +
12459 +static void dy_free(struct kref *kref)
12460 +{
12461 +       struct au_dykey *key;
12462 +       struct hlist_bl_head *hbl;
12463 +
12464 +       key = container_of(kref, struct au_dykey, dk_kref);
12465 +       hbl = dynop + key->dk_op.dy_type;
12466 +       au_hbl_del(&key->dk_hnode, hbl);
12467 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12468 +}
12469 +
12470 +void au_dy_put(struct au_dykey *key)
12471 +{
12472 +       kref_put(&key->dk_kref, dy_free);
12473 +}
12474 +
12475 +/* ---------------------------------------------------------------------- */
12476 +
12477 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12478 +
12479 +#ifdef CONFIG_AUFS_DEBUG
12480 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12481 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12482 +#else
12483 +#define DyDbgDeclare(cnt)      do {} while (0)
12484 +#define DyDbgInc(cnt)          do {} while (0)
12485 +#endif
12486 +
12487 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12488 +       DyDbgInc(cnt);                                                  \
12489 +       if (h_op->func) {                                               \
12490 +               if (src.func)                                           \
12491 +                       dst.func = src.func;                            \
12492 +               else                                                    \
12493 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12494 +       }                                                               \
12495 +} while (0)
12496 +
12497 +#define DySetForce(func, dst, src) do {                \
12498 +       AuDebugOn(!src.func);                   \
12499 +       DyDbgInc(cnt);                          \
12500 +       dst.func = src.func;                    \
12501 +} while (0)
12502 +
12503 +#define DySetAop(func) \
12504 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12505 +#define DySetAopForce(func) \
12506 +       DySetForce(func, dyaop->da_op, aufs_aop)
12507 +
12508 +static void dy_aop(struct au_dykey *key, const void *h_op,
12509 +                  struct super_block *h_sb __maybe_unused)
12510 +{
12511 +       struct au_dyaop *dyaop = (void *)key;
12512 +       const struct address_space_operations *h_aop = h_op;
12513 +       DyDbgDeclare(cnt);
12514 +
12515 +       AuDbg("%s\n", au_sbtype(h_sb));
12516 +
12517 +       DySetAop(writepage);
12518 +       DySetAopForce(read_folio);      /* force */
12519 +       DySetAop(writepages);
12520 +       DySetAop(dirty_folio);
12521 +       DySetAop(invalidate_folio);
12522 +       DySetAop(readahead);
12523 +       DySetAop(write_begin);
12524 +       DySetAop(write_end);
12525 +       DySetAop(bmap);
12526 +       DySetAop(release_folio);
12527 +       DySetAop(free_folio);
12528 +       /* this one will be changed according to an aufs mount option */
12529 +       DySetAop(direct_IO);
12530 +       DySetAop(migrate_folio);
12531 +       DySetAop(launder_folio);
12532 +       DySetAop(is_partially_uptodate);
12533 +       DySetAop(is_dirty_writeback);
12534 +       DySetAop(error_remove_page);
12535 +       DySetAop(swap_activate);
12536 +       DySetAop(swap_deactivate);
12537 +       DySetAop(swap_rw);
12538 +
12539 +       DyDbgSize(cnt, *h_aop);
12540 +}
12541 +
12542 +/* ---------------------------------------------------------------------- */
12543 +
12544 +static void dy_bug(struct kref *kref)
12545 +{
12546 +       BUG();
12547 +}
12548 +
12549 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12550 +{
12551 +       struct au_dykey *key, *old;
12552 +       struct hlist_bl_head *hbl;
12553 +       struct op {
12554 +               unsigned int sz;
12555 +               void (*set)(struct au_dykey *key, const void *h_op,
12556 +                           struct super_block *h_sb __maybe_unused);
12557 +       };
12558 +       static const struct op a[] = {
12559 +               [AuDy_AOP] = {
12560 +                       .sz     = sizeof(struct au_dyaop),
12561 +                       .set    = dy_aop
12562 +               }
12563 +       };
12564 +       const struct op *p;
12565 +
12566 +       hbl = dynop + op->dy_type;
12567 +       key = dy_gfind_get(hbl, op->dy_hop);
12568 +       if (key)
12569 +               goto out_add; /* success */
12570 +
12571 +       p = a + op->dy_type;
12572 +       key = kzalloc(p->sz, GFP_NOFS);
12573 +       if (unlikely(!key)) {
12574 +               key = ERR_PTR(-ENOMEM);
12575 +               goto out;
12576 +       }
12577 +
12578 +       key->dk_op.dy_hop = op->dy_hop;
12579 +       kref_init(&key->dk_kref);
12580 +       p->set(key, op->dy_hop, au_br_sb(br));
12581 +       old = dy_gadd(hbl, key);
12582 +       if (old) {
12583 +               au_kfree_rcu(key);
12584 +               key = old;
12585 +       }
12586 +
12587 +out_add:
12588 +       old = dy_bradd(br, key);
12589 +       if (old)
12590 +               /* its ref-count should never be zero here */
12591 +               kref_put(&key->dk_kref, dy_bug);
12592 +out:
12593 +       return key;
12594 +}
12595 +
12596 +/* ---------------------------------------------------------------------- */
12597 +/*
12598 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12599 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12600 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12601 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12602 + * See the aufs manual in detail.
12603 + */
12604 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12605 +{
12606 +       if (!do_dx)
12607 +               dyaop->da_op.direct_IO = NULL;
12608 +       else
12609 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12610 +}
12611 +
12612 +static struct au_dyaop *dy_aget(struct au_branch *br,
12613 +                               const struct address_space_operations *h_aop,
12614 +                               int do_dx)
12615 +{
12616 +       struct au_dyaop *dyaop;
12617 +       struct au_dynop op;
12618 +
12619 +       op.dy_type = AuDy_AOP;
12620 +       op.dy_haop = h_aop;
12621 +       dyaop = (void *)dy_get(&op, br);
12622 +       if (IS_ERR(dyaop))
12623 +               goto out;
12624 +       dy_adx(dyaop, do_dx);
12625 +
12626 +out:
12627 +       return dyaop;
12628 +}
12629 +
12630 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12631 +               struct inode *h_inode)
12632 +{
12633 +       int err, do_dx;
12634 +       struct super_block *sb;
12635 +       struct au_branch *br;
12636 +       struct au_dyaop *dyaop;
12637 +
12638 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12639 +       IiMustWriteLock(inode);
12640 +
12641 +       sb = inode->i_sb;
12642 +       br = au_sbr(sb, bindex);
12643 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12644 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12645 +       err = PTR_ERR(dyaop);
12646 +       if (IS_ERR(dyaop))
12647 +               /* unnecessary to call dy_fput() */
12648 +               goto out;
12649 +
12650 +       err = 0;
12651 +       inode->i_mapping->a_ops = &dyaop->da_op;
12652 +
12653 +out:
12654 +       return err;
12655 +}
12656 +
12657 +/*
12658 + * Is it safe to replace a_ops during the inode/file is in operation?
12659 + * Yes, I hope so.
12660 + */
12661 +int au_dy_irefresh(struct inode *inode)
12662 +{
12663 +       int err;
12664 +       aufs_bindex_t btop;
12665 +       struct inode *h_inode;
12666 +
12667 +       err = 0;
12668 +       if (S_ISREG(inode->i_mode)) {
12669 +               btop = au_ibtop(inode);
12670 +               h_inode = au_h_iptr(inode, btop);
12671 +               err = au_dy_iaop(inode, btop, h_inode);
12672 +       }
12673 +       return err;
12674 +}
12675 +
12676 +void au_dy_arefresh(int do_dx)
12677 +{
12678 +       struct hlist_bl_head *hbl;
12679 +       struct hlist_bl_node *pos;
12680 +       struct au_dykey *key;
12681 +
12682 +       hbl = dynop + AuDy_AOP;
12683 +       hlist_bl_lock(hbl);
12684 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12685 +               dy_adx((void *)key, do_dx);
12686 +       hlist_bl_unlock(hbl);
12687 +}
12688 +
12689 +/* ---------------------------------------------------------------------- */
12690 +
12691 +void __init au_dy_init(void)
12692 +{
12693 +       int i;
12694 +
12695 +       for (i = 0; i < AuDyLast; i++)
12696 +               INIT_HLIST_BL_HEAD(dynop + i);
12697 +}
12698 +
12699 +void au_dy_fin(void)
12700 +{
12701 +       int i;
12702 +
12703 +       for (i = 0; i < AuDyLast; i++)
12704 +               WARN_ON(!hlist_bl_empty(dynop + i));
12705 +}
12706 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12707 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12708 +++ linux/fs/aufs/dynop.h       2022-11-05 23:02:18.962555950 +0100
12709 @@ -0,0 +1,77 @@
12710 +/* SPDX-License-Identifier: GPL-2.0 */
12711 +/*
12712 + * Copyright (C) 2010-2022 Junjiro R. Okajima
12713 + *
12714 + * This program is free software; you can redistribute it and/or modify
12715 + * it under the terms of the GNU General Public License as published by
12716 + * the Free Software Foundation; either version 2 of the License, or
12717 + * (at your option) any later version.
12718 + *
12719 + * This program is distributed in the hope that it will be useful,
12720 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12721 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12722 + * GNU General Public License for more details.
12723 + *
12724 + * You should have received a copy of the GNU General Public License
12725 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12726 + */
12727 +
12728 +/*
12729 + * dynamically customizable operations (for regular files only)
12730 + */
12731 +
12732 +#ifndef __AUFS_DYNOP_H__
12733 +#define __AUFS_DYNOP_H__
12734 +
12735 +#ifdef __KERNEL__
12736 +
12737 +#include <linux/fs.h>
12738 +#include <linux/kref.h>
12739 +
12740 +enum {AuDy_AOP, AuDyLast};
12741 +
12742 +struct au_dynop {
12743 +       int                                             dy_type;
12744 +       union {
12745 +               const void                              *dy_hop;
12746 +               const struct address_space_operations   *dy_haop;
12747 +       };
12748 +};
12749 +
12750 +struct au_dykey {
12751 +       union {
12752 +               struct hlist_bl_node    dk_hnode;
12753 +               struct rcu_head         dk_rcu;
12754 +       };
12755 +       struct au_dynop         dk_op;
12756 +
12757 +       /*
12758 +        * during I am in the branch local array, kref is gotten. when the
12759 +        * branch is removed, kref is put.
12760 +        */
12761 +       struct kref             dk_kref;
12762 +};
12763 +
12764 +/* stop unioning since their sizes are very different from each other */
12765 +struct au_dyaop {
12766 +       struct au_dykey                 da_key;
12767 +       struct address_space_operations da_op; /* not const */
12768 +};
12769 +/* make sure that 'struct au_dykey *' can be any type */
12770 +static_assert(!offsetof(struct au_dyaop, da_key));
12771 +
12772 +/* ---------------------------------------------------------------------- */
12773 +
12774 +/* dynop.c */
12775 +struct au_branch;
12776 +void au_dy_put(struct au_dykey *key);
12777 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12778 +               struct inode *h_inode);
12779 +int au_dy_irefresh(struct inode *inode);
12780 +void au_dy_arefresh(int do_dio);
12781 +
12782 +void __init au_dy_init(void);
12783 +void au_dy_fin(void);
12784 +
12785 +#endif /* __KERNEL__ */
12786 +#endif /* __AUFS_DYNOP_H__ */
12787 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12788 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12789 +++ linux/fs/aufs/export.c      2022-12-17 09:21:34.796521861 +0100
12790 @@ -0,0 +1,830 @@
12791 +// SPDX-License-Identifier: GPL-2.0
12792 +/*
12793 + * Copyright (C) 2005-2022 Junjiro R. Okajima
12794 + *
12795 + * This program is free software; you can redistribute it and/or modify
12796 + * it under the terms of the GNU General Public License as published by
12797 + * the Free Software Foundation; either version 2 of the License, or
12798 + * (at your option) any later version.
12799 + *
12800 + * This program is distributed in the hope that it will be useful,
12801 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12802 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12803 + * GNU General Public License for more details.
12804 + *
12805 + * You should have received a copy of the GNU General Public License
12806 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12807 + */
12808 +
12809 +/*
12810 + * export via nfs
12811 + */
12812 +
12813 +#include <linux/exportfs.h>
12814 +#include <linux/fs_struct.h>
12815 +#include <linux/nsproxy.h>
12816 +#include <linux/random.h>
12817 +#include <linux/writeback.h>
12818 +#include "aufs.h"
12819 +
12820 +union conv {
12821 +#ifdef CONFIG_AUFS_INO_T_64
12822 +       __u32 a[2];
12823 +#else
12824 +       __u32 a[1];
12825 +#endif
12826 +       ino_t ino;
12827 +};
12828 +
12829 +static ino_t decode_ino(__u32 *a)
12830 +{
12831 +       union conv u;
12832 +
12833 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12834 +       u.a[0] = a[0];
12835 +#ifdef CONFIG_AUFS_INO_T_64
12836 +       u.a[1] = a[1];
12837 +#endif
12838 +       return u.ino;
12839 +}
12840 +
12841 +static void encode_ino(__u32 *a, ino_t ino)
12842 +{
12843 +       union conv u;
12844 +
12845 +       u.ino = ino;
12846 +       a[0] = u.a[0];
12847 +#ifdef CONFIG_AUFS_INO_T_64
12848 +       a[1] = u.a[1];
12849 +#endif
12850 +}
12851 +
12852 +/* NFS file handle */
12853 +enum {
12854 +       Fh_br_id,
12855 +       Fh_sigen,
12856 +#ifdef CONFIG_AUFS_INO_T_64
12857 +       /* support 64bit inode number */
12858 +       Fh_ino1,
12859 +       Fh_ino2,
12860 +       Fh_dir_ino1,
12861 +       Fh_dir_ino2,
12862 +#else
12863 +       Fh_ino1,
12864 +       Fh_dir_ino1,
12865 +#endif
12866 +       Fh_igen,
12867 +       Fh_h_type,
12868 +       Fh_tail,
12869 +
12870 +       Fh_ino = Fh_ino1,
12871 +       Fh_dir_ino = Fh_dir_ino1
12872 +};
12873 +
12874 +static int au_test_anon(struct dentry *dentry)
12875 +{
12876 +       /* note: read d_flags without d_lock */
12877 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12878 +}
12879 +
12880 +int au_test_nfsd(void)
12881 +{
12882 +       int ret;
12883 +       struct task_struct *tsk = current;
12884 +       char comm[sizeof(tsk->comm)];
12885 +
12886 +       ret = 0;
12887 +       if (tsk->flags & PF_KTHREAD) {
12888 +               get_task_comm(comm, tsk);
12889 +               ret = !strcmp(comm, "nfsd");
12890 +       }
12891 +
12892 +       return ret;
12893 +}
12894 +
12895 +/* ---------------------------------------------------------------------- */
12896 +/* inode generation external table */
12897 +
12898 +void au_xigen_inc(struct inode *inode)
12899 +{
12900 +       loff_t pos;
12901 +       ssize_t sz;
12902 +       __u32 igen;
12903 +       struct super_block *sb;
12904 +       struct au_sbinfo *sbinfo;
12905 +
12906 +       sb = inode->i_sb;
12907 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12908 +
12909 +       sbinfo = au_sbi(sb);
12910 +       pos = inode->i_ino;
12911 +       pos *= sizeof(igen);
12912 +       igen = inode->i_generation + 1;
12913 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12914 +       if (sz == sizeof(igen))
12915 +               return; /* success */
12916 +
12917 +       if (unlikely(sz >= 0))
12918 +               AuIOErr("xigen error (%zd)\n", sz);
12919 +}
12920 +
12921 +int au_xigen_new(struct inode *inode)
12922 +{
12923 +       int err;
12924 +       loff_t pos;
12925 +       ssize_t sz;
12926 +       struct super_block *sb;
12927 +       struct au_sbinfo *sbinfo;
12928 +       struct file *file;
12929 +
12930 +       err = 0;
12931 +       /* todo: dirty, at mount time */
12932 +       if (inode->i_ino == AUFS_ROOT_INO)
12933 +               goto out;
12934 +       sb = inode->i_sb;
12935 +       SiMustAnyLock(sb);
12936 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12937 +               goto out;
12938 +
12939 +       err = -EFBIG;
12940 +       pos = inode->i_ino;
12941 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12942 +               AuIOErr1("too large i%lld\n", pos);
12943 +               goto out;
12944 +       }
12945 +       pos *= sizeof(inode->i_generation);
12946 +
12947 +       err = 0;
12948 +       sbinfo = au_sbi(sb);
12949 +       file = sbinfo->si_xigen;
12950 +       BUG_ON(!file);
12951 +
12952 +       if (vfsub_f_size_read(file)
12953 +           < pos + sizeof(inode->i_generation)) {
12954 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12955 +               sz = xino_fwrite(file, &inode->i_generation,
12956 +                                sizeof(inode->i_generation), &pos);
12957 +       } else
12958 +               sz = xino_fread(file, &inode->i_generation,
12959 +                               sizeof(inode->i_generation), &pos);
12960 +       if (sz == sizeof(inode->i_generation))
12961 +               goto out; /* success */
12962 +
12963 +       err = sz;
12964 +       if (unlikely(sz >= 0)) {
12965 +               err = -EIO;
12966 +               AuIOErr("xigen error (%zd)\n", sz);
12967 +       }
12968 +
12969 +out:
12970 +       return err;
12971 +}
12972 +
12973 +int au_xigen_set(struct super_block *sb, struct path *path)
12974 +{
12975 +       int err;
12976 +       struct au_sbinfo *sbinfo;
12977 +       struct file *file;
12978 +
12979 +       SiMustWriteLock(sb);
12980 +
12981 +       sbinfo = au_sbi(sb);
12982 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12983 +       err = PTR_ERR(file);
12984 +       if (IS_ERR(file))
12985 +               goto out;
12986 +       err = 0;
12987 +       if (sbinfo->si_xigen)
12988 +               fput(sbinfo->si_xigen);
12989 +       sbinfo->si_xigen = file;
12990 +
12991 +out:
12992 +       AuTraceErr(err);
12993 +       return err;
12994 +}
12995 +
12996 +void au_xigen_clr(struct super_block *sb)
12997 +{
12998 +       struct au_sbinfo *sbinfo;
12999 +
13000 +       SiMustWriteLock(sb);
13001 +
13002 +       sbinfo = au_sbi(sb);
13003 +       if (sbinfo->si_xigen) {
13004 +               fput(sbinfo->si_xigen);
13005 +               sbinfo->si_xigen = NULL;
13006 +       }
13007 +}
13008 +
13009 +/* ---------------------------------------------------------------------- */
13010 +
13011 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13012 +                                   ino_t dir_ino)
13013 +{
13014 +       struct dentry *dentry, *d;
13015 +       struct inode *inode;
13016 +       unsigned int sigen;
13017 +
13018 +       dentry = NULL;
13019 +       inode = ilookup(sb, ino);
13020 +       if (!inode)
13021 +               goto out;
13022 +
13023 +       dentry = ERR_PTR(-ESTALE);
13024 +       sigen = au_sigen(sb);
13025 +       if (unlikely(au_is_bad_inode(inode)
13026 +                    || IS_DEADDIR(inode)
13027 +                    || sigen != au_iigen(inode, NULL)))
13028 +               goto out_iput;
13029 +
13030 +       dentry = NULL;
13031 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13032 +               dentry = d_find_alias(inode);
13033 +       else {
13034 +               spin_lock(&inode->i_lock);
13035 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13036 +                       spin_lock(&d->d_lock);
13037 +                       if (!au_test_anon(d)
13038 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13039 +                               dentry = dget_dlock(d);
13040 +                               spin_unlock(&d->d_lock);
13041 +                               break;
13042 +                       }
13043 +                       spin_unlock(&d->d_lock);
13044 +               }
13045 +               spin_unlock(&inode->i_lock);
13046 +       }
13047 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13048 +               /* need to refresh */
13049 +               dput(dentry);
13050 +               dentry = NULL;
13051 +       }
13052 +
13053 +out_iput:
13054 +       iput(inode);
13055 +out:
13056 +       AuTraceErrPtr(dentry);
13057 +       return dentry;
13058 +}
13059 +
13060 +/* ---------------------------------------------------------------------- */
13061 +
13062 +/* todo: dirty? */
13063 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13064 +
13065 +struct au_compare_mnt_args {
13066 +       /* input */
13067 +       struct super_block *sb;
13068 +
13069 +       /* output */
13070 +       struct vfsmount *mnt;
13071 +};
13072 +
13073 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13074 +{
13075 +       struct au_compare_mnt_args *a = arg;
13076 +
13077 +       if (mnt->mnt_sb != a->sb)
13078 +               return 0;
13079 +       a->mnt = mntget(mnt);
13080 +       return 1;
13081 +}
13082 +
13083 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13084 +{
13085 +       int err;
13086 +       struct path root;
13087 +       struct au_compare_mnt_args args = {
13088 +               .sb = sb
13089 +       };
13090 +
13091 +       get_fs_root(current->fs, &root);
13092 +       rcu_read_lock();
13093 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13094 +       rcu_read_unlock();
13095 +       path_put(&root);
13096 +       AuDebugOn(!err);
13097 +       AuDebugOn(!args.mnt);
13098 +       return args.mnt;
13099 +}
13100 +
13101 +struct au_nfsd_si_lock {
13102 +       unsigned int sigen;
13103 +       aufs_bindex_t bindex, br_id;
13104 +       unsigned char force_lock;
13105 +};
13106 +
13107 +static int si_nfsd_read_lock(struct super_block *sb,
13108 +                            struct au_nfsd_si_lock *nsi_lock)
13109 +{
13110 +       int err;
13111 +       aufs_bindex_t bindex;
13112 +
13113 +       si_read_lock(sb, AuLock_FLUSH);
13114 +
13115 +       /* branch id may be wrapped around */
13116 +       err = 0;
13117 +       bindex = au_br_index(sb, nsi_lock->br_id);
13118 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13119 +               goto out; /* success */
13120 +
13121 +       err = -ESTALE;
13122 +       bindex = -1;
13123 +       if (!nsi_lock->force_lock)
13124 +               si_read_unlock(sb);
13125 +
13126 +out:
13127 +       nsi_lock->bindex = bindex;
13128 +       return err;
13129 +}
13130 +
13131 +struct find_name_by_ino {
13132 +       struct dir_context ctx;
13133 +       int called, found;
13134 +       ino_t ino;
13135 +       char *name;
13136 +       int namelen;
13137 +};
13138 +
13139 +static bool
13140 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13141 +                loff_t offset, u64 ino, unsigned int d_type)
13142 +{
13143 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13144 +                                                 ctx);
13145 +
13146 +       a->called++;
13147 +       if (a->ino != ino)
13148 +               return true;
13149 +
13150 +       memcpy(a->name, name, namelen);
13151 +       a->namelen = namelen;
13152 +       a->found = 1;
13153 +       return false;
13154 +}
13155 +
13156 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13157 +                                    struct au_nfsd_si_lock *nsi_lock)
13158 +{
13159 +       struct dentry *dentry, *parent;
13160 +       struct file *file;
13161 +       struct inode *dir;
13162 +       struct find_name_by_ino arg = {
13163 +               .ctx = {
13164 +                       .actor = find_name_by_ino
13165 +               }
13166 +       };
13167 +       int err;
13168 +
13169 +       parent = path->dentry;
13170 +       if (nsi_lock)
13171 +               si_read_unlock(parent->d_sb);
13172 +       file = vfsub_dentry_open(path, au_dir_roflags);
13173 +       dentry = (void *)file;
13174 +       if (IS_ERR(file))
13175 +               goto out;
13176 +
13177 +       dentry = ERR_PTR(-ENOMEM);
13178 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13179 +       if (unlikely(!arg.name))
13180 +               goto out_file;
13181 +       arg.ino = ino;
13182 +       arg.found = 0;
13183 +       do {
13184 +               arg.called = 0;
13185 +               /* smp_mb(); */
13186 +               err = vfsub_iterate_dir(file, &arg.ctx);
13187 +       } while (!err && !arg.found && arg.called);
13188 +       dentry = ERR_PTR(err);
13189 +       if (unlikely(err))
13190 +               goto out_name;
13191 +       /* instead of ENOENT */
13192 +       dentry = ERR_PTR(-ESTALE);
13193 +       if (!arg.found)
13194 +               goto out_name;
13195 +
13196 +       /* do not call vfsub_lkup_one() */
13197 +       dir = d_inode(parent);
13198 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13199 +       AuTraceErrPtr(dentry);
13200 +       if (IS_ERR(dentry))
13201 +               goto out_name;
13202 +       AuDebugOn(au_test_anon(dentry));
13203 +       if (unlikely(d_really_is_negative(dentry))) {
13204 +               dput(dentry);
13205 +               dentry = ERR_PTR(-ENOENT);
13206 +       }
13207 +
13208 +out_name:
13209 +       free_page((unsigned long)arg.name);
13210 +out_file:
13211 +       fput(file);
13212 +out:
13213 +       if (unlikely(nsi_lock
13214 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13215 +               if (!IS_ERR(dentry)) {
13216 +                       dput(dentry);
13217 +                       dentry = ERR_PTR(-ESTALE);
13218 +               }
13219 +       AuTraceErrPtr(dentry);
13220 +       return dentry;
13221 +}
13222 +
13223 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13224 +                                       ino_t dir_ino,
13225 +                                       struct au_nfsd_si_lock *nsi_lock)
13226 +{
13227 +       struct dentry *dentry;
13228 +       struct path path;
13229 +
13230 +       if (dir_ino != AUFS_ROOT_INO) {
13231 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13232 +               dentry = path.dentry;
13233 +               if (!path.dentry || IS_ERR(path.dentry))
13234 +                       goto out;
13235 +               AuDebugOn(au_test_anon(path.dentry));
13236 +       } else
13237 +               path.dentry = dget(sb->s_root);
13238 +
13239 +       path.mnt = au_mnt_get(sb);
13240 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13241 +       path_put(&path);
13242 +
13243 +out:
13244 +       AuTraceErrPtr(dentry);
13245 +       return dentry;
13246 +}
13247 +
13248 +/* ---------------------------------------------------------------------- */
13249 +
13250 +static int h_acceptable(void *expv, struct dentry *dentry)
13251 +{
13252 +       return 1;
13253 +}
13254 +
13255 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13256 +                          char *buf, int len, struct super_block *sb)
13257 +{
13258 +       char *p;
13259 +       int n;
13260 +       struct path path;
13261 +
13262 +       p = d_path(h_rootpath, buf, len);
13263 +       if (IS_ERR(p))
13264 +               goto out;
13265 +       n = strlen(p);
13266 +
13267 +       path.mnt = h_rootpath->mnt;
13268 +       path.dentry = h_parent;
13269 +       p = d_path(&path, buf, len);
13270 +       if (IS_ERR(p))
13271 +               goto out;
13272 +       if (n != 1)
13273 +               p += n;
13274 +
13275 +       path.mnt = au_mnt_get(sb);
13276 +       path.dentry = sb->s_root;
13277 +       p = d_path(&path, buf, len - strlen(p));
13278 +       mntput(path.mnt);
13279 +       if (IS_ERR(p))
13280 +               goto out;
13281 +       if (n != 1)
13282 +               p[strlen(p)] = '/';
13283 +
13284 +out:
13285 +       AuTraceErrPtr(p);
13286 +       return p;
13287 +}
13288 +
13289 +static
13290 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13291 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13292 +{
13293 +       struct dentry *dentry, *h_parent, *root;
13294 +       struct super_block *h_sb;
13295 +       char *pathname, *p;
13296 +       struct vfsmount *h_mnt;
13297 +       struct au_branch *br;
13298 +       int err;
13299 +       struct path path;
13300 +
13301 +       br = au_sbr(sb, nsi_lock->bindex);
13302 +       h_mnt = au_br_mnt(br);
13303 +       h_sb = h_mnt->mnt_sb;
13304 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13305 +       lockdep_off();
13306 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13307 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13308 +                                     h_acceptable, /*context*/NULL);
13309 +       lockdep_on();
13310 +       dentry = h_parent;
13311 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13312 +               AuWarn1("%s decode_fh failed, %ld\n",
13313 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13314 +               goto out;
13315 +       }
13316 +       dentry = NULL;
13317 +       if (unlikely(au_test_anon(h_parent))) {
13318 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13319 +                       au_sbtype(h_sb));
13320 +               goto out_h_parent;
13321 +       }
13322 +
13323 +       dentry = ERR_PTR(-ENOMEM);
13324 +       pathname = (void *)__get_free_page(GFP_NOFS);
13325 +       if (unlikely(!pathname))
13326 +               goto out_h_parent;
13327 +
13328 +       root = sb->s_root;
13329 +       path.mnt = h_mnt;
13330 +       di_read_lock_parent(root, !AuLock_IR);
13331 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13332 +       di_read_unlock(root, !AuLock_IR);
13333 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13334 +       dentry = (void *)p;
13335 +       if (IS_ERR(p))
13336 +               goto out_pathname;
13337 +
13338 +       si_read_unlock(sb);
13339 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13340 +       dentry = ERR_PTR(err);
13341 +       if (unlikely(err))
13342 +               goto out_relock;
13343 +
13344 +       dentry = ERR_PTR(-ENOENT);
13345 +       AuDebugOn(au_test_anon(path.dentry));
13346 +       if (unlikely(d_really_is_negative(path.dentry)))
13347 +               goto out_path;
13348 +
13349 +       if (ino != d_inode(path.dentry)->i_ino)
13350 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13351 +       else
13352 +               dentry = dget(path.dentry);
13353 +
13354 +out_path:
13355 +       path_put(&path);
13356 +out_relock:
13357 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13358 +               if (!IS_ERR(dentry)) {
13359 +                       dput(dentry);
13360 +                       dentry = ERR_PTR(-ESTALE);
13361 +               }
13362 +out_pathname:
13363 +       free_page((unsigned long)pathname);
13364 +out_h_parent:
13365 +       dput(h_parent);
13366 +out:
13367 +       AuTraceErrPtr(dentry);
13368 +       return dentry;
13369 +}
13370 +
13371 +/* ---------------------------------------------------------------------- */
13372 +
13373 +static struct dentry *
13374 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13375 +                 int fh_type)
13376 +{
13377 +       struct dentry *dentry;
13378 +       __u32 *fh = fid->raw;
13379 +       struct au_branch *br;
13380 +       ino_t ino, dir_ino;
13381 +       struct au_nfsd_si_lock nsi_lock = {
13382 +               .force_lock     = 0
13383 +       };
13384 +
13385 +       dentry = ERR_PTR(-ESTALE);
13386 +       /* it should never happen, but the file handle is unreliable */
13387 +       if (unlikely(fh_len < Fh_tail))
13388 +               goto out;
13389 +       nsi_lock.sigen = fh[Fh_sigen];
13390 +       nsi_lock.br_id = fh[Fh_br_id];
13391 +
13392 +       /* branch id may be wrapped around */
13393 +       br = NULL;
13394 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13395 +               goto out;
13396 +       nsi_lock.force_lock = 1;
13397 +
13398 +       /* is this inode still cached? */
13399 +       ino = decode_ino(fh + Fh_ino);
13400 +       /* it should never happen */
13401 +       if (unlikely(ino == AUFS_ROOT_INO))
13402 +               goto out_unlock;
13403 +
13404 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13405 +       dentry = decode_by_ino(sb, ino, dir_ino);
13406 +       if (IS_ERR(dentry))
13407 +               goto out_unlock;
13408 +       if (dentry)
13409 +               goto accept;
13410 +
13411 +       /* is the parent dir cached? */
13412 +       br = au_sbr(sb, nsi_lock.bindex);
13413 +       au_lcnt_inc(&br->br_nfiles);
13414 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13415 +       if (IS_ERR(dentry))
13416 +               goto out_unlock;
13417 +       if (dentry)
13418 +               goto accept;
13419 +
13420 +       /* lookup path */
13421 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13422 +       if (IS_ERR(dentry))
13423 +               goto out_unlock;
13424 +       if (unlikely(!dentry))
13425 +               /* todo?: make it ESTALE */
13426 +               goto out_unlock;
13427 +
13428 +accept:
13429 +       if (!au_digen_test(dentry, au_sigen(sb))
13430 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13431 +               goto out_unlock; /* success */
13432 +
13433 +       dput(dentry);
13434 +       dentry = ERR_PTR(-ESTALE);
13435 +out_unlock:
13436 +       if (br)
13437 +               au_lcnt_dec(&br->br_nfiles);
13438 +       si_read_unlock(sb);
13439 +out:
13440 +       AuTraceErrPtr(dentry);
13441 +       return dentry;
13442 +}
13443 +
13444 +#if 0 /* reserved for future use */
13445 +/* support subtreecheck option */
13446 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13447 +                                       int fh_len, int fh_type)
13448 +{
13449 +       struct dentry *parent;
13450 +       __u32 *fh = fid->raw;
13451 +       ino_t dir_ino;
13452 +
13453 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13454 +       parent = decode_by_ino(sb, dir_ino, 0);
13455 +       if (IS_ERR(parent))
13456 +               goto out;
13457 +       if (!parent)
13458 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13459 +                                       dir_ino, fh, fh_len);
13460 +
13461 +out:
13462 +       AuTraceErrPtr(parent);
13463 +       return parent;
13464 +}
13465 +#endif
13466 +
13467 +/* ---------------------------------------------------------------------- */
13468 +
13469 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13470 +                         struct inode *dir)
13471 +{
13472 +       int err;
13473 +       aufs_bindex_t bindex;
13474 +       struct super_block *sb, *h_sb;
13475 +       struct dentry *dentry, *parent, *h_parent;
13476 +       struct inode *h_dir;
13477 +       struct au_branch *br;
13478 +
13479 +       err = -ENOSPC;
13480 +       if (unlikely(*max_len <= Fh_tail)) {
13481 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13482 +               goto out;
13483 +       }
13484 +
13485 +       err = FILEID_ROOT;
13486 +       if (inode->i_ino == AUFS_ROOT_INO) {
13487 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13488 +               goto out;
13489 +       }
13490 +
13491 +       h_parent = NULL;
13492 +       sb = inode->i_sb;
13493 +       err = si_read_lock(sb, AuLock_FLUSH);
13494 +       if (unlikely(err))
13495 +               goto out;
13496 +
13497 +#ifdef CONFIG_AUFS_DEBUG
13498 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13499 +               AuWarn1("NFS-exporting requires xino\n");
13500 +#endif
13501 +       err = -EIO;
13502 +       parent = NULL;
13503 +       ii_read_lock_child(inode);
13504 +       bindex = au_ibtop(inode);
13505 +       if (!dir) {
13506 +               dentry = d_find_any_alias(inode);
13507 +               if (unlikely(!dentry))
13508 +                       goto out_unlock;
13509 +               AuDebugOn(au_test_anon(dentry));
13510 +               parent = dget_parent(dentry);
13511 +               dput(dentry);
13512 +               if (unlikely(!parent))
13513 +                       goto out_unlock;
13514 +               if (d_really_is_positive(parent))
13515 +                       dir = d_inode(parent);
13516 +       }
13517 +
13518 +       ii_read_lock_parent(dir);
13519 +       h_dir = au_h_iptr(dir, bindex);
13520 +       ii_read_unlock(dir);
13521 +       if (unlikely(!h_dir))
13522 +               goto out_parent;
13523 +       h_parent = d_find_any_alias(h_dir);
13524 +       if (unlikely(!h_parent))
13525 +               goto out_hparent;
13526 +
13527 +       err = -EPERM;
13528 +       br = au_sbr(sb, bindex);
13529 +       h_sb = au_br_sb(br);
13530 +       if (unlikely(!h_sb->s_export_op)) {
13531 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13532 +               goto out_hparent;
13533 +       }
13534 +
13535 +       fh[Fh_br_id] = br->br_id;
13536 +       fh[Fh_sigen] = au_sigen(sb);
13537 +       encode_ino(fh + Fh_ino, inode->i_ino);
13538 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13539 +       fh[Fh_igen] = inode->i_generation;
13540 +
13541 +       *max_len -= Fh_tail;
13542 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13543 +                                          max_len,
13544 +                                          /*connectable or subtreecheck*/0);
13545 +       err = fh[Fh_h_type];
13546 +       *max_len += Fh_tail;
13547 +       /* todo: macros? */
13548 +       if (err != FILEID_INVALID)
13549 +               err = 99;
13550 +       else
13551 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13552 +
13553 +out_hparent:
13554 +       dput(h_parent);
13555 +out_parent:
13556 +       dput(parent);
13557 +out_unlock:
13558 +       ii_read_unlock(inode);
13559 +       si_read_unlock(sb);
13560 +out:
13561 +       if (unlikely(err < 0))
13562 +               err = FILEID_INVALID;
13563 +       return err;
13564 +}
13565 +
13566 +/* ---------------------------------------------------------------------- */
13567 +
13568 +static int aufs_commit_metadata(struct inode *inode)
13569 +{
13570 +       int err;
13571 +       aufs_bindex_t bindex;
13572 +       struct super_block *sb;
13573 +       struct inode *h_inode;
13574 +       int (*f)(struct inode *inode);
13575 +
13576 +       sb = inode->i_sb;
13577 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13578 +       ii_write_lock_child(inode);
13579 +       bindex = au_ibtop(inode);
13580 +       AuDebugOn(bindex < 0);
13581 +       h_inode = au_h_iptr(inode, bindex);
13582 +
13583 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13584 +       if (f)
13585 +               err = f(h_inode);
13586 +       else
13587 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13588 +
13589 +       au_cpup_attr_timesizes(inode);
13590 +       ii_write_unlock(inode);
13591 +       si_read_unlock(sb);
13592 +       return err;
13593 +}
13594 +
13595 +/* ---------------------------------------------------------------------- */
13596 +
13597 +static struct export_operations aufs_export_op = {
13598 +       .fh_to_dentry           = aufs_fh_to_dentry,
13599 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13600 +       .encode_fh              = aufs_encode_fh,
13601 +       .commit_metadata        = aufs_commit_metadata
13602 +};
13603 +
13604 +void au_export_init(struct super_block *sb)
13605 +{
13606 +       struct au_sbinfo *sbinfo;
13607 +       __u32 u;
13608 +
13609 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13610 +                        && IS_MODULE(CONFIG_EXPORTFS),
13611 +                        AUFS_NAME ": unsupported configuration "
13612 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13613 +
13614 +       sb->s_export_op = &aufs_export_op;
13615 +       sbinfo = au_sbi(sb);
13616 +       sbinfo->si_xigen = NULL;
13617 +       get_random_bytes(&u, sizeof(u));
13618 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13619 +       atomic_set(&sbinfo->si_xigen_next, u);
13620 +}
13621 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13622 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13623 +++ linux/fs/aufs/fhsm.c        2022-11-05 23:02:18.962555950 +0100
13624 @@ -0,0 +1,426 @@
13625 +// SPDX-License-Identifier: GPL-2.0
13626 +/*
13627 + * Copyright (C) 2011-2022 Junjiro R. Okajima
13628 + *
13629 + * This program is free software; you can redistribute it and/or modify
13630 + * it under the terms of the GNU General Public License as published by
13631 + * the Free Software Foundation; either version 2 of the License, or
13632 + * (at your option) any later version.
13633 + *
13634 + * This program is distributed in the hope that it will be useful,
13635 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13636 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13637 + * GNU General Public License for more details.
13638 + *
13639 + * You should have received a copy of the GNU General Public License
13640 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13641 + */
13642 +
13643 +/*
13644 + * File-based Hierarchy Storage Management
13645 + */
13646 +
13647 +#include <linux/anon_inodes.h>
13648 +#include <linux/poll.h>
13649 +#include <linux/seq_file.h>
13650 +#include <linux/statfs.h>
13651 +#include "aufs.h"
13652 +
13653 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13654 +{
13655 +       struct au_sbinfo *sbinfo;
13656 +       struct au_fhsm *fhsm;
13657 +
13658 +       SiMustAnyLock(sb);
13659 +
13660 +       sbinfo = au_sbi(sb);
13661 +       fhsm = &sbinfo->si_fhsm;
13662 +       AuDebugOn(!fhsm);
13663 +       return fhsm->fhsm_bottom;
13664 +}
13665 +
13666 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13667 +{
13668 +       struct au_sbinfo *sbinfo;
13669 +       struct au_fhsm *fhsm;
13670 +
13671 +       SiMustWriteLock(sb);
13672 +
13673 +       sbinfo = au_sbi(sb);
13674 +       fhsm = &sbinfo->si_fhsm;
13675 +       AuDebugOn(!fhsm);
13676 +       fhsm->fhsm_bottom = bindex;
13677 +}
13678 +
13679 +/* ---------------------------------------------------------------------- */
13680 +
13681 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13682 +{
13683 +       struct au_br_fhsm *bf;
13684 +
13685 +       bf = br->br_fhsm;
13686 +       MtxMustLock(&bf->bf_lock);
13687 +
13688 +       return !bf->bf_readable
13689 +               || time_after(jiffies,
13690 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13691 +}
13692 +
13693 +/* ---------------------------------------------------------------------- */
13694 +
13695 +static void au_fhsm_notify(struct super_block *sb, int val)
13696 +{
13697 +       struct au_sbinfo *sbinfo;
13698 +       struct au_fhsm *fhsm;
13699 +
13700 +       SiMustAnyLock(sb);
13701 +
13702 +       sbinfo = au_sbi(sb);
13703 +       fhsm = &sbinfo->si_fhsm;
13704 +       if (au_fhsm_pid(fhsm)
13705 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13706 +               atomic_set(&fhsm->fhsm_readable, val);
13707 +               if (val)
13708 +                       wake_up(&fhsm->fhsm_wqh);
13709 +       }
13710 +}
13711 +
13712 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13713 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13714 +{
13715 +       int err;
13716 +       struct au_branch *br;
13717 +       struct au_br_fhsm *bf;
13718 +
13719 +       br = au_sbr(sb, bindex);
13720 +       AuDebugOn(au_br_rdonly(br));
13721 +       bf = br->br_fhsm;
13722 +       AuDebugOn(!bf);
13723 +
13724 +       if (do_lock)
13725 +               mutex_lock(&bf->bf_lock);
13726 +       else
13727 +               MtxMustLock(&bf->bf_lock);
13728 +
13729 +       /* sb->s_root for NFS is unreliable */
13730 +       err = au_br_stfs(br, &bf->bf_stfs);
13731 +       if (unlikely(err)) {
13732 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13733 +               goto out;
13734 +       }
13735 +
13736 +       bf->bf_jiffy = jiffies;
13737 +       bf->bf_readable = 1;
13738 +       if (do_notify)
13739 +               au_fhsm_notify(sb, /*val*/1);
13740 +       if (rstfs)
13741 +               *rstfs = bf->bf_stfs;
13742 +
13743 +out:
13744 +       if (do_lock)
13745 +               mutex_unlock(&bf->bf_lock);
13746 +       au_fhsm_notify(sb, /*val*/1);
13747 +
13748 +       return err;
13749 +}
13750 +
13751 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13752 +{
13753 +       int err;
13754 +       struct au_sbinfo *sbinfo;
13755 +       struct au_fhsm *fhsm;
13756 +       struct au_branch *br;
13757 +       struct au_br_fhsm *bf;
13758 +
13759 +       AuDbg("b%d, force %d\n", bindex, force);
13760 +       SiMustAnyLock(sb);
13761 +
13762 +       sbinfo = au_sbi(sb);
13763 +       fhsm = &sbinfo->si_fhsm;
13764 +       if (!au_ftest_si(sbinfo, FHSM)
13765 +           || fhsm->fhsm_bottom == bindex)
13766 +               return;
13767 +
13768 +       br = au_sbr(sb, bindex);
13769 +       bf = br->br_fhsm;
13770 +       AuDebugOn(!bf);
13771 +       mutex_lock(&bf->bf_lock);
13772 +       if (force
13773 +           || au_fhsm_pid(fhsm)
13774 +           || au_fhsm_test_jiffy(sbinfo, br))
13775 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13776 +                                 /*do_notify*/1);
13777 +       mutex_unlock(&bf->bf_lock);
13778 +}
13779 +
13780 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13781 +{
13782 +       aufs_bindex_t bindex, bbot;
13783 +       struct au_branch *br;
13784 +
13785 +       /* exclude the bottom */
13786 +       bbot = au_fhsm_bottom(sb);
13787 +       for (bindex = 0; bindex < bbot; bindex++) {
13788 +               br = au_sbr(sb, bindex);
13789 +               if (au_br_fhsm(br->br_perm))
13790 +                       au_fhsm_wrote(sb, bindex, force);
13791 +       }
13792 +}
13793 +
13794 +/* ---------------------------------------------------------------------- */
13795 +
13796 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13797 +{
13798 +       __poll_t mask;
13799 +       struct au_sbinfo *sbinfo;
13800 +       struct au_fhsm *fhsm;
13801 +
13802 +       mask = 0;
13803 +       sbinfo = file->private_data;
13804 +       fhsm = &sbinfo->si_fhsm;
13805 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13806 +       if (atomic_read(&fhsm->fhsm_readable))
13807 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13808 +
13809 +       if (!mask)
13810 +               AuDbg("mask 0x%x\n", mask);
13811 +       return mask;
13812 +}
13813 +
13814 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13815 +                             struct aufs_stfs *stfs, __s16 brid)
13816 +{
13817 +       int err;
13818 +
13819 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13820 +       if (!err)
13821 +               err = __put_user(brid, &stbr->brid);
13822 +       if (unlikely(err))
13823 +               err = -EFAULT;
13824 +
13825 +       return err;
13826 +}
13827 +
13828 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13829 +                              struct aufs_stbr __user *stbr, size_t count)
13830 +{
13831 +       ssize_t err;
13832 +       int nstbr;
13833 +       aufs_bindex_t bindex, bbot;
13834 +       struct au_branch *br;
13835 +       struct au_br_fhsm *bf;
13836 +
13837 +       /* except the bottom branch */
13838 +       err = 0;
13839 +       nstbr = 0;
13840 +       bbot = au_fhsm_bottom(sb);
13841 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13842 +               br = au_sbr(sb, bindex);
13843 +               if (!au_br_fhsm(br->br_perm))
13844 +                       continue;
13845 +
13846 +               bf = br->br_fhsm;
13847 +               mutex_lock(&bf->bf_lock);
13848 +               if (bf->bf_readable) {
13849 +                       err = -EFAULT;
13850 +                       if (count >= sizeof(*stbr))
13851 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13852 +                                                         br->br_id);
13853 +                       if (!err) {
13854 +                               bf->bf_readable = 0;
13855 +                               count -= sizeof(*stbr);
13856 +                               nstbr++;
13857 +                       }
13858 +               }
13859 +               mutex_unlock(&bf->bf_lock);
13860 +       }
13861 +       if (!err)
13862 +               err = sizeof(*stbr) * nstbr;
13863 +
13864 +       return err;
13865 +}
13866 +
13867 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13868 +                          loff_t *pos)
13869 +{
13870 +       ssize_t err;
13871 +       int readable;
13872 +       aufs_bindex_t nfhsm, bindex, bbot;
13873 +       struct au_sbinfo *sbinfo;
13874 +       struct au_fhsm *fhsm;
13875 +       struct au_branch *br;
13876 +       struct super_block *sb;
13877 +
13878 +       err = 0;
13879 +       sbinfo = file->private_data;
13880 +       fhsm = &sbinfo->si_fhsm;
13881 +need_data:
13882 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13883 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13884 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13885 +                       err = -EAGAIN;
13886 +               else
13887 +                       err = wait_event_interruptible_locked_irq
13888 +                               (fhsm->fhsm_wqh,
13889 +                                atomic_read(&fhsm->fhsm_readable));
13890 +       }
13891 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13892 +       if (unlikely(err))
13893 +               goto out;
13894 +
13895 +       /* sb may already be dead */
13896 +       au_rw_read_lock(&sbinfo->si_rwsem);
13897 +       readable = atomic_read(&fhsm->fhsm_readable);
13898 +       if (readable > 0) {
13899 +               sb = sbinfo->si_sb;
13900 +               AuDebugOn(!sb);
13901 +               /* exclude the bottom branch */
13902 +               nfhsm = 0;
13903 +               bbot = au_fhsm_bottom(sb);
13904 +               for (bindex = 0; bindex < bbot; bindex++) {
13905 +                       br = au_sbr(sb, bindex);
13906 +                       if (au_br_fhsm(br->br_perm))
13907 +                               nfhsm++;
13908 +               }
13909 +               err = -EMSGSIZE;
13910 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13911 +                       atomic_set(&fhsm->fhsm_readable, 0);
13912 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13913 +                                            count);
13914 +               }
13915 +       }
13916 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13917 +       if (!readable)
13918 +               goto need_data;
13919 +
13920 +out:
13921 +       return err;
13922 +}
13923 +
13924 +static int au_fhsm_release(struct inode *inode, struct file *file)
13925 +{
13926 +       struct au_sbinfo *sbinfo;
13927 +       struct au_fhsm *fhsm;
13928 +
13929 +       /* sb may already be dead */
13930 +       sbinfo = file->private_data;
13931 +       fhsm = &sbinfo->si_fhsm;
13932 +       spin_lock(&fhsm->fhsm_spin);
13933 +       fhsm->fhsm_pid = 0;
13934 +       spin_unlock(&fhsm->fhsm_spin);
13935 +       kobject_put(&sbinfo->si_kobj);
13936 +
13937 +       return 0;
13938 +}
13939 +
13940 +static const struct file_operations au_fhsm_fops = {
13941 +       .owner          = THIS_MODULE,
13942 +       .llseek         = noop_llseek,
13943 +       .read           = au_fhsm_read,
13944 +       .poll           = au_fhsm_poll,
13945 +       .release        = au_fhsm_release
13946 +};
13947 +
13948 +int au_fhsm_fd(struct super_block *sb, int oflags)
13949 +{
13950 +       int err, fd;
13951 +       struct au_sbinfo *sbinfo;
13952 +       struct au_fhsm *fhsm;
13953 +
13954 +       err = -EPERM;
13955 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13956 +               goto out;
13957 +
13958 +       err = -EINVAL;
13959 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13960 +               goto out;
13961 +
13962 +       err = 0;
13963 +       sbinfo = au_sbi(sb);
13964 +       fhsm = &sbinfo->si_fhsm;
13965 +       spin_lock(&fhsm->fhsm_spin);
13966 +       if (!fhsm->fhsm_pid)
13967 +               fhsm->fhsm_pid = current->pid;
13968 +       else
13969 +               err = -EBUSY;
13970 +       spin_unlock(&fhsm->fhsm_spin);
13971 +       if (unlikely(err))
13972 +               goto out;
13973 +
13974 +       oflags |= O_RDONLY;
13975 +       /* oflags |= FMODE_NONOTIFY; */
13976 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13977 +       err = fd;
13978 +       if (unlikely(fd < 0))
13979 +               goto out_pid;
13980 +
13981 +       /* succeed regardless 'fhsm' status */
13982 +       kobject_get(&sbinfo->si_kobj);
13983 +       si_noflush_read_lock(sb);
13984 +       if (au_ftest_si(sbinfo, FHSM))
13985 +               au_fhsm_wrote_all(sb, /*force*/0);
13986 +       si_read_unlock(sb);
13987 +       goto out; /* success */
13988 +
13989 +out_pid:
13990 +       spin_lock(&fhsm->fhsm_spin);
13991 +       fhsm->fhsm_pid = 0;
13992 +       spin_unlock(&fhsm->fhsm_spin);
13993 +out:
13994 +       AuTraceErr(err);
13995 +       return err;
13996 +}
13997 +
13998 +/* ---------------------------------------------------------------------- */
13999 +
14000 +int au_fhsm_br_alloc(struct au_branch *br)
14001 +{
14002 +       int err;
14003 +
14004 +       err = 0;
14005 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14006 +       if (br->br_fhsm)
14007 +               au_br_fhsm_init(br->br_fhsm);
14008 +       else
14009 +               err = -ENOMEM;
14010 +
14011 +       return err;
14012 +}
14013 +
14014 +/* ---------------------------------------------------------------------- */
14015 +
14016 +void au_fhsm_fin(struct super_block *sb)
14017 +{
14018 +       au_fhsm_notify(sb, /*val*/-1);
14019 +}
14020 +
14021 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14022 +{
14023 +       struct au_fhsm *fhsm;
14024 +
14025 +       fhsm = &sbinfo->si_fhsm;
14026 +       spin_lock_init(&fhsm->fhsm_spin);
14027 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14028 +       atomic_set(&fhsm->fhsm_readable, 0);
14029 +       fhsm->fhsm_expire
14030 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14031 +       fhsm->fhsm_bottom = -1;
14032 +}
14033 +
14034 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14035 +{
14036 +       sbinfo->si_fhsm.fhsm_expire
14037 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14038 +}
14039 +
14040 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14041 +{
14042 +       unsigned int u;
14043 +
14044 +       if (!au_ftest_si(sbinfo, FHSM))
14045 +               return;
14046 +
14047 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14048 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14049 +               seq_printf(seq, ",fhsm_sec=%u", u);
14050 +}
14051 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14052 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14053 +++ linux/fs/aufs/file.c        2022-12-17 09:21:34.799855195 +0100
14054 @@ -0,0 +1,860 @@
14055 +// SPDX-License-Identifier: GPL-2.0
14056 +/*
14057 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14058 + *
14059 + * This program is free software; you can redistribute it and/or modify
14060 + * it under the terms of the GNU General Public License as published by
14061 + * the Free Software Foundation; either version 2 of the License, or
14062 + * (at your option) any later version.
14063 + *
14064 + * This program is distributed in the hope that it will be useful,
14065 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14066 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14067 + * GNU General Public License for more details.
14068 + *
14069 + * You should have received a copy of the GNU General Public License
14070 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14071 + */
14072 +
14073 +/*
14074 + * handling file/dir, and address_space operation
14075 + */
14076 +
14077 +#ifdef CONFIG_AUFS_DEBUG
14078 +#include <linux/migrate.h>
14079 +#endif
14080 +#include <linux/pagemap.h>
14081 +#include "aufs.h"
14082 +
14083 +/* drop flags for writing */
14084 +unsigned int au_file_roflags(unsigned int flags)
14085 +{
14086 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14087 +       flags |= O_RDONLY | O_NOATIME;
14088 +       return flags;
14089 +}
14090 +
14091 +/* common functions to regular file and dir */
14092 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14093 +                      struct file *file, int force_wr)
14094 +{
14095 +       struct file *h_file;
14096 +       struct dentry *h_dentry;
14097 +       struct inode *h_inode;
14098 +       struct super_block *sb;
14099 +       struct au_branch *br;
14100 +       struct path h_path;
14101 +       int err;
14102 +
14103 +       /* a race condition can happen between open and unlink/rmdir */
14104 +       h_file = ERR_PTR(-ENOENT);
14105 +       h_dentry = au_h_dptr(dentry, bindex);
14106 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14107 +               goto out;
14108 +       h_inode = d_inode(h_dentry);
14109 +       spin_lock(&h_dentry->d_lock);
14110 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14111 +               /* || !d_inode(dentry)->i_nlink */
14112 +               ;
14113 +       spin_unlock(&h_dentry->d_lock);
14114 +       if (unlikely(err))
14115 +               goto out;
14116 +
14117 +       sb = dentry->d_sb;
14118 +       br = au_sbr(sb, bindex);
14119 +       err = au_br_test_oflag(flags, br);
14120 +       h_file = ERR_PTR(err);
14121 +       if (unlikely(err))
14122 +               goto out;
14123 +
14124 +       /* drop flags for writing */
14125 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14126 +               if (force_wr && !(flags & O_WRONLY))
14127 +                       force_wr = 0;
14128 +               flags = au_file_roflags(flags);
14129 +               if (force_wr) {
14130 +                       h_file = ERR_PTR(-EROFS);
14131 +                       flags = au_file_roflags(flags);
14132 +                       if (unlikely(vfsub_native_ro(h_inode)
14133 +                                    || IS_APPEND(h_inode)))
14134 +                               goto out;
14135 +                       flags &= ~O_ACCMODE;
14136 +                       flags |= O_WRONLY;
14137 +               }
14138 +       }
14139 +       flags &= ~O_CREAT;
14140 +       au_lcnt_inc(&br->br_nfiles);
14141 +       h_path.dentry = h_dentry;
14142 +       h_path.mnt = au_br_mnt(br);
14143 +       h_file = vfsub_dentry_open(&h_path, flags);
14144 +       if (IS_ERR(h_file))
14145 +               goto out_br;
14146 +
14147 +       if (flags & __FMODE_EXEC) {
14148 +               err = deny_write_access(h_file);
14149 +               if (unlikely(err)) {
14150 +                       fput(h_file);
14151 +                       h_file = ERR_PTR(err);
14152 +                       goto out_br;
14153 +               }
14154 +       }
14155 +       fsnotify_open(h_file);
14156 +       goto out; /* success */
14157 +
14158 +out_br:
14159 +       au_lcnt_dec(&br->br_nfiles);
14160 +out:
14161 +       return h_file;
14162 +}
14163 +
14164 +static int au_cmoo(struct dentry *dentry)
14165 +{
14166 +       int err, cmoo, matched;
14167 +       unsigned int udba;
14168 +       struct path h_path;
14169 +       struct au_pin pin;
14170 +       struct au_cp_generic cpg = {
14171 +               .dentry = dentry,
14172 +               .bdst   = -1,
14173 +               .bsrc   = -1,
14174 +               .len    = -1,
14175 +               .pin    = &pin,
14176 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14177 +       };
14178 +       struct inode *delegated;
14179 +       struct super_block *sb;
14180 +       struct au_sbinfo *sbinfo;
14181 +       struct au_fhsm *fhsm;
14182 +       pid_t pid;
14183 +       struct au_branch *br;
14184 +       struct dentry *parent;
14185 +       struct au_hinode *hdir;
14186 +
14187 +       DiMustWriteLock(dentry);
14188 +       IiMustWriteLock(d_inode(dentry));
14189 +
14190 +       err = 0;
14191 +       if (IS_ROOT(dentry))
14192 +               goto out;
14193 +       cpg.bsrc = au_dbtop(dentry);
14194 +       if (!cpg.bsrc)
14195 +               goto out;
14196 +
14197 +       sb = dentry->d_sb;
14198 +       sbinfo = au_sbi(sb);
14199 +       fhsm = &sbinfo->si_fhsm;
14200 +       pid = au_fhsm_pid(fhsm);
14201 +       rcu_read_lock();
14202 +       matched = (pid
14203 +                  && (current->pid == pid
14204 +                      || rcu_dereference(current->real_parent)->pid == pid));
14205 +       rcu_read_unlock();
14206 +       if (matched)
14207 +               goto out;
14208 +
14209 +       br = au_sbr(sb, cpg.bsrc);
14210 +       cmoo = au_br_cmoo(br->br_perm);
14211 +       if (!cmoo)
14212 +               goto out;
14213 +       if (!d_is_reg(dentry))
14214 +               cmoo &= AuBrAttr_COO_ALL;
14215 +       if (!cmoo)
14216 +               goto out;
14217 +
14218 +       parent = dget_parent(dentry);
14219 +       di_write_lock_parent(parent);
14220 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14221 +       cpg.bdst = err;
14222 +       if (unlikely(err < 0)) {
14223 +               err = 0;        /* there is no upper writable branch */
14224 +               goto out_dgrade;
14225 +       }
14226 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14227 +
14228 +       /* do not respect the coo attrib for the target branch */
14229 +       err = au_cpup_dirs(dentry, cpg.bdst);
14230 +       if (unlikely(err))
14231 +               goto out_dgrade;
14232 +
14233 +       di_downgrade_lock(parent, AuLock_IR);
14234 +       udba = au_opt_udba(sb);
14235 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14236 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14237 +       if (unlikely(err))
14238 +               goto out_parent;
14239 +
14240 +       err = au_sio_cpup_simple(&cpg);
14241 +       au_unpin(&pin);
14242 +       if (unlikely(err))
14243 +               goto out_parent;
14244 +       if (!(cmoo & AuBrWAttr_MOO))
14245 +               goto out_parent; /* success */
14246 +
14247 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14248 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14249 +       if (unlikely(err))
14250 +               goto out_parent;
14251 +
14252 +       h_path.mnt = au_br_mnt(br);
14253 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14254 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14255 +       delegated = NULL;
14256 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14257 +       au_unpin(&pin);
14258 +       /* todo: keep h_dentry or not? */
14259 +       if (unlikely(err == -EWOULDBLOCK)) {
14260 +               pr_warn("cannot retry for NFSv4 delegation"
14261 +                       " for an internal unlink\n");
14262 +               iput(delegated);
14263 +       }
14264 +       if (unlikely(err)) {
14265 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14266 +                      dentry, err);
14267 +               err = 0;
14268 +       }
14269 +       goto out_parent; /* success */
14270 +
14271 +out_dgrade:
14272 +       di_downgrade_lock(parent, AuLock_IR);
14273 +out_parent:
14274 +       di_read_unlock(parent, AuLock_IR);
14275 +       dput(parent);
14276 +out:
14277 +       AuTraceErr(err);
14278 +       return err;
14279 +}
14280 +
14281 +int au_do_open(struct file *file, struct au_do_open_args *args)
14282 +{
14283 +       int err, aopen = args->aopen;
14284 +       struct dentry *dentry;
14285 +       struct au_finfo *finfo;
14286 +
14287 +       if (!aopen)
14288 +               err = au_finfo_init(file, args->fidir);
14289 +       else {
14290 +               lockdep_off();
14291 +               err = au_finfo_init(file, args->fidir);
14292 +               lockdep_on();
14293 +       }
14294 +       if (unlikely(err))
14295 +               goto out;
14296 +
14297 +       dentry = file->f_path.dentry;
14298 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14299 +       di_write_lock_child(dentry);
14300 +       err = au_cmoo(dentry);
14301 +       if (!err) {
14302 +               if (!aopen) {
14303 +                       err = args->open(file, vfsub_file_flags(file),
14304 +                                        au_di(dentry)->di_htmpfile);
14305 +                       di_write_unlock(dentry);
14306 +               } else {
14307 +                       di_downgrade_lock(dentry, AuLock_IR);
14308 +                       lockdep_off();
14309 +                       err = args->open(file, vfsub_file_flags(file),
14310 +                                        args->h_file);
14311 +                       lockdep_on();
14312 +                       di_read_unlock(dentry, AuLock_IR);
14313 +               }
14314 +       }
14315 +
14316 +       finfo = au_fi(file);
14317 +       if (!err) {
14318 +               finfo->fi_file = file;
14319 +               au_hbl_add(&finfo->fi_hlist,
14320 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14321 +       }
14322 +       if (!aopen)
14323 +               fi_write_unlock(file);
14324 +       else {
14325 +               lockdep_off();
14326 +               fi_write_unlock(file);
14327 +               lockdep_on();
14328 +       }
14329 +       if (unlikely(err)) {
14330 +               finfo->fi_hdir = NULL;
14331 +               au_finfo_fin(file);
14332 +       }
14333 +
14334 +out:
14335 +       AuTraceErr(err);
14336 +       return err;
14337 +}
14338 +
14339 +int au_reopen_nondir(struct file *file)
14340 +{
14341 +       int err;
14342 +       aufs_bindex_t btop;
14343 +       struct dentry *dentry;
14344 +       struct au_branch *br;
14345 +       struct file *h_file, *h_file_tmp;
14346 +
14347 +       dentry = file->f_path.dentry;
14348 +       btop = au_dbtop(dentry);
14349 +       br = au_sbr(dentry->d_sb, btop);
14350 +       h_file_tmp = NULL;
14351 +       if (au_fbtop(file) == btop) {
14352 +               h_file = au_hf_top(file);
14353 +               if (file->f_mode == h_file->f_mode)
14354 +                       return 0; /* success */
14355 +               h_file_tmp = h_file;
14356 +               get_file(h_file_tmp);
14357 +               au_lcnt_inc(&br->br_nfiles);
14358 +               au_set_h_fptr(file, btop, NULL);
14359 +       }
14360 +       AuDebugOn(au_fi(file)->fi_hdir);
14361 +       /*
14362 +        * it can happen
14363 +        * file exists on both of rw and ro
14364 +        * open --> dbtop and fbtop are both 0
14365 +        * prepend a branch as rw, "rw" become ro
14366 +        * remove rw/file
14367 +        * delete the top branch, "rw" becomes rw again
14368 +        *      --> dbtop is 1, fbtop is still 0
14369 +        * write --> fbtop is 0 but dbtop is 1
14370 +        */
14371 +       /* AuDebugOn(au_fbtop(file) < btop); */
14372 +
14373 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14374 +                          file, /*force_wr*/0);
14375 +       err = PTR_ERR(h_file);
14376 +       if (IS_ERR(h_file)) {
14377 +               if (h_file_tmp) {
14378 +                       /* revert */
14379 +                       au_set_h_fptr(file, btop, h_file_tmp);
14380 +                       h_file_tmp = NULL;
14381 +               }
14382 +               goto out; /* todo: close all? */
14383 +       }
14384 +
14385 +       err = 0;
14386 +       au_set_fbtop(file, btop);
14387 +       au_set_h_fptr(file, btop, h_file);
14388 +       au_update_figen(file);
14389 +       /* todo: necessary? */
14390 +       /* file->f_ra = h_file->f_ra; */
14391 +
14392 +out:
14393 +       if (h_file_tmp) {
14394 +               fput(h_file_tmp);
14395 +               au_lcnt_dec(&br->br_nfiles);
14396 +       }
14397 +       return err;
14398 +}
14399 +
14400 +/* ---------------------------------------------------------------------- */
14401 +
14402 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14403 +                       struct dentry *hi_wh)
14404 +{
14405 +       int err;
14406 +       aufs_bindex_t btop;
14407 +       struct au_dinfo *dinfo;
14408 +       struct dentry *h_dentry;
14409 +       struct au_hdentry *hdp;
14410 +
14411 +       dinfo = au_di(file->f_path.dentry);
14412 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14413 +
14414 +       btop = dinfo->di_btop;
14415 +       dinfo->di_btop = btgt;
14416 +       hdp = au_hdentry(dinfo, btgt);
14417 +       h_dentry = hdp->hd_dentry;
14418 +       hdp->hd_dentry = hi_wh;
14419 +       err = au_reopen_nondir(file);
14420 +       hdp->hd_dentry = h_dentry;
14421 +       dinfo->di_btop = btop;
14422 +
14423 +       return err;
14424 +}
14425 +
14426 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14427 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14428 +{
14429 +       int err;
14430 +       struct inode *inode, *h_inode;
14431 +       struct dentry *h_dentry, *hi_wh;
14432 +       struct au_cp_generic cpg = {
14433 +               .dentry = file->f_path.dentry,
14434 +               .bdst   = bcpup,
14435 +               .bsrc   = -1,
14436 +               .len    = len,
14437 +               .pin    = pin
14438 +       };
14439 +
14440 +       au_update_dbtop(cpg.dentry);
14441 +       inode = d_inode(cpg.dentry);
14442 +       h_inode = NULL;
14443 +       if (au_dbtop(cpg.dentry) <= bcpup
14444 +           && au_dbbot(cpg.dentry) >= bcpup) {
14445 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14446 +               if (h_dentry && d_is_positive(h_dentry))
14447 +                       h_inode = d_inode(h_dentry);
14448 +       }
14449 +       hi_wh = au_hi_wh(inode, bcpup);
14450 +       if (!hi_wh && !h_inode)
14451 +               err = au_sio_cpup_wh(&cpg, file);
14452 +       else
14453 +               /* already copied-up after unlink */
14454 +               err = au_reopen_wh(file, bcpup, hi_wh);
14455 +
14456 +       if (!err
14457 +           && (inode->i_nlink > 1
14458 +               || (inode->i_state & I_LINKABLE))
14459 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14460 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14461 +
14462 +       return err;
14463 +}
14464 +
14465 +/*
14466 + * prepare the @file for writing.
14467 + */
14468 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14469 +{
14470 +       int err;
14471 +       aufs_bindex_t dbtop;
14472 +       struct dentry *parent;
14473 +       struct inode *inode;
14474 +       struct super_block *sb;
14475 +       struct file *h_file;
14476 +       struct au_cp_generic cpg = {
14477 +               .dentry = file->f_path.dentry,
14478 +               .bdst   = -1,
14479 +               .bsrc   = -1,
14480 +               .len    = len,
14481 +               .pin    = pin,
14482 +               .flags  = AuCpup_DTIME
14483 +       };
14484 +
14485 +       sb = cpg.dentry->d_sb;
14486 +       inode = d_inode(cpg.dentry);
14487 +       cpg.bsrc = au_fbtop(file);
14488 +       err = au_test_ro(sb, cpg.bsrc, inode);
14489 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14490 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14491 +                            /*flags*/0);
14492 +               goto out;
14493 +       }
14494 +
14495 +       /* need to cpup or reopen */
14496 +       parent = dget_parent(cpg.dentry);
14497 +       di_write_lock_parent(parent);
14498 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14499 +       cpg.bdst = err;
14500 +       if (unlikely(err < 0))
14501 +               goto out_dgrade;
14502 +       err = 0;
14503 +
14504 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14505 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14506 +               if (unlikely(err))
14507 +                       goto out_dgrade;
14508 +       }
14509 +
14510 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14511 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14512 +       if (unlikely(err))
14513 +               goto out_dgrade;
14514 +
14515 +       dbtop = au_dbtop(cpg.dentry);
14516 +       if (dbtop <= cpg.bdst)
14517 +               cpg.bsrc = cpg.bdst;
14518 +
14519 +       if (dbtop <= cpg.bdst           /* just reopen */
14520 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14521 +               ) {
14522 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14523 +               if (IS_ERR(h_file))
14524 +                       err = PTR_ERR(h_file);
14525 +               else {
14526 +                       di_downgrade_lock(parent, AuLock_IR);
14527 +                       if (dbtop > cpg.bdst)
14528 +                               err = au_sio_cpup_simple(&cpg);
14529 +                       if (!err)
14530 +                               err = au_reopen_nondir(file);
14531 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14532 +               }
14533 +       } else {                        /* copyup as wh and reopen */
14534 +               /*
14535 +                * since writable hfsplus branch is not supported,
14536 +                * h_open_pre/post() are unnecessary.
14537 +                */
14538 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14539 +               di_downgrade_lock(parent, AuLock_IR);
14540 +       }
14541 +
14542 +       if (!err) {
14543 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14544 +               goto out_dput; /* success */
14545 +       }
14546 +       au_unpin(pin);
14547 +       goto out_unlock;
14548 +
14549 +out_dgrade:
14550 +       di_downgrade_lock(parent, AuLock_IR);
14551 +out_unlock:
14552 +       di_read_unlock(parent, AuLock_IR);
14553 +out_dput:
14554 +       dput(parent);
14555 +out:
14556 +       return err;
14557 +}
14558 +
14559 +/* ---------------------------------------------------------------------- */
14560 +
14561 +int au_do_flush(struct file *file, fl_owner_t id,
14562 +               int (*flush)(struct file *file, fl_owner_t id))
14563 +{
14564 +       int err;
14565 +       struct super_block *sb;
14566 +       struct inode *inode;
14567 +
14568 +       inode = file_inode(file);
14569 +       sb = inode->i_sb;
14570 +       si_noflush_read_lock(sb);
14571 +       fi_read_lock(file);
14572 +       ii_read_lock_child(inode);
14573 +
14574 +       err = flush(file, id);
14575 +       au_cpup_attr_timesizes(inode);
14576 +
14577 +       ii_read_unlock(inode);
14578 +       fi_read_unlock(file);
14579 +       si_read_unlock(sb);
14580 +       return err;
14581 +}
14582 +
14583 +/* ---------------------------------------------------------------------- */
14584 +
14585 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14586 +{
14587 +       int err;
14588 +       struct au_pin pin;
14589 +       struct au_finfo *finfo;
14590 +       struct dentry *parent, *hi_wh;
14591 +       struct inode *inode;
14592 +       struct super_block *sb;
14593 +       struct au_cp_generic cpg = {
14594 +               .dentry = file->f_path.dentry,
14595 +               .bdst   = -1,
14596 +               .bsrc   = -1,
14597 +               .len    = -1,
14598 +               .pin    = &pin,
14599 +               .flags  = AuCpup_DTIME
14600 +       };
14601 +
14602 +       FiMustWriteLock(file);
14603 +
14604 +       err = 0;
14605 +       finfo = au_fi(file);
14606 +       sb = cpg.dentry->d_sb;
14607 +       inode = d_inode(cpg.dentry);
14608 +       cpg.bdst = au_ibtop(inode);
14609 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14610 +               goto out;
14611 +
14612 +       parent = dget_parent(cpg.dentry);
14613 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14614 +               di_read_lock_parent(parent, !AuLock_IR);
14615 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14616 +               cpg.bdst = err;
14617 +               di_read_unlock(parent, !AuLock_IR);
14618 +               if (unlikely(err < 0))
14619 +                       goto out_parent;
14620 +               err = 0;
14621 +       }
14622 +
14623 +       di_read_lock_parent(parent, AuLock_IR);
14624 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14625 +       if (!S_ISDIR(inode->i_mode)
14626 +           && au_opt_test(au_mntflags(sb), PLINK)
14627 +           && au_plink_test(inode)
14628 +           && !d_unhashed(cpg.dentry)
14629 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14630 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14631 +               if (unlikely(err))
14632 +                       goto out_unlock;
14633 +
14634 +               /* always superio. */
14635 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14636 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14637 +               if (!err) {
14638 +                       err = au_sio_cpup_simple(&cpg);
14639 +                       au_unpin(&pin);
14640 +               }
14641 +       } else if (hi_wh) {
14642 +               /* already copied-up after unlink */
14643 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14644 +               *need_reopen = 0;
14645 +       }
14646 +
14647 +out_unlock:
14648 +       di_read_unlock(parent, AuLock_IR);
14649 +out_parent:
14650 +       dput(parent);
14651 +out:
14652 +       return err;
14653 +}
14654 +
14655 +static void au_do_refresh_dir(struct file *file)
14656 +{
14657 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14658 +       struct au_hfile *p, tmp, *q;
14659 +       struct au_finfo *finfo;
14660 +       struct super_block *sb;
14661 +       struct au_fidir *fidir;
14662 +
14663 +       FiMustWriteLock(file);
14664 +
14665 +       sb = file->f_path.dentry->d_sb;
14666 +       finfo = au_fi(file);
14667 +       fidir = finfo->fi_hdir;
14668 +       AuDebugOn(!fidir);
14669 +       p = fidir->fd_hfile + finfo->fi_btop;
14670 +       brid = p->hf_br->br_id;
14671 +       bbot = fidir->fd_bbot;
14672 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14673 +               if (!p->hf_file)
14674 +                       continue;
14675 +
14676 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14677 +               if (new_bindex == bindex)
14678 +                       continue;
14679 +               if (new_bindex < 0) {
14680 +                       au_set_h_fptr(file, bindex, NULL);
14681 +                       continue;
14682 +               }
14683 +
14684 +               /* swap two lower inode, and loop again */
14685 +               q = fidir->fd_hfile + new_bindex;
14686 +               tmp = *q;
14687 +               *q = *p;
14688 +               *p = tmp;
14689 +               if (tmp.hf_file) {
14690 +                       bindex--;
14691 +                       p--;
14692 +               }
14693 +       }
14694 +
14695 +       p = fidir->fd_hfile;
14696 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14697 +               bbot = au_sbbot(sb);
14698 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14699 +                    finfo->fi_btop++, p++)
14700 +                       if (p->hf_file) {
14701 +                               if (file_inode(p->hf_file))
14702 +                                       break;
14703 +                               au_hfput(p, /*execed*/0);
14704 +                       }
14705 +       } else {
14706 +               bbot = au_br_index(sb, brid);
14707 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14708 +                    finfo->fi_btop++, p++)
14709 +                       if (p->hf_file)
14710 +                               au_hfput(p, /*execed*/0);
14711 +               bbot = au_sbbot(sb);
14712 +       }
14713 +
14714 +       p = fidir->fd_hfile + bbot;
14715 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14716 +            fidir->fd_bbot--, p--)
14717 +               if (p->hf_file) {
14718 +                       if (file_inode(p->hf_file))
14719 +                               break;
14720 +                       au_hfput(p, /*execed*/0);
14721 +               }
14722 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14723 +}
14724 +
14725 +/*
14726 + * after branch manipulating, refresh the file.
14727 + */
14728 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14729 +{
14730 +       int err, need_reopen, nbr;
14731 +       aufs_bindex_t bbot, bindex;
14732 +       struct dentry *dentry;
14733 +       struct super_block *sb;
14734 +       struct au_finfo *finfo;
14735 +       struct au_hfile *hfile;
14736 +
14737 +       dentry = file->f_path.dentry;
14738 +       sb = dentry->d_sb;
14739 +       nbr = au_sbbot(sb) + 1;
14740 +       finfo = au_fi(file);
14741 +       if (!finfo->fi_hdir) {
14742 +               hfile = &finfo->fi_htop;
14743 +               AuDebugOn(!hfile->hf_file);
14744 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14745 +               AuDebugOn(bindex < 0);
14746 +               if (bindex != finfo->fi_btop)
14747 +                       au_set_fbtop(file, bindex);
14748 +       } else {
14749 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14750 +               if (unlikely(err))
14751 +                       goto out;
14752 +               au_do_refresh_dir(file);
14753 +       }
14754 +
14755 +       err = 0;
14756 +       need_reopen = 1;
14757 +       if (!au_test_mmapped(file))
14758 +               err = au_file_refresh_by_inode(file, &need_reopen);
14759 +       if (finfo->fi_hdir)
14760 +               /* harmless if err */
14761 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14762 +       if (!err && need_reopen && !d_unlinked(dentry))
14763 +               err = reopen(file);
14764 +       if (!err) {
14765 +               au_update_figen(file);
14766 +               goto out; /* success */
14767 +       }
14768 +
14769 +       /* error, close all lower files */
14770 +       if (finfo->fi_hdir) {
14771 +               bbot = au_fbbot_dir(file);
14772 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14773 +                       au_set_h_fptr(file, bindex, NULL);
14774 +       }
14775 +
14776 +out:
14777 +       return err;
14778 +}
14779 +
14780 +/* common function to regular file and dir */
14781 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14782 +                         int wlock, unsigned int fi_lsc)
14783 +{
14784 +       int err;
14785 +       unsigned int sigen, figen;
14786 +       aufs_bindex_t btop;
14787 +       unsigned char pseudo_link;
14788 +       struct dentry *dentry;
14789 +       struct inode *inode;
14790 +
14791 +       err = 0;
14792 +       dentry = file->f_path.dentry;
14793 +       inode = d_inode(dentry);
14794 +       sigen = au_sigen(dentry->d_sb);
14795 +       fi_write_lock_nested(file, fi_lsc);
14796 +       figen = au_figen(file);
14797 +       if (!fi_lsc)
14798 +               di_write_lock_child(dentry);
14799 +       else
14800 +               di_write_lock_child2(dentry);
14801 +       btop = au_dbtop(dentry);
14802 +       pseudo_link = (btop != au_ibtop(inode));
14803 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14804 +               if (!wlock) {
14805 +                       di_downgrade_lock(dentry, AuLock_IR);
14806 +                       fi_downgrade_lock(file);
14807 +               }
14808 +               goto out; /* success */
14809 +       }
14810 +
14811 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14812 +       if (au_digen_test(dentry, sigen)) {
14813 +               err = au_reval_dpath(dentry, sigen);
14814 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14815 +       }
14816 +
14817 +       if (!err)
14818 +               err = refresh_file(file, reopen);
14819 +       if (!err) {
14820 +               if (!wlock) {
14821 +                       di_downgrade_lock(dentry, AuLock_IR);
14822 +                       fi_downgrade_lock(file);
14823 +               }
14824 +       } else {
14825 +               di_write_unlock(dentry);
14826 +               fi_write_unlock(file);
14827 +       }
14828 +
14829 +out:
14830 +       return err;
14831 +}
14832 +
14833 +/* ---------------------------------------------------------------------- */
14834 +
14835 +/* cf. aufs_nopage() */
14836 +/* for madvise(2) */
14837 +static int aufs_read_folio(struct file *file __maybe_unused, struct folio *folio)
14838 +{
14839 +       folio_unlock(folio);
14840 +       return 0;
14841 +}
14842 +
14843 +/* it will never be called, but necessary to support O_DIRECT */
14844 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14845 +{ BUG(); return 0; }
14846 +
14847 +/* they will never be called. */
14848 +#ifdef CONFIG_AUFS_DEBUG
14849 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14850 +                           loff_t pos, unsigned len,
14851 +                           struct page **pagep, void **fsdata)
14852 +{ AuUnsupport(); return 0; }
14853 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14854 +                         loff_t pos, unsigned len, unsigned copied,
14855 +                         struct page *page, void *fsdata)
14856 +{ AuUnsupport(); return 0; }
14857 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14858 +{ AuUnsupport(); return 0; }
14859 +
14860 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14861 +{ AuUnsupport(); return true; }
14862 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14863 +{ AuUnsupport(); }
14864 +static bool aufs_release_folio(struct folio *folio, gfp_t gfp)
14865 +{ AuUnsupport(); return true; }
14866 +#if 0 /* called by memory compaction regardless file */
14867 +static int aufs_migrate_folio(struct address_space *mapping, struct folio *dst,
14868 +                             struct folio *src, enum migrate_mode mode)
14869 +{ AuUnsupport(); return 0; }
14870 +#endif
14871 +static int aufs_launder_folio(struct folio *folio)
14872 +{ AuUnsupport(); return 0; }
14873 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14874 +                                     size_t count)
14875 +{ AuUnsupport(); return true; }
14876 +static void aufs_is_dirty_writeback(struct folio *folio, bool *dirty,
14877 +                                   bool *writeback)
14878 +{ AuUnsupport(); }
14879 +static int aufs_error_remove_page(struct address_space *mapping,
14880 +                                 struct page *page)
14881 +{ AuUnsupport(); return 0; }
14882 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14883 +                             sector_t *span)
14884 +{ AuUnsupport(); return 0; }
14885 +static void aufs_swap_deactivate(struct file *file)
14886 +{ AuUnsupport(); }
14887 +static int aufs_swap_rw(struct kiocb *iocb, struct iov_iter *iter)
14888 +{ AuUnsupport(); return 0; }
14889 +#endif /* CONFIG_AUFS_DEBUG */
14890 +
14891 +const struct address_space_operations aufs_aop = {
14892 +       .read_folio             = aufs_read_folio,
14893 +       .direct_IO              = aufs_direct_IO,
14894 +#ifdef CONFIG_AUFS_DEBUG
14895 +       .writepage              = aufs_writepage,
14896 +       /* no writepages, because of writepage */
14897 +       .dirty_folio            = aufs_dirty_folio,
14898 +       /* no readpages, because of readpage */
14899 +       .write_begin            = aufs_write_begin,
14900 +       .write_end              = aufs_write_end,
14901 +       /* no bmap, no block device */
14902 +       .invalidate_folio       = aufs_invalidate_folio,
14903 +       .release_folio          = aufs_release_folio,
14904 +       /* is fallback_migrate_page ok? */
14905 +       /* .migrate_folio       = aufs_migrate_folio, */
14906 +       .launder_folio          = aufs_launder_folio,
14907 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14908 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14909 +       .error_remove_page      = aufs_error_remove_page,
14910 +       .swap_activate          = aufs_swap_activate,
14911 +       .swap_deactivate        = aufs_swap_deactivate,
14912 +       .swap_rw                = aufs_swap_rw
14913 +#endif /* CONFIG_AUFS_DEBUG */
14914 +};
14915 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14916 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14917 +++ linux/fs/aufs/file.h        2022-11-05 23:02:18.965889284 +0100
14918 @@ -0,0 +1,342 @@
14919 +/* SPDX-License-Identifier: GPL-2.0 */
14920 +/*
14921 + * Copyright (C) 2005-2022 Junjiro R. Okajima
14922 + *
14923 + * This program is free software; you can redistribute it and/or modify
14924 + * it under the terms of the GNU General Public License as published by
14925 + * the Free Software Foundation; either version 2 of the License, or
14926 + * (at your option) any later version.
14927 + *
14928 + * This program is distributed in the hope that it will be useful,
14929 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14930 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14931 + * GNU General Public License for more details.
14932 + *
14933 + * You should have received a copy of the GNU General Public License
14934 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14935 + */
14936 +
14937 +/*
14938 + * file operations
14939 + */
14940 +
14941 +#ifndef __AUFS_FILE_H__
14942 +#define __AUFS_FILE_H__
14943 +
14944 +#ifdef __KERNEL__
14945 +
14946 +#include <linux/file.h>
14947 +#include <linux/fs.h>
14948 +#include <linux/mm_types.h>
14949 +#include <linux/poll.h>
14950 +#include "rwsem.h"
14951 +
14952 +struct au_branch;
14953 +struct au_hfile {
14954 +       struct file             *hf_file;
14955 +       struct au_branch        *hf_br;
14956 +};
14957 +
14958 +struct au_vdir;
14959 +struct au_fidir {
14960 +       aufs_bindex_t           fd_bbot;
14961 +       aufs_bindex_t           fd_nent;
14962 +       struct au_vdir          *fd_vdir_cache;
14963 +       struct au_hfile         fd_hfile[];
14964 +};
14965 +
14966 +static inline int au_fidir_sz(int nent)
14967 +{
14968 +       AuDebugOn(nent < 0);
14969 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14970 +}
14971 +
14972 +struct au_finfo {
14973 +       atomic_t                fi_generation;
14974 +
14975 +       struct au_rwsem         fi_rwsem;
14976 +       aufs_bindex_t           fi_btop;
14977 +
14978 +       /* do not union them */
14979 +       struct {                                /* for non-dir */
14980 +               struct au_hfile                 fi_htop;
14981 +               atomic_t                        fi_mmapped;
14982 +       };
14983 +       struct au_fidir         *fi_hdir;       /* for dir only */
14984 +
14985 +       struct hlist_bl_node    fi_hlist;
14986 +       struct file             *fi_file;       /* very ugly */
14987 +       struct rcu_head         rcu;
14988 +} ____cacheline_aligned_in_smp;
14989 +
14990 +/* ---------------------------------------------------------------------- */
14991 +
14992 +/* file.c */
14993 +extern const struct address_space_operations aufs_aop;
14994 +unsigned int au_file_roflags(unsigned int flags);
14995 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14996 +                      struct file *file, int force_wr);
14997 +struct au_do_open_args {
14998 +       int             aopen;
14999 +       int             (*open)(struct file *file, int flags,
15000 +                               struct file *h_file);
15001 +       struct au_fidir *fidir;
15002 +       struct file     *h_file;
15003 +};
15004 +int au_do_open(struct file *file, struct au_do_open_args *args);
15005 +int au_reopen_nondir(struct file *file);
15006 +struct au_pin;
15007 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15008 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15009 +                         int wlock, unsigned int fi_lsc);
15010 +int au_do_flush(struct file *file, fl_owner_t id,
15011 +               int (*flush)(struct file *file, fl_owner_t id));
15012 +
15013 +/* poll.c */
15014 +#ifdef CONFIG_AUFS_POLL
15015 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15016 +#endif
15017 +
15018 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15019 +/* hfsplus.c */
15020 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15021 +                          int force_wr);
15022 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15023 +                   struct file *h_file);
15024 +#else
15025 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15026 +       aufs_bindex_t bindex, int force_wr)
15027 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15028 +          struct file *h_file);
15029 +#endif
15030 +
15031 +/* f_op.c */
15032 +extern const struct file_operations aufs_file_fop;
15033 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15034 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15035 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15036 +
15037 +/* finfo.c */
15038 +void au_hfput(struct au_hfile *hf, int execed);
15039 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15040 +                  struct file *h_file);
15041 +
15042 +void au_update_figen(struct file *file);
15043 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15044 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15045 +
15046 +void au_fi_init_once(void *_fi);
15047 +void au_finfo_fin(struct file *file);
15048 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15049 +
15050 +/* ioctl.c */
15051 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15052 +#ifdef CONFIG_COMPAT
15053 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15054 +                          unsigned long arg);
15055 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15056 +                             unsigned long arg);
15057 +#endif
15058 +
15059 +/* ---------------------------------------------------------------------- */
15060 +
15061 +static inline struct au_finfo *au_fi(struct file *file)
15062 +{
15063 +       return file->private_data;
15064 +}
15065 +
15066 +/* ---------------------------------------------------------------------- */
15067 +
15068 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15069 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15070 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15071 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15072 +/*
15073 +#define fi_read_trylock_nested(f) \
15074 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15075 +#define fi_write_trylock_nested(f) \
15076 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15077 +*/
15078 +
15079 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15080 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15081 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15082 +
15083 +/* lock subclass for finfo */
15084 +enum {
15085 +       AuLsc_FI_1,
15086 +       AuLsc_FI_2
15087 +};
15088 +
15089 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15090 +{
15091 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15092 +}
15093 +
15094 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15095 +{
15096 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15097 +}
15098 +
15099 +/*
15100 + * fi_read_lock_1, fi_write_lock_1,
15101 + * fi_read_lock_2, fi_write_lock_2
15102 + */
15103 +#define AuReadLockFunc(name) \
15104 +static inline void fi_read_lock_##name(struct file *f) \
15105 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15106 +
15107 +#define AuWriteLockFunc(name) \
15108 +static inline void fi_write_lock_##name(struct file *f) \
15109 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15110 +
15111 +#define AuRWLockFuncs(name) \
15112 +       AuReadLockFunc(name) \
15113 +       AuWriteLockFunc(name)
15114 +
15115 +AuRWLockFuncs(1);
15116 +AuRWLockFuncs(2);
15117 +
15118 +#undef AuReadLockFunc
15119 +#undef AuWriteLockFunc
15120 +#undef AuRWLockFuncs
15121 +
15122 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15123 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15124 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15125 +
15126 +/* ---------------------------------------------------------------------- */
15127 +
15128 +/* todo: hard/soft set? */
15129 +static inline aufs_bindex_t au_fbtop(struct file *file)
15130 +{
15131 +       FiMustAnyLock(file);
15132 +       return au_fi(file)->fi_btop;
15133 +}
15134 +
15135 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15136 +{
15137 +       FiMustAnyLock(file);
15138 +       AuDebugOn(!au_fi(file)->fi_hdir);
15139 +       return au_fi(file)->fi_hdir->fd_bbot;
15140 +}
15141 +
15142 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15143 +{
15144 +       FiMustAnyLock(file);
15145 +       AuDebugOn(!au_fi(file)->fi_hdir);
15146 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15147 +}
15148 +
15149 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15150 +{
15151 +       FiMustWriteLock(file);
15152 +       au_fi(file)->fi_btop = bindex;
15153 +}
15154 +
15155 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15156 +{
15157 +       FiMustWriteLock(file);
15158 +       AuDebugOn(!au_fi(file)->fi_hdir);
15159 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15160 +}
15161 +
15162 +static inline void au_set_fvdir_cache(struct file *file,
15163 +                                     struct au_vdir *vdir_cache)
15164 +{
15165 +       FiMustWriteLock(file);
15166 +       AuDebugOn(!au_fi(file)->fi_hdir);
15167 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15168 +}
15169 +
15170 +static inline struct file *au_hf_top(struct file *file)
15171 +{
15172 +       FiMustAnyLock(file);
15173 +       AuDebugOn(au_fi(file)->fi_hdir);
15174 +       return au_fi(file)->fi_htop.hf_file;
15175 +}
15176 +
15177 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15178 +{
15179 +       FiMustAnyLock(file);
15180 +       AuDebugOn(!au_fi(file)->fi_hdir);
15181 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15182 +}
15183 +
15184 +/* todo: memory barrier? */
15185 +static inline unsigned int au_figen(struct file *f)
15186 +{
15187 +       return atomic_read(&au_fi(f)->fi_generation);
15188 +}
15189 +
15190 +static inline void au_set_mmapped(struct file *f)
15191 +{
15192 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15193 +               return;
15194 +       pr_warn("fi_mmapped wrapped around\n");
15195 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15196 +               ;
15197 +}
15198 +
15199 +static inline void au_unset_mmapped(struct file *f)
15200 +{
15201 +       atomic_dec(&au_fi(f)->fi_mmapped);
15202 +}
15203 +
15204 +static inline int au_test_mmapped(struct file *f)
15205 +{
15206 +       return atomic_read(&au_fi(f)->fi_mmapped);
15207 +}
15208 +
15209 +/* customize vma->vm_file */
15210 +
15211 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15212 +                                      struct file *file)
15213 +{
15214 +       struct file *f;
15215 +
15216 +       f = vma->vm_file;
15217 +       get_file(file);
15218 +       vma->vm_file = file;
15219 +       fput(f);
15220 +}
15221 +
15222 +#ifdef CONFIG_MMU
15223 +#define AuDbgVmRegion(file, vma) do {} while (0)
15224 +
15225 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15226 +                                   struct file *file)
15227 +{
15228 +       au_do_vm_file_reset(vma, file);
15229 +}
15230 +#else
15231 +#define AuDbgVmRegion(file, vma) \
15232 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15233 +
15234 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15235 +                                   struct file *file)
15236 +{
15237 +       struct file *f;
15238 +
15239 +       au_do_vm_file_reset(vma, file);
15240 +       f = vma->vm_region->vm_file;
15241 +       get_file(file);
15242 +       vma->vm_region->vm_file = file;
15243 +       fput(f);
15244 +}
15245 +#endif /* CONFIG_MMU */
15246 +
15247 +/* handle vma->vm_prfile */
15248 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15249 +                                   struct file *file)
15250 +{
15251 +       get_file(file);
15252 +       vma->vm_prfile = file;
15253 +#ifndef CONFIG_MMU
15254 +       get_file(file);
15255 +       vma->vm_region->vm_prfile = file;
15256 +#endif
15257 +}
15258 +
15259 +#endif /* __KERNEL__ */
15260 +#endif /* __AUFS_FILE_H__ */
15261 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15262 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15263 +++ linux/fs/aufs/finfo.c       2022-11-05 23:02:18.965889284 +0100
15264 @@ -0,0 +1,149 @@
15265 +// SPDX-License-Identifier: GPL-2.0
15266 +/*
15267 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15268 + *
15269 + * This program is free software; you can redistribute it and/or modify
15270 + * it under the terms of the GNU General Public License as published by
15271 + * the Free Software Foundation; either version 2 of the License, or
15272 + * (at your option) any later version.
15273 + *
15274 + * This program is distributed in the hope that it will be useful,
15275 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15276 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15277 + * GNU General Public License for more details.
15278 + *
15279 + * You should have received a copy of the GNU General Public License
15280 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15281 + */
15282 +
15283 +/*
15284 + * file private data
15285 + */
15286 +
15287 +#include "aufs.h"
15288 +
15289 +void au_hfput(struct au_hfile *hf, int execed)
15290 +{
15291 +       if (execed)
15292 +               allow_write_access(hf->hf_file);
15293 +       fput(hf->hf_file);
15294 +       hf->hf_file = NULL;
15295 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15296 +       hf->hf_br = NULL;
15297 +}
15298 +
15299 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15300 +{
15301 +       struct au_finfo *finfo = au_fi(file);
15302 +       struct au_hfile *hf;
15303 +       struct au_fidir *fidir;
15304 +
15305 +       fidir = finfo->fi_hdir;
15306 +       if (!fidir) {
15307 +               AuDebugOn(finfo->fi_btop != bindex);
15308 +               hf = &finfo->fi_htop;
15309 +       } else
15310 +               hf = fidir->fd_hfile + bindex;
15311 +
15312 +       if (hf && hf->hf_file)
15313 +               au_hfput(hf, vfsub_file_execed(file));
15314 +       if (val) {
15315 +               FiMustWriteLock(file);
15316 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15317 +               hf->hf_file = val;
15318 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15319 +       }
15320 +}
15321 +
15322 +void au_update_figen(struct file *file)
15323 +{
15324 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15325 +       /* smp_mb(); */ /* atomic_set */
15326 +}
15327 +
15328 +/* ---------------------------------------------------------------------- */
15329 +
15330 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15331 +{
15332 +       struct au_fidir *fidir;
15333 +       int nbr;
15334 +
15335 +       nbr = au_sbbot(sb) + 1;
15336 +       if (nbr < 2)
15337 +               nbr = 2; /* initial allocate for 2 branches */
15338 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15339 +       if (fidir) {
15340 +               fidir->fd_bbot = -1;
15341 +               fidir->fd_nent = nbr;
15342 +       }
15343 +
15344 +       return fidir;
15345 +}
15346 +
15347 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15348 +{
15349 +       int err;
15350 +       struct au_fidir *fidir, *p;
15351 +
15352 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15353 +       fidir = finfo->fi_hdir;
15354 +       AuDebugOn(!fidir);
15355 +
15356 +       err = -ENOMEM;
15357 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15358 +                        GFP_NOFS, may_shrink);
15359 +       if (p) {
15360 +               p->fd_nent = nbr;
15361 +               finfo->fi_hdir = p;
15362 +               err = 0;
15363 +       }
15364 +
15365 +       return err;
15366 +}
15367 +
15368 +/* ---------------------------------------------------------------------- */
15369 +
15370 +void au_finfo_fin(struct file *file)
15371 +{
15372 +       struct au_finfo *finfo;
15373 +
15374 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15375 +
15376 +       finfo = au_fi(file);
15377 +       AuDebugOn(finfo->fi_hdir);
15378 +       AuRwDestroy(&finfo->fi_rwsem);
15379 +       au_cache_free_finfo(finfo);
15380 +}
15381 +
15382 +void au_fi_init_once(void *_finfo)
15383 +{
15384 +       struct au_finfo *finfo = _finfo;
15385 +
15386 +       au_rw_init(&finfo->fi_rwsem);
15387 +}
15388 +
15389 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15390 +{
15391 +       int err;
15392 +       struct au_finfo *finfo;
15393 +       struct dentry *dentry;
15394 +
15395 +       err = -ENOMEM;
15396 +       dentry = file->f_path.dentry;
15397 +       finfo = au_cache_alloc_finfo();
15398 +       if (unlikely(!finfo))
15399 +               goto out;
15400 +
15401 +       err = 0;
15402 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15403 +       au_rw_write_lock(&finfo->fi_rwsem);
15404 +       finfo->fi_btop = -1;
15405 +       finfo->fi_hdir = fidir;
15406 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15407 +       /* smp_mb(); */ /* atomic_set */
15408 +
15409 +       file->private_data = finfo;
15410 +
15411 +out:
15412 +       return err;
15413 +}
15414 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15415 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15416 +++ linux/fs/aufs/f_op.c        2022-12-17 09:21:34.799855195 +0100
15417 @@ -0,0 +1,776 @@
15418 +// SPDX-License-Identifier: GPL-2.0
15419 +/*
15420 + * Copyright (C) 2005-2022 Junjiro R. Okajima
15421 + *
15422 + * This program is free software; you can redistribute it and/or modify
15423 + * it under the terms of the GNU General Public License as published by
15424 + * the Free Software Foundation; either version 2 of the License, or
15425 + * (at your option) any later version.
15426 + *
15427 + * This program is distributed in the hope that it will be useful,
15428 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15429 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15430 + * GNU General Public License for more details.
15431 + *
15432 + * You should have received a copy of the GNU General Public License
15433 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15434 + */
15435 +
15436 +/*
15437 + * file and vm operations
15438 + */
15439 +
15440 +#include <linux/aio.h>
15441 +#include <linux/fs_stack.h>
15442 +#include <linux/mman.h>
15443 +#include <linux/security.h>
15444 +#include "aufs.h"
15445 +
15446 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15447 +{
15448 +       int err;
15449 +       aufs_bindex_t bindex;
15450 +       struct dentry *dentry, *h_dentry;
15451 +       struct au_finfo *finfo;
15452 +       struct inode *h_inode;
15453 +
15454 +       FiMustWriteLock(file);
15455 +
15456 +       err = 0;
15457 +       dentry = file->f_path.dentry;
15458 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15459 +       finfo = au_fi(file);
15460 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15461 +       atomic_set(&finfo->fi_mmapped, 0);
15462 +       bindex = au_dbtop(dentry);
15463 +       if (!h_file) {
15464 +               h_dentry = au_h_dptr(dentry, bindex);
15465 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15466 +               if (unlikely(err))
15467 +                       goto out;
15468 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15469 +               if (IS_ERR(h_file)) {
15470 +                       err = PTR_ERR(h_file);
15471 +                       goto out;
15472 +               }
15473 +       } else {
15474 +               h_dentry = h_file->f_path.dentry;
15475 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15476 +               if (unlikely(err))
15477 +                       goto out;
15478 +               /* br ref is already inc-ed */
15479 +       }
15480 +
15481 +       if (flags & __O_TMPFILE) {
15482 +               AuDebugOn(!h_file);
15483 +               AuDebugOn(h_file != au_di(dentry)->di_htmpfile);
15484 +               au_di(dentry)->di_htmpfile = NULL;
15485 +
15486 +               if (!(flags & O_EXCL)) {
15487 +                       h_inode = file_inode(h_file);
15488 +                       spin_lock(&h_inode->i_lock);
15489 +                       h_inode->i_state |= I_LINKABLE;
15490 +                       spin_unlock(&h_inode->i_lock);
15491 +               }
15492 +       }
15493 +       au_set_fbtop(file, bindex);
15494 +       au_set_h_fptr(file, bindex, h_file);
15495 +       au_update_figen(file);
15496 +       /* todo: necessary? */
15497 +       /* file->f_ra = h_file->f_ra; */
15498 +
15499 +out:
15500 +       return err;
15501 +}
15502 +
15503 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15504 +                           struct file *file)
15505 +{
15506 +       int err;
15507 +       struct super_block *sb;
15508 +       struct au_do_open_args args = {
15509 +               .open   = au_do_open_nondir
15510 +       };
15511 +
15512 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15513 +             file, vfsub_file_flags(file), file->f_mode);
15514 +
15515 +       sb = file->f_path.dentry->d_sb;
15516 +       si_read_lock(sb, AuLock_FLUSH);
15517 +       err = au_do_open(file, &args);
15518 +       si_read_unlock(sb);
15519 +       return err;
15520 +}
15521 +
15522 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15523 +{
15524 +       struct au_finfo *finfo;
15525 +       aufs_bindex_t bindex;
15526 +
15527 +       finfo = au_fi(file);
15528 +       au_hbl_del(&finfo->fi_hlist,
15529 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15530 +       bindex = finfo->fi_btop;
15531 +       if (bindex >= 0)
15532 +               au_set_h_fptr(file, bindex, NULL);
15533 +
15534 +       au_finfo_fin(file);
15535 +       return 0;
15536 +}
15537 +
15538 +/* ---------------------------------------------------------------------- */
15539 +
15540 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15541 +{
15542 +       int err;
15543 +       struct file *h_file;
15544 +
15545 +       err = 0;
15546 +       h_file = au_hf_top(file);
15547 +       if (h_file)
15548 +               err = vfsub_flush(h_file, id);
15549 +       return err;
15550 +}
15551 +
15552 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15553 +{
15554 +       return au_do_flush(file, id, au_do_flush_nondir);
15555 +}
15556 +
15557 +/* ---------------------------------------------------------------------- */
15558 +/*
15559 + * read and write functions acquire [fdi]_rwsem once, but release before
15560 + * mmap_sem. This is because to stop a race condition between mmap(2).
15561 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15562 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15563 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15564 + */
15565 +
15566 +/* Callers should call au_read_post() or fput() in the end */
15567 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15568 +{
15569 +       struct file *h_file;
15570 +       int err;
15571 +
15572 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15573 +       if (!err) {
15574 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15575 +               h_file = au_hf_top(file);
15576 +               get_file(h_file);
15577 +               if (!keep_fi)
15578 +                       fi_read_unlock(file);
15579 +       } else
15580 +               h_file = ERR_PTR(err);
15581 +
15582 +       return h_file;
15583 +}
15584 +
15585 +static void au_read_post(struct inode *inode, struct file *h_file)
15586 +{
15587 +       /* update without lock, I don't think it a problem */
15588 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15589 +       fput(h_file);
15590 +}
15591 +
15592 +struct au_write_pre {
15593 +       /* input */
15594 +       unsigned int lsc;
15595 +
15596 +       /* output */
15597 +       blkcnt_t blks;
15598 +       aufs_bindex_t btop;
15599 +};
15600 +
15601 +/*
15602 + * return with iinfo is write-locked
15603 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15604 + * end
15605 + */
15606 +static struct file *au_write_pre(struct file *file, int do_ready,
15607 +                                struct au_write_pre *wpre)
15608 +{
15609 +       struct file *h_file;
15610 +       struct dentry *dentry;
15611 +       int err;
15612 +       unsigned int lsc;
15613 +       struct au_pin pin;
15614 +
15615 +       lsc = 0;
15616 +       if (wpre)
15617 +               lsc = wpre->lsc;
15618 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15619 +       h_file = ERR_PTR(err);
15620 +       if (unlikely(err))
15621 +               goto out;
15622 +
15623 +       dentry = file->f_path.dentry;
15624 +       if (do_ready) {
15625 +               err = au_ready_to_write(file, -1, &pin);
15626 +               if (unlikely(err)) {
15627 +                       h_file = ERR_PTR(err);
15628 +                       di_write_unlock(dentry);
15629 +                       goto out_fi;
15630 +               }
15631 +       }
15632 +
15633 +       di_downgrade_lock(dentry, /*flags*/0);
15634 +       if (wpre)
15635 +               wpre->btop = au_fbtop(file);
15636 +       h_file = au_hf_top(file);
15637 +       get_file(h_file);
15638 +       if (wpre)
15639 +               wpre->blks = file_inode(h_file)->i_blocks;
15640 +       if (do_ready)
15641 +               au_unpin(&pin);
15642 +       di_read_unlock(dentry, /*flags*/0);
15643 +
15644 +out_fi:
15645 +       fi_write_unlock(file);
15646 +out:
15647 +       return h_file;
15648 +}
15649 +
15650 +static void au_write_post(struct inode *inode, struct file *h_file,
15651 +                         struct au_write_pre *wpre, ssize_t written)
15652 +{
15653 +       struct inode *h_inode;
15654 +
15655 +       au_cpup_attr_timesizes(inode);
15656 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15657 +       h_inode = file_inode(h_file);
15658 +       inode->i_mode = h_inode->i_mode;
15659 +       ii_write_unlock(inode);
15660 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15661 +       if (written > 0)
15662 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15663 +                             /*force*/h_inode->i_blocks > wpre->blks);
15664 +       fput(h_file);
15665 +}
15666 +
15667 +/*
15668 + * todo: very ugly
15669 + * it locks both of i_mutex and si_rwsem for read in safe.
15670 + * if the plink maintenance mode continues forever (that is the problem),
15671 + * may loop forever.
15672 + */
15673 +static void au_mtx_and_read_lock(struct inode *inode)
15674 +{
15675 +       int err;
15676 +       struct super_block *sb = inode->i_sb;
15677 +
15678 +       while (1) {
15679 +               inode_lock(inode);
15680 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15681 +               if (!err)
15682 +                       break;
15683 +               inode_unlock(inode);
15684 +               si_read_lock(sb, AuLock_NOPLMW);
15685 +               si_read_unlock(sb);
15686 +       }
15687 +}
15688 +
15689 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15690 +                         struct iov_iter *iov_iter)
15691 +{
15692 +       ssize_t err;
15693 +       struct file *file;
15694 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15695 +
15696 +       err = security_file_permission(h_file, rw);
15697 +       if (unlikely(err))
15698 +               goto out;
15699 +
15700 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15701 +       iter = NULL;
15702 +       if (rw == MAY_READ)
15703 +               iter = h_file->f_op->read_iter;
15704 +       else if (rw == MAY_WRITE)
15705 +               iter = h_file->f_op->write_iter;
15706 +
15707 +       file = kio->ki_filp;
15708 +       kio->ki_filp = h_file;
15709 +       if (iter) {
15710 +               lockdep_off();
15711 +               err = iter(kio, iov_iter);
15712 +               lockdep_on();
15713 +       } else
15714 +               /* currently there is no such fs */
15715 +               WARN_ON_ONCE(1);
15716 +       kio->ki_filp = file;
15717 +
15718 +out:
15719 +       return err;
15720 +}
15721 +
15722 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15723 +{
15724 +       ssize_t err;
15725 +       struct file *file, *h_file;
15726 +       struct inode *inode;
15727 +       struct super_block *sb;
15728 +
15729 +       file = kio->ki_filp;
15730 +       inode = file_inode(file);
15731 +       sb = inode->i_sb;
15732 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15733 +
15734 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15735 +       err = PTR_ERR(h_file);
15736 +       if (IS_ERR(h_file))
15737 +               goto out;
15738 +
15739 +       if (au_test_loopback_kthread()) {
15740 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15741 +               if (file->f_mapping != h_file->f_mapping) {
15742 +                       file->f_mapping = h_file->f_mapping;
15743 +                       smp_mb(); /* unnecessary? */
15744 +               }
15745 +       }
15746 +       fi_read_unlock(file);
15747 +
15748 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15749 +       /* todo: necessary? */
15750 +       /* file->f_ra = h_file->f_ra; */
15751 +       au_read_post(inode, h_file);
15752 +
15753 +out:
15754 +       si_read_unlock(sb);
15755 +       return err;
15756 +}
15757 +
15758 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15759 +{
15760 +       ssize_t err;
15761 +       struct au_write_pre wpre;
15762 +       struct inode *inode;
15763 +       struct file *file, *h_file;
15764 +
15765 +       file = kio->ki_filp;
15766 +       inode = file_inode(file);
15767 +       au_mtx_and_read_lock(inode);
15768 +
15769 +       wpre.lsc = 0;
15770 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15771 +       err = PTR_ERR(h_file);
15772 +       if (IS_ERR(h_file))
15773 +               goto out;
15774 +
15775 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15776 +       au_write_post(inode, h_file, &wpre, err);
15777 +
15778 +out:
15779 +       si_read_unlock(inode->i_sb);
15780 +       inode_unlock(inode);
15781 +       return err;
15782 +}
15783 +
15784 +/*
15785 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15786 + * don't have their own .splice_{read,write} implimentations, and they use
15787 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15788 + * simple converters to f_op->iter_read() and ->iter_write().
15789 + * But we keep our own implementations because some non-mainlined FSes may have
15790 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15791 + * away an opportunity to co-work with aufs from them.
15792 + */
15793 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15794 +                               struct pipe_inode_info *pipe, size_t len,
15795 +                               unsigned int flags)
15796 +{
15797 +       ssize_t err;
15798 +       struct file *h_file;
15799 +       struct inode *inode;
15800 +       struct super_block *sb;
15801 +
15802 +       inode = file_inode(file);
15803 +       sb = inode->i_sb;
15804 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15805 +
15806 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15807 +       err = PTR_ERR(h_file);
15808 +       if (IS_ERR(h_file))
15809 +               goto out;
15810 +
15811 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15812 +       /* todo: necessary? */
15813 +       /* file->f_ra = h_file->f_ra; */
15814 +       au_read_post(inode, h_file);
15815 +
15816 +out:
15817 +       si_read_unlock(sb);
15818 +       return err;
15819 +}
15820 +
15821 +static ssize_t
15822 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15823 +                 size_t len, unsigned int flags)
15824 +{
15825 +       ssize_t err;
15826 +       struct au_write_pre wpre;
15827 +       struct inode *inode;
15828 +       struct file *h_file;
15829 +
15830 +       inode = file_inode(file);
15831 +       au_mtx_and_read_lock(inode);
15832 +
15833 +       wpre.lsc = 0;
15834 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15835 +       err = PTR_ERR(h_file);
15836 +       if (IS_ERR(h_file))
15837 +               goto out;
15838 +
15839 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15840 +       au_write_post(inode, h_file, &wpre, err);
15841 +
15842 +out:
15843 +       si_read_unlock(inode->i_sb);
15844 +       inode_unlock(inode);
15845 +       return err;
15846 +}
15847 +
15848 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15849 +                          loff_t len)
15850 +{
15851 +       long err;
15852 +       struct au_write_pre wpre;
15853 +       struct inode *inode;
15854 +       struct file *h_file;
15855 +
15856 +       inode = file_inode(file);
15857 +       au_mtx_and_read_lock(inode);
15858 +
15859 +       wpre.lsc = 0;
15860 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15861 +       err = PTR_ERR(h_file);
15862 +       if (IS_ERR(h_file))
15863 +               goto out;
15864 +
15865 +       lockdep_off();
15866 +       err = vfs_fallocate(h_file, mode, offset, len);
15867 +       lockdep_on();
15868 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15869 +
15870 +out:
15871 +       si_read_unlock(inode->i_sb);
15872 +       inode_unlock(inode);
15873 +       return err;
15874 +}
15875 +
15876 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15877 +                                   struct file *dst, loff_t dst_pos,
15878 +                                   size_t len, unsigned int flags)
15879 +{
15880 +       ssize_t err;
15881 +       struct au_write_pre wpre;
15882 +       enum { SRC, DST };
15883 +       struct {
15884 +               struct inode *inode;
15885 +               struct file *h_file;
15886 +               struct super_block *h_sb;
15887 +       } a[2];
15888 +#define a_src  a[SRC]
15889 +#define a_dst  a[DST]
15890 +
15891 +       err = -EINVAL;
15892 +       a_src.inode = file_inode(src);
15893 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15894 +               goto out;
15895 +       a_dst.inode = file_inode(dst);
15896 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15897 +               goto out;
15898 +
15899 +       au_mtx_and_read_lock(a_dst.inode);
15900 +       /*
15901 +        * in order to match the order in di_write_lock2_{child,parent}(),
15902 +        * use f_path.dentry for this comparison.
15903 +        */
15904 +       if (src->f_path.dentry < dst->f_path.dentry) {
15905 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15906 +               err = PTR_ERR(a_src.h_file);
15907 +               if (IS_ERR(a_src.h_file))
15908 +                       goto out_si;
15909 +
15910 +               wpre.lsc = AuLsc_FI_2;
15911 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15912 +               err = PTR_ERR(a_dst.h_file);
15913 +               if (IS_ERR(a_dst.h_file)) {
15914 +                       au_read_post(a_src.inode, a_src.h_file);
15915 +                       goto out_si;
15916 +               }
15917 +       } else {
15918 +               wpre.lsc = AuLsc_FI_1;
15919 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15920 +               err = PTR_ERR(a_dst.h_file);
15921 +               if (IS_ERR(a_dst.h_file))
15922 +                       goto out_si;
15923 +
15924 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15925 +               err = PTR_ERR(a_src.h_file);
15926 +               if (IS_ERR(a_src.h_file)) {
15927 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15928 +                                     /*written*/0);
15929 +                       goto out_si;
15930 +               }
15931 +       }
15932 +
15933 +       err = -EXDEV;
15934 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15935 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15936 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15937 +               AuDbgFile(src);
15938 +               AuDbgFile(dst);
15939 +               goto out_file;
15940 +       }
15941 +
15942 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15943 +                                   dst_pos, len, flags);
15944 +
15945 +out_file:
15946 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15947 +       fi_read_unlock(src);
15948 +       au_read_post(a_src.inode, a_src.h_file);
15949 +out_si:
15950 +       si_read_unlock(a_dst.inode->i_sb);
15951 +       inode_unlock(a_dst.inode);
15952 +out:
15953 +       return err;
15954 +#undef a_src
15955 +#undef a_dst
15956 +}
15957 +
15958 +/* ---------------------------------------------------------------------- */
15959 +
15960 +/*
15961 + * The locking order around current->mmap_sem.
15962 + * - in most and regular cases
15963 + *   file I/O syscall -- aufs_read() or something
15964 + *     -- si_rwsem for read -- mmap_sem
15965 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15966 + * - in mmap case
15967 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15968 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15969 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15970 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15971 + * It means that when aufs acquires si_rwsem for write, the process should never
15972 + * acquire mmap_sem.
15973 + *
15974 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15975 + * problem either since any directory is not able to be mmap-ed.
15976 + * The similar scenario is applied to aufs_readlink() too.
15977 + */
15978 +
15979 +#if 0 /* stop calling security_file_mmap() */
15980 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15981 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15982 +
15983 +static unsigned long au_arch_prot_conv(unsigned long flags)
15984 +{
15985 +       /* currently ppc64 only */
15986 +#ifdef CONFIG_PPC64
15987 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15988 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15989 +       return AuConv_VM_PROT(flags, SAO);
15990 +#else
15991 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15992 +       return 0;
15993 +#endif
15994 +}
15995 +
15996 +static unsigned long au_prot_conv(unsigned long flags)
15997 +{
15998 +       return AuConv_VM_PROT(flags, READ)
15999 +               | AuConv_VM_PROT(flags, WRITE)
16000 +               | AuConv_VM_PROT(flags, EXEC)
16001 +               | au_arch_prot_conv(flags);
16002 +}
16003 +
16004 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16005 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16006 +
16007 +static unsigned long au_flag_conv(unsigned long flags)
16008 +{
16009 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16010 +               | AuConv_VM_MAP(flags, DENYWRITE)
16011 +               | AuConv_VM_MAP(flags, LOCKED);
16012 +}
16013 +#endif
16014 +
16015 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16016 +{
16017 +       int err;
16018 +       const unsigned char wlock
16019 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16020 +       struct super_block *sb;
16021 +       struct file *h_file;
16022 +       struct inode *inode;
16023 +
16024 +       AuDbgVmRegion(file, vma);
16025 +
16026 +       inode = file_inode(file);
16027 +       sb = inode->i_sb;
16028 +       lockdep_off();
16029 +       si_read_lock(sb, AuLock_NOPLMW);
16030 +
16031 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16032 +       lockdep_on();
16033 +       err = PTR_ERR(h_file);
16034 +       if (IS_ERR(h_file))
16035 +               goto out;
16036 +
16037 +       err = 0;
16038 +       au_set_mmapped(file);
16039 +       au_vm_file_reset(vma, h_file);
16040 +       /*
16041 +        * we cannot call security_mmap_file() here since it may acquire
16042 +        * mmap_sem or i_mutex.
16043 +        *
16044 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16045 +        *                       au_flag_conv(vma->vm_flags));
16046 +        */
16047 +       if (!err)
16048 +               err = call_mmap(h_file, vma);
16049 +       if (!err) {
16050 +               au_vm_prfile_set(vma, file);
16051 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16052 +               goto out_fput; /* success */
16053 +       }
16054 +       au_unset_mmapped(file);
16055 +       au_vm_file_reset(vma, file);
16056 +
16057 +out_fput:
16058 +       lockdep_off();
16059 +       ii_write_unlock(inode);
16060 +       lockdep_on();
16061 +       fput(h_file);
16062 +out:
16063 +       lockdep_off();
16064 +       si_read_unlock(sb);
16065 +       lockdep_on();
16066 +       AuTraceErr(err);
16067 +       return err;
16068 +}
16069 +
16070 +/* ---------------------------------------------------------------------- */
16071 +
16072 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16073 +                            int datasync)
16074 +{
16075 +       int err;
16076 +       struct au_write_pre wpre;
16077 +       struct inode *inode;
16078 +       struct file *h_file;
16079 +
16080 +       err = 0; /* -EBADF; */ /* posix? */
16081 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16082 +               goto out;
16083 +
16084 +       inode = file_inode(file);
16085 +       au_mtx_and_read_lock(inode);
16086 +
16087 +       wpre.lsc = 0;
16088 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16089 +       err = PTR_ERR(h_file);
16090 +       if (IS_ERR(h_file))
16091 +               goto out_unlock;
16092 +
16093 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16094 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16095 +
16096 +out_unlock:
16097 +       si_read_unlock(inode->i_sb);
16098 +       inode_unlock(inode);
16099 +out:
16100 +       return err;
16101 +}
16102 +
16103 +static int aufs_fasync(int fd, struct file *file, int flag)
16104 +{
16105 +       int err;
16106 +       struct file *h_file;
16107 +       struct super_block *sb;
16108 +
16109 +       sb = file->f_path.dentry->d_sb;
16110 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16111 +
16112 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16113 +       err = PTR_ERR(h_file);
16114 +       if (IS_ERR(h_file))
16115 +               goto out;
16116 +
16117 +       if (h_file->f_op->fasync)
16118 +               err = h_file->f_op->fasync(fd, h_file, flag);
16119 +       fput(h_file); /* instead of au_read_post() */
16120 +
16121 +out:
16122 +       si_read_unlock(sb);
16123 +       return err;
16124 +}
16125 +
16126 +static int aufs_setfl(struct file *file, unsigned long arg)
16127 +{
16128 +       int err;
16129 +       struct file *h_file;
16130 +       struct super_block *sb;
16131 +
16132 +       sb = file->f_path.dentry->d_sb;
16133 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16134 +
16135 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16136 +       err = PTR_ERR(h_file);
16137 +       if (IS_ERR(h_file))
16138 +               goto out;
16139 +
16140 +       /* stop calling h_file->fasync */
16141 +       arg |= vfsub_file_flags(file) & FASYNC;
16142 +       err = setfl(/*unused fd*/-1, h_file, arg);
16143 +       fput(h_file); /* instead of au_read_post() */
16144 +
16145 +out:
16146 +       si_read_unlock(sb);
16147 +       return err;
16148 +}
16149 +
16150 +/* ---------------------------------------------------------------------- */
16151 +
16152 +/* no one supports this operation, currently */
16153 +#if 0 /* reserved for future use */
16154 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16155 +                            size_t len, loff_t *pos, int more)
16156 +{
16157 +}
16158 +#endif
16159 +
16160 +/* ---------------------------------------------------------------------- */
16161 +
16162 +const struct file_operations aufs_file_fop = {
16163 +       .owner          = THIS_MODULE,
16164 +
16165 +       .llseek         = default_llseek,
16166 +
16167 +       .read_iter      = aufs_read_iter,
16168 +       .write_iter     = aufs_write_iter,
16169 +
16170 +#ifdef CONFIG_AUFS_POLL
16171 +       .poll           = aufs_poll,
16172 +#endif
16173 +       .unlocked_ioctl = aufs_ioctl_nondir,
16174 +#ifdef CONFIG_COMPAT
16175 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16176 +#endif
16177 +       .mmap           = aufs_mmap,
16178 +       .open           = aufs_open_nondir,
16179 +       .flush          = aufs_flush_nondir,
16180 +       .release        = aufs_release_nondir,
16181 +       .fsync          = aufs_fsync_nondir,
16182 +       .fasync         = aufs_fasync,
16183 +       /* .sendpage    = aufs_sendpage, */
16184 +       .setfl          = aufs_setfl,
16185 +       .splice_write   = aufs_splice_write,
16186 +       .splice_read    = aufs_splice_read,
16187 +#if 0 /* reserved for future use */
16188 +       .aio_splice_write = aufs_aio_splice_write,
16189 +       .aio_splice_read  = aufs_aio_splice_read,
16190 +#endif
16191 +       .fallocate      = aufs_fallocate,
16192 +       .copy_file_range = aufs_copy_file_range
16193 +};
16194 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16195 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16196 +++ linux/fs/aufs/fsctx.c       2022-11-05 23:02:18.965889284 +0100
16197 @@ -0,0 +1,1242 @@
16198 +// SPDX-License-Identifier: GPL-2.0
16199 +/*
16200 + * Copyright (C) 2022 Junjiro R. Okajima
16201 + *
16202 + * This program is free software; you can redistribute it and/or modify
16203 + * it under the terms of the GNU General Public License as published by
16204 + * the Free Software Foundation; either version 2 of the License, or
16205 + * (at your option) any later version.
16206 + *
16207 + * This program is distributed in the hope that it will be useful,
16208 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16209 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16210 + * GNU General Public License for more details.
16211 + *
16212 + * You should have received a copy of the GNU General Public License
16213 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16214 + */
16215 +
16216 +/*
16217 + * fs context, aka new mount api
16218 + */
16219 +
16220 +#include <linux/fs_context.h>
16221 +#include "aufs.h"
16222 +
16223 +struct au_fsctx_opts {
16224 +       aufs_bindex_t bindex;
16225 +       unsigned char skipped;
16226 +       struct au_opt *opt, *opt_tail;
16227 +       struct super_block *sb;
16228 +       struct au_sbinfo *sbinfo;
16229 +       struct au_opts opts;
16230 +};
16231 +
16232 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16233 +static int cvt_err(int err)
16234 +{
16235 +       AuTraceErr(err);
16236 +
16237 +       switch (err) {
16238 +       case -ENOENT:
16239 +       case -ENOTDIR:
16240 +       case -EEXIST:
16241 +       case -EIO:
16242 +               err = -EINVAL;
16243 +       }
16244 +       return err;
16245 +}
16246 +
16247 +static int au_fsctx_reconfigure(struct fs_context *fc)
16248 +{
16249 +       int err, do_dx;
16250 +       unsigned int mntflags;
16251 +       struct dentry *root;
16252 +       struct super_block *sb;
16253 +       struct inode *inode;
16254 +       struct au_fsctx_opts *a = fc->fs_private;
16255 +
16256 +       AuDbg("fc %p\n", fc);
16257 +
16258 +       root = fc->root;
16259 +       sb = root->d_sb;
16260 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16261 +       if (!err) {
16262 +               di_write_lock_child(root);
16263 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16264 +               aufs_write_unlock(root);
16265 +       }
16266 +
16267 +       inode = d_inode(root);
16268 +       inode_lock(inode);
16269 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16270 +       if (unlikely(err))
16271 +               goto out;
16272 +       di_write_lock_child(root);
16273 +
16274 +       /* au_opts_remount() may return an error */
16275 +       err = au_opts_remount(sb, &a->opts);
16276 +
16277 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16278 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16279 +                                                    REFRESH_IDOP));
16280 +
16281 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16282 +               mntflags = au_mntflags(sb);
16283 +               do_dx = !!au_opt_test(mntflags, DIO);
16284 +               au_dy_arefresh(do_dx);
16285 +       }
16286 +
16287 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16288 +       aufs_write_unlock(root);
16289 +
16290 +out:
16291 +       inode_unlock(inode);
16292 +       err = cvt_err(err);
16293 +       AuTraceErr(err);
16294 +
16295 +       return err;
16296 +}
16297 +
16298 +/* ---------------------------------------------------------------------- */
16299 +
16300 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16301 +{
16302 +       int err;
16303 +       struct au_fsctx_opts *a = fc->fs_private;
16304 +       struct au_sbinfo *sbinfo = a->sbinfo;
16305 +       struct dentry *root;
16306 +       struct inode *inode;
16307 +
16308 +       sbinfo->si_sb = sb;
16309 +       sb->s_fs_info = sbinfo;
16310 +       kobject_get(&sbinfo->si_kobj);
16311 +
16312 +       __si_write_lock(sb);
16313 +       si_pid_set(sb);
16314 +       au_sbilist_add(sb);
16315 +
16316 +       /* all timestamps always follow the ones on the branch */
16317 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16318 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16319 +       sb->s_op = &aufs_sop;
16320 +       sb->s_d_op = &aufs_dop;
16321 +       sb->s_magic = AUFS_SUPER_MAGIC;
16322 +       sb->s_maxbytes = 0;
16323 +       sb->s_stack_depth = 1;
16324 +       au_export_init(sb);
16325 +       au_xattr_init(sb);
16326 +
16327 +       err = au_alloc_root(sb);
16328 +       if (unlikely(err)) {
16329 +               si_write_unlock(sb);
16330 +               goto out;
16331 +       }
16332 +       root = sb->s_root;
16333 +       inode = d_inode(root);
16334 +       ii_write_lock_parent(inode);
16335 +       aufs_write_unlock(root);
16336 +
16337 +       /* lock vfs_inode first, then aufs. */
16338 +       inode_lock(inode);
16339 +       aufs_write_lock(root);
16340 +       err = au_opts_mount(sb, &a->opts);
16341 +       AuTraceErr(err);
16342 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16343 +               sb->s_d_op = &aufs_dop_noreval;
16344 +               /* infofc(fc, "%ps", sb->s_d_op); */
16345 +               pr_info("%ps\n", sb->s_d_op);
16346 +               au_refresh_dop(root, /*force_reval*/0);
16347 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16348 +               au_refresh_iop(inode, /*force_getattr*/0);
16349 +       }
16350 +       aufs_write_unlock(root);
16351 +       inode_unlock(inode);
16352 +       if (!err)
16353 +               goto out; /* success */
16354 +
16355 +       dput(root);
16356 +       sb->s_root = NULL;
16357 +
16358 +out:
16359 +       if (unlikely(err))
16360 +               kobject_put(&sbinfo->si_kobj);
16361 +       AuTraceErr(err);
16362 +       err = cvt_err(err);
16363 +       AuTraceErr(err);
16364 +       return err;
16365 +}
16366 +
16367 +static int au_fsctx_get_tree(struct fs_context *fc)
16368 +{
16369 +       int err;
16370 +
16371 +       AuDbg("fc %p\n", fc);
16372 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16373 +
16374 +       AuTraceErr(err);
16375 +       return err;
16376 +}
16377 +
16378 +/* ---------------------------------------------------------------------- */
16379 +
16380 +static void au_fsctx_dump(struct au_opts *opts)
16381 +{
16382 +#ifdef CONFIG_AUFS_DEBUG
16383 +       /* reduce stack space */
16384 +       union {
16385 +               struct au_opt_add *add;
16386 +               struct au_opt_del *del;
16387 +               struct au_opt_mod *mod;
16388 +               struct au_opt_xino *xino;
16389 +               struct au_opt_xino_itrunc *xino_itrunc;
16390 +               struct au_opt_wbr_create *create;
16391 +       } u;
16392 +       struct au_opt *opt;
16393 +
16394 +       opt = opts->opt;
16395 +       while (opt->type != Opt_tail) {
16396 +               switch (opt->type) {
16397 +               case Opt_add:
16398 +                       u.add = &opt->add;
16399 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16400 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16401 +                                 u.add->path.dentry);
16402 +                       break;
16403 +               case Opt_del:
16404 +                       fallthrough;
16405 +               case Opt_idel:
16406 +                       u.del = &opt->del;
16407 +                       AuDbg("del {%s, %p}\n",
16408 +                             u.del->pathname, u.del->h_path.dentry);
16409 +                       break;
16410 +               case Opt_mod:
16411 +                       fallthrough;
16412 +               case Opt_imod:
16413 +                       u.mod = &opt->mod;
16414 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16415 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16416 +                       break;
16417 +               case Opt_append:
16418 +                       u.add = &opt->add;
16419 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16420 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16421 +                                 u.add->path.dentry);
16422 +                       break;
16423 +               case Opt_prepend:
16424 +                       u.add = &opt->add;
16425 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16426 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16427 +                                 u.add->path.dentry);
16428 +                       break;
16429 +
16430 +               case Opt_dirwh:
16431 +                       AuDbg("dirwh %d\n", opt->dirwh);
16432 +                       break;
16433 +               case Opt_rdcache:
16434 +                       AuDbg("rdcache %d\n", opt->rdcache);
16435 +                       break;
16436 +               case Opt_rdblk:
16437 +                       AuDbg("rdblk %d\n", opt->rdblk);
16438 +                       break;
16439 +               case Opt_rdhash:
16440 +                       AuDbg("rdhash %u\n", opt->rdhash);
16441 +                       break;
16442 +
16443 +               case Opt_xino:
16444 +                       u.xino = &opt->xino;
16445 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16446 +                       break;
16447 +
16448 +#define au_fsctx_TF(name)                                        \
16449 +                       case Opt_##name:                          \
16450 +                               if (opt->tf)                      \
16451 +                                       AuLabel(name);            \
16452 +                               else                              \
16453 +                                       AuLabel(no##name);        \
16454 +                               break;
16455 +
16456 +               /* simple true/false flag */
16457 +               au_fsctx_TF(trunc_xino);
16458 +               au_fsctx_TF(trunc_xib);
16459 +               au_fsctx_TF(dirperm1);
16460 +               au_fsctx_TF(plink);
16461 +               au_fsctx_TF(shwh);
16462 +               au_fsctx_TF(dio);
16463 +               au_fsctx_TF(warn_perm);
16464 +               au_fsctx_TF(verbose);
16465 +               au_fsctx_TF(sum);
16466 +               au_fsctx_TF(dirren);
16467 +               au_fsctx_TF(acl);
16468 +#undef au_fsctx_TF
16469 +
16470 +               case Opt_trunc_xino_path:
16471 +                       fallthrough;
16472 +               case Opt_itrunc_xino:
16473 +                       u.xino_itrunc = &opt->xino_itrunc;
16474 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16475 +                       break;
16476 +               case Opt_noxino:
16477 +                       AuLabel(noxino);
16478 +                       break;
16479 +
16480 +               case Opt_list_plink:
16481 +                       AuLabel(list_plink);
16482 +                       break;
16483 +               case Opt_udba:
16484 +                       AuDbg("udba %d, %s\n",
16485 +                                 opt->udba, au_optstr_udba(opt->udba));
16486 +                       break;
16487 +               case Opt_diropq_a:
16488 +                       AuLabel(diropq_a);
16489 +                       break;
16490 +               case Opt_diropq_w:
16491 +                       AuLabel(diropq_w);
16492 +                       break;
16493 +               case Opt_wsum:
16494 +                       AuLabel(wsum);
16495 +                       break;
16496 +               case Opt_wbr_create:
16497 +                       u.create = &opt->wbr_create;
16498 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16499 +                                 au_optstr_wbr_create(u.create->wbr_create));
16500 +                       switch (u.create->wbr_create) {
16501 +                       case AuWbrCreate_MFSV:
16502 +                               fallthrough;
16503 +                       case AuWbrCreate_PMFSV:
16504 +                               AuDbg("%d sec\n", u.create->mfs_second);
16505 +                               break;
16506 +                       case AuWbrCreate_MFSRR:
16507 +                               fallthrough;
16508 +                       case AuWbrCreate_TDMFS:
16509 +                               AuDbg("%llu watermark\n",
16510 +                                         u.create->mfsrr_watermark);
16511 +                               break;
16512 +                       case AuWbrCreate_MFSRRV:
16513 +                               fallthrough;
16514 +                       case AuWbrCreate_TDMFSV:
16515 +                               fallthrough;
16516 +                       case AuWbrCreate_PMFSRRV:
16517 +                               AuDbg("%llu watermark, %d sec\n",
16518 +                                         u.create->mfsrr_watermark,
16519 +                                         u.create->mfs_second);
16520 +                               break;
16521 +                       }
16522 +                       break;
16523 +               case Opt_wbr_copyup:
16524 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16525 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16526 +                       break;
16527 +               case Opt_fhsm_sec:
16528 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16529 +                       break;
16530 +
16531 +               default:
16532 +                       AuDbg("type %d\n", opt->type);
16533 +                       BUG();
16534 +               }
16535 +               opt++;
16536 +       }
16537 +#endif
16538 +}
16539 +
16540 +/* ---------------------------------------------------------------------- */
16541 +
16542 +/*
16543 + * For conditionally compiled mount options.
16544 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16545 + */
16546 +#define au_ignore_flag(name, action)           \
16547 +       fsparam_flag(name, action),             \
16548 +       fsparam_flag("no" name, Opt_ignore_silent)
16549 +
16550 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16551 +       fsparam_string("br", Opt_br),
16552 +
16553 +       /* "add=%d:%s" or "ins=%d:%s" */
16554 +       fsparam_string("add", Opt_add),
16555 +       fsparam_string("ins", Opt_add),
16556 +       fsparam_path("append", Opt_append),
16557 +       fsparam_path("prepend", Opt_prepend),
16558 +
16559 +       fsparam_path("del", Opt_del),
16560 +       /* fsparam_s32("idel", Opt_idel), */
16561 +       fsparam_path("mod", Opt_mod),
16562 +       /* fsparam_string("imod", Opt_imod), */
16563 +
16564 +       fsparam_s32("dirwh", Opt_dirwh),
16565 +
16566 +       fsparam_path("xino", Opt_xino),
16567 +       fsparam_flag("noxino", Opt_noxino),
16568 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16569 +       /* "trunc_xino_v=%d:%d" */
16570 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16571 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16572 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16573 +       /* fsparam_path("zxino", Opt_zxino), */
16574 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16575 +
16576 +#ifdef CONFIG_PROC_FS
16577 +       fsparam_flag_no("plink", Opt_plink),
16578 +#else
16579 +       au_ignore_flag("plink", Opt_ignore),
16580 +#endif
16581 +
16582 +#ifdef CONFIG_AUFS_DEBUG
16583 +       fsparam_flag("list_plink", Opt_list_plink),
16584 +#endif
16585 +
16586 +       fsparam_string("udba", Opt_udba),
16587 +
16588 +       fsparam_flag_no("dio", Opt_dio),
16589 +
16590 +#ifdef CONFIG_AUFS_DIRREN
16591 +       fsparam_flag_no("dirren", Opt_dirren),
16592 +#else
16593 +       au_ignore_flag("dirren", Opt_ignore),
16594 +#endif
16595 +
16596 +#ifdef CONFIG_AUFS_FHSM
16597 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16598 +#else
16599 +       fsparam_s32("fhsm_sec", Opt_ignore),
16600 +#endif
16601 +
16602 +       /* always | a | whiteouted | w */
16603 +       fsparam_string("diropq", Opt_diropq),
16604 +
16605 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16606 +
16607 +#ifdef CONFIG_AUFS_SHWH
16608 +       fsparam_flag_no("shwh", Opt_shwh),
16609 +#else
16610 +       au_ignore_flag("shwh", Opt_err),
16611 +#endif
16612 +
16613 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16614 +
16615 +       fsparam_flag_no("verbose", Opt_verbose),
16616 +       fsparam_flag("v", Opt_verbose),
16617 +       fsparam_flag("quiet", Opt_noverbose),
16618 +       fsparam_flag("q", Opt_noverbose),
16619 +       /* user-space may handle this */
16620 +       fsparam_flag("silent", Opt_noverbose),
16621 +
16622 +       fsparam_flag_no("sum", Opt_sum),
16623 +       fsparam_flag("wsum", Opt_wsum),
16624 +
16625 +       fsparam_s32("rdcache", Opt_rdcache),
16626 +       /* "def" or s32 */
16627 +       fsparam_string("rdblk", Opt_rdblk),
16628 +       /* "def" or s32 */
16629 +       fsparam_string("rdhash", Opt_rdhash),
16630 +
16631 +       fsparam_string("create", Opt_wbr_create),
16632 +       fsparam_string("create_policy", Opt_wbr_create),
16633 +       fsparam_string("cpup", Opt_wbr_copyup),
16634 +       fsparam_string("copyup", Opt_wbr_copyup),
16635 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16636 +
16637 +       /* generic VFS flag */
16638 +#ifdef CONFIG_FS_POSIX_ACL
16639 +       fsparam_flag_no("acl", Opt_acl),
16640 +#else
16641 +       au_ignore_flag("acl", Opt_ignore),
16642 +#endif
16643 +
16644 +       /* internal use for the scripts */
16645 +       fsparam_string("si", Opt_ignore_silent),
16646 +
16647 +       /* obsoleted, keep them temporary */
16648 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16649 +       fsparam_flag("clean_plink", Opt_ignore),
16650 +       fsparam_string("dirs", Opt_br),
16651 +       fsparam_u32("debug", Opt_ignore),
16652 +       /* "whiteout" or "all" */
16653 +       fsparam_string("delete", Opt_ignore),
16654 +       fsparam_string("imap", Opt_ignore),
16655 +
16656 +       /* temporary workaround, due to old mount(8)? */
16657 +       fsparam_flag("relatime", Opt_ignore_silent),
16658 +
16659 +       {}
16660 +};
16661 +
16662 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16663 +                                char *brspec, size_t speclen,
16664 +                                aufs_bindex_t bindex)
16665 +{
16666 +       int err;
16667 +       char *p;
16668 +
16669 +       AuDbg("brspec %s\n", brspec);
16670 +
16671 +       err = -ENOMEM;
16672 +       if (!speclen)
16673 +               speclen = strlen(brspec);
16674 +       /* will be freed by au_fsctx_free() */
16675 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16676 +       if (unlikely(!p)) {
16677 +               errorfc(fc, "failed in %s", brspec);
16678 +               goto out;
16679 +       }
16680 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16681 +
16682 +out:
16683 +       AuTraceErr(err);
16684 +       return err;
16685 +}
16686 +
16687 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16688 +{
16689 +       int err;
16690 +       char *p;
16691 +       struct au_fsctx_opts *a = fc->fs_private;
16692 +       struct au_opt *opt = a->opt;
16693 +       aufs_bindex_t bindex = a->bindex;
16694 +
16695 +       AuDbg("brspec %s\n", brspec);
16696 +
16697 +       err = -EINVAL;
16698 +       while ((p = strsep(&brspec, ":")) && *p) {
16699 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16700 +               AuTraceErr(err);
16701 +               if (unlikely(err))
16702 +                       break;
16703 +               bindex++;
16704 +               opt++;
16705 +               if (unlikely(opt > a->opt_tail)) {
16706 +                       err = -E2BIG;
16707 +                       bindex--;
16708 +                       opt--;
16709 +                       break;
16710 +               }
16711 +               opt->type = Opt_tail;
16712 +               a->skipped = 1;
16713 +       }
16714 +       a->bindex = bindex;
16715 +       a->opt = opt;
16716 +
16717 +       AuTraceErr(err);
16718 +       return err;
16719 +}
16720 +
16721 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16722 +{
16723 +       int err, n;
16724 +       char *p;
16725 +       struct au_fsctx_opts *a = fc->fs_private;
16726 +       struct au_opt *opt = a->opt;
16727 +
16728 +       err = -EINVAL;
16729 +       p = strchr(addspec, ':');
16730 +       if (unlikely(!p)) {
16731 +               errorfc(fc, "bad arg in %s", addspec);
16732 +               goto out;
16733 +       }
16734 +       *p++ = '\0';
16735 +       err = kstrtoint(addspec, 0, &n);
16736 +       if (unlikely(err)) {
16737 +               errorfc(fc, "bad integer in %s", addspec);
16738 +               goto out;
16739 +       }
16740 +       AuDbg("n %d\n", n);
16741 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16742 +
16743 +out:
16744 +       AuTraceErr(err);
16745 +       return err;
16746 +}
16747 +
16748 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16749 +                             struct fs_parameter *param)
16750 +{
16751 +       int err;
16752 +
16753 +       err = -ENOMEM;
16754 +       /* will be freed by au_fsctx_free() */
16755 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16756 +       if (unlikely(!del->pathname))
16757 +               goto out;
16758 +       AuDbg("del %s\n", del->pathname);
16759 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16760 +       if (unlikely(err))
16761 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16762 +
16763 +out:
16764 +       AuTraceErr(err);
16765 +       return err;
16766 +}
16767 +
16768 +#if 0 /* reserved for future use */
16769 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16770 +                              aufs_bindex_t bindex)
16771 +{
16772 +       int err;
16773 +       struct super_block *sb;
16774 +       struct dentry *root;
16775 +       struct au_fsctx_opts *a = fc->fs_private;
16776 +
16777 +       sb = a->sb;
16778 +       AuDebugOn(!sb);
16779 +
16780 +       err = -EINVAL;
16781 +       root = sb->s_root;
16782 +       aufs_read_lock(root, AuLock_FLUSH);
16783 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16784 +               errorfc(fc, "out of bounds, %d", bindex);
16785 +               goto out;
16786 +       }
16787 +
16788 +       err = 0;
16789 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16790 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16791 +
16792 +out:
16793 +       aufs_read_unlock(root, !AuLock_IR);
16794 +       AuTraceErr(err);
16795 +       return err;
16796 +}
16797 +#endif
16798 +
16799 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16800 +                             struct fs_parameter *param)
16801 +{
16802 +       int err;
16803 +       struct path path;
16804 +       char *p;
16805 +
16806 +       err = -ENOMEM;
16807 +       /* will be freed by au_fsctx_free() */
16808 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16809 +       if (unlikely(!mod->path))
16810 +               goto out;
16811 +
16812 +       err = -EINVAL;
16813 +       p = strchr(mod->path, '=');
16814 +       if (unlikely(!p)) {
16815 +               errorfc(fc, "no permission %s", mod->path);
16816 +               goto out;
16817 +       }
16818 +
16819 +       *p++ = 0;
16820 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16821 +       if (unlikely(err)) {
16822 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16823 +               goto out;
16824 +       }
16825 +
16826 +       mod->perm = au_br_perm_val(p);
16827 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16828 +       mod->h_root = dget(path.dentry);
16829 +       path_put(&path);
16830 +
16831 +out:
16832 +       AuTraceErr(err);
16833 +       return err;
16834 +}
16835 +
16836 +#if 0 /* reserved for future use */
16837 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16838 +                              char *ibrspec)
16839 +{
16840 +       int err, n;
16841 +       char *p;
16842 +       struct super_block *sb;
16843 +       struct dentry *root;
16844 +       struct au_fsctx_opts *a = fc->fs_private;
16845 +
16846 +       sb = a->sb;
16847 +       AuDebugOn(!sb);
16848 +
16849 +       err = -EINVAL;
16850 +       p = strchr(ibrspec, ':');
16851 +       if (unlikely(!p)) {
16852 +               errorfc(fc, "no index, %s", ibrspec);
16853 +               goto out;
16854 +       }
16855 +       *p++ = '\0';
16856 +       err = kstrtoint(ibrspec, 0, &n);
16857 +       if (unlikely(err)) {
16858 +               errorfc(fc, "bad integer in %s", ibrspec);
16859 +               goto out;
16860 +       }
16861 +       AuDbg("n %d\n", n);
16862 +
16863 +       root = sb->s_root;
16864 +       aufs_read_lock(root, AuLock_FLUSH);
16865 +       if (n < 0 || au_sbbot(sb) < n) {
16866 +               errorfc(fc, "out of bounds, %d", bindex);
16867 +               goto out_root;
16868 +       }
16869 +
16870 +       err = 0;
16871 +       mod->perm = au_br_perm_val(p);
16872 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16873 +             mod->path, mod->perm, p);
16874 +       mod->h_root = dget(au_h_dptr(root, bindex));
16875 +
16876 +out_root:
16877 +       aufs_read_unlock(root, !AuLock_IR);
16878 +out:
16879 +       AuTraceErr(err);
16880 +       return err;
16881 +}
16882 +#endif
16883 +
16884 +static int au_fsctx_parse_xino(struct fs_context *fc,
16885 +                              struct au_opt_xino *xino,
16886 +                              struct fs_parameter *param)
16887 +{
16888 +       int err;
16889 +       struct au_fsctx_opts *a = fc->fs_private;
16890 +
16891 +       err = -ENOMEM;
16892 +       /* will be freed by au_opts_free() */
16893 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16894 +       if (unlikely(!xino->path))
16895 +               goto out;
16896 +       AuDbg("path %s\n", xino->path);
16897 +
16898 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16899 +                                   /*wbrtop*/0);
16900 +       err = PTR_ERR(xino->file);
16901 +       if (IS_ERR(xino->file)) {
16902 +               xino->file = NULL;
16903 +               goto out;
16904 +       }
16905 +
16906 +       err = 0;
16907 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16908 +               err = -EINVAL;
16909 +               errorfc(fc, "%s must be outside", xino->path);
16910 +       }
16911 +
16912 +out:
16913 +       AuTraceErr(err);
16914 +       return err;
16915 +}
16916 +
16917 +static
16918 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16919 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16920 +                                   char *pathname)
16921 +{
16922 +       int err;
16923 +       aufs_bindex_t bbot, bindex;
16924 +       struct path path;
16925 +       struct dentry *root;
16926 +       struct au_fsctx_opts *a = fc->fs_private;
16927 +
16928 +       AuDebugOn(!a->sb);
16929 +
16930 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16931 +       if (unlikely(err)) {
16932 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16933 +               goto out;
16934 +       }
16935 +
16936 +       xino_itrunc->bindex = -1;
16937 +       root = a->sb->s_root;
16938 +       aufs_read_lock(root, AuLock_FLUSH);
16939 +       bbot = au_sbbot(a->sb);
16940 +       for (bindex = 0; bindex <= bbot; bindex++) {
16941 +               if (au_h_dptr(root, bindex) == path.dentry) {
16942 +                       xino_itrunc->bindex = bindex;
16943 +                       break;
16944 +               }
16945 +       }
16946 +       aufs_read_unlock(root, !AuLock_IR);
16947 +       path_put(&path);
16948 +
16949 +       if (unlikely(xino_itrunc->bindex < 0)) {
16950 +               err = -EINVAL;
16951 +               errorfc(fc, "no such branch %s", pathname);
16952 +       }
16953 +
16954 +out:
16955 +       AuTraceErr(err);
16956 +       return err;
16957 +}
16958 +
16959 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16960 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16961 +                                     unsigned int bindex)
16962 +{
16963 +       int err;
16964 +       aufs_bindex_t bbot;
16965 +       struct super_block *sb;
16966 +       struct au_fsctx_opts *a = fc->fs_private;
16967 +
16968 +       sb = a->sb;
16969 +       AuDebugOn(!sb);
16970 +
16971 +       err = 0;
16972 +       si_noflush_read_lock(sb);
16973 +       bbot = au_sbbot(sb);
16974 +       si_read_unlock(sb);
16975 +       if (bindex <= bbot)
16976 +               xino_itrunc->bindex = bindex;
16977 +       else {
16978 +               err = -EINVAL;
16979 +               errorfc(fc, "out of bounds, %u", bindex);
16980 +       }
16981 +
16982 +       AuTraceErr(err);
16983 +       return err;
16984 +}
16985 +
16986 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16987 +{
16988 +       int err, token;
16989 +       struct fs_parse_result result;
16990 +       struct au_fsctx_opts *a = fc->fs_private;
16991 +       struct au_opt *opt = a->opt;
16992 +
16993 +       AuDbg("fc %p, param {key %s, string %s}\n",
16994 +             fc, param->key, param->string);
16995 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16996 +       if (unlikely(err < 0))
16997 +               goto out;
16998 +       token = err;
16999 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
17000 +             token, result.negated, result.uint_64);
17001 +
17002 +       err = -EINVAL;
17003 +       a->skipped = 0;
17004 +       switch (token) {
17005 +       case Opt_br:
17006 +               err = au_fsctx_parse_br(fc, param->string);
17007 +               break;
17008 +       case Opt_add:
17009 +               err = au_fsctx_parse_add(fc, param->string);
17010 +               break;
17011 +       case Opt_append:
17012 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17013 +                                           /*dummy bindex*/1);
17014 +               break;
17015 +       case Opt_prepend:
17016 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17017 +                                           /*bindex*/0);
17018 +               break;
17019 +
17020 +       case Opt_del:
17021 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17022 +               break;
17023 +#if 0 /* reserved for future use */
17024 +       case Opt_idel:
17025 +               if (!a->sb) {
17026 +                       err = 0;
17027 +                       a->skipped = 1;
17028 +                       break;
17029 +               }
17030 +               del->pathname = "(indexed)";
17031 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17032 +               break;
17033 +#endif
17034 +
17035 +       case Opt_mod:
17036 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17037 +               break;
17038 +#ifdef IMOD /* reserved for future use */
17039 +       case Opt_imod:
17040 +               if (!a->sb) {
17041 +                       err = 0;
17042 +                       a->skipped = 1;
17043 +                       break;
17044 +               }
17045 +               u.mod->path = "(indexed)";
17046 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17047 +               break;
17048 +#endif
17049 +
17050 +       case Opt_xino:
17051 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17052 +               break;
17053 +       case Opt_trunc_xino_path:
17054 +               if (!a->sb) {
17055 +                       errorfc(fc, "no such branch %s", param->string);
17056 +                       break;
17057 +               }
17058 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17059 +                                                     param->string);
17060 +               break;
17061 +#if 0
17062 +       case Opt_trunc_xino_v:
17063 +               if (!a->sb) {
17064 +                       err = 0;
17065 +                       a->skipped = 1;
17066 +                       break;
17067 +               }
17068 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17069 +                                                     param->string);
17070 +               break;
17071 +#endif
17072 +       case Opt_itrunc_xino:
17073 +               if (!a->sb) {
17074 +                       errorfc(fc, "out of bounds %s", param->string);
17075 +                       break;
17076 +               }
17077 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17078 +                                                result.int_32);
17079 +               break;
17080 +
17081 +       case Opt_dirwh:
17082 +               err = 0;
17083 +               opt->dirwh = result.int_32;
17084 +               break;
17085 +
17086 +       case Opt_rdcache:
17087 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17088 +                       errorfc(fc, "rdcache must be smaller than %d",
17089 +                               AUFS_RDCACHE_MAX);
17090 +                       break;
17091 +               }
17092 +               err = 0;
17093 +               opt->rdcache = result.int_32;
17094 +               break;
17095 +
17096 +       case Opt_rdblk:
17097 +               err = 0;
17098 +               opt->rdblk = AUFS_RDBLK_DEF;
17099 +               if (!strcmp(param->string, "def"))
17100 +                       break;
17101 +
17102 +               err = kstrtoint(param->string, 0, &result.int_32);
17103 +               if (unlikely(err)) {
17104 +                       errorfc(fc, "bad value in %s", param->key);
17105 +                       break;
17106 +               }
17107 +               err = -EINVAL;
17108 +               if (unlikely(result.int_32 < 0
17109 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17110 +                       errorfc(fc, "bad value in %s", param->key);
17111 +                       break;
17112 +               }
17113 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17114 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17115 +                       break;
17116 +               }
17117 +               err = 0;
17118 +               opt->rdblk = result.int_32;
17119 +               break;
17120 +
17121 +       case Opt_rdhash:
17122 +               err = 0;
17123 +               opt->rdhash = AUFS_RDHASH_DEF;
17124 +               if (!strcmp(param->string, "def"))
17125 +                       break;
17126 +
17127 +               err = kstrtoint(param->string, 0, &result.int_32);
17128 +               if (unlikely(err)) {
17129 +                       errorfc(fc, "bad value in %s", param->key);
17130 +                       break;
17131 +               }
17132 +               /* how about zero? */
17133 +               if (result.int_32 < 0
17134 +                   || result.int_32 * sizeof(struct hlist_head)
17135 +                   > KMALLOC_MAX_SIZE) {
17136 +                       err = -EINVAL;
17137 +                       errorfc(fc, "bad integer in %s", param->key);
17138 +                       break;
17139 +               }
17140 +               opt->rdhash = result.int_32;
17141 +               break;
17142 +
17143 +       case Opt_diropq:
17144 +               /*
17145 +                * As other options, fs/aufs/opts.c can handle these strings by
17146 +                * match_token().  But "diropq=" is deprecated now and will
17147 +                * never have other value.  So simple strcmp() is enough here.
17148 +                */
17149 +               if (!strcmp(param->string, "a") ||
17150 +                   !strcmp(param->string, "always")) {
17151 +                       err = 0;
17152 +                       opt->type = Opt_diropq_a;
17153 +               } else if (!strcmp(param->string, "w") ||
17154 +                          !strcmp(param->string, "whiteouted")) {
17155 +                       err = 0;
17156 +                       opt->type = Opt_diropq_w;
17157 +               } else
17158 +                       errorfc(fc, "unknown value %s", param->string);
17159 +               break;
17160 +
17161 +       case Opt_udba:
17162 +               opt->udba = au_udba_val(param->string);
17163 +               if (opt->udba >= 0)
17164 +                       err = 0;
17165 +               else
17166 +                       errorf(fc, "wrong value, %s", param->string);
17167 +               break;
17168 +
17169 +       case Opt_wbr_create:
17170 +               opt->wbr_create.wbr_create
17171 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17172 +               if (opt->wbr_create.wbr_create >= 0)
17173 +                       err = 0;
17174 +               else
17175 +                       errorf(fc, "wrong value, %s", param->key);
17176 +               break;
17177 +
17178 +       case Opt_wbr_copyup:
17179 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17180 +               if (opt->wbr_copyup >= 0)
17181 +                       err = 0;
17182 +               else
17183 +                       errorfc(fc, "wrong value, %s", param->key);
17184 +               break;
17185 +
17186 +       case Opt_fhsm_sec:
17187 +               if (unlikely(result.int_32 < 0)) {
17188 +                       errorfc(fc, "bad integer in %s\n", param->key);
17189 +                       break;
17190 +               }
17191 +               err = 0;
17192 +               if (sysaufs_brs)
17193 +                       opt->fhsm_second = result.int_32;
17194 +               else
17195 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17196 +               break;
17197 +
17198 +       /* simple true/false flag */
17199 +#define au_fsctx_TF(name)                              \
17200 +               case Opt_##name:                        \
17201 +                       err = 0;                        \
17202 +                       opt->tf = !result.negated;      \
17203 +                       break
17204 +       au_fsctx_TF(trunc_xino);
17205 +       au_fsctx_TF(trunc_xib);
17206 +       au_fsctx_TF(dirperm1);
17207 +       au_fsctx_TF(plink);
17208 +       au_fsctx_TF(shwh);
17209 +       au_fsctx_TF(dio);
17210 +       au_fsctx_TF(warn_perm);
17211 +       au_fsctx_TF(verbose);
17212 +       au_fsctx_TF(sum);
17213 +       au_fsctx_TF(dirren);
17214 +       au_fsctx_TF(acl);
17215 +#undef au_fsctx_TF
17216 +
17217 +       case Opt_noverbose:
17218 +               err = 0;
17219 +               opt->type = Opt_verbose;
17220 +               opt->tf = false;
17221 +               break;
17222 +
17223 +       case Opt_noxino:
17224 +               fallthrough;
17225 +       case Opt_list_plink:
17226 +               fallthrough;
17227 +       case Opt_wsum:
17228 +               err = 0;
17229 +               break;
17230 +
17231 +       case Opt_ignore:
17232 +               warnfc(fc, "ignored %s", param->key);
17233 +               fallthrough;
17234 +       case Opt_ignore_silent:
17235 +               a->skipped = 1;
17236 +               err = 0;
17237 +               break;
17238 +       default:
17239 +               a->skipped = 1;
17240 +               err = -ENOPARAM;
17241 +               break;
17242 +       }
17243 +       if (unlikely(err))
17244 +               goto out;
17245 +       if (a->skipped)
17246 +               goto out;
17247 +
17248 +       switch (token) {
17249 +       case Opt_br:
17250 +               fallthrough;
17251 +       case Opt_noverbose:
17252 +               fallthrough;
17253 +       case Opt_diropq:
17254 +               break;
17255 +       default:
17256 +               opt->type = token;
17257 +               break;
17258 +       }
17259 +       opt++;
17260 +       if (unlikely(opt > a->opt_tail)) {
17261 +               err = -E2BIG;
17262 +               opt--;
17263 +       }
17264 +       opt->type = Opt_tail;
17265 +       a->opt = opt;
17266 +
17267 +out:
17268 +       return err;
17269 +}
17270 +
17271 +/*
17272 + * these options accept both 'name=val' and 'name:val' form.
17273 + * some accept optional '=' in its value.
17274 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17275 + */
17276 +static inline unsigned int is_colonopt(char *str)
17277 +{
17278 +#define do_test(name)                                  \
17279 +       if (!strncmp(str, name ":", sizeof(name)))      \
17280 +               return sizeof(name) - 1
17281 +       do_test("br");
17282 +       do_test("add");
17283 +       do_test("ins");
17284 +       do_test("append");
17285 +       do_test("prepend");
17286 +       do_test("del");
17287 +       /* do_test("idel"); */
17288 +       do_test("mod");
17289 +       /* do_test("imod"); */
17290 +#undef do_test
17291 +
17292 +       return 0;
17293 +}
17294 +
17295 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17296 +{
17297 +       int err;
17298 +       unsigned int u;
17299 +       char *str;
17300 +       struct au_fsctx_opts *a = fc->fs_private;
17301 +
17302 +       str = data;
17303 +       AuDbg("str %s\n", str);
17304 +       while (str) {
17305 +               u = is_colonopt(str);
17306 +               if (u)
17307 +                       str[u] = '=';
17308 +               str = strchr(str, ',');
17309 +               if (!str)
17310 +                       break;
17311 +               str++;
17312 +       }
17313 +       str = data;
17314 +       AuDbg("str %s\n", str);
17315 +
17316 +       err = generic_parse_monolithic(fc, str);
17317 +       AuTraceErr(err);
17318 +       au_fsctx_dump(&a->opts);
17319 +
17320 +       return err;
17321 +}
17322 +
17323 +/* ---------------------------------------------------------------------- */
17324 +
17325 +static void au_fsctx_opts_free(struct au_opts *opts)
17326 +{
17327 +       struct au_opt *opt;
17328 +
17329 +       opt = opts->opt;
17330 +       while (opt->type != Opt_tail) {
17331 +               switch (opt->type) {
17332 +               case Opt_add:
17333 +                       fallthrough;
17334 +               case Opt_append:
17335 +                       fallthrough;
17336 +               case Opt_prepend:
17337 +                       kfree(opt->add.pathname);
17338 +                       path_put(&opt->add.path);
17339 +                       break;
17340 +               case Opt_del:
17341 +                       kfree(opt->del.pathname);
17342 +                       fallthrough;
17343 +               case Opt_idel:
17344 +                       path_put(&opt->del.h_path);
17345 +                       break;
17346 +               case Opt_mod:
17347 +                       kfree(opt->mod.path);
17348 +                       fallthrough;
17349 +               case Opt_imod:
17350 +                       dput(opt->mod.h_root);
17351 +                       break;
17352 +               case Opt_xino:
17353 +                       kfree(opt->xino.path);
17354 +                       fput(opt->xino.file);
17355 +                       break;
17356 +               }
17357 +               opt++;
17358 +       }
17359 +}
17360 +
17361 +static void au_fsctx_free(struct fs_context *fc)
17362 +{
17363 +       struct au_fsctx_opts *a = fc->fs_private;
17364 +
17365 +       /* fs_type=%p, root=%pD */
17366 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17367 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17368 +
17369 +       kobject_put(&a->sbinfo->si_kobj);
17370 +       au_fsctx_opts_free(&a->opts);
17371 +       free_page((unsigned long)a->opts.opt);
17372 +       au_kfree_rcu(a);
17373 +}
17374 +
17375 +static const struct fs_context_operations au_fsctx_ops = {
17376 +       .free                   = au_fsctx_free,
17377 +       .parse_param            = au_fsctx_parse_param,
17378 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17379 +       .get_tree               = au_fsctx_get_tree,
17380 +       .reconfigure            = au_fsctx_reconfigure
17381 +       /*
17382 +        * nfs4 requires ->dup()? No.
17383 +        * I don't know what is this ->dup() for.
17384 +        */
17385 +};
17386 +
17387 +int aufs_fsctx_init(struct fs_context *fc)
17388 +{
17389 +       int err;
17390 +       struct au_fsctx_opts *a;
17391 +
17392 +       /* fs_type=%p, root=%pD */
17393 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17394 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17395 +
17396 +       /* they will be freed by au_fsctx_free() */
17397 +       err = -ENOMEM;
17398 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17399 +       if (unlikely(!a))
17400 +               goto out;
17401 +       a->bindex = 0;
17402 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17403 +       if (unlikely(!a->opts.opt))
17404 +               goto out_a;
17405 +       a->opt = a->opts.opt;
17406 +       a->opt->type = Opt_tail;
17407 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17408 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17409 +       a->opts.sb_flags = fc->sb_flags;
17410 +
17411 +       a->sb = NULL;
17412 +       if (fc->root) {
17413 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17414 +               a->opts.flags = AuOpts_REMOUNT;
17415 +               a->sb = fc->root->d_sb;
17416 +               a->sbinfo = au_sbi(a->sb);
17417 +               kobject_get(&a->sbinfo->si_kobj);
17418 +       } else {
17419 +               a->sbinfo = au_si_alloc(a->sb);
17420 +               AuDebugOn(!a->sbinfo);
17421 +               err = PTR_ERR(a->sbinfo);
17422 +               if (IS_ERR(a->sbinfo))
17423 +                       goto out_opt;
17424 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17425 +       }
17426 +
17427 +       err = 0;
17428 +       fc->fs_private = a;
17429 +       fc->ops = &au_fsctx_ops;
17430 +       goto out; /* success */
17431 +
17432 +out_opt:
17433 +       free_page((unsigned long)a->opts.opt);
17434 +out_a:
17435 +       au_kfree_rcu(a);
17436 +out:
17437 +       AuTraceErr(err);
17438 +       return err;
17439 +}
17440 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17441 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17442 +++ linux/fs/aufs/fstype.h      2022-11-05 23:02:18.965889284 +0100
17443 @@ -0,0 +1,401 @@
17444 +/* SPDX-License-Identifier: GPL-2.0 */
17445 +/*
17446 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17447 + *
17448 + * This program is free software; you can redistribute it and/or modify
17449 + * it under the terms of the GNU General Public License as published by
17450 + * the Free Software Foundation; either version 2 of the License, or
17451 + * (at your option) any later version.
17452 + *
17453 + * This program is distributed in the hope that it will be useful,
17454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17456 + * GNU General Public License for more details.
17457 + *
17458 + * You should have received a copy of the GNU General Public License
17459 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17460 + */
17461 +
17462 +/*
17463 + * judging filesystem type
17464 + */
17465 +
17466 +#ifndef __AUFS_FSTYPE_H__
17467 +#define __AUFS_FSTYPE_H__
17468 +
17469 +#ifdef __KERNEL__
17470 +
17471 +#include <linux/fs.h>
17472 +#include <linux/magic.h>
17473 +#include <linux/nfs_fs.h>
17474 +#include <linux/romfs_fs.h>
17475 +
17476 +static inline int au_test_aufs(struct super_block *sb)
17477 +{
17478 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17479 +}
17480 +
17481 +static inline const char *au_sbtype(struct super_block *sb)
17482 +{
17483 +       return sb->s_type->name;
17484 +}
17485 +
17486 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17487 +{
17488 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17489 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17490 +#else
17491 +       return 0;
17492 +#endif
17493 +}
17494 +
17495 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17496 +{
17497 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17498 +       return sb->s_magic == ROMFS_MAGIC;
17499 +#else
17500 +       return 0;
17501 +#endif
17502 +}
17503 +
17504 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17505 +{
17506 +#if IS_ENABLED(CONFIG_CRAMFS)
17507 +       return sb->s_magic == CRAMFS_MAGIC;
17508 +#endif
17509 +       return 0;
17510 +}
17511 +
17512 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17513 +{
17514 +#if IS_ENABLED(CONFIG_NFS_FS)
17515 +       return sb->s_magic == NFS_SUPER_MAGIC;
17516 +#else
17517 +       return 0;
17518 +#endif
17519 +}
17520 +
17521 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17522 +{
17523 +#if IS_ENABLED(CONFIG_FUSE_FS)
17524 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17525 +#else
17526 +       return 0;
17527 +#endif
17528 +}
17529 +
17530 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17531 +{
17532 +#if IS_ENABLED(CONFIG_XFS_FS)
17533 +       return sb->s_magic == XFS_SB_MAGIC;
17534 +#else
17535 +       return 0;
17536 +#endif
17537 +}
17538 +
17539 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17540 +{
17541 +#ifdef CONFIG_TMPFS
17542 +       return sb->s_magic == TMPFS_MAGIC;
17543 +#else
17544 +       return 0;
17545 +#endif
17546 +}
17547 +
17548 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17549 +{
17550 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17551 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17552 +#else
17553 +       return 0;
17554 +#endif
17555 +}
17556 +
17557 +static inline int au_test_ramfs(struct super_block *sb)
17558 +{
17559 +       return sb->s_magic == RAMFS_MAGIC;
17560 +}
17561 +
17562 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17563 +{
17564 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17565 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17566 +#else
17567 +       return 0;
17568 +#endif
17569 +}
17570 +
17571 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17572 +{
17573 +#ifdef CONFIG_PROC_FS
17574 +       return sb->s_magic == PROC_SUPER_MAGIC;
17575 +#else
17576 +       return 0;
17577 +#endif
17578 +}
17579 +
17580 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17581 +{
17582 +#ifdef CONFIG_SYSFS
17583 +       return sb->s_magic == SYSFS_MAGIC;
17584 +#else
17585 +       return 0;
17586 +#endif
17587 +}
17588 +
17589 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17590 +{
17591 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17592 +       return sb->s_magic == CONFIGFS_MAGIC;
17593 +#else
17594 +       return 0;
17595 +#endif
17596 +}
17597 +
17598 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17599 +{
17600 +#if IS_ENABLED(CONFIG_MINIX_FS)
17601 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17602 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17603 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17604 +               || sb->s_magic == MINIX_SUPER_MAGIC
17605 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17606 +#else
17607 +       return 0;
17608 +#endif
17609 +}
17610 +
17611 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17612 +{
17613 +#if IS_ENABLED(CONFIG_FAT_FS)
17614 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17615 +#else
17616 +       return 0;
17617 +#endif
17618 +}
17619 +
17620 +static inline int au_test_msdos(struct super_block *sb)
17621 +{
17622 +       return au_test_fat(sb);
17623 +}
17624 +
17625 +static inline int au_test_vfat(struct super_block *sb)
17626 +{
17627 +       return au_test_fat(sb);
17628 +}
17629 +
17630 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17631 +{
17632 +#ifdef CONFIG_SECURITYFS
17633 +       return sb->s_magic == SECURITYFS_MAGIC;
17634 +#else
17635 +       return 0;
17636 +#endif
17637 +}
17638 +
17639 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17640 +{
17641 +#if IS_ENABLED(CONFIG_SQUASHFS)
17642 +       return sb->s_magic == SQUASHFS_MAGIC;
17643 +#else
17644 +       return 0;
17645 +#endif
17646 +}
17647 +
17648 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17649 +{
17650 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17651 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17652 +#else
17653 +       return 0;
17654 +#endif
17655 +}
17656 +
17657 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17658 +{
17659 +#if IS_ENABLED(CONFIG_XENFS)
17660 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17661 +#else
17662 +       return 0;
17663 +#endif
17664 +}
17665 +
17666 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17667 +{
17668 +#ifdef CONFIG_DEBUG_FS
17669 +       return sb->s_magic == DEBUGFS_MAGIC;
17670 +#else
17671 +       return 0;
17672 +#endif
17673 +}
17674 +
17675 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17676 +{
17677 +#if IS_ENABLED(CONFIG_NILFS)
17678 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17679 +#else
17680 +       return 0;
17681 +#endif
17682 +}
17683 +
17684 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17685 +{
17686 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17687 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17688 +#else
17689 +       return 0;
17690 +#endif
17691 +}
17692 +
17693 +/* ---------------------------------------------------------------------- */
17694 +/*
17695 + * they can't be an aufs branch.
17696 + */
17697 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17698 +{
17699 +       return
17700 +#ifndef CONFIG_AUFS_BR_RAMFS
17701 +               au_test_ramfs(sb) ||
17702 +#endif
17703 +               au_test_procfs(sb)
17704 +               || au_test_sysfs(sb)
17705 +               || au_test_configfs(sb)
17706 +               || au_test_debugfs(sb)
17707 +               || au_test_securityfs(sb)
17708 +               || au_test_xenfs(sb)
17709 +               || au_test_ecryptfs(sb)
17710 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17711 +               || au_test_aufs(sb); /* will be supported in next version */
17712 +}
17713 +
17714 +static inline int au_test_fs_remote(struct super_block *sb)
17715 +{
17716 +       return !au_test_tmpfs(sb)
17717 +#ifdef CONFIG_AUFS_BR_RAMFS
17718 +               && !au_test_ramfs(sb)
17719 +#endif
17720 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17721 +}
17722 +
17723 +/* ---------------------------------------------------------------------- */
17724 +
17725 +/*
17726 + * Note: these functions (below) are created after reading ->getattr() in all
17727 + * filesystems under linux/fs. it means we have to do so in every update...
17728 + */
17729 +
17730 +/*
17731 + * some filesystems require getattr to refresh the inode attributes before
17732 + * referencing.
17733 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17734 + * and leave the work for d_revalidate()
17735 + */
17736 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17737 +{
17738 +       return au_test_nfs(sb)
17739 +               || au_test_fuse(sb)
17740 +               /* || au_test_btrfs(sb) */      /* untested */
17741 +               ;
17742 +}
17743 +
17744 +/*
17745 + * filesystems which don't maintain i_size or i_blocks.
17746 + */
17747 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17748 +{
17749 +       return au_test_xfs(sb)
17750 +               || au_test_btrfs(sb)
17751 +               || au_test_ubifs(sb)
17752 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17753 +               /* || au_test_minix(sb) */      /* untested */
17754 +               ;
17755 +}
17756 +
17757 +/*
17758 + * filesystems which don't store the correct value in some of their inode
17759 + * attributes.
17760 + */
17761 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17762 +{
17763 +       return au_test_fs_bad_iattr_size(sb)
17764 +               || au_test_fat(sb)
17765 +               || au_test_msdos(sb)
17766 +               || au_test_vfat(sb);
17767 +}
17768 +
17769 +/* they don't check i_nlink in link(2) */
17770 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17771 +{
17772 +       return au_test_tmpfs(sb)
17773 +#ifdef CONFIG_AUFS_BR_RAMFS
17774 +               || au_test_ramfs(sb)
17775 +#endif
17776 +               || au_test_ubifs(sb)
17777 +               || au_test_hfsplus(sb);
17778 +}
17779 +
17780 +/*
17781 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17782 + */
17783 +static inline int au_test_fs_notime(struct super_block *sb)
17784 +{
17785 +       return au_test_nfs(sb)
17786 +               || au_test_fuse(sb)
17787 +               || au_test_ubifs(sb)
17788 +               ;
17789 +}
17790 +
17791 +/* temporary support for i#1 in cramfs */
17792 +static inline int au_test_fs_unique_ino(struct inode *inode)
17793 +{
17794 +       if (au_test_cramfs(inode->i_sb))
17795 +               return inode->i_ino != 1;
17796 +       return 1;
17797 +}
17798 +
17799 +/* ---------------------------------------------------------------------- */
17800 +
17801 +/*
17802 + * the filesystem where the xino files placed must support i/o after unlink and
17803 + * maintain i_size and i_blocks.
17804 + */
17805 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17806 +{
17807 +       return au_test_fs_remote(sb)
17808 +               || au_test_fs_bad_iattr_size(sb)
17809 +               /* don't want unnecessary work for xino */
17810 +               || au_test_aufs(sb)
17811 +               || au_test_ecryptfs(sb)
17812 +               || au_test_nilfs(sb);
17813 +}
17814 +
17815 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17816 +{
17817 +       return au_test_tmpfs(sb)
17818 +               || au_test_ramfs(sb);
17819 +}
17820 +
17821 +/*
17822 + * test if the @sb is real-readonly.
17823 + */
17824 +static inline int au_test_fs_rr(struct super_block *sb)
17825 +{
17826 +       return au_test_squashfs(sb)
17827 +               || au_test_iso9660(sb)
17828 +               || au_test_cramfs(sb)
17829 +               || au_test_romfs(sb);
17830 +}
17831 +
17832 +/*
17833 + * test if the @inode is nfs with 'noacl' option
17834 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17835 + */
17836 +static inline int au_test_nfs_noacl(struct inode *inode)
17837 +{
17838 +       return au_test_nfs(inode->i_sb)
17839 +               /* && IS_POSIXACL(inode) */
17840 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17841 +}
17842 +
17843 +#endif /* __KERNEL__ */
17844 +#endif /* __AUFS_FSTYPE_H__ */
17845 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17846 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17847 +++ linux/fs/aufs/hbl.h 2022-11-05 23:02:18.965889284 +0100
17848 @@ -0,0 +1,65 @@
17849 +/* SPDX-License-Identifier: GPL-2.0 */
17850 +/*
17851 + * Copyright (C) 2017-2022 Junjiro R. Okajima
17852 + *
17853 + * This program is free software; you can redistribute it and/or modify
17854 + * it under the terms of the GNU General Public License as published by
17855 + * the Free Software Foundation; either version 2 of the License, or
17856 + * (at your option) any later version.
17857 + *
17858 + * This program is distributed in the hope that it will be useful,
17859 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17860 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17861 + * GNU General Public License for more details.
17862 + *
17863 + * You should have received a copy of the GNU General Public License
17864 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17865 + */
17866 +
17867 +/*
17868 + * helpers for hlist_bl.h
17869 + */
17870 +
17871 +#ifndef __AUFS_HBL_H__
17872 +#define __AUFS_HBL_H__
17873 +
17874 +#ifdef __KERNEL__
17875 +
17876 +#include <linux/list_bl.h>
17877 +
17878 +static inline void au_hbl_add(struct hlist_bl_node *node,
17879 +                             struct hlist_bl_head *hbl)
17880 +{
17881 +       hlist_bl_lock(hbl);
17882 +       hlist_bl_add_head(node, hbl);
17883 +       hlist_bl_unlock(hbl);
17884 +}
17885 +
17886 +static inline void au_hbl_del(struct hlist_bl_node *node,
17887 +                             struct hlist_bl_head *hbl)
17888 +{
17889 +       hlist_bl_lock(hbl);
17890 +       hlist_bl_del(node);
17891 +       hlist_bl_unlock(hbl);
17892 +}
17893 +
17894 +#define au_hbl_for_each(pos, head)                                     \
17895 +       for (pos = hlist_bl_first(head);                                \
17896 +            pos;                                                       \
17897 +            pos = pos->next)
17898 +
17899 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17900 +{
17901 +       unsigned long cnt;
17902 +       struct hlist_bl_node *pos;
17903 +
17904 +       cnt = 0;
17905 +       hlist_bl_lock(hbl);
17906 +       au_hbl_for_each(pos, hbl)
17907 +               cnt++;
17908 +       hlist_bl_unlock(hbl);
17909 +       return cnt;
17910 +}
17911 +
17912 +#endif /* __KERNEL__ */
17913 +#endif /* __AUFS_HBL_H__ */
17914 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17915 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17916 +++ linux/fs/aufs/hfsnotify.c   2022-11-05 23:02:18.965889284 +0100
17917 @@ -0,0 +1,290 @@
17918 +// SPDX-License-Identifier: GPL-2.0
17919 +/*
17920 + * Copyright (C) 2005-2022 Junjiro R. Okajima
17921 + *
17922 + * This program is free software; you can redistribute it and/or modify
17923 + * it under the terms of the GNU General Public License as published by
17924 + * the Free Software Foundation; either version 2 of the License, or
17925 + * (at your option) any later version.
17926 + *
17927 + * This program is distributed in the hope that it will be useful,
17928 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17929 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17930 + * GNU General Public License for more details.
17931 + *
17932 + * You should have received a copy of the GNU General Public License
17933 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17934 + */
17935 +
17936 +/*
17937 + * fsnotify for the lower directories
17938 + */
17939 +
17940 +#include "aufs.h"
17941 +
17942 +/* FS_IN_IGNORED is unnecessary */
17943 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17944 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17945 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17946 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17947 +
17948 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17949 +{
17950 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17951 +                                            hn_mark);
17952 +       /* AuDbg("here\n"); */
17953 +       au_cache_free_hnotify(hn);
17954 +       smp_mb__before_atomic(); /* for atomic64_dec */
17955 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17956 +               wake_up(&au_hfsn_wq);
17957 +}
17958 +
17959 +static int au_hfsn_alloc(struct au_hinode *hinode)
17960 +{
17961 +       int err;
17962 +       struct au_hnotify *hn;
17963 +       struct super_block *sb;
17964 +       struct au_branch *br;
17965 +       struct fsnotify_mark *mark;
17966 +       aufs_bindex_t bindex;
17967 +
17968 +       hn = hinode->hi_notify;
17969 +       sb = hn->hn_aufs_inode->i_sb;
17970 +       bindex = au_br_index(sb, hinode->hi_id);
17971 +       br = au_sbr(sb, bindex);
17972 +       AuDebugOn(!br->br_hfsn);
17973 +
17974 +       mark = &hn->hn_mark;
17975 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17976 +       mark->mask = AuHfsnMask;
17977 +       /*
17978 +        * by udba rename or rmdir, aufs assign a new inode to the known
17979 +        * h_inode, so specify 1 to allow dups.
17980 +        */
17981 +       lockdep_off();
17982 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17983 +       lockdep_on();
17984 +
17985 +       return err;
17986 +}
17987 +
17988 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17989 +{
17990 +       struct fsnotify_mark *mark;
17991 +       unsigned long long ull;
17992 +       struct fsnotify_group *group;
17993 +
17994 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17995 +       BUG_ON(!ull);
17996 +
17997 +       mark = &hn->hn_mark;
17998 +       spin_lock(&mark->lock);
17999 +       group = mark->group;
18000 +       fsnotify_get_group(group);
18001 +       spin_unlock(&mark->lock);
18002 +       lockdep_off();
18003 +       fsnotify_destroy_mark(mark, group);
18004 +       fsnotify_put_mark(mark);
18005 +       fsnotify_put_group(group);
18006 +       lockdep_on();
18007 +
18008 +       /* free hn by myself */
18009 +       return 0;
18010 +}
18011 +
18012 +/* ---------------------------------------------------------------------- */
18013 +
18014 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18015 +{
18016 +       struct fsnotify_mark *mark;
18017 +
18018 +       mark = &hinode->hi_notify->hn_mark;
18019 +       spin_lock(&mark->lock);
18020 +       if (do_set) {
18021 +               AuDebugOn(mark->mask & AuHfsnMask);
18022 +               mark->mask |= AuHfsnMask;
18023 +       } else {
18024 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18025 +               mark->mask &= ~AuHfsnMask;
18026 +       }
18027 +       spin_unlock(&mark->lock);
18028 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18029 +}
18030 +
18031 +/* ---------------------------------------------------------------------- */
18032 +
18033 +/* #define AuDbgHnotify */
18034 +#ifdef AuDbgHnotify
18035 +static char *au_hfsn_name(u32 mask)
18036 +{
18037 +#ifdef CONFIG_AUFS_DEBUG
18038 +#define test_ret(flag)                         \
18039 +       do {                                    \
18040 +               if (mask & flag)                \
18041 +                       return #flag;           \
18042 +       } while (0)
18043 +       test_ret(FS_ACCESS);
18044 +       test_ret(FS_MODIFY);
18045 +       test_ret(FS_ATTRIB);
18046 +       test_ret(FS_CLOSE_WRITE);
18047 +       test_ret(FS_CLOSE_NOWRITE);
18048 +       test_ret(FS_OPEN);
18049 +       test_ret(FS_MOVED_FROM);
18050 +       test_ret(FS_MOVED_TO);
18051 +       test_ret(FS_CREATE);
18052 +       test_ret(FS_DELETE);
18053 +       test_ret(FS_DELETE_SELF);
18054 +       test_ret(FS_MOVE_SELF);
18055 +       test_ret(FS_UNMOUNT);
18056 +       test_ret(FS_Q_OVERFLOW);
18057 +       test_ret(FS_IN_IGNORED);
18058 +       test_ret(FS_ISDIR);
18059 +       test_ret(FS_IN_ONESHOT);
18060 +       test_ret(FS_EVENT_ON_CHILD);
18061 +       return "";
18062 +#undef test_ret
18063 +#else
18064 +       return "??";
18065 +#endif
18066 +}
18067 +#endif
18068 +
18069 +/* ---------------------------------------------------------------------- */
18070 +
18071 +static void au_hfsn_free_group(struct fsnotify_group *group)
18072 +{
18073 +       struct au_br_hfsnotify *hfsn = group->private;
18074 +
18075 +       /* AuDbg("here\n"); */
18076 +       au_kfree_try_rcu(hfsn);
18077 +}
18078 +
18079 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18080 +                               u32 mask, const void *data, int data_type,
18081 +                               struct inode *dir,
18082 +                               const struct qstr *file_name, u32 cookie,
18083 +                               struct fsnotify_iter_info *iter_info)
18084 +{
18085 +       int err;
18086 +       struct au_hnotify *hnotify;
18087 +       struct inode *h_dir, *h_inode;
18088 +       struct fsnotify_mark *inode_mark;
18089 +
18090 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18091 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18092 +
18093 +       err = 0;
18094 +       /* if FS_UNMOUNT happens, there must be another bug */
18095 +       AuDebugOn(mask & FS_UNMOUNT);
18096 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18097 +               goto out;
18098 +
18099 +       h_dir = dir;
18100 +       h_inode = NULL;
18101 +#ifdef AuDbgHnotify
18102 +       au_debug_on();
18103 +       if (1 || file_name.len != sizeof(AUFS_XINO_FNAME) - 1
18104 +           || strncmp(file_name.name, AUFS_XINO_FNAME, file_name.len)) {
18105 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18106 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18107 +                     AuLNPair(file_name), h_inode ? h_inode->i_ino : 0);
18108 +               /* WARN_ON(1); */
18109 +       }
18110 +       au_debug_off();
18111 +#endif
18112 +
18113 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18114 +       AuDebugOn(!inode_mark);
18115 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18116 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18117 +
18118 +out:
18119 +       return err;
18120 +}
18121 +
18122 +static struct fsnotify_ops au_hfsn_ops = {
18123 +       .handle_event           = au_hfsn_handle_event,
18124 +       .free_group_priv        = au_hfsn_free_group,
18125 +       .free_mark              = au_hfsn_free_mark
18126 +};
18127 +
18128 +/* ---------------------------------------------------------------------- */
18129 +
18130 +static void au_hfsn_fin_br(struct au_branch *br)
18131 +{
18132 +       struct au_br_hfsnotify *hfsn;
18133 +
18134 +       hfsn = br->br_hfsn;
18135 +       if (hfsn) {
18136 +               lockdep_off();
18137 +               fsnotify_put_group(hfsn->hfsn_group);
18138 +               lockdep_on();
18139 +       }
18140 +}
18141 +
18142 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18143 +{
18144 +       int err;
18145 +       struct fsnotify_group *group;
18146 +       struct au_br_hfsnotify *hfsn;
18147 +
18148 +       err = 0;
18149 +       br->br_hfsn = NULL;
18150 +       if (!au_br_hnotifyable(perm))
18151 +               goto out;
18152 +
18153 +       err = -ENOMEM;
18154 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18155 +       if (unlikely(!hfsn))
18156 +               goto out;
18157 +
18158 +       err = 0;
18159 +       group = fsnotify_alloc_group(&au_hfsn_ops,
18160 +                                    /*flags - not for userspace*/0);
18161 +       if (IS_ERR(group)) {
18162 +               err = PTR_ERR(group);
18163 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18164 +               goto out_hfsn;
18165 +       }
18166 +
18167 +       group->private = hfsn;
18168 +       hfsn->hfsn_group = group;
18169 +       br->br_hfsn = hfsn;
18170 +       goto out; /* success */
18171 +
18172 +out_hfsn:
18173 +       au_kfree_try_rcu(hfsn);
18174 +out:
18175 +       return err;
18176 +}
18177 +
18178 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18179 +{
18180 +       int err;
18181 +
18182 +       err = 0;
18183 +       if (!br->br_hfsn)
18184 +               err = au_hfsn_init_br(br, perm);
18185 +
18186 +       return err;
18187 +}
18188 +
18189 +/* ---------------------------------------------------------------------- */
18190 +
18191 +static void au_hfsn_fin(void)
18192 +{
18193 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18194 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18195 +}
18196 +
18197 +const struct au_hnotify_op au_hnotify_op = {
18198 +       .ctl            = au_hfsn_ctl,
18199 +       .alloc          = au_hfsn_alloc,
18200 +       .free           = au_hfsn_free,
18201 +
18202 +       .fin            = au_hfsn_fin,
18203 +
18204 +       .reset_br       = au_hfsn_reset_br,
18205 +       .fin_br         = au_hfsn_fin_br,
18206 +       .init_br        = au_hfsn_init_br
18207 +};
18208 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18209 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18210 +++ linux/fs/aufs/hfsplus.c     2022-11-05 23:02:18.965889284 +0100
18211 @@ -0,0 +1,60 @@
18212 +// SPDX-License-Identifier: GPL-2.0
18213 +/*
18214 + * Copyright (C) 2010-2022 Junjiro R. Okajima
18215 + *
18216 + * This program is free software; you can redistribute it and/or modify
18217 + * it under the terms of the GNU General Public License as published by
18218 + * the Free Software Foundation; either version 2 of the License, or
18219 + * (at your option) any later version.
18220 + *
18221 + * This program is distributed in the hope that it will be useful,
18222 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18223 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18224 + * GNU General Public License for more details.
18225 + *
18226 + * You should have received a copy of the GNU General Public License
18227 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18228 + */
18229 +
18230 +/*
18231 + * special support for filesystems which acquires an inode mutex
18232 + * at final closing a file, eg, hfsplus.
18233 + *
18234 + * This trick is very simple and stupid, just to open the file before really
18235 + * necessary open to tell hfsplus that this is not the final closing.
18236 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18237 + * and au_h_open_post() after releasing it.
18238 + */
18239 +
18240 +#include "aufs.h"
18241 +
18242 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18243 +                          int force_wr)
18244 +{
18245 +       struct file *h_file;
18246 +       struct dentry *h_dentry;
18247 +
18248 +       h_dentry = au_h_dptr(dentry, bindex);
18249 +       AuDebugOn(!h_dentry);
18250 +       AuDebugOn(d_is_negative(h_dentry));
18251 +
18252 +       h_file = NULL;
18253 +       if (au_test_hfsplus(h_dentry->d_sb)
18254 +           && d_is_reg(h_dentry))
18255 +               h_file = au_h_open(dentry, bindex,
18256 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18257 +                                  /*file*/NULL, force_wr);
18258 +       return h_file;
18259 +}
18260 +
18261 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18262 +                   struct file *h_file)
18263 +{
18264 +       struct au_branch *br;
18265 +
18266 +       if (h_file) {
18267 +               fput(h_file);
18268 +               br = au_sbr(dentry->d_sb, bindex);
18269 +               au_lcnt_dec(&br->br_nfiles);
18270 +       }
18271 +}
18272 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18273 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18274 +++ linux/fs/aufs/hnotify.c     2022-11-05 23:02:18.965889284 +0100
18275 @@ -0,0 +1,715 @@
18276 +// SPDX-License-Identifier: GPL-2.0
18277 +/*
18278 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18279 + *
18280 + * This program is free software; you can redistribute it and/or modify
18281 + * it under the terms of the GNU General Public License as published by
18282 + * the Free Software Foundation; either version 2 of the License, or
18283 + * (at your option) any later version.
18284 + *
18285 + * This program is distributed in the hope that it will be useful,
18286 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18287 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18288 + * GNU General Public License for more details.
18289 + *
18290 + * You should have received a copy of the GNU General Public License
18291 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18292 + */
18293 +
18294 +/*
18295 + * abstraction to notify the direct changes on lower directories
18296 + */
18297 +
18298 +/* #include <linux/iversion.h> */
18299 +#include "aufs.h"
18300 +
18301 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18302 +{
18303 +       int err;
18304 +       struct au_hnotify *hn;
18305 +
18306 +       err = -ENOMEM;
18307 +       hn = au_cache_alloc_hnotify();
18308 +       if (hn) {
18309 +               hn->hn_aufs_inode = inode;
18310 +               hinode->hi_notify = hn;
18311 +               err = au_hnotify_op.alloc(hinode);
18312 +               AuTraceErr(err);
18313 +               if (unlikely(err)) {
18314 +                       hinode->hi_notify = NULL;
18315 +                       au_cache_free_hnotify(hn);
18316 +                       /*
18317 +                        * The upper dir was removed by udba, but the same named
18318 +                        * dir left. In this case, aufs assigns a new inode
18319 +                        * number and set the monitor again.
18320 +                        * For the lower dir, the old monitor is still left.
18321 +                        */
18322 +                       if (err == -EEXIST)
18323 +                               err = 0;
18324 +               }
18325 +       }
18326 +
18327 +       AuTraceErr(err);
18328 +       return err;
18329 +}
18330 +
18331 +void au_hn_free(struct au_hinode *hinode)
18332 +{
18333 +       struct au_hnotify *hn;
18334 +
18335 +       hn = hinode->hi_notify;
18336 +       if (hn) {
18337 +               hinode->hi_notify = NULL;
18338 +               if (au_hnotify_op.free(hinode, hn))
18339 +                       au_cache_free_hnotify(hn);
18340 +       }
18341 +}
18342 +
18343 +/* ---------------------------------------------------------------------- */
18344 +
18345 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18346 +{
18347 +       if (hinode->hi_notify)
18348 +               au_hnotify_op.ctl(hinode, do_set);
18349 +}
18350 +
18351 +void au_hn_reset(struct inode *inode, unsigned int flags)
18352 +{
18353 +       aufs_bindex_t bindex, bbot;
18354 +       struct inode *hi;
18355 +       struct dentry *iwhdentry;
18356 +
18357 +       bbot = au_ibbot(inode);
18358 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18359 +               hi = au_h_iptr(inode, bindex);
18360 +               if (!hi)
18361 +                       continue;
18362 +
18363 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18364 +               iwhdentry = au_hi_wh(inode, bindex);
18365 +               if (iwhdentry)
18366 +                       dget(iwhdentry);
18367 +               au_igrab(hi);
18368 +               au_set_h_iptr(inode, bindex, NULL, 0);
18369 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18370 +                             flags & ~AuHi_XINO);
18371 +               iput(hi);
18372 +               dput(iwhdentry);
18373 +               /* inode_unlock(hi); */
18374 +       }
18375 +}
18376 +
18377 +/* ---------------------------------------------------------------------- */
18378 +
18379 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18380 +{
18381 +       int err;
18382 +       aufs_bindex_t bindex, bbot, bfound, btop;
18383 +       struct inode *h_i;
18384 +
18385 +       err = 0;
18386 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18387 +               pr_warn("branch root dir was changed\n");
18388 +               goto out;
18389 +       }
18390 +
18391 +       bfound = -1;
18392 +       bbot = au_ibbot(inode);
18393 +       btop = au_ibtop(inode);
18394 +#if 0 /* reserved for future use */
18395 +       if (bindex == bbot) {
18396 +               /* keep this ino in rename case */
18397 +               goto out;
18398 +       }
18399 +#endif
18400 +       for (bindex = btop; bindex <= bbot; bindex++)
18401 +               if (au_h_iptr(inode, bindex) == h_inode) {
18402 +                       bfound = bindex;
18403 +                       break;
18404 +               }
18405 +       if (bfound < 0)
18406 +               goto out;
18407 +
18408 +       for (bindex = btop; bindex <= bbot; bindex++) {
18409 +               h_i = au_h_iptr(inode, bindex);
18410 +               if (!h_i)
18411 +                       continue;
18412 +
18413 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18414 +               /* ignore this error */
18415 +               /* bad action? */
18416 +       }
18417 +
18418 +       /* children inode number will be broken */
18419 +
18420 +out:
18421 +       AuTraceErr(err);
18422 +       return err;
18423 +}
18424 +
18425 +static int hn_gen_tree(struct dentry *dentry)
18426 +{
18427 +       int err, i, j, ndentry;
18428 +       struct au_dcsub_pages dpages;
18429 +       struct au_dpage *dpage;
18430 +       struct dentry **dentries;
18431 +
18432 +       err = au_dpages_init(&dpages, GFP_NOFS);
18433 +       if (unlikely(err))
18434 +               goto out;
18435 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18436 +       if (unlikely(err))
18437 +               goto out_dpages;
18438 +
18439 +       for (i = 0; i < dpages.ndpage; i++) {
18440 +               dpage = dpages.dpages + i;
18441 +               dentries = dpage->dentries;
18442 +               ndentry = dpage->ndentry;
18443 +               for (j = 0; j < ndentry; j++) {
18444 +                       struct dentry *d;
18445 +
18446 +                       d = dentries[j];
18447 +                       if (IS_ROOT(d))
18448 +                               continue;
18449 +
18450 +                       au_digen_dec(d);
18451 +                       if (d_really_is_positive(d))
18452 +                               /* todo: reset children xino?
18453 +                                  cached children only? */
18454 +                               au_iigen_dec(d_inode(d));
18455 +               }
18456 +       }
18457 +
18458 +out_dpages:
18459 +       au_dpages_free(&dpages);
18460 +out:
18461 +       return err;
18462 +}
18463 +
18464 +/*
18465 + * return 0 if processed.
18466 + */
18467 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18468 +                          const unsigned int isdir)
18469 +{
18470 +       int err;
18471 +       struct dentry *d;
18472 +       struct qstr *dname;
18473 +
18474 +       err = 1;
18475 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18476 +               pr_warn("branch root dir was changed\n");
18477 +               err = 0;
18478 +               goto out;
18479 +       }
18480 +
18481 +       if (!isdir) {
18482 +               AuDebugOn(!name);
18483 +               au_iigen_dec(inode);
18484 +               spin_lock(&inode->i_lock);
18485 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18486 +                       spin_lock(&d->d_lock);
18487 +                       dname = &d->d_name;
18488 +                       if (dname->len != nlen
18489 +                           && memcmp(dname->name, name, nlen)) {
18490 +                               spin_unlock(&d->d_lock);
18491 +                               continue;
18492 +                       }
18493 +                       err = 0;
18494 +                       au_digen_dec(d);
18495 +                       spin_unlock(&d->d_lock);
18496 +                       break;
18497 +               }
18498 +               spin_unlock(&inode->i_lock);
18499 +       } else {
18500 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18501 +               d = d_find_any_alias(inode);
18502 +               if (!d) {
18503 +                       au_iigen_dec(inode);
18504 +                       goto out;
18505 +               }
18506 +
18507 +               spin_lock(&d->d_lock);
18508 +               dname = &d->d_name;
18509 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18510 +                       spin_unlock(&d->d_lock);
18511 +                       err = hn_gen_tree(d);
18512 +                       spin_lock(&d->d_lock);
18513 +               }
18514 +               spin_unlock(&d->d_lock);
18515 +               dput(d);
18516 +       }
18517 +
18518 +out:
18519 +       AuTraceErr(err);
18520 +       return err;
18521 +}
18522 +
18523 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18524 +{
18525 +       int err;
18526 +
18527 +       if (IS_ROOT(dentry)) {
18528 +               pr_warn("branch root dir was changed\n");
18529 +               return 0;
18530 +       }
18531 +
18532 +       err = 0;
18533 +       if (!isdir) {
18534 +               au_digen_dec(dentry);
18535 +               if (d_really_is_positive(dentry))
18536 +                       au_iigen_dec(d_inode(dentry));
18537 +       } else {
18538 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18539 +               if (d_really_is_positive(dentry))
18540 +                       err = hn_gen_tree(dentry);
18541 +       }
18542 +
18543 +       AuTraceErr(err);
18544 +       return err;
18545 +}
18546 +
18547 +/* ---------------------------------------------------------------------- */
18548 +
18549 +/* hnotify job flags */
18550 +#define AuHnJob_XINO0          1
18551 +#define AuHnJob_GEN            (1 << 1)
18552 +#define AuHnJob_DIRENT         (1 << 2)
18553 +#define AuHnJob_ISDIR          (1 << 3)
18554 +#define AuHnJob_TRYXINO0       (1 << 4)
18555 +#define AuHnJob_MNTPNT         (1 << 5)
18556 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18557 +#define au_fset_hnjob(flags, name) \
18558 +       do { (flags) |= AuHnJob_##name; } while (0)
18559 +#define au_fclr_hnjob(flags, name) \
18560 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18561 +
18562 +enum {
18563 +       AuHn_CHILD,
18564 +       AuHn_PARENT,
18565 +       AuHnLast
18566 +};
18567 +
18568 +struct au_hnotify_args {
18569 +       struct inode *h_dir, *dir, *h_child_inode;
18570 +       u32 mask;
18571 +       unsigned int flags[AuHnLast];
18572 +       unsigned int h_child_nlen;
18573 +       char h_child_name[];
18574 +};
18575 +
18576 +struct hn_job_args {
18577 +       unsigned int flags;
18578 +       struct inode *inode, *h_inode, *dir, *h_dir;
18579 +       struct dentry *dentry;
18580 +       char *h_name;
18581 +       int h_nlen;
18582 +};
18583 +
18584 +static int hn_job(struct hn_job_args *a)
18585 +{
18586 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18587 +       int e;
18588 +
18589 +       /* reset xino */
18590 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18591 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18592 +
18593 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18594 +           && a->inode
18595 +           && a->h_inode) {
18596 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18597 +               if (!a->h_inode->i_nlink
18598 +                   && !(a->h_inode->i_state & I_LINKABLE))
18599 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18600 +               inode_unlock_shared(a->h_inode);
18601 +       }
18602 +
18603 +       /* make the generation obsolete */
18604 +       if (au_ftest_hnjob(a->flags, GEN)) {
18605 +               e = -1;
18606 +               if (a->inode)
18607 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18608 +                                             isdir);
18609 +               if (e && a->dentry)
18610 +                       hn_gen_by_name(a->dentry, isdir);
18611 +               /* ignore this error */
18612 +       }
18613 +
18614 +       /* make dir entries obsolete */
18615 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18616 +               struct au_vdir *vdir;
18617 +
18618 +               vdir = au_ivdir(a->inode);
18619 +               if (vdir)
18620 +                       vdir->vd_jiffy = 0;
18621 +               /* IMustLock(a->inode); */
18622 +               /* inode_inc_iversion(a->inode); */
18623 +       }
18624 +
18625 +       /* can do nothing but warn */
18626 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18627 +           && a->dentry
18628 +           && d_mountpoint(a->dentry))
18629 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18630 +
18631 +       return 0;
18632 +}
18633 +
18634 +/* ---------------------------------------------------------------------- */
18635 +
18636 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18637 +                                          struct inode *dir)
18638 +{
18639 +       struct dentry *dentry, *d, *parent;
18640 +       struct qstr *dname;
18641 +
18642 +       parent = d_find_any_alias(dir);
18643 +       if (!parent)
18644 +               return NULL;
18645 +
18646 +       dentry = NULL;
18647 +       spin_lock(&parent->d_lock);
18648 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18649 +               /* AuDbg("%pd\n", d); */
18650 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18651 +               dname = &d->d_name;
18652 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18653 +                       goto cont_unlock;
18654 +               if (au_di(d))
18655 +                       au_digen_dec(d);
18656 +               else
18657 +                       goto cont_unlock;
18658 +               if (au_dcount(d) > 0) {
18659 +                       dentry = dget_dlock(d);
18660 +                       spin_unlock(&d->d_lock);
18661 +                       break;
18662 +               }
18663 +
18664 +cont_unlock:
18665 +               spin_unlock(&d->d_lock);
18666 +       }
18667 +       spin_unlock(&parent->d_lock);
18668 +       dput(parent);
18669 +
18670 +       if (dentry)
18671 +               di_write_lock_child(dentry);
18672 +
18673 +       return dentry;
18674 +}
18675 +
18676 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18677 +                                        aufs_bindex_t bindex, ino_t h_ino)
18678 +{
18679 +       struct inode *inode;
18680 +       ino_t ino;
18681 +       int err;
18682 +
18683 +       inode = NULL;
18684 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18685 +       if (!err && ino)
18686 +               inode = ilookup(sb, ino);
18687 +       if (!inode)
18688 +               goto out;
18689 +
18690 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18691 +               pr_warn("wrong root branch\n");
18692 +               iput(inode);
18693 +               inode = NULL;
18694 +               goto out;
18695 +       }
18696 +
18697 +       ii_write_lock_child(inode);
18698 +
18699 +out:
18700 +       return inode;
18701 +}
18702 +
18703 +static void au_hn_bh(void *_args)
18704 +{
18705 +       struct au_hnotify_args *a = _args;
18706 +       struct super_block *sb;
18707 +       aufs_bindex_t bindex, bbot, bfound;
18708 +       unsigned char xino, try_iput;
18709 +       int err;
18710 +       struct inode *inode;
18711 +       ino_t h_ino;
18712 +       struct hn_job_args args;
18713 +       struct dentry *dentry;
18714 +       struct au_sbinfo *sbinfo;
18715 +
18716 +       AuDebugOn(!_args);
18717 +       AuDebugOn(!a->h_dir);
18718 +       AuDebugOn(!a->dir);
18719 +       AuDebugOn(!a->mask);
18720 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18721 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18722 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18723 +
18724 +       inode = NULL;
18725 +       dentry = NULL;
18726 +       /*
18727 +        * do not lock a->dir->i_mutex here
18728 +        * because of d_revalidate() may cause a deadlock.
18729 +        */
18730 +       sb = a->dir->i_sb;
18731 +       AuDebugOn(!sb);
18732 +       sbinfo = au_sbi(sb);
18733 +       AuDebugOn(!sbinfo);
18734 +       si_write_lock(sb, AuLock_NOPLMW);
18735 +
18736 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18737 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18738 +               case FS_MOVED_FROM:
18739 +               case FS_MOVED_TO:
18740 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18741 +                               "for the direct rename(2)\n");
18742 +               }
18743 +
18744 +       ii_read_lock_parent(a->dir);
18745 +       bfound = -1;
18746 +       bbot = au_ibbot(a->dir);
18747 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18748 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18749 +                       bfound = bindex;
18750 +                       break;
18751 +               }
18752 +       ii_read_unlock(a->dir);
18753 +       if (unlikely(bfound < 0))
18754 +               goto out;
18755 +
18756 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18757 +       h_ino = 0;
18758 +       if (a->h_child_inode)
18759 +               h_ino = a->h_child_inode->i_ino;
18760 +
18761 +       if (a->h_child_nlen
18762 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18763 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18764 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18765 +                                             a->dir);
18766 +       try_iput = 0;
18767 +       if (dentry && d_really_is_positive(dentry))
18768 +               inode = d_inode(dentry);
18769 +       if (xino && !inode && h_ino
18770 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18771 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18772 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18773 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18774 +               try_iput = 1;
18775 +       }
18776 +
18777 +       args.flags = a->flags[AuHn_CHILD];
18778 +       args.dentry = dentry;
18779 +       args.inode = inode;
18780 +       args.h_inode = a->h_child_inode;
18781 +       args.dir = a->dir;
18782 +       args.h_dir = a->h_dir;
18783 +       args.h_name = a->h_child_name;
18784 +       args.h_nlen = a->h_child_nlen;
18785 +       err = hn_job(&args);
18786 +       if (dentry) {
18787 +               if (au_di(dentry))
18788 +                       di_write_unlock(dentry);
18789 +               dput(dentry);
18790 +       }
18791 +       if (inode && try_iput) {
18792 +               ii_write_unlock(inode);
18793 +               iput(inode);
18794 +       }
18795 +
18796 +       ii_write_lock_parent(a->dir);
18797 +       args.flags = a->flags[AuHn_PARENT];
18798 +       args.dentry = NULL;
18799 +       args.inode = a->dir;
18800 +       args.h_inode = a->h_dir;
18801 +       args.dir = NULL;
18802 +       args.h_dir = NULL;
18803 +       args.h_name = NULL;
18804 +       args.h_nlen = 0;
18805 +       err = hn_job(&args);
18806 +       ii_write_unlock(a->dir);
18807 +
18808 +out:
18809 +       iput(a->h_child_inode);
18810 +       iput(a->h_dir);
18811 +       iput(a->dir);
18812 +       si_write_unlock(sb);
18813 +       au_nwt_done(&sbinfo->si_nowait);
18814 +       au_kfree_rcu(a);
18815 +}
18816 +
18817 +/* ---------------------------------------------------------------------- */
18818 +
18819 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18820 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18821 +{
18822 +       int err, len;
18823 +       unsigned int flags[AuHnLast], f;
18824 +       unsigned char isdir, isroot, wh;
18825 +       struct inode *dir;
18826 +       struct au_hnotify_args *args;
18827 +       char *p, *h_child_name;
18828 +
18829 +       err = 0;
18830 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18831 +       dir = igrab(hnotify->hn_aufs_inode);
18832 +       if (!dir)
18833 +               goto out;
18834 +
18835 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18836 +       wh = 0;
18837 +       h_child_name = (void *)h_child_qstr->name;
18838 +       len = h_child_qstr->len;
18839 +       if (h_child_name) {
18840 +               if (len > AUFS_WH_PFX_LEN
18841 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18842 +                       h_child_name += AUFS_WH_PFX_LEN;
18843 +                       len -= AUFS_WH_PFX_LEN;
18844 +                       wh = 1;
18845 +               }
18846 +       }
18847 +
18848 +       isdir = 0;
18849 +       if (h_child_inode)
18850 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18851 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18852 +       flags[AuHn_CHILD] = 0;
18853 +       if (isdir)
18854 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18855 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18856 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18857 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18858 +       case FS_MOVED_FROM:
18859 +       case FS_MOVED_TO:
18860 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18861 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18862 +               fallthrough;
18863 +       case FS_CREATE:
18864 +               AuDebugOn(!h_child_name);
18865 +               break;
18866 +
18867 +       case FS_DELETE:
18868 +               /*
18869 +                * aufs never be able to get this child inode.
18870 +                * revalidation should be in d_revalidate()
18871 +                * by checking i_nlink, i_generation or d_unhashed().
18872 +                */
18873 +               AuDebugOn(!h_child_name);
18874 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18875 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18876 +               break;
18877 +
18878 +       default:
18879 +               AuDebugOn(1);
18880 +       }
18881 +
18882 +       if (wh)
18883 +               h_child_inode = NULL;
18884 +
18885 +       err = -ENOMEM;
18886 +       /* iput() and kfree() will be called in au_hnotify() */
18887 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18888 +       if (unlikely(!args)) {
18889 +               AuErr1("no memory\n");
18890 +               iput(dir);
18891 +               goto out;
18892 +       }
18893 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18894 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18895 +       args->mask = mask;
18896 +       args->dir = dir;
18897 +       args->h_dir = igrab(h_dir);
18898 +       if (h_child_inode)
18899 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18900 +       args->h_child_inode = h_child_inode;
18901 +       args->h_child_nlen = len;
18902 +       if (len) {
18903 +               p = (void *)args;
18904 +               p += sizeof(*args);
18905 +               memcpy(p, h_child_name, len);
18906 +               p[len] = 0;
18907 +       }
18908 +
18909 +       /* NFS fires the event for silly-renamed one from kworker */
18910 +       f = 0;
18911 +       if (!dir->i_nlink
18912 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18913 +               f = AuWkq_NEST;
18914 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18915 +       if (unlikely(err)) {
18916 +               pr_err("wkq %d\n", err);
18917 +               iput(args->h_child_inode);
18918 +               iput(args->h_dir);
18919 +               iput(args->dir);
18920 +               au_kfree_rcu(args);
18921 +       }
18922 +
18923 +out:
18924 +       return err;
18925 +}
18926 +
18927 +/* ---------------------------------------------------------------------- */
18928 +
18929 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18930 +{
18931 +       int err;
18932 +
18933 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18934 +
18935 +       err = 0;
18936 +       if (au_hnotify_op.reset_br)
18937 +               err = au_hnotify_op.reset_br(udba, br, perm);
18938 +
18939 +       return err;
18940 +}
18941 +
18942 +int au_hnotify_init_br(struct au_branch *br, int perm)
18943 +{
18944 +       int err;
18945 +
18946 +       err = 0;
18947 +       if (au_hnotify_op.init_br)
18948 +               err = au_hnotify_op.init_br(br, perm);
18949 +
18950 +       return err;
18951 +}
18952 +
18953 +void au_hnotify_fin_br(struct au_branch *br)
18954 +{
18955 +       if (au_hnotify_op.fin_br)
18956 +               au_hnotify_op.fin_br(br);
18957 +}
18958 +
18959 +static void au_hn_destroy_cache(void)
18960 +{
18961 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18962 +       au_cache[AuCache_HNOTIFY] = NULL;
18963 +}
18964 +
18965 +int __init au_hnotify_init(void)
18966 +{
18967 +       int err;
18968 +
18969 +       err = -ENOMEM;
18970 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18971 +       if (au_cache[AuCache_HNOTIFY]) {
18972 +               err = 0;
18973 +               if (au_hnotify_op.init)
18974 +                       err = au_hnotify_op.init();
18975 +               if (unlikely(err))
18976 +                       au_hn_destroy_cache();
18977 +       }
18978 +       AuTraceErr(err);
18979 +       return err;
18980 +}
18981 +
18982 +void au_hnotify_fin(void)
18983 +{
18984 +       if (au_hnotify_op.fin)
18985 +               au_hnotify_op.fin();
18986 +
18987 +       /* cf. au_cache_fin() */
18988 +       if (au_cache[AuCache_HNOTIFY])
18989 +               au_hn_destroy_cache();
18990 +}
18991 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18992 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18993 +++ linux/fs/aufs/iinfo.c       2022-11-05 23:02:18.965889284 +0100
18994 @@ -0,0 +1,286 @@
18995 +// SPDX-License-Identifier: GPL-2.0
18996 +/*
18997 + * Copyright (C) 2005-2022 Junjiro R. Okajima
18998 + *
18999 + * This program is free software; you can redistribute it and/or modify
19000 + * it under the terms of the GNU General Public License as published by
19001 + * the Free Software Foundation; either version 2 of the License, or
19002 + * (at your option) any later version.
19003 + *
19004 + * This program is distributed in the hope that it will be useful,
19005 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19006 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19007 + * GNU General Public License for more details.
19008 + *
19009 + * You should have received a copy of the GNU General Public License
19010 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19011 + */
19012 +
19013 +/*
19014 + * inode private data
19015 + */
19016 +
19017 +#include "aufs.h"
19018 +
19019 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19020 +{
19021 +       struct inode *h_inode;
19022 +       struct au_hinode *hinode;
19023 +
19024 +       IiMustAnyLock(inode);
19025 +
19026 +       hinode = au_hinode(au_ii(inode), bindex);
19027 +       h_inode = hinode->hi_inode;
19028 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19029 +       return h_inode;
19030 +}
19031 +
19032 +/* todo: hard/soft set? */
19033 +void au_hiput(struct au_hinode *hinode)
19034 +{
19035 +       au_hn_free(hinode);
19036 +       dput(hinode->hi_whdentry);
19037 +       iput(hinode->hi_inode);
19038 +}
19039 +
19040 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19041 +{
19042 +       unsigned int flags;
19043 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19044 +
19045 +       flags = 0;
19046 +       if (au_opt_test(mnt_flags, XINO))
19047 +               au_fset_hi(flags, XINO);
19048 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19049 +               au_fset_hi(flags, HNOTIFY);
19050 +       return flags;
19051 +}
19052 +
19053 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19054 +                  struct inode *h_inode, unsigned int flags)
19055 +{
19056 +       struct au_hinode *hinode;
19057 +       struct inode *hi;
19058 +       struct au_iinfo *iinfo = au_ii(inode);
19059 +
19060 +       IiMustWriteLock(inode);
19061 +
19062 +       hinode = au_hinode(iinfo, bindex);
19063 +       hi = hinode->hi_inode;
19064 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19065 +
19066 +       if (hi)
19067 +               au_hiput(hinode);
19068 +       hinode->hi_inode = h_inode;
19069 +       if (h_inode) {
19070 +               int err;
19071 +               struct super_block *sb = inode->i_sb;
19072 +               struct au_branch *br;
19073 +
19074 +               AuDebugOn(inode->i_mode
19075 +                         && (h_inode->i_mode & S_IFMT)
19076 +                         != (inode->i_mode & S_IFMT));
19077 +               if (bindex == iinfo->ii_btop)
19078 +                       au_cpup_igen(inode, h_inode);
19079 +               br = au_sbr(sb, bindex);
19080 +               hinode->hi_id = br->br_id;
19081 +               if (au_ftest_hi(flags, XINO)) {
19082 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19083 +                                           inode->i_ino);
19084 +                       if (unlikely(err))
19085 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19086 +               }
19087 +
19088 +               if (au_ftest_hi(flags, HNOTIFY)
19089 +                   && au_br_hnotifyable(br->br_perm)) {
19090 +                       err = au_hn_alloc(hinode, inode);
19091 +                       if (unlikely(err))
19092 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19093 +               }
19094 +       }
19095 +}
19096 +
19097 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19098 +                 struct dentry *h_wh)
19099 +{
19100 +       struct au_hinode *hinode;
19101 +
19102 +       IiMustWriteLock(inode);
19103 +
19104 +       hinode = au_hinode(au_ii(inode), bindex);
19105 +       AuDebugOn(hinode->hi_whdentry);
19106 +       hinode->hi_whdentry = h_wh;
19107 +}
19108 +
19109 +void au_update_iigen(struct inode *inode, int half)
19110 +{
19111 +       struct au_iinfo *iinfo;
19112 +       struct au_iigen *iigen;
19113 +       unsigned int sigen;
19114 +
19115 +       sigen = au_sigen(inode->i_sb);
19116 +       iinfo = au_ii(inode);
19117 +       iigen = &iinfo->ii_generation;
19118 +       spin_lock(&iigen->ig_spin);
19119 +       iigen->ig_generation = sigen;
19120 +       if (half)
19121 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19122 +       else
19123 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19124 +       spin_unlock(&iigen->ig_spin);
19125 +}
19126 +
19127 +/* it may be called at remount time, too */
19128 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19129 +{
19130 +       struct au_iinfo *iinfo;
19131 +       aufs_bindex_t bindex, bbot;
19132 +
19133 +       AuDebugOn(au_is_bad_inode(inode));
19134 +       IiMustWriteLock(inode);
19135 +
19136 +       iinfo = au_ii(inode);
19137 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19138 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19139 +                    bindex++) {
19140 +                       struct inode *h_i;
19141 +
19142 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19143 +                       if (h_i
19144 +                           && !h_i->i_nlink
19145 +                           && !(h_i->i_state & I_LINKABLE))
19146 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19147 +               }
19148 +       }
19149 +
19150 +       iinfo->ii_btop = -1;
19151 +       iinfo->ii_bbot = -1;
19152 +       bbot = au_sbbot(inode->i_sb);
19153 +       for (bindex = 0; bindex <= bbot; bindex++)
19154 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19155 +                       iinfo->ii_btop = bindex;
19156 +                       break;
19157 +               }
19158 +       if (iinfo->ii_btop >= 0)
19159 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19160 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19161 +                               iinfo->ii_bbot = bindex;
19162 +                               break;
19163 +                       }
19164 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19165 +}
19166 +
19167 +/* ---------------------------------------------------------------------- */
19168 +
19169 +void au_icntnr_init_once(void *_c)
19170 +{
19171 +       struct au_icntnr *c = _c;
19172 +       struct au_iinfo *iinfo = &c->iinfo;
19173 +
19174 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19175 +       au_rw_init(&iinfo->ii_rwsem);
19176 +       inode_init_once(&c->vfs_inode);
19177 +}
19178 +
19179 +void au_hinode_init(struct au_hinode *hinode)
19180 +{
19181 +       hinode->hi_inode = NULL;
19182 +       hinode->hi_id = -1;
19183 +       au_hn_init(hinode);
19184 +       hinode->hi_whdentry = NULL;
19185 +}
19186 +
19187 +int au_iinfo_init(struct inode *inode)
19188 +{
19189 +       struct au_iinfo *iinfo;
19190 +       struct super_block *sb;
19191 +       struct au_hinode *hi;
19192 +       int nbr, i;
19193 +
19194 +       sb = inode->i_sb;
19195 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19196 +       nbr = au_sbbot(sb) + 1;
19197 +       if (unlikely(nbr <= 0))
19198 +               nbr = 1;
19199 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19200 +       if (hi) {
19201 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19202 +
19203 +               iinfo->ii_hinode = hi;
19204 +               for (i = 0; i < nbr; i++, hi++)
19205 +                       au_hinode_init(hi);
19206 +
19207 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19208 +               iinfo->ii_btop = -1;
19209 +               iinfo->ii_bbot = -1;
19210 +               iinfo->ii_vdir = NULL;
19211 +               return 0;
19212 +       }
19213 +       return -ENOMEM;
19214 +}
19215 +
19216 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19217 +{
19218 +       int err, i;
19219 +       struct au_hinode *hip;
19220 +
19221 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19222 +
19223 +       err = -ENOMEM;
19224 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19225 +                         may_shrink);
19226 +       if (hip) {
19227 +               iinfo->ii_hinode = hip;
19228 +               i = iinfo->ii_bbot + 1;
19229 +               hip += i;
19230 +               for (; i < nbr; i++, hip++)
19231 +                       au_hinode_init(hip);
19232 +               err = 0;
19233 +       }
19234 +
19235 +       return err;
19236 +}
19237 +
19238 +void au_iinfo_fin(struct inode *inode)
19239 +{
19240 +       struct au_iinfo *iinfo;
19241 +       struct au_hinode *hi;
19242 +       struct super_block *sb;
19243 +       aufs_bindex_t bindex, bbot;
19244 +       const unsigned char unlinked = !inode->i_nlink;
19245 +
19246 +       AuDebugOn(au_is_bad_inode(inode));
19247 +
19248 +       sb = inode->i_sb;
19249 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19250 +       if (si_pid_test(sb))
19251 +               au_xino_delete_inode(inode, unlinked);
19252 +       else {
19253 +               /*
19254 +                * it is safe to hide the dependency between sbinfo and
19255 +                * sb->s_umount.
19256 +                */
19257 +               lockdep_off();
19258 +               si_noflush_read_lock(sb);
19259 +               au_xino_delete_inode(inode, unlinked);
19260 +               si_read_unlock(sb);
19261 +               lockdep_on();
19262 +       }
19263 +
19264 +       iinfo = au_ii(inode);
19265 +       if (iinfo->ii_vdir)
19266 +               au_vdir_free(iinfo->ii_vdir);
19267 +
19268 +       bindex = iinfo->ii_btop;
19269 +       if (bindex >= 0) {
19270 +               hi = au_hinode(iinfo, bindex);
19271 +               bbot = iinfo->ii_bbot;
19272 +               while (bindex++ <= bbot) {
19273 +                       if (hi->hi_inode)
19274 +                               au_hiput(hi);
19275 +                       hi++;
19276 +               }
19277 +       }
19278 +       au_kfree_rcu(iinfo->ii_hinode);
19279 +       AuRwDestroy(&iinfo->ii_rwsem);
19280 +}
19281 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19282 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19283 +++ linux/fs/aufs/inode.c       2022-11-05 23:02:18.965889284 +0100
19284 @@ -0,0 +1,531 @@
19285 +// SPDX-License-Identifier: GPL-2.0
19286 +/*
19287 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19288 + *
19289 + * This program is free software; you can redistribute it and/or modify
19290 + * it under the terms of the GNU General Public License as published by
19291 + * the Free Software Foundation; either version 2 of the License, or
19292 + * (at your option) any later version.
19293 + *
19294 + * This program is distributed in the hope that it will be useful,
19295 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19296 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19297 + * GNU General Public License for more details.
19298 + *
19299 + * You should have received a copy of the GNU General Public License
19300 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19301 + */
19302 +
19303 +/*
19304 + * inode functions
19305 + */
19306 +
19307 +#include <linux/iversion.h>
19308 +#include "aufs.h"
19309 +
19310 +struct inode *au_igrab(struct inode *inode)
19311 +{
19312 +       if (inode) {
19313 +               AuDebugOn(!atomic_read(&inode->i_count));
19314 +               ihold(inode);
19315 +       }
19316 +       return inode;
19317 +}
19318 +
19319 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19320 +{
19321 +       au_cpup_attr_all(inode, /*force*/0);
19322 +       au_update_iigen(inode, /*half*/1);
19323 +       if (do_version)
19324 +               inode_inc_iversion(inode);
19325 +}
19326 +
19327 +static int au_ii_refresh(struct inode *inode, int *update)
19328 +{
19329 +       int err, e, nbr;
19330 +       umode_t type;
19331 +       aufs_bindex_t bindex, new_bindex;
19332 +       struct super_block *sb;
19333 +       struct au_iinfo *iinfo;
19334 +       struct au_hinode *p, *q, tmp;
19335 +
19336 +       AuDebugOn(au_is_bad_inode(inode));
19337 +       IiMustWriteLock(inode);
19338 +
19339 +       *update = 0;
19340 +       sb = inode->i_sb;
19341 +       nbr = au_sbbot(sb) + 1;
19342 +       type = inode->i_mode & S_IFMT;
19343 +       iinfo = au_ii(inode);
19344 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19345 +       if (unlikely(err))
19346 +               goto out;
19347 +
19348 +       AuDebugOn(iinfo->ii_btop < 0);
19349 +       p = au_hinode(iinfo, iinfo->ii_btop);
19350 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19351 +            bindex++, p++) {
19352 +               if (!p->hi_inode)
19353 +                       continue;
19354 +
19355 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19356 +               new_bindex = au_br_index(sb, p->hi_id);
19357 +               if (new_bindex == bindex)
19358 +                       continue;
19359 +
19360 +               if (new_bindex < 0) {
19361 +                       *update = 1;
19362 +                       au_hiput(p);
19363 +                       p->hi_inode = NULL;
19364 +                       continue;
19365 +               }
19366 +
19367 +               if (new_bindex < iinfo->ii_btop)
19368 +                       iinfo->ii_btop = new_bindex;
19369 +               if (iinfo->ii_bbot < new_bindex)
19370 +                       iinfo->ii_bbot = new_bindex;
19371 +               /* swap two lower inode, and loop again */
19372 +               q = au_hinode(iinfo, new_bindex);
19373 +               tmp = *q;
19374 +               *q = *p;
19375 +               *p = tmp;
19376 +               if (tmp.hi_inode) {
19377 +                       bindex--;
19378 +                       p--;
19379 +               }
19380 +       }
19381 +       au_update_ibrange(inode, /*do_put_zero*/0);
19382 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19383 +       e = au_dy_irefresh(inode);
19384 +       if (unlikely(e && !err))
19385 +               err = e;
19386 +
19387 +out:
19388 +       AuTraceErr(err);
19389 +       return err;
19390 +}
19391 +
19392 +void au_refresh_iop(struct inode *inode, int force_getattr)
19393 +{
19394 +       int type;
19395 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19396 +       const struct inode_operations *iop
19397 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19398 +
19399 +       if (inode->i_op == iop)
19400 +               return;
19401 +
19402 +       switch (inode->i_mode & S_IFMT) {
19403 +       case S_IFDIR:
19404 +               type = AuIop_DIR;
19405 +               break;
19406 +       case S_IFLNK:
19407 +               type = AuIop_SYMLINK;
19408 +               break;
19409 +       default:
19410 +               type = AuIop_OTHER;
19411 +               break;
19412 +       }
19413 +
19414 +       inode->i_op = iop + type;
19415 +       /* unnecessary smp_wmb() */
19416 +}
19417 +
19418 +int au_refresh_hinode_self(struct inode *inode)
19419 +{
19420 +       int err, update;
19421 +
19422 +       err = au_ii_refresh(inode, &update);
19423 +       if (!err)
19424 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19425 +
19426 +       AuTraceErr(err);
19427 +       return err;
19428 +}
19429 +
19430 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19431 +{
19432 +       int err, e, update;
19433 +       unsigned int flags;
19434 +       umode_t mode;
19435 +       aufs_bindex_t bindex, bbot;
19436 +       unsigned char isdir;
19437 +       struct au_hinode *p;
19438 +       struct au_iinfo *iinfo;
19439 +
19440 +       err = au_ii_refresh(inode, &update);
19441 +       if (unlikely(err))
19442 +               goto out;
19443 +
19444 +       update = 0;
19445 +       iinfo = au_ii(inode);
19446 +       p = au_hinode(iinfo, iinfo->ii_btop);
19447 +       mode = (inode->i_mode & S_IFMT);
19448 +       isdir = S_ISDIR(mode);
19449 +       flags = au_hi_flags(inode, isdir);
19450 +       bbot = au_dbbot(dentry);
19451 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19452 +               struct inode *h_i, *h_inode;
19453 +               struct dentry *h_d;
19454 +
19455 +               h_d = au_h_dptr(dentry, bindex);
19456 +               if (!h_d || d_is_negative(h_d))
19457 +                       continue;
19458 +
19459 +               h_inode = d_inode(h_d);
19460 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19461 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19462 +                       h_i = au_h_iptr(inode, bindex);
19463 +                       if (h_i) {
19464 +                               if (h_i == h_inode)
19465 +                                       continue;
19466 +                               err = -EIO;
19467 +                               break;
19468 +                       }
19469 +               }
19470 +               if (bindex < iinfo->ii_btop)
19471 +                       iinfo->ii_btop = bindex;
19472 +               if (iinfo->ii_bbot < bindex)
19473 +                       iinfo->ii_bbot = bindex;
19474 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19475 +               update = 1;
19476 +       }
19477 +       au_update_ibrange(inode, /*do_put_zero*/0);
19478 +       e = au_dy_irefresh(inode);
19479 +       if (unlikely(e && !err))
19480 +               err = e;
19481 +       if (!err)
19482 +               au_refresh_hinode_attr(inode, update && isdir);
19483 +
19484 +out:
19485 +       AuTraceErr(err);
19486 +       return err;
19487 +}
19488 +
19489 +static int set_inode(struct inode *inode, struct dentry *dentry)
19490 +{
19491 +       int err;
19492 +       unsigned int flags;
19493 +       umode_t mode;
19494 +       aufs_bindex_t bindex, btop, btail;
19495 +       unsigned char isdir;
19496 +       struct dentry *h_dentry;
19497 +       struct inode *h_inode;
19498 +       struct au_iinfo *iinfo;
19499 +       const struct inode_operations *iop;
19500 +
19501 +       IiMustWriteLock(inode);
19502 +
19503 +       err = 0;
19504 +       isdir = 0;
19505 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19506 +       btop = au_dbtop(dentry);
19507 +       h_dentry = au_h_dptr(dentry, btop);
19508 +       h_inode = d_inode(h_dentry);
19509 +       mode = h_inode->i_mode;
19510 +       switch (mode & S_IFMT) {
19511 +       case S_IFREG:
19512 +               btail = au_dbtail(dentry);
19513 +               inode->i_op = iop + AuIop_OTHER;
19514 +               inode->i_fop = &aufs_file_fop;
19515 +               err = au_dy_iaop(inode, btop, h_inode);
19516 +               if (unlikely(err))
19517 +                       goto out;
19518 +               break;
19519 +       case S_IFDIR:
19520 +               isdir = 1;
19521 +               btail = au_dbtaildir(dentry);
19522 +               inode->i_op = iop + AuIop_DIR;
19523 +               inode->i_fop = &aufs_dir_fop;
19524 +               break;
19525 +       case S_IFLNK:
19526 +               btail = au_dbtail(dentry);
19527 +               inode->i_op = iop + AuIop_SYMLINK;
19528 +               break;
19529 +       case S_IFBLK:
19530 +       case S_IFCHR:
19531 +       case S_IFIFO:
19532 +       case S_IFSOCK:
19533 +               btail = au_dbtail(dentry);
19534 +               inode->i_op = iop + AuIop_OTHER;
19535 +               init_special_inode(inode, mode, h_inode->i_rdev);
19536 +               break;
19537 +       default:
19538 +               AuIOErr("Unknown file type 0%o\n", mode);
19539 +               err = -EIO;
19540 +               goto out;
19541 +       }
19542 +
19543 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19544 +       flags = au_hi_flags(inode, isdir);
19545 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19546 +           && au_ftest_hi(flags, HNOTIFY)
19547 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19548 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19549 +               au_fclr_hi(flags, HNOTIFY);
19550 +       iinfo = au_ii(inode);
19551 +       iinfo->ii_btop = btop;
19552 +       iinfo->ii_bbot = btail;
19553 +       for (bindex = btop; bindex <= btail; bindex++) {
19554 +               h_dentry = au_h_dptr(dentry, bindex);
19555 +               if (h_dentry)
19556 +                       au_set_h_iptr(inode, bindex,
19557 +                                     au_igrab(d_inode(h_dentry)), flags);
19558 +       }
19559 +       au_cpup_attr_all(inode, /*force*/1);
19560 +       /*
19561 +        * to force calling aufs_get_acl() every time,
19562 +        * do not call cache_no_acl() for aufs inode.
19563 +        */
19564 +
19565 +out:
19566 +       return err;
19567 +}
19568 +
19569 +/*
19570 + * successful returns with iinfo write_locked
19571 + * minus: errno
19572 + * zero: success, matched
19573 + * plus: no error, but unmatched
19574 + */
19575 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19576 +{
19577 +       int err;
19578 +       unsigned int gen, igflags;
19579 +       aufs_bindex_t bindex, bbot;
19580 +       struct inode *h_inode, *h_dinode;
19581 +       struct dentry *h_dentry;
19582 +
19583 +       /*
19584 +        * before this function, if aufs got any iinfo lock, it must be only
19585 +        * one, the parent dir.
19586 +        * it can happen by UDBA and the obsoleted inode number.
19587 +        */
19588 +       err = -EIO;
19589 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19590 +               goto out;
19591 +
19592 +       err = 1;
19593 +       ii_write_lock_new_child(inode);
19594 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19595 +       h_dinode = d_inode(h_dentry);
19596 +       bbot = au_ibbot(inode);
19597 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19598 +               h_inode = au_h_iptr(inode, bindex);
19599 +               if (!h_inode || h_inode != h_dinode)
19600 +                       continue;
19601 +
19602 +               err = 0;
19603 +               gen = au_iigen(inode, &igflags);
19604 +               if (gen == au_digen(dentry)
19605 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19606 +                       break;
19607 +
19608 +               /* fully refresh inode using dentry */
19609 +               err = au_refresh_hinode(inode, dentry);
19610 +               if (!err)
19611 +                       au_update_iigen(inode, /*half*/0);
19612 +               break;
19613 +       }
19614 +
19615 +       if (unlikely(err))
19616 +               ii_write_unlock(inode);
19617 +out:
19618 +       return err;
19619 +}
19620 +
19621 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19622 +          unsigned int d_type, ino_t *ino)
19623 +{
19624 +       int err, idx;
19625 +       const int isnondir = d_type != DT_DIR;
19626 +
19627 +       /* prevent hardlinked inode number from race condition */
19628 +       if (isnondir) {
19629 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19630 +               if (unlikely(err))
19631 +                       goto out;
19632 +       }
19633 +
19634 +       err = au_xino_read(sb, bindex, h_ino, ino);
19635 +       if (unlikely(err))
19636 +               goto out_xinondir;
19637 +
19638 +       if (!*ino) {
19639 +               err = -EIO;
19640 +               *ino = au_xino_new_ino(sb);
19641 +               if (unlikely(!*ino))
19642 +                       goto out_xinondir;
19643 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19644 +               if (unlikely(err))
19645 +                       goto out_xinondir;
19646 +       }
19647 +
19648 +out_xinondir:
19649 +       if (isnondir && idx >= 0)
19650 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19651 +out:
19652 +       return err;
19653 +}
19654 +
19655 +/* successful returns with iinfo write_locked */
19656 +/* todo: return with unlocked? */
19657 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19658 +{
19659 +       struct inode *inode, *h_inode;
19660 +       struct dentry *h_dentry;
19661 +       struct super_block *sb;
19662 +       ino_t h_ino, ino;
19663 +       int err, idx, hlinked;
19664 +       aufs_bindex_t btop;
19665 +
19666 +       sb = dentry->d_sb;
19667 +       btop = au_dbtop(dentry);
19668 +       h_dentry = au_h_dptr(dentry, btop);
19669 +       h_inode = d_inode(h_dentry);
19670 +       h_ino = h_inode->i_ino;
19671 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19672 +
19673 +new_ino:
19674 +       /*
19675 +        * stop 'race'-ing between hardlinks under different
19676 +        * parents.
19677 +        */
19678 +       if (hlinked) {
19679 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19680 +               inode = ERR_PTR(err);
19681 +               if (unlikely(err))
19682 +                       goto out;
19683 +       }
19684 +
19685 +       err = au_xino_read(sb, btop, h_ino, &ino);
19686 +       inode = ERR_PTR(err);
19687 +       if (unlikely(err))
19688 +               goto out_xinondir;
19689 +
19690 +       if (!ino) {
19691 +               ino = au_xino_new_ino(sb);
19692 +               if (unlikely(!ino)) {
19693 +                       inode = ERR_PTR(-EIO);
19694 +                       goto out_xinondir;
19695 +               }
19696 +       }
19697 +
19698 +       AuDbg("i%lu\n", (unsigned long)ino);
19699 +       inode = au_iget_locked(sb, ino);
19700 +       err = PTR_ERR(inode);
19701 +       if (IS_ERR(inode))
19702 +               goto out_xinondir;
19703 +
19704 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19705 +       if (inode->i_state & I_NEW) {
19706 +               ii_write_lock_new_child(inode);
19707 +               err = set_inode(inode, dentry);
19708 +               if (!err) {
19709 +                       unlock_new_inode(inode);
19710 +                       goto out_xinondir; /* success */
19711 +               }
19712 +
19713 +               /*
19714 +                * iget_failed() calls iput(), but we need to call
19715 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19716 +                * i_count.
19717 +                */
19718 +               atomic_inc(&inode->i_count);
19719 +               iget_failed(inode);
19720 +               ii_write_unlock(inode);
19721 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19722 +               /* ignore this error */
19723 +               goto out_iput;
19724 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19725 +               /*
19726 +                * horrible race condition between lookup, readdir and copyup
19727 +                * (or something).
19728 +                */
19729 +               if (hlinked && idx >= 0)
19730 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19731 +               err = reval_inode(inode, dentry);
19732 +               if (unlikely(err < 0)) {
19733 +                       hlinked = 0;
19734 +                       goto out_iput;
19735 +               }
19736 +               if (!err)
19737 +                       goto out; /* success */
19738 +               else if (hlinked && idx >= 0) {
19739 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19740 +                       if (unlikely(err)) {
19741 +                               iput(inode);
19742 +                               inode = ERR_PTR(err);
19743 +                               goto out;
19744 +                       }
19745 +               }
19746 +       }
19747 +
19748 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19749 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19750 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19751 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19752 +                       (unsigned long)h_ino, (unsigned long)ino);
19753 +       ino = 0;
19754 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19755 +       if (!err) {
19756 +               iput(inode);
19757 +               if (hlinked && idx >= 0)
19758 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19759 +               goto new_ino;
19760 +       }
19761 +
19762 +out_iput:
19763 +       iput(inode);
19764 +       inode = ERR_PTR(err);
19765 +out_xinondir:
19766 +       if (hlinked && idx >= 0)
19767 +               au_xinondir_leave(sb, btop, h_ino, idx);
19768 +out:
19769 +       return inode;
19770 +}
19771 +
19772 +/* ---------------------------------------------------------------------- */
19773 +
19774 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19775 +              struct inode *inode)
19776 +{
19777 +       int err;
19778 +       struct inode *hi;
19779 +
19780 +       err = au_br_rdonly(au_sbr(sb, bindex));
19781 +
19782 +       /* pseudo-link after flushed may happen out of bounds */
19783 +       if (!err
19784 +           && inode
19785 +           && au_ibtop(inode) <= bindex
19786 +           && bindex <= au_ibbot(inode)) {
19787 +               /*
19788 +                * permission check is unnecessary since vfsub routine
19789 +                * will be called later
19790 +                */
19791 +               hi = au_h_iptr(inode, bindex);
19792 +               if (hi)
19793 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19794 +       }
19795 +
19796 +       return err;
19797 +}
19798 +
19799 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19800 +                  int mask)
19801 +{
19802 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19803 +               return 0;
19804 +       return inode_permission(h_userns, h_inode, mask);
19805 +}
19806 +
19807 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19808 +                      int mask)
19809 +{
19810 +       if (au_test_nfs(h_inode->i_sb)
19811 +           && (mask & MAY_WRITE)
19812 +           && S_ISDIR(h_inode->i_mode))
19813 +               mask |= MAY_READ; /* force permission check */
19814 +       return au_test_h_perm(h_userns, h_inode, mask);
19815 +}
19816 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19817 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19818 +++ linux/fs/aufs/inode.h       2022-12-17 09:21:34.799855195 +0100
19819 @@ -0,0 +1,705 @@
19820 +/* SPDX-License-Identifier: GPL-2.0 */
19821 +/*
19822 + * Copyright (C) 2005-2022 Junjiro R. Okajima
19823 + *
19824 + * This program is free software; you can redistribute it and/or modify
19825 + * it under the terms of the GNU General Public License as published by
19826 + * the Free Software Foundation; either version 2 of the License, or
19827 + * (at your option) any later version.
19828 + *
19829 + * This program is distributed in the hope that it will be useful,
19830 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19831 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19832 + * GNU General Public License for more details.
19833 + *
19834 + * You should have received a copy of the GNU General Public License
19835 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19836 + */
19837 +
19838 +/*
19839 + * inode operations
19840 + */
19841 +
19842 +#ifndef __AUFS_INODE_H__
19843 +#define __AUFS_INODE_H__
19844 +
19845 +#ifdef __KERNEL__
19846 +
19847 +#include <linux/fsnotify.h>
19848 +#include "rwsem.h"
19849 +
19850 +struct vfsmount;
19851 +
19852 +struct au_hnotify {
19853 +#ifdef CONFIG_AUFS_HNOTIFY
19854 +#ifdef CONFIG_AUFS_HFSNOTIFY
19855 +       /* never use fsnotify_add_vfsmount_mark() */
19856 +       struct fsnotify_mark            hn_mark;
19857 +#endif
19858 +       struct inode            *hn_aufs_inode; /* no get/put */
19859 +       struct rcu_head         rcu;
19860 +#endif
19861 +} ____cacheline_aligned_in_smp;
19862 +
19863 +struct au_hinode {
19864 +       struct inode            *hi_inode;
19865 +       aufs_bindex_t           hi_id;
19866 +#ifdef CONFIG_AUFS_HNOTIFY
19867 +       struct au_hnotify       *hi_notify;
19868 +#endif
19869 +
19870 +       /* reference to the copied-up whiteout with get/put */
19871 +       struct dentry           *hi_whdentry;
19872 +};
19873 +
19874 +/* ig_flags */
19875 +#define AuIG_HALF_REFRESHED            1
19876 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19877 +#define au_ig_fset(flags, name) \
19878 +       do { (flags) |= AuIG_##name; } while (0)
19879 +#define au_ig_fclr(flags, name) \
19880 +       do { (flags) &= ~AuIG_##name; } while (0)
19881 +
19882 +struct au_iigen {
19883 +       spinlock_t      ig_spin;
19884 +       __u32           ig_generation, ig_flags;
19885 +};
19886 +
19887 +struct au_vdir;
19888 +struct au_iinfo {
19889 +       struct au_iigen         ii_generation;
19890 +       struct super_block      *ii_hsb1;       /* no get/put */
19891 +
19892 +       struct au_rwsem         ii_rwsem;
19893 +       aufs_bindex_t           ii_btop, ii_bbot;
19894 +       __u32                   ii_higen;
19895 +       struct au_hinode        *ii_hinode;
19896 +       struct au_vdir          *ii_vdir;
19897 +};
19898 +
19899 +struct au_icntnr {
19900 +       struct au_iinfo         iinfo;
19901 +       struct inode            vfs_inode;
19902 +       struct hlist_bl_node    plink;
19903 +       struct rcu_head         rcu;
19904 +} ____cacheline_aligned_in_smp;
19905 +
19906 +/* au_pin flags */
19907 +#define AuPin_DI_LOCKED                1
19908 +#define AuPin_MNT_WRITE                (1 << 1)
19909 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19910 +#define au_fset_pin(flags, name) \
19911 +       do { (flags) |= AuPin_##name; } while (0)
19912 +#define au_fclr_pin(flags, name) \
19913 +       do { (flags) &= ~AuPin_##name; } while (0)
19914 +
19915 +struct au_pin {
19916 +       /* input */
19917 +       struct dentry *dentry;
19918 +       unsigned int udba;
19919 +       unsigned char lsc_di, lsc_hi, flags;
19920 +       aufs_bindex_t bindex;
19921 +
19922 +       /* output */
19923 +       struct dentry *parent;
19924 +       struct au_hinode *hdir;
19925 +       struct vfsmount *h_mnt;
19926 +
19927 +       /* temporary unlock/relock for copyup */
19928 +       struct dentry *h_dentry, *h_parent;
19929 +       struct au_branch *br;
19930 +       struct task_struct *task;
19931 +};
19932 +
19933 +void au_pin_hdir_unlock(struct au_pin *p);
19934 +int au_pin_hdir_lock(struct au_pin *p);
19935 +int au_pin_hdir_relock(struct au_pin *p);
19936 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19937 +void au_pin_hdir_release(struct au_pin *p);
19938 +
19939 +/* ---------------------------------------------------------------------- */
19940 +
19941 +static inline struct au_iinfo *au_ii(struct inode *inode)
19942 +{
19943 +       BUG_ON(is_bad_inode(inode));
19944 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19945 +}
19946 +
19947 +/* ---------------------------------------------------------------------- */
19948 +
19949 +/* inode.c */
19950 +struct inode *au_igrab(struct inode *inode);
19951 +void au_refresh_iop(struct inode *inode, int force_getattr);
19952 +int au_refresh_hinode_self(struct inode *inode);
19953 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19954 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19955 +          unsigned int d_type, ino_t *ino);
19956 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19957 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19958 +              struct inode *inode);
19959 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19960 +                  int mask);
19961 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19962 +                      int mask);
19963 +
19964 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19965 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19966 +{
19967 +#ifdef CONFIG_AUFS_SHWH
19968 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19969 +#else
19970 +       return 0;
19971 +#endif
19972 +}
19973 +
19974 +/* i_op.c */
19975 +enum {
19976 +       AuIop_SYMLINK,
19977 +       AuIop_DIR,
19978 +       AuIop_OTHER,
19979 +       AuIop_Last
19980 +};
19981 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19982 +       aufs_iop_nogetattr[AuIop_Last];
19983 +
19984 +/* au_wr_dir flags */
19985 +#define AuWrDir_ADD_ENTRY      1
19986 +#define AuWrDir_ISDIR          (1 << 1)
19987 +#define AuWrDir_TMPFILE                (1 << 2)
19988 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19989 +#define au_fset_wrdir(flags, name) \
19990 +       do { (flags) |= AuWrDir_##name; } while (0)
19991 +#define au_fclr_wrdir(flags, name) \
19992 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19993 +
19994 +struct au_wr_dir_args {
19995 +       aufs_bindex_t force_btgt;
19996 +       unsigned char flags;
19997 +};
19998 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19999 +             struct au_wr_dir_args *args);
20000 +
20001 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
20002 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
20003 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
20004 +                unsigned int udba, unsigned char flags);
20005 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
20006 +          unsigned int udba, unsigned char flags) __must_check;
20007 +int au_do_pin(struct au_pin *pin) __must_check;
20008 +void au_unpin(struct au_pin *pin);
20009 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
20010 +
20011 +#define AuIcpup_DID_CPUP       1
20012 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
20013 +#define au_fset_icpup(flags, name) \
20014 +       do { (flags) |= AuIcpup_##name; } while (0)
20015 +#define au_fclr_icpup(flags, name) \
20016 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20017 +
20018 +struct au_icpup_args {
20019 +       unsigned char flags;
20020 +       unsigned char pin_flags;
20021 +       aufs_bindex_t btgt;
20022 +       unsigned int udba;
20023 +       struct au_pin pin;
20024 +       struct path h_path;
20025 +       struct inode *h_inode;
20026 +};
20027 +
20028 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20029 +                    struct au_icpup_args *a);
20030 +
20031 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20032 +                     struct path *h_path, int locked);
20033 +
20034 +/* i_op_add.c */
20035 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20036 +              struct dentry *h_parent, int isdir);
20037 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
20038 +              struct dentry *dentry, umode_t mode, dev_t dev);
20039 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
20040 +                struct dentry *dentry, const char *symname);
20041 +int aufs_create(struct user_namespace *userns, struct inode *dir,
20042 +               struct dentry *dentry, umode_t mode, bool want_excl);
20043 +struct vfsub_aopen_args;
20044 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20045 +                      struct vfsub_aopen_args *args);
20046 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
20047 +                struct file *file, umode_t mode);
20048 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20049 +             struct dentry *dentry);
20050 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20051 +              struct dentry *dentry, umode_t mode);
20052 +
20053 +/* i_op_del.c */
20054 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20055 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20056 +              struct dentry *h_parent, int isdir);
20057 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20058 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20059 +
20060 +/* i_op_ren.c */
20061 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20062 +int aufs_rename(struct user_namespace *userns,
20063 +               struct inode *_src_dir, struct dentry *_src_dentry,
20064 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20065 +               unsigned int _flags);
20066 +
20067 +/* iinfo.c */
20068 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20069 +void au_hiput(struct au_hinode *hinode);
20070 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20071 +                 struct dentry *h_wh);
20072 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20073 +
20074 +/* hinode flags */
20075 +#define AuHi_XINO      1
20076 +#define AuHi_HNOTIFY   (1 << 1)
20077 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20078 +#define au_fset_hi(flags, name) \
20079 +       do { (flags) |= AuHi_##name; } while (0)
20080 +#define au_fclr_hi(flags, name) \
20081 +       do { (flags) &= ~AuHi_##name; } while (0)
20082 +
20083 +#ifndef CONFIG_AUFS_HNOTIFY
20084 +#undef AuHi_HNOTIFY
20085 +#define AuHi_HNOTIFY   0
20086 +#endif
20087 +
20088 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20089 +                  struct inode *h_inode, unsigned int flags);
20090 +
20091 +void au_update_iigen(struct inode *inode, int half);
20092 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20093 +
20094 +void au_icntnr_init_once(void *_c);
20095 +void au_hinode_init(struct au_hinode *hinode);
20096 +int au_iinfo_init(struct inode *inode);
20097 +void au_iinfo_fin(struct inode *inode);
20098 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20099 +
20100 +#ifdef CONFIG_PROC_FS
20101 +/* plink.c */
20102 +int au_plink_maint(struct super_block *sb, int flags);
20103 +struct au_sbinfo;
20104 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20105 +int au_plink_maint_enter(struct super_block *sb);
20106 +#ifdef CONFIG_AUFS_DEBUG
20107 +void au_plink_list(struct super_block *sb);
20108 +#else
20109 +AuStubVoid(au_plink_list, struct super_block *sb)
20110 +#endif
20111 +int au_plink_test(struct inode *inode);
20112 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20113 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20114 +                    struct dentry *h_dentry);
20115 +void au_plink_put(struct super_block *sb, int verbose);
20116 +void au_plink_clean(struct super_block *sb, int verbose);
20117 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20118 +#else
20119 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20120 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20121 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20122 +AuStubVoid(au_plink_list, struct super_block *sb);
20123 +AuStubInt0(au_plink_test, struct inode *inode);
20124 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20125 +       struct inode *inode, aufs_bindex_t bindex);
20126 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20127 +          struct dentry *h_dentry);
20128 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20129 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20130 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20131 +#endif /* CONFIG_PROC_FS */
20132 +
20133 +#ifdef CONFIG_AUFS_XATTR
20134 +/* xattr.c */
20135 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20136 +                 unsigned int verbose);
20137 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20138 +void au_xattr_init(struct super_block *sb);
20139 +#else
20140 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20141 +          int ignore_flags, unsigned int verbose);
20142 +AuStubVoid(au_xattr_init, struct super_block *sb);
20143 +#endif
20144 +
20145 +#ifdef CONFIG_FS_POSIX_ACL
20146 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
20147 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
20148 +                struct posix_acl *acl, int type);
20149 +#endif
20150 +
20151 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20152 +enum {
20153 +       AU_XATTR_SET,
20154 +       AU_ACL_SET
20155 +};
20156 +
20157 +struct au_sxattr {
20158 +       int type;
20159 +       union {
20160 +               struct {
20161 +                       const char      *name;
20162 +                       const void      *value;
20163 +                       size_t          size;
20164 +                       int             flags;
20165 +               } set;
20166 +               struct {
20167 +                       struct posix_acl *acl;
20168 +                       int             type;
20169 +               } acl_set;
20170 +       } u;
20171 +};
20172 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20173 +                 struct au_sxattr *arg);
20174 +#endif
20175 +
20176 +/* ---------------------------------------------------------------------- */
20177 +
20178 +/* lock subclass for iinfo */
20179 +enum {
20180 +       AuLsc_II_CHILD,         /* child first */
20181 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20182 +       AuLsc_II_CHILD3,        /* copyup dirs */
20183 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20184 +       AuLsc_II_PARENT2,
20185 +       AuLsc_II_PARENT3,       /* copyup dirs */
20186 +       AuLsc_II_NEW_CHILD
20187 +};
20188 +
20189 +/*
20190 + * ii_read_lock_child, ii_write_lock_child,
20191 + * ii_read_lock_child2, ii_write_lock_child2,
20192 + * ii_read_lock_child3, ii_write_lock_child3,
20193 + * ii_read_lock_parent, ii_write_lock_parent,
20194 + * ii_read_lock_parent2, ii_write_lock_parent2,
20195 + * ii_read_lock_parent3, ii_write_lock_parent3,
20196 + * ii_read_lock_new_child, ii_write_lock_new_child,
20197 + */
20198 +#define AuReadLockFunc(name, lsc) \
20199 +static inline void ii_read_lock_##name(struct inode *i) \
20200 +{ \
20201 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20202 +}
20203 +
20204 +#define AuWriteLockFunc(name, lsc) \
20205 +static inline void ii_write_lock_##name(struct inode *i) \
20206 +{ \
20207 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20208 +}
20209 +
20210 +#define AuRWLockFuncs(name, lsc) \
20211 +       AuReadLockFunc(name, lsc) \
20212 +       AuWriteLockFunc(name, lsc)
20213 +
20214 +AuRWLockFuncs(child, CHILD);
20215 +AuRWLockFuncs(child2, CHILD2);
20216 +AuRWLockFuncs(child3, CHILD3);
20217 +AuRWLockFuncs(parent, PARENT);
20218 +AuRWLockFuncs(parent2, PARENT2);
20219 +AuRWLockFuncs(parent3, PARENT3);
20220 +AuRWLockFuncs(new_child, NEW_CHILD);
20221 +
20222 +#undef AuReadLockFunc
20223 +#undef AuWriteLockFunc
20224 +#undef AuRWLockFuncs
20225 +
20226 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20227 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20228 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20229 +
20230 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20231 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20232 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20233 +
20234 +/* ---------------------------------------------------------------------- */
20235 +
20236 +static inline void au_icntnr_init(struct au_icntnr *c)
20237 +{
20238 +#ifdef CONFIG_AUFS_DEBUG
20239 +       c->vfs_inode.i_mode = 0;
20240 +#endif
20241 +}
20242 +
20243 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20244 +{
20245 +       unsigned int gen;
20246 +       struct au_iinfo *iinfo;
20247 +       struct au_iigen *iigen;
20248 +
20249 +       iinfo = au_ii(inode);
20250 +       iigen = &iinfo->ii_generation;
20251 +       spin_lock(&iigen->ig_spin);
20252 +       if (igflags)
20253 +               *igflags = iigen->ig_flags;
20254 +       gen = iigen->ig_generation;
20255 +       spin_unlock(&iigen->ig_spin);
20256 +
20257 +       return gen;
20258 +}
20259 +
20260 +/* tiny test for inode number */
20261 +/* tmpfs generation is too rough */
20262 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20263 +{
20264 +       struct au_iinfo *iinfo;
20265 +
20266 +       iinfo = au_ii(inode);
20267 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20268 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20269 +                && iinfo->ii_higen == h_inode->i_generation);
20270 +}
20271 +
20272 +static inline void au_iigen_dec(struct inode *inode)
20273 +{
20274 +       struct au_iinfo *iinfo;
20275 +       struct au_iigen *iigen;
20276 +
20277 +       iinfo = au_ii(inode);
20278 +       iigen = &iinfo->ii_generation;
20279 +       spin_lock(&iigen->ig_spin);
20280 +       iigen->ig_generation--;
20281 +       spin_unlock(&iigen->ig_spin);
20282 +}
20283 +
20284 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20285 +{
20286 +       int err;
20287 +
20288 +       err = 0;
20289 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20290 +               err = -EIO;
20291 +
20292 +       return err;
20293 +}
20294 +
20295 +/* ---------------------------------------------------------------------- */
20296 +
20297 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20298 +                                         aufs_bindex_t bindex)
20299 +{
20300 +       return iinfo->ii_hinode + bindex;
20301 +}
20302 +
20303 +static inline int au_is_bad_inode(struct inode *inode)
20304 +{
20305 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20306 +}
20307 +
20308 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20309 +                                       aufs_bindex_t bindex)
20310 +{
20311 +       IiMustAnyLock(inode);
20312 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20313 +}
20314 +
20315 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20316 +{
20317 +       IiMustAnyLock(inode);
20318 +       return au_ii(inode)->ii_btop;
20319 +}
20320 +
20321 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20322 +{
20323 +       IiMustAnyLock(inode);
20324 +       return au_ii(inode)->ii_bbot;
20325 +}
20326 +
20327 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20328 +{
20329 +       IiMustAnyLock(inode);
20330 +       return au_ii(inode)->ii_vdir;
20331 +}
20332 +
20333 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20334 +{
20335 +       IiMustAnyLock(inode);
20336 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20337 +}
20338 +
20339 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20340 +{
20341 +       IiMustWriteLock(inode);
20342 +       au_ii(inode)->ii_btop = bindex;
20343 +}
20344 +
20345 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20346 +{
20347 +       IiMustWriteLock(inode);
20348 +       au_ii(inode)->ii_bbot = bindex;
20349 +}
20350 +
20351 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20352 +{
20353 +       IiMustWriteLock(inode);
20354 +       au_ii(inode)->ii_vdir = vdir;
20355 +}
20356 +
20357 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20358 +{
20359 +       IiMustAnyLock(inode);
20360 +       return au_hinode(au_ii(inode), bindex);
20361 +}
20362 +
20363 +/* ---------------------------------------------------------------------- */
20364 +
20365 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20366 +{
20367 +       if (pin)
20368 +               return pin->parent;
20369 +       return NULL;
20370 +}
20371 +
20372 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20373 +{
20374 +       if (pin && pin->hdir)
20375 +               return pin->hdir->hi_inode;
20376 +       return NULL;
20377 +}
20378 +
20379 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20380 +{
20381 +       if (pin)
20382 +               return pin->hdir;
20383 +       return NULL;
20384 +}
20385 +
20386 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20387 +{
20388 +       if (pin)
20389 +               pin->dentry = dentry;
20390 +}
20391 +
20392 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20393 +                                          unsigned char lflag)
20394 +{
20395 +       if (pin) {
20396 +               if (lflag)
20397 +                       au_fset_pin(pin->flags, DI_LOCKED);
20398 +               else
20399 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20400 +       }
20401 +}
20402 +
20403 +#if 0 /* reserved */
20404 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20405 +{
20406 +       if (pin) {
20407 +               dput(pin->parent);
20408 +               pin->parent = dget(parent);
20409 +       }
20410 +}
20411 +#endif
20412 +
20413 +/* ---------------------------------------------------------------------- */
20414 +
20415 +struct au_branch;
20416 +#ifdef CONFIG_AUFS_HNOTIFY
20417 +struct au_hnotify_op {
20418 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20419 +       int (*alloc)(struct au_hinode *hinode);
20420 +
20421 +       /*
20422 +        * if it returns true, the caller should free hinode->hi_notify,
20423 +        * otherwise ->free() frees it.
20424 +        */
20425 +       int (*free)(struct au_hinode *hinode,
20426 +                   struct au_hnotify *hn) __must_check;
20427 +
20428 +       void (*fin)(void);
20429 +       int (*init)(void);
20430 +
20431 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20432 +       void (*fin_br)(struct au_branch *br);
20433 +       int (*init_br)(struct au_branch *br, int perm);
20434 +};
20435 +
20436 +/* hnotify.c */
20437 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20438 +void au_hn_free(struct au_hinode *hinode);
20439 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20440 +void au_hn_reset(struct inode *inode, unsigned int flags);
20441 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20442 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20443 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20444 +int au_hnotify_init_br(struct au_branch *br, int perm);
20445 +void au_hnotify_fin_br(struct au_branch *br);
20446 +int __init au_hnotify_init(void);
20447 +void au_hnotify_fin(void);
20448 +
20449 +/* hfsnotify.c */
20450 +extern const struct au_hnotify_op au_hnotify_op;
20451 +
20452 +static inline
20453 +void au_hn_init(struct au_hinode *hinode)
20454 +{
20455 +       hinode->hi_notify = NULL;
20456 +}
20457 +
20458 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20459 +{
20460 +       return hinode->hi_notify;
20461 +}
20462 +
20463 +#else
20464 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20465 +       struct au_hinode *hinode __maybe_unused,
20466 +       struct inode *inode __maybe_unused)
20467 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20468 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20469 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20470 +          int do_set __maybe_unused)
20471 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20472 +          unsigned int flags __maybe_unused)
20473 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20474 +          struct au_branch *br __maybe_unused,
20475 +          int perm __maybe_unused)
20476 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20477 +          int perm __maybe_unused)
20478 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20479 +AuStubInt0(__init au_hnotify_init, void)
20480 +AuStubVoid(au_hnotify_fin, void)
20481 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20482 +#endif /* CONFIG_AUFS_HNOTIFY */
20483 +
20484 +static inline void au_hn_suspend(struct au_hinode *hdir)
20485 +{
20486 +       au_hn_ctl(hdir, /*do_set*/0);
20487 +}
20488 +
20489 +static inline void au_hn_resume(struct au_hinode *hdir)
20490 +{
20491 +       au_hn_ctl(hdir, /*do_set*/1);
20492 +}
20493 +
20494 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20495 +{
20496 +       inode_lock(hdir->hi_inode);
20497 +       au_hn_suspend(hdir);
20498 +}
20499 +
20500 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20501 +                                         unsigned int sc __maybe_unused)
20502 +{
20503 +       inode_lock_nested(hdir->hi_inode, sc);
20504 +       au_hn_suspend(hdir);
20505 +}
20506 +
20507 +#if 0 /* unused */
20508 +#include "vfsub.h"
20509 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20510 +                                                 unsigned int sc)
20511 +{
20512 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20513 +       au_hn_suspend(hdir);
20514 +}
20515 +#endif
20516 +
20517 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20518 +{
20519 +       au_hn_resume(hdir);
20520 +       inode_unlock(hdir->hi_inode);
20521 +}
20522 +
20523 +#endif /* __KERNEL__ */
20524 +#endif /* __AUFS_INODE_H__ */
20525 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20526 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20527 +++ linux/fs/aufs/ioctl.c       2022-11-05 23:02:18.965889284 +0100
20528 @@ -0,0 +1,220 @@
20529 +// SPDX-License-Identifier: GPL-2.0
20530 +/*
20531 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20532 + *
20533 + * This program is free software; you can redistribute it and/or modify
20534 + * it under the terms of the GNU General Public License as published by
20535 + * the Free Software Foundation; either version 2 of the License, or
20536 + * (at your option) any later version.
20537 + *
20538 + * This program is distributed in the hope that it will be useful,
20539 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20540 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20541 + * GNU General Public License for more details.
20542 + *
20543 + * You should have received a copy of the GNU General Public License
20544 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20545 + */
20546 +
20547 +/*
20548 + * ioctl
20549 + * plink-management and readdir in userspace.
20550 + * assist the pathconf(3) wrapper library.
20551 + * move-down
20552 + * File-based Hierarchical Storage Management.
20553 + */
20554 +
20555 +#include <linux/compat.h>
20556 +#include <linux/file.h>
20557 +#include "aufs.h"
20558 +
20559 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20560 +{
20561 +       int err, fd;
20562 +       aufs_bindex_t wbi, bindex, bbot;
20563 +       struct file *h_file;
20564 +       struct super_block *sb;
20565 +       struct dentry *root;
20566 +       struct au_branch *br;
20567 +       struct aufs_wbr_fd wbrfd = {
20568 +               .oflags = au_dir_roflags,
20569 +               .brid   = -1
20570 +       };
20571 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20572 +               | O_NOATIME | O_CLOEXEC;
20573 +
20574 +       AuDebugOn(wbrfd.oflags & ~valid);
20575 +
20576 +       if (arg) {
20577 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20578 +               if (unlikely(err)) {
20579 +                       err = -EFAULT;
20580 +                       goto out;
20581 +               }
20582 +
20583 +               err = -EINVAL;
20584 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20585 +               wbrfd.oflags |= au_dir_roflags;
20586 +               AuDbg("0%o\n", wbrfd.oflags);
20587 +               if (unlikely(wbrfd.oflags & ~valid))
20588 +                       goto out;
20589 +       }
20590 +
20591 +       fd = get_unused_fd_flags(0);
20592 +       err = fd;
20593 +       if (unlikely(fd < 0))
20594 +               goto out;
20595 +
20596 +       h_file = ERR_PTR(-EINVAL);
20597 +       wbi = 0;
20598 +       br = NULL;
20599 +       sb = path->dentry->d_sb;
20600 +       root = sb->s_root;
20601 +       aufs_read_lock(root, AuLock_IR);
20602 +       bbot = au_sbbot(sb);
20603 +       if (wbrfd.brid >= 0) {
20604 +               wbi = au_br_index(sb, wbrfd.brid);
20605 +               if (unlikely(wbi < 0 || wbi > bbot))
20606 +                       goto out_unlock;
20607 +       }
20608 +
20609 +       h_file = ERR_PTR(-ENOENT);
20610 +       br = au_sbr(sb, wbi);
20611 +       if (!au_br_writable(br->br_perm)) {
20612 +               if (arg)
20613 +                       goto out_unlock;
20614 +
20615 +               bindex = wbi + 1;
20616 +               wbi = -1;
20617 +               for (; bindex <= bbot; bindex++) {
20618 +                       br = au_sbr(sb, bindex);
20619 +                       if (au_br_writable(br->br_perm)) {
20620 +                               wbi = bindex;
20621 +                               br = au_sbr(sb, wbi);
20622 +                               break;
20623 +                       }
20624 +               }
20625 +       }
20626 +       AuDbg("wbi %d\n", wbi);
20627 +       if (wbi >= 0)
20628 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20629 +                                  /*force_wr*/0);
20630 +
20631 +out_unlock:
20632 +       aufs_read_unlock(root, AuLock_IR);
20633 +       err = PTR_ERR(h_file);
20634 +       if (IS_ERR(h_file))
20635 +               goto out_fd;
20636 +
20637 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20638 +       fd_install(fd, h_file);
20639 +       err = fd;
20640 +       goto out; /* success */
20641 +
20642 +out_fd:
20643 +       put_unused_fd(fd);
20644 +out:
20645 +       AuTraceErr(err);
20646 +       return err;
20647 +}
20648 +
20649 +/* ---------------------------------------------------------------------- */
20650 +
20651 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20652 +{
20653 +       long err;
20654 +       struct dentry *dentry;
20655 +
20656 +       switch (cmd) {
20657 +       case AUFS_CTL_RDU:
20658 +       case AUFS_CTL_RDU_INO:
20659 +               err = au_rdu_ioctl(file, cmd, arg);
20660 +               break;
20661 +
20662 +       case AUFS_CTL_WBR_FD:
20663 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20664 +               break;
20665 +
20666 +       case AUFS_CTL_IBUSY:
20667 +               err = au_ibusy_ioctl(file, arg);
20668 +               break;
20669 +
20670 +       case AUFS_CTL_BRINFO:
20671 +               err = au_brinfo_ioctl(file, arg);
20672 +               break;
20673 +
20674 +       case AUFS_CTL_FHSM_FD:
20675 +               dentry = file->f_path.dentry;
20676 +               if (IS_ROOT(dentry))
20677 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20678 +               else
20679 +                       err = -ENOTTY;
20680 +               break;
20681 +
20682 +       default:
20683 +               /* do not call the lower */
20684 +               AuDbg("0x%x\n", cmd);
20685 +               err = -ENOTTY;
20686 +       }
20687 +
20688 +       AuTraceErr(err);
20689 +       return err;
20690 +}
20691 +
20692 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20693 +{
20694 +       long err;
20695 +
20696 +       switch (cmd) {
20697 +       case AUFS_CTL_MVDOWN:
20698 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20699 +               break;
20700 +
20701 +       case AUFS_CTL_WBR_FD:
20702 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20703 +               break;
20704 +
20705 +       default:
20706 +               /* do not call the lower */
20707 +               AuDbg("0x%x\n", cmd);
20708 +               err = -ENOTTY;
20709 +       }
20710 +
20711 +       AuTraceErr(err);
20712 +       return err;
20713 +}
20714 +
20715 +#ifdef CONFIG_COMPAT
20716 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20717 +                          unsigned long arg)
20718 +{
20719 +       long err;
20720 +
20721 +       switch (cmd) {
20722 +       case AUFS_CTL_RDU:
20723 +       case AUFS_CTL_RDU_INO:
20724 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20725 +               break;
20726 +
20727 +       case AUFS_CTL_IBUSY:
20728 +               err = au_ibusy_compat_ioctl(file, arg);
20729 +               break;
20730 +
20731 +       case AUFS_CTL_BRINFO:
20732 +               err = au_brinfo_compat_ioctl(file, arg);
20733 +               break;
20734 +
20735 +       default:
20736 +               err = aufs_ioctl_dir(file, cmd, arg);
20737 +       }
20738 +
20739 +       AuTraceErr(err);
20740 +       return err;
20741 +}
20742 +
20743 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20744 +                             unsigned long arg)
20745 +{
20746 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20747 +}
20748 +#endif
20749 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20750 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20751 +++ linux/fs/aufs/i_op_add.c    2022-12-17 09:21:34.799855195 +0100
20752 @@ -0,0 +1,972 @@
20753 +// SPDX-License-Identifier: GPL-2.0
20754 +/*
20755 + * Copyright (C) 2005-2022 Junjiro R. Okajima
20756 + *
20757 + * This program is free software; you can redistribute it and/or modify
20758 + * it under the terms of the GNU General Public License as published by
20759 + * the Free Software Foundation; either version 2 of the License, or
20760 + * (at your option) any later version.
20761 + *
20762 + * This program is distributed in the hope that it will be useful,
20763 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20764 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20765 + * GNU General Public License for more details.
20766 + *
20767 + * You should have received a copy of the GNU General Public License
20768 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20769 + */
20770 +
20771 +/*
20772 + * inode operations (add entry)
20773 + */
20774 +
20775 +#include <linux/iversion.h>
20776 +#include "aufs.h"
20777 +
20778 +/*
20779 + * final procedure of adding a new entry, except link(2).
20780 + * remove whiteout, instantiate, copyup the parent dir's times and size
20781 + * and update version.
20782 + * if it failed, re-create the removed whiteout.
20783 + */
20784 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20785 +                 struct dentry *wh_dentry, struct dentry *dentry)
20786 +{
20787 +       int err, rerr;
20788 +       aufs_bindex_t bwh;
20789 +       struct path h_path;
20790 +       struct super_block *sb;
20791 +       struct inode *inode, *h_dir;
20792 +       struct dentry *wh;
20793 +
20794 +       bwh = -1;
20795 +       sb = dir->i_sb;
20796 +       if (wh_dentry) {
20797 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20798 +               IMustLock(h_dir);
20799 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20800 +               bwh = au_dbwh(dentry);
20801 +               h_path.dentry = wh_dentry;
20802 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20803 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20804 +                                         dentry);
20805 +               if (unlikely(err))
20806 +                       goto out;
20807 +       }
20808 +
20809 +       inode = au_new_inode(dentry, /*must_new*/1);
20810 +       if (!IS_ERR(inode)) {
20811 +               d_instantiate(dentry, inode);
20812 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20813 +               IMustLock(dir);
20814 +               au_dir_ts(dir, bindex);
20815 +               inode_inc_iversion(dir);
20816 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20817 +               return 0; /* success */
20818 +       }
20819 +
20820 +       err = PTR_ERR(inode);
20821 +       if (!wh_dentry)
20822 +               goto out;
20823 +
20824 +       /* revert */
20825 +       /* dir inode is locked */
20826 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20827 +       rerr = PTR_ERR(wh);
20828 +       if (IS_ERR(wh)) {
20829 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20830 +                       dentry, err, rerr);
20831 +               err = -EIO;
20832 +       } else
20833 +               dput(wh);
20834 +
20835 +out:
20836 +       return err;
20837 +}
20838 +
20839 +static int au_d_may_add(struct dentry *dentry)
20840 +{
20841 +       int err;
20842 +
20843 +       err = 0;
20844 +       if (unlikely(d_unhashed(dentry)))
20845 +               err = -ENOENT;
20846 +       if (unlikely(d_really_is_positive(dentry)))
20847 +               err = -EEXIST;
20848 +       return err;
20849 +}
20850 +
20851 +/*
20852 + * simple tests for the adding inode operations.
20853 + * following the checks in vfs, plus the parent-child relationship.
20854 + */
20855 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20856 +              struct dentry *h_parent, int isdir)
20857 +{
20858 +       int err;
20859 +       umode_t h_mode;
20860 +       struct dentry *h_dentry;
20861 +       struct inode *h_inode;
20862 +
20863 +       err = -ENAMETOOLONG;
20864 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20865 +               goto out;
20866 +
20867 +       h_dentry = au_h_dptr(dentry, bindex);
20868 +       if (d_really_is_negative(dentry)) {
20869 +               err = -EEXIST;
20870 +               if (unlikely(d_is_positive(h_dentry)))
20871 +                       goto out;
20872 +       } else {
20873 +               /* rename(2) case */
20874 +               err = -EIO;
20875 +               if (unlikely(d_is_negative(h_dentry)))
20876 +                       goto out;
20877 +               h_inode = d_inode(h_dentry);
20878 +               if (unlikely(!h_inode->i_nlink))
20879 +                       goto out;
20880 +
20881 +               h_mode = h_inode->i_mode;
20882 +               if (!isdir) {
20883 +                       err = -EISDIR;
20884 +                       if (unlikely(S_ISDIR(h_mode)))
20885 +                               goto out;
20886 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20887 +                       err = -ENOTDIR;
20888 +                       goto out;
20889 +               }
20890 +       }
20891 +
20892 +       err = 0;
20893 +       /* expected parent dir is locked */
20894 +       if (unlikely(h_parent != h_dentry->d_parent))
20895 +               err = -EIO;
20896 +
20897 +out:
20898 +       AuTraceErr(err);
20899 +       return err;
20900 +}
20901 +
20902 +/*
20903 + * initial procedure of adding a new entry.
20904 + * prepare writable branch and the parent dir, lock it,
20905 + * and lookup whiteout for the new entry.
20906 + */
20907 +static struct dentry*
20908 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20909 +                 struct dentry *src_dentry, struct au_pin *pin,
20910 +                 struct au_wr_dir_args *wr_dir_args)
20911 +{
20912 +       struct dentry *wh_dentry, *h_parent;
20913 +       struct super_block *sb;
20914 +       struct au_branch *br;
20915 +       int err;
20916 +       unsigned int udba;
20917 +       aufs_bindex_t bcpup;
20918 +
20919 +       AuDbg("%pd\n", dentry);
20920 +
20921 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20922 +       bcpup = err;
20923 +       wh_dentry = ERR_PTR(err);
20924 +       if (unlikely(err < 0))
20925 +               goto out;
20926 +
20927 +       sb = dentry->d_sb;
20928 +       udba = au_opt_udba(sb);
20929 +       err = au_pin(pin, dentry, bcpup, udba,
20930 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20931 +       wh_dentry = ERR_PTR(err);
20932 +       if (unlikely(err))
20933 +               goto out;
20934 +
20935 +       h_parent = au_pinned_h_parent(pin);
20936 +       if (udba != AuOpt_UDBA_NONE
20937 +           && au_dbtop(dentry) == bcpup)
20938 +               err = au_may_add(dentry, bcpup, h_parent,
20939 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20940 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20941 +               err = -ENAMETOOLONG;
20942 +       wh_dentry = ERR_PTR(err);
20943 +       if (unlikely(err))
20944 +               goto out_unpin;
20945 +
20946 +       br = au_sbr(sb, bcpup);
20947 +       if (dt) {
20948 +               struct path tmp = {
20949 +                       .dentry = h_parent,
20950 +                       .mnt    = au_br_mnt(br)
20951 +               };
20952 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20953 +       }
20954 +
20955 +       wh_dentry = NULL;
20956 +       if (bcpup != au_dbwh(dentry))
20957 +               goto out; /* success */
20958 +
20959 +       /*
20960 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20961 +        * would not be able to removed in the future. So we don't allow such
20962 +        * name here and we don't handle ENAMETOOLONG differently here.
20963 +        */
20964 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20965 +
20966 +out_unpin:
20967 +       if (IS_ERR(wh_dentry))
20968 +               au_unpin(pin);
20969 +out:
20970 +       return wh_dentry;
20971 +}
20972 +
20973 +/* ---------------------------------------------------------------------- */
20974 +
20975 +enum { Mknod, Symlink, Creat };
20976 +struct simple_arg {
20977 +       int type;
20978 +       union {
20979 +               struct {
20980 +                       umode_t                 mode;
20981 +                       bool                    want_excl;
20982 +                       bool                    try_aopen;
20983 +                       struct vfsub_aopen_args *aopen;
20984 +               } c;
20985 +               struct {
20986 +                       const char *symname;
20987 +               } s;
20988 +               struct {
20989 +                       umode_t mode;
20990 +                       dev_t dev;
20991 +               } m;
20992 +       } u;
20993 +};
20994 +
20995 +static int add_simple(struct inode *dir, struct dentry *dentry,
20996 +                     struct simple_arg *arg)
20997 +{
20998 +       int err, rerr;
20999 +       aufs_bindex_t btop;
21000 +       unsigned char created;
21001 +       const unsigned char try_aopen
21002 +               = (arg->type == Creat && arg->u.c.try_aopen);
21003 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
21004 +       struct dentry *wh_dentry, *parent;
21005 +       struct inode *h_dir;
21006 +       struct super_block *sb;
21007 +       struct au_branch *br;
21008 +       /* to reduce stack size */
21009 +       struct {
21010 +               struct au_dtime dt;
21011 +               struct au_pin pin;
21012 +               struct path h_path;
21013 +               struct au_wr_dir_args wr_dir_args;
21014 +       } *a;
21015 +
21016 +       AuDbg("%pd\n", dentry);
21017 +       IMustLock(dir);
21018 +
21019 +       err = -ENOMEM;
21020 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21021 +       if (unlikely(!a))
21022 +               goto out;
21023 +       a->wr_dir_args.force_btgt = -1;
21024 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21025 +
21026 +       parent = dentry->d_parent; /* dir inode is locked */
21027 +       if (!try_aopen) {
21028 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21029 +               if (unlikely(err))
21030 +                       goto out_free;
21031 +       }
21032 +       err = au_d_may_add(dentry);
21033 +       if (unlikely(err))
21034 +               goto out_unlock;
21035 +       if (!try_aopen)
21036 +               di_write_lock_parent(parent);
21037 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21038 +                                     &a->pin, &a->wr_dir_args);
21039 +       err = PTR_ERR(wh_dentry);
21040 +       if (IS_ERR(wh_dentry))
21041 +               goto out_parent;
21042 +
21043 +       btop = au_dbtop(dentry);
21044 +       sb = dentry->d_sb;
21045 +       br = au_sbr(sb, btop);
21046 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21047 +       a->h_path.mnt = au_br_mnt(br);
21048 +       h_dir = au_pinned_h_dir(&a->pin);
21049 +       switch (arg->type) {
21050 +       case Creat:
21051 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21052 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21053 +                                          arg->u.c.want_excl);
21054 +                       created = !err;
21055 +                       if (!err && try_aopen)
21056 +                               aopen->file->f_mode |= FMODE_CREATED;
21057 +               } else {
21058 +                       aopen->br = br;
21059 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21060 +                       AuDbg("err %d\n", err);
21061 +                       AuDbgFile(aopen->file);
21062 +                       created = err >= 0
21063 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21064 +               }
21065 +               break;
21066 +       case Symlink:
21067 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21068 +               created = !err;
21069 +               break;
21070 +       case Mknod:
21071 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21072 +                                 arg->u.m.dev);
21073 +               created = !err;
21074 +               break;
21075 +       default:
21076 +               BUG();
21077 +       }
21078 +       if (unlikely(err < 0))
21079 +               goto out_unpin;
21080 +
21081 +       err = epilog(dir, btop, wh_dentry, dentry);
21082 +       if (!err)
21083 +               goto out_unpin; /* success */
21084 +
21085 +       /* revert */
21086 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21087 +               /* no delegation since it is just created */
21088 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21089 +                                   /*force*/0);
21090 +               if (rerr) {
21091 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21092 +                               dentry, err, rerr);
21093 +                       err = -EIO;
21094 +               }
21095 +               au_dtime_revert(&a->dt);
21096 +       }
21097 +       if (try_aopen && h_dir->i_op->atomic_open
21098 +           && (aopen->file->f_mode & FMODE_OPENED))
21099 +               /* aopen->file is still opened */
21100 +               au_lcnt_dec(&aopen->br->br_nfiles);
21101 +
21102 +out_unpin:
21103 +       au_unpin(&a->pin);
21104 +       dput(wh_dentry);
21105 +out_parent:
21106 +       if (!try_aopen)
21107 +               di_write_unlock(parent);
21108 +out_unlock:
21109 +       if (unlikely(err)) {
21110 +               au_update_dbtop(dentry);
21111 +               d_drop(dentry);
21112 +       }
21113 +       if (!try_aopen)
21114 +               aufs_read_unlock(dentry, AuLock_DW);
21115 +out_free:
21116 +       au_kfree_rcu(a);
21117 +out:
21118 +       return err;
21119 +}
21120 +
21121 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
21122 +              struct dentry *dentry, umode_t mode, dev_t dev)
21123 +{
21124 +       struct simple_arg arg = {
21125 +               .type = Mknod,
21126 +               .u.m = {
21127 +                       .mode   = mode,
21128 +                       .dev    = dev
21129 +               }
21130 +       };
21131 +       return add_simple(dir, dentry, &arg);
21132 +}
21133 +
21134 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
21135 +                struct dentry *dentry, const char *symname)
21136 +{
21137 +       struct simple_arg arg = {
21138 +               .type = Symlink,
21139 +               .u.s.symname = symname
21140 +       };
21141 +       return add_simple(dir, dentry, &arg);
21142 +}
21143 +
21144 +int aufs_create(struct user_namespace *userns, struct inode *dir,
21145 +               struct dentry *dentry, umode_t mode, bool want_excl)
21146 +{
21147 +       struct simple_arg arg = {
21148 +               .type = Creat,
21149 +               .u.c = {
21150 +                       .mode           = mode,
21151 +                       .want_excl      = want_excl
21152 +               }
21153 +       };
21154 +       return add_simple(dir, dentry, &arg);
21155 +}
21156 +
21157 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21158 +                      struct vfsub_aopen_args *aopen_args)
21159 +{
21160 +       struct simple_arg arg = {
21161 +               .type = Creat,
21162 +               .u.c = {
21163 +                       .mode           = aopen_args->create_mode,
21164 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21165 +                       .try_aopen      = true,
21166 +                       .aopen          = aopen_args
21167 +               }
21168 +       };
21169 +       return add_simple(dir, dentry, &arg);
21170 +}
21171 +
21172 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
21173 +                struct file *file, umode_t mode)
21174 +{
21175 +       int err;
21176 +       aufs_bindex_t bindex;
21177 +       struct path h_ppath;
21178 +       struct super_block *sb;
21179 +       struct au_branch *br;
21180 +       struct dentry *dentry, *parent, *h_parent, *h_dentry;
21181 +       struct inode *h_dir, *inode;
21182 +       struct vfsmount *h_mnt;
21183 +       struct user_namespace *h_userns;
21184 +       struct file *h_file;
21185 +       struct au_wr_dir_args wr_dir_args = {
21186 +               .force_btgt     = -1,
21187 +               .flags          = AuWrDir_TMPFILE
21188 +       };
21189 +
21190 +       /* copy-up may happen */
21191 +       inode_lock(dir);
21192 +
21193 +       h_file = NULL;
21194 +       sb = dir->i_sb;
21195 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21196 +       if (unlikely(err))
21197 +               goto out;
21198 +
21199 +       dentry = file->f_path.dentry;
21200 +       err = au_di_init(dentry);
21201 +       if (unlikely(err))
21202 +               goto out_si;
21203 +
21204 +       err = -EBUSY;
21205 +       parent = d_find_any_alias(dir);
21206 +       AuDebugOn(!parent);
21207 +       di_write_lock_parent(parent);
21208 +       if (unlikely(d_inode(parent) != dir))
21209 +               goto out_parent;
21210 +
21211 +       err = au_digen_test(parent, au_sigen(sb));
21212 +       if (unlikely(err))
21213 +               goto out_parent;
21214 +
21215 +       bindex = au_dbtop(parent);
21216 +       au_set_dbtop(dentry, bindex);
21217 +       au_set_dbbot(dentry, bindex);
21218 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21219 +       bindex = err;
21220 +       if (unlikely(err < 0))
21221 +               goto out_parent;
21222 +
21223 +       err = -EOPNOTSUPP;
21224 +       h_dir = au_h_iptr(dir, bindex);
21225 +       if (unlikely(!h_dir->i_op->tmpfile))
21226 +               goto out_parent;
21227 +
21228 +       br = au_sbr(sb, bindex);
21229 +       h_mnt = au_br_mnt(br);
21230 +       err = vfsub_mnt_want_write(h_mnt);
21231 +       if (unlikely(err))
21232 +               goto out_parent;
21233 +
21234 +       h_userns = mnt_user_ns(h_mnt);
21235 +       h_parent = au_h_dptr(parent, bindex);
21236 +       h_ppath.mnt = h_mnt;
21237 +       h_ppath.dentry = h_parent;
21238 +       h_file = vfs_tmpfile_open(h_userns, &h_ppath, mode, /*open_flag*/0,
21239 +                                   /*cred*/NULL);
21240 +       if (IS_ERR(h_file)) {
21241 +               err = PTR_ERR(h_file);
21242 +               h_file = NULL;
21243 +               goto out_mnt;
21244 +       }
21245 +
21246 +       h_dentry = h_file->f_path.dentry;
21247 +       au_set_dbtop(dentry, bindex);
21248 +       au_set_dbbot(dentry, bindex);
21249 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21250 +       inode = au_new_inode(dentry, /*must_new*/1);
21251 +       if (IS_ERR(inode)) {
21252 +               err = PTR_ERR(inode);
21253 +               au_set_h_dptr(dentry, bindex, NULL);
21254 +               au_set_dbtop(dentry, -1);
21255 +               au_set_dbbot(dentry, -1);
21256 +               goto out_h_file;
21257 +       }
21258 +
21259 +       if (!inode->i_nlink)
21260 +               set_nlink(inode, 1);
21261 +       d_tmpfile(file, inode);
21262 +       au_di(dentry)->di_tmpfile = 1;
21263 +       get_file(h_file);
21264 +       au_di(dentry)->di_htmpfile = h_file;
21265 +
21266 +       /* update without i_mutex */
21267 +       if (au_ibtop(dir) == au_dbtop(dentry))
21268 +               au_cpup_attr_timesizes(dir);
21269 +
21270 +out_h_file:
21271 +       fput(h_file);
21272 +out_mnt:
21273 +       vfsub_mnt_drop_write(h_mnt);
21274 +out_parent:
21275 +       di_write_unlock(parent);
21276 +       dput(parent);
21277 +       di_write_unlock(dentry);
21278 +       if (!err)
21279 +               goto out_si;
21280 +       if (h_file)
21281 +               fput(h_file);
21282 +       au_di(dentry)->di_htmpfile = NULL;
21283 +       au_di_fin(dentry);
21284 +       dentry->d_fsdata = NULL;
21285 +out_si:
21286 +       si_read_unlock(sb);
21287 +       if (!err && h_file) {
21288 +               /* finally... */
21289 +               err = finish_open_simple(file, err);
21290 +               if (!err)
21291 +                       au_lcnt_inc(&br->br_nfiles);
21292 +               else {
21293 +                       fput(h_file);
21294 +                       au_di(dentry)->di_htmpfile = NULL;
21295 +                       au_di_fin(dentry);
21296 +                       dentry->d_fsdata = NULL;
21297 +               }
21298 +       }
21299 +out:
21300 +       inode_unlock(dir);
21301 +       AuTraceErr(err);
21302 +       return err;
21303 +}
21304 +
21305 +/* ---------------------------------------------------------------------- */
21306 +
21307 +struct au_link_args {
21308 +       aufs_bindex_t bdst, bsrc;
21309 +       struct au_pin pin;
21310 +       struct path h_path;
21311 +       struct dentry *src_parent, *parent;
21312 +};
21313 +
21314 +static int au_cpup_before_link(struct dentry *src_dentry,
21315 +                              struct au_link_args *a)
21316 +{
21317 +       int err;
21318 +       struct dentry *h_src_dentry;
21319 +       struct au_cp_generic cpg = {
21320 +               .dentry = src_dentry,
21321 +               .bdst   = a->bdst,
21322 +               .bsrc   = a->bsrc,
21323 +               .len    = -1,
21324 +               .pin    = &a->pin,
21325 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21326 +       };
21327 +
21328 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21329 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21330 +       if (unlikely(err))
21331 +               goto out;
21332 +
21333 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21334 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21335 +                    au_opt_udba(src_dentry->d_sb),
21336 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21337 +       if (unlikely(err))
21338 +               goto out;
21339 +
21340 +       err = au_sio_cpup_simple(&cpg);
21341 +       au_unpin(&a->pin);
21342 +
21343 +out:
21344 +       di_read_unlock(a->src_parent, AuLock_IR);
21345 +       return err;
21346 +}
21347 +
21348 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21349 +                          struct au_link_args *a)
21350 +{
21351 +       int err;
21352 +       unsigned char plink;
21353 +       aufs_bindex_t bbot;
21354 +       struct dentry *h_src_dentry;
21355 +       struct inode *h_inode, *inode, *delegated;
21356 +       struct super_block *sb;
21357 +       struct file *h_file;
21358 +
21359 +       plink = 0;
21360 +       h_inode = NULL;
21361 +       sb = src_dentry->d_sb;
21362 +       inode = d_inode(src_dentry);
21363 +       if (au_ibtop(inode) <= a->bdst)
21364 +               h_inode = au_h_iptr(inode, a->bdst);
21365 +       if (!h_inode || !h_inode->i_nlink) {
21366 +               /* copyup src_dentry as the name of dentry. */
21367 +               bbot = au_dbbot(dentry);
21368 +               if (bbot < a->bsrc)
21369 +                       au_set_dbbot(dentry, a->bsrc);
21370 +               au_set_h_dptr(dentry, a->bsrc,
21371 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21372 +               dget(a->h_path.dentry);
21373 +               au_set_h_dptr(dentry, a->bdst, NULL);
21374 +               AuDbg("temporary d_inode...\n");
21375 +               spin_lock(&dentry->d_lock);
21376 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21377 +               spin_unlock(&dentry->d_lock);
21378 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21379 +               if (IS_ERR(h_file))
21380 +                       err = PTR_ERR(h_file);
21381 +               else {
21382 +                       struct au_cp_generic cpg = {
21383 +                               .dentry = dentry,
21384 +                               .bdst   = a->bdst,
21385 +                               .bsrc   = -1,
21386 +                               .len    = -1,
21387 +                               .pin    = &a->pin,
21388 +                               .flags  = AuCpup_KEEPLINO
21389 +                       };
21390 +                       err = au_sio_cpup_simple(&cpg);
21391 +                       au_h_open_post(dentry, a->bsrc, h_file);
21392 +                       if (!err) {
21393 +                               dput(a->h_path.dentry);
21394 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21395 +                       } else
21396 +                               au_set_h_dptr(dentry, a->bdst,
21397 +                                             a->h_path.dentry);
21398 +               }
21399 +               spin_lock(&dentry->d_lock);
21400 +               dentry->d_inode = NULL; /* restore */
21401 +               spin_unlock(&dentry->d_lock);
21402 +               AuDbg("temporary d_inode...done\n");
21403 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21404 +               au_set_dbbot(dentry, bbot);
21405 +       } else {
21406 +               /* the inode of src_dentry already exists on a.bdst branch */
21407 +               h_src_dentry = d_find_alias(h_inode);
21408 +               if (!h_src_dentry && au_plink_test(inode)) {
21409 +                       plink = 1;
21410 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21411 +                       err = PTR_ERR(h_src_dentry);
21412 +                       if (IS_ERR(h_src_dentry))
21413 +                               goto out;
21414 +
21415 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21416 +                               dput(h_src_dentry);
21417 +                               h_src_dentry = NULL;
21418 +                       }
21419 +
21420 +               }
21421 +               if (h_src_dentry) {
21422 +                       delegated = NULL;
21423 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21424 +                                        &a->h_path, &delegated);
21425 +                       if (unlikely(err == -EWOULDBLOCK)) {
21426 +                               pr_warn("cannot retry for NFSv4 delegation"
21427 +                                       " for an internal link\n");
21428 +                               iput(delegated);
21429 +                       }
21430 +                       dput(h_src_dentry);
21431 +               } else {
21432 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21433 +                               h_inode->i_ino, a->bdst);
21434 +                       err = -EIO;
21435 +               }
21436 +       }
21437 +
21438 +       if (!err && !plink)
21439 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21440 +
21441 +out:
21442 +       AuTraceErr(err);
21443 +       return err;
21444 +}
21445 +
21446 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21447 +             struct dentry *dentry)
21448 +{
21449 +       int err, rerr;
21450 +       struct au_dtime dt;
21451 +       struct au_link_args *a;
21452 +       struct dentry *wh_dentry, *h_src_dentry;
21453 +       struct inode *inode, *delegated;
21454 +       struct super_block *sb;
21455 +       struct au_wr_dir_args wr_dir_args = {
21456 +               /* .force_btgt  = -1, */
21457 +               .flags          = AuWrDir_ADD_ENTRY
21458 +       };
21459 +
21460 +       IMustLock(dir);
21461 +       inode = d_inode(src_dentry);
21462 +       IMustLock(inode);
21463 +
21464 +       err = -ENOMEM;
21465 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21466 +       if (unlikely(!a))
21467 +               goto out;
21468 +
21469 +       a->parent = dentry->d_parent; /* dir inode is locked */
21470 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21471 +                                       AuLock_NOPLM | AuLock_GEN);
21472 +       if (unlikely(err))
21473 +               goto out_kfree;
21474 +       err = au_d_linkable(src_dentry);
21475 +       if (unlikely(err))
21476 +               goto out_unlock;
21477 +       err = au_d_may_add(dentry);
21478 +       if (unlikely(err))
21479 +               goto out_unlock;
21480 +
21481 +       a->src_parent = dget_parent(src_dentry);
21482 +       wr_dir_args.force_btgt = au_ibtop(inode);
21483 +
21484 +       di_write_lock_parent(a->parent);
21485 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21486 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21487 +                                     &wr_dir_args);
21488 +       err = PTR_ERR(wh_dentry);
21489 +       if (IS_ERR(wh_dentry))
21490 +               goto out_parent;
21491 +
21492 +       err = 0;
21493 +       sb = dentry->d_sb;
21494 +       a->bdst = au_dbtop(dentry);
21495 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21496 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21497 +       a->bsrc = au_ibtop(inode);
21498 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21499 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21500 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21501 +       if (!h_src_dentry) {
21502 +               a->bsrc = au_dbtop(src_dentry);
21503 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21504 +               AuDebugOn(!h_src_dentry);
21505 +       } else if (IS_ERR(h_src_dentry)) {
21506 +               err = PTR_ERR(h_src_dentry);
21507 +               goto out_parent;
21508 +       }
21509 +
21510 +       /*
21511 +        * aufs doesn't touch the credential so
21512 +        * security_dentry_create_files_as() is unnecessary.
21513 +        */
21514 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21515 +               if (a->bdst < a->bsrc
21516 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21517 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21518 +               else {
21519 +                       delegated = NULL;
21520 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21521 +                                        &a->h_path, &delegated);
21522 +                       if (unlikely(err == -EWOULDBLOCK)) {
21523 +                               pr_warn("cannot retry for NFSv4 delegation"
21524 +                                       " for an internal link\n");
21525 +                               iput(delegated);
21526 +                       }
21527 +               }
21528 +               dput(h_src_dentry);
21529 +       } else {
21530 +               /*
21531 +                * copyup src_dentry to the branch we process,
21532 +                * and then link(2) to it.
21533 +                */
21534 +               dput(h_src_dentry);
21535 +               if (a->bdst < a->bsrc
21536 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21537 +                       au_unpin(&a->pin);
21538 +                       di_write_unlock(a->parent);
21539 +                       err = au_cpup_before_link(src_dentry, a);
21540 +                       di_write_lock_parent(a->parent);
21541 +                       if (!err)
21542 +                               err = au_pin(&a->pin, dentry, a->bdst,
21543 +                                            au_opt_udba(sb),
21544 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21545 +                       if (unlikely(err))
21546 +                               goto out_wh;
21547 +               }
21548 +               if (!err) {
21549 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21550 +                       err = -ENOENT;
21551 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21552 +                               delegated = NULL;
21553 +                               err = vfsub_link(h_src_dentry,
21554 +                                                au_pinned_h_dir(&a->pin),
21555 +                                                &a->h_path, &delegated);
21556 +                               if (unlikely(err == -EWOULDBLOCK)) {
21557 +                                       pr_warn("cannot retry"
21558 +                                               " for NFSv4 delegation"
21559 +                                               " for an internal link\n");
21560 +                                       iput(delegated);
21561 +                               }
21562 +                       }
21563 +               }
21564 +       }
21565 +       if (unlikely(err))
21566 +               goto out_unpin;
21567 +
21568 +       if (wh_dentry) {
21569 +               a->h_path.dentry = wh_dentry;
21570 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21571 +                                         dentry);
21572 +               if (unlikely(err))
21573 +                       goto out_revert;
21574 +       }
21575 +
21576 +       au_dir_ts(dir, a->bdst);
21577 +       inode_inc_iversion(dir);
21578 +       inc_nlink(inode);
21579 +       inode->i_ctime = dir->i_ctime;
21580 +       d_instantiate(dentry, au_igrab(inode));
21581 +       if (d_unhashed(a->h_path.dentry))
21582 +               /* some filesystem calls d_drop() */
21583 +               d_drop(dentry);
21584 +       /* some filesystems consume an inode even hardlink */
21585 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21586 +       goto out_unpin; /* success */
21587 +
21588 +out_revert:
21589 +       /* no delegation since it is just created */
21590 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21591 +                           /*delegated*/NULL, /*force*/0);
21592 +       if (unlikely(rerr)) {
21593 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21594 +               err = -EIO;
21595 +       }
21596 +       au_dtime_revert(&dt);
21597 +out_unpin:
21598 +       au_unpin(&a->pin);
21599 +out_wh:
21600 +       dput(wh_dentry);
21601 +out_parent:
21602 +       di_write_unlock(a->parent);
21603 +       dput(a->src_parent);
21604 +out_unlock:
21605 +       if (unlikely(err)) {
21606 +               au_update_dbtop(dentry);
21607 +               d_drop(dentry);
21608 +       }
21609 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21610 +out_kfree:
21611 +       au_kfree_rcu(a);
21612 +out:
21613 +       AuTraceErr(err);
21614 +       return err;
21615 +}
21616 +
21617 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
21618 +              struct dentry *dentry, umode_t mode)
21619 +{
21620 +       int err, rerr;
21621 +       aufs_bindex_t bindex;
21622 +       unsigned char diropq;
21623 +       struct path h_path;
21624 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21625 +       struct inode *h_inode;
21626 +       struct super_block *sb;
21627 +       struct {
21628 +               struct au_pin pin;
21629 +               struct au_dtime dt;
21630 +       } *a; /* reduce the stack usage */
21631 +       struct au_wr_dir_args wr_dir_args = {
21632 +               .force_btgt     = -1,
21633 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21634 +       };
21635 +
21636 +       IMustLock(dir);
21637 +
21638 +       err = -ENOMEM;
21639 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21640 +       if (unlikely(!a))
21641 +               goto out;
21642 +
21643 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21644 +       if (unlikely(err))
21645 +               goto out_free;
21646 +       err = au_d_may_add(dentry);
21647 +       if (unlikely(err))
21648 +               goto out_unlock;
21649 +
21650 +       parent = dentry->d_parent; /* dir inode is locked */
21651 +       di_write_lock_parent(parent);
21652 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21653 +                                     &a->pin, &wr_dir_args);
21654 +       err = PTR_ERR(wh_dentry);
21655 +       if (IS_ERR(wh_dentry))
21656 +               goto out_parent;
21657 +
21658 +       sb = dentry->d_sb;
21659 +       bindex = au_dbtop(dentry);
21660 +       h_path.dentry = au_h_dptr(dentry, bindex);
21661 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21662 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21663 +       if (unlikely(err))
21664 +               goto out_unpin;
21665 +
21666 +       /* make the dir opaque */
21667 +       diropq = 0;
21668 +       h_inode = d_inode(h_path.dentry);
21669 +       if (wh_dentry
21670 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21671 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21672 +               opq_dentry = au_diropq_create(dentry, bindex);
21673 +               inode_unlock(h_inode);
21674 +               err = PTR_ERR(opq_dentry);
21675 +               if (IS_ERR(opq_dentry))
21676 +                       goto out_dir;
21677 +               dput(opq_dentry);
21678 +               diropq = 1;
21679 +       }
21680 +
21681 +       err = epilog(dir, bindex, wh_dentry, dentry);
21682 +       if (!err) {
21683 +               inc_nlink(dir);
21684 +               goto out_unpin; /* success */
21685 +       }
21686 +
21687 +       /* revert */
21688 +       if (diropq) {
21689 +               AuLabel(revert opq);
21690 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21691 +               rerr = au_diropq_remove(dentry, bindex);
21692 +               inode_unlock(h_inode);
21693 +               if (rerr) {
21694 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21695 +                               dentry, err, rerr);
21696 +                       err = -EIO;
21697 +               }
21698 +       }
21699 +
21700 +out_dir:
21701 +       AuLabel(revert dir);
21702 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21703 +       if (rerr) {
21704 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21705 +                       dentry, err, rerr);
21706 +               err = -EIO;
21707 +       }
21708 +       au_dtime_revert(&a->dt);
21709 +out_unpin:
21710 +       au_unpin(&a->pin);
21711 +       dput(wh_dentry);
21712 +out_parent:
21713 +       di_write_unlock(parent);
21714 +out_unlock:
21715 +       if (unlikely(err)) {
21716 +               au_update_dbtop(dentry);
21717 +               d_drop(dentry);
21718 +       }
21719 +       aufs_read_unlock(dentry, AuLock_DW);
21720 +out_free:
21721 +       au_kfree_rcu(a);
21722 +out:
21723 +       return err;
21724 +}
21725 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21726 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21727 +++ linux/fs/aufs/i_op.c        2022-11-05 23:02:18.965889284 +0100
21728 @@ -0,0 +1,1512 @@
21729 +// SPDX-License-Identifier: GPL-2.0
21730 +/*
21731 + * Copyright (C) 2005-2022 Junjiro R. Okajima
21732 + *
21733 + * This program is free software; you can redistribute it and/or modify
21734 + * it under the terms of the GNU General Public License as published by
21735 + * the Free Software Foundation; either version 2 of the License, or
21736 + * (at your option) any later version.
21737 + *
21738 + * This program is distributed in the hope that it will be useful,
21739 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21740 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21741 + * GNU General Public License for more details.
21742 + *
21743 + * You should have received a copy of the GNU General Public License
21744 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21745 + */
21746 +
21747 +/*
21748 + * inode operations (except add/del/rename)
21749 + */
21750 +
21751 +#include <linux/device_cgroup.h>
21752 +#include <linux/fs_stack.h>
21753 +#include <linux/iversion.h>
21754 +#include <linux/security.h>
21755 +#include "aufs.h"
21756 +
21757 +static int h_permission(struct inode *h_inode, int mask,
21758 +                       struct path *h_path, int brperm)
21759 +{
21760 +       int err;
21761 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21762 +       struct user_namespace *h_userns;
21763 +
21764 +       err = -EPERM;
21765 +       if (write_mask && IS_IMMUTABLE(h_inode))
21766 +               goto out;
21767 +
21768 +       err = -EACCES;
21769 +       if (((mask & MAY_EXEC)
21770 +            && S_ISREG(h_inode->i_mode)
21771 +            && (path_noexec(h_path)
21772 +                || !(h_inode->i_mode & 0111))))
21773 +               goto out;
21774 +
21775 +       /*
21776 +        * - skip the lower fs test in the case of write to ro branch.
21777 +        * - nfs dir permission write check is optimized, but a policy for
21778 +        *   link/rename requires a real check.
21779 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21780 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21781 +        */
21782 +       h_userns = mnt_user_ns(h_path->mnt);
21783 +       if ((write_mask && !au_br_writable(brperm))
21784 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21785 +               && write_mask && !(mask & MAY_READ))
21786 +           || !h_inode->i_op->permission) {
21787 +               /* AuLabel(generic_permission); */
21788 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
21789 +               err = generic_permission(h_userns, h_inode, mask);
21790 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21791 +                       err = h_inode->i_op->permission(h_userns, h_inode,
21792 +                                                       mask);
21793 +               AuTraceErr(err);
21794 +       } else {
21795 +               /* AuLabel(h_inode->permission); */
21796 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
21797 +               AuTraceErr(err);
21798 +       }
21799 +
21800 +       if (!err)
21801 +               err = devcgroup_inode_permission(h_inode, mask);
21802 +       if (!err)
21803 +               err = security_inode_permission(h_inode, mask);
21804 +
21805 +out:
21806 +       return err;
21807 +}
21808 +
21809 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
21810 +                          int mask)
21811 +{
21812 +       int err;
21813 +       aufs_bindex_t bindex, bbot;
21814 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21815 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21816 +       struct inode *h_inode;
21817 +       struct super_block *sb;
21818 +       struct au_branch *br;
21819 +
21820 +       /* todo: support rcu-walk? */
21821 +       if (mask & MAY_NOT_BLOCK)
21822 +               return -ECHILD;
21823 +
21824 +       sb = inode->i_sb;
21825 +       si_read_lock(sb, AuLock_FLUSH);
21826 +       ii_read_lock_child(inode);
21827 +#if 0 /* reserved for future use */
21828 +       /*
21829 +        * This test may be rather 'too much' since the test is essentially done
21830 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21831 +        * generation doesn't match to the superblock's here.  But it isn't a
21832 +        * big deal I suppose.
21833 +        */
21834 +       err = au_iigen_test(inode, au_sigen(sb));
21835 +       if (unlikely(err))
21836 +               goto out;
21837 +#endif
21838 +
21839 +       if (!isdir
21840 +           || write_mask
21841 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21842 +               err = au_busy_or_stale();
21843 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21844 +               if (unlikely(!h_inode
21845 +                            || (h_inode->i_mode & S_IFMT)
21846 +                            != (inode->i_mode & S_IFMT)))
21847 +                       goto out;
21848 +
21849 +               err = 0;
21850 +               bindex = au_ibtop(inode);
21851 +               br = au_sbr(sb, bindex);
21852 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21853 +               if (write_mask
21854 +                   && !err
21855 +                   && !special_file(h_inode->i_mode)) {
21856 +                       /* test whether the upper writable branch exists */
21857 +                       err = -EROFS;
21858 +                       for (; bindex >= 0; bindex--)
21859 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21860 +                                       err = 0;
21861 +                                       break;
21862 +                               }
21863 +               }
21864 +               goto out;
21865 +       }
21866 +
21867 +       /* non-write to dir */
21868 +       err = 0;
21869 +       bbot = au_ibbot(inode);
21870 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21871 +               h_inode = au_h_iptr(inode, bindex);
21872 +               if (h_inode) {
21873 +                       err = au_busy_or_stale();
21874 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21875 +                               break;
21876 +
21877 +                       br = au_sbr(sb, bindex);
21878 +                       err = h_permission(h_inode, mask, &br->br_path,
21879 +                                          br->br_perm);
21880 +               }
21881 +       }
21882 +
21883 +out:
21884 +       ii_read_unlock(inode);
21885 +       si_read_unlock(sb);
21886 +       return err;
21887 +}
21888 +
21889 +/* ---------------------------------------------------------------------- */
21890 +
21891 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21892 +                                 unsigned int flags)
21893 +{
21894 +       struct dentry *ret, *parent;
21895 +       struct inode *inode;
21896 +       struct super_block *sb;
21897 +       int err, npositive;
21898 +
21899 +       IMustLock(dir);
21900 +
21901 +       /* todo: support rcu-walk? */
21902 +       ret = ERR_PTR(-ECHILD);
21903 +       if (flags & LOOKUP_RCU)
21904 +               goto out;
21905 +
21906 +       ret = ERR_PTR(-ENAMETOOLONG);
21907 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21908 +               goto out;
21909 +
21910 +       sb = dir->i_sb;
21911 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21912 +       ret = ERR_PTR(err);
21913 +       if (unlikely(err))
21914 +               goto out;
21915 +
21916 +       err = au_di_init(dentry);
21917 +       ret = ERR_PTR(err);
21918 +       if (unlikely(err))
21919 +               goto out_si;
21920 +
21921 +       inode = NULL;
21922 +       npositive = 0; /* suppress a warning */
21923 +       parent = dentry->d_parent; /* dir inode is locked */
21924 +       di_read_lock_parent(parent, AuLock_IR);
21925 +       err = au_alive_dir(parent);
21926 +       if (!err)
21927 +               err = au_digen_test(parent, au_sigen(sb));
21928 +       if (!err) {
21929 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21930 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21931 +                                          AuLkup_ALLOW_NEG);
21932 +               err = npositive;
21933 +       }
21934 +       di_read_unlock(parent, AuLock_IR);
21935 +       ret = ERR_PTR(err);
21936 +       if (unlikely(err < 0))
21937 +               goto out_unlock;
21938 +
21939 +       if (npositive) {
21940 +               inode = au_new_inode(dentry, /*must_new*/0);
21941 +               if (IS_ERR(inode)) {
21942 +                       ret = (void *)inode;
21943 +                       inode = NULL;
21944 +                       goto out_unlock;
21945 +               }
21946 +       }
21947 +
21948 +       if (inode)
21949 +               atomic_inc(&inode->i_count);
21950 +       ret = d_splice_alias(inode, dentry);
21951 +#if 0 /* reserved for future use */
21952 +       if (unlikely(d_need_lookup(dentry))) {
21953 +               spin_lock(&dentry->d_lock);
21954 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21955 +               spin_unlock(&dentry->d_lock);
21956 +       } else
21957 +#endif
21958 +       if (inode) {
21959 +               if (!IS_ERR(ret)) {
21960 +                       iput(inode);
21961 +                       if (ret && ret != dentry)
21962 +                               ii_write_unlock(inode);
21963 +               } else {
21964 +                       ii_write_unlock(inode);
21965 +                       iput(inode);
21966 +                       inode = NULL;
21967 +               }
21968 +       }
21969 +
21970 +out_unlock:
21971 +       di_write_unlock(dentry);
21972 +out_si:
21973 +       si_read_unlock(sb);
21974 +out:
21975 +       return ret;
21976 +}
21977 +
21978 +/* ---------------------------------------------------------------------- */
21979 +
21980 +/*
21981 + * very dirty and complicated aufs ->atomic_open().
21982 + * aufs_atomic_open()
21983 + * + au_aopen_or_create()
21984 + *   + add_simple()
21985 + *     + vfsub_atomic_open()
21986 + *       + branch fs ->atomic_open()
21987 + *        may call the actual 'open' for h_file
21988 + *       + inc br_nfiles only if opened
21989 + * + au_aopen_no_open() or au_aopen_do_open()
21990 + *
21991 + * au_aopen_do_open()
21992 + * + finish_open()
21993 + *   + au_do_aopen()
21994 + *     + au_do_open() the body of all 'open'
21995 + *       + au_do_open_nondir()
21996 + *        set the passed h_file
21997 + *
21998 + * au_aopen_no_open()
21999 + * + finish_no_open()
22000 + */
22001 +
22002 +struct aopen_node {
22003 +       struct hlist_bl_node hblist;
22004 +       struct file *file, *h_file;
22005 +};
22006 +
22007 +static int au_do_aopen(struct inode *inode, struct file *file)
22008 +{
22009 +       struct hlist_bl_head *aopen;
22010 +       struct hlist_bl_node *pos;
22011 +       struct aopen_node *node;
22012 +       struct au_do_open_args args = {
22013 +               .aopen  = 1,
22014 +               .open   = au_do_open_nondir
22015 +       };
22016 +
22017 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
22018 +       hlist_bl_lock(aopen);
22019 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
22020 +               if (node->file == file) {
22021 +                       args.h_file = node->h_file;
22022 +                       break;
22023 +               }
22024 +       hlist_bl_unlock(aopen);
22025 +       /* AuDebugOn(!args.h_file); */
22026 +
22027 +       return au_do_open(file, &args);
22028 +}
22029 +
22030 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
22031 +                           struct aopen_node *aopen_node)
22032 +{
22033 +       int err;
22034 +       struct hlist_bl_head *aopen;
22035 +
22036 +       AuLabel(here);
22037 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
22038 +       au_hbl_add(&aopen_node->hblist, aopen);
22039 +       err = finish_open(file, dentry, au_do_aopen);
22040 +       au_hbl_del(&aopen_node->hblist, aopen);
22041 +       /* AuDbgFile(file); */
22042 +       AuDbg("%pd%s%s\n", dentry,
22043 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
22044 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22045 +
22046 +       AuTraceErr(err);
22047 +       return err;
22048 +}
22049 +
22050 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22051 +{
22052 +       int err;
22053 +
22054 +       AuLabel(here);
22055 +       dget(dentry);
22056 +       err = finish_no_open(file, dentry);
22057 +
22058 +       AuTraceErr(err);
22059 +       return err;
22060 +}
22061 +
22062 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22063 +                           struct file *file, unsigned int open_flag,
22064 +                           umode_t create_mode)
22065 +{
22066 +       int err, did_open;
22067 +       unsigned int lkup_flags;
22068 +       aufs_bindex_t bindex;
22069 +       struct super_block *sb;
22070 +       struct dentry *parent, *d;
22071 +       struct vfsub_aopen_args args = {
22072 +               .open_flag      = open_flag,
22073 +               .create_mode    = create_mode
22074 +       };
22075 +       struct aopen_node aopen_node = {
22076 +               .file   = file
22077 +       };
22078 +
22079 +       IMustLock(dir);
22080 +       AuDbg("open_flag 0%o\n", open_flag);
22081 +       AuDbgDentry(dentry);
22082 +
22083 +       err = 0;
22084 +       if (!au_di(dentry)) {
22085 +               lkup_flags = LOOKUP_OPEN;
22086 +               if (open_flag & O_CREAT)
22087 +                       lkup_flags |= LOOKUP_CREATE;
22088 +               d = aufs_lookup(dir, dentry, lkup_flags);
22089 +               if (IS_ERR(d)) {
22090 +                       err = PTR_ERR(d);
22091 +                       AuTraceErr(err);
22092 +                       goto out;
22093 +               } else if (d) {
22094 +                       /*
22095 +                        * obsoleted dentry found.
22096 +                        * another error will be returned later.
22097 +                        */
22098 +                       d_drop(d);
22099 +                       AuDbgDentry(d);
22100 +                       dput(d);
22101 +               }
22102 +               AuDbgDentry(dentry);
22103 +       }
22104 +
22105 +       if (d_is_positive(dentry)
22106 +           || d_unhashed(dentry)
22107 +           || d_unlinked(dentry)
22108 +           || !(open_flag & O_CREAT)) {
22109 +               err = au_aopen_no_open(file, dentry);
22110 +               goto out; /* success */
22111 +       }
22112 +
22113 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22114 +       if (unlikely(err))
22115 +               goto out;
22116 +
22117 +       sb = dentry->d_sb;
22118 +       parent = dentry->d_parent;      /* dir is locked */
22119 +       di_write_lock_parent(parent);
22120 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22121 +       if (unlikely(err < 0))
22122 +               goto out_parent;
22123 +
22124 +       AuDbgDentry(dentry);
22125 +       if (d_is_positive(dentry)) {
22126 +               err = au_aopen_no_open(file, dentry);
22127 +               goto out_parent; /* success */
22128 +       }
22129 +
22130 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22131 +       err = PTR_ERR(args.file);
22132 +       if (IS_ERR(args.file))
22133 +               goto out_parent;
22134 +
22135 +       bindex = au_dbtop(dentry);
22136 +       err = au_aopen_or_create(dir, dentry, &args);
22137 +       AuTraceErr(err);
22138 +       AuDbgFile(args.file);
22139 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22140 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22141 +       if (!did_open) {
22142 +               fput(args.file);
22143 +               args.file = NULL;
22144 +       }
22145 +       di_write_unlock(parent);
22146 +       di_write_unlock(dentry);
22147 +       if (unlikely(err < 0)) {
22148 +               if (args.file)
22149 +                       fput(args.file);
22150 +               goto out_sb;
22151 +       }
22152 +
22153 +       if (!did_open)
22154 +               err = au_aopen_no_open(file, dentry);
22155 +       else {
22156 +               aopen_node.h_file = args.file;
22157 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22158 +       }
22159 +       if (unlikely(err < 0)) {
22160 +               if (args.file)
22161 +                       fput(args.file);
22162 +               if (did_open)
22163 +                       au_lcnt_dec(&args.br->br_nfiles);
22164 +       }
22165 +       goto out_sb; /* success */
22166 +
22167 +out_parent:
22168 +       di_write_unlock(parent);
22169 +       di_write_unlock(dentry);
22170 +out_sb:
22171 +       si_read_unlock(sb);
22172 +out:
22173 +       AuTraceErr(err);
22174 +       AuDbgFile(file);
22175 +       return err;
22176 +}
22177 +
22178 +
22179 +/* ---------------------------------------------------------------------- */
22180 +
22181 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22182 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22183 +                         aufs_bindex_t btop)
22184 +{
22185 +       int err;
22186 +       struct dentry *h_parent;
22187 +       struct inode *h_dir;
22188 +
22189 +       if (add_entry)
22190 +               IMustLock(d_inode(parent));
22191 +       else
22192 +               di_write_lock_parent(parent);
22193 +
22194 +       err = 0;
22195 +       if (!au_h_dptr(parent, bcpup)) {
22196 +               if (btop > bcpup)
22197 +                       err = au_cpup_dirs(dentry, bcpup);
22198 +               else if (btop < bcpup)
22199 +                       err = au_cpdown_dirs(dentry, bcpup);
22200 +               else
22201 +                       BUG();
22202 +       }
22203 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22204 +               h_parent = au_h_dptr(parent, bcpup);
22205 +               h_dir = d_inode(h_parent);
22206 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22207 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22208 +               /* todo: no unlock here */
22209 +               inode_unlock_shared(h_dir);
22210 +
22211 +               AuDbg("bcpup %d\n", bcpup);
22212 +               if (!err) {
22213 +                       if (d_really_is_negative(dentry))
22214 +                               au_set_h_dptr(dentry, btop, NULL);
22215 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22216 +               }
22217 +       }
22218 +
22219 +       if (!add_entry)
22220 +               di_write_unlock(parent);
22221 +       if (!err)
22222 +               err = bcpup; /* success */
22223 +
22224 +       AuTraceErr(err);
22225 +       return err;
22226 +}
22227 +
22228 +/*
22229 + * decide the branch and the parent dir where we will create a new entry.
22230 + * returns new bindex or an error.
22231 + * copyup the parent dir if needed.
22232 + */
22233 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22234 +             struct au_wr_dir_args *args)
22235 +{
22236 +       int err;
22237 +       unsigned int flags;
22238 +       aufs_bindex_t bcpup, btop, src_btop;
22239 +       const unsigned char add_entry
22240 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22241 +               | au_ftest_wrdir(args->flags, TMPFILE);
22242 +       struct super_block *sb;
22243 +       struct dentry *parent;
22244 +       struct au_sbinfo *sbinfo;
22245 +
22246 +       sb = dentry->d_sb;
22247 +       sbinfo = au_sbi(sb);
22248 +       parent = dget_parent(dentry);
22249 +       btop = au_dbtop(dentry);
22250 +       bcpup = btop;
22251 +       if (args->force_btgt < 0) {
22252 +               if (src_dentry) {
22253 +                       src_btop = au_dbtop(src_dentry);
22254 +                       if (src_btop < btop)
22255 +                               bcpup = src_btop;
22256 +               } else if (add_entry) {
22257 +                       flags = 0;
22258 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22259 +                               au_fset_wbr(flags, DIR);
22260 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22261 +                       bcpup = err;
22262 +               }
22263 +
22264 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22265 +                       if (add_entry)
22266 +                               err = AuWbrCopyup(sbinfo, dentry);
22267 +                       else {
22268 +                               if (!IS_ROOT(dentry)) {
22269 +                                       di_read_lock_parent(parent, !AuLock_IR);
22270 +                                       err = AuWbrCopyup(sbinfo, dentry);
22271 +                                       di_read_unlock(parent, !AuLock_IR);
22272 +                               } else
22273 +                                       err = AuWbrCopyup(sbinfo, dentry);
22274 +                       }
22275 +                       bcpup = err;
22276 +                       if (unlikely(err < 0))
22277 +                               goto out;
22278 +               }
22279 +       } else {
22280 +               bcpup = args->force_btgt;
22281 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22282 +       }
22283 +
22284 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22285 +       err = bcpup;
22286 +       if (bcpup == btop)
22287 +               goto out; /* success */
22288 +
22289 +       /* copyup the new parent into the branch we process */
22290 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22291 +       if (err >= 0) {
22292 +               if (d_really_is_negative(dentry)) {
22293 +                       au_set_h_dptr(dentry, btop, NULL);
22294 +                       au_set_dbtop(dentry, bcpup);
22295 +                       au_set_dbbot(dentry, bcpup);
22296 +               }
22297 +               AuDebugOn(add_entry
22298 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22299 +                         && !au_h_dptr(dentry, bcpup));
22300 +       }
22301 +
22302 +out:
22303 +       dput(parent);
22304 +       return err;
22305 +}
22306 +
22307 +/* ---------------------------------------------------------------------- */
22308 +
22309 +void au_pin_hdir_unlock(struct au_pin *p)
22310 +{
22311 +       if (p->hdir)
22312 +               au_hn_inode_unlock(p->hdir);
22313 +}
22314 +
22315 +int au_pin_hdir_lock(struct au_pin *p)
22316 +{
22317 +       int err;
22318 +
22319 +       err = 0;
22320 +       if (!p->hdir)
22321 +               goto out;
22322 +
22323 +       /* even if an error happens later, keep this lock */
22324 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22325 +
22326 +       err = -EBUSY;
22327 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22328 +               goto out;
22329 +
22330 +       err = 0;
22331 +       if (p->h_dentry)
22332 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22333 +                                 p->h_parent, p->br);
22334 +
22335 +out:
22336 +       return err;
22337 +}
22338 +
22339 +int au_pin_hdir_relock(struct au_pin *p)
22340 +{
22341 +       int err, i;
22342 +       struct inode *h_i;
22343 +       struct dentry *h_d[] = {
22344 +               p->h_dentry,
22345 +               p->h_parent
22346 +       };
22347 +
22348 +       err = au_pin_hdir_lock(p);
22349 +       if (unlikely(err))
22350 +               goto out;
22351 +
22352 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22353 +               if (!h_d[i])
22354 +                       continue;
22355 +               if (d_is_positive(h_d[i])) {
22356 +                       h_i = d_inode(h_d[i]);
22357 +                       err = !h_i->i_nlink;
22358 +               }
22359 +       }
22360 +
22361 +out:
22362 +       return err;
22363 +}
22364 +
22365 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22366 +{
22367 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22368 +}
22369 +
22370 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22371 +{
22372 +       if (p->hdir) {
22373 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22374 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22375 +               au_pin_hdir_set_owner(p, current);
22376 +       }
22377 +}
22378 +
22379 +void au_pin_hdir_release(struct au_pin *p)
22380 +{
22381 +       if (p->hdir) {
22382 +               au_pin_hdir_set_owner(p, p->task);
22383 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22384 +       }
22385 +}
22386 +
22387 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22388 +{
22389 +       if (pin && pin->parent)
22390 +               return au_h_dptr(pin->parent, pin->bindex);
22391 +       return NULL;
22392 +}
22393 +
22394 +void au_unpin(struct au_pin *p)
22395 +{
22396 +       if (p->hdir)
22397 +               au_pin_hdir_unlock(p);
22398 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22399 +               vfsub_mnt_drop_write(p->h_mnt);
22400 +       if (!p->hdir)
22401 +               return;
22402 +
22403 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22404 +               di_read_unlock(p->parent, AuLock_IR);
22405 +       iput(p->hdir->hi_inode);
22406 +       dput(p->parent);
22407 +       p->parent = NULL;
22408 +       p->hdir = NULL;
22409 +       p->h_mnt = NULL;
22410 +       /* do not clear p->task */
22411 +}
22412 +
22413 +int au_do_pin(struct au_pin *p)
22414 +{
22415 +       int err;
22416 +       struct super_block *sb;
22417 +       struct inode *h_dir;
22418 +
22419 +       err = 0;
22420 +       sb = p->dentry->d_sb;
22421 +       p->br = au_sbr(sb, p->bindex);
22422 +       if (IS_ROOT(p->dentry)) {
22423 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22424 +                       p->h_mnt = au_br_mnt(p->br);
22425 +                       err = vfsub_mnt_want_write(p->h_mnt);
22426 +                       if (unlikely(err)) {
22427 +                               au_fclr_pin(p->flags, MNT_WRITE);
22428 +                               goto out_err;
22429 +                       }
22430 +               }
22431 +               goto out;
22432 +       }
22433 +
22434 +       p->h_dentry = NULL;
22435 +       if (p->bindex <= au_dbbot(p->dentry))
22436 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22437 +
22438 +       p->parent = dget_parent(p->dentry);
22439 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22440 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22441 +
22442 +       h_dir = NULL;
22443 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22444 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22445 +       if (p->hdir)
22446 +               h_dir = p->hdir->hi_inode;
22447 +
22448 +       /*
22449 +        * udba case, or
22450 +        * if DI_LOCKED is not set, then p->parent may be different
22451 +        * and h_parent can be NULL.
22452 +        */
22453 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22454 +               err = -EBUSY;
22455 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22456 +                       di_read_unlock(p->parent, AuLock_IR);
22457 +               dput(p->parent);
22458 +               p->parent = NULL;
22459 +               goto out_err;
22460 +       }
22461 +
22462 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22463 +               p->h_mnt = au_br_mnt(p->br);
22464 +               err = vfsub_mnt_want_write(p->h_mnt);
22465 +               if (unlikely(err)) {
22466 +                       au_fclr_pin(p->flags, MNT_WRITE);
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 +
22475 +       au_igrab(h_dir);
22476 +       err = au_pin_hdir_lock(p);
22477 +       if (!err)
22478 +               goto out; /* success */
22479 +
22480 +       au_unpin(p);
22481 +
22482 +out_err:
22483 +       pr_err("err %d\n", err);
22484 +       err = au_busy_or_stale();
22485 +out:
22486 +       return err;
22487 +}
22488 +
22489 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22490 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22491 +                unsigned int udba, unsigned char flags)
22492 +{
22493 +       p->dentry = dentry;
22494 +       p->udba = udba;
22495 +       p->lsc_di = lsc_di;
22496 +       p->lsc_hi = lsc_hi;
22497 +       p->flags = flags;
22498 +       p->bindex = bindex;
22499 +
22500 +       p->parent = NULL;
22501 +       p->hdir = NULL;
22502 +       p->h_mnt = NULL;
22503 +
22504 +       p->h_dentry = NULL;
22505 +       p->h_parent = NULL;
22506 +       p->br = NULL;
22507 +       p->task = current;
22508 +}
22509 +
22510 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22511 +          unsigned int udba, unsigned char flags)
22512 +{
22513 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22514 +                   udba, flags);
22515 +       return au_do_pin(pin);
22516 +}
22517 +
22518 +/* ---------------------------------------------------------------------- */
22519 +
22520 +/*
22521 + * ->setattr() and ->getattr() are called in various cases.
22522 + * chmod, stat: dentry is revalidated.
22523 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22524 + *               unhashed.
22525 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22526 + */
22527 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22528 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22529 +{
22530 +       int err;
22531 +       struct dentry *parent;
22532 +
22533 +       err = 0;
22534 +       if (au_digen_test(dentry, sigen)) {
22535 +               parent = dget_parent(dentry);
22536 +               di_read_lock_parent(parent, AuLock_IR);
22537 +               err = au_refresh_dentry(dentry, parent);
22538 +               di_read_unlock(parent, AuLock_IR);
22539 +               dput(parent);
22540 +       }
22541 +
22542 +       AuTraceErr(err);
22543 +       return err;
22544 +}
22545 +
22546 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22547 +                    struct au_icpup_args *a)
22548 +{
22549 +       int err;
22550 +       loff_t sz;
22551 +       aufs_bindex_t btop, ibtop;
22552 +       struct dentry *hi_wh, *parent;
22553 +       struct inode *inode;
22554 +       struct au_wr_dir_args wr_dir_args = {
22555 +               .force_btgt     = -1,
22556 +               .flags          = 0
22557 +       };
22558 +
22559 +       if (d_is_dir(dentry))
22560 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22561 +       /* plink or hi_wh() case */
22562 +       btop = au_dbtop(dentry);
22563 +       inode = d_inode(dentry);
22564 +       ibtop = au_ibtop(inode);
22565 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22566 +               wr_dir_args.force_btgt = ibtop;
22567 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22568 +       if (unlikely(err < 0))
22569 +               goto out;
22570 +       a->btgt = err;
22571 +       if (err != btop)
22572 +               au_fset_icpup(a->flags, DID_CPUP);
22573 +
22574 +       err = 0;
22575 +       a->pin_flags = AuPin_MNT_WRITE;
22576 +       parent = NULL;
22577 +       if (!IS_ROOT(dentry)) {
22578 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22579 +               parent = dget_parent(dentry);
22580 +               di_write_lock_parent(parent);
22581 +       }
22582 +
22583 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22584 +       if (unlikely(err))
22585 +               goto out_parent;
22586 +
22587 +       sz = -1;
22588 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22589 +       a->h_inode = d_inode(a->h_path.dentry);
22590 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22591 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22592 +               if (ia->ia_size < i_size_read(a->h_inode))
22593 +                       sz = ia->ia_size;
22594 +               inode_unlock_shared(a->h_inode);
22595 +       }
22596 +
22597 +       hi_wh = NULL;
22598 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22599 +               hi_wh = au_hi_wh(inode, a->btgt);
22600 +               if (!hi_wh) {
22601 +                       struct au_cp_generic cpg = {
22602 +                               .dentry = dentry,
22603 +                               .bdst   = a->btgt,
22604 +                               .bsrc   = -1,
22605 +                               .len    = sz,
22606 +                               .pin    = &a->pin
22607 +                       };
22608 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22609 +                       if (unlikely(err))
22610 +                               goto out_unlock;
22611 +                       hi_wh = au_hi_wh(inode, a->btgt);
22612 +                       /* todo: revalidate hi_wh? */
22613 +               }
22614 +       }
22615 +
22616 +       if (parent) {
22617 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22618 +               di_downgrade_lock(parent, AuLock_IR);
22619 +               dput(parent);
22620 +               parent = NULL;
22621 +       }
22622 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22623 +               goto out; /* success */
22624 +
22625 +       if (!d_unhashed(dentry)) {
22626 +               struct au_cp_generic cpg = {
22627 +                       .dentry = dentry,
22628 +                       .bdst   = a->btgt,
22629 +                       .bsrc   = btop,
22630 +                       .len    = sz,
22631 +                       .pin    = &a->pin,
22632 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22633 +               };
22634 +               err = au_sio_cpup_simple(&cpg);
22635 +               if (!err)
22636 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22637 +       } else if (!hi_wh)
22638 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22639 +       else
22640 +               a->h_path.dentry = hi_wh; /* do not dget here */
22641 +
22642 +out_unlock:
22643 +       a->h_inode = d_inode(a->h_path.dentry);
22644 +       if (!err)
22645 +               goto out; /* success */
22646 +       au_unpin(&a->pin);
22647 +out_parent:
22648 +       if (parent) {
22649 +               di_write_unlock(parent);
22650 +               dput(parent);
22651 +       }
22652 +out:
22653 +       if (!err)
22654 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22655 +       return err;
22656 +}
22657 +
22658 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
22659 +                       struct iattr *ia)
22660 +{
22661 +       int err;
22662 +       struct inode *inode, *delegated;
22663 +       struct super_block *sb;
22664 +       struct file *file;
22665 +       struct au_icpup_args *a;
22666 +       struct user_namespace *h_userns;
22667 +
22668 +       inode = d_inode(dentry);
22669 +       IMustLock(inode);
22670 +
22671 +       err = setattr_prepare(userns, dentry, ia);
22672 +       if (unlikely(err))
22673 +               goto out;
22674 +
22675 +       err = -ENOMEM;
22676 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22677 +       if (unlikely(!a))
22678 +               goto out;
22679 +
22680 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22681 +               ia->ia_valid &= ~ATTR_MODE;
22682 +
22683 +       file = NULL;
22684 +       sb = dentry->d_sb;
22685 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22686 +       if (unlikely(err))
22687 +               goto out_kfree;
22688 +
22689 +       if (ia->ia_valid & ATTR_FILE) {
22690 +               /* currently ftruncate(2) only */
22691 +               AuDebugOn(!d_is_reg(dentry));
22692 +               file = ia->ia_file;
22693 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22694 +                                           /*fi_lsc*/0);
22695 +               if (unlikely(err))
22696 +                       goto out_si;
22697 +               ia->ia_file = au_hf_top(file);
22698 +               a->udba = AuOpt_UDBA_NONE;
22699 +       } else {
22700 +               /* fchmod() doesn't pass ia_file */
22701 +               a->udba = au_opt_udba(sb);
22702 +               di_write_lock_child(dentry);
22703 +               /* no d_unlinked(), to set UDBA_NONE for root */
22704 +               if (d_unhashed(dentry))
22705 +                       a->udba = AuOpt_UDBA_NONE;
22706 +               if (a->udba != AuOpt_UDBA_NONE) {
22707 +                       AuDebugOn(IS_ROOT(dentry));
22708 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22709 +                       if (unlikely(err))
22710 +                               goto out_dentry;
22711 +               }
22712 +       }
22713 +
22714 +       err = au_pin_and_icpup(dentry, ia, a);
22715 +       if (unlikely(err < 0))
22716 +               goto out_dentry;
22717 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22718 +               ia->ia_file = NULL;
22719 +               ia->ia_valid &= ~ATTR_FILE;
22720 +       }
22721 +
22722 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22723 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22724 +           == (ATTR_MODE | ATTR_CTIME)) {
22725 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22726 +               if (unlikely(err))
22727 +                       goto out_unlock;
22728 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22729 +                  && (ia->ia_valid & ATTR_CTIME)) {
22730 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22731 +               if (unlikely(err))
22732 +                       goto out_unlock;
22733 +       }
22734 +
22735 +       if (ia->ia_valid & ATTR_SIZE) {
22736 +               struct file *f;
22737 +
22738 +               if (ia->ia_size < i_size_read(inode))
22739 +                       /* unmap only */
22740 +                       truncate_setsize(inode, ia->ia_size);
22741 +
22742 +               f = NULL;
22743 +               if (ia->ia_valid & ATTR_FILE)
22744 +                       f = ia->ia_file;
22745 +               inode_unlock(a->h_inode);
22746 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22747 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22748 +       } else {
22749 +               delegated = NULL;
22750 +               while (1) {
22751 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22752 +                       if (delegated) {
22753 +                               err = break_deleg_wait(&delegated);
22754 +                               if (!err)
22755 +                                       continue;
22756 +                       }
22757 +                       break;
22758 +               }
22759 +       }
22760 +       /*
22761 +        * regardless aufs 'acl' option setting.
22762 +        * why don't all acl-aware fs call this func from their ->setattr()?
22763 +        */
22764 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22765 +               h_userns = mnt_user_ns(a->h_path.mnt);
22766 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
22767 +       }
22768 +       if (!err)
22769 +               au_cpup_attr_changeable(inode);
22770 +
22771 +out_unlock:
22772 +       inode_unlock(a->h_inode);
22773 +       au_unpin(&a->pin);
22774 +       if (unlikely(err))
22775 +               au_update_dbtop(dentry);
22776 +out_dentry:
22777 +       di_write_unlock(dentry);
22778 +       if (file) {
22779 +               fi_write_unlock(file);
22780 +               ia->ia_file = file;
22781 +               ia->ia_valid |= ATTR_FILE;
22782 +       }
22783 +out_si:
22784 +       si_read_unlock(sb);
22785 +out_kfree:
22786 +       au_kfree_rcu(a);
22787 +out:
22788 +       AuTraceErr(err);
22789 +       return err;
22790 +}
22791 +
22792 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22793 +static int au_h_path_to_set_attr(struct dentry *dentry,
22794 +                                struct au_icpup_args *a, struct path *h_path)
22795 +{
22796 +       int err;
22797 +       struct super_block *sb;
22798 +
22799 +       sb = dentry->d_sb;
22800 +       a->udba = au_opt_udba(sb);
22801 +       /* no d_unlinked(), to set UDBA_NONE for root */
22802 +       if (d_unhashed(dentry))
22803 +               a->udba = AuOpt_UDBA_NONE;
22804 +       if (a->udba != AuOpt_UDBA_NONE) {
22805 +               AuDebugOn(IS_ROOT(dentry));
22806 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22807 +               if (unlikely(err))
22808 +                       goto out;
22809 +       }
22810 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22811 +       if (unlikely(err < 0))
22812 +               goto out;
22813 +
22814 +       h_path->dentry = a->h_path.dentry;
22815 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22816 +
22817 +out:
22818 +       return err;
22819 +}
22820 +
22821 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22822 +                 struct au_sxattr *arg)
22823 +{
22824 +       int err;
22825 +       struct path h_path;
22826 +       struct super_block *sb;
22827 +       struct au_icpup_args *a;
22828 +       struct inode *h_inode;
22829 +       struct user_namespace *h_userns;
22830 +
22831 +       IMustLock(inode);
22832 +
22833 +       err = -ENOMEM;
22834 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22835 +       if (unlikely(!a))
22836 +               goto out;
22837 +
22838 +       sb = dentry->d_sb;
22839 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22840 +       if (unlikely(err))
22841 +               goto out_kfree;
22842 +
22843 +       h_path.dentry = NULL;   /* silence gcc */
22844 +       di_write_lock_child(dentry);
22845 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22846 +       if (unlikely(err))
22847 +               goto out_di;
22848 +       h_userns = mnt_user_ns(h_path.mnt);
22849 +
22850 +       inode_unlock(a->h_inode);
22851 +       switch (arg->type) {
22852 +       case AU_XATTR_SET:
22853 +               AuDebugOn(d_is_negative(h_path.dentry));
22854 +               err = vfsub_setxattr(h_userns, h_path.dentry,
22855 +                                    arg->u.set.name, arg->u.set.value,
22856 +                                    arg->u.set.size, arg->u.set.flags);
22857 +               break;
22858 +       case AU_ACL_SET:
22859 +               err = -EOPNOTSUPP;
22860 +               h_inode = d_inode(h_path.dentry);
22861 +               if (h_inode->i_op->set_acl) {
22862 +                       /* this will call posix_acl_update_mode */
22863 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
22864 +                                                    arg->u.acl_set.acl,
22865 +                                                    arg->u.acl_set.type);
22866 +               }
22867 +               break;
22868 +       }
22869 +       if (!err)
22870 +               au_cpup_attr_timesizes(inode);
22871 +
22872 +       au_unpin(&a->pin);
22873 +       if (unlikely(err))
22874 +               au_update_dbtop(dentry);
22875 +
22876 +out_di:
22877 +       di_write_unlock(dentry);
22878 +       si_read_unlock(sb);
22879 +out_kfree:
22880 +       au_kfree_rcu(a);
22881 +out:
22882 +       AuTraceErr(err);
22883 +       return err;
22884 +}
22885 +#endif
22886 +
22887 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22888 +                            unsigned int nlink)
22889 +{
22890 +       unsigned int n;
22891 +
22892 +       inode->i_mode = st->mode;
22893 +       /* don't i_[ug]id_write() here */
22894 +       inode->i_uid = st->uid;
22895 +       inode->i_gid = st->gid;
22896 +       inode->i_atime = st->atime;
22897 +       inode->i_mtime = st->mtime;
22898 +       inode->i_ctime = st->ctime;
22899 +
22900 +       au_cpup_attr_nlink(inode, /*force*/0);
22901 +       if (S_ISDIR(inode->i_mode)) {
22902 +               n = inode->i_nlink;
22903 +               n -= nlink;
22904 +               n += st->nlink;
22905 +               smp_mb(); /* for i_nlink */
22906 +               /* 0 can happen */
22907 +               set_nlink(inode, n);
22908 +       }
22909 +
22910 +       spin_lock(&inode->i_lock);
22911 +       inode->i_blocks = st->blocks;
22912 +       i_size_write(inode, st->size);
22913 +       spin_unlock(&inode->i_lock);
22914 +}
22915 +
22916 +/*
22917 + * common routine for aufs_getattr() and au_getxattr().
22918 + * returns zero or negative (an error).
22919 + * @dentry will be read-locked in success.
22920 + */
22921 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22922 +                     struct path *h_path, int locked)
22923 +{
22924 +       int err;
22925 +       unsigned int mnt_flags, sigen;
22926 +       unsigned char udba_none;
22927 +       aufs_bindex_t bindex;
22928 +       struct super_block *sb, *h_sb;
22929 +
22930 +       h_path->mnt = NULL;
22931 +       h_path->dentry = NULL;
22932 +
22933 +       err = 0;
22934 +       sb = dentry->d_sb;
22935 +       mnt_flags = au_mntflags(sb);
22936 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22937 +
22938 +       if (unlikely(locked))
22939 +               goto body; /* skip locking dinfo */
22940 +
22941 +       /* support fstat(2) */
22942 +       if (!d_unlinked(dentry) && !udba_none) {
22943 +               sigen = au_sigen(sb);
22944 +               err = au_digen_test(dentry, sigen);
22945 +               if (!err) {
22946 +                       di_read_lock_child(dentry, AuLock_IR);
22947 +                       err = au_dbrange_test(dentry);
22948 +                       if (unlikely(err)) {
22949 +                               di_read_unlock(dentry, AuLock_IR);
22950 +                               goto out;
22951 +                       }
22952 +               } else {
22953 +                       AuDebugOn(IS_ROOT(dentry));
22954 +                       di_write_lock_child(dentry);
22955 +                       err = au_dbrange_test(dentry);
22956 +                       if (!err)
22957 +                               err = au_reval_for_attr(dentry, sigen);
22958 +                       if (!err)
22959 +                               di_downgrade_lock(dentry, AuLock_IR);
22960 +                       else {
22961 +                               di_write_unlock(dentry);
22962 +                               goto out;
22963 +                       }
22964 +               }
22965 +       } else
22966 +               di_read_lock_child(dentry, AuLock_IR);
22967 +
22968 +body:
22969 +       if (!inode) {
22970 +               inode = d_inode(dentry);
22971 +               if (unlikely(!inode))
22972 +                       goto out;
22973 +       }
22974 +       bindex = au_ibtop(inode);
22975 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22976 +       h_sb = h_path->mnt->mnt_sb;
22977 +       if (!force
22978 +           && !au_test_fs_bad_iattr(h_sb)
22979 +           && udba_none)
22980 +               goto out; /* success */
22981 +
22982 +       if (au_dbtop(dentry) == bindex)
22983 +               h_path->dentry = au_h_dptr(dentry, bindex);
22984 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22985 +               h_path->dentry = au_plink_lkup(inode, bindex);
22986 +               if (IS_ERR(h_path->dentry))
22987 +                       /* pretending success */
22988 +                       h_path->dentry = NULL;
22989 +               else
22990 +                       dput(h_path->dentry);
22991 +       }
22992 +
22993 +out:
22994 +       return err;
22995 +}
22996 +
22997 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
22998 +                       struct kstat *st, u32 request, unsigned int query)
22999 +{
23000 +       int err;
23001 +       unsigned char positive;
23002 +       struct path h_path;
23003 +       struct dentry *dentry;
23004 +       struct inode *inode;
23005 +       struct super_block *sb;
23006 +
23007 +       dentry = path->dentry;
23008 +       inode = d_inode(dentry);
23009 +       sb = dentry->d_sb;
23010 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
23011 +       if (unlikely(err))
23012 +               goto out;
23013 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
23014 +                               /*locked*/0);
23015 +       if (unlikely(err))
23016 +               goto out_si;
23017 +       if (unlikely(!h_path.dentry))
23018 +               /* illegally overlapped or something */
23019 +               goto out_fill; /* pretending success */
23020 +
23021 +       positive = d_is_positive(h_path.dentry);
23022 +       if (positive)
23023 +               /* no vfsub version */
23024 +               err = vfs_getattr(&h_path, st, request, query);
23025 +       if (!err) {
23026 +               if (positive)
23027 +                       au_refresh_iattr(inode, st,
23028 +                                        d_inode(h_path.dentry)->i_nlink);
23029 +               goto out_fill; /* success */
23030 +       }
23031 +       AuTraceErr(err);
23032 +       goto out_di;
23033 +
23034 +out_fill:
23035 +       generic_fillattr(userns, inode, st);
23036 +out_di:
23037 +       di_read_unlock(dentry, AuLock_IR);
23038 +out_si:
23039 +       si_read_unlock(sb);
23040 +out:
23041 +       AuTraceErr(err);
23042 +       return err;
23043 +}
23044 +
23045 +/* ---------------------------------------------------------------------- */
23046 +
23047 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23048 +                                struct delayed_call *done)
23049 +{
23050 +       const char *ret;
23051 +       struct dentry *h_dentry;
23052 +       struct inode *h_inode;
23053 +       int err;
23054 +       aufs_bindex_t bindex;
23055 +
23056 +       ret = NULL; /* suppress a warning */
23057 +       err = -ECHILD;
23058 +       if (!dentry)
23059 +               goto out;
23060 +
23061 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23062 +       if (unlikely(err))
23063 +               goto out;
23064 +
23065 +       err = au_d_hashed_positive(dentry);
23066 +       if (unlikely(err))
23067 +               goto out_unlock;
23068 +
23069 +       err = -EINVAL;
23070 +       inode = d_inode(dentry);
23071 +       bindex = au_ibtop(inode);
23072 +       h_inode = au_h_iptr(inode, bindex);
23073 +       if (unlikely(!h_inode->i_op->get_link))
23074 +               goto out_unlock;
23075 +
23076 +       err = -EBUSY;
23077 +       h_dentry = NULL;
23078 +       if (au_dbtop(dentry) <= bindex) {
23079 +               h_dentry = au_h_dptr(dentry, bindex);
23080 +               if (h_dentry)
23081 +                       dget(h_dentry);
23082 +       }
23083 +       if (!h_dentry) {
23084 +               h_dentry = d_find_any_alias(h_inode);
23085 +               if (IS_ERR(h_dentry)) {
23086 +                       err = PTR_ERR(h_dentry);
23087 +                       goto out_unlock;
23088 +               }
23089 +       }
23090 +       if (unlikely(!h_dentry))
23091 +               goto out_unlock;
23092 +
23093 +       err = 0;
23094 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23095 +       AuDbgDentry(h_dentry);
23096 +       ret = vfs_get_link(h_dentry, done);
23097 +       dput(h_dentry);
23098 +       if (IS_ERR(ret))
23099 +               err = PTR_ERR(ret);
23100 +
23101 +out_unlock:
23102 +       aufs_read_unlock(dentry, AuLock_IR);
23103 +out:
23104 +       if (unlikely(err))
23105 +               ret = ERR_PTR(err);
23106 +       AuTraceErrPtr(ret);
23107 +       return ret;
23108 +}
23109 +
23110 +/* ---------------------------------------------------------------------- */
23111 +
23112 +static int au_is_special(struct inode *inode)
23113 +{
23114 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23115 +}
23116 +
23117 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23118 +                           int flags)
23119 +{
23120 +       int err;
23121 +       aufs_bindex_t bindex;
23122 +       struct super_block *sb;
23123 +       struct inode *h_inode;
23124 +       struct vfsmount *h_mnt;
23125 +
23126 +       sb = inode->i_sb;
23127 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23128 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23129 +
23130 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23131 +       lockdep_off();
23132 +       si_read_lock(sb, AuLock_FLUSH);
23133 +       ii_write_lock_child(inode);
23134 +
23135 +       err = 0;
23136 +       bindex = au_ibtop(inode);
23137 +       h_inode = au_h_iptr(inode, bindex);
23138 +       if (!au_test_ro(sb, bindex, inode)) {
23139 +               h_mnt = au_sbr_mnt(sb, bindex);
23140 +               err = vfsub_mnt_want_write(h_mnt);
23141 +               if (!err) {
23142 +                       err = vfsub_update_time(h_inode, ts, flags);
23143 +                       vfsub_mnt_drop_write(h_mnt);
23144 +               }
23145 +       } else if (au_is_special(h_inode)) {
23146 +               /*
23147 +                * Never copy-up here.
23148 +                * These special files may already be opened and used for
23149 +                * communicating. If we copied it up, then the communication
23150 +                * would be corrupted.
23151 +                */
23152 +               AuWarn1("timestamps for i%lu are ignored "
23153 +                       "since it is on readonly branch (hi%lu).\n",
23154 +                       inode->i_ino, h_inode->i_ino);
23155 +       } else if (flags & ~S_ATIME) {
23156 +               err = -EIO;
23157 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23158 +               AuDebugOn(1);
23159 +       }
23160 +
23161 +       if (!err)
23162 +               au_cpup_attr_timesizes(inode);
23163 +       ii_write_unlock(inode);
23164 +       si_read_unlock(sb);
23165 +       lockdep_on();
23166 +
23167 +       if (!err && (flags & S_VERSION))
23168 +               inode_inc_iversion(inode);
23169 +
23170 +       return err;
23171 +}
23172 +
23173 +/* ---------------------------------------------------------------------- */
23174 +
23175 +/* no getattr version will be set by module.c:aufs_init() */
23176 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23177 +       aufs_iop[] = {
23178 +       [AuIop_SYMLINK] = {
23179 +               .permission     = aufs_permission,
23180 +#ifdef CONFIG_FS_POSIX_ACL
23181 +               .get_acl        = aufs_get_acl,
23182 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23183 +#endif
23184 +
23185 +               .setattr        = aufs_setattr,
23186 +               .getattr        = aufs_getattr,
23187 +
23188 +#ifdef CONFIG_AUFS_XATTR
23189 +               .listxattr      = aufs_listxattr,
23190 +#endif
23191 +
23192 +               .get_link       = aufs_get_link
23193 +
23194 +               /* .update_time = aufs_update_time */
23195 +       },
23196 +       [AuIop_DIR] = {
23197 +               .create         = aufs_create,
23198 +               .lookup         = aufs_lookup,
23199 +               .link           = aufs_link,
23200 +               .unlink         = aufs_unlink,
23201 +               .symlink        = aufs_symlink,
23202 +               .mkdir          = aufs_mkdir,
23203 +               .rmdir          = aufs_rmdir,
23204 +               .mknod          = aufs_mknod,
23205 +               .rename         = aufs_rename,
23206 +
23207 +               .permission     = aufs_permission,
23208 +#ifdef CONFIG_FS_POSIX_ACL
23209 +               .get_acl        = aufs_get_acl,
23210 +               .set_acl        = aufs_set_acl,
23211 +#endif
23212 +
23213 +               .setattr        = aufs_setattr,
23214 +               .getattr        = aufs_getattr,
23215 +
23216 +#ifdef CONFIG_AUFS_XATTR
23217 +               .listxattr      = aufs_listxattr,
23218 +#endif
23219 +
23220 +               .update_time    = aufs_update_time,
23221 +               .atomic_open    = aufs_atomic_open,
23222 +               .tmpfile        = aufs_tmpfile
23223 +       },
23224 +       [AuIop_OTHER] = {
23225 +               .permission     = aufs_permission,
23226 +#ifdef CONFIG_FS_POSIX_ACL
23227 +               .get_acl        = aufs_get_acl,
23228 +               .set_acl        = aufs_set_acl,
23229 +#endif
23230 +
23231 +               .setattr        = aufs_setattr,
23232 +               .getattr        = aufs_getattr,
23233 +
23234 +#ifdef CONFIG_AUFS_XATTR
23235 +               .listxattr      = aufs_listxattr,
23236 +#endif
23237 +
23238 +               .update_time    = aufs_update_time
23239 +       }
23240 +};
23241 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23242 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23243 +++ linux/fs/aufs/i_op_del.c    2022-11-05 23:02:18.965889284 +0100
23244 @@ -0,0 +1,522 @@
23245 +// SPDX-License-Identifier: GPL-2.0
23246 +/*
23247 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23248 + *
23249 + * This program is free software; you can redistribute it and/or modify
23250 + * it under the terms of the GNU General Public License as published by
23251 + * the Free Software Foundation; either version 2 of the License, or
23252 + * (at your option) any later version.
23253 + *
23254 + * This program is distributed in the hope that it will be useful,
23255 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23256 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23257 + * GNU General Public License for more details.
23258 + *
23259 + * You should have received a copy of the GNU General Public License
23260 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23261 + */
23262 +
23263 +/*
23264 + * inode operations (del entry)
23265 + */
23266 +
23267 +#include <linux/iversion.h>
23268 +#include "aufs.h"
23269 +
23270 +/*
23271 + * decide if a new whiteout for @dentry is necessary or not.
23272 + * when it is necessary, prepare the parent dir for the upper branch whose
23273 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23274 + * be done by caller.
23275 + * return value:
23276 + * 0: wh is unnecessary
23277 + * plus: wh is necessary
23278 + * minus: error
23279 + */
23280 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23281 +{
23282 +       int need_wh, err;
23283 +       aufs_bindex_t btop;
23284 +       struct super_block *sb;
23285 +
23286 +       sb = dentry->d_sb;
23287 +       btop = au_dbtop(dentry);
23288 +       if (*bcpup < 0) {
23289 +               *bcpup = btop;
23290 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23291 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23292 +                       *bcpup = err;
23293 +                       if (unlikely(err < 0))
23294 +                               goto out;
23295 +               }
23296 +       } else
23297 +               AuDebugOn(btop < *bcpup
23298 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23299 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23300 +
23301 +       if (*bcpup != btop) {
23302 +               err = au_cpup_dirs(dentry, *bcpup);
23303 +               if (unlikely(err))
23304 +                       goto out;
23305 +               need_wh = 1;
23306 +       } else {
23307 +               struct au_dinfo *dinfo, *tmp;
23308 +
23309 +               need_wh = -ENOMEM;
23310 +               dinfo = au_di(dentry);
23311 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23312 +               if (tmp) {
23313 +                       au_di_cp(tmp, dinfo);
23314 +                       au_di_swap(tmp, dinfo);
23315 +                       /* returns the number of positive dentries */
23316 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23317 +                                                /* AuLkup_IGNORE_PERM */ 0);
23318 +                       au_di_swap(tmp, dinfo);
23319 +                       au_rw_write_unlock(&tmp->di_rwsem);
23320 +                       au_di_free(tmp);
23321 +               }
23322 +       }
23323 +       AuDbg("need_wh %d\n", need_wh);
23324 +       err = need_wh;
23325 +
23326 +out:
23327 +       return err;
23328 +}
23329 +
23330 +/*
23331 + * simple tests for the del-entry operations.
23332 + * following the checks in vfs, plus the parent-child relationship.
23333 + */
23334 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23335 +              struct dentry *h_parent, int isdir)
23336 +{
23337 +       int err;
23338 +       umode_t h_mode;
23339 +       struct dentry *h_dentry, *h_latest;
23340 +       struct inode *h_inode;
23341 +       struct path h_ppath;
23342 +       struct super_block *sb;
23343 +       struct au_branch *br;
23344 +       struct user_namespace *h_userns;
23345 +
23346 +       h_dentry = au_h_dptr(dentry, bindex);
23347 +       if (d_really_is_positive(dentry)) {
23348 +               err = -ENOENT;
23349 +               if (unlikely(d_is_negative(h_dentry)))
23350 +                       goto out;
23351 +               h_inode = d_inode(h_dentry);
23352 +               if (unlikely(!h_inode->i_nlink))
23353 +                       goto out;
23354 +
23355 +               h_mode = h_inode->i_mode;
23356 +               if (!isdir) {
23357 +                       err = -EISDIR;
23358 +                       if (unlikely(S_ISDIR(h_mode)))
23359 +                               goto out;
23360 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23361 +                       err = -ENOTDIR;
23362 +                       goto out;
23363 +               }
23364 +       } else {
23365 +               /* rename(2) case */
23366 +               err = -EIO;
23367 +               if (unlikely(d_is_positive(h_dentry)))
23368 +                       goto out;
23369 +       }
23370 +
23371 +       err = -ENOENT;
23372 +       /* expected parent dir is locked */
23373 +       if (unlikely(h_parent != h_dentry->d_parent))
23374 +               goto out;
23375 +       err = 0;
23376 +
23377 +       /*
23378 +        * rmdir a dir may break the consistency on some filesystem.
23379 +        * let's try heavy test.
23380 +        */
23381 +       err = -EACCES;
23382 +       sb = dentry->d_sb;
23383 +       br = au_sbr(sb, bindex);
23384 +       h_userns = au_br_userns(br);
23385 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23386 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
23387 +                                      MAY_EXEC | MAY_WRITE)))
23388 +               goto out;
23389 +
23390 +       h_ppath.dentry = h_parent;
23391 +       h_ppath.mnt = au_br_mnt(br);
23392 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
23393 +       err = -EIO;
23394 +       if (IS_ERR(h_latest))
23395 +               goto out;
23396 +       if (h_latest == h_dentry)
23397 +               err = 0;
23398 +       dput(h_latest);
23399 +
23400 +out:
23401 +       return err;
23402 +}
23403 +
23404 +/*
23405 + * decide the branch where we operate for @dentry. the branch index will be set
23406 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23407 + * dir for reverting.
23408 + * when a new whiteout is necessary, create it.
23409 + */
23410 +static struct dentry*
23411 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23412 +                   struct au_dtime *dt, struct au_pin *pin)
23413 +{
23414 +       struct dentry *wh_dentry;
23415 +       struct super_block *sb;
23416 +       struct path h_path;
23417 +       int err, need_wh;
23418 +       unsigned int udba;
23419 +       aufs_bindex_t bcpup;
23420 +
23421 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23422 +       wh_dentry = ERR_PTR(need_wh);
23423 +       if (unlikely(need_wh < 0))
23424 +               goto out;
23425 +
23426 +       sb = dentry->d_sb;
23427 +       udba = au_opt_udba(sb);
23428 +       bcpup = *rbcpup;
23429 +       err = au_pin(pin, dentry, bcpup, udba,
23430 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23431 +       wh_dentry = ERR_PTR(err);
23432 +       if (unlikely(err))
23433 +               goto out;
23434 +
23435 +       h_path.dentry = au_pinned_h_parent(pin);
23436 +       if (udba != AuOpt_UDBA_NONE
23437 +           && au_dbtop(dentry) == bcpup) {
23438 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23439 +               wh_dentry = ERR_PTR(err);
23440 +               if (unlikely(err))
23441 +                       goto out_unpin;
23442 +       }
23443 +
23444 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23445 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23446 +       wh_dentry = NULL;
23447 +       if (!need_wh)
23448 +               goto out; /* success, no need to create whiteout */
23449 +
23450 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23451 +       if (IS_ERR(wh_dentry))
23452 +               goto out_unpin;
23453 +
23454 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23455 +       goto out; /* success */
23456 +
23457 +out_unpin:
23458 +       au_unpin(pin);
23459 +out:
23460 +       return wh_dentry;
23461 +}
23462 +
23463 +/*
23464 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23465 + * in order to be revertible and save time for removing many child whiteouts
23466 + * under the dir.
23467 + * returns 1 when there are too many child whiteout and caller should remove
23468 + * them asynchronously. returns 0 when the number of children is enough small to
23469 + * remove now or the branch fs is a remote fs.
23470 + * otherwise return an error.
23471 + */
23472 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23473 +                          struct au_nhash *whlist, struct inode *dir)
23474 +{
23475 +       int rmdir_later, err, dirwh;
23476 +       struct dentry *h_dentry;
23477 +       struct super_block *sb;
23478 +       struct inode *inode;
23479 +
23480 +       sb = dentry->d_sb;
23481 +       SiMustAnyLock(sb);
23482 +       h_dentry = au_h_dptr(dentry, bindex);
23483 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23484 +       if (unlikely(err))
23485 +               goto out;
23486 +
23487 +       /* stop monitoring */
23488 +       inode = d_inode(dentry);
23489 +       au_hn_free(au_hi(inode, bindex));
23490 +
23491 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23492 +               dirwh = au_sbi(sb)->si_dirwh;
23493 +               rmdir_later = (dirwh <= 1);
23494 +               if (!rmdir_later)
23495 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23496 +                                                             dirwh);
23497 +               if (rmdir_later)
23498 +                       return rmdir_later;
23499 +       }
23500 +
23501 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23502 +       if (unlikely(err)) {
23503 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23504 +                       h_dentry, bindex, err);
23505 +               err = 0;
23506 +       }
23507 +
23508 +out:
23509 +       AuTraceErr(err);
23510 +       return err;
23511 +}
23512 +
23513 +/*
23514 + * final procedure for deleting a entry.
23515 + * maintain dentry and iattr.
23516 + */
23517 +static void epilog(struct inode *dir, struct dentry *dentry,
23518 +                  aufs_bindex_t bindex)
23519 +{
23520 +       struct inode *inode;
23521 +
23522 +       inode = d_inode(dentry);
23523 +       d_drop(dentry);
23524 +       inode->i_ctime = dir->i_ctime;
23525 +
23526 +       au_dir_ts(dir, bindex);
23527 +       inode_inc_iversion(dir);
23528 +}
23529 +
23530 +/*
23531 + * when an error happened, remove the created whiteout and revert everything.
23532 + */
23533 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23534 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23535 +                    struct dentry *dentry, struct au_dtime *dt)
23536 +{
23537 +       int rerr;
23538 +       struct path h_path = {
23539 +               .dentry = wh_dentry,
23540 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23541 +       };
23542 +
23543 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23544 +       if (!rerr) {
23545 +               au_set_dbwh(dentry, bwh);
23546 +               au_dtime_revert(dt);
23547 +               return 0;
23548 +       }
23549 +
23550 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23551 +       return -EIO;
23552 +}
23553 +
23554 +/* ---------------------------------------------------------------------- */
23555 +
23556 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23557 +{
23558 +       int err;
23559 +       aufs_bindex_t bwh, bindex, btop;
23560 +       struct inode *inode, *h_dir, *delegated;
23561 +       struct dentry *parent, *wh_dentry;
23562 +       /* to reduce stack size */
23563 +       struct {
23564 +               struct au_dtime dt;
23565 +               struct au_pin pin;
23566 +               struct path h_path;
23567 +       } *a;
23568 +
23569 +       IMustLock(dir);
23570 +
23571 +       err = -ENOMEM;
23572 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23573 +       if (unlikely(!a))
23574 +               goto out;
23575 +
23576 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23577 +       if (unlikely(err))
23578 +               goto out_free;
23579 +       err = au_d_hashed_positive(dentry);
23580 +       if (unlikely(err))
23581 +               goto out_unlock;
23582 +       inode = d_inode(dentry);
23583 +       IMustLock(inode);
23584 +       err = -EISDIR;
23585 +       if (unlikely(d_is_dir(dentry)))
23586 +               goto out_unlock; /* possible? */
23587 +
23588 +       btop = au_dbtop(dentry);
23589 +       bwh = au_dbwh(dentry);
23590 +       bindex = -1;
23591 +       parent = dentry->d_parent; /* dir inode is locked */
23592 +       di_write_lock_parent(parent);
23593 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23594 +                                       &a->pin);
23595 +       err = PTR_ERR(wh_dentry);
23596 +       if (IS_ERR(wh_dentry))
23597 +               goto out_parent;
23598 +
23599 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23600 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23601 +       dget(a->h_path.dentry);
23602 +       if (bindex == btop) {
23603 +               h_dir = au_pinned_h_dir(&a->pin);
23604 +               delegated = NULL;
23605 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23606 +               if (unlikely(err == -EWOULDBLOCK)) {
23607 +                       pr_warn("cannot retry for NFSv4 delegation"
23608 +                               " for an internal unlink\n");
23609 +                       iput(delegated);
23610 +               }
23611 +       } else {
23612 +               /* dir inode is locked */
23613 +               h_dir = d_inode(wh_dentry->d_parent);
23614 +               IMustLock(h_dir);
23615 +               err = 0;
23616 +       }
23617 +
23618 +       if (!err) {
23619 +               vfsub_drop_nlink(inode);
23620 +               epilog(dir, dentry, bindex);
23621 +
23622 +               /* update target timestamps */
23623 +               if (bindex == btop) {
23624 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23625 +                       /*ignore*/
23626 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23627 +               } else
23628 +                       /* todo: this timestamp may be reverted later */
23629 +                       inode->i_ctime = h_dir->i_ctime;
23630 +               goto out_unpin; /* success */
23631 +       }
23632 +
23633 +       /* revert */
23634 +       if (wh_dentry) {
23635 +               int rerr;
23636 +
23637 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23638 +                                &a->dt);
23639 +               if (rerr)
23640 +                       err = rerr;
23641 +       }
23642 +
23643 +out_unpin:
23644 +       au_unpin(&a->pin);
23645 +       dput(wh_dentry);
23646 +       dput(a->h_path.dentry);
23647 +out_parent:
23648 +       di_write_unlock(parent);
23649 +out_unlock:
23650 +       aufs_read_unlock(dentry, AuLock_DW);
23651 +out_free:
23652 +       au_kfree_rcu(a);
23653 +out:
23654 +       return err;
23655 +}
23656 +
23657 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23658 +{
23659 +       int err, rmdir_later;
23660 +       aufs_bindex_t bwh, bindex, btop;
23661 +       struct inode *inode;
23662 +       struct dentry *parent, *wh_dentry, *h_dentry;
23663 +       struct au_whtmp_rmdir *args;
23664 +       /* to reduce stack size */
23665 +       struct {
23666 +               struct au_dtime dt;
23667 +               struct au_pin pin;
23668 +       } *a;
23669 +
23670 +       IMustLock(dir);
23671 +
23672 +       err = -ENOMEM;
23673 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23674 +       if (unlikely(!a))
23675 +               goto out;
23676 +
23677 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23678 +       if (unlikely(err))
23679 +               goto out_free;
23680 +       err = au_alive_dir(dentry);
23681 +       if (unlikely(err))
23682 +               goto out_unlock;
23683 +       inode = d_inode(dentry);
23684 +       IMustLock(inode);
23685 +       err = -ENOTDIR;
23686 +       if (unlikely(!d_is_dir(dentry)))
23687 +               goto out_unlock; /* possible? */
23688 +
23689 +       err = -ENOMEM;
23690 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23691 +       if (unlikely(!args))
23692 +               goto out_unlock;
23693 +
23694 +       parent = dentry->d_parent; /* dir inode is locked */
23695 +       di_write_lock_parent(parent);
23696 +       err = au_test_empty(dentry, &args->whlist);
23697 +       if (unlikely(err))
23698 +               goto out_parent;
23699 +
23700 +       btop = au_dbtop(dentry);
23701 +       bwh = au_dbwh(dentry);
23702 +       bindex = -1;
23703 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23704 +                                       &a->pin);
23705 +       err = PTR_ERR(wh_dentry);
23706 +       if (IS_ERR(wh_dentry))
23707 +               goto out_parent;
23708 +
23709 +       h_dentry = au_h_dptr(dentry, btop);
23710 +       dget(h_dentry);
23711 +       rmdir_later = 0;
23712 +       if (bindex == btop) {
23713 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23714 +               if (err > 0) {
23715 +                       rmdir_later = err;
23716 +                       err = 0;
23717 +               }
23718 +       } else {
23719 +               /* stop monitoring */
23720 +               au_hn_free(au_hi(inode, btop));
23721 +
23722 +               /* dir inode is locked */
23723 +               IMustLock(d_inode(wh_dentry->d_parent));
23724 +               err = 0;
23725 +       }
23726 +
23727 +       if (!err) {
23728 +               vfsub_dead_dir(inode);
23729 +               au_set_dbdiropq(dentry, -1);
23730 +               epilog(dir, dentry, bindex);
23731 +
23732 +               if (rmdir_later) {
23733 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23734 +                       args = NULL;
23735 +               }
23736 +
23737 +               goto out_unpin; /* success */
23738 +       }
23739 +
23740 +       /* revert */
23741 +       AuLabel(revert);
23742 +       if (wh_dentry) {
23743 +               int rerr;
23744 +
23745 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23746 +                                &a->dt);
23747 +               if (rerr)
23748 +                       err = rerr;
23749 +       }
23750 +
23751 +out_unpin:
23752 +       au_unpin(&a->pin);
23753 +       dput(wh_dentry);
23754 +       dput(h_dentry);
23755 +out_parent:
23756 +       di_write_unlock(parent);
23757 +       if (args)
23758 +               au_whtmp_rmdir_free(args);
23759 +out_unlock:
23760 +       aufs_read_unlock(dentry, AuLock_DW);
23761 +out_free:
23762 +       au_kfree_rcu(a);
23763 +out:
23764 +       AuTraceErr(err);
23765 +       return err;
23766 +}
23767 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23768 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23769 +++ linux/fs/aufs/i_op_ren.c    2022-11-05 23:02:18.965889284 +0100
23770 @@ -0,0 +1,1257 @@
23771 +// SPDX-License-Identifier: GPL-2.0
23772 +/*
23773 + * Copyright (C) 2005-2022 Junjiro R. Okajima
23774 + *
23775 + * This program is free software; you can redistribute it and/or modify
23776 + * it under the terms of the GNU General Public License as published by
23777 + * the Free Software Foundation; either version 2 of the License, or
23778 + * (at your option) any later version.
23779 + *
23780 + * This program is distributed in the hope that it will be useful,
23781 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23782 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23783 + * GNU General Public License for more details.
23784 + *
23785 + * You should have received a copy of the GNU General Public License
23786 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23787 + */
23788 +
23789 +/*
23790 + * inode operation (rename entry)
23791 + * todo: this is crazy monster
23792 + */
23793 +
23794 +#include <linux/iversion.h>
23795 +#include "aufs.h"
23796 +
23797 +enum { AuSRC, AuDST, AuSrcDst };
23798 +enum { AuPARENT, AuCHILD, AuParentChild };
23799 +
23800 +#define AuRen_ISDIR_SRC                1
23801 +#define AuRen_ISDIR_DST                (1 << 1)
23802 +#define AuRen_ISSAMEDIR                (1 << 2)
23803 +#define AuRen_WHSRC            (1 << 3)
23804 +#define AuRen_WHDST            (1 << 4)
23805 +#define AuRen_MNT_WRITE                (1 << 5)
23806 +#define AuRen_DT_DSTDIR                (1 << 6)
23807 +#define AuRen_DIROPQ_SRC       (1 << 7)
23808 +#define AuRen_DIROPQ_DST       (1 << 8)
23809 +#define AuRen_DIRREN           (1 << 9)
23810 +#define AuRen_DROPPED_SRC      (1 << 10)
23811 +#define AuRen_DROPPED_DST      (1 << 11)
23812 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23813 +#define au_fset_ren(flags, name) \
23814 +       do { (flags) |= AuRen_##name; } while (0)
23815 +#define au_fclr_ren(flags, name) \
23816 +       do { (flags) &= ~AuRen_##name; } while (0)
23817 +
23818 +#ifndef CONFIG_AUFS_DIRREN
23819 +#undef AuRen_DIRREN
23820 +#define AuRen_DIRREN           0
23821 +#endif
23822 +
23823 +struct au_ren_args {
23824 +       struct {
23825 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23826 +                       *wh_dentry;
23827 +               struct inode *dir, *inode;
23828 +               struct au_hinode *hdir, *hinode;
23829 +               struct au_dtime dt[AuParentChild];
23830 +               aufs_bindex_t btop, bdiropq;
23831 +       } sd[AuSrcDst];
23832 +
23833 +#define src_dentry     sd[AuSRC].dentry
23834 +#define src_dir                sd[AuSRC].dir
23835 +#define src_inode      sd[AuSRC].inode
23836 +#define src_h_dentry   sd[AuSRC].h_dentry
23837 +#define src_parent     sd[AuSRC].parent
23838 +#define src_h_parent   sd[AuSRC].h_parent
23839 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23840 +#define src_hdir       sd[AuSRC].hdir
23841 +#define src_hinode     sd[AuSRC].hinode
23842 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23843 +#define src_dt         sd[AuSRC].dt
23844 +#define src_btop       sd[AuSRC].btop
23845 +#define src_bdiropq    sd[AuSRC].bdiropq
23846 +
23847 +#define dst_dentry     sd[AuDST].dentry
23848 +#define dst_dir                sd[AuDST].dir
23849 +#define dst_inode      sd[AuDST].inode
23850 +#define dst_h_dentry   sd[AuDST].h_dentry
23851 +#define dst_parent     sd[AuDST].parent
23852 +#define dst_h_parent   sd[AuDST].h_parent
23853 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23854 +#define dst_hdir       sd[AuDST].hdir
23855 +#define dst_hinode     sd[AuDST].hinode
23856 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23857 +#define dst_dt         sd[AuDST].dt
23858 +#define dst_btop       sd[AuDST].btop
23859 +#define dst_bdiropq    sd[AuDST].bdiropq
23860 +
23861 +       struct dentry *h_trap;
23862 +       struct au_branch *br;
23863 +       struct path h_path;
23864 +       struct au_nhash whlist;
23865 +       aufs_bindex_t btgt, src_bwh;
23866 +
23867 +       struct {
23868 +               unsigned short auren_flags;
23869 +               unsigned char flags;    /* syscall parameter */
23870 +               unsigned char exchange;
23871 +       } __packed;
23872 +
23873 +       struct au_whtmp_rmdir *thargs;
23874 +       struct dentry *h_dst;
23875 +       struct au_hinode *h_root;
23876 +};
23877 +
23878 +/* ---------------------------------------------------------------------- */
23879 +
23880 +/*
23881 + * functions for reverting.
23882 + * when an error happened in a single rename systemcall, we should revert
23883 + * everything as if nothing happened.
23884 + * we don't need to revert the copied-up/down the parent dir since they are
23885 + * harmless.
23886 + */
23887 +
23888 +#define RevertFailure(fmt, ...) do { \
23889 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23890 +               ##__VA_ARGS__, err, rerr); \
23891 +       err = -EIO; \
23892 +} while (0)
23893 +
23894 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23895 +{
23896 +       int rerr;
23897 +       struct dentry *d;
23898 +#define src_or_dst(member) a->sd[idx].member
23899 +
23900 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23901 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23902 +       rerr = au_diropq_remove(d, a->btgt);
23903 +       au_hn_inode_unlock(src_or_dst(hinode));
23904 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23905 +       if (rerr)
23906 +               RevertFailure("remove diropq %pd", d);
23907 +
23908 +#undef src_or_dst_
23909 +}
23910 +
23911 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23912 +{
23913 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23914 +               au_ren_do_rev_diropq(err, a, AuSRC);
23915 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23916 +               au_ren_do_rev_diropq(err, a, AuDST);
23917 +}
23918 +
23919 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23920 +{
23921 +       int rerr;
23922 +       struct inode *delegated;
23923 +       struct path h_ppath = {
23924 +               .dentry = a->src_h_parent,
23925 +               .mnt    = a->h_path.mnt
23926 +       };
23927 +
23928 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23929 +       rerr = PTR_ERR(a->h_path.dentry);
23930 +       if (IS_ERR(a->h_path.dentry)) {
23931 +               RevertFailure("lkup one %pd", a->src_dentry);
23932 +               return;
23933 +       }
23934 +
23935 +       delegated = NULL;
23936 +       rerr = vfsub_rename(a->dst_h_dir,
23937 +                           au_h_dptr(a->src_dentry, a->btgt),
23938 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23939 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23940 +               pr_warn("cannot retry for NFSv4 delegation"
23941 +                       " for an internal rename\n");
23942 +               iput(delegated);
23943 +       }
23944 +       d_drop(a->h_path.dentry);
23945 +       dput(a->h_path.dentry);
23946 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23947 +       if (rerr)
23948 +               RevertFailure("rename %pd", a->src_dentry);
23949 +}
23950 +
23951 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23952 +{
23953 +       int rerr;
23954 +       struct inode *delegated;
23955 +       struct path h_ppath = {
23956 +               .dentry = a->dst_h_parent,
23957 +               .mnt    = a->h_path.mnt
23958 +       };
23959 +
23960 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23961 +       rerr = PTR_ERR(a->h_path.dentry);
23962 +       if (IS_ERR(a->h_path.dentry)) {
23963 +               RevertFailure("lkup one %pd", a->dst_dentry);
23964 +               return;
23965 +       }
23966 +       if (d_is_positive(a->h_path.dentry)) {
23967 +               d_drop(a->h_path.dentry);
23968 +               dput(a->h_path.dentry);
23969 +               return;
23970 +       }
23971 +
23972 +       delegated = NULL;
23973 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23974 +                           &delegated, a->flags);
23975 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23976 +               pr_warn("cannot retry for NFSv4 delegation"
23977 +                       " for an internal rename\n");
23978 +               iput(delegated);
23979 +       }
23980 +       d_drop(a->h_path.dentry);
23981 +       dput(a->h_path.dentry);
23982 +       if (!rerr)
23983 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23984 +       else
23985 +               RevertFailure("rename %pd", a->h_dst);
23986 +}
23987 +
23988 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23989 +{
23990 +       int rerr;
23991 +
23992 +       a->h_path.dentry = a->src_wh_dentry;
23993 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23994 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23995 +       if (rerr)
23996 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23997 +}
23998 +#undef RevertFailure
23999 +
24000 +/* ---------------------------------------------------------------------- */
24001 +
24002 +/*
24003 + * when we have to copyup the renaming entry, do it with the rename-target name
24004 + * in order to minimize the cost (the later actual rename is unnecessary).
24005 + * otherwise rename it on the target branch.
24006 + */
24007 +static int au_ren_or_cpup(struct au_ren_args *a)
24008 +{
24009 +       int err;
24010 +       struct dentry *d;
24011 +       struct inode *delegated;
24012 +
24013 +       d = a->src_dentry;
24014 +       if (au_dbtop(d) == a->btgt) {
24015 +               a->h_path.dentry = a->dst_h_dentry;
24016 +               AuDebugOn(au_dbtop(d) != a->btgt);
24017 +               delegated = NULL;
24018 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
24019 +                                  a->dst_h_dir, &a->h_path, &delegated,
24020 +                                  a->flags);
24021 +               if (unlikely(err == -EWOULDBLOCK)) {
24022 +                       pr_warn("cannot retry for NFSv4 delegation"
24023 +                               " for an internal rename\n");
24024 +                       iput(delegated);
24025 +               }
24026 +       } else
24027 +               BUG();
24028 +
24029 +       if (!err && a->h_dst)
24030 +               /* it will be set to dinfo later */
24031 +               dget(a->h_dst);
24032 +
24033 +       return err;
24034 +}
24035 +
24036 +/* cf. aufs_rmdir() */
24037 +static int au_ren_del_whtmp(struct au_ren_args *a)
24038 +{
24039 +       int err;
24040 +       struct inode *dir;
24041 +
24042 +       dir = a->dst_dir;
24043 +       SiMustAnyLock(dir->i_sb);
24044 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24045 +                                    au_sbi(dir->i_sb)->si_dirwh)
24046 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24047 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24048 +               if (unlikely(err))
24049 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24050 +                               "ignored.\n", a->h_dst, err);
24051 +       } else {
24052 +               au_nhash_wh_free(&a->thargs->whlist);
24053 +               a->thargs->whlist = a->whlist;
24054 +               a->whlist.nh_num = 0;
24055 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24056 +               dput(a->h_dst);
24057 +               a->thargs = NULL;
24058 +       }
24059 +
24060 +       return 0;
24061 +}
24062 +
24063 +/* make it 'opaque' dir. */
24064 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24065 +{
24066 +       int err;
24067 +       struct dentry *d, *diropq;
24068 +#define src_or_dst(member) a->sd[idx].member
24069 +
24070 +       err = 0;
24071 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24072 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24073 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24074 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24075 +       diropq = au_diropq_create(d, a->btgt);
24076 +       au_hn_inode_unlock(src_or_dst(hinode));
24077 +       if (IS_ERR(diropq))
24078 +               err = PTR_ERR(diropq);
24079 +       else
24080 +               dput(diropq);
24081 +
24082 +#undef src_or_dst_
24083 +       return err;
24084 +}
24085 +
24086 +static int au_ren_diropq(struct au_ren_args *a)
24087 +{
24088 +       int err;
24089 +       unsigned char always;
24090 +       struct dentry *d;
24091 +
24092 +       err = 0;
24093 +       d = a->dst_dentry; /* already renamed on the branch */
24094 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24095 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24096 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24097 +           && a->btgt != au_dbdiropq(a->src_dentry)
24098 +           && (a->dst_wh_dentry
24099 +               || a->btgt <= au_dbdiropq(d)
24100 +               /* hide the lower to keep xino */
24101 +               /* the lowers may not be a dir, but we hide them anyway */
24102 +               || a->btgt < au_dbbot(d)
24103 +               || always)) {
24104 +               AuDbg("here\n");
24105 +               err = au_ren_do_diropq(a, AuSRC);
24106 +               if (unlikely(err))
24107 +                       goto out;
24108 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24109 +       }
24110 +       if (!a->exchange)
24111 +               goto out; /* success */
24112 +
24113 +       d = a->src_dentry; /* already renamed on the branch */
24114 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24115 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24116 +           && (a->btgt < au_dbdiropq(d)
24117 +               || a->btgt < au_dbbot(d)
24118 +               || always)) {
24119 +               AuDbgDentry(a->src_dentry);
24120 +               AuDbgDentry(a->dst_dentry);
24121 +               err = au_ren_do_diropq(a, AuDST);
24122 +               if (unlikely(err))
24123 +                       goto out_rev_src;
24124 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24125 +       }
24126 +       goto out; /* success */
24127 +
24128 +out_rev_src:
24129 +       AuDbg("err %d, reverting src\n", err);
24130 +       au_ren_rev_diropq(err, a);
24131 +out:
24132 +       return err;
24133 +}
24134 +
24135 +static int do_rename(struct au_ren_args *a)
24136 +{
24137 +       int err;
24138 +       struct dentry *d, *h_d;
24139 +
24140 +       if (!a->exchange) {
24141 +               /* prepare workqueue args for asynchronous rmdir */
24142 +               h_d = a->dst_h_dentry;
24143 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24144 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24145 +                   && d_is_positive(h_d)) {
24146 +                       err = -ENOMEM;
24147 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24148 +                                                        GFP_NOFS);
24149 +                       if (unlikely(!a->thargs))
24150 +                               goto out;
24151 +                       a->h_dst = dget(h_d);
24152 +               }
24153 +
24154 +               /* create whiteout for src_dentry */
24155 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24156 +                       a->src_bwh = au_dbwh(a->src_dentry);
24157 +                       AuDebugOn(a->src_bwh >= 0);
24158 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24159 +                                                       a->src_h_parent);
24160 +                       err = PTR_ERR(a->src_wh_dentry);
24161 +                       if (IS_ERR(a->src_wh_dentry))
24162 +                               goto out_thargs;
24163 +               }
24164 +
24165 +               /* lookup whiteout for dentry */
24166 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24167 +                       h_d = au_wh_lkup(a->dst_h_parent,
24168 +                                        &a->dst_dentry->d_name, a->br);
24169 +                       err = PTR_ERR(h_d);
24170 +                       if (IS_ERR(h_d))
24171 +                               goto out_whsrc;
24172 +                       if (d_is_negative(h_d))
24173 +                               dput(h_d);
24174 +                       else
24175 +                               a->dst_wh_dentry = h_d;
24176 +               }
24177 +
24178 +               /* rename dentry to tmpwh */
24179 +               if (a->thargs) {
24180 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24181 +                       if (unlikely(err))
24182 +                               goto out_whdst;
24183 +
24184 +                       d = a->dst_dentry;
24185 +                       au_set_h_dptr(d, a->btgt, NULL);
24186 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24187 +                       if (unlikely(err))
24188 +                               goto out_whtmp;
24189 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24190 +               }
24191 +       }
24192 +
24193 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24194 +#if 0 /* debugging */
24195 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24196 +              && d_is_positive(a->dst_h_dentry)
24197 +              && a->src_btop != a->btgt);
24198 +#endif
24199 +
24200 +       /* rename by vfs_rename or cpup */
24201 +       err = au_ren_or_cpup(a);
24202 +       if (unlikely(err))
24203 +               /* leave the copied-up one */
24204 +               goto out_whtmp;
24205 +
24206 +       /* make dir opaque */
24207 +       err = au_ren_diropq(a);
24208 +       if (unlikely(err))
24209 +               goto out_rename;
24210 +
24211 +       /* update target timestamps */
24212 +       if (a->exchange) {
24213 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24214 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24215 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24216 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24217 +       }
24218 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24219 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24220 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24221 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24222 +
24223 +       if (!a->exchange) {
24224 +               /* remove whiteout for dentry */
24225 +               if (a->dst_wh_dentry) {
24226 +                       a->h_path.dentry = a->dst_wh_dentry;
24227 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24228 +                                                 a->dst_dentry);
24229 +                       if (unlikely(err))
24230 +                               goto out_diropq;
24231 +               }
24232 +
24233 +               /* remove whtmp */
24234 +               if (a->thargs)
24235 +                       au_ren_del_whtmp(a); /* ignore this error */
24236 +
24237 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24238 +       }
24239 +       err = 0;
24240 +       goto out_success;
24241 +
24242 +out_diropq:
24243 +       au_ren_rev_diropq(err, a);
24244 +out_rename:
24245 +       au_ren_rev_rename(err, a);
24246 +       dput(a->h_dst);
24247 +out_whtmp:
24248 +       if (a->thargs)
24249 +               au_ren_rev_whtmp(err, a);
24250 +out_whdst:
24251 +       dput(a->dst_wh_dentry);
24252 +       a->dst_wh_dentry = NULL;
24253 +out_whsrc:
24254 +       if (a->src_wh_dentry)
24255 +               au_ren_rev_whsrc(err, a);
24256 +out_success:
24257 +       dput(a->src_wh_dentry);
24258 +       dput(a->dst_wh_dentry);
24259 +out_thargs:
24260 +       if (a->thargs) {
24261 +               dput(a->h_dst);
24262 +               au_whtmp_rmdir_free(a->thargs);
24263 +               a->thargs = NULL;
24264 +       }
24265 +out:
24266 +       return err;
24267 +}
24268 +
24269 +/* ---------------------------------------------------------------------- */
24270 +
24271 +/*
24272 + * test if @dentry dir can be rename destination or not.
24273 + * success means, it is a logically empty dir.
24274 + */
24275 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24276 +{
24277 +       return au_test_empty(dentry, whlist);
24278 +}
24279 +
24280 +/*
24281 + * test if @a->src_dentry dir can be rename source or not.
24282 + * if it can, return 0.
24283 + * success means,
24284 + * - it is a logically empty dir.
24285 + * - or, it exists on writable branch and has no children including whiteouts
24286 + *   on the lower branch unless DIRREN is on.
24287 + */
24288 +static int may_rename_srcdir(struct au_ren_args *a)
24289 +{
24290 +       int err;
24291 +       unsigned int rdhash;
24292 +       aufs_bindex_t btop, btgt;
24293 +       struct dentry *dentry;
24294 +       struct super_block *sb;
24295 +       struct au_sbinfo *sbinfo;
24296 +
24297 +       dentry = a->src_dentry;
24298 +       sb = dentry->d_sb;
24299 +       sbinfo = au_sbi(sb);
24300 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24301 +               au_fset_ren(a->auren_flags, DIRREN);
24302 +
24303 +       btgt = a->btgt;
24304 +       btop = au_dbtop(dentry);
24305 +       if (btop != btgt) {
24306 +               struct au_nhash whlist;
24307 +
24308 +               SiMustAnyLock(sb);
24309 +               rdhash = sbinfo->si_rdhash;
24310 +               if (!rdhash)
24311 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24312 +                                                          dentry));
24313 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24314 +               if (unlikely(err))
24315 +                       goto out;
24316 +               err = au_test_empty(dentry, &whlist);
24317 +               au_nhash_wh_free(&whlist);
24318 +               goto out;
24319 +       }
24320 +
24321 +       if (btop == au_dbtaildir(dentry))
24322 +               return 0; /* success */
24323 +
24324 +       err = au_test_empty_lower(dentry);
24325 +
24326 +out:
24327 +       if (err == -ENOTEMPTY) {
24328 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24329 +                       err = 0;
24330 +               } else {
24331 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24332 +                               "branches, is not supported\n");
24333 +                       err = -EXDEV;
24334 +               }
24335 +       }
24336 +       return err;
24337 +}
24338 +
24339 +/* side effect: sets whlist and h_dentry */
24340 +static int au_ren_may_dir(struct au_ren_args *a)
24341 +{
24342 +       int err;
24343 +       unsigned int rdhash;
24344 +       struct dentry *d;
24345 +
24346 +       d = a->dst_dentry;
24347 +       SiMustAnyLock(d->d_sb);
24348 +
24349 +       err = 0;
24350 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24351 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24352 +               if (!rdhash)
24353 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24354 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24355 +               if (unlikely(err))
24356 +                       goto out;
24357 +
24358 +               if (!a->exchange) {
24359 +                       au_set_dbtop(d, a->dst_btop);
24360 +                       err = may_rename_dstdir(d, &a->whlist);
24361 +                       au_set_dbtop(d, a->btgt);
24362 +               } else
24363 +                       err = may_rename_srcdir(a);
24364 +       }
24365 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24366 +       if (unlikely(err))
24367 +               goto out;
24368 +
24369 +       d = a->src_dentry;
24370 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24371 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24372 +               err = may_rename_srcdir(a);
24373 +               if (unlikely(err)) {
24374 +                       au_nhash_wh_free(&a->whlist);
24375 +                       a->whlist.nh_num = 0;
24376 +               }
24377 +       }
24378 +out:
24379 +       return err;
24380 +}
24381 +
24382 +/* ---------------------------------------------------------------------- */
24383 +
24384 +/*
24385 + * simple tests for rename.
24386 + * following the checks in vfs, plus the parent-child relationship.
24387 + */
24388 +static int au_may_ren(struct au_ren_args *a)
24389 +{
24390 +       int err, isdir;
24391 +       struct inode *h_inode;
24392 +
24393 +       if (a->src_btop == a->btgt) {
24394 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24395 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24396 +               if (unlikely(err))
24397 +                       goto out;
24398 +               err = -EINVAL;
24399 +               if (unlikely(a->src_h_dentry == a->h_trap))
24400 +                       goto out;
24401 +       }
24402 +
24403 +       err = 0;
24404 +       if (a->dst_btop != a->btgt)
24405 +               goto out;
24406 +
24407 +       err = -ENOTEMPTY;
24408 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24409 +               goto out;
24410 +
24411 +       err = -EIO;
24412 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24413 +       if (d_really_is_negative(a->dst_dentry)) {
24414 +               if (d_is_negative(a->dst_h_dentry))
24415 +                       err = au_may_add(a->dst_dentry, a->btgt,
24416 +                                        a->dst_h_parent, isdir);
24417 +       } else {
24418 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24419 +                       goto out;
24420 +               h_inode = d_inode(a->dst_h_dentry);
24421 +               if (h_inode->i_nlink)
24422 +                       err = au_may_del(a->dst_dentry, a->btgt,
24423 +                                        a->dst_h_parent, isdir);
24424 +       }
24425 +
24426 +out:
24427 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24428 +               err = -EIO;
24429 +       AuTraceErr(err);
24430 +       return err;
24431 +}
24432 +
24433 +/* ---------------------------------------------------------------------- */
24434 +
24435 +/*
24436 + * locking order
24437 + * (VFS)
24438 + * - src_dir and dir by lock_rename()
24439 + * - inode if exists
24440 + * (aufs)
24441 + * - lock all
24442 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24443 + *     + si_read_lock
24444 + *     + di_write_lock2_child()
24445 + *       + di_write_lock_child()
24446 + *        + ii_write_lock_child()
24447 + *       + di_write_lock_child2()
24448 + *        + ii_write_lock_child2()
24449 + *     + src_parent and parent
24450 + *       + di_write_lock_parent()
24451 + *        + ii_write_lock_parent()
24452 + *       + di_write_lock_parent2()
24453 + *        + ii_write_lock_parent2()
24454 + *   + lower src_dir and dir by vfsub_lock_rename()
24455 + *   + verify the every relationships between child and parent. if any
24456 + *     of them failed, unlock all and return -EBUSY.
24457 + */
24458 +static void au_ren_unlock(struct au_ren_args *a)
24459 +{
24460 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24461 +                           a->dst_h_parent, a->dst_hdir);
24462 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24463 +           && a->h_root)
24464 +               au_hn_inode_unlock(a->h_root);
24465 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24466 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24467 +}
24468 +
24469 +static int au_ren_lock(struct au_ren_args *a)
24470 +{
24471 +       int err;
24472 +       unsigned int udba;
24473 +
24474 +       err = 0;
24475 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24476 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24477 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24478 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24479 +
24480 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24481 +       if (unlikely(err))
24482 +               goto out;
24483 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24484 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24485 +               struct dentry *root;
24486 +               struct inode *dir;
24487 +
24488 +               /*
24489 +                * sbinfo is already locked, so this ii_read_lock is
24490 +                * unnecessary. but our debugging feature checks it.
24491 +                */
24492 +               root = a->src_inode->i_sb->s_root;
24493 +               if (root != a->src_parent && root != a->dst_parent) {
24494 +                       dir = d_inode(root);
24495 +                       ii_read_lock_parent3(dir);
24496 +                       a->h_root = au_hi(dir, a->btgt);
24497 +                       ii_read_unlock(dir);
24498 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24499 +               }
24500 +       }
24501 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24502 +                                     a->dst_h_parent, a->dst_hdir);
24503 +       udba = au_opt_udba(a->src_dentry->d_sb);
24504 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24505 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24506 +               err = au_busy_or_stale();
24507 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24508 +               err = au_h_verify(a->src_h_dentry, udba,
24509 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24510 +                                 a->br);
24511 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24512 +               err = au_h_verify(a->dst_h_dentry, udba,
24513 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24514 +                                 a->br);
24515 +       if (!err)
24516 +               goto out; /* success */
24517 +
24518 +       err = au_busy_or_stale();
24519 +       au_ren_unlock(a);
24520 +
24521 +out:
24522 +       return err;
24523 +}
24524 +
24525 +/* ---------------------------------------------------------------------- */
24526 +
24527 +static void au_ren_refresh_dir(struct au_ren_args *a)
24528 +{
24529 +       struct inode *dir;
24530 +
24531 +       dir = a->dst_dir;
24532 +       inode_inc_iversion(dir);
24533 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24534 +               /* is this updating defined in POSIX? */
24535 +               au_cpup_attr_timesizes(a->src_inode);
24536 +               au_cpup_attr_nlink(dir, /*force*/1);
24537 +       }
24538 +       au_dir_ts(dir, a->btgt);
24539 +
24540 +       if (a->exchange) {
24541 +               dir = a->src_dir;
24542 +               inode_inc_iversion(dir);
24543 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24544 +                       /* is this updating defined in POSIX? */
24545 +                       au_cpup_attr_timesizes(a->dst_inode);
24546 +                       au_cpup_attr_nlink(dir, /*force*/1);
24547 +               }
24548 +               au_dir_ts(dir, a->btgt);
24549 +       }
24550 +
24551 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24552 +               return;
24553 +
24554 +       dir = a->src_dir;
24555 +       inode_inc_iversion(dir);
24556 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24557 +               au_cpup_attr_nlink(dir, /*force*/1);
24558 +       au_dir_ts(dir, a->btgt);
24559 +}
24560 +
24561 +static void au_ren_refresh(struct au_ren_args *a)
24562 +{
24563 +       aufs_bindex_t bbot, bindex;
24564 +       struct dentry *d, *h_d;
24565 +       struct inode *i, *h_i;
24566 +       struct super_block *sb;
24567 +
24568 +       d = a->dst_dentry;
24569 +       d_drop(d);
24570 +       if (a->h_dst)
24571 +               /* already dget-ed by au_ren_or_cpup() */
24572 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24573 +
24574 +       i = a->dst_inode;
24575 +       if (i) {
24576 +               if (!a->exchange) {
24577 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24578 +                               vfsub_drop_nlink(i);
24579 +                       else {
24580 +                               vfsub_dead_dir(i);
24581 +                               au_cpup_attr_timesizes(i);
24582 +                       }
24583 +                       au_update_dbrange(d, /*do_put_zero*/1);
24584 +               } else
24585 +                       au_cpup_attr_nlink(i, /*force*/1);
24586 +       } else {
24587 +               bbot = a->btgt;
24588 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24589 +                       au_set_h_dptr(d, bindex, NULL);
24590 +               bbot = au_dbbot(d);
24591 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24592 +                       au_set_h_dptr(d, bindex, NULL);
24593 +               au_update_dbrange(d, /*do_put_zero*/0);
24594 +       }
24595 +
24596 +       if (a->exchange
24597 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24598 +               d_drop(a->src_dentry);
24599 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24600 +                       au_set_dbwh(a->src_dentry, -1);
24601 +               return;
24602 +       }
24603 +
24604 +       d = a->src_dentry;
24605 +       au_set_dbwh(d, -1);
24606 +       bbot = au_dbbot(d);
24607 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24608 +               h_d = au_h_dptr(d, bindex);
24609 +               if (h_d)
24610 +                       au_set_h_dptr(d, bindex, NULL);
24611 +       }
24612 +       au_set_dbbot(d, a->btgt);
24613 +
24614 +       sb = d->d_sb;
24615 +       i = a->src_inode;
24616 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24617 +               return; /* success */
24618 +
24619 +       bbot = au_ibbot(i);
24620 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24621 +               h_i = au_h_iptr(i, bindex);
24622 +               if (h_i) {
24623 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24624 +                       /* ignore this error */
24625 +                       au_set_h_iptr(i, bindex, NULL, 0);
24626 +               }
24627 +       }
24628 +       au_set_ibbot(i, a->btgt);
24629 +}
24630 +
24631 +/* ---------------------------------------------------------------------- */
24632 +
24633 +/* mainly for link(2) and rename(2) */
24634 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24635 +{
24636 +       aufs_bindex_t bdiropq, bwh;
24637 +       struct dentry *parent;
24638 +       struct au_branch *br;
24639 +
24640 +       parent = dentry->d_parent;
24641 +       IMustLock(d_inode(parent)); /* dir is locked */
24642 +
24643 +       bdiropq = au_dbdiropq(parent);
24644 +       bwh = au_dbwh(dentry);
24645 +       br = au_sbr(dentry->d_sb, btgt);
24646 +       if (au_br_rdonly(br)
24647 +           || (0 <= bdiropq && bdiropq < btgt)
24648 +           || (0 <= bwh && bwh < btgt))
24649 +               btgt = -1;
24650 +
24651 +       AuDbg("btgt %d\n", btgt);
24652 +       return btgt;
24653 +}
24654 +
24655 +/* sets src_btop, dst_btop and btgt */
24656 +static int au_ren_wbr(struct au_ren_args *a)
24657 +{
24658 +       int err;
24659 +       struct au_wr_dir_args wr_dir_args = {
24660 +               /* .force_btgt  = -1, */
24661 +               .flags          = AuWrDir_ADD_ENTRY
24662 +       };
24663 +
24664 +       a->src_btop = au_dbtop(a->src_dentry);
24665 +       a->dst_btop = au_dbtop(a->dst_dentry);
24666 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24667 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24668 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24669 +       wr_dir_args.force_btgt = a->src_btop;
24670 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24671 +               wr_dir_args.force_btgt = a->dst_btop;
24672 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24673 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24674 +       a->btgt = err;
24675 +       if (a->exchange)
24676 +               au_update_dbtop(a->dst_dentry);
24677 +
24678 +       return err;
24679 +}
24680 +
24681 +static void au_ren_dt(struct au_ren_args *a)
24682 +{
24683 +       a->h_path.dentry = a->src_h_parent;
24684 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24685 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24686 +               a->h_path.dentry = a->dst_h_parent;
24687 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24688 +       }
24689 +
24690 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24691 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24692 +           && !a->exchange)
24693 +               return;
24694 +
24695 +       a->h_path.dentry = a->src_h_dentry;
24696 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24697 +       if (d_is_positive(a->dst_h_dentry)) {
24698 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24699 +               a->h_path.dentry = a->dst_h_dentry;
24700 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24701 +       }
24702 +}
24703 +
24704 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24705 +{
24706 +       struct dentry *h_d;
24707 +       struct inode *h_inode;
24708 +
24709 +       au_dtime_revert(a->src_dt + AuPARENT);
24710 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24711 +               au_dtime_revert(a->dst_dt + AuPARENT);
24712 +
24713 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24714 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24715 +               h_inode = d_inode(h_d);
24716 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24717 +               au_dtime_revert(a->src_dt + AuCHILD);
24718 +               inode_unlock(h_inode);
24719 +
24720 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24721 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24722 +                       h_inode = d_inode(h_d);
24723 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24724 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24725 +                       inode_unlock(h_inode);
24726 +               }
24727 +       }
24728 +}
24729 +
24730 +/* ---------------------------------------------------------------------- */
24731 +
24732 +int aufs_rename(struct user_namespace *userns,
24733 +               struct inode *_src_dir, struct dentry *_src_dentry,
24734 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24735 +               unsigned int _flags)
24736 +{
24737 +       int err, lock_flags;
24738 +       void *rev;
24739 +       /* reduce stack space */
24740 +       struct au_ren_args *a;
24741 +       struct au_pin pin;
24742 +
24743 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24744 +       IMustLock(_src_dir);
24745 +       IMustLock(_dst_dir);
24746 +
24747 +       err = -EINVAL;
24748 +       if (unlikely(_flags & RENAME_WHITEOUT))
24749 +               goto out;
24750 +
24751 +       err = -ENOMEM;
24752 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24753 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24754 +       if (unlikely(!a))
24755 +               goto out;
24756 +
24757 +       a->flags = _flags;
24758 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24759 +                    && RENAME_EXCHANGE > U8_MAX);
24760 +       a->exchange = _flags & RENAME_EXCHANGE;
24761 +       a->src_dir = _src_dir;
24762 +       a->src_dentry = _src_dentry;
24763 +       a->src_inode = NULL;
24764 +       if (d_really_is_positive(a->src_dentry))
24765 +               a->src_inode = d_inode(a->src_dentry);
24766 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24767 +       a->dst_dir = _dst_dir;
24768 +       a->dst_dentry = _dst_dentry;
24769 +       a->dst_inode = NULL;
24770 +       if (d_really_is_positive(a->dst_dentry))
24771 +               a->dst_inode = d_inode(a->dst_dentry);
24772 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24773 +       if (a->dst_inode) {
24774 +               /*
24775 +                * if EXCHANGE && src is non-dir && dst is dir,
24776 +                * dst is not locked.
24777 +                */
24778 +               /* IMustLock(a->dst_inode); */
24779 +               au_igrab(a->dst_inode);
24780 +       }
24781 +
24782 +       err = -ENOTDIR;
24783 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24784 +       if (d_is_dir(a->src_dentry)) {
24785 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24786 +               if (unlikely(!a->exchange
24787 +                            && d_really_is_positive(a->dst_dentry)
24788 +                            && !d_is_dir(a->dst_dentry)))
24789 +                       goto out_free;
24790 +               lock_flags |= AuLock_DIRS;
24791 +       }
24792 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24793 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24794 +               if (unlikely(!a->exchange
24795 +                            && d_really_is_positive(a->src_dentry)
24796 +                            && !d_is_dir(a->src_dentry)))
24797 +                       goto out_free;
24798 +               lock_flags |= AuLock_DIRS;
24799 +       }
24800 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24801 +                                       lock_flags);
24802 +       if (unlikely(err))
24803 +               goto out_free;
24804 +
24805 +       err = au_d_hashed_positive(a->src_dentry);
24806 +       if (unlikely(err))
24807 +               goto out_unlock;
24808 +       err = -ENOENT;
24809 +       if (a->dst_inode) {
24810 +               /*
24811 +                * If it is a dir, VFS unhash it before this
24812 +                * function. It means we cannot rely upon d_unhashed().
24813 +                */
24814 +               if (unlikely(!a->dst_inode->i_nlink))
24815 +                       goto out_unlock;
24816 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24817 +                       err = au_d_hashed_positive(a->dst_dentry);
24818 +                       if (unlikely(err && !a->exchange))
24819 +                               goto out_unlock;
24820 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24821 +                       goto out_unlock;
24822 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24823 +               goto out_unlock;
24824 +
24825 +       /*
24826 +        * is it possible?
24827 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24828 +        * there may exist a problem somewhere else.
24829 +        */
24830 +       err = -EINVAL;
24831 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24832 +               goto out_unlock;
24833 +
24834 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24835 +       di_write_lock_parent(a->dst_parent);
24836 +
24837 +       /* which branch we process */
24838 +       err = au_ren_wbr(a);
24839 +       if (unlikely(err < 0))
24840 +               goto out_parent;
24841 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24842 +       a->h_path.mnt = au_br_mnt(a->br);
24843 +
24844 +       /* are they available to be renamed */
24845 +       err = au_ren_may_dir(a);
24846 +       if (unlikely(err))
24847 +               goto out_children;
24848 +
24849 +       /* prepare the writable parent dir on the same branch */
24850 +       if (a->dst_btop == a->btgt) {
24851 +               au_fset_ren(a->auren_flags, WHDST);
24852 +       } else {
24853 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24854 +               if (unlikely(err))
24855 +                       goto out_children;
24856 +       }
24857 +
24858 +       err = 0;
24859 +       if (!a->exchange) {
24860 +               if (a->src_dir != a->dst_dir) {
24861 +                       /*
24862 +                        * this temporary unlock is safe,
24863 +                        * because both dir->i_mutex are locked.
24864 +                        */
24865 +                       di_write_unlock(a->dst_parent);
24866 +                       di_write_lock_parent(a->src_parent);
24867 +                       err = au_wr_dir_need_wh(a->src_dentry,
24868 +                                               au_ftest_ren(a->auren_flags,
24869 +                                                            ISDIR_SRC),
24870 +                                               &a->btgt);
24871 +                       di_write_unlock(a->src_parent);
24872 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24873 +                                             /*isdir*/1);
24874 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24875 +               } else
24876 +                       err = au_wr_dir_need_wh(a->src_dentry,
24877 +                                               au_ftest_ren(a->auren_flags,
24878 +                                                            ISDIR_SRC),
24879 +                                               &a->btgt);
24880 +       }
24881 +       if (unlikely(err < 0))
24882 +               goto out_children;
24883 +       if (err)
24884 +               au_fset_ren(a->auren_flags, WHSRC);
24885 +
24886 +       /* cpup src */
24887 +       if (a->src_btop != a->btgt) {
24888 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24889 +                            au_opt_udba(a->src_dentry->d_sb),
24890 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24891 +               if (!err) {
24892 +                       struct au_cp_generic cpg = {
24893 +                               .dentry = a->src_dentry,
24894 +                               .bdst   = a->btgt,
24895 +                               .bsrc   = a->src_btop,
24896 +                               .len    = -1,
24897 +                               .pin    = &pin,
24898 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24899 +                       };
24900 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24901 +                       err = au_sio_cpup_simple(&cpg);
24902 +                       au_unpin(&pin);
24903 +               }
24904 +               if (unlikely(err))
24905 +                       goto out_children;
24906 +               a->src_btop = a->btgt;
24907 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24908 +               if (!a->exchange)
24909 +                       au_fset_ren(a->auren_flags, WHSRC);
24910 +       }
24911 +
24912 +       /* cpup dst */
24913 +       if (a->exchange && a->dst_inode
24914 +           && a->dst_btop != a->btgt) {
24915 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24916 +                            au_opt_udba(a->dst_dentry->d_sb),
24917 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24918 +               if (!err) {
24919 +                       struct au_cp_generic cpg = {
24920 +                               .dentry = a->dst_dentry,
24921 +                               .bdst   = a->btgt,
24922 +                               .bsrc   = a->dst_btop,
24923 +                               .len    = -1,
24924 +                               .pin    = &pin,
24925 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24926 +                       };
24927 +                       err = au_sio_cpup_simple(&cpg);
24928 +                       au_unpin(&pin);
24929 +               }
24930 +               if (unlikely(err))
24931 +                       goto out_children;
24932 +               a->dst_btop = a->btgt;
24933 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24934 +       }
24935 +
24936 +       /* lock them all */
24937 +       err = au_ren_lock(a);
24938 +       if (unlikely(err))
24939 +               /* leave the copied-up one */
24940 +               goto out_children;
24941 +
24942 +       if (!a->exchange) {
24943 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24944 +                       err = au_may_ren(a);
24945 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24946 +                       err = -ENAMETOOLONG;
24947 +               if (unlikely(err))
24948 +                       goto out_hdir;
24949 +       }
24950 +
24951 +       /* store timestamps to be revertible */
24952 +       au_ren_dt(a);
24953 +
24954 +       /* store dirren info */
24955 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24956 +               err = au_dr_rename(a->src_dentry, a->btgt,
24957 +                                  &a->dst_dentry->d_name, &rev);
24958 +               AuTraceErr(err);
24959 +               if (unlikely(err))
24960 +                       goto out_dt;
24961 +       }
24962 +
24963 +       /* here we go */
24964 +       err = do_rename(a);
24965 +       if (unlikely(err))
24966 +               goto out_dirren;
24967 +
24968 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24969 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24970 +
24971 +       /* update dir attributes */
24972 +       au_ren_refresh_dir(a);
24973 +
24974 +       /* dput/iput all lower dentries */
24975 +       au_ren_refresh(a);
24976 +
24977 +       goto out_hdir; /* success */
24978 +
24979 +out_dirren:
24980 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24981 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24982 +out_dt:
24983 +       au_ren_rev_dt(err, a);
24984 +out_hdir:
24985 +       au_ren_unlock(a);
24986 +out_children:
24987 +       au_nhash_wh_free(&a->whlist);
24988 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24989 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24990 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24991 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24992 +       }
24993 +out_parent:
24994 +       if (!err) {
24995 +               if (d_unhashed(a->src_dentry))
24996 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24997 +               if (d_unhashed(a->dst_dentry))
24998 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24999 +               if (!a->exchange)
25000 +                       d_move(a->src_dentry, a->dst_dentry);
25001 +               else {
25002 +                       d_exchange(a->src_dentry, a->dst_dentry);
25003 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
25004 +                               d_drop(a->dst_dentry);
25005 +               }
25006 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
25007 +                       d_drop(a->src_dentry);
25008 +       } else {
25009 +               au_update_dbtop(a->dst_dentry);
25010 +               if (!a->dst_inode)
25011 +                       d_drop(a->dst_dentry);
25012 +       }
25013 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
25014 +               di_write_unlock(a->dst_parent);
25015 +       else
25016 +               di_write_unlock2(a->src_parent, a->dst_parent);
25017 +out_unlock:
25018 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
25019 +out_free:
25020 +       iput(a->dst_inode);
25021 +       if (a->thargs)
25022 +               au_whtmp_rmdir_free(a->thargs);
25023 +       au_kfree_rcu(a);
25024 +out:
25025 +       AuTraceErr(err);
25026 +       return err;
25027 +}
25028 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
25029 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
25030 +++ linux/fs/aufs/Kconfig       2022-11-05 23:02:18.959222617 +0100
25031 @@ -0,0 +1,199 @@
25032 +# SPDX-License-Identifier: GPL-2.0
25033 +config AUFS_FS
25034 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
25035 +       help
25036 +       Aufs is a stackable unification filesystem such as Unionfs,
25037 +       which unifies several directories and provides a merged single
25038 +       directory.
25039 +       In the early days, aufs was entirely re-designed and
25040 +       re-implemented Unionfs Version 1.x series. Introducing many
25041 +       original ideas, approaches and improvements, it becomes totally
25042 +       different from Unionfs while keeping the basic features.
25043 +
25044 +if AUFS_FS
25045 +choice
25046 +       prompt "Maximum number of branches"
25047 +       default AUFS_BRANCH_MAX_127
25048 +       help
25049 +       Specifies the maximum number of branches (or member directories)
25050 +       in a single aufs. The larger value consumes more system
25051 +       resources and has a minor impact to performance.
25052 +config AUFS_BRANCH_MAX_127
25053 +       bool "127"
25054 +       help
25055 +       Specifies the maximum number of branches (or member directories)
25056 +       in a single aufs. The larger value consumes more system
25057 +       resources and has a minor impact to performance.
25058 +config AUFS_BRANCH_MAX_511
25059 +       bool "511"
25060 +       help
25061 +       Specifies the maximum number of branches (or member directories)
25062 +       in a single aufs. The larger value consumes more system
25063 +       resources and has a minor impact to performance.
25064 +config AUFS_BRANCH_MAX_1023
25065 +       bool "1023"
25066 +       help
25067 +       Specifies the maximum number of branches (or member directories)
25068 +       in a single aufs. The larger value consumes more system
25069 +       resources and has a minor impact to performance.
25070 +config AUFS_BRANCH_MAX_32767
25071 +       bool "32767"
25072 +       help
25073 +       Specifies the maximum number of branches (or member directories)
25074 +       in a single aufs. The larger value consumes more system
25075 +       resources and has a minor impact to performance.
25076 +endchoice
25077 +
25078 +config AUFS_SBILIST
25079 +       bool
25080 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25081 +       default y
25082 +       help
25083 +       Automatic configuration for internal use.
25084 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25085 +
25086 +config AUFS_HNOTIFY
25087 +       bool "Detect direct branch access (bypassing aufs)"
25088 +       help
25089 +       If you want to modify files on branches directly, eg. bypassing aufs,
25090 +       and want aufs to detect the changes of them fully, then enable this
25091 +       option and use 'udba=notify' mount option.
25092 +       Currently there is only one available configuration, "fsnotify".
25093 +       It will have a negative impact to the performance.
25094 +       See detail in aufs.5.
25095 +
25096 +choice
25097 +       prompt "method" if AUFS_HNOTIFY
25098 +       default AUFS_HFSNOTIFY
25099 +config AUFS_HFSNOTIFY
25100 +       bool "fsnotify"
25101 +       select FSNOTIFY
25102 +endchoice
25103 +
25104 +config AUFS_EXPORT
25105 +       bool "NFS-exportable aufs"
25106 +       depends on EXPORTFS
25107 +       help
25108 +       If you want to export your mounted aufs via NFS, then enable this
25109 +       option. There are several requirements for this configuration.
25110 +       See detail in aufs.5.
25111 +
25112 +config AUFS_INO_T_64
25113 +       bool
25114 +       depends on AUFS_EXPORT
25115 +       depends on 64BIT && !(ALPHA || S390)
25116 +       default y
25117 +       help
25118 +       Automatic configuration for internal use.
25119 +       /* typedef unsigned long/int __kernel_ino_t */
25120 +       /* alpha and s390x are int */
25121 +
25122 +config AUFS_XATTR
25123 +       bool "support for XATTR/EA (including Security Labels)"
25124 +       help
25125 +       If your branch fs supports XATTR/EA and you want to make them
25126 +       available in aufs too, then enable this opsion and specify the
25127 +       branch attributes for EA.
25128 +       See detail in aufs.5.
25129 +
25130 +config AUFS_FHSM
25131 +       bool "File-based Hierarchical Storage Management"
25132 +       help
25133 +       Hierarchical Storage Management (or HSM) is a well-known feature
25134 +       in the storage world. Aufs provides this feature as file-based.
25135 +       with multiple branches.
25136 +       These multiple branches are prioritized, ie. the topmost one
25137 +       should be the fastest drive and be used heavily.
25138 +
25139 +config AUFS_RDU
25140 +       bool "Readdir in userspace"
25141 +       help
25142 +       Aufs has two methods to provide a merged view for a directory,
25143 +       by a user-space library and by kernel-space natively. The latter
25144 +       is always enabled but sometimes large and slow.
25145 +       If you enable this option, install the library in aufs2-util
25146 +       package, and set some environment variables for your readdir(3),
25147 +       then the work will be handled in user-space which generally
25148 +       shows better performance in most cases.
25149 +       See detail in aufs.5.
25150 +
25151 +config AUFS_DIRREN
25152 +       bool "Workaround for rename(2)-ing a directory"
25153 +       help
25154 +       By default, aufs returns EXDEV error in renameing a dir who has
25155 +       his child on the lower branch, since it is a bad idea to issue
25156 +       rename(2) internally for every lower branch. But user may not
25157 +       accept this behaviour. So here is a workaround to allow such
25158 +       rename(2) and store some extra information on the writable
25159 +       branch. Obviously this costs high (and I don't like it).
25160 +       To use this feature, you need to enable this configuration AND
25161 +       to specify the mount option `dirren.'
25162 +       See details in aufs.5 and the design documents.
25163 +
25164 +config AUFS_SHWH
25165 +       bool "Show whiteouts"
25166 +       help
25167 +       If you want to make the whiteouts in aufs visible, then enable
25168 +       this option and specify 'shwh' mount option. Although it may
25169 +       sounds like philosophy or something, but in technically it
25170 +       simply shows the name of whiteout with keeping its behaviour.
25171 +
25172 +config AUFS_BR_RAMFS
25173 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25174 +       help
25175 +       If you want to use ramfs as an aufs branch fs, then enable this
25176 +       option. Generally tmpfs is recommended.
25177 +       Aufs prohibited them to be a branch fs by default, because
25178 +       initramfs becomes unusable after switch_root or something
25179 +       generally. If you sets initramfs as an aufs branch and boot your
25180 +       system by switch_root, you will meet a problem easily since the
25181 +       files in initramfs may be inaccessible.
25182 +       Unless you are going to use ramfs as an aufs branch fs without
25183 +       switch_root or something, leave it N.
25184 +
25185 +config AUFS_BR_FUSE
25186 +       bool "Fuse fs as an aufs branch"
25187 +       depends on FUSE_FS
25188 +       select AUFS_POLL
25189 +       help
25190 +       If you want to use fuse-based userspace filesystem as an aufs
25191 +       branch fs, then enable this option.
25192 +       It implements the internal poll(2) operation which is
25193 +       implemented by fuse only (curretnly).
25194 +
25195 +config AUFS_POLL
25196 +       bool
25197 +       help
25198 +       Automatic configuration for internal use.
25199 +
25200 +config AUFS_BR_HFSPLUS
25201 +       bool "Hfsplus as an aufs branch"
25202 +       depends on HFSPLUS_FS
25203 +       default y
25204 +       help
25205 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25206 +       this option. This option introduces a small overhead at
25207 +       copying-up a file on hfsplus.
25208 +
25209 +config AUFS_BDEV_LOOP
25210 +       bool
25211 +       depends on BLK_DEV_LOOP
25212 +       default y
25213 +       help
25214 +       Automatic configuration for internal use.
25215 +       Convert =[ym] into =y.
25216 +
25217 +config AUFS_DEBUG
25218 +       bool "Debug aufs"
25219 +       help
25220 +       Enable this to compile aufs internal debug code.
25221 +       It will have a negative impact to the performance.
25222 +
25223 +config AUFS_MAGIC_SYSRQ
25224 +       bool
25225 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25226 +       default y
25227 +       help
25228 +       Automatic configuration for internal use.
25229 +       When aufs supports Magic SysRq, enabled automatically.
25230 +endif
25231 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25232 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25233 +++ linux/fs/aufs/lcnt.h        2022-11-05 23:02:18.965889284 +0100
25234 @@ -0,0 +1,186 @@
25235 +/* SPDX-License-Identifier: GPL-2.0 */
25236 +/*
25237 + * Copyright (C) 2018-2022 Junjiro R. Okajima
25238 + *
25239 + * This program is free software; you can redistribute it and/or modify
25240 + * it under the terms of the GNU General Public License as published by
25241 + * the Free Software Foundation; either version 2 of the License, or
25242 + * (at your option) any later version.
25243 + *
25244 + * This program is distributed in the hope that it will be useful,
25245 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25246 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25247 + * GNU General Public License for more details.
25248 + *
25249 + * You should have received a copy of the GNU General Public License
25250 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25251 + */
25252 +
25253 +/*
25254 + * simple long counter wrapper
25255 + */
25256 +
25257 +#ifndef __AUFS_LCNT_H__
25258 +#define __AUFS_LCNT_H__
25259 +
25260 +#ifdef __KERNEL__
25261 +
25262 +#include "debug.h"
25263 +
25264 +#define AuLCntATOMIC   1
25265 +#define AuLCntPCPUCNT  2
25266 +/*
25267 + * why does percpu_refcount require extra synchronize_rcu()s in
25268 + * au_br_do_free()
25269 + */
25270 +#define AuLCntPCPUREF  3
25271 +
25272 +/* #define AuLCntChosen        AuLCntATOMIC */
25273 +#define AuLCntChosen   AuLCntPCPUCNT
25274 +/* #define AuLCntChosen        AuLCntPCPUREF */
25275 +
25276 +#if AuLCntChosen == AuLCntATOMIC
25277 +#include <linux/atomic.h>
25278 +
25279 +typedef atomic_long_t au_lcnt_t;
25280 +
25281 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25282 +{
25283 +       atomic_long_set(cnt, 0);
25284 +       return 0;
25285 +}
25286 +
25287 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25288 +{
25289 +       /* empty */
25290 +}
25291 +
25292 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25293 +                              int do_sync __maybe_unused)
25294 +{
25295 +       /* empty */
25296 +}
25297 +
25298 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25299 +{
25300 +       atomic_long_inc(cnt);
25301 +}
25302 +
25303 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25304 +{
25305 +       atomic_long_dec(cnt);
25306 +}
25307 +
25308 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25309 +{
25310 +       return atomic_long_read(cnt);
25311 +}
25312 +#endif
25313 +
25314 +#if AuLCntChosen == AuLCntPCPUCNT
25315 +#include <linux/percpu_counter.h>
25316 +
25317 +typedef struct percpu_counter au_lcnt_t;
25318 +
25319 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25320 +{
25321 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25322 +}
25323 +
25324 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25325 +{
25326 +       /* empty */
25327 +}
25328 +
25329 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25330 +{
25331 +       percpu_counter_destroy(cnt);
25332 +}
25333 +
25334 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25335 +{
25336 +       percpu_counter_inc(cnt);
25337 +}
25338 +
25339 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25340 +{
25341 +       percpu_counter_dec(cnt);
25342 +}
25343 +
25344 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25345 +{
25346 +       s64 n;
25347 +
25348 +       n = percpu_counter_sum(cnt);
25349 +       BUG_ON(n < 0);
25350 +       if (LONG_MAX != LLONG_MAX
25351 +           && n > LONG_MAX)
25352 +               AuWarn1("%s\n", "wrap-around");
25353 +
25354 +       return n;
25355 +}
25356 +#endif
25357 +
25358 +#if AuLCntChosen == AuLCntPCPUREF
25359 +#include <linux/percpu-refcount.h>
25360 +
25361 +typedef struct percpu_ref au_lcnt_t;
25362 +
25363 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25364 +{
25365 +       if (!release)
25366 +               release = percpu_ref_exit;
25367 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25368 +}
25369 +
25370 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25371 +{
25372 +       synchronize_rcu();
25373 +}
25374 +
25375 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25376 +{
25377 +       percpu_ref_kill(cnt);
25378 +       if (do_sync)
25379 +               au_lcnt_wait_for_fin(cnt);
25380 +}
25381 +
25382 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25383 +{
25384 +       percpu_ref_get(cnt);
25385 +}
25386 +
25387 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25388 +{
25389 +       percpu_ref_put(cnt);
25390 +}
25391 +
25392 +/*
25393 + * avoid calling this func as possible.
25394 + */
25395 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25396 +{
25397 +       long l;
25398 +
25399 +       percpu_ref_switch_to_atomic_sync(cnt);
25400 +       l = atomic_long_read(&cnt->count);
25401 +       if (do_rev)
25402 +               percpu_ref_switch_to_percpu(cnt);
25403 +
25404 +       /* percpu_ref is initialized by 1 instead of 0 */
25405 +       return l - 1;
25406 +}
25407 +#endif
25408 +
25409 +#ifdef CONFIG_AUFS_DEBUG
25410 +#define AuLCntZero(val) do {                   \
25411 +       long l = val;                           \
25412 +       if (l)                                  \
25413 +               AuDbg("%s = %ld\n", #val, l);   \
25414 +} while (0)
25415 +#else
25416 +#define AuLCntZero(val)                do {} while (0)
25417 +#endif
25418 +
25419 +#endif /* __KERNEL__ */
25420 +#endif /* __AUFS_LCNT_H__ */
25421 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25422 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25423 +++ linux/fs/aufs/loop.c        2022-11-05 23:02:18.965889284 +0100
25424 @@ -0,0 +1,148 @@
25425 +// SPDX-License-Identifier: GPL-2.0
25426 +/*
25427 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25428 + *
25429 + * This program is free software; you can redistribute it and/or modify
25430 + * it under the terms of the GNU General Public License as published by
25431 + * the Free Software Foundation; either version 2 of the License, or
25432 + * (at your option) any later version.
25433 + *
25434 + * This program is distributed in the hope that it will be useful,
25435 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25436 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25437 + * GNU General Public License for more details.
25438 + *
25439 + * You should have received a copy of the GNU General Public License
25440 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25441 + */
25442 +
25443 +/*
25444 + * support for loopback block device as a branch
25445 + */
25446 +
25447 +#include "aufs.h"
25448 +
25449 +/* added into drivers/block/loop.c */
25450 +static struct file *(*backing_file_func)(struct super_block *sb);
25451 +
25452 +/*
25453 + * test if two lower dentries have overlapping branches.
25454 + */
25455 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25456 +{
25457 +       struct super_block *h_sb;
25458 +       struct file *backing_file;
25459 +
25460 +       if (unlikely(!backing_file_func)) {
25461 +               /* don't load "loop" module here */
25462 +               backing_file_func = symbol_get(loop_backing_file);
25463 +               if (unlikely(!backing_file_func))
25464 +                       /* "loop" module is not loaded */
25465 +                       return 0;
25466 +       }
25467 +
25468 +       h_sb = h_adding->d_sb;
25469 +       backing_file = backing_file_func(h_sb);
25470 +       if (!backing_file)
25471 +               return 0;
25472 +
25473 +       h_adding = backing_file->f_path.dentry;
25474 +       /*
25475 +        * h_adding can be local NFS.
25476 +        * in this case aufs cannot detect the loop.
25477 +        */
25478 +       if (unlikely(h_adding->d_sb == sb))
25479 +               return 1;
25480 +       return !!au_test_subdir(h_adding, sb->s_root);
25481 +}
25482 +
25483 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25484 +int au_test_loopback_kthread(void)
25485 +{
25486 +       int ret;
25487 +       struct task_struct *tsk = current;
25488 +       char c, comm[sizeof(tsk->comm)];
25489 +
25490 +       ret = 0;
25491 +       if (tsk->flags & PF_KTHREAD) {
25492 +               get_task_comm(comm, tsk);
25493 +               c = comm[4];
25494 +               ret = ('0' <= c && c <= '9'
25495 +                      && !strncmp(comm, "loop", 4));
25496 +       }
25497 +
25498 +       return ret;
25499 +}
25500 +
25501 +/* ---------------------------------------------------------------------- */
25502 +
25503 +#define au_warn_loopback_step  16
25504 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25505 +static unsigned long *au_warn_loopback_array;
25506 +
25507 +void au_warn_loopback(struct super_block *h_sb)
25508 +{
25509 +       int i, new_nelem;
25510 +       unsigned long *a, magic;
25511 +       static DEFINE_SPINLOCK(spin);
25512 +
25513 +       magic = h_sb->s_magic;
25514 +       spin_lock(&spin);
25515 +       a = au_warn_loopback_array;
25516 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25517 +               if (a[i] == magic) {
25518 +                       spin_unlock(&spin);
25519 +                       return;
25520 +               }
25521 +
25522 +       /* h_sb is new to us, print it */
25523 +       if (i < au_warn_loopback_nelem) {
25524 +               a[i] = magic;
25525 +               goto pr;
25526 +       }
25527 +
25528 +       /* expand the array */
25529 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25530 +       a = au_kzrealloc(au_warn_loopback_array,
25531 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25532 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25533 +                        /*may_shrink*/0);
25534 +       if (a) {
25535 +               au_warn_loopback_nelem = new_nelem;
25536 +               au_warn_loopback_array = a;
25537 +               a[i] = magic;
25538 +               goto pr;
25539 +       }
25540 +
25541 +       spin_unlock(&spin);
25542 +       AuWarn1("realloc failed, ignored\n");
25543 +       return;
25544 +
25545 +pr:
25546 +       spin_unlock(&spin);
25547 +       pr_warn("you may want to try another patch for loopback file "
25548 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25549 +}
25550 +
25551 +int au_loopback_init(void)
25552 +{
25553 +       int err;
25554 +       struct super_block *sb __maybe_unused;
25555 +
25556 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25557 +
25558 +       err = 0;
25559 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25560 +                                        sizeof(unsigned long), GFP_NOFS);
25561 +       if (unlikely(!au_warn_loopback_array))
25562 +               err = -ENOMEM;
25563 +
25564 +       return err;
25565 +}
25566 +
25567 +void au_loopback_fin(void)
25568 +{
25569 +       if (backing_file_func)
25570 +               symbol_put(loop_backing_file);
25571 +       au_kfree_try_rcu(au_warn_loopback_array);
25572 +}
25573 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25574 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25575 +++ linux/fs/aufs/loop.h        2022-11-05 23:02:18.965889284 +0100
25576 @@ -0,0 +1,55 @@
25577 +/* SPDX-License-Identifier: GPL-2.0 */
25578 +/*
25579 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25580 + *
25581 + * This program is free software; you can redistribute it and/or modify
25582 + * it under the terms of the GNU General Public License as published by
25583 + * the Free Software Foundation; either version 2 of the License, or
25584 + * (at your option) any later version.
25585 + *
25586 + * This program is distributed in the hope that it will be useful,
25587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25589 + * GNU General Public License for more details.
25590 + *
25591 + * You should have received a copy of the GNU General Public License
25592 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25593 + */
25594 +
25595 +/*
25596 + * support for loopback mount as a branch
25597 + */
25598 +
25599 +#ifndef __AUFS_LOOP_H__
25600 +#define __AUFS_LOOP_H__
25601 +
25602 +#ifdef __KERNEL__
25603 +
25604 +struct dentry;
25605 +struct super_block;
25606 +
25607 +#ifdef CONFIG_AUFS_BDEV_LOOP
25608 +/* drivers/block/loop.c */
25609 +struct file *loop_backing_file(struct super_block *sb);
25610 +
25611 +/* loop.c */
25612 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25613 +int au_test_loopback_kthread(void);
25614 +void au_warn_loopback(struct super_block *h_sb);
25615 +
25616 +int au_loopback_init(void);
25617 +void au_loopback_fin(void);
25618 +#else
25619 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25620 +
25621 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25622 +          struct dentry *h_adding)
25623 +AuStubInt0(au_test_loopback_kthread, void)
25624 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25625 +
25626 +AuStubInt0(au_loopback_init, void)
25627 +AuStubVoid(au_loopback_fin, void)
25628 +#endif /* BLK_DEV_LOOP */
25629 +
25630 +#endif /* __KERNEL__ */
25631 +#endif /* __AUFS_LOOP_H__ */
25632 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25633 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25634 +++ linux/fs/aufs/magic.mk      2022-11-05 23:02:18.965889284 +0100
25635 @@ -0,0 +1,31 @@
25636 +# SPDX-License-Identifier: GPL-2.0
25637 +
25638 +# defined in ${srctree}/fs/fuse/inode.c
25639 +# tristate
25640 +ifdef CONFIG_FUSE_FS
25641 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25642 +endif
25643 +
25644 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25645 +# tristate
25646 +ifdef CONFIG_XFS_FS
25647 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25648 +endif
25649 +
25650 +# defined in ${srctree}/fs/configfs/mount.c
25651 +# tristate
25652 +ifdef CONFIG_CONFIGFS_FS
25653 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25654 +endif
25655 +
25656 +# defined in ${srctree}/fs/ubifs/ubifs.h
25657 +# tristate
25658 +ifdef CONFIG_UBIFS_FS
25659 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25660 +endif
25661 +
25662 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25663 +# tristate
25664 +ifdef CONFIG_HFSPLUS_FS
25665 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25666 +endif
25667 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25668 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25669 +++ linux/fs/aufs/Makefile      2022-11-05 23:02:18.959222617 +0100
25670 @@ -0,0 +1,46 @@
25671 +# SPDX-License-Identifier: GPL-2.0
25672 +
25673 +include ${src}/magic.mk
25674 +ifeq (${CONFIG_AUFS_FS},m)
25675 +include ${src}/conf.mk
25676 +endif
25677 +-include ${src}/priv_def.mk
25678 +
25679 +# cf. include/linux/kernel.h
25680 +# enable pr_debug
25681 +ccflags-y += -DDEBUG
25682 +# sparse requires the full pathname
25683 +ifdef M
25684 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25685 +else
25686 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25687 +endif
25688 +
25689 +obj-$(CONFIG_AUFS_FS) += aufs.o
25690 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25691 +       wkq.o vfsub.o dcsub.o \
25692 +       cpup.o whout.o wbr_policy.o \
25693 +       dinfo.o dentry.o \
25694 +       dynop.o \
25695 +       finfo.o file.o f_op.o \
25696 +       dir.o vdir.o \
25697 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25698 +       mvdown.o ioctl.o
25699 +
25700 +# all are boolean
25701 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25702 +aufs-$(CONFIG_SYSFS) += sysfs.o
25703 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25704 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25705 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25706 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25707 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25708 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25709 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25710 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25711 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25712 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25713 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25714 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25715 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25716 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25717 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25718 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25719 +++ linux/fs/aufs/module.c      2022-11-05 23:02:18.965889284 +0100
25720 @@ -0,0 +1,273 @@
25721 +// SPDX-License-Identifier: GPL-2.0
25722 +/*
25723 + * Copyright (C) 2005-2022 Junjiro R. Okajima
25724 + *
25725 + * This program is free software; you can redistribute it and/or modify
25726 + * it under the terms of the GNU General Public License as published by
25727 + * the Free Software Foundation; either version 2 of the License, or
25728 + * (at your option) any later version.
25729 + *
25730 + * This program is distributed in the hope that it will be useful,
25731 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25732 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25733 + * GNU General Public License for more details.
25734 + *
25735 + * You should have received a copy of the GNU General Public License
25736 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25737 + */
25738 +
25739 +/*
25740 + * module global variables and operations
25741 + */
25742 +
25743 +#include <linux/module.h>
25744 +#include <linux/seq_file.h>
25745 +#include "aufs.h"
25746 +
25747 +/* shrinkable realloc */
25748 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25749 +{
25750 +       size_t sz;
25751 +       int diff;
25752 +
25753 +       sz = 0;
25754 +       diff = -1;
25755 +       if (p) {
25756 +#if 0 /* unused */
25757 +               if (!new_sz) {
25758 +                       au_kfree_rcu(p);
25759 +                       p = NULL;
25760 +                       goto out;
25761 +               }
25762 +#else
25763 +               AuDebugOn(!new_sz);
25764 +#endif
25765 +               sz = ksize(p);
25766 +               diff = au_kmidx_sub(sz, new_sz);
25767 +       }
25768 +       if (sz && !diff)
25769 +               goto out;
25770 +
25771 +       if (sz < new_sz)
25772 +               /* expand or SLOB */
25773 +               p = krealloc(p, new_sz, gfp);
25774 +       else if (new_sz < sz && may_shrink) {
25775 +               /* shrink */
25776 +               void *q;
25777 +
25778 +               q = kmalloc(new_sz, gfp);
25779 +               if (q) {
25780 +                       if (p) {
25781 +                               memcpy(q, p, new_sz);
25782 +                               au_kfree_try_rcu(p);
25783 +                       }
25784 +                       p = q;
25785 +               } else
25786 +                       p = NULL;
25787 +       }
25788 +
25789 +out:
25790 +       return p;
25791 +}
25792 +
25793 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25794 +                  int may_shrink)
25795 +{
25796 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25797 +       if (p && new_sz > nused)
25798 +               memset(p + nused, 0, new_sz - nused);
25799 +       return p;
25800 +}
25801 +
25802 +/* ---------------------------------------------------------------------- */
25803 +/*
25804 + * aufs caches
25805 + */
25806 +struct kmem_cache *au_cache[AuCache_Last];
25807 +
25808 +static void au_cache_fin(void)
25809 +{
25810 +       int i;
25811 +
25812 +       /*
25813 +        * Make sure all delayed rcu free inodes are flushed before we
25814 +        * destroy cache.
25815 +        */
25816 +       rcu_barrier();
25817 +
25818 +       /* excluding AuCache_HNOTIFY */
25819 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25820 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25821 +               kmem_cache_destroy(au_cache[i]);
25822 +               au_cache[i] = NULL;
25823 +       }
25824 +}
25825 +
25826 +static int __init au_cache_init(void)
25827 +{
25828 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25829 +       if (au_cache[AuCache_DINFO])
25830 +               /* SLAB_DESTROY_BY_RCU */
25831 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25832 +                                                      au_icntnr_init_once);
25833 +       if (au_cache[AuCache_ICNTNR])
25834 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25835 +                                                     au_fi_init_once);
25836 +       if (au_cache[AuCache_FINFO])
25837 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25838 +       if (au_cache[AuCache_VDIR])
25839 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25840 +       if (au_cache[AuCache_DEHSTR])
25841 +               return 0;
25842 +
25843 +       au_cache_fin();
25844 +       return -ENOMEM;
25845 +}
25846 +
25847 +/* ---------------------------------------------------------------------- */
25848 +
25849 +int au_dir_roflags;
25850 +
25851 +#ifdef CONFIG_AUFS_SBILIST
25852 +/*
25853 + * iterate_supers_type() doesn't protect us from
25854 + * remounting (branch management)
25855 + */
25856 +struct hlist_bl_head au_sbilist;
25857 +#endif
25858 +
25859 +/*
25860 + * functions for module interface.
25861 + */
25862 +MODULE_LICENSE("GPL");
25863 +/* MODULE_LICENSE("GPL v2"); */
25864 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25865 +MODULE_DESCRIPTION(AUFS_NAME
25866 +       " -- Advanced multi layered unification filesystem");
25867 +MODULE_VERSION(AUFS_VERSION);
25868 +MODULE_ALIAS_FS(AUFS_NAME);
25869 +
25870 +/* this module parameter has no meaning when SYSFS is disabled */
25871 +int sysaufs_brs = 1;
25872 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25873 +module_param_named(brs, sysaufs_brs, int, 0444);
25874 +
25875 +/* this module parameter has no meaning when USER_NS is disabled */
25876 +bool au_userns;
25877 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25878 +module_param_named(allow_userns, au_userns, bool, 0444);
25879 +
25880 +/* ---------------------------------------------------------------------- */
25881 +
25882 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25883 +
25884 +int au_seq_path(struct seq_file *seq, struct path *path)
25885 +{
25886 +       int err;
25887 +
25888 +       err = seq_path(seq, path, au_esc_chars);
25889 +       if (err >= 0)
25890 +               err = 0;
25891 +       else
25892 +               err = -ENOMEM;
25893 +
25894 +       return err;
25895 +}
25896 +
25897 +/* ---------------------------------------------------------------------- */
25898 +
25899 +static int __init aufs_init(void)
25900 +{
25901 +       int err, i;
25902 +       char *p;
25903 +
25904 +       p = au_esc_chars;
25905 +       for (i = 1; i <= ' '; i++)
25906 +               *p++ = i;
25907 +       *p++ = '\\';
25908 +       *p++ = '\x7f';
25909 +       *p = 0;
25910 +
25911 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25912 +
25913 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25914 +       for (i = 0; i < AuIop_Last; i++)
25915 +               aufs_iop_nogetattr[i].getattr = NULL;
25916 +
25917 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25918 +
25919 +       au_sbilist_init();
25920 +       sysaufs_brs_init();
25921 +       au_debug_init();
25922 +       au_dy_init();
25923 +       err = sysaufs_init();
25924 +       if (unlikely(err))
25925 +               goto out;
25926 +       err = dbgaufs_init();
25927 +       if (unlikely(err))
25928 +               goto out_sysaufs;
25929 +       err = au_procfs_init();
25930 +       if (unlikely(err))
25931 +               goto out_dbgaufs;
25932 +       err = au_wkq_init();
25933 +       if (unlikely(err))
25934 +               goto out_procfs;
25935 +       err = au_loopback_init();
25936 +       if (unlikely(err))
25937 +               goto out_wkq;
25938 +       err = au_hnotify_init();
25939 +       if (unlikely(err))
25940 +               goto out_loopback;
25941 +       err = au_sysrq_init();
25942 +       if (unlikely(err))
25943 +               goto out_hin;
25944 +       err = au_cache_init();
25945 +       if (unlikely(err))
25946 +               goto out_sysrq;
25947 +
25948 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25949 +       err = register_filesystem(&aufs_fs_type);
25950 +       if (unlikely(err))
25951 +               goto out_cache;
25952 +
25953 +       /* since we define pr_fmt, call printk directly */
25954 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25955 +       goto out; /* success */
25956 +
25957 +out_cache:
25958 +       au_cache_fin();
25959 +out_sysrq:
25960 +       au_sysrq_fin();
25961 +out_hin:
25962 +       au_hnotify_fin();
25963 +out_loopback:
25964 +       au_loopback_fin();
25965 +out_wkq:
25966 +       au_wkq_fin();
25967 +out_procfs:
25968 +       au_procfs_fin();
25969 +out_dbgaufs:
25970 +       dbgaufs_fin();
25971 +out_sysaufs:
25972 +       sysaufs_fin();
25973 +       au_dy_fin();
25974 +out:
25975 +       return err;
25976 +}
25977 +
25978 +static void __exit aufs_exit(void)
25979 +{
25980 +       unregister_filesystem(&aufs_fs_type);
25981 +       au_cache_fin();
25982 +       au_sysrq_fin();
25983 +       au_hnotify_fin();
25984 +       au_loopback_fin();
25985 +       au_wkq_fin();
25986 +       au_procfs_fin();
25987 +       dbgaufs_fin();
25988 +       sysaufs_fin();
25989 +       au_dy_fin();
25990 +}
25991 +
25992 +module_init(aufs_init);
25993 +module_exit(aufs_exit);
25994 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25995 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25996 +++ linux/fs/aufs/module.h      2022-11-05 23:02:18.969222617 +0100
25997 @@ -0,0 +1,180 @@
25998 +/* SPDX-License-Identifier: GPL-2.0 */
25999 +/*
26000 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26001 + *
26002 + * This program is free software; you can redistribute it and/or modify
26003 + * it under the terms of the GNU General Public License as published by
26004 + * the Free Software Foundation; either version 2 of the License, or
26005 + * (at your option) any later version.
26006 + *
26007 + * This program is distributed in the hope that it will be useful,
26008 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26009 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26010 + * GNU General Public License for more details.
26011 + *
26012 + * You should have received a copy of the GNU General Public License
26013 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26014 + */
26015 +
26016 +/*
26017 + * module initialization and module-global
26018 + */
26019 +
26020 +#ifndef __AUFS_MODULE_H__
26021 +#define __AUFS_MODULE_H__
26022 +
26023 +#ifdef __KERNEL__
26024 +
26025 +#include <linux/slab.h>
26026 +#include "debug.h"
26027 +#include "dentry.h"
26028 +#include "dir.h"
26029 +#include "file.h"
26030 +#include "inode.h"
26031 +
26032 +struct path;
26033 +struct seq_file;
26034 +
26035 +/* module parameters */
26036 +extern int sysaufs_brs;
26037 +extern bool au_userns;
26038 +
26039 +/* ---------------------------------------------------------------------- */
26040 +
26041 +extern int au_dir_roflags;
26042 +
26043 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
26044 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26045 +                  int may_shrink);
26046 +
26047 +/*
26048 + * Comparing the size of the object with sizeof(struct rcu_head)
26049 + * case 1: object is always larger
26050 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26051 + * case 2: object is always smaller
26052 + *     --> au_kfree_small()
26053 + * case 3: object can be any size
26054 + *     --> au_kfree_try_rcu()
26055 + */
26056 +
26057 +static inline void au_kfree_do_rcu(const void *p)
26058 +{
26059 +       struct {
26060 +               struct rcu_head rcu;
26061 +       } *a = (void *)p;
26062 +
26063 +       kfree_rcu(a, rcu);
26064 +}
26065 +
26066 +#define au_kfree_rcu(_p) do {                                          \
26067 +               typeof(_p) p = (_p);                                    \
26068 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26069 +               if (p)                                                  \
26070 +                       au_kfree_do_rcu(p);                             \
26071 +       } while (0)
26072 +
26073 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26074 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26075 +
26076 +static inline void au_kfree_try_rcu(const void *p)
26077 +{
26078 +       if (!p)
26079 +               return;
26080 +       if (au_kfree_sz_test(p))
26081 +               au_kfree_do_rcu(p);
26082 +       else
26083 +               kfree(p);
26084 +}
26085 +
26086 +static inline void au_kfree_small(const void *p)
26087 +{
26088 +       if (!p)
26089 +               return;
26090 +       AuDebugOn(au_kfree_sz_test(p));
26091 +       kfree(p);
26092 +}
26093 +
26094 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26095 +{
26096 +#ifndef CONFIG_SLOB
26097 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26098 +#else
26099 +       return -1; /* SLOB is untested */
26100 +#endif
26101 +}
26102 +
26103 +int au_seq_path(struct seq_file *seq, struct path *path);
26104 +
26105 +#ifdef CONFIG_PROC_FS
26106 +/* procfs.c */
26107 +int __init au_procfs_init(void);
26108 +void au_procfs_fin(void);
26109 +#else
26110 +AuStubInt0(au_procfs_init, void);
26111 +AuStubVoid(au_procfs_fin, void);
26112 +#endif
26113 +
26114 +/* ---------------------------------------------------------------------- */
26115 +
26116 +/* kmem cache */
26117 +enum {
26118 +       AuCache_DINFO,
26119 +       AuCache_ICNTNR,
26120 +       AuCache_FINFO,
26121 +       AuCache_VDIR,
26122 +       AuCache_DEHSTR,
26123 +       AuCache_HNOTIFY, /* must be last */
26124 +       AuCache_Last
26125 +};
26126 +
26127 +extern struct kmem_cache *au_cache[AuCache_Last];
26128 +
26129 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26130 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26131 +#define AuCacheCtor(type, ctor)        \
26132 +       kmem_cache_create(#type, sizeof(struct type), \
26133 +                         __alignof__(struct type), AuCacheFlags, ctor)
26134 +
26135 +#define AuCacheFuncAlloc(name, index)                                  \
26136 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26137 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26138 +
26139 +#define AuCacheFuncs(name, index)                                      \
26140 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26141 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26142 +                                                                       \
26143 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26144 +       { void *p = rcu;                                                \
26145 +               p -= offsetof(struct au_##name, rcu);                   \
26146 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26147 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26148 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26149 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26150 +                                                                       \
26151 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26152 +       { /* au_cache_free_##name##_norcu(p); */                        \
26153 +               au_cache_free_##name##_rcu(p); }
26154 +
26155 +AuCacheFuncs(dinfo, DINFO);
26156 +AuCacheFuncAlloc(dinfo, DINFO);
26157 +
26158 +AuCacheFuncs(icntnr, ICNTNR);
26159 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26160 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26161 +
26162 +AuCacheFuncs(finfo, FINFO);
26163 +AuCacheFuncAlloc(finfo, FINFO);
26164 +
26165 +AuCacheFuncs(vdir, VDIR);
26166 +AuCacheFuncAlloc(vdir, VDIR);
26167 +
26168 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26169 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26170 +
26171 +#ifdef CONFIG_AUFS_HNOTIFY
26172 +AuCacheFuncs(hnotify, HNOTIFY);
26173 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26174 +#endif
26175 +
26176 +#endif /* __KERNEL__ */
26177 +#endif /* __AUFS_MODULE_H__ */
26178 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26179 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26180 +++ linux/fs/aufs/mvdown.c      2022-11-05 23:02:18.969222617 +0100
26181 @@ -0,0 +1,706 @@
26182 +// SPDX-License-Identifier: GPL-2.0
26183 +/*
26184 + * Copyright (C) 2011-2022 Junjiro R. Okajima
26185 + *
26186 + * This program is free software; you can redistribute it and/or modify
26187 + * it under the terms of the GNU General Public License as published by
26188 + * the Free Software Foundation; either version 2 of the License, or
26189 + * (at your option) any later version.
26190 + *
26191 + * This program is distributed in the hope that it will be useful,
26192 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26193 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26194 + * GNU General Public License for more details.
26195 + *
26196 + * You should have received a copy of the GNU General Public License
26197 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26198 + */
26199 +
26200 +/*
26201 + * move-down, opposite of copy-up
26202 + */
26203 +
26204 +#include "aufs.h"
26205 +
26206 +struct au_mvd_args {
26207 +       struct {
26208 +               struct super_block *h_sb;
26209 +               struct dentry *h_parent;
26210 +               struct au_hinode *hdir;
26211 +               struct inode *h_dir, *h_inode;
26212 +               struct au_pin pin;
26213 +       } info[AUFS_MVDOWN_NARRAY];
26214 +
26215 +       struct aufs_mvdown mvdown;
26216 +       struct dentry *dentry, *parent;
26217 +       struct inode *inode, *dir;
26218 +       struct super_block *sb;
26219 +       aufs_bindex_t bopq, bwh, bfound;
26220 +       unsigned char rename_lock;
26221 +};
26222 +
26223 +#define mvd_errno              mvdown.au_errno
26224 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26225 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26226 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26227 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26228 +
26229 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26230 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26231 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26232 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26233 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26234 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26235 +
26236 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26237 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26238 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26239 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26240 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26241 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26242 +
26243 +#define AU_MVD_PR(flag, ...) do {                      \
26244 +               if (flag)                               \
26245 +                       pr_err(__VA_ARGS__);            \
26246 +       } while (0)
26247 +
26248 +static int find_lower_writable(struct au_mvd_args *a)
26249 +{
26250 +       struct super_block *sb;
26251 +       aufs_bindex_t bindex, bbot;
26252 +       struct au_branch *br;
26253 +
26254 +       sb = a->sb;
26255 +       bindex = a->mvd_bsrc;
26256 +       bbot = au_sbbot(sb);
26257 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26258 +               for (bindex++; bindex <= bbot; bindex++) {
26259 +                       br = au_sbr(sb, bindex);
26260 +                       if (au_br_fhsm(br->br_perm)
26261 +                           && !sb_rdonly(au_br_sb(br)))
26262 +                               return bindex;
26263 +               }
26264 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26265 +               for (bindex++; bindex <= bbot; bindex++) {
26266 +                       br = au_sbr(sb, bindex);
26267 +                       if (!au_br_rdonly(br))
26268 +                               return bindex;
26269 +               }
26270 +       else
26271 +               for (bindex++; bindex <= bbot; bindex++) {
26272 +                       br = au_sbr(sb, bindex);
26273 +                       if (!sb_rdonly(au_br_sb(br))) {
26274 +                               if (au_br_rdonly(br))
26275 +                                       a->mvdown.flags
26276 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26277 +                               return bindex;
26278 +                       }
26279 +               }
26280 +
26281 +       return -1;
26282 +}
26283 +
26284 +/* make the parent dir on bdst */
26285 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26286 +{
26287 +       int err;
26288 +
26289 +       err = 0;
26290 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26291 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26292 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26293 +       a->mvd_h_dst_parent = NULL;
26294 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26295 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26296 +       if (!a->mvd_h_dst_parent) {
26297 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26298 +               if (unlikely(err)) {
26299 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26300 +                       goto out;
26301 +               }
26302 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26303 +       }
26304 +
26305 +out:
26306 +       AuTraceErr(err);
26307 +       return err;
26308 +}
26309 +
26310 +/* lock them all */
26311 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26312 +{
26313 +       int err;
26314 +       struct dentry *h_trap;
26315 +
26316 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26317 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26318 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26319 +                    au_opt_udba(a->sb),
26320 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26321 +       AuTraceErr(err);
26322 +       if (unlikely(err)) {
26323 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26324 +               goto out;
26325 +       }
26326 +
26327 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26328 +               a->rename_lock = 0;
26329 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26330 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26331 +                           au_opt_udba(a->sb),
26332 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26333 +               err = au_do_pin(&a->mvd_pin_src);
26334 +               AuTraceErr(err);
26335 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26336 +               if (unlikely(err)) {
26337 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26338 +                       goto out_dst;
26339 +               }
26340 +               goto out; /* success */
26341 +       }
26342 +
26343 +       a->rename_lock = 1;
26344 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26345 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26346 +                    au_opt_udba(a->sb),
26347 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26348 +       AuTraceErr(err);
26349 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26350 +       if (unlikely(err)) {
26351 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26352 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26353 +               goto out_dst;
26354 +       }
26355 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26356 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26357 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26358 +       if (h_trap) {
26359 +               err = (h_trap != a->mvd_h_src_parent);
26360 +               if (err)
26361 +                       err = (h_trap != a->mvd_h_dst_parent);
26362 +       }
26363 +       BUG_ON(err); /* it should never happen */
26364 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26365 +               err = -EBUSY;
26366 +               AuTraceErr(err);
26367 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26368 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26369 +               au_pin_hdir_lock(&a->mvd_pin_src);
26370 +               au_unpin(&a->mvd_pin_src);
26371 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26372 +               goto out_dst;
26373 +       }
26374 +       goto out; /* success */
26375 +
26376 +out_dst:
26377 +       au_unpin(&a->mvd_pin_dst);
26378 +out:
26379 +       AuTraceErr(err);
26380 +       return err;
26381 +}
26382 +
26383 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26384 +{
26385 +       if (!a->rename_lock)
26386 +               au_unpin(&a->mvd_pin_src);
26387 +       else {
26388 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26389 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26390 +               au_pin_hdir_lock(&a->mvd_pin_src);
26391 +               au_unpin(&a->mvd_pin_src);
26392 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26393 +       }
26394 +       au_unpin(&a->mvd_pin_dst);
26395 +}
26396 +
26397 +/* copy-down the file */
26398 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26399 +{
26400 +       int err;
26401 +       struct au_cp_generic cpg = {
26402 +               .dentry = a->dentry,
26403 +               .bdst   = a->mvd_bdst,
26404 +               .bsrc   = a->mvd_bsrc,
26405 +               .len    = -1,
26406 +               .pin    = &a->mvd_pin_dst,
26407 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26408 +       };
26409 +
26410 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26411 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26412 +               au_fset_cpup(cpg.flags, OVERWRITE);
26413 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26414 +               au_fset_cpup(cpg.flags, RWDST);
26415 +       err = au_sio_cpdown_simple(&cpg);
26416 +       if (unlikely(err))
26417 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26418 +
26419 +       AuTraceErr(err);
26420 +       return err;
26421 +}
26422 +
26423 +/*
26424 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26425 + * were sleeping
26426 + */
26427 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26428 +{
26429 +       int err;
26430 +       struct path h_path;
26431 +       struct au_branch *br;
26432 +       struct inode *delegated;
26433 +
26434 +       br = au_sbr(a->sb, a->mvd_bdst);
26435 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26436 +       err = PTR_ERR(h_path.dentry);
26437 +       if (IS_ERR(h_path.dentry)) {
26438 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26439 +               goto out;
26440 +       }
26441 +
26442 +       err = 0;
26443 +       if (d_is_positive(h_path.dentry)) {
26444 +               h_path.mnt = au_br_mnt(br);
26445 +               delegated = NULL;
26446 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26447 +                                  &delegated, /*force*/0);
26448 +               if (unlikely(err == -EWOULDBLOCK)) {
26449 +                       pr_warn("cannot retry for NFSv4 delegation"
26450 +                               " for an internal unlink\n");
26451 +                       iput(delegated);
26452 +               }
26453 +               if (unlikely(err))
26454 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26455 +       }
26456 +       dput(h_path.dentry);
26457 +
26458 +out:
26459 +       AuTraceErr(err);
26460 +       return err;
26461 +}
26462 +
26463 +/*
26464 + * unlink the topmost h_dentry
26465 + */
26466 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26467 +{
26468 +       int err;
26469 +       struct path h_path;
26470 +       struct inode *delegated;
26471 +
26472 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26473 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26474 +       delegated = NULL;
26475 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26476 +       if (unlikely(err == -EWOULDBLOCK)) {
26477 +               pr_warn("cannot retry for NFSv4 delegation"
26478 +                       " for an internal unlink\n");
26479 +               iput(delegated);
26480 +       }
26481 +       if (unlikely(err))
26482 +               AU_MVD_PR(dmsg, "unlink failed\n");
26483 +
26484 +       AuTraceErr(err);
26485 +       return err;
26486 +}
26487 +
26488 +/* Since mvdown succeeded, we ignore an error of this function */
26489 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26490 +{
26491 +       int err;
26492 +       struct au_branch *br;
26493 +
26494 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26495 +       br = au_sbr(a->sb, a->mvd_bsrc);
26496 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26497 +       if (!err) {
26498 +               br = au_sbr(a->sb, a->mvd_bdst);
26499 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26500 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26501 +       }
26502 +       if (!err)
26503 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26504 +       else
26505 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26506 +}
26507 +
26508 +/*
26509 + * copy-down the file and unlink the bsrc file.
26510 + * - unlink the bdst whout if exist
26511 + * - copy-down the file (with whtmp name and rename)
26512 + * - unlink the bsrc file
26513 + */
26514 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26515 +{
26516 +       int err;
26517 +
26518 +       err = au_do_mkdir(dmsg, a);
26519 +       if (!err)
26520 +               err = au_do_lock(dmsg, a);
26521 +       if (unlikely(err))
26522 +               goto out;
26523 +
26524 +       /*
26525 +        * do not revert the activities we made on bdst since they should be
26526 +        * harmless in aufs.
26527 +        */
26528 +
26529 +       err = au_do_cpdown(dmsg, a);
26530 +       if (!err)
26531 +               err = au_do_unlink_wh(dmsg, a);
26532 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26533 +               err = au_do_unlink(dmsg, a);
26534 +       if (unlikely(err))
26535 +               goto out_unlock;
26536 +
26537 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26538 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26539 +       if (find_lower_writable(a) < 0)
26540 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26541 +
26542 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26543 +               au_do_stfs(dmsg, a);
26544 +
26545 +       /* maintain internal array */
26546 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26547 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26548 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26549 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26550 +               au_set_ibtop(a->inode, a->mvd_bdst);
26551 +       } else {
26552 +               /* hide the lower */
26553 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26554 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26555 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26556 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26557 +       }
26558 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26559 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26560 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26561 +               au_set_ibbot(a->inode, a->mvd_bdst);
26562 +
26563 +out_unlock:
26564 +       au_do_unlock(dmsg, a);
26565 +out:
26566 +       AuTraceErr(err);
26567 +       return err;
26568 +}
26569 +
26570 +/* ---------------------------------------------------------------------- */
26571 +
26572 +/* make sure the file is idle */
26573 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26574 +{
26575 +       int err, plinked;
26576 +
26577 +       err = 0;
26578 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26579 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26580 +           && au_dcount(a->dentry) == 1
26581 +           && atomic_read(&a->inode->i_count) == 1
26582 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26583 +           && (!plinked || !au_plink_test(a->inode))
26584 +           && a->inode->i_nlink == 1)
26585 +               goto out;
26586 +
26587 +       err = -EBUSY;
26588 +       AU_MVD_PR(dmsg,
26589 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26590 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26591 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26592 +                 a->mvd_h_src_inode->i_nlink,
26593 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26594 +
26595 +out:
26596 +       AuTraceErr(err);
26597 +       return err;
26598 +}
26599 +
26600 +/* make sure the parent dir is fine */
26601 +static int au_mvd_args_parent(const unsigned char dmsg,
26602 +                             struct au_mvd_args *a)
26603 +{
26604 +       int err;
26605 +       aufs_bindex_t bindex;
26606 +
26607 +       err = 0;
26608 +       if (unlikely(au_alive_dir(a->parent))) {
26609 +               err = -ENOENT;
26610 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26611 +               goto out;
26612 +       }
26613 +
26614 +       a->bopq = au_dbdiropq(a->parent);
26615 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26616 +       AuDbg("b%d\n", bindex);
26617 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26618 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26619 +               err = -EINVAL;
26620 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26621 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26622 +                         a->bopq, a->mvd_bdst);
26623 +       }
26624 +
26625 +out:
26626 +       AuTraceErr(err);
26627 +       return err;
26628 +}
26629 +
26630 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26631 +                                   struct au_mvd_args *a)
26632 +{
26633 +       int err;
26634 +       struct au_dinfo *dinfo, *tmp;
26635 +
26636 +       /* lookup the next lower positive entry */
26637 +       err = -ENOMEM;
26638 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26639 +       if (unlikely(!tmp))
26640 +               goto out;
26641 +
26642 +       a->bfound = -1;
26643 +       a->bwh = -1;
26644 +       dinfo = au_di(a->dentry);
26645 +       au_di_cp(tmp, dinfo);
26646 +       au_di_swap(tmp, dinfo);
26647 +
26648 +       /* returns the number of positive dentries */
26649 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26650 +                            /* AuLkup_IGNORE_PERM */ 0);
26651 +       if (!err)
26652 +               a->bwh = au_dbwh(a->dentry);
26653 +       else if (err > 0)
26654 +               a->bfound = au_dbtop(a->dentry);
26655 +
26656 +       au_di_swap(tmp, dinfo);
26657 +       au_rw_write_unlock(&tmp->di_rwsem);
26658 +       au_di_free(tmp);
26659 +       if (unlikely(err < 0))
26660 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26661 +
26662 +       /*
26663 +        * here, we have these cases.
26664 +        * bfound == -1
26665 +        *      no positive dentry under bsrc. there are more sub-cases.
26666 +        *      bwh < 0
26667 +        *              there no whiteout, we can safely move-down.
26668 +        *      bwh <= bsrc
26669 +        *              impossible
26670 +        *      bsrc < bwh && bwh < bdst
26671 +        *              there is a whiteout on RO branch. cannot proceed.
26672 +        *      bwh == bdst
26673 +        *              there is a whiteout on the RW target branch. it should
26674 +        *              be removed.
26675 +        *      bdst < bwh
26676 +        *              there is a whiteout somewhere unrelated branch.
26677 +        * -1 < bfound && bfound <= bsrc
26678 +        *      impossible.
26679 +        * bfound < bdst
26680 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26681 +        *      proceed.
26682 +        * bfound == bdst
26683 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26684 +        *      error.
26685 +        * bdst < bfound
26686 +        *      found, after we create the file on bdst, it will be hidden.
26687 +        */
26688 +
26689 +       AuDebugOn(a->bfound == -1
26690 +                 && a->bwh != -1
26691 +                 && a->bwh <= a->mvd_bsrc);
26692 +       AuDebugOn(-1 < a->bfound
26693 +                 && a->bfound <= a->mvd_bsrc);
26694 +
26695 +       err = -EINVAL;
26696 +       if (a->bfound == -1
26697 +           && a->mvd_bsrc < a->bwh
26698 +           && a->bwh != -1
26699 +           && a->bwh < a->mvd_bdst) {
26700 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26701 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26702 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26703 +               goto out;
26704 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26705 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26706 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26707 +                         a->mvd_bdst, a->bfound);
26708 +               goto out;
26709 +       }
26710 +
26711 +       err = 0; /* success */
26712 +
26713 +out:
26714 +       AuTraceErr(err);
26715 +       return err;
26716 +}
26717 +
26718 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26719 +{
26720 +       int err;
26721 +
26722 +       err = 0;
26723 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26724 +           && a->bfound == a->mvd_bdst)
26725 +               err = -EEXIST;
26726 +       AuTraceErr(err);
26727 +       return err;
26728 +}
26729 +
26730 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26731 +{
26732 +       int err;
26733 +       struct au_branch *br;
26734 +
26735 +       err = -EISDIR;
26736 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26737 +               goto out;
26738 +
26739 +       err = -EINVAL;
26740 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26741 +               a->mvd_bsrc = au_ibtop(a->inode);
26742 +       else {
26743 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26744 +               if (unlikely(a->mvd_bsrc < 0
26745 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26746 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26747 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26748 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26749 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26750 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26751 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26752 +                       AU_MVD_PR(dmsg, "no upper\n");
26753 +                       goto out;
26754 +               }
26755 +       }
26756 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26757 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26758 +               AU_MVD_PR(dmsg, "on the bottom\n");
26759 +               goto out;
26760 +       }
26761 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26762 +       br = au_sbr(a->sb, a->mvd_bsrc);
26763 +       err = au_br_rdonly(br);
26764 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26765 +               if (unlikely(err))
26766 +                       goto out;
26767 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26768 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26769 +               if (err)
26770 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26771 +               /* go on */
26772 +       } else
26773 +               goto out;
26774 +
26775 +       err = -EINVAL;
26776 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26777 +               a->mvd_bdst = find_lower_writable(a);
26778 +               if (unlikely(a->mvd_bdst < 0)) {
26779 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26780 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26781 +                       goto out;
26782 +               }
26783 +       } else {
26784 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26785 +               if (unlikely(a->mvd_bdst < 0
26786 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26787 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26788 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26789 +                       goto out;
26790 +               }
26791 +       }
26792 +
26793 +       err = au_mvd_args_busy(dmsg, a);
26794 +       if (!err)
26795 +               err = au_mvd_args_parent(dmsg, a);
26796 +       if (!err)
26797 +               err = au_mvd_args_intermediate(dmsg, a);
26798 +       if (!err)
26799 +               err = au_mvd_args_exist(dmsg, a);
26800 +       if (!err)
26801 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26802 +
26803 +out:
26804 +       AuTraceErr(err);
26805 +       return err;
26806 +}
26807 +
26808 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26809 +{
26810 +       int err, e;
26811 +       unsigned char dmsg;
26812 +       struct au_mvd_args *args;
26813 +       struct inode *inode;
26814 +
26815 +       inode = d_inode(dentry);
26816 +       err = -EPERM;
26817 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26818 +               goto out;
26819 +
26820 +       err = -ENOMEM;
26821 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26822 +       if (unlikely(!args))
26823 +               goto out;
26824 +
26825 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26826 +       if (!err)
26827 +               /* VERIFY_WRITE */
26828 +               err = !access_ok(uarg, sizeof(*uarg));
26829 +       if (unlikely(err)) {
26830 +               err = -EFAULT;
26831 +               AuTraceErr(err);
26832 +               goto out_free;
26833 +       }
26834 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26835 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26836 +       args->mvdown.au_errno = 0;
26837 +       args->dentry = dentry;
26838 +       args->inode = inode;
26839 +       args->sb = dentry->d_sb;
26840 +
26841 +       err = -ENOENT;
26842 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26843 +       args->parent = dget_parent(dentry);
26844 +       args->dir = d_inode(args->parent);
26845 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26846 +       dput(args->parent);
26847 +       if (unlikely(args->parent != dentry->d_parent)) {
26848 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26849 +               goto out_dir;
26850 +       }
26851 +
26852 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26853 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26854 +       if (unlikely(err))
26855 +               goto out_inode;
26856 +
26857 +       di_write_lock_parent(args->parent);
26858 +       err = au_mvd_args(dmsg, args);
26859 +       if (unlikely(err))
26860 +               goto out_parent;
26861 +
26862 +       err = au_do_mvdown(dmsg, args);
26863 +       if (unlikely(err))
26864 +               goto out_parent;
26865 +
26866 +       au_cpup_attr_timesizes(args->dir);
26867 +       au_cpup_attr_timesizes(inode);
26868 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26869 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26870 +       /* au_digen_dec(dentry); */
26871 +
26872 +out_parent:
26873 +       di_write_unlock(args->parent);
26874 +       aufs_read_unlock(dentry, AuLock_DW);
26875 +out_inode:
26876 +       inode_unlock(inode);
26877 +out_dir:
26878 +       inode_unlock(args->dir);
26879 +out_free:
26880 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26881 +       if (unlikely(e))
26882 +               err = -EFAULT;
26883 +       au_kfree_rcu(args);
26884 +out:
26885 +       AuTraceErr(err);
26886 +       return err;
26887 +}
26888 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26889 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26890 +++ linux/fs/aufs/opts.c        2022-11-05 23:02:18.969222617 +0100
26891 @@ -0,0 +1,1032 @@
26892 +// SPDX-License-Identifier: GPL-2.0
26893 +/*
26894 + * Copyright (C) 2005-2022 Junjiro R. Okajima
26895 + *
26896 + * This program is free software; you can redistribute it and/or modify
26897 + * it under the terms of the GNU General Public License as published by
26898 + * the Free Software Foundation; either version 2 of the License, or
26899 + * (at your option) any later version.
26900 + *
26901 + * This program is distributed in the hope that it will be useful,
26902 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26903 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26904 + * GNU General Public License for more details.
26905 + *
26906 + * You should have received a copy of the GNU General Public License
26907 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26908 + */
26909 +
26910 +/*
26911 + * mount options/flags
26912 + */
26913 +
26914 +#include <linux/types.h> /* a distribution requires */
26915 +#include <linux/parser.h>
26916 +#include "aufs.h"
26917 +
26918 +/* ---------------------------------------------------------------------- */
26919 +
26920 +static const char *au_parser_pattern(int val, match_table_t tbl)
26921 +{
26922 +       struct match_token *p;
26923 +
26924 +       p = tbl;
26925 +       while (p->pattern) {
26926 +               if (p->token == val)
26927 +                       return p->pattern;
26928 +               p++;
26929 +       }
26930 +       BUG();
26931 +       return "??";
26932 +}
26933 +
26934 +static const char *au_optstr(int *val, match_table_t tbl)
26935 +{
26936 +       struct match_token *p;
26937 +       int v;
26938 +
26939 +       v = *val;
26940 +       if (!v)
26941 +               goto out;
26942 +       p = tbl;
26943 +       while (p->pattern) {
26944 +               if (p->token
26945 +                   && (v & p->token) == p->token) {
26946 +                       *val &= ~p->token;
26947 +                       return p->pattern;
26948 +               }
26949 +               p++;
26950 +       }
26951 +
26952 +out:
26953 +       return NULL;
26954 +}
26955 +
26956 +/* ---------------------------------------------------------------------- */
26957 +
26958 +static match_table_t brperm = {
26959 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26960 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26961 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26962 +       {0, NULL}
26963 +};
26964 +
26965 +static match_table_t brattr = {
26966 +       /* general */
26967 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26968 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26969 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26970 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26971 +#ifdef CONFIG_AUFS_FHSM
26972 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26973 +#endif
26974 +#ifdef CONFIG_AUFS_XATTR
26975 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26976 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26977 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26978 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26979 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26980 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26981 +#endif
26982 +
26983 +       /* ro/rr branch */
26984 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26985 +
26986 +       /* rw branch */
26987 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26988 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26989 +
26990 +       {0, NULL}
26991 +};
26992 +
26993 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26994 +{
26995 +       int attr, v;
26996 +       char *p;
26997 +
26998 +       attr = 0;
26999 +       do {
27000 +               p = strchr(str, '+');
27001 +               if (p)
27002 +                       *p = 0;
27003 +               v = match_token(str, table, args);
27004 +               if (v) {
27005 +                       if (v & AuBrAttr_CMOO_Mask)
27006 +                               attr &= ~AuBrAttr_CMOO_Mask;
27007 +                       attr |= v;
27008 +               } else {
27009 +                       if (p)
27010 +                               *p = '+';
27011 +                       pr_warn("ignored branch attribute %s\n", str);
27012 +                       break;
27013 +               }
27014 +               if (p)
27015 +                       str = p + 1;
27016 +       } while (p);
27017 +
27018 +       return attr;
27019 +}
27020 +
27021 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
27022 +{
27023 +       int sz;
27024 +       const char *p;
27025 +       char *q;
27026 +
27027 +       q = str->a;
27028 +       *q = 0;
27029 +       p = au_optstr(&perm, brattr);
27030 +       if (p) {
27031 +               sz = strlen(p);
27032 +               memcpy(q, p, sz + 1);
27033 +               q += sz;
27034 +       } else
27035 +               goto out;
27036 +
27037 +       do {
27038 +               p = au_optstr(&perm, brattr);
27039 +               if (p) {
27040 +                       *q++ = '+';
27041 +                       sz = strlen(p);
27042 +                       memcpy(q, p, sz + 1);
27043 +                       q += sz;
27044 +               }
27045 +       } while (p);
27046 +
27047 +out:
27048 +       return q - str->a;
27049 +}
27050 +
27051 +int au_br_perm_val(char *perm)
27052 +{
27053 +       int val, bad, sz;
27054 +       char *p;
27055 +       substring_t args[MAX_OPT_ARGS];
27056 +       au_br_perm_str_t attr;
27057 +
27058 +       p = strchr(perm, '+');
27059 +       if (p)
27060 +               *p = 0;
27061 +       val = match_token(perm, brperm, args);
27062 +       if (!val) {
27063 +               if (p)
27064 +                       *p = '+';
27065 +               pr_warn("ignored branch permission %s\n", perm);
27066 +               val = AuBrPerm_RO;
27067 +               goto out;
27068 +       }
27069 +       if (!p)
27070 +               goto out;
27071 +
27072 +       val |= br_attr_val(p + 1, brattr, args);
27073 +
27074 +       bad = 0;
27075 +       switch (val & AuBrPerm_Mask) {
27076 +       case AuBrPerm_RO:
27077 +       case AuBrPerm_RR:
27078 +               bad = val & AuBrWAttr_Mask;
27079 +               val &= ~AuBrWAttr_Mask;
27080 +               break;
27081 +       case AuBrPerm_RW:
27082 +               bad = val & AuBrRAttr_Mask;
27083 +               val &= ~AuBrRAttr_Mask;
27084 +               break;
27085 +       }
27086 +
27087 +       /*
27088 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27089 +        * does not treat it as an error, just warning.
27090 +        * this is a tiny guard for the user operation.
27091 +        */
27092 +       if (val & AuBrAttr_UNPIN) {
27093 +               bad |= AuBrAttr_UNPIN;
27094 +               val &= ~AuBrAttr_UNPIN;
27095 +       }
27096 +
27097 +       if (unlikely(bad)) {
27098 +               sz = au_do_optstr_br_attr(&attr, bad);
27099 +               AuDebugOn(!sz);
27100 +               pr_warn("ignored branch attribute %s\n", attr.a);
27101 +       }
27102 +
27103 +out:
27104 +       return val;
27105 +}
27106 +
27107 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27108 +{
27109 +       au_br_perm_str_t attr;
27110 +       const char *p;
27111 +       char *q;
27112 +       int sz;
27113 +
27114 +       q = str->a;
27115 +       p = au_optstr(&perm, brperm);
27116 +       AuDebugOn(!p || !*p);
27117 +       sz = strlen(p);
27118 +       memcpy(q, p, sz + 1);
27119 +       q += sz;
27120 +
27121 +       sz = au_do_optstr_br_attr(&attr, perm);
27122 +       if (sz) {
27123 +               *q++ = '+';
27124 +               memcpy(q, attr.a, sz + 1);
27125 +       }
27126 +
27127 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27128 +}
27129 +
27130 +/* ---------------------------------------------------------------------- */
27131 +
27132 +static match_table_t udbalevel = {
27133 +       {AuOpt_UDBA_REVAL, "reval"},
27134 +       {AuOpt_UDBA_NONE, "none"},
27135 +#ifdef CONFIG_AUFS_HNOTIFY
27136 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27137 +#ifdef CONFIG_AUFS_HFSNOTIFY
27138 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27139 +#endif
27140 +#endif
27141 +       {-1, NULL}
27142 +};
27143 +
27144 +int au_udba_val(char *str)
27145 +{
27146 +       substring_t args[MAX_OPT_ARGS];
27147 +
27148 +       return match_token(str, udbalevel, args);
27149 +}
27150 +
27151 +const char *au_optstr_udba(int udba)
27152 +{
27153 +       return au_parser_pattern(udba, udbalevel);
27154 +}
27155 +
27156 +/* ---------------------------------------------------------------------- */
27157 +
27158 +static match_table_t au_wbr_create_policy = {
27159 +       {AuWbrCreate_TDP, "tdp"},
27160 +       {AuWbrCreate_TDP, "top-down-parent"},
27161 +       {AuWbrCreate_RR, "rr"},
27162 +       {AuWbrCreate_RR, "round-robin"},
27163 +       {AuWbrCreate_MFS, "mfs"},
27164 +       {AuWbrCreate_MFS, "most-free-space"},
27165 +       {AuWbrCreate_MFSV, "mfs:%d"},
27166 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27167 +
27168 +       /* top-down regardless the parent, and then mfs */
27169 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27170 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27171 +
27172 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27173 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27174 +       {AuWbrCreate_PMFS, "pmfs"},
27175 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27176 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27177 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27178 +
27179 +       {-1, NULL}
27180 +};
27181 +
27182 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27183 +                           struct au_opt_wbr_create *create)
27184 +{
27185 +       int err;
27186 +       unsigned long long ull;
27187 +
27188 +       err = 0;
27189 +       if (!match_u64(arg, &ull))
27190 +               create->mfsrr_watermark = ull;
27191 +       else {
27192 +               pr_err("bad integer in %s\n", str);
27193 +               err = -EINVAL;
27194 +       }
27195 +
27196 +       return err;
27197 +}
27198 +
27199 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27200 +                         struct au_opt_wbr_create *create)
27201 +{
27202 +       int n, err;
27203 +
27204 +       err = 0;
27205 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27206 +               create->mfs_second = n;
27207 +       else {
27208 +               pr_err("bad integer in %s\n", str);
27209 +               err = -EINVAL;
27210 +       }
27211 +
27212 +       return err;
27213 +}
27214 +
27215 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27216 +{
27217 +       int err, e;
27218 +       substring_t args[MAX_OPT_ARGS];
27219 +
27220 +       err = match_token(str, au_wbr_create_policy, args);
27221 +       create->wbr_create = err;
27222 +       switch (err) {
27223 +       case AuWbrCreate_MFSRRV:
27224 +       case AuWbrCreate_TDMFSV:
27225 +       case AuWbrCreate_PMFSRRV:
27226 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27227 +               if (!e)
27228 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27229 +               if (unlikely(e))
27230 +                       err = e;
27231 +               break;
27232 +       case AuWbrCreate_MFSRR:
27233 +       case AuWbrCreate_TDMFS:
27234 +       case AuWbrCreate_PMFSRR:
27235 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27236 +               if (unlikely(e)) {
27237 +                       err = e;
27238 +                       break;
27239 +               }
27240 +               fallthrough;
27241 +       case AuWbrCreate_MFS:
27242 +       case AuWbrCreate_PMFS:
27243 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27244 +               break;
27245 +       case AuWbrCreate_MFSV:
27246 +       case AuWbrCreate_PMFSV:
27247 +               e = au_wbr_mfs_sec(&args[0], str, create);
27248 +               if (unlikely(e))
27249 +                       err = e;
27250 +               break;
27251 +       }
27252 +
27253 +       return err;
27254 +}
27255 +
27256 +const char *au_optstr_wbr_create(int wbr_create)
27257 +{
27258 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27259 +}
27260 +
27261 +static match_table_t au_wbr_copyup_policy = {
27262 +       {AuWbrCopyup_TDP, "tdp"},
27263 +       {AuWbrCopyup_TDP, "top-down-parent"},
27264 +       {AuWbrCopyup_BUP, "bup"},
27265 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27266 +       {AuWbrCopyup_BU, "bu"},
27267 +       {AuWbrCopyup_BU, "bottom-up"},
27268 +       {-1, NULL}
27269 +};
27270 +
27271 +int au_wbr_copyup_val(char *str)
27272 +{
27273 +       substring_t args[MAX_OPT_ARGS];
27274 +
27275 +       return match_token(str, au_wbr_copyup_policy, args);
27276 +}
27277 +
27278 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27279 +{
27280 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27281 +}
27282 +
27283 +/* ---------------------------------------------------------------------- */
27284 +
27285 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27286 +              aufs_bindex_t bindex)
27287 +{
27288 +       int err;
27289 +       struct au_opt_add *add = &opt->add;
27290 +       char *p;
27291 +
27292 +       add->bindex = bindex;
27293 +       add->perm = AuBrPerm_RO;
27294 +       add->pathname = opt_str;
27295 +       p = strchr(opt_str, '=');
27296 +       if (p) {
27297 +               *p++ = 0;
27298 +               if (*p)
27299 +                       add->perm = au_br_perm_val(p);
27300 +       }
27301 +
27302 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27303 +       if (!err) {
27304 +               if (!p) {
27305 +                       add->perm = AuBrPerm_RO;
27306 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27307 +                               add->perm = AuBrPerm_RR;
27308 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27309 +                               add->perm = AuBrPerm_RW;
27310 +               }
27311 +               opt->type = Opt_add;
27312 +               goto out;
27313 +       }
27314 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27315 +       err = -EINVAL;
27316 +
27317 +out:
27318 +       return err;
27319 +}
27320 +
27321 +static int au_opt_wbr_create(struct super_block *sb,
27322 +                            struct au_opt_wbr_create *create)
27323 +{
27324 +       int err;
27325 +       struct au_sbinfo *sbinfo;
27326 +
27327 +       SiMustWriteLock(sb);
27328 +
27329 +       err = 1; /* handled */
27330 +       sbinfo = au_sbi(sb);
27331 +       if (sbinfo->si_wbr_create_ops->fin) {
27332 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27333 +               if (!err)
27334 +                       err = 1;
27335 +       }
27336 +
27337 +       sbinfo->si_wbr_create = create->wbr_create;
27338 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27339 +       switch (create->wbr_create) {
27340 +       case AuWbrCreate_MFSRRV:
27341 +       case AuWbrCreate_MFSRR:
27342 +       case AuWbrCreate_TDMFS:
27343 +       case AuWbrCreate_TDMFSV:
27344 +       case AuWbrCreate_PMFSRR:
27345 +       case AuWbrCreate_PMFSRRV:
27346 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27347 +               fallthrough;
27348 +       case AuWbrCreate_MFS:
27349 +       case AuWbrCreate_MFSV:
27350 +       case AuWbrCreate_PMFS:
27351 +       case AuWbrCreate_PMFSV:
27352 +               sbinfo->si_wbr_mfs.mfs_expire
27353 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27354 +               break;
27355 +       }
27356 +
27357 +       if (sbinfo->si_wbr_create_ops->init)
27358 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27359 +
27360 +       return err;
27361 +}
27362 +
27363 +/*
27364 + * returns,
27365 + * plus: processed without an error
27366 + * zero: unprocessed
27367 + */
27368 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27369 +                        struct au_opts *opts)
27370 +{
27371 +       int err;
27372 +       struct au_sbinfo *sbinfo;
27373 +
27374 +       SiMustWriteLock(sb);
27375 +
27376 +       err = 1; /* handled */
27377 +       sbinfo = au_sbi(sb);
27378 +       switch (opt->type) {
27379 +       case Opt_udba:
27380 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27381 +               sbinfo->si_mntflags |= opt->udba;
27382 +               opts->given_udba |= opt->udba;
27383 +               break;
27384 +
27385 +       case Opt_plink:
27386 +               if (opt->tf)
27387 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27388 +               else {
27389 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27390 +                               au_plink_put(sb, /*verbose*/1);
27391 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27392 +               }
27393 +               break;
27394 +       case Opt_list_plink:
27395 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27396 +                       au_plink_list(sb);
27397 +               break;
27398 +
27399 +       case Opt_dio:
27400 +               if (opt->tf) {
27401 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27402 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27403 +               } else {
27404 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27405 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27406 +               }
27407 +               break;
27408 +
27409 +       case Opt_fhsm_sec:
27410 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27411 +               break;
27412 +
27413 +       case Opt_diropq_a:
27414 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27415 +               break;
27416 +       case Opt_diropq_w:
27417 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27418 +               break;
27419 +
27420 +       case Opt_warn_perm:
27421 +               if (opt->tf)
27422 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27423 +               else
27424 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27425 +               break;
27426 +
27427 +       case Opt_verbose:
27428 +               if (opt->tf)
27429 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27430 +               else
27431 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27432 +               break;
27433 +
27434 +       case Opt_sum:
27435 +               if (opt->tf)
27436 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27437 +               else {
27438 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27439 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27440 +               }
27441 +               break;
27442 +       case Opt_wsum:
27443 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27444 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27445 +               break;
27446 +
27447 +       case Opt_wbr_create:
27448 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27449 +               break;
27450 +       case Opt_wbr_copyup:
27451 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27452 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27453 +               break;
27454 +
27455 +       case Opt_dirwh:
27456 +               sbinfo->si_dirwh = opt->dirwh;
27457 +               break;
27458 +
27459 +       case Opt_rdcache:
27460 +               sbinfo->si_rdcache
27461 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27462 +               break;
27463 +       case Opt_rdblk:
27464 +               sbinfo->si_rdblk = opt->rdblk;
27465 +               break;
27466 +       case Opt_rdhash:
27467 +               sbinfo->si_rdhash = opt->rdhash;
27468 +               break;
27469 +
27470 +       case Opt_shwh:
27471 +               if (opt->tf)
27472 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27473 +               else
27474 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27475 +               break;
27476 +
27477 +       case Opt_dirperm1:
27478 +               if (opt->tf)
27479 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27480 +               else
27481 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27482 +               break;
27483 +
27484 +       case Opt_trunc_xino:
27485 +               if (opt->tf)
27486 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27487 +               else
27488 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27489 +               break;
27490 +
27491 +       case Opt_trunc_xino_path:
27492 +       case Opt_itrunc_xino:
27493 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27494 +                                   /*idx_begin*/0);
27495 +               if (!err)
27496 +                       err = 1;
27497 +               break;
27498 +
27499 +       case Opt_trunc_xib:
27500 +               if (opt->tf)
27501 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27502 +               else
27503 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27504 +               break;
27505 +
27506 +       case Opt_dirren:
27507 +               err = 1;
27508 +               if (opt->tf) {
27509 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27510 +                               err = au_dr_opt_set(sb);
27511 +                               if (!err)
27512 +                                       err = 1;
27513 +                       }
27514 +                       if (err == 1)
27515 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27516 +               } else {
27517 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27518 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27519 +                                                                     DR_FLUSHED));
27520 +                               if (!err)
27521 +                                       err = 1;
27522 +                       }
27523 +                       if (err == 1)
27524 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27525 +               }
27526 +               break;
27527 +
27528 +       case Opt_acl:
27529 +               if (opt->tf)
27530 +                       sb->s_flags |= SB_POSIXACL;
27531 +               else
27532 +                       sb->s_flags &= ~SB_POSIXACL;
27533 +               break;
27534 +
27535 +       default:
27536 +               err = 0;
27537 +               break;
27538 +       }
27539 +
27540 +       return err;
27541 +}
27542 +
27543 +/*
27544 + * returns tri-state.
27545 + * plus: processed without an error
27546 + * zero: unprocessed
27547 + * minus: error
27548 + */
27549 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27550 +                    struct au_opts *opts)
27551 +{
27552 +       int err, do_refresh;
27553 +
27554 +       err = 0;
27555 +       switch (opt->type) {
27556 +       case Opt_append:
27557 +               opt->add.bindex = au_sbbot(sb) + 1;
27558 +               if (opt->add.bindex < 0)
27559 +                       opt->add.bindex = 0;
27560 +               goto add;
27561 +               /* Always goto add, not fallthrough */
27562 +       case Opt_prepend:
27563 +               opt->add.bindex = 0;
27564 +               fallthrough;
27565 +       add: /* indented label */
27566 +       case Opt_add:
27567 +               err = au_br_add(sb, &opt->add,
27568 +                               au_ftest_opts(opts->flags, REMOUNT));
27569 +               if (!err) {
27570 +                       err = 1;
27571 +                       au_fset_opts(opts->flags, REFRESH);
27572 +               }
27573 +               break;
27574 +
27575 +       case Opt_del:
27576 +       case Opt_idel:
27577 +               err = au_br_del(sb, &opt->del,
27578 +                               au_ftest_opts(opts->flags, REMOUNT));
27579 +               if (!err) {
27580 +                       err = 1;
27581 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27582 +                       au_fset_opts(opts->flags, REFRESH);
27583 +               }
27584 +               break;
27585 +
27586 +       case Opt_mod:
27587 +       case Opt_imod:
27588 +               err = au_br_mod(sb, &opt->mod,
27589 +                               au_ftest_opts(opts->flags, REMOUNT),
27590 +                               &do_refresh);
27591 +               if (!err) {
27592 +                       err = 1;
27593 +                       if (do_refresh)
27594 +                               au_fset_opts(opts->flags, REFRESH);
27595 +               }
27596 +               break;
27597 +       }
27598 +       return err;
27599 +}
27600 +
27601 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27602 +                      struct au_opt_xino **opt_xino,
27603 +                      struct au_opts *opts)
27604 +{
27605 +       int err;
27606 +
27607 +       err = 0;
27608 +       switch (opt->type) {
27609 +       case Opt_xino:
27610 +               err = au_xino_set(sb, &opt->xino,
27611 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27612 +               if (!err)
27613 +                       *opt_xino = &opt->xino;
27614 +               break;
27615 +       case Opt_noxino:
27616 +               au_xino_clr(sb);
27617 +               *opt_xino = (void *)-1;
27618 +               break;
27619 +       }
27620 +
27621 +       return err;
27622 +}
27623 +
27624 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27625 +                  unsigned int pending)
27626 +{
27627 +       int err, fhsm;
27628 +       aufs_bindex_t bindex, bbot;
27629 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27630 +       struct au_branch *br;
27631 +       struct au_wbr *wbr;
27632 +       struct dentry *root, *dentry;
27633 +       struct inode *dir, *h_dir;
27634 +       struct au_sbinfo *sbinfo;
27635 +       struct au_hinode *hdir;
27636 +
27637 +       SiMustAnyLock(sb);
27638 +
27639 +       sbinfo = au_sbi(sb);
27640 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27641 +
27642 +       if (!(sb_flags & SB_RDONLY)) {
27643 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27644 +                       pr_warn("first branch should be rw\n");
27645 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27646 +                       pr_warn_once("shwh should be used with ro\n");
27647 +       }
27648 +
27649 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27650 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27651 +               pr_warn_once("udba=*notify requires xino\n");
27652 +
27653 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27654 +               pr_warn_once("dirperm1 breaks the protection"
27655 +                            " by the permission bits on the lower branch\n");
27656 +
27657 +       err = 0;
27658 +       fhsm = 0;
27659 +       root = sb->s_root;
27660 +       dir = d_inode(root);
27661 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27662 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27663 +                                     UDBA_NONE);
27664 +       bbot = au_sbbot(sb);
27665 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27666 +               skip = 0;
27667 +               h_dir = au_h_iptr(dir, bindex);
27668 +               br = au_sbr(sb, bindex);
27669 +
27670 +               if ((br->br_perm & AuBrAttr_ICEX)
27671 +                   && !h_dir->i_op->listxattr)
27672 +                       br->br_perm &= ~AuBrAttr_ICEX;
27673 +#if 0 /* untested */
27674 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27675 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27676 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27677 +#endif
27678 +
27679 +               do_free = 0;
27680 +               wbr = br->br_wbr;
27681 +               if (wbr)
27682 +                       wbr_wh_read_lock(wbr);
27683 +
27684 +               if (!au_br_writable(br->br_perm)) {
27685 +                       do_free = !!wbr;
27686 +                       skip = (!wbr
27687 +                               || (!wbr->wbr_whbase
27688 +                                   && !wbr->wbr_plink
27689 +                                   && !wbr->wbr_orph));
27690 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27691 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27692 +                       skip = (!wbr || !wbr->wbr_whbase);
27693 +                       if (skip && wbr) {
27694 +                               if (do_plink)
27695 +                                       skip = !!wbr->wbr_plink;
27696 +                               else
27697 +                                       skip = !wbr->wbr_plink;
27698 +                       }
27699 +               } else {
27700 +                       /* skip = (br->br_whbase && br->br_ohph); */
27701 +                       skip = (wbr && wbr->wbr_whbase);
27702 +                       if (skip) {
27703 +                               if (do_plink)
27704 +                                       skip = !!wbr->wbr_plink;
27705 +                               else
27706 +                                       skip = !wbr->wbr_plink;
27707 +                       }
27708 +               }
27709 +               if (wbr)
27710 +                       wbr_wh_read_unlock(wbr);
27711 +
27712 +               if (can_no_dreval) {
27713 +                       dentry = br->br_path.dentry;
27714 +                       spin_lock(&dentry->d_lock);
27715 +                       if (dentry->d_flags &
27716 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27717 +                               can_no_dreval = 0;
27718 +                       spin_unlock(&dentry->d_lock);
27719 +               }
27720 +
27721 +               if (au_br_fhsm(br->br_perm)) {
27722 +                       fhsm++;
27723 +                       AuDebugOn(!br->br_fhsm);
27724 +               }
27725 +
27726 +               if (skip)
27727 +                       continue;
27728 +
27729 +               hdir = au_hi(dir, bindex);
27730 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27731 +               if (wbr)
27732 +                       wbr_wh_write_lock(wbr);
27733 +               err = au_wh_init(br, sb);
27734 +               if (wbr)
27735 +                       wbr_wh_write_unlock(wbr);
27736 +               au_hn_inode_unlock(hdir);
27737 +
27738 +               if (!err && do_free) {
27739 +                       au_kfree_rcu(wbr);
27740 +                       br->br_wbr = NULL;
27741 +               }
27742 +       }
27743 +
27744 +       if (can_no_dreval)
27745 +               au_fset_si(sbinfo, NO_DREVAL);
27746 +       else
27747 +               au_fclr_si(sbinfo, NO_DREVAL);
27748 +
27749 +       if (fhsm >= 2) {
27750 +               au_fset_si(sbinfo, FHSM);
27751 +               for (bindex = bbot; bindex >= 0; bindex--) {
27752 +                       br = au_sbr(sb, bindex);
27753 +                       if (au_br_fhsm(br->br_perm)) {
27754 +                               au_fhsm_set_bottom(sb, bindex);
27755 +                               break;
27756 +                       }
27757 +               }
27758 +       } else {
27759 +               au_fclr_si(sbinfo, FHSM);
27760 +               au_fhsm_set_bottom(sb, -1);
27761 +       }
27762 +
27763 +       return err;
27764 +}
27765 +
27766 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27767 +{
27768 +       int err;
27769 +       unsigned int tmp;
27770 +       aufs_bindex_t bindex, bbot;
27771 +       struct au_opt *opt;
27772 +       struct au_opt_xino *opt_xino, xino;
27773 +       struct au_sbinfo *sbinfo;
27774 +       struct au_branch *br;
27775 +       struct inode *dir;
27776 +
27777 +       SiMustWriteLock(sb);
27778 +
27779 +       err = 0;
27780 +       opt_xino = NULL;
27781 +       opt = opts->opt;
27782 +       while (err >= 0 && opt->type != Opt_tail)
27783 +               err = au_opt_simple(sb, opt++, opts);
27784 +       if (err > 0)
27785 +               err = 0;
27786 +       else if (unlikely(err < 0))
27787 +               goto out;
27788 +
27789 +       /* disable xino and udba temporary */
27790 +       sbinfo = au_sbi(sb);
27791 +       tmp = sbinfo->si_mntflags;
27792 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27793 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27794 +
27795 +       opt = opts->opt;
27796 +       while (err >= 0 && opt->type != Opt_tail)
27797 +               err = au_opt_br(sb, opt++, opts);
27798 +       if (err > 0)
27799 +               err = 0;
27800 +       else if (unlikely(err < 0))
27801 +               goto out;
27802 +
27803 +       bbot = au_sbbot(sb);
27804 +       if (unlikely(bbot < 0)) {
27805 +               err = -EINVAL;
27806 +               pr_err("no branches\n");
27807 +               goto out;
27808 +       }
27809 +
27810 +       if (au_opt_test(tmp, XINO))
27811 +               au_opt_set(sbinfo->si_mntflags, XINO);
27812 +       opt = opts->opt;
27813 +       while (!err && opt->type != Opt_tail)
27814 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27815 +       if (unlikely(err))
27816 +               goto out;
27817 +
27818 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27819 +       if (unlikely(err))
27820 +               goto out;
27821 +
27822 +       /* restore xino */
27823 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27824 +               xino.file = au_xino_def(sb);
27825 +               err = PTR_ERR(xino.file);
27826 +               if (IS_ERR(xino.file))
27827 +                       goto out;
27828 +
27829 +               err = au_xino_set(sb, &xino, /*remount*/0);
27830 +               fput(xino.file);
27831 +               if (unlikely(err))
27832 +                       goto out;
27833 +       }
27834 +
27835 +       /* restore udba */
27836 +       tmp &= AuOptMask_UDBA;
27837 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27838 +       sbinfo->si_mntflags |= tmp;
27839 +       bbot = au_sbbot(sb);
27840 +       for (bindex = 0; bindex <= bbot; bindex++) {
27841 +               br = au_sbr(sb, bindex);
27842 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27843 +               if (unlikely(err))
27844 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27845 +                               bindex, err);
27846 +               /* go on even if err */
27847 +       }
27848 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27849 +               dir = d_inode(sb->s_root);
27850 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27851 +       }
27852 +
27853 +out:
27854 +       return err;
27855 +}
27856 +
27857 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27858 +{
27859 +       int err, rerr;
27860 +       unsigned char no_dreval;
27861 +       struct inode *dir;
27862 +       struct au_opt_xino *opt_xino;
27863 +       struct au_opt *opt;
27864 +       struct au_sbinfo *sbinfo;
27865 +
27866 +       SiMustWriteLock(sb);
27867 +
27868 +       err = au_dr_opt_flush(sb);
27869 +       if (unlikely(err))
27870 +               goto out;
27871 +       au_fset_opts(opts->flags, DR_FLUSHED);
27872 +
27873 +       dir = d_inode(sb->s_root);
27874 +       sbinfo = au_sbi(sb);
27875 +       opt_xino = NULL;
27876 +       opt = opts->opt;
27877 +       while (err >= 0 && opt->type != Opt_tail) {
27878 +               err = au_opt_simple(sb, opt, opts);
27879 +               if (!err)
27880 +                       err = au_opt_br(sb, opt, opts);
27881 +               if (!err)
27882 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27883 +               opt++;
27884 +       }
27885 +       if (err > 0)
27886 +               err = 0;
27887 +       AuTraceErr(err);
27888 +       /* go on even err */
27889 +
27890 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27891 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27892 +       if (unlikely(rerr && !err))
27893 +               err = rerr;
27894 +
27895 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27896 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27897 +
27898 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27899 +               rerr = au_xib_trunc(sb);
27900 +               if (unlikely(rerr && !err))
27901 +                       err = rerr;
27902 +       }
27903 +
27904 +       /* will be handled by the caller */
27905 +       if (!au_ftest_opts(opts->flags, REFRESH)
27906 +           && (opts->given_udba
27907 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27908 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27909 +                   ))
27910 +               au_fset_opts(opts->flags, REFRESH);
27911 +
27912 +       AuDbg("status 0x%x\n", opts->flags);
27913 +
27914 +out:
27915 +       return err;
27916 +}
27917 +
27918 +/* ---------------------------------------------------------------------- */
27919 +
27920 +unsigned int au_opt_udba(struct super_block *sb)
27921 +{
27922 +       return au_mntflags(sb) & AuOptMask_UDBA;
27923 +}
27924 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27925 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27926 +++ linux/fs/aufs/opts.h        2022-11-05 23:02:18.969222617 +0100
27927 @@ -0,0 +1,263 @@
27928 +/* SPDX-License-Identifier: GPL-2.0 */
27929 +/*
27930 + * Copyright (C) 2005-2022 Junjiro R. Okajima
27931 + *
27932 + * This program is free software; you can redistribute it and/or modify
27933 + * it under the terms of the GNU General Public License as published by
27934 + * the Free Software Foundation; either version 2 of the License, or
27935 + * (at your option) any later version.
27936 + *
27937 + * This program is distributed in the hope that it will be useful,
27938 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27939 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27940 + * GNU General Public License for more details.
27941 + *
27942 + * You should have received a copy of the GNU General Public License
27943 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27944 + */
27945 +
27946 +/*
27947 + * mount options/flags
27948 + */
27949 +
27950 +#ifndef __AUFS_OPTS_H__
27951 +#define __AUFS_OPTS_H__
27952 +
27953 +#ifdef __KERNEL__
27954 +
27955 +#include <linux/fs_parser.h>
27956 +#include <linux/namei.h>
27957 +#include <linux/path.h>
27958 +
27959 +enum {
27960 +       Opt_br,
27961 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27962 +       Opt_idel, Opt_imod,
27963 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27964 +       Opt_xino, Opt_noxino,
27965 +       Opt_trunc_xino, Opt_trunc_xino_v,
27966 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27967 +       Opt_trunc_xib,
27968 +       Opt_shwh,
27969 +       Opt_plink, Opt_list_plink,
27970 +       Opt_udba,
27971 +       Opt_dio,
27972 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27973 +       Opt_warn_perm,
27974 +       Opt_wbr_copyup, Opt_wbr_create,
27975 +       Opt_fhsm_sec,
27976 +       Opt_verbose, Opt_noverbose,
27977 +       Opt_sum, Opt_wsum,
27978 +       Opt_dirperm1,
27979 +       Opt_dirren,
27980 +       Opt_acl,
27981 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27982 +};
27983 +
27984 +/* ---------------------------------------------------------------------- */
27985 +
27986 +/* mount flags */
27987 +#define AuOpt_XINO             1               /* external inode number bitmap
27988 +                                                  and translation table */
27989 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27990 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27991 +#define AuOpt_UDBA_REVAL       (1 << 3)
27992 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27993 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27994 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27995 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27996 +                                                  bits */
27997 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27998 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27999 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
28000 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
28001 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
28002 +#define AuOpt_DIO              (1 << 14)       /* direct io */
28003 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
28004 +
28005 +#ifndef CONFIG_AUFS_HNOTIFY
28006 +#undef AuOpt_UDBA_HNOTIFY
28007 +#define AuOpt_UDBA_HNOTIFY     0
28008 +#endif
28009 +#ifndef CONFIG_AUFS_DIRREN
28010 +#undef AuOpt_DIRREN
28011 +#define AuOpt_DIRREN           0
28012 +#endif
28013 +#ifndef CONFIG_AUFS_SHWH
28014 +#undef AuOpt_SHWH
28015 +#define AuOpt_SHWH             0
28016 +#endif
28017 +
28018 +#define AuOpt_Def      (AuOpt_XINO \
28019 +                        | AuOpt_UDBA_REVAL \
28020 +                        | AuOpt_PLINK \
28021 +                        /* | AuOpt_DIRPERM1 */ \
28022 +                        | AuOpt_WARN_PERM)
28023 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
28024 +                        | AuOpt_UDBA_REVAL \
28025 +                        | AuOpt_UDBA_HNOTIFY)
28026 +
28027 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
28028 +
28029 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
28030 +#define au_opt_set(flags, name) do { \
28031 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
28032 +       ((flags) |= AuOpt_##name); \
28033 +} while (0)
28034 +#define au_opt_set_udba(flags, name) do { \
28035 +       (flags) &= ~AuOptMask_UDBA; \
28036 +       ((flags) |= AuOpt_##name); \
28037 +} while (0)
28038 +#define au_opt_clr(flags, name) do { \
28039 +       ((flags) &= ~AuOpt_##name); \
28040 +} while (0)
28041 +
28042 +static inline unsigned int au_opts_plink(unsigned int mntflags)
28043 +{
28044 +#ifdef CONFIG_PROC_FS
28045 +       return mntflags;
28046 +#else
28047 +       return mntflags & ~AuOpt_PLINK;
28048 +#endif
28049 +}
28050 +
28051 +/* ---------------------------------------------------------------------- */
28052 +
28053 +/* policies to select one among multiple writable branches */
28054 +enum {
28055 +       AuWbrCreate_TDP,        /* top down parent */
28056 +       AuWbrCreate_RR,         /* round robin */
28057 +       AuWbrCreate_MFS,        /* most free space */
28058 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28059 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28060 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28061 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28062 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28063 +       AuWbrCreate_PMFS,       /* parent and mfs */
28064 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28065 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28066 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28067 +
28068 +       AuWbrCreate_Def = AuWbrCreate_TDP
28069 +};
28070 +
28071 +enum {
28072 +       AuWbrCopyup_TDP,        /* top down parent */
28073 +       AuWbrCopyup_BUP,        /* bottom up parent */
28074 +       AuWbrCopyup_BU,         /* bottom up */
28075 +
28076 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28077 +};
28078 +
28079 +/* ---------------------------------------------------------------------- */
28080 +
28081 +struct file;
28082 +
28083 +struct au_opt_add {
28084 +       aufs_bindex_t   bindex;
28085 +       char            *pathname;
28086 +       int             perm;
28087 +       struct path     path;
28088 +};
28089 +
28090 +struct au_opt_del {
28091 +       char            *pathname;
28092 +       struct path     h_path;
28093 +};
28094 +
28095 +struct au_opt_mod {
28096 +       char            *path;
28097 +       int             perm;
28098 +       struct dentry   *h_root;
28099 +};
28100 +
28101 +struct au_opt_xino {
28102 +       char            *path;
28103 +       struct file     *file;
28104 +};
28105 +
28106 +struct au_opt_xino_itrunc {
28107 +       aufs_bindex_t   bindex;
28108 +};
28109 +
28110 +struct au_opt_wbr_create {
28111 +       int                     wbr_create;
28112 +       int                     mfs_second;
28113 +       unsigned long long      mfsrr_watermark;
28114 +};
28115 +
28116 +struct au_opt {
28117 +       int type;
28118 +       union {
28119 +               struct au_opt_xino      xino;
28120 +               struct au_opt_xino_itrunc xino_itrunc;
28121 +               struct au_opt_add       add;
28122 +               struct au_opt_del       del;
28123 +               struct au_opt_mod       mod;
28124 +               int                     dirwh;
28125 +               int                     rdcache;
28126 +               unsigned int            rdblk;
28127 +               unsigned int            rdhash;
28128 +               int                     udba;
28129 +               struct au_opt_wbr_create wbr_create;
28130 +               int                     wbr_copyup;
28131 +               unsigned int            fhsm_second;
28132 +               bool                    tf; /* generic flag, true or false */
28133 +       };
28134 +};
28135 +
28136 +/* opts flags */
28137 +#define AuOpts_REMOUNT         1
28138 +#define AuOpts_REFRESH         (1 << 1)
28139 +#define AuOpts_TRUNC_XIB       (1 << 2)
28140 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28141 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28142 +#define AuOpts_DR_FLUSHED      (1 << 5)
28143 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28144 +#define au_fset_opts(flags, name) \
28145 +       do { (flags) |= AuOpts_##name; } while (0)
28146 +#define au_fclr_opts(flags, name) \
28147 +       do { (flags) &= ~AuOpts_##name; } while (0)
28148 +
28149 +#ifndef CONFIG_AUFS_DIRREN
28150 +#undef AuOpts_DR_FLUSHED
28151 +#define AuOpts_DR_FLUSHED      0
28152 +#endif
28153 +
28154 +struct au_opts {
28155 +       struct au_opt   *opt;
28156 +       int             max_opt;
28157 +
28158 +       unsigned int    given_udba;
28159 +       unsigned int    flags;
28160 +       unsigned long   sb_flags;
28161 +};
28162 +
28163 +/* ---------------------------------------------------------------------- */
28164 +
28165 +/* opts.c */
28166 +int au_br_perm_val(char *perm);
28167 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28168 +int au_udba_val(char *str);
28169 +const char *au_optstr_udba(int udba);
28170 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28171 +const char *au_optstr_wbr_create(int wbr_create);
28172 +int au_wbr_copyup_val(char *str);
28173 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28174 +
28175 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28176 +              aufs_bindex_t bindex);
28177 +struct super_block;
28178 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28179 +                  unsigned int pending);
28180 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28181 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28182 +
28183 +unsigned int au_opt_udba(struct super_block *sb);
28184 +
28185 +/* fsctx.c */
28186 +int aufs_fsctx_init(struct fs_context *fc);
28187 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28188 +
28189 +#endif /* __KERNEL__ */
28190 +#endif /* __AUFS_OPTS_H__ */
28191 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28192 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28193 +++ linux/fs/aufs/plink.c       2022-11-05 23:02:18.969222617 +0100
28194 @@ -0,0 +1,516 @@
28195 +// SPDX-License-Identifier: GPL-2.0
28196 +/*
28197 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28198 + *
28199 + * This program is free software; you can redistribute it and/or modify
28200 + * it under the terms of the GNU General Public License as published by
28201 + * the Free Software Foundation; either version 2 of the License, or
28202 + * (at your option) any later version.
28203 + *
28204 + * This program is distributed in the hope that it will be useful,
28205 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28206 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28207 + * GNU General Public License for more details.
28208 + *
28209 + * You should have received a copy of the GNU General Public License
28210 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28211 + */
28212 +
28213 +/*
28214 + * pseudo-link
28215 + */
28216 +
28217 +#include "aufs.h"
28218 +
28219 +/*
28220 + * the pseudo-link maintenance mode.
28221 + * during a user process maintains the pseudo-links,
28222 + * prohibit adding a new plink and branch manipulation.
28223 + *
28224 + * Flags
28225 + * NOPLM:
28226 + *     For entry functions which will handle plink, and i_mutex is already held
28227 + *     in VFS.
28228 + *     They cannot wait and should return an error at once.
28229 + *     Callers has to check the error.
28230 + * NOPLMW:
28231 + *     For entry functions which will handle plink, but i_mutex is not held
28232 + *     in VFS.
28233 + *     They can wait the plink maintenance mode to finish.
28234 + *
28235 + * They behave like F_SETLK and F_SETLKW.
28236 + * If the caller never handle plink, then both flags are unnecessary.
28237 + */
28238 +
28239 +int au_plink_maint(struct super_block *sb, int flags)
28240 +{
28241 +       int err;
28242 +       pid_t pid, ppid;
28243 +       struct task_struct *parent, *prev;
28244 +       struct au_sbinfo *sbi;
28245 +
28246 +       SiMustAnyLock(sb);
28247 +
28248 +       err = 0;
28249 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28250 +               goto out;
28251 +
28252 +       sbi = au_sbi(sb);
28253 +       pid = sbi->si_plink_maint_pid;
28254 +       if (!pid || pid == current->pid)
28255 +               goto out;
28256 +
28257 +       /* todo: it highly depends upon /sbin/mount.aufs */
28258 +       prev = NULL;
28259 +       parent = current;
28260 +       ppid = 0;
28261 +       rcu_read_lock();
28262 +       while (1) {
28263 +               parent = rcu_dereference(parent->real_parent);
28264 +               if (parent == prev)
28265 +                       break;
28266 +               ppid = task_pid_vnr(parent);
28267 +               if (pid == ppid) {
28268 +                       rcu_read_unlock();
28269 +                       goto out;
28270 +               }
28271 +               prev = parent;
28272 +       }
28273 +       rcu_read_unlock();
28274 +
28275 +       if (au_ftest_lock(flags, NOPLMW)) {
28276 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28277 +               /* AuDebugOn(!lockdep_depth(current)); */
28278 +               while (sbi->si_plink_maint_pid) {
28279 +                       si_read_unlock(sb);
28280 +                       /* gave up wake_up_bit() */
28281 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28282 +
28283 +                       if (au_ftest_lock(flags, FLUSH))
28284 +                               au_nwt_flush(&sbi->si_nowait);
28285 +                       si_noflush_read_lock(sb);
28286 +               }
28287 +       } else if (au_ftest_lock(flags, NOPLM)) {
28288 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28289 +               err = -EAGAIN;
28290 +       }
28291 +
28292 +out:
28293 +       return err;
28294 +}
28295 +
28296 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28297 +{
28298 +       spin_lock(&sbinfo->si_plink_maint_lock);
28299 +       sbinfo->si_plink_maint_pid = 0;
28300 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28301 +       wake_up_all(&sbinfo->si_plink_wq);
28302 +}
28303 +
28304 +int au_plink_maint_enter(struct super_block *sb)
28305 +{
28306 +       int err;
28307 +       struct au_sbinfo *sbinfo;
28308 +
28309 +       err = 0;
28310 +       sbinfo = au_sbi(sb);
28311 +       /* make sure i am the only one in this fs */
28312 +       si_write_lock(sb, AuLock_FLUSH);
28313 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28314 +               spin_lock(&sbinfo->si_plink_maint_lock);
28315 +               if (!sbinfo->si_plink_maint_pid)
28316 +                       sbinfo->si_plink_maint_pid = current->pid;
28317 +               else
28318 +                       err = -EBUSY;
28319 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28320 +       }
28321 +       si_write_unlock(sb);
28322 +
28323 +       return err;
28324 +}
28325 +
28326 +/* ---------------------------------------------------------------------- */
28327 +
28328 +#ifdef CONFIG_AUFS_DEBUG
28329 +void au_plink_list(struct super_block *sb)
28330 +{
28331 +       int i;
28332 +       struct au_sbinfo *sbinfo;
28333 +       struct hlist_bl_head *hbl;
28334 +       struct hlist_bl_node *pos;
28335 +       struct au_icntnr *icntnr;
28336 +
28337 +       SiMustAnyLock(sb);
28338 +
28339 +       sbinfo = au_sbi(sb);
28340 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28341 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28342 +
28343 +       for (i = 0; i < AuPlink_NHASH; i++) {
28344 +               hbl = sbinfo->si_plink + i;
28345 +               hlist_bl_lock(hbl);
28346 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28347 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28348 +               hlist_bl_unlock(hbl);
28349 +       }
28350 +}
28351 +#endif
28352 +
28353 +/* is the inode pseudo-linked? */
28354 +int au_plink_test(struct inode *inode)
28355 +{
28356 +       int found, i;
28357 +       struct au_sbinfo *sbinfo;
28358 +       struct hlist_bl_head *hbl;
28359 +       struct hlist_bl_node *pos;
28360 +       struct au_icntnr *icntnr;
28361 +
28362 +       sbinfo = au_sbi(inode->i_sb);
28363 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28364 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28365 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28366 +
28367 +       found = 0;
28368 +       i = au_plink_hash(inode->i_ino);
28369 +       hbl =  sbinfo->si_plink + i;
28370 +       hlist_bl_lock(hbl);
28371 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28372 +               if (&icntnr->vfs_inode == inode) {
28373 +                       found = 1;
28374 +                       break;
28375 +               }
28376 +       hlist_bl_unlock(hbl);
28377 +       return found;
28378 +}
28379 +
28380 +/* ---------------------------------------------------------------------- */
28381 +
28382 +/*
28383 + * generate a name for plink.
28384 + * the file will be stored under AUFS_WH_PLINKDIR.
28385 + */
28386 +/* 20 is max digits length of ulong 64 */
28387 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28388 +
28389 +static int plink_name(char *name, int len, struct inode *inode,
28390 +                     aufs_bindex_t bindex)
28391 +{
28392 +       int rlen;
28393 +       struct inode *h_inode;
28394 +
28395 +       h_inode = au_h_iptr(inode, bindex);
28396 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28397 +       return rlen;
28398 +}
28399 +
28400 +struct au_do_plink_lkup_args {
28401 +       struct dentry **errp;
28402 +       struct qstr *tgtname;
28403 +       struct path *h_ppath;
28404 +};
28405 +
28406 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28407 +                                      struct path *h_ppath)
28408 +{
28409 +       struct dentry *h_dentry;
28410 +       struct inode *h_inode;
28411 +
28412 +       h_inode = d_inode(h_ppath->dentry);
28413 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28414 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28415 +       inode_unlock_shared(h_inode);
28416 +
28417 +       return h_dentry;
28418 +}
28419 +
28420 +static void au_call_do_plink_lkup(void *args)
28421 +{
28422 +       struct au_do_plink_lkup_args *a = args;
28423 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28424 +}
28425 +
28426 +/* lookup the plink-ed @inode under the branch at @bindex */
28427 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28428 +{
28429 +       struct dentry *h_dentry;
28430 +       struct au_branch *br;
28431 +       struct path h_ppath;
28432 +       int wkq_err;
28433 +       char a[PLINK_NAME_LEN];
28434 +       struct qstr tgtname = QSTR_INIT(a, 0);
28435 +
28436 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28437 +
28438 +       br = au_sbr(inode->i_sb, bindex);
28439 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28440 +       h_ppath.mnt = au_br_mnt(br);
28441 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28442 +
28443 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28444 +               struct au_do_plink_lkup_args args = {
28445 +                       .errp           = &h_dentry,
28446 +                       .tgtname        = &tgtname,
28447 +                       .h_ppath        = &h_ppath
28448 +               };
28449 +
28450 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28451 +               if (unlikely(wkq_err))
28452 +                       h_dentry = ERR_PTR(wkq_err);
28453 +       } else
28454 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28455 +
28456 +       return h_dentry;
28457 +}
28458 +
28459 +/* create a pseudo-link */
28460 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28461 +                     struct dentry *h_dentry)
28462 +{
28463 +       int err;
28464 +       struct path h_path;
28465 +       struct inode *h_dir, *delegated;
28466 +
28467 +       h_dir = d_inode(h_ppath->dentry);
28468 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28469 +       h_path.mnt = h_ppath->mnt;
28470 +again:
28471 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28472 +       err = PTR_ERR(h_path.dentry);
28473 +       if (IS_ERR(h_path.dentry))
28474 +               goto out;
28475 +
28476 +       err = 0;
28477 +       /* wh.plink dir is not monitored */
28478 +       /* todo: is it really safe? */
28479 +       if (d_is_positive(h_path.dentry)
28480 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28481 +               delegated = NULL;
28482 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28483 +               if (unlikely(err == -EWOULDBLOCK)) {
28484 +                       pr_warn("cannot retry for NFSv4 delegation"
28485 +                               " for an internal unlink\n");
28486 +                       iput(delegated);
28487 +               }
28488 +               dput(h_path.dentry);
28489 +               h_path.dentry = NULL;
28490 +               if (!err)
28491 +                       goto again;
28492 +       }
28493 +       if (!err && d_is_negative(h_path.dentry)) {
28494 +               delegated = NULL;
28495 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28496 +               if (unlikely(err == -EWOULDBLOCK)) {
28497 +                       pr_warn("cannot retry for NFSv4 delegation"
28498 +                               " for an internal link\n");
28499 +                       iput(delegated);
28500 +               }
28501 +       }
28502 +       dput(h_path.dentry);
28503 +
28504 +out:
28505 +       inode_unlock(h_dir);
28506 +       return err;
28507 +}
28508 +
28509 +struct do_whplink_args {
28510 +       int *errp;
28511 +       struct qstr *tgt;
28512 +       struct path *h_ppath;
28513 +       struct dentry *h_dentry;
28514 +};
28515 +
28516 +static void call_do_whplink(void *args)
28517 +{
28518 +       struct do_whplink_args *a = args;
28519 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28520 +}
28521 +
28522 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28523 +                  aufs_bindex_t bindex)
28524 +{
28525 +       int err, wkq_err;
28526 +       struct au_branch *br;
28527 +       struct au_wbr *wbr;
28528 +       struct path h_ppath;
28529 +       char a[PLINK_NAME_LEN];
28530 +       struct qstr tgtname = QSTR_INIT(a, 0);
28531 +
28532 +       br = au_sbr(inode->i_sb, bindex);
28533 +       wbr = br->br_wbr;
28534 +       h_ppath.dentry = wbr->wbr_plink;
28535 +       h_ppath.mnt = au_br_mnt(br);
28536 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28537 +
28538 +       /* always superio. */
28539 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28540 +               struct do_whplink_args args = {
28541 +                       .errp           = &err,
28542 +                       .tgt            = &tgtname,
28543 +                       .h_ppath        = &h_ppath,
28544 +                       .h_dentry       = h_dentry
28545 +               };
28546 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28547 +               if (unlikely(wkq_err))
28548 +                       err = wkq_err;
28549 +       } else
28550 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28551 +
28552 +       return err;
28553 +}
28554 +
28555 +/*
28556 + * create a new pseudo-link for @h_dentry on @bindex.
28557 + * the linked inode is held in aufs @inode.
28558 + */
28559 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28560 +                    struct dentry *h_dentry)
28561 +{
28562 +       struct super_block *sb;
28563 +       struct au_sbinfo *sbinfo;
28564 +       struct hlist_bl_head *hbl;
28565 +       struct hlist_bl_node *pos;
28566 +       struct au_icntnr *icntnr;
28567 +       int found, err, cnt, i;
28568 +
28569 +       sb = inode->i_sb;
28570 +       sbinfo = au_sbi(sb);
28571 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28572 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28573 +
28574 +       found = au_plink_test(inode);
28575 +       if (found)
28576 +               return;
28577 +
28578 +       i = au_plink_hash(inode->i_ino);
28579 +       hbl = sbinfo->si_plink + i;
28580 +       au_igrab(inode);
28581 +
28582 +       hlist_bl_lock(hbl);
28583 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28584 +               if (&icntnr->vfs_inode == inode) {
28585 +                       found = 1;
28586 +                       break;
28587 +               }
28588 +       }
28589 +       if (!found) {
28590 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28591 +               hlist_bl_add_head(&icntnr->plink, hbl);
28592 +       }
28593 +       hlist_bl_unlock(hbl);
28594 +       if (!found) {
28595 +               cnt = au_hbl_count(hbl);
28596 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28597 +               if (cnt > AUFS_PLINK_WARN)
28598 +                       AuWarn1(msg ", %d\n", cnt);
28599 +#undef msg
28600 +               err = whplink(h_dentry, inode, bindex);
28601 +               if (unlikely(err)) {
28602 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28603 +                       au_hbl_del(&icntnr->plink, hbl);
28604 +                       iput(&icntnr->vfs_inode);
28605 +               }
28606 +       } else
28607 +               iput(&icntnr->vfs_inode);
28608 +}
28609 +
28610 +/* free all plinks */
28611 +void au_plink_put(struct super_block *sb, int verbose)
28612 +{
28613 +       int i, warned;
28614 +       struct au_sbinfo *sbinfo;
28615 +       struct hlist_bl_head *hbl;
28616 +       struct hlist_bl_node *pos, *tmp;
28617 +       struct au_icntnr *icntnr;
28618 +
28619 +       SiMustWriteLock(sb);
28620 +
28621 +       sbinfo = au_sbi(sb);
28622 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28623 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28624 +
28625 +       /* no spin_lock since sbinfo is write-locked */
28626 +       warned = 0;
28627 +       for (i = 0; i < AuPlink_NHASH; i++) {
28628 +               hbl = sbinfo->si_plink + i;
28629 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28630 +                       pr_warn("pseudo-link is not flushed");
28631 +                       warned = 1;
28632 +               }
28633 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28634 +                       iput(&icntnr->vfs_inode);
28635 +               INIT_HLIST_BL_HEAD(hbl);
28636 +       }
28637 +}
28638 +
28639 +void au_plink_clean(struct super_block *sb, int verbose)
28640 +{
28641 +       struct dentry *root;
28642 +
28643 +       root = sb->s_root;
28644 +       aufs_write_lock(root);
28645 +       if (au_opt_test(au_mntflags(sb), PLINK))
28646 +               au_plink_put(sb, verbose);
28647 +       aufs_write_unlock(root);
28648 +}
28649 +
28650 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28651 +{
28652 +       int do_put;
28653 +       aufs_bindex_t btop, bbot, bindex;
28654 +
28655 +       do_put = 0;
28656 +       btop = au_ibtop(inode);
28657 +       bbot = au_ibbot(inode);
28658 +       if (btop >= 0) {
28659 +               for (bindex = btop; bindex <= bbot; bindex++) {
28660 +                       if (!au_h_iptr(inode, bindex)
28661 +                           || au_ii_br_id(inode, bindex) != br_id)
28662 +                               continue;
28663 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28664 +                       do_put = 1;
28665 +                       break;
28666 +               }
28667 +               if (do_put)
28668 +                       for (bindex = btop; bindex <= bbot; bindex++)
28669 +                               if (au_h_iptr(inode, bindex)) {
28670 +                                       do_put = 0;
28671 +                                       break;
28672 +                               }
28673 +       } else
28674 +               do_put = 1;
28675 +
28676 +       return do_put;
28677 +}
28678 +
28679 +/* free the plinks on a branch specified by @br_id */
28680 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28681 +{
28682 +       struct au_sbinfo *sbinfo;
28683 +       struct hlist_bl_head *hbl;
28684 +       struct hlist_bl_node *pos, *tmp;
28685 +       struct au_icntnr *icntnr;
28686 +       struct inode *inode;
28687 +       int i, do_put;
28688 +
28689 +       SiMustWriteLock(sb);
28690 +
28691 +       sbinfo = au_sbi(sb);
28692 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28693 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28694 +
28695 +       /* no bit_lock since sbinfo is write-locked */
28696 +       for (i = 0; i < AuPlink_NHASH; i++) {
28697 +               hbl = sbinfo->si_plink + i;
28698 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28699 +                       inode = au_igrab(&icntnr->vfs_inode);
28700 +                       ii_write_lock_child(inode);
28701 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28702 +                       if (do_put) {
28703 +                               hlist_bl_del(&icntnr->plink);
28704 +                               iput(inode);
28705 +                       }
28706 +                       ii_write_unlock(inode);
28707 +                       iput(inode);
28708 +               }
28709 +       }
28710 +}
28711 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28712 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28713 +++ linux/fs/aufs/poll.c        2022-11-05 23:02:18.969222617 +0100
28714 @@ -0,0 +1,51 @@
28715 +// SPDX-License-Identifier: GPL-2.0
28716 +/*
28717 + * Copyright (C) 2005-2022 Junjiro R. Okajima
28718 + *
28719 + * This program is free software; you can redistribute it and/or modify
28720 + * it under the terms of the GNU General Public License as published by
28721 + * the Free Software Foundation; either version 2 of the License, or
28722 + * (at your option) any later version.
28723 + *
28724 + * This program is distributed in the hope that it will be useful,
28725 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28726 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28727 + * GNU General Public License for more details.
28728 + *
28729 + * You should have received a copy of the GNU General Public License
28730 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28731 + */
28732 +
28733 +/*
28734 + * poll operation
28735 + * There is only one filesystem which implements ->poll operation, currently.
28736 + */
28737 +
28738 +#include "aufs.h"
28739 +
28740 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28741 +{
28742 +       __poll_t mask;
28743 +       struct file *h_file;
28744 +       struct super_block *sb;
28745 +
28746 +       /* We should pretend an error happened. */
28747 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28748 +       sb = file->f_path.dentry->d_sb;
28749 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28750 +
28751 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28752 +       if (IS_ERR(h_file)) {
28753 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28754 +               goto out;
28755 +       }
28756 +
28757 +       mask = vfs_poll(h_file, pt);
28758 +       fput(h_file); /* instead of au_read_post() */
28759 +
28760 +out:
28761 +       si_read_unlock(sb);
28762 +       if (mask & EPOLLERR)
28763 +               AuDbg("mask 0x%x\n", mask);
28764 +       return mask;
28765 +}
28766 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28767 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28768 +++ linux/fs/aufs/posix_acl.c   2022-11-05 23:02:18.969222617 +0100
28769 @@ -0,0 +1,111 @@
28770 +// SPDX-License-Identifier: GPL-2.0
28771 +/*
28772 + * Copyright (C) 2014-2022 Junjiro R. Okajima
28773 + *
28774 + * This program is free software; you can redistribute it and/or modify
28775 + * it under the terms of the GNU General Public License as published by
28776 + * the Free Software Foundation; either version 2 of the License, or
28777 + * (at your option) any later version.
28778 + *
28779 + * This program is distributed in the hope that it will be useful,
28780 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28781 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28782 + * GNU General Public License for more details.
28783 + *
28784 + * You should have received a copy of the GNU General Public License
28785 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28786 + */
28787 +
28788 +/*
28789 + * posix acl operations
28790 + */
28791 +
28792 +#include <linux/fs.h>
28793 +#include "aufs.h"
28794 +
28795 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28796 +{
28797 +       struct posix_acl *acl;
28798 +       int err;
28799 +       aufs_bindex_t bindex;
28800 +       struct inode *h_inode;
28801 +       struct super_block *sb;
28802 +
28803 +       acl = ERR_PTR(-ECHILD);
28804 +       if (rcu)
28805 +               goto out;
28806 +
28807 +       acl = NULL;
28808 +       sb = inode->i_sb;
28809 +       si_read_lock(sb, AuLock_FLUSH);
28810 +       ii_read_lock_child(inode);
28811 +       if (!(sb->s_flags & SB_POSIXACL))
28812 +               goto unlock;
28813 +
28814 +       bindex = au_ibtop(inode);
28815 +       h_inode = au_h_iptr(inode, bindex);
28816 +       if (unlikely(!h_inode
28817 +                    || ((h_inode->i_mode & S_IFMT)
28818 +                        != (inode->i_mode & S_IFMT)))) {
28819 +               err = au_busy_or_stale();
28820 +               acl = ERR_PTR(err);
28821 +               goto unlock;
28822 +       }
28823 +
28824 +       /* always topmost only */
28825 +       acl = get_acl(h_inode, type);
28826 +       if (IS_ERR(acl))
28827 +               forget_cached_acl(inode, type);
28828 +       else
28829 +               set_cached_acl(inode, type, acl);
28830 +
28831 +unlock:
28832 +       ii_read_unlock(inode);
28833 +       si_read_unlock(sb);
28834 +
28835 +out:
28836 +       AuTraceErrPtr(acl);
28837 +       return acl;
28838 +}
28839 +
28840 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28841 +                struct posix_acl *acl, int type)
28842 +{
28843 +       int err;
28844 +       ssize_t ssz;
28845 +       struct dentry *dentry;
28846 +       struct au_sxattr arg = {
28847 +               .type = AU_ACL_SET,
28848 +               .u.acl_set = {
28849 +                       .acl    = acl,
28850 +                       .type   = type
28851 +               },
28852 +       };
28853 +
28854 +       IMustLock(inode);
28855 +
28856 +       if (inode->i_ino == AUFS_ROOT_INO)
28857 +               dentry = dget(inode->i_sb->s_root);
28858 +       else {
28859 +               dentry = d_find_alias(inode);
28860 +               if (!dentry)
28861 +                       dentry = d_find_any_alias(inode);
28862 +               if (!dentry) {
28863 +                       pr_warn("cannot handle this inode, "
28864 +                               "please report to aufs-users ML\n");
28865 +                       err = -ENOENT;
28866 +                       goto out;
28867 +               }
28868 +       }
28869 +
28870 +       ssz = au_sxattr(dentry, inode, &arg);
28871 +       /* forget even it if succeeds since the branch might set differently */
28872 +       forget_cached_acl(inode, type);
28873 +       dput(dentry);
28874 +       err = ssz;
28875 +       if (ssz >= 0)
28876 +               err = 0;
28877 +
28878 +out:
28879 +       return err;
28880 +}
28881 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28882 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28883 +++ linux/fs/aufs/procfs.c      2022-11-05 23:02:18.969222617 +0100
28884 @@ -0,0 +1,170 @@
28885 +// SPDX-License-Identifier: GPL-2.0
28886 +/*
28887 + * Copyright (C) 2010-2022 Junjiro R. Okajima
28888 + *
28889 + * This program is free software; you can redistribute it and/or modify
28890 + * it under the terms of the GNU General Public License as published by
28891 + * the Free Software Foundation; either version 2 of the License, or
28892 + * (at your option) any later version.
28893 + *
28894 + * This program is distributed in the hope that it will be useful,
28895 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28896 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28897 + * GNU General Public License for more details.
28898 + *
28899 + * You should have received a copy of the GNU General Public License
28900 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28901 + */
28902 +
28903 +/*
28904 + * procfs interfaces
28905 + */
28906 +
28907 +#include <linux/proc_fs.h>
28908 +#include "aufs.h"
28909 +
28910 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28911 +{
28912 +       struct au_sbinfo *sbinfo;
28913 +
28914 +       sbinfo = file->private_data;
28915 +       if (sbinfo) {
28916 +               au_plink_maint_leave(sbinfo);
28917 +               kobject_put(&sbinfo->si_kobj);
28918 +       }
28919 +
28920 +       return 0;
28921 +}
28922 +
28923 +static void au_procfs_plm_write_clean(struct file *file)
28924 +{
28925 +       struct au_sbinfo *sbinfo;
28926 +
28927 +       sbinfo = file->private_data;
28928 +       if (sbinfo)
28929 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28930 +}
28931 +
28932 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28933 +{
28934 +       int err;
28935 +       struct super_block *sb;
28936 +       struct au_sbinfo *sbinfo;
28937 +       struct hlist_bl_node *pos;
28938 +
28939 +       err = -EBUSY;
28940 +       if (unlikely(file->private_data))
28941 +               goto out;
28942 +
28943 +       sb = NULL;
28944 +       /* don't use au_sbilist_lock() here */
28945 +       hlist_bl_lock(&au_sbilist);
28946 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28947 +               if (id == sysaufs_si_id(sbinfo)) {
28948 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28949 +                               sb = sbinfo->si_sb;
28950 +                       break;
28951 +               }
28952 +       hlist_bl_unlock(&au_sbilist);
28953 +
28954 +       err = -EINVAL;
28955 +       if (unlikely(!sb))
28956 +               goto out;
28957 +
28958 +       err = au_plink_maint_enter(sb);
28959 +       if (!err)
28960 +               /* keep kobject_get() */
28961 +               file->private_data = sbinfo;
28962 +       else
28963 +               kobject_put(&sbinfo->si_kobj);
28964 +out:
28965 +       return err;
28966 +}
28967 +
28968 +/*
28969 + * Accept a valid "si=xxxx" only.
28970 + * Once it is accepted successfully, accept "clean" too.
28971 + */
28972 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28973 +                                  size_t count, loff_t *ppos)
28974 +{
28975 +       ssize_t err;
28976 +       unsigned long id;
28977 +       /* last newline is allowed */
28978 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28979 +
28980 +       err = -EACCES;
28981 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28982 +               goto out;
28983 +
28984 +       err = -EINVAL;
28985 +       if (unlikely(count > sizeof(buf)))
28986 +               goto out;
28987 +
28988 +       err = copy_from_user(buf, ubuf, count);
28989 +       if (unlikely(err)) {
28990 +               err = -EFAULT;
28991 +               goto out;
28992 +       }
28993 +       buf[count] = 0;
28994 +
28995 +       err = -EINVAL;
28996 +       if (!strcmp("clean", buf)) {
28997 +               au_procfs_plm_write_clean(file);
28998 +               goto out_success;
28999 +       } else if (unlikely(strncmp("si=", buf, 3)))
29000 +               goto out;
29001 +
29002 +       err = kstrtoul(buf + 3, 16, &id);
29003 +       if (unlikely(err))
29004 +               goto out;
29005 +
29006 +       err = au_procfs_plm_write_si(file, id);
29007 +       if (unlikely(err))
29008 +               goto out;
29009 +
29010 +out_success:
29011 +       err = count; /* success */
29012 +out:
29013 +       return err;
29014 +}
29015 +
29016 +static const struct proc_ops au_procfs_plm_op = {
29017 +       .proc_write     = au_procfs_plm_write,
29018 +       .proc_release   = au_procfs_plm_release
29019 +};
29020 +
29021 +/* ---------------------------------------------------------------------- */
29022 +
29023 +static struct proc_dir_entry *au_procfs_dir;
29024 +
29025 +void au_procfs_fin(void)
29026 +{
29027 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
29028 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29029 +}
29030 +
29031 +int __init au_procfs_init(void)
29032 +{
29033 +       int err;
29034 +       struct proc_dir_entry *entry;
29035 +
29036 +       err = -ENOMEM;
29037 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
29038 +       if (unlikely(!au_procfs_dir))
29039 +               goto out;
29040 +
29041 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
29042 +                           au_procfs_dir, &au_procfs_plm_op);
29043 +       if (unlikely(!entry))
29044 +               goto out_dir;
29045 +
29046 +       err = 0;
29047 +       goto out; /* success */
29048 +
29049 +
29050 +out_dir:
29051 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29052 +out:
29053 +       return err;
29054 +}
29055 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29056 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29057 +++ linux/fs/aufs/rdu.c 2022-12-17 09:21:34.799855195 +0100
29058 @@ -0,0 +1,384 @@
29059 +// SPDX-License-Identifier: GPL-2.0
29060 +/*
29061 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29062 + *
29063 + * This program is free software; you can redistribute it and/or modify
29064 + * it under the terms of the GNU General Public License as published by
29065 + * the Free Software Foundation; either version 2 of the License, or
29066 + * (at your option) any later version.
29067 + *
29068 + * This program is distributed in the hope that it will be useful,
29069 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29070 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29071 + * GNU General Public License for more details.
29072 + *
29073 + * You should have received a copy of the GNU General Public License
29074 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29075 + */
29076 +
29077 +/*
29078 + * readdir in userspace.
29079 + */
29080 +
29081 +#include <linux/compat.h>
29082 +#include <linux/fs_stack.h>
29083 +#include <linux/security.h>
29084 +#include "aufs.h"
29085 +
29086 +/* bits for struct aufs_rdu.flags */
29087 +#define        AuRdu_CALLED    1
29088 +#define        AuRdu_CONT      (1 << 1)
29089 +#define        AuRdu_FULL      (1 << 2)
29090 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29091 +#define au_fset_rdu(flags, name) \
29092 +       do { (flags) |= AuRdu_##name; } while (0)
29093 +#define au_fclr_rdu(flags, name) \
29094 +       do { (flags) &= ~AuRdu_##name; } while (0)
29095 +
29096 +struct au_rdu_arg {
29097 +       struct dir_context              ctx;
29098 +       struct aufs_rdu                 *rdu;
29099 +       union au_rdu_ent_ul             ent;
29100 +       unsigned long                   end;
29101 +
29102 +       struct super_block              *sb;
29103 +       int                             err;
29104 +};
29105 +
29106 +static bool au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29107 +                       loff_t offset, u64 h_ino, unsigned int d_type)
29108 +{
29109 +       int err, len;
29110 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29111 +       struct aufs_rdu *rdu = arg->rdu;
29112 +       struct au_rdu_ent ent;
29113 +
29114 +       err = 0;
29115 +       arg->err = 0;
29116 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29117 +       len = au_rdu_len(nlen);
29118 +       if (arg->ent.ul + len  < arg->end) {
29119 +               ent.ino = h_ino;
29120 +               ent.bindex = rdu->cookie.bindex;
29121 +               ent.type = d_type;
29122 +               ent.nlen = nlen;
29123 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29124 +                       ent.type = DT_UNKNOWN;
29125 +
29126 +               /* unnecessary to support mmap_sem since this is a dir */
29127 +               err = -EFAULT;
29128 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29129 +                       goto out;
29130 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29131 +                       goto out;
29132 +               /* the terminating NULL */
29133 +               if (__put_user(0, arg->ent.e->name + nlen))
29134 +                       goto out;
29135 +               err = 0;
29136 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29137 +               arg->ent.ul += len;
29138 +               rdu->rent++;
29139 +       } else {
29140 +               err = -EFAULT;
29141 +               au_fset_rdu(rdu->cookie.flags, FULL);
29142 +               rdu->full = 1;
29143 +               rdu->tail = arg->ent;
29144 +       }
29145 +
29146 +out:
29147 +       /* AuTraceErr(err); */
29148 +       return !err;
29149 +}
29150 +
29151 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29152 +{
29153 +       int err;
29154 +       loff_t offset;
29155 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29156 +
29157 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29158 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29159 +       err = offset;
29160 +       if (unlikely(offset != cookie->h_pos))
29161 +               goto out;
29162 +
29163 +       err = 0;
29164 +       do {
29165 +               arg->err = 0;
29166 +               au_fclr_rdu(cookie->flags, CALLED);
29167 +               /* smp_mb(); */
29168 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29169 +               if (err >= 0)
29170 +                       err = arg->err;
29171 +       } while (!err
29172 +                && au_ftest_rdu(cookie->flags, CALLED)
29173 +                && !au_ftest_rdu(cookie->flags, FULL));
29174 +       cookie->h_pos = h_file->f_pos;
29175 +
29176 +out:
29177 +       AuTraceErr(err);
29178 +       return err;
29179 +}
29180 +
29181 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29182 +{
29183 +       int err;
29184 +       aufs_bindex_t bbot;
29185 +       struct au_rdu_arg arg = {
29186 +               .ctx = {
29187 +                       .actor = au_rdu_fill
29188 +               }
29189 +       };
29190 +       struct dentry *dentry;
29191 +       struct inode *inode;
29192 +       struct file *h_file;
29193 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29194 +
29195 +       /* VERIFY_WRITE */
29196 +       err = !access_ok(rdu->ent.e, rdu->sz);
29197 +       if (unlikely(err)) {
29198 +               err = -EFAULT;
29199 +               AuTraceErr(err);
29200 +               goto out;
29201 +       }
29202 +       rdu->rent = 0;
29203 +       rdu->tail = rdu->ent;
29204 +       rdu->full = 0;
29205 +       arg.rdu = rdu;
29206 +       arg.ent = rdu->ent;
29207 +       arg.end = arg.ent.ul;
29208 +       arg.end += rdu->sz;
29209 +
29210 +       err = -ENOTDIR;
29211 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29212 +               goto out;
29213 +
29214 +       err = security_file_permission(file, MAY_READ);
29215 +       AuTraceErr(err);
29216 +       if (unlikely(err))
29217 +               goto out;
29218 +
29219 +       dentry = file->f_path.dentry;
29220 +       inode = d_inode(dentry);
29221 +       inode_lock_shared(inode);
29222 +
29223 +       arg.sb = inode->i_sb;
29224 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29225 +       if (unlikely(err))
29226 +               goto out_mtx;
29227 +       err = au_alive_dir(dentry);
29228 +       if (unlikely(err))
29229 +               goto out_si;
29230 +       /* todo: reval? */
29231 +       fi_read_lock(file);
29232 +
29233 +       err = -EAGAIN;
29234 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29235 +                    && cookie->generation != au_figen(file)))
29236 +               goto out_unlock;
29237 +
29238 +       err = 0;
29239 +       if (!rdu->blk) {
29240 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29241 +               if (!rdu->blk)
29242 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29243 +       }
29244 +       bbot = au_fbtop(file);
29245 +       if (cookie->bindex < bbot)
29246 +               cookie->bindex = bbot;
29247 +       bbot = au_fbbot_dir(file);
29248 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29249 +       for (; !err && cookie->bindex <= bbot;
29250 +            cookie->bindex++, cookie->h_pos = 0) {
29251 +               h_file = au_hf_dir(file, cookie->bindex);
29252 +               if (!h_file)
29253 +                       continue;
29254 +
29255 +               au_fclr_rdu(cookie->flags, FULL);
29256 +               err = au_rdu_do(h_file, &arg);
29257 +               AuTraceErr(err);
29258 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29259 +                       break;
29260 +       }
29261 +       AuDbg("rent %llu\n", rdu->rent);
29262 +
29263 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29264 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29265 +               au_fset_rdu(cookie->flags, CONT);
29266 +               cookie->generation = au_figen(file);
29267 +       }
29268 +
29269 +       ii_read_lock_child(inode);
29270 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29271 +       ii_read_unlock(inode);
29272 +
29273 +out_unlock:
29274 +       fi_read_unlock(file);
29275 +out_si:
29276 +       si_read_unlock(arg.sb);
29277 +out_mtx:
29278 +       inode_unlock_shared(inode);
29279 +out:
29280 +       AuTraceErr(err);
29281 +       return err;
29282 +}
29283 +
29284 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29285 +{
29286 +       int err;
29287 +       ino_t ino;
29288 +       unsigned long long nent;
29289 +       union au_rdu_ent_ul *u;
29290 +       struct au_rdu_ent ent;
29291 +       struct super_block *sb;
29292 +
29293 +       err = 0;
29294 +       nent = rdu->nent;
29295 +       u = &rdu->ent;
29296 +       sb = file->f_path.dentry->d_sb;
29297 +       si_read_lock(sb, AuLock_FLUSH);
29298 +       while (nent-- > 0) {
29299 +               /* unnecessary to support mmap_sem since this is a dir */
29300 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29301 +               if (!err)
29302 +                       /* VERIFY_WRITE */
29303 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29304 +               if (unlikely(err)) {
29305 +                       err = -EFAULT;
29306 +                       AuTraceErr(err);
29307 +                       break;
29308 +               }
29309 +
29310 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29311 +               if (!ent.wh)
29312 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29313 +               else
29314 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29315 +                                       &ino);
29316 +               if (unlikely(err)) {
29317 +                       AuTraceErr(err);
29318 +                       break;
29319 +               }
29320 +
29321 +               err = __put_user(ino, &u->e->ino);
29322 +               if (unlikely(err)) {
29323 +                       err = -EFAULT;
29324 +                       AuTraceErr(err);
29325 +                       break;
29326 +               }
29327 +               u->ul += au_rdu_len(ent.nlen);
29328 +       }
29329 +       si_read_unlock(sb);
29330 +
29331 +       return err;
29332 +}
29333 +
29334 +/* ---------------------------------------------------------------------- */
29335 +
29336 +static int au_rdu_verify(struct aufs_rdu *rdu)
29337 +{
29338 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29339 +             "%llu, b%d, 0x%x, g%u}\n",
29340 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29341 +             rdu->blk,
29342 +             rdu->rent, rdu->shwh, rdu->full,
29343 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29344 +             rdu->cookie.generation);
29345 +
29346 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29347 +               return 0;
29348 +
29349 +       AuDbg("%u:%u\n",
29350 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29351 +       return -EINVAL;
29352 +}
29353 +
29354 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29355 +{
29356 +       long err, e;
29357 +       struct aufs_rdu rdu;
29358 +       void __user *p = (void __user *)arg;
29359 +
29360 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29361 +       if (unlikely(err)) {
29362 +               err = -EFAULT;
29363 +               AuTraceErr(err);
29364 +               goto out;
29365 +       }
29366 +       err = au_rdu_verify(&rdu);
29367 +       if (unlikely(err))
29368 +               goto out;
29369 +
29370 +       switch (cmd) {
29371 +       case AUFS_CTL_RDU:
29372 +               err = au_rdu(file, &rdu);
29373 +               if (unlikely(err))
29374 +                       break;
29375 +
29376 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29377 +               if (unlikely(e)) {
29378 +                       err = -EFAULT;
29379 +                       AuTraceErr(err);
29380 +               }
29381 +               break;
29382 +       case AUFS_CTL_RDU_INO:
29383 +               err = au_rdu_ino(file, &rdu);
29384 +               break;
29385 +
29386 +       default:
29387 +               /* err = -ENOTTY; */
29388 +               err = -EINVAL;
29389 +       }
29390 +
29391 +out:
29392 +       AuTraceErr(err);
29393 +       return err;
29394 +}
29395 +
29396 +#ifdef CONFIG_COMPAT
29397 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29398 +{
29399 +       long err, e;
29400 +       struct aufs_rdu rdu;
29401 +       void __user *p = compat_ptr(arg);
29402 +
29403 +       /* todo: get_user()? */
29404 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29405 +       if (unlikely(err)) {
29406 +               err = -EFAULT;
29407 +               AuTraceErr(err);
29408 +               goto out;
29409 +       }
29410 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29411 +       err = au_rdu_verify(&rdu);
29412 +       if (unlikely(err))
29413 +               goto out;
29414 +
29415 +       switch (cmd) {
29416 +       case AUFS_CTL_RDU:
29417 +               err = au_rdu(file, &rdu);
29418 +               if (unlikely(err))
29419 +                       break;
29420 +
29421 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29422 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29423 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29424 +               if (unlikely(e)) {
29425 +                       err = -EFAULT;
29426 +                       AuTraceErr(err);
29427 +               }
29428 +               break;
29429 +       case AUFS_CTL_RDU_INO:
29430 +               err = au_rdu_ino(file, &rdu);
29431 +               break;
29432 +
29433 +       default:
29434 +               /* err = -ENOTTY; */
29435 +               err = -EINVAL;
29436 +       }
29437 +
29438 +out:
29439 +       AuTraceErr(err);
29440 +       return err;
29441 +}
29442 +#endif
29443 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29444 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29445 +++ linux/fs/aufs/rwsem.h       2022-11-05 23:02:18.969222617 +0100
29446 @@ -0,0 +1,85 @@
29447 +/* SPDX-License-Identifier: GPL-2.0 */
29448 +/*
29449 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29450 + *
29451 + * This program is free software; you can redistribute it and/or modify
29452 + * it under the terms of the GNU General Public License as published by
29453 + * the Free Software Foundation; either version 2 of the License, or
29454 + * (at your option) any later version.
29455 + *
29456 + * This program is distributed in the hope that it will be useful,
29457 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29458 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29459 + * GNU General Public License for more details.
29460 + *
29461 + * You should have received a copy of the GNU General Public License
29462 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29463 + */
29464 +
29465 +/*
29466 + * simple read-write semaphore wrappers
29467 + */
29468 +
29469 +#ifndef __AUFS_RWSEM_H__
29470 +#define __AUFS_RWSEM_H__
29471 +
29472 +#ifdef __KERNEL__
29473 +
29474 +#include "debug.h"
29475 +
29476 +/* in the future, the name 'au_rwsem' will be totally gone */
29477 +#define au_rwsem       rw_semaphore
29478 +
29479 +/* to debug easier, do not make them inlined functions */
29480 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29481 +
29482 +#ifdef CONFIG_LOCKDEP
29483 +/* rwsem_is_locked() is unusable */
29484 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29485 +                                         && !lockdep_recursing(current) \
29486 +                                         && debug_locks                \
29487 +                                         && !lockdep_is_held_type(rw, 1))
29488 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29489 +                                         && !lockdep_recursing(current) \
29490 +                                         && debug_locks                \
29491 +                                         && !lockdep_is_held_type(rw, 0))
29492 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29493 +                                         && !lockdep_recursing(current) \
29494 +                                         && debug_locks                \
29495 +                                         && !lockdep_is_held(rw))
29496 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29497 +                                         && !lockdep_recursing(current) \
29498 +                                         && debug_locks                \
29499 +                                         && lockdep_is_held(rw))
29500 +#else
29501 +#define AuRwMustReadLock(rw)   do {} while (0)
29502 +#define AuRwMustWriteLock(rw)  do {} while (0)
29503 +#define AuRwMustAnyLock(rw)    do {} while (0)
29504 +#define AuRwDestroy(rw)                do {} while (0)
29505 +#endif
29506 +
29507 +#define au_rw_init(rw) init_rwsem(rw)
29508 +
29509 +#define au_rw_init_wlock(rw) do {              \
29510 +               au_rw_init(rw);                 \
29511 +               down_write(rw);                 \
29512 +       } while (0)
29513 +
29514 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29515 +               au_rw_init(rw);                 \
29516 +               down_write_nested(rw, lsc);     \
29517 +       } while (0)
29518 +
29519 +#define au_rw_read_lock(rw)            down_read(rw)
29520 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29521 +#define au_rw_read_unlock(rw)          up_read(rw)
29522 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29523 +#define au_rw_write_lock(rw)           down_write(rw)
29524 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29525 +#define au_rw_write_unlock(rw)         up_write(rw)
29526 +/* why is not _nested version defined? */
29527 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29528 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29529 +
29530 +#endif /* __KERNEL__ */
29531 +#endif /* __AUFS_RWSEM_H__ */
29532 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29533 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29534 +++ linux/fs/aufs/sbinfo.c      2022-11-05 23:02:18.969222617 +0100
29535 @@ -0,0 +1,316 @@
29536 +// SPDX-License-Identifier: GPL-2.0
29537 +/*
29538 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29539 + *
29540 + * This program is free software; you can redistribute it and/or modify
29541 + * it under the terms of the GNU General Public License as published by
29542 + * the Free Software Foundation; either version 2 of the License, or
29543 + * (at your option) any later version.
29544 + *
29545 + * This program is distributed in the hope that it will be useful,
29546 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29547 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29548 + * GNU General Public License for more details.
29549 + *
29550 + * You should have received a copy of the GNU General Public License
29551 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29552 + */
29553 +
29554 +/*
29555 + * superblock private data
29556 + */
29557 +
29558 +#include <linux/iversion.h>
29559 +#include "aufs.h"
29560 +
29561 +/*
29562 + * they are necessary regardless sysfs is disabled.
29563 + */
29564 +void au_si_free(struct kobject *kobj)
29565 +{
29566 +       int i;
29567 +       struct au_sbinfo *sbinfo;
29568 +       char *locked __maybe_unused; /* debug only */
29569 +
29570 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29571 +       for (i = 0; i < AuPlink_NHASH; i++)
29572 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29573 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29574 +
29575 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29576 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29577 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29578 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29579 +
29580 +       dbgaufs_si_fin(sbinfo);
29581 +       au_rw_write_lock(&sbinfo->si_rwsem);
29582 +       au_br_free(sbinfo);
29583 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29584 +
29585 +       au_kfree_try_rcu(sbinfo->si_branch);
29586 +       mutex_destroy(&sbinfo->si_xib_mtx);
29587 +       AuRwDestroy(&sbinfo->si_rwsem);
29588 +
29589 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29590 +       /* si_nfiles is waited too */
29591 +       au_kfree_rcu(sbinfo);
29592 +}
29593 +
29594 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29595 +{
29596 +       struct au_sbinfo *sbinfo;
29597 +       int err, i;
29598 +
29599 +       err = -ENOMEM;
29600 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29601 +       if (unlikely(!sbinfo))
29602 +               goto out;
29603 +
29604 +       /* will be reallocated separately */
29605 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29606 +       if (unlikely(!sbinfo->si_branch))
29607 +               goto out_sbinfo;
29608 +
29609 +       err = sysaufs_si_init(sbinfo);
29610 +       if (!err) {
29611 +               dbgaufs_si_null(sbinfo);
29612 +               err = dbgaufs_si_init(sbinfo);
29613 +               if (unlikely(err))
29614 +                       kobject_put(&sbinfo->si_kobj);
29615 +       }
29616 +       if (unlikely(err))
29617 +               goto out_br;
29618 +
29619 +       au_nwt_init(&sbinfo->si_nowait);
29620 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29621 +
29622 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29623 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29624 +
29625 +       sbinfo->si_bbot = -1;
29626 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29627 +
29628 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29629 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29630 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29631 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29632 +
29633 +       au_fhsm_init(sbinfo);
29634 +
29635 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29636 +
29637 +       sbinfo->si_xino_jiffy = jiffies;
29638 +       sbinfo->si_xino_expire
29639 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29640 +       mutex_init(&sbinfo->si_xib_mtx);
29641 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29642 +
29643 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29644 +
29645 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29646 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29647 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29648 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29649 +
29650 +       for (i = 0; i < AuPlink_NHASH; i++)
29651 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29652 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29653 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29654 +
29655 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29656 +
29657 +       /* with getattr by default */
29658 +       sbinfo->si_iop_array = aufs_iop;
29659 +
29660 +       /* leave other members for sysaufs and si_mnt. */
29661 +       sbinfo->si_sb = sb;
29662 +       if (sb) {
29663 +               sb->s_fs_info = sbinfo;
29664 +               si_pid_set(sb);
29665 +       }
29666 +       return sbinfo; /* success */
29667 +
29668 +out_br:
29669 +       au_kfree_try_rcu(sbinfo->si_branch);
29670 +out_sbinfo:
29671 +       au_kfree_rcu(sbinfo);
29672 +out:
29673 +       return ERR_PTR(err);
29674 +}
29675 +
29676 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29677 +{
29678 +       int err, sz;
29679 +       struct au_branch **brp;
29680 +
29681 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29682 +
29683 +       err = -ENOMEM;
29684 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29685 +       if (unlikely(!sz))
29686 +               sz = sizeof(*brp);
29687 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29688 +                          may_shrink);
29689 +       if (brp) {
29690 +               sbinfo->si_branch = brp;
29691 +               err = 0;
29692 +       }
29693 +
29694 +       return err;
29695 +}
29696 +
29697 +/* ---------------------------------------------------------------------- */
29698 +
29699 +unsigned int au_sigen_inc(struct super_block *sb)
29700 +{
29701 +       unsigned int gen;
29702 +       struct inode *inode;
29703 +
29704 +       SiMustWriteLock(sb);
29705 +
29706 +       gen = ++au_sbi(sb)->si_generation;
29707 +       au_update_digen(sb->s_root);
29708 +       inode = d_inode(sb->s_root);
29709 +       au_update_iigen(inode, /*half*/0);
29710 +       inode_inc_iversion(inode);
29711 +       return gen;
29712 +}
29713 +
29714 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29715 +{
29716 +       aufs_bindex_t br_id;
29717 +       int i;
29718 +       struct au_sbinfo *sbinfo;
29719 +
29720 +       SiMustWriteLock(sb);
29721 +
29722 +       sbinfo = au_sbi(sb);
29723 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29724 +               br_id = ++sbinfo->si_last_br_id;
29725 +               AuDebugOn(br_id < 0);
29726 +               if (br_id && au_br_index(sb, br_id) < 0)
29727 +                       return br_id;
29728 +       }
29729 +
29730 +       return -1;
29731 +}
29732 +
29733 +/* ---------------------------------------------------------------------- */
29734 +
29735 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29736 +int si_read_lock(struct super_block *sb, int flags)
29737 +{
29738 +       int err;
29739 +
29740 +       err = 0;
29741 +       if (au_ftest_lock(flags, FLUSH))
29742 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29743 +
29744 +       si_noflush_read_lock(sb);
29745 +       err = au_plink_maint(sb, flags);
29746 +       if (unlikely(err))
29747 +               si_read_unlock(sb);
29748 +
29749 +       return err;
29750 +}
29751 +
29752 +int si_write_lock(struct super_block *sb, int flags)
29753 +{
29754 +       int err;
29755 +
29756 +       if (au_ftest_lock(flags, FLUSH))
29757 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29758 +
29759 +       si_noflush_write_lock(sb);
29760 +       err = au_plink_maint(sb, flags);
29761 +       if (unlikely(err))
29762 +               si_write_unlock(sb);
29763 +
29764 +       return err;
29765 +}
29766 +
29767 +/* dentry and super_block lock. call at entry point */
29768 +int aufs_read_lock(struct dentry *dentry, int flags)
29769 +{
29770 +       int err;
29771 +       struct super_block *sb;
29772 +
29773 +       sb = dentry->d_sb;
29774 +       err = si_read_lock(sb, flags);
29775 +       if (unlikely(err))
29776 +               goto out;
29777 +
29778 +       if (au_ftest_lock(flags, DW))
29779 +               di_write_lock_child(dentry);
29780 +       else
29781 +               di_read_lock_child(dentry, flags);
29782 +
29783 +       if (au_ftest_lock(flags, GEN)) {
29784 +               err = au_digen_test(dentry, au_sigen(sb));
29785 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29786 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29787 +               else if (!err)
29788 +                       err = au_dbrange_test(dentry);
29789 +               if (unlikely(err))
29790 +                       aufs_read_unlock(dentry, flags);
29791 +       }
29792 +
29793 +out:
29794 +       return err;
29795 +}
29796 +
29797 +void aufs_read_unlock(struct dentry *dentry, int flags)
29798 +{
29799 +       if (au_ftest_lock(flags, DW))
29800 +               di_write_unlock(dentry);
29801 +       else
29802 +               di_read_unlock(dentry, flags);
29803 +       si_read_unlock(dentry->d_sb);
29804 +}
29805 +
29806 +void aufs_write_lock(struct dentry *dentry)
29807 +{
29808 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29809 +       di_write_lock_child(dentry);
29810 +}
29811 +
29812 +void aufs_write_unlock(struct dentry *dentry)
29813 +{
29814 +       di_write_unlock(dentry);
29815 +       si_write_unlock(dentry->d_sb);
29816 +}
29817 +
29818 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29819 +{
29820 +       int err;
29821 +       unsigned int sigen;
29822 +       struct super_block *sb;
29823 +
29824 +       sb = d1->d_sb;
29825 +       err = si_read_lock(sb, flags);
29826 +       if (unlikely(err))
29827 +               goto out;
29828 +
29829 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29830 +
29831 +       if (au_ftest_lock(flags, GEN)) {
29832 +               sigen = au_sigen(sb);
29833 +               err = au_digen_test(d1, sigen);
29834 +               AuDebugOn(!err && au_dbrange_test(d1));
29835 +               if (!err) {
29836 +                       err = au_digen_test(d2, sigen);
29837 +                       AuDebugOn(!err && au_dbrange_test(d2));
29838 +               }
29839 +               if (unlikely(err))
29840 +                       aufs_read_and_write_unlock2(d1, d2);
29841 +       }
29842 +
29843 +out:
29844 +       return err;
29845 +}
29846 +
29847 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29848 +{
29849 +       di_write_unlock2(d1, d2);
29850 +       si_read_unlock(d1->d_sb);
29851 +}
29852 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29853 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29854 +++ linux/fs/aufs/super.c       2022-11-05 23:02:18.969222617 +0100
29855 @@ -0,0 +1,871 @@
29856 +// SPDX-License-Identifier: GPL-2.0
29857 +/*
29858 + * Copyright (C) 2005-2022 Junjiro R. Okajima
29859 + *
29860 + * This program is free software; you can redistribute it and/or modify
29861 + * it under the terms of the GNU General Public License as published by
29862 + * the Free Software Foundation; either version 2 of the License, or
29863 + * (at your option) any later version.
29864 + *
29865 + * This program is distributed in the hope that it will be useful,
29866 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29867 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29868 + * GNU General Public License for more details.
29869 + *
29870 + * You should have received a copy of the GNU General Public License
29871 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29872 + */
29873 +
29874 +/*
29875 + * mount and super_block operations
29876 + */
29877 +
29878 +#include <linux/iversion.h>
29879 +#include <linux/mm.h>
29880 +#include <linux/seq_file.h>
29881 +#include <linux/statfs.h>
29882 +#include <linux/vmalloc.h>
29883 +#include "aufs.h"
29884 +
29885 +/*
29886 + * super_operations
29887 + */
29888 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29889 +{
29890 +       struct au_icntnr *c;
29891 +
29892 +       c = au_cache_alloc_icntnr(sb);
29893 +       if (c) {
29894 +               au_icntnr_init(c);
29895 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29896 +               c->iinfo.ii_hinode = NULL;
29897 +               return &c->vfs_inode;
29898 +       }
29899 +       return NULL;
29900 +}
29901 +
29902 +static void aufs_destroy_inode(struct inode *inode)
29903 +{
29904 +       if (!au_is_bad_inode(inode))
29905 +               au_iinfo_fin(inode);
29906 +}
29907 +
29908 +static void aufs_free_inode(struct inode *inode)
29909 +{
29910 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29911 +}
29912 +
29913 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29914 +{
29915 +       struct inode *inode;
29916 +       int err;
29917 +
29918 +       inode = iget_locked(sb, ino);
29919 +       if (unlikely(!inode)) {
29920 +               inode = ERR_PTR(-ENOMEM);
29921 +               goto out;
29922 +       }
29923 +       if (!(inode->i_state & I_NEW))
29924 +               goto out;
29925 +
29926 +       err = au_xigen_new(inode);
29927 +       if (!err)
29928 +               err = au_iinfo_init(inode);
29929 +       if (!err)
29930 +               inode_inc_iversion(inode);
29931 +       else {
29932 +               iget_failed(inode);
29933 +               inode = ERR_PTR(err);
29934 +       }
29935 +
29936 +out:
29937 +       /* never return NULL */
29938 +       AuDebugOn(!inode);
29939 +       AuTraceErrPtr(inode);
29940 +       return inode;
29941 +}
29942 +
29943 +/* lock free root dinfo */
29944 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29945 +{
29946 +       int err;
29947 +       aufs_bindex_t bindex, bbot;
29948 +       struct path path;
29949 +       struct au_hdentry *hdp;
29950 +       struct au_branch *br;
29951 +       au_br_perm_str_t perm;
29952 +
29953 +       err = 0;
29954 +       bbot = au_sbbot(sb);
29955 +       bindex = 0;
29956 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29957 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29958 +               br = au_sbr(sb, bindex);
29959 +               path.mnt = au_br_mnt(br);
29960 +               path.dentry = hdp->hd_dentry;
29961 +               err = au_seq_path(seq, &path);
29962 +               if (!err) {
29963 +                       au_optstr_br_perm(&perm, br->br_perm);
29964 +                       seq_printf(seq, "=%s", perm.a);
29965 +                       if (bindex != bbot)
29966 +                               seq_putc(seq, ':');
29967 +               }
29968 +       }
29969 +       if (unlikely(err || seq_has_overflowed(seq)))
29970 +               err = -E2BIG;
29971 +
29972 +       return err;
29973 +}
29974 +
29975 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29976 +                      const char *append)
29977 +{
29978 +       char *p;
29979 +
29980 +       p = fmt;
29981 +       while (*pat != ':')
29982 +               *p++ = *pat++;
29983 +       *p++ = *pat++;
29984 +       strcpy(p, append);
29985 +       AuDebugOn(strlen(fmt) >= len);
29986 +}
29987 +
29988 +static void au_show_wbr_create(struct seq_file *m, int v,
29989 +                              struct au_sbinfo *sbinfo)
29990 +{
29991 +       const char *pat;
29992 +       char fmt[32];
29993 +       struct au_wbr_mfs *mfs;
29994 +
29995 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29996 +
29997 +       seq_puts(m, ",create=");
29998 +       pat = au_optstr_wbr_create(v);
29999 +       mfs = &sbinfo->si_wbr_mfs;
30000 +       switch (v) {
30001 +       case AuWbrCreate_TDP:
30002 +       case AuWbrCreate_RR:
30003 +       case AuWbrCreate_MFS:
30004 +       case AuWbrCreate_PMFS:
30005 +               seq_puts(m, pat);
30006 +               break;
30007 +       case AuWbrCreate_MFSRR:
30008 +       case AuWbrCreate_TDMFS:
30009 +       case AuWbrCreate_PMFSRR:
30010 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
30011 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
30012 +               break;
30013 +       case AuWbrCreate_MFSV:
30014 +       case AuWbrCreate_PMFSV:
30015 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
30016 +               seq_printf(m, fmt,
30017 +                          jiffies_to_msecs(mfs->mfs_expire)
30018 +                          / MSEC_PER_SEC);
30019 +               break;
30020 +       case AuWbrCreate_MFSRRV:
30021 +       case AuWbrCreate_TDMFSV:
30022 +       case AuWbrCreate_PMFSRRV:
30023 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
30024 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
30025 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
30026 +               break;
30027 +       default:
30028 +               BUG();
30029 +       }
30030 +}
30031 +
30032 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
30033 +{
30034 +#ifdef CONFIG_SYSFS
30035 +       return 0;
30036 +#else
30037 +       int err;
30038 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
30039 +       aufs_bindex_t bindex, brid;
30040 +       struct qstr *name;
30041 +       struct file *f;
30042 +       struct dentry *d, *h_root;
30043 +       struct au_branch *br;
30044 +
30045 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30046 +
30047 +       err = 0;
30048 +       f = au_sbi(sb)->si_xib;
30049 +       if (!f)
30050 +               goto out;
30051 +
30052 +       /* stop printing the default xino path on the first writable branch */
30053 +       h_root = NULL;
30054 +       bindex = au_xi_root(sb, f->f_path.dentry);
30055 +       if (bindex >= 0) {
30056 +               br = au_sbr_sb(sb, bindex);
30057 +               h_root = au_br_dentry(br);
30058 +       }
30059 +
30060 +       d = f->f_path.dentry;
30061 +       name = &d->d_name;
30062 +       /* safe ->d_parent because the file is unlinked */
30063 +       if (d->d_parent == h_root
30064 +           && name->len == len
30065 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30066 +               goto out;
30067 +
30068 +       seq_puts(seq, ",xino=");
30069 +       err = au_xino_path(seq, f);
30070 +
30071 +out:
30072 +       return err;
30073 +#endif
30074 +}
30075 +
30076 +/* seq_file will re-call me in case of too long string */
30077 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30078 +{
30079 +       int err;
30080 +       unsigned int mnt_flags, v;
30081 +       struct super_block *sb;
30082 +       struct au_sbinfo *sbinfo;
30083 +
30084 +#define AuBool(name, str) do { \
30085 +       v = au_opt_test(mnt_flags, name); \
30086 +       if (v != au_opt_test(AuOpt_Def, name)) \
30087 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30088 +} while (0)
30089 +
30090 +#define AuStr(name, str) do { \
30091 +       v = mnt_flags & AuOptMask_##name; \
30092 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30093 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30094 +} while (0)
30095 +
30096 +#define AuUInt(name, str, val) do { \
30097 +       if (val != AUFS_##name##_DEF) \
30098 +               seq_printf(m, "," #str "=%u", val); \
30099 +} while (0)
30100 +
30101 +       sb = dentry->d_sb;
30102 +       if (sb->s_flags & SB_POSIXACL)
30103 +               seq_puts(m, ",acl");
30104 +#if 0 /* reserved for future use */
30105 +       if (sb->s_flags & SB_I_VERSION)
30106 +               seq_puts(m, ",i_version");
30107 +#endif
30108 +
30109 +       /* lock free root dinfo */
30110 +       si_noflush_read_lock(sb);
30111 +       sbinfo = au_sbi(sb);
30112 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30113 +
30114 +       mnt_flags = au_mntflags(sb);
30115 +       if (au_opt_test(mnt_flags, XINO)) {
30116 +               err = au_show_xino(m, sb);
30117 +               if (unlikely(err))
30118 +                       goto out;
30119 +       } else
30120 +               seq_puts(m, ",noxino");
30121 +
30122 +       AuBool(TRUNC_XINO, trunc_xino);
30123 +       AuStr(UDBA, udba);
30124 +       AuBool(SHWH, shwh);
30125 +       AuBool(PLINK, plink);
30126 +       AuBool(DIO, dio);
30127 +       AuBool(DIRPERM1, dirperm1);
30128 +
30129 +       v = sbinfo->si_wbr_create;
30130 +       if (v != AuWbrCreate_Def)
30131 +               au_show_wbr_create(m, v, sbinfo);
30132 +
30133 +       v = sbinfo->si_wbr_copyup;
30134 +       if (v != AuWbrCopyup_Def)
30135 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30136 +
30137 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30138 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30139 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30140 +
30141 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30142 +
30143 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30144 +       AuUInt(RDCACHE, rdcache, v);
30145 +
30146 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30147 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30148 +
30149 +       au_fhsm_show(m, sbinfo);
30150 +
30151 +       AuBool(DIRREN, dirren);
30152 +       AuBool(SUM, sum);
30153 +       /* AuBool(SUM_W, wsum); */
30154 +       AuBool(WARN_PERM, warn_perm);
30155 +       AuBool(VERBOSE, verbose);
30156 +
30157 +out:
30158 +       /* be sure to print "br:" last */
30159 +       if (!sysaufs_brs) {
30160 +               seq_puts(m, ",br:");
30161 +               au_show_brs(m, sb);
30162 +       }
30163 +       si_read_unlock(sb);
30164 +       return 0;
30165 +
30166 +#undef AuBool
30167 +#undef AuStr
30168 +#undef AuUInt
30169 +}
30170 +
30171 +/* ---------------------------------------------------------------------- */
30172 +
30173 +/* sum mode which returns the summation for statfs(2) */
30174 +
30175 +static u64 au_add_till_max(u64 a, u64 b)
30176 +{
30177 +       u64 old;
30178 +
30179 +       old = a;
30180 +       a += b;
30181 +       if (old <= a)
30182 +               return a;
30183 +       return ULLONG_MAX;
30184 +}
30185 +
30186 +static u64 au_mul_till_max(u64 a, long mul)
30187 +{
30188 +       u64 old;
30189 +
30190 +       old = a;
30191 +       a *= mul;
30192 +       if (old <= a)
30193 +               return a;
30194 +       return ULLONG_MAX;
30195 +}
30196 +
30197 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30198 +{
30199 +       int err;
30200 +       long bsize, factor;
30201 +       u64 blocks, bfree, bavail, files, ffree;
30202 +       aufs_bindex_t bbot, bindex, i;
30203 +       unsigned char shared;
30204 +       struct path h_path;
30205 +       struct super_block *h_sb;
30206 +
30207 +       err = 0;
30208 +       bsize = LONG_MAX;
30209 +       files = 0;
30210 +       ffree = 0;
30211 +       blocks = 0;
30212 +       bfree = 0;
30213 +       bavail = 0;
30214 +       bbot = au_sbbot(sb);
30215 +       for (bindex = 0; bindex <= bbot; bindex++) {
30216 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30217 +               h_sb = h_path.mnt->mnt_sb;
30218 +               shared = 0;
30219 +               for (i = 0; !shared && i < bindex; i++)
30220 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30221 +               if (shared)
30222 +                       continue;
30223 +
30224 +               /* sb->s_root for NFS is unreliable */
30225 +               h_path.dentry = h_path.mnt->mnt_root;
30226 +               err = vfs_statfs(&h_path, buf);
30227 +               if (unlikely(err))
30228 +                       goto out;
30229 +
30230 +               if (bsize > buf->f_bsize) {
30231 +                       /*
30232 +                        * we will reduce bsize, so we have to expand blocks
30233 +                        * etc. to match them again
30234 +                        */
30235 +                       factor = (bsize / buf->f_bsize);
30236 +                       blocks = au_mul_till_max(blocks, factor);
30237 +                       bfree = au_mul_till_max(bfree, factor);
30238 +                       bavail = au_mul_till_max(bavail, factor);
30239 +                       bsize = buf->f_bsize;
30240 +               }
30241 +
30242 +               factor = (buf->f_bsize / bsize);
30243 +               blocks = au_add_till_max(blocks,
30244 +                               au_mul_till_max(buf->f_blocks, factor));
30245 +               bfree = au_add_till_max(bfree,
30246 +                               au_mul_till_max(buf->f_bfree, factor));
30247 +               bavail = au_add_till_max(bavail,
30248 +                               au_mul_till_max(buf->f_bavail, factor));
30249 +               files = au_add_till_max(files, buf->f_files);
30250 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30251 +       }
30252 +
30253 +       buf->f_bsize = bsize;
30254 +       buf->f_blocks = blocks;
30255 +       buf->f_bfree = bfree;
30256 +       buf->f_bavail = bavail;
30257 +       buf->f_files = files;
30258 +       buf->f_ffree = ffree;
30259 +       buf->f_frsize = 0;
30260 +
30261 +out:
30262 +       return err;
30263 +}
30264 +
30265 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30266 +{
30267 +       int err;
30268 +       struct path h_path;
30269 +       struct super_block *sb;
30270 +
30271 +       /* lock free root dinfo */
30272 +       sb = dentry->d_sb;
30273 +       si_noflush_read_lock(sb);
30274 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30275 +               /* sb->s_root for NFS is unreliable */
30276 +               h_path.mnt = au_sbr_mnt(sb, 0);
30277 +               h_path.dentry = h_path.mnt->mnt_root;
30278 +               err = vfs_statfs(&h_path, buf);
30279 +       } else
30280 +               err = au_statfs_sum(sb, buf);
30281 +       si_read_unlock(sb);
30282 +
30283 +       if (!err) {
30284 +               buf->f_type = AUFS_SUPER_MAGIC;
30285 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30286 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30287 +       }
30288 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30289 +
30290 +       return err;
30291 +}
30292 +
30293 +/* ---------------------------------------------------------------------- */
30294 +
30295 +static int aufs_sync_fs(struct super_block *sb, int wait)
30296 +{
30297 +       int err, e;
30298 +       aufs_bindex_t bbot, bindex;
30299 +       struct au_branch *br;
30300 +       struct super_block *h_sb;
30301 +
30302 +       err = 0;
30303 +       si_noflush_read_lock(sb);
30304 +       bbot = au_sbbot(sb);
30305 +       for (bindex = 0; bindex <= bbot; bindex++) {
30306 +               br = au_sbr(sb, bindex);
30307 +               if (!au_br_writable(br->br_perm))
30308 +                       continue;
30309 +
30310 +               h_sb = au_sbr_sb(sb, bindex);
30311 +               e = vfsub_sync_filesystem(h_sb);
30312 +               if (unlikely(e && !err))
30313 +                       err = e;
30314 +               /* go on even if an error happens */
30315 +       }
30316 +       si_read_unlock(sb);
30317 +
30318 +       return err;
30319 +}
30320 +
30321 +/* ---------------------------------------------------------------------- */
30322 +
30323 +/* final actions when unmounting a file system */
30324 +static void aufs_put_super(struct super_block *sb)
30325 +{
30326 +       struct au_sbinfo *sbinfo;
30327 +
30328 +       sbinfo = au_sbi(sb);
30329 +       if (sbinfo)
30330 +               kobject_put(&sbinfo->si_kobj);
30331 +}
30332 +
30333 +/* ---------------------------------------------------------------------- */
30334 +
30335 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30336 +                    struct super_block *sb, void *arg)
30337 +{
30338 +       void *array;
30339 +       unsigned long long n, sz;
30340 +
30341 +       array = NULL;
30342 +       n = 0;
30343 +       if (!*hint)
30344 +               goto out;
30345 +
30346 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30347 +               array = ERR_PTR(-EMFILE);
30348 +               pr_err("hint %llu\n", *hint);
30349 +               goto out;
30350 +       }
30351 +
30352 +       sz = sizeof(array) * *hint;
30353 +       array = kzalloc(sz, GFP_NOFS);
30354 +       if (unlikely(!array))
30355 +               array = vzalloc(sz);
30356 +       if (unlikely(!array)) {
30357 +               array = ERR_PTR(-ENOMEM);
30358 +               goto out;
30359 +       }
30360 +
30361 +       n = cb(sb, array, *hint, arg);
30362 +       AuDebugOn(n > *hint);
30363 +
30364 +out:
30365 +       *hint = n;
30366 +       return array;
30367 +}
30368 +
30369 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30370 +                                      unsigned long long max __maybe_unused,
30371 +                                      void *arg)
30372 +{
30373 +       unsigned long long n;
30374 +       struct inode **p, *inode;
30375 +       struct list_head *head;
30376 +
30377 +       n = 0;
30378 +       p = a;
30379 +       head = arg;
30380 +       spin_lock(&sb->s_inode_list_lock);
30381 +       list_for_each_entry(inode, head, i_sb_list) {
30382 +               if (!au_is_bad_inode(inode)
30383 +                   && au_ii(inode)->ii_btop >= 0) {
30384 +                       spin_lock(&inode->i_lock);
30385 +                       if (atomic_read(&inode->i_count)) {
30386 +                               au_igrab(inode);
30387 +                               *p++ = inode;
30388 +                               n++;
30389 +                               AuDebugOn(n > max);
30390 +                       }
30391 +                       spin_unlock(&inode->i_lock);
30392 +               }
30393 +       }
30394 +       spin_unlock(&sb->s_inode_list_lock);
30395 +
30396 +       return n;
30397 +}
30398 +
30399 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30400 +{
30401 +       struct au_sbinfo *sbi;
30402 +
30403 +       sbi = au_sbi(sb);
30404 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30405 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30406 +}
30407 +
30408 +void au_iarray_free(struct inode **a, unsigned long long max)
30409 +{
30410 +       unsigned long long ull;
30411 +
30412 +       for (ull = 0; ull < max; ull++)
30413 +               iput(a[ull]);
30414 +       kvfree(a);
30415 +}
30416 +
30417 +/* ---------------------------------------------------------------------- */
30418 +
30419 +/*
30420 + * refresh dentry and inode at remount time.
30421 + */
30422 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30423 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30424 +                     struct dentry *parent)
30425 +{
30426 +       int err;
30427 +
30428 +       di_write_lock_child(dentry);
30429 +       di_read_lock_parent(parent, AuLock_IR);
30430 +       err = au_refresh_dentry(dentry, parent);
30431 +       if (!err && dir_flags)
30432 +               au_hn_reset(d_inode(dentry), dir_flags);
30433 +       di_read_unlock(parent, AuLock_IR);
30434 +       di_write_unlock(dentry);
30435 +
30436 +       return err;
30437 +}
30438 +
30439 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30440 +                          struct au_sbinfo *sbinfo,
30441 +                          const unsigned int dir_flags, unsigned int do_idop)
30442 +{
30443 +       int err;
30444 +       struct dentry *parent;
30445 +
30446 +       err = 0;
30447 +       parent = dget_parent(dentry);
30448 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30449 +               if (d_really_is_positive(dentry)) {
30450 +                       if (!d_is_dir(dentry))
30451 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30452 +                                                parent);
30453 +                       else {
30454 +                               err = au_do_refresh(dentry, dir_flags, parent);
30455 +                               if (unlikely(err))
30456 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30457 +                       }
30458 +               } else
30459 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30460 +               AuDbgDentry(dentry);
30461 +       }
30462 +       dput(parent);
30463 +
30464 +       if (!err) {
30465 +               if (do_idop)
30466 +                       au_refresh_dop(dentry, /*force_reval*/0);
30467 +       } else
30468 +               au_refresh_dop(dentry, /*force_reval*/1);
30469 +
30470 +       AuTraceErr(err);
30471 +       return err;
30472 +}
30473 +
30474 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30475 +{
30476 +       int err, i, j, ndentry, e;
30477 +       unsigned int sigen;
30478 +       struct au_dcsub_pages dpages;
30479 +       struct au_dpage *dpage;
30480 +       struct dentry **dentries, *d;
30481 +       struct au_sbinfo *sbinfo;
30482 +       struct dentry *root = sb->s_root;
30483 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30484 +
30485 +       if (do_idop)
30486 +               au_refresh_dop(root, /*force_reval*/0);
30487 +
30488 +       err = au_dpages_init(&dpages, GFP_NOFS);
30489 +       if (unlikely(err))
30490 +               goto out;
30491 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30492 +       if (unlikely(err))
30493 +               goto out_dpages;
30494 +
30495 +       sigen = au_sigen(sb);
30496 +       sbinfo = au_sbi(sb);
30497 +       for (i = 0; i < dpages.ndpage; i++) {
30498 +               dpage = dpages.dpages + i;
30499 +               dentries = dpage->dentries;
30500 +               ndentry = dpage->ndentry;
30501 +               for (j = 0; j < ndentry; j++) {
30502 +                       d = dentries[j];
30503 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30504 +                                           do_idop);
30505 +                       if (unlikely(e && !err))
30506 +                               err = e;
30507 +                       /* go on even err */
30508 +               }
30509 +       }
30510 +
30511 +out_dpages:
30512 +       au_dpages_free(&dpages);
30513 +out:
30514 +       return err;
30515 +}
30516 +
30517 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30518 +{
30519 +       int err, e;
30520 +       unsigned int sigen;
30521 +       unsigned long long max, ull;
30522 +       struct inode *inode, **array;
30523 +
30524 +       array = au_iarray_alloc(sb, &max);
30525 +       err = PTR_ERR(array);
30526 +       if (IS_ERR(array))
30527 +               goto out;
30528 +
30529 +       err = 0;
30530 +       sigen = au_sigen(sb);
30531 +       for (ull = 0; ull < max; ull++) {
30532 +               inode = array[ull];
30533 +               if (unlikely(!inode))
30534 +                       break;
30535 +
30536 +               e = 0;
30537 +               ii_write_lock_child(inode);
30538 +               if (au_iigen(inode, NULL) != sigen) {
30539 +                       e = au_refresh_hinode_self(inode);
30540 +                       if (unlikely(e)) {
30541 +                               au_refresh_iop(inode, /*force_getattr*/1);
30542 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30543 +                               if (!err)
30544 +                                       err = e;
30545 +                               /* go on even if err */
30546 +                       }
30547 +               }
30548 +               if (!e && do_idop)
30549 +                       au_refresh_iop(inode, /*force_getattr*/0);
30550 +               ii_write_unlock(inode);
30551 +       }
30552 +
30553 +       au_iarray_free(array, max);
30554 +
30555 +out:
30556 +       return err;
30557 +}
30558 +
30559 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30560 +{
30561 +       int err, e;
30562 +       unsigned int udba;
30563 +       aufs_bindex_t bindex, bbot;
30564 +       struct dentry *root;
30565 +       struct inode *inode;
30566 +       struct au_branch *br;
30567 +       struct au_sbinfo *sbi;
30568 +
30569 +       au_sigen_inc(sb);
30570 +       sbi = au_sbi(sb);
30571 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30572 +
30573 +       root = sb->s_root;
30574 +       DiMustNoWaiters(root);
30575 +       inode = d_inode(root);
30576 +       IiMustNoWaiters(inode);
30577 +
30578 +       udba = au_opt_udba(sb);
30579 +       bbot = au_sbbot(sb);
30580 +       for (bindex = 0; bindex <= bbot; bindex++) {
30581 +               br = au_sbr(sb, bindex);
30582 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30583 +               if (unlikely(err))
30584 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30585 +                               bindex, err);
30586 +               /* go on even if err */
30587 +       }
30588 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30589 +
30590 +       if (do_idop) {
30591 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30592 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30593 +                       sb->s_d_op = &aufs_dop_noreval;
30594 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30595 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30596 +               } else {
30597 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30598 +                       sb->s_d_op = &aufs_dop;
30599 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30600 +                       sbi->si_iop_array = aufs_iop;
30601 +               }
30602 +               pr_info("reset to %ps and %ps\n",
30603 +                       sb->s_d_op, sbi->si_iop_array);
30604 +       }
30605 +
30606 +       di_write_unlock(root);
30607 +       err = au_refresh_d(sb, do_idop);
30608 +       e = au_refresh_i(sb, do_idop);
30609 +       if (unlikely(e && !err))
30610 +               err = e;
30611 +       /* aufs_write_lock() calls ..._child() */
30612 +       di_write_lock_child(root);
30613 +
30614 +       au_cpup_attr_all(inode, /*force*/1);
30615 +
30616 +       if (unlikely(err))
30617 +               AuIOErr("refresh failed, ignored, %d\n", err);
30618 +}
30619 +
30620 +const struct super_operations aufs_sop = {
30621 +       .alloc_inode    = aufs_alloc_inode,
30622 +       .destroy_inode  = aufs_destroy_inode,
30623 +       .free_inode     = aufs_free_inode,
30624 +       /* always deleting, no clearing */
30625 +       .drop_inode     = generic_delete_inode,
30626 +       .show_options   = aufs_show_options,
30627 +       .statfs         = aufs_statfs,
30628 +       .put_super      = aufs_put_super,
30629 +       .sync_fs        = aufs_sync_fs
30630 +};
30631 +
30632 +/* ---------------------------------------------------------------------- */
30633 +
30634 +int au_alloc_root(struct super_block *sb)
30635 +{
30636 +       int err;
30637 +       struct inode *inode;
30638 +       struct dentry *root;
30639 +
30640 +       err = -ENOMEM;
30641 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30642 +       err = PTR_ERR(inode);
30643 +       if (IS_ERR(inode))
30644 +               goto out;
30645 +
30646 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30647 +       inode->i_fop = &aufs_dir_fop;
30648 +       inode->i_mode = S_IFDIR;
30649 +       set_nlink(inode, 2);
30650 +       unlock_new_inode(inode);
30651 +
30652 +       root = d_make_root(inode);
30653 +       if (unlikely(!root))
30654 +               goto out;
30655 +       err = PTR_ERR(root);
30656 +       if (IS_ERR(root))
30657 +               goto out;
30658 +
30659 +       err = au_di_init(root);
30660 +       if (!err) {
30661 +               sb->s_root = root;
30662 +               return 0; /* success */
30663 +       }
30664 +       dput(root);
30665 +
30666 +out:
30667 +       return err;
30668 +}
30669 +
30670 +/* ---------------------------------------------------------------------- */
30671 +
30672 +static void aufs_kill_sb(struct super_block *sb)
30673 +{
30674 +       struct au_sbinfo *sbinfo;
30675 +       struct dentry *root;
30676 +
30677 +       sbinfo = au_sbi(sb);
30678 +       if (!sbinfo)
30679 +               goto out;
30680 +
30681 +       au_sbilist_del(sb);
30682 +
30683 +       root = sb->s_root;
30684 +       if (root)
30685 +               aufs_write_lock(root);
30686 +       else
30687 +               __si_write_lock(sb);
30688 +
30689 +       au_fhsm_fin(sb);
30690 +       if (sbinfo->si_wbr_create_ops->fin)
30691 +               sbinfo->si_wbr_create_ops->fin(sb);
30692 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30693 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30694 +               au_remount_refresh(sb, /*do_idop*/0);
30695 +       }
30696 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30697 +               au_plink_put(sb, /*verbose*/1);
30698 +       au_xino_clr(sb);
30699 +       if (root)
30700 +               au_dr_opt_flush(sb);
30701 +
30702 +       if (root)
30703 +               aufs_write_unlock(root);
30704 +       else
30705 +               __si_write_unlock(sb);
30706 +
30707 +       sbinfo->si_sb = NULL;
30708 +       au_nwt_flush(&sbinfo->si_nowait);
30709 +
30710 +out:
30711 +       kill_anon_super(sb);
30712 +}
30713 +
30714 +struct file_system_type aufs_fs_type = {
30715 +       .name           = AUFS_FSTYPE,
30716 +       /* a race between rename and others */
30717 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30718 +                               /* untested */
30719 +                               /*| FS_ALLOW_IDMAP*/
30720 +                               ,
30721 +       .init_fs_context = aufs_fsctx_init,
30722 +       .parameters     = aufs_fsctx_paramspec,
30723 +       .kill_sb        = aufs_kill_sb,
30724 +       /* no need to __module_get() and module_put(). */
30725 +       .owner          = THIS_MODULE,
30726 +};
30727 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30728 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30729 +++ linux/fs/aufs/super.h       2022-11-05 23:02:18.969222617 +0100
30730 @@ -0,0 +1,592 @@
30731 +/* SPDX-License-Identifier: GPL-2.0 */
30732 +/*
30733 + * Copyright (C) 2005-2022 Junjiro R. Okajima
30734 + *
30735 + * This program is free software; you can redistribute it and/or modify
30736 + * it under the terms of the GNU General Public License as published by
30737 + * the Free Software Foundation; either version 2 of the License, or
30738 + * (at your option) any later version.
30739 + *
30740 + * This program is distributed in the hope that it will be useful,
30741 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30742 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30743 + * GNU General Public License for more details.
30744 + *
30745 + * You should have received a copy of the GNU General Public License
30746 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30747 + */
30748 +
30749 +/*
30750 + * super_block operations
30751 + */
30752 +
30753 +#ifndef __AUFS_SUPER_H__
30754 +#define __AUFS_SUPER_H__
30755 +
30756 +#ifdef __KERNEL__
30757 +
30758 +#include <linux/fs.h>
30759 +#include <linux/kobject.h>
30760 +#include "hbl.h"
30761 +#include "lcnt.h"
30762 +#include "rwsem.h"
30763 +#include "wkq.h"
30764 +
30765 +/* policies to select one among multiple writable branches */
30766 +struct au_wbr_copyup_operations {
30767 +       int (*copyup)(struct dentry *dentry);
30768 +};
30769 +
30770 +#define AuWbr_DIR      1               /* target is a dir */
30771 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30772 +
30773 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30774 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30775 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30776 +
30777 +struct au_wbr_create_operations {
30778 +       int (*create)(struct dentry *dentry, unsigned int flags);
30779 +       int (*init)(struct super_block *sb);
30780 +       int (*fin)(struct super_block *sb);
30781 +};
30782 +
30783 +struct au_wbr_mfs {
30784 +       struct mutex    mfs_lock; /* protect this structure */
30785 +       unsigned long   mfs_jiffy;
30786 +       unsigned long   mfs_expire;
30787 +       aufs_bindex_t   mfs_bindex;
30788 +
30789 +       unsigned long long      mfsrr_bytes;
30790 +       unsigned long long      mfsrr_watermark;
30791 +};
30792 +
30793 +#define AuPlink_NHASH 100
30794 +static inline int au_plink_hash(ino_t ino)
30795 +{
30796 +       return ino % AuPlink_NHASH;
30797 +}
30798 +
30799 +/* File-based Hierarchical Storage Management */
30800 +struct au_fhsm {
30801 +#ifdef CONFIG_AUFS_FHSM
30802 +       /* allow only one process who can receive the notification */
30803 +       spinlock_t              fhsm_spin;
30804 +       pid_t                   fhsm_pid;
30805 +       wait_queue_head_t       fhsm_wqh;
30806 +       atomic_t                fhsm_readable;
30807 +
30808 +       /* these are protected by si_rwsem */
30809 +       unsigned long           fhsm_expire;
30810 +       aufs_bindex_t           fhsm_bottom;
30811 +#endif
30812 +};
30813 +
30814 +struct au_branch;
30815 +struct au_sbinfo {
30816 +       /* nowait tasks in the system-wide workqueue */
30817 +       struct au_nowait_tasks  si_nowait;
30818 +
30819 +       /*
30820 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30821 +        * rwsem for au_sbinfo is necessary.
30822 +        */
30823 +       struct au_rwsem         si_rwsem;
30824 +
30825 +       /*
30826 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30827 +        * remount.
30828 +        */
30829 +       au_lcnt_t               si_ninodes, si_nfiles;
30830 +
30831 +       /* branch management */
30832 +       unsigned int            si_generation;
30833 +
30834 +       /* see AuSi_ flags */
30835 +       unsigned char           au_si_status;
30836 +
30837 +       aufs_bindex_t           si_bbot;
30838 +
30839 +       /* dirty trick to keep br_id plus */
30840 +       unsigned int            si_last_br_id :
30841 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30842 +       struct au_branch        **si_branch;
30843 +
30844 +       /* policy to select a writable branch */
30845 +       unsigned char           si_wbr_copyup;
30846 +       unsigned char           si_wbr_create;
30847 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30848 +       struct au_wbr_create_operations *si_wbr_create_ops;
30849 +
30850 +       /* round robin */
30851 +       atomic_t                si_wbr_rr_next;
30852 +
30853 +       /* most free space */
30854 +       struct au_wbr_mfs       si_wbr_mfs;
30855 +
30856 +       /* File-based Hierarchical Storage Management */
30857 +       struct au_fhsm          si_fhsm;
30858 +
30859 +       /* mount flags */
30860 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30861 +       unsigned int            si_mntflags;
30862 +
30863 +       /* external inode number (bitmap and translation table) */
30864 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30865 +
30866 +       struct file             *si_xib;
30867 +       struct mutex            si_xib_mtx; /* protect xib members */
30868 +       unsigned long           *si_xib_buf;
30869 +       unsigned long           si_xib_last_pindex;
30870 +       int                     si_xib_next_bit;
30871 +
30872 +       unsigned long           si_xino_jiffy;
30873 +       unsigned long           si_xino_expire;
30874 +       /* reserved for future use */
30875 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30876 +
30877 +#ifdef CONFIG_AUFS_EXPORT
30878 +       /* i_generation */
30879 +       /* todo: make xigen file an array to support many inode numbers */
30880 +       struct file             *si_xigen;
30881 +       atomic_t                si_xigen_next;
30882 +#endif
30883 +
30884 +       /* dirty trick to support atomic_open */
30885 +       struct hlist_bl_head    si_aopen;
30886 +
30887 +       /* vdir parameters */
30888 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30889 +       unsigned int            si_rdblk;       /* deblk size */
30890 +       unsigned int            si_rdhash;      /* hash size */
30891 +
30892 +       /*
30893 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30894 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30895 +        * future fsck.aufs or kernel thread will remove them later.
30896 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30897 +        */
30898 +       unsigned int            si_dirwh;
30899 +
30900 +       /* pseudo_link list */
30901 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30902 +       wait_queue_head_t       si_plink_wq;
30903 +       spinlock_t              si_plink_maint_lock;
30904 +       pid_t                   si_plink_maint_pid;
30905 +
30906 +       /* file list */
30907 +       struct hlist_bl_head    si_files;
30908 +
30909 +       /* with/without getattr, brother of sb->s_d_op */
30910 +       const struct inode_operations *si_iop_array;
30911 +
30912 +       /*
30913 +        * sysfs and lifetime management.
30914 +        * this is not a small structure and it may be a waste of memory in case
30915 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30916 +        * but using sysfs is majority.
30917 +        */
30918 +       struct kobject          si_kobj;
30919 +#ifdef CONFIG_DEBUG_FS
30920 +       struct dentry            *si_dbgaufs;
30921 +       struct dentry            *si_dbgaufs_plink;
30922 +       struct dentry            *si_dbgaufs_xib;
30923 +#ifdef CONFIG_AUFS_EXPORT
30924 +       struct dentry            *si_dbgaufs_xigen;
30925 +#endif
30926 +#endif
30927 +
30928 +#ifdef CONFIG_AUFS_SBILIST
30929 +       struct hlist_bl_node    si_list;
30930 +#endif
30931 +
30932 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30933 +       struct super_block      *si_sb;
30934 +};
30935 +
30936 +/* sbinfo status flags */
30937 +/*
30938 + * set true when refresh_dirs() failed at remount time.
30939 + * then try refreshing dirs at access time again.
30940 + * if it is false, refreshing dirs at access time is unnecessary
30941 + */
30942 +#define AuSi_FAILED_REFRESH_DIR        1
30943 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30944 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30945 +
30946 +#ifndef CONFIG_AUFS_FHSM
30947 +#undef AuSi_FHSM
30948 +#define AuSi_FHSM              0
30949 +#endif
30950 +
30951 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30952 +                                          unsigned int flag)
30953 +{
30954 +       AuRwMustAnyLock(&sbi->si_rwsem);
30955 +       return sbi->au_si_status & flag;
30956 +}
30957 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30958 +#define au_fset_si(sbinfo, name) do { \
30959 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30960 +       (sbinfo)->au_si_status |= AuSi_##name; \
30961 +} while (0)
30962 +#define au_fclr_si(sbinfo, name) do { \
30963 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30964 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30965 +} while (0)
30966 +
30967 +/* ---------------------------------------------------------------------- */
30968 +
30969 +/* policy to select one among writable branches */
30970 +#define AuWbrCopyup(sbinfo, ...) \
30971 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30972 +#define AuWbrCreate(sbinfo, ...) \
30973 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30974 +
30975 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30976 +#define AuLock_DW              1               /* write-lock dentry */
30977 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30978 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30979 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30980 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30981 +                                               /* except RENAME_EXCHANGE */
30982 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30983 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30984 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30985 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30986 +#define au_fset_lock(flags, name) \
30987 +       do { (flags) |= AuLock_##name; } while (0)
30988 +#define au_fclr_lock(flags, name) \
30989 +       do { (flags) &= ~AuLock_##name; } while (0)
30990 +
30991 +/* ---------------------------------------------------------------------- */
30992 +
30993 +/* super.c */
30994 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30995 +
30996 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30997 +                                          unsigned long long max, void *arg);
30998 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30999 +                    struct super_block *sb, void *arg);
31000 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
31001 +void au_iarray_free(struct inode **a, unsigned long long max);
31002 +
31003 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
31004 +extern const struct super_operations aufs_sop;
31005 +int au_alloc_root(struct super_block *sb);
31006 +extern struct file_system_type aufs_fs_type;
31007 +
31008 +/* sbinfo.c */
31009 +void au_si_free(struct kobject *kobj);
31010 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
31011 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
31012 +
31013 +unsigned int au_sigen_inc(struct super_block *sb);
31014 +aufs_bindex_t au_new_br_id(struct super_block *sb);
31015 +
31016 +int si_read_lock(struct super_block *sb, int flags);
31017 +int si_write_lock(struct super_block *sb, int flags);
31018 +int aufs_read_lock(struct dentry *dentry, int flags);
31019 +void aufs_read_unlock(struct dentry *dentry, int flags);
31020 +void aufs_write_lock(struct dentry *dentry);
31021 +void aufs_write_unlock(struct dentry *dentry);
31022 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
31023 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
31024 +
31025 +/* wbr_policy.c */
31026 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
31027 +extern struct au_wbr_create_operations au_wbr_create_ops[];
31028 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
31029 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
31030 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
31031 +
31032 +/* mvdown.c */
31033 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
31034 +
31035 +#ifdef CONFIG_AUFS_FHSM
31036 +/* fhsm.c */
31037 +
31038 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
31039 +{
31040 +       pid_t pid;
31041 +
31042 +       spin_lock(&fhsm->fhsm_spin);
31043 +       pid = fhsm->fhsm_pid;
31044 +       spin_unlock(&fhsm->fhsm_spin);
31045 +
31046 +       return pid;
31047 +}
31048 +
31049 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31050 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31051 +int au_fhsm_fd(struct super_block *sb, int oflags);
31052 +int au_fhsm_br_alloc(struct au_branch *br);
31053 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31054 +void au_fhsm_fin(struct super_block *sb);
31055 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31056 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31057 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31058 +#else
31059 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31060 +          int force)
31061 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31062 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31063 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31064 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31065 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31066 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31067 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31068 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31069 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31070 +#endif
31071 +
31072 +/* ---------------------------------------------------------------------- */
31073 +
31074 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31075 +{
31076 +       return sb->s_fs_info;
31077 +}
31078 +
31079 +/* ---------------------------------------------------------------------- */
31080 +
31081 +#ifdef CONFIG_AUFS_EXPORT
31082 +int au_test_nfsd(void);
31083 +void au_export_init(struct super_block *sb);
31084 +void au_xigen_inc(struct inode *inode);
31085 +int au_xigen_new(struct inode *inode);
31086 +int au_xigen_set(struct super_block *sb, struct path *path);
31087 +void au_xigen_clr(struct super_block *sb);
31088 +
31089 +static inline int au_busy_or_stale(void)
31090 +{
31091 +       if (!au_test_nfsd())
31092 +               return -EBUSY;
31093 +       return -ESTALE;
31094 +}
31095 +#else
31096 +AuStubInt0(au_test_nfsd, void)
31097 +AuStubVoid(au_export_init, struct super_block *sb)
31098 +AuStubVoid(au_xigen_inc, struct inode *inode)
31099 +AuStubInt0(au_xigen_new, struct inode *inode)
31100 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31101 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31102 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31103 +#endif /* CONFIG_AUFS_EXPORT */
31104 +
31105 +/* ---------------------------------------------------------------------- */
31106 +
31107 +#ifdef CONFIG_AUFS_SBILIST
31108 +/* module.c */
31109 +extern struct hlist_bl_head au_sbilist;
31110 +
31111 +static inline void au_sbilist_init(void)
31112 +{
31113 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31114 +}
31115 +
31116 +static inline void au_sbilist_add(struct super_block *sb)
31117 +{
31118 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31119 +}
31120 +
31121 +static inline void au_sbilist_del(struct super_block *sb)
31122 +{
31123 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31124 +}
31125 +
31126 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31127 +static inline void au_sbilist_lock(void)
31128 +{
31129 +       hlist_bl_lock(&au_sbilist);
31130 +}
31131 +
31132 +static inline void au_sbilist_unlock(void)
31133 +{
31134 +       hlist_bl_unlock(&au_sbilist);
31135 +}
31136 +#define AuGFP_SBILIST  GFP_ATOMIC
31137 +#else
31138 +AuStubVoid(au_sbilist_lock, void)
31139 +AuStubVoid(au_sbilist_unlock, void)
31140 +#define AuGFP_SBILIST  GFP_NOFS
31141 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31142 +#else
31143 +AuStubVoid(au_sbilist_init, void)
31144 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31145 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31146 +AuStubVoid(au_sbilist_lock, void)
31147 +AuStubVoid(au_sbilist_unlock, void)
31148 +#define AuGFP_SBILIST  GFP_NOFS
31149 +#endif
31150 +
31151 +/* ---------------------------------------------------------------------- */
31152 +
31153 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31154 +{
31155 +       /*
31156 +        * This function is a dynamic '__init' function actually,
31157 +        * so the tiny check for si_rwsem is unnecessary.
31158 +        */
31159 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31160 +#ifdef CONFIG_DEBUG_FS
31161 +       sbinfo->si_dbgaufs = NULL;
31162 +       sbinfo->si_dbgaufs_plink = NULL;
31163 +       sbinfo->si_dbgaufs_xib = NULL;
31164 +#ifdef CONFIG_AUFS_EXPORT
31165 +       sbinfo->si_dbgaufs_xigen = NULL;
31166 +#endif
31167 +#endif
31168 +}
31169 +
31170 +/* ---------------------------------------------------------------------- */
31171 +
31172 +/* current->atomic_flags */
31173 +/* this value should never corrupt the ones defined in linux/sched.h */
31174 +#define PFA_AUFS       0x10
31175 +
31176 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31177 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31178 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31179 +
31180 +static inline int si_pid_test(struct super_block *sb)
31181 +{
31182 +       return !!task_test_aufs(current);
31183 +}
31184 +
31185 +static inline void si_pid_clr(struct super_block *sb)
31186 +{
31187 +       AuDebugOn(!task_test_aufs(current));
31188 +       task_clear_aufs(current);
31189 +}
31190 +
31191 +static inline void si_pid_set(struct super_block *sb)
31192 +{
31193 +       AuDebugOn(task_test_aufs(current));
31194 +       task_set_aufs(current);
31195 +}
31196 +
31197 +/* ---------------------------------------------------------------------- */
31198 +
31199 +/* lock superblock. mainly for entry point functions */
31200 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31201 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31202 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31203 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31204 +/*
31205 +#define __si_read_trylock_nested(sb) \
31206 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31207 +#define __si_write_trylock_nested(sb) \
31208 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31209 +*/
31210 +
31211 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31212 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31213 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31214 +
31215 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31216 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31217 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31218 +
31219 +static inline void si_noflush_read_lock(struct super_block *sb)
31220 +{
31221 +       __si_read_lock(sb);
31222 +       si_pid_set(sb);
31223 +}
31224 +
31225 +static inline int si_noflush_read_trylock(struct super_block *sb)
31226 +{
31227 +       int locked;
31228 +
31229 +       locked = __si_read_trylock(sb);
31230 +       if (locked)
31231 +               si_pid_set(sb);
31232 +       return locked;
31233 +}
31234 +
31235 +static inline void si_noflush_write_lock(struct super_block *sb)
31236 +{
31237 +       __si_write_lock(sb);
31238 +       si_pid_set(sb);
31239 +}
31240 +
31241 +static inline int si_noflush_write_trylock(struct super_block *sb)
31242 +{
31243 +       int locked;
31244 +
31245 +       locked = __si_write_trylock(sb);
31246 +       if (locked)
31247 +               si_pid_set(sb);
31248 +       return locked;
31249 +}
31250 +
31251 +#if 0 /* reserved */
31252 +static inline int si_read_trylock(struct super_block *sb, int flags)
31253 +{
31254 +       if (au_ftest_lock(flags, FLUSH))
31255 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31256 +       return si_noflush_read_trylock(sb);
31257 +}
31258 +#endif
31259 +
31260 +static inline void si_read_unlock(struct super_block *sb)
31261 +{
31262 +       si_pid_clr(sb);
31263 +       __si_read_unlock(sb);
31264 +}
31265 +
31266 +#if 0 /* reserved */
31267 +static inline int si_write_trylock(struct super_block *sb, int flags)
31268 +{
31269 +       if (au_ftest_lock(flags, FLUSH))
31270 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31271 +       return si_noflush_write_trylock(sb);
31272 +}
31273 +#endif
31274 +
31275 +static inline void si_write_unlock(struct super_block *sb)
31276 +{
31277 +       si_pid_clr(sb);
31278 +       __si_write_unlock(sb);
31279 +}
31280 +
31281 +#if 0 /* reserved */
31282 +static inline void si_downgrade_lock(struct super_block *sb)
31283 +{
31284 +       __si_downgrade_lock(sb);
31285 +}
31286 +#endif
31287 +
31288 +/* ---------------------------------------------------------------------- */
31289 +
31290 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31291 +{
31292 +       SiMustAnyLock(sb);
31293 +       return au_sbi(sb)->si_bbot;
31294 +}
31295 +
31296 +static inline unsigned int au_mntflags(struct super_block *sb)
31297 +{
31298 +       SiMustAnyLock(sb);
31299 +       return au_sbi(sb)->si_mntflags;
31300 +}
31301 +
31302 +static inline unsigned int au_sigen(struct super_block *sb)
31303 +{
31304 +       SiMustAnyLock(sb);
31305 +       return au_sbi(sb)->si_generation;
31306 +}
31307 +
31308 +static inline struct au_branch *au_sbr(struct super_block *sb,
31309 +                                      aufs_bindex_t bindex)
31310 +{
31311 +       SiMustAnyLock(sb);
31312 +       return au_sbi(sb)->si_branch[0 + bindex];
31313 +}
31314 +
31315 +static inline loff_t au_xi_maxent(struct super_block *sb)
31316 +{
31317 +       SiMustAnyLock(sb);
31318 +       return au_sbi(sb)->si_ximaxent;
31319 +}
31320 +
31321 +#endif /* __KERNEL__ */
31322 +#endif /* __AUFS_SUPER_H__ */
31323 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31324 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31325 +++ linux/fs/aufs/sysaufs.c     2022-11-05 23:02:18.969222617 +0100
31326 @@ -0,0 +1,94 @@
31327 +// SPDX-License-Identifier: GPL-2.0
31328 +/*
31329 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31330 + *
31331 + * This program is free software; you can redistribute it and/or modify
31332 + * it under the terms of the GNU General Public License as published by
31333 + * the Free Software Foundation; either version 2 of the License, or
31334 + * (at your option) any later version.
31335 + *
31336 + * This program is distributed in the hope that it will be useful,
31337 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31338 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31339 + * GNU General Public License for more details.
31340 + *
31341 + * You should have received a copy of the GNU General Public License
31342 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31343 + */
31344 +
31345 +/*
31346 + * sysfs interface and lifetime management
31347 + * they are necessary regardless sysfs is disabled.
31348 + */
31349 +
31350 +#include <linux/random.h>
31351 +#include "aufs.h"
31352 +
31353 +unsigned long sysaufs_si_mask;
31354 +struct kset *sysaufs_kset;
31355 +
31356 +#define AuSiAttr(_name) { \
31357 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31358 +       .show   = sysaufs_si_##_name,                           \
31359 +}
31360 +
31361 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31362 +struct attribute *sysaufs_si_attrs[] = {
31363 +       &sysaufs_si_attr_xi_path.attr,
31364 +       NULL,
31365 +};
31366 +ATTRIBUTE_GROUPS(sysaufs_si);
31367 +
31368 +static const struct sysfs_ops au_sbi_ops = {
31369 +       .show   = sysaufs_si_show
31370 +};
31371 +
31372 +static struct kobj_type au_sbi_ktype = {
31373 +       .release        = au_si_free,
31374 +       .sysfs_ops      = &au_sbi_ops,
31375 +       .default_groups = sysaufs_si_groups
31376 +};
31377 +
31378 +/* ---------------------------------------------------------------------- */
31379 +
31380 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31381 +{
31382 +       int err;
31383 +
31384 +       sbinfo->si_kobj.kset = sysaufs_kset;
31385 +       /* cf. sysaufs_name() */
31386 +       err = kobject_init_and_add
31387 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31388 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31389 +
31390 +       return err;
31391 +}
31392 +
31393 +void sysaufs_fin(void)
31394 +{
31395 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31396 +       kset_unregister(sysaufs_kset);
31397 +}
31398 +
31399 +int __init sysaufs_init(void)
31400 +{
31401 +       int err;
31402 +
31403 +       do {
31404 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31405 +       } while (!sysaufs_si_mask);
31406 +
31407 +       err = -EINVAL;
31408 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31409 +       if (unlikely(!sysaufs_kset))
31410 +               goto out;
31411 +       err = PTR_ERR(sysaufs_kset);
31412 +       if (IS_ERR(sysaufs_kset))
31413 +               goto out;
31414 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31415 +       if (unlikely(err))
31416 +               kset_unregister(sysaufs_kset);
31417 +
31418 +out:
31419 +       return err;
31420 +}
31421 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31422 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31423 +++ linux/fs/aufs/sysaufs.h     2022-11-05 23:02:18.969222617 +0100
31424 @@ -0,0 +1,102 @@
31425 +/* SPDX-License-Identifier: GPL-2.0 */
31426 +/*
31427 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31428 + *
31429 + * This program is free software; you can redistribute it and/or modify
31430 + * it under the terms of the GNU General Public License as published by
31431 + * the Free Software Foundation; either version 2 of the License, or
31432 + * (at your option) any later version.
31433 + *
31434 + * This program is distributed in the hope that it will be useful,
31435 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31436 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31437 + * GNU General Public License for more details.
31438 + *
31439 + * You should have received a copy of the GNU General Public License
31440 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31441 + */
31442 +
31443 +/*
31444 + * sysfs interface and mount lifetime management
31445 + */
31446 +
31447 +#ifndef __SYSAUFS_H__
31448 +#define __SYSAUFS_H__
31449 +
31450 +#ifdef __KERNEL__
31451 +
31452 +#include <linux/sysfs.h>
31453 +#include "module.h"
31454 +
31455 +struct super_block;
31456 +struct au_sbinfo;
31457 +
31458 +struct sysaufs_si_attr {
31459 +       struct attribute attr;
31460 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31461 +};
31462 +
31463 +/* ---------------------------------------------------------------------- */
31464 +
31465 +/* sysaufs.c */
31466 +extern unsigned long sysaufs_si_mask;
31467 +extern struct kset *sysaufs_kset;
31468 +extern struct attribute *sysaufs_si_attrs[];
31469 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31470 +int __init sysaufs_init(void);
31471 +void sysaufs_fin(void);
31472 +
31473 +/* ---------------------------------------------------------------------- */
31474 +
31475 +/* some people doesn't like to show a pointer in kernel */
31476 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31477 +{
31478 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31479 +}
31480 +
31481 +#define SysaufsSiNamePrefix    "si_"
31482 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31483 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31484 +{
31485 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31486 +                sysaufs_si_id(sbinfo));
31487 +}
31488 +
31489 +struct au_branch;
31490 +#ifdef CONFIG_SYSFS
31491 +/* sysfs.c */
31492 +extern struct attribute_group *sysaufs_attr_group;
31493 +
31494 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31495 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31496 +                        char *buf);
31497 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31498 +#ifdef CONFIG_COMPAT
31499 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31500 +#endif
31501 +
31502 +void sysaufs_br_init(struct au_branch *br);
31503 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31504 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31505 +
31506 +#define sysaufs_brs_init()     do {} while (0)
31507 +
31508 +#else
31509 +#define sysaufs_attr_group     NULL
31510 +
31511 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31512 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31513 +       struct attribute *attr, char *buf)
31514 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31515 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31516 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31517 +
31518 +static inline void sysaufs_brs_init(void)
31519 +{
31520 +       sysaufs_brs = 0;
31521 +}
31522 +
31523 +#endif /* CONFIG_SYSFS */
31524 +
31525 +#endif /* __KERNEL__ */
31526 +#endif /* __SYSAUFS_H__ */
31527 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31528 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31529 +++ linux/fs/aufs/sysfs.c       2022-11-05 23:02:18.969222617 +0100
31530 @@ -0,0 +1,374 @@
31531 +// SPDX-License-Identifier: GPL-2.0
31532 +/*
31533 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31534 + *
31535 + * This program is free software; you can redistribute it and/or modify
31536 + * it under the terms of the GNU General Public License as published by
31537 + * the Free Software Foundation; either version 2 of the License, or
31538 + * (at your option) any later version.
31539 + *
31540 + * This program is distributed in the hope that it will be useful,
31541 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31542 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31543 + * GNU General Public License for more details.
31544 + *
31545 + * You should have received a copy of the GNU General Public License
31546 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31547 + */
31548 +
31549 +/*
31550 + * sysfs interface
31551 + */
31552 +
31553 +#include <linux/compat.h>
31554 +#include <linux/seq_file.h>
31555 +#include "aufs.h"
31556 +
31557 +#ifdef CONFIG_AUFS_FS_MODULE
31558 +/* this entry violates the "one line per file" policy of sysfs */
31559 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31560 +                          char *buf)
31561 +{
31562 +       ssize_t err;
31563 +       static char *conf =
31564 +/* this file is generated at compiling */
31565 +#include "conf.str"
31566 +               ;
31567 +
31568 +       err = snprintf(buf, PAGE_SIZE, conf);
31569 +       if (unlikely(err >= PAGE_SIZE))
31570 +               err = -EFBIG;
31571 +       return err;
31572 +}
31573 +
31574 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31575 +#endif
31576 +
31577 +static struct attribute *au_attr[] = {
31578 +#ifdef CONFIG_AUFS_FS_MODULE
31579 +       &au_config_attr.attr,
31580 +#endif
31581 +       NULL,   /* need to NULL terminate the list of attributes */
31582 +};
31583 +
31584 +static struct attribute_group sysaufs_attr_group_body = {
31585 +       .attrs = au_attr
31586 +};
31587 +
31588 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31589 +
31590 +/* ---------------------------------------------------------------------- */
31591 +
31592 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31593 +{
31594 +       int err;
31595 +
31596 +       SiMustAnyLock(sb);
31597 +
31598 +       err = 0;
31599 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31600 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31601 +               seq_putc(seq, '\n');
31602 +       }
31603 +       return err;
31604 +}
31605 +
31606 +/*
31607 + * the lifetime of branch is independent from the entry under sysfs.
31608 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31609 + * unlinked.
31610 + */
31611 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31612 +                        aufs_bindex_t bindex, int idx)
31613 +{
31614 +       int err;
31615 +       struct path path;
31616 +       struct dentry *root;
31617 +       struct au_branch *br;
31618 +       au_br_perm_str_t perm;
31619 +
31620 +       AuDbg("b%d\n", bindex);
31621 +
31622 +       err = 0;
31623 +       root = sb->s_root;
31624 +       di_read_lock_parent(root, !AuLock_IR);
31625 +       br = au_sbr(sb, bindex);
31626 +
31627 +       switch (idx) {
31628 +       case AuBrSysfs_BR:
31629 +               path.mnt = au_br_mnt(br);
31630 +               path.dentry = au_h_dptr(root, bindex);
31631 +               err = au_seq_path(seq, &path);
31632 +               if (!err) {
31633 +                       au_optstr_br_perm(&perm, br->br_perm);
31634 +                       seq_printf(seq, "=%s\n", perm.a);
31635 +               }
31636 +               break;
31637 +       case AuBrSysfs_BRID:
31638 +               seq_printf(seq, "%d\n", br->br_id);
31639 +               break;
31640 +       }
31641 +       di_read_unlock(root, !AuLock_IR);
31642 +       if (unlikely(err || seq_has_overflowed(seq)))
31643 +               err = -E2BIG;
31644 +
31645 +       return err;
31646 +}
31647 +
31648 +/* ---------------------------------------------------------------------- */
31649 +
31650 +static struct seq_file *au_seq(char *p, ssize_t len)
31651 +{
31652 +       struct seq_file *seq;
31653 +
31654 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31655 +       if (seq) {
31656 +               /* mutex_init(&seq.lock); */
31657 +               seq->buf = p;
31658 +               seq->size = len;
31659 +               return seq; /* success */
31660 +       }
31661 +
31662 +       seq = ERR_PTR(-ENOMEM);
31663 +       return seq;
31664 +}
31665 +
31666 +#define SysaufsBr_PREFIX       "br"
31667 +#define SysaufsBrid_PREFIX     "brid"
31668 +
31669 +/* todo: file size may exceed PAGE_SIZE */
31670 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31671 +                       char *buf)
31672 +{
31673 +       ssize_t err;
31674 +       int idx;
31675 +       long l;
31676 +       aufs_bindex_t bbot;
31677 +       struct au_sbinfo *sbinfo;
31678 +       struct super_block *sb;
31679 +       struct seq_file *seq;
31680 +       char *name;
31681 +       struct attribute **cattr;
31682 +
31683 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31684 +       sb = sbinfo->si_sb;
31685 +
31686 +       /*
31687 +        * prevent a race condition between sysfs and aufs.
31688 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31689 +        * prohibits maintaining the sysfs entries.
31690 +        * hew we acquire read lock after sysfs_get_active_two().
31691 +        * on the other hand, the remount process may maintain the sysfs/aufs
31692 +        * entries after acquiring write lock.
31693 +        * it can cause a deadlock.
31694 +        * simply we gave up processing read here.
31695 +        */
31696 +       err = -EBUSY;
31697 +       if (unlikely(!si_noflush_read_trylock(sb)))
31698 +               goto out;
31699 +
31700 +       seq = au_seq(buf, PAGE_SIZE);
31701 +       err = PTR_ERR(seq);
31702 +       if (IS_ERR(seq))
31703 +               goto out_unlock;
31704 +
31705 +       name = (void *)attr->name;
31706 +       cattr = sysaufs_si_attrs;
31707 +       while (*cattr) {
31708 +               if (!strcmp(name, (*cattr)->name)) {
31709 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31710 +                               ->show(seq, sb);
31711 +                       goto out_seq;
31712 +               }
31713 +               cattr++;
31714 +       }
31715 +
31716 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31717 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31718 +               idx = AuBrSysfs_BRID;
31719 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31720 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31721 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31722 +               idx = AuBrSysfs_BR;
31723 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31724 +       } else
31725 +                 BUG();
31726 +
31727 +       err = kstrtol(name, 10, &l);
31728 +       if (!err) {
31729 +               bbot = au_sbbot(sb);
31730 +               if (l <= bbot)
31731 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31732 +               else
31733 +                       err = -ENOENT;
31734 +       }
31735 +
31736 +out_seq:
31737 +       if (!err) {
31738 +               err = seq->count;
31739 +               /* sysfs limit */
31740 +               if (unlikely(err == PAGE_SIZE))
31741 +                       err = -EFBIG;
31742 +       }
31743 +       au_kfree_rcu(seq);
31744 +out_unlock:
31745 +       si_read_unlock(sb);
31746 +out:
31747 +       return err;
31748 +}
31749 +
31750 +/* ---------------------------------------------------------------------- */
31751 +
31752 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31753 +{
31754 +       int err;
31755 +       int16_t brid;
31756 +       aufs_bindex_t bindex, bbot;
31757 +       size_t sz;
31758 +       char *buf;
31759 +       struct seq_file *seq;
31760 +       struct au_branch *br;
31761 +
31762 +       si_read_lock(sb, AuLock_FLUSH);
31763 +       bbot = au_sbbot(sb);
31764 +       err = bbot + 1;
31765 +       if (!arg)
31766 +               goto out;
31767 +
31768 +       err = -ENOMEM;
31769 +       buf = (void *)__get_free_page(GFP_NOFS);
31770 +       if (unlikely(!buf))
31771 +               goto out;
31772 +
31773 +       seq = au_seq(buf, PAGE_SIZE);
31774 +       err = PTR_ERR(seq);
31775 +       if (IS_ERR(seq))
31776 +               goto out_buf;
31777 +
31778 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31779 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31780 +               /* VERIFY_WRITE */
31781 +               err = !access_ok(arg, sizeof(*arg));
31782 +               if (unlikely(err))
31783 +                       break;
31784 +
31785 +               br = au_sbr(sb, bindex);
31786 +               brid = br->br_id;
31787 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31788 +               err = __put_user(brid, &arg->id);
31789 +               if (unlikely(err))
31790 +                       break;
31791 +
31792 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31793 +               err = __put_user(br->br_perm, &arg->perm);
31794 +               if (unlikely(err))
31795 +                       break;
31796 +
31797 +               err = au_seq_path(seq, &br->br_path);
31798 +               if (unlikely(err))
31799 +                       break;
31800 +               seq_putc(seq, '\0');
31801 +               if (!seq_has_overflowed(seq)) {
31802 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31803 +                       seq->count = 0;
31804 +                       if (unlikely(err))
31805 +                               break;
31806 +               } else {
31807 +                       err = -E2BIG;
31808 +                       goto out_seq;
31809 +               }
31810 +       }
31811 +       if (unlikely(err))
31812 +               err = -EFAULT;
31813 +
31814 +out_seq:
31815 +       au_kfree_rcu(seq);
31816 +out_buf:
31817 +       free_page((unsigned long)buf);
31818 +out:
31819 +       si_read_unlock(sb);
31820 +       return err;
31821 +}
31822 +
31823 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31824 +{
31825 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31826 +}
31827 +
31828 +#ifdef CONFIG_COMPAT
31829 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31830 +{
31831 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31832 +}
31833 +#endif
31834 +
31835 +/* ---------------------------------------------------------------------- */
31836 +
31837 +void sysaufs_br_init(struct au_branch *br)
31838 +{
31839 +       int i;
31840 +       struct au_brsysfs *br_sysfs;
31841 +       struct attribute *attr;
31842 +
31843 +       br_sysfs = br->br_sysfs;
31844 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31845 +               attr = &br_sysfs->attr;
31846 +               sysfs_attr_init(attr);
31847 +               attr->name = br_sysfs->name;
31848 +               attr->mode = 0444;
31849 +               br_sysfs++;
31850 +       }
31851 +}
31852 +
31853 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31854 +{
31855 +       struct au_branch *br;
31856 +       struct kobject *kobj;
31857 +       struct au_brsysfs *br_sysfs;
31858 +       int i;
31859 +       aufs_bindex_t bbot;
31860 +
31861 +       if (!sysaufs_brs)
31862 +               return;
31863 +
31864 +       kobj = &au_sbi(sb)->si_kobj;
31865 +       bbot = au_sbbot(sb);
31866 +       for (; bindex <= bbot; bindex++) {
31867 +               br = au_sbr(sb, bindex);
31868 +               br_sysfs = br->br_sysfs;
31869 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31870 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31871 +                       br_sysfs++;
31872 +               }
31873 +       }
31874 +}
31875 +
31876 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31877 +{
31878 +       int err, i;
31879 +       aufs_bindex_t bbot;
31880 +       struct kobject *kobj;
31881 +       struct au_branch *br;
31882 +       struct au_brsysfs *br_sysfs;
31883 +
31884 +       if (!sysaufs_brs)
31885 +               return;
31886 +
31887 +       kobj = &au_sbi(sb)->si_kobj;
31888 +       bbot = au_sbbot(sb);
31889 +       for (; bindex <= bbot; bindex++) {
31890 +               br = au_sbr(sb, bindex);
31891 +               br_sysfs = br->br_sysfs;
31892 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31893 +                        SysaufsBr_PREFIX "%d", bindex);
31894 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31895 +                        SysaufsBrid_PREFIX "%d", bindex);
31896 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31897 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31898 +                       if (unlikely(err))
31899 +                               pr_warn("failed %s under sysfs(%d)\n",
31900 +                                       br_sysfs->name, err);
31901 +                       br_sysfs++;
31902 +               }
31903 +       }
31904 +}
31905 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31906 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31907 +++ linux/fs/aufs/sysrq.c       2022-11-05 23:02:18.969222617 +0100
31908 @@ -0,0 +1,149 @@
31909 +// SPDX-License-Identifier: GPL-2.0
31910 +/*
31911 + * Copyright (C) 2005-2022 Junjiro R. Okajima
31912 + *
31913 + * This program is free software; you can redistribute it and/or modify
31914 + * it under the terms of the GNU General Public License as published by
31915 + * the Free Software Foundation; either version 2 of the License, or
31916 + * (at your option) any later version.
31917 + *
31918 + * This program is distributed in the hope that it will be useful,
31919 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31920 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31921 + * GNU General Public License for more details.
31922 + *
31923 + * You should have received a copy of the GNU General Public License
31924 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31925 + */
31926 +
31927 +/*
31928 + * magic sysrq handler
31929 + */
31930 +
31931 +/* #include <linux/sysrq.h> */
31932 +#include <linux/writeback.h>
31933 +#include "aufs.h"
31934 +
31935 +/* ---------------------------------------------------------------------- */
31936 +
31937 +static void sysrq_sb(struct super_block *sb)
31938 +{
31939 +       char *plevel;
31940 +       struct au_sbinfo *sbinfo;
31941 +       struct file *file;
31942 +       struct hlist_bl_head *files;
31943 +       struct hlist_bl_node *pos;
31944 +       struct au_finfo *finfo;
31945 +       struct inode *i;
31946 +
31947 +       plevel = au_plevel;
31948 +       au_plevel = KERN_WARNING;
31949 +
31950 +       /* since we define pr_fmt, call printk directly */
31951 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31952 +
31953 +       sbinfo = au_sbi(sb);
31954 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31955 +       pr("superblock\n");
31956 +       au_dpri_sb(sb);
31957 +
31958 +#if 0 /* reserved */
31959 +       do {
31960 +               int err, i, j, ndentry;
31961 +               struct au_dcsub_pages dpages;
31962 +               struct au_dpage *dpage;
31963 +
31964 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31965 +               if (unlikely(err))
31966 +                       break;
31967 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31968 +               if (!err)
31969 +                       for (i = 0; i < dpages.ndpage; i++) {
31970 +                               dpage = dpages.dpages + i;
31971 +                               ndentry = dpage->ndentry;
31972 +                               for (j = 0; j < ndentry; j++)
31973 +                                       au_dpri_dentry(dpage->dentries[j]);
31974 +                       }
31975 +               au_dpages_free(&dpages);
31976 +       } while (0);
31977 +#endif
31978 +
31979 +       pr("isolated inode\n");
31980 +       spin_lock(&sb->s_inode_list_lock);
31981 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31982 +               spin_lock(&i->i_lock);
31983 +               if (hlist_empty(&i->i_dentry))
31984 +                       au_dpri_inode(i);
31985 +               spin_unlock(&i->i_lock);
31986 +       }
31987 +       spin_unlock(&sb->s_inode_list_lock);
31988 +
31989 +       pr("files\n");
31990 +       files = &au_sbi(sb)->si_files;
31991 +       hlist_bl_lock(files);
31992 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31993 +               umode_t mode;
31994 +
31995 +               file = finfo->fi_file;
31996 +               mode = file_inode(file)->i_mode;
31997 +               if (!special_file(mode))
31998 +                       au_dpri_file(file);
31999 +       }
32000 +       hlist_bl_unlock(files);
32001 +       pr("done\n");
32002 +
32003 +#undef pr
32004 +       au_plevel = plevel;
32005 +}
32006 +
32007 +/* ---------------------------------------------------------------------- */
32008 +
32009 +/* module parameter */
32010 +static char *aufs_sysrq_key = "a";
32011 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
32012 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
32013 +
32014 +static void au_sysrq(int key __maybe_unused)
32015 +{
32016 +       struct au_sbinfo *sbinfo;
32017 +       struct hlist_bl_node *pos;
32018 +
32019 +       lockdep_off();
32020 +       au_sbilist_lock();
32021 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
32022 +               sysrq_sb(sbinfo->si_sb);
32023 +       au_sbilist_unlock();
32024 +       lockdep_on();
32025 +}
32026 +
32027 +static struct sysrq_key_op au_sysrq_op = {
32028 +       .handler        = au_sysrq,
32029 +       .help_msg       = "Aufs",
32030 +       .action_msg     = "Aufs",
32031 +       .enable_mask    = SYSRQ_ENABLE_DUMP
32032 +};
32033 +
32034 +/* ---------------------------------------------------------------------- */
32035 +
32036 +int __init au_sysrq_init(void)
32037 +{
32038 +       int err;
32039 +       char key;
32040 +
32041 +       err = -1;
32042 +       key = *aufs_sysrq_key;
32043 +       if ('a' <= key && key <= 'z')
32044 +               err = register_sysrq_key(key, &au_sysrq_op);
32045 +       if (unlikely(err))
32046 +               pr_err("err %d, sysrq=%c\n", err, key);
32047 +       return err;
32048 +}
32049 +
32050 +void au_sysrq_fin(void)
32051 +{
32052 +       int err;
32053 +
32054 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32055 +       if (unlikely(err))
32056 +               pr_err("err %d (ignored)\n", err);
32057 +}
32058 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32059 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32060 +++ linux/fs/aufs/vdir.c        2022-12-17 09:21:34.799855195 +0100
32061 @@ -0,0 +1,896 @@
32062 +// SPDX-License-Identifier: GPL-2.0
32063 +/*
32064 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32065 + *
32066 + * This program is free software; you can redistribute it and/or modify
32067 + * it under the terms of the GNU General Public License as published by
32068 + * the Free Software Foundation; either version 2 of the License, or
32069 + * (at your option) any later version.
32070 + *
32071 + * This program is distributed in the hope that it will be useful,
32072 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32073 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32074 + * GNU General Public License for more details.
32075 + *
32076 + * You should have received a copy of the GNU General Public License
32077 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32078 + */
32079 +
32080 +/*
32081 + * virtual or vertical directory
32082 + */
32083 +
32084 +#include <linux/iversion.h>
32085 +#include "aufs.h"
32086 +
32087 +static unsigned int calc_size(int nlen)
32088 +{
32089 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32090 +}
32091 +
32092 +static int set_deblk_end(union au_vdir_deblk_p *p,
32093 +                        union au_vdir_deblk_p *deblk_end)
32094 +{
32095 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32096 +               p->de->de_str.len = 0;
32097 +               /* smp_mb(); */
32098 +               return 0;
32099 +       }
32100 +       return -1; /* error */
32101 +}
32102 +
32103 +/* returns true or false */
32104 +static int is_deblk_end(union au_vdir_deblk_p *p,
32105 +                       union au_vdir_deblk_p *deblk_end)
32106 +{
32107 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32108 +               return !p->de->de_str.len;
32109 +       return 1;
32110 +}
32111 +
32112 +static unsigned char *last_deblk(struct au_vdir *vdir)
32113 +{
32114 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32115 +}
32116 +
32117 +/* ---------------------------------------------------------------------- */
32118 +
32119 +/* estimate the appropriate size for name hash table */
32120 +unsigned int au_rdhash_est(loff_t sz)
32121 +{
32122 +       unsigned int n;
32123 +
32124 +       n = UINT_MAX;
32125 +       sz >>= 10;
32126 +       if (sz < n)
32127 +               n = sz;
32128 +       if (sz < AUFS_RDHASH_DEF)
32129 +               n = AUFS_RDHASH_DEF;
32130 +       /* pr_info("n %u\n", n); */
32131 +       return n;
32132 +}
32133 +
32134 +/*
32135 + * the allocated memory has to be freed by
32136 + * au_nhash_wh_free() or au_nhash_de_free().
32137 + */
32138 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32139 +{
32140 +       struct hlist_head *head;
32141 +       unsigned int u;
32142 +       size_t sz;
32143 +
32144 +       sz = sizeof(*nhash->nh_head) * num_hash;
32145 +       head = kmalloc(sz, gfp);
32146 +       if (head) {
32147 +               nhash->nh_num = num_hash;
32148 +               nhash->nh_head = head;
32149 +               for (u = 0; u < num_hash; u++)
32150 +                       INIT_HLIST_HEAD(head++);
32151 +               return 0; /* success */
32152 +       }
32153 +
32154 +       return -ENOMEM;
32155 +}
32156 +
32157 +static void nhash_count(struct hlist_head *head)
32158 +{
32159 +#if 0 /* debugging */
32160 +       unsigned long n;
32161 +       struct hlist_node *pos;
32162 +
32163 +       n = 0;
32164 +       hlist_for_each(pos, head)
32165 +               n++;
32166 +       pr_info("%lu\n", n);
32167 +#endif
32168 +}
32169 +
32170 +static void au_nhash_wh_do_free(struct hlist_head *head)
32171 +{
32172 +       struct au_vdir_wh *pos;
32173 +       struct hlist_node *node;
32174 +
32175 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32176 +               au_kfree_rcu(pos);
32177 +}
32178 +
32179 +static void au_nhash_de_do_free(struct hlist_head *head)
32180 +{
32181 +       struct au_vdir_dehstr *pos;
32182 +       struct hlist_node *node;
32183 +
32184 +       hlist_for_each_entry_safe(pos, node, head, hash)
32185 +               au_cache_free_vdir_dehstr(pos);
32186 +}
32187 +
32188 +static void au_nhash_do_free(struct au_nhash *nhash,
32189 +                            void (*free)(struct hlist_head *head))
32190 +{
32191 +       unsigned int n;
32192 +       struct hlist_head *head;
32193 +
32194 +       n = nhash->nh_num;
32195 +       if (!n)
32196 +               return;
32197 +
32198 +       head = nhash->nh_head;
32199 +       while (n-- > 0) {
32200 +               nhash_count(head);
32201 +               free(head++);
32202 +       }
32203 +       au_kfree_try_rcu(nhash->nh_head);
32204 +}
32205 +
32206 +void au_nhash_wh_free(struct au_nhash *whlist)
32207 +{
32208 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32209 +}
32210 +
32211 +static void au_nhash_de_free(struct au_nhash *delist)
32212 +{
32213 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32214 +}
32215 +
32216 +/* ---------------------------------------------------------------------- */
32217 +
32218 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32219 +                           int limit)
32220 +{
32221 +       int num;
32222 +       unsigned int u, n;
32223 +       struct hlist_head *head;
32224 +       struct au_vdir_wh *pos;
32225 +
32226 +       num = 0;
32227 +       n = whlist->nh_num;
32228 +       head = whlist->nh_head;
32229 +       for (u = 0; u < n; u++, head++)
32230 +               hlist_for_each_entry(pos, head, wh_hash)
32231 +                       if (pos->wh_bindex == btgt && ++num > limit)
32232 +                               return 1;
32233 +       return 0;
32234 +}
32235 +
32236 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32237 +                                      unsigned char *name,
32238 +                                      unsigned int len)
32239 +{
32240 +       unsigned int v;
32241 +       /* const unsigned int magic_bit = 12; */
32242 +
32243 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32244 +
32245 +       v = 0;
32246 +       if (len > 8)
32247 +               len = 8;
32248 +       while (len--)
32249 +               v += *name++;
32250 +       /* v = hash_long(v, magic_bit); */
32251 +       v %= nhash->nh_num;
32252 +       return nhash->nh_head + v;
32253 +}
32254 +
32255 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32256 +                             int nlen)
32257 +{
32258 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32259 +}
32260 +
32261 +/* returns found or not */
32262 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32263 +{
32264 +       struct hlist_head *head;
32265 +       struct au_vdir_wh *pos;
32266 +       struct au_vdir_destr *str;
32267 +
32268 +       head = au_name_hash(whlist, name, nlen);
32269 +       hlist_for_each_entry(pos, head, wh_hash) {
32270 +               str = &pos->wh_str;
32271 +               AuDbg("%.*s\n", str->len, str->name);
32272 +               if (au_nhash_test_name(str, name, nlen))
32273 +                       return 1;
32274 +       }
32275 +       return 0;
32276 +}
32277 +
32278 +/* returns found(true) or not */
32279 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32280 +{
32281 +       struct hlist_head *head;
32282 +       struct au_vdir_dehstr *pos;
32283 +       struct au_vdir_destr *str;
32284 +
32285 +       head = au_name_hash(delist, name, nlen);
32286 +       hlist_for_each_entry(pos, head, hash) {
32287 +               str = pos->str;
32288 +               AuDbg("%.*s\n", str->len, str->name);
32289 +               if (au_nhash_test_name(str, name, nlen))
32290 +                       return 1;
32291 +       }
32292 +       return 0;
32293 +}
32294 +
32295 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32296 +                           unsigned char d_type)
32297 +{
32298 +#ifdef CONFIG_AUFS_SHWH
32299 +       wh->wh_ino = ino;
32300 +       wh->wh_type = d_type;
32301 +#endif
32302 +}
32303 +
32304 +/* ---------------------------------------------------------------------- */
32305 +
32306 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32307 +                      unsigned int d_type, aufs_bindex_t bindex,
32308 +                      unsigned char shwh)
32309 +{
32310 +       int err;
32311 +       struct au_vdir_destr *str;
32312 +       struct au_vdir_wh *wh;
32313 +
32314 +       AuDbg("%.*s\n", nlen, name);
32315 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32316 +
32317 +       err = -ENOMEM;
32318 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32319 +       if (unlikely(!wh))
32320 +               goto out;
32321 +
32322 +       err = 0;
32323 +       wh->wh_bindex = bindex;
32324 +       if (shwh)
32325 +               au_shwh_init_wh(wh, ino, d_type);
32326 +       str = &wh->wh_str;
32327 +       str->len = nlen;
32328 +       memcpy(str->name, name, nlen);
32329 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32330 +       /* smp_mb(); */
32331 +
32332 +out:
32333 +       return err;
32334 +}
32335 +
32336 +static int append_deblk(struct au_vdir *vdir)
32337 +{
32338 +       int err;
32339 +       unsigned long ul;
32340 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32341 +       union au_vdir_deblk_p p, deblk_end;
32342 +       unsigned char **o;
32343 +
32344 +       err = -ENOMEM;
32345 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32346 +                       GFP_NOFS, /*may_shrink*/0);
32347 +       if (unlikely(!o))
32348 +               goto out;
32349 +
32350 +       vdir->vd_deblk = o;
32351 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32352 +       if (p.deblk) {
32353 +               ul = vdir->vd_nblk++;
32354 +               vdir->vd_deblk[ul] = p.deblk;
32355 +               vdir->vd_last.ul = ul;
32356 +               vdir->vd_last.p.deblk = p.deblk;
32357 +               deblk_end.deblk = p.deblk + deblk_sz;
32358 +               err = set_deblk_end(&p, &deblk_end);
32359 +       }
32360 +
32361 +out:
32362 +       return err;
32363 +}
32364 +
32365 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32366 +                    unsigned int d_type, struct au_nhash *delist)
32367 +{
32368 +       int err;
32369 +       unsigned int sz;
32370 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32371 +       union au_vdir_deblk_p p, *room, deblk_end;
32372 +       struct au_vdir_dehstr *dehstr;
32373 +
32374 +       p.deblk = last_deblk(vdir);
32375 +       deblk_end.deblk = p.deblk + deblk_sz;
32376 +       room = &vdir->vd_last.p;
32377 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32378 +                 || !is_deblk_end(room, &deblk_end));
32379 +
32380 +       sz = calc_size(nlen);
32381 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32382 +               err = append_deblk(vdir);
32383 +               if (unlikely(err))
32384 +                       goto out;
32385 +
32386 +               p.deblk = last_deblk(vdir);
32387 +               deblk_end.deblk = p.deblk + deblk_sz;
32388 +               /* smp_mb(); */
32389 +               AuDebugOn(room->deblk != p.deblk);
32390 +       }
32391 +
32392 +       err = -ENOMEM;
32393 +       dehstr = au_cache_alloc_vdir_dehstr();
32394 +       if (unlikely(!dehstr))
32395 +               goto out;
32396 +
32397 +       dehstr->str = &room->de->de_str;
32398 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32399 +       room->de->de_ino = ino;
32400 +       room->de->de_type = d_type;
32401 +       room->de->de_str.len = nlen;
32402 +       memcpy(room->de->de_str.name, name, nlen);
32403 +
32404 +       err = 0;
32405 +       room->deblk += sz;
32406 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32407 +               err = append_deblk(vdir);
32408 +       /* smp_mb(); */
32409 +
32410 +out:
32411 +       return err;
32412 +}
32413 +
32414 +/* ---------------------------------------------------------------------- */
32415 +
32416 +void au_vdir_free(struct au_vdir *vdir)
32417 +{
32418 +       unsigned char **deblk;
32419 +
32420 +       deblk = vdir->vd_deblk;
32421 +       while (vdir->vd_nblk--)
32422 +               au_kfree_try_rcu(*deblk++);
32423 +       au_kfree_try_rcu(vdir->vd_deblk);
32424 +       au_cache_free_vdir(vdir);
32425 +}
32426 +
32427 +static struct au_vdir *alloc_vdir(struct file *file)
32428 +{
32429 +       struct au_vdir *vdir;
32430 +       struct super_block *sb;
32431 +       int err;
32432 +
32433 +       sb = file->f_path.dentry->d_sb;
32434 +       SiMustAnyLock(sb);
32435 +
32436 +       err = -ENOMEM;
32437 +       vdir = au_cache_alloc_vdir();
32438 +       if (unlikely(!vdir))
32439 +               goto out;
32440 +
32441 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32442 +       if (unlikely(!vdir->vd_deblk))
32443 +               goto out_free;
32444 +
32445 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32446 +       if (!vdir->vd_deblk_sz) {
32447 +               /* estimate the appropriate size for deblk */
32448 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32449 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32450 +       }
32451 +       vdir->vd_nblk = 0;
32452 +       vdir->vd_version = 0;
32453 +       vdir->vd_jiffy = 0;
32454 +       err = append_deblk(vdir);
32455 +       if (!err)
32456 +               return vdir; /* success */
32457 +
32458 +       au_kfree_try_rcu(vdir->vd_deblk);
32459 +
32460 +out_free:
32461 +       au_cache_free_vdir(vdir);
32462 +out:
32463 +       vdir = ERR_PTR(err);
32464 +       return vdir;
32465 +}
32466 +
32467 +static int reinit_vdir(struct au_vdir *vdir)
32468 +{
32469 +       int err;
32470 +       union au_vdir_deblk_p p, deblk_end;
32471 +
32472 +       while (vdir->vd_nblk > 1) {
32473 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32474 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32475 +               vdir->vd_nblk--;
32476 +       }
32477 +       p.deblk = vdir->vd_deblk[0];
32478 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32479 +       err = set_deblk_end(&p, &deblk_end);
32480 +       /* keep vd_dblk_sz */
32481 +       vdir->vd_last.ul = 0;
32482 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32483 +       vdir->vd_version = 0;
32484 +       vdir->vd_jiffy = 0;
32485 +       /* smp_mb(); */
32486 +       return err;
32487 +}
32488 +
32489 +/* ---------------------------------------------------------------------- */
32490 +
32491 +#define AuFillVdir_CALLED      1
32492 +#define AuFillVdir_WHABLE      (1 << 1)
32493 +#define AuFillVdir_SHWH                (1 << 2)
32494 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32495 +#define au_fset_fillvdir(flags, name) \
32496 +       do { (flags) |= AuFillVdir_##name; } while (0)
32497 +#define au_fclr_fillvdir(flags, name) \
32498 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32499 +
32500 +#ifndef CONFIG_AUFS_SHWH
32501 +#undef AuFillVdir_SHWH
32502 +#define AuFillVdir_SHWH                0
32503 +#endif
32504 +
32505 +struct fillvdir_arg {
32506 +       struct dir_context      ctx;
32507 +       struct file             *file;
32508 +       struct au_vdir          *vdir;
32509 +       struct au_nhash         delist;
32510 +       struct au_nhash         whlist;
32511 +       aufs_bindex_t           bindex;
32512 +       unsigned int            flags;
32513 +       int                     err;
32514 +};
32515 +
32516 +static bool fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32517 +                   loff_t offset __maybe_unused, u64 h_ino,
32518 +                   unsigned int d_type)
32519 +{
32520 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32521 +       char *name = (void *)__name;
32522 +       struct super_block *sb;
32523 +       ino_t ino;
32524 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32525 +
32526 +       arg->err = 0;
32527 +       sb = arg->file->f_path.dentry->d_sb;
32528 +       au_fset_fillvdir(arg->flags, CALLED);
32529 +       /* smp_mb(); */
32530 +       if (nlen <= AUFS_WH_PFX_LEN
32531 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32532 +               if (test_known(&arg->delist, name, nlen)
32533 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32534 +                       goto out; /* already exists or whiteouted */
32535 +
32536 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32537 +               if (!arg->err) {
32538 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32539 +                               d_type = DT_UNKNOWN;
32540 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32541 +                                            d_type, &arg->delist);
32542 +               }
32543 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32544 +               name += AUFS_WH_PFX_LEN;
32545 +               nlen -= AUFS_WH_PFX_LEN;
32546 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32547 +                       goto out; /* already whiteouted */
32548 +
32549 +               ino = 0; /* just to suppress a warning */
32550 +               if (shwh)
32551 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32552 +                                            &ino);
32553 +               if (!arg->err) {
32554 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32555 +                               d_type = DT_UNKNOWN;
32556 +                       arg->err = au_nhash_append_wh
32557 +                               (&arg->whlist, name, nlen, ino, d_type,
32558 +                                arg->bindex, shwh);
32559 +               }
32560 +       }
32561 +
32562 +out:
32563 +       if (!arg->err)
32564 +               arg->vdir->vd_jiffy = jiffies;
32565 +       /* smp_mb(); */
32566 +       AuTraceErr(arg->err);
32567 +       return !arg->err;
32568 +}
32569 +
32570 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32571 +                         struct au_nhash *whlist, struct au_nhash *delist)
32572 +{
32573 +#ifdef CONFIG_AUFS_SHWH
32574 +       int err;
32575 +       unsigned int nh, u;
32576 +       struct hlist_head *head;
32577 +       struct au_vdir_wh *pos;
32578 +       struct hlist_node *n;
32579 +       char *p, *o;
32580 +       struct au_vdir_destr *destr;
32581 +
32582 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32583 +
32584 +       err = -ENOMEM;
32585 +       o = p = (void *)__get_free_page(GFP_NOFS);
32586 +       if (unlikely(!p))
32587 +               goto out;
32588 +
32589 +       err = 0;
32590 +       nh = whlist->nh_num;
32591 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32592 +       p += AUFS_WH_PFX_LEN;
32593 +       for (u = 0; u < nh; u++) {
32594 +               head = whlist->nh_head + u;
32595 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32596 +                       destr = &pos->wh_str;
32597 +                       memcpy(p, destr->name, destr->len);
32598 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32599 +                                       pos->wh_ino, pos->wh_type, delist);
32600 +                       if (unlikely(err))
32601 +                               break;
32602 +               }
32603 +       }
32604 +
32605 +       free_page((unsigned long)o);
32606 +
32607 +out:
32608 +       AuTraceErr(err);
32609 +       return err;
32610 +#else
32611 +       return 0;
32612 +#endif
32613 +}
32614 +
32615 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32616 +{
32617 +       int err;
32618 +       unsigned int rdhash;
32619 +       loff_t offset;
32620 +       aufs_bindex_t bbot, bindex, btop;
32621 +       unsigned char shwh;
32622 +       struct file *hf, *file;
32623 +       struct super_block *sb;
32624 +
32625 +       file = arg->file;
32626 +       sb = file->f_path.dentry->d_sb;
32627 +       SiMustAnyLock(sb);
32628 +
32629 +       rdhash = au_sbi(sb)->si_rdhash;
32630 +       if (!rdhash)
32631 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32632 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32633 +       if (unlikely(err))
32634 +               goto out;
32635 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32636 +       if (unlikely(err))
32637 +               goto out_delist;
32638 +
32639 +       err = 0;
32640 +       arg->flags = 0;
32641 +       shwh = 0;
32642 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32643 +               shwh = 1;
32644 +               au_fset_fillvdir(arg->flags, SHWH);
32645 +       }
32646 +       btop = au_fbtop(file);
32647 +       bbot = au_fbbot_dir(file);
32648 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32649 +               hf = au_hf_dir(file, bindex);
32650 +               if (!hf)
32651 +                       continue;
32652 +
32653 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32654 +               err = offset;
32655 +               if (unlikely(offset))
32656 +                       break;
32657 +
32658 +               arg->bindex = bindex;
32659 +               au_fclr_fillvdir(arg->flags, WHABLE);
32660 +               if (shwh
32661 +                   || (bindex != bbot
32662 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32663 +                       au_fset_fillvdir(arg->flags, WHABLE);
32664 +               do {
32665 +                       arg->err = 0;
32666 +                       au_fclr_fillvdir(arg->flags, CALLED);
32667 +                       /* smp_mb(); */
32668 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32669 +                       if (err >= 0)
32670 +                               err = arg->err;
32671 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32672 +
32673 +               /*
32674 +                * dir_relax() may be good for concurrency, but aufs should not
32675 +                * use it since it will cause a lockdep problem.
32676 +                */
32677 +       }
32678 +
32679 +       if (!err && shwh)
32680 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32681 +
32682 +       au_nhash_wh_free(&arg->whlist);
32683 +
32684 +out_delist:
32685 +       au_nhash_de_free(&arg->delist);
32686 +out:
32687 +       return err;
32688 +}
32689 +
32690 +static int read_vdir(struct file *file, int may_read)
32691 +{
32692 +       int err;
32693 +       unsigned long expire;
32694 +       unsigned char do_read;
32695 +       struct fillvdir_arg arg = {
32696 +               .ctx = {
32697 +                       .actor = fillvdir
32698 +               }
32699 +       };
32700 +       struct inode *inode;
32701 +       struct au_vdir *vdir, *allocated;
32702 +
32703 +       err = 0;
32704 +       inode = file_inode(file);
32705 +       IMustLock(inode);
32706 +       IiMustWriteLock(inode);
32707 +       SiMustAnyLock(inode->i_sb);
32708 +
32709 +       allocated = NULL;
32710 +       do_read = 0;
32711 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32712 +       vdir = au_ivdir(inode);
32713 +       if (!vdir) {
32714 +               do_read = 1;
32715 +               vdir = alloc_vdir(file);
32716 +               err = PTR_ERR(vdir);
32717 +               if (IS_ERR(vdir))
32718 +                       goto out;
32719 +               err = 0;
32720 +               allocated = vdir;
32721 +       } else if (may_read
32722 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32723 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32724 +               do_read = 1;
32725 +               err = reinit_vdir(vdir);
32726 +               if (unlikely(err))
32727 +                       goto out;
32728 +       }
32729 +
32730 +       if (!do_read)
32731 +               return 0; /* success */
32732 +
32733 +       arg.file = file;
32734 +       arg.vdir = vdir;
32735 +       err = au_do_read_vdir(&arg);
32736 +       if (!err) {
32737 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32738 +               vdir->vd_version = inode_query_iversion(inode);
32739 +               vdir->vd_last.ul = 0;
32740 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32741 +               if (allocated)
32742 +                       au_set_ivdir(inode, allocated);
32743 +       } else if (allocated)
32744 +               au_vdir_free(allocated);
32745 +
32746 +out:
32747 +       return err;
32748 +}
32749 +
32750 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32751 +{
32752 +       int err, rerr;
32753 +       unsigned long ul, n;
32754 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32755 +
32756 +       AuDebugOn(tgt->vd_nblk != 1);
32757 +
32758 +       err = -ENOMEM;
32759 +       if (tgt->vd_nblk < src->vd_nblk) {
32760 +               unsigned char **p;
32761 +
32762 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32763 +                               GFP_NOFS, /*may_shrink*/0);
32764 +               if (unlikely(!p))
32765 +                       goto out;
32766 +               tgt->vd_deblk = p;
32767 +       }
32768 +
32769 +       if (tgt->vd_deblk_sz != deblk_sz) {
32770 +               unsigned char *p;
32771 +
32772 +               tgt->vd_deblk_sz = deblk_sz;
32773 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32774 +                               /*may_shrink*/1);
32775 +               if (unlikely(!p))
32776 +                       goto out;
32777 +               tgt->vd_deblk[0] = p;
32778 +       }
32779 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32780 +       tgt->vd_version = src->vd_version;
32781 +       tgt->vd_jiffy = src->vd_jiffy;
32782 +
32783 +       n = src->vd_nblk;
32784 +       for (ul = 1; ul < n; ul++) {
32785 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32786 +                                           GFP_NOFS);
32787 +               if (unlikely(!tgt->vd_deblk[ul]))
32788 +                       goto out;
32789 +               tgt->vd_nblk++;
32790 +       }
32791 +       tgt->vd_nblk = n;
32792 +       tgt->vd_last.ul = tgt->vd_last.ul;
32793 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32794 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32795 +               - src->vd_deblk[src->vd_last.ul];
32796 +       /* smp_mb(); */
32797 +       return 0; /* success */
32798 +
32799 +out:
32800 +       rerr = reinit_vdir(tgt);
32801 +       BUG_ON(rerr);
32802 +       return err;
32803 +}
32804 +
32805 +int au_vdir_init(struct file *file)
32806 +{
32807 +       int err;
32808 +       struct inode *inode;
32809 +       struct au_vdir *vdir_cache, *allocated;
32810 +
32811 +       /* test file->f_pos here instead of ctx->pos */
32812 +       err = read_vdir(file, !file->f_pos);
32813 +       if (unlikely(err))
32814 +               goto out;
32815 +
32816 +       allocated = NULL;
32817 +       vdir_cache = au_fvdir_cache(file);
32818 +       if (!vdir_cache) {
32819 +               vdir_cache = alloc_vdir(file);
32820 +               err = PTR_ERR(vdir_cache);
32821 +               if (IS_ERR(vdir_cache))
32822 +                       goto out;
32823 +               allocated = vdir_cache;
32824 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32825 +               /* test file->f_pos here instead of ctx->pos */
32826 +               err = reinit_vdir(vdir_cache);
32827 +               if (unlikely(err))
32828 +                       goto out;
32829 +       } else
32830 +               return 0; /* success */
32831 +
32832 +       inode = file_inode(file);
32833 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32834 +       if (!err) {
32835 +               file->f_version = inode_query_iversion(inode);
32836 +               if (allocated)
32837 +                       au_set_fvdir_cache(file, allocated);
32838 +       } else if (allocated)
32839 +               au_vdir_free(allocated);
32840 +
32841 +out:
32842 +       return err;
32843 +}
32844 +
32845 +static loff_t calc_offset(struct au_vdir *vdir)
32846 +{
32847 +       loff_t offset;
32848 +       union au_vdir_deblk_p p;
32849 +
32850 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32851 +       offset = vdir->vd_last.p.deblk - p.deblk;
32852 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32853 +       return offset;
32854 +}
32855 +
32856 +/* returns true or false */
32857 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32858 +{
32859 +       int valid;
32860 +       unsigned int deblk_sz;
32861 +       unsigned long ul, n;
32862 +       loff_t offset;
32863 +       union au_vdir_deblk_p p, deblk_end;
32864 +       struct au_vdir *vdir_cache;
32865 +
32866 +       valid = 1;
32867 +       vdir_cache = au_fvdir_cache(file);
32868 +       offset = calc_offset(vdir_cache);
32869 +       AuDbg("offset %lld\n", offset);
32870 +       if (ctx->pos == offset)
32871 +               goto out;
32872 +
32873 +       vdir_cache->vd_last.ul = 0;
32874 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32875 +       if (!ctx->pos)
32876 +               goto out;
32877 +
32878 +       valid = 0;
32879 +       deblk_sz = vdir_cache->vd_deblk_sz;
32880 +       ul = div64_u64(ctx->pos, deblk_sz);
32881 +       AuDbg("ul %lu\n", ul);
32882 +       if (ul >= vdir_cache->vd_nblk)
32883 +               goto out;
32884 +
32885 +       n = vdir_cache->vd_nblk;
32886 +       for (; ul < n; ul++) {
32887 +               p.deblk = vdir_cache->vd_deblk[ul];
32888 +               deblk_end.deblk = p.deblk + deblk_sz;
32889 +               offset = ul;
32890 +               offset *= deblk_sz;
32891 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32892 +                       unsigned int l;
32893 +
32894 +                       l = calc_size(p.de->de_str.len);
32895 +                       offset += l;
32896 +                       p.deblk += l;
32897 +               }
32898 +               if (!is_deblk_end(&p, &deblk_end)) {
32899 +                       valid = 1;
32900 +                       vdir_cache->vd_last.ul = ul;
32901 +                       vdir_cache->vd_last.p = p;
32902 +                       break;
32903 +               }
32904 +       }
32905 +
32906 +out:
32907 +       /* smp_mb(); */
32908 +       if (!valid)
32909 +               AuDbg("valid %d\n", !valid);
32910 +       return valid;
32911 +}
32912 +
32913 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32914 +{
32915 +       unsigned int l, deblk_sz;
32916 +       union au_vdir_deblk_p deblk_end;
32917 +       struct au_vdir *vdir_cache;
32918 +       struct au_vdir_de *de;
32919 +
32920 +       if (!seek_vdir(file, ctx))
32921 +               return 0;
32922 +
32923 +       vdir_cache = au_fvdir_cache(file);
32924 +       deblk_sz = vdir_cache->vd_deblk_sz;
32925 +       while (1) {
32926 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32927 +               deblk_end.deblk += deblk_sz;
32928 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32929 +                       de = vdir_cache->vd_last.p.de;
32930 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32931 +                             de->de_str.len, de->de_str.name, ctx->pos,
32932 +                             (unsigned long)de->de_ino, de->de_type);
32933 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32934 +                                              de->de_str.len, de->de_ino,
32935 +                                              de->de_type))) {
32936 +                               /* todo: ignore the error caused by udba? */
32937 +                               /* return err; */
32938 +                               return 0;
32939 +                       }
32940 +
32941 +                       l = calc_size(de->de_str.len);
32942 +                       vdir_cache->vd_last.p.deblk += l;
32943 +                       ctx->pos += l;
32944 +               }
32945 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32946 +                       vdir_cache->vd_last.ul++;
32947 +                       vdir_cache->vd_last.p.deblk
32948 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32949 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32950 +                       continue;
32951 +               }
32952 +               break;
32953 +       }
32954 +
32955 +       /* smp_mb(); */
32956 +       return 0;
32957 +}
32958 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32959 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32960 +++ linux/fs/aufs/vfsub.c       2022-11-05 23:02:18.969222617 +0100
32961 @@ -0,0 +1,918 @@
32962 +// SPDX-License-Identifier: GPL-2.0
32963 +/*
32964 + * Copyright (C) 2005-2022 Junjiro R. Okajima
32965 + *
32966 + * This program is free software; you can redistribute it and/or modify
32967 + * it under the terms of the GNU General Public License as published by
32968 + * the Free Software Foundation; either version 2 of the License, or
32969 + * (at your option) any later version.
32970 + *
32971 + * This program is distributed in the hope that it will be useful,
32972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32974 + * GNU General Public License for more details.
32975 + *
32976 + * You should have received a copy of the GNU General Public License
32977 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32978 + */
32979 +
32980 +/*
32981 + * sub-routines for VFS
32982 + */
32983 +
32984 +#include <linux/mnt_namespace.h>
32985 +#include <linux/nsproxy.h>
32986 +#include <linux/security.h>
32987 +#include <linux/splice.h>
32988 +#include "aufs.h"
32989 +
32990 +#ifdef CONFIG_AUFS_BR_FUSE
32991 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32992 +{
32993 +       if (!au_test_fuse(h_sb) || !au_userns)
32994 +               return 0;
32995 +
32996 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32997 +}
32998 +#endif
32999 +
33000 +int vfsub_sync_filesystem(struct super_block *h_sb)
33001 +{
33002 +       int err;
33003 +
33004 +       lockdep_off();
33005 +       down_read(&h_sb->s_umount);
33006 +       err = sync_filesystem(h_sb);
33007 +       up_read(&h_sb->s_umount);
33008 +       lockdep_on();
33009 +
33010 +       return err;
33011 +}
33012 +
33013 +/* ---------------------------------------------------------------------- */
33014 +
33015 +int vfsub_update_h_iattr(struct path *h_path, int *did)
33016 +{
33017 +       int err;
33018 +       struct kstat st;
33019 +       struct super_block *h_sb;
33020 +
33021 +       /*
33022 +        * Always needs h_path->mnt for LSM or FUSE branch.
33023 +        */
33024 +       AuDebugOn(!h_path->mnt);
33025 +
33026 +       /* for remote fs, leave work for its getattr or d_revalidate */
33027 +       /* for bad i_attr fs, handle them in aufs_getattr() */
33028 +       /* still some fs may acquire i_mutex. we need to skip them */
33029 +       err = 0;
33030 +       if (!did)
33031 +               did = &err;
33032 +       h_sb = h_path->dentry->d_sb;
33033 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
33034 +       if (*did)
33035 +               err = vfsub_getattr(h_path, &st);
33036 +
33037 +       return err;
33038 +}
33039 +
33040 +/* ---------------------------------------------------------------------- */
33041 +
33042 +struct file *vfsub_dentry_open(struct path *path, int flags)
33043 +{
33044 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33045 +                          current_cred());
33046 +}
33047 +
33048 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33049 +{
33050 +       struct file *file;
33051 +
33052 +       lockdep_off();
33053 +       file = filp_open(path,
33054 +                        oflags /* | __FMODE_NONOTIFY */,
33055 +                        mode);
33056 +       lockdep_on();
33057 +       if (IS_ERR(file))
33058 +               goto out;
33059 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33060 +
33061 +out:
33062 +       return file;
33063 +}
33064 +
33065 +/*
33066 + * Ideally this function should call VFS:do_last() in order to keep all its
33067 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33068 + * structure such as nameidata. This is a second (or third) best approach.
33069 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33070 + */
33071 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33072 +                     struct vfsub_aopen_args *args)
33073 +{
33074 +       int err;
33075 +       struct au_branch *br = args->br;
33076 +       struct file *file = args->file;
33077 +       /* copied from linux/fs/namei.c:atomic_open() */
33078 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33079 +
33080 +       IMustLock(dir);
33081 +       AuDebugOn(!dir->i_op->atomic_open);
33082 +
33083 +       err = au_br_test_oflag(args->open_flag, br);
33084 +       if (unlikely(err))
33085 +               goto out;
33086 +
33087 +       au_lcnt_inc(&br->br_nfiles);
33088 +       file->f_path.dentry = DENTRY_NOT_SET;
33089 +       file->f_path.mnt = au_br_mnt(br);
33090 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33091 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33092 +                                    args->create_mode);
33093 +       if (unlikely(err < 0)) {
33094 +               au_lcnt_dec(&br->br_nfiles);
33095 +               goto out;
33096 +       }
33097 +
33098 +       /* temporary workaround for nfsv4 branch */
33099 +       if (au_test_nfs(dir->i_sb))
33100 +               nfs_mark_for_revalidate(dir);
33101 +
33102 +       if (file->f_mode & FMODE_CREATED)
33103 +               fsnotify_create(dir, dentry);
33104 +       if (!(file->f_mode & FMODE_OPENED)) {
33105 +               au_lcnt_dec(&br->br_nfiles);
33106 +               goto out;
33107 +       }
33108 +
33109 +       /* todo: call VFS:may_open() here */
33110 +       /* todo: ima_file_check() too? */
33111 +       if (!err && (args->open_flag & __FMODE_EXEC))
33112 +               err = deny_write_access(file);
33113 +       if (!err)
33114 +               fsnotify_open(file);
33115 +       else
33116 +               au_lcnt_dec(&br->br_nfiles);
33117 +       /* note that the file is created and still opened */
33118 +
33119 +out:
33120 +       return err;
33121 +}
33122 +
33123 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33124 +{
33125 +       int err;
33126 +
33127 +       err = kern_path(name, flags, path);
33128 +       if (!err && d_is_positive(path->dentry))
33129 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33130 +       return err;
33131 +}
33132 +
33133 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33134 +                                            struct path *ppath, int len)
33135 +{
33136 +       struct path path;
33137 +
33138 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33139 +       if (IS_ERR(path.dentry))
33140 +               goto out;
33141 +       if (d_is_positive(path.dentry)) {
33142 +               path.mnt = ppath->mnt;
33143 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33144 +       }
33145 +
33146 +out:
33147 +       AuTraceErrPtr(path.dentry);
33148 +       return path.dentry;
33149 +}
33150 +
33151 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33152 +                                   int len)
33153 +{
33154 +       struct path path;
33155 +
33156 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33157 +       IMustLock(d_inode(ppath->dentry));
33158 +
33159 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33160 +       if (IS_ERR(path.dentry))
33161 +               goto out;
33162 +       if (d_is_positive(path.dentry)) {
33163 +               path.mnt = ppath->mnt;
33164 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33165 +       }
33166 +
33167 +out:
33168 +       AuTraceErrPtr(path.dentry);
33169 +       return path.dentry;
33170 +}
33171 +
33172 +void vfsub_call_lkup_one(void *args)
33173 +{
33174 +       struct vfsub_lkup_one_args *a = args;
33175 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33176 +}
33177 +
33178 +/* ---------------------------------------------------------------------- */
33179 +
33180 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33181 +                                struct dentry *d2, struct au_hinode *hdir2)
33182 +{
33183 +       struct dentry *d;
33184 +
33185 +       lockdep_off();
33186 +       d = lock_rename(d1, d2);
33187 +       lockdep_on();
33188 +       au_hn_suspend(hdir1);
33189 +       if (hdir1 != hdir2)
33190 +               au_hn_suspend(hdir2);
33191 +
33192 +       return d;
33193 +}
33194 +
33195 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33196 +                        struct dentry *d2, struct au_hinode *hdir2)
33197 +{
33198 +       au_hn_resume(hdir1);
33199 +       if (hdir1 != hdir2)
33200 +               au_hn_resume(hdir2);
33201 +       lockdep_off();
33202 +       unlock_rename(d1, d2);
33203 +       lockdep_on();
33204 +}
33205 +
33206 +/* ---------------------------------------------------------------------- */
33207 +
33208 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33209 +{
33210 +       int err;
33211 +       struct dentry *d;
33212 +       struct user_namespace *userns;
33213 +
33214 +       IMustLock(dir);
33215 +
33216 +       d = path->dentry;
33217 +       path->dentry = d->d_parent;
33218 +       err = security_path_mknod(path, d, mode, 0);
33219 +       path->dentry = d;
33220 +       if (unlikely(err))
33221 +               goto out;
33222 +       userns = mnt_user_ns(path->mnt);
33223 +
33224 +       lockdep_off();
33225 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
33226 +       lockdep_on();
33227 +       if (!err) {
33228 +               struct path tmp = *path;
33229 +               int did;
33230 +
33231 +               vfsub_update_h_iattr(&tmp, &did);
33232 +               if (did) {
33233 +                       tmp.dentry = path->dentry->d_parent;
33234 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33235 +               }
33236 +               /*ignore*/
33237 +       }
33238 +
33239 +out:
33240 +       return err;
33241 +}
33242 +
33243 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33244 +{
33245 +       int err;
33246 +       struct dentry *d;
33247 +       struct user_namespace *userns;
33248 +
33249 +       IMustLock(dir);
33250 +
33251 +       d = path->dentry;
33252 +       path->dentry = d->d_parent;
33253 +       err = security_path_symlink(path, d, symname);
33254 +       path->dentry = d;
33255 +       if (unlikely(err))
33256 +               goto out;
33257 +       userns = mnt_user_ns(path->mnt);
33258 +
33259 +       lockdep_off();
33260 +       err = vfs_symlink(userns, dir, path->dentry, symname);
33261 +       lockdep_on();
33262 +       if (!err) {
33263 +               struct path tmp = *path;
33264 +               int did;
33265 +
33266 +               vfsub_update_h_iattr(&tmp, &did);
33267 +               if (did) {
33268 +                       tmp.dentry = path->dentry->d_parent;
33269 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33270 +               }
33271 +               /*ignore*/
33272 +       }
33273 +
33274 +out:
33275 +       return err;
33276 +}
33277 +
33278 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33279 +{
33280 +       int err;
33281 +       struct dentry *d;
33282 +       struct user_namespace *userns;
33283 +
33284 +       IMustLock(dir);
33285 +
33286 +       d = path->dentry;
33287 +       path->dentry = d->d_parent;
33288 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33289 +       path->dentry = d;
33290 +       if (unlikely(err))
33291 +               goto out;
33292 +       userns = mnt_user_ns(path->mnt);
33293 +
33294 +       lockdep_off();
33295 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
33296 +       lockdep_on();
33297 +       if (!err) {
33298 +               struct path tmp = *path;
33299 +               int did;
33300 +
33301 +               vfsub_update_h_iattr(&tmp, &did);
33302 +               if (did) {
33303 +                       tmp.dentry = path->dentry->d_parent;
33304 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33305 +               }
33306 +               /*ignore*/
33307 +       }
33308 +
33309 +out:
33310 +       return err;
33311 +}
33312 +
33313 +static int au_test_nlink(struct inode *inode)
33314 +{
33315 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33316 +
33317 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33318 +           || inode->i_nlink < link_max)
33319 +               return 0;
33320 +       return -EMLINK;
33321 +}
33322 +
33323 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33324 +              struct inode **delegated_inode)
33325 +{
33326 +       int err;
33327 +       struct dentry *d;
33328 +       struct user_namespace *userns;
33329 +
33330 +       IMustLock(dir);
33331 +
33332 +       err = au_test_nlink(d_inode(src_dentry));
33333 +       if (unlikely(err))
33334 +               return err;
33335 +
33336 +       /* we don't call may_linkat() */
33337 +       d = path->dentry;
33338 +       path->dentry = d->d_parent;
33339 +       err = security_path_link(src_dentry, path, d);
33340 +       path->dentry = d;
33341 +       if (unlikely(err))
33342 +               goto out;
33343 +       userns = mnt_user_ns(path->mnt);
33344 +
33345 +       lockdep_off();
33346 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33347 +       lockdep_on();
33348 +       if (!err) {
33349 +               struct path tmp = *path;
33350 +               int did;
33351 +
33352 +               /* fuse has different memory inode for the same inumber */
33353 +               vfsub_update_h_iattr(&tmp, &did);
33354 +               if (did) {
33355 +                       tmp.dentry = path->dentry->d_parent;
33356 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33357 +                       tmp.dentry = src_dentry;
33358 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33359 +               }
33360 +               /*ignore*/
33361 +       }
33362 +
33363 +out:
33364 +       return err;
33365 +}
33366 +
33367 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33368 +                struct inode *dir, struct path *path,
33369 +                struct inode **delegated_inode, unsigned int flags)
33370 +{
33371 +       int err;
33372 +       struct renamedata rd;
33373 +       struct path tmp = {
33374 +               .mnt    = path->mnt
33375 +       };
33376 +       struct dentry *d;
33377 +
33378 +       IMustLock(dir);
33379 +       IMustLock(src_dir);
33380 +
33381 +       d = path->dentry;
33382 +       path->dentry = d->d_parent;
33383 +       tmp.dentry = src_dentry->d_parent;
33384 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33385 +       path->dentry = d;
33386 +       if (unlikely(err))
33387 +               goto out;
33388 +
33389 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33390 +       rd.old_dir = src_dir;
33391 +       rd.old_dentry = src_dentry;
33392 +       rd.new_mnt_userns = rd.old_mnt_userns;
33393 +       rd.new_dir = dir;
33394 +       rd.new_dentry = path->dentry;
33395 +       rd.delegated_inode = delegated_inode;
33396 +       rd.flags = flags;
33397 +       lockdep_off();
33398 +       err = vfs_rename(&rd);
33399 +       lockdep_on();
33400 +       if (!err) {
33401 +               int did;
33402 +
33403 +               tmp.dentry = d->d_parent;
33404 +               vfsub_update_h_iattr(&tmp, &did);
33405 +               if (did) {
33406 +                       tmp.dentry = src_dentry;
33407 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33408 +                       tmp.dentry = src_dentry->d_parent;
33409 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33410 +               }
33411 +               /*ignore*/
33412 +       }
33413 +
33414 +out:
33415 +       return err;
33416 +}
33417 +
33418 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33419 +{
33420 +       int err;
33421 +       struct dentry *d;
33422 +       struct user_namespace *userns;
33423 +
33424 +       IMustLock(dir);
33425 +
33426 +       d = path->dentry;
33427 +       path->dentry = d->d_parent;
33428 +       err = security_path_mkdir(path, d, mode);
33429 +       path->dentry = d;
33430 +       if (unlikely(err))
33431 +               goto out;
33432 +       userns = mnt_user_ns(path->mnt);
33433 +
33434 +       lockdep_off();
33435 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33436 +       lockdep_on();
33437 +       if (!err) {
33438 +               struct path tmp = *path;
33439 +               int did;
33440 +
33441 +               vfsub_update_h_iattr(&tmp, &did);
33442 +               if (did) {
33443 +                       tmp.dentry = path->dentry->d_parent;
33444 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33445 +               }
33446 +               /*ignore*/
33447 +       }
33448 +
33449 +out:
33450 +       return err;
33451 +}
33452 +
33453 +int vfsub_rmdir(struct inode *dir, struct path *path)
33454 +{
33455 +       int err;
33456 +       struct dentry *d;
33457 +       struct user_namespace *userns;
33458 +
33459 +       IMustLock(dir);
33460 +
33461 +       d = path->dentry;
33462 +       path->dentry = d->d_parent;
33463 +       err = security_path_rmdir(path, d);
33464 +       path->dentry = d;
33465 +       if (unlikely(err))
33466 +               goto out;
33467 +       userns = mnt_user_ns(path->mnt);
33468 +
33469 +       lockdep_off();
33470 +       err = vfs_rmdir(userns, dir, path->dentry);
33471 +       lockdep_on();
33472 +       if (!err) {
33473 +               struct path tmp = {
33474 +                       .dentry = path->dentry->d_parent,
33475 +                       .mnt    = path->mnt
33476 +               };
33477 +
33478 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33479 +       }
33480 +
33481 +out:
33482 +       return err;
33483 +}
33484 +
33485 +/* ---------------------------------------------------------------------- */
33486 +
33487 +/* todo: support mmap_sem? */
33488 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33489 +                    loff_t *ppos)
33490 +{
33491 +       ssize_t err;
33492 +
33493 +       lockdep_off();
33494 +       err = vfs_read(file, ubuf, count, ppos);
33495 +       lockdep_on();
33496 +       if (err >= 0)
33497 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33498 +       return err;
33499 +}
33500 +
33501 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33502 +                    loff_t *ppos)
33503 +{
33504 +       ssize_t err;
33505 +
33506 +       lockdep_off();
33507 +       err = kernel_read(file, kbuf, count, ppos);
33508 +       lockdep_on();
33509 +       AuTraceErr(err);
33510 +       if (err >= 0)
33511 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33512 +       return err;
33513 +}
33514 +
33515 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33516 +                     loff_t *ppos)
33517 +{
33518 +       ssize_t err;
33519 +
33520 +       lockdep_off();
33521 +       err = vfs_write(file, ubuf, count, ppos);
33522 +       lockdep_on();
33523 +       if (err >= 0)
33524 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33525 +       return err;
33526 +}
33527 +
33528 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33529 +{
33530 +       ssize_t err;
33531 +
33532 +       lockdep_off();
33533 +       err = kernel_write(file, kbuf, count, ppos);
33534 +       lockdep_on();
33535 +       if (err >= 0)
33536 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33537 +       return err;
33538 +}
33539 +
33540 +int vfsub_flush(struct file *file, fl_owner_t id)
33541 +{
33542 +       int err;
33543 +
33544 +       err = 0;
33545 +       if (file->f_op->flush) {
33546 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33547 +                       err = file->f_op->flush(file, id);
33548 +               else {
33549 +                       lockdep_off();
33550 +                       err = file->f_op->flush(file, id);
33551 +                       lockdep_on();
33552 +               }
33553 +               if (!err)
33554 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33555 +               /*ignore*/
33556 +       }
33557 +       return err;
33558 +}
33559 +
33560 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33561 +{
33562 +       int err;
33563 +
33564 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33565 +
33566 +       lockdep_off();
33567 +       err = iterate_dir(file, ctx);
33568 +       lockdep_on();
33569 +       if (err >= 0)
33570 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33571 +
33572 +       return err;
33573 +}
33574 +
33575 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33576 +                    struct pipe_inode_info *pipe, size_t len,
33577 +                    unsigned int flags)
33578 +{
33579 +       long err;
33580 +
33581 +       lockdep_off();
33582 +       err = do_splice_to(in, ppos, pipe, len, flags);
33583 +       lockdep_on();
33584 +       file_accessed(in);
33585 +       if (err >= 0)
33586 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33587 +       return err;
33588 +}
33589 +
33590 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33591 +                      loff_t *ppos, size_t len, unsigned int flags)
33592 +{
33593 +       long err;
33594 +
33595 +       lockdep_off();
33596 +       err = do_splice_from(pipe, out, ppos, len, flags);
33597 +       lockdep_on();
33598 +       if (err >= 0)
33599 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33600 +       return err;
33601 +}
33602 +
33603 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33604 +{
33605 +       int err;
33606 +
33607 +       /* file can be NULL */
33608 +       lockdep_off();
33609 +       err = vfs_fsync(file, datasync);
33610 +       lockdep_on();
33611 +       if (!err) {
33612 +               if (!path) {
33613 +                       AuDebugOn(!file);
33614 +                       path = &file->f_path;
33615 +               }
33616 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33617 +       }
33618 +       return err;
33619 +}
33620 +
33621 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33622 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33623 +               struct file *h_file)
33624 +{
33625 +       int err;
33626 +       struct inode *h_inode;
33627 +       struct super_block *h_sb;
33628 +       struct user_namespace *h_userns;
33629 +
33630 +       if (!h_file) {
33631 +               err = vfsub_truncate(h_path, length);
33632 +               goto out;
33633 +       }
33634 +
33635 +       h_inode = d_inode(h_path->dentry);
33636 +       h_sb = h_inode->i_sb;
33637 +       lockdep_off();
33638 +       sb_start_write(h_sb);
33639 +       lockdep_on();
33640 +       err = security_path_truncate(h_path);
33641 +       if (!err) {
33642 +               h_userns = mnt_user_ns(h_path->mnt);
33643 +               lockdep_off();
33644 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33645 +                                 h_file);
33646 +               lockdep_on();
33647 +       }
33648 +       lockdep_off();
33649 +       sb_end_write(h_sb);
33650 +       lockdep_on();
33651 +
33652 +out:
33653 +       return err;
33654 +}
33655 +
33656 +/* ---------------------------------------------------------------------- */
33657 +
33658 +struct au_vfsub_mkdir_args {
33659 +       int *errp;
33660 +       struct inode *dir;
33661 +       struct path *path;
33662 +       int mode;
33663 +};
33664 +
33665 +static void au_call_vfsub_mkdir(void *args)
33666 +{
33667 +       struct au_vfsub_mkdir_args *a = args;
33668 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33669 +}
33670 +
33671 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33672 +{
33673 +       int err, do_sio, wkq_err;
33674 +       struct user_namespace *userns;
33675 +
33676 +       userns = mnt_user_ns(path->mnt);
33677 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33678 +       if (!do_sio) {
33679 +               lockdep_off();
33680 +               err = vfsub_mkdir(dir, path, mode);
33681 +               lockdep_on();
33682 +       } else {
33683 +               struct au_vfsub_mkdir_args args = {
33684 +                       .errp   = &err,
33685 +                       .dir    = dir,
33686 +                       .path   = path,
33687 +                       .mode   = mode
33688 +               };
33689 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33690 +               if (unlikely(wkq_err))
33691 +                       err = wkq_err;
33692 +       }
33693 +
33694 +       return err;
33695 +}
33696 +
33697 +struct au_vfsub_rmdir_args {
33698 +       int *errp;
33699 +       struct inode *dir;
33700 +       struct path *path;
33701 +};
33702 +
33703 +static void au_call_vfsub_rmdir(void *args)
33704 +{
33705 +       struct au_vfsub_rmdir_args *a = args;
33706 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33707 +}
33708 +
33709 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33710 +{
33711 +       int err, do_sio, wkq_err;
33712 +       struct user_namespace *userns;
33713 +
33714 +       userns = mnt_user_ns(path->mnt);
33715 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33716 +       if (!do_sio) {
33717 +               lockdep_off();
33718 +               err = vfsub_rmdir(dir, path);
33719 +               lockdep_on();
33720 +       } else {
33721 +               struct au_vfsub_rmdir_args args = {
33722 +                       .errp   = &err,
33723 +                       .dir    = dir,
33724 +                       .path   = path
33725 +               };
33726 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33727 +               if (unlikely(wkq_err))
33728 +                       err = wkq_err;
33729 +       }
33730 +
33731 +       return err;
33732 +}
33733 +
33734 +/* ---------------------------------------------------------------------- */
33735 +
33736 +struct notify_change_args {
33737 +       int *errp;
33738 +       struct path *path;
33739 +       struct iattr *ia;
33740 +       struct inode **delegated_inode;
33741 +};
33742 +
33743 +static void call_notify_change(void *args)
33744 +{
33745 +       struct notify_change_args *a = args;
33746 +       struct inode *h_inode;
33747 +       struct user_namespace *userns;
33748 +
33749 +       h_inode = d_inode(a->path->dentry);
33750 +       IMustLock(h_inode);
33751 +
33752 +       *a->errp = -EPERM;
33753 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33754 +               userns = mnt_user_ns(a->path->mnt);
33755 +               lockdep_off();
33756 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33757 +                                        a->delegated_inode);
33758 +               lockdep_on();
33759 +               if (!*a->errp)
33760 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33761 +       }
33762 +       AuTraceErr(*a->errp);
33763 +}
33764 +
33765 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33766 +                       struct inode **delegated_inode)
33767 +{
33768 +       int err;
33769 +       struct notify_change_args args = {
33770 +               .errp                   = &err,
33771 +               .path                   = path,
33772 +               .ia                     = ia,
33773 +               .delegated_inode        = delegated_inode
33774 +       };
33775 +
33776 +       call_notify_change(&args);
33777 +
33778 +       return err;
33779 +}
33780 +
33781 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33782 +                           struct inode **delegated_inode)
33783 +{
33784 +       int err, wkq_err;
33785 +       struct notify_change_args args = {
33786 +               .errp                   = &err,
33787 +               .path                   = path,
33788 +               .ia                     = ia,
33789 +               .delegated_inode        = delegated_inode
33790 +       };
33791 +
33792 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33793 +       if (unlikely(wkq_err))
33794 +               err = wkq_err;
33795 +
33796 +       return err;
33797 +}
33798 +
33799 +/* ---------------------------------------------------------------------- */
33800 +
33801 +struct unlink_args {
33802 +       int *errp;
33803 +       struct inode *dir;
33804 +       struct path *path;
33805 +       struct inode **delegated_inode;
33806 +};
33807 +
33808 +static void call_unlink(void *args)
33809 +{
33810 +       struct unlink_args *a = args;
33811 +       struct dentry *d = a->path->dentry;
33812 +       struct inode *h_inode;
33813 +       struct user_namespace *userns;
33814 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33815 +                                     && au_dcount(d) == 1);
33816 +
33817 +       IMustLock(a->dir);
33818 +
33819 +       a->path->dentry = d->d_parent;
33820 +       *a->errp = security_path_unlink(a->path, d);
33821 +       a->path->dentry = d;
33822 +       if (unlikely(*a->errp))
33823 +               return;
33824 +
33825 +       if (!stop_sillyrename)
33826 +               dget(d);
33827 +       h_inode = NULL;
33828 +       if (d_is_positive(d)) {
33829 +               h_inode = d_inode(d);
33830 +               ihold(h_inode);
33831 +       }
33832 +
33833 +       userns = mnt_user_ns(a->path->mnt);
33834 +       lockdep_off();
33835 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33836 +       lockdep_on();
33837 +       if (!*a->errp) {
33838 +               struct path tmp = {
33839 +                       .dentry = d->d_parent,
33840 +                       .mnt    = a->path->mnt
33841 +               };
33842 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33843 +       }
33844 +
33845 +       if (!stop_sillyrename)
33846 +               dput(d);
33847 +       if (h_inode)
33848 +               iput(h_inode);
33849 +
33850 +       AuTraceErr(*a->errp);
33851 +}
33852 +
33853 +/*
33854 + * @dir: must be locked.
33855 + * @dentry: target dentry.
33856 + */
33857 +int vfsub_unlink(struct inode *dir, struct path *path,
33858 +                struct inode **delegated_inode, int force)
33859 +{
33860 +       int err;
33861 +       struct unlink_args args = {
33862 +               .errp                   = &err,
33863 +               .dir                    = dir,
33864 +               .path                   = path,
33865 +               .delegated_inode        = delegated_inode
33866 +       };
33867 +
33868 +       if (!force)
33869 +               call_unlink(&args);
33870 +       else {
33871 +               int wkq_err;
33872 +
33873 +               wkq_err = au_wkq_wait(call_unlink, &args);
33874 +               if (unlikely(wkq_err))
33875 +                       err = wkq_err;
33876 +       }
33877 +
33878 +       return err;
33879 +}
33880 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33881 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33882 +++ linux/fs/aufs/vfsub.h       2022-12-17 09:21:34.799855195 +0100
33883 @@ -0,0 +1,358 @@
33884 +/* SPDX-License-Identifier: GPL-2.0 */
33885 +/*
33886 + * Copyright (C) 2005-2022 Junjiro R. Okajima
33887 + *
33888 + * This program is free software; you can redistribute it and/or modify
33889 + * it under the terms of the GNU General Public License as published by
33890 + * the Free Software Foundation; either version 2 of the License, or
33891 + * (at your option) any later version.
33892 + *
33893 + * This program is distributed in the hope that it will be useful,
33894 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33895 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33896 + * GNU General Public License for more details.
33897 + *
33898 + * You should have received a copy of the GNU General Public License
33899 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33900 + */
33901 +
33902 +/*
33903 + * sub-routines for VFS
33904 + */
33905 +
33906 +#ifndef __AUFS_VFSUB_H__
33907 +#define __AUFS_VFSUB_H__
33908 +
33909 +#ifdef __KERNEL__
33910 +
33911 +#include <linux/fs.h>
33912 +#include <linux/mount.h>
33913 +#include <linux/posix_acl.h>
33914 +#include <linux/xattr.h>
33915 +#include "debug.h"
33916 +
33917 +/* copied from linux/fs/internal.h */
33918 +/* todo: BAD approach!! */
33919 +extern void __mnt_drop_write(struct vfsmount *);
33920 +extern struct file *alloc_empty_file(int, const struct cred *);
33921 +
33922 +/* ---------------------------------------------------------------------- */
33923 +
33924 +/* lock subclass for lower inode */
33925 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33926 +/* reduce? gave up. */
33927 +enum {
33928 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33929 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33930 +       AuLsc_I_PARENT2,        /* copyup dirs */
33931 +       AuLsc_I_PARENT3,        /* copyup wh */
33932 +       AuLsc_I_CHILD,
33933 +       AuLsc_I_CHILD2,
33934 +       AuLsc_I_End
33935 +};
33936 +
33937 +/* to debug easier, do not make them inlined functions */
33938 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33939 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33940 +
33941 +/* ---------------------------------------------------------------------- */
33942 +
33943 +static inline void vfsub_drop_nlink(struct inode *inode)
33944 +{
33945 +       AuDebugOn(!inode->i_nlink);
33946 +       drop_nlink(inode);
33947 +}
33948 +
33949 +static inline void vfsub_dead_dir(struct inode *inode)
33950 +{
33951 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33952 +       inode->i_flags |= S_DEAD;
33953 +       clear_nlink(inode);
33954 +}
33955 +
33956 +static inline int vfsub_native_ro(struct inode *inode)
33957 +{
33958 +       return sb_rdonly(inode->i_sb)
33959 +               || IS_RDONLY(inode)
33960 +               /* || IS_APPEND(inode) */
33961 +               || IS_IMMUTABLE(inode);
33962 +}
33963 +
33964 +#ifdef CONFIG_AUFS_BR_FUSE
33965 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33966 +#else
33967 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33968 +#endif
33969 +
33970 +int vfsub_sync_filesystem(struct super_block *h_sb);
33971 +
33972 +/* ---------------------------------------------------------------------- */
33973 +
33974 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33975 +struct file *vfsub_dentry_open(struct path *path, int flags);
33976 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33977 +struct au_branch;
33978 +struct vfsub_aopen_args {
33979 +       struct file             *file;
33980 +       unsigned int            open_flag;
33981 +       umode_t                 create_mode;
33982 +       struct au_branch        *br;
33983 +};
33984 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33985 +                     struct vfsub_aopen_args *args);
33986 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33987 +
33988 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33989 +                                            struct path *ppath, int len);
33990 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33991 +                                   int len);
33992 +
33993 +struct vfsub_lkup_one_args {
33994 +       struct dentry **errp;
33995 +       struct qstr *name;
33996 +       struct path *ppath;
33997 +};
33998 +
33999 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
34000 +                                           struct path *ppath)
34001 +{
34002 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
34003 +}
34004 +
34005 +void vfsub_call_lkup_one(void *args);
34006 +
34007 +/* ---------------------------------------------------------------------- */
34008 +
34009 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
34010 +{
34011 +       int err;
34012 +
34013 +       lockdep_off();
34014 +       err = mnt_want_write(mnt);
34015 +       lockdep_on();
34016 +       return err;
34017 +}
34018 +
34019 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
34020 +{
34021 +       lockdep_off();
34022 +       mnt_drop_write(mnt);
34023 +       lockdep_on();
34024 +}
34025 +
34026 +#if 0 /* reserved */
34027 +static inline void vfsub_mnt_drop_write_file(struct file *file)
34028 +{
34029 +       lockdep_off();
34030 +       mnt_drop_write_file(file);
34031 +       lockdep_on();
34032 +}
34033 +#endif
34034 +
34035 +/* ---------------------------------------------------------------------- */
34036 +
34037 +struct au_hinode;
34038 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
34039 +                                struct dentry *d2, struct au_hinode *hdir2);
34040 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
34041 +                        struct dentry *d2, struct au_hinode *hdir2);
34042 +
34043 +int vfsub_create(struct inode *dir, struct path *path, int mode,
34044 +                bool want_excl);
34045 +int vfsub_symlink(struct inode *dir, struct path *path,
34046 +                 const char *symname);
34047 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34048 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34049 +              struct path *path, struct inode **delegated_inode);
34050 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34051 +                struct inode *hdir, struct path *path,
34052 +                struct inode **delegated_inode, unsigned int flags);
34053 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34054 +int vfsub_rmdir(struct inode *dir, struct path *path);
34055 +
34056 +/* ---------------------------------------------------------------------- */
34057 +
34058 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34059 +                    loff_t *ppos);
34060 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34061 +                       loff_t *ppos);
34062 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34063 +                     loff_t *ppos);
34064 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34065 +                     loff_t *ppos);
34066 +int vfsub_flush(struct file *file, fl_owner_t id);
34067 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34068 +
34069 +static inline loff_t vfsub_f_size_read(struct file *file)
34070 +{
34071 +       return i_size_read(file_inode(file));
34072 +}
34073 +
34074 +static inline unsigned int vfsub_file_flags(struct file *file)
34075 +{
34076 +       unsigned int flags;
34077 +
34078 +       spin_lock(&file->f_lock);
34079 +       flags = file->f_flags;
34080 +       spin_unlock(&file->f_lock);
34081 +
34082 +       return flags;
34083 +}
34084 +
34085 +static inline int vfsub_file_execed(struct file *file)
34086 +{
34087 +       /* todo: direct access f_flags */
34088 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34089 +}
34090 +
34091 +#if 0 /* reserved */
34092 +static inline void vfsub_file_accessed(struct file *h_file)
34093 +{
34094 +       file_accessed(h_file);
34095 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34096 +}
34097 +#endif
34098 +
34099 +#if 0 /* reserved */
34100 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34101 +                                    struct dentry *h_dentry)
34102 +{
34103 +       struct path h_path = {
34104 +               .dentry = h_dentry,
34105 +               .mnt    = h_mnt
34106 +       };
34107 +       touch_atime(&h_path);
34108 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34109 +}
34110 +#endif
34111 +
34112 +static inline int vfsub_update_time(struct inode *h_inode,
34113 +                                   struct timespec64 *ts, int flags)
34114 +{
34115 +       return inode_update_time(h_inode, ts, flags);
34116 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34117 +}
34118 +
34119 +#ifdef CONFIG_FS_POSIX_ACL
34120 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
34121 +                                 struct inode *h_inode, umode_t h_mode)
34122 +{
34123 +       int err;
34124 +
34125 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
34126 +       if (err == -EOPNOTSUPP)
34127 +               err = 0;
34128 +       return err;
34129 +}
34130 +#else
34131 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
34132 +          struct inode *h_inode, umode_t h_mode);
34133 +#endif
34134 +
34135 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34136 +                    struct pipe_inode_info *pipe, size_t len,
34137 +                    unsigned int flags);
34138 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34139 +                      loff_t *ppos, size_t len, unsigned int flags);
34140 +
34141 +static inline long vfsub_truncate(struct path *path, loff_t length)
34142 +{
34143 +       long err;
34144 +
34145 +       lockdep_off();
34146 +       err = vfs_truncate(path, length);
34147 +       lockdep_on();
34148 +       return err;
34149 +}
34150 +
34151 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34152 +               struct file *h_file);
34153 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34154 +
34155 +/*
34156 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34157 + * ioctl.
34158 + */
34159 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34160 +                                           loff_t len)
34161 +{
34162 +       loff_t err;
34163 +
34164 +       lockdep_off();
34165 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34166 +       lockdep_on();
34167 +
34168 +       return err;
34169 +}
34170 +
34171 +/* copy_file_range(2) is a systemcall */
34172 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34173 +                                           struct file *dst, loff_t dst_pos,
34174 +                                           size_t len, unsigned int flags)
34175 +{
34176 +       ssize_t ssz;
34177 +
34178 +       lockdep_off();
34179 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34180 +       lockdep_on();
34181 +
34182 +       return ssz;
34183 +}
34184 +
34185 +/* ---------------------------------------------------------------------- */
34186 +
34187 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34188 +{
34189 +       loff_t err;
34190 +
34191 +       lockdep_off();
34192 +       err = vfs_llseek(file, offset, origin);
34193 +       lockdep_on();
34194 +       return err;
34195 +}
34196 +
34197 +/* ---------------------------------------------------------------------- */
34198 +
34199 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34200 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34201 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34202 +                           struct inode **delegated_inode);
34203 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34204 +                       struct inode **delegated_inode);
34205 +int vfsub_unlink(struct inode *dir, struct path *path,
34206 +                struct inode **delegated_inode, int force);
34207 +
34208 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34209 +{
34210 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34211 +}
34212 +
34213 +/* ---------------------------------------------------------------------- */
34214 +
34215 +static inline int vfsub_setxattr(struct user_namespace *userns,
34216 +                                struct dentry *dentry, const char *name,
34217 +                                const void *value, size_t size, int flags)
34218 +{
34219 +       int err;
34220 +
34221 +       lockdep_off();
34222 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
34223 +       lockdep_on();
34224 +
34225 +       return err;
34226 +}
34227 +
34228 +static inline int vfsub_removexattr(struct user_namespace *userns,
34229 +                                   struct dentry *dentry, const char *name)
34230 +{
34231 +       int err;
34232 +
34233 +       lockdep_off();
34234 +       err = vfs_removexattr(userns, dentry, name);
34235 +       lockdep_on();
34236 +
34237 +       return err;
34238 +}
34239 +
34240 +#endif /* __KERNEL__ */
34241 +#endif /* __AUFS_VFSUB_H__ */
34242 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34243 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34244 +++ linux/fs/aufs/wbr_policy.c  2022-11-05 23:02:18.969222617 +0100
34245 @@ -0,0 +1,830 @@
34246 +// SPDX-License-Identifier: GPL-2.0
34247 +/*
34248 + * Copyright (C) 2005-2022 Junjiro R. Okajima
34249 + *
34250 + * This program is free software; you can redistribute it and/or modify
34251 + * it under the terms of the GNU General Public License as published by
34252 + * the Free Software Foundation; either version 2 of the License, or
34253 + * (at your option) any later version.
34254 + *
34255 + * This program is distributed in the hope that it will be useful,
34256 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34257 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34258 + * GNU General Public License for more details.
34259 + *
34260 + * You should have received a copy of the GNU General Public License
34261 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34262 + */
34263 +
34264 +/*
34265 + * policies for selecting one among multiple writable branches
34266 + */
34267 +
34268 +#include <linux/statfs.h>
34269 +#include "aufs.h"
34270 +
34271 +/* subset of cpup_attr() */
34272 +static noinline_for_stack
34273 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34274 +{
34275 +       int err, sbits;
34276 +       struct iattr ia;
34277 +       struct inode *h_isrc;
34278 +
34279 +       h_isrc = d_inode(h_src);
34280 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34281 +       ia.ia_mode = h_isrc->i_mode;
34282 +       ia.ia_uid = h_isrc->i_uid;
34283 +       ia.ia_gid = h_isrc->i_gid;
34284 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34285 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34286 +       /* no delegation since it is just created */
34287 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34288 +
34289 +       /* is this nfs only? */
34290 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34291 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34292 +               ia.ia_mode = h_isrc->i_mode;
34293 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34294 +       }
34295 +
34296 +       return err;
34297 +}
34298 +
34299 +#define AuCpdown_PARENT_OPQ    1
34300 +#define AuCpdown_WHED          (1 << 1)
34301 +#define AuCpdown_MADE_DIR      (1 << 2)
34302 +#define AuCpdown_DIROPQ                (1 << 3)
34303 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34304 +#define au_fset_cpdown(flags, name) \
34305 +       do { (flags) |= AuCpdown_##name; } while (0)
34306 +#define au_fclr_cpdown(flags, name) \
34307 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34308 +
34309 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34310 +                            unsigned int *flags)
34311 +{
34312 +       int err;
34313 +       struct dentry *opq_dentry;
34314 +
34315 +       opq_dentry = au_diropq_create(dentry, bdst);
34316 +       err = PTR_ERR(opq_dentry);
34317 +       if (IS_ERR(opq_dentry))
34318 +               goto out;
34319 +       dput(opq_dentry);
34320 +       au_fset_cpdown(*flags, DIROPQ);
34321 +
34322 +out:
34323 +       return err;
34324 +}
34325 +
34326 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34327 +                           struct inode *dir, aufs_bindex_t bdst)
34328 +{
34329 +       int err;
34330 +       struct path h_path;
34331 +       struct au_branch *br;
34332 +
34333 +       br = au_sbr(dentry->d_sb, bdst);
34334 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34335 +       err = PTR_ERR(h_path.dentry);
34336 +       if (IS_ERR(h_path.dentry))
34337 +               goto out;
34338 +
34339 +       err = 0;
34340 +       if (d_is_positive(h_path.dentry)) {
34341 +               h_path.mnt = au_br_mnt(br);
34342 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34343 +                                         dentry);
34344 +       }
34345 +       dput(h_path.dentry);
34346 +
34347 +out:
34348 +       return err;
34349 +}
34350 +
34351 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34352 +                        struct au_pin *pin,
34353 +                        struct dentry *h_parent, void *arg)
34354 +{
34355 +       int err, rerr;
34356 +       aufs_bindex_t bopq, btop;
34357 +       struct path h_path;
34358 +       struct dentry *parent;
34359 +       struct inode *h_dir, *h_inode, *inode, *dir;
34360 +       unsigned int *flags = arg;
34361 +
34362 +       btop = au_dbtop(dentry);
34363 +       /* dentry is di-locked */
34364 +       parent = dget_parent(dentry);
34365 +       dir = d_inode(parent);
34366 +       h_dir = d_inode(h_parent);
34367 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34368 +       IMustLock(h_dir);
34369 +
34370 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34371 +       if (unlikely(err < 0))
34372 +               goto out;
34373 +       h_path.dentry = au_h_dptr(dentry, bdst);
34374 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34375 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34376 +       if (unlikely(err))
34377 +               goto out_put;
34378 +       au_fset_cpdown(*flags, MADE_DIR);
34379 +
34380 +       bopq = au_dbdiropq(dentry);
34381 +       au_fclr_cpdown(*flags, WHED);
34382 +       au_fclr_cpdown(*flags, DIROPQ);
34383 +       if (au_dbwh(dentry) == bdst)
34384 +               au_fset_cpdown(*flags, WHED);
34385 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34386 +               au_fset_cpdown(*flags, PARENT_OPQ);
34387 +       h_inode = d_inode(h_path.dentry);
34388 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34389 +       if (au_ftest_cpdown(*flags, WHED)) {
34390 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34391 +               if (unlikely(err)) {
34392 +                       inode_unlock(h_inode);
34393 +                       goto out_dir;
34394 +               }
34395 +       }
34396 +
34397 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34398 +       inode_unlock(h_inode);
34399 +       if (unlikely(err))
34400 +               goto out_opq;
34401 +
34402 +       if (au_ftest_cpdown(*flags, WHED)) {
34403 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34404 +               if (unlikely(err))
34405 +                       goto out_opq;
34406 +       }
34407 +
34408 +       inode = d_inode(dentry);
34409 +       if (au_ibbot(inode) < bdst)
34410 +               au_set_ibbot(inode, bdst);
34411 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34412 +                     au_hi_flags(inode, /*isdir*/1));
34413 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34414 +       goto out; /* success */
34415 +
34416 +       /* revert */
34417 +out_opq:
34418 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34419 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34420 +               rerr = au_diropq_remove(dentry, bdst);
34421 +               inode_unlock(h_inode);
34422 +               if (unlikely(rerr)) {
34423 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34424 +                               dentry, bdst, rerr);
34425 +                       err = -EIO;
34426 +                       goto out;
34427 +               }
34428 +       }
34429 +out_dir:
34430 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34431 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34432 +               if (unlikely(rerr)) {
34433 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34434 +                               dentry, bdst, rerr);
34435 +                       err = -EIO;
34436 +               }
34437 +       }
34438 +out_put:
34439 +       au_set_h_dptr(dentry, bdst, NULL);
34440 +       if (au_dbbot(dentry) == bdst)
34441 +               au_update_dbbot(dentry);
34442 +out:
34443 +       dput(parent);
34444 +       return err;
34445 +}
34446 +
34447 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34448 +{
34449 +       int err;
34450 +       unsigned int flags;
34451 +
34452 +       flags = 0;
34453 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34454 +
34455 +       return err;
34456 +}
34457 +
34458 +/* ---------------------------------------------------------------------- */
34459 +
34460 +/* policies for create */
34461 +
34462 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34463 +{
34464 +       int err, i, j, ndentry;
34465 +       aufs_bindex_t bopq;
34466 +       struct au_dcsub_pages dpages;
34467 +       struct au_dpage *dpage;
34468 +       struct dentry **dentries, *parent, *d;
34469 +
34470 +       err = au_dpages_init(&dpages, GFP_NOFS);
34471 +       if (unlikely(err))
34472 +               goto out;
34473 +       parent = dget_parent(dentry);
34474 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34475 +       if (unlikely(err))
34476 +               goto out_free;
34477 +
34478 +       err = bindex;
34479 +       for (i = 0; i < dpages.ndpage; i++) {
34480 +               dpage = dpages.dpages + i;
34481 +               dentries = dpage->dentries;
34482 +               ndentry = dpage->ndentry;
34483 +               for (j = 0; j < ndentry; j++) {
34484 +                       d = dentries[j];
34485 +                       di_read_lock_parent2(d, !AuLock_IR);
34486 +                       bopq = au_dbdiropq(d);
34487 +                       di_read_unlock(d, !AuLock_IR);
34488 +                       if (bopq >= 0 && bopq < err)
34489 +                               err = bopq;
34490 +               }
34491 +       }
34492 +
34493 +out_free:
34494 +       dput(parent);
34495 +       au_dpages_free(&dpages);
34496 +out:
34497 +       return err;
34498 +}
34499 +
34500 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34501 +{
34502 +       for (; bindex >= 0; bindex--)
34503 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34504 +                       return bindex;
34505 +       return -EROFS;
34506 +}
34507 +
34508 +/* top down parent */
34509 +static int au_wbr_create_tdp(struct dentry *dentry,
34510 +                            unsigned int flags __maybe_unused)
34511 +{
34512 +       int err;
34513 +       aufs_bindex_t btop, bindex;
34514 +       struct super_block *sb;
34515 +       struct dentry *parent, *h_parent;
34516 +
34517 +       sb = dentry->d_sb;
34518 +       btop = au_dbtop(dentry);
34519 +       err = btop;
34520 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34521 +               goto out;
34522 +
34523 +       err = -EROFS;
34524 +       parent = dget_parent(dentry);
34525 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34526 +               h_parent = au_h_dptr(parent, bindex);
34527 +               if (!h_parent || d_is_negative(h_parent))
34528 +                       continue;
34529 +
34530 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34531 +                       err = bindex;
34532 +                       break;
34533 +               }
34534 +       }
34535 +       dput(parent);
34536 +
34537 +       /* bottom up here */
34538 +       if (unlikely(err < 0)) {
34539 +               err = au_wbr_bu(sb, btop - 1);
34540 +               if (err >= 0)
34541 +                       err = au_wbr_nonopq(dentry, err);
34542 +       }
34543 +
34544 +out:
34545 +       AuDbg("b%d\n", err);
34546 +       return err;
34547 +}
34548 +
34549 +/* ---------------------------------------------------------------------- */
34550 +
34551 +/* an exception for the policy other than tdp */
34552 +static int au_wbr_create_exp(struct dentry *dentry)
34553 +{
34554 +       int err;
34555 +       aufs_bindex_t bwh, bdiropq;
34556 +       struct dentry *parent;
34557 +
34558 +       err = -1;
34559 +       bwh = au_dbwh(dentry);
34560 +       parent = dget_parent(dentry);
34561 +       bdiropq = au_dbdiropq(parent);
34562 +       if (bwh >= 0) {
34563 +               if (bdiropq >= 0)
34564 +                       err = min(bdiropq, bwh);
34565 +               else
34566 +                       err = bwh;
34567 +               AuDbg("%d\n", err);
34568 +       } else if (bdiropq >= 0) {
34569 +               err = bdiropq;
34570 +               AuDbg("%d\n", err);
34571 +       }
34572 +       dput(parent);
34573 +
34574 +       if (err >= 0)
34575 +               err = au_wbr_nonopq(dentry, err);
34576 +
34577 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34578 +               err = -1;
34579 +
34580 +       AuDbg("%d\n", err);
34581 +       return err;
34582 +}
34583 +
34584 +/* ---------------------------------------------------------------------- */
34585 +
34586 +/* round robin */
34587 +static int au_wbr_create_init_rr(struct super_block *sb)
34588 +{
34589 +       int err;
34590 +
34591 +       err = au_wbr_bu(sb, au_sbbot(sb));
34592 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34593 +       /* smp_mb(); */
34594 +
34595 +       AuDbg("b%d\n", err);
34596 +       return err;
34597 +}
34598 +
34599 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34600 +{
34601 +       int err, nbr;
34602 +       unsigned int u;
34603 +       aufs_bindex_t bindex, bbot;
34604 +       struct super_block *sb;
34605 +       atomic_t *next;
34606 +
34607 +       err = au_wbr_create_exp(dentry);
34608 +       if (err >= 0)
34609 +               goto out;
34610 +
34611 +       sb = dentry->d_sb;
34612 +       next = &au_sbi(sb)->si_wbr_rr_next;
34613 +       bbot = au_sbbot(sb);
34614 +       nbr = bbot + 1;
34615 +       for (bindex = 0; bindex <= bbot; bindex++) {
34616 +               if (!au_ftest_wbr(flags, DIR)) {
34617 +                       err = atomic_dec_return(next) + 1;
34618 +                       /* modulo for 0 is meaningless */
34619 +                       if (unlikely(!err))
34620 +                               err = atomic_dec_return(next) + 1;
34621 +               } else
34622 +                       err = atomic_read(next);
34623 +               AuDbg("%d\n", err);
34624 +               u = err;
34625 +               err = u % nbr;
34626 +               AuDbg("%d\n", err);
34627 +               if (!au_br_rdonly(au_sbr(sb, err)))
34628 +                       break;
34629 +               err = -EROFS;
34630 +       }
34631 +
34632 +       if (err >= 0)
34633 +               err = au_wbr_nonopq(dentry, err);
34634 +
34635 +out:
34636 +       AuDbg("%d\n", err);
34637 +       return err;
34638 +}
34639 +
34640 +/* ---------------------------------------------------------------------- */
34641 +
34642 +/* most free space */
34643 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34644 +{
34645 +       struct super_block *sb;
34646 +       struct au_branch *br;
34647 +       struct au_wbr_mfs *mfs;
34648 +       struct dentry *h_parent;
34649 +       aufs_bindex_t bindex, bbot;
34650 +       int err;
34651 +       unsigned long long b, bavail;
34652 +       struct path h_path;
34653 +       /* reduce the stack usage */
34654 +       struct kstatfs *st;
34655 +
34656 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34657 +       if (unlikely(!st)) {
34658 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34659 +               return;
34660 +       }
34661 +
34662 +       bavail = 0;
34663 +       sb = dentry->d_sb;
34664 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34665 +       MtxMustLock(&mfs->mfs_lock);
34666 +       mfs->mfs_bindex = -EROFS;
34667 +       mfs->mfsrr_bytes = 0;
34668 +       if (!parent) {
34669 +               bindex = 0;
34670 +               bbot = au_sbbot(sb);
34671 +       } else {
34672 +               bindex = au_dbtop(parent);
34673 +               bbot = au_dbtaildir(parent);
34674 +       }
34675 +
34676 +       for (; bindex <= bbot; bindex++) {
34677 +               if (parent) {
34678 +                       h_parent = au_h_dptr(parent, bindex);
34679 +                       if (!h_parent || d_is_negative(h_parent))
34680 +                               continue;
34681 +               }
34682 +               br = au_sbr(sb, bindex);
34683 +               if (au_br_rdonly(br))
34684 +                       continue;
34685 +
34686 +               /* sb->s_root for NFS is unreliable */
34687 +               h_path.mnt = au_br_mnt(br);
34688 +               h_path.dentry = h_path.mnt->mnt_root;
34689 +               err = vfs_statfs(&h_path, st);
34690 +               if (unlikely(err)) {
34691 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34692 +                       continue;
34693 +               }
34694 +
34695 +               /* when the available size is equal, select the lower one */
34696 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34697 +                            || sizeof(b) < sizeof(st->f_bsize));
34698 +               b = st->f_bavail * st->f_bsize;
34699 +               br->br_wbr->wbr_bytes = b;
34700 +               if (b >= bavail) {
34701 +                       bavail = b;
34702 +                       mfs->mfs_bindex = bindex;
34703 +                       mfs->mfs_jiffy = jiffies;
34704 +               }
34705 +       }
34706 +
34707 +       mfs->mfsrr_bytes = bavail;
34708 +       AuDbg("b%d\n", mfs->mfs_bindex);
34709 +       au_kfree_rcu(st);
34710 +}
34711 +
34712 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34713 +{
34714 +       int err;
34715 +       struct dentry *parent;
34716 +       struct super_block *sb;
34717 +       struct au_wbr_mfs *mfs;
34718 +
34719 +       err = au_wbr_create_exp(dentry);
34720 +       if (err >= 0)
34721 +               goto out;
34722 +
34723 +       sb = dentry->d_sb;
34724 +       parent = NULL;
34725 +       if (au_ftest_wbr(flags, PARENT))
34726 +               parent = dget_parent(dentry);
34727 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34728 +       mutex_lock(&mfs->mfs_lock);
34729 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34730 +           || mfs->mfs_bindex < 0
34731 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34732 +               au_mfs(dentry, parent);
34733 +       mutex_unlock(&mfs->mfs_lock);
34734 +       err = mfs->mfs_bindex;
34735 +       dput(parent);
34736 +
34737 +       if (err >= 0)
34738 +               err = au_wbr_nonopq(dentry, err);
34739 +
34740 +out:
34741 +       AuDbg("b%d\n", err);
34742 +       return err;
34743 +}
34744 +
34745 +static int au_wbr_create_init_mfs(struct super_block *sb)
34746 +{
34747 +       struct au_wbr_mfs *mfs;
34748 +
34749 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34750 +       mutex_init(&mfs->mfs_lock);
34751 +       mfs->mfs_jiffy = 0;
34752 +       mfs->mfs_bindex = -EROFS;
34753 +
34754 +       return 0;
34755 +}
34756 +
34757 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34758 +{
34759 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34760 +       return 0;
34761 +}
34762 +
34763 +/* ---------------------------------------------------------------------- */
34764 +
34765 +/* top down regardless parent, and then mfs */
34766 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34767 +                              unsigned int flags __maybe_unused)
34768 +{
34769 +       int err;
34770 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34771 +       unsigned long long watermark;
34772 +       struct super_block *sb;
34773 +       struct au_wbr_mfs *mfs;
34774 +       struct au_branch *br;
34775 +       struct dentry *parent;
34776 +
34777 +       sb = dentry->d_sb;
34778 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34779 +       mutex_lock(&mfs->mfs_lock);
34780 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34781 +           || mfs->mfs_bindex < 0)
34782 +               au_mfs(dentry, /*parent*/NULL);
34783 +       watermark = mfs->mfsrr_watermark;
34784 +       bmfs = mfs->mfs_bindex;
34785 +       mutex_unlock(&mfs->mfs_lock);
34786 +
34787 +       /* another style of au_wbr_create_exp() */
34788 +       bwh = au_dbwh(dentry);
34789 +       parent = dget_parent(dentry);
34790 +       btail = au_dbtaildir(parent);
34791 +       if (bwh >= 0 && bwh < btail)
34792 +               btail = bwh;
34793 +
34794 +       err = au_wbr_nonopq(dentry, btail);
34795 +       if (unlikely(err < 0))
34796 +               goto out;
34797 +       btail = err;
34798 +       bfound = -1;
34799 +       for (bindex = 0; bindex <= btail; bindex++) {
34800 +               br = au_sbr(sb, bindex);
34801 +               if (au_br_rdonly(br))
34802 +                       continue;
34803 +               if (br->br_wbr->wbr_bytes > watermark) {
34804 +                       bfound = bindex;
34805 +                       break;
34806 +               }
34807 +       }
34808 +       err = bfound;
34809 +       if (err < 0)
34810 +               err = bmfs;
34811 +
34812 +out:
34813 +       dput(parent);
34814 +       AuDbg("b%d\n", err);
34815 +       return err;
34816 +}
34817 +
34818 +/* ---------------------------------------------------------------------- */
34819 +
34820 +/* most free space and then round robin */
34821 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34822 +{
34823 +       int err;
34824 +       struct au_wbr_mfs *mfs;
34825 +
34826 +       err = au_wbr_create_mfs(dentry, flags);
34827 +       if (err >= 0) {
34828 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34829 +               mutex_lock(&mfs->mfs_lock);
34830 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34831 +                       err = au_wbr_create_rr(dentry, flags);
34832 +               mutex_unlock(&mfs->mfs_lock);
34833 +       }
34834 +
34835 +       AuDbg("b%d\n", err);
34836 +       return err;
34837 +}
34838 +
34839 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34840 +{
34841 +       int err;
34842 +
34843 +       au_wbr_create_init_mfs(sb); /* ignore */
34844 +       err = au_wbr_create_init_rr(sb);
34845 +
34846 +       return err;
34847 +}
34848 +
34849 +/* ---------------------------------------------------------------------- */
34850 +
34851 +/* top down parent and most free space */
34852 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34853 +{
34854 +       int err, e2;
34855 +       unsigned long long b;
34856 +       aufs_bindex_t bindex, btop, bbot;
34857 +       struct super_block *sb;
34858 +       struct dentry *parent, *h_parent;
34859 +       struct au_branch *br;
34860 +
34861 +       err = au_wbr_create_tdp(dentry, flags);
34862 +       if (unlikely(err < 0))
34863 +               goto out;
34864 +       parent = dget_parent(dentry);
34865 +       btop = au_dbtop(parent);
34866 +       bbot = au_dbtaildir(parent);
34867 +       if (btop == bbot)
34868 +               goto out_parent; /* success */
34869 +
34870 +       e2 = au_wbr_create_mfs(dentry, flags);
34871 +       if (e2 < 0)
34872 +               goto out_parent; /* success */
34873 +
34874 +       /* when the available size is equal, select upper one */
34875 +       sb = dentry->d_sb;
34876 +       br = au_sbr(sb, err);
34877 +       b = br->br_wbr->wbr_bytes;
34878 +       AuDbg("b%d, %llu\n", err, b);
34879 +
34880 +       for (bindex = btop; bindex <= bbot; bindex++) {
34881 +               h_parent = au_h_dptr(parent, bindex);
34882 +               if (!h_parent || d_is_negative(h_parent))
34883 +                       continue;
34884 +
34885 +               br = au_sbr(sb, bindex);
34886 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34887 +                       b = br->br_wbr->wbr_bytes;
34888 +                       err = bindex;
34889 +                       AuDbg("b%d, %llu\n", err, b);
34890 +               }
34891 +       }
34892 +
34893 +       if (err >= 0)
34894 +               err = au_wbr_nonopq(dentry, err);
34895 +
34896 +out_parent:
34897 +       dput(parent);
34898 +out:
34899 +       AuDbg("b%d\n", err);
34900 +       return err;
34901 +}
34902 +
34903 +/* ---------------------------------------------------------------------- */
34904 +
34905 +/*
34906 + * - top down parent
34907 + * - most free space with parent
34908 + * - most free space round-robin regardless parent
34909 + */
34910 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34911 +{
34912 +       int err;
34913 +       unsigned long long watermark;
34914 +       struct super_block *sb;
34915 +       struct au_branch *br;
34916 +       struct au_wbr_mfs *mfs;
34917 +
34918 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34919 +       if (unlikely(err < 0))
34920 +               goto out;
34921 +
34922 +       sb = dentry->d_sb;
34923 +       br = au_sbr(sb, err);
34924 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34925 +       mutex_lock(&mfs->mfs_lock);
34926 +       watermark = mfs->mfsrr_watermark;
34927 +       mutex_unlock(&mfs->mfs_lock);
34928 +       if (br->br_wbr->wbr_bytes < watermark)
34929 +               /* regardless the parent dir */
34930 +               err = au_wbr_create_mfsrr(dentry, flags);
34931 +
34932 +out:
34933 +       AuDbg("b%d\n", err);
34934 +       return err;
34935 +}
34936 +
34937 +/* ---------------------------------------------------------------------- */
34938 +
34939 +/* policies for copyup */
34940 +
34941 +/* top down parent */
34942 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34943 +{
34944 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34945 +}
34946 +
34947 +/* bottom up parent */
34948 +static int au_wbr_copyup_bup(struct dentry *dentry)
34949 +{
34950 +       int err;
34951 +       aufs_bindex_t bindex, btop;
34952 +       struct dentry *parent, *h_parent;
34953 +       struct super_block *sb;
34954 +
34955 +       err = -EROFS;
34956 +       sb = dentry->d_sb;
34957 +       parent = dget_parent(dentry);
34958 +       btop = au_dbtop(parent);
34959 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34960 +               h_parent = au_h_dptr(parent, bindex);
34961 +               if (!h_parent || d_is_negative(h_parent))
34962 +                       continue;
34963 +
34964 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34965 +                       err = bindex;
34966 +                       break;
34967 +               }
34968 +       }
34969 +       dput(parent);
34970 +
34971 +       /* bottom up here */
34972 +       if (unlikely(err < 0))
34973 +               err = au_wbr_bu(sb, btop - 1);
34974 +
34975 +       AuDbg("b%d\n", err);
34976 +       return err;
34977 +}
34978 +
34979 +/* bottom up */
34980 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34981 +{
34982 +       int err;
34983 +
34984 +       err = au_wbr_bu(dentry->d_sb, btop);
34985 +       AuDbg("b%d\n", err);
34986 +       if (err > btop)
34987 +               err = au_wbr_nonopq(dentry, err);
34988 +
34989 +       AuDbg("b%d\n", err);
34990 +       return err;
34991 +}
34992 +
34993 +static int au_wbr_copyup_bu(struct dentry *dentry)
34994 +{
34995 +       int err;
34996 +       aufs_bindex_t btop;
34997 +
34998 +       btop = au_dbtop(dentry);
34999 +       err = au_wbr_do_copyup_bu(dentry, btop);
35000 +       return err;
35001 +}
35002 +
35003 +/* ---------------------------------------------------------------------- */
35004 +
35005 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
35006 +       [AuWbrCopyup_TDP] = {
35007 +               .copyup = au_wbr_copyup_tdp
35008 +       },
35009 +       [AuWbrCopyup_BUP] = {
35010 +               .copyup = au_wbr_copyup_bup
35011 +       },
35012 +       [AuWbrCopyup_BU] = {
35013 +               .copyup = au_wbr_copyup_bu
35014 +       }
35015 +};
35016 +
35017 +struct au_wbr_create_operations au_wbr_create_ops[] = {
35018 +       [AuWbrCreate_TDP] = {
35019 +               .create = au_wbr_create_tdp
35020 +       },
35021 +       [AuWbrCreate_RR] = {
35022 +               .create = au_wbr_create_rr,
35023 +               .init   = au_wbr_create_init_rr
35024 +       },
35025 +       [AuWbrCreate_MFS] = {
35026 +               .create = au_wbr_create_mfs,
35027 +               .init   = au_wbr_create_init_mfs,
35028 +               .fin    = au_wbr_create_fin_mfs
35029 +       },
35030 +       [AuWbrCreate_MFSV] = {
35031 +               .create = au_wbr_create_mfs,
35032 +               .init   = au_wbr_create_init_mfs,
35033 +               .fin    = au_wbr_create_fin_mfs
35034 +       },
35035 +       [AuWbrCreate_MFSRR] = {
35036 +               .create = au_wbr_create_mfsrr,
35037 +               .init   = au_wbr_create_init_mfsrr,
35038 +               .fin    = au_wbr_create_fin_mfs
35039 +       },
35040 +       [AuWbrCreate_MFSRRV] = {
35041 +               .create = au_wbr_create_mfsrr,
35042 +               .init   = au_wbr_create_init_mfsrr,
35043 +               .fin    = au_wbr_create_fin_mfs
35044 +       },
35045 +       [AuWbrCreate_TDMFS] = {
35046 +               .create = au_wbr_create_tdmfs,
35047 +               .init   = au_wbr_create_init_mfs,
35048 +               .fin    = au_wbr_create_fin_mfs
35049 +       },
35050 +       [AuWbrCreate_TDMFSV] = {
35051 +               .create = au_wbr_create_tdmfs,
35052 +               .init   = au_wbr_create_init_mfs,
35053 +               .fin    = au_wbr_create_fin_mfs
35054 +       },
35055 +       [AuWbrCreate_PMFS] = {
35056 +               .create = au_wbr_create_pmfs,
35057 +               .init   = au_wbr_create_init_mfs,
35058 +               .fin    = au_wbr_create_fin_mfs
35059 +       },
35060 +       [AuWbrCreate_PMFSV] = {
35061 +               .create = au_wbr_create_pmfs,
35062 +               .init   = au_wbr_create_init_mfs,
35063 +               .fin    = au_wbr_create_fin_mfs
35064 +       },
35065 +       [AuWbrCreate_PMFSRR] = {
35066 +               .create = au_wbr_create_pmfsrr,
35067 +               .init   = au_wbr_create_init_mfsrr,
35068 +               .fin    = au_wbr_create_fin_mfs
35069 +       },
35070 +       [AuWbrCreate_PMFSRRV] = {
35071 +               .create = au_wbr_create_pmfsrr,
35072 +               .init   = au_wbr_create_init_mfsrr,
35073 +               .fin    = au_wbr_create_fin_mfs
35074 +       }
35075 +};
35076 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35077 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35078 +++ linux/fs/aufs/whout.c       2022-11-05 23:02:18.972555950 +0100
35079 @@ -0,0 +1,1072 @@
35080 +// SPDX-License-Identifier: GPL-2.0
35081 +/*
35082 + * Copyright (C) 2005-2022 Junjiro R. Okajima
35083 + *
35084 + * This program is free software; you can redistribute it and/or modify
35085 + * it under the terms of the GNU General Public License as published by
35086 + * the Free Software Foundation; either version 2 of the License, or
35087 + * (at your option) any later version.
35088 + *
35089 + * This program is distributed in the hope that it will be useful,
35090 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35091 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35092 + * GNU General Public License for more details.
35093 + *
35094 + * You should have received a copy of the GNU General Public License
35095 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35096 + */
35097 +
35098 +/*
35099 + * whiteout for logical deletion and opaque directory
35100 + */
35101 +
35102 +#include "aufs.h"
35103 +
35104 +#define WH_MASK                        0444
35105 +
35106 +/*
35107 + * If a directory contains this file, then it is opaque.  We start with the
35108 + * .wh. flag so that it is blocked by lookup.
35109 + */
35110 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35111 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35112 +
35113 +/*
35114 + * generate whiteout name, which is NOT terminated by NULL.
35115 + * @name: original d_name.name
35116 + * @len: original d_name.len
35117 + * @wh: whiteout qstr
35118 + * returns zero when succeeds, otherwise error.
35119 + * succeeded value as wh->name should be freed by kfree().
35120 + */
35121 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35122 +{
35123 +       char *p;
35124 +
35125 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35126 +               return -ENAMETOOLONG;
35127 +
35128 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35129 +       p = kmalloc(wh->len, GFP_NOFS);
35130 +       wh->name = p;
35131 +       if (p) {
35132 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35133 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35134 +               /* smp_mb(); */
35135 +               return 0;
35136 +       }
35137 +       return -ENOMEM;
35138 +}
35139 +
35140 +/* ---------------------------------------------------------------------- */
35141 +
35142 +/*
35143 + * test if the @wh_name exists under @h_ppath.
35144 + * @try_sio specifies the necessary of super-io.
35145 + */
35146 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35147 +              struct qstr *wh_name, int try_sio)
35148 +{
35149 +       int err;
35150 +       struct dentry *wh_dentry;
35151 +
35152 +       if (!try_sio)
35153 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35154 +       else
35155 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
35156 +       err = PTR_ERR(wh_dentry);
35157 +       if (IS_ERR(wh_dentry)) {
35158 +               if (err == -ENAMETOOLONG)
35159 +                       err = 0;
35160 +               goto out;
35161 +       }
35162 +
35163 +       err = 0;
35164 +       if (d_is_negative(wh_dentry))
35165 +               goto out_wh; /* success */
35166 +
35167 +       err = 1;
35168 +       if (d_is_reg(wh_dentry))
35169 +               goto out_wh; /* success */
35170 +
35171 +       err = -EIO;
35172 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35173 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35174 +
35175 +out_wh:
35176 +       dput(wh_dentry);
35177 +out:
35178 +       return err;
35179 +}
35180 +
35181 +/*
35182 + * test if the @h_path->dentry sets opaque or not.
35183 + */
35184 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
35185 +{
35186 +       int err;
35187 +       struct inode *h_dir;
35188 +
35189 +       h_dir = d_inode(h_path->dentry);
35190 +       err = au_wh_test(h_userns, h_path, &diropq_name,
35191 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
35192 +       return err;
35193 +}
35194 +
35195 +/*
35196 + * returns a negative dentry whose name is unique and temporary.
35197 + */
35198 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35199 +                            struct qstr *prefix)
35200 +{
35201 +       struct dentry *dentry;
35202 +       int i;
35203 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35204 +               *name, *p;
35205 +       /* strict atomic_t is unnecessary here */
35206 +       static unsigned short cnt;
35207 +       struct qstr qs;
35208 +       struct path h_ppath;
35209 +       struct user_namespace *h_userns;
35210 +
35211 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35212 +
35213 +       name = defname;
35214 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35215 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35216 +               dentry = ERR_PTR(-ENAMETOOLONG);
35217 +               if (unlikely(qs.len > NAME_MAX))
35218 +                       goto out;
35219 +               dentry = ERR_PTR(-ENOMEM);
35220 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35221 +               if (unlikely(!name))
35222 +                       goto out;
35223 +       }
35224 +
35225 +       /* doubly whiteout-ed */
35226 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35227 +       p = name + AUFS_WH_PFX_LEN * 2;
35228 +       memcpy(p, prefix->name, prefix->len);
35229 +       p += prefix->len;
35230 +       *p++ = '.';
35231 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35232 +
35233 +       h_ppath.dentry = h_parent;
35234 +       h_ppath.mnt = au_br_mnt(br);
35235 +       h_userns = au_br_userns(br);
35236 +       qs.name = name;
35237 +       for (i = 0; i < 3; i++) {
35238 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35239 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
35240 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35241 +                       goto out_name;
35242 +               dput(dentry);
35243 +       }
35244 +       /* pr_warn("could not get random name\n"); */
35245 +       dentry = ERR_PTR(-EEXIST);
35246 +       AuDbg("%.*s\n", AuLNPair(&qs));
35247 +       BUG();
35248 +
35249 +out_name:
35250 +       if (name != defname)
35251 +               au_kfree_try_rcu(name);
35252 +out:
35253 +       AuTraceErrPtr(dentry);
35254 +       return dentry;
35255 +}
35256 +
35257 +/*
35258 + * rename the @h_dentry on @br to the whiteouted temporary name.
35259 + */
35260 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35261 +{
35262 +       int err;
35263 +       struct path h_path = {
35264 +               .mnt = au_br_mnt(br)
35265 +       };
35266 +       struct inode *h_dir, *delegated;
35267 +       struct dentry *h_parent;
35268 +
35269 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35270 +       h_dir = d_inode(h_parent);
35271 +       IMustLock(h_dir);
35272 +
35273 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35274 +       err = PTR_ERR(h_path.dentry);
35275 +       if (IS_ERR(h_path.dentry))
35276 +               goto out;
35277 +
35278 +       /* under the same dir, no need to lock_rename() */
35279 +       delegated = NULL;
35280 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35281 +                          /*flags*/0);
35282 +       AuTraceErr(err);
35283 +       if (unlikely(err == -EWOULDBLOCK)) {
35284 +               pr_warn("cannot retry for NFSv4 delegation"
35285 +                       " for an internal rename\n");
35286 +               iput(delegated);
35287 +       }
35288 +       dput(h_path.dentry);
35289 +
35290 +out:
35291 +       AuTraceErr(err);
35292 +       return err;
35293 +}
35294 +
35295 +/* ---------------------------------------------------------------------- */
35296 +/*
35297 + * functions for removing a whiteout
35298 + */
35299 +
35300 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35301 +{
35302 +       int err, force;
35303 +       struct inode *delegated;
35304 +
35305 +       /*
35306 +        * forces superio when the dir has a sticky bit.
35307 +        * this may be a violation of unix fs semantics.
35308 +        */
35309 +       force = (h_dir->i_mode & S_ISVTX)
35310 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35311 +       delegated = NULL;
35312 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35313 +       if (unlikely(err == -EWOULDBLOCK)) {
35314 +               pr_warn("cannot retry for NFSv4 delegation"
35315 +                       " for an internal unlink\n");
35316 +               iput(delegated);
35317 +       }
35318 +       return err;
35319 +}
35320 +
35321 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35322 +                       struct dentry *dentry)
35323 +{
35324 +       int err;
35325 +
35326 +       err = do_unlink_wh(h_dir, h_path);
35327 +       if (!err && dentry)
35328 +               au_set_dbwh(dentry, -1);
35329 +
35330 +       return err;
35331 +}
35332 +
35333 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35334 +{
35335 +       int err;
35336 +       struct path h_path;
35337 +
35338 +       err = 0;
35339 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35340 +       if (IS_ERR(h_path.dentry))
35341 +               err = PTR_ERR(h_path.dentry);
35342 +       else {
35343 +               if (d_is_reg(h_path.dentry)) {
35344 +                       h_path.mnt = h_ppath->mnt;
35345 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35346 +               }
35347 +               dput(h_path.dentry);
35348 +       }
35349 +
35350 +       return err;
35351 +}
35352 +
35353 +/* ---------------------------------------------------------------------- */
35354 +/*
35355 + * initialize/clean whiteout for a branch
35356 + */
35357 +
35358 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35359 +                       const int isdir)
35360 +{
35361 +       int err;
35362 +       struct inode *delegated;
35363 +
35364 +       if (d_is_negative(whpath->dentry))
35365 +               return;
35366 +
35367 +       if (isdir)
35368 +               err = vfsub_rmdir(h_dir, whpath);
35369 +       else {
35370 +               delegated = NULL;
35371 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35372 +               if (unlikely(err == -EWOULDBLOCK)) {
35373 +                       pr_warn("cannot retry for NFSv4 delegation"
35374 +                               " for an internal unlink\n");
35375 +                       iput(delegated);
35376 +               }
35377 +       }
35378 +       if (unlikely(err))
35379 +               pr_warn("failed removing %pd (%d), ignored.\n",
35380 +                       whpath->dentry, err);
35381 +}
35382 +
35383 +static int test_linkable(struct dentry *h_root)
35384 +{
35385 +       struct inode *h_dir = d_inode(h_root);
35386 +
35387 +       if (h_dir->i_op->link)
35388 +               return 0;
35389 +
35390 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35391 +              h_root, au_sbtype(h_root->d_sb));
35392 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35393 +}
35394 +
35395 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35396 +static int au_whdir(struct inode *h_dir, struct path *path)
35397 +{
35398 +       int err;
35399 +
35400 +       err = -EEXIST;
35401 +       if (d_is_negative(path->dentry)) {
35402 +               int mode = 0700;
35403 +
35404 +               if (au_test_nfs(path->dentry->d_sb))
35405 +                       mode |= 0111;
35406 +               err = vfsub_mkdir(h_dir, path, mode);
35407 +       } else if (d_is_dir(path->dentry))
35408 +               err = 0;
35409 +       else
35410 +               pr_err("unknown %pd exists\n", path->dentry);
35411 +
35412 +       return err;
35413 +}
35414 +
35415 +struct au_wh_base {
35416 +       const struct qstr *name;
35417 +       struct dentry *dentry;
35418 +};
35419 +
35420 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35421 +                         struct path *h_path)
35422 +{
35423 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35424 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35425 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35426 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35427 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35428 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35429 +}
35430 +
35431 +/*
35432 + * returns tri-state,
35433 + * minus: error, caller should print the message
35434 + * zero: success
35435 + * plus: error, caller should NOT print the message
35436 + */
35437 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35438 +                               int do_plink, struct au_wh_base base[],
35439 +                               struct path *h_path)
35440 +{
35441 +       int err;
35442 +       struct inode *h_dir;
35443 +
35444 +       h_dir = d_inode(h_root);
35445 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35446 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35447 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35448 +       if (do_plink) {
35449 +               err = test_linkable(h_root);
35450 +               if (unlikely(err)) {
35451 +                       err = 1;
35452 +                       goto out;
35453 +               }
35454 +
35455 +               err = au_whdir(h_dir, h_path);
35456 +               if (unlikely(err))
35457 +                       goto out;
35458 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35459 +       } else
35460 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35461 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35462 +       err = au_whdir(h_dir, h_path);
35463 +       if (unlikely(err))
35464 +               goto out;
35465 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35466 +
35467 +out:
35468 +       return err;
35469 +}
35470 +
35471 +/*
35472 + * for the moment, aufs supports the branch filesystem which does not support
35473 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35474 + * copyup failed. finally, such filesystem will not be used as the writable
35475 + * branch.
35476 + *
35477 + * returns tri-state, see above.
35478 + */
35479 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35480 +                        int do_plink, struct au_wh_base base[],
35481 +                        struct path *h_path)
35482 +{
35483 +       int err;
35484 +       struct inode *h_dir;
35485 +
35486 +       WbrWhMustWriteLock(wbr);
35487 +
35488 +       err = test_linkable(h_root);
35489 +       if (unlikely(err)) {
35490 +               err = 1;
35491 +               goto out;
35492 +       }
35493 +
35494 +       /*
35495 +        * todo: should this create be done in /sbin/mount.aufs helper?
35496 +        */
35497 +       err = -EEXIST;
35498 +       h_dir = d_inode(h_root);
35499 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35500 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35501 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35502 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35503 +               err = 0;
35504 +       else
35505 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35506 +       if (unlikely(err))
35507 +               goto out;
35508 +
35509 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35510 +       if (do_plink) {
35511 +               err = au_whdir(h_dir, h_path);
35512 +               if (unlikely(err))
35513 +                       goto out;
35514 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35515 +       } else
35516 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35517 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35518 +
35519 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35520 +       err = au_whdir(h_dir, h_path);
35521 +       if (unlikely(err))
35522 +               goto out;
35523 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35524 +
35525 +out:
35526 +       return err;
35527 +}
35528 +
35529 +/*
35530 + * initialize the whiteout base file/dir for @br.
35531 + */
35532 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35533 +{
35534 +       int err, i;
35535 +       const unsigned char do_plink
35536 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35537 +       struct inode *h_dir;
35538 +       struct path path = br->br_path;
35539 +       struct dentry *h_root = path.dentry;
35540 +       struct au_wbr *wbr = br->br_wbr;
35541 +       static const struct qstr base_name[] = {
35542 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35543 +                                         sizeof(AUFS_BASE_NAME) - 1),
35544 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35545 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35546 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35547 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35548 +       };
35549 +       struct au_wh_base base[] = {
35550 +               [AuBrWh_BASE] = {
35551 +                       .name   = base_name + AuBrWh_BASE,
35552 +                       .dentry = NULL
35553 +               },
35554 +               [AuBrWh_PLINK] = {
35555 +                       .name   = base_name + AuBrWh_PLINK,
35556 +                       .dentry = NULL
35557 +               },
35558 +               [AuBrWh_ORPH] = {
35559 +                       .name   = base_name + AuBrWh_ORPH,
35560 +                       .dentry = NULL
35561 +               }
35562 +       };
35563 +
35564 +       if (wbr)
35565 +               WbrWhMustWriteLock(wbr);
35566 +
35567 +       for (i = 0; i < AuBrWh_Last; i++) {
35568 +               /* doubly whiteouted */
35569 +               struct dentry *d;
35570 +
35571 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35572 +               err = PTR_ERR(d);
35573 +               if (IS_ERR(d))
35574 +                       goto out;
35575 +
35576 +               base[i].dentry = d;
35577 +               AuDebugOn(wbr
35578 +                         && wbr->wbr_wh[i]
35579 +                         && wbr->wbr_wh[i] != base[i].dentry);
35580 +       }
35581 +
35582 +       if (wbr)
35583 +               for (i = 0; i < AuBrWh_Last; i++) {
35584 +                       dput(wbr->wbr_wh[i]);
35585 +                       wbr->wbr_wh[i] = NULL;
35586 +               }
35587 +
35588 +       err = 0;
35589 +       if (!au_br_writable(br->br_perm)) {
35590 +               h_dir = d_inode(h_root);
35591 +               au_wh_init_ro(h_dir, base, &path);
35592 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35593 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35594 +               if (err > 0)
35595 +                       goto out;
35596 +               else if (err)
35597 +                       goto out_err;
35598 +       } else {
35599 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35600 +               if (err > 0)
35601 +                       goto out;
35602 +               else if (err)
35603 +                       goto out_err;
35604 +       }
35605 +       goto out; /* success */
35606 +
35607 +out_err:
35608 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35609 +              err, h_root, au_sbtype(h_root->d_sb));
35610 +out:
35611 +       for (i = 0; i < AuBrWh_Last; i++)
35612 +               dput(base[i].dentry);
35613 +       return err;
35614 +}
35615 +
35616 +/* ---------------------------------------------------------------------- */
35617 +/*
35618 + * whiteouts are all hard-linked usually.
35619 + * when its link count reaches a ceiling, we create a new whiteout base
35620 + * asynchronously.
35621 + */
35622 +
35623 +struct reinit_br_wh {
35624 +       struct super_block *sb;
35625 +       struct au_branch *br;
35626 +};
35627 +
35628 +static void reinit_br_wh(void *arg)
35629 +{
35630 +       int err;
35631 +       aufs_bindex_t bindex;
35632 +       struct path h_path;
35633 +       struct reinit_br_wh *a = arg;
35634 +       struct au_wbr *wbr;
35635 +       struct inode *dir, *delegated;
35636 +       struct dentry *h_root;
35637 +       struct au_hinode *hdir;
35638 +
35639 +       err = 0;
35640 +       wbr = a->br->br_wbr;
35641 +       /* big aufs lock */
35642 +       si_noflush_write_lock(a->sb);
35643 +       if (!au_br_writable(a->br->br_perm))
35644 +               goto out;
35645 +       bindex = au_br_index(a->sb, a->br->br_id);
35646 +       if (unlikely(bindex < 0))
35647 +               goto out;
35648 +
35649 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35650 +       dir = d_inode(a->sb->s_root);
35651 +       hdir = au_hi(dir, bindex);
35652 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35653 +       AuDebugOn(h_root != au_br_dentry(a->br));
35654 +
35655 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35656 +       wbr_wh_write_lock(wbr);
35657 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35658 +                         h_root, a->br);
35659 +       if (!err) {
35660 +               h_path.dentry = wbr->wbr_whbase;
35661 +               h_path.mnt = au_br_mnt(a->br);
35662 +               delegated = NULL;
35663 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35664 +                                  /*force*/0);
35665 +               if (unlikely(err == -EWOULDBLOCK)) {
35666 +                       pr_warn("cannot retry for NFSv4 delegation"
35667 +                               " for an internal unlink\n");
35668 +                       iput(delegated);
35669 +               }
35670 +       } else {
35671 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35672 +               err = 0;
35673 +       }
35674 +       dput(wbr->wbr_whbase);
35675 +       wbr->wbr_whbase = NULL;
35676 +       if (!err)
35677 +               err = au_wh_init(a->br, a->sb);
35678 +       wbr_wh_write_unlock(wbr);
35679 +       au_hn_inode_unlock(hdir);
35680 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35681 +       if (!err)
35682 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35683 +
35684 +out:
35685 +       if (wbr)
35686 +               atomic_dec(&wbr->wbr_wh_running);
35687 +       au_lcnt_dec(&a->br->br_count);
35688 +       si_write_unlock(a->sb);
35689 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35690 +       au_kfree_rcu(a);
35691 +       if (unlikely(err))
35692 +               AuIOErr("err %d\n", err);
35693 +}
35694 +
35695 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35696 +{
35697 +       int do_dec, wkq_err;
35698 +       struct reinit_br_wh *arg;
35699 +
35700 +       do_dec = 1;
35701 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35702 +               goto out;
35703 +
35704 +       /* ignore ENOMEM */
35705 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35706 +       if (arg) {
35707 +               /*
35708 +                * dec(wh_running), kfree(arg) and dec(br_count)
35709 +                * in reinit function
35710 +                */
35711 +               arg->sb = sb;
35712 +               arg->br = br;
35713 +               au_lcnt_inc(&br->br_count);
35714 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35715 +               if (unlikely(wkq_err)) {
35716 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35717 +                       au_lcnt_dec(&br->br_count);
35718 +                       au_kfree_rcu(arg);
35719 +               }
35720 +               do_dec = 0;
35721 +       }
35722 +
35723 +out:
35724 +       if (do_dec)
35725 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35726 +}
35727 +
35728 +/* ---------------------------------------------------------------------- */
35729 +
35730 +/*
35731 + * create the whiteout @wh.
35732 + */
35733 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35734 +                            struct dentry *wh)
35735 +{
35736 +       int err;
35737 +       struct path h_path = {
35738 +               .dentry = wh
35739 +       };
35740 +       struct au_branch *br;
35741 +       struct au_wbr *wbr;
35742 +       struct dentry *h_parent;
35743 +       struct inode *h_dir, *delegated;
35744 +
35745 +       h_parent = wh->d_parent; /* dir inode is locked */
35746 +       h_dir = d_inode(h_parent);
35747 +       IMustLock(h_dir);
35748 +
35749 +       br = au_sbr(sb, bindex);
35750 +       h_path.mnt = au_br_mnt(br);
35751 +       wbr = br->br_wbr;
35752 +       wbr_wh_read_lock(wbr);
35753 +       if (wbr->wbr_whbase) {
35754 +               delegated = NULL;
35755 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35756 +               if (unlikely(err == -EWOULDBLOCK)) {
35757 +                       pr_warn("cannot retry for NFSv4 delegation"
35758 +                               " for an internal link\n");
35759 +                       iput(delegated);
35760 +               }
35761 +               if (!err || err != -EMLINK)
35762 +                       goto out;
35763 +
35764 +               /* link count full. re-initialize br_whbase. */
35765 +               kick_reinit_br_wh(sb, br);
35766 +       }
35767 +
35768 +       /* return this error in this context */
35769 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35770 +       if (!err)
35771 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35772 +
35773 +out:
35774 +       wbr_wh_read_unlock(wbr);
35775 +       return err;
35776 +}
35777 +
35778 +/* ---------------------------------------------------------------------- */
35779 +
35780 +/*
35781 + * create or remove the diropq.
35782 + */
35783 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35784 +                               unsigned int flags)
35785 +{
35786 +       struct dentry *opq_dentry;
35787 +       struct super_block *sb;
35788 +       struct au_branch *br;
35789 +       struct path h_path;
35790 +       int err;
35791 +
35792 +       sb = dentry->d_sb;
35793 +       br = au_sbr(sb, bindex);
35794 +       h_path.dentry = au_h_dptr(dentry, bindex);
35795 +       h_path.mnt = au_br_mnt(br);
35796 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35797 +       if (IS_ERR(opq_dentry))
35798 +               goto out;
35799 +
35800 +       if (au_ftest_diropq(flags, CREATE)) {
35801 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35802 +               if (!err) {
35803 +                       au_set_dbdiropq(dentry, bindex);
35804 +                       goto out; /* success */
35805 +               }
35806 +       } else {
35807 +               h_path.dentry = opq_dentry;
35808 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35809 +               if (!err)
35810 +                       au_set_dbdiropq(dentry, -1);
35811 +       }
35812 +       dput(opq_dentry);
35813 +       opq_dentry = ERR_PTR(err);
35814 +
35815 +out:
35816 +       return opq_dentry;
35817 +}
35818 +
35819 +struct do_diropq_args {
35820 +       struct dentry **errp;
35821 +       struct dentry *dentry;
35822 +       aufs_bindex_t bindex;
35823 +       unsigned int flags;
35824 +};
35825 +
35826 +static void call_do_diropq(void *args)
35827 +{
35828 +       struct do_diropq_args *a = args;
35829 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35830 +}
35831 +
35832 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35833 +                            unsigned int flags)
35834 +{
35835 +       struct dentry *diropq, *h_dentry;
35836 +       struct user_namespace *h_userns;
35837 +
35838 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35839 +       h_dentry = au_h_dptr(dentry, bindex);
35840 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35841 +                               MAY_EXEC | MAY_WRITE))
35842 +               diropq = do_diropq(dentry, bindex, flags);
35843 +       else {
35844 +               int wkq_err;
35845 +               struct do_diropq_args args = {
35846 +                       .errp           = &diropq,
35847 +                       .dentry         = dentry,
35848 +                       .bindex         = bindex,
35849 +                       .flags          = flags
35850 +               };
35851 +
35852 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35853 +               if (unlikely(wkq_err))
35854 +                       diropq = ERR_PTR(wkq_err);
35855 +       }
35856 +
35857 +       return diropq;
35858 +}
35859 +
35860 +/* ---------------------------------------------------------------------- */
35861 +
35862 +/*
35863 + * lookup whiteout dentry.
35864 + * @h_parent: lower parent dentry which must exist and be locked
35865 + * @base_name: name of dentry which will be whiteouted
35866 + * returns dentry for whiteout.
35867 + */
35868 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35869 +                         struct au_branch *br)
35870 +{
35871 +       int err;
35872 +       struct qstr wh_name;
35873 +       struct dentry *wh_dentry;
35874 +       struct path h_path;
35875 +
35876 +       err = au_wh_name_alloc(&wh_name, base_name);
35877 +       wh_dentry = ERR_PTR(err);
35878 +       if (!err) {
35879 +               h_path.dentry = h_parent;
35880 +               h_path.mnt = au_br_mnt(br);
35881 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35882 +               au_kfree_try_rcu(wh_name.name);
35883 +       }
35884 +       return wh_dentry;
35885 +}
35886 +
35887 +/*
35888 + * link/create a whiteout for @dentry on @bindex.
35889 + */
35890 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35891 +                           struct dentry *h_parent)
35892 +{
35893 +       struct dentry *wh_dentry;
35894 +       struct super_block *sb;
35895 +       int err;
35896 +
35897 +       sb = dentry->d_sb;
35898 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35899 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35900 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35901 +               if (!err) {
35902 +                       au_set_dbwh(dentry, bindex);
35903 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35904 +               } else {
35905 +                       dput(wh_dentry);
35906 +                       wh_dentry = ERR_PTR(err);
35907 +               }
35908 +       }
35909 +
35910 +       return wh_dentry;
35911 +}
35912 +
35913 +/* ---------------------------------------------------------------------- */
35914 +
35915 +/* Delete all whiteouts in this directory on branch bindex. */
35916 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35917 +                          aufs_bindex_t bindex)
35918 +{
35919 +       int err;
35920 +       unsigned long ul, n;
35921 +       struct qstr wh_name;
35922 +       char *p;
35923 +       struct hlist_head *head;
35924 +       struct au_vdir_wh *pos;
35925 +       struct au_vdir_destr *str;
35926 +
35927 +       err = -ENOMEM;
35928 +       p = (void *)__get_free_page(GFP_NOFS);
35929 +       wh_name.name = p;
35930 +       if (unlikely(!wh_name.name))
35931 +               goto out;
35932 +
35933 +       err = 0;
35934 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35935 +       p += AUFS_WH_PFX_LEN;
35936 +       n = whlist->nh_num;
35937 +       head = whlist->nh_head;
35938 +       for (ul = 0; !err && ul < n; ul++, head++) {
35939 +               hlist_for_each_entry(pos, head, wh_hash) {
35940 +                       if (pos->wh_bindex != bindex)
35941 +                               continue;
35942 +
35943 +                       str = &pos->wh_str;
35944 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35945 +                               memcpy(p, str->name, str->len);
35946 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35947 +                               err = unlink_wh_name(h_path, &wh_name);
35948 +                               if (!err)
35949 +                                       continue;
35950 +                               break;
35951 +                       }
35952 +                       AuIOErr("whiteout name too long %.*s\n",
35953 +                               str->len, str->name);
35954 +                       err = -EIO;
35955 +                       break;
35956 +               }
35957 +       }
35958 +       free_page((unsigned long)wh_name.name);
35959 +
35960 +out:
35961 +       return err;
35962 +}
35963 +
35964 +struct del_wh_children_args {
35965 +       int *errp;
35966 +       struct path *h_path;
35967 +       struct au_nhash *whlist;
35968 +       aufs_bindex_t bindex;
35969 +};
35970 +
35971 +static void call_del_wh_children(void *args)
35972 +{
35973 +       struct del_wh_children_args *a = args;
35974 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35975 +}
35976 +
35977 +/* ---------------------------------------------------------------------- */
35978 +
35979 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35980 +{
35981 +       struct au_whtmp_rmdir *whtmp;
35982 +       int err;
35983 +       unsigned int rdhash;
35984 +
35985 +       SiMustAnyLock(sb);
35986 +
35987 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35988 +       if (unlikely(!whtmp)) {
35989 +               whtmp = ERR_PTR(-ENOMEM);
35990 +               goto out;
35991 +       }
35992 +
35993 +       /* no estimation for dir size */
35994 +       rdhash = au_sbi(sb)->si_rdhash;
35995 +       if (!rdhash)
35996 +               rdhash = AUFS_RDHASH_DEF;
35997 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35998 +       if (unlikely(err)) {
35999 +               au_kfree_rcu(whtmp);
36000 +               whtmp = ERR_PTR(err);
36001 +       }
36002 +
36003 +out:
36004 +       return whtmp;
36005 +}
36006 +
36007 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
36008 +{
36009 +       if (whtmp->br)
36010 +               au_lcnt_dec(&whtmp->br->br_count);
36011 +       dput(whtmp->wh_dentry);
36012 +       iput(whtmp->dir);
36013 +       au_nhash_wh_free(&whtmp->whlist);
36014 +       au_kfree_rcu(whtmp);
36015 +}
36016 +
36017 +/*
36018 + * rmdir the whiteouted temporary named dir @h_dentry.
36019 + * @whlist: whiteouted children.
36020 + */
36021 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36022 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
36023 +{
36024 +       int err;
36025 +       unsigned int h_nlink;
36026 +       struct path wh_path;
36027 +       struct inode *wh_inode, *h_dir;
36028 +       struct au_branch *br;
36029 +       struct user_namespace *h_userns;
36030 +
36031 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
36032 +       IMustLock(h_dir);
36033 +
36034 +       br = au_sbr(dir->i_sb, bindex);
36035 +       wh_path.dentry = wh_dentry;
36036 +       wh_path.mnt = au_br_mnt(br);
36037 +       h_userns = au_br_userns(br);
36038 +       wh_inode = d_inode(wh_dentry);
36039 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
36040 +
36041 +       /*
36042 +        * someone else might change some whiteouts while we were sleeping.
36043 +        * it means this whlist may have an obsoleted entry.
36044 +        */
36045 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
36046 +               err = del_wh_children(&wh_path, whlist, bindex);
36047 +       else {
36048 +               int wkq_err;
36049 +               struct del_wh_children_args args = {
36050 +                       .errp           = &err,
36051 +                       .h_path         = &wh_path,
36052 +                       .whlist         = whlist,
36053 +                       .bindex         = bindex
36054 +               };
36055 +
36056 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36057 +               if (unlikely(wkq_err))
36058 +                       err = wkq_err;
36059 +       }
36060 +       inode_unlock(wh_inode);
36061 +
36062 +       if (!err) {
36063 +               h_nlink = h_dir->i_nlink;
36064 +               err = vfsub_rmdir(h_dir, &wh_path);
36065 +               /* some fs doesn't change the parent nlink in some cases */
36066 +               h_nlink -= h_dir->i_nlink;
36067 +       }
36068 +
36069 +       if (!err) {
36070 +               if (au_ibtop(dir) == bindex) {
36071 +                       /* todo: dir->i_mutex is necessary */
36072 +                       au_cpup_attr_timesizes(dir);
36073 +                       if (h_nlink)
36074 +                               vfsub_drop_nlink(dir);
36075 +               }
36076 +               return 0; /* success */
36077 +       }
36078 +
36079 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36080 +       return err;
36081 +}
36082 +
36083 +static void call_rmdir_whtmp(void *args)
36084 +{
36085 +       int err;
36086 +       aufs_bindex_t bindex;
36087 +       struct au_whtmp_rmdir *a = args;
36088 +       struct super_block *sb;
36089 +       struct dentry *h_parent;
36090 +       struct inode *h_dir;
36091 +       struct au_hinode *hdir;
36092 +
36093 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36094 +       /* inode_lock(a->dir); */
36095 +       err = -EROFS;
36096 +       sb = a->dir->i_sb;
36097 +       si_read_lock(sb, !AuLock_FLUSH);
36098 +       if (!au_br_writable(a->br->br_perm))
36099 +               goto out;
36100 +       bindex = au_br_index(sb, a->br->br_id);
36101 +       if (unlikely(bindex < 0))
36102 +               goto out;
36103 +
36104 +       err = -EIO;
36105 +       ii_write_lock_parent(a->dir);
36106 +       h_parent = dget_parent(a->wh_dentry);
36107 +       h_dir = d_inode(h_parent);
36108 +       hdir = au_hi(a->dir, bindex);
36109 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36110 +       if (unlikely(err))
36111 +               goto out_mnt;
36112 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36113 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36114 +                         a->br);
36115 +       if (!err)
36116 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36117 +       au_hn_inode_unlock(hdir);
36118 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36119 +
36120 +out_mnt:
36121 +       dput(h_parent);
36122 +       ii_write_unlock(a->dir);
36123 +out:
36124 +       /* inode_unlock(a->dir); */
36125 +       au_whtmp_rmdir_free(a);
36126 +       si_read_unlock(sb);
36127 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36128 +       if (unlikely(err))
36129 +               AuIOErr("err %d\n", err);
36130 +}
36131 +
36132 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36133 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36134 +{
36135 +       int wkq_err;
36136 +       struct super_block *sb;
36137 +
36138 +       IMustLock(dir);
36139 +
36140 +       /* all post-process will be done in do_rmdir_whtmp(). */
36141 +       sb = dir->i_sb;
36142 +       args->dir = au_igrab(dir);
36143 +       args->br = au_sbr(sb, bindex);
36144 +       au_lcnt_inc(&args->br->br_count);
36145 +       args->wh_dentry = dget(wh_dentry);
36146 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36147 +       if (unlikely(wkq_err)) {
36148 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36149 +               au_whtmp_rmdir_free(args);
36150 +       }
36151 +}
36152 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36153 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36154 +++ linux/fs/aufs/whout.h       2022-11-05 23:02:18.972555950 +0100
36155 @@ -0,0 +1,87 @@
36156 +/* SPDX-License-Identifier: GPL-2.0 */
36157 +/*
36158 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36159 + *
36160 + * This program is free software; you can redistribute it and/or modify
36161 + * it under the terms of the GNU General Public License as published by
36162 + * the Free Software Foundation; either version 2 of the License, or
36163 + * (at your option) any later version.
36164 + *
36165 + * This program is distributed in the hope that it will be useful,
36166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36168 + * GNU General Public License for more details.
36169 + *
36170 + * You should have received a copy of the GNU General Public License
36171 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36172 + */
36173 +
36174 +/*
36175 + * whiteout for logical deletion and opaque directory
36176 + */
36177 +
36178 +#ifndef __AUFS_WHOUT_H__
36179 +#define __AUFS_WHOUT_H__
36180 +
36181 +#ifdef __KERNEL__
36182 +
36183 +#include "dir.h"
36184 +
36185 +/* whout.c */
36186 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36187 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
36188 +              struct qstr *wh_name, int try_sio);
36189 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
36190 +struct au_branch;
36191 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36192 +                            struct qstr *prefix);
36193 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36194 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36195 +                       struct dentry *dentry);
36196 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36197 +
36198 +/* diropq flags */
36199 +#define AuDiropq_CREATE        1
36200 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36201 +#define au_fset_diropq(flags, name) \
36202 +       do { (flags) |= AuDiropq_##name; } while (0)
36203 +#define au_fclr_diropq(flags, name) \
36204 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36205 +
36206 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36207 +                            unsigned int flags);
36208 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36209 +                         struct au_branch *br);
36210 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36211 +                           struct dentry *h_parent);
36212 +
36213 +/* real rmdir for the whiteout-ed dir */
36214 +struct au_whtmp_rmdir {
36215 +       struct inode *dir;
36216 +       struct au_branch *br;
36217 +       struct dentry *wh_dentry;
36218 +       struct au_nhash whlist;
36219 +};
36220 +
36221 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36222 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36223 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36224 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36225 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36226 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36227 +
36228 +/* ---------------------------------------------------------------------- */
36229 +
36230 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36231 +                                             aufs_bindex_t bindex)
36232 +{
36233 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36234 +}
36235 +
36236 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36237 +{
36238 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36239 +}
36240 +
36241 +#endif /* __KERNEL__ */
36242 +#endif /* __AUFS_WHOUT_H__ */
36243 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36244 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36245 +++ linux/fs/aufs/wkq.c 2022-11-05 23:02:18.972555950 +0100
36246 @@ -0,0 +1,372 @@
36247 +// SPDX-License-Identifier: GPL-2.0
36248 +/*
36249 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36250 + *
36251 + * This program is free software; you can redistribute it and/or modify
36252 + * it under the terms of the GNU General Public License as published by
36253 + * the Free Software Foundation; either version 2 of the License, or
36254 + * (at your option) any later version.
36255 + *
36256 + * This program is distributed in the hope that it will be useful,
36257 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36258 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36259 + * GNU General Public License for more details.
36260 + *
36261 + * You should have received a copy of the GNU General Public License
36262 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36263 + */
36264 +
36265 +/*
36266 + * workqueue for asynchronous/super-io operations
36267 + * todo: try new credential scheme
36268 + */
36269 +
36270 +#include <linux/module.h>
36271 +#include "aufs.h"
36272 +
36273 +/* internal workqueue named AUFS_WKQ_NAME */
36274 +
36275 +static struct workqueue_struct *au_wkq;
36276 +
36277 +struct au_wkinfo {
36278 +       struct work_struct wk;
36279 +       struct kobject *kobj;
36280 +
36281 +       unsigned int flags; /* see wkq.h */
36282 +
36283 +       au_wkq_func_t func;
36284 +       void *args;
36285 +
36286 +#ifdef CONFIG_LOCKDEP
36287 +       int dont_check;
36288 +       struct held_lock **hlock;
36289 +#endif
36290 +
36291 +       struct completion *comp;
36292 +};
36293 +
36294 +/* ---------------------------------------------------------------------- */
36295 +/*
36296 + * Aufs passes some operations to the workqueue such as the internal copyup.
36297 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36298 + * job run by workqueue depends upon the locks acquired in the other task.
36299 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36300 + * information too. And the job in the workqueue restores the info in order to
36301 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36302 + * correctly and expectedly.
36303 + */
36304 +
36305 +#ifndef CONFIG_LOCKDEP
36306 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36307 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36308 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36309 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36310 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36311 +#else
36312 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36313 +{
36314 +       wkinfo->hlock = NULL;
36315 +       wkinfo->dont_check = 0;
36316 +}
36317 +
36318 +/*
36319 + * 1: matched
36320 + * 0: unmatched
36321 + */
36322 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36323 +{
36324 +       static DEFINE_SPINLOCK(spin);
36325 +       static struct {
36326 +               char *name;
36327 +               struct lock_class_key *key;
36328 +       } a[] = {
36329 +               { .name = "&sbinfo->si_rwsem" },
36330 +               { .name = "&finfo->fi_rwsem" },
36331 +               { .name = "&dinfo->di_rwsem" },
36332 +               { .name = "&iinfo->ii_rwsem" }
36333 +       };
36334 +       static int set;
36335 +       int i;
36336 +
36337 +       /* lockless read from 'set.' see below */
36338 +       if (set == ARRAY_SIZE(a)) {
36339 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36340 +                       if (a[i].key == key)
36341 +                               goto match;
36342 +               goto unmatch;
36343 +       }
36344 +
36345 +       spin_lock(&spin);
36346 +       if (set)
36347 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36348 +                       if (a[i].key == key) {
36349 +                               spin_unlock(&spin);
36350 +                               goto match;
36351 +                       }
36352 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36353 +               if (a[i].key) {
36354 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36355 +                               spin_unlock(&spin);
36356 +                               goto match;
36357 +                       } else
36358 +                               continue;
36359 +               }
36360 +               if (strstr(a[i].name, name)) {
36361 +                       /*
36362 +                        * the order of these three lines is important for the
36363 +                        * lockless read above.
36364 +                        */
36365 +                       a[i].key = key;
36366 +                       spin_unlock(&spin);
36367 +                       set++;
36368 +                       /* AuDbg("%d, %s\n", set, name); */
36369 +                       goto match;
36370 +               }
36371 +       }
36372 +       spin_unlock(&spin);
36373 +       goto unmatch;
36374 +
36375 +match:
36376 +       return 1;
36377 +unmatch:
36378 +       return 0;
36379 +}
36380 +
36381 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36382 +{
36383 +       int err, n;
36384 +       struct task_struct *curr;
36385 +       struct held_lock **hl, *held_locks, *p;
36386 +
36387 +       err = 0;
36388 +       curr = current;
36389 +       wkinfo->dont_check = lockdep_recursing(curr);
36390 +       if (wkinfo->dont_check)
36391 +               goto out;
36392 +       n = curr->lockdep_depth;
36393 +       if (!n)
36394 +               goto out;
36395 +
36396 +       err = -ENOMEM;
36397 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36398 +       if (unlikely(!wkinfo->hlock))
36399 +               goto out;
36400 +
36401 +       err = 0;
36402 +#if 0 /* left for debugging */
36403 +       if (0 && au_debug_test())
36404 +               lockdep_print_held_locks(curr);
36405 +#endif
36406 +       held_locks = curr->held_locks;
36407 +       hl = wkinfo->hlock;
36408 +       while (n--) {
36409 +               p = held_locks++;
36410 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36411 +                       *hl++ = p;
36412 +       }
36413 +       *hl = NULL;
36414 +
36415 +out:
36416 +       return err;
36417 +}
36418 +
36419 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36420 +{
36421 +       au_kfree_try_rcu(wkinfo->hlock);
36422 +}
36423 +
36424 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36425 +{
36426 +       struct held_lock *p, **hl = wkinfo->hlock;
36427 +       int subclass;
36428 +
36429 +       if (wkinfo->dont_check)
36430 +               lockdep_off();
36431 +       if (!hl)
36432 +               return;
36433 +       while ((p = *hl++)) { /* assignment */
36434 +               subclass = lockdep_hlock_class(p)->subclass;
36435 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36436 +               if (p->read)
36437 +                       rwsem_acquire_read(p->instance, subclass, 0,
36438 +                                          /*p->acquire_ip*/_RET_IP_);
36439 +               else
36440 +                       rwsem_acquire(p->instance, subclass, 0,
36441 +                                     /*p->acquire_ip*/_RET_IP_);
36442 +       }
36443 +}
36444 +
36445 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36446 +{
36447 +       struct held_lock *p, **hl = wkinfo->hlock;
36448 +
36449 +       if (wkinfo->dont_check)
36450 +               lockdep_on();
36451 +       if (!hl)
36452 +               return;
36453 +       while ((p = *hl++)) /* assignment */
36454 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36455 +}
36456 +#endif
36457 +
36458 +static void wkq_func(struct work_struct *wk)
36459 +{
36460 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36461 +
36462 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36463 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36464 +
36465 +       au_wkq_lockdep_pre(wkinfo);
36466 +       wkinfo->func(wkinfo->args);
36467 +       au_wkq_lockdep_post(wkinfo);
36468 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36469 +               complete(wkinfo->comp);
36470 +       else {
36471 +               kobject_put(wkinfo->kobj);
36472 +               module_put(THIS_MODULE); /* todo: ?? */
36473 +               au_kfree_rcu(wkinfo);
36474 +       }
36475 +}
36476 +
36477 +/*
36478 + * Since struct completion is large, try allocating it dynamically.
36479 + */
36480 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36481 +
36482 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36483 +{
36484 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36485 +       if (*comp) {
36486 +               init_completion(*comp);
36487 +               wkinfo->comp = *comp;
36488 +               return 0;
36489 +       }
36490 +       return -ENOMEM;
36491 +}
36492 +
36493 +static void au_wkq_comp_free(struct completion *comp)
36494 +{
36495 +       au_kfree_rcu(comp);
36496 +}
36497 +
36498 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36499 +{
36500 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36501 +               if (au_wkq_test()) {
36502 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36503 +                               " due to a dead dir by UDBA,"
36504 +                               " or async xino write?\n");
36505 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36506 +               }
36507 +       } else
36508 +               au_dbg_verify_kthread();
36509 +
36510 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36511 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36512 +               queue_work(au_wkq, &wkinfo->wk);
36513 +       } else {
36514 +               INIT_WORK(&wkinfo->wk, wkq_func);
36515 +               schedule_work(&wkinfo->wk);
36516 +       }
36517 +}
36518 +
36519 +/*
36520 + * Be careful. It is easy to make deadlock happen.
36521 + * processA: lock, wkq and wait
36522 + * processB: wkq and wait, lock in wkq
36523 + * --> deadlock
36524 + */
36525 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36526 +{
36527 +       int err;
36528 +       AuWkqCompDeclare(comp);
36529 +       struct au_wkinfo wkinfo = {
36530 +               .flags  = flags,
36531 +               .func   = func,
36532 +               .args   = args
36533 +       };
36534 +
36535 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36536 +       if (unlikely(err))
36537 +               goto out;
36538 +       err = au_wkq_lockdep_alloc(&wkinfo);
36539 +       if (unlikely(err))
36540 +               goto out_comp;
36541 +       if (!err) {
36542 +               au_wkq_run(&wkinfo);
36543 +               /* no timeout, no interrupt */
36544 +               wait_for_completion(wkinfo.comp);
36545 +       }
36546 +       au_wkq_lockdep_free(&wkinfo);
36547 +
36548 +out_comp:
36549 +       au_wkq_comp_free(comp);
36550 +out:
36551 +       destroy_work_on_stack(&wkinfo.wk);
36552 +       return err;
36553 +}
36554 +
36555 +/*
36556 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36557 + * problem in a concurrent umounting.
36558 + */
36559 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36560 +                 unsigned int flags)
36561 +{
36562 +       int err;
36563 +       struct au_wkinfo *wkinfo;
36564 +
36565 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36566 +
36567 +       /*
36568 +        * wkq_func() must free this wkinfo.
36569 +        * it highly depends upon the implementation of workqueue.
36570 +        */
36571 +       err = 0;
36572 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36573 +       if (wkinfo) {
36574 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36575 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36576 +               wkinfo->func = func;
36577 +               wkinfo->args = args;
36578 +               wkinfo->comp = NULL;
36579 +               au_wkq_lockdep_init(wkinfo);
36580 +               kobject_get(wkinfo->kobj);
36581 +               __module_get(THIS_MODULE); /* todo: ?? */
36582 +
36583 +               au_wkq_run(wkinfo);
36584 +       } else {
36585 +               err = -ENOMEM;
36586 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36587 +       }
36588 +
36589 +       return err;
36590 +}
36591 +
36592 +/* ---------------------------------------------------------------------- */
36593 +
36594 +void au_nwt_init(struct au_nowait_tasks *nwt)
36595 +{
36596 +       atomic_set(&nwt->nw_len, 0);
36597 +       /* smp_mb(); */ /* atomic_set */
36598 +       init_waitqueue_head(&nwt->nw_wq);
36599 +}
36600 +
36601 +void au_wkq_fin(void)
36602 +{
36603 +       destroy_workqueue(au_wkq);
36604 +}
36605 +
36606 +int __init au_wkq_init(void)
36607 +{
36608 +       int err;
36609 +
36610 +       err = 0;
36611 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36612 +       if (IS_ERR(au_wkq))
36613 +               err = PTR_ERR(au_wkq);
36614 +       else if (!au_wkq)
36615 +               err = -ENOMEM;
36616 +
36617 +       return err;
36618 +}
36619 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36620 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36621 +++ linux/fs/aufs/wkq.h 2022-11-05 23:02:18.972555950 +0100
36622 @@ -0,0 +1,89 @@
36623 +/* SPDX-License-Identifier: GPL-2.0 */
36624 +/*
36625 + * Copyright (C) 2005-2022 Junjiro R. Okajima
36626 + *
36627 + * This program is free software; you can redistribute it and/or modify
36628 + * it under the terms of the GNU General Public License as published by
36629 + * the Free Software Foundation; either version 2 of the License, or
36630 + * (at your option) any later version.
36631 + *
36632 + * This program is distributed in the hope that it will be useful,
36633 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36634 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36635 + * GNU General Public License for more details.
36636 + *
36637 + * You should have received a copy of the GNU General Public License
36638 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36639 + */
36640 +
36641 +/*
36642 + * workqueue for asynchronous/super-io operations
36643 + * todo: try new credentials management scheme
36644 + */
36645 +
36646 +#ifndef __AUFS_WKQ_H__
36647 +#define __AUFS_WKQ_H__
36648 +
36649 +#ifdef __KERNEL__
36650 +
36651 +#include <linux/wait.h>
36652 +
36653 +struct super_block;
36654 +
36655 +/* ---------------------------------------------------------------------- */
36656 +
36657 +/*
36658 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36659 + */
36660 +struct au_nowait_tasks {
36661 +       atomic_t                nw_len;
36662 +       wait_queue_head_t       nw_wq;
36663 +};
36664 +
36665 +/* ---------------------------------------------------------------------- */
36666 +
36667 +typedef void (*au_wkq_func_t)(void *args);
36668 +
36669 +/* wkq flags */
36670 +#define AuWkq_WAIT     1
36671 +#define AuWkq_NEST     (1 << 1)
36672 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36673 +#define au_fset_wkq(flags, name) \
36674 +       do { (flags) |= AuWkq_##name; } while (0)
36675 +#define au_fclr_wkq(flags, name) \
36676 +       do { (flags) &= ~AuWkq_##name; } while (0)
36677 +
36678 +/* wkq.c */
36679 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36680 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36681 +                 unsigned int flags);
36682 +void au_nwt_init(struct au_nowait_tasks *nwt);
36683 +int __init au_wkq_init(void);
36684 +void au_wkq_fin(void);
36685 +
36686 +/* ---------------------------------------------------------------------- */
36687 +
36688 +static inline int au_wkq_test(void)
36689 +{
36690 +       return current->flags & PF_WQ_WORKER;
36691 +}
36692 +
36693 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36694 +{
36695 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36696 +}
36697 +
36698 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36699 +{
36700 +       if (atomic_dec_and_test(&nwt->nw_len))
36701 +               wake_up_all(&nwt->nw_wq);
36702 +}
36703 +
36704 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36705 +{
36706 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36707 +       return 0;
36708 +}
36709 +
36710 +#endif /* __KERNEL__ */
36711 +#endif /* __AUFS_WKQ_H__ */
36712 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36713 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36714 +++ linux/fs/aufs/xattr.c       2022-12-17 09:21:34.799855195 +0100
36715 @@ -0,0 +1,368 @@
36716 +// SPDX-License-Identifier: GPL-2.0
36717 +/*
36718 + * Copyright (C) 2014-2022 Junjiro R. Okajima
36719 + *
36720 + * This program is free software; you can redistribute it and/or modify
36721 + * it under the terms of the GNU General Public License as published by
36722 + * the Free Software Foundation; either version 2 of the License, or
36723 + * (at your option) any later version.
36724 + *
36725 + * This program is distributed in the hope that it will be useful,
36726 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36727 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36728 + * GNU General Public License for more details.
36729 + *
36730 + * You should have received a copy of the GNU General Public License
36731 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36732 + */
36733 +
36734 +/*
36735 + * handling xattr functions
36736 + */
36737 +
36738 +#include <linux/fs.h>
36739 +#include <linux/posix_acl_xattr.h>
36740 +#include <linux/xattr.h>
36741 +#include "aufs.h"
36742 +
36743 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36744 +{
36745 +       if (!ignore_flags)
36746 +               goto out;
36747 +       switch (err) {
36748 +       case -ENOMEM:
36749 +       case -EDQUOT:
36750 +               goto out;
36751 +       }
36752 +
36753 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36754 +               err = 0;
36755 +               goto out;
36756 +       }
36757 +
36758 +#define cmp(brattr, prefix) do {                                       \
36759 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36760 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36761 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36762 +                               err = 0;                                \
36763 +                       goto out;                                       \
36764 +               }                                                       \
36765 +       } while (0)
36766 +
36767 +       cmp(SEC, SECURITY);
36768 +       cmp(SYS, SYSTEM);
36769 +       cmp(TR, TRUSTED);
36770 +       cmp(USR, USER);
36771 +#undef cmp
36772 +
36773 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36774 +               err = 0;
36775 +
36776 +out:
36777 +       return err;
36778 +}
36779 +
36780 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36781 +
36782 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36783 +                           char *name, char **buf, unsigned int ignore_flags,
36784 +                           unsigned int verbose)
36785 +{
36786 +       int err;
36787 +       ssize_t ssz;
36788 +       struct inode *h_idst;
36789 +       struct dentry *h_dst_dentry, *h_src_dentry;
36790 +       struct user_namespace *h_dst_userns, *h_src_userns;
36791 +
36792 +       h_src_userns = mnt_user_ns(h_src->mnt);
36793 +       h_src_dentry = h_src->dentry;
36794 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36795 +                                GFP_NOFS);
36796 +       err = ssz;
36797 +       if (unlikely(err <= 0)) {
36798 +               if (err == -ENODATA
36799 +                   || (err == -EOPNOTSUPP
36800 +                       && ((ignore_flags & au_xattr_out_of_list)
36801 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36802 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36803 +                                   || !strcmp(name,
36804 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36805 +                           ))
36806 +                       err = 0;
36807 +               if (err && (verbose || au_debug_test()))
36808 +                       pr_err("%s, err %d\n", name, err);
36809 +               goto out;
36810 +       }
36811 +
36812 +       /* unlock it temporary */
36813 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36814 +       h_dst_dentry = h_dst->dentry;
36815 +       h_idst = d_inode(h_dst_dentry);
36816 +       inode_unlock(h_idst);
36817 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36818 +                            /*flags*/0);
36819 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36820 +       if (unlikely(err)) {
36821 +               if (verbose || au_debug_test())
36822 +                       pr_err("%s, err %d\n", name, err);
36823 +               err = au_xattr_ignore(err, name, ignore_flags);
36824 +       }
36825 +
36826 +out:
36827 +       return err;
36828 +}
36829 +
36830 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36831 +                 unsigned int verbose)
36832 +{
36833 +       int err, unlocked, acl_access, acl_default;
36834 +       ssize_t ssz;
36835 +       struct dentry *h_dst_dentry, *h_src_dentry;
36836 +       struct inode *h_isrc, *h_idst;
36837 +       char *value, *p, *o, *e;
36838 +
36839 +       /* try stopping to update the source inode while we are referencing */
36840 +       /* there should not be the parent-child relationship between them */
36841 +       h_dst_dentry = h_dst->dentry;
36842 +       h_idst = d_inode(h_dst_dentry);
36843 +       h_src_dentry = h_src->dentry;
36844 +       h_isrc = d_inode(h_src_dentry);
36845 +       inode_unlock(h_idst);
36846 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36847 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36848 +       unlocked = 0;
36849 +
36850 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36851 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36852 +       err = ssz;
36853 +       if (unlikely(err < 0)) {
36854 +               AuTraceErr(err);
36855 +               if (err == -ENODATA
36856 +                   || err == -EOPNOTSUPP)
36857 +                       err = 0;        /* ignore */
36858 +               goto out;
36859 +       }
36860 +
36861 +       err = 0;
36862 +       p = NULL;
36863 +       o = NULL;
36864 +       if (ssz) {
36865 +               err = -ENOMEM;
36866 +               p = kmalloc(ssz, GFP_NOFS);
36867 +               o = p;
36868 +               if (unlikely(!p))
36869 +                       goto out;
36870 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36871 +       }
36872 +       inode_unlock_shared(h_isrc);
36873 +       unlocked = 1;
36874 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36875 +       if (unlikely(err < 0))
36876 +               goto out_free;
36877 +
36878 +       err = 0;
36879 +       e = p + ssz;
36880 +       value = NULL;
36881 +       acl_access = 0;
36882 +       acl_default = 0;
36883 +       while (!err && p < e) {
36884 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36885 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36886 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36887 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36888 +                                       - 1);
36889 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36890 +                                      verbose);
36891 +               p += strlen(p) + 1;
36892 +       }
36893 +       AuTraceErr(err);
36894 +       ignore_flags |= au_xattr_out_of_list;
36895 +       if (!err && !acl_access) {
36896 +               err = au_do_cpup_xattr(h_dst, h_src,
36897 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36898 +                                      ignore_flags, verbose);
36899 +               AuTraceErr(err);
36900 +       }
36901 +       if (!err && !acl_default) {
36902 +               err = au_do_cpup_xattr(h_dst, h_src,
36903 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36904 +                                      ignore_flags, verbose);
36905 +               AuTraceErr(err);
36906 +       }
36907 +
36908 +       au_kfree_try_rcu(value);
36909 +
36910 +out_free:
36911 +       au_kfree_try_rcu(o);
36912 +out:
36913 +       if (!unlocked)
36914 +               inode_unlock_shared(h_isrc);
36915 +       AuTraceErr(err);
36916 +       return err;
36917 +}
36918 +
36919 +/* ---------------------------------------------------------------------- */
36920 +
36921 +static int au_smack_reentering(struct super_block *sb)
36922 +{
36923 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36924 +       /*
36925 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36926 +        * i_op->getxattr(). ouch.
36927 +        */
36928 +       return si_pid_test(sb);
36929 +#else
36930 +       return 0;
36931 +#endif
36932 +}
36933 +
36934 +enum {
36935 +       AU_XATTR_LIST,
36936 +       AU_XATTR_GET
36937 +};
36938 +
36939 +struct au_lgxattr {
36940 +       int type;
36941 +       union {
36942 +               struct {
36943 +                       char    *list;
36944 +                       size_t  size;
36945 +               } list;
36946 +               struct {
36947 +                       const char      *name;
36948 +                       void            *value;
36949 +                       size_t          size;
36950 +               } get;
36951 +       } u;
36952 +};
36953 +
36954 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36955 +                         struct au_lgxattr *arg)
36956 +{
36957 +       ssize_t err;
36958 +       int reenter;
36959 +       struct path h_path;
36960 +       struct super_block *sb;
36961 +
36962 +       sb = dentry->d_sb;
36963 +       reenter = au_smack_reentering(sb);
36964 +       if (!reenter) {
36965 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36966 +               if (unlikely(err))
36967 +                       goto out;
36968 +       }
36969 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36970 +       if (unlikely(err))
36971 +               goto out_si;
36972 +       if (unlikely(!h_path.dentry))
36973 +               /* illegally overlapped or something */
36974 +               goto out_di; /* pretending success */
36975 +
36976 +       /* always topmost entry only */
36977 +       switch (arg->type) {
36978 +       case AU_XATTR_LIST:
36979 +               err = vfs_listxattr(h_path.dentry,
36980 +                                   arg->u.list.list, arg->u.list.size);
36981 +               break;
36982 +       case AU_XATTR_GET:
36983 +               AuDebugOn(d_is_negative(h_path.dentry));
36984 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36985 +                                  arg->u.get.name, arg->u.get.value,
36986 +                                  arg->u.get.size);
36987 +               break;
36988 +       }
36989 +
36990 +out_di:
36991 +       if (!reenter)
36992 +               di_read_unlock(dentry, AuLock_IR);
36993 +out_si:
36994 +       if (!reenter)
36995 +               si_read_unlock(sb);
36996 +out:
36997 +       AuTraceErr(err);
36998 +       return err;
36999 +}
37000 +
37001 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
37002 +{
37003 +       struct au_lgxattr arg = {
37004 +               .type = AU_XATTR_LIST,
37005 +               .u.list = {
37006 +                       .list   = list,
37007 +                       .size   = size
37008 +               },
37009 +       };
37010 +
37011 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
37012 +}
37013 +
37014 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
37015 +                          const char *name, void *value, size_t size)
37016 +{
37017 +       struct au_lgxattr arg = {
37018 +               .type = AU_XATTR_GET,
37019 +               .u.get = {
37020 +                       .name   = name,
37021 +                       .value  = value,
37022 +                       .size   = size
37023 +               },
37024 +       };
37025 +
37026 +       return au_lgxattr(dentry, inode, &arg);
37027 +}
37028 +
37029 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
37030 +                      const char *name, const void *value, size_t size,
37031 +                      int flags)
37032 +{
37033 +       struct au_sxattr arg = {
37034 +               .type = AU_XATTR_SET,
37035 +               .u.set = {
37036 +                       .name   = name,
37037 +                       .value  = value,
37038 +                       .size   = size,
37039 +                       .flags  = flags
37040 +               },
37041 +       };
37042 +
37043 +       return au_sxattr(dentry, inode, &arg);
37044 +}
37045 +
37046 +/* ---------------------------------------------------------------------- */
37047 +
37048 +static int au_xattr_get(const struct xattr_handler *handler,
37049 +                       struct dentry *dentry, struct inode *inode,
37050 +                       const char *name, void *buffer, size_t size)
37051 +{
37052 +       return au_getxattr(dentry, inode, name, buffer, size);
37053 +}
37054 +
37055 +static int au_xattr_set(const struct xattr_handler *handler,
37056 +                       struct user_namespace *userns,
37057 +                       struct dentry *dentry, struct inode *inode,
37058 +                       const char *name, const void *value, size_t size,
37059 +                       int flags)
37060 +{
37061 +       return au_setxattr(dentry, inode, name, value, size, flags);
37062 +}
37063 +
37064 +static const struct xattr_handler au_xattr_handler = {
37065 +       .name   = "",
37066 +       .prefix = "",
37067 +       .get    = au_xattr_get,
37068 +       .set    = au_xattr_set
37069 +};
37070 +
37071 +static const struct xattr_handler *au_xattr_handlers[] = {
37072 +#ifdef CONFIG_FS_POSIX_ACL
37073 +       &posix_acl_access_xattr_handler,
37074 +       &posix_acl_default_xattr_handler,
37075 +#endif
37076 +       &au_xattr_handler, /* must be last */
37077 +       NULL
37078 +};
37079 +
37080 +void au_xattr_init(struct super_block *sb)
37081 +{
37082 +       sb->s_xattr = au_xattr_handlers;
37083 +}
37084 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37085 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37086 +++ linux/fs/aufs/xino.c        2022-11-05 23:02:18.972555950 +0100
37087 @@ -0,0 +1,1926 @@
37088 +// SPDX-License-Identifier: GPL-2.0
37089 +/*
37090 + * Copyright (C) 2005-2022 Junjiro R. Okajima
37091 + *
37092 + * This program is free software; you can redistribute it and/or modify
37093 + * it under the terms of the GNU General Public License as published by
37094 + * the Free Software Foundation; either version 2 of the License, or
37095 + * (at your option) any later version.
37096 + *
37097 + * This program is distributed in the hope that it will be useful,
37098 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37099 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37100 + * GNU General Public License for more details.
37101 + *
37102 + * You should have received a copy of the GNU General Public License
37103 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37104 + */
37105 +
37106 +/*
37107 + * external inode number translation table and bitmap
37108 + *
37109 + * things to consider
37110 + * - the lifetime
37111 + *   + au_xino object
37112 + *   + XINO files (xino, xib, xigen)
37113 + *   + dynamic debugfs entries (xiN)
37114 + *   + static debugfs entries (xib, xigen)
37115 + *   + static sysfs entry (xi_path)
37116 + * - several entry points to handle them.
37117 + *   + mount(2) without xino option (default)
37118 + *   + mount(2) with xino option
37119 + *   + mount(2) with noxino option
37120 + *   + umount(2)
37121 + *   + remount with add/del branches
37122 + *   + remount with xino/noxino options
37123 + */
37124 +
37125 +#include <linux/seq_file.h>
37126 +#include <linux/statfs.h>
37127 +#include "aufs.h"
37128 +
37129 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37130 +                                    aufs_bindex_t bbot,
37131 +                                    struct super_block *h_sb)
37132 +{
37133 +       /* todo: try binary-search if the branches are many */
37134 +       for (; btop <= bbot; btop++)
37135 +               if (h_sb == au_sbr_sb(sb, btop))
37136 +                       return btop;
37137 +       return -1;
37138 +}
37139 +
37140 +/*
37141 + * find another branch who is on the same filesystem of the specified
37142 + * branch{@btgt}. search until @bbot.
37143 + */
37144 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37145 +                                 aufs_bindex_t bbot)
37146 +{
37147 +       aufs_bindex_t bindex;
37148 +       struct super_block *tgt_sb;
37149 +
37150 +       tgt_sb = au_sbr_sb(sb, btgt);
37151 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37152 +       if (bindex < 0)
37153 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37154 +
37155 +       return bindex;
37156 +}
37157 +
37158 +/* ---------------------------------------------------------------------- */
37159 +
37160 +/*
37161 + * stop unnecessary notify events at creating xino files
37162 + */
37163 +
37164 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37165 +{
37166 +       aufs_bindex_t bfound, bindex, bbot;
37167 +       struct dentry *parent;
37168 +       struct au_branch *br;
37169 +
37170 +       bfound = -1;
37171 +       parent = dentry->d_parent; /* safe d_parent access */
37172 +       bbot = au_sbbot(sb);
37173 +       for (bindex = 0; bindex <= bbot; bindex++) {
37174 +               br = au_sbr(sb, bindex);
37175 +               if (au_br_dentry(br) == parent) {
37176 +                       bfound = bindex;
37177 +                       break;
37178 +               }
37179 +       }
37180 +
37181 +       AuDbg("bfound b%d\n", bfound);
37182 +       return bfound;
37183 +}
37184 +
37185 +struct au_xino_lock_dir {
37186 +       struct au_hinode *hdir;
37187 +       struct dentry *parent;
37188 +       struct inode *dir;
37189 +};
37190 +
37191 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37192 +                                         unsigned int lsc)
37193 +{
37194 +       struct dentry *parent;
37195 +       struct inode *dir;
37196 +
37197 +       parent = dget_parent(dentry);
37198 +       dir = d_inode(parent);
37199 +       inode_lock_nested(dir, lsc);
37200 +#if 0 /* it should not happen */
37201 +       spin_lock(&dentry->d_lock);
37202 +       if (unlikely(dentry->d_parent != parent)) {
37203 +               spin_unlock(&dentry->d_lock);
37204 +               inode_unlock(dir);
37205 +               dput(parent);
37206 +               parent = NULL;
37207 +               goto out;
37208 +       }
37209 +       spin_unlock(&dentry->d_lock);
37210 +
37211 +out:
37212 +#endif
37213 +       return parent;
37214 +}
37215 +
37216 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37217 +                            struct au_xino_lock_dir *ldir)
37218 +{
37219 +       aufs_bindex_t bindex;
37220 +
37221 +       ldir->hdir = NULL;
37222 +       bindex = au_xi_root(sb, xipath->dentry);
37223 +       if (bindex >= 0) {
37224 +               /* rw branch root */
37225 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37226 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37227 +       } else {
37228 +               /* other */
37229 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37230 +                                                  AuLsc_I_PARENT);
37231 +               ldir->dir = d_inode(ldir->parent);
37232 +       }
37233 +}
37234 +
37235 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37236 +{
37237 +       if (ldir->hdir)
37238 +               au_hn_inode_unlock(ldir->hdir);
37239 +       else {
37240 +               inode_unlock(ldir->dir);
37241 +               dput(ldir->parent);
37242 +       }
37243 +}
37244 +
37245 +/* ---------------------------------------------------------------------- */
37246 +
37247 +/*
37248 + * create and set a new xino file
37249 + */
37250 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37251 +                           int wbrtop)
37252 +{
37253 +       struct file *file;
37254 +       struct dentry *h_parent, *d;
37255 +       struct inode *h_dir, *inode;
37256 +       int err;
37257 +       static DEFINE_MUTEX(mtx);
37258 +
37259 +       /*
37260 +        * at mount-time, and the xino file is the default path,
37261 +        * hnotify is disabled so we have no notify events to ignore.
37262 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37263 +        */
37264 +       if (!wbrtop)
37265 +               mutex_lock(&mtx);
37266 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37267 +                              /* | __FMODE_NONOTIFY */,
37268 +                              0666);
37269 +       if (IS_ERR(file)) {
37270 +               if (!wbrtop)
37271 +                       mutex_unlock(&mtx);
37272 +               if (!silent)
37273 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37274 +               return file;
37275 +       }
37276 +
37277 +       /* keep file count */
37278 +       err = 0;
37279 +       d = file->f_path.dentry;
37280 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37281 +       if (!wbrtop)
37282 +               mutex_unlock(&mtx);
37283 +       /* mnt_want_write() is unnecessary here */
37284 +       h_dir = d_inode(h_parent);
37285 +       inode = file_inode(file);
37286 +       /* no delegation since it is just created */
37287 +       if (inode->i_nlink)
37288 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37289 +                                  /*force*/0);
37290 +       inode_unlock(h_dir);
37291 +       dput(h_parent);
37292 +       if (unlikely(err)) {
37293 +               if (!silent)
37294 +                       pr_err("unlink %s(%d)\n", fpath, err);
37295 +               goto out;
37296 +       }
37297 +
37298 +       err = -EINVAL;
37299 +       if (unlikely(sb && sb == d->d_sb)) {
37300 +               if (!silent)
37301 +                       pr_err("%s must be outside\n", fpath);
37302 +               goto out;
37303 +       }
37304 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37305 +               if (!silent)
37306 +                       pr_err("xino doesn't support %s(%s)\n",
37307 +                              fpath, au_sbtype(d->d_sb));
37308 +               goto out;
37309 +       }
37310 +       return file; /* success */
37311 +
37312 +out:
37313 +       fput(file);
37314 +       file = ERR_PTR(err);
37315 +       return file;
37316 +}
37317 +
37318 +/*
37319 + * create a new xinofile at the same place/path as @base.
37320 + */
37321 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37322 +                            struct file *copy_src)
37323 +{
37324 +       struct file *file;
37325 +       struct dentry *dentry;
37326 +       struct inode *dir, *delegated;
37327 +       struct qstr *name;
37328 +       struct path ppath, path;
37329 +       int err, do_unlock;
37330 +       struct au_xino_lock_dir ldir;
37331 +
37332 +       do_unlock = 1;
37333 +       au_xino_lock_dir(sb, base, &ldir);
37334 +       dentry = base->dentry;
37335 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37336 +       ppath.mnt = base->mnt;
37337 +       dir = d_inode(ppath.dentry);
37338 +       IMustLock(dir);
37339 +
37340 +       name = &dentry->d_name;
37341 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37342 +       if (IS_ERR(path.dentry)) {
37343 +               file = (void *)path.dentry;
37344 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37345 +               goto out;
37346 +       }
37347 +
37348 +       /* no need to mnt_want_write() since we call dentry_open() later */
37349 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37350 +       if (unlikely(err)) {
37351 +               file = ERR_PTR(err);
37352 +               pr_err("%pd create err %d\n", dentry, err);
37353 +               goto out_dput;
37354 +       }
37355 +
37356 +       path.mnt = base->mnt;
37357 +       file = vfsub_dentry_open(&path,
37358 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37359 +                                /* | __FMODE_NONOTIFY */);
37360 +       if (IS_ERR(file)) {
37361 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37362 +               goto out_dput;
37363 +       }
37364 +
37365 +       delegated = NULL;
37366 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37367 +       au_xino_unlock_dir(&ldir);
37368 +       do_unlock = 0;
37369 +       if (unlikely(err == -EWOULDBLOCK)) {
37370 +               pr_warn("cannot retry for NFSv4 delegation"
37371 +                       " for an internal unlink\n");
37372 +               iput(delegated);
37373 +       }
37374 +       if (unlikely(err)) {
37375 +               pr_err("%pd unlink err %d\n", dentry, err);
37376 +               goto out_fput;
37377 +       }
37378 +
37379 +       if (copy_src) {
37380 +               /* no one can touch copy_src xino */
37381 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37382 +               if (unlikely(err)) {
37383 +                       pr_err("%pd copy err %d\n", dentry, err);
37384 +                       goto out_fput;
37385 +               }
37386 +       }
37387 +       goto out_dput; /* success */
37388 +
37389 +out_fput:
37390 +       fput(file);
37391 +       file = ERR_PTR(err);
37392 +out_dput:
37393 +       dput(path.dentry);
37394 +out:
37395 +       if (do_unlock)
37396 +               au_xino_unlock_dir(&ldir);
37397 +       return file;
37398 +}
37399 +
37400 +struct file *au_xino_file1(struct au_xino *xi)
37401 +{
37402 +       struct file *file;
37403 +       unsigned int u, nfile;
37404 +
37405 +       file = NULL;
37406 +       nfile = xi->xi_nfile;
37407 +       for (u = 0; u < nfile; u++) {
37408 +               file = xi->xi_file[u];
37409 +               if (file)
37410 +                       break;
37411 +       }
37412 +
37413 +       return file;
37414 +}
37415 +
37416 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37417 +{
37418 +       int err;
37419 +       struct file *f;
37420 +       void *p;
37421 +
37422 +       if (file)
37423 +               get_file(file);
37424 +
37425 +       err = 0;
37426 +       f = NULL;
37427 +       if (idx < xi->xi_nfile) {
37428 +               f = xi->xi_file[idx];
37429 +               if (f)
37430 +                       fput(f);
37431 +       } else {
37432 +               p = au_kzrealloc(xi->xi_file,
37433 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37434 +                                sizeof(*xi->xi_file) * (idx + 1),
37435 +                                GFP_NOFS, /*may_shrink*/0);
37436 +               if (p) {
37437 +                       MtxMustLock(&xi->xi_mtx);
37438 +                       xi->xi_file = p;
37439 +                       xi->xi_nfile = idx + 1;
37440 +               } else {
37441 +                       err = -ENOMEM;
37442 +                       if (file)
37443 +                               fput(file);
37444 +                       goto out;
37445 +               }
37446 +       }
37447 +       xi->xi_file[idx] = file;
37448 +
37449 +out:
37450 +       return err;
37451 +}
37452 +
37453 +/*
37454 + * if @xinew->xi is not set, then create new xigen file.
37455 + */
37456 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37457 +{
37458 +       struct file *file;
37459 +       int err;
37460 +
37461 +       SiMustAnyLock(sb);
37462 +
37463 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37464 +       if (IS_ERR(file)) {
37465 +               err = PTR_ERR(file);
37466 +               pr_err("%s[%d], err %d\n",
37467 +                      xinew->xi ? "xino" : "xigen",
37468 +                      xinew->idx, err);
37469 +               goto out;
37470 +       }
37471 +
37472 +       if (xinew->xi)
37473 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37474 +       else {
37475 +               BUG();
37476 +               /* todo: make xigen file an array */
37477 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37478 +       }
37479 +       fput(file);
37480 +       if (unlikely(err))
37481 +               file = ERR_PTR(err);
37482 +
37483 +out:
37484 +       return file;
37485 +}
37486 +
37487 +/* ---------------------------------------------------------------------- */
37488 +
37489 +/*
37490 + * truncate xino files
37491 + */
37492 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37493 +                           int idx, struct kstatfs *st)
37494 +{
37495 +       int err;
37496 +       blkcnt_t blocks;
37497 +       struct file *file, *new_xino;
37498 +       struct au_xi_new xinew = {
37499 +               .idx = idx
37500 +       };
37501 +
37502 +       err = 0;
37503 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37504 +       file = au_xino_file(xinew.xi, idx);
37505 +       if (!file)
37506 +               goto out;
37507 +
37508 +       xinew.base = &file->f_path;
37509 +       err = vfs_statfs(xinew.base, st);
37510 +       if (unlikely(err)) {
37511 +               AuErr1("statfs err %d, ignored\n", err);
37512 +               err = 0;
37513 +               goto out;
37514 +       }
37515 +
37516 +       blocks = file_inode(file)->i_blocks;
37517 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37518 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37519 +
37520 +       xinew.copy_src = file;
37521 +       new_xino = au_xi_new(sb, &xinew);
37522 +       if (IS_ERR(new_xino)) {
37523 +               err = PTR_ERR(new_xino);
37524 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37525 +               goto out;
37526 +       }
37527 +
37528 +       err = vfs_statfs(&new_xino->f_path, st);
37529 +       if (!err)
37530 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37531 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37532 +                       st->f_bfree, st->f_blocks);
37533 +       else {
37534 +               AuErr1("statfs err %d, ignored\n", err);
37535 +               err = 0;
37536 +       }
37537 +
37538 +out:
37539 +       return err;
37540 +}
37541 +
37542 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37543 +{
37544 +       int err, i;
37545 +       unsigned long jiffy;
37546 +       aufs_bindex_t bbot;
37547 +       struct kstatfs *st;
37548 +       struct au_branch *br;
37549 +       struct au_xino *xi;
37550 +
37551 +       err = -ENOMEM;
37552 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37553 +       if (unlikely(!st))
37554 +               goto out;
37555 +
37556 +       err = -EINVAL;
37557 +       bbot = au_sbbot(sb);
37558 +       if (unlikely(bindex < 0 || bbot < bindex))
37559 +               goto out_st;
37560 +
37561 +       err = 0;
37562 +       jiffy = jiffies;
37563 +       br = au_sbr(sb, bindex);
37564 +       xi = br->br_xino;
37565 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37566 +               err = au_xino_do_trunc(sb, bindex, i, st);
37567 +       if (!err)
37568 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37569 +
37570 +out_st:
37571 +       au_kfree_rcu(st);
37572 +out:
37573 +       return err;
37574 +}
37575 +
37576 +struct xino_do_trunc_args {
37577 +       struct super_block *sb;
37578 +       struct au_branch *br;
37579 +       int idx;
37580 +};
37581 +
37582 +static void xino_do_trunc(void *_args)
37583 +{
37584 +       struct xino_do_trunc_args *args = _args;
37585 +       struct super_block *sb;
37586 +       struct au_branch *br;
37587 +       struct inode *dir;
37588 +       int err, idx;
37589 +       aufs_bindex_t bindex;
37590 +
37591 +       err = 0;
37592 +       sb = args->sb;
37593 +       dir = d_inode(sb->s_root);
37594 +       br = args->br;
37595 +       idx = args->idx;
37596 +
37597 +       si_noflush_write_lock(sb);
37598 +       ii_read_lock_parent(dir);
37599 +       bindex = au_br_index(sb, br->br_id);
37600 +       err = au_xino_trunc(sb, bindex, idx);
37601 +       ii_read_unlock(dir);
37602 +       if (unlikely(err))
37603 +               pr_warn("err b%d, (%d)\n", bindex, err);
37604 +       atomic_dec(&br->br_xino->xi_truncating);
37605 +       au_lcnt_dec(&br->br_count);
37606 +       si_write_unlock(sb);
37607 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37608 +       au_kfree_rcu(args);
37609 +}
37610 +
37611 +/*
37612 + * returns the index in the xi_file array whose corresponding file is necessary
37613 + * to truncate, or -1 which means no need to truncate.
37614 + */
37615 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37616 +{
37617 +       int err;
37618 +       unsigned int u;
37619 +       struct kstatfs st;
37620 +       struct au_sbinfo *sbinfo;
37621 +       struct au_xino *xi;
37622 +       struct file *file;
37623 +
37624 +       /* todo: si_xino_expire and the ratio should be customizable */
37625 +       sbinfo = au_sbi(sb);
37626 +       if (time_before(jiffies,
37627 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37628 +               return -1;
37629 +
37630 +       /* truncation border */
37631 +       xi = br->br_xino;
37632 +       for (u = 0; u < xi->xi_nfile; u++) {
37633 +               file = au_xino_file(xi, u);
37634 +               if (!file)
37635 +                       continue;
37636 +
37637 +               err = vfs_statfs(&file->f_path, &st);
37638 +               if (unlikely(err)) {
37639 +                       AuErr1("statfs err %d, ignored\n", err);
37640 +                       return -1;
37641 +               }
37642 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37643 +                   >= AUFS_XINO_DEF_TRUNC)
37644 +                       return u;
37645 +       }
37646 +
37647 +       return -1;
37648 +}
37649 +
37650 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37651 +{
37652 +       int idx;
37653 +       struct xino_do_trunc_args *args;
37654 +       int wkq_err;
37655 +
37656 +       idx = xino_trunc_test(sb, br);
37657 +       if (idx < 0)
37658 +               return;
37659 +
37660 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37661 +               goto out;
37662 +
37663 +       /* lock and kfree() will be called in trunc_xino() */
37664 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37665 +       if (unlikely(!args)) {
37666 +               AuErr1("no memory\n");
37667 +               goto out;
37668 +       }
37669 +
37670 +       au_lcnt_inc(&br->br_count);
37671 +       args->sb = sb;
37672 +       args->br = br;
37673 +       args->idx = idx;
37674 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37675 +       if (!wkq_err)
37676 +               return; /* success */
37677 +
37678 +       pr_err("wkq %d\n", wkq_err);
37679 +       au_lcnt_dec(&br->br_count);
37680 +       au_kfree_rcu(args);
37681 +
37682 +out:
37683 +       atomic_dec(&br->br_xino->xi_truncating);
37684 +}
37685 +
37686 +/* ---------------------------------------------------------------------- */
37687 +
37688 +struct au_xi_calc {
37689 +       int idx;
37690 +       loff_t pos;
37691 +};
37692 +
37693 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37694 +                      struct au_xi_calc *calc)
37695 +{
37696 +       loff_t maxent;
37697 +
37698 +       maxent = au_xi_maxent(sb);
37699 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37700 +       calc->pos *= sizeof(ino_t);
37701 +}
37702 +
37703 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37704 +                               struct au_xi_calc *calc)
37705 +{
37706 +       int err;
37707 +       struct file *file;
37708 +       struct au_xino *xi = br->br_xino;
37709 +       struct au_xi_new xinew = {
37710 +               .xi = xi
37711 +       };
37712 +
37713 +       SiMustAnyLock(sb);
37714 +
37715 +       err = 0;
37716 +       if (!xi)
37717 +               goto out;
37718 +
37719 +       mutex_lock(&xi->xi_mtx);
37720 +       file = au_xino_file(xi, calc->idx);
37721 +       if (file)
37722 +               goto out_mtx;
37723 +
37724 +       file = au_xino_file(xi, /*idx*/-1);
37725 +       AuDebugOn(!file);
37726 +       xinew.idx = calc->idx;
37727 +       xinew.base = &file->f_path;
37728 +       /* xinew.copy_src = NULL; */
37729 +       file = au_xi_new(sb, &xinew);
37730 +       if (IS_ERR(file))
37731 +               err = PTR_ERR(file);
37732 +
37733 +out_mtx:
37734 +       mutex_unlock(&xi->xi_mtx);
37735 +out:
37736 +       return err;
37737 +}
37738 +
37739 +struct au_xino_do_new_async_args {
37740 +       struct super_block *sb;
37741 +       struct au_branch *br;
37742 +       struct au_xi_calc calc;
37743 +       ino_t ino;
37744 +};
37745 +
37746 +struct au_xi_writing {
37747 +       struct hlist_bl_node node;
37748 +       ino_t h_ino, ino;
37749 +};
37750 +
37751 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37752 +                           ino_t ino);
37753 +
37754 +static void au_xino_call_do_new_async(void *args)
37755 +{
37756 +       struct au_xino_do_new_async_args *a = args;
37757 +       struct au_branch *br;
37758 +       struct super_block *sb;
37759 +       struct au_sbinfo *sbi;
37760 +       struct inode *root;
37761 +       struct file *file;
37762 +       struct au_xi_writing *del, *p;
37763 +       struct hlist_bl_head *hbl;
37764 +       struct hlist_bl_node *pos;
37765 +       int err;
37766 +
37767 +       br = a->br;
37768 +       sb = a->sb;
37769 +       sbi = au_sbi(sb);
37770 +       si_noflush_read_lock(sb);
37771 +       root = d_inode(sb->s_root);
37772 +       ii_read_lock_child(root);
37773 +       err = au_xino_do_new_async(sb, br, &a->calc);
37774 +       if (unlikely(err)) {
37775 +               AuIOErr("err %d\n", err);
37776 +               goto out;
37777 +       }
37778 +
37779 +       file = au_xino_file(br->br_xino, a->calc.idx);
37780 +       AuDebugOn(!file);
37781 +       err = au_xino_do_write(file, &a->calc, a->ino);
37782 +       if (unlikely(err)) {
37783 +               AuIOErr("err %d\n", err);
37784 +               goto out;
37785 +       }
37786 +
37787 +       del = NULL;
37788 +       hbl = &br->br_xino->xi_writing;
37789 +       hlist_bl_lock(hbl);
37790 +       au_hbl_for_each(pos, hbl) {
37791 +               p = container_of(pos, struct au_xi_writing, node);
37792 +               if (p->ino == a->ino) {
37793 +                       del = p;
37794 +                       hlist_bl_del(&p->node);
37795 +                       break;
37796 +               }
37797 +       }
37798 +       hlist_bl_unlock(hbl);
37799 +       au_kfree_rcu(del);
37800 +
37801 +out:
37802 +       au_lcnt_dec(&br->br_count);
37803 +       ii_read_unlock(root);
37804 +       si_read_unlock(sb);
37805 +       au_nwt_done(&sbi->si_nowait);
37806 +       au_kfree_rcu(a);
37807 +}
37808 +
37809 +/*
37810 + * create a new xino file asynchronously
37811 + */
37812 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37813 +                            struct au_xi_calc *calc, ino_t ino)
37814 +{
37815 +       int err;
37816 +       struct au_xino_do_new_async_args *arg;
37817 +
37818 +       err = -ENOMEM;
37819 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37820 +       if (unlikely(!arg))
37821 +               goto out;
37822 +
37823 +       arg->sb = sb;
37824 +       arg->br = br;
37825 +       arg->calc = *calc;
37826 +       arg->ino = ino;
37827 +       au_lcnt_inc(&br->br_count);
37828 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37829 +       if (unlikely(err)) {
37830 +               pr_err("wkq %d\n", err);
37831 +               au_lcnt_dec(&br->br_count);
37832 +               au_kfree_rcu(arg);
37833 +       }
37834 +
37835 +out:
37836 +       return err;
37837 +}
37838 +
37839 +/*
37840 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37841 + * at the position of @h_ino.
37842 + */
37843 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37844 +                ino_t *ino)
37845 +{
37846 +       int err;
37847 +       ssize_t sz;
37848 +       struct au_xi_calc calc;
37849 +       struct au_sbinfo *sbinfo;
37850 +       struct file *file;
37851 +       struct au_xino *xi;
37852 +       struct hlist_bl_head *hbl;
37853 +       struct hlist_bl_node *pos;
37854 +       struct au_xi_writing *p;
37855 +
37856 +       *ino = 0;
37857 +       if (!au_opt_test(au_mntflags(sb), XINO))
37858 +               return 0; /* no xino */
37859 +
37860 +       err = 0;
37861 +       au_xi_calc(sb, h_ino, &calc);
37862 +       xi = au_sbr(sb, bindex)->br_xino;
37863 +       file = au_xino_file(xi, calc.idx);
37864 +       if (!file) {
37865 +               hbl = &xi->xi_writing;
37866 +               hlist_bl_lock(hbl);
37867 +               au_hbl_for_each(pos, hbl) {
37868 +                       p = container_of(pos, struct au_xi_writing, node);
37869 +                       if (p->h_ino == h_ino) {
37870 +                               AuDbg("hi%llu, i%llu, found\n",
37871 +                                     (u64)p->h_ino, (u64)p->ino);
37872 +                               *ino = p->ino;
37873 +                               break;
37874 +                       }
37875 +               }
37876 +               hlist_bl_unlock(hbl);
37877 +               return 0;
37878 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37879 +               return 0; /* no xino */
37880 +
37881 +       sbinfo = au_sbi(sb);
37882 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37883 +       if (sz == sizeof(*ino))
37884 +               return 0; /* success */
37885 +
37886 +       err = sz;
37887 +       if (unlikely(sz >= 0)) {
37888 +               err = -EIO;
37889 +               AuIOErr("xino read error (%zd)\n", sz);
37890 +       }
37891 +       return err;
37892 +}
37893 +
37894 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37895 +                           ino_t ino)
37896 +{
37897 +       ssize_t sz;
37898 +
37899 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37900 +       if (sz == sizeof(ino))
37901 +               return 0; /* success */
37902 +
37903 +       AuIOErr("write failed (%zd)\n", sz);
37904 +       return -EIO;
37905 +}
37906 +
37907 +/*
37908 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37909 + * at the position of @h_ino.
37910 + * even if @ino is zero, it is written to the xinofile and means no entry.
37911 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37912 + * try truncating it.
37913 + */
37914 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37915 +                 ino_t ino)
37916 +{
37917 +       int err;
37918 +       unsigned int mnt_flags;
37919 +       struct au_xi_calc calc;
37920 +       struct file *file;
37921 +       struct au_branch *br;
37922 +       struct au_xino *xi;
37923 +       struct au_xi_writing *p;
37924 +
37925 +       SiMustAnyLock(sb);
37926 +
37927 +       mnt_flags = au_mntflags(sb);
37928 +       if (!au_opt_test(mnt_flags, XINO))
37929 +               return 0;
37930 +
37931 +       au_xi_calc(sb, h_ino, &calc);
37932 +       br = au_sbr(sb, bindex);
37933 +       xi = br->br_xino;
37934 +       file = au_xino_file(xi, calc.idx);
37935 +       if (!file) {
37936 +               /* store the inum pair into the list */
37937 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37938 +               p->h_ino = h_ino;
37939 +               p->ino = ino;
37940 +               au_hbl_add(&p->node, &xi->xi_writing);
37941 +
37942 +               /* create and write a new xino file asynchronously */
37943 +               err = au_xino_new_async(sb, br, &calc, ino);
37944 +               if (!err)
37945 +                       return 0; /* success */
37946 +               goto out;
37947 +       }
37948 +
37949 +       err = au_xino_do_write(file, &calc, ino);
37950 +       if (!err) {
37951 +               br = au_sbr(sb, bindex);
37952 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37953 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37954 +                       xino_try_trunc(sb, br);
37955 +               return 0; /* success */
37956 +       }
37957 +
37958 +out:
37959 +       AuIOErr("write failed (%d)\n", err);
37960 +       return -EIO;
37961 +}
37962 +
37963 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37964 +                             loff_t *pos);
37965 +
37966 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37967 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37968 +{
37969 +       ssize_t err;
37970 +       int i;
37971 +       const int prevent_endless = 10;
37972 +
37973 +       i = 0;
37974 +       do {
37975 +               err = vfsub_read_k(file, kbuf, size, pos);
37976 +               if (err == -EINTR
37977 +                   && !au_wkq_test()
37978 +                   && fatal_signal_pending(current)) {
37979 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37980 +                       BUG_ON(err == -EINTR);
37981 +               }
37982 +       } while (i++ < prevent_endless
37983 +                && (err == -EAGAIN || err == -EINTR));
37984 +
37985 +#if 0 /* reserved for future use */
37986 +       if (err > 0)
37987 +               fsnotify_access(file->f_path.dentry);
37988 +#endif
37989 +
37990 +       return err;
37991 +}
37992 +
37993 +struct xino_fread_args {
37994 +       ssize_t *errp;
37995 +       struct file *file;
37996 +       void *buf;
37997 +       size_t size;
37998 +       loff_t *pos;
37999 +};
38000 +
38001 +static void call_xino_fread(void *args)
38002 +{
38003 +       struct xino_fread_args *a = args;
38004 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
38005 +}
38006 +
38007 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
38008 +                             loff_t *pos)
38009 +{
38010 +       ssize_t err;
38011 +       int wkq_err;
38012 +       struct xino_fread_args args = {
38013 +               .errp   = &err,
38014 +               .file   = file,
38015 +               .buf    = buf,
38016 +               .size   = size,
38017 +               .pos    = pos
38018 +       };
38019 +
38020 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
38021 +       if (unlikely(wkq_err))
38022 +               err = wkq_err;
38023 +
38024 +       return err;
38025 +}
38026 +
38027 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38028 +                              loff_t *pos);
38029 +
38030 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
38031 +                             loff_t *pos)
38032 +{
38033 +       ssize_t err;
38034 +       int i;
38035 +       const int prevent_endless = 10;
38036 +
38037 +       i = 0;
38038 +       do {
38039 +               err = vfsub_write_k(file, kbuf, size, pos);
38040 +               if (err == -EINTR
38041 +                   && !au_wkq_test()
38042 +                   && fatal_signal_pending(current)) {
38043 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
38044 +                       BUG_ON(err == -EINTR);
38045 +               }
38046 +       } while (i++ < prevent_endless
38047 +                && (err == -EAGAIN || err == -EINTR));
38048 +
38049 +#if 0 /* reserved for future use */
38050 +       if (err > 0)
38051 +               fsnotify_modify(file->f_path.dentry);
38052 +#endif
38053 +
38054 +       return err;
38055 +}
38056 +
38057 +struct do_xino_fwrite_args {
38058 +       ssize_t *errp;
38059 +       struct file *file;
38060 +       void *buf;
38061 +       size_t size;
38062 +       loff_t *pos;
38063 +};
38064 +
38065 +static void call_do_xino_fwrite(void *args)
38066 +{
38067 +       struct do_xino_fwrite_args *a = args;
38068 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38069 +}
38070 +
38071 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38072 +                              loff_t *pos)
38073 +{
38074 +       ssize_t err;
38075 +       int wkq_err;
38076 +       struct do_xino_fwrite_args args = {
38077 +               .errp   = &err,
38078 +               .file   = file,
38079 +               .buf    = buf,
38080 +               .size   = size,
38081 +               .pos    = pos
38082 +       };
38083 +
38084 +       /*
38085 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38086 +        * users should care about quota and real 'filesystem full.'
38087 +        */
38088 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38089 +       if (unlikely(wkq_err))
38090 +               err = wkq_err;
38091 +
38092 +       return err;
38093 +}
38094 +
38095 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38096 +{
38097 +       ssize_t err;
38098 +
38099 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38100 +               lockdep_off();
38101 +               err = do_xino_fwrite(file, buf, size, pos);
38102 +               lockdep_on();
38103 +       } else {
38104 +               lockdep_off();
38105 +               err = xino_fwrite_wkq(file, buf, size, pos);
38106 +               lockdep_on();
38107 +       }
38108 +
38109 +       return err;
38110 +}
38111 +
38112 +/* ---------------------------------------------------------------------- */
38113 +
38114 +/*
38115 + * inode number bitmap
38116 + */
38117 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38118 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38119 +{
38120 +       ino_t ino;
38121 +
38122 +       AuDebugOn(bit < 0 || page_bits <= bit);
38123 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38124 +       return ino;
38125 +}
38126 +
38127 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38128 +{
38129 +       AuDebugOn(ino < AUFS_FIRST_INO);
38130 +       ino -= AUFS_FIRST_INO;
38131 +       *pindex = ino / page_bits;
38132 +       *bit = ino % page_bits;
38133 +}
38134 +
38135 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38136 +{
38137 +       int err;
38138 +       loff_t pos;
38139 +       ssize_t sz;
38140 +       struct au_sbinfo *sbinfo;
38141 +       struct file *xib;
38142 +       unsigned long *p;
38143 +
38144 +       sbinfo = au_sbi(sb);
38145 +       MtxMustLock(&sbinfo->si_xib_mtx);
38146 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38147 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38148 +
38149 +       if (pindex == sbinfo->si_xib_last_pindex)
38150 +               return 0;
38151 +
38152 +       xib = sbinfo->si_xib;
38153 +       p = sbinfo->si_xib_buf;
38154 +       pos = sbinfo->si_xib_last_pindex;
38155 +       pos *= PAGE_SIZE;
38156 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38157 +       if (unlikely(sz != PAGE_SIZE))
38158 +               goto out;
38159 +
38160 +       pos = pindex;
38161 +       pos *= PAGE_SIZE;
38162 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38163 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38164 +       else {
38165 +               memset(p, 0, PAGE_SIZE);
38166 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38167 +       }
38168 +       if (sz == PAGE_SIZE) {
38169 +               sbinfo->si_xib_last_pindex = pindex;
38170 +               return 0; /* success */
38171 +       }
38172 +
38173 +out:
38174 +       AuIOErr1("write failed (%zd)\n", sz);
38175 +       err = sz;
38176 +       if (sz >= 0)
38177 +               err = -EIO;
38178 +       return err;
38179 +}
38180 +
38181 +static void au_xib_clear_bit(struct inode *inode)
38182 +{
38183 +       int err, bit;
38184 +       unsigned long pindex;
38185 +       struct super_block *sb;
38186 +       struct au_sbinfo *sbinfo;
38187 +
38188 +       AuDebugOn(inode->i_nlink);
38189 +
38190 +       sb = inode->i_sb;
38191 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38192 +       AuDebugOn(page_bits <= bit);
38193 +       sbinfo = au_sbi(sb);
38194 +       mutex_lock(&sbinfo->si_xib_mtx);
38195 +       err = xib_pindex(sb, pindex);
38196 +       if (!err) {
38197 +               clear_bit(bit, sbinfo->si_xib_buf);
38198 +               sbinfo->si_xib_next_bit = bit;
38199 +       }
38200 +       mutex_unlock(&sbinfo->si_xib_mtx);
38201 +}
38202 +
38203 +/* ---------------------------------------------------------------------- */
38204 +
38205 +/*
38206 + * truncate a xino bitmap file
38207 + */
38208 +
38209 +/* todo: slow */
38210 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38211 +{
38212 +       int err, bit;
38213 +       ssize_t sz;
38214 +       unsigned long pindex;
38215 +       loff_t pos, pend;
38216 +       struct au_sbinfo *sbinfo;
38217 +       ino_t *ino;
38218 +       unsigned long *p;
38219 +
38220 +       err = 0;
38221 +       sbinfo = au_sbi(sb);
38222 +       MtxMustLock(&sbinfo->si_xib_mtx);
38223 +       p = sbinfo->si_xib_buf;
38224 +       pend = vfsub_f_size_read(file);
38225 +       pos = 0;
38226 +       while (pos < pend) {
38227 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38228 +               err = sz;
38229 +               if (unlikely(sz <= 0))
38230 +                       goto out;
38231 +
38232 +               err = 0;
38233 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38234 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38235 +                               continue;
38236 +
38237 +                       xib_calc_bit(*ino, &pindex, &bit);
38238 +                       AuDebugOn(page_bits <= bit);
38239 +                       err = xib_pindex(sb, pindex);
38240 +                       if (!err)
38241 +                               set_bit(bit, p);
38242 +                       else
38243 +                               goto out;
38244 +               }
38245 +       }
38246 +
38247 +out:
38248 +       return err;
38249 +}
38250 +
38251 +static int xib_restore(struct super_block *sb)
38252 +{
38253 +       int err, i;
38254 +       unsigned int nfile;
38255 +       aufs_bindex_t bindex, bbot;
38256 +       void *page;
38257 +       struct au_branch *br;
38258 +       struct au_xino *xi;
38259 +       struct file *file;
38260 +
38261 +       err = -ENOMEM;
38262 +       page = (void *)__get_free_page(GFP_NOFS);
38263 +       if (unlikely(!page))
38264 +               goto out;
38265 +
38266 +       err = 0;
38267 +       bbot = au_sbbot(sb);
38268 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38269 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38270 +                       br = au_sbr(sb, bindex);
38271 +                       xi = br->br_xino;
38272 +                       nfile = xi->xi_nfile;
38273 +                       for (i = 0; i < nfile; i++) {
38274 +                               file = au_xino_file(xi, i);
38275 +                               if (file)
38276 +                                       err = do_xib_restore(sb, file, page);
38277 +                       }
38278 +               } else
38279 +                       AuDbg("skip shared b%d\n", bindex);
38280 +       free_page((unsigned long)page);
38281 +
38282 +out:
38283 +       return err;
38284 +}
38285 +
38286 +int au_xib_trunc(struct super_block *sb)
38287 +{
38288 +       int err;
38289 +       ssize_t sz;
38290 +       loff_t pos;
38291 +       struct au_sbinfo *sbinfo;
38292 +       unsigned long *p;
38293 +       struct file *file;
38294 +
38295 +       SiMustWriteLock(sb);
38296 +
38297 +       err = 0;
38298 +       sbinfo = au_sbi(sb);
38299 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38300 +               goto out;
38301 +
38302 +       file = sbinfo->si_xib;
38303 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38304 +               goto out;
38305 +
38306 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38307 +       err = PTR_ERR(file);
38308 +       if (IS_ERR(file))
38309 +               goto out;
38310 +       fput(sbinfo->si_xib);
38311 +       sbinfo->si_xib = file;
38312 +
38313 +       p = sbinfo->si_xib_buf;
38314 +       memset(p, 0, PAGE_SIZE);
38315 +       pos = 0;
38316 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38317 +       if (unlikely(sz != PAGE_SIZE)) {
38318 +               err = sz;
38319 +               AuIOErr("err %d\n", err);
38320 +               if (sz >= 0)
38321 +                       err = -EIO;
38322 +               goto out;
38323 +       }
38324 +
38325 +       mutex_lock(&sbinfo->si_xib_mtx);
38326 +       /* mnt_want_write() is unnecessary here */
38327 +       err = xib_restore(sb);
38328 +       mutex_unlock(&sbinfo->si_xib_mtx);
38329 +
38330 +out:
38331 +       return err;
38332 +}
38333 +
38334 +/* ---------------------------------------------------------------------- */
38335 +
38336 +struct au_xino *au_xino_alloc(unsigned int nfile)
38337 +{
38338 +       struct au_xino *xi;
38339 +
38340 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38341 +       if (unlikely(!xi))
38342 +               goto out;
38343 +       xi->xi_nfile = nfile;
38344 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38345 +       if (unlikely(!xi->xi_file))
38346 +               goto out_free;
38347 +
38348 +       xi->xi_nondir.total = 8; /* initial size */
38349 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38350 +                                     GFP_NOFS);
38351 +       if (unlikely(!xi->xi_nondir.array))
38352 +               goto out_file;
38353 +
38354 +       spin_lock_init(&xi->xi_nondir.spin);
38355 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38356 +       mutex_init(&xi->xi_mtx);
38357 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38358 +       atomic_set(&xi->xi_truncating, 0);
38359 +       kref_init(&xi->xi_kref);
38360 +       goto out; /* success */
38361 +
38362 +out_file:
38363 +       au_kfree_try_rcu(xi->xi_file);
38364 +out_free:
38365 +       au_kfree_rcu(xi);
38366 +       xi = NULL;
38367 +out:
38368 +       return xi;
38369 +}
38370 +
38371 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38372 +{
38373 +       int err;
38374 +       struct au_xino *xi;
38375 +
38376 +       err = 0;
38377 +       xi = au_xino_alloc(idx + 1);
38378 +       if (unlikely(!xi)) {
38379 +               err = -ENOMEM;
38380 +               goto out;
38381 +       }
38382 +
38383 +       if (file)
38384 +               get_file(file);
38385 +       xi->xi_file[idx] = file;
38386 +       AuDebugOn(br->br_xino);
38387 +       br->br_xino = xi;
38388 +
38389 +out:
38390 +       return err;
38391 +}
38392 +
38393 +static void au_xino_release(struct kref *kref)
38394 +{
38395 +       struct au_xino *xi;
38396 +       int i;
38397 +       unsigned long ul;
38398 +       struct hlist_bl_head *hbl;
38399 +       struct hlist_bl_node *pos, *n;
38400 +       struct au_xi_writing *p;
38401 +
38402 +       xi = container_of(kref, struct au_xino, xi_kref);
38403 +       for (i = 0; i < xi->xi_nfile; i++)
38404 +               if (xi->xi_file[i])
38405 +                       fput(xi->xi_file[i]);
38406 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38407 +               AuDebugOn(xi->xi_nondir.array[i]);
38408 +       mutex_destroy(&xi->xi_mtx);
38409 +       hbl = &xi->xi_writing;
38410 +       ul = au_hbl_count(hbl);
38411 +       if (unlikely(ul)) {
38412 +               pr_warn("xi_writing %lu\n", ul);
38413 +               hlist_bl_lock(hbl);
38414 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38415 +                       hlist_bl_del(&p->node);
38416 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38417 +                       kfree(p);
38418 +               }
38419 +               hlist_bl_unlock(hbl);
38420 +       }
38421 +       au_kfree_try_rcu(xi->xi_file);
38422 +       au_kfree_try_rcu(xi->xi_nondir.array);
38423 +       au_kfree_rcu(xi);
38424 +}
38425 +
38426 +int au_xino_put(struct au_branch *br)
38427 +{
38428 +       int ret;
38429 +       struct au_xino *xi;
38430 +
38431 +       ret = 0;
38432 +       xi = br->br_xino;
38433 +       if (xi) {
38434 +               br->br_xino = NULL;
38435 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38436 +       }
38437 +
38438 +       return ret;
38439 +}
38440 +
38441 +/* ---------------------------------------------------------------------- */
38442 +
38443 +/*
38444 + * xino mount option handlers
38445 + */
38446 +
38447 +/* xino bitmap */
38448 +static void xino_clear_xib(struct super_block *sb)
38449 +{
38450 +       struct au_sbinfo *sbinfo;
38451 +
38452 +       SiMustWriteLock(sb);
38453 +
38454 +       sbinfo = au_sbi(sb);
38455 +       if (sbinfo->si_xib)
38456 +               fput(sbinfo->si_xib);
38457 +       sbinfo->si_xib = NULL;
38458 +       if (sbinfo->si_xib_buf)
38459 +               free_page((unsigned long)sbinfo->si_xib_buf);
38460 +       sbinfo->si_xib_buf = NULL;
38461 +}
38462 +
38463 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38464 +{
38465 +       int err;
38466 +       loff_t pos;
38467 +       struct au_sbinfo *sbinfo;
38468 +       struct file *file;
38469 +       struct super_block *xi_sb;
38470 +
38471 +       SiMustWriteLock(sb);
38472 +
38473 +       sbinfo = au_sbi(sb);
38474 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38475 +       err = PTR_ERR(file);
38476 +       if (IS_ERR(file))
38477 +               goto out;
38478 +       if (sbinfo->si_xib)
38479 +               fput(sbinfo->si_xib);
38480 +       sbinfo->si_xib = file;
38481 +       xi_sb = file_inode(file)->i_sb;
38482 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38483 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38484 +               err = -EIO;
38485 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38486 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38487 +               goto out_unset;
38488 +       }
38489 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38490 +
38491 +       err = -ENOMEM;
38492 +       if (!sbinfo->si_xib_buf)
38493 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38494 +       if (unlikely(!sbinfo->si_xib_buf))
38495 +               goto out_unset;
38496 +
38497 +       sbinfo->si_xib_last_pindex = 0;
38498 +       sbinfo->si_xib_next_bit = 0;
38499 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38500 +               pos = 0;
38501 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38502 +               if (unlikely(err != PAGE_SIZE))
38503 +                       goto out_free;
38504 +       }
38505 +       err = 0;
38506 +       goto out; /* success */
38507 +
38508 +out_free:
38509 +       if (sbinfo->si_xib_buf)
38510 +               free_page((unsigned long)sbinfo->si_xib_buf);
38511 +       sbinfo->si_xib_buf = NULL;
38512 +       if (err >= 0)
38513 +               err = -EIO;
38514 +out_unset:
38515 +       fput(sbinfo->si_xib);
38516 +       sbinfo->si_xib = NULL;
38517 +out:
38518 +       AuTraceErr(err);
38519 +       return err;
38520 +}
38521 +
38522 +/* xino for each branch */
38523 +static void xino_clear_br(struct super_block *sb)
38524 +{
38525 +       aufs_bindex_t bindex, bbot;
38526 +       struct au_branch *br;
38527 +
38528 +       bbot = au_sbbot(sb);
38529 +       for (bindex = 0; bindex <= bbot; bindex++) {
38530 +               br = au_sbr(sb, bindex);
38531 +               AuDebugOn(!br);
38532 +               au_xino_put(br);
38533 +       }
38534 +}
38535 +
38536 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38537 +                                 aufs_bindex_t bshared)
38538 +{
38539 +       struct au_branch *brshared;
38540 +
38541 +       brshared = au_sbr(sb, bshared);
38542 +       AuDebugOn(!brshared->br_xino);
38543 +       AuDebugOn(!brshared->br_xino->xi_file);
38544 +       if (br->br_xino != brshared->br_xino) {
38545 +               au_xino_get(brshared);
38546 +               au_xino_put(br);
38547 +               br->br_xino = brshared->br_xino;
38548 +       }
38549 +}
38550 +
38551 +struct au_xino_do_set_br {
38552 +       struct au_branch *br;
38553 +       ino_t h_ino;
38554 +       aufs_bindex_t bshared;
38555 +};
38556 +
38557 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38558 +                            struct au_xino_do_set_br *args)
38559 +{
38560 +       int err;
38561 +       struct au_xi_calc calc;
38562 +       struct file *file;
38563 +       struct au_branch *br;
38564 +       struct au_xi_new xinew = {
38565 +               .base = path
38566 +       };
38567 +
38568 +       br = args->br;
38569 +       xinew.xi = br->br_xino;
38570 +       au_xi_calc(sb, args->h_ino, &calc);
38571 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38572 +       if (args->bshared >= 0)
38573 +               /* shared xino */
38574 +               au_xino_set_br_shared(sb, br, args->bshared);
38575 +       else if (!xinew.xi) {
38576 +               /* new xino */
38577 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38578 +               if (unlikely(err))
38579 +                       goto out;
38580 +       }
38581 +
38582 +       /* force re-creating */
38583 +       xinew.xi = br->br_xino;
38584 +       xinew.idx = calc.idx;
38585 +       mutex_lock(&xinew.xi->xi_mtx);
38586 +       file = au_xi_new(sb, &xinew);
38587 +       mutex_unlock(&xinew.xi->xi_mtx);
38588 +       err = PTR_ERR(file);
38589 +       if (IS_ERR(file))
38590 +               goto out;
38591 +       AuDebugOn(!file);
38592 +
38593 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38594 +       if (unlikely(err))
38595 +               au_xino_put(br);
38596 +
38597 +out:
38598 +       AuTraceErr(err);
38599 +       return err;
38600 +}
38601 +
38602 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38603 +{
38604 +       int err;
38605 +       aufs_bindex_t bindex, bbot;
38606 +       struct au_xino_do_set_br args;
38607 +       struct inode *inode;
38608 +
38609 +       SiMustWriteLock(sb);
38610 +
38611 +       bbot = au_sbbot(sb);
38612 +       inode = d_inode(sb->s_root);
38613 +       for (bindex = 0; bindex <= bbot; bindex++) {
38614 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38615 +               args.br = au_sbr(sb, bindex);
38616 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38617 +               err = au_xino_do_set_br(sb, path, &args);
38618 +               if (unlikely(err))
38619 +                       break;
38620 +       }
38621 +
38622 +       AuTraceErr(err);
38623 +       return err;
38624 +}
38625 +
38626 +void au_xino_clr(struct super_block *sb)
38627 +{
38628 +       struct au_sbinfo *sbinfo;
38629 +
38630 +       au_xigen_clr(sb);
38631 +       xino_clear_xib(sb);
38632 +       xino_clear_br(sb);
38633 +       dbgaufs_brs_del(sb, 0);
38634 +       sbinfo = au_sbi(sb);
38635 +       /* lvalue, do not call au_mntflags() */
38636 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38637 +}
38638 +
38639 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38640 +{
38641 +       int err, skip;
38642 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38643 +       struct qstr *dname, *cur_name;
38644 +       struct file *cur_xino;
38645 +       struct au_sbinfo *sbinfo;
38646 +       struct path *path, *cur_path;
38647 +
38648 +       SiMustWriteLock(sb);
38649 +
38650 +       err = 0;
38651 +       sbinfo = au_sbi(sb);
38652 +       path = &xiopt->file->f_path;
38653 +       dentry = path->dentry;
38654 +       parent = dget_parent(dentry);
38655 +       if (remount) {
38656 +               skip = 0;
38657 +               cur_xino = sbinfo->si_xib;
38658 +               if (cur_xino) {
38659 +                       cur_path = &cur_xino->f_path;
38660 +                       cur_dentry = cur_path->dentry;
38661 +                       cur_parent = dget_parent(cur_dentry);
38662 +                       cur_name = &cur_dentry->d_name;
38663 +                       dname = &dentry->d_name;
38664 +                       skip = (cur_parent == parent
38665 +                               && au_qstreq(dname, cur_name));
38666 +                       dput(cur_parent);
38667 +               }
38668 +               if (skip)
38669 +                       goto out;
38670 +       }
38671 +
38672 +       au_opt_set(sbinfo->si_mntflags, XINO);
38673 +       err = au_xino_set_xib(sb, path);
38674 +       /* si_x{read,write} are set */
38675 +       if (!err)
38676 +               err = au_xigen_set(sb, path);
38677 +       if (!err)
38678 +               err = au_xino_set_br(sb, path);
38679 +       if (!err) {
38680 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38681 +               goto out; /* success */
38682 +       }
38683 +
38684 +       /* reset all */
38685 +       AuIOErr("failed setting xino(%d).\n", err);
38686 +       au_xino_clr(sb);
38687 +
38688 +out:
38689 +       dput(parent);
38690 +       return err;
38691 +}
38692 +
38693 +/*
38694 + * create a xinofile at the default place/path.
38695 + */
38696 +struct file *au_xino_def(struct super_block *sb)
38697 +{
38698 +       struct file *file;
38699 +       char *page, *p;
38700 +       struct au_branch *br;
38701 +       struct super_block *h_sb;
38702 +       struct path path;
38703 +       aufs_bindex_t bbot, bindex, bwr;
38704 +
38705 +       br = NULL;
38706 +       bbot = au_sbbot(sb);
38707 +       bwr = -1;
38708 +       for (bindex = 0; bindex <= bbot; bindex++) {
38709 +               br = au_sbr(sb, bindex);
38710 +               if (au_br_writable(br->br_perm)
38711 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38712 +                       bwr = bindex;
38713 +                       break;
38714 +               }
38715 +       }
38716 +
38717 +       if (bwr >= 0) {
38718 +               file = ERR_PTR(-ENOMEM);
38719 +               page = (void *)__get_free_page(GFP_NOFS);
38720 +               if (unlikely(!page))
38721 +                       goto out;
38722 +               path.mnt = au_br_mnt(br);
38723 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38724 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38725 +               file = (void *)p;
38726 +               if (!IS_ERR(p)) {
38727 +                       strcat(p, "/" AUFS_XINO_FNAME);
38728 +                       AuDbg("%s\n", p);
38729 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38730 +               }
38731 +               free_page((unsigned long)page);
38732 +       } else {
38733 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38734 +                                     /*wbrtop*/0);
38735 +               if (IS_ERR(file))
38736 +                       goto out;
38737 +               h_sb = file->f_path.dentry->d_sb;
38738 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38739 +                       pr_err("xino doesn't support %s(%s)\n",
38740 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38741 +                       fput(file);
38742 +                       file = ERR_PTR(-EINVAL);
38743 +               }
38744 +       }
38745 +
38746 +out:
38747 +       return file;
38748 +}
38749 +
38750 +/* ---------------------------------------------------------------------- */
38751 +
38752 +/*
38753 + * initialize the xinofile for the specified branch @br
38754 + * at the place/path where @base_file indicates.
38755 + * test whether another branch is on the same filesystem or not,
38756 + * if found then share the xinofile with another branch.
38757 + */
38758 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38759 +                   struct path *base)
38760 +{
38761 +       int err;
38762 +       struct au_xino_do_set_br args = {
38763 +               .h_ino  = h_ino,
38764 +               .br     = br
38765 +       };
38766 +
38767 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38768 +                                      au_br_sb(br));
38769 +       err = au_xino_do_set_br(sb, base, &args);
38770 +       if (unlikely(err))
38771 +               au_xino_put(br);
38772 +
38773 +       return err;
38774 +}
38775 +
38776 +/* ---------------------------------------------------------------------- */
38777 +
38778 +/*
38779 + * get an unused inode number from bitmap
38780 + */
38781 +ino_t au_xino_new_ino(struct super_block *sb)
38782 +{
38783 +       ino_t ino;
38784 +       unsigned long *p, pindex, ul, pend;
38785 +       struct au_sbinfo *sbinfo;
38786 +       struct file *file;
38787 +       int free_bit, err;
38788 +
38789 +       if (!au_opt_test(au_mntflags(sb), XINO))
38790 +               return iunique(sb, AUFS_FIRST_INO);
38791 +
38792 +       sbinfo = au_sbi(sb);
38793 +       mutex_lock(&sbinfo->si_xib_mtx);
38794 +       p = sbinfo->si_xib_buf;
38795 +       free_bit = sbinfo->si_xib_next_bit;
38796 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38797 +               goto out; /* success */
38798 +       free_bit = find_first_zero_bit(p, page_bits);
38799 +       if (free_bit < page_bits)
38800 +               goto out; /* success */
38801 +
38802 +       pindex = sbinfo->si_xib_last_pindex;
38803 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38804 +               err = xib_pindex(sb, ul);
38805 +               if (unlikely(err))
38806 +                       goto out_err;
38807 +               free_bit = find_first_zero_bit(p, page_bits);
38808 +               if (free_bit < page_bits)
38809 +                       goto out; /* success */
38810 +       }
38811 +
38812 +       file = sbinfo->si_xib;
38813 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38814 +       for (ul = pindex + 1; ul <= pend; ul++) {
38815 +               err = xib_pindex(sb, ul);
38816 +               if (unlikely(err))
38817 +                       goto out_err;
38818 +               free_bit = find_first_zero_bit(p, page_bits);
38819 +               if (free_bit < page_bits)
38820 +                       goto out; /* success */
38821 +       }
38822 +       BUG();
38823 +
38824 +out:
38825 +       set_bit(free_bit, p);
38826 +       sbinfo->si_xib_next_bit = free_bit + 1;
38827 +       pindex = sbinfo->si_xib_last_pindex;
38828 +       mutex_unlock(&sbinfo->si_xib_mtx);
38829 +       ino = xib_calc_ino(pindex, free_bit);
38830 +       AuDbg("i%lu\n", (unsigned long)ino);
38831 +       return ino;
38832 +out_err:
38833 +       mutex_unlock(&sbinfo->si_xib_mtx);
38834 +       AuDbg("i0\n");
38835 +       return 0;
38836 +}
38837 +
38838 +/* for s_op->delete_inode() */
38839 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38840 +{
38841 +       int err;
38842 +       unsigned int mnt_flags;
38843 +       aufs_bindex_t bindex, bbot, bi;
38844 +       unsigned char try_trunc;
38845 +       struct au_iinfo *iinfo;
38846 +       struct super_block *sb;
38847 +       struct au_hinode *hi;
38848 +       struct inode *h_inode;
38849 +       struct au_branch *br;
38850 +       struct au_xi_calc calc;
38851 +       struct file *file;
38852 +
38853 +       AuDebugOn(au_is_bad_inode(inode));
38854 +
38855 +       sb = inode->i_sb;
38856 +       mnt_flags = au_mntflags(sb);
38857 +       if (!au_opt_test(mnt_flags, XINO)
38858 +           || inode->i_ino == AUFS_ROOT_INO)
38859 +               return;
38860 +
38861 +       if (unlinked) {
38862 +               au_xigen_inc(inode);
38863 +               au_xib_clear_bit(inode);
38864 +       }
38865 +
38866 +       iinfo = au_ii(inode);
38867 +       bindex = iinfo->ii_btop;
38868 +       if (bindex < 0)
38869 +               return;
38870 +
38871 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38872 +       hi = au_hinode(iinfo, bindex);
38873 +       bbot = iinfo->ii_bbot;
38874 +       for (; bindex <= bbot; bindex++, hi++) {
38875 +               h_inode = hi->hi_inode;
38876 +               if (!h_inode
38877 +                   || (!unlinked && h_inode->i_nlink))
38878 +                       continue;
38879 +
38880 +               /* inode may not be revalidated */
38881 +               bi = au_br_index(sb, hi->hi_id);
38882 +               if (bi < 0)
38883 +                       continue;
38884 +
38885 +               br = au_sbr(sb, bi);
38886 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38887 +               file = au_xino_file(br->br_xino, calc.idx);
38888 +               if (IS_ERR_OR_NULL(file))
38889 +                       continue;
38890 +
38891 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38892 +               if (!err && try_trunc
38893 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38894 +                       xino_try_trunc(sb, br);
38895 +       }
38896 +}
38897 +
38898 +/* ---------------------------------------------------------------------- */
38899 +
38900 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38901 +{
38902 +       int found, total, i;
38903 +
38904 +       found = -1;
38905 +       total = xi->xi_nondir.total;
38906 +       for (i = 0; i < total; i++) {
38907 +               if (xi->xi_nondir.array[i] != h_ino)
38908 +                       continue;
38909 +               found = i;
38910 +               break;
38911 +       }
38912 +
38913 +       return found;
38914 +}
38915 +
38916 +static int au_xinondir_expand(struct au_xino *xi)
38917 +{
38918 +       int err, sz;
38919 +       ino_t *p;
38920 +
38921 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38922 +
38923 +       err = -ENOMEM;
38924 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38925 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38926 +               goto out;
38927 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38928 +                        /*may_shrink*/0);
38929 +       if (p) {
38930 +               xi->xi_nondir.array = p;
38931 +               xi->xi_nondir.total <<= 1;
38932 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38933 +               err = 0;
38934 +       }
38935 +
38936 +out:
38937 +       return err;
38938 +}
38939 +
38940 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38941 +                      ino_t h_ino, int idx)
38942 +{
38943 +       struct au_xino *xi;
38944 +
38945 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38946 +       xi = au_sbr(sb, bindex)->br_xino;
38947 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38948 +
38949 +       spin_lock(&xi->xi_nondir.spin);
38950 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38951 +       xi->xi_nondir.array[idx] = 0;
38952 +       spin_unlock(&xi->xi_nondir.spin);
38953 +       wake_up_all(&xi->xi_nondir.wqh);
38954 +}
38955 +
38956 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38957 +                     int *idx)
38958 +{
38959 +       int err, found, empty;
38960 +       struct au_xino *xi;
38961 +
38962 +       err = 0;
38963 +       *idx = -1;
38964 +       if (!au_opt_test(au_mntflags(sb), XINO))
38965 +               goto out; /* no xino */
38966 +
38967 +       xi = au_sbr(sb, bindex)->br_xino;
38968 +
38969 +again:
38970 +       spin_lock(&xi->xi_nondir.spin);
38971 +       found = au_xinondir_find(xi, h_ino);
38972 +       if (found == -1) {
38973 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38974 +               if (empty == -1) {
38975 +                       empty = xi->xi_nondir.total;
38976 +                       err = au_xinondir_expand(xi);
38977 +                       if (unlikely(err))
38978 +                               goto out_unlock;
38979 +               }
38980 +               xi->xi_nondir.array[empty] = h_ino;
38981 +               *idx = empty;
38982 +       } else {
38983 +               spin_unlock(&xi->xi_nondir.spin);
38984 +               wait_event(xi->xi_nondir.wqh,
38985 +                          xi->xi_nondir.array[found] != h_ino);
38986 +               goto again;
38987 +       }
38988 +
38989 +out_unlock:
38990 +       spin_unlock(&xi->xi_nondir.spin);
38991 +out:
38992 +       return err;
38993 +}
38994 +
38995 +/* ---------------------------------------------------------------------- */
38996 +
38997 +int au_xino_path(struct seq_file *seq, struct file *file)
38998 +{
38999 +       int err;
39000 +
39001 +       err = au_seq_path(seq, &file->f_path);
39002 +       if (unlikely(err))
39003 +               goto out;
39004 +
39005 +#define Deleted "\\040(deleted)"
39006 +       seq->count -= sizeof(Deleted) - 1;
39007 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
39008 +                        sizeof(Deleted) - 1));
39009 +#undef Deleted
39010 +
39011 +out:
39012 +       return err;
39013 +}
39014 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
39015 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
39016 +++ linux/include/uapi/linux/aufs_type.h        2022-12-17 09:21:34.803188528 +0100
39017 @@ -0,0 +1,452 @@
39018 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
39019 +/*
39020 + * Copyright (C) 2005-2022 Junjiro R. Okajima
39021 + *
39022 + * This program is free software; you can redistribute it and/or modify
39023 + * it under the terms of the GNU General Public License as published by
39024 + * the Free Software Foundation; either version 2 of the License, or
39025 + * (at your option) any later version.
39026 + *
39027 + * This program is distributed in the hope that it will be useful,
39028 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
39029 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39030 + * GNU General Public License for more details.
39031 + *
39032 + * You should have received a copy of the GNU General Public License
39033 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
39034 + */
39035 +
39036 +#ifndef __AUFS_TYPE_H__
39037 +#define __AUFS_TYPE_H__
39038 +
39039 +#define AUFS_NAME      "aufs"
39040 +
39041 +#ifdef __KERNEL__
39042 +/*
39043 + * define it before including all other headers.
39044 + * sched.h may use pr_* macros before defining "current", so define the
39045 + * no-current version first, and re-define later.
39046 + */
39047 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39048 +#include <linux/sched.h>
39049 +#undef pr_fmt
39050 +#define pr_fmt(fmt) \
39051 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39052 +               (int)sizeof(current->comm), current->comm, current->pid
39053 +#include <linux/limits.h>
39054 +#else
39055 +#include <stdint.h>
39056 +#include <sys/types.h>
39057 +#include <limits.h>
39058 +#endif /* __KERNEL__ */
39059 +
39060 +#define AUFS_VERSION   "6.x-rcN-20221107"
39061 +
39062 +/* todo? move this to linux-2.6.19/include/magic.h */
39063 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39064 +
39065 +/* ---------------------------------------------------------------------- */
39066 +
39067 +#ifdef __KERNEL__
39068 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39069 +typedef int8_t aufs_bindex_t;
39070 +#define AUFS_BRANCH_MAX 127
39071 +#else
39072 +typedef int16_t aufs_bindex_t;
39073 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39074 +#define AUFS_BRANCH_MAX 511
39075 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39076 +#define AUFS_BRANCH_MAX 1023
39077 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39078 +#define AUFS_BRANCH_MAX 32767
39079 +#endif
39080 +#endif
39081 +
39082 +#ifndef AUFS_BRANCH_MAX
39083 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39084 +#endif
39085 +#endif /* __KERNEL__ */
39086 +
39087 +/* ---------------------------------------------------------------------- */
39088 +
39089 +#define AUFS_FSTYPE            AUFS_NAME
39090 +
39091 +#define AUFS_ROOT_INO          2
39092 +#define AUFS_FIRST_INO         11
39093 +
39094 +#define AUFS_WH_PFX            ".wh."
39095 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39096 +#define AUFS_WH_TMP_LEN                4
39097 +/* a limit for rmdir/rename a dir and copyup */
39098 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39099 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39100 +                               - 1                     /* dot */\
39101 +                               - AUFS_WH_TMP_LEN)      /* hex */
39102 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39103 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39104 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39105 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39106 +#define AUFS_DIRWH_DEF         3
39107 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39108 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39109 +#define AUFS_RDBLK_DEF         512 /* bytes */
39110 +#define AUFS_RDHASH_DEF                32
39111 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39112 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39113 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39114 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39115 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39116 +
39117 +/* pseudo-link maintenace under /proc */
39118 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39119 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39120 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39121 +
39122 +/* dirren, renamed dir */
39123 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39124 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39125 +/* whiteouted doubly */
39126 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39127 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39128 +
39129 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39130 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39131 +
39132 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39133 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39134 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39135 +
39136 +/* doubly whiteouted */
39137 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39138 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39139 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39140 +
39141 +/* branch permissions and attributes */
39142 +#define AUFS_BRPERM_RW         "rw"
39143 +#define AUFS_BRPERM_RO         "ro"
39144 +#define AUFS_BRPERM_RR         "rr"
39145 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39146 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39147 +#define AUFS_BRATTR_FHSM       "fhsm"
39148 +#define AUFS_BRATTR_UNPIN      "unpin"
39149 +#define AUFS_BRATTR_ICEX       "icex"
39150 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39151 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39152 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39153 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39154 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39155 +#define AUFS_BRRATTR_WH                "wh"
39156 +#define AUFS_BRWATTR_NLWH      "nolwh"
39157 +#define AUFS_BRWATTR_MOO       "moo"
39158 +
39159 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39160 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39161 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39162 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39163 +
39164 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39165 +#define AuBrAttr_COO_ALL       (1 << 4)
39166 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39167 +
39168 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39169 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39170 +                                                  branch. meaningless since
39171 +                                                  linux-3.18-rc1 */
39172 +
39173 +/* ignore error in copying XATTR */
39174 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39175 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39176 +#define AuBrAttr_ICEX_TR       (1 << 9)
39177 +#define AuBrAttr_ICEX_USR      (1 << 10)
39178 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39179 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39180 +                                | AuBrAttr_ICEX_SYS    \
39181 +                                | AuBrAttr_ICEX_TR     \
39182 +                                | AuBrAttr_ICEX_USR    \
39183 +                                | AuBrAttr_ICEX_OTH)
39184 +
39185 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39186 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39187 +
39188 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39189 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39190 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39191 +
39192 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39193 +
39194 +/* #warning test userspace */
39195 +#ifdef __KERNEL__
39196 +#ifndef CONFIG_AUFS_FHSM
39197 +#undef AuBrAttr_FHSM
39198 +#define AuBrAttr_FHSM          0
39199 +#endif
39200 +#ifndef CONFIG_AUFS_XATTR
39201 +#undef AuBrAttr_ICEX
39202 +#define AuBrAttr_ICEX          0
39203 +#undef AuBrAttr_ICEX_SEC
39204 +#define AuBrAttr_ICEX_SEC      0
39205 +#undef AuBrAttr_ICEX_SYS
39206 +#define AuBrAttr_ICEX_SYS      0
39207 +#undef AuBrAttr_ICEX_TR
39208 +#define AuBrAttr_ICEX_TR       0
39209 +#undef AuBrAttr_ICEX_USR
39210 +#define AuBrAttr_ICEX_USR      0
39211 +#undef AuBrAttr_ICEX_OTH
39212 +#define AuBrAttr_ICEX_OTH      0
39213 +#endif
39214 +#endif
39215 +
39216 +/* the longest combination */
39217 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39218 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39219 +                              "+" AUFS_BRATTR_COO_REG          \
39220 +                              "+" AUFS_BRATTR_FHSM             \
39221 +                              "+" AUFS_BRATTR_UNPIN            \
39222 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39223 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39224 +                              "+" AUFS_BRATTR_ICEX_USR         \
39225 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39226 +                              "+" AUFS_BRWATTR_NLWH)
39227 +
39228 +typedef struct {
39229 +       char a[AuBrPermStrSz];
39230 +} au_br_perm_str_t;
39231 +
39232 +static inline int au_br_writable(int brperm)
39233 +{
39234 +       return brperm & AuBrPerm_RW;
39235 +}
39236 +
39237 +static inline int au_br_whable(int brperm)
39238 +{
39239 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39240 +}
39241 +
39242 +static inline int au_br_wh_linkable(int brperm)
39243 +{
39244 +       return !(brperm & AuBrWAttr_NoLinkWH);
39245 +}
39246 +
39247 +static inline int au_br_cmoo(int brperm)
39248 +{
39249 +       return brperm & AuBrAttr_CMOO_Mask;
39250 +}
39251 +
39252 +static inline int au_br_fhsm(int brperm)
39253 +{
39254 +       return brperm & AuBrAttr_FHSM;
39255 +}
39256 +
39257 +/* ---------------------------------------------------------------------- */
39258 +
39259 +/* ioctl */
39260 +enum {
39261 +       /* readdir in userspace */
39262 +       AuCtl_RDU,
39263 +       AuCtl_RDU_INO,
39264 +
39265 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39266 +       AuCtl_IBUSY,    /* busy inode */
39267 +       AuCtl_MVDOWN,   /* move-down */
39268 +       AuCtl_BR,       /* info about branches */
39269 +       AuCtl_FHSM_FD   /* connection for fhsm */
39270 +};
39271 +
39272 +/* borrowed from linux/include/linux/kernel.h */
39273 +#ifndef ALIGN
39274 +#ifdef _GNU_SOURCE
39275 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39276 +#else
39277 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39278 +#endif
39279 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39280 +#endif
39281 +
39282 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39283 +#ifndef __aligned
39284 +#define __aligned(x)                   __attribute__((aligned(x)))
39285 +#endif
39286 +
39287 +#ifdef __KERNEL__
39288 +#ifndef __packed
39289 +#define __packed                       __attribute__((packed))
39290 +#endif
39291 +#endif
39292 +
39293 +struct au_rdu_cookie {
39294 +       uint64_t        h_pos;
39295 +       int16_t         bindex;
39296 +       uint8_t         flags;
39297 +       uint8_t         pad;
39298 +       uint32_t        generation;
39299 +} __aligned(8);
39300 +
39301 +struct au_rdu_ent {
39302 +       uint64_t        ino;
39303 +       int16_t         bindex;
39304 +       uint8_t         type;
39305 +       uint8_t         nlen;
39306 +       uint8_t         wh;
39307 +       char            name[];
39308 +} __aligned(8);
39309 +
39310 +static inline int au_rdu_len(int nlen)
39311 +{
39312 +       /* include the terminating NULL */
39313 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39314 +                    sizeof(uint64_t));
39315 +}
39316 +
39317 +union au_rdu_ent_ul {
39318 +       struct au_rdu_ent __user        *e;
39319 +       uint64_t                        ul;
39320 +};
39321 +
39322 +enum {
39323 +       AufsCtlRduV_SZ,
39324 +       AufsCtlRduV_End
39325 +};
39326 +
39327 +struct aufs_rdu {
39328 +       /* input */
39329 +       union {
39330 +               uint64_t        sz;     /* AuCtl_RDU */
39331 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39332 +       };
39333 +       union au_rdu_ent_ul     ent;
39334 +       uint16_t                verify[AufsCtlRduV_End];
39335 +
39336 +       /* input/output */
39337 +       uint32_t                blk;
39338 +
39339 +       /* output */
39340 +       union au_rdu_ent_ul     tail;
39341 +       /* number of entries which were added in a single call */
39342 +       uint64_t                rent;
39343 +       uint8_t                 full;
39344 +       uint8_t                 shwh;
39345 +
39346 +       struct au_rdu_cookie    cookie;
39347 +} __aligned(8);
39348 +
39349 +/* ---------------------------------------------------------------------- */
39350 +
39351 +/* dirren. the branch is identified by the filename who contains this */
39352 +struct au_drinfo {
39353 +       uint64_t ino;
39354 +       union {
39355 +               uint8_t oldnamelen;
39356 +               uint64_t _padding;
39357 +       };
39358 +       uint8_t oldname[];
39359 +} __aligned(8);
39360 +
39361 +struct au_drinfo_fdata {
39362 +       uint32_t magic;
39363 +       struct au_drinfo drinfo;
39364 +} __aligned(8);
39365 +
39366 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39367 +/* future */
39368 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39369 +
39370 +/* ---------------------------------------------------------------------- */
39371 +
39372 +struct aufs_wbr_fd {
39373 +       uint32_t        oflags;
39374 +       int16_t         brid;
39375 +} __aligned(8);
39376 +
39377 +/* ---------------------------------------------------------------------- */
39378 +
39379 +struct aufs_ibusy {
39380 +       uint64_t        ino, h_ino;
39381 +       int16_t         bindex;
39382 +} __aligned(8);
39383 +
39384 +/* ---------------------------------------------------------------------- */
39385 +
39386 +/* error code for move-down */
39387 +/* the actual message strings are implemented in aufs-util.git */
39388 +enum {
39389 +       EAU_MVDOWN_OPAQUE = 1,
39390 +       EAU_MVDOWN_WHITEOUT,
39391 +       EAU_MVDOWN_UPPER,
39392 +       EAU_MVDOWN_BOTTOM,
39393 +       EAU_MVDOWN_NOUPPER,
39394 +       EAU_MVDOWN_NOLOWERBR,
39395 +       EAU_Last
39396 +};
39397 +
39398 +/* flags for move-down */
39399 +#define AUFS_MVDOWN_DMSG       1
39400 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39401 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39402 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39403 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39404 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39405 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39406 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39407 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39408 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39409 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39410 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39411 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39412 +
39413 +/* index for move-down */
39414 +enum {
39415 +       AUFS_MVDOWN_UPPER,
39416 +       AUFS_MVDOWN_LOWER,
39417 +       AUFS_MVDOWN_NARRAY
39418 +};
39419 +
39420 +/*
39421 + * additional info of move-down
39422 + * number of free blocks and inodes.
39423 + * subset of struct kstatfs, but smaller and always 64bit.
39424 + */
39425 +struct aufs_stfs {
39426 +       uint64_t        f_blocks;
39427 +       uint64_t        f_bavail;
39428 +       uint64_t        f_files;
39429 +       uint64_t        f_ffree;
39430 +};
39431 +
39432 +struct aufs_stbr {
39433 +       int16_t                 brid;   /* optional input */
39434 +       int16_t                 bindex; /* output */
39435 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39436 +} __aligned(8);
39437 +
39438 +struct aufs_mvdown {
39439 +       uint32_t                flags;                  /* input/output */
39440 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39441 +       int8_t                  au_errno;               /* output */
39442 +} __aligned(8);
39443 +
39444 +/* ---------------------------------------------------------------------- */
39445 +
39446 +union aufs_brinfo {
39447 +       /* PATH_MAX may differ between kernel-space and user-space */
39448 +       char    _spacer[4096];
39449 +       struct {
39450 +               int16_t id;
39451 +               int     perm;
39452 +               char    path[];
39453 +       };
39454 +} __aligned(8);
39455 +
39456 +/* ---------------------------------------------------------------------- */
39457 +
39458 +#define AuCtlType              'A'
39459 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39460 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39461 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39462 +                                    struct aufs_wbr_fd)
39463 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39464 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39465 +                                     struct aufs_mvdown)
39466 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39467 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39468 +
39469 +#endif /* __AUFS_TYPE_H__ */
39470 SPDX-License-Identifier: GPL-2.0
39471 aufs6.x-rcN loopback patch
39472
39473 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39474 index 8ca56da3ca69..9ac6f67edf09 100644
39475 --- a/drivers/block/loop.c
39476 +++ b/drivers/block/loop.c
39477 @@ -54,7 +54,7 @@ struct loop_device {
39478         int             lo_flags;
39479         char            lo_file_name[LO_NAME_SIZE];
39480  
39481 -       struct file *   lo_backing_file;
39482 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39483         struct block_device *lo_device;
39484  
39485         gfp_t           old_gfp_mask;
39486 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39487                                 lo->use_dio);
39488  }
39489  
39490 +static struct file *loop_real_file(struct file *file)
39491 +{
39492 +       struct file *f = NULL;
39493 +
39494 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39495 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39496 +       return f;
39497 +}
39498 +
39499  static void loop_reread_partitions(struct loop_device *lo)
39500  {
39501         int rc;
39502 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39503  {
39504         struct file *file = fget(arg);
39505         struct file *old_file;
39506 +       struct file *f, *virt_file = NULL, *old_virt_file;
39507         int error;
39508         bool partscan;
39509         bool is_loop;
39510 @@ -590,11 +600,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39511         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39512                 goto out_err;
39513  
39514 +       f = loop_real_file(file);
39515 +       if (f) {
39516 +               virt_file = file;
39517 +               file = f;
39518 +               get_file(file);
39519 +       }
39520 +
39521         error = loop_validate_file(file, bdev);
39522         if (error)
39523                 goto out_err;
39524  
39525         old_file = lo->lo_backing_file;
39526 +       old_virt_file = lo->lo_backing_virt_file;
39527  
39528         error = -EINVAL;
39529  
39530 @@ -607,6 +625,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39531         blk_mq_freeze_queue(lo->lo_queue);
39532         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39533         lo->lo_backing_file = file;
39534 +       lo->lo_backing_virt_file = virt_file;
39535         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39536         mapping_set_gfp_mask(file->f_mapping,
39537                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39538 @@ -629,6 +648,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39539          * dependency.
39540          */
39541         fput(old_file);
39542 +       if (old_virt_file)
39543 +               fput(old_virt_file);
39544         if (partscan)
39545                 loop_reread_partitions(lo);
39546  
39547 @@ -642,6 +663,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39548         loop_global_unlock(lo, is_loop);
39549  out_putf:
39550         fput(file);
39551 +       if (virt_file)
39552 +               fput(virt_file);
39553         goto done;
39554  }
39555  
39556 @@ -1013,6 +1036,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39557                           const struct loop_config *config)
39558  {
39559         struct file *file = fget(config->fd);
39560 +       struct file *f, *virt_file = NULL;
39561         struct inode *inode;
39562         struct address_space *mapping;
39563         int error;
39564 @@ -1031,6 +1055,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39565         /* This is safe, since we have a reference from open(). */
39566         __module_get(THIS_MODULE);
39567  
39568 +       f = loop_real_file(file);
39569 +       if (f) {
39570 +               virt_file = file;
39571 +               file = f;
39572 +               get_file(file);
39573 +       }
39574 +
39575         /*
39576          * If we don't hold exclusive handle for the device, upgrade to it
39577          * here to avoid changing device under exclusive owner.
39578 @@ -1091,6 +1122,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39579         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39580         lo->lo_device = bdev;
39581         lo->lo_backing_file = file;
39582 +       lo->lo_backing_virt_file = virt_file;
39583         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39584         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39585  
39586 @@ -1146,6 +1178,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39587                 bd_abort_claiming(bdev, loop_configure);
39588  out_putf:
39589         fput(file);
39590 +       if (virt_file)
39591 +               fput(virt_file);
39592         /* This is safe: open() is still holding a reference. */
39593         module_put(THIS_MODULE);
39594         return error;
39595 @@ -1154,6 +1188,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39596  static void __loop_clr_fd(struct loop_device *lo, bool release)
39597  {
39598         struct file *filp;
39599 +       struct file *virt_filp = lo->lo_backing_virt_file;
39600         gfp_t gfp = lo->old_gfp_mask;
39601  
39602         if (test_bit(QUEUE_FLAG_WC, &lo->lo_queue->queue_flags))
39603 @@ -1170,6 +1205,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39604         spin_lock_irq(&lo->lo_lock);
39605         filp = lo->lo_backing_file;
39606         lo->lo_backing_file = NULL;
39607 +       lo->lo_backing_virt_file = NULL;
39608         spin_unlock_irq(&lo->lo_lock);
39609  
39610         lo->lo_device = NULL;
39611 @@ -1232,6 +1268,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39612          * fput can take open_mutex which is usually taken before lo_mutex.
39613          */
39614         fput(filp);
39615 +       if (virt_filp)
39616 +               fput(virt_filp);
39617  }
39618  
39619  static int loop_clr_fd(struct loop_device *lo)
39620 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39621 index 66ddf86a3331..6cbef597d5b9 100644
39622 --- a/fs/aufs/f_op.c
39623 +++ b/fs/aufs/f_op.c
39624 @@ -309,7 +309,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39625         if (IS_ERR(h_file))
39626                 goto out;
39627  
39628 -       if (au_test_loopback_kthread()) {
39629 +       if (0 && au_test_loopback_kthread()) {
39630                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39631                 if (file->f_mapping != h_file->f_mapping) {
39632                         file->f_mapping = h_file->f_mapping;
39633 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39634 index 58043e31e5f3..e2bfae6f9d59 100644
39635 --- a/fs/aufs/loop.c
39636 +++ b/fs/aufs/loop.c
39637 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39638                 symbol_put(loop_backing_file);
39639         au_kfree_try_rcu(au_warn_loopback_array);
39640  }
39641 +
39642 +/* ---------------------------------------------------------------------- */
39643 +
39644 +/* support the loopback block device insude aufs */
39645 +
39646 +struct file *aufs_real_loop(struct file *file)
39647 +{
39648 +       struct file *f;
39649 +
39650 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39651 +       fi_read_lock(file);
39652 +       f = au_hf_top(file);
39653 +       fi_read_unlock(file);
39654 +       AuDebugOn(!f);
39655 +       return f;
39656 +}
39657 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39658 index 03d4908a6c03..34d356e181d5 100644
39659 --- a/fs/aufs/loop.h
39660 +++ b/fs/aufs/loop.h
39661 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39662  
39663  int au_loopback_init(void);
39664  void au_loopback_fin(void);
39665 +
39666 +struct file *aufs_real_loop(struct file *file);
39667  #else
39668  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39669  
39670 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39671  
39672  AuStubInt0(au_loopback_init, void)
39673  AuStubVoid(au_loopback_fin, void)
39674 +
39675 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39676  #endif /* BLK_DEV_LOOP */
39677  
39678  #endif /* __KERNEL__ */
39679 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39680 index 81922d4faf54..c8a62c267d72 100644
39681 --- a/fs/aufs/super.c
39682 +++ b/fs/aufs/super.c
39683 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39684         .show_options   = aufs_show_options,
39685         .statfs         = aufs_statfs,
39686         .put_super      = aufs_put_super,
39687 -       .sync_fs        = aufs_sync_fs
39688 +       .sync_fs        = aufs_sync_fs,
39689 +#ifdef CONFIG_AUFS_BDEV_LOOP
39690 +       .real_loop      = aufs_real_loop
39691 +#endif
39692  };
39693  
39694  /* ---------------------------------------------------------------------- */
39695 diff --git a/include/linux/fs.h b/include/linux/fs.h
39696 index 433a4f08dab4..0b17b7e5ee11 100644
39697 --- a/include/linux/fs.h
39698 +++ b/include/linux/fs.h
39699 @@ -2257,6 +2257,10 @@ struct super_operations {
39700                                   struct shrink_control *);
39701         long (*free_cached_objects)(struct super_block *,
39702                                     struct shrink_control *);
39703 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39704 +       /* and aufs */
39705 +       struct file *(*real_loop)(struct file *);
39706 +#endif
39707  };
39708  
39709  /*
This page took 2.930136 seconds and 3 git commands to generate.