]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
Up to 5.15.5.
[packages/kernel.git] / kernel-aufs5.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs5.15 kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index a6313a969bc5f..aca4b89d41a14 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -312,6 +312,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 84c5e4cdfee5a..b4fcdad8412ed 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -138,3 +138,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 aufs5.15 base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 3b79fd441dde8..3fb0a57f61b05 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3122,6 +3122,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 7bf4686af774e..a62b132cbd551 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -811,6 +811,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57         return error;
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 cf871a81f4fdc..bc5095b734f58 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1320,7 +1320,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 9c6c6a3e2de51..02382fa9bd341 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 @@ -64,6 +64,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/namespace.c b/fs/namespace.c
117 index 659a8f39c61af..1283670737826 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -808,6 +808,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 5dbce4dcc1a7d..3e6ba363b7775 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -759,8 +759,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 @@ -770,9 +770,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/fs/sync.c b/fs/sync.c
162 index 1373a610dc784..b7b5a0a0df6ff 100644
163 --- a/fs/sync.c
164 +++ b/fs/sync.c
165 @@ -28,7 +28,7 @@
166   * wait == 1 case since in that case write_inode() functions do
167   * sync_dirty_buffer() and thus effectively write one block at a time.
168   */
169 -static int __sync_filesystem(struct super_block *sb, int wait)
170 +int __sync_filesystem(struct super_block *sb, int wait)
171  {
172         if (wait)
173                 sync_inodes_sb(sb);
174 diff --git a/include/linux/fs.h b/include/linux/fs.h
175 index e7a633353fd20..376ddf1965aa2 100644
176 --- a/include/linux/fs.h
177 +++ b/include/linux/fs.h
178 @@ -1381,6 +1381,7 @@ extern void fasync_free(struct fasync_struct *);
179  /* can be called from interrupts */
180  extern void kill_fasync(struct fasync_struct **, int, int);
181  
182 +extern int setfl(int fd, struct file *filp, unsigned long arg);
183  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
184  extern int f_setown(struct file *filp, unsigned long arg, int force);
185  extern void f_delown(struct file *filp);
186 @@ -2092,6 +2093,7 @@ struct file_operations {
187         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
188         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
189         int (*check_flags)(int);
190 +       int (*setfl)(struct file *, unsigned long);
191         int (*flock) (struct file *, int, struct file_lock *);
192         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
193         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
194 @@ -2776,6 +2779,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
195  }
196  
197  void emergency_thaw_all(void);
198 +extern int __sync_filesystem(struct super_block *, int);
199  extern int sync_filesystem(struct super_block *);
200  extern const struct file_operations def_blk_fops;
201  extern const struct file_operations def_chr_fops;
202 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
203 index 9fe165beb0f9e..e47f7e15eeaf7 100644
204 --- a/include/linux/lockdep.h
205 +++ b/include/linux/lockdep.h
206 @@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
207         return lock->key == key;
208  }
209  
210 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
211 +
212  /*
213   * Acquire a lock.
214   *
215 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
216 index 8f882f5881e87..6b9808f098435 100644
217 --- a/include/linux/mnt_namespace.h
218 +++ b/include/linux/mnt_namespace.h
219 @@ -7,12 +7,15 @@ struct mnt_namespace;
220  struct fs_struct;
221  struct user_namespace;
222  struct ns_common;
223 +struct vfsmount;
224  
225  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
226                 struct user_namespace *, struct fs_struct *);
227  extern void put_mnt_ns(struct mnt_namespace *ns);
228  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
229  
230 +extern int is_current_mnt_ns(struct vfsmount *mnt);
231 +
232  extern const struct file_operations proc_mounts_operations;
233  extern const struct file_operations proc_mountinfo_operations;
234  extern const struct file_operations proc_mountstats_operations;
235 diff --git a/include/linux/splice.h b/include/linux/splice.h
236 index a55179fd60fc3..8e21c53cf8831 100644
237 --- a/include/linux/splice.h
238 +++ b/include/linux/splice.h
239 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
240  
241  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
242  extern const struct pipe_buf_operations default_pipe_buf_ops;
243 +
244 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
245 +                          loff_t *ppos, size_t len, unsigned int flags);
246 +extern long do_splice_to(struct file *in, loff_t *ppos,
247 +                        struct pipe_inode_info *pipe, size_t len,
248 +                        unsigned int flags);
249  #endif
250 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
251 index bf1c00c881e48..e0aaf34974df5 100644
252 --- a/kernel/locking/lockdep.c
253 +++ b/kernel/locking/lockdep.c
254 @@ -189,7 +189,7 @@ static
255  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
256  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
257  
258 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
259 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
260  {
261         unsigned int class_idx = hlock->class_idx;
262  
263 @@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
264          */
265         return lock_classes + class_idx;
266  }
267 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
268  
269  #ifdef CONFIG_LOCK_STAT
270  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
271 SPDX-License-Identifier: GPL-2.0
272 aufs5.15 mmap patch
273
274 diff --git a/fs/proc/base.c b/fs/proc/base.c
275 index 533d5836eb9a4..f5de60c5327f5 100644
276 --- a/fs/proc/base.c
277 +++ b/fs/proc/base.c
278 @@ -2186,7 +2186,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
279         rc = -ENOENT;
280         vma = find_exact_vma(mm, vm_start, vm_end);
281         if (vma && vma->vm_file) {
282 -               *path = vma->vm_file->f_path;
283 +               *path = vma_pr_or_file(vma)->f_path;
284                 path_get(path);
285                 rc = 0;
286         }
287 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
288 index 13452b32e2bd5..38acccfef9d49 100644
289 --- a/fs/proc/nommu.c
290 +++ b/fs/proc/nommu.c
291 @@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
292         file = region->vm_file;
293  
294         if (file) {
295 -               struct inode *inode = file_inode(region->vm_file);
296 +               struct inode *inode;
297 +
298 +               file = vmr_pr_or_file(region);
299 +               inode = file_inode(file);
300                 dev = inode->i_sb->s_dev;
301                 ino = inode->i_ino;
302         }
303 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
304 index cf25be3e03212..70af1c25ffa22 100644
305 --- a/fs/proc/task_mmu.c
306 +++ b/fs/proc/task_mmu.c
307 @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
308         const char *name = NULL;
309  
310         if (file) {
311 -               struct inode *inode = file_inode(vma->vm_file);
312 +               struct inode *inode;
313 +
314 +               file = vma_pr_or_file(vma);
315 +               inode = file_inode(file);
316                 dev = inode->i_sb->s_dev;
317                 ino = inode->i_ino;
318                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
319 @@ -1865,7 +1868,7 @@ static int show_numa_map(struct seq_file *m, void *v)
320         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
321         struct vm_area_struct *vma = v;
322         struct numa_maps *md = &numa_priv->md;
323 -       struct file *file = vma->vm_file;
324 +       struct file *file = vma_pr_or_file(vma);
325         struct mm_struct *mm = vma->vm_mm;
326         struct mempolicy *pol;
327         char buffer[64];
328 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
329 index a6d21fc0033c6..02c2de31196e0 100644
330 --- a/fs/proc/task_nommu.c
331 +++ b/fs/proc/task_nommu.c
332 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
333         file = vma->vm_file;
334  
335         if (file) {
336 -               struct inode *inode = file_inode(vma->vm_file);
337 +               struct inode *inode;
338 +
339 +               file = vma_pr_or_file(vma);
340 +               inode = file_inode(file);
341                 dev = inode->i_sb->s_dev;
342                 ino = inode->i_ino;
343                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
344 diff --git a/include/linux/mm.h b/include/linux/mm.h
345 index 73a52aba448f9..5dd42acf0707b 100644
346 --- a/include/linux/mm.h
347 +++ b/include/linux/mm.h
348 @@ -1806,6 +1806,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
349         unmap_mapping_range(mapping, holebegin, holelen, 0);
350  }
351  
352 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
353 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
354 +                                     int);
355 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
356 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
357 +
358 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
359 +                                                               __LINE__)
360 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
361 +                                                         __LINE__)
362 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
363 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
364 +
365 +#ifndef CONFIG_MMU
366 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
367 +extern void vmr_do_fput(struct vm_region *, const char[], int);
368 +
369 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
370 +                                                         __LINE__)
371 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
372 +#endif /* !CONFIG_MMU */
373 +
374  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
375                 void *buf, int len, unsigned int gup_flags);
376  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
377 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
378 index 7f8ee09c711f4..3a9a798a4ae1f 100644
379 --- a/include/linux/mm_types.h
380 +++ b/include/linux/mm_types.h
381 @@ -294,6 +294,7 @@ struct vm_region {
382         unsigned long   vm_top;         /* region allocated to here */
383         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
384         struct file     *vm_file;       /* the backing file or NULL */
385 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
386  
387         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
388         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
389 @@ -373,6 +374,7 @@ struct vm_area_struct {
390         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
391                                            units */
392         struct file * vm_file;          /* File we map to (can be NULL). */
393 +       struct file *vm_prfile;         /* shadow of vm_file */
394         void * vm_private_data;         /* was vm_pte (shared mem) */
395  
396  #ifdef CONFIG_SWAP
397 diff --git a/kernel/fork.c b/kernel/fork.c
398 index 38681ad44c76b..95de8e9843192 100644
399 --- a/kernel/fork.c
400 +++ b/kernel/fork.c
401 @@ -573,7 +573,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
402                 if (file) {
403                         struct address_space *mapping = file->f_mapping;
404  
405 -                       get_file(file);
406 +                       vma_get_file(tmp);
407                         i_mmap_lock_write(mapping);
408                         if (tmp->vm_flags & VM_SHARED)
409                                 mapping_allow_writable(mapping);
410 diff --git a/mm/Makefile b/mm/Makefile
411 index fc60a40ce954b..c715b0138237b 100644
412 --- a/mm/Makefile
413 +++ b/mm/Makefile
414 @@ -52,7 +52,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
415                            mm_init.o percpu.o slab_common.o \
416                            compaction.o vmacache.o \
417                            interval_tree.o list_lru.o workingset.o \
418 -                          debug.o gup.o mmap_lock.o $(mmu-y)
419 +                          prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
420  
421  # Give 'page_alloc' its own module-parameter namespace
422  page-alloc-y := page_alloc.o
423 diff --git a/mm/filemap.c b/mm/filemap.c
424 index dae481293b5d9..52bf3bf87b757 100644
425 --- a/mm/filemap.c
426 +++ b/mm/filemap.c
427 @@ -3350,7 +3350,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
428         vm_fault_t ret = VM_FAULT_LOCKED;
429  
430         sb_start_pagefault(mapping->host->i_sb);
431 -       file_update_time(vmf->vma->vm_file);
432 +       vma_file_update_time(vmf->vma);
433         lock_page(page);
434         if (page->mapping != mapping) {
435                 unlock_page(page);
436 diff --git a/mm/mmap.c b/mm/mmap.c
437 index 88dcc5c252255..6c276614ca962 100644
438 --- a/mm/mmap.c
439 +++ b/mm/mmap.c
440 @@ -183,7 +183,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
441         if (vma->vm_ops && vma->vm_ops->close)
442                 vma->vm_ops->close(vma);
443         if (vma->vm_file)
444 -               fput(vma->vm_file);
445 +               vma_fput(vma);
446         mpol_put(vma_policy(vma));
447         vm_area_free(vma);
448         return next;
449 @@ -952,7 +952,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
450         if (remove_next) {
451                 if (file) {
452                         uprobe_munmap(next, next->vm_start, next->vm_end);
453 -                       fput(file);
454 +                       vma_fput(vma);
455                 }
456                 if (next->anon_vma)
457                         anon_vma_merge(vma, next);
458 @@ -1873,7 +1873,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
459         return addr;
460  
461  unmap_and_free_vma:
462 -       fput(vma->vm_file);
463 +       vma_fput(vma);
464         vma->vm_file = NULL;
465  
466         /* Undo any partial mapping done by a device driver. */
467 @@ -2731,7 +2731,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
468                 goto out_free_mpol;
469  
470         if (new->vm_file)
471 -               get_file(new->vm_file);
472 +               vma_get_file(new);
473  
474         if (new->vm_ops && new->vm_ops->open)
475                 new->vm_ops->open(new);
476 @@ -2750,7 +2750,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
477         if (new->vm_ops && new->vm_ops->close)
478                 new->vm_ops->close(new);
479         if (new->vm_file)
480 -               fput(new->vm_file);
481 +               vma_fput(new);
482         unlink_anon_vmas(new);
483   out_free_mpol:
484         mpol_put(vma_policy(new));
485 @@ -2945,7 +2945,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
486         struct vm_area_struct *vma;
487         unsigned long populate = 0;
488         unsigned long ret = -EINVAL;
489 -       struct file *file;
490 +       struct file *file, *prfile;
491  
492         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
493                      current->comm, current->pid);
494 @@ -3001,10 +3001,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
495         if (vma->vm_flags & VM_LOCKED)
496                 flags |= MAP_LOCKED;
497  
498 -       file = get_file(vma->vm_file);
499 +       vma_get_file(vma);
500 +       file = vma->vm_file;
501 +       prfile = vma->vm_prfile;
502         ret = do_mmap(vma->vm_file, start, size,
503                         prot, flags, pgoff, &populate, NULL);
504 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
505 +               struct vm_area_struct *new_vma;
506 +
507 +               new_vma = find_vma(mm, ret);
508 +               if (!new_vma->vm_prfile)
509 +                       new_vma->vm_prfile = prfile;
510 +               if (new_vma != vma)
511 +                       get_file(prfile);
512 +       }
513 +       /*
514 +        * two fput()s instead of vma_fput(vma),
515 +        * coz vma may not be available anymore.
516 +        */
517         fput(file);
518 +       if (prfile)
519 +               fput(prfile);
520  out:
521         mmap_write_unlock(mm);
522         if (populate)
523 @@ -3285,7 +3302,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
524                 if (anon_vma_clone(new_vma, vma))
525                         goto out_free_mempol;
526                 if (new_vma->vm_file)
527 -                       get_file(new_vma->vm_file);
528 +                       vma_get_file(new_vma);
529                 if (new_vma->vm_ops && new_vma->vm_ops->open)
530                         new_vma->vm_ops->open(new_vma);
531                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
532 diff --git a/mm/nommu.c b/mm/nommu.c
533 index 02d2427b8f9e8..a7419302ab4ed 100644
534 --- a/mm/nommu.c
535 +++ b/mm/nommu.c
536 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
537                 up_write(&nommu_region_sem);
538  
539                 if (region->vm_file)
540 -                       fput(region->vm_file);
541 +                       vmr_fput(region);
542  
543                 /* IO memory and memory shared directly out of the pagecache
544                  * from ramfs/tmpfs mustn't be released here */
545 @@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
546         if (vma->vm_ops && vma->vm_ops->close)
547                 vma->vm_ops->close(vma);
548         if (vma->vm_file)
549 -               fput(vma->vm_file);
550 +               vma_fput(vma);
551         put_nommu_region(vma->vm_region);
552         vm_area_free(vma);
553  }
554 @@ -1175,7 +1175,7 @@ unsigned long do_mmap(struct file *file,
555                                         goto error_just_free;
556                                 }
557                         }
558 -                       fput(region->vm_file);
559 +                       vmr_fput(region);
560                         kmem_cache_free(vm_region_jar, region);
561                         region = pregion;
562                         result = start;
563 @@ -1252,10 +1252,10 @@ unsigned long do_mmap(struct file *file,
564         up_write(&nommu_region_sem);
565  error:
566         if (region->vm_file)
567 -               fput(region->vm_file);
568 +               vmr_fput(region);
569         kmem_cache_free(vm_region_jar, region);
570         if (vma->vm_file)
571 -               fput(vma->vm_file);
572 +               vma_fput(vma);
573         vm_area_free(vma);
574         return ret;
575  
576 diff --git a/mm/prfile.c b/mm/prfile.c
577 new file mode 100644
578 index 0000000000000..511543ab1b418
579 --- /dev/null
580 +++ b/mm/prfile.c
581 @@ -0,0 +1,86 @@
582 +// SPDX-License-Identifier: GPL-2.0
583 +/*
584 + * Mainly for aufs which mmap(2) different file and wants to print different
585 + * path in /proc/PID/maps.
586 + * Call these functions via macros defined in linux/mm.h.
587 + *
588 + * See Documentation/filesystems/aufs/design/06mmap.txt
589 + *
590 + * Copyright (c) 2014-2021 Junjro R. Okajima
591 + * Copyright (c) 2014 Ian Campbell
592 + */
593 +
594 +#include <linux/mm.h>
595 +#include <linux/file.h>
596 +#include <linux/fs.h>
597 +
598 +/* #define PRFILE_TRACE */
599 +static inline void prfile_trace(struct file *f, struct file *pr,
600 +                             const char func[], int line, const char func2[])
601 +{
602 +#ifdef PRFILE_TRACE
603 +       if (pr)
604 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
605 +#endif
606 +}
607 +
608 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
609 +                            int line)
610 +{
611 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
612 +
613 +       prfile_trace(f, pr, func, line, __func__);
614 +       file_update_time(f);
615 +       if (f && pr)
616 +               file_update_time(pr);
617 +}
618 +
619 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
620 +                              int line)
621 +{
622 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
623 +
624 +       prfile_trace(f, pr, func, line, __func__);
625 +       return (f && pr) ? pr : f;
626 +}
627 +
628 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
629 +{
630 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
631 +
632 +       prfile_trace(f, pr, func, line, __func__);
633 +       get_file(f);
634 +       if (f && pr)
635 +               get_file(pr);
636 +}
637 +
638 +void vma_do_fput(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 +       fput(f);
644 +       if (f && pr)
645 +               fput(pr);
646 +}
647 +
648 +#ifndef CONFIG_MMU
649 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
650 +                              int line)
651 +{
652 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
653 +
654 +       prfile_trace(f, pr, func, line, __func__);
655 +       return (f && pr) ? pr : f;
656 +}
657 +
658 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
659 +{
660 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
661 +
662 +       prfile_trace(f, pr, func, line, __func__);
663 +       fput(f);
664 +       if (f && pr)
665 +               fput(pr);
666 +}
667 +#endif /* !CONFIG_MMU */
668 SPDX-License-Identifier: GPL-2.0
669 aufs5.15 standalone patch
670
671 diff --git a/fs/dcache.c b/fs/dcache.c
672 index bc5095b734f58..9508bd57a3bc0 100644
673 --- a/fs/dcache.c
674 +++ b/fs/dcache.c
675 @@ -1425,6 +1425,7 @@ void d_walk(struct dentry *parent, void *data,
676         seq = 1;
677         goto again;
678  }
679 +EXPORT_SYMBOL_GPL(d_walk);
680  
681  struct check_mount {
682         struct vfsmount *mnt;
683 @@ -2970,6 +2971,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
684  
685         write_sequnlock(&rename_lock);
686  }
687 +EXPORT_SYMBOL_GPL(d_exchange);
688  
689  /**
690   * d_ancestor - search for an ancestor
691 diff --git a/fs/exec.c b/fs/exec.c
692 index a098c133d8d74..7b6a249fd4778 100644
693 --- a/fs/exec.c
694 +++ b/fs/exec.c
695 @@ -111,6 +111,7 @@ bool path_noexec(const struct path *path)
696         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
697                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
698  }
699 +EXPORT_SYMBOL_GPL(path_noexec);
700  
701  #ifdef CONFIG_USELIB
702  /*
703 diff --git a/fs/fcntl.c b/fs/fcntl.c
704 index 02382fa9bd341..3418c60b90146 100644
705 --- a/fs/fcntl.c
706 +++ b/fs/fcntl.c
707 @@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
708   out:
709         return error;
710  }
711 +EXPORT_SYMBOL_GPL(setfl);
712  
713  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
714                       int force)
715 diff --git a/fs/file_table.c b/fs/file_table.c
716 index 45437f8e1003e..786af52904fcf 100644
717 --- a/fs/file_table.c
718 +++ b/fs/file_table.c
719 @@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
720         }
721         return ERR_PTR(-ENFILE);
722  }
723 +EXPORT_SYMBOL_GPL(alloc_empty_file);
724  
725  /*
726   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
727 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
728  }
729  
730  EXPORT_SYMBOL(fput);
731 +EXPORT_SYMBOL_GPL(__fput_sync);
732  
733  void __init files_init(void)
734  {
735 diff --git a/fs/namespace.c b/fs/namespace.c
736 index 1283670737826..db99365620118 100644
737 --- a/fs/namespace.c
738 +++ b/fs/namespace.c
739 @@ -439,6 +439,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
740         mnt_dec_writers(real_mount(mnt));
741         preempt_enable();
742  }
743 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
744  
745  /**
746   * mnt_drop_write - give up write access to a mount
747 @@ -813,6 +814,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
748  {
749         return check_mnt(real_mount(mnt));
750  }
751 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
752  
753  /*
754   * vfsmount lock must be held for write
755 @@ -2011,6 +2013,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
756         }
757         return 0;
758  }
759 +EXPORT_SYMBOL_GPL(iterate_mounts);
760  
761  static void lock_mnt_tree(struct mount *mnt)
762  {
763 diff --git a/fs/notify/group.c b/fs/notify/group.c
764 index fb89c351295d6..460ad19c2570a 100644
765 --- a/fs/notify/group.c
766 +++ b/fs/notify/group.c
767 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
768  {
769         refcount_inc(&group->refcnt);
770  }
771 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
772  
773  /*
774   * Drop a reference to a group.  Free it if it's through.
775 diff --git a/fs/open.c b/fs/open.c
776 index daa324606a41f..0d8bb2f1922a3 100644
777 --- a/fs/open.c
778 +++ b/fs/open.c
779 @@ -65,6 +65,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
780         inode_unlock(dentry->d_inode);
781         return ret;
782  }
783 +EXPORT_SYMBOL_GPL(do_truncate);
784  
785  long vfs_truncate(const struct path *path, loff_t length)
786  {
787 diff --git a/fs/read_write.c b/fs/read_write.c
788 index af057c57bdc64..76017f8331fbf 100644
789 --- a/fs/read_write.c
790 +++ b/fs/read_write.c
791 @@ -492,6 +492,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
792         inc_syscr(current);
793         return ret;
794  }
795 +EXPORT_SYMBOL_GPL(vfs_read);
796  
797  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
798  {
799 @@ -602,6 +603,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
800         file_end_write(file);
801         return ret;
802  }
803 +EXPORT_SYMBOL_GPL(vfs_write);
804  
805  /* file_ppos returns &file->f_pos or NULL if file is stream */
806  static inline loff_t *file_ppos(struct file *file)
807 diff --git a/fs/splice.c b/fs/splice.c
808 index 3e6ba363b7775..7c1be373eb7cd 100644
809 --- a/fs/splice.c
810 +++ b/fs/splice.c
811 @@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
812                 return warn_unsupported(out, "write");
813         return out->f_op->splice_write(pipe, out, ppos, len, flags);
814  }
815 +EXPORT_SYMBOL_GPL(do_splice_from);
816  
817  /*
818   * Attempt to initiate a splice from a file to a pipe.
819 @@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
820                 return warn_unsupported(in, "read");
821         return in->f_op->splice_read(in, ppos, pipe, len, flags);
822  }
823 +EXPORT_SYMBOL_GPL(do_splice_to);
824  
825  /**
826   * splice_direct_to_actor - splices data directly between two non-pipes
827 diff --git a/fs/sync.c b/fs/sync.c
828 index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
829 --- a/fs/sync.c
830 +++ b/fs/sync.c
831 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
832                 sb->s_op->sync_fs(sb, wait);
833         return __sync_blockdev(sb->s_bdev, wait);
834  }
835 +EXPORT_SYMBOL_GPL(__sync_filesystem);
836  
837  /*
838   * Write out and wait upon all dirty data associated with this
839 diff --git a/fs/xattr.c b/fs/xattr.c
840 index 5c8c5175b385c..ff7e9ff774b73 100644
841 --- a/fs/xattr.c
842 +++ b/fs/xattr.c
843 @@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
844         *xattr_value = value;
845         return error;
846  }
847 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
848  
849  ssize_t
850  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
851 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
852 index e0aaf34974df5..d7b737731d4ef 100644
853 --- a/kernel/locking/lockdep.c
854 +++ b/kernel/locking/lockdep.c
855 @@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
856          */
857         return lock_classes + class_idx;
858  }
859 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
860  #define hlock_class(hlock) lockdep_hlock_class(hlock)
861  
862  #ifdef CONFIG_LOCK_STAT
863 diff --git a/kernel/task_work.c b/kernel/task_work.c
864 index 1698fbe6f0e13..081b05acadf82 100644
865 --- a/kernel/task_work.c
866 +++ b/kernel/task_work.c
867 @@ -167,3 +167,4 @@ void task_work_run(void)
868                 } while (work);
869         }
870  }
871 +EXPORT_SYMBOL_GPL(task_work_run);
872 diff --git a/security/security.c b/security/security.c
873 index 9ffa9e9c5c554..83b533a3fb8e8 100644
874 --- a/security/security.c
875 +++ b/security/security.c
876 @@ -1147,6 +1147,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
877                 return 0;
878         return call_int_hook(path_rmdir, 0, dir, dentry);
879  }
880 +EXPORT_SYMBOL_GPL(security_path_rmdir);
881  
882  int security_path_unlink(const struct path *dir, struct dentry *dentry)
883  {
884 @@ -1163,6 +1164,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
885                 return 0;
886         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
887  }
888 +EXPORT_SYMBOL_GPL(security_path_symlink);
889  
890  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
891                        struct dentry *new_dentry)
892 @@ -1171,6 +1173,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
893                 return 0;
894         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
895  }
896 +EXPORT_SYMBOL_GPL(security_path_link);
897  
898  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
899                          const struct path *new_dir, struct dentry *new_dentry,
900 @@ -1198,6 +1201,7 @@ int security_path_truncate(const struct path *path)
901                 return 0;
902         return call_int_hook(path_truncate, 0, path);
903  }
904 +EXPORT_SYMBOL_GPL(security_path_truncate);
905  
906  int security_path_chmod(const struct path *path, umode_t mode)
907  {
908 @@ -1205,6 +1209,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
909                 return 0;
910         return call_int_hook(path_chmod, 0, path, mode);
911  }
912 +EXPORT_SYMBOL_GPL(security_path_chmod);
913  
914  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
915  {
916 @@ -1212,6 +1217,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
917                 return 0;
918         return call_int_hook(path_chown, 0, path, uid, gid);
919  }
920 +EXPORT_SYMBOL_GPL(security_path_chown);
921  
922  int security_path_chroot(const struct path *path)
923  {
924 @@ -1312,6 +1318,7 @@ int security_inode_permission(struct inode *inode, int mask)
925                 return 0;
926         return call_int_hook(inode_permission, 0, inode, mask);
927  }
928 +EXPORT_SYMBOL_GPL(security_inode_permission);
929  
930  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
931  {
932 @@ -1509,6 +1516,7 @@ int security_file_permission(struct file *file, int mask)
933  
934         return fsnotify_perm(file, mask);
935  }
936 +EXPORT_SYMBOL_GPL(security_file_permission);
937  
938  int security_file_alloc(struct file *file)
939  {
940 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
941 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
942 +++ linux/Documentation/ABI/testing/debugfs-aufs        2021-11-01 23:48:34.199692595 +0100
943 @@ -0,0 +1,55 @@
944 +What:          /debug/aufs/si_<id>/
945 +Date:          March 2009
946 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
947 +Description:
948 +               Under /debug/aufs, a directory named si_<id> is created
949 +               per aufs mount, where <id> is a unique id generated
950 +               internally.
951 +
952 +What:          /debug/aufs/si_<id>/plink
953 +Date:          Apr 2013
954 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
955 +Description:
956 +               It has three lines and shows the information about the
957 +               pseudo-link. The first line is a single number
958 +               representing a number of buckets. The second line is a
959 +               number of pseudo-links per buckets (separated by a
960 +               blank). The last line is a single number representing a
961 +               total number of psedo-links.
962 +               When the aufs mount option 'noplink' is specified, it
963 +               will show "1\n0\n0\n".
964 +
965 +What:          /debug/aufs/si_<id>/xib
966 +Date:          March 2009
967 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
968 +Description:
969 +               It shows the consumed blocks by xib (External Inode Number
970 +               Bitmap), its block size and file size.
971 +               When the aufs mount option 'noxino' is specified, it
972 +               will be empty. About XINO files, see the aufs manual.
973 +
974 +What:          /debug/aufs/si_<id>/xi<branch-index>
975 +Date:          March 2009
976 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
977 +Description:
978 +               It shows the consumed blocks by xino (External Inode Number
979 +               Translation Table), its link count, block size and file
980 +               size.
981 +               Due to the file size limit, there may exist multiple
982 +               xino files per branch.  In this case, "-N" is added to
983 +               the filename and it corresponds to the index of the
984 +               internal xino array.  "-0" is omitted.
985 +               When the aufs mount option 'noxino' is specified, Those
986 +               entries won't exist.  About XINO files, see the aufs
987 +               manual.
988 +
989 +What:          /debug/aufs/si_<id>/xigen
990 +Date:          March 2009
991 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
992 +Description:
993 +               It shows the consumed blocks by xigen (External Inode
994 +               Generation Table), its block size and file size.
995 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
996 +               be created.
997 +               When the aufs mount option 'noxino' is specified, it
998 +               will be empty. About XINO files, see the aufs manual.
999 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1000 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1001 +++ linux/Documentation/ABI/testing/sysfs-aufs  2021-11-01 23:48:34.199692595 +0100
1002 @@ -0,0 +1,31 @@
1003 +What:          /sys/fs/aufs/si_<id>/
1004 +Date:          March 2009
1005 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1006 +Description:
1007 +               Under /sys/fs/aufs, a directory named si_<id> is created
1008 +               per aufs mount, where <id> is a unique id generated
1009 +               internally.
1010 +
1011 +What:          /sys/fs/aufs/si_<id>/br<idx>
1012 +Date:          March 2009
1013 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1014 +Description:
1015 +               It shows the abolute path of a member directory (which
1016 +               is called branch) in aufs, and its permission.
1017 +
1018 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1019 +Date:          July 2013
1020 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1021 +Description:
1022 +               It shows the id of a member directory (which is called
1023 +               branch) in aufs.
1024 +
1025 +What:          /sys/fs/aufs/si_<id>/xi_path
1026 +Date:          March 2009
1027 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1028 +Description:
1029 +               It shows the abolute path of XINO (External Inode Number
1030 +               Bitmap, Translation Table and Generation Table) file
1031 +               even if it is the default path.
1032 +               When the aufs mount option 'noxino' is specified, it
1033 +               will be empty. About XINO files, see the aufs manual.
1034 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1035 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1036 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2021-11-01 23:48:34.199692595 +0100
1037 @@ -0,0 +1,171 @@
1038 +
1039 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1040 +# 
1041 +# This program is free software; you can redistribute it and/or modify
1042 +# it under the terms of the GNU General Public License as published by
1043 +# the Free Software Foundation; either version 2 of the License, or
1044 +# (at your option) any later version.
1045 +# 
1046 +# This program is distributed in the hope that it will be useful,
1047 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1048 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1049 +# GNU General Public License for more details.
1050 +# 
1051 +# You should have received a copy of the GNU General Public License
1052 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1053 +
1054 +Introduction
1055 +----------------------------------------
1056 +
1057 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1058 +1. abbrev. for "advanced multi-layered unification filesystem".
1059 +2. abbrev. for "another unionfs".
1060 +3. abbrev. for "auf das" in German which means "on the" in English.
1061 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1062 +   But "Filesystem aufs Filesystem" is hard to understand.
1063 +4. abbrev. for "African Urban Fashion Show".
1064 +
1065 +AUFS is a filesystem with features:
1066 +- multi layered stackable unification filesystem, the member directory
1067 +  is called as a branch.
1068 +- branch permission and attribute, 'readonly', 'real-readonly',
1069 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1070 +  combination.
1071 +- internal "file copy-on-write".
1072 +- logical deletion, whiteout.
1073 +- dynamic branch manipulation, adding, deleting and changing permission.
1074 +- allow bypassing aufs, user's direct branch access.
1075 +- external inode number translation table and bitmap which maintains the
1076 +  persistent aufs inode number.
1077 +- seekable directory, including NFS readdir.
1078 +- file mapping, mmap and sharing pages.
1079 +- pseudo-link, hardlink over branches.
1080 +- loopback mounted filesystem as a branch.
1081 +- several policies to select one among multiple writable branches.
1082 +- revert a single systemcall when an error occurs in aufs.
1083 +- and more...
1084 +
1085 +
1086 +Multi Layered Stackable Unification Filesystem
1087 +----------------------------------------------------------------------
1088 +Most people already knows what it is.
1089 +It is a filesystem which unifies several directories and provides a
1090 +merged single directory. When users access a file, the access will be
1091 +passed/re-directed/converted (sorry, I am not sure which English word is
1092 +correct) to the real file on the member filesystem. The member
1093 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1094 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1095 +readonly branch is handled by creating 'whiteout' on the upper writable
1096 +branch.
1097 +
1098 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1099 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1100 +different approaches to implement the merged-view.
1101 +The former tries putting it into VFS, and the latter implements as a
1102 +separate filesystem.
1103 +(If I misunderstand about these implementations, please let me know and
1104 +I shall correct it. Because it is a long time ago when I read their
1105 +source files last time).
1106 +
1107 +UnionMount's approach will be able to small, but may be hard to share
1108 +branches between several UnionMount since the whiteout in it is
1109 +implemented in the inode on branch filesystem and always
1110 +shared. According to Bharata's post, readdir does not seems to be
1111 +finished yet.
1112 +There are several missing features known in this implementations such as
1113 +- for users, the inode number may change silently. eg. copy-up.
1114 +- link(2) may break by copy-up.
1115 +- read(2) may get an obsoleted filedata (fstat(2) too).
1116 +- fcntl(F_SETLK) may be broken by copy-up.
1117 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1118 +  open(O_RDWR).
1119 +
1120 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1121 +merged into mainline. This is another implementation of UnionMount as a
1122 +separated filesystem. All the limitations and known problems which
1123 +UnionMount are equally inherited to "overlay" filesystem.
1124 +
1125 +Unionfs has a longer history. When I started implementing a stackable
1126 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1127 +inode, dentry and file objects and they have an array pointing lower
1128 +same kind objects. After contributing many patches for Unionfs, I
1129 +re-started my project AUFS (Jun 2006).
1130 +
1131 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1132 +implemented my own ideas, approaches and enhancements and it became
1133 +totally different one.
1134 +
1135 +Comparing DM snapshot and fs based implementation
1136 +- the number of bytes to be copied between devices is much smaller.
1137 +- the type of filesystem must be one and only.
1138 +- the fs must be writable, no readonly fs, even for the lower original
1139 +  device. so the compression fs will not be usable. but if we use
1140 +  loopback mount, we may address this issue.
1141 +  for instance,
1142 +       mount /cdrom/squashfs.img /sq
1143 +       losetup /sq/ext2.img
1144 +       losetup /somewhere/cow
1145 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1146 +- it will be difficult (or needs more operations) to extract the
1147 +  difference between the original device and COW.
1148 +- DM snapshot-merge may help a lot when users try merging. in the
1149 +  fs-layer union, users will use rsync(1).
1150 +
1151 +You may want to read my old paper "Filesystems in LiveCD"
1152 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1153 +
1154 +
1155 +Several characters/aspects/persona of aufs
1156 +----------------------------------------------------------------------
1157 +
1158 +Aufs has several characters, aspects or persona.
1159 +1. a filesystem, callee of VFS helper
1160 +2. sub-VFS, caller of VFS helper for branches
1161 +3. a virtual filesystem which maintains persistent inode number
1162 +4. reader/writer of files on branches such like an application
1163 +
1164 +1. Callee of VFS Helper
1165 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1166 +unlink(2) from an application reaches sys_unlink() kernel function and
1167 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1168 +calls filesystem specific unlink operation. Actually aufs implements the
1169 +unlink operation but it behaves like a redirector.
1170 +
1171 +2. Caller of VFS Helper for Branches
1172 +aufs_unlink() passes the unlink request to the branch filesystem as if
1173 +it were called from VFS. So the called unlink operation of the branch
1174 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1175 +every necessary pre/post operation for the branch filesystem.
1176 +- acquire the lock for the parent dir on a branch
1177 +- lookup in a branch
1178 +- revalidate dentry on a branch
1179 +- mnt_want_write() for a branch
1180 +- vfs_unlink() for a branch
1181 +- mnt_drop_write() for a branch
1182 +- release the lock on a branch
1183 +
1184 +3. Persistent Inode Number
1185 +One of the most important issue for a filesystem is to maintain inode
1186 +numbers. This is particularly important to support exporting a
1187 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1188 +backend block device for its own. But some storage is necessary to
1189 +keep and maintain the inode numbers. It may be a large space and may not
1190 +suit to keep in memory. Aufs rents some space from its first writable
1191 +branch filesystem (by default) and creates file(s) on it. These files
1192 +are created by aufs internally and removed soon (currently) keeping
1193 +opened.
1194 +Note: Because these files are removed, they are totally gone after
1195 +      unmounting aufs. It means the inode numbers are not persistent
1196 +      across unmount or reboot. I have a plan to make them really
1197 +      persistent which will be important for aufs on NFS server.
1198 +
1199 +4. Read/Write Files Internally (copy-on-write)
1200 +Because a branch can be readonly, when you write a file on it, aufs will
1201 +"copy-up" it to the upper writable branch internally. And then write the
1202 +originally requested thing to the file. Generally kernel doesn't
1203 +open/read/write file actively. In aufs, even a single write may cause a
1204 +internal "file copy". This behaviour is very similar to cp(1) command.
1205 +
1206 +Some people may think it is better to pass such work to user space
1207 +helper, instead of doing in kernel space. Actually I am still thinking
1208 +about it. But currently I have implemented it in kernel space.
1209 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1210 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1211 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2021-11-01 23:48:34.199692595 +0100
1212 @@ -0,0 +1,258 @@
1213 +
1214 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1215 +# 
1216 +# This program is free software; you can redistribute it and/or modify
1217 +# it under the terms of the GNU General Public License as published by
1218 +# the Free Software Foundation; either version 2 of the License, or
1219 +# (at your option) any later version.
1220 +# 
1221 +# This program is distributed in the hope that it will be useful,
1222 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1223 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1224 +# GNU General Public License for more details.
1225 +# 
1226 +# You should have received a copy of the GNU General Public License
1227 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1228 +
1229 +Basic Aufs Internal Structure
1230 +
1231 +Superblock/Inode/Dentry/File Objects
1232 +----------------------------------------------------------------------
1233 +As like an ordinary filesystem, aufs has its own
1234 +superblock/inode/dentry/file objects. All these objects have a
1235 +dynamically allocated array and store the same kind of pointers to the
1236 +lower filesystem, branch.
1237 +For example, when you build a union with one readwrite branch and one
1238 +readonly, mounted /au, /rw and /ro respectively.
1239 +- /au = /rw + /ro
1240 +- /ro/fileA exists but /rw/fileA
1241 +
1242 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1243 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1244 +- [0] = NULL (because /rw/fileA doesn't exist)
1245 +- [1] = /ro/fileA
1246 +
1247 +This style of an array is essentially same to the aufs
1248 +superblock/inode/dentry/file objects.
1249 +
1250 +Because aufs supports manipulating branches, ie. add/delete/change
1251 +branches dynamically, these objects has its own generation. When
1252 +branches are changed, the generation in aufs superblock is
1253 +incremented. And a generation in other object are compared when it is
1254 +accessed. When a generation in other objects are obsoleted, aufs
1255 +refreshes the internal array.
1256 +
1257 +
1258 +Superblock
1259 +----------------------------------------------------------------------
1260 +Additionally aufs superblock has some data for policies to select one
1261 +among multiple writable branches, XIB files, pseudo-links and kobject.
1262 +See below in detail.
1263 +About the policies which supports copy-down a directory, see
1264 +wbr_policy.txt too.
1265 +
1266 +
1267 +Branch and XINO(External Inode Number Translation Table)
1268 +----------------------------------------------------------------------
1269 +Every branch has its own xino (external inode number translation table)
1270 +file. The xino file is created and unlinked by aufs internally. When two
1271 +members of a union exist on the same filesystem, they share the single
1272 +xino file.
1273 +The struct of a xino file is simple, just a sequence of aufs inode
1274 +numbers which is indexed by the lower inode number.
1275 +In the above sample, assume the inode number of /ro/fileA is i111 and
1276 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1277 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1278 +
1279 +When the inode numbers are not contiguous, the xino file will be sparse
1280 +which has a hole in it and doesn't consume as much disk space as it
1281 +might appear. If your branch filesystem consumes disk space for such
1282 +holes, then you should specify 'xino=' option at mounting aufs.
1283 +
1284 +Aufs has a mount option to free the disk blocks for such holes in XINO
1285 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1286 +meet a problem of disk shortage due to XINO files, then you should try
1287 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1288 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1289 +the holes in XINO files.
1290 +
1291 +Also a writable branch has three kinds of "whiteout bases". All these
1292 +are existed when the branch is joined to aufs, and their names are
1293 +whiteout-ed doubly, so that users will never see their names in aufs
1294 +hierarchy.
1295 +1. a regular file which will be hardlinked to all whiteouts.
1296 +2. a directory to store a pseudo-link.
1297 +3. a directory to store an "orphan"-ed file temporary.
1298 +
1299 +1. Whiteout Base
1300 +   When you remove a file on a readonly branch, aufs handles it as a
1301 +   logical deletion and creates a whiteout on the upper writable branch
1302 +   as a hardlink of this file in order not to consume inode on the
1303 +   writable branch.
1304 +2. Pseudo-link Dir
1305 +   See below, Pseudo-link.
1306 +3. Step-Parent Dir
1307 +   When "fileC" exists on the lower readonly branch only and it is
1308 +   opened and removed with its parent dir, and then user writes
1309 +   something into it, then aufs copies-up fileC to this
1310 +   directory. Because there is no other dir to store fileC. After
1311 +   creating a file under this dir, the file is unlinked.
1312 +
1313 +Because aufs supports manipulating branches, ie. add/delete/change
1314 +dynamically, a branch has its own id. When the branch order changes,
1315 +aufs finds the new index by searching the branch id.
1316 +
1317 +
1318 +Pseudo-link
1319 +----------------------------------------------------------------------
1320 +Assume "fileA" exists on the lower readonly branch only and it is
1321 +hardlinked to "fileB" on the branch. When you write something to fileA,
1322 +aufs copies-up it to the upper writable branch. Additionally aufs
1323 +creates a hardlink under the Pseudo-link Directory of the writable
1324 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1325 +simple list. If fileB is read after unlinking fileA, aufs returns
1326 +filedata from the pseudo-link instead of the lower readonly
1327 +branch. Because the pseudo-link is based upon the inode, to keep the
1328 +inode number by xino (see above) is essentially necessary.
1329 +
1330 +All the hardlinks under the Pseudo-link Directory of the writable branch
1331 +should be restored in a proper location later. Aufs provides a utility
1332 +to do this. The userspace helpers executed at remounting and unmounting
1333 +aufs by default.
1334 +During this utility is running, it puts aufs into the pseudo-link
1335 +maintenance mode. In this mode, only the process which began the
1336 +maintenance mode (and its child processes) is allowed to operate in
1337 +aufs. Some other processes which are not related to the pseudo-link will
1338 +be allowed to run too, but the rest have to return an error or wait
1339 +until the maintenance mode ends. If a process already acquires an inode
1340 +mutex (in VFS), it has to return an error.
1341 +
1342 +
1343 +XIB(external inode number bitmap)
1344 +----------------------------------------------------------------------
1345 +Addition to the xino file per a branch, aufs has an external inode number
1346 +bitmap in a superblock object. It is also an internal file such like a
1347 +xino file.
1348 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1349 +not.
1350 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1351 +
1352 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1353 +reduce the number of consumed disk blocks for these files.
1354 +
1355 +
1356 +Virtual or Vertical Dir, and Readdir in Userspace
1357 +----------------------------------------------------------------------
1358 +In order to support multiple layers (branches), aufs readdir operation
1359 +constructs a virtual dir block on memory. For readdir, aufs calls
1360 +vfs_readdir() internally for each dir on branches, merges their entries
1361 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1362 +object. So the file object has its entry list until it is closed. The
1363 +entry list will be updated when the file position is zero and becomes
1364 +obsoleted. This decision is made in aufs automatically.
1365 +
1366 +The dynamically allocated memory block for the name of entries has a
1367 +unit of 512 bytes (by default) and stores the names contiguously (no
1368 +padding). Another block for each entry is handled by kmem_cache too.
1369 +During building dir blocks, aufs creates hash list and judging whether
1370 +the entry is whiteouted by its upper branch or already listed.
1371 +The merged result is cached in the corresponding inode object and
1372 +maintained by a customizable life-time option.
1373 +
1374 +Some people may call it can be a security hole or invite DoS attack
1375 +since the opened and once readdir-ed dir (file object) holds its entry
1376 +list and becomes a pressure for system memory. But I'd say it is similar
1377 +to files under /proc or /sys. The virtual files in them also holds a
1378 +memory page (generally) while they are opened. When an idea to reduce
1379 +memory for them is introduced, it will be applied to aufs too.
1380 +For those who really hate this situation, I've developed readdir(3)
1381 +library which operates this merging in userspace. You just need to set
1382 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1383 +kernel space for readdir(3).
1384 +
1385 +
1386 +Workqueue
1387 +----------------------------------------------------------------------
1388 +Aufs sometimes requires privilege access to a branch. For instance,
1389 +in copy-up/down operation. When a user process is going to make changes
1390 +to a file which exists in the lower readonly branch only, and the mode
1391 +of one of ancestor directories may not be writable by a user
1392 +process. Here aufs copy-up the file with its ancestors and they may
1393 +require privilege to set its owner/group/mode/etc.
1394 +This is a typical case of a application character of aufs (see
1395 +Introduction).
1396 +
1397 +Aufs uses workqueue synchronously for this case. It creates its own
1398 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1399 +passes the request to call mkdir or write (for example), and wait for
1400 +its completion. This approach solves a problem of a signal handler
1401 +simply.
1402 +If aufs didn't adopt the workqueue and changed the privilege of the
1403 +process, then the process may receive the unexpected SIGXFSZ or other
1404 +signals.
1405 +
1406 +Also aufs uses the system global workqueue ("events" kernel thread) too
1407 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1408 +whiteout base and etc. This is unrelated to a privilege.
1409 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1410 +superblock at the beginning, at the same time waits for the completion
1411 +of all queued asynchronous tasks.
1412 +
1413 +
1414 +Whiteout
1415 +----------------------------------------------------------------------
1416 +The whiteout in aufs is very similar to Unionfs's. That is represented
1417 +by its filename. UnionMount takes an approach of a file mode, but I am
1418 +afraid several utilities (find(1) or something) will have to support it.
1419 +
1420 +Basically the whiteout represents "logical deletion" which stops aufs to
1421 +lookup further, but also it represents "dir is opaque" which also stop
1422 +further lookup.
1423 +
1424 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1425 +In order to make several functions in a single systemcall to be
1426 +revertible, aufs adopts an approach to rename a directory to a temporary
1427 +unique whiteouted name.
1428 +For example, in rename(2) dir where the target dir already existed, aufs
1429 +renames the target dir to a temporary unique whiteouted name before the
1430 +actual rename on a branch, and then handles other actions (make it opaque,
1431 +update the attributes, etc). If an error happens in these actions, aufs
1432 +simply renames the whiteouted name back and returns an error. If all are
1433 +succeeded, aufs registers a function to remove the whiteouted unique
1434 +temporary name completely and asynchronously to the system global
1435 +workqueue.
1436 +
1437 +
1438 +Copy-up
1439 +----------------------------------------------------------------------
1440 +It is a well-known feature or concept.
1441 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1442 +internally and makes change to the new file on the upper writable branch.
1443 +When the trigger systemcall does not update the timestamps of the parent
1444 +dir, aufs reverts it after copy-up.
1445 +
1446 +
1447 +Move-down (aufs3.9 and later)
1448 +----------------------------------------------------------------------
1449 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1450 +the lower readonly branch to the upper writable branch when a user
1451 +changes something about the file.
1452 +"Move-down" is an opposite action of copy-up. Basically this action is
1453 +ran manually instead of automatically and internally.
1454 +For desgin and implementation, aufs has to consider these issues.
1455 +- whiteout for the file may exist on the lower branch.
1456 +- ancestor directories may not exist on the lower branch.
1457 +- diropq for the ancestor directories may exist on the upper branch.
1458 +- free space on the lower branch will reduce.
1459 +- another access to the file may happen during moving-down, including
1460 +  UDBA (see "Revalidate Dentry and UDBA").
1461 +- the file should not be hard-linked nor pseudo-linked. they should be
1462 +  handled by auplink utility later.
1463 +
1464 +Sometimes users want to move-down a file from the upper writable branch
1465 +to the lower readonly or writable branch. For instance,
1466 +- the free space of the upper writable branch is going to run out.
1467 +- create a new intermediate branch between the upper and lower branch.
1468 +- etc.
1469 +
1470 +For this purpose, use "aumvdown" command in aufs-util.git.
1471 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1472 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1473 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2021-11-01 23:48:34.203025928 +0100
1474 @@ -0,0 +1,85 @@
1475 +
1476 +# Copyright (C) 2015-2021 Junjiro R. Okajima
1477 +# 
1478 +# This program is free software; you can redistribute it and/or modify
1479 +# it under the terms of the GNU General Public License as published by
1480 +# the Free Software Foundation; either version 2 of the License, or
1481 +# (at your option) any later version.
1482 +# 
1483 +# This program is distributed in the hope that it will be useful,
1484 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1485 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1486 +# GNU General Public License for more details.
1487 +# 
1488 +# You should have received a copy of the GNU General Public License
1489 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1490 +
1491 +Support for a branch who has its ->atomic_open()
1492 +----------------------------------------------------------------------
1493 +The filesystems who implement its ->atomic_open() are not majority. For
1494 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1495 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1496 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1497 +sure whether all filesystems who have ->atomic_open() behave like this,
1498 +but NFSv4 surely returns the error.
1499 +
1500 +In order to support ->atomic_open() for aufs, there are a few
1501 +approaches.
1502 +
1503 +A. Introduce aufs_atomic_open()
1504 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1505 +     branch fs.
1506 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1507 +   an aufs user Pip Cet's approach
1508 +   - calls aufs_create(), VFS finish_open() and notify_change().
1509 +   - pass fake-mode to finish_open(), and then correct the mode by
1510 +     notify_change().
1511 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1512 +   - no aufs_atomic_open().
1513 +   - aufs_lookup() registers the TID to an aufs internal object.
1514 +   - aufs_create() does nothing when the matching TID is registered, but
1515 +     registers the mode.
1516 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1517 +     TID is registered.
1518 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1519 +   credential
1520 +   - no aufs_atomic_open().
1521 +   - aufs_create() registers the TID to an internal object. this info
1522 +     represents "this process created this file just now."
1523 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1524 +     registered TID and re-try open() with superuser's credential.
1525 +
1526 +Pros and cons for each approach.
1527 +
1528 +A.
1529 +   - straightforward but highly depends upon VFS internal.
1530 +   - the atomic behavaiour is kept.
1531 +   - some of parameters such as nameidata are hard to reproduce for
1532 +     branch fs.
1533 +   - large overhead.
1534 +B.
1535 +   - easy to implement.
1536 +   - the atomic behavaiour is lost.
1537 +C.
1538 +   - the atomic behavaiour is kept.
1539 +   - dirty and tricky.
1540 +   - VFS checks whether the file is created correctly after calling
1541 +     ->create(), which means this approach doesn't work.
1542 +D.
1543 +   - easy to implement.
1544 +   - the atomic behavaiour is lost.
1545 +   - to open a file with superuser's credential and give it to a user
1546 +     process is a bad idea, since the file object keeps the credential
1547 +     in it. It may affect LSM or something. This approach doesn't work
1548 +     either.
1549 +
1550 +The approach A is ideal, but it hard to implement. So here is a
1551 +variation of A, which is to be implemented.
1552 +
1553 +A-1. Introduce aufs_atomic_open()
1554 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1555 +       vfs_create() and finish_open().
1556 +     - the demerit is that the several checks after branch fs
1557 +       ->atomic_open() are lost. in the ordinary case, the checks are
1558 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1559 +       be implemented in aufs, but not all I am afraid.
1560 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1561 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1562 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2021-11-01 23:48:34.203025928 +0100
1563 @@ -0,0 +1,113 @@
1564 +
1565 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1566 +# 
1567 +# This program is free software; you can redistribute it and/or modify
1568 +# it under the terms of the GNU General Public License as published by
1569 +# the Free Software Foundation; either version 2 of the License, or
1570 +# (at your option) any later version.
1571 +# 
1572 +# This program is distributed in the hope that it will be useful,
1573 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1574 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1575 +# GNU General Public License for more details.
1576 +# 
1577 +# You should have received a copy of the GNU General Public License
1578 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1579 +
1580 +Lookup in a Branch
1581 +----------------------------------------------------------------------
1582 +Since aufs has a character of sub-VFS (see Introduction), it operates
1583 +lookup for branches as VFS does. It may be a heavy work. But almost all
1584 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1585 +directly connected to its parent. Digging down the directory hierarchy
1586 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1587 +aufs calls it.
1588 +
1589 +When a branch is a remote filesystem, aufs basically relies upon its
1590 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1591 +them.
1592 +For d_revalidate, aufs implements three levels of revalidate tests. See
1593 +"Revalidate Dentry and UDBA" in detail.
1594 +
1595 +
1596 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1597 +----------------------------------------------------------------------
1598 +Let's try case study.
1599 +- aufs has two branches, upper readwrite and lower readonly.
1600 +  /au = /rw + /ro
1601 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1602 +- user invoked "chmod a+rx /au/dirA"
1603 +- the internal copy-up is activated and "/rw/dirA" is created and its
1604 +  permission bits are set to world readable.
1605 +- then "/au/dirA" becomes world readable?
1606 +
1607 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1608 +or it may be a natively readonly filesystem. If aufs respects the lower
1609 +branch, it should not respond readdir request from other users. But user
1610 +allowed it by chmod. Should really aufs rejects showing the entries
1611 +under /ro/dirA?
1612 +
1613 +To be honest, I don't have a good solution for this case. So aufs
1614 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1615 +users.
1616 +When dirperm1 is specified, aufs checks only the highest one for the
1617 +directory permission, and shows the entries. Otherwise, as usual, checks
1618 +every dir existing on all branches and rejects the request.
1619 +
1620 +As a side effect, dirperm1 option improves the performance of aufs
1621 +because the number of permission check is reduced when the number of
1622 +branch is many.
1623 +
1624 +
1625 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1626 +----------------------------------------------------------------------
1627 +Generally VFS helpers re-validate a dentry as a part of lookup.
1628 +0. digging down the directory hierarchy.
1629 +1. lock the parent dir by its i_mutex.
1630 +2. lookup the final (child) entry.
1631 +3. revalidate it.
1632 +4. call the actual operation (create, unlink, etc.)
1633 +5. unlock the parent dir
1634 +
1635 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1636 +called. Actually aufs implements it and checks the dentry on a branch is
1637 +still valid.
1638 +But it is not enough. Because aufs has to release the lock for the
1639 +parent dir on a branch at the end of ->lookup() (step 2) and
1640 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1641 +held by VFS.
1642 +If the file on a branch is changed directly, eg. bypassing aufs, after
1643 +aufs released the lock, then the subsequent operation may cause
1644 +something unpleasant result.
1645 +
1646 +This situation is a result of VFS architecture, ->lookup() and
1647 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1648 +design from VFS's point of view. It is just not suitable for sub-VFS
1649 +character in aufs.
1650 +
1651 +Aufs supports such case by three level of revalidation which is
1652 +selectable by user.
1653 +1. Simple Revalidate
1654 +   Addition to the native flow in VFS's, confirm the child-parent
1655 +   relationship on the branch just after locking the parent dir on the
1656 +   branch in the "actual operation" (step 4). When this validation
1657 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1658 +   checks the validation of the dentry on branches.
1659 +2. Monitor Changes Internally by Inotify/Fsnotify
1660 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1661 +   the dentry on the branch, and returns EBUSY if it finds different
1662 +   dentry.
1663 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1664 +   during it is in cache. When the event is notified, aufs registers a
1665 +   function to kernel 'events' thread by schedule_work(). And the
1666 +   function sets some special status to the cached aufs dentry and inode
1667 +   private data. If they are not cached, then aufs has nothing to
1668 +   do. When the same file is accessed through aufs (step 0-3) later,
1669 +   aufs will detect the status and refresh all necessary data.
1670 +   In this mode, aufs has to ignore the event which is fired by aufs
1671 +   itself.
1672 +3. No Extra Validation
1673 +   This is the simplest test and doesn't add any additional revalidation
1674 +   test, and skip the revalidation in step 4. It is useful and improves
1675 +   aufs performance when system surely hide the aufs branches from user,
1676 +   by over-mounting something (or another method).
1677 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1678 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1679 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2021-11-01 23:48:34.203025928 +0100
1680 @@ -0,0 +1,74 @@
1681 +
1682 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1683 +# 
1684 +# This program is free software; you can redistribute it and/or modify
1685 +# it under the terms of the GNU General Public License as published by
1686 +# the Free Software Foundation; either version 2 of the License, or
1687 +# (at your option) any later version.
1688 +# 
1689 +# This program is distributed in the hope that it will be useful,
1690 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1691 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1692 +# GNU General Public License for more details.
1693 +# 
1694 +# You should have received a copy of the GNU General Public License
1695 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1696 +
1697 +Branch Manipulation
1698 +
1699 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1700 +and changing its permission/attribute, there are a lot of works to do.
1701 +
1702 +
1703 +Add a Branch
1704 +----------------------------------------------------------------------
1705 +o Confirm the adding dir exists outside of aufs, including loopback
1706 +  mount, and its various attributes.
1707 +o Initialize the xino file and whiteout bases if necessary.
1708 +  See struct.txt.
1709 +
1710 +o Check the owner/group/mode of the directory
1711 +  When the owner/group/mode of the adding directory differs from the
1712 +  existing branch, aufs issues a warning because it may impose a
1713 +  security risk.
1714 +  For example, when a upper writable branch has a world writable empty
1715 +  top directory, a malicious user can create any files on the writable
1716 +  branch directly, like copy-up and modify manually. If something like
1717 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1718 +  writable branch, and the writable branch is world-writable, then a
1719 +  malicious guy may create /etc/passwd on the writable branch directly
1720 +  and the infected file will be valid in aufs.
1721 +  I am afraid it can be a security issue, but aufs can do nothing except
1722 +  producing a warning.
1723 +
1724 +
1725 +Delete a Branch
1726 +----------------------------------------------------------------------
1727 +o Confirm the deleting branch is not busy
1728 +  To be general, there is one merit to adopt "remount" interface to
1729 +  manipulate branches. It is to discard caches. At deleting a branch,
1730 +  aufs checks the still cached (and connected) dentries and inodes. If
1731 +  there are any, then they are all in-use. An inode without its
1732 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1733 +
1734 +  For the cached one, aufs checks whether the same named entry exists on
1735 +  other branches.
1736 +  If the cached one is a directory, because aufs provides a merged view
1737 +  to users, as long as one dir is left on any branch aufs can show the
1738 +  dir to users. In this case, the branch can be removed from aufs.
1739 +  Otherwise aufs rejects deleting the branch.
1740 +
1741 +  If any file on the deleting branch is opened by aufs, then aufs
1742 +  rejects deleting.
1743 +
1744 +
1745 +Modify the Permission of a Branch
1746 +----------------------------------------------------------------------
1747 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1748 +  See struct.txt.
1749 +
1750 +o rw --> ro: Confirm the modifying branch is not busy
1751 +  Aufs rejects the request if any of these conditions are true.
1752 +  - a file on the branch is mmap-ed.
1753 +  - a regular file on the branch is opened for write and there is no
1754 +    same named entry on the upper branch.
1755 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1756 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1757 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2021-11-01 23:48:34.203025928 +0100
1758 @@ -0,0 +1,64 @@
1759 +
1760 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1761 +# 
1762 +# This program is free software; you can redistribute it and/or modify
1763 +# it under the terms of the GNU General Public License as published by
1764 +# the Free Software Foundation; either version 2 of the License, or
1765 +# (at your option) any later version.
1766 +# 
1767 +# This program is distributed in the hope that it will be useful,
1768 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1769 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1770 +# GNU General Public License for more details.
1771 +# 
1772 +# You should have received a copy of the GNU General Public License
1773 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1774 +
1775 +Policies to Select One among Multiple Writable Branches
1776 +----------------------------------------------------------------------
1777 +When the number of writable branch is more than one, aufs has to decide
1778 +the target branch for file creation or copy-up. By default, the highest
1779 +writable branch which has the parent (or ancestor) dir of the target
1780 +file is chosen (top-down-parent policy).
1781 +By user's request, aufs implements some other policies to select the
1782 +writable branch, for file creation several policies, round-robin,
1783 +most-free-space, and other policies. For copy-up, top-down-parent,
1784 +bottom-up-parent, bottom-up and others.
1785 +
1786 +As expected, the round-robin policy selects the branch in circular. When
1787 +you have two writable branches and creates 10 new files, 5 files will be
1788 +created for each branch. mkdir(2) systemcall is an exception. When you
1789 +create 10 new directories, all will be created on the same branch.
1790 +And the most-free-space policy selects the one which has most free
1791 +space among the writable branches. The amount of free space will be
1792 +checked by aufs internally, and users can specify its time interval.
1793 +
1794 +The policies for copy-up is more simple,
1795 +top-down-parent is equivalent to the same named on in create policy,
1796 +bottom-up-parent selects the writable branch where the parent dir
1797 +exists and the nearest upper one from the copyup-source,
1798 +bottom-up selects the nearest upper writable branch from the
1799 +copyup-source, regardless the existence of the parent dir.
1800 +
1801 +There are some rules or exceptions to apply these policies.
1802 +- If there is a readonly branch above the policy-selected branch and
1803 +  the parent dir is marked as opaque (a variation of whiteout), or the
1804 +  target (creating) file is whiteout-ed on the upper readonly branch,
1805 +  then the result of the policy is ignored and the target file will be
1806 +  created on the nearest upper writable branch than the readonly branch.
1807 +- If there is a writable branch above the policy-selected branch and
1808 +  the parent dir is marked as opaque or the target file is whiteouted
1809 +  on the branch, then the result of the policy is ignored and the target
1810 +  file will be created on the highest one among the upper writable
1811 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1812 +  it as usual.
1813 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1814 +  They try selecting the branch where the source exists as possible
1815 +  since copyup a large file will take long time. If it can't be,
1816 +  ie. the branch where the source exists is readonly, then they will
1817 +  follow the copyup policy.
1818 +- There is an exception for rename(2) when the target exists.
1819 +  If the rename target exists, aufs compares the index of the branches
1820 +  where the source and the target exists and selects the higher
1821 +  one. If the selected branch is readonly, then aufs follows the
1822 +  copyup policy.
1823 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1824 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1825 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2021-11-01 23:48:34.203025928 +0100
1826 @@ -0,0 +1,31 @@
1827 +
1828 +// to view this graph, run dot(1) command in GRAPHVIZ.
1829 +
1830 +digraph G {
1831 +node [shape=box];
1832 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1833 +
1834 +node [shape=oval];
1835 +
1836 +aufs_rename -> whinfo [label="store/remove"];
1837 +
1838 +node [shape=oval];
1839 +inode_list [label="h_inum list in branch\ncache"];
1840 +
1841 +node [shape=box];
1842 +whinode [label="h_inum list file"];
1843 +
1844 +node [shape=oval];
1845 +brmgmt [label="br_add/del/mod/umount"];
1846 +
1847 +brmgmt -> inode_list [label="create/remove"];
1848 +brmgmt -> whinode [label="load/store"];
1849 +
1850 +inode_list -> whinode [style=dashed,dir=both];
1851 +
1852 +aufs_rename -> inode_list [label="add/del"];
1853 +
1854 +aufs_lookup -> inode_list [label="search"];
1855 +
1856 +aufs_lookup -> whinfo [label="load/remove"];
1857 +}
1858 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1859 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1860 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2021-11-01 23:48:34.203025928 +0100
1861 @@ -0,0 +1,102 @@
1862 +
1863 +# Copyright (C) 2017-2021 Junjiro R. Okajima
1864 +#
1865 +# This program is free software; you can redistribute it and/or modify
1866 +# it under the terms of the GNU General Public License as published by
1867 +# the Free Software Foundation; either version 2 of the License, or
1868 +# (at your option) any later version.
1869 +#
1870 +# This program is distributed in the hope that it will be useful,
1871 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1872 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1873 +# GNU General Public License for more details.
1874 +#
1875 +# You should have received a copy of the GNU General Public License
1876 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1877 +
1878 +Special handling for renaming a directory (DIRREN)
1879 +----------------------------------------------------------------------
1880 +First, let's assume we have a simple usecase.
1881 +
1882 +- /u = /rw + /ro
1883 +- /rw/dirA exists
1884 +- /ro/dirA and /ro/dirA/file exist too
1885 +- there is no dirB on both branches
1886 +- a user issues rename("dirA", "dirB")
1887 +
1888 +Now, what should aufs behave against this rename(2)?
1889 +There are a few possible cases.
1890 +
1891 +A. returns EROFS.
1892 +   since dirA exists on a readonly branch which cannot be renamed.
1893 +B. returns EXDEV.
1894 +   it is possible to copy-up dirA (only the dir itself), but the child
1895 +   entries ("file" in this case) should not be. it must be a bad
1896 +   approach to copy-up recursively.
1897 +C. returns a success.
1898 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1899 +   is a violation of aufs' policy.
1900 +D. construct an extra information which indicates that /ro/dirA should
1901 +   be handled as the name of dirB.
1902 +   overlayfs has a similar feature called REDIRECT.
1903 +
1904 +Until now, aufs implements the case B only which returns EXDEV, and
1905 +expects the userspace application behaves like mv(1) which tries
1906 +issueing rename(2) recursively.
1907 +
1908 +A new aufs feature called DIRREN is introduced which implements the case
1909 +D. There are several "extra information" added.
1910 +
1911 +1. detailed info per renamed directory
1912 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1913 +2. the inode-number list of directories on a branch
1914 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1915 +
1916 +The filename of "detailed info per directory" represents the lower
1917 +branch, and its format is
1918 +- a type of the branch id
1919 +  one of these.
1920 +  + uuid (not implemented yet)
1921 +  + fsid
1922 +  + dev
1923 +- the inode-number of the branch root dir
1924 +
1925 +And it contains these info in a single regular file.
1926 +- magic number
1927 +- branch's inode-number of the logically renamed dir
1928 +- the name of the before-renamed dir
1929 +
1930 +The "detailed info per directory" file is created in aufs rename(2), and
1931 +loaded in any lookup.
1932 +The info is considered in lookup for the matching case only. Here
1933 +"matching" means that the root of branch (in the info filename) is same
1934 +to the current looking-up branch. After looking-up the before-renamed
1935 +name, the inode-number is compared. And the matched dentry is used.
1936 +
1937 +The "inode-number list of directories" is a regular file which contains
1938 +simply the inode-numbers on the branch. The file is created or updated
1939 +in removing the branch, and loaded in adding the branch. Its lifetime is
1940 +equal to the branch.
1941 +The list is refered in lookup, and when the current target inode is
1942 +found in the list, the aufs tries loading the "detailed info per
1943 +directory" and get the changed and valid name of the dir.
1944 +
1945 +Theoretically these "extra informaiton" may be able to be put into XATTR
1946 +in the dir inode. But aufs doesn't choose this way because
1947 +1. XATTR may not be supported by the branch (or its configuration)
1948 +2. XATTR may have its size limit.
1949 +3. XATTR may be less easy to convert than a regular file, when the
1950 +   format of the info is changed in the future.
1951 +At the same time, I agree that the regular file approach is much slower
1952 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1953 +better approach.
1954 +
1955 +This DIRREN feature is enabled by aufs configuration, and is activated
1956 +by a new mount option.
1957 +
1958 +For the more complicated case, there is a work with UDBA option, which
1959 +is to dected the direct access to the branches (by-passing aufs) and to
1960 +maintain the cashes in aufs. Since a single cached aufs dentry may
1961 +contains two names, before- and after-rename, the name comparision in
1962 +UDBA handler may not work correctly. In this case, the behaviour will be
1963 +equivalen to udba=reval case.
1964 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1965 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1966 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2021-11-01 23:48:34.203025928 +0100
1967 @@ -0,0 +1,120 @@
1968 +
1969 +# Copyright (C) 2011-2021 Junjiro R. Okajima
1970 +# 
1971 +# This program is free software; you can redistribute it and/or modify
1972 +# it under the terms of the GNU General Public License as published by
1973 +# the Free Software Foundation; either version 2 of the License, or
1974 +# (at your option) any later version.
1975 +# 
1976 +# This program is distributed in the hope that it will be useful,
1977 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1978 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1979 +# GNU General Public License for more details.
1980 +# 
1981 +# You should have received a copy of the GNU General Public License
1982 +# along with this program; if not, write to the Free Software
1983 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1984 +
1985 +
1986 +File-based Hierarchical Storage Management (FHSM)
1987 +----------------------------------------------------------------------
1988 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1989 +storage world. Aufs provides this feature as file-based with multiple
1990 +writable branches, based upon the principle of "Colder, the Lower".
1991 +Here the word "colder" means that the less used files, and "lower" means
1992 +that the position in the order of the stacked branches vertically.
1993 +These multiple writable branches are prioritized, ie. the topmost one
1994 +should be the fastest drive and be used heavily.
1995 +
1996 +o Characters in aufs FHSM story
1997 +- aufs itself and a new branch attribute.
1998 +- a new ioctl interface to move-down and to establish a connection with
1999 +  the daemon ("move-down" is a converse of "copy-up").
2000 +- userspace tool and daemon.
2001 +
2002 +The userspace daemon establishes a connection with aufs and waits for
2003 +the notification. The notified information is very similar to struct
2004 +statfs containing the number of consumed blocks and inodes.
2005 +When the consumed blocks/inodes of a branch exceeds the user-specified
2006 +upper watermark, the daemon activates its move-down process until the
2007 +consumed blocks/inodes reaches the user-specified lower watermark.
2008 +
2009 +The actual move-down is done by aufs based upon the request from
2010 +user-space since we need to maintain the inode number and the internal
2011 +pointer arrays in aufs.
2012 +
2013 +Currently aufs FHSM handles the regular files only. Additionally they
2014 +must not be hard-linked nor pseudo-linked.
2015 +
2016 +
2017 +o Cowork of aufs and the user-space daemon
2018 +  During the userspace daemon established the connection, aufs sends a
2019 +  small notification to it whenever aufs writes something into the
2020 +  writable branch. But it may cost high since aufs issues statfs(2)
2021 +  internally. So user can specify a new option to cache the
2022 +  info. Actually the notification is controlled by these factors.
2023 +  + the specified cache time.
2024 +  + classified as "force" by aufs internally.
2025 +  Until the specified time expires, aufs doesn't send the info
2026 +  except the forced cases. When aufs decide forcing, the info is always
2027 +  notified to userspace.
2028 +  For example, the number of free inodes is generally large enough and
2029 +  the shortage of it happens rarely. So aufs doesn't force the
2030 +  notification when creating a new file, directory and others. This is
2031 +  the typical case which aufs doesn't force.
2032 +  When aufs writes the actual filedata and the files consumes any of new
2033 +  blocks, the aufs forces notifying.
2034 +
2035 +
2036 +o Interfaces in aufs
2037 +- New branch attribute.
2038 +  + fhsm
2039 +    Specifies that the branch is managed by FHSM feature. In other word,
2040 +    participant in the FHSM.
2041 +    When nofhsm is set to the branch, it will not be the source/target
2042 +    branch of the move-down operation. This attribute is set
2043 +    independently from coo and moo attributes, and if you want full
2044 +    FHSM, you should specify them as well.
2045 +- New mount option.
2046 +  + fhsm_sec
2047 +    Specifies a second to suppress many less important info to be
2048 +    notified.
2049 +- New ioctl.
2050 +  + AUFS_CTL_FHSM_FD
2051 +    create a new file descriptor which userspace can read the notification
2052 +    (a subset of struct statfs) from aufs.
2053 +- Module parameter 'brs'
2054 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2055 +  be set.
2056 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2057 +  When there are two or more branches with fhsm attributes,
2058 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2059 +  terminates it. As a result of remounting and branch-manipulation, the
2060 +  number of branches with fhsm attribute can be one. In this case,
2061 +  /sbin/mount.aufs will terminate the user-space daemon.
2062 +
2063 +
2064 +Finally the operation is done as these steps in kernel-space.
2065 +- make sure that,
2066 +  + no one else is using the file.
2067 +  + the file is not hard-linked.
2068 +  + the file is not pseudo-linked.
2069 +  + the file is a regular file.
2070 +  + the parent dir is not opaqued.
2071 +- find the target writable branch.
2072 +- make sure the file is not whiteout-ed by the upper (than the target)
2073 +  branch.
2074 +- make the parent dir on the target branch.
2075 +- mutex lock the inode on the branch.
2076 +- unlink the whiteout on the target branch (if exists).
2077 +- lookup and create the whiteout-ed temporary name on the target branch.
2078 +- copy the file as the whiteout-ed temporary name on the target branch.
2079 +- rename the whiteout-ed temporary name to the original name.
2080 +- unlink the file on the source branch.
2081 +- maintain the internal pointer array and the external inode number
2082 +  table (XINO).
2083 +- maintain the timestamps and other attributes of the parent dir and the
2084 +  file.
2085 +
2086 +And of course, in every step, an error may happen. So the operation
2087 +should restore the original file state after an error happens.
2088 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2089 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2090 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2021-11-01 23:48:34.203025928 +0100
2091 @@ -0,0 +1,72 @@
2092 +
2093 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2094 +# 
2095 +# This program is free software; you can redistribute it and/or modify
2096 +# it under the terms of the GNU General Public License as published by
2097 +# the Free Software Foundation; either version 2 of the License, or
2098 +# (at your option) any later version.
2099 +# 
2100 +# This program is distributed in the hope that it will be useful,
2101 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2102 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2103 +# GNU General Public License for more details.
2104 +# 
2105 +# You should have received a copy of the GNU General Public License
2106 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2107 +
2108 +mmap(2) -- File Memory Mapping
2109 +----------------------------------------------------------------------
2110 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2111 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2112 +->mmap().
2113 +This approach is simple and good, but there is one problem.
2114 +Under /proc, several entries show the mmapped files by its path (with
2115 +device and inode number), and the printed path will be the path on the
2116 +branch fs's instead of virtual aufs's.
2117 +This is not a problem in most cases, but some utilities lsof(1) (and its
2118 +user) may expect the path on aufs.
2119 +
2120 +To address this issue, aufs adds a new member called vm_prfile in struct
2121 +vm_area_struct (and struct vm_region). The original vm_file points to
2122 +the file on the branch fs in order to handle everything correctly as
2123 +usual. The new vm_prfile points to a virtual file in aufs, and the
2124 +show-functions in procfs refers to vm_prfile if it is set.
2125 +Also we need to maintain several other places where touching vm_file
2126 +such like
2127 +- fork()/clone() copies vma and the reference count of vm_file is
2128 +  incremented.
2129 +- merging vma maintains the ref count too.
2130 +
2131 +This is not a good approach. It just fakes the printed path. But it
2132 +leaves all behaviour around f_mapping unchanged. This is surely an
2133 +advantage.
2134 +Actually aufs had adopted another complicated approach which calls
2135 +generic_file_mmap() and handles struct vm_operations_struct. In this
2136 +approach, aufs met a hard problem and I could not solve it without
2137 +switching the approach.
2138 +
2139 +There may be one more another approach which is
2140 +- bind-mount the branch-root onto the aufs-root internally
2141 +- grab the new vfsmount (ie. struct mount)
2142 +- lazy-umount the branch-root internally
2143 +- in open(2) the aufs-file, open the branch-file with the hidden
2144 +  vfsmount (instead of the original branch's vfsmount)
2145 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2146 +  but it may be possible from userspace by the mount helper.
2147 +
2148 +Adding the internal hidden vfsmount and using it in opening a file, the
2149 +file path under /proc will be printed correctly. This approach looks
2150 +smarter, but is not possible I am afraid.
2151 +- aufs-root may be bind-mount later. when it happens, another hidden
2152 +  vfsmount will be required.
2153 +- it is hard to get the chance to bind-mount and lazy-umount
2154 +  + in kernel-space, FS can have vfsmount in open(2) via
2155 +    file->f_path, and aufs can know its vfsmount. But several locks are
2156 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2157 +    here, then it may cause a deadlock.
2158 +  + in user-space, bind-mount doesn't invoke the mount helper.
2159 +- since /proc shows dev and ino, aufs has to give vma these info. it
2160 +  means a new member vm_prinode will be necessary. this is essentially
2161 +  equivalent to vm_prfile described above.
2162 +
2163 +I have to give up this "looks-smater" approach.
2164 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2165 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2166 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2021-11-01 23:48:34.203025928 +0100
2167 @@ -0,0 +1,96 @@
2168 +
2169 +# Copyright (C) 2014-2021 Junjiro R. Okajima
2170 +#
2171 +# This program is free software; you can redistribute it and/or modify
2172 +# it under the terms of the GNU General Public License as published by
2173 +# the Free Software Foundation; either version 2 of the License, or
2174 +# (at your option) any later version.
2175 +#
2176 +# This program is distributed in the hope that it will be useful,
2177 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2178 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2179 +# GNU General Public License for more details.
2180 +#
2181 +# You should have received a copy of the GNU General Public License
2182 +# along with this program; if not, write to the Free Software
2183 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2184 +
2185 +
2186 +Listing XATTR/EA and getting the value
2187 +----------------------------------------------------------------------
2188 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2189 +shows the values from the topmost existing file. This behaviour is good
2190 +for the non-dir entries since the bahaviour exactly matches the shown
2191 +information. But for the directories, aufs considers all the same named
2192 +entries on the lower branches. Which means, if one of the lower entry
2193 +rejects readdir call, then aufs returns an error even if the topmost
2194 +entry allows it. This behaviour is necessary to respect the branch fs's
2195 +security, but can make users confused since the user-visible standard
2196 +attributes don't match the behaviour.
2197 +To address this issue, aufs has a mount option called dirperm1 which
2198 +checks the permission for the topmost entry only, and ignores the lower
2199 +entry's permission.
2200 +
2201 +A similar issue can happen around XATTR.
2202 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2203 +always set. Otherwise these very unpleasant situation would happen.
2204 +- listxattr(2) may return the duplicated entries.
2205 +- users may not be able to remove or reset the XATTR forever,
2206 +
2207 +
2208 +XATTR/EA support in the internal (copy,move)-(up,down)
2209 +----------------------------------------------------------------------
2210 +Generally the extended attributes of inode are categorized as these.
2211 +- "security" for LSM and capability.
2212 +- "system" for posix ACL, 'acl' mount option is required for the branch
2213 +  fs generally.
2214 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2215 +- "user" for userspace, 'user_xattr' mount option is required for the
2216 +  branch fs generally.
2217 +
2218 +Moreover there are some other categories. Aufs handles these rather
2219 +unpopular categories as the ordinary ones, ie. there is no special
2220 +condition nor exception.
2221 +
2222 +In copy-up, the support for XATTR on the dst branch may differ from the
2223 +src branch. In this case, the copy-up operation will get an error and
2224 +the original user operation which triggered the copy-up will fail. It
2225 +can happen that even all copy-up will fail.
2226 +When both of src and dst branches support XATTR and if an error occurs
2227 +during copying XATTR, then the copy-up should fail obviously. That is a
2228 +good reason and aufs should return an error to userspace. But when only
2229 +the src branch support that XATTR, aufs should not return an error.
2230 +For example, the src branch supports ACL but the dst branch doesn't
2231 +because the dst branch may natively un-support it or temporary
2232 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2233 +may NOT return an error even if the XATTR is not supported. It is
2234 +totally up to the branch fs.
2235 +
2236 +Anyway when the aufs internal copy-up gets an error from the dst branch
2237 +fs, then aufs tries removing the just copied entry and returns the error
2238 +to the userspace. The worst case of this situation will be all copy-up
2239 +will fail.
2240 +
2241 +For the copy-up operation, there two basic approaches.
2242 +- copy the specified XATTR only (by category above), and return the
2243 +  error unconditionally if it happens.
2244 +- copy all XATTR, and ignore the error on the specified category only.
2245 +
2246 +In order to support XATTR and to implement the correct behaviour, aufs
2247 +chooses the latter approach and introduces some new branch attributes,
2248 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2249 +They correspond to the XATTR namespaces (see above). Additionally, to be
2250 +convenient, "icex" is also provided which means all "icex*" attributes
2251 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2252 +
2253 +The meaning of these attributes is to ignore the error from setting
2254 +XATTR on that branch.
2255 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2256 +error from the dst branch according to the specified attributes.
2257 +
2258 +Some XATTR may have its default value. The default value may come from
2259 +the parent dir or the environment. If the default value is set at the
2260 +file creating-time, it will be overwritten by copy-up.
2261 +Some contradiction may happen I am afraid.
2262 +Do we need another attribute to stop copying XATTR? I am unsure. For
2263 +now, aufs implements the branch attributes to ignore the error.
2264 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2265 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2266 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2021-11-01 23:48:34.203025928 +0100
2267 @@ -0,0 +1,58 @@
2268 +
2269 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2270 +# 
2271 +# This program is free software; you can redistribute it and/or modify
2272 +# it under the terms of the GNU General Public License as published by
2273 +# the Free Software Foundation; either version 2 of the License, or
2274 +# (at your option) any later version.
2275 +# 
2276 +# This program is distributed in the hope that it will be useful,
2277 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2278 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2279 +# GNU General Public License for more details.
2280 +# 
2281 +# You should have received a copy of the GNU General Public License
2282 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2283 +
2284 +Export Aufs via NFS
2285 +----------------------------------------------------------------------
2286 +Here is an approach.
2287 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2288 +  generation.
2289 +- iget_locked(): initialize aufs inode generation for a new inode, and
2290 +  store it in xigen file.
2291 +- destroy_inode(): increment aufs inode generation and store it in xigen
2292 +  file. it is necessary even if it is not unlinked, because any data of
2293 +  inode may be changed by UDBA.
2294 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2295 +  build file handle by
2296 +  + branch id (4 bytes)
2297 +  + superblock generation (4 bytes)
2298 +  + inode number (4 or 8 bytes)
2299 +  + parent dir inode number (4 or 8 bytes)
2300 +  + inode generation (4 bytes))
2301 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2302 +    bytes)
2303 +  + file handle for a branch (by exportfs_encode_fh())
2304 +- fh_to_dentry():
2305 +  + find the index of a branch from its id in handle, and check it is
2306 +    still exist in aufs.
2307 +  + 1st level: get the inode number from handle and search it in cache.
2308 +  + 2nd level: if not found in cache, get the parent inode number from
2309 +    the handle and search it in cache. and then open the found parent
2310 +    dir, find the matching inode number by vfs_readdir() and get its
2311 +    name, and call lookup_one_len() for the target dentry.
2312 +  + 3rd level: if the parent dir is not cached, call
2313 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2314 +    build a pathname of it, convert it a pathname in aufs, call
2315 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2316 +    the 2nd level.
2317 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2318 +    for every branch, but not itself. to get this, (currently) aufs
2319 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2320 +    idea, but I didn't get other approach.
2321 +  + test the generation of the gotten inode.
2322 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2323 +  convert it into ESTALE for NFSD.
2324 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2325 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2326 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2327 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2328 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2021-11-01 23:48:34.203025928 +0100
2329 @@ -0,0 +1,52 @@
2330 +
2331 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2332 +# 
2333 +# This program is free software; you can redistribute it and/or modify
2334 +# it under the terms of the GNU General Public License as published by
2335 +# the Free Software Foundation; either version 2 of the License, or
2336 +# (at your option) any later version.
2337 +# 
2338 +# This program is distributed in the hope that it will be useful,
2339 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2340 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2341 +# GNU General Public License for more details.
2342 +# 
2343 +# You should have received a copy of the GNU General Public License
2344 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2345 +
2346 +Show Whiteout Mode (shwh)
2347 +----------------------------------------------------------------------
2348 +Generally aufs hides the name of whiteouts. But in some cases, to show
2349 +them is very useful for users. For instance, creating a new middle layer
2350 +(branch) by merging existing layers.
2351 +
2352 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2353 +When you have three branches,
2354 +- Bottom: 'system', squashfs (underlying base system), read-only
2355 +- Middle: 'mods', squashfs, read-only
2356 +- Top: 'overlay', ram (tmpfs), read-write
2357 +
2358 +The top layer is loaded at boot time and saved at shutdown, to preserve
2359 +the changes made to the system during the session.
2360 +When larger changes have been made, or smaller changes have accumulated,
2361 +the size of the saved top layer data grows. At this point, it would be
2362 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2363 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2364 +restoring save and load speed.
2365 +
2366 +This merging is simplified by the use of another aufs mount, of just the
2367 +two overlay branches using the 'shwh' option.
2368 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2369 +       aufs /livesys/merge_union
2370 +
2371 +A merged view of these two branches is then available at
2372 +/livesys/merge_union, and the new feature is that the whiteouts are
2373 +visible!
2374 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2375 +writing to all branches. Also the default mode for all branches is 'ro'.
2376 +It is now possible to save the combined contents of the two overlay
2377 +branches to a new squashfs, e.g.:
2378 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2379 +
2380 +This new squashfs archive can be stored on the boot device and the
2381 +initramfs will use it to replace the old one at the next boot.
2382 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2383 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2384 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2021-11-01 23:48:34.203025928 +0100
2385 @@ -0,0 +1,47 @@
2386 +
2387 +# Copyright (C) 2010-2021 Junjiro R. Okajima
2388 +#
2389 +# This program is free software; you can redistribute it and/or modify
2390 +# it under the terms of the GNU General Public License as published by
2391 +# the Free Software Foundation; either version 2 of the License, or
2392 +# (at your option) any later version.
2393 +#
2394 +# This program is distributed in the hope that it will be useful,
2395 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2396 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2397 +# GNU General Public License for more details.
2398 +#
2399 +# You should have received a copy of the GNU General Public License
2400 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2401 +
2402 +Dynamically customizable FS operations
2403 +----------------------------------------------------------------------
2404 +Generally FS operations (struct inode_operations, struct
2405 +address_space_operations, struct file_operations, etc.) are defined as
2406 +"static const", but it never means that FS have only one set of
2407 +operation. Some FS have multiple sets of them. For instance, ext2 has
2408 +three sets, one for XIP, for NOBH, and for normal.
2409 +Since aufs overrides and redirects these operations, sometimes aufs has
2410 +to change its behaviour according to the branch FS type. More importantly
2411 +VFS acts differently if a function (member in the struct) is set or
2412 +not. It means aufs should have several sets of operations and select one
2413 +among them according to the branch FS definition.
2414 +
2415 +In order to solve this problem and not to affect the behaviour of VFS,
2416 +aufs defines these operations dynamically. For instance, aufs defines
2417 +dummy direct_IO function for struct address_space_operations, but it may
2418 +not be set to the address_space_operations actually. When the branch FS
2419 +doesn't have it, aufs doesn't set it to its address_space_operations
2420 +while the function definition itself is still alive. So the behaviour
2421 +itself will not change, and it will return an error when direct_IO is
2422 +not set.
2423 +
2424 +The lifetime of these dynamically generated operation object is
2425 +maintained by aufs branch object. When the branch is removed from aufs,
2426 +the reference counter of the object is decremented. When it reaches
2427 +zero, the dynamically generated operation object will be freed.
2428 +
2429 +This approach is designed to support AIO (io_submit), Direct I/O and
2430 +XIP (DAX) mainly.
2431 +Currently this approach is applied to address_space_operations for
2432 +regular files only.
2433 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2434 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2435 +++ linux/Documentation/filesystems/aufs/README 2021-11-01 23:48:34.199692595 +0100
2436 @@ -0,0 +1,396 @@
2437 +
2438 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2439 +http://aufs.sf.net
2440 +Junjiro R. Okajima
2441 +
2442 +
2443 +0. Introduction
2444 +----------------------------------------
2445 +In the early days, aufs was entirely re-designed and re-implemented
2446 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2447 +improvements and implementations, it became totally different from
2448 +Unionfs while keeping the basic features.
2449 +Later, Unionfs Version 2.x series began taking some of the same
2450 +approaches to aufs1's.
2451 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2452 +University and his team.
2453 +
2454 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2455 +support,
2456 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2457 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2458 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2459 +  or aufs1 from CVS on SourceForge.
2460 +
2461 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2462 +      According to Christoph Hellwig, linux rejects all union-type
2463 +      filesystems but UnionMount.
2464 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2465 +
2466 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2467 +    UnionMount, and he pointed out an issue around a directory mutex
2468 +    lock and aufs addressed it. But it is still unsure whether aufs will
2469 +    be merged (or any other union solution).
2470 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2471 +
2472 +
2473 +1. Features
2474 +----------------------------------------
2475 +- unite several directories into a single virtual filesystem. The member
2476 +  directory is called as a branch.
2477 +- you can specify the permission flags to the branch, which are 'readonly',
2478 +  'readwrite' and 'whiteout-able.'
2479 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2480 +  readonly branch are modifiable logically.
2481 +- dynamic branch manipulation, add, del.
2482 +- etc...
2483 +
2484 +Also there are many enhancements in aufs, such as:
2485 +- test only the highest one for the directory permission (dirperm1)
2486 +- copyup on open (coo=)
2487 +- 'move' policy for copy-up between two writable branches, after
2488 +  checking free space.
2489 +- xattr, acl
2490 +- readdir(3) in userspace.
2491 +- keep inode number by external inode number table
2492 +- keep the timestamps of file/dir in internal copyup operation
2493 +- seekable directory, supporting NFS readdir.
2494 +- whiteout is hardlinked in order to reduce the consumption of inodes
2495 +  on branch
2496 +- do not copyup, nor create a whiteout when it is unnecessary
2497 +- revert a single systemcall when an error occurs in aufs
2498 +- remount interface instead of ioctl
2499 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2500 +- loopback mounted filesystem as a branch
2501 +- kernel thread for removing the dir who has a plenty of whiteouts
2502 +- support copyup sparse file (a file which has a 'hole' in it)
2503 +- default permission flags for branches
2504 +- selectable permission flags for ro branch, whether whiteout can
2505 +  exist or not
2506 +- export via NFS.
2507 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2508 +- support multiple writable branches, some policies to select one
2509 +  among multiple writable branches.
2510 +- a new semantics for link(2) and rename(2) to support multiple
2511 +  writable branches.
2512 +- no glibc changes are required.
2513 +- pseudo hardlink (hardlink over branches)
2514 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2515 +  including NFS or remote filesystem branch.
2516 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2517 +- and more...
2518 +
2519 +Currently these features are dropped temporary from aufs5.
2520 +See design/08plan.txt in detail.
2521 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2522 +  (robr)
2523 +- statistics of aufs thread (/sys/fs/aufs/stat)
2524 +
2525 +Features or just an idea in the future (see also design/*.txt),
2526 +- reorder the branch index without del/re-add.
2527 +- permanent xino files for NFSD
2528 +- an option for refreshing the opened files after add/del branches
2529 +- light version, without branch manipulation. (unnecessary?)
2530 +- copyup in userspace
2531 +- inotify in userspace
2532 +- readv/writev
2533 +
2534 +
2535 +2. Download
2536 +----------------------------------------
2537 +There are three GIT trees for aufs5, aufs5-linux.git,
2538 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2539 +"aufs-util.git."
2540 +While the aufs-util is always necessary, you need either of aufs5-linux
2541 +or aufs5-standalone.
2542 +
2543 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2544 +git://git.kernel.org/.../torvalds/linux.git.
2545 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2546 +build aufs5 as an external kernel module.
2547 +Several extra patches are not included in this tree. Only
2548 +aufs5-standalone tree contains them. They are described in the later
2549 +section "Configuration and Compilation."
2550 +
2551 +On the other hand, the aufs5-standalone tree has only aufs source files
2552 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2553 +But you need to apply all aufs patches manually.
2554 +
2555 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2556 +represents the linux kernel version, "linux-5.x". For instance,
2557 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2558 +"aufs5.x-rcN" branch.
2559 +
2560 +o aufs5-linux tree
2561 +$ git clone --reference /your/linux/git/tree \
2562 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2563 +- if you don't have linux GIT tree, then remove "--reference ..."
2564 +$ cd aufs5-linux.git
2565 +$ git checkout origin/aufs5.0
2566 +
2567 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2568 +leave the patch-work to GIT.
2569 +$ cd /your/linux/git/tree
2570 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2571 +$ git fetch aufs5
2572 +$ git checkout -b my5.0 v5.0
2573 +$ (add your local change...)
2574 +$ git pull aufs5 aufs5.0
2575 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2576 +- you may need to solve some conflicts between your_changes and
2577 +  aufs5.0. in this case, git-rerere is recommended so that you can
2578 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2579 +  later in the future.
2580 +
2581 +o aufs5-standalone tree
2582 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2583 +$ cd aufs5-standalone.git
2584 +$ git checkout origin/aufs5.0
2585 +
2586 +o aufs-util tree
2587 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2588 +- note that the public aufs-util.git is on SourceForge instead of
2589 +  GitHUB.
2590 +$ cd aufs-util.git
2591 +$ git checkout origin/aufs5.0
2592 +
2593 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2594 +The minor version number, 'x' in '5.x', of aufs may not always
2595 +follow the minor version number of the kernel.
2596 +Because changes in the kernel that cause the use of a new
2597 +minor version number do not always require changes to aufs-util.
2598 +
2599 +Since aufs-util has its own minor version number, you may not be
2600 +able to find a GIT branch in aufs-util for your kernel's
2601 +exact minor version number.
2602 +In this case, you should git-checkout the branch for the
2603 +nearest lower number.
2604 +
2605 +For (an unreleased) example:
2606 +If you are using "linux-5.10" and the "aufs5.10" branch
2607 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2608 +or something numerically smaller is the branch for your kernel.
2609 +
2610 +Also you can view all branches by
2611 +       $ git branch -a
2612 +
2613 +
2614 +3. Configuration and Compilation
2615 +----------------------------------------
2616 +Make sure you have git-checkout'ed the correct branch.
2617 +
2618 +For aufs5-linux tree,
2619 +- enable CONFIG_AUFS_FS.
2620 +- set other aufs configurations if necessary.
2621 +- for aufs5.13 and later
2622 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2623 +  also a caller of VFS functions for branch filesystems, subclassing of
2624 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2625 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2626 +  need to customize some LOCKDEP numbers. Here are what I use on my
2627 +  test environment.
2628 +       CONFIG_LOCKDEP_BITS=21
2629 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2630 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2631 +
2632 +For aufs5-standalone tree,
2633 +There are several ways to build.
2634 +
2635 +1.
2636 +- apply ./aufs5-kbuild.patch to your kernel source files.
2637 +- apply ./aufs5-base.patch too.
2638 +- apply ./aufs5-mmap.patch too.
2639 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2640 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2641 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2642 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2643 +- enable CONFIG_AUFS_FS, you can select either
2644 +  =m or =y.
2645 +- and build your kernel as usual.
2646 +- install the built kernel.
2647 +- install the header files too by "make headers_install" to the
2648 +  directory where you specify. By default, it is $PWD/usr.
2649 +  "make help" shows a brief note for headers_install.
2650 +- and reboot your system.
2651 +
2652 +2.
2653 +- module only (CONFIG_AUFS_FS=m).
2654 +- apply ./aufs5-base.patch to your kernel source files.
2655 +- apply ./aufs5-mmap.patch too.
2656 +- apply ./aufs5-standalone.patch too.
2657 +- build your kernel, don't forget "make headers_install", and reboot.
2658 +- edit ./config.mk and set other aufs configurations if necessary.
2659 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2660 +  every aufs configurations.
2661 +- build the module by simple "make".
2662 +- you can specify ${KDIR} make variable which points to your kernel
2663 +  source tree.
2664 +- install the files
2665 +  + run "make install" to install the aufs module, or copy the built
2666 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2667 +  + run "make install_headers" (instead of headers_install) to install
2668 +    the modified aufs header file (you can specify DESTDIR which is
2669 +    available in aufs standalone version's Makefile only), or copy
2670 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2671 +    you like manually. By default, the target directory is $PWD/usr.
2672 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2673 +  kernel source tree.
2674 +
2675 +Note: The header file aufs_type.h is necessary to build aufs-util
2676 +      as well as "make headers_install" in the kernel source tree.
2677 +      headers_install is subject to be forgotten, but it is essentially
2678 +      necessary, not only for building aufs-util.
2679 +      You may not meet problems without headers_install in some older
2680 +      version though.
2681 +
2682 +And then,
2683 +- read README in aufs-util, build and install it
2684 +- note that your distribution may contain an obsoleted version of
2685 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2686 +  utilities, make sure that your compiler refers the correct aufs header
2687 +  file which is built by "make headers_install."
2688 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2689 +  then run "make install_ulib" too. And refer to the aufs manual in
2690 +  detail.
2691 +
2692 +There several other patches in aufs5-standalone.git. They are all
2693 +optional. When you meet some problems, they will help you.
2694 +- aufs5-loopback.patch
2695 +  Supports a nested loopback mount in a branch-fs. This patch is
2696 +  unnecessary until aufs produces a message like "you may want to try
2697 +  another patch for loopback file".
2698 +- vfs-ino.patch
2699 +  Modifies a system global kernel internal function get_next_ino() in
2700 +  order to stop assigning 0 for an inode-number. Not directly related to
2701 +  aufs, but recommended generally.
2702 +- tmpfs-idr.patch
2703 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2704 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2705 +  duplication of inode number, which is important for backup tools and
2706 +  other utilities. When you find aufs XINO files for tmpfs branch
2707 +  growing too much, try this patch.
2708 +
2709 +
2710 +4. Usage
2711 +----------------------------------------
2712 +At first, make sure aufs-util are installed, and please read the aufs
2713 +manual, aufs.5 in aufs-util.git tree.
2714 +$ man -l aufs.5
2715 +
2716 +And then,
2717 +$ mkdir /tmp/rw /tmp/aufs
2718 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2719 +
2720 +Here is another example. The result is equivalent.
2721 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2722 +  Or
2723 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2724 +# mount -o remount,append:${HOME} /tmp/aufs
2725 +
2726 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2727 +you modify a file under /tmp/aufs, the one on your home directory is
2728 +not affected, instead the same named file will be newly created under
2729 +/tmp/rw. And all of your modification to a file will be applied to
2730 +the one under /tmp/rw. This is called the file based Copy on Write
2731 +(COW) method.
2732 +Aufs mount options are described in aufs.5.
2733 +If you run chroot or something and make your aufs as a root directory,
2734 +then you need to customize the shutdown script. See the aufs manual in
2735 +detail.
2736 +
2737 +Additionally, there are some sample usages of aufs which are a
2738 +diskless system with network booting, and LiveCD over NFS.
2739 +See sample dir in CVS tree on SourceForge.
2740 +
2741 +
2742 +5. Contact
2743 +----------------------------------------
2744 +When you have any problems or strange behaviour in aufs, please let me
2745 +know with:
2746 +- /proc/mounts (instead of the output of mount(8))
2747 +- /sys/module/aufs/*
2748 +- /sys/fs/aufs/* (if you have them)
2749 +- /debug/aufs/* (if you have them)
2750 +- linux kernel version
2751 +  if your kernel is not plain, for example modified by distributor,
2752 +  the url where i can download its source is necessary too.
2753 +- aufs version which was printed at loading the module or booting the
2754 +  system, instead of the date you downloaded.
2755 +- configuration (define/undefine CONFIG_AUFS_xxx)
2756 +- kernel configuration or /proc/config.gz (if you have it)
2757 +- LSM (linux security module, if you are using)
2758 +- behaviour which you think to be incorrect
2759 +- actual operation, reproducible one is better
2760 +- mailto: aufs-users at lists.sourceforge.net
2761 +
2762 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2763 +and Feature Requests) on SourceForge. Please join and write to
2764 +aufs-users ML.
2765 +
2766 +
2767 +6. Acknowledgements
2768 +----------------------------------------
2769 +Thanks to everyone who have tried and are using aufs, whoever
2770 +have reported a bug or any feedback.
2771 +
2772 +Especially donators:
2773 +Tomas Matejicek(slax.org) made a donation (much more than once).
2774 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2775 +       scripts) is making "doubling" donations.
2776 +       Unfortunately I cannot list all of the donators, but I really
2777 +       appreciate.
2778 +       It ends Aug 2010, but the ordinary donation URL is still available.
2779 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2780 +Dai Itasaka made a donation (2007/8).
2781 +Chuck Smith made a donation (2008/4, 10 and 12).
2782 +Henk Schoneveld made a donation (2008/9).
2783 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2784 +Francois Dupoux made a donation (2008/11).
2785 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2786 +       aufs2 GIT tree (2009/2).
2787 +William Grant made a donation (2009/3).
2788 +Patrick Lane made a donation (2009/4).
2789 +The Mail Archive (mail-archive.com) made donations (2009/5).
2790 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2791 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2792 +Pavel Pronskiy made a donation (2011/2).
2793 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2794 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2795 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2796 +11).
2797 +Sam Liddicott made a donation (2011/9).
2798 +Era Scarecrow made a donation (2013/4).
2799 +Bor Ratajc made a donation (2013/4).
2800 +Alessandro Gorreta made a donation (2013/4).
2801 +POIRETTE Marc made a donation (2013/4).
2802 +Alessandro Gorreta made a donation (2013/4).
2803 +lauri kasvandik made a donation (2013/5).
2804 +"pemasu from Finland" made a donation (2013/7).
2805 +The Parted Magic Project made a donation (2013/9 and 11).
2806 +Pavel Barta made a donation (2013/10).
2807 +Nikolay Pertsev made a donation (2014/5).
2808 +James B made a donation (2014/7 and 2015/7).
2809 +Stefano Di Biase made a donation (2014/8).
2810 +Daniel Epellei made a donation (2015/1).
2811 +OmegaPhil made a donation (2016/1, 2018/4).
2812 +Tomasz Szewczyk made a donation (2016/4).
2813 +James Burry made a donation (2016/12).
2814 +Carsten Rose made a donation (2018/9).
2815 +Porteus Kiosk made a donation (2018/10).
2816 +
2817 +Thank you very much.
2818 +Donations are always, including future donations, very important and
2819 +helpful for me to keep on developing aufs.
2820 +
2821 +
2822 +7.
2823 +----------------------------------------
2824 +If you are an experienced user, no explanation is needed. Aufs is
2825 +just a linux filesystem.
2826 +
2827 +
2828 +Enjoy!
2829 +
2830 +# Local variables: ;
2831 +# mode: text;
2832 +# End: ;
2833 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2834 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2835 +++ linux/fs/aufs/aufs.h        2021-11-01 23:48:34.203025928 +0100
2836 @@ -0,0 +1,62 @@
2837 +/* SPDX-License-Identifier: GPL-2.0 */
2838 +/*
2839 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2840 + *
2841 + * This program, aufs is free software; you can redistribute it and/or modify
2842 + * it under the terms of the GNU General Public License as published by
2843 + * the Free Software Foundation; either version 2 of the License, or
2844 + * (at your option) any later version.
2845 + *
2846 + * This program is distributed in the hope that it will be useful,
2847 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2848 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2849 + * GNU General Public License for more details.
2850 + *
2851 + * You should have received a copy of the GNU General Public License
2852 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2853 + */
2854 +
2855 +/*
2856 + * all header files
2857 + */
2858 +
2859 +#ifndef __AUFS_H__
2860 +#define __AUFS_H__
2861 +
2862 +#ifdef __KERNEL__
2863 +
2864 +#define AuStub(type, name, body, ...) \
2865 +       static inline type name(__VA_ARGS__) { body; }
2866 +
2867 +#define AuStubVoid(name, ...) \
2868 +       AuStub(void, name, , __VA_ARGS__)
2869 +#define AuStubInt0(name, ...) \
2870 +       AuStub(int, name, return 0, __VA_ARGS__)
2871 +
2872 +#include "debug.h"
2873 +
2874 +#include "branch.h"
2875 +#include "cpup.h"
2876 +#include "dcsub.h"
2877 +#include "dbgaufs.h"
2878 +#include "dentry.h"
2879 +#include "dir.h"
2880 +#include "dirren.h"
2881 +#include "dynop.h"
2882 +#include "file.h"
2883 +#include "fstype.h"
2884 +#include "hbl.h"
2885 +#include "inode.h"
2886 +#include "lcnt.h"
2887 +#include "loop.h"
2888 +#include "module.h"
2889 +#include "opts.h"
2890 +#include "rwsem.h"
2891 +#include "super.h"
2892 +#include "sysaufs.h"
2893 +#include "vfsub.h"
2894 +#include "whout.h"
2895 +#include "wkq.h"
2896 +
2897 +#endif /* __KERNEL__ */
2898 +#endif /* __AUFS_H__ */
2899 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2900 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2901 +++ linux/fs/aufs/branch.c      2021-11-01 23:48:34.203025928 +0100
2902 @@ -0,0 +1,1427 @@
2903 +// SPDX-License-Identifier: GPL-2.0
2904 +/*
2905 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2906 + *
2907 + * This program, aufs is free software; you can redistribute it and/or modify
2908 + * it under the terms of the GNU General Public License as published by
2909 + * the Free Software Foundation; either version 2 of the License, or
2910 + * (at your option) any later version.
2911 + *
2912 + * This program is distributed in the hope that it will be useful,
2913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2915 + * GNU General Public License for more details.
2916 + *
2917 + * You should have received a copy of the GNU General Public License
2918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2919 + */
2920 +
2921 +/*
2922 + * branch management
2923 + */
2924 +
2925 +#include <linux/compat.h>
2926 +#include <linux/statfs.h>
2927 +#include "aufs.h"
2928 +
2929 +/*
2930 + * free a single branch
2931 + */
2932 +static void au_br_do_free(struct au_branch *br)
2933 +{
2934 +       int i;
2935 +       struct au_wbr *wbr;
2936 +       struct au_dykey **key;
2937 +
2938 +       au_hnotify_fin_br(br);
2939 +       /* always, regardless the mount option */
2940 +       au_dr_hino_free(&br->br_dirren);
2941 +       au_xino_put(br);
2942 +
2943 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2944 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2945 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2946 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2947 +
2948 +       wbr = br->br_wbr;
2949 +       if (wbr) {
2950 +               for (i = 0; i < AuBrWh_Last; i++)
2951 +                       dput(wbr->wbr_wh[i]);
2952 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2953 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2954 +       }
2955 +
2956 +       if (br->br_fhsm) {
2957 +               au_br_fhsm_fin(br->br_fhsm);
2958 +               au_kfree_try_rcu(br->br_fhsm);
2959 +       }
2960 +
2961 +       key = br->br_dykey;
2962 +       for (i = 0; i < AuBrDynOp; i++, key++)
2963 +               if (*key)
2964 +                       au_dy_put(*key);
2965 +               else
2966 +                       break;
2967 +
2968 +       /* recursive lock, s_umount of branch's */
2969 +       /* synchronize_rcu(); */ /* why? */
2970 +       lockdep_off();
2971 +       path_put(&br->br_path);
2972 +       lockdep_on();
2973 +       au_kfree_rcu(wbr);
2974 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2975 +       au_lcnt_wait_for_fin(&br->br_count);
2976 +       /* I don't know why, but percpu_refcount requires this */
2977 +       /* synchronize_rcu(); */
2978 +       au_kfree_rcu(br);
2979 +}
2980 +
2981 +/*
2982 + * frees all branches
2983 + */
2984 +void au_br_free(struct au_sbinfo *sbinfo)
2985 +{
2986 +       aufs_bindex_t bmax;
2987 +       struct au_branch **br;
2988 +
2989 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2990 +
2991 +       bmax = sbinfo->si_bbot + 1;
2992 +       br = sbinfo->si_branch;
2993 +       while (bmax--)
2994 +               au_br_do_free(*br++);
2995 +}
2996 +
2997 +/*
2998 + * find the index of a branch which is specified by @br_id.
2999 + */
3000 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3001 +{
3002 +       aufs_bindex_t bindex, bbot;
3003 +
3004 +       bbot = au_sbbot(sb);
3005 +       for (bindex = 0; bindex <= bbot; bindex++)
3006 +               if (au_sbr_id(sb, bindex) == br_id)
3007 +                       return bindex;
3008 +       return -1;
3009 +}
3010 +
3011 +/* ---------------------------------------------------------------------- */
3012 +
3013 +/*
3014 + * add a branch
3015 + */
3016 +
3017 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3018 +                       struct dentry *h_root)
3019 +{
3020 +       if (unlikely(h_adding == h_root
3021 +                    || au_test_loopback_overlap(sb, h_adding)))
3022 +               return 1;
3023 +       if (h_adding->d_sb != h_root->d_sb)
3024 +               return 0;
3025 +       return au_test_subdir(h_adding, h_root)
3026 +               || au_test_subdir(h_root, h_adding);
3027 +}
3028 +
3029 +/*
3030 + * returns a newly allocated branch. @new_nbranch is a number of branches
3031 + * after adding a branch.
3032 + */
3033 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3034 +                                    int perm)
3035 +{
3036 +       struct au_branch *add_branch;
3037 +       struct dentry *root;
3038 +       struct inode *inode;
3039 +       int err;
3040 +
3041 +       err = -ENOMEM;
3042 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3043 +       if (unlikely(!add_branch))
3044 +               goto out;
3045 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3046 +       if (unlikely(!add_branch->br_xino))
3047 +               goto out_br;
3048 +       err = au_hnotify_init_br(add_branch, perm);
3049 +       if (unlikely(err))
3050 +               goto out_xino;
3051 +
3052 +       if (au_br_writable(perm)) {
3053 +               /* may be freed separately at changing the branch permission */
3054 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3055 +                                            GFP_NOFS);
3056 +               if (unlikely(!add_branch->br_wbr))
3057 +                       goto out_hnotify;
3058 +       }
3059 +
3060 +       if (au_br_fhsm(perm)) {
3061 +               err = au_fhsm_br_alloc(add_branch);
3062 +               if (unlikely(err))
3063 +                       goto out_wbr;
3064 +       }
3065 +
3066 +       root = sb->s_root;
3067 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3068 +       if (!err)
3069 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3070 +       if (!err) {
3071 +               inode = d_inode(root);
3072 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3073 +                                       /*may_shrink*/0);
3074 +       }
3075 +       if (!err)
3076 +               return add_branch; /* success */
3077 +
3078 +out_wbr:
3079 +       au_kfree_rcu(add_branch->br_wbr);
3080 +out_hnotify:
3081 +       au_hnotify_fin_br(add_branch);
3082 +out_xino:
3083 +       au_xino_put(add_branch);
3084 +out_br:
3085 +       au_kfree_rcu(add_branch);
3086 +out:
3087 +       return ERR_PTR(err);
3088 +}
3089 +
3090 +/*
3091 + * test if the branch permission is legal or not.
3092 + */
3093 +static int test_br(struct inode *inode, int brperm, char *path)
3094 +{
3095 +       int err;
3096 +
3097 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3098 +       if (!err)
3099 +               goto out;
3100 +
3101 +       err = -EINVAL;
3102 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3103 +
3104 +out:
3105 +       return err;
3106 +}
3107 +
3108 +/*
3109 + * returns:
3110 + * 0: success, the caller will add it
3111 + * plus: success, it is already unified, the caller should ignore it
3112 + * minus: error
3113 + */
3114 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3115 +{
3116 +       int err;
3117 +       aufs_bindex_t bbot, bindex;
3118 +       struct dentry *root, *h_dentry;
3119 +       struct inode *inode, *h_inode;
3120 +
3121 +       root = sb->s_root;
3122 +       bbot = au_sbbot(sb);
3123 +       if (unlikely(bbot >= 0
3124 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3125 +               err = 1;
3126 +               if (!remount) {
3127 +                       err = -EINVAL;
3128 +                       pr_err("%s duplicated\n", add->pathname);
3129 +               }
3130 +               goto out;
3131 +       }
3132 +
3133 +       err = -ENOSPC; /* -E2BIG; */
3134 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3135 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3136 +               pr_err("number of branches exceeded %s\n", add->pathname);
3137 +               goto out;
3138 +       }
3139 +
3140 +       err = -EDOM;
3141 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3142 +               pr_err("bad index %d\n", add->bindex);
3143 +               goto out;
3144 +       }
3145 +
3146 +       inode = d_inode(add->path.dentry);
3147 +       err = -ENOENT;
3148 +       if (unlikely(!inode->i_nlink)) {
3149 +               pr_err("no existence %s\n", add->pathname);
3150 +               goto out;
3151 +       }
3152 +
3153 +       err = -EINVAL;
3154 +       if (unlikely(inode->i_sb == sb)) {
3155 +               pr_err("%s must be outside\n", add->pathname);
3156 +               goto out;
3157 +       }
3158 +
3159 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3160 +               pr_err("unsupported filesystem, %s (%s)\n",
3161 +                      add->pathname, au_sbtype(inode->i_sb));
3162 +               goto out;
3163 +       }
3164 +
3165 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3166 +               pr_err("already stacked, %s (%s)\n",
3167 +                      add->pathname, au_sbtype(inode->i_sb));
3168 +               goto out;
3169 +       }
3170 +
3171 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3172 +       if (unlikely(err))
3173 +               goto out;
3174 +
3175 +       if (bbot < 0)
3176 +               return 0; /* success */
3177 +
3178 +       err = -EINVAL;
3179 +       for (bindex = 0; bindex <= bbot; bindex++)
3180 +               if (unlikely(test_overlap(sb, add->path.dentry,
3181 +                                         au_h_dptr(root, bindex)))) {
3182 +                       pr_err("%s is overlapped\n", add->pathname);
3183 +                       goto out;
3184 +               }
3185 +
3186 +       err = 0;
3187 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3188 +               h_dentry = au_h_dptr(root, 0);
3189 +               h_inode = d_inode(h_dentry);
3190 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3191 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3192 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3193 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3194 +                               add->pathname,
3195 +                               i_uid_read(inode), i_gid_read(inode),
3196 +                               (inode->i_mode & S_IALLUGO),
3197 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3198 +                               (h_inode->i_mode & S_IALLUGO));
3199 +       }
3200 +
3201 +out:
3202 +       return err;
3203 +}
3204 +
3205 +/*
3206 + * initialize or clean the whiteouts for an adding branch
3207 + */
3208 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3209 +                        int new_perm)
3210 +{
3211 +       int err, old_perm;
3212 +       aufs_bindex_t bindex;
3213 +       struct inode *h_inode;
3214 +       struct au_wbr *wbr;
3215 +       struct au_hinode *hdir;
3216 +       struct dentry *h_dentry;
3217 +
3218 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3219 +       if (unlikely(err))
3220 +               goto out;
3221 +
3222 +       wbr = br->br_wbr;
3223 +       old_perm = br->br_perm;
3224 +       br->br_perm = new_perm;
3225 +       hdir = NULL;
3226 +       h_inode = NULL;
3227 +       bindex = au_br_index(sb, br->br_id);
3228 +       if (0 <= bindex) {
3229 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3230 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3231 +       } else {
3232 +               h_dentry = au_br_dentry(br);
3233 +               h_inode = d_inode(h_dentry);
3234 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3235 +       }
3236 +       if (!wbr)
3237 +               err = au_wh_init(br, sb);
3238 +       else {
3239 +               wbr_wh_write_lock(wbr);
3240 +               err = au_wh_init(br, sb);
3241 +               wbr_wh_write_unlock(wbr);
3242 +       }
3243 +       if (hdir)
3244 +               au_hn_inode_unlock(hdir);
3245 +       else
3246 +               inode_unlock(h_inode);
3247 +       vfsub_mnt_drop_write(au_br_mnt(br));
3248 +       br->br_perm = old_perm;
3249 +
3250 +       if (!err && wbr && !au_br_writable(new_perm)) {
3251 +               au_kfree_rcu(wbr);
3252 +               br->br_wbr = NULL;
3253 +       }
3254 +
3255 +out:
3256 +       return err;
3257 +}
3258 +
3259 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3260 +                      int perm)
3261 +{
3262 +       int err;
3263 +       struct kstatfs kst;
3264 +       struct au_wbr *wbr;
3265 +
3266 +       wbr = br->br_wbr;
3267 +       au_rw_init(&wbr->wbr_wh_rwsem);
3268 +       atomic_set(&wbr->wbr_wh_running, 0);
3269 +
3270 +       /*
3271 +        * a limit for rmdir/rename a dir
3272 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3273 +        */
3274 +       err = vfs_statfs(&br->br_path, &kst);
3275 +       if (unlikely(err))
3276 +               goto out;
3277 +       err = -EINVAL;
3278 +       if (kst.f_namelen >= NAME_MAX)
3279 +               err = au_br_init_wh(sb, br, perm);
3280 +       else
3281 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3282 +                      au_br_dentry(br),
3283 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3284 +
3285 +out:
3286 +       return err;
3287 +}
3288 +
3289 +/* initialize a new branch */
3290 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3291 +                     struct au_opt_add *add)
3292 +{
3293 +       int err;
3294 +       struct au_branch *brbase;
3295 +       struct file *xf;
3296 +       struct inode *h_inode;
3297 +
3298 +       err = 0;
3299 +       br->br_perm = add->perm;
3300 +       br->br_path = add->path; /* set first, path_get() later */
3301 +       spin_lock_init(&br->br_dykey_lock);
3302 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3303 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3304 +       br->br_id = au_new_br_id(sb);
3305 +       AuDebugOn(br->br_id < 0);
3306 +
3307 +       /* always, regardless the given option */
3308 +       err = au_dr_br_init(sb, br, &add->path);
3309 +       if (unlikely(err))
3310 +               goto out_err;
3311 +
3312 +       if (au_br_writable(add->perm)) {
3313 +               err = au_wbr_init(br, sb, add->perm);
3314 +               if (unlikely(err))
3315 +                       goto out_err;
3316 +       }
3317 +
3318 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3319 +               brbase = au_sbr(sb, 0);
3320 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3321 +               AuDebugOn(!xf);
3322 +               h_inode = d_inode(add->path.dentry);
3323 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3324 +               if (unlikely(err)) {
3325 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3326 +                       goto out_err;
3327 +               }
3328 +       }
3329 +
3330 +       sysaufs_br_init(br);
3331 +       path_get(&br->br_path);
3332 +       goto out; /* success */
3333 +
3334 +out_err:
3335 +       memset(&br->br_path, 0, sizeof(br->br_path));
3336 +out:
3337 +       return err;
3338 +}
3339 +
3340 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3341 +                            struct au_branch *br, aufs_bindex_t bbot,
3342 +                            aufs_bindex_t amount)
3343 +{
3344 +       struct au_branch **brp;
3345 +
3346 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3347 +
3348 +       brp = sbinfo->si_branch + bindex;
3349 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3350 +       *brp = br;
3351 +       sbinfo->si_bbot++;
3352 +       if (unlikely(bbot < 0))
3353 +               sbinfo->si_bbot = 0;
3354 +}
3355 +
3356 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3357 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3358 +{
3359 +       struct au_hdentry *hdp;
3360 +
3361 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3362 +
3363 +       hdp = au_hdentry(dinfo, bindex);
3364 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3365 +       au_h_dentry_init(hdp);
3366 +       dinfo->di_bbot++;
3367 +       if (unlikely(bbot < 0))
3368 +               dinfo->di_btop = 0;
3369 +}
3370 +
3371 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3372 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3373 +{
3374 +       struct au_hinode *hip;
3375 +
3376 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3377 +
3378 +       hip = au_hinode(iinfo, bindex);
3379 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3380 +       au_hinode_init(hip);
3381 +       iinfo->ii_bbot++;
3382 +       if (unlikely(bbot < 0))
3383 +               iinfo->ii_btop = 0;
3384 +}
3385 +
3386 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3387 +                        aufs_bindex_t bindex)
3388 +{
3389 +       struct dentry *root, *h_dentry;
3390 +       struct inode *root_inode, *h_inode;
3391 +       aufs_bindex_t bbot, amount;
3392 +
3393 +       root = sb->s_root;
3394 +       root_inode = d_inode(root);
3395 +       bbot = au_sbbot(sb);
3396 +       amount = bbot + 1 - bindex;
3397 +       h_dentry = au_br_dentry(br);
3398 +       au_sbilist_lock();
3399 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3400 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3401 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3402 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3403 +       h_inode = d_inode(h_dentry);
3404 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3405 +       au_sbilist_unlock();
3406 +}
3407 +
3408 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3409 +{
3410 +       int err;
3411 +       aufs_bindex_t bbot, add_bindex;
3412 +       struct dentry *root, *h_dentry;
3413 +       struct inode *root_inode;
3414 +       struct au_branch *add_branch;
3415 +
3416 +       root = sb->s_root;
3417 +       root_inode = d_inode(root);
3418 +       IMustLock(root_inode);
3419 +       IiMustWriteLock(root_inode);
3420 +       err = test_add(sb, add, remount);
3421 +       if (unlikely(err < 0))
3422 +               goto out;
3423 +       if (err) {
3424 +               err = 0;
3425 +               goto out; /* success */
3426 +       }
3427 +
3428 +       bbot = au_sbbot(sb);
3429 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3430 +       err = PTR_ERR(add_branch);
3431 +       if (IS_ERR(add_branch))
3432 +               goto out;
3433 +
3434 +       err = au_br_init(add_branch, sb, add);
3435 +       if (unlikely(err)) {
3436 +               au_br_do_free(add_branch);
3437 +               goto out;
3438 +       }
3439 +
3440 +       add_bindex = add->bindex;
3441 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3442 +       au_br_do_add(sb, add_branch, add_bindex);
3443 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3444 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3445 +
3446 +       h_dentry = add->path.dentry;
3447 +       if (!add_bindex) {
3448 +               au_cpup_attr_all(root_inode, /*force*/1);
3449 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3450 +       } else
3451 +               au_add_nlink(root_inode, d_inode(h_dentry));
3452 +
3453 +out:
3454 +       return err;
3455 +}
3456 +
3457 +/* ---------------------------------------------------------------------- */
3458 +
3459 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3460 +                                      unsigned long long max __maybe_unused,
3461 +                                      void *arg)
3462 +{
3463 +       unsigned long long n;
3464 +       struct file **p, *f;
3465 +       struct hlist_bl_head *files;
3466 +       struct hlist_bl_node *pos;
3467 +       struct au_finfo *finfo;
3468 +
3469 +       n = 0;
3470 +       p = a;
3471 +       files = &au_sbi(sb)->si_files;
3472 +       hlist_bl_lock(files);
3473 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3474 +               f = finfo->fi_file;
3475 +               if (file_count(f)
3476 +                   && !special_file(file_inode(f)->i_mode)) {
3477 +                       get_file(f);
3478 +                       *p++ = f;
3479 +                       n++;
3480 +                       AuDebugOn(n > max);
3481 +               }
3482 +       }
3483 +       hlist_bl_unlock(files);
3484 +
3485 +       return n;
3486 +}
3487 +
3488 +static struct file **au_farray_alloc(struct super_block *sb,
3489 +                                    unsigned long long *max)
3490 +{
3491 +       struct au_sbinfo *sbi;
3492 +
3493 +       sbi = au_sbi(sb);
3494 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3495 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3496 +}
3497 +
3498 +static void au_farray_free(struct file **a, unsigned long long max)
3499 +{
3500 +       unsigned long long ull;
3501 +
3502 +       for (ull = 0; ull < max; ull++)
3503 +               if (a[ull])
3504 +                       fput(a[ull]);
3505 +       kvfree(a);
3506 +}
3507 +
3508 +/* ---------------------------------------------------------------------- */
3509 +
3510 +/*
3511 + * delete a branch
3512 + */
3513 +
3514 +/* to show the line number, do not make it inlined function */
3515 +#define AuVerbose(do_info, fmt, ...) do { \
3516 +       if (do_info) \
3517 +               pr_info(fmt, ##__VA_ARGS__); \
3518 +} while (0)
3519 +
3520 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3521 +                        aufs_bindex_t bbot)
3522 +{
3523 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3524 +}
3525 +
3526 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3527 +                        aufs_bindex_t bbot)
3528 +{
3529 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3530 +}
3531 +
3532 +/*
3533 + * test if the branch is deletable or not.
3534 + */
3535 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3536 +                           unsigned int sigen, const unsigned int verbose)
3537 +{
3538 +       int err, i, j, ndentry;
3539 +       aufs_bindex_t btop, bbot;
3540 +       struct au_dcsub_pages dpages;
3541 +       struct au_dpage *dpage;
3542 +       struct dentry *d;
3543 +
3544 +       err = au_dpages_init(&dpages, GFP_NOFS);
3545 +       if (unlikely(err))
3546 +               goto out;
3547 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3548 +       if (unlikely(err))
3549 +               goto out_dpages;
3550 +
3551 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3552 +               dpage = dpages.dpages + i;
3553 +               ndentry = dpage->ndentry;
3554 +               for (j = 0; !err && j < ndentry; j++) {
3555 +                       d = dpage->dentries[j];
3556 +                       AuDebugOn(au_dcount(d) <= 0);
3557 +                       if (!au_digen_test(d, sigen)) {
3558 +                               di_read_lock_child(d, AuLock_IR);
3559 +                               if (unlikely(au_dbrange_test(d))) {
3560 +                                       di_read_unlock(d, AuLock_IR);
3561 +                                       continue;
3562 +                               }
3563 +                       } else {
3564 +                               di_write_lock_child(d);
3565 +                               if (unlikely(au_dbrange_test(d))) {
3566 +                                       di_write_unlock(d);
3567 +                                       continue;
3568 +                               }
3569 +                               err = au_reval_dpath(d, sigen);
3570 +                               if (!err)
3571 +                                       di_downgrade_lock(d, AuLock_IR);
3572 +                               else {
3573 +                                       di_write_unlock(d);
3574 +                                       break;
3575 +                               }
3576 +                       }
3577 +
3578 +                       /* AuDbgDentry(d); */
3579 +                       btop = au_dbtop(d);
3580 +                       bbot = au_dbbot(d);
3581 +                       if (btop <= bindex
3582 +                           && bindex <= bbot
3583 +                           && au_h_dptr(d, bindex)
3584 +                           && au_test_dbusy(d, btop, bbot)) {
3585 +                               err = -EBUSY;
3586 +                               AuVerbose(verbose, "busy %pd\n", d);
3587 +                               AuDbgDentry(d);
3588 +                       }
3589 +                       di_read_unlock(d, AuLock_IR);
3590 +               }
3591 +       }
3592 +
3593 +out_dpages:
3594 +       au_dpages_free(&dpages);
3595 +out:
3596 +       return err;
3597 +}
3598 +
3599 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3600 +                          unsigned int sigen, const unsigned int verbose)
3601 +{
3602 +       int err;
3603 +       unsigned long long max, ull;
3604 +       struct inode *i, **array;
3605 +       aufs_bindex_t btop, bbot;
3606 +
3607 +       array = au_iarray_alloc(sb, &max);
3608 +       err = PTR_ERR(array);
3609 +       if (IS_ERR(array))
3610 +               goto out;
3611 +
3612 +       err = 0;
3613 +       AuDbg("b%d\n", bindex);
3614 +       for (ull = 0; !err && ull < max; ull++) {
3615 +               i = array[ull];
3616 +               if (unlikely(!i))
3617 +                       break;
3618 +               if (i->i_ino == AUFS_ROOT_INO)
3619 +                       continue;
3620 +
3621 +               /* AuDbgInode(i); */
3622 +               if (au_iigen(i, NULL) == sigen)
3623 +                       ii_read_lock_child(i);
3624 +               else {
3625 +                       ii_write_lock_child(i);
3626 +                       err = au_refresh_hinode_self(i);
3627 +                       au_iigen_dec(i);
3628 +                       if (!err)
3629 +                               ii_downgrade_lock(i);
3630 +                       else {
3631 +                               ii_write_unlock(i);
3632 +                               break;
3633 +                       }
3634 +               }
3635 +
3636 +               btop = au_ibtop(i);
3637 +               bbot = au_ibbot(i);
3638 +               if (btop <= bindex
3639 +                   && bindex <= bbot
3640 +                   && au_h_iptr(i, bindex)
3641 +                   && au_test_ibusy(i, btop, bbot)) {
3642 +                       err = -EBUSY;
3643 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3644 +                       AuDbgInode(i);
3645 +               }
3646 +               ii_read_unlock(i);
3647 +       }
3648 +       au_iarray_free(array, max);
3649 +
3650 +out:
3651 +       return err;
3652 +}
3653 +
3654 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3655 +                             const unsigned int verbose)
3656 +{
3657 +       int err;
3658 +       unsigned int sigen;
3659 +
3660 +       sigen = au_sigen(root->d_sb);
3661 +       DiMustNoWaiters(root);
3662 +       IiMustNoWaiters(d_inode(root));
3663 +       di_write_unlock(root);
3664 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3665 +       if (!err)
3666 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3667 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3668 +
3669 +       return err;
3670 +}
3671 +
3672 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3673 +                        struct file **to_free, int *idx)
3674 +{
3675 +       int err;
3676 +       unsigned char matched, root;
3677 +       aufs_bindex_t bindex, bbot;
3678 +       struct au_fidir *fidir;
3679 +       struct au_hfile *hfile;
3680 +
3681 +       err = 0;
3682 +       root = IS_ROOT(file->f_path.dentry);
3683 +       if (root) {
3684 +               get_file(file);
3685 +               to_free[*idx] = file;
3686 +               (*idx)++;
3687 +               goto out;
3688 +       }
3689 +
3690 +       matched = 0;
3691 +       fidir = au_fi(file)->fi_hdir;
3692 +       AuDebugOn(!fidir);
3693 +       bbot = au_fbbot_dir(file);
3694 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3695 +               hfile = fidir->fd_hfile + bindex;
3696 +               if (!hfile->hf_file)
3697 +                       continue;
3698 +
3699 +               if (hfile->hf_br->br_id == br_id) {
3700 +                       matched = 1;
3701 +                       break;
3702 +               }
3703 +       }
3704 +       if (matched)
3705 +               err = -EBUSY;
3706 +
3707 +out:
3708 +       return err;
3709 +}
3710 +
3711 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3712 +                         struct file **to_free, int opened)
3713 +{
3714 +       int err, idx;
3715 +       unsigned long long ull, max;
3716 +       aufs_bindex_t btop;
3717 +       struct file *file, **array;
3718 +       struct dentry *root;
3719 +       struct au_hfile *hfile;
3720 +
3721 +       array = au_farray_alloc(sb, &max);
3722 +       err = PTR_ERR(array);
3723 +       if (IS_ERR(array))
3724 +               goto out;
3725 +
3726 +       err = 0;
3727 +       idx = 0;
3728 +       root = sb->s_root;
3729 +       di_write_unlock(root);
3730 +       for (ull = 0; ull < max; ull++) {
3731 +               file = array[ull];
3732 +               if (unlikely(!file))
3733 +                       break;
3734 +
3735 +               /* AuDbg("%pD\n", file); */
3736 +               fi_read_lock(file);
3737 +               btop = au_fbtop(file);
3738 +               if (!d_is_dir(file->f_path.dentry)) {
3739 +                       hfile = &au_fi(file)->fi_htop;
3740 +                       if (hfile->hf_br->br_id == br_id)
3741 +                               err = -EBUSY;
3742 +               } else
3743 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3744 +               fi_read_unlock(file);
3745 +               if (unlikely(err))
3746 +                       break;
3747 +       }
3748 +       di_write_lock_child(root);
3749 +       au_farray_free(array, max);
3750 +       AuDebugOn(idx > opened);
3751 +
3752 +out:
3753 +       return err;
3754 +}
3755 +
3756 +static void br_del_file(struct file **to_free, unsigned long long opened,
3757 +                       aufs_bindex_t br_id)
3758 +{
3759 +       unsigned long long ull;
3760 +       aufs_bindex_t bindex, btop, bbot, bfound;
3761 +       struct file *file;
3762 +       struct au_fidir *fidir;
3763 +       struct au_hfile *hfile;
3764 +
3765 +       for (ull = 0; ull < opened; ull++) {
3766 +               file = to_free[ull];
3767 +               if (unlikely(!file))
3768 +                       break;
3769 +
3770 +               /* AuDbg("%pD\n", file); */
3771 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3772 +               bfound = -1;
3773 +               fidir = au_fi(file)->fi_hdir;
3774 +               AuDebugOn(!fidir);
3775 +               fi_write_lock(file);
3776 +               btop = au_fbtop(file);
3777 +               bbot = au_fbbot_dir(file);
3778 +               for (bindex = btop; bindex <= bbot; bindex++) {
3779 +                       hfile = fidir->fd_hfile + bindex;
3780 +                       if (!hfile->hf_file)
3781 +                               continue;
3782 +
3783 +                       if (hfile->hf_br->br_id == br_id) {
3784 +                               bfound = bindex;
3785 +                               break;
3786 +                       }
3787 +               }
3788 +               AuDebugOn(bfound < 0);
3789 +               au_set_h_fptr(file, bfound, NULL);
3790 +               if (bfound == btop) {
3791 +                       for (btop++; btop <= bbot; btop++)
3792 +                               if (au_hf_dir(file, btop)) {
3793 +                                       au_set_fbtop(file, btop);
3794 +                                       break;
3795 +                               }
3796 +               }
3797 +               fi_write_unlock(file);
3798 +       }
3799 +}
3800 +
3801 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3802 +                            const aufs_bindex_t bindex,
3803 +                            const aufs_bindex_t bbot)
3804 +{
3805 +       struct au_branch **brp, **p;
3806 +
3807 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3808 +
3809 +       brp = sbinfo->si_branch + bindex;
3810 +       if (bindex < bbot)
3811 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3812 +       sbinfo->si_branch[0 + bbot] = NULL;
3813 +       sbinfo->si_bbot--;
3814 +
3815 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3816 +                       /*may_shrink*/1);
3817 +       if (p)
3818 +               sbinfo->si_branch = p;
3819 +       /* harmless error */
3820 +}
3821 +
3822 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3823 +                            const aufs_bindex_t bbot)
3824 +{
3825 +       struct au_hdentry *hdp, *p;
3826 +
3827 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3828 +
3829 +       hdp = au_hdentry(dinfo, bindex);
3830 +       if (bindex < bbot)
3831 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3832 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3833 +       dinfo->di_bbot--;
3834 +
3835 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3836 +                       /*may_shrink*/1);
3837 +       if (p)
3838 +               dinfo->di_hdentry = p;
3839 +       /* harmless error */
3840 +}
3841 +
3842 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3843 +                            const aufs_bindex_t bbot)
3844 +{
3845 +       struct au_hinode *hip, *p;
3846 +
3847 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3848 +
3849 +       hip = au_hinode(iinfo, bindex);
3850 +       if (bindex < bbot)
3851 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3852 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3853 +       iinfo->ii_bbot--;
3854 +
3855 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3856 +                       /*may_shrink*/1);
3857 +       if (p)
3858 +               iinfo->ii_hinode = p;
3859 +       /* harmless error */
3860 +}
3861 +
3862 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3863 +                        struct au_branch *br)
3864 +{
3865 +       aufs_bindex_t bbot;
3866 +       struct au_sbinfo *sbinfo;
3867 +       struct dentry *root, *h_root;
3868 +       struct inode *inode, *h_inode;
3869 +       struct au_hinode *hinode;
3870 +
3871 +       SiMustWriteLock(sb);
3872 +
3873 +       root = sb->s_root;
3874 +       inode = d_inode(root);
3875 +       sbinfo = au_sbi(sb);
3876 +       bbot = sbinfo->si_bbot;
3877 +
3878 +       h_root = au_h_dptr(root, bindex);
3879 +       hinode = au_hi(inode, bindex);
3880 +       h_inode = au_igrab(hinode->hi_inode);
3881 +       au_hiput(hinode);
3882 +
3883 +       au_sbilist_lock();
3884 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3885 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3886 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3887 +       au_sbilist_unlock();
3888 +
3889 +       /* ignore an error */
3890 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3891 +
3892 +       dput(h_root);
3893 +       iput(h_inode);
3894 +       au_br_do_free(br);
3895 +}
3896 +
3897 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3898 +                                  unsigned long long max, void *arg)
3899 +{
3900 +       return max;
3901 +}
3902 +
3903 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3904 +{
3905 +       int err, rerr, i;
3906 +       unsigned long long opened;
3907 +       unsigned int mnt_flags;
3908 +       aufs_bindex_t bindex, bbot, br_id;
3909 +       unsigned char do_wh, verbose;
3910 +       struct au_branch *br;
3911 +       struct au_wbr *wbr;
3912 +       struct dentry *root;
3913 +       struct file **to_free;
3914 +
3915 +       err = 0;
3916 +       opened = 0;
3917 +       to_free = NULL;
3918 +       root = sb->s_root;
3919 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3920 +       if (bindex < 0) {
3921 +               if (remount)
3922 +                       goto out; /* success */
3923 +               err = -ENOENT;
3924 +               pr_err("%s no such branch\n", del->pathname);
3925 +               goto out;
3926 +       }
3927 +       AuDbg("bindex b%d\n", bindex);
3928 +
3929 +       err = -EBUSY;
3930 +       mnt_flags = au_mntflags(sb);
3931 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3932 +       bbot = au_sbbot(sb);
3933 +       if (unlikely(!bbot)) {
3934 +               AuVerbose(verbose, "no more branches left\n");
3935 +               goto out;
3936 +       }
3937 +
3938 +       br = au_sbr(sb, bindex);
3939 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3940 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3941 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3942 +               goto out;
3943 +       }
3944 +
3945 +       br_id = br->br_id;
3946 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3947 +       if (unlikely(opened)) {
3948 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3949 +               err = PTR_ERR(to_free);
3950 +               if (IS_ERR(to_free))
3951 +                       goto out;
3952 +
3953 +               err = test_file_busy(sb, br_id, to_free, opened);
3954 +               if (unlikely(err)) {
3955 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3956 +                       goto out;
3957 +               }
3958 +       }
3959 +
3960 +       wbr = br->br_wbr;
3961 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3962 +       if (do_wh) {
3963 +               /* instead of WbrWhMustWriteLock(wbr) */
3964 +               SiMustWriteLock(sb);
3965 +               for (i = 0; i < AuBrWh_Last; i++) {
3966 +                       dput(wbr->wbr_wh[i]);
3967 +                       wbr->wbr_wh[i] = NULL;
3968 +               }
3969 +       }
3970 +
3971 +       err = test_children_busy(root, bindex, verbose);
3972 +       if (unlikely(err)) {
3973 +               if (do_wh)
3974 +                       goto out_wh;
3975 +               goto out;
3976 +       }
3977 +
3978 +       err = 0;
3979 +       if (to_free) {
3980 +               /*
3981 +                * now we confirmed the branch is deletable.
3982 +                * let's free the remaining opened dirs on the branch.
3983 +                */
3984 +               di_write_unlock(root);
3985 +               br_del_file(to_free, opened, br_id);
3986 +               di_write_lock_child(root);
3987 +       }
3988 +
3989 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3990 +       dbgaufs_xino_del(br);           /* remove one */
3991 +       au_br_do_del(sb, bindex, br);
3992 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3993 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3994 +
3995 +       if (!bindex) {
3996 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3997 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3998 +       } else
3999 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4000 +       if (au_opt_test(mnt_flags, PLINK))
4001 +               au_plink_half_refresh(sb, br_id);
4002 +
4003 +       goto out; /* success */
4004 +
4005 +out_wh:
4006 +       /* revert */
4007 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4008 +       if (rerr)
4009 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4010 +                       del->pathname, rerr);
4011 +out:
4012 +       if (to_free)
4013 +               au_farray_free(to_free, opened);
4014 +       return err;
4015 +}
4016 +
4017 +/* ---------------------------------------------------------------------- */
4018 +
4019 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4020 +{
4021 +       int err;
4022 +       aufs_bindex_t btop, bbot;
4023 +       struct aufs_ibusy ibusy;
4024 +       struct inode *inode, *h_inode;
4025 +
4026 +       err = -EPERM;
4027 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4028 +               goto out;
4029 +
4030 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4031 +       if (!err)
4032 +               /* VERIFY_WRITE */
4033 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4034 +       if (unlikely(err)) {
4035 +               err = -EFAULT;
4036 +               AuTraceErr(err);
4037 +               goto out;
4038 +       }
4039 +
4040 +       err = -EINVAL;
4041 +       si_read_lock(sb, AuLock_FLUSH);
4042 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4043 +               goto out_unlock;
4044 +
4045 +       err = 0;
4046 +       ibusy.h_ino = 0; /* invalid */
4047 +       inode = ilookup(sb, ibusy.ino);
4048 +       if (!inode
4049 +           || inode->i_ino == AUFS_ROOT_INO
4050 +           || au_is_bad_inode(inode))
4051 +               goto out_unlock;
4052 +
4053 +       ii_read_lock_child(inode);
4054 +       btop = au_ibtop(inode);
4055 +       bbot = au_ibbot(inode);
4056 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4057 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4058 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4059 +                       ibusy.h_ino = h_inode->i_ino;
4060 +       }
4061 +       ii_read_unlock(inode);
4062 +       iput(inode);
4063 +
4064 +out_unlock:
4065 +       si_read_unlock(sb);
4066 +       if (!err) {
4067 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4068 +               if (unlikely(err)) {
4069 +                       err = -EFAULT;
4070 +                       AuTraceErr(err);
4071 +               }
4072 +       }
4073 +out:
4074 +       return err;
4075 +}
4076 +
4077 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4078 +{
4079 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4080 +}
4081 +
4082 +#ifdef CONFIG_COMPAT
4083 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4084 +{
4085 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4086 +}
4087 +#endif
4088 +
4089 +/* ---------------------------------------------------------------------- */
4090 +
4091 +/*
4092 + * change a branch permission
4093 + */
4094 +
4095 +static void au_warn_ima(void)
4096 +{
4097 +#ifdef CONFIG_IMA
4098 +       /* since it doesn't support mark_files_ro() */
4099 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4100 +#endif
4101 +}
4102 +
4103 +static int do_need_sigen_inc(int a, int b)
4104 +{
4105 +       return au_br_whable(a) && !au_br_whable(b);
4106 +}
4107 +
4108 +static int need_sigen_inc(int old, int new)
4109 +{
4110 +       return do_need_sigen_inc(old, new)
4111 +               || do_need_sigen_inc(new, old);
4112 +}
4113 +
4114 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4115 +{
4116 +       int err, do_warn;
4117 +       unsigned int mnt_flags;
4118 +       unsigned long long ull, max;
4119 +       aufs_bindex_t br_id;
4120 +       unsigned char verbose, writer;
4121 +       struct file *file, *hf, **array;
4122 +       struct au_hfile *hfile;
4123 +       struct inode *h_inode;
4124 +
4125 +       mnt_flags = au_mntflags(sb);
4126 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4127 +
4128 +       array = au_farray_alloc(sb, &max);
4129 +       err = PTR_ERR(array);
4130 +       if (IS_ERR(array))
4131 +               goto out;
4132 +
4133 +       do_warn = 0;
4134 +       br_id = au_sbr_id(sb, bindex);
4135 +       for (ull = 0; ull < max; ull++) {
4136 +               file = array[ull];
4137 +               if (unlikely(!file))
4138 +                       break;
4139 +
4140 +               /* AuDbg("%pD\n", file); */
4141 +               fi_read_lock(file);
4142 +               if (unlikely(au_test_mmapped(file))) {
4143 +                       err = -EBUSY;
4144 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4145 +                       AuDbgFile(file);
4146 +                       FiMustNoWaiters(file);
4147 +                       fi_read_unlock(file);
4148 +                       goto out_array;
4149 +               }
4150 +
4151 +               hfile = &au_fi(file)->fi_htop;
4152 +               hf = hfile->hf_file;
4153 +               if (!d_is_reg(file->f_path.dentry)
4154 +                   || !(file->f_mode & FMODE_WRITE)
4155 +                   || hfile->hf_br->br_id != br_id
4156 +                   || !(hf->f_mode & FMODE_WRITE))
4157 +                       array[ull] = NULL;
4158 +               else {
4159 +                       do_warn = 1;
4160 +                       get_file(file);
4161 +               }
4162 +
4163 +               FiMustNoWaiters(file);
4164 +               fi_read_unlock(file);
4165 +               fput(file);
4166 +       }
4167 +
4168 +       err = 0;
4169 +       if (do_warn)
4170 +               au_warn_ima();
4171 +
4172 +       for (ull = 0; ull < max; ull++) {
4173 +               file = array[ull];
4174 +               if (!file)
4175 +                       continue;
4176 +
4177 +               /* todo: already flushed? */
4178 +               /*
4179 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4180 +                * approach which resets f_mode and calls mnt_drop_write() and
4181 +                * file_release_write() for each file, because the branch
4182 +                * attribute in aufs world is totally different from the native
4183 +                * fs rw/ro mode.
4184 +               */
4185 +               /* fi_read_lock(file); */
4186 +               hfile = &au_fi(file)->fi_htop;
4187 +               hf = hfile->hf_file;
4188 +               /* fi_read_unlock(file); */
4189 +               spin_lock(&hf->f_lock);
4190 +               writer = !!(hf->f_mode & FMODE_WRITER);
4191 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4192 +               spin_unlock(&hf->f_lock);
4193 +               if (writer) {
4194 +                       h_inode = file_inode(hf);
4195 +                       if (hf->f_mode & FMODE_READ)
4196 +                               i_readcount_inc(h_inode);
4197 +                       put_write_access(h_inode);
4198 +                       __mnt_drop_write(hf->f_path.mnt);
4199 +               }
4200 +       }
4201 +
4202 +out_array:
4203 +       au_farray_free(array, max);
4204 +out:
4205 +       AuTraceErr(err);
4206 +       return err;
4207 +}
4208 +
4209 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4210 +             int *do_refresh)
4211 +{
4212 +       int err, rerr;
4213 +       aufs_bindex_t bindex;
4214 +       struct dentry *root;
4215 +       struct au_branch *br;
4216 +       struct au_br_fhsm *bf;
4217 +
4218 +       root = sb->s_root;
4219 +       bindex = au_find_dbindex(root, mod->h_root);
4220 +       if (bindex < 0) {
4221 +               if (remount)
4222 +                       return 0; /* success */
4223 +               err = -ENOENT;
4224 +               pr_err("%s no such branch\n", mod->path);
4225 +               goto out;
4226 +       }
4227 +       AuDbg("bindex b%d\n", bindex);
4228 +
4229 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4230 +       if (unlikely(err))
4231 +               goto out;
4232 +
4233 +       br = au_sbr(sb, bindex);
4234 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4235 +       if (br->br_perm == mod->perm)
4236 +               return 0; /* success */
4237 +
4238 +       /* pre-allocate for non-fhsm --> fhsm */
4239 +       bf = NULL;
4240 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4241 +               err = au_fhsm_br_alloc(br);
4242 +               if (unlikely(err))
4243 +                       goto out;
4244 +               bf = br->br_fhsm;
4245 +               br->br_fhsm = NULL;
4246 +       }
4247 +
4248 +       if (au_br_writable(br->br_perm)) {
4249 +               /* remove whiteout base */
4250 +               err = au_br_init_wh(sb, br, mod->perm);
4251 +               if (unlikely(err))
4252 +                       goto out_bf;
4253 +
4254 +               if (!au_br_writable(mod->perm)) {
4255 +                       /* rw --> ro, file might be mmapped */
4256 +                       DiMustNoWaiters(root);
4257 +                       IiMustNoWaiters(d_inode(root));
4258 +                       di_write_unlock(root);
4259 +                       err = au_br_mod_files_ro(sb, bindex);
4260 +                       /* aufs_write_lock() calls ..._child() */
4261 +                       di_write_lock_child(root);
4262 +
4263 +                       if (unlikely(err)) {
4264 +                               rerr = -ENOMEM;
4265 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4266 +                                                    GFP_NOFS);
4267 +                               if (br->br_wbr)
4268 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4269 +                               if (unlikely(rerr)) {
4270 +                                       AuIOErr("nested error %d (%d)\n",
4271 +                                               rerr, err);
4272 +                                       br->br_perm = mod->perm;
4273 +                               }
4274 +                       }
4275 +               }
4276 +       } else if (au_br_writable(mod->perm)) {
4277 +               /* ro --> rw */
4278 +               err = -ENOMEM;
4279 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4280 +               if (br->br_wbr) {
4281 +                       err = au_wbr_init(br, sb, mod->perm);
4282 +                       if (unlikely(err)) {
4283 +                               au_kfree_rcu(br->br_wbr);
4284 +                               br->br_wbr = NULL;
4285 +                       }
4286 +               }
4287 +       }
4288 +       if (unlikely(err))
4289 +               goto out_bf;
4290 +
4291 +       if (au_br_fhsm(br->br_perm)) {
4292 +               if (!au_br_fhsm(mod->perm)) {
4293 +                       /* fhsm --> non-fhsm */
4294 +                       au_br_fhsm_fin(br->br_fhsm);
4295 +                       au_kfree_rcu(br->br_fhsm);
4296 +                       br->br_fhsm = NULL;
4297 +               }
4298 +       } else if (au_br_fhsm(mod->perm))
4299 +               /* non-fhsm --> fhsm */
4300 +               br->br_fhsm = bf;
4301 +
4302 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4303 +       br->br_perm = mod->perm;
4304 +       goto out; /* success */
4305 +
4306 +out_bf:
4307 +       au_kfree_try_rcu(bf);
4308 +out:
4309 +       AuTraceErr(err);
4310 +       return err;
4311 +}
4312 +
4313 +/* ---------------------------------------------------------------------- */
4314 +
4315 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4316 +{
4317 +       int err;
4318 +       struct kstatfs kstfs;
4319 +
4320 +       err = vfs_statfs(&br->br_path, &kstfs);
4321 +       if (!err) {
4322 +               stfs->f_blocks = kstfs.f_blocks;
4323 +               stfs->f_bavail = kstfs.f_bavail;
4324 +               stfs->f_files = kstfs.f_files;
4325 +               stfs->f_ffree = kstfs.f_ffree;
4326 +       }
4327 +
4328 +       return err;
4329 +}
4330 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4331 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4332 +++ linux/fs/aufs/branch.h      2021-11-01 23:48:34.203025928 +0100
4333 @@ -0,0 +1,375 @@
4334 +/* SPDX-License-Identifier: GPL-2.0 */
4335 +/*
4336 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4337 + *
4338 + * This program, aufs is free software; you can redistribute it and/or modify
4339 + * it under the terms of the GNU General Public License as published by
4340 + * the Free Software Foundation; either version 2 of the License, or
4341 + * (at your option) any later version.
4342 + *
4343 + * This program is distributed in the hope that it will be useful,
4344 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4345 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4346 + * GNU General Public License for more details.
4347 + *
4348 + * You should have received a copy of the GNU General Public License
4349 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4350 + */
4351 +
4352 +/*
4353 + * branch filesystems and xino for them
4354 + */
4355 +
4356 +#ifndef __AUFS_BRANCH_H__
4357 +#define __AUFS_BRANCH_H__
4358 +
4359 +#ifdef __KERNEL__
4360 +
4361 +#include <linux/mount.h>
4362 +#include "dirren.h"
4363 +#include "dynop.h"
4364 +#include "lcnt.h"
4365 +#include "rwsem.h"
4366 +#include "super.h"
4367 +
4368 +/* ---------------------------------------------------------------------- */
4369 +
4370 +/* a xino file */
4371 +struct au_xino {
4372 +       struct file             **xi_file;
4373 +       unsigned int            xi_nfile;
4374 +
4375 +       struct {
4376 +               spinlock_t              spin;
4377 +               ino_t                   *array;
4378 +               int                     total;
4379 +               /* reserved for future use */
4380 +               /* unsigned long        *bitmap; */
4381 +               wait_queue_head_t       wqh;
4382 +       } xi_nondir;
4383 +
4384 +       struct mutex            xi_mtx; /* protects xi_file array */
4385 +       struct hlist_bl_head    xi_writing;
4386 +
4387 +       atomic_t                xi_truncating;
4388 +
4389 +       struct kref             xi_kref;
4390 +};
4391 +
4392 +/* File-based Hierarchical Storage Management */
4393 +struct au_br_fhsm {
4394 +#ifdef CONFIG_AUFS_FHSM
4395 +       struct mutex            bf_lock;
4396 +       unsigned long           bf_jiffy;
4397 +       struct aufs_stfs        bf_stfs;
4398 +       int                     bf_readable;
4399 +#endif
4400 +};
4401 +
4402 +/* members for writable branch only */
4403 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4404 +struct au_wbr {
4405 +       struct au_rwsem         wbr_wh_rwsem;
4406 +       struct dentry           *wbr_wh[AuBrWh_Last];
4407 +       atomic_t                wbr_wh_running;
4408 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4409 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4410 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4411 +
4412 +       /* mfs mode */
4413 +       unsigned long long      wbr_bytes;
4414 +};
4415 +
4416 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4417 +#define AuBrDynOp (AuDyLast * 4)
4418 +
4419 +#ifdef CONFIG_AUFS_HFSNOTIFY
4420 +/* support for asynchronous destruction */
4421 +struct au_br_hfsnotify {
4422 +       struct fsnotify_group   *hfsn_group;
4423 +};
4424 +#endif
4425 +
4426 +/* sysfs entries */
4427 +struct au_brsysfs {
4428 +       char                    name[16];
4429 +       struct attribute        attr;
4430 +};
4431 +
4432 +enum {
4433 +       AuBrSysfs_BR,
4434 +       AuBrSysfs_BRID,
4435 +       AuBrSysfs_Last
4436 +};
4437 +
4438 +/* protected by superblock rwsem */
4439 +struct au_branch {
4440 +       struct au_xino          *br_xino;
4441 +
4442 +       aufs_bindex_t           br_id;
4443 +
4444 +       int                     br_perm;
4445 +       struct path             br_path;
4446 +       spinlock_t              br_dykey_lock;
4447 +       struct au_dykey         *br_dykey[AuBrDynOp];
4448 +       au_lcnt_t               br_nfiles;      /* opened files */
4449 +       au_lcnt_t               br_count;       /* in-use for other */
4450 +
4451 +       struct au_wbr           *br_wbr;
4452 +       struct au_br_fhsm       *br_fhsm;
4453 +
4454 +#ifdef CONFIG_AUFS_HFSNOTIFY
4455 +       struct au_br_hfsnotify  *br_hfsn;
4456 +#endif
4457 +
4458 +#ifdef CONFIG_SYSFS
4459 +       /* entries under sysfs per mount-point */
4460 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4461 +#endif
4462 +
4463 +#ifdef CONFIG_DEBUG_FS
4464 +       struct dentry            *br_dbgaufs; /* xino */
4465 +#endif
4466 +
4467 +       struct au_dr_br         br_dirren;
4468 +};
4469 +
4470 +/* ---------------------------------------------------------------------- */
4471 +
4472 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4473 +{
4474 +       return br->br_path.mnt;
4475 +}
4476 +
4477 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4478 +{
4479 +       return br->br_path.dentry;
4480 +}
4481 +
4482 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4483 +{
4484 +       return mnt_user_ns(br->br_path.mnt);
4485 +}
4486 +
4487 +static inline struct super_block *au_br_sb(struct au_branch *br)
4488 +{
4489 +       return au_br_mnt(br)->mnt_sb;
4490 +}
4491 +
4492 +static inline int au_br_rdonly(struct au_branch *br)
4493 +{
4494 +       return (sb_rdonly(au_br_sb(br))
4495 +               || !au_br_writable(br->br_perm))
4496 +               ? -EROFS : 0;
4497 +}
4498 +
4499 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4500 +{
4501 +#ifdef CONFIG_AUFS_HNOTIFY
4502 +       return !(brperm & AuBrPerm_RR);
4503 +#else
4504 +       return 0;
4505 +#endif
4506 +}
4507 +
4508 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4509 +{
4510 +       int err, exec_flag;
4511 +
4512 +       err = 0;
4513 +       exec_flag = oflag & __FMODE_EXEC;
4514 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4515 +               err = -EACCES;
4516 +
4517 +       return err;
4518 +}
4519 +
4520 +static inline void au_xino_get(struct au_branch *br)
4521 +{
4522 +       struct au_xino *xi;
4523 +
4524 +       xi = br->br_xino;
4525 +       if (xi)
4526 +               kref_get(&xi->xi_kref);
4527 +}
4528 +
4529 +static inline int au_xino_count(struct au_branch *br)
4530 +{
4531 +       int v;
4532 +       struct au_xino *xi;
4533 +
4534 +       v = 0;
4535 +       xi = br->br_xino;
4536 +       if (xi)
4537 +               v = kref_read(&xi->xi_kref);
4538 +
4539 +       return v;
4540 +}
4541 +
4542 +/* ---------------------------------------------------------------------- */
4543 +
4544 +/* branch.c */
4545 +struct au_sbinfo;
4546 +void au_br_free(struct au_sbinfo *sinfo);
4547 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4548 +struct au_opt_add;
4549 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4550 +struct au_opt_del;
4551 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4552 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4553 +#ifdef CONFIG_COMPAT
4554 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4555 +#endif
4556 +struct au_opt_mod;
4557 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4558 +             int *do_refresh);
4559 +struct aufs_stfs;
4560 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4561 +
4562 +/* xino.c */
4563 +static const loff_t au_loff_max = LLONG_MAX;
4564 +
4565 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4566 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4567 +                           int wbrtop);
4568 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4569 +                            struct file *copy_src);
4570 +struct au_xi_new {
4571 +       struct au_xino *xi;     /* switch between xino and xigen */
4572 +       int idx;
4573 +       struct path *base;
4574 +       struct file *copy_src;
4575 +};
4576 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4577 +
4578 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4579 +                ino_t *ino);
4580 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4581 +                 ino_t ino);
4582 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4583 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4584 +
4585 +int au_xib_trunc(struct super_block *sb);
4586 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4587 +
4588 +struct au_xino *au_xino_alloc(unsigned int nfile);
4589 +int au_xino_put(struct au_branch *br);
4590 +struct file *au_xino_file1(struct au_xino *xi);
4591 +
4592 +struct au_opt_xino;
4593 +void au_xino_clr(struct super_block *sb);
4594 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4595 +struct file *au_xino_def(struct super_block *sb);
4596 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4597 +                   struct path *base);
4598 +
4599 +ino_t au_xino_new_ino(struct super_block *sb);
4600 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4601 +
4602 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4603 +                      ino_t h_ino, int idx);
4604 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4605 +                     int *idx);
4606 +
4607 +int au_xino_path(struct seq_file *seq, struct file *file);
4608 +
4609 +/* ---------------------------------------------------------------------- */
4610 +
4611 +/* @idx is signed to accept -1 meaning the first file */
4612 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4613 +{
4614 +       struct file *file;
4615 +
4616 +       file = NULL;
4617 +       if (!xi)
4618 +               goto out;
4619 +
4620 +       if (idx >= 0) {
4621 +               if (idx < xi->xi_nfile)
4622 +                       file = xi->xi_file[idx];
4623 +       } else
4624 +               file = au_xino_file1(xi);
4625 +
4626 +out:
4627 +       return file;
4628 +}
4629 +
4630 +/* ---------------------------------------------------------------------- */
4631 +
4632 +/* Superblock to branch */
4633 +static inline
4634 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4635 +{
4636 +       return au_sbr(sb, bindex)->br_id;
4637 +}
4638 +
4639 +static inline
4640 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4641 +{
4642 +       return au_br_mnt(au_sbr(sb, bindex));
4643 +}
4644 +
4645 +static inline
4646 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4647 +{
4648 +       return au_br_userns(au_sbr(sb, bindex));
4649 +}
4650 +
4651 +static inline
4652 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4653 +{
4654 +       return au_br_sb(au_sbr(sb, bindex));
4655 +}
4656 +
4657 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4658 +{
4659 +       return au_sbr(sb, bindex)->br_perm;
4660 +}
4661 +
4662 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4663 +{
4664 +       return au_br_whable(au_sbr_perm(sb, bindex));
4665 +}
4666 +
4667 +/* ---------------------------------------------------------------------- */
4668 +
4669 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4670 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4671 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4672 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4673 +/*
4674 +#define wbr_wh_read_trylock_nested(wbr) \
4675 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4676 +#define wbr_wh_write_trylock_nested(wbr) \
4677 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4678 +*/
4679 +
4680 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4681 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4682 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4683 +
4684 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4685 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4686 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4687 +
4688 +/* ---------------------------------------------------------------------- */
4689 +
4690 +#ifdef CONFIG_AUFS_FHSM
4691 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4692 +{
4693 +       mutex_init(&brfhsm->bf_lock);
4694 +       brfhsm->bf_jiffy = 0;
4695 +       brfhsm->bf_readable = 0;
4696 +}
4697 +
4698 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4699 +{
4700 +       mutex_destroy(&brfhsm->bf_lock);
4701 +}
4702 +#else
4703 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4704 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4705 +#endif
4706 +
4707 +#endif /* __KERNEL__ */
4708 +#endif /* __AUFS_BRANCH_H__ */
4709 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4710 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4711 +++ linux/fs/aufs/conf.mk       2021-11-01 23:48:34.203025928 +0100
4712 @@ -0,0 +1,40 @@
4713 +# SPDX-License-Identifier: GPL-2.0
4714 +
4715 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4716 +
4717 +define AuConf
4718 +ifdef ${1}
4719 +AuConfStr += ${1}=${${1}}
4720 +endif
4721 +endef
4722 +
4723 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4724 +       SBILIST \
4725 +       HNOTIFY HFSNOTIFY \
4726 +       EXPORT INO_T_64 \
4727 +       XATTR \
4728 +       FHSM \
4729 +       RDU \
4730 +       DIRREN \
4731 +       SHWH \
4732 +       BR_RAMFS \
4733 +       BR_FUSE POLL \
4734 +       BR_HFSPLUS \
4735 +       BDEV_LOOP \
4736 +       DEBUG MAGIC_SYSRQ
4737 +$(foreach i, ${AuConfAll}, \
4738 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4739 +
4740 +AuConfName = ${obj}/conf.str
4741 +${AuConfName}.tmp: FORCE
4742 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4743 +${AuConfName}: ${AuConfName}.tmp
4744 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4745 +       echo '  GEN    ' $@; \
4746 +       cp -p $< $@; \
4747 +       }
4748 +FORCE:
4749 +clean-files += ${AuConfName} ${AuConfName}.tmp
4750 +${obj}/sysfs.o: ${AuConfName}
4751 +
4752 +-include ${srctree}/${src}/conf_priv.mk
4753 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4754 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4755 +++ linux/fs/aufs/cpup.c        2021-11-01 23:48:34.203025928 +0100
4756 @@ -0,0 +1,1459 @@
4757 +// SPDX-License-Identifier: GPL-2.0
4758 +/*
4759 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4760 + *
4761 + * This program, aufs is free software; you can redistribute it and/or modify
4762 + * it under the terms of the GNU General Public License as published by
4763 + * the Free Software Foundation; either version 2 of the License, or
4764 + * (at your option) any later version.
4765 + *
4766 + * This program is distributed in the hope that it will be useful,
4767 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4768 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4769 + * GNU General Public License for more details.
4770 + *
4771 + * You should have received a copy of the GNU General Public License
4772 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4773 + */
4774 +
4775 +/*
4776 + * copy-up functions, see wbr_policy.c for copy-down
4777 + */
4778 +
4779 +#include <linux/fs_stack.h>
4780 +#include <linux/mm.h>
4781 +#include <linux/task_work.h>
4782 +#include "aufs.h"
4783 +
4784 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4785 +{
4786 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4787 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4788 +
4789 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4790 +
4791 +       dst->i_flags |= iflags & ~mask;
4792 +       if (au_test_fs_notime(dst->i_sb))
4793 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4794 +}
4795 +
4796 +void au_cpup_attr_timesizes(struct inode *inode)
4797 +{
4798 +       struct inode *h_inode;
4799 +
4800 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4801 +       fsstack_copy_attr_times(inode, h_inode);
4802 +       fsstack_copy_inode_size(inode, h_inode);
4803 +}
4804 +
4805 +void au_cpup_attr_nlink(struct inode *inode, int force)
4806 +{
4807 +       struct inode *h_inode;
4808 +       struct super_block *sb;
4809 +       aufs_bindex_t bindex, bbot;
4810 +
4811 +       sb = inode->i_sb;
4812 +       bindex = au_ibtop(inode);
4813 +       h_inode = au_h_iptr(inode, bindex);
4814 +       if (!force
4815 +           && !S_ISDIR(h_inode->i_mode)
4816 +           && au_opt_test(au_mntflags(sb), PLINK)
4817 +           && au_plink_test(inode))
4818 +               return;
4819 +
4820 +       /*
4821 +        * 0 can happen in revalidating.
4822 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4823 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4824 +        * case.
4825 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4826 +        *       the incorrect link count.
4827 +        */
4828 +       set_nlink(inode, h_inode->i_nlink);
4829 +
4830 +       /*
4831 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4832 +        * it may includes whplink directory.
4833 +        */
4834 +       if (S_ISDIR(h_inode->i_mode)) {
4835 +               bbot = au_ibbot(inode);
4836 +               for (bindex++; bindex <= bbot; bindex++) {
4837 +                       h_inode = au_h_iptr(inode, bindex);
4838 +                       if (h_inode)
4839 +                               au_add_nlink(inode, h_inode);
4840 +               }
4841 +       }
4842 +}
4843 +
4844 +void au_cpup_attr_changeable(struct inode *inode)
4845 +{
4846 +       struct inode *h_inode;
4847 +
4848 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4849 +       inode->i_mode = h_inode->i_mode;
4850 +       inode->i_uid = h_inode->i_uid;
4851 +       inode->i_gid = h_inode->i_gid;
4852 +       au_cpup_attr_timesizes(inode);
4853 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4854 +}
4855 +
4856 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4857 +{
4858 +       struct au_iinfo *iinfo = au_ii(inode);
4859 +
4860 +       IiMustWriteLock(inode);
4861 +
4862 +       iinfo->ii_higen = h_inode->i_generation;
4863 +       iinfo->ii_hsb1 = h_inode->i_sb;
4864 +}
4865 +
4866 +void au_cpup_attr_all(struct inode *inode, int force)
4867 +{
4868 +       struct inode *h_inode;
4869 +
4870 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4871 +       au_cpup_attr_changeable(inode);
4872 +       if (inode->i_nlink > 0)
4873 +               au_cpup_attr_nlink(inode, force);
4874 +       inode->i_rdev = h_inode->i_rdev;
4875 +       inode->i_blkbits = h_inode->i_blkbits;
4876 +       au_cpup_igen(inode, h_inode);
4877 +}
4878 +
4879 +/* ---------------------------------------------------------------------- */
4880 +
4881 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4882 +
4883 +/* keep the timestamps of the parent dir when cpup */
4884 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4885 +                   struct path *h_path)
4886 +{
4887 +       struct inode *h_inode;
4888 +
4889 +       dt->dt_dentry = dentry;
4890 +       dt->dt_h_path = *h_path;
4891 +       h_inode = d_inode(h_path->dentry);
4892 +       dt->dt_atime = h_inode->i_atime;
4893 +       dt->dt_mtime = h_inode->i_mtime;
4894 +       /* smp_mb(); */
4895 +}
4896 +
4897 +void au_dtime_revert(struct au_dtime *dt)
4898 +{
4899 +       struct iattr attr;
4900 +       int err;
4901 +
4902 +       attr.ia_atime = dt->dt_atime;
4903 +       attr.ia_mtime = dt->dt_mtime;
4904 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4905 +               | ATTR_ATIME | ATTR_ATIME_SET;
4906 +
4907 +       /* no delegation since this is a directory */
4908 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4909 +       if (unlikely(err))
4910 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4911 +}
4912 +
4913 +/* ---------------------------------------------------------------------- */
4914 +
4915 +/* internal use only */
4916 +struct au_cpup_reg_attr {
4917 +       int             valid;
4918 +       struct kstat    st;
4919 +       unsigned int    iflags; /* inode->i_flags */
4920 +};
4921 +
4922 +static noinline_for_stack
4923 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4924 +              struct au_cpup_reg_attr *h_src_attr)
4925 +{
4926 +       int err, sbits, icex;
4927 +       unsigned int mnt_flags;
4928 +       unsigned char verbose;
4929 +       struct iattr ia;
4930 +       struct path h_path;
4931 +       struct inode *h_isrc, *h_idst;
4932 +       struct kstat *h_st;
4933 +       struct au_branch *br;
4934 +
4935 +       br = au_sbr(dst->d_sb, bindex);
4936 +       h_path.mnt = au_br_mnt(br);
4937 +       h_path.dentry = au_h_dptr(dst, bindex);
4938 +       h_idst = d_inode(h_path.dentry);
4939 +       h_isrc = d_inode(h_src->dentry);
4940 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4941 +               | ATTR_ATIME | ATTR_MTIME
4942 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4943 +       if (h_src_attr && h_src_attr->valid) {
4944 +               h_st = &h_src_attr->st;
4945 +               ia.ia_uid = h_st->uid;
4946 +               ia.ia_gid = h_st->gid;
4947 +               ia.ia_atime = h_st->atime;
4948 +               ia.ia_mtime = h_st->mtime;
4949 +               if (h_idst->i_mode != h_st->mode
4950 +                   && !S_ISLNK(h_idst->i_mode)) {
4951 +                       ia.ia_valid |= ATTR_MODE;
4952 +                       ia.ia_mode = h_st->mode;
4953 +               }
4954 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4955 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4956 +       } else {
4957 +               ia.ia_uid = h_isrc->i_uid;
4958 +               ia.ia_gid = h_isrc->i_gid;
4959 +               ia.ia_atime = h_isrc->i_atime;
4960 +               ia.ia_mtime = h_isrc->i_mtime;
4961 +               if (h_idst->i_mode != h_isrc->i_mode
4962 +                   && !S_ISLNK(h_idst->i_mode)) {
4963 +                       ia.ia_valid |= ATTR_MODE;
4964 +                       ia.ia_mode = h_isrc->i_mode;
4965 +               }
4966 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4967 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4968 +       }
4969 +       /* no delegation since it is just created */
4970 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4971 +
4972 +       /* is this nfs only? */
4973 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4974 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4975 +               ia.ia_mode = h_isrc->i_mode;
4976 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4977 +       }
4978 +
4979 +       icex = br->br_perm & AuBrAttr_ICEX;
4980 +       if (!err) {
4981 +               mnt_flags = au_mntflags(dst->d_sb);
4982 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4983 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4984 +       }
4985 +
4986 +       return err;
4987 +}
4988 +
4989 +/* ---------------------------------------------------------------------- */
4990 +
4991 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4992 +                          char *buf, unsigned long blksize)
4993 +{
4994 +       int err;
4995 +       size_t sz, rbytes, wbytes;
4996 +       unsigned char all_zero;
4997 +       char *p, *zp;
4998 +       struct inode *h_inode;
4999 +       /* reduce stack usage */
5000 +       struct iattr *ia;
5001 +
5002 +       zp = page_address(ZERO_PAGE(0));
5003 +       if (unlikely(!zp))
5004 +               return -ENOMEM; /* possible? */
5005 +
5006 +       err = 0;
5007 +       all_zero = 0;
5008 +       while (len) {
5009 +               AuDbg("len %lld\n", len);
5010 +               sz = blksize;
5011 +               if (len < blksize)
5012 +                       sz = len;
5013 +
5014 +               rbytes = 0;
5015 +               /* todo: signal_pending? */
5016 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5017 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5018 +                       err = rbytes;
5019 +               }
5020 +               if (unlikely(err < 0))
5021 +                       break;
5022 +
5023 +               all_zero = 0;
5024 +               if (len >= rbytes && rbytes == blksize)
5025 +                       all_zero = !memcmp(buf, zp, rbytes);
5026 +               if (!all_zero) {
5027 +                       wbytes = rbytes;
5028 +                       p = buf;
5029 +                       while (wbytes) {
5030 +                               size_t b;
5031 +
5032 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5033 +                               err = b;
5034 +                               /* todo: signal_pending? */
5035 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5036 +                                       continue;
5037 +                               if (unlikely(err < 0))
5038 +                                       break;
5039 +                               wbytes -= b;
5040 +                               p += b;
5041 +                       }
5042 +                       if (unlikely(err < 0))
5043 +                               break;
5044 +               } else {
5045 +                       loff_t res;
5046 +
5047 +                       AuLabel(hole);
5048 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5049 +                       err = res;
5050 +                       if (unlikely(res < 0))
5051 +                               break;
5052 +               }
5053 +               len -= rbytes;
5054 +               err = 0;
5055 +       }
5056 +
5057 +       /* the last block may be a hole */
5058 +       if (!err && all_zero) {
5059 +               AuLabel(last hole);
5060 +
5061 +               err = 1;
5062 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5063 +                       /* nfs requires this step to make last hole */
5064 +                       /* is this only nfs? */
5065 +                       do {
5066 +                               /* todo: signal_pending? */
5067 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5068 +                       } while (err == -EAGAIN || err == -EINTR);
5069 +                       if (err == 1)
5070 +                               dst->f_pos--;
5071 +               }
5072 +
5073 +               if (err == 1) {
5074 +                       ia = (void *)buf;
5075 +                       ia->ia_size = dst->f_pos;
5076 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5077 +                       ia->ia_file = dst;
5078 +                       h_inode = file_inode(dst);
5079 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5080 +                       /* no delegation since it is just created */
5081 +                       err = vfsub_notify_change(&dst->f_path, ia,
5082 +                                                 /*delegated*/NULL);
5083 +                       inode_unlock(h_inode);
5084 +               }
5085 +       }
5086 +
5087 +       return err;
5088 +}
5089 +
5090 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5091 +{
5092 +       int err;
5093 +       unsigned long blksize;
5094 +       unsigned char do_kfree;
5095 +       char *buf;
5096 +       struct super_block *h_sb;
5097 +
5098 +       err = -ENOMEM;
5099 +       h_sb = file_inode(dst)->i_sb;
5100 +       blksize = h_sb->s_blocksize;
5101 +       if (!blksize || PAGE_SIZE < blksize)
5102 +               blksize = PAGE_SIZE;
5103 +       AuDbg("blksize %lu\n", blksize);
5104 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5105 +       if (do_kfree)
5106 +               buf = kmalloc(blksize, GFP_NOFS);
5107 +       else
5108 +               buf = (void *)__get_free_page(GFP_NOFS);
5109 +       if (unlikely(!buf))
5110 +               goto out;
5111 +
5112 +       if (len > (1 << 22))
5113 +               AuDbg("copying a large file %lld\n", (long long)len);
5114 +
5115 +       src->f_pos = 0;
5116 +       dst->f_pos = 0;
5117 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5118 +       if (do_kfree) {
5119 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5120 +               au_kfree_do_rcu(buf);
5121 +       } else
5122 +               free_page((unsigned long)buf);
5123 +
5124 +out:
5125 +       return err;
5126 +}
5127 +
5128 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5129 +{
5130 +       int err;
5131 +       struct super_block *h_src_sb;
5132 +       struct inode *h_src_inode;
5133 +
5134 +       h_src_inode = file_inode(src);
5135 +       h_src_sb = h_src_inode->i_sb;
5136 +
5137 +       /* XFS acquires inode_lock */
5138 +       if (!au_test_xfs(h_src_sb))
5139 +               err = au_copy_file(dst, src, len);
5140 +       else {
5141 +               inode_unlock_shared(h_src_inode);
5142 +               err = au_copy_file(dst, src, len);
5143 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5144 +       }
5145 +
5146 +       return err;
5147 +}
5148 +
5149 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5150 +{
5151 +       int err;
5152 +       loff_t lo;
5153 +       struct super_block *h_src_sb;
5154 +       struct inode *h_src_inode;
5155 +
5156 +       h_src_inode = file_inode(src);
5157 +       h_src_sb = h_src_inode->i_sb;
5158 +       if (h_src_sb != file_inode(dst)->i_sb
5159 +           || !dst->f_op->remap_file_range) {
5160 +               err = au_do_copy(dst, src, len);
5161 +               goto out;
5162 +       }
5163 +
5164 +       if (!au_test_nfs(h_src_sb)) {
5165 +               inode_unlock_shared(h_src_inode);
5166 +               lo = vfsub_clone_file_range(src, dst, len);
5167 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5168 +       } else
5169 +               lo = vfsub_clone_file_range(src, dst, len);
5170 +       if (lo == len) {
5171 +               err = 0;
5172 +               goto out; /* success */
5173 +       } else if (lo >= 0)
5174 +               /* todo: possible? */
5175 +               /* paritially succeeded */
5176 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5177 +       else if (lo != -EOPNOTSUPP) {
5178 +               /* older XFS has a condition in cloning */
5179 +               err = lo;
5180 +               goto out;
5181 +       }
5182 +
5183 +       /* the backend fs on NFS may not support cloning */
5184 +       err = au_do_copy(dst, src, len);
5185 +
5186 +out:
5187 +       AuTraceErr(err);
5188 +       return err;
5189 +}
5190 +
5191 +/*
5192 + * to support a sparse file which is opened with O_APPEND,
5193 + * we need to close the file.
5194 + */
5195 +static int au_cp_regular(struct au_cp_generic *cpg)
5196 +{
5197 +       int err, i;
5198 +       enum { SRC, DST };
5199 +       struct {
5200 +               aufs_bindex_t bindex;
5201 +               unsigned int flags;
5202 +               struct dentry *dentry;
5203 +               int force_wr;
5204 +               struct file *file;
5205 +       } *f, file[] = {
5206 +               {
5207 +                       .bindex = cpg->bsrc,
5208 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5209 +               },
5210 +               {
5211 +                       .bindex = cpg->bdst,
5212 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5213 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5214 +               }
5215 +       };
5216 +       struct au_branch *br;
5217 +       struct super_block *sb, *h_src_sb;
5218 +       struct inode *h_src_inode;
5219 +       struct task_struct *tsk = current;
5220 +
5221 +       /* bsrc branch can be ro/rw. */
5222 +       sb = cpg->dentry->d_sb;
5223 +       f = file;
5224 +       for (i = 0; i < 2; i++, f++) {
5225 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5226 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5227 +                                   /*file*/NULL, f->force_wr);
5228 +               if (IS_ERR(f->file)) {
5229 +                       err = PTR_ERR(f->file);
5230 +                       if (i == SRC)
5231 +                               goto out;
5232 +                       else
5233 +                               goto out_src;
5234 +               }
5235 +       }
5236 +
5237 +       /* try stopping to update while we copyup */
5238 +       h_src_inode = d_inode(file[SRC].dentry);
5239 +       h_src_sb = h_src_inode->i_sb;
5240 +       if (!au_test_nfs(h_src_sb))
5241 +               IMustLock(h_src_inode);
5242 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5243 +
5244 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5245 +       if (tsk->flags & PF_KTHREAD)
5246 +               __fput_sync(file[DST].file);
5247 +       else {
5248 +               /* it happened actually */
5249 +               fput(file[DST].file);
5250 +               /*
5251 +                * too bad.
5252 +                * we have to call both since we don't know which place the file
5253 +                * was added to.
5254 +                */
5255 +               task_work_run();
5256 +               flush_delayed_fput();
5257 +       }
5258 +       br = au_sbr(sb, file[DST].bindex);
5259 +       au_lcnt_dec(&br->br_nfiles);
5260 +
5261 +out_src:
5262 +       fput(file[SRC].file);
5263 +       br = au_sbr(sb, file[SRC].bindex);
5264 +       au_lcnt_dec(&br->br_nfiles);
5265 +out:
5266 +       return err;
5267 +}
5268 +
5269 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5270 +                             struct au_cpup_reg_attr *h_src_attr)
5271 +{
5272 +       int err, rerr;
5273 +       loff_t l;
5274 +       struct path h_path;
5275 +       struct inode *h_src_inode, *h_dst_inode;
5276 +
5277 +       err = 0;
5278 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5279 +       l = i_size_read(h_src_inode);
5280 +       if (cpg->len == -1 || l < cpg->len)
5281 +               cpg->len = l;
5282 +       if (cpg->len) {
5283 +               /* try stopping to update while we are referencing */
5284 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5285 +               au_pin_hdir_unlock(cpg->pin);
5286 +
5287 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5288 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5289 +               h_src_attr->iflags = h_src_inode->i_flags;
5290 +               if (!au_test_nfs(h_src_inode->i_sb))
5291 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5292 +               else {
5293 +                       inode_unlock_shared(h_src_inode);
5294 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5295 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5296 +               }
5297 +               if (unlikely(err)) {
5298 +                       inode_unlock_shared(h_src_inode);
5299 +                       goto out;
5300 +               }
5301 +               h_src_attr->valid = 1;
5302 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5303 +                       err = au_cp_regular(cpg);
5304 +                       inode_unlock_shared(h_src_inode);
5305 +               } else {
5306 +                       inode_unlock_shared(h_src_inode);
5307 +                       err = au_cp_regular(cpg);
5308 +               }
5309 +               rerr = au_pin_hdir_relock(cpg->pin);
5310 +               if (!err && rerr)
5311 +                       err = rerr;
5312 +       }
5313 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5314 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5315 +               h_dst_inode = d_inode(h_path.dentry);
5316 +               spin_lock(&h_dst_inode->i_lock);
5317 +               h_dst_inode->i_state |= I_LINKABLE;
5318 +               spin_unlock(&h_dst_inode->i_lock);
5319 +       }
5320 +
5321 +out:
5322 +       return err;
5323 +}
5324 +
5325 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5326 +                             struct inode *h_dir)
5327 +{
5328 +       int err;
5329 +       DEFINE_DELAYED_CALL(done);
5330 +       const char *sym;
5331 +
5332 +       sym = vfs_get_link(h_src, &done);
5333 +       err = PTR_ERR(sym);
5334 +       if (IS_ERR(sym))
5335 +               goto out;
5336 +
5337 +       err = vfsub_symlink(h_dir, h_path, sym);
5338 +
5339 +out:
5340 +       do_delayed_call(&done);
5341 +       return err;
5342 +}
5343 +
5344 +/*
5345 + * regardless 'acl' option, reset all ACL.
5346 + * All ACL will be copied up later from the original entry on the lower branch.
5347 + */
5348 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5349 +{
5350 +       int err;
5351 +       struct dentry *h_dentry;
5352 +       struct inode *h_inode;
5353 +       struct user_namespace *h_userns;
5354 +
5355 +       h_userns = mnt_user_ns(h_path->mnt);
5356 +       h_dentry = h_path->dentry;
5357 +       h_inode = d_inode(h_dentry);
5358 +       /* forget_all_cached_acls(h_inode)); */
5359 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5360 +       AuTraceErr(err);
5361 +       if (err == -EOPNOTSUPP)
5362 +               err = 0;
5363 +       if (!err)
5364 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5365 +
5366 +       AuTraceErr(err);
5367 +       return err;
5368 +}
5369 +
5370 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5371 +                         struct inode *h_dir, struct path *h_path)
5372 +{
5373 +       int err;
5374 +       struct inode *dir, *inode;
5375 +       struct user_namespace *h_userns;
5376 +
5377 +       h_userns = mnt_user_ns(h_path->mnt);
5378 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5379 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5380 +       AuTraceErr(err);
5381 +       if (err == -EOPNOTSUPP)
5382 +               err = 0;
5383 +       if (unlikely(err))
5384 +               goto out;
5385 +
5386 +       /*
5387 +        * strange behaviour from the users view,
5388 +        * particularly setattr case
5389 +        */
5390 +       dir = d_inode(dst_parent);
5391 +       if (au_ibtop(dir) == cpg->bdst)
5392 +               au_cpup_attr_nlink(dir, /*force*/1);
5393 +       inode = d_inode(cpg->dentry);
5394 +       au_cpup_attr_nlink(inode, /*force*/1);
5395 +
5396 +out:
5397 +       return err;
5398 +}
5399 +
5400 +static noinline_for_stack
5401 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5402 +              struct au_cpup_reg_attr *h_src_attr)
5403 +{
5404 +       int err;
5405 +       umode_t mode;
5406 +       unsigned int mnt_flags;
5407 +       unsigned char isdir, isreg, force;
5408 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5409 +       struct au_dtime dt;
5410 +       struct path h_path;
5411 +       struct dentry *h_src, *h_dst, *h_parent;
5412 +       struct inode *h_inode, *h_dir;
5413 +       struct super_block *sb;
5414 +
5415 +       /* bsrc branch can be ro/rw. */
5416 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5417 +       h_inode = d_inode(h_src);
5418 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5419 +
5420 +       /* try stopping to be referenced while we are creating */
5421 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5422 +       if (au_ftest_cpup(cpg->flags, RENAME))
5423 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5424 +                                 AUFS_WH_PFX_LEN));
5425 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5426 +       h_dir = d_inode(h_parent);
5427 +       IMustLock(h_dir);
5428 +       AuDebugOn(h_parent != h_dst->d_parent);
5429 +
5430 +       sb = cpg->dentry->d_sb;
5431 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5432 +       if (do_dt) {
5433 +               h_path.dentry = h_parent;
5434 +               au_dtime_store(&dt, dst_parent, &h_path);
5435 +       }
5436 +       h_path.dentry = h_dst;
5437 +
5438 +       isreg = 0;
5439 +       isdir = 0;
5440 +       mode = h_inode->i_mode;
5441 +       switch (mode & S_IFMT) {
5442 +       case S_IFREG:
5443 +               isreg = 1;
5444 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5445 +               if (!err)
5446 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5447 +               break;
5448 +       case S_IFDIR:
5449 +               isdir = 1;
5450 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5451 +               if (!err)
5452 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5453 +               break;
5454 +       case S_IFLNK:
5455 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5456 +               break;
5457 +       case S_IFCHR:
5458 +       case S_IFBLK:
5459 +               AuDebugOn(!capable(CAP_MKNOD));
5460 +               fallthrough;
5461 +       case S_IFIFO:
5462 +       case S_IFSOCK:
5463 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5464 +               break;
5465 +       default:
5466 +               AuIOErr("Unknown inode type 0%o\n", mode);
5467 +               err = -EIO;
5468 +       }
5469 +       if (!err)
5470 +               err = au_reset_acl(h_dir, &h_path, mode);
5471 +
5472 +       mnt_flags = au_mntflags(sb);
5473 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5474 +           && !isdir
5475 +           && au_opt_test(mnt_flags, XINO)
5476 +           && (h_inode->i_nlink == 1
5477 +               || (h_inode->i_state & I_LINKABLE))
5478 +           /* todo: unnecessary? */
5479 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5480 +           && cpg->bdst < cpg->bsrc
5481 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5482 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5483 +               /* ignore this error */
5484 +
5485 +       if (!err) {
5486 +               force = 0;
5487 +               if (isreg) {
5488 +                       force = !!cpg->len;
5489 +                       if (cpg->len == -1)
5490 +                               force = !!i_size_read(h_inode);
5491 +               }
5492 +               au_fhsm_wrote(sb, cpg->bdst, force);
5493 +       }
5494 +
5495 +       if (do_dt)
5496 +               au_dtime_revert(&dt);
5497 +       return err;
5498 +}
5499 +
5500 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5501 +{
5502 +       int err;
5503 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5504 +       struct path h_ppath;
5505 +       struct inode *h_dir;
5506 +       aufs_bindex_t bdst;
5507 +
5508 +       dentry = cpg->dentry;
5509 +       bdst = cpg->bdst;
5510 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5511 +       h_dentry = au_h_dptr(dentry, bdst);
5512 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5513 +               dget(h_dentry);
5514 +               au_set_h_dptr(dentry, bdst, NULL);
5515 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5516 +               if (!err)
5517 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5518 +               au_set_h_dptr(dentry, bdst, h_dentry);
5519 +       } else {
5520 +               err = 0;
5521 +               parent = dget_parent(dentry);
5522 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5523 +               dput(parent);
5524 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5525 +               if (IS_ERR(h_path->dentry))
5526 +                       err = PTR_ERR(h_path->dentry);
5527 +       }
5528 +       if (unlikely(err))
5529 +               goto out;
5530 +
5531 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5532 +       h_dir = d_inode(h_parent);
5533 +       IMustLock(h_dir);
5534 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5535 +       /* no delegation since it is just created */
5536 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5537 +                          /*flags*/0);
5538 +       dput(h_path->dentry);
5539 +
5540 +out:
5541 +       return err;
5542 +}
5543 +
5544 +/*
5545 + * copyup the @dentry from @bsrc to @bdst.
5546 + * the caller must set the both of lower dentries.
5547 + * @len is for truncating when it is -1 copyup the entire file.
5548 + * in link/rename cases, @dst_parent may be different from the real one.
5549 + * basic->bsrc can be larger than basic->bdst.
5550 + * aufs doesn't touch the credential so
5551 + * security_inode_copy_up{,_xattr}() are unnecessary.
5552 + */
5553 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5554 +{
5555 +       int err, rerr;
5556 +       aufs_bindex_t old_ibtop;
5557 +       unsigned char isdir, plink;
5558 +       struct dentry *h_src, *h_dst, *h_parent;
5559 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5560 +       struct super_block *sb;
5561 +       struct au_branch *br;
5562 +       struct path h_src_path;
5563 +       /* to reduce stack size */
5564 +       struct {
5565 +               struct au_dtime dt;
5566 +               struct path h_path;
5567 +               struct au_cpup_reg_attr h_src_attr;
5568 +       } *a;
5569 +
5570 +       err = -ENOMEM;
5571 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5572 +       if (unlikely(!a))
5573 +               goto out;
5574 +       a->h_src_attr.valid = 0;
5575 +
5576 +       sb = cpg->dentry->d_sb;
5577 +       br = au_sbr(sb, cpg->bdst);
5578 +       a->h_path.mnt = au_br_mnt(br);
5579 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5580 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5581 +       h_dir = d_inode(h_parent);
5582 +       IMustLock(h_dir);
5583 +
5584 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5585 +       inode = d_inode(cpg->dentry);
5586 +
5587 +       if (!dst_parent)
5588 +               dst_parent = dget_parent(cpg->dentry);
5589 +       else
5590 +               dget(dst_parent);
5591 +
5592 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5593 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5594 +       if (dst_inode) {
5595 +               if (unlikely(!plink)) {
5596 +                       err = -EIO;
5597 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5598 +                               "but plink is disabled\n",
5599 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5600 +                       goto out_parent;
5601 +               }
5602 +
5603 +               if (dst_inode->i_nlink) {
5604 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5605 +
5606 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5607 +                       err = PTR_ERR(h_src);
5608 +                       if (IS_ERR(h_src))
5609 +                               goto out_parent;
5610 +                       if (unlikely(d_is_negative(h_src))) {
5611 +                               err = -EIO;
5612 +                               AuIOErr("i%lu exists on b%d "
5613 +                                       "but not pseudo-linked\n",
5614 +                                       inode->i_ino, cpg->bdst);
5615 +                               dput(h_src);
5616 +                               goto out_parent;
5617 +                       }
5618 +
5619 +                       if (do_dt) {
5620 +                               a->h_path.dentry = h_parent;
5621 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5622 +                       }
5623 +
5624 +                       a->h_path.dentry = h_dst;
5625 +                       delegated = NULL;
5626 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5627 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5628 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5629 +                       if (do_dt)
5630 +                               au_dtime_revert(&a->dt);
5631 +                       if (unlikely(err == -EWOULDBLOCK)) {
5632 +                               pr_warn("cannot retry for NFSv4 delegation"
5633 +                                       " for an internal link\n");
5634 +                               iput(delegated);
5635 +                       }
5636 +                       dput(h_src);
5637 +                       goto out_parent;
5638 +               } else
5639 +                       /* todo: cpup_wh_file? */
5640 +                       /* udba work */
5641 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5642 +       }
5643 +
5644 +       isdir = S_ISDIR(inode->i_mode);
5645 +       old_ibtop = au_ibtop(inode);
5646 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5647 +       if (unlikely(err))
5648 +               goto out_rev;
5649 +       dst_inode = d_inode(h_dst);
5650 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5651 +       /* todo: necessary? */
5652 +       /* au_pin_hdir_unlock(cpg->pin); */
5653 +
5654 +       h_src_path.dentry = h_src;
5655 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5656 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5657 +       if (unlikely(err)) {
5658 +               /* todo: necessary? */
5659 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5660 +               inode_unlock(dst_inode);
5661 +               goto out_rev;
5662 +       }
5663 +
5664 +       if (cpg->bdst < old_ibtop) {
5665 +               if (S_ISREG(inode->i_mode)) {
5666 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5667 +                       if (unlikely(err)) {
5668 +                               /* ignore an error */
5669 +                               /* au_pin_hdir_relock(cpg->pin); */
5670 +                               inode_unlock(dst_inode);
5671 +                               goto out_rev;
5672 +                       }
5673 +               }
5674 +               au_set_ibtop(inode, cpg->bdst);
5675 +       } else
5676 +               au_set_ibbot(inode, cpg->bdst);
5677 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5678 +                     au_hi_flags(inode, isdir));
5679 +
5680 +       /* todo: necessary? */
5681 +       /* err = au_pin_hdir_relock(cpg->pin); */
5682 +       inode_unlock(dst_inode);
5683 +       if (unlikely(err))
5684 +               goto out_rev;
5685 +
5686 +       src_inode = d_inode(h_src);
5687 +       if (!isdir
5688 +           && (src_inode->i_nlink > 1
5689 +               || src_inode->i_state & I_LINKABLE)
5690 +           && plink)
5691 +               au_plink_append(inode, cpg->bdst, h_dst);
5692 +
5693 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5694 +               a->h_path.dentry = h_dst;
5695 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5696 +       }
5697 +       if (!err)
5698 +               goto out_parent; /* success */
5699 +
5700 +       /* revert */
5701 +out_rev:
5702 +       a->h_path.dentry = h_parent;
5703 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5704 +       a->h_path.dentry = h_dst;
5705 +       rerr = 0;
5706 +       if (d_is_positive(h_dst)) {
5707 +               if (!isdir) {
5708 +                       /* no delegation since it is just created */
5709 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5710 +                                           /*delegated*/NULL, /*force*/0);
5711 +               } else
5712 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5713 +       }
5714 +       au_dtime_revert(&a->dt);
5715 +       if (rerr) {
5716 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5717 +               err = -EIO;
5718 +       }
5719 +out_parent:
5720 +       dput(dst_parent);
5721 +       au_kfree_rcu(a);
5722 +out:
5723 +       return err;
5724 +}
5725 +
5726 +#if 0 /* reserved */
5727 +struct au_cpup_single_args {
5728 +       int *errp;
5729 +       struct au_cp_generic *cpg;
5730 +       struct dentry *dst_parent;
5731 +};
5732 +
5733 +static void au_call_cpup_single(void *args)
5734 +{
5735 +       struct au_cpup_single_args *a = args;
5736 +
5737 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5738 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5739 +       au_pin_hdir_release(a->cpg->pin);
5740 +}
5741 +#endif
5742 +
5743 +/*
5744 + * prevent SIGXFSZ in copy-up.
5745 + * testing CAP_MKNOD is for generic fs,
5746 + * but CAP_FSETID is for xfs only, currently.
5747 + */
5748 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5749 +{
5750 +       int do_sio;
5751 +       struct super_block *sb;
5752 +       struct inode *h_dir;
5753 +
5754 +       do_sio = 0;
5755 +       sb = au_pinned_parent(pin)->d_sb;
5756 +       if (!au_wkq_test()
5757 +           && (!au_sbi(sb)->si_plink_maint_pid
5758 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5759 +               switch (mode & S_IFMT) {
5760 +               case S_IFREG:
5761 +                       /* no condition about RLIMIT_FSIZE and the file size */
5762 +                       do_sio = 1;
5763 +                       break;
5764 +               case S_IFCHR:
5765 +               case S_IFBLK:
5766 +                       do_sio = !capable(CAP_MKNOD);
5767 +                       break;
5768 +               }
5769 +               if (!do_sio)
5770 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5771 +                                 && !capable(CAP_FSETID));
5772 +               /* this workaround may be removed in the future */
5773 +               if (!do_sio) {
5774 +                       h_dir = au_pinned_h_dir(pin);
5775 +                       do_sio = h_dir->i_mode & S_ISVTX;
5776 +               }
5777 +       }
5778 +
5779 +       return do_sio;
5780 +}
5781 +
5782 +#if 0 /* reserved */
5783 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5784 +{
5785 +       int err, wkq_err;
5786 +       struct dentry *h_dentry;
5787 +
5788 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5789 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5790 +               err = au_cpup_single(cpg, dst_parent);
5791 +       else {
5792 +               struct au_cpup_single_args args = {
5793 +                       .errp           = &err,
5794 +                       .cpg            = cpg,
5795 +                       .dst_parent     = dst_parent
5796 +               };
5797 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5798 +               if (unlikely(wkq_err))
5799 +                       err = wkq_err;
5800 +       }
5801 +
5802 +       return err;
5803 +}
5804 +#endif
5805 +
5806 +/*
5807 + * copyup the @dentry from the first active lower branch to @bdst,
5808 + * using au_cpup_single().
5809 + */
5810 +static int au_cpup_simple(struct au_cp_generic *cpg)
5811 +{
5812 +       int err;
5813 +       unsigned int flags_orig;
5814 +       struct dentry *dentry;
5815 +
5816 +       AuDebugOn(cpg->bsrc < 0);
5817 +
5818 +       dentry = cpg->dentry;
5819 +       DiMustWriteLock(dentry);
5820 +
5821 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5822 +       if (!err) {
5823 +               flags_orig = cpg->flags;
5824 +               au_fset_cpup(cpg->flags, RENAME);
5825 +               err = au_cpup_single(cpg, NULL);
5826 +               cpg->flags = flags_orig;
5827 +               if (!err)
5828 +                       return 0; /* success */
5829 +
5830 +               /* revert */
5831 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5832 +               au_set_dbtop(dentry, cpg->bsrc);
5833 +       }
5834 +
5835 +       return err;
5836 +}
5837 +
5838 +struct au_cpup_simple_args {
5839 +       int *errp;
5840 +       struct au_cp_generic *cpg;
5841 +};
5842 +
5843 +static void au_call_cpup_simple(void *args)
5844 +{
5845 +       struct au_cpup_simple_args *a = args;
5846 +
5847 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5848 +       *a->errp = au_cpup_simple(a->cpg);
5849 +       au_pin_hdir_release(a->cpg->pin);
5850 +}
5851 +
5852 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5853 +{
5854 +       int err, wkq_err;
5855 +       struct dentry *dentry, *parent;
5856 +       struct file *h_file;
5857 +       struct inode *h_dir;
5858 +       struct user_namespace *h_userns;
5859 +
5860 +       dentry = cpg->dentry;
5861 +       h_file = NULL;
5862 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5863 +               AuDebugOn(cpg->bsrc < 0);
5864 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5865 +               err = PTR_ERR(h_file);
5866 +               if (IS_ERR(h_file))
5867 +                       goto out;
5868 +       }
5869 +
5870 +       parent = dget_parent(dentry);
5871 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5872 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5873 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5874 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5875 +               err = au_cpup_simple(cpg);
5876 +       else {
5877 +               struct au_cpup_simple_args args = {
5878 +                       .errp           = &err,
5879 +                       .cpg            = cpg
5880 +               };
5881 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5882 +               if (unlikely(wkq_err))
5883 +                       err = wkq_err;
5884 +       }
5885 +
5886 +       dput(parent);
5887 +       if (h_file)
5888 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5889 +
5890 +out:
5891 +       return err;
5892 +}
5893 +
5894 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5895 +{
5896 +       aufs_bindex_t bsrc, bbot;
5897 +       struct dentry *dentry, *h_dentry;
5898 +
5899 +       if (cpg->bsrc < 0) {
5900 +               dentry = cpg->dentry;
5901 +               bbot = au_dbbot(dentry);
5902 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5903 +                       h_dentry = au_h_dptr(dentry, bsrc);
5904 +                       if (h_dentry) {
5905 +                               AuDebugOn(d_is_negative(h_dentry));
5906 +                               break;
5907 +                       }
5908 +               }
5909 +               AuDebugOn(bsrc > bbot);
5910 +               cpg->bsrc = bsrc;
5911 +       }
5912 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5913 +       return au_do_sio_cpup_simple(cpg);
5914 +}
5915 +
5916 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5917 +{
5918 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5919 +       return au_do_sio_cpup_simple(cpg);
5920 +}
5921 +
5922 +/* ---------------------------------------------------------------------- */
5923 +
5924 +/*
5925 + * copyup the deleted file for writing.
5926 + */
5927 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5928 +                        struct file *file)
5929 +{
5930 +       int err;
5931 +       unsigned int flags_orig;
5932 +       aufs_bindex_t bsrc_orig;
5933 +       struct au_dinfo *dinfo;
5934 +       struct {
5935 +               struct au_hdentry *hd;
5936 +               struct dentry *h_dentry;
5937 +       } hdst, hsrc;
5938 +
5939 +       dinfo = au_di(cpg->dentry);
5940 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5941 +
5942 +       bsrc_orig = cpg->bsrc;
5943 +       cpg->bsrc = dinfo->di_btop;
5944 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5945 +       hdst.h_dentry = hdst.hd->hd_dentry;
5946 +       hdst.hd->hd_dentry = wh_dentry;
5947 +       dinfo->di_btop = cpg->bdst;
5948 +
5949 +       hsrc.h_dentry = NULL;
5950 +       if (file) {
5951 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5952 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5953 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5954 +       }
5955 +       flags_orig = cpg->flags;
5956 +       cpg->flags = !AuCpup_DTIME;
5957 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5958 +       cpg->flags = flags_orig;
5959 +       if (file) {
5960 +               if (!err)
5961 +                       err = au_reopen_nondir(file);
5962 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5963 +       }
5964 +       hdst.hd->hd_dentry = hdst.h_dentry;
5965 +       dinfo->di_btop = cpg->bsrc;
5966 +       cpg->bsrc = bsrc_orig;
5967 +
5968 +       return err;
5969 +}
5970 +
5971 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5972 +{
5973 +       int err;
5974 +       aufs_bindex_t bdst;
5975 +       struct au_dtime dt;
5976 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5977 +       struct au_branch *br;
5978 +       struct path h_path;
5979 +
5980 +       dentry = cpg->dentry;
5981 +       bdst = cpg->bdst;
5982 +       br = au_sbr(dentry->d_sb, bdst);
5983 +       parent = dget_parent(dentry);
5984 +       h_parent = au_h_dptr(parent, bdst);
5985 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5986 +       err = PTR_ERR(wh_dentry);
5987 +       if (IS_ERR(wh_dentry))
5988 +               goto out;
5989 +
5990 +       h_path.dentry = h_parent;
5991 +       h_path.mnt = au_br_mnt(br);
5992 +       au_dtime_store(&dt, parent, &h_path);
5993 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5994 +       if (unlikely(err))
5995 +               goto out_wh;
5996 +
5997 +       dget(wh_dentry);
5998 +       h_path.dentry = wh_dentry;
5999 +       if (!d_is_dir(wh_dentry)) {
6000 +               /* no delegation since it is just created */
6001 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6002 +                                  /*delegated*/NULL, /*force*/0);
6003 +       } else
6004 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6005 +       if (unlikely(err)) {
6006 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6007 +                       wh_dentry, err);
6008 +               err = -EIO;
6009 +       }
6010 +       au_dtime_revert(&dt);
6011 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6012 +
6013 +out_wh:
6014 +       dput(wh_dentry);
6015 +out:
6016 +       dput(parent);
6017 +       return err;
6018 +}
6019 +
6020 +struct au_cpup_wh_args {
6021 +       int *errp;
6022 +       struct au_cp_generic *cpg;
6023 +       struct file *file;
6024 +};
6025 +
6026 +static void au_call_cpup_wh(void *args)
6027 +{
6028 +       struct au_cpup_wh_args *a = args;
6029 +
6030 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6031 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6032 +       au_pin_hdir_release(a->cpg->pin);
6033 +}
6034 +
6035 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6036 +{
6037 +       int err, wkq_err;
6038 +       aufs_bindex_t bdst;
6039 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6040 +       struct inode *dir, *h_dir, *h_tmpdir;
6041 +       struct au_wbr *wbr;
6042 +       struct au_pin wh_pin, *pin_orig;
6043 +       struct user_namespace *h_userns;
6044 +
6045 +       dentry = cpg->dentry;
6046 +       bdst = cpg->bdst;
6047 +       parent = dget_parent(dentry);
6048 +       dir = d_inode(parent);
6049 +       h_orph = NULL;
6050 +       h_parent = NULL;
6051 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6052 +       h_tmpdir = h_dir;
6053 +       pin_orig = NULL;
6054 +       if (!h_dir->i_nlink) {
6055 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6056 +               h_orph = wbr->wbr_orph;
6057 +
6058 +               h_parent = dget(au_h_dptr(parent, bdst));
6059 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6060 +               h_tmpdir = d_inode(h_orph);
6061 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6062 +
6063 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6064 +               /* todo: au_h_open_pre()? */
6065 +
6066 +               pin_orig = cpg->pin;
6067 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6068 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6069 +               cpg->pin = &wh_pin;
6070 +       }
6071 +
6072 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6073 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6074 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6075 +               err = au_cpup_wh(cpg, file);
6076 +       else {
6077 +               struct au_cpup_wh_args args = {
6078 +                       .errp   = &err,
6079 +                       .cpg    = cpg,
6080 +                       .file   = file
6081 +               };
6082 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6083 +               if (unlikely(wkq_err))
6084 +                       err = wkq_err;
6085 +       }
6086 +
6087 +       if (h_orph) {
6088 +               inode_unlock(h_tmpdir);
6089 +               /* todo: au_h_open_post()? */
6090 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6091 +               au_set_h_dptr(parent, bdst, h_parent);
6092 +               AuDebugOn(!pin_orig);
6093 +               cpg->pin = pin_orig;
6094 +       }
6095 +       iput(h_dir);
6096 +       dput(parent);
6097 +
6098 +       return err;
6099 +}
6100 +
6101 +/* ---------------------------------------------------------------------- */
6102 +
6103 +/*
6104 + * generic routine for both of copy-up and copy-down.
6105 + */
6106 +/* cf. revalidate function in file.c */
6107 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6108 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6109 +                        struct au_pin *pin,
6110 +                        struct dentry *h_parent, void *arg),
6111 +              void *arg)
6112 +{
6113 +       int err;
6114 +       struct au_pin pin;
6115 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6116 +
6117 +       err = 0;
6118 +       parent = dget_parent(dentry);
6119 +       if (IS_ROOT(parent))
6120 +               goto out;
6121 +
6122 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6123 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6124 +
6125 +       /* do not use au_dpage */
6126 +       real_parent = parent;
6127 +       while (1) {
6128 +               dput(parent);
6129 +               parent = dget_parent(dentry);
6130 +               h_parent = au_h_dptr(parent, bdst);
6131 +               if (h_parent)
6132 +                       goto out; /* success */
6133 +
6134 +               /* find top dir which is necessary to cpup */
6135 +               do {
6136 +                       d = parent;
6137 +                       dput(parent);
6138 +                       parent = dget_parent(d);
6139 +                       di_read_lock_parent3(parent, !AuLock_IR);
6140 +                       h_parent = au_h_dptr(parent, bdst);
6141 +                       di_read_unlock(parent, !AuLock_IR);
6142 +               } while (!h_parent);
6143 +
6144 +               if (d != real_parent)
6145 +                       di_write_lock_child3(d);
6146 +
6147 +               /* somebody else might create while we were sleeping */
6148 +               h_dentry = au_h_dptr(d, bdst);
6149 +               if (!h_dentry || d_is_negative(h_dentry)) {
6150 +                       if (h_dentry)
6151 +                               au_update_dbtop(d);
6152 +
6153 +                       au_pin_set_dentry(&pin, d);
6154 +                       err = au_do_pin(&pin);
6155 +                       if (!err) {
6156 +                               err = cp(d, bdst, &pin, h_parent, arg);
6157 +                               au_unpin(&pin);
6158 +                       }
6159 +               }
6160 +
6161 +               if (d != real_parent)
6162 +                       di_write_unlock(d);
6163 +               if (unlikely(err))
6164 +                       break;
6165 +       }
6166 +
6167 +out:
6168 +       dput(parent);
6169 +       return err;
6170 +}
6171 +
6172 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6173 +                      struct au_pin *pin,
6174 +                      struct dentry *h_parent __maybe_unused,
6175 +                      void *arg __maybe_unused)
6176 +{
6177 +       struct au_cp_generic cpg = {
6178 +               .dentry = dentry,
6179 +               .bdst   = bdst,
6180 +               .bsrc   = -1,
6181 +               .len    = 0,
6182 +               .pin    = pin,
6183 +               .flags  = AuCpup_DTIME
6184 +       };
6185 +       return au_sio_cpup_simple(&cpg);
6186 +}
6187 +
6188 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6189 +{
6190 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6191 +}
6192 +
6193 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6194 +{
6195 +       int err;
6196 +       struct dentry *parent;
6197 +       struct inode *dir;
6198 +
6199 +       parent = dget_parent(dentry);
6200 +       dir = d_inode(parent);
6201 +       err = 0;
6202 +       if (au_h_iptr(dir, bdst))
6203 +               goto out;
6204 +
6205 +       di_read_unlock(parent, AuLock_IR);
6206 +       di_write_lock_parent(parent);
6207 +       /* someone else might change our inode while we were sleeping */
6208 +       if (!au_h_iptr(dir, bdst))
6209 +               err = au_cpup_dirs(dentry, bdst);
6210 +       di_downgrade_lock(parent, AuLock_IR);
6211 +
6212 +out:
6213 +       dput(parent);
6214 +       return err;
6215 +}
6216 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6217 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6218 +++ linux/fs/aufs/cpup.h        2021-11-01 23:48:34.203025928 +0100
6219 @@ -0,0 +1,100 @@
6220 +/* SPDX-License-Identifier: GPL-2.0 */
6221 +/*
6222 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6223 + *
6224 + * This program, aufs is free software; you can redistribute it and/or modify
6225 + * it under the terms of the GNU General Public License as published by
6226 + * the Free Software Foundation; either version 2 of the License, or
6227 + * (at your option) any later version.
6228 + *
6229 + * This program is distributed in the hope that it will be useful,
6230 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6231 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6232 + * GNU General Public License for more details.
6233 + *
6234 + * You should have received a copy of the GNU General Public License
6235 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6236 + */
6237 +
6238 +/*
6239 + * copy-up/down functions
6240 + */
6241 +
6242 +#ifndef __AUFS_CPUP_H__
6243 +#define __AUFS_CPUP_H__
6244 +
6245 +#ifdef __KERNEL__
6246 +
6247 +#include <linux/path.h>
6248 +
6249 +struct inode;
6250 +struct file;
6251 +struct au_pin;
6252 +
6253 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6254 +void au_cpup_attr_timesizes(struct inode *inode);
6255 +void au_cpup_attr_nlink(struct inode *inode, int force);
6256 +void au_cpup_attr_changeable(struct inode *inode);
6257 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6258 +void au_cpup_attr_all(struct inode *inode, int force);
6259 +
6260 +/* ---------------------------------------------------------------------- */
6261 +
6262 +struct au_cp_generic {
6263 +       struct dentry   *dentry;
6264 +       aufs_bindex_t   bdst, bsrc;
6265 +       loff_t          len;
6266 +       struct au_pin   *pin;
6267 +       unsigned int    flags;
6268 +};
6269 +
6270 +/* cpup flags */
6271 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6272 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6273 +                                                  for link(2) */
6274 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6275 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6276 +                                                  cpup */
6277 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6278 +                                                  existing entry */
6279 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6280 +                                                  the branch is marked as RO */
6281 +
6282 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6283 +#undef AuCpup_HOPEN
6284 +#define AuCpup_HOPEN           0
6285 +#endif
6286 +
6287 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6288 +#define au_fset_cpup(flags, name) \
6289 +       do { (flags) |= AuCpup_##name; } while (0)
6290 +#define au_fclr_cpup(flags, name) \
6291 +       do { (flags) &= ~AuCpup_##name; } while (0)
6292 +
6293 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6294 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6295 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6296 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6297 +
6298 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6299 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6300 +                        struct au_pin *pin,
6301 +                        struct dentry *h_parent, void *arg),
6302 +              void *arg);
6303 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6304 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6305 +
6306 +/* ---------------------------------------------------------------------- */
6307 +
6308 +/* keep timestamps when copyup */
6309 +struct au_dtime {
6310 +       struct dentry *dt_dentry;
6311 +       struct path dt_h_path;
6312 +       struct timespec64 dt_atime, dt_mtime;
6313 +};
6314 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6315 +                   struct path *h_path);
6316 +void au_dtime_revert(struct au_dtime *dt);
6317 +
6318 +#endif /* __KERNEL__ */
6319 +#endif /* __AUFS_CPUP_H__ */
6320 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6321 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6322 +++ linux/fs/aufs/dbgaufs.c     2021-11-01 23:48:34.203025928 +0100
6323 @@ -0,0 +1,526 @@
6324 +// SPDX-License-Identifier: GPL-2.0
6325 +/*
6326 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6327 + *
6328 + * This program, aufs is free software; you can redistribute it and/or modify
6329 + * it under the terms of the GNU General Public License as published by
6330 + * the Free Software Foundation; either version 2 of the License, or
6331 + * (at your option) any later version.
6332 + *
6333 + * This program is distributed in the hope that it will be useful,
6334 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6335 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6336 + * GNU General Public License for more details.
6337 + *
6338 + * You should have received a copy of the GNU General Public License
6339 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6340 + */
6341 +
6342 +/*
6343 + * debugfs interface
6344 + */
6345 +
6346 +#include <linux/debugfs.h>
6347 +#include "aufs.h"
6348 +
6349 +#ifndef CONFIG_SYSFS
6350 +#error DEBUG_FS depends upon SYSFS
6351 +#endif
6352 +
6353 +static struct dentry *dbgaufs;
6354 +static const mode_t dbgaufs_mode = 0444;
6355 +
6356 +/* 20 is max digits length of ulong 64 */
6357 +struct dbgaufs_arg {
6358 +       int n;
6359 +       char a[20 * 4];
6360 +};
6361 +
6362 +/*
6363 + * common function for all XINO files
6364 + */
6365 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6366 +                             struct file *file)
6367 +{
6368 +       void *p;
6369 +
6370 +       p = file->private_data;
6371 +       if (p) {
6372 +               /* this is struct dbgaufs_arg */
6373 +               AuDebugOn(!au_kfree_sz_test(p));
6374 +               au_kfree_do_rcu(p);
6375 +       }
6376 +       return 0;
6377 +}
6378 +
6379 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6380 +                          int cnt)
6381 +{
6382 +       int err;
6383 +       struct kstat st;
6384 +       struct dbgaufs_arg *p;
6385 +
6386 +       err = -ENOMEM;
6387 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6388 +       if (unlikely(!p))
6389 +               goto out;
6390 +
6391 +       err = 0;
6392 +       p->n = 0;
6393 +       file->private_data = p;
6394 +       if (!xf)
6395 +               goto out;
6396 +
6397 +       err = vfsub_getattr(&xf->f_path, &st);
6398 +       if (!err) {
6399 +               if (do_fcnt)
6400 +                       p->n = snprintf
6401 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6402 +                                cnt, st.blocks, st.blksize,
6403 +                                (long long)st.size);
6404 +               else
6405 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6406 +                                       st.blocks, st.blksize,
6407 +                                       (long long)st.size);
6408 +               AuDebugOn(p->n >= sizeof(p->a));
6409 +       } else {
6410 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6411 +               err = 0;
6412 +       }
6413 +
6414 +out:
6415 +       return err;
6416 +}
6417 +
6418 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6419 +                              size_t count, loff_t *ppos)
6420 +{
6421 +       struct dbgaufs_arg *p;
6422 +
6423 +       p = file->private_data;
6424 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6425 +}
6426 +
6427 +/* ---------------------------------------------------------------------- */
6428 +
6429 +struct dbgaufs_plink_arg {
6430 +       int n;
6431 +       char a[];
6432 +};
6433 +
6434 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6435 +                                struct file *file)
6436 +{
6437 +       free_page((unsigned long)file->private_data);
6438 +       return 0;
6439 +}
6440 +
6441 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6442 +{
6443 +       int err, i, limit;
6444 +       unsigned long n, sum;
6445 +       struct dbgaufs_plink_arg *p;
6446 +       struct au_sbinfo *sbinfo;
6447 +       struct super_block *sb;
6448 +       struct hlist_bl_head *hbl;
6449 +
6450 +       err = -ENOMEM;
6451 +       p = (void *)get_zeroed_page(GFP_NOFS);
6452 +       if (unlikely(!p))
6453 +               goto out;
6454 +
6455 +       err = -EFBIG;
6456 +       sbinfo = inode->i_private;
6457 +       sb = sbinfo->si_sb;
6458 +       si_noflush_read_lock(sb);
6459 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6460 +               limit = PAGE_SIZE - sizeof(p->n);
6461 +
6462 +               /* the number of buckets */
6463 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6464 +               p->n += n;
6465 +               limit -= n;
6466 +
6467 +               sum = 0;
6468 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6469 +                    i++, hbl++) {
6470 +                       n = au_hbl_count(hbl);
6471 +                       sum += n;
6472 +
6473 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6474 +                       p->n += n;
6475 +                       limit -= n;
6476 +                       if (unlikely(limit <= 0))
6477 +                               goto out_free;
6478 +               }
6479 +               p->a[p->n - 1] = '\n';
6480 +
6481 +               /* the sum of plinks */
6482 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6483 +               p->n += n;
6484 +               limit -= n;
6485 +               if (unlikely(limit <= 0))
6486 +                       goto out_free;
6487 +       } else {
6488 +#define str "1\n0\n0\n"
6489 +               p->n = sizeof(str) - 1;
6490 +               strcpy(p->a, str);
6491 +#undef str
6492 +       }
6493 +       si_read_unlock(sb);
6494 +
6495 +       err = 0;
6496 +       file->private_data = p;
6497 +       goto out; /* success */
6498 +
6499 +out_free:
6500 +       free_page((unsigned long)p);
6501 +out:
6502 +       return err;
6503 +}
6504 +
6505 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6506 +                                 size_t count, loff_t *ppos)
6507 +{
6508 +       struct dbgaufs_plink_arg *p;
6509 +
6510 +       p = file->private_data;
6511 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6512 +}
6513 +
6514 +static const struct file_operations dbgaufs_plink_fop = {
6515 +       .owner          = THIS_MODULE,
6516 +       .open           = dbgaufs_plink_open,
6517 +       .release        = dbgaufs_plink_release,
6518 +       .read           = dbgaufs_plink_read
6519 +};
6520 +
6521 +/* ---------------------------------------------------------------------- */
6522 +
6523 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6524 +{
6525 +       int err;
6526 +       struct au_sbinfo *sbinfo;
6527 +       struct super_block *sb;
6528 +
6529 +       sbinfo = inode->i_private;
6530 +       sb = sbinfo->si_sb;
6531 +       si_noflush_read_lock(sb);
6532 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6533 +       si_read_unlock(sb);
6534 +       return err;
6535 +}
6536 +
6537 +static const struct file_operations dbgaufs_xib_fop = {
6538 +       .owner          = THIS_MODULE,
6539 +       .open           = dbgaufs_xib_open,
6540 +       .release        = dbgaufs_xi_release,
6541 +       .read           = dbgaufs_xi_read
6542 +};
6543 +
6544 +/* ---------------------------------------------------------------------- */
6545 +
6546 +#define DbgaufsXi_PREFIX "xi"
6547 +
6548 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6549 +{
6550 +       int err, idx;
6551 +       long l;
6552 +       aufs_bindex_t bindex;
6553 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6554 +       struct au_sbinfo *sbinfo;
6555 +       struct super_block *sb;
6556 +       struct au_xino *xi;
6557 +       struct file *xf;
6558 +       struct qstr *name;
6559 +       struct au_branch *br;
6560 +
6561 +       err = -ENOENT;
6562 +       name = &file->f_path.dentry->d_name;
6563 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6564 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6565 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6566 +               goto out;
6567 +
6568 +       AuDebugOn(name->len >= sizeof(a));
6569 +       memcpy(a, name->name, name->len);
6570 +       a[name->len] = '\0';
6571 +       p = strchr(a, '-');
6572 +       if (p)
6573 +               *p = '\0';
6574 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6575 +       if (unlikely(err))
6576 +               goto out;
6577 +       bindex = l;
6578 +       idx = 0;
6579 +       if (p) {
6580 +               err = kstrtol(p + 1, 10, &l);
6581 +               if (unlikely(err))
6582 +                       goto out;
6583 +               idx = l;
6584 +       }
6585 +
6586 +       err = -ENOENT;
6587 +       sbinfo = inode->i_private;
6588 +       sb = sbinfo->si_sb;
6589 +       si_noflush_read_lock(sb);
6590 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6591 +               goto out_si;
6592 +       br = au_sbr(sb, bindex);
6593 +       xi = br->br_xino;
6594 +       if (unlikely(idx >= xi->xi_nfile))
6595 +               goto out_si;
6596 +       xf = au_xino_file(xi, idx);
6597 +       if (xf)
6598 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6599 +                                     au_xino_count(br));
6600 +
6601 +out_si:
6602 +       si_read_unlock(sb);
6603 +out:
6604 +       AuTraceErr(err);
6605 +       return err;
6606 +}
6607 +
6608 +static const struct file_operations dbgaufs_xino_fop = {
6609 +       .owner          = THIS_MODULE,
6610 +       .open           = dbgaufs_xino_open,
6611 +       .release        = dbgaufs_xi_release,
6612 +       .read           = dbgaufs_xi_read
6613 +};
6614 +
6615 +void dbgaufs_xino_del(struct au_branch *br)
6616 +{
6617 +       struct dentry *dbgaufs;
6618 +
6619 +       dbgaufs = br->br_dbgaufs;
6620 +       if (!dbgaufs)
6621 +               return;
6622 +
6623 +       br->br_dbgaufs = NULL;
6624 +       /* debugfs acquires the parent i_mutex */
6625 +       lockdep_off();
6626 +       debugfs_remove(dbgaufs);
6627 +       lockdep_on();
6628 +}
6629 +
6630 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6631 +{
6632 +       aufs_bindex_t bbot;
6633 +       struct au_branch *br;
6634 +
6635 +       if (!au_sbi(sb)->si_dbgaufs)
6636 +               return;
6637 +
6638 +       bbot = au_sbbot(sb);
6639 +       for (; bindex <= bbot; bindex++) {
6640 +               br = au_sbr(sb, bindex);
6641 +               dbgaufs_xino_del(br);
6642 +       }
6643 +}
6644 +
6645 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6646 +                             unsigned int idx, struct dentry *parent,
6647 +                             struct au_sbinfo *sbinfo)
6648 +{
6649 +       struct au_branch *br;
6650 +       struct dentry *d;
6651 +       /* "xi" bindex(5) "-" idx(2) NULL */
6652 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6653 +
6654 +       if (!idx)
6655 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6656 +       else
6657 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6658 +                        bindex, idx);
6659 +       br = au_sbr(sb, bindex);
6660 +       if (br->br_dbgaufs) {
6661 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6662 +
6663 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6664 +                       /* debugfs acquires the parent i_mutex */
6665 +                       lockdep_off();
6666 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6667 +                                          name);
6668 +                       lockdep_on();
6669 +                       if (unlikely(!d))
6670 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6671 +                                       parent, name);
6672 +               }
6673 +       } else {
6674 +               lockdep_off();
6675 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6676 +                                                    sbinfo, &dbgaufs_xino_fop);
6677 +               lockdep_on();
6678 +               if (unlikely(!br->br_dbgaufs))
6679 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6680 +                               parent, name);
6681 +       }
6682 +}
6683 +
6684 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6685 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6686 +{
6687 +       struct au_branch *br;
6688 +       struct au_xino *xi;
6689 +       unsigned int u;
6690 +
6691 +       br = au_sbr(sb, bindex);
6692 +       xi = br->br_xino;
6693 +       for (u = 0; u < xi->xi_nfile; u++)
6694 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6695 +}
6696 +
6697 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6698 +{
6699 +       struct au_sbinfo *sbinfo;
6700 +       struct dentry *parent;
6701 +       aufs_bindex_t bbot;
6702 +
6703 +       if (!au_opt_test(au_mntflags(sb), XINO))
6704 +               return;
6705 +
6706 +       sbinfo = au_sbi(sb);
6707 +       parent = sbinfo->si_dbgaufs;
6708 +       if (!parent)
6709 +               return;
6710 +
6711 +       bbot = au_sbbot(sb);
6712 +       if (topdown)
6713 +               for (; bindex <= bbot; bindex++)
6714 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6715 +       else
6716 +               for (; bbot >= bindex; bbot--)
6717 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6718 +}
6719 +
6720 +/* ---------------------------------------------------------------------- */
6721 +
6722 +#ifdef CONFIG_AUFS_EXPORT
6723 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6724 +{
6725 +       int err;
6726 +       struct au_sbinfo *sbinfo;
6727 +       struct super_block *sb;
6728 +
6729 +       sbinfo = inode->i_private;
6730 +       sb = sbinfo->si_sb;
6731 +       si_noflush_read_lock(sb);
6732 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6733 +       si_read_unlock(sb);
6734 +       return err;
6735 +}
6736 +
6737 +static const struct file_operations dbgaufs_xigen_fop = {
6738 +       .owner          = THIS_MODULE,
6739 +       .open           = dbgaufs_xigen_open,
6740 +       .release        = dbgaufs_xi_release,
6741 +       .read           = dbgaufs_xi_read
6742 +};
6743 +
6744 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6745 +{
6746 +       int err;
6747 +
6748 +       /*
6749 +        * This function is a dynamic '__init' function actually,
6750 +        * so the tiny check for si_rwsem is unnecessary.
6751 +        */
6752 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6753 +
6754 +       err = -EIO;
6755 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6756 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6757 +                &dbgaufs_xigen_fop);
6758 +       if (sbinfo->si_dbgaufs_xigen)
6759 +               err = 0;
6760 +
6761 +       return err;
6762 +}
6763 +#else
6764 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6765 +{
6766 +       return 0;
6767 +}
6768 +#endif /* CONFIG_AUFS_EXPORT */
6769 +
6770 +/* ---------------------------------------------------------------------- */
6771 +
6772 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6773 +{
6774 +       /*
6775 +        * This function is a dynamic '__fin' function actually,
6776 +        * so the tiny check for si_rwsem is unnecessary.
6777 +        */
6778 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6779 +
6780 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6781 +       sbinfo->si_dbgaufs = NULL;
6782 +}
6783 +
6784 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6785 +{
6786 +       int err;
6787 +       char name[SysaufsSiNameLen];
6788 +
6789 +       /*
6790 +        * This function is a dynamic '__init' function actually,
6791 +        * so the tiny check for si_rwsem is unnecessary.
6792 +        */
6793 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6794 +
6795 +       err = -ENOENT;
6796 +       if (!dbgaufs) {
6797 +               AuErr1("/debug/aufs is uninitialized\n");
6798 +               goto out;
6799 +       }
6800 +
6801 +       err = -EIO;
6802 +       sysaufs_name(sbinfo, name);
6803 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6804 +       if (unlikely(!sbinfo->si_dbgaufs))
6805 +               goto out;
6806 +
6807 +       /* regardless plink/noplink option */
6808 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6809 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6810 +                &dbgaufs_plink_fop);
6811 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6812 +               goto out_dir;
6813 +
6814 +       /* regardless xino/noxino option */
6815 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6816 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6817 +                &dbgaufs_xib_fop);
6818 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6819 +               goto out_dir;
6820 +
6821 +       err = dbgaufs_xigen_init(sbinfo);
6822 +       if (!err)
6823 +               goto out; /* success */
6824 +
6825 +out_dir:
6826 +       dbgaufs_si_fin(sbinfo);
6827 +out:
6828 +       if (unlikely(err))
6829 +               pr_err("debugfs/aufs failed\n");
6830 +       return err;
6831 +}
6832 +
6833 +/* ---------------------------------------------------------------------- */
6834 +
6835 +void dbgaufs_fin(void)
6836 +{
6837 +       debugfs_remove(dbgaufs);
6838 +}
6839 +
6840 +int __init dbgaufs_init(void)
6841 +{
6842 +       int err;
6843 +
6844 +       err = -EIO;
6845 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6846 +       if (dbgaufs)
6847 +               err = 0;
6848 +       return err;
6849 +}
6850 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6851 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6852 +++ linux/fs/aufs/dbgaufs.h     2021-11-01 23:48:34.203025928 +0100
6853 @@ -0,0 +1,53 @@
6854 +/* SPDX-License-Identifier: GPL-2.0 */
6855 +/*
6856 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6857 + *
6858 + * This program, aufs is free software; you can redistribute it and/or modify
6859 + * it under the terms of the GNU General Public License as published by
6860 + * the Free Software Foundation; either version 2 of the License, or
6861 + * (at your option) any later version.
6862 + *
6863 + * This program is distributed in the hope that it will be useful,
6864 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6865 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6866 + * GNU General Public License for more details.
6867 + *
6868 + * You should have received a copy of the GNU General Public License
6869 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6870 + */
6871 +
6872 +/*
6873 + * debugfs interface
6874 + */
6875 +
6876 +#ifndef __DBGAUFS_H__
6877 +#define __DBGAUFS_H__
6878 +
6879 +#ifdef __KERNEL__
6880 +
6881 +struct super_block;
6882 +struct au_sbinfo;
6883 +struct au_branch;
6884 +
6885 +#ifdef CONFIG_DEBUG_FS
6886 +/* dbgaufs.c */
6887 +void dbgaufs_xino_del(struct au_branch *br);
6888 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6889 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6890 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6891 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6892 +void dbgaufs_fin(void);
6893 +int __init dbgaufs_init(void);
6894 +#else
6895 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6896 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6897 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6898 +          int topdown)
6899 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6900 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6901 +AuStubVoid(dbgaufs_fin, void)
6902 +AuStubInt0(__init dbgaufs_init, void)
6903 +#endif /* CONFIG_DEBUG_FS */
6904 +
6905 +#endif /* __KERNEL__ */
6906 +#endif /* __DBGAUFS_H__ */
6907 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6908 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6909 +++ linux/fs/aufs/dcsub.c       2021-11-01 23:48:34.206359262 +0100
6910 @@ -0,0 +1,225 @@
6911 +// SPDX-License-Identifier: GPL-2.0
6912 +/*
6913 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6914 + *
6915 + * This program, aufs is free software; you can redistribute it and/or modify
6916 + * it under the terms of the GNU General Public License as published by
6917 + * the Free Software Foundation; either version 2 of the License, or
6918 + * (at your option) any later version.
6919 + *
6920 + * This program is distributed in the hope that it will be useful,
6921 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6922 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6923 + * GNU General Public License for more details.
6924 + *
6925 + * You should have received a copy of the GNU General Public License
6926 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6927 + */
6928 +
6929 +/*
6930 + * sub-routines for dentry cache
6931 + */
6932 +
6933 +#include "aufs.h"
6934 +
6935 +static void au_dpage_free(struct au_dpage *dpage)
6936 +{
6937 +       int i;
6938 +       struct dentry **p;
6939 +
6940 +       p = dpage->dentries;
6941 +       for (i = 0; i < dpage->ndentry; i++)
6942 +               dput(*p++);
6943 +       free_page((unsigned long)dpage->dentries);
6944 +}
6945 +
6946 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6947 +{
6948 +       int err;
6949 +       void *p;
6950 +
6951 +       err = -ENOMEM;
6952 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6953 +       if (unlikely(!dpages->dpages))
6954 +               goto out;
6955 +
6956 +       p = (void *)__get_free_page(gfp);
6957 +       if (unlikely(!p))
6958 +               goto out_dpages;
6959 +
6960 +       dpages->dpages[0].ndentry = 0;
6961 +       dpages->dpages[0].dentries = p;
6962 +       dpages->ndpage = 1;
6963 +       return 0; /* success */
6964 +
6965 +out_dpages:
6966 +       au_kfree_try_rcu(dpages->dpages);
6967 +out:
6968 +       return err;
6969 +}
6970 +
6971 +void au_dpages_free(struct au_dcsub_pages *dpages)
6972 +{
6973 +       int i;
6974 +       struct au_dpage *p;
6975 +
6976 +       p = dpages->dpages;
6977 +       for (i = 0; i < dpages->ndpage; i++)
6978 +               au_dpage_free(p++);
6979 +       au_kfree_try_rcu(dpages->dpages);
6980 +}
6981 +
6982 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6983 +                           struct dentry *dentry, gfp_t gfp)
6984 +{
6985 +       int err, sz;
6986 +       struct au_dpage *dpage;
6987 +       void *p;
6988 +
6989 +       dpage = dpages->dpages + dpages->ndpage - 1;
6990 +       sz = PAGE_SIZE / sizeof(dentry);
6991 +       if (unlikely(dpage->ndentry >= sz)) {
6992 +               AuLabel(new dpage);
6993 +               err = -ENOMEM;
6994 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6995 +               p = au_kzrealloc(dpages->dpages, sz,
6996 +                                sz + sizeof(*dpages->dpages), gfp,
6997 +                                /*may_shrink*/0);
6998 +               if (unlikely(!p))
6999 +                       goto out;
7000 +
7001 +               dpages->dpages = p;
7002 +               dpage = dpages->dpages + dpages->ndpage;
7003 +               p = (void *)__get_free_page(gfp);
7004 +               if (unlikely(!p))
7005 +                       goto out;
7006 +
7007 +               dpage->ndentry = 0;
7008 +               dpage->dentries = p;
7009 +               dpages->ndpage++;
7010 +       }
7011 +
7012 +       AuDebugOn(au_dcount(dentry) <= 0);
7013 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7014 +       return 0; /* success */
7015 +
7016 +out:
7017 +       return err;
7018 +}
7019 +
7020 +/* todo: BAD approach */
7021 +/* copied from linux/fs/dcache.c */
7022 +enum d_walk_ret {
7023 +       D_WALK_CONTINUE,
7024 +       D_WALK_QUIT,
7025 +       D_WALK_NORETRY,
7026 +       D_WALK_SKIP,
7027 +};
7028 +
7029 +extern void d_walk(struct dentry *parent, void *data,
7030 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7031 +
7032 +struct ac_dpages_arg {
7033 +       int err;
7034 +       struct au_dcsub_pages *dpages;
7035 +       struct super_block *sb;
7036 +       au_dpages_test test;
7037 +       void *arg;
7038 +};
7039 +
7040 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7041 +{
7042 +       enum d_walk_ret ret;
7043 +       struct ac_dpages_arg *arg = _arg;
7044 +
7045 +       ret = D_WALK_CONTINUE;
7046 +       if (dentry->d_sb == arg->sb
7047 +           && !IS_ROOT(dentry)
7048 +           && au_dcount(dentry) > 0
7049 +           && au_di(dentry)
7050 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7051 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7052 +               if (unlikely(arg->err))
7053 +                       ret = D_WALK_QUIT;
7054 +       }
7055 +
7056 +       return ret;
7057 +}
7058 +
7059 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7060 +                  au_dpages_test test, void *arg)
7061 +{
7062 +       struct ac_dpages_arg args = {
7063 +               .err    = 0,
7064 +               .dpages = dpages,
7065 +               .sb     = root->d_sb,
7066 +               .test   = test,
7067 +               .arg    = arg
7068 +       };
7069 +
7070 +       d_walk(root, &args, au_call_dpages_append);
7071 +
7072 +       return args.err;
7073 +}
7074 +
7075 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7076 +                      int do_include, au_dpages_test test, void *arg)
7077 +{
7078 +       int err;
7079 +
7080 +       err = 0;
7081 +       write_seqlock(&rename_lock);
7082 +       spin_lock(&dentry->d_lock);
7083 +       if (do_include
7084 +           && au_dcount(dentry) > 0
7085 +           && (!test || test(dentry, arg)))
7086 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7087 +       spin_unlock(&dentry->d_lock);
7088 +       if (unlikely(err))
7089 +               goto out;
7090 +
7091 +       /*
7092 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7093 +        * mount
7094 +        */
7095 +       while (!IS_ROOT(dentry)) {
7096 +               dentry = dentry->d_parent; /* rename_lock is locked */
7097 +               spin_lock(&dentry->d_lock);
7098 +               if (au_dcount(dentry) > 0
7099 +                   && (!test || test(dentry, arg)))
7100 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7101 +               spin_unlock(&dentry->d_lock);
7102 +               if (unlikely(err))
7103 +                       break;
7104 +       }
7105 +
7106 +out:
7107 +       write_sequnlock(&rename_lock);
7108 +       return err;
7109 +}
7110 +
7111 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7112 +{
7113 +       return au_di(dentry) && dentry->d_sb == arg;
7114 +}
7115 +
7116 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7117 +                           struct dentry *dentry, int do_include)
7118 +{
7119 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7120 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7121 +}
7122 +
7123 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7124 +{
7125 +       struct path path[2] = {
7126 +               {
7127 +                       .dentry = d1
7128 +               },
7129 +               {
7130 +                       .dentry = d2
7131 +               }
7132 +       };
7133 +
7134 +       return path_is_under(path + 0, path + 1);
7135 +}
7136 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7137 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7138 +++ linux/fs/aufs/dcsub.h       2021-11-01 23:48:34.206359262 +0100
7139 @@ -0,0 +1,137 @@
7140 +/* SPDX-License-Identifier: GPL-2.0 */
7141 +/*
7142 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7143 + *
7144 + * This program, aufs is free software; you can redistribute it and/or modify
7145 + * it under the terms of the GNU General Public License as published by
7146 + * the Free Software Foundation; either version 2 of the License, or
7147 + * (at your option) any later version.
7148 + *
7149 + * This program is distributed in the hope that it will be useful,
7150 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7151 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7152 + * GNU General Public License for more details.
7153 + *
7154 + * You should have received a copy of the GNU General Public License
7155 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7156 + */
7157 +
7158 +/*
7159 + * sub-routines for dentry cache
7160 + */
7161 +
7162 +#ifndef __AUFS_DCSUB_H__
7163 +#define __AUFS_DCSUB_H__
7164 +
7165 +#ifdef __KERNEL__
7166 +
7167 +#include <linux/dcache.h>
7168 +#include <linux/fs.h>
7169 +
7170 +struct au_dpage {
7171 +       int ndentry;
7172 +       struct dentry **dentries;
7173 +};
7174 +
7175 +struct au_dcsub_pages {
7176 +       int ndpage;
7177 +       struct au_dpage *dpages;
7178 +};
7179 +
7180 +/* ---------------------------------------------------------------------- */
7181 +
7182 +/* dcsub.c */
7183 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7184 +void au_dpages_free(struct au_dcsub_pages *dpages);
7185 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7186 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7187 +                  au_dpages_test test, void *arg);
7188 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7189 +                      int do_include, au_dpages_test test, void *arg);
7190 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7191 +                           struct dentry *dentry, int do_include);
7192 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7193 +
7194 +/* ---------------------------------------------------------------------- */
7195 +
7196 +/*
7197 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7198 + * include/linux/dcache.h. Try them (in the future).
7199 + */
7200 +
7201 +static inline int au_d_hashed_positive(struct dentry *d)
7202 +{
7203 +       int err;
7204 +       struct inode *inode = d_inode(d);
7205 +
7206 +       err = 0;
7207 +       if (unlikely(d_unhashed(d)
7208 +                    || d_is_negative(d)
7209 +                    || !inode->i_nlink))
7210 +               err = -ENOENT;
7211 +       return err;
7212 +}
7213 +
7214 +static inline int au_d_linkable(struct dentry *d)
7215 +{
7216 +       int err;
7217 +       struct inode *inode = d_inode(d);
7218 +
7219 +       err = au_d_hashed_positive(d);
7220 +       if (err
7221 +           && d_is_positive(d)
7222 +           && (inode->i_state & I_LINKABLE))
7223 +               err = 0;
7224 +       return err;
7225 +}
7226 +
7227 +static inline int au_d_alive(struct dentry *d)
7228 +{
7229 +       int err;
7230 +       struct inode *inode;
7231 +
7232 +       err = 0;
7233 +       if (!IS_ROOT(d))
7234 +               err = au_d_hashed_positive(d);
7235 +       else {
7236 +               inode = d_inode(d);
7237 +               if (unlikely(d_unlinked(d)
7238 +                            || d_is_negative(d)
7239 +                            || !inode->i_nlink))
7240 +                       err = -ENOENT;
7241 +       }
7242 +       return err;
7243 +}
7244 +
7245 +static inline int au_alive_dir(struct dentry *d)
7246 +{
7247 +       int err;
7248 +
7249 +       err = au_d_alive(d);
7250 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7251 +               err = -ENOENT;
7252 +       return err;
7253 +}
7254 +
7255 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7256 +{
7257 +       return a->len == b->len
7258 +               && !memcmp(a->name, b->name, a->len);
7259 +}
7260 +
7261 +/*
7262 + * by the commit
7263 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7264 + *                     taking d_lock
7265 + * the type of d_lockref.count became int, but the inlined function d_count()
7266 + * still returns unsigned int.
7267 + * I don't know why. Maybe it is for every d_count() users?
7268 + * Anyway au_dcount() lives on.
7269 + */
7270 +static inline int au_dcount(struct dentry *d)
7271 +{
7272 +       return (int)d_count(d);
7273 +}
7274 +
7275 +#endif /* __KERNEL__ */
7276 +#endif /* __AUFS_DCSUB_H__ */
7277 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7278 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7279 +++ linux/fs/aufs/debug.c       2021-11-01 23:48:34.206359262 +0100
7280 @@ -0,0 +1,444 @@
7281 +// SPDX-License-Identifier: GPL-2.0
7282 +/*
7283 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7284 + *
7285 + * This program, aufs is free software; you can redistribute it and/or modify
7286 + * it under the terms of the GNU General Public License as published by
7287 + * the Free Software Foundation; either version 2 of the License, or
7288 + * (at your option) any later version.
7289 + *
7290 + * This program is distributed in the hope that it will be useful,
7291 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7292 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7293 + * GNU General Public License for more details.
7294 + *
7295 + * You should have received a copy of the GNU General Public License
7296 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7297 + */
7298 +
7299 +/*
7300 + * debug print functions
7301 + */
7302 +
7303 +#include <linux/iversion.h>
7304 +#include "aufs.h"
7305 +
7306 +/* Returns 0, or -errno.  arg is in kp->arg. */
7307 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7308 +{
7309 +       int err, n;
7310 +
7311 +       err = kstrtoint(val, 0, &n);
7312 +       if (!err) {
7313 +               if (n > 0)
7314 +                       au_debug_on();
7315 +               else
7316 +                       au_debug_off();
7317 +       }
7318 +       return err;
7319 +}
7320 +
7321 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7322 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7323 +{
7324 +       atomic_t *a;
7325 +
7326 +       a = kp->arg;
7327 +       return sprintf(buffer, "%d", atomic_read(a));
7328 +}
7329 +
7330 +static struct kernel_param_ops param_ops_atomic_t = {
7331 +       .set = param_atomic_t_set,
7332 +       .get = param_atomic_t_get
7333 +       /* void (*free)(void *arg) */
7334 +};
7335 +
7336 +atomic_t aufs_debug = ATOMIC_INIT(0);
7337 +MODULE_PARM_DESC(debug, "debug print");
7338 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7339 +
7340 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7341 +char *au_plevel = KERN_DEBUG;
7342 +#define dpri(fmt, ...) do {                                    \
7343 +       if ((au_plevel                                          \
7344 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7345 +           || au_debug_test())                                 \
7346 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7347 +} while (0)
7348 +
7349 +/* ---------------------------------------------------------------------- */
7350 +
7351 +void au_dpri_whlist(struct au_nhash *whlist)
7352 +{
7353 +       unsigned long ul, n;
7354 +       struct hlist_head *head;
7355 +       struct au_vdir_wh *pos;
7356 +
7357 +       n = whlist->nh_num;
7358 +       head = whlist->nh_head;
7359 +       for (ul = 0; ul < n; ul++) {
7360 +               hlist_for_each_entry(pos, head, wh_hash)
7361 +                       dpri("b%d, %.*s, %d\n",
7362 +                            pos->wh_bindex,
7363 +                            pos->wh_str.len, pos->wh_str.name,
7364 +                            pos->wh_str.len);
7365 +               head++;
7366 +       }
7367 +}
7368 +
7369 +void au_dpri_vdir(struct au_vdir *vdir)
7370 +{
7371 +       unsigned long ul;
7372 +       union au_vdir_deblk_p p;
7373 +       unsigned char *o;
7374 +
7375 +       if (!vdir || IS_ERR(vdir)) {
7376 +               dpri("err %ld\n", PTR_ERR(vdir));
7377 +               return;
7378 +       }
7379 +
7380 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7381 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7382 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7383 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7384 +               p.deblk = vdir->vd_deblk[ul];
7385 +               o = p.deblk;
7386 +               dpri("[%lu]: %p\n", ul, o);
7387 +       }
7388 +}
7389 +
7390 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7391 +                       struct dentry *wh)
7392 +{
7393 +       char *n = NULL;
7394 +       int l = 0;
7395 +
7396 +       if (!inode || IS_ERR(inode)) {
7397 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7398 +               return -1;
7399 +       }
7400 +
7401 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7402 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7403 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7404 +       if (wh) {
7405 +               n = (void *)wh->d_name.name;
7406 +               l = wh->d_name.len;
7407 +       }
7408 +
7409 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7410 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7411 +            bindex, inode,
7412 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7413 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7414 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7415 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7416 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7417 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7418 +            inode->i_generation,
7419 +            l ? ", wh " : "", l, n);
7420 +       return 0;
7421 +}
7422 +
7423 +void au_dpri_inode(struct inode *inode)
7424 +{
7425 +       struct au_iinfo *iinfo;
7426 +       struct au_hinode *hi;
7427 +       aufs_bindex_t bindex;
7428 +       int err, hn;
7429 +
7430 +       err = do_pri_inode(-1, inode, -1, NULL);
7431 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7432 +               return;
7433 +
7434 +       iinfo = au_ii(inode);
7435 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7436 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7437 +       if (iinfo->ii_btop < 0)
7438 +               return;
7439 +       hn = 0;
7440 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7441 +               hi = au_hinode(iinfo, bindex);
7442 +               hn = !!au_hn(hi);
7443 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7444 +       }
7445 +}
7446 +
7447 +void au_dpri_dalias(struct inode *inode)
7448 +{
7449 +       struct dentry *d;
7450 +
7451 +       spin_lock(&inode->i_lock);
7452 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7453 +               au_dpri_dentry(d);
7454 +       spin_unlock(&inode->i_lock);
7455 +}
7456 +
7457 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7458 +{
7459 +       struct dentry *wh = NULL;
7460 +       int hn;
7461 +       struct inode *inode;
7462 +       struct au_iinfo *iinfo;
7463 +       struct au_hinode *hi;
7464 +
7465 +       if (!dentry || IS_ERR(dentry)) {
7466 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7467 +               return -1;
7468 +       }
7469 +       /* do not call dget_parent() here */
7470 +       /* note: access d_xxx without d_lock */
7471 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7472 +            bindex, dentry, dentry,
7473 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7474 +            au_dcount(dentry), dentry->d_flags,
7475 +            d_unhashed(dentry) ? "un" : "");
7476 +       hn = -1;
7477 +       inode = NULL;
7478 +       if (d_is_positive(dentry))
7479 +               inode = d_inode(dentry);
7480 +       if (inode
7481 +           && au_test_aufs(dentry->d_sb)
7482 +           && bindex >= 0
7483 +           && !au_is_bad_inode(inode)) {
7484 +               iinfo = au_ii(inode);
7485 +               hi = au_hinode(iinfo, bindex);
7486 +               hn = !!au_hn(hi);
7487 +               wh = hi->hi_whdentry;
7488 +       }
7489 +       do_pri_inode(bindex, inode, hn, wh);
7490 +       return 0;
7491 +}
7492 +
7493 +void au_dpri_dentry(struct dentry *dentry)
7494 +{
7495 +       struct au_dinfo *dinfo;
7496 +       aufs_bindex_t bindex;
7497 +       int err;
7498 +
7499 +       err = do_pri_dentry(-1, dentry);
7500 +       if (err || !au_test_aufs(dentry->d_sb))
7501 +               return;
7502 +
7503 +       dinfo = au_di(dentry);
7504 +       if (!dinfo)
7505 +               return;
7506 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7507 +            dinfo->di_btop, dinfo->di_bbot,
7508 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7509 +            dinfo->di_tmpfile);
7510 +       if (dinfo->di_btop < 0)
7511 +               return;
7512 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7513 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7514 +}
7515 +
7516 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7517 +{
7518 +       char a[32];
7519 +
7520 +       if (!file || IS_ERR(file)) {
7521 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7522 +               return -1;
7523 +       }
7524 +       a[0] = 0;
7525 +       if (bindex < 0
7526 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7527 +           && au_test_aufs(file->f_path.dentry->d_sb)
7528 +           && au_fi(file))
7529 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7530 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7531 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7532 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7533 +            file->f_version, file->f_pos, a);
7534 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7535 +               do_pri_dentry(bindex, file->f_path.dentry);
7536 +       return 0;
7537 +}
7538 +
7539 +void au_dpri_file(struct file *file)
7540 +{
7541 +       struct au_finfo *finfo;
7542 +       struct au_fidir *fidir;
7543 +       struct au_hfile *hfile;
7544 +       aufs_bindex_t bindex;
7545 +       int err;
7546 +
7547 +       err = do_pri_file(-1, file);
7548 +       if (err
7549 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7550 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7551 +               return;
7552 +
7553 +       finfo = au_fi(file);
7554 +       if (!finfo)
7555 +               return;
7556 +       if (finfo->fi_btop < 0)
7557 +               return;
7558 +       fidir = finfo->fi_hdir;
7559 +       if (!fidir)
7560 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7561 +       else
7562 +               for (bindex = finfo->fi_btop;
7563 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7564 +                    bindex++) {
7565 +                       hfile = fidir->fd_hfile + bindex;
7566 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7567 +               }
7568 +}
7569 +
7570 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7571 +{
7572 +       struct vfsmount *mnt;
7573 +       struct super_block *sb;
7574 +
7575 +       if (!br || IS_ERR(br))
7576 +               goto out;
7577 +       mnt = au_br_mnt(br);
7578 +       if (!mnt || IS_ERR(mnt))
7579 +               goto out;
7580 +       sb = mnt->mnt_sb;
7581 +       if (!sb || IS_ERR(sb))
7582 +               goto out;
7583 +
7584 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7585 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7586 +            "xino %d\n",
7587 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7588 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7589 +            sb->s_flags, sb->s_count,
7590 +            atomic_read(&sb->s_active),
7591 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7592 +       return 0;
7593 +
7594 +out:
7595 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7596 +       return -1;
7597 +}
7598 +
7599 +void au_dpri_sb(struct super_block *sb)
7600 +{
7601 +       struct au_sbinfo *sbinfo;
7602 +       aufs_bindex_t bindex;
7603 +       int err;
7604 +       /* to reduce stack size */
7605 +       struct {
7606 +               struct vfsmount mnt;
7607 +               struct au_branch fake;
7608 +       } *a;
7609 +
7610 +       /* this function can be called from magic sysrq */
7611 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7612 +       if (unlikely(!a)) {
7613 +               dpri("no memory\n");
7614 +               return;
7615 +       }
7616 +
7617 +       a->mnt.mnt_sb = sb;
7618 +       a->fake.br_path.mnt = &a->mnt;
7619 +       err = do_pri_br(-1, &a->fake);
7620 +       au_kfree_rcu(a);
7621 +       dpri("dev 0x%x\n", sb->s_dev);
7622 +       if (err || !au_test_aufs(sb))
7623 +               return;
7624 +
7625 +       sbinfo = au_sbi(sb);
7626 +       if (!sbinfo)
7627 +               return;
7628 +       dpri("nw %d, gen %u, kobj %d\n",
7629 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7630 +            kref_read(&sbinfo->si_kobj.kref));
7631 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7632 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7633 +}
7634 +
7635 +/* ---------------------------------------------------------------------- */
7636 +
7637 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7638 +{
7639 +       struct inode *h_inode, *inode = d_inode(dentry);
7640 +       struct dentry *h_dentry;
7641 +       aufs_bindex_t bindex, bbot, bi;
7642 +
7643 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7644 +               return;
7645 +
7646 +       bbot = au_dbbot(dentry);
7647 +       bi = au_ibbot(inode);
7648 +       if (bi < bbot)
7649 +               bbot = bi;
7650 +       bindex = au_dbtop(dentry);
7651 +       bi = au_ibtop(inode);
7652 +       if (bi > bindex)
7653 +               bindex = bi;
7654 +
7655 +       for (; bindex <= bbot; bindex++) {
7656 +               h_dentry = au_h_dptr(dentry, bindex);
7657 +               if (!h_dentry)
7658 +                       continue;
7659 +               h_inode = au_h_iptr(inode, bindex);
7660 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7661 +                       au_debug_on();
7662 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7663 +                       AuDbgDentry(dentry);
7664 +                       AuDbgInode(inode);
7665 +                       au_debug_off();
7666 +                       if (au_test_fuse(h_inode->i_sb))
7667 +                               WARN_ON_ONCE(1);
7668 +                       else
7669 +                               BUG();
7670 +               }
7671 +       }
7672 +}
7673 +
7674 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7675 +{
7676 +       int err, i, j;
7677 +       struct au_dcsub_pages dpages;
7678 +       struct au_dpage *dpage;
7679 +       struct dentry **dentries;
7680 +
7681 +       err = au_dpages_init(&dpages, GFP_NOFS);
7682 +       AuDebugOn(err);
7683 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7684 +       AuDebugOn(err);
7685 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7686 +               dpage = dpages.dpages + i;
7687 +               dentries = dpage->dentries;
7688 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7689 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7690 +       }
7691 +       au_dpages_free(&dpages);
7692 +}
7693 +
7694 +void au_dbg_verify_kthread(void)
7695 +{
7696 +       if (au_wkq_test()) {
7697 +               au_dbg_blocked();
7698 +               /*
7699 +                * It may be recursive, but udba=notify between two aufs mounts,
7700 +                * where a single ro branch is shared, is not a problem.
7701 +                */
7702 +               /* WARN_ON(1); */
7703 +       }
7704 +}
7705 +
7706 +/* ---------------------------------------------------------------------- */
7707 +
7708 +int __init au_debug_init(void)
7709 +{
7710 +       aufs_bindex_t bindex;
7711 +       struct au_vdir_destr destr;
7712 +
7713 +       bindex = -1;
7714 +       AuDebugOn(bindex >= 0);
7715 +
7716 +       destr.len = -1;
7717 +       AuDebugOn(destr.len < NAME_MAX);
7718 +
7719 +#ifdef CONFIG_4KSTACKS
7720 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7721 +#endif
7722 +
7723 +       return 0;
7724 +}
7725 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7726 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7727 +++ linux/fs/aufs/debug.h       2021-11-01 23:48:34.206359262 +0100
7728 @@ -0,0 +1,226 @@
7729 +/* SPDX-License-Identifier: GPL-2.0 */
7730 +/*
7731 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7732 + *
7733 + * This program, aufs is free software; you can redistribute it and/or modify
7734 + * it under the terms of the GNU General Public License as published by
7735 + * the Free Software Foundation; either version 2 of the License, or
7736 + * (at your option) any later version.
7737 + *
7738 + * This program is distributed in the hope that it will be useful,
7739 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7740 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7741 + * GNU General Public License for more details.
7742 + *
7743 + * You should have received a copy of the GNU General Public License
7744 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7745 + */
7746 +
7747 +/*
7748 + * debug print functions
7749 + */
7750 +
7751 +#ifndef __AUFS_DEBUG_H__
7752 +#define __AUFS_DEBUG_H__
7753 +
7754 +#ifdef __KERNEL__
7755 +
7756 +#include <linux/atomic.h>
7757 +#include <linux/module.h>
7758 +#include <linux/kallsyms.h>
7759 +#include <linux/sysrq.h>
7760 +
7761 +#ifdef CONFIG_AUFS_DEBUG
7762 +#define AuDebugOn(a)           BUG_ON(a)
7763 +
7764 +/* module parameter */
7765 +extern atomic_t aufs_debug;
7766 +static inline void au_debug_on(void)
7767 +{
7768 +       atomic_inc(&aufs_debug);
7769 +}
7770 +static inline void au_debug_off(void)
7771 +{
7772 +       atomic_dec_if_positive(&aufs_debug);
7773 +}
7774 +
7775 +static inline int au_debug_test(void)
7776 +{
7777 +       return atomic_read(&aufs_debug) > 0;
7778 +}
7779 +#else
7780 +#define AuDebugOn(a)           do {} while (0)
7781 +AuStubVoid(au_debug_on, void)
7782 +AuStubVoid(au_debug_off, void)
7783 +AuStubInt0(au_debug_test, void)
7784 +#endif /* CONFIG_AUFS_DEBUG */
7785 +
7786 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7787 +
7788 +/* ---------------------------------------------------------------------- */
7789 +
7790 +/* debug print */
7791 +
7792 +#define AuDbg(fmt, ...) do { \
7793 +       if (au_debug_test()) \
7794 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7795 +} while (0)
7796 +#define AuLabel(l)             AuDbg(#l "\n")
7797 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7798 +#define AuWarn1(fmt, ...) do { \
7799 +       static unsigned char _c; \
7800 +       if (!_c++) \
7801 +               pr_warn(fmt, ##__VA_ARGS__); \
7802 +} while (0)
7803 +
7804 +#define AuErr1(fmt, ...) do { \
7805 +       static unsigned char _c; \
7806 +       if (!_c++) \
7807 +               pr_err(fmt, ##__VA_ARGS__); \
7808 +} while (0)
7809 +
7810 +#define AuIOErr1(fmt, ...) do { \
7811 +       static unsigned char _c; \
7812 +       if (!_c++) \
7813 +               AuIOErr(fmt, ##__VA_ARGS__); \
7814 +} while (0)
7815 +
7816 +#define AuUnsupportMsg "This operation is not supported." \
7817 +                       " Please report this application to aufs-users ML."
7818 +#define AuUnsupport(fmt, ...) do { \
7819 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7820 +       dump_stack(); \
7821 +} while (0)
7822 +
7823 +#define AuTraceErr(e) do { \
7824 +       if (unlikely((e) < 0)) \
7825 +               AuDbg("err %d\n", (int)(e)); \
7826 +} while (0)
7827 +
7828 +#define AuTraceErrPtr(p) do { \
7829 +       if (IS_ERR(p)) \
7830 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7831 +} while (0)
7832 +
7833 +/* dirty macros for debug print, use with "%.*s" and caution */
7834 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7835 +
7836 +/* ---------------------------------------------------------------------- */
7837 +
7838 +struct dentry;
7839 +#ifdef CONFIG_AUFS_DEBUG
7840 +extern struct mutex au_dbg_mtx;
7841 +extern char *au_plevel;
7842 +struct au_nhash;
7843 +void au_dpri_whlist(struct au_nhash *whlist);
7844 +struct au_vdir;
7845 +void au_dpri_vdir(struct au_vdir *vdir);
7846 +struct inode;
7847 +void au_dpri_inode(struct inode *inode);
7848 +void au_dpri_dalias(struct inode *inode);
7849 +void au_dpri_dentry(struct dentry *dentry);
7850 +struct file;
7851 +void au_dpri_file(struct file *filp);
7852 +struct super_block;
7853 +void au_dpri_sb(struct super_block *sb);
7854 +
7855 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7856 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7857 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7858 +void au_dbg_verify_kthread(void);
7859 +
7860 +int __init au_debug_init(void);
7861 +
7862 +#define AuDbgWhlist(w) do { \
7863 +       mutex_lock(&au_dbg_mtx); \
7864 +       AuDbg(#w "\n"); \
7865 +       au_dpri_whlist(w); \
7866 +       mutex_unlock(&au_dbg_mtx); \
7867 +} while (0)
7868 +
7869 +#define AuDbgVdir(v) do { \
7870 +       mutex_lock(&au_dbg_mtx); \
7871 +       AuDbg(#v "\n"); \
7872 +       au_dpri_vdir(v); \
7873 +       mutex_unlock(&au_dbg_mtx); \
7874 +} while (0)
7875 +
7876 +#define AuDbgInode(i) do { \
7877 +       mutex_lock(&au_dbg_mtx); \
7878 +       AuDbg(#i "\n"); \
7879 +       au_dpri_inode(i); \
7880 +       mutex_unlock(&au_dbg_mtx); \
7881 +} while (0)
7882 +
7883 +#define AuDbgDAlias(i) do { \
7884 +       mutex_lock(&au_dbg_mtx); \
7885 +       AuDbg(#i "\n"); \
7886 +       au_dpri_dalias(i); \
7887 +       mutex_unlock(&au_dbg_mtx); \
7888 +} while (0)
7889 +
7890 +#define AuDbgDentry(d) do { \
7891 +       mutex_lock(&au_dbg_mtx); \
7892 +       AuDbg(#d "\n"); \
7893 +       au_dpri_dentry(d); \
7894 +       mutex_unlock(&au_dbg_mtx); \
7895 +} while (0)
7896 +
7897 +#define AuDbgFile(f) do { \
7898 +       mutex_lock(&au_dbg_mtx); \
7899 +       AuDbg(#f "\n"); \
7900 +       au_dpri_file(f); \
7901 +       mutex_unlock(&au_dbg_mtx); \
7902 +} while (0)
7903 +
7904 +#define AuDbgSb(sb) do { \
7905 +       mutex_lock(&au_dbg_mtx); \
7906 +       AuDbg(#sb "\n"); \
7907 +       au_dpri_sb(sb); \
7908 +       mutex_unlock(&au_dbg_mtx); \
7909 +} while (0)
7910 +
7911 +#define AuDbgSym(addr) do {                            \
7912 +       char sym[KSYM_SYMBOL_LEN];                      \
7913 +       sprint_symbol(sym, (unsigned long)addr);        \
7914 +       AuDbg("%s\n", sym);                             \
7915 +} while (0)
7916 +#else
7917 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7918 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7919 +AuStubVoid(au_dbg_verify_kthread, void)
7920 +AuStubInt0(__init au_debug_init, void)
7921 +
7922 +#define AuDbgWhlist(w)         do {} while (0)
7923 +#define AuDbgVdir(v)           do {} while (0)
7924 +#define AuDbgInode(i)          do {} while (0)
7925 +#define AuDbgDAlias(i)         do {} while (0)
7926 +#define AuDbgDentry(d)         do {} while (0)
7927 +#define AuDbgFile(f)           do {} while (0)
7928 +#define AuDbgSb(sb)            do {} while (0)
7929 +#define AuDbgSym(addr)         do {} while (0)
7930 +#endif /* CONFIG_AUFS_DEBUG */
7931 +
7932 +/* ---------------------------------------------------------------------- */
7933 +
7934 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7935 +int __init au_sysrq_init(void);
7936 +void au_sysrq_fin(void);
7937 +
7938 +#ifdef CONFIG_HW_CONSOLE
7939 +#define au_dbg_blocked() do { \
7940 +       WARN_ON(1); \
7941 +       handle_sysrq('w'); \
7942 +} while (0)
7943 +#else
7944 +AuStubVoid(au_dbg_blocked, void)
7945 +#endif
7946 +
7947 +#else
7948 +AuStubInt0(__init au_sysrq_init, void)
7949 +AuStubVoid(au_sysrq_fin, void)
7950 +AuStubVoid(au_dbg_blocked, void)
7951 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7952 +
7953 +#endif /* __KERNEL__ */
7954 +#endif /* __AUFS_DEBUG_H__ */
7955 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7956 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7957 +++ linux/fs/aufs/dentry.c      2021-11-01 23:48:34.206359262 +0100
7958 @@ -0,0 +1,1169 @@
7959 +// SPDX-License-Identifier: GPL-2.0
7960 +/*
7961 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7962 + *
7963 + * This program, aufs is free software; you can redistribute it and/or modify
7964 + * it under the terms of the GNU General Public License as published by
7965 + * the Free Software Foundation; either version 2 of the License, or
7966 + * (at your option) any later version.
7967 + *
7968 + * This program is distributed in the hope that it will be useful,
7969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7971 + * GNU General Public License for more details.
7972 + *
7973 + * You should have received a copy of the GNU General Public License
7974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7975 + */
7976 +
7977 +/*
7978 + * lookup and dentry operations
7979 + */
7980 +
7981 +#include <linux/iversion.h>
7982 +#include <linux/namei.h>
7983 +#include "aufs.h"
7984 +
7985 +/*
7986 + * returns positive/negative dentry, NULL or an error.
7987 + * NULL means whiteout-ed or not-found.
7988 + */
7989 +static struct dentry*
7990 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7991 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7992 +{
7993 +       struct dentry *h_dentry;
7994 +       struct inode *h_inode;
7995 +       struct au_branch *br;
7996 +       struct user_namespace *h_userns;
7997 +       struct path h_path;
7998 +       int wh_found, opq;
7999 +       unsigned char wh_able;
8000 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8001 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8002 +                                                         IGNORE_PERM);
8003 +
8004 +       wh_found = 0;
8005 +       br = au_sbr(dentry->d_sb, bindex);
8006 +       h_path.dentry = h_parent;
8007 +       h_path.mnt = au_br_mnt(br);
8008 +       h_userns = au_br_userns(br);
8009 +       wh_able = !!au_br_whable(br->br_perm);
8010 +       if (wh_able)
8011 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
8012 +                                     ignore_perm);
8013 +       h_dentry = ERR_PTR(wh_found);
8014 +       if (!wh_found)
8015 +               goto real_lookup;
8016 +       if (unlikely(wh_found < 0))
8017 +               goto out;
8018 +
8019 +       /* We found a whiteout */
8020 +       /* au_set_dbbot(dentry, bindex); */
8021 +       au_set_dbwh(dentry, bindex);
8022 +       if (!allow_neg)
8023 +               return NULL; /* success */
8024 +
8025 +real_lookup:
8026 +       if (!ignore_perm)
8027 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8028 +       else
8029 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8030 +       if (IS_ERR(h_dentry)) {
8031 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8032 +                   && !allow_neg)
8033 +                       h_dentry = NULL;
8034 +               goto out;
8035 +       }
8036 +
8037 +       h_inode = d_inode(h_dentry);
8038 +       if (d_is_negative(h_dentry)) {
8039 +               if (!allow_neg)
8040 +                       goto out_neg;
8041 +       } else if (wh_found
8042 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8043 +               goto out_neg;
8044 +       else if (au_ftest_lkup(args->flags, DIRREN)
8045 +                /* && h_inode */
8046 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8047 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8048 +                     (unsigned long long)h_inode->i_ino);
8049 +               goto out_neg;
8050 +       }
8051 +
8052 +       if (au_dbbot(dentry) <= bindex)
8053 +               au_set_dbbot(dentry, bindex);
8054 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8055 +               au_set_dbtop(dentry, bindex);
8056 +       au_set_h_dptr(dentry, bindex, h_dentry);
8057 +
8058 +       if (!d_is_dir(h_dentry)
8059 +           || !wh_able
8060 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8061 +               goto out; /* success */
8062 +
8063 +       h_path.dentry = h_dentry;
8064 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8065 +       opq = au_diropq_test(h_userns, &h_path);
8066 +       inode_unlock_shared(h_inode);
8067 +       if (opq > 0)
8068 +               au_set_dbdiropq(dentry, bindex);
8069 +       else if (unlikely(opq < 0)) {
8070 +               au_set_h_dptr(dentry, bindex, NULL);
8071 +               h_dentry = ERR_PTR(opq);
8072 +       }
8073 +       goto out;
8074 +
8075 +out_neg:
8076 +       dput(h_dentry);
8077 +       h_dentry = NULL;
8078 +out:
8079 +       return h_dentry;
8080 +}
8081 +
8082 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8083 +{
8084 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8085 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8086 +               return -EPERM;
8087 +       return 0;
8088 +}
8089 +
8090 +/*
8091 + * returns the number of lower positive dentries,
8092 + * otherwise an error.
8093 + * can be called at unlinking with @type is zero.
8094 + */
8095 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8096 +                  unsigned int flags)
8097 +{
8098 +       int npositive, err;
8099 +       aufs_bindex_t bindex, btail, bdiropq;
8100 +       unsigned char isdir, dirperm1, dirren;
8101 +       struct au_do_lookup_args args = {
8102 +               .flags          = flags,
8103 +               .name           = &dentry->d_name
8104 +       };
8105 +       struct dentry *parent;
8106 +       struct super_block *sb;
8107 +
8108 +       sb = dentry->d_sb;
8109 +       err = au_test_shwh(sb, args.name);
8110 +       if (unlikely(err))
8111 +               goto out;
8112 +
8113 +       err = au_wh_name_alloc(&args.whname, args.name);
8114 +       if (unlikely(err))
8115 +               goto out;
8116 +
8117 +       isdir = !!d_is_dir(dentry);
8118 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8119 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8120 +       if (dirren)
8121 +               au_fset_lkup(args.flags, DIRREN);
8122 +
8123 +       npositive = 0;
8124 +       parent = dget_parent(dentry);
8125 +       btail = au_dbtaildir(parent);
8126 +       for (bindex = btop; bindex <= btail; bindex++) {
8127 +               struct dentry *h_parent, *h_dentry;
8128 +               struct inode *h_inode, *h_dir;
8129 +               struct au_branch *br;
8130 +
8131 +               h_dentry = au_h_dptr(dentry, bindex);
8132 +               if (h_dentry) {
8133 +                       if (d_is_positive(h_dentry))
8134 +                               npositive++;
8135 +                       break;
8136 +               }
8137 +               h_parent = au_h_dptr(parent, bindex);
8138 +               if (!h_parent || !d_is_dir(h_parent))
8139 +                       continue;
8140 +
8141 +               if (dirren) {
8142 +                       /* if the inum matches, then use the prepared name */
8143 +                       err = au_dr_lkup_name(&args, bindex);
8144 +                       if (unlikely(err))
8145 +                               goto out_parent;
8146 +               }
8147 +
8148 +               h_dir = d_inode(h_parent);
8149 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8150 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8151 +               inode_unlock_shared(h_dir);
8152 +               err = PTR_ERR(h_dentry);
8153 +               if (IS_ERR(h_dentry))
8154 +                       goto out_parent;
8155 +               if (h_dentry)
8156 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8157 +               if (dirperm1)
8158 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8159 +
8160 +               if (au_dbwh(dentry) == bindex)
8161 +                       break;
8162 +               if (!h_dentry)
8163 +                       continue;
8164 +               if (d_is_negative(h_dentry))
8165 +                       continue;
8166 +               h_inode = d_inode(h_dentry);
8167 +               npositive++;
8168 +               if (!args.type)
8169 +                       args.type = h_inode->i_mode & S_IFMT;
8170 +               if (args.type != S_IFDIR)
8171 +                       break;
8172 +               else if (isdir) {
8173 +                       /* the type of lower may be different */
8174 +                       bdiropq = au_dbdiropq(dentry);
8175 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8176 +                               break;
8177 +               }
8178 +               br = au_sbr(sb, bindex);
8179 +               if (dirren
8180 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8181 +                                          /*add_ent*/NULL)) {
8182 +                       /* prepare next name to lookup */
8183 +                       err = au_dr_lkup(&args, dentry, bindex);
8184 +                       if (unlikely(err))
8185 +                               goto out_parent;
8186 +               }
8187 +       }
8188 +
8189 +       if (npositive) {
8190 +               AuLabel(positive);
8191 +               au_update_dbtop(dentry);
8192 +       }
8193 +       err = npositive;
8194 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8195 +                    && au_dbtop(dentry) < 0)) {
8196 +               err = -EIO;
8197 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8198 +                       dentry, err);
8199 +       }
8200 +
8201 +out_parent:
8202 +       dput(parent);
8203 +       au_kfree_try_rcu(args.whname.name);
8204 +       if (dirren)
8205 +               au_dr_lkup_fin(&args);
8206 +out:
8207 +       return err;
8208 +}
8209 +
8210 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8211 +                              struct path *ppath)
8212 +{
8213 +       struct dentry *dentry;
8214 +       int wkq_err;
8215 +
8216 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8217 +               dentry = vfsub_lkup_one(name, ppath);
8218 +       else {
8219 +               struct vfsub_lkup_one_args args = {
8220 +                       .errp   = &dentry,
8221 +                       .name   = name,
8222 +                       .ppath  = ppath
8223 +               };
8224 +
8225 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8226 +               if (unlikely(wkq_err))
8227 +                       dentry = ERR_PTR(wkq_err);
8228 +       }
8229 +
8230 +       return dentry;
8231 +}
8232 +
8233 +/*
8234 + * lookup @dentry on @bindex which should be negative.
8235 + */
8236 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8237 +{
8238 +       int err;
8239 +       struct dentry *parent, *h_dentry;
8240 +       struct au_branch *br;
8241 +       struct user_namespace *h_userns;
8242 +       struct path h_ppath;
8243 +
8244 +       parent = dget_parent(dentry);
8245 +       br = au_sbr(dentry->d_sb, bindex);
8246 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8247 +       h_ppath.mnt = au_br_mnt(br);
8248 +       h_userns = au_br_userns(br);
8249 +       if (wh)
8250 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8251 +       else
8252 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8253 +       err = PTR_ERR(h_dentry);
8254 +       if (IS_ERR(h_dentry))
8255 +               goto out;
8256 +       if (unlikely(d_is_positive(h_dentry))) {
8257 +               err = -EIO;
8258 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8259 +               dput(h_dentry);
8260 +               goto out;
8261 +       }
8262 +
8263 +       err = 0;
8264 +       if (bindex < au_dbtop(dentry))
8265 +               au_set_dbtop(dentry, bindex);
8266 +       if (au_dbbot(dentry) < bindex)
8267 +               au_set_dbbot(dentry, bindex);
8268 +       au_set_h_dptr(dentry, bindex, h_dentry);
8269 +
8270 +out:
8271 +       dput(parent);
8272 +       return err;
8273 +}
8274 +
8275 +/* ---------------------------------------------------------------------- */
8276 +
8277 +/* subset of struct inode */
8278 +struct au_iattr {
8279 +       unsigned long           i_ino;
8280 +       /* unsigned int         i_nlink; */
8281 +       kuid_t                  i_uid;
8282 +       kgid_t                  i_gid;
8283 +       u64                     i_version;
8284 +/*
8285 +       loff_t                  i_size;
8286 +       blkcnt_t                i_blocks;
8287 +*/
8288 +       umode_t                 i_mode;
8289 +};
8290 +
8291 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8292 +{
8293 +       ia->i_ino = h_inode->i_ino;
8294 +       /* ia->i_nlink = h_inode->i_nlink; */
8295 +       ia->i_uid = h_inode->i_uid;
8296 +       ia->i_gid = h_inode->i_gid;
8297 +       ia->i_version = inode_query_iversion(h_inode);
8298 +/*
8299 +       ia->i_size = h_inode->i_size;
8300 +       ia->i_blocks = h_inode->i_blocks;
8301 +*/
8302 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8303 +}
8304 +
8305 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8306 +{
8307 +       return ia->i_ino != h_inode->i_ino
8308 +               /* || ia->i_nlink != h_inode->i_nlink */
8309 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8310 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8311 +               || !inode_eq_iversion(h_inode, ia->i_version)
8312 +/*
8313 +               || ia->i_size != h_inode->i_size
8314 +               || ia->i_blocks != h_inode->i_blocks
8315 +*/
8316 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8317 +}
8318 +
8319 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8320 +                             struct au_branch *br)
8321 +{
8322 +       int err;
8323 +       struct au_iattr ia;
8324 +       struct inode *h_inode;
8325 +       struct dentry *h_d;
8326 +       struct super_block *h_sb;
8327 +       struct path h_ppath;
8328 +
8329 +       err = 0;
8330 +       memset(&ia, -1, sizeof(ia));
8331 +       h_sb = h_dentry->d_sb;
8332 +       h_inode = NULL;
8333 +       if (d_is_positive(h_dentry)) {
8334 +               h_inode = d_inode(h_dentry);
8335 +               au_iattr_save(&ia, h_inode);
8336 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8337 +               /* nfs d_revalidate may return 0 for negative dentry */
8338 +               /* fuse d_revalidate always return 0 for negative dentry */
8339 +               goto out;
8340 +
8341 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8342 +       h_ppath.dentry = h_parent;
8343 +       h_ppath.mnt = au_br_mnt(br);
8344 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8345 +       err = PTR_ERR(h_d);
8346 +       if (IS_ERR(h_d))
8347 +               goto out;
8348 +
8349 +       err = 0;
8350 +       if (unlikely(h_d != h_dentry
8351 +                    || d_inode(h_d) != h_inode
8352 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8353 +               err = au_busy_or_stale();
8354 +       dput(h_d);
8355 +
8356 +out:
8357 +       AuTraceErr(err);
8358 +       return err;
8359 +}
8360 +
8361 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8362 +               struct dentry *h_parent, struct au_branch *br)
8363 +{
8364 +       int err;
8365 +
8366 +       err = 0;
8367 +       if (udba == AuOpt_UDBA_REVAL
8368 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8369 +               IMustLock(h_dir);
8370 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8371 +       } else if (udba != AuOpt_UDBA_NONE)
8372 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8373 +
8374 +       return err;
8375 +}
8376 +
8377 +/* ---------------------------------------------------------------------- */
8378 +
8379 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8380 +{
8381 +       int err;
8382 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8383 +       struct au_hdentry tmp, *p, *q;
8384 +       struct au_dinfo *dinfo;
8385 +       struct super_block *sb;
8386 +
8387 +       DiMustWriteLock(dentry);
8388 +
8389 +       sb = dentry->d_sb;
8390 +       dinfo = au_di(dentry);
8391 +       bbot = dinfo->di_bbot;
8392 +       bwh = dinfo->di_bwh;
8393 +       bdiropq = dinfo->di_bdiropq;
8394 +       bindex = dinfo->di_btop;
8395 +       p = au_hdentry(dinfo, bindex);
8396 +       for (; bindex <= bbot; bindex++, p++) {
8397 +               if (!p->hd_dentry)
8398 +                       continue;
8399 +
8400 +               new_bindex = au_br_index(sb, p->hd_id);
8401 +               if (new_bindex == bindex)
8402 +                       continue;
8403 +
8404 +               if (dinfo->di_bwh == bindex)
8405 +                       bwh = new_bindex;
8406 +               if (dinfo->di_bdiropq == bindex)
8407 +                       bdiropq = new_bindex;
8408 +               if (new_bindex < 0) {
8409 +                       au_hdput(p);
8410 +                       p->hd_dentry = NULL;
8411 +                       continue;
8412 +               }
8413 +
8414 +               /* swap two lower dentries, and loop again */
8415 +               q = au_hdentry(dinfo, new_bindex);
8416 +               tmp = *q;
8417 +               *q = *p;
8418 +               *p = tmp;
8419 +               if (tmp.hd_dentry) {
8420 +                       bindex--;
8421 +                       p--;
8422 +               }
8423 +       }
8424 +
8425 +       dinfo->di_bwh = -1;
8426 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8427 +               dinfo->di_bwh = bwh;
8428 +
8429 +       dinfo->di_bdiropq = -1;
8430 +       if (bdiropq >= 0
8431 +           && bdiropq <= au_sbbot(sb)
8432 +           && au_sbr_whable(sb, bdiropq))
8433 +               dinfo->di_bdiropq = bdiropq;
8434 +
8435 +       err = -EIO;
8436 +       dinfo->di_btop = -1;
8437 +       dinfo->di_bbot = -1;
8438 +       bbot = au_dbbot(parent);
8439 +       bindex = 0;
8440 +       p = au_hdentry(dinfo, bindex);
8441 +       for (; bindex <= bbot; bindex++, p++)
8442 +               if (p->hd_dentry) {
8443 +                       dinfo->di_btop = bindex;
8444 +                       break;
8445 +               }
8446 +
8447 +       if (dinfo->di_btop >= 0) {
8448 +               bindex = bbot;
8449 +               p = au_hdentry(dinfo, bindex);
8450 +               for (; bindex >= 0; bindex--, p--)
8451 +                       if (p->hd_dentry) {
8452 +                               dinfo->di_bbot = bindex;
8453 +                               err = 0;
8454 +                               break;
8455 +                       }
8456 +       }
8457 +
8458 +       return err;
8459 +}
8460 +
8461 +static void au_do_hide(struct dentry *dentry)
8462 +{
8463 +       struct inode *inode;
8464 +
8465 +       if (d_really_is_positive(dentry)) {
8466 +               inode = d_inode(dentry);
8467 +               if (!d_is_dir(dentry)) {
8468 +                       if (inode->i_nlink && !d_unhashed(dentry))
8469 +                               drop_nlink(inode);
8470 +               } else {
8471 +                       clear_nlink(inode);
8472 +                       /* stop next lookup */
8473 +                       inode->i_flags |= S_DEAD;
8474 +               }
8475 +               smp_mb(); /* necessary? */
8476 +       }
8477 +       d_drop(dentry);
8478 +}
8479 +
8480 +static int au_hide_children(struct dentry *parent)
8481 +{
8482 +       int err, i, j, ndentry;
8483 +       struct au_dcsub_pages dpages;
8484 +       struct au_dpage *dpage;
8485 +       struct dentry *dentry;
8486 +
8487 +       err = au_dpages_init(&dpages, GFP_NOFS);
8488 +       if (unlikely(err))
8489 +               goto out;
8490 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8491 +       if (unlikely(err))
8492 +               goto out_dpages;
8493 +
8494 +       /* in reverse order */
8495 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8496 +               dpage = dpages.dpages + i;
8497 +               ndentry = dpage->ndentry;
8498 +               for (j = ndentry - 1; j >= 0; j--) {
8499 +                       dentry = dpage->dentries[j];
8500 +                       if (dentry != parent)
8501 +                               au_do_hide(dentry);
8502 +               }
8503 +       }
8504 +
8505 +out_dpages:
8506 +       au_dpages_free(&dpages);
8507 +out:
8508 +       return err;
8509 +}
8510 +
8511 +static void au_hide(struct dentry *dentry)
8512 +{
8513 +       int err;
8514 +
8515 +       AuDbgDentry(dentry);
8516 +       if (d_is_dir(dentry)) {
8517 +               /* shrink_dcache_parent(dentry); */
8518 +               err = au_hide_children(dentry);
8519 +               if (unlikely(err))
8520 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8521 +                               dentry, err);
8522 +       }
8523 +       au_do_hide(dentry);
8524 +}
8525 +
8526 +/*
8527 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8528 + *
8529 + * a dirty branch is added
8530 + * - on the top of layers
8531 + * - in the middle of layers
8532 + * - to the bottom of layers
8533 + *
8534 + * on the added branch there exists
8535 + * - a whiteout
8536 + * - a diropq
8537 + * - a same named entry
8538 + *   + exist
8539 + *     * negative --> positive
8540 + *     * positive --> positive
8541 + *      - type is unchanged
8542 + *      - type is changed
8543 + *   + doesn't exist
8544 + *     * negative --> negative
8545 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8546 + * - none
8547 + */
8548 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8549 +                              struct au_dinfo *tmp)
8550 +{
8551 +       int err;
8552 +       aufs_bindex_t bindex, bbot;
8553 +       struct {
8554 +               struct dentry *dentry;
8555 +               struct inode *inode;
8556 +               mode_t mode;
8557 +       } orig_h, tmp_h = {
8558 +               .dentry = NULL
8559 +       };
8560 +       struct au_hdentry *hd;
8561 +       struct inode *inode, *h_inode;
8562 +       struct dentry *h_dentry;
8563 +
8564 +       err = 0;
8565 +       AuDebugOn(dinfo->di_btop < 0);
8566 +       orig_h.mode = 0;
8567 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8568 +       orig_h.inode = NULL;
8569 +       if (d_is_positive(orig_h.dentry)) {
8570 +               orig_h.inode = d_inode(orig_h.dentry);
8571 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8572 +       }
8573 +       if (tmp->di_btop >= 0) {
8574 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8575 +               if (d_is_positive(tmp_h.dentry)) {
8576 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8577 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8578 +               }
8579 +       }
8580 +
8581 +       inode = NULL;
8582 +       if (d_really_is_positive(dentry))
8583 +               inode = d_inode(dentry);
8584 +       if (!orig_h.inode) {
8585 +               AuDbg("negative originally\n");
8586 +               if (inode) {
8587 +                       au_hide(dentry);
8588 +                       goto out;
8589 +               }
8590 +               AuDebugOn(inode);
8591 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8592 +               AuDebugOn(dinfo->di_bdiropq != -1);
8593 +
8594 +               if (!tmp_h.inode) {
8595 +                       AuDbg("negative --> negative\n");
8596 +                       /* should have only one negative lower */
8597 +                       if (tmp->di_btop >= 0
8598 +                           && tmp->di_btop < dinfo->di_btop) {
8599 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8600 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8601 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8602 +                               au_di_cp(dinfo, tmp);
8603 +                               hd = au_hdentry(tmp, tmp->di_btop);
8604 +                               au_set_h_dptr(dentry, tmp->di_btop,
8605 +                                             dget(hd->hd_dentry));
8606 +                       }
8607 +                       au_dbg_verify_dinode(dentry);
8608 +               } else {
8609 +                       AuDbg("negative --> positive\n");
8610 +                       /*
8611 +                        * similar to the behaviour of creating with bypassing
8612 +                        * aufs.
8613 +                        * unhash it in order to force an error in the
8614 +                        * succeeding create operation.
8615 +                        * we should not set S_DEAD here.
8616 +                        */
8617 +                       d_drop(dentry);
8618 +                       /* au_di_swap(tmp, dinfo); */
8619 +                       au_dbg_verify_dinode(dentry);
8620 +               }
8621 +       } else {
8622 +               AuDbg("positive originally\n");
8623 +               /* inode may be NULL */
8624 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8625 +               if (!tmp_h.inode) {
8626 +                       AuDbg("positive --> negative\n");
8627 +                       /* or bypassing aufs */
8628 +                       au_hide(dentry);
8629 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8630 +                               dinfo->di_bwh = tmp->di_bwh;
8631 +                       if (inode)
8632 +                               err = au_refresh_hinode_self(inode);
8633 +                       au_dbg_verify_dinode(dentry);
8634 +               } else if (orig_h.mode == tmp_h.mode) {
8635 +                       AuDbg("positive --> positive, same type\n");
8636 +                       if (!S_ISDIR(orig_h.mode)
8637 +                           && dinfo->di_btop > tmp->di_btop) {
8638 +                               /*
8639 +                                * similar to the behaviour of removing and
8640 +                                * creating.
8641 +                                */
8642 +                               au_hide(dentry);
8643 +                               if (inode)
8644 +                                       err = au_refresh_hinode_self(inode);
8645 +                               au_dbg_verify_dinode(dentry);
8646 +                       } else {
8647 +                               /* fill empty slots */
8648 +                               if (dinfo->di_btop > tmp->di_btop)
8649 +                                       dinfo->di_btop = tmp->di_btop;
8650 +                               if (dinfo->di_bbot < tmp->di_bbot)
8651 +                                       dinfo->di_bbot = tmp->di_bbot;
8652 +                               dinfo->di_bwh = tmp->di_bwh;
8653 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8654 +                               bbot = dinfo->di_bbot;
8655 +                               bindex = tmp->di_btop;
8656 +                               hd = au_hdentry(tmp, bindex);
8657 +                               for (; bindex <= bbot; bindex++, hd++) {
8658 +                                       if (au_h_dptr(dentry, bindex))
8659 +                                               continue;
8660 +                                       h_dentry = hd->hd_dentry;
8661 +                                       if (!h_dentry)
8662 +                                               continue;
8663 +                                       AuDebugOn(d_is_negative(h_dentry));
8664 +                                       h_inode = d_inode(h_dentry);
8665 +                                       AuDebugOn(orig_h.mode
8666 +                                                 != (h_inode->i_mode
8667 +                                                     & S_IFMT));
8668 +                                       au_set_h_dptr(dentry, bindex,
8669 +                                                     dget(h_dentry));
8670 +                               }
8671 +                               if (inode)
8672 +                                       err = au_refresh_hinode(inode, dentry);
8673 +                               au_dbg_verify_dinode(dentry);
8674 +                       }
8675 +               } else {
8676 +                       AuDbg("positive --> positive, different type\n");
8677 +                       /* similar to the behaviour of removing and creating */
8678 +                       au_hide(dentry);
8679 +                       if (inode)
8680 +                               err = au_refresh_hinode_self(inode);
8681 +                       au_dbg_verify_dinode(dentry);
8682 +               }
8683 +       }
8684 +
8685 +out:
8686 +       return err;
8687 +}
8688 +
8689 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8690 +{
8691 +       const struct dentry_operations *dop
8692 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8693 +       static const unsigned int mask
8694 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8695 +
8696 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8697 +
8698 +       if (dentry->d_op == dop)
8699 +               return;
8700 +
8701 +       AuDbg("%pd\n", dentry);
8702 +       spin_lock(&dentry->d_lock);
8703 +       if (dop == &aufs_dop)
8704 +               dentry->d_flags |= mask;
8705 +       else
8706 +               dentry->d_flags &= ~mask;
8707 +       dentry->d_op = dop;
8708 +       spin_unlock(&dentry->d_lock);
8709 +}
8710 +
8711 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8712 +{
8713 +       int err, ebrange, nbr;
8714 +       unsigned int sigen;
8715 +       struct au_dinfo *dinfo, *tmp;
8716 +       struct super_block *sb;
8717 +       struct inode *inode;
8718 +
8719 +       DiMustWriteLock(dentry);
8720 +       AuDebugOn(IS_ROOT(dentry));
8721 +       AuDebugOn(d_really_is_negative(parent));
8722 +
8723 +       sb = dentry->d_sb;
8724 +       sigen = au_sigen(sb);
8725 +       err = au_digen_test(parent, sigen);
8726 +       if (unlikely(err))
8727 +               goto out;
8728 +
8729 +       nbr = au_sbbot(sb) + 1;
8730 +       dinfo = au_di(dentry);
8731 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8732 +       if (unlikely(err))
8733 +               goto out;
8734 +       ebrange = au_dbrange_test(dentry);
8735 +       if (!ebrange)
8736 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8737 +
8738 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8739 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8740 +               if (d_really_is_positive(dentry)) {
8741 +                       inode = d_inode(dentry);
8742 +                       err = au_refresh_hinode_self(inode);
8743 +               }
8744 +               au_dbg_verify_dinode(dentry);
8745 +               if (!err)
8746 +                       goto out_dgen; /* success */
8747 +               goto out;
8748 +       }
8749 +
8750 +       /* temporary dinfo */
8751 +       AuDbgDentry(dentry);
8752 +       err = -ENOMEM;
8753 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8754 +       if (unlikely(!tmp))
8755 +               goto out;
8756 +       au_di_swap(tmp, dinfo);
8757 +       /* returns the number of positive dentries */
8758 +       /*
8759 +        * if current working dir is removed, it returns an error.
8760 +        * but the dentry is legal.
8761 +        */
8762 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8763 +       AuDbgDentry(dentry);
8764 +       au_di_swap(tmp, dinfo);
8765 +       if (err == -ENOENT)
8766 +               err = 0;
8767 +       if (err >= 0) {
8768 +               /* compare/refresh by dinfo */
8769 +               AuDbgDentry(dentry);
8770 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8771 +               au_dbg_verify_dinode(dentry);
8772 +               AuTraceErr(err);
8773 +       }
8774 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8775 +       au_rw_write_unlock(&tmp->di_rwsem);
8776 +       au_di_free(tmp);
8777 +       if (unlikely(err))
8778 +               goto out;
8779 +
8780 +out_dgen:
8781 +       au_update_digen(dentry);
8782 +out:
8783 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8784 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8785 +               AuDbgDentry(dentry);
8786 +       }
8787 +       AuTraceErr(err);
8788 +       return err;
8789 +}
8790 +
8791 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8792 +                          struct dentry *dentry, aufs_bindex_t bindex)
8793 +{
8794 +       int err, valid;
8795 +
8796 +       err = 0;
8797 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8798 +               goto out;
8799 +
8800 +       AuDbg("b%d\n", bindex);
8801 +       /*
8802 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8803 +        * due to whiteout and branch permission.
8804 +        */
8805 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8806 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8807 +       /* it may return tri-state */
8808 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8809 +
8810 +       if (unlikely(valid < 0))
8811 +               err = valid;
8812 +       else if (!valid)
8813 +               err = -EINVAL;
8814 +
8815 +out:
8816 +       AuTraceErr(err);
8817 +       return err;
8818 +}
8819 +
8820 +/* todo: remove this */
8821 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8822 +                         unsigned int flags, int do_udba, int dirren)
8823 +{
8824 +       int err;
8825 +       umode_t mode, h_mode;
8826 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8827 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8828 +       struct inode *h_inode, *h_cached_inode;
8829 +       struct dentry *h_dentry;
8830 +       struct qstr *name, *h_name;
8831 +
8832 +       err = 0;
8833 +       plus = 0;
8834 +       mode = 0;
8835 +       ibs = -1;
8836 +       ibe = -1;
8837 +       unhashed = !!d_unhashed(dentry);
8838 +       is_root = !!IS_ROOT(dentry);
8839 +       name = &dentry->d_name;
8840 +       tmpfile = au_di(dentry)->di_tmpfile;
8841 +
8842 +       /*
8843 +        * Theoretically, REVAL test should be unnecessary in case of
8844 +        * {FS,I}NOTIFY.
8845 +        * But {fs,i}notify doesn't fire some necessary events,
8846 +        *      IN_ATTRIB for atime/nlink/pageio
8847 +        * Let's do REVAL test too.
8848 +        */
8849 +       if (do_udba && inode) {
8850 +               mode = (inode->i_mode & S_IFMT);
8851 +               plus = (inode->i_nlink > 0);
8852 +               ibs = au_ibtop(inode);
8853 +               ibe = au_ibbot(inode);
8854 +       }
8855 +
8856 +       btop = au_dbtop(dentry);
8857 +       btail = btop;
8858 +       if (inode && S_ISDIR(inode->i_mode))
8859 +               btail = au_dbtaildir(dentry);
8860 +       for (bindex = btop; bindex <= btail; bindex++) {
8861 +               h_dentry = au_h_dptr(dentry, bindex);
8862 +               if (!h_dentry)
8863 +                       continue;
8864 +
8865 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8866 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8867 +               spin_lock(&h_dentry->d_lock);
8868 +               h_name = &h_dentry->d_name;
8869 +               if (unlikely(do_udba
8870 +                            && !is_root
8871 +                            && ((!h_nfs
8872 +                                 && (unhashed != !!d_unhashed(h_dentry)
8873 +                                     || (!tmpfile && !dirren
8874 +                                         && !au_qstreq(name, h_name))
8875 +                                         ))
8876 +                                || (h_nfs
8877 +                                    && !(flags & LOOKUP_OPEN)
8878 +                                    && (h_dentry->d_flags
8879 +                                        & DCACHE_NFSFS_RENAMED)))
8880 +                           )) {
8881 +                       int h_unhashed;
8882 +
8883 +                       h_unhashed = d_unhashed(h_dentry);
8884 +                       spin_unlock(&h_dentry->d_lock);
8885 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8886 +                             unhashed, h_unhashed, dentry, h_dentry);
8887 +                       goto err;
8888 +               }
8889 +               spin_unlock(&h_dentry->d_lock);
8890 +
8891 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8892 +               if (unlikely(err))
8893 +                       /* do not goto err, to keep the errno */
8894 +                       break;
8895 +
8896 +               /* todo: plink too? */
8897 +               if (!do_udba)
8898 +                       continue;
8899 +
8900 +               /* UDBA tests */
8901 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8902 +                       goto err;
8903 +
8904 +               h_inode = NULL;
8905 +               if (d_is_positive(h_dentry))
8906 +                       h_inode = d_inode(h_dentry);
8907 +               h_plus = plus;
8908 +               h_mode = mode;
8909 +               h_cached_inode = h_inode;
8910 +               if (h_inode) {
8911 +                       h_mode = (h_inode->i_mode & S_IFMT);
8912 +                       h_plus = (h_inode->i_nlink > 0);
8913 +               }
8914 +               if (inode && ibs <= bindex && bindex <= ibe)
8915 +                       h_cached_inode = au_h_iptr(inode, bindex);
8916 +
8917 +               if (!h_nfs) {
8918 +                       if (unlikely(plus != h_plus && !tmpfile))
8919 +                               goto err;
8920 +               } else {
8921 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8922 +                                    && !is_root
8923 +                                    && !IS_ROOT(h_dentry)
8924 +                                    && unhashed != d_unhashed(h_dentry)))
8925 +                               goto err;
8926 +               }
8927 +               if (unlikely(mode != h_mode
8928 +                            || h_cached_inode != h_inode))
8929 +                       goto err;
8930 +               continue;
8931 +
8932 +err:
8933 +               err = -EINVAL;
8934 +               break;
8935 +       }
8936 +
8937 +       AuTraceErr(err);
8938 +       return err;
8939 +}
8940 +
8941 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8942 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8943 +{
8944 +       int err;
8945 +       struct dentry *parent;
8946 +
8947 +       if (!au_digen_test(dentry, sigen))
8948 +               return 0;
8949 +
8950 +       parent = dget_parent(dentry);
8951 +       di_read_lock_parent(parent, AuLock_IR);
8952 +       AuDebugOn(au_digen_test(parent, sigen));
8953 +       au_dbg_verify_gen(parent, sigen);
8954 +       err = au_refresh_dentry(dentry, parent);
8955 +       di_read_unlock(parent, AuLock_IR);
8956 +       dput(parent);
8957 +       AuTraceErr(err);
8958 +       return err;
8959 +}
8960 +
8961 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8962 +{
8963 +       int err;
8964 +       struct dentry *d, *parent;
8965 +
8966 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8967 +               return simple_reval_dpath(dentry, sigen);
8968 +
8969 +       /* slow loop, keep it simple and stupid */
8970 +       /* cf: au_cpup_dirs() */
8971 +       err = 0;
8972 +       parent = NULL;
8973 +       while (au_digen_test(dentry, sigen)) {
8974 +               d = dentry;
8975 +               while (1) {
8976 +                       dput(parent);
8977 +                       parent = dget_parent(d);
8978 +                       if (!au_digen_test(parent, sigen))
8979 +                               break;
8980 +                       d = parent;
8981 +               }
8982 +
8983 +               if (d != dentry)
8984 +                       di_write_lock_child2(d);
8985 +
8986 +               /* someone might update our dentry while we were sleeping */
8987 +               if (au_digen_test(d, sigen)) {
8988 +                       /*
8989 +                        * todo: consolidate with simple_reval_dpath(),
8990 +                        * do_refresh() and au_reval_for_attr().
8991 +                        */
8992 +                       di_read_lock_parent(parent, AuLock_IR);
8993 +                       err = au_refresh_dentry(d, parent);
8994 +                       di_read_unlock(parent, AuLock_IR);
8995 +               }
8996 +
8997 +               if (d != dentry)
8998 +                       di_write_unlock(d);
8999 +               dput(parent);
9000 +               if (unlikely(err))
9001 +                       break;
9002 +       }
9003 +
9004 +       return err;
9005 +}
9006 +
9007 +/*
9008 + * if valid returns 1, otherwise 0.
9009 + */
9010 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9011 +{
9012 +       int valid, err;
9013 +       unsigned int sigen;
9014 +       unsigned char do_udba, dirren;
9015 +       struct super_block *sb;
9016 +       struct inode *inode;
9017 +
9018 +       /* todo: support rcu-walk? */
9019 +       if (flags & LOOKUP_RCU)
9020 +               return -ECHILD;
9021 +
9022 +       valid = 0;
9023 +       if (unlikely(!au_di(dentry)))
9024 +               goto out;
9025 +
9026 +       valid = 1;
9027 +       sb = dentry->d_sb;
9028 +       /*
9029 +        * todo: very ugly
9030 +        * i_mutex of parent dir may be held,
9031 +        * but we should not return 'invalid' due to busy.
9032 +        */
9033 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9034 +       if (unlikely(err)) {
9035 +               valid = err;
9036 +               AuTraceErr(err);
9037 +               goto out;
9038 +       }
9039 +       inode = NULL;
9040 +       if (d_really_is_positive(dentry))
9041 +               inode = d_inode(dentry);
9042 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9043 +               err = -EINVAL;
9044 +               AuTraceErr(err);
9045 +               goto out_dgrade;
9046 +       }
9047 +       if (unlikely(au_dbrange_test(dentry))) {
9048 +               err = -EINVAL;
9049 +               AuTraceErr(err);
9050 +               goto out_dgrade;
9051 +       }
9052 +
9053 +       sigen = au_sigen(sb);
9054 +       if (au_digen_test(dentry, sigen)) {
9055 +               AuDebugOn(IS_ROOT(dentry));
9056 +               err = au_reval_dpath(dentry, sigen);
9057 +               if (unlikely(err)) {
9058 +                       AuTraceErr(err);
9059 +                       goto out_dgrade;
9060 +               }
9061 +       }
9062 +       di_downgrade_lock(dentry, AuLock_IR);
9063 +
9064 +       err = -EINVAL;
9065 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9066 +           && inode
9067 +           && !(inode->i_state && I_LINKABLE)
9068 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9069 +               AuTraceErr(err);
9070 +               goto out_inval;
9071 +       }
9072 +
9073 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9074 +       if (do_udba && inode) {
9075 +               aufs_bindex_t btop = au_ibtop(inode);
9076 +               struct inode *h_inode;
9077 +
9078 +               if (btop >= 0) {
9079 +                       h_inode = au_h_iptr(inode, btop);
9080 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9081 +                               AuTraceErr(err);
9082 +                               goto out_inval;
9083 +                       }
9084 +               }
9085 +       }
9086 +
9087 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9088 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9089 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9090 +               err = -EIO;
9091 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9092 +                     dentry, err);
9093 +       }
9094 +       goto out_inval;
9095 +
9096 +out_dgrade:
9097 +       di_downgrade_lock(dentry, AuLock_IR);
9098 +out_inval:
9099 +       aufs_read_unlock(dentry, AuLock_IR);
9100 +       AuTraceErr(err);
9101 +       valid = !err;
9102 +out:
9103 +       if (!valid) {
9104 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9105 +               d_drop(dentry);
9106 +       }
9107 +       return valid;
9108 +}
9109 +
9110 +static void aufs_d_release(struct dentry *dentry)
9111 +{
9112 +       if (au_di(dentry)) {
9113 +               au_di_fin(dentry);
9114 +               au_hn_di_reinit(dentry);
9115 +       }
9116 +}
9117 +
9118 +const struct dentry_operations aufs_dop = {
9119 +       .d_revalidate           = aufs_d_revalidate,
9120 +       .d_weak_revalidate      = aufs_d_revalidate,
9121 +       .d_release              = aufs_d_release
9122 +};
9123 +
9124 +/* aufs_dop without d_revalidate */
9125 +const struct dentry_operations aufs_dop_noreval = {
9126 +       .d_release              = aufs_d_release
9127 +};
9128 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9129 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9130 +++ linux/fs/aufs/dentry.h      2021-11-01 23:48:34.206359262 +0100
9131 @@ -0,0 +1,269 @@
9132 +/* SPDX-License-Identifier: GPL-2.0 */
9133 +/*
9134 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9135 + *
9136 + * This program, aufs is free software; you can redistribute it and/or modify
9137 + * it under the terms of the GNU General Public License as published by
9138 + * the Free Software Foundation; either version 2 of the License, or
9139 + * (at your option) any later version.
9140 + *
9141 + * This program is distributed in the hope that it will be useful,
9142 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9143 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9144 + * GNU General Public License for more details.
9145 + *
9146 + * You should have received a copy of the GNU General Public License
9147 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9148 + */
9149 +
9150 +/*
9151 + * lookup and dentry operations
9152 + */
9153 +
9154 +#ifndef __AUFS_DENTRY_H__
9155 +#define __AUFS_DENTRY_H__
9156 +
9157 +#ifdef __KERNEL__
9158 +
9159 +#include <linux/dcache.h>
9160 +#include "dirren.h"
9161 +#include "rwsem.h"
9162 +
9163 +struct au_hdentry {
9164 +       struct dentry           *hd_dentry;
9165 +       aufs_bindex_t           hd_id;
9166 +};
9167 +
9168 +struct au_dinfo {
9169 +       atomic_t                di_generation;
9170 +
9171 +       struct au_rwsem         di_rwsem;
9172 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9173 +       unsigned char           di_tmpfile; /* to allow the different name */
9174 +       struct au_hdentry       *di_hdentry;
9175 +       struct rcu_head         rcu;
9176 +} ____cacheline_aligned_in_smp;
9177 +
9178 +/* ---------------------------------------------------------------------- */
9179 +
9180 +/* flags for au_lkup_dentry() */
9181 +#define AuLkup_ALLOW_NEG       1
9182 +#define AuLkup_IGNORE_PERM     (1 << 1)
9183 +#define AuLkup_DIRREN          (1 << 2)
9184 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9185 +#define au_fset_lkup(flags, name) \
9186 +       do { (flags) |= AuLkup_##name; } while (0)
9187 +#define au_fclr_lkup(flags, name) \
9188 +       do { (flags) &= ~AuLkup_##name; } while (0)
9189 +
9190 +#ifndef CONFIG_AUFS_DIRREN
9191 +#undef AuLkup_DIRREN
9192 +#define AuLkup_DIRREN 0
9193 +#endif
9194 +
9195 +struct au_do_lookup_args {
9196 +       unsigned int            flags;
9197 +       mode_t                  type;
9198 +       struct qstr             whname, *name;
9199 +       struct au_dr_lookup     dirren;
9200 +};
9201 +
9202 +/* ---------------------------------------------------------------------- */
9203 +
9204 +/* dentry.c */
9205 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9206 +struct au_branch;
9207 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9208 +                              struct path *ppath);
9209 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9210 +               struct dentry *h_parent, struct au_branch *br);
9211 +
9212 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9213 +                  unsigned int flags);
9214 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9215 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9216 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9217 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9218 +
9219 +/* dinfo.c */
9220 +void au_di_init_once(void *_di);
9221 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9222 +void au_di_free(struct au_dinfo *dinfo);
9223 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9224 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9225 +int au_di_init(struct dentry *dentry);
9226 +void au_di_fin(struct dentry *dentry);
9227 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9228 +
9229 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9230 +void di_read_unlock(struct dentry *d, int flags);
9231 +void di_downgrade_lock(struct dentry *d, int flags);
9232 +void di_write_lock(struct dentry *d, unsigned int lsc);
9233 +void di_write_unlock(struct dentry *d);
9234 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9235 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9236 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9237 +
9238 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9239 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9240 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9241 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9242 +
9243 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9244 +                  struct dentry *h_dentry);
9245 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9246 +int au_dbrange_test(struct dentry *dentry);
9247 +void au_update_digen(struct dentry *dentry);
9248 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9249 +void au_update_dbtop(struct dentry *dentry);
9250 +void au_update_dbbot(struct dentry *dentry);
9251 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9252 +
9253 +/* ---------------------------------------------------------------------- */
9254 +
9255 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9256 +{
9257 +       return dentry->d_fsdata;
9258 +}
9259 +
9260 +/* ---------------------------------------------------------------------- */
9261 +
9262 +/* lock subclass for dinfo */
9263 +enum {
9264 +       AuLsc_DI_CHILD,         /* child first */
9265 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9266 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9267 +       AuLsc_DI_PARENT,
9268 +       AuLsc_DI_PARENT2,
9269 +       AuLsc_DI_PARENT3,
9270 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9271 +};
9272 +
9273 +/*
9274 + * di_read_lock_child, di_write_lock_child,
9275 + * di_read_lock_child2, di_write_lock_child2,
9276 + * di_read_lock_child3, di_write_lock_child3,
9277 + * di_read_lock_parent, di_write_lock_parent,
9278 + * di_read_lock_parent2, di_write_lock_parent2,
9279 + * di_read_lock_parent3, di_write_lock_parent3,
9280 + */
9281 +#define AuReadLockFunc(name, lsc) \
9282 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9283 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9284 +
9285 +#define AuWriteLockFunc(name, lsc) \
9286 +static inline void di_write_lock_##name(struct dentry *d) \
9287 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9288 +
9289 +#define AuRWLockFuncs(name, lsc) \
9290 +       AuReadLockFunc(name, lsc) \
9291 +       AuWriteLockFunc(name, lsc)
9292 +
9293 +AuRWLockFuncs(child, CHILD);
9294 +AuRWLockFuncs(child2, CHILD2);
9295 +AuRWLockFuncs(child3, CHILD3);
9296 +AuRWLockFuncs(parent, PARENT);
9297 +AuRWLockFuncs(parent2, PARENT2);
9298 +AuRWLockFuncs(parent3, PARENT3);
9299 +
9300 +#undef AuReadLockFunc
9301 +#undef AuWriteLockFunc
9302 +#undef AuRWLockFuncs
9303 +
9304 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9305 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9306 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9307 +
9308 +/* ---------------------------------------------------------------------- */
9309 +
9310 +/* todo: memory barrier? */
9311 +static inline unsigned int au_digen(struct dentry *d)
9312 +{
9313 +       return atomic_read(&au_di(d)->di_generation);
9314 +}
9315 +
9316 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9317 +{
9318 +       hdentry->hd_dentry = NULL;
9319 +}
9320 +
9321 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9322 +                                           aufs_bindex_t bindex)
9323 +{
9324 +       return di->di_hdentry + bindex;
9325 +}
9326 +
9327 +static inline void au_hdput(struct au_hdentry *hd)
9328 +{
9329 +       if (hd)
9330 +               dput(hd->hd_dentry);
9331 +}
9332 +
9333 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9334 +{
9335 +       DiMustAnyLock(dentry);
9336 +       return au_di(dentry)->di_btop;
9337 +}
9338 +
9339 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9340 +{
9341 +       DiMustAnyLock(dentry);
9342 +       return au_di(dentry)->di_bbot;
9343 +}
9344 +
9345 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9346 +{
9347 +       DiMustAnyLock(dentry);
9348 +       return au_di(dentry)->di_bwh;
9349 +}
9350 +
9351 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9352 +{
9353 +       DiMustAnyLock(dentry);
9354 +       return au_di(dentry)->di_bdiropq;
9355 +}
9356 +
9357 +/* todo: hard/soft set? */
9358 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9359 +{
9360 +       DiMustWriteLock(dentry);
9361 +       au_di(dentry)->di_btop = bindex;
9362 +}
9363 +
9364 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9365 +{
9366 +       DiMustWriteLock(dentry);
9367 +       au_di(dentry)->di_bbot = bindex;
9368 +}
9369 +
9370 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9371 +{
9372 +       DiMustWriteLock(dentry);
9373 +       /* dbwh can be outside of btop - bbot range */
9374 +       au_di(dentry)->di_bwh = bindex;
9375 +}
9376 +
9377 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9378 +{
9379 +       DiMustWriteLock(dentry);
9380 +       au_di(dentry)->di_bdiropq = bindex;
9381 +}
9382 +
9383 +/* ---------------------------------------------------------------------- */
9384 +
9385 +#ifdef CONFIG_AUFS_HNOTIFY
9386 +static inline void au_digen_dec(struct dentry *d)
9387 +{
9388 +       atomic_dec(&au_di(d)->di_generation);
9389 +}
9390 +
9391 +static inline void au_hn_di_reinit(struct dentry *dentry)
9392 +{
9393 +       dentry->d_fsdata = NULL;
9394 +}
9395 +#else
9396 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9397 +#endif /* CONFIG_AUFS_HNOTIFY */
9398 +
9399 +#endif /* __KERNEL__ */
9400 +#endif /* __AUFS_DENTRY_H__ */
9401 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9402 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9403 +++ linux/fs/aufs/dinfo.c       2021-11-01 23:48:34.206359262 +0100
9404 @@ -0,0 +1,554 @@
9405 +// SPDX-License-Identifier: GPL-2.0
9406 +/*
9407 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9408 + *
9409 + * This program, aufs is free software; you can redistribute it and/or modify
9410 + * it under the terms of the GNU General Public License as published by
9411 + * the Free Software Foundation; either version 2 of the License, or
9412 + * (at your option) any later version.
9413 + *
9414 + * This program is distributed in the hope that it will be useful,
9415 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9416 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9417 + * GNU General Public License for more details.
9418 + *
9419 + * You should have received a copy of the GNU General Public License
9420 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9421 + */
9422 +
9423 +/*
9424 + * dentry private data
9425 + */
9426 +
9427 +#include "aufs.h"
9428 +
9429 +void au_di_init_once(void *_dinfo)
9430 +{
9431 +       struct au_dinfo *dinfo = _dinfo;
9432 +
9433 +       au_rw_init(&dinfo->di_rwsem);
9434 +}
9435 +
9436 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9437 +{
9438 +       struct au_dinfo *dinfo;
9439 +       int nbr, i;
9440 +
9441 +       dinfo = au_cache_alloc_dinfo();
9442 +       if (unlikely(!dinfo))
9443 +               goto out;
9444 +
9445 +       nbr = au_sbbot(sb) + 1;
9446 +       if (nbr <= 0)
9447 +               nbr = 1;
9448 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9449 +       if (dinfo->di_hdentry) {
9450 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9451 +               dinfo->di_btop = -1;
9452 +               dinfo->di_bbot = -1;
9453 +               dinfo->di_bwh = -1;
9454 +               dinfo->di_bdiropq = -1;
9455 +               dinfo->di_tmpfile = 0;
9456 +               for (i = 0; i < nbr; i++)
9457 +                       dinfo->di_hdentry[i].hd_id = -1;
9458 +               goto out;
9459 +       }
9460 +
9461 +       au_cache_free_dinfo(dinfo);
9462 +       dinfo = NULL;
9463 +
9464 +out:
9465 +       return dinfo;
9466 +}
9467 +
9468 +void au_di_free(struct au_dinfo *dinfo)
9469 +{
9470 +       struct au_hdentry *p;
9471 +       aufs_bindex_t bbot, bindex;
9472 +
9473 +       /* dentry may not be revalidated */
9474 +       bindex = dinfo->di_btop;
9475 +       if (bindex >= 0) {
9476 +               bbot = dinfo->di_bbot;
9477 +               p = au_hdentry(dinfo, bindex);
9478 +               while (bindex++ <= bbot)
9479 +                       au_hdput(p++);
9480 +       }
9481 +       au_kfree_try_rcu(dinfo->di_hdentry);
9482 +       au_cache_free_dinfo(dinfo);
9483 +}
9484 +
9485 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9486 +{
9487 +       struct au_hdentry *p;
9488 +       aufs_bindex_t bi;
9489 +
9490 +       AuRwMustWriteLock(&a->di_rwsem);
9491 +       AuRwMustWriteLock(&b->di_rwsem);
9492 +
9493 +#define DiSwap(v, name)                                \
9494 +       do {                                    \
9495 +               v = a->di_##name;               \
9496 +               a->di_##name = b->di_##name;    \
9497 +               b->di_##name = v;               \
9498 +       } while (0)
9499 +
9500 +       DiSwap(p, hdentry);
9501 +       DiSwap(bi, btop);
9502 +       DiSwap(bi, bbot);
9503 +       DiSwap(bi, bwh);
9504 +       DiSwap(bi, bdiropq);
9505 +       /* smp_mb(); */
9506 +
9507 +#undef DiSwap
9508 +}
9509 +
9510 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9511 +{
9512 +       AuRwMustWriteLock(&dst->di_rwsem);
9513 +       AuRwMustWriteLock(&src->di_rwsem);
9514 +
9515 +       dst->di_btop = src->di_btop;
9516 +       dst->di_bbot = src->di_bbot;
9517 +       dst->di_bwh = src->di_bwh;
9518 +       dst->di_bdiropq = src->di_bdiropq;
9519 +       /* smp_mb(); */
9520 +}
9521 +
9522 +int au_di_init(struct dentry *dentry)
9523 +{
9524 +       int err;
9525 +       struct super_block *sb;
9526 +       struct au_dinfo *dinfo;
9527 +
9528 +       err = 0;
9529 +       sb = dentry->d_sb;
9530 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9531 +       if (dinfo) {
9532 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9533 +               /* smp_mb(); */ /* atomic_set */
9534 +               dentry->d_fsdata = dinfo;
9535 +       } else
9536 +               err = -ENOMEM;
9537 +
9538 +       return err;
9539 +}
9540 +
9541 +void au_di_fin(struct dentry *dentry)
9542 +{
9543 +       struct au_dinfo *dinfo;
9544 +
9545 +       dinfo = au_di(dentry);
9546 +       AuRwDestroy(&dinfo->di_rwsem);
9547 +       au_di_free(dinfo);
9548 +}
9549 +
9550 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9551 +{
9552 +       int err, sz;
9553 +       struct au_hdentry *hdp;
9554 +
9555 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9556 +
9557 +       err = -ENOMEM;
9558 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9559 +       if (!sz)
9560 +               sz = sizeof(*hdp);
9561 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9562 +                          may_shrink);
9563 +       if (hdp) {
9564 +               dinfo->di_hdentry = hdp;
9565 +               err = 0;
9566 +       }
9567 +
9568 +       return err;
9569 +}
9570 +
9571 +/* ---------------------------------------------------------------------- */
9572 +
9573 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9574 +{
9575 +       switch (lsc) {
9576 +       case AuLsc_DI_CHILD:
9577 +               ii_write_lock_child(inode);
9578 +               break;
9579 +       case AuLsc_DI_CHILD2:
9580 +               ii_write_lock_child2(inode);
9581 +               break;
9582 +       case AuLsc_DI_CHILD3:
9583 +               ii_write_lock_child3(inode);
9584 +               break;
9585 +       case AuLsc_DI_PARENT:
9586 +               ii_write_lock_parent(inode);
9587 +               break;
9588 +       case AuLsc_DI_PARENT2:
9589 +               ii_write_lock_parent2(inode);
9590 +               break;
9591 +       case AuLsc_DI_PARENT3:
9592 +               ii_write_lock_parent3(inode);
9593 +               break;
9594 +       default:
9595 +               BUG();
9596 +       }
9597 +}
9598 +
9599 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9600 +{
9601 +       switch (lsc) {
9602 +       case AuLsc_DI_CHILD:
9603 +               ii_read_lock_child(inode);
9604 +               break;
9605 +       case AuLsc_DI_CHILD2:
9606 +               ii_read_lock_child2(inode);
9607 +               break;
9608 +       case AuLsc_DI_CHILD3:
9609 +               ii_read_lock_child3(inode);
9610 +               break;
9611 +       case AuLsc_DI_PARENT:
9612 +               ii_read_lock_parent(inode);
9613 +               break;
9614 +       case AuLsc_DI_PARENT2:
9615 +               ii_read_lock_parent2(inode);
9616 +               break;
9617 +       case AuLsc_DI_PARENT3:
9618 +               ii_read_lock_parent3(inode);
9619 +               break;
9620 +       default:
9621 +               BUG();
9622 +       }
9623 +}
9624 +
9625 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9626 +{
9627 +       struct inode *inode;
9628 +
9629 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9630 +       if (d_really_is_positive(d)) {
9631 +               inode = d_inode(d);
9632 +               if (au_ftest_lock(flags, IW))
9633 +                       do_ii_write_lock(inode, lsc);
9634 +               else if (au_ftest_lock(flags, IR))
9635 +                       do_ii_read_lock(inode, lsc);
9636 +       }
9637 +}
9638 +
9639 +void di_read_unlock(struct dentry *d, int flags)
9640 +{
9641 +       struct inode *inode;
9642 +
9643 +       if (d_really_is_positive(d)) {
9644 +               inode = d_inode(d);
9645 +               if (au_ftest_lock(flags, IW)) {
9646 +                       au_dbg_verify_dinode(d);
9647 +                       ii_write_unlock(inode);
9648 +               } else if (au_ftest_lock(flags, IR)) {
9649 +                       au_dbg_verify_dinode(d);
9650 +                       ii_read_unlock(inode);
9651 +               }
9652 +       }
9653 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9654 +}
9655 +
9656 +void di_downgrade_lock(struct dentry *d, int flags)
9657 +{
9658 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9659 +               ii_downgrade_lock(d_inode(d));
9660 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9661 +}
9662 +
9663 +void di_write_lock(struct dentry *d, unsigned int lsc)
9664 +{
9665 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9666 +       if (d_really_is_positive(d))
9667 +               do_ii_write_lock(d_inode(d), lsc);
9668 +}
9669 +
9670 +void di_write_unlock(struct dentry *d)
9671 +{
9672 +       au_dbg_verify_dinode(d);
9673 +       if (d_really_is_positive(d))
9674 +               ii_write_unlock(d_inode(d));
9675 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9676 +}
9677 +
9678 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9679 +{
9680 +       AuDebugOn(d1 == d2
9681 +                 || d_inode(d1) == d_inode(d2)
9682 +                 || d1->d_sb != d2->d_sb);
9683 +
9684 +       if ((isdir && au_test_subdir(d1, d2))
9685 +           || d1 < d2) {
9686 +               di_write_lock_child(d1);
9687 +               di_write_lock_child2(d2);
9688 +       } else {
9689 +               di_write_lock_child(d2);
9690 +               di_write_lock_child2(d1);
9691 +       }
9692 +}
9693 +
9694 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9695 +{
9696 +       AuDebugOn(d1 == d2
9697 +                 || d_inode(d1) == d_inode(d2)
9698 +                 || d1->d_sb != d2->d_sb);
9699 +
9700 +       if ((isdir && au_test_subdir(d1, d2))
9701 +           || d1 < d2) {
9702 +               di_write_lock_parent(d1);
9703 +               di_write_lock_parent2(d2);
9704 +       } else {
9705 +               di_write_lock_parent(d2);
9706 +               di_write_lock_parent2(d1);
9707 +       }
9708 +}
9709 +
9710 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9711 +{
9712 +       di_write_unlock(d1);
9713 +       if (d_inode(d1) == d_inode(d2))
9714 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9715 +       else
9716 +               di_write_unlock(d2);
9717 +}
9718 +
9719 +/* ---------------------------------------------------------------------- */
9720 +
9721 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9722 +{
9723 +       struct dentry *d;
9724 +
9725 +       DiMustAnyLock(dentry);
9726 +
9727 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9728 +               return NULL;
9729 +       AuDebugOn(bindex < 0);
9730 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9731 +       AuDebugOn(d && au_dcount(d) <= 0);
9732 +       return d;
9733 +}
9734 +
9735 +/*
9736 + * extended version of au_h_dptr().
9737 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9738 + * error.
9739 + */
9740 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9741 +{
9742 +       struct dentry *h_dentry;
9743 +       struct inode *inode, *h_inode;
9744 +
9745 +       AuDebugOn(d_really_is_negative(dentry));
9746 +
9747 +       h_dentry = NULL;
9748 +       if (au_dbtop(dentry) <= bindex
9749 +           && bindex <= au_dbbot(dentry))
9750 +               h_dentry = au_h_dptr(dentry, bindex);
9751 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9752 +               dget(h_dentry);
9753 +               goto out; /* success */
9754 +       }
9755 +
9756 +       inode = d_inode(dentry);
9757 +       AuDebugOn(bindex < au_ibtop(inode));
9758 +       AuDebugOn(au_ibbot(inode) < bindex);
9759 +       h_inode = au_h_iptr(inode, bindex);
9760 +       h_dentry = d_find_alias(h_inode);
9761 +       if (h_dentry) {
9762 +               if (!IS_ERR(h_dentry)) {
9763 +                       if (!au_d_linkable(h_dentry))
9764 +                               goto out; /* success */
9765 +                       dput(h_dentry);
9766 +               } else
9767 +                       goto out;
9768 +       }
9769 +
9770 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9771 +               h_dentry = au_plink_lkup(inode, bindex);
9772 +               AuDebugOn(!h_dentry);
9773 +               if (!IS_ERR(h_dentry)) {
9774 +                       if (!au_d_hashed_positive(h_dentry))
9775 +                               goto out; /* success */
9776 +                       dput(h_dentry);
9777 +                       h_dentry = NULL;
9778 +               }
9779 +       }
9780 +
9781 +out:
9782 +       AuDbgDentry(h_dentry);
9783 +       return h_dentry;
9784 +}
9785 +
9786 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9787 +{
9788 +       aufs_bindex_t bbot, bwh;
9789 +
9790 +       bbot = au_dbbot(dentry);
9791 +       if (0 <= bbot) {
9792 +               bwh = au_dbwh(dentry);
9793 +               if (!bwh)
9794 +                       return bwh;
9795 +               if (0 < bwh && bwh < bbot)
9796 +                       return bwh - 1;
9797 +       }
9798 +       return bbot;
9799 +}
9800 +
9801 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9802 +{
9803 +       aufs_bindex_t bbot, bopq;
9804 +
9805 +       bbot = au_dbtail(dentry);
9806 +       if (0 <= bbot) {
9807 +               bopq = au_dbdiropq(dentry);
9808 +               if (0 <= bopq && bopq < bbot)
9809 +                       bbot = bopq;
9810 +       }
9811 +       return bbot;
9812 +}
9813 +
9814 +/* ---------------------------------------------------------------------- */
9815 +
9816 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9817 +                  struct dentry *h_dentry)
9818 +{
9819 +       struct au_dinfo *dinfo;
9820 +       struct au_hdentry *hd;
9821 +       struct au_branch *br;
9822 +
9823 +       DiMustWriteLock(dentry);
9824 +
9825 +       dinfo = au_di(dentry);
9826 +       hd = au_hdentry(dinfo, bindex);
9827 +       au_hdput(hd);
9828 +       hd->hd_dentry = h_dentry;
9829 +       if (h_dentry) {
9830 +               br = au_sbr(dentry->d_sb, bindex);
9831 +               hd->hd_id = br->br_id;
9832 +       }
9833 +}
9834 +
9835 +int au_dbrange_test(struct dentry *dentry)
9836 +{
9837 +       int err;
9838 +       aufs_bindex_t btop, bbot;
9839 +
9840 +       err = 0;
9841 +       btop = au_dbtop(dentry);
9842 +       bbot = au_dbbot(dentry);
9843 +       if (btop >= 0)
9844 +               AuDebugOn(bbot < 0 && btop > bbot);
9845 +       else {
9846 +               err = -EIO;
9847 +               AuDebugOn(bbot >= 0);
9848 +       }
9849 +
9850 +       return err;
9851 +}
9852 +
9853 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9854 +{
9855 +       int err;
9856 +
9857 +       err = 0;
9858 +       if (unlikely(au_digen(dentry) != sigen
9859 +                    || au_iigen_test(d_inode(dentry), sigen)))
9860 +               err = -EIO;
9861 +
9862 +       return err;
9863 +}
9864 +
9865 +void au_update_digen(struct dentry *dentry)
9866 +{
9867 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9868 +       /* smp_mb(); */ /* atomic_set */
9869 +}
9870 +
9871 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9872 +{
9873 +       struct au_dinfo *dinfo;
9874 +       struct dentry *h_d;
9875 +       struct au_hdentry *hdp;
9876 +       aufs_bindex_t bindex, bbot;
9877 +
9878 +       DiMustWriteLock(dentry);
9879 +
9880 +       dinfo = au_di(dentry);
9881 +       if (!dinfo || dinfo->di_btop < 0)
9882 +               return;
9883 +
9884 +       if (do_put_zero) {
9885 +               bbot = dinfo->di_bbot;
9886 +               bindex = dinfo->di_btop;
9887 +               hdp = au_hdentry(dinfo, bindex);
9888 +               for (; bindex <= bbot; bindex++, hdp++) {
9889 +                       h_d = hdp->hd_dentry;
9890 +                       if (h_d && d_is_negative(h_d))
9891 +                               au_set_h_dptr(dentry, bindex, NULL);
9892 +               }
9893 +       }
9894 +
9895 +       dinfo->di_btop = 0;
9896 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9897 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9898 +               if (hdp->hd_dentry)
9899 +                       break;
9900 +       if (dinfo->di_btop > dinfo->di_bbot) {
9901 +               dinfo->di_btop = -1;
9902 +               dinfo->di_bbot = -1;
9903 +               return;
9904 +       }
9905 +
9906 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9907 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9908 +               if (hdp->hd_dentry)
9909 +                       break;
9910 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9911 +}
9912 +
9913 +void au_update_dbtop(struct dentry *dentry)
9914 +{
9915 +       aufs_bindex_t bindex, bbot;
9916 +       struct dentry *h_dentry;
9917 +
9918 +       bbot = au_dbbot(dentry);
9919 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9920 +               h_dentry = au_h_dptr(dentry, bindex);
9921 +               if (!h_dentry)
9922 +                       continue;
9923 +               if (d_is_positive(h_dentry)) {
9924 +                       au_set_dbtop(dentry, bindex);
9925 +                       return;
9926 +               }
9927 +               au_set_h_dptr(dentry, bindex, NULL);
9928 +       }
9929 +}
9930 +
9931 +void au_update_dbbot(struct dentry *dentry)
9932 +{
9933 +       aufs_bindex_t bindex, btop;
9934 +       struct dentry *h_dentry;
9935 +
9936 +       btop = au_dbtop(dentry);
9937 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9938 +               h_dentry = au_h_dptr(dentry, bindex);
9939 +               if (!h_dentry)
9940 +                       continue;
9941 +               if (d_is_positive(h_dentry)) {
9942 +                       au_set_dbbot(dentry, bindex);
9943 +                       return;
9944 +               }
9945 +               au_set_h_dptr(dentry, bindex, NULL);
9946 +       }
9947 +}
9948 +
9949 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9950 +{
9951 +       aufs_bindex_t bindex, bbot;
9952 +
9953 +       bbot = au_dbbot(dentry);
9954 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9955 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9956 +                       return bindex;
9957 +       return -1;
9958 +}
9959 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9960 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9961 +++ linux/fs/aufs/dir.c 2021-11-01 23:48:34.206359262 +0100
9962 @@ -0,0 +1,765 @@
9963 +// SPDX-License-Identifier: GPL-2.0
9964 +/*
9965 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9966 + *
9967 + * This program, aufs is free software; you can redistribute it and/or modify
9968 + * it under the terms of the GNU General Public License as published by
9969 + * the Free Software Foundation; either version 2 of the License, or
9970 + * (at your option) any later version.
9971 + *
9972 + * This program is distributed in the hope that it will be useful,
9973 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9974 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9975 + * GNU General Public License for more details.
9976 + *
9977 + * You should have received a copy of the GNU General Public License
9978 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9979 + */
9980 +
9981 +/*
9982 + * directory operations
9983 + */
9984 +
9985 +#include <linux/fs_stack.h>
9986 +#include <linux/iversion.h>
9987 +#include "aufs.h"
9988 +
9989 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9990 +{
9991 +       unsigned int nlink;
9992 +
9993 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9994 +
9995 +       nlink = dir->i_nlink;
9996 +       nlink += h_dir->i_nlink - 2;
9997 +       if (h_dir->i_nlink < 2)
9998 +               nlink += 2;
9999 +       smp_mb(); /* for i_nlink */
10000 +       /* 0 can happen in revaliding */
10001 +       set_nlink(dir, nlink);
10002 +}
10003 +
10004 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10005 +{
10006 +       unsigned int nlink;
10007 +
10008 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10009 +
10010 +       nlink = dir->i_nlink;
10011 +       nlink -= h_dir->i_nlink - 2;
10012 +       if (h_dir->i_nlink < 2)
10013 +               nlink -= 2;
10014 +       smp_mb(); /* for i_nlink */
10015 +       /* nlink == 0 means the branch-fs is broken */
10016 +       set_nlink(dir, nlink);
10017 +}
10018 +
10019 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10020 +{
10021 +       loff_t sz;
10022 +       aufs_bindex_t bindex, bbot;
10023 +       struct file *h_file;
10024 +       struct dentry *h_dentry;
10025 +
10026 +       sz = 0;
10027 +       if (file) {
10028 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10029 +
10030 +               bbot = au_fbbot_dir(file);
10031 +               for (bindex = au_fbtop(file);
10032 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10033 +                    bindex++) {
10034 +                       h_file = au_hf_dir(file, bindex);
10035 +                       if (h_file && file_inode(h_file))
10036 +                               sz += vfsub_f_size_read(h_file);
10037 +               }
10038 +       } else {
10039 +               AuDebugOn(!dentry);
10040 +               AuDebugOn(!d_is_dir(dentry));
10041 +
10042 +               bbot = au_dbtaildir(dentry);
10043 +               for (bindex = au_dbtop(dentry);
10044 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10045 +                    bindex++) {
10046 +                       h_dentry = au_h_dptr(dentry, bindex);
10047 +                       if (h_dentry && d_is_positive(h_dentry))
10048 +                               sz += i_size_read(d_inode(h_dentry));
10049 +               }
10050 +       }
10051 +       if (sz < KMALLOC_MAX_SIZE)
10052 +               sz = roundup_pow_of_two(sz);
10053 +       if (sz > KMALLOC_MAX_SIZE)
10054 +               sz = KMALLOC_MAX_SIZE;
10055 +       else if (sz < NAME_MAX) {
10056 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10057 +               sz = AUFS_RDBLK_DEF;
10058 +       }
10059 +       return sz;
10060 +}
10061 +
10062 +struct au_dir_ts_arg {
10063 +       struct dentry *dentry;
10064 +       aufs_bindex_t brid;
10065 +};
10066 +
10067 +static void au_do_dir_ts(void *arg)
10068 +{
10069 +       struct au_dir_ts_arg *a = arg;
10070 +       struct au_dtime dt;
10071 +       struct path h_path;
10072 +       struct inode *dir, *h_dir;
10073 +       struct super_block *sb;
10074 +       struct au_branch *br;
10075 +       struct au_hinode *hdir;
10076 +       int err;
10077 +       aufs_bindex_t btop, bindex;
10078 +
10079 +       sb = a->dentry->d_sb;
10080 +       if (d_really_is_negative(a->dentry))
10081 +               goto out;
10082 +       /* no dir->i_mutex lock */
10083 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10084 +
10085 +       dir = d_inode(a->dentry);
10086 +       btop = au_ibtop(dir);
10087 +       bindex = au_br_index(sb, a->brid);
10088 +       if (bindex < btop)
10089 +               goto out_unlock;
10090 +
10091 +       br = au_sbr(sb, bindex);
10092 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10093 +       if (!h_path.dentry)
10094 +               goto out_unlock;
10095 +       h_path.mnt = au_br_mnt(br);
10096 +       au_dtime_store(&dt, a->dentry, &h_path);
10097 +
10098 +       br = au_sbr(sb, btop);
10099 +       if (!au_br_writable(br->br_perm))
10100 +               goto out_unlock;
10101 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10102 +       h_path.mnt = au_br_mnt(br);
10103 +       err = vfsub_mnt_want_write(h_path.mnt);
10104 +       if (err)
10105 +               goto out_unlock;
10106 +       hdir = au_hi(dir, btop);
10107 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10108 +       h_dir = au_h_iptr(dir, btop);
10109 +       if (h_dir->i_nlink
10110 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10111 +               dt.dt_h_path = h_path;
10112 +               au_dtime_revert(&dt);
10113 +       }
10114 +       au_hn_inode_unlock(hdir);
10115 +       vfsub_mnt_drop_write(h_path.mnt);
10116 +       au_cpup_attr_timesizes(dir);
10117 +
10118 +out_unlock:
10119 +       aufs_read_unlock(a->dentry, AuLock_DW);
10120 +out:
10121 +       dput(a->dentry);
10122 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10123 +       au_kfree_try_rcu(arg);
10124 +}
10125 +
10126 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10127 +{
10128 +       int perm, wkq_err;
10129 +       aufs_bindex_t btop;
10130 +       struct au_dir_ts_arg *arg;
10131 +       struct dentry *dentry;
10132 +       struct super_block *sb;
10133 +
10134 +       IMustLock(dir);
10135 +
10136 +       dentry = d_find_any_alias(dir);
10137 +       AuDebugOn(!dentry);
10138 +       sb = dentry->d_sb;
10139 +       btop = au_ibtop(dir);
10140 +       if (btop == bindex) {
10141 +               au_cpup_attr_timesizes(dir);
10142 +               goto out;
10143 +       }
10144 +
10145 +       perm = au_sbr_perm(sb, btop);
10146 +       if (!au_br_writable(perm))
10147 +               goto out;
10148 +
10149 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10150 +       if (!arg)
10151 +               goto out;
10152 +
10153 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10154 +       arg->brid = au_sbr_id(sb, bindex);
10155 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10156 +       if (unlikely(wkq_err)) {
10157 +               pr_err("wkq %d\n", wkq_err);
10158 +               dput(dentry);
10159 +               au_kfree_try_rcu(arg);
10160 +       }
10161 +
10162 +out:
10163 +       dput(dentry);
10164 +}
10165 +
10166 +/* ---------------------------------------------------------------------- */
10167 +
10168 +static int reopen_dir(struct file *file)
10169 +{
10170 +       int err;
10171 +       unsigned int flags;
10172 +       aufs_bindex_t bindex, btail, btop;
10173 +       struct dentry *dentry, *h_dentry;
10174 +       struct file *h_file;
10175 +
10176 +       /* open all lower dirs */
10177 +       dentry = file->f_path.dentry;
10178 +       btop = au_dbtop(dentry);
10179 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10180 +               au_set_h_fptr(file, bindex, NULL);
10181 +       au_set_fbtop(file, btop);
10182 +
10183 +       btail = au_dbtaildir(dentry);
10184 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10185 +               au_set_h_fptr(file, bindex, NULL);
10186 +       au_set_fbbot_dir(file, btail);
10187 +
10188 +       flags = vfsub_file_flags(file);
10189 +       for (bindex = btop; bindex <= btail; bindex++) {
10190 +               h_dentry = au_h_dptr(dentry, bindex);
10191 +               if (!h_dentry)
10192 +                       continue;
10193 +               h_file = au_hf_dir(file, bindex);
10194 +               if (h_file)
10195 +                       continue;
10196 +
10197 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10198 +               err = PTR_ERR(h_file);
10199 +               if (IS_ERR(h_file))
10200 +                       goto out; /* close all? */
10201 +               au_set_h_fptr(file, bindex, h_file);
10202 +       }
10203 +       au_update_figen(file);
10204 +       /* todo: necessary? */
10205 +       /* file->f_ra = h_file->f_ra; */
10206 +       err = 0;
10207 +
10208 +out:
10209 +       return err;
10210 +}
10211 +
10212 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10213 +{
10214 +       int err;
10215 +       aufs_bindex_t bindex, btail;
10216 +       struct dentry *dentry, *h_dentry;
10217 +       struct vfsmount *mnt;
10218 +
10219 +       FiMustWriteLock(file);
10220 +       AuDebugOn(h_file);
10221 +
10222 +       err = 0;
10223 +       mnt = file->f_path.mnt;
10224 +       dentry = file->f_path.dentry;
10225 +       file->f_version = inode_query_iversion(d_inode(dentry));
10226 +       bindex = au_dbtop(dentry);
10227 +       au_set_fbtop(file, bindex);
10228 +       btail = au_dbtaildir(dentry);
10229 +       au_set_fbbot_dir(file, btail);
10230 +       for (; !err && bindex <= btail; bindex++) {
10231 +               h_dentry = au_h_dptr(dentry, bindex);
10232 +               if (!h_dentry)
10233 +                       continue;
10234 +
10235 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10236 +               if (unlikely(err))
10237 +                       break;
10238 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10239 +               if (IS_ERR(h_file)) {
10240 +                       err = PTR_ERR(h_file);
10241 +                       break;
10242 +               }
10243 +               au_set_h_fptr(file, bindex, h_file);
10244 +       }
10245 +       au_update_figen(file);
10246 +       /* todo: necessary? */
10247 +       /* file->f_ra = h_file->f_ra; */
10248 +       if (!err)
10249 +               return 0; /* success */
10250 +
10251 +       /* close all */
10252 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10253 +               au_set_h_fptr(file, bindex, NULL);
10254 +       au_set_fbtop(file, -1);
10255 +       au_set_fbbot_dir(file, -1);
10256 +
10257 +       return err;
10258 +}
10259 +
10260 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10261 +                        struct file *file)
10262 +{
10263 +       int err;
10264 +       struct super_block *sb;
10265 +       struct au_fidir *fidir;
10266 +
10267 +       err = -ENOMEM;
10268 +       sb = file->f_path.dentry->d_sb;
10269 +       si_read_lock(sb, AuLock_FLUSH);
10270 +       fidir = au_fidir_alloc(sb);
10271 +       if (fidir) {
10272 +               struct au_do_open_args args = {
10273 +                       .open   = do_open_dir,
10274 +                       .fidir  = fidir
10275 +               };
10276 +               err = au_do_open(file, &args);
10277 +               if (unlikely(err))
10278 +                       au_kfree_rcu(fidir);
10279 +       }
10280 +       si_read_unlock(sb);
10281 +       return err;
10282 +}
10283 +
10284 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10285 +                           struct file *file)
10286 +{
10287 +       struct au_vdir *vdir_cache;
10288 +       struct au_finfo *finfo;
10289 +       struct au_fidir *fidir;
10290 +       struct au_hfile *hf;
10291 +       aufs_bindex_t bindex, bbot;
10292 +
10293 +       finfo = au_fi(file);
10294 +       fidir = finfo->fi_hdir;
10295 +       if (fidir) {
10296 +               au_hbl_del(&finfo->fi_hlist,
10297 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10298 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10299 +               if (vdir_cache)
10300 +                       au_vdir_free(vdir_cache);
10301 +
10302 +               bindex = finfo->fi_btop;
10303 +               if (bindex >= 0) {
10304 +                       hf = fidir->fd_hfile + bindex;
10305 +                       /*
10306 +                        * calls fput() instead of filp_close(),
10307 +                        * since no dnotify or lock for the lower file.
10308 +                        */
10309 +                       bbot = fidir->fd_bbot;
10310 +                       for (; bindex <= bbot; bindex++, hf++)
10311 +                               if (hf->hf_file)
10312 +                                       au_hfput(hf, /*execed*/0);
10313 +               }
10314 +               au_kfree_rcu(fidir);
10315 +               finfo->fi_hdir = NULL;
10316 +       }
10317 +       au_finfo_fin(file);
10318 +       return 0;
10319 +}
10320 +
10321 +/* ---------------------------------------------------------------------- */
10322 +
10323 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10324 +{
10325 +       int err;
10326 +       aufs_bindex_t bindex, bbot;
10327 +       struct file *h_file;
10328 +
10329 +       err = 0;
10330 +       bbot = au_fbbot_dir(file);
10331 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10332 +               h_file = au_hf_dir(file, bindex);
10333 +               if (h_file)
10334 +                       err = vfsub_flush(h_file, id);
10335 +       }
10336 +       return err;
10337 +}
10338 +
10339 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10340 +{
10341 +       return au_do_flush(file, id, au_do_flush_dir);
10342 +}
10343 +
10344 +/* ---------------------------------------------------------------------- */
10345 +
10346 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10347 +{
10348 +       int err;
10349 +       aufs_bindex_t bbot, bindex;
10350 +       struct inode *inode;
10351 +       struct super_block *sb;
10352 +
10353 +       err = 0;
10354 +       sb = dentry->d_sb;
10355 +       inode = d_inode(dentry);
10356 +       IMustLock(inode);
10357 +       bbot = au_dbbot(dentry);
10358 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10359 +               struct path h_path;
10360 +
10361 +               if (au_test_ro(sb, bindex, inode))
10362 +                       continue;
10363 +               h_path.dentry = au_h_dptr(dentry, bindex);
10364 +               if (!h_path.dentry)
10365 +                       continue;
10366 +
10367 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10368 +               err = vfsub_fsync(NULL, &h_path, datasync);
10369 +       }
10370 +
10371 +       return err;
10372 +}
10373 +
10374 +static int au_do_fsync_dir(struct file *file, int datasync)
10375 +{
10376 +       int err;
10377 +       aufs_bindex_t bbot, bindex;
10378 +       struct file *h_file;
10379 +       struct super_block *sb;
10380 +       struct inode *inode;
10381 +
10382 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10383 +       if (unlikely(err))
10384 +               goto out;
10385 +
10386 +       inode = file_inode(file);
10387 +       sb = inode->i_sb;
10388 +       bbot = au_fbbot_dir(file);
10389 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10390 +               h_file = au_hf_dir(file, bindex);
10391 +               if (!h_file || au_test_ro(sb, bindex, inode))
10392 +                       continue;
10393 +
10394 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10395 +       }
10396 +
10397 +out:
10398 +       return err;
10399 +}
10400 +
10401 +/*
10402 + * @file may be NULL
10403 + */
10404 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10405 +                         int datasync)
10406 +{
10407 +       int err;
10408 +       struct dentry *dentry;
10409 +       struct inode *inode;
10410 +       struct super_block *sb;
10411 +
10412 +       err = 0;
10413 +       dentry = file->f_path.dentry;
10414 +       inode = d_inode(dentry);
10415 +       inode_lock(inode);
10416 +       sb = dentry->d_sb;
10417 +       si_noflush_read_lock(sb);
10418 +       if (file)
10419 +               err = au_do_fsync_dir(file, datasync);
10420 +       else {
10421 +               di_write_lock_child(dentry);
10422 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10423 +       }
10424 +       au_cpup_attr_timesizes(inode);
10425 +       di_write_unlock(dentry);
10426 +       if (file)
10427 +               fi_write_unlock(file);
10428 +
10429 +       si_read_unlock(sb);
10430 +       inode_unlock(inode);
10431 +       return err;
10432 +}
10433 +
10434 +/* ---------------------------------------------------------------------- */
10435 +
10436 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10437 +{
10438 +       int err;
10439 +       struct dentry *dentry;
10440 +       struct inode *inode, *h_inode;
10441 +       struct super_block *sb;
10442 +
10443 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10444 +
10445 +       dentry = file->f_path.dentry;
10446 +       inode = d_inode(dentry);
10447 +       IMustLock(inode);
10448 +
10449 +       sb = dentry->d_sb;
10450 +       si_read_lock(sb, AuLock_FLUSH);
10451 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10452 +       if (unlikely(err))
10453 +               goto out;
10454 +       err = au_alive_dir(dentry);
10455 +       if (!err)
10456 +               err = au_vdir_init(file);
10457 +       di_downgrade_lock(dentry, AuLock_IR);
10458 +       if (unlikely(err))
10459 +               goto out_unlock;
10460 +
10461 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10462 +       if (!au_test_nfsd()) {
10463 +               err = au_vdir_fill_de(file, ctx);
10464 +               fsstack_copy_attr_atime(inode, h_inode);
10465 +       } else {
10466 +               /*
10467 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10468 +                * encode_fh() and others.
10469 +                */
10470 +               atomic_inc(&h_inode->i_count);
10471 +               di_read_unlock(dentry, AuLock_IR);
10472 +               si_read_unlock(sb);
10473 +               err = au_vdir_fill_de(file, ctx);
10474 +               fsstack_copy_attr_atime(inode, h_inode);
10475 +               fi_write_unlock(file);
10476 +               iput(h_inode);
10477 +
10478 +               AuTraceErr(err);
10479 +               return err;
10480 +       }
10481 +
10482 +out_unlock:
10483 +       di_read_unlock(dentry, AuLock_IR);
10484 +       fi_write_unlock(file);
10485 +out:
10486 +       si_read_unlock(sb);
10487 +       return err;
10488 +}
10489 +
10490 +/* ---------------------------------------------------------------------- */
10491 +
10492 +#define AuTestEmpty_WHONLY     1
10493 +#define AuTestEmpty_CALLED     (1 << 1)
10494 +#define AuTestEmpty_SHWH       (1 << 2)
10495 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10496 +#define au_fset_testempty(flags, name) \
10497 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10498 +#define au_fclr_testempty(flags, name) \
10499 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10500 +
10501 +#ifndef CONFIG_AUFS_SHWH
10502 +#undef AuTestEmpty_SHWH
10503 +#define AuTestEmpty_SHWH       0
10504 +#endif
10505 +
10506 +struct test_empty_arg {
10507 +       struct dir_context ctx;
10508 +       struct au_nhash *whlist;
10509 +       unsigned int flags;
10510 +       int err;
10511 +       aufs_bindex_t bindex;
10512 +};
10513 +
10514 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10515 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10516 +                        unsigned int d_type)
10517 +{
10518 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10519 +                                                 ctx);
10520 +       char *name = (void *)__name;
10521 +
10522 +       arg->err = 0;
10523 +       au_fset_testempty(arg->flags, CALLED);
10524 +       /* smp_mb(); */
10525 +       if (name[0] == '.'
10526 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10527 +               goto out; /* success */
10528 +
10529 +       if (namelen <= AUFS_WH_PFX_LEN
10530 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10531 +               if (au_ftest_testempty(arg->flags, WHONLY)
10532 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10533 +                       arg->err = -ENOTEMPTY;
10534 +               goto out;
10535 +       }
10536 +
10537 +       name += AUFS_WH_PFX_LEN;
10538 +       namelen -= AUFS_WH_PFX_LEN;
10539 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10540 +               arg->err = au_nhash_append_wh
10541 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10542 +                        au_ftest_testempty(arg->flags, SHWH));
10543 +
10544 +out:
10545 +       /* smp_mb(); */
10546 +       AuTraceErr(arg->err);
10547 +       return arg->err;
10548 +}
10549 +
10550 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10551 +{
10552 +       int err;
10553 +       struct file *h_file;
10554 +       struct au_branch *br;
10555 +
10556 +       h_file = au_h_open(dentry, arg->bindex,
10557 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10558 +                          /*file*/NULL, /*force_wr*/0);
10559 +       err = PTR_ERR(h_file);
10560 +       if (IS_ERR(h_file))
10561 +               goto out;
10562 +
10563 +       err = 0;
10564 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10565 +           && !file_inode(h_file)->i_nlink)
10566 +               goto out_put;
10567 +
10568 +       do {
10569 +               arg->err = 0;
10570 +               au_fclr_testempty(arg->flags, CALLED);
10571 +               /* smp_mb(); */
10572 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10573 +               if (err >= 0)
10574 +                       err = arg->err;
10575 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10576 +
10577 +out_put:
10578 +       fput(h_file);
10579 +       br = au_sbr(dentry->d_sb, arg->bindex);
10580 +       au_lcnt_dec(&br->br_nfiles);
10581 +out:
10582 +       return err;
10583 +}
10584 +
10585 +struct do_test_empty_args {
10586 +       int *errp;
10587 +       struct dentry *dentry;
10588 +       struct test_empty_arg *arg;
10589 +};
10590 +
10591 +static void call_do_test_empty(void *args)
10592 +{
10593 +       struct do_test_empty_args *a = args;
10594 +       *a->errp = do_test_empty(a->dentry, a->arg);
10595 +}
10596 +
10597 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10598 +{
10599 +       int err, wkq_err;
10600 +       struct dentry *h_dentry;
10601 +       struct inode *h_inode;
10602 +       struct user_namespace *h_userns;
10603 +
10604 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10605 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10606 +       h_inode = d_inode(h_dentry);
10607 +       /* todo: i_mode changes anytime? */
10608 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10609 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10610 +       inode_unlock_shared(h_inode);
10611 +       if (!err)
10612 +               err = do_test_empty(dentry, arg);
10613 +       else {
10614 +               struct do_test_empty_args args = {
10615 +                       .errp   = &err,
10616 +                       .dentry = dentry,
10617 +                       .arg    = arg
10618 +               };
10619 +               unsigned int flags = arg->flags;
10620 +
10621 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10622 +               if (unlikely(wkq_err))
10623 +                       err = wkq_err;
10624 +               arg->flags = flags;
10625 +       }
10626 +
10627 +       return err;
10628 +}
10629 +
10630 +int au_test_empty_lower(struct dentry *dentry)
10631 +{
10632 +       int err;
10633 +       unsigned int rdhash;
10634 +       aufs_bindex_t bindex, btop, btail;
10635 +       struct au_nhash whlist;
10636 +       struct test_empty_arg arg = {
10637 +               .ctx = {
10638 +                       .actor = test_empty_cb
10639 +               }
10640 +       };
10641 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10642 +
10643 +       SiMustAnyLock(dentry->d_sb);
10644 +
10645 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10646 +       if (!rdhash)
10647 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10648 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10649 +       if (unlikely(err))
10650 +               goto out;
10651 +
10652 +       arg.flags = 0;
10653 +       arg.whlist = &whlist;
10654 +       btop = au_dbtop(dentry);
10655 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10656 +               au_fset_testempty(arg.flags, SHWH);
10657 +       test_empty = do_test_empty;
10658 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10659 +               test_empty = sio_test_empty;
10660 +       arg.bindex = btop;
10661 +       err = test_empty(dentry, &arg);
10662 +       if (unlikely(err))
10663 +               goto out_whlist;
10664 +
10665 +       au_fset_testempty(arg.flags, WHONLY);
10666 +       btail = au_dbtaildir(dentry);
10667 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10668 +               struct dentry *h_dentry;
10669 +
10670 +               h_dentry = au_h_dptr(dentry, bindex);
10671 +               if (h_dentry && d_is_positive(h_dentry)) {
10672 +                       arg.bindex = bindex;
10673 +                       err = test_empty(dentry, &arg);
10674 +               }
10675 +       }
10676 +
10677 +out_whlist:
10678 +       au_nhash_wh_free(&whlist);
10679 +out:
10680 +       return err;
10681 +}
10682 +
10683 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10684 +{
10685 +       int err;
10686 +       struct test_empty_arg arg = {
10687 +               .ctx = {
10688 +                       .actor = test_empty_cb
10689 +               }
10690 +       };
10691 +       aufs_bindex_t bindex, btail;
10692 +
10693 +       err = 0;
10694 +       arg.whlist = whlist;
10695 +       arg.flags = AuTestEmpty_WHONLY;
10696 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10697 +               au_fset_testempty(arg.flags, SHWH);
10698 +       btail = au_dbtaildir(dentry);
10699 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10700 +               struct dentry *h_dentry;
10701 +
10702 +               h_dentry = au_h_dptr(dentry, bindex);
10703 +               if (h_dentry && d_is_positive(h_dentry)) {
10704 +                       arg.bindex = bindex;
10705 +                       err = sio_test_empty(dentry, &arg);
10706 +               }
10707 +       }
10708 +
10709 +       return err;
10710 +}
10711 +
10712 +/* ---------------------------------------------------------------------- */
10713 +
10714 +const struct file_operations aufs_dir_fop = {
10715 +       .owner          = THIS_MODULE,
10716 +       .llseek         = default_llseek,
10717 +       .read           = generic_read_dir,
10718 +       .iterate_shared = aufs_iterate_shared,
10719 +       .unlocked_ioctl = aufs_ioctl_dir,
10720 +#ifdef CONFIG_COMPAT
10721 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10722 +#endif
10723 +       .open           = aufs_open_dir,
10724 +       .release        = aufs_release_dir,
10725 +       .flush          = aufs_flush_dir,
10726 +       .fsync          = aufs_fsync_dir
10727 +};
10728 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10729 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10730 +++ linux/fs/aufs/dir.h 2021-11-01 23:48:34.206359262 +0100
10731 @@ -0,0 +1,134 @@
10732 +/* SPDX-License-Identifier: GPL-2.0 */
10733 +/*
10734 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10735 + *
10736 + * This program, aufs is free software; you can redistribute it and/or modify
10737 + * it under the terms of the GNU General Public License as published by
10738 + * the Free Software Foundation; either version 2 of the License, or
10739 + * (at your option) any later version.
10740 + *
10741 + * This program is distributed in the hope that it will be useful,
10742 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10743 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10744 + * GNU General Public License for more details.
10745 + *
10746 + * You should have received a copy of the GNU General Public License
10747 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10748 + */
10749 +
10750 +/*
10751 + * directory operations
10752 + */
10753 +
10754 +#ifndef __AUFS_DIR_H__
10755 +#define __AUFS_DIR_H__
10756 +
10757 +#ifdef __KERNEL__
10758 +
10759 +#include <linux/fs.h>
10760 +
10761 +/* ---------------------------------------------------------------------- */
10762 +
10763 +/* need to be faster and smaller */
10764 +
10765 +struct au_nhash {
10766 +       unsigned int            nh_num;
10767 +       struct hlist_head       *nh_head;
10768 +};
10769 +
10770 +struct au_vdir_destr {
10771 +       unsigned char   len;
10772 +       unsigned char   name[];
10773 +} __packed;
10774 +
10775 +struct au_vdir_dehstr {
10776 +       struct hlist_node       hash;
10777 +       struct au_vdir_destr    *str;
10778 +       struct rcu_head         rcu;
10779 +} ____cacheline_aligned_in_smp;
10780 +
10781 +struct au_vdir_de {
10782 +       ino_t                   de_ino;
10783 +       unsigned char           de_type;
10784 +       /* caution: packed */
10785 +       struct au_vdir_destr    de_str;
10786 +} __packed;
10787 +
10788 +struct au_vdir_wh {
10789 +       struct hlist_node       wh_hash;
10790 +#ifdef CONFIG_AUFS_SHWH
10791 +       ino_t                   wh_ino;
10792 +       aufs_bindex_t           wh_bindex;
10793 +       unsigned char           wh_type;
10794 +#else
10795 +       aufs_bindex_t           wh_bindex;
10796 +#endif
10797 +       /* caution: packed */
10798 +       struct au_vdir_destr    wh_str;
10799 +} __packed;
10800 +
10801 +union au_vdir_deblk_p {
10802 +       unsigned char           *deblk;
10803 +       struct au_vdir_de       *de;
10804 +};
10805 +
10806 +struct au_vdir {
10807 +       unsigned char   **vd_deblk;
10808 +       unsigned long   vd_nblk;
10809 +       struct {
10810 +               unsigned long           ul;
10811 +               union au_vdir_deblk_p   p;
10812 +       } vd_last;
10813 +
10814 +       u64             vd_version;
10815 +       unsigned int    vd_deblk_sz;
10816 +       unsigned long   vd_jiffy;
10817 +       struct rcu_head rcu;
10818 +} ____cacheline_aligned_in_smp;
10819 +
10820 +/* ---------------------------------------------------------------------- */
10821 +
10822 +/* dir.c */
10823 +extern const struct file_operations aufs_dir_fop;
10824 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10825 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10826 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10827 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10828 +int au_test_empty_lower(struct dentry *dentry);
10829 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10830 +
10831 +/* vdir.c */
10832 +unsigned int au_rdhash_est(loff_t sz);
10833 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10834 +void au_nhash_wh_free(struct au_nhash *whlist);
10835 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10836 +                           int limit);
10837 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10838 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10839 +                      unsigned int d_type, aufs_bindex_t bindex,
10840 +                      unsigned char shwh);
10841 +void au_vdir_free(struct au_vdir *vdir);
10842 +int au_vdir_init(struct file *file);
10843 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10844 +
10845 +/* ioctl.c */
10846 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10847 +
10848 +#ifdef CONFIG_AUFS_RDU
10849 +/* rdu.c */
10850 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10851 +#ifdef CONFIG_COMPAT
10852 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10853 +                        unsigned long arg);
10854 +#endif
10855 +#else
10856 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10857 +       unsigned int cmd, unsigned long arg)
10858 +#ifdef CONFIG_COMPAT
10859 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10860 +       unsigned int cmd, unsigned long arg)
10861 +#endif
10862 +#endif
10863 +
10864 +#endif /* __KERNEL__ */
10865 +#endif /* __AUFS_DIR_H__ */
10866 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10867 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10868 +++ linux/fs/aufs/dirren.c      2021-11-01 23:48:34.206359262 +0100
10869 @@ -0,0 +1,1315 @@
10870 +// SPDX-License-Identifier: GPL-2.0
10871 +/*
10872 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10873 + *
10874 + * This program, aufs is free software; you can redistribute it and/or modify
10875 + * it under the terms of the GNU General Public License as published by
10876 + * the Free Software Foundation; either version 2 of the License, or
10877 + * (at your option) any later version.
10878 + *
10879 + * This program is distributed in the hope that it will be useful,
10880 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10881 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10882 + * GNU General Public License for more details.
10883 + *
10884 + * You should have received a copy of the GNU General Public License
10885 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10886 + */
10887 +
10888 +/*
10889 + * special handling in renaming a directory
10890 + * in order to support looking-up the before-renamed name on the lower readonly
10891 + * branches
10892 + */
10893 +
10894 +#include <linux/byteorder/generic.h>
10895 +#include "aufs.h"
10896 +
10897 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10898 +{
10899 +       int idx;
10900 +
10901 +       idx = au_dr_ihash(ent->dr_h_ino);
10902 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10903 +}
10904 +
10905 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10906 +{
10907 +       int ret, i;
10908 +       struct hlist_bl_head *hbl;
10909 +
10910 +       ret = 1;
10911 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10912 +               hbl = dr->dr_h_ino + i;
10913 +               hlist_bl_lock(hbl);
10914 +               ret &= hlist_bl_empty(hbl);
10915 +               hlist_bl_unlock(hbl);
10916 +       }
10917 +
10918 +       return ret;
10919 +}
10920 +
10921 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10922 +{
10923 +       struct au_dr_hino *found, *ent;
10924 +       struct hlist_bl_head *hbl;
10925 +       struct hlist_bl_node *pos;
10926 +       int idx;
10927 +
10928 +       found = NULL;
10929 +       idx = au_dr_ihash(ino);
10930 +       hbl = dr->dr_h_ino + idx;
10931 +       hlist_bl_lock(hbl);
10932 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10933 +               if (ent->dr_h_ino == ino) {
10934 +                       found = ent;
10935 +                       break;
10936 +               }
10937 +       hlist_bl_unlock(hbl);
10938 +
10939 +       return found;
10940 +}
10941 +
10942 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10943 +                       struct au_dr_hino *add_ent)
10944 +{
10945 +       int found, idx;
10946 +       struct hlist_bl_head *hbl;
10947 +       struct hlist_bl_node *pos;
10948 +       struct au_dr_hino *ent;
10949 +
10950 +       found = 0;
10951 +       idx = au_dr_ihash(ino);
10952 +       hbl = dr->dr_h_ino + idx;
10953 +#if 0 /* debug print */
10954 +       {
10955 +               struct hlist_bl_node *tmp;
10956 +
10957 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10958 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10959 +       }
10960 +#endif
10961 +       hlist_bl_lock(hbl);
10962 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10963 +               if (ent->dr_h_ino == ino) {
10964 +                       found = 1;
10965 +                       break;
10966 +               }
10967 +       if (!found && add_ent)
10968 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10969 +       hlist_bl_unlock(hbl);
10970 +
10971 +       if (!found && add_ent)
10972 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10973 +
10974 +       return found;
10975 +}
10976 +
10977 +void au_dr_hino_free(struct au_dr_br *dr)
10978 +{
10979 +       int i;
10980 +       struct hlist_bl_head *hbl;
10981 +       struct hlist_bl_node *pos, *tmp;
10982 +       struct au_dr_hino *ent;
10983 +
10984 +       /* SiMustWriteLock(sb); */
10985 +
10986 +       for (i = 0; i < AuDirren_NHASH; i++) {
10987 +               hbl = dr->dr_h_ino + i;
10988 +               /* no spinlock since sbinfo must be write-locked */
10989 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10990 +                       au_kfree_rcu(ent);
10991 +               INIT_HLIST_BL_HEAD(hbl);
10992 +       }
10993 +}
10994 +
10995 +/* returns the number of inodes or an error */
10996 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10997 +                           struct file *hinofile)
10998 +{
10999 +       int err, i;
11000 +       ssize_t ssz;
11001 +       loff_t pos, oldsize;
11002 +       __be64 u64;
11003 +       struct inode *hinoinode;
11004 +       struct hlist_bl_head *hbl;
11005 +       struct hlist_bl_node *n1, *n2;
11006 +       struct au_dr_hino *ent;
11007 +
11008 +       SiMustWriteLock(sb);
11009 +       AuDebugOn(!au_br_writable(br->br_perm));
11010 +
11011 +       hinoinode = file_inode(hinofile);
11012 +       oldsize = i_size_read(hinoinode);
11013 +
11014 +       err = 0;
11015 +       pos = 0;
11016 +       hbl = br->br_dirren.dr_h_ino;
11017 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11018 +               /* no bit-lock since sbinfo must be write-locked */
11019 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11020 +                       AuDbg("hi%llu, %pD2\n",
11021 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11022 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11023 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11024 +                       if (ssz == sizeof(u64))
11025 +                               continue;
11026 +
11027 +                       /* write error */
11028 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11029 +                       err = -ENOSPC;
11030 +                       if (ssz < 0)
11031 +                               err = ssz;
11032 +                       break;
11033 +               }
11034 +       }
11035 +       /* regardless the error */
11036 +       if (pos < oldsize) {
11037 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11038 +               AuTraceErr(err);
11039 +       }
11040 +
11041 +       AuTraceErr(err);
11042 +       return err;
11043 +}
11044 +
11045 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11046 +{
11047 +       int err, hidx;
11048 +       ssize_t ssz;
11049 +       size_t sz, n;
11050 +       loff_t pos;
11051 +       uint64_t u64;
11052 +       struct au_dr_hino *ent;
11053 +       struct inode *hinoinode;
11054 +       struct hlist_bl_head *hbl;
11055 +
11056 +       err = 0;
11057 +       pos = 0;
11058 +       hbl = dr->dr_h_ino;
11059 +       hinoinode = file_inode(hinofile);
11060 +       sz = i_size_read(hinoinode);
11061 +       AuDebugOn(sz % sizeof(u64));
11062 +       n = sz / sizeof(u64);
11063 +       while (n--) {
11064 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11065 +               if (unlikely(ssz != sizeof(u64))) {
11066 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11067 +                       err = -EINVAL;
11068 +                       if (ssz < 0)
11069 +                               err = ssz;
11070 +                       goto out_free;
11071 +               }
11072 +
11073 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11074 +               if (!ent) {
11075 +                       err = -ENOMEM;
11076 +                       AuTraceErr(err);
11077 +                       goto out_free;
11078 +               }
11079 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11080 +               AuDbg("hi%llu, %pD2\n",
11081 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11082 +               hidx = au_dr_ihash(ent->dr_h_ino);
11083 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11084 +       }
11085 +       goto out; /* success */
11086 +
11087 +out_free:
11088 +       au_dr_hino_free(dr);
11089 +out:
11090 +       AuTraceErr(err);
11091 +       return err;
11092 +}
11093 +
11094 +/*
11095 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11096 + * @path is a switch to distinguish load and store.
11097 + */
11098 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11099 +                     struct au_branch *br, const struct path *path)
11100 +{
11101 +       int err, flags;
11102 +       unsigned char load, suspend;
11103 +       struct file *hinofile;
11104 +       struct au_hinode *hdir;
11105 +       struct inode *dir, *delegated;
11106 +       struct path hinopath;
11107 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11108 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11109 +
11110 +       AuDebugOn(bindex < 0 && !br);
11111 +       AuDebugOn(bindex >= 0 && br);
11112 +
11113 +       err = -EINVAL;
11114 +       suspend = !br;
11115 +       if (suspend)
11116 +               br = au_sbr(sb, bindex);
11117 +       load = !!path;
11118 +       if (!load) {
11119 +               path = &br->br_path;
11120 +               AuDebugOn(!au_br_writable(br->br_perm));
11121 +               if (unlikely(!au_br_writable(br->br_perm)))
11122 +                       goto out;
11123 +       }
11124 +
11125 +       hdir = NULL;
11126 +       if (suspend) {
11127 +               dir = d_inode(sb->s_root);
11128 +               hdir = au_hinode(au_ii(dir), bindex);
11129 +               dir = hdir->hi_inode;
11130 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11131 +       } else {
11132 +               dir = d_inode(path->dentry);
11133 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11134 +       }
11135 +       hinopath.mnt = path->mnt;
11136 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11137 +       err = PTR_ERR(hinopath.dentry);
11138 +       if (IS_ERR(hinopath.dentry))
11139 +               goto out_unlock;
11140 +
11141 +       err = 0;
11142 +       flags = O_RDONLY;
11143 +       if (load) {
11144 +               if (d_is_negative(hinopath.dentry))
11145 +                       goto out_dput; /* success */
11146 +       } else {
11147 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11148 +                       if (d_is_positive(hinopath.dentry)) {
11149 +                               delegated = NULL;
11150 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11151 +                                                  /*force*/0);
11152 +                               AuTraceErr(err);
11153 +                               if (unlikely(err))
11154 +                                       pr_err("ignored err %d, %pd2\n",
11155 +                                              err, hinopath.dentry);
11156 +                               if (unlikely(err == -EWOULDBLOCK))
11157 +                                       iput(delegated);
11158 +                               err = 0;
11159 +                       }
11160 +                       goto out_dput;
11161 +               } else if (!d_is_positive(hinopath.dentry)) {
11162 +                       err = vfsub_create(dir, &hinopath, 0600,
11163 +                                          /*want_excl*/false);
11164 +                       AuTraceErr(err);
11165 +                       if (unlikely(err))
11166 +                               goto out_dput;
11167 +               }
11168 +               flags = O_WRONLY;
11169 +       }
11170 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11171 +       if (suspend)
11172 +               au_hn_inode_unlock(hdir);
11173 +       else
11174 +               inode_unlock(dir);
11175 +       dput(hinopath.dentry);
11176 +       AuTraceErrPtr(hinofile);
11177 +       if (IS_ERR(hinofile)) {
11178 +               err = PTR_ERR(hinofile);
11179 +               goto out;
11180 +       }
11181 +
11182 +       if (load)
11183 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11184 +       else
11185 +               err = au_dr_hino_store(sb, br, hinofile);
11186 +       fput(hinofile);
11187 +       goto out;
11188 +
11189 +out_dput:
11190 +       dput(hinopath.dentry);
11191 +out_unlock:
11192 +       if (suspend)
11193 +               au_hn_inode_unlock(hdir);
11194 +       else
11195 +               inode_unlock(dir);
11196 +out:
11197 +       AuTraceErr(err);
11198 +       return err;
11199 +}
11200 +
11201 +/* ---------------------------------------------------------------------- */
11202 +
11203 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11204 +{
11205 +       int err;
11206 +       struct kstatfs kstfs;
11207 +       dev_t dev;
11208 +       struct dentry *dentry;
11209 +       struct super_block *sb;
11210 +
11211 +       err = vfs_statfs((void *)path, &kstfs);
11212 +       AuTraceErr(err);
11213 +       if (unlikely(err))
11214 +               goto out;
11215 +
11216 +       /* todo: support for UUID */
11217 +
11218 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11219 +               brid->type = AuBrid_FSID;
11220 +               brid->fsid = kstfs.f_fsid;
11221 +       } else {
11222 +               dentry = path->dentry;
11223 +               sb = dentry->d_sb;
11224 +               dev = sb->s_dev;
11225 +               if (dev) {
11226 +                       brid->type = AuBrid_DEV;
11227 +                       brid->dev = dev;
11228 +               }
11229 +       }
11230 +
11231 +out:
11232 +       return err;
11233 +}
11234 +
11235 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11236 +                 const struct path *path)
11237 +{
11238 +       int err, i;
11239 +       struct au_dr_br *dr;
11240 +       struct hlist_bl_head *hbl;
11241 +
11242 +       dr = &br->br_dirren;
11243 +       hbl = dr->dr_h_ino;
11244 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11245 +               INIT_HLIST_BL_HEAD(hbl);
11246 +
11247 +       err = au_dr_brid_init(&dr->dr_brid, path);
11248 +       if (unlikely(err))
11249 +               goto out;
11250 +
11251 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11252 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11253 +
11254 +out:
11255 +       AuTraceErr(err);
11256 +       return err;
11257 +}
11258 +
11259 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11260 +{
11261 +       int err;
11262 +
11263 +       err = 0;
11264 +       if (au_br_writable(br->br_perm))
11265 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11266 +       if (!err)
11267 +               au_dr_hino_free(&br->br_dirren);
11268 +
11269 +       return err;
11270 +}
11271 +
11272 +/* ---------------------------------------------------------------------- */
11273 +
11274 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11275 +                      char *buf, size_t sz)
11276 +{
11277 +       int err;
11278 +       unsigned int major, minor;
11279 +       char *p;
11280 +
11281 +       p = buf;
11282 +       err = snprintf(p, sz, "%d_", brid->type);
11283 +       AuDebugOn(err > sz);
11284 +       p += err;
11285 +       sz -= err;
11286 +       switch (brid->type) {
11287 +       case AuBrid_Unset:
11288 +               return -EINVAL;
11289 +       case AuBrid_UUID:
11290 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11291 +               break;
11292 +       case AuBrid_FSID:
11293 +               err = snprintf(p, sz, "%08x-%08x",
11294 +                              brid->fsid.val[0], brid->fsid.val[1]);
11295 +               break;
11296 +       case AuBrid_DEV:
11297 +               major = MAJOR(brid->dev);
11298 +               minor = MINOR(brid->dev);
11299 +               if (major <= 0xff && minor <= 0xff)
11300 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11301 +               else
11302 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11303 +               break;
11304 +       }
11305 +       AuDebugOn(err > sz);
11306 +       p += err;
11307 +       sz -= err;
11308 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11309 +       AuDebugOn(err > sz);
11310 +       p += err;
11311 +       sz -= err;
11312 +
11313 +       return p - buf;
11314 +}
11315 +
11316 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11317 +{
11318 +       int rlen;
11319 +       struct dentry *br_dentry;
11320 +       struct inode *br_inode;
11321 +
11322 +       br_dentry = au_br_dentry(br);
11323 +       br_inode = d_inode(br_dentry);
11324 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11325 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11326 +       AuDebugOn(rlen > len);
11327 +
11328 +       return rlen;
11329 +}
11330 +
11331 +/* ---------------------------------------------------------------------- */
11332 +
11333 +/*
11334 + * from the given @h_dentry, construct drinfo at @*fdata.
11335 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11336 + * @allocated.
11337 + */
11338 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11339 +                              struct dentry *h_dentry,
11340 +                              unsigned char *allocated)
11341 +{
11342 +       int err, v;
11343 +       struct au_drinfo_fdata *f, *p;
11344 +       struct au_drinfo *drinfo;
11345 +       struct inode *h_inode;
11346 +       struct qstr *qname;
11347 +
11348 +       err = 0;
11349 +       f = *fdata;
11350 +       h_inode = d_inode(h_dentry);
11351 +       qname = &h_dentry->d_name;
11352 +       drinfo = &f->drinfo;
11353 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11354 +       drinfo->oldnamelen = qname->len;
11355 +       if (*allocated < sizeof(*f) + qname->len) {
11356 +               v = roundup_pow_of_two(*allocated + qname->len);
11357 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11358 +               if (unlikely(!p)) {
11359 +                       err = -ENOMEM;
11360 +                       AuTraceErr(err);
11361 +                       goto out;
11362 +               }
11363 +               f = p;
11364 +               *fdata = f;
11365 +               *allocated = v;
11366 +               drinfo = &f->drinfo;
11367 +       }
11368 +       memcpy(drinfo->oldname, qname->name, qname->len);
11369 +       AuDbg("i%llu, %.*s\n",
11370 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11371 +             drinfo->oldname);
11372 +
11373 +out:
11374 +       AuTraceErr(err);
11375 +       return err;
11376 +}
11377 +
11378 +/* callers have to free the return value */
11379 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11380 +{
11381 +       struct au_drinfo *ret, *drinfo;
11382 +       struct au_drinfo_fdata fdata;
11383 +       int len;
11384 +       loff_t pos;
11385 +       ssize_t ssz;
11386 +
11387 +       ret = ERR_PTR(-EIO);
11388 +       pos = 0;
11389 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11390 +       if (unlikely(ssz != sizeof(fdata))) {
11391 +               AuIOErr("ssz %zd, %u, %pD2\n",
11392 +                       ssz, (unsigned int)sizeof(fdata), file);
11393 +               goto out;
11394 +       }
11395 +
11396 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11397 +       switch (fdata.magic) {
11398 +       case AUFS_DRINFO_MAGIC_V1:
11399 +               break;
11400 +       default:
11401 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11402 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11403 +               goto out;
11404 +       }
11405 +
11406 +       drinfo = &fdata.drinfo;
11407 +       len = drinfo->oldnamelen;
11408 +       if (!len) {
11409 +               AuIOErr("broken drinfo %pD2\n", file);
11410 +               goto out;
11411 +       }
11412 +
11413 +       ret = NULL;
11414 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11415 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11416 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11417 +                     (unsigned long long)drinfo->ino,
11418 +                     (unsigned long long)h_ino, file);
11419 +               goto out; /* success */
11420 +       }
11421 +
11422 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11423 +       if (unlikely(!ret)) {
11424 +               ret = ERR_PTR(-ENOMEM);
11425 +               AuTraceErrPtr(ret);
11426 +               goto out;
11427 +       }
11428 +
11429 +       *ret = *drinfo;
11430 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11431 +       if (unlikely(ssz != len)) {
11432 +               au_kfree_rcu(ret);
11433 +               ret = ERR_PTR(-EIO);
11434 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11435 +               goto out;
11436 +       }
11437 +
11438 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11439 +
11440 +out:
11441 +       return ret;
11442 +}
11443 +
11444 +/* ---------------------------------------------------------------------- */
11445 +
11446 +/* in order to be revertible */
11447 +struct au_drinfo_rev_elm {
11448 +       int                     created;
11449 +       struct dentry           *info_dentry;
11450 +       struct au_drinfo        *info_last;
11451 +};
11452 +
11453 +struct au_drinfo_rev {
11454 +       unsigned char                   already;
11455 +       aufs_bindex_t                   nelm;
11456 +       struct au_drinfo_rev_elm        elm[];
11457 +};
11458 +
11459 +/* todo: isn't it too large? */
11460 +struct au_drinfo_store {
11461 +       struct path h_ppath;
11462 +       struct dentry *h_dentry;
11463 +       struct au_drinfo_fdata *fdata;
11464 +       char *infoname;                 /* inside of whname, just after PFX */
11465 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11466 +       aufs_bindex_t btgt, btail;
11467 +       unsigned char no_sio,
11468 +               allocated,              /* current size of *fdata */
11469 +               infonamelen,            /* room size for p */
11470 +               whnamelen,              /* length of the generated name */
11471 +               renameback;             /* renamed back */
11472 +};
11473 +
11474 +/* on rename(2) error, the caller should revert it using @elm */
11475 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11476 +                             struct au_drinfo_rev_elm *elm)
11477 +{
11478 +       int err, len;
11479 +       ssize_t ssz;
11480 +       loff_t pos;
11481 +       struct path infopath = {
11482 +               .mnt = w->h_ppath.mnt
11483 +       };
11484 +       struct inode *h_dir, *h_inode, *delegated;
11485 +       struct file *infofile;
11486 +       struct qstr *qname;
11487 +
11488 +       AuDebugOn(elm
11489 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11490 +
11491 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11492 +                                              w->whnamelen);
11493 +       AuTraceErrPtr(infopath.dentry);
11494 +       if (IS_ERR(infopath.dentry)) {
11495 +               err = PTR_ERR(infopath.dentry);
11496 +               goto out;
11497 +       }
11498 +
11499 +       err = 0;
11500 +       h_dir = d_inode(w->h_ppath.dentry);
11501 +       if (elm && d_is_negative(infopath.dentry)) {
11502 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11503 +               AuTraceErr(err);
11504 +               if (unlikely(err))
11505 +                       goto out_dput;
11506 +               elm->created = 1;
11507 +               elm->info_dentry = dget(infopath.dentry);
11508 +       }
11509 +
11510 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11511 +       AuTraceErrPtr(infofile);
11512 +       if (IS_ERR(infofile)) {
11513 +               err = PTR_ERR(infofile);
11514 +               goto out_dput;
11515 +       }
11516 +
11517 +       h_inode = d_inode(infopath.dentry);
11518 +       if (elm && i_size_read(h_inode)) {
11519 +               h_inode = d_inode(w->h_dentry);
11520 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11521 +               AuTraceErrPtr(elm->info_last);
11522 +               if (IS_ERR(elm->info_last)) {
11523 +                       err = PTR_ERR(elm->info_last);
11524 +                       elm->info_last = NULL;
11525 +                       AuDebugOn(elm->info_dentry);
11526 +                       goto out_fput;
11527 +               }
11528 +       }
11529 +
11530 +       if (elm && w->renameback) {
11531 +               delegated = NULL;
11532 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11533 +               AuTraceErr(err);
11534 +               if (unlikely(err == -EWOULDBLOCK))
11535 +                       iput(delegated);
11536 +               goto out_fput;
11537 +       }
11538 +
11539 +       pos = 0;
11540 +       qname = &w->h_dentry->d_name;
11541 +       len = sizeof(*w->fdata) + qname->len;
11542 +       if (!elm)
11543 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11544 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11545 +       if (ssz == len) {
11546 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11547 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11548 +               goto out_fput; /* success */
11549 +       } else {
11550 +               err = -EIO;
11551 +               if (ssz < 0)
11552 +                       err = ssz;
11553 +               /* the caller should revert it using @elm */
11554 +       }
11555 +
11556 +out_fput:
11557 +       fput(infofile);
11558 +out_dput:
11559 +       dput(infopath.dentry);
11560 +out:
11561 +       AuTraceErr(err);
11562 +       return err;
11563 +}
11564 +
11565 +struct au_call_drinfo_do_store_args {
11566 +       int *errp;
11567 +       struct au_drinfo_store *w;
11568 +       struct au_drinfo_rev_elm *elm;
11569 +};
11570 +
11571 +static void au_call_drinfo_do_store(void *args)
11572 +{
11573 +       struct au_call_drinfo_do_store_args *a = args;
11574 +
11575 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11576 +}
11577 +
11578 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11579 +                              struct au_drinfo_rev_elm *elm)
11580 +{
11581 +       int err, wkq_err;
11582 +
11583 +       if (w->no_sio)
11584 +               err = au_drinfo_do_store(w, elm);
11585 +       else {
11586 +               struct au_call_drinfo_do_store_args a = {
11587 +                       .errp   = &err,
11588 +                       .w      = w,
11589 +                       .elm    = elm
11590 +               };
11591 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11592 +               if (unlikely(wkq_err))
11593 +                       err = wkq_err;
11594 +       }
11595 +       AuTraceErr(err);
11596 +
11597 +       return err;
11598 +}
11599 +
11600 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11601 +                                    aufs_bindex_t btgt)
11602 +{
11603 +       int err;
11604 +
11605 +       memset(w, 0, sizeof(*w));
11606 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11607 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11608 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11609 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11610 +       w->btgt = btgt;
11611 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11612 +
11613 +       err = -ENOMEM;
11614 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11615 +       if (unlikely(!w->fdata)) {
11616 +               AuTraceErr(err);
11617 +               goto out;
11618 +       }
11619 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11620 +       err = 0;
11621 +
11622 +out:
11623 +       return err;
11624 +}
11625 +
11626 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11627 +{
11628 +       au_kfree_rcu(w->fdata);
11629 +}
11630 +
11631 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11632 +                               struct au_drinfo_store *w)
11633 +{
11634 +       struct au_drinfo_rev_elm *elm;
11635 +       struct inode *h_dir, *delegated;
11636 +       int err, nelm;
11637 +       struct path infopath = {
11638 +               .mnt = w->h_ppath.mnt
11639 +       };
11640 +
11641 +       h_dir = d_inode(w->h_ppath.dentry);
11642 +       IMustLock(h_dir);
11643 +
11644 +       err = 0;
11645 +       elm = rev->elm;
11646 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11647 +               AuDebugOn(elm->created && elm->info_last);
11648 +               if (elm->created) {
11649 +                       AuDbg("here\n");
11650 +                       delegated = NULL;
11651 +                       infopath.dentry = elm->info_dentry;
11652 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11653 +                                          !w->no_sio);
11654 +                       AuTraceErr(err);
11655 +                       if (unlikely(err == -EWOULDBLOCK))
11656 +                               iput(delegated);
11657 +                       dput(elm->info_dentry);
11658 +               } else if (elm->info_last) {
11659 +                       AuDbg("here\n");
11660 +                       w->fdata->drinfo = *elm->info_last;
11661 +                       memcpy(w->fdata->drinfo.oldname,
11662 +                              elm->info_last->oldname,
11663 +                              elm->info_last->oldnamelen);
11664 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11665 +                       au_kfree_rcu(elm->info_last);
11666 +               }
11667 +               if (unlikely(err))
11668 +                       AuIOErr("%d, %s\n", err, w->whname);
11669 +               /* go on even if err */
11670 +       }
11671 +}
11672 +
11673 +/* caller has to call au_dr_rename_fin() later */
11674 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11675 +                          struct qstr *dst_name, void *_rev)
11676 +{
11677 +       int err, sz, nelm;
11678 +       aufs_bindex_t bindex, btail;
11679 +       struct au_drinfo_store work;
11680 +       struct au_drinfo_rev *rev, **p;
11681 +       struct au_drinfo_rev_elm *elm;
11682 +       struct super_block *sb;
11683 +       struct au_branch *br;
11684 +       struct au_hinode *hdir;
11685 +
11686 +       err = au_drinfo_store_work_init(&work, btgt);
11687 +       AuTraceErr(err);
11688 +       if (unlikely(err))
11689 +               goto out;
11690 +
11691 +       err = -ENOMEM;
11692 +       btail = au_dbtaildir(dentry);
11693 +       nelm = btail - btgt;
11694 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11695 +       rev = kcalloc(1, sz, GFP_NOFS);
11696 +       if (unlikely(!rev)) {
11697 +               AuTraceErr(err);
11698 +               goto out_args;
11699 +       }
11700 +       rev->nelm = nelm;
11701 +       elm = rev->elm;
11702 +       p = _rev;
11703 +       *p = rev;
11704 +
11705 +       err = 0;
11706 +       sb = dentry->d_sb;
11707 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11708 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11709 +       hdir = au_hi(d_inode(dentry), btgt);
11710 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11711 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11712 +               work.h_dentry = au_h_dptr(dentry, bindex);
11713 +               if (!work.h_dentry)
11714 +                       continue;
11715 +
11716 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11717 +                                         &work.allocated);
11718 +               AuTraceErr(err);
11719 +               if (unlikely(err))
11720 +                       break;
11721 +
11722 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11723 +               br = au_sbr(sb, bindex);
11724 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11725 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11726 +                                                work.infonamelen);
11727 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11728 +                     work.whnamelen, work.whname,
11729 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11730 +                     work.fdata->drinfo.oldnamelen,
11731 +                     work.fdata->drinfo.oldname);
11732 +
11733 +               err = au_drinfo_store_sio(&work, elm);
11734 +               AuTraceErr(err);
11735 +               if (unlikely(err))
11736 +                       break;
11737 +       }
11738 +       if (unlikely(err)) {
11739 +               /* revert all drinfo */
11740 +               au_drinfo_store_rev(rev, &work);
11741 +               au_kfree_try_rcu(rev);
11742 +               *p = NULL;
11743 +       }
11744 +       au_hn_inode_unlock(hdir);
11745 +
11746 +out_args:
11747 +       au_drinfo_store_work_fin(&work);
11748 +out:
11749 +       return err;
11750 +}
11751 +
11752 +/* ---------------------------------------------------------------------- */
11753 +
11754 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11755 +                struct qstr *dst_name, void *_rev)
11756 +{
11757 +       int err, already;
11758 +       ino_t ino;
11759 +       struct super_block *sb;
11760 +       struct au_branch *br;
11761 +       struct au_dr_br *dr;
11762 +       struct dentry *h_dentry;
11763 +       struct inode *h_inode;
11764 +       struct au_dr_hino *ent;
11765 +       struct au_drinfo_rev *rev, **p;
11766 +
11767 +       AuDbg("bindex %d\n", bindex);
11768 +
11769 +       err = -ENOMEM;
11770 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11771 +       if (unlikely(!ent))
11772 +               goto out;
11773 +
11774 +       sb = src->d_sb;
11775 +       br = au_sbr(sb, bindex);
11776 +       dr = &br->br_dirren;
11777 +       h_dentry = au_h_dptr(src, bindex);
11778 +       h_inode = d_inode(h_dentry);
11779 +       ino = h_inode->i_ino;
11780 +       ent->dr_h_ino = ino;
11781 +       already = au_dr_hino_test_add(dr, ino, ent);
11782 +       AuDbg("b%d, hi%llu, already %d\n",
11783 +             bindex, (unsigned long long)ino, already);
11784 +
11785 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11786 +       AuTraceErr(err);
11787 +       if (!err) {
11788 +               p = _rev;
11789 +               rev = *p;
11790 +               rev->already = already;
11791 +               goto out; /* success */
11792 +       }
11793 +
11794 +       /* revert */
11795 +       if (!already)
11796 +               au_dr_hino_del(dr, ent);
11797 +       au_kfree_rcu(ent);
11798 +
11799 +out:
11800 +       AuTraceErr(err);
11801 +       return err;
11802 +}
11803 +
11804 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11805 +{
11806 +       struct au_drinfo_rev *rev;
11807 +       struct au_drinfo_rev_elm *elm;
11808 +       int nelm;
11809 +
11810 +       rev = _rev;
11811 +       elm = rev->elm;
11812 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11813 +               dput(elm->info_dentry);
11814 +               au_kfree_rcu(elm->info_last);
11815 +       }
11816 +       au_kfree_try_rcu(rev);
11817 +}
11818 +
11819 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11820 +{
11821 +       int err;
11822 +       struct au_drinfo_store work;
11823 +       struct au_drinfo_rev *rev = _rev;
11824 +       struct super_block *sb;
11825 +       struct au_branch *br;
11826 +       struct inode *h_inode;
11827 +       struct au_dr_br *dr;
11828 +       struct au_dr_hino *ent;
11829 +
11830 +       err = au_drinfo_store_work_init(&work, btgt);
11831 +       if (unlikely(err))
11832 +               goto out;
11833 +
11834 +       sb = src->d_sb;
11835 +       br = au_sbr(sb, btgt);
11836 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11837 +       work.h_ppath.mnt = au_br_mnt(br);
11838 +       au_drinfo_store_rev(rev, &work);
11839 +       au_drinfo_store_work_fin(&work);
11840 +       if (rev->already)
11841 +               goto out;
11842 +
11843 +       dr = &br->br_dirren;
11844 +       h_inode = d_inode(work.h_ppath.dentry);
11845 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11846 +       BUG_ON(!ent);
11847 +       au_dr_hino_del(dr, ent);
11848 +       au_kfree_rcu(ent);
11849 +
11850 +out:
11851 +       au_kfree_try_rcu(rev);
11852 +       if (unlikely(err))
11853 +               pr_err("failed to remove dirren info\n");
11854 +}
11855 +
11856 +/* ---------------------------------------------------------------------- */
11857 +
11858 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11859 +                                          char *whname, int whnamelen,
11860 +                                          struct dentry **info_dentry)
11861 +{
11862 +       struct au_drinfo *drinfo;
11863 +       struct file *f;
11864 +       struct inode *h_dir;
11865 +       struct path infopath;
11866 +       int unlocked;
11867 +
11868 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11869 +
11870 +       *info_dentry = NULL;
11871 +       drinfo = NULL;
11872 +       unlocked = 0;
11873 +       h_dir = d_inode(h_ppath->dentry);
11874 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11875 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11876 +       if (IS_ERR(infopath.dentry)) {
11877 +               drinfo = (void *)infopath.dentry;
11878 +               goto out;
11879 +       }
11880 +
11881 +       if (d_is_negative(infopath.dentry))
11882 +               goto out_dput; /* success */
11883 +
11884 +       infopath.mnt = h_ppath->mnt;
11885 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11886 +       inode_unlock_shared(h_dir);
11887 +       unlocked = 1;
11888 +       if (IS_ERR(f)) {
11889 +               drinfo = (void *)f;
11890 +               goto out_dput;
11891 +       }
11892 +
11893 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11894 +       if (IS_ERR_OR_NULL(drinfo))
11895 +               goto out_fput;
11896 +
11897 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11898 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11899 +
11900 +out_fput:
11901 +       fput(f);
11902 +out_dput:
11903 +       dput(infopath.dentry);
11904 +out:
11905 +       if (!unlocked)
11906 +               inode_unlock_shared(h_dir);
11907 +       AuTraceErrPtr(drinfo);
11908 +       return drinfo;
11909 +}
11910 +
11911 +struct au_drinfo_do_load_args {
11912 +       struct au_drinfo **drinfop;
11913 +       struct path *h_ppath;
11914 +       char *whname;
11915 +       int whnamelen;
11916 +       struct dentry **info_dentry;
11917 +};
11918 +
11919 +static void au_call_drinfo_do_load(void *args)
11920 +{
11921 +       struct au_drinfo_do_load_args *a = args;
11922 +
11923 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11924 +                                       a->info_dentry);
11925 +}
11926 +
11927 +struct au_drinfo_load {
11928 +       struct path h_ppath;
11929 +       struct qstr *qname;
11930 +       unsigned char no_sio;
11931 +
11932 +       aufs_bindex_t ninfo;
11933 +       struct au_drinfo **drinfo;
11934 +};
11935 +
11936 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11937 +                         struct au_branch *br)
11938 +{
11939 +       int err, wkq_err, whnamelen, e;
11940 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11941 +               = AUFS_WH_DR_INFO_PFX;
11942 +       struct au_drinfo *drinfo;
11943 +       struct qstr oldname;
11944 +       struct inode *h_dir, *delegated;
11945 +       struct dentry *info_dentry;
11946 +       struct path infopath;
11947 +
11948 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11949 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11950 +                                   sizeof(whname) - whnamelen);
11951 +       if (w->no_sio)
11952 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11953 +                                          &info_dentry);
11954 +       else {
11955 +               struct au_drinfo_do_load_args args = {
11956 +                       .drinfop        = &drinfo,
11957 +                       .h_ppath        = &w->h_ppath,
11958 +                       .whname         = whname,
11959 +                       .whnamelen      = whnamelen,
11960 +                       .info_dentry    = &info_dentry
11961 +               };
11962 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11963 +               if (unlikely(wkq_err))
11964 +                       drinfo = ERR_PTR(wkq_err);
11965 +       }
11966 +       err = PTR_ERR(drinfo);
11967 +       if (IS_ERR_OR_NULL(drinfo))
11968 +               goto out;
11969 +
11970 +       err = 0;
11971 +       oldname.len = drinfo->oldnamelen;
11972 +       oldname.name = drinfo->oldname;
11973 +       if (au_qstreq(w->qname, &oldname)) {
11974 +               /* the name is renamed back */
11975 +               au_kfree_rcu(drinfo);
11976 +               drinfo = NULL;
11977 +
11978 +               infopath.dentry = info_dentry;
11979 +               infopath.mnt = w->h_ppath.mnt;
11980 +               h_dir = d_inode(w->h_ppath.dentry);
11981 +               delegated = NULL;
11982 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11983 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11984 +               inode_unlock(h_dir);
11985 +               if (unlikely(e))
11986 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11987 +               if (unlikely(e == -EWOULDBLOCK))
11988 +                       iput(delegated);
11989 +       }
11990 +       au_kfree_rcu(w->drinfo[bindex]);
11991 +       w->drinfo[bindex] = drinfo;
11992 +       dput(info_dentry);
11993 +
11994 +out:
11995 +       AuTraceErr(err);
11996 +       return err;
11997 +}
11998 +
11999 +/* ---------------------------------------------------------------------- */
12000 +
12001 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12002 +{
12003 +       struct au_drinfo **p = drinfo;
12004 +
12005 +       while (n-- > 0)
12006 +               au_kfree_rcu(*drinfo++);
12007 +       au_kfree_try_rcu(p);
12008 +}
12009 +
12010 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12011 +              aufs_bindex_t btgt)
12012 +{
12013 +       int err, ninfo;
12014 +       struct au_drinfo_load w;
12015 +       aufs_bindex_t bindex, bbot;
12016 +       struct au_branch *br;
12017 +       struct inode *h_dir;
12018 +       struct au_dr_hino *ent;
12019 +       struct super_block *sb;
12020 +
12021 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12022 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12023 +             AuLNPair(&lkup->whname), btgt);
12024 +
12025 +       sb = dentry->d_sb;
12026 +       bbot = au_sbbot(sb);
12027 +       w.ninfo = bbot + 1;
12028 +       if (!lkup->dirren.drinfo) {
12029 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12030 +                                             sizeof(*lkup->dirren.drinfo),
12031 +                                             GFP_NOFS);
12032 +               if (unlikely(!lkup->dirren.drinfo)) {
12033 +                       err = -ENOMEM;
12034 +                       goto out;
12035 +               }
12036 +               lkup->dirren.ninfo = w.ninfo;
12037 +       }
12038 +       w.drinfo = lkup->dirren.drinfo;
12039 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12040 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12041 +       AuDebugOn(!w.h_ppath.dentry);
12042 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12043 +       w.qname = &dentry->d_name;
12044 +
12045 +       ninfo = 0;
12046 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12047 +               br = au_sbr(sb, bindex);
12048 +               err = au_drinfo_load(&w, bindex, br);
12049 +               if (unlikely(err))
12050 +                       goto out_free;
12051 +               if (w.drinfo[bindex])
12052 +                       ninfo++;
12053 +       }
12054 +       if (!ninfo) {
12055 +               br = au_sbr(sb, btgt);
12056 +               h_dir = d_inode(w.h_ppath.dentry);
12057 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12058 +               AuDebugOn(!ent);
12059 +               au_dr_hino_del(&br->br_dirren, ent);
12060 +               au_kfree_rcu(ent);
12061 +       }
12062 +       goto out; /* success */
12063 +
12064 +out_free:
12065 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12066 +       lkup->dirren.ninfo = 0;
12067 +       lkup->dirren.drinfo = NULL;
12068 +out:
12069 +       AuTraceErr(err);
12070 +       return err;
12071 +}
12072 +
12073 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12074 +{
12075 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12076 +}
12077 +
12078 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12079 +{
12080 +       int err;
12081 +       struct au_drinfo *drinfo;
12082 +
12083 +       err = 0;
12084 +       if (!lkup->dirren.drinfo)
12085 +               goto out;
12086 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12087 +       drinfo = lkup->dirren.drinfo[btgt];
12088 +       if (!drinfo)
12089 +               goto out;
12090 +
12091 +       au_kfree_try_rcu(lkup->whname.name);
12092 +       lkup->whname.name = NULL;
12093 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12094 +       lkup->dirren.dr_name.name = drinfo->oldname;
12095 +       lkup->name = &lkup->dirren.dr_name;
12096 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12097 +       if (!err)
12098 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12099 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12100 +                     btgt);
12101 +
12102 +out:
12103 +       AuTraceErr(err);
12104 +       return err;
12105 +}
12106 +
12107 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12108 +                    ino_t h_ino)
12109 +{
12110 +       int match;
12111 +       struct au_drinfo *drinfo;
12112 +
12113 +       match = 1;
12114 +       if (!lkup->dirren.drinfo)
12115 +               goto out;
12116 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12117 +       drinfo = lkup->dirren.drinfo[bindex];
12118 +       if (!drinfo)
12119 +               goto out;
12120 +
12121 +       match = (drinfo->ino == h_ino);
12122 +       AuDbg("match %d\n", match);
12123 +
12124 +out:
12125 +       return match;
12126 +}
12127 +
12128 +/* ---------------------------------------------------------------------- */
12129 +
12130 +int au_dr_opt_set(struct super_block *sb)
12131 +{
12132 +       int err;
12133 +       aufs_bindex_t bindex, bbot;
12134 +       struct au_branch *br;
12135 +
12136 +       err = 0;
12137 +       bbot = au_sbbot(sb);
12138 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12139 +               br = au_sbr(sb, bindex);
12140 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12141 +       }
12142 +
12143 +       return err;
12144 +}
12145 +
12146 +int au_dr_opt_flush(struct super_block *sb)
12147 +{
12148 +       int err;
12149 +       aufs_bindex_t bindex, bbot;
12150 +       struct au_branch *br;
12151 +
12152 +       err = 0;
12153 +       bbot = au_sbbot(sb);
12154 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12155 +               br = au_sbr(sb, bindex);
12156 +               if (au_br_writable(br->br_perm))
12157 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12158 +       }
12159 +
12160 +       return err;
12161 +}
12162 +
12163 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12164 +{
12165 +       int err;
12166 +       aufs_bindex_t bindex, bbot;
12167 +       struct au_branch *br;
12168 +
12169 +       err = 0;
12170 +       if (!no_flush) {
12171 +               err = au_dr_opt_flush(sb);
12172 +               if (unlikely(err))
12173 +                       goto out;
12174 +       }
12175 +
12176 +       bbot = au_sbbot(sb);
12177 +       for (bindex = 0; bindex <= bbot; bindex++) {
12178 +               br = au_sbr(sb, bindex);
12179 +               au_dr_hino_free(&br->br_dirren);
12180 +       }
12181 +
12182 +out:
12183 +       return err;
12184 +}
12185 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12186 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12187 +++ linux/fs/aufs/dirren.h      2021-11-01 23:48:34.206359262 +0100
12188 @@ -0,0 +1,140 @@
12189 +/* SPDX-License-Identifier: GPL-2.0 */
12190 +/*
12191 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12192 + *
12193 + * This program, aufs is free software; you can redistribute it and/or modify
12194 + * it under the terms of the GNU General Public License as published by
12195 + * the Free Software Foundation; either version 2 of the License, or
12196 + * (at your option) any later version.
12197 + *
12198 + * This program is distributed in the hope that it will be useful,
12199 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12200 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12201 + * GNU General Public License for more details.
12202 + *
12203 + * You should have received a copy of the GNU General Public License
12204 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12205 + */
12206 +
12207 +/*
12208 + * renamed dir info
12209 + */
12210 +
12211 +#ifndef __AUFS_DIRREN_H__
12212 +#define __AUFS_DIRREN_H__
12213 +
12214 +#ifdef __KERNEL__
12215 +
12216 +#include <linux/dcache.h>
12217 +#include <linux/statfs.h>
12218 +#include <linux/uuid.h>
12219 +#include "hbl.h"
12220 +
12221 +#define AuDirren_NHASH 100
12222 +
12223 +#ifdef CONFIG_AUFS_DIRREN
12224 +enum au_brid_type {
12225 +       AuBrid_Unset,
12226 +       AuBrid_UUID,
12227 +       AuBrid_FSID,
12228 +       AuBrid_DEV
12229 +};
12230 +
12231 +struct au_dr_brid {
12232 +       enum au_brid_type       type;
12233 +       union {
12234 +               uuid_t  uuid;   /* unimplemented yet */
12235 +               fsid_t  fsid;
12236 +               dev_t   dev;
12237 +       };
12238 +};
12239 +
12240 +/* 20 is the max digits length of ulong 64 */
12241 +/* brid-type "_" uuid "_" inum */
12242 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12243 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12244 +
12245 +struct au_dr_hino {
12246 +       struct hlist_bl_node    dr_hnode;
12247 +       ino_t                   dr_h_ino;
12248 +};
12249 +
12250 +struct au_dr_br {
12251 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12252 +       struct au_dr_brid       dr_brid;
12253 +};
12254 +
12255 +struct au_dr_lookup {
12256 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12257 +       struct qstr             dr_name; /* subset of dr_info */
12258 +       aufs_bindex_t           ninfo;
12259 +       struct au_drinfo        **drinfo;
12260 +};
12261 +#else
12262 +struct au_dr_hino;
12263 +/* empty */
12264 +struct au_dr_br { };
12265 +struct au_dr_lookup { };
12266 +#endif
12267 +
12268 +/* ---------------------------------------------------------------------- */
12269 +
12270 +struct au_branch;
12271 +struct au_do_lookup_args;
12272 +struct au_hinode;
12273 +#ifdef CONFIG_AUFS_DIRREN
12274 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12275 +                       struct au_dr_hino *add_ent);
12276 +void au_dr_hino_free(struct au_dr_br *dr);
12277 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12278 +                 const struct path *path);
12279 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12280 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12281 +                struct qstr *dst_name, void *_rev);
12282 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12283 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12284 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12285 +              aufs_bindex_t bindex);
12286 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12287 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12288 +                    ino_t h_ino);
12289 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12290 +int au_dr_opt_set(struct super_block *sb);
12291 +int au_dr_opt_flush(struct super_block *sb);
12292 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12293 +#else
12294 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12295 +          struct au_dr_hino *add_ent);
12296 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12297 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12298 +          const struct path *path);
12299 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12300 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12301 +          struct qstr *dst_name, void *_rev);
12302 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12303 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12304 +          void *rev);
12305 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12306 +          aufs_bindex_t bindex);
12307 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12308 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12309 +          aufs_bindex_t bindex, ino_t h_ino);
12310 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12311 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12312 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12313 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12314 +#endif
12315 +
12316 +/* ---------------------------------------------------------------------- */
12317 +
12318 +#ifdef CONFIG_AUFS_DIRREN
12319 +static inline int au_dr_ihash(ino_t h_ino)
12320 +{
12321 +       return h_ino % AuDirren_NHASH;
12322 +}
12323 +#else
12324 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12325 +#endif
12326 +
12327 +#endif /* __KERNEL__ */
12328 +#endif /* __AUFS_DIRREN_H__ */
12329 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12330 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12331 +++ linux/fs/aufs/dynop.c       2021-11-01 23:48:34.206359262 +0100
12332 @@ -0,0 +1,368 @@
12333 +// SPDX-License-Identifier: GPL-2.0
12334 +/*
12335 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12336 + *
12337 + * This program, aufs is free software; you can redistribute it and/or modify
12338 + * it under the terms of the GNU General Public License as published by
12339 + * the Free Software Foundation; either version 2 of the License, or
12340 + * (at your option) any later version.
12341 + *
12342 + * This program is distributed in the hope that it will be useful,
12343 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12344 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12345 + * GNU General Public License for more details.
12346 + *
12347 + * You should have received a copy of the GNU General Public License
12348 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12349 + */
12350 +
12351 +/*
12352 + * dynamically customizable operations for regular files
12353 + */
12354 +
12355 +#include "aufs.h"
12356 +
12357 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12358 +
12359 +/*
12360 + * How large will these lists be?
12361 + * Usually just a few elements, 20-30 at most for each, I guess.
12362 + */
12363 +static struct hlist_bl_head dynop[AuDyLast];
12364 +
12365 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12366 +                                    const void *h_op)
12367 +{
12368 +       struct au_dykey *key, *tmp;
12369 +       struct hlist_bl_node *pos;
12370 +
12371 +       key = NULL;
12372 +       hlist_bl_lock(hbl);
12373 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12374 +               if (tmp->dk_op.dy_hop == h_op) {
12375 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12376 +                               key = tmp;
12377 +                       break;
12378 +               }
12379 +       hlist_bl_unlock(hbl);
12380 +
12381 +       return key;
12382 +}
12383 +
12384 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12385 +{
12386 +       struct au_dykey **k, *found;
12387 +       const void *h_op = key->dk_op.dy_hop;
12388 +       int i;
12389 +
12390 +       found = NULL;
12391 +       k = br->br_dykey;
12392 +       for (i = 0; i < AuBrDynOp; i++)
12393 +               if (k[i]) {
12394 +                       if (k[i]->dk_op.dy_hop == h_op) {
12395 +                               found = k[i];
12396 +                               break;
12397 +                       }
12398 +               } else
12399 +                       break;
12400 +       if (!found) {
12401 +               spin_lock(&br->br_dykey_lock);
12402 +               for (; i < AuBrDynOp; i++)
12403 +                       if (k[i]) {
12404 +                               if (k[i]->dk_op.dy_hop == h_op) {
12405 +                                       found = k[i];
12406 +                                       break;
12407 +                               }
12408 +                       } else {
12409 +                               k[i] = key;
12410 +                               break;
12411 +                       }
12412 +               spin_unlock(&br->br_dykey_lock);
12413 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12414 +       }
12415 +
12416 +       return found;
12417 +}
12418 +
12419 +/* kref_get() if @key is already added */
12420 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12421 +{
12422 +       struct au_dykey *tmp, *found;
12423 +       struct hlist_bl_node *pos;
12424 +       const void *h_op = key->dk_op.dy_hop;
12425 +
12426 +       found = NULL;
12427 +       hlist_bl_lock(hbl);
12428 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12429 +               if (tmp->dk_op.dy_hop == h_op) {
12430 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12431 +                               found = tmp;
12432 +                       break;
12433 +               }
12434 +       if (!found)
12435 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12436 +       hlist_bl_unlock(hbl);
12437 +
12438 +       if (!found)
12439 +               DyPrSym(key);
12440 +       return found;
12441 +}
12442 +
12443 +static void dy_free_rcu(struct rcu_head *rcu)
12444 +{
12445 +       struct au_dykey *key;
12446 +
12447 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12448 +       DyPrSym(key);
12449 +       kfree(key);
12450 +}
12451 +
12452 +static void dy_free(struct kref *kref)
12453 +{
12454 +       struct au_dykey *key;
12455 +       struct hlist_bl_head *hbl;
12456 +
12457 +       key = container_of(kref, struct au_dykey, dk_kref);
12458 +       hbl = dynop + key->dk_op.dy_type;
12459 +       au_hbl_del(&key->dk_hnode, hbl);
12460 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12461 +}
12462 +
12463 +void au_dy_put(struct au_dykey *key)
12464 +{
12465 +       kref_put(&key->dk_kref, dy_free);
12466 +}
12467 +
12468 +/* ---------------------------------------------------------------------- */
12469 +
12470 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12471 +
12472 +#ifdef CONFIG_AUFS_DEBUG
12473 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12474 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12475 +#else
12476 +#define DyDbgDeclare(cnt)      do {} while (0)
12477 +#define DyDbgInc(cnt)          do {} while (0)
12478 +#endif
12479 +
12480 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12481 +       DyDbgInc(cnt);                                                  \
12482 +       if (h_op->func) {                                               \
12483 +               if (src.func)                                           \
12484 +                       dst.func = src.func;                            \
12485 +               else                                                    \
12486 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12487 +       }                                                               \
12488 +} while (0)
12489 +
12490 +#define DySetForce(func, dst, src) do {                \
12491 +       AuDebugOn(!src.func);                   \
12492 +       DyDbgInc(cnt);                          \
12493 +       dst.func = src.func;                    \
12494 +} while (0)
12495 +
12496 +#define DySetAop(func) \
12497 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12498 +#define DySetAopForce(func) \
12499 +       DySetForce(func, dyaop->da_op, aufs_aop)
12500 +
12501 +static void dy_aop(struct au_dykey *key, const void *h_op,
12502 +                  struct super_block *h_sb __maybe_unused)
12503 +{
12504 +       struct au_dyaop *dyaop = (void *)key;
12505 +       const struct address_space_operations *h_aop = h_op;
12506 +       DyDbgDeclare(cnt);
12507 +
12508 +       AuDbg("%s\n", au_sbtype(h_sb));
12509 +
12510 +       DySetAop(writepage);
12511 +       DySetAopForce(readpage);        /* force */
12512 +       DySetAop(writepages);
12513 +       DySetAop(set_page_dirty);
12514 +       DySetAop(readpages);
12515 +       DySetAop(readahead);
12516 +       DySetAop(write_begin);
12517 +       DySetAop(write_end);
12518 +       DySetAop(bmap);
12519 +       DySetAop(invalidatepage);
12520 +       DySetAop(releasepage);
12521 +       DySetAop(freepage);
12522 +       /* this one will be changed according to an aufs mount option */
12523 +       DySetAop(direct_IO);
12524 +       DySetAop(migratepage);
12525 +       DySetAop(isolate_page);
12526 +       DySetAop(putback_page);
12527 +       DySetAop(launder_page);
12528 +       DySetAop(is_partially_uptodate);
12529 +       DySetAop(is_dirty_writeback);
12530 +       DySetAop(error_remove_page);
12531 +       DySetAop(swap_activate);
12532 +       DySetAop(swap_deactivate);
12533 +
12534 +       DyDbgSize(cnt, *h_aop);
12535 +}
12536 +
12537 +/* ---------------------------------------------------------------------- */
12538 +
12539 +static void dy_bug(struct kref *kref)
12540 +{
12541 +       BUG();
12542 +}
12543 +
12544 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12545 +{
12546 +       struct au_dykey *key, *old;
12547 +       struct hlist_bl_head *hbl;
12548 +       struct op {
12549 +               unsigned int sz;
12550 +               void (*set)(struct au_dykey *key, const void *h_op,
12551 +                           struct super_block *h_sb __maybe_unused);
12552 +       };
12553 +       static const struct op a[] = {
12554 +               [AuDy_AOP] = {
12555 +                       .sz     = sizeof(struct au_dyaop),
12556 +                       .set    = dy_aop
12557 +               }
12558 +       };
12559 +       const struct op *p;
12560 +
12561 +       hbl = dynop + op->dy_type;
12562 +       key = dy_gfind_get(hbl, op->dy_hop);
12563 +       if (key)
12564 +               goto out_add; /* success */
12565 +
12566 +       p = a + op->dy_type;
12567 +       key = kzalloc(p->sz, GFP_NOFS);
12568 +       if (unlikely(!key)) {
12569 +               key = ERR_PTR(-ENOMEM);
12570 +               goto out;
12571 +       }
12572 +
12573 +       key->dk_op.dy_hop = op->dy_hop;
12574 +       kref_init(&key->dk_kref);
12575 +       p->set(key, op->dy_hop, au_br_sb(br));
12576 +       old = dy_gadd(hbl, key);
12577 +       if (old) {
12578 +               au_kfree_rcu(key);
12579 +               key = old;
12580 +       }
12581 +
12582 +out_add:
12583 +       old = dy_bradd(br, key);
12584 +       if (old)
12585 +               /* its ref-count should never be zero here */
12586 +               kref_put(&key->dk_kref, dy_bug);
12587 +out:
12588 +       return key;
12589 +}
12590 +
12591 +/* ---------------------------------------------------------------------- */
12592 +/*
12593 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12594 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12595 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12596 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12597 + * See the aufs manual in detail.
12598 + */
12599 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12600 +{
12601 +       if (!do_dx)
12602 +               dyaop->da_op.direct_IO = NULL;
12603 +       else
12604 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12605 +}
12606 +
12607 +static struct au_dyaop *dy_aget(struct au_branch *br,
12608 +                               const struct address_space_operations *h_aop,
12609 +                               int do_dx)
12610 +{
12611 +       struct au_dyaop *dyaop;
12612 +       struct au_dynop op;
12613 +
12614 +       op.dy_type = AuDy_AOP;
12615 +       op.dy_haop = h_aop;
12616 +       dyaop = (void *)dy_get(&op, br);
12617 +       if (IS_ERR(dyaop))
12618 +               goto out;
12619 +       dy_adx(dyaop, do_dx);
12620 +
12621 +out:
12622 +       return dyaop;
12623 +}
12624 +
12625 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12626 +               struct inode *h_inode)
12627 +{
12628 +       int err, do_dx;
12629 +       struct super_block *sb;
12630 +       struct au_branch *br;
12631 +       struct au_dyaop *dyaop;
12632 +
12633 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12634 +       IiMustWriteLock(inode);
12635 +
12636 +       sb = inode->i_sb;
12637 +       br = au_sbr(sb, bindex);
12638 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12639 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12640 +       err = PTR_ERR(dyaop);
12641 +       if (IS_ERR(dyaop))
12642 +               /* unnecessary to call dy_fput() */
12643 +               goto out;
12644 +
12645 +       err = 0;
12646 +       inode->i_mapping->a_ops = &dyaop->da_op;
12647 +
12648 +out:
12649 +       return err;
12650 +}
12651 +
12652 +/*
12653 + * Is it safe to replace a_ops during the inode/file is in operation?
12654 + * Yes, I hope so.
12655 + */
12656 +int au_dy_irefresh(struct inode *inode)
12657 +{
12658 +       int err;
12659 +       aufs_bindex_t btop;
12660 +       struct inode *h_inode;
12661 +
12662 +       err = 0;
12663 +       if (S_ISREG(inode->i_mode)) {
12664 +               btop = au_ibtop(inode);
12665 +               h_inode = au_h_iptr(inode, btop);
12666 +               err = au_dy_iaop(inode, btop, h_inode);
12667 +       }
12668 +       return err;
12669 +}
12670 +
12671 +void au_dy_arefresh(int do_dx)
12672 +{
12673 +       struct hlist_bl_head *hbl;
12674 +       struct hlist_bl_node *pos;
12675 +       struct au_dykey *key;
12676 +
12677 +       hbl = dynop + AuDy_AOP;
12678 +       hlist_bl_lock(hbl);
12679 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12680 +               dy_adx((void *)key, do_dx);
12681 +       hlist_bl_unlock(hbl);
12682 +}
12683 +
12684 +/* ---------------------------------------------------------------------- */
12685 +
12686 +void __init au_dy_init(void)
12687 +{
12688 +       int i;
12689 +
12690 +       for (i = 0; i < AuDyLast; i++)
12691 +               INIT_HLIST_BL_HEAD(dynop + i);
12692 +}
12693 +
12694 +void au_dy_fin(void)
12695 +{
12696 +       int i;
12697 +
12698 +       for (i = 0; i < AuDyLast; i++)
12699 +               WARN_ON(!hlist_bl_empty(dynop + i));
12700 +}
12701 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12702 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12703 +++ linux/fs/aufs/dynop.h       2021-11-01 23:48:34.206359262 +0100
12704 @@ -0,0 +1,77 @@
12705 +/* SPDX-License-Identifier: GPL-2.0 */
12706 +/*
12707 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12708 + *
12709 + * This program, aufs is free software; you can redistribute it and/or modify
12710 + * it under the terms of the GNU General Public License as published by
12711 + * the Free Software Foundation; either version 2 of the License, or
12712 + * (at your option) any later version.
12713 + *
12714 + * This program is distributed in the hope that it will be useful,
12715 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12716 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12717 + * GNU General Public License for more details.
12718 + *
12719 + * You should have received a copy of the GNU General Public License
12720 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12721 + */
12722 +
12723 +/*
12724 + * dynamically customizable operations (for regular files only)
12725 + */
12726 +
12727 +#ifndef __AUFS_DYNOP_H__
12728 +#define __AUFS_DYNOP_H__
12729 +
12730 +#ifdef __KERNEL__
12731 +
12732 +#include <linux/fs.h>
12733 +#include <linux/kref.h>
12734 +
12735 +enum {AuDy_AOP, AuDyLast};
12736 +
12737 +struct au_dynop {
12738 +       int                                             dy_type;
12739 +       union {
12740 +               const void                              *dy_hop;
12741 +               const struct address_space_operations   *dy_haop;
12742 +       };
12743 +};
12744 +
12745 +struct au_dykey {
12746 +       union {
12747 +               struct hlist_bl_node    dk_hnode;
12748 +               struct rcu_head         dk_rcu;
12749 +       };
12750 +       struct au_dynop         dk_op;
12751 +
12752 +       /*
12753 +        * during I am in the branch local array, kref is gotten. when the
12754 +        * branch is removed, kref is put.
12755 +        */
12756 +       struct kref             dk_kref;
12757 +};
12758 +
12759 +/* stop unioning since their sizes are very different from each other */
12760 +struct au_dyaop {
12761 +       struct au_dykey                 da_key;
12762 +       struct address_space_operations da_op; /* not const */
12763 +};
12764 +/* make sure that 'struct au_dykey *' can be any type */
12765 +static_assert(!offsetof(struct au_dyaop, da_key));
12766 +
12767 +/* ---------------------------------------------------------------------- */
12768 +
12769 +/* dynop.c */
12770 +struct au_branch;
12771 +void au_dy_put(struct au_dykey *key);
12772 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12773 +               struct inode *h_inode);
12774 +int au_dy_irefresh(struct inode *inode);
12775 +void au_dy_arefresh(int do_dio);
12776 +
12777 +void __init au_dy_init(void);
12778 +void au_dy_fin(void);
12779 +
12780 +#endif /* __KERNEL__ */
12781 +#endif /* __AUFS_DYNOP_H__ */
12782 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12783 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12784 +++ linux/fs/aufs/export.c      2021-11-01 23:48:34.206359262 +0100
12785 @@ -0,0 +1,831 @@
12786 +// SPDX-License-Identifier: GPL-2.0
12787 +/*
12788 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12789 + *
12790 + * This program, aufs is free software; you can redistribute it and/or modify
12791 + * it under the terms of the GNU General Public License as published by
12792 + * the Free Software Foundation; either version 2 of the License, or
12793 + * (at your option) any later version.
12794 + *
12795 + * This program is distributed in the hope that it will be useful,
12796 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12797 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12798 + * GNU General Public License for more details.
12799 + *
12800 + * You should have received a copy of the GNU General Public License
12801 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12802 + */
12803 +
12804 +/*
12805 + * export via nfs
12806 + */
12807 +
12808 +#include <linux/exportfs.h>
12809 +#include <linux/fs_struct.h>
12810 +#include <linux/namei.h>
12811 +#include <linux/nsproxy.h>
12812 +#include <linux/random.h>
12813 +#include <linux/writeback.h>
12814 +#include "aufs.h"
12815 +
12816 +union conv {
12817 +#ifdef CONFIG_AUFS_INO_T_64
12818 +       __u32 a[2];
12819 +#else
12820 +       __u32 a[1];
12821 +#endif
12822 +       ino_t ino;
12823 +};
12824 +
12825 +static ino_t decode_ino(__u32 *a)
12826 +{
12827 +       union conv u;
12828 +
12829 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12830 +       u.a[0] = a[0];
12831 +#ifdef CONFIG_AUFS_INO_T_64
12832 +       u.a[1] = a[1];
12833 +#endif
12834 +       return u.ino;
12835 +}
12836 +
12837 +static void encode_ino(__u32 *a, ino_t ino)
12838 +{
12839 +       union conv u;
12840 +
12841 +       u.ino = ino;
12842 +       a[0] = u.a[0];
12843 +#ifdef CONFIG_AUFS_INO_T_64
12844 +       a[1] = u.a[1];
12845 +#endif
12846 +}
12847 +
12848 +/* NFS file handle */
12849 +enum {
12850 +       Fh_br_id,
12851 +       Fh_sigen,
12852 +#ifdef CONFIG_AUFS_INO_T_64
12853 +       /* support 64bit inode number */
12854 +       Fh_ino1,
12855 +       Fh_ino2,
12856 +       Fh_dir_ino1,
12857 +       Fh_dir_ino2,
12858 +#else
12859 +       Fh_ino1,
12860 +       Fh_dir_ino1,
12861 +#endif
12862 +       Fh_igen,
12863 +       Fh_h_type,
12864 +       Fh_tail,
12865 +
12866 +       Fh_ino = Fh_ino1,
12867 +       Fh_dir_ino = Fh_dir_ino1
12868 +};
12869 +
12870 +static int au_test_anon(struct dentry *dentry)
12871 +{
12872 +       /* note: read d_flags without d_lock */
12873 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12874 +}
12875 +
12876 +int au_test_nfsd(void)
12877 +{
12878 +       int ret;
12879 +       struct task_struct *tsk = current;
12880 +       char comm[sizeof(tsk->comm)];
12881 +
12882 +       ret = 0;
12883 +       if (tsk->flags & PF_KTHREAD) {
12884 +               get_task_comm(comm, tsk);
12885 +               ret = !strcmp(comm, "nfsd");
12886 +       }
12887 +
12888 +       return ret;
12889 +}
12890 +
12891 +/* ---------------------------------------------------------------------- */
12892 +/* inode generation external table */
12893 +
12894 +void au_xigen_inc(struct inode *inode)
12895 +{
12896 +       loff_t pos;
12897 +       ssize_t sz;
12898 +       __u32 igen;
12899 +       struct super_block *sb;
12900 +       struct au_sbinfo *sbinfo;
12901 +
12902 +       sb = inode->i_sb;
12903 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12904 +
12905 +       sbinfo = au_sbi(sb);
12906 +       pos = inode->i_ino;
12907 +       pos *= sizeof(igen);
12908 +       igen = inode->i_generation + 1;
12909 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12910 +       if (sz == sizeof(igen))
12911 +               return; /* success */
12912 +
12913 +       if (unlikely(sz >= 0))
12914 +               AuIOErr("xigen error (%zd)\n", sz);
12915 +}
12916 +
12917 +int au_xigen_new(struct inode *inode)
12918 +{
12919 +       int err;
12920 +       loff_t pos;
12921 +       ssize_t sz;
12922 +       struct super_block *sb;
12923 +       struct au_sbinfo *sbinfo;
12924 +       struct file *file;
12925 +
12926 +       err = 0;
12927 +       /* todo: dirty, at mount time */
12928 +       if (inode->i_ino == AUFS_ROOT_INO)
12929 +               goto out;
12930 +       sb = inode->i_sb;
12931 +       SiMustAnyLock(sb);
12932 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12933 +               goto out;
12934 +
12935 +       err = -EFBIG;
12936 +       pos = inode->i_ino;
12937 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12938 +               AuIOErr1("too large i%lld\n", pos);
12939 +               goto out;
12940 +       }
12941 +       pos *= sizeof(inode->i_generation);
12942 +
12943 +       err = 0;
12944 +       sbinfo = au_sbi(sb);
12945 +       file = sbinfo->si_xigen;
12946 +       BUG_ON(!file);
12947 +
12948 +       if (vfsub_f_size_read(file)
12949 +           < pos + sizeof(inode->i_generation)) {
12950 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12951 +               sz = xino_fwrite(file, &inode->i_generation,
12952 +                                sizeof(inode->i_generation), &pos);
12953 +       } else
12954 +               sz = xino_fread(file, &inode->i_generation,
12955 +                               sizeof(inode->i_generation), &pos);
12956 +       if (sz == sizeof(inode->i_generation))
12957 +               goto out; /* success */
12958 +
12959 +       err = sz;
12960 +       if (unlikely(sz >= 0)) {
12961 +               err = -EIO;
12962 +               AuIOErr("xigen error (%zd)\n", sz);
12963 +       }
12964 +
12965 +out:
12966 +       return err;
12967 +}
12968 +
12969 +int au_xigen_set(struct super_block *sb, struct path *path)
12970 +{
12971 +       int err;
12972 +       struct au_sbinfo *sbinfo;
12973 +       struct file *file;
12974 +
12975 +       SiMustWriteLock(sb);
12976 +
12977 +       sbinfo = au_sbi(sb);
12978 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12979 +       err = PTR_ERR(file);
12980 +       if (IS_ERR(file))
12981 +               goto out;
12982 +       err = 0;
12983 +       if (sbinfo->si_xigen)
12984 +               fput(sbinfo->si_xigen);
12985 +       sbinfo->si_xigen = file;
12986 +
12987 +out:
12988 +       AuTraceErr(err);
12989 +       return err;
12990 +}
12991 +
12992 +void au_xigen_clr(struct super_block *sb)
12993 +{
12994 +       struct au_sbinfo *sbinfo;
12995 +
12996 +       SiMustWriteLock(sb);
12997 +
12998 +       sbinfo = au_sbi(sb);
12999 +       if (sbinfo->si_xigen) {
13000 +               fput(sbinfo->si_xigen);
13001 +               sbinfo->si_xigen = NULL;
13002 +       }
13003 +}
13004 +
13005 +/* ---------------------------------------------------------------------- */
13006 +
13007 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13008 +                                   ino_t dir_ino)
13009 +{
13010 +       struct dentry *dentry, *d;
13011 +       struct inode *inode;
13012 +       unsigned int sigen;
13013 +
13014 +       dentry = NULL;
13015 +       inode = ilookup(sb, ino);
13016 +       if (!inode)
13017 +               goto out;
13018 +
13019 +       dentry = ERR_PTR(-ESTALE);
13020 +       sigen = au_sigen(sb);
13021 +       if (unlikely(au_is_bad_inode(inode)
13022 +                    || IS_DEADDIR(inode)
13023 +                    || sigen != au_iigen(inode, NULL)))
13024 +               goto out_iput;
13025 +
13026 +       dentry = NULL;
13027 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13028 +               dentry = d_find_alias(inode);
13029 +       else {
13030 +               spin_lock(&inode->i_lock);
13031 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13032 +                       spin_lock(&d->d_lock);
13033 +                       if (!au_test_anon(d)
13034 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13035 +                               dentry = dget_dlock(d);
13036 +                               spin_unlock(&d->d_lock);
13037 +                               break;
13038 +                       }
13039 +                       spin_unlock(&d->d_lock);
13040 +               }
13041 +               spin_unlock(&inode->i_lock);
13042 +       }
13043 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13044 +               /* need to refresh */
13045 +               dput(dentry);
13046 +               dentry = NULL;
13047 +       }
13048 +
13049 +out_iput:
13050 +       iput(inode);
13051 +out:
13052 +       AuTraceErrPtr(dentry);
13053 +       return dentry;
13054 +}
13055 +
13056 +/* ---------------------------------------------------------------------- */
13057 +
13058 +/* todo: dirty? */
13059 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13060 +
13061 +struct au_compare_mnt_args {
13062 +       /* input */
13063 +       struct super_block *sb;
13064 +
13065 +       /* output */
13066 +       struct vfsmount *mnt;
13067 +};
13068 +
13069 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13070 +{
13071 +       struct au_compare_mnt_args *a = arg;
13072 +
13073 +       if (mnt->mnt_sb != a->sb)
13074 +               return 0;
13075 +       a->mnt = mntget(mnt);
13076 +       return 1;
13077 +}
13078 +
13079 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13080 +{
13081 +       int err;
13082 +       struct path root;
13083 +       struct au_compare_mnt_args args = {
13084 +               .sb = sb
13085 +       };
13086 +
13087 +       get_fs_root(current->fs, &root);
13088 +       rcu_read_lock();
13089 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13090 +       rcu_read_unlock();
13091 +       path_put(&root);
13092 +       AuDebugOn(!err);
13093 +       AuDebugOn(!args.mnt);
13094 +       return args.mnt;
13095 +}
13096 +
13097 +struct au_nfsd_si_lock {
13098 +       unsigned int sigen;
13099 +       aufs_bindex_t bindex, br_id;
13100 +       unsigned char force_lock;
13101 +};
13102 +
13103 +static int si_nfsd_read_lock(struct super_block *sb,
13104 +                            struct au_nfsd_si_lock *nsi_lock)
13105 +{
13106 +       int err;
13107 +       aufs_bindex_t bindex;
13108 +
13109 +       si_read_lock(sb, AuLock_FLUSH);
13110 +
13111 +       /* branch id may be wrapped around */
13112 +       err = 0;
13113 +       bindex = au_br_index(sb, nsi_lock->br_id);
13114 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13115 +               goto out; /* success */
13116 +
13117 +       err = -ESTALE;
13118 +       bindex = -1;
13119 +       if (!nsi_lock->force_lock)
13120 +               si_read_unlock(sb);
13121 +
13122 +out:
13123 +       nsi_lock->bindex = bindex;
13124 +       return err;
13125 +}
13126 +
13127 +struct find_name_by_ino {
13128 +       struct dir_context ctx;
13129 +       int called, found;
13130 +       ino_t ino;
13131 +       char *name;
13132 +       int namelen;
13133 +};
13134 +
13135 +static int
13136 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13137 +                loff_t offset, u64 ino, unsigned int d_type)
13138 +{
13139 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13140 +                                                 ctx);
13141 +
13142 +       a->called++;
13143 +       if (a->ino != ino)
13144 +               return 0;
13145 +
13146 +       memcpy(a->name, name, namelen);
13147 +       a->namelen = namelen;
13148 +       a->found = 1;
13149 +       return 1;
13150 +}
13151 +
13152 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13153 +                                    struct au_nfsd_si_lock *nsi_lock)
13154 +{
13155 +       struct dentry *dentry, *parent;
13156 +       struct file *file;
13157 +       struct inode *dir;
13158 +       struct find_name_by_ino arg = {
13159 +               .ctx = {
13160 +                       .actor = find_name_by_ino
13161 +               }
13162 +       };
13163 +       int err;
13164 +
13165 +       parent = path->dentry;
13166 +       if (nsi_lock)
13167 +               si_read_unlock(parent->d_sb);
13168 +       file = vfsub_dentry_open(path, au_dir_roflags);
13169 +       dentry = (void *)file;
13170 +       if (IS_ERR(file))
13171 +               goto out;
13172 +
13173 +       dentry = ERR_PTR(-ENOMEM);
13174 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13175 +       if (unlikely(!arg.name))
13176 +               goto out_file;
13177 +       arg.ino = ino;
13178 +       arg.found = 0;
13179 +       do {
13180 +               arg.called = 0;
13181 +               /* smp_mb(); */
13182 +               err = vfsub_iterate_dir(file, &arg.ctx);
13183 +       } while (!err && !arg.found && arg.called);
13184 +       dentry = ERR_PTR(err);
13185 +       if (unlikely(err))
13186 +               goto out_name;
13187 +       /* instead of ENOENT */
13188 +       dentry = ERR_PTR(-ESTALE);
13189 +       if (!arg.found)
13190 +               goto out_name;
13191 +
13192 +       /* do not call vfsub_lkup_one() */
13193 +       dir = d_inode(parent);
13194 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13195 +       AuTraceErrPtr(dentry);
13196 +       if (IS_ERR(dentry))
13197 +               goto out_name;
13198 +       AuDebugOn(au_test_anon(dentry));
13199 +       if (unlikely(d_really_is_negative(dentry))) {
13200 +               dput(dentry);
13201 +               dentry = ERR_PTR(-ENOENT);
13202 +       }
13203 +
13204 +out_name:
13205 +       free_page((unsigned long)arg.name);
13206 +out_file:
13207 +       fput(file);
13208 +out:
13209 +       if (unlikely(nsi_lock
13210 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13211 +               if (!IS_ERR(dentry)) {
13212 +                       dput(dentry);
13213 +                       dentry = ERR_PTR(-ESTALE);
13214 +               }
13215 +       AuTraceErrPtr(dentry);
13216 +       return dentry;
13217 +}
13218 +
13219 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13220 +                                       ino_t dir_ino,
13221 +                                       struct au_nfsd_si_lock *nsi_lock)
13222 +{
13223 +       struct dentry *dentry;
13224 +       struct path path;
13225 +
13226 +       if (dir_ino != AUFS_ROOT_INO) {
13227 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13228 +               dentry = path.dentry;
13229 +               if (!path.dentry || IS_ERR(path.dentry))
13230 +                       goto out;
13231 +               AuDebugOn(au_test_anon(path.dentry));
13232 +       } else
13233 +               path.dentry = dget(sb->s_root);
13234 +
13235 +       path.mnt = au_mnt_get(sb);
13236 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13237 +       path_put(&path);
13238 +
13239 +out:
13240 +       AuTraceErrPtr(dentry);
13241 +       return dentry;
13242 +}
13243 +
13244 +/* ---------------------------------------------------------------------- */
13245 +
13246 +static int h_acceptable(void *expv, struct dentry *dentry)
13247 +{
13248 +       return 1;
13249 +}
13250 +
13251 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13252 +                          char *buf, int len, struct super_block *sb)
13253 +{
13254 +       char *p;
13255 +       int n;
13256 +       struct path path;
13257 +
13258 +       p = d_path(h_rootpath, buf, len);
13259 +       if (IS_ERR(p))
13260 +               goto out;
13261 +       n = strlen(p);
13262 +
13263 +       path.mnt = h_rootpath->mnt;
13264 +       path.dentry = h_parent;
13265 +       p = d_path(&path, buf, len);
13266 +       if (IS_ERR(p))
13267 +               goto out;
13268 +       if (n != 1)
13269 +               p += n;
13270 +
13271 +       path.mnt = au_mnt_get(sb);
13272 +       path.dentry = sb->s_root;
13273 +       p = d_path(&path, buf, len - strlen(p));
13274 +       mntput(path.mnt);
13275 +       if (IS_ERR(p))
13276 +               goto out;
13277 +       if (n != 1)
13278 +               p[strlen(p)] = '/';
13279 +
13280 +out:
13281 +       AuTraceErrPtr(p);
13282 +       return p;
13283 +}
13284 +
13285 +static
13286 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13287 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13288 +{
13289 +       struct dentry *dentry, *h_parent, *root;
13290 +       struct super_block *h_sb;
13291 +       char *pathname, *p;
13292 +       struct vfsmount *h_mnt;
13293 +       struct au_branch *br;
13294 +       int err;
13295 +       struct path path;
13296 +
13297 +       br = au_sbr(sb, nsi_lock->bindex);
13298 +       h_mnt = au_br_mnt(br);
13299 +       h_sb = h_mnt->mnt_sb;
13300 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13301 +       lockdep_off();
13302 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13303 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13304 +                                     h_acceptable, /*context*/NULL);
13305 +       lockdep_on();
13306 +       dentry = h_parent;
13307 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13308 +               AuWarn1("%s decode_fh failed, %ld\n",
13309 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13310 +               goto out;
13311 +       }
13312 +       dentry = NULL;
13313 +       if (unlikely(au_test_anon(h_parent))) {
13314 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13315 +                       au_sbtype(h_sb));
13316 +               goto out_h_parent;
13317 +       }
13318 +
13319 +       dentry = ERR_PTR(-ENOMEM);
13320 +       pathname = (void *)__get_free_page(GFP_NOFS);
13321 +       if (unlikely(!pathname))
13322 +               goto out_h_parent;
13323 +
13324 +       root = sb->s_root;
13325 +       path.mnt = h_mnt;
13326 +       di_read_lock_parent(root, !AuLock_IR);
13327 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13328 +       di_read_unlock(root, !AuLock_IR);
13329 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13330 +       dentry = (void *)p;
13331 +       if (IS_ERR(p))
13332 +               goto out_pathname;
13333 +
13334 +       si_read_unlock(sb);
13335 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13336 +       dentry = ERR_PTR(err);
13337 +       if (unlikely(err))
13338 +               goto out_relock;
13339 +
13340 +       dentry = ERR_PTR(-ENOENT);
13341 +       AuDebugOn(au_test_anon(path.dentry));
13342 +       if (unlikely(d_really_is_negative(path.dentry)))
13343 +               goto out_path;
13344 +
13345 +       if (ino != d_inode(path.dentry)->i_ino)
13346 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13347 +       else
13348 +               dentry = dget(path.dentry);
13349 +
13350 +out_path:
13351 +       path_put(&path);
13352 +out_relock:
13353 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13354 +               if (!IS_ERR(dentry)) {
13355 +                       dput(dentry);
13356 +                       dentry = ERR_PTR(-ESTALE);
13357 +               }
13358 +out_pathname:
13359 +       free_page((unsigned long)pathname);
13360 +out_h_parent:
13361 +       dput(h_parent);
13362 +out:
13363 +       AuTraceErrPtr(dentry);
13364 +       return dentry;
13365 +}
13366 +
13367 +/* ---------------------------------------------------------------------- */
13368 +
13369 +static struct dentry *
13370 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13371 +                 int fh_type)
13372 +{
13373 +       struct dentry *dentry;
13374 +       __u32 *fh = fid->raw;
13375 +       struct au_branch *br;
13376 +       ino_t ino, dir_ino;
13377 +       struct au_nfsd_si_lock nsi_lock = {
13378 +               .force_lock     = 0
13379 +       };
13380 +
13381 +       dentry = ERR_PTR(-ESTALE);
13382 +       /* it should never happen, but the file handle is unreliable */
13383 +       if (unlikely(fh_len < Fh_tail))
13384 +               goto out;
13385 +       nsi_lock.sigen = fh[Fh_sigen];
13386 +       nsi_lock.br_id = fh[Fh_br_id];
13387 +
13388 +       /* branch id may be wrapped around */
13389 +       br = NULL;
13390 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13391 +               goto out;
13392 +       nsi_lock.force_lock = 1;
13393 +
13394 +       /* is this inode still cached? */
13395 +       ino = decode_ino(fh + Fh_ino);
13396 +       /* it should never happen */
13397 +       if (unlikely(ino == AUFS_ROOT_INO))
13398 +               goto out_unlock;
13399 +
13400 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13401 +       dentry = decode_by_ino(sb, ino, dir_ino);
13402 +       if (IS_ERR(dentry))
13403 +               goto out_unlock;
13404 +       if (dentry)
13405 +               goto accept;
13406 +
13407 +       /* is the parent dir cached? */
13408 +       br = au_sbr(sb, nsi_lock.bindex);
13409 +       au_lcnt_inc(&br->br_nfiles);
13410 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13411 +       if (IS_ERR(dentry))
13412 +               goto out_unlock;
13413 +       if (dentry)
13414 +               goto accept;
13415 +
13416 +       /* lookup path */
13417 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13418 +       if (IS_ERR(dentry))
13419 +               goto out_unlock;
13420 +       if (unlikely(!dentry))
13421 +               /* todo?: make it ESTALE */
13422 +               goto out_unlock;
13423 +
13424 +accept:
13425 +       if (!au_digen_test(dentry, au_sigen(sb))
13426 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13427 +               goto out_unlock; /* success */
13428 +
13429 +       dput(dentry);
13430 +       dentry = ERR_PTR(-ESTALE);
13431 +out_unlock:
13432 +       if (br)
13433 +               au_lcnt_dec(&br->br_nfiles);
13434 +       si_read_unlock(sb);
13435 +out:
13436 +       AuTraceErrPtr(dentry);
13437 +       return dentry;
13438 +}
13439 +
13440 +#if 0 /* reserved for future use */
13441 +/* support subtreecheck option */
13442 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13443 +                                       int fh_len, int fh_type)
13444 +{
13445 +       struct dentry *parent;
13446 +       __u32 *fh = fid->raw;
13447 +       ino_t dir_ino;
13448 +
13449 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13450 +       parent = decode_by_ino(sb, dir_ino, 0);
13451 +       if (IS_ERR(parent))
13452 +               goto out;
13453 +       if (!parent)
13454 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13455 +                                       dir_ino, fh, fh_len);
13456 +
13457 +out:
13458 +       AuTraceErrPtr(parent);
13459 +       return parent;
13460 +}
13461 +#endif
13462 +
13463 +/* ---------------------------------------------------------------------- */
13464 +
13465 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13466 +                         struct inode *dir)
13467 +{
13468 +       int err;
13469 +       aufs_bindex_t bindex;
13470 +       struct super_block *sb, *h_sb;
13471 +       struct dentry *dentry, *parent, *h_parent;
13472 +       struct inode *h_dir;
13473 +       struct au_branch *br;
13474 +
13475 +       err = -ENOSPC;
13476 +       if (unlikely(*max_len <= Fh_tail)) {
13477 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13478 +               goto out;
13479 +       }
13480 +
13481 +       err = FILEID_ROOT;
13482 +       if (inode->i_ino == AUFS_ROOT_INO) {
13483 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13484 +               goto out;
13485 +       }
13486 +
13487 +       h_parent = NULL;
13488 +       sb = inode->i_sb;
13489 +       err = si_read_lock(sb, AuLock_FLUSH);
13490 +       if (unlikely(err))
13491 +               goto out;
13492 +
13493 +#ifdef CONFIG_AUFS_DEBUG
13494 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13495 +               AuWarn1("NFS-exporting requires xino\n");
13496 +#endif
13497 +       err = -EIO;
13498 +       parent = NULL;
13499 +       ii_read_lock_child(inode);
13500 +       bindex = au_ibtop(inode);
13501 +       if (!dir) {
13502 +               dentry = d_find_any_alias(inode);
13503 +               if (unlikely(!dentry))
13504 +                       goto out_unlock;
13505 +               AuDebugOn(au_test_anon(dentry));
13506 +               parent = dget_parent(dentry);
13507 +               dput(dentry);
13508 +               if (unlikely(!parent))
13509 +                       goto out_unlock;
13510 +               if (d_really_is_positive(parent))
13511 +                       dir = d_inode(parent);
13512 +       }
13513 +
13514 +       ii_read_lock_parent(dir);
13515 +       h_dir = au_h_iptr(dir, bindex);
13516 +       ii_read_unlock(dir);
13517 +       if (unlikely(!h_dir))
13518 +               goto out_parent;
13519 +       h_parent = d_find_any_alias(h_dir);
13520 +       if (unlikely(!h_parent))
13521 +               goto out_hparent;
13522 +
13523 +       err = -EPERM;
13524 +       br = au_sbr(sb, bindex);
13525 +       h_sb = au_br_sb(br);
13526 +       if (unlikely(!h_sb->s_export_op)) {
13527 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13528 +               goto out_hparent;
13529 +       }
13530 +
13531 +       fh[Fh_br_id] = br->br_id;
13532 +       fh[Fh_sigen] = au_sigen(sb);
13533 +       encode_ino(fh + Fh_ino, inode->i_ino);
13534 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13535 +       fh[Fh_igen] = inode->i_generation;
13536 +
13537 +       *max_len -= Fh_tail;
13538 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13539 +                                          max_len,
13540 +                                          /*connectable or subtreecheck*/0);
13541 +       err = fh[Fh_h_type];
13542 +       *max_len += Fh_tail;
13543 +       /* todo: macros? */
13544 +       if (err != FILEID_INVALID)
13545 +               err = 99;
13546 +       else
13547 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13548 +
13549 +out_hparent:
13550 +       dput(h_parent);
13551 +out_parent:
13552 +       dput(parent);
13553 +out_unlock:
13554 +       ii_read_unlock(inode);
13555 +       si_read_unlock(sb);
13556 +out:
13557 +       if (unlikely(err < 0))
13558 +               err = FILEID_INVALID;
13559 +       return err;
13560 +}
13561 +
13562 +/* ---------------------------------------------------------------------- */
13563 +
13564 +static int aufs_commit_metadata(struct inode *inode)
13565 +{
13566 +       int err;
13567 +       aufs_bindex_t bindex;
13568 +       struct super_block *sb;
13569 +       struct inode *h_inode;
13570 +       int (*f)(struct inode *inode);
13571 +
13572 +       sb = inode->i_sb;
13573 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13574 +       ii_write_lock_child(inode);
13575 +       bindex = au_ibtop(inode);
13576 +       AuDebugOn(bindex < 0);
13577 +       h_inode = au_h_iptr(inode, bindex);
13578 +
13579 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13580 +       if (f)
13581 +               err = f(h_inode);
13582 +       else
13583 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13584 +
13585 +       au_cpup_attr_timesizes(inode);
13586 +       ii_write_unlock(inode);
13587 +       si_read_unlock(sb);
13588 +       return err;
13589 +}
13590 +
13591 +/* ---------------------------------------------------------------------- */
13592 +
13593 +static struct export_operations aufs_export_op = {
13594 +       .fh_to_dentry           = aufs_fh_to_dentry,
13595 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13596 +       .encode_fh              = aufs_encode_fh,
13597 +       .commit_metadata        = aufs_commit_metadata
13598 +};
13599 +
13600 +void au_export_init(struct super_block *sb)
13601 +{
13602 +       struct au_sbinfo *sbinfo;
13603 +       __u32 u;
13604 +
13605 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13606 +                        && IS_MODULE(CONFIG_EXPORTFS),
13607 +                        AUFS_NAME ": unsupported configuration "
13608 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13609 +
13610 +       sb->s_export_op = &aufs_export_op;
13611 +       sbinfo = au_sbi(sb);
13612 +       sbinfo->si_xigen = NULL;
13613 +       get_random_bytes(&u, sizeof(u));
13614 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13615 +       atomic_set(&sbinfo->si_xigen_next, u);
13616 +}
13617 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13618 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13619 +++ linux/fs/aufs/fhsm.c        2021-11-01 23:48:34.206359262 +0100
13620 @@ -0,0 +1,427 @@
13621 +// SPDX-License-Identifier: GPL-2.0
13622 +/*
13623 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13624 + *
13625 + * This program, aufs is free software; you can redistribute it and/or modify
13626 + * it under the terms of the GNU General Public License as published by
13627 + * the Free Software Foundation; either version 2 of the License, or
13628 + * (at your option) any later version.
13629 + *
13630 + * This program is distributed in the hope that it will be useful,
13631 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13632 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13633 + * GNU General Public License for more details.
13634 + *
13635 + * You should have received a copy of the GNU General Public License
13636 + * along with this program; if not, write to the Free Software
13637 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13638 + */
13639 +
13640 +/*
13641 + * File-based Hierarchy Storage Management
13642 + */
13643 +
13644 +#include <linux/anon_inodes.h>
13645 +#include <linux/poll.h>
13646 +#include <linux/seq_file.h>
13647 +#include <linux/statfs.h>
13648 +#include "aufs.h"
13649 +
13650 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13651 +{
13652 +       struct au_sbinfo *sbinfo;
13653 +       struct au_fhsm *fhsm;
13654 +
13655 +       SiMustAnyLock(sb);
13656 +
13657 +       sbinfo = au_sbi(sb);
13658 +       fhsm = &sbinfo->si_fhsm;
13659 +       AuDebugOn(!fhsm);
13660 +       return fhsm->fhsm_bottom;
13661 +}
13662 +
13663 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13664 +{
13665 +       struct au_sbinfo *sbinfo;
13666 +       struct au_fhsm *fhsm;
13667 +
13668 +       SiMustWriteLock(sb);
13669 +
13670 +       sbinfo = au_sbi(sb);
13671 +       fhsm = &sbinfo->si_fhsm;
13672 +       AuDebugOn(!fhsm);
13673 +       fhsm->fhsm_bottom = bindex;
13674 +}
13675 +
13676 +/* ---------------------------------------------------------------------- */
13677 +
13678 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13679 +{
13680 +       struct au_br_fhsm *bf;
13681 +
13682 +       bf = br->br_fhsm;
13683 +       MtxMustLock(&bf->bf_lock);
13684 +
13685 +       return !bf->bf_readable
13686 +               || time_after(jiffies,
13687 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13688 +}
13689 +
13690 +/* ---------------------------------------------------------------------- */
13691 +
13692 +static void au_fhsm_notify(struct super_block *sb, int val)
13693 +{
13694 +       struct au_sbinfo *sbinfo;
13695 +       struct au_fhsm *fhsm;
13696 +
13697 +       SiMustAnyLock(sb);
13698 +
13699 +       sbinfo = au_sbi(sb);
13700 +       fhsm = &sbinfo->si_fhsm;
13701 +       if (au_fhsm_pid(fhsm)
13702 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13703 +               atomic_set(&fhsm->fhsm_readable, val);
13704 +               if (val)
13705 +                       wake_up(&fhsm->fhsm_wqh);
13706 +       }
13707 +}
13708 +
13709 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13710 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13711 +{
13712 +       int err;
13713 +       struct au_branch *br;
13714 +       struct au_br_fhsm *bf;
13715 +
13716 +       br = au_sbr(sb, bindex);
13717 +       AuDebugOn(au_br_rdonly(br));
13718 +       bf = br->br_fhsm;
13719 +       AuDebugOn(!bf);
13720 +
13721 +       if (do_lock)
13722 +               mutex_lock(&bf->bf_lock);
13723 +       else
13724 +               MtxMustLock(&bf->bf_lock);
13725 +
13726 +       /* sb->s_root for NFS is unreliable */
13727 +       err = au_br_stfs(br, &bf->bf_stfs);
13728 +       if (unlikely(err)) {
13729 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13730 +               goto out;
13731 +       }
13732 +
13733 +       bf->bf_jiffy = jiffies;
13734 +       bf->bf_readable = 1;
13735 +       if (do_notify)
13736 +               au_fhsm_notify(sb, /*val*/1);
13737 +       if (rstfs)
13738 +               *rstfs = bf->bf_stfs;
13739 +
13740 +out:
13741 +       if (do_lock)
13742 +               mutex_unlock(&bf->bf_lock);
13743 +       au_fhsm_notify(sb, /*val*/1);
13744 +
13745 +       return err;
13746 +}
13747 +
13748 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13749 +{
13750 +       int err;
13751 +       struct au_sbinfo *sbinfo;
13752 +       struct au_fhsm *fhsm;
13753 +       struct au_branch *br;
13754 +       struct au_br_fhsm *bf;
13755 +
13756 +       AuDbg("b%d, force %d\n", bindex, force);
13757 +       SiMustAnyLock(sb);
13758 +
13759 +       sbinfo = au_sbi(sb);
13760 +       fhsm = &sbinfo->si_fhsm;
13761 +       if (!au_ftest_si(sbinfo, FHSM)
13762 +           || fhsm->fhsm_bottom == bindex)
13763 +               return;
13764 +
13765 +       br = au_sbr(sb, bindex);
13766 +       bf = br->br_fhsm;
13767 +       AuDebugOn(!bf);
13768 +       mutex_lock(&bf->bf_lock);
13769 +       if (force
13770 +           || au_fhsm_pid(fhsm)
13771 +           || au_fhsm_test_jiffy(sbinfo, br))
13772 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13773 +                                 /*do_notify*/1);
13774 +       mutex_unlock(&bf->bf_lock);
13775 +}
13776 +
13777 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13778 +{
13779 +       aufs_bindex_t bindex, bbot;
13780 +       struct au_branch *br;
13781 +
13782 +       /* exclude the bottom */
13783 +       bbot = au_fhsm_bottom(sb);
13784 +       for (bindex = 0; bindex < bbot; bindex++) {
13785 +               br = au_sbr(sb, bindex);
13786 +               if (au_br_fhsm(br->br_perm))
13787 +                       au_fhsm_wrote(sb, bindex, force);
13788 +       }
13789 +}
13790 +
13791 +/* ---------------------------------------------------------------------- */
13792 +
13793 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13794 +{
13795 +       __poll_t mask;
13796 +       struct au_sbinfo *sbinfo;
13797 +       struct au_fhsm *fhsm;
13798 +
13799 +       mask = 0;
13800 +       sbinfo = file->private_data;
13801 +       fhsm = &sbinfo->si_fhsm;
13802 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13803 +       if (atomic_read(&fhsm->fhsm_readable))
13804 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13805 +
13806 +       if (!mask)
13807 +               AuDbg("mask 0x%x\n", mask);
13808 +       return mask;
13809 +}
13810 +
13811 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13812 +                             struct aufs_stfs *stfs, __s16 brid)
13813 +{
13814 +       int err;
13815 +
13816 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13817 +       if (!err)
13818 +               err = __put_user(brid, &stbr->brid);
13819 +       if (unlikely(err))
13820 +               err = -EFAULT;
13821 +
13822 +       return err;
13823 +}
13824 +
13825 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13826 +                              struct aufs_stbr __user *stbr, size_t count)
13827 +{
13828 +       ssize_t err;
13829 +       int nstbr;
13830 +       aufs_bindex_t bindex, bbot;
13831 +       struct au_branch *br;
13832 +       struct au_br_fhsm *bf;
13833 +
13834 +       /* except the bottom branch */
13835 +       err = 0;
13836 +       nstbr = 0;
13837 +       bbot = au_fhsm_bottom(sb);
13838 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13839 +               br = au_sbr(sb, bindex);
13840 +               if (!au_br_fhsm(br->br_perm))
13841 +                       continue;
13842 +
13843 +               bf = br->br_fhsm;
13844 +               mutex_lock(&bf->bf_lock);
13845 +               if (bf->bf_readable) {
13846 +                       err = -EFAULT;
13847 +                       if (count >= sizeof(*stbr))
13848 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13849 +                                                         br->br_id);
13850 +                       if (!err) {
13851 +                               bf->bf_readable = 0;
13852 +                               count -= sizeof(*stbr);
13853 +                               nstbr++;
13854 +                       }
13855 +               }
13856 +               mutex_unlock(&bf->bf_lock);
13857 +       }
13858 +       if (!err)
13859 +               err = sizeof(*stbr) * nstbr;
13860 +
13861 +       return err;
13862 +}
13863 +
13864 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13865 +                          loff_t *pos)
13866 +{
13867 +       ssize_t err;
13868 +       int readable;
13869 +       aufs_bindex_t nfhsm, bindex, bbot;
13870 +       struct au_sbinfo *sbinfo;
13871 +       struct au_fhsm *fhsm;
13872 +       struct au_branch *br;
13873 +       struct super_block *sb;
13874 +
13875 +       err = 0;
13876 +       sbinfo = file->private_data;
13877 +       fhsm = &sbinfo->si_fhsm;
13878 +need_data:
13879 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13880 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13881 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13882 +                       err = -EAGAIN;
13883 +               else
13884 +                       err = wait_event_interruptible_locked_irq
13885 +                               (fhsm->fhsm_wqh,
13886 +                                atomic_read(&fhsm->fhsm_readable));
13887 +       }
13888 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13889 +       if (unlikely(err))
13890 +               goto out;
13891 +
13892 +       /* sb may already be dead */
13893 +       au_rw_read_lock(&sbinfo->si_rwsem);
13894 +       readable = atomic_read(&fhsm->fhsm_readable);
13895 +       if (readable > 0) {
13896 +               sb = sbinfo->si_sb;
13897 +               AuDebugOn(!sb);
13898 +               /* exclude the bottom branch */
13899 +               nfhsm = 0;
13900 +               bbot = au_fhsm_bottom(sb);
13901 +               for (bindex = 0; bindex < bbot; bindex++) {
13902 +                       br = au_sbr(sb, bindex);
13903 +                       if (au_br_fhsm(br->br_perm))
13904 +                               nfhsm++;
13905 +               }
13906 +               err = -EMSGSIZE;
13907 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13908 +                       atomic_set(&fhsm->fhsm_readable, 0);
13909 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13910 +                                            count);
13911 +               }
13912 +       }
13913 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13914 +       if (!readable)
13915 +               goto need_data;
13916 +
13917 +out:
13918 +       return err;
13919 +}
13920 +
13921 +static int au_fhsm_release(struct inode *inode, struct file *file)
13922 +{
13923 +       struct au_sbinfo *sbinfo;
13924 +       struct au_fhsm *fhsm;
13925 +
13926 +       /* sb may already be dead */
13927 +       sbinfo = file->private_data;
13928 +       fhsm = &sbinfo->si_fhsm;
13929 +       spin_lock(&fhsm->fhsm_spin);
13930 +       fhsm->fhsm_pid = 0;
13931 +       spin_unlock(&fhsm->fhsm_spin);
13932 +       kobject_put(&sbinfo->si_kobj);
13933 +
13934 +       return 0;
13935 +}
13936 +
13937 +static const struct file_operations au_fhsm_fops = {
13938 +       .owner          = THIS_MODULE,
13939 +       .llseek         = noop_llseek,
13940 +       .read           = au_fhsm_read,
13941 +       .poll           = au_fhsm_poll,
13942 +       .release        = au_fhsm_release
13943 +};
13944 +
13945 +int au_fhsm_fd(struct super_block *sb, int oflags)
13946 +{
13947 +       int err, fd;
13948 +       struct au_sbinfo *sbinfo;
13949 +       struct au_fhsm *fhsm;
13950 +
13951 +       err = -EPERM;
13952 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13953 +               goto out;
13954 +
13955 +       err = -EINVAL;
13956 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13957 +               goto out;
13958 +
13959 +       err = 0;
13960 +       sbinfo = au_sbi(sb);
13961 +       fhsm = &sbinfo->si_fhsm;
13962 +       spin_lock(&fhsm->fhsm_spin);
13963 +       if (!fhsm->fhsm_pid)
13964 +               fhsm->fhsm_pid = current->pid;
13965 +       else
13966 +               err = -EBUSY;
13967 +       spin_unlock(&fhsm->fhsm_spin);
13968 +       if (unlikely(err))
13969 +               goto out;
13970 +
13971 +       oflags |= O_RDONLY;
13972 +       /* oflags |= FMODE_NONOTIFY; */
13973 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13974 +       err = fd;
13975 +       if (unlikely(fd < 0))
13976 +               goto out_pid;
13977 +
13978 +       /* succeed regardless 'fhsm' status */
13979 +       kobject_get(&sbinfo->si_kobj);
13980 +       si_noflush_read_lock(sb);
13981 +       if (au_ftest_si(sbinfo, FHSM))
13982 +               au_fhsm_wrote_all(sb, /*force*/0);
13983 +       si_read_unlock(sb);
13984 +       goto out; /* success */
13985 +
13986 +out_pid:
13987 +       spin_lock(&fhsm->fhsm_spin);
13988 +       fhsm->fhsm_pid = 0;
13989 +       spin_unlock(&fhsm->fhsm_spin);
13990 +out:
13991 +       AuTraceErr(err);
13992 +       return err;
13993 +}
13994 +
13995 +/* ---------------------------------------------------------------------- */
13996 +
13997 +int au_fhsm_br_alloc(struct au_branch *br)
13998 +{
13999 +       int err;
14000 +
14001 +       err = 0;
14002 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14003 +       if (br->br_fhsm)
14004 +               au_br_fhsm_init(br->br_fhsm);
14005 +       else
14006 +               err = -ENOMEM;
14007 +
14008 +       return err;
14009 +}
14010 +
14011 +/* ---------------------------------------------------------------------- */
14012 +
14013 +void au_fhsm_fin(struct super_block *sb)
14014 +{
14015 +       au_fhsm_notify(sb, /*val*/-1);
14016 +}
14017 +
14018 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14019 +{
14020 +       struct au_fhsm *fhsm;
14021 +
14022 +       fhsm = &sbinfo->si_fhsm;
14023 +       spin_lock_init(&fhsm->fhsm_spin);
14024 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14025 +       atomic_set(&fhsm->fhsm_readable, 0);
14026 +       fhsm->fhsm_expire
14027 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14028 +       fhsm->fhsm_bottom = -1;
14029 +}
14030 +
14031 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14032 +{
14033 +       sbinfo->si_fhsm.fhsm_expire
14034 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14035 +}
14036 +
14037 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14038 +{
14039 +       unsigned int u;
14040 +
14041 +       if (!au_ftest_si(sbinfo, FHSM))
14042 +               return;
14043 +
14044 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14045 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14046 +               seq_printf(seq, ",fhsm_sec=%u", u);
14047 +}
14048 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14049 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14050 +++ linux/fs/aufs/file.c        2021-11-01 23:48:34.206359262 +0100
14051 @@ -0,0 +1,863 @@
14052 +// SPDX-License-Identifier: GPL-2.0
14053 +/*
14054 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14055 + *
14056 + * This program, aufs is free software; you can redistribute it and/or modify
14057 + * it under the terms of the GNU General Public License as published by
14058 + * the Free Software Foundation; either version 2 of the License, or
14059 + * (at your option) any later version.
14060 + *
14061 + * This program is distributed in the hope that it will be useful,
14062 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14063 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14064 + * GNU General Public License for more details.
14065 + *
14066 + * You should have received a copy of the GNU General Public License
14067 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14068 + */
14069 +
14070 +/*
14071 + * handling file/dir, and address_space operation
14072 + */
14073 +
14074 +#ifdef CONFIG_AUFS_DEBUG
14075 +#include <linux/migrate.h>
14076 +#endif
14077 +#include <linux/pagemap.h>
14078 +#include "aufs.h"
14079 +
14080 +/* drop flags for writing */
14081 +unsigned int au_file_roflags(unsigned int flags)
14082 +{
14083 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14084 +       flags |= O_RDONLY | O_NOATIME;
14085 +       return flags;
14086 +}
14087 +
14088 +/* common functions to regular file and dir */
14089 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14090 +                      struct file *file, int force_wr)
14091 +{
14092 +       struct file *h_file;
14093 +       struct dentry *h_dentry;
14094 +       struct inode *h_inode;
14095 +       struct super_block *sb;
14096 +       struct au_branch *br;
14097 +       struct path h_path;
14098 +       int err;
14099 +
14100 +       /* a race condition can happen between open and unlink/rmdir */
14101 +       h_file = ERR_PTR(-ENOENT);
14102 +       h_dentry = au_h_dptr(dentry, bindex);
14103 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14104 +               goto out;
14105 +       h_inode = d_inode(h_dentry);
14106 +       spin_lock(&h_dentry->d_lock);
14107 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14108 +               /* || !d_inode(dentry)->i_nlink */
14109 +               ;
14110 +       spin_unlock(&h_dentry->d_lock);
14111 +       if (unlikely(err))
14112 +               goto out;
14113 +
14114 +       sb = dentry->d_sb;
14115 +       br = au_sbr(sb, bindex);
14116 +       err = au_br_test_oflag(flags, br);
14117 +       h_file = ERR_PTR(err);
14118 +       if (unlikely(err))
14119 +               goto out;
14120 +
14121 +       /* drop flags for writing */
14122 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14123 +               if (force_wr && !(flags & O_WRONLY))
14124 +                       force_wr = 0;
14125 +               flags = au_file_roflags(flags);
14126 +               if (force_wr) {
14127 +                       h_file = ERR_PTR(-EROFS);
14128 +                       flags = au_file_roflags(flags);
14129 +                       if (unlikely(vfsub_native_ro(h_inode)
14130 +                                    || IS_APPEND(h_inode)))
14131 +                               goto out;
14132 +                       flags &= ~O_ACCMODE;
14133 +                       flags |= O_WRONLY;
14134 +               }
14135 +       }
14136 +       flags &= ~O_CREAT;
14137 +       au_lcnt_inc(&br->br_nfiles);
14138 +       h_path.dentry = h_dentry;
14139 +       h_path.mnt = au_br_mnt(br);
14140 +       h_file = vfsub_dentry_open(&h_path, flags);
14141 +       if (IS_ERR(h_file))
14142 +               goto out_br;
14143 +
14144 +       if (flags & __FMODE_EXEC) {
14145 +               err = deny_write_access(h_file);
14146 +               if (unlikely(err)) {
14147 +                       fput(h_file);
14148 +                       h_file = ERR_PTR(err);
14149 +                       goto out_br;
14150 +               }
14151 +       }
14152 +       fsnotify_open(h_file);
14153 +       goto out; /* success */
14154 +
14155 +out_br:
14156 +       au_lcnt_dec(&br->br_nfiles);
14157 +out:
14158 +       return h_file;
14159 +}
14160 +
14161 +static int au_cmoo(struct dentry *dentry)
14162 +{
14163 +       int err, cmoo, matched;
14164 +       unsigned int udba;
14165 +       struct path h_path;
14166 +       struct au_pin pin;
14167 +       struct au_cp_generic cpg = {
14168 +               .dentry = dentry,
14169 +               .bdst   = -1,
14170 +               .bsrc   = -1,
14171 +               .len    = -1,
14172 +               .pin    = &pin,
14173 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14174 +       };
14175 +       struct inode *delegated;
14176 +       struct super_block *sb;
14177 +       struct au_sbinfo *sbinfo;
14178 +       struct au_fhsm *fhsm;
14179 +       pid_t pid;
14180 +       struct au_branch *br;
14181 +       struct dentry *parent;
14182 +       struct au_hinode *hdir;
14183 +
14184 +       DiMustWriteLock(dentry);
14185 +       IiMustWriteLock(d_inode(dentry));
14186 +
14187 +       err = 0;
14188 +       if (IS_ROOT(dentry))
14189 +               goto out;
14190 +       cpg.bsrc = au_dbtop(dentry);
14191 +       if (!cpg.bsrc)
14192 +               goto out;
14193 +
14194 +       sb = dentry->d_sb;
14195 +       sbinfo = au_sbi(sb);
14196 +       fhsm = &sbinfo->si_fhsm;
14197 +       pid = au_fhsm_pid(fhsm);
14198 +       rcu_read_lock();
14199 +       matched = (pid
14200 +                  && (current->pid == pid
14201 +                      || rcu_dereference(current->real_parent)->pid == pid));
14202 +       rcu_read_unlock();
14203 +       if (matched)
14204 +               goto out;
14205 +
14206 +       br = au_sbr(sb, cpg.bsrc);
14207 +       cmoo = au_br_cmoo(br->br_perm);
14208 +       if (!cmoo)
14209 +               goto out;
14210 +       if (!d_is_reg(dentry))
14211 +               cmoo &= AuBrAttr_COO_ALL;
14212 +       if (!cmoo)
14213 +               goto out;
14214 +
14215 +       parent = dget_parent(dentry);
14216 +       di_write_lock_parent(parent);
14217 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14218 +       cpg.bdst = err;
14219 +       if (unlikely(err < 0)) {
14220 +               err = 0;        /* there is no upper writable branch */
14221 +               goto out_dgrade;
14222 +       }
14223 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14224 +
14225 +       /* do not respect the coo attrib for the target branch */
14226 +       err = au_cpup_dirs(dentry, cpg.bdst);
14227 +       if (unlikely(err))
14228 +               goto out_dgrade;
14229 +
14230 +       di_downgrade_lock(parent, AuLock_IR);
14231 +       udba = au_opt_udba(sb);
14232 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14233 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14234 +       if (unlikely(err))
14235 +               goto out_parent;
14236 +
14237 +       err = au_sio_cpup_simple(&cpg);
14238 +       au_unpin(&pin);
14239 +       if (unlikely(err))
14240 +               goto out_parent;
14241 +       if (!(cmoo & AuBrWAttr_MOO))
14242 +               goto out_parent; /* success */
14243 +
14244 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14245 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14246 +       if (unlikely(err))
14247 +               goto out_parent;
14248 +
14249 +       h_path.mnt = au_br_mnt(br);
14250 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14251 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14252 +       delegated = NULL;
14253 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14254 +       au_unpin(&pin);
14255 +       /* todo: keep h_dentry or not? */
14256 +       if (unlikely(err == -EWOULDBLOCK)) {
14257 +               pr_warn("cannot retry for NFSv4 delegation"
14258 +                       " for an internal unlink\n");
14259 +               iput(delegated);
14260 +       }
14261 +       if (unlikely(err)) {
14262 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14263 +                      dentry, err);
14264 +               err = 0;
14265 +       }
14266 +       goto out_parent; /* success */
14267 +
14268 +out_dgrade:
14269 +       di_downgrade_lock(parent, AuLock_IR);
14270 +out_parent:
14271 +       di_read_unlock(parent, AuLock_IR);
14272 +       dput(parent);
14273 +out:
14274 +       AuTraceErr(err);
14275 +       return err;
14276 +}
14277 +
14278 +int au_do_open(struct file *file, struct au_do_open_args *args)
14279 +{
14280 +       int err, aopen = args->aopen;
14281 +       struct dentry *dentry;
14282 +       struct au_finfo *finfo;
14283 +
14284 +       if (!aopen)
14285 +               err = au_finfo_init(file, args->fidir);
14286 +       else {
14287 +               lockdep_off();
14288 +               err = au_finfo_init(file, args->fidir);
14289 +               lockdep_on();
14290 +       }
14291 +       if (unlikely(err))
14292 +               goto out;
14293 +
14294 +       dentry = file->f_path.dentry;
14295 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14296 +       di_write_lock_child(dentry);
14297 +       err = au_cmoo(dentry);
14298 +       di_downgrade_lock(dentry, AuLock_IR);
14299 +       if (!err) {
14300 +               if (!aopen)
14301 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14302 +               else {
14303 +                       lockdep_off();
14304 +                       err = args->open(file, vfsub_file_flags(file),
14305 +                                        args->h_file);
14306 +                       lockdep_on();
14307 +               }
14308 +       }
14309 +       di_read_unlock(dentry, AuLock_IR);
14310 +
14311 +       finfo = au_fi(file);
14312 +       if (!err) {
14313 +               finfo->fi_file = file;
14314 +               au_hbl_add(&finfo->fi_hlist,
14315 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14316 +       }
14317 +       if (!aopen)
14318 +               fi_write_unlock(file);
14319 +       else {
14320 +               lockdep_off();
14321 +               fi_write_unlock(file);
14322 +               lockdep_on();
14323 +       }
14324 +       if (unlikely(err)) {
14325 +               finfo->fi_hdir = NULL;
14326 +               au_finfo_fin(file);
14327 +       }
14328 +
14329 +out:
14330 +       AuTraceErr(err);
14331 +       return err;
14332 +}
14333 +
14334 +int au_reopen_nondir(struct file *file)
14335 +{
14336 +       int err;
14337 +       aufs_bindex_t btop;
14338 +       struct dentry *dentry;
14339 +       struct au_branch *br;
14340 +       struct file *h_file, *h_file_tmp;
14341 +
14342 +       dentry = file->f_path.dentry;
14343 +       btop = au_dbtop(dentry);
14344 +       br = au_sbr(dentry->d_sb, btop);
14345 +       h_file_tmp = NULL;
14346 +       if (au_fbtop(file) == btop) {
14347 +               h_file = au_hf_top(file);
14348 +               if (file->f_mode == h_file->f_mode)
14349 +                       return 0; /* success */
14350 +               h_file_tmp = h_file;
14351 +               get_file(h_file_tmp);
14352 +               au_lcnt_inc(&br->br_nfiles);
14353 +               au_set_h_fptr(file, btop, NULL);
14354 +       }
14355 +       AuDebugOn(au_fi(file)->fi_hdir);
14356 +       /*
14357 +        * it can happen
14358 +        * file exists on both of rw and ro
14359 +        * open --> dbtop and fbtop are both 0
14360 +        * prepend a branch as rw, "rw" become ro
14361 +        * remove rw/file
14362 +        * delete the top branch, "rw" becomes rw again
14363 +        *      --> dbtop is 1, fbtop is still 0
14364 +        * write --> fbtop is 0 but dbtop is 1
14365 +        */
14366 +       /* AuDebugOn(au_fbtop(file) < btop); */
14367 +
14368 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14369 +                          file, /*force_wr*/0);
14370 +       err = PTR_ERR(h_file);
14371 +       if (IS_ERR(h_file)) {
14372 +               if (h_file_tmp) {
14373 +                       /* revert */
14374 +                       au_set_h_fptr(file, btop, h_file_tmp);
14375 +                       h_file_tmp = NULL;
14376 +               }
14377 +               goto out; /* todo: close all? */
14378 +       }
14379 +
14380 +       err = 0;
14381 +       au_set_fbtop(file, btop);
14382 +       au_set_h_fptr(file, btop, h_file);
14383 +       au_update_figen(file);
14384 +       /* todo: necessary? */
14385 +       /* file->f_ra = h_file->f_ra; */
14386 +
14387 +out:
14388 +       if (h_file_tmp) {
14389 +               fput(h_file_tmp);
14390 +               au_lcnt_dec(&br->br_nfiles);
14391 +       }
14392 +       return err;
14393 +}
14394 +
14395 +/* ---------------------------------------------------------------------- */
14396 +
14397 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14398 +                       struct dentry *hi_wh)
14399 +{
14400 +       int err;
14401 +       aufs_bindex_t btop;
14402 +       struct au_dinfo *dinfo;
14403 +       struct dentry *h_dentry;
14404 +       struct au_hdentry *hdp;
14405 +
14406 +       dinfo = au_di(file->f_path.dentry);
14407 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14408 +
14409 +       btop = dinfo->di_btop;
14410 +       dinfo->di_btop = btgt;
14411 +       hdp = au_hdentry(dinfo, btgt);
14412 +       h_dentry = hdp->hd_dentry;
14413 +       hdp->hd_dentry = hi_wh;
14414 +       err = au_reopen_nondir(file);
14415 +       hdp->hd_dentry = h_dentry;
14416 +       dinfo->di_btop = btop;
14417 +
14418 +       return err;
14419 +}
14420 +
14421 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14422 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14423 +{
14424 +       int err;
14425 +       struct inode *inode, *h_inode;
14426 +       struct dentry *h_dentry, *hi_wh;
14427 +       struct au_cp_generic cpg = {
14428 +               .dentry = file->f_path.dentry,
14429 +               .bdst   = bcpup,
14430 +               .bsrc   = -1,
14431 +               .len    = len,
14432 +               .pin    = pin
14433 +       };
14434 +
14435 +       au_update_dbtop(cpg.dentry);
14436 +       inode = d_inode(cpg.dentry);
14437 +       h_inode = NULL;
14438 +       if (au_dbtop(cpg.dentry) <= bcpup
14439 +           && au_dbbot(cpg.dentry) >= bcpup) {
14440 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14441 +               if (h_dentry && d_is_positive(h_dentry))
14442 +                       h_inode = d_inode(h_dentry);
14443 +       }
14444 +       hi_wh = au_hi_wh(inode, bcpup);
14445 +       if (!hi_wh && !h_inode)
14446 +               err = au_sio_cpup_wh(&cpg, file);
14447 +       else
14448 +               /* already copied-up after unlink */
14449 +               err = au_reopen_wh(file, bcpup, hi_wh);
14450 +
14451 +       if (!err
14452 +           && (inode->i_nlink > 1
14453 +               || (inode->i_state & I_LINKABLE))
14454 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14455 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14456 +
14457 +       return err;
14458 +}
14459 +
14460 +/*
14461 + * prepare the @file for writing.
14462 + */
14463 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14464 +{
14465 +       int err;
14466 +       aufs_bindex_t dbtop;
14467 +       struct dentry *parent;
14468 +       struct inode *inode;
14469 +       struct super_block *sb;
14470 +       struct file *h_file;
14471 +       struct au_cp_generic cpg = {
14472 +               .dentry = file->f_path.dentry,
14473 +               .bdst   = -1,
14474 +               .bsrc   = -1,
14475 +               .len    = len,
14476 +               .pin    = pin,
14477 +               .flags  = AuCpup_DTIME
14478 +       };
14479 +
14480 +       sb = cpg.dentry->d_sb;
14481 +       inode = d_inode(cpg.dentry);
14482 +       cpg.bsrc = au_fbtop(file);
14483 +       err = au_test_ro(sb, cpg.bsrc, inode);
14484 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14485 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14486 +                            /*flags*/0);
14487 +               goto out;
14488 +       }
14489 +
14490 +       /* need to cpup or reopen */
14491 +       parent = dget_parent(cpg.dentry);
14492 +       di_write_lock_parent(parent);
14493 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14494 +       cpg.bdst = err;
14495 +       if (unlikely(err < 0))
14496 +               goto out_dgrade;
14497 +       err = 0;
14498 +
14499 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14500 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14501 +               if (unlikely(err))
14502 +                       goto out_dgrade;
14503 +       }
14504 +
14505 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14506 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14507 +       if (unlikely(err))
14508 +               goto out_dgrade;
14509 +
14510 +       dbtop = au_dbtop(cpg.dentry);
14511 +       if (dbtop <= cpg.bdst)
14512 +               cpg.bsrc = cpg.bdst;
14513 +
14514 +       if (dbtop <= cpg.bdst           /* just reopen */
14515 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14516 +               ) {
14517 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14518 +               if (IS_ERR(h_file))
14519 +                       err = PTR_ERR(h_file);
14520 +               else {
14521 +                       di_downgrade_lock(parent, AuLock_IR);
14522 +                       if (dbtop > cpg.bdst)
14523 +                               err = au_sio_cpup_simple(&cpg);
14524 +                       if (!err)
14525 +                               err = au_reopen_nondir(file);
14526 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14527 +               }
14528 +       } else {                        /* copyup as wh and reopen */
14529 +               /*
14530 +                * since writable hfsplus branch is not supported,
14531 +                * h_open_pre/post() are unnecessary.
14532 +                */
14533 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14534 +               di_downgrade_lock(parent, AuLock_IR);
14535 +       }
14536 +
14537 +       if (!err) {
14538 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14539 +               goto out_dput; /* success */
14540 +       }
14541 +       au_unpin(pin);
14542 +       goto out_unlock;
14543 +
14544 +out_dgrade:
14545 +       di_downgrade_lock(parent, AuLock_IR);
14546 +out_unlock:
14547 +       di_read_unlock(parent, AuLock_IR);
14548 +out_dput:
14549 +       dput(parent);
14550 +out:
14551 +       return err;
14552 +}
14553 +
14554 +/* ---------------------------------------------------------------------- */
14555 +
14556 +int au_do_flush(struct file *file, fl_owner_t id,
14557 +               int (*flush)(struct file *file, fl_owner_t id))
14558 +{
14559 +       int err;
14560 +       struct super_block *sb;
14561 +       struct inode *inode;
14562 +
14563 +       inode = file_inode(file);
14564 +       sb = inode->i_sb;
14565 +       si_noflush_read_lock(sb);
14566 +       fi_read_lock(file);
14567 +       ii_read_lock_child(inode);
14568 +
14569 +       err = flush(file, id);
14570 +       au_cpup_attr_timesizes(inode);
14571 +
14572 +       ii_read_unlock(inode);
14573 +       fi_read_unlock(file);
14574 +       si_read_unlock(sb);
14575 +       return err;
14576 +}
14577 +
14578 +/* ---------------------------------------------------------------------- */
14579 +
14580 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14581 +{
14582 +       int err;
14583 +       struct au_pin pin;
14584 +       struct au_finfo *finfo;
14585 +       struct dentry *parent, *hi_wh;
14586 +       struct inode *inode;
14587 +       struct super_block *sb;
14588 +       struct au_cp_generic cpg = {
14589 +               .dentry = file->f_path.dentry,
14590 +               .bdst   = -1,
14591 +               .bsrc   = -1,
14592 +               .len    = -1,
14593 +               .pin    = &pin,
14594 +               .flags  = AuCpup_DTIME
14595 +       };
14596 +
14597 +       FiMustWriteLock(file);
14598 +
14599 +       err = 0;
14600 +       finfo = au_fi(file);
14601 +       sb = cpg.dentry->d_sb;
14602 +       inode = d_inode(cpg.dentry);
14603 +       cpg.bdst = au_ibtop(inode);
14604 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14605 +               goto out;
14606 +
14607 +       parent = dget_parent(cpg.dentry);
14608 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14609 +               di_read_lock_parent(parent, !AuLock_IR);
14610 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14611 +               cpg.bdst = err;
14612 +               di_read_unlock(parent, !AuLock_IR);
14613 +               if (unlikely(err < 0))
14614 +                       goto out_parent;
14615 +               err = 0;
14616 +       }
14617 +
14618 +       di_read_lock_parent(parent, AuLock_IR);
14619 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14620 +       if (!S_ISDIR(inode->i_mode)
14621 +           && au_opt_test(au_mntflags(sb), PLINK)
14622 +           && au_plink_test(inode)
14623 +           && !d_unhashed(cpg.dentry)
14624 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14625 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14626 +               if (unlikely(err))
14627 +                       goto out_unlock;
14628 +
14629 +               /* always superio. */
14630 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14631 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14632 +               if (!err) {
14633 +                       err = au_sio_cpup_simple(&cpg);
14634 +                       au_unpin(&pin);
14635 +               }
14636 +       } else if (hi_wh) {
14637 +               /* already copied-up after unlink */
14638 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14639 +               *need_reopen = 0;
14640 +       }
14641 +
14642 +out_unlock:
14643 +       di_read_unlock(parent, AuLock_IR);
14644 +out_parent:
14645 +       dput(parent);
14646 +out:
14647 +       return err;
14648 +}
14649 +
14650 +static void au_do_refresh_dir(struct file *file)
14651 +{
14652 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14653 +       struct au_hfile *p, tmp, *q;
14654 +       struct au_finfo *finfo;
14655 +       struct super_block *sb;
14656 +       struct au_fidir *fidir;
14657 +
14658 +       FiMustWriteLock(file);
14659 +
14660 +       sb = file->f_path.dentry->d_sb;
14661 +       finfo = au_fi(file);
14662 +       fidir = finfo->fi_hdir;
14663 +       AuDebugOn(!fidir);
14664 +       p = fidir->fd_hfile + finfo->fi_btop;
14665 +       brid = p->hf_br->br_id;
14666 +       bbot = fidir->fd_bbot;
14667 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14668 +               if (!p->hf_file)
14669 +                       continue;
14670 +
14671 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14672 +               if (new_bindex == bindex)
14673 +                       continue;
14674 +               if (new_bindex < 0) {
14675 +                       au_set_h_fptr(file, bindex, NULL);
14676 +                       continue;
14677 +               }
14678 +
14679 +               /* swap two lower inode, and loop again */
14680 +               q = fidir->fd_hfile + new_bindex;
14681 +               tmp = *q;
14682 +               *q = *p;
14683 +               *p = tmp;
14684 +               if (tmp.hf_file) {
14685 +                       bindex--;
14686 +                       p--;
14687 +               }
14688 +       }
14689 +
14690 +       p = fidir->fd_hfile;
14691 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14692 +               bbot = au_sbbot(sb);
14693 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14694 +                    finfo->fi_btop++, p++)
14695 +                       if (p->hf_file) {
14696 +                               if (file_inode(p->hf_file))
14697 +                                       break;
14698 +                               au_hfput(p, /*execed*/0);
14699 +                       }
14700 +       } else {
14701 +               bbot = au_br_index(sb, brid);
14702 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14703 +                    finfo->fi_btop++, p++)
14704 +                       if (p->hf_file)
14705 +                               au_hfput(p, /*execed*/0);
14706 +               bbot = au_sbbot(sb);
14707 +       }
14708 +
14709 +       p = fidir->fd_hfile + bbot;
14710 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14711 +            fidir->fd_bbot--, p--)
14712 +               if (p->hf_file) {
14713 +                       if (file_inode(p->hf_file))
14714 +                               break;
14715 +                       au_hfput(p, /*execed*/0);
14716 +               }
14717 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14718 +}
14719 +
14720 +/*
14721 + * after branch manipulating, refresh the file.
14722 + */
14723 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14724 +{
14725 +       int err, need_reopen, nbr;
14726 +       aufs_bindex_t bbot, bindex;
14727 +       struct dentry *dentry;
14728 +       struct super_block *sb;
14729 +       struct au_finfo *finfo;
14730 +       struct au_hfile *hfile;
14731 +
14732 +       dentry = file->f_path.dentry;
14733 +       sb = dentry->d_sb;
14734 +       nbr = au_sbbot(sb) + 1;
14735 +       finfo = au_fi(file);
14736 +       if (!finfo->fi_hdir) {
14737 +               hfile = &finfo->fi_htop;
14738 +               AuDebugOn(!hfile->hf_file);
14739 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14740 +               AuDebugOn(bindex < 0);
14741 +               if (bindex != finfo->fi_btop)
14742 +                       au_set_fbtop(file, bindex);
14743 +       } else {
14744 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14745 +               if (unlikely(err))
14746 +                       goto out;
14747 +               au_do_refresh_dir(file);
14748 +       }
14749 +
14750 +       err = 0;
14751 +       need_reopen = 1;
14752 +       if (!au_test_mmapped(file))
14753 +               err = au_file_refresh_by_inode(file, &need_reopen);
14754 +       if (finfo->fi_hdir)
14755 +               /* harmless if err */
14756 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14757 +       if (!err && need_reopen && !d_unlinked(dentry))
14758 +               err = reopen(file);
14759 +       if (!err) {
14760 +               au_update_figen(file);
14761 +               goto out; /* success */
14762 +       }
14763 +
14764 +       /* error, close all lower files */
14765 +       if (finfo->fi_hdir) {
14766 +               bbot = au_fbbot_dir(file);
14767 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14768 +                       au_set_h_fptr(file, bindex, NULL);
14769 +       }
14770 +
14771 +out:
14772 +       return err;
14773 +}
14774 +
14775 +/* common function to regular file and dir */
14776 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14777 +                         int wlock, unsigned int fi_lsc)
14778 +{
14779 +       int err;
14780 +       unsigned int sigen, figen;
14781 +       aufs_bindex_t btop;
14782 +       unsigned char pseudo_link;
14783 +       struct dentry *dentry;
14784 +       struct inode *inode;
14785 +
14786 +       err = 0;
14787 +       dentry = file->f_path.dentry;
14788 +       inode = d_inode(dentry);
14789 +       sigen = au_sigen(dentry->d_sb);
14790 +       fi_write_lock_nested(file, fi_lsc);
14791 +       figen = au_figen(file);
14792 +       if (!fi_lsc)
14793 +               di_write_lock_child(dentry);
14794 +       else
14795 +               di_write_lock_child2(dentry);
14796 +       btop = au_dbtop(dentry);
14797 +       pseudo_link = (btop != au_ibtop(inode));
14798 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14799 +               if (!wlock) {
14800 +                       di_downgrade_lock(dentry, AuLock_IR);
14801 +                       fi_downgrade_lock(file);
14802 +               }
14803 +               goto out; /* success */
14804 +       }
14805 +
14806 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14807 +       if (au_digen_test(dentry, sigen)) {
14808 +               err = au_reval_dpath(dentry, sigen);
14809 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14810 +       }
14811 +
14812 +       if (!err)
14813 +               err = refresh_file(file, reopen);
14814 +       if (!err) {
14815 +               if (!wlock) {
14816 +                       di_downgrade_lock(dentry, AuLock_IR);
14817 +                       fi_downgrade_lock(file);
14818 +               }
14819 +       } else {
14820 +               di_write_unlock(dentry);
14821 +               fi_write_unlock(file);
14822 +       }
14823 +
14824 +out:
14825 +       return err;
14826 +}
14827 +
14828 +/* ---------------------------------------------------------------------- */
14829 +
14830 +/* cf. aufs_nopage() */
14831 +/* for madvise(2) */
14832 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14833 +{
14834 +       unlock_page(page);
14835 +       return 0;
14836 +}
14837 +
14838 +/* it will never be called, but necessary to support O_DIRECT */
14839 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14840 +{ BUG(); return 0; }
14841 +
14842 +/* they will never be called. */
14843 +#ifdef CONFIG_AUFS_DEBUG
14844 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14845 +                           loff_t pos, unsigned len, unsigned flags,
14846 +                           struct page **pagep, void **fsdata)
14847 +{ AuUnsupport(); return 0; }
14848 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14849 +                         loff_t pos, unsigned len, unsigned copied,
14850 +                         struct page *page, void *fsdata)
14851 +{ AuUnsupport(); return 0; }
14852 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14853 +{ AuUnsupport(); return 0; }
14854 +
14855 +static int aufs_set_page_dirty(struct page *page)
14856 +{ AuUnsupport(); return 0; }
14857 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14858 +                               unsigned int length)
14859 +{ AuUnsupport(); }
14860 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14861 +{ AuUnsupport(); return 0; }
14862 +#if 0 /* called by memory compaction regardless file */
14863 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14864 +                           struct page *page, enum migrate_mode mode)
14865 +{ AuUnsupport(); return 0; }
14866 +#endif
14867 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14868 +{ AuUnsupport(); return true; }
14869 +static void aufs_putback_page(struct page *page)
14870 +{ AuUnsupport(); }
14871 +static int aufs_launder_page(struct page *page)
14872 +{ AuUnsupport(); return 0; }
14873 +static int aufs_is_partially_uptodate(struct page *page,
14874 +                                     unsigned long from,
14875 +                                     unsigned long count)
14876 +{ AuUnsupport(); return 0; }
14877 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14878 +                                   bool *writeback)
14879 +{ AuUnsupport(); }
14880 +static int aufs_error_remove_page(struct address_space *mapping,
14881 +                                 struct page *page)
14882 +{ AuUnsupport(); return 0; }
14883 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14884 +                             sector_t *span)
14885 +{ AuUnsupport(); return 0; }
14886 +static void aufs_swap_deactivate(struct file *file)
14887 +{ AuUnsupport(); }
14888 +#endif /* CONFIG_AUFS_DEBUG */
14889 +
14890 +const struct address_space_operations aufs_aop = {
14891 +       .readpage               = aufs_readpage,
14892 +       .direct_IO              = aufs_direct_IO,
14893 +#ifdef CONFIG_AUFS_DEBUG
14894 +       .writepage              = aufs_writepage,
14895 +       /* no writepages, because of writepage */
14896 +       .set_page_dirty         = aufs_set_page_dirty,
14897 +       /* no readpages, because of readpage */
14898 +       .write_begin            = aufs_write_begin,
14899 +       .write_end              = aufs_write_end,
14900 +       /* no bmap, no block device */
14901 +       .invalidatepage         = aufs_invalidatepage,
14902 +       .releasepage            = aufs_releasepage,
14903 +       /* is fallback_migrate_page ok? */
14904 +       /* .migratepage         = aufs_migratepage, */
14905 +       .isolate_page           = aufs_isolate_page,
14906 +       .putback_page           = aufs_putback_page,
14907 +       .launder_page           = aufs_launder_page,
14908 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14909 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14910 +       .error_remove_page      = aufs_error_remove_page,
14911 +       .swap_activate          = aufs_swap_activate,
14912 +       .swap_deactivate        = aufs_swap_deactivate
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        2021-11-01 23:48:34.206359262 +0100
14918 @@ -0,0 +1,342 @@
14919 +/* SPDX-License-Identifier: GPL-2.0 */
14920 +/*
14921 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14922 + *
14923 + * This program, aufs 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       2021-11-01 23:48:34.206359262 +0100
15264 @@ -0,0 +1,149 @@
15265 +// SPDX-License-Identifier: GPL-2.0
15266 +/*
15267 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15268 + *
15269 + * This program, aufs 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        2021-11-01 23:48:34.206359262 +0100
15417 @@ -0,0 +1,771 @@
15418 +// SPDX-License-Identifier: GPL-2.0
15419 +/*
15420 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15421 + *
15422 + * This program, aufs 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 +           && !(flags & O_EXCL)) {
15483 +               h_inode = file_inode(h_file);
15484 +               spin_lock(&h_inode->i_lock);
15485 +               h_inode->i_state |= I_LINKABLE;
15486 +               spin_unlock(&h_inode->i_lock);
15487 +       }
15488 +       au_set_fbtop(file, bindex);
15489 +       au_set_h_fptr(file, bindex, h_file);
15490 +       au_update_figen(file);
15491 +       /* todo: necessary? */
15492 +       /* file->f_ra = h_file->f_ra; */
15493 +
15494 +out:
15495 +       return err;
15496 +}
15497 +
15498 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15499 +                           struct file *file)
15500 +{
15501 +       int err;
15502 +       struct super_block *sb;
15503 +       struct au_do_open_args args = {
15504 +               .open   = au_do_open_nondir
15505 +       };
15506 +
15507 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15508 +             file, vfsub_file_flags(file), file->f_mode);
15509 +
15510 +       sb = file->f_path.dentry->d_sb;
15511 +       si_read_lock(sb, AuLock_FLUSH);
15512 +       err = au_do_open(file, &args);
15513 +       si_read_unlock(sb);
15514 +       return err;
15515 +}
15516 +
15517 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15518 +{
15519 +       struct au_finfo *finfo;
15520 +       aufs_bindex_t bindex;
15521 +
15522 +       finfo = au_fi(file);
15523 +       au_hbl_del(&finfo->fi_hlist,
15524 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15525 +       bindex = finfo->fi_btop;
15526 +       if (bindex >= 0)
15527 +               au_set_h_fptr(file, bindex, NULL);
15528 +
15529 +       au_finfo_fin(file);
15530 +       return 0;
15531 +}
15532 +
15533 +/* ---------------------------------------------------------------------- */
15534 +
15535 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15536 +{
15537 +       int err;
15538 +       struct file *h_file;
15539 +
15540 +       err = 0;
15541 +       h_file = au_hf_top(file);
15542 +       if (h_file)
15543 +               err = vfsub_flush(h_file, id);
15544 +       return err;
15545 +}
15546 +
15547 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15548 +{
15549 +       return au_do_flush(file, id, au_do_flush_nondir);
15550 +}
15551 +
15552 +/* ---------------------------------------------------------------------- */
15553 +/*
15554 + * read and write functions acquire [fdi]_rwsem once, but release before
15555 + * mmap_sem. This is because to stop a race condition between mmap(2).
15556 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15557 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15558 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15559 + */
15560 +
15561 +/* Callers should call au_read_post() or fput() in the end */
15562 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15563 +{
15564 +       struct file *h_file;
15565 +       int err;
15566 +
15567 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15568 +       if (!err) {
15569 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15570 +               h_file = au_hf_top(file);
15571 +               get_file(h_file);
15572 +               if (!keep_fi)
15573 +                       fi_read_unlock(file);
15574 +       } else
15575 +               h_file = ERR_PTR(err);
15576 +
15577 +       return h_file;
15578 +}
15579 +
15580 +static void au_read_post(struct inode *inode, struct file *h_file)
15581 +{
15582 +       /* update without lock, I don't think it a problem */
15583 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15584 +       fput(h_file);
15585 +}
15586 +
15587 +struct au_write_pre {
15588 +       /* input */
15589 +       unsigned int lsc;
15590 +
15591 +       /* output */
15592 +       blkcnt_t blks;
15593 +       aufs_bindex_t btop;
15594 +};
15595 +
15596 +/*
15597 + * return with iinfo is write-locked
15598 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15599 + * end
15600 + */
15601 +static struct file *au_write_pre(struct file *file, int do_ready,
15602 +                                struct au_write_pre *wpre)
15603 +{
15604 +       struct file *h_file;
15605 +       struct dentry *dentry;
15606 +       int err;
15607 +       unsigned int lsc;
15608 +       struct au_pin pin;
15609 +
15610 +       lsc = 0;
15611 +       if (wpre)
15612 +               lsc = wpre->lsc;
15613 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15614 +       h_file = ERR_PTR(err);
15615 +       if (unlikely(err))
15616 +               goto out;
15617 +
15618 +       dentry = file->f_path.dentry;
15619 +       if (do_ready) {
15620 +               err = au_ready_to_write(file, -1, &pin);
15621 +               if (unlikely(err)) {
15622 +                       h_file = ERR_PTR(err);
15623 +                       di_write_unlock(dentry);
15624 +                       goto out_fi;
15625 +               }
15626 +       }
15627 +
15628 +       di_downgrade_lock(dentry, /*flags*/0);
15629 +       if (wpre)
15630 +               wpre->btop = au_fbtop(file);
15631 +       h_file = au_hf_top(file);
15632 +       get_file(h_file);
15633 +       if (wpre)
15634 +               wpre->blks = file_inode(h_file)->i_blocks;
15635 +       if (do_ready)
15636 +               au_unpin(&pin);
15637 +       di_read_unlock(dentry, /*flags*/0);
15638 +
15639 +out_fi:
15640 +       fi_write_unlock(file);
15641 +out:
15642 +       return h_file;
15643 +}
15644 +
15645 +static void au_write_post(struct inode *inode, struct file *h_file,
15646 +                         struct au_write_pre *wpre, ssize_t written)
15647 +{
15648 +       struct inode *h_inode;
15649 +
15650 +       au_cpup_attr_timesizes(inode);
15651 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15652 +       h_inode = file_inode(h_file);
15653 +       inode->i_mode = h_inode->i_mode;
15654 +       ii_write_unlock(inode);
15655 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15656 +       if (written > 0)
15657 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15658 +                             /*force*/h_inode->i_blocks > wpre->blks);
15659 +       fput(h_file);
15660 +}
15661 +
15662 +/*
15663 + * todo: very ugly
15664 + * it locks both of i_mutex and si_rwsem for read in safe.
15665 + * if the plink maintenance mode continues forever (that is the problem),
15666 + * may loop forever.
15667 + */
15668 +static void au_mtx_and_read_lock(struct inode *inode)
15669 +{
15670 +       int err;
15671 +       struct super_block *sb = inode->i_sb;
15672 +
15673 +       while (1) {
15674 +               inode_lock(inode);
15675 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15676 +               if (!err)
15677 +                       break;
15678 +               inode_unlock(inode);
15679 +               si_read_lock(sb, AuLock_NOPLMW);
15680 +               si_read_unlock(sb);
15681 +       }
15682 +}
15683 +
15684 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15685 +                         struct iov_iter *iov_iter)
15686 +{
15687 +       ssize_t err;
15688 +       struct file *file;
15689 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15690 +
15691 +       err = security_file_permission(h_file, rw);
15692 +       if (unlikely(err))
15693 +               goto out;
15694 +
15695 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15696 +       iter = NULL;
15697 +       if (rw == MAY_READ)
15698 +               iter = h_file->f_op->read_iter;
15699 +       else if (rw == MAY_WRITE)
15700 +               iter = h_file->f_op->write_iter;
15701 +
15702 +       file = kio->ki_filp;
15703 +       kio->ki_filp = h_file;
15704 +       if (iter) {
15705 +               lockdep_off();
15706 +               err = iter(kio, iov_iter);
15707 +               lockdep_on();
15708 +       } else
15709 +               /* currently there is no such fs */
15710 +               WARN_ON_ONCE(1);
15711 +       kio->ki_filp = file;
15712 +
15713 +out:
15714 +       return err;
15715 +}
15716 +
15717 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15718 +{
15719 +       ssize_t err;
15720 +       struct file *file, *h_file;
15721 +       struct inode *inode;
15722 +       struct super_block *sb;
15723 +
15724 +       file = kio->ki_filp;
15725 +       inode = file_inode(file);
15726 +       sb = inode->i_sb;
15727 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15728 +
15729 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15730 +       err = PTR_ERR(h_file);
15731 +       if (IS_ERR(h_file))
15732 +               goto out;
15733 +
15734 +       if (au_test_loopback_kthread()) {
15735 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15736 +               if (file->f_mapping != h_file->f_mapping) {
15737 +                       file->f_mapping = h_file->f_mapping;
15738 +                       smp_mb(); /* unnecessary? */
15739 +               }
15740 +       }
15741 +       fi_read_unlock(file);
15742 +
15743 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15744 +       /* todo: necessary? */
15745 +       /* file->f_ra = h_file->f_ra; */
15746 +       au_read_post(inode, h_file);
15747 +
15748 +out:
15749 +       si_read_unlock(sb);
15750 +       return err;
15751 +}
15752 +
15753 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15754 +{
15755 +       ssize_t err;
15756 +       struct au_write_pre wpre;
15757 +       struct inode *inode;
15758 +       struct file *file, *h_file;
15759 +
15760 +       file = kio->ki_filp;
15761 +       inode = file_inode(file);
15762 +       au_mtx_and_read_lock(inode);
15763 +
15764 +       wpre.lsc = 0;
15765 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15766 +       err = PTR_ERR(h_file);
15767 +       if (IS_ERR(h_file))
15768 +               goto out;
15769 +
15770 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15771 +       au_write_post(inode, h_file, &wpre, err);
15772 +
15773 +out:
15774 +       si_read_unlock(inode->i_sb);
15775 +       inode_unlock(inode);
15776 +       return err;
15777 +}
15778 +
15779 +/*
15780 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15781 + * don't have their own .splice_{read,write} implimentations, and they use
15782 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15783 + * simple converters to f_op->iter_read() and ->iter_write().
15784 + * But we keep our own implementations because some non-mainlined FSes may have
15785 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15786 + * away an opportunity to co-work with aufs from them.
15787 + */
15788 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15789 +                               struct pipe_inode_info *pipe, size_t len,
15790 +                               unsigned int flags)
15791 +{
15792 +       ssize_t err;
15793 +       struct file *h_file;
15794 +       struct inode *inode;
15795 +       struct super_block *sb;
15796 +
15797 +       inode = file_inode(file);
15798 +       sb = inode->i_sb;
15799 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15800 +
15801 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15802 +       err = PTR_ERR(h_file);
15803 +       if (IS_ERR(h_file))
15804 +               goto out;
15805 +
15806 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15807 +       /* todo: necessary? */
15808 +       /* file->f_ra = h_file->f_ra; */
15809 +       au_read_post(inode, h_file);
15810 +
15811 +out:
15812 +       si_read_unlock(sb);
15813 +       return err;
15814 +}
15815 +
15816 +static ssize_t
15817 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15818 +                 size_t len, unsigned int flags)
15819 +{
15820 +       ssize_t err;
15821 +       struct au_write_pre wpre;
15822 +       struct inode *inode;
15823 +       struct file *h_file;
15824 +
15825 +       inode = file_inode(file);
15826 +       au_mtx_and_read_lock(inode);
15827 +
15828 +       wpre.lsc = 0;
15829 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15830 +       err = PTR_ERR(h_file);
15831 +       if (IS_ERR(h_file))
15832 +               goto out;
15833 +
15834 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15835 +       au_write_post(inode, h_file, &wpre, err);
15836 +
15837 +out:
15838 +       si_read_unlock(inode->i_sb);
15839 +       inode_unlock(inode);
15840 +       return err;
15841 +}
15842 +
15843 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15844 +                          loff_t len)
15845 +{
15846 +       long err;
15847 +       struct au_write_pre wpre;
15848 +       struct inode *inode;
15849 +       struct file *h_file;
15850 +
15851 +       inode = file_inode(file);
15852 +       au_mtx_and_read_lock(inode);
15853 +
15854 +       wpre.lsc = 0;
15855 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15856 +       err = PTR_ERR(h_file);
15857 +       if (IS_ERR(h_file))
15858 +               goto out;
15859 +
15860 +       lockdep_off();
15861 +       err = vfs_fallocate(h_file, mode, offset, len);
15862 +       lockdep_on();
15863 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15864 +
15865 +out:
15866 +       si_read_unlock(inode->i_sb);
15867 +       inode_unlock(inode);
15868 +       return err;
15869 +}
15870 +
15871 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15872 +                                   struct file *dst, loff_t dst_pos,
15873 +                                   size_t len, unsigned int flags)
15874 +{
15875 +       ssize_t err;
15876 +       struct au_write_pre wpre;
15877 +       enum { SRC, DST };
15878 +       struct {
15879 +               struct inode *inode;
15880 +               struct file *h_file;
15881 +               struct super_block *h_sb;
15882 +       } a[2];
15883 +#define a_src  a[SRC]
15884 +#define a_dst  a[DST]
15885 +
15886 +       err = -EINVAL;
15887 +       a_src.inode = file_inode(src);
15888 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15889 +               goto out;
15890 +       a_dst.inode = file_inode(dst);
15891 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15892 +               goto out;
15893 +
15894 +       au_mtx_and_read_lock(a_dst.inode);
15895 +       /*
15896 +        * in order to match the order in di_write_lock2_{child,parent}(),
15897 +        * use f_path.dentry for this comparison.
15898 +        */
15899 +       if (src->f_path.dentry < dst->f_path.dentry) {
15900 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15901 +               err = PTR_ERR(a_src.h_file);
15902 +               if (IS_ERR(a_src.h_file))
15903 +                       goto out_si;
15904 +
15905 +               wpre.lsc = AuLsc_FI_2;
15906 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15907 +               err = PTR_ERR(a_dst.h_file);
15908 +               if (IS_ERR(a_dst.h_file)) {
15909 +                       au_read_post(a_src.inode, a_src.h_file);
15910 +                       goto out_si;
15911 +               }
15912 +       } else {
15913 +               wpre.lsc = AuLsc_FI_1;
15914 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15915 +               err = PTR_ERR(a_dst.h_file);
15916 +               if (IS_ERR(a_dst.h_file))
15917 +                       goto out_si;
15918 +
15919 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15920 +               err = PTR_ERR(a_src.h_file);
15921 +               if (IS_ERR(a_src.h_file)) {
15922 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15923 +                                     /*written*/0);
15924 +                       goto out_si;
15925 +               }
15926 +       }
15927 +
15928 +       err = -EXDEV;
15929 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15930 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15931 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15932 +               AuDbgFile(src);
15933 +               AuDbgFile(dst);
15934 +               goto out_file;
15935 +       }
15936 +
15937 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15938 +                                   dst_pos, len, flags);
15939 +
15940 +out_file:
15941 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15942 +       fi_read_unlock(src);
15943 +       au_read_post(a_src.inode, a_src.h_file);
15944 +out_si:
15945 +       si_read_unlock(a_dst.inode->i_sb);
15946 +       inode_unlock(a_dst.inode);
15947 +out:
15948 +       return err;
15949 +#undef a_src
15950 +#undef a_dst
15951 +}
15952 +
15953 +/* ---------------------------------------------------------------------- */
15954 +
15955 +/*
15956 + * The locking order around current->mmap_sem.
15957 + * - in most and regular cases
15958 + *   file I/O syscall -- aufs_read() or something
15959 + *     -- si_rwsem for read -- mmap_sem
15960 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15961 + * - in mmap case
15962 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15963 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15964 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15965 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15966 + * It means that when aufs acquires si_rwsem for write, the process should never
15967 + * acquire mmap_sem.
15968 + *
15969 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15970 + * problem either since any directory is not able to be mmap-ed.
15971 + * The similar scenario is applied to aufs_readlink() too.
15972 + */
15973 +
15974 +#if 0 /* stop calling security_file_mmap() */
15975 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15976 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15977 +
15978 +static unsigned long au_arch_prot_conv(unsigned long flags)
15979 +{
15980 +       /* currently ppc64 only */
15981 +#ifdef CONFIG_PPC64
15982 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15983 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15984 +       return AuConv_VM_PROT(flags, SAO);
15985 +#else
15986 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15987 +       return 0;
15988 +#endif
15989 +}
15990 +
15991 +static unsigned long au_prot_conv(unsigned long flags)
15992 +{
15993 +       return AuConv_VM_PROT(flags, READ)
15994 +               | AuConv_VM_PROT(flags, WRITE)
15995 +               | AuConv_VM_PROT(flags, EXEC)
15996 +               | au_arch_prot_conv(flags);
15997 +}
15998 +
15999 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16000 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16001 +
16002 +static unsigned long au_flag_conv(unsigned long flags)
16003 +{
16004 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16005 +               | AuConv_VM_MAP(flags, DENYWRITE)
16006 +               | AuConv_VM_MAP(flags, LOCKED);
16007 +}
16008 +#endif
16009 +
16010 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16011 +{
16012 +       int err;
16013 +       const unsigned char wlock
16014 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16015 +       struct super_block *sb;
16016 +       struct file *h_file;
16017 +       struct inode *inode;
16018 +
16019 +       AuDbgVmRegion(file, vma);
16020 +
16021 +       inode = file_inode(file);
16022 +       sb = inode->i_sb;
16023 +       lockdep_off();
16024 +       si_read_lock(sb, AuLock_NOPLMW);
16025 +
16026 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16027 +       lockdep_on();
16028 +       err = PTR_ERR(h_file);
16029 +       if (IS_ERR(h_file))
16030 +               goto out;
16031 +
16032 +       err = 0;
16033 +       au_set_mmapped(file);
16034 +       au_vm_file_reset(vma, h_file);
16035 +       /*
16036 +        * we cannot call security_mmap_file() here since it may acquire
16037 +        * mmap_sem or i_mutex.
16038 +        *
16039 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16040 +        *                       au_flag_conv(vma->vm_flags));
16041 +        */
16042 +       if (!err)
16043 +               err = call_mmap(h_file, vma);
16044 +       if (!err) {
16045 +               au_vm_prfile_set(vma, file);
16046 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16047 +               goto out_fput; /* success */
16048 +       }
16049 +       au_unset_mmapped(file);
16050 +       au_vm_file_reset(vma, file);
16051 +
16052 +out_fput:
16053 +       lockdep_off();
16054 +       ii_write_unlock(inode);
16055 +       lockdep_on();
16056 +       fput(h_file);
16057 +out:
16058 +       lockdep_off();
16059 +       si_read_unlock(sb);
16060 +       lockdep_on();
16061 +       AuTraceErr(err);
16062 +       return err;
16063 +}
16064 +
16065 +/* ---------------------------------------------------------------------- */
16066 +
16067 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16068 +                            int datasync)
16069 +{
16070 +       int err;
16071 +       struct au_write_pre wpre;
16072 +       struct inode *inode;
16073 +       struct file *h_file;
16074 +
16075 +       err = 0; /* -EBADF; */ /* posix? */
16076 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16077 +               goto out;
16078 +
16079 +       inode = file_inode(file);
16080 +       au_mtx_and_read_lock(inode);
16081 +
16082 +       wpre.lsc = 0;
16083 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16084 +       err = PTR_ERR(h_file);
16085 +       if (IS_ERR(h_file))
16086 +               goto out_unlock;
16087 +
16088 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16089 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16090 +
16091 +out_unlock:
16092 +       si_read_unlock(inode->i_sb);
16093 +       inode_unlock(inode);
16094 +out:
16095 +       return err;
16096 +}
16097 +
16098 +static int aufs_fasync(int fd, struct file *file, int flag)
16099 +{
16100 +       int err;
16101 +       struct file *h_file;
16102 +       struct super_block *sb;
16103 +
16104 +       sb = file->f_path.dentry->d_sb;
16105 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16106 +
16107 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16108 +       err = PTR_ERR(h_file);
16109 +       if (IS_ERR(h_file))
16110 +               goto out;
16111 +
16112 +       if (h_file->f_op->fasync)
16113 +               err = h_file->f_op->fasync(fd, h_file, flag);
16114 +       fput(h_file); /* instead of au_read_post() */
16115 +
16116 +out:
16117 +       si_read_unlock(sb);
16118 +       return err;
16119 +}
16120 +
16121 +static int aufs_setfl(struct file *file, unsigned long arg)
16122 +{
16123 +       int err;
16124 +       struct file *h_file;
16125 +       struct super_block *sb;
16126 +
16127 +       sb = file->f_path.dentry->d_sb;
16128 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16129 +
16130 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16131 +       err = PTR_ERR(h_file);
16132 +       if (IS_ERR(h_file))
16133 +               goto out;
16134 +
16135 +       /* stop calling h_file->fasync */
16136 +       arg |= vfsub_file_flags(file) & FASYNC;
16137 +       err = setfl(/*unused fd*/-1, h_file, arg);
16138 +       fput(h_file); /* instead of au_read_post() */
16139 +
16140 +out:
16141 +       si_read_unlock(sb);
16142 +       return err;
16143 +}
16144 +
16145 +/* ---------------------------------------------------------------------- */
16146 +
16147 +/* no one supports this operation, currently */
16148 +#if 0 /* reserved for future use */
16149 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16150 +                            size_t len, loff_t *pos, int more)
16151 +{
16152 +}
16153 +#endif
16154 +
16155 +/* ---------------------------------------------------------------------- */
16156 +
16157 +const struct file_operations aufs_file_fop = {
16158 +       .owner          = THIS_MODULE,
16159 +
16160 +       .llseek         = default_llseek,
16161 +
16162 +       .read_iter      = aufs_read_iter,
16163 +       .write_iter     = aufs_write_iter,
16164 +
16165 +#ifdef CONFIG_AUFS_POLL
16166 +       .poll           = aufs_poll,
16167 +#endif
16168 +       .unlocked_ioctl = aufs_ioctl_nondir,
16169 +#ifdef CONFIG_COMPAT
16170 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16171 +#endif
16172 +       .mmap           = aufs_mmap,
16173 +       .open           = aufs_open_nondir,
16174 +       .flush          = aufs_flush_nondir,
16175 +       .release        = aufs_release_nondir,
16176 +       .fsync          = aufs_fsync_nondir,
16177 +       .fasync         = aufs_fasync,
16178 +       /* .sendpage    = aufs_sendpage, */
16179 +       .setfl          = aufs_setfl,
16180 +       .splice_write   = aufs_splice_write,
16181 +       .splice_read    = aufs_splice_read,
16182 +#if 0 /* reserved for future use */
16183 +       .aio_splice_write = aufs_aio_splice_write,
16184 +       .aio_splice_read  = aufs_aio_splice_read,
16185 +#endif
16186 +       .fallocate      = aufs_fallocate,
16187 +       .copy_file_range = aufs_copy_file_range
16188 +};
16189 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16190 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16191 +++ linux/fs/aufs/fstype.h      2021-11-01 23:48:34.206359262 +0100
16192 @@ -0,0 +1,401 @@
16193 +/* SPDX-License-Identifier: GPL-2.0 */
16194 +/*
16195 + * Copyright (C) 2005-2021 Junjiro R. Okajima
16196 + *
16197 + * This program, aufs is free software; you can redistribute it and/or modify
16198 + * it under the terms of the GNU General Public License as published by
16199 + * the Free Software Foundation; either version 2 of the License, or
16200 + * (at your option) any later version.
16201 + *
16202 + * This program is distributed in the hope that it will be useful,
16203 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16204 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16205 + * GNU General Public License for more details.
16206 + *
16207 + * You should have received a copy of the GNU General Public License
16208 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16209 + */
16210 +
16211 +/*
16212 + * judging filesystem type
16213 + */
16214 +
16215 +#ifndef __AUFS_FSTYPE_H__
16216 +#define __AUFS_FSTYPE_H__
16217 +
16218 +#ifdef __KERNEL__
16219 +
16220 +#include <linux/fs.h>
16221 +#include <linux/magic.h>
16222 +#include <linux/nfs_fs.h>
16223 +#include <linux/romfs_fs.h>
16224 +
16225 +static inline int au_test_aufs(struct super_block *sb)
16226 +{
16227 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16228 +}
16229 +
16230 +static inline const char *au_sbtype(struct super_block *sb)
16231 +{
16232 +       return sb->s_type->name;
16233 +}
16234 +
16235 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16236 +{
16237 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16238 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16239 +#else
16240 +       return 0;
16241 +#endif
16242 +}
16243 +
16244 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16245 +{
16246 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16247 +       return sb->s_magic == ROMFS_MAGIC;
16248 +#else
16249 +       return 0;
16250 +#endif
16251 +}
16252 +
16253 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16254 +{
16255 +#if IS_ENABLED(CONFIG_CRAMFS)
16256 +       return sb->s_magic == CRAMFS_MAGIC;
16257 +#endif
16258 +       return 0;
16259 +}
16260 +
16261 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16262 +{
16263 +#if IS_ENABLED(CONFIG_NFS_FS)
16264 +       return sb->s_magic == NFS_SUPER_MAGIC;
16265 +#else
16266 +       return 0;
16267 +#endif
16268 +}
16269 +
16270 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16271 +{
16272 +#if IS_ENABLED(CONFIG_FUSE_FS)
16273 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16274 +#else
16275 +       return 0;
16276 +#endif
16277 +}
16278 +
16279 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16280 +{
16281 +#if IS_ENABLED(CONFIG_XFS_FS)
16282 +       return sb->s_magic == XFS_SB_MAGIC;
16283 +#else
16284 +       return 0;
16285 +#endif
16286 +}
16287 +
16288 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16289 +{
16290 +#ifdef CONFIG_TMPFS
16291 +       return sb->s_magic == TMPFS_MAGIC;
16292 +#else
16293 +       return 0;
16294 +#endif
16295 +}
16296 +
16297 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16298 +{
16299 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16300 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16301 +#else
16302 +       return 0;
16303 +#endif
16304 +}
16305 +
16306 +static inline int au_test_ramfs(struct super_block *sb)
16307 +{
16308 +       return sb->s_magic == RAMFS_MAGIC;
16309 +}
16310 +
16311 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16312 +{
16313 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16314 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16315 +#else
16316 +       return 0;
16317 +#endif
16318 +}
16319 +
16320 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16321 +{
16322 +#ifdef CONFIG_PROC_FS
16323 +       return sb->s_magic == PROC_SUPER_MAGIC;
16324 +#else
16325 +       return 0;
16326 +#endif
16327 +}
16328 +
16329 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16330 +{
16331 +#ifdef CONFIG_SYSFS
16332 +       return sb->s_magic == SYSFS_MAGIC;
16333 +#else
16334 +       return 0;
16335 +#endif
16336 +}
16337 +
16338 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16339 +{
16340 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16341 +       return sb->s_magic == CONFIGFS_MAGIC;
16342 +#else
16343 +       return 0;
16344 +#endif
16345 +}
16346 +
16347 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16348 +{
16349 +#if IS_ENABLED(CONFIG_MINIX_FS)
16350 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16351 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16352 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16353 +               || sb->s_magic == MINIX_SUPER_MAGIC
16354 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16355 +#else
16356 +       return 0;
16357 +#endif
16358 +}
16359 +
16360 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16361 +{
16362 +#if IS_ENABLED(CONFIG_FAT_FS)
16363 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16364 +#else
16365 +       return 0;
16366 +#endif
16367 +}
16368 +
16369 +static inline int au_test_msdos(struct super_block *sb)
16370 +{
16371 +       return au_test_fat(sb);
16372 +}
16373 +
16374 +static inline int au_test_vfat(struct super_block *sb)
16375 +{
16376 +       return au_test_fat(sb);
16377 +}
16378 +
16379 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16380 +{
16381 +#ifdef CONFIG_SECURITYFS
16382 +       return sb->s_magic == SECURITYFS_MAGIC;
16383 +#else
16384 +       return 0;
16385 +#endif
16386 +}
16387 +
16388 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16389 +{
16390 +#if IS_ENABLED(CONFIG_SQUASHFS)
16391 +       return sb->s_magic == SQUASHFS_MAGIC;
16392 +#else
16393 +       return 0;
16394 +#endif
16395 +}
16396 +
16397 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16398 +{
16399 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16400 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16401 +#else
16402 +       return 0;
16403 +#endif
16404 +}
16405 +
16406 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16407 +{
16408 +#if IS_ENABLED(CONFIG_XENFS)
16409 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16410 +#else
16411 +       return 0;
16412 +#endif
16413 +}
16414 +
16415 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16416 +{
16417 +#ifdef CONFIG_DEBUG_FS
16418 +       return sb->s_magic == DEBUGFS_MAGIC;
16419 +#else
16420 +       return 0;
16421 +#endif
16422 +}
16423 +
16424 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16425 +{
16426 +#if IS_ENABLED(CONFIG_NILFS)
16427 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16428 +#else
16429 +       return 0;
16430 +#endif
16431 +}
16432 +
16433 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16434 +{
16435 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16436 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16437 +#else
16438 +       return 0;
16439 +#endif
16440 +}
16441 +
16442 +/* ---------------------------------------------------------------------- */
16443 +/*
16444 + * they can't be an aufs branch.
16445 + */
16446 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16447 +{
16448 +       return
16449 +#ifndef CONFIG_AUFS_BR_RAMFS
16450 +               au_test_ramfs(sb) ||
16451 +#endif
16452 +               au_test_procfs(sb)
16453 +               || au_test_sysfs(sb)
16454 +               || au_test_configfs(sb)
16455 +               || au_test_debugfs(sb)
16456 +               || au_test_securityfs(sb)
16457 +               || au_test_xenfs(sb)
16458 +               || au_test_ecryptfs(sb)
16459 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16460 +               || au_test_aufs(sb); /* will be supported in next version */
16461 +}
16462 +
16463 +static inline int au_test_fs_remote(struct super_block *sb)
16464 +{
16465 +       return !au_test_tmpfs(sb)
16466 +#ifdef CONFIG_AUFS_BR_RAMFS
16467 +               && !au_test_ramfs(sb)
16468 +#endif
16469 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16470 +}
16471 +
16472 +/* ---------------------------------------------------------------------- */
16473 +
16474 +/*
16475 + * Note: these functions (below) are created after reading ->getattr() in all
16476 + * filesystems under linux/fs. it means we have to do so in every update...
16477 + */
16478 +
16479 +/*
16480 + * some filesystems require getattr to refresh the inode attributes before
16481 + * referencing.
16482 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16483 + * and leave the work for d_revalidate()
16484 + */
16485 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16486 +{
16487 +       return au_test_nfs(sb)
16488 +               || au_test_fuse(sb)
16489 +               /* || au_test_btrfs(sb) */      /* untested */
16490 +               ;
16491 +}
16492 +
16493 +/*
16494 + * filesystems which don't maintain i_size or i_blocks.
16495 + */
16496 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16497 +{
16498 +       return au_test_xfs(sb)
16499 +               || au_test_btrfs(sb)
16500 +               || au_test_ubifs(sb)
16501 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16502 +               /* || au_test_minix(sb) */      /* untested */
16503 +               ;
16504 +}
16505 +
16506 +/*
16507 + * filesystems which don't store the correct value in some of their inode
16508 + * attributes.
16509 + */
16510 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16511 +{
16512 +       return au_test_fs_bad_iattr_size(sb)
16513 +               || au_test_fat(sb)
16514 +               || au_test_msdos(sb)
16515 +               || au_test_vfat(sb);
16516 +}
16517 +
16518 +/* they don't check i_nlink in link(2) */
16519 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16520 +{
16521 +       return au_test_tmpfs(sb)
16522 +#ifdef CONFIG_AUFS_BR_RAMFS
16523 +               || au_test_ramfs(sb)
16524 +#endif
16525 +               || au_test_ubifs(sb)
16526 +               || au_test_hfsplus(sb);
16527 +}
16528 +
16529 +/*
16530 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16531 + */
16532 +static inline int au_test_fs_notime(struct super_block *sb)
16533 +{
16534 +       return au_test_nfs(sb)
16535 +               || au_test_fuse(sb)
16536 +               || au_test_ubifs(sb)
16537 +               ;
16538 +}
16539 +
16540 +/* temporary support for i#1 in cramfs */
16541 +static inline int au_test_fs_unique_ino(struct inode *inode)
16542 +{
16543 +       if (au_test_cramfs(inode->i_sb))
16544 +               return inode->i_ino != 1;
16545 +       return 1;
16546 +}
16547 +
16548 +/* ---------------------------------------------------------------------- */
16549 +
16550 +/*
16551 + * the filesystem where the xino files placed must support i/o after unlink and
16552 + * maintain i_size and i_blocks.
16553 + */
16554 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16555 +{
16556 +       return au_test_fs_remote(sb)
16557 +               || au_test_fs_bad_iattr_size(sb)
16558 +               /* don't want unnecessary work for xino */
16559 +               || au_test_aufs(sb)
16560 +               || au_test_ecryptfs(sb)
16561 +               || au_test_nilfs(sb);
16562 +}
16563 +
16564 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16565 +{
16566 +       return au_test_tmpfs(sb)
16567 +               || au_test_ramfs(sb);
16568 +}
16569 +
16570 +/*
16571 + * test if the @sb is real-readonly.
16572 + */
16573 +static inline int au_test_fs_rr(struct super_block *sb)
16574 +{
16575 +       return au_test_squashfs(sb)
16576 +               || au_test_iso9660(sb)
16577 +               || au_test_cramfs(sb)
16578 +               || au_test_romfs(sb);
16579 +}
16580 +
16581 +/*
16582 + * test if the @inode is nfs with 'noacl' option
16583 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16584 + */
16585 +static inline int au_test_nfs_noacl(struct inode *inode)
16586 +{
16587 +       return au_test_nfs(inode->i_sb)
16588 +               /* && IS_POSIXACL(inode) */
16589 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16590 +}
16591 +
16592 +#endif /* __KERNEL__ */
16593 +#endif /* __AUFS_FSTYPE_H__ */
16594 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16595 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16596 +++ linux/fs/aufs/hbl.h 2021-11-01 23:48:34.206359262 +0100
16597 @@ -0,0 +1,65 @@
16598 +/* SPDX-License-Identifier: GPL-2.0 */
16599 +/*
16600 + * Copyright (C) 2017-2021 Junjiro R. Okajima
16601 + *
16602 + * This program, aufs is free software; you can redistribute it and/or modify
16603 + * it under the terms of the GNU General Public License as published by
16604 + * the Free Software Foundation; either version 2 of the License, or
16605 + * (at your option) any later version.
16606 + *
16607 + * This program is distributed in the hope that it will be useful,
16608 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16609 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16610 + * GNU General Public License for more details.
16611 + *
16612 + * You should have received a copy of the GNU General Public License
16613 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16614 + */
16615 +
16616 +/*
16617 + * helpers for hlist_bl.h
16618 + */
16619 +
16620 +#ifndef __AUFS_HBL_H__
16621 +#define __AUFS_HBL_H__
16622 +
16623 +#ifdef __KERNEL__
16624 +
16625 +#include <linux/list_bl.h>
16626 +
16627 +static inline void au_hbl_add(struct hlist_bl_node *node,
16628 +                             struct hlist_bl_head *hbl)
16629 +{
16630 +       hlist_bl_lock(hbl);
16631 +       hlist_bl_add_head(node, hbl);
16632 +       hlist_bl_unlock(hbl);
16633 +}
16634 +
16635 +static inline void au_hbl_del(struct hlist_bl_node *node,
16636 +                             struct hlist_bl_head *hbl)
16637 +{
16638 +       hlist_bl_lock(hbl);
16639 +       hlist_bl_del(node);
16640 +       hlist_bl_unlock(hbl);
16641 +}
16642 +
16643 +#define au_hbl_for_each(pos, head)                                     \
16644 +       for (pos = hlist_bl_first(head);                                \
16645 +            pos;                                                       \
16646 +            pos = pos->next)
16647 +
16648 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16649 +{
16650 +       unsigned long cnt;
16651 +       struct hlist_bl_node *pos;
16652 +
16653 +       cnt = 0;
16654 +       hlist_bl_lock(hbl);
16655 +       au_hbl_for_each(pos, hbl)
16656 +               cnt++;
16657 +       hlist_bl_unlock(hbl);
16658 +       return cnt;
16659 +}
16660 +
16661 +#endif /* __KERNEL__ */
16662 +#endif /* __AUFS_HBL_H__ */
16663 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16664 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16665 +++ linux/fs/aufs/hfsnotify.c   2021-11-25 11:42:28.836641524 +0100
16666 @@ -0,0 +1,288 @@
16667 +// SPDX-License-Identifier: GPL-2.0
16668 +/*
16669 + * Copyright (C) 2005-2021 Junjiro R. Okajima
16670 + *
16671 + * This program, aufs is free software; you can redistribute it and/or modify
16672 + * it under the terms of the GNU General Public License as published by
16673 + * the Free Software Foundation; either version 2 of the License, or
16674 + * (at your option) any later version.
16675 + *
16676 + * This program is distributed in the hope that it will be useful,
16677 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16678 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16679 + * GNU General Public License for more details.
16680 + *
16681 + * You should have received a copy of the GNU General Public License
16682 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16683 + */
16684 +
16685 +/*
16686 + * fsnotify for the lower directories
16687 + */
16688 +
16689 +#include "aufs.h"
16690 +
16691 +/* FS_IN_IGNORED is unnecessary */
16692 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16693 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16694 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16695 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16696 +
16697 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16698 +{
16699 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16700 +                                            hn_mark);
16701 +       /* AuDbg("here\n"); */
16702 +       au_cache_free_hnotify(hn);
16703 +       smp_mb__before_atomic(); /* for atomic64_dec */
16704 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16705 +               wake_up(&au_hfsn_wq);
16706 +}
16707 +
16708 +static int au_hfsn_alloc(struct au_hinode *hinode)
16709 +{
16710 +       int err;
16711 +       struct au_hnotify *hn;
16712 +       struct super_block *sb;
16713 +       struct au_branch *br;
16714 +       struct fsnotify_mark *mark;
16715 +       aufs_bindex_t bindex;
16716 +
16717 +       hn = hinode->hi_notify;
16718 +       sb = hn->hn_aufs_inode->i_sb;
16719 +       bindex = au_br_index(sb, hinode->hi_id);
16720 +       br = au_sbr(sb, bindex);
16721 +       AuDebugOn(!br->br_hfsn);
16722 +
16723 +       mark = &hn->hn_mark;
16724 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16725 +       mark->mask = AuHfsnMask;
16726 +       /*
16727 +        * by udba rename or rmdir, aufs assign a new inode to the known
16728 +        * h_inode, so specify 1 to allow dups.
16729 +        */
16730 +       lockdep_off();
16731 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16732 +       lockdep_on();
16733 +
16734 +       return err;
16735 +}
16736 +
16737 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16738 +{
16739 +       struct fsnotify_mark *mark;
16740 +       unsigned long long ull;
16741 +       struct fsnotify_group *group;
16742 +
16743 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16744 +       BUG_ON(!ull);
16745 +
16746 +       mark = &hn->hn_mark;
16747 +       spin_lock(&mark->lock);
16748 +       group = mark->group;
16749 +       fsnotify_get_group(group);
16750 +       spin_unlock(&mark->lock);
16751 +       lockdep_off();
16752 +       fsnotify_destroy_mark(mark, group);
16753 +       fsnotify_put_mark(mark);
16754 +       fsnotify_put_group(group);
16755 +       lockdep_on();
16756 +
16757 +       /* free hn by myself */
16758 +       return 0;
16759 +}
16760 +
16761 +/* ---------------------------------------------------------------------- */
16762 +
16763 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16764 +{
16765 +       struct fsnotify_mark *mark;
16766 +
16767 +       mark = &hinode->hi_notify->hn_mark;
16768 +       spin_lock(&mark->lock);
16769 +       if (do_set) {
16770 +               AuDebugOn(mark->mask & AuHfsnMask);
16771 +               mark->mask |= AuHfsnMask;
16772 +       } else {
16773 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16774 +               mark->mask &= ~AuHfsnMask;
16775 +       }
16776 +       spin_unlock(&mark->lock);
16777 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16778 +}
16779 +
16780 +/* ---------------------------------------------------------------------- */
16781 +
16782 +/* #define AuDbgHnotify */
16783 +#ifdef AuDbgHnotify
16784 +static char *au_hfsn_name(u32 mask)
16785 +{
16786 +#ifdef CONFIG_AUFS_DEBUG
16787 +#define test_ret(flag)                         \
16788 +       do {                                    \
16789 +               if (mask & flag)                \
16790 +                       return #flag;           \
16791 +       } while (0)
16792 +       test_ret(FS_ACCESS);
16793 +       test_ret(FS_MODIFY);
16794 +       test_ret(FS_ATTRIB);
16795 +       test_ret(FS_CLOSE_WRITE);
16796 +       test_ret(FS_CLOSE_NOWRITE);
16797 +       test_ret(FS_OPEN);
16798 +       test_ret(FS_MOVED_FROM);
16799 +       test_ret(FS_MOVED_TO);
16800 +       test_ret(FS_CREATE);
16801 +       test_ret(FS_DELETE);
16802 +       test_ret(FS_DELETE_SELF);
16803 +       test_ret(FS_MOVE_SELF);
16804 +       test_ret(FS_UNMOUNT);
16805 +       test_ret(FS_Q_OVERFLOW);
16806 +       test_ret(FS_IN_IGNORED);
16807 +       test_ret(FS_ISDIR);
16808 +       test_ret(FS_IN_ONESHOT);
16809 +       test_ret(FS_EVENT_ON_CHILD);
16810 +       return "";
16811 +#undef test_ret
16812 +#else
16813 +       return "??";
16814 +#endif
16815 +}
16816 +#endif
16817 +
16818 +/* ---------------------------------------------------------------------- */
16819 +
16820 +static void au_hfsn_free_group(struct fsnotify_group *group)
16821 +{
16822 +       struct au_br_hfsnotify *hfsn = group->private;
16823 +
16824 +       /* AuDbg("here\n"); */
16825 +       au_kfree_try_rcu(hfsn);
16826 +}
16827 +
16828 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16829 +                               u32 mask, const void *data, int data_type,
16830 +                               struct inode *dir,
16831 +                               const struct qstr *file_name, u32 cookie,
16832 +                               struct fsnotify_iter_info *iter_info)
16833 +{
16834 +       int err;
16835 +       struct au_hnotify *hnotify;
16836 +       struct inode *h_dir, *h_inode;
16837 +       struct fsnotify_mark *inode_mark;
16838 +
16839 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16840 +
16841 +       err = 0;
16842 +       /* if FS_UNMOUNT happens, there must be another bug */
16843 +       AuDebugOn(mask & FS_UNMOUNT);
16844 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16845 +               goto out;
16846 +
16847 +       h_dir = dir;
16848 +       h_inode = NULL;
16849 +#ifdef AuDbgHnotify
16850 +       au_debug_on();
16851 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16852 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16853 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16854 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16855 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16856 +               /* WARN_ON(1); */
16857 +       }
16858 +       au_debug_off();
16859 +#endif
16860 +
16861 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16862 +       AuDebugOn(!inode_mark);
16863 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16864 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16865 +
16866 +out:
16867 +       return err;
16868 +}
16869 +
16870 +static struct fsnotify_ops au_hfsn_ops = {
16871 +       .handle_event           = au_hfsn_handle_event,
16872 +       .free_group_priv        = au_hfsn_free_group,
16873 +       .free_mark              = au_hfsn_free_mark
16874 +};
16875 +
16876 +/* ---------------------------------------------------------------------- */
16877 +
16878 +static void au_hfsn_fin_br(struct au_branch *br)
16879 +{
16880 +       struct au_br_hfsnotify *hfsn;
16881 +
16882 +       hfsn = br->br_hfsn;
16883 +       if (hfsn) {
16884 +               lockdep_off();
16885 +               fsnotify_put_group(hfsn->hfsn_group);
16886 +               lockdep_on();
16887 +       }
16888 +}
16889 +
16890 +static int au_hfsn_init_br(struct au_branch *br, int perm)
16891 +{
16892 +       int err;
16893 +       struct fsnotify_group *group;
16894 +       struct au_br_hfsnotify *hfsn;
16895 +
16896 +       err = 0;
16897 +       br->br_hfsn = NULL;
16898 +       if (!au_br_hnotifyable(perm))
16899 +               goto out;
16900 +
16901 +       err = -ENOMEM;
16902 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16903 +       if (unlikely(!hfsn))
16904 +               goto out;
16905 +
16906 +       err = 0;
16907 +       group = fsnotify_alloc_group(&au_hfsn_ops);
16908 +       if (IS_ERR(group)) {
16909 +               err = PTR_ERR(group);
16910 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
16911 +               goto out_hfsn;
16912 +       }
16913 +
16914 +       group->private = hfsn;
16915 +       hfsn->hfsn_group = group;
16916 +       br->br_hfsn = hfsn;
16917 +       goto out; /* success */
16918 +
16919 +out_hfsn:
16920 +       au_kfree_try_rcu(hfsn);
16921 +out:
16922 +       return err;
16923 +}
16924 +
16925 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16926 +{
16927 +       int err;
16928 +
16929 +       err = 0;
16930 +       if (!br->br_hfsn)
16931 +               err = au_hfsn_init_br(br, perm);
16932 +
16933 +       return err;
16934 +}
16935 +
16936 +/* ---------------------------------------------------------------------- */
16937 +
16938 +static void au_hfsn_fin(void)
16939 +{
16940 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16941 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16942 +}
16943 +
16944 +const struct au_hnotify_op au_hnotify_op = {
16945 +       .ctl            = au_hfsn_ctl,
16946 +       .alloc          = au_hfsn_alloc,
16947 +       .free           = au_hfsn_free,
16948 +
16949 +       .fin            = au_hfsn_fin,
16950 +
16951 +       .reset_br       = au_hfsn_reset_br,
16952 +       .fin_br         = au_hfsn_fin_br,
16953 +       .init_br        = au_hfsn_init_br
16954 +};
16955 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16956 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
16957 +++ linux/fs/aufs/hfsplus.c     2021-11-01 23:48:34.206359262 +0100
16958 @@ -0,0 +1,60 @@
16959 +// SPDX-License-Identifier: GPL-2.0
16960 +/*
16961 + * Copyright (C) 2010-2021 Junjiro R. Okajima
16962 + *
16963 + * This program, aufs is free software; you can redistribute it and/or modify
16964 + * it under the terms of the GNU General Public License as published by
16965 + * the Free Software Foundation; either version 2 of the License, or
16966 + * (at your option) any later version.
16967 + *
16968 + * This program is distributed in the hope that it will be useful,
16969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16971 + * GNU General Public License for more details.
16972 + *
16973 + * You should have received a copy of the GNU General Public License
16974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16975 + */
16976 +
16977 +/*
16978 + * special support for filesystems which acquires an inode mutex
16979 + * at final closing a file, eg, hfsplus.
16980 + *
16981 + * This trick is very simple and stupid, just to open the file before really
16982 + * necessary open to tell hfsplus that this is not the final closing.
16983 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
16984 + * and au_h_open_post() after releasing it.
16985 + */
16986 +
16987 +#include "aufs.h"
16988 +
16989 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
16990 +                          int force_wr)
16991 +{
16992 +       struct file *h_file;
16993 +       struct dentry *h_dentry;
16994 +
16995 +       h_dentry = au_h_dptr(dentry, bindex);
16996 +       AuDebugOn(!h_dentry);
16997 +       AuDebugOn(d_is_negative(h_dentry));
16998 +
16999 +       h_file = NULL;
17000 +       if (au_test_hfsplus(h_dentry->d_sb)
17001 +           && d_is_reg(h_dentry))
17002 +               h_file = au_h_open(dentry, bindex,
17003 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17004 +                                  /*file*/NULL, force_wr);
17005 +       return h_file;
17006 +}
17007 +
17008 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17009 +                   struct file *h_file)
17010 +{
17011 +       struct au_branch *br;
17012 +
17013 +       if (h_file) {
17014 +               fput(h_file);
17015 +               br = au_sbr(dentry->d_sb, bindex);
17016 +               au_lcnt_dec(&br->br_nfiles);
17017 +       }
17018 +}
17019 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17020 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17021 +++ linux/fs/aufs/hnotify.c     2021-11-01 23:48:34.209692595 +0100
17022 @@ -0,0 +1,715 @@
17023 +// SPDX-License-Identifier: GPL-2.0
17024 +/*
17025 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17026 + *
17027 + * This program, aufs is free software; you can redistribute it and/or modify
17028 + * it under the terms of the GNU General Public License as published by
17029 + * the Free Software Foundation; either version 2 of the License, or
17030 + * (at your option) any later version.
17031 + *
17032 + * This program is distributed in the hope that it will be useful,
17033 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17034 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17035 + * GNU General Public License for more details.
17036 + *
17037 + * You should have received a copy of the GNU General Public License
17038 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17039 + */
17040 +
17041 +/*
17042 + * abstraction to notify the direct changes on lower directories
17043 + */
17044 +
17045 +/* #include <linux/iversion.h> */
17046 +#include "aufs.h"
17047 +
17048 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17049 +{
17050 +       int err;
17051 +       struct au_hnotify *hn;
17052 +
17053 +       err = -ENOMEM;
17054 +       hn = au_cache_alloc_hnotify();
17055 +       if (hn) {
17056 +               hn->hn_aufs_inode = inode;
17057 +               hinode->hi_notify = hn;
17058 +               err = au_hnotify_op.alloc(hinode);
17059 +               AuTraceErr(err);
17060 +               if (unlikely(err)) {
17061 +                       hinode->hi_notify = NULL;
17062 +                       au_cache_free_hnotify(hn);
17063 +                       /*
17064 +                        * The upper dir was removed by udba, but the same named
17065 +                        * dir left. In this case, aufs assigns a new inode
17066 +                        * number and set the monitor again.
17067 +                        * For the lower dir, the old monitor is still left.
17068 +                        */
17069 +                       if (err == -EEXIST)
17070 +                               err = 0;
17071 +               }
17072 +       }
17073 +
17074 +       AuTraceErr(err);
17075 +       return err;
17076 +}
17077 +
17078 +void au_hn_free(struct au_hinode *hinode)
17079 +{
17080 +       struct au_hnotify *hn;
17081 +
17082 +       hn = hinode->hi_notify;
17083 +       if (hn) {
17084 +               hinode->hi_notify = NULL;
17085 +               if (au_hnotify_op.free(hinode, hn))
17086 +                       au_cache_free_hnotify(hn);
17087 +       }
17088 +}
17089 +
17090 +/* ---------------------------------------------------------------------- */
17091 +
17092 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17093 +{
17094 +       if (hinode->hi_notify)
17095 +               au_hnotify_op.ctl(hinode, do_set);
17096 +}
17097 +
17098 +void au_hn_reset(struct inode *inode, unsigned int flags)
17099 +{
17100 +       aufs_bindex_t bindex, bbot;
17101 +       struct inode *hi;
17102 +       struct dentry *iwhdentry;
17103 +
17104 +       bbot = au_ibbot(inode);
17105 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17106 +               hi = au_h_iptr(inode, bindex);
17107 +               if (!hi)
17108 +                       continue;
17109 +
17110 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17111 +               iwhdentry = au_hi_wh(inode, bindex);
17112 +               if (iwhdentry)
17113 +                       dget(iwhdentry);
17114 +               au_igrab(hi);
17115 +               au_set_h_iptr(inode, bindex, NULL, 0);
17116 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17117 +                             flags & ~AuHi_XINO);
17118 +               iput(hi);
17119 +               dput(iwhdentry);
17120 +               /* inode_unlock(hi); */
17121 +       }
17122 +}
17123 +
17124 +/* ---------------------------------------------------------------------- */
17125 +
17126 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17127 +{
17128 +       int err;
17129 +       aufs_bindex_t bindex, bbot, bfound, btop;
17130 +       struct inode *h_i;
17131 +
17132 +       err = 0;
17133 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17134 +               pr_warn("branch root dir was changed\n");
17135 +               goto out;
17136 +       }
17137 +
17138 +       bfound = -1;
17139 +       bbot = au_ibbot(inode);
17140 +       btop = au_ibtop(inode);
17141 +#if 0 /* reserved for future use */
17142 +       if (bindex == bbot) {
17143 +               /* keep this ino in rename case */
17144 +               goto out;
17145 +       }
17146 +#endif
17147 +       for (bindex = btop; bindex <= bbot; bindex++)
17148 +               if (au_h_iptr(inode, bindex) == h_inode) {
17149 +                       bfound = bindex;
17150 +                       break;
17151 +               }
17152 +       if (bfound < 0)
17153 +               goto out;
17154 +
17155 +       for (bindex = btop; bindex <= bbot; bindex++) {
17156 +               h_i = au_h_iptr(inode, bindex);
17157 +               if (!h_i)
17158 +                       continue;
17159 +
17160 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17161 +               /* ignore this error */
17162 +               /* bad action? */
17163 +       }
17164 +
17165 +       /* children inode number will be broken */
17166 +
17167 +out:
17168 +       AuTraceErr(err);
17169 +       return err;
17170 +}
17171 +
17172 +static int hn_gen_tree(struct dentry *dentry)
17173 +{
17174 +       int err, i, j, ndentry;
17175 +       struct au_dcsub_pages dpages;
17176 +       struct au_dpage *dpage;
17177 +       struct dentry **dentries;
17178 +
17179 +       err = au_dpages_init(&dpages, GFP_NOFS);
17180 +       if (unlikely(err))
17181 +               goto out;
17182 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17183 +       if (unlikely(err))
17184 +               goto out_dpages;
17185 +
17186 +       for (i = 0; i < dpages.ndpage; i++) {
17187 +               dpage = dpages.dpages + i;
17188 +               dentries = dpage->dentries;
17189 +               ndentry = dpage->ndentry;
17190 +               for (j = 0; j < ndentry; j++) {
17191 +                       struct dentry *d;
17192 +
17193 +                       d = dentries[j];
17194 +                       if (IS_ROOT(d))
17195 +                               continue;
17196 +
17197 +                       au_digen_dec(d);
17198 +                       if (d_really_is_positive(d))
17199 +                               /* todo: reset children xino?
17200 +                                  cached children only? */
17201 +                               au_iigen_dec(d_inode(d));
17202 +               }
17203 +       }
17204 +
17205 +out_dpages:
17206 +       au_dpages_free(&dpages);
17207 +out:
17208 +       return err;
17209 +}
17210 +
17211 +/*
17212 + * return 0 if processed.
17213 + */
17214 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17215 +                          const unsigned int isdir)
17216 +{
17217 +       int err;
17218 +       struct dentry *d;
17219 +       struct qstr *dname;
17220 +
17221 +       err = 1;
17222 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17223 +               pr_warn("branch root dir was changed\n");
17224 +               err = 0;
17225 +               goto out;
17226 +       }
17227 +
17228 +       if (!isdir) {
17229 +               AuDebugOn(!name);
17230 +               au_iigen_dec(inode);
17231 +               spin_lock(&inode->i_lock);
17232 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17233 +                       spin_lock(&d->d_lock);
17234 +                       dname = &d->d_name;
17235 +                       if (dname->len != nlen
17236 +                           && memcmp(dname->name, name, nlen)) {
17237 +                               spin_unlock(&d->d_lock);
17238 +                               continue;
17239 +                       }
17240 +                       err = 0;
17241 +                       au_digen_dec(d);
17242 +                       spin_unlock(&d->d_lock);
17243 +                       break;
17244 +               }
17245 +               spin_unlock(&inode->i_lock);
17246 +       } else {
17247 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17248 +               d = d_find_any_alias(inode);
17249 +               if (!d) {
17250 +                       au_iigen_dec(inode);
17251 +                       goto out;
17252 +               }
17253 +
17254 +               spin_lock(&d->d_lock);
17255 +               dname = &d->d_name;
17256 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17257 +                       spin_unlock(&d->d_lock);
17258 +                       err = hn_gen_tree(d);
17259 +                       spin_lock(&d->d_lock);
17260 +               }
17261 +               spin_unlock(&d->d_lock);
17262 +               dput(d);
17263 +       }
17264 +
17265 +out:
17266 +       AuTraceErr(err);
17267 +       return err;
17268 +}
17269 +
17270 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17271 +{
17272 +       int err;
17273 +
17274 +       if (IS_ROOT(dentry)) {
17275 +               pr_warn("branch root dir was changed\n");
17276 +               return 0;
17277 +       }
17278 +
17279 +       err = 0;
17280 +       if (!isdir) {
17281 +               au_digen_dec(dentry);
17282 +               if (d_really_is_positive(dentry))
17283 +                       au_iigen_dec(d_inode(dentry));
17284 +       } else {
17285 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17286 +               if (d_really_is_positive(dentry))
17287 +                       err = hn_gen_tree(dentry);
17288 +       }
17289 +
17290 +       AuTraceErr(err);
17291 +       return err;
17292 +}
17293 +
17294 +/* ---------------------------------------------------------------------- */
17295 +
17296 +/* hnotify job flags */
17297 +#define AuHnJob_XINO0          1
17298 +#define AuHnJob_GEN            (1 << 1)
17299 +#define AuHnJob_DIRENT         (1 << 2)
17300 +#define AuHnJob_ISDIR          (1 << 3)
17301 +#define AuHnJob_TRYXINO0       (1 << 4)
17302 +#define AuHnJob_MNTPNT         (1 << 5)
17303 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17304 +#define au_fset_hnjob(flags, name) \
17305 +       do { (flags) |= AuHnJob_##name; } while (0)
17306 +#define au_fclr_hnjob(flags, name) \
17307 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17308 +
17309 +enum {
17310 +       AuHn_CHILD,
17311 +       AuHn_PARENT,
17312 +       AuHnLast
17313 +};
17314 +
17315 +struct au_hnotify_args {
17316 +       struct inode *h_dir, *dir, *h_child_inode;
17317 +       u32 mask;
17318 +       unsigned int flags[AuHnLast];
17319 +       unsigned int h_child_nlen;
17320 +       char h_child_name[];
17321 +};
17322 +
17323 +struct hn_job_args {
17324 +       unsigned int flags;
17325 +       struct inode *inode, *h_inode, *dir, *h_dir;
17326 +       struct dentry *dentry;
17327 +       char *h_name;
17328 +       int h_nlen;
17329 +};
17330 +
17331 +static int hn_job(struct hn_job_args *a)
17332 +{
17333 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17334 +       int e;
17335 +
17336 +       /* reset xino */
17337 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17338 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17339 +
17340 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17341 +           && a->inode
17342 +           && a->h_inode) {
17343 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17344 +               if (!a->h_inode->i_nlink
17345 +                   && !(a->h_inode->i_state & I_LINKABLE))
17346 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17347 +               inode_unlock_shared(a->h_inode);
17348 +       }
17349 +
17350 +       /* make the generation obsolete */
17351 +       if (au_ftest_hnjob(a->flags, GEN)) {
17352 +               e = -1;
17353 +               if (a->inode)
17354 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17355 +                                             isdir);
17356 +               if (e && a->dentry)
17357 +                       hn_gen_by_name(a->dentry, isdir);
17358 +               /* ignore this error */
17359 +       }
17360 +
17361 +       /* make dir entries obsolete */
17362 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17363 +               struct au_vdir *vdir;
17364 +
17365 +               vdir = au_ivdir(a->inode);
17366 +               if (vdir)
17367 +                       vdir->vd_jiffy = 0;
17368 +               /* IMustLock(a->inode); */
17369 +               /* inode_inc_iversion(a->inode); */
17370 +       }
17371 +
17372 +       /* can do nothing but warn */
17373 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17374 +           && a->dentry
17375 +           && d_mountpoint(a->dentry))
17376 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17377 +
17378 +       return 0;
17379 +}
17380 +
17381 +/* ---------------------------------------------------------------------- */
17382 +
17383 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17384 +                                          struct inode *dir)
17385 +{
17386 +       struct dentry *dentry, *d, *parent;
17387 +       struct qstr *dname;
17388 +
17389 +       parent = d_find_any_alias(dir);
17390 +       if (!parent)
17391 +               return NULL;
17392 +
17393 +       dentry = NULL;
17394 +       spin_lock(&parent->d_lock);
17395 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17396 +               /* AuDbg("%pd\n", d); */
17397 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17398 +               dname = &d->d_name;
17399 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17400 +                       goto cont_unlock;
17401 +               if (au_di(d))
17402 +                       au_digen_dec(d);
17403 +               else
17404 +                       goto cont_unlock;
17405 +               if (au_dcount(d) > 0) {
17406 +                       dentry = dget_dlock(d);
17407 +                       spin_unlock(&d->d_lock);
17408 +                       break;
17409 +               }
17410 +
17411 +cont_unlock:
17412 +               spin_unlock(&d->d_lock);
17413 +       }
17414 +       spin_unlock(&parent->d_lock);
17415 +       dput(parent);
17416 +
17417 +       if (dentry)
17418 +               di_write_lock_child(dentry);
17419 +
17420 +       return dentry;
17421 +}
17422 +
17423 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17424 +                                        aufs_bindex_t bindex, ino_t h_ino)
17425 +{
17426 +       struct inode *inode;
17427 +       ino_t ino;
17428 +       int err;
17429 +
17430 +       inode = NULL;
17431 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17432 +       if (!err && ino)
17433 +               inode = ilookup(sb, ino);
17434 +       if (!inode)
17435 +               goto out;
17436 +
17437 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17438 +               pr_warn("wrong root branch\n");
17439 +               iput(inode);
17440 +               inode = NULL;
17441 +               goto out;
17442 +       }
17443 +
17444 +       ii_write_lock_child(inode);
17445 +
17446 +out:
17447 +       return inode;
17448 +}
17449 +
17450 +static void au_hn_bh(void *_args)
17451 +{
17452 +       struct au_hnotify_args *a = _args;
17453 +       struct super_block *sb;
17454 +       aufs_bindex_t bindex, bbot, bfound;
17455 +       unsigned char xino, try_iput;
17456 +       int err;
17457 +       struct inode *inode;
17458 +       ino_t h_ino;
17459 +       struct hn_job_args args;
17460 +       struct dentry *dentry;
17461 +       struct au_sbinfo *sbinfo;
17462 +
17463 +       AuDebugOn(!_args);
17464 +       AuDebugOn(!a->h_dir);
17465 +       AuDebugOn(!a->dir);
17466 +       AuDebugOn(!a->mask);
17467 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17468 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17469 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17470 +
17471 +       inode = NULL;
17472 +       dentry = NULL;
17473 +       /*
17474 +        * do not lock a->dir->i_mutex here
17475 +        * because of d_revalidate() may cause a deadlock.
17476 +        */
17477 +       sb = a->dir->i_sb;
17478 +       AuDebugOn(!sb);
17479 +       sbinfo = au_sbi(sb);
17480 +       AuDebugOn(!sbinfo);
17481 +       si_write_lock(sb, AuLock_NOPLMW);
17482 +
17483 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17484 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17485 +               case FS_MOVED_FROM:
17486 +               case FS_MOVED_TO:
17487 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17488 +                               "for the direct rename(2)\n");
17489 +               }
17490 +
17491 +       ii_read_lock_parent(a->dir);
17492 +       bfound = -1;
17493 +       bbot = au_ibbot(a->dir);
17494 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17495 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17496 +                       bfound = bindex;
17497 +                       break;
17498 +               }
17499 +       ii_read_unlock(a->dir);
17500 +       if (unlikely(bfound < 0))
17501 +               goto out;
17502 +
17503 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17504 +       h_ino = 0;
17505 +       if (a->h_child_inode)
17506 +               h_ino = a->h_child_inode->i_ino;
17507 +
17508 +       if (a->h_child_nlen
17509 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17510 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17511 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17512 +                                             a->dir);
17513 +       try_iput = 0;
17514 +       if (dentry && d_really_is_positive(dentry))
17515 +               inode = d_inode(dentry);
17516 +       if (xino && !inode && h_ino
17517 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17518 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17519 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17520 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17521 +               try_iput = 1;
17522 +       }
17523 +
17524 +       args.flags = a->flags[AuHn_CHILD];
17525 +       args.dentry = dentry;
17526 +       args.inode = inode;
17527 +       args.h_inode = a->h_child_inode;
17528 +       args.dir = a->dir;
17529 +       args.h_dir = a->h_dir;
17530 +       args.h_name = a->h_child_name;
17531 +       args.h_nlen = a->h_child_nlen;
17532 +       err = hn_job(&args);
17533 +       if (dentry) {
17534 +               if (au_di(dentry))
17535 +                       di_write_unlock(dentry);
17536 +               dput(dentry);
17537 +       }
17538 +       if (inode && try_iput) {
17539 +               ii_write_unlock(inode);
17540 +               iput(inode);
17541 +       }
17542 +
17543 +       ii_write_lock_parent(a->dir);
17544 +       args.flags = a->flags[AuHn_PARENT];
17545 +       args.dentry = NULL;
17546 +       args.inode = a->dir;
17547 +       args.h_inode = a->h_dir;
17548 +       args.dir = NULL;
17549 +       args.h_dir = NULL;
17550 +       args.h_name = NULL;
17551 +       args.h_nlen = 0;
17552 +       err = hn_job(&args);
17553 +       ii_write_unlock(a->dir);
17554 +
17555 +out:
17556 +       iput(a->h_child_inode);
17557 +       iput(a->h_dir);
17558 +       iput(a->dir);
17559 +       si_write_unlock(sb);
17560 +       au_nwt_done(&sbinfo->si_nowait);
17561 +       au_kfree_rcu(a);
17562 +}
17563 +
17564 +/* ---------------------------------------------------------------------- */
17565 +
17566 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17567 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17568 +{
17569 +       int err, len;
17570 +       unsigned int flags[AuHnLast], f;
17571 +       unsigned char isdir, isroot, wh;
17572 +       struct inode *dir;
17573 +       struct au_hnotify_args *args;
17574 +       char *p, *h_child_name;
17575 +
17576 +       err = 0;
17577 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17578 +       dir = igrab(hnotify->hn_aufs_inode);
17579 +       if (!dir)
17580 +               goto out;
17581 +
17582 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17583 +       wh = 0;
17584 +       h_child_name = (void *)h_child_qstr->name;
17585 +       len = h_child_qstr->len;
17586 +       if (h_child_name) {
17587 +               if (len > AUFS_WH_PFX_LEN
17588 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17589 +                       h_child_name += AUFS_WH_PFX_LEN;
17590 +                       len -= AUFS_WH_PFX_LEN;
17591 +                       wh = 1;
17592 +               }
17593 +       }
17594 +
17595 +       isdir = 0;
17596 +       if (h_child_inode)
17597 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17598 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17599 +       flags[AuHn_CHILD] = 0;
17600 +       if (isdir)
17601 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17602 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17603 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17604 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17605 +       case FS_MOVED_FROM:
17606 +       case FS_MOVED_TO:
17607 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17608 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17609 +               fallthrough;
17610 +       case FS_CREATE:
17611 +               AuDebugOn(!h_child_name);
17612 +               break;
17613 +
17614 +       case FS_DELETE:
17615 +               /*
17616 +                * aufs never be able to get this child inode.
17617 +                * revalidation should be in d_revalidate()
17618 +                * by checking i_nlink, i_generation or d_unhashed().
17619 +                */
17620 +               AuDebugOn(!h_child_name);
17621 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17622 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17623 +               break;
17624 +
17625 +       default:
17626 +               AuDebugOn(1);
17627 +       }
17628 +
17629 +       if (wh)
17630 +               h_child_inode = NULL;
17631 +
17632 +       err = -ENOMEM;
17633 +       /* iput() and kfree() will be called in au_hnotify() */
17634 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17635 +       if (unlikely(!args)) {
17636 +               AuErr1("no memory\n");
17637 +               iput(dir);
17638 +               goto out;
17639 +       }
17640 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17641 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17642 +       args->mask = mask;
17643 +       args->dir = dir;
17644 +       args->h_dir = igrab(h_dir);
17645 +       if (h_child_inode)
17646 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17647 +       args->h_child_inode = h_child_inode;
17648 +       args->h_child_nlen = len;
17649 +       if (len) {
17650 +               p = (void *)args;
17651 +               p += sizeof(*args);
17652 +               memcpy(p, h_child_name, len);
17653 +               p[len] = 0;
17654 +       }
17655 +
17656 +       /* NFS fires the event for silly-renamed one from kworker */
17657 +       f = 0;
17658 +       if (!dir->i_nlink
17659 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17660 +               f = AuWkq_NEST;
17661 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17662 +       if (unlikely(err)) {
17663 +               pr_err("wkq %d\n", err);
17664 +               iput(args->h_child_inode);
17665 +               iput(args->h_dir);
17666 +               iput(args->dir);
17667 +               au_kfree_rcu(args);
17668 +       }
17669 +
17670 +out:
17671 +       return err;
17672 +}
17673 +
17674 +/* ---------------------------------------------------------------------- */
17675 +
17676 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17677 +{
17678 +       int err;
17679 +
17680 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17681 +
17682 +       err = 0;
17683 +       if (au_hnotify_op.reset_br)
17684 +               err = au_hnotify_op.reset_br(udba, br, perm);
17685 +
17686 +       return err;
17687 +}
17688 +
17689 +int au_hnotify_init_br(struct au_branch *br, int perm)
17690 +{
17691 +       int err;
17692 +
17693 +       err = 0;
17694 +       if (au_hnotify_op.init_br)
17695 +               err = au_hnotify_op.init_br(br, perm);
17696 +
17697 +       return err;
17698 +}
17699 +
17700 +void au_hnotify_fin_br(struct au_branch *br)
17701 +{
17702 +       if (au_hnotify_op.fin_br)
17703 +               au_hnotify_op.fin_br(br);
17704 +}
17705 +
17706 +static void au_hn_destroy_cache(void)
17707 +{
17708 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17709 +       au_cache[AuCache_HNOTIFY] = NULL;
17710 +}
17711 +
17712 +int __init au_hnotify_init(void)
17713 +{
17714 +       int err;
17715 +
17716 +       err = -ENOMEM;
17717 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17718 +       if (au_cache[AuCache_HNOTIFY]) {
17719 +               err = 0;
17720 +               if (au_hnotify_op.init)
17721 +                       err = au_hnotify_op.init();
17722 +               if (unlikely(err))
17723 +                       au_hn_destroy_cache();
17724 +       }
17725 +       AuTraceErr(err);
17726 +       return err;
17727 +}
17728 +
17729 +void au_hnotify_fin(void)
17730 +{
17731 +       if (au_hnotify_op.fin)
17732 +               au_hnotify_op.fin();
17733 +
17734 +       /* cf. au_cache_fin() */
17735 +       if (au_cache[AuCache_HNOTIFY])
17736 +               au_hn_destroy_cache();
17737 +}
17738 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17739 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17740 +++ linux/fs/aufs/iinfo.c       2021-11-01 23:48:34.209692595 +0100
17741 @@ -0,0 +1,286 @@
17742 +// SPDX-License-Identifier: GPL-2.0
17743 +/*
17744 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17745 + *
17746 + * This program, aufs is free software; you can redistribute it and/or modify
17747 + * it under the terms of the GNU General Public License as published by
17748 + * the Free Software Foundation; either version 2 of the License, or
17749 + * (at your option) any later version.
17750 + *
17751 + * This program is distributed in the hope that it will be useful,
17752 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17753 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17754 + * GNU General Public License for more details.
17755 + *
17756 + * You should have received a copy of the GNU General Public License
17757 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17758 + */
17759 +
17760 +/*
17761 + * inode private data
17762 + */
17763 +
17764 +#include "aufs.h"
17765 +
17766 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17767 +{
17768 +       struct inode *h_inode;
17769 +       struct au_hinode *hinode;
17770 +
17771 +       IiMustAnyLock(inode);
17772 +
17773 +       hinode = au_hinode(au_ii(inode), bindex);
17774 +       h_inode = hinode->hi_inode;
17775 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17776 +       return h_inode;
17777 +}
17778 +
17779 +/* todo: hard/soft set? */
17780 +void au_hiput(struct au_hinode *hinode)
17781 +{
17782 +       au_hn_free(hinode);
17783 +       dput(hinode->hi_whdentry);
17784 +       iput(hinode->hi_inode);
17785 +}
17786 +
17787 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17788 +{
17789 +       unsigned int flags;
17790 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17791 +
17792 +       flags = 0;
17793 +       if (au_opt_test(mnt_flags, XINO))
17794 +               au_fset_hi(flags, XINO);
17795 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17796 +               au_fset_hi(flags, HNOTIFY);
17797 +       return flags;
17798 +}
17799 +
17800 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17801 +                  struct inode *h_inode, unsigned int flags)
17802 +{
17803 +       struct au_hinode *hinode;
17804 +       struct inode *hi;
17805 +       struct au_iinfo *iinfo = au_ii(inode);
17806 +
17807 +       IiMustWriteLock(inode);
17808 +
17809 +       hinode = au_hinode(iinfo, bindex);
17810 +       hi = hinode->hi_inode;
17811 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17812 +
17813 +       if (hi)
17814 +               au_hiput(hinode);
17815 +       hinode->hi_inode = h_inode;
17816 +       if (h_inode) {
17817 +               int err;
17818 +               struct super_block *sb = inode->i_sb;
17819 +               struct au_branch *br;
17820 +
17821 +               AuDebugOn(inode->i_mode
17822 +                         && (h_inode->i_mode & S_IFMT)
17823 +                         != (inode->i_mode & S_IFMT));
17824 +               if (bindex == iinfo->ii_btop)
17825 +                       au_cpup_igen(inode, h_inode);
17826 +               br = au_sbr(sb, bindex);
17827 +               hinode->hi_id = br->br_id;
17828 +               if (au_ftest_hi(flags, XINO)) {
17829 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17830 +                                           inode->i_ino);
17831 +                       if (unlikely(err))
17832 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17833 +               }
17834 +
17835 +               if (au_ftest_hi(flags, HNOTIFY)
17836 +                   && au_br_hnotifyable(br->br_perm)) {
17837 +                       err = au_hn_alloc(hinode, inode);
17838 +                       if (unlikely(err))
17839 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17840 +               }
17841 +       }
17842 +}
17843 +
17844 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17845 +                 struct dentry *h_wh)
17846 +{
17847 +       struct au_hinode *hinode;
17848 +
17849 +       IiMustWriteLock(inode);
17850 +
17851 +       hinode = au_hinode(au_ii(inode), bindex);
17852 +       AuDebugOn(hinode->hi_whdentry);
17853 +       hinode->hi_whdentry = h_wh;
17854 +}
17855 +
17856 +void au_update_iigen(struct inode *inode, int half)
17857 +{
17858 +       struct au_iinfo *iinfo;
17859 +       struct au_iigen *iigen;
17860 +       unsigned int sigen;
17861 +
17862 +       sigen = au_sigen(inode->i_sb);
17863 +       iinfo = au_ii(inode);
17864 +       iigen = &iinfo->ii_generation;
17865 +       spin_lock(&iigen->ig_spin);
17866 +       iigen->ig_generation = sigen;
17867 +       if (half)
17868 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17869 +       else
17870 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17871 +       spin_unlock(&iigen->ig_spin);
17872 +}
17873 +
17874 +/* it may be called at remount time, too */
17875 +void au_update_ibrange(struct inode *inode, int do_put_zero)
17876 +{
17877 +       struct au_iinfo *iinfo;
17878 +       aufs_bindex_t bindex, bbot;
17879 +
17880 +       AuDebugOn(au_is_bad_inode(inode));
17881 +       IiMustWriteLock(inode);
17882 +
17883 +       iinfo = au_ii(inode);
17884 +       if (do_put_zero && iinfo->ii_btop >= 0) {
17885 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17886 +                    bindex++) {
17887 +                       struct inode *h_i;
17888 +
17889 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
17890 +                       if (h_i
17891 +                           && !h_i->i_nlink
17892 +                           && !(h_i->i_state & I_LINKABLE))
17893 +                               au_set_h_iptr(inode, bindex, NULL, 0);
17894 +               }
17895 +       }
17896 +
17897 +       iinfo->ii_btop = -1;
17898 +       iinfo->ii_bbot = -1;
17899 +       bbot = au_sbbot(inode->i_sb);
17900 +       for (bindex = 0; bindex <= bbot; bindex++)
17901 +               if (au_hinode(iinfo, bindex)->hi_inode) {
17902 +                       iinfo->ii_btop = bindex;
17903 +                       break;
17904 +               }
17905 +       if (iinfo->ii_btop >= 0)
17906 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17907 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
17908 +                               iinfo->ii_bbot = bindex;
17909 +                               break;
17910 +                       }
17911 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17912 +}
17913 +
17914 +/* ---------------------------------------------------------------------- */
17915 +
17916 +void au_icntnr_init_once(void *_c)
17917 +{
17918 +       struct au_icntnr *c = _c;
17919 +       struct au_iinfo *iinfo = &c->iinfo;
17920 +
17921 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
17922 +       au_rw_init(&iinfo->ii_rwsem);
17923 +       inode_init_once(&c->vfs_inode);
17924 +}
17925 +
17926 +void au_hinode_init(struct au_hinode *hinode)
17927 +{
17928 +       hinode->hi_inode = NULL;
17929 +       hinode->hi_id = -1;
17930 +       au_hn_init(hinode);
17931 +       hinode->hi_whdentry = NULL;
17932 +}
17933 +
17934 +int au_iinfo_init(struct inode *inode)
17935 +{
17936 +       struct au_iinfo *iinfo;
17937 +       struct super_block *sb;
17938 +       struct au_hinode *hi;
17939 +       int nbr, i;
17940 +
17941 +       sb = inode->i_sb;
17942 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17943 +       nbr = au_sbbot(sb) + 1;
17944 +       if (unlikely(nbr <= 0))
17945 +               nbr = 1;
17946 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17947 +       if (hi) {
17948 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17949 +
17950 +               iinfo->ii_hinode = hi;
17951 +               for (i = 0; i < nbr; i++, hi++)
17952 +                       au_hinode_init(hi);
17953 +
17954 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
17955 +               iinfo->ii_btop = -1;
17956 +               iinfo->ii_bbot = -1;
17957 +               iinfo->ii_vdir = NULL;
17958 +               return 0;
17959 +       }
17960 +       return -ENOMEM;
17961 +}
17962 +
17963 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17964 +{
17965 +       int err, i;
17966 +       struct au_hinode *hip;
17967 +
17968 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
17969 +
17970 +       err = -ENOMEM;
17971 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17972 +                         may_shrink);
17973 +       if (hip) {
17974 +               iinfo->ii_hinode = hip;
17975 +               i = iinfo->ii_bbot + 1;
17976 +               hip += i;
17977 +               for (; i < nbr; i++, hip++)
17978 +                       au_hinode_init(hip);
17979 +               err = 0;
17980 +       }
17981 +
17982 +       return err;
17983 +}
17984 +
17985 +void au_iinfo_fin(struct inode *inode)
17986 +{
17987 +       struct au_iinfo *iinfo;
17988 +       struct au_hinode *hi;
17989 +       struct super_block *sb;
17990 +       aufs_bindex_t bindex, bbot;
17991 +       const unsigned char unlinked = !inode->i_nlink;
17992 +
17993 +       AuDebugOn(au_is_bad_inode(inode));
17994 +
17995 +       sb = inode->i_sb;
17996 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
17997 +       if (si_pid_test(sb))
17998 +               au_xino_delete_inode(inode, unlinked);
17999 +       else {
18000 +               /*
18001 +                * it is safe to hide the dependency between sbinfo and
18002 +                * sb->s_umount.
18003 +                */
18004 +               lockdep_off();
18005 +               si_noflush_read_lock(sb);
18006 +               au_xino_delete_inode(inode, unlinked);
18007 +               si_read_unlock(sb);
18008 +               lockdep_on();
18009 +       }
18010 +
18011 +       iinfo = au_ii(inode);
18012 +       if (iinfo->ii_vdir)
18013 +               au_vdir_free(iinfo->ii_vdir);
18014 +
18015 +       bindex = iinfo->ii_btop;
18016 +       if (bindex >= 0) {
18017 +               hi = au_hinode(iinfo, bindex);
18018 +               bbot = iinfo->ii_bbot;
18019 +               while (bindex++ <= bbot) {
18020 +                       if (hi->hi_inode)
18021 +                               au_hiput(hi);
18022 +                       hi++;
18023 +               }
18024 +       }
18025 +       au_kfree_rcu(iinfo->ii_hinode);
18026 +       AuRwDestroy(&iinfo->ii_rwsem);
18027 +}
18028 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18029 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18030 +++ linux/fs/aufs/inode.c       2021-11-01 23:48:34.209692595 +0100
18031 @@ -0,0 +1,531 @@
18032 +// SPDX-License-Identifier: GPL-2.0
18033 +/*
18034 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18035 + *
18036 + * This program, aufs is free software; you can redistribute it and/or modify
18037 + * it under the terms of the GNU General Public License as published by
18038 + * the Free Software Foundation; either version 2 of the License, or
18039 + * (at your option) any later version.
18040 + *
18041 + * This program is distributed in the hope that it will be useful,
18042 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18043 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18044 + * GNU General Public License for more details.
18045 + *
18046 + * You should have received a copy of the GNU General Public License
18047 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18048 + */
18049 +
18050 +/*
18051 + * inode functions
18052 + */
18053 +
18054 +#include <linux/iversion.h>
18055 +#include "aufs.h"
18056 +
18057 +struct inode *au_igrab(struct inode *inode)
18058 +{
18059 +       if (inode) {
18060 +               AuDebugOn(!atomic_read(&inode->i_count));
18061 +               ihold(inode);
18062 +       }
18063 +       return inode;
18064 +}
18065 +
18066 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18067 +{
18068 +       au_cpup_attr_all(inode, /*force*/0);
18069 +       au_update_iigen(inode, /*half*/1);
18070 +       if (do_version)
18071 +               inode_inc_iversion(inode);
18072 +}
18073 +
18074 +static int au_ii_refresh(struct inode *inode, int *update)
18075 +{
18076 +       int err, e, nbr;
18077 +       umode_t type;
18078 +       aufs_bindex_t bindex, new_bindex;
18079 +       struct super_block *sb;
18080 +       struct au_iinfo *iinfo;
18081 +       struct au_hinode *p, *q, tmp;
18082 +
18083 +       AuDebugOn(au_is_bad_inode(inode));
18084 +       IiMustWriteLock(inode);
18085 +
18086 +       *update = 0;
18087 +       sb = inode->i_sb;
18088 +       nbr = au_sbbot(sb) + 1;
18089 +       type = inode->i_mode & S_IFMT;
18090 +       iinfo = au_ii(inode);
18091 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18092 +       if (unlikely(err))
18093 +               goto out;
18094 +
18095 +       AuDebugOn(iinfo->ii_btop < 0);
18096 +       p = au_hinode(iinfo, iinfo->ii_btop);
18097 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18098 +            bindex++, p++) {
18099 +               if (!p->hi_inode)
18100 +                       continue;
18101 +
18102 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18103 +               new_bindex = au_br_index(sb, p->hi_id);
18104 +               if (new_bindex == bindex)
18105 +                       continue;
18106 +
18107 +               if (new_bindex < 0) {
18108 +                       *update = 1;
18109 +                       au_hiput(p);
18110 +                       p->hi_inode = NULL;
18111 +                       continue;
18112 +               }
18113 +
18114 +               if (new_bindex < iinfo->ii_btop)
18115 +                       iinfo->ii_btop = new_bindex;
18116 +               if (iinfo->ii_bbot < new_bindex)
18117 +                       iinfo->ii_bbot = new_bindex;
18118 +               /* swap two lower inode, and loop again */
18119 +               q = au_hinode(iinfo, new_bindex);
18120 +               tmp = *q;
18121 +               *q = *p;
18122 +               *p = tmp;
18123 +               if (tmp.hi_inode) {
18124 +                       bindex--;
18125 +                       p--;
18126 +               }
18127 +       }
18128 +       au_update_ibrange(inode, /*do_put_zero*/0);
18129 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18130 +       e = au_dy_irefresh(inode);
18131 +       if (unlikely(e && !err))
18132 +               err = e;
18133 +
18134 +out:
18135 +       AuTraceErr(err);
18136 +       return err;
18137 +}
18138 +
18139 +void au_refresh_iop(struct inode *inode, int force_getattr)
18140 +{
18141 +       int type;
18142 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18143 +       const struct inode_operations *iop
18144 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18145 +
18146 +       if (inode->i_op == iop)
18147 +               return;
18148 +
18149 +       switch (inode->i_mode & S_IFMT) {
18150 +       case S_IFDIR:
18151 +               type = AuIop_DIR;
18152 +               break;
18153 +       case S_IFLNK:
18154 +               type = AuIop_SYMLINK;
18155 +               break;
18156 +       default:
18157 +               type = AuIop_OTHER;
18158 +               break;
18159 +       }
18160 +
18161 +       inode->i_op = iop + type;
18162 +       /* unnecessary smp_wmb() */
18163 +}
18164 +
18165 +int au_refresh_hinode_self(struct inode *inode)
18166 +{
18167 +       int err, update;
18168 +
18169 +       err = au_ii_refresh(inode, &update);
18170 +       if (!err)
18171 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18172 +
18173 +       AuTraceErr(err);
18174 +       return err;
18175 +}
18176 +
18177 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18178 +{
18179 +       int err, e, update;
18180 +       unsigned int flags;
18181 +       umode_t mode;
18182 +       aufs_bindex_t bindex, bbot;
18183 +       unsigned char isdir;
18184 +       struct au_hinode *p;
18185 +       struct au_iinfo *iinfo;
18186 +
18187 +       err = au_ii_refresh(inode, &update);
18188 +       if (unlikely(err))
18189 +               goto out;
18190 +
18191 +       update = 0;
18192 +       iinfo = au_ii(inode);
18193 +       p = au_hinode(iinfo, iinfo->ii_btop);
18194 +       mode = (inode->i_mode & S_IFMT);
18195 +       isdir = S_ISDIR(mode);
18196 +       flags = au_hi_flags(inode, isdir);
18197 +       bbot = au_dbbot(dentry);
18198 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18199 +               struct inode *h_i, *h_inode;
18200 +               struct dentry *h_d;
18201 +
18202 +               h_d = au_h_dptr(dentry, bindex);
18203 +               if (!h_d || d_is_negative(h_d))
18204 +                       continue;
18205 +
18206 +               h_inode = d_inode(h_d);
18207 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18208 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18209 +                       h_i = au_h_iptr(inode, bindex);
18210 +                       if (h_i) {
18211 +                               if (h_i == h_inode)
18212 +                                       continue;
18213 +                               err = -EIO;
18214 +                               break;
18215 +                       }
18216 +               }
18217 +               if (bindex < iinfo->ii_btop)
18218 +                       iinfo->ii_btop = bindex;
18219 +               if (iinfo->ii_bbot < bindex)
18220 +                       iinfo->ii_bbot = bindex;
18221 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18222 +               update = 1;
18223 +       }
18224 +       au_update_ibrange(inode, /*do_put_zero*/0);
18225 +       e = au_dy_irefresh(inode);
18226 +       if (unlikely(e && !err))
18227 +               err = e;
18228 +       if (!err)
18229 +               au_refresh_hinode_attr(inode, update && isdir);
18230 +
18231 +out:
18232 +       AuTraceErr(err);
18233 +       return err;
18234 +}
18235 +
18236 +static int set_inode(struct inode *inode, struct dentry *dentry)
18237 +{
18238 +       int err;
18239 +       unsigned int flags;
18240 +       umode_t mode;
18241 +       aufs_bindex_t bindex, btop, btail;
18242 +       unsigned char isdir;
18243 +       struct dentry *h_dentry;
18244 +       struct inode *h_inode;
18245 +       struct au_iinfo *iinfo;
18246 +       const struct inode_operations *iop;
18247 +
18248 +       IiMustWriteLock(inode);
18249 +
18250 +       err = 0;
18251 +       isdir = 0;
18252 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18253 +       btop = au_dbtop(dentry);
18254 +       h_dentry = au_h_dptr(dentry, btop);
18255 +       h_inode = d_inode(h_dentry);
18256 +       mode = h_inode->i_mode;
18257 +       switch (mode & S_IFMT) {
18258 +       case S_IFREG:
18259 +               btail = au_dbtail(dentry);
18260 +               inode->i_op = iop + AuIop_OTHER;
18261 +               inode->i_fop = &aufs_file_fop;
18262 +               err = au_dy_iaop(inode, btop, h_inode);
18263 +               if (unlikely(err))
18264 +                       goto out;
18265 +               break;
18266 +       case S_IFDIR:
18267 +               isdir = 1;
18268 +               btail = au_dbtaildir(dentry);
18269 +               inode->i_op = iop + AuIop_DIR;
18270 +               inode->i_fop = &aufs_dir_fop;
18271 +               break;
18272 +       case S_IFLNK:
18273 +               btail = au_dbtail(dentry);
18274 +               inode->i_op = iop + AuIop_SYMLINK;
18275 +               break;
18276 +       case S_IFBLK:
18277 +       case S_IFCHR:
18278 +       case S_IFIFO:
18279 +       case S_IFSOCK:
18280 +               btail = au_dbtail(dentry);
18281 +               inode->i_op = iop + AuIop_OTHER;
18282 +               init_special_inode(inode, mode, h_inode->i_rdev);
18283 +               break;
18284 +       default:
18285 +               AuIOErr("Unknown file type 0%o\n", mode);
18286 +               err = -EIO;
18287 +               goto out;
18288 +       }
18289 +
18290 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18291 +       flags = au_hi_flags(inode, isdir);
18292 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18293 +           && au_ftest_hi(flags, HNOTIFY)
18294 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18295 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18296 +               au_fclr_hi(flags, HNOTIFY);
18297 +       iinfo = au_ii(inode);
18298 +       iinfo->ii_btop = btop;
18299 +       iinfo->ii_bbot = btail;
18300 +       for (bindex = btop; bindex <= btail; bindex++) {
18301 +               h_dentry = au_h_dptr(dentry, bindex);
18302 +               if (h_dentry)
18303 +                       au_set_h_iptr(inode, bindex,
18304 +                                     au_igrab(d_inode(h_dentry)), flags);
18305 +       }
18306 +       au_cpup_attr_all(inode, /*force*/1);
18307 +       /*
18308 +        * to force calling aufs_get_acl() every time,
18309 +        * do not call cache_no_acl() for aufs inode.
18310 +        */
18311 +
18312 +out:
18313 +       return err;
18314 +}
18315 +
18316 +/*
18317 + * successful returns with iinfo write_locked
18318 + * minus: errno
18319 + * zero: success, matched
18320 + * plus: no error, but unmatched
18321 + */
18322 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18323 +{
18324 +       int err;
18325 +       unsigned int gen, igflags;
18326 +       aufs_bindex_t bindex, bbot;
18327 +       struct inode *h_inode, *h_dinode;
18328 +       struct dentry *h_dentry;
18329 +
18330 +       /*
18331 +        * before this function, if aufs got any iinfo lock, it must be only
18332 +        * one, the parent dir.
18333 +        * it can happen by UDBA and the obsoleted inode number.
18334 +        */
18335 +       err = -EIO;
18336 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18337 +               goto out;
18338 +
18339 +       err = 1;
18340 +       ii_write_lock_new_child(inode);
18341 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18342 +       h_dinode = d_inode(h_dentry);
18343 +       bbot = au_ibbot(inode);
18344 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18345 +               h_inode = au_h_iptr(inode, bindex);
18346 +               if (!h_inode || h_inode != h_dinode)
18347 +                       continue;
18348 +
18349 +               err = 0;
18350 +               gen = au_iigen(inode, &igflags);
18351 +               if (gen == au_digen(dentry)
18352 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18353 +                       break;
18354 +
18355 +               /* fully refresh inode using dentry */
18356 +               err = au_refresh_hinode(inode, dentry);
18357 +               if (!err)
18358 +                       au_update_iigen(inode, /*half*/0);
18359 +               break;
18360 +       }
18361 +
18362 +       if (unlikely(err))
18363 +               ii_write_unlock(inode);
18364 +out:
18365 +       return err;
18366 +}
18367 +
18368 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18369 +          unsigned int d_type, ino_t *ino)
18370 +{
18371 +       int err, idx;
18372 +       const int isnondir = d_type != DT_DIR;
18373 +
18374 +       /* prevent hardlinked inode number from race condition */
18375 +       if (isnondir) {
18376 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18377 +               if (unlikely(err))
18378 +                       goto out;
18379 +       }
18380 +
18381 +       err = au_xino_read(sb, bindex, h_ino, ino);
18382 +       if (unlikely(err))
18383 +               goto out_xinondir;
18384 +
18385 +       if (!*ino) {
18386 +               err = -EIO;
18387 +               *ino = au_xino_new_ino(sb);
18388 +               if (unlikely(!*ino))
18389 +                       goto out_xinondir;
18390 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18391 +               if (unlikely(err))
18392 +                       goto out_xinondir;
18393 +       }
18394 +
18395 +out_xinondir:
18396 +       if (isnondir && idx >= 0)
18397 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18398 +out:
18399 +       return err;
18400 +}
18401 +
18402 +/* successful returns with iinfo write_locked */
18403 +/* todo: return with unlocked? */
18404 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18405 +{
18406 +       struct inode *inode, *h_inode;
18407 +       struct dentry *h_dentry;
18408 +       struct super_block *sb;
18409 +       ino_t h_ino, ino;
18410 +       int err, idx, hlinked;
18411 +       aufs_bindex_t btop;
18412 +
18413 +       sb = dentry->d_sb;
18414 +       btop = au_dbtop(dentry);
18415 +       h_dentry = au_h_dptr(dentry, btop);
18416 +       h_inode = d_inode(h_dentry);
18417 +       h_ino = h_inode->i_ino;
18418 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18419 +
18420 +new_ino:
18421 +       /*
18422 +        * stop 'race'-ing between hardlinks under different
18423 +        * parents.
18424 +        */
18425 +       if (hlinked) {
18426 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18427 +               inode = ERR_PTR(err);
18428 +               if (unlikely(err))
18429 +                       goto out;
18430 +       }
18431 +
18432 +       err = au_xino_read(sb, btop, h_ino, &ino);
18433 +       inode = ERR_PTR(err);
18434 +       if (unlikely(err))
18435 +               goto out_xinondir;
18436 +
18437 +       if (!ino) {
18438 +               ino = au_xino_new_ino(sb);
18439 +               if (unlikely(!ino)) {
18440 +                       inode = ERR_PTR(-EIO);
18441 +                       goto out_xinondir;
18442 +               }
18443 +       }
18444 +
18445 +       AuDbg("i%lu\n", (unsigned long)ino);
18446 +       inode = au_iget_locked(sb, ino);
18447 +       err = PTR_ERR(inode);
18448 +       if (IS_ERR(inode))
18449 +               goto out_xinondir;
18450 +
18451 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18452 +       if (inode->i_state & I_NEW) {
18453 +               ii_write_lock_new_child(inode);
18454 +               err = set_inode(inode, dentry);
18455 +               if (!err) {
18456 +                       unlock_new_inode(inode);
18457 +                       goto out_xinondir; /* success */
18458 +               }
18459 +
18460 +               /*
18461 +                * iget_failed() calls iput(), but we need to call
18462 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18463 +                * i_count.
18464 +                */
18465 +               atomic_inc(&inode->i_count);
18466 +               iget_failed(inode);
18467 +               ii_write_unlock(inode);
18468 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18469 +               /* ignore this error */
18470 +               goto out_iput;
18471 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18472 +               /*
18473 +                * horrible race condition between lookup, readdir and copyup
18474 +                * (or something).
18475 +                */
18476 +               if (hlinked && idx >= 0)
18477 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18478 +               err = reval_inode(inode, dentry);
18479 +               if (unlikely(err < 0)) {
18480 +                       hlinked = 0;
18481 +                       goto out_iput;
18482 +               }
18483 +               if (!err)
18484 +                       goto out; /* success */
18485 +               else if (hlinked && idx >= 0) {
18486 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18487 +                       if (unlikely(err)) {
18488 +                               iput(inode);
18489 +                               inode = ERR_PTR(err);
18490 +                               goto out;
18491 +                       }
18492 +               }
18493 +       }
18494 +
18495 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18496 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18497 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18498 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18499 +                       (unsigned long)h_ino, (unsigned long)ino);
18500 +       ino = 0;
18501 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18502 +       if (!err) {
18503 +               iput(inode);
18504 +               if (hlinked && idx >= 0)
18505 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18506 +               goto new_ino;
18507 +       }
18508 +
18509 +out_iput:
18510 +       iput(inode);
18511 +       inode = ERR_PTR(err);
18512 +out_xinondir:
18513 +       if (hlinked && idx >= 0)
18514 +               au_xinondir_leave(sb, btop, h_ino, idx);
18515 +out:
18516 +       return inode;
18517 +}
18518 +
18519 +/* ---------------------------------------------------------------------- */
18520 +
18521 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18522 +              struct inode *inode)
18523 +{
18524 +       int err;
18525 +       struct inode *hi;
18526 +
18527 +       err = au_br_rdonly(au_sbr(sb, bindex));
18528 +
18529 +       /* pseudo-link after flushed may happen out of bounds */
18530 +       if (!err
18531 +           && inode
18532 +           && au_ibtop(inode) <= bindex
18533 +           && bindex <= au_ibbot(inode)) {
18534 +               /*
18535 +                * permission check is unnecessary since vfsub routine
18536 +                * will be called later
18537 +                */
18538 +               hi = au_h_iptr(inode, bindex);
18539 +               if (hi)
18540 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18541 +       }
18542 +
18543 +       return err;
18544 +}
18545 +
18546 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18547 +                  int mask)
18548 +{
18549 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18550 +               return 0;
18551 +       return inode_permission(h_userns, h_inode, mask);
18552 +}
18553 +
18554 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18555 +                      int mask)
18556 +{
18557 +       if (au_test_nfs(h_inode->i_sb)
18558 +           && (mask & MAY_WRITE)
18559 +           && S_ISDIR(h_inode->i_mode))
18560 +               mask |= MAY_READ; /* force permission check */
18561 +       return au_test_h_perm(h_userns, h_inode, mask);
18562 +}
18563 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18564 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18565 +++ linux/fs/aufs/inode.h       2021-11-01 23:48:34.209692595 +0100
18566 @@ -0,0 +1,705 @@
18567 +/* SPDX-License-Identifier: GPL-2.0 */
18568 +/*
18569 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18570 + *
18571 + * This program, aufs is free software; you can redistribute it and/or modify
18572 + * it under the terms of the GNU General Public License as published by
18573 + * the Free Software Foundation; either version 2 of the License, or
18574 + * (at your option) any later version.
18575 + *
18576 + * This program is distributed in the hope that it will be useful,
18577 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18578 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18579 + * GNU General Public License for more details.
18580 + *
18581 + * You should have received a copy of the GNU General Public License
18582 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18583 + */
18584 +
18585 +/*
18586 + * inode operations
18587 + */
18588 +
18589 +#ifndef __AUFS_INODE_H__
18590 +#define __AUFS_INODE_H__
18591 +
18592 +#ifdef __KERNEL__
18593 +
18594 +#include <linux/fsnotify.h>
18595 +#include "rwsem.h"
18596 +
18597 +struct vfsmount;
18598 +
18599 +struct au_hnotify {
18600 +#ifdef CONFIG_AUFS_HNOTIFY
18601 +#ifdef CONFIG_AUFS_HFSNOTIFY
18602 +       /* never use fsnotify_add_vfsmount_mark() */
18603 +       struct fsnotify_mark            hn_mark;
18604 +#endif
18605 +       struct inode            *hn_aufs_inode; /* no get/put */
18606 +       struct rcu_head         rcu;
18607 +#endif
18608 +} ____cacheline_aligned_in_smp;
18609 +
18610 +struct au_hinode {
18611 +       struct inode            *hi_inode;
18612 +       aufs_bindex_t           hi_id;
18613 +#ifdef CONFIG_AUFS_HNOTIFY
18614 +       struct au_hnotify       *hi_notify;
18615 +#endif
18616 +
18617 +       /* reference to the copied-up whiteout with get/put */
18618 +       struct dentry           *hi_whdentry;
18619 +};
18620 +
18621 +/* ig_flags */
18622 +#define AuIG_HALF_REFRESHED            1
18623 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18624 +#define au_ig_fset(flags, name) \
18625 +       do { (flags) |= AuIG_##name; } while (0)
18626 +#define au_ig_fclr(flags, name) \
18627 +       do { (flags) &= ~AuIG_##name; } while (0)
18628 +
18629 +struct au_iigen {
18630 +       spinlock_t      ig_spin;
18631 +       __u32           ig_generation, ig_flags;
18632 +};
18633 +
18634 +struct au_vdir;
18635 +struct au_iinfo {
18636 +       struct au_iigen         ii_generation;
18637 +       struct super_block      *ii_hsb1;       /* no get/put */
18638 +
18639 +       struct au_rwsem         ii_rwsem;
18640 +       aufs_bindex_t           ii_btop, ii_bbot;
18641 +       __u32                   ii_higen;
18642 +       struct au_hinode        *ii_hinode;
18643 +       struct au_vdir          *ii_vdir;
18644 +};
18645 +
18646 +struct au_icntnr {
18647 +       struct au_iinfo         iinfo;
18648 +       struct inode            vfs_inode;
18649 +       struct hlist_bl_node    plink;
18650 +       struct rcu_head         rcu;
18651 +} ____cacheline_aligned_in_smp;
18652 +
18653 +/* au_pin flags */
18654 +#define AuPin_DI_LOCKED                1
18655 +#define AuPin_MNT_WRITE                (1 << 1)
18656 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18657 +#define au_fset_pin(flags, name) \
18658 +       do { (flags) |= AuPin_##name; } while (0)
18659 +#define au_fclr_pin(flags, name) \
18660 +       do { (flags) &= ~AuPin_##name; } while (0)
18661 +
18662 +struct au_pin {
18663 +       /* input */
18664 +       struct dentry *dentry;
18665 +       unsigned int udba;
18666 +       unsigned char lsc_di, lsc_hi, flags;
18667 +       aufs_bindex_t bindex;
18668 +
18669 +       /* output */
18670 +       struct dentry *parent;
18671 +       struct au_hinode *hdir;
18672 +       struct vfsmount *h_mnt;
18673 +
18674 +       /* temporary unlock/relock for copyup */
18675 +       struct dentry *h_dentry, *h_parent;
18676 +       struct au_branch *br;
18677 +       struct task_struct *task;
18678 +};
18679 +
18680 +void au_pin_hdir_unlock(struct au_pin *p);
18681 +int au_pin_hdir_lock(struct au_pin *p);
18682 +int au_pin_hdir_relock(struct au_pin *p);
18683 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18684 +void au_pin_hdir_release(struct au_pin *p);
18685 +
18686 +/* ---------------------------------------------------------------------- */
18687 +
18688 +static inline struct au_iinfo *au_ii(struct inode *inode)
18689 +{
18690 +       BUG_ON(is_bad_inode(inode));
18691 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18692 +}
18693 +
18694 +/* ---------------------------------------------------------------------- */
18695 +
18696 +/* inode.c */
18697 +struct inode *au_igrab(struct inode *inode);
18698 +void au_refresh_iop(struct inode *inode, int force_getattr);
18699 +int au_refresh_hinode_self(struct inode *inode);
18700 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18701 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18702 +          unsigned int d_type, ino_t *ino);
18703 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18704 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18705 +              struct inode *inode);
18706 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18707 +                  int mask);
18708 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18709 +                      int mask);
18710 +
18711 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18712 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18713 +{
18714 +#ifdef CONFIG_AUFS_SHWH
18715 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18716 +#else
18717 +       return 0;
18718 +#endif
18719 +}
18720 +
18721 +/* i_op.c */
18722 +enum {
18723 +       AuIop_SYMLINK,
18724 +       AuIop_DIR,
18725 +       AuIop_OTHER,
18726 +       AuIop_Last
18727 +};
18728 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18729 +       aufs_iop_nogetattr[AuIop_Last];
18730 +
18731 +/* au_wr_dir flags */
18732 +#define AuWrDir_ADD_ENTRY      1
18733 +#define AuWrDir_ISDIR          (1 << 1)
18734 +#define AuWrDir_TMPFILE                (1 << 2)
18735 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18736 +#define au_fset_wrdir(flags, name) \
18737 +       do { (flags) |= AuWrDir_##name; } while (0)
18738 +#define au_fclr_wrdir(flags, name) \
18739 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18740 +
18741 +struct au_wr_dir_args {
18742 +       aufs_bindex_t force_btgt;
18743 +       unsigned char flags;
18744 +};
18745 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18746 +             struct au_wr_dir_args *args);
18747 +
18748 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18749 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18750 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18751 +                unsigned int udba, unsigned char flags);
18752 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18753 +          unsigned int udba, unsigned char flags) __must_check;
18754 +int au_do_pin(struct au_pin *pin) __must_check;
18755 +void au_unpin(struct au_pin *pin);
18756 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18757 +
18758 +#define AuIcpup_DID_CPUP       1
18759 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18760 +#define au_fset_icpup(flags, name) \
18761 +       do { (flags) |= AuIcpup_##name; } while (0)
18762 +#define au_fclr_icpup(flags, name) \
18763 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18764 +
18765 +struct au_icpup_args {
18766 +       unsigned char flags;
18767 +       unsigned char pin_flags;
18768 +       aufs_bindex_t btgt;
18769 +       unsigned int udba;
18770 +       struct au_pin pin;
18771 +       struct path h_path;
18772 +       struct inode *h_inode;
18773 +};
18774 +
18775 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18776 +                    struct au_icpup_args *a);
18777 +
18778 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18779 +                     struct path *h_path, int locked);
18780 +
18781 +/* i_op_add.c */
18782 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18783 +              struct dentry *h_parent, int isdir);
18784 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
18785 +              struct dentry *dentry, umode_t mode, dev_t dev);
18786 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
18787 +                struct dentry *dentry, const char *symname);
18788 +int aufs_create(struct user_namespace *userns, struct inode *dir,
18789 +               struct dentry *dentry, umode_t mode, bool want_excl);
18790 +struct vfsub_aopen_args;
18791 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18792 +                      struct vfsub_aopen_args *args);
18793 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
18794 +                struct dentry *dentry, umode_t mode);
18795 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18796 +             struct dentry *dentry);
18797 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
18798 +              struct dentry *dentry, umode_t mode);
18799 +
18800 +/* i_op_del.c */
18801 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18802 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18803 +              struct dentry *h_parent, int isdir);
18804 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18805 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18806 +
18807 +/* i_op_ren.c */
18808 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18809 +int aufs_rename(struct user_namespace *userns,
18810 +               struct inode *_src_dir, struct dentry *_src_dentry,
18811 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
18812 +               unsigned int _flags);
18813 +
18814 +/* iinfo.c */
18815 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18816 +void au_hiput(struct au_hinode *hinode);
18817 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18818 +                 struct dentry *h_wh);
18819 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18820 +
18821 +/* hinode flags */
18822 +#define AuHi_XINO      1
18823 +#define AuHi_HNOTIFY   (1 << 1)
18824 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18825 +#define au_fset_hi(flags, name) \
18826 +       do { (flags) |= AuHi_##name; } while (0)
18827 +#define au_fclr_hi(flags, name) \
18828 +       do { (flags) &= ~AuHi_##name; } while (0)
18829 +
18830 +#ifndef CONFIG_AUFS_HNOTIFY
18831 +#undef AuHi_HNOTIFY
18832 +#define AuHi_HNOTIFY   0
18833 +#endif
18834 +
18835 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18836 +                  struct inode *h_inode, unsigned int flags);
18837 +
18838 +void au_update_iigen(struct inode *inode, int half);
18839 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18840 +
18841 +void au_icntnr_init_once(void *_c);
18842 +void au_hinode_init(struct au_hinode *hinode);
18843 +int au_iinfo_init(struct inode *inode);
18844 +void au_iinfo_fin(struct inode *inode);
18845 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18846 +
18847 +#ifdef CONFIG_PROC_FS
18848 +/* plink.c */
18849 +int au_plink_maint(struct super_block *sb, int flags);
18850 +struct au_sbinfo;
18851 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18852 +int au_plink_maint_enter(struct super_block *sb);
18853 +#ifdef CONFIG_AUFS_DEBUG
18854 +void au_plink_list(struct super_block *sb);
18855 +#else
18856 +AuStubVoid(au_plink_list, struct super_block *sb)
18857 +#endif
18858 +int au_plink_test(struct inode *inode);
18859 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18860 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18861 +                    struct dentry *h_dentry);
18862 +void au_plink_put(struct super_block *sb, int verbose);
18863 +void au_plink_clean(struct super_block *sb, int verbose);
18864 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18865 +#else
18866 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18867 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18868 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18869 +AuStubVoid(au_plink_list, struct super_block *sb);
18870 +AuStubInt0(au_plink_test, struct inode *inode);
18871 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18872 +       struct inode *inode, aufs_bindex_t bindex);
18873 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18874 +          struct dentry *h_dentry);
18875 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18876 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18877 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18878 +#endif /* CONFIG_PROC_FS */
18879 +
18880 +#ifdef CONFIG_AUFS_XATTR
18881 +/* xattr.c */
18882 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
18883 +                 unsigned int verbose);
18884 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18885 +void au_xattr_init(struct super_block *sb);
18886 +#else
18887 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
18888 +          int ignore_flags, unsigned int verbose);
18889 +AuStubVoid(au_xattr_init, struct super_block *sb);
18890 +#endif
18891 +
18892 +#ifdef CONFIG_FS_POSIX_ACL
18893 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
18894 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
18895 +                struct posix_acl *acl, int type);
18896 +#endif
18897 +
18898 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18899 +enum {
18900 +       AU_XATTR_SET,
18901 +       AU_ACL_SET
18902 +};
18903 +
18904 +struct au_sxattr {
18905 +       int type;
18906 +       union {
18907 +               struct {
18908 +                       const char      *name;
18909 +                       const void      *value;
18910 +                       size_t          size;
18911 +                       int             flags;
18912 +               } set;
18913 +               struct {
18914 +                       struct posix_acl *acl;
18915 +                       int             type;
18916 +               } acl_set;
18917 +       } u;
18918 +};
18919 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18920 +                 struct au_sxattr *arg);
18921 +#endif
18922 +
18923 +/* ---------------------------------------------------------------------- */
18924 +
18925 +/* lock subclass for iinfo */
18926 +enum {
18927 +       AuLsc_II_CHILD,         /* child first */
18928 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
18929 +       AuLsc_II_CHILD3,        /* copyup dirs */
18930 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
18931 +       AuLsc_II_PARENT2,
18932 +       AuLsc_II_PARENT3,       /* copyup dirs */
18933 +       AuLsc_II_NEW_CHILD
18934 +};
18935 +
18936 +/*
18937 + * ii_read_lock_child, ii_write_lock_child,
18938 + * ii_read_lock_child2, ii_write_lock_child2,
18939 + * ii_read_lock_child3, ii_write_lock_child3,
18940 + * ii_read_lock_parent, ii_write_lock_parent,
18941 + * ii_read_lock_parent2, ii_write_lock_parent2,
18942 + * ii_read_lock_parent3, ii_write_lock_parent3,
18943 + * ii_read_lock_new_child, ii_write_lock_new_child,
18944 + */
18945 +#define AuReadLockFunc(name, lsc) \
18946 +static inline void ii_read_lock_##name(struct inode *i) \
18947 +{ \
18948 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18949 +}
18950 +
18951 +#define AuWriteLockFunc(name, lsc) \
18952 +static inline void ii_write_lock_##name(struct inode *i) \
18953 +{ \
18954 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18955 +}
18956 +
18957 +#define AuRWLockFuncs(name, lsc) \
18958 +       AuReadLockFunc(name, lsc) \
18959 +       AuWriteLockFunc(name, lsc)
18960 +
18961 +AuRWLockFuncs(child, CHILD);
18962 +AuRWLockFuncs(child2, CHILD2);
18963 +AuRWLockFuncs(child3, CHILD3);
18964 +AuRWLockFuncs(parent, PARENT);
18965 +AuRWLockFuncs(parent2, PARENT2);
18966 +AuRWLockFuncs(parent3, PARENT3);
18967 +AuRWLockFuncs(new_child, NEW_CHILD);
18968 +
18969 +#undef AuReadLockFunc
18970 +#undef AuWriteLockFunc
18971 +#undef AuRWLockFuncs
18972 +
18973 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
18974 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
18975 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
18976 +
18977 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
18978 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
18979 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
18980 +
18981 +/* ---------------------------------------------------------------------- */
18982 +
18983 +static inline void au_icntnr_init(struct au_icntnr *c)
18984 +{
18985 +#ifdef CONFIG_AUFS_DEBUG
18986 +       c->vfs_inode.i_mode = 0;
18987 +#endif
18988 +}
18989 +
18990 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
18991 +{
18992 +       unsigned int gen;
18993 +       struct au_iinfo *iinfo;
18994 +       struct au_iigen *iigen;
18995 +
18996 +       iinfo = au_ii(inode);
18997 +       iigen = &iinfo->ii_generation;
18998 +       spin_lock(&iigen->ig_spin);
18999 +       if (igflags)
19000 +               *igflags = iigen->ig_flags;
19001 +       gen = iigen->ig_generation;
19002 +       spin_unlock(&iigen->ig_spin);
19003 +
19004 +       return gen;
19005 +}
19006 +
19007 +/* tiny test for inode number */
19008 +/* tmpfs generation is too rough */
19009 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19010 +{
19011 +       struct au_iinfo *iinfo;
19012 +
19013 +       iinfo = au_ii(inode);
19014 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19015 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19016 +                && iinfo->ii_higen == h_inode->i_generation);
19017 +}
19018 +
19019 +static inline void au_iigen_dec(struct inode *inode)
19020 +{
19021 +       struct au_iinfo *iinfo;
19022 +       struct au_iigen *iigen;
19023 +
19024 +       iinfo = au_ii(inode);
19025 +       iigen = &iinfo->ii_generation;
19026 +       spin_lock(&iigen->ig_spin);
19027 +       iigen->ig_generation--;
19028 +       spin_unlock(&iigen->ig_spin);
19029 +}
19030 +
19031 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19032 +{
19033 +       int err;
19034 +
19035 +       err = 0;
19036 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19037 +               err = -EIO;
19038 +
19039 +       return err;
19040 +}
19041 +
19042 +/* ---------------------------------------------------------------------- */
19043 +
19044 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19045 +                                         aufs_bindex_t bindex)
19046 +{
19047 +       return iinfo->ii_hinode + bindex;
19048 +}
19049 +
19050 +static inline int au_is_bad_inode(struct inode *inode)
19051 +{
19052 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19053 +}
19054 +
19055 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19056 +                                       aufs_bindex_t bindex)
19057 +{
19058 +       IiMustAnyLock(inode);
19059 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19060 +}
19061 +
19062 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19063 +{
19064 +       IiMustAnyLock(inode);
19065 +       return au_ii(inode)->ii_btop;
19066 +}
19067 +
19068 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19069 +{
19070 +       IiMustAnyLock(inode);
19071 +       return au_ii(inode)->ii_bbot;
19072 +}
19073 +
19074 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19075 +{
19076 +       IiMustAnyLock(inode);
19077 +       return au_ii(inode)->ii_vdir;
19078 +}
19079 +
19080 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19081 +{
19082 +       IiMustAnyLock(inode);
19083 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19084 +}
19085 +
19086 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19087 +{
19088 +       IiMustWriteLock(inode);
19089 +       au_ii(inode)->ii_btop = bindex;
19090 +}
19091 +
19092 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19093 +{
19094 +       IiMustWriteLock(inode);
19095 +       au_ii(inode)->ii_bbot = bindex;
19096 +}
19097 +
19098 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19099 +{
19100 +       IiMustWriteLock(inode);
19101 +       au_ii(inode)->ii_vdir = vdir;
19102 +}
19103 +
19104 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19105 +{
19106 +       IiMustAnyLock(inode);
19107 +       return au_hinode(au_ii(inode), bindex);
19108 +}
19109 +
19110 +/* ---------------------------------------------------------------------- */
19111 +
19112 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19113 +{
19114 +       if (pin)
19115 +               return pin->parent;
19116 +       return NULL;
19117 +}
19118 +
19119 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19120 +{
19121 +       if (pin && pin->hdir)
19122 +               return pin->hdir->hi_inode;
19123 +       return NULL;
19124 +}
19125 +
19126 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19127 +{
19128 +       if (pin)
19129 +               return pin->hdir;
19130 +       return NULL;
19131 +}
19132 +
19133 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19134 +{
19135 +       if (pin)
19136 +               pin->dentry = dentry;
19137 +}
19138 +
19139 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19140 +                                          unsigned char lflag)
19141 +{
19142 +       if (pin) {
19143 +               if (lflag)
19144 +                       au_fset_pin(pin->flags, DI_LOCKED);
19145 +               else
19146 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19147 +       }
19148 +}
19149 +
19150 +#if 0 /* reserved */
19151 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19152 +{
19153 +       if (pin) {
19154 +               dput(pin->parent);
19155 +               pin->parent = dget(parent);
19156 +       }
19157 +}
19158 +#endif
19159 +
19160 +/* ---------------------------------------------------------------------- */
19161 +
19162 +struct au_branch;
19163 +#ifdef CONFIG_AUFS_HNOTIFY
19164 +struct au_hnotify_op {
19165 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19166 +       int (*alloc)(struct au_hinode *hinode);
19167 +
19168 +       /*
19169 +        * if it returns true, the caller should free hinode->hi_notify,
19170 +        * otherwise ->free() frees it.
19171 +        */
19172 +       int (*free)(struct au_hinode *hinode,
19173 +                   struct au_hnotify *hn) __must_check;
19174 +
19175 +       void (*fin)(void);
19176 +       int (*init)(void);
19177 +
19178 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19179 +       void (*fin_br)(struct au_branch *br);
19180 +       int (*init_br)(struct au_branch *br, int perm);
19181 +};
19182 +
19183 +/* hnotify.c */
19184 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19185 +void au_hn_free(struct au_hinode *hinode);
19186 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19187 +void au_hn_reset(struct inode *inode, unsigned int flags);
19188 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19189 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19190 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19191 +int au_hnotify_init_br(struct au_branch *br, int perm);
19192 +void au_hnotify_fin_br(struct au_branch *br);
19193 +int __init au_hnotify_init(void);
19194 +void au_hnotify_fin(void);
19195 +
19196 +/* hfsnotify.c */
19197 +extern const struct au_hnotify_op au_hnotify_op;
19198 +
19199 +static inline
19200 +void au_hn_init(struct au_hinode *hinode)
19201 +{
19202 +       hinode->hi_notify = NULL;
19203 +}
19204 +
19205 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19206 +{
19207 +       return hinode->hi_notify;
19208 +}
19209 +
19210 +#else
19211 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19212 +       struct au_hinode *hinode __maybe_unused,
19213 +       struct inode *inode __maybe_unused)
19214 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19215 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19216 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19217 +          int do_set __maybe_unused)
19218 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19219 +          unsigned int flags __maybe_unused)
19220 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19221 +          struct au_branch *br __maybe_unused,
19222 +          int perm __maybe_unused)
19223 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19224 +          int perm __maybe_unused)
19225 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19226 +AuStubInt0(__init au_hnotify_init, void)
19227 +AuStubVoid(au_hnotify_fin, void)
19228 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19229 +#endif /* CONFIG_AUFS_HNOTIFY */
19230 +
19231 +static inline void au_hn_suspend(struct au_hinode *hdir)
19232 +{
19233 +       au_hn_ctl(hdir, /*do_set*/0);
19234 +}
19235 +
19236 +static inline void au_hn_resume(struct au_hinode *hdir)
19237 +{
19238 +       au_hn_ctl(hdir, /*do_set*/1);
19239 +}
19240 +
19241 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19242 +{
19243 +       inode_lock(hdir->hi_inode);
19244 +       au_hn_suspend(hdir);
19245 +}
19246 +
19247 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19248 +                                         unsigned int sc __maybe_unused)
19249 +{
19250 +       inode_lock_nested(hdir->hi_inode, sc);
19251 +       au_hn_suspend(hdir);
19252 +}
19253 +
19254 +#if 0 /* unused */
19255 +#include "vfsub.h"
19256 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19257 +                                                 unsigned int sc)
19258 +{
19259 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19260 +       au_hn_suspend(hdir);
19261 +}
19262 +#endif
19263 +
19264 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19265 +{
19266 +       au_hn_resume(hdir);
19267 +       inode_unlock(hdir->hi_inode);
19268 +}
19269 +
19270 +#endif /* __KERNEL__ */
19271 +#endif /* __AUFS_INODE_H__ */
19272 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19273 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19274 +++ linux/fs/aufs/ioctl.c       2021-11-01 23:48:34.209692595 +0100
19275 @@ -0,0 +1,220 @@
19276 +// SPDX-License-Identifier: GPL-2.0
19277 +/*
19278 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19279 + *
19280 + * This program, aufs is free software; you can redistribute it and/or modify
19281 + * it under the terms of the GNU General Public License as published by
19282 + * the Free Software Foundation; either version 2 of the License, or
19283 + * (at your option) any later version.
19284 + *
19285 + * This program is distributed in the hope that it will be useful,
19286 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19287 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19288 + * GNU General Public License for more details.
19289 + *
19290 + * You should have received a copy of the GNU General Public License
19291 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19292 + */
19293 +
19294 +/*
19295 + * ioctl
19296 + * plink-management and readdir in userspace.
19297 + * assist the pathconf(3) wrapper library.
19298 + * move-down
19299 + * File-based Hierarchical Storage Management.
19300 + */
19301 +
19302 +#include <linux/compat.h>
19303 +#include <linux/file.h>
19304 +#include "aufs.h"
19305 +
19306 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19307 +{
19308 +       int err, fd;
19309 +       aufs_bindex_t wbi, bindex, bbot;
19310 +       struct file *h_file;
19311 +       struct super_block *sb;
19312 +       struct dentry *root;
19313 +       struct au_branch *br;
19314 +       struct aufs_wbr_fd wbrfd = {
19315 +               .oflags = au_dir_roflags,
19316 +               .brid   = -1
19317 +       };
19318 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19319 +               | O_NOATIME | O_CLOEXEC;
19320 +
19321 +       AuDebugOn(wbrfd.oflags & ~valid);
19322 +
19323 +       if (arg) {
19324 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19325 +               if (unlikely(err)) {
19326 +                       err = -EFAULT;
19327 +                       goto out;
19328 +               }
19329 +
19330 +               err = -EINVAL;
19331 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19332 +               wbrfd.oflags |= au_dir_roflags;
19333 +               AuDbg("0%o\n", wbrfd.oflags);
19334 +               if (unlikely(wbrfd.oflags & ~valid))
19335 +                       goto out;
19336 +       }
19337 +
19338 +       fd = get_unused_fd_flags(0);
19339 +       err = fd;
19340 +       if (unlikely(fd < 0))
19341 +               goto out;
19342 +
19343 +       h_file = ERR_PTR(-EINVAL);
19344 +       wbi = 0;
19345 +       br = NULL;
19346 +       sb = path->dentry->d_sb;
19347 +       root = sb->s_root;
19348 +       aufs_read_lock(root, AuLock_IR);
19349 +       bbot = au_sbbot(sb);
19350 +       if (wbrfd.brid >= 0) {
19351 +               wbi = au_br_index(sb, wbrfd.brid);
19352 +               if (unlikely(wbi < 0 || wbi > bbot))
19353 +                       goto out_unlock;
19354 +       }
19355 +
19356 +       h_file = ERR_PTR(-ENOENT);
19357 +       br = au_sbr(sb, wbi);
19358 +       if (!au_br_writable(br->br_perm)) {
19359 +               if (arg)
19360 +                       goto out_unlock;
19361 +
19362 +               bindex = wbi + 1;
19363 +               wbi = -1;
19364 +               for (; bindex <= bbot; bindex++) {
19365 +                       br = au_sbr(sb, bindex);
19366 +                       if (au_br_writable(br->br_perm)) {
19367 +                               wbi = bindex;
19368 +                               br = au_sbr(sb, wbi);
19369 +                               break;
19370 +                       }
19371 +               }
19372 +       }
19373 +       AuDbg("wbi %d\n", wbi);
19374 +       if (wbi >= 0)
19375 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19376 +                                  /*force_wr*/0);
19377 +
19378 +out_unlock:
19379 +       aufs_read_unlock(root, AuLock_IR);
19380 +       err = PTR_ERR(h_file);
19381 +       if (IS_ERR(h_file))
19382 +               goto out_fd;
19383 +
19384 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19385 +       fd_install(fd, h_file);
19386 +       err = fd;
19387 +       goto out; /* success */
19388 +
19389 +out_fd:
19390 +       put_unused_fd(fd);
19391 +out:
19392 +       AuTraceErr(err);
19393 +       return err;
19394 +}
19395 +
19396 +/* ---------------------------------------------------------------------- */
19397 +
19398 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19399 +{
19400 +       long err;
19401 +       struct dentry *dentry;
19402 +
19403 +       switch (cmd) {
19404 +       case AUFS_CTL_RDU:
19405 +       case AUFS_CTL_RDU_INO:
19406 +               err = au_rdu_ioctl(file, cmd, arg);
19407 +               break;
19408 +
19409 +       case AUFS_CTL_WBR_FD:
19410 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19411 +               break;
19412 +
19413 +       case AUFS_CTL_IBUSY:
19414 +               err = au_ibusy_ioctl(file, arg);
19415 +               break;
19416 +
19417 +       case AUFS_CTL_BRINFO:
19418 +               err = au_brinfo_ioctl(file, arg);
19419 +               break;
19420 +
19421 +       case AUFS_CTL_FHSM_FD:
19422 +               dentry = file->f_path.dentry;
19423 +               if (IS_ROOT(dentry))
19424 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19425 +               else
19426 +                       err = -ENOTTY;
19427 +               break;
19428 +
19429 +       default:
19430 +               /* do not call the lower */
19431 +               AuDbg("0x%x\n", cmd);
19432 +               err = -ENOTTY;
19433 +       }
19434 +
19435 +       AuTraceErr(err);
19436 +       return err;
19437 +}
19438 +
19439 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19440 +{
19441 +       long err;
19442 +
19443 +       switch (cmd) {
19444 +       case AUFS_CTL_MVDOWN:
19445 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19446 +               break;
19447 +
19448 +       case AUFS_CTL_WBR_FD:
19449 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19450 +               break;
19451 +
19452 +       default:
19453 +               /* do not call the lower */
19454 +               AuDbg("0x%x\n", cmd);
19455 +               err = -ENOTTY;
19456 +       }
19457 +
19458 +       AuTraceErr(err);
19459 +       return err;
19460 +}
19461 +
19462 +#ifdef CONFIG_COMPAT
19463 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19464 +                          unsigned long arg)
19465 +{
19466 +       long err;
19467 +
19468 +       switch (cmd) {
19469 +       case AUFS_CTL_RDU:
19470 +       case AUFS_CTL_RDU_INO:
19471 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19472 +               break;
19473 +
19474 +       case AUFS_CTL_IBUSY:
19475 +               err = au_ibusy_compat_ioctl(file, arg);
19476 +               break;
19477 +
19478 +       case AUFS_CTL_BRINFO:
19479 +               err = au_brinfo_compat_ioctl(file, arg);
19480 +               break;
19481 +
19482 +       default:
19483 +               err = aufs_ioctl_dir(file, cmd, arg);
19484 +       }
19485 +
19486 +       AuTraceErr(err);
19487 +       return err;
19488 +}
19489 +
19490 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19491 +                             unsigned long arg)
19492 +{
19493 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19494 +}
19495 +#endif
19496 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19497 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19498 +++ linux/fs/aufs/i_op_add.c    2021-11-01 23:48:34.209692595 +0100
19499 @@ -0,0 +1,941 @@
19500 +// SPDX-License-Identifier: GPL-2.0
19501 +/*
19502 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19503 + *
19504 + * This program, aufs is free software; you can redistribute it and/or modify
19505 + * it under the terms of the GNU General Public License as published by
19506 + * the Free Software Foundation; either version 2 of the License, or
19507 + * (at your option) any later version.
19508 + *
19509 + * This program is distributed in the hope that it will be useful,
19510 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19511 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19512 + * GNU General Public License for more details.
19513 + *
19514 + * You should have received a copy of the GNU General Public License
19515 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19516 + */
19517 +
19518 +/*
19519 + * inode operations (add entry)
19520 + */
19521 +
19522 +#include <linux/iversion.h>
19523 +#include "aufs.h"
19524 +
19525 +/*
19526 + * final procedure of adding a new entry, except link(2).
19527 + * remove whiteout, instantiate, copyup the parent dir's times and size
19528 + * and update version.
19529 + * if it failed, re-create the removed whiteout.
19530 + */
19531 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19532 +                 struct dentry *wh_dentry, struct dentry *dentry)
19533 +{
19534 +       int err, rerr;
19535 +       aufs_bindex_t bwh;
19536 +       struct path h_path;
19537 +       struct super_block *sb;
19538 +       struct inode *inode, *h_dir;
19539 +       struct dentry *wh;
19540 +
19541 +       bwh = -1;
19542 +       sb = dir->i_sb;
19543 +       if (wh_dentry) {
19544 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19545 +               IMustLock(h_dir);
19546 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19547 +               bwh = au_dbwh(dentry);
19548 +               h_path.dentry = wh_dentry;
19549 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19550 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19551 +                                         dentry);
19552 +               if (unlikely(err))
19553 +                       goto out;
19554 +       }
19555 +
19556 +       inode = au_new_inode(dentry, /*must_new*/1);
19557 +       if (!IS_ERR(inode)) {
19558 +               d_instantiate(dentry, inode);
19559 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19560 +               IMustLock(dir);
19561 +               au_dir_ts(dir, bindex);
19562 +               inode_inc_iversion(dir);
19563 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19564 +               return 0; /* success */
19565 +       }
19566 +
19567 +       err = PTR_ERR(inode);
19568 +       if (!wh_dentry)
19569 +               goto out;
19570 +
19571 +       /* revert */
19572 +       /* dir inode is locked */
19573 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19574 +       rerr = PTR_ERR(wh);
19575 +       if (IS_ERR(wh)) {
19576 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19577 +                       dentry, err, rerr);
19578 +               err = -EIO;
19579 +       } else
19580 +               dput(wh);
19581 +
19582 +out:
19583 +       return err;
19584 +}
19585 +
19586 +static int au_d_may_add(struct dentry *dentry)
19587 +{
19588 +       int err;
19589 +
19590 +       err = 0;
19591 +       if (unlikely(d_unhashed(dentry)))
19592 +               err = -ENOENT;
19593 +       if (unlikely(d_really_is_positive(dentry)))
19594 +               err = -EEXIST;
19595 +       return err;
19596 +}
19597 +
19598 +/*
19599 + * simple tests for the adding inode operations.
19600 + * following the checks in vfs, plus the parent-child relationship.
19601 + */
19602 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19603 +              struct dentry *h_parent, int isdir)
19604 +{
19605 +       int err;
19606 +       umode_t h_mode;
19607 +       struct dentry *h_dentry;
19608 +       struct inode *h_inode;
19609 +
19610 +       err = -ENAMETOOLONG;
19611 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19612 +               goto out;
19613 +
19614 +       h_dentry = au_h_dptr(dentry, bindex);
19615 +       if (d_really_is_negative(dentry)) {
19616 +               err = -EEXIST;
19617 +               if (unlikely(d_is_positive(h_dentry)))
19618 +                       goto out;
19619 +       } else {
19620 +               /* rename(2) case */
19621 +               err = -EIO;
19622 +               if (unlikely(d_is_negative(h_dentry)))
19623 +                       goto out;
19624 +               h_inode = d_inode(h_dentry);
19625 +               if (unlikely(!h_inode->i_nlink))
19626 +                       goto out;
19627 +
19628 +               h_mode = h_inode->i_mode;
19629 +               if (!isdir) {
19630 +                       err = -EISDIR;
19631 +                       if (unlikely(S_ISDIR(h_mode)))
19632 +                               goto out;
19633 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19634 +                       err = -ENOTDIR;
19635 +                       goto out;
19636 +               }
19637 +       }
19638 +
19639 +       err = 0;
19640 +       /* expected parent dir is locked */
19641 +       if (unlikely(h_parent != h_dentry->d_parent))
19642 +               err = -EIO;
19643 +
19644 +out:
19645 +       AuTraceErr(err);
19646 +       return err;
19647 +}
19648 +
19649 +/*
19650 + * initial procedure of adding a new entry.
19651 + * prepare writable branch and the parent dir, lock it,
19652 + * and lookup whiteout for the new entry.
19653 + */
19654 +static struct dentry*
19655 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19656 +                 struct dentry *src_dentry, struct au_pin *pin,
19657 +                 struct au_wr_dir_args *wr_dir_args)
19658 +{
19659 +       struct dentry *wh_dentry, *h_parent;
19660 +       struct super_block *sb;
19661 +       struct au_branch *br;
19662 +       int err;
19663 +       unsigned int udba;
19664 +       aufs_bindex_t bcpup;
19665 +
19666 +       AuDbg("%pd\n", dentry);
19667 +
19668 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19669 +       bcpup = err;
19670 +       wh_dentry = ERR_PTR(err);
19671 +       if (unlikely(err < 0))
19672 +               goto out;
19673 +
19674 +       sb = dentry->d_sb;
19675 +       udba = au_opt_udba(sb);
19676 +       err = au_pin(pin, dentry, bcpup, udba,
19677 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19678 +       wh_dentry = ERR_PTR(err);
19679 +       if (unlikely(err))
19680 +               goto out;
19681 +
19682 +       h_parent = au_pinned_h_parent(pin);
19683 +       if (udba != AuOpt_UDBA_NONE
19684 +           && au_dbtop(dentry) == bcpup)
19685 +               err = au_may_add(dentry, bcpup, h_parent,
19686 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19687 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19688 +               err = -ENAMETOOLONG;
19689 +       wh_dentry = ERR_PTR(err);
19690 +       if (unlikely(err))
19691 +               goto out_unpin;
19692 +
19693 +       br = au_sbr(sb, bcpup);
19694 +       if (dt) {
19695 +               struct path tmp = {
19696 +                       .dentry = h_parent,
19697 +                       .mnt    = au_br_mnt(br)
19698 +               };
19699 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19700 +       }
19701 +
19702 +       wh_dentry = NULL;
19703 +       if (bcpup != au_dbwh(dentry))
19704 +               goto out; /* success */
19705 +
19706 +       /*
19707 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19708 +        * would not be able to removed in the future. So we don't allow such
19709 +        * name here and we don't handle ENAMETOOLONG differently here.
19710 +        */
19711 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19712 +
19713 +out_unpin:
19714 +       if (IS_ERR(wh_dentry))
19715 +               au_unpin(pin);
19716 +out:
19717 +       return wh_dentry;
19718 +}
19719 +
19720 +/* ---------------------------------------------------------------------- */
19721 +
19722 +enum { Mknod, Symlink, Creat };
19723 +struct simple_arg {
19724 +       int type;
19725 +       union {
19726 +               struct {
19727 +                       umode_t                 mode;
19728 +                       bool                    want_excl;
19729 +                       bool                    try_aopen;
19730 +                       struct vfsub_aopen_args *aopen;
19731 +               } c;
19732 +               struct {
19733 +                       const char *symname;
19734 +               } s;
19735 +               struct {
19736 +                       umode_t mode;
19737 +                       dev_t dev;
19738 +               } m;
19739 +       } u;
19740 +};
19741 +
19742 +static int add_simple(struct inode *dir, struct dentry *dentry,
19743 +                     struct simple_arg *arg)
19744 +{
19745 +       int err, rerr;
19746 +       aufs_bindex_t btop;
19747 +       unsigned char created;
19748 +       const unsigned char try_aopen
19749 +               = (arg->type == Creat && arg->u.c.try_aopen);
19750 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19751 +       struct dentry *wh_dentry, *parent;
19752 +       struct inode *h_dir;
19753 +       struct super_block *sb;
19754 +       struct au_branch *br;
19755 +       /* to reduce stack size */
19756 +       struct {
19757 +               struct au_dtime dt;
19758 +               struct au_pin pin;
19759 +               struct path h_path;
19760 +               struct au_wr_dir_args wr_dir_args;
19761 +       } *a;
19762 +
19763 +       AuDbg("%pd\n", dentry);
19764 +       IMustLock(dir);
19765 +
19766 +       err = -ENOMEM;
19767 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19768 +       if (unlikely(!a))
19769 +               goto out;
19770 +       a->wr_dir_args.force_btgt = -1;
19771 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19772 +
19773 +       parent = dentry->d_parent; /* dir inode is locked */
19774 +       if (!try_aopen) {
19775 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19776 +               if (unlikely(err))
19777 +                       goto out_free;
19778 +       }
19779 +       err = au_d_may_add(dentry);
19780 +       if (unlikely(err))
19781 +               goto out_unlock;
19782 +       if (!try_aopen)
19783 +               di_write_lock_parent(parent);
19784 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19785 +                                     &a->pin, &a->wr_dir_args);
19786 +       err = PTR_ERR(wh_dentry);
19787 +       if (IS_ERR(wh_dentry))
19788 +               goto out_parent;
19789 +
19790 +       btop = au_dbtop(dentry);
19791 +       sb = dentry->d_sb;
19792 +       br = au_sbr(sb, btop);
19793 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19794 +       a->h_path.mnt = au_br_mnt(br);
19795 +       h_dir = au_pinned_h_dir(&a->pin);
19796 +       switch (arg->type) {
19797 +       case Creat:
19798 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19799 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19800 +                                          arg->u.c.want_excl);
19801 +                       created = !err;
19802 +                       if (!err && try_aopen)
19803 +                               aopen->file->f_mode |= FMODE_CREATED;
19804 +               } else {
19805 +                       aopen->br = br;
19806 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19807 +                       AuDbg("err %d\n", err);
19808 +                       AuDbgFile(aopen->file);
19809 +                       created = err >= 0
19810 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19811 +               }
19812 +               break;
19813 +       case Symlink:
19814 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19815 +               created = !err;
19816 +               break;
19817 +       case Mknod:
19818 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19819 +                                 arg->u.m.dev);
19820 +               created = !err;
19821 +               break;
19822 +       default:
19823 +               BUG();
19824 +       }
19825 +       if (unlikely(err < 0))
19826 +               goto out_unpin;
19827 +
19828 +       err = epilog(dir, btop, wh_dentry, dentry);
19829 +       if (!err)
19830 +               goto out_unpin; /* success */
19831 +
19832 +       /* revert */
19833 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19834 +               /* no delegation since it is just created */
19835 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19836 +                                   /*force*/0);
19837 +               if (rerr) {
19838 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19839 +                               dentry, err, rerr);
19840 +                       err = -EIO;
19841 +               }
19842 +               au_dtime_revert(&a->dt);
19843 +       }
19844 +       if (try_aopen && h_dir->i_op->atomic_open
19845 +           && (aopen->file->f_mode & FMODE_OPENED))
19846 +               /* aopen->file is still opened */
19847 +               au_lcnt_dec(&aopen->br->br_nfiles);
19848 +
19849 +out_unpin:
19850 +       au_unpin(&a->pin);
19851 +       dput(wh_dentry);
19852 +out_parent:
19853 +       if (!try_aopen)
19854 +               di_write_unlock(parent);
19855 +out_unlock:
19856 +       if (unlikely(err)) {
19857 +               au_update_dbtop(dentry);
19858 +               d_drop(dentry);
19859 +       }
19860 +       if (!try_aopen)
19861 +               aufs_read_unlock(dentry, AuLock_DW);
19862 +out_free:
19863 +       au_kfree_rcu(a);
19864 +out:
19865 +       return err;
19866 +}
19867 +
19868 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
19869 +              struct dentry *dentry, umode_t mode, dev_t dev)
19870 +{
19871 +       struct simple_arg arg = {
19872 +               .type = Mknod,
19873 +               .u.m = {
19874 +                       .mode   = mode,
19875 +                       .dev    = dev
19876 +               }
19877 +       };
19878 +       return add_simple(dir, dentry, &arg);
19879 +}
19880 +
19881 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
19882 +                struct dentry *dentry, const char *symname)
19883 +{
19884 +       struct simple_arg arg = {
19885 +               .type = Symlink,
19886 +               .u.s.symname = symname
19887 +       };
19888 +       return add_simple(dir, dentry, &arg);
19889 +}
19890 +
19891 +int aufs_create(struct user_namespace *userns, struct inode *dir,
19892 +               struct dentry *dentry, umode_t mode, bool want_excl)
19893 +{
19894 +       struct simple_arg arg = {
19895 +               .type = Creat,
19896 +               .u.c = {
19897 +                       .mode           = mode,
19898 +                       .want_excl      = want_excl
19899 +               }
19900 +       };
19901 +       return add_simple(dir, dentry, &arg);
19902 +}
19903 +
19904 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19905 +                      struct vfsub_aopen_args *aopen_args)
19906 +{
19907 +       struct simple_arg arg = {
19908 +               .type = Creat,
19909 +               .u.c = {
19910 +                       .mode           = aopen_args->create_mode,
19911 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
19912 +                       .try_aopen      = true,
19913 +                       .aopen          = aopen_args
19914 +               }
19915 +       };
19916 +       return add_simple(dir, dentry, &arg);
19917 +}
19918 +
19919 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
19920 +                struct dentry *dentry, umode_t mode)
19921 +{
19922 +       int err;
19923 +       aufs_bindex_t bindex;
19924 +       struct super_block *sb;
19925 +       struct dentry *parent, *h_parent, *h_dentry;
19926 +       struct inode *h_dir, *inode;
19927 +       struct vfsmount *h_mnt;
19928 +       struct user_namespace *h_userns;
19929 +       struct au_wr_dir_args wr_dir_args = {
19930 +               .force_btgt     = -1,
19931 +               .flags          = AuWrDir_TMPFILE
19932 +       };
19933 +
19934 +       /* copy-up may happen */
19935 +       inode_lock(dir);
19936 +
19937 +       sb = dir->i_sb;
19938 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19939 +       if (unlikely(err))
19940 +               goto out;
19941 +
19942 +       err = au_di_init(dentry);
19943 +       if (unlikely(err))
19944 +               goto out_si;
19945 +
19946 +       err = -EBUSY;
19947 +       parent = d_find_any_alias(dir);
19948 +       AuDebugOn(!parent);
19949 +       di_write_lock_parent(parent);
19950 +       if (unlikely(d_inode(parent) != dir))
19951 +               goto out_parent;
19952 +
19953 +       err = au_digen_test(parent, au_sigen(sb));
19954 +       if (unlikely(err))
19955 +               goto out_parent;
19956 +
19957 +       bindex = au_dbtop(parent);
19958 +       au_set_dbtop(dentry, bindex);
19959 +       au_set_dbbot(dentry, bindex);
19960 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19961 +       bindex = err;
19962 +       if (unlikely(err < 0))
19963 +               goto out_parent;
19964 +
19965 +       err = -EOPNOTSUPP;
19966 +       h_dir = au_h_iptr(dir, bindex);
19967 +       if (unlikely(!h_dir->i_op->tmpfile))
19968 +               goto out_parent;
19969 +
19970 +       h_mnt = au_sbr_mnt(sb, bindex);
19971 +       err = vfsub_mnt_want_write(h_mnt);
19972 +       if (unlikely(err))
19973 +               goto out_parent;
19974 +
19975 +       h_userns = mnt_user_ns(h_mnt);
19976 +       h_parent = au_h_dptr(parent, bindex);
19977 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
19978 +       if (IS_ERR(h_dentry)) {
19979 +               err = PTR_ERR(h_dentry);
19980 +               goto out_mnt;
19981 +       }
19982 +
19983 +       au_set_dbtop(dentry, bindex);
19984 +       au_set_dbbot(dentry, bindex);
19985 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
19986 +       inode = au_new_inode(dentry, /*must_new*/1);
19987 +       if (IS_ERR(inode)) {
19988 +               err = PTR_ERR(inode);
19989 +               au_set_h_dptr(dentry, bindex, NULL);
19990 +               au_set_dbtop(dentry, -1);
19991 +               au_set_dbbot(dentry, -1);
19992 +       } else {
19993 +               if (!inode->i_nlink)
19994 +                       set_nlink(inode, 1);
19995 +               d_tmpfile(dentry, inode);
19996 +               au_di(dentry)->di_tmpfile = 1;
19997 +
19998 +               /* update without i_mutex */
19999 +               if (au_ibtop(dir) == au_dbtop(dentry))
20000 +                       au_cpup_attr_timesizes(dir);
20001 +       }
20002 +       dput(h_dentry);
20003 +
20004 +out_mnt:
20005 +       vfsub_mnt_drop_write(h_mnt);
20006 +out_parent:
20007 +       di_write_unlock(parent);
20008 +       dput(parent);
20009 +       di_write_unlock(dentry);
20010 +       if (unlikely(err)) {
20011 +               au_di_fin(dentry);
20012 +               dentry->d_fsdata = NULL;
20013 +       }
20014 +out_si:
20015 +       si_read_unlock(sb);
20016 +out:
20017 +       inode_unlock(dir);
20018 +       return err;
20019 +}
20020 +
20021 +/* ---------------------------------------------------------------------- */
20022 +
20023 +struct au_link_args {
20024 +       aufs_bindex_t bdst, bsrc;
20025 +       struct au_pin pin;
20026 +       struct path h_path;
20027 +       struct dentry *src_parent, *parent;
20028 +};
20029 +
20030 +static int au_cpup_before_link(struct dentry *src_dentry,
20031 +                              struct au_link_args *a)
20032 +{
20033 +       int err;
20034 +       struct dentry *h_src_dentry;
20035 +       struct au_cp_generic cpg = {
20036 +               .dentry = src_dentry,
20037 +               .bdst   = a->bdst,
20038 +               .bsrc   = a->bsrc,
20039 +               .len    = -1,
20040 +               .pin    = &a->pin,
20041 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20042 +       };
20043 +
20044 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20045 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20046 +       if (unlikely(err))
20047 +               goto out;
20048 +
20049 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20050 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20051 +                    au_opt_udba(src_dentry->d_sb),
20052 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20053 +       if (unlikely(err))
20054 +               goto out;
20055 +
20056 +       err = au_sio_cpup_simple(&cpg);
20057 +       au_unpin(&a->pin);
20058 +
20059 +out:
20060 +       di_read_unlock(a->src_parent, AuLock_IR);
20061 +       return err;
20062 +}
20063 +
20064 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20065 +                          struct au_link_args *a)
20066 +{
20067 +       int err;
20068 +       unsigned char plink;
20069 +       aufs_bindex_t bbot;
20070 +       struct dentry *h_src_dentry;
20071 +       struct inode *h_inode, *inode, *delegated;
20072 +       struct super_block *sb;
20073 +       struct file *h_file;
20074 +
20075 +       plink = 0;
20076 +       h_inode = NULL;
20077 +       sb = src_dentry->d_sb;
20078 +       inode = d_inode(src_dentry);
20079 +       if (au_ibtop(inode) <= a->bdst)
20080 +               h_inode = au_h_iptr(inode, a->bdst);
20081 +       if (!h_inode || !h_inode->i_nlink) {
20082 +               /* copyup src_dentry as the name of dentry. */
20083 +               bbot = au_dbbot(dentry);
20084 +               if (bbot < a->bsrc)
20085 +                       au_set_dbbot(dentry, a->bsrc);
20086 +               au_set_h_dptr(dentry, a->bsrc,
20087 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20088 +               dget(a->h_path.dentry);
20089 +               au_set_h_dptr(dentry, a->bdst, NULL);
20090 +               AuDbg("temporary d_inode...\n");
20091 +               spin_lock(&dentry->d_lock);
20092 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20093 +               spin_unlock(&dentry->d_lock);
20094 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20095 +               if (IS_ERR(h_file))
20096 +                       err = PTR_ERR(h_file);
20097 +               else {
20098 +                       struct au_cp_generic cpg = {
20099 +                               .dentry = dentry,
20100 +                               .bdst   = a->bdst,
20101 +                               .bsrc   = -1,
20102 +                               .len    = -1,
20103 +                               .pin    = &a->pin,
20104 +                               .flags  = AuCpup_KEEPLINO
20105 +                       };
20106 +                       err = au_sio_cpup_simple(&cpg);
20107 +                       au_h_open_post(dentry, a->bsrc, h_file);
20108 +                       if (!err) {
20109 +                               dput(a->h_path.dentry);
20110 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20111 +                       } else
20112 +                               au_set_h_dptr(dentry, a->bdst,
20113 +                                             a->h_path.dentry);
20114 +               }
20115 +               spin_lock(&dentry->d_lock);
20116 +               dentry->d_inode = NULL; /* restore */
20117 +               spin_unlock(&dentry->d_lock);
20118 +               AuDbg("temporary d_inode...done\n");
20119 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20120 +               au_set_dbbot(dentry, bbot);
20121 +       } else {
20122 +               /* the inode of src_dentry already exists on a.bdst branch */
20123 +               h_src_dentry = d_find_alias(h_inode);
20124 +               if (!h_src_dentry && au_plink_test(inode)) {
20125 +                       plink = 1;
20126 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20127 +                       err = PTR_ERR(h_src_dentry);
20128 +                       if (IS_ERR(h_src_dentry))
20129 +                               goto out;
20130 +
20131 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20132 +                               dput(h_src_dentry);
20133 +                               h_src_dentry = NULL;
20134 +                       }
20135 +
20136 +               }
20137 +               if (h_src_dentry) {
20138 +                       delegated = NULL;
20139 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20140 +                                        &a->h_path, &delegated);
20141 +                       if (unlikely(err == -EWOULDBLOCK)) {
20142 +                               pr_warn("cannot retry for NFSv4 delegation"
20143 +                                       " for an internal link\n");
20144 +                               iput(delegated);
20145 +                       }
20146 +                       dput(h_src_dentry);
20147 +               } else {
20148 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20149 +                               h_inode->i_ino, a->bdst);
20150 +                       err = -EIO;
20151 +               }
20152 +       }
20153 +
20154 +       if (!err && !plink)
20155 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20156 +
20157 +out:
20158 +       AuTraceErr(err);
20159 +       return err;
20160 +}
20161 +
20162 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20163 +             struct dentry *dentry)
20164 +{
20165 +       int err, rerr;
20166 +       struct au_dtime dt;
20167 +       struct au_link_args *a;
20168 +       struct dentry *wh_dentry, *h_src_dentry;
20169 +       struct inode *inode, *delegated;
20170 +       struct super_block *sb;
20171 +       struct au_wr_dir_args wr_dir_args = {
20172 +               /* .force_btgt  = -1, */
20173 +               .flags          = AuWrDir_ADD_ENTRY
20174 +       };
20175 +
20176 +       IMustLock(dir);
20177 +       inode = d_inode(src_dentry);
20178 +       IMustLock(inode);
20179 +
20180 +       err = -ENOMEM;
20181 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20182 +       if (unlikely(!a))
20183 +               goto out;
20184 +
20185 +       a->parent = dentry->d_parent; /* dir inode is locked */
20186 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20187 +                                       AuLock_NOPLM | AuLock_GEN);
20188 +       if (unlikely(err))
20189 +               goto out_kfree;
20190 +       err = au_d_linkable(src_dentry);
20191 +       if (unlikely(err))
20192 +               goto out_unlock;
20193 +       err = au_d_may_add(dentry);
20194 +       if (unlikely(err))
20195 +               goto out_unlock;
20196 +
20197 +       a->src_parent = dget_parent(src_dentry);
20198 +       wr_dir_args.force_btgt = au_ibtop(inode);
20199 +
20200 +       di_write_lock_parent(a->parent);
20201 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20202 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20203 +                                     &wr_dir_args);
20204 +       err = PTR_ERR(wh_dentry);
20205 +       if (IS_ERR(wh_dentry))
20206 +               goto out_parent;
20207 +
20208 +       err = 0;
20209 +       sb = dentry->d_sb;
20210 +       a->bdst = au_dbtop(dentry);
20211 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20212 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20213 +       a->bsrc = au_ibtop(inode);
20214 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20215 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20216 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20217 +       if (!h_src_dentry) {
20218 +               a->bsrc = au_dbtop(src_dentry);
20219 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20220 +               AuDebugOn(!h_src_dentry);
20221 +       } else if (IS_ERR(h_src_dentry)) {
20222 +               err = PTR_ERR(h_src_dentry);
20223 +               goto out_parent;
20224 +       }
20225 +
20226 +       /*
20227 +        * aufs doesn't touch the credential so
20228 +        * security_dentry_create_files_as() is unnecessary.
20229 +        */
20230 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20231 +               if (a->bdst < a->bsrc
20232 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20233 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20234 +               else {
20235 +                       delegated = NULL;
20236 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20237 +                                        &a->h_path, &delegated);
20238 +                       if (unlikely(err == -EWOULDBLOCK)) {
20239 +                               pr_warn("cannot retry for NFSv4 delegation"
20240 +                                       " for an internal link\n");
20241 +                               iput(delegated);
20242 +                       }
20243 +               }
20244 +               dput(h_src_dentry);
20245 +       } else {
20246 +               /*
20247 +                * copyup src_dentry to the branch we process,
20248 +                * and then link(2) to it.
20249 +                */
20250 +               dput(h_src_dentry);
20251 +               if (a->bdst < a->bsrc
20252 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20253 +                       au_unpin(&a->pin);
20254 +                       di_write_unlock(a->parent);
20255 +                       err = au_cpup_before_link(src_dentry, a);
20256 +                       di_write_lock_parent(a->parent);
20257 +                       if (!err)
20258 +                               err = au_pin(&a->pin, dentry, a->bdst,
20259 +                                            au_opt_udba(sb),
20260 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20261 +                       if (unlikely(err))
20262 +                               goto out_wh;
20263 +               }
20264 +               if (!err) {
20265 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20266 +                       err = -ENOENT;
20267 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20268 +                               delegated = NULL;
20269 +                               err = vfsub_link(h_src_dentry,
20270 +                                                au_pinned_h_dir(&a->pin),
20271 +                                                &a->h_path, &delegated);
20272 +                               if (unlikely(err == -EWOULDBLOCK)) {
20273 +                                       pr_warn("cannot retry"
20274 +                                               " for NFSv4 delegation"
20275 +                                               " for an internal link\n");
20276 +                                       iput(delegated);
20277 +                               }
20278 +                       }
20279 +               }
20280 +       }
20281 +       if (unlikely(err))
20282 +               goto out_unpin;
20283 +
20284 +       if (wh_dentry) {
20285 +               a->h_path.dentry = wh_dentry;
20286 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20287 +                                         dentry);
20288 +               if (unlikely(err))
20289 +                       goto out_revert;
20290 +       }
20291 +
20292 +       au_dir_ts(dir, a->bdst);
20293 +       inode_inc_iversion(dir);
20294 +       inc_nlink(inode);
20295 +       inode->i_ctime = dir->i_ctime;
20296 +       d_instantiate(dentry, au_igrab(inode));
20297 +       if (d_unhashed(a->h_path.dentry))
20298 +               /* some filesystem calls d_drop() */
20299 +               d_drop(dentry);
20300 +       /* some filesystems consume an inode even hardlink */
20301 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20302 +       goto out_unpin; /* success */
20303 +
20304 +out_revert:
20305 +       /* no delegation since it is just created */
20306 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20307 +                           /*delegated*/NULL, /*force*/0);
20308 +       if (unlikely(rerr)) {
20309 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20310 +               err = -EIO;
20311 +       }
20312 +       au_dtime_revert(&dt);
20313 +out_unpin:
20314 +       au_unpin(&a->pin);
20315 +out_wh:
20316 +       dput(wh_dentry);
20317 +out_parent:
20318 +       di_write_unlock(a->parent);
20319 +       dput(a->src_parent);
20320 +out_unlock:
20321 +       if (unlikely(err)) {
20322 +               au_update_dbtop(dentry);
20323 +               d_drop(dentry);
20324 +       }
20325 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20326 +out_kfree:
20327 +       au_kfree_rcu(a);
20328 +out:
20329 +       AuTraceErr(err);
20330 +       return err;
20331 +}
20332 +
20333 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20334 +              struct dentry *dentry, umode_t mode)
20335 +{
20336 +       int err, rerr;
20337 +       aufs_bindex_t bindex;
20338 +       unsigned char diropq;
20339 +       struct path h_path;
20340 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20341 +       struct inode *h_inode;
20342 +       struct super_block *sb;
20343 +       struct {
20344 +               struct au_pin pin;
20345 +               struct au_dtime dt;
20346 +       } *a; /* reduce the stack usage */
20347 +       struct au_wr_dir_args wr_dir_args = {
20348 +               .force_btgt     = -1,
20349 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20350 +       };
20351 +
20352 +       IMustLock(dir);
20353 +
20354 +       err = -ENOMEM;
20355 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20356 +       if (unlikely(!a))
20357 +               goto out;
20358 +
20359 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20360 +       if (unlikely(err))
20361 +               goto out_free;
20362 +       err = au_d_may_add(dentry);
20363 +       if (unlikely(err))
20364 +               goto out_unlock;
20365 +
20366 +       parent = dentry->d_parent; /* dir inode is locked */
20367 +       di_write_lock_parent(parent);
20368 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20369 +                                     &a->pin, &wr_dir_args);
20370 +       err = PTR_ERR(wh_dentry);
20371 +       if (IS_ERR(wh_dentry))
20372 +               goto out_parent;
20373 +
20374 +       sb = dentry->d_sb;
20375 +       bindex = au_dbtop(dentry);
20376 +       h_path.dentry = au_h_dptr(dentry, bindex);
20377 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20378 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20379 +       if (unlikely(err))
20380 +               goto out_unpin;
20381 +
20382 +       /* make the dir opaque */
20383 +       diropq = 0;
20384 +       h_inode = d_inode(h_path.dentry);
20385 +       if (wh_dentry
20386 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20387 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20388 +               opq_dentry = au_diropq_create(dentry, bindex);
20389 +               inode_unlock(h_inode);
20390 +               err = PTR_ERR(opq_dentry);
20391 +               if (IS_ERR(opq_dentry))
20392 +                       goto out_dir;
20393 +               dput(opq_dentry);
20394 +               diropq = 1;
20395 +       }
20396 +
20397 +       err = epilog(dir, bindex, wh_dentry, dentry);
20398 +       if (!err) {
20399 +               inc_nlink(dir);
20400 +               goto out_unpin; /* success */
20401 +       }
20402 +
20403 +       /* revert */
20404 +       if (diropq) {
20405 +               AuLabel(revert opq);
20406 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20407 +               rerr = au_diropq_remove(dentry, bindex);
20408 +               inode_unlock(h_inode);
20409 +               if (rerr) {
20410 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20411 +                               dentry, err, rerr);
20412 +                       err = -EIO;
20413 +               }
20414 +       }
20415 +
20416 +out_dir:
20417 +       AuLabel(revert dir);
20418 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20419 +       if (rerr) {
20420 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20421 +                       dentry, err, rerr);
20422 +               err = -EIO;
20423 +       }
20424 +       au_dtime_revert(&a->dt);
20425 +out_unpin:
20426 +       au_unpin(&a->pin);
20427 +       dput(wh_dentry);
20428 +out_parent:
20429 +       di_write_unlock(parent);
20430 +out_unlock:
20431 +       if (unlikely(err)) {
20432 +               au_update_dbtop(dentry);
20433 +               d_drop(dentry);
20434 +       }
20435 +       aufs_read_unlock(dentry, AuLock_DW);
20436 +out_free:
20437 +       au_kfree_rcu(a);
20438 +out:
20439 +       return err;
20440 +}
20441 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20442 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20443 +++ linux/fs/aufs/i_op.c        2021-11-01 23:48:34.209692595 +0100
20444 @@ -0,0 +1,1513 @@
20445 +// SPDX-License-Identifier: GPL-2.0
20446 +/*
20447 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20448 + *
20449 + * This program, aufs is free software; you can redistribute it and/or modify
20450 + * it under the terms of the GNU General Public License as published by
20451 + * the Free Software Foundation; either version 2 of the License, or
20452 + * (at your option) any later version.
20453 + *
20454 + * This program is distributed in the hope that it will be useful,
20455 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20456 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20457 + * GNU General Public License for more details.
20458 + *
20459 + * You should have received a copy of the GNU General Public License
20460 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20461 + */
20462 +
20463 +/*
20464 + * inode operations (except add/del/rename)
20465 + */
20466 +
20467 +#include <linux/device_cgroup.h>
20468 +#include <linux/fs_stack.h>
20469 +#include <linux/iversion.h>
20470 +#include <linux/namei.h>
20471 +#include <linux/security.h>
20472 +#include "aufs.h"
20473 +
20474 +static int h_permission(struct inode *h_inode, int mask,
20475 +                       struct path *h_path, int brperm)
20476 +{
20477 +       int err;
20478 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20479 +       struct user_namespace *h_userns;
20480 +
20481 +       err = -EPERM;
20482 +       if (write_mask && IS_IMMUTABLE(h_inode))
20483 +               goto out;
20484 +
20485 +       err = -EACCES;
20486 +       if (((mask & MAY_EXEC)
20487 +            && S_ISREG(h_inode->i_mode)
20488 +            && (path_noexec(h_path)
20489 +                || !(h_inode->i_mode & 0111))))
20490 +               goto out;
20491 +
20492 +       /*
20493 +        * - skip the lower fs test in the case of write to ro branch.
20494 +        * - nfs dir permission write check is optimized, but a policy for
20495 +        *   link/rename requires a real check.
20496 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20497 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20498 +        */
20499 +       h_userns = mnt_user_ns(h_path->mnt);
20500 +       if ((write_mask && !au_br_writable(brperm))
20501 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20502 +               && write_mask && !(mask & MAY_READ))
20503 +           || !h_inode->i_op->permission) {
20504 +               /* AuLabel(generic_permission); */
20505 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20506 +               err = generic_permission(h_userns, h_inode, mask);
20507 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20508 +                       err = h_inode->i_op->permission(h_userns, h_inode,
20509 +                                                       mask);
20510 +               AuTraceErr(err);
20511 +       } else {
20512 +               /* AuLabel(h_inode->permission); */
20513 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
20514 +               AuTraceErr(err);
20515 +       }
20516 +
20517 +       if (!err)
20518 +               err = devcgroup_inode_permission(h_inode, mask);
20519 +       if (!err)
20520 +               err = security_inode_permission(h_inode, mask);
20521 +
20522 +out:
20523 +       return err;
20524 +}
20525 +
20526 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
20527 +                          int mask)
20528 +{
20529 +       int err;
20530 +       aufs_bindex_t bindex, bbot;
20531 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20532 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20533 +       struct inode *h_inode;
20534 +       struct super_block *sb;
20535 +       struct au_branch *br;
20536 +
20537 +       /* todo: support rcu-walk? */
20538 +       if (mask & MAY_NOT_BLOCK)
20539 +               return -ECHILD;
20540 +
20541 +       sb = inode->i_sb;
20542 +       si_read_lock(sb, AuLock_FLUSH);
20543 +       ii_read_lock_child(inode);
20544 +#if 0 /* reserved for future use */
20545 +       /*
20546 +        * This test may be rather 'too much' since the test is essentially done
20547 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20548 +        * generation doesn't match to the superblock's here.  But it isn't a
20549 +        * big deal I suppose.
20550 +        */
20551 +       err = au_iigen_test(inode, au_sigen(sb));
20552 +       if (unlikely(err))
20553 +               goto out;
20554 +#endif
20555 +
20556 +       if (!isdir
20557 +           || write_mask
20558 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20559 +               err = au_busy_or_stale();
20560 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20561 +               if (unlikely(!h_inode
20562 +                            || (h_inode->i_mode & S_IFMT)
20563 +                            != (inode->i_mode & S_IFMT)))
20564 +                       goto out;
20565 +
20566 +               err = 0;
20567 +               bindex = au_ibtop(inode);
20568 +               br = au_sbr(sb, bindex);
20569 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20570 +               if (write_mask
20571 +                   && !err
20572 +                   && !special_file(h_inode->i_mode)) {
20573 +                       /* test whether the upper writable branch exists */
20574 +                       err = -EROFS;
20575 +                       for (; bindex >= 0; bindex--)
20576 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20577 +                                       err = 0;
20578 +                                       break;
20579 +                               }
20580 +               }
20581 +               goto out;
20582 +       }
20583 +
20584 +       /* non-write to dir */
20585 +       err = 0;
20586 +       bbot = au_ibbot(inode);
20587 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20588 +               h_inode = au_h_iptr(inode, bindex);
20589 +               if (h_inode) {
20590 +                       err = au_busy_or_stale();
20591 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20592 +                               break;
20593 +
20594 +                       br = au_sbr(sb, bindex);
20595 +                       err = h_permission(h_inode, mask, &br->br_path,
20596 +                                          br->br_perm);
20597 +               }
20598 +       }
20599 +
20600 +out:
20601 +       ii_read_unlock(inode);
20602 +       si_read_unlock(sb);
20603 +       return err;
20604 +}
20605 +
20606 +/* ---------------------------------------------------------------------- */
20607 +
20608 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20609 +                                 unsigned int flags)
20610 +{
20611 +       struct dentry *ret, *parent;
20612 +       struct inode *inode;
20613 +       struct super_block *sb;
20614 +       int err, npositive;
20615 +
20616 +       IMustLock(dir);
20617 +
20618 +       /* todo: support rcu-walk? */
20619 +       ret = ERR_PTR(-ECHILD);
20620 +       if (flags & LOOKUP_RCU)
20621 +               goto out;
20622 +
20623 +       ret = ERR_PTR(-ENAMETOOLONG);
20624 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20625 +               goto out;
20626 +
20627 +       sb = dir->i_sb;
20628 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20629 +       ret = ERR_PTR(err);
20630 +       if (unlikely(err))
20631 +               goto out;
20632 +
20633 +       err = au_di_init(dentry);
20634 +       ret = ERR_PTR(err);
20635 +       if (unlikely(err))
20636 +               goto out_si;
20637 +
20638 +       inode = NULL;
20639 +       npositive = 0; /* suppress a warning */
20640 +       parent = dentry->d_parent; /* dir inode is locked */
20641 +       di_read_lock_parent(parent, AuLock_IR);
20642 +       err = au_alive_dir(parent);
20643 +       if (!err)
20644 +               err = au_digen_test(parent, au_sigen(sb));
20645 +       if (!err) {
20646 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20647 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20648 +                                          AuLkup_ALLOW_NEG);
20649 +               err = npositive;
20650 +       }
20651 +       di_read_unlock(parent, AuLock_IR);
20652 +       ret = ERR_PTR(err);
20653 +       if (unlikely(err < 0))
20654 +               goto out_unlock;
20655 +
20656 +       if (npositive) {
20657 +               inode = au_new_inode(dentry, /*must_new*/0);
20658 +               if (IS_ERR(inode)) {
20659 +                       ret = (void *)inode;
20660 +                       inode = NULL;
20661 +                       goto out_unlock;
20662 +               }
20663 +       }
20664 +
20665 +       if (inode)
20666 +               atomic_inc(&inode->i_count);
20667 +       ret = d_splice_alias(inode, dentry);
20668 +#if 0 /* reserved for future use */
20669 +       if (unlikely(d_need_lookup(dentry))) {
20670 +               spin_lock(&dentry->d_lock);
20671 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20672 +               spin_unlock(&dentry->d_lock);
20673 +       } else
20674 +#endif
20675 +       if (inode) {
20676 +               if (!IS_ERR(ret)) {
20677 +                       iput(inode);
20678 +                       if (ret && ret != dentry)
20679 +                               ii_write_unlock(inode);
20680 +               } else {
20681 +                       ii_write_unlock(inode);
20682 +                       iput(inode);
20683 +                       inode = NULL;
20684 +               }
20685 +       }
20686 +
20687 +out_unlock:
20688 +       di_write_unlock(dentry);
20689 +out_si:
20690 +       si_read_unlock(sb);
20691 +out:
20692 +       return ret;
20693 +}
20694 +
20695 +/* ---------------------------------------------------------------------- */
20696 +
20697 +/*
20698 + * very dirty and complicated aufs ->atomic_open().
20699 + * aufs_atomic_open()
20700 + * + au_aopen_or_create()
20701 + *   + add_simple()
20702 + *     + vfsub_atomic_open()
20703 + *       + branch fs ->atomic_open()
20704 + *        may call the actual 'open' for h_file
20705 + *       + inc br_nfiles only if opened
20706 + * + au_aopen_no_open() or au_aopen_do_open()
20707 + *
20708 + * au_aopen_do_open()
20709 + * + finish_open()
20710 + *   + au_do_aopen()
20711 + *     + au_do_open() the body of all 'open'
20712 + *       + au_do_open_nondir()
20713 + *        set the passed h_file
20714 + *
20715 + * au_aopen_no_open()
20716 + * + finish_no_open()
20717 + */
20718 +
20719 +struct aopen_node {
20720 +       struct hlist_bl_node hblist;
20721 +       struct file *file, *h_file;
20722 +};
20723 +
20724 +static int au_do_aopen(struct inode *inode, struct file *file)
20725 +{
20726 +       struct hlist_bl_head *aopen;
20727 +       struct hlist_bl_node *pos;
20728 +       struct aopen_node *node;
20729 +       struct au_do_open_args args = {
20730 +               .aopen  = 1,
20731 +               .open   = au_do_open_nondir
20732 +       };
20733 +
20734 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20735 +       hlist_bl_lock(aopen);
20736 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20737 +               if (node->file == file) {
20738 +                       args.h_file = node->h_file;
20739 +                       break;
20740 +               }
20741 +       hlist_bl_unlock(aopen);
20742 +       /* AuDebugOn(!args.h_file); */
20743 +
20744 +       return au_do_open(file, &args);
20745 +}
20746 +
20747 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20748 +                           struct aopen_node *aopen_node)
20749 +{
20750 +       int err;
20751 +       struct hlist_bl_head *aopen;
20752 +
20753 +       AuLabel(here);
20754 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20755 +       au_hbl_add(&aopen_node->hblist, aopen);
20756 +       err = finish_open(file, dentry, au_do_aopen);
20757 +       au_hbl_del(&aopen_node->hblist, aopen);
20758 +       /* AuDbgFile(file); */
20759 +       AuDbg("%pd%s%s\n", dentry,
20760 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20761 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20762 +
20763 +       AuTraceErr(err);
20764 +       return err;
20765 +}
20766 +
20767 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20768 +{
20769 +       int err;
20770 +
20771 +       AuLabel(here);
20772 +       dget(dentry);
20773 +       err = finish_no_open(file, dentry);
20774 +
20775 +       AuTraceErr(err);
20776 +       return err;
20777 +}
20778 +
20779 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20780 +                           struct file *file, unsigned int open_flag,
20781 +                           umode_t create_mode)
20782 +{
20783 +       int err, did_open;
20784 +       unsigned int lkup_flags;
20785 +       aufs_bindex_t bindex;
20786 +       struct super_block *sb;
20787 +       struct dentry *parent, *d;
20788 +       struct vfsub_aopen_args args = {
20789 +               .open_flag      = open_flag,
20790 +               .create_mode    = create_mode
20791 +       };
20792 +       struct aopen_node aopen_node = {
20793 +               .file   = file
20794 +       };
20795 +
20796 +       IMustLock(dir);
20797 +       AuDbg("open_flag 0%o\n", open_flag);
20798 +       AuDbgDentry(dentry);
20799 +
20800 +       err = 0;
20801 +       if (!au_di(dentry)) {
20802 +               lkup_flags = LOOKUP_OPEN;
20803 +               if (open_flag & O_CREAT)
20804 +                       lkup_flags |= LOOKUP_CREATE;
20805 +               d = aufs_lookup(dir, dentry, lkup_flags);
20806 +               if (IS_ERR(d)) {
20807 +                       err = PTR_ERR(d);
20808 +                       AuTraceErr(err);
20809 +                       goto out;
20810 +               } else if (d) {
20811 +                       /*
20812 +                        * obsoleted dentry found.
20813 +                        * another error will be returned later.
20814 +                        */
20815 +                       d_drop(d);
20816 +                       AuDbgDentry(d);
20817 +                       dput(d);
20818 +               }
20819 +               AuDbgDentry(dentry);
20820 +       }
20821 +
20822 +       if (d_is_positive(dentry)
20823 +           || d_unhashed(dentry)
20824 +           || d_unlinked(dentry)
20825 +           || !(open_flag & O_CREAT)) {
20826 +               err = au_aopen_no_open(file, dentry);
20827 +               goto out; /* success */
20828 +       }
20829 +
20830 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20831 +       if (unlikely(err))
20832 +               goto out;
20833 +
20834 +       sb = dentry->d_sb;
20835 +       parent = dentry->d_parent;      /* dir is locked */
20836 +       di_write_lock_parent(parent);
20837 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20838 +       if (unlikely(err < 0))
20839 +               goto out_parent;
20840 +
20841 +       AuDbgDentry(dentry);
20842 +       if (d_is_positive(dentry)) {
20843 +               err = au_aopen_no_open(file, dentry);
20844 +               goto out_parent; /* success */
20845 +       }
20846 +
20847 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20848 +       err = PTR_ERR(args.file);
20849 +       if (IS_ERR(args.file))
20850 +               goto out_parent;
20851 +
20852 +       bindex = au_dbtop(dentry);
20853 +       err = au_aopen_or_create(dir, dentry, &args);
20854 +       AuTraceErr(err);
20855 +       AuDbgFile(args.file);
20856 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20857 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20858 +       if (!did_open) {
20859 +               fput(args.file);
20860 +               args.file = NULL;
20861 +       }
20862 +       di_write_unlock(parent);
20863 +       di_write_unlock(dentry);
20864 +       if (unlikely(err < 0)) {
20865 +               if (args.file)
20866 +                       fput(args.file);
20867 +               goto out_sb;
20868 +       }
20869 +
20870 +       if (!did_open)
20871 +               err = au_aopen_no_open(file, dentry);
20872 +       else {
20873 +               aopen_node.h_file = args.file;
20874 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20875 +       }
20876 +       if (unlikely(err < 0)) {
20877 +               if (args.file)
20878 +                       fput(args.file);
20879 +               if (did_open)
20880 +                       au_lcnt_dec(&args.br->br_nfiles);
20881 +       }
20882 +       goto out_sb; /* success */
20883 +
20884 +out_parent:
20885 +       di_write_unlock(parent);
20886 +       di_write_unlock(dentry);
20887 +out_sb:
20888 +       si_read_unlock(sb);
20889 +out:
20890 +       AuTraceErr(err);
20891 +       AuDbgFile(file);
20892 +       return err;
20893 +}
20894 +
20895 +
20896 +/* ---------------------------------------------------------------------- */
20897 +
20898 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20899 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
20900 +                         aufs_bindex_t btop)
20901 +{
20902 +       int err;
20903 +       struct dentry *h_parent;
20904 +       struct inode *h_dir;
20905 +
20906 +       if (add_entry)
20907 +               IMustLock(d_inode(parent));
20908 +       else
20909 +               di_write_lock_parent(parent);
20910 +
20911 +       err = 0;
20912 +       if (!au_h_dptr(parent, bcpup)) {
20913 +               if (btop > bcpup)
20914 +                       err = au_cpup_dirs(dentry, bcpup);
20915 +               else if (btop < bcpup)
20916 +                       err = au_cpdown_dirs(dentry, bcpup);
20917 +               else
20918 +                       BUG();
20919 +       }
20920 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20921 +               h_parent = au_h_dptr(parent, bcpup);
20922 +               h_dir = d_inode(h_parent);
20923 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20924 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20925 +               /* todo: no unlock here */
20926 +               inode_unlock_shared(h_dir);
20927 +
20928 +               AuDbg("bcpup %d\n", bcpup);
20929 +               if (!err) {
20930 +                       if (d_really_is_negative(dentry))
20931 +                               au_set_h_dptr(dentry, btop, NULL);
20932 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
20933 +               }
20934 +       }
20935 +
20936 +       if (!add_entry)
20937 +               di_write_unlock(parent);
20938 +       if (!err)
20939 +               err = bcpup; /* success */
20940 +
20941 +       AuTraceErr(err);
20942 +       return err;
20943 +}
20944 +
20945 +/*
20946 + * decide the branch and the parent dir where we will create a new entry.
20947 + * returns new bindex or an error.
20948 + * copyup the parent dir if needed.
20949 + */
20950 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20951 +             struct au_wr_dir_args *args)
20952 +{
20953 +       int err;
20954 +       unsigned int flags;
20955 +       aufs_bindex_t bcpup, btop, src_btop;
20956 +       const unsigned char add_entry
20957 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
20958 +               | au_ftest_wrdir(args->flags, TMPFILE);
20959 +       struct super_block *sb;
20960 +       struct dentry *parent;
20961 +       struct au_sbinfo *sbinfo;
20962 +
20963 +       sb = dentry->d_sb;
20964 +       sbinfo = au_sbi(sb);
20965 +       parent = dget_parent(dentry);
20966 +       btop = au_dbtop(dentry);
20967 +       bcpup = btop;
20968 +       if (args->force_btgt < 0) {
20969 +               if (src_dentry) {
20970 +                       src_btop = au_dbtop(src_dentry);
20971 +                       if (src_btop < btop)
20972 +                               bcpup = src_btop;
20973 +               } else if (add_entry) {
20974 +                       flags = 0;
20975 +                       if (au_ftest_wrdir(args->flags, ISDIR))
20976 +                               au_fset_wbr(flags, DIR);
20977 +                       err = AuWbrCreate(sbinfo, dentry, flags);
20978 +                       bcpup = err;
20979 +               }
20980 +
20981 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
20982 +                       if (add_entry)
20983 +                               err = AuWbrCopyup(sbinfo, dentry);
20984 +                       else {
20985 +                               if (!IS_ROOT(dentry)) {
20986 +                                       di_read_lock_parent(parent, !AuLock_IR);
20987 +                                       err = AuWbrCopyup(sbinfo, dentry);
20988 +                                       di_read_unlock(parent, !AuLock_IR);
20989 +                               } else
20990 +                                       err = AuWbrCopyup(sbinfo, dentry);
20991 +                       }
20992 +                       bcpup = err;
20993 +                       if (unlikely(err < 0))
20994 +                               goto out;
20995 +               }
20996 +       } else {
20997 +               bcpup = args->force_btgt;
20998 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
20999 +       }
21000 +
21001 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21002 +       err = bcpup;
21003 +       if (bcpup == btop)
21004 +               goto out; /* success */
21005 +
21006 +       /* copyup the new parent into the branch we process */
21007 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21008 +       if (err >= 0) {
21009 +               if (d_really_is_negative(dentry)) {
21010 +                       au_set_h_dptr(dentry, btop, NULL);
21011 +                       au_set_dbtop(dentry, bcpup);
21012 +                       au_set_dbbot(dentry, bcpup);
21013 +               }
21014 +               AuDebugOn(add_entry
21015 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21016 +                         && !au_h_dptr(dentry, bcpup));
21017 +       }
21018 +
21019 +out:
21020 +       dput(parent);
21021 +       return err;
21022 +}
21023 +
21024 +/* ---------------------------------------------------------------------- */
21025 +
21026 +void au_pin_hdir_unlock(struct au_pin *p)
21027 +{
21028 +       if (p->hdir)
21029 +               au_hn_inode_unlock(p->hdir);
21030 +}
21031 +
21032 +int au_pin_hdir_lock(struct au_pin *p)
21033 +{
21034 +       int err;
21035 +
21036 +       err = 0;
21037 +       if (!p->hdir)
21038 +               goto out;
21039 +
21040 +       /* even if an error happens later, keep this lock */
21041 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21042 +
21043 +       err = -EBUSY;
21044 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21045 +               goto out;
21046 +
21047 +       err = 0;
21048 +       if (p->h_dentry)
21049 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21050 +                                 p->h_parent, p->br);
21051 +
21052 +out:
21053 +       return err;
21054 +}
21055 +
21056 +int au_pin_hdir_relock(struct au_pin *p)
21057 +{
21058 +       int err, i;
21059 +       struct inode *h_i;
21060 +       struct dentry *h_d[] = {
21061 +               p->h_dentry,
21062 +               p->h_parent
21063 +       };
21064 +
21065 +       err = au_pin_hdir_lock(p);
21066 +       if (unlikely(err))
21067 +               goto out;
21068 +
21069 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21070 +               if (!h_d[i])
21071 +                       continue;
21072 +               if (d_is_positive(h_d[i])) {
21073 +                       h_i = d_inode(h_d[i]);
21074 +                       err = !h_i->i_nlink;
21075 +               }
21076 +       }
21077 +
21078 +out:
21079 +       return err;
21080 +}
21081 +
21082 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21083 +{
21084 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21085 +}
21086 +
21087 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21088 +{
21089 +       if (p->hdir) {
21090 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21091 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21092 +               au_pin_hdir_set_owner(p, current);
21093 +       }
21094 +}
21095 +
21096 +void au_pin_hdir_release(struct au_pin *p)
21097 +{
21098 +       if (p->hdir) {
21099 +               au_pin_hdir_set_owner(p, p->task);
21100 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21101 +       }
21102 +}
21103 +
21104 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21105 +{
21106 +       if (pin && pin->parent)
21107 +               return au_h_dptr(pin->parent, pin->bindex);
21108 +       return NULL;
21109 +}
21110 +
21111 +void au_unpin(struct au_pin *p)
21112 +{
21113 +       if (p->hdir)
21114 +               au_pin_hdir_unlock(p);
21115 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21116 +               vfsub_mnt_drop_write(p->h_mnt);
21117 +       if (!p->hdir)
21118 +               return;
21119 +
21120 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21121 +               di_read_unlock(p->parent, AuLock_IR);
21122 +       iput(p->hdir->hi_inode);
21123 +       dput(p->parent);
21124 +       p->parent = NULL;
21125 +       p->hdir = NULL;
21126 +       p->h_mnt = NULL;
21127 +       /* do not clear p->task */
21128 +}
21129 +
21130 +int au_do_pin(struct au_pin *p)
21131 +{
21132 +       int err;
21133 +       struct super_block *sb;
21134 +       struct inode *h_dir;
21135 +
21136 +       err = 0;
21137 +       sb = p->dentry->d_sb;
21138 +       p->br = au_sbr(sb, p->bindex);
21139 +       if (IS_ROOT(p->dentry)) {
21140 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21141 +                       p->h_mnt = au_br_mnt(p->br);
21142 +                       err = vfsub_mnt_want_write(p->h_mnt);
21143 +                       if (unlikely(err)) {
21144 +                               au_fclr_pin(p->flags, MNT_WRITE);
21145 +                               goto out_err;
21146 +                       }
21147 +               }
21148 +               goto out;
21149 +       }
21150 +
21151 +       p->h_dentry = NULL;
21152 +       if (p->bindex <= au_dbbot(p->dentry))
21153 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21154 +
21155 +       p->parent = dget_parent(p->dentry);
21156 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21157 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21158 +
21159 +       h_dir = NULL;
21160 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21161 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21162 +       if (p->hdir)
21163 +               h_dir = p->hdir->hi_inode;
21164 +
21165 +       /*
21166 +        * udba case, or
21167 +        * if DI_LOCKED is not set, then p->parent may be different
21168 +        * and h_parent can be NULL.
21169 +        */
21170 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21171 +               err = -EBUSY;
21172 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21173 +                       di_read_unlock(p->parent, AuLock_IR);
21174 +               dput(p->parent);
21175 +               p->parent = NULL;
21176 +               goto out_err;
21177 +       }
21178 +
21179 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21180 +               p->h_mnt = au_br_mnt(p->br);
21181 +               err = vfsub_mnt_want_write(p->h_mnt);
21182 +               if (unlikely(err)) {
21183 +                       au_fclr_pin(p->flags, MNT_WRITE);
21184 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21185 +                               di_read_unlock(p->parent, AuLock_IR);
21186 +                       dput(p->parent);
21187 +                       p->parent = NULL;
21188 +                       goto out_err;
21189 +               }
21190 +       }
21191 +
21192 +       au_igrab(h_dir);
21193 +       err = au_pin_hdir_lock(p);
21194 +       if (!err)
21195 +               goto out; /* success */
21196 +
21197 +       au_unpin(p);
21198 +
21199 +out_err:
21200 +       pr_err("err %d\n", err);
21201 +       err = au_busy_or_stale();
21202 +out:
21203 +       return err;
21204 +}
21205 +
21206 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21207 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21208 +                unsigned int udba, unsigned char flags)
21209 +{
21210 +       p->dentry = dentry;
21211 +       p->udba = udba;
21212 +       p->lsc_di = lsc_di;
21213 +       p->lsc_hi = lsc_hi;
21214 +       p->flags = flags;
21215 +       p->bindex = bindex;
21216 +
21217 +       p->parent = NULL;
21218 +       p->hdir = NULL;
21219 +       p->h_mnt = NULL;
21220 +
21221 +       p->h_dentry = NULL;
21222 +       p->h_parent = NULL;
21223 +       p->br = NULL;
21224 +       p->task = current;
21225 +}
21226 +
21227 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21228 +          unsigned int udba, unsigned char flags)
21229 +{
21230 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21231 +                   udba, flags);
21232 +       return au_do_pin(pin);
21233 +}
21234 +
21235 +/* ---------------------------------------------------------------------- */
21236 +
21237 +/*
21238 + * ->setattr() and ->getattr() are called in various cases.
21239 + * chmod, stat: dentry is revalidated.
21240 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21241 + *               unhashed.
21242 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21243 + */
21244 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21245 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21246 +{
21247 +       int err;
21248 +       struct dentry *parent;
21249 +
21250 +       err = 0;
21251 +       if (au_digen_test(dentry, sigen)) {
21252 +               parent = dget_parent(dentry);
21253 +               di_read_lock_parent(parent, AuLock_IR);
21254 +               err = au_refresh_dentry(dentry, parent);
21255 +               di_read_unlock(parent, AuLock_IR);
21256 +               dput(parent);
21257 +       }
21258 +
21259 +       AuTraceErr(err);
21260 +       return err;
21261 +}
21262 +
21263 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21264 +                    struct au_icpup_args *a)
21265 +{
21266 +       int err;
21267 +       loff_t sz;
21268 +       aufs_bindex_t btop, ibtop;
21269 +       struct dentry *hi_wh, *parent;
21270 +       struct inode *inode;
21271 +       struct au_wr_dir_args wr_dir_args = {
21272 +               .force_btgt     = -1,
21273 +               .flags          = 0
21274 +       };
21275 +
21276 +       if (d_is_dir(dentry))
21277 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21278 +       /* plink or hi_wh() case */
21279 +       btop = au_dbtop(dentry);
21280 +       inode = d_inode(dentry);
21281 +       ibtop = au_ibtop(inode);
21282 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21283 +               wr_dir_args.force_btgt = ibtop;
21284 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21285 +       if (unlikely(err < 0))
21286 +               goto out;
21287 +       a->btgt = err;
21288 +       if (err != btop)
21289 +               au_fset_icpup(a->flags, DID_CPUP);
21290 +
21291 +       err = 0;
21292 +       a->pin_flags = AuPin_MNT_WRITE;
21293 +       parent = NULL;
21294 +       if (!IS_ROOT(dentry)) {
21295 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21296 +               parent = dget_parent(dentry);
21297 +               di_write_lock_parent(parent);
21298 +       }
21299 +
21300 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21301 +       if (unlikely(err))
21302 +               goto out_parent;
21303 +
21304 +       sz = -1;
21305 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21306 +       a->h_inode = d_inode(a->h_path.dentry);
21307 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21308 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21309 +               if (ia->ia_size < i_size_read(a->h_inode))
21310 +                       sz = ia->ia_size;
21311 +               inode_unlock_shared(a->h_inode);
21312 +       }
21313 +
21314 +       hi_wh = NULL;
21315 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21316 +               hi_wh = au_hi_wh(inode, a->btgt);
21317 +               if (!hi_wh) {
21318 +                       struct au_cp_generic cpg = {
21319 +                               .dentry = dentry,
21320 +                               .bdst   = a->btgt,
21321 +                               .bsrc   = -1,
21322 +                               .len    = sz,
21323 +                               .pin    = &a->pin
21324 +                       };
21325 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21326 +                       if (unlikely(err))
21327 +                               goto out_unlock;
21328 +                       hi_wh = au_hi_wh(inode, a->btgt);
21329 +                       /* todo: revalidate hi_wh? */
21330 +               }
21331 +       }
21332 +
21333 +       if (parent) {
21334 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21335 +               di_downgrade_lock(parent, AuLock_IR);
21336 +               dput(parent);
21337 +               parent = NULL;
21338 +       }
21339 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21340 +               goto out; /* success */
21341 +
21342 +       if (!d_unhashed(dentry)) {
21343 +               struct au_cp_generic cpg = {
21344 +                       .dentry = dentry,
21345 +                       .bdst   = a->btgt,
21346 +                       .bsrc   = btop,
21347 +                       .len    = sz,
21348 +                       .pin    = &a->pin,
21349 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21350 +               };
21351 +               err = au_sio_cpup_simple(&cpg);
21352 +               if (!err)
21353 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21354 +       } else if (!hi_wh)
21355 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21356 +       else
21357 +               a->h_path.dentry = hi_wh; /* do not dget here */
21358 +
21359 +out_unlock:
21360 +       a->h_inode = d_inode(a->h_path.dentry);
21361 +       if (!err)
21362 +               goto out; /* success */
21363 +       au_unpin(&a->pin);
21364 +out_parent:
21365 +       if (parent) {
21366 +               di_write_unlock(parent);
21367 +               dput(parent);
21368 +       }
21369 +out:
21370 +       if (!err)
21371 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21372 +       return err;
21373 +}
21374 +
21375 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
21376 +                       struct iattr *ia)
21377 +{
21378 +       int err;
21379 +       struct inode *inode, *delegated;
21380 +       struct super_block *sb;
21381 +       struct file *file;
21382 +       struct au_icpup_args *a;
21383 +       struct user_namespace *h_userns;
21384 +
21385 +       inode = d_inode(dentry);
21386 +       IMustLock(inode);
21387 +
21388 +       err = setattr_prepare(userns, dentry, ia);
21389 +       if (unlikely(err))
21390 +               goto out;
21391 +
21392 +       err = -ENOMEM;
21393 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21394 +       if (unlikely(!a))
21395 +               goto out;
21396 +
21397 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21398 +               ia->ia_valid &= ~ATTR_MODE;
21399 +
21400 +       file = NULL;
21401 +       sb = dentry->d_sb;
21402 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21403 +       if (unlikely(err))
21404 +               goto out_kfree;
21405 +
21406 +       if (ia->ia_valid & ATTR_FILE) {
21407 +               /* currently ftruncate(2) only */
21408 +               AuDebugOn(!d_is_reg(dentry));
21409 +               file = ia->ia_file;
21410 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21411 +                                           /*fi_lsc*/0);
21412 +               if (unlikely(err))
21413 +                       goto out_si;
21414 +               ia->ia_file = au_hf_top(file);
21415 +               a->udba = AuOpt_UDBA_NONE;
21416 +       } else {
21417 +               /* fchmod() doesn't pass ia_file */
21418 +               a->udba = au_opt_udba(sb);
21419 +               di_write_lock_child(dentry);
21420 +               /* no d_unlinked(), to set UDBA_NONE for root */
21421 +               if (d_unhashed(dentry))
21422 +                       a->udba = AuOpt_UDBA_NONE;
21423 +               if (a->udba != AuOpt_UDBA_NONE) {
21424 +                       AuDebugOn(IS_ROOT(dentry));
21425 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21426 +                       if (unlikely(err))
21427 +                               goto out_dentry;
21428 +               }
21429 +       }
21430 +
21431 +       err = au_pin_and_icpup(dentry, ia, a);
21432 +       if (unlikely(err < 0))
21433 +               goto out_dentry;
21434 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21435 +               ia->ia_file = NULL;
21436 +               ia->ia_valid &= ~ATTR_FILE;
21437 +       }
21438 +
21439 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21440 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21441 +           == (ATTR_MODE | ATTR_CTIME)) {
21442 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21443 +               if (unlikely(err))
21444 +                       goto out_unlock;
21445 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21446 +                  && (ia->ia_valid & ATTR_CTIME)) {
21447 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21448 +               if (unlikely(err))
21449 +                       goto out_unlock;
21450 +       }
21451 +
21452 +       if (ia->ia_valid & ATTR_SIZE) {
21453 +               struct file *f;
21454 +
21455 +               if (ia->ia_size < i_size_read(inode))
21456 +                       /* unmap only */
21457 +                       truncate_setsize(inode, ia->ia_size);
21458 +
21459 +               f = NULL;
21460 +               if (ia->ia_valid & ATTR_FILE)
21461 +                       f = ia->ia_file;
21462 +               inode_unlock(a->h_inode);
21463 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21464 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21465 +       } else {
21466 +               delegated = NULL;
21467 +               while (1) {
21468 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21469 +                       if (delegated) {
21470 +                               err = break_deleg_wait(&delegated);
21471 +                               if (!err)
21472 +                                       continue;
21473 +                       }
21474 +                       break;
21475 +               }
21476 +       }
21477 +       /*
21478 +        * regardless aufs 'acl' option setting.
21479 +        * why don't all acl-aware fs call this func from their ->setattr()?
21480 +        */
21481 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
21482 +               h_userns = mnt_user_ns(a->h_path.mnt);
21483 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
21484 +       }
21485 +       if (!err)
21486 +               au_cpup_attr_changeable(inode);
21487 +
21488 +out_unlock:
21489 +       inode_unlock(a->h_inode);
21490 +       au_unpin(&a->pin);
21491 +       if (unlikely(err))
21492 +               au_update_dbtop(dentry);
21493 +out_dentry:
21494 +       di_write_unlock(dentry);
21495 +       if (file) {
21496 +               fi_write_unlock(file);
21497 +               ia->ia_file = file;
21498 +               ia->ia_valid |= ATTR_FILE;
21499 +       }
21500 +out_si:
21501 +       si_read_unlock(sb);
21502 +out_kfree:
21503 +       au_kfree_rcu(a);
21504 +out:
21505 +       AuTraceErr(err);
21506 +       return err;
21507 +}
21508 +
21509 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21510 +static int au_h_path_to_set_attr(struct dentry *dentry,
21511 +                                struct au_icpup_args *a, struct path *h_path)
21512 +{
21513 +       int err;
21514 +       struct super_block *sb;
21515 +
21516 +       sb = dentry->d_sb;
21517 +       a->udba = au_opt_udba(sb);
21518 +       /* no d_unlinked(), to set UDBA_NONE for root */
21519 +       if (d_unhashed(dentry))
21520 +               a->udba = AuOpt_UDBA_NONE;
21521 +       if (a->udba != AuOpt_UDBA_NONE) {
21522 +               AuDebugOn(IS_ROOT(dentry));
21523 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21524 +               if (unlikely(err))
21525 +                       goto out;
21526 +       }
21527 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21528 +       if (unlikely(err < 0))
21529 +               goto out;
21530 +
21531 +       h_path->dentry = a->h_path.dentry;
21532 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21533 +
21534 +out:
21535 +       return err;
21536 +}
21537 +
21538 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21539 +                 struct au_sxattr *arg)
21540 +{
21541 +       int err;
21542 +       struct path h_path;
21543 +       struct super_block *sb;
21544 +       struct au_icpup_args *a;
21545 +       struct inode *h_inode;
21546 +       struct user_namespace *h_userns;
21547 +
21548 +       IMustLock(inode);
21549 +
21550 +       err = -ENOMEM;
21551 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21552 +       if (unlikely(!a))
21553 +               goto out;
21554 +
21555 +       sb = dentry->d_sb;
21556 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21557 +       if (unlikely(err))
21558 +               goto out_kfree;
21559 +
21560 +       h_path.dentry = NULL;   /* silence gcc */
21561 +       di_write_lock_child(dentry);
21562 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21563 +       if (unlikely(err))
21564 +               goto out_di;
21565 +       h_userns = mnt_user_ns(h_path.mnt);
21566 +
21567 +       inode_unlock(a->h_inode);
21568 +       switch (arg->type) {
21569 +       case AU_XATTR_SET:
21570 +               AuDebugOn(d_is_negative(h_path.dentry));
21571 +               err = vfsub_setxattr(h_userns, h_path.dentry,
21572 +                                    arg->u.set.name, arg->u.set.value,
21573 +                                    arg->u.set.size, arg->u.set.flags);
21574 +               break;
21575 +       case AU_ACL_SET:
21576 +               err = -EOPNOTSUPP;
21577 +               h_inode = d_inode(h_path.dentry);
21578 +               if (h_inode->i_op->set_acl) {
21579 +                       /* this will call posix_acl_update_mode */
21580 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
21581 +                                                    arg->u.acl_set.acl,
21582 +                                                    arg->u.acl_set.type);
21583 +               }
21584 +               break;
21585 +       }
21586 +       if (!err)
21587 +               au_cpup_attr_timesizes(inode);
21588 +
21589 +       au_unpin(&a->pin);
21590 +       if (unlikely(err))
21591 +               au_update_dbtop(dentry);
21592 +
21593 +out_di:
21594 +       di_write_unlock(dentry);
21595 +       si_read_unlock(sb);
21596 +out_kfree:
21597 +       au_kfree_rcu(a);
21598 +out:
21599 +       AuTraceErr(err);
21600 +       return err;
21601 +}
21602 +#endif
21603 +
21604 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21605 +                            unsigned int nlink)
21606 +{
21607 +       unsigned int n;
21608 +
21609 +       inode->i_mode = st->mode;
21610 +       /* don't i_[ug]id_write() here */
21611 +       inode->i_uid = st->uid;
21612 +       inode->i_gid = st->gid;
21613 +       inode->i_atime = st->atime;
21614 +       inode->i_mtime = st->mtime;
21615 +       inode->i_ctime = st->ctime;
21616 +
21617 +       au_cpup_attr_nlink(inode, /*force*/0);
21618 +       if (S_ISDIR(inode->i_mode)) {
21619 +               n = inode->i_nlink;
21620 +               n -= nlink;
21621 +               n += st->nlink;
21622 +               smp_mb(); /* for i_nlink */
21623 +               /* 0 can happen */
21624 +               set_nlink(inode, n);
21625 +       }
21626 +
21627 +       spin_lock(&inode->i_lock);
21628 +       inode->i_blocks = st->blocks;
21629 +       i_size_write(inode, st->size);
21630 +       spin_unlock(&inode->i_lock);
21631 +}
21632 +
21633 +/*
21634 + * common routine for aufs_getattr() and au_getxattr().
21635 + * returns zero or negative (an error).
21636 + * @dentry will be read-locked in success.
21637 + */
21638 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21639 +                     struct path *h_path, int locked)
21640 +{
21641 +       int err;
21642 +       unsigned int mnt_flags, sigen;
21643 +       unsigned char udba_none;
21644 +       aufs_bindex_t bindex;
21645 +       struct super_block *sb, *h_sb;
21646 +
21647 +       h_path->mnt = NULL;
21648 +       h_path->dentry = NULL;
21649 +
21650 +       err = 0;
21651 +       sb = dentry->d_sb;
21652 +       mnt_flags = au_mntflags(sb);
21653 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21654 +
21655 +       if (unlikely(locked))
21656 +               goto body; /* skip locking dinfo */
21657 +
21658 +       /* support fstat(2) */
21659 +       if (!d_unlinked(dentry) && !udba_none) {
21660 +               sigen = au_sigen(sb);
21661 +               err = au_digen_test(dentry, sigen);
21662 +               if (!err) {
21663 +                       di_read_lock_child(dentry, AuLock_IR);
21664 +                       err = au_dbrange_test(dentry);
21665 +                       if (unlikely(err)) {
21666 +                               di_read_unlock(dentry, AuLock_IR);
21667 +                               goto out;
21668 +                       }
21669 +               } else {
21670 +                       AuDebugOn(IS_ROOT(dentry));
21671 +                       di_write_lock_child(dentry);
21672 +                       err = au_dbrange_test(dentry);
21673 +                       if (!err)
21674 +                               err = au_reval_for_attr(dentry, sigen);
21675 +                       if (!err)
21676 +                               di_downgrade_lock(dentry, AuLock_IR);
21677 +                       else {
21678 +                               di_write_unlock(dentry);
21679 +                               goto out;
21680 +                       }
21681 +               }
21682 +       } else
21683 +               di_read_lock_child(dentry, AuLock_IR);
21684 +
21685 +body:
21686 +       if (!inode) {
21687 +               inode = d_inode(dentry);
21688 +               if (unlikely(!inode))
21689 +                       goto out;
21690 +       }
21691 +       bindex = au_ibtop(inode);
21692 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21693 +       h_sb = h_path->mnt->mnt_sb;
21694 +       if (!force
21695 +           && !au_test_fs_bad_iattr(h_sb)
21696 +           && udba_none)
21697 +               goto out; /* success */
21698 +
21699 +       if (au_dbtop(dentry) == bindex)
21700 +               h_path->dentry = au_h_dptr(dentry, bindex);
21701 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21702 +               h_path->dentry = au_plink_lkup(inode, bindex);
21703 +               if (IS_ERR(h_path->dentry))
21704 +                       /* pretending success */
21705 +                       h_path->dentry = NULL;
21706 +               else
21707 +                       dput(h_path->dentry);
21708 +       }
21709 +
21710 +out:
21711 +       return err;
21712 +}
21713 +
21714 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
21715 +                       struct kstat *st, u32 request, unsigned int query)
21716 +{
21717 +       int err;
21718 +       unsigned char positive;
21719 +       struct path h_path;
21720 +       struct dentry *dentry;
21721 +       struct inode *inode;
21722 +       struct super_block *sb;
21723 +
21724 +       dentry = path->dentry;
21725 +       inode = d_inode(dentry);
21726 +       sb = dentry->d_sb;
21727 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21728 +       if (unlikely(err))
21729 +               goto out;
21730 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21731 +                               /*locked*/0);
21732 +       if (unlikely(err))
21733 +               goto out_si;
21734 +       if (unlikely(!h_path.dentry))
21735 +               /* illegally overlapped or something */
21736 +               goto out_fill; /* pretending success */
21737 +
21738 +       positive = d_is_positive(h_path.dentry);
21739 +       if (positive)
21740 +               /* no vfsub version */
21741 +               err = vfs_getattr(&h_path, st, request, query);
21742 +       if (!err) {
21743 +               if (positive)
21744 +                       au_refresh_iattr(inode, st,
21745 +                                        d_inode(h_path.dentry)->i_nlink);
21746 +               goto out_fill; /* success */
21747 +       }
21748 +       AuTraceErr(err);
21749 +       goto out_di;
21750 +
21751 +out_fill:
21752 +       generic_fillattr(userns, inode, st);
21753 +out_di:
21754 +       di_read_unlock(dentry, AuLock_IR);
21755 +out_si:
21756 +       si_read_unlock(sb);
21757 +out:
21758 +       AuTraceErr(err);
21759 +       return err;
21760 +}
21761 +
21762 +/* ---------------------------------------------------------------------- */
21763 +
21764 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21765 +                                struct delayed_call *done)
21766 +{
21767 +       const char *ret;
21768 +       struct dentry *h_dentry;
21769 +       struct inode *h_inode;
21770 +       int err;
21771 +       aufs_bindex_t bindex;
21772 +
21773 +       ret = NULL; /* suppress a warning */
21774 +       err = -ECHILD;
21775 +       if (!dentry)
21776 +               goto out;
21777 +
21778 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21779 +       if (unlikely(err))
21780 +               goto out;
21781 +
21782 +       err = au_d_hashed_positive(dentry);
21783 +       if (unlikely(err))
21784 +               goto out_unlock;
21785 +
21786 +       err = -EINVAL;
21787 +       inode = d_inode(dentry);
21788 +       bindex = au_ibtop(inode);
21789 +       h_inode = au_h_iptr(inode, bindex);
21790 +       if (unlikely(!h_inode->i_op->get_link))
21791 +               goto out_unlock;
21792 +
21793 +       err = -EBUSY;
21794 +       h_dentry = NULL;
21795 +       if (au_dbtop(dentry) <= bindex) {
21796 +               h_dentry = au_h_dptr(dentry, bindex);
21797 +               if (h_dentry)
21798 +                       dget(h_dentry);
21799 +       }
21800 +       if (!h_dentry) {
21801 +               h_dentry = d_find_any_alias(h_inode);
21802 +               if (IS_ERR(h_dentry)) {
21803 +                       err = PTR_ERR(h_dentry);
21804 +                       goto out_unlock;
21805 +               }
21806 +       }
21807 +       if (unlikely(!h_dentry))
21808 +               goto out_unlock;
21809 +
21810 +       err = 0;
21811 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21812 +       AuDbgDentry(h_dentry);
21813 +       ret = vfs_get_link(h_dentry, done);
21814 +       dput(h_dentry);
21815 +       if (IS_ERR(ret))
21816 +               err = PTR_ERR(ret);
21817 +
21818 +out_unlock:
21819 +       aufs_read_unlock(dentry, AuLock_IR);
21820 +out:
21821 +       if (unlikely(err))
21822 +               ret = ERR_PTR(err);
21823 +       AuTraceErrPtr(ret);
21824 +       return ret;
21825 +}
21826 +
21827 +/* ---------------------------------------------------------------------- */
21828 +
21829 +static int au_is_special(struct inode *inode)
21830 +{
21831 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21832 +}
21833 +
21834 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21835 +                           int flags)
21836 +{
21837 +       int err;
21838 +       aufs_bindex_t bindex;
21839 +       struct super_block *sb;
21840 +       struct inode *h_inode;
21841 +       struct vfsmount *h_mnt;
21842 +
21843 +       sb = inode->i_sb;
21844 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21845 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21846 +
21847 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21848 +       lockdep_off();
21849 +       si_read_lock(sb, AuLock_FLUSH);
21850 +       ii_write_lock_child(inode);
21851 +
21852 +       err = 0;
21853 +       bindex = au_ibtop(inode);
21854 +       h_inode = au_h_iptr(inode, bindex);
21855 +       if (!au_test_ro(sb, bindex, inode)) {
21856 +               h_mnt = au_sbr_mnt(sb, bindex);
21857 +               err = vfsub_mnt_want_write(h_mnt);
21858 +               if (!err) {
21859 +                       err = vfsub_update_time(h_inode, ts, flags);
21860 +                       vfsub_mnt_drop_write(h_mnt);
21861 +               }
21862 +       } else if (au_is_special(h_inode)) {
21863 +               /*
21864 +                * Never copy-up here.
21865 +                * These special files may already be opened and used for
21866 +                * communicating. If we copied it up, then the communication
21867 +                * would be corrupted.
21868 +                */
21869 +               AuWarn1("timestamps for i%lu are ignored "
21870 +                       "since it is on readonly branch (hi%lu).\n",
21871 +                       inode->i_ino, h_inode->i_ino);
21872 +       } else if (flags & ~S_ATIME) {
21873 +               err = -EIO;
21874 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21875 +               AuDebugOn(1);
21876 +       }
21877 +
21878 +       if (!err)
21879 +               au_cpup_attr_timesizes(inode);
21880 +       ii_write_unlock(inode);
21881 +       si_read_unlock(sb);
21882 +       lockdep_on();
21883 +
21884 +       if (!err && (flags & S_VERSION))
21885 +               inode_inc_iversion(inode);
21886 +
21887 +       return err;
21888 +}
21889 +
21890 +/* ---------------------------------------------------------------------- */
21891 +
21892 +/* no getattr version will be set by module.c:aufs_init() */
21893 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21894 +       aufs_iop[] = {
21895 +       [AuIop_SYMLINK] = {
21896 +               .permission     = aufs_permission,
21897 +#ifdef CONFIG_FS_POSIX_ACL
21898 +               .get_acl        = aufs_get_acl,
21899 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
21900 +#endif
21901 +
21902 +               .setattr        = aufs_setattr,
21903 +               .getattr        = aufs_getattr,
21904 +
21905 +#ifdef CONFIG_AUFS_XATTR
21906 +               .listxattr      = aufs_listxattr,
21907 +#endif
21908 +
21909 +               .get_link       = aufs_get_link,
21910 +
21911 +               /* .update_time = aufs_update_time */
21912 +       },
21913 +       [AuIop_DIR] = {
21914 +               .create         = aufs_create,
21915 +               .lookup         = aufs_lookup,
21916 +               .link           = aufs_link,
21917 +               .unlink         = aufs_unlink,
21918 +               .symlink        = aufs_symlink,
21919 +               .mkdir          = aufs_mkdir,
21920 +               .rmdir          = aufs_rmdir,
21921 +               .mknod          = aufs_mknod,
21922 +               .rename         = aufs_rename,
21923 +
21924 +               .permission     = aufs_permission,
21925 +#ifdef CONFIG_FS_POSIX_ACL
21926 +               .get_acl        = aufs_get_acl,
21927 +               .set_acl        = aufs_set_acl,
21928 +#endif
21929 +
21930 +               .setattr        = aufs_setattr,
21931 +               .getattr        = aufs_getattr,
21932 +
21933 +#ifdef CONFIG_AUFS_XATTR
21934 +               .listxattr      = aufs_listxattr,
21935 +#endif
21936 +
21937 +               .update_time    = aufs_update_time,
21938 +               .atomic_open    = aufs_atomic_open,
21939 +               .tmpfile        = aufs_tmpfile
21940 +       },
21941 +       [AuIop_OTHER] = {
21942 +               .permission     = aufs_permission,
21943 +#ifdef CONFIG_FS_POSIX_ACL
21944 +               .get_acl        = aufs_get_acl,
21945 +               .set_acl        = aufs_set_acl,
21946 +#endif
21947 +
21948 +               .setattr        = aufs_setattr,
21949 +               .getattr        = aufs_getattr,
21950 +
21951 +#ifdef CONFIG_AUFS_XATTR
21952 +               .listxattr      = aufs_listxattr,
21953 +#endif
21954 +
21955 +               .update_time    = aufs_update_time
21956 +       }
21957 +};
21958 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21959 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21960 +++ linux/fs/aufs/i_op_del.c    2021-11-01 23:48:34.209692595 +0100
21961 @@ -0,0 +1,522 @@
21962 +// SPDX-License-Identifier: GPL-2.0
21963 +/*
21964 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21965 + *
21966 + * This program, aufs is free software; you can redistribute it and/or modify
21967 + * it under the terms of the GNU General Public License as published by
21968 + * the Free Software Foundation; either version 2 of the License, or
21969 + * (at your option) any later version.
21970 + *
21971 + * This program is distributed in the hope that it will be useful,
21972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21974 + * GNU General Public License for more details.
21975 + *
21976 + * You should have received a copy of the GNU General Public License
21977 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21978 + */
21979 +
21980 +/*
21981 + * inode operations (del entry)
21982 + */
21983 +
21984 +#include <linux/iversion.h>
21985 +#include "aufs.h"
21986 +
21987 +/*
21988 + * decide if a new whiteout for @dentry is necessary or not.
21989 + * when it is necessary, prepare the parent dir for the upper branch whose
21990 + * branch index is @bcpup for creation. the actual creation of the whiteout will
21991 + * be done by caller.
21992 + * return value:
21993 + * 0: wh is unnecessary
21994 + * plus: wh is necessary
21995 + * minus: error
21996 + */
21997 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
21998 +{
21999 +       int need_wh, err;
22000 +       aufs_bindex_t btop;
22001 +       struct super_block *sb;
22002 +
22003 +       sb = dentry->d_sb;
22004 +       btop = au_dbtop(dentry);
22005 +       if (*bcpup < 0) {
22006 +               *bcpup = btop;
22007 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22008 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22009 +                       *bcpup = err;
22010 +                       if (unlikely(err < 0))
22011 +                               goto out;
22012 +               }
22013 +       } else
22014 +               AuDebugOn(btop < *bcpup
22015 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22016 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22017 +
22018 +       if (*bcpup != btop) {
22019 +               err = au_cpup_dirs(dentry, *bcpup);
22020 +               if (unlikely(err))
22021 +                       goto out;
22022 +               need_wh = 1;
22023 +       } else {
22024 +               struct au_dinfo *dinfo, *tmp;
22025 +
22026 +               need_wh = -ENOMEM;
22027 +               dinfo = au_di(dentry);
22028 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22029 +               if (tmp) {
22030 +                       au_di_cp(tmp, dinfo);
22031 +                       au_di_swap(tmp, dinfo);
22032 +                       /* returns the number of positive dentries */
22033 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22034 +                                                /* AuLkup_IGNORE_PERM */ 0);
22035 +                       au_di_swap(tmp, dinfo);
22036 +                       au_rw_write_unlock(&tmp->di_rwsem);
22037 +                       au_di_free(tmp);
22038 +               }
22039 +       }
22040 +       AuDbg("need_wh %d\n", need_wh);
22041 +       err = need_wh;
22042 +
22043 +out:
22044 +       return err;
22045 +}
22046 +
22047 +/*
22048 + * simple tests for the del-entry operations.
22049 + * following the checks in vfs, plus the parent-child relationship.
22050 + */
22051 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22052 +              struct dentry *h_parent, int isdir)
22053 +{
22054 +       int err;
22055 +       umode_t h_mode;
22056 +       struct dentry *h_dentry, *h_latest;
22057 +       struct inode *h_inode;
22058 +       struct path h_ppath;
22059 +       struct super_block *sb;
22060 +       struct au_branch *br;
22061 +       struct user_namespace *h_userns;
22062 +
22063 +       h_dentry = au_h_dptr(dentry, bindex);
22064 +       if (d_really_is_positive(dentry)) {
22065 +               err = -ENOENT;
22066 +               if (unlikely(d_is_negative(h_dentry)))
22067 +                       goto out;
22068 +               h_inode = d_inode(h_dentry);
22069 +               if (unlikely(!h_inode->i_nlink))
22070 +                       goto out;
22071 +
22072 +               h_mode = h_inode->i_mode;
22073 +               if (!isdir) {
22074 +                       err = -EISDIR;
22075 +                       if (unlikely(S_ISDIR(h_mode)))
22076 +                               goto out;
22077 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22078 +                       err = -ENOTDIR;
22079 +                       goto out;
22080 +               }
22081 +       } else {
22082 +               /* rename(2) case */
22083 +               err = -EIO;
22084 +               if (unlikely(d_is_positive(h_dentry)))
22085 +                       goto out;
22086 +       }
22087 +
22088 +       err = -ENOENT;
22089 +       /* expected parent dir is locked */
22090 +       if (unlikely(h_parent != h_dentry->d_parent))
22091 +               goto out;
22092 +       err = 0;
22093 +
22094 +       /*
22095 +        * rmdir a dir may break the consistency on some filesystem.
22096 +        * let's try heavy test.
22097 +        */
22098 +       err = -EACCES;
22099 +       sb = dentry->d_sb;
22100 +       br = au_sbr(sb, bindex);
22101 +       h_userns = au_br_userns(br);
22102 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
22103 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
22104 +                                      MAY_EXEC | MAY_WRITE)))
22105 +               goto out;
22106 +
22107 +       h_ppath.dentry = h_parent;
22108 +       h_ppath.mnt = au_br_mnt(br);
22109 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
22110 +       err = -EIO;
22111 +       if (IS_ERR(h_latest))
22112 +               goto out;
22113 +       if (h_latest == h_dentry)
22114 +               err = 0;
22115 +       dput(h_latest);
22116 +
22117 +out:
22118 +       return err;
22119 +}
22120 +
22121 +/*
22122 + * decide the branch where we operate for @dentry. the branch index will be set
22123 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22124 + * dir for reverting.
22125 + * when a new whiteout is necessary, create it.
22126 + */
22127 +static struct dentry*
22128 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22129 +                   struct au_dtime *dt, struct au_pin *pin)
22130 +{
22131 +       struct dentry *wh_dentry;
22132 +       struct super_block *sb;
22133 +       struct path h_path;
22134 +       int err, need_wh;
22135 +       unsigned int udba;
22136 +       aufs_bindex_t bcpup;
22137 +
22138 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22139 +       wh_dentry = ERR_PTR(need_wh);
22140 +       if (unlikely(need_wh < 0))
22141 +               goto out;
22142 +
22143 +       sb = dentry->d_sb;
22144 +       udba = au_opt_udba(sb);
22145 +       bcpup = *rbcpup;
22146 +       err = au_pin(pin, dentry, bcpup, udba,
22147 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22148 +       wh_dentry = ERR_PTR(err);
22149 +       if (unlikely(err))
22150 +               goto out;
22151 +
22152 +       h_path.dentry = au_pinned_h_parent(pin);
22153 +       if (udba != AuOpt_UDBA_NONE
22154 +           && au_dbtop(dentry) == bcpup) {
22155 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22156 +               wh_dentry = ERR_PTR(err);
22157 +               if (unlikely(err))
22158 +                       goto out_unpin;
22159 +       }
22160 +
22161 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22162 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22163 +       wh_dentry = NULL;
22164 +       if (!need_wh)
22165 +               goto out; /* success, no need to create whiteout */
22166 +
22167 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22168 +       if (IS_ERR(wh_dentry))
22169 +               goto out_unpin;
22170 +
22171 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22172 +       goto out; /* success */
22173 +
22174 +out_unpin:
22175 +       au_unpin(pin);
22176 +out:
22177 +       return wh_dentry;
22178 +}
22179 +
22180 +/*
22181 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22182 + * in order to be revertible and save time for removing many child whiteouts
22183 + * under the dir.
22184 + * returns 1 when there are too many child whiteout and caller should remove
22185 + * them asynchronously. returns 0 when the number of children is enough small to
22186 + * remove now or the branch fs is a remote fs.
22187 + * otherwise return an error.
22188 + */
22189 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22190 +                          struct au_nhash *whlist, struct inode *dir)
22191 +{
22192 +       int rmdir_later, err, dirwh;
22193 +       struct dentry *h_dentry;
22194 +       struct super_block *sb;
22195 +       struct inode *inode;
22196 +
22197 +       sb = dentry->d_sb;
22198 +       SiMustAnyLock(sb);
22199 +       h_dentry = au_h_dptr(dentry, bindex);
22200 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22201 +       if (unlikely(err))
22202 +               goto out;
22203 +
22204 +       /* stop monitoring */
22205 +       inode = d_inode(dentry);
22206 +       au_hn_free(au_hi(inode, bindex));
22207 +
22208 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22209 +               dirwh = au_sbi(sb)->si_dirwh;
22210 +               rmdir_later = (dirwh <= 1);
22211 +               if (!rmdir_later)
22212 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22213 +                                                             dirwh);
22214 +               if (rmdir_later)
22215 +                       return rmdir_later;
22216 +       }
22217 +
22218 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22219 +       if (unlikely(err)) {
22220 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22221 +                       h_dentry, bindex, err);
22222 +               err = 0;
22223 +       }
22224 +
22225 +out:
22226 +       AuTraceErr(err);
22227 +       return err;
22228 +}
22229 +
22230 +/*
22231 + * final procedure for deleting a entry.
22232 + * maintain dentry and iattr.
22233 + */
22234 +static void epilog(struct inode *dir, struct dentry *dentry,
22235 +                  aufs_bindex_t bindex)
22236 +{
22237 +       struct inode *inode;
22238 +
22239 +       inode = d_inode(dentry);
22240 +       d_drop(dentry);
22241 +       inode->i_ctime = dir->i_ctime;
22242 +
22243 +       au_dir_ts(dir, bindex);
22244 +       inode_inc_iversion(dir);
22245 +}
22246 +
22247 +/*
22248 + * when an error happened, remove the created whiteout and revert everything.
22249 + */
22250 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22251 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22252 +                    struct dentry *dentry, struct au_dtime *dt)
22253 +{
22254 +       int rerr;
22255 +       struct path h_path = {
22256 +               .dentry = wh_dentry,
22257 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22258 +       };
22259 +
22260 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22261 +       if (!rerr) {
22262 +               au_set_dbwh(dentry, bwh);
22263 +               au_dtime_revert(dt);
22264 +               return 0;
22265 +       }
22266 +
22267 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22268 +       return -EIO;
22269 +}
22270 +
22271 +/* ---------------------------------------------------------------------- */
22272 +
22273 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22274 +{
22275 +       int err;
22276 +       aufs_bindex_t bwh, bindex, btop;
22277 +       struct inode *inode, *h_dir, *delegated;
22278 +       struct dentry *parent, *wh_dentry;
22279 +       /* to reduce stack size */
22280 +       struct {
22281 +               struct au_dtime dt;
22282 +               struct au_pin pin;
22283 +               struct path h_path;
22284 +       } *a;
22285 +
22286 +       IMustLock(dir);
22287 +
22288 +       err = -ENOMEM;
22289 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22290 +       if (unlikely(!a))
22291 +               goto out;
22292 +
22293 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22294 +       if (unlikely(err))
22295 +               goto out_free;
22296 +       err = au_d_hashed_positive(dentry);
22297 +       if (unlikely(err))
22298 +               goto out_unlock;
22299 +       inode = d_inode(dentry);
22300 +       IMustLock(inode);
22301 +       err = -EISDIR;
22302 +       if (unlikely(d_is_dir(dentry)))
22303 +               goto out_unlock; /* possible? */
22304 +
22305 +       btop = au_dbtop(dentry);
22306 +       bwh = au_dbwh(dentry);
22307 +       bindex = -1;
22308 +       parent = dentry->d_parent; /* dir inode is locked */
22309 +       di_write_lock_parent(parent);
22310 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22311 +                                       &a->pin);
22312 +       err = PTR_ERR(wh_dentry);
22313 +       if (IS_ERR(wh_dentry))
22314 +               goto out_parent;
22315 +
22316 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22317 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22318 +       dget(a->h_path.dentry);
22319 +       if (bindex == btop) {
22320 +               h_dir = au_pinned_h_dir(&a->pin);
22321 +               delegated = NULL;
22322 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22323 +               if (unlikely(err == -EWOULDBLOCK)) {
22324 +                       pr_warn("cannot retry for NFSv4 delegation"
22325 +                               " for an internal unlink\n");
22326 +                       iput(delegated);
22327 +               }
22328 +       } else {
22329 +               /* dir inode is locked */
22330 +               h_dir = d_inode(wh_dentry->d_parent);
22331 +               IMustLock(h_dir);
22332 +               err = 0;
22333 +       }
22334 +
22335 +       if (!err) {
22336 +               vfsub_drop_nlink(inode);
22337 +               epilog(dir, dentry, bindex);
22338 +
22339 +               /* update target timestamps */
22340 +               if (bindex == btop) {
22341 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22342 +                       /*ignore*/
22343 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22344 +               } else
22345 +                       /* todo: this timestamp may be reverted later */
22346 +                       inode->i_ctime = h_dir->i_ctime;
22347 +               goto out_unpin; /* success */
22348 +       }
22349 +
22350 +       /* revert */
22351 +       if (wh_dentry) {
22352 +               int rerr;
22353 +
22354 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22355 +                                &a->dt);
22356 +               if (rerr)
22357 +                       err = rerr;
22358 +       }
22359 +
22360 +out_unpin:
22361 +       au_unpin(&a->pin);
22362 +       dput(wh_dentry);
22363 +       dput(a->h_path.dentry);
22364 +out_parent:
22365 +       di_write_unlock(parent);
22366 +out_unlock:
22367 +       aufs_read_unlock(dentry, AuLock_DW);
22368 +out_free:
22369 +       au_kfree_rcu(a);
22370 +out:
22371 +       return err;
22372 +}
22373 +
22374 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22375 +{
22376 +       int err, rmdir_later;
22377 +       aufs_bindex_t bwh, bindex, btop;
22378 +       struct inode *inode;
22379 +       struct dentry *parent, *wh_dentry, *h_dentry;
22380 +       struct au_whtmp_rmdir *args;
22381 +       /* to reduce stack size */
22382 +       struct {
22383 +               struct au_dtime dt;
22384 +               struct au_pin pin;
22385 +       } *a;
22386 +
22387 +       IMustLock(dir);
22388 +
22389 +       err = -ENOMEM;
22390 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22391 +       if (unlikely(!a))
22392 +               goto out;
22393 +
22394 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22395 +       if (unlikely(err))
22396 +               goto out_free;
22397 +       err = au_alive_dir(dentry);
22398 +       if (unlikely(err))
22399 +               goto out_unlock;
22400 +       inode = d_inode(dentry);
22401 +       IMustLock(inode);
22402 +       err = -ENOTDIR;
22403 +       if (unlikely(!d_is_dir(dentry)))
22404 +               goto out_unlock; /* possible? */
22405 +
22406 +       err = -ENOMEM;
22407 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22408 +       if (unlikely(!args))
22409 +               goto out_unlock;
22410 +
22411 +       parent = dentry->d_parent; /* dir inode is locked */
22412 +       di_write_lock_parent(parent);
22413 +       err = au_test_empty(dentry, &args->whlist);
22414 +       if (unlikely(err))
22415 +               goto out_parent;
22416 +
22417 +       btop = au_dbtop(dentry);
22418 +       bwh = au_dbwh(dentry);
22419 +       bindex = -1;
22420 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22421 +                                       &a->pin);
22422 +       err = PTR_ERR(wh_dentry);
22423 +       if (IS_ERR(wh_dentry))
22424 +               goto out_parent;
22425 +
22426 +       h_dentry = au_h_dptr(dentry, btop);
22427 +       dget(h_dentry);
22428 +       rmdir_later = 0;
22429 +       if (bindex == btop) {
22430 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22431 +               if (err > 0) {
22432 +                       rmdir_later = err;
22433 +                       err = 0;
22434 +               }
22435 +       } else {
22436 +               /* stop monitoring */
22437 +               au_hn_free(au_hi(inode, btop));
22438 +
22439 +               /* dir inode is locked */
22440 +               IMustLock(d_inode(wh_dentry->d_parent));
22441 +               err = 0;
22442 +       }
22443 +
22444 +       if (!err) {
22445 +               vfsub_dead_dir(inode);
22446 +               au_set_dbdiropq(dentry, -1);
22447 +               epilog(dir, dentry, bindex);
22448 +
22449 +               if (rmdir_later) {
22450 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22451 +                       args = NULL;
22452 +               }
22453 +
22454 +               goto out_unpin; /* success */
22455 +       }
22456 +
22457 +       /* revert */
22458 +       AuLabel(revert);
22459 +       if (wh_dentry) {
22460 +               int rerr;
22461 +
22462 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22463 +                                &a->dt);
22464 +               if (rerr)
22465 +                       err = rerr;
22466 +       }
22467 +
22468 +out_unpin:
22469 +       au_unpin(&a->pin);
22470 +       dput(wh_dentry);
22471 +       dput(h_dentry);
22472 +out_parent:
22473 +       di_write_unlock(parent);
22474 +       if (args)
22475 +               au_whtmp_rmdir_free(args);
22476 +out_unlock:
22477 +       aufs_read_unlock(dentry, AuLock_DW);
22478 +out_free:
22479 +       au_kfree_rcu(a);
22480 +out:
22481 +       AuTraceErr(err);
22482 +       return err;
22483 +}
22484 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22485 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22486 +++ linux/fs/aufs/i_op_ren.c    2021-11-01 23:48:34.209692595 +0100
22487 @@ -0,0 +1,1257 @@
22488 +// SPDX-License-Identifier: GPL-2.0
22489 +/*
22490 + * Copyright (C) 2005-2021 Junjiro R. Okajima
22491 + *
22492 + * This program, aufs is free software; you can redistribute it and/or modify
22493 + * it under the terms of the GNU General Public License as published by
22494 + * the Free Software Foundation; either version 2 of the License, or
22495 + * (at your option) any later version.
22496 + *
22497 + * This program is distributed in the hope that it will be useful,
22498 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22499 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22500 + * GNU General Public License for more details.
22501 + *
22502 + * You should have received a copy of the GNU General Public License
22503 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22504 + */
22505 +
22506 +/*
22507 + * inode operation (rename entry)
22508 + * todo: this is crazy monster
22509 + */
22510 +
22511 +#include <linux/iversion.h>
22512 +#include "aufs.h"
22513 +
22514 +enum { AuSRC, AuDST, AuSrcDst };
22515 +enum { AuPARENT, AuCHILD, AuParentChild };
22516 +
22517 +#define AuRen_ISDIR_SRC                1
22518 +#define AuRen_ISDIR_DST                (1 << 1)
22519 +#define AuRen_ISSAMEDIR                (1 << 2)
22520 +#define AuRen_WHSRC            (1 << 3)
22521 +#define AuRen_WHDST            (1 << 4)
22522 +#define AuRen_MNT_WRITE                (1 << 5)
22523 +#define AuRen_DT_DSTDIR                (1 << 6)
22524 +#define AuRen_DIROPQ_SRC       (1 << 7)
22525 +#define AuRen_DIROPQ_DST       (1 << 8)
22526 +#define AuRen_DIRREN           (1 << 9)
22527 +#define AuRen_DROPPED_SRC      (1 << 10)
22528 +#define AuRen_DROPPED_DST      (1 << 11)
22529 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22530 +#define au_fset_ren(flags, name) \
22531 +       do { (flags) |= AuRen_##name; } while (0)
22532 +#define au_fclr_ren(flags, name) \
22533 +       do { (flags) &= ~AuRen_##name; } while (0)
22534 +
22535 +#ifndef CONFIG_AUFS_DIRREN
22536 +#undef AuRen_DIRREN
22537 +#define AuRen_DIRREN           0
22538 +#endif
22539 +
22540 +struct au_ren_args {
22541 +       struct {
22542 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22543 +                       *wh_dentry;
22544 +               struct inode *dir, *inode;
22545 +               struct au_hinode *hdir, *hinode;
22546 +               struct au_dtime dt[AuParentChild];
22547 +               aufs_bindex_t btop, bdiropq;
22548 +       } sd[AuSrcDst];
22549 +
22550 +#define src_dentry     sd[AuSRC].dentry
22551 +#define src_dir                sd[AuSRC].dir
22552 +#define src_inode      sd[AuSRC].inode
22553 +#define src_h_dentry   sd[AuSRC].h_dentry
22554 +#define src_parent     sd[AuSRC].parent
22555 +#define src_h_parent   sd[AuSRC].h_parent
22556 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22557 +#define src_hdir       sd[AuSRC].hdir
22558 +#define src_hinode     sd[AuSRC].hinode
22559 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22560 +#define src_dt         sd[AuSRC].dt
22561 +#define src_btop       sd[AuSRC].btop
22562 +#define src_bdiropq    sd[AuSRC].bdiropq
22563 +
22564 +#define dst_dentry     sd[AuDST].dentry
22565 +#define dst_dir                sd[AuDST].dir
22566 +#define dst_inode      sd[AuDST].inode
22567 +#define dst_h_dentry   sd[AuDST].h_dentry
22568 +#define dst_parent     sd[AuDST].parent
22569 +#define dst_h_parent   sd[AuDST].h_parent
22570 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22571 +#define dst_hdir       sd[AuDST].hdir
22572 +#define dst_hinode     sd[AuDST].hinode
22573 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22574 +#define dst_dt         sd[AuDST].dt
22575 +#define dst_btop       sd[AuDST].btop
22576 +#define dst_bdiropq    sd[AuDST].bdiropq
22577 +
22578 +       struct dentry *h_trap;
22579 +       struct au_branch *br;
22580 +       struct path h_path;
22581 +       struct au_nhash whlist;
22582 +       aufs_bindex_t btgt, src_bwh;
22583 +
22584 +       struct {
22585 +               unsigned short auren_flags;
22586 +               unsigned char flags;    /* syscall parameter */
22587 +               unsigned char exchange;
22588 +       } __packed;
22589 +
22590 +       struct au_whtmp_rmdir *thargs;
22591 +       struct dentry *h_dst;
22592 +       struct au_hinode *h_root;
22593 +};
22594 +
22595 +/* ---------------------------------------------------------------------- */
22596 +
22597 +/*
22598 + * functions for reverting.
22599 + * when an error happened in a single rename systemcall, we should revert
22600 + * everything as if nothing happened.
22601 + * we don't need to revert the copied-up/down the parent dir since they are
22602 + * harmless.
22603 + */
22604 +
22605 +#define RevertFailure(fmt, ...) do { \
22606 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22607 +               ##__VA_ARGS__, err, rerr); \
22608 +       err = -EIO; \
22609 +} while (0)
22610 +
22611 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22612 +{
22613 +       int rerr;
22614 +       struct dentry *d;
22615 +#define src_or_dst(member) a->sd[idx].member
22616 +
22617 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22618 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22619 +       rerr = au_diropq_remove(d, a->btgt);
22620 +       au_hn_inode_unlock(src_or_dst(hinode));
22621 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22622 +       if (rerr)
22623 +               RevertFailure("remove diropq %pd", d);
22624 +
22625 +#undef src_or_dst_
22626 +}
22627 +
22628 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22629 +{
22630 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22631 +               au_ren_do_rev_diropq(err, a, AuSRC);
22632 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22633 +               au_ren_do_rev_diropq(err, a, AuDST);
22634 +}
22635 +
22636 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22637 +{
22638 +       int rerr;
22639 +       struct inode *delegated;
22640 +       struct path h_ppath = {
22641 +               .dentry = a->src_h_parent,
22642 +               .mnt    = a->h_path.mnt
22643 +       };
22644 +
22645 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
22646 +       rerr = PTR_ERR(a->h_path.dentry);
22647 +       if (IS_ERR(a->h_path.dentry)) {
22648 +               RevertFailure("lkup one %pd", a->src_dentry);
22649 +               return;
22650 +       }
22651 +
22652 +       delegated = NULL;
22653 +       rerr = vfsub_rename(a->dst_h_dir,
22654 +                           au_h_dptr(a->src_dentry, a->btgt),
22655 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22656 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22657 +               pr_warn("cannot retry for NFSv4 delegation"
22658 +                       " for an internal rename\n");
22659 +               iput(delegated);
22660 +       }
22661 +       d_drop(a->h_path.dentry);
22662 +       dput(a->h_path.dentry);
22663 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22664 +       if (rerr)
22665 +               RevertFailure("rename %pd", a->src_dentry);
22666 +}
22667 +
22668 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22669 +{
22670 +       int rerr;
22671 +       struct inode *delegated;
22672 +       struct path h_ppath = {
22673 +               .dentry = a->dst_h_parent,
22674 +               .mnt    = a->h_path.mnt
22675 +       };
22676 +
22677 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
22678 +       rerr = PTR_ERR(a->h_path.dentry);
22679 +       if (IS_ERR(a->h_path.dentry)) {
22680 +               RevertFailure("lkup one %pd", a->dst_dentry);
22681 +               return;
22682 +       }
22683 +       if (d_is_positive(a->h_path.dentry)) {
22684 +               d_drop(a->h_path.dentry);
22685 +               dput(a->h_path.dentry);
22686 +               return;
22687 +       }
22688 +
22689 +       delegated = NULL;
22690 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22691 +                           &delegated, a->flags);
22692 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22693 +               pr_warn("cannot retry for NFSv4 delegation"
22694 +                       " for an internal rename\n");
22695 +               iput(delegated);
22696 +       }
22697 +       d_drop(a->h_path.dentry);
22698 +       dput(a->h_path.dentry);
22699 +       if (!rerr)
22700 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22701 +       else
22702 +               RevertFailure("rename %pd", a->h_dst);
22703 +}
22704 +
22705 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22706 +{
22707 +       int rerr;
22708 +
22709 +       a->h_path.dentry = a->src_wh_dentry;
22710 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22711 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22712 +       if (rerr)
22713 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22714 +}
22715 +#undef RevertFailure
22716 +
22717 +/* ---------------------------------------------------------------------- */
22718 +
22719 +/*
22720 + * when we have to copyup the renaming entry, do it with the rename-target name
22721 + * in order to minimize the cost (the later actual rename is unnecessary).
22722 + * otherwise rename it on the target branch.
22723 + */
22724 +static int au_ren_or_cpup(struct au_ren_args *a)
22725 +{
22726 +       int err;
22727 +       struct dentry *d;
22728 +       struct inode *delegated;
22729 +
22730 +       d = a->src_dentry;
22731 +       if (au_dbtop(d) == a->btgt) {
22732 +               a->h_path.dentry = a->dst_h_dentry;
22733 +               AuDebugOn(au_dbtop(d) != a->btgt);
22734 +               delegated = NULL;
22735 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22736 +                                  a->dst_h_dir, &a->h_path, &delegated,
22737 +                                  a->flags);
22738 +               if (unlikely(err == -EWOULDBLOCK)) {
22739 +                       pr_warn("cannot retry for NFSv4 delegation"
22740 +                               " for an internal rename\n");
22741 +                       iput(delegated);
22742 +               }
22743 +       } else
22744 +               BUG();
22745 +
22746 +       if (!err && a->h_dst)
22747 +               /* it will be set to dinfo later */
22748 +               dget(a->h_dst);
22749 +
22750 +       return err;
22751 +}
22752 +
22753 +/* cf. aufs_rmdir() */
22754 +static int au_ren_del_whtmp(struct au_ren_args *a)
22755 +{
22756 +       int err;
22757 +       struct inode *dir;
22758 +
22759 +       dir = a->dst_dir;
22760 +       SiMustAnyLock(dir->i_sb);
22761 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22762 +                                    au_sbi(dir->i_sb)->si_dirwh)
22763 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22764 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22765 +               if (unlikely(err))
22766 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22767 +                               "ignored.\n", a->h_dst, err);
22768 +       } else {
22769 +               au_nhash_wh_free(&a->thargs->whlist);
22770 +               a->thargs->whlist = a->whlist;
22771 +               a->whlist.nh_num = 0;
22772 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22773 +               dput(a->h_dst);
22774 +               a->thargs = NULL;
22775 +       }
22776 +
22777 +       return 0;
22778 +}
22779 +
22780 +/* make it 'opaque' dir. */
22781 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22782 +{
22783 +       int err;
22784 +       struct dentry *d, *diropq;
22785 +#define src_or_dst(member) a->sd[idx].member
22786 +
22787 +       err = 0;
22788 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22789 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22790 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22791 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22792 +       diropq = au_diropq_create(d, a->btgt);
22793 +       au_hn_inode_unlock(src_or_dst(hinode));
22794 +       if (IS_ERR(diropq))
22795 +               err = PTR_ERR(diropq);
22796 +       else
22797 +               dput(diropq);
22798 +
22799 +#undef src_or_dst_
22800 +       return err;
22801 +}
22802 +
22803 +static int au_ren_diropq(struct au_ren_args *a)
22804 +{
22805 +       int err;
22806 +       unsigned char always;
22807 +       struct dentry *d;
22808 +
22809 +       err = 0;
22810 +       d = a->dst_dentry; /* already renamed on the branch */
22811 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22812 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22813 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22814 +           && a->btgt != au_dbdiropq(a->src_dentry)
22815 +           && (a->dst_wh_dentry
22816 +               || a->btgt <= au_dbdiropq(d)
22817 +               /* hide the lower to keep xino */
22818 +               /* the lowers may not be a dir, but we hide them anyway */
22819 +               || a->btgt < au_dbbot(d)
22820 +               || always)) {
22821 +               AuDbg("here\n");
22822 +               err = au_ren_do_diropq(a, AuSRC);
22823 +               if (unlikely(err))
22824 +                       goto out;
22825 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22826 +       }
22827 +       if (!a->exchange)
22828 +               goto out; /* success */
22829 +
22830 +       d = a->src_dentry; /* already renamed on the branch */
22831 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22832 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22833 +           && (a->btgt < au_dbdiropq(d)
22834 +               || a->btgt < au_dbbot(d)
22835 +               || always)) {
22836 +               AuDbgDentry(a->src_dentry);
22837 +               AuDbgDentry(a->dst_dentry);
22838 +               err = au_ren_do_diropq(a, AuDST);
22839 +               if (unlikely(err))
22840 +                       goto out_rev_src;
22841 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22842 +       }
22843 +       goto out; /* success */
22844 +
22845 +out_rev_src:
22846 +       AuDbg("err %d, reverting src\n", err);
22847 +       au_ren_rev_diropq(err, a);
22848 +out:
22849 +       return err;
22850 +}
22851 +
22852 +static int do_rename(struct au_ren_args *a)
22853 +{
22854 +       int err;
22855 +       struct dentry *d, *h_d;
22856 +
22857 +       if (!a->exchange) {
22858 +               /* prepare workqueue args for asynchronous rmdir */
22859 +               h_d = a->dst_h_dentry;
22860 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22861 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22862 +                   && d_is_positive(h_d)) {
22863 +                       err = -ENOMEM;
22864 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22865 +                                                        GFP_NOFS);
22866 +                       if (unlikely(!a->thargs))
22867 +                               goto out;
22868 +                       a->h_dst = dget(h_d);
22869 +               }
22870 +
22871 +               /* create whiteout for src_dentry */
22872 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22873 +                       a->src_bwh = au_dbwh(a->src_dentry);
22874 +                       AuDebugOn(a->src_bwh >= 0);
22875 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22876 +                                                       a->src_h_parent);
22877 +                       err = PTR_ERR(a->src_wh_dentry);
22878 +                       if (IS_ERR(a->src_wh_dentry))
22879 +                               goto out_thargs;
22880 +               }
22881 +
22882 +               /* lookup whiteout for dentry */
22883 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22884 +                       h_d = au_wh_lkup(a->dst_h_parent,
22885 +                                        &a->dst_dentry->d_name, a->br);
22886 +                       err = PTR_ERR(h_d);
22887 +                       if (IS_ERR(h_d))
22888 +                               goto out_whsrc;
22889 +                       if (d_is_negative(h_d))
22890 +                               dput(h_d);
22891 +                       else
22892 +                               a->dst_wh_dentry = h_d;
22893 +               }
22894 +
22895 +               /* rename dentry to tmpwh */
22896 +               if (a->thargs) {
22897 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22898 +                       if (unlikely(err))
22899 +                               goto out_whdst;
22900 +
22901 +                       d = a->dst_dentry;
22902 +                       au_set_h_dptr(d, a->btgt, NULL);
22903 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22904 +                       if (unlikely(err))
22905 +                               goto out_whtmp;
22906 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22907 +               }
22908 +       }
22909 +
22910 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22911 +#if 0 /* debugging */
22912 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22913 +              && d_is_positive(a->dst_h_dentry)
22914 +              && a->src_btop != a->btgt);
22915 +#endif
22916 +
22917 +       /* rename by vfs_rename or cpup */
22918 +       err = au_ren_or_cpup(a);
22919 +       if (unlikely(err))
22920 +               /* leave the copied-up one */
22921 +               goto out_whtmp;
22922 +
22923 +       /* make dir opaque */
22924 +       err = au_ren_diropq(a);
22925 +       if (unlikely(err))
22926 +               goto out_rename;
22927 +
22928 +       /* update target timestamps */
22929 +       if (a->exchange) {
22930 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22931 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22932 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22933 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22934 +       }
22935 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22936 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22937 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22938 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22939 +
22940 +       if (!a->exchange) {
22941 +               /* remove whiteout for dentry */
22942 +               if (a->dst_wh_dentry) {
22943 +                       a->h_path.dentry = a->dst_wh_dentry;
22944 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22945 +                                                 a->dst_dentry);
22946 +                       if (unlikely(err))
22947 +                               goto out_diropq;
22948 +               }
22949 +
22950 +               /* remove whtmp */
22951 +               if (a->thargs)
22952 +                       au_ren_del_whtmp(a); /* ignore this error */
22953 +
22954 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22955 +       }
22956 +       err = 0;
22957 +       goto out_success;
22958 +
22959 +out_diropq:
22960 +       au_ren_rev_diropq(err, a);
22961 +out_rename:
22962 +       au_ren_rev_rename(err, a);
22963 +       dput(a->h_dst);
22964 +out_whtmp:
22965 +       if (a->thargs)
22966 +               au_ren_rev_whtmp(err, a);
22967 +out_whdst:
22968 +       dput(a->dst_wh_dentry);
22969 +       a->dst_wh_dentry = NULL;
22970 +out_whsrc:
22971 +       if (a->src_wh_dentry)
22972 +               au_ren_rev_whsrc(err, a);
22973 +out_success:
22974 +       dput(a->src_wh_dentry);
22975 +       dput(a->dst_wh_dentry);
22976 +out_thargs:
22977 +       if (a->thargs) {
22978 +               dput(a->h_dst);
22979 +               au_whtmp_rmdir_free(a->thargs);
22980 +               a->thargs = NULL;
22981 +       }
22982 +out:
22983 +       return err;
22984 +}
22985 +
22986 +/* ---------------------------------------------------------------------- */
22987 +
22988 +/*
22989 + * test if @dentry dir can be rename destination or not.
22990 + * success means, it is a logically empty dir.
22991 + */
22992 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
22993 +{
22994 +       return au_test_empty(dentry, whlist);
22995 +}
22996 +
22997 +/*
22998 + * test if @a->src_dentry dir can be rename source or not.
22999 + * if it can, return 0.
23000 + * success means,
23001 + * - it is a logically empty dir.
23002 + * - or, it exists on writable branch and has no children including whiteouts
23003 + *   on the lower branch unless DIRREN is on.
23004 + */
23005 +static int may_rename_srcdir(struct au_ren_args *a)
23006 +{
23007 +       int err;
23008 +       unsigned int rdhash;
23009 +       aufs_bindex_t btop, btgt;
23010 +       struct dentry *dentry;
23011 +       struct super_block *sb;
23012 +       struct au_sbinfo *sbinfo;
23013 +
23014 +       dentry = a->src_dentry;
23015 +       sb = dentry->d_sb;
23016 +       sbinfo = au_sbi(sb);
23017 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23018 +               au_fset_ren(a->auren_flags, DIRREN);
23019 +
23020 +       btgt = a->btgt;
23021 +       btop = au_dbtop(dentry);
23022 +       if (btop != btgt) {
23023 +               struct au_nhash whlist;
23024 +
23025 +               SiMustAnyLock(sb);
23026 +               rdhash = sbinfo->si_rdhash;
23027 +               if (!rdhash)
23028 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23029 +                                                          dentry));
23030 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23031 +               if (unlikely(err))
23032 +                       goto out;
23033 +               err = au_test_empty(dentry, &whlist);
23034 +               au_nhash_wh_free(&whlist);
23035 +               goto out;
23036 +       }
23037 +
23038 +       if (btop == au_dbtaildir(dentry))
23039 +               return 0; /* success */
23040 +
23041 +       err = au_test_empty_lower(dentry);
23042 +
23043 +out:
23044 +       if (err == -ENOTEMPTY) {
23045 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23046 +                       err = 0;
23047 +               } else {
23048 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23049 +                               "branches, is not supported\n");
23050 +                       err = -EXDEV;
23051 +               }
23052 +       }
23053 +       return err;
23054 +}
23055 +
23056 +/* side effect: sets whlist and h_dentry */
23057 +static int au_ren_may_dir(struct au_ren_args *a)
23058 +{
23059 +       int err;
23060 +       unsigned int rdhash;
23061 +       struct dentry *d;
23062 +
23063 +       d = a->dst_dentry;
23064 +       SiMustAnyLock(d->d_sb);
23065 +
23066 +       err = 0;
23067 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23068 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23069 +               if (!rdhash)
23070 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23071 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23072 +               if (unlikely(err))
23073 +                       goto out;
23074 +
23075 +               if (!a->exchange) {
23076 +                       au_set_dbtop(d, a->dst_btop);
23077 +                       err = may_rename_dstdir(d, &a->whlist);
23078 +                       au_set_dbtop(d, a->btgt);
23079 +               } else
23080 +                       err = may_rename_srcdir(a);
23081 +       }
23082 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23083 +       if (unlikely(err))
23084 +               goto out;
23085 +
23086 +       d = a->src_dentry;
23087 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23088 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23089 +               err = may_rename_srcdir(a);
23090 +               if (unlikely(err)) {
23091 +                       au_nhash_wh_free(&a->whlist);
23092 +                       a->whlist.nh_num = 0;
23093 +               }
23094 +       }
23095 +out:
23096 +       return err;
23097 +}
23098 +
23099 +/* ---------------------------------------------------------------------- */
23100 +
23101 +/*
23102 + * simple tests for rename.
23103 + * following the checks in vfs, plus the parent-child relationship.
23104 + */
23105 +static int au_may_ren(struct au_ren_args *a)
23106 +{
23107 +       int err, isdir;
23108 +       struct inode *h_inode;
23109 +
23110 +       if (a->src_btop == a->btgt) {
23111 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23112 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23113 +               if (unlikely(err))
23114 +                       goto out;
23115 +               err = -EINVAL;
23116 +               if (unlikely(a->src_h_dentry == a->h_trap))
23117 +                       goto out;
23118 +       }
23119 +
23120 +       err = 0;
23121 +       if (a->dst_btop != a->btgt)
23122 +               goto out;
23123 +
23124 +       err = -ENOTEMPTY;
23125 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23126 +               goto out;
23127 +
23128 +       err = -EIO;
23129 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23130 +       if (d_really_is_negative(a->dst_dentry)) {
23131 +               if (d_is_negative(a->dst_h_dentry))
23132 +                       err = au_may_add(a->dst_dentry, a->btgt,
23133 +                                        a->dst_h_parent, isdir);
23134 +       } else {
23135 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23136 +                       goto out;
23137 +               h_inode = d_inode(a->dst_h_dentry);
23138 +               if (h_inode->i_nlink)
23139 +                       err = au_may_del(a->dst_dentry, a->btgt,
23140 +                                        a->dst_h_parent, isdir);
23141 +       }
23142 +
23143 +out:
23144 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23145 +               err = -EIO;
23146 +       AuTraceErr(err);
23147 +       return err;
23148 +}
23149 +
23150 +/* ---------------------------------------------------------------------- */
23151 +
23152 +/*
23153 + * locking order
23154 + * (VFS)
23155 + * - src_dir and dir by lock_rename()
23156 + * - inode if exists
23157 + * (aufs)
23158 + * - lock all
23159 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23160 + *     + si_read_lock
23161 + *     + di_write_lock2_child()
23162 + *       + di_write_lock_child()
23163 + *        + ii_write_lock_child()
23164 + *       + di_write_lock_child2()
23165 + *        + ii_write_lock_child2()
23166 + *     + src_parent and parent
23167 + *       + di_write_lock_parent()
23168 + *        + ii_write_lock_parent()
23169 + *       + di_write_lock_parent2()
23170 + *        + ii_write_lock_parent2()
23171 + *   + lower src_dir and dir by vfsub_lock_rename()
23172 + *   + verify the every relationships between child and parent. if any
23173 + *     of them failed, unlock all and return -EBUSY.
23174 + */
23175 +static void au_ren_unlock(struct au_ren_args *a)
23176 +{
23177 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23178 +                           a->dst_h_parent, a->dst_hdir);
23179 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23180 +           && a->h_root)
23181 +               au_hn_inode_unlock(a->h_root);
23182 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23183 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23184 +}
23185 +
23186 +static int au_ren_lock(struct au_ren_args *a)
23187 +{
23188 +       int err;
23189 +       unsigned int udba;
23190 +
23191 +       err = 0;
23192 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23193 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23194 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23195 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23196 +
23197 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23198 +       if (unlikely(err))
23199 +               goto out;
23200 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23201 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23202 +               struct dentry *root;
23203 +               struct inode *dir;
23204 +
23205 +               /*
23206 +                * sbinfo is already locked, so this ii_read_lock is
23207 +                * unnecessary. but our debugging feature checks it.
23208 +                */
23209 +               root = a->src_inode->i_sb->s_root;
23210 +               if (root != a->src_parent && root != a->dst_parent) {
23211 +                       dir = d_inode(root);
23212 +                       ii_read_lock_parent3(dir);
23213 +                       a->h_root = au_hi(dir, a->btgt);
23214 +                       ii_read_unlock(dir);
23215 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23216 +               }
23217 +       }
23218 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23219 +                                     a->dst_h_parent, a->dst_hdir);
23220 +       udba = au_opt_udba(a->src_dentry->d_sb);
23221 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23222 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23223 +               err = au_busy_or_stale();
23224 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23225 +               err = au_h_verify(a->src_h_dentry, udba,
23226 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23227 +                                 a->br);
23228 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23229 +               err = au_h_verify(a->dst_h_dentry, udba,
23230 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23231 +                                 a->br);
23232 +       if (!err)
23233 +               goto out; /* success */
23234 +
23235 +       err = au_busy_or_stale();
23236 +       au_ren_unlock(a);
23237 +
23238 +out:
23239 +       return err;
23240 +}
23241 +
23242 +/* ---------------------------------------------------------------------- */
23243 +
23244 +static void au_ren_refresh_dir(struct au_ren_args *a)
23245 +{
23246 +       struct inode *dir;
23247 +
23248 +       dir = a->dst_dir;
23249 +       inode_inc_iversion(dir);
23250 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23251 +               /* is this updating defined in POSIX? */
23252 +               au_cpup_attr_timesizes(a->src_inode);
23253 +               au_cpup_attr_nlink(dir, /*force*/1);
23254 +       }
23255 +       au_dir_ts(dir, a->btgt);
23256 +
23257 +       if (a->exchange) {
23258 +               dir = a->src_dir;
23259 +               inode_inc_iversion(dir);
23260 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23261 +                       /* is this updating defined in POSIX? */
23262 +                       au_cpup_attr_timesizes(a->dst_inode);
23263 +                       au_cpup_attr_nlink(dir, /*force*/1);
23264 +               }
23265 +               au_dir_ts(dir, a->btgt);
23266 +       }
23267 +
23268 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23269 +               return;
23270 +
23271 +       dir = a->src_dir;
23272 +       inode_inc_iversion(dir);
23273 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23274 +               au_cpup_attr_nlink(dir, /*force*/1);
23275 +       au_dir_ts(dir, a->btgt);
23276 +}
23277 +
23278 +static void au_ren_refresh(struct au_ren_args *a)
23279 +{
23280 +       aufs_bindex_t bbot, bindex;
23281 +       struct dentry *d, *h_d;
23282 +       struct inode *i, *h_i;
23283 +       struct super_block *sb;
23284 +
23285 +       d = a->dst_dentry;
23286 +       d_drop(d);
23287 +       if (a->h_dst)
23288 +               /* already dget-ed by au_ren_or_cpup() */
23289 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23290 +
23291 +       i = a->dst_inode;
23292 +       if (i) {
23293 +               if (!a->exchange) {
23294 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23295 +                               vfsub_drop_nlink(i);
23296 +                       else {
23297 +                               vfsub_dead_dir(i);
23298 +                               au_cpup_attr_timesizes(i);
23299 +                       }
23300 +                       au_update_dbrange(d, /*do_put_zero*/1);
23301 +               } else
23302 +                       au_cpup_attr_nlink(i, /*force*/1);
23303 +       } else {
23304 +               bbot = a->btgt;
23305 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23306 +                       au_set_h_dptr(d, bindex, NULL);
23307 +               bbot = au_dbbot(d);
23308 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23309 +                       au_set_h_dptr(d, bindex, NULL);
23310 +               au_update_dbrange(d, /*do_put_zero*/0);
23311 +       }
23312 +
23313 +       if (a->exchange
23314 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23315 +               d_drop(a->src_dentry);
23316 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23317 +                       au_set_dbwh(a->src_dentry, -1);
23318 +               return;
23319 +       }
23320 +
23321 +       d = a->src_dentry;
23322 +       au_set_dbwh(d, -1);
23323 +       bbot = au_dbbot(d);
23324 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23325 +               h_d = au_h_dptr(d, bindex);
23326 +               if (h_d)
23327 +                       au_set_h_dptr(d, bindex, NULL);
23328 +       }
23329 +       au_set_dbbot(d, a->btgt);
23330 +
23331 +       sb = d->d_sb;
23332 +       i = a->src_inode;
23333 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23334 +               return; /* success */
23335 +
23336 +       bbot = au_ibbot(i);
23337 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23338 +               h_i = au_h_iptr(i, bindex);
23339 +               if (h_i) {
23340 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23341 +                       /* ignore this error */
23342 +                       au_set_h_iptr(i, bindex, NULL, 0);
23343 +               }
23344 +       }
23345 +       au_set_ibbot(i, a->btgt);
23346 +}
23347 +
23348 +/* ---------------------------------------------------------------------- */
23349 +
23350 +/* mainly for link(2) and rename(2) */
23351 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23352 +{
23353 +       aufs_bindex_t bdiropq, bwh;
23354 +       struct dentry *parent;
23355 +       struct au_branch *br;
23356 +
23357 +       parent = dentry->d_parent;
23358 +       IMustLock(d_inode(parent)); /* dir is locked */
23359 +
23360 +       bdiropq = au_dbdiropq(parent);
23361 +       bwh = au_dbwh(dentry);
23362 +       br = au_sbr(dentry->d_sb, btgt);
23363 +       if (au_br_rdonly(br)
23364 +           || (0 <= bdiropq && bdiropq < btgt)
23365 +           || (0 <= bwh && bwh < btgt))
23366 +               btgt = -1;
23367 +
23368 +       AuDbg("btgt %d\n", btgt);
23369 +       return btgt;
23370 +}
23371 +
23372 +/* sets src_btop, dst_btop and btgt */
23373 +static int au_ren_wbr(struct au_ren_args *a)
23374 +{
23375 +       int err;
23376 +       struct au_wr_dir_args wr_dir_args = {
23377 +               /* .force_btgt  = -1, */
23378 +               .flags          = AuWrDir_ADD_ENTRY
23379 +       };
23380 +
23381 +       a->src_btop = au_dbtop(a->src_dentry);
23382 +       a->dst_btop = au_dbtop(a->dst_dentry);
23383 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23384 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23385 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23386 +       wr_dir_args.force_btgt = a->src_btop;
23387 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23388 +               wr_dir_args.force_btgt = a->dst_btop;
23389 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23390 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23391 +       a->btgt = err;
23392 +       if (a->exchange)
23393 +               au_update_dbtop(a->dst_dentry);
23394 +
23395 +       return err;
23396 +}
23397 +
23398 +static void au_ren_dt(struct au_ren_args *a)
23399 +{
23400 +       a->h_path.dentry = a->src_h_parent;
23401 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23402 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23403 +               a->h_path.dentry = a->dst_h_parent;
23404 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23405 +       }
23406 +
23407 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23408 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23409 +           && !a->exchange)
23410 +               return;
23411 +
23412 +       a->h_path.dentry = a->src_h_dentry;
23413 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23414 +       if (d_is_positive(a->dst_h_dentry)) {
23415 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23416 +               a->h_path.dentry = a->dst_h_dentry;
23417 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23418 +       }
23419 +}
23420 +
23421 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23422 +{
23423 +       struct dentry *h_d;
23424 +       struct inode *h_inode;
23425 +
23426 +       au_dtime_revert(a->src_dt + AuPARENT);
23427 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23428 +               au_dtime_revert(a->dst_dt + AuPARENT);
23429 +
23430 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23431 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23432 +               h_inode = d_inode(h_d);
23433 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23434 +               au_dtime_revert(a->src_dt + AuCHILD);
23435 +               inode_unlock(h_inode);
23436 +
23437 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23438 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23439 +                       h_inode = d_inode(h_d);
23440 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23441 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23442 +                       inode_unlock(h_inode);
23443 +               }
23444 +       }
23445 +}
23446 +
23447 +/* ---------------------------------------------------------------------- */
23448 +
23449 +int aufs_rename(struct user_namespace *userns,
23450 +               struct inode *_src_dir, struct dentry *_src_dentry,
23451 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23452 +               unsigned int _flags)
23453 +{
23454 +       int err, lock_flags;
23455 +       void *rev;
23456 +       /* reduce stack space */
23457 +       struct au_ren_args *a;
23458 +       struct au_pin pin;
23459 +
23460 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23461 +       IMustLock(_src_dir);
23462 +       IMustLock(_dst_dir);
23463 +
23464 +       err = -EINVAL;
23465 +       if (unlikely(_flags & RENAME_WHITEOUT))
23466 +               goto out;
23467 +
23468 +       err = -ENOMEM;
23469 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23470 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23471 +       if (unlikely(!a))
23472 +               goto out;
23473 +
23474 +       a->flags = _flags;
23475 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23476 +                    && RENAME_EXCHANGE > U8_MAX);
23477 +       a->exchange = _flags & RENAME_EXCHANGE;
23478 +       a->src_dir = _src_dir;
23479 +       a->src_dentry = _src_dentry;
23480 +       a->src_inode = NULL;
23481 +       if (d_really_is_positive(a->src_dentry))
23482 +               a->src_inode = d_inode(a->src_dentry);
23483 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23484 +       a->dst_dir = _dst_dir;
23485 +       a->dst_dentry = _dst_dentry;
23486 +       a->dst_inode = NULL;
23487 +       if (d_really_is_positive(a->dst_dentry))
23488 +               a->dst_inode = d_inode(a->dst_dentry);
23489 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23490 +       if (a->dst_inode) {
23491 +               /*
23492 +                * if EXCHANGE && src is non-dir && dst is dir,
23493 +                * dst is not locked.
23494 +                */
23495 +               /* IMustLock(a->dst_inode); */
23496 +               au_igrab(a->dst_inode);
23497 +       }
23498 +
23499 +       err = -ENOTDIR;
23500 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23501 +       if (d_is_dir(a->src_dentry)) {
23502 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23503 +               if (unlikely(!a->exchange
23504 +                            && d_really_is_positive(a->dst_dentry)
23505 +                            && !d_is_dir(a->dst_dentry)))
23506 +                       goto out_free;
23507 +               lock_flags |= AuLock_DIRS;
23508 +       }
23509 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23510 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23511 +               if (unlikely(!a->exchange
23512 +                            && d_really_is_positive(a->src_dentry)
23513 +                            && !d_is_dir(a->src_dentry)))
23514 +                       goto out_free;
23515 +               lock_flags |= AuLock_DIRS;
23516 +       }
23517 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23518 +                                       lock_flags);
23519 +       if (unlikely(err))
23520 +               goto out_free;
23521 +
23522 +       err = au_d_hashed_positive(a->src_dentry);
23523 +       if (unlikely(err))
23524 +               goto out_unlock;
23525 +       err = -ENOENT;
23526 +       if (a->dst_inode) {
23527 +               /*
23528 +                * If it is a dir, VFS unhash it before this
23529 +                * function. It means we cannot rely upon d_unhashed().
23530 +                */
23531 +               if (unlikely(!a->dst_inode->i_nlink))
23532 +                       goto out_unlock;
23533 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23534 +                       err = au_d_hashed_positive(a->dst_dentry);
23535 +                       if (unlikely(err && !a->exchange))
23536 +                               goto out_unlock;
23537 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23538 +                       goto out_unlock;
23539 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23540 +               goto out_unlock;
23541 +
23542 +       /*
23543 +        * is it possible?
23544 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23545 +        * there may exist a problem somewhere else.
23546 +        */
23547 +       err = -EINVAL;
23548 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23549 +               goto out_unlock;
23550 +
23551 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23552 +       di_write_lock_parent(a->dst_parent);
23553 +
23554 +       /* which branch we process */
23555 +       err = au_ren_wbr(a);
23556 +       if (unlikely(err < 0))
23557 +               goto out_parent;
23558 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23559 +       a->h_path.mnt = au_br_mnt(a->br);
23560 +
23561 +       /* are they available to be renamed */
23562 +       err = au_ren_may_dir(a);
23563 +       if (unlikely(err))
23564 +               goto out_children;
23565 +
23566 +       /* prepare the writable parent dir on the same branch */
23567 +       if (a->dst_btop == a->btgt) {
23568 +               au_fset_ren(a->auren_flags, WHDST);
23569 +       } else {
23570 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23571 +               if (unlikely(err))
23572 +                       goto out_children;
23573 +       }
23574 +
23575 +       err = 0;
23576 +       if (!a->exchange) {
23577 +               if (a->src_dir != a->dst_dir) {
23578 +                       /*
23579 +                        * this temporary unlock is safe,
23580 +                        * because both dir->i_mutex are locked.
23581 +                        */
23582 +                       di_write_unlock(a->dst_parent);
23583 +                       di_write_lock_parent(a->src_parent);
23584 +                       err = au_wr_dir_need_wh(a->src_dentry,
23585 +                                               au_ftest_ren(a->auren_flags,
23586 +                                                            ISDIR_SRC),
23587 +                                               &a->btgt);
23588 +                       di_write_unlock(a->src_parent);
23589 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23590 +                                             /*isdir*/1);
23591 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23592 +               } else
23593 +                       err = au_wr_dir_need_wh(a->src_dentry,
23594 +                                               au_ftest_ren(a->auren_flags,
23595 +                                                            ISDIR_SRC),
23596 +                                               &a->btgt);
23597 +       }
23598 +       if (unlikely(err < 0))
23599 +               goto out_children;
23600 +       if (err)
23601 +               au_fset_ren(a->auren_flags, WHSRC);
23602 +
23603 +       /* cpup src */
23604 +       if (a->src_btop != a->btgt) {
23605 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23606 +                            au_opt_udba(a->src_dentry->d_sb),
23607 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23608 +               if (!err) {
23609 +                       struct au_cp_generic cpg = {
23610 +                               .dentry = a->src_dentry,
23611 +                               .bdst   = a->btgt,
23612 +                               .bsrc   = a->src_btop,
23613 +                               .len    = -1,
23614 +                               .pin    = &pin,
23615 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23616 +                       };
23617 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23618 +                       err = au_sio_cpup_simple(&cpg);
23619 +                       au_unpin(&pin);
23620 +               }
23621 +               if (unlikely(err))
23622 +                       goto out_children;
23623 +               a->src_btop = a->btgt;
23624 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23625 +               if (!a->exchange)
23626 +                       au_fset_ren(a->auren_flags, WHSRC);
23627 +       }
23628 +
23629 +       /* cpup dst */
23630 +       if (a->exchange && a->dst_inode
23631 +           && a->dst_btop != a->btgt) {
23632 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23633 +                            au_opt_udba(a->dst_dentry->d_sb),
23634 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23635 +               if (!err) {
23636 +                       struct au_cp_generic cpg = {
23637 +                               .dentry = a->dst_dentry,
23638 +                               .bdst   = a->btgt,
23639 +                               .bsrc   = a->dst_btop,
23640 +                               .len    = -1,
23641 +                               .pin    = &pin,
23642 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23643 +                       };
23644 +                       err = au_sio_cpup_simple(&cpg);
23645 +                       au_unpin(&pin);
23646 +               }
23647 +               if (unlikely(err))
23648 +                       goto out_children;
23649 +               a->dst_btop = a->btgt;
23650 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23651 +       }
23652 +
23653 +       /* lock them all */
23654 +       err = au_ren_lock(a);
23655 +       if (unlikely(err))
23656 +               /* leave the copied-up one */
23657 +               goto out_children;
23658 +
23659 +       if (!a->exchange) {
23660 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23661 +                       err = au_may_ren(a);
23662 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23663 +                       err = -ENAMETOOLONG;
23664 +               if (unlikely(err))
23665 +                       goto out_hdir;
23666 +       }
23667 +
23668 +       /* store timestamps to be revertible */
23669 +       au_ren_dt(a);
23670 +
23671 +       /* store dirren info */
23672 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23673 +               err = au_dr_rename(a->src_dentry, a->btgt,
23674 +                                  &a->dst_dentry->d_name, &rev);
23675 +               AuTraceErr(err);
23676 +               if (unlikely(err))
23677 +                       goto out_dt;
23678 +       }
23679 +
23680 +       /* here we go */
23681 +       err = do_rename(a);
23682 +       if (unlikely(err))
23683 +               goto out_dirren;
23684 +
23685 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23686 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23687 +
23688 +       /* update dir attributes */
23689 +       au_ren_refresh_dir(a);
23690 +
23691 +       /* dput/iput all lower dentries */
23692 +       au_ren_refresh(a);
23693 +
23694 +       goto out_hdir; /* success */
23695 +
23696 +out_dirren:
23697 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23698 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23699 +out_dt:
23700 +       au_ren_rev_dt(err, a);
23701 +out_hdir:
23702 +       au_ren_unlock(a);
23703 +out_children:
23704 +       au_nhash_wh_free(&a->whlist);
23705 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23706 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23707 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23708 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23709 +       }
23710 +out_parent:
23711 +       if (!err) {
23712 +               if (d_unhashed(a->src_dentry))
23713 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23714 +               if (d_unhashed(a->dst_dentry))
23715 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23716 +               if (!a->exchange)
23717 +                       d_move(a->src_dentry, a->dst_dentry);
23718 +               else {
23719 +                       d_exchange(a->src_dentry, a->dst_dentry);
23720 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23721 +                               d_drop(a->dst_dentry);
23722 +               }
23723 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23724 +                       d_drop(a->src_dentry);
23725 +       } else {
23726 +               au_update_dbtop(a->dst_dentry);
23727 +               if (!a->dst_inode)
23728 +                       d_drop(a->dst_dentry);
23729 +       }
23730 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23731 +               di_write_unlock(a->dst_parent);
23732 +       else
23733 +               di_write_unlock2(a->src_parent, a->dst_parent);
23734 +out_unlock:
23735 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23736 +out_free:
23737 +       iput(a->dst_inode);
23738 +       if (a->thargs)
23739 +               au_whtmp_rmdir_free(a->thargs);
23740 +       au_kfree_rcu(a);
23741 +out:
23742 +       AuTraceErr(err);
23743 +       return err;
23744 +}
23745 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23746 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23747 +++ linux/fs/aufs/Kconfig       2021-11-01 23:48:34.203025928 +0100
23748 @@ -0,0 +1,199 @@
23749 +# SPDX-License-Identifier: GPL-2.0
23750 +config AUFS_FS
23751 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23752 +       help
23753 +       Aufs is a stackable unification filesystem such as Unionfs,
23754 +       which unifies several directories and provides a merged single
23755 +       directory.
23756 +       In the early days, aufs was entirely re-designed and
23757 +       re-implemented Unionfs Version 1.x series. Introducing many
23758 +       original ideas, approaches and improvements, it becomes totally
23759 +       different from Unionfs while keeping the basic features.
23760 +
23761 +if AUFS_FS
23762 +choice
23763 +       prompt "Maximum number of branches"
23764 +       default AUFS_BRANCH_MAX_127
23765 +       help
23766 +       Specifies the maximum number of branches (or member directories)
23767 +       in a single aufs. The larger value consumes more system
23768 +       resources and has a minor impact to performance.
23769 +config AUFS_BRANCH_MAX_127
23770 +       bool "127"
23771 +       help
23772 +       Specifies the maximum number of branches (or member directories)
23773 +       in a single aufs. The larger value consumes more system
23774 +       resources and has a minor impact to performance.
23775 +config AUFS_BRANCH_MAX_511
23776 +       bool "511"
23777 +       help
23778 +       Specifies the maximum number of branches (or member directories)
23779 +       in a single aufs. The larger value consumes more system
23780 +       resources and has a minor impact to performance.
23781 +config AUFS_BRANCH_MAX_1023
23782 +       bool "1023"
23783 +       help
23784 +       Specifies the maximum number of branches (or member directories)
23785 +       in a single aufs. The larger value consumes more system
23786 +       resources and has a minor impact to performance.
23787 +config AUFS_BRANCH_MAX_32767
23788 +       bool "32767"
23789 +       help
23790 +       Specifies the maximum number of branches (or member directories)
23791 +       in a single aufs. The larger value consumes more system
23792 +       resources and has a minor impact to performance.
23793 +endchoice
23794 +
23795 +config AUFS_SBILIST
23796 +       bool
23797 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23798 +       default y
23799 +       help
23800 +       Automatic configuration for internal use.
23801 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23802 +
23803 +config AUFS_HNOTIFY
23804 +       bool "Detect direct branch access (bypassing aufs)"
23805 +       help
23806 +       If you want to modify files on branches directly, eg. bypassing aufs,
23807 +       and want aufs to detect the changes of them fully, then enable this
23808 +       option and use 'udba=notify' mount option.
23809 +       Currently there is only one available configuration, "fsnotify".
23810 +       It will have a negative impact to the performance.
23811 +       See detail in aufs.5.
23812 +
23813 +choice
23814 +       prompt "method" if AUFS_HNOTIFY
23815 +       default AUFS_HFSNOTIFY
23816 +config AUFS_HFSNOTIFY
23817 +       bool "fsnotify"
23818 +       select FSNOTIFY
23819 +endchoice
23820 +
23821 +config AUFS_EXPORT
23822 +       bool "NFS-exportable aufs"
23823 +       depends on EXPORTFS
23824 +       help
23825 +       If you want to export your mounted aufs via NFS, then enable this
23826 +       option. There are several requirements for this configuration.
23827 +       See detail in aufs.5.
23828 +
23829 +config AUFS_INO_T_64
23830 +       bool
23831 +       depends on AUFS_EXPORT
23832 +       depends on 64BIT && !(ALPHA || S390)
23833 +       default y
23834 +       help
23835 +       Automatic configuration for internal use.
23836 +       /* typedef unsigned long/int __kernel_ino_t */
23837 +       /* alpha and s390x are int */
23838 +
23839 +config AUFS_XATTR
23840 +       bool "support for XATTR/EA (including Security Labels)"
23841 +       help
23842 +       If your branch fs supports XATTR/EA and you want to make them
23843 +       available in aufs too, then enable this opsion and specify the
23844 +       branch attributes for EA.
23845 +       See detail in aufs.5.
23846 +
23847 +config AUFS_FHSM
23848 +       bool "File-based Hierarchical Storage Management"
23849 +       help
23850 +       Hierarchical Storage Management (or HSM) is a well-known feature
23851 +       in the storage world. Aufs provides this feature as file-based.
23852 +       with multiple branches.
23853 +       These multiple branches are prioritized, ie. the topmost one
23854 +       should be the fastest drive and be used heavily.
23855 +
23856 +config AUFS_RDU
23857 +       bool "Readdir in userspace"
23858 +       help
23859 +       Aufs has two methods to provide a merged view for a directory,
23860 +       by a user-space library and by kernel-space natively. The latter
23861 +       is always enabled but sometimes large and slow.
23862 +       If you enable this option, install the library in aufs2-util
23863 +       package, and set some environment variables for your readdir(3),
23864 +       then the work will be handled in user-space which generally
23865 +       shows better performance in most cases.
23866 +       See detail in aufs.5.
23867 +
23868 +config AUFS_DIRREN
23869 +       bool "Workaround for rename(2)-ing a directory"
23870 +       help
23871 +       By default, aufs returns EXDEV error in renameing a dir who has
23872 +       his child on the lower branch, since it is a bad idea to issue
23873 +       rename(2) internally for every lower branch. But user may not
23874 +       accept this behaviour. So here is a workaround to allow such
23875 +       rename(2) and store some extra infromation on the writable
23876 +       branch. Obviously this costs high (and I don't like it).
23877 +       To use this feature, you need to enable this configuration AND
23878 +       to specify the mount option `dirren.'
23879 +       See details in aufs.5 and the design documents.
23880 +
23881 +config AUFS_SHWH
23882 +       bool "Show whiteouts"
23883 +       help
23884 +       If you want to make the whiteouts in aufs visible, then enable
23885 +       this option and specify 'shwh' mount option. Although it may
23886 +       sounds like philosophy or something, but in technically it
23887 +       simply shows the name of whiteout with keeping its behaviour.
23888 +
23889 +config AUFS_BR_RAMFS
23890 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23891 +       help
23892 +       If you want to use ramfs as an aufs branch fs, then enable this
23893 +       option. Generally tmpfs is recommended.
23894 +       Aufs prohibited them to be a branch fs by default, because
23895 +       initramfs becomes unusable after switch_root or something
23896 +       generally. If you sets initramfs as an aufs branch and boot your
23897 +       system by switch_root, you will meet a problem easily since the
23898 +       files in initramfs may be inaccessible.
23899 +       Unless you are going to use ramfs as an aufs branch fs without
23900 +       switch_root or something, leave it N.
23901 +
23902 +config AUFS_BR_FUSE
23903 +       bool "Fuse fs as an aufs branch"
23904 +       depends on FUSE_FS
23905 +       select AUFS_POLL
23906 +       help
23907 +       If you want to use fuse-based userspace filesystem as an aufs
23908 +       branch fs, then enable this option.
23909 +       It implements the internal poll(2) operation which is
23910 +       implemented by fuse only (curretnly).
23911 +
23912 +config AUFS_POLL
23913 +       bool
23914 +       help
23915 +       Automatic configuration for internal use.
23916 +
23917 +config AUFS_BR_HFSPLUS
23918 +       bool "Hfsplus as an aufs branch"
23919 +       depends on HFSPLUS_FS
23920 +       default y
23921 +       help
23922 +       If you want to use hfsplus fs as an aufs branch fs, then enable
23923 +       this option. This option introduces a small overhead at
23924 +       copying-up a file on hfsplus.
23925 +
23926 +config AUFS_BDEV_LOOP
23927 +       bool
23928 +       depends on BLK_DEV_LOOP
23929 +       default y
23930 +       help
23931 +       Automatic configuration for internal use.
23932 +       Convert =[ym] into =y.
23933 +
23934 +config AUFS_DEBUG
23935 +       bool "Debug aufs"
23936 +       help
23937 +       Enable this to compile aufs internal debug code.
23938 +       It will have a negative impact to the performance.
23939 +
23940 +config AUFS_MAGIC_SYSRQ
23941 +       bool
23942 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
23943 +       default y
23944 +       help
23945 +       Automatic configuration for internal use.
23946 +       When aufs supports Magic SysRq, enabled automatically.
23947 +endif
23948 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23949 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
23950 +++ linux/fs/aufs/lcnt.h        2021-11-01 23:48:34.209692595 +0100
23951 @@ -0,0 +1,186 @@
23952 +/* SPDX-License-Identifier: GPL-2.0 */
23953 +/*
23954 + * Copyright (C) 2018-2021 Junjiro R. Okajima
23955 + *
23956 + * This program, aufs is free software; you can redistribute it and/or modify
23957 + * it under the terms of the GNU General Public License as published by
23958 + * the Free Software Foundation; either version 2 of the License, or
23959 + * (at your option) any later version.
23960 + *
23961 + * This program is distributed in the hope that it will be useful,
23962 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23963 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23964 + * GNU General Public License for more details.
23965 + *
23966 + * You should have received a copy of the GNU General Public License
23967 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23968 + */
23969 +
23970 +/*
23971 + * simple long counter wrapper
23972 + */
23973 +
23974 +#ifndef __AUFS_LCNT_H__
23975 +#define __AUFS_LCNT_H__
23976 +
23977 +#ifdef __KERNEL__
23978 +
23979 +#include "debug.h"
23980 +
23981 +#define AuLCntATOMIC   1
23982 +#define AuLCntPCPUCNT  2
23983 +/*
23984 + * why does percpu_refcount require extra synchronize_rcu()s in
23985 + * au_br_do_free()
23986 + */
23987 +#define AuLCntPCPUREF  3
23988 +
23989 +/* #define AuLCntChosen        AuLCntATOMIC */
23990 +#define AuLCntChosen   AuLCntPCPUCNT
23991 +/* #define AuLCntChosen        AuLCntPCPUREF */
23992 +
23993 +#if AuLCntChosen == AuLCntATOMIC
23994 +#include <linux/atomic.h>
23995 +
23996 +typedef atomic_long_t au_lcnt_t;
23997 +
23998 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23999 +{
24000 +       atomic_long_set(cnt, 0);
24001 +       return 0;
24002 +}
24003 +
24004 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24005 +{
24006 +       /* empty */
24007 +}
24008 +
24009 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24010 +                              int do_sync __maybe_unused)
24011 +{
24012 +       /* empty */
24013 +}
24014 +
24015 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24016 +{
24017 +       atomic_long_inc(cnt);
24018 +}
24019 +
24020 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24021 +{
24022 +       atomic_long_dec(cnt);
24023 +}
24024 +
24025 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24026 +{
24027 +       return atomic_long_read(cnt);
24028 +}
24029 +#endif
24030 +
24031 +#if AuLCntChosen == AuLCntPCPUCNT
24032 +#include <linux/percpu_counter.h>
24033 +
24034 +typedef struct percpu_counter au_lcnt_t;
24035 +
24036 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24037 +{
24038 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24039 +}
24040 +
24041 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24042 +{
24043 +       /* empty */
24044 +}
24045 +
24046 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24047 +{
24048 +       percpu_counter_destroy(cnt);
24049 +}
24050 +
24051 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24052 +{
24053 +       percpu_counter_inc(cnt);
24054 +}
24055 +
24056 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24057 +{
24058 +       percpu_counter_dec(cnt);
24059 +}
24060 +
24061 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24062 +{
24063 +       s64 n;
24064 +
24065 +       n = percpu_counter_sum(cnt);
24066 +       BUG_ON(n < 0);
24067 +       if (LONG_MAX != LLONG_MAX
24068 +           && n > LONG_MAX)
24069 +               AuWarn1("%s\n", "wrap-around");
24070 +
24071 +       return n;
24072 +}
24073 +#endif
24074 +
24075 +#if AuLCntChosen == AuLCntPCPUREF
24076 +#include <linux/percpu-refcount.h>
24077 +
24078 +typedef struct percpu_ref au_lcnt_t;
24079 +
24080 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24081 +{
24082 +       if (!release)
24083 +               release = percpu_ref_exit;
24084 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24085 +}
24086 +
24087 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24088 +{
24089 +       synchronize_rcu();
24090 +}
24091 +
24092 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24093 +{
24094 +       percpu_ref_kill(cnt);
24095 +       if (do_sync)
24096 +               au_lcnt_wait_for_fin(cnt);
24097 +}
24098 +
24099 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24100 +{
24101 +       percpu_ref_get(cnt);
24102 +}
24103 +
24104 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24105 +{
24106 +       percpu_ref_put(cnt);
24107 +}
24108 +
24109 +/*
24110 + * avoid calling this func as possible.
24111 + */
24112 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24113 +{
24114 +       long l;
24115 +
24116 +       percpu_ref_switch_to_atomic_sync(cnt);
24117 +       l = atomic_long_read(&cnt->count);
24118 +       if (do_rev)
24119 +               percpu_ref_switch_to_percpu(cnt);
24120 +
24121 +       /* percpu_ref is initialized by 1 instead of 0 */
24122 +       return l - 1;
24123 +}
24124 +#endif
24125 +
24126 +#ifdef CONFIG_AUFS_DEBUG
24127 +#define AuLCntZero(val) do {                   \
24128 +       long l = val;                           \
24129 +       if (l)                                  \
24130 +               AuDbg("%s = %ld\n", #val, l);   \
24131 +} while (0)
24132 +#else
24133 +#define AuLCntZero(val)                do {} while (0)
24134 +#endif
24135 +
24136 +#endif /* __KERNEL__ */
24137 +#endif /* __AUFS_LCNT_H__ */
24138 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24139 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24140 +++ linux/fs/aufs/loop.c        2021-11-01 23:48:34.209692595 +0100
24141 @@ -0,0 +1,148 @@
24142 +// SPDX-License-Identifier: GPL-2.0
24143 +/*
24144 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24145 + *
24146 + * This program, aufs is free software; you can redistribute it and/or modify
24147 + * it under the terms of the GNU General Public License as published by
24148 + * the Free Software Foundation; either version 2 of the License, or
24149 + * (at your option) any later version.
24150 + *
24151 + * This program is distributed in the hope that it will be useful,
24152 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24153 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24154 + * GNU General Public License for more details.
24155 + *
24156 + * You should have received a copy of the GNU General Public License
24157 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24158 + */
24159 +
24160 +/*
24161 + * support for loopback block device as a branch
24162 + */
24163 +
24164 +#include "aufs.h"
24165 +
24166 +/* added into drivers/block/loop.c */
24167 +static struct file *(*backing_file_func)(struct super_block *sb);
24168 +
24169 +/*
24170 + * test if two lower dentries have overlapping branches.
24171 + */
24172 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24173 +{
24174 +       struct super_block *h_sb;
24175 +       struct file *backing_file;
24176 +
24177 +       if (unlikely(!backing_file_func)) {
24178 +               /* don't load "loop" module here */
24179 +               backing_file_func = symbol_get(loop_backing_file);
24180 +               if (unlikely(!backing_file_func))
24181 +                       /* "loop" module is not loaded */
24182 +                       return 0;
24183 +       }
24184 +
24185 +       h_sb = h_adding->d_sb;
24186 +       backing_file = backing_file_func(h_sb);
24187 +       if (!backing_file)
24188 +               return 0;
24189 +
24190 +       h_adding = backing_file->f_path.dentry;
24191 +       /*
24192 +        * h_adding can be local NFS.
24193 +        * in this case aufs cannot detect the loop.
24194 +        */
24195 +       if (unlikely(h_adding->d_sb == sb))
24196 +               return 1;
24197 +       return !!au_test_subdir(h_adding, sb->s_root);
24198 +}
24199 +
24200 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24201 +int au_test_loopback_kthread(void)
24202 +{
24203 +       int ret;
24204 +       struct task_struct *tsk = current;
24205 +       char c, comm[sizeof(tsk->comm)];
24206 +
24207 +       ret = 0;
24208 +       if (tsk->flags & PF_KTHREAD) {
24209 +               get_task_comm(comm, tsk);
24210 +               c = comm[4];
24211 +               ret = ('0' <= c && c <= '9'
24212 +                      && !strncmp(comm, "loop", 4));
24213 +       }
24214 +
24215 +       return ret;
24216 +}
24217 +
24218 +/* ---------------------------------------------------------------------- */
24219 +
24220 +#define au_warn_loopback_step  16
24221 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24222 +static unsigned long *au_warn_loopback_array;
24223 +
24224 +void au_warn_loopback(struct super_block *h_sb)
24225 +{
24226 +       int i, new_nelem;
24227 +       unsigned long *a, magic;
24228 +       static DEFINE_SPINLOCK(spin);
24229 +
24230 +       magic = h_sb->s_magic;
24231 +       spin_lock(&spin);
24232 +       a = au_warn_loopback_array;
24233 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24234 +               if (a[i] == magic) {
24235 +                       spin_unlock(&spin);
24236 +                       return;
24237 +               }
24238 +
24239 +       /* h_sb is new to us, print it */
24240 +       if (i < au_warn_loopback_nelem) {
24241 +               a[i] = magic;
24242 +               goto pr;
24243 +       }
24244 +
24245 +       /* expand the array */
24246 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24247 +       a = au_kzrealloc(au_warn_loopback_array,
24248 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24249 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24250 +                        /*may_shrink*/0);
24251 +       if (a) {
24252 +               au_warn_loopback_nelem = new_nelem;
24253 +               au_warn_loopback_array = a;
24254 +               a[i] = magic;
24255 +               goto pr;
24256 +       }
24257 +
24258 +       spin_unlock(&spin);
24259 +       AuWarn1("realloc failed, ignored\n");
24260 +       return;
24261 +
24262 +pr:
24263 +       spin_unlock(&spin);
24264 +       pr_warn("you may want to try another patch for loopback file "
24265 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24266 +}
24267 +
24268 +int au_loopback_init(void)
24269 +{
24270 +       int err;
24271 +       struct super_block *sb __maybe_unused;
24272 +
24273 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24274 +
24275 +       err = 0;
24276 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24277 +                                        sizeof(unsigned long), GFP_NOFS);
24278 +       if (unlikely(!au_warn_loopback_array))
24279 +               err = -ENOMEM;
24280 +
24281 +       return err;
24282 +}
24283 +
24284 +void au_loopback_fin(void)
24285 +{
24286 +       if (backing_file_func)
24287 +               symbol_put(loop_backing_file);
24288 +       au_kfree_try_rcu(au_warn_loopback_array);
24289 +}
24290 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24291 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24292 +++ linux/fs/aufs/loop.h        2021-11-01 23:48:34.209692595 +0100
24293 @@ -0,0 +1,55 @@
24294 +/* SPDX-License-Identifier: GPL-2.0 */
24295 +/*
24296 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24297 + *
24298 + * This program, aufs is free software; you can redistribute it and/or modify
24299 + * it under the terms of the GNU General Public License as published by
24300 + * the Free Software Foundation; either version 2 of the License, or
24301 + * (at your option) any later version.
24302 + *
24303 + * This program is distributed in the hope that it will be useful,
24304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24306 + * GNU General Public License for more details.
24307 + *
24308 + * You should have received a copy of the GNU General Public License
24309 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24310 + */
24311 +
24312 +/*
24313 + * support for loopback mount as a branch
24314 + */
24315 +
24316 +#ifndef __AUFS_LOOP_H__
24317 +#define __AUFS_LOOP_H__
24318 +
24319 +#ifdef __KERNEL__
24320 +
24321 +struct dentry;
24322 +struct super_block;
24323 +
24324 +#ifdef CONFIG_AUFS_BDEV_LOOP
24325 +/* drivers/block/loop.c */
24326 +struct file *loop_backing_file(struct super_block *sb);
24327 +
24328 +/* loop.c */
24329 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24330 +int au_test_loopback_kthread(void);
24331 +void au_warn_loopback(struct super_block *h_sb);
24332 +
24333 +int au_loopback_init(void);
24334 +void au_loopback_fin(void);
24335 +#else
24336 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24337 +
24338 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24339 +          struct dentry *h_adding)
24340 +AuStubInt0(au_test_loopback_kthread, void)
24341 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24342 +
24343 +AuStubInt0(au_loopback_init, void)
24344 +AuStubVoid(au_loopback_fin, void)
24345 +#endif /* BLK_DEV_LOOP */
24346 +
24347 +#endif /* __KERNEL__ */
24348 +#endif /* __AUFS_LOOP_H__ */
24349 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24350 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24351 +++ linux/fs/aufs/magic.mk      2021-11-01 23:48:34.209692595 +0100
24352 @@ -0,0 +1,31 @@
24353 +# SPDX-License-Identifier: GPL-2.0
24354 +
24355 +# defined in ${srctree}/fs/fuse/inode.c
24356 +# tristate
24357 +ifdef CONFIG_FUSE_FS
24358 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24359 +endif
24360 +
24361 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24362 +# tristate
24363 +ifdef CONFIG_XFS_FS
24364 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24365 +endif
24366 +
24367 +# defined in ${srctree}/fs/configfs/mount.c
24368 +# tristate
24369 +ifdef CONFIG_CONFIGFS_FS
24370 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24371 +endif
24372 +
24373 +# defined in ${srctree}/fs/ubifs/ubifs.h
24374 +# tristate
24375 +ifdef CONFIG_UBIFS_FS
24376 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24377 +endif
24378 +
24379 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24380 +# tristate
24381 +ifdef CONFIG_HFSPLUS_FS
24382 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24383 +endif
24384 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24385 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24386 +++ linux/fs/aufs/Makefile      2021-11-01 23:48:34.203025928 +0100
24387 @@ -0,0 +1,46 @@
24388 +# SPDX-License-Identifier: GPL-2.0
24389 +
24390 +include ${src}/magic.mk
24391 +ifeq (${CONFIG_AUFS_FS},m)
24392 +include ${src}/conf.mk
24393 +endif
24394 +-include ${src}/priv_def.mk
24395 +
24396 +# cf. include/linux/kernel.h
24397 +# enable pr_debug
24398 +ccflags-y += -DDEBUG
24399 +# sparse requires the full pathname
24400 +ifdef M
24401 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24402 +else
24403 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24404 +endif
24405 +
24406 +obj-$(CONFIG_AUFS_FS) += aufs.o
24407 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24408 +       wkq.o vfsub.o dcsub.o \
24409 +       cpup.o whout.o wbr_policy.o \
24410 +       dinfo.o dentry.o \
24411 +       dynop.o \
24412 +       finfo.o file.o f_op.o \
24413 +       dir.o vdir.o \
24414 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24415 +       mvdown.o ioctl.o
24416 +
24417 +# all are boolean
24418 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24419 +aufs-$(CONFIG_SYSFS) += sysfs.o
24420 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24421 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24422 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24423 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24424 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24425 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24426 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24427 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24428 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24429 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24430 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24431 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24432 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24433 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24434 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24435 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24436 +++ linux/fs/aufs/module.c      2021-11-01 23:48:34.209692595 +0100
24437 @@ -0,0 +1,273 @@
24438 +// SPDX-License-Identifier: GPL-2.0
24439 +/*
24440 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24441 + *
24442 + * This program, aufs is free software; you can redistribute it and/or modify
24443 + * it under the terms of the GNU General Public License as published by
24444 + * the Free Software Foundation; either version 2 of the License, or
24445 + * (at your option) any later version.
24446 + *
24447 + * This program is distributed in the hope that it will be useful,
24448 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24449 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24450 + * GNU General Public License for more details.
24451 + *
24452 + * You should have received a copy of the GNU General Public License
24453 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24454 + */
24455 +
24456 +/*
24457 + * module global variables and operations
24458 + */
24459 +
24460 +#include <linux/module.h>
24461 +#include <linux/seq_file.h>
24462 +#include "aufs.h"
24463 +
24464 +/* shrinkable realloc */
24465 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24466 +{
24467 +       size_t sz;
24468 +       int diff;
24469 +
24470 +       sz = 0;
24471 +       diff = -1;
24472 +       if (p) {
24473 +#if 0 /* unused */
24474 +               if (!new_sz) {
24475 +                       au_kfree_rcu(p);
24476 +                       p = NULL;
24477 +                       goto out;
24478 +               }
24479 +#else
24480 +               AuDebugOn(!new_sz);
24481 +#endif
24482 +               sz = ksize(p);
24483 +               diff = au_kmidx_sub(sz, new_sz);
24484 +       }
24485 +       if (sz && !diff)
24486 +               goto out;
24487 +
24488 +       if (sz < new_sz)
24489 +               /* expand or SLOB */
24490 +               p = krealloc(p, new_sz, gfp);
24491 +       else if (new_sz < sz && may_shrink) {
24492 +               /* shrink */
24493 +               void *q;
24494 +
24495 +               q = kmalloc(new_sz, gfp);
24496 +               if (q) {
24497 +                       if (p) {
24498 +                               memcpy(q, p, new_sz);
24499 +                               au_kfree_try_rcu(p);
24500 +                       }
24501 +                       p = q;
24502 +               } else
24503 +                       p = NULL;
24504 +       }
24505 +
24506 +out:
24507 +       return p;
24508 +}
24509 +
24510 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24511 +                  int may_shrink)
24512 +{
24513 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24514 +       if (p && new_sz > nused)
24515 +               memset(p + nused, 0, new_sz - nused);
24516 +       return p;
24517 +}
24518 +
24519 +/* ---------------------------------------------------------------------- */
24520 +/*
24521 + * aufs caches
24522 + */
24523 +struct kmem_cache *au_cache[AuCache_Last];
24524 +
24525 +static void au_cache_fin(void)
24526 +{
24527 +       int i;
24528 +
24529 +       /*
24530 +        * Make sure all delayed rcu free inodes are flushed before we
24531 +        * destroy cache.
24532 +        */
24533 +       rcu_barrier();
24534 +
24535 +       /* excluding AuCache_HNOTIFY */
24536 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24537 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24538 +               kmem_cache_destroy(au_cache[i]);
24539 +               au_cache[i] = NULL;
24540 +       }
24541 +}
24542 +
24543 +static int __init au_cache_init(void)
24544 +{
24545 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24546 +       if (au_cache[AuCache_DINFO])
24547 +               /* SLAB_DESTROY_BY_RCU */
24548 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24549 +                                                      au_icntnr_init_once);
24550 +       if (au_cache[AuCache_ICNTNR])
24551 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24552 +                                                     au_fi_init_once);
24553 +       if (au_cache[AuCache_FINFO])
24554 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24555 +       if (au_cache[AuCache_VDIR])
24556 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24557 +       if (au_cache[AuCache_DEHSTR])
24558 +               return 0;
24559 +
24560 +       au_cache_fin();
24561 +       return -ENOMEM;
24562 +}
24563 +
24564 +/* ---------------------------------------------------------------------- */
24565 +
24566 +int au_dir_roflags;
24567 +
24568 +#ifdef CONFIG_AUFS_SBILIST
24569 +/*
24570 + * iterate_supers_type() doesn't protect us from
24571 + * remounting (branch management)
24572 + */
24573 +struct hlist_bl_head au_sbilist;
24574 +#endif
24575 +
24576 +/*
24577 + * functions for module interface.
24578 + */
24579 +MODULE_LICENSE("GPL");
24580 +/* MODULE_LICENSE("GPL v2"); */
24581 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24582 +MODULE_DESCRIPTION(AUFS_NAME
24583 +       " -- Advanced multi layered unification filesystem");
24584 +MODULE_VERSION(AUFS_VERSION);
24585 +MODULE_ALIAS_FS(AUFS_NAME);
24586 +
24587 +/* this module parameter has no meaning when SYSFS is disabled */
24588 +int sysaufs_brs = 1;
24589 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24590 +module_param_named(brs, sysaufs_brs, int, 0444);
24591 +
24592 +/* this module parameter has no meaning when USER_NS is disabled */
24593 +bool au_userns;
24594 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24595 +module_param_named(allow_userns, au_userns, bool, 0444);
24596 +
24597 +/* ---------------------------------------------------------------------- */
24598 +
24599 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24600 +
24601 +int au_seq_path(struct seq_file *seq, struct path *path)
24602 +{
24603 +       int err;
24604 +
24605 +       err = seq_path(seq, path, au_esc_chars);
24606 +       if (err >= 0)
24607 +               err = 0;
24608 +       else
24609 +               err = -ENOMEM;
24610 +
24611 +       return err;
24612 +}
24613 +
24614 +/* ---------------------------------------------------------------------- */
24615 +
24616 +static int __init aufs_init(void)
24617 +{
24618 +       int err, i;
24619 +       char *p;
24620 +
24621 +       p = au_esc_chars;
24622 +       for (i = 1; i <= ' '; i++)
24623 +               *p++ = i;
24624 +       *p++ = '\\';
24625 +       *p++ = '\x7f';
24626 +       *p = 0;
24627 +
24628 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24629 +
24630 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24631 +       for (i = 0; i < AuIop_Last; i++)
24632 +               aufs_iop_nogetattr[i].getattr = NULL;
24633 +
24634 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24635 +
24636 +       au_sbilist_init();
24637 +       sysaufs_brs_init();
24638 +       au_debug_init();
24639 +       au_dy_init();
24640 +       err = sysaufs_init();
24641 +       if (unlikely(err))
24642 +               goto out;
24643 +       err = dbgaufs_init();
24644 +       if (unlikely(err))
24645 +               goto out_sysaufs;
24646 +       err = au_procfs_init();
24647 +       if (unlikely(err))
24648 +               goto out_dbgaufs;
24649 +       err = au_wkq_init();
24650 +       if (unlikely(err))
24651 +               goto out_procfs;
24652 +       err = au_loopback_init();
24653 +       if (unlikely(err))
24654 +               goto out_wkq;
24655 +       err = au_hnotify_init();
24656 +       if (unlikely(err))
24657 +               goto out_loopback;
24658 +       err = au_sysrq_init();
24659 +       if (unlikely(err))
24660 +               goto out_hin;
24661 +       err = au_cache_init();
24662 +       if (unlikely(err))
24663 +               goto out_sysrq;
24664 +
24665 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24666 +       err = register_filesystem(&aufs_fs_type);
24667 +       if (unlikely(err))
24668 +               goto out_cache;
24669 +
24670 +       /* since we define pr_fmt, call printk directly */
24671 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24672 +       goto out; /* success */
24673 +
24674 +out_cache:
24675 +       au_cache_fin();
24676 +out_sysrq:
24677 +       au_sysrq_fin();
24678 +out_hin:
24679 +       au_hnotify_fin();
24680 +out_loopback:
24681 +       au_loopback_fin();
24682 +out_wkq:
24683 +       au_wkq_fin();
24684 +out_procfs:
24685 +       au_procfs_fin();
24686 +out_dbgaufs:
24687 +       dbgaufs_fin();
24688 +out_sysaufs:
24689 +       sysaufs_fin();
24690 +       au_dy_fin();
24691 +out:
24692 +       return err;
24693 +}
24694 +
24695 +static void __exit aufs_exit(void)
24696 +{
24697 +       unregister_filesystem(&aufs_fs_type);
24698 +       au_cache_fin();
24699 +       au_sysrq_fin();
24700 +       au_hnotify_fin();
24701 +       au_loopback_fin();
24702 +       au_wkq_fin();
24703 +       au_procfs_fin();
24704 +       dbgaufs_fin();
24705 +       sysaufs_fin();
24706 +       au_dy_fin();
24707 +}
24708 +
24709 +module_init(aufs_init);
24710 +module_exit(aufs_exit);
24711 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24712 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24713 +++ linux/fs/aufs/module.h      2021-11-01 23:48:34.209692595 +0100
24714 @@ -0,0 +1,166 @@
24715 +/* SPDX-License-Identifier: GPL-2.0 */
24716 +/*
24717 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24718 + *
24719 + * This program, aufs is free software; you can redistribute it and/or modify
24720 + * it under the terms of the GNU General Public License as published by
24721 + * the Free Software Foundation; either version 2 of the License, or
24722 + * (at your option) any later version.
24723 + *
24724 + * This program is distributed in the hope that it will be useful,
24725 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24726 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24727 + * GNU General Public License for more details.
24728 + *
24729 + * You should have received a copy of the GNU General Public License
24730 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24731 + */
24732 +
24733 +/*
24734 + * module initialization and module-global
24735 + */
24736 +
24737 +#ifndef __AUFS_MODULE_H__
24738 +#define __AUFS_MODULE_H__
24739 +
24740 +#ifdef __KERNEL__
24741 +
24742 +#include <linux/slab.h>
24743 +#include "debug.h"
24744 +#include "dentry.h"
24745 +#include "dir.h"
24746 +#include "file.h"
24747 +#include "inode.h"
24748 +
24749 +struct path;
24750 +struct seq_file;
24751 +
24752 +/* module parameters */
24753 +extern int sysaufs_brs;
24754 +extern bool au_userns;
24755 +
24756 +/* ---------------------------------------------------------------------- */
24757 +
24758 +extern int au_dir_roflags;
24759 +
24760 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24761 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24762 +                  int may_shrink);
24763 +
24764 +/*
24765 + * Comparing the size of the object with sizeof(struct rcu_head)
24766 + * case 1: object is always larger
24767 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24768 + * case 2: object is always smaller
24769 + *     --> au_kfree_small()
24770 + * case 3: object can be any size
24771 + *     --> au_kfree_try_rcu()
24772 + */
24773 +
24774 +static inline void au_kfree_do_rcu(const void *p)
24775 +{
24776 +       struct {
24777 +               struct rcu_head rcu;
24778 +       } *a = (void *)p;
24779 +
24780 +       kfree_rcu(a, rcu);
24781 +}
24782 +
24783 +#define au_kfree_rcu(_p) do {                                          \
24784 +               typeof(_p) p = (_p);                                    \
24785 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24786 +               if (p)                                                  \
24787 +                       au_kfree_do_rcu(p);                             \
24788 +       } while (0)
24789 +
24790 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24791 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24792 +
24793 +static inline void au_kfree_try_rcu(const void *p)
24794 +{
24795 +       if (!p)
24796 +               return;
24797 +       if (au_kfree_sz_test(p))
24798 +               au_kfree_do_rcu(p);
24799 +       else
24800 +               kfree(p);
24801 +}
24802 +
24803 +static inline void au_kfree_small(const void *p)
24804 +{
24805 +       if (!p)
24806 +               return;
24807 +       AuDebugOn(au_kfree_sz_test(p));
24808 +       kfree(p);
24809 +}
24810 +
24811 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24812 +{
24813 +#ifndef CONFIG_SLOB
24814 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
24815 +#else
24816 +       return -1; /* SLOB is untested */
24817 +#endif
24818 +}
24819 +
24820 +int au_seq_path(struct seq_file *seq, struct path *path);
24821 +
24822 +#ifdef CONFIG_PROC_FS
24823 +/* procfs.c */
24824 +int __init au_procfs_init(void);
24825 +void au_procfs_fin(void);
24826 +#else
24827 +AuStubInt0(au_procfs_init, void);
24828 +AuStubVoid(au_procfs_fin, void);
24829 +#endif
24830 +
24831 +/* ---------------------------------------------------------------------- */
24832 +
24833 +/* kmem cache */
24834 +enum {
24835 +       AuCache_DINFO,
24836 +       AuCache_ICNTNR,
24837 +       AuCache_FINFO,
24838 +       AuCache_VDIR,
24839 +       AuCache_DEHSTR,
24840 +       AuCache_HNOTIFY, /* must be last */
24841 +       AuCache_Last
24842 +};
24843 +
24844 +extern struct kmem_cache *au_cache[AuCache_Last];
24845 +
24846 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24847 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24848 +#define AuCacheCtor(type, ctor)        \
24849 +       kmem_cache_create(#type, sizeof(struct type), \
24850 +                         __alignof__(struct type), AuCacheFlags, ctor)
24851 +
24852 +#define AuCacheFuncs(name, index)                                      \
24853 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24854 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24855 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24856 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24857 +                                                                       \
24858 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24859 +       { void *p = rcu;                                                \
24860 +               p -= offsetof(struct au_##name, rcu);                   \
24861 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24862 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24863 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24864 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24865 +                                                                       \
24866 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24867 +       { /* au_cache_free_##name##_norcu(p); */                        \
24868 +               au_cache_free_##name##_rcu(p); }
24869 +
24870 +AuCacheFuncs(dinfo, DINFO);
24871 +AuCacheFuncs(icntnr, ICNTNR);
24872 +AuCacheFuncs(finfo, FINFO);
24873 +AuCacheFuncs(vdir, VDIR);
24874 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24875 +#ifdef CONFIG_AUFS_HNOTIFY
24876 +AuCacheFuncs(hnotify, HNOTIFY);
24877 +#endif
24878 +
24879 +#endif /* __KERNEL__ */
24880 +#endif /* __AUFS_MODULE_H__ */
24881 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24882 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24883 +++ linux/fs/aufs/mvdown.c      2021-11-01 23:48:34.209692595 +0100
24884 @@ -0,0 +1,706 @@
24885 +// SPDX-License-Identifier: GPL-2.0
24886 +/*
24887 + * Copyright (C) 2011-2021 Junjiro R. Okajima
24888 + *
24889 + * This program, aufs is free software; you can redistribute it and/or modify
24890 + * it under the terms of the GNU General Public License as published by
24891 + * the Free Software Foundation; either version 2 of the License, or
24892 + * (at your option) any later version.
24893 + *
24894 + * This program is distributed in the hope that it will be useful,
24895 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24896 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24897 + * GNU General Public License for more details.
24898 + *
24899 + * You should have received a copy of the GNU General Public License
24900 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24901 + */
24902 +
24903 +/*
24904 + * move-down, opposite of copy-up
24905 + */
24906 +
24907 +#include "aufs.h"
24908 +
24909 +struct au_mvd_args {
24910 +       struct {
24911 +               struct super_block *h_sb;
24912 +               struct dentry *h_parent;
24913 +               struct au_hinode *hdir;
24914 +               struct inode *h_dir, *h_inode;
24915 +               struct au_pin pin;
24916 +       } info[AUFS_MVDOWN_NARRAY];
24917 +
24918 +       struct aufs_mvdown mvdown;
24919 +       struct dentry *dentry, *parent;
24920 +       struct inode *inode, *dir;
24921 +       struct super_block *sb;
24922 +       aufs_bindex_t bopq, bwh, bfound;
24923 +       unsigned char rename_lock;
24924 +};
24925 +
24926 +#define mvd_errno              mvdown.au_errno
24927 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24928 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24929 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24930 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24931 +
24932 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24933 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24934 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24935 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24936 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24937 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24938 +
24939 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24940 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24941 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24942 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24943 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24944 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24945 +
24946 +#define AU_MVD_PR(flag, ...) do {                      \
24947 +               if (flag)                               \
24948 +                       pr_err(__VA_ARGS__);            \
24949 +       } while (0)
24950 +
24951 +static int find_lower_writable(struct au_mvd_args *a)
24952 +{
24953 +       struct super_block *sb;
24954 +       aufs_bindex_t bindex, bbot;
24955 +       struct au_branch *br;
24956 +
24957 +       sb = a->sb;
24958 +       bindex = a->mvd_bsrc;
24959 +       bbot = au_sbbot(sb);
24960 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24961 +               for (bindex++; bindex <= bbot; bindex++) {
24962 +                       br = au_sbr(sb, bindex);
24963 +                       if (au_br_fhsm(br->br_perm)
24964 +                           && !sb_rdonly(au_br_sb(br)))
24965 +                               return bindex;
24966 +               }
24967 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24968 +               for (bindex++; bindex <= bbot; bindex++) {
24969 +                       br = au_sbr(sb, bindex);
24970 +                       if (!au_br_rdonly(br))
24971 +                               return bindex;
24972 +               }
24973 +       else
24974 +               for (bindex++; bindex <= bbot; bindex++) {
24975 +                       br = au_sbr(sb, bindex);
24976 +                       if (!sb_rdonly(au_br_sb(br))) {
24977 +                               if (au_br_rdonly(br))
24978 +                                       a->mvdown.flags
24979 +                                               |= AUFS_MVDOWN_ROLOWER_R;
24980 +                               return bindex;
24981 +                       }
24982 +               }
24983 +
24984 +       return -1;
24985 +}
24986 +
24987 +/* make the parent dir on bdst */
24988 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24989 +{
24990 +       int err;
24991 +
24992 +       err = 0;
24993 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24994 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24995 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24996 +       a->mvd_h_dst_parent = NULL;
24997 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
24998 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24999 +       if (!a->mvd_h_dst_parent) {
25000 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25001 +               if (unlikely(err)) {
25002 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25003 +                       goto out;
25004 +               }
25005 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25006 +       }
25007 +
25008 +out:
25009 +       AuTraceErr(err);
25010 +       return err;
25011 +}
25012 +
25013 +/* lock them all */
25014 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25015 +{
25016 +       int err;
25017 +       struct dentry *h_trap;
25018 +
25019 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25020 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25021 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25022 +                    au_opt_udba(a->sb),
25023 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25024 +       AuTraceErr(err);
25025 +       if (unlikely(err)) {
25026 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25027 +               goto out;
25028 +       }
25029 +
25030 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25031 +               a->rename_lock = 0;
25032 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25033 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25034 +                           au_opt_udba(a->sb),
25035 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25036 +               err = au_do_pin(&a->mvd_pin_src);
25037 +               AuTraceErr(err);
25038 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25039 +               if (unlikely(err)) {
25040 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25041 +                       goto out_dst;
25042 +               }
25043 +               goto out; /* success */
25044 +       }
25045 +
25046 +       a->rename_lock = 1;
25047 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25048 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25049 +                    au_opt_udba(a->sb),
25050 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25051 +       AuTraceErr(err);
25052 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25053 +       if (unlikely(err)) {
25054 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25055 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25056 +               goto out_dst;
25057 +       }
25058 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25059 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25060 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25061 +       if (h_trap) {
25062 +               err = (h_trap != a->mvd_h_src_parent);
25063 +               if (err)
25064 +                       err = (h_trap != a->mvd_h_dst_parent);
25065 +       }
25066 +       BUG_ON(err); /* it should never happen */
25067 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25068 +               err = -EBUSY;
25069 +               AuTraceErr(err);
25070 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25071 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25072 +               au_pin_hdir_lock(&a->mvd_pin_src);
25073 +               au_unpin(&a->mvd_pin_src);
25074 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25075 +               goto out_dst;
25076 +       }
25077 +       goto out; /* success */
25078 +
25079 +out_dst:
25080 +       au_unpin(&a->mvd_pin_dst);
25081 +out:
25082 +       AuTraceErr(err);
25083 +       return err;
25084 +}
25085 +
25086 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25087 +{
25088 +       if (!a->rename_lock)
25089 +               au_unpin(&a->mvd_pin_src);
25090 +       else {
25091 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25092 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25093 +               au_pin_hdir_lock(&a->mvd_pin_src);
25094 +               au_unpin(&a->mvd_pin_src);
25095 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25096 +       }
25097 +       au_unpin(&a->mvd_pin_dst);
25098 +}
25099 +
25100 +/* copy-down the file */
25101 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25102 +{
25103 +       int err;
25104 +       struct au_cp_generic cpg = {
25105 +               .dentry = a->dentry,
25106 +               .bdst   = a->mvd_bdst,
25107 +               .bsrc   = a->mvd_bsrc,
25108 +               .len    = -1,
25109 +               .pin    = &a->mvd_pin_dst,
25110 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25111 +       };
25112 +
25113 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25114 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25115 +               au_fset_cpup(cpg.flags, OVERWRITE);
25116 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25117 +               au_fset_cpup(cpg.flags, RWDST);
25118 +       err = au_sio_cpdown_simple(&cpg);
25119 +       if (unlikely(err))
25120 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25121 +
25122 +       AuTraceErr(err);
25123 +       return err;
25124 +}
25125 +
25126 +/*
25127 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25128 + * were sleeping
25129 + */
25130 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25131 +{
25132 +       int err;
25133 +       struct path h_path;
25134 +       struct au_branch *br;
25135 +       struct inode *delegated;
25136 +
25137 +       br = au_sbr(a->sb, a->mvd_bdst);
25138 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25139 +       err = PTR_ERR(h_path.dentry);
25140 +       if (IS_ERR(h_path.dentry)) {
25141 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25142 +               goto out;
25143 +       }
25144 +
25145 +       err = 0;
25146 +       if (d_is_positive(h_path.dentry)) {
25147 +               h_path.mnt = au_br_mnt(br);
25148 +               delegated = NULL;
25149 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25150 +                                  &delegated, /*force*/0);
25151 +               if (unlikely(err == -EWOULDBLOCK)) {
25152 +                       pr_warn("cannot retry for NFSv4 delegation"
25153 +                               " for an internal unlink\n");
25154 +                       iput(delegated);
25155 +               }
25156 +               if (unlikely(err))
25157 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25158 +       }
25159 +       dput(h_path.dentry);
25160 +
25161 +out:
25162 +       AuTraceErr(err);
25163 +       return err;
25164 +}
25165 +
25166 +/*
25167 + * unlink the topmost h_dentry
25168 + */
25169 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25170 +{
25171 +       int err;
25172 +       struct path h_path;
25173 +       struct inode *delegated;
25174 +
25175 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25176 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25177 +       delegated = NULL;
25178 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25179 +       if (unlikely(err == -EWOULDBLOCK)) {
25180 +               pr_warn("cannot retry for NFSv4 delegation"
25181 +                       " for an internal unlink\n");
25182 +               iput(delegated);
25183 +       }
25184 +       if (unlikely(err))
25185 +               AU_MVD_PR(dmsg, "unlink failed\n");
25186 +
25187 +       AuTraceErr(err);
25188 +       return err;
25189 +}
25190 +
25191 +/* Since mvdown succeeded, we ignore an error of this function */
25192 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25193 +{
25194 +       int err;
25195 +       struct au_branch *br;
25196 +
25197 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25198 +       br = au_sbr(a->sb, a->mvd_bsrc);
25199 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25200 +       if (!err) {
25201 +               br = au_sbr(a->sb, a->mvd_bdst);
25202 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25203 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25204 +       }
25205 +       if (!err)
25206 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25207 +       else
25208 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25209 +}
25210 +
25211 +/*
25212 + * copy-down the file and unlink the bsrc file.
25213 + * - unlink the bdst whout if exist
25214 + * - copy-down the file (with whtmp name and rename)
25215 + * - unlink the bsrc file
25216 + */
25217 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25218 +{
25219 +       int err;
25220 +
25221 +       err = au_do_mkdir(dmsg, a);
25222 +       if (!err)
25223 +               err = au_do_lock(dmsg, a);
25224 +       if (unlikely(err))
25225 +               goto out;
25226 +
25227 +       /*
25228 +        * do not revert the activities we made on bdst since they should be
25229 +        * harmless in aufs.
25230 +        */
25231 +
25232 +       err = au_do_cpdown(dmsg, a);
25233 +       if (!err)
25234 +               err = au_do_unlink_wh(dmsg, a);
25235 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25236 +               err = au_do_unlink(dmsg, a);
25237 +       if (unlikely(err))
25238 +               goto out_unlock;
25239 +
25240 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25241 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25242 +       if (find_lower_writable(a) < 0)
25243 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25244 +
25245 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25246 +               au_do_stfs(dmsg, a);
25247 +
25248 +       /* maintain internal array */
25249 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25250 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25251 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25252 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25253 +               au_set_ibtop(a->inode, a->mvd_bdst);
25254 +       } else {
25255 +               /* hide the lower */
25256 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25257 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25258 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25259 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25260 +       }
25261 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25262 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25263 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25264 +               au_set_ibbot(a->inode, a->mvd_bdst);
25265 +
25266 +out_unlock:
25267 +       au_do_unlock(dmsg, a);
25268 +out:
25269 +       AuTraceErr(err);
25270 +       return err;
25271 +}
25272 +
25273 +/* ---------------------------------------------------------------------- */
25274 +
25275 +/* make sure the file is idle */
25276 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25277 +{
25278 +       int err, plinked;
25279 +
25280 +       err = 0;
25281 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25282 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25283 +           && au_dcount(a->dentry) == 1
25284 +           && atomic_read(&a->inode->i_count) == 1
25285 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25286 +           && (!plinked || !au_plink_test(a->inode))
25287 +           && a->inode->i_nlink == 1)
25288 +               goto out;
25289 +
25290 +       err = -EBUSY;
25291 +       AU_MVD_PR(dmsg,
25292 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25293 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25294 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25295 +                 a->mvd_h_src_inode->i_nlink,
25296 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25297 +
25298 +out:
25299 +       AuTraceErr(err);
25300 +       return err;
25301 +}
25302 +
25303 +/* make sure the parent dir is fine */
25304 +static int au_mvd_args_parent(const unsigned char dmsg,
25305 +                             struct au_mvd_args *a)
25306 +{
25307 +       int err;
25308 +       aufs_bindex_t bindex;
25309 +
25310 +       err = 0;
25311 +       if (unlikely(au_alive_dir(a->parent))) {
25312 +               err = -ENOENT;
25313 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25314 +               goto out;
25315 +       }
25316 +
25317 +       a->bopq = au_dbdiropq(a->parent);
25318 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25319 +       AuDbg("b%d\n", bindex);
25320 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25321 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25322 +               err = -EINVAL;
25323 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25324 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25325 +                         a->bopq, a->mvd_bdst);
25326 +       }
25327 +
25328 +out:
25329 +       AuTraceErr(err);
25330 +       return err;
25331 +}
25332 +
25333 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25334 +                                   struct au_mvd_args *a)
25335 +{
25336 +       int err;
25337 +       struct au_dinfo *dinfo, *tmp;
25338 +
25339 +       /* lookup the next lower positive entry */
25340 +       err = -ENOMEM;
25341 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25342 +       if (unlikely(!tmp))
25343 +               goto out;
25344 +
25345 +       a->bfound = -1;
25346 +       a->bwh = -1;
25347 +       dinfo = au_di(a->dentry);
25348 +       au_di_cp(tmp, dinfo);
25349 +       au_di_swap(tmp, dinfo);
25350 +
25351 +       /* returns the number of positive dentries */
25352 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25353 +                            /* AuLkup_IGNORE_PERM */ 0);
25354 +       if (!err)
25355 +               a->bwh = au_dbwh(a->dentry);
25356 +       else if (err > 0)
25357 +               a->bfound = au_dbtop(a->dentry);
25358 +
25359 +       au_di_swap(tmp, dinfo);
25360 +       au_rw_write_unlock(&tmp->di_rwsem);
25361 +       au_di_free(tmp);
25362 +       if (unlikely(err < 0))
25363 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25364 +
25365 +       /*
25366 +        * here, we have these cases.
25367 +        * bfound == -1
25368 +        *      no positive dentry under bsrc. there are more sub-cases.
25369 +        *      bwh < 0
25370 +        *              there no whiteout, we can safely move-down.
25371 +        *      bwh <= bsrc
25372 +        *              impossible
25373 +        *      bsrc < bwh && bwh < bdst
25374 +        *              there is a whiteout on RO branch. cannot proceed.
25375 +        *      bwh == bdst
25376 +        *              there is a whiteout on the RW target branch. it should
25377 +        *              be removed.
25378 +        *      bdst < bwh
25379 +        *              there is a whiteout somewhere unrelated branch.
25380 +        * -1 < bfound && bfound <= bsrc
25381 +        *      impossible.
25382 +        * bfound < bdst
25383 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25384 +        *      proceed.
25385 +        * bfound == bdst
25386 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25387 +        *      error.
25388 +        * bdst < bfound
25389 +        *      found, after we create the file on bdst, it will be hidden.
25390 +        */
25391 +
25392 +       AuDebugOn(a->bfound == -1
25393 +                 && a->bwh != -1
25394 +                 && a->bwh <= a->mvd_bsrc);
25395 +       AuDebugOn(-1 < a->bfound
25396 +                 && a->bfound <= a->mvd_bsrc);
25397 +
25398 +       err = -EINVAL;
25399 +       if (a->bfound == -1
25400 +           && a->mvd_bsrc < a->bwh
25401 +           && a->bwh != -1
25402 +           && a->bwh < a->mvd_bdst) {
25403 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25404 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25405 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25406 +               goto out;
25407 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25408 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25409 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25410 +                         a->mvd_bdst, a->bfound);
25411 +               goto out;
25412 +       }
25413 +
25414 +       err = 0; /* success */
25415 +
25416 +out:
25417 +       AuTraceErr(err);
25418 +       return err;
25419 +}
25420 +
25421 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25422 +{
25423 +       int err;
25424 +
25425 +       err = 0;
25426 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25427 +           && a->bfound == a->mvd_bdst)
25428 +               err = -EEXIST;
25429 +       AuTraceErr(err);
25430 +       return err;
25431 +}
25432 +
25433 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25434 +{
25435 +       int err;
25436 +       struct au_branch *br;
25437 +
25438 +       err = -EISDIR;
25439 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25440 +               goto out;
25441 +
25442 +       err = -EINVAL;
25443 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25444 +               a->mvd_bsrc = au_ibtop(a->inode);
25445 +       else {
25446 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25447 +               if (unlikely(a->mvd_bsrc < 0
25448 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25449 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25450 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25451 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25452 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25453 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25454 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25455 +                       AU_MVD_PR(dmsg, "no upper\n");
25456 +                       goto out;
25457 +               }
25458 +       }
25459 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25460 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25461 +               AU_MVD_PR(dmsg, "on the bottom\n");
25462 +               goto out;
25463 +       }
25464 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25465 +       br = au_sbr(a->sb, a->mvd_bsrc);
25466 +       err = au_br_rdonly(br);
25467 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25468 +               if (unlikely(err))
25469 +                       goto out;
25470 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25471 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25472 +               if (err)
25473 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25474 +               /* go on */
25475 +       } else
25476 +               goto out;
25477 +
25478 +       err = -EINVAL;
25479 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25480 +               a->mvd_bdst = find_lower_writable(a);
25481 +               if (unlikely(a->mvd_bdst < 0)) {
25482 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25483 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25484 +                       goto out;
25485 +               }
25486 +       } else {
25487 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25488 +               if (unlikely(a->mvd_bdst < 0
25489 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25490 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25491 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25492 +                       goto out;
25493 +               }
25494 +       }
25495 +
25496 +       err = au_mvd_args_busy(dmsg, a);
25497 +       if (!err)
25498 +               err = au_mvd_args_parent(dmsg, a);
25499 +       if (!err)
25500 +               err = au_mvd_args_intermediate(dmsg, a);
25501 +       if (!err)
25502 +               err = au_mvd_args_exist(dmsg, a);
25503 +       if (!err)
25504 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25505 +
25506 +out:
25507 +       AuTraceErr(err);
25508 +       return err;
25509 +}
25510 +
25511 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25512 +{
25513 +       int err, e;
25514 +       unsigned char dmsg;
25515 +       struct au_mvd_args *args;
25516 +       struct inode *inode;
25517 +
25518 +       inode = d_inode(dentry);
25519 +       err = -EPERM;
25520 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25521 +               goto out;
25522 +
25523 +       err = -ENOMEM;
25524 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25525 +       if (unlikely(!args))
25526 +               goto out;
25527 +
25528 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25529 +       if (!err)
25530 +               /* VERIFY_WRITE */
25531 +               err = !access_ok(uarg, sizeof(*uarg));
25532 +       if (unlikely(err)) {
25533 +               err = -EFAULT;
25534 +               AuTraceErr(err);
25535 +               goto out_free;
25536 +       }
25537 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25538 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25539 +       args->mvdown.au_errno = 0;
25540 +       args->dentry = dentry;
25541 +       args->inode = inode;
25542 +       args->sb = dentry->d_sb;
25543 +
25544 +       err = -ENOENT;
25545 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25546 +       args->parent = dget_parent(dentry);
25547 +       args->dir = d_inode(args->parent);
25548 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25549 +       dput(args->parent);
25550 +       if (unlikely(args->parent != dentry->d_parent)) {
25551 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25552 +               goto out_dir;
25553 +       }
25554 +
25555 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25556 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25557 +       if (unlikely(err))
25558 +               goto out_inode;
25559 +
25560 +       di_write_lock_parent(args->parent);
25561 +       err = au_mvd_args(dmsg, args);
25562 +       if (unlikely(err))
25563 +               goto out_parent;
25564 +
25565 +       err = au_do_mvdown(dmsg, args);
25566 +       if (unlikely(err))
25567 +               goto out_parent;
25568 +
25569 +       au_cpup_attr_timesizes(args->dir);
25570 +       au_cpup_attr_timesizes(inode);
25571 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25572 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25573 +       /* au_digen_dec(dentry); */
25574 +
25575 +out_parent:
25576 +       di_write_unlock(args->parent);
25577 +       aufs_read_unlock(dentry, AuLock_DW);
25578 +out_inode:
25579 +       inode_unlock(inode);
25580 +out_dir:
25581 +       inode_unlock(args->dir);
25582 +out_free:
25583 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25584 +       if (unlikely(e))
25585 +               err = -EFAULT;
25586 +       au_kfree_rcu(args);
25587 +out:
25588 +       AuTraceErr(err);
25589 +       return err;
25590 +}
25591 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25592 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25593 +++ linux/fs/aufs/opts.c        2021-11-01 23:48:34.209692595 +0100
25594 @@ -0,0 +1,1880 @@
25595 +// SPDX-License-Identifier: GPL-2.0
25596 +/*
25597 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25598 + *
25599 + * This program, aufs is free software; you can redistribute it and/or modify
25600 + * it under the terms of the GNU General Public License as published by
25601 + * the Free Software Foundation; either version 2 of the License, or
25602 + * (at your option) any later version.
25603 + *
25604 + * This program is distributed in the hope that it will be useful,
25605 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25606 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25607 + * GNU General Public License for more details.
25608 + *
25609 + * You should have received a copy of the GNU General Public License
25610 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25611 + */
25612 +
25613 +/*
25614 + * mount options/flags
25615 + */
25616 +
25617 +#include <linux/namei.h>
25618 +#include <linux/types.h> /* a distribution requires */
25619 +#include <linux/parser.h>
25620 +#include "aufs.h"
25621 +
25622 +/* ---------------------------------------------------------------------- */
25623 +
25624 +enum {
25625 +       Opt_br,
25626 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25627 +       Opt_idel, Opt_imod,
25628 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25629 +       Opt_rdblk_def, Opt_rdhash_def,
25630 +       Opt_xino, Opt_noxino,
25631 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25632 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25633 +       Opt_trunc_xib, Opt_notrunc_xib,
25634 +       Opt_shwh, Opt_noshwh,
25635 +       Opt_plink, Opt_noplink, Opt_list_plink,
25636 +       Opt_udba,
25637 +       Opt_dio, Opt_nodio,
25638 +       Opt_diropq_a, Opt_diropq_w,
25639 +       Opt_warn_perm, Opt_nowarn_perm,
25640 +       Opt_wbr_copyup, Opt_wbr_create,
25641 +       Opt_fhsm_sec,
25642 +       Opt_verbose, Opt_noverbose,
25643 +       Opt_sum, Opt_nosum, Opt_wsum,
25644 +       Opt_dirperm1, Opt_nodirperm1,
25645 +       Opt_dirren, Opt_nodirren,
25646 +       Opt_acl, Opt_noacl,
25647 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25648 +};
25649 +
25650 +static match_table_t options = {
25651 +       {Opt_br, "br=%s"},
25652 +       {Opt_br, "br:%s"},
25653 +
25654 +       {Opt_add, "add=%d:%s"},
25655 +       {Opt_add, "add:%d:%s"},
25656 +       {Opt_add, "ins=%d:%s"},
25657 +       {Opt_add, "ins:%d:%s"},
25658 +       {Opt_append, "append=%s"},
25659 +       {Opt_append, "append:%s"},
25660 +       {Opt_prepend, "prepend=%s"},
25661 +       {Opt_prepend, "prepend:%s"},
25662 +
25663 +       {Opt_del, "del=%s"},
25664 +       {Opt_del, "del:%s"},
25665 +       /* {Opt_idel, "idel:%d"}, */
25666 +       {Opt_mod, "mod=%s"},
25667 +       {Opt_mod, "mod:%s"},
25668 +       /* {Opt_imod, "imod:%d:%s"}, */
25669 +
25670 +       {Opt_dirwh, "dirwh=%d"},
25671 +
25672 +       {Opt_xino, "xino=%s"},
25673 +       {Opt_noxino, "noxino"},
25674 +       {Opt_trunc_xino, "trunc_xino"},
25675 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25676 +       {Opt_notrunc_xino, "notrunc_xino"},
25677 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25678 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25679 +       /* {Opt_zxino, "zxino=%s"}, */
25680 +       {Opt_trunc_xib, "trunc_xib"},
25681 +       {Opt_notrunc_xib, "notrunc_xib"},
25682 +
25683 +#ifdef CONFIG_PROC_FS
25684 +       {Opt_plink, "plink"},
25685 +#else
25686 +       {Opt_ignore_silent, "plink"},
25687 +#endif
25688 +
25689 +       {Opt_noplink, "noplink"},
25690 +
25691 +#ifdef CONFIG_AUFS_DEBUG
25692 +       {Opt_list_plink, "list_plink"},
25693 +#endif
25694 +
25695 +       {Opt_udba, "udba=%s"},
25696 +
25697 +       {Opt_dio, "dio"},
25698 +       {Opt_nodio, "nodio"},
25699 +
25700 +#ifdef CONFIG_AUFS_DIRREN
25701 +       {Opt_dirren, "dirren"},
25702 +       {Opt_nodirren, "nodirren"},
25703 +#else
25704 +       {Opt_ignore, "dirren"},
25705 +       {Opt_ignore_silent, "nodirren"},
25706 +#endif
25707 +
25708 +#ifdef CONFIG_AUFS_FHSM
25709 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25710 +#else
25711 +       {Opt_ignore, "fhsm_sec=%d"},
25712 +#endif
25713 +
25714 +       {Opt_diropq_a, "diropq=always"},
25715 +       {Opt_diropq_a, "diropq=a"},
25716 +       {Opt_diropq_w, "diropq=whiteouted"},
25717 +       {Opt_diropq_w, "diropq=w"},
25718 +
25719 +       {Opt_warn_perm, "warn_perm"},
25720 +       {Opt_nowarn_perm, "nowarn_perm"},
25721 +
25722 +       /* keep them temporary */
25723 +       {Opt_ignore_silent, "nodlgt"},
25724 +       {Opt_ignore, "clean_plink"},
25725 +
25726 +#ifdef CONFIG_AUFS_SHWH
25727 +       {Opt_shwh, "shwh"},
25728 +#endif
25729 +       {Opt_noshwh, "noshwh"},
25730 +
25731 +       {Opt_dirperm1, "dirperm1"},
25732 +       {Opt_nodirperm1, "nodirperm1"},
25733 +
25734 +       {Opt_verbose, "verbose"},
25735 +       {Opt_verbose, "v"},
25736 +       {Opt_noverbose, "noverbose"},
25737 +       {Opt_noverbose, "quiet"},
25738 +       {Opt_noverbose, "q"},
25739 +       {Opt_noverbose, "silent"},
25740 +
25741 +       {Opt_sum, "sum"},
25742 +       {Opt_nosum, "nosum"},
25743 +       {Opt_wsum, "wsum"},
25744 +
25745 +       {Opt_rdcache, "rdcache=%d"},
25746 +       {Opt_rdblk, "rdblk=%d"},
25747 +       {Opt_rdblk_def, "rdblk=def"},
25748 +       {Opt_rdhash, "rdhash=%d"},
25749 +       {Opt_rdhash_def, "rdhash=def"},
25750 +
25751 +       {Opt_wbr_create, "create=%s"},
25752 +       {Opt_wbr_create, "create_policy=%s"},
25753 +       {Opt_wbr_copyup, "cpup=%s"},
25754 +       {Opt_wbr_copyup, "copyup=%s"},
25755 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25756 +
25757 +       /* generic VFS flag */
25758 +#ifdef CONFIG_FS_POSIX_ACL
25759 +       {Opt_acl, "acl"},
25760 +       {Opt_noacl, "noacl"},
25761 +#else
25762 +       {Opt_ignore, "acl"},
25763 +       {Opt_ignore_silent, "noacl"},
25764 +#endif
25765 +
25766 +       /* internal use for the scripts */
25767 +       {Opt_ignore_silent, "si=%s"},
25768 +
25769 +       {Opt_br, "dirs=%s"},
25770 +       {Opt_ignore, "debug=%d"},
25771 +       {Opt_ignore, "delete=whiteout"},
25772 +       {Opt_ignore, "delete=all"},
25773 +       {Opt_ignore, "imap=%s"},
25774 +
25775 +       /* temporary workaround, due to old mount(8)? */
25776 +       {Opt_ignore_silent, "relatime"},
25777 +
25778 +       {Opt_err, NULL}
25779 +};
25780 +
25781 +/* ---------------------------------------------------------------------- */
25782 +
25783 +static const char *au_parser_pattern(int val, match_table_t tbl)
25784 +{
25785 +       struct match_token *p;
25786 +
25787 +       p = tbl;
25788 +       while (p->pattern) {
25789 +               if (p->token == val)
25790 +                       return p->pattern;
25791 +               p++;
25792 +       }
25793 +       BUG();
25794 +       return "??";
25795 +}
25796 +
25797 +static const char *au_optstr(int *val, match_table_t tbl)
25798 +{
25799 +       struct match_token *p;
25800 +       int v;
25801 +
25802 +       v = *val;
25803 +       if (!v)
25804 +               goto out;
25805 +       p = tbl;
25806 +       while (p->pattern) {
25807 +               if (p->token
25808 +                   && (v & p->token) == p->token) {
25809 +                       *val &= ~p->token;
25810 +                       return p->pattern;
25811 +               }
25812 +               p++;
25813 +       }
25814 +
25815 +out:
25816 +       return NULL;
25817 +}
25818 +
25819 +/* ---------------------------------------------------------------------- */
25820 +
25821 +static match_table_t brperm = {
25822 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25823 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25824 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25825 +       {0, NULL}
25826 +};
25827 +
25828 +static match_table_t brattr = {
25829 +       /* general */
25830 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25831 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25832 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25833 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25834 +#ifdef CONFIG_AUFS_FHSM
25835 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25836 +#endif
25837 +#ifdef CONFIG_AUFS_XATTR
25838 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25839 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25840 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25841 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25842 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25843 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25844 +#endif
25845 +
25846 +       /* ro/rr branch */
25847 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25848 +
25849 +       /* rw branch */
25850 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25851 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25852 +
25853 +       {0, NULL}
25854 +};
25855 +
25856 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25857 +{
25858 +       int attr, v;
25859 +       char *p;
25860 +
25861 +       attr = 0;
25862 +       do {
25863 +               p = strchr(str, '+');
25864 +               if (p)
25865 +                       *p = 0;
25866 +               v = match_token(str, table, args);
25867 +               if (v) {
25868 +                       if (v & AuBrAttr_CMOO_Mask)
25869 +                               attr &= ~AuBrAttr_CMOO_Mask;
25870 +                       attr |= v;
25871 +               } else {
25872 +                       if (p)
25873 +                               *p = '+';
25874 +                       pr_warn("ignored branch attribute %s\n", str);
25875 +                       break;
25876 +               }
25877 +               if (p)
25878 +                       str = p + 1;
25879 +       } while (p);
25880 +
25881 +       return attr;
25882 +}
25883 +
25884 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25885 +{
25886 +       int sz;
25887 +       const char *p;
25888 +       char *q;
25889 +
25890 +       q = str->a;
25891 +       *q = 0;
25892 +       p = au_optstr(&perm, brattr);
25893 +       if (p) {
25894 +               sz = strlen(p);
25895 +               memcpy(q, p, sz + 1);
25896 +               q += sz;
25897 +       } else
25898 +               goto out;
25899 +
25900 +       do {
25901 +               p = au_optstr(&perm, brattr);
25902 +               if (p) {
25903 +                       *q++ = '+';
25904 +                       sz = strlen(p);
25905 +                       memcpy(q, p, sz + 1);
25906 +                       q += sz;
25907 +               }
25908 +       } while (p);
25909 +
25910 +out:
25911 +       return q - str->a;
25912 +}
25913 +
25914 +static int noinline_for_stack br_perm_val(char *perm)
25915 +{
25916 +       int val, bad, sz;
25917 +       char *p;
25918 +       substring_t args[MAX_OPT_ARGS];
25919 +       au_br_perm_str_t attr;
25920 +
25921 +       p = strchr(perm, '+');
25922 +       if (p)
25923 +               *p = 0;
25924 +       val = match_token(perm, brperm, args);
25925 +       if (!val) {
25926 +               if (p)
25927 +                       *p = '+';
25928 +               pr_warn("ignored branch permission %s\n", perm);
25929 +               val = AuBrPerm_RO;
25930 +               goto out;
25931 +       }
25932 +       if (!p)
25933 +               goto out;
25934 +
25935 +       val |= br_attr_val(p + 1, brattr, args);
25936 +
25937 +       bad = 0;
25938 +       switch (val & AuBrPerm_Mask) {
25939 +       case AuBrPerm_RO:
25940 +       case AuBrPerm_RR:
25941 +               bad = val & AuBrWAttr_Mask;
25942 +               val &= ~AuBrWAttr_Mask;
25943 +               break;
25944 +       case AuBrPerm_RW:
25945 +               bad = val & AuBrRAttr_Mask;
25946 +               val &= ~AuBrRAttr_Mask;
25947 +               break;
25948 +       }
25949 +
25950 +       /*
25951 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25952 +        * does not treat it as an error, just warning.
25953 +        * this is a tiny guard for the user operation.
25954 +        */
25955 +       if (val & AuBrAttr_UNPIN) {
25956 +               bad |= AuBrAttr_UNPIN;
25957 +               val &= ~AuBrAttr_UNPIN;
25958 +       }
25959 +
25960 +       if (unlikely(bad)) {
25961 +               sz = au_do_optstr_br_attr(&attr, bad);
25962 +               AuDebugOn(!sz);
25963 +               pr_warn("ignored branch attribute %s\n", attr.a);
25964 +       }
25965 +
25966 +out:
25967 +       return val;
25968 +}
25969 +
25970 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25971 +{
25972 +       au_br_perm_str_t attr;
25973 +       const char *p;
25974 +       char *q;
25975 +       int sz;
25976 +
25977 +       q = str->a;
25978 +       p = au_optstr(&perm, brperm);
25979 +       AuDebugOn(!p || !*p);
25980 +       sz = strlen(p);
25981 +       memcpy(q, p, sz + 1);
25982 +       q += sz;
25983 +
25984 +       sz = au_do_optstr_br_attr(&attr, perm);
25985 +       if (sz) {
25986 +               *q++ = '+';
25987 +               memcpy(q, attr.a, sz + 1);
25988 +       }
25989 +
25990 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
25991 +}
25992 +
25993 +/* ---------------------------------------------------------------------- */
25994 +
25995 +static match_table_t udbalevel = {
25996 +       {AuOpt_UDBA_REVAL, "reval"},
25997 +       {AuOpt_UDBA_NONE, "none"},
25998 +#ifdef CONFIG_AUFS_HNOTIFY
25999 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26000 +#ifdef CONFIG_AUFS_HFSNOTIFY
26001 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26002 +#endif
26003 +#endif
26004 +       {-1, NULL}
26005 +};
26006 +
26007 +static int noinline_for_stack udba_val(char *str)
26008 +{
26009 +       substring_t args[MAX_OPT_ARGS];
26010 +
26011 +       return match_token(str, udbalevel, args);
26012 +}
26013 +
26014 +const char *au_optstr_udba(int udba)
26015 +{
26016 +       return au_parser_pattern(udba, udbalevel);
26017 +}
26018 +
26019 +/* ---------------------------------------------------------------------- */
26020 +
26021 +static match_table_t au_wbr_create_policy = {
26022 +       {AuWbrCreate_TDP, "tdp"},
26023 +       {AuWbrCreate_TDP, "top-down-parent"},
26024 +       {AuWbrCreate_RR, "rr"},
26025 +       {AuWbrCreate_RR, "round-robin"},
26026 +       {AuWbrCreate_MFS, "mfs"},
26027 +       {AuWbrCreate_MFS, "most-free-space"},
26028 +       {AuWbrCreate_MFSV, "mfs:%d"},
26029 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26030 +
26031 +       /* top-down regardless the parent, and then mfs */
26032 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26033 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26034 +
26035 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26036 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26037 +       {AuWbrCreate_PMFS, "pmfs"},
26038 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26039 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26040 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26041 +
26042 +       {-1, NULL}
26043 +};
26044 +
26045 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26046 +                           struct au_opt_wbr_create *create)
26047 +{
26048 +       int err;
26049 +       unsigned long long ull;
26050 +
26051 +       err = 0;
26052 +       if (!match_u64(arg, &ull))
26053 +               create->mfsrr_watermark = ull;
26054 +       else {
26055 +               pr_err("bad integer in %s\n", str);
26056 +               err = -EINVAL;
26057 +       }
26058 +
26059 +       return err;
26060 +}
26061 +
26062 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26063 +                         struct au_opt_wbr_create *create)
26064 +{
26065 +       int n, err;
26066 +
26067 +       err = 0;
26068 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26069 +               create->mfs_second = n;
26070 +       else {
26071 +               pr_err("bad integer in %s\n", str);
26072 +               err = -EINVAL;
26073 +       }
26074 +
26075 +       return err;
26076 +}
26077 +
26078 +static int noinline_for_stack
26079 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26080 +{
26081 +       int err, e;
26082 +       substring_t args[MAX_OPT_ARGS];
26083 +
26084 +       err = match_token(str, au_wbr_create_policy, args);
26085 +       create->wbr_create = err;
26086 +       switch (err) {
26087 +       case AuWbrCreate_MFSRRV:
26088 +       case AuWbrCreate_TDMFSV:
26089 +       case AuWbrCreate_PMFSRRV:
26090 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26091 +               if (!e)
26092 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26093 +               if (unlikely(e))
26094 +                       err = e;
26095 +               break;
26096 +       case AuWbrCreate_MFSRR:
26097 +       case AuWbrCreate_TDMFS:
26098 +       case AuWbrCreate_PMFSRR:
26099 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26100 +               if (unlikely(e)) {
26101 +                       err = e;
26102 +                       break;
26103 +               }
26104 +               fallthrough;
26105 +       case AuWbrCreate_MFS:
26106 +       case AuWbrCreate_PMFS:
26107 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26108 +               break;
26109 +       case AuWbrCreate_MFSV:
26110 +       case AuWbrCreate_PMFSV:
26111 +               e = au_wbr_mfs_sec(&args[0], str, create);
26112 +               if (unlikely(e))
26113 +                       err = e;
26114 +               break;
26115 +       }
26116 +
26117 +       return err;
26118 +}
26119 +
26120 +const char *au_optstr_wbr_create(int wbr_create)
26121 +{
26122 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26123 +}
26124 +
26125 +static match_table_t au_wbr_copyup_policy = {
26126 +       {AuWbrCopyup_TDP, "tdp"},
26127 +       {AuWbrCopyup_TDP, "top-down-parent"},
26128 +       {AuWbrCopyup_BUP, "bup"},
26129 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26130 +       {AuWbrCopyup_BU, "bu"},
26131 +       {AuWbrCopyup_BU, "bottom-up"},
26132 +       {-1, NULL}
26133 +};
26134 +
26135 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26136 +{
26137 +       substring_t args[MAX_OPT_ARGS];
26138 +
26139 +       return match_token(str, au_wbr_copyup_policy, args);
26140 +}
26141 +
26142 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26143 +{
26144 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26145 +}
26146 +
26147 +/* ---------------------------------------------------------------------- */
26148 +
26149 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26150 +
26151 +static void dump_opts(struct au_opts *opts)
26152 +{
26153 +#ifdef CONFIG_AUFS_DEBUG
26154 +       /* reduce stack space */
26155 +       union {
26156 +               struct au_opt_add *add;
26157 +               struct au_opt_del *del;
26158 +               struct au_opt_mod *mod;
26159 +               struct au_opt_xino *xino;
26160 +               struct au_opt_xino_itrunc *xino_itrunc;
26161 +               struct au_opt_wbr_create *create;
26162 +       } u;
26163 +       struct au_opt *opt;
26164 +
26165 +       opt = opts->opt;
26166 +       while (opt->type != Opt_tail) {
26167 +               switch (opt->type) {
26168 +               case Opt_add:
26169 +                       u.add = &opt->add;
26170 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26171 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26172 +                                 u.add->path.dentry);
26173 +                       break;
26174 +               case Opt_del:
26175 +               case Opt_idel:
26176 +                       u.del = &opt->del;
26177 +                       AuDbg("del {%s, %p}\n",
26178 +                             u.del->pathname, u.del->h_path.dentry);
26179 +                       break;
26180 +               case Opt_mod:
26181 +               case Opt_imod:
26182 +                       u.mod = &opt->mod;
26183 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26184 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26185 +                       break;
26186 +               case Opt_append:
26187 +                       u.add = &opt->add;
26188 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26189 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26190 +                                 u.add->path.dentry);
26191 +                       break;
26192 +               case Opt_prepend:
26193 +                       u.add = &opt->add;
26194 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26195 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26196 +                                 u.add->path.dentry);
26197 +                       break;
26198 +               case Opt_dirwh:
26199 +                       AuDbg("dirwh %d\n", opt->dirwh);
26200 +                       break;
26201 +               case Opt_rdcache:
26202 +                       AuDbg("rdcache %d\n", opt->rdcache);
26203 +                       break;
26204 +               case Opt_rdblk:
26205 +                       AuDbg("rdblk %u\n", opt->rdblk);
26206 +                       break;
26207 +               case Opt_rdblk_def:
26208 +                       AuDbg("rdblk_def\n");
26209 +                       break;
26210 +               case Opt_rdhash:
26211 +                       AuDbg("rdhash %u\n", opt->rdhash);
26212 +                       break;
26213 +               case Opt_rdhash_def:
26214 +                       AuDbg("rdhash_def\n");
26215 +                       break;
26216 +               case Opt_xino:
26217 +                       u.xino = &opt->xino;
26218 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26219 +                       break;
26220 +               case Opt_trunc_xino:
26221 +                       AuLabel(trunc_xino);
26222 +                       break;
26223 +               case Opt_notrunc_xino:
26224 +                       AuLabel(notrunc_xino);
26225 +                       break;
26226 +               case Opt_trunc_xino_path:
26227 +               case Opt_itrunc_xino:
26228 +                       u.xino_itrunc = &opt->xino_itrunc;
26229 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26230 +                       break;
26231 +               case Opt_noxino:
26232 +                       AuLabel(noxino);
26233 +                       break;
26234 +               case Opt_trunc_xib:
26235 +                       AuLabel(trunc_xib);
26236 +                       break;
26237 +               case Opt_notrunc_xib:
26238 +                       AuLabel(notrunc_xib);
26239 +                       break;
26240 +               case Opt_shwh:
26241 +                       AuLabel(shwh);
26242 +                       break;
26243 +               case Opt_noshwh:
26244 +                       AuLabel(noshwh);
26245 +                       break;
26246 +               case Opt_dirperm1:
26247 +                       AuLabel(dirperm1);
26248 +                       break;
26249 +               case Opt_nodirperm1:
26250 +                       AuLabel(nodirperm1);
26251 +                       break;
26252 +               case Opt_plink:
26253 +                       AuLabel(plink);
26254 +                       break;
26255 +               case Opt_noplink:
26256 +                       AuLabel(noplink);
26257 +                       break;
26258 +               case Opt_list_plink:
26259 +                       AuLabel(list_plink);
26260 +                       break;
26261 +               case Opt_udba:
26262 +                       AuDbg("udba %d, %s\n",
26263 +                                 opt->udba, au_optstr_udba(opt->udba));
26264 +                       break;
26265 +               case Opt_dio:
26266 +                       AuLabel(dio);
26267 +                       break;
26268 +               case Opt_nodio:
26269 +                       AuLabel(nodio);
26270 +                       break;
26271 +               case Opt_diropq_a:
26272 +                       AuLabel(diropq_a);
26273 +                       break;
26274 +               case Opt_diropq_w:
26275 +                       AuLabel(diropq_w);
26276 +                       break;
26277 +               case Opt_warn_perm:
26278 +                       AuLabel(warn_perm);
26279 +                       break;
26280 +               case Opt_nowarn_perm:
26281 +                       AuLabel(nowarn_perm);
26282 +                       break;
26283 +               case Opt_verbose:
26284 +                       AuLabel(verbose);
26285 +                       break;
26286 +               case Opt_noverbose:
26287 +                       AuLabel(noverbose);
26288 +                       break;
26289 +               case Opt_sum:
26290 +                       AuLabel(sum);
26291 +                       break;
26292 +               case Opt_nosum:
26293 +                       AuLabel(nosum);
26294 +                       break;
26295 +               case Opt_wsum:
26296 +                       AuLabel(wsum);
26297 +                       break;
26298 +               case Opt_wbr_create:
26299 +                       u.create = &opt->wbr_create;
26300 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26301 +                                 au_optstr_wbr_create(u.create->wbr_create));
26302 +                       switch (u.create->wbr_create) {
26303 +                       case AuWbrCreate_MFSV:
26304 +                       case AuWbrCreate_PMFSV:
26305 +                               AuDbg("%d sec\n", u.create->mfs_second);
26306 +                               break;
26307 +                       case AuWbrCreate_MFSRR:
26308 +                       case AuWbrCreate_TDMFS:
26309 +                               AuDbg("%llu watermark\n",
26310 +                                         u.create->mfsrr_watermark);
26311 +                               break;
26312 +                       case AuWbrCreate_MFSRRV:
26313 +                       case AuWbrCreate_TDMFSV:
26314 +                       case AuWbrCreate_PMFSRRV:
26315 +                               AuDbg("%llu watermark, %d sec\n",
26316 +                                         u.create->mfsrr_watermark,
26317 +                                         u.create->mfs_second);
26318 +                               break;
26319 +                       }
26320 +                       break;
26321 +               case Opt_wbr_copyup:
26322 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26323 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26324 +                       break;
26325 +               case Opt_fhsm_sec:
26326 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26327 +                       break;
26328 +               case Opt_dirren:
26329 +                       AuLabel(dirren);
26330 +                       break;
26331 +               case Opt_nodirren:
26332 +                       AuLabel(nodirren);
26333 +                       break;
26334 +               case Opt_acl:
26335 +                       AuLabel(acl);
26336 +                       break;
26337 +               case Opt_noacl:
26338 +                       AuLabel(noacl);
26339 +                       break;
26340 +               default:
26341 +                       BUG();
26342 +               }
26343 +               opt++;
26344 +       }
26345 +#endif
26346 +}
26347 +
26348 +void au_opts_free(struct au_opts *opts)
26349 +{
26350 +       struct au_opt *opt;
26351 +
26352 +       opt = opts->opt;
26353 +       while (opt->type != Opt_tail) {
26354 +               switch (opt->type) {
26355 +               case Opt_add:
26356 +               case Opt_append:
26357 +               case Opt_prepend:
26358 +                       path_put(&opt->add.path);
26359 +                       break;
26360 +               case Opt_del:
26361 +               case Opt_idel:
26362 +                       path_put(&opt->del.h_path);
26363 +                       break;
26364 +               case Opt_mod:
26365 +               case Opt_imod:
26366 +                       dput(opt->mod.h_root);
26367 +                       break;
26368 +               case Opt_xino:
26369 +                       fput(opt->xino.file);
26370 +                       break;
26371 +               }
26372 +               opt++;
26373 +       }
26374 +}
26375 +
26376 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26377 +                  aufs_bindex_t bindex)
26378 +{
26379 +       int err;
26380 +       struct au_opt_add *add = &opt->add;
26381 +       char *p;
26382 +
26383 +       add->bindex = bindex;
26384 +       add->perm = AuBrPerm_RO;
26385 +       add->pathname = opt_str;
26386 +       p = strchr(opt_str, '=');
26387 +       if (p) {
26388 +               *p++ = 0;
26389 +               if (*p)
26390 +                       add->perm = br_perm_val(p);
26391 +       }
26392 +
26393 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26394 +       if (!err) {
26395 +               if (!p) {
26396 +                       add->perm = AuBrPerm_RO;
26397 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26398 +                               add->perm = AuBrPerm_RR;
26399 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26400 +                               add->perm = AuBrPerm_RW;
26401 +               }
26402 +               opt->type = Opt_add;
26403 +               goto out;
26404 +       }
26405 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26406 +       err = -EINVAL;
26407 +
26408 +out:
26409 +       return err;
26410 +}
26411 +
26412 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26413 +{
26414 +       int err;
26415 +
26416 +       del->pathname = args[0].from;
26417 +       AuDbg("del path %s\n", del->pathname);
26418 +
26419 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26420 +       if (unlikely(err))
26421 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26422 +
26423 +       return err;
26424 +}
26425 +
26426 +#if 0 /* reserved for future use */
26427 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26428 +                             struct au_opt_del *del, substring_t args[])
26429 +{
26430 +       int err;
26431 +       struct dentry *root;
26432 +
26433 +       err = -EINVAL;
26434 +       root = sb->s_root;
26435 +       aufs_read_lock(root, AuLock_FLUSH);
26436 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26437 +               pr_err("out of bounds, %d\n", bindex);
26438 +               goto out;
26439 +       }
26440 +
26441 +       err = 0;
26442 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26443 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26444 +
26445 +out:
26446 +       aufs_read_unlock(root, !AuLock_IR);
26447 +       return err;
26448 +}
26449 +#endif
26450 +
26451 +static int noinline_for_stack
26452 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26453 +{
26454 +       int err;
26455 +       struct path path;
26456 +       char *p;
26457 +
26458 +       err = -EINVAL;
26459 +       mod->path = args[0].from;
26460 +       p = strchr(mod->path, '=');
26461 +       if (unlikely(!p)) {
26462 +               pr_err("no permission %s\n", args[0].from);
26463 +               goto out;
26464 +       }
26465 +
26466 +       *p++ = 0;
26467 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26468 +       if (unlikely(err)) {
26469 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26470 +               goto out;
26471 +       }
26472 +
26473 +       mod->perm = br_perm_val(p);
26474 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26475 +       mod->h_root = dget(path.dentry);
26476 +       path_put(&path);
26477 +
26478 +out:
26479 +       return err;
26480 +}
26481 +
26482 +#if 0 /* reserved for future use */
26483 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26484 +                             struct au_opt_mod *mod, substring_t args[])
26485 +{
26486 +       int err;
26487 +       struct dentry *root;
26488 +
26489 +       err = -EINVAL;
26490 +       root = sb->s_root;
26491 +       aufs_read_lock(root, AuLock_FLUSH);
26492 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26493 +               pr_err("out of bounds, %d\n", bindex);
26494 +               goto out;
26495 +       }
26496 +
26497 +       err = 0;
26498 +       mod->perm = br_perm_val(args[1].from);
26499 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26500 +             mod->path, mod->perm, args[1].from);
26501 +       mod->h_root = dget(au_h_dptr(root, bindex));
26502 +
26503 +out:
26504 +       aufs_read_unlock(root, !AuLock_IR);
26505 +       return err;
26506 +}
26507 +#endif
26508 +
26509 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26510 +                             substring_t args[])
26511 +{
26512 +       int err;
26513 +       struct file *file;
26514 +
26515 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26516 +       err = PTR_ERR(file);
26517 +       if (IS_ERR(file))
26518 +               goto out;
26519 +
26520 +       err = -EINVAL;
26521 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26522 +               fput(file);
26523 +               pr_err("%s must be outside\n", args[0].from);
26524 +               goto out;
26525 +       }
26526 +
26527 +       err = 0;
26528 +       xino->file = file;
26529 +       xino->path = args[0].from;
26530 +
26531 +out:
26532 +       return err;
26533 +}
26534 +
26535 +static int noinline_for_stack
26536 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26537 +                              struct au_opt_xino_itrunc *xino_itrunc,
26538 +                              substring_t args[])
26539 +{
26540 +       int err;
26541 +       aufs_bindex_t bbot, bindex;
26542 +       struct path path;
26543 +       struct dentry *root;
26544 +
26545 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26546 +       if (unlikely(err)) {
26547 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26548 +               goto out;
26549 +       }
26550 +
26551 +       xino_itrunc->bindex = -1;
26552 +       root = sb->s_root;
26553 +       aufs_read_lock(root, AuLock_FLUSH);
26554 +       bbot = au_sbbot(sb);
26555 +       for (bindex = 0; bindex <= bbot; bindex++) {
26556 +               if (au_h_dptr(root, bindex) == path.dentry) {
26557 +                       xino_itrunc->bindex = bindex;
26558 +                       break;
26559 +               }
26560 +       }
26561 +       aufs_read_unlock(root, !AuLock_IR);
26562 +       path_put(&path);
26563 +
26564 +       if (unlikely(xino_itrunc->bindex < 0)) {
26565 +               pr_err("no such branch %s\n", args[0].from);
26566 +               err = -EINVAL;
26567 +       }
26568 +
26569 +out:
26570 +       return err;
26571 +}
26572 +
26573 +/* called without aufs lock */
26574 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26575 +{
26576 +       int err, n, token;
26577 +       aufs_bindex_t bindex;
26578 +       unsigned char skipped;
26579 +       struct dentry *root;
26580 +       struct au_opt *opt, *opt_tail;
26581 +       char *opt_str;
26582 +       /* reduce the stack space */
26583 +       union {
26584 +               struct au_opt_xino_itrunc *xino_itrunc;
26585 +               struct au_opt_wbr_create *create;
26586 +       } u;
26587 +       struct {
26588 +               substring_t args[MAX_OPT_ARGS];
26589 +       } *a;
26590 +
26591 +       err = -ENOMEM;
26592 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26593 +       if (unlikely(!a))
26594 +               goto out;
26595 +
26596 +       root = sb->s_root;
26597 +       err = 0;
26598 +       bindex = 0;
26599 +       opt = opts->opt;
26600 +       opt_tail = opt + opts->max_opt - 1;
26601 +       opt->type = Opt_tail;
26602 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26603 +               err = -EINVAL;
26604 +               skipped = 0;
26605 +               token = match_token(opt_str, options, a->args);
26606 +               switch (token) {
26607 +               case Opt_br:
26608 +                       err = 0;
26609 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26610 +                              && *opt_str) {
26611 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26612 +                                             bindex++);
26613 +                               if (unlikely(!err && ++opt > opt_tail)) {
26614 +                                       err = -E2BIG;
26615 +                                       break;
26616 +                               }
26617 +                               opt->type = Opt_tail;
26618 +                               skipped = 1;
26619 +                       }
26620 +                       break;
26621 +               case Opt_add:
26622 +                       if (unlikely(match_int(&a->args[0], &n))) {
26623 +                               pr_err("bad integer in %s\n", opt_str);
26624 +                               break;
26625 +                       }
26626 +                       bindex = n;
26627 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26628 +                                     bindex);
26629 +                       if (!err)
26630 +                               opt->type = token;
26631 +                       break;
26632 +               case Opt_append:
26633 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26634 +                                     /*dummy bindex*/1);
26635 +                       if (!err)
26636 +                               opt->type = token;
26637 +                       break;
26638 +               case Opt_prepend:
26639 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26640 +                                     /*bindex*/0);
26641 +                       if (!err)
26642 +                               opt->type = token;
26643 +                       break;
26644 +               case Opt_del:
26645 +                       err = au_opts_parse_del(&opt->del, a->args);
26646 +                       if (!err)
26647 +                               opt->type = token;
26648 +                       break;
26649 +#if 0 /* reserved for future use */
26650 +               case Opt_idel:
26651 +                       del->pathname = "(indexed)";
26652 +                       if (unlikely(match_int(&args[0], &n))) {
26653 +                               pr_err("bad integer in %s\n", opt_str);
26654 +                               break;
26655 +                       }
26656 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26657 +                       if (!err)
26658 +                               opt->type = token;
26659 +                       break;
26660 +#endif
26661 +               case Opt_mod:
26662 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26663 +                       if (!err)
26664 +                               opt->type = token;
26665 +                       break;
26666 +#ifdef IMOD /* reserved for future use */
26667 +               case Opt_imod:
26668 +                       u.mod->path = "(indexed)";
26669 +                       if (unlikely(match_int(&a->args[0], &n))) {
26670 +                               pr_err("bad integer in %s\n", opt_str);
26671 +                               break;
26672 +                       }
26673 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26674 +                       if (!err)
26675 +                               opt->type = token;
26676 +                       break;
26677 +#endif
26678 +               case Opt_xino:
26679 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26680 +                       if (!err)
26681 +                               opt->type = token;
26682 +                       break;
26683 +
26684 +               case Opt_trunc_xino_path:
26685 +                       err = au_opts_parse_xino_itrunc_path
26686 +                               (sb, &opt->xino_itrunc, a->args);
26687 +                       if (!err)
26688 +                               opt->type = token;
26689 +                       break;
26690 +
26691 +               case Opt_itrunc_xino:
26692 +                       u.xino_itrunc = &opt->xino_itrunc;
26693 +                       if (unlikely(match_int(&a->args[0], &n))) {
26694 +                               pr_err("bad integer in %s\n", opt_str);
26695 +                               break;
26696 +                       }
26697 +                       u.xino_itrunc->bindex = n;
26698 +                       aufs_read_lock(root, AuLock_FLUSH);
26699 +                       if (n < 0 || au_sbbot(sb) < n) {
26700 +                               pr_err("out of bounds, %d\n", n);
26701 +                               aufs_read_unlock(root, !AuLock_IR);
26702 +                               break;
26703 +                       }
26704 +                       aufs_read_unlock(root, !AuLock_IR);
26705 +                       err = 0;
26706 +                       opt->type = token;
26707 +                       break;
26708 +
26709 +               case Opt_dirwh:
26710 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26711 +                               break;
26712 +                       err = 0;
26713 +                       opt->type = token;
26714 +                       break;
26715 +
26716 +               case Opt_rdcache:
26717 +                       if (unlikely(match_int(&a->args[0], &n))) {
26718 +                               pr_err("bad integer in %s\n", opt_str);
26719 +                               break;
26720 +                       }
26721 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26722 +                               pr_err("rdcache must be smaller than %d\n",
26723 +                                      AUFS_RDCACHE_MAX);
26724 +                               break;
26725 +                       }
26726 +                       opt->rdcache = n;
26727 +                       err = 0;
26728 +                       opt->type = token;
26729 +                       break;
26730 +               case Opt_rdblk:
26731 +                       if (unlikely(match_int(&a->args[0], &n)
26732 +                                    || n < 0
26733 +                                    || n > KMALLOC_MAX_SIZE)) {
26734 +                               pr_err("bad integer in %s\n", opt_str);
26735 +                               break;
26736 +                       }
26737 +                       if (unlikely(n && n < NAME_MAX)) {
26738 +                               pr_err("rdblk must be larger than %d\n",
26739 +                                      NAME_MAX);
26740 +                               break;
26741 +                       }
26742 +                       opt->rdblk = n;
26743 +                       err = 0;
26744 +                       opt->type = token;
26745 +                       break;
26746 +               case Opt_rdhash:
26747 +                       if (unlikely(match_int(&a->args[0], &n)
26748 +                                    || n < 0
26749 +                                    || n * sizeof(struct hlist_head)
26750 +                                    > KMALLOC_MAX_SIZE)) {
26751 +                               pr_err("bad integer in %s\n", opt_str);
26752 +                               break;
26753 +                       }
26754 +                       opt->rdhash = n;
26755 +                       err = 0;
26756 +                       opt->type = token;
26757 +                       break;
26758 +
26759 +               case Opt_trunc_xino:
26760 +               case Opt_notrunc_xino:
26761 +               case Opt_noxino:
26762 +               case Opt_trunc_xib:
26763 +               case Opt_notrunc_xib:
26764 +               case Opt_shwh:
26765 +               case Opt_noshwh:
26766 +               case Opt_dirperm1:
26767 +               case Opt_nodirperm1:
26768 +               case Opt_plink:
26769 +               case Opt_noplink:
26770 +               case Opt_list_plink:
26771 +               case Opt_dio:
26772 +               case Opt_nodio:
26773 +               case Opt_diropq_a:
26774 +               case Opt_diropq_w:
26775 +               case Opt_warn_perm:
26776 +               case Opt_nowarn_perm:
26777 +               case Opt_verbose:
26778 +               case Opt_noverbose:
26779 +               case Opt_sum:
26780 +               case Opt_nosum:
26781 +               case Opt_wsum:
26782 +               case Opt_rdblk_def:
26783 +               case Opt_rdhash_def:
26784 +               case Opt_dirren:
26785 +               case Opt_nodirren:
26786 +               case Opt_acl:
26787 +               case Opt_noacl:
26788 +                       err = 0;
26789 +                       opt->type = token;
26790 +                       break;
26791 +
26792 +               case Opt_udba:
26793 +                       opt->udba = udba_val(a->args[0].from);
26794 +                       if (opt->udba >= 0) {
26795 +                               err = 0;
26796 +                               opt->type = token;
26797 +                       } else
26798 +                               pr_err("wrong value, %s\n", opt_str);
26799 +                       break;
26800 +
26801 +               case Opt_wbr_create:
26802 +                       u.create = &opt->wbr_create;
26803 +                       u.create->wbr_create
26804 +                               = au_wbr_create_val(a->args[0].from, u.create);
26805 +                       if (u.create->wbr_create >= 0) {
26806 +                               err = 0;
26807 +                               opt->type = token;
26808 +                       } else
26809 +                               pr_err("wrong value, %s\n", opt_str);
26810 +                       break;
26811 +               case Opt_wbr_copyup:
26812 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26813 +                       if (opt->wbr_copyup >= 0) {
26814 +                               err = 0;
26815 +                               opt->type = token;
26816 +                       } else
26817 +                               pr_err("wrong value, %s\n", opt_str);
26818 +                       break;
26819 +
26820 +               case Opt_fhsm_sec:
26821 +                       if (unlikely(match_int(&a->args[0], &n)
26822 +                                    || n < 0)) {
26823 +                               pr_err("bad integer in %s\n", opt_str);
26824 +                               break;
26825 +                       }
26826 +                       if (sysaufs_brs) {
26827 +                               opt->fhsm_second = n;
26828 +                               opt->type = token;
26829 +                       } else
26830 +                               pr_warn("ignored %s\n", opt_str);
26831 +                       err = 0;
26832 +                       break;
26833 +
26834 +               case Opt_ignore:
26835 +                       pr_warn("ignored %s\n", opt_str);
26836 +                       fallthrough;
26837 +               case Opt_ignore_silent:
26838 +                       skipped = 1;
26839 +                       err = 0;
26840 +                       break;
26841 +               case Opt_err:
26842 +                       pr_err("unknown option %s\n", opt_str);
26843 +                       break;
26844 +               }
26845 +
26846 +               if (!err && !skipped) {
26847 +                       if (unlikely(++opt > opt_tail)) {
26848 +                               err = -E2BIG;
26849 +                               opt--;
26850 +                               opt->type = Opt_tail;
26851 +                               break;
26852 +                       }
26853 +                       opt->type = Opt_tail;
26854 +               }
26855 +       }
26856 +
26857 +       au_kfree_rcu(a);
26858 +       dump_opts(opts);
26859 +       if (unlikely(err))
26860 +               au_opts_free(opts);
26861 +
26862 +out:
26863 +       return err;
26864 +}
26865 +
26866 +static int au_opt_wbr_create(struct super_block *sb,
26867 +                            struct au_opt_wbr_create *create)
26868 +{
26869 +       int err;
26870 +       struct au_sbinfo *sbinfo;
26871 +
26872 +       SiMustWriteLock(sb);
26873 +
26874 +       err = 1; /* handled */
26875 +       sbinfo = au_sbi(sb);
26876 +       if (sbinfo->si_wbr_create_ops->fin) {
26877 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26878 +               if (!err)
26879 +                       err = 1;
26880 +       }
26881 +
26882 +       sbinfo->si_wbr_create = create->wbr_create;
26883 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26884 +       switch (create->wbr_create) {
26885 +       case AuWbrCreate_MFSRRV:
26886 +       case AuWbrCreate_MFSRR:
26887 +       case AuWbrCreate_TDMFS:
26888 +       case AuWbrCreate_TDMFSV:
26889 +       case AuWbrCreate_PMFSRR:
26890 +       case AuWbrCreate_PMFSRRV:
26891 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26892 +               fallthrough;
26893 +       case AuWbrCreate_MFS:
26894 +       case AuWbrCreate_MFSV:
26895 +       case AuWbrCreate_PMFS:
26896 +       case AuWbrCreate_PMFSV:
26897 +               sbinfo->si_wbr_mfs.mfs_expire
26898 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26899 +               break;
26900 +       }
26901 +
26902 +       if (sbinfo->si_wbr_create_ops->init)
26903 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26904 +
26905 +       return err;
26906 +}
26907 +
26908 +/*
26909 + * returns,
26910 + * plus: processed without an error
26911 + * zero: unprocessed
26912 + */
26913 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26914 +                        struct au_opts *opts)
26915 +{
26916 +       int err;
26917 +       struct au_sbinfo *sbinfo;
26918 +
26919 +       SiMustWriteLock(sb);
26920 +
26921 +       err = 1; /* handled */
26922 +       sbinfo = au_sbi(sb);
26923 +       switch (opt->type) {
26924 +       case Opt_udba:
26925 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26926 +               sbinfo->si_mntflags |= opt->udba;
26927 +               opts->given_udba |= opt->udba;
26928 +               break;
26929 +
26930 +       case Opt_plink:
26931 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26932 +               break;
26933 +       case Opt_noplink:
26934 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26935 +                       au_plink_put(sb, /*verbose*/1);
26936 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26937 +               break;
26938 +       case Opt_list_plink:
26939 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26940 +                       au_plink_list(sb);
26941 +               break;
26942 +
26943 +       case Opt_dio:
26944 +               au_opt_set(sbinfo->si_mntflags, DIO);
26945 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26946 +               break;
26947 +       case Opt_nodio:
26948 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26949 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26950 +               break;
26951 +
26952 +       case Opt_fhsm_sec:
26953 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26954 +               break;
26955 +
26956 +       case Opt_diropq_a:
26957 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26958 +               break;
26959 +       case Opt_diropq_w:
26960 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26961 +               break;
26962 +
26963 +       case Opt_warn_perm:
26964 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26965 +               break;
26966 +       case Opt_nowarn_perm:
26967 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26968 +               break;
26969 +
26970 +       case Opt_verbose:
26971 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
26972 +               break;
26973 +       case Opt_noverbose:
26974 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26975 +               break;
26976 +
26977 +       case Opt_sum:
26978 +               au_opt_set(sbinfo->si_mntflags, SUM);
26979 +               break;
26980 +       case Opt_wsum:
26981 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26982 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
26983 +               break;
26984 +       case Opt_nosum:
26985 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26986 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
26987 +               break;
26988 +
26989 +       case Opt_wbr_create:
26990 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
26991 +               break;
26992 +       case Opt_wbr_copyup:
26993 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
26994 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26995 +               break;
26996 +
26997 +       case Opt_dirwh:
26998 +               sbinfo->si_dirwh = opt->dirwh;
26999 +               break;
27000 +
27001 +       case Opt_rdcache:
27002 +               sbinfo->si_rdcache
27003 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27004 +               break;
27005 +       case Opt_rdblk:
27006 +               sbinfo->si_rdblk = opt->rdblk;
27007 +               break;
27008 +       case Opt_rdblk_def:
27009 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27010 +               break;
27011 +       case Opt_rdhash:
27012 +               sbinfo->si_rdhash = opt->rdhash;
27013 +               break;
27014 +       case Opt_rdhash_def:
27015 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27016 +               break;
27017 +
27018 +       case Opt_shwh:
27019 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27020 +               break;
27021 +       case Opt_noshwh:
27022 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27023 +               break;
27024 +
27025 +       case Opt_dirperm1:
27026 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27027 +               break;
27028 +       case Opt_nodirperm1:
27029 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27030 +               break;
27031 +
27032 +       case Opt_trunc_xino:
27033 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27034 +               break;
27035 +       case Opt_notrunc_xino:
27036 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27037 +               break;
27038 +
27039 +       case Opt_trunc_xino_path:
27040 +       case Opt_itrunc_xino:
27041 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27042 +                                   /*idx_begin*/0);
27043 +               if (!err)
27044 +                       err = 1;
27045 +               break;
27046 +
27047 +       case Opt_trunc_xib:
27048 +               au_fset_opts(opts->flags, TRUNC_XIB);
27049 +               break;
27050 +       case Opt_notrunc_xib:
27051 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27052 +               break;
27053 +
27054 +       case Opt_dirren:
27055 +               err = 1;
27056 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27057 +                       err = au_dr_opt_set(sb);
27058 +                       if (!err)
27059 +                               err = 1;
27060 +               }
27061 +               if (err == 1)
27062 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27063 +               break;
27064 +       case Opt_nodirren:
27065 +               err = 1;
27066 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27067 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27068 +                                                             DR_FLUSHED));
27069 +                       if (!err)
27070 +                               err = 1;
27071 +               }
27072 +               if (err == 1)
27073 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27074 +               break;
27075 +
27076 +       case Opt_acl:
27077 +               sb->s_flags |= SB_POSIXACL;
27078 +               break;
27079 +       case Opt_noacl:
27080 +               sb->s_flags &= ~SB_POSIXACL;
27081 +               break;
27082 +
27083 +       default:
27084 +               err = 0;
27085 +               break;
27086 +       }
27087 +
27088 +       return err;
27089 +}
27090 +
27091 +/*
27092 + * returns tri-state.
27093 + * plus: processed without an error
27094 + * zero: unprocessed
27095 + * minus: error
27096 + */
27097 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27098 +                    struct au_opts *opts)
27099 +{
27100 +       int err, do_refresh;
27101 +
27102 +       err = 0;
27103 +       switch (opt->type) {
27104 +       case Opt_append:
27105 +               opt->add.bindex = au_sbbot(sb) + 1;
27106 +               if (opt->add.bindex < 0)
27107 +                       opt->add.bindex = 0;
27108 +               goto add;
27109 +               /* Always goto add, not fallthrough */
27110 +       case Opt_prepend:
27111 +               opt->add.bindex = 0;
27112 +               fallthrough;
27113 +       add: /* indented label */
27114 +       case Opt_add:
27115 +               err = au_br_add(sb, &opt->add,
27116 +                               au_ftest_opts(opts->flags, REMOUNT));
27117 +               if (!err) {
27118 +                       err = 1;
27119 +                       au_fset_opts(opts->flags, REFRESH);
27120 +               }
27121 +               break;
27122 +
27123 +       case Opt_del:
27124 +       case Opt_idel:
27125 +               err = au_br_del(sb, &opt->del,
27126 +                               au_ftest_opts(opts->flags, REMOUNT));
27127 +               if (!err) {
27128 +                       err = 1;
27129 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27130 +                       au_fset_opts(opts->flags, REFRESH);
27131 +               }
27132 +               break;
27133 +
27134 +       case Opt_mod:
27135 +       case Opt_imod:
27136 +               err = au_br_mod(sb, &opt->mod,
27137 +                               au_ftest_opts(opts->flags, REMOUNT),
27138 +                               &do_refresh);
27139 +               if (!err) {
27140 +                       err = 1;
27141 +                       if (do_refresh)
27142 +                               au_fset_opts(opts->flags, REFRESH);
27143 +               }
27144 +               break;
27145 +       }
27146 +       return err;
27147 +}
27148 +
27149 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27150 +                      struct au_opt_xino **opt_xino,
27151 +                      struct au_opts *opts)
27152 +{
27153 +       int err;
27154 +
27155 +       err = 0;
27156 +       switch (opt->type) {
27157 +       case Opt_xino:
27158 +               err = au_xino_set(sb, &opt->xino,
27159 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27160 +               if (unlikely(err))
27161 +                       break;
27162 +
27163 +               *opt_xino = &opt->xino;
27164 +               break;
27165 +
27166 +       case Opt_noxino:
27167 +               au_xino_clr(sb);
27168 +               *opt_xino = (void *)-1;
27169 +               break;
27170 +       }
27171 +
27172 +       return err;
27173 +}
27174 +
27175 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27176 +                  unsigned int pending)
27177 +{
27178 +       int err, fhsm;
27179 +       aufs_bindex_t bindex, bbot;
27180 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27181 +       struct au_branch *br;
27182 +       struct au_wbr *wbr;
27183 +       struct dentry *root, *dentry;
27184 +       struct inode *dir, *h_dir;
27185 +       struct au_sbinfo *sbinfo;
27186 +       struct au_hinode *hdir;
27187 +
27188 +       SiMustAnyLock(sb);
27189 +
27190 +       sbinfo = au_sbi(sb);
27191 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27192 +
27193 +       if (!(sb_flags & SB_RDONLY)) {
27194 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27195 +                       pr_warn("first branch should be rw\n");
27196 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27197 +                       pr_warn_once("shwh should be used with ro\n");
27198 +       }
27199 +
27200 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27201 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27202 +               pr_warn_once("udba=*notify requires xino\n");
27203 +
27204 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27205 +               pr_warn_once("dirperm1 breaks the protection"
27206 +                            " by the permission bits on the lower branch\n");
27207 +
27208 +       err = 0;
27209 +       fhsm = 0;
27210 +       root = sb->s_root;
27211 +       dir = d_inode(root);
27212 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27213 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27214 +                                     UDBA_NONE);
27215 +       bbot = au_sbbot(sb);
27216 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27217 +               skip = 0;
27218 +               h_dir = au_h_iptr(dir, bindex);
27219 +               br = au_sbr(sb, bindex);
27220 +
27221 +               if ((br->br_perm & AuBrAttr_ICEX)
27222 +                   && !h_dir->i_op->listxattr)
27223 +                       br->br_perm &= ~AuBrAttr_ICEX;
27224 +#if 0 /* untested */
27225 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27226 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27227 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27228 +#endif
27229 +
27230 +               do_free = 0;
27231 +               wbr = br->br_wbr;
27232 +               if (wbr)
27233 +                       wbr_wh_read_lock(wbr);
27234 +
27235 +               if (!au_br_writable(br->br_perm)) {
27236 +                       do_free = !!wbr;
27237 +                       skip = (!wbr
27238 +                               || (!wbr->wbr_whbase
27239 +                                   && !wbr->wbr_plink
27240 +                                   && !wbr->wbr_orph));
27241 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27242 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27243 +                       skip = (!wbr || !wbr->wbr_whbase);
27244 +                       if (skip && wbr) {
27245 +                               if (do_plink)
27246 +                                       skip = !!wbr->wbr_plink;
27247 +                               else
27248 +                                       skip = !wbr->wbr_plink;
27249 +                       }
27250 +               } else {
27251 +                       /* skip = (br->br_whbase && br->br_ohph); */
27252 +                       skip = (wbr && wbr->wbr_whbase);
27253 +                       if (skip) {
27254 +                               if (do_plink)
27255 +                                       skip = !!wbr->wbr_plink;
27256 +                               else
27257 +                                       skip = !wbr->wbr_plink;
27258 +                       }
27259 +               }
27260 +               if (wbr)
27261 +                       wbr_wh_read_unlock(wbr);
27262 +
27263 +               if (can_no_dreval) {
27264 +                       dentry = br->br_path.dentry;
27265 +                       spin_lock(&dentry->d_lock);
27266 +                       if (dentry->d_flags &
27267 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27268 +                               can_no_dreval = 0;
27269 +                       spin_unlock(&dentry->d_lock);
27270 +               }
27271 +
27272 +               if (au_br_fhsm(br->br_perm)) {
27273 +                       fhsm++;
27274 +                       AuDebugOn(!br->br_fhsm);
27275 +               }
27276 +
27277 +               if (skip)
27278 +                       continue;
27279 +
27280 +               hdir = au_hi(dir, bindex);
27281 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27282 +               if (wbr)
27283 +                       wbr_wh_write_lock(wbr);
27284 +               err = au_wh_init(br, sb);
27285 +               if (wbr)
27286 +                       wbr_wh_write_unlock(wbr);
27287 +               au_hn_inode_unlock(hdir);
27288 +
27289 +               if (!err && do_free) {
27290 +                       au_kfree_rcu(wbr);
27291 +                       br->br_wbr = NULL;
27292 +               }
27293 +       }
27294 +
27295 +       if (can_no_dreval)
27296 +               au_fset_si(sbinfo, NO_DREVAL);
27297 +       else
27298 +               au_fclr_si(sbinfo, NO_DREVAL);
27299 +
27300 +       if (fhsm >= 2) {
27301 +               au_fset_si(sbinfo, FHSM);
27302 +               for (bindex = bbot; bindex >= 0; bindex--) {
27303 +                       br = au_sbr(sb, bindex);
27304 +                       if (au_br_fhsm(br->br_perm)) {
27305 +                               au_fhsm_set_bottom(sb, bindex);
27306 +                               break;
27307 +                       }
27308 +               }
27309 +       } else {
27310 +               au_fclr_si(sbinfo, FHSM);
27311 +               au_fhsm_set_bottom(sb, -1);
27312 +       }
27313 +
27314 +       return err;
27315 +}
27316 +
27317 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27318 +{
27319 +       int err;
27320 +       unsigned int tmp;
27321 +       aufs_bindex_t bindex, bbot;
27322 +       struct au_opt *opt;
27323 +       struct au_opt_xino *opt_xino, xino;
27324 +       struct au_sbinfo *sbinfo;
27325 +       struct au_branch *br;
27326 +       struct inode *dir;
27327 +
27328 +       SiMustWriteLock(sb);
27329 +
27330 +       err = 0;
27331 +       opt_xino = NULL;
27332 +       opt = opts->opt;
27333 +       while (err >= 0 && opt->type != Opt_tail)
27334 +               err = au_opt_simple(sb, opt++, opts);
27335 +       if (err > 0)
27336 +               err = 0;
27337 +       else if (unlikely(err < 0))
27338 +               goto out;
27339 +
27340 +       /* disable xino and udba temporary */
27341 +       sbinfo = au_sbi(sb);
27342 +       tmp = sbinfo->si_mntflags;
27343 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27344 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27345 +
27346 +       opt = opts->opt;
27347 +       while (err >= 0 && opt->type != Opt_tail)
27348 +               err = au_opt_br(sb, opt++, opts);
27349 +       if (err > 0)
27350 +               err = 0;
27351 +       else if (unlikely(err < 0))
27352 +               goto out;
27353 +
27354 +       bbot = au_sbbot(sb);
27355 +       if (unlikely(bbot < 0)) {
27356 +               err = -EINVAL;
27357 +               pr_err("no branches\n");
27358 +               goto out;
27359 +       }
27360 +
27361 +       if (au_opt_test(tmp, XINO))
27362 +               au_opt_set(sbinfo->si_mntflags, XINO);
27363 +       opt = opts->opt;
27364 +       while (!err && opt->type != Opt_tail)
27365 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27366 +       if (unlikely(err))
27367 +               goto out;
27368 +
27369 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27370 +       if (unlikely(err))
27371 +               goto out;
27372 +
27373 +       /* restore xino */
27374 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27375 +               xino.file = au_xino_def(sb);
27376 +               err = PTR_ERR(xino.file);
27377 +               if (IS_ERR(xino.file))
27378 +                       goto out;
27379 +
27380 +               err = au_xino_set(sb, &xino, /*remount*/0);
27381 +               fput(xino.file);
27382 +               if (unlikely(err))
27383 +                       goto out;
27384 +       }
27385 +
27386 +       /* restore udba */
27387 +       tmp &= AuOptMask_UDBA;
27388 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27389 +       sbinfo->si_mntflags |= tmp;
27390 +       bbot = au_sbbot(sb);
27391 +       for (bindex = 0; bindex <= bbot; bindex++) {
27392 +               br = au_sbr(sb, bindex);
27393 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27394 +               if (unlikely(err))
27395 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27396 +                               bindex, err);
27397 +               /* go on even if err */
27398 +       }
27399 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27400 +               dir = d_inode(sb->s_root);
27401 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27402 +       }
27403 +
27404 +out:
27405 +       return err;
27406 +}
27407 +
27408 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27409 +{
27410 +       int err, rerr;
27411 +       unsigned char no_dreval;
27412 +       struct inode *dir;
27413 +       struct au_opt_xino *opt_xino;
27414 +       struct au_opt *opt;
27415 +       struct au_sbinfo *sbinfo;
27416 +
27417 +       SiMustWriteLock(sb);
27418 +
27419 +       err = au_dr_opt_flush(sb);
27420 +       if (unlikely(err))
27421 +               goto out;
27422 +       au_fset_opts(opts->flags, DR_FLUSHED);
27423 +
27424 +       dir = d_inode(sb->s_root);
27425 +       sbinfo = au_sbi(sb);
27426 +       opt_xino = NULL;
27427 +       opt = opts->opt;
27428 +       while (err >= 0 && opt->type != Opt_tail) {
27429 +               err = au_opt_simple(sb, opt, opts);
27430 +               if (!err)
27431 +                       err = au_opt_br(sb, opt, opts);
27432 +               if (!err)
27433 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27434 +               opt++;
27435 +       }
27436 +       if (err > 0)
27437 +               err = 0;
27438 +       AuTraceErr(err);
27439 +       /* go on even err */
27440 +
27441 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27442 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27443 +       if (unlikely(rerr && !err))
27444 +               err = rerr;
27445 +
27446 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27447 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27448 +
27449 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27450 +               rerr = au_xib_trunc(sb);
27451 +               if (unlikely(rerr && !err))
27452 +                       err = rerr;
27453 +       }
27454 +
27455 +       /* will be handled by the caller */
27456 +       if (!au_ftest_opts(opts->flags, REFRESH)
27457 +           && (opts->given_udba
27458 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27459 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27460 +                   ))
27461 +               au_fset_opts(opts->flags, REFRESH);
27462 +
27463 +       AuDbg("status 0x%x\n", opts->flags);
27464 +
27465 +out:
27466 +       return err;
27467 +}
27468 +
27469 +/* ---------------------------------------------------------------------- */
27470 +
27471 +unsigned int au_opt_udba(struct super_block *sb)
27472 +{
27473 +       return au_mntflags(sb) & AuOptMask_UDBA;
27474 +}
27475 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27476 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27477 +++ linux/fs/aufs/opts.h        2021-11-01 23:48:34.209692595 +0100
27478 @@ -0,0 +1,225 @@
27479 +/* SPDX-License-Identifier: GPL-2.0 */
27480 +/*
27481 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27482 + *
27483 + * This program, aufs is free software; you can redistribute it and/or modify
27484 + * it under the terms of the GNU General Public License as published by
27485 + * the Free Software Foundation; either version 2 of the License, or
27486 + * (at your option) any later version.
27487 + *
27488 + * This program is distributed in the hope that it will be useful,
27489 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27490 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27491 + * GNU General Public License for more details.
27492 + *
27493 + * You should have received a copy of the GNU General Public License
27494 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27495 + */
27496 +
27497 +/*
27498 + * mount options/flags
27499 + */
27500 +
27501 +#ifndef __AUFS_OPTS_H__
27502 +#define __AUFS_OPTS_H__
27503 +
27504 +#ifdef __KERNEL__
27505 +
27506 +#include <linux/path.h>
27507 +
27508 +struct file;
27509 +
27510 +/* ---------------------------------------------------------------------- */
27511 +
27512 +/* mount flags */
27513 +#define AuOpt_XINO             1               /* external inode number bitmap
27514 +                                                  and translation table */
27515 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27516 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27517 +#define AuOpt_UDBA_REVAL       (1 << 3)
27518 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27519 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27520 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27521 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27522 +                                                  bits */
27523 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27524 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27525 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27526 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27527 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27528 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27529 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27530 +
27531 +#ifndef CONFIG_AUFS_HNOTIFY
27532 +#undef AuOpt_UDBA_HNOTIFY
27533 +#define AuOpt_UDBA_HNOTIFY     0
27534 +#endif
27535 +#ifndef CONFIG_AUFS_DIRREN
27536 +#undef AuOpt_DIRREN
27537 +#define AuOpt_DIRREN           0
27538 +#endif
27539 +#ifndef CONFIG_AUFS_SHWH
27540 +#undef AuOpt_SHWH
27541 +#define AuOpt_SHWH             0
27542 +#endif
27543 +
27544 +#define AuOpt_Def      (AuOpt_XINO \
27545 +                        | AuOpt_UDBA_REVAL \
27546 +                        | AuOpt_PLINK \
27547 +                        /* | AuOpt_DIRPERM1 */ \
27548 +                        | AuOpt_WARN_PERM)
27549 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27550 +                        | AuOpt_UDBA_REVAL \
27551 +                        | AuOpt_UDBA_HNOTIFY)
27552 +
27553 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27554 +#define au_opt_set(flags, name) do { \
27555 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27556 +       ((flags) |= AuOpt_##name); \
27557 +} while (0)
27558 +#define au_opt_set_udba(flags, name) do { \
27559 +       (flags) &= ~AuOptMask_UDBA; \
27560 +       ((flags) |= AuOpt_##name); \
27561 +} while (0)
27562 +#define au_opt_clr(flags, name) do { \
27563 +       ((flags) &= ~AuOpt_##name); \
27564 +} while (0)
27565 +
27566 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27567 +{
27568 +#ifdef CONFIG_PROC_FS
27569 +       return mntflags;
27570 +#else
27571 +       return mntflags & ~AuOpt_PLINK;
27572 +#endif
27573 +}
27574 +
27575 +/* ---------------------------------------------------------------------- */
27576 +
27577 +/* policies to select one among multiple writable branches */
27578 +enum {
27579 +       AuWbrCreate_TDP,        /* top down parent */
27580 +       AuWbrCreate_RR,         /* round robin */
27581 +       AuWbrCreate_MFS,        /* most free space */
27582 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27583 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27584 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27585 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27586 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27587 +       AuWbrCreate_PMFS,       /* parent and mfs */
27588 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27589 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27590 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27591 +
27592 +       AuWbrCreate_Def = AuWbrCreate_TDP
27593 +};
27594 +
27595 +enum {
27596 +       AuWbrCopyup_TDP,        /* top down parent */
27597 +       AuWbrCopyup_BUP,        /* bottom up parent */
27598 +       AuWbrCopyup_BU,         /* bottom up */
27599 +
27600 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27601 +};
27602 +
27603 +/* ---------------------------------------------------------------------- */
27604 +
27605 +struct au_opt_add {
27606 +       aufs_bindex_t   bindex;
27607 +       char            *pathname;
27608 +       int             perm;
27609 +       struct path     path;
27610 +};
27611 +
27612 +struct au_opt_del {
27613 +       char            *pathname;
27614 +       struct path     h_path;
27615 +};
27616 +
27617 +struct au_opt_mod {
27618 +       char            *path;
27619 +       int             perm;
27620 +       struct dentry   *h_root;
27621 +};
27622 +
27623 +struct au_opt_xino {
27624 +       char            *path;
27625 +       struct file     *file;
27626 +};
27627 +
27628 +struct au_opt_xino_itrunc {
27629 +       aufs_bindex_t   bindex;
27630 +};
27631 +
27632 +struct au_opt_wbr_create {
27633 +       int                     wbr_create;
27634 +       int                     mfs_second;
27635 +       unsigned long long      mfsrr_watermark;
27636 +};
27637 +
27638 +struct au_opt {
27639 +       int type;
27640 +       union {
27641 +               struct au_opt_xino      xino;
27642 +               struct au_opt_xino_itrunc xino_itrunc;
27643 +               struct au_opt_add       add;
27644 +               struct au_opt_del       del;
27645 +               struct au_opt_mod       mod;
27646 +               int                     dirwh;
27647 +               int                     rdcache;
27648 +               unsigned int            rdblk;
27649 +               unsigned int            rdhash;
27650 +               int                     udba;
27651 +               struct au_opt_wbr_create wbr_create;
27652 +               int                     wbr_copyup;
27653 +               unsigned int            fhsm_second;
27654 +       };
27655 +};
27656 +
27657 +/* opts flags */
27658 +#define AuOpts_REMOUNT         1
27659 +#define AuOpts_REFRESH         (1 << 1)
27660 +#define AuOpts_TRUNC_XIB       (1 << 2)
27661 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27662 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27663 +#define AuOpts_DR_FLUSHED      (1 << 5)
27664 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27665 +#define au_fset_opts(flags, name) \
27666 +       do { (flags) |= AuOpts_##name; } while (0)
27667 +#define au_fclr_opts(flags, name) \
27668 +       do { (flags) &= ~AuOpts_##name; } while (0)
27669 +
27670 +#ifndef CONFIG_AUFS_DIRREN
27671 +#undef AuOpts_DR_FLUSHED
27672 +#define AuOpts_DR_FLUSHED      0
27673 +#endif
27674 +
27675 +struct au_opts {
27676 +       struct au_opt   *opt;
27677 +       int             max_opt;
27678 +
27679 +       unsigned int    given_udba;
27680 +       unsigned int    flags;
27681 +       unsigned long   sb_flags;
27682 +};
27683 +
27684 +/* ---------------------------------------------------------------------- */
27685 +
27686 +/* opts.c */
27687 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27688 +const char *au_optstr_udba(int udba);
27689 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27690 +const char *au_optstr_wbr_create(int wbr_create);
27691 +
27692 +void au_opts_free(struct au_opts *opts);
27693 +struct super_block;
27694 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27695 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27696 +                  unsigned int pending);
27697 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27698 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27699 +
27700 +unsigned int au_opt_udba(struct super_block *sb);
27701 +
27702 +#endif /* __KERNEL__ */
27703 +#endif /* __AUFS_OPTS_H__ */
27704 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27705 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27706 +++ linux/fs/aufs/plink.c       2021-11-01 23:48:34.209692595 +0100
27707 @@ -0,0 +1,516 @@
27708 +// SPDX-License-Identifier: GPL-2.0
27709 +/*
27710 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27711 + *
27712 + * This program, aufs is free software; you can redistribute it and/or modify
27713 + * it under the terms of the GNU General Public License as published by
27714 + * the Free Software Foundation; either version 2 of the License, or
27715 + * (at your option) any later version.
27716 + *
27717 + * This program is distributed in the hope that it will be useful,
27718 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27719 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27720 + * GNU General Public License for more details.
27721 + *
27722 + * You should have received a copy of the GNU General Public License
27723 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27724 + */
27725 +
27726 +/*
27727 + * pseudo-link
27728 + */
27729 +
27730 +#include "aufs.h"
27731 +
27732 +/*
27733 + * the pseudo-link maintenance mode.
27734 + * during a user process maintains the pseudo-links,
27735 + * prohibit adding a new plink and branch manipulation.
27736 + *
27737 + * Flags
27738 + * NOPLM:
27739 + *     For entry functions which will handle plink, and i_mutex is already held
27740 + *     in VFS.
27741 + *     They cannot wait and should return an error at once.
27742 + *     Callers has to check the error.
27743 + * NOPLMW:
27744 + *     For entry functions which will handle plink, but i_mutex is not held
27745 + *     in VFS.
27746 + *     They can wait the plink maintenance mode to finish.
27747 + *
27748 + * They behave like F_SETLK and F_SETLKW.
27749 + * If the caller never handle plink, then both flags are unnecessary.
27750 + */
27751 +
27752 +int au_plink_maint(struct super_block *sb, int flags)
27753 +{
27754 +       int err;
27755 +       pid_t pid, ppid;
27756 +       struct task_struct *parent, *prev;
27757 +       struct au_sbinfo *sbi;
27758 +
27759 +       SiMustAnyLock(sb);
27760 +
27761 +       err = 0;
27762 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27763 +               goto out;
27764 +
27765 +       sbi = au_sbi(sb);
27766 +       pid = sbi->si_plink_maint_pid;
27767 +       if (!pid || pid == current->pid)
27768 +               goto out;
27769 +
27770 +       /* todo: it highly depends upon /sbin/mount.aufs */
27771 +       prev = NULL;
27772 +       parent = current;
27773 +       ppid = 0;
27774 +       rcu_read_lock();
27775 +       while (1) {
27776 +               parent = rcu_dereference(parent->real_parent);
27777 +               if (parent == prev)
27778 +                       break;
27779 +               ppid = task_pid_vnr(parent);
27780 +               if (pid == ppid) {
27781 +                       rcu_read_unlock();
27782 +                       goto out;
27783 +               }
27784 +               prev = parent;
27785 +       }
27786 +       rcu_read_unlock();
27787 +
27788 +       if (au_ftest_lock(flags, NOPLMW)) {
27789 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27790 +               /* AuDebugOn(!lockdep_depth(current)); */
27791 +               while (sbi->si_plink_maint_pid) {
27792 +                       si_read_unlock(sb);
27793 +                       /* gave up wake_up_bit() */
27794 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27795 +
27796 +                       if (au_ftest_lock(flags, FLUSH))
27797 +                               au_nwt_flush(&sbi->si_nowait);
27798 +                       si_noflush_read_lock(sb);
27799 +               }
27800 +       } else if (au_ftest_lock(flags, NOPLM)) {
27801 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27802 +               err = -EAGAIN;
27803 +       }
27804 +
27805 +out:
27806 +       return err;
27807 +}
27808 +
27809 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27810 +{
27811 +       spin_lock(&sbinfo->si_plink_maint_lock);
27812 +       sbinfo->si_plink_maint_pid = 0;
27813 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27814 +       wake_up_all(&sbinfo->si_plink_wq);
27815 +}
27816 +
27817 +int au_plink_maint_enter(struct super_block *sb)
27818 +{
27819 +       int err;
27820 +       struct au_sbinfo *sbinfo;
27821 +
27822 +       err = 0;
27823 +       sbinfo = au_sbi(sb);
27824 +       /* make sure i am the only one in this fs */
27825 +       si_write_lock(sb, AuLock_FLUSH);
27826 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27827 +               spin_lock(&sbinfo->si_plink_maint_lock);
27828 +               if (!sbinfo->si_plink_maint_pid)
27829 +                       sbinfo->si_plink_maint_pid = current->pid;
27830 +               else
27831 +                       err = -EBUSY;
27832 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27833 +       }
27834 +       si_write_unlock(sb);
27835 +
27836 +       return err;
27837 +}
27838 +
27839 +/* ---------------------------------------------------------------------- */
27840 +
27841 +#ifdef CONFIG_AUFS_DEBUG
27842 +void au_plink_list(struct super_block *sb)
27843 +{
27844 +       int i;
27845 +       struct au_sbinfo *sbinfo;
27846 +       struct hlist_bl_head *hbl;
27847 +       struct hlist_bl_node *pos;
27848 +       struct au_icntnr *icntnr;
27849 +
27850 +       SiMustAnyLock(sb);
27851 +
27852 +       sbinfo = au_sbi(sb);
27853 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27854 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27855 +
27856 +       for (i = 0; i < AuPlink_NHASH; i++) {
27857 +               hbl = sbinfo->si_plink + i;
27858 +               hlist_bl_lock(hbl);
27859 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27860 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27861 +               hlist_bl_unlock(hbl);
27862 +       }
27863 +}
27864 +#endif
27865 +
27866 +/* is the inode pseudo-linked? */
27867 +int au_plink_test(struct inode *inode)
27868 +{
27869 +       int found, i;
27870 +       struct au_sbinfo *sbinfo;
27871 +       struct hlist_bl_head *hbl;
27872 +       struct hlist_bl_node *pos;
27873 +       struct au_icntnr *icntnr;
27874 +
27875 +       sbinfo = au_sbi(inode->i_sb);
27876 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27877 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27878 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27879 +
27880 +       found = 0;
27881 +       i = au_plink_hash(inode->i_ino);
27882 +       hbl =  sbinfo->si_plink + i;
27883 +       hlist_bl_lock(hbl);
27884 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27885 +               if (&icntnr->vfs_inode == inode) {
27886 +                       found = 1;
27887 +                       break;
27888 +               }
27889 +       hlist_bl_unlock(hbl);
27890 +       return found;
27891 +}
27892 +
27893 +/* ---------------------------------------------------------------------- */
27894 +
27895 +/*
27896 + * generate a name for plink.
27897 + * the file will be stored under AUFS_WH_PLINKDIR.
27898 + */
27899 +/* 20 is max digits length of ulong 64 */
27900 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27901 +
27902 +static int plink_name(char *name, int len, struct inode *inode,
27903 +                     aufs_bindex_t bindex)
27904 +{
27905 +       int rlen;
27906 +       struct inode *h_inode;
27907 +
27908 +       h_inode = au_h_iptr(inode, bindex);
27909 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27910 +       return rlen;
27911 +}
27912 +
27913 +struct au_do_plink_lkup_args {
27914 +       struct dentry **errp;
27915 +       struct qstr *tgtname;
27916 +       struct path *h_ppath;
27917 +};
27918 +
27919 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27920 +                                      struct path *h_ppath)
27921 +{
27922 +       struct dentry *h_dentry;
27923 +       struct inode *h_inode;
27924 +
27925 +       h_inode = d_inode(h_ppath->dentry);
27926 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27927 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
27928 +       inode_unlock_shared(h_inode);
27929 +
27930 +       return h_dentry;
27931 +}
27932 +
27933 +static void au_call_do_plink_lkup(void *args)
27934 +{
27935 +       struct au_do_plink_lkup_args *a = args;
27936 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
27937 +}
27938 +
27939 +/* lookup the plink-ed @inode under the branch at @bindex */
27940 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27941 +{
27942 +       struct dentry *h_dentry;
27943 +       struct au_branch *br;
27944 +       struct path h_ppath;
27945 +       int wkq_err;
27946 +       char a[PLINK_NAME_LEN];
27947 +       struct qstr tgtname = QSTR_INIT(a, 0);
27948 +
27949 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27950 +
27951 +       br = au_sbr(inode->i_sb, bindex);
27952 +       h_ppath.dentry = br->br_wbr->wbr_plink;
27953 +       h_ppath.mnt = au_br_mnt(br);
27954 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27955 +
27956 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27957 +               struct au_do_plink_lkup_args args = {
27958 +                       .errp           = &h_dentry,
27959 +                       .tgtname        = &tgtname,
27960 +                       .h_ppath        = &h_ppath
27961 +               };
27962 +
27963 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27964 +               if (unlikely(wkq_err))
27965 +                       h_dentry = ERR_PTR(wkq_err);
27966 +       } else
27967 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
27968 +
27969 +       return h_dentry;
27970 +}
27971 +
27972 +/* create a pseudo-link */
27973 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
27974 +                     struct dentry *h_dentry)
27975 +{
27976 +       int err;
27977 +       struct path h_path;
27978 +       struct inode *h_dir, *delegated;
27979 +
27980 +       h_dir = d_inode(h_ppath->dentry);
27981 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27982 +       h_path.mnt = h_ppath->mnt;
27983 +again:
27984 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
27985 +       err = PTR_ERR(h_path.dentry);
27986 +       if (IS_ERR(h_path.dentry))
27987 +               goto out;
27988 +
27989 +       err = 0;
27990 +       /* wh.plink dir is not monitored */
27991 +       /* todo: is it really safe? */
27992 +       if (d_is_positive(h_path.dentry)
27993 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27994 +               delegated = NULL;
27995 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27996 +               if (unlikely(err == -EWOULDBLOCK)) {
27997 +                       pr_warn("cannot retry for NFSv4 delegation"
27998 +                               " for an internal unlink\n");
27999 +                       iput(delegated);
28000 +               }
28001 +               dput(h_path.dentry);
28002 +               h_path.dentry = NULL;
28003 +               if (!err)
28004 +                       goto again;
28005 +       }
28006 +       if (!err && d_is_negative(h_path.dentry)) {
28007 +               delegated = NULL;
28008 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28009 +               if (unlikely(err == -EWOULDBLOCK)) {
28010 +                       pr_warn("cannot retry for NFSv4 delegation"
28011 +                               " for an internal link\n");
28012 +                       iput(delegated);
28013 +               }
28014 +       }
28015 +       dput(h_path.dentry);
28016 +
28017 +out:
28018 +       inode_unlock(h_dir);
28019 +       return err;
28020 +}
28021 +
28022 +struct do_whplink_args {
28023 +       int *errp;
28024 +       struct qstr *tgt;
28025 +       struct path *h_ppath;
28026 +       struct dentry *h_dentry;
28027 +};
28028 +
28029 +static void call_do_whplink(void *args)
28030 +{
28031 +       struct do_whplink_args *a = args;
28032 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28033 +}
28034 +
28035 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28036 +                  aufs_bindex_t bindex)
28037 +{
28038 +       int err, wkq_err;
28039 +       struct au_branch *br;
28040 +       struct au_wbr *wbr;
28041 +       struct path h_ppath;
28042 +       char a[PLINK_NAME_LEN];
28043 +       struct qstr tgtname = QSTR_INIT(a, 0);
28044 +
28045 +       br = au_sbr(inode->i_sb, bindex);
28046 +       wbr = br->br_wbr;
28047 +       h_ppath.dentry = wbr->wbr_plink;
28048 +       h_ppath.mnt = au_br_mnt(br);
28049 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28050 +
28051 +       /* always superio. */
28052 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28053 +               struct do_whplink_args args = {
28054 +                       .errp           = &err,
28055 +                       .tgt            = &tgtname,
28056 +                       .h_ppath        = &h_ppath,
28057 +                       .h_dentry       = h_dentry
28058 +               };
28059 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28060 +               if (unlikely(wkq_err))
28061 +                       err = wkq_err;
28062 +       } else
28063 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28064 +
28065 +       return err;
28066 +}
28067 +
28068 +/*
28069 + * create a new pseudo-link for @h_dentry on @bindex.
28070 + * the linked inode is held in aufs @inode.
28071 + */
28072 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28073 +                    struct dentry *h_dentry)
28074 +{
28075 +       struct super_block *sb;
28076 +       struct au_sbinfo *sbinfo;
28077 +       struct hlist_bl_head *hbl;
28078 +       struct hlist_bl_node *pos;
28079 +       struct au_icntnr *icntnr;
28080 +       int found, err, cnt, i;
28081 +
28082 +       sb = inode->i_sb;
28083 +       sbinfo = au_sbi(sb);
28084 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28085 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28086 +
28087 +       found = au_plink_test(inode);
28088 +       if (found)
28089 +               return;
28090 +
28091 +       i = au_plink_hash(inode->i_ino);
28092 +       hbl = sbinfo->si_plink + i;
28093 +       au_igrab(inode);
28094 +
28095 +       hlist_bl_lock(hbl);
28096 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28097 +               if (&icntnr->vfs_inode == inode) {
28098 +                       found = 1;
28099 +                       break;
28100 +               }
28101 +       }
28102 +       if (!found) {
28103 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28104 +               hlist_bl_add_head(&icntnr->plink, hbl);
28105 +       }
28106 +       hlist_bl_unlock(hbl);
28107 +       if (!found) {
28108 +               cnt = au_hbl_count(hbl);
28109 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28110 +               if (cnt > AUFS_PLINK_WARN)
28111 +                       AuWarn1(msg ", %d\n", cnt);
28112 +#undef msg
28113 +               err = whplink(h_dentry, inode, bindex);
28114 +               if (unlikely(err)) {
28115 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28116 +                       au_hbl_del(&icntnr->plink, hbl);
28117 +                       iput(&icntnr->vfs_inode);
28118 +               }
28119 +       } else
28120 +               iput(&icntnr->vfs_inode);
28121 +}
28122 +
28123 +/* free all plinks */
28124 +void au_plink_put(struct super_block *sb, int verbose)
28125 +{
28126 +       int i, warned;
28127 +       struct au_sbinfo *sbinfo;
28128 +       struct hlist_bl_head *hbl;
28129 +       struct hlist_bl_node *pos, *tmp;
28130 +       struct au_icntnr *icntnr;
28131 +
28132 +       SiMustWriteLock(sb);
28133 +
28134 +       sbinfo = au_sbi(sb);
28135 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28136 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28137 +
28138 +       /* no spin_lock since sbinfo is write-locked */
28139 +       warned = 0;
28140 +       for (i = 0; i < AuPlink_NHASH; i++) {
28141 +               hbl = sbinfo->si_plink + i;
28142 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28143 +                       pr_warn("pseudo-link is not flushed");
28144 +                       warned = 1;
28145 +               }
28146 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28147 +                       iput(&icntnr->vfs_inode);
28148 +               INIT_HLIST_BL_HEAD(hbl);
28149 +       }
28150 +}
28151 +
28152 +void au_plink_clean(struct super_block *sb, int verbose)
28153 +{
28154 +       struct dentry *root;
28155 +
28156 +       root = sb->s_root;
28157 +       aufs_write_lock(root);
28158 +       if (au_opt_test(au_mntflags(sb), PLINK))
28159 +               au_plink_put(sb, verbose);
28160 +       aufs_write_unlock(root);
28161 +}
28162 +
28163 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28164 +{
28165 +       int do_put;
28166 +       aufs_bindex_t btop, bbot, bindex;
28167 +
28168 +       do_put = 0;
28169 +       btop = au_ibtop(inode);
28170 +       bbot = au_ibbot(inode);
28171 +       if (btop >= 0) {
28172 +               for (bindex = btop; bindex <= bbot; bindex++) {
28173 +                       if (!au_h_iptr(inode, bindex)
28174 +                           || au_ii_br_id(inode, bindex) != br_id)
28175 +                               continue;
28176 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28177 +                       do_put = 1;
28178 +                       break;
28179 +               }
28180 +               if (do_put)
28181 +                       for (bindex = btop; bindex <= bbot; bindex++)
28182 +                               if (au_h_iptr(inode, bindex)) {
28183 +                                       do_put = 0;
28184 +                                       break;
28185 +                               }
28186 +       } else
28187 +               do_put = 1;
28188 +
28189 +       return do_put;
28190 +}
28191 +
28192 +/* free the plinks on a branch specified by @br_id */
28193 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28194 +{
28195 +       struct au_sbinfo *sbinfo;
28196 +       struct hlist_bl_head *hbl;
28197 +       struct hlist_bl_node *pos, *tmp;
28198 +       struct au_icntnr *icntnr;
28199 +       struct inode *inode;
28200 +       int i, do_put;
28201 +
28202 +       SiMustWriteLock(sb);
28203 +
28204 +       sbinfo = au_sbi(sb);
28205 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28206 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28207 +
28208 +       /* no bit_lock since sbinfo is write-locked */
28209 +       for (i = 0; i < AuPlink_NHASH; i++) {
28210 +               hbl = sbinfo->si_plink + i;
28211 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28212 +                       inode = au_igrab(&icntnr->vfs_inode);
28213 +                       ii_write_lock_child(inode);
28214 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28215 +                       if (do_put) {
28216 +                               hlist_bl_del(&icntnr->plink);
28217 +                               iput(inode);
28218 +                       }
28219 +                       ii_write_unlock(inode);
28220 +                       iput(inode);
28221 +               }
28222 +       }
28223 +}
28224 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28225 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28226 +++ linux/fs/aufs/poll.c        2021-11-01 23:48:34.209692595 +0100
28227 @@ -0,0 +1,51 @@
28228 +// SPDX-License-Identifier: GPL-2.0
28229 +/*
28230 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28231 + *
28232 + * This program, aufs is free software; you can redistribute it and/or modify
28233 + * it under the terms of the GNU General Public License as published by
28234 + * the Free Software Foundation; either version 2 of the License, or
28235 + * (at your option) any later version.
28236 + *
28237 + * This program is distributed in the hope that it will be useful,
28238 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28239 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28240 + * GNU General Public License for more details.
28241 + *
28242 + * You should have received a copy of the GNU General Public License
28243 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28244 + */
28245 +
28246 +/*
28247 + * poll operation
28248 + * There is only one filesystem which implements ->poll operation, currently.
28249 + */
28250 +
28251 +#include "aufs.h"
28252 +
28253 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28254 +{
28255 +       __poll_t mask;
28256 +       struct file *h_file;
28257 +       struct super_block *sb;
28258 +
28259 +       /* We should pretend an error happened. */
28260 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28261 +       sb = file->f_path.dentry->d_sb;
28262 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28263 +
28264 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28265 +       if (IS_ERR(h_file)) {
28266 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28267 +               goto out;
28268 +       }
28269 +
28270 +       mask = vfs_poll(h_file, pt);
28271 +       fput(h_file); /* instead of au_read_post() */
28272 +
28273 +out:
28274 +       si_read_unlock(sb);
28275 +       if (mask & EPOLLERR)
28276 +               AuDbg("mask 0x%x\n", mask);
28277 +       return mask;
28278 +}
28279 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28280 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28281 +++ linux/fs/aufs/posix_acl.c   2021-11-01 23:48:34.209692595 +0100
28282 @@ -0,0 +1,111 @@
28283 +// SPDX-License-Identifier: GPL-2.0
28284 +/*
28285 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28286 + *
28287 + * This program, aufs is free software; you can redistribute it and/or modify
28288 + * it under the terms of the GNU General Public License as published by
28289 + * the Free Software Foundation; either version 2 of the License, or
28290 + * (at your option) any later version.
28291 + *
28292 + * This program is distributed in the hope that it will be useful,
28293 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28294 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28295 + * GNU General Public License for more details.
28296 + *
28297 + * You should have received a copy of the GNU General Public License
28298 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28299 + */
28300 +
28301 +/*
28302 + * posix acl operations
28303 + */
28304 +
28305 +#include <linux/fs.h>
28306 +#include "aufs.h"
28307 +
28308 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28309 +{
28310 +       struct posix_acl *acl;
28311 +       int err;
28312 +       aufs_bindex_t bindex;
28313 +       struct inode *h_inode;
28314 +       struct super_block *sb;
28315 +
28316 +       acl = ERR_PTR(-ECHILD);
28317 +       if (rcu)
28318 +               goto out;
28319 +
28320 +       acl = NULL;
28321 +       sb = inode->i_sb;
28322 +       si_read_lock(sb, AuLock_FLUSH);
28323 +       ii_read_lock_child(inode);
28324 +       if (!(sb->s_flags & SB_POSIXACL))
28325 +               goto unlock;
28326 +
28327 +       bindex = au_ibtop(inode);
28328 +       h_inode = au_h_iptr(inode, bindex);
28329 +       if (unlikely(!h_inode
28330 +                    || ((h_inode->i_mode & S_IFMT)
28331 +                        != (inode->i_mode & S_IFMT)))) {
28332 +               err = au_busy_or_stale();
28333 +               acl = ERR_PTR(err);
28334 +               goto unlock;
28335 +       }
28336 +
28337 +       /* always topmost only */
28338 +       acl = get_acl(h_inode, type);
28339 +       if (IS_ERR(acl))
28340 +               forget_cached_acl(inode, type);
28341 +       else
28342 +               set_cached_acl(inode, type, acl);
28343 +
28344 +unlock:
28345 +       ii_read_unlock(inode);
28346 +       si_read_unlock(sb);
28347 +
28348 +out:
28349 +       AuTraceErrPtr(acl);
28350 +       return acl;
28351 +}
28352 +
28353 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28354 +                struct posix_acl *acl, int type)
28355 +{
28356 +       int err;
28357 +       ssize_t ssz;
28358 +       struct dentry *dentry;
28359 +       struct au_sxattr arg = {
28360 +               .type = AU_ACL_SET,
28361 +               .u.acl_set = {
28362 +                       .acl    = acl,
28363 +                       .type   = type
28364 +               },
28365 +       };
28366 +
28367 +       IMustLock(inode);
28368 +
28369 +       if (inode->i_ino == AUFS_ROOT_INO)
28370 +               dentry = dget(inode->i_sb->s_root);
28371 +       else {
28372 +               dentry = d_find_alias(inode);
28373 +               if (!dentry)
28374 +                       dentry = d_find_any_alias(inode);
28375 +               if (!dentry) {
28376 +                       pr_warn("cannot handle this inode, "
28377 +                               "please report to aufs-users ML\n");
28378 +                       err = -ENOENT;
28379 +                       goto out;
28380 +               }
28381 +       }
28382 +
28383 +       ssz = au_sxattr(dentry, inode, &arg);
28384 +       /* forget even it if succeeds since the branch might set differently */
28385 +       forget_cached_acl(inode, type);
28386 +       dput(dentry);
28387 +       err = ssz;
28388 +       if (ssz >= 0)
28389 +               err = 0;
28390 +
28391 +out:
28392 +       return err;
28393 +}
28394 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28395 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28396 +++ linux/fs/aufs/procfs.c      2021-11-01 23:48:34.209692595 +0100
28397 @@ -0,0 +1,170 @@
28398 +// SPDX-License-Identifier: GPL-2.0
28399 +/*
28400 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28401 + *
28402 + * This program, aufs is free software; you can redistribute it and/or modify
28403 + * it under the terms of the GNU General Public License as published by
28404 + * the Free Software Foundation; either version 2 of the License, or
28405 + * (at your option) any later version.
28406 + *
28407 + * This program is distributed in the hope that it will be useful,
28408 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28409 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28410 + * GNU General Public License for more details.
28411 + *
28412 + * You should have received a copy of the GNU General Public License
28413 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28414 + */
28415 +
28416 +/*
28417 + * procfs interfaces
28418 + */
28419 +
28420 +#include <linux/proc_fs.h>
28421 +#include "aufs.h"
28422 +
28423 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28424 +{
28425 +       struct au_sbinfo *sbinfo;
28426 +
28427 +       sbinfo = file->private_data;
28428 +       if (sbinfo) {
28429 +               au_plink_maint_leave(sbinfo);
28430 +               kobject_put(&sbinfo->si_kobj);
28431 +       }
28432 +
28433 +       return 0;
28434 +}
28435 +
28436 +static void au_procfs_plm_write_clean(struct file *file)
28437 +{
28438 +       struct au_sbinfo *sbinfo;
28439 +
28440 +       sbinfo = file->private_data;
28441 +       if (sbinfo)
28442 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28443 +}
28444 +
28445 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28446 +{
28447 +       int err;
28448 +       struct super_block *sb;
28449 +       struct au_sbinfo *sbinfo;
28450 +       struct hlist_bl_node *pos;
28451 +
28452 +       err = -EBUSY;
28453 +       if (unlikely(file->private_data))
28454 +               goto out;
28455 +
28456 +       sb = NULL;
28457 +       /* don't use au_sbilist_lock() here */
28458 +       hlist_bl_lock(&au_sbilist);
28459 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28460 +               if (id == sysaufs_si_id(sbinfo)) {
28461 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28462 +                               sb = sbinfo->si_sb;
28463 +                       break;
28464 +               }
28465 +       hlist_bl_unlock(&au_sbilist);
28466 +
28467 +       err = -EINVAL;
28468 +       if (unlikely(!sb))
28469 +               goto out;
28470 +
28471 +       err = au_plink_maint_enter(sb);
28472 +       if (!err)
28473 +               /* keep kobject_get() */
28474 +               file->private_data = sbinfo;
28475 +       else
28476 +               kobject_put(&sbinfo->si_kobj);
28477 +out:
28478 +       return err;
28479 +}
28480 +
28481 +/*
28482 + * Accept a valid "si=xxxx" only.
28483 + * Once it is accepted successfully, accept "clean" too.
28484 + */
28485 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28486 +                                  size_t count, loff_t *ppos)
28487 +{
28488 +       ssize_t err;
28489 +       unsigned long id;
28490 +       /* last newline is allowed */
28491 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28492 +
28493 +       err = -EACCES;
28494 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28495 +               goto out;
28496 +
28497 +       err = -EINVAL;
28498 +       if (unlikely(count > sizeof(buf)))
28499 +               goto out;
28500 +
28501 +       err = copy_from_user(buf, ubuf, count);
28502 +       if (unlikely(err)) {
28503 +               err = -EFAULT;
28504 +               goto out;
28505 +       }
28506 +       buf[count] = 0;
28507 +
28508 +       err = -EINVAL;
28509 +       if (!strcmp("clean", buf)) {
28510 +               au_procfs_plm_write_clean(file);
28511 +               goto out_success;
28512 +       } else if (unlikely(strncmp("si=", buf, 3)))
28513 +               goto out;
28514 +
28515 +       err = kstrtoul(buf + 3, 16, &id);
28516 +       if (unlikely(err))
28517 +               goto out;
28518 +
28519 +       err = au_procfs_plm_write_si(file, id);
28520 +       if (unlikely(err))
28521 +               goto out;
28522 +
28523 +out_success:
28524 +       err = count; /* success */
28525 +out:
28526 +       return err;
28527 +}
28528 +
28529 +static const struct proc_ops au_procfs_plm_op = {
28530 +       .proc_write     = au_procfs_plm_write,
28531 +       .proc_release   = au_procfs_plm_release
28532 +};
28533 +
28534 +/* ---------------------------------------------------------------------- */
28535 +
28536 +static struct proc_dir_entry *au_procfs_dir;
28537 +
28538 +void au_procfs_fin(void)
28539 +{
28540 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28541 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28542 +}
28543 +
28544 +int __init au_procfs_init(void)
28545 +{
28546 +       int err;
28547 +       struct proc_dir_entry *entry;
28548 +
28549 +       err = -ENOMEM;
28550 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28551 +       if (unlikely(!au_procfs_dir))
28552 +               goto out;
28553 +
28554 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28555 +                           au_procfs_dir, &au_procfs_plm_op);
28556 +       if (unlikely(!entry))
28557 +               goto out_dir;
28558 +
28559 +       err = 0;
28560 +       goto out; /* success */
28561 +
28562 +
28563 +out_dir:
28564 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28565 +out:
28566 +       return err;
28567 +}
28568 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28569 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28570 +++ linux/fs/aufs/rdu.c 2021-11-01 23:48:34.209692595 +0100
28571 @@ -0,0 +1,384 @@
28572 +// SPDX-License-Identifier: GPL-2.0
28573 +/*
28574 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28575 + *
28576 + * This program, aufs is free software; you can redistribute it and/or modify
28577 + * it under the terms of the GNU General Public License as published by
28578 + * the Free Software Foundation; either version 2 of the License, or
28579 + * (at your option) any later version.
28580 + *
28581 + * This program is distributed in the hope that it will be useful,
28582 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28583 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28584 + * GNU General Public License for more details.
28585 + *
28586 + * You should have received a copy of the GNU General Public License
28587 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28588 + */
28589 +
28590 +/*
28591 + * readdir in userspace.
28592 + */
28593 +
28594 +#include <linux/compat.h>
28595 +#include <linux/fs_stack.h>
28596 +#include <linux/security.h>
28597 +#include "aufs.h"
28598 +
28599 +/* bits for struct aufs_rdu.flags */
28600 +#define        AuRdu_CALLED    1
28601 +#define        AuRdu_CONT      (1 << 1)
28602 +#define        AuRdu_FULL      (1 << 2)
28603 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28604 +#define au_fset_rdu(flags, name) \
28605 +       do { (flags) |= AuRdu_##name; } while (0)
28606 +#define au_fclr_rdu(flags, name) \
28607 +       do { (flags) &= ~AuRdu_##name; } while (0)
28608 +
28609 +struct au_rdu_arg {
28610 +       struct dir_context              ctx;
28611 +       struct aufs_rdu                 *rdu;
28612 +       union au_rdu_ent_ul             ent;
28613 +       unsigned long                   end;
28614 +
28615 +       struct super_block              *sb;
28616 +       int                             err;
28617 +};
28618 +
28619 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28620 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28621 +{
28622 +       int err, len;
28623 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28624 +       struct aufs_rdu *rdu = arg->rdu;
28625 +       struct au_rdu_ent ent;
28626 +
28627 +       err = 0;
28628 +       arg->err = 0;
28629 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28630 +       len = au_rdu_len(nlen);
28631 +       if (arg->ent.ul + len  < arg->end) {
28632 +               ent.ino = h_ino;
28633 +               ent.bindex = rdu->cookie.bindex;
28634 +               ent.type = d_type;
28635 +               ent.nlen = nlen;
28636 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28637 +                       ent.type = DT_UNKNOWN;
28638 +
28639 +               /* unnecessary to support mmap_sem since this is a dir */
28640 +               err = -EFAULT;
28641 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28642 +                       goto out;
28643 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28644 +                       goto out;
28645 +               /* the terminating NULL */
28646 +               if (__put_user(0, arg->ent.e->name + nlen))
28647 +                       goto out;
28648 +               err = 0;
28649 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28650 +               arg->ent.ul += len;
28651 +               rdu->rent++;
28652 +       } else {
28653 +               err = -EFAULT;
28654 +               au_fset_rdu(rdu->cookie.flags, FULL);
28655 +               rdu->full = 1;
28656 +               rdu->tail = arg->ent;
28657 +       }
28658 +
28659 +out:
28660 +       /* AuTraceErr(err); */
28661 +       return err;
28662 +}
28663 +
28664 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28665 +{
28666 +       int err;
28667 +       loff_t offset;
28668 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28669 +
28670 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28671 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28672 +       err = offset;
28673 +       if (unlikely(offset != cookie->h_pos))
28674 +               goto out;
28675 +
28676 +       err = 0;
28677 +       do {
28678 +               arg->err = 0;
28679 +               au_fclr_rdu(cookie->flags, CALLED);
28680 +               /* smp_mb(); */
28681 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28682 +               if (err >= 0)
28683 +                       err = arg->err;
28684 +       } while (!err
28685 +                && au_ftest_rdu(cookie->flags, CALLED)
28686 +                && !au_ftest_rdu(cookie->flags, FULL));
28687 +       cookie->h_pos = h_file->f_pos;
28688 +
28689 +out:
28690 +       AuTraceErr(err);
28691 +       return err;
28692 +}
28693 +
28694 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28695 +{
28696 +       int err;
28697 +       aufs_bindex_t bbot;
28698 +       struct au_rdu_arg arg = {
28699 +               .ctx = {
28700 +                       .actor = au_rdu_fill
28701 +               }
28702 +       };
28703 +       struct dentry *dentry;
28704 +       struct inode *inode;
28705 +       struct file *h_file;
28706 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28707 +
28708 +       /* VERIFY_WRITE */
28709 +       err = !access_ok(rdu->ent.e, rdu->sz);
28710 +       if (unlikely(err)) {
28711 +               err = -EFAULT;
28712 +               AuTraceErr(err);
28713 +               goto out;
28714 +       }
28715 +       rdu->rent = 0;
28716 +       rdu->tail = rdu->ent;
28717 +       rdu->full = 0;
28718 +       arg.rdu = rdu;
28719 +       arg.ent = rdu->ent;
28720 +       arg.end = arg.ent.ul;
28721 +       arg.end += rdu->sz;
28722 +
28723 +       err = -ENOTDIR;
28724 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28725 +               goto out;
28726 +
28727 +       err = security_file_permission(file, MAY_READ);
28728 +       AuTraceErr(err);
28729 +       if (unlikely(err))
28730 +               goto out;
28731 +
28732 +       dentry = file->f_path.dentry;
28733 +       inode = d_inode(dentry);
28734 +       inode_lock_shared(inode);
28735 +
28736 +       arg.sb = inode->i_sb;
28737 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28738 +       if (unlikely(err))
28739 +               goto out_mtx;
28740 +       err = au_alive_dir(dentry);
28741 +       if (unlikely(err))
28742 +               goto out_si;
28743 +       /* todo: reval? */
28744 +       fi_read_lock(file);
28745 +
28746 +       err = -EAGAIN;
28747 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28748 +                    && cookie->generation != au_figen(file)))
28749 +               goto out_unlock;
28750 +
28751 +       err = 0;
28752 +       if (!rdu->blk) {
28753 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28754 +               if (!rdu->blk)
28755 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28756 +       }
28757 +       bbot = au_fbtop(file);
28758 +       if (cookie->bindex < bbot)
28759 +               cookie->bindex = bbot;
28760 +       bbot = au_fbbot_dir(file);
28761 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28762 +       for (; !err && cookie->bindex <= bbot;
28763 +            cookie->bindex++, cookie->h_pos = 0) {
28764 +               h_file = au_hf_dir(file, cookie->bindex);
28765 +               if (!h_file)
28766 +                       continue;
28767 +
28768 +               au_fclr_rdu(cookie->flags, FULL);
28769 +               err = au_rdu_do(h_file, &arg);
28770 +               AuTraceErr(err);
28771 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28772 +                       break;
28773 +       }
28774 +       AuDbg("rent %llu\n", rdu->rent);
28775 +
28776 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28777 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28778 +               au_fset_rdu(cookie->flags, CONT);
28779 +               cookie->generation = au_figen(file);
28780 +       }
28781 +
28782 +       ii_read_lock_child(inode);
28783 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28784 +       ii_read_unlock(inode);
28785 +
28786 +out_unlock:
28787 +       fi_read_unlock(file);
28788 +out_si:
28789 +       si_read_unlock(arg.sb);
28790 +out_mtx:
28791 +       inode_unlock_shared(inode);
28792 +out:
28793 +       AuTraceErr(err);
28794 +       return err;
28795 +}
28796 +
28797 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28798 +{
28799 +       int err;
28800 +       ino_t ino;
28801 +       unsigned long long nent;
28802 +       union au_rdu_ent_ul *u;
28803 +       struct au_rdu_ent ent;
28804 +       struct super_block *sb;
28805 +
28806 +       err = 0;
28807 +       nent = rdu->nent;
28808 +       u = &rdu->ent;
28809 +       sb = file->f_path.dentry->d_sb;
28810 +       si_read_lock(sb, AuLock_FLUSH);
28811 +       while (nent-- > 0) {
28812 +               /* unnecessary to support mmap_sem since this is a dir */
28813 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28814 +               if (!err)
28815 +                       /* VERIFY_WRITE */
28816 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28817 +               if (unlikely(err)) {
28818 +                       err = -EFAULT;
28819 +                       AuTraceErr(err);
28820 +                       break;
28821 +               }
28822 +
28823 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28824 +               if (!ent.wh)
28825 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28826 +               else
28827 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28828 +                                       &ino);
28829 +               if (unlikely(err)) {
28830 +                       AuTraceErr(err);
28831 +                       break;
28832 +               }
28833 +
28834 +               err = __put_user(ino, &u->e->ino);
28835 +               if (unlikely(err)) {
28836 +                       err = -EFAULT;
28837 +                       AuTraceErr(err);
28838 +                       break;
28839 +               }
28840 +               u->ul += au_rdu_len(ent.nlen);
28841 +       }
28842 +       si_read_unlock(sb);
28843 +
28844 +       return err;
28845 +}
28846 +
28847 +/* ---------------------------------------------------------------------- */
28848 +
28849 +static int au_rdu_verify(struct aufs_rdu *rdu)
28850 +{
28851 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28852 +             "%llu, b%d, 0x%x, g%u}\n",
28853 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28854 +             rdu->blk,
28855 +             rdu->rent, rdu->shwh, rdu->full,
28856 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28857 +             rdu->cookie.generation);
28858 +
28859 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28860 +               return 0;
28861 +
28862 +       AuDbg("%u:%u\n",
28863 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28864 +       return -EINVAL;
28865 +}
28866 +
28867 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28868 +{
28869 +       long err, e;
28870 +       struct aufs_rdu rdu;
28871 +       void __user *p = (void __user *)arg;
28872 +
28873 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28874 +       if (unlikely(err)) {
28875 +               err = -EFAULT;
28876 +               AuTraceErr(err);
28877 +               goto out;
28878 +       }
28879 +       err = au_rdu_verify(&rdu);
28880 +       if (unlikely(err))
28881 +               goto out;
28882 +
28883 +       switch (cmd) {
28884 +       case AUFS_CTL_RDU:
28885 +               err = au_rdu(file, &rdu);
28886 +               if (unlikely(err))
28887 +                       break;
28888 +
28889 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28890 +               if (unlikely(e)) {
28891 +                       err = -EFAULT;
28892 +                       AuTraceErr(err);
28893 +               }
28894 +               break;
28895 +       case AUFS_CTL_RDU_INO:
28896 +               err = au_rdu_ino(file, &rdu);
28897 +               break;
28898 +
28899 +       default:
28900 +               /* err = -ENOTTY; */
28901 +               err = -EINVAL;
28902 +       }
28903 +
28904 +out:
28905 +       AuTraceErr(err);
28906 +       return err;
28907 +}
28908 +
28909 +#ifdef CONFIG_COMPAT
28910 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28911 +{
28912 +       long err, e;
28913 +       struct aufs_rdu rdu;
28914 +       void __user *p = compat_ptr(arg);
28915 +
28916 +       /* todo: get_user()? */
28917 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28918 +       if (unlikely(err)) {
28919 +               err = -EFAULT;
28920 +               AuTraceErr(err);
28921 +               goto out;
28922 +       }
28923 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28924 +       err = au_rdu_verify(&rdu);
28925 +       if (unlikely(err))
28926 +               goto out;
28927 +
28928 +       switch (cmd) {
28929 +       case AUFS_CTL_RDU:
28930 +               err = au_rdu(file, &rdu);
28931 +               if (unlikely(err))
28932 +                       break;
28933 +
28934 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28935 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28936 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28937 +               if (unlikely(e)) {
28938 +                       err = -EFAULT;
28939 +                       AuTraceErr(err);
28940 +               }
28941 +               break;
28942 +       case AUFS_CTL_RDU_INO:
28943 +               err = au_rdu_ino(file, &rdu);
28944 +               break;
28945 +
28946 +       default:
28947 +               /* err = -ENOTTY; */
28948 +               err = -EINVAL;
28949 +       }
28950 +
28951 +out:
28952 +       AuTraceErr(err);
28953 +       return err;
28954 +}
28955 +#endif
28956 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28957 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28958 +++ linux/fs/aufs/rwsem.h       2021-11-01 23:48:34.209692595 +0100
28959 @@ -0,0 +1,85 @@
28960 +/* SPDX-License-Identifier: GPL-2.0 */
28961 +/*
28962 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28963 + *
28964 + * This program, aufs is free software; you can redistribute it and/or modify
28965 + * it under the terms of the GNU General Public License as published by
28966 + * the Free Software Foundation; either version 2 of the License, or
28967 + * (at your option) any later version.
28968 + *
28969 + * This program is distributed in the hope that it will be useful,
28970 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28971 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28972 + * GNU General Public License for more details.
28973 + *
28974 + * You should have received a copy of the GNU General Public License
28975 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28976 + */
28977 +
28978 +/*
28979 + * simple read-write semaphore wrappers
28980 + */
28981 +
28982 +#ifndef __AUFS_RWSEM_H__
28983 +#define __AUFS_RWSEM_H__
28984 +
28985 +#ifdef __KERNEL__
28986 +
28987 +#include "debug.h"
28988 +
28989 +/* in the future, the name 'au_rwsem' will be totally gone */
28990 +#define au_rwsem       rw_semaphore
28991 +
28992 +/* to debug easier, do not make them inlined functions */
28993 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
28994 +
28995 +#ifdef CONFIG_LOCKDEP
28996 +/* rwsem_is_locked() is unusable */
28997 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28998 +                                         && !lockdep_recursing(current) \
28999 +                                         && debug_locks                \
29000 +                                         && !lockdep_is_held_type(rw, 1))
29001 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29002 +                                         && !lockdep_recursing(current) \
29003 +                                         && debug_locks                \
29004 +                                         && !lockdep_is_held_type(rw, 0))
29005 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29006 +                                         && !lockdep_recursing(current) \
29007 +                                         && debug_locks                \
29008 +                                         && !lockdep_is_held(rw))
29009 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29010 +                                         && !lockdep_recursing(current) \
29011 +                                         && debug_locks                \
29012 +                                         && lockdep_is_held(rw))
29013 +#else
29014 +#define AuRwMustReadLock(rw)   do {} while (0)
29015 +#define AuRwMustWriteLock(rw)  do {} while (0)
29016 +#define AuRwMustAnyLock(rw)    do {} while (0)
29017 +#define AuRwDestroy(rw)                do {} while (0)
29018 +#endif
29019 +
29020 +#define au_rw_init(rw) init_rwsem(rw)
29021 +
29022 +#define au_rw_init_wlock(rw) do {              \
29023 +               au_rw_init(rw);                 \
29024 +               down_write(rw);                 \
29025 +       } while (0)
29026 +
29027 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29028 +               au_rw_init(rw);                 \
29029 +               down_write_nested(rw, lsc);     \
29030 +       } while (0)
29031 +
29032 +#define au_rw_read_lock(rw)            down_read(rw)
29033 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29034 +#define au_rw_read_unlock(rw)          up_read(rw)
29035 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29036 +#define au_rw_write_lock(rw)           down_write(rw)
29037 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29038 +#define au_rw_write_unlock(rw)         up_write(rw)
29039 +/* why is not _nested version defined? */
29040 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29041 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29042 +
29043 +#endif /* __KERNEL__ */
29044 +#endif /* __AUFS_RWSEM_H__ */
29045 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29046 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29047 +++ linux/fs/aufs/sbinfo.c      2021-11-01 23:48:34.213025928 +0100
29048 @@ -0,0 +1,314 @@
29049 +// SPDX-License-Identifier: GPL-2.0
29050 +/*
29051 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29052 + *
29053 + * This program, aufs is free software; you can redistribute it and/or modify
29054 + * it under the terms of the GNU General Public License as published by
29055 + * the Free Software Foundation; either version 2 of the License, or
29056 + * (at your option) any later version.
29057 + *
29058 + * This program is distributed in the hope that it will be useful,
29059 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29060 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29061 + * GNU General Public License for more details.
29062 + *
29063 + * You should have received a copy of the GNU General Public License
29064 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29065 + */
29066 +
29067 +/*
29068 + * superblock private data
29069 + */
29070 +
29071 +#include <linux/iversion.h>
29072 +#include "aufs.h"
29073 +
29074 +/*
29075 + * they are necessary regardless sysfs is disabled.
29076 + */
29077 +void au_si_free(struct kobject *kobj)
29078 +{
29079 +       int i;
29080 +       struct au_sbinfo *sbinfo;
29081 +       char *locked __maybe_unused; /* debug only */
29082 +
29083 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29084 +       for (i = 0; i < AuPlink_NHASH; i++)
29085 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29086 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29087 +
29088 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29089 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29090 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29091 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29092 +
29093 +       dbgaufs_si_fin(sbinfo);
29094 +       au_rw_write_lock(&sbinfo->si_rwsem);
29095 +       au_br_free(sbinfo);
29096 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29097 +
29098 +       au_kfree_try_rcu(sbinfo->si_branch);
29099 +       mutex_destroy(&sbinfo->si_xib_mtx);
29100 +       AuRwDestroy(&sbinfo->si_rwsem);
29101 +
29102 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29103 +       /* si_nfiles is waited too */
29104 +       au_kfree_rcu(sbinfo);
29105 +}
29106 +
29107 +int au_si_alloc(struct super_block *sb)
29108 +{
29109 +       int err, i;
29110 +       struct au_sbinfo *sbinfo;
29111 +
29112 +       err = -ENOMEM;
29113 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29114 +       if (unlikely(!sbinfo))
29115 +               goto out;
29116 +
29117 +       /* will be reallocated separately */
29118 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29119 +       if (unlikely(!sbinfo->si_branch))
29120 +               goto out_sbinfo;
29121 +
29122 +       err = sysaufs_si_init(sbinfo);
29123 +       if (!err) {
29124 +               dbgaufs_si_null(sbinfo);
29125 +               err = dbgaufs_si_init(sbinfo);
29126 +               if (unlikely(err))
29127 +                       kobject_put(&sbinfo->si_kobj);
29128 +       }
29129 +       if (unlikely(err))
29130 +               goto out_br;
29131 +
29132 +       au_nwt_init(&sbinfo->si_nowait);
29133 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29134 +
29135 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29136 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29137 +
29138 +       sbinfo->si_bbot = -1;
29139 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29140 +
29141 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29142 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29143 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29144 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29145 +
29146 +       au_fhsm_init(sbinfo);
29147 +
29148 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29149 +
29150 +       sbinfo->si_xino_jiffy = jiffies;
29151 +       sbinfo->si_xino_expire
29152 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29153 +       mutex_init(&sbinfo->si_xib_mtx);
29154 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29155 +
29156 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29157 +
29158 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29159 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29160 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29161 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29162 +
29163 +       for (i = 0; i < AuPlink_NHASH; i++)
29164 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29165 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29166 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29167 +
29168 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29169 +
29170 +       /* with getattr by default */
29171 +       sbinfo->si_iop_array = aufs_iop;
29172 +
29173 +       /* leave other members for sysaufs and si_mnt. */
29174 +       sbinfo->si_sb = sb;
29175 +       sb->s_fs_info = sbinfo;
29176 +       si_pid_set(sb);
29177 +       return 0; /* success */
29178 +
29179 +out_br:
29180 +       au_kfree_try_rcu(sbinfo->si_branch);
29181 +out_sbinfo:
29182 +       au_kfree_rcu(sbinfo);
29183 +out:
29184 +       return err;
29185 +}
29186 +
29187 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29188 +{
29189 +       int err, sz;
29190 +       struct au_branch **brp;
29191 +
29192 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29193 +
29194 +       err = -ENOMEM;
29195 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29196 +       if (unlikely(!sz))
29197 +               sz = sizeof(*brp);
29198 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29199 +                          may_shrink);
29200 +       if (brp) {
29201 +               sbinfo->si_branch = brp;
29202 +               err = 0;
29203 +       }
29204 +
29205 +       return err;
29206 +}
29207 +
29208 +/* ---------------------------------------------------------------------- */
29209 +
29210 +unsigned int au_sigen_inc(struct super_block *sb)
29211 +{
29212 +       unsigned int gen;
29213 +       struct inode *inode;
29214 +
29215 +       SiMustWriteLock(sb);
29216 +
29217 +       gen = ++au_sbi(sb)->si_generation;
29218 +       au_update_digen(sb->s_root);
29219 +       inode = d_inode(sb->s_root);
29220 +       au_update_iigen(inode, /*half*/0);
29221 +       inode_inc_iversion(inode);
29222 +       return gen;
29223 +}
29224 +
29225 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29226 +{
29227 +       aufs_bindex_t br_id;
29228 +       int i;
29229 +       struct au_sbinfo *sbinfo;
29230 +
29231 +       SiMustWriteLock(sb);
29232 +
29233 +       sbinfo = au_sbi(sb);
29234 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29235 +               br_id = ++sbinfo->si_last_br_id;
29236 +               AuDebugOn(br_id < 0);
29237 +               if (br_id && au_br_index(sb, br_id) < 0)
29238 +                       return br_id;
29239 +       }
29240 +
29241 +       return -1;
29242 +}
29243 +
29244 +/* ---------------------------------------------------------------------- */
29245 +
29246 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29247 +int si_read_lock(struct super_block *sb, int flags)
29248 +{
29249 +       int err;
29250 +
29251 +       err = 0;
29252 +       if (au_ftest_lock(flags, FLUSH))
29253 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29254 +
29255 +       si_noflush_read_lock(sb);
29256 +       err = au_plink_maint(sb, flags);
29257 +       if (unlikely(err))
29258 +               si_read_unlock(sb);
29259 +
29260 +       return err;
29261 +}
29262 +
29263 +int si_write_lock(struct super_block *sb, int flags)
29264 +{
29265 +       int err;
29266 +
29267 +       if (au_ftest_lock(flags, FLUSH))
29268 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29269 +
29270 +       si_noflush_write_lock(sb);
29271 +       err = au_plink_maint(sb, flags);
29272 +       if (unlikely(err))
29273 +               si_write_unlock(sb);
29274 +
29275 +       return err;
29276 +}
29277 +
29278 +/* dentry and super_block lock. call at entry point */
29279 +int aufs_read_lock(struct dentry *dentry, int flags)
29280 +{
29281 +       int err;
29282 +       struct super_block *sb;
29283 +
29284 +       sb = dentry->d_sb;
29285 +       err = si_read_lock(sb, flags);
29286 +       if (unlikely(err))
29287 +               goto out;
29288 +
29289 +       if (au_ftest_lock(flags, DW))
29290 +               di_write_lock_child(dentry);
29291 +       else
29292 +               di_read_lock_child(dentry, flags);
29293 +
29294 +       if (au_ftest_lock(flags, GEN)) {
29295 +               err = au_digen_test(dentry, au_sigen(sb));
29296 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29297 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29298 +               else if (!err)
29299 +                       err = au_dbrange_test(dentry);
29300 +               if (unlikely(err))
29301 +                       aufs_read_unlock(dentry, flags);
29302 +       }
29303 +
29304 +out:
29305 +       return err;
29306 +}
29307 +
29308 +void aufs_read_unlock(struct dentry *dentry, int flags)
29309 +{
29310 +       if (au_ftest_lock(flags, DW))
29311 +               di_write_unlock(dentry);
29312 +       else
29313 +               di_read_unlock(dentry, flags);
29314 +       si_read_unlock(dentry->d_sb);
29315 +}
29316 +
29317 +void aufs_write_lock(struct dentry *dentry)
29318 +{
29319 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29320 +       di_write_lock_child(dentry);
29321 +}
29322 +
29323 +void aufs_write_unlock(struct dentry *dentry)
29324 +{
29325 +       di_write_unlock(dentry);
29326 +       si_write_unlock(dentry->d_sb);
29327 +}
29328 +
29329 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29330 +{
29331 +       int err;
29332 +       unsigned int sigen;
29333 +       struct super_block *sb;
29334 +
29335 +       sb = d1->d_sb;
29336 +       err = si_read_lock(sb, flags);
29337 +       if (unlikely(err))
29338 +               goto out;
29339 +
29340 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29341 +
29342 +       if (au_ftest_lock(flags, GEN)) {
29343 +               sigen = au_sigen(sb);
29344 +               err = au_digen_test(d1, sigen);
29345 +               AuDebugOn(!err && au_dbrange_test(d1));
29346 +               if (!err) {
29347 +                       err = au_digen_test(d2, sigen);
29348 +                       AuDebugOn(!err && au_dbrange_test(d2));
29349 +               }
29350 +               if (unlikely(err))
29351 +                       aufs_read_and_write_unlock2(d1, d2);
29352 +       }
29353 +
29354 +out:
29355 +       return err;
29356 +}
29357 +
29358 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29359 +{
29360 +       di_write_unlock2(d1, d2);
29361 +       si_read_unlock(d1->d_sb);
29362 +}
29363 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29364 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29365 +++ linux/fs/aufs/super.c       2021-11-25 11:42:28.836641524 +0100
29366 @@ -0,0 +1,1050 @@
29367 +// SPDX-License-Identifier: GPL-2.0
29368 +/*
29369 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29370 + *
29371 + * This program, aufs is free software; you can redistribute it and/or modify
29372 + * it under the terms of the GNU General Public License as published by
29373 + * the Free Software Foundation; either version 2 of the License, or
29374 + * (at your option) any later version.
29375 + *
29376 + * This program is distributed in the hope that it will be useful,
29377 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29378 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29379 + * GNU General Public License for more details.
29380 + *
29381 + * You should have received a copy of the GNU General Public License
29382 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29383 + */
29384 +
29385 +/*
29386 + * mount and super_block operations
29387 + */
29388 +
29389 +#include <linux/iversion.h>
29390 +#include <linux/mm.h>
29391 +#include <linux/seq_file.h>
29392 +#include <linux/statfs.h>
29393 +#include <linux/vmalloc.h>
29394 +#include "aufs.h"
29395 +
29396 +/*
29397 + * super_operations
29398 + */
29399 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29400 +{
29401 +       struct au_icntnr *c;
29402 +
29403 +       c = au_cache_alloc_icntnr();
29404 +       if (c) {
29405 +               au_icntnr_init(c);
29406 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29407 +               c->iinfo.ii_hinode = NULL;
29408 +               return &c->vfs_inode;
29409 +       }
29410 +       return NULL;
29411 +}
29412 +
29413 +static void aufs_destroy_inode(struct inode *inode)
29414 +{
29415 +       if (!au_is_bad_inode(inode))
29416 +               au_iinfo_fin(inode);
29417 +}
29418 +
29419 +static void aufs_free_inode(struct inode *inode)
29420 +{
29421 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29422 +}
29423 +
29424 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29425 +{
29426 +       struct inode *inode;
29427 +       int err;
29428 +
29429 +       inode = iget_locked(sb, ino);
29430 +       if (unlikely(!inode)) {
29431 +               inode = ERR_PTR(-ENOMEM);
29432 +               goto out;
29433 +       }
29434 +       if (!(inode->i_state & I_NEW))
29435 +               goto out;
29436 +
29437 +       err = au_xigen_new(inode);
29438 +       if (!err)
29439 +               err = au_iinfo_init(inode);
29440 +       if (!err)
29441 +               inode_inc_iversion(inode);
29442 +       else {
29443 +               iget_failed(inode);
29444 +               inode = ERR_PTR(err);
29445 +       }
29446 +
29447 +out:
29448 +       /* never return NULL */
29449 +       AuDebugOn(!inode);
29450 +       AuTraceErrPtr(inode);
29451 +       return inode;
29452 +}
29453 +
29454 +/* lock free root dinfo */
29455 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29456 +{
29457 +       int err;
29458 +       aufs_bindex_t bindex, bbot;
29459 +       struct path path;
29460 +       struct au_hdentry *hdp;
29461 +       struct au_branch *br;
29462 +       au_br_perm_str_t perm;
29463 +
29464 +       err = 0;
29465 +       bbot = au_sbbot(sb);
29466 +       bindex = 0;
29467 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29468 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29469 +               br = au_sbr(sb, bindex);
29470 +               path.mnt = au_br_mnt(br);
29471 +               path.dentry = hdp->hd_dentry;
29472 +               err = au_seq_path(seq, &path);
29473 +               if (!err) {
29474 +                       au_optstr_br_perm(&perm, br->br_perm);
29475 +                       seq_printf(seq, "=%s", perm.a);
29476 +                       if (bindex != bbot)
29477 +                               seq_putc(seq, ':');
29478 +               }
29479 +       }
29480 +       if (unlikely(err || seq_has_overflowed(seq)))
29481 +               err = -E2BIG;
29482 +
29483 +       return err;
29484 +}
29485 +
29486 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29487 +                      const char *append)
29488 +{
29489 +       char *p;
29490 +
29491 +       p = fmt;
29492 +       while (*pat != ':')
29493 +               *p++ = *pat++;
29494 +       *p++ = *pat++;
29495 +       strcpy(p, append);
29496 +       AuDebugOn(strlen(fmt) >= len);
29497 +}
29498 +
29499 +static void au_show_wbr_create(struct seq_file *m, int v,
29500 +                              struct au_sbinfo *sbinfo)
29501 +{
29502 +       const char *pat;
29503 +       char fmt[32];
29504 +       struct au_wbr_mfs *mfs;
29505 +
29506 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29507 +
29508 +       seq_puts(m, ",create=");
29509 +       pat = au_optstr_wbr_create(v);
29510 +       mfs = &sbinfo->si_wbr_mfs;
29511 +       switch (v) {
29512 +       case AuWbrCreate_TDP:
29513 +       case AuWbrCreate_RR:
29514 +       case AuWbrCreate_MFS:
29515 +       case AuWbrCreate_PMFS:
29516 +               seq_puts(m, pat);
29517 +               break;
29518 +       case AuWbrCreate_MFSRR:
29519 +       case AuWbrCreate_TDMFS:
29520 +       case AuWbrCreate_PMFSRR:
29521 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29522 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29523 +               break;
29524 +       case AuWbrCreate_MFSV:
29525 +       case AuWbrCreate_PMFSV:
29526 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29527 +               seq_printf(m, fmt,
29528 +                          jiffies_to_msecs(mfs->mfs_expire)
29529 +                          / MSEC_PER_SEC);
29530 +               break;
29531 +       case AuWbrCreate_MFSRRV:
29532 +       case AuWbrCreate_TDMFSV:
29533 +       case AuWbrCreate_PMFSRRV:
29534 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29535 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29536 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29537 +               break;
29538 +       default:
29539 +               BUG();
29540 +       }
29541 +}
29542 +
29543 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29544 +{
29545 +#ifdef CONFIG_SYSFS
29546 +       return 0;
29547 +#else
29548 +       int err;
29549 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29550 +       aufs_bindex_t bindex, brid;
29551 +       struct qstr *name;
29552 +       struct file *f;
29553 +       struct dentry *d, *h_root;
29554 +       struct au_branch *br;
29555 +
29556 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29557 +
29558 +       err = 0;
29559 +       f = au_sbi(sb)->si_xib;
29560 +       if (!f)
29561 +               goto out;
29562 +
29563 +       /* stop printing the default xino path on the first writable branch */
29564 +       h_root = NULL;
29565 +       bindex = au_xi_root(sb, f->f_path.dentry);
29566 +       if (bindex >= 0) {
29567 +               br = au_sbr_sb(sb, bindex);
29568 +               h_root = au_br_dentry(br);
29569 +       }
29570 +
29571 +       d = f->f_path.dentry;
29572 +       name = &d->d_name;
29573 +       /* safe ->d_parent because the file is unlinked */
29574 +       if (d->d_parent == h_root
29575 +           && name->len == len
29576 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29577 +               goto out;
29578 +
29579 +       seq_puts(seq, ",xino=");
29580 +       err = au_xino_path(seq, f);
29581 +
29582 +out:
29583 +       return err;
29584 +#endif
29585 +}
29586 +
29587 +/* seq_file will re-call me in case of too long string */
29588 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29589 +{
29590 +       int err;
29591 +       unsigned int mnt_flags, v;
29592 +       struct super_block *sb;
29593 +       struct au_sbinfo *sbinfo;
29594 +
29595 +#define AuBool(name, str) do { \
29596 +       v = au_opt_test(mnt_flags, name); \
29597 +       if (v != au_opt_test(AuOpt_Def, name)) \
29598 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29599 +} while (0)
29600 +
29601 +#define AuStr(name, str) do { \
29602 +       v = mnt_flags & AuOptMask_##name; \
29603 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29604 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29605 +} while (0)
29606 +
29607 +#define AuUInt(name, str, val) do { \
29608 +       if (val != AUFS_##name##_DEF) \
29609 +               seq_printf(m, "," #str "=%u", val); \
29610 +} while (0)
29611 +
29612 +       sb = dentry->d_sb;
29613 +       if (sb->s_flags & SB_POSIXACL)
29614 +               seq_puts(m, ",acl");
29615 +#if 0 /* reserved for future use */
29616 +       if (sb->s_flags & SB_I_VERSION)
29617 +               seq_puts(m, ",i_version");
29618 +#endif
29619 +
29620 +       /* lock free root dinfo */
29621 +       si_noflush_read_lock(sb);
29622 +       sbinfo = au_sbi(sb);
29623 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29624 +
29625 +       mnt_flags = au_mntflags(sb);
29626 +       if (au_opt_test(mnt_flags, XINO)) {
29627 +               err = au_show_xino(m, sb);
29628 +               if (unlikely(err))
29629 +                       goto out;
29630 +       } else
29631 +               seq_puts(m, ",noxino");
29632 +
29633 +       AuBool(TRUNC_XINO, trunc_xino);
29634 +       AuStr(UDBA, udba);
29635 +       AuBool(SHWH, shwh);
29636 +       AuBool(PLINK, plink);
29637 +       AuBool(DIO, dio);
29638 +       AuBool(DIRPERM1, dirperm1);
29639 +
29640 +       v = sbinfo->si_wbr_create;
29641 +       if (v != AuWbrCreate_Def)
29642 +               au_show_wbr_create(m, v, sbinfo);
29643 +
29644 +       v = sbinfo->si_wbr_copyup;
29645 +       if (v != AuWbrCopyup_Def)
29646 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29647 +
29648 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29649 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29650 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29651 +
29652 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29653 +
29654 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29655 +       AuUInt(RDCACHE, rdcache, v);
29656 +
29657 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29658 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29659 +
29660 +       au_fhsm_show(m, sbinfo);
29661 +
29662 +       AuBool(DIRREN, dirren);
29663 +       AuBool(SUM, sum);
29664 +       /* AuBool(SUM_W, wsum); */
29665 +       AuBool(WARN_PERM, warn_perm);
29666 +       AuBool(VERBOSE, verbose);
29667 +
29668 +out:
29669 +       /* be sure to print "br:" last */
29670 +       if (!sysaufs_brs) {
29671 +               seq_puts(m, ",br:");
29672 +               au_show_brs(m, sb);
29673 +       }
29674 +       si_read_unlock(sb);
29675 +       return 0;
29676 +
29677 +#undef AuBool
29678 +#undef AuStr
29679 +#undef AuUInt
29680 +}
29681 +
29682 +/* ---------------------------------------------------------------------- */
29683 +
29684 +/* sum mode which returns the summation for statfs(2) */
29685 +
29686 +static u64 au_add_till_max(u64 a, u64 b)
29687 +{
29688 +       u64 old;
29689 +
29690 +       old = a;
29691 +       a += b;
29692 +       if (old <= a)
29693 +               return a;
29694 +       return ULLONG_MAX;
29695 +}
29696 +
29697 +static u64 au_mul_till_max(u64 a, long mul)
29698 +{
29699 +       u64 old;
29700 +
29701 +       old = a;
29702 +       a *= mul;
29703 +       if (old <= a)
29704 +               return a;
29705 +       return ULLONG_MAX;
29706 +}
29707 +
29708 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29709 +{
29710 +       int err;
29711 +       long bsize, factor;
29712 +       u64 blocks, bfree, bavail, files, ffree;
29713 +       aufs_bindex_t bbot, bindex, i;
29714 +       unsigned char shared;
29715 +       struct path h_path;
29716 +       struct super_block *h_sb;
29717 +
29718 +       err = 0;
29719 +       bsize = LONG_MAX;
29720 +       files = 0;
29721 +       ffree = 0;
29722 +       blocks = 0;
29723 +       bfree = 0;
29724 +       bavail = 0;
29725 +       bbot = au_sbbot(sb);
29726 +       for (bindex = 0; bindex <= bbot; bindex++) {
29727 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29728 +               h_sb = h_path.mnt->mnt_sb;
29729 +               shared = 0;
29730 +               for (i = 0; !shared && i < bindex; i++)
29731 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29732 +               if (shared)
29733 +                       continue;
29734 +
29735 +               /* sb->s_root for NFS is unreliable */
29736 +               h_path.dentry = h_path.mnt->mnt_root;
29737 +               err = vfs_statfs(&h_path, buf);
29738 +               if (unlikely(err))
29739 +                       goto out;
29740 +
29741 +               if (bsize > buf->f_bsize) {
29742 +                       /*
29743 +                        * we will reduce bsize, so we have to expand blocks
29744 +                        * etc. to match them again
29745 +                        */
29746 +                       factor = (bsize / buf->f_bsize);
29747 +                       blocks = au_mul_till_max(blocks, factor);
29748 +                       bfree = au_mul_till_max(bfree, factor);
29749 +                       bavail = au_mul_till_max(bavail, factor);
29750 +                       bsize = buf->f_bsize;
29751 +               }
29752 +
29753 +               factor = (buf->f_bsize / bsize);
29754 +               blocks = au_add_till_max(blocks,
29755 +                               au_mul_till_max(buf->f_blocks, factor));
29756 +               bfree = au_add_till_max(bfree,
29757 +                               au_mul_till_max(buf->f_bfree, factor));
29758 +               bavail = au_add_till_max(bavail,
29759 +                               au_mul_till_max(buf->f_bavail, factor));
29760 +               files = au_add_till_max(files, buf->f_files);
29761 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29762 +       }
29763 +
29764 +       buf->f_bsize = bsize;
29765 +       buf->f_blocks = blocks;
29766 +       buf->f_bfree = bfree;
29767 +       buf->f_bavail = bavail;
29768 +       buf->f_files = files;
29769 +       buf->f_ffree = ffree;
29770 +       buf->f_frsize = 0;
29771 +
29772 +out:
29773 +       return err;
29774 +}
29775 +
29776 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29777 +{
29778 +       int err;
29779 +       struct path h_path;
29780 +       struct super_block *sb;
29781 +
29782 +       /* lock free root dinfo */
29783 +       sb = dentry->d_sb;
29784 +       si_noflush_read_lock(sb);
29785 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29786 +               /* sb->s_root for NFS is unreliable */
29787 +               h_path.mnt = au_sbr_mnt(sb, 0);
29788 +               h_path.dentry = h_path.mnt->mnt_root;
29789 +               err = vfs_statfs(&h_path, buf);
29790 +       } else
29791 +               err = au_statfs_sum(sb, buf);
29792 +       si_read_unlock(sb);
29793 +
29794 +       if (!err) {
29795 +               buf->f_type = AUFS_SUPER_MAGIC;
29796 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29797 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29798 +       }
29799 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29800 +
29801 +       return err;
29802 +}
29803 +
29804 +/* ---------------------------------------------------------------------- */
29805 +
29806 +static int aufs_sync_fs(struct super_block *sb, int wait)
29807 +{
29808 +       int err, e;
29809 +       aufs_bindex_t bbot, bindex;
29810 +       struct au_branch *br;
29811 +       struct super_block *h_sb;
29812 +
29813 +       err = 0;
29814 +       si_noflush_read_lock(sb);
29815 +       bbot = au_sbbot(sb);
29816 +       for (bindex = 0; bindex <= bbot; bindex++) {
29817 +               br = au_sbr(sb, bindex);
29818 +               if (!au_br_writable(br->br_perm))
29819 +                       continue;
29820 +
29821 +               h_sb = au_sbr_sb(sb, bindex);
29822 +               e = vfsub_sync_filesystem(h_sb, wait);
29823 +               if (unlikely(e && !err))
29824 +                       err = e;
29825 +               /* go on even if an error happens */
29826 +       }
29827 +       si_read_unlock(sb);
29828 +
29829 +       return err;
29830 +}
29831 +
29832 +/* ---------------------------------------------------------------------- */
29833 +
29834 +/* final actions when unmounting a file system */
29835 +static void aufs_put_super(struct super_block *sb)
29836 +{
29837 +       struct au_sbinfo *sbinfo;
29838 +
29839 +       sbinfo = au_sbi(sb);
29840 +       if (sbinfo)
29841 +               kobject_put(&sbinfo->si_kobj);
29842 +}
29843 +
29844 +/* ---------------------------------------------------------------------- */
29845 +
29846 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29847 +                    struct super_block *sb, void *arg)
29848 +{
29849 +       void *array;
29850 +       unsigned long long n, sz;
29851 +
29852 +       array = NULL;
29853 +       n = 0;
29854 +       if (!*hint)
29855 +               goto out;
29856 +
29857 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29858 +               array = ERR_PTR(-EMFILE);
29859 +               pr_err("hint %llu\n", *hint);
29860 +               goto out;
29861 +       }
29862 +
29863 +       sz = sizeof(array) * *hint;
29864 +       array = kzalloc(sz, GFP_NOFS);
29865 +       if (unlikely(!array))
29866 +               array = vzalloc(sz);
29867 +       if (unlikely(!array)) {
29868 +               array = ERR_PTR(-ENOMEM);
29869 +               goto out;
29870 +       }
29871 +
29872 +       n = cb(sb, array, *hint, arg);
29873 +       AuDebugOn(n > *hint);
29874 +
29875 +out:
29876 +       *hint = n;
29877 +       return array;
29878 +}
29879 +
29880 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29881 +                                      unsigned long long max __maybe_unused,
29882 +                                      void *arg)
29883 +{
29884 +       unsigned long long n;
29885 +       struct inode **p, *inode;
29886 +       struct list_head *head;
29887 +
29888 +       n = 0;
29889 +       p = a;
29890 +       head = arg;
29891 +       spin_lock(&sb->s_inode_list_lock);
29892 +       list_for_each_entry(inode, head, i_sb_list) {
29893 +               if (!au_is_bad_inode(inode)
29894 +                   && au_ii(inode)->ii_btop >= 0) {
29895 +                       spin_lock(&inode->i_lock);
29896 +                       if (atomic_read(&inode->i_count)) {
29897 +                               au_igrab(inode);
29898 +                               *p++ = inode;
29899 +                               n++;
29900 +                               AuDebugOn(n > max);
29901 +                       }
29902 +                       spin_unlock(&inode->i_lock);
29903 +               }
29904 +       }
29905 +       spin_unlock(&sb->s_inode_list_lock);
29906 +
29907 +       return n;
29908 +}
29909 +
29910 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29911 +{
29912 +       struct au_sbinfo *sbi;
29913 +
29914 +       sbi = au_sbi(sb);
29915 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29916 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29917 +}
29918 +
29919 +void au_iarray_free(struct inode **a, unsigned long long max)
29920 +{
29921 +       unsigned long long ull;
29922 +
29923 +       for (ull = 0; ull < max; ull++)
29924 +               iput(a[ull]);
29925 +       kvfree(a);
29926 +}
29927 +
29928 +/* ---------------------------------------------------------------------- */
29929 +
29930 +/*
29931 + * refresh dentry and inode at remount time.
29932 + */
29933 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29934 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29935 +                     struct dentry *parent)
29936 +{
29937 +       int err;
29938 +
29939 +       di_write_lock_child(dentry);
29940 +       di_read_lock_parent(parent, AuLock_IR);
29941 +       err = au_refresh_dentry(dentry, parent);
29942 +       if (!err && dir_flags)
29943 +               au_hn_reset(d_inode(dentry), dir_flags);
29944 +       di_read_unlock(parent, AuLock_IR);
29945 +       di_write_unlock(dentry);
29946 +
29947 +       return err;
29948 +}
29949 +
29950 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29951 +                          struct au_sbinfo *sbinfo,
29952 +                          const unsigned int dir_flags, unsigned int do_idop)
29953 +{
29954 +       int err;
29955 +       struct dentry *parent;
29956 +
29957 +       err = 0;
29958 +       parent = dget_parent(dentry);
29959 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29960 +               if (d_really_is_positive(dentry)) {
29961 +                       if (!d_is_dir(dentry))
29962 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29963 +                                                parent);
29964 +                       else {
29965 +                               err = au_do_refresh(dentry, dir_flags, parent);
29966 +                               if (unlikely(err))
29967 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29968 +                       }
29969 +               } else
29970 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29971 +               AuDbgDentry(dentry);
29972 +       }
29973 +       dput(parent);
29974 +
29975 +       if (!err) {
29976 +               if (do_idop)
29977 +                       au_refresh_dop(dentry, /*force_reval*/0);
29978 +       } else
29979 +               au_refresh_dop(dentry, /*force_reval*/1);
29980 +
29981 +       AuTraceErr(err);
29982 +       return err;
29983 +}
29984 +
29985 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29986 +{
29987 +       int err, i, j, ndentry, e;
29988 +       unsigned int sigen;
29989 +       struct au_dcsub_pages dpages;
29990 +       struct au_dpage *dpage;
29991 +       struct dentry **dentries, *d;
29992 +       struct au_sbinfo *sbinfo;
29993 +       struct dentry *root = sb->s_root;
29994 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29995 +
29996 +       if (do_idop)
29997 +               au_refresh_dop(root, /*force_reval*/0);
29998 +
29999 +       err = au_dpages_init(&dpages, GFP_NOFS);
30000 +       if (unlikely(err))
30001 +               goto out;
30002 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30003 +       if (unlikely(err))
30004 +               goto out_dpages;
30005 +
30006 +       sigen = au_sigen(sb);
30007 +       sbinfo = au_sbi(sb);
30008 +       for (i = 0; i < dpages.ndpage; i++) {
30009 +               dpage = dpages.dpages + i;
30010 +               dentries = dpage->dentries;
30011 +               ndentry = dpage->ndentry;
30012 +               for (j = 0; j < ndentry; j++) {
30013 +                       d = dentries[j];
30014 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30015 +                                           do_idop);
30016 +                       if (unlikely(e && !err))
30017 +                               err = e;
30018 +                       /* go on even err */
30019 +               }
30020 +       }
30021 +
30022 +out_dpages:
30023 +       au_dpages_free(&dpages);
30024 +out:
30025 +       return err;
30026 +}
30027 +
30028 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30029 +{
30030 +       int err, e;
30031 +       unsigned int sigen;
30032 +       unsigned long long max, ull;
30033 +       struct inode *inode, **array;
30034 +
30035 +       array = au_iarray_alloc(sb, &max);
30036 +       err = PTR_ERR(array);
30037 +       if (IS_ERR(array))
30038 +               goto out;
30039 +
30040 +       err = 0;
30041 +       sigen = au_sigen(sb);
30042 +       for (ull = 0; ull < max; ull++) {
30043 +               inode = array[ull];
30044 +               if (unlikely(!inode))
30045 +                       break;
30046 +
30047 +               e = 0;
30048 +               ii_write_lock_child(inode);
30049 +               if (au_iigen(inode, NULL) != sigen) {
30050 +                       e = au_refresh_hinode_self(inode);
30051 +                       if (unlikely(e)) {
30052 +                               au_refresh_iop(inode, /*force_getattr*/1);
30053 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30054 +                               if (!err)
30055 +                                       err = e;
30056 +                               /* go on even if err */
30057 +                       }
30058 +               }
30059 +               if (!e && do_idop)
30060 +                       au_refresh_iop(inode, /*force_getattr*/0);
30061 +               ii_write_unlock(inode);
30062 +       }
30063 +
30064 +       au_iarray_free(array, max);
30065 +
30066 +out:
30067 +       return err;
30068 +}
30069 +
30070 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30071 +{
30072 +       int err, e;
30073 +       unsigned int udba;
30074 +       aufs_bindex_t bindex, bbot;
30075 +       struct dentry *root;
30076 +       struct inode *inode;
30077 +       struct au_branch *br;
30078 +       struct au_sbinfo *sbi;
30079 +
30080 +       au_sigen_inc(sb);
30081 +       sbi = au_sbi(sb);
30082 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30083 +
30084 +       root = sb->s_root;
30085 +       DiMustNoWaiters(root);
30086 +       inode = d_inode(root);
30087 +       IiMustNoWaiters(inode);
30088 +
30089 +       udba = au_opt_udba(sb);
30090 +       bbot = au_sbbot(sb);
30091 +       for (bindex = 0; bindex <= bbot; bindex++) {
30092 +               br = au_sbr(sb, bindex);
30093 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30094 +               if (unlikely(err))
30095 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30096 +                               bindex, err);
30097 +               /* go on even if err */
30098 +       }
30099 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30100 +
30101 +       if (do_idop) {
30102 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30103 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30104 +                       sb->s_d_op = &aufs_dop_noreval;
30105 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30106 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30107 +               } else {
30108 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30109 +                       sb->s_d_op = &aufs_dop;
30110 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30111 +                       sbi->si_iop_array = aufs_iop;
30112 +               }
30113 +               pr_info("reset to %ps and %ps\n",
30114 +                       sb->s_d_op, sbi->si_iop_array);
30115 +       }
30116 +
30117 +       di_write_unlock(root);
30118 +       err = au_refresh_d(sb, do_idop);
30119 +       e = au_refresh_i(sb, do_idop);
30120 +       if (unlikely(e && !err))
30121 +               err = e;
30122 +       /* aufs_write_lock() calls ..._child() */
30123 +       di_write_lock_child(root);
30124 +
30125 +       au_cpup_attr_all(inode, /*force*/1);
30126 +
30127 +       if (unlikely(err))
30128 +               AuIOErr("refresh failed, ignored, %d\n", err);
30129 +}
30130 +
30131 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30132 +static int cvt_err(int err)
30133 +{
30134 +       AuTraceErr(err);
30135 +
30136 +       switch (err) {
30137 +       case -ENOENT:
30138 +       case -ENOTDIR:
30139 +       case -EEXIST:
30140 +       case -EIO:
30141 +               err = -EINVAL;
30142 +       }
30143 +       return err;
30144 +}
30145 +
30146 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30147 +{
30148 +       int err, do_dx;
30149 +       unsigned int mntflags;
30150 +       struct au_opts opts = {
30151 +               .opt = NULL
30152 +       };
30153 +       struct dentry *root;
30154 +       struct inode *inode;
30155 +       struct au_sbinfo *sbinfo;
30156 +
30157 +       err = 0;
30158 +       root = sb->s_root;
30159 +       if (!data || !*data) {
30160 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30161 +               if (!err) {
30162 +                       di_write_lock_child(root);
30163 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30164 +                       aufs_write_unlock(root);
30165 +               }
30166 +               goto out;
30167 +       }
30168 +
30169 +       err = -ENOMEM;
30170 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30171 +       if (unlikely(!opts.opt))
30172 +               goto out;
30173 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30174 +       opts.flags = AuOpts_REMOUNT;
30175 +       opts.sb_flags = *flags;
30176 +
30177 +       /* parse it before aufs lock */
30178 +       err = au_opts_parse(sb, data, &opts);
30179 +       if (unlikely(err))
30180 +               goto out_opts;
30181 +
30182 +       sbinfo = au_sbi(sb);
30183 +       inode = d_inode(root);
30184 +       inode_lock(inode);
30185 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30186 +       if (unlikely(err))
30187 +               goto out_mtx;
30188 +       di_write_lock_child(root);
30189 +
30190 +       /* au_opts_remount() may return an error */
30191 +       err = au_opts_remount(sb, &opts);
30192 +       au_opts_free(&opts);
30193 +
30194 +       if (au_ftest_opts(opts.flags, REFRESH))
30195 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30196 +
30197 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30198 +               mntflags = au_mntflags(sb);
30199 +               do_dx = !!au_opt_test(mntflags, DIO);
30200 +               au_dy_arefresh(do_dx);
30201 +       }
30202 +
30203 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30204 +       aufs_write_unlock(root);
30205 +
30206 +out_mtx:
30207 +       inode_unlock(inode);
30208 +out_opts:
30209 +       free_page((unsigned long)opts.opt);
30210 +out:
30211 +       err = cvt_err(err);
30212 +       AuTraceErr(err);
30213 +       return err;
30214 +}
30215 +
30216 +static const struct super_operations aufs_sop = {
30217 +       .alloc_inode    = aufs_alloc_inode,
30218 +       .destroy_inode  = aufs_destroy_inode,
30219 +       .free_inode     = aufs_free_inode,
30220 +       /* always deleting, no clearing */
30221 +       .drop_inode     = generic_delete_inode,
30222 +       .show_options   = aufs_show_options,
30223 +       .statfs         = aufs_statfs,
30224 +       .put_super      = aufs_put_super,
30225 +       .sync_fs        = aufs_sync_fs,
30226 +       .remount_fs     = aufs_remount_fs
30227 +};
30228 +
30229 +/* ---------------------------------------------------------------------- */
30230 +
30231 +static int alloc_root(struct super_block *sb)
30232 +{
30233 +       int err;
30234 +       struct inode *inode;
30235 +       struct dentry *root;
30236 +
30237 +       err = -ENOMEM;
30238 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30239 +       err = PTR_ERR(inode);
30240 +       if (IS_ERR(inode))
30241 +               goto out;
30242 +
30243 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30244 +       inode->i_fop = &aufs_dir_fop;
30245 +       inode->i_mode = S_IFDIR;
30246 +       set_nlink(inode, 2);
30247 +       unlock_new_inode(inode);
30248 +
30249 +       root = d_make_root(inode);
30250 +       if (unlikely(!root))
30251 +               goto out;
30252 +       err = PTR_ERR(root);
30253 +       if (IS_ERR(root))
30254 +               goto out;
30255 +
30256 +       err = au_di_init(root);
30257 +       if (!err) {
30258 +               sb->s_root = root;
30259 +               return 0; /* success */
30260 +       }
30261 +       dput(root);
30262 +
30263 +out:
30264 +       return err;
30265 +}
30266 +
30267 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30268 +                          int silent __maybe_unused)
30269 +{
30270 +       int err;
30271 +       struct au_opts opts = {
30272 +               .opt = NULL
30273 +       };
30274 +       struct au_sbinfo *sbinfo;
30275 +       struct dentry *root;
30276 +       struct inode *inode;
30277 +       char *arg = raw_data;
30278 +
30279 +       if (unlikely(!arg || !*arg)) {
30280 +               err = -EINVAL;
30281 +               pr_err("no arg\n");
30282 +               goto out;
30283 +       }
30284 +
30285 +       err = -ENOMEM;
30286 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30287 +       if (unlikely(!opts.opt))
30288 +               goto out;
30289 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30290 +       opts.sb_flags = sb->s_flags;
30291 +
30292 +       err = au_si_alloc(sb);
30293 +       if (unlikely(err))
30294 +               goto out_opts;
30295 +       sbinfo = au_sbi(sb);
30296 +
30297 +       /* all timestamps always follow the ones on the branch */
30298 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30299 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30300 +       sb->s_op = &aufs_sop;
30301 +       sb->s_d_op = &aufs_dop;
30302 +       sb->s_magic = AUFS_SUPER_MAGIC;
30303 +       sb->s_maxbytes = 0;
30304 +       sb->s_stack_depth = 1;
30305 +       au_export_init(sb);
30306 +       au_xattr_init(sb);
30307 +
30308 +       err = alloc_root(sb);
30309 +       if (unlikely(err)) {
30310 +               si_write_unlock(sb);
30311 +               goto out_info;
30312 +       }
30313 +       root = sb->s_root;
30314 +       inode = d_inode(root);
30315 +
30316 +       /*
30317 +        * actually we can parse options regardless aufs lock here.
30318 +        * but at remount time, parsing must be done before aufs lock.
30319 +        * so we follow the same rule.
30320 +        */
30321 +       ii_write_lock_parent(inode);
30322 +       aufs_write_unlock(root);
30323 +       err = au_opts_parse(sb, arg, &opts);
30324 +       if (unlikely(err))
30325 +               goto out_root;
30326 +
30327 +       /* lock vfs_inode first, then aufs. */
30328 +       inode_lock(inode);
30329 +       aufs_write_lock(root);
30330 +       err = au_opts_mount(sb, &opts);
30331 +       au_opts_free(&opts);
30332 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30333 +               sb->s_d_op = &aufs_dop_noreval;
30334 +               pr_info("%ps\n", sb->s_d_op);
30335 +               au_refresh_dop(root, /*force_reval*/0);
30336 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30337 +               au_refresh_iop(inode, /*force_getattr*/0);
30338 +       }
30339 +       aufs_write_unlock(root);
30340 +       inode_unlock(inode);
30341 +       if (!err)
30342 +               goto out_opts; /* success */
30343 +
30344 +out_root:
30345 +       dput(root);
30346 +       sb->s_root = NULL;
30347 +out_info:
30348 +       kobject_put(&sbinfo->si_kobj);
30349 +       sb->s_fs_info = NULL;
30350 +out_opts:
30351 +       free_page((unsigned long)opts.opt);
30352 +out:
30353 +       AuTraceErr(err);
30354 +       err = cvt_err(err);
30355 +       AuTraceErr(err);
30356 +       return err;
30357 +}
30358 +
30359 +/* ---------------------------------------------------------------------- */
30360 +
30361 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30362 +                                const char *dev_name __maybe_unused,
30363 +                                void *raw_data)
30364 +{
30365 +       struct dentry *root;
30366 +
30367 +       /* all timestamps always follow the ones on the branch */
30368 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30369 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30370 +       if (IS_ERR(root))
30371 +               goto out;
30372 +
30373 +       au_sbilist_add(root->d_sb);
30374 +
30375 +out:
30376 +       return root;
30377 +}
30378 +
30379 +static void aufs_kill_sb(struct super_block *sb)
30380 +{
30381 +       struct au_sbinfo *sbinfo;
30382 +
30383 +       sbinfo = au_sbi(sb);
30384 +       if (sbinfo) {
30385 +               au_sbilist_del(sb);
30386 +               aufs_write_lock(sb->s_root);
30387 +               au_fhsm_fin(sb);
30388 +               if (sbinfo->si_wbr_create_ops->fin)
30389 +                       sbinfo->si_wbr_create_ops->fin(sb);
30390 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30391 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30392 +                       au_remount_refresh(sb, /*do_idop*/0);
30393 +               }
30394 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30395 +                       au_plink_put(sb, /*verbose*/1);
30396 +               au_xino_clr(sb);
30397 +               au_dr_opt_flush(sb);
30398 +               sbinfo->si_sb = NULL;
30399 +               aufs_write_unlock(sb->s_root);
30400 +               au_nwt_flush(&sbinfo->si_nowait);
30401 +       }
30402 +       kill_anon_super(sb);
30403 +}
30404 +
30405 +struct file_system_type aufs_fs_type = {
30406 +       .name           = AUFS_FSTYPE,
30407 +       /* a race between rename and others */
30408 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30409 +                               /* untested */
30410 +                               /*| FS_ALLOW_IDMAP*/
30411 +                               ,
30412 +       .mount          = aufs_mount,
30413 +       .kill_sb        = aufs_kill_sb,
30414 +       /* no need to __module_get() and module_put(). */
30415 +       .owner          = THIS_MODULE,
30416 +};
30417 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30418 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30419 +++ linux/fs/aufs/super.h       2021-11-01 23:48:34.213025928 +0100
30420 @@ -0,0 +1,587 @@
30421 +/* SPDX-License-Identifier: GPL-2.0 */
30422 +/*
30423 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30424 + *
30425 + * This program, aufs is free software; you can redistribute it and/or modify
30426 + * it under the terms of the GNU General Public License as published by
30427 + * the Free Software Foundation; either version 2 of the License, or
30428 + * (at your option) any later version.
30429 + *
30430 + * This program is distributed in the hope that it will be useful,
30431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30433 + * GNU General Public License for more details.
30434 + *
30435 + * You should have received a copy of the GNU General Public License
30436 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30437 + */
30438 +
30439 +/*
30440 + * super_block operations
30441 + */
30442 +
30443 +#ifndef __AUFS_SUPER_H__
30444 +#define __AUFS_SUPER_H__
30445 +
30446 +#ifdef __KERNEL__
30447 +
30448 +#include <linux/fs.h>
30449 +#include <linux/kobject.h>
30450 +#include "hbl.h"
30451 +#include "lcnt.h"
30452 +#include "rwsem.h"
30453 +#include "wkq.h"
30454 +
30455 +/* policies to select one among multiple writable branches */
30456 +struct au_wbr_copyup_operations {
30457 +       int (*copyup)(struct dentry *dentry);
30458 +};
30459 +
30460 +#define AuWbr_DIR      1               /* target is a dir */
30461 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30462 +
30463 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30464 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30465 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30466 +
30467 +struct au_wbr_create_operations {
30468 +       int (*create)(struct dentry *dentry, unsigned int flags);
30469 +       int (*init)(struct super_block *sb);
30470 +       int (*fin)(struct super_block *sb);
30471 +};
30472 +
30473 +struct au_wbr_mfs {
30474 +       struct mutex    mfs_lock; /* protect this structure */
30475 +       unsigned long   mfs_jiffy;
30476 +       unsigned long   mfs_expire;
30477 +       aufs_bindex_t   mfs_bindex;
30478 +
30479 +       unsigned long long      mfsrr_bytes;
30480 +       unsigned long long      mfsrr_watermark;
30481 +};
30482 +
30483 +#define AuPlink_NHASH 100
30484 +static inline int au_plink_hash(ino_t ino)
30485 +{
30486 +       return ino % AuPlink_NHASH;
30487 +}
30488 +
30489 +/* File-based Hierarchical Storage Management */
30490 +struct au_fhsm {
30491 +#ifdef CONFIG_AUFS_FHSM
30492 +       /* allow only one process who can receive the notification */
30493 +       spinlock_t              fhsm_spin;
30494 +       pid_t                   fhsm_pid;
30495 +       wait_queue_head_t       fhsm_wqh;
30496 +       atomic_t                fhsm_readable;
30497 +
30498 +       /* these are protected by si_rwsem */
30499 +       unsigned long           fhsm_expire;
30500 +       aufs_bindex_t           fhsm_bottom;
30501 +#endif
30502 +};
30503 +
30504 +struct au_branch;
30505 +struct au_sbinfo {
30506 +       /* nowait tasks in the system-wide workqueue */
30507 +       struct au_nowait_tasks  si_nowait;
30508 +
30509 +       /*
30510 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30511 +        * rwsem for au_sbinfo is necessary.
30512 +        */
30513 +       struct au_rwsem         si_rwsem;
30514 +
30515 +       /*
30516 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30517 +        * remount.
30518 +        */
30519 +       au_lcnt_t               si_ninodes, si_nfiles;
30520 +
30521 +       /* branch management */
30522 +       unsigned int            si_generation;
30523 +
30524 +       /* see AuSi_ flags */
30525 +       unsigned char           au_si_status;
30526 +
30527 +       aufs_bindex_t           si_bbot;
30528 +
30529 +       /* dirty trick to keep br_id plus */
30530 +       unsigned int            si_last_br_id :
30531 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30532 +       struct au_branch        **si_branch;
30533 +
30534 +       /* policy to select a writable branch */
30535 +       unsigned char           si_wbr_copyup;
30536 +       unsigned char           si_wbr_create;
30537 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30538 +       struct au_wbr_create_operations *si_wbr_create_ops;
30539 +
30540 +       /* round robin */
30541 +       atomic_t                si_wbr_rr_next;
30542 +
30543 +       /* most free space */
30544 +       struct au_wbr_mfs       si_wbr_mfs;
30545 +
30546 +       /* File-based Hierarchical Storage Management */
30547 +       struct au_fhsm          si_fhsm;
30548 +
30549 +       /* mount flags */
30550 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30551 +       unsigned int            si_mntflags;
30552 +
30553 +       /* external inode number (bitmap and translation table) */
30554 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30555 +
30556 +       struct file             *si_xib;
30557 +       struct mutex            si_xib_mtx; /* protect xib members */
30558 +       unsigned long           *si_xib_buf;
30559 +       unsigned long           si_xib_last_pindex;
30560 +       int                     si_xib_next_bit;
30561 +
30562 +       unsigned long           si_xino_jiffy;
30563 +       unsigned long           si_xino_expire;
30564 +       /* reserved for future use */
30565 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30566 +
30567 +#ifdef CONFIG_AUFS_EXPORT
30568 +       /* i_generation */
30569 +       /* todo: make xigen file an array to support many inode numbers */
30570 +       struct file             *si_xigen;
30571 +       atomic_t                si_xigen_next;
30572 +#endif
30573 +
30574 +       /* dirty trick to support atomic_open */
30575 +       struct hlist_bl_head    si_aopen;
30576 +
30577 +       /* vdir parameters */
30578 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30579 +       unsigned int            si_rdblk;       /* deblk size */
30580 +       unsigned int            si_rdhash;      /* hash size */
30581 +
30582 +       /*
30583 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30584 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30585 +        * future fsck.aufs or kernel thread will remove them later.
30586 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30587 +        */
30588 +       unsigned int            si_dirwh;
30589 +
30590 +       /* pseudo_link list */
30591 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30592 +       wait_queue_head_t       si_plink_wq;
30593 +       spinlock_t              si_plink_maint_lock;
30594 +       pid_t                   si_plink_maint_pid;
30595 +
30596 +       /* file list */
30597 +       struct hlist_bl_head    si_files;
30598 +
30599 +       /* with/without getattr, brother of sb->s_d_op */
30600 +       const struct inode_operations *si_iop_array;
30601 +
30602 +       /*
30603 +        * sysfs and lifetime management.
30604 +        * this is not a small structure and it may be a waste of memory in case
30605 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30606 +        * but using sysfs is majority.
30607 +        */
30608 +       struct kobject          si_kobj;
30609 +#ifdef CONFIG_DEBUG_FS
30610 +       struct dentry            *si_dbgaufs;
30611 +       struct dentry            *si_dbgaufs_plink;
30612 +       struct dentry            *si_dbgaufs_xib;
30613 +#ifdef CONFIG_AUFS_EXPORT
30614 +       struct dentry            *si_dbgaufs_xigen;
30615 +#endif
30616 +#endif
30617 +
30618 +#ifdef CONFIG_AUFS_SBILIST
30619 +       struct hlist_bl_node    si_list;
30620 +#endif
30621 +
30622 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30623 +       struct super_block      *si_sb;
30624 +};
30625 +
30626 +/* sbinfo status flags */
30627 +/*
30628 + * set true when refresh_dirs() failed at remount time.
30629 + * then try refreshing dirs at access time again.
30630 + * if it is false, refreshing dirs at access time is unnecessary
30631 + */
30632 +#define AuSi_FAILED_REFRESH_DIR        1
30633 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30634 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30635 +
30636 +#ifndef CONFIG_AUFS_FHSM
30637 +#undef AuSi_FHSM
30638 +#define AuSi_FHSM              0
30639 +#endif
30640 +
30641 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30642 +                                          unsigned int flag)
30643 +{
30644 +       AuRwMustAnyLock(&sbi->si_rwsem);
30645 +       return sbi->au_si_status & flag;
30646 +}
30647 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30648 +#define au_fset_si(sbinfo, name) do { \
30649 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30650 +       (sbinfo)->au_si_status |= AuSi_##name; \
30651 +} while (0)
30652 +#define au_fclr_si(sbinfo, name) do { \
30653 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30654 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30655 +} while (0)
30656 +
30657 +/* ---------------------------------------------------------------------- */
30658 +
30659 +/* policy to select one among writable branches */
30660 +#define AuWbrCopyup(sbinfo, ...) \
30661 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30662 +#define AuWbrCreate(sbinfo, ...) \
30663 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30664 +
30665 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30666 +#define AuLock_DW              1               /* write-lock dentry */
30667 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30668 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30669 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30670 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30671 +                                               /* except RENAME_EXCHANGE */
30672 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30673 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30674 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30675 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30676 +#define au_fset_lock(flags, name) \
30677 +       do { (flags) |= AuLock_##name; } while (0)
30678 +#define au_fclr_lock(flags, name) \
30679 +       do { (flags) &= ~AuLock_##name; } while (0)
30680 +
30681 +/* ---------------------------------------------------------------------- */
30682 +
30683 +/* super.c */
30684 +extern struct file_system_type aufs_fs_type;
30685 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30686 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30687 +                                          unsigned long long max, void *arg);
30688 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30689 +                    struct super_block *sb, void *arg);
30690 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30691 +void au_iarray_free(struct inode **a, unsigned long long max);
30692 +
30693 +/* sbinfo.c */
30694 +void au_si_free(struct kobject *kobj);
30695 +int au_si_alloc(struct super_block *sb);
30696 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30697 +
30698 +unsigned int au_sigen_inc(struct super_block *sb);
30699 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30700 +
30701 +int si_read_lock(struct super_block *sb, int flags);
30702 +int si_write_lock(struct super_block *sb, int flags);
30703 +int aufs_read_lock(struct dentry *dentry, int flags);
30704 +void aufs_read_unlock(struct dentry *dentry, int flags);
30705 +void aufs_write_lock(struct dentry *dentry);
30706 +void aufs_write_unlock(struct dentry *dentry);
30707 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30708 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30709 +
30710 +/* wbr_policy.c */
30711 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30712 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30713 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30714 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30715 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30716 +
30717 +/* mvdown.c */
30718 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30719 +
30720 +#ifdef CONFIG_AUFS_FHSM
30721 +/* fhsm.c */
30722 +
30723 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30724 +{
30725 +       pid_t pid;
30726 +
30727 +       spin_lock(&fhsm->fhsm_spin);
30728 +       pid = fhsm->fhsm_pid;
30729 +       spin_unlock(&fhsm->fhsm_spin);
30730 +
30731 +       return pid;
30732 +}
30733 +
30734 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30735 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30736 +int au_fhsm_fd(struct super_block *sb, int oflags);
30737 +int au_fhsm_br_alloc(struct au_branch *br);
30738 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30739 +void au_fhsm_fin(struct super_block *sb);
30740 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30741 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30742 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30743 +#else
30744 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30745 +          int force)
30746 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30747 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30748 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30749 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30750 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30751 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30752 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30753 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30754 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30755 +#endif
30756 +
30757 +/* ---------------------------------------------------------------------- */
30758 +
30759 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30760 +{
30761 +       return sb->s_fs_info;
30762 +}
30763 +
30764 +/* ---------------------------------------------------------------------- */
30765 +
30766 +#ifdef CONFIG_AUFS_EXPORT
30767 +int au_test_nfsd(void);
30768 +void au_export_init(struct super_block *sb);
30769 +void au_xigen_inc(struct inode *inode);
30770 +int au_xigen_new(struct inode *inode);
30771 +int au_xigen_set(struct super_block *sb, struct path *path);
30772 +void au_xigen_clr(struct super_block *sb);
30773 +
30774 +static inline int au_busy_or_stale(void)
30775 +{
30776 +       if (!au_test_nfsd())
30777 +               return -EBUSY;
30778 +       return -ESTALE;
30779 +}
30780 +#else
30781 +AuStubInt0(au_test_nfsd, void)
30782 +AuStubVoid(au_export_init, struct super_block *sb)
30783 +AuStubVoid(au_xigen_inc, struct inode *inode)
30784 +AuStubInt0(au_xigen_new, struct inode *inode)
30785 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30786 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30787 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30788 +#endif /* CONFIG_AUFS_EXPORT */
30789 +
30790 +/* ---------------------------------------------------------------------- */
30791 +
30792 +#ifdef CONFIG_AUFS_SBILIST
30793 +/* module.c */
30794 +extern struct hlist_bl_head au_sbilist;
30795 +
30796 +static inline void au_sbilist_init(void)
30797 +{
30798 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30799 +}
30800 +
30801 +static inline void au_sbilist_add(struct super_block *sb)
30802 +{
30803 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30804 +}
30805 +
30806 +static inline void au_sbilist_del(struct super_block *sb)
30807 +{
30808 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30809 +}
30810 +
30811 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30812 +static inline void au_sbilist_lock(void)
30813 +{
30814 +       hlist_bl_lock(&au_sbilist);
30815 +}
30816 +
30817 +static inline void au_sbilist_unlock(void)
30818 +{
30819 +       hlist_bl_unlock(&au_sbilist);
30820 +}
30821 +#define AuGFP_SBILIST  GFP_ATOMIC
30822 +#else
30823 +AuStubVoid(au_sbilist_lock, void)
30824 +AuStubVoid(au_sbilist_unlock, void)
30825 +#define AuGFP_SBILIST  GFP_NOFS
30826 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30827 +#else
30828 +AuStubVoid(au_sbilist_init, void)
30829 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30830 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30831 +AuStubVoid(au_sbilist_lock, void)
30832 +AuStubVoid(au_sbilist_unlock, void)
30833 +#define AuGFP_SBILIST  GFP_NOFS
30834 +#endif
30835 +
30836 +/* ---------------------------------------------------------------------- */
30837 +
30838 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30839 +{
30840 +       /*
30841 +        * This function is a dynamic '__init' function actually,
30842 +        * so the tiny check for si_rwsem is unnecessary.
30843 +        */
30844 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30845 +#ifdef CONFIG_DEBUG_FS
30846 +       sbinfo->si_dbgaufs = NULL;
30847 +       sbinfo->si_dbgaufs_plink = NULL;
30848 +       sbinfo->si_dbgaufs_xib = NULL;
30849 +#ifdef CONFIG_AUFS_EXPORT
30850 +       sbinfo->si_dbgaufs_xigen = NULL;
30851 +#endif
30852 +#endif
30853 +}
30854 +
30855 +/* ---------------------------------------------------------------------- */
30856 +
30857 +/* current->atomic_flags */
30858 +/* this value should never corrupt the ones defined in linux/sched.h */
30859 +#define PFA_AUFS       0x10
30860 +
30861 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30862 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30863 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30864 +
30865 +static inline int si_pid_test(struct super_block *sb)
30866 +{
30867 +       return !!task_test_aufs(current);
30868 +}
30869 +
30870 +static inline void si_pid_clr(struct super_block *sb)
30871 +{
30872 +       AuDebugOn(!task_test_aufs(current));
30873 +       task_clear_aufs(current);
30874 +}
30875 +
30876 +static inline void si_pid_set(struct super_block *sb)
30877 +{
30878 +       AuDebugOn(task_test_aufs(current));
30879 +       task_set_aufs(current);
30880 +}
30881 +
30882 +/* ---------------------------------------------------------------------- */
30883 +
30884 +/* lock superblock. mainly for entry point functions */
30885 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30886 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30887 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30888 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30889 +/*
30890 +#define __si_read_trylock_nested(sb) \
30891 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30892 +#define __si_write_trylock_nested(sb) \
30893 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30894 +*/
30895 +
30896 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30897 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30898 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30899 +
30900 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30901 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30902 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30903 +
30904 +static inline void si_noflush_read_lock(struct super_block *sb)
30905 +{
30906 +       __si_read_lock(sb);
30907 +       si_pid_set(sb);
30908 +}
30909 +
30910 +static inline int si_noflush_read_trylock(struct super_block *sb)
30911 +{
30912 +       int locked;
30913 +
30914 +       locked = __si_read_trylock(sb);
30915 +       if (locked)
30916 +               si_pid_set(sb);
30917 +       return locked;
30918 +}
30919 +
30920 +static inline void si_noflush_write_lock(struct super_block *sb)
30921 +{
30922 +       __si_write_lock(sb);
30923 +       si_pid_set(sb);
30924 +}
30925 +
30926 +static inline int si_noflush_write_trylock(struct super_block *sb)
30927 +{
30928 +       int locked;
30929 +
30930 +       locked = __si_write_trylock(sb);
30931 +       if (locked)
30932 +               si_pid_set(sb);
30933 +       return locked;
30934 +}
30935 +
30936 +#if 0 /* reserved */
30937 +static inline int si_read_trylock(struct super_block *sb, int flags)
30938 +{
30939 +       if (au_ftest_lock(flags, FLUSH))
30940 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30941 +       return si_noflush_read_trylock(sb);
30942 +}
30943 +#endif
30944 +
30945 +static inline void si_read_unlock(struct super_block *sb)
30946 +{
30947 +       si_pid_clr(sb);
30948 +       __si_read_unlock(sb);
30949 +}
30950 +
30951 +#if 0 /* reserved */
30952 +static inline int si_write_trylock(struct super_block *sb, int flags)
30953 +{
30954 +       if (au_ftest_lock(flags, FLUSH))
30955 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30956 +       return si_noflush_write_trylock(sb);
30957 +}
30958 +#endif
30959 +
30960 +static inline void si_write_unlock(struct super_block *sb)
30961 +{
30962 +       si_pid_clr(sb);
30963 +       __si_write_unlock(sb);
30964 +}
30965 +
30966 +#if 0 /* reserved */
30967 +static inline void si_downgrade_lock(struct super_block *sb)
30968 +{
30969 +       __si_downgrade_lock(sb);
30970 +}
30971 +#endif
30972 +
30973 +/* ---------------------------------------------------------------------- */
30974 +
30975 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30976 +{
30977 +       SiMustAnyLock(sb);
30978 +       return au_sbi(sb)->si_bbot;
30979 +}
30980 +
30981 +static inline unsigned int au_mntflags(struct super_block *sb)
30982 +{
30983 +       SiMustAnyLock(sb);
30984 +       return au_sbi(sb)->si_mntflags;
30985 +}
30986 +
30987 +static inline unsigned int au_sigen(struct super_block *sb)
30988 +{
30989 +       SiMustAnyLock(sb);
30990 +       return au_sbi(sb)->si_generation;
30991 +}
30992 +
30993 +static inline struct au_branch *au_sbr(struct super_block *sb,
30994 +                                      aufs_bindex_t bindex)
30995 +{
30996 +       SiMustAnyLock(sb);
30997 +       return au_sbi(sb)->si_branch[0 + bindex];
30998 +}
30999 +
31000 +static inline loff_t au_xi_maxent(struct super_block *sb)
31001 +{
31002 +       SiMustAnyLock(sb);
31003 +       return au_sbi(sb)->si_ximaxent;
31004 +}
31005 +
31006 +#endif /* __KERNEL__ */
31007 +#endif /* __AUFS_SUPER_H__ */
31008 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31009 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31010 +++ linux/fs/aufs/sysaufs.c     2021-11-01 23:48:34.213025928 +0100
31011 @@ -0,0 +1,93 @@
31012 +// SPDX-License-Identifier: GPL-2.0
31013 +/*
31014 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31015 + *
31016 + * This program, aufs is free software; you can redistribute it and/or modify
31017 + * it under the terms of the GNU General Public License as published by
31018 + * the Free Software Foundation; either version 2 of the License, or
31019 + * (at your option) any later version.
31020 + *
31021 + * This program is distributed in the hope that it will be useful,
31022 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31023 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31024 + * GNU General Public License for more details.
31025 + *
31026 + * You should have received a copy of the GNU General Public License
31027 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31028 + */
31029 +
31030 +/*
31031 + * sysfs interface and lifetime management
31032 + * they are necessary regardless sysfs is disabled.
31033 + */
31034 +
31035 +#include <linux/random.h>
31036 +#include "aufs.h"
31037 +
31038 +unsigned long sysaufs_si_mask;
31039 +struct kset *sysaufs_kset;
31040 +
31041 +#define AuSiAttr(_name) { \
31042 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31043 +       .show   = sysaufs_si_##_name,                           \
31044 +}
31045 +
31046 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31047 +struct attribute *sysaufs_si_attrs[] = {
31048 +       &sysaufs_si_attr_xi_path.attr,
31049 +       NULL,
31050 +};
31051 +
31052 +static const struct sysfs_ops au_sbi_ops = {
31053 +       .show   = sysaufs_si_show
31054 +};
31055 +
31056 +static struct kobj_type au_sbi_ktype = {
31057 +       .release        = au_si_free,
31058 +       .sysfs_ops      = &au_sbi_ops,
31059 +       .default_attrs  = sysaufs_si_attrs
31060 +};
31061 +
31062 +/* ---------------------------------------------------------------------- */
31063 +
31064 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31065 +{
31066 +       int err;
31067 +
31068 +       sbinfo->si_kobj.kset = sysaufs_kset;
31069 +       /* cf. sysaufs_name() */
31070 +       err = kobject_init_and_add
31071 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31072 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31073 +
31074 +       return err;
31075 +}
31076 +
31077 +void sysaufs_fin(void)
31078 +{
31079 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31080 +       kset_unregister(sysaufs_kset);
31081 +}
31082 +
31083 +int __init sysaufs_init(void)
31084 +{
31085 +       int err;
31086 +
31087 +       do {
31088 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31089 +       } while (!sysaufs_si_mask);
31090 +
31091 +       err = -EINVAL;
31092 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31093 +       if (unlikely(!sysaufs_kset))
31094 +               goto out;
31095 +       err = PTR_ERR(sysaufs_kset);
31096 +       if (IS_ERR(sysaufs_kset))
31097 +               goto out;
31098 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31099 +       if (unlikely(err))
31100 +               kset_unregister(sysaufs_kset);
31101 +
31102 +out:
31103 +       return err;
31104 +}
31105 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31106 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31107 +++ linux/fs/aufs/sysaufs.h     2021-11-01 23:48:34.213025928 +0100
31108 @@ -0,0 +1,102 @@
31109 +/* SPDX-License-Identifier: GPL-2.0 */
31110 +/*
31111 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31112 + *
31113 + * This program, aufs is free software; you can redistribute it and/or modify
31114 + * it under the terms of the GNU General Public License as published by
31115 + * the Free Software Foundation; either version 2 of the License, or
31116 + * (at your option) any later version.
31117 + *
31118 + * This program is distributed in the hope that it will be useful,
31119 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31120 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31121 + * GNU General Public License for more details.
31122 + *
31123 + * You should have received a copy of the GNU General Public License
31124 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31125 + */
31126 +
31127 +/*
31128 + * sysfs interface and mount lifetime management
31129 + */
31130 +
31131 +#ifndef __SYSAUFS_H__
31132 +#define __SYSAUFS_H__
31133 +
31134 +#ifdef __KERNEL__
31135 +
31136 +#include <linux/sysfs.h>
31137 +#include "module.h"
31138 +
31139 +struct super_block;
31140 +struct au_sbinfo;
31141 +
31142 +struct sysaufs_si_attr {
31143 +       struct attribute attr;
31144 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31145 +};
31146 +
31147 +/* ---------------------------------------------------------------------- */
31148 +
31149 +/* sysaufs.c */
31150 +extern unsigned long sysaufs_si_mask;
31151 +extern struct kset *sysaufs_kset;
31152 +extern struct attribute *sysaufs_si_attrs[];
31153 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31154 +int __init sysaufs_init(void);
31155 +void sysaufs_fin(void);
31156 +
31157 +/* ---------------------------------------------------------------------- */
31158 +
31159 +/* some people doesn't like to show a pointer in kernel */
31160 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31161 +{
31162 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31163 +}
31164 +
31165 +#define SysaufsSiNamePrefix    "si_"
31166 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31167 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31168 +{
31169 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31170 +                sysaufs_si_id(sbinfo));
31171 +}
31172 +
31173 +struct au_branch;
31174 +#ifdef CONFIG_SYSFS
31175 +/* sysfs.c */
31176 +extern struct attribute_group *sysaufs_attr_group;
31177 +
31178 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31179 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31180 +                        char *buf);
31181 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31182 +#ifdef CONFIG_COMPAT
31183 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31184 +#endif
31185 +
31186 +void sysaufs_br_init(struct au_branch *br);
31187 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31188 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31189 +
31190 +#define sysaufs_brs_init()     do {} while (0)
31191 +
31192 +#else
31193 +#define sysaufs_attr_group     NULL
31194 +
31195 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31196 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31197 +       struct attribute *attr, char *buf)
31198 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31199 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31200 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31201 +
31202 +static inline void sysaufs_brs_init(void)
31203 +{
31204 +       sysaufs_brs = 0;
31205 +}
31206 +
31207 +#endif /* CONFIG_SYSFS */
31208 +
31209 +#endif /* __KERNEL__ */
31210 +#endif /* __SYSAUFS_H__ */
31211 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31212 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31213 +++ linux/fs/aufs/sysfs.c       2021-11-01 23:48:34.213025928 +0100
31214 @@ -0,0 +1,374 @@
31215 +// SPDX-License-Identifier: GPL-2.0
31216 +/*
31217 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31218 + *
31219 + * This program, aufs is free software; you can redistribute it and/or modify
31220 + * it under the terms of the GNU General Public License as published by
31221 + * the Free Software Foundation; either version 2 of the License, or
31222 + * (at your option) any later version.
31223 + *
31224 + * This program is distributed in the hope that it will be useful,
31225 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31226 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31227 + * GNU General Public License for more details.
31228 + *
31229 + * You should have received a copy of the GNU General Public License
31230 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31231 + */
31232 +
31233 +/*
31234 + * sysfs interface
31235 + */
31236 +
31237 +#include <linux/compat.h>
31238 +#include <linux/seq_file.h>
31239 +#include "aufs.h"
31240 +
31241 +#ifdef CONFIG_AUFS_FS_MODULE
31242 +/* this entry violates the "one line per file" policy of sysfs */
31243 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31244 +                          char *buf)
31245 +{
31246 +       ssize_t err;
31247 +       static char *conf =
31248 +/* this file is generated at compiling */
31249 +#include "conf.str"
31250 +               ;
31251 +
31252 +       err = snprintf(buf, PAGE_SIZE, conf);
31253 +       if (unlikely(err >= PAGE_SIZE))
31254 +               err = -EFBIG;
31255 +       return err;
31256 +}
31257 +
31258 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31259 +#endif
31260 +
31261 +static struct attribute *au_attr[] = {
31262 +#ifdef CONFIG_AUFS_FS_MODULE
31263 +       &au_config_attr.attr,
31264 +#endif
31265 +       NULL,   /* need to NULL terminate the list of attributes */
31266 +};
31267 +
31268 +static struct attribute_group sysaufs_attr_group_body = {
31269 +       .attrs = au_attr
31270 +};
31271 +
31272 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31273 +
31274 +/* ---------------------------------------------------------------------- */
31275 +
31276 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31277 +{
31278 +       int err;
31279 +
31280 +       SiMustAnyLock(sb);
31281 +
31282 +       err = 0;
31283 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31284 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31285 +               seq_putc(seq, '\n');
31286 +       }
31287 +       return err;
31288 +}
31289 +
31290 +/*
31291 + * the lifetime of branch is independent from the entry under sysfs.
31292 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31293 + * unlinked.
31294 + */
31295 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31296 +                        aufs_bindex_t bindex, int idx)
31297 +{
31298 +       int err;
31299 +       struct path path;
31300 +       struct dentry *root;
31301 +       struct au_branch *br;
31302 +       au_br_perm_str_t perm;
31303 +
31304 +       AuDbg("b%d\n", bindex);
31305 +
31306 +       err = 0;
31307 +       root = sb->s_root;
31308 +       di_read_lock_parent(root, !AuLock_IR);
31309 +       br = au_sbr(sb, bindex);
31310 +
31311 +       switch (idx) {
31312 +       case AuBrSysfs_BR:
31313 +               path.mnt = au_br_mnt(br);
31314 +               path.dentry = au_h_dptr(root, bindex);
31315 +               err = au_seq_path(seq, &path);
31316 +               if (!err) {
31317 +                       au_optstr_br_perm(&perm, br->br_perm);
31318 +                       seq_printf(seq, "=%s\n", perm.a);
31319 +               }
31320 +               break;
31321 +       case AuBrSysfs_BRID:
31322 +               seq_printf(seq, "%d\n", br->br_id);
31323 +               break;
31324 +       }
31325 +       di_read_unlock(root, !AuLock_IR);
31326 +       if (unlikely(err || seq_has_overflowed(seq)))
31327 +               err = -E2BIG;
31328 +
31329 +       return err;
31330 +}
31331 +
31332 +/* ---------------------------------------------------------------------- */
31333 +
31334 +static struct seq_file *au_seq(char *p, ssize_t len)
31335 +{
31336 +       struct seq_file *seq;
31337 +
31338 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31339 +       if (seq) {
31340 +               /* mutex_init(&seq.lock); */
31341 +               seq->buf = p;
31342 +               seq->size = len;
31343 +               return seq; /* success */
31344 +       }
31345 +
31346 +       seq = ERR_PTR(-ENOMEM);
31347 +       return seq;
31348 +}
31349 +
31350 +#define SysaufsBr_PREFIX       "br"
31351 +#define SysaufsBrid_PREFIX     "brid"
31352 +
31353 +/* todo: file size may exceed PAGE_SIZE */
31354 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31355 +                       char *buf)
31356 +{
31357 +       ssize_t err;
31358 +       int idx;
31359 +       long l;
31360 +       aufs_bindex_t bbot;
31361 +       struct au_sbinfo *sbinfo;
31362 +       struct super_block *sb;
31363 +       struct seq_file *seq;
31364 +       char *name;
31365 +       struct attribute **cattr;
31366 +
31367 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31368 +       sb = sbinfo->si_sb;
31369 +
31370 +       /*
31371 +        * prevent a race condition between sysfs and aufs.
31372 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31373 +        * prohibits maintaining the sysfs entries.
31374 +        * hew we acquire read lock after sysfs_get_active_two().
31375 +        * on the other hand, the remount process may maintain the sysfs/aufs
31376 +        * entries after acquiring write lock.
31377 +        * it can cause a deadlock.
31378 +        * simply we gave up processing read here.
31379 +        */
31380 +       err = -EBUSY;
31381 +       if (unlikely(!si_noflush_read_trylock(sb)))
31382 +               goto out;
31383 +
31384 +       seq = au_seq(buf, PAGE_SIZE);
31385 +       err = PTR_ERR(seq);
31386 +       if (IS_ERR(seq))
31387 +               goto out_unlock;
31388 +
31389 +       name = (void *)attr->name;
31390 +       cattr = sysaufs_si_attrs;
31391 +       while (*cattr) {
31392 +               if (!strcmp(name, (*cattr)->name)) {
31393 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31394 +                               ->show(seq, sb);
31395 +                       goto out_seq;
31396 +               }
31397 +               cattr++;
31398 +       }
31399 +
31400 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31401 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31402 +               idx = AuBrSysfs_BRID;
31403 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31404 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31405 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31406 +               idx = AuBrSysfs_BR;
31407 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31408 +       } else
31409 +                 BUG();
31410 +
31411 +       err = kstrtol(name, 10, &l);
31412 +       if (!err) {
31413 +               bbot = au_sbbot(sb);
31414 +               if (l <= bbot)
31415 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31416 +               else
31417 +                       err = -ENOENT;
31418 +       }
31419 +
31420 +out_seq:
31421 +       if (!err) {
31422 +               err = seq->count;
31423 +               /* sysfs limit */
31424 +               if (unlikely(err == PAGE_SIZE))
31425 +                       err = -EFBIG;
31426 +       }
31427 +       au_kfree_rcu(seq);
31428 +out_unlock:
31429 +       si_read_unlock(sb);
31430 +out:
31431 +       return err;
31432 +}
31433 +
31434 +/* ---------------------------------------------------------------------- */
31435 +
31436 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31437 +{
31438 +       int err;
31439 +       int16_t brid;
31440 +       aufs_bindex_t bindex, bbot;
31441 +       size_t sz;
31442 +       char *buf;
31443 +       struct seq_file *seq;
31444 +       struct au_branch *br;
31445 +
31446 +       si_read_lock(sb, AuLock_FLUSH);
31447 +       bbot = au_sbbot(sb);
31448 +       err = bbot + 1;
31449 +       if (!arg)
31450 +               goto out;
31451 +
31452 +       err = -ENOMEM;
31453 +       buf = (void *)__get_free_page(GFP_NOFS);
31454 +       if (unlikely(!buf))
31455 +               goto out;
31456 +
31457 +       seq = au_seq(buf, PAGE_SIZE);
31458 +       err = PTR_ERR(seq);
31459 +       if (IS_ERR(seq))
31460 +               goto out_buf;
31461 +
31462 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31463 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31464 +               /* VERIFY_WRITE */
31465 +               err = !access_ok(arg, sizeof(*arg));
31466 +               if (unlikely(err))
31467 +                       break;
31468 +
31469 +               br = au_sbr(sb, bindex);
31470 +               brid = br->br_id;
31471 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31472 +               err = __put_user(brid, &arg->id);
31473 +               if (unlikely(err))
31474 +                       break;
31475 +
31476 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31477 +               err = __put_user(br->br_perm, &arg->perm);
31478 +               if (unlikely(err))
31479 +                       break;
31480 +
31481 +               err = au_seq_path(seq, &br->br_path);
31482 +               if (unlikely(err))
31483 +                       break;
31484 +               seq_putc(seq, '\0');
31485 +               if (!seq_has_overflowed(seq)) {
31486 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31487 +                       seq->count = 0;
31488 +                       if (unlikely(err))
31489 +                               break;
31490 +               } else {
31491 +                       err = -E2BIG;
31492 +                       goto out_seq;
31493 +               }
31494 +       }
31495 +       if (unlikely(err))
31496 +               err = -EFAULT;
31497 +
31498 +out_seq:
31499 +       au_kfree_rcu(seq);
31500 +out_buf:
31501 +       free_page((unsigned long)buf);
31502 +out:
31503 +       si_read_unlock(sb);
31504 +       return err;
31505 +}
31506 +
31507 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31508 +{
31509 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31510 +}
31511 +
31512 +#ifdef CONFIG_COMPAT
31513 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31514 +{
31515 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31516 +}
31517 +#endif
31518 +
31519 +/* ---------------------------------------------------------------------- */
31520 +
31521 +void sysaufs_br_init(struct au_branch *br)
31522 +{
31523 +       int i;
31524 +       struct au_brsysfs *br_sysfs;
31525 +       struct attribute *attr;
31526 +
31527 +       br_sysfs = br->br_sysfs;
31528 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31529 +               attr = &br_sysfs->attr;
31530 +               sysfs_attr_init(attr);
31531 +               attr->name = br_sysfs->name;
31532 +               attr->mode = 0444;
31533 +               br_sysfs++;
31534 +       }
31535 +}
31536 +
31537 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31538 +{
31539 +       struct au_branch *br;
31540 +       struct kobject *kobj;
31541 +       struct au_brsysfs *br_sysfs;
31542 +       int i;
31543 +       aufs_bindex_t bbot;
31544 +
31545 +       if (!sysaufs_brs)
31546 +               return;
31547 +
31548 +       kobj = &au_sbi(sb)->si_kobj;
31549 +       bbot = au_sbbot(sb);
31550 +       for (; bindex <= bbot; bindex++) {
31551 +               br = au_sbr(sb, bindex);
31552 +               br_sysfs = br->br_sysfs;
31553 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31554 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31555 +                       br_sysfs++;
31556 +               }
31557 +       }
31558 +}
31559 +
31560 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31561 +{
31562 +       int err, i;
31563 +       aufs_bindex_t bbot;
31564 +       struct kobject *kobj;
31565 +       struct au_branch *br;
31566 +       struct au_brsysfs *br_sysfs;
31567 +
31568 +       if (!sysaufs_brs)
31569 +               return;
31570 +
31571 +       kobj = &au_sbi(sb)->si_kobj;
31572 +       bbot = au_sbbot(sb);
31573 +       for (; bindex <= bbot; bindex++) {
31574 +               br = au_sbr(sb, bindex);
31575 +               br_sysfs = br->br_sysfs;
31576 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31577 +                        SysaufsBr_PREFIX "%d", bindex);
31578 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31579 +                        SysaufsBrid_PREFIX "%d", bindex);
31580 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31581 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31582 +                       if (unlikely(err))
31583 +                               pr_warn("failed %s under sysfs(%d)\n",
31584 +                                       br_sysfs->name, err);
31585 +                       br_sysfs++;
31586 +               }
31587 +       }
31588 +}
31589 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31590 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31591 +++ linux/fs/aufs/sysrq.c       2021-11-01 23:48:34.213025928 +0100
31592 @@ -0,0 +1,149 @@
31593 +// SPDX-License-Identifier: GPL-2.0
31594 +/*
31595 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31596 + *
31597 + * This program, aufs is free software; you can redistribute it and/or modify
31598 + * it under the terms of the GNU General Public License as published by
31599 + * the Free Software Foundation; either version 2 of the License, or
31600 + * (at your option) any later version.
31601 + *
31602 + * This program is distributed in the hope that it will be useful,
31603 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31604 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31605 + * GNU General Public License for more details.
31606 + *
31607 + * You should have received a copy of the GNU General Public License
31608 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31609 + */
31610 +
31611 +/*
31612 + * magic sysrq handler
31613 + */
31614 +
31615 +/* #include <linux/sysrq.h> */
31616 +#include <linux/writeback.h>
31617 +#include "aufs.h"
31618 +
31619 +/* ---------------------------------------------------------------------- */
31620 +
31621 +static void sysrq_sb(struct super_block *sb)
31622 +{
31623 +       char *plevel;
31624 +       struct au_sbinfo *sbinfo;
31625 +       struct file *file;
31626 +       struct hlist_bl_head *files;
31627 +       struct hlist_bl_node *pos;
31628 +       struct au_finfo *finfo;
31629 +       struct inode *i;
31630 +
31631 +       plevel = au_plevel;
31632 +       au_plevel = KERN_WARNING;
31633 +
31634 +       /* since we define pr_fmt, call printk directly */
31635 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31636 +
31637 +       sbinfo = au_sbi(sb);
31638 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31639 +       pr("superblock\n");
31640 +       au_dpri_sb(sb);
31641 +
31642 +#if 0 /* reserved */
31643 +       do {
31644 +               int err, i, j, ndentry;
31645 +               struct au_dcsub_pages dpages;
31646 +               struct au_dpage *dpage;
31647 +
31648 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31649 +               if (unlikely(err))
31650 +                       break;
31651 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31652 +               if (!err)
31653 +                       for (i = 0; i < dpages.ndpage; i++) {
31654 +                               dpage = dpages.dpages + i;
31655 +                               ndentry = dpage->ndentry;
31656 +                               for (j = 0; j < ndentry; j++)
31657 +                                       au_dpri_dentry(dpage->dentries[j]);
31658 +                       }
31659 +               au_dpages_free(&dpages);
31660 +       } while (0);
31661 +#endif
31662 +
31663 +       pr("isolated inode\n");
31664 +       spin_lock(&sb->s_inode_list_lock);
31665 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31666 +               spin_lock(&i->i_lock);
31667 +               if (hlist_empty(&i->i_dentry))
31668 +                       au_dpri_inode(i);
31669 +               spin_unlock(&i->i_lock);
31670 +       }
31671 +       spin_unlock(&sb->s_inode_list_lock);
31672 +
31673 +       pr("files\n");
31674 +       files = &au_sbi(sb)->si_files;
31675 +       hlist_bl_lock(files);
31676 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31677 +               umode_t mode;
31678 +
31679 +               file = finfo->fi_file;
31680 +               mode = file_inode(file)->i_mode;
31681 +               if (!special_file(mode))
31682 +                       au_dpri_file(file);
31683 +       }
31684 +       hlist_bl_unlock(files);
31685 +       pr("done\n");
31686 +
31687 +#undef pr
31688 +       au_plevel = plevel;
31689 +}
31690 +
31691 +/* ---------------------------------------------------------------------- */
31692 +
31693 +/* module parameter */
31694 +static char *aufs_sysrq_key = "a";
31695 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31696 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31697 +
31698 +static void au_sysrq(int key __maybe_unused)
31699 +{
31700 +       struct au_sbinfo *sbinfo;
31701 +       struct hlist_bl_node *pos;
31702 +
31703 +       lockdep_off();
31704 +       au_sbilist_lock();
31705 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31706 +               sysrq_sb(sbinfo->si_sb);
31707 +       au_sbilist_unlock();
31708 +       lockdep_on();
31709 +}
31710 +
31711 +static struct sysrq_key_op au_sysrq_op = {
31712 +       .handler        = au_sysrq,
31713 +       .help_msg       = "Aufs",
31714 +       .action_msg     = "Aufs",
31715 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31716 +};
31717 +
31718 +/* ---------------------------------------------------------------------- */
31719 +
31720 +int __init au_sysrq_init(void)
31721 +{
31722 +       int err;
31723 +       char key;
31724 +
31725 +       err = -1;
31726 +       key = *aufs_sysrq_key;
31727 +       if ('a' <= key && key <= 'z')
31728 +               err = register_sysrq_key(key, &au_sysrq_op);
31729 +       if (unlikely(err))
31730 +               pr_err("err %d, sysrq=%c\n", err, key);
31731 +       return err;
31732 +}
31733 +
31734 +void au_sysrq_fin(void)
31735 +{
31736 +       int err;
31737 +
31738 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31739 +       if (unlikely(err))
31740 +               pr_err("err %d (ignored)\n", err);
31741 +}
31742 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31743 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31744 +++ linux/fs/aufs/vdir.c        2021-11-01 23:48:34.213025928 +0100
31745 @@ -0,0 +1,896 @@
31746 +// SPDX-License-Identifier: GPL-2.0
31747 +/*
31748 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31749 + *
31750 + * This program, aufs is free software; you can redistribute it and/or modify
31751 + * it under the terms of the GNU General Public License as published by
31752 + * the Free Software Foundation; either version 2 of the License, or
31753 + * (at your option) any later version.
31754 + *
31755 + * This program is distributed in the hope that it will be useful,
31756 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31757 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31758 + * GNU General Public License for more details.
31759 + *
31760 + * You should have received a copy of the GNU General Public License
31761 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31762 + */
31763 +
31764 +/*
31765 + * virtual or vertical directory
31766 + */
31767 +
31768 +#include <linux/iversion.h>
31769 +#include "aufs.h"
31770 +
31771 +static unsigned int calc_size(int nlen)
31772 +{
31773 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31774 +}
31775 +
31776 +static int set_deblk_end(union au_vdir_deblk_p *p,
31777 +                        union au_vdir_deblk_p *deblk_end)
31778 +{
31779 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31780 +               p->de->de_str.len = 0;
31781 +               /* smp_mb(); */
31782 +               return 0;
31783 +       }
31784 +       return -1; /* error */
31785 +}
31786 +
31787 +/* returns true or false */
31788 +static int is_deblk_end(union au_vdir_deblk_p *p,
31789 +                       union au_vdir_deblk_p *deblk_end)
31790 +{
31791 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31792 +               return !p->de->de_str.len;
31793 +       return 1;
31794 +}
31795 +
31796 +static unsigned char *last_deblk(struct au_vdir *vdir)
31797 +{
31798 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31799 +}
31800 +
31801 +/* ---------------------------------------------------------------------- */
31802 +
31803 +/* estimate the appropriate size for name hash table */
31804 +unsigned int au_rdhash_est(loff_t sz)
31805 +{
31806 +       unsigned int n;
31807 +
31808 +       n = UINT_MAX;
31809 +       sz >>= 10;
31810 +       if (sz < n)
31811 +               n = sz;
31812 +       if (sz < AUFS_RDHASH_DEF)
31813 +               n = AUFS_RDHASH_DEF;
31814 +       /* pr_info("n %u\n", n); */
31815 +       return n;
31816 +}
31817 +
31818 +/*
31819 + * the allocated memory has to be freed by
31820 + * au_nhash_wh_free() or au_nhash_de_free().
31821 + */
31822 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31823 +{
31824 +       struct hlist_head *head;
31825 +       unsigned int u;
31826 +       size_t sz;
31827 +
31828 +       sz = sizeof(*nhash->nh_head) * num_hash;
31829 +       head = kmalloc(sz, gfp);
31830 +       if (head) {
31831 +               nhash->nh_num = num_hash;
31832 +               nhash->nh_head = head;
31833 +               for (u = 0; u < num_hash; u++)
31834 +                       INIT_HLIST_HEAD(head++);
31835 +               return 0; /* success */
31836 +       }
31837 +
31838 +       return -ENOMEM;
31839 +}
31840 +
31841 +static void nhash_count(struct hlist_head *head)
31842 +{
31843 +#if 0 /* debugging */
31844 +       unsigned long n;
31845 +       struct hlist_node *pos;
31846 +
31847 +       n = 0;
31848 +       hlist_for_each(pos, head)
31849 +               n++;
31850 +       pr_info("%lu\n", n);
31851 +#endif
31852 +}
31853 +
31854 +static void au_nhash_wh_do_free(struct hlist_head *head)
31855 +{
31856 +       struct au_vdir_wh *pos;
31857 +       struct hlist_node *node;
31858 +
31859 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31860 +               au_kfree_rcu(pos);
31861 +}
31862 +
31863 +static void au_nhash_de_do_free(struct hlist_head *head)
31864 +{
31865 +       struct au_vdir_dehstr *pos;
31866 +       struct hlist_node *node;
31867 +
31868 +       hlist_for_each_entry_safe(pos, node, head, hash)
31869 +               au_cache_free_vdir_dehstr(pos);
31870 +}
31871 +
31872 +static void au_nhash_do_free(struct au_nhash *nhash,
31873 +                            void (*free)(struct hlist_head *head))
31874 +{
31875 +       unsigned int n;
31876 +       struct hlist_head *head;
31877 +
31878 +       n = nhash->nh_num;
31879 +       if (!n)
31880 +               return;
31881 +
31882 +       head = nhash->nh_head;
31883 +       while (n-- > 0) {
31884 +               nhash_count(head);
31885 +               free(head++);
31886 +       }
31887 +       au_kfree_try_rcu(nhash->nh_head);
31888 +}
31889 +
31890 +void au_nhash_wh_free(struct au_nhash *whlist)
31891 +{
31892 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31893 +}
31894 +
31895 +static void au_nhash_de_free(struct au_nhash *delist)
31896 +{
31897 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31898 +}
31899 +
31900 +/* ---------------------------------------------------------------------- */
31901 +
31902 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31903 +                           int limit)
31904 +{
31905 +       int num;
31906 +       unsigned int u, n;
31907 +       struct hlist_head *head;
31908 +       struct au_vdir_wh *pos;
31909 +
31910 +       num = 0;
31911 +       n = whlist->nh_num;
31912 +       head = whlist->nh_head;
31913 +       for (u = 0; u < n; u++, head++)
31914 +               hlist_for_each_entry(pos, head, wh_hash)
31915 +                       if (pos->wh_bindex == btgt && ++num > limit)
31916 +                               return 1;
31917 +       return 0;
31918 +}
31919 +
31920 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31921 +                                      unsigned char *name,
31922 +                                      unsigned int len)
31923 +{
31924 +       unsigned int v;
31925 +       /* const unsigned int magic_bit = 12; */
31926 +
31927 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31928 +
31929 +       v = 0;
31930 +       if (len > 8)
31931 +               len = 8;
31932 +       while (len--)
31933 +               v += *name++;
31934 +       /* v = hash_long(v, magic_bit); */
31935 +       v %= nhash->nh_num;
31936 +       return nhash->nh_head + v;
31937 +}
31938 +
31939 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31940 +                             int nlen)
31941 +{
31942 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31943 +}
31944 +
31945 +/* returns found or not */
31946 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31947 +{
31948 +       struct hlist_head *head;
31949 +       struct au_vdir_wh *pos;
31950 +       struct au_vdir_destr *str;
31951 +
31952 +       head = au_name_hash(whlist, name, nlen);
31953 +       hlist_for_each_entry(pos, head, wh_hash) {
31954 +               str = &pos->wh_str;
31955 +               AuDbg("%.*s\n", str->len, str->name);
31956 +               if (au_nhash_test_name(str, name, nlen))
31957 +                       return 1;
31958 +       }
31959 +       return 0;
31960 +}
31961 +
31962 +/* returns found(true) or not */
31963 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31964 +{
31965 +       struct hlist_head *head;
31966 +       struct au_vdir_dehstr *pos;
31967 +       struct au_vdir_destr *str;
31968 +
31969 +       head = au_name_hash(delist, name, nlen);
31970 +       hlist_for_each_entry(pos, head, hash) {
31971 +               str = pos->str;
31972 +               AuDbg("%.*s\n", str->len, str->name);
31973 +               if (au_nhash_test_name(str, name, nlen))
31974 +                       return 1;
31975 +       }
31976 +       return 0;
31977 +}
31978 +
31979 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31980 +                           unsigned char d_type)
31981 +{
31982 +#ifdef CONFIG_AUFS_SHWH
31983 +       wh->wh_ino = ino;
31984 +       wh->wh_type = d_type;
31985 +#endif
31986 +}
31987 +
31988 +/* ---------------------------------------------------------------------- */
31989 +
31990 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31991 +                      unsigned int d_type, aufs_bindex_t bindex,
31992 +                      unsigned char shwh)
31993 +{
31994 +       int err;
31995 +       struct au_vdir_destr *str;
31996 +       struct au_vdir_wh *wh;
31997 +
31998 +       AuDbg("%.*s\n", nlen, name);
31999 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32000 +
32001 +       err = -ENOMEM;
32002 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32003 +       if (unlikely(!wh))
32004 +               goto out;
32005 +
32006 +       err = 0;
32007 +       wh->wh_bindex = bindex;
32008 +       if (shwh)
32009 +               au_shwh_init_wh(wh, ino, d_type);
32010 +       str = &wh->wh_str;
32011 +       str->len = nlen;
32012 +       memcpy(str->name, name, nlen);
32013 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32014 +       /* smp_mb(); */
32015 +
32016 +out:
32017 +       return err;
32018 +}
32019 +
32020 +static int append_deblk(struct au_vdir *vdir)
32021 +{
32022 +       int err;
32023 +       unsigned long ul;
32024 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32025 +       union au_vdir_deblk_p p, deblk_end;
32026 +       unsigned char **o;
32027 +
32028 +       err = -ENOMEM;
32029 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32030 +                       GFP_NOFS, /*may_shrink*/0);
32031 +       if (unlikely(!o))
32032 +               goto out;
32033 +
32034 +       vdir->vd_deblk = o;
32035 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32036 +       if (p.deblk) {
32037 +               ul = vdir->vd_nblk++;
32038 +               vdir->vd_deblk[ul] = p.deblk;
32039 +               vdir->vd_last.ul = ul;
32040 +               vdir->vd_last.p.deblk = p.deblk;
32041 +               deblk_end.deblk = p.deblk + deblk_sz;
32042 +               err = set_deblk_end(&p, &deblk_end);
32043 +       }
32044 +
32045 +out:
32046 +       return err;
32047 +}
32048 +
32049 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32050 +                    unsigned int d_type, struct au_nhash *delist)
32051 +{
32052 +       int err;
32053 +       unsigned int sz;
32054 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32055 +       union au_vdir_deblk_p p, *room, deblk_end;
32056 +       struct au_vdir_dehstr *dehstr;
32057 +
32058 +       p.deblk = last_deblk(vdir);
32059 +       deblk_end.deblk = p.deblk + deblk_sz;
32060 +       room = &vdir->vd_last.p;
32061 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32062 +                 || !is_deblk_end(room, &deblk_end));
32063 +
32064 +       sz = calc_size(nlen);
32065 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32066 +               err = append_deblk(vdir);
32067 +               if (unlikely(err))
32068 +                       goto out;
32069 +
32070 +               p.deblk = last_deblk(vdir);
32071 +               deblk_end.deblk = p.deblk + deblk_sz;
32072 +               /* smp_mb(); */
32073 +               AuDebugOn(room->deblk != p.deblk);
32074 +       }
32075 +
32076 +       err = -ENOMEM;
32077 +       dehstr = au_cache_alloc_vdir_dehstr();
32078 +       if (unlikely(!dehstr))
32079 +               goto out;
32080 +
32081 +       dehstr->str = &room->de->de_str;
32082 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32083 +       room->de->de_ino = ino;
32084 +       room->de->de_type = d_type;
32085 +       room->de->de_str.len = nlen;
32086 +       memcpy(room->de->de_str.name, name, nlen);
32087 +
32088 +       err = 0;
32089 +       room->deblk += sz;
32090 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32091 +               err = append_deblk(vdir);
32092 +       /* smp_mb(); */
32093 +
32094 +out:
32095 +       return err;
32096 +}
32097 +
32098 +/* ---------------------------------------------------------------------- */
32099 +
32100 +void au_vdir_free(struct au_vdir *vdir)
32101 +{
32102 +       unsigned char **deblk;
32103 +
32104 +       deblk = vdir->vd_deblk;
32105 +       while (vdir->vd_nblk--)
32106 +               au_kfree_try_rcu(*deblk++);
32107 +       au_kfree_try_rcu(vdir->vd_deblk);
32108 +       au_cache_free_vdir(vdir);
32109 +}
32110 +
32111 +static struct au_vdir *alloc_vdir(struct file *file)
32112 +{
32113 +       struct au_vdir *vdir;
32114 +       struct super_block *sb;
32115 +       int err;
32116 +
32117 +       sb = file->f_path.dentry->d_sb;
32118 +       SiMustAnyLock(sb);
32119 +
32120 +       err = -ENOMEM;
32121 +       vdir = au_cache_alloc_vdir();
32122 +       if (unlikely(!vdir))
32123 +               goto out;
32124 +
32125 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32126 +       if (unlikely(!vdir->vd_deblk))
32127 +               goto out_free;
32128 +
32129 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32130 +       if (!vdir->vd_deblk_sz) {
32131 +               /* estimate the appropriate size for deblk */
32132 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32133 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32134 +       }
32135 +       vdir->vd_nblk = 0;
32136 +       vdir->vd_version = 0;
32137 +       vdir->vd_jiffy = 0;
32138 +       err = append_deblk(vdir);
32139 +       if (!err)
32140 +               return vdir; /* success */
32141 +
32142 +       au_kfree_try_rcu(vdir->vd_deblk);
32143 +
32144 +out_free:
32145 +       au_cache_free_vdir(vdir);
32146 +out:
32147 +       vdir = ERR_PTR(err);
32148 +       return vdir;
32149 +}
32150 +
32151 +static int reinit_vdir(struct au_vdir *vdir)
32152 +{
32153 +       int err;
32154 +       union au_vdir_deblk_p p, deblk_end;
32155 +
32156 +       while (vdir->vd_nblk > 1) {
32157 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32158 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32159 +               vdir->vd_nblk--;
32160 +       }
32161 +       p.deblk = vdir->vd_deblk[0];
32162 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32163 +       err = set_deblk_end(&p, &deblk_end);
32164 +       /* keep vd_dblk_sz */
32165 +       vdir->vd_last.ul = 0;
32166 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32167 +       vdir->vd_version = 0;
32168 +       vdir->vd_jiffy = 0;
32169 +       /* smp_mb(); */
32170 +       return err;
32171 +}
32172 +
32173 +/* ---------------------------------------------------------------------- */
32174 +
32175 +#define AuFillVdir_CALLED      1
32176 +#define AuFillVdir_WHABLE      (1 << 1)
32177 +#define AuFillVdir_SHWH                (1 << 2)
32178 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32179 +#define au_fset_fillvdir(flags, name) \
32180 +       do { (flags) |= AuFillVdir_##name; } while (0)
32181 +#define au_fclr_fillvdir(flags, name) \
32182 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32183 +
32184 +#ifndef CONFIG_AUFS_SHWH
32185 +#undef AuFillVdir_SHWH
32186 +#define AuFillVdir_SHWH                0
32187 +#endif
32188 +
32189 +struct fillvdir_arg {
32190 +       struct dir_context      ctx;
32191 +       struct file             *file;
32192 +       struct au_vdir          *vdir;
32193 +       struct au_nhash         delist;
32194 +       struct au_nhash         whlist;
32195 +       aufs_bindex_t           bindex;
32196 +       unsigned int            flags;
32197 +       int                     err;
32198 +};
32199 +
32200 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32201 +                   loff_t offset __maybe_unused, u64 h_ino,
32202 +                   unsigned int d_type)
32203 +{
32204 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32205 +       char *name = (void *)__name;
32206 +       struct super_block *sb;
32207 +       ino_t ino;
32208 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32209 +
32210 +       arg->err = 0;
32211 +       sb = arg->file->f_path.dentry->d_sb;
32212 +       au_fset_fillvdir(arg->flags, CALLED);
32213 +       /* smp_mb(); */
32214 +       if (nlen <= AUFS_WH_PFX_LEN
32215 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32216 +               if (test_known(&arg->delist, name, nlen)
32217 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32218 +                       goto out; /* already exists or whiteouted */
32219 +
32220 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32221 +               if (!arg->err) {
32222 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32223 +                               d_type = DT_UNKNOWN;
32224 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32225 +                                            d_type, &arg->delist);
32226 +               }
32227 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32228 +               name += AUFS_WH_PFX_LEN;
32229 +               nlen -= AUFS_WH_PFX_LEN;
32230 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32231 +                       goto out; /* already whiteouted */
32232 +
32233 +               ino = 0; /* just to suppress a warning */
32234 +               if (shwh)
32235 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32236 +                                            &ino);
32237 +               if (!arg->err) {
32238 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32239 +                               d_type = DT_UNKNOWN;
32240 +                       arg->err = au_nhash_append_wh
32241 +                               (&arg->whlist, name, nlen, ino, d_type,
32242 +                                arg->bindex, shwh);
32243 +               }
32244 +       }
32245 +
32246 +out:
32247 +       if (!arg->err)
32248 +               arg->vdir->vd_jiffy = jiffies;
32249 +       /* smp_mb(); */
32250 +       AuTraceErr(arg->err);
32251 +       return arg->err;
32252 +}
32253 +
32254 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32255 +                         struct au_nhash *whlist, struct au_nhash *delist)
32256 +{
32257 +#ifdef CONFIG_AUFS_SHWH
32258 +       int err;
32259 +       unsigned int nh, u;
32260 +       struct hlist_head *head;
32261 +       struct au_vdir_wh *pos;
32262 +       struct hlist_node *n;
32263 +       char *p, *o;
32264 +       struct au_vdir_destr *destr;
32265 +
32266 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32267 +
32268 +       err = -ENOMEM;
32269 +       o = p = (void *)__get_free_page(GFP_NOFS);
32270 +       if (unlikely(!p))
32271 +               goto out;
32272 +
32273 +       err = 0;
32274 +       nh = whlist->nh_num;
32275 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32276 +       p += AUFS_WH_PFX_LEN;
32277 +       for (u = 0; u < nh; u++) {
32278 +               head = whlist->nh_head + u;
32279 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32280 +                       destr = &pos->wh_str;
32281 +                       memcpy(p, destr->name, destr->len);
32282 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32283 +                                       pos->wh_ino, pos->wh_type, delist);
32284 +                       if (unlikely(err))
32285 +                               break;
32286 +               }
32287 +       }
32288 +
32289 +       free_page((unsigned long)o);
32290 +
32291 +out:
32292 +       AuTraceErr(err);
32293 +       return err;
32294 +#else
32295 +       return 0;
32296 +#endif
32297 +}
32298 +
32299 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32300 +{
32301 +       int err;
32302 +       unsigned int rdhash;
32303 +       loff_t offset;
32304 +       aufs_bindex_t bbot, bindex, btop;
32305 +       unsigned char shwh;
32306 +       struct file *hf, *file;
32307 +       struct super_block *sb;
32308 +
32309 +       file = arg->file;
32310 +       sb = file->f_path.dentry->d_sb;
32311 +       SiMustAnyLock(sb);
32312 +
32313 +       rdhash = au_sbi(sb)->si_rdhash;
32314 +       if (!rdhash)
32315 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32316 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32317 +       if (unlikely(err))
32318 +               goto out;
32319 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32320 +       if (unlikely(err))
32321 +               goto out_delist;
32322 +
32323 +       err = 0;
32324 +       arg->flags = 0;
32325 +       shwh = 0;
32326 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32327 +               shwh = 1;
32328 +               au_fset_fillvdir(arg->flags, SHWH);
32329 +       }
32330 +       btop = au_fbtop(file);
32331 +       bbot = au_fbbot_dir(file);
32332 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32333 +               hf = au_hf_dir(file, bindex);
32334 +               if (!hf)
32335 +                       continue;
32336 +
32337 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32338 +               err = offset;
32339 +               if (unlikely(offset))
32340 +                       break;
32341 +
32342 +               arg->bindex = bindex;
32343 +               au_fclr_fillvdir(arg->flags, WHABLE);
32344 +               if (shwh
32345 +                   || (bindex != bbot
32346 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32347 +                       au_fset_fillvdir(arg->flags, WHABLE);
32348 +               do {
32349 +                       arg->err = 0;
32350 +                       au_fclr_fillvdir(arg->flags, CALLED);
32351 +                       /* smp_mb(); */
32352 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32353 +                       if (err >= 0)
32354 +                               err = arg->err;
32355 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32356 +
32357 +               /*
32358 +                * dir_relax() may be good for concurrency, but aufs should not
32359 +                * use it since it will cause a lockdep problem.
32360 +                */
32361 +       }
32362 +
32363 +       if (!err && shwh)
32364 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32365 +
32366 +       au_nhash_wh_free(&arg->whlist);
32367 +
32368 +out_delist:
32369 +       au_nhash_de_free(&arg->delist);
32370 +out:
32371 +       return err;
32372 +}
32373 +
32374 +static int read_vdir(struct file *file, int may_read)
32375 +{
32376 +       int err;
32377 +       unsigned long expire;
32378 +       unsigned char do_read;
32379 +       struct fillvdir_arg arg = {
32380 +               .ctx = {
32381 +                       .actor = fillvdir
32382 +               }
32383 +       };
32384 +       struct inode *inode;
32385 +       struct au_vdir *vdir, *allocated;
32386 +
32387 +       err = 0;
32388 +       inode = file_inode(file);
32389 +       IMustLock(inode);
32390 +       IiMustWriteLock(inode);
32391 +       SiMustAnyLock(inode->i_sb);
32392 +
32393 +       allocated = NULL;
32394 +       do_read = 0;
32395 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32396 +       vdir = au_ivdir(inode);
32397 +       if (!vdir) {
32398 +               do_read = 1;
32399 +               vdir = alloc_vdir(file);
32400 +               err = PTR_ERR(vdir);
32401 +               if (IS_ERR(vdir))
32402 +                       goto out;
32403 +               err = 0;
32404 +               allocated = vdir;
32405 +       } else if (may_read
32406 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32407 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32408 +               do_read = 1;
32409 +               err = reinit_vdir(vdir);
32410 +               if (unlikely(err))
32411 +                       goto out;
32412 +       }
32413 +
32414 +       if (!do_read)
32415 +               return 0; /* success */
32416 +
32417 +       arg.file = file;
32418 +       arg.vdir = vdir;
32419 +       err = au_do_read_vdir(&arg);
32420 +       if (!err) {
32421 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32422 +               vdir->vd_version = inode_query_iversion(inode);
32423 +               vdir->vd_last.ul = 0;
32424 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32425 +               if (allocated)
32426 +                       au_set_ivdir(inode, allocated);
32427 +       } else if (allocated)
32428 +               au_vdir_free(allocated);
32429 +
32430 +out:
32431 +       return err;
32432 +}
32433 +
32434 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32435 +{
32436 +       int err, rerr;
32437 +       unsigned long ul, n;
32438 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32439 +
32440 +       AuDebugOn(tgt->vd_nblk != 1);
32441 +
32442 +       err = -ENOMEM;
32443 +       if (tgt->vd_nblk < src->vd_nblk) {
32444 +               unsigned char **p;
32445 +
32446 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32447 +                               GFP_NOFS, /*may_shrink*/0);
32448 +               if (unlikely(!p))
32449 +                       goto out;
32450 +               tgt->vd_deblk = p;
32451 +       }
32452 +
32453 +       if (tgt->vd_deblk_sz != deblk_sz) {
32454 +               unsigned char *p;
32455 +
32456 +               tgt->vd_deblk_sz = deblk_sz;
32457 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32458 +                               /*may_shrink*/1);
32459 +               if (unlikely(!p))
32460 +                       goto out;
32461 +               tgt->vd_deblk[0] = p;
32462 +       }
32463 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32464 +       tgt->vd_version = src->vd_version;
32465 +       tgt->vd_jiffy = src->vd_jiffy;
32466 +
32467 +       n = src->vd_nblk;
32468 +       for (ul = 1; ul < n; ul++) {
32469 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32470 +                                           GFP_NOFS);
32471 +               if (unlikely(!tgt->vd_deblk[ul]))
32472 +                       goto out;
32473 +               tgt->vd_nblk++;
32474 +       }
32475 +       tgt->vd_nblk = n;
32476 +       tgt->vd_last.ul = tgt->vd_last.ul;
32477 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32478 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32479 +               - src->vd_deblk[src->vd_last.ul];
32480 +       /* smp_mb(); */
32481 +       return 0; /* success */
32482 +
32483 +out:
32484 +       rerr = reinit_vdir(tgt);
32485 +       BUG_ON(rerr);
32486 +       return err;
32487 +}
32488 +
32489 +int au_vdir_init(struct file *file)
32490 +{
32491 +       int err;
32492 +       struct inode *inode;
32493 +       struct au_vdir *vdir_cache, *allocated;
32494 +
32495 +       /* test file->f_pos here instead of ctx->pos */
32496 +       err = read_vdir(file, !file->f_pos);
32497 +       if (unlikely(err))
32498 +               goto out;
32499 +
32500 +       allocated = NULL;
32501 +       vdir_cache = au_fvdir_cache(file);
32502 +       if (!vdir_cache) {
32503 +               vdir_cache = alloc_vdir(file);
32504 +               err = PTR_ERR(vdir_cache);
32505 +               if (IS_ERR(vdir_cache))
32506 +                       goto out;
32507 +               allocated = vdir_cache;
32508 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32509 +               /* test file->f_pos here instead of ctx->pos */
32510 +               err = reinit_vdir(vdir_cache);
32511 +               if (unlikely(err))
32512 +                       goto out;
32513 +       } else
32514 +               return 0; /* success */
32515 +
32516 +       inode = file_inode(file);
32517 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32518 +       if (!err) {
32519 +               file->f_version = inode_query_iversion(inode);
32520 +               if (allocated)
32521 +                       au_set_fvdir_cache(file, allocated);
32522 +       } else if (allocated)
32523 +               au_vdir_free(allocated);
32524 +
32525 +out:
32526 +       return err;
32527 +}
32528 +
32529 +static loff_t calc_offset(struct au_vdir *vdir)
32530 +{
32531 +       loff_t offset;
32532 +       union au_vdir_deblk_p p;
32533 +
32534 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32535 +       offset = vdir->vd_last.p.deblk - p.deblk;
32536 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32537 +       return offset;
32538 +}
32539 +
32540 +/* returns true or false */
32541 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32542 +{
32543 +       int valid;
32544 +       unsigned int deblk_sz;
32545 +       unsigned long ul, n;
32546 +       loff_t offset;
32547 +       union au_vdir_deblk_p p, deblk_end;
32548 +       struct au_vdir *vdir_cache;
32549 +
32550 +       valid = 1;
32551 +       vdir_cache = au_fvdir_cache(file);
32552 +       offset = calc_offset(vdir_cache);
32553 +       AuDbg("offset %lld\n", offset);
32554 +       if (ctx->pos == offset)
32555 +               goto out;
32556 +
32557 +       vdir_cache->vd_last.ul = 0;
32558 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32559 +       if (!ctx->pos)
32560 +               goto out;
32561 +
32562 +       valid = 0;
32563 +       deblk_sz = vdir_cache->vd_deblk_sz;
32564 +       ul = div64_u64(ctx->pos, deblk_sz);
32565 +       AuDbg("ul %lu\n", ul);
32566 +       if (ul >= vdir_cache->vd_nblk)
32567 +               goto out;
32568 +
32569 +       n = vdir_cache->vd_nblk;
32570 +       for (; ul < n; ul++) {
32571 +               p.deblk = vdir_cache->vd_deblk[ul];
32572 +               deblk_end.deblk = p.deblk + deblk_sz;
32573 +               offset = ul;
32574 +               offset *= deblk_sz;
32575 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32576 +                       unsigned int l;
32577 +
32578 +                       l = calc_size(p.de->de_str.len);
32579 +                       offset += l;
32580 +                       p.deblk += l;
32581 +               }
32582 +               if (!is_deblk_end(&p, &deblk_end)) {
32583 +                       valid = 1;
32584 +                       vdir_cache->vd_last.ul = ul;
32585 +                       vdir_cache->vd_last.p = p;
32586 +                       break;
32587 +               }
32588 +       }
32589 +
32590 +out:
32591 +       /* smp_mb(); */
32592 +       if (!valid)
32593 +               AuDbg("valid %d\n", !valid);
32594 +       return valid;
32595 +}
32596 +
32597 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32598 +{
32599 +       unsigned int l, deblk_sz;
32600 +       union au_vdir_deblk_p deblk_end;
32601 +       struct au_vdir *vdir_cache;
32602 +       struct au_vdir_de *de;
32603 +
32604 +       if (!seek_vdir(file, ctx))
32605 +               return 0;
32606 +
32607 +       vdir_cache = au_fvdir_cache(file);
32608 +       deblk_sz = vdir_cache->vd_deblk_sz;
32609 +       while (1) {
32610 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32611 +               deblk_end.deblk += deblk_sz;
32612 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32613 +                       de = vdir_cache->vd_last.p.de;
32614 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32615 +                             de->de_str.len, de->de_str.name, ctx->pos,
32616 +                             (unsigned long)de->de_ino, de->de_type);
32617 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32618 +                                              de->de_str.len, de->de_ino,
32619 +                                              de->de_type))) {
32620 +                               /* todo: ignore the error caused by udba? */
32621 +                               /* return err; */
32622 +                               return 0;
32623 +                       }
32624 +
32625 +                       l = calc_size(de->de_str.len);
32626 +                       vdir_cache->vd_last.p.deblk += l;
32627 +                       ctx->pos += l;
32628 +               }
32629 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32630 +                       vdir_cache->vd_last.ul++;
32631 +                       vdir_cache->vd_last.p.deblk
32632 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32633 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32634 +                       continue;
32635 +               }
32636 +               break;
32637 +       }
32638 +
32639 +       /* smp_mb(); */
32640 +       return 0;
32641 +}
32642 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32643 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32644 +++ linux/fs/aufs/vfsub.c       2021-11-25 11:42:28.836641524 +0100
32645 @@ -0,0 +1,919 @@
32646 +// SPDX-License-Identifier: GPL-2.0
32647 +/*
32648 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32649 + *
32650 + * This program, aufs is free software; you can redistribute it and/or modify
32651 + * it under the terms of the GNU General Public License as published by
32652 + * the Free Software Foundation; either version 2 of the License, or
32653 + * (at your option) any later version.
32654 + *
32655 + * This program is distributed in the hope that it will be useful,
32656 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32657 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32658 + * GNU General Public License for more details.
32659 + *
32660 + * You should have received a copy of the GNU General Public License
32661 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32662 + */
32663 +
32664 +/*
32665 + * sub-routines for VFS
32666 + */
32667 +
32668 +#include <linux/mnt_namespace.h>
32669 +#include <linux/namei.h>
32670 +#include <linux/nsproxy.h>
32671 +#include <linux/security.h>
32672 +#include <linux/splice.h>
32673 +#include "aufs.h"
32674 +
32675 +#ifdef CONFIG_AUFS_BR_FUSE
32676 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32677 +{
32678 +       if (!au_test_fuse(h_sb) || !au_userns)
32679 +               return 0;
32680 +
32681 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32682 +}
32683 +#endif
32684 +
32685 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32686 +{
32687 +       int err;
32688 +
32689 +       lockdep_off();
32690 +       down_read(&h_sb->s_umount);
32691 +       err = __sync_filesystem(h_sb, wait);
32692 +       up_read(&h_sb->s_umount);
32693 +       lockdep_on();
32694 +
32695 +       return err;
32696 +}
32697 +
32698 +/* ---------------------------------------------------------------------- */
32699 +
32700 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32701 +{
32702 +       int err;
32703 +       struct kstat st;
32704 +       struct super_block *h_sb;
32705 +
32706 +       /*
32707 +        * Always needs h_path->mnt for LSM or FUSE branch.
32708 +        */
32709 +       AuDebugOn(!h_path->mnt);
32710 +
32711 +       /* for remote fs, leave work for its getattr or d_revalidate */
32712 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32713 +       /* still some fs may acquire i_mutex. we need to skip them */
32714 +       err = 0;
32715 +       if (!did)
32716 +               did = &err;
32717 +       h_sb = h_path->dentry->d_sb;
32718 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32719 +       if (*did)
32720 +               err = vfsub_getattr(h_path, &st);
32721 +
32722 +       return err;
32723 +}
32724 +
32725 +/* ---------------------------------------------------------------------- */
32726 +
32727 +struct file *vfsub_dentry_open(struct path *path, int flags)
32728 +{
32729 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32730 +                          current_cred());
32731 +}
32732 +
32733 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32734 +{
32735 +       struct file *file;
32736 +
32737 +       lockdep_off();
32738 +       file = filp_open(path,
32739 +                        oflags /* | __FMODE_NONOTIFY */,
32740 +                        mode);
32741 +       lockdep_on();
32742 +       if (IS_ERR(file))
32743 +               goto out;
32744 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32745 +
32746 +out:
32747 +       return file;
32748 +}
32749 +
32750 +/*
32751 + * Ideally this function should call VFS:do_last() in order to keep all its
32752 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32753 + * structure such as nameidata. This is a second (or third) best approach.
32754 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32755 + */
32756 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32757 +                     struct vfsub_aopen_args *args)
32758 +{
32759 +       int err;
32760 +       struct au_branch *br = args->br;
32761 +       struct file *file = args->file;
32762 +       /* copied from linux/fs/namei.c:atomic_open() */
32763 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32764 +
32765 +       IMustLock(dir);
32766 +       AuDebugOn(!dir->i_op->atomic_open);
32767 +
32768 +       err = au_br_test_oflag(args->open_flag, br);
32769 +       if (unlikely(err))
32770 +               goto out;
32771 +
32772 +       au_lcnt_inc(&br->br_nfiles);
32773 +       file->f_path.dentry = DENTRY_NOT_SET;
32774 +       file->f_path.mnt = au_br_mnt(br);
32775 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32776 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32777 +                                    args->create_mode);
32778 +       if (unlikely(err < 0)) {
32779 +               au_lcnt_dec(&br->br_nfiles);
32780 +               goto out;
32781 +       }
32782 +
32783 +       /* temporary workaround for nfsv4 branch */
32784 +       if (au_test_nfs(dir->i_sb))
32785 +               nfs_mark_for_revalidate(dir);
32786 +
32787 +       if (file->f_mode & FMODE_CREATED)
32788 +               fsnotify_create(dir, dentry);
32789 +       if (!(file->f_mode & FMODE_OPENED)) {
32790 +               au_lcnt_dec(&br->br_nfiles);
32791 +               goto out;
32792 +       }
32793 +
32794 +       /* todo: call VFS:may_open() here */
32795 +       /* todo: ima_file_check() too? */
32796 +       if (!err && (args->open_flag & __FMODE_EXEC))
32797 +               err = deny_write_access(file);
32798 +       if (!err)
32799 +               fsnotify_open(file);
32800 +       else
32801 +               au_lcnt_dec(&br->br_nfiles);
32802 +       /* note that the file is created and still opened */
32803 +
32804 +out:
32805 +       return err;
32806 +}
32807 +
32808 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32809 +{
32810 +       int err;
32811 +
32812 +       err = kern_path(name, flags, path);
32813 +       if (!err && d_is_positive(path->dentry))
32814 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32815 +       return err;
32816 +}
32817 +
32818 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32819 +                                            struct path *ppath, int len)
32820 +{
32821 +       struct path path;
32822 +
32823 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
32824 +       if (IS_ERR(path.dentry))
32825 +               goto out;
32826 +       if (d_is_positive(path.dentry)) {
32827 +               path.mnt = ppath->mnt;
32828 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32829 +       }
32830 +
32831 +out:
32832 +       AuTraceErrPtr(path.dentry);
32833 +       return path.dentry;
32834 +}
32835 +
32836 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
32837 +                                   int len)
32838 +{
32839 +       struct path path;
32840 +
32841 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32842 +       IMustLock(d_inode(ppath->dentry));
32843 +
32844 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
32845 +       if (IS_ERR(path.dentry))
32846 +               goto out;
32847 +       if (d_is_positive(path.dentry)) {
32848 +               path.mnt = ppath->mnt;
32849 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32850 +       }
32851 +
32852 +out:
32853 +       AuTraceErrPtr(path.dentry);
32854 +       return path.dentry;
32855 +}
32856 +
32857 +void vfsub_call_lkup_one(void *args)
32858 +{
32859 +       struct vfsub_lkup_one_args *a = args;
32860 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
32861 +}
32862 +
32863 +/* ---------------------------------------------------------------------- */
32864 +
32865 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32866 +                                struct dentry *d2, struct au_hinode *hdir2)
32867 +{
32868 +       struct dentry *d;
32869 +
32870 +       lockdep_off();
32871 +       d = lock_rename(d1, d2);
32872 +       lockdep_on();
32873 +       au_hn_suspend(hdir1);
32874 +       if (hdir1 != hdir2)
32875 +               au_hn_suspend(hdir2);
32876 +
32877 +       return d;
32878 +}
32879 +
32880 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32881 +                        struct dentry *d2, struct au_hinode *hdir2)
32882 +{
32883 +       au_hn_resume(hdir1);
32884 +       if (hdir1 != hdir2)
32885 +               au_hn_resume(hdir2);
32886 +       lockdep_off();
32887 +       unlock_rename(d1, d2);
32888 +       lockdep_on();
32889 +}
32890 +
32891 +/* ---------------------------------------------------------------------- */
32892 +
32893 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32894 +{
32895 +       int err;
32896 +       struct dentry *d;
32897 +       struct user_namespace *userns;
32898 +
32899 +       IMustLock(dir);
32900 +
32901 +       d = path->dentry;
32902 +       path->dentry = d->d_parent;
32903 +       err = security_path_mknod(path, d, mode, 0);
32904 +       path->dentry = d;
32905 +       if (unlikely(err))
32906 +               goto out;
32907 +       userns = mnt_user_ns(path->mnt);
32908 +
32909 +       lockdep_off();
32910 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
32911 +       lockdep_on();
32912 +       if (!err) {
32913 +               struct path tmp = *path;
32914 +               int did;
32915 +
32916 +               vfsub_update_h_iattr(&tmp, &did);
32917 +               if (did) {
32918 +                       tmp.dentry = path->dentry->d_parent;
32919 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32920 +               }
32921 +               /*ignore*/
32922 +       }
32923 +
32924 +out:
32925 +       return err;
32926 +}
32927 +
32928 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32929 +{
32930 +       int err;
32931 +       struct dentry *d;
32932 +       struct user_namespace *userns;
32933 +
32934 +       IMustLock(dir);
32935 +
32936 +       d = path->dentry;
32937 +       path->dentry = d->d_parent;
32938 +       err = security_path_symlink(path, d, symname);
32939 +       path->dentry = d;
32940 +       if (unlikely(err))
32941 +               goto out;
32942 +       userns = mnt_user_ns(path->mnt);
32943 +
32944 +       lockdep_off();
32945 +       err = vfs_symlink(userns, dir, path->dentry, symname);
32946 +       lockdep_on();
32947 +       if (!err) {
32948 +               struct path tmp = *path;
32949 +               int did;
32950 +
32951 +               vfsub_update_h_iattr(&tmp, &did);
32952 +               if (did) {
32953 +                       tmp.dentry = path->dentry->d_parent;
32954 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32955 +               }
32956 +               /*ignore*/
32957 +       }
32958 +
32959 +out:
32960 +       return err;
32961 +}
32962 +
32963 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32964 +{
32965 +       int err;
32966 +       struct dentry *d;
32967 +       struct user_namespace *userns;
32968 +
32969 +       IMustLock(dir);
32970 +
32971 +       d = path->dentry;
32972 +       path->dentry = d->d_parent;
32973 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32974 +       path->dentry = d;
32975 +       if (unlikely(err))
32976 +               goto out;
32977 +       userns = mnt_user_ns(path->mnt);
32978 +
32979 +       lockdep_off();
32980 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
32981 +       lockdep_on();
32982 +       if (!err) {
32983 +               struct path tmp = *path;
32984 +               int did;
32985 +
32986 +               vfsub_update_h_iattr(&tmp, &did);
32987 +               if (did) {
32988 +                       tmp.dentry = path->dentry->d_parent;
32989 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32990 +               }
32991 +               /*ignore*/
32992 +       }
32993 +
32994 +out:
32995 +       return err;
32996 +}
32997 +
32998 +static int au_test_nlink(struct inode *inode)
32999 +{
33000 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33001 +
33002 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33003 +           || inode->i_nlink < link_max)
33004 +               return 0;
33005 +       return -EMLINK;
33006 +}
33007 +
33008 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33009 +              struct inode **delegated_inode)
33010 +{
33011 +       int err;
33012 +       struct dentry *d;
33013 +       struct user_namespace *userns;
33014 +
33015 +       IMustLock(dir);
33016 +
33017 +       err = au_test_nlink(d_inode(src_dentry));
33018 +       if (unlikely(err))
33019 +               return err;
33020 +
33021 +       /* we don't call may_linkat() */
33022 +       d = path->dentry;
33023 +       path->dentry = d->d_parent;
33024 +       err = security_path_link(src_dentry, path, d);
33025 +       path->dentry = d;
33026 +       if (unlikely(err))
33027 +               goto out;
33028 +       userns = mnt_user_ns(path->mnt);
33029 +
33030 +       lockdep_off();
33031 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33032 +       lockdep_on();
33033 +       if (!err) {
33034 +               struct path tmp = *path;
33035 +               int did;
33036 +
33037 +               /* fuse has different memory inode for the same inumber */
33038 +               vfsub_update_h_iattr(&tmp, &did);
33039 +               if (did) {
33040 +                       tmp.dentry = path->dentry->d_parent;
33041 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33042 +                       tmp.dentry = src_dentry;
33043 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33044 +               }
33045 +               /*ignore*/
33046 +       }
33047 +
33048 +out:
33049 +       return err;
33050 +}
33051 +
33052 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33053 +                struct inode *dir, struct path *path,
33054 +                struct inode **delegated_inode, unsigned int flags)
33055 +{
33056 +       int err;
33057 +       struct renamedata rd;
33058 +       struct path tmp = {
33059 +               .mnt    = path->mnt
33060 +       };
33061 +       struct dentry *d;
33062 +
33063 +       IMustLock(dir);
33064 +       IMustLock(src_dir);
33065 +
33066 +       d = path->dentry;
33067 +       path->dentry = d->d_parent;
33068 +       tmp.dentry = src_dentry->d_parent;
33069 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33070 +       path->dentry = d;
33071 +       if (unlikely(err))
33072 +               goto out;
33073 +
33074 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33075 +       rd.old_dir = src_dir;
33076 +       rd.old_dentry = src_dentry;
33077 +       rd.new_mnt_userns = rd.old_mnt_userns;
33078 +       rd.new_dir = dir;
33079 +       rd.new_dentry = path->dentry;
33080 +       rd.delegated_inode = delegated_inode;
33081 +       rd.flags = flags;
33082 +       lockdep_off();
33083 +       err = vfs_rename(&rd);
33084 +       lockdep_on();
33085 +       if (!err) {
33086 +               int did;
33087 +
33088 +               tmp.dentry = d->d_parent;
33089 +               vfsub_update_h_iattr(&tmp, &did);
33090 +               if (did) {
33091 +                       tmp.dentry = src_dentry;
33092 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33093 +                       tmp.dentry = src_dentry->d_parent;
33094 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33095 +               }
33096 +               /*ignore*/
33097 +       }
33098 +
33099 +out:
33100 +       return err;
33101 +}
33102 +
33103 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33104 +{
33105 +       int err;
33106 +       struct dentry *d;
33107 +       struct user_namespace *userns;
33108 +
33109 +       IMustLock(dir);
33110 +
33111 +       d = path->dentry;
33112 +       path->dentry = d->d_parent;
33113 +       err = security_path_mkdir(path, d, mode);
33114 +       path->dentry = d;
33115 +       if (unlikely(err))
33116 +               goto out;
33117 +       userns = mnt_user_ns(path->mnt);
33118 +
33119 +       lockdep_off();
33120 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33121 +       lockdep_on();
33122 +       if (!err) {
33123 +               struct path tmp = *path;
33124 +               int did;
33125 +
33126 +               vfsub_update_h_iattr(&tmp, &did);
33127 +               if (did) {
33128 +                       tmp.dentry = path->dentry->d_parent;
33129 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33130 +               }
33131 +               /*ignore*/
33132 +       }
33133 +
33134 +out:
33135 +       return err;
33136 +}
33137 +
33138 +int vfsub_rmdir(struct inode *dir, struct path *path)
33139 +{
33140 +       int err;
33141 +       struct dentry *d;
33142 +       struct user_namespace *userns;
33143 +
33144 +       IMustLock(dir);
33145 +
33146 +       d = path->dentry;
33147 +       path->dentry = d->d_parent;
33148 +       err = security_path_rmdir(path, d);
33149 +       path->dentry = d;
33150 +       if (unlikely(err))
33151 +               goto out;
33152 +       userns = mnt_user_ns(path->mnt);
33153 +
33154 +       lockdep_off();
33155 +       err = vfs_rmdir(userns, dir, path->dentry);
33156 +       lockdep_on();
33157 +       if (!err) {
33158 +               struct path tmp = {
33159 +                       .dentry = path->dentry->d_parent,
33160 +                       .mnt    = path->mnt
33161 +               };
33162 +
33163 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33164 +       }
33165 +
33166 +out:
33167 +       return err;
33168 +}
33169 +
33170 +/* ---------------------------------------------------------------------- */
33171 +
33172 +/* todo: support mmap_sem? */
33173 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33174 +                    loff_t *ppos)
33175 +{
33176 +       ssize_t err;
33177 +
33178 +       lockdep_off();
33179 +       err = vfs_read(file, ubuf, count, ppos);
33180 +       lockdep_on();
33181 +       if (err >= 0)
33182 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33183 +       return err;
33184 +}
33185 +
33186 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33187 +                    loff_t *ppos)
33188 +{
33189 +       ssize_t err;
33190 +
33191 +       lockdep_off();
33192 +       err = kernel_read(file, kbuf, count, ppos);
33193 +       lockdep_on();
33194 +       AuTraceErr(err);
33195 +       if (err >= 0)
33196 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33197 +       return err;
33198 +}
33199 +
33200 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33201 +                     loff_t *ppos)
33202 +{
33203 +       ssize_t err;
33204 +
33205 +       lockdep_off();
33206 +       err = vfs_write(file, ubuf, count, ppos);
33207 +       lockdep_on();
33208 +       if (err >= 0)
33209 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33210 +       return err;
33211 +}
33212 +
33213 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33214 +{
33215 +       ssize_t err;
33216 +
33217 +       lockdep_off();
33218 +       err = kernel_write(file, kbuf, count, ppos);
33219 +       lockdep_on();
33220 +       if (err >= 0)
33221 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33222 +       return err;
33223 +}
33224 +
33225 +int vfsub_flush(struct file *file, fl_owner_t id)
33226 +{
33227 +       int err;
33228 +
33229 +       err = 0;
33230 +       if (file->f_op->flush) {
33231 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33232 +                       err = file->f_op->flush(file, id);
33233 +               else {
33234 +                       lockdep_off();
33235 +                       err = file->f_op->flush(file, id);
33236 +                       lockdep_on();
33237 +               }
33238 +               if (!err)
33239 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33240 +               /*ignore*/
33241 +       }
33242 +       return err;
33243 +}
33244 +
33245 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33246 +{
33247 +       int err;
33248 +
33249 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33250 +
33251 +       lockdep_off();
33252 +       err = iterate_dir(file, ctx);
33253 +       lockdep_on();
33254 +       if (err >= 0)
33255 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33256 +
33257 +       return err;
33258 +}
33259 +
33260 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33261 +                    struct pipe_inode_info *pipe, size_t len,
33262 +                    unsigned int flags)
33263 +{
33264 +       long err;
33265 +
33266 +       lockdep_off();
33267 +       err = do_splice_to(in, ppos, pipe, len, flags);
33268 +       lockdep_on();
33269 +       file_accessed(in);
33270 +       if (err >= 0)
33271 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33272 +       return err;
33273 +}
33274 +
33275 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33276 +                      loff_t *ppos, size_t len, unsigned int flags)
33277 +{
33278 +       long err;
33279 +
33280 +       lockdep_off();
33281 +       err = do_splice_from(pipe, out, ppos, len, flags);
33282 +       lockdep_on();
33283 +       if (err >= 0)
33284 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33285 +       return err;
33286 +}
33287 +
33288 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33289 +{
33290 +       int err;
33291 +
33292 +       /* file can be NULL */
33293 +       lockdep_off();
33294 +       err = vfs_fsync(file, datasync);
33295 +       lockdep_on();
33296 +       if (!err) {
33297 +               if (!path) {
33298 +                       AuDebugOn(!file);
33299 +                       path = &file->f_path;
33300 +               }
33301 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33302 +       }
33303 +       return err;
33304 +}
33305 +
33306 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33307 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33308 +               struct file *h_file)
33309 +{
33310 +       int err;
33311 +       struct inode *h_inode;
33312 +       struct super_block *h_sb;
33313 +       struct user_namespace *h_userns;
33314 +
33315 +       if (!h_file) {
33316 +               err = vfsub_truncate(h_path, length);
33317 +               goto out;
33318 +       }
33319 +
33320 +       h_inode = d_inode(h_path->dentry);
33321 +       h_sb = h_inode->i_sb;
33322 +       lockdep_off();
33323 +       sb_start_write(h_sb);
33324 +       lockdep_on();
33325 +       err = security_path_truncate(h_path);
33326 +       if (!err) {
33327 +               h_userns = mnt_user_ns(h_path->mnt);
33328 +               lockdep_off();
33329 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33330 +                                 h_file);
33331 +               lockdep_on();
33332 +       }
33333 +       lockdep_off();
33334 +       sb_end_write(h_sb);
33335 +       lockdep_on();
33336 +
33337 +out:
33338 +       return err;
33339 +}
33340 +
33341 +/* ---------------------------------------------------------------------- */
33342 +
33343 +struct au_vfsub_mkdir_args {
33344 +       int *errp;
33345 +       struct inode *dir;
33346 +       struct path *path;
33347 +       int mode;
33348 +};
33349 +
33350 +static void au_call_vfsub_mkdir(void *args)
33351 +{
33352 +       struct au_vfsub_mkdir_args *a = args;
33353 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33354 +}
33355 +
33356 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33357 +{
33358 +       int err, do_sio, wkq_err;
33359 +       struct user_namespace *userns;
33360 +
33361 +       userns = mnt_user_ns(path->mnt);
33362 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33363 +       if (!do_sio) {
33364 +               lockdep_off();
33365 +               err = vfsub_mkdir(dir, path, mode);
33366 +               lockdep_on();
33367 +       } else {
33368 +               struct au_vfsub_mkdir_args args = {
33369 +                       .errp   = &err,
33370 +                       .dir    = dir,
33371 +                       .path   = path,
33372 +                       .mode   = mode
33373 +               };
33374 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33375 +               if (unlikely(wkq_err))
33376 +                       err = wkq_err;
33377 +       }
33378 +
33379 +       return err;
33380 +}
33381 +
33382 +struct au_vfsub_rmdir_args {
33383 +       int *errp;
33384 +       struct inode *dir;
33385 +       struct path *path;
33386 +};
33387 +
33388 +static void au_call_vfsub_rmdir(void *args)
33389 +{
33390 +       struct au_vfsub_rmdir_args *a = args;
33391 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33392 +}
33393 +
33394 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33395 +{
33396 +       int err, do_sio, wkq_err;
33397 +       struct user_namespace *userns;
33398 +
33399 +       userns = mnt_user_ns(path->mnt);
33400 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33401 +       if (!do_sio) {
33402 +               lockdep_off();
33403 +               err = vfsub_rmdir(dir, path);
33404 +               lockdep_on();
33405 +       } else {
33406 +               struct au_vfsub_rmdir_args args = {
33407 +                       .errp   = &err,
33408 +                       .dir    = dir,
33409 +                       .path   = path
33410 +               };
33411 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33412 +               if (unlikely(wkq_err))
33413 +                       err = wkq_err;
33414 +       }
33415 +
33416 +       return err;
33417 +}
33418 +
33419 +/* ---------------------------------------------------------------------- */
33420 +
33421 +struct notify_change_args {
33422 +       int *errp;
33423 +       struct path *path;
33424 +       struct iattr *ia;
33425 +       struct inode **delegated_inode;
33426 +};
33427 +
33428 +static void call_notify_change(void *args)
33429 +{
33430 +       struct notify_change_args *a = args;
33431 +       struct inode *h_inode;
33432 +       struct user_namespace *userns;
33433 +
33434 +       h_inode = d_inode(a->path->dentry);
33435 +       IMustLock(h_inode);
33436 +
33437 +       *a->errp = -EPERM;
33438 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33439 +               userns = mnt_user_ns(a->path->mnt);
33440 +               lockdep_off();
33441 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33442 +                                        a->delegated_inode);
33443 +               lockdep_on();
33444 +               if (!*a->errp)
33445 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33446 +       }
33447 +       AuTraceErr(*a->errp);
33448 +}
33449 +
33450 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33451 +                       struct inode **delegated_inode)
33452 +{
33453 +       int err;
33454 +       struct notify_change_args args = {
33455 +               .errp                   = &err,
33456 +               .path                   = path,
33457 +               .ia                     = ia,
33458 +               .delegated_inode        = delegated_inode
33459 +       };
33460 +
33461 +       call_notify_change(&args);
33462 +
33463 +       return err;
33464 +}
33465 +
33466 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33467 +                           struct inode **delegated_inode)
33468 +{
33469 +       int err, wkq_err;
33470 +       struct notify_change_args args = {
33471 +               .errp                   = &err,
33472 +               .path                   = path,
33473 +               .ia                     = ia,
33474 +               .delegated_inode        = delegated_inode
33475 +       };
33476 +
33477 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33478 +       if (unlikely(wkq_err))
33479 +               err = wkq_err;
33480 +
33481 +       return err;
33482 +}
33483 +
33484 +/* ---------------------------------------------------------------------- */
33485 +
33486 +struct unlink_args {
33487 +       int *errp;
33488 +       struct inode *dir;
33489 +       struct path *path;
33490 +       struct inode **delegated_inode;
33491 +};
33492 +
33493 +static void call_unlink(void *args)
33494 +{
33495 +       struct unlink_args *a = args;
33496 +       struct dentry *d = a->path->dentry;
33497 +       struct inode *h_inode;
33498 +       struct user_namespace *userns;
33499 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33500 +                                     && au_dcount(d) == 1);
33501 +
33502 +       IMustLock(a->dir);
33503 +
33504 +       a->path->dentry = d->d_parent;
33505 +       *a->errp = security_path_unlink(a->path, d);
33506 +       a->path->dentry = d;
33507 +       if (unlikely(*a->errp))
33508 +               return;
33509 +
33510 +       if (!stop_sillyrename)
33511 +               dget(d);
33512 +       h_inode = NULL;
33513 +       if (d_is_positive(d)) {
33514 +               h_inode = d_inode(d);
33515 +               ihold(h_inode);
33516 +       }
33517 +
33518 +       userns = mnt_user_ns(a->path->mnt);
33519 +       lockdep_off();
33520 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33521 +       lockdep_on();
33522 +       if (!*a->errp) {
33523 +               struct path tmp = {
33524 +                       .dentry = d->d_parent,
33525 +                       .mnt    = a->path->mnt
33526 +               };
33527 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33528 +       }
33529 +
33530 +       if (!stop_sillyrename)
33531 +               dput(d);
33532 +       if (h_inode)
33533 +               iput(h_inode);
33534 +
33535 +       AuTraceErr(*a->errp);
33536 +}
33537 +
33538 +/*
33539 + * @dir: must be locked.
33540 + * @dentry: target dentry.
33541 + */
33542 +int vfsub_unlink(struct inode *dir, struct path *path,
33543 +                struct inode **delegated_inode, int force)
33544 +{
33545 +       int err;
33546 +       struct unlink_args args = {
33547 +               .errp                   = &err,
33548 +               .dir                    = dir,
33549 +               .path                   = path,
33550 +               .delegated_inode        = delegated_inode
33551 +       };
33552 +
33553 +       if (!force)
33554 +               call_unlink(&args);
33555 +       else {
33556 +               int wkq_err;
33557 +
33558 +               wkq_err = au_wkq_wait(call_unlink, &args);
33559 +               if (unlikely(wkq_err))
33560 +                       err = wkq_err;
33561 +       }
33562 +
33563 +       return err;
33564 +}
33565 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33566 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33567 +++ linux/fs/aufs/vfsub.h       2021-11-25 11:42:28.836641524 +0100
33568 @@ -0,0 +1,358 @@
33569 +/* SPDX-License-Identifier: GPL-2.0 */
33570 +/*
33571 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33572 + *
33573 + * This program, aufs is free software; you can redistribute it and/or modify
33574 + * it under the terms of the GNU General Public License as published by
33575 + * the Free Software Foundation; either version 2 of the License, or
33576 + * (at your option) any later version.
33577 + *
33578 + * This program is distributed in the hope that it will be useful,
33579 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33580 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33581 + * GNU General Public License for more details.
33582 + *
33583 + * You should have received a copy of the GNU General Public License
33584 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33585 + */
33586 +
33587 +/*
33588 + * sub-routines for VFS
33589 + */
33590 +
33591 +#ifndef __AUFS_VFSUB_H__
33592 +#define __AUFS_VFSUB_H__
33593 +
33594 +#ifdef __KERNEL__
33595 +
33596 +#include <linux/fs.h>
33597 +#include <linux/mount.h>
33598 +#include <linux/posix_acl.h>
33599 +#include <linux/xattr.h>
33600 +#include "debug.h"
33601 +
33602 +/* copied from linux/fs/internal.h */
33603 +/* todo: BAD approach!! */
33604 +extern void __mnt_drop_write(struct vfsmount *);
33605 +extern struct file *alloc_empty_file(int, const struct cred *);
33606 +
33607 +/* ---------------------------------------------------------------------- */
33608 +
33609 +/* lock subclass for lower inode */
33610 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33611 +/* reduce? gave up. */
33612 +enum {
33613 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33614 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33615 +       AuLsc_I_PARENT2,        /* copyup dirs */
33616 +       AuLsc_I_PARENT3,        /* copyup wh */
33617 +       AuLsc_I_CHILD,
33618 +       AuLsc_I_CHILD2,
33619 +       AuLsc_I_End
33620 +};
33621 +
33622 +/* to debug easier, do not make them inlined functions */
33623 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33624 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33625 +
33626 +/* ---------------------------------------------------------------------- */
33627 +
33628 +static inline void vfsub_drop_nlink(struct inode *inode)
33629 +{
33630 +       AuDebugOn(!inode->i_nlink);
33631 +       drop_nlink(inode);
33632 +}
33633 +
33634 +static inline void vfsub_dead_dir(struct inode *inode)
33635 +{
33636 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33637 +       inode->i_flags |= S_DEAD;
33638 +       clear_nlink(inode);
33639 +}
33640 +
33641 +static inline int vfsub_native_ro(struct inode *inode)
33642 +{
33643 +       return sb_rdonly(inode->i_sb)
33644 +               || IS_RDONLY(inode)
33645 +               /* || IS_APPEND(inode) */
33646 +               || IS_IMMUTABLE(inode);
33647 +}
33648 +
33649 +#ifdef CONFIG_AUFS_BR_FUSE
33650 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33651 +#else
33652 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33653 +#endif
33654 +
33655 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33656 +
33657 +/* ---------------------------------------------------------------------- */
33658 +
33659 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33660 +struct file *vfsub_dentry_open(struct path *path, int flags);
33661 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33662 +struct au_branch;
33663 +struct vfsub_aopen_args {
33664 +       struct file             *file;
33665 +       unsigned int            open_flag;
33666 +       umode_t                 create_mode;
33667 +       struct au_branch        *br;
33668 +};
33669 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33670 +                     struct vfsub_aopen_args *args);
33671 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33672 +
33673 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33674 +                                            struct path *ppath, int len);
33675 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33676 +                                   int len);
33677 +
33678 +struct vfsub_lkup_one_args {
33679 +       struct dentry **errp;
33680 +       struct qstr *name;
33681 +       struct path *ppath;
33682 +};
33683 +
33684 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33685 +                                           struct path *ppath)
33686 +{
33687 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33688 +}
33689 +
33690 +void vfsub_call_lkup_one(void *args);
33691 +
33692 +/* ---------------------------------------------------------------------- */
33693 +
33694 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33695 +{
33696 +       int err;
33697 +
33698 +       lockdep_off();
33699 +       err = mnt_want_write(mnt);
33700 +       lockdep_on();
33701 +       return err;
33702 +}
33703 +
33704 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33705 +{
33706 +       lockdep_off();
33707 +       mnt_drop_write(mnt);
33708 +       lockdep_on();
33709 +}
33710 +
33711 +#if 0 /* reserved */
33712 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33713 +{
33714 +       lockdep_off();
33715 +       mnt_drop_write_file(file);
33716 +       lockdep_on();
33717 +}
33718 +#endif
33719 +
33720 +/* ---------------------------------------------------------------------- */
33721 +
33722 +struct au_hinode;
33723 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33724 +                                struct dentry *d2, struct au_hinode *hdir2);
33725 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33726 +                        struct dentry *d2, struct au_hinode *hdir2);
33727 +
33728 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33729 +                bool want_excl);
33730 +int vfsub_symlink(struct inode *dir, struct path *path,
33731 +                 const char *symname);
33732 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33733 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33734 +              struct path *path, struct inode **delegated_inode);
33735 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33736 +                struct inode *hdir, struct path *path,
33737 +                struct inode **delegated_inode, unsigned int flags);
33738 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33739 +int vfsub_rmdir(struct inode *dir, struct path *path);
33740 +
33741 +/* ---------------------------------------------------------------------- */
33742 +
33743 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33744 +                    loff_t *ppos);
33745 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33746 +                       loff_t *ppos);
33747 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33748 +                     loff_t *ppos);
33749 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33750 +                     loff_t *ppos);
33751 +int vfsub_flush(struct file *file, fl_owner_t id);
33752 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33753 +
33754 +static inline loff_t vfsub_f_size_read(struct file *file)
33755 +{
33756 +       return i_size_read(file_inode(file));
33757 +}
33758 +
33759 +static inline unsigned int vfsub_file_flags(struct file *file)
33760 +{
33761 +       unsigned int flags;
33762 +
33763 +       spin_lock(&file->f_lock);
33764 +       flags = file->f_flags;
33765 +       spin_unlock(&file->f_lock);
33766 +
33767 +       return flags;
33768 +}
33769 +
33770 +static inline int vfsub_file_execed(struct file *file)
33771 +{
33772 +       /* todo: direct access f_flags */
33773 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33774 +}
33775 +
33776 +#if 0 /* reserved */
33777 +static inline void vfsub_file_accessed(struct file *h_file)
33778 +{
33779 +       file_accessed(h_file);
33780 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33781 +}
33782 +#endif
33783 +
33784 +#if 0 /* reserved */
33785 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33786 +                                    struct dentry *h_dentry)
33787 +{
33788 +       struct path h_path = {
33789 +               .dentry = h_dentry,
33790 +               .mnt    = h_mnt
33791 +       };
33792 +       touch_atime(&h_path);
33793 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33794 +}
33795 +#endif
33796 +
33797 +static inline int vfsub_update_time(struct inode *h_inode,
33798 +                                   struct timespec64 *ts, int flags)
33799 +{
33800 +       return inode_update_time(h_inode, ts, flags);
33801 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33802 +}
33803 +
33804 +#ifdef CONFIG_FS_POSIX_ACL
33805 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
33806 +                                 struct inode *h_inode, umode_t h_mode)
33807 +{
33808 +       int err;
33809 +
33810 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
33811 +       if (err == -EOPNOTSUPP)
33812 +               err = 0;
33813 +       return err;
33814 +}
33815 +#else
33816 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
33817 +          struct inode *h_inode, umode_t h_mode);
33818 +#endif
33819 +
33820 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33821 +                    struct pipe_inode_info *pipe, size_t len,
33822 +                    unsigned int flags);
33823 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33824 +                      loff_t *ppos, size_t len, unsigned int flags);
33825 +
33826 +static inline long vfsub_truncate(struct path *path, loff_t length)
33827 +{
33828 +       long err;
33829 +
33830 +       lockdep_off();
33831 +       err = vfs_truncate(path, length);
33832 +       lockdep_on();
33833 +       return err;
33834 +}
33835 +
33836 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33837 +               struct file *h_file);
33838 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33839 +
33840 +/*
33841 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33842 + * ioctl.
33843 + */
33844 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33845 +                                           loff_t len)
33846 +{
33847 +       loff_t err;
33848 +
33849 +       lockdep_off();
33850 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33851 +       lockdep_on();
33852 +
33853 +       return err;
33854 +}
33855 +
33856 +/* copy_file_range(2) is a systemcall */
33857 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33858 +                                           struct file *dst, loff_t dst_pos,
33859 +                                           size_t len, unsigned int flags)
33860 +{
33861 +       ssize_t ssz;
33862 +
33863 +       lockdep_off();
33864 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33865 +       lockdep_on();
33866 +
33867 +       return ssz;
33868 +}
33869 +
33870 +/* ---------------------------------------------------------------------- */
33871 +
33872 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33873 +{
33874 +       loff_t err;
33875 +
33876 +       lockdep_off();
33877 +       err = vfs_llseek(file, offset, origin);
33878 +       lockdep_on();
33879 +       return err;
33880 +}
33881 +
33882 +/* ---------------------------------------------------------------------- */
33883 +
33884 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33885 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33886 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33887 +                           struct inode **delegated_inode);
33888 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33889 +                       struct inode **delegated_inode);
33890 +int vfsub_unlink(struct inode *dir, struct path *path,
33891 +                struct inode **delegated_inode, int force);
33892 +
33893 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33894 +{
33895 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33896 +}
33897 +
33898 +/* ---------------------------------------------------------------------- */
33899 +
33900 +static inline int vfsub_setxattr(struct user_namespace *userns,
33901 +                                struct dentry *dentry, const char *name,
33902 +                                const void *value, size_t size, int flags)
33903 +{
33904 +       int err;
33905 +
33906 +       lockdep_off();
33907 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
33908 +       lockdep_on();
33909 +
33910 +       return err;
33911 +}
33912 +
33913 +static inline int vfsub_removexattr(struct user_namespace *userns,
33914 +                                   struct dentry *dentry, const char *name)
33915 +{
33916 +       int err;
33917 +
33918 +       lockdep_off();
33919 +       err = vfs_removexattr(userns, dentry, name);
33920 +       lockdep_on();
33921 +
33922 +       return err;
33923 +}
33924 +
33925 +#endif /* __KERNEL__ */
33926 +#endif /* __AUFS_VFSUB_H__ */
33927 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33928 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33929 +++ linux/fs/aufs/wbr_policy.c  2021-11-01 23:48:34.213025928 +0100
33930 @@ -0,0 +1,830 @@
33931 +// SPDX-License-Identifier: GPL-2.0
33932 +/*
33933 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33934 + *
33935 + * This program, aufs is free software; you can redistribute it and/or modify
33936 + * it under the terms of the GNU General Public License as published by
33937 + * the Free Software Foundation; either version 2 of the License, or
33938 + * (at your option) any later version.
33939 + *
33940 + * This program is distributed in the hope that it will be useful,
33941 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33942 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33943 + * GNU General Public License for more details.
33944 + *
33945 + * You should have received a copy of the GNU General Public License
33946 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33947 + */
33948 +
33949 +/*
33950 + * policies for selecting one among multiple writable branches
33951 + */
33952 +
33953 +#include <linux/statfs.h>
33954 +#include "aufs.h"
33955 +
33956 +/* subset of cpup_attr() */
33957 +static noinline_for_stack
33958 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33959 +{
33960 +       int err, sbits;
33961 +       struct iattr ia;
33962 +       struct inode *h_isrc;
33963 +
33964 +       h_isrc = d_inode(h_src);
33965 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33966 +       ia.ia_mode = h_isrc->i_mode;
33967 +       ia.ia_uid = h_isrc->i_uid;
33968 +       ia.ia_gid = h_isrc->i_gid;
33969 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33970 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33971 +       /* no delegation since it is just created */
33972 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33973 +
33974 +       /* is this nfs only? */
33975 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33976 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33977 +               ia.ia_mode = h_isrc->i_mode;
33978 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33979 +       }
33980 +
33981 +       return err;
33982 +}
33983 +
33984 +#define AuCpdown_PARENT_OPQ    1
33985 +#define AuCpdown_WHED          (1 << 1)
33986 +#define AuCpdown_MADE_DIR      (1 << 2)
33987 +#define AuCpdown_DIROPQ                (1 << 3)
33988 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
33989 +#define au_fset_cpdown(flags, name) \
33990 +       do { (flags) |= AuCpdown_##name; } while (0)
33991 +#define au_fclr_cpdown(flags, name) \
33992 +       do { (flags) &= ~AuCpdown_##name; } while (0)
33993 +
33994 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33995 +                            unsigned int *flags)
33996 +{
33997 +       int err;
33998 +       struct dentry *opq_dentry;
33999 +
34000 +       opq_dentry = au_diropq_create(dentry, bdst);
34001 +       err = PTR_ERR(opq_dentry);
34002 +       if (IS_ERR(opq_dentry))
34003 +               goto out;
34004 +       dput(opq_dentry);
34005 +       au_fset_cpdown(*flags, DIROPQ);
34006 +
34007 +out:
34008 +       return err;
34009 +}
34010 +
34011 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34012 +                           struct inode *dir, aufs_bindex_t bdst)
34013 +{
34014 +       int err;
34015 +       struct path h_path;
34016 +       struct au_branch *br;
34017 +
34018 +       br = au_sbr(dentry->d_sb, bdst);
34019 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34020 +       err = PTR_ERR(h_path.dentry);
34021 +       if (IS_ERR(h_path.dentry))
34022 +               goto out;
34023 +
34024 +       err = 0;
34025 +       if (d_is_positive(h_path.dentry)) {
34026 +               h_path.mnt = au_br_mnt(br);
34027 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34028 +                                         dentry);
34029 +       }
34030 +       dput(h_path.dentry);
34031 +
34032 +out:
34033 +       return err;
34034 +}
34035 +
34036 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34037 +                        struct au_pin *pin,
34038 +                        struct dentry *h_parent, void *arg)
34039 +{
34040 +       int err, rerr;
34041 +       aufs_bindex_t bopq, btop;
34042 +       struct path h_path;
34043 +       struct dentry *parent;
34044 +       struct inode *h_dir, *h_inode, *inode, *dir;
34045 +       unsigned int *flags = arg;
34046 +
34047 +       btop = au_dbtop(dentry);
34048 +       /* dentry is di-locked */
34049 +       parent = dget_parent(dentry);
34050 +       dir = d_inode(parent);
34051 +       h_dir = d_inode(h_parent);
34052 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34053 +       IMustLock(h_dir);
34054 +
34055 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34056 +       if (unlikely(err < 0))
34057 +               goto out;
34058 +       h_path.dentry = au_h_dptr(dentry, bdst);
34059 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34060 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34061 +       if (unlikely(err))
34062 +               goto out_put;
34063 +       au_fset_cpdown(*flags, MADE_DIR);
34064 +
34065 +       bopq = au_dbdiropq(dentry);
34066 +       au_fclr_cpdown(*flags, WHED);
34067 +       au_fclr_cpdown(*flags, DIROPQ);
34068 +       if (au_dbwh(dentry) == bdst)
34069 +               au_fset_cpdown(*flags, WHED);
34070 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34071 +               au_fset_cpdown(*flags, PARENT_OPQ);
34072 +       h_inode = d_inode(h_path.dentry);
34073 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34074 +       if (au_ftest_cpdown(*flags, WHED)) {
34075 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34076 +               if (unlikely(err)) {
34077 +                       inode_unlock(h_inode);
34078 +                       goto out_dir;
34079 +               }
34080 +       }
34081 +
34082 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34083 +       inode_unlock(h_inode);
34084 +       if (unlikely(err))
34085 +               goto out_opq;
34086 +
34087 +       if (au_ftest_cpdown(*flags, WHED)) {
34088 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34089 +               if (unlikely(err))
34090 +                       goto out_opq;
34091 +       }
34092 +
34093 +       inode = d_inode(dentry);
34094 +       if (au_ibbot(inode) < bdst)
34095 +               au_set_ibbot(inode, bdst);
34096 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34097 +                     au_hi_flags(inode, /*isdir*/1));
34098 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34099 +       goto out; /* success */
34100 +
34101 +       /* revert */
34102 +out_opq:
34103 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34104 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34105 +               rerr = au_diropq_remove(dentry, bdst);
34106 +               inode_unlock(h_inode);
34107 +               if (unlikely(rerr)) {
34108 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34109 +                               dentry, bdst, rerr);
34110 +                       err = -EIO;
34111 +                       goto out;
34112 +               }
34113 +       }
34114 +out_dir:
34115 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34116 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34117 +               if (unlikely(rerr)) {
34118 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34119 +                               dentry, bdst, rerr);
34120 +                       err = -EIO;
34121 +               }
34122 +       }
34123 +out_put:
34124 +       au_set_h_dptr(dentry, bdst, NULL);
34125 +       if (au_dbbot(dentry) == bdst)
34126 +               au_update_dbbot(dentry);
34127 +out:
34128 +       dput(parent);
34129 +       return err;
34130 +}
34131 +
34132 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34133 +{
34134 +       int err;
34135 +       unsigned int flags;
34136 +
34137 +       flags = 0;
34138 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34139 +
34140 +       return err;
34141 +}
34142 +
34143 +/* ---------------------------------------------------------------------- */
34144 +
34145 +/* policies for create */
34146 +
34147 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34148 +{
34149 +       int err, i, j, ndentry;
34150 +       aufs_bindex_t bopq;
34151 +       struct au_dcsub_pages dpages;
34152 +       struct au_dpage *dpage;
34153 +       struct dentry **dentries, *parent, *d;
34154 +
34155 +       err = au_dpages_init(&dpages, GFP_NOFS);
34156 +       if (unlikely(err))
34157 +               goto out;
34158 +       parent = dget_parent(dentry);
34159 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34160 +       if (unlikely(err))
34161 +               goto out_free;
34162 +
34163 +       err = bindex;
34164 +       for (i = 0; i < dpages.ndpage; i++) {
34165 +               dpage = dpages.dpages + i;
34166 +               dentries = dpage->dentries;
34167 +               ndentry = dpage->ndentry;
34168 +               for (j = 0; j < ndentry; j++) {
34169 +                       d = dentries[j];
34170 +                       di_read_lock_parent2(d, !AuLock_IR);
34171 +                       bopq = au_dbdiropq(d);
34172 +                       di_read_unlock(d, !AuLock_IR);
34173 +                       if (bopq >= 0 && bopq < err)
34174 +                               err = bopq;
34175 +               }
34176 +       }
34177 +
34178 +out_free:
34179 +       dput(parent);
34180 +       au_dpages_free(&dpages);
34181 +out:
34182 +       return err;
34183 +}
34184 +
34185 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34186 +{
34187 +       for (; bindex >= 0; bindex--)
34188 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34189 +                       return bindex;
34190 +       return -EROFS;
34191 +}
34192 +
34193 +/* top down parent */
34194 +static int au_wbr_create_tdp(struct dentry *dentry,
34195 +                            unsigned int flags __maybe_unused)
34196 +{
34197 +       int err;
34198 +       aufs_bindex_t btop, bindex;
34199 +       struct super_block *sb;
34200 +       struct dentry *parent, *h_parent;
34201 +
34202 +       sb = dentry->d_sb;
34203 +       btop = au_dbtop(dentry);
34204 +       err = btop;
34205 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34206 +               goto out;
34207 +
34208 +       err = -EROFS;
34209 +       parent = dget_parent(dentry);
34210 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34211 +               h_parent = au_h_dptr(parent, bindex);
34212 +               if (!h_parent || d_is_negative(h_parent))
34213 +                       continue;
34214 +
34215 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34216 +                       err = bindex;
34217 +                       break;
34218 +               }
34219 +       }
34220 +       dput(parent);
34221 +
34222 +       /* bottom up here */
34223 +       if (unlikely(err < 0)) {
34224 +               err = au_wbr_bu(sb, btop - 1);
34225 +               if (err >= 0)
34226 +                       err = au_wbr_nonopq(dentry, err);
34227 +       }
34228 +
34229 +out:
34230 +       AuDbg("b%d\n", err);
34231 +       return err;
34232 +}
34233 +
34234 +/* ---------------------------------------------------------------------- */
34235 +
34236 +/* an exception for the policy other than tdp */
34237 +static int au_wbr_create_exp(struct dentry *dentry)
34238 +{
34239 +       int err;
34240 +       aufs_bindex_t bwh, bdiropq;
34241 +       struct dentry *parent;
34242 +
34243 +       err = -1;
34244 +       bwh = au_dbwh(dentry);
34245 +       parent = dget_parent(dentry);
34246 +       bdiropq = au_dbdiropq(parent);
34247 +       if (bwh >= 0) {
34248 +               if (bdiropq >= 0)
34249 +                       err = min(bdiropq, bwh);
34250 +               else
34251 +                       err = bwh;
34252 +               AuDbg("%d\n", err);
34253 +       } else if (bdiropq >= 0) {
34254 +               err = bdiropq;
34255 +               AuDbg("%d\n", err);
34256 +       }
34257 +       dput(parent);
34258 +
34259 +       if (err >= 0)
34260 +               err = au_wbr_nonopq(dentry, err);
34261 +
34262 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34263 +               err = -1;
34264 +
34265 +       AuDbg("%d\n", err);
34266 +       return err;
34267 +}
34268 +
34269 +/* ---------------------------------------------------------------------- */
34270 +
34271 +/* round robin */
34272 +static int au_wbr_create_init_rr(struct super_block *sb)
34273 +{
34274 +       int err;
34275 +
34276 +       err = au_wbr_bu(sb, au_sbbot(sb));
34277 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34278 +       /* smp_mb(); */
34279 +
34280 +       AuDbg("b%d\n", err);
34281 +       return err;
34282 +}
34283 +
34284 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34285 +{
34286 +       int err, nbr;
34287 +       unsigned int u;
34288 +       aufs_bindex_t bindex, bbot;
34289 +       struct super_block *sb;
34290 +       atomic_t *next;
34291 +
34292 +       err = au_wbr_create_exp(dentry);
34293 +       if (err >= 0)
34294 +               goto out;
34295 +
34296 +       sb = dentry->d_sb;
34297 +       next = &au_sbi(sb)->si_wbr_rr_next;
34298 +       bbot = au_sbbot(sb);
34299 +       nbr = bbot + 1;
34300 +       for (bindex = 0; bindex <= bbot; bindex++) {
34301 +               if (!au_ftest_wbr(flags, DIR)) {
34302 +                       err = atomic_dec_return(next) + 1;
34303 +                       /* modulo for 0 is meaningless */
34304 +                       if (unlikely(!err))
34305 +                               err = atomic_dec_return(next) + 1;
34306 +               } else
34307 +                       err = atomic_read(next);
34308 +               AuDbg("%d\n", err);
34309 +               u = err;
34310 +               err = u % nbr;
34311 +               AuDbg("%d\n", err);
34312 +               if (!au_br_rdonly(au_sbr(sb, err)))
34313 +                       break;
34314 +               err = -EROFS;
34315 +       }
34316 +
34317 +       if (err >= 0)
34318 +               err = au_wbr_nonopq(dentry, err);
34319 +
34320 +out:
34321 +       AuDbg("%d\n", err);
34322 +       return err;
34323 +}
34324 +
34325 +/* ---------------------------------------------------------------------- */
34326 +
34327 +/* most free space */
34328 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34329 +{
34330 +       struct super_block *sb;
34331 +       struct au_branch *br;
34332 +       struct au_wbr_mfs *mfs;
34333 +       struct dentry *h_parent;
34334 +       aufs_bindex_t bindex, bbot;
34335 +       int err;
34336 +       unsigned long long b, bavail;
34337 +       struct path h_path;
34338 +       /* reduce the stack usage */
34339 +       struct kstatfs *st;
34340 +
34341 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34342 +       if (unlikely(!st)) {
34343 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34344 +               return;
34345 +       }
34346 +
34347 +       bavail = 0;
34348 +       sb = dentry->d_sb;
34349 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34350 +       MtxMustLock(&mfs->mfs_lock);
34351 +       mfs->mfs_bindex = -EROFS;
34352 +       mfs->mfsrr_bytes = 0;
34353 +       if (!parent) {
34354 +               bindex = 0;
34355 +               bbot = au_sbbot(sb);
34356 +       } else {
34357 +               bindex = au_dbtop(parent);
34358 +               bbot = au_dbtaildir(parent);
34359 +       }
34360 +
34361 +       for (; bindex <= bbot; bindex++) {
34362 +               if (parent) {
34363 +                       h_parent = au_h_dptr(parent, bindex);
34364 +                       if (!h_parent || d_is_negative(h_parent))
34365 +                               continue;
34366 +               }
34367 +               br = au_sbr(sb, bindex);
34368 +               if (au_br_rdonly(br))
34369 +                       continue;
34370 +
34371 +               /* sb->s_root for NFS is unreliable */
34372 +               h_path.mnt = au_br_mnt(br);
34373 +               h_path.dentry = h_path.mnt->mnt_root;
34374 +               err = vfs_statfs(&h_path, st);
34375 +               if (unlikely(err)) {
34376 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34377 +                       continue;
34378 +               }
34379 +
34380 +               /* when the available size is equal, select the lower one */
34381 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34382 +                            || sizeof(b) < sizeof(st->f_bsize));
34383 +               b = st->f_bavail * st->f_bsize;
34384 +               br->br_wbr->wbr_bytes = b;
34385 +               if (b >= bavail) {
34386 +                       bavail = b;
34387 +                       mfs->mfs_bindex = bindex;
34388 +                       mfs->mfs_jiffy = jiffies;
34389 +               }
34390 +       }
34391 +
34392 +       mfs->mfsrr_bytes = bavail;
34393 +       AuDbg("b%d\n", mfs->mfs_bindex);
34394 +       au_kfree_rcu(st);
34395 +}
34396 +
34397 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34398 +{
34399 +       int err;
34400 +       struct dentry *parent;
34401 +       struct super_block *sb;
34402 +       struct au_wbr_mfs *mfs;
34403 +
34404 +       err = au_wbr_create_exp(dentry);
34405 +       if (err >= 0)
34406 +               goto out;
34407 +
34408 +       sb = dentry->d_sb;
34409 +       parent = NULL;
34410 +       if (au_ftest_wbr(flags, PARENT))
34411 +               parent = dget_parent(dentry);
34412 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34413 +       mutex_lock(&mfs->mfs_lock);
34414 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34415 +           || mfs->mfs_bindex < 0
34416 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34417 +               au_mfs(dentry, parent);
34418 +       mutex_unlock(&mfs->mfs_lock);
34419 +       err = mfs->mfs_bindex;
34420 +       dput(parent);
34421 +
34422 +       if (err >= 0)
34423 +               err = au_wbr_nonopq(dentry, err);
34424 +
34425 +out:
34426 +       AuDbg("b%d\n", err);
34427 +       return err;
34428 +}
34429 +
34430 +static int au_wbr_create_init_mfs(struct super_block *sb)
34431 +{
34432 +       struct au_wbr_mfs *mfs;
34433 +
34434 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34435 +       mutex_init(&mfs->mfs_lock);
34436 +       mfs->mfs_jiffy = 0;
34437 +       mfs->mfs_bindex = -EROFS;
34438 +
34439 +       return 0;
34440 +}
34441 +
34442 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34443 +{
34444 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34445 +       return 0;
34446 +}
34447 +
34448 +/* ---------------------------------------------------------------------- */
34449 +
34450 +/* top down regardless parent, and then mfs */
34451 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34452 +                              unsigned int flags __maybe_unused)
34453 +{
34454 +       int err;
34455 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34456 +       unsigned long long watermark;
34457 +       struct super_block *sb;
34458 +       struct au_wbr_mfs *mfs;
34459 +       struct au_branch *br;
34460 +       struct dentry *parent;
34461 +
34462 +       sb = dentry->d_sb;
34463 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34464 +       mutex_lock(&mfs->mfs_lock);
34465 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34466 +           || mfs->mfs_bindex < 0)
34467 +               au_mfs(dentry, /*parent*/NULL);
34468 +       watermark = mfs->mfsrr_watermark;
34469 +       bmfs = mfs->mfs_bindex;
34470 +       mutex_unlock(&mfs->mfs_lock);
34471 +
34472 +       /* another style of au_wbr_create_exp() */
34473 +       bwh = au_dbwh(dentry);
34474 +       parent = dget_parent(dentry);
34475 +       btail = au_dbtaildir(parent);
34476 +       if (bwh >= 0 && bwh < btail)
34477 +               btail = bwh;
34478 +
34479 +       err = au_wbr_nonopq(dentry, btail);
34480 +       if (unlikely(err < 0))
34481 +               goto out;
34482 +       btail = err;
34483 +       bfound = -1;
34484 +       for (bindex = 0; bindex <= btail; bindex++) {
34485 +               br = au_sbr(sb, bindex);
34486 +               if (au_br_rdonly(br))
34487 +                       continue;
34488 +               if (br->br_wbr->wbr_bytes > watermark) {
34489 +                       bfound = bindex;
34490 +                       break;
34491 +               }
34492 +       }
34493 +       err = bfound;
34494 +       if (err < 0)
34495 +               err = bmfs;
34496 +
34497 +out:
34498 +       dput(parent);
34499 +       AuDbg("b%d\n", err);
34500 +       return err;
34501 +}
34502 +
34503 +/* ---------------------------------------------------------------------- */
34504 +
34505 +/* most free space and then round robin */
34506 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34507 +{
34508 +       int err;
34509 +       struct au_wbr_mfs *mfs;
34510 +
34511 +       err = au_wbr_create_mfs(dentry, flags);
34512 +       if (err >= 0) {
34513 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34514 +               mutex_lock(&mfs->mfs_lock);
34515 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34516 +                       err = au_wbr_create_rr(dentry, flags);
34517 +               mutex_unlock(&mfs->mfs_lock);
34518 +       }
34519 +
34520 +       AuDbg("b%d\n", err);
34521 +       return err;
34522 +}
34523 +
34524 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34525 +{
34526 +       int err;
34527 +
34528 +       au_wbr_create_init_mfs(sb); /* ignore */
34529 +       err = au_wbr_create_init_rr(sb);
34530 +
34531 +       return err;
34532 +}
34533 +
34534 +/* ---------------------------------------------------------------------- */
34535 +
34536 +/* top down parent and most free space */
34537 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34538 +{
34539 +       int err, e2;
34540 +       unsigned long long b;
34541 +       aufs_bindex_t bindex, btop, bbot;
34542 +       struct super_block *sb;
34543 +       struct dentry *parent, *h_parent;
34544 +       struct au_branch *br;
34545 +
34546 +       err = au_wbr_create_tdp(dentry, flags);
34547 +       if (unlikely(err < 0))
34548 +               goto out;
34549 +       parent = dget_parent(dentry);
34550 +       btop = au_dbtop(parent);
34551 +       bbot = au_dbtaildir(parent);
34552 +       if (btop == bbot)
34553 +               goto out_parent; /* success */
34554 +
34555 +       e2 = au_wbr_create_mfs(dentry, flags);
34556 +       if (e2 < 0)
34557 +               goto out_parent; /* success */
34558 +
34559 +       /* when the available size is equal, select upper one */
34560 +       sb = dentry->d_sb;
34561 +       br = au_sbr(sb, err);
34562 +       b = br->br_wbr->wbr_bytes;
34563 +       AuDbg("b%d, %llu\n", err, b);
34564 +
34565 +       for (bindex = btop; bindex <= bbot; bindex++) {
34566 +               h_parent = au_h_dptr(parent, bindex);
34567 +               if (!h_parent || d_is_negative(h_parent))
34568 +                       continue;
34569 +
34570 +               br = au_sbr(sb, bindex);
34571 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34572 +                       b = br->br_wbr->wbr_bytes;
34573 +                       err = bindex;
34574 +                       AuDbg("b%d, %llu\n", err, b);
34575 +               }
34576 +       }
34577 +
34578 +       if (err >= 0)
34579 +               err = au_wbr_nonopq(dentry, err);
34580 +
34581 +out_parent:
34582 +       dput(parent);
34583 +out:
34584 +       AuDbg("b%d\n", err);
34585 +       return err;
34586 +}
34587 +
34588 +/* ---------------------------------------------------------------------- */
34589 +
34590 +/*
34591 + * - top down parent
34592 + * - most free space with parent
34593 + * - most free space round-robin regardless parent
34594 + */
34595 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34596 +{
34597 +       int err;
34598 +       unsigned long long watermark;
34599 +       struct super_block *sb;
34600 +       struct au_branch *br;
34601 +       struct au_wbr_mfs *mfs;
34602 +
34603 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34604 +       if (unlikely(err < 0))
34605 +               goto out;
34606 +
34607 +       sb = dentry->d_sb;
34608 +       br = au_sbr(sb, err);
34609 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34610 +       mutex_lock(&mfs->mfs_lock);
34611 +       watermark = mfs->mfsrr_watermark;
34612 +       mutex_unlock(&mfs->mfs_lock);
34613 +       if (br->br_wbr->wbr_bytes < watermark)
34614 +               /* regardless the parent dir */
34615 +               err = au_wbr_create_mfsrr(dentry, flags);
34616 +
34617 +out:
34618 +       AuDbg("b%d\n", err);
34619 +       return err;
34620 +}
34621 +
34622 +/* ---------------------------------------------------------------------- */
34623 +
34624 +/* policies for copyup */
34625 +
34626 +/* top down parent */
34627 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34628 +{
34629 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34630 +}
34631 +
34632 +/* bottom up parent */
34633 +static int au_wbr_copyup_bup(struct dentry *dentry)
34634 +{
34635 +       int err;
34636 +       aufs_bindex_t bindex, btop;
34637 +       struct dentry *parent, *h_parent;
34638 +       struct super_block *sb;
34639 +
34640 +       err = -EROFS;
34641 +       sb = dentry->d_sb;
34642 +       parent = dget_parent(dentry);
34643 +       btop = au_dbtop(parent);
34644 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34645 +               h_parent = au_h_dptr(parent, bindex);
34646 +               if (!h_parent || d_is_negative(h_parent))
34647 +                       continue;
34648 +
34649 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34650 +                       err = bindex;
34651 +                       break;
34652 +               }
34653 +       }
34654 +       dput(parent);
34655 +
34656 +       /* bottom up here */
34657 +       if (unlikely(err < 0))
34658 +               err = au_wbr_bu(sb, btop - 1);
34659 +
34660 +       AuDbg("b%d\n", err);
34661 +       return err;
34662 +}
34663 +
34664 +/* bottom up */
34665 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34666 +{
34667 +       int err;
34668 +
34669 +       err = au_wbr_bu(dentry->d_sb, btop);
34670 +       AuDbg("b%d\n", err);
34671 +       if (err > btop)
34672 +               err = au_wbr_nonopq(dentry, err);
34673 +
34674 +       AuDbg("b%d\n", err);
34675 +       return err;
34676 +}
34677 +
34678 +static int au_wbr_copyup_bu(struct dentry *dentry)
34679 +{
34680 +       int err;
34681 +       aufs_bindex_t btop;
34682 +
34683 +       btop = au_dbtop(dentry);
34684 +       err = au_wbr_do_copyup_bu(dentry, btop);
34685 +       return err;
34686 +}
34687 +
34688 +/* ---------------------------------------------------------------------- */
34689 +
34690 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34691 +       [AuWbrCopyup_TDP] = {
34692 +               .copyup = au_wbr_copyup_tdp
34693 +       },
34694 +       [AuWbrCopyup_BUP] = {
34695 +               .copyup = au_wbr_copyup_bup
34696 +       },
34697 +       [AuWbrCopyup_BU] = {
34698 +               .copyup = au_wbr_copyup_bu
34699 +       }
34700 +};
34701 +
34702 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34703 +       [AuWbrCreate_TDP] = {
34704 +               .create = au_wbr_create_tdp
34705 +       },
34706 +       [AuWbrCreate_RR] = {
34707 +               .create = au_wbr_create_rr,
34708 +               .init   = au_wbr_create_init_rr
34709 +       },
34710 +       [AuWbrCreate_MFS] = {
34711 +               .create = au_wbr_create_mfs,
34712 +               .init   = au_wbr_create_init_mfs,
34713 +               .fin    = au_wbr_create_fin_mfs
34714 +       },
34715 +       [AuWbrCreate_MFSV] = {
34716 +               .create = au_wbr_create_mfs,
34717 +               .init   = au_wbr_create_init_mfs,
34718 +               .fin    = au_wbr_create_fin_mfs
34719 +       },
34720 +       [AuWbrCreate_MFSRR] = {
34721 +               .create = au_wbr_create_mfsrr,
34722 +               .init   = au_wbr_create_init_mfsrr,
34723 +               .fin    = au_wbr_create_fin_mfs
34724 +       },
34725 +       [AuWbrCreate_MFSRRV] = {
34726 +               .create = au_wbr_create_mfsrr,
34727 +               .init   = au_wbr_create_init_mfsrr,
34728 +               .fin    = au_wbr_create_fin_mfs
34729 +       },
34730 +       [AuWbrCreate_TDMFS] = {
34731 +               .create = au_wbr_create_tdmfs,
34732 +               .init   = au_wbr_create_init_mfs,
34733 +               .fin    = au_wbr_create_fin_mfs
34734 +       },
34735 +       [AuWbrCreate_TDMFSV] = {
34736 +               .create = au_wbr_create_tdmfs,
34737 +               .init   = au_wbr_create_init_mfs,
34738 +               .fin    = au_wbr_create_fin_mfs
34739 +       },
34740 +       [AuWbrCreate_PMFS] = {
34741 +               .create = au_wbr_create_pmfs,
34742 +               .init   = au_wbr_create_init_mfs,
34743 +               .fin    = au_wbr_create_fin_mfs
34744 +       },
34745 +       [AuWbrCreate_PMFSV] = {
34746 +               .create = au_wbr_create_pmfs,
34747 +               .init   = au_wbr_create_init_mfs,
34748 +               .fin    = au_wbr_create_fin_mfs
34749 +       },
34750 +       [AuWbrCreate_PMFSRR] = {
34751 +               .create = au_wbr_create_pmfsrr,
34752 +               .init   = au_wbr_create_init_mfsrr,
34753 +               .fin    = au_wbr_create_fin_mfs
34754 +       },
34755 +       [AuWbrCreate_PMFSRRV] = {
34756 +               .create = au_wbr_create_pmfsrr,
34757 +               .init   = au_wbr_create_init_mfsrr,
34758 +               .fin    = au_wbr_create_fin_mfs
34759 +       }
34760 +};
34761 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34762 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34763 +++ linux/fs/aufs/whout.c       2021-11-01 23:48:34.213025928 +0100
34764 @@ -0,0 +1,1072 @@
34765 +// SPDX-License-Identifier: GPL-2.0
34766 +/*
34767 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34768 + *
34769 + * This program, aufs is free software; you can redistribute it and/or modify
34770 + * it under the terms of the GNU General Public License as published by
34771 + * the Free Software Foundation; either version 2 of the License, or
34772 + * (at your option) any later version.
34773 + *
34774 + * This program is distributed in the hope that it will be useful,
34775 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34776 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34777 + * GNU General Public License for more details.
34778 + *
34779 + * You should have received a copy of the GNU General Public License
34780 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34781 + */
34782 +
34783 +/*
34784 + * whiteout for logical deletion and opaque directory
34785 + */
34786 +
34787 +#include "aufs.h"
34788 +
34789 +#define WH_MASK                        0444
34790 +
34791 +/*
34792 + * If a directory contains this file, then it is opaque.  We start with the
34793 + * .wh. flag so that it is blocked by lookup.
34794 + */
34795 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34796 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34797 +
34798 +/*
34799 + * generate whiteout name, which is NOT terminated by NULL.
34800 + * @name: original d_name.name
34801 + * @len: original d_name.len
34802 + * @wh: whiteout qstr
34803 + * returns zero when succeeds, otherwise error.
34804 + * succeeded value as wh->name should be freed by kfree().
34805 + */
34806 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34807 +{
34808 +       char *p;
34809 +
34810 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34811 +               return -ENAMETOOLONG;
34812 +
34813 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34814 +       p = kmalloc(wh->len, GFP_NOFS);
34815 +       wh->name = p;
34816 +       if (p) {
34817 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34818 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34819 +               /* smp_mb(); */
34820 +               return 0;
34821 +       }
34822 +       return -ENOMEM;
34823 +}
34824 +
34825 +/* ---------------------------------------------------------------------- */
34826 +
34827 +/*
34828 + * test if the @wh_name exists under @h_ppath.
34829 + * @try_sio specifies the necessary of super-io.
34830 + */
34831 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
34832 +              struct qstr *wh_name, int try_sio)
34833 +{
34834 +       int err;
34835 +       struct dentry *wh_dentry;
34836 +
34837 +       if (!try_sio)
34838 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
34839 +       else
34840 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
34841 +       err = PTR_ERR(wh_dentry);
34842 +       if (IS_ERR(wh_dentry)) {
34843 +               if (err == -ENAMETOOLONG)
34844 +                       err = 0;
34845 +               goto out;
34846 +       }
34847 +
34848 +       err = 0;
34849 +       if (d_is_negative(wh_dentry))
34850 +               goto out_wh; /* success */
34851 +
34852 +       err = 1;
34853 +       if (d_is_reg(wh_dentry))
34854 +               goto out_wh; /* success */
34855 +
34856 +       err = -EIO;
34857 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34858 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34859 +
34860 +out_wh:
34861 +       dput(wh_dentry);
34862 +out:
34863 +       return err;
34864 +}
34865 +
34866 +/*
34867 + * test if the @h_path->dentry sets opaque or not.
34868 + */
34869 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
34870 +{
34871 +       int err;
34872 +       struct inode *h_dir;
34873 +
34874 +       h_dir = d_inode(h_path->dentry);
34875 +       err = au_wh_test(h_userns, h_path, &diropq_name,
34876 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
34877 +       return err;
34878 +}
34879 +
34880 +/*
34881 + * returns a negative dentry whose name is unique and temporary.
34882 + */
34883 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34884 +                            struct qstr *prefix)
34885 +{
34886 +       struct dentry *dentry;
34887 +       int i;
34888 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34889 +               *name, *p;
34890 +       /* strict atomic_t is unnecessary here */
34891 +       static unsigned short cnt;
34892 +       struct qstr qs;
34893 +       struct path h_ppath;
34894 +       struct user_namespace *h_userns;
34895 +
34896 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34897 +
34898 +       name = defname;
34899 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34900 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34901 +               dentry = ERR_PTR(-ENAMETOOLONG);
34902 +               if (unlikely(qs.len > NAME_MAX))
34903 +                       goto out;
34904 +               dentry = ERR_PTR(-ENOMEM);
34905 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34906 +               if (unlikely(!name))
34907 +                       goto out;
34908 +       }
34909 +
34910 +       /* doubly whiteout-ed */
34911 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34912 +       p = name + AUFS_WH_PFX_LEN * 2;
34913 +       memcpy(p, prefix->name, prefix->len);
34914 +       p += prefix->len;
34915 +       *p++ = '.';
34916 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34917 +
34918 +       h_ppath.dentry = h_parent;
34919 +       h_ppath.mnt = au_br_mnt(br);
34920 +       h_userns = au_br_userns(br);
34921 +       qs.name = name;
34922 +       for (i = 0; i < 3; i++) {
34923 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34924 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
34925 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34926 +                       goto out_name;
34927 +               dput(dentry);
34928 +       }
34929 +       /* pr_warn("could not get random name\n"); */
34930 +       dentry = ERR_PTR(-EEXIST);
34931 +       AuDbg("%.*s\n", AuLNPair(&qs));
34932 +       BUG();
34933 +
34934 +out_name:
34935 +       if (name != defname)
34936 +               au_kfree_try_rcu(name);
34937 +out:
34938 +       AuTraceErrPtr(dentry);
34939 +       return dentry;
34940 +}
34941 +
34942 +/*
34943 + * rename the @h_dentry on @br to the whiteouted temporary name.
34944 + */
34945 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34946 +{
34947 +       int err;
34948 +       struct path h_path = {
34949 +               .mnt = au_br_mnt(br)
34950 +       };
34951 +       struct inode *h_dir, *delegated;
34952 +       struct dentry *h_parent;
34953 +
34954 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34955 +       h_dir = d_inode(h_parent);
34956 +       IMustLock(h_dir);
34957 +
34958 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34959 +       err = PTR_ERR(h_path.dentry);
34960 +       if (IS_ERR(h_path.dentry))
34961 +               goto out;
34962 +
34963 +       /* under the same dir, no need to lock_rename() */
34964 +       delegated = NULL;
34965 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34966 +                          /*flags*/0);
34967 +       AuTraceErr(err);
34968 +       if (unlikely(err == -EWOULDBLOCK)) {
34969 +               pr_warn("cannot retry for NFSv4 delegation"
34970 +                       " for an internal rename\n");
34971 +               iput(delegated);
34972 +       }
34973 +       dput(h_path.dentry);
34974 +
34975 +out:
34976 +       AuTraceErr(err);
34977 +       return err;
34978 +}
34979 +
34980 +/* ---------------------------------------------------------------------- */
34981 +/*
34982 + * functions for removing a whiteout
34983 + */
34984 +
34985 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34986 +{
34987 +       int err, force;
34988 +       struct inode *delegated;
34989 +
34990 +       /*
34991 +        * forces superio when the dir has a sticky bit.
34992 +        * this may be a violation of unix fs semantics.
34993 +        */
34994 +       force = (h_dir->i_mode & S_ISVTX)
34995 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34996 +       delegated = NULL;
34997 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
34998 +       if (unlikely(err == -EWOULDBLOCK)) {
34999 +               pr_warn("cannot retry for NFSv4 delegation"
35000 +                       " for an internal unlink\n");
35001 +               iput(delegated);
35002 +       }
35003 +       return err;
35004 +}
35005 +
35006 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35007 +                       struct dentry *dentry)
35008 +{
35009 +       int err;
35010 +
35011 +       err = do_unlink_wh(h_dir, h_path);
35012 +       if (!err && dentry)
35013 +               au_set_dbwh(dentry, -1);
35014 +
35015 +       return err;
35016 +}
35017 +
35018 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35019 +{
35020 +       int err;
35021 +       struct path h_path;
35022 +
35023 +       err = 0;
35024 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35025 +       if (IS_ERR(h_path.dentry))
35026 +               err = PTR_ERR(h_path.dentry);
35027 +       else {
35028 +               if (d_is_reg(h_path.dentry)) {
35029 +                       h_path.mnt = h_ppath->mnt;
35030 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35031 +               }
35032 +               dput(h_path.dentry);
35033 +       }
35034 +
35035 +       return err;
35036 +}
35037 +
35038 +/* ---------------------------------------------------------------------- */
35039 +/*
35040 + * initialize/clean whiteout for a branch
35041 + */
35042 +
35043 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35044 +                       const int isdir)
35045 +{
35046 +       int err;
35047 +       struct inode *delegated;
35048 +
35049 +       if (d_is_negative(whpath->dentry))
35050 +               return;
35051 +
35052 +       if (isdir)
35053 +               err = vfsub_rmdir(h_dir, whpath);
35054 +       else {
35055 +               delegated = NULL;
35056 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35057 +               if (unlikely(err == -EWOULDBLOCK)) {
35058 +                       pr_warn("cannot retry for NFSv4 delegation"
35059 +                               " for an internal unlink\n");
35060 +                       iput(delegated);
35061 +               }
35062 +       }
35063 +       if (unlikely(err))
35064 +               pr_warn("failed removing %pd (%d), ignored.\n",
35065 +                       whpath->dentry, err);
35066 +}
35067 +
35068 +static int test_linkable(struct dentry *h_root)
35069 +{
35070 +       struct inode *h_dir = d_inode(h_root);
35071 +
35072 +       if (h_dir->i_op->link)
35073 +               return 0;
35074 +
35075 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35076 +              h_root, au_sbtype(h_root->d_sb));
35077 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35078 +}
35079 +
35080 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35081 +static int au_whdir(struct inode *h_dir, struct path *path)
35082 +{
35083 +       int err;
35084 +
35085 +       err = -EEXIST;
35086 +       if (d_is_negative(path->dentry)) {
35087 +               int mode = 0700;
35088 +
35089 +               if (au_test_nfs(path->dentry->d_sb))
35090 +                       mode |= 0111;
35091 +               err = vfsub_mkdir(h_dir, path, mode);
35092 +       } else if (d_is_dir(path->dentry))
35093 +               err = 0;
35094 +       else
35095 +               pr_err("unknown %pd exists\n", path->dentry);
35096 +
35097 +       return err;
35098 +}
35099 +
35100 +struct au_wh_base {
35101 +       const struct qstr *name;
35102 +       struct dentry *dentry;
35103 +};
35104 +
35105 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35106 +                         struct path *h_path)
35107 +{
35108 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35109 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35110 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35111 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35112 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35113 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35114 +}
35115 +
35116 +/*
35117 + * returns tri-state,
35118 + * minus: error, caller should print the message
35119 + * zero: success
35120 + * plus: error, caller should NOT print the message
35121 + */
35122 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35123 +                               int do_plink, struct au_wh_base base[],
35124 +                               struct path *h_path)
35125 +{
35126 +       int err;
35127 +       struct inode *h_dir;
35128 +
35129 +       h_dir = d_inode(h_root);
35130 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35131 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35132 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35133 +       if (do_plink) {
35134 +               err = test_linkable(h_root);
35135 +               if (unlikely(err)) {
35136 +                       err = 1;
35137 +                       goto out;
35138 +               }
35139 +
35140 +               err = au_whdir(h_dir, h_path);
35141 +               if (unlikely(err))
35142 +                       goto out;
35143 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35144 +       } else
35145 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35146 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35147 +       err = au_whdir(h_dir, h_path);
35148 +       if (unlikely(err))
35149 +               goto out;
35150 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35151 +
35152 +out:
35153 +       return err;
35154 +}
35155 +
35156 +/*
35157 + * for the moment, aufs supports the branch filesystem which does not support
35158 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35159 + * copyup failed. finally, such filesystem will not be used as the writable
35160 + * branch.
35161 + *
35162 + * returns tri-state, see above.
35163 + */
35164 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35165 +                        int do_plink, struct au_wh_base base[],
35166 +                        struct path *h_path)
35167 +{
35168 +       int err;
35169 +       struct inode *h_dir;
35170 +
35171 +       WbrWhMustWriteLock(wbr);
35172 +
35173 +       err = test_linkable(h_root);
35174 +       if (unlikely(err)) {
35175 +               err = 1;
35176 +               goto out;
35177 +       }
35178 +
35179 +       /*
35180 +        * todo: should this create be done in /sbin/mount.aufs helper?
35181 +        */
35182 +       err = -EEXIST;
35183 +       h_dir = d_inode(h_root);
35184 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35185 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35186 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35187 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35188 +               err = 0;
35189 +       else
35190 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35191 +       if (unlikely(err))
35192 +               goto out;
35193 +
35194 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35195 +       if (do_plink) {
35196 +               err = au_whdir(h_dir, h_path);
35197 +               if (unlikely(err))
35198 +                       goto out;
35199 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35200 +       } else
35201 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35202 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35203 +
35204 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35205 +       err = au_whdir(h_dir, h_path);
35206 +       if (unlikely(err))
35207 +               goto out;
35208 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35209 +
35210 +out:
35211 +       return err;
35212 +}
35213 +
35214 +/*
35215 + * initialize the whiteout base file/dir for @br.
35216 + */
35217 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35218 +{
35219 +       int err, i;
35220 +       const unsigned char do_plink
35221 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35222 +       struct inode *h_dir;
35223 +       struct path path = br->br_path;
35224 +       struct dentry *h_root = path.dentry;
35225 +       struct au_wbr *wbr = br->br_wbr;
35226 +       static const struct qstr base_name[] = {
35227 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35228 +                                         sizeof(AUFS_BASE_NAME) - 1),
35229 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35230 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35231 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35232 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35233 +       };
35234 +       struct au_wh_base base[] = {
35235 +               [AuBrWh_BASE] = {
35236 +                       .name   = base_name + AuBrWh_BASE,
35237 +                       .dentry = NULL
35238 +               },
35239 +               [AuBrWh_PLINK] = {
35240 +                       .name   = base_name + AuBrWh_PLINK,
35241 +                       .dentry = NULL
35242 +               },
35243 +               [AuBrWh_ORPH] = {
35244 +                       .name   = base_name + AuBrWh_ORPH,
35245 +                       .dentry = NULL
35246 +               }
35247 +       };
35248 +
35249 +       if (wbr)
35250 +               WbrWhMustWriteLock(wbr);
35251 +
35252 +       for (i = 0; i < AuBrWh_Last; i++) {
35253 +               /* doubly whiteouted */
35254 +               struct dentry *d;
35255 +
35256 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35257 +               err = PTR_ERR(d);
35258 +               if (IS_ERR(d))
35259 +                       goto out;
35260 +
35261 +               base[i].dentry = d;
35262 +               AuDebugOn(wbr
35263 +                         && wbr->wbr_wh[i]
35264 +                         && wbr->wbr_wh[i] != base[i].dentry);
35265 +       }
35266 +
35267 +       if (wbr)
35268 +               for (i = 0; i < AuBrWh_Last; i++) {
35269 +                       dput(wbr->wbr_wh[i]);
35270 +                       wbr->wbr_wh[i] = NULL;
35271 +               }
35272 +
35273 +       err = 0;
35274 +       if (!au_br_writable(br->br_perm)) {
35275 +               h_dir = d_inode(h_root);
35276 +               au_wh_init_ro(h_dir, base, &path);
35277 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35278 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35279 +               if (err > 0)
35280 +                       goto out;
35281 +               else if (err)
35282 +                       goto out_err;
35283 +       } else {
35284 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35285 +               if (err > 0)
35286 +                       goto out;
35287 +               else if (err)
35288 +                       goto out_err;
35289 +       }
35290 +       goto out; /* success */
35291 +
35292 +out_err:
35293 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35294 +              err, h_root, au_sbtype(h_root->d_sb));
35295 +out:
35296 +       for (i = 0; i < AuBrWh_Last; i++)
35297 +               dput(base[i].dentry);
35298 +       return err;
35299 +}
35300 +
35301 +/* ---------------------------------------------------------------------- */
35302 +/*
35303 + * whiteouts are all hard-linked usually.
35304 + * when its link count reaches a ceiling, we create a new whiteout base
35305 + * asynchronously.
35306 + */
35307 +
35308 +struct reinit_br_wh {
35309 +       struct super_block *sb;
35310 +       struct au_branch *br;
35311 +};
35312 +
35313 +static void reinit_br_wh(void *arg)
35314 +{
35315 +       int err;
35316 +       aufs_bindex_t bindex;
35317 +       struct path h_path;
35318 +       struct reinit_br_wh *a = arg;
35319 +       struct au_wbr *wbr;
35320 +       struct inode *dir, *delegated;
35321 +       struct dentry *h_root;
35322 +       struct au_hinode *hdir;
35323 +
35324 +       err = 0;
35325 +       wbr = a->br->br_wbr;
35326 +       /* big aufs lock */
35327 +       si_noflush_write_lock(a->sb);
35328 +       if (!au_br_writable(a->br->br_perm))
35329 +               goto out;
35330 +       bindex = au_br_index(a->sb, a->br->br_id);
35331 +       if (unlikely(bindex < 0))
35332 +               goto out;
35333 +
35334 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35335 +       dir = d_inode(a->sb->s_root);
35336 +       hdir = au_hi(dir, bindex);
35337 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35338 +       AuDebugOn(h_root != au_br_dentry(a->br));
35339 +
35340 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35341 +       wbr_wh_write_lock(wbr);
35342 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35343 +                         h_root, a->br);
35344 +       if (!err) {
35345 +               h_path.dentry = wbr->wbr_whbase;
35346 +               h_path.mnt = au_br_mnt(a->br);
35347 +               delegated = NULL;
35348 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35349 +                                  /*force*/0);
35350 +               if (unlikely(err == -EWOULDBLOCK)) {
35351 +                       pr_warn("cannot retry for NFSv4 delegation"
35352 +                               " for an internal unlink\n");
35353 +                       iput(delegated);
35354 +               }
35355 +       } else {
35356 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35357 +               err = 0;
35358 +       }
35359 +       dput(wbr->wbr_whbase);
35360 +       wbr->wbr_whbase = NULL;
35361 +       if (!err)
35362 +               err = au_wh_init(a->br, a->sb);
35363 +       wbr_wh_write_unlock(wbr);
35364 +       au_hn_inode_unlock(hdir);
35365 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35366 +       if (!err)
35367 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35368 +
35369 +out:
35370 +       if (wbr)
35371 +               atomic_dec(&wbr->wbr_wh_running);
35372 +       au_lcnt_dec(&a->br->br_count);
35373 +       si_write_unlock(a->sb);
35374 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35375 +       au_kfree_rcu(a);
35376 +       if (unlikely(err))
35377 +               AuIOErr("err %d\n", err);
35378 +}
35379 +
35380 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35381 +{
35382 +       int do_dec, wkq_err;
35383 +       struct reinit_br_wh *arg;
35384 +
35385 +       do_dec = 1;
35386 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35387 +               goto out;
35388 +
35389 +       /* ignore ENOMEM */
35390 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35391 +       if (arg) {
35392 +               /*
35393 +                * dec(wh_running), kfree(arg) and dec(br_count)
35394 +                * in reinit function
35395 +                */
35396 +               arg->sb = sb;
35397 +               arg->br = br;
35398 +               au_lcnt_inc(&br->br_count);
35399 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35400 +               if (unlikely(wkq_err)) {
35401 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35402 +                       au_lcnt_dec(&br->br_count);
35403 +                       au_kfree_rcu(arg);
35404 +               }
35405 +               do_dec = 0;
35406 +       }
35407 +
35408 +out:
35409 +       if (do_dec)
35410 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35411 +}
35412 +
35413 +/* ---------------------------------------------------------------------- */
35414 +
35415 +/*
35416 + * create the whiteout @wh.
35417 + */
35418 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35419 +                            struct dentry *wh)
35420 +{
35421 +       int err;
35422 +       struct path h_path = {
35423 +               .dentry = wh
35424 +       };
35425 +       struct au_branch *br;
35426 +       struct au_wbr *wbr;
35427 +       struct dentry *h_parent;
35428 +       struct inode *h_dir, *delegated;
35429 +
35430 +       h_parent = wh->d_parent; /* dir inode is locked */
35431 +       h_dir = d_inode(h_parent);
35432 +       IMustLock(h_dir);
35433 +
35434 +       br = au_sbr(sb, bindex);
35435 +       h_path.mnt = au_br_mnt(br);
35436 +       wbr = br->br_wbr;
35437 +       wbr_wh_read_lock(wbr);
35438 +       if (wbr->wbr_whbase) {
35439 +               delegated = NULL;
35440 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35441 +               if (unlikely(err == -EWOULDBLOCK)) {
35442 +                       pr_warn("cannot retry for NFSv4 delegation"
35443 +                               " for an internal link\n");
35444 +                       iput(delegated);
35445 +               }
35446 +               if (!err || err != -EMLINK)
35447 +                       goto out;
35448 +
35449 +               /* link count full. re-initialize br_whbase. */
35450 +               kick_reinit_br_wh(sb, br);
35451 +       }
35452 +
35453 +       /* return this error in this context */
35454 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35455 +       if (!err)
35456 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35457 +
35458 +out:
35459 +       wbr_wh_read_unlock(wbr);
35460 +       return err;
35461 +}
35462 +
35463 +/* ---------------------------------------------------------------------- */
35464 +
35465 +/*
35466 + * create or remove the diropq.
35467 + */
35468 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35469 +                               unsigned int flags)
35470 +{
35471 +       struct dentry *opq_dentry;
35472 +       struct super_block *sb;
35473 +       struct au_branch *br;
35474 +       struct path h_path;
35475 +       int err;
35476 +
35477 +       sb = dentry->d_sb;
35478 +       br = au_sbr(sb, bindex);
35479 +       h_path.dentry = au_h_dptr(dentry, bindex);
35480 +       h_path.mnt = au_br_mnt(br);
35481 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35482 +       if (IS_ERR(opq_dentry))
35483 +               goto out;
35484 +
35485 +       if (au_ftest_diropq(flags, CREATE)) {
35486 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35487 +               if (!err) {
35488 +                       au_set_dbdiropq(dentry, bindex);
35489 +                       goto out; /* success */
35490 +               }
35491 +       } else {
35492 +               h_path.dentry = opq_dentry;
35493 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35494 +               if (!err)
35495 +                       au_set_dbdiropq(dentry, -1);
35496 +       }
35497 +       dput(opq_dentry);
35498 +       opq_dentry = ERR_PTR(err);
35499 +
35500 +out:
35501 +       return opq_dentry;
35502 +}
35503 +
35504 +struct do_diropq_args {
35505 +       struct dentry **errp;
35506 +       struct dentry *dentry;
35507 +       aufs_bindex_t bindex;
35508 +       unsigned int flags;
35509 +};
35510 +
35511 +static void call_do_diropq(void *args)
35512 +{
35513 +       struct do_diropq_args *a = args;
35514 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35515 +}
35516 +
35517 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35518 +                            unsigned int flags)
35519 +{
35520 +       struct dentry *diropq, *h_dentry;
35521 +       struct user_namespace *h_userns;
35522 +
35523 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35524 +       h_dentry = au_h_dptr(dentry, bindex);
35525 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35526 +                               MAY_EXEC | MAY_WRITE))
35527 +               diropq = do_diropq(dentry, bindex, flags);
35528 +       else {
35529 +               int wkq_err;
35530 +               struct do_diropq_args args = {
35531 +                       .errp           = &diropq,
35532 +                       .dentry         = dentry,
35533 +                       .bindex         = bindex,
35534 +                       .flags          = flags
35535 +               };
35536 +
35537 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35538 +               if (unlikely(wkq_err))
35539 +                       diropq = ERR_PTR(wkq_err);
35540 +       }
35541 +
35542 +       return diropq;
35543 +}
35544 +
35545 +/* ---------------------------------------------------------------------- */
35546 +
35547 +/*
35548 + * lookup whiteout dentry.
35549 + * @h_parent: lower parent dentry which must exist and be locked
35550 + * @base_name: name of dentry which will be whiteouted
35551 + * returns dentry for whiteout.
35552 + */
35553 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35554 +                         struct au_branch *br)
35555 +{
35556 +       int err;
35557 +       struct qstr wh_name;
35558 +       struct dentry *wh_dentry;
35559 +       struct path h_path;
35560 +
35561 +       err = au_wh_name_alloc(&wh_name, base_name);
35562 +       wh_dentry = ERR_PTR(err);
35563 +       if (!err) {
35564 +               h_path.dentry = h_parent;
35565 +               h_path.mnt = au_br_mnt(br);
35566 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35567 +               au_kfree_try_rcu(wh_name.name);
35568 +       }
35569 +       return wh_dentry;
35570 +}
35571 +
35572 +/*
35573 + * link/create a whiteout for @dentry on @bindex.
35574 + */
35575 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35576 +                           struct dentry *h_parent)
35577 +{
35578 +       struct dentry *wh_dentry;
35579 +       struct super_block *sb;
35580 +       int err;
35581 +
35582 +       sb = dentry->d_sb;
35583 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35584 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35585 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35586 +               if (!err) {
35587 +                       au_set_dbwh(dentry, bindex);
35588 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35589 +               } else {
35590 +                       dput(wh_dentry);
35591 +                       wh_dentry = ERR_PTR(err);
35592 +               }
35593 +       }
35594 +
35595 +       return wh_dentry;
35596 +}
35597 +
35598 +/* ---------------------------------------------------------------------- */
35599 +
35600 +/* Delete all whiteouts in this directory on branch bindex. */
35601 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35602 +                          aufs_bindex_t bindex)
35603 +{
35604 +       int err;
35605 +       unsigned long ul, n;
35606 +       struct qstr wh_name;
35607 +       char *p;
35608 +       struct hlist_head *head;
35609 +       struct au_vdir_wh *pos;
35610 +       struct au_vdir_destr *str;
35611 +
35612 +       err = -ENOMEM;
35613 +       p = (void *)__get_free_page(GFP_NOFS);
35614 +       wh_name.name = p;
35615 +       if (unlikely(!wh_name.name))
35616 +               goto out;
35617 +
35618 +       err = 0;
35619 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35620 +       p += AUFS_WH_PFX_LEN;
35621 +       n = whlist->nh_num;
35622 +       head = whlist->nh_head;
35623 +       for (ul = 0; !err && ul < n; ul++, head++) {
35624 +               hlist_for_each_entry(pos, head, wh_hash) {
35625 +                       if (pos->wh_bindex != bindex)
35626 +                               continue;
35627 +
35628 +                       str = &pos->wh_str;
35629 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35630 +                               memcpy(p, str->name, str->len);
35631 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35632 +                               err = unlink_wh_name(h_path, &wh_name);
35633 +                               if (!err)
35634 +                                       continue;
35635 +                               break;
35636 +                       }
35637 +                       AuIOErr("whiteout name too long %.*s\n",
35638 +                               str->len, str->name);
35639 +                       err = -EIO;
35640 +                       break;
35641 +               }
35642 +       }
35643 +       free_page((unsigned long)wh_name.name);
35644 +
35645 +out:
35646 +       return err;
35647 +}
35648 +
35649 +struct del_wh_children_args {
35650 +       int *errp;
35651 +       struct path *h_path;
35652 +       struct au_nhash *whlist;
35653 +       aufs_bindex_t bindex;
35654 +};
35655 +
35656 +static void call_del_wh_children(void *args)
35657 +{
35658 +       struct del_wh_children_args *a = args;
35659 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35660 +}
35661 +
35662 +/* ---------------------------------------------------------------------- */
35663 +
35664 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35665 +{
35666 +       struct au_whtmp_rmdir *whtmp;
35667 +       int err;
35668 +       unsigned int rdhash;
35669 +
35670 +       SiMustAnyLock(sb);
35671 +
35672 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35673 +       if (unlikely(!whtmp)) {
35674 +               whtmp = ERR_PTR(-ENOMEM);
35675 +               goto out;
35676 +       }
35677 +
35678 +       /* no estimation for dir size */
35679 +       rdhash = au_sbi(sb)->si_rdhash;
35680 +       if (!rdhash)
35681 +               rdhash = AUFS_RDHASH_DEF;
35682 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35683 +       if (unlikely(err)) {
35684 +               au_kfree_rcu(whtmp);
35685 +               whtmp = ERR_PTR(err);
35686 +       }
35687 +
35688 +out:
35689 +       return whtmp;
35690 +}
35691 +
35692 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35693 +{
35694 +       if (whtmp->br)
35695 +               au_lcnt_dec(&whtmp->br->br_count);
35696 +       dput(whtmp->wh_dentry);
35697 +       iput(whtmp->dir);
35698 +       au_nhash_wh_free(&whtmp->whlist);
35699 +       au_kfree_rcu(whtmp);
35700 +}
35701 +
35702 +/*
35703 + * rmdir the whiteouted temporary named dir @h_dentry.
35704 + * @whlist: whiteouted children.
35705 + */
35706 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35707 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35708 +{
35709 +       int err;
35710 +       unsigned int h_nlink;
35711 +       struct path wh_path;
35712 +       struct inode *wh_inode, *h_dir;
35713 +       struct au_branch *br;
35714 +       struct user_namespace *h_userns;
35715 +
35716 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35717 +       IMustLock(h_dir);
35718 +
35719 +       br = au_sbr(dir->i_sb, bindex);
35720 +       wh_path.dentry = wh_dentry;
35721 +       wh_path.mnt = au_br_mnt(br);
35722 +       h_userns = au_br_userns(br);
35723 +       wh_inode = d_inode(wh_dentry);
35724 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35725 +
35726 +       /*
35727 +        * someone else might change some whiteouts while we were sleeping.
35728 +        * it means this whlist may have an obsoleted entry.
35729 +        */
35730 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35731 +               err = del_wh_children(&wh_path, whlist, bindex);
35732 +       else {
35733 +               int wkq_err;
35734 +               struct del_wh_children_args args = {
35735 +                       .errp           = &err,
35736 +                       .h_path         = &wh_path,
35737 +                       .whlist         = whlist,
35738 +                       .bindex         = bindex
35739 +               };
35740 +
35741 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35742 +               if (unlikely(wkq_err))
35743 +                       err = wkq_err;
35744 +       }
35745 +       inode_unlock(wh_inode);
35746 +
35747 +       if (!err) {
35748 +               h_nlink = h_dir->i_nlink;
35749 +               err = vfsub_rmdir(h_dir, &wh_path);
35750 +               /* some fs doesn't change the parent nlink in some cases */
35751 +               h_nlink -= h_dir->i_nlink;
35752 +       }
35753 +
35754 +       if (!err) {
35755 +               if (au_ibtop(dir) == bindex) {
35756 +                       /* todo: dir->i_mutex is necessary */
35757 +                       au_cpup_attr_timesizes(dir);
35758 +                       if (h_nlink)
35759 +                               vfsub_drop_nlink(dir);
35760 +               }
35761 +               return 0; /* success */
35762 +       }
35763 +
35764 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35765 +       return err;
35766 +}
35767 +
35768 +static void call_rmdir_whtmp(void *args)
35769 +{
35770 +       int err;
35771 +       aufs_bindex_t bindex;
35772 +       struct au_whtmp_rmdir *a = args;
35773 +       struct super_block *sb;
35774 +       struct dentry *h_parent;
35775 +       struct inode *h_dir;
35776 +       struct au_hinode *hdir;
35777 +
35778 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35779 +       /* inode_lock(a->dir); */
35780 +       err = -EROFS;
35781 +       sb = a->dir->i_sb;
35782 +       si_read_lock(sb, !AuLock_FLUSH);
35783 +       if (!au_br_writable(a->br->br_perm))
35784 +               goto out;
35785 +       bindex = au_br_index(sb, a->br->br_id);
35786 +       if (unlikely(bindex < 0))
35787 +               goto out;
35788 +
35789 +       err = -EIO;
35790 +       ii_write_lock_parent(a->dir);
35791 +       h_parent = dget_parent(a->wh_dentry);
35792 +       h_dir = d_inode(h_parent);
35793 +       hdir = au_hi(a->dir, bindex);
35794 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35795 +       if (unlikely(err))
35796 +               goto out_mnt;
35797 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35798 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35799 +                         a->br);
35800 +       if (!err)
35801 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35802 +       au_hn_inode_unlock(hdir);
35803 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35804 +
35805 +out_mnt:
35806 +       dput(h_parent);
35807 +       ii_write_unlock(a->dir);
35808 +out:
35809 +       /* inode_unlock(a->dir); */
35810 +       au_whtmp_rmdir_free(a);
35811 +       si_read_unlock(sb);
35812 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35813 +       if (unlikely(err))
35814 +               AuIOErr("err %d\n", err);
35815 +}
35816 +
35817 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35818 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35819 +{
35820 +       int wkq_err;
35821 +       struct super_block *sb;
35822 +
35823 +       IMustLock(dir);
35824 +
35825 +       /* all post-process will be done in do_rmdir_whtmp(). */
35826 +       sb = dir->i_sb;
35827 +       args->dir = au_igrab(dir);
35828 +       args->br = au_sbr(sb, bindex);
35829 +       au_lcnt_inc(&args->br->br_count);
35830 +       args->wh_dentry = dget(wh_dentry);
35831 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35832 +       if (unlikely(wkq_err)) {
35833 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35834 +               au_whtmp_rmdir_free(args);
35835 +       }
35836 +}
35837 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35838 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35839 +++ linux/fs/aufs/whout.h       2021-11-01 23:48:34.213025928 +0100
35840 @@ -0,0 +1,87 @@
35841 +/* SPDX-License-Identifier: GPL-2.0 */
35842 +/*
35843 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35844 + *
35845 + * This program, aufs is free software; you can redistribute it and/or modify
35846 + * it under the terms of the GNU General Public License as published by
35847 + * the Free Software Foundation; either version 2 of the License, or
35848 + * (at your option) any later version.
35849 + *
35850 + * This program is distributed in the hope that it will be useful,
35851 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35852 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35853 + * GNU General Public License for more details.
35854 + *
35855 + * You should have received a copy of the GNU General Public License
35856 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35857 + */
35858 +
35859 +/*
35860 + * whiteout for logical deletion and opaque directory
35861 + */
35862 +
35863 +#ifndef __AUFS_WHOUT_H__
35864 +#define __AUFS_WHOUT_H__
35865 +
35866 +#ifdef __KERNEL__
35867 +
35868 +#include "dir.h"
35869 +
35870 +/* whout.c */
35871 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35872 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35873 +              struct qstr *wh_name, int try_sio);
35874 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
35875 +struct au_branch;
35876 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35877 +                            struct qstr *prefix);
35878 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35879 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35880 +                       struct dentry *dentry);
35881 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35882 +
35883 +/* diropq flags */
35884 +#define AuDiropq_CREATE        1
35885 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35886 +#define au_fset_diropq(flags, name) \
35887 +       do { (flags) |= AuDiropq_##name; } while (0)
35888 +#define au_fclr_diropq(flags, name) \
35889 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35890 +
35891 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35892 +                            unsigned int flags);
35893 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35894 +                         struct au_branch *br);
35895 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35896 +                           struct dentry *h_parent);
35897 +
35898 +/* real rmdir for the whiteout-ed dir */
35899 +struct au_whtmp_rmdir {
35900 +       struct inode *dir;
35901 +       struct au_branch *br;
35902 +       struct dentry *wh_dentry;
35903 +       struct au_nhash whlist;
35904 +};
35905 +
35906 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35907 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35908 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35909 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35910 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35911 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35912 +
35913 +/* ---------------------------------------------------------------------- */
35914 +
35915 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35916 +                                             aufs_bindex_t bindex)
35917 +{
35918 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35919 +}
35920 +
35921 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35922 +{
35923 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35924 +}
35925 +
35926 +#endif /* __KERNEL__ */
35927 +#endif /* __AUFS_WHOUT_H__ */
35928 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35929 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35930 +++ linux/fs/aufs/wkq.c 2021-11-01 23:48:34.213025928 +0100
35931 @@ -0,0 +1,372 @@
35932 +// SPDX-License-Identifier: GPL-2.0
35933 +/*
35934 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35935 + *
35936 + * This program, aufs is free software; you can redistribute it and/or modify
35937 + * it under the terms of the GNU General Public License as published by
35938 + * the Free Software Foundation; either version 2 of the License, or
35939 + * (at your option) any later version.
35940 + *
35941 + * This program is distributed in the hope that it will be useful,
35942 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35943 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35944 + * GNU General Public License for more details.
35945 + *
35946 + * You should have received a copy of the GNU General Public License
35947 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35948 + */
35949 +
35950 +/*
35951 + * workqueue for asynchronous/super-io operations
35952 + * todo: try new credential scheme
35953 + */
35954 +
35955 +#include <linux/module.h>
35956 +#include "aufs.h"
35957 +
35958 +/* internal workqueue named AUFS_WKQ_NAME */
35959 +
35960 +static struct workqueue_struct *au_wkq;
35961 +
35962 +struct au_wkinfo {
35963 +       struct work_struct wk;
35964 +       struct kobject *kobj;
35965 +
35966 +       unsigned int flags; /* see wkq.h */
35967 +
35968 +       au_wkq_func_t func;
35969 +       void *args;
35970 +
35971 +#ifdef CONFIG_LOCKDEP
35972 +       int dont_check;
35973 +       struct held_lock **hlock;
35974 +#endif
35975 +
35976 +       struct completion *comp;
35977 +};
35978 +
35979 +/* ---------------------------------------------------------------------- */
35980 +/*
35981 + * Aufs passes some operations to the workqueue such as the internal copyup.
35982 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35983 + * job run by workqueue depends upon the locks acquired in the other task.
35984 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35985 + * information too. And the job in the workqueue restores the info in order to
35986 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35987 + * correctly and expectedly.
35988 + */
35989 +
35990 +#ifndef CONFIG_LOCKDEP
35991 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35992 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35993 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35994 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35995 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35996 +#else
35997 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35998 +{
35999 +       wkinfo->hlock = NULL;
36000 +       wkinfo->dont_check = 0;
36001 +}
36002 +
36003 +/*
36004 + * 1: matched
36005 + * 0: unmatched
36006 + */
36007 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36008 +{
36009 +       static DEFINE_SPINLOCK(spin);
36010 +       static struct {
36011 +               char *name;
36012 +               struct lock_class_key *key;
36013 +       } a[] = {
36014 +               { .name = "&sbinfo->si_rwsem" },
36015 +               { .name = "&finfo->fi_rwsem" },
36016 +               { .name = "&dinfo->di_rwsem" },
36017 +               { .name = "&iinfo->ii_rwsem" }
36018 +       };
36019 +       static int set;
36020 +       int i;
36021 +
36022 +       /* lockless read from 'set.' see below */
36023 +       if (set == ARRAY_SIZE(a)) {
36024 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36025 +                       if (a[i].key == key)
36026 +                               goto match;
36027 +               goto unmatch;
36028 +       }
36029 +
36030 +       spin_lock(&spin);
36031 +       if (set)
36032 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36033 +                       if (a[i].key == key) {
36034 +                               spin_unlock(&spin);
36035 +                               goto match;
36036 +                       }
36037 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36038 +               if (a[i].key) {
36039 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36040 +                               spin_unlock(&spin);
36041 +                               goto match;
36042 +                       } else
36043 +                               continue;
36044 +               }
36045 +               if (strstr(a[i].name, name)) {
36046 +                       /*
36047 +                        * the order of these three lines is important for the
36048 +                        * lockless read above.
36049 +                        */
36050 +                       a[i].key = key;
36051 +                       spin_unlock(&spin);
36052 +                       set++;
36053 +                       /* AuDbg("%d, %s\n", set, name); */
36054 +                       goto match;
36055 +               }
36056 +       }
36057 +       spin_unlock(&spin);
36058 +       goto unmatch;
36059 +
36060 +match:
36061 +       return 1;
36062 +unmatch:
36063 +       return 0;
36064 +}
36065 +
36066 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36067 +{
36068 +       int err, n;
36069 +       struct task_struct *curr;
36070 +       struct held_lock **hl, *held_locks, *p;
36071 +
36072 +       err = 0;
36073 +       curr = current;
36074 +       wkinfo->dont_check = lockdep_recursing(curr);
36075 +       if (wkinfo->dont_check)
36076 +               goto out;
36077 +       n = curr->lockdep_depth;
36078 +       if (!n)
36079 +               goto out;
36080 +
36081 +       err = -ENOMEM;
36082 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36083 +       if (unlikely(!wkinfo->hlock))
36084 +               goto out;
36085 +
36086 +       err = 0;
36087 +#if 0 /* left for debugging */
36088 +       if (0 && au_debug_test())
36089 +               lockdep_print_held_locks(curr);
36090 +#endif
36091 +       held_locks = curr->held_locks;
36092 +       hl = wkinfo->hlock;
36093 +       while (n--) {
36094 +               p = held_locks++;
36095 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36096 +                       *hl++ = p;
36097 +       }
36098 +       *hl = NULL;
36099 +
36100 +out:
36101 +       return err;
36102 +}
36103 +
36104 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36105 +{
36106 +       au_kfree_try_rcu(wkinfo->hlock);
36107 +}
36108 +
36109 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36110 +{
36111 +       struct held_lock *p, **hl = wkinfo->hlock;
36112 +       int subclass;
36113 +
36114 +       if (wkinfo->dont_check)
36115 +               lockdep_off();
36116 +       if (!hl)
36117 +               return;
36118 +       while ((p = *hl++)) { /* assignment */
36119 +               subclass = lockdep_hlock_class(p)->subclass;
36120 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36121 +               if (p->read)
36122 +                       rwsem_acquire_read(p->instance, subclass, 0,
36123 +                                          /*p->acquire_ip*/_RET_IP_);
36124 +               else
36125 +                       rwsem_acquire(p->instance, subclass, 0,
36126 +                                     /*p->acquire_ip*/_RET_IP_);
36127 +       }
36128 +}
36129 +
36130 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36131 +{
36132 +       struct held_lock *p, **hl = wkinfo->hlock;
36133 +
36134 +       if (wkinfo->dont_check)
36135 +               lockdep_on();
36136 +       if (!hl)
36137 +               return;
36138 +       while ((p = *hl++)) /* assignment */
36139 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36140 +}
36141 +#endif
36142 +
36143 +static void wkq_func(struct work_struct *wk)
36144 +{
36145 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36146 +
36147 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36148 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36149 +
36150 +       au_wkq_lockdep_pre(wkinfo);
36151 +       wkinfo->func(wkinfo->args);
36152 +       au_wkq_lockdep_post(wkinfo);
36153 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36154 +               complete(wkinfo->comp);
36155 +       else {
36156 +               kobject_put(wkinfo->kobj);
36157 +               module_put(THIS_MODULE); /* todo: ?? */
36158 +               au_kfree_rcu(wkinfo);
36159 +       }
36160 +}
36161 +
36162 +/*
36163 + * Since struct completion is large, try allocating it dynamically.
36164 + */
36165 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36166 +
36167 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36168 +{
36169 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36170 +       if (*comp) {
36171 +               init_completion(*comp);
36172 +               wkinfo->comp = *comp;
36173 +               return 0;
36174 +       }
36175 +       return -ENOMEM;
36176 +}
36177 +
36178 +static void au_wkq_comp_free(struct completion *comp)
36179 +{
36180 +       au_kfree_rcu(comp);
36181 +}
36182 +
36183 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36184 +{
36185 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36186 +               if (au_wkq_test()) {
36187 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36188 +                               " due to a dead dir by UDBA,"
36189 +                               " or async xino write?\n");
36190 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36191 +               }
36192 +       } else
36193 +               au_dbg_verify_kthread();
36194 +
36195 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36196 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36197 +               queue_work(au_wkq, &wkinfo->wk);
36198 +       } else {
36199 +               INIT_WORK(&wkinfo->wk, wkq_func);
36200 +               schedule_work(&wkinfo->wk);
36201 +       }
36202 +}
36203 +
36204 +/*
36205 + * Be careful. It is easy to make deadlock happen.
36206 + * processA: lock, wkq and wait
36207 + * processB: wkq and wait, lock in wkq
36208 + * --> deadlock
36209 + */
36210 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36211 +{
36212 +       int err;
36213 +       AuWkqCompDeclare(comp);
36214 +       struct au_wkinfo wkinfo = {
36215 +               .flags  = flags,
36216 +               .func   = func,
36217 +               .args   = args
36218 +       };
36219 +
36220 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36221 +       if (unlikely(err))
36222 +               goto out;
36223 +       err = au_wkq_lockdep_alloc(&wkinfo);
36224 +       if (unlikely(err))
36225 +               goto out_comp;
36226 +       if (!err) {
36227 +               au_wkq_run(&wkinfo);
36228 +               /* no timeout, no interrupt */
36229 +               wait_for_completion(wkinfo.comp);
36230 +       }
36231 +       au_wkq_lockdep_free(&wkinfo);
36232 +
36233 +out_comp:
36234 +       au_wkq_comp_free(comp);
36235 +out:
36236 +       destroy_work_on_stack(&wkinfo.wk);
36237 +       return err;
36238 +}
36239 +
36240 +/*
36241 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36242 + * problem in a concurrent umounting.
36243 + */
36244 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36245 +                 unsigned int flags)
36246 +{
36247 +       int err;
36248 +       struct au_wkinfo *wkinfo;
36249 +
36250 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36251 +
36252 +       /*
36253 +        * wkq_func() must free this wkinfo.
36254 +        * it highly depends upon the implementation of workqueue.
36255 +        */
36256 +       err = 0;
36257 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36258 +       if (wkinfo) {
36259 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36260 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36261 +               wkinfo->func = func;
36262 +               wkinfo->args = args;
36263 +               wkinfo->comp = NULL;
36264 +               au_wkq_lockdep_init(wkinfo);
36265 +               kobject_get(wkinfo->kobj);
36266 +               __module_get(THIS_MODULE); /* todo: ?? */
36267 +
36268 +               au_wkq_run(wkinfo);
36269 +       } else {
36270 +               err = -ENOMEM;
36271 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36272 +       }
36273 +
36274 +       return err;
36275 +}
36276 +
36277 +/* ---------------------------------------------------------------------- */
36278 +
36279 +void au_nwt_init(struct au_nowait_tasks *nwt)
36280 +{
36281 +       atomic_set(&nwt->nw_len, 0);
36282 +       /* smp_mb(); */ /* atomic_set */
36283 +       init_waitqueue_head(&nwt->nw_wq);
36284 +}
36285 +
36286 +void au_wkq_fin(void)
36287 +{
36288 +       destroy_workqueue(au_wkq);
36289 +}
36290 +
36291 +int __init au_wkq_init(void)
36292 +{
36293 +       int err;
36294 +
36295 +       err = 0;
36296 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36297 +       if (IS_ERR(au_wkq))
36298 +               err = PTR_ERR(au_wkq);
36299 +       else if (!au_wkq)
36300 +               err = -ENOMEM;
36301 +
36302 +       return err;
36303 +}
36304 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36305 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36306 +++ linux/fs/aufs/wkq.h 2021-11-01 23:48:34.213025928 +0100
36307 @@ -0,0 +1,89 @@
36308 +/* SPDX-License-Identifier: GPL-2.0 */
36309 +/*
36310 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36311 + *
36312 + * This program, aufs is free software; you can redistribute it and/or modify
36313 + * it under the terms of the GNU General Public License as published by
36314 + * the Free Software Foundation; either version 2 of the License, or
36315 + * (at your option) any later version.
36316 + *
36317 + * This program is distributed in the hope that it will be useful,
36318 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36319 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36320 + * GNU General Public License for more details.
36321 + *
36322 + * You should have received a copy of the GNU General Public License
36323 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36324 + */
36325 +
36326 +/*
36327 + * workqueue for asynchronous/super-io operations
36328 + * todo: try new credentials management scheme
36329 + */
36330 +
36331 +#ifndef __AUFS_WKQ_H__
36332 +#define __AUFS_WKQ_H__
36333 +
36334 +#ifdef __KERNEL__
36335 +
36336 +#include <linux/wait.h>
36337 +
36338 +struct super_block;
36339 +
36340 +/* ---------------------------------------------------------------------- */
36341 +
36342 +/*
36343 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36344 + */
36345 +struct au_nowait_tasks {
36346 +       atomic_t                nw_len;
36347 +       wait_queue_head_t       nw_wq;
36348 +};
36349 +
36350 +/* ---------------------------------------------------------------------- */
36351 +
36352 +typedef void (*au_wkq_func_t)(void *args);
36353 +
36354 +/* wkq flags */
36355 +#define AuWkq_WAIT     1
36356 +#define AuWkq_NEST     (1 << 1)
36357 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36358 +#define au_fset_wkq(flags, name) \
36359 +       do { (flags) |= AuWkq_##name; } while (0)
36360 +#define au_fclr_wkq(flags, name) \
36361 +       do { (flags) &= ~AuWkq_##name; } while (0)
36362 +
36363 +/* wkq.c */
36364 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36365 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36366 +                 unsigned int flags);
36367 +void au_nwt_init(struct au_nowait_tasks *nwt);
36368 +int __init au_wkq_init(void);
36369 +void au_wkq_fin(void);
36370 +
36371 +/* ---------------------------------------------------------------------- */
36372 +
36373 +static inline int au_wkq_test(void)
36374 +{
36375 +       return current->flags & PF_WQ_WORKER;
36376 +}
36377 +
36378 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36379 +{
36380 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36381 +}
36382 +
36383 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36384 +{
36385 +       if (atomic_dec_and_test(&nwt->nw_len))
36386 +               wake_up_all(&nwt->nw_wq);
36387 +}
36388 +
36389 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36390 +{
36391 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36392 +       return 0;
36393 +}
36394 +
36395 +#endif /* __KERNEL__ */
36396 +#endif /* __AUFS_WKQ_H__ */
36397 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36398 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36399 +++ linux/fs/aufs/xattr.c       2021-11-01 23:48:34.213025928 +0100
36400 @@ -0,0 +1,368 @@
36401 +// SPDX-License-Identifier: GPL-2.0
36402 +/*
36403 + * Copyright (C) 2014-2021 Junjiro R. Okajima
36404 + *
36405 + * This program, aufs is free software; you can redistribute it and/or modify
36406 + * it under the terms of the GNU General Public License as published by
36407 + * the Free Software Foundation; either version 2 of the License, or
36408 + * (at your option) any later version.
36409 + *
36410 + * This program is distributed in the hope that it will be useful,
36411 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36412 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36413 + * GNU General Public License for more details.
36414 + *
36415 + * You should have received a copy of the GNU General Public License
36416 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36417 + */
36418 +
36419 +/*
36420 + * handling xattr functions
36421 + */
36422 +
36423 +#include <linux/fs.h>
36424 +#include <linux/posix_acl_xattr.h>
36425 +#include <linux/xattr.h>
36426 +#include "aufs.h"
36427 +
36428 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36429 +{
36430 +       if (!ignore_flags)
36431 +               goto out;
36432 +       switch (err) {
36433 +       case -ENOMEM:
36434 +       case -EDQUOT:
36435 +               goto out;
36436 +       }
36437 +
36438 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36439 +               err = 0;
36440 +               goto out;
36441 +       }
36442 +
36443 +#define cmp(brattr, prefix) do {                                       \
36444 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36445 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36446 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36447 +                               err = 0;                                \
36448 +                       goto out;                                       \
36449 +               }                                                       \
36450 +       } while (0)
36451 +
36452 +       cmp(SEC, SECURITY);
36453 +       cmp(SYS, SYSTEM);
36454 +       cmp(TR, TRUSTED);
36455 +       cmp(USR, USER);
36456 +#undef cmp
36457 +
36458 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36459 +               err = 0;
36460 +
36461 +out:
36462 +       return err;
36463 +}
36464 +
36465 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36466 +
36467 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36468 +                           char *name, char **buf, unsigned int ignore_flags,
36469 +                           unsigned int verbose)
36470 +{
36471 +       int err;
36472 +       ssize_t ssz;
36473 +       struct inode *h_idst;
36474 +       struct dentry *h_dst_dentry, *h_src_dentry;
36475 +       struct user_namespace *h_dst_userns, *h_src_userns;
36476 +
36477 +       h_src_userns = mnt_user_ns(h_src->mnt);
36478 +       h_src_dentry = h_src->dentry;
36479 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36480 +                                GFP_NOFS);
36481 +       err = ssz;
36482 +       if (unlikely(err <= 0)) {
36483 +               if (err == -ENODATA
36484 +                   || (err == -EOPNOTSUPP
36485 +                       && ((ignore_flags & au_xattr_out_of_list)
36486 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36487 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36488 +                                   || !strcmp(name,
36489 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36490 +                           ))
36491 +                       err = 0;
36492 +               if (err && (verbose || au_debug_test()))
36493 +                       pr_err("%s, err %d\n", name, err);
36494 +               goto out;
36495 +       }
36496 +
36497 +       /* unlock it temporary */
36498 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36499 +       h_dst_dentry = h_dst->dentry;
36500 +       h_idst = d_inode(h_dst_dentry);
36501 +       inode_unlock(h_idst);
36502 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36503 +                            /*flags*/0);
36504 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36505 +       if (unlikely(err)) {
36506 +               if (verbose || au_debug_test())
36507 +                       pr_err("%s, err %d\n", name, err);
36508 +               err = au_xattr_ignore(err, name, ignore_flags);
36509 +       }
36510 +
36511 +out:
36512 +       return err;
36513 +}
36514 +
36515 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36516 +                 unsigned int verbose)
36517 +{
36518 +       int err, unlocked, acl_access, acl_default;
36519 +       ssize_t ssz;
36520 +       struct dentry *h_dst_dentry, *h_src_dentry;
36521 +       struct inode *h_isrc, *h_idst;
36522 +       char *value, *p, *o, *e;
36523 +
36524 +       /* try stopping to update the source inode while we are referencing */
36525 +       /* there should not be the parent-child relationship between them */
36526 +       h_dst_dentry = h_dst->dentry;
36527 +       h_idst = d_inode(h_dst_dentry);
36528 +       h_src_dentry = h_src->dentry;
36529 +       h_isrc = d_inode(h_src_dentry);
36530 +       inode_unlock(h_idst);
36531 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36532 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36533 +       unlocked = 0;
36534 +
36535 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36536 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36537 +       err = ssz;
36538 +       if (unlikely(err < 0)) {
36539 +               AuTraceErr(err);
36540 +               if (err == -ENODATA
36541 +                   || err == -EOPNOTSUPP)
36542 +                       err = 0;        /* ignore */
36543 +               goto out;
36544 +       }
36545 +
36546 +       err = 0;
36547 +       p = NULL;
36548 +       o = NULL;
36549 +       if (ssz) {
36550 +               err = -ENOMEM;
36551 +               p = kmalloc(ssz, GFP_NOFS);
36552 +               o = p;
36553 +               if (unlikely(!p))
36554 +                       goto out;
36555 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36556 +       }
36557 +       inode_unlock_shared(h_isrc);
36558 +       unlocked = 1;
36559 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36560 +       if (unlikely(err < 0))
36561 +               goto out_free;
36562 +
36563 +       err = 0;
36564 +       e = p + ssz;
36565 +       value = NULL;
36566 +       acl_access = 0;
36567 +       acl_default = 0;
36568 +       while (!err && p < e) {
36569 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36570 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36571 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36572 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36573 +                                       - 1);
36574 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36575 +                                      verbose);
36576 +               p += strlen(p) + 1;
36577 +       }
36578 +       AuTraceErr(err);
36579 +       ignore_flags |= au_xattr_out_of_list;
36580 +       if (!err && !acl_access) {
36581 +               err = au_do_cpup_xattr(h_dst, h_src,
36582 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36583 +                                      ignore_flags, verbose);
36584 +               AuTraceErr(err);
36585 +       }
36586 +       if (!err && !acl_default) {
36587 +               err = au_do_cpup_xattr(h_dst, h_src,
36588 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36589 +                                      ignore_flags, verbose);
36590 +               AuTraceErr(err);
36591 +       }
36592 +
36593 +       au_kfree_try_rcu(value);
36594 +
36595 +out_free:
36596 +       au_kfree_try_rcu(o);
36597 +out:
36598 +       if (!unlocked)
36599 +               inode_unlock_shared(h_isrc);
36600 +       AuTraceErr(err);
36601 +       return err;
36602 +}
36603 +
36604 +/* ---------------------------------------------------------------------- */
36605 +
36606 +static int au_smack_reentering(struct super_block *sb)
36607 +{
36608 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36609 +       /*
36610 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36611 +        * i_op->getxattr(). ouch.
36612 +        */
36613 +       return si_pid_test(sb);
36614 +#else
36615 +       return 0;
36616 +#endif
36617 +}
36618 +
36619 +enum {
36620 +       AU_XATTR_LIST,
36621 +       AU_XATTR_GET
36622 +};
36623 +
36624 +struct au_lgxattr {
36625 +       int type;
36626 +       union {
36627 +               struct {
36628 +                       char    *list;
36629 +                       size_t  size;
36630 +               } list;
36631 +               struct {
36632 +                       const char      *name;
36633 +                       void            *value;
36634 +                       size_t          size;
36635 +               } get;
36636 +       } u;
36637 +};
36638 +
36639 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36640 +                         struct au_lgxattr *arg)
36641 +{
36642 +       ssize_t err;
36643 +       int reenter;
36644 +       struct path h_path;
36645 +       struct super_block *sb;
36646 +
36647 +       sb = dentry->d_sb;
36648 +       reenter = au_smack_reentering(sb);
36649 +       if (!reenter) {
36650 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36651 +               if (unlikely(err))
36652 +                       goto out;
36653 +       }
36654 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36655 +       if (unlikely(err))
36656 +               goto out_si;
36657 +       if (unlikely(!h_path.dentry))
36658 +               /* illegally overlapped or something */
36659 +               goto out_di; /* pretending success */
36660 +
36661 +       /* always topmost entry only */
36662 +       switch (arg->type) {
36663 +       case AU_XATTR_LIST:
36664 +               err = vfs_listxattr(h_path.dentry,
36665 +                                   arg->u.list.list, arg->u.list.size);
36666 +               break;
36667 +       case AU_XATTR_GET:
36668 +               AuDebugOn(d_is_negative(h_path.dentry));
36669 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36670 +                                  arg->u.get.name, arg->u.get.value,
36671 +                                  arg->u.get.size);
36672 +               break;
36673 +       }
36674 +
36675 +out_di:
36676 +       if (!reenter)
36677 +               di_read_unlock(dentry, AuLock_IR);
36678 +out_si:
36679 +       if (!reenter)
36680 +               si_read_unlock(sb);
36681 +out:
36682 +       AuTraceErr(err);
36683 +       return err;
36684 +}
36685 +
36686 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36687 +{
36688 +       struct au_lgxattr arg = {
36689 +               .type = AU_XATTR_LIST,
36690 +               .u.list = {
36691 +                       .list   = list,
36692 +                       .size   = size
36693 +               },
36694 +       };
36695 +
36696 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36697 +}
36698 +
36699 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36700 +                          const char *name, void *value, size_t size)
36701 +{
36702 +       struct au_lgxattr arg = {
36703 +               .type = AU_XATTR_GET,
36704 +               .u.get = {
36705 +                       .name   = name,
36706 +                       .value  = value,
36707 +                       .size   = size
36708 +               },
36709 +       };
36710 +
36711 +       return au_lgxattr(dentry, inode, &arg);
36712 +}
36713 +
36714 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36715 +                      const char *name, const void *value, size_t size,
36716 +                      int flags)
36717 +{
36718 +       struct au_sxattr arg = {
36719 +               .type = AU_XATTR_SET,
36720 +               .u.set = {
36721 +                       .name   = name,
36722 +                       .value  = value,
36723 +                       .size   = size,
36724 +                       .flags  = flags
36725 +               },
36726 +       };
36727 +
36728 +       return au_sxattr(dentry, inode, &arg);
36729 +}
36730 +
36731 +/* ---------------------------------------------------------------------- */
36732 +
36733 +static int au_xattr_get(const struct xattr_handler *handler,
36734 +                       struct dentry *dentry, struct inode *inode,
36735 +                       const char *name, void *buffer, size_t size)
36736 +{
36737 +       return au_getxattr(dentry, inode, name, buffer, size);
36738 +}
36739 +
36740 +static int au_xattr_set(const struct xattr_handler *handler,
36741 +                       struct user_namespace *userns,
36742 +                       struct dentry *dentry, struct inode *inode,
36743 +                       const char *name, const void *value, size_t size,
36744 +                       int flags)
36745 +{
36746 +       return au_setxattr(dentry, inode, name, value, size, flags);
36747 +}
36748 +
36749 +static const struct xattr_handler au_xattr_handler = {
36750 +       .name   = "",
36751 +       .prefix = "",
36752 +       .get    = au_xattr_get,
36753 +       .set    = au_xattr_set
36754 +};
36755 +
36756 +static const struct xattr_handler *au_xattr_handlers[] = {
36757 +#ifdef CONFIG_FS_POSIX_ACL
36758 +       &posix_acl_access_xattr_handler,
36759 +       &posix_acl_default_xattr_handler,
36760 +#endif
36761 +       &au_xattr_handler, /* must be last */
36762 +       NULL
36763 +};
36764 +
36765 +void au_xattr_init(struct super_block *sb)
36766 +{
36767 +       sb->s_xattr = au_xattr_handlers;
36768 +}
36769 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36770 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36771 +++ linux/fs/aufs/xino.c        2021-11-01 23:48:34.213025928 +0100
36772 @@ -0,0 +1,1926 @@
36773 +// SPDX-License-Identifier: GPL-2.0
36774 +/*
36775 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36776 + *
36777 + * This program, aufs is free software; you can redistribute it and/or modify
36778 + * it under the terms of the GNU General Public License as published by
36779 + * the Free Software Foundation; either version 2 of the License, or
36780 + * (at your option) any later version.
36781 + *
36782 + * This program is distributed in the hope that it will be useful,
36783 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36784 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36785 + * GNU General Public License for more details.
36786 + *
36787 + * You should have received a copy of the GNU General Public License
36788 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36789 + */
36790 +
36791 +/*
36792 + * external inode number translation table and bitmap
36793 + *
36794 + * things to consider
36795 + * - the lifetime
36796 + *   + au_xino object
36797 + *   + XINO files (xino, xib, xigen)
36798 + *   + dynamic debugfs entries (xiN)
36799 + *   + static debugfs entries (xib, xigen)
36800 + *   + static sysfs entry (xi_path)
36801 + * - several entry points to handle them.
36802 + *   + mount(2) without xino option (default)
36803 + *   + mount(2) with xino option
36804 + *   + mount(2) with noxino option
36805 + *   + umount(2)
36806 + *   + remount with add/del branches
36807 + *   + remount with xino/noxino options
36808 + */
36809 +
36810 +#include <linux/seq_file.h>
36811 +#include <linux/statfs.h>
36812 +#include "aufs.h"
36813 +
36814 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36815 +                                    aufs_bindex_t bbot,
36816 +                                    struct super_block *h_sb)
36817 +{
36818 +       /* todo: try binary-search if the branches are many */
36819 +       for (; btop <= bbot; btop++)
36820 +               if (h_sb == au_sbr_sb(sb, btop))
36821 +                       return btop;
36822 +       return -1;
36823 +}
36824 +
36825 +/*
36826 + * find another branch who is on the same filesystem of the specified
36827 + * branch{@btgt}. search until @bbot.
36828 + */
36829 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36830 +                                 aufs_bindex_t bbot)
36831 +{
36832 +       aufs_bindex_t bindex;
36833 +       struct super_block *tgt_sb;
36834 +
36835 +       tgt_sb = au_sbr_sb(sb, btgt);
36836 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36837 +       if (bindex < 0)
36838 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36839 +
36840 +       return bindex;
36841 +}
36842 +
36843 +/* ---------------------------------------------------------------------- */
36844 +
36845 +/*
36846 + * stop unnecessary notify events at creating xino files
36847 + */
36848 +
36849 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36850 +{
36851 +       aufs_bindex_t bfound, bindex, bbot;
36852 +       struct dentry *parent;
36853 +       struct au_branch *br;
36854 +
36855 +       bfound = -1;
36856 +       parent = dentry->d_parent; /* safe d_parent access */
36857 +       bbot = au_sbbot(sb);
36858 +       for (bindex = 0; bindex <= bbot; bindex++) {
36859 +               br = au_sbr(sb, bindex);
36860 +               if (au_br_dentry(br) == parent) {
36861 +                       bfound = bindex;
36862 +                       break;
36863 +               }
36864 +       }
36865 +
36866 +       AuDbg("bfound b%d\n", bfound);
36867 +       return bfound;
36868 +}
36869 +
36870 +struct au_xino_lock_dir {
36871 +       struct au_hinode *hdir;
36872 +       struct dentry *parent;
36873 +       struct inode *dir;
36874 +};
36875 +
36876 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36877 +                                         unsigned int lsc)
36878 +{
36879 +       struct dentry *parent;
36880 +       struct inode *dir;
36881 +
36882 +       parent = dget_parent(dentry);
36883 +       dir = d_inode(parent);
36884 +       inode_lock_nested(dir, lsc);
36885 +#if 0 /* it should not happen */
36886 +       spin_lock(&dentry->d_lock);
36887 +       if (unlikely(dentry->d_parent != parent)) {
36888 +               spin_unlock(&dentry->d_lock);
36889 +               inode_unlock(dir);
36890 +               dput(parent);
36891 +               parent = NULL;
36892 +               goto out;
36893 +       }
36894 +       spin_unlock(&dentry->d_lock);
36895 +
36896 +out:
36897 +#endif
36898 +       return parent;
36899 +}
36900 +
36901 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36902 +                            struct au_xino_lock_dir *ldir)
36903 +{
36904 +       aufs_bindex_t bindex;
36905 +
36906 +       ldir->hdir = NULL;
36907 +       bindex = au_xi_root(sb, xipath->dentry);
36908 +       if (bindex >= 0) {
36909 +               /* rw branch root */
36910 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36911 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36912 +       } else {
36913 +               /* other */
36914 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36915 +                                                  AuLsc_I_PARENT);
36916 +               ldir->dir = d_inode(ldir->parent);
36917 +       }
36918 +}
36919 +
36920 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36921 +{
36922 +       if (ldir->hdir)
36923 +               au_hn_inode_unlock(ldir->hdir);
36924 +       else {
36925 +               inode_unlock(ldir->dir);
36926 +               dput(ldir->parent);
36927 +       }
36928 +}
36929 +
36930 +/* ---------------------------------------------------------------------- */
36931 +
36932 +/*
36933 + * create and set a new xino file
36934 + */
36935 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36936 +                           int wbrtop)
36937 +{
36938 +       struct file *file;
36939 +       struct dentry *h_parent, *d;
36940 +       struct inode *h_dir, *inode;
36941 +       int err;
36942 +       static DEFINE_MUTEX(mtx);
36943 +
36944 +       /*
36945 +        * at mount-time, and the xino file is the default path,
36946 +        * hnotify is disabled so we have no notify events to ignore.
36947 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36948 +        */
36949 +       if (!wbrtop)
36950 +               mutex_lock(&mtx);
36951 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36952 +                              /* | __FMODE_NONOTIFY */,
36953 +                              0666);
36954 +       if (IS_ERR(file)) {
36955 +               if (!wbrtop)
36956 +                       mutex_unlock(&mtx);
36957 +               if (!silent)
36958 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36959 +               return file;
36960 +       }
36961 +
36962 +       /* keep file count */
36963 +       err = 0;
36964 +       d = file->f_path.dentry;
36965 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36966 +       if (!wbrtop)
36967 +               mutex_unlock(&mtx);
36968 +       /* mnt_want_write() is unnecessary here */
36969 +       h_dir = d_inode(h_parent);
36970 +       inode = file_inode(file);
36971 +       /* no delegation since it is just created */
36972 +       if (inode->i_nlink)
36973 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36974 +                                  /*force*/0);
36975 +       inode_unlock(h_dir);
36976 +       dput(h_parent);
36977 +       if (unlikely(err)) {
36978 +               if (!silent)
36979 +                       pr_err("unlink %s(%d)\n", fpath, err);
36980 +               goto out;
36981 +       }
36982 +
36983 +       err = -EINVAL;
36984 +       if (unlikely(sb == d->d_sb)) {
36985 +               if (!silent)
36986 +                       pr_err("%s must be outside\n", fpath);
36987 +               goto out;
36988 +       }
36989 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36990 +               if (!silent)
36991 +                       pr_err("xino doesn't support %s(%s)\n",
36992 +                              fpath, au_sbtype(d->d_sb));
36993 +               goto out;
36994 +       }
36995 +       return file; /* success */
36996 +
36997 +out:
36998 +       fput(file);
36999 +       file = ERR_PTR(err);
37000 +       return file;
37001 +}
37002 +
37003 +/*
37004 + * create a new xinofile at the same place/path as @base.
37005 + */
37006 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37007 +                            struct file *copy_src)
37008 +{
37009 +       struct file *file;
37010 +       struct dentry *dentry;
37011 +       struct inode *dir, *delegated;
37012 +       struct qstr *name;
37013 +       struct path ppath, path;
37014 +       int err, do_unlock;
37015 +       struct au_xino_lock_dir ldir;
37016 +
37017 +       do_unlock = 1;
37018 +       au_xino_lock_dir(sb, base, &ldir);
37019 +       dentry = base->dentry;
37020 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37021 +       ppath.mnt = base->mnt;
37022 +       dir = d_inode(ppath.dentry);
37023 +       IMustLock(dir);
37024 +
37025 +       name = &dentry->d_name;
37026 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37027 +       if (IS_ERR(path.dentry)) {
37028 +               file = (void *)path.dentry;
37029 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37030 +               goto out;
37031 +       }
37032 +
37033 +       /* no need to mnt_want_write() since we call dentry_open() later */
37034 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37035 +       if (unlikely(err)) {
37036 +               file = ERR_PTR(err);
37037 +               pr_err("%pd create err %d\n", dentry, err);
37038 +               goto out_dput;
37039 +       }
37040 +
37041 +       path.mnt = base->mnt;
37042 +       file = vfsub_dentry_open(&path,
37043 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37044 +                                /* | __FMODE_NONOTIFY */);
37045 +       if (IS_ERR(file)) {
37046 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37047 +               goto out_dput;
37048 +       }
37049 +
37050 +       delegated = NULL;
37051 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37052 +       au_xino_unlock_dir(&ldir);
37053 +       do_unlock = 0;
37054 +       if (unlikely(err == -EWOULDBLOCK)) {
37055 +               pr_warn("cannot retry for NFSv4 delegation"
37056 +                       " for an internal unlink\n");
37057 +               iput(delegated);
37058 +       }
37059 +       if (unlikely(err)) {
37060 +               pr_err("%pd unlink err %d\n", dentry, err);
37061 +               goto out_fput;
37062 +       }
37063 +
37064 +       if (copy_src) {
37065 +               /* no one can touch copy_src xino */
37066 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37067 +               if (unlikely(err)) {
37068 +                       pr_err("%pd copy err %d\n", dentry, err);
37069 +                       goto out_fput;
37070 +               }
37071 +       }
37072 +       goto out_dput; /* success */
37073 +
37074 +out_fput:
37075 +       fput(file);
37076 +       file = ERR_PTR(err);
37077 +out_dput:
37078 +       dput(path.dentry);
37079 +out:
37080 +       if (do_unlock)
37081 +               au_xino_unlock_dir(&ldir);
37082 +       return file;
37083 +}
37084 +
37085 +struct file *au_xino_file1(struct au_xino *xi)
37086 +{
37087 +       struct file *file;
37088 +       unsigned int u, nfile;
37089 +
37090 +       file = NULL;
37091 +       nfile = xi->xi_nfile;
37092 +       for (u = 0; u < nfile; u++) {
37093 +               file = xi->xi_file[u];
37094 +               if (file)
37095 +                       break;
37096 +       }
37097 +
37098 +       return file;
37099 +}
37100 +
37101 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37102 +{
37103 +       int err;
37104 +       struct file *f;
37105 +       void *p;
37106 +
37107 +       if (file)
37108 +               get_file(file);
37109 +
37110 +       err = 0;
37111 +       f = NULL;
37112 +       if (idx < xi->xi_nfile) {
37113 +               f = xi->xi_file[idx];
37114 +               if (f)
37115 +                       fput(f);
37116 +       } else {
37117 +               p = au_kzrealloc(xi->xi_file,
37118 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37119 +                                sizeof(*xi->xi_file) * (idx + 1),
37120 +                                GFP_NOFS, /*may_shrink*/0);
37121 +               if (p) {
37122 +                       MtxMustLock(&xi->xi_mtx);
37123 +                       xi->xi_file = p;
37124 +                       xi->xi_nfile = idx + 1;
37125 +               } else {
37126 +                       err = -ENOMEM;
37127 +                       if (file)
37128 +                               fput(file);
37129 +                       goto out;
37130 +               }
37131 +       }
37132 +       xi->xi_file[idx] = file;
37133 +
37134 +out:
37135 +       return err;
37136 +}
37137 +
37138 +/*
37139 + * if @xinew->xi is not set, then create new xigen file.
37140 + */
37141 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37142 +{
37143 +       struct file *file;
37144 +       int err;
37145 +
37146 +       SiMustAnyLock(sb);
37147 +
37148 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37149 +       if (IS_ERR(file)) {
37150 +               err = PTR_ERR(file);
37151 +               pr_err("%s[%d], err %d\n",
37152 +                      xinew->xi ? "xino" : "xigen",
37153 +                      xinew->idx, err);
37154 +               goto out;
37155 +       }
37156 +
37157 +       if (xinew->xi)
37158 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37159 +       else {
37160 +               BUG();
37161 +               /* todo: make xigen file an array */
37162 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37163 +       }
37164 +       fput(file);
37165 +       if (unlikely(err))
37166 +               file = ERR_PTR(err);
37167 +
37168 +out:
37169 +       return file;
37170 +}
37171 +
37172 +/* ---------------------------------------------------------------------- */
37173 +
37174 +/*
37175 + * truncate xino files
37176 + */
37177 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37178 +                           int idx, struct kstatfs *st)
37179 +{
37180 +       int err;
37181 +       blkcnt_t blocks;
37182 +       struct file *file, *new_xino;
37183 +       struct au_xi_new xinew = {
37184 +               .idx = idx
37185 +       };
37186 +
37187 +       err = 0;
37188 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37189 +       file = au_xino_file(xinew.xi, idx);
37190 +       if (!file)
37191 +               goto out;
37192 +
37193 +       xinew.base = &file->f_path;
37194 +       err = vfs_statfs(xinew.base, st);
37195 +       if (unlikely(err)) {
37196 +               AuErr1("statfs err %d, ignored\n", err);
37197 +               err = 0;
37198 +               goto out;
37199 +       }
37200 +
37201 +       blocks = file_inode(file)->i_blocks;
37202 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37203 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37204 +
37205 +       xinew.copy_src = file;
37206 +       new_xino = au_xi_new(sb, &xinew);
37207 +       if (IS_ERR(new_xino)) {
37208 +               err = PTR_ERR(new_xino);
37209 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37210 +               goto out;
37211 +       }
37212 +
37213 +       err = vfs_statfs(&new_xino->f_path, st);
37214 +       if (!err)
37215 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37216 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37217 +                       st->f_bfree, st->f_blocks);
37218 +       else {
37219 +               AuErr1("statfs err %d, ignored\n", err);
37220 +               err = 0;
37221 +       }
37222 +
37223 +out:
37224 +       return err;
37225 +}
37226 +
37227 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37228 +{
37229 +       int err, i;
37230 +       unsigned long jiffy;
37231 +       aufs_bindex_t bbot;
37232 +       struct kstatfs *st;
37233 +       struct au_branch *br;
37234 +       struct au_xino *xi;
37235 +
37236 +       err = -ENOMEM;
37237 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37238 +       if (unlikely(!st))
37239 +               goto out;
37240 +
37241 +       err = -EINVAL;
37242 +       bbot = au_sbbot(sb);
37243 +       if (unlikely(bindex < 0 || bbot < bindex))
37244 +               goto out_st;
37245 +
37246 +       err = 0;
37247 +       jiffy = jiffies;
37248 +       br = au_sbr(sb, bindex);
37249 +       xi = br->br_xino;
37250 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37251 +               err = au_xino_do_trunc(sb, bindex, i, st);
37252 +       if (!err)
37253 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37254 +
37255 +out_st:
37256 +       au_kfree_rcu(st);
37257 +out:
37258 +       return err;
37259 +}
37260 +
37261 +struct xino_do_trunc_args {
37262 +       struct super_block *sb;
37263 +       struct au_branch *br;
37264 +       int idx;
37265 +};
37266 +
37267 +static void xino_do_trunc(void *_args)
37268 +{
37269 +       struct xino_do_trunc_args *args = _args;
37270 +       struct super_block *sb;
37271 +       struct au_branch *br;
37272 +       struct inode *dir;
37273 +       int err, idx;
37274 +       aufs_bindex_t bindex;
37275 +
37276 +       err = 0;
37277 +       sb = args->sb;
37278 +       dir = d_inode(sb->s_root);
37279 +       br = args->br;
37280 +       idx = args->idx;
37281 +
37282 +       si_noflush_write_lock(sb);
37283 +       ii_read_lock_parent(dir);
37284 +       bindex = au_br_index(sb, br->br_id);
37285 +       err = au_xino_trunc(sb, bindex, idx);
37286 +       ii_read_unlock(dir);
37287 +       if (unlikely(err))
37288 +               pr_warn("err b%d, (%d)\n", bindex, err);
37289 +       atomic_dec(&br->br_xino->xi_truncating);
37290 +       au_lcnt_dec(&br->br_count);
37291 +       si_write_unlock(sb);
37292 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37293 +       au_kfree_rcu(args);
37294 +}
37295 +
37296 +/*
37297 + * returns the index in the xi_file array whose corresponding file is necessary
37298 + * to truncate, or -1 which means no need to truncate.
37299 + */
37300 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37301 +{
37302 +       int err;
37303 +       unsigned int u;
37304 +       struct kstatfs st;
37305 +       struct au_sbinfo *sbinfo;
37306 +       struct au_xino *xi;
37307 +       struct file *file;
37308 +
37309 +       /* todo: si_xino_expire and the ratio should be customizable */
37310 +       sbinfo = au_sbi(sb);
37311 +       if (time_before(jiffies,
37312 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37313 +               return -1;
37314 +
37315 +       /* truncation border */
37316 +       xi = br->br_xino;
37317 +       for (u = 0; u < xi->xi_nfile; u++) {
37318 +               file = au_xino_file(xi, u);
37319 +               if (!file)
37320 +                       continue;
37321 +
37322 +               err = vfs_statfs(&file->f_path, &st);
37323 +               if (unlikely(err)) {
37324 +                       AuErr1("statfs err %d, ignored\n", err);
37325 +                       return -1;
37326 +               }
37327 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37328 +                   >= AUFS_XINO_DEF_TRUNC)
37329 +                       return u;
37330 +       }
37331 +
37332 +       return -1;
37333 +}
37334 +
37335 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37336 +{
37337 +       int idx;
37338 +       struct xino_do_trunc_args *args;
37339 +       int wkq_err;
37340 +
37341 +       idx = xino_trunc_test(sb, br);
37342 +       if (idx < 0)
37343 +               return;
37344 +
37345 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37346 +               goto out;
37347 +
37348 +       /* lock and kfree() will be called in trunc_xino() */
37349 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37350 +       if (unlikely(!args)) {
37351 +               AuErr1("no memory\n");
37352 +               goto out;
37353 +       }
37354 +
37355 +       au_lcnt_inc(&br->br_count);
37356 +       args->sb = sb;
37357 +       args->br = br;
37358 +       args->idx = idx;
37359 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37360 +       if (!wkq_err)
37361 +               return; /* success */
37362 +
37363 +       pr_err("wkq %d\n", wkq_err);
37364 +       au_lcnt_dec(&br->br_count);
37365 +       au_kfree_rcu(args);
37366 +
37367 +out:
37368 +       atomic_dec(&br->br_xino->xi_truncating);
37369 +}
37370 +
37371 +/* ---------------------------------------------------------------------- */
37372 +
37373 +struct au_xi_calc {
37374 +       int idx;
37375 +       loff_t pos;
37376 +};
37377 +
37378 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37379 +                      struct au_xi_calc *calc)
37380 +{
37381 +       loff_t maxent;
37382 +
37383 +       maxent = au_xi_maxent(sb);
37384 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37385 +       calc->pos *= sizeof(ino_t);
37386 +}
37387 +
37388 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37389 +                               struct au_xi_calc *calc)
37390 +{
37391 +       int err;
37392 +       struct file *file;
37393 +       struct au_xino *xi = br->br_xino;
37394 +       struct au_xi_new xinew = {
37395 +               .xi = xi
37396 +       };
37397 +
37398 +       SiMustAnyLock(sb);
37399 +
37400 +       err = 0;
37401 +       if (!xi)
37402 +               goto out;
37403 +
37404 +       mutex_lock(&xi->xi_mtx);
37405 +       file = au_xino_file(xi, calc->idx);
37406 +       if (file)
37407 +               goto out_mtx;
37408 +
37409 +       file = au_xino_file(xi, /*idx*/-1);
37410 +       AuDebugOn(!file);
37411 +       xinew.idx = calc->idx;
37412 +       xinew.base = &file->f_path;
37413 +       /* xinew.copy_src = NULL; */
37414 +       file = au_xi_new(sb, &xinew);
37415 +       if (IS_ERR(file))
37416 +               err = PTR_ERR(file);
37417 +
37418 +out_mtx:
37419 +       mutex_unlock(&xi->xi_mtx);
37420 +out:
37421 +       return err;
37422 +}
37423 +
37424 +struct au_xino_do_new_async_args {
37425 +       struct super_block *sb;
37426 +       struct au_branch *br;
37427 +       struct au_xi_calc calc;
37428 +       ino_t ino;
37429 +};
37430 +
37431 +struct au_xi_writing {
37432 +       struct hlist_bl_node node;
37433 +       ino_t h_ino, ino;
37434 +};
37435 +
37436 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37437 +                           ino_t ino);
37438 +
37439 +static void au_xino_call_do_new_async(void *args)
37440 +{
37441 +       struct au_xino_do_new_async_args *a = args;
37442 +       struct au_branch *br;
37443 +       struct super_block *sb;
37444 +       struct au_sbinfo *sbi;
37445 +       struct inode *root;
37446 +       struct file *file;
37447 +       struct au_xi_writing *del, *p;
37448 +       struct hlist_bl_head *hbl;
37449 +       struct hlist_bl_node *pos;
37450 +       int err;
37451 +
37452 +       br = a->br;
37453 +       sb = a->sb;
37454 +       sbi = au_sbi(sb);
37455 +       si_noflush_read_lock(sb);
37456 +       root = d_inode(sb->s_root);
37457 +       ii_read_lock_child(root);
37458 +       err = au_xino_do_new_async(sb, br, &a->calc);
37459 +       if (unlikely(err)) {
37460 +               AuIOErr("err %d\n", err);
37461 +               goto out;
37462 +       }
37463 +
37464 +       file = au_xino_file(br->br_xino, a->calc.idx);
37465 +       AuDebugOn(!file);
37466 +       err = au_xino_do_write(file, &a->calc, a->ino);
37467 +       if (unlikely(err)) {
37468 +               AuIOErr("err %d\n", err);
37469 +               goto out;
37470 +       }
37471 +
37472 +       del = NULL;
37473 +       hbl = &br->br_xino->xi_writing;
37474 +       hlist_bl_lock(hbl);
37475 +       au_hbl_for_each(pos, hbl) {
37476 +               p = container_of(pos, struct au_xi_writing, node);
37477 +               if (p->ino == a->ino) {
37478 +                       del = p;
37479 +                       hlist_bl_del(&p->node);
37480 +                       break;
37481 +               }
37482 +       }
37483 +       hlist_bl_unlock(hbl);
37484 +       au_kfree_rcu(del);
37485 +
37486 +out:
37487 +       au_lcnt_dec(&br->br_count);
37488 +       ii_read_unlock(root);
37489 +       si_read_unlock(sb);
37490 +       au_nwt_done(&sbi->si_nowait);
37491 +       au_kfree_rcu(a);
37492 +}
37493 +
37494 +/*
37495 + * create a new xino file asynchronously
37496 + */
37497 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37498 +                            struct au_xi_calc *calc, ino_t ino)
37499 +{
37500 +       int err;
37501 +       struct au_xino_do_new_async_args *arg;
37502 +
37503 +       err = -ENOMEM;
37504 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37505 +       if (unlikely(!arg))
37506 +               goto out;
37507 +
37508 +       arg->sb = sb;
37509 +       arg->br = br;
37510 +       arg->calc = *calc;
37511 +       arg->ino = ino;
37512 +       au_lcnt_inc(&br->br_count);
37513 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37514 +       if (unlikely(err)) {
37515 +               pr_err("wkq %d\n", err);
37516 +               au_lcnt_dec(&br->br_count);
37517 +               au_kfree_rcu(arg);
37518 +       }
37519 +
37520 +out:
37521 +       return err;
37522 +}
37523 +
37524 +/*
37525 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37526 + * at the position of @h_ino.
37527 + */
37528 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37529 +                ino_t *ino)
37530 +{
37531 +       int err;
37532 +       ssize_t sz;
37533 +       struct au_xi_calc calc;
37534 +       struct au_sbinfo *sbinfo;
37535 +       struct file *file;
37536 +       struct au_xino *xi;
37537 +       struct hlist_bl_head *hbl;
37538 +       struct hlist_bl_node *pos;
37539 +       struct au_xi_writing *p;
37540 +
37541 +       *ino = 0;
37542 +       if (!au_opt_test(au_mntflags(sb), XINO))
37543 +               return 0; /* no xino */
37544 +
37545 +       err = 0;
37546 +       au_xi_calc(sb, h_ino, &calc);
37547 +       xi = au_sbr(sb, bindex)->br_xino;
37548 +       file = au_xino_file(xi, calc.idx);
37549 +       if (!file) {
37550 +               hbl = &xi->xi_writing;
37551 +               hlist_bl_lock(hbl);
37552 +               au_hbl_for_each(pos, hbl) {
37553 +                       p = container_of(pos, struct au_xi_writing, node);
37554 +                       if (p->h_ino == h_ino) {
37555 +                               AuDbg("hi%llu, i%llu, found\n",
37556 +                                     (u64)p->h_ino, (u64)p->ino);
37557 +                               *ino = p->ino;
37558 +                               break;
37559 +                       }
37560 +               }
37561 +               hlist_bl_unlock(hbl);
37562 +               return 0;
37563 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37564 +               return 0; /* no xino */
37565 +
37566 +       sbinfo = au_sbi(sb);
37567 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37568 +       if (sz == sizeof(*ino))
37569 +               return 0; /* success */
37570 +
37571 +       err = sz;
37572 +       if (unlikely(sz >= 0)) {
37573 +               err = -EIO;
37574 +               AuIOErr("xino read error (%zd)\n", sz);
37575 +       }
37576 +       return err;
37577 +}
37578 +
37579 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37580 +                           ino_t ino)
37581 +{
37582 +       ssize_t sz;
37583 +
37584 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37585 +       if (sz == sizeof(ino))
37586 +               return 0; /* success */
37587 +
37588 +       AuIOErr("write failed (%zd)\n", sz);
37589 +       return -EIO;
37590 +}
37591 +
37592 +/*
37593 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37594 + * at the position of @h_ino.
37595 + * even if @ino is zero, it is written to the xinofile and means no entry.
37596 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37597 + * try truncating it.
37598 + */
37599 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37600 +                 ino_t ino)
37601 +{
37602 +       int err;
37603 +       unsigned int mnt_flags;
37604 +       struct au_xi_calc calc;
37605 +       struct file *file;
37606 +       struct au_branch *br;
37607 +       struct au_xino *xi;
37608 +       struct au_xi_writing *p;
37609 +
37610 +       SiMustAnyLock(sb);
37611 +
37612 +       mnt_flags = au_mntflags(sb);
37613 +       if (!au_opt_test(mnt_flags, XINO))
37614 +               return 0;
37615 +
37616 +       au_xi_calc(sb, h_ino, &calc);
37617 +       br = au_sbr(sb, bindex);
37618 +       xi = br->br_xino;
37619 +       file = au_xino_file(xi, calc.idx);
37620 +       if (!file) {
37621 +               /* store the inum pair into the list */
37622 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37623 +               p->h_ino = h_ino;
37624 +               p->ino = ino;
37625 +               au_hbl_add(&p->node, &xi->xi_writing);
37626 +
37627 +               /* create and write a new xino file asynchronously */
37628 +               err = au_xino_new_async(sb, br, &calc, ino);
37629 +               if (!err)
37630 +                       return 0; /* success */
37631 +               goto out;
37632 +       }
37633 +
37634 +       err = au_xino_do_write(file, &calc, ino);
37635 +       if (!err) {
37636 +               br = au_sbr(sb, bindex);
37637 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37638 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37639 +                       xino_try_trunc(sb, br);
37640 +               return 0; /* success */
37641 +       }
37642 +
37643 +out:
37644 +       AuIOErr("write failed (%d)\n", err);
37645 +       return -EIO;
37646 +}
37647 +
37648 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37649 +                             loff_t *pos);
37650 +
37651 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37652 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37653 +{
37654 +       ssize_t err;
37655 +       int i;
37656 +       const int prevent_endless = 10;
37657 +
37658 +       i = 0;
37659 +       do {
37660 +               err = vfsub_read_k(file, kbuf, size, pos);
37661 +               if (err == -EINTR
37662 +                   && !au_wkq_test()
37663 +                   && fatal_signal_pending(current)) {
37664 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37665 +                       BUG_ON(err == -EINTR);
37666 +               }
37667 +       } while (i++ < prevent_endless
37668 +                && (err == -EAGAIN || err == -EINTR));
37669 +
37670 +#if 0 /* reserved for future use */
37671 +       if (err > 0)
37672 +               fsnotify_access(file->f_path.dentry);
37673 +#endif
37674 +
37675 +       return err;
37676 +}
37677 +
37678 +struct xino_fread_args {
37679 +       ssize_t *errp;
37680 +       struct file *file;
37681 +       void *buf;
37682 +       size_t size;
37683 +       loff_t *pos;
37684 +};
37685 +
37686 +static void call_xino_fread(void *args)
37687 +{
37688 +       struct xino_fread_args *a = args;
37689 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37690 +}
37691 +
37692 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37693 +                             loff_t *pos)
37694 +{
37695 +       ssize_t err;
37696 +       int wkq_err;
37697 +       struct xino_fread_args args = {
37698 +               .errp   = &err,
37699 +               .file   = file,
37700 +               .buf    = buf,
37701 +               .size   = size,
37702 +               .pos    = pos
37703 +       };
37704 +
37705 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37706 +       if (unlikely(wkq_err))
37707 +               err = wkq_err;
37708 +
37709 +       return err;
37710 +}
37711 +
37712 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37713 +                              loff_t *pos);
37714 +
37715 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37716 +                             loff_t *pos)
37717 +{
37718 +       ssize_t err;
37719 +       int i;
37720 +       const int prevent_endless = 10;
37721 +
37722 +       i = 0;
37723 +       do {
37724 +               err = vfsub_write_k(file, kbuf, size, pos);
37725 +               if (err == -EINTR
37726 +                   && !au_wkq_test()
37727 +                   && fatal_signal_pending(current)) {
37728 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37729 +                       BUG_ON(err == -EINTR);
37730 +               }
37731 +       } while (i++ < prevent_endless
37732 +                && (err == -EAGAIN || err == -EINTR));
37733 +
37734 +#if 0 /* reserved for future use */
37735 +       if (err > 0)
37736 +               fsnotify_modify(file->f_path.dentry);
37737 +#endif
37738 +
37739 +       return err;
37740 +}
37741 +
37742 +struct do_xino_fwrite_args {
37743 +       ssize_t *errp;
37744 +       struct file *file;
37745 +       void *buf;
37746 +       size_t size;
37747 +       loff_t *pos;
37748 +};
37749 +
37750 +static void call_do_xino_fwrite(void *args)
37751 +{
37752 +       struct do_xino_fwrite_args *a = args;
37753 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37754 +}
37755 +
37756 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37757 +                              loff_t *pos)
37758 +{
37759 +       ssize_t err;
37760 +       int wkq_err;
37761 +       struct do_xino_fwrite_args args = {
37762 +               .errp   = &err,
37763 +               .file   = file,
37764 +               .buf    = buf,
37765 +               .size   = size,
37766 +               .pos    = pos
37767 +       };
37768 +
37769 +       /*
37770 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37771 +        * users should care about quota and real 'filesystem full.'
37772 +        */
37773 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37774 +       if (unlikely(wkq_err))
37775 +               err = wkq_err;
37776 +
37777 +       return err;
37778 +}
37779 +
37780 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37781 +{
37782 +       ssize_t err;
37783 +
37784 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37785 +               lockdep_off();
37786 +               err = do_xino_fwrite(file, buf, size, pos);
37787 +               lockdep_on();
37788 +       } else {
37789 +               lockdep_off();
37790 +               err = xino_fwrite_wkq(file, buf, size, pos);
37791 +               lockdep_on();
37792 +       }
37793 +
37794 +       return err;
37795 +}
37796 +
37797 +/* ---------------------------------------------------------------------- */
37798 +
37799 +/*
37800 + * inode number bitmap
37801 + */
37802 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37803 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37804 +{
37805 +       ino_t ino;
37806 +
37807 +       AuDebugOn(bit < 0 || page_bits <= bit);
37808 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37809 +       return ino;
37810 +}
37811 +
37812 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37813 +{
37814 +       AuDebugOn(ino < AUFS_FIRST_INO);
37815 +       ino -= AUFS_FIRST_INO;
37816 +       *pindex = ino / page_bits;
37817 +       *bit = ino % page_bits;
37818 +}
37819 +
37820 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37821 +{
37822 +       int err;
37823 +       loff_t pos;
37824 +       ssize_t sz;
37825 +       struct au_sbinfo *sbinfo;
37826 +       struct file *xib;
37827 +       unsigned long *p;
37828 +
37829 +       sbinfo = au_sbi(sb);
37830 +       MtxMustLock(&sbinfo->si_xib_mtx);
37831 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37832 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37833 +
37834 +       if (pindex == sbinfo->si_xib_last_pindex)
37835 +               return 0;
37836 +
37837 +       xib = sbinfo->si_xib;
37838 +       p = sbinfo->si_xib_buf;
37839 +       pos = sbinfo->si_xib_last_pindex;
37840 +       pos *= PAGE_SIZE;
37841 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37842 +       if (unlikely(sz != PAGE_SIZE))
37843 +               goto out;
37844 +
37845 +       pos = pindex;
37846 +       pos *= PAGE_SIZE;
37847 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37848 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37849 +       else {
37850 +               memset(p, 0, PAGE_SIZE);
37851 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37852 +       }
37853 +       if (sz == PAGE_SIZE) {
37854 +               sbinfo->si_xib_last_pindex = pindex;
37855 +               return 0; /* success */
37856 +       }
37857 +
37858 +out:
37859 +       AuIOErr1("write failed (%zd)\n", sz);
37860 +       err = sz;
37861 +       if (sz >= 0)
37862 +               err = -EIO;
37863 +       return err;
37864 +}
37865 +
37866 +static void au_xib_clear_bit(struct inode *inode)
37867 +{
37868 +       int err, bit;
37869 +       unsigned long pindex;
37870 +       struct super_block *sb;
37871 +       struct au_sbinfo *sbinfo;
37872 +
37873 +       AuDebugOn(inode->i_nlink);
37874 +
37875 +       sb = inode->i_sb;
37876 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37877 +       AuDebugOn(page_bits <= bit);
37878 +       sbinfo = au_sbi(sb);
37879 +       mutex_lock(&sbinfo->si_xib_mtx);
37880 +       err = xib_pindex(sb, pindex);
37881 +       if (!err) {
37882 +               clear_bit(bit, sbinfo->si_xib_buf);
37883 +               sbinfo->si_xib_next_bit = bit;
37884 +       }
37885 +       mutex_unlock(&sbinfo->si_xib_mtx);
37886 +}
37887 +
37888 +/* ---------------------------------------------------------------------- */
37889 +
37890 +/*
37891 + * truncate a xino bitmap file
37892 + */
37893 +
37894 +/* todo: slow */
37895 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37896 +{
37897 +       int err, bit;
37898 +       ssize_t sz;
37899 +       unsigned long pindex;
37900 +       loff_t pos, pend;
37901 +       struct au_sbinfo *sbinfo;
37902 +       ino_t *ino;
37903 +       unsigned long *p;
37904 +
37905 +       err = 0;
37906 +       sbinfo = au_sbi(sb);
37907 +       MtxMustLock(&sbinfo->si_xib_mtx);
37908 +       p = sbinfo->si_xib_buf;
37909 +       pend = vfsub_f_size_read(file);
37910 +       pos = 0;
37911 +       while (pos < pend) {
37912 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
37913 +               err = sz;
37914 +               if (unlikely(sz <= 0))
37915 +                       goto out;
37916 +
37917 +               err = 0;
37918 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37919 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37920 +                               continue;
37921 +
37922 +                       xib_calc_bit(*ino, &pindex, &bit);
37923 +                       AuDebugOn(page_bits <= bit);
37924 +                       err = xib_pindex(sb, pindex);
37925 +                       if (!err)
37926 +                               set_bit(bit, p);
37927 +                       else
37928 +                               goto out;
37929 +               }
37930 +       }
37931 +
37932 +out:
37933 +       return err;
37934 +}
37935 +
37936 +static int xib_restore(struct super_block *sb)
37937 +{
37938 +       int err, i;
37939 +       unsigned int nfile;
37940 +       aufs_bindex_t bindex, bbot;
37941 +       void *page;
37942 +       struct au_branch *br;
37943 +       struct au_xino *xi;
37944 +       struct file *file;
37945 +
37946 +       err = -ENOMEM;
37947 +       page = (void *)__get_free_page(GFP_NOFS);
37948 +       if (unlikely(!page))
37949 +               goto out;
37950 +
37951 +       err = 0;
37952 +       bbot = au_sbbot(sb);
37953 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37954 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37955 +                       br = au_sbr(sb, bindex);
37956 +                       xi = br->br_xino;
37957 +                       nfile = xi->xi_nfile;
37958 +                       for (i = 0; i < nfile; i++) {
37959 +                               file = au_xino_file(xi, i);
37960 +                               if (file)
37961 +                                       err = do_xib_restore(sb, file, page);
37962 +                       }
37963 +               } else
37964 +                       AuDbg("skip shared b%d\n", bindex);
37965 +       free_page((unsigned long)page);
37966 +
37967 +out:
37968 +       return err;
37969 +}
37970 +
37971 +int au_xib_trunc(struct super_block *sb)
37972 +{
37973 +       int err;
37974 +       ssize_t sz;
37975 +       loff_t pos;
37976 +       struct au_sbinfo *sbinfo;
37977 +       unsigned long *p;
37978 +       struct file *file;
37979 +
37980 +       SiMustWriteLock(sb);
37981 +
37982 +       err = 0;
37983 +       sbinfo = au_sbi(sb);
37984 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
37985 +               goto out;
37986 +
37987 +       file = sbinfo->si_xib;
37988 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
37989 +               goto out;
37990 +
37991 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37992 +       err = PTR_ERR(file);
37993 +       if (IS_ERR(file))
37994 +               goto out;
37995 +       fput(sbinfo->si_xib);
37996 +       sbinfo->si_xib = file;
37997 +
37998 +       p = sbinfo->si_xib_buf;
37999 +       memset(p, 0, PAGE_SIZE);
38000 +       pos = 0;
38001 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38002 +       if (unlikely(sz != PAGE_SIZE)) {
38003 +               err = sz;
38004 +               AuIOErr("err %d\n", err);
38005 +               if (sz >= 0)
38006 +                       err = -EIO;
38007 +               goto out;
38008 +       }
38009 +
38010 +       mutex_lock(&sbinfo->si_xib_mtx);
38011 +       /* mnt_want_write() is unnecessary here */
38012 +       err = xib_restore(sb);
38013 +       mutex_unlock(&sbinfo->si_xib_mtx);
38014 +
38015 +out:
38016 +       return err;
38017 +}
38018 +
38019 +/* ---------------------------------------------------------------------- */
38020 +
38021 +struct au_xino *au_xino_alloc(unsigned int nfile)
38022 +{
38023 +       struct au_xino *xi;
38024 +
38025 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38026 +       if (unlikely(!xi))
38027 +               goto out;
38028 +       xi->xi_nfile = nfile;
38029 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38030 +       if (unlikely(!xi->xi_file))
38031 +               goto out_free;
38032 +
38033 +       xi->xi_nondir.total = 8; /* initial size */
38034 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38035 +                                     GFP_NOFS);
38036 +       if (unlikely(!xi->xi_nondir.array))
38037 +               goto out_file;
38038 +
38039 +       spin_lock_init(&xi->xi_nondir.spin);
38040 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38041 +       mutex_init(&xi->xi_mtx);
38042 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38043 +       atomic_set(&xi->xi_truncating, 0);
38044 +       kref_init(&xi->xi_kref);
38045 +       goto out; /* success */
38046 +
38047 +out_file:
38048 +       au_kfree_try_rcu(xi->xi_file);
38049 +out_free:
38050 +       au_kfree_rcu(xi);
38051 +       xi = NULL;
38052 +out:
38053 +       return xi;
38054 +}
38055 +
38056 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38057 +{
38058 +       int err;
38059 +       struct au_xino *xi;
38060 +
38061 +       err = 0;
38062 +       xi = au_xino_alloc(idx + 1);
38063 +       if (unlikely(!xi)) {
38064 +               err = -ENOMEM;
38065 +               goto out;
38066 +       }
38067 +
38068 +       if (file)
38069 +               get_file(file);
38070 +       xi->xi_file[idx] = file;
38071 +       AuDebugOn(br->br_xino);
38072 +       br->br_xino = xi;
38073 +
38074 +out:
38075 +       return err;
38076 +}
38077 +
38078 +static void au_xino_release(struct kref *kref)
38079 +{
38080 +       struct au_xino *xi;
38081 +       int i;
38082 +       unsigned long ul;
38083 +       struct hlist_bl_head *hbl;
38084 +       struct hlist_bl_node *pos, *n;
38085 +       struct au_xi_writing *p;
38086 +
38087 +       xi = container_of(kref, struct au_xino, xi_kref);
38088 +       for (i = 0; i < xi->xi_nfile; i++)
38089 +               if (xi->xi_file[i])
38090 +                       fput(xi->xi_file[i]);
38091 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38092 +               AuDebugOn(xi->xi_nondir.array[i]);
38093 +       mutex_destroy(&xi->xi_mtx);
38094 +       hbl = &xi->xi_writing;
38095 +       ul = au_hbl_count(hbl);
38096 +       if (unlikely(ul)) {
38097 +               pr_warn("xi_writing %lu\n", ul);
38098 +               hlist_bl_lock(hbl);
38099 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38100 +                       hlist_bl_del(&p->node);
38101 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38102 +                       kfree(p);
38103 +               }
38104 +               hlist_bl_unlock(hbl);
38105 +       }
38106 +       au_kfree_try_rcu(xi->xi_file);
38107 +       au_kfree_try_rcu(xi->xi_nondir.array);
38108 +       au_kfree_rcu(xi);
38109 +}
38110 +
38111 +int au_xino_put(struct au_branch *br)
38112 +{
38113 +       int ret;
38114 +       struct au_xino *xi;
38115 +
38116 +       ret = 0;
38117 +       xi = br->br_xino;
38118 +       if (xi) {
38119 +               br->br_xino = NULL;
38120 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38121 +       }
38122 +
38123 +       return ret;
38124 +}
38125 +
38126 +/* ---------------------------------------------------------------------- */
38127 +
38128 +/*
38129 + * xino mount option handlers
38130 + */
38131 +
38132 +/* xino bitmap */
38133 +static void xino_clear_xib(struct super_block *sb)
38134 +{
38135 +       struct au_sbinfo *sbinfo;
38136 +
38137 +       SiMustWriteLock(sb);
38138 +
38139 +       sbinfo = au_sbi(sb);
38140 +       if (sbinfo->si_xib)
38141 +               fput(sbinfo->si_xib);
38142 +       sbinfo->si_xib = NULL;
38143 +       if (sbinfo->si_xib_buf)
38144 +               free_page((unsigned long)sbinfo->si_xib_buf);
38145 +       sbinfo->si_xib_buf = NULL;
38146 +}
38147 +
38148 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38149 +{
38150 +       int err;
38151 +       loff_t pos;
38152 +       struct au_sbinfo *sbinfo;
38153 +       struct file *file;
38154 +       struct super_block *xi_sb;
38155 +
38156 +       SiMustWriteLock(sb);
38157 +
38158 +       sbinfo = au_sbi(sb);
38159 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38160 +       err = PTR_ERR(file);
38161 +       if (IS_ERR(file))
38162 +               goto out;
38163 +       if (sbinfo->si_xib)
38164 +               fput(sbinfo->si_xib);
38165 +       sbinfo->si_xib = file;
38166 +       xi_sb = file_inode(file)->i_sb;
38167 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38168 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38169 +               err = -EIO;
38170 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38171 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38172 +               goto out_unset;
38173 +       }
38174 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38175 +
38176 +       err = -ENOMEM;
38177 +       if (!sbinfo->si_xib_buf)
38178 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38179 +       if (unlikely(!sbinfo->si_xib_buf))
38180 +               goto out_unset;
38181 +
38182 +       sbinfo->si_xib_last_pindex = 0;
38183 +       sbinfo->si_xib_next_bit = 0;
38184 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38185 +               pos = 0;
38186 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38187 +               if (unlikely(err != PAGE_SIZE))
38188 +                       goto out_free;
38189 +       }
38190 +       err = 0;
38191 +       goto out; /* success */
38192 +
38193 +out_free:
38194 +       if (sbinfo->si_xib_buf)
38195 +               free_page((unsigned long)sbinfo->si_xib_buf);
38196 +       sbinfo->si_xib_buf = NULL;
38197 +       if (err >= 0)
38198 +               err = -EIO;
38199 +out_unset:
38200 +       fput(sbinfo->si_xib);
38201 +       sbinfo->si_xib = NULL;
38202 +out:
38203 +       AuTraceErr(err);
38204 +       return err;
38205 +}
38206 +
38207 +/* xino for each branch */
38208 +static void xino_clear_br(struct super_block *sb)
38209 +{
38210 +       aufs_bindex_t bindex, bbot;
38211 +       struct au_branch *br;
38212 +
38213 +       bbot = au_sbbot(sb);
38214 +       for (bindex = 0; bindex <= bbot; bindex++) {
38215 +               br = au_sbr(sb, bindex);
38216 +               AuDebugOn(!br);
38217 +               au_xino_put(br);
38218 +       }
38219 +}
38220 +
38221 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38222 +                                 aufs_bindex_t bshared)
38223 +{
38224 +       struct au_branch *brshared;
38225 +
38226 +       brshared = au_sbr(sb, bshared);
38227 +       AuDebugOn(!brshared->br_xino);
38228 +       AuDebugOn(!brshared->br_xino->xi_file);
38229 +       if (br->br_xino != brshared->br_xino) {
38230 +               au_xino_get(brshared);
38231 +               au_xino_put(br);
38232 +               br->br_xino = brshared->br_xino;
38233 +       }
38234 +}
38235 +
38236 +struct au_xino_do_set_br {
38237 +       struct au_branch *br;
38238 +       ino_t h_ino;
38239 +       aufs_bindex_t bshared;
38240 +};
38241 +
38242 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38243 +                            struct au_xino_do_set_br *args)
38244 +{
38245 +       int err;
38246 +       struct au_xi_calc calc;
38247 +       struct file *file;
38248 +       struct au_branch *br;
38249 +       struct au_xi_new xinew = {
38250 +               .base = path
38251 +       };
38252 +
38253 +       br = args->br;
38254 +       xinew.xi = br->br_xino;
38255 +       au_xi_calc(sb, args->h_ino, &calc);
38256 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38257 +       if (args->bshared >= 0)
38258 +               /* shared xino */
38259 +               au_xino_set_br_shared(sb, br, args->bshared);
38260 +       else if (!xinew.xi) {
38261 +               /* new xino */
38262 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38263 +               if (unlikely(err))
38264 +                       goto out;
38265 +       }
38266 +
38267 +       /* force re-creating */
38268 +       xinew.xi = br->br_xino;
38269 +       xinew.idx = calc.idx;
38270 +       mutex_lock(&xinew.xi->xi_mtx);
38271 +       file = au_xi_new(sb, &xinew);
38272 +       mutex_unlock(&xinew.xi->xi_mtx);
38273 +       err = PTR_ERR(file);
38274 +       if (IS_ERR(file))
38275 +               goto out;
38276 +       AuDebugOn(!file);
38277 +
38278 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38279 +       if (unlikely(err))
38280 +               au_xino_put(br);
38281 +
38282 +out:
38283 +       AuTraceErr(err);
38284 +       return err;
38285 +}
38286 +
38287 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38288 +{
38289 +       int err;
38290 +       aufs_bindex_t bindex, bbot;
38291 +       struct au_xino_do_set_br args;
38292 +       struct inode *inode;
38293 +
38294 +       SiMustWriteLock(sb);
38295 +
38296 +       bbot = au_sbbot(sb);
38297 +       inode = d_inode(sb->s_root);
38298 +       for (bindex = 0; bindex <= bbot; bindex++) {
38299 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38300 +               args.br = au_sbr(sb, bindex);
38301 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38302 +               err = au_xino_do_set_br(sb, path, &args);
38303 +               if (unlikely(err))
38304 +                       break;
38305 +       }
38306 +
38307 +       AuTraceErr(err);
38308 +       return err;
38309 +}
38310 +
38311 +void au_xino_clr(struct super_block *sb)
38312 +{
38313 +       struct au_sbinfo *sbinfo;
38314 +
38315 +       au_xigen_clr(sb);
38316 +       xino_clear_xib(sb);
38317 +       xino_clear_br(sb);
38318 +       dbgaufs_brs_del(sb, 0);
38319 +       sbinfo = au_sbi(sb);
38320 +       /* lvalue, do not call au_mntflags() */
38321 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38322 +}
38323 +
38324 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38325 +{
38326 +       int err, skip;
38327 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38328 +       struct qstr *dname, *cur_name;
38329 +       struct file *cur_xino;
38330 +       struct au_sbinfo *sbinfo;
38331 +       struct path *path, *cur_path;
38332 +
38333 +       SiMustWriteLock(sb);
38334 +
38335 +       err = 0;
38336 +       sbinfo = au_sbi(sb);
38337 +       path = &xiopt->file->f_path;
38338 +       dentry = path->dentry;
38339 +       parent = dget_parent(dentry);
38340 +       if (remount) {
38341 +               skip = 0;
38342 +               cur_xino = sbinfo->si_xib;
38343 +               if (cur_xino) {
38344 +                       cur_path = &cur_xino->f_path;
38345 +                       cur_dentry = cur_path->dentry;
38346 +                       cur_parent = dget_parent(cur_dentry);
38347 +                       cur_name = &cur_dentry->d_name;
38348 +                       dname = &dentry->d_name;
38349 +                       skip = (cur_parent == parent
38350 +                               && au_qstreq(dname, cur_name));
38351 +                       dput(cur_parent);
38352 +               }
38353 +               if (skip)
38354 +                       goto out;
38355 +       }
38356 +
38357 +       au_opt_set(sbinfo->si_mntflags, XINO);
38358 +       err = au_xino_set_xib(sb, path);
38359 +       /* si_x{read,write} are set */
38360 +       if (!err)
38361 +               err = au_xigen_set(sb, path);
38362 +       if (!err)
38363 +               err = au_xino_set_br(sb, path);
38364 +       if (!err) {
38365 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38366 +               goto out; /* success */
38367 +       }
38368 +
38369 +       /* reset all */
38370 +       AuIOErr("failed setting xino(%d).\n", err);
38371 +       au_xino_clr(sb);
38372 +
38373 +out:
38374 +       dput(parent);
38375 +       return err;
38376 +}
38377 +
38378 +/*
38379 + * create a xinofile at the default place/path.
38380 + */
38381 +struct file *au_xino_def(struct super_block *sb)
38382 +{
38383 +       struct file *file;
38384 +       char *page, *p;
38385 +       struct au_branch *br;
38386 +       struct super_block *h_sb;
38387 +       struct path path;
38388 +       aufs_bindex_t bbot, bindex, bwr;
38389 +
38390 +       br = NULL;
38391 +       bbot = au_sbbot(sb);
38392 +       bwr = -1;
38393 +       for (bindex = 0; bindex <= bbot; bindex++) {
38394 +               br = au_sbr(sb, bindex);
38395 +               if (au_br_writable(br->br_perm)
38396 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38397 +                       bwr = bindex;
38398 +                       break;
38399 +               }
38400 +       }
38401 +
38402 +       if (bwr >= 0) {
38403 +               file = ERR_PTR(-ENOMEM);
38404 +               page = (void *)__get_free_page(GFP_NOFS);
38405 +               if (unlikely(!page))
38406 +                       goto out;
38407 +               path.mnt = au_br_mnt(br);
38408 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38409 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38410 +               file = (void *)p;
38411 +               if (!IS_ERR(p)) {
38412 +                       strcat(p, "/" AUFS_XINO_FNAME);
38413 +                       AuDbg("%s\n", p);
38414 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38415 +               }
38416 +               free_page((unsigned long)page);
38417 +       } else {
38418 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38419 +                                     /*wbrtop*/0);
38420 +               if (IS_ERR(file))
38421 +                       goto out;
38422 +               h_sb = file->f_path.dentry->d_sb;
38423 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38424 +                       pr_err("xino doesn't support %s(%s)\n",
38425 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38426 +                       fput(file);
38427 +                       file = ERR_PTR(-EINVAL);
38428 +               }
38429 +       }
38430 +
38431 +out:
38432 +       return file;
38433 +}
38434 +
38435 +/* ---------------------------------------------------------------------- */
38436 +
38437 +/*
38438 + * initialize the xinofile for the specified branch @br
38439 + * at the place/path where @base_file indicates.
38440 + * test whether another branch is on the same filesystem or not,
38441 + * if found then share the xinofile with another branch.
38442 + */
38443 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38444 +                   struct path *base)
38445 +{
38446 +       int err;
38447 +       struct au_xino_do_set_br args = {
38448 +               .h_ino  = h_ino,
38449 +               .br     = br
38450 +       };
38451 +
38452 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38453 +                                      au_br_sb(br));
38454 +       err = au_xino_do_set_br(sb, base, &args);
38455 +       if (unlikely(err))
38456 +               au_xino_put(br);
38457 +
38458 +       return err;
38459 +}
38460 +
38461 +/* ---------------------------------------------------------------------- */
38462 +
38463 +/*
38464 + * get an unused inode number from bitmap
38465 + */
38466 +ino_t au_xino_new_ino(struct super_block *sb)
38467 +{
38468 +       ino_t ino;
38469 +       unsigned long *p, pindex, ul, pend;
38470 +       struct au_sbinfo *sbinfo;
38471 +       struct file *file;
38472 +       int free_bit, err;
38473 +
38474 +       if (!au_opt_test(au_mntflags(sb), XINO))
38475 +               return iunique(sb, AUFS_FIRST_INO);
38476 +
38477 +       sbinfo = au_sbi(sb);
38478 +       mutex_lock(&sbinfo->si_xib_mtx);
38479 +       p = sbinfo->si_xib_buf;
38480 +       free_bit = sbinfo->si_xib_next_bit;
38481 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38482 +               goto out; /* success */
38483 +       free_bit = find_first_zero_bit(p, page_bits);
38484 +       if (free_bit < page_bits)
38485 +               goto out; /* success */
38486 +
38487 +       pindex = sbinfo->si_xib_last_pindex;
38488 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38489 +               err = xib_pindex(sb, ul);
38490 +               if (unlikely(err))
38491 +                       goto out_err;
38492 +               free_bit = find_first_zero_bit(p, page_bits);
38493 +               if (free_bit < page_bits)
38494 +                       goto out; /* success */
38495 +       }
38496 +
38497 +       file = sbinfo->si_xib;
38498 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38499 +       for (ul = pindex + 1; ul <= pend; ul++) {
38500 +               err = xib_pindex(sb, ul);
38501 +               if (unlikely(err))
38502 +                       goto out_err;
38503 +               free_bit = find_first_zero_bit(p, page_bits);
38504 +               if (free_bit < page_bits)
38505 +                       goto out; /* success */
38506 +       }
38507 +       BUG();
38508 +
38509 +out:
38510 +       set_bit(free_bit, p);
38511 +       sbinfo->si_xib_next_bit = free_bit + 1;
38512 +       pindex = sbinfo->si_xib_last_pindex;
38513 +       mutex_unlock(&sbinfo->si_xib_mtx);
38514 +       ino = xib_calc_ino(pindex, free_bit);
38515 +       AuDbg("i%lu\n", (unsigned long)ino);
38516 +       return ino;
38517 +out_err:
38518 +       mutex_unlock(&sbinfo->si_xib_mtx);
38519 +       AuDbg("i0\n");
38520 +       return 0;
38521 +}
38522 +
38523 +/* for s_op->delete_inode() */
38524 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38525 +{
38526 +       int err;
38527 +       unsigned int mnt_flags;
38528 +       aufs_bindex_t bindex, bbot, bi;
38529 +       unsigned char try_trunc;
38530 +       struct au_iinfo *iinfo;
38531 +       struct super_block *sb;
38532 +       struct au_hinode *hi;
38533 +       struct inode *h_inode;
38534 +       struct au_branch *br;
38535 +       struct au_xi_calc calc;
38536 +       struct file *file;
38537 +
38538 +       AuDebugOn(au_is_bad_inode(inode));
38539 +
38540 +       sb = inode->i_sb;
38541 +       mnt_flags = au_mntflags(sb);
38542 +       if (!au_opt_test(mnt_flags, XINO)
38543 +           || inode->i_ino == AUFS_ROOT_INO)
38544 +               return;
38545 +
38546 +       if (unlinked) {
38547 +               au_xigen_inc(inode);
38548 +               au_xib_clear_bit(inode);
38549 +       }
38550 +
38551 +       iinfo = au_ii(inode);
38552 +       bindex = iinfo->ii_btop;
38553 +       if (bindex < 0)
38554 +               return;
38555 +
38556 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38557 +       hi = au_hinode(iinfo, bindex);
38558 +       bbot = iinfo->ii_bbot;
38559 +       for (; bindex <= bbot; bindex++, hi++) {
38560 +               h_inode = hi->hi_inode;
38561 +               if (!h_inode
38562 +                   || (!unlinked && h_inode->i_nlink))
38563 +                       continue;
38564 +
38565 +               /* inode may not be revalidated */
38566 +               bi = au_br_index(sb, hi->hi_id);
38567 +               if (bi < 0)
38568 +                       continue;
38569 +
38570 +               br = au_sbr(sb, bi);
38571 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38572 +               file = au_xino_file(br->br_xino, calc.idx);
38573 +               if (IS_ERR_OR_NULL(file))
38574 +                       continue;
38575 +
38576 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38577 +               if (!err && try_trunc
38578 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38579 +                       xino_try_trunc(sb, br);
38580 +       }
38581 +}
38582 +
38583 +/* ---------------------------------------------------------------------- */
38584 +
38585 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38586 +{
38587 +       int found, total, i;
38588 +
38589 +       found = -1;
38590 +       total = xi->xi_nondir.total;
38591 +       for (i = 0; i < total; i++) {
38592 +               if (xi->xi_nondir.array[i] != h_ino)
38593 +                       continue;
38594 +               found = i;
38595 +               break;
38596 +       }
38597 +
38598 +       return found;
38599 +}
38600 +
38601 +static int au_xinondir_expand(struct au_xino *xi)
38602 +{
38603 +       int err, sz;
38604 +       ino_t *p;
38605 +
38606 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38607 +
38608 +       err = -ENOMEM;
38609 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38610 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38611 +               goto out;
38612 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38613 +                        /*may_shrink*/0);
38614 +       if (p) {
38615 +               xi->xi_nondir.array = p;
38616 +               xi->xi_nondir.total <<= 1;
38617 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38618 +               err = 0;
38619 +       }
38620 +
38621 +out:
38622 +       return err;
38623 +}
38624 +
38625 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38626 +                      ino_t h_ino, int idx)
38627 +{
38628 +       struct au_xino *xi;
38629 +
38630 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38631 +       xi = au_sbr(sb, bindex)->br_xino;
38632 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38633 +
38634 +       spin_lock(&xi->xi_nondir.spin);
38635 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38636 +       xi->xi_nondir.array[idx] = 0;
38637 +       spin_unlock(&xi->xi_nondir.spin);
38638 +       wake_up_all(&xi->xi_nondir.wqh);
38639 +}
38640 +
38641 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38642 +                     int *idx)
38643 +{
38644 +       int err, found, empty;
38645 +       struct au_xino *xi;
38646 +
38647 +       err = 0;
38648 +       *idx = -1;
38649 +       if (!au_opt_test(au_mntflags(sb), XINO))
38650 +               goto out; /* no xino */
38651 +
38652 +       xi = au_sbr(sb, bindex)->br_xino;
38653 +
38654 +again:
38655 +       spin_lock(&xi->xi_nondir.spin);
38656 +       found = au_xinondir_find(xi, h_ino);
38657 +       if (found == -1) {
38658 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38659 +               if (empty == -1) {
38660 +                       empty = xi->xi_nondir.total;
38661 +                       err = au_xinondir_expand(xi);
38662 +                       if (unlikely(err))
38663 +                               goto out_unlock;
38664 +               }
38665 +               xi->xi_nondir.array[empty] = h_ino;
38666 +               *idx = empty;
38667 +       } else {
38668 +               spin_unlock(&xi->xi_nondir.spin);
38669 +               wait_event(xi->xi_nondir.wqh,
38670 +                          xi->xi_nondir.array[found] != h_ino);
38671 +               goto again;
38672 +       }
38673 +
38674 +out_unlock:
38675 +       spin_unlock(&xi->xi_nondir.spin);
38676 +out:
38677 +       return err;
38678 +}
38679 +
38680 +/* ---------------------------------------------------------------------- */
38681 +
38682 +int au_xino_path(struct seq_file *seq, struct file *file)
38683 +{
38684 +       int err;
38685 +
38686 +       err = au_seq_path(seq, &file->f_path);
38687 +       if (unlikely(err))
38688 +               goto out;
38689 +
38690 +#define Deleted "\\040(deleted)"
38691 +       seq->count -= sizeof(Deleted) - 1;
38692 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38693 +                        sizeof(Deleted) - 1));
38694 +#undef Deleted
38695 +
38696 +out:
38697 +       return err;
38698 +}
38699 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38700 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38701 +++ linux/include/uapi/linux/aufs_type.h        2021-11-25 11:42:28.836641524 +0100
38702 @@ -0,0 +1,452 @@
38703 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38704 +/*
38705 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38706 + *
38707 + * This program, aufs is free software; you can redistribute it and/or modify
38708 + * it under the terms of the GNU General Public License as published by
38709 + * the Free Software Foundation; either version 2 of the License, or
38710 + * (at your option) any later version.
38711 + *
38712 + * This program is distributed in the hope that it will be useful,
38713 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38714 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38715 + * GNU General Public License for more details.
38716 + *
38717 + * You should have received a copy of the GNU General Public License
38718 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38719 + */
38720 +
38721 +#ifndef __AUFS_TYPE_H__
38722 +#define __AUFS_TYPE_H__
38723 +
38724 +#define AUFS_NAME      "aufs"
38725 +
38726 +#ifdef __KERNEL__
38727 +/*
38728 + * define it before including all other headers.
38729 + * sched.h may use pr_* macros before defining "current", so define the
38730 + * no-current version first, and re-define later.
38731 + */
38732 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38733 +#include <linux/sched.h>
38734 +#undef pr_fmt
38735 +#define pr_fmt(fmt) \
38736 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38737 +               (int)sizeof(current->comm), current->comm, current->pid
38738 +#include <linux/limits.h>
38739 +#else
38740 +#include <stdint.h>
38741 +#include <sys/types.h>
38742 +#include <limits.h>
38743 +#endif /* __KERNEL__ */
38744 +
38745 +#define AUFS_VERSION   "5.15-20211115"
38746 +
38747 +/* todo? move this to linux-2.6.19/include/magic.h */
38748 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38749 +
38750 +/* ---------------------------------------------------------------------- */
38751 +
38752 +#ifdef __KERNEL__
38753 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38754 +typedef int8_t aufs_bindex_t;
38755 +#define AUFS_BRANCH_MAX 127
38756 +#else
38757 +typedef int16_t aufs_bindex_t;
38758 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38759 +#define AUFS_BRANCH_MAX 511
38760 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38761 +#define AUFS_BRANCH_MAX 1023
38762 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38763 +#define AUFS_BRANCH_MAX 32767
38764 +#endif
38765 +#endif
38766 +
38767 +#ifndef AUFS_BRANCH_MAX
38768 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38769 +#endif
38770 +#endif /* __KERNEL__ */
38771 +
38772 +/* ---------------------------------------------------------------------- */
38773 +
38774 +#define AUFS_FSTYPE            AUFS_NAME
38775 +
38776 +#define AUFS_ROOT_INO          2
38777 +#define AUFS_FIRST_INO         11
38778 +
38779 +#define AUFS_WH_PFX            ".wh."
38780 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38781 +#define AUFS_WH_TMP_LEN                4
38782 +/* a limit for rmdir/rename a dir and copyup */
38783 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38784 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38785 +                               - 1                     /* dot */\
38786 +                               - AUFS_WH_TMP_LEN)      /* hex */
38787 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38788 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38789 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38790 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38791 +#define AUFS_DIRWH_DEF         3
38792 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38793 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38794 +#define AUFS_RDBLK_DEF         512 /* bytes */
38795 +#define AUFS_RDHASH_DEF                32
38796 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38797 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38798 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38799 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38800 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38801 +
38802 +/* pseudo-link maintenace under /proc */
38803 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38804 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38805 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38806 +
38807 +/* dirren, renamed dir */
38808 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38809 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38810 +/* whiteouted doubly */
38811 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38812 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38813 +
38814 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38815 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38816 +
38817 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38818 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38819 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38820 +
38821 +/* doubly whiteouted */
38822 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38823 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38824 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38825 +
38826 +/* branch permissions and attributes */
38827 +#define AUFS_BRPERM_RW         "rw"
38828 +#define AUFS_BRPERM_RO         "ro"
38829 +#define AUFS_BRPERM_RR         "rr"
38830 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38831 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38832 +#define AUFS_BRATTR_FHSM       "fhsm"
38833 +#define AUFS_BRATTR_UNPIN      "unpin"
38834 +#define AUFS_BRATTR_ICEX       "icex"
38835 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38836 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38837 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38838 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38839 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38840 +#define AUFS_BRRATTR_WH                "wh"
38841 +#define AUFS_BRWATTR_NLWH      "nolwh"
38842 +#define AUFS_BRWATTR_MOO       "moo"
38843 +
38844 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38845 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38846 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38847 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38848 +
38849 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38850 +#define AuBrAttr_COO_ALL       (1 << 4)
38851 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38852 +
38853 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38854 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38855 +                                                  branch. meaningless since
38856 +                                                  linux-3.18-rc1 */
38857 +
38858 +/* ignore error in copying XATTR */
38859 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38860 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38861 +#define AuBrAttr_ICEX_TR       (1 << 9)
38862 +#define AuBrAttr_ICEX_USR      (1 << 10)
38863 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38864 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38865 +                                | AuBrAttr_ICEX_SYS    \
38866 +                                | AuBrAttr_ICEX_TR     \
38867 +                                | AuBrAttr_ICEX_USR    \
38868 +                                | AuBrAttr_ICEX_OTH)
38869 +
38870 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38871 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38872 +
38873 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38874 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38875 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38876 +
38877 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38878 +
38879 +/* #warning test userspace */
38880 +#ifdef __KERNEL__
38881 +#ifndef CONFIG_AUFS_FHSM
38882 +#undef AuBrAttr_FHSM
38883 +#define AuBrAttr_FHSM          0
38884 +#endif
38885 +#ifndef CONFIG_AUFS_XATTR
38886 +#undef AuBrAttr_ICEX
38887 +#define AuBrAttr_ICEX          0
38888 +#undef AuBrAttr_ICEX_SEC
38889 +#define AuBrAttr_ICEX_SEC      0
38890 +#undef AuBrAttr_ICEX_SYS
38891 +#define AuBrAttr_ICEX_SYS      0
38892 +#undef AuBrAttr_ICEX_TR
38893 +#define AuBrAttr_ICEX_TR       0
38894 +#undef AuBrAttr_ICEX_USR
38895 +#define AuBrAttr_ICEX_USR      0
38896 +#undef AuBrAttr_ICEX_OTH
38897 +#define AuBrAttr_ICEX_OTH      0
38898 +#endif
38899 +#endif
38900 +
38901 +/* the longest combination */
38902 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38903 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38904 +                              "+" AUFS_BRATTR_COO_REG          \
38905 +                              "+" AUFS_BRATTR_FHSM             \
38906 +                              "+" AUFS_BRATTR_UNPIN            \
38907 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38908 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38909 +                              "+" AUFS_BRATTR_ICEX_USR         \
38910 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38911 +                              "+" AUFS_BRWATTR_NLWH)
38912 +
38913 +typedef struct {
38914 +       char a[AuBrPermStrSz];
38915 +} au_br_perm_str_t;
38916 +
38917 +static inline int au_br_writable(int brperm)
38918 +{
38919 +       return brperm & AuBrPerm_RW;
38920 +}
38921 +
38922 +static inline int au_br_whable(int brperm)
38923 +{
38924 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38925 +}
38926 +
38927 +static inline int au_br_wh_linkable(int brperm)
38928 +{
38929 +       return !(brperm & AuBrWAttr_NoLinkWH);
38930 +}
38931 +
38932 +static inline int au_br_cmoo(int brperm)
38933 +{
38934 +       return brperm & AuBrAttr_CMOO_Mask;
38935 +}
38936 +
38937 +static inline int au_br_fhsm(int brperm)
38938 +{
38939 +       return brperm & AuBrAttr_FHSM;
38940 +}
38941 +
38942 +/* ---------------------------------------------------------------------- */
38943 +
38944 +/* ioctl */
38945 +enum {
38946 +       /* readdir in userspace */
38947 +       AuCtl_RDU,
38948 +       AuCtl_RDU_INO,
38949 +
38950 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38951 +       AuCtl_IBUSY,    /* busy inode */
38952 +       AuCtl_MVDOWN,   /* move-down */
38953 +       AuCtl_BR,       /* info about branches */
38954 +       AuCtl_FHSM_FD   /* connection for fhsm */
38955 +};
38956 +
38957 +/* borrowed from linux/include/linux/kernel.h */
38958 +#ifndef ALIGN
38959 +#ifdef _GNU_SOURCE
38960 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38961 +#else
38962 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
38963 +#endif
38964 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38965 +#endif
38966 +
38967 +/* borrowed from linux/include/linux/compiler-gcc3.h */
38968 +#ifndef __aligned
38969 +#define __aligned(x)                   __attribute__((aligned(x)))
38970 +#endif
38971 +
38972 +#ifdef __KERNEL__
38973 +#ifndef __packed
38974 +#define __packed                       __attribute__((packed))
38975 +#endif
38976 +#endif
38977 +
38978 +struct au_rdu_cookie {
38979 +       uint64_t        h_pos;
38980 +       int16_t         bindex;
38981 +       uint8_t         flags;
38982 +       uint8_t         pad;
38983 +       uint32_t        generation;
38984 +} __aligned(8);
38985 +
38986 +struct au_rdu_ent {
38987 +       uint64_t        ino;
38988 +       int16_t         bindex;
38989 +       uint8_t         type;
38990 +       uint8_t         nlen;
38991 +       uint8_t         wh;
38992 +       char            name[];
38993 +} __aligned(8);
38994 +
38995 +static inline int au_rdu_len(int nlen)
38996 +{
38997 +       /* include the terminating NULL */
38998 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38999 +                    sizeof(uint64_t));
39000 +}
39001 +
39002 +union au_rdu_ent_ul {
39003 +       struct au_rdu_ent __user        *e;
39004 +       uint64_t                        ul;
39005 +};
39006 +
39007 +enum {
39008 +       AufsCtlRduV_SZ,
39009 +       AufsCtlRduV_End
39010 +};
39011 +
39012 +struct aufs_rdu {
39013 +       /* input */
39014 +       union {
39015 +               uint64_t        sz;     /* AuCtl_RDU */
39016 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39017 +       };
39018 +       union au_rdu_ent_ul     ent;
39019 +       uint16_t                verify[AufsCtlRduV_End];
39020 +
39021 +       /* input/output */
39022 +       uint32_t                blk;
39023 +
39024 +       /* output */
39025 +       union au_rdu_ent_ul     tail;
39026 +       /* number of entries which were added in a single call */
39027 +       uint64_t                rent;
39028 +       uint8_t                 full;
39029 +       uint8_t                 shwh;
39030 +
39031 +       struct au_rdu_cookie    cookie;
39032 +} __aligned(8);
39033 +
39034 +/* ---------------------------------------------------------------------- */
39035 +
39036 +/* dirren. the branch is identified by the filename who contains this */
39037 +struct au_drinfo {
39038 +       uint64_t ino;
39039 +       union {
39040 +               uint8_t oldnamelen;
39041 +               uint64_t _padding;
39042 +       };
39043 +       uint8_t oldname[];
39044 +} __aligned(8);
39045 +
39046 +struct au_drinfo_fdata {
39047 +       uint32_t magic;
39048 +       struct au_drinfo drinfo;
39049 +} __aligned(8);
39050 +
39051 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39052 +/* future */
39053 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39054 +
39055 +/* ---------------------------------------------------------------------- */
39056 +
39057 +struct aufs_wbr_fd {
39058 +       uint32_t        oflags;
39059 +       int16_t         brid;
39060 +} __aligned(8);
39061 +
39062 +/* ---------------------------------------------------------------------- */
39063 +
39064 +struct aufs_ibusy {
39065 +       uint64_t        ino, h_ino;
39066 +       int16_t         bindex;
39067 +} __aligned(8);
39068 +
39069 +/* ---------------------------------------------------------------------- */
39070 +
39071 +/* error code for move-down */
39072 +/* the actual message strings are implemented in aufs-util.git */
39073 +enum {
39074 +       EAU_MVDOWN_OPAQUE = 1,
39075 +       EAU_MVDOWN_WHITEOUT,
39076 +       EAU_MVDOWN_UPPER,
39077 +       EAU_MVDOWN_BOTTOM,
39078 +       EAU_MVDOWN_NOUPPER,
39079 +       EAU_MVDOWN_NOLOWERBR,
39080 +       EAU_Last
39081 +};
39082 +
39083 +/* flags for move-down */
39084 +#define AUFS_MVDOWN_DMSG       1
39085 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39086 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39087 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39088 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39089 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39090 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39091 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39092 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39093 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39094 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39095 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39096 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39097 +
39098 +/* index for move-down */
39099 +enum {
39100 +       AUFS_MVDOWN_UPPER,
39101 +       AUFS_MVDOWN_LOWER,
39102 +       AUFS_MVDOWN_NARRAY
39103 +};
39104 +
39105 +/*
39106 + * additional info of move-down
39107 + * number of free blocks and inodes.
39108 + * subset of struct kstatfs, but smaller and always 64bit.
39109 + */
39110 +struct aufs_stfs {
39111 +       uint64_t        f_blocks;
39112 +       uint64_t        f_bavail;
39113 +       uint64_t        f_files;
39114 +       uint64_t        f_ffree;
39115 +};
39116 +
39117 +struct aufs_stbr {
39118 +       int16_t                 brid;   /* optional input */
39119 +       int16_t                 bindex; /* output */
39120 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39121 +} __aligned(8);
39122 +
39123 +struct aufs_mvdown {
39124 +       uint32_t                flags;                  /* input/output */
39125 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39126 +       int8_t                  au_errno;               /* output */
39127 +} __aligned(8);
39128 +
39129 +/* ---------------------------------------------------------------------- */
39130 +
39131 +union aufs_brinfo {
39132 +       /* PATH_MAX may differ between kernel-space and user-space */
39133 +       char    _spacer[4096];
39134 +       struct {
39135 +               int16_t id;
39136 +               int     perm;
39137 +               char    path[];
39138 +       };
39139 +} __aligned(8);
39140 +
39141 +/* ---------------------------------------------------------------------- */
39142 +
39143 +#define AuCtlType              'A'
39144 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39145 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39146 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39147 +                                    struct aufs_wbr_fd)
39148 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39149 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39150 +                                     struct aufs_mvdown)
39151 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39152 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39153 +
39154 +#endif /* __AUFS_TYPE_H__ */
39155 SPDX-License-Identifier: GPL-2.0
39156 aufs5.15 loopback patch
39157
39158 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39159 index a62b132cbd551..79cf20555c2b1 100644
39160 --- a/drivers/block/loop.c
39161 +++ b/drivers/block/loop.c
39162 @@ -685,6 +685,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39163                                 lo->use_dio);
39164  }
39165  
39166 +static struct file *loop_real_file(struct file *file)
39167 +{
39168 +       struct file *f = NULL;
39169 +
39170 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39171 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39172 +       return f;
39173 +}
39174 +
39175  static void loop_reread_partitions(struct loop_device *lo)
39176  {
39177         int rc;
39178 @@ -742,6 +751,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39179  {
39180         struct file *file = fget(arg);
39181         struct file *old_file;
39182 +       struct file *f, *virt_file = NULL, *old_virt_file;
39183         int error;
39184         bool partscan;
39185         bool is_loop;
39186 @@ -761,11 +771,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39187         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39188                 goto out_err;
39189  
39190 +       f = loop_real_file(file);
39191 +       if (f) {
39192 +               virt_file = file;
39193 +               file = f;
39194 +               get_file(file);
39195 +       }
39196 +
39197         error = loop_validate_file(file, bdev);
39198         if (error)
39199                 goto out_err;
39200  
39201         old_file = lo->lo_backing_file;
39202 +       old_virt_file = lo->lo_backing_virt_file;
39203  
39204         error = -EINVAL;
39205  
39206 @@ -778,6 +796,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39207         blk_mq_freeze_queue(lo->lo_queue);
39208         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39209         lo->lo_backing_file = file;
39210 +       lo->lo_backing_virt_file = virt_file;
39211         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39212         mapping_set_gfp_mask(file->f_mapping,
39213                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39214 @@ -800,6 +819,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39215          * dependency.
39216          */
39217         fput(old_file);
39218 +       if (old_virt_file)
39219 +               fput(old_virt_file);
39220         if (partscan)
39221                 loop_reread_partitions(lo);
39222         return 0;
39223 @@ -808,6 +829,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39224         loop_global_unlock(lo, is_loop);
39225  out_putf:
39226         fput(file);
39227 +       if (virt_file)
39228 +               fput(virt_file);
39229         return error;
39230  }
39231  
39232 @@ -1208,6 +1231,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39233                           const struct loop_config *config)
39234  {
39235         struct file *file = fget(config->fd);
39236 +       struct file *f, *virt_file = NULL;
39237         struct inode *inode;
39238         struct address_space *mapping;
39239         int error;
39240 @@ -1223,6 +1247,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39241         /* This is safe, since we have a reference from open(). */
39242         __module_get(THIS_MODULE);
39243  
39244 +       f = loop_real_file(file);
39245 +       if (f) {
39246 +               virt_file = file;
39247 +               file = f;
39248 +               get_file(file);
39249 +       }
39250 +
39251         /*
39252          * If we don't hold exclusive handle for the device, upgrade to it
39253          * here to avoid changing device under exclusive owner.
39254 @@ -1288,6 +1319,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39255         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39256         lo->lo_device = bdev;
39257         lo->lo_backing_file = file;
39258 +       lo->lo_backing_virt_file = virt_file;
39259         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39260         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39261  
39262 @@ -1338,6 +1370,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39263                 bd_abort_claiming(bdev, loop_configure);
39264  out_putf:
39265         fput(file);
39266 +       if (virt_file)
39267 +               fput(virt_file);
39268         /* This is safe: open() is still holding a reference. */
39269         module_put(THIS_MODULE);
39270         return error;
39271 @@ -1346,6 +1380,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39272  static int __loop_clr_fd(struct loop_device *lo, bool release)
39273  {
39274         struct file *filp = NULL;
39275 +       struct file *virt_filp = lo->lo_backing_virt_file;
39276         gfp_t gfp = lo->old_gfp_mask;
39277         struct block_device *bdev = lo->lo_device;
39278         int err = 0;
39279 @@ -1397,6 +1432,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39280  
39281         spin_lock_irq(&lo->lo_lock);
39282         lo->lo_backing_file = NULL;
39283 +       lo->lo_backing_virt_file = NULL;
39284         spin_unlock_irq(&lo->lo_lock);
39285  
39286         loop_release_xfer(lo);
39287 @@ -1477,6 +1513,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39288          */
39289         if (filp)
39290                 fput(filp);
39291 +       if (virt_filp)
39292 +               fput(virt_filp);
39293         return err;
39294  }
39295  
39296 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39297 index 04c88dd6eabd6..0ff3ba22ee170 100644
39298 --- a/drivers/block/loop.h
39299 +++ b/drivers/block/loop.h
39300 @@ -46,7 +46,7 @@ struct loop_device {
39301         int             (*ioctl)(struct loop_device *, int cmd, 
39302                                  unsigned long arg); 
39303  
39304 -       struct file *   lo_backing_file;
39305 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39306         struct block_device *lo_device;
39307         void            *key_data; 
39308  
39309 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39310 index 99ceca1440449..e49dfe8550329 100644
39311 --- a/fs/aufs/f_op.c
39312 +++ b/fs/aufs/f_op.c
39313 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39314         if (IS_ERR(h_file))
39315                 goto out;
39316  
39317 -       if (au_test_loopback_kthread()) {
39318 +       if (0 && au_test_loopback_kthread()) {
39319                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39320                 if (file->f_mapping != h_file->f_mapping) {
39321                         file->f_mapping = h_file->f_mapping;
39322 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39323 index 74347bd75b380..5ef888a1d53f4 100644
39324 --- a/fs/aufs/loop.c
39325 +++ b/fs/aufs/loop.c
39326 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39327                 symbol_put(loop_backing_file);
39328         au_kfree_try_rcu(au_warn_loopback_array);
39329  }
39330 +
39331 +/* ---------------------------------------------------------------------- */
39332 +
39333 +/* support the loopback block device insude aufs */
39334 +
39335 +struct file *aufs_real_loop(struct file *file)
39336 +{
39337 +       struct file *f;
39338 +
39339 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39340 +       fi_read_lock(file);
39341 +       f = au_hf_top(file);
39342 +       fi_read_unlock(file);
39343 +       AuDebugOn(!f);
39344 +       return f;
39345 +}
39346 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39347 index 7293bee427f96..3345c098d0d47 100644
39348 --- a/fs/aufs/loop.h
39349 +++ b/fs/aufs/loop.h
39350 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39351  
39352  int au_loopback_init(void);
39353  void au_loopback_fin(void);
39354 +
39355 +struct file *aufs_real_loop(struct file *file);
39356  #else
39357  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39358  
39359 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39360  
39361  AuStubInt0(au_loopback_init, void)
39362  AuStubVoid(au_loopback_fin, void)
39363 +
39364 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39365  #endif /* BLK_DEV_LOOP */
39366  
39367  #endif /* __KERNEL__ */
39368 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39369 index ae38857541533..af32f0960e595 100644
39370 --- a/fs/aufs/super.c
39371 +++ b/fs/aufs/super.c
39372 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39373         .statfs         = aufs_statfs,
39374         .put_super      = aufs_put_super,
39375         .sync_fs        = aufs_sync_fs,
39376 -       .remount_fs     = aufs_remount_fs
39377 +       .remount_fs     = aufs_remount_fs,
39378 +#ifdef CONFIG_AUFS_BDEV_LOOP
39379 +       .real_loop      = aufs_real_loop
39380 +#endif
39381  };
39382  
39383  /* ---------------------------------------------------------------------- */
39384 diff --git a/include/linux/fs.h b/include/linux/fs.h
39385 index 376ddf1965aa2..adb8c854de599 100644
39386 --- a/include/linux/fs.h
39387 +++ b/include/linux/fs.h
39388 @@ -2226,6 +2226,10 @@ struct super_operations {
39389                                   struct shrink_control *);
39390         long (*free_cached_objects)(struct super_block *,
39391                                     struct shrink_control *);
39392 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39393 +       /* and aufs */
39394 +       struct file *(*real_loop)(struct file *);
39395 +#endif
39396  };
39397  
39398  /*
This page took 2.765815 seconds and 4 git commands to generate.