]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
- 5.16.15
[packages/kernel.git] / kernel-aufs5.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs5.x-rcN 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.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 5250298d28170..6b11c93d6750a 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3169,6 +3169,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 a154cab6cd989..52776d48e306a 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -635,6 +635,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/include/linux/fs.h b/include/linux/fs.h
162 index 1cb616fc11053..02ce64c28ef15 100644
163 --- a/include/linux/fs.h
164 +++ b/include/linux/fs.h
165 @@ -1378,6 +1378,7 @@ extern void fasync_free(struct fasync_struct *);
166  /* can be called from interrupts */
167  extern void kill_fasync(struct fasync_struct **, int, int);
168  
169 +extern int setfl(int fd, struct file *filp, unsigned long arg);
170  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
171  extern int f_setown(struct file *filp, unsigned long arg, int force);
172  extern void f_delown(struct file *filp);
173 @@ -2091,6 +2092,7 @@ struct file_operations {
174         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
175         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
176         int (*check_flags)(int);
177 +       int (*setfl)(struct file *, unsigned long);
178         int (*flock) (struct file *, int, struct file_lock *);
179         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
180         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
181 @@ -2614,6 +2616,7 @@ extern int current_umask(void);
182  extern void ihold(struct inode * inode);
183  extern void iput(struct inode *);
184  extern int generic_update_time(struct inode *, struct timespec64 *, int);
185 +extern int update_time(struct inode *, struct timespec64 *, int);
186  
187  /* /sys/fs */
188  extern struct kobject *fs_kobj;
189 @@ -2777,6 +2780,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
190  }
191  
192  void emergency_thaw_all(void);
193 +extern int __sync_filesystem(struct super_block *, int);
194  extern int sync_filesystem(struct super_block *);
195  extern const struct file_operations def_blk_fops;
196  extern const struct file_operations def_chr_fops;
197 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
198 index 467b94257105e..dab20282c82e2 100644
199 --- a/include/linux/lockdep.h
200 +++ b/include/linux/lockdep.h
201 @@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
202         return lock->key == key;
203  }
204  
205 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
206 +
207  /*
208   * Acquire a lock.
209   *
210 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
211 index 8f882f5881e87..6b9808f098435 100644
212 --- a/include/linux/mnt_namespace.h
213 +++ b/include/linux/mnt_namespace.h
214 @@ -7,12 +7,15 @@ struct mnt_namespace;
215  struct fs_struct;
216  struct user_namespace;
217  struct ns_common;
218 +struct vfsmount;
219  
220  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
221                 struct user_namespace *, struct fs_struct *);
222  extern void put_mnt_ns(struct mnt_namespace *ns);
223  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
224  
225 +extern int is_current_mnt_ns(struct vfsmount *mnt);
226 +
227  extern const struct file_operations proc_mounts_operations;
228  extern const struct file_operations proc_mountinfo_operations;
229  extern const struct file_operations proc_mountstats_operations;
230 diff --git a/include/linux/splice.h b/include/linux/splice.h
231 index a55179fd60fc3..8e21c53cf8831 100644
232 --- a/include/linux/splice.h
233 +++ b/include/linux/splice.h
234 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
235  
236  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
237  extern const struct pipe_buf_operations default_pipe_buf_ops;
238 +
239 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
240 +                          loff_t *ppos, size_t len, unsigned int flags);
241 +extern long do_splice_to(struct file *in, loff_t *ppos,
242 +                        struct pipe_inode_info *pipe, size_t len,
243 +                        unsigned int flags);
244  #endif
245 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
246 index 2270ec68f10a1..fbb8e650c1749 100644
247 --- a/kernel/locking/lockdep.c
248 +++ b/kernel/locking/lockdep.c
249 @@ -189,7 +189,7 @@ static
250  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
251  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
252  
253 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
254 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
255  {
256         unsigned int class_idx = hlock->class_idx;
257  
258 @@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
259          */
260         return lock_classes + class_idx;
261  }
262 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
263  
264  #ifdef CONFIG_LOCK_STAT
265  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
266 SPDX-License-Identifier: GPL-2.0
267 aufs5.x-rcN mmap patch
268
269 diff --git a/fs/proc/base.c b/fs/proc/base.c
270 index 13eda8de29981..24fd5e986cb79 100644
271 --- a/fs/proc/base.c
272 +++ b/fs/proc/base.c
273 @@ -2191,7 +2191,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
274         rc = -ENOENT;
275         vma = find_exact_vma(mm, vm_start, vm_end);
276         if (vma && vma->vm_file) {
277 -               *path = vma->vm_file->f_path;
278 +               *path = vma_pr_or_file(vma)->f_path;
279                 path_get(path);
280                 rc = 0;
281         }
282 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
283 index 13452b32e2bd5..38acccfef9d49 100644
284 --- a/fs/proc/nommu.c
285 +++ b/fs/proc/nommu.c
286 @@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
287         file = region->vm_file;
288  
289         if (file) {
290 -               struct inode *inode = file_inode(region->vm_file);
291 +               struct inode *inode;
292 +
293 +               file = vmr_pr_or_file(region);
294 +               inode = file_inode(file);
295                 dev = inode->i_sb->s_dev;
296                 ino = inode->i_ino;
297         }
298 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
299 index ad667dbc96f5c..d50423613c8ed 100644
300 --- a/fs/proc/task_mmu.c
301 +++ b/fs/proc/task_mmu.c
302 @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
303         const char *name = NULL;
304  
305         if (file) {
306 -               struct inode *inode = file_inode(vma->vm_file);
307 +               struct inode *inode;
308 +
309 +               file = vma_pr_or_file(vma);
310 +               inode = file_inode(file);
311                 dev = inode->i_sb->s_dev;
312                 ino = inode->i_ino;
313                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
314 @@ -1869,7 +1872,7 @@ static int show_numa_map(struct seq_file *m, void *v)
315         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
316         struct vm_area_struct *vma = v;
317         struct numa_maps *md = &numa_priv->md;
318 -       struct file *file = vma->vm_file;
319 +       struct file *file = vma_pr_or_file(vma);
320         struct mm_struct *mm = vma->vm_mm;
321         struct mempolicy *pol;
322         char buffer[64];
323 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
324 index a6d21fc0033c6..02c2de31196e0 100644
325 --- a/fs/proc/task_nommu.c
326 +++ b/fs/proc/task_nommu.c
327 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
328         file = vma->vm_file;
329  
330         if (file) {
331 -               struct inode *inode = file_inode(vma->vm_file);
332 +               struct inode *inode;
333 +
334 +               file = vma_pr_or_file(vma);
335 +               inode = file_inode(file);
336                 dev = inode->i_sb->s_dev;
337                 ino = inode->i_ino;
338                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
339 diff --git a/include/linux/mm.h b/include/linux/mm.h
340 index a7e4a9e7d807a..30699240b45d6 100644
341 --- a/include/linux/mm.h
342 +++ b/include/linux/mm.h
343 @@ -1926,6 +1926,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
344         unmap_mapping_range(mapping, holebegin, holelen, 0);
345  }
346  
347 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
348 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
349 +                                     int);
350 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
351 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
352 +
353 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
354 +                                                               __LINE__)
355 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
356 +                                                         __LINE__)
357 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
358 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
359 +
360 +#ifndef CONFIG_MMU
361 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
362 +extern void vmr_do_fput(struct vm_region *, const char[], int);
363 +
364 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
365 +                                                         __LINE__)
366 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
367 +#endif /* !CONFIG_MMU */
368 +
369  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
370                 void *buf, int len, unsigned int gup_flags);
371  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
372 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
373 index c3a6e62096006..45f5754d51cd1 100644
374 --- a/include/linux/mm_types.h
375 +++ b/include/linux/mm_types.h
376 @@ -370,6 +370,7 @@ struct vm_region {
377         unsigned long   vm_top;         /* region allocated to here */
378         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
379         struct file     *vm_file;       /* the backing file or NULL */
380 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
381  
382         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
383         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
384 @@ -449,6 +450,7 @@ struct vm_area_struct {
385         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
386                                            units */
387         struct file * vm_file;          /* File we map to (can be NULL). */
388 +       struct file *vm_prfile;         /* shadow of vm_file */
389         void * vm_private_data;         /* was vm_pte (shared mem) */
390  
391  #ifdef CONFIG_SWAP
392 diff --git a/kernel/fork.c b/kernel/fork.c
393 index 3244cc56b697d..ae63cce182fdc 100644
394 --- a/kernel/fork.c
395 +++ b/kernel/fork.c
396 @@ -572,7 +572,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
397                 if (file) {
398                         struct address_space *mapping = file->f_mapping;
399  
400 -                       get_file(file);
401 +                       vma_get_file(tmp);
402                         i_mmap_lock_write(mapping);
403                         if (tmp->vm_flags & VM_SHARED)
404                                 mapping_allow_writable(mapping);
405 diff --git a/mm/Makefile b/mm/Makefile
406 index d6c0042e3aa0d..1f11c655a632b 100644
407 --- a/mm/Makefile
408 +++ b/mm/Makefile
409 @@ -52,7 +52,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
410                            mm_init.o percpu.o slab_common.o \
411                            compaction.o vmacache.o \
412                            interval_tree.o list_lru.o workingset.o \
413 -                          debug.o gup.o mmap_lock.o $(mmu-y)
414 +                          prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
415  
416  # Give 'page_alloc' its own module-parameter namespace
417  page-alloc-y := page_alloc.o
418 diff --git a/mm/filemap.c b/mm/filemap.c
419 index daa0e23a6ee66..1c1f517516f26 100644
420 --- a/mm/filemap.c
421 +++ b/mm/filemap.c
422 @@ -3358,7 +3358,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
423         vm_fault_t ret = VM_FAULT_LOCKED;
424  
425         sb_start_pagefault(mapping->host->i_sb);
426 -       file_update_time(vmf->vma->vm_file);
427 +       vma_file_update_time(vmf->vma);
428         lock_page(page);
429         if (page->mapping != mapping) {
430                 unlock_page(page);
431 diff --git a/mm/mmap.c b/mm/mmap.c
432 index bfb0ea164a90a..6bbc17d4733df 100644
433 --- a/mm/mmap.c
434 +++ b/mm/mmap.c
435 @@ -183,7 +183,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
436         if (vma->vm_ops && vma->vm_ops->close)
437                 vma->vm_ops->close(vma);
438         if (vma->vm_file)
439 -               fput(vma->vm_file);
440 +               vma_fput(vma);
441         mpol_put(vma_policy(vma));
442         vm_area_free(vma);
443         return next;
444 @@ -952,7 +952,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
445         if (remove_next) {
446                 if (file) {
447                         uprobe_munmap(next, next->vm_start, next->vm_end);
448 -                       fput(file);
449 +                       vma_fput(vma);
450                 }
451                 if (next->anon_vma)
452                         anon_vma_merge(vma, next);
453 @@ -1872,7 +1872,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
454         return addr;
455  
456  unmap_and_free_vma:
457 -       fput(vma->vm_file);
458 +       vma_fput(vma);
459         vma->vm_file = NULL;
460  
461         /* Undo any partial mapping done by a device driver. */
462 @@ -2730,7 +2730,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
463                 goto out_free_mpol;
464  
465         if (new->vm_file)
466 -               get_file(new->vm_file);
467 +               vma_get_file(new);
468  
469         if (new->vm_ops && new->vm_ops->open)
470                 new->vm_ops->open(new);
471 @@ -2749,7 +2749,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
472         if (new->vm_ops && new->vm_ops->close)
473                 new->vm_ops->close(new);
474         if (new->vm_file)
475 -               fput(new->vm_file);
476 +               vma_fput(new);
477         unlink_anon_vmas(new);
478   out_free_mpol:
479         mpol_put(vma_policy(new));
480 @@ -2944,7 +2944,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
481         struct vm_area_struct *vma;
482         unsigned long populate = 0;
483         unsigned long ret = -EINVAL;
484 -       struct file *file;
485 +       struct file *file, *prfile;
486  
487         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
488                      current->comm, current->pid);
489 @@ -3000,10 +3000,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
490         if (vma->vm_flags & VM_LOCKED)
491                 flags |= MAP_LOCKED;
492  
493 -       file = get_file(vma->vm_file);
494 +       vma_get_file(vma);
495 +       file = vma->vm_file;
496 +       prfile = vma->vm_prfile;
497         ret = do_mmap(vma->vm_file, start, size,
498                         prot, flags, pgoff, &populate, NULL);
499 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
500 +               struct vm_area_struct *new_vma;
501 +
502 +               new_vma = find_vma(mm, ret);
503 +               if (!new_vma->vm_prfile)
504 +                       new_vma->vm_prfile = prfile;
505 +               if (new_vma != vma)
506 +                       get_file(prfile);
507 +       }
508 +       /*
509 +        * two fput()s instead of vma_fput(vma),
510 +        * coz vma may not be available anymore.
511 +        */
512         fput(file);
513 +       if (prfile)
514 +               fput(prfile);
515  out:
516         mmap_write_unlock(mm);
517         if (populate)
518 @@ -3284,7 +3301,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
519                 if (anon_vma_clone(new_vma, vma))
520                         goto out_free_mempol;
521                 if (new_vma->vm_file)
522 -                       get_file(new_vma->vm_file);
523 +                       vma_get_file(new_vma);
524                 if (new_vma->vm_ops && new_vma->vm_ops->open)
525                         new_vma->vm_ops->open(new_vma);
526                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
527 diff --git a/mm/nommu.c b/mm/nommu.c
528 index 55a9e48a7a026..8dc77ce96335a 100644
529 --- a/mm/nommu.c
530 +++ b/mm/nommu.c
531 @@ -522,7 +522,7 @@ static void __put_nommu_region(struct vm_region *region)
532                 up_write(&nommu_region_sem);
533  
534                 if (region->vm_file)
535 -                       fput(region->vm_file);
536 +                       vmr_fput(region);
537  
538                 /* IO memory and memory shared directly out of the pagecache
539                  * from ramfs/tmpfs mustn't be released here */
540 @@ -654,7 +654,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
541         if (vma->vm_ops && vma->vm_ops->close)
542                 vma->vm_ops->close(vma);
543         if (vma->vm_file)
544 -               fput(vma->vm_file);
545 +               vma_fput(vma);
546         put_nommu_region(vma->vm_region);
547         vm_area_free(vma);
548  }
549 @@ -1174,7 +1174,7 @@ unsigned long do_mmap(struct file *file,
550                                         goto error_just_free;
551                                 }
552                         }
553 -                       fput(region->vm_file);
554 +                       vmr_fput(region);
555                         kmem_cache_free(vm_region_jar, region);
556                         region = pregion;
557                         result = start;
558 @@ -1251,10 +1251,10 @@ unsigned long do_mmap(struct file *file,
559         up_write(&nommu_region_sem);
560  error:
561         if (region->vm_file)
562 -               fput(region->vm_file);
563 +               vmr_fput(region);
564         kmem_cache_free(vm_region_jar, region);
565         if (vma->vm_file)
566 -               fput(vma->vm_file);
567 +               vma_fput(vma);
568         vm_area_free(vma);
569         return ret;
570  
571 diff --git a/mm/prfile.c b/mm/prfile.c
572 new file mode 100644
573 index 0000000000000..511543ab1b418
574 --- /dev/null
575 +++ b/mm/prfile.c
576 @@ -0,0 +1,86 @@
577 +// SPDX-License-Identifier: GPL-2.0
578 +/*
579 + * Mainly for aufs which mmap(2) different file and wants to print different
580 + * path in /proc/PID/maps.
581 + * Call these functions via macros defined in linux/mm.h.
582 + *
583 + * See Documentation/filesystems/aufs/design/06mmap.txt
584 + *
585 + * Copyright (c) 2014-2021 Junjro R. Okajima
586 + * Copyright (c) 2014 Ian Campbell
587 + */
588 +
589 +#include <linux/mm.h>
590 +#include <linux/file.h>
591 +#include <linux/fs.h>
592 +
593 +/* #define PRFILE_TRACE */
594 +static inline void prfile_trace(struct file *f, struct file *pr,
595 +                             const char func[], int line, const char func2[])
596 +{
597 +#ifdef PRFILE_TRACE
598 +       if (pr)
599 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
600 +#endif
601 +}
602 +
603 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
604 +                            int line)
605 +{
606 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
607 +
608 +       prfile_trace(f, pr, func, line, __func__);
609 +       file_update_time(f);
610 +       if (f && pr)
611 +               file_update_time(pr);
612 +}
613 +
614 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
615 +                              int line)
616 +{
617 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
618 +
619 +       prfile_trace(f, pr, func, line, __func__);
620 +       return (f && pr) ? pr : f;
621 +}
622 +
623 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
624 +{
625 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
626 +
627 +       prfile_trace(f, pr, func, line, __func__);
628 +       get_file(f);
629 +       if (f && pr)
630 +               get_file(pr);
631 +}
632 +
633 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
634 +{
635 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
636 +
637 +       prfile_trace(f, pr, func, line, __func__);
638 +       fput(f);
639 +       if (f && pr)
640 +               fput(pr);
641 +}
642 +
643 +#ifndef CONFIG_MMU
644 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
645 +                              int line)
646 +{
647 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
648 +
649 +       prfile_trace(f, pr, func, line, __func__);
650 +       return (f && pr) ? pr : f;
651 +}
652 +
653 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
654 +{
655 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
656 +
657 +       prfile_trace(f, pr, func, line, __func__);
658 +       fput(f);
659 +       if (f && pr)
660 +               fput(pr);
661 +}
662 +#endif /* !CONFIG_MMU */
663 SPDX-License-Identifier: GPL-2.0
664 aufs5.x-rcN standalone patch
665
666 diff --git a/fs/dcache.c b/fs/dcache.c
667 index bc5095b734f58..9508bd57a3bc0 100644
668 --- a/fs/dcache.c
669 +++ b/fs/dcache.c
670 @@ -1425,6 +1425,7 @@ void d_walk(struct dentry *parent, void *data,
671         seq = 1;
672         goto again;
673  }
674 +EXPORT_SYMBOL_GPL(d_walk);
675  
676  struct check_mount {
677         struct vfsmount *mnt;
678 @@ -2970,6 +2971,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
679  
680         write_sequnlock(&rename_lock);
681  }
682 +EXPORT_SYMBOL_GPL(d_exchange);
683  
684  /**
685   * d_ancestor - search for an ancestor
686 diff --git a/fs/exec.c b/fs/exec.c
687 index 537d92c41105b..0ab811e0fdaae 100644
688 --- a/fs/exec.c
689 +++ b/fs/exec.c
690 @@ -111,6 +111,7 @@ bool path_noexec(const struct path *path)
691         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
692                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
693  }
694 +EXPORT_SYMBOL_GPL(path_noexec);
695  
696  #ifdef CONFIG_USELIB
697  /*
698 diff --git a/fs/fcntl.c b/fs/fcntl.c
699 index 02382fa9bd341..3418c60b90146 100644
700 --- a/fs/fcntl.c
701 +++ b/fs/fcntl.c
702 @@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
703   out:
704         return error;
705  }
706 +EXPORT_SYMBOL_GPL(setfl);
707  
708  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
709                       int force)
710 diff --git a/fs/file_table.c b/fs/file_table.c
711 index 45437f8e1003e..786af52904fcf 100644
712 --- a/fs/file_table.c
713 +++ b/fs/file_table.c
714 @@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
715         }
716         return ERR_PTR(-ENFILE);
717  }
718 +EXPORT_SYMBOL_GPL(alloc_empty_file);
719  
720  /*
721   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
722 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
723  }
724  
725  EXPORT_SYMBOL(fput);
726 +EXPORT_SYMBOL_GPL(__fput_sync);
727  
728  void __init files_init(void)
729  {
730 diff --git a/fs/namespace.c b/fs/namespace.c
731 index 1283670737826..db99365620118 100644
732 --- a/fs/namespace.c
733 +++ b/fs/namespace.c
734 @@ -439,6 +439,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
735         mnt_dec_writers(real_mount(mnt));
736         preempt_enable();
737  }
738 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
739  
740  /**
741   * mnt_drop_write - give up write access to a mount
742 @@ -813,6 +814,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
743  {
744         return check_mnt(real_mount(mnt));
745  }
746 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
747  
748  /*
749   * vfsmount lock must be held for write
750 @@ -2011,6 +2013,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
751         }
752         return 0;
753  }
754 +EXPORT_SYMBOL_GPL(iterate_mounts);
755  
756  static void lock_mnt_tree(struct mount *mnt)
757  {
758 diff --git a/fs/notify/group.c b/fs/notify/group.c
759 index 6a297efc47887..ed394ccb10e00 100644
760 --- a/fs/notify/group.c
761 +++ b/fs/notify/group.c
762 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
763  {
764         refcount_inc(&group->refcnt);
765  }
766 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
767  
768  /*
769   * Drop a reference to a group.  Free it if it's through.
770 diff --git a/fs/open.c b/fs/open.c
771 index f732fb94600ce..ca33d86e73fd2 100644
772 --- a/fs/open.c
773 +++ b/fs/open.c
774 @@ -65,6 +65,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
775         inode_unlock(dentry->d_inode);
776         return ret;
777  }
778 +EXPORT_SYMBOL_GPL(do_truncate);
779  
780  long vfs_truncate(const struct path *path, loff_t length)
781  {
782 diff --git a/fs/read_write.c b/fs/read_write.c
783 index 0074afa7ecb3e..612cf04d9f6b9 100644
784 --- a/fs/read_write.c
785 +++ b/fs/read_write.c
786 @@ -488,6 +488,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
787         inc_syscr(current);
788         return ret;
789  }
790 +EXPORT_SYMBOL_GPL(vfs_read);
791  
792  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
793  {
794 @@ -598,6 +599,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
795         file_end_write(file);
796         return ret;
797  }
798 +EXPORT_SYMBOL_GPL(vfs_write);
799  
800  /* file_ppos returns &file->f_pos or NULL if file is stream */
801  static inline loff_t *file_ppos(struct file *file)
802 diff --git a/fs/splice.c b/fs/splice.c
803 index 3e6ba363b7775..7c1be373eb7cd 100644
804 --- a/fs/splice.c
805 +++ b/fs/splice.c
806 @@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
807                 return warn_unsupported(out, "write");
808         return out->f_op->splice_write(pipe, out, ppos, len, flags);
809  }
810 +EXPORT_SYMBOL_GPL(do_splice_from);
811  
812  /*
813   * Attempt to initiate a splice from a file to a pipe.
814 @@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
815                 return warn_unsupported(in, "read");
816         return in->f_op->splice_read(in, ppos, pipe, len, flags);
817  }
818 +EXPORT_SYMBOL_GPL(do_splice_to);
819  
820  /**
821   * splice_direct_to_actor - splices data directly between two non-pipes
822 diff --git a/fs/xattr.c b/fs/xattr.c
823 index 5c8c5175b385c..ff7e9ff774b73 100644
824 --- a/fs/xattr.c
825 +++ b/fs/xattr.c
826 @@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
827         *xattr_value = value;
828         return error;
829  }
830 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
831  
832  ssize_t
833  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
834 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
835 index fbb8e650c1749..49c4d11b08938 100644
836 --- a/kernel/locking/lockdep.c
837 +++ b/kernel/locking/lockdep.c
838 @@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
839          */
840         return lock_classes + class_idx;
841  }
842 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
843  #define hlock_class(hlock) lockdep_hlock_class(hlock)
844  
845  #ifdef CONFIG_LOCK_STAT
846 diff --git a/kernel/task_work.c b/kernel/task_work.c
847 index 1698fbe6f0e13..081b05acadf82 100644
848 --- a/kernel/task_work.c
849 +++ b/kernel/task_work.c
850 @@ -167,3 +167,4 @@ void task_work_run(void)
851                 } while (work);
852         }
853  }
854 +EXPORT_SYMBOL_GPL(task_work_run);
855 diff --git a/security/security.c b/security/security.c
856 index c88167a414b41..125724525d5c0 100644
857 --- a/security/security.c
858 +++ b/security/security.c
859 @@ -1148,6 +1148,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
860                 return 0;
861         return call_int_hook(path_rmdir, 0, dir, dentry);
862  }
863 +EXPORT_SYMBOL_GPL(security_path_rmdir);
864  
865  int security_path_unlink(const struct path *dir, struct dentry *dentry)
866  {
867 @@ -1164,6 +1165,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
868                 return 0;
869         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
870  }
871 +EXPORT_SYMBOL_GPL(security_path_symlink);
872  
873  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
874                        struct dentry *new_dentry)
875 @@ -1172,6 +1174,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
876                 return 0;
877         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
878  }
879 +EXPORT_SYMBOL_GPL(security_path_link);
880  
881  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
882                          const struct path *new_dir, struct dentry *new_dentry,
883 @@ -1199,6 +1202,7 @@ int security_path_truncate(const struct path *path)
884                 return 0;
885         return call_int_hook(path_truncate, 0, path);
886  }
887 +EXPORT_SYMBOL_GPL(security_path_truncate);
888  
889  int security_path_chmod(const struct path *path, umode_t mode)
890  {
891 @@ -1206,6 +1210,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
892                 return 0;
893         return call_int_hook(path_chmod, 0, path, mode);
894  }
895 +EXPORT_SYMBOL_GPL(security_path_chmod);
896  
897  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
898  {
899 @@ -1213,6 +1218,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
900                 return 0;
901         return call_int_hook(path_chown, 0, path, uid, gid);
902  }
903 +EXPORT_SYMBOL_GPL(security_path_chown);
904  
905  int security_path_chroot(const struct path *path)
906  {
907 @@ -1313,6 +1319,7 @@ int security_inode_permission(struct inode *inode, int mask)
908                 return 0;
909         return call_int_hook(inode_permission, 0, inode, mask);
910  }
911 +EXPORT_SYMBOL_GPL(security_inode_permission);
912  
913  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
914  {
915 @@ -1510,6 +1517,7 @@ int security_file_permission(struct file *file, int mask)
916  
917         return fsnotify_perm(file, mask);
918  }
919 +EXPORT_SYMBOL_GPL(security_file_permission);
920  
921  int security_file_alloc(struct file *file)
922  {
923 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
924 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
925 +++ linux/Documentation/ABI/testing/debugfs-aufs        2021-12-03 15:38:59.926647310 +0100
926 @@ -0,0 +1,55 @@
927 +What:          /debug/aufs/si_<id>/
928 +Date:          March 2009
929 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
930 +Description:
931 +               Under /debug/aufs, a directory named si_<id> is created
932 +               per aufs mount, where <id> is a unique id generated
933 +               internally.
934 +
935 +What:          /debug/aufs/si_<id>/plink
936 +Date:          Apr 2013
937 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
938 +Description:
939 +               It has three lines and shows the information about the
940 +               pseudo-link. The first line is a single number
941 +               representing a number of buckets. The second line is a
942 +               number of pseudo-links per buckets (separated by a
943 +               blank). The last line is a single number representing a
944 +               total number of psedo-links.
945 +               When the aufs mount option 'noplink' is specified, it
946 +               will show "1\n0\n0\n".
947 +
948 +What:          /debug/aufs/si_<id>/xib
949 +Date:          March 2009
950 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
951 +Description:
952 +               It shows the consumed blocks by xib (External Inode Number
953 +               Bitmap), its block size and file size.
954 +               When the aufs mount option 'noxino' is specified, it
955 +               will be empty. About XINO files, see the aufs manual.
956 +
957 +What:          /debug/aufs/si_<id>/xi<branch-index>
958 +Date:          March 2009
959 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
960 +Description:
961 +               It shows the consumed blocks by xino (External Inode Number
962 +               Translation Table), its link count, block size and file
963 +               size.
964 +               Due to the file size limit, there may exist multiple
965 +               xino files per branch.  In this case, "-N" is added to
966 +               the filename and it corresponds to the index of the
967 +               internal xino array.  "-0" is omitted.
968 +               When the aufs mount option 'noxino' is specified, Those
969 +               entries won't exist.  About XINO files, see the aufs
970 +               manual.
971 +
972 +What:          /debug/aufs/si_<id>/xigen
973 +Date:          March 2009
974 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
975 +Description:
976 +               It shows the consumed blocks by xigen (External Inode
977 +               Generation Table), its block size and file size.
978 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
979 +               be created.
980 +               When the aufs mount option 'noxino' is specified, it
981 +               will be empty. About XINO files, see the aufs manual.
982 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
983 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
984 +++ linux/Documentation/ABI/testing/sysfs-aufs  2021-12-03 15:38:59.926647310 +0100
985 @@ -0,0 +1,31 @@
986 +What:          /sys/fs/aufs/si_<id>/
987 +Date:          March 2009
988 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
989 +Description:
990 +               Under /sys/fs/aufs, a directory named si_<id> is created
991 +               per aufs mount, where <id> is a unique id generated
992 +               internally.
993 +
994 +What:          /sys/fs/aufs/si_<id>/br<idx>
995 +Date:          March 2009
996 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
997 +Description:
998 +               It shows the abolute path of a member directory (which
999 +               is called branch) in aufs, and its permission.
1000 +
1001 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1002 +Date:          July 2013
1003 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1004 +Description:
1005 +               It shows the id of a member directory (which is called
1006 +               branch) in aufs.
1007 +
1008 +What:          /sys/fs/aufs/si_<id>/xi_path
1009 +Date:          March 2009
1010 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1011 +Description:
1012 +               It shows the abolute path of XINO (External Inode Number
1013 +               Bitmap, Translation Table and Generation Table) file
1014 +               even if it is the default path.
1015 +               When the aufs mount option 'noxino' is specified, it
1016 +               will be empty. About XINO files, see the aufs manual.
1017 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1018 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1019 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2021-12-03 15:38:59.929980643 +0100
1020 @@ -0,0 +1,171 @@
1021 +
1022 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1023 +# 
1024 +# This program is free software; you can redistribute it and/or modify
1025 +# it under the terms of the GNU General Public License as published by
1026 +# the Free Software Foundation; either version 2 of the License, or
1027 +# (at your option) any later version.
1028 +# 
1029 +# This program is distributed in the hope that it will be useful,
1030 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1031 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1032 +# GNU General Public License for more details.
1033 +# 
1034 +# You should have received a copy of the GNU General Public License
1035 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1036 +
1037 +Introduction
1038 +----------------------------------------
1039 +
1040 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1041 +1. abbrev. for "advanced multi-layered unification filesystem".
1042 +2. abbrev. for "another unionfs".
1043 +3. abbrev. for "auf das" in German which means "on the" in English.
1044 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1045 +   But "Filesystem aufs Filesystem" is hard to understand.
1046 +4. abbrev. for "African Urban Fashion Show".
1047 +
1048 +AUFS is a filesystem with features:
1049 +- multi layered stackable unification filesystem, the member directory
1050 +  is called as a branch.
1051 +- branch permission and attribute, 'readonly', 'real-readonly',
1052 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1053 +  combination.
1054 +- internal "file copy-on-write".
1055 +- logical deletion, whiteout.
1056 +- dynamic branch manipulation, adding, deleting and changing permission.
1057 +- allow bypassing aufs, user's direct branch access.
1058 +- external inode number translation table and bitmap which maintains the
1059 +  persistent aufs inode number.
1060 +- seekable directory, including NFS readdir.
1061 +- file mapping, mmap and sharing pages.
1062 +- pseudo-link, hardlink over branches.
1063 +- loopback mounted filesystem as a branch.
1064 +- several policies to select one among multiple writable branches.
1065 +- revert a single systemcall when an error occurs in aufs.
1066 +- and more...
1067 +
1068 +
1069 +Multi Layered Stackable Unification Filesystem
1070 +----------------------------------------------------------------------
1071 +Most people already knows what it is.
1072 +It is a filesystem which unifies several directories and provides a
1073 +merged single directory. When users access a file, the access will be
1074 +passed/re-directed/converted (sorry, I am not sure which English word is
1075 +correct) to the real file on the member filesystem. The member
1076 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1077 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1078 +readonly branch is handled by creating 'whiteout' on the upper writable
1079 +branch.
1080 +
1081 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1082 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1083 +different approaches to implement the merged-view.
1084 +The former tries putting it into VFS, and the latter implements as a
1085 +separate filesystem.
1086 +(If I misunderstand about these implementations, please let me know and
1087 +I shall correct it. Because it is a long time ago when I read their
1088 +source files last time).
1089 +
1090 +UnionMount's approach will be able to small, but may be hard to share
1091 +branches between several UnionMount since the whiteout in it is
1092 +implemented in the inode on branch filesystem and always
1093 +shared. According to Bharata's post, readdir does not seems to be
1094 +finished yet.
1095 +There are several missing features known in this implementations such as
1096 +- for users, the inode number may change silently. eg. copy-up.
1097 +- link(2) may break by copy-up.
1098 +- read(2) may get an obsoleted filedata (fstat(2) too).
1099 +- fcntl(F_SETLK) may be broken by copy-up.
1100 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1101 +  open(O_RDWR).
1102 +
1103 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1104 +merged into mainline. This is another implementation of UnionMount as a
1105 +separated filesystem. All the limitations and known problems which
1106 +UnionMount are equally inherited to "overlay" filesystem.
1107 +
1108 +Unionfs has a longer history. When I started implementing a stackable
1109 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1110 +inode, dentry and file objects and they have an array pointing lower
1111 +same kind objects. After contributing many patches for Unionfs, I
1112 +re-started my project AUFS (Jun 2006).
1113 +
1114 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1115 +implemented my own ideas, approaches and enhancements and it became
1116 +totally different one.
1117 +
1118 +Comparing DM snapshot and fs based implementation
1119 +- the number of bytes to be copied between devices is much smaller.
1120 +- the type of filesystem must be one and only.
1121 +- the fs must be writable, no readonly fs, even for the lower original
1122 +  device. so the compression fs will not be usable. but if we use
1123 +  loopback mount, we may address this issue.
1124 +  for instance,
1125 +       mount /cdrom/squashfs.img /sq
1126 +       losetup /sq/ext2.img
1127 +       losetup /somewhere/cow
1128 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1129 +- it will be difficult (or needs more operations) to extract the
1130 +  difference between the original device and COW.
1131 +- DM snapshot-merge may help a lot when users try merging. in the
1132 +  fs-layer union, users will use rsync(1).
1133 +
1134 +You may want to read my old paper "Filesystems in LiveCD"
1135 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1136 +
1137 +
1138 +Several characters/aspects/persona of aufs
1139 +----------------------------------------------------------------------
1140 +
1141 +Aufs has several characters, aspects or persona.
1142 +1. a filesystem, callee of VFS helper
1143 +2. sub-VFS, caller of VFS helper for branches
1144 +3. a virtual filesystem which maintains persistent inode number
1145 +4. reader/writer of files on branches such like an application
1146 +
1147 +1. Callee of VFS Helper
1148 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1149 +unlink(2) from an application reaches sys_unlink() kernel function and
1150 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1151 +calls filesystem specific unlink operation. Actually aufs implements the
1152 +unlink operation but it behaves like a redirector.
1153 +
1154 +2. Caller of VFS Helper for Branches
1155 +aufs_unlink() passes the unlink request to the branch filesystem as if
1156 +it were called from VFS. So the called unlink operation of the branch
1157 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1158 +every necessary pre/post operation for the branch filesystem.
1159 +- acquire the lock for the parent dir on a branch
1160 +- lookup in a branch
1161 +- revalidate dentry on a branch
1162 +- mnt_want_write() for a branch
1163 +- vfs_unlink() for a branch
1164 +- mnt_drop_write() for a branch
1165 +- release the lock on a branch
1166 +
1167 +3. Persistent Inode Number
1168 +One of the most important issue for a filesystem is to maintain inode
1169 +numbers. This is particularly important to support exporting a
1170 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1171 +backend block device for its own. But some storage is necessary to
1172 +keep and maintain the inode numbers. It may be a large space and may not
1173 +suit to keep in memory. Aufs rents some space from its first writable
1174 +branch filesystem (by default) and creates file(s) on it. These files
1175 +are created by aufs internally and removed soon (currently) keeping
1176 +opened.
1177 +Note: Because these files are removed, they are totally gone after
1178 +      unmounting aufs. It means the inode numbers are not persistent
1179 +      across unmount or reboot. I have a plan to make them really
1180 +      persistent which will be important for aufs on NFS server.
1181 +
1182 +4. Read/Write Files Internally (copy-on-write)
1183 +Because a branch can be readonly, when you write a file on it, aufs will
1184 +"copy-up" it to the upper writable branch internally. And then write the
1185 +originally requested thing to the file. Generally kernel doesn't
1186 +open/read/write file actively. In aufs, even a single write may cause a
1187 +internal "file copy". This behaviour is very similar to cp(1) command.
1188 +
1189 +Some people may think it is better to pass such work to user space
1190 +helper, instead of doing in kernel space. Actually I am still thinking
1191 +about it. But currently I have implemented it in kernel space.
1192 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1193 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1194 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2021-12-03 15:38:59.929980643 +0100
1195 @@ -0,0 +1,258 @@
1196 +
1197 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1198 +# 
1199 +# This program is free software; you can redistribute it and/or modify
1200 +# it under the terms of the GNU General Public License as published by
1201 +# the Free Software Foundation; either version 2 of the License, or
1202 +# (at your option) any later version.
1203 +# 
1204 +# This program is distributed in the hope that it will be useful,
1205 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1206 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1207 +# GNU General Public License for more details.
1208 +# 
1209 +# You should have received a copy of the GNU General Public License
1210 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1211 +
1212 +Basic Aufs Internal Structure
1213 +
1214 +Superblock/Inode/Dentry/File Objects
1215 +----------------------------------------------------------------------
1216 +As like an ordinary filesystem, aufs has its own
1217 +superblock/inode/dentry/file objects. All these objects have a
1218 +dynamically allocated array and store the same kind of pointers to the
1219 +lower filesystem, branch.
1220 +For example, when you build a union with one readwrite branch and one
1221 +readonly, mounted /au, /rw and /ro respectively.
1222 +- /au = /rw + /ro
1223 +- /ro/fileA exists but /rw/fileA
1224 +
1225 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1226 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1227 +- [0] = NULL (because /rw/fileA doesn't exist)
1228 +- [1] = /ro/fileA
1229 +
1230 +This style of an array is essentially same to the aufs
1231 +superblock/inode/dentry/file objects.
1232 +
1233 +Because aufs supports manipulating branches, ie. add/delete/change
1234 +branches dynamically, these objects has its own generation. When
1235 +branches are changed, the generation in aufs superblock is
1236 +incremented. And a generation in other object are compared when it is
1237 +accessed. When a generation in other objects are obsoleted, aufs
1238 +refreshes the internal array.
1239 +
1240 +
1241 +Superblock
1242 +----------------------------------------------------------------------
1243 +Additionally aufs superblock has some data for policies to select one
1244 +among multiple writable branches, XIB files, pseudo-links and kobject.
1245 +See below in detail.
1246 +About the policies which supports copy-down a directory, see
1247 +wbr_policy.txt too.
1248 +
1249 +
1250 +Branch and XINO(External Inode Number Translation Table)
1251 +----------------------------------------------------------------------
1252 +Every branch has its own xino (external inode number translation table)
1253 +file. The xino file is created and unlinked by aufs internally. When two
1254 +members of a union exist on the same filesystem, they share the single
1255 +xino file.
1256 +The struct of a xino file is simple, just a sequence of aufs inode
1257 +numbers which is indexed by the lower inode number.
1258 +In the above sample, assume the inode number of /ro/fileA is i111 and
1259 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1260 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1261 +
1262 +When the inode numbers are not contiguous, the xino file will be sparse
1263 +which has a hole in it and doesn't consume as much disk space as it
1264 +might appear. If your branch filesystem consumes disk space for such
1265 +holes, then you should specify 'xino=' option at mounting aufs.
1266 +
1267 +Aufs has a mount option to free the disk blocks for such holes in XINO
1268 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1269 +meet a problem of disk shortage due to XINO files, then you should try
1270 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1271 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1272 +the holes in XINO files.
1273 +
1274 +Also a writable branch has three kinds of "whiteout bases". All these
1275 +are existed when the branch is joined to aufs, and their names are
1276 +whiteout-ed doubly, so that users will never see their names in aufs
1277 +hierarchy.
1278 +1. a regular file which will be hardlinked to all whiteouts.
1279 +2. a directory to store a pseudo-link.
1280 +3. a directory to store an "orphan"-ed file temporary.
1281 +
1282 +1. Whiteout Base
1283 +   When you remove a file on a readonly branch, aufs handles it as a
1284 +   logical deletion and creates a whiteout on the upper writable branch
1285 +   as a hardlink of this file in order not to consume inode on the
1286 +   writable branch.
1287 +2. Pseudo-link Dir
1288 +   See below, Pseudo-link.
1289 +3. Step-Parent Dir
1290 +   When "fileC" exists on the lower readonly branch only and it is
1291 +   opened and removed with its parent dir, and then user writes
1292 +   something into it, then aufs copies-up fileC to this
1293 +   directory. Because there is no other dir to store fileC. After
1294 +   creating a file under this dir, the file is unlinked.
1295 +
1296 +Because aufs supports manipulating branches, ie. add/delete/change
1297 +dynamically, a branch has its own id. When the branch order changes,
1298 +aufs finds the new index by searching the branch id.
1299 +
1300 +
1301 +Pseudo-link
1302 +----------------------------------------------------------------------
1303 +Assume "fileA" exists on the lower readonly branch only and it is
1304 +hardlinked to "fileB" on the branch. When you write something to fileA,
1305 +aufs copies-up it to the upper writable branch. Additionally aufs
1306 +creates a hardlink under the Pseudo-link Directory of the writable
1307 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1308 +simple list. If fileB is read after unlinking fileA, aufs returns
1309 +filedata from the pseudo-link instead of the lower readonly
1310 +branch. Because the pseudo-link is based upon the inode, to keep the
1311 +inode number by xino (see above) is essentially necessary.
1312 +
1313 +All the hardlinks under the Pseudo-link Directory of the writable branch
1314 +should be restored in a proper location later. Aufs provides a utility
1315 +to do this. The userspace helpers executed at remounting and unmounting
1316 +aufs by default.
1317 +During this utility is running, it puts aufs into the pseudo-link
1318 +maintenance mode. In this mode, only the process which began the
1319 +maintenance mode (and its child processes) is allowed to operate in
1320 +aufs. Some other processes which are not related to the pseudo-link will
1321 +be allowed to run too, but the rest have to return an error or wait
1322 +until the maintenance mode ends. If a process already acquires an inode
1323 +mutex (in VFS), it has to return an error.
1324 +
1325 +
1326 +XIB(external inode number bitmap)
1327 +----------------------------------------------------------------------
1328 +Addition to the xino file per a branch, aufs has an external inode number
1329 +bitmap in a superblock object. It is also an internal file such like a
1330 +xino file.
1331 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1332 +not.
1333 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1334 +
1335 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1336 +reduce the number of consumed disk blocks for these files.
1337 +
1338 +
1339 +Virtual or Vertical Dir, and Readdir in Userspace
1340 +----------------------------------------------------------------------
1341 +In order to support multiple layers (branches), aufs readdir operation
1342 +constructs a virtual dir block on memory. For readdir, aufs calls
1343 +vfs_readdir() internally for each dir on branches, merges their entries
1344 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1345 +object. So the file object has its entry list until it is closed. The
1346 +entry list will be updated when the file position is zero and becomes
1347 +obsoleted. This decision is made in aufs automatically.
1348 +
1349 +The dynamically allocated memory block for the name of entries has a
1350 +unit of 512 bytes (by default) and stores the names contiguously (no
1351 +padding). Another block for each entry is handled by kmem_cache too.
1352 +During building dir blocks, aufs creates hash list and judging whether
1353 +the entry is whiteouted by its upper branch or already listed.
1354 +The merged result is cached in the corresponding inode object and
1355 +maintained by a customizable life-time option.
1356 +
1357 +Some people may call it can be a security hole or invite DoS attack
1358 +since the opened and once readdir-ed dir (file object) holds its entry
1359 +list and becomes a pressure for system memory. But I'd say it is similar
1360 +to files under /proc or /sys. The virtual files in them also holds a
1361 +memory page (generally) while they are opened. When an idea to reduce
1362 +memory for them is introduced, it will be applied to aufs too.
1363 +For those who really hate this situation, I've developed readdir(3)
1364 +library which operates this merging in userspace. You just need to set
1365 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1366 +kernel space for readdir(3).
1367 +
1368 +
1369 +Workqueue
1370 +----------------------------------------------------------------------
1371 +Aufs sometimes requires privilege access to a branch. For instance,
1372 +in copy-up/down operation. When a user process is going to make changes
1373 +to a file which exists in the lower readonly branch only, and the mode
1374 +of one of ancestor directories may not be writable by a user
1375 +process. Here aufs copy-up the file with its ancestors and they may
1376 +require privilege to set its owner/group/mode/etc.
1377 +This is a typical case of a application character of aufs (see
1378 +Introduction).
1379 +
1380 +Aufs uses workqueue synchronously for this case. It creates its own
1381 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1382 +passes the request to call mkdir or write (for example), and wait for
1383 +its completion. This approach solves a problem of a signal handler
1384 +simply.
1385 +If aufs didn't adopt the workqueue and changed the privilege of the
1386 +process, then the process may receive the unexpected SIGXFSZ or other
1387 +signals.
1388 +
1389 +Also aufs uses the system global workqueue ("events" kernel thread) too
1390 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1391 +whiteout base and etc. This is unrelated to a privilege.
1392 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1393 +superblock at the beginning, at the same time waits for the completion
1394 +of all queued asynchronous tasks.
1395 +
1396 +
1397 +Whiteout
1398 +----------------------------------------------------------------------
1399 +The whiteout in aufs is very similar to Unionfs's. That is represented
1400 +by its filename. UnionMount takes an approach of a file mode, but I am
1401 +afraid several utilities (find(1) or something) will have to support it.
1402 +
1403 +Basically the whiteout represents "logical deletion" which stops aufs to
1404 +lookup further, but also it represents "dir is opaque" which also stop
1405 +further lookup.
1406 +
1407 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1408 +In order to make several functions in a single systemcall to be
1409 +revertible, aufs adopts an approach to rename a directory to a temporary
1410 +unique whiteouted name.
1411 +For example, in rename(2) dir where the target dir already existed, aufs
1412 +renames the target dir to a temporary unique whiteouted name before the
1413 +actual rename on a branch, and then handles other actions (make it opaque,
1414 +update the attributes, etc). If an error happens in these actions, aufs
1415 +simply renames the whiteouted name back and returns an error. If all are
1416 +succeeded, aufs registers a function to remove the whiteouted unique
1417 +temporary name completely and asynchronously to the system global
1418 +workqueue.
1419 +
1420 +
1421 +Copy-up
1422 +----------------------------------------------------------------------
1423 +It is a well-known feature or concept.
1424 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1425 +internally and makes change to the new file on the upper writable branch.
1426 +When the trigger systemcall does not update the timestamps of the parent
1427 +dir, aufs reverts it after copy-up.
1428 +
1429 +
1430 +Move-down (aufs3.9 and later)
1431 +----------------------------------------------------------------------
1432 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1433 +the lower readonly branch to the upper writable branch when a user
1434 +changes something about the file.
1435 +"Move-down" is an opposite action of copy-up. Basically this action is
1436 +ran manually instead of automatically and internally.
1437 +For desgin and implementation, aufs has to consider these issues.
1438 +- whiteout for the file may exist on the lower branch.
1439 +- ancestor directories may not exist on the lower branch.
1440 +- diropq for the ancestor directories may exist on the upper branch.
1441 +- free space on the lower branch will reduce.
1442 +- another access to the file may happen during moving-down, including
1443 +  UDBA (see "Revalidate Dentry and UDBA").
1444 +- the file should not be hard-linked nor pseudo-linked. they should be
1445 +  handled by auplink utility later.
1446 +
1447 +Sometimes users want to move-down a file from the upper writable branch
1448 +to the lower readonly or writable branch. For instance,
1449 +- the free space of the upper writable branch is going to run out.
1450 +- create a new intermediate branch between the upper and lower branch.
1451 +- etc.
1452 +
1453 +For this purpose, use "aumvdown" command in aufs-util.git.
1454 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1455 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1456 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2021-12-03 15:38:59.929980643 +0100
1457 @@ -0,0 +1,85 @@
1458 +
1459 +# Copyright (C) 2015-2021 Junjiro R. Okajima
1460 +# 
1461 +# This program is free software; you can redistribute it and/or modify
1462 +# it under the terms of the GNU General Public License as published by
1463 +# the Free Software Foundation; either version 2 of the License, or
1464 +# (at your option) any later version.
1465 +# 
1466 +# This program is distributed in the hope that it will be useful,
1467 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1468 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1469 +# GNU General Public License for more details.
1470 +# 
1471 +# You should have received a copy of the GNU General Public License
1472 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1473 +
1474 +Support for a branch who has its ->atomic_open()
1475 +----------------------------------------------------------------------
1476 +The filesystems who implement its ->atomic_open() are not majority. For
1477 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1478 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1479 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1480 +sure whether all filesystems who have ->atomic_open() behave like this,
1481 +but NFSv4 surely returns the error.
1482 +
1483 +In order to support ->atomic_open() for aufs, there are a few
1484 +approaches.
1485 +
1486 +A. Introduce aufs_atomic_open()
1487 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1488 +     branch fs.
1489 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1490 +   an aufs user Pip Cet's approach
1491 +   - calls aufs_create(), VFS finish_open() and notify_change().
1492 +   - pass fake-mode to finish_open(), and then correct the mode by
1493 +     notify_change().
1494 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1495 +   - no aufs_atomic_open().
1496 +   - aufs_lookup() registers the TID to an aufs internal object.
1497 +   - aufs_create() does nothing when the matching TID is registered, but
1498 +     registers the mode.
1499 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1500 +     TID is registered.
1501 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1502 +   credential
1503 +   - no aufs_atomic_open().
1504 +   - aufs_create() registers the TID to an internal object. this info
1505 +     represents "this process created this file just now."
1506 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1507 +     registered TID and re-try open() with superuser's credential.
1508 +
1509 +Pros and cons for each approach.
1510 +
1511 +A.
1512 +   - straightforward but highly depends upon VFS internal.
1513 +   - the atomic behavaiour is kept.
1514 +   - some of parameters such as nameidata are hard to reproduce for
1515 +     branch fs.
1516 +   - large overhead.
1517 +B.
1518 +   - easy to implement.
1519 +   - the atomic behavaiour is lost.
1520 +C.
1521 +   - the atomic behavaiour is kept.
1522 +   - dirty and tricky.
1523 +   - VFS checks whether the file is created correctly after calling
1524 +     ->create(), which means this approach doesn't work.
1525 +D.
1526 +   - easy to implement.
1527 +   - the atomic behavaiour is lost.
1528 +   - to open a file with superuser's credential and give it to a user
1529 +     process is a bad idea, since the file object keeps the credential
1530 +     in it. It may affect LSM or something. This approach doesn't work
1531 +     either.
1532 +
1533 +The approach A is ideal, but it hard to implement. So here is a
1534 +variation of A, which is to be implemented.
1535 +
1536 +A-1. Introduce aufs_atomic_open()
1537 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1538 +       vfs_create() and finish_open().
1539 +     - the demerit is that the several checks after branch fs
1540 +       ->atomic_open() are lost. in the ordinary case, the checks are
1541 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1542 +       be implemented in aufs, but not all I am afraid.
1543 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1544 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1545 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2021-12-03 15:38:59.929980643 +0100
1546 @@ -0,0 +1,113 @@
1547 +
1548 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1549 +# 
1550 +# This program is free software; you can redistribute it and/or modify
1551 +# it under the terms of the GNU General Public License as published by
1552 +# the Free Software Foundation; either version 2 of the License, or
1553 +# (at your option) any later version.
1554 +# 
1555 +# This program is distributed in the hope that it will be useful,
1556 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1557 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1558 +# GNU General Public License for more details.
1559 +# 
1560 +# You should have received a copy of the GNU General Public License
1561 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1562 +
1563 +Lookup in a Branch
1564 +----------------------------------------------------------------------
1565 +Since aufs has a character of sub-VFS (see Introduction), it operates
1566 +lookup for branches as VFS does. It may be a heavy work. But almost all
1567 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1568 +directly connected to its parent. Digging down the directory hierarchy
1569 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1570 +aufs calls it.
1571 +
1572 +When a branch is a remote filesystem, aufs basically relies upon its
1573 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1574 +them.
1575 +For d_revalidate, aufs implements three levels of revalidate tests. See
1576 +"Revalidate Dentry and UDBA" in detail.
1577 +
1578 +
1579 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1580 +----------------------------------------------------------------------
1581 +Let's try case study.
1582 +- aufs has two branches, upper readwrite and lower readonly.
1583 +  /au = /rw + /ro
1584 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1585 +- user invoked "chmod a+rx /au/dirA"
1586 +- the internal copy-up is activated and "/rw/dirA" is created and its
1587 +  permission bits are set to world readable.
1588 +- then "/au/dirA" becomes world readable?
1589 +
1590 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1591 +or it may be a natively readonly filesystem. If aufs respects the lower
1592 +branch, it should not respond readdir request from other users. But user
1593 +allowed it by chmod. Should really aufs rejects showing the entries
1594 +under /ro/dirA?
1595 +
1596 +To be honest, I don't have a good solution for this case. So aufs
1597 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1598 +users.
1599 +When dirperm1 is specified, aufs checks only the highest one for the
1600 +directory permission, and shows the entries. Otherwise, as usual, checks
1601 +every dir existing on all branches and rejects the request.
1602 +
1603 +As a side effect, dirperm1 option improves the performance of aufs
1604 +because the number of permission check is reduced when the number of
1605 +branch is many.
1606 +
1607 +
1608 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1609 +----------------------------------------------------------------------
1610 +Generally VFS helpers re-validate a dentry as a part of lookup.
1611 +0. digging down the directory hierarchy.
1612 +1. lock the parent dir by its i_mutex.
1613 +2. lookup the final (child) entry.
1614 +3. revalidate it.
1615 +4. call the actual operation (create, unlink, etc.)
1616 +5. unlock the parent dir
1617 +
1618 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1619 +called. Actually aufs implements it and checks the dentry on a branch is
1620 +still valid.
1621 +But it is not enough. Because aufs has to release the lock for the
1622 +parent dir on a branch at the end of ->lookup() (step 2) and
1623 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1624 +held by VFS.
1625 +If the file on a branch is changed directly, eg. bypassing aufs, after
1626 +aufs released the lock, then the subsequent operation may cause
1627 +something unpleasant result.
1628 +
1629 +This situation is a result of VFS architecture, ->lookup() and
1630 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1631 +design from VFS's point of view. It is just not suitable for sub-VFS
1632 +character in aufs.
1633 +
1634 +Aufs supports such case by three level of revalidation which is
1635 +selectable by user.
1636 +1. Simple Revalidate
1637 +   Addition to the native flow in VFS's, confirm the child-parent
1638 +   relationship on the branch just after locking the parent dir on the
1639 +   branch in the "actual operation" (step 4). When this validation
1640 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1641 +   checks the validation of the dentry on branches.
1642 +2. Monitor Changes Internally by Inotify/Fsnotify
1643 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1644 +   the dentry on the branch, and returns EBUSY if it finds different
1645 +   dentry.
1646 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1647 +   during it is in cache. When the event is notified, aufs registers a
1648 +   function to kernel 'events' thread by schedule_work(). And the
1649 +   function sets some special status to the cached aufs dentry and inode
1650 +   private data. If they are not cached, then aufs has nothing to
1651 +   do. When the same file is accessed through aufs (step 0-3) later,
1652 +   aufs will detect the status and refresh all necessary data.
1653 +   In this mode, aufs has to ignore the event which is fired by aufs
1654 +   itself.
1655 +3. No Extra Validation
1656 +   This is the simplest test and doesn't add any additional revalidation
1657 +   test, and skip the revalidation in step 4. It is useful and improves
1658 +   aufs performance when system surely hide the aufs branches from user,
1659 +   by over-mounting something (or another method).
1660 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1661 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1662 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2021-12-03 15:38:59.929980643 +0100
1663 @@ -0,0 +1,74 @@
1664 +
1665 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1666 +# 
1667 +# This program is free software; you can redistribute it and/or modify
1668 +# it under the terms of the GNU General Public License as published by
1669 +# the Free Software Foundation; either version 2 of the License, or
1670 +# (at your option) any later version.
1671 +# 
1672 +# This program is distributed in the hope that it will be useful,
1673 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1674 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1675 +# GNU General Public License for more details.
1676 +# 
1677 +# You should have received a copy of the GNU General Public License
1678 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1679 +
1680 +Branch Manipulation
1681 +
1682 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1683 +and changing its permission/attribute, there are a lot of works to do.
1684 +
1685 +
1686 +Add a Branch
1687 +----------------------------------------------------------------------
1688 +o Confirm the adding dir exists outside of aufs, including loopback
1689 +  mount, and its various attributes.
1690 +o Initialize the xino file and whiteout bases if necessary.
1691 +  See struct.txt.
1692 +
1693 +o Check the owner/group/mode of the directory
1694 +  When the owner/group/mode of the adding directory differs from the
1695 +  existing branch, aufs issues a warning because it may impose a
1696 +  security risk.
1697 +  For example, when a upper writable branch has a world writable empty
1698 +  top directory, a malicious user can create any files on the writable
1699 +  branch directly, like copy-up and modify manually. If something like
1700 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1701 +  writable branch, and the writable branch is world-writable, then a
1702 +  malicious guy may create /etc/passwd on the writable branch directly
1703 +  and the infected file will be valid in aufs.
1704 +  I am afraid it can be a security issue, but aufs can do nothing except
1705 +  producing a warning.
1706 +
1707 +
1708 +Delete a Branch
1709 +----------------------------------------------------------------------
1710 +o Confirm the deleting branch is not busy
1711 +  To be general, there is one merit to adopt "remount" interface to
1712 +  manipulate branches. It is to discard caches. At deleting a branch,
1713 +  aufs checks the still cached (and connected) dentries and inodes. If
1714 +  there are any, then they are all in-use. An inode without its
1715 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1716 +
1717 +  For the cached one, aufs checks whether the same named entry exists on
1718 +  other branches.
1719 +  If the cached one is a directory, because aufs provides a merged view
1720 +  to users, as long as one dir is left on any branch aufs can show the
1721 +  dir to users. In this case, the branch can be removed from aufs.
1722 +  Otherwise aufs rejects deleting the branch.
1723 +
1724 +  If any file on the deleting branch is opened by aufs, then aufs
1725 +  rejects deleting.
1726 +
1727 +
1728 +Modify the Permission of a Branch
1729 +----------------------------------------------------------------------
1730 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1731 +  See struct.txt.
1732 +
1733 +o rw --> ro: Confirm the modifying branch is not busy
1734 +  Aufs rejects the request if any of these conditions are true.
1735 +  - a file on the branch is mmap-ed.
1736 +  - a regular file on the branch is opened for write and there is no
1737 +    same named entry on the upper branch.
1738 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1739 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1740 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2021-12-03 15:38:59.929980643 +0100
1741 @@ -0,0 +1,64 @@
1742 +
1743 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1744 +# 
1745 +# This program is free software; you can redistribute it and/or modify
1746 +# it under the terms of the GNU General Public License as published by
1747 +# the Free Software Foundation; either version 2 of the License, or
1748 +# (at your option) any later version.
1749 +# 
1750 +# This program is distributed in the hope that it will be useful,
1751 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1752 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1753 +# GNU General Public License for more details.
1754 +# 
1755 +# You should have received a copy of the GNU General Public License
1756 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1757 +
1758 +Policies to Select One among Multiple Writable Branches
1759 +----------------------------------------------------------------------
1760 +When the number of writable branch is more than one, aufs has to decide
1761 +the target branch for file creation or copy-up. By default, the highest
1762 +writable branch which has the parent (or ancestor) dir of the target
1763 +file is chosen (top-down-parent policy).
1764 +By user's request, aufs implements some other policies to select the
1765 +writable branch, for file creation several policies, round-robin,
1766 +most-free-space, and other policies. For copy-up, top-down-parent,
1767 +bottom-up-parent, bottom-up and others.
1768 +
1769 +As expected, the round-robin policy selects the branch in circular. When
1770 +you have two writable branches and creates 10 new files, 5 files will be
1771 +created for each branch. mkdir(2) systemcall is an exception. When you
1772 +create 10 new directories, all will be created on the same branch.
1773 +And the most-free-space policy selects the one which has most free
1774 +space among the writable branches. The amount of free space will be
1775 +checked by aufs internally, and users can specify its time interval.
1776 +
1777 +The policies for copy-up is more simple,
1778 +top-down-parent is equivalent to the same named on in create policy,
1779 +bottom-up-parent selects the writable branch where the parent dir
1780 +exists and the nearest upper one from the copyup-source,
1781 +bottom-up selects the nearest upper writable branch from the
1782 +copyup-source, regardless the existence of the parent dir.
1783 +
1784 +There are some rules or exceptions to apply these policies.
1785 +- If there is a readonly branch above the policy-selected branch and
1786 +  the parent dir is marked as opaque (a variation of whiteout), or the
1787 +  target (creating) file is whiteout-ed on the upper readonly branch,
1788 +  then the result of the policy is ignored and the target file will be
1789 +  created on the nearest upper writable branch than the readonly branch.
1790 +- If there is a writable branch above the policy-selected branch and
1791 +  the parent dir is marked as opaque or the target file is whiteouted
1792 +  on the branch, then the result of the policy is ignored and the target
1793 +  file will be created on the highest one among the upper writable
1794 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1795 +  it as usual.
1796 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1797 +  They try selecting the branch where the source exists as possible
1798 +  since copyup a large file will take long time. If it can't be,
1799 +  ie. the branch where the source exists is readonly, then they will
1800 +  follow the copyup policy.
1801 +- There is an exception for rename(2) when the target exists.
1802 +  If the rename target exists, aufs compares the index of the branches
1803 +  where the source and the target exists and selects the higher
1804 +  one. If the selected branch is readonly, then aufs follows the
1805 +  copyup policy.
1806 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1807 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1808 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2021-12-03 15:38:59.929980643 +0100
1809 @@ -0,0 +1,31 @@
1810 +
1811 +// to view this graph, run dot(1) command in GRAPHVIZ.
1812 +
1813 +digraph G {
1814 +node [shape=box];
1815 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1816 +
1817 +node [shape=oval];
1818 +
1819 +aufs_rename -> whinfo [label="store/remove"];
1820 +
1821 +node [shape=oval];
1822 +inode_list [label="h_inum list in branch\ncache"];
1823 +
1824 +node [shape=box];
1825 +whinode [label="h_inum list file"];
1826 +
1827 +node [shape=oval];
1828 +brmgmt [label="br_add/del/mod/umount"];
1829 +
1830 +brmgmt -> inode_list [label="create/remove"];
1831 +brmgmt -> whinode [label="load/store"];
1832 +
1833 +inode_list -> whinode [style=dashed,dir=both];
1834 +
1835 +aufs_rename -> inode_list [label="add/del"];
1836 +
1837 +aufs_lookup -> inode_list [label="search"];
1838 +
1839 +aufs_lookup -> whinfo [label="load/remove"];
1840 +}
1841 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1842 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1843 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2021-12-03 15:38:59.929980643 +0100
1844 @@ -0,0 +1,102 @@
1845 +
1846 +# Copyright (C) 2017-2021 Junjiro R. Okajima
1847 +#
1848 +# This program is free software; you can redistribute it and/or modify
1849 +# it under the terms of the GNU General Public License as published by
1850 +# the Free Software Foundation; either version 2 of the License, or
1851 +# (at your option) any later version.
1852 +#
1853 +# This program is distributed in the hope that it will be useful,
1854 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1855 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1856 +# GNU General Public License for more details.
1857 +#
1858 +# You should have received a copy of the GNU General Public License
1859 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1860 +
1861 +Special handling for renaming a directory (DIRREN)
1862 +----------------------------------------------------------------------
1863 +First, let's assume we have a simple usecase.
1864 +
1865 +- /u = /rw + /ro
1866 +- /rw/dirA exists
1867 +- /ro/dirA and /ro/dirA/file exist too
1868 +- there is no dirB on both branches
1869 +- a user issues rename("dirA", "dirB")
1870 +
1871 +Now, what should aufs behave against this rename(2)?
1872 +There are a few possible cases.
1873 +
1874 +A. returns EROFS.
1875 +   since dirA exists on a readonly branch which cannot be renamed.
1876 +B. returns EXDEV.
1877 +   it is possible to copy-up dirA (only the dir itself), but the child
1878 +   entries ("file" in this case) should not be. it must be a bad
1879 +   approach to copy-up recursively.
1880 +C. returns a success.
1881 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1882 +   is a violation of aufs' policy.
1883 +D. construct an extra information which indicates that /ro/dirA should
1884 +   be handled as the name of dirB.
1885 +   overlayfs has a similar feature called REDIRECT.
1886 +
1887 +Until now, aufs implements the case B only which returns EXDEV, and
1888 +expects the userspace application behaves like mv(1) which tries
1889 +issueing rename(2) recursively.
1890 +
1891 +A new aufs feature called DIRREN is introduced which implements the case
1892 +D. There are several "extra information" added.
1893 +
1894 +1. detailed info per renamed directory
1895 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1896 +2. the inode-number list of directories on a branch
1897 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1898 +
1899 +The filename of "detailed info per directory" represents the lower
1900 +branch, and its format is
1901 +- a type of the branch id
1902 +  one of these.
1903 +  + uuid (not implemented yet)
1904 +  + fsid
1905 +  + dev
1906 +- the inode-number of the branch root dir
1907 +
1908 +And it contains these info in a single regular file.
1909 +- magic number
1910 +- branch's inode-number of the logically renamed dir
1911 +- the name of the before-renamed dir
1912 +
1913 +The "detailed info per directory" file is created in aufs rename(2), and
1914 +loaded in any lookup.
1915 +The info is considered in lookup for the matching case only. Here
1916 +"matching" means that the root of branch (in the info filename) is same
1917 +to the current looking-up branch. After looking-up the before-renamed
1918 +name, the inode-number is compared. And the matched dentry is used.
1919 +
1920 +The "inode-number list of directories" is a regular file which contains
1921 +simply the inode-numbers on the branch. The file is created or updated
1922 +in removing the branch, and loaded in adding the branch. Its lifetime is
1923 +equal to the branch.
1924 +The list is refered in lookup, and when the current target inode is
1925 +found in the list, the aufs tries loading the "detailed info per
1926 +directory" and get the changed and valid name of the dir.
1927 +
1928 +Theoretically these "extra informaiton" may be able to be put into XATTR
1929 +in the dir inode. But aufs doesn't choose this way because
1930 +1. XATTR may not be supported by the branch (or its configuration)
1931 +2. XATTR may have its size limit.
1932 +3. XATTR may be less easy to convert than a regular file, when the
1933 +   format of the info is changed in the future.
1934 +At the same time, I agree that the regular file approach is much slower
1935 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1936 +better approach.
1937 +
1938 +This DIRREN feature is enabled by aufs configuration, and is activated
1939 +by a new mount option.
1940 +
1941 +For the more complicated case, there is a work with UDBA option, which
1942 +is to dected the direct access to the branches (by-passing aufs) and to
1943 +maintain the cashes in aufs. Since a single cached aufs dentry may
1944 +contains two names, before- and after-rename, the name comparision in
1945 +UDBA handler may not work correctly. In this case, the behaviour will be
1946 +equivalen to udba=reval case.
1947 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1948 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1949 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2021-12-03 15:38:59.929980643 +0100
1950 @@ -0,0 +1,120 @@
1951 +
1952 +# Copyright (C) 2011-2021 Junjiro R. Okajima
1953 +# 
1954 +# This program is free software; you can redistribute it and/or modify
1955 +# it under the terms of the GNU General Public License as published by
1956 +# the Free Software Foundation; either version 2 of the License, or
1957 +# (at your option) any later version.
1958 +# 
1959 +# This program is distributed in the hope that it will be useful,
1960 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1961 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1962 +# GNU General Public License for more details.
1963 +# 
1964 +# You should have received a copy of the GNU General Public License
1965 +# along with this program; if not, write to the Free Software
1966 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1967 +
1968 +
1969 +File-based Hierarchical Storage Management (FHSM)
1970 +----------------------------------------------------------------------
1971 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1972 +storage world. Aufs provides this feature as file-based with multiple
1973 +writable branches, based upon the principle of "Colder, the Lower".
1974 +Here the word "colder" means that the less used files, and "lower" means
1975 +that the position in the order of the stacked branches vertically.
1976 +These multiple writable branches are prioritized, ie. the topmost one
1977 +should be the fastest drive and be used heavily.
1978 +
1979 +o Characters in aufs FHSM story
1980 +- aufs itself and a new branch attribute.
1981 +- a new ioctl interface to move-down and to establish a connection with
1982 +  the daemon ("move-down" is a converse of "copy-up").
1983 +- userspace tool and daemon.
1984 +
1985 +The userspace daemon establishes a connection with aufs and waits for
1986 +the notification. The notified information is very similar to struct
1987 +statfs containing the number of consumed blocks and inodes.
1988 +When the consumed blocks/inodes of a branch exceeds the user-specified
1989 +upper watermark, the daemon activates its move-down process until the
1990 +consumed blocks/inodes reaches the user-specified lower watermark.
1991 +
1992 +The actual move-down is done by aufs based upon the request from
1993 +user-space since we need to maintain the inode number and the internal
1994 +pointer arrays in aufs.
1995 +
1996 +Currently aufs FHSM handles the regular files only. Additionally they
1997 +must not be hard-linked nor pseudo-linked.
1998 +
1999 +
2000 +o Cowork of aufs and the user-space daemon
2001 +  During the userspace daemon established the connection, aufs sends a
2002 +  small notification to it whenever aufs writes something into the
2003 +  writable branch. But it may cost high since aufs issues statfs(2)
2004 +  internally. So user can specify a new option to cache the
2005 +  info. Actually the notification is controlled by these factors.
2006 +  + the specified cache time.
2007 +  + classified as "force" by aufs internally.
2008 +  Until the specified time expires, aufs doesn't send the info
2009 +  except the forced cases. When aufs decide forcing, the info is always
2010 +  notified to userspace.
2011 +  For example, the number of free inodes is generally large enough and
2012 +  the shortage of it happens rarely. So aufs doesn't force the
2013 +  notification when creating a new file, directory and others. This is
2014 +  the typical case which aufs doesn't force.
2015 +  When aufs writes the actual filedata and the files consumes any of new
2016 +  blocks, the aufs forces notifying.
2017 +
2018 +
2019 +o Interfaces in aufs
2020 +- New branch attribute.
2021 +  + fhsm
2022 +    Specifies that the branch is managed by FHSM feature. In other word,
2023 +    participant in the FHSM.
2024 +    When nofhsm is set to the branch, it will not be the source/target
2025 +    branch of the move-down operation. This attribute is set
2026 +    independently from coo and moo attributes, and if you want full
2027 +    FHSM, you should specify them as well.
2028 +- New mount option.
2029 +  + fhsm_sec
2030 +    Specifies a second to suppress many less important info to be
2031 +    notified.
2032 +- New ioctl.
2033 +  + AUFS_CTL_FHSM_FD
2034 +    create a new file descriptor which userspace can read the notification
2035 +    (a subset of struct statfs) from aufs.
2036 +- Module parameter 'brs'
2037 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2038 +  be set.
2039 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2040 +  When there are two or more branches with fhsm attributes,
2041 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2042 +  terminates it. As a result of remounting and branch-manipulation, the
2043 +  number of branches with fhsm attribute can be one. In this case,
2044 +  /sbin/mount.aufs will terminate the user-space daemon.
2045 +
2046 +
2047 +Finally the operation is done as these steps in kernel-space.
2048 +- make sure that,
2049 +  + no one else is using the file.
2050 +  + the file is not hard-linked.
2051 +  + the file is not pseudo-linked.
2052 +  + the file is a regular file.
2053 +  + the parent dir is not opaqued.
2054 +- find the target writable branch.
2055 +- make sure the file is not whiteout-ed by the upper (than the target)
2056 +  branch.
2057 +- make the parent dir on the target branch.
2058 +- mutex lock the inode on the branch.
2059 +- unlink the whiteout on the target branch (if exists).
2060 +- lookup and create the whiteout-ed temporary name on the target branch.
2061 +- copy the file as the whiteout-ed temporary name on the target branch.
2062 +- rename the whiteout-ed temporary name to the original name.
2063 +- unlink the file on the source branch.
2064 +- maintain the internal pointer array and the external inode number
2065 +  table (XINO).
2066 +- maintain the timestamps and other attributes of the parent dir and the
2067 +  file.
2068 +
2069 +And of course, in every step, an error may happen. So the operation
2070 +should restore the original file state after an error happens.
2071 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2072 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2073 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2021-12-03 15:38:59.929980643 +0100
2074 @@ -0,0 +1,72 @@
2075 +
2076 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2077 +# 
2078 +# This program is free software; you can redistribute it and/or modify
2079 +# it under the terms of the GNU General Public License as published by
2080 +# the Free Software Foundation; either version 2 of the License, or
2081 +# (at your option) any later version.
2082 +# 
2083 +# This program is distributed in the hope that it will be useful,
2084 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2085 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2086 +# GNU General Public License for more details.
2087 +# 
2088 +# You should have received a copy of the GNU General Public License
2089 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2090 +
2091 +mmap(2) -- File Memory Mapping
2092 +----------------------------------------------------------------------
2093 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2094 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2095 +->mmap().
2096 +This approach is simple and good, but there is one problem.
2097 +Under /proc, several entries show the mmapped files by its path (with
2098 +device and inode number), and the printed path will be the path on the
2099 +branch fs's instead of virtual aufs's.
2100 +This is not a problem in most cases, but some utilities lsof(1) (and its
2101 +user) may expect the path on aufs.
2102 +
2103 +To address this issue, aufs adds a new member called vm_prfile in struct
2104 +vm_area_struct (and struct vm_region). The original vm_file points to
2105 +the file on the branch fs in order to handle everything correctly as
2106 +usual. The new vm_prfile points to a virtual file in aufs, and the
2107 +show-functions in procfs refers to vm_prfile if it is set.
2108 +Also we need to maintain several other places where touching vm_file
2109 +such like
2110 +- fork()/clone() copies vma and the reference count of vm_file is
2111 +  incremented.
2112 +- merging vma maintains the ref count too.
2113 +
2114 +This is not a good approach. It just fakes the printed path. But it
2115 +leaves all behaviour around f_mapping unchanged. This is surely an
2116 +advantage.
2117 +Actually aufs had adopted another complicated approach which calls
2118 +generic_file_mmap() and handles struct vm_operations_struct. In this
2119 +approach, aufs met a hard problem and I could not solve it without
2120 +switching the approach.
2121 +
2122 +There may be one more another approach which is
2123 +- bind-mount the branch-root onto the aufs-root internally
2124 +- grab the new vfsmount (ie. struct mount)
2125 +- lazy-umount the branch-root internally
2126 +- in open(2) the aufs-file, open the branch-file with the hidden
2127 +  vfsmount (instead of the original branch's vfsmount)
2128 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2129 +  but it may be possible from userspace by the mount helper.
2130 +
2131 +Adding the internal hidden vfsmount and using it in opening a file, the
2132 +file path under /proc will be printed correctly. This approach looks
2133 +smarter, but is not possible I am afraid.
2134 +- aufs-root may be bind-mount later. when it happens, another hidden
2135 +  vfsmount will be required.
2136 +- it is hard to get the chance to bind-mount and lazy-umount
2137 +  + in kernel-space, FS can have vfsmount in open(2) via
2138 +    file->f_path, and aufs can know its vfsmount. But several locks are
2139 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2140 +    here, then it may cause a deadlock.
2141 +  + in user-space, bind-mount doesn't invoke the mount helper.
2142 +- since /proc shows dev and ino, aufs has to give vma these info. it
2143 +  means a new member vm_prinode will be necessary. this is essentially
2144 +  equivalent to vm_prfile described above.
2145 +
2146 +I have to give up this "looks-smater" approach.
2147 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2148 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2149 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2021-12-03 15:38:59.929980643 +0100
2150 @@ -0,0 +1,96 @@
2151 +
2152 +# Copyright (C) 2014-2021 Junjiro R. Okajima
2153 +#
2154 +# This program is free software; you can redistribute it and/or modify
2155 +# it under the terms of the GNU General Public License as published by
2156 +# the Free Software Foundation; either version 2 of the License, or
2157 +# (at your option) any later version.
2158 +#
2159 +# This program is distributed in the hope that it will be useful,
2160 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2161 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2162 +# GNU General Public License for more details.
2163 +#
2164 +# You should have received a copy of the GNU General Public License
2165 +# along with this program; if not, write to the Free Software
2166 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2167 +
2168 +
2169 +Listing XATTR/EA and getting the value
2170 +----------------------------------------------------------------------
2171 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2172 +shows the values from the topmost existing file. This behaviour is good
2173 +for the non-dir entries since the bahaviour exactly matches the shown
2174 +information. But for the directories, aufs considers all the same named
2175 +entries on the lower branches. Which means, if one of the lower entry
2176 +rejects readdir call, then aufs returns an error even if the topmost
2177 +entry allows it. This behaviour is necessary to respect the branch fs's
2178 +security, but can make users confused since the user-visible standard
2179 +attributes don't match the behaviour.
2180 +To address this issue, aufs has a mount option called dirperm1 which
2181 +checks the permission for the topmost entry only, and ignores the lower
2182 +entry's permission.
2183 +
2184 +A similar issue can happen around XATTR.
2185 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2186 +always set. Otherwise these very unpleasant situation would happen.
2187 +- listxattr(2) may return the duplicated entries.
2188 +- users may not be able to remove or reset the XATTR forever,
2189 +
2190 +
2191 +XATTR/EA support in the internal (copy,move)-(up,down)
2192 +----------------------------------------------------------------------
2193 +Generally the extended attributes of inode are categorized as these.
2194 +- "security" for LSM and capability.
2195 +- "system" for posix ACL, 'acl' mount option is required for the branch
2196 +  fs generally.
2197 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2198 +- "user" for userspace, 'user_xattr' mount option is required for the
2199 +  branch fs generally.
2200 +
2201 +Moreover there are some other categories. Aufs handles these rather
2202 +unpopular categories as the ordinary ones, ie. there is no special
2203 +condition nor exception.
2204 +
2205 +In copy-up, the support for XATTR on the dst branch may differ from the
2206 +src branch. In this case, the copy-up operation will get an error and
2207 +the original user operation which triggered the copy-up will fail. It
2208 +can happen that even all copy-up will fail.
2209 +When both of src and dst branches support XATTR and if an error occurs
2210 +during copying XATTR, then the copy-up should fail obviously. That is a
2211 +good reason and aufs should return an error to userspace. But when only
2212 +the src branch support that XATTR, aufs should not return an error.
2213 +For example, the src branch supports ACL but the dst branch doesn't
2214 +because the dst branch may natively un-support it or temporary
2215 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2216 +may NOT return an error even if the XATTR is not supported. It is
2217 +totally up to the branch fs.
2218 +
2219 +Anyway when the aufs internal copy-up gets an error from the dst branch
2220 +fs, then aufs tries removing the just copied entry and returns the error
2221 +to the userspace. The worst case of this situation will be all copy-up
2222 +will fail.
2223 +
2224 +For the copy-up operation, there two basic approaches.
2225 +- copy the specified XATTR only (by category above), and return the
2226 +  error unconditionally if it happens.
2227 +- copy all XATTR, and ignore the error on the specified category only.
2228 +
2229 +In order to support XATTR and to implement the correct behaviour, aufs
2230 +chooses the latter approach and introduces some new branch attributes,
2231 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2232 +They correspond to the XATTR namespaces (see above). Additionally, to be
2233 +convenient, "icex" is also provided which means all "icex*" attributes
2234 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2235 +
2236 +The meaning of these attributes is to ignore the error from setting
2237 +XATTR on that branch.
2238 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2239 +error from the dst branch according to the specified attributes.
2240 +
2241 +Some XATTR may have its default value. The default value may come from
2242 +the parent dir or the environment. If the default value is set at the
2243 +file creating-time, it will be overwritten by copy-up.
2244 +Some contradiction may happen I am afraid.
2245 +Do we need another attribute to stop copying XATTR? I am unsure. For
2246 +now, aufs implements the branch attributes to ignore the error.
2247 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2248 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2249 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2021-12-03 15:38:59.929980643 +0100
2250 @@ -0,0 +1,58 @@
2251 +
2252 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2253 +# 
2254 +# This program is free software; you can redistribute it and/or modify
2255 +# it under the terms of the GNU General Public License as published by
2256 +# the Free Software Foundation; either version 2 of the License, or
2257 +# (at your option) any later version.
2258 +# 
2259 +# This program is distributed in the hope that it will be useful,
2260 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2261 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2262 +# GNU General Public License for more details.
2263 +# 
2264 +# You should have received a copy of the GNU General Public License
2265 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2266 +
2267 +Export Aufs via NFS
2268 +----------------------------------------------------------------------
2269 +Here is an approach.
2270 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2271 +  generation.
2272 +- iget_locked(): initialize aufs inode generation for a new inode, and
2273 +  store it in xigen file.
2274 +- destroy_inode(): increment aufs inode generation and store it in xigen
2275 +  file. it is necessary even if it is not unlinked, because any data of
2276 +  inode may be changed by UDBA.
2277 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2278 +  build file handle by
2279 +  + branch id (4 bytes)
2280 +  + superblock generation (4 bytes)
2281 +  + inode number (4 or 8 bytes)
2282 +  + parent dir inode number (4 or 8 bytes)
2283 +  + inode generation (4 bytes))
2284 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2285 +    bytes)
2286 +  + file handle for a branch (by exportfs_encode_fh())
2287 +- fh_to_dentry():
2288 +  + find the index of a branch from its id in handle, and check it is
2289 +    still exist in aufs.
2290 +  + 1st level: get the inode number from handle and search it in cache.
2291 +  + 2nd level: if not found in cache, get the parent inode number from
2292 +    the handle and search it in cache. and then open the found parent
2293 +    dir, find the matching inode number by vfs_readdir() and get its
2294 +    name, and call lookup_one_len() for the target dentry.
2295 +  + 3rd level: if the parent dir is not cached, call
2296 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2297 +    build a pathname of it, convert it a pathname in aufs, call
2298 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2299 +    the 2nd level.
2300 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2301 +    for every branch, but not itself. to get this, (currently) aufs
2302 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2303 +    idea, but I didn't get other approach.
2304 +  + test the generation of the gotten inode.
2305 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2306 +  convert it into ESTALE for NFSD.
2307 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2308 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2309 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2310 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2311 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2021-12-03 15:38:59.929980643 +0100
2312 @@ -0,0 +1,52 @@
2313 +
2314 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2315 +# 
2316 +# This program is free software; you can redistribute it and/or modify
2317 +# it under the terms of the GNU General Public License as published by
2318 +# the Free Software Foundation; either version 2 of the License, or
2319 +# (at your option) any later version.
2320 +# 
2321 +# This program is distributed in the hope that it will be useful,
2322 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2323 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2324 +# GNU General Public License for more details.
2325 +# 
2326 +# You should have received a copy of the GNU General Public License
2327 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2328 +
2329 +Show Whiteout Mode (shwh)
2330 +----------------------------------------------------------------------
2331 +Generally aufs hides the name of whiteouts. But in some cases, to show
2332 +them is very useful for users. For instance, creating a new middle layer
2333 +(branch) by merging existing layers.
2334 +
2335 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2336 +When you have three branches,
2337 +- Bottom: 'system', squashfs (underlying base system), read-only
2338 +- Middle: 'mods', squashfs, read-only
2339 +- Top: 'overlay', ram (tmpfs), read-write
2340 +
2341 +The top layer is loaded at boot time and saved at shutdown, to preserve
2342 +the changes made to the system during the session.
2343 +When larger changes have been made, or smaller changes have accumulated,
2344 +the size of the saved top layer data grows. At this point, it would be
2345 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2346 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2347 +restoring save and load speed.
2348 +
2349 +This merging is simplified by the use of another aufs mount, of just the
2350 +two overlay branches using the 'shwh' option.
2351 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2352 +       aufs /livesys/merge_union
2353 +
2354 +A merged view of these two branches is then available at
2355 +/livesys/merge_union, and the new feature is that the whiteouts are
2356 +visible!
2357 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2358 +writing to all branches. Also the default mode for all branches is 'ro'.
2359 +It is now possible to save the combined contents of the two overlay
2360 +branches to a new squashfs, e.g.:
2361 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2362 +
2363 +This new squashfs archive can be stored on the boot device and the
2364 +initramfs will use it to replace the old one at the next boot.
2365 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2366 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2367 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2021-12-03 15:38:59.929980643 +0100
2368 @@ -0,0 +1,47 @@
2369 +
2370 +# Copyright (C) 2010-2021 Junjiro R. Okajima
2371 +#
2372 +# This program is free software; you can redistribute it and/or modify
2373 +# it under the terms of the GNU General Public License as published by
2374 +# the Free Software Foundation; either version 2 of the License, or
2375 +# (at your option) any later version.
2376 +#
2377 +# This program is distributed in the hope that it will be useful,
2378 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2379 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2380 +# GNU General Public License for more details.
2381 +#
2382 +# You should have received a copy of the GNU General Public License
2383 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2384 +
2385 +Dynamically customizable FS operations
2386 +----------------------------------------------------------------------
2387 +Generally FS operations (struct inode_operations, struct
2388 +address_space_operations, struct file_operations, etc.) are defined as
2389 +"static const", but it never means that FS have only one set of
2390 +operation. Some FS have multiple sets of them. For instance, ext2 has
2391 +three sets, one for XIP, for NOBH, and for normal.
2392 +Since aufs overrides and redirects these operations, sometimes aufs has
2393 +to change its behaviour according to the branch FS type. More importantly
2394 +VFS acts differently if a function (member in the struct) is set or
2395 +not. It means aufs should have several sets of operations and select one
2396 +among them according to the branch FS definition.
2397 +
2398 +In order to solve this problem and not to affect the behaviour of VFS,
2399 +aufs defines these operations dynamically. For instance, aufs defines
2400 +dummy direct_IO function for struct address_space_operations, but it may
2401 +not be set to the address_space_operations actually. When the branch FS
2402 +doesn't have it, aufs doesn't set it to its address_space_operations
2403 +while the function definition itself is still alive. So the behaviour
2404 +itself will not change, and it will return an error when direct_IO is
2405 +not set.
2406 +
2407 +The lifetime of these dynamically generated operation object is
2408 +maintained by aufs branch object. When the branch is removed from aufs,
2409 +the reference counter of the object is decremented. When it reaches
2410 +zero, the dynamically generated operation object will be freed.
2411 +
2412 +This approach is designed to support AIO (io_submit), Direct I/O and
2413 +XIP (DAX) mainly.
2414 +Currently this approach is applied to address_space_operations for
2415 +regular files only.
2416 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2417 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2418 +++ linux/Documentation/filesystems/aufs/README 2021-12-03 15:40:58.233313963 +0100
2419 @@ -0,0 +1,396 @@
2420 +
2421 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2422 +http://aufs.sf.net
2423 +Junjiro R. Okajima
2424 +
2425 +
2426 +0. Introduction
2427 +----------------------------------------
2428 +In the early days, aufs was entirely re-designed and re-implemented
2429 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2430 +improvements and implementations, it became totally different from
2431 +Unionfs while keeping the basic features.
2432 +Later, Unionfs Version 2.x series began taking some of the same
2433 +approaches to aufs1's.
2434 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2435 +University and his team.
2436 +
2437 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2438 +support,
2439 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2440 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2441 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2442 +  or aufs1 from CVS on SourceForge.
2443 +
2444 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2445 +      According to Christoph Hellwig, linux rejects all union-type
2446 +      filesystems but UnionMount.
2447 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2448 +
2449 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2450 +    UnionMount, and he pointed out an issue around a directory mutex
2451 +    lock and aufs addressed it. But it is still unsure whether aufs will
2452 +    be merged (or any other union solution).
2453 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2454 +
2455 +
2456 +1. Features
2457 +----------------------------------------
2458 +- unite several directories into a single virtual filesystem. The member
2459 +  directory is called as a branch.
2460 +- you can specify the permission flags to the branch, which are 'readonly',
2461 +  'readwrite' and 'whiteout-able.'
2462 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2463 +  readonly branch are modifiable logically.
2464 +- dynamic branch manipulation, add, del.
2465 +- etc...
2466 +
2467 +Also there are many enhancements in aufs, such as:
2468 +- test only the highest one for the directory permission (dirperm1)
2469 +- copyup on open (coo=)
2470 +- 'move' policy for copy-up between two writable branches, after
2471 +  checking free space.
2472 +- xattr, acl
2473 +- readdir(3) in userspace.
2474 +- keep inode number by external inode number table
2475 +- keep the timestamps of file/dir in internal copyup operation
2476 +- seekable directory, supporting NFS readdir.
2477 +- whiteout is hardlinked in order to reduce the consumption of inodes
2478 +  on branch
2479 +- do not copyup, nor create a whiteout when it is unnecessary
2480 +- revert a single systemcall when an error occurs in aufs
2481 +- remount interface instead of ioctl
2482 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2483 +- loopback mounted filesystem as a branch
2484 +- kernel thread for removing the dir who has a plenty of whiteouts
2485 +- support copyup sparse file (a file which has a 'hole' in it)
2486 +- default permission flags for branches
2487 +- selectable permission flags for ro branch, whether whiteout can
2488 +  exist or not
2489 +- export via NFS.
2490 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2491 +- support multiple writable branches, some policies to select one
2492 +  among multiple writable branches.
2493 +- a new semantics for link(2) and rename(2) to support multiple
2494 +  writable branches.
2495 +- no glibc changes are required.
2496 +- pseudo hardlink (hardlink over branches)
2497 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2498 +  including NFS or remote filesystem branch.
2499 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2500 +- and more...
2501 +
2502 +Currently these features are dropped temporary from aufs5.
2503 +See design/08plan.txt in detail.
2504 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2505 +  (robr)
2506 +- statistics of aufs thread (/sys/fs/aufs/stat)
2507 +
2508 +Features or just an idea in the future (see also design/*.txt),
2509 +- reorder the branch index without del/re-add.
2510 +- permanent xino files for NFSD
2511 +- an option for refreshing the opened files after add/del branches
2512 +- light version, without branch manipulation. (unnecessary?)
2513 +- copyup in userspace
2514 +- inotify in userspace
2515 +- readv/writev
2516 +
2517 +
2518 +2. Download
2519 +----------------------------------------
2520 +There are three GIT trees for aufs5, aufs5-linux.git,
2521 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2522 +"aufs-util.git."
2523 +While the aufs-util is always necessary, you need either of aufs5-linux
2524 +or aufs5-standalone.
2525 +
2526 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2527 +git://git.kernel.org/.../torvalds/linux.git.
2528 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2529 +build aufs5 as an external kernel module.
2530 +Several extra patches are not included in this tree. Only
2531 +aufs5-standalone tree contains them. They are described in the later
2532 +section "Configuration and Compilation."
2533 +
2534 +On the other hand, the aufs5-standalone tree has only aufs source files
2535 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2536 +But you need to apply all aufs patches manually.
2537 +
2538 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2539 +represents the linux kernel version, "linux-5.x". For instance,
2540 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2541 +"aufs5.x-rcN" branch.
2542 +
2543 +o aufs5-linux tree
2544 +$ git clone --reference /your/linux/git/tree \
2545 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2546 +- if you don't have linux GIT tree, then remove "--reference ..."
2547 +$ cd aufs5-linux.git
2548 +$ git checkout origin/aufs5.0
2549 +
2550 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2551 +leave the patch-work to GIT.
2552 +$ cd /your/linux/git/tree
2553 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2554 +$ git fetch aufs5
2555 +$ git checkout -b my5.0 v5.0
2556 +$ (add your local change...)
2557 +$ git pull aufs5 aufs5.0
2558 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2559 +- you may need to solve some conflicts between your_changes and
2560 +  aufs5.0. in this case, git-rerere is recommended so that you can
2561 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2562 +  later in the future.
2563 +
2564 +o aufs5-standalone tree
2565 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2566 +$ cd aufs5-standalone.git
2567 +$ git checkout origin/aufs5.0
2568 +
2569 +o aufs-util tree
2570 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2571 +- note that the public aufs-util.git is on SourceForge instead of
2572 +  GitHUB.
2573 +$ cd aufs-util.git
2574 +$ git checkout origin/aufs5.0
2575 +
2576 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2577 +The minor version number, 'x' in '5.x', of aufs may not always
2578 +follow the minor version number of the kernel.
2579 +Because changes in the kernel that cause the use of a new
2580 +minor version number do not always require changes to aufs-util.
2581 +
2582 +Since aufs-util has its own minor version number, you may not be
2583 +able to find a GIT branch in aufs-util for your kernel's
2584 +exact minor version number.
2585 +In this case, you should git-checkout the branch for the
2586 +nearest lower number.
2587 +
2588 +For (an unreleased) example:
2589 +If you are using "linux-5.10" and the "aufs5.10" branch
2590 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2591 +or something numerically smaller is the branch for your kernel.
2592 +
2593 +Also you can view all branches by
2594 +       $ git branch -a
2595 +
2596 +
2597 +3. Configuration and Compilation
2598 +----------------------------------------
2599 +Make sure you have git-checkout'ed the correct branch.
2600 +
2601 +For aufs5-linux tree,
2602 +- enable CONFIG_AUFS_FS.
2603 +- set other aufs configurations if necessary.
2604 +- for aufs5.13 and later
2605 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2606 +  also a caller of VFS functions for branch filesystems, subclassing of
2607 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2608 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2609 +  need to customize some LOCKDEP numbers. Here are what I use on my
2610 +  test environment.
2611 +       CONFIG_LOCKDEP_BITS=21
2612 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2613 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2614 +
2615 +For aufs5-standalone tree,
2616 +There are several ways to build.
2617 +
2618 +1.
2619 +- apply ./aufs5-kbuild.patch to your kernel source files.
2620 +- apply ./aufs5-base.patch too.
2621 +- apply ./aufs5-mmap.patch too.
2622 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2623 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2624 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2625 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2626 +- enable CONFIG_AUFS_FS, you can select either
2627 +  =m or =y.
2628 +- and build your kernel as usual.
2629 +- install the built kernel.
2630 +- install the header files too by "make headers_install" to the
2631 +  directory where you specify. By default, it is $PWD/usr.
2632 +  "make help" shows a brief note for headers_install.
2633 +- and reboot your system.
2634 +
2635 +2.
2636 +- module only (CONFIG_AUFS_FS=m).
2637 +- apply ./aufs5-base.patch to your kernel source files.
2638 +- apply ./aufs5-mmap.patch too.
2639 +- apply ./aufs5-standalone.patch too.
2640 +- build your kernel, don't forget "make headers_install", and reboot.
2641 +- edit ./config.mk and set other aufs configurations if necessary.
2642 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2643 +  every aufs configurations.
2644 +- build the module by simple "make".
2645 +- you can specify ${KDIR} make variable which points to your kernel
2646 +  source tree.
2647 +- install the files
2648 +  + run "make install" to install the aufs module, or copy the built
2649 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2650 +  + run "make install_headers" (instead of headers_install) to install
2651 +    the modified aufs header file (you can specify DESTDIR which is
2652 +    available in aufs standalone version's Makefile only), or copy
2653 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2654 +    you like manually. By default, the target directory is $PWD/usr.
2655 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2656 +  kernel source tree.
2657 +
2658 +Note: The header file aufs_type.h is necessary to build aufs-util
2659 +      as well as "make headers_install" in the kernel source tree.
2660 +      headers_install is subject to be forgotten, but it is essentially
2661 +      necessary, not only for building aufs-util.
2662 +      You may not meet problems without headers_install in some older
2663 +      version though.
2664 +
2665 +And then,
2666 +- read README in aufs-util, build and install it
2667 +- note that your distribution may contain an obsoleted version of
2668 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2669 +  utilities, make sure that your compiler refers the correct aufs header
2670 +  file which is built by "make headers_install."
2671 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2672 +  then run "make install_ulib" too. And refer to the aufs manual in
2673 +  detail.
2674 +
2675 +There several other patches in aufs5-standalone.git. They are all
2676 +optional. When you meet some problems, they will help you.
2677 +- aufs5-loopback.patch
2678 +  Supports a nested loopback mount in a branch-fs. This patch is
2679 +  unnecessary until aufs produces a message like "you may want to try
2680 +  another patch for loopback file".
2681 +- vfs-ino.patch
2682 +  Modifies a system global kernel internal function get_next_ino() in
2683 +  order to stop assigning 0 for an inode-number. Not directly related to
2684 +  aufs, but recommended generally.
2685 +- tmpfs-idr.patch
2686 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2687 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2688 +  duplication of inode number, which is important for backup tools and
2689 +  other utilities. When you find aufs XINO files for tmpfs branch
2690 +  growing too much, try this patch.
2691 +
2692 +
2693 +4. Usage
2694 +----------------------------------------
2695 +At first, make sure aufs-util are installed, and please read the aufs
2696 +manual, aufs.5 in aufs-util.git tree.
2697 +$ man -l aufs.5
2698 +
2699 +And then,
2700 +$ mkdir /tmp/rw /tmp/aufs
2701 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2702 +
2703 +Here is another example. The result is equivalent.
2704 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2705 +  Or
2706 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2707 +# mount -o remount,append:${HOME} /tmp/aufs
2708 +
2709 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2710 +you modify a file under /tmp/aufs, the one on your home directory is
2711 +not affected, instead the same named file will be newly created under
2712 +/tmp/rw. And all of your modification to a file will be applied to
2713 +the one under /tmp/rw. This is called the file based Copy on Write
2714 +(COW) method.
2715 +Aufs mount options are described in aufs.5.
2716 +If you run chroot or something and make your aufs as a root directory,
2717 +then you need to customize the shutdown script. See the aufs manual in
2718 +detail.
2719 +
2720 +Additionally, there are some sample usages of aufs which are a
2721 +diskless system with network booting, and LiveCD over NFS.
2722 +See sample dir in CVS tree on SourceForge.
2723 +
2724 +
2725 +5. Contact
2726 +----------------------------------------
2727 +When you have any problems or strange behaviour in aufs, please let me
2728 +know with:
2729 +- /proc/mounts (instead of the output of mount(8))
2730 +- /sys/module/aufs/*
2731 +- /sys/fs/aufs/* (if you have them)
2732 +- /debug/aufs/* (if you have them)
2733 +- linux kernel version
2734 +  if your kernel is not plain, for example modified by distributor,
2735 +  the url where i can download its source is necessary too.
2736 +- aufs version which was printed at loading the module or booting the
2737 +  system, instead of the date you downloaded.
2738 +- configuration (define/undefine CONFIG_AUFS_xxx)
2739 +- kernel configuration or /proc/config.gz (if you have it)
2740 +- LSM (linux security module, if you are using)
2741 +- behaviour which you think to be incorrect
2742 +- actual operation, reproducible one is better
2743 +- mailto: aufs-users at lists.sourceforge.net
2744 +
2745 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2746 +and Feature Requests) on SourceForge. Please join and write to
2747 +aufs-users ML.
2748 +
2749 +
2750 +6. Acknowledgements
2751 +----------------------------------------
2752 +Thanks to everyone who have tried and are using aufs, whoever
2753 +have reported a bug or any feedback.
2754 +
2755 +Especially donators:
2756 +Tomas Matejicek(slax.org) made a donation (much more than once).
2757 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2758 +       scripts) is making "doubling" donations.
2759 +       Unfortunately I cannot list all of the donators, but I really
2760 +       appreciate.
2761 +       It ends Aug 2010, but the ordinary donation URL is still available.
2762 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2763 +Dai Itasaka made a donation (2007/8).
2764 +Chuck Smith made a donation (2008/4, 10 and 12).
2765 +Henk Schoneveld made a donation (2008/9).
2766 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2767 +Francois Dupoux made a donation (2008/11).
2768 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2769 +       aufs2 GIT tree (2009/2).
2770 +William Grant made a donation (2009/3).
2771 +Patrick Lane made a donation (2009/4).
2772 +The Mail Archive (mail-archive.com) made donations (2009/5).
2773 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2774 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2775 +Pavel Pronskiy made a donation (2011/2).
2776 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2777 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2778 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2779 +11).
2780 +Sam Liddicott made a donation (2011/9).
2781 +Era Scarecrow made a donation (2013/4).
2782 +Bor Ratajc made a donation (2013/4).
2783 +Alessandro Gorreta made a donation (2013/4).
2784 +POIRETTE Marc made a donation (2013/4).
2785 +Alessandro Gorreta made a donation (2013/4).
2786 +lauri kasvandik made a donation (2013/5).
2787 +"pemasu from Finland" made a donation (2013/7).
2788 +The Parted Magic Project made a donation (2013/9 and 11).
2789 +Pavel Barta made a donation (2013/10).
2790 +Nikolay Pertsev made a donation (2014/5).
2791 +James B made a donation (2014/7 and 2015/7).
2792 +Stefano Di Biase made a donation (2014/8).
2793 +Daniel Epellei made a donation (2015/1).
2794 +OmegaPhil made a donation (2016/1, 2018/4).
2795 +Tomasz Szewczyk made a donation (2016/4).
2796 +James Burry made a donation (2016/12).
2797 +Carsten Rose made a donation (2018/9).
2798 +Porteus Kiosk made a donation (2018/10).
2799 +
2800 +Thank you very much.
2801 +Donations are always, including future donations, very important and
2802 +helpful for me to keep on developing aufs.
2803 +
2804 +
2805 +7.
2806 +----------------------------------------
2807 +If you are an experienced user, no explanation is needed. Aufs is
2808 +just a linux filesystem.
2809 +
2810 +
2811 +Enjoy!
2812 +
2813 +# Local variables: ;
2814 +# mode: text;
2815 +# End: ;
2816 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2817 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2818 +++ linux/fs/aufs/aufs.h        2021-12-03 15:38:59.929980643 +0100
2819 @@ -0,0 +1,62 @@
2820 +/* SPDX-License-Identifier: GPL-2.0 */
2821 +/*
2822 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2823 + *
2824 + * This program, aufs is free software; you can redistribute it and/or modify
2825 + * it under the terms of the GNU General Public License as published by
2826 + * the Free Software Foundation; either version 2 of the License, or
2827 + * (at your option) any later version.
2828 + *
2829 + * This program is distributed in the hope that it will be useful,
2830 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2831 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2832 + * GNU General Public License for more details.
2833 + *
2834 + * You should have received a copy of the GNU General Public License
2835 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2836 + */
2837 +
2838 +/*
2839 + * all header files
2840 + */
2841 +
2842 +#ifndef __AUFS_H__
2843 +#define __AUFS_H__
2844 +
2845 +#ifdef __KERNEL__
2846 +
2847 +#define AuStub(type, name, body, ...) \
2848 +       static inline type name(__VA_ARGS__) { body; }
2849 +
2850 +#define AuStubVoid(name, ...) \
2851 +       AuStub(void, name, , __VA_ARGS__)
2852 +#define AuStubInt0(name, ...) \
2853 +       AuStub(int, name, return 0, __VA_ARGS__)
2854 +
2855 +#include "debug.h"
2856 +
2857 +#include "branch.h"
2858 +#include "cpup.h"
2859 +#include "dcsub.h"
2860 +#include "dbgaufs.h"
2861 +#include "dentry.h"
2862 +#include "dir.h"
2863 +#include "dirren.h"
2864 +#include "dynop.h"
2865 +#include "file.h"
2866 +#include "fstype.h"
2867 +#include "hbl.h"
2868 +#include "inode.h"
2869 +#include "lcnt.h"
2870 +#include "loop.h"
2871 +#include "module.h"
2872 +#include "opts.h"
2873 +#include "rwsem.h"
2874 +#include "super.h"
2875 +#include "sysaufs.h"
2876 +#include "vfsub.h"
2877 +#include "whout.h"
2878 +#include "wkq.h"
2879 +
2880 +#endif /* __KERNEL__ */
2881 +#endif /* __AUFS_H__ */
2882 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2883 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2884 +++ linux/fs/aufs/branch.c      2021-12-03 15:38:59.933313976 +0100
2885 @@ -0,0 +1,1427 @@
2886 +// SPDX-License-Identifier: GPL-2.0
2887 +/*
2888 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2889 + *
2890 + * This program, aufs is free software; you can redistribute it and/or modify
2891 + * it under the terms of the GNU General Public License as published by
2892 + * the Free Software Foundation; either version 2 of the License, or
2893 + * (at your option) any later version.
2894 + *
2895 + * This program is distributed in the hope that it will be useful,
2896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2898 + * GNU General Public License for more details.
2899 + *
2900 + * You should have received a copy of the GNU General Public License
2901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2902 + */
2903 +
2904 +/*
2905 + * branch management
2906 + */
2907 +
2908 +#include <linux/compat.h>
2909 +#include <linux/statfs.h>
2910 +#include "aufs.h"
2911 +
2912 +/*
2913 + * free a single branch
2914 + */
2915 +static void au_br_do_free(struct au_branch *br)
2916 +{
2917 +       int i;
2918 +       struct au_wbr *wbr;
2919 +       struct au_dykey **key;
2920 +
2921 +       au_hnotify_fin_br(br);
2922 +       /* always, regardless the mount option */
2923 +       au_dr_hino_free(&br->br_dirren);
2924 +       au_xino_put(br);
2925 +
2926 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2927 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2928 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2929 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2930 +
2931 +       wbr = br->br_wbr;
2932 +       if (wbr) {
2933 +               for (i = 0; i < AuBrWh_Last; i++)
2934 +                       dput(wbr->wbr_wh[i]);
2935 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2936 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2937 +       }
2938 +
2939 +       if (br->br_fhsm) {
2940 +               au_br_fhsm_fin(br->br_fhsm);
2941 +               au_kfree_try_rcu(br->br_fhsm);
2942 +       }
2943 +
2944 +       key = br->br_dykey;
2945 +       for (i = 0; i < AuBrDynOp; i++, key++)
2946 +               if (*key)
2947 +                       au_dy_put(*key);
2948 +               else
2949 +                       break;
2950 +
2951 +       /* recursive lock, s_umount of branch's */
2952 +       /* synchronize_rcu(); */ /* why? */
2953 +       lockdep_off();
2954 +       path_put(&br->br_path);
2955 +       lockdep_on();
2956 +       au_kfree_rcu(wbr);
2957 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2958 +       au_lcnt_wait_for_fin(&br->br_count);
2959 +       /* I don't know why, but percpu_refcount requires this */
2960 +       /* synchronize_rcu(); */
2961 +       au_kfree_rcu(br);
2962 +}
2963 +
2964 +/*
2965 + * frees all branches
2966 + */
2967 +void au_br_free(struct au_sbinfo *sbinfo)
2968 +{
2969 +       aufs_bindex_t bmax;
2970 +       struct au_branch **br;
2971 +
2972 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2973 +
2974 +       bmax = sbinfo->si_bbot + 1;
2975 +       br = sbinfo->si_branch;
2976 +       while (bmax--)
2977 +               au_br_do_free(*br++);
2978 +}
2979 +
2980 +/*
2981 + * find the index of a branch which is specified by @br_id.
2982 + */
2983 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2984 +{
2985 +       aufs_bindex_t bindex, bbot;
2986 +
2987 +       bbot = au_sbbot(sb);
2988 +       for (bindex = 0; bindex <= bbot; bindex++)
2989 +               if (au_sbr_id(sb, bindex) == br_id)
2990 +                       return bindex;
2991 +       return -1;
2992 +}
2993 +
2994 +/* ---------------------------------------------------------------------- */
2995 +
2996 +/*
2997 + * add a branch
2998 + */
2999 +
3000 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3001 +                       struct dentry *h_root)
3002 +{
3003 +       if (unlikely(h_adding == h_root
3004 +                    || au_test_loopback_overlap(sb, h_adding)))
3005 +               return 1;
3006 +       if (h_adding->d_sb != h_root->d_sb)
3007 +               return 0;
3008 +       return au_test_subdir(h_adding, h_root)
3009 +               || au_test_subdir(h_root, h_adding);
3010 +}
3011 +
3012 +/*
3013 + * returns a newly allocated branch. @new_nbranch is a number of branches
3014 + * after adding a branch.
3015 + */
3016 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3017 +                                    int perm)
3018 +{
3019 +       struct au_branch *add_branch;
3020 +       struct dentry *root;
3021 +       struct inode *inode;
3022 +       int err;
3023 +
3024 +       err = -ENOMEM;
3025 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3026 +       if (unlikely(!add_branch))
3027 +               goto out;
3028 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3029 +       if (unlikely(!add_branch->br_xino))
3030 +               goto out_br;
3031 +       err = au_hnotify_init_br(add_branch, perm);
3032 +       if (unlikely(err))
3033 +               goto out_xino;
3034 +
3035 +       if (au_br_writable(perm)) {
3036 +               /* may be freed separately at changing the branch permission */
3037 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3038 +                                            GFP_NOFS);
3039 +               if (unlikely(!add_branch->br_wbr))
3040 +                       goto out_hnotify;
3041 +       }
3042 +
3043 +       if (au_br_fhsm(perm)) {
3044 +               err = au_fhsm_br_alloc(add_branch);
3045 +               if (unlikely(err))
3046 +                       goto out_wbr;
3047 +       }
3048 +
3049 +       root = sb->s_root;
3050 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3051 +       if (!err)
3052 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3053 +       if (!err) {
3054 +               inode = d_inode(root);
3055 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3056 +                                       /*may_shrink*/0);
3057 +       }
3058 +       if (!err)
3059 +               return add_branch; /* success */
3060 +
3061 +out_wbr:
3062 +       au_kfree_rcu(add_branch->br_wbr);
3063 +out_hnotify:
3064 +       au_hnotify_fin_br(add_branch);
3065 +out_xino:
3066 +       au_xino_put(add_branch);
3067 +out_br:
3068 +       au_kfree_rcu(add_branch);
3069 +out:
3070 +       return ERR_PTR(err);
3071 +}
3072 +
3073 +/*
3074 + * test if the branch permission is legal or not.
3075 + */
3076 +static int test_br(struct inode *inode, int brperm, char *path)
3077 +{
3078 +       int err;
3079 +
3080 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3081 +       if (!err)
3082 +               goto out;
3083 +
3084 +       err = -EINVAL;
3085 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3086 +
3087 +out:
3088 +       return err;
3089 +}
3090 +
3091 +/*
3092 + * returns:
3093 + * 0: success, the caller will add it
3094 + * plus: success, it is already unified, the caller should ignore it
3095 + * minus: error
3096 + */
3097 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3098 +{
3099 +       int err;
3100 +       aufs_bindex_t bbot, bindex;
3101 +       struct dentry *root, *h_dentry;
3102 +       struct inode *inode, *h_inode;
3103 +
3104 +       root = sb->s_root;
3105 +       bbot = au_sbbot(sb);
3106 +       if (unlikely(bbot >= 0
3107 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3108 +               err = 1;
3109 +               if (!remount) {
3110 +                       err = -EINVAL;
3111 +                       pr_err("%s duplicated\n", add->pathname);
3112 +               }
3113 +               goto out;
3114 +       }
3115 +
3116 +       err = -ENOSPC; /* -E2BIG; */
3117 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3118 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3119 +               pr_err("number of branches exceeded %s\n", add->pathname);
3120 +               goto out;
3121 +       }
3122 +
3123 +       err = -EDOM;
3124 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3125 +               pr_err("bad index %d\n", add->bindex);
3126 +               goto out;
3127 +       }
3128 +
3129 +       inode = d_inode(add->path.dentry);
3130 +       err = -ENOENT;
3131 +       if (unlikely(!inode->i_nlink)) {
3132 +               pr_err("no existence %s\n", add->pathname);
3133 +               goto out;
3134 +       }
3135 +
3136 +       err = -EINVAL;
3137 +       if (unlikely(inode->i_sb == sb)) {
3138 +               pr_err("%s must be outside\n", add->pathname);
3139 +               goto out;
3140 +       }
3141 +
3142 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3143 +               pr_err("unsupported filesystem, %s (%s)\n",
3144 +                      add->pathname, au_sbtype(inode->i_sb));
3145 +               goto out;
3146 +       }
3147 +
3148 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3149 +               pr_err("already stacked, %s (%s)\n",
3150 +                      add->pathname, au_sbtype(inode->i_sb));
3151 +               goto out;
3152 +       }
3153 +
3154 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3155 +       if (unlikely(err))
3156 +               goto out;
3157 +
3158 +       if (bbot < 0)
3159 +               return 0; /* success */
3160 +
3161 +       err = -EINVAL;
3162 +       for (bindex = 0; bindex <= bbot; bindex++)
3163 +               if (unlikely(test_overlap(sb, add->path.dentry,
3164 +                                         au_h_dptr(root, bindex)))) {
3165 +                       pr_err("%s is overlapped\n", add->pathname);
3166 +                       goto out;
3167 +               }
3168 +
3169 +       err = 0;
3170 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3171 +               h_dentry = au_h_dptr(root, 0);
3172 +               h_inode = d_inode(h_dentry);
3173 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3174 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3175 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3176 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3177 +                               add->pathname,
3178 +                               i_uid_read(inode), i_gid_read(inode),
3179 +                               (inode->i_mode & S_IALLUGO),
3180 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3181 +                               (h_inode->i_mode & S_IALLUGO));
3182 +       }
3183 +
3184 +out:
3185 +       return err;
3186 +}
3187 +
3188 +/*
3189 + * initialize or clean the whiteouts for an adding branch
3190 + */
3191 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3192 +                        int new_perm)
3193 +{
3194 +       int err, old_perm;
3195 +       aufs_bindex_t bindex;
3196 +       struct inode *h_inode;
3197 +       struct au_wbr *wbr;
3198 +       struct au_hinode *hdir;
3199 +       struct dentry *h_dentry;
3200 +
3201 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3202 +       if (unlikely(err))
3203 +               goto out;
3204 +
3205 +       wbr = br->br_wbr;
3206 +       old_perm = br->br_perm;
3207 +       br->br_perm = new_perm;
3208 +       hdir = NULL;
3209 +       h_inode = NULL;
3210 +       bindex = au_br_index(sb, br->br_id);
3211 +       if (0 <= bindex) {
3212 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3213 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3214 +       } else {
3215 +               h_dentry = au_br_dentry(br);
3216 +               h_inode = d_inode(h_dentry);
3217 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3218 +       }
3219 +       if (!wbr)
3220 +               err = au_wh_init(br, sb);
3221 +       else {
3222 +               wbr_wh_write_lock(wbr);
3223 +               err = au_wh_init(br, sb);
3224 +               wbr_wh_write_unlock(wbr);
3225 +       }
3226 +       if (hdir)
3227 +               au_hn_inode_unlock(hdir);
3228 +       else
3229 +               inode_unlock(h_inode);
3230 +       vfsub_mnt_drop_write(au_br_mnt(br));
3231 +       br->br_perm = old_perm;
3232 +
3233 +       if (!err && wbr && !au_br_writable(new_perm)) {
3234 +               au_kfree_rcu(wbr);
3235 +               br->br_wbr = NULL;
3236 +       }
3237 +
3238 +out:
3239 +       return err;
3240 +}
3241 +
3242 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3243 +                      int perm)
3244 +{
3245 +       int err;
3246 +       struct kstatfs kst;
3247 +       struct au_wbr *wbr;
3248 +
3249 +       wbr = br->br_wbr;
3250 +       au_rw_init(&wbr->wbr_wh_rwsem);
3251 +       atomic_set(&wbr->wbr_wh_running, 0);
3252 +
3253 +       /*
3254 +        * a limit for rmdir/rename a dir
3255 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3256 +        */
3257 +       err = vfs_statfs(&br->br_path, &kst);
3258 +       if (unlikely(err))
3259 +               goto out;
3260 +       err = -EINVAL;
3261 +       if (kst.f_namelen >= NAME_MAX)
3262 +               err = au_br_init_wh(sb, br, perm);
3263 +       else
3264 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3265 +                      au_br_dentry(br),
3266 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3267 +
3268 +out:
3269 +       return err;
3270 +}
3271 +
3272 +/* initialize a new branch */
3273 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3274 +                     struct au_opt_add *add)
3275 +{
3276 +       int err;
3277 +       struct au_branch *brbase;
3278 +       struct file *xf;
3279 +       struct inode *h_inode;
3280 +
3281 +       err = 0;
3282 +       br->br_perm = add->perm;
3283 +       br->br_path = add->path; /* set first, path_get() later */
3284 +       spin_lock_init(&br->br_dykey_lock);
3285 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3286 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3287 +       br->br_id = au_new_br_id(sb);
3288 +       AuDebugOn(br->br_id < 0);
3289 +
3290 +       /* always, regardless the given option */
3291 +       err = au_dr_br_init(sb, br, &add->path);
3292 +       if (unlikely(err))
3293 +               goto out_err;
3294 +
3295 +       if (au_br_writable(add->perm)) {
3296 +               err = au_wbr_init(br, sb, add->perm);
3297 +               if (unlikely(err))
3298 +                       goto out_err;
3299 +       }
3300 +
3301 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3302 +               brbase = au_sbr(sb, 0);
3303 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3304 +               AuDebugOn(!xf);
3305 +               h_inode = d_inode(add->path.dentry);
3306 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3307 +               if (unlikely(err)) {
3308 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3309 +                       goto out_err;
3310 +               }
3311 +       }
3312 +
3313 +       sysaufs_br_init(br);
3314 +       path_get(&br->br_path);
3315 +       goto out; /* success */
3316 +
3317 +out_err:
3318 +       memset(&br->br_path, 0, sizeof(br->br_path));
3319 +out:
3320 +       return err;
3321 +}
3322 +
3323 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3324 +                            struct au_branch *br, aufs_bindex_t bbot,
3325 +                            aufs_bindex_t amount)
3326 +{
3327 +       struct au_branch **brp;
3328 +
3329 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3330 +
3331 +       brp = sbinfo->si_branch + bindex;
3332 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3333 +       *brp = br;
3334 +       sbinfo->si_bbot++;
3335 +       if (unlikely(bbot < 0))
3336 +               sbinfo->si_bbot = 0;
3337 +}
3338 +
3339 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3340 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3341 +{
3342 +       struct au_hdentry *hdp;
3343 +
3344 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3345 +
3346 +       hdp = au_hdentry(dinfo, bindex);
3347 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3348 +       au_h_dentry_init(hdp);
3349 +       dinfo->di_bbot++;
3350 +       if (unlikely(bbot < 0))
3351 +               dinfo->di_btop = 0;
3352 +}
3353 +
3354 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3355 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3356 +{
3357 +       struct au_hinode *hip;
3358 +
3359 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3360 +
3361 +       hip = au_hinode(iinfo, bindex);
3362 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3363 +       au_hinode_init(hip);
3364 +       iinfo->ii_bbot++;
3365 +       if (unlikely(bbot < 0))
3366 +               iinfo->ii_btop = 0;
3367 +}
3368 +
3369 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3370 +                        aufs_bindex_t bindex)
3371 +{
3372 +       struct dentry *root, *h_dentry;
3373 +       struct inode *root_inode, *h_inode;
3374 +       aufs_bindex_t bbot, amount;
3375 +
3376 +       root = sb->s_root;
3377 +       root_inode = d_inode(root);
3378 +       bbot = au_sbbot(sb);
3379 +       amount = bbot + 1 - bindex;
3380 +       h_dentry = au_br_dentry(br);
3381 +       au_sbilist_lock();
3382 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3383 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3384 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3385 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3386 +       h_inode = d_inode(h_dentry);
3387 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3388 +       au_sbilist_unlock();
3389 +}
3390 +
3391 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3392 +{
3393 +       int err;
3394 +       aufs_bindex_t bbot, add_bindex;
3395 +       struct dentry *root, *h_dentry;
3396 +       struct inode *root_inode;
3397 +       struct au_branch *add_branch;
3398 +
3399 +       root = sb->s_root;
3400 +       root_inode = d_inode(root);
3401 +       IMustLock(root_inode);
3402 +       IiMustWriteLock(root_inode);
3403 +       err = test_add(sb, add, remount);
3404 +       if (unlikely(err < 0))
3405 +               goto out;
3406 +       if (err) {
3407 +               err = 0;
3408 +               goto out; /* success */
3409 +       }
3410 +
3411 +       bbot = au_sbbot(sb);
3412 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3413 +       err = PTR_ERR(add_branch);
3414 +       if (IS_ERR(add_branch))
3415 +               goto out;
3416 +
3417 +       err = au_br_init(add_branch, sb, add);
3418 +       if (unlikely(err)) {
3419 +               au_br_do_free(add_branch);
3420 +               goto out;
3421 +       }
3422 +
3423 +       add_bindex = add->bindex;
3424 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3425 +       au_br_do_add(sb, add_branch, add_bindex);
3426 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3427 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3428 +
3429 +       h_dentry = add->path.dentry;
3430 +       if (!add_bindex) {
3431 +               au_cpup_attr_all(root_inode, /*force*/1);
3432 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3433 +       } else
3434 +               au_add_nlink(root_inode, d_inode(h_dentry));
3435 +
3436 +out:
3437 +       return err;
3438 +}
3439 +
3440 +/* ---------------------------------------------------------------------- */
3441 +
3442 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3443 +                                      unsigned long long max __maybe_unused,
3444 +                                      void *arg)
3445 +{
3446 +       unsigned long long n;
3447 +       struct file **p, *f;
3448 +       struct hlist_bl_head *files;
3449 +       struct hlist_bl_node *pos;
3450 +       struct au_finfo *finfo;
3451 +
3452 +       n = 0;
3453 +       p = a;
3454 +       files = &au_sbi(sb)->si_files;
3455 +       hlist_bl_lock(files);
3456 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3457 +               f = finfo->fi_file;
3458 +               if (file_count(f)
3459 +                   && !special_file(file_inode(f)->i_mode)) {
3460 +                       get_file(f);
3461 +                       *p++ = f;
3462 +                       n++;
3463 +                       AuDebugOn(n > max);
3464 +               }
3465 +       }
3466 +       hlist_bl_unlock(files);
3467 +
3468 +       return n;
3469 +}
3470 +
3471 +static struct file **au_farray_alloc(struct super_block *sb,
3472 +                                    unsigned long long *max)
3473 +{
3474 +       struct au_sbinfo *sbi;
3475 +
3476 +       sbi = au_sbi(sb);
3477 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3478 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3479 +}
3480 +
3481 +static void au_farray_free(struct file **a, unsigned long long max)
3482 +{
3483 +       unsigned long long ull;
3484 +
3485 +       for (ull = 0; ull < max; ull++)
3486 +               if (a[ull])
3487 +                       fput(a[ull]);
3488 +       kvfree(a);
3489 +}
3490 +
3491 +/* ---------------------------------------------------------------------- */
3492 +
3493 +/*
3494 + * delete a branch
3495 + */
3496 +
3497 +/* to show the line number, do not make it inlined function */
3498 +#define AuVerbose(do_info, fmt, ...) do { \
3499 +       if (do_info) \
3500 +               pr_info(fmt, ##__VA_ARGS__); \
3501 +} while (0)
3502 +
3503 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3504 +                        aufs_bindex_t bbot)
3505 +{
3506 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3507 +}
3508 +
3509 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3510 +                        aufs_bindex_t bbot)
3511 +{
3512 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3513 +}
3514 +
3515 +/*
3516 + * test if the branch is deletable or not.
3517 + */
3518 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3519 +                           unsigned int sigen, const unsigned int verbose)
3520 +{
3521 +       int err, i, j, ndentry;
3522 +       aufs_bindex_t btop, bbot;
3523 +       struct au_dcsub_pages dpages;
3524 +       struct au_dpage *dpage;
3525 +       struct dentry *d;
3526 +
3527 +       err = au_dpages_init(&dpages, GFP_NOFS);
3528 +       if (unlikely(err))
3529 +               goto out;
3530 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3531 +       if (unlikely(err))
3532 +               goto out_dpages;
3533 +
3534 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3535 +               dpage = dpages.dpages + i;
3536 +               ndentry = dpage->ndentry;
3537 +               for (j = 0; !err && j < ndentry; j++) {
3538 +                       d = dpage->dentries[j];
3539 +                       AuDebugOn(au_dcount(d) <= 0);
3540 +                       if (!au_digen_test(d, sigen)) {
3541 +                               di_read_lock_child(d, AuLock_IR);
3542 +                               if (unlikely(au_dbrange_test(d))) {
3543 +                                       di_read_unlock(d, AuLock_IR);
3544 +                                       continue;
3545 +                               }
3546 +                       } else {
3547 +                               di_write_lock_child(d);
3548 +                               if (unlikely(au_dbrange_test(d))) {
3549 +                                       di_write_unlock(d);
3550 +                                       continue;
3551 +                               }
3552 +                               err = au_reval_dpath(d, sigen);
3553 +                               if (!err)
3554 +                                       di_downgrade_lock(d, AuLock_IR);
3555 +                               else {
3556 +                                       di_write_unlock(d);
3557 +                                       break;
3558 +                               }
3559 +                       }
3560 +
3561 +                       /* AuDbgDentry(d); */
3562 +                       btop = au_dbtop(d);
3563 +                       bbot = au_dbbot(d);
3564 +                       if (btop <= bindex
3565 +                           && bindex <= bbot
3566 +                           && au_h_dptr(d, bindex)
3567 +                           && au_test_dbusy(d, btop, bbot)) {
3568 +                               err = -EBUSY;
3569 +                               AuVerbose(verbose, "busy %pd\n", d);
3570 +                               AuDbgDentry(d);
3571 +                       }
3572 +                       di_read_unlock(d, AuLock_IR);
3573 +               }
3574 +       }
3575 +
3576 +out_dpages:
3577 +       au_dpages_free(&dpages);
3578 +out:
3579 +       return err;
3580 +}
3581 +
3582 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3583 +                          unsigned int sigen, const unsigned int verbose)
3584 +{
3585 +       int err;
3586 +       unsigned long long max, ull;
3587 +       struct inode *i, **array;
3588 +       aufs_bindex_t btop, bbot;
3589 +
3590 +       array = au_iarray_alloc(sb, &max);
3591 +       err = PTR_ERR(array);
3592 +       if (IS_ERR(array))
3593 +               goto out;
3594 +
3595 +       err = 0;
3596 +       AuDbg("b%d\n", bindex);
3597 +       for (ull = 0; !err && ull < max; ull++) {
3598 +               i = array[ull];
3599 +               if (unlikely(!i))
3600 +                       break;
3601 +               if (i->i_ino == AUFS_ROOT_INO)
3602 +                       continue;
3603 +
3604 +               /* AuDbgInode(i); */
3605 +               if (au_iigen(i, NULL) == sigen)
3606 +                       ii_read_lock_child(i);
3607 +               else {
3608 +                       ii_write_lock_child(i);
3609 +                       err = au_refresh_hinode_self(i);
3610 +                       au_iigen_dec(i);
3611 +                       if (!err)
3612 +                               ii_downgrade_lock(i);
3613 +                       else {
3614 +                               ii_write_unlock(i);
3615 +                               break;
3616 +                       }
3617 +               }
3618 +
3619 +               btop = au_ibtop(i);
3620 +               bbot = au_ibbot(i);
3621 +               if (btop <= bindex
3622 +                   && bindex <= bbot
3623 +                   && au_h_iptr(i, bindex)
3624 +                   && au_test_ibusy(i, btop, bbot)) {
3625 +                       err = -EBUSY;
3626 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3627 +                       AuDbgInode(i);
3628 +               }
3629 +               ii_read_unlock(i);
3630 +       }
3631 +       au_iarray_free(array, max);
3632 +
3633 +out:
3634 +       return err;
3635 +}
3636 +
3637 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3638 +                             const unsigned int verbose)
3639 +{
3640 +       int err;
3641 +       unsigned int sigen;
3642 +
3643 +       sigen = au_sigen(root->d_sb);
3644 +       DiMustNoWaiters(root);
3645 +       IiMustNoWaiters(d_inode(root));
3646 +       di_write_unlock(root);
3647 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3648 +       if (!err)
3649 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3650 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3651 +
3652 +       return err;
3653 +}
3654 +
3655 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3656 +                        struct file **to_free, int *idx)
3657 +{
3658 +       int err;
3659 +       unsigned char matched, root;
3660 +       aufs_bindex_t bindex, bbot;
3661 +       struct au_fidir *fidir;
3662 +       struct au_hfile *hfile;
3663 +
3664 +       err = 0;
3665 +       root = IS_ROOT(file->f_path.dentry);
3666 +       if (root) {
3667 +               get_file(file);
3668 +               to_free[*idx] = file;
3669 +               (*idx)++;
3670 +               goto out;
3671 +       }
3672 +
3673 +       matched = 0;
3674 +       fidir = au_fi(file)->fi_hdir;
3675 +       AuDebugOn(!fidir);
3676 +       bbot = au_fbbot_dir(file);
3677 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3678 +               hfile = fidir->fd_hfile + bindex;
3679 +               if (!hfile->hf_file)
3680 +                       continue;
3681 +
3682 +               if (hfile->hf_br->br_id == br_id) {
3683 +                       matched = 1;
3684 +                       break;
3685 +               }
3686 +       }
3687 +       if (matched)
3688 +               err = -EBUSY;
3689 +
3690 +out:
3691 +       return err;
3692 +}
3693 +
3694 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3695 +                         struct file **to_free, int opened)
3696 +{
3697 +       int err, idx;
3698 +       unsigned long long ull, max;
3699 +       aufs_bindex_t btop;
3700 +       struct file *file, **array;
3701 +       struct dentry *root;
3702 +       struct au_hfile *hfile;
3703 +
3704 +       array = au_farray_alloc(sb, &max);
3705 +       err = PTR_ERR(array);
3706 +       if (IS_ERR(array))
3707 +               goto out;
3708 +
3709 +       err = 0;
3710 +       idx = 0;
3711 +       root = sb->s_root;
3712 +       di_write_unlock(root);
3713 +       for (ull = 0; ull < max; ull++) {
3714 +               file = array[ull];
3715 +               if (unlikely(!file))
3716 +                       break;
3717 +
3718 +               /* AuDbg("%pD\n", file); */
3719 +               fi_read_lock(file);
3720 +               btop = au_fbtop(file);
3721 +               if (!d_is_dir(file->f_path.dentry)) {
3722 +                       hfile = &au_fi(file)->fi_htop;
3723 +                       if (hfile->hf_br->br_id == br_id)
3724 +                               err = -EBUSY;
3725 +               } else
3726 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3727 +               fi_read_unlock(file);
3728 +               if (unlikely(err))
3729 +                       break;
3730 +       }
3731 +       di_write_lock_child(root);
3732 +       au_farray_free(array, max);
3733 +       AuDebugOn(idx > opened);
3734 +
3735 +out:
3736 +       return err;
3737 +}
3738 +
3739 +static void br_del_file(struct file **to_free, unsigned long long opened,
3740 +                       aufs_bindex_t br_id)
3741 +{
3742 +       unsigned long long ull;
3743 +       aufs_bindex_t bindex, btop, bbot, bfound;
3744 +       struct file *file;
3745 +       struct au_fidir *fidir;
3746 +       struct au_hfile *hfile;
3747 +
3748 +       for (ull = 0; ull < opened; ull++) {
3749 +               file = to_free[ull];
3750 +               if (unlikely(!file))
3751 +                       break;
3752 +
3753 +               /* AuDbg("%pD\n", file); */
3754 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3755 +               bfound = -1;
3756 +               fidir = au_fi(file)->fi_hdir;
3757 +               AuDebugOn(!fidir);
3758 +               fi_write_lock(file);
3759 +               btop = au_fbtop(file);
3760 +               bbot = au_fbbot_dir(file);
3761 +               for (bindex = btop; bindex <= bbot; bindex++) {
3762 +                       hfile = fidir->fd_hfile + bindex;
3763 +                       if (!hfile->hf_file)
3764 +                               continue;
3765 +
3766 +                       if (hfile->hf_br->br_id == br_id) {
3767 +                               bfound = bindex;
3768 +                               break;
3769 +                       }
3770 +               }
3771 +               AuDebugOn(bfound < 0);
3772 +               au_set_h_fptr(file, bfound, NULL);
3773 +               if (bfound == btop) {
3774 +                       for (btop++; btop <= bbot; btop++)
3775 +                               if (au_hf_dir(file, btop)) {
3776 +                                       au_set_fbtop(file, btop);
3777 +                                       break;
3778 +                               }
3779 +               }
3780 +               fi_write_unlock(file);
3781 +       }
3782 +}
3783 +
3784 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3785 +                            const aufs_bindex_t bindex,
3786 +                            const aufs_bindex_t bbot)
3787 +{
3788 +       struct au_branch **brp, **p;
3789 +
3790 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3791 +
3792 +       brp = sbinfo->si_branch + bindex;
3793 +       if (bindex < bbot)
3794 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3795 +       sbinfo->si_branch[0 + bbot] = NULL;
3796 +       sbinfo->si_bbot--;
3797 +
3798 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3799 +                       /*may_shrink*/1);
3800 +       if (p)
3801 +               sbinfo->si_branch = p;
3802 +       /* harmless error */
3803 +}
3804 +
3805 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3806 +                            const aufs_bindex_t bbot)
3807 +{
3808 +       struct au_hdentry *hdp, *p;
3809 +
3810 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3811 +
3812 +       hdp = au_hdentry(dinfo, bindex);
3813 +       if (bindex < bbot)
3814 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3815 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3816 +       dinfo->di_bbot--;
3817 +
3818 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3819 +                       /*may_shrink*/1);
3820 +       if (p)
3821 +               dinfo->di_hdentry = p;
3822 +       /* harmless error */
3823 +}
3824 +
3825 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3826 +                            const aufs_bindex_t bbot)
3827 +{
3828 +       struct au_hinode *hip, *p;
3829 +
3830 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3831 +
3832 +       hip = au_hinode(iinfo, bindex);
3833 +       if (bindex < bbot)
3834 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3835 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3836 +       iinfo->ii_bbot--;
3837 +
3838 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3839 +                       /*may_shrink*/1);
3840 +       if (p)
3841 +               iinfo->ii_hinode = p;
3842 +       /* harmless error */
3843 +}
3844 +
3845 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3846 +                        struct au_branch *br)
3847 +{
3848 +       aufs_bindex_t bbot;
3849 +       struct au_sbinfo *sbinfo;
3850 +       struct dentry *root, *h_root;
3851 +       struct inode *inode, *h_inode;
3852 +       struct au_hinode *hinode;
3853 +
3854 +       SiMustWriteLock(sb);
3855 +
3856 +       root = sb->s_root;
3857 +       inode = d_inode(root);
3858 +       sbinfo = au_sbi(sb);
3859 +       bbot = sbinfo->si_bbot;
3860 +
3861 +       h_root = au_h_dptr(root, bindex);
3862 +       hinode = au_hi(inode, bindex);
3863 +       h_inode = au_igrab(hinode->hi_inode);
3864 +       au_hiput(hinode);
3865 +
3866 +       au_sbilist_lock();
3867 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3868 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3869 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3870 +       au_sbilist_unlock();
3871 +
3872 +       /* ignore an error */
3873 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3874 +
3875 +       dput(h_root);
3876 +       iput(h_inode);
3877 +       au_br_do_free(br);
3878 +}
3879 +
3880 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3881 +                                  unsigned long long max, void *arg)
3882 +{
3883 +       return max;
3884 +}
3885 +
3886 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3887 +{
3888 +       int err, rerr, i;
3889 +       unsigned long long opened;
3890 +       unsigned int mnt_flags;
3891 +       aufs_bindex_t bindex, bbot, br_id;
3892 +       unsigned char do_wh, verbose;
3893 +       struct au_branch *br;
3894 +       struct au_wbr *wbr;
3895 +       struct dentry *root;
3896 +       struct file **to_free;
3897 +
3898 +       err = 0;
3899 +       opened = 0;
3900 +       to_free = NULL;
3901 +       root = sb->s_root;
3902 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3903 +       if (bindex < 0) {
3904 +               if (remount)
3905 +                       goto out; /* success */
3906 +               err = -ENOENT;
3907 +               pr_err("%s no such branch\n", del->pathname);
3908 +               goto out;
3909 +       }
3910 +       AuDbg("bindex b%d\n", bindex);
3911 +
3912 +       err = -EBUSY;
3913 +       mnt_flags = au_mntflags(sb);
3914 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3915 +       bbot = au_sbbot(sb);
3916 +       if (unlikely(!bbot)) {
3917 +               AuVerbose(verbose, "no more branches left\n");
3918 +               goto out;
3919 +       }
3920 +
3921 +       br = au_sbr(sb, bindex);
3922 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3923 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3924 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3925 +               goto out;
3926 +       }
3927 +
3928 +       br_id = br->br_id;
3929 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3930 +       if (unlikely(opened)) {
3931 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3932 +               err = PTR_ERR(to_free);
3933 +               if (IS_ERR(to_free))
3934 +                       goto out;
3935 +
3936 +               err = test_file_busy(sb, br_id, to_free, opened);
3937 +               if (unlikely(err)) {
3938 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3939 +                       goto out;
3940 +               }
3941 +       }
3942 +
3943 +       wbr = br->br_wbr;
3944 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3945 +       if (do_wh) {
3946 +               /* instead of WbrWhMustWriteLock(wbr) */
3947 +               SiMustWriteLock(sb);
3948 +               for (i = 0; i < AuBrWh_Last; i++) {
3949 +                       dput(wbr->wbr_wh[i]);
3950 +                       wbr->wbr_wh[i] = NULL;
3951 +               }
3952 +       }
3953 +
3954 +       err = test_children_busy(root, bindex, verbose);
3955 +       if (unlikely(err)) {
3956 +               if (do_wh)
3957 +                       goto out_wh;
3958 +               goto out;
3959 +       }
3960 +
3961 +       err = 0;
3962 +       if (to_free) {
3963 +               /*
3964 +                * now we confirmed the branch is deletable.
3965 +                * let's free the remaining opened dirs on the branch.
3966 +                */
3967 +               di_write_unlock(root);
3968 +               br_del_file(to_free, opened, br_id);
3969 +               di_write_lock_child(root);
3970 +       }
3971 +
3972 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3973 +       dbgaufs_xino_del(br);           /* remove one */
3974 +       au_br_do_del(sb, bindex, br);
3975 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3976 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3977 +
3978 +       if (!bindex) {
3979 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3980 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3981 +       } else
3982 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3983 +       if (au_opt_test(mnt_flags, PLINK))
3984 +               au_plink_half_refresh(sb, br_id);
3985 +
3986 +       goto out; /* success */
3987 +
3988 +out_wh:
3989 +       /* revert */
3990 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3991 +       if (rerr)
3992 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3993 +                       del->pathname, rerr);
3994 +out:
3995 +       if (to_free)
3996 +               au_farray_free(to_free, opened);
3997 +       return err;
3998 +}
3999 +
4000 +/* ---------------------------------------------------------------------- */
4001 +
4002 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4003 +{
4004 +       int err;
4005 +       aufs_bindex_t btop, bbot;
4006 +       struct aufs_ibusy ibusy;
4007 +       struct inode *inode, *h_inode;
4008 +
4009 +       err = -EPERM;
4010 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4011 +               goto out;
4012 +
4013 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4014 +       if (!err)
4015 +               /* VERIFY_WRITE */
4016 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4017 +       if (unlikely(err)) {
4018 +               err = -EFAULT;
4019 +               AuTraceErr(err);
4020 +               goto out;
4021 +       }
4022 +
4023 +       err = -EINVAL;
4024 +       si_read_lock(sb, AuLock_FLUSH);
4025 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4026 +               goto out_unlock;
4027 +
4028 +       err = 0;
4029 +       ibusy.h_ino = 0; /* invalid */
4030 +       inode = ilookup(sb, ibusy.ino);
4031 +       if (!inode
4032 +           || inode->i_ino == AUFS_ROOT_INO
4033 +           || au_is_bad_inode(inode))
4034 +               goto out_unlock;
4035 +
4036 +       ii_read_lock_child(inode);
4037 +       btop = au_ibtop(inode);
4038 +       bbot = au_ibbot(inode);
4039 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4040 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4041 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4042 +                       ibusy.h_ino = h_inode->i_ino;
4043 +       }
4044 +       ii_read_unlock(inode);
4045 +       iput(inode);
4046 +
4047 +out_unlock:
4048 +       si_read_unlock(sb);
4049 +       if (!err) {
4050 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4051 +               if (unlikely(err)) {
4052 +                       err = -EFAULT;
4053 +                       AuTraceErr(err);
4054 +               }
4055 +       }
4056 +out:
4057 +       return err;
4058 +}
4059 +
4060 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4061 +{
4062 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4063 +}
4064 +
4065 +#ifdef CONFIG_COMPAT
4066 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4067 +{
4068 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4069 +}
4070 +#endif
4071 +
4072 +/* ---------------------------------------------------------------------- */
4073 +
4074 +/*
4075 + * change a branch permission
4076 + */
4077 +
4078 +static void au_warn_ima(void)
4079 +{
4080 +#ifdef CONFIG_IMA
4081 +       /* since it doesn't support mark_files_ro() */
4082 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4083 +#endif
4084 +}
4085 +
4086 +static int do_need_sigen_inc(int a, int b)
4087 +{
4088 +       return au_br_whable(a) && !au_br_whable(b);
4089 +}
4090 +
4091 +static int need_sigen_inc(int old, int new)
4092 +{
4093 +       return do_need_sigen_inc(old, new)
4094 +               || do_need_sigen_inc(new, old);
4095 +}
4096 +
4097 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4098 +{
4099 +       int err, do_warn;
4100 +       unsigned int mnt_flags;
4101 +       unsigned long long ull, max;
4102 +       aufs_bindex_t br_id;
4103 +       unsigned char verbose, writer;
4104 +       struct file *file, *hf, **array;
4105 +       struct au_hfile *hfile;
4106 +       struct inode *h_inode;
4107 +
4108 +       mnt_flags = au_mntflags(sb);
4109 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4110 +
4111 +       array = au_farray_alloc(sb, &max);
4112 +       err = PTR_ERR(array);
4113 +       if (IS_ERR(array))
4114 +               goto out;
4115 +
4116 +       do_warn = 0;
4117 +       br_id = au_sbr_id(sb, bindex);
4118 +       for (ull = 0; ull < max; ull++) {
4119 +               file = array[ull];
4120 +               if (unlikely(!file))
4121 +                       break;
4122 +
4123 +               /* AuDbg("%pD\n", file); */
4124 +               fi_read_lock(file);
4125 +               if (unlikely(au_test_mmapped(file))) {
4126 +                       err = -EBUSY;
4127 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4128 +                       AuDbgFile(file);
4129 +                       FiMustNoWaiters(file);
4130 +                       fi_read_unlock(file);
4131 +                       goto out_array;
4132 +               }
4133 +
4134 +               hfile = &au_fi(file)->fi_htop;
4135 +               hf = hfile->hf_file;
4136 +               if (!d_is_reg(file->f_path.dentry)
4137 +                   || !(file->f_mode & FMODE_WRITE)
4138 +                   || hfile->hf_br->br_id != br_id
4139 +                   || !(hf->f_mode & FMODE_WRITE))
4140 +                       array[ull] = NULL;
4141 +               else {
4142 +                       do_warn = 1;
4143 +                       get_file(file);
4144 +               }
4145 +
4146 +               FiMustNoWaiters(file);
4147 +               fi_read_unlock(file);
4148 +               fput(file);
4149 +       }
4150 +
4151 +       err = 0;
4152 +       if (do_warn)
4153 +               au_warn_ima();
4154 +
4155 +       for (ull = 0; ull < max; ull++) {
4156 +               file = array[ull];
4157 +               if (!file)
4158 +                       continue;
4159 +
4160 +               /* todo: already flushed? */
4161 +               /*
4162 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4163 +                * approach which resets f_mode and calls mnt_drop_write() and
4164 +                * file_release_write() for each file, because the branch
4165 +                * attribute in aufs world is totally different from the native
4166 +                * fs rw/ro mode.
4167 +               */
4168 +               /* fi_read_lock(file); */
4169 +               hfile = &au_fi(file)->fi_htop;
4170 +               hf = hfile->hf_file;
4171 +               /* fi_read_unlock(file); */
4172 +               spin_lock(&hf->f_lock);
4173 +               writer = !!(hf->f_mode & FMODE_WRITER);
4174 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4175 +               spin_unlock(&hf->f_lock);
4176 +               if (writer) {
4177 +                       h_inode = file_inode(hf);
4178 +                       if (hf->f_mode & FMODE_READ)
4179 +                               i_readcount_inc(h_inode);
4180 +                       put_write_access(h_inode);
4181 +                       __mnt_drop_write(hf->f_path.mnt);
4182 +               }
4183 +       }
4184 +
4185 +out_array:
4186 +       au_farray_free(array, max);
4187 +out:
4188 +       AuTraceErr(err);
4189 +       return err;
4190 +}
4191 +
4192 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4193 +             int *do_refresh)
4194 +{
4195 +       int err, rerr;
4196 +       aufs_bindex_t bindex;
4197 +       struct dentry *root;
4198 +       struct au_branch *br;
4199 +       struct au_br_fhsm *bf;
4200 +
4201 +       root = sb->s_root;
4202 +       bindex = au_find_dbindex(root, mod->h_root);
4203 +       if (bindex < 0) {
4204 +               if (remount)
4205 +                       return 0; /* success */
4206 +               err = -ENOENT;
4207 +               pr_err("%s no such branch\n", mod->path);
4208 +               goto out;
4209 +       }
4210 +       AuDbg("bindex b%d\n", bindex);
4211 +
4212 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4213 +       if (unlikely(err))
4214 +               goto out;
4215 +
4216 +       br = au_sbr(sb, bindex);
4217 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4218 +       if (br->br_perm == mod->perm)
4219 +               return 0; /* success */
4220 +
4221 +       /* pre-allocate for non-fhsm --> fhsm */
4222 +       bf = NULL;
4223 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4224 +               err = au_fhsm_br_alloc(br);
4225 +               if (unlikely(err))
4226 +                       goto out;
4227 +               bf = br->br_fhsm;
4228 +               br->br_fhsm = NULL;
4229 +       }
4230 +
4231 +       if (au_br_writable(br->br_perm)) {
4232 +               /* remove whiteout base */
4233 +               err = au_br_init_wh(sb, br, mod->perm);
4234 +               if (unlikely(err))
4235 +                       goto out_bf;
4236 +
4237 +               if (!au_br_writable(mod->perm)) {
4238 +                       /* rw --> ro, file might be mmapped */
4239 +                       DiMustNoWaiters(root);
4240 +                       IiMustNoWaiters(d_inode(root));
4241 +                       di_write_unlock(root);
4242 +                       err = au_br_mod_files_ro(sb, bindex);
4243 +                       /* aufs_write_lock() calls ..._child() */
4244 +                       di_write_lock_child(root);
4245 +
4246 +                       if (unlikely(err)) {
4247 +                               rerr = -ENOMEM;
4248 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4249 +                                                    GFP_NOFS);
4250 +                               if (br->br_wbr)
4251 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4252 +                               if (unlikely(rerr)) {
4253 +                                       AuIOErr("nested error %d (%d)\n",
4254 +                                               rerr, err);
4255 +                                       br->br_perm = mod->perm;
4256 +                               }
4257 +                       }
4258 +               }
4259 +       } else if (au_br_writable(mod->perm)) {
4260 +               /* ro --> rw */
4261 +               err = -ENOMEM;
4262 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4263 +               if (br->br_wbr) {
4264 +                       err = au_wbr_init(br, sb, mod->perm);
4265 +                       if (unlikely(err)) {
4266 +                               au_kfree_rcu(br->br_wbr);
4267 +                               br->br_wbr = NULL;
4268 +                       }
4269 +               }
4270 +       }
4271 +       if (unlikely(err))
4272 +               goto out_bf;
4273 +
4274 +       if (au_br_fhsm(br->br_perm)) {
4275 +               if (!au_br_fhsm(mod->perm)) {
4276 +                       /* fhsm --> non-fhsm */
4277 +                       au_br_fhsm_fin(br->br_fhsm);
4278 +                       au_kfree_rcu(br->br_fhsm);
4279 +                       br->br_fhsm = NULL;
4280 +               }
4281 +       } else if (au_br_fhsm(mod->perm))
4282 +               /* non-fhsm --> fhsm */
4283 +               br->br_fhsm = bf;
4284 +
4285 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4286 +       br->br_perm = mod->perm;
4287 +       goto out; /* success */
4288 +
4289 +out_bf:
4290 +       au_kfree_try_rcu(bf);
4291 +out:
4292 +       AuTraceErr(err);
4293 +       return err;
4294 +}
4295 +
4296 +/* ---------------------------------------------------------------------- */
4297 +
4298 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4299 +{
4300 +       int err;
4301 +       struct kstatfs kstfs;
4302 +
4303 +       err = vfs_statfs(&br->br_path, &kstfs);
4304 +       if (!err) {
4305 +               stfs->f_blocks = kstfs.f_blocks;
4306 +               stfs->f_bavail = kstfs.f_bavail;
4307 +               stfs->f_files = kstfs.f_files;
4308 +               stfs->f_ffree = kstfs.f_ffree;
4309 +       }
4310 +
4311 +       return err;
4312 +}
4313 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4314 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4315 +++ linux/fs/aufs/branch.h      2021-12-03 15:40:58.233313963 +0100
4316 @@ -0,0 +1,375 @@
4317 +/* SPDX-License-Identifier: GPL-2.0 */
4318 +/*
4319 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4320 + *
4321 + * This program, aufs is free software; you can redistribute it and/or modify
4322 + * it under the terms of the GNU General Public License as published by
4323 + * the Free Software Foundation; either version 2 of the License, or
4324 + * (at your option) any later version.
4325 + *
4326 + * This program is distributed in the hope that it will be useful,
4327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4329 + * GNU General Public License for more details.
4330 + *
4331 + * You should have received a copy of the GNU General Public License
4332 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4333 + */
4334 +
4335 +/*
4336 + * branch filesystems and xino for them
4337 + */
4338 +
4339 +#ifndef __AUFS_BRANCH_H__
4340 +#define __AUFS_BRANCH_H__
4341 +
4342 +#ifdef __KERNEL__
4343 +
4344 +#include <linux/mount.h>
4345 +#include "dirren.h"
4346 +#include "dynop.h"
4347 +#include "lcnt.h"
4348 +#include "rwsem.h"
4349 +#include "super.h"
4350 +
4351 +/* ---------------------------------------------------------------------- */
4352 +
4353 +/* a xino file */
4354 +struct au_xino {
4355 +       struct file             **xi_file;
4356 +       unsigned int            xi_nfile;
4357 +
4358 +       struct {
4359 +               spinlock_t              spin;
4360 +               ino_t                   *array;
4361 +               int                     total;
4362 +               /* reserved for future use */
4363 +               /* unsigned long        *bitmap; */
4364 +               wait_queue_head_t       wqh;
4365 +       } xi_nondir;
4366 +
4367 +       struct mutex            xi_mtx; /* protects xi_file array */
4368 +       struct hlist_bl_head    xi_writing;
4369 +
4370 +       atomic_t                xi_truncating;
4371 +
4372 +       struct kref             xi_kref;
4373 +};
4374 +
4375 +/* File-based Hierarchical Storage Management */
4376 +struct au_br_fhsm {
4377 +#ifdef CONFIG_AUFS_FHSM
4378 +       struct mutex            bf_lock;
4379 +       unsigned long           bf_jiffy;
4380 +       struct aufs_stfs        bf_stfs;
4381 +       int                     bf_readable;
4382 +#endif
4383 +};
4384 +
4385 +/* members for writable branch only */
4386 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4387 +struct au_wbr {
4388 +       struct au_rwsem         wbr_wh_rwsem;
4389 +       struct dentry           *wbr_wh[AuBrWh_Last];
4390 +       atomic_t                wbr_wh_running;
4391 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4392 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4393 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4394 +
4395 +       /* mfs mode */
4396 +       unsigned long long      wbr_bytes;
4397 +};
4398 +
4399 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4400 +#define AuBrDynOp (AuDyLast * 4)
4401 +
4402 +#ifdef CONFIG_AUFS_HFSNOTIFY
4403 +/* support for asynchronous destruction */
4404 +struct au_br_hfsnotify {
4405 +       struct fsnotify_group   *hfsn_group;
4406 +};
4407 +#endif
4408 +
4409 +/* sysfs entries */
4410 +struct au_brsysfs {
4411 +       char                    name[16];
4412 +       struct attribute        attr;
4413 +};
4414 +
4415 +enum {
4416 +       AuBrSysfs_BR,
4417 +       AuBrSysfs_BRID,
4418 +       AuBrSysfs_Last
4419 +};
4420 +
4421 +/* protected by superblock rwsem */
4422 +struct au_branch {
4423 +       struct au_xino          *br_xino;
4424 +
4425 +       aufs_bindex_t           br_id;
4426 +
4427 +       int                     br_perm;
4428 +       struct path             br_path;
4429 +       spinlock_t              br_dykey_lock;
4430 +       struct au_dykey         *br_dykey[AuBrDynOp];
4431 +       au_lcnt_t               br_nfiles;      /* opened files */
4432 +       au_lcnt_t               br_count;       /* in-use for other */
4433 +
4434 +       struct au_wbr           *br_wbr;
4435 +       struct au_br_fhsm       *br_fhsm;
4436 +
4437 +#ifdef CONFIG_AUFS_HFSNOTIFY
4438 +       struct au_br_hfsnotify  *br_hfsn;
4439 +#endif
4440 +
4441 +#ifdef CONFIG_SYSFS
4442 +       /* entries under sysfs per mount-point */
4443 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4444 +#endif
4445 +
4446 +#ifdef CONFIG_DEBUG_FS
4447 +       struct dentry            *br_dbgaufs; /* xino */
4448 +#endif
4449 +
4450 +       struct au_dr_br         br_dirren;
4451 +};
4452 +
4453 +/* ---------------------------------------------------------------------- */
4454 +
4455 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4456 +{
4457 +       return br->br_path.mnt;
4458 +}
4459 +
4460 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4461 +{
4462 +       return br->br_path.dentry;
4463 +}
4464 +
4465 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4466 +{
4467 +       return mnt_user_ns(br->br_path.mnt);
4468 +}
4469 +
4470 +static inline struct super_block *au_br_sb(struct au_branch *br)
4471 +{
4472 +       return au_br_mnt(br)->mnt_sb;
4473 +}
4474 +
4475 +static inline int au_br_rdonly(struct au_branch *br)
4476 +{
4477 +       return (sb_rdonly(au_br_sb(br))
4478 +               || !au_br_writable(br->br_perm))
4479 +               ? -EROFS : 0;
4480 +}
4481 +
4482 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4483 +{
4484 +#ifdef CONFIG_AUFS_HNOTIFY
4485 +       return !(brperm & AuBrPerm_RR);
4486 +#else
4487 +       return 0;
4488 +#endif
4489 +}
4490 +
4491 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4492 +{
4493 +       int err, exec_flag;
4494 +
4495 +       err = 0;
4496 +       exec_flag = oflag & __FMODE_EXEC;
4497 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4498 +               err = -EACCES;
4499 +
4500 +       return err;
4501 +}
4502 +
4503 +static inline void au_xino_get(struct au_branch *br)
4504 +{
4505 +       struct au_xino *xi;
4506 +
4507 +       xi = br->br_xino;
4508 +       if (xi)
4509 +               kref_get(&xi->xi_kref);
4510 +}
4511 +
4512 +static inline int au_xino_count(struct au_branch *br)
4513 +{
4514 +       int v;
4515 +       struct au_xino *xi;
4516 +
4517 +       v = 0;
4518 +       xi = br->br_xino;
4519 +       if (xi)
4520 +               v = kref_read(&xi->xi_kref);
4521 +
4522 +       return v;
4523 +}
4524 +
4525 +/* ---------------------------------------------------------------------- */
4526 +
4527 +/* branch.c */
4528 +struct au_sbinfo;
4529 +void au_br_free(struct au_sbinfo *sinfo);
4530 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4531 +struct au_opt_add;
4532 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4533 +struct au_opt_del;
4534 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4535 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4536 +#ifdef CONFIG_COMPAT
4537 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4538 +#endif
4539 +struct au_opt_mod;
4540 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4541 +             int *do_refresh);
4542 +struct aufs_stfs;
4543 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4544 +
4545 +/* xino.c */
4546 +static const loff_t au_loff_max = LLONG_MAX;
4547 +
4548 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4549 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4550 +                           int wbrtop);
4551 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4552 +                            struct file *copy_src);
4553 +struct au_xi_new {
4554 +       struct au_xino *xi;     /* switch between xino and xigen */
4555 +       int idx;
4556 +       struct path *base;
4557 +       struct file *copy_src;
4558 +};
4559 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4560 +
4561 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4562 +                ino_t *ino);
4563 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4564 +                 ino_t ino);
4565 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4566 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4567 +
4568 +int au_xib_trunc(struct super_block *sb);
4569 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4570 +
4571 +struct au_xino *au_xino_alloc(unsigned int nfile);
4572 +int au_xino_put(struct au_branch *br);
4573 +struct file *au_xino_file1(struct au_xino *xi);
4574 +
4575 +struct au_opt_xino;
4576 +void au_xino_clr(struct super_block *sb);
4577 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4578 +struct file *au_xino_def(struct super_block *sb);
4579 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4580 +                   struct path *base);
4581 +
4582 +ino_t au_xino_new_ino(struct super_block *sb);
4583 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4584 +
4585 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4586 +                      ino_t h_ino, int idx);
4587 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4588 +                     int *idx);
4589 +
4590 +int au_xino_path(struct seq_file *seq, struct file *file);
4591 +
4592 +/* ---------------------------------------------------------------------- */
4593 +
4594 +/* @idx is signed to accept -1 meaning the first file */
4595 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4596 +{
4597 +       struct file *file;
4598 +
4599 +       file = NULL;
4600 +       if (!xi)
4601 +               goto out;
4602 +
4603 +       if (idx >= 0) {
4604 +               if (idx < xi->xi_nfile)
4605 +                       file = xi->xi_file[idx];
4606 +       } else
4607 +               file = au_xino_file1(xi);
4608 +
4609 +out:
4610 +       return file;
4611 +}
4612 +
4613 +/* ---------------------------------------------------------------------- */
4614 +
4615 +/* Superblock to branch */
4616 +static inline
4617 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4618 +{
4619 +       return au_sbr(sb, bindex)->br_id;
4620 +}
4621 +
4622 +static inline
4623 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4624 +{
4625 +       return au_br_mnt(au_sbr(sb, bindex));
4626 +}
4627 +
4628 +static inline
4629 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4630 +{
4631 +       return au_br_userns(au_sbr(sb, bindex));
4632 +}
4633 +
4634 +static inline
4635 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4636 +{
4637 +       return au_br_sb(au_sbr(sb, bindex));
4638 +}
4639 +
4640 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4641 +{
4642 +       return au_sbr(sb, bindex)->br_perm;
4643 +}
4644 +
4645 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4646 +{
4647 +       return au_br_whable(au_sbr_perm(sb, bindex));
4648 +}
4649 +
4650 +/* ---------------------------------------------------------------------- */
4651 +
4652 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4653 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4654 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4655 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4656 +/*
4657 +#define wbr_wh_read_trylock_nested(wbr) \
4658 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4659 +#define wbr_wh_write_trylock_nested(wbr) \
4660 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4661 +*/
4662 +
4663 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4664 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4665 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4666 +
4667 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4668 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4669 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4670 +
4671 +/* ---------------------------------------------------------------------- */
4672 +
4673 +#ifdef CONFIG_AUFS_FHSM
4674 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4675 +{
4676 +       mutex_init(&brfhsm->bf_lock);
4677 +       brfhsm->bf_jiffy = 0;
4678 +       brfhsm->bf_readable = 0;
4679 +}
4680 +
4681 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4682 +{
4683 +       mutex_destroy(&brfhsm->bf_lock);
4684 +}
4685 +#else
4686 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4687 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4688 +#endif
4689 +
4690 +#endif /* __KERNEL__ */
4691 +#endif /* __AUFS_BRANCH_H__ */
4692 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4693 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4694 +++ linux/fs/aufs/conf.mk       2021-12-03 15:38:59.933313976 +0100
4695 @@ -0,0 +1,40 @@
4696 +# SPDX-License-Identifier: GPL-2.0
4697 +
4698 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4699 +
4700 +define AuConf
4701 +ifdef ${1}
4702 +AuConfStr += ${1}=${${1}}
4703 +endif
4704 +endef
4705 +
4706 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4707 +       SBILIST \
4708 +       HNOTIFY HFSNOTIFY \
4709 +       EXPORT INO_T_64 \
4710 +       XATTR \
4711 +       FHSM \
4712 +       RDU \
4713 +       DIRREN \
4714 +       SHWH \
4715 +       BR_RAMFS \
4716 +       BR_FUSE POLL \
4717 +       BR_HFSPLUS \
4718 +       BDEV_LOOP \
4719 +       DEBUG MAGIC_SYSRQ
4720 +$(foreach i, ${AuConfAll}, \
4721 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4722 +
4723 +AuConfName = ${obj}/conf.str
4724 +${AuConfName}.tmp: FORCE
4725 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4726 +${AuConfName}: ${AuConfName}.tmp
4727 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4728 +       echo '  GEN    ' $@; \
4729 +       cp -p $< $@; \
4730 +       }
4731 +FORCE:
4732 +clean-files += ${AuConfName} ${AuConfName}.tmp
4733 +${obj}/sysfs.o: ${AuConfName}
4734 +
4735 +-include ${srctree}/${src}/conf_priv.mk
4736 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4737 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4738 +++ linux/fs/aufs/cpup.c        2021-12-03 15:40:58.233313963 +0100
4739 @@ -0,0 +1,1459 @@
4740 +// SPDX-License-Identifier: GPL-2.0
4741 +/*
4742 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4743 + *
4744 + * This program, aufs is free software; you can redistribute it and/or modify
4745 + * it under the terms of the GNU General Public License as published by
4746 + * the Free Software Foundation; either version 2 of the License, or
4747 + * (at your option) any later version.
4748 + *
4749 + * This program is distributed in the hope that it will be useful,
4750 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4751 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4752 + * GNU General Public License for more details.
4753 + *
4754 + * You should have received a copy of the GNU General Public License
4755 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4756 + */
4757 +
4758 +/*
4759 + * copy-up functions, see wbr_policy.c for copy-down
4760 + */
4761 +
4762 +#include <linux/fs_stack.h>
4763 +#include <linux/mm.h>
4764 +#include <linux/task_work.h>
4765 +#include "aufs.h"
4766 +
4767 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4768 +{
4769 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4770 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4771 +
4772 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4773 +
4774 +       dst->i_flags |= iflags & ~mask;
4775 +       if (au_test_fs_notime(dst->i_sb))
4776 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4777 +}
4778 +
4779 +void au_cpup_attr_timesizes(struct inode *inode)
4780 +{
4781 +       struct inode *h_inode;
4782 +
4783 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4784 +       fsstack_copy_attr_times(inode, h_inode);
4785 +       fsstack_copy_inode_size(inode, h_inode);
4786 +}
4787 +
4788 +void au_cpup_attr_nlink(struct inode *inode, int force)
4789 +{
4790 +       struct inode *h_inode;
4791 +       struct super_block *sb;
4792 +       aufs_bindex_t bindex, bbot;
4793 +
4794 +       sb = inode->i_sb;
4795 +       bindex = au_ibtop(inode);
4796 +       h_inode = au_h_iptr(inode, bindex);
4797 +       if (!force
4798 +           && !S_ISDIR(h_inode->i_mode)
4799 +           && au_opt_test(au_mntflags(sb), PLINK)
4800 +           && au_plink_test(inode))
4801 +               return;
4802 +
4803 +       /*
4804 +        * 0 can happen in revalidating.
4805 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4806 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4807 +        * case.
4808 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4809 +        *       the incorrect link count.
4810 +        */
4811 +       set_nlink(inode, h_inode->i_nlink);
4812 +
4813 +       /*
4814 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4815 +        * it may includes whplink directory.
4816 +        */
4817 +       if (S_ISDIR(h_inode->i_mode)) {
4818 +               bbot = au_ibbot(inode);
4819 +               for (bindex++; bindex <= bbot; bindex++) {
4820 +                       h_inode = au_h_iptr(inode, bindex);
4821 +                       if (h_inode)
4822 +                               au_add_nlink(inode, h_inode);
4823 +               }
4824 +       }
4825 +}
4826 +
4827 +void au_cpup_attr_changeable(struct inode *inode)
4828 +{
4829 +       struct inode *h_inode;
4830 +
4831 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4832 +       inode->i_mode = h_inode->i_mode;
4833 +       inode->i_uid = h_inode->i_uid;
4834 +       inode->i_gid = h_inode->i_gid;
4835 +       au_cpup_attr_timesizes(inode);
4836 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4837 +}
4838 +
4839 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4840 +{
4841 +       struct au_iinfo *iinfo = au_ii(inode);
4842 +
4843 +       IiMustWriteLock(inode);
4844 +
4845 +       iinfo->ii_higen = h_inode->i_generation;
4846 +       iinfo->ii_hsb1 = h_inode->i_sb;
4847 +}
4848 +
4849 +void au_cpup_attr_all(struct inode *inode, int force)
4850 +{
4851 +       struct inode *h_inode;
4852 +
4853 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4854 +       au_cpup_attr_changeable(inode);
4855 +       if (inode->i_nlink > 0)
4856 +               au_cpup_attr_nlink(inode, force);
4857 +       inode->i_rdev = h_inode->i_rdev;
4858 +       inode->i_blkbits = h_inode->i_blkbits;
4859 +       au_cpup_igen(inode, h_inode);
4860 +}
4861 +
4862 +/* ---------------------------------------------------------------------- */
4863 +
4864 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4865 +
4866 +/* keep the timestamps of the parent dir when cpup */
4867 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4868 +                   struct path *h_path)
4869 +{
4870 +       struct inode *h_inode;
4871 +
4872 +       dt->dt_dentry = dentry;
4873 +       dt->dt_h_path = *h_path;
4874 +       h_inode = d_inode(h_path->dentry);
4875 +       dt->dt_atime = h_inode->i_atime;
4876 +       dt->dt_mtime = h_inode->i_mtime;
4877 +       /* smp_mb(); */
4878 +}
4879 +
4880 +void au_dtime_revert(struct au_dtime *dt)
4881 +{
4882 +       struct iattr attr;
4883 +       int err;
4884 +
4885 +       attr.ia_atime = dt->dt_atime;
4886 +       attr.ia_mtime = dt->dt_mtime;
4887 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4888 +               | ATTR_ATIME | ATTR_ATIME_SET;
4889 +
4890 +       /* no delegation since this is a directory */
4891 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4892 +       if (unlikely(err))
4893 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4894 +}
4895 +
4896 +/* ---------------------------------------------------------------------- */
4897 +
4898 +/* internal use only */
4899 +struct au_cpup_reg_attr {
4900 +       int             valid;
4901 +       struct kstat    st;
4902 +       unsigned int    iflags; /* inode->i_flags */
4903 +};
4904 +
4905 +static noinline_for_stack
4906 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4907 +              struct au_cpup_reg_attr *h_src_attr)
4908 +{
4909 +       int err, sbits, icex;
4910 +       unsigned int mnt_flags;
4911 +       unsigned char verbose;
4912 +       struct iattr ia;
4913 +       struct path h_path;
4914 +       struct inode *h_isrc, *h_idst;
4915 +       struct kstat *h_st;
4916 +       struct au_branch *br;
4917 +
4918 +       br = au_sbr(dst->d_sb, bindex);
4919 +       h_path.mnt = au_br_mnt(br);
4920 +       h_path.dentry = au_h_dptr(dst, bindex);
4921 +       h_idst = d_inode(h_path.dentry);
4922 +       h_isrc = d_inode(h_src->dentry);
4923 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4924 +               | ATTR_ATIME | ATTR_MTIME
4925 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4926 +       if (h_src_attr && h_src_attr->valid) {
4927 +               h_st = &h_src_attr->st;
4928 +               ia.ia_uid = h_st->uid;
4929 +               ia.ia_gid = h_st->gid;
4930 +               ia.ia_atime = h_st->atime;
4931 +               ia.ia_mtime = h_st->mtime;
4932 +               if (h_idst->i_mode != h_st->mode
4933 +                   && !S_ISLNK(h_idst->i_mode)) {
4934 +                       ia.ia_valid |= ATTR_MODE;
4935 +                       ia.ia_mode = h_st->mode;
4936 +               }
4937 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4938 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4939 +       } else {
4940 +               ia.ia_uid = h_isrc->i_uid;
4941 +               ia.ia_gid = h_isrc->i_gid;
4942 +               ia.ia_atime = h_isrc->i_atime;
4943 +               ia.ia_mtime = h_isrc->i_mtime;
4944 +               if (h_idst->i_mode != h_isrc->i_mode
4945 +                   && !S_ISLNK(h_idst->i_mode)) {
4946 +                       ia.ia_valid |= ATTR_MODE;
4947 +                       ia.ia_mode = h_isrc->i_mode;
4948 +               }
4949 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4950 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4951 +       }
4952 +       /* no delegation since it is just created */
4953 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4954 +
4955 +       /* is this nfs only? */
4956 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4957 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4958 +               ia.ia_mode = h_isrc->i_mode;
4959 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4960 +       }
4961 +
4962 +       icex = br->br_perm & AuBrAttr_ICEX;
4963 +       if (!err) {
4964 +               mnt_flags = au_mntflags(dst->d_sb);
4965 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4966 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4967 +       }
4968 +
4969 +       return err;
4970 +}
4971 +
4972 +/* ---------------------------------------------------------------------- */
4973 +
4974 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4975 +                          char *buf, unsigned long blksize)
4976 +{
4977 +       int err;
4978 +       size_t sz, rbytes, wbytes;
4979 +       unsigned char all_zero;
4980 +       char *p, *zp;
4981 +       struct inode *h_inode;
4982 +       /* reduce stack usage */
4983 +       struct iattr *ia;
4984 +
4985 +       zp = page_address(ZERO_PAGE(0));
4986 +       if (unlikely(!zp))
4987 +               return -ENOMEM; /* possible? */
4988 +
4989 +       err = 0;
4990 +       all_zero = 0;
4991 +       while (len) {
4992 +               AuDbg("len %lld\n", len);
4993 +               sz = blksize;
4994 +               if (len < blksize)
4995 +                       sz = len;
4996 +
4997 +               rbytes = 0;
4998 +               /* todo: signal_pending? */
4999 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5000 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5001 +                       err = rbytes;
5002 +               }
5003 +               if (unlikely(err < 0))
5004 +                       break;
5005 +
5006 +               all_zero = 0;
5007 +               if (len >= rbytes && rbytes == blksize)
5008 +                       all_zero = !memcmp(buf, zp, rbytes);
5009 +               if (!all_zero) {
5010 +                       wbytes = rbytes;
5011 +                       p = buf;
5012 +                       while (wbytes) {
5013 +                               size_t b;
5014 +
5015 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5016 +                               err = b;
5017 +                               /* todo: signal_pending? */
5018 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5019 +                                       continue;
5020 +                               if (unlikely(err < 0))
5021 +                                       break;
5022 +                               wbytes -= b;
5023 +                               p += b;
5024 +                       }
5025 +                       if (unlikely(err < 0))
5026 +                               break;
5027 +               } else {
5028 +                       loff_t res;
5029 +
5030 +                       AuLabel(hole);
5031 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5032 +                       err = res;
5033 +                       if (unlikely(res < 0))
5034 +                               break;
5035 +               }
5036 +               len -= rbytes;
5037 +               err = 0;
5038 +       }
5039 +
5040 +       /* the last block may be a hole */
5041 +       if (!err && all_zero) {
5042 +               AuLabel(last hole);
5043 +
5044 +               err = 1;
5045 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5046 +                       /* nfs requires this step to make last hole */
5047 +                       /* is this only nfs? */
5048 +                       do {
5049 +                               /* todo: signal_pending? */
5050 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5051 +                       } while (err == -EAGAIN || err == -EINTR);
5052 +                       if (err == 1)
5053 +                               dst->f_pos--;
5054 +               }
5055 +
5056 +               if (err == 1) {
5057 +                       ia = (void *)buf;
5058 +                       ia->ia_size = dst->f_pos;
5059 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5060 +                       ia->ia_file = dst;
5061 +                       h_inode = file_inode(dst);
5062 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5063 +                       /* no delegation since it is just created */
5064 +                       err = vfsub_notify_change(&dst->f_path, ia,
5065 +                                                 /*delegated*/NULL);
5066 +                       inode_unlock(h_inode);
5067 +               }
5068 +       }
5069 +
5070 +       return err;
5071 +}
5072 +
5073 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5074 +{
5075 +       int err;
5076 +       unsigned long blksize;
5077 +       unsigned char do_kfree;
5078 +       char *buf;
5079 +       struct super_block *h_sb;
5080 +
5081 +       err = -ENOMEM;
5082 +       h_sb = file_inode(dst)->i_sb;
5083 +       blksize = h_sb->s_blocksize;
5084 +       if (!blksize || PAGE_SIZE < blksize)
5085 +               blksize = PAGE_SIZE;
5086 +       AuDbg("blksize %lu\n", blksize);
5087 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5088 +       if (do_kfree)
5089 +               buf = kmalloc(blksize, GFP_NOFS);
5090 +       else
5091 +               buf = (void *)__get_free_page(GFP_NOFS);
5092 +       if (unlikely(!buf))
5093 +               goto out;
5094 +
5095 +       if (len > (1 << 22))
5096 +               AuDbg("copying a large file %lld\n", (long long)len);
5097 +
5098 +       src->f_pos = 0;
5099 +       dst->f_pos = 0;
5100 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5101 +       if (do_kfree) {
5102 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5103 +               au_kfree_do_rcu(buf);
5104 +       } else
5105 +               free_page((unsigned long)buf);
5106 +
5107 +out:
5108 +       return err;
5109 +}
5110 +
5111 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5112 +{
5113 +       int err;
5114 +       struct super_block *h_src_sb;
5115 +       struct inode *h_src_inode;
5116 +
5117 +       h_src_inode = file_inode(src);
5118 +       h_src_sb = h_src_inode->i_sb;
5119 +
5120 +       /* XFS acquires inode_lock */
5121 +       if (!au_test_xfs(h_src_sb))
5122 +               err = au_copy_file(dst, src, len);
5123 +       else {
5124 +               inode_unlock_shared(h_src_inode);
5125 +               err = au_copy_file(dst, src, len);
5126 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5127 +       }
5128 +
5129 +       return err;
5130 +}
5131 +
5132 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5133 +{
5134 +       int err;
5135 +       loff_t lo;
5136 +       struct super_block *h_src_sb;
5137 +       struct inode *h_src_inode;
5138 +
5139 +       h_src_inode = file_inode(src);
5140 +       h_src_sb = h_src_inode->i_sb;
5141 +       if (h_src_sb != file_inode(dst)->i_sb
5142 +           || !dst->f_op->remap_file_range) {
5143 +               err = au_do_copy(dst, src, len);
5144 +               goto out;
5145 +       }
5146 +
5147 +       if (!au_test_nfs(h_src_sb)) {
5148 +               inode_unlock_shared(h_src_inode);
5149 +               lo = vfsub_clone_file_range(src, dst, len);
5150 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5151 +       } else
5152 +               lo = vfsub_clone_file_range(src, dst, len);
5153 +       if (lo == len) {
5154 +               err = 0;
5155 +               goto out; /* success */
5156 +       } else if (lo >= 0)
5157 +               /* todo: possible? */
5158 +               /* paritially succeeded */
5159 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5160 +       else if (lo != -EOPNOTSUPP) {
5161 +               /* older XFS has a condition in cloning */
5162 +               err = lo;
5163 +               goto out;
5164 +       }
5165 +
5166 +       /* the backend fs on NFS may not support cloning */
5167 +       err = au_do_copy(dst, src, len);
5168 +
5169 +out:
5170 +       AuTraceErr(err);
5171 +       return err;
5172 +}
5173 +
5174 +/*
5175 + * to support a sparse file which is opened with O_APPEND,
5176 + * we need to close the file.
5177 + */
5178 +static int au_cp_regular(struct au_cp_generic *cpg)
5179 +{
5180 +       int err, i;
5181 +       enum { SRC, DST };
5182 +       struct {
5183 +               aufs_bindex_t bindex;
5184 +               unsigned int flags;
5185 +               struct dentry *dentry;
5186 +               int force_wr;
5187 +               struct file *file;
5188 +       } *f, file[] = {
5189 +               {
5190 +                       .bindex = cpg->bsrc,
5191 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5192 +               },
5193 +               {
5194 +                       .bindex = cpg->bdst,
5195 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5196 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5197 +               }
5198 +       };
5199 +       struct au_branch *br;
5200 +       struct super_block *sb, *h_src_sb;
5201 +       struct inode *h_src_inode;
5202 +       struct task_struct *tsk = current;
5203 +
5204 +       /* bsrc branch can be ro/rw. */
5205 +       sb = cpg->dentry->d_sb;
5206 +       f = file;
5207 +       for (i = 0; i < 2; i++, f++) {
5208 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5209 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5210 +                                   /*file*/NULL, f->force_wr);
5211 +               if (IS_ERR(f->file)) {
5212 +                       err = PTR_ERR(f->file);
5213 +                       if (i == SRC)
5214 +                               goto out;
5215 +                       else
5216 +                               goto out_src;
5217 +               }
5218 +       }
5219 +
5220 +       /* try stopping to update while we copyup */
5221 +       h_src_inode = d_inode(file[SRC].dentry);
5222 +       h_src_sb = h_src_inode->i_sb;
5223 +       if (!au_test_nfs(h_src_sb))
5224 +               IMustLock(h_src_inode);
5225 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5226 +
5227 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5228 +       if (tsk->flags & PF_KTHREAD)
5229 +               __fput_sync(file[DST].file);
5230 +       else {
5231 +               /* it happened actually */
5232 +               fput(file[DST].file);
5233 +               /*
5234 +                * too bad.
5235 +                * we have to call both since we don't know which place the file
5236 +                * was added to.
5237 +                */
5238 +               task_work_run();
5239 +               flush_delayed_fput();
5240 +       }
5241 +       br = au_sbr(sb, file[DST].bindex);
5242 +       au_lcnt_dec(&br->br_nfiles);
5243 +
5244 +out_src:
5245 +       fput(file[SRC].file);
5246 +       br = au_sbr(sb, file[SRC].bindex);
5247 +       au_lcnt_dec(&br->br_nfiles);
5248 +out:
5249 +       return err;
5250 +}
5251 +
5252 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5253 +                             struct au_cpup_reg_attr *h_src_attr)
5254 +{
5255 +       int err, rerr;
5256 +       loff_t l;
5257 +       struct path h_path;
5258 +       struct inode *h_src_inode, *h_dst_inode;
5259 +
5260 +       err = 0;
5261 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5262 +       l = i_size_read(h_src_inode);
5263 +       if (cpg->len == -1 || l < cpg->len)
5264 +               cpg->len = l;
5265 +       if (cpg->len) {
5266 +               /* try stopping to update while we are referencing */
5267 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5268 +               au_pin_hdir_unlock(cpg->pin);
5269 +
5270 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5271 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5272 +               h_src_attr->iflags = h_src_inode->i_flags;
5273 +               if (!au_test_nfs(h_src_inode->i_sb))
5274 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5275 +               else {
5276 +                       inode_unlock_shared(h_src_inode);
5277 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5278 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5279 +               }
5280 +               if (unlikely(err)) {
5281 +                       inode_unlock_shared(h_src_inode);
5282 +                       goto out;
5283 +               }
5284 +               h_src_attr->valid = 1;
5285 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5286 +                       err = au_cp_regular(cpg);
5287 +                       inode_unlock_shared(h_src_inode);
5288 +               } else {
5289 +                       inode_unlock_shared(h_src_inode);
5290 +                       err = au_cp_regular(cpg);
5291 +               }
5292 +               rerr = au_pin_hdir_relock(cpg->pin);
5293 +               if (!err && rerr)
5294 +                       err = rerr;
5295 +       }
5296 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5297 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5298 +               h_dst_inode = d_inode(h_path.dentry);
5299 +               spin_lock(&h_dst_inode->i_lock);
5300 +               h_dst_inode->i_state |= I_LINKABLE;
5301 +               spin_unlock(&h_dst_inode->i_lock);
5302 +       }
5303 +
5304 +out:
5305 +       return err;
5306 +}
5307 +
5308 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5309 +                             struct inode *h_dir)
5310 +{
5311 +       int err;
5312 +       DEFINE_DELAYED_CALL(done);
5313 +       const char *sym;
5314 +
5315 +       sym = vfs_get_link(h_src, &done);
5316 +       err = PTR_ERR(sym);
5317 +       if (IS_ERR(sym))
5318 +               goto out;
5319 +
5320 +       err = vfsub_symlink(h_dir, h_path, sym);
5321 +
5322 +out:
5323 +       do_delayed_call(&done);
5324 +       return err;
5325 +}
5326 +
5327 +/*
5328 + * regardless 'acl' option, reset all ACL.
5329 + * All ACL will be copied up later from the original entry on the lower branch.
5330 + */
5331 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5332 +{
5333 +       int err;
5334 +       struct dentry *h_dentry;
5335 +       struct inode *h_inode;
5336 +       struct user_namespace *h_userns;
5337 +
5338 +       h_userns = mnt_user_ns(h_path->mnt);
5339 +       h_dentry = h_path->dentry;
5340 +       h_inode = d_inode(h_dentry);
5341 +       /* forget_all_cached_acls(h_inode)); */
5342 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5343 +       AuTraceErr(err);
5344 +       if (err == -EOPNOTSUPP)
5345 +               err = 0;
5346 +       if (!err)
5347 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5348 +
5349 +       AuTraceErr(err);
5350 +       return err;
5351 +}
5352 +
5353 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5354 +                         struct inode *h_dir, struct path *h_path)
5355 +{
5356 +       int err;
5357 +       struct inode *dir, *inode;
5358 +       struct user_namespace *h_userns;
5359 +
5360 +       h_userns = mnt_user_ns(h_path->mnt);
5361 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5362 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5363 +       AuTraceErr(err);
5364 +       if (err == -EOPNOTSUPP)
5365 +               err = 0;
5366 +       if (unlikely(err))
5367 +               goto out;
5368 +
5369 +       /*
5370 +        * strange behaviour from the users view,
5371 +        * particularly setattr case
5372 +        */
5373 +       dir = d_inode(dst_parent);
5374 +       if (au_ibtop(dir) == cpg->bdst)
5375 +               au_cpup_attr_nlink(dir, /*force*/1);
5376 +       inode = d_inode(cpg->dentry);
5377 +       au_cpup_attr_nlink(inode, /*force*/1);
5378 +
5379 +out:
5380 +       return err;
5381 +}
5382 +
5383 +static noinline_for_stack
5384 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5385 +              struct au_cpup_reg_attr *h_src_attr)
5386 +{
5387 +       int err;
5388 +       umode_t mode;
5389 +       unsigned int mnt_flags;
5390 +       unsigned char isdir, isreg, force;
5391 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5392 +       struct au_dtime dt;
5393 +       struct path h_path;
5394 +       struct dentry *h_src, *h_dst, *h_parent;
5395 +       struct inode *h_inode, *h_dir;
5396 +       struct super_block *sb;
5397 +
5398 +       /* bsrc branch can be ro/rw. */
5399 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5400 +       h_inode = d_inode(h_src);
5401 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5402 +
5403 +       /* try stopping to be referenced while we are creating */
5404 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5405 +       if (au_ftest_cpup(cpg->flags, RENAME))
5406 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5407 +                                 AUFS_WH_PFX_LEN));
5408 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5409 +       h_dir = d_inode(h_parent);
5410 +       IMustLock(h_dir);
5411 +       AuDebugOn(h_parent != h_dst->d_parent);
5412 +
5413 +       sb = cpg->dentry->d_sb;
5414 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5415 +       if (do_dt) {
5416 +               h_path.dentry = h_parent;
5417 +               au_dtime_store(&dt, dst_parent, &h_path);
5418 +       }
5419 +       h_path.dentry = h_dst;
5420 +
5421 +       isreg = 0;
5422 +       isdir = 0;
5423 +       mode = h_inode->i_mode;
5424 +       switch (mode & S_IFMT) {
5425 +       case S_IFREG:
5426 +               isreg = 1;
5427 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5428 +               if (!err)
5429 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5430 +               break;
5431 +       case S_IFDIR:
5432 +               isdir = 1;
5433 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5434 +               if (!err)
5435 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5436 +               break;
5437 +       case S_IFLNK:
5438 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5439 +               break;
5440 +       case S_IFCHR:
5441 +       case S_IFBLK:
5442 +               AuDebugOn(!capable(CAP_MKNOD));
5443 +               fallthrough;
5444 +       case S_IFIFO:
5445 +       case S_IFSOCK:
5446 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5447 +               break;
5448 +       default:
5449 +               AuIOErr("Unknown inode type 0%o\n", mode);
5450 +               err = -EIO;
5451 +       }
5452 +       if (!err)
5453 +               err = au_reset_acl(h_dir, &h_path, mode);
5454 +
5455 +       mnt_flags = au_mntflags(sb);
5456 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5457 +           && !isdir
5458 +           && au_opt_test(mnt_flags, XINO)
5459 +           && (h_inode->i_nlink == 1
5460 +               || (h_inode->i_state & I_LINKABLE))
5461 +           /* todo: unnecessary? */
5462 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5463 +           && cpg->bdst < cpg->bsrc
5464 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5465 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5466 +               /* ignore this error */
5467 +
5468 +       if (!err) {
5469 +               force = 0;
5470 +               if (isreg) {
5471 +                       force = !!cpg->len;
5472 +                       if (cpg->len == -1)
5473 +                               force = !!i_size_read(h_inode);
5474 +               }
5475 +               au_fhsm_wrote(sb, cpg->bdst, force);
5476 +       }
5477 +
5478 +       if (do_dt)
5479 +               au_dtime_revert(&dt);
5480 +       return err;
5481 +}
5482 +
5483 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5484 +{
5485 +       int err;
5486 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5487 +       struct path h_ppath;
5488 +       struct inode *h_dir;
5489 +       aufs_bindex_t bdst;
5490 +
5491 +       dentry = cpg->dentry;
5492 +       bdst = cpg->bdst;
5493 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5494 +       h_dentry = au_h_dptr(dentry, bdst);
5495 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5496 +               dget(h_dentry);
5497 +               au_set_h_dptr(dentry, bdst, NULL);
5498 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5499 +               if (!err)
5500 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5501 +               au_set_h_dptr(dentry, bdst, h_dentry);
5502 +       } else {
5503 +               err = 0;
5504 +               parent = dget_parent(dentry);
5505 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5506 +               dput(parent);
5507 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5508 +               if (IS_ERR(h_path->dentry))
5509 +                       err = PTR_ERR(h_path->dentry);
5510 +       }
5511 +       if (unlikely(err))
5512 +               goto out;
5513 +
5514 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5515 +       h_dir = d_inode(h_parent);
5516 +       IMustLock(h_dir);
5517 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5518 +       /* no delegation since it is just created */
5519 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5520 +                          /*flags*/0);
5521 +       dput(h_path->dentry);
5522 +
5523 +out:
5524 +       return err;
5525 +}
5526 +
5527 +/*
5528 + * copyup the @dentry from @bsrc to @bdst.
5529 + * the caller must set the both of lower dentries.
5530 + * @len is for truncating when it is -1 copyup the entire file.
5531 + * in link/rename cases, @dst_parent may be different from the real one.
5532 + * basic->bsrc can be larger than basic->bdst.
5533 + * aufs doesn't touch the credential so
5534 + * security_inode_copy_up{,_xattr}() are unnecessary.
5535 + */
5536 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5537 +{
5538 +       int err, rerr;
5539 +       aufs_bindex_t old_ibtop;
5540 +       unsigned char isdir, plink;
5541 +       struct dentry *h_src, *h_dst, *h_parent;
5542 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5543 +       struct super_block *sb;
5544 +       struct au_branch *br;
5545 +       struct path h_src_path;
5546 +       /* to reduce stack size */
5547 +       struct {
5548 +               struct au_dtime dt;
5549 +               struct path h_path;
5550 +               struct au_cpup_reg_attr h_src_attr;
5551 +       } *a;
5552 +
5553 +       err = -ENOMEM;
5554 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5555 +       if (unlikely(!a))
5556 +               goto out;
5557 +       a->h_src_attr.valid = 0;
5558 +
5559 +       sb = cpg->dentry->d_sb;
5560 +       br = au_sbr(sb, cpg->bdst);
5561 +       a->h_path.mnt = au_br_mnt(br);
5562 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5563 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5564 +       h_dir = d_inode(h_parent);
5565 +       IMustLock(h_dir);
5566 +
5567 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5568 +       inode = d_inode(cpg->dentry);
5569 +
5570 +       if (!dst_parent)
5571 +               dst_parent = dget_parent(cpg->dentry);
5572 +       else
5573 +               dget(dst_parent);
5574 +
5575 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5576 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5577 +       if (dst_inode) {
5578 +               if (unlikely(!plink)) {
5579 +                       err = -EIO;
5580 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5581 +                               "but plink is disabled\n",
5582 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5583 +                       goto out_parent;
5584 +               }
5585 +
5586 +               if (dst_inode->i_nlink) {
5587 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5588 +
5589 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5590 +                       err = PTR_ERR(h_src);
5591 +                       if (IS_ERR(h_src))
5592 +                               goto out_parent;
5593 +                       if (unlikely(d_is_negative(h_src))) {
5594 +                               err = -EIO;
5595 +                               AuIOErr("i%lu exists on b%d "
5596 +                                       "but not pseudo-linked\n",
5597 +                                       inode->i_ino, cpg->bdst);
5598 +                               dput(h_src);
5599 +                               goto out_parent;
5600 +                       }
5601 +
5602 +                       if (do_dt) {
5603 +                               a->h_path.dentry = h_parent;
5604 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5605 +                       }
5606 +
5607 +                       a->h_path.dentry = h_dst;
5608 +                       delegated = NULL;
5609 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5610 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5611 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5612 +                       if (do_dt)
5613 +                               au_dtime_revert(&a->dt);
5614 +                       if (unlikely(err == -EWOULDBLOCK)) {
5615 +                               pr_warn("cannot retry for NFSv4 delegation"
5616 +                                       " for an internal link\n");
5617 +                               iput(delegated);
5618 +                       }
5619 +                       dput(h_src);
5620 +                       goto out_parent;
5621 +               } else
5622 +                       /* todo: cpup_wh_file? */
5623 +                       /* udba work */
5624 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5625 +       }
5626 +
5627 +       isdir = S_ISDIR(inode->i_mode);
5628 +       old_ibtop = au_ibtop(inode);
5629 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5630 +       if (unlikely(err))
5631 +               goto out_rev;
5632 +       dst_inode = d_inode(h_dst);
5633 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5634 +       /* todo: necessary? */
5635 +       /* au_pin_hdir_unlock(cpg->pin); */
5636 +
5637 +       h_src_path.dentry = h_src;
5638 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5639 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5640 +       if (unlikely(err)) {
5641 +               /* todo: necessary? */
5642 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5643 +               inode_unlock(dst_inode);
5644 +               goto out_rev;
5645 +       }
5646 +
5647 +       if (cpg->bdst < old_ibtop) {
5648 +               if (S_ISREG(inode->i_mode)) {
5649 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5650 +                       if (unlikely(err)) {
5651 +                               /* ignore an error */
5652 +                               /* au_pin_hdir_relock(cpg->pin); */
5653 +                               inode_unlock(dst_inode);
5654 +                               goto out_rev;
5655 +                       }
5656 +               }
5657 +               au_set_ibtop(inode, cpg->bdst);
5658 +       } else
5659 +               au_set_ibbot(inode, cpg->bdst);
5660 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5661 +                     au_hi_flags(inode, isdir));
5662 +
5663 +       /* todo: necessary? */
5664 +       /* err = au_pin_hdir_relock(cpg->pin); */
5665 +       inode_unlock(dst_inode);
5666 +       if (unlikely(err))
5667 +               goto out_rev;
5668 +
5669 +       src_inode = d_inode(h_src);
5670 +       if (!isdir
5671 +           && (src_inode->i_nlink > 1
5672 +               || src_inode->i_state & I_LINKABLE)
5673 +           && plink)
5674 +               au_plink_append(inode, cpg->bdst, h_dst);
5675 +
5676 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5677 +               a->h_path.dentry = h_dst;
5678 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5679 +       }
5680 +       if (!err)
5681 +               goto out_parent; /* success */
5682 +
5683 +       /* revert */
5684 +out_rev:
5685 +       a->h_path.dentry = h_parent;
5686 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5687 +       a->h_path.dentry = h_dst;
5688 +       rerr = 0;
5689 +       if (d_is_positive(h_dst)) {
5690 +               if (!isdir) {
5691 +                       /* no delegation since it is just created */
5692 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5693 +                                           /*delegated*/NULL, /*force*/0);
5694 +               } else
5695 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5696 +       }
5697 +       au_dtime_revert(&a->dt);
5698 +       if (rerr) {
5699 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5700 +               err = -EIO;
5701 +       }
5702 +out_parent:
5703 +       dput(dst_parent);
5704 +       au_kfree_rcu(a);
5705 +out:
5706 +       return err;
5707 +}
5708 +
5709 +#if 0 /* reserved */
5710 +struct au_cpup_single_args {
5711 +       int *errp;
5712 +       struct au_cp_generic *cpg;
5713 +       struct dentry *dst_parent;
5714 +};
5715 +
5716 +static void au_call_cpup_single(void *args)
5717 +{
5718 +       struct au_cpup_single_args *a = args;
5719 +
5720 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5721 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5722 +       au_pin_hdir_release(a->cpg->pin);
5723 +}
5724 +#endif
5725 +
5726 +/*
5727 + * prevent SIGXFSZ in copy-up.
5728 + * testing CAP_MKNOD is for generic fs,
5729 + * but CAP_FSETID is for xfs only, currently.
5730 + */
5731 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5732 +{
5733 +       int do_sio;
5734 +       struct super_block *sb;
5735 +       struct inode *h_dir;
5736 +
5737 +       do_sio = 0;
5738 +       sb = au_pinned_parent(pin)->d_sb;
5739 +       if (!au_wkq_test()
5740 +           && (!au_sbi(sb)->si_plink_maint_pid
5741 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5742 +               switch (mode & S_IFMT) {
5743 +               case S_IFREG:
5744 +                       /* no condition about RLIMIT_FSIZE and the file size */
5745 +                       do_sio = 1;
5746 +                       break;
5747 +               case S_IFCHR:
5748 +               case S_IFBLK:
5749 +                       do_sio = !capable(CAP_MKNOD);
5750 +                       break;
5751 +               }
5752 +               if (!do_sio)
5753 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5754 +                                 && !capable(CAP_FSETID));
5755 +               /* this workaround may be removed in the future */
5756 +               if (!do_sio) {
5757 +                       h_dir = au_pinned_h_dir(pin);
5758 +                       do_sio = h_dir->i_mode & S_ISVTX;
5759 +               }
5760 +       }
5761 +
5762 +       return do_sio;
5763 +}
5764 +
5765 +#if 0 /* reserved */
5766 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5767 +{
5768 +       int err, wkq_err;
5769 +       struct dentry *h_dentry;
5770 +
5771 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5772 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5773 +               err = au_cpup_single(cpg, dst_parent);
5774 +       else {
5775 +               struct au_cpup_single_args args = {
5776 +                       .errp           = &err,
5777 +                       .cpg            = cpg,
5778 +                       .dst_parent     = dst_parent
5779 +               };
5780 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5781 +               if (unlikely(wkq_err))
5782 +                       err = wkq_err;
5783 +       }
5784 +
5785 +       return err;
5786 +}
5787 +#endif
5788 +
5789 +/*
5790 + * copyup the @dentry from the first active lower branch to @bdst,
5791 + * using au_cpup_single().
5792 + */
5793 +static int au_cpup_simple(struct au_cp_generic *cpg)
5794 +{
5795 +       int err;
5796 +       unsigned int flags_orig;
5797 +       struct dentry *dentry;
5798 +
5799 +       AuDebugOn(cpg->bsrc < 0);
5800 +
5801 +       dentry = cpg->dentry;
5802 +       DiMustWriteLock(dentry);
5803 +
5804 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5805 +       if (!err) {
5806 +               flags_orig = cpg->flags;
5807 +               au_fset_cpup(cpg->flags, RENAME);
5808 +               err = au_cpup_single(cpg, NULL);
5809 +               cpg->flags = flags_orig;
5810 +               if (!err)
5811 +                       return 0; /* success */
5812 +
5813 +               /* revert */
5814 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5815 +               au_set_dbtop(dentry, cpg->bsrc);
5816 +       }
5817 +
5818 +       return err;
5819 +}
5820 +
5821 +struct au_cpup_simple_args {
5822 +       int *errp;
5823 +       struct au_cp_generic *cpg;
5824 +};
5825 +
5826 +static void au_call_cpup_simple(void *args)
5827 +{
5828 +       struct au_cpup_simple_args *a = args;
5829 +
5830 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5831 +       *a->errp = au_cpup_simple(a->cpg);
5832 +       au_pin_hdir_release(a->cpg->pin);
5833 +}
5834 +
5835 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5836 +{
5837 +       int err, wkq_err;
5838 +       struct dentry *dentry, *parent;
5839 +       struct file *h_file;
5840 +       struct inode *h_dir;
5841 +       struct user_namespace *h_userns;
5842 +
5843 +       dentry = cpg->dentry;
5844 +       h_file = NULL;
5845 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5846 +               AuDebugOn(cpg->bsrc < 0);
5847 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5848 +               err = PTR_ERR(h_file);
5849 +               if (IS_ERR(h_file))
5850 +                       goto out;
5851 +       }
5852 +
5853 +       parent = dget_parent(dentry);
5854 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5855 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5856 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5857 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5858 +               err = au_cpup_simple(cpg);
5859 +       else {
5860 +               struct au_cpup_simple_args args = {
5861 +                       .errp           = &err,
5862 +                       .cpg            = cpg
5863 +               };
5864 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5865 +               if (unlikely(wkq_err))
5866 +                       err = wkq_err;
5867 +       }
5868 +
5869 +       dput(parent);
5870 +       if (h_file)
5871 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5872 +
5873 +out:
5874 +       return err;
5875 +}
5876 +
5877 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5878 +{
5879 +       aufs_bindex_t bsrc, bbot;
5880 +       struct dentry *dentry, *h_dentry;
5881 +
5882 +       if (cpg->bsrc < 0) {
5883 +               dentry = cpg->dentry;
5884 +               bbot = au_dbbot(dentry);
5885 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5886 +                       h_dentry = au_h_dptr(dentry, bsrc);
5887 +                       if (h_dentry) {
5888 +                               AuDebugOn(d_is_negative(h_dentry));
5889 +                               break;
5890 +                       }
5891 +               }
5892 +               AuDebugOn(bsrc > bbot);
5893 +               cpg->bsrc = bsrc;
5894 +       }
5895 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5896 +       return au_do_sio_cpup_simple(cpg);
5897 +}
5898 +
5899 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5900 +{
5901 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5902 +       return au_do_sio_cpup_simple(cpg);
5903 +}
5904 +
5905 +/* ---------------------------------------------------------------------- */
5906 +
5907 +/*
5908 + * copyup the deleted file for writing.
5909 + */
5910 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5911 +                        struct file *file)
5912 +{
5913 +       int err;
5914 +       unsigned int flags_orig;
5915 +       aufs_bindex_t bsrc_orig;
5916 +       struct au_dinfo *dinfo;
5917 +       struct {
5918 +               struct au_hdentry *hd;
5919 +               struct dentry *h_dentry;
5920 +       } hdst, hsrc;
5921 +
5922 +       dinfo = au_di(cpg->dentry);
5923 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5924 +
5925 +       bsrc_orig = cpg->bsrc;
5926 +       cpg->bsrc = dinfo->di_btop;
5927 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5928 +       hdst.h_dentry = hdst.hd->hd_dentry;
5929 +       hdst.hd->hd_dentry = wh_dentry;
5930 +       dinfo->di_btop = cpg->bdst;
5931 +
5932 +       hsrc.h_dentry = NULL;
5933 +       if (file) {
5934 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5935 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5936 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5937 +       }
5938 +       flags_orig = cpg->flags;
5939 +       cpg->flags = !AuCpup_DTIME;
5940 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5941 +       cpg->flags = flags_orig;
5942 +       if (file) {
5943 +               if (!err)
5944 +                       err = au_reopen_nondir(file);
5945 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5946 +       }
5947 +       hdst.hd->hd_dentry = hdst.h_dentry;
5948 +       dinfo->di_btop = cpg->bsrc;
5949 +       cpg->bsrc = bsrc_orig;
5950 +
5951 +       return err;
5952 +}
5953 +
5954 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5955 +{
5956 +       int err;
5957 +       aufs_bindex_t bdst;
5958 +       struct au_dtime dt;
5959 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5960 +       struct au_branch *br;
5961 +       struct path h_path;
5962 +
5963 +       dentry = cpg->dentry;
5964 +       bdst = cpg->bdst;
5965 +       br = au_sbr(dentry->d_sb, bdst);
5966 +       parent = dget_parent(dentry);
5967 +       h_parent = au_h_dptr(parent, bdst);
5968 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5969 +       err = PTR_ERR(wh_dentry);
5970 +       if (IS_ERR(wh_dentry))
5971 +               goto out;
5972 +
5973 +       h_path.dentry = h_parent;
5974 +       h_path.mnt = au_br_mnt(br);
5975 +       au_dtime_store(&dt, parent, &h_path);
5976 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5977 +       if (unlikely(err))
5978 +               goto out_wh;
5979 +
5980 +       dget(wh_dentry);
5981 +       h_path.dentry = wh_dentry;
5982 +       if (!d_is_dir(wh_dentry)) {
5983 +               /* no delegation since it is just created */
5984 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5985 +                                  /*delegated*/NULL, /*force*/0);
5986 +       } else
5987 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5988 +       if (unlikely(err)) {
5989 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5990 +                       wh_dentry, err);
5991 +               err = -EIO;
5992 +       }
5993 +       au_dtime_revert(&dt);
5994 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5995 +
5996 +out_wh:
5997 +       dput(wh_dentry);
5998 +out:
5999 +       dput(parent);
6000 +       return err;
6001 +}
6002 +
6003 +struct au_cpup_wh_args {
6004 +       int *errp;
6005 +       struct au_cp_generic *cpg;
6006 +       struct file *file;
6007 +};
6008 +
6009 +static void au_call_cpup_wh(void *args)
6010 +{
6011 +       struct au_cpup_wh_args *a = args;
6012 +
6013 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6014 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6015 +       au_pin_hdir_release(a->cpg->pin);
6016 +}
6017 +
6018 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6019 +{
6020 +       int err, wkq_err;
6021 +       aufs_bindex_t bdst;
6022 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6023 +       struct inode *dir, *h_dir, *h_tmpdir;
6024 +       struct au_wbr *wbr;
6025 +       struct au_pin wh_pin, *pin_orig;
6026 +       struct user_namespace *h_userns;
6027 +
6028 +       dentry = cpg->dentry;
6029 +       bdst = cpg->bdst;
6030 +       parent = dget_parent(dentry);
6031 +       dir = d_inode(parent);
6032 +       h_orph = NULL;
6033 +       h_parent = NULL;
6034 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6035 +       h_tmpdir = h_dir;
6036 +       pin_orig = NULL;
6037 +       if (!h_dir->i_nlink) {
6038 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6039 +               h_orph = wbr->wbr_orph;
6040 +
6041 +               h_parent = dget(au_h_dptr(parent, bdst));
6042 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6043 +               h_tmpdir = d_inode(h_orph);
6044 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6045 +
6046 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6047 +               /* todo: au_h_open_pre()? */
6048 +
6049 +               pin_orig = cpg->pin;
6050 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6051 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6052 +               cpg->pin = &wh_pin;
6053 +       }
6054 +
6055 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6056 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6057 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6058 +               err = au_cpup_wh(cpg, file);
6059 +       else {
6060 +               struct au_cpup_wh_args args = {
6061 +                       .errp   = &err,
6062 +                       .cpg    = cpg,
6063 +                       .file   = file
6064 +               };
6065 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6066 +               if (unlikely(wkq_err))
6067 +                       err = wkq_err;
6068 +       }
6069 +
6070 +       if (h_orph) {
6071 +               inode_unlock(h_tmpdir);
6072 +               /* todo: au_h_open_post()? */
6073 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6074 +               au_set_h_dptr(parent, bdst, h_parent);
6075 +               AuDebugOn(!pin_orig);
6076 +               cpg->pin = pin_orig;
6077 +       }
6078 +       iput(h_dir);
6079 +       dput(parent);
6080 +
6081 +       return err;
6082 +}
6083 +
6084 +/* ---------------------------------------------------------------------- */
6085 +
6086 +/*
6087 + * generic routine for both of copy-up and copy-down.
6088 + */
6089 +/* cf. revalidate function in file.c */
6090 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6091 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6092 +                        struct au_pin *pin,
6093 +                        struct dentry *h_parent, void *arg),
6094 +              void *arg)
6095 +{
6096 +       int err;
6097 +       struct au_pin pin;
6098 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6099 +
6100 +       err = 0;
6101 +       parent = dget_parent(dentry);
6102 +       if (IS_ROOT(parent))
6103 +               goto out;
6104 +
6105 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6106 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6107 +
6108 +       /* do not use au_dpage */
6109 +       real_parent = parent;
6110 +       while (1) {
6111 +               dput(parent);
6112 +               parent = dget_parent(dentry);
6113 +               h_parent = au_h_dptr(parent, bdst);
6114 +               if (h_parent)
6115 +                       goto out; /* success */
6116 +
6117 +               /* find top dir which is necessary to cpup */
6118 +               do {
6119 +                       d = parent;
6120 +                       dput(parent);
6121 +                       parent = dget_parent(d);
6122 +                       di_read_lock_parent3(parent, !AuLock_IR);
6123 +                       h_parent = au_h_dptr(parent, bdst);
6124 +                       di_read_unlock(parent, !AuLock_IR);
6125 +               } while (!h_parent);
6126 +
6127 +               if (d != real_parent)
6128 +                       di_write_lock_child3(d);
6129 +
6130 +               /* somebody else might create while we were sleeping */
6131 +               h_dentry = au_h_dptr(d, bdst);
6132 +               if (!h_dentry || d_is_negative(h_dentry)) {
6133 +                       if (h_dentry)
6134 +                               au_update_dbtop(d);
6135 +
6136 +                       au_pin_set_dentry(&pin, d);
6137 +                       err = au_do_pin(&pin);
6138 +                       if (!err) {
6139 +                               err = cp(d, bdst, &pin, h_parent, arg);
6140 +                               au_unpin(&pin);
6141 +                       }
6142 +               }
6143 +
6144 +               if (d != real_parent)
6145 +                       di_write_unlock(d);
6146 +               if (unlikely(err))
6147 +                       break;
6148 +       }
6149 +
6150 +out:
6151 +       dput(parent);
6152 +       return err;
6153 +}
6154 +
6155 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6156 +                      struct au_pin *pin,
6157 +                      struct dentry *h_parent __maybe_unused,
6158 +                      void *arg __maybe_unused)
6159 +{
6160 +       struct au_cp_generic cpg = {
6161 +               .dentry = dentry,
6162 +               .bdst   = bdst,
6163 +               .bsrc   = -1,
6164 +               .len    = 0,
6165 +               .pin    = pin,
6166 +               .flags  = AuCpup_DTIME
6167 +       };
6168 +       return au_sio_cpup_simple(&cpg);
6169 +}
6170 +
6171 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6172 +{
6173 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6174 +}
6175 +
6176 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6177 +{
6178 +       int err;
6179 +       struct dentry *parent;
6180 +       struct inode *dir;
6181 +
6182 +       parent = dget_parent(dentry);
6183 +       dir = d_inode(parent);
6184 +       err = 0;
6185 +       if (au_h_iptr(dir, bdst))
6186 +               goto out;
6187 +
6188 +       di_read_unlock(parent, AuLock_IR);
6189 +       di_write_lock_parent(parent);
6190 +       /* someone else might change our inode while we were sleeping */
6191 +       if (!au_h_iptr(dir, bdst))
6192 +               err = au_cpup_dirs(dentry, bdst);
6193 +       di_downgrade_lock(parent, AuLock_IR);
6194 +
6195 +out:
6196 +       dput(parent);
6197 +       return err;
6198 +}
6199 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6200 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6201 +++ linux/fs/aufs/cpup.h        2021-12-03 15:38:59.933313976 +0100
6202 @@ -0,0 +1,100 @@
6203 +/* SPDX-License-Identifier: GPL-2.0 */
6204 +/*
6205 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6206 + *
6207 + * This program, aufs is free software; you can redistribute it and/or modify
6208 + * it under the terms of the GNU General Public License as published by
6209 + * the Free Software Foundation; either version 2 of the License, or
6210 + * (at your option) any later version.
6211 + *
6212 + * This program is distributed in the hope that it will be useful,
6213 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6214 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6215 + * GNU General Public License for more details.
6216 + *
6217 + * You should have received a copy of the GNU General Public License
6218 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6219 + */
6220 +
6221 +/*
6222 + * copy-up/down functions
6223 + */
6224 +
6225 +#ifndef __AUFS_CPUP_H__
6226 +#define __AUFS_CPUP_H__
6227 +
6228 +#ifdef __KERNEL__
6229 +
6230 +#include <linux/path.h>
6231 +
6232 +struct inode;
6233 +struct file;
6234 +struct au_pin;
6235 +
6236 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6237 +void au_cpup_attr_timesizes(struct inode *inode);
6238 +void au_cpup_attr_nlink(struct inode *inode, int force);
6239 +void au_cpup_attr_changeable(struct inode *inode);
6240 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6241 +void au_cpup_attr_all(struct inode *inode, int force);
6242 +
6243 +/* ---------------------------------------------------------------------- */
6244 +
6245 +struct au_cp_generic {
6246 +       struct dentry   *dentry;
6247 +       aufs_bindex_t   bdst, bsrc;
6248 +       loff_t          len;
6249 +       struct au_pin   *pin;
6250 +       unsigned int    flags;
6251 +};
6252 +
6253 +/* cpup flags */
6254 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6255 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6256 +                                                  for link(2) */
6257 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6258 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6259 +                                                  cpup */
6260 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6261 +                                                  existing entry */
6262 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6263 +                                                  the branch is marked as RO */
6264 +
6265 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6266 +#undef AuCpup_HOPEN
6267 +#define AuCpup_HOPEN           0
6268 +#endif
6269 +
6270 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6271 +#define au_fset_cpup(flags, name) \
6272 +       do { (flags) |= AuCpup_##name; } while (0)
6273 +#define au_fclr_cpup(flags, name) \
6274 +       do { (flags) &= ~AuCpup_##name; } while (0)
6275 +
6276 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6277 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6278 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6279 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6280 +
6281 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6282 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6283 +                        struct au_pin *pin,
6284 +                        struct dentry *h_parent, void *arg),
6285 +              void *arg);
6286 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6287 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6288 +
6289 +/* ---------------------------------------------------------------------- */
6290 +
6291 +/* keep timestamps when copyup */
6292 +struct au_dtime {
6293 +       struct dentry *dt_dentry;
6294 +       struct path dt_h_path;
6295 +       struct timespec64 dt_atime, dt_mtime;
6296 +};
6297 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6298 +                   struct path *h_path);
6299 +void au_dtime_revert(struct au_dtime *dt);
6300 +
6301 +#endif /* __KERNEL__ */
6302 +#endif /* __AUFS_CPUP_H__ */
6303 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6304 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6305 +++ linux/fs/aufs/dbgaufs.c     2021-12-03 15:38:59.933313976 +0100
6306 @@ -0,0 +1,526 @@
6307 +// SPDX-License-Identifier: GPL-2.0
6308 +/*
6309 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6310 + *
6311 + * This program, aufs is free software; you can redistribute it and/or modify
6312 + * it under the terms of the GNU General Public License as published by
6313 + * the Free Software Foundation; either version 2 of the License, or
6314 + * (at your option) any later version.
6315 + *
6316 + * This program is distributed in the hope that it will be useful,
6317 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6318 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6319 + * GNU General Public License for more details.
6320 + *
6321 + * You should have received a copy of the GNU General Public License
6322 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6323 + */
6324 +
6325 +/*
6326 + * debugfs interface
6327 + */
6328 +
6329 +#include <linux/debugfs.h>
6330 +#include "aufs.h"
6331 +
6332 +#ifndef CONFIG_SYSFS
6333 +#error DEBUG_FS depends upon SYSFS
6334 +#endif
6335 +
6336 +static struct dentry *dbgaufs;
6337 +static const mode_t dbgaufs_mode = 0444;
6338 +
6339 +/* 20 is max digits length of ulong 64 */
6340 +struct dbgaufs_arg {
6341 +       int n;
6342 +       char a[20 * 4];
6343 +};
6344 +
6345 +/*
6346 + * common function for all XINO files
6347 + */
6348 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6349 +                             struct file *file)
6350 +{
6351 +       void *p;
6352 +
6353 +       p = file->private_data;
6354 +       if (p) {
6355 +               /* this is struct dbgaufs_arg */
6356 +               AuDebugOn(!au_kfree_sz_test(p));
6357 +               au_kfree_do_rcu(p);
6358 +       }
6359 +       return 0;
6360 +}
6361 +
6362 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6363 +                          int cnt)
6364 +{
6365 +       int err;
6366 +       struct kstat st;
6367 +       struct dbgaufs_arg *p;
6368 +
6369 +       err = -ENOMEM;
6370 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6371 +       if (unlikely(!p))
6372 +               goto out;
6373 +
6374 +       err = 0;
6375 +       p->n = 0;
6376 +       file->private_data = p;
6377 +       if (!xf)
6378 +               goto out;
6379 +
6380 +       err = vfsub_getattr(&xf->f_path, &st);
6381 +       if (!err) {
6382 +               if (do_fcnt)
6383 +                       p->n = snprintf
6384 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6385 +                                cnt, st.blocks, st.blksize,
6386 +                                (long long)st.size);
6387 +               else
6388 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6389 +                                       st.blocks, st.blksize,
6390 +                                       (long long)st.size);
6391 +               AuDebugOn(p->n >= sizeof(p->a));
6392 +       } else {
6393 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6394 +               err = 0;
6395 +       }
6396 +
6397 +out:
6398 +       return err;
6399 +}
6400 +
6401 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6402 +                              size_t count, loff_t *ppos)
6403 +{
6404 +       struct dbgaufs_arg *p;
6405 +
6406 +       p = file->private_data;
6407 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6408 +}
6409 +
6410 +/* ---------------------------------------------------------------------- */
6411 +
6412 +struct dbgaufs_plink_arg {
6413 +       int n;
6414 +       char a[];
6415 +};
6416 +
6417 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6418 +                                struct file *file)
6419 +{
6420 +       free_page((unsigned long)file->private_data);
6421 +       return 0;
6422 +}
6423 +
6424 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6425 +{
6426 +       int err, i, limit;
6427 +       unsigned long n, sum;
6428 +       struct dbgaufs_plink_arg *p;
6429 +       struct au_sbinfo *sbinfo;
6430 +       struct super_block *sb;
6431 +       struct hlist_bl_head *hbl;
6432 +
6433 +       err = -ENOMEM;
6434 +       p = (void *)get_zeroed_page(GFP_NOFS);
6435 +       if (unlikely(!p))
6436 +               goto out;
6437 +
6438 +       err = -EFBIG;
6439 +       sbinfo = inode->i_private;
6440 +       sb = sbinfo->si_sb;
6441 +       si_noflush_read_lock(sb);
6442 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6443 +               limit = PAGE_SIZE - sizeof(p->n);
6444 +
6445 +               /* the number of buckets */
6446 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6447 +               p->n += n;
6448 +               limit -= n;
6449 +
6450 +               sum = 0;
6451 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6452 +                    i++, hbl++) {
6453 +                       n = au_hbl_count(hbl);
6454 +                       sum += n;
6455 +
6456 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6457 +                       p->n += n;
6458 +                       limit -= n;
6459 +                       if (unlikely(limit <= 0))
6460 +                               goto out_free;
6461 +               }
6462 +               p->a[p->n - 1] = '\n';
6463 +
6464 +               /* the sum of plinks */
6465 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6466 +               p->n += n;
6467 +               limit -= n;
6468 +               if (unlikely(limit <= 0))
6469 +                       goto out_free;
6470 +       } else {
6471 +#define str "1\n0\n0\n"
6472 +               p->n = sizeof(str) - 1;
6473 +               strcpy(p->a, str);
6474 +#undef str
6475 +       }
6476 +       si_read_unlock(sb);
6477 +
6478 +       err = 0;
6479 +       file->private_data = p;
6480 +       goto out; /* success */
6481 +
6482 +out_free:
6483 +       free_page((unsigned long)p);
6484 +out:
6485 +       return err;
6486 +}
6487 +
6488 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6489 +                                 size_t count, loff_t *ppos)
6490 +{
6491 +       struct dbgaufs_plink_arg *p;
6492 +
6493 +       p = file->private_data;
6494 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6495 +}
6496 +
6497 +static const struct file_operations dbgaufs_plink_fop = {
6498 +       .owner          = THIS_MODULE,
6499 +       .open           = dbgaufs_plink_open,
6500 +       .release        = dbgaufs_plink_release,
6501 +       .read           = dbgaufs_plink_read
6502 +};
6503 +
6504 +/* ---------------------------------------------------------------------- */
6505 +
6506 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6507 +{
6508 +       int err;
6509 +       struct au_sbinfo *sbinfo;
6510 +       struct super_block *sb;
6511 +
6512 +       sbinfo = inode->i_private;
6513 +       sb = sbinfo->si_sb;
6514 +       si_noflush_read_lock(sb);
6515 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6516 +       si_read_unlock(sb);
6517 +       return err;
6518 +}
6519 +
6520 +static const struct file_operations dbgaufs_xib_fop = {
6521 +       .owner          = THIS_MODULE,
6522 +       .open           = dbgaufs_xib_open,
6523 +       .release        = dbgaufs_xi_release,
6524 +       .read           = dbgaufs_xi_read
6525 +};
6526 +
6527 +/* ---------------------------------------------------------------------- */
6528 +
6529 +#define DbgaufsXi_PREFIX "xi"
6530 +
6531 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6532 +{
6533 +       int err, idx;
6534 +       long l;
6535 +       aufs_bindex_t bindex;
6536 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6537 +       struct au_sbinfo *sbinfo;
6538 +       struct super_block *sb;
6539 +       struct au_xino *xi;
6540 +       struct file *xf;
6541 +       struct qstr *name;
6542 +       struct au_branch *br;
6543 +
6544 +       err = -ENOENT;
6545 +       name = &file->f_path.dentry->d_name;
6546 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6547 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6548 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6549 +               goto out;
6550 +
6551 +       AuDebugOn(name->len >= sizeof(a));
6552 +       memcpy(a, name->name, name->len);
6553 +       a[name->len] = '\0';
6554 +       p = strchr(a, '-');
6555 +       if (p)
6556 +               *p = '\0';
6557 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6558 +       if (unlikely(err))
6559 +               goto out;
6560 +       bindex = l;
6561 +       idx = 0;
6562 +       if (p) {
6563 +               err = kstrtol(p + 1, 10, &l);
6564 +               if (unlikely(err))
6565 +                       goto out;
6566 +               idx = l;
6567 +       }
6568 +
6569 +       err = -ENOENT;
6570 +       sbinfo = inode->i_private;
6571 +       sb = sbinfo->si_sb;
6572 +       si_noflush_read_lock(sb);
6573 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6574 +               goto out_si;
6575 +       br = au_sbr(sb, bindex);
6576 +       xi = br->br_xino;
6577 +       if (unlikely(idx >= xi->xi_nfile))
6578 +               goto out_si;
6579 +       xf = au_xino_file(xi, idx);
6580 +       if (xf)
6581 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6582 +                                     au_xino_count(br));
6583 +
6584 +out_si:
6585 +       si_read_unlock(sb);
6586 +out:
6587 +       AuTraceErr(err);
6588 +       return err;
6589 +}
6590 +
6591 +static const struct file_operations dbgaufs_xino_fop = {
6592 +       .owner          = THIS_MODULE,
6593 +       .open           = dbgaufs_xino_open,
6594 +       .release        = dbgaufs_xi_release,
6595 +       .read           = dbgaufs_xi_read
6596 +};
6597 +
6598 +void dbgaufs_xino_del(struct au_branch *br)
6599 +{
6600 +       struct dentry *dbgaufs;
6601 +
6602 +       dbgaufs = br->br_dbgaufs;
6603 +       if (!dbgaufs)
6604 +               return;
6605 +
6606 +       br->br_dbgaufs = NULL;
6607 +       /* debugfs acquires the parent i_mutex */
6608 +       lockdep_off();
6609 +       debugfs_remove(dbgaufs);
6610 +       lockdep_on();
6611 +}
6612 +
6613 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6614 +{
6615 +       aufs_bindex_t bbot;
6616 +       struct au_branch *br;
6617 +
6618 +       if (!au_sbi(sb)->si_dbgaufs)
6619 +               return;
6620 +
6621 +       bbot = au_sbbot(sb);
6622 +       for (; bindex <= bbot; bindex++) {
6623 +               br = au_sbr(sb, bindex);
6624 +               dbgaufs_xino_del(br);
6625 +       }
6626 +}
6627 +
6628 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6629 +                             unsigned int idx, struct dentry *parent,
6630 +                             struct au_sbinfo *sbinfo)
6631 +{
6632 +       struct au_branch *br;
6633 +       struct dentry *d;
6634 +       /* "xi" bindex(5) "-" idx(2) NULL */
6635 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6636 +
6637 +       if (!idx)
6638 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6639 +       else
6640 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6641 +                        bindex, idx);
6642 +       br = au_sbr(sb, bindex);
6643 +       if (br->br_dbgaufs) {
6644 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6645 +
6646 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6647 +                       /* debugfs acquires the parent i_mutex */
6648 +                       lockdep_off();
6649 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6650 +                                          name);
6651 +                       lockdep_on();
6652 +                       if (unlikely(!d))
6653 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6654 +                                       parent, name);
6655 +               }
6656 +       } else {
6657 +               lockdep_off();
6658 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6659 +                                                    sbinfo, &dbgaufs_xino_fop);
6660 +               lockdep_on();
6661 +               if (unlikely(!br->br_dbgaufs))
6662 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6663 +                               parent, name);
6664 +       }
6665 +}
6666 +
6667 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6668 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6669 +{
6670 +       struct au_branch *br;
6671 +       struct au_xino *xi;
6672 +       unsigned int u;
6673 +
6674 +       br = au_sbr(sb, bindex);
6675 +       xi = br->br_xino;
6676 +       for (u = 0; u < xi->xi_nfile; u++)
6677 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6678 +}
6679 +
6680 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6681 +{
6682 +       struct au_sbinfo *sbinfo;
6683 +       struct dentry *parent;
6684 +       aufs_bindex_t bbot;
6685 +
6686 +       if (!au_opt_test(au_mntflags(sb), XINO))
6687 +               return;
6688 +
6689 +       sbinfo = au_sbi(sb);
6690 +       parent = sbinfo->si_dbgaufs;
6691 +       if (!parent)
6692 +               return;
6693 +
6694 +       bbot = au_sbbot(sb);
6695 +       if (topdown)
6696 +               for (; bindex <= bbot; bindex++)
6697 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6698 +       else
6699 +               for (; bbot >= bindex; bbot--)
6700 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6701 +}
6702 +
6703 +/* ---------------------------------------------------------------------- */
6704 +
6705 +#ifdef CONFIG_AUFS_EXPORT
6706 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6707 +{
6708 +       int err;
6709 +       struct au_sbinfo *sbinfo;
6710 +       struct super_block *sb;
6711 +
6712 +       sbinfo = inode->i_private;
6713 +       sb = sbinfo->si_sb;
6714 +       si_noflush_read_lock(sb);
6715 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6716 +       si_read_unlock(sb);
6717 +       return err;
6718 +}
6719 +
6720 +static const struct file_operations dbgaufs_xigen_fop = {
6721 +       .owner          = THIS_MODULE,
6722 +       .open           = dbgaufs_xigen_open,
6723 +       .release        = dbgaufs_xi_release,
6724 +       .read           = dbgaufs_xi_read
6725 +};
6726 +
6727 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6728 +{
6729 +       int err;
6730 +
6731 +       /*
6732 +        * This function is a dynamic '__init' function actually,
6733 +        * so the tiny check for si_rwsem is unnecessary.
6734 +        */
6735 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6736 +
6737 +       err = -EIO;
6738 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6739 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6740 +                &dbgaufs_xigen_fop);
6741 +       if (sbinfo->si_dbgaufs_xigen)
6742 +               err = 0;
6743 +
6744 +       return err;
6745 +}
6746 +#else
6747 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6748 +{
6749 +       return 0;
6750 +}
6751 +#endif /* CONFIG_AUFS_EXPORT */
6752 +
6753 +/* ---------------------------------------------------------------------- */
6754 +
6755 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6756 +{
6757 +       /*
6758 +        * This function is a dynamic '__fin' function actually,
6759 +        * so the tiny check for si_rwsem is unnecessary.
6760 +        */
6761 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6762 +
6763 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6764 +       sbinfo->si_dbgaufs = NULL;
6765 +}
6766 +
6767 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6768 +{
6769 +       int err;
6770 +       char name[SysaufsSiNameLen];
6771 +
6772 +       /*
6773 +        * This function is a dynamic '__init' function actually,
6774 +        * so the tiny check for si_rwsem is unnecessary.
6775 +        */
6776 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6777 +
6778 +       err = -ENOENT;
6779 +       if (!dbgaufs) {
6780 +               AuErr1("/debug/aufs is uninitialized\n");
6781 +               goto out;
6782 +       }
6783 +
6784 +       err = -EIO;
6785 +       sysaufs_name(sbinfo, name);
6786 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6787 +       if (unlikely(!sbinfo->si_dbgaufs))
6788 +               goto out;
6789 +
6790 +       /* regardless plink/noplink option */
6791 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6792 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6793 +                &dbgaufs_plink_fop);
6794 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6795 +               goto out_dir;
6796 +
6797 +       /* regardless xino/noxino option */
6798 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6799 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6800 +                &dbgaufs_xib_fop);
6801 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6802 +               goto out_dir;
6803 +
6804 +       err = dbgaufs_xigen_init(sbinfo);
6805 +       if (!err)
6806 +               goto out; /* success */
6807 +
6808 +out_dir:
6809 +       dbgaufs_si_fin(sbinfo);
6810 +out:
6811 +       if (unlikely(err))
6812 +               pr_err("debugfs/aufs failed\n");
6813 +       return err;
6814 +}
6815 +
6816 +/* ---------------------------------------------------------------------- */
6817 +
6818 +void dbgaufs_fin(void)
6819 +{
6820 +       debugfs_remove(dbgaufs);
6821 +}
6822 +
6823 +int __init dbgaufs_init(void)
6824 +{
6825 +       int err;
6826 +
6827 +       err = -EIO;
6828 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6829 +       if (dbgaufs)
6830 +               err = 0;
6831 +       return err;
6832 +}
6833 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6834 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6835 +++ linux/fs/aufs/dbgaufs.h     2021-12-03 15:38:59.933313976 +0100
6836 @@ -0,0 +1,53 @@
6837 +/* SPDX-License-Identifier: GPL-2.0 */
6838 +/*
6839 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6840 + *
6841 + * This program, aufs is free software; you can redistribute it and/or modify
6842 + * it under the terms of the GNU General Public License as published by
6843 + * the Free Software Foundation; either version 2 of the License, or
6844 + * (at your option) any later version.
6845 + *
6846 + * This program is distributed in the hope that it will be useful,
6847 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6848 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6849 + * GNU General Public License for more details.
6850 + *
6851 + * You should have received a copy of the GNU General Public License
6852 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6853 + */
6854 +
6855 +/*
6856 + * debugfs interface
6857 + */
6858 +
6859 +#ifndef __DBGAUFS_H__
6860 +#define __DBGAUFS_H__
6861 +
6862 +#ifdef __KERNEL__
6863 +
6864 +struct super_block;
6865 +struct au_sbinfo;
6866 +struct au_branch;
6867 +
6868 +#ifdef CONFIG_DEBUG_FS
6869 +/* dbgaufs.c */
6870 +void dbgaufs_xino_del(struct au_branch *br);
6871 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6872 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6873 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6874 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6875 +void dbgaufs_fin(void);
6876 +int __init dbgaufs_init(void);
6877 +#else
6878 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6879 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6880 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6881 +          int topdown)
6882 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6883 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6884 +AuStubVoid(dbgaufs_fin, void)
6885 +AuStubInt0(__init dbgaufs_init, void)
6886 +#endif /* CONFIG_DEBUG_FS */
6887 +
6888 +#endif /* __KERNEL__ */
6889 +#endif /* __DBGAUFS_H__ */
6890 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6891 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6892 +++ linux/fs/aufs/dcsub.c       2021-12-03 15:38:59.933313976 +0100
6893 @@ -0,0 +1,225 @@
6894 +// SPDX-License-Identifier: GPL-2.0
6895 +/*
6896 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6897 + *
6898 + * This program, aufs is free software; you can redistribute it and/or modify
6899 + * it under the terms of the GNU General Public License as published by
6900 + * the Free Software Foundation; either version 2 of the License, or
6901 + * (at your option) any later version.
6902 + *
6903 + * This program is distributed in the hope that it will be useful,
6904 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6905 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6906 + * GNU General Public License for more details.
6907 + *
6908 + * You should have received a copy of the GNU General Public License
6909 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6910 + */
6911 +
6912 +/*
6913 + * sub-routines for dentry cache
6914 + */
6915 +
6916 +#include "aufs.h"
6917 +
6918 +static void au_dpage_free(struct au_dpage *dpage)
6919 +{
6920 +       int i;
6921 +       struct dentry **p;
6922 +
6923 +       p = dpage->dentries;
6924 +       for (i = 0; i < dpage->ndentry; i++)
6925 +               dput(*p++);
6926 +       free_page((unsigned long)dpage->dentries);
6927 +}
6928 +
6929 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6930 +{
6931 +       int err;
6932 +       void *p;
6933 +
6934 +       err = -ENOMEM;
6935 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6936 +       if (unlikely(!dpages->dpages))
6937 +               goto out;
6938 +
6939 +       p = (void *)__get_free_page(gfp);
6940 +       if (unlikely(!p))
6941 +               goto out_dpages;
6942 +
6943 +       dpages->dpages[0].ndentry = 0;
6944 +       dpages->dpages[0].dentries = p;
6945 +       dpages->ndpage = 1;
6946 +       return 0; /* success */
6947 +
6948 +out_dpages:
6949 +       au_kfree_try_rcu(dpages->dpages);
6950 +out:
6951 +       return err;
6952 +}
6953 +
6954 +void au_dpages_free(struct au_dcsub_pages *dpages)
6955 +{
6956 +       int i;
6957 +       struct au_dpage *p;
6958 +
6959 +       p = dpages->dpages;
6960 +       for (i = 0; i < dpages->ndpage; i++)
6961 +               au_dpage_free(p++);
6962 +       au_kfree_try_rcu(dpages->dpages);
6963 +}
6964 +
6965 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6966 +                           struct dentry *dentry, gfp_t gfp)
6967 +{
6968 +       int err, sz;
6969 +       struct au_dpage *dpage;
6970 +       void *p;
6971 +
6972 +       dpage = dpages->dpages + dpages->ndpage - 1;
6973 +       sz = PAGE_SIZE / sizeof(dentry);
6974 +       if (unlikely(dpage->ndentry >= sz)) {
6975 +               AuLabel(new dpage);
6976 +               err = -ENOMEM;
6977 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6978 +               p = au_kzrealloc(dpages->dpages, sz,
6979 +                                sz + sizeof(*dpages->dpages), gfp,
6980 +                                /*may_shrink*/0);
6981 +               if (unlikely(!p))
6982 +                       goto out;
6983 +
6984 +               dpages->dpages = p;
6985 +               dpage = dpages->dpages + dpages->ndpage;
6986 +               p = (void *)__get_free_page(gfp);
6987 +               if (unlikely(!p))
6988 +                       goto out;
6989 +
6990 +               dpage->ndentry = 0;
6991 +               dpage->dentries = p;
6992 +               dpages->ndpage++;
6993 +       }
6994 +
6995 +       AuDebugOn(au_dcount(dentry) <= 0);
6996 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6997 +       return 0; /* success */
6998 +
6999 +out:
7000 +       return err;
7001 +}
7002 +
7003 +/* todo: BAD approach */
7004 +/* copied from linux/fs/dcache.c */
7005 +enum d_walk_ret {
7006 +       D_WALK_CONTINUE,
7007 +       D_WALK_QUIT,
7008 +       D_WALK_NORETRY,
7009 +       D_WALK_SKIP,
7010 +};
7011 +
7012 +extern void d_walk(struct dentry *parent, void *data,
7013 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7014 +
7015 +struct ac_dpages_arg {
7016 +       int err;
7017 +       struct au_dcsub_pages *dpages;
7018 +       struct super_block *sb;
7019 +       au_dpages_test test;
7020 +       void *arg;
7021 +};
7022 +
7023 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7024 +{
7025 +       enum d_walk_ret ret;
7026 +       struct ac_dpages_arg *arg = _arg;
7027 +
7028 +       ret = D_WALK_CONTINUE;
7029 +       if (dentry->d_sb == arg->sb
7030 +           && !IS_ROOT(dentry)
7031 +           && au_dcount(dentry) > 0
7032 +           && au_di(dentry)
7033 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7034 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7035 +               if (unlikely(arg->err))
7036 +                       ret = D_WALK_QUIT;
7037 +       }
7038 +
7039 +       return ret;
7040 +}
7041 +
7042 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7043 +                  au_dpages_test test, void *arg)
7044 +{
7045 +       struct ac_dpages_arg args = {
7046 +               .err    = 0,
7047 +               .dpages = dpages,
7048 +               .sb     = root->d_sb,
7049 +               .test   = test,
7050 +               .arg    = arg
7051 +       };
7052 +
7053 +       d_walk(root, &args, au_call_dpages_append);
7054 +
7055 +       return args.err;
7056 +}
7057 +
7058 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7059 +                      int do_include, au_dpages_test test, void *arg)
7060 +{
7061 +       int err;
7062 +
7063 +       err = 0;
7064 +       write_seqlock(&rename_lock);
7065 +       spin_lock(&dentry->d_lock);
7066 +       if (do_include
7067 +           && au_dcount(dentry) > 0
7068 +           && (!test || test(dentry, arg)))
7069 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7070 +       spin_unlock(&dentry->d_lock);
7071 +       if (unlikely(err))
7072 +               goto out;
7073 +
7074 +       /*
7075 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7076 +        * mount
7077 +        */
7078 +       while (!IS_ROOT(dentry)) {
7079 +               dentry = dentry->d_parent; /* rename_lock is locked */
7080 +               spin_lock(&dentry->d_lock);
7081 +               if (au_dcount(dentry) > 0
7082 +                   && (!test || test(dentry, arg)))
7083 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7084 +               spin_unlock(&dentry->d_lock);
7085 +               if (unlikely(err))
7086 +                       break;
7087 +       }
7088 +
7089 +out:
7090 +       write_sequnlock(&rename_lock);
7091 +       return err;
7092 +}
7093 +
7094 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7095 +{
7096 +       return au_di(dentry) && dentry->d_sb == arg;
7097 +}
7098 +
7099 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7100 +                           struct dentry *dentry, int do_include)
7101 +{
7102 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7103 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7104 +}
7105 +
7106 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7107 +{
7108 +       struct path path[2] = {
7109 +               {
7110 +                       .dentry = d1
7111 +               },
7112 +               {
7113 +                       .dentry = d2
7114 +               }
7115 +       };
7116 +
7117 +       return path_is_under(path + 0, path + 1);
7118 +}
7119 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7120 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7121 +++ linux/fs/aufs/dcsub.h       2021-12-03 15:38:59.933313976 +0100
7122 @@ -0,0 +1,137 @@
7123 +/* SPDX-License-Identifier: GPL-2.0 */
7124 +/*
7125 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7126 + *
7127 + * This program, aufs is free software; you can redistribute it and/or modify
7128 + * it under the terms of the GNU General Public License as published by
7129 + * the Free Software Foundation; either version 2 of the License, or
7130 + * (at your option) any later version.
7131 + *
7132 + * This program is distributed in the hope that it will be useful,
7133 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7134 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7135 + * GNU General Public License for more details.
7136 + *
7137 + * You should have received a copy of the GNU General Public License
7138 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7139 + */
7140 +
7141 +/*
7142 + * sub-routines for dentry cache
7143 + */
7144 +
7145 +#ifndef __AUFS_DCSUB_H__
7146 +#define __AUFS_DCSUB_H__
7147 +
7148 +#ifdef __KERNEL__
7149 +
7150 +#include <linux/dcache.h>
7151 +#include <linux/fs.h>
7152 +
7153 +struct au_dpage {
7154 +       int ndentry;
7155 +       struct dentry **dentries;
7156 +};
7157 +
7158 +struct au_dcsub_pages {
7159 +       int ndpage;
7160 +       struct au_dpage *dpages;
7161 +};
7162 +
7163 +/* ---------------------------------------------------------------------- */
7164 +
7165 +/* dcsub.c */
7166 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7167 +void au_dpages_free(struct au_dcsub_pages *dpages);
7168 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7169 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7170 +                  au_dpages_test test, void *arg);
7171 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7172 +                      int do_include, au_dpages_test test, void *arg);
7173 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7174 +                           struct dentry *dentry, int do_include);
7175 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7176 +
7177 +/* ---------------------------------------------------------------------- */
7178 +
7179 +/*
7180 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7181 + * include/linux/dcache.h. Try them (in the future).
7182 + */
7183 +
7184 +static inline int au_d_hashed_positive(struct dentry *d)
7185 +{
7186 +       int err;
7187 +       struct inode *inode = d_inode(d);
7188 +
7189 +       err = 0;
7190 +       if (unlikely(d_unhashed(d)
7191 +                    || d_is_negative(d)
7192 +                    || !inode->i_nlink))
7193 +               err = -ENOENT;
7194 +       return err;
7195 +}
7196 +
7197 +static inline int au_d_linkable(struct dentry *d)
7198 +{
7199 +       int err;
7200 +       struct inode *inode = d_inode(d);
7201 +
7202 +       err = au_d_hashed_positive(d);
7203 +       if (err
7204 +           && d_is_positive(d)
7205 +           && (inode->i_state & I_LINKABLE))
7206 +               err = 0;
7207 +       return err;
7208 +}
7209 +
7210 +static inline int au_d_alive(struct dentry *d)
7211 +{
7212 +       int err;
7213 +       struct inode *inode;
7214 +
7215 +       err = 0;
7216 +       if (!IS_ROOT(d))
7217 +               err = au_d_hashed_positive(d);
7218 +       else {
7219 +               inode = d_inode(d);
7220 +               if (unlikely(d_unlinked(d)
7221 +                            || d_is_negative(d)
7222 +                            || !inode->i_nlink))
7223 +                       err = -ENOENT;
7224 +       }
7225 +       return err;
7226 +}
7227 +
7228 +static inline int au_alive_dir(struct dentry *d)
7229 +{
7230 +       int err;
7231 +
7232 +       err = au_d_alive(d);
7233 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7234 +               err = -ENOENT;
7235 +       return err;
7236 +}
7237 +
7238 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7239 +{
7240 +       return a->len == b->len
7241 +               && !memcmp(a->name, b->name, a->len);
7242 +}
7243 +
7244 +/*
7245 + * by the commit
7246 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7247 + *                     taking d_lock
7248 + * the type of d_lockref.count became int, but the inlined function d_count()
7249 + * still returns unsigned int.
7250 + * I don't know why. Maybe it is for every d_count() users?
7251 + * Anyway au_dcount() lives on.
7252 + */
7253 +static inline int au_dcount(struct dentry *d)
7254 +{
7255 +       return (int)d_count(d);
7256 +}
7257 +
7258 +#endif /* __KERNEL__ */
7259 +#endif /* __AUFS_DCSUB_H__ */
7260 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7261 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7262 +++ linux/fs/aufs/debug.c       2021-12-03 15:38:59.933313976 +0100
7263 @@ -0,0 +1,444 @@
7264 +// SPDX-License-Identifier: GPL-2.0
7265 +/*
7266 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7267 + *
7268 + * This program, aufs is free software; you can redistribute it and/or modify
7269 + * it under the terms of the GNU General Public License as published by
7270 + * the Free Software Foundation; either version 2 of the License, or
7271 + * (at your option) any later version.
7272 + *
7273 + * This program is distributed in the hope that it will be useful,
7274 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7275 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7276 + * GNU General Public License for more details.
7277 + *
7278 + * You should have received a copy of the GNU General Public License
7279 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7280 + */
7281 +
7282 +/*
7283 + * debug print functions
7284 + */
7285 +
7286 +#include <linux/iversion.h>
7287 +#include "aufs.h"
7288 +
7289 +/* Returns 0, or -errno.  arg is in kp->arg. */
7290 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7291 +{
7292 +       int err, n;
7293 +
7294 +       err = kstrtoint(val, 0, &n);
7295 +       if (!err) {
7296 +               if (n > 0)
7297 +                       au_debug_on();
7298 +               else
7299 +                       au_debug_off();
7300 +       }
7301 +       return err;
7302 +}
7303 +
7304 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7305 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7306 +{
7307 +       atomic_t *a;
7308 +
7309 +       a = kp->arg;
7310 +       return sprintf(buffer, "%d", atomic_read(a));
7311 +}
7312 +
7313 +static struct kernel_param_ops param_ops_atomic_t = {
7314 +       .set = param_atomic_t_set,
7315 +       .get = param_atomic_t_get
7316 +       /* void (*free)(void *arg) */
7317 +};
7318 +
7319 +atomic_t aufs_debug = ATOMIC_INIT(0);
7320 +MODULE_PARM_DESC(debug, "debug print");
7321 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7322 +
7323 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7324 +char *au_plevel = KERN_DEBUG;
7325 +#define dpri(fmt, ...) do {                                    \
7326 +       if ((au_plevel                                          \
7327 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7328 +           || au_debug_test())                                 \
7329 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7330 +} while (0)
7331 +
7332 +/* ---------------------------------------------------------------------- */
7333 +
7334 +void au_dpri_whlist(struct au_nhash *whlist)
7335 +{
7336 +       unsigned long ul, n;
7337 +       struct hlist_head *head;
7338 +       struct au_vdir_wh *pos;
7339 +
7340 +       n = whlist->nh_num;
7341 +       head = whlist->nh_head;
7342 +       for (ul = 0; ul < n; ul++) {
7343 +               hlist_for_each_entry(pos, head, wh_hash)
7344 +                       dpri("b%d, %.*s, %d\n",
7345 +                            pos->wh_bindex,
7346 +                            pos->wh_str.len, pos->wh_str.name,
7347 +                            pos->wh_str.len);
7348 +               head++;
7349 +       }
7350 +}
7351 +
7352 +void au_dpri_vdir(struct au_vdir *vdir)
7353 +{
7354 +       unsigned long ul;
7355 +       union au_vdir_deblk_p p;
7356 +       unsigned char *o;
7357 +
7358 +       if (!vdir || IS_ERR(vdir)) {
7359 +               dpri("err %ld\n", PTR_ERR(vdir));
7360 +               return;
7361 +       }
7362 +
7363 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7364 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7365 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7366 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7367 +               p.deblk = vdir->vd_deblk[ul];
7368 +               o = p.deblk;
7369 +               dpri("[%lu]: %p\n", ul, o);
7370 +       }
7371 +}
7372 +
7373 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7374 +                       struct dentry *wh)
7375 +{
7376 +       char *n = NULL;
7377 +       int l = 0;
7378 +
7379 +       if (!inode || IS_ERR(inode)) {
7380 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7381 +               return -1;
7382 +       }
7383 +
7384 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7385 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7386 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7387 +       if (wh) {
7388 +               n = (void *)wh->d_name.name;
7389 +               l = wh->d_name.len;
7390 +       }
7391 +
7392 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7393 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7394 +            bindex, inode,
7395 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7396 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7397 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7398 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7399 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7400 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7401 +            inode->i_generation,
7402 +            l ? ", wh " : "", l, n);
7403 +       return 0;
7404 +}
7405 +
7406 +void au_dpri_inode(struct inode *inode)
7407 +{
7408 +       struct au_iinfo *iinfo;
7409 +       struct au_hinode *hi;
7410 +       aufs_bindex_t bindex;
7411 +       int err, hn;
7412 +
7413 +       err = do_pri_inode(-1, inode, -1, NULL);
7414 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7415 +               return;
7416 +
7417 +       iinfo = au_ii(inode);
7418 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7419 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7420 +       if (iinfo->ii_btop < 0)
7421 +               return;
7422 +       hn = 0;
7423 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7424 +               hi = au_hinode(iinfo, bindex);
7425 +               hn = !!au_hn(hi);
7426 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7427 +       }
7428 +}
7429 +
7430 +void au_dpri_dalias(struct inode *inode)
7431 +{
7432 +       struct dentry *d;
7433 +
7434 +       spin_lock(&inode->i_lock);
7435 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7436 +               au_dpri_dentry(d);
7437 +       spin_unlock(&inode->i_lock);
7438 +}
7439 +
7440 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7441 +{
7442 +       struct dentry *wh = NULL;
7443 +       int hn;
7444 +       struct inode *inode;
7445 +       struct au_iinfo *iinfo;
7446 +       struct au_hinode *hi;
7447 +
7448 +       if (!dentry || IS_ERR(dentry)) {
7449 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7450 +               return -1;
7451 +       }
7452 +       /* do not call dget_parent() here */
7453 +       /* note: access d_xxx without d_lock */
7454 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7455 +            bindex, dentry, dentry,
7456 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7457 +            au_dcount(dentry), dentry->d_flags,
7458 +            d_unhashed(dentry) ? "un" : "");
7459 +       hn = -1;
7460 +       inode = NULL;
7461 +       if (d_is_positive(dentry))
7462 +               inode = d_inode(dentry);
7463 +       if (inode
7464 +           && au_test_aufs(dentry->d_sb)
7465 +           && bindex >= 0
7466 +           && !au_is_bad_inode(inode)) {
7467 +               iinfo = au_ii(inode);
7468 +               hi = au_hinode(iinfo, bindex);
7469 +               hn = !!au_hn(hi);
7470 +               wh = hi->hi_whdentry;
7471 +       }
7472 +       do_pri_inode(bindex, inode, hn, wh);
7473 +       return 0;
7474 +}
7475 +
7476 +void au_dpri_dentry(struct dentry *dentry)
7477 +{
7478 +       struct au_dinfo *dinfo;
7479 +       aufs_bindex_t bindex;
7480 +       int err;
7481 +
7482 +       err = do_pri_dentry(-1, dentry);
7483 +       if (err || !au_test_aufs(dentry->d_sb))
7484 +               return;
7485 +
7486 +       dinfo = au_di(dentry);
7487 +       if (!dinfo)
7488 +               return;
7489 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7490 +            dinfo->di_btop, dinfo->di_bbot,
7491 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7492 +            dinfo->di_tmpfile);
7493 +       if (dinfo->di_btop < 0)
7494 +               return;
7495 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7496 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7497 +}
7498 +
7499 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7500 +{
7501 +       char a[32];
7502 +
7503 +       if (!file || IS_ERR(file)) {
7504 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7505 +               return -1;
7506 +       }
7507 +       a[0] = 0;
7508 +       if (bindex < 0
7509 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7510 +           && au_test_aufs(file->f_path.dentry->d_sb)
7511 +           && au_fi(file))
7512 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7513 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7514 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7515 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7516 +            file->f_version, file->f_pos, a);
7517 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7518 +               do_pri_dentry(bindex, file->f_path.dentry);
7519 +       return 0;
7520 +}
7521 +
7522 +void au_dpri_file(struct file *file)
7523 +{
7524 +       struct au_finfo *finfo;
7525 +       struct au_fidir *fidir;
7526 +       struct au_hfile *hfile;
7527 +       aufs_bindex_t bindex;
7528 +       int err;
7529 +
7530 +       err = do_pri_file(-1, file);
7531 +       if (err
7532 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7533 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7534 +               return;
7535 +
7536 +       finfo = au_fi(file);
7537 +       if (!finfo)
7538 +               return;
7539 +       if (finfo->fi_btop < 0)
7540 +               return;
7541 +       fidir = finfo->fi_hdir;
7542 +       if (!fidir)
7543 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7544 +       else
7545 +               for (bindex = finfo->fi_btop;
7546 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7547 +                    bindex++) {
7548 +                       hfile = fidir->fd_hfile + bindex;
7549 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7550 +               }
7551 +}
7552 +
7553 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7554 +{
7555 +       struct vfsmount *mnt;
7556 +       struct super_block *sb;
7557 +
7558 +       if (!br || IS_ERR(br))
7559 +               goto out;
7560 +       mnt = au_br_mnt(br);
7561 +       if (!mnt || IS_ERR(mnt))
7562 +               goto out;
7563 +       sb = mnt->mnt_sb;
7564 +       if (!sb || IS_ERR(sb))
7565 +               goto out;
7566 +
7567 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7568 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7569 +            "xino %d\n",
7570 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7571 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7572 +            sb->s_flags, sb->s_count,
7573 +            atomic_read(&sb->s_active),
7574 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7575 +       return 0;
7576 +
7577 +out:
7578 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7579 +       return -1;
7580 +}
7581 +
7582 +void au_dpri_sb(struct super_block *sb)
7583 +{
7584 +       struct au_sbinfo *sbinfo;
7585 +       aufs_bindex_t bindex;
7586 +       int err;
7587 +       /* to reduce stack size */
7588 +       struct {
7589 +               struct vfsmount mnt;
7590 +               struct au_branch fake;
7591 +       } *a;
7592 +
7593 +       /* this function can be called from magic sysrq */
7594 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7595 +       if (unlikely(!a)) {
7596 +               dpri("no memory\n");
7597 +               return;
7598 +       }
7599 +
7600 +       a->mnt.mnt_sb = sb;
7601 +       a->fake.br_path.mnt = &a->mnt;
7602 +       err = do_pri_br(-1, &a->fake);
7603 +       au_kfree_rcu(a);
7604 +       dpri("dev 0x%x\n", sb->s_dev);
7605 +       if (err || !au_test_aufs(sb))
7606 +               return;
7607 +
7608 +       sbinfo = au_sbi(sb);
7609 +       if (!sbinfo)
7610 +               return;
7611 +       dpri("nw %d, gen %u, kobj %d\n",
7612 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7613 +            kref_read(&sbinfo->si_kobj.kref));
7614 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7615 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7616 +}
7617 +
7618 +/* ---------------------------------------------------------------------- */
7619 +
7620 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7621 +{
7622 +       struct inode *h_inode, *inode = d_inode(dentry);
7623 +       struct dentry *h_dentry;
7624 +       aufs_bindex_t bindex, bbot, bi;
7625 +
7626 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7627 +               return;
7628 +
7629 +       bbot = au_dbbot(dentry);
7630 +       bi = au_ibbot(inode);
7631 +       if (bi < bbot)
7632 +               bbot = bi;
7633 +       bindex = au_dbtop(dentry);
7634 +       bi = au_ibtop(inode);
7635 +       if (bi > bindex)
7636 +               bindex = bi;
7637 +
7638 +       for (; bindex <= bbot; bindex++) {
7639 +               h_dentry = au_h_dptr(dentry, bindex);
7640 +               if (!h_dentry)
7641 +                       continue;
7642 +               h_inode = au_h_iptr(inode, bindex);
7643 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7644 +                       au_debug_on();
7645 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7646 +                       AuDbgDentry(dentry);
7647 +                       AuDbgInode(inode);
7648 +                       au_debug_off();
7649 +                       if (au_test_fuse(h_inode->i_sb))
7650 +                               WARN_ON_ONCE(1);
7651 +                       else
7652 +                               BUG();
7653 +               }
7654 +       }
7655 +}
7656 +
7657 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7658 +{
7659 +       int err, i, j;
7660 +       struct au_dcsub_pages dpages;
7661 +       struct au_dpage *dpage;
7662 +       struct dentry **dentries;
7663 +
7664 +       err = au_dpages_init(&dpages, GFP_NOFS);
7665 +       AuDebugOn(err);
7666 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7667 +       AuDebugOn(err);
7668 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7669 +               dpage = dpages.dpages + i;
7670 +               dentries = dpage->dentries;
7671 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7672 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7673 +       }
7674 +       au_dpages_free(&dpages);
7675 +}
7676 +
7677 +void au_dbg_verify_kthread(void)
7678 +{
7679 +       if (au_wkq_test()) {
7680 +               au_dbg_blocked();
7681 +               /*
7682 +                * It may be recursive, but udba=notify between two aufs mounts,
7683 +                * where a single ro branch is shared, is not a problem.
7684 +                */
7685 +               /* WARN_ON(1); */
7686 +       }
7687 +}
7688 +
7689 +/* ---------------------------------------------------------------------- */
7690 +
7691 +int __init au_debug_init(void)
7692 +{
7693 +       aufs_bindex_t bindex;
7694 +       struct au_vdir_destr destr;
7695 +
7696 +       bindex = -1;
7697 +       AuDebugOn(bindex >= 0);
7698 +
7699 +       destr.len = -1;
7700 +       AuDebugOn(destr.len < NAME_MAX);
7701 +
7702 +#ifdef CONFIG_4KSTACKS
7703 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7704 +#endif
7705 +
7706 +       return 0;
7707 +}
7708 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7709 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7710 +++ linux/fs/aufs/debug.h       2021-12-03 15:38:59.933313976 +0100
7711 @@ -0,0 +1,226 @@
7712 +/* SPDX-License-Identifier: GPL-2.0 */
7713 +/*
7714 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7715 + *
7716 + * This program, aufs is free software; you can redistribute it and/or modify
7717 + * it under the terms of the GNU General Public License as published by
7718 + * the Free Software Foundation; either version 2 of the License, or
7719 + * (at your option) any later version.
7720 + *
7721 + * This program is distributed in the hope that it will be useful,
7722 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7723 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7724 + * GNU General Public License for more details.
7725 + *
7726 + * You should have received a copy of the GNU General Public License
7727 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7728 + */
7729 +
7730 +/*
7731 + * debug print functions
7732 + */
7733 +
7734 +#ifndef __AUFS_DEBUG_H__
7735 +#define __AUFS_DEBUG_H__
7736 +
7737 +#ifdef __KERNEL__
7738 +
7739 +#include <linux/atomic.h>
7740 +#include <linux/module.h>
7741 +#include <linux/kallsyms.h>
7742 +#include <linux/sysrq.h>
7743 +
7744 +#ifdef CONFIG_AUFS_DEBUG
7745 +#define AuDebugOn(a)           BUG_ON(a)
7746 +
7747 +/* module parameter */
7748 +extern atomic_t aufs_debug;
7749 +static inline void au_debug_on(void)
7750 +{
7751 +       atomic_inc(&aufs_debug);
7752 +}
7753 +static inline void au_debug_off(void)
7754 +{
7755 +       atomic_dec_if_positive(&aufs_debug);
7756 +}
7757 +
7758 +static inline int au_debug_test(void)
7759 +{
7760 +       return atomic_read(&aufs_debug) > 0;
7761 +}
7762 +#else
7763 +#define AuDebugOn(a)           do {} while (0)
7764 +AuStubVoid(au_debug_on, void)
7765 +AuStubVoid(au_debug_off, void)
7766 +AuStubInt0(au_debug_test, void)
7767 +#endif /* CONFIG_AUFS_DEBUG */
7768 +
7769 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7770 +
7771 +/* ---------------------------------------------------------------------- */
7772 +
7773 +/* debug print */
7774 +
7775 +#define AuDbg(fmt, ...) do { \
7776 +       if (au_debug_test()) \
7777 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7778 +} while (0)
7779 +#define AuLabel(l)             AuDbg(#l "\n")
7780 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7781 +#define AuWarn1(fmt, ...) do { \
7782 +       static unsigned char _c; \
7783 +       if (!_c++) \
7784 +               pr_warn(fmt, ##__VA_ARGS__); \
7785 +} while (0)
7786 +
7787 +#define AuErr1(fmt, ...) do { \
7788 +       static unsigned char _c; \
7789 +       if (!_c++) \
7790 +               pr_err(fmt, ##__VA_ARGS__); \
7791 +} while (0)
7792 +
7793 +#define AuIOErr1(fmt, ...) do { \
7794 +       static unsigned char _c; \
7795 +       if (!_c++) \
7796 +               AuIOErr(fmt, ##__VA_ARGS__); \
7797 +} while (0)
7798 +
7799 +#define AuUnsupportMsg "This operation is not supported." \
7800 +                       " Please report this application to aufs-users ML."
7801 +#define AuUnsupport(fmt, ...) do { \
7802 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7803 +       dump_stack(); \
7804 +} while (0)
7805 +
7806 +#define AuTraceErr(e) do { \
7807 +       if (unlikely((e) < 0)) \
7808 +               AuDbg("err %d\n", (int)(e)); \
7809 +} while (0)
7810 +
7811 +#define AuTraceErrPtr(p) do { \
7812 +       if (IS_ERR(p)) \
7813 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7814 +} while (0)
7815 +
7816 +/* dirty macros for debug print, use with "%.*s" and caution */
7817 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7818 +
7819 +/* ---------------------------------------------------------------------- */
7820 +
7821 +struct dentry;
7822 +#ifdef CONFIG_AUFS_DEBUG
7823 +extern struct mutex au_dbg_mtx;
7824 +extern char *au_plevel;
7825 +struct au_nhash;
7826 +void au_dpri_whlist(struct au_nhash *whlist);
7827 +struct au_vdir;
7828 +void au_dpri_vdir(struct au_vdir *vdir);
7829 +struct inode;
7830 +void au_dpri_inode(struct inode *inode);
7831 +void au_dpri_dalias(struct inode *inode);
7832 +void au_dpri_dentry(struct dentry *dentry);
7833 +struct file;
7834 +void au_dpri_file(struct file *filp);
7835 +struct super_block;
7836 +void au_dpri_sb(struct super_block *sb);
7837 +
7838 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7839 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7840 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7841 +void au_dbg_verify_kthread(void);
7842 +
7843 +int __init au_debug_init(void);
7844 +
7845 +#define AuDbgWhlist(w) do { \
7846 +       mutex_lock(&au_dbg_mtx); \
7847 +       AuDbg(#w "\n"); \
7848 +       au_dpri_whlist(w); \
7849 +       mutex_unlock(&au_dbg_mtx); \
7850 +} while (0)
7851 +
7852 +#define AuDbgVdir(v) do { \
7853 +       mutex_lock(&au_dbg_mtx); \
7854 +       AuDbg(#v "\n"); \
7855 +       au_dpri_vdir(v); \
7856 +       mutex_unlock(&au_dbg_mtx); \
7857 +} while (0)
7858 +
7859 +#define AuDbgInode(i) do { \
7860 +       mutex_lock(&au_dbg_mtx); \
7861 +       AuDbg(#i "\n"); \
7862 +       au_dpri_inode(i); \
7863 +       mutex_unlock(&au_dbg_mtx); \
7864 +} while (0)
7865 +
7866 +#define AuDbgDAlias(i) do { \
7867 +       mutex_lock(&au_dbg_mtx); \
7868 +       AuDbg(#i "\n"); \
7869 +       au_dpri_dalias(i); \
7870 +       mutex_unlock(&au_dbg_mtx); \
7871 +} while (0)
7872 +
7873 +#define AuDbgDentry(d) do { \
7874 +       mutex_lock(&au_dbg_mtx); \
7875 +       AuDbg(#d "\n"); \
7876 +       au_dpri_dentry(d); \
7877 +       mutex_unlock(&au_dbg_mtx); \
7878 +} while (0)
7879 +
7880 +#define AuDbgFile(f) do { \
7881 +       mutex_lock(&au_dbg_mtx); \
7882 +       AuDbg(#f "\n"); \
7883 +       au_dpri_file(f); \
7884 +       mutex_unlock(&au_dbg_mtx); \
7885 +} while (0)
7886 +
7887 +#define AuDbgSb(sb) do { \
7888 +       mutex_lock(&au_dbg_mtx); \
7889 +       AuDbg(#sb "\n"); \
7890 +       au_dpri_sb(sb); \
7891 +       mutex_unlock(&au_dbg_mtx); \
7892 +} while (0)
7893 +
7894 +#define AuDbgSym(addr) do {                            \
7895 +       char sym[KSYM_SYMBOL_LEN];                      \
7896 +       sprint_symbol(sym, (unsigned long)addr);        \
7897 +       AuDbg("%s\n", sym);                             \
7898 +} while (0)
7899 +#else
7900 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7901 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7902 +AuStubVoid(au_dbg_verify_kthread, void)
7903 +AuStubInt0(__init au_debug_init, void)
7904 +
7905 +#define AuDbgWhlist(w)         do {} while (0)
7906 +#define AuDbgVdir(v)           do {} while (0)
7907 +#define AuDbgInode(i)          do {} while (0)
7908 +#define AuDbgDAlias(i)         do {} while (0)
7909 +#define AuDbgDentry(d)         do {} while (0)
7910 +#define AuDbgFile(f)           do {} while (0)
7911 +#define AuDbgSb(sb)            do {} while (0)
7912 +#define AuDbgSym(addr)         do {} while (0)
7913 +#endif /* CONFIG_AUFS_DEBUG */
7914 +
7915 +/* ---------------------------------------------------------------------- */
7916 +
7917 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7918 +int __init au_sysrq_init(void);
7919 +void au_sysrq_fin(void);
7920 +
7921 +#ifdef CONFIG_HW_CONSOLE
7922 +#define au_dbg_blocked() do { \
7923 +       WARN_ON(1); \
7924 +       handle_sysrq('w'); \
7925 +} while (0)
7926 +#else
7927 +AuStubVoid(au_dbg_blocked, void)
7928 +#endif
7929 +
7930 +#else
7931 +AuStubInt0(__init au_sysrq_init, void)
7932 +AuStubVoid(au_sysrq_fin, void)
7933 +AuStubVoid(au_dbg_blocked, void)
7934 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7935 +
7936 +#endif /* __KERNEL__ */
7937 +#endif /* __AUFS_DEBUG_H__ */
7938 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7939 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7940 +++ linux/fs/aufs/dentry.c      2021-12-03 15:40:58.233313963 +0100
7941 @@ -0,0 +1,1169 @@
7942 +// SPDX-License-Identifier: GPL-2.0
7943 +/*
7944 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7945 + *
7946 + * This program, aufs is free software; you can redistribute it and/or modify
7947 + * it under the terms of the GNU General Public License as published by
7948 + * the Free Software Foundation; either version 2 of the License, or
7949 + * (at your option) any later version.
7950 + *
7951 + * This program is distributed in the hope that it will be useful,
7952 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7953 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7954 + * GNU General Public License for more details.
7955 + *
7956 + * You should have received a copy of the GNU General Public License
7957 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7958 + */
7959 +
7960 +/*
7961 + * lookup and dentry operations
7962 + */
7963 +
7964 +#include <linux/iversion.h>
7965 +#include <linux/namei.h>
7966 +#include "aufs.h"
7967 +
7968 +/*
7969 + * returns positive/negative dentry, NULL or an error.
7970 + * NULL means whiteout-ed or not-found.
7971 + */
7972 +static struct dentry*
7973 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7974 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7975 +{
7976 +       struct dentry *h_dentry;
7977 +       struct inode *h_inode;
7978 +       struct au_branch *br;
7979 +       struct user_namespace *h_userns;
7980 +       struct path h_path;
7981 +       int wh_found, opq;
7982 +       unsigned char wh_able;
7983 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7984 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7985 +                                                         IGNORE_PERM);
7986 +
7987 +       wh_found = 0;
7988 +       br = au_sbr(dentry->d_sb, bindex);
7989 +       h_path.dentry = h_parent;
7990 +       h_path.mnt = au_br_mnt(br);
7991 +       h_userns = au_br_userns(br);
7992 +       wh_able = !!au_br_whable(br->br_perm);
7993 +       if (wh_able)
7994 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
7995 +                                     ignore_perm);
7996 +       h_dentry = ERR_PTR(wh_found);
7997 +       if (!wh_found)
7998 +               goto real_lookup;
7999 +       if (unlikely(wh_found < 0))
8000 +               goto out;
8001 +
8002 +       /* We found a whiteout */
8003 +       /* au_set_dbbot(dentry, bindex); */
8004 +       au_set_dbwh(dentry, bindex);
8005 +       if (!allow_neg)
8006 +               return NULL; /* success */
8007 +
8008 +real_lookup:
8009 +       if (!ignore_perm)
8010 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8011 +       else
8012 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8013 +       if (IS_ERR(h_dentry)) {
8014 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8015 +                   && !allow_neg)
8016 +                       h_dentry = NULL;
8017 +               goto out;
8018 +       }
8019 +
8020 +       h_inode = d_inode(h_dentry);
8021 +       if (d_is_negative(h_dentry)) {
8022 +               if (!allow_neg)
8023 +                       goto out_neg;
8024 +       } else if (wh_found
8025 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8026 +               goto out_neg;
8027 +       else if (au_ftest_lkup(args->flags, DIRREN)
8028 +                /* && h_inode */
8029 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8030 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8031 +                     (unsigned long long)h_inode->i_ino);
8032 +               goto out_neg;
8033 +       }
8034 +
8035 +       if (au_dbbot(dentry) <= bindex)
8036 +               au_set_dbbot(dentry, bindex);
8037 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8038 +               au_set_dbtop(dentry, bindex);
8039 +       au_set_h_dptr(dentry, bindex, h_dentry);
8040 +
8041 +       if (!d_is_dir(h_dentry)
8042 +           || !wh_able
8043 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8044 +               goto out; /* success */
8045 +
8046 +       h_path.dentry = h_dentry;
8047 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8048 +       opq = au_diropq_test(h_userns, &h_path);
8049 +       inode_unlock_shared(h_inode);
8050 +       if (opq > 0)
8051 +               au_set_dbdiropq(dentry, bindex);
8052 +       else if (unlikely(opq < 0)) {
8053 +               au_set_h_dptr(dentry, bindex, NULL);
8054 +               h_dentry = ERR_PTR(opq);
8055 +       }
8056 +       goto out;
8057 +
8058 +out_neg:
8059 +       dput(h_dentry);
8060 +       h_dentry = NULL;
8061 +out:
8062 +       return h_dentry;
8063 +}
8064 +
8065 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8066 +{
8067 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8068 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8069 +               return -EPERM;
8070 +       return 0;
8071 +}
8072 +
8073 +/*
8074 + * returns the number of lower positive dentries,
8075 + * otherwise an error.
8076 + * can be called at unlinking with @type is zero.
8077 + */
8078 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8079 +                  unsigned int flags)
8080 +{
8081 +       int npositive, err;
8082 +       aufs_bindex_t bindex, btail, bdiropq;
8083 +       unsigned char isdir, dirperm1, dirren;
8084 +       struct au_do_lookup_args args = {
8085 +               .flags          = flags,
8086 +               .name           = &dentry->d_name
8087 +       };
8088 +       struct dentry *parent;
8089 +       struct super_block *sb;
8090 +
8091 +       sb = dentry->d_sb;
8092 +       err = au_test_shwh(sb, args.name);
8093 +       if (unlikely(err))
8094 +               goto out;
8095 +
8096 +       err = au_wh_name_alloc(&args.whname, args.name);
8097 +       if (unlikely(err))
8098 +               goto out;
8099 +
8100 +       isdir = !!d_is_dir(dentry);
8101 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8102 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8103 +       if (dirren)
8104 +               au_fset_lkup(args.flags, DIRREN);
8105 +
8106 +       npositive = 0;
8107 +       parent = dget_parent(dentry);
8108 +       btail = au_dbtaildir(parent);
8109 +       for (bindex = btop; bindex <= btail; bindex++) {
8110 +               struct dentry *h_parent, *h_dentry;
8111 +               struct inode *h_inode, *h_dir;
8112 +               struct au_branch *br;
8113 +
8114 +               h_dentry = au_h_dptr(dentry, bindex);
8115 +               if (h_dentry) {
8116 +                       if (d_is_positive(h_dentry))
8117 +                               npositive++;
8118 +                       break;
8119 +               }
8120 +               h_parent = au_h_dptr(parent, bindex);
8121 +               if (!h_parent || !d_is_dir(h_parent))
8122 +                       continue;
8123 +
8124 +               if (dirren) {
8125 +                       /* if the inum matches, then use the prepared name */
8126 +                       err = au_dr_lkup_name(&args, bindex);
8127 +                       if (unlikely(err))
8128 +                               goto out_parent;
8129 +               }
8130 +
8131 +               h_dir = d_inode(h_parent);
8132 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8133 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8134 +               inode_unlock_shared(h_dir);
8135 +               err = PTR_ERR(h_dentry);
8136 +               if (IS_ERR(h_dentry))
8137 +                       goto out_parent;
8138 +               if (h_dentry)
8139 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8140 +               if (dirperm1)
8141 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8142 +
8143 +               if (au_dbwh(dentry) == bindex)
8144 +                       break;
8145 +               if (!h_dentry)
8146 +                       continue;
8147 +               if (d_is_negative(h_dentry))
8148 +                       continue;
8149 +               h_inode = d_inode(h_dentry);
8150 +               npositive++;
8151 +               if (!args.type)
8152 +                       args.type = h_inode->i_mode & S_IFMT;
8153 +               if (args.type != S_IFDIR)
8154 +                       break;
8155 +               else if (isdir) {
8156 +                       /* the type of lower may be different */
8157 +                       bdiropq = au_dbdiropq(dentry);
8158 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8159 +                               break;
8160 +               }
8161 +               br = au_sbr(sb, bindex);
8162 +               if (dirren
8163 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8164 +                                          /*add_ent*/NULL)) {
8165 +                       /* prepare next name to lookup */
8166 +                       err = au_dr_lkup(&args, dentry, bindex);
8167 +                       if (unlikely(err))
8168 +                               goto out_parent;
8169 +               }
8170 +       }
8171 +
8172 +       if (npositive) {
8173 +               AuLabel(positive);
8174 +               au_update_dbtop(dentry);
8175 +       }
8176 +       err = npositive;
8177 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8178 +                    && au_dbtop(dentry) < 0)) {
8179 +               err = -EIO;
8180 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8181 +                       dentry, err);
8182 +       }
8183 +
8184 +out_parent:
8185 +       dput(parent);
8186 +       au_kfree_try_rcu(args.whname.name);
8187 +       if (dirren)
8188 +               au_dr_lkup_fin(&args);
8189 +out:
8190 +       return err;
8191 +}
8192 +
8193 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8194 +                              struct path *ppath)
8195 +{
8196 +       struct dentry *dentry;
8197 +       int wkq_err;
8198 +
8199 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8200 +               dentry = vfsub_lkup_one(name, ppath);
8201 +       else {
8202 +               struct vfsub_lkup_one_args args = {
8203 +                       .errp   = &dentry,
8204 +                       .name   = name,
8205 +                       .ppath  = ppath
8206 +               };
8207 +
8208 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8209 +               if (unlikely(wkq_err))
8210 +                       dentry = ERR_PTR(wkq_err);
8211 +       }
8212 +
8213 +       return dentry;
8214 +}
8215 +
8216 +/*
8217 + * lookup @dentry on @bindex which should be negative.
8218 + */
8219 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8220 +{
8221 +       int err;
8222 +       struct dentry *parent, *h_dentry;
8223 +       struct au_branch *br;
8224 +       struct user_namespace *h_userns;
8225 +       struct path h_ppath;
8226 +
8227 +       parent = dget_parent(dentry);
8228 +       br = au_sbr(dentry->d_sb, bindex);
8229 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8230 +       h_ppath.mnt = au_br_mnt(br);
8231 +       h_userns = au_br_userns(br);
8232 +       if (wh)
8233 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8234 +       else
8235 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8236 +       err = PTR_ERR(h_dentry);
8237 +       if (IS_ERR(h_dentry))
8238 +               goto out;
8239 +       if (unlikely(d_is_positive(h_dentry))) {
8240 +               err = -EIO;
8241 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8242 +               dput(h_dentry);
8243 +               goto out;
8244 +       }
8245 +
8246 +       err = 0;
8247 +       if (bindex < au_dbtop(dentry))
8248 +               au_set_dbtop(dentry, bindex);
8249 +       if (au_dbbot(dentry) < bindex)
8250 +               au_set_dbbot(dentry, bindex);
8251 +       au_set_h_dptr(dentry, bindex, h_dentry);
8252 +
8253 +out:
8254 +       dput(parent);
8255 +       return err;
8256 +}
8257 +
8258 +/* ---------------------------------------------------------------------- */
8259 +
8260 +/* subset of struct inode */
8261 +struct au_iattr {
8262 +       unsigned long           i_ino;
8263 +       /* unsigned int         i_nlink; */
8264 +       kuid_t                  i_uid;
8265 +       kgid_t                  i_gid;
8266 +       u64                     i_version;
8267 +/*
8268 +       loff_t                  i_size;
8269 +       blkcnt_t                i_blocks;
8270 +*/
8271 +       umode_t                 i_mode;
8272 +};
8273 +
8274 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8275 +{
8276 +       ia->i_ino = h_inode->i_ino;
8277 +       /* ia->i_nlink = h_inode->i_nlink; */
8278 +       ia->i_uid = h_inode->i_uid;
8279 +       ia->i_gid = h_inode->i_gid;
8280 +       ia->i_version = inode_query_iversion(h_inode);
8281 +/*
8282 +       ia->i_size = h_inode->i_size;
8283 +       ia->i_blocks = h_inode->i_blocks;
8284 +*/
8285 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8286 +}
8287 +
8288 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8289 +{
8290 +       return ia->i_ino != h_inode->i_ino
8291 +               /* || ia->i_nlink != h_inode->i_nlink */
8292 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8293 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8294 +               || !inode_eq_iversion(h_inode, ia->i_version)
8295 +/*
8296 +               || ia->i_size != h_inode->i_size
8297 +               || ia->i_blocks != h_inode->i_blocks
8298 +*/
8299 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8300 +}
8301 +
8302 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8303 +                             struct au_branch *br)
8304 +{
8305 +       int err;
8306 +       struct au_iattr ia;
8307 +       struct inode *h_inode;
8308 +       struct dentry *h_d;
8309 +       struct super_block *h_sb;
8310 +       struct path h_ppath;
8311 +
8312 +       err = 0;
8313 +       memset(&ia, -1, sizeof(ia));
8314 +       h_sb = h_dentry->d_sb;
8315 +       h_inode = NULL;
8316 +       if (d_is_positive(h_dentry)) {
8317 +               h_inode = d_inode(h_dentry);
8318 +               au_iattr_save(&ia, h_inode);
8319 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8320 +               /* nfs d_revalidate may return 0 for negative dentry */
8321 +               /* fuse d_revalidate always return 0 for negative dentry */
8322 +               goto out;
8323 +
8324 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8325 +       h_ppath.dentry = h_parent;
8326 +       h_ppath.mnt = au_br_mnt(br);
8327 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8328 +       err = PTR_ERR(h_d);
8329 +       if (IS_ERR(h_d))
8330 +               goto out;
8331 +
8332 +       err = 0;
8333 +       if (unlikely(h_d != h_dentry
8334 +                    || d_inode(h_d) != h_inode
8335 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8336 +               err = au_busy_or_stale();
8337 +       dput(h_d);
8338 +
8339 +out:
8340 +       AuTraceErr(err);
8341 +       return err;
8342 +}
8343 +
8344 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8345 +               struct dentry *h_parent, struct au_branch *br)
8346 +{
8347 +       int err;
8348 +
8349 +       err = 0;
8350 +       if (udba == AuOpt_UDBA_REVAL
8351 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8352 +               IMustLock(h_dir);
8353 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8354 +       } else if (udba != AuOpt_UDBA_NONE)
8355 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8356 +
8357 +       return err;
8358 +}
8359 +
8360 +/* ---------------------------------------------------------------------- */
8361 +
8362 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8363 +{
8364 +       int err;
8365 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8366 +       struct au_hdentry tmp, *p, *q;
8367 +       struct au_dinfo *dinfo;
8368 +       struct super_block *sb;
8369 +
8370 +       DiMustWriteLock(dentry);
8371 +
8372 +       sb = dentry->d_sb;
8373 +       dinfo = au_di(dentry);
8374 +       bbot = dinfo->di_bbot;
8375 +       bwh = dinfo->di_bwh;
8376 +       bdiropq = dinfo->di_bdiropq;
8377 +       bindex = dinfo->di_btop;
8378 +       p = au_hdentry(dinfo, bindex);
8379 +       for (; bindex <= bbot; bindex++, p++) {
8380 +               if (!p->hd_dentry)
8381 +                       continue;
8382 +
8383 +               new_bindex = au_br_index(sb, p->hd_id);
8384 +               if (new_bindex == bindex)
8385 +                       continue;
8386 +
8387 +               if (dinfo->di_bwh == bindex)
8388 +                       bwh = new_bindex;
8389 +               if (dinfo->di_bdiropq == bindex)
8390 +                       bdiropq = new_bindex;
8391 +               if (new_bindex < 0) {
8392 +                       au_hdput(p);
8393 +                       p->hd_dentry = NULL;
8394 +                       continue;
8395 +               }
8396 +
8397 +               /* swap two lower dentries, and loop again */
8398 +               q = au_hdentry(dinfo, new_bindex);
8399 +               tmp = *q;
8400 +               *q = *p;
8401 +               *p = tmp;
8402 +               if (tmp.hd_dentry) {
8403 +                       bindex--;
8404 +                       p--;
8405 +               }
8406 +       }
8407 +
8408 +       dinfo->di_bwh = -1;
8409 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8410 +               dinfo->di_bwh = bwh;
8411 +
8412 +       dinfo->di_bdiropq = -1;
8413 +       if (bdiropq >= 0
8414 +           && bdiropq <= au_sbbot(sb)
8415 +           && au_sbr_whable(sb, bdiropq))
8416 +               dinfo->di_bdiropq = bdiropq;
8417 +
8418 +       err = -EIO;
8419 +       dinfo->di_btop = -1;
8420 +       dinfo->di_bbot = -1;
8421 +       bbot = au_dbbot(parent);
8422 +       bindex = 0;
8423 +       p = au_hdentry(dinfo, bindex);
8424 +       for (; bindex <= bbot; bindex++, p++)
8425 +               if (p->hd_dentry) {
8426 +                       dinfo->di_btop = bindex;
8427 +                       break;
8428 +               }
8429 +
8430 +       if (dinfo->di_btop >= 0) {
8431 +               bindex = bbot;
8432 +               p = au_hdentry(dinfo, bindex);
8433 +               for (; bindex >= 0; bindex--, p--)
8434 +                       if (p->hd_dentry) {
8435 +                               dinfo->di_bbot = bindex;
8436 +                               err = 0;
8437 +                               break;
8438 +                       }
8439 +       }
8440 +
8441 +       return err;
8442 +}
8443 +
8444 +static void au_do_hide(struct dentry *dentry)
8445 +{
8446 +       struct inode *inode;
8447 +
8448 +       if (d_really_is_positive(dentry)) {
8449 +               inode = d_inode(dentry);
8450 +               if (!d_is_dir(dentry)) {
8451 +                       if (inode->i_nlink && !d_unhashed(dentry))
8452 +                               drop_nlink(inode);
8453 +               } else {
8454 +                       clear_nlink(inode);
8455 +                       /* stop next lookup */
8456 +                       inode->i_flags |= S_DEAD;
8457 +               }
8458 +               smp_mb(); /* necessary? */
8459 +       }
8460 +       d_drop(dentry);
8461 +}
8462 +
8463 +static int au_hide_children(struct dentry *parent)
8464 +{
8465 +       int err, i, j, ndentry;
8466 +       struct au_dcsub_pages dpages;
8467 +       struct au_dpage *dpage;
8468 +       struct dentry *dentry;
8469 +
8470 +       err = au_dpages_init(&dpages, GFP_NOFS);
8471 +       if (unlikely(err))
8472 +               goto out;
8473 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8474 +       if (unlikely(err))
8475 +               goto out_dpages;
8476 +
8477 +       /* in reverse order */
8478 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8479 +               dpage = dpages.dpages + i;
8480 +               ndentry = dpage->ndentry;
8481 +               for (j = ndentry - 1; j >= 0; j--) {
8482 +                       dentry = dpage->dentries[j];
8483 +                       if (dentry != parent)
8484 +                               au_do_hide(dentry);
8485 +               }
8486 +       }
8487 +
8488 +out_dpages:
8489 +       au_dpages_free(&dpages);
8490 +out:
8491 +       return err;
8492 +}
8493 +
8494 +static void au_hide(struct dentry *dentry)
8495 +{
8496 +       int err;
8497 +
8498 +       AuDbgDentry(dentry);
8499 +       if (d_is_dir(dentry)) {
8500 +               /* shrink_dcache_parent(dentry); */
8501 +               err = au_hide_children(dentry);
8502 +               if (unlikely(err))
8503 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8504 +                               dentry, err);
8505 +       }
8506 +       au_do_hide(dentry);
8507 +}
8508 +
8509 +/*
8510 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8511 + *
8512 + * a dirty branch is added
8513 + * - on the top of layers
8514 + * - in the middle of layers
8515 + * - to the bottom of layers
8516 + *
8517 + * on the added branch there exists
8518 + * - a whiteout
8519 + * - a diropq
8520 + * - a same named entry
8521 + *   + exist
8522 + *     * negative --> positive
8523 + *     * positive --> positive
8524 + *      - type is unchanged
8525 + *      - type is changed
8526 + *   + doesn't exist
8527 + *     * negative --> negative
8528 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8529 + * - none
8530 + */
8531 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8532 +                              struct au_dinfo *tmp)
8533 +{
8534 +       int err;
8535 +       aufs_bindex_t bindex, bbot;
8536 +       struct {
8537 +               struct dentry *dentry;
8538 +               struct inode *inode;
8539 +               mode_t mode;
8540 +       } orig_h, tmp_h = {
8541 +               .dentry = NULL
8542 +       };
8543 +       struct au_hdentry *hd;
8544 +       struct inode *inode, *h_inode;
8545 +       struct dentry *h_dentry;
8546 +
8547 +       err = 0;
8548 +       AuDebugOn(dinfo->di_btop < 0);
8549 +       orig_h.mode = 0;
8550 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8551 +       orig_h.inode = NULL;
8552 +       if (d_is_positive(orig_h.dentry)) {
8553 +               orig_h.inode = d_inode(orig_h.dentry);
8554 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8555 +       }
8556 +       if (tmp->di_btop >= 0) {
8557 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8558 +               if (d_is_positive(tmp_h.dentry)) {
8559 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8560 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8561 +               }
8562 +       }
8563 +
8564 +       inode = NULL;
8565 +       if (d_really_is_positive(dentry))
8566 +               inode = d_inode(dentry);
8567 +       if (!orig_h.inode) {
8568 +               AuDbg("negative originally\n");
8569 +               if (inode) {
8570 +                       au_hide(dentry);
8571 +                       goto out;
8572 +               }
8573 +               AuDebugOn(inode);
8574 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8575 +               AuDebugOn(dinfo->di_bdiropq != -1);
8576 +
8577 +               if (!tmp_h.inode) {
8578 +                       AuDbg("negative --> negative\n");
8579 +                       /* should have only one negative lower */
8580 +                       if (tmp->di_btop >= 0
8581 +                           && tmp->di_btop < dinfo->di_btop) {
8582 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8583 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8584 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8585 +                               au_di_cp(dinfo, tmp);
8586 +                               hd = au_hdentry(tmp, tmp->di_btop);
8587 +                               au_set_h_dptr(dentry, tmp->di_btop,
8588 +                                             dget(hd->hd_dentry));
8589 +                       }
8590 +                       au_dbg_verify_dinode(dentry);
8591 +               } else {
8592 +                       AuDbg("negative --> positive\n");
8593 +                       /*
8594 +                        * similar to the behaviour of creating with bypassing
8595 +                        * aufs.
8596 +                        * unhash it in order to force an error in the
8597 +                        * succeeding create operation.
8598 +                        * we should not set S_DEAD here.
8599 +                        */
8600 +                       d_drop(dentry);
8601 +                       /* au_di_swap(tmp, dinfo); */
8602 +                       au_dbg_verify_dinode(dentry);
8603 +               }
8604 +       } else {
8605 +               AuDbg("positive originally\n");
8606 +               /* inode may be NULL */
8607 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8608 +               if (!tmp_h.inode) {
8609 +                       AuDbg("positive --> negative\n");
8610 +                       /* or bypassing aufs */
8611 +                       au_hide(dentry);
8612 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8613 +                               dinfo->di_bwh = tmp->di_bwh;
8614 +                       if (inode)
8615 +                               err = au_refresh_hinode_self(inode);
8616 +                       au_dbg_verify_dinode(dentry);
8617 +               } else if (orig_h.mode == tmp_h.mode) {
8618 +                       AuDbg("positive --> positive, same type\n");
8619 +                       if (!S_ISDIR(orig_h.mode)
8620 +                           && dinfo->di_btop > tmp->di_btop) {
8621 +                               /*
8622 +                                * similar to the behaviour of removing and
8623 +                                * creating.
8624 +                                */
8625 +                               au_hide(dentry);
8626 +                               if (inode)
8627 +                                       err = au_refresh_hinode_self(inode);
8628 +                               au_dbg_verify_dinode(dentry);
8629 +                       } else {
8630 +                               /* fill empty slots */
8631 +                               if (dinfo->di_btop > tmp->di_btop)
8632 +                                       dinfo->di_btop = tmp->di_btop;
8633 +                               if (dinfo->di_bbot < tmp->di_bbot)
8634 +                                       dinfo->di_bbot = tmp->di_bbot;
8635 +                               dinfo->di_bwh = tmp->di_bwh;
8636 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8637 +                               bbot = dinfo->di_bbot;
8638 +                               bindex = tmp->di_btop;
8639 +                               hd = au_hdentry(tmp, bindex);
8640 +                               for (; bindex <= bbot; bindex++, hd++) {
8641 +                                       if (au_h_dptr(dentry, bindex))
8642 +                                               continue;
8643 +                                       h_dentry = hd->hd_dentry;
8644 +                                       if (!h_dentry)
8645 +                                               continue;
8646 +                                       AuDebugOn(d_is_negative(h_dentry));
8647 +                                       h_inode = d_inode(h_dentry);
8648 +                                       AuDebugOn(orig_h.mode
8649 +                                                 != (h_inode->i_mode
8650 +                                                     & S_IFMT));
8651 +                                       au_set_h_dptr(dentry, bindex,
8652 +                                                     dget(h_dentry));
8653 +                               }
8654 +                               if (inode)
8655 +                                       err = au_refresh_hinode(inode, dentry);
8656 +                               au_dbg_verify_dinode(dentry);
8657 +                       }
8658 +               } else {
8659 +                       AuDbg("positive --> positive, different type\n");
8660 +                       /* similar to the behaviour of removing and creating */
8661 +                       au_hide(dentry);
8662 +                       if (inode)
8663 +                               err = au_refresh_hinode_self(inode);
8664 +                       au_dbg_verify_dinode(dentry);
8665 +               }
8666 +       }
8667 +
8668 +out:
8669 +       return err;
8670 +}
8671 +
8672 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8673 +{
8674 +       const struct dentry_operations *dop
8675 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8676 +       static const unsigned int mask
8677 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8678 +
8679 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8680 +
8681 +       if (dentry->d_op == dop)
8682 +               return;
8683 +
8684 +       AuDbg("%pd\n", dentry);
8685 +       spin_lock(&dentry->d_lock);
8686 +       if (dop == &aufs_dop)
8687 +               dentry->d_flags |= mask;
8688 +       else
8689 +               dentry->d_flags &= ~mask;
8690 +       dentry->d_op = dop;
8691 +       spin_unlock(&dentry->d_lock);
8692 +}
8693 +
8694 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8695 +{
8696 +       int err, ebrange, nbr;
8697 +       unsigned int sigen;
8698 +       struct au_dinfo *dinfo, *tmp;
8699 +       struct super_block *sb;
8700 +       struct inode *inode;
8701 +
8702 +       DiMustWriteLock(dentry);
8703 +       AuDebugOn(IS_ROOT(dentry));
8704 +       AuDebugOn(d_really_is_negative(parent));
8705 +
8706 +       sb = dentry->d_sb;
8707 +       sigen = au_sigen(sb);
8708 +       err = au_digen_test(parent, sigen);
8709 +       if (unlikely(err))
8710 +               goto out;
8711 +
8712 +       nbr = au_sbbot(sb) + 1;
8713 +       dinfo = au_di(dentry);
8714 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8715 +       if (unlikely(err))
8716 +               goto out;
8717 +       ebrange = au_dbrange_test(dentry);
8718 +       if (!ebrange)
8719 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8720 +
8721 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8722 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8723 +               if (d_really_is_positive(dentry)) {
8724 +                       inode = d_inode(dentry);
8725 +                       err = au_refresh_hinode_self(inode);
8726 +               }
8727 +               au_dbg_verify_dinode(dentry);
8728 +               if (!err)
8729 +                       goto out_dgen; /* success */
8730 +               goto out;
8731 +       }
8732 +
8733 +       /* temporary dinfo */
8734 +       AuDbgDentry(dentry);
8735 +       err = -ENOMEM;
8736 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8737 +       if (unlikely(!tmp))
8738 +               goto out;
8739 +       au_di_swap(tmp, dinfo);
8740 +       /* returns the number of positive dentries */
8741 +       /*
8742 +        * if current working dir is removed, it returns an error.
8743 +        * but the dentry is legal.
8744 +        */
8745 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8746 +       AuDbgDentry(dentry);
8747 +       au_di_swap(tmp, dinfo);
8748 +       if (err == -ENOENT)
8749 +               err = 0;
8750 +       if (err >= 0) {
8751 +               /* compare/refresh by dinfo */
8752 +               AuDbgDentry(dentry);
8753 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8754 +               au_dbg_verify_dinode(dentry);
8755 +               AuTraceErr(err);
8756 +       }
8757 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8758 +       au_rw_write_unlock(&tmp->di_rwsem);
8759 +       au_di_free(tmp);
8760 +       if (unlikely(err))
8761 +               goto out;
8762 +
8763 +out_dgen:
8764 +       au_update_digen(dentry);
8765 +out:
8766 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8767 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8768 +               AuDbgDentry(dentry);
8769 +       }
8770 +       AuTraceErr(err);
8771 +       return err;
8772 +}
8773 +
8774 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8775 +                          struct dentry *dentry, aufs_bindex_t bindex)
8776 +{
8777 +       int err, valid;
8778 +
8779 +       err = 0;
8780 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8781 +               goto out;
8782 +
8783 +       AuDbg("b%d\n", bindex);
8784 +       /*
8785 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8786 +        * due to whiteout and branch permission.
8787 +        */
8788 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8789 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8790 +       /* it may return tri-state */
8791 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8792 +
8793 +       if (unlikely(valid < 0))
8794 +               err = valid;
8795 +       else if (!valid)
8796 +               err = -EINVAL;
8797 +
8798 +out:
8799 +       AuTraceErr(err);
8800 +       return err;
8801 +}
8802 +
8803 +/* todo: remove this */
8804 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8805 +                         unsigned int flags, int do_udba, int dirren)
8806 +{
8807 +       int err;
8808 +       umode_t mode, h_mode;
8809 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8810 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8811 +       struct inode *h_inode, *h_cached_inode;
8812 +       struct dentry *h_dentry;
8813 +       struct qstr *name, *h_name;
8814 +
8815 +       err = 0;
8816 +       plus = 0;
8817 +       mode = 0;
8818 +       ibs = -1;
8819 +       ibe = -1;
8820 +       unhashed = !!d_unhashed(dentry);
8821 +       is_root = !!IS_ROOT(dentry);
8822 +       name = &dentry->d_name;
8823 +       tmpfile = au_di(dentry)->di_tmpfile;
8824 +
8825 +       /*
8826 +        * Theoretically, REVAL test should be unnecessary in case of
8827 +        * {FS,I}NOTIFY.
8828 +        * But {fs,i}notify doesn't fire some necessary events,
8829 +        *      IN_ATTRIB for atime/nlink/pageio
8830 +        * Let's do REVAL test too.
8831 +        */
8832 +       if (do_udba && inode) {
8833 +               mode = (inode->i_mode & S_IFMT);
8834 +               plus = (inode->i_nlink > 0);
8835 +               ibs = au_ibtop(inode);
8836 +               ibe = au_ibbot(inode);
8837 +       }
8838 +
8839 +       btop = au_dbtop(dentry);
8840 +       btail = btop;
8841 +       if (inode && S_ISDIR(inode->i_mode))
8842 +               btail = au_dbtaildir(dentry);
8843 +       for (bindex = btop; bindex <= btail; bindex++) {
8844 +               h_dentry = au_h_dptr(dentry, bindex);
8845 +               if (!h_dentry)
8846 +                       continue;
8847 +
8848 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8849 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8850 +               spin_lock(&h_dentry->d_lock);
8851 +               h_name = &h_dentry->d_name;
8852 +               if (unlikely(do_udba
8853 +                            && !is_root
8854 +                            && ((!h_nfs
8855 +                                 && (unhashed != !!d_unhashed(h_dentry)
8856 +                                     || (!tmpfile && !dirren
8857 +                                         && !au_qstreq(name, h_name))
8858 +                                         ))
8859 +                                || (h_nfs
8860 +                                    && !(flags & LOOKUP_OPEN)
8861 +                                    && (h_dentry->d_flags
8862 +                                        & DCACHE_NFSFS_RENAMED)))
8863 +                           )) {
8864 +                       int h_unhashed;
8865 +
8866 +                       h_unhashed = d_unhashed(h_dentry);
8867 +                       spin_unlock(&h_dentry->d_lock);
8868 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8869 +                             unhashed, h_unhashed, dentry, h_dentry);
8870 +                       goto err;
8871 +               }
8872 +               spin_unlock(&h_dentry->d_lock);
8873 +
8874 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8875 +               if (unlikely(err))
8876 +                       /* do not goto err, to keep the errno */
8877 +                       break;
8878 +
8879 +               /* todo: plink too? */
8880 +               if (!do_udba)
8881 +                       continue;
8882 +
8883 +               /* UDBA tests */
8884 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8885 +                       goto err;
8886 +
8887 +               h_inode = NULL;
8888 +               if (d_is_positive(h_dentry))
8889 +                       h_inode = d_inode(h_dentry);
8890 +               h_plus = plus;
8891 +               h_mode = mode;
8892 +               h_cached_inode = h_inode;
8893 +               if (h_inode) {
8894 +                       h_mode = (h_inode->i_mode & S_IFMT);
8895 +                       h_plus = (h_inode->i_nlink > 0);
8896 +               }
8897 +               if (inode && ibs <= bindex && bindex <= ibe)
8898 +                       h_cached_inode = au_h_iptr(inode, bindex);
8899 +
8900 +               if (!h_nfs) {
8901 +                       if (unlikely(plus != h_plus && !tmpfile))
8902 +                               goto err;
8903 +               } else {
8904 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8905 +                                    && !is_root
8906 +                                    && !IS_ROOT(h_dentry)
8907 +                                    && unhashed != d_unhashed(h_dentry)))
8908 +                               goto err;
8909 +               }
8910 +               if (unlikely(mode != h_mode
8911 +                            || h_cached_inode != h_inode))
8912 +                       goto err;
8913 +               continue;
8914 +
8915 +err:
8916 +               err = -EINVAL;
8917 +               break;
8918 +       }
8919 +
8920 +       AuTraceErr(err);
8921 +       return err;
8922 +}
8923 +
8924 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8925 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8926 +{
8927 +       int err;
8928 +       struct dentry *parent;
8929 +
8930 +       if (!au_digen_test(dentry, sigen))
8931 +               return 0;
8932 +
8933 +       parent = dget_parent(dentry);
8934 +       di_read_lock_parent(parent, AuLock_IR);
8935 +       AuDebugOn(au_digen_test(parent, sigen));
8936 +       au_dbg_verify_gen(parent, sigen);
8937 +       err = au_refresh_dentry(dentry, parent);
8938 +       di_read_unlock(parent, AuLock_IR);
8939 +       dput(parent);
8940 +       AuTraceErr(err);
8941 +       return err;
8942 +}
8943 +
8944 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8945 +{
8946 +       int err;
8947 +       struct dentry *d, *parent;
8948 +
8949 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8950 +               return simple_reval_dpath(dentry, sigen);
8951 +
8952 +       /* slow loop, keep it simple and stupid */
8953 +       /* cf: au_cpup_dirs() */
8954 +       err = 0;
8955 +       parent = NULL;
8956 +       while (au_digen_test(dentry, sigen)) {
8957 +               d = dentry;
8958 +               while (1) {
8959 +                       dput(parent);
8960 +                       parent = dget_parent(d);
8961 +                       if (!au_digen_test(parent, sigen))
8962 +                               break;
8963 +                       d = parent;
8964 +               }
8965 +
8966 +               if (d != dentry)
8967 +                       di_write_lock_child2(d);
8968 +
8969 +               /* someone might update our dentry while we were sleeping */
8970 +               if (au_digen_test(d, sigen)) {
8971 +                       /*
8972 +                        * todo: consolidate with simple_reval_dpath(),
8973 +                        * do_refresh() and au_reval_for_attr().
8974 +                        */
8975 +                       di_read_lock_parent(parent, AuLock_IR);
8976 +                       err = au_refresh_dentry(d, parent);
8977 +                       di_read_unlock(parent, AuLock_IR);
8978 +               }
8979 +
8980 +               if (d != dentry)
8981 +                       di_write_unlock(d);
8982 +               dput(parent);
8983 +               if (unlikely(err))
8984 +                       break;
8985 +       }
8986 +
8987 +       return err;
8988 +}
8989 +
8990 +/*
8991 + * if valid returns 1, otherwise 0.
8992 + */
8993 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8994 +{
8995 +       int valid, err;
8996 +       unsigned int sigen;
8997 +       unsigned char do_udba, dirren;
8998 +       struct super_block *sb;
8999 +       struct inode *inode;
9000 +
9001 +       /* todo: support rcu-walk? */
9002 +       if (flags & LOOKUP_RCU)
9003 +               return -ECHILD;
9004 +
9005 +       valid = 0;
9006 +       if (unlikely(!au_di(dentry)))
9007 +               goto out;
9008 +
9009 +       valid = 1;
9010 +       sb = dentry->d_sb;
9011 +       /*
9012 +        * todo: very ugly
9013 +        * i_mutex of parent dir may be held,
9014 +        * but we should not return 'invalid' due to busy.
9015 +        */
9016 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9017 +       if (unlikely(err)) {
9018 +               valid = err;
9019 +               AuTraceErr(err);
9020 +               goto out;
9021 +       }
9022 +       inode = NULL;
9023 +       if (d_really_is_positive(dentry))
9024 +               inode = d_inode(dentry);
9025 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9026 +               err = -EINVAL;
9027 +               AuTraceErr(err);
9028 +               goto out_dgrade;
9029 +       }
9030 +       if (unlikely(au_dbrange_test(dentry))) {
9031 +               err = -EINVAL;
9032 +               AuTraceErr(err);
9033 +               goto out_dgrade;
9034 +       }
9035 +
9036 +       sigen = au_sigen(sb);
9037 +       if (au_digen_test(dentry, sigen)) {
9038 +               AuDebugOn(IS_ROOT(dentry));
9039 +               err = au_reval_dpath(dentry, sigen);
9040 +               if (unlikely(err)) {
9041 +                       AuTraceErr(err);
9042 +                       goto out_dgrade;
9043 +               }
9044 +       }
9045 +       di_downgrade_lock(dentry, AuLock_IR);
9046 +
9047 +       err = -EINVAL;
9048 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9049 +           && inode
9050 +           && !(inode->i_state && I_LINKABLE)
9051 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9052 +               AuTraceErr(err);
9053 +               goto out_inval;
9054 +       }
9055 +
9056 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9057 +       if (do_udba && inode) {
9058 +               aufs_bindex_t btop = au_ibtop(inode);
9059 +               struct inode *h_inode;
9060 +
9061 +               if (btop >= 0) {
9062 +                       h_inode = au_h_iptr(inode, btop);
9063 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9064 +                               AuTraceErr(err);
9065 +                               goto out_inval;
9066 +                       }
9067 +               }
9068 +       }
9069 +
9070 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9071 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9072 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9073 +               err = -EIO;
9074 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9075 +                     dentry, err);
9076 +       }
9077 +       goto out_inval;
9078 +
9079 +out_dgrade:
9080 +       di_downgrade_lock(dentry, AuLock_IR);
9081 +out_inval:
9082 +       aufs_read_unlock(dentry, AuLock_IR);
9083 +       AuTraceErr(err);
9084 +       valid = !err;
9085 +out:
9086 +       if (!valid) {
9087 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9088 +               d_drop(dentry);
9089 +       }
9090 +       return valid;
9091 +}
9092 +
9093 +static void aufs_d_release(struct dentry *dentry)
9094 +{
9095 +       if (au_di(dentry)) {
9096 +               au_di_fin(dentry);
9097 +               au_hn_di_reinit(dentry);
9098 +       }
9099 +}
9100 +
9101 +const struct dentry_operations aufs_dop = {
9102 +       .d_revalidate           = aufs_d_revalidate,
9103 +       .d_weak_revalidate      = aufs_d_revalidate,
9104 +       .d_release              = aufs_d_release
9105 +};
9106 +
9107 +/* aufs_dop without d_revalidate */
9108 +const struct dentry_operations aufs_dop_noreval = {
9109 +       .d_release              = aufs_d_release
9110 +};
9111 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9112 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9113 +++ linux/fs/aufs/dentry.h      2021-12-03 15:40:58.233313963 +0100
9114 @@ -0,0 +1,269 @@
9115 +/* SPDX-License-Identifier: GPL-2.0 */
9116 +/*
9117 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9118 + *
9119 + * This program, aufs is free software; you can redistribute it and/or modify
9120 + * it under the terms of the GNU General Public License as published by
9121 + * the Free Software Foundation; either version 2 of the License, or
9122 + * (at your option) any later version.
9123 + *
9124 + * This program is distributed in the hope that it will be useful,
9125 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9126 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9127 + * GNU General Public License for more details.
9128 + *
9129 + * You should have received a copy of the GNU General Public License
9130 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9131 + */
9132 +
9133 +/*
9134 + * lookup and dentry operations
9135 + */
9136 +
9137 +#ifndef __AUFS_DENTRY_H__
9138 +#define __AUFS_DENTRY_H__
9139 +
9140 +#ifdef __KERNEL__
9141 +
9142 +#include <linux/dcache.h>
9143 +#include "dirren.h"
9144 +#include "rwsem.h"
9145 +
9146 +struct au_hdentry {
9147 +       struct dentry           *hd_dentry;
9148 +       aufs_bindex_t           hd_id;
9149 +};
9150 +
9151 +struct au_dinfo {
9152 +       atomic_t                di_generation;
9153 +
9154 +       struct au_rwsem         di_rwsem;
9155 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9156 +       unsigned char           di_tmpfile; /* to allow the different name */
9157 +       struct au_hdentry       *di_hdentry;
9158 +       struct rcu_head         rcu;
9159 +} ____cacheline_aligned_in_smp;
9160 +
9161 +/* ---------------------------------------------------------------------- */
9162 +
9163 +/* flags for au_lkup_dentry() */
9164 +#define AuLkup_ALLOW_NEG       1
9165 +#define AuLkup_IGNORE_PERM     (1 << 1)
9166 +#define AuLkup_DIRREN          (1 << 2)
9167 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9168 +#define au_fset_lkup(flags, name) \
9169 +       do { (flags) |= AuLkup_##name; } while (0)
9170 +#define au_fclr_lkup(flags, name) \
9171 +       do { (flags) &= ~AuLkup_##name; } while (0)
9172 +
9173 +#ifndef CONFIG_AUFS_DIRREN
9174 +#undef AuLkup_DIRREN
9175 +#define AuLkup_DIRREN 0
9176 +#endif
9177 +
9178 +struct au_do_lookup_args {
9179 +       unsigned int            flags;
9180 +       mode_t                  type;
9181 +       struct qstr             whname, *name;
9182 +       struct au_dr_lookup     dirren;
9183 +};
9184 +
9185 +/* ---------------------------------------------------------------------- */
9186 +
9187 +/* dentry.c */
9188 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9189 +struct au_branch;
9190 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9191 +                              struct path *ppath);
9192 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9193 +               struct dentry *h_parent, struct au_branch *br);
9194 +
9195 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9196 +                  unsigned int flags);
9197 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9198 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9199 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9200 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9201 +
9202 +/* dinfo.c */
9203 +void au_di_init_once(void *_di);
9204 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9205 +void au_di_free(struct au_dinfo *dinfo);
9206 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9207 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9208 +int au_di_init(struct dentry *dentry);
9209 +void au_di_fin(struct dentry *dentry);
9210 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9211 +
9212 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9213 +void di_read_unlock(struct dentry *d, int flags);
9214 +void di_downgrade_lock(struct dentry *d, int flags);
9215 +void di_write_lock(struct dentry *d, unsigned int lsc);
9216 +void di_write_unlock(struct dentry *d);
9217 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9218 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9219 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9220 +
9221 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9222 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9223 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9224 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9225 +
9226 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9227 +                  struct dentry *h_dentry);
9228 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9229 +int au_dbrange_test(struct dentry *dentry);
9230 +void au_update_digen(struct dentry *dentry);
9231 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9232 +void au_update_dbtop(struct dentry *dentry);
9233 +void au_update_dbbot(struct dentry *dentry);
9234 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9235 +
9236 +/* ---------------------------------------------------------------------- */
9237 +
9238 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9239 +{
9240 +       return dentry->d_fsdata;
9241 +}
9242 +
9243 +/* ---------------------------------------------------------------------- */
9244 +
9245 +/* lock subclass for dinfo */
9246 +enum {
9247 +       AuLsc_DI_CHILD,         /* child first */
9248 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9249 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9250 +       AuLsc_DI_PARENT,
9251 +       AuLsc_DI_PARENT2,
9252 +       AuLsc_DI_PARENT3,
9253 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9254 +};
9255 +
9256 +/*
9257 + * di_read_lock_child, di_write_lock_child,
9258 + * di_read_lock_child2, di_write_lock_child2,
9259 + * di_read_lock_child3, di_write_lock_child3,
9260 + * di_read_lock_parent, di_write_lock_parent,
9261 + * di_read_lock_parent2, di_write_lock_parent2,
9262 + * di_read_lock_parent3, di_write_lock_parent3,
9263 + */
9264 +#define AuReadLockFunc(name, lsc) \
9265 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9266 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9267 +
9268 +#define AuWriteLockFunc(name, lsc) \
9269 +static inline void di_write_lock_##name(struct dentry *d) \
9270 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9271 +
9272 +#define AuRWLockFuncs(name, lsc) \
9273 +       AuReadLockFunc(name, lsc) \
9274 +       AuWriteLockFunc(name, lsc)
9275 +
9276 +AuRWLockFuncs(child, CHILD);
9277 +AuRWLockFuncs(child2, CHILD2);
9278 +AuRWLockFuncs(child3, CHILD3);
9279 +AuRWLockFuncs(parent, PARENT);
9280 +AuRWLockFuncs(parent2, PARENT2);
9281 +AuRWLockFuncs(parent3, PARENT3);
9282 +
9283 +#undef AuReadLockFunc
9284 +#undef AuWriteLockFunc
9285 +#undef AuRWLockFuncs
9286 +
9287 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9288 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9289 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9290 +
9291 +/* ---------------------------------------------------------------------- */
9292 +
9293 +/* todo: memory barrier? */
9294 +static inline unsigned int au_digen(struct dentry *d)
9295 +{
9296 +       return atomic_read(&au_di(d)->di_generation);
9297 +}
9298 +
9299 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9300 +{
9301 +       hdentry->hd_dentry = NULL;
9302 +}
9303 +
9304 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9305 +                                           aufs_bindex_t bindex)
9306 +{
9307 +       return di->di_hdentry + bindex;
9308 +}
9309 +
9310 +static inline void au_hdput(struct au_hdentry *hd)
9311 +{
9312 +       if (hd)
9313 +               dput(hd->hd_dentry);
9314 +}
9315 +
9316 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9317 +{
9318 +       DiMustAnyLock(dentry);
9319 +       return au_di(dentry)->di_btop;
9320 +}
9321 +
9322 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9323 +{
9324 +       DiMustAnyLock(dentry);
9325 +       return au_di(dentry)->di_bbot;
9326 +}
9327 +
9328 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9329 +{
9330 +       DiMustAnyLock(dentry);
9331 +       return au_di(dentry)->di_bwh;
9332 +}
9333 +
9334 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9335 +{
9336 +       DiMustAnyLock(dentry);
9337 +       return au_di(dentry)->di_bdiropq;
9338 +}
9339 +
9340 +/* todo: hard/soft set? */
9341 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9342 +{
9343 +       DiMustWriteLock(dentry);
9344 +       au_di(dentry)->di_btop = bindex;
9345 +}
9346 +
9347 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9348 +{
9349 +       DiMustWriteLock(dentry);
9350 +       au_di(dentry)->di_bbot = bindex;
9351 +}
9352 +
9353 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9354 +{
9355 +       DiMustWriteLock(dentry);
9356 +       /* dbwh can be outside of btop - bbot range */
9357 +       au_di(dentry)->di_bwh = bindex;
9358 +}
9359 +
9360 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9361 +{
9362 +       DiMustWriteLock(dentry);
9363 +       au_di(dentry)->di_bdiropq = bindex;
9364 +}
9365 +
9366 +/* ---------------------------------------------------------------------- */
9367 +
9368 +#ifdef CONFIG_AUFS_HNOTIFY
9369 +static inline void au_digen_dec(struct dentry *d)
9370 +{
9371 +       atomic_dec(&au_di(d)->di_generation);
9372 +}
9373 +
9374 +static inline void au_hn_di_reinit(struct dentry *dentry)
9375 +{
9376 +       dentry->d_fsdata = NULL;
9377 +}
9378 +#else
9379 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9380 +#endif /* CONFIG_AUFS_HNOTIFY */
9381 +
9382 +#endif /* __KERNEL__ */
9383 +#endif /* __AUFS_DENTRY_H__ */
9384 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9385 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9386 +++ linux/fs/aufs/dinfo.c       2021-12-03 15:38:59.933313976 +0100
9387 @@ -0,0 +1,554 @@
9388 +// SPDX-License-Identifier: GPL-2.0
9389 +/*
9390 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9391 + *
9392 + * This program, aufs is free software; you can redistribute it and/or modify
9393 + * it under the terms of the GNU General Public License as published by
9394 + * the Free Software Foundation; either version 2 of the License, or
9395 + * (at your option) any later version.
9396 + *
9397 + * This program is distributed in the hope that it will be useful,
9398 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9399 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9400 + * GNU General Public License for more details.
9401 + *
9402 + * You should have received a copy of the GNU General Public License
9403 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9404 + */
9405 +
9406 +/*
9407 + * dentry private data
9408 + */
9409 +
9410 +#include "aufs.h"
9411 +
9412 +void au_di_init_once(void *_dinfo)
9413 +{
9414 +       struct au_dinfo *dinfo = _dinfo;
9415 +
9416 +       au_rw_init(&dinfo->di_rwsem);
9417 +}
9418 +
9419 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9420 +{
9421 +       struct au_dinfo *dinfo;
9422 +       int nbr, i;
9423 +
9424 +       dinfo = au_cache_alloc_dinfo();
9425 +       if (unlikely(!dinfo))
9426 +               goto out;
9427 +
9428 +       nbr = au_sbbot(sb) + 1;
9429 +       if (nbr <= 0)
9430 +               nbr = 1;
9431 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9432 +       if (dinfo->di_hdentry) {
9433 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9434 +               dinfo->di_btop = -1;
9435 +               dinfo->di_bbot = -1;
9436 +               dinfo->di_bwh = -1;
9437 +               dinfo->di_bdiropq = -1;
9438 +               dinfo->di_tmpfile = 0;
9439 +               for (i = 0; i < nbr; i++)
9440 +                       dinfo->di_hdentry[i].hd_id = -1;
9441 +               goto out;
9442 +       }
9443 +
9444 +       au_cache_free_dinfo(dinfo);
9445 +       dinfo = NULL;
9446 +
9447 +out:
9448 +       return dinfo;
9449 +}
9450 +
9451 +void au_di_free(struct au_dinfo *dinfo)
9452 +{
9453 +       struct au_hdentry *p;
9454 +       aufs_bindex_t bbot, bindex;
9455 +
9456 +       /* dentry may not be revalidated */
9457 +       bindex = dinfo->di_btop;
9458 +       if (bindex >= 0) {
9459 +               bbot = dinfo->di_bbot;
9460 +               p = au_hdentry(dinfo, bindex);
9461 +               while (bindex++ <= bbot)
9462 +                       au_hdput(p++);
9463 +       }
9464 +       au_kfree_try_rcu(dinfo->di_hdentry);
9465 +       au_cache_free_dinfo(dinfo);
9466 +}
9467 +
9468 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9469 +{
9470 +       struct au_hdentry *p;
9471 +       aufs_bindex_t bi;
9472 +
9473 +       AuRwMustWriteLock(&a->di_rwsem);
9474 +       AuRwMustWriteLock(&b->di_rwsem);
9475 +
9476 +#define DiSwap(v, name)                                \
9477 +       do {                                    \
9478 +               v = a->di_##name;               \
9479 +               a->di_##name = b->di_##name;    \
9480 +               b->di_##name = v;               \
9481 +       } while (0)
9482 +
9483 +       DiSwap(p, hdentry);
9484 +       DiSwap(bi, btop);
9485 +       DiSwap(bi, bbot);
9486 +       DiSwap(bi, bwh);
9487 +       DiSwap(bi, bdiropq);
9488 +       /* smp_mb(); */
9489 +
9490 +#undef DiSwap
9491 +}
9492 +
9493 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9494 +{
9495 +       AuRwMustWriteLock(&dst->di_rwsem);
9496 +       AuRwMustWriteLock(&src->di_rwsem);
9497 +
9498 +       dst->di_btop = src->di_btop;
9499 +       dst->di_bbot = src->di_bbot;
9500 +       dst->di_bwh = src->di_bwh;
9501 +       dst->di_bdiropq = src->di_bdiropq;
9502 +       /* smp_mb(); */
9503 +}
9504 +
9505 +int au_di_init(struct dentry *dentry)
9506 +{
9507 +       int err;
9508 +       struct super_block *sb;
9509 +       struct au_dinfo *dinfo;
9510 +
9511 +       err = 0;
9512 +       sb = dentry->d_sb;
9513 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9514 +       if (dinfo) {
9515 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9516 +               /* smp_mb(); */ /* atomic_set */
9517 +               dentry->d_fsdata = dinfo;
9518 +       } else
9519 +               err = -ENOMEM;
9520 +
9521 +       return err;
9522 +}
9523 +
9524 +void au_di_fin(struct dentry *dentry)
9525 +{
9526 +       struct au_dinfo *dinfo;
9527 +
9528 +       dinfo = au_di(dentry);
9529 +       AuRwDestroy(&dinfo->di_rwsem);
9530 +       au_di_free(dinfo);
9531 +}
9532 +
9533 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9534 +{
9535 +       int err, sz;
9536 +       struct au_hdentry *hdp;
9537 +
9538 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9539 +
9540 +       err = -ENOMEM;
9541 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9542 +       if (!sz)
9543 +               sz = sizeof(*hdp);
9544 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9545 +                          may_shrink);
9546 +       if (hdp) {
9547 +               dinfo->di_hdentry = hdp;
9548 +               err = 0;
9549 +       }
9550 +
9551 +       return err;
9552 +}
9553 +
9554 +/* ---------------------------------------------------------------------- */
9555 +
9556 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9557 +{
9558 +       switch (lsc) {
9559 +       case AuLsc_DI_CHILD:
9560 +               ii_write_lock_child(inode);
9561 +               break;
9562 +       case AuLsc_DI_CHILD2:
9563 +               ii_write_lock_child2(inode);
9564 +               break;
9565 +       case AuLsc_DI_CHILD3:
9566 +               ii_write_lock_child3(inode);
9567 +               break;
9568 +       case AuLsc_DI_PARENT:
9569 +               ii_write_lock_parent(inode);
9570 +               break;
9571 +       case AuLsc_DI_PARENT2:
9572 +               ii_write_lock_parent2(inode);
9573 +               break;
9574 +       case AuLsc_DI_PARENT3:
9575 +               ii_write_lock_parent3(inode);
9576 +               break;
9577 +       default:
9578 +               BUG();
9579 +       }
9580 +}
9581 +
9582 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9583 +{
9584 +       switch (lsc) {
9585 +       case AuLsc_DI_CHILD:
9586 +               ii_read_lock_child(inode);
9587 +               break;
9588 +       case AuLsc_DI_CHILD2:
9589 +               ii_read_lock_child2(inode);
9590 +               break;
9591 +       case AuLsc_DI_CHILD3:
9592 +               ii_read_lock_child3(inode);
9593 +               break;
9594 +       case AuLsc_DI_PARENT:
9595 +               ii_read_lock_parent(inode);
9596 +               break;
9597 +       case AuLsc_DI_PARENT2:
9598 +               ii_read_lock_parent2(inode);
9599 +               break;
9600 +       case AuLsc_DI_PARENT3:
9601 +               ii_read_lock_parent3(inode);
9602 +               break;
9603 +       default:
9604 +               BUG();
9605 +       }
9606 +}
9607 +
9608 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9609 +{
9610 +       struct inode *inode;
9611 +
9612 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9613 +       if (d_really_is_positive(d)) {
9614 +               inode = d_inode(d);
9615 +               if (au_ftest_lock(flags, IW))
9616 +                       do_ii_write_lock(inode, lsc);
9617 +               else if (au_ftest_lock(flags, IR))
9618 +                       do_ii_read_lock(inode, lsc);
9619 +       }
9620 +}
9621 +
9622 +void di_read_unlock(struct dentry *d, int flags)
9623 +{
9624 +       struct inode *inode;
9625 +
9626 +       if (d_really_is_positive(d)) {
9627 +               inode = d_inode(d);
9628 +               if (au_ftest_lock(flags, IW)) {
9629 +                       au_dbg_verify_dinode(d);
9630 +                       ii_write_unlock(inode);
9631 +               } else if (au_ftest_lock(flags, IR)) {
9632 +                       au_dbg_verify_dinode(d);
9633 +                       ii_read_unlock(inode);
9634 +               }
9635 +       }
9636 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9637 +}
9638 +
9639 +void di_downgrade_lock(struct dentry *d, int flags)
9640 +{
9641 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9642 +               ii_downgrade_lock(d_inode(d));
9643 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9644 +}
9645 +
9646 +void di_write_lock(struct dentry *d, unsigned int lsc)
9647 +{
9648 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9649 +       if (d_really_is_positive(d))
9650 +               do_ii_write_lock(d_inode(d), lsc);
9651 +}
9652 +
9653 +void di_write_unlock(struct dentry *d)
9654 +{
9655 +       au_dbg_verify_dinode(d);
9656 +       if (d_really_is_positive(d))
9657 +               ii_write_unlock(d_inode(d));
9658 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9659 +}
9660 +
9661 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9662 +{
9663 +       AuDebugOn(d1 == d2
9664 +                 || d_inode(d1) == d_inode(d2)
9665 +                 || d1->d_sb != d2->d_sb);
9666 +
9667 +       if ((isdir && au_test_subdir(d1, d2))
9668 +           || d1 < d2) {
9669 +               di_write_lock_child(d1);
9670 +               di_write_lock_child2(d2);
9671 +       } else {
9672 +               di_write_lock_child(d2);
9673 +               di_write_lock_child2(d1);
9674 +       }
9675 +}
9676 +
9677 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9678 +{
9679 +       AuDebugOn(d1 == d2
9680 +                 || d_inode(d1) == d_inode(d2)
9681 +                 || d1->d_sb != d2->d_sb);
9682 +
9683 +       if ((isdir && au_test_subdir(d1, d2))
9684 +           || d1 < d2) {
9685 +               di_write_lock_parent(d1);
9686 +               di_write_lock_parent2(d2);
9687 +       } else {
9688 +               di_write_lock_parent(d2);
9689 +               di_write_lock_parent2(d1);
9690 +       }
9691 +}
9692 +
9693 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9694 +{
9695 +       di_write_unlock(d1);
9696 +       if (d_inode(d1) == d_inode(d2))
9697 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9698 +       else
9699 +               di_write_unlock(d2);
9700 +}
9701 +
9702 +/* ---------------------------------------------------------------------- */
9703 +
9704 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9705 +{
9706 +       struct dentry *d;
9707 +
9708 +       DiMustAnyLock(dentry);
9709 +
9710 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9711 +               return NULL;
9712 +       AuDebugOn(bindex < 0);
9713 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9714 +       AuDebugOn(d && au_dcount(d) <= 0);
9715 +       return d;
9716 +}
9717 +
9718 +/*
9719 + * extended version of au_h_dptr().
9720 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9721 + * error.
9722 + */
9723 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9724 +{
9725 +       struct dentry *h_dentry;
9726 +       struct inode *inode, *h_inode;
9727 +
9728 +       AuDebugOn(d_really_is_negative(dentry));
9729 +
9730 +       h_dentry = NULL;
9731 +       if (au_dbtop(dentry) <= bindex
9732 +           && bindex <= au_dbbot(dentry))
9733 +               h_dentry = au_h_dptr(dentry, bindex);
9734 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9735 +               dget(h_dentry);
9736 +               goto out; /* success */
9737 +       }
9738 +
9739 +       inode = d_inode(dentry);
9740 +       AuDebugOn(bindex < au_ibtop(inode));
9741 +       AuDebugOn(au_ibbot(inode) < bindex);
9742 +       h_inode = au_h_iptr(inode, bindex);
9743 +       h_dentry = d_find_alias(h_inode);
9744 +       if (h_dentry) {
9745 +               if (!IS_ERR(h_dentry)) {
9746 +                       if (!au_d_linkable(h_dentry))
9747 +                               goto out; /* success */
9748 +                       dput(h_dentry);
9749 +               } else
9750 +                       goto out;
9751 +       }
9752 +
9753 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9754 +               h_dentry = au_plink_lkup(inode, bindex);
9755 +               AuDebugOn(!h_dentry);
9756 +               if (!IS_ERR(h_dentry)) {
9757 +                       if (!au_d_hashed_positive(h_dentry))
9758 +                               goto out; /* success */
9759 +                       dput(h_dentry);
9760 +                       h_dentry = NULL;
9761 +               }
9762 +       }
9763 +
9764 +out:
9765 +       AuDbgDentry(h_dentry);
9766 +       return h_dentry;
9767 +}
9768 +
9769 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9770 +{
9771 +       aufs_bindex_t bbot, bwh;
9772 +
9773 +       bbot = au_dbbot(dentry);
9774 +       if (0 <= bbot) {
9775 +               bwh = au_dbwh(dentry);
9776 +               if (!bwh)
9777 +                       return bwh;
9778 +               if (0 < bwh && bwh < bbot)
9779 +                       return bwh - 1;
9780 +       }
9781 +       return bbot;
9782 +}
9783 +
9784 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9785 +{
9786 +       aufs_bindex_t bbot, bopq;
9787 +
9788 +       bbot = au_dbtail(dentry);
9789 +       if (0 <= bbot) {
9790 +               bopq = au_dbdiropq(dentry);
9791 +               if (0 <= bopq && bopq < bbot)
9792 +                       bbot = bopq;
9793 +       }
9794 +       return bbot;
9795 +}
9796 +
9797 +/* ---------------------------------------------------------------------- */
9798 +
9799 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9800 +                  struct dentry *h_dentry)
9801 +{
9802 +       struct au_dinfo *dinfo;
9803 +       struct au_hdentry *hd;
9804 +       struct au_branch *br;
9805 +
9806 +       DiMustWriteLock(dentry);
9807 +
9808 +       dinfo = au_di(dentry);
9809 +       hd = au_hdentry(dinfo, bindex);
9810 +       au_hdput(hd);
9811 +       hd->hd_dentry = h_dentry;
9812 +       if (h_dentry) {
9813 +               br = au_sbr(dentry->d_sb, bindex);
9814 +               hd->hd_id = br->br_id;
9815 +       }
9816 +}
9817 +
9818 +int au_dbrange_test(struct dentry *dentry)
9819 +{
9820 +       int err;
9821 +       aufs_bindex_t btop, bbot;
9822 +
9823 +       err = 0;
9824 +       btop = au_dbtop(dentry);
9825 +       bbot = au_dbbot(dentry);
9826 +       if (btop >= 0)
9827 +               AuDebugOn(bbot < 0 && btop > bbot);
9828 +       else {
9829 +               err = -EIO;
9830 +               AuDebugOn(bbot >= 0);
9831 +       }
9832 +
9833 +       return err;
9834 +}
9835 +
9836 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9837 +{
9838 +       int err;
9839 +
9840 +       err = 0;
9841 +       if (unlikely(au_digen(dentry) != sigen
9842 +                    || au_iigen_test(d_inode(dentry), sigen)))
9843 +               err = -EIO;
9844 +
9845 +       return err;
9846 +}
9847 +
9848 +void au_update_digen(struct dentry *dentry)
9849 +{
9850 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9851 +       /* smp_mb(); */ /* atomic_set */
9852 +}
9853 +
9854 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9855 +{
9856 +       struct au_dinfo *dinfo;
9857 +       struct dentry *h_d;
9858 +       struct au_hdentry *hdp;
9859 +       aufs_bindex_t bindex, bbot;
9860 +
9861 +       DiMustWriteLock(dentry);
9862 +
9863 +       dinfo = au_di(dentry);
9864 +       if (!dinfo || dinfo->di_btop < 0)
9865 +               return;
9866 +
9867 +       if (do_put_zero) {
9868 +               bbot = dinfo->di_bbot;
9869 +               bindex = dinfo->di_btop;
9870 +               hdp = au_hdentry(dinfo, bindex);
9871 +               for (; bindex <= bbot; bindex++, hdp++) {
9872 +                       h_d = hdp->hd_dentry;
9873 +                       if (h_d && d_is_negative(h_d))
9874 +                               au_set_h_dptr(dentry, bindex, NULL);
9875 +               }
9876 +       }
9877 +
9878 +       dinfo->di_btop = 0;
9879 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9880 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9881 +               if (hdp->hd_dentry)
9882 +                       break;
9883 +       if (dinfo->di_btop > dinfo->di_bbot) {
9884 +               dinfo->di_btop = -1;
9885 +               dinfo->di_bbot = -1;
9886 +               return;
9887 +       }
9888 +
9889 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9890 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9891 +               if (hdp->hd_dentry)
9892 +                       break;
9893 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9894 +}
9895 +
9896 +void au_update_dbtop(struct dentry *dentry)
9897 +{
9898 +       aufs_bindex_t bindex, bbot;
9899 +       struct dentry *h_dentry;
9900 +
9901 +       bbot = au_dbbot(dentry);
9902 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9903 +               h_dentry = au_h_dptr(dentry, bindex);
9904 +               if (!h_dentry)
9905 +                       continue;
9906 +               if (d_is_positive(h_dentry)) {
9907 +                       au_set_dbtop(dentry, bindex);
9908 +                       return;
9909 +               }
9910 +               au_set_h_dptr(dentry, bindex, NULL);
9911 +       }
9912 +}
9913 +
9914 +void au_update_dbbot(struct dentry *dentry)
9915 +{
9916 +       aufs_bindex_t bindex, btop;
9917 +       struct dentry *h_dentry;
9918 +
9919 +       btop = au_dbtop(dentry);
9920 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9921 +               h_dentry = au_h_dptr(dentry, bindex);
9922 +               if (!h_dentry)
9923 +                       continue;
9924 +               if (d_is_positive(h_dentry)) {
9925 +                       au_set_dbbot(dentry, bindex);
9926 +                       return;
9927 +               }
9928 +               au_set_h_dptr(dentry, bindex, NULL);
9929 +       }
9930 +}
9931 +
9932 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9933 +{
9934 +       aufs_bindex_t bindex, bbot;
9935 +
9936 +       bbot = au_dbbot(dentry);
9937 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9938 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9939 +                       return bindex;
9940 +       return -1;
9941 +}
9942 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9943 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9944 +++ linux/fs/aufs/dir.c 2021-12-03 15:40:58.233313963 +0100
9945 @@ -0,0 +1,765 @@
9946 +// SPDX-License-Identifier: GPL-2.0
9947 +/*
9948 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9949 + *
9950 + * This program, aufs is free software; you can redistribute it and/or modify
9951 + * it under the terms of the GNU General Public License as published by
9952 + * the Free Software Foundation; either version 2 of the License, or
9953 + * (at your option) any later version.
9954 + *
9955 + * This program is distributed in the hope that it will be useful,
9956 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9957 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9958 + * GNU General Public License for more details.
9959 + *
9960 + * You should have received a copy of the GNU General Public License
9961 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9962 + */
9963 +
9964 +/*
9965 + * directory operations
9966 + */
9967 +
9968 +#include <linux/fs_stack.h>
9969 +#include <linux/iversion.h>
9970 +#include "aufs.h"
9971 +
9972 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9973 +{
9974 +       unsigned int nlink;
9975 +
9976 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9977 +
9978 +       nlink = dir->i_nlink;
9979 +       nlink += h_dir->i_nlink - 2;
9980 +       if (h_dir->i_nlink < 2)
9981 +               nlink += 2;
9982 +       smp_mb(); /* for i_nlink */
9983 +       /* 0 can happen in revaliding */
9984 +       set_nlink(dir, nlink);
9985 +}
9986 +
9987 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9988 +{
9989 +       unsigned int nlink;
9990 +
9991 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9992 +
9993 +       nlink = dir->i_nlink;
9994 +       nlink -= h_dir->i_nlink - 2;
9995 +       if (h_dir->i_nlink < 2)
9996 +               nlink -= 2;
9997 +       smp_mb(); /* for i_nlink */
9998 +       /* nlink == 0 means the branch-fs is broken */
9999 +       set_nlink(dir, nlink);
10000 +}
10001 +
10002 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10003 +{
10004 +       loff_t sz;
10005 +       aufs_bindex_t bindex, bbot;
10006 +       struct file *h_file;
10007 +       struct dentry *h_dentry;
10008 +
10009 +       sz = 0;
10010 +       if (file) {
10011 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10012 +
10013 +               bbot = au_fbbot_dir(file);
10014 +               for (bindex = au_fbtop(file);
10015 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10016 +                    bindex++) {
10017 +                       h_file = au_hf_dir(file, bindex);
10018 +                       if (h_file && file_inode(h_file))
10019 +                               sz += vfsub_f_size_read(h_file);
10020 +               }
10021 +       } else {
10022 +               AuDebugOn(!dentry);
10023 +               AuDebugOn(!d_is_dir(dentry));
10024 +
10025 +               bbot = au_dbtaildir(dentry);
10026 +               for (bindex = au_dbtop(dentry);
10027 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10028 +                    bindex++) {
10029 +                       h_dentry = au_h_dptr(dentry, bindex);
10030 +                       if (h_dentry && d_is_positive(h_dentry))
10031 +                               sz += i_size_read(d_inode(h_dentry));
10032 +               }
10033 +       }
10034 +       if (sz < KMALLOC_MAX_SIZE)
10035 +               sz = roundup_pow_of_two(sz);
10036 +       if (sz > KMALLOC_MAX_SIZE)
10037 +               sz = KMALLOC_MAX_SIZE;
10038 +       else if (sz < NAME_MAX) {
10039 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10040 +               sz = AUFS_RDBLK_DEF;
10041 +       }
10042 +       return sz;
10043 +}
10044 +
10045 +struct au_dir_ts_arg {
10046 +       struct dentry *dentry;
10047 +       aufs_bindex_t brid;
10048 +};
10049 +
10050 +static void au_do_dir_ts(void *arg)
10051 +{
10052 +       struct au_dir_ts_arg *a = arg;
10053 +       struct au_dtime dt;
10054 +       struct path h_path;
10055 +       struct inode *dir, *h_dir;
10056 +       struct super_block *sb;
10057 +       struct au_branch *br;
10058 +       struct au_hinode *hdir;
10059 +       int err;
10060 +       aufs_bindex_t btop, bindex;
10061 +
10062 +       sb = a->dentry->d_sb;
10063 +       if (d_really_is_negative(a->dentry))
10064 +               goto out;
10065 +       /* no dir->i_mutex lock */
10066 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10067 +
10068 +       dir = d_inode(a->dentry);
10069 +       btop = au_ibtop(dir);
10070 +       bindex = au_br_index(sb, a->brid);
10071 +       if (bindex < btop)
10072 +               goto out_unlock;
10073 +
10074 +       br = au_sbr(sb, bindex);
10075 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10076 +       if (!h_path.dentry)
10077 +               goto out_unlock;
10078 +       h_path.mnt = au_br_mnt(br);
10079 +       au_dtime_store(&dt, a->dentry, &h_path);
10080 +
10081 +       br = au_sbr(sb, btop);
10082 +       if (!au_br_writable(br->br_perm))
10083 +               goto out_unlock;
10084 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10085 +       h_path.mnt = au_br_mnt(br);
10086 +       err = vfsub_mnt_want_write(h_path.mnt);
10087 +       if (err)
10088 +               goto out_unlock;
10089 +       hdir = au_hi(dir, btop);
10090 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10091 +       h_dir = au_h_iptr(dir, btop);
10092 +       if (h_dir->i_nlink
10093 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10094 +               dt.dt_h_path = h_path;
10095 +               au_dtime_revert(&dt);
10096 +       }
10097 +       au_hn_inode_unlock(hdir);
10098 +       vfsub_mnt_drop_write(h_path.mnt);
10099 +       au_cpup_attr_timesizes(dir);
10100 +
10101 +out_unlock:
10102 +       aufs_read_unlock(a->dentry, AuLock_DW);
10103 +out:
10104 +       dput(a->dentry);
10105 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10106 +       au_kfree_try_rcu(arg);
10107 +}
10108 +
10109 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10110 +{
10111 +       int perm, wkq_err;
10112 +       aufs_bindex_t btop;
10113 +       struct au_dir_ts_arg *arg;
10114 +       struct dentry *dentry;
10115 +       struct super_block *sb;
10116 +
10117 +       IMustLock(dir);
10118 +
10119 +       dentry = d_find_any_alias(dir);
10120 +       AuDebugOn(!dentry);
10121 +       sb = dentry->d_sb;
10122 +       btop = au_ibtop(dir);
10123 +       if (btop == bindex) {
10124 +               au_cpup_attr_timesizes(dir);
10125 +               goto out;
10126 +       }
10127 +
10128 +       perm = au_sbr_perm(sb, btop);
10129 +       if (!au_br_writable(perm))
10130 +               goto out;
10131 +
10132 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10133 +       if (!arg)
10134 +               goto out;
10135 +
10136 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10137 +       arg->brid = au_sbr_id(sb, bindex);
10138 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10139 +       if (unlikely(wkq_err)) {
10140 +               pr_err("wkq %d\n", wkq_err);
10141 +               dput(dentry);
10142 +               au_kfree_try_rcu(arg);
10143 +       }
10144 +
10145 +out:
10146 +       dput(dentry);
10147 +}
10148 +
10149 +/* ---------------------------------------------------------------------- */
10150 +
10151 +static int reopen_dir(struct file *file)
10152 +{
10153 +       int err;
10154 +       unsigned int flags;
10155 +       aufs_bindex_t bindex, btail, btop;
10156 +       struct dentry *dentry, *h_dentry;
10157 +       struct file *h_file;
10158 +
10159 +       /* open all lower dirs */
10160 +       dentry = file->f_path.dentry;
10161 +       btop = au_dbtop(dentry);
10162 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10163 +               au_set_h_fptr(file, bindex, NULL);
10164 +       au_set_fbtop(file, btop);
10165 +
10166 +       btail = au_dbtaildir(dentry);
10167 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10168 +               au_set_h_fptr(file, bindex, NULL);
10169 +       au_set_fbbot_dir(file, btail);
10170 +
10171 +       flags = vfsub_file_flags(file);
10172 +       for (bindex = btop; bindex <= btail; bindex++) {
10173 +               h_dentry = au_h_dptr(dentry, bindex);
10174 +               if (!h_dentry)
10175 +                       continue;
10176 +               h_file = au_hf_dir(file, bindex);
10177 +               if (h_file)
10178 +                       continue;
10179 +
10180 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10181 +               err = PTR_ERR(h_file);
10182 +               if (IS_ERR(h_file))
10183 +                       goto out; /* close all? */
10184 +               au_set_h_fptr(file, bindex, h_file);
10185 +       }
10186 +       au_update_figen(file);
10187 +       /* todo: necessary? */
10188 +       /* file->f_ra = h_file->f_ra; */
10189 +       err = 0;
10190 +
10191 +out:
10192 +       return err;
10193 +}
10194 +
10195 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10196 +{
10197 +       int err;
10198 +       aufs_bindex_t bindex, btail;
10199 +       struct dentry *dentry, *h_dentry;
10200 +       struct vfsmount *mnt;
10201 +
10202 +       FiMustWriteLock(file);
10203 +       AuDebugOn(h_file);
10204 +
10205 +       err = 0;
10206 +       mnt = file->f_path.mnt;
10207 +       dentry = file->f_path.dentry;
10208 +       file->f_version = inode_query_iversion(d_inode(dentry));
10209 +       bindex = au_dbtop(dentry);
10210 +       au_set_fbtop(file, bindex);
10211 +       btail = au_dbtaildir(dentry);
10212 +       au_set_fbbot_dir(file, btail);
10213 +       for (; !err && bindex <= btail; bindex++) {
10214 +               h_dentry = au_h_dptr(dentry, bindex);
10215 +               if (!h_dentry)
10216 +                       continue;
10217 +
10218 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10219 +               if (unlikely(err))
10220 +                       break;
10221 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10222 +               if (IS_ERR(h_file)) {
10223 +                       err = PTR_ERR(h_file);
10224 +                       break;
10225 +               }
10226 +               au_set_h_fptr(file, bindex, h_file);
10227 +       }
10228 +       au_update_figen(file);
10229 +       /* todo: necessary? */
10230 +       /* file->f_ra = h_file->f_ra; */
10231 +       if (!err)
10232 +               return 0; /* success */
10233 +
10234 +       /* close all */
10235 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10236 +               au_set_h_fptr(file, bindex, NULL);
10237 +       au_set_fbtop(file, -1);
10238 +       au_set_fbbot_dir(file, -1);
10239 +
10240 +       return err;
10241 +}
10242 +
10243 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10244 +                        struct file *file)
10245 +{
10246 +       int err;
10247 +       struct super_block *sb;
10248 +       struct au_fidir *fidir;
10249 +
10250 +       err = -ENOMEM;
10251 +       sb = file->f_path.dentry->d_sb;
10252 +       si_read_lock(sb, AuLock_FLUSH);
10253 +       fidir = au_fidir_alloc(sb);
10254 +       if (fidir) {
10255 +               struct au_do_open_args args = {
10256 +                       .open   = do_open_dir,
10257 +                       .fidir  = fidir
10258 +               };
10259 +               err = au_do_open(file, &args);
10260 +               if (unlikely(err))
10261 +                       au_kfree_rcu(fidir);
10262 +       }
10263 +       si_read_unlock(sb);
10264 +       return err;
10265 +}
10266 +
10267 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10268 +                           struct file *file)
10269 +{
10270 +       struct au_vdir *vdir_cache;
10271 +       struct au_finfo *finfo;
10272 +       struct au_fidir *fidir;
10273 +       struct au_hfile *hf;
10274 +       aufs_bindex_t bindex, bbot;
10275 +
10276 +       finfo = au_fi(file);
10277 +       fidir = finfo->fi_hdir;
10278 +       if (fidir) {
10279 +               au_hbl_del(&finfo->fi_hlist,
10280 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10281 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10282 +               if (vdir_cache)
10283 +                       au_vdir_free(vdir_cache);
10284 +
10285 +               bindex = finfo->fi_btop;
10286 +               if (bindex >= 0) {
10287 +                       hf = fidir->fd_hfile + bindex;
10288 +                       /*
10289 +                        * calls fput() instead of filp_close(),
10290 +                        * since no dnotify or lock for the lower file.
10291 +                        */
10292 +                       bbot = fidir->fd_bbot;
10293 +                       for (; bindex <= bbot; bindex++, hf++)
10294 +                               if (hf->hf_file)
10295 +                                       au_hfput(hf, /*execed*/0);
10296 +               }
10297 +               au_kfree_rcu(fidir);
10298 +               finfo->fi_hdir = NULL;
10299 +       }
10300 +       au_finfo_fin(file);
10301 +       return 0;
10302 +}
10303 +
10304 +/* ---------------------------------------------------------------------- */
10305 +
10306 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10307 +{
10308 +       int err;
10309 +       aufs_bindex_t bindex, bbot;
10310 +       struct file *h_file;
10311 +
10312 +       err = 0;
10313 +       bbot = au_fbbot_dir(file);
10314 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10315 +               h_file = au_hf_dir(file, bindex);
10316 +               if (h_file)
10317 +                       err = vfsub_flush(h_file, id);
10318 +       }
10319 +       return err;
10320 +}
10321 +
10322 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10323 +{
10324 +       return au_do_flush(file, id, au_do_flush_dir);
10325 +}
10326 +
10327 +/* ---------------------------------------------------------------------- */
10328 +
10329 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10330 +{
10331 +       int err;
10332 +       aufs_bindex_t bbot, bindex;
10333 +       struct inode *inode;
10334 +       struct super_block *sb;
10335 +
10336 +       err = 0;
10337 +       sb = dentry->d_sb;
10338 +       inode = d_inode(dentry);
10339 +       IMustLock(inode);
10340 +       bbot = au_dbbot(dentry);
10341 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10342 +               struct path h_path;
10343 +
10344 +               if (au_test_ro(sb, bindex, inode))
10345 +                       continue;
10346 +               h_path.dentry = au_h_dptr(dentry, bindex);
10347 +               if (!h_path.dentry)
10348 +                       continue;
10349 +
10350 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10351 +               err = vfsub_fsync(NULL, &h_path, datasync);
10352 +       }
10353 +
10354 +       return err;
10355 +}
10356 +
10357 +static int au_do_fsync_dir(struct file *file, int datasync)
10358 +{
10359 +       int err;
10360 +       aufs_bindex_t bbot, bindex;
10361 +       struct file *h_file;
10362 +       struct super_block *sb;
10363 +       struct inode *inode;
10364 +
10365 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10366 +       if (unlikely(err))
10367 +               goto out;
10368 +
10369 +       inode = file_inode(file);
10370 +       sb = inode->i_sb;
10371 +       bbot = au_fbbot_dir(file);
10372 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10373 +               h_file = au_hf_dir(file, bindex);
10374 +               if (!h_file || au_test_ro(sb, bindex, inode))
10375 +                       continue;
10376 +
10377 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10378 +       }
10379 +
10380 +out:
10381 +       return err;
10382 +}
10383 +
10384 +/*
10385 + * @file may be NULL
10386 + */
10387 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10388 +                         int datasync)
10389 +{
10390 +       int err;
10391 +       struct dentry *dentry;
10392 +       struct inode *inode;
10393 +       struct super_block *sb;
10394 +
10395 +       err = 0;
10396 +       dentry = file->f_path.dentry;
10397 +       inode = d_inode(dentry);
10398 +       inode_lock(inode);
10399 +       sb = dentry->d_sb;
10400 +       si_noflush_read_lock(sb);
10401 +       if (file)
10402 +               err = au_do_fsync_dir(file, datasync);
10403 +       else {
10404 +               di_write_lock_child(dentry);
10405 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10406 +       }
10407 +       au_cpup_attr_timesizes(inode);
10408 +       di_write_unlock(dentry);
10409 +       if (file)
10410 +               fi_write_unlock(file);
10411 +
10412 +       si_read_unlock(sb);
10413 +       inode_unlock(inode);
10414 +       return err;
10415 +}
10416 +
10417 +/* ---------------------------------------------------------------------- */
10418 +
10419 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10420 +{
10421 +       int err;
10422 +       struct dentry *dentry;
10423 +       struct inode *inode, *h_inode;
10424 +       struct super_block *sb;
10425 +
10426 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10427 +
10428 +       dentry = file->f_path.dentry;
10429 +       inode = d_inode(dentry);
10430 +       IMustLock(inode);
10431 +
10432 +       sb = dentry->d_sb;
10433 +       si_read_lock(sb, AuLock_FLUSH);
10434 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10435 +       if (unlikely(err))
10436 +               goto out;
10437 +       err = au_alive_dir(dentry);
10438 +       if (!err)
10439 +               err = au_vdir_init(file);
10440 +       di_downgrade_lock(dentry, AuLock_IR);
10441 +       if (unlikely(err))
10442 +               goto out_unlock;
10443 +
10444 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10445 +       if (!au_test_nfsd()) {
10446 +               err = au_vdir_fill_de(file, ctx);
10447 +               fsstack_copy_attr_atime(inode, h_inode);
10448 +       } else {
10449 +               /*
10450 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10451 +                * encode_fh() and others.
10452 +                */
10453 +               atomic_inc(&h_inode->i_count);
10454 +               di_read_unlock(dentry, AuLock_IR);
10455 +               si_read_unlock(sb);
10456 +               err = au_vdir_fill_de(file, ctx);
10457 +               fsstack_copy_attr_atime(inode, h_inode);
10458 +               fi_write_unlock(file);
10459 +               iput(h_inode);
10460 +
10461 +               AuTraceErr(err);
10462 +               return err;
10463 +       }
10464 +
10465 +out_unlock:
10466 +       di_read_unlock(dentry, AuLock_IR);
10467 +       fi_write_unlock(file);
10468 +out:
10469 +       si_read_unlock(sb);
10470 +       return err;
10471 +}
10472 +
10473 +/* ---------------------------------------------------------------------- */
10474 +
10475 +#define AuTestEmpty_WHONLY     1
10476 +#define AuTestEmpty_CALLED     (1 << 1)
10477 +#define AuTestEmpty_SHWH       (1 << 2)
10478 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10479 +#define au_fset_testempty(flags, name) \
10480 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10481 +#define au_fclr_testempty(flags, name) \
10482 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10483 +
10484 +#ifndef CONFIG_AUFS_SHWH
10485 +#undef AuTestEmpty_SHWH
10486 +#define AuTestEmpty_SHWH       0
10487 +#endif
10488 +
10489 +struct test_empty_arg {
10490 +       struct dir_context ctx;
10491 +       struct au_nhash *whlist;
10492 +       unsigned int flags;
10493 +       int err;
10494 +       aufs_bindex_t bindex;
10495 +};
10496 +
10497 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10498 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10499 +                        unsigned int d_type)
10500 +{
10501 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10502 +                                                 ctx);
10503 +       char *name = (void *)__name;
10504 +
10505 +       arg->err = 0;
10506 +       au_fset_testempty(arg->flags, CALLED);
10507 +       /* smp_mb(); */
10508 +       if (name[0] == '.'
10509 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10510 +               goto out; /* success */
10511 +
10512 +       if (namelen <= AUFS_WH_PFX_LEN
10513 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10514 +               if (au_ftest_testempty(arg->flags, WHONLY)
10515 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10516 +                       arg->err = -ENOTEMPTY;
10517 +               goto out;
10518 +       }
10519 +
10520 +       name += AUFS_WH_PFX_LEN;
10521 +       namelen -= AUFS_WH_PFX_LEN;
10522 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10523 +               arg->err = au_nhash_append_wh
10524 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10525 +                        au_ftest_testempty(arg->flags, SHWH));
10526 +
10527 +out:
10528 +       /* smp_mb(); */
10529 +       AuTraceErr(arg->err);
10530 +       return arg->err;
10531 +}
10532 +
10533 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10534 +{
10535 +       int err;
10536 +       struct file *h_file;
10537 +       struct au_branch *br;
10538 +
10539 +       h_file = au_h_open(dentry, arg->bindex,
10540 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10541 +                          /*file*/NULL, /*force_wr*/0);
10542 +       err = PTR_ERR(h_file);
10543 +       if (IS_ERR(h_file))
10544 +               goto out;
10545 +
10546 +       err = 0;
10547 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10548 +           && !file_inode(h_file)->i_nlink)
10549 +               goto out_put;
10550 +
10551 +       do {
10552 +               arg->err = 0;
10553 +               au_fclr_testempty(arg->flags, CALLED);
10554 +               /* smp_mb(); */
10555 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10556 +               if (err >= 0)
10557 +                       err = arg->err;
10558 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10559 +
10560 +out_put:
10561 +       fput(h_file);
10562 +       br = au_sbr(dentry->d_sb, arg->bindex);
10563 +       au_lcnt_dec(&br->br_nfiles);
10564 +out:
10565 +       return err;
10566 +}
10567 +
10568 +struct do_test_empty_args {
10569 +       int *errp;
10570 +       struct dentry *dentry;
10571 +       struct test_empty_arg *arg;
10572 +};
10573 +
10574 +static void call_do_test_empty(void *args)
10575 +{
10576 +       struct do_test_empty_args *a = args;
10577 +       *a->errp = do_test_empty(a->dentry, a->arg);
10578 +}
10579 +
10580 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10581 +{
10582 +       int err, wkq_err;
10583 +       struct dentry *h_dentry;
10584 +       struct inode *h_inode;
10585 +       struct user_namespace *h_userns;
10586 +
10587 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10588 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10589 +       h_inode = d_inode(h_dentry);
10590 +       /* todo: i_mode changes anytime? */
10591 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10592 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10593 +       inode_unlock_shared(h_inode);
10594 +       if (!err)
10595 +               err = do_test_empty(dentry, arg);
10596 +       else {
10597 +               struct do_test_empty_args args = {
10598 +                       .errp   = &err,
10599 +                       .dentry = dentry,
10600 +                       .arg    = arg
10601 +               };
10602 +               unsigned int flags = arg->flags;
10603 +
10604 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10605 +               if (unlikely(wkq_err))
10606 +                       err = wkq_err;
10607 +               arg->flags = flags;
10608 +       }
10609 +
10610 +       return err;
10611 +}
10612 +
10613 +int au_test_empty_lower(struct dentry *dentry)
10614 +{
10615 +       int err;
10616 +       unsigned int rdhash;
10617 +       aufs_bindex_t bindex, btop, btail;
10618 +       struct au_nhash whlist;
10619 +       struct test_empty_arg arg = {
10620 +               .ctx = {
10621 +                       .actor = test_empty_cb
10622 +               }
10623 +       };
10624 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10625 +
10626 +       SiMustAnyLock(dentry->d_sb);
10627 +
10628 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10629 +       if (!rdhash)
10630 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10631 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10632 +       if (unlikely(err))
10633 +               goto out;
10634 +
10635 +       arg.flags = 0;
10636 +       arg.whlist = &whlist;
10637 +       btop = au_dbtop(dentry);
10638 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10639 +               au_fset_testempty(arg.flags, SHWH);
10640 +       test_empty = do_test_empty;
10641 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10642 +               test_empty = sio_test_empty;
10643 +       arg.bindex = btop;
10644 +       err = test_empty(dentry, &arg);
10645 +       if (unlikely(err))
10646 +               goto out_whlist;
10647 +
10648 +       au_fset_testempty(arg.flags, WHONLY);
10649 +       btail = au_dbtaildir(dentry);
10650 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10651 +               struct dentry *h_dentry;
10652 +
10653 +               h_dentry = au_h_dptr(dentry, bindex);
10654 +               if (h_dentry && d_is_positive(h_dentry)) {
10655 +                       arg.bindex = bindex;
10656 +                       err = test_empty(dentry, &arg);
10657 +               }
10658 +       }
10659 +
10660 +out_whlist:
10661 +       au_nhash_wh_free(&whlist);
10662 +out:
10663 +       return err;
10664 +}
10665 +
10666 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10667 +{
10668 +       int err;
10669 +       struct test_empty_arg arg = {
10670 +               .ctx = {
10671 +                       .actor = test_empty_cb
10672 +               }
10673 +       };
10674 +       aufs_bindex_t bindex, btail;
10675 +
10676 +       err = 0;
10677 +       arg.whlist = whlist;
10678 +       arg.flags = AuTestEmpty_WHONLY;
10679 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10680 +               au_fset_testempty(arg.flags, SHWH);
10681 +       btail = au_dbtaildir(dentry);
10682 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10683 +               struct dentry *h_dentry;
10684 +
10685 +               h_dentry = au_h_dptr(dentry, bindex);
10686 +               if (h_dentry && d_is_positive(h_dentry)) {
10687 +                       arg.bindex = bindex;
10688 +                       err = sio_test_empty(dentry, &arg);
10689 +               }
10690 +       }
10691 +
10692 +       return err;
10693 +}
10694 +
10695 +/* ---------------------------------------------------------------------- */
10696 +
10697 +const struct file_operations aufs_dir_fop = {
10698 +       .owner          = THIS_MODULE,
10699 +       .llseek         = default_llseek,
10700 +       .read           = generic_read_dir,
10701 +       .iterate_shared = aufs_iterate_shared,
10702 +       .unlocked_ioctl = aufs_ioctl_dir,
10703 +#ifdef CONFIG_COMPAT
10704 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10705 +#endif
10706 +       .open           = aufs_open_dir,
10707 +       .release        = aufs_release_dir,
10708 +       .flush          = aufs_flush_dir,
10709 +       .fsync          = aufs_fsync_dir
10710 +};
10711 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10712 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10713 +++ linux/fs/aufs/dir.h 2021-12-03 15:38:59.933313976 +0100
10714 @@ -0,0 +1,134 @@
10715 +/* SPDX-License-Identifier: GPL-2.0 */
10716 +/*
10717 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10718 + *
10719 + * This program, aufs is free software; you can redistribute it and/or modify
10720 + * it under the terms of the GNU General Public License as published by
10721 + * the Free Software Foundation; either version 2 of the License, or
10722 + * (at your option) any later version.
10723 + *
10724 + * This program is distributed in the hope that it will be useful,
10725 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10726 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10727 + * GNU General Public License for more details.
10728 + *
10729 + * You should have received a copy of the GNU General Public License
10730 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10731 + */
10732 +
10733 +/*
10734 + * directory operations
10735 + */
10736 +
10737 +#ifndef __AUFS_DIR_H__
10738 +#define __AUFS_DIR_H__
10739 +
10740 +#ifdef __KERNEL__
10741 +
10742 +#include <linux/fs.h>
10743 +
10744 +/* ---------------------------------------------------------------------- */
10745 +
10746 +/* need to be faster and smaller */
10747 +
10748 +struct au_nhash {
10749 +       unsigned int            nh_num;
10750 +       struct hlist_head       *nh_head;
10751 +};
10752 +
10753 +struct au_vdir_destr {
10754 +       unsigned char   len;
10755 +       unsigned char   name[];
10756 +} __packed;
10757 +
10758 +struct au_vdir_dehstr {
10759 +       struct hlist_node       hash;
10760 +       struct au_vdir_destr    *str;
10761 +       struct rcu_head         rcu;
10762 +} ____cacheline_aligned_in_smp;
10763 +
10764 +struct au_vdir_de {
10765 +       ino_t                   de_ino;
10766 +       unsigned char           de_type;
10767 +       /* caution: packed */
10768 +       struct au_vdir_destr    de_str;
10769 +} __packed;
10770 +
10771 +struct au_vdir_wh {
10772 +       struct hlist_node       wh_hash;
10773 +#ifdef CONFIG_AUFS_SHWH
10774 +       ino_t                   wh_ino;
10775 +       aufs_bindex_t           wh_bindex;
10776 +       unsigned char           wh_type;
10777 +#else
10778 +       aufs_bindex_t           wh_bindex;
10779 +#endif
10780 +       /* caution: packed */
10781 +       struct au_vdir_destr    wh_str;
10782 +} __packed;
10783 +
10784 +union au_vdir_deblk_p {
10785 +       unsigned char           *deblk;
10786 +       struct au_vdir_de       *de;
10787 +};
10788 +
10789 +struct au_vdir {
10790 +       unsigned char   **vd_deblk;
10791 +       unsigned long   vd_nblk;
10792 +       struct {
10793 +               unsigned long           ul;
10794 +               union au_vdir_deblk_p   p;
10795 +       } vd_last;
10796 +
10797 +       u64             vd_version;
10798 +       unsigned int    vd_deblk_sz;
10799 +       unsigned long   vd_jiffy;
10800 +       struct rcu_head rcu;
10801 +} ____cacheline_aligned_in_smp;
10802 +
10803 +/* ---------------------------------------------------------------------- */
10804 +
10805 +/* dir.c */
10806 +extern const struct file_operations aufs_dir_fop;
10807 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10808 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10809 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10810 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10811 +int au_test_empty_lower(struct dentry *dentry);
10812 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10813 +
10814 +/* vdir.c */
10815 +unsigned int au_rdhash_est(loff_t sz);
10816 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10817 +void au_nhash_wh_free(struct au_nhash *whlist);
10818 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10819 +                           int limit);
10820 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10821 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10822 +                      unsigned int d_type, aufs_bindex_t bindex,
10823 +                      unsigned char shwh);
10824 +void au_vdir_free(struct au_vdir *vdir);
10825 +int au_vdir_init(struct file *file);
10826 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10827 +
10828 +/* ioctl.c */
10829 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10830 +
10831 +#ifdef CONFIG_AUFS_RDU
10832 +/* rdu.c */
10833 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10834 +#ifdef CONFIG_COMPAT
10835 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10836 +                        unsigned long arg);
10837 +#endif
10838 +#else
10839 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10840 +       unsigned int cmd, unsigned long arg)
10841 +#ifdef CONFIG_COMPAT
10842 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10843 +       unsigned int cmd, unsigned long arg)
10844 +#endif
10845 +#endif
10846 +
10847 +#endif /* __KERNEL__ */
10848 +#endif /* __AUFS_DIR_H__ */
10849 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10850 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10851 +++ linux/fs/aufs/dirren.c      2021-12-03 15:38:59.933313976 +0100
10852 @@ -0,0 +1,1315 @@
10853 +// SPDX-License-Identifier: GPL-2.0
10854 +/*
10855 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10856 + *
10857 + * This program, aufs is free software; you can redistribute it and/or modify
10858 + * it under the terms of the GNU General Public License as published by
10859 + * the Free Software Foundation; either version 2 of the License, or
10860 + * (at your option) any later version.
10861 + *
10862 + * This program is distributed in the hope that it will be useful,
10863 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10864 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10865 + * GNU General Public License for more details.
10866 + *
10867 + * You should have received a copy of the GNU General Public License
10868 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10869 + */
10870 +
10871 +/*
10872 + * special handling in renaming a directory
10873 + * in order to support looking-up the before-renamed name on the lower readonly
10874 + * branches
10875 + */
10876 +
10877 +#include <linux/byteorder/generic.h>
10878 +#include "aufs.h"
10879 +
10880 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10881 +{
10882 +       int idx;
10883 +
10884 +       idx = au_dr_ihash(ent->dr_h_ino);
10885 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10886 +}
10887 +
10888 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10889 +{
10890 +       int ret, i;
10891 +       struct hlist_bl_head *hbl;
10892 +
10893 +       ret = 1;
10894 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10895 +               hbl = dr->dr_h_ino + i;
10896 +               hlist_bl_lock(hbl);
10897 +               ret &= hlist_bl_empty(hbl);
10898 +               hlist_bl_unlock(hbl);
10899 +       }
10900 +
10901 +       return ret;
10902 +}
10903 +
10904 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10905 +{
10906 +       struct au_dr_hino *found, *ent;
10907 +       struct hlist_bl_head *hbl;
10908 +       struct hlist_bl_node *pos;
10909 +       int idx;
10910 +
10911 +       found = NULL;
10912 +       idx = au_dr_ihash(ino);
10913 +       hbl = dr->dr_h_ino + idx;
10914 +       hlist_bl_lock(hbl);
10915 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10916 +               if (ent->dr_h_ino == ino) {
10917 +                       found = ent;
10918 +                       break;
10919 +               }
10920 +       hlist_bl_unlock(hbl);
10921 +
10922 +       return found;
10923 +}
10924 +
10925 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10926 +                       struct au_dr_hino *add_ent)
10927 +{
10928 +       int found, idx;
10929 +       struct hlist_bl_head *hbl;
10930 +       struct hlist_bl_node *pos;
10931 +       struct au_dr_hino *ent;
10932 +
10933 +       found = 0;
10934 +       idx = au_dr_ihash(ino);
10935 +       hbl = dr->dr_h_ino + idx;
10936 +#if 0 /* debug print */
10937 +       {
10938 +               struct hlist_bl_node *tmp;
10939 +
10940 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10941 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10942 +       }
10943 +#endif
10944 +       hlist_bl_lock(hbl);
10945 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10946 +               if (ent->dr_h_ino == ino) {
10947 +                       found = 1;
10948 +                       break;
10949 +               }
10950 +       if (!found && add_ent)
10951 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10952 +       hlist_bl_unlock(hbl);
10953 +
10954 +       if (!found && add_ent)
10955 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10956 +
10957 +       return found;
10958 +}
10959 +
10960 +void au_dr_hino_free(struct au_dr_br *dr)
10961 +{
10962 +       int i;
10963 +       struct hlist_bl_head *hbl;
10964 +       struct hlist_bl_node *pos, *tmp;
10965 +       struct au_dr_hino *ent;
10966 +
10967 +       /* SiMustWriteLock(sb); */
10968 +
10969 +       for (i = 0; i < AuDirren_NHASH; i++) {
10970 +               hbl = dr->dr_h_ino + i;
10971 +               /* no spinlock since sbinfo must be write-locked */
10972 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10973 +                       au_kfree_rcu(ent);
10974 +               INIT_HLIST_BL_HEAD(hbl);
10975 +       }
10976 +}
10977 +
10978 +/* returns the number of inodes or an error */
10979 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10980 +                           struct file *hinofile)
10981 +{
10982 +       int err, i;
10983 +       ssize_t ssz;
10984 +       loff_t pos, oldsize;
10985 +       __be64 u64;
10986 +       struct inode *hinoinode;
10987 +       struct hlist_bl_head *hbl;
10988 +       struct hlist_bl_node *n1, *n2;
10989 +       struct au_dr_hino *ent;
10990 +
10991 +       SiMustWriteLock(sb);
10992 +       AuDebugOn(!au_br_writable(br->br_perm));
10993 +
10994 +       hinoinode = file_inode(hinofile);
10995 +       oldsize = i_size_read(hinoinode);
10996 +
10997 +       err = 0;
10998 +       pos = 0;
10999 +       hbl = br->br_dirren.dr_h_ino;
11000 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11001 +               /* no bit-lock since sbinfo must be write-locked */
11002 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11003 +                       AuDbg("hi%llu, %pD2\n",
11004 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11005 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11006 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11007 +                       if (ssz == sizeof(u64))
11008 +                               continue;
11009 +
11010 +                       /* write error */
11011 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11012 +                       err = -ENOSPC;
11013 +                       if (ssz < 0)
11014 +                               err = ssz;
11015 +                       break;
11016 +               }
11017 +       }
11018 +       /* regardless the error */
11019 +       if (pos < oldsize) {
11020 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11021 +               AuTraceErr(err);
11022 +       }
11023 +
11024 +       AuTraceErr(err);
11025 +       return err;
11026 +}
11027 +
11028 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11029 +{
11030 +       int err, hidx;
11031 +       ssize_t ssz;
11032 +       size_t sz, n;
11033 +       loff_t pos;
11034 +       uint64_t u64;
11035 +       struct au_dr_hino *ent;
11036 +       struct inode *hinoinode;
11037 +       struct hlist_bl_head *hbl;
11038 +
11039 +       err = 0;
11040 +       pos = 0;
11041 +       hbl = dr->dr_h_ino;
11042 +       hinoinode = file_inode(hinofile);
11043 +       sz = i_size_read(hinoinode);
11044 +       AuDebugOn(sz % sizeof(u64));
11045 +       n = sz / sizeof(u64);
11046 +       while (n--) {
11047 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11048 +               if (unlikely(ssz != sizeof(u64))) {
11049 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11050 +                       err = -EINVAL;
11051 +                       if (ssz < 0)
11052 +                               err = ssz;
11053 +                       goto out_free;
11054 +               }
11055 +
11056 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11057 +               if (!ent) {
11058 +                       err = -ENOMEM;
11059 +                       AuTraceErr(err);
11060 +                       goto out_free;
11061 +               }
11062 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11063 +               AuDbg("hi%llu, %pD2\n",
11064 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11065 +               hidx = au_dr_ihash(ent->dr_h_ino);
11066 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11067 +       }
11068 +       goto out; /* success */
11069 +
11070 +out_free:
11071 +       au_dr_hino_free(dr);
11072 +out:
11073 +       AuTraceErr(err);
11074 +       return err;
11075 +}
11076 +
11077 +/*
11078 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11079 + * @path is a switch to distinguish load and store.
11080 + */
11081 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11082 +                     struct au_branch *br, const struct path *path)
11083 +{
11084 +       int err, flags;
11085 +       unsigned char load, suspend;
11086 +       struct file *hinofile;
11087 +       struct au_hinode *hdir;
11088 +       struct inode *dir, *delegated;
11089 +       struct path hinopath;
11090 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11091 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11092 +
11093 +       AuDebugOn(bindex < 0 && !br);
11094 +       AuDebugOn(bindex >= 0 && br);
11095 +
11096 +       err = -EINVAL;
11097 +       suspend = !br;
11098 +       if (suspend)
11099 +               br = au_sbr(sb, bindex);
11100 +       load = !!path;
11101 +       if (!load) {
11102 +               path = &br->br_path;
11103 +               AuDebugOn(!au_br_writable(br->br_perm));
11104 +               if (unlikely(!au_br_writable(br->br_perm)))
11105 +                       goto out;
11106 +       }
11107 +
11108 +       hdir = NULL;
11109 +       if (suspend) {
11110 +               dir = d_inode(sb->s_root);
11111 +               hdir = au_hinode(au_ii(dir), bindex);
11112 +               dir = hdir->hi_inode;
11113 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11114 +       } else {
11115 +               dir = d_inode(path->dentry);
11116 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11117 +       }
11118 +       hinopath.mnt = path->mnt;
11119 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11120 +       err = PTR_ERR(hinopath.dentry);
11121 +       if (IS_ERR(hinopath.dentry))
11122 +               goto out_unlock;
11123 +
11124 +       err = 0;
11125 +       flags = O_RDONLY;
11126 +       if (load) {
11127 +               if (d_is_negative(hinopath.dentry))
11128 +                       goto out_dput; /* success */
11129 +       } else {
11130 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11131 +                       if (d_is_positive(hinopath.dentry)) {
11132 +                               delegated = NULL;
11133 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11134 +                                                  /*force*/0);
11135 +                               AuTraceErr(err);
11136 +                               if (unlikely(err))
11137 +                                       pr_err("ignored err %d, %pd2\n",
11138 +                                              err, hinopath.dentry);
11139 +                               if (unlikely(err == -EWOULDBLOCK))
11140 +                                       iput(delegated);
11141 +                               err = 0;
11142 +                       }
11143 +                       goto out_dput;
11144 +               } else if (!d_is_positive(hinopath.dentry)) {
11145 +                       err = vfsub_create(dir, &hinopath, 0600,
11146 +                                          /*want_excl*/false);
11147 +                       AuTraceErr(err);
11148 +                       if (unlikely(err))
11149 +                               goto out_dput;
11150 +               }
11151 +               flags = O_WRONLY;
11152 +       }
11153 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11154 +       if (suspend)
11155 +               au_hn_inode_unlock(hdir);
11156 +       else
11157 +               inode_unlock(dir);
11158 +       dput(hinopath.dentry);
11159 +       AuTraceErrPtr(hinofile);
11160 +       if (IS_ERR(hinofile)) {
11161 +               err = PTR_ERR(hinofile);
11162 +               goto out;
11163 +       }
11164 +
11165 +       if (load)
11166 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11167 +       else
11168 +               err = au_dr_hino_store(sb, br, hinofile);
11169 +       fput(hinofile);
11170 +       goto out;
11171 +
11172 +out_dput:
11173 +       dput(hinopath.dentry);
11174 +out_unlock:
11175 +       if (suspend)
11176 +               au_hn_inode_unlock(hdir);
11177 +       else
11178 +               inode_unlock(dir);
11179 +out:
11180 +       AuTraceErr(err);
11181 +       return err;
11182 +}
11183 +
11184 +/* ---------------------------------------------------------------------- */
11185 +
11186 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11187 +{
11188 +       int err;
11189 +       struct kstatfs kstfs;
11190 +       dev_t dev;
11191 +       struct dentry *dentry;
11192 +       struct super_block *sb;
11193 +
11194 +       err = vfs_statfs((void *)path, &kstfs);
11195 +       AuTraceErr(err);
11196 +       if (unlikely(err))
11197 +               goto out;
11198 +
11199 +       /* todo: support for UUID */
11200 +
11201 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11202 +               brid->type = AuBrid_FSID;
11203 +               brid->fsid = kstfs.f_fsid;
11204 +       } else {
11205 +               dentry = path->dentry;
11206 +               sb = dentry->d_sb;
11207 +               dev = sb->s_dev;
11208 +               if (dev) {
11209 +                       brid->type = AuBrid_DEV;
11210 +                       brid->dev = dev;
11211 +               }
11212 +       }
11213 +
11214 +out:
11215 +       return err;
11216 +}
11217 +
11218 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11219 +                 const struct path *path)
11220 +{
11221 +       int err, i;
11222 +       struct au_dr_br *dr;
11223 +       struct hlist_bl_head *hbl;
11224 +
11225 +       dr = &br->br_dirren;
11226 +       hbl = dr->dr_h_ino;
11227 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11228 +               INIT_HLIST_BL_HEAD(hbl);
11229 +
11230 +       err = au_dr_brid_init(&dr->dr_brid, path);
11231 +       if (unlikely(err))
11232 +               goto out;
11233 +
11234 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11235 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11236 +
11237 +out:
11238 +       AuTraceErr(err);
11239 +       return err;
11240 +}
11241 +
11242 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11243 +{
11244 +       int err;
11245 +
11246 +       err = 0;
11247 +       if (au_br_writable(br->br_perm))
11248 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11249 +       if (!err)
11250 +               au_dr_hino_free(&br->br_dirren);
11251 +
11252 +       return err;
11253 +}
11254 +
11255 +/* ---------------------------------------------------------------------- */
11256 +
11257 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11258 +                      char *buf, size_t sz)
11259 +{
11260 +       int err;
11261 +       unsigned int major, minor;
11262 +       char *p;
11263 +
11264 +       p = buf;
11265 +       err = snprintf(p, sz, "%d_", brid->type);
11266 +       AuDebugOn(err > sz);
11267 +       p += err;
11268 +       sz -= err;
11269 +       switch (brid->type) {
11270 +       case AuBrid_Unset:
11271 +               return -EINVAL;
11272 +       case AuBrid_UUID:
11273 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11274 +               break;
11275 +       case AuBrid_FSID:
11276 +               err = snprintf(p, sz, "%08x-%08x",
11277 +                              brid->fsid.val[0], brid->fsid.val[1]);
11278 +               break;
11279 +       case AuBrid_DEV:
11280 +               major = MAJOR(brid->dev);
11281 +               minor = MINOR(brid->dev);
11282 +               if (major <= 0xff && minor <= 0xff)
11283 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11284 +               else
11285 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11286 +               break;
11287 +       }
11288 +       AuDebugOn(err > sz);
11289 +       p += err;
11290 +       sz -= err;
11291 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11292 +       AuDebugOn(err > sz);
11293 +       p += err;
11294 +       sz -= err;
11295 +
11296 +       return p - buf;
11297 +}
11298 +
11299 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11300 +{
11301 +       int rlen;
11302 +       struct dentry *br_dentry;
11303 +       struct inode *br_inode;
11304 +
11305 +       br_dentry = au_br_dentry(br);
11306 +       br_inode = d_inode(br_dentry);
11307 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11308 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11309 +       AuDebugOn(rlen > len);
11310 +
11311 +       return rlen;
11312 +}
11313 +
11314 +/* ---------------------------------------------------------------------- */
11315 +
11316 +/*
11317 + * from the given @h_dentry, construct drinfo at @*fdata.
11318 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11319 + * @allocated.
11320 + */
11321 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11322 +                              struct dentry *h_dentry,
11323 +                              unsigned char *allocated)
11324 +{
11325 +       int err, v;
11326 +       struct au_drinfo_fdata *f, *p;
11327 +       struct au_drinfo *drinfo;
11328 +       struct inode *h_inode;
11329 +       struct qstr *qname;
11330 +
11331 +       err = 0;
11332 +       f = *fdata;
11333 +       h_inode = d_inode(h_dentry);
11334 +       qname = &h_dentry->d_name;
11335 +       drinfo = &f->drinfo;
11336 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11337 +       drinfo->oldnamelen = qname->len;
11338 +       if (*allocated < sizeof(*f) + qname->len) {
11339 +               v = roundup_pow_of_two(*allocated + qname->len);
11340 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11341 +               if (unlikely(!p)) {
11342 +                       err = -ENOMEM;
11343 +                       AuTraceErr(err);
11344 +                       goto out;
11345 +               }
11346 +               f = p;
11347 +               *fdata = f;
11348 +               *allocated = v;
11349 +               drinfo = &f->drinfo;
11350 +       }
11351 +       memcpy(drinfo->oldname, qname->name, qname->len);
11352 +       AuDbg("i%llu, %.*s\n",
11353 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11354 +             drinfo->oldname);
11355 +
11356 +out:
11357 +       AuTraceErr(err);
11358 +       return err;
11359 +}
11360 +
11361 +/* callers have to free the return value */
11362 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11363 +{
11364 +       struct au_drinfo *ret, *drinfo;
11365 +       struct au_drinfo_fdata fdata;
11366 +       int len;
11367 +       loff_t pos;
11368 +       ssize_t ssz;
11369 +
11370 +       ret = ERR_PTR(-EIO);
11371 +       pos = 0;
11372 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11373 +       if (unlikely(ssz != sizeof(fdata))) {
11374 +               AuIOErr("ssz %zd, %u, %pD2\n",
11375 +                       ssz, (unsigned int)sizeof(fdata), file);
11376 +               goto out;
11377 +       }
11378 +
11379 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11380 +       switch (fdata.magic) {
11381 +       case AUFS_DRINFO_MAGIC_V1:
11382 +               break;
11383 +       default:
11384 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11385 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11386 +               goto out;
11387 +       }
11388 +
11389 +       drinfo = &fdata.drinfo;
11390 +       len = drinfo->oldnamelen;
11391 +       if (!len) {
11392 +               AuIOErr("broken drinfo %pD2\n", file);
11393 +               goto out;
11394 +       }
11395 +
11396 +       ret = NULL;
11397 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11398 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11399 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11400 +                     (unsigned long long)drinfo->ino,
11401 +                     (unsigned long long)h_ino, file);
11402 +               goto out; /* success */
11403 +       }
11404 +
11405 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11406 +       if (unlikely(!ret)) {
11407 +               ret = ERR_PTR(-ENOMEM);
11408 +               AuTraceErrPtr(ret);
11409 +               goto out;
11410 +       }
11411 +
11412 +       *ret = *drinfo;
11413 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11414 +       if (unlikely(ssz != len)) {
11415 +               au_kfree_rcu(ret);
11416 +               ret = ERR_PTR(-EIO);
11417 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11418 +               goto out;
11419 +       }
11420 +
11421 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11422 +
11423 +out:
11424 +       return ret;
11425 +}
11426 +
11427 +/* ---------------------------------------------------------------------- */
11428 +
11429 +/* in order to be revertible */
11430 +struct au_drinfo_rev_elm {
11431 +       int                     created;
11432 +       struct dentry           *info_dentry;
11433 +       struct au_drinfo        *info_last;
11434 +};
11435 +
11436 +struct au_drinfo_rev {
11437 +       unsigned char                   already;
11438 +       aufs_bindex_t                   nelm;
11439 +       struct au_drinfo_rev_elm        elm[];
11440 +};
11441 +
11442 +/* todo: isn't it too large? */
11443 +struct au_drinfo_store {
11444 +       struct path h_ppath;
11445 +       struct dentry *h_dentry;
11446 +       struct au_drinfo_fdata *fdata;
11447 +       char *infoname;                 /* inside of whname, just after PFX */
11448 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11449 +       aufs_bindex_t btgt, btail;
11450 +       unsigned char no_sio,
11451 +               allocated,              /* current size of *fdata */
11452 +               infonamelen,            /* room size for p */
11453 +               whnamelen,              /* length of the generated name */
11454 +               renameback;             /* renamed back */
11455 +};
11456 +
11457 +/* on rename(2) error, the caller should revert it using @elm */
11458 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11459 +                             struct au_drinfo_rev_elm *elm)
11460 +{
11461 +       int err, len;
11462 +       ssize_t ssz;
11463 +       loff_t pos;
11464 +       struct path infopath = {
11465 +               .mnt = w->h_ppath.mnt
11466 +       };
11467 +       struct inode *h_dir, *h_inode, *delegated;
11468 +       struct file *infofile;
11469 +       struct qstr *qname;
11470 +
11471 +       AuDebugOn(elm
11472 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11473 +
11474 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11475 +                                              w->whnamelen);
11476 +       AuTraceErrPtr(infopath.dentry);
11477 +       if (IS_ERR(infopath.dentry)) {
11478 +               err = PTR_ERR(infopath.dentry);
11479 +               goto out;
11480 +       }
11481 +
11482 +       err = 0;
11483 +       h_dir = d_inode(w->h_ppath.dentry);
11484 +       if (elm && d_is_negative(infopath.dentry)) {
11485 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11486 +               AuTraceErr(err);
11487 +               if (unlikely(err))
11488 +                       goto out_dput;
11489 +               elm->created = 1;
11490 +               elm->info_dentry = dget(infopath.dentry);
11491 +       }
11492 +
11493 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11494 +       AuTraceErrPtr(infofile);
11495 +       if (IS_ERR(infofile)) {
11496 +               err = PTR_ERR(infofile);
11497 +               goto out_dput;
11498 +       }
11499 +
11500 +       h_inode = d_inode(infopath.dentry);
11501 +       if (elm && i_size_read(h_inode)) {
11502 +               h_inode = d_inode(w->h_dentry);
11503 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11504 +               AuTraceErrPtr(elm->info_last);
11505 +               if (IS_ERR(elm->info_last)) {
11506 +                       err = PTR_ERR(elm->info_last);
11507 +                       elm->info_last = NULL;
11508 +                       AuDebugOn(elm->info_dentry);
11509 +                       goto out_fput;
11510 +               }
11511 +       }
11512 +
11513 +       if (elm && w->renameback) {
11514 +               delegated = NULL;
11515 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11516 +               AuTraceErr(err);
11517 +               if (unlikely(err == -EWOULDBLOCK))
11518 +                       iput(delegated);
11519 +               goto out_fput;
11520 +       }
11521 +
11522 +       pos = 0;
11523 +       qname = &w->h_dentry->d_name;
11524 +       len = sizeof(*w->fdata) + qname->len;
11525 +       if (!elm)
11526 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11527 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11528 +       if (ssz == len) {
11529 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11530 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11531 +               goto out_fput; /* success */
11532 +       } else {
11533 +               err = -EIO;
11534 +               if (ssz < 0)
11535 +                       err = ssz;
11536 +               /* the caller should revert it using @elm */
11537 +       }
11538 +
11539 +out_fput:
11540 +       fput(infofile);
11541 +out_dput:
11542 +       dput(infopath.dentry);
11543 +out:
11544 +       AuTraceErr(err);
11545 +       return err;
11546 +}
11547 +
11548 +struct au_call_drinfo_do_store_args {
11549 +       int *errp;
11550 +       struct au_drinfo_store *w;
11551 +       struct au_drinfo_rev_elm *elm;
11552 +};
11553 +
11554 +static void au_call_drinfo_do_store(void *args)
11555 +{
11556 +       struct au_call_drinfo_do_store_args *a = args;
11557 +
11558 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11559 +}
11560 +
11561 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11562 +                              struct au_drinfo_rev_elm *elm)
11563 +{
11564 +       int err, wkq_err;
11565 +
11566 +       if (w->no_sio)
11567 +               err = au_drinfo_do_store(w, elm);
11568 +       else {
11569 +               struct au_call_drinfo_do_store_args a = {
11570 +                       .errp   = &err,
11571 +                       .w      = w,
11572 +                       .elm    = elm
11573 +               };
11574 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11575 +               if (unlikely(wkq_err))
11576 +                       err = wkq_err;
11577 +       }
11578 +       AuTraceErr(err);
11579 +
11580 +       return err;
11581 +}
11582 +
11583 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11584 +                                    aufs_bindex_t btgt)
11585 +{
11586 +       int err;
11587 +
11588 +       memset(w, 0, sizeof(*w));
11589 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11590 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11591 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11592 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11593 +       w->btgt = btgt;
11594 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11595 +
11596 +       err = -ENOMEM;
11597 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11598 +       if (unlikely(!w->fdata)) {
11599 +               AuTraceErr(err);
11600 +               goto out;
11601 +       }
11602 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11603 +       err = 0;
11604 +
11605 +out:
11606 +       return err;
11607 +}
11608 +
11609 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11610 +{
11611 +       au_kfree_rcu(w->fdata);
11612 +}
11613 +
11614 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11615 +                               struct au_drinfo_store *w)
11616 +{
11617 +       struct au_drinfo_rev_elm *elm;
11618 +       struct inode *h_dir, *delegated;
11619 +       int err, nelm;
11620 +       struct path infopath = {
11621 +               .mnt = w->h_ppath.mnt
11622 +       };
11623 +
11624 +       h_dir = d_inode(w->h_ppath.dentry);
11625 +       IMustLock(h_dir);
11626 +
11627 +       err = 0;
11628 +       elm = rev->elm;
11629 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11630 +               AuDebugOn(elm->created && elm->info_last);
11631 +               if (elm->created) {
11632 +                       AuDbg("here\n");
11633 +                       delegated = NULL;
11634 +                       infopath.dentry = elm->info_dentry;
11635 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11636 +                                          !w->no_sio);
11637 +                       AuTraceErr(err);
11638 +                       if (unlikely(err == -EWOULDBLOCK))
11639 +                               iput(delegated);
11640 +                       dput(elm->info_dentry);
11641 +               } else if (elm->info_last) {
11642 +                       AuDbg("here\n");
11643 +                       w->fdata->drinfo = *elm->info_last;
11644 +                       memcpy(w->fdata->drinfo.oldname,
11645 +                              elm->info_last->oldname,
11646 +                              elm->info_last->oldnamelen);
11647 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11648 +                       au_kfree_rcu(elm->info_last);
11649 +               }
11650 +               if (unlikely(err))
11651 +                       AuIOErr("%d, %s\n", err, w->whname);
11652 +               /* go on even if err */
11653 +       }
11654 +}
11655 +
11656 +/* caller has to call au_dr_rename_fin() later */
11657 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11658 +                          struct qstr *dst_name, void *_rev)
11659 +{
11660 +       int err, sz, nelm;
11661 +       aufs_bindex_t bindex, btail;
11662 +       struct au_drinfo_store work;
11663 +       struct au_drinfo_rev *rev, **p;
11664 +       struct au_drinfo_rev_elm *elm;
11665 +       struct super_block *sb;
11666 +       struct au_branch *br;
11667 +       struct au_hinode *hdir;
11668 +
11669 +       err = au_drinfo_store_work_init(&work, btgt);
11670 +       AuTraceErr(err);
11671 +       if (unlikely(err))
11672 +               goto out;
11673 +
11674 +       err = -ENOMEM;
11675 +       btail = au_dbtaildir(dentry);
11676 +       nelm = btail - btgt;
11677 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11678 +       rev = kcalloc(1, sz, GFP_NOFS);
11679 +       if (unlikely(!rev)) {
11680 +               AuTraceErr(err);
11681 +               goto out_args;
11682 +       }
11683 +       rev->nelm = nelm;
11684 +       elm = rev->elm;
11685 +       p = _rev;
11686 +       *p = rev;
11687 +
11688 +       err = 0;
11689 +       sb = dentry->d_sb;
11690 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11691 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11692 +       hdir = au_hi(d_inode(dentry), btgt);
11693 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11694 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11695 +               work.h_dentry = au_h_dptr(dentry, bindex);
11696 +               if (!work.h_dentry)
11697 +                       continue;
11698 +
11699 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11700 +                                         &work.allocated);
11701 +               AuTraceErr(err);
11702 +               if (unlikely(err))
11703 +                       break;
11704 +
11705 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11706 +               br = au_sbr(sb, bindex);
11707 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11708 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11709 +                                                work.infonamelen);
11710 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11711 +                     work.whnamelen, work.whname,
11712 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11713 +                     work.fdata->drinfo.oldnamelen,
11714 +                     work.fdata->drinfo.oldname);
11715 +
11716 +               err = au_drinfo_store_sio(&work, elm);
11717 +               AuTraceErr(err);
11718 +               if (unlikely(err))
11719 +                       break;
11720 +       }
11721 +       if (unlikely(err)) {
11722 +               /* revert all drinfo */
11723 +               au_drinfo_store_rev(rev, &work);
11724 +               au_kfree_try_rcu(rev);
11725 +               *p = NULL;
11726 +       }
11727 +       au_hn_inode_unlock(hdir);
11728 +
11729 +out_args:
11730 +       au_drinfo_store_work_fin(&work);
11731 +out:
11732 +       return err;
11733 +}
11734 +
11735 +/* ---------------------------------------------------------------------- */
11736 +
11737 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11738 +                struct qstr *dst_name, void *_rev)
11739 +{
11740 +       int err, already;
11741 +       ino_t ino;
11742 +       struct super_block *sb;
11743 +       struct au_branch *br;
11744 +       struct au_dr_br *dr;
11745 +       struct dentry *h_dentry;
11746 +       struct inode *h_inode;
11747 +       struct au_dr_hino *ent;
11748 +       struct au_drinfo_rev *rev, **p;
11749 +
11750 +       AuDbg("bindex %d\n", bindex);
11751 +
11752 +       err = -ENOMEM;
11753 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11754 +       if (unlikely(!ent))
11755 +               goto out;
11756 +
11757 +       sb = src->d_sb;
11758 +       br = au_sbr(sb, bindex);
11759 +       dr = &br->br_dirren;
11760 +       h_dentry = au_h_dptr(src, bindex);
11761 +       h_inode = d_inode(h_dentry);
11762 +       ino = h_inode->i_ino;
11763 +       ent->dr_h_ino = ino;
11764 +       already = au_dr_hino_test_add(dr, ino, ent);
11765 +       AuDbg("b%d, hi%llu, already %d\n",
11766 +             bindex, (unsigned long long)ino, already);
11767 +
11768 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11769 +       AuTraceErr(err);
11770 +       if (!err) {
11771 +               p = _rev;
11772 +               rev = *p;
11773 +               rev->already = already;
11774 +               goto out; /* success */
11775 +       }
11776 +
11777 +       /* revert */
11778 +       if (!already)
11779 +               au_dr_hino_del(dr, ent);
11780 +       au_kfree_rcu(ent);
11781 +
11782 +out:
11783 +       AuTraceErr(err);
11784 +       return err;
11785 +}
11786 +
11787 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11788 +{
11789 +       struct au_drinfo_rev *rev;
11790 +       struct au_drinfo_rev_elm *elm;
11791 +       int nelm;
11792 +
11793 +       rev = _rev;
11794 +       elm = rev->elm;
11795 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11796 +               dput(elm->info_dentry);
11797 +               au_kfree_rcu(elm->info_last);
11798 +       }
11799 +       au_kfree_try_rcu(rev);
11800 +}
11801 +
11802 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11803 +{
11804 +       int err;
11805 +       struct au_drinfo_store work;
11806 +       struct au_drinfo_rev *rev = _rev;
11807 +       struct super_block *sb;
11808 +       struct au_branch *br;
11809 +       struct inode *h_inode;
11810 +       struct au_dr_br *dr;
11811 +       struct au_dr_hino *ent;
11812 +
11813 +       err = au_drinfo_store_work_init(&work, btgt);
11814 +       if (unlikely(err))
11815 +               goto out;
11816 +
11817 +       sb = src->d_sb;
11818 +       br = au_sbr(sb, btgt);
11819 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11820 +       work.h_ppath.mnt = au_br_mnt(br);
11821 +       au_drinfo_store_rev(rev, &work);
11822 +       au_drinfo_store_work_fin(&work);
11823 +       if (rev->already)
11824 +               goto out;
11825 +
11826 +       dr = &br->br_dirren;
11827 +       h_inode = d_inode(work.h_ppath.dentry);
11828 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11829 +       BUG_ON(!ent);
11830 +       au_dr_hino_del(dr, ent);
11831 +       au_kfree_rcu(ent);
11832 +
11833 +out:
11834 +       au_kfree_try_rcu(rev);
11835 +       if (unlikely(err))
11836 +               pr_err("failed to remove dirren info\n");
11837 +}
11838 +
11839 +/* ---------------------------------------------------------------------- */
11840 +
11841 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11842 +                                          char *whname, int whnamelen,
11843 +                                          struct dentry **info_dentry)
11844 +{
11845 +       struct au_drinfo *drinfo;
11846 +       struct file *f;
11847 +       struct inode *h_dir;
11848 +       struct path infopath;
11849 +       int unlocked;
11850 +
11851 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11852 +
11853 +       *info_dentry = NULL;
11854 +       drinfo = NULL;
11855 +       unlocked = 0;
11856 +       h_dir = d_inode(h_ppath->dentry);
11857 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11858 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11859 +       if (IS_ERR(infopath.dentry)) {
11860 +               drinfo = (void *)infopath.dentry;
11861 +               goto out;
11862 +       }
11863 +
11864 +       if (d_is_negative(infopath.dentry))
11865 +               goto out_dput; /* success */
11866 +
11867 +       infopath.mnt = h_ppath->mnt;
11868 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11869 +       inode_unlock_shared(h_dir);
11870 +       unlocked = 1;
11871 +       if (IS_ERR(f)) {
11872 +               drinfo = (void *)f;
11873 +               goto out_dput;
11874 +       }
11875 +
11876 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11877 +       if (IS_ERR_OR_NULL(drinfo))
11878 +               goto out_fput;
11879 +
11880 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11881 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11882 +
11883 +out_fput:
11884 +       fput(f);
11885 +out_dput:
11886 +       dput(infopath.dentry);
11887 +out:
11888 +       if (!unlocked)
11889 +               inode_unlock_shared(h_dir);
11890 +       AuTraceErrPtr(drinfo);
11891 +       return drinfo;
11892 +}
11893 +
11894 +struct au_drinfo_do_load_args {
11895 +       struct au_drinfo **drinfop;
11896 +       struct path *h_ppath;
11897 +       char *whname;
11898 +       int whnamelen;
11899 +       struct dentry **info_dentry;
11900 +};
11901 +
11902 +static void au_call_drinfo_do_load(void *args)
11903 +{
11904 +       struct au_drinfo_do_load_args *a = args;
11905 +
11906 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11907 +                                       a->info_dentry);
11908 +}
11909 +
11910 +struct au_drinfo_load {
11911 +       struct path h_ppath;
11912 +       struct qstr *qname;
11913 +       unsigned char no_sio;
11914 +
11915 +       aufs_bindex_t ninfo;
11916 +       struct au_drinfo **drinfo;
11917 +};
11918 +
11919 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11920 +                         struct au_branch *br)
11921 +{
11922 +       int err, wkq_err, whnamelen, e;
11923 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11924 +               = AUFS_WH_DR_INFO_PFX;
11925 +       struct au_drinfo *drinfo;
11926 +       struct qstr oldname;
11927 +       struct inode *h_dir, *delegated;
11928 +       struct dentry *info_dentry;
11929 +       struct path infopath;
11930 +
11931 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11932 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11933 +                                   sizeof(whname) - whnamelen);
11934 +       if (w->no_sio)
11935 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11936 +                                          &info_dentry);
11937 +       else {
11938 +               struct au_drinfo_do_load_args args = {
11939 +                       .drinfop        = &drinfo,
11940 +                       .h_ppath        = &w->h_ppath,
11941 +                       .whname         = whname,
11942 +                       .whnamelen      = whnamelen,
11943 +                       .info_dentry    = &info_dentry
11944 +               };
11945 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11946 +               if (unlikely(wkq_err))
11947 +                       drinfo = ERR_PTR(wkq_err);
11948 +       }
11949 +       err = PTR_ERR(drinfo);
11950 +       if (IS_ERR_OR_NULL(drinfo))
11951 +               goto out;
11952 +
11953 +       err = 0;
11954 +       oldname.len = drinfo->oldnamelen;
11955 +       oldname.name = drinfo->oldname;
11956 +       if (au_qstreq(w->qname, &oldname)) {
11957 +               /* the name is renamed back */
11958 +               au_kfree_rcu(drinfo);
11959 +               drinfo = NULL;
11960 +
11961 +               infopath.dentry = info_dentry;
11962 +               infopath.mnt = w->h_ppath.mnt;
11963 +               h_dir = d_inode(w->h_ppath.dentry);
11964 +               delegated = NULL;
11965 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11966 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11967 +               inode_unlock(h_dir);
11968 +               if (unlikely(e))
11969 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11970 +               if (unlikely(e == -EWOULDBLOCK))
11971 +                       iput(delegated);
11972 +       }
11973 +       au_kfree_rcu(w->drinfo[bindex]);
11974 +       w->drinfo[bindex] = drinfo;
11975 +       dput(info_dentry);
11976 +
11977 +out:
11978 +       AuTraceErr(err);
11979 +       return err;
11980 +}
11981 +
11982 +/* ---------------------------------------------------------------------- */
11983 +
11984 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11985 +{
11986 +       struct au_drinfo **p = drinfo;
11987 +
11988 +       while (n-- > 0)
11989 +               au_kfree_rcu(*drinfo++);
11990 +       au_kfree_try_rcu(p);
11991 +}
11992 +
11993 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
11994 +              aufs_bindex_t btgt)
11995 +{
11996 +       int err, ninfo;
11997 +       struct au_drinfo_load w;
11998 +       aufs_bindex_t bindex, bbot;
11999 +       struct au_branch *br;
12000 +       struct inode *h_dir;
12001 +       struct au_dr_hino *ent;
12002 +       struct super_block *sb;
12003 +
12004 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12005 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12006 +             AuLNPair(&lkup->whname), btgt);
12007 +
12008 +       sb = dentry->d_sb;
12009 +       bbot = au_sbbot(sb);
12010 +       w.ninfo = bbot + 1;
12011 +       if (!lkup->dirren.drinfo) {
12012 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12013 +                                             sizeof(*lkup->dirren.drinfo),
12014 +                                             GFP_NOFS);
12015 +               if (unlikely(!lkup->dirren.drinfo)) {
12016 +                       err = -ENOMEM;
12017 +                       goto out;
12018 +               }
12019 +               lkup->dirren.ninfo = w.ninfo;
12020 +       }
12021 +       w.drinfo = lkup->dirren.drinfo;
12022 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12023 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12024 +       AuDebugOn(!w.h_ppath.dentry);
12025 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12026 +       w.qname = &dentry->d_name;
12027 +
12028 +       ninfo = 0;
12029 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12030 +               br = au_sbr(sb, bindex);
12031 +               err = au_drinfo_load(&w, bindex, br);
12032 +               if (unlikely(err))
12033 +                       goto out_free;
12034 +               if (w.drinfo[bindex])
12035 +                       ninfo++;
12036 +       }
12037 +       if (!ninfo) {
12038 +               br = au_sbr(sb, btgt);
12039 +               h_dir = d_inode(w.h_ppath.dentry);
12040 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12041 +               AuDebugOn(!ent);
12042 +               au_dr_hino_del(&br->br_dirren, ent);
12043 +               au_kfree_rcu(ent);
12044 +       }
12045 +       goto out; /* success */
12046 +
12047 +out_free:
12048 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12049 +       lkup->dirren.ninfo = 0;
12050 +       lkup->dirren.drinfo = NULL;
12051 +out:
12052 +       AuTraceErr(err);
12053 +       return err;
12054 +}
12055 +
12056 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12057 +{
12058 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12059 +}
12060 +
12061 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12062 +{
12063 +       int err;
12064 +       struct au_drinfo *drinfo;
12065 +
12066 +       err = 0;
12067 +       if (!lkup->dirren.drinfo)
12068 +               goto out;
12069 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12070 +       drinfo = lkup->dirren.drinfo[btgt];
12071 +       if (!drinfo)
12072 +               goto out;
12073 +
12074 +       au_kfree_try_rcu(lkup->whname.name);
12075 +       lkup->whname.name = NULL;
12076 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12077 +       lkup->dirren.dr_name.name = drinfo->oldname;
12078 +       lkup->name = &lkup->dirren.dr_name;
12079 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12080 +       if (!err)
12081 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12082 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12083 +                     btgt);
12084 +
12085 +out:
12086 +       AuTraceErr(err);
12087 +       return err;
12088 +}
12089 +
12090 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12091 +                    ino_t h_ino)
12092 +{
12093 +       int match;
12094 +       struct au_drinfo *drinfo;
12095 +
12096 +       match = 1;
12097 +       if (!lkup->dirren.drinfo)
12098 +               goto out;
12099 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12100 +       drinfo = lkup->dirren.drinfo[bindex];
12101 +       if (!drinfo)
12102 +               goto out;
12103 +
12104 +       match = (drinfo->ino == h_ino);
12105 +       AuDbg("match %d\n", match);
12106 +
12107 +out:
12108 +       return match;
12109 +}
12110 +
12111 +/* ---------------------------------------------------------------------- */
12112 +
12113 +int au_dr_opt_set(struct super_block *sb)
12114 +{
12115 +       int err;
12116 +       aufs_bindex_t bindex, bbot;
12117 +       struct au_branch *br;
12118 +
12119 +       err = 0;
12120 +       bbot = au_sbbot(sb);
12121 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12122 +               br = au_sbr(sb, bindex);
12123 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12124 +       }
12125 +
12126 +       return err;
12127 +}
12128 +
12129 +int au_dr_opt_flush(struct super_block *sb)
12130 +{
12131 +       int err;
12132 +       aufs_bindex_t bindex, bbot;
12133 +       struct au_branch *br;
12134 +
12135 +       err = 0;
12136 +       bbot = au_sbbot(sb);
12137 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12138 +               br = au_sbr(sb, bindex);
12139 +               if (au_br_writable(br->br_perm))
12140 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12141 +       }
12142 +
12143 +       return err;
12144 +}
12145 +
12146 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12147 +{
12148 +       int err;
12149 +       aufs_bindex_t bindex, bbot;
12150 +       struct au_branch *br;
12151 +
12152 +       err = 0;
12153 +       if (!no_flush) {
12154 +               err = au_dr_opt_flush(sb);
12155 +               if (unlikely(err))
12156 +                       goto out;
12157 +       }
12158 +
12159 +       bbot = au_sbbot(sb);
12160 +       for (bindex = 0; bindex <= bbot; bindex++) {
12161 +               br = au_sbr(sb, bindex);
12162 +               au_dr_hino_free(&br->br_dirren);
12163 +       }
12164 +
12165 +out:
12166 +       return err;
12167 +}
12168 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12169 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12170 +++ linux/fs/aufs/dirren.h      2021-12-03 15:38:59.933313976 +0100
12171 @@ -0,0 +1,140 @@
12172 +/* SPDX-License-Identifier: GPL-2.0 */
12173 +/*
12174 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12175 + *
12176 + * This program, aufs is free software; you can redistribute it and/or modify
12177 + * it under the terms of the GNU General Public License as published by
12178 + * the Free Software Foundation; either version 2 of the License, or
12179 + * (at your option) any later version.
12180 + *
12181 + * This program is distributed in the hope that it will be useful,
12182 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12183 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12184 + * GNU General Public License for more details.
12185 + *
12186 + * You should have received a copy of the GNU General Public License
12187 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12188 + */
12189 +
12190 +/*
12191 + * renamed dir info
12192 + */
12193 +
12194 +#ifndef __AUFS_DIRREN_H__
12195 +#define __AUFS_DIRREN_H__
12196 +
12197 +#ifdef __KERNEL__
12198 +
12199 +#include <linux/dcache.h>
12200 +#include <linux/statfs.h>
12201 +#include <linux/uuid.h>
12202 +#include "hbl.h"
12203 +
12204 +#define AuDirren_NHASH 100
12205 +
12206 +#ifdef CONFIG_AUFS_DIRREN
12207 +enum au_brid_type {
12208 +       AuBrid_Unset,
12209 +       AuBrid_UUID,
12210 +       AuBrid_FSID,
12211 +       AuBrid_DEV
12212 +};
12213 +
12214 +struct au_dr_brid {
12215 +       enum au_brid_type       type;
12216 +       union {
12217 +               uuid_t  uuid;   /* unimplemented yet */
12218 +               fsid_t  fsid;
12219 +               dev_t   dev;
12220 +       };
12221 +};
12222 +
12223 +/* 20 is the max digits length of ulong 64 */
12224 +/* brid-type "_" uuid "_" inum */
12225 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12226 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12227 +
12228 +struct au_dr_hino {
12229 +       struct hlist_bl_node    dr_hnode;
12230 +       ino_t                   dr_h_ino;
12231 +};
12232 +
12233 +struct au_dr_br {
12234 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12235 +       struct au_dr_brid       dr_brid;
12236 +};
12237 +
12238 +struct au_dr_lookup {
12239 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12240 +       struct qstr             dr_name; /* subset of dr_info */
12241 +       aufs_bindex_t           ninfo;
12242 +       struct au_drinfo        **drinfo;
12243 +};
12244 +#else
12245 +struct au_dr_hino;
12246 +/* empty */
12247 +struct au_dr_br { };
12248 +struct au_dr_lookup { };
12249 +#endif
12250 +
12251 +/* ---------------------------------------------------------------------- */
12252 +
12253 +struct au_branch;
12254 +struct au_do_lookup_args;
12255 +struct au_hinode;
12256 +#ifdef CONFIG_AUFS_DIRREN
12257 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12258 +                       struct au_dr_hino *add_ent);
12259 +void au_dr_hino_free(struct au_dr_br *dr);
12260 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12261 +                 const struct path *path);
12262 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12263 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12264 +                struct qstr *dst_name, void *_rev);
12265 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12266 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12267 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12268 +              aufs_bindex_t bindex);
12269 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12270 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12271 +                    ino_t h_ino);
12272 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12273 +int au_dr_opt_set(struct super_block *sb);
12274 +int au_dr_opt_flush(struct super_block *sb);
12275 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12276 +#else
12277 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12278 +          struct au_dr_hino *add_ent);
12279 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12280 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12281 +          const struct path *path);
12282 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12283 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12284 +          struct qstr *dst_name, void *_rev);
12285 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12286 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12287 +          void *rev);
12288 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12289 +          aufs_bindex_t bindex);
12290 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12291 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12292 +          aufs_bindex_t bindex, ino_t h_ino);
12293 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12294 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12295 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12296 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12297 +#endif
12298 +
12299 +/* ---------------------------------------------------------------------- */
12300 +
12301 +#ifdef CONFIG_AUFS_DIRREN
12302 +static inline int au_dr_ihash(ino_t h_ino)
12303 +{
12304 +       return h_ino % AuDirren_NHASH;
12305 +}
12306 +#else
12307 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12308 +#endif
12309 +
12310 +#endif /* __KERNEL__ */
12311 +#endif /* __AUFS_DIRREN_H__ */
12312 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12313 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12314 +++ linux/fs/aufs/dynop.c       2021-12-03 15:38:59.933313976 +0100
12315 @@ -0,0 +1,368 @@
12316 +// SPDX-License-Identifier: GPL-2.0
12317 +/*
12318 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12319 + *
12320 + * This program, aufs is free software; you can redistribute it and/or modify
12321 + * it under the terms of the GNU General Public License as published by
12322 + * the Free Software Foundation; either version 2 of the License, or
12323 + * (at your option) any later version.
12324 + *
12325 + * This program is distributed in the hope that it will be useful,
12326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12328 + * GNU General Public License for more details.
12329 + *
12330 + * You should have received a copy of the GNU General Public License
12331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12332 + */
12333 +
12334 +/*
12335 + * dynamically customizable operations for regular files
12336 + */
12337 +
12338 +#include "aufs.h"
12339 +
12340 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12341 +
12342 +/*
12343 + * How large will these lists be?
12344 + * Usually just a few elements, 20-30 at most for each, I guess.
12345 + */
12346 +static struct hlist_bl_head dynop[AuDyLast];
12347 +
12348 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12349 +                                    const void *h_op)
12350 +{
12351 +       struct au_dykey *key, *tmp;
12352 +       struct hlist_bl_node *pos;
12353 +
12354 +       key = NULL;
12355 +       hlist_bl_lock(hbl);
12356 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12357 +               if (tmp->dk_op.dy_hop == h_op) {
12358 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12359 +                               key = tmp;
12360 +                       break;
12361 +               }
12362 +       hlist_bl_unlock(hbl);
12363 +
12364 +       return key;
12365 +}
12366 +
12367 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12368 +{
12369 +       struct au_dykey **k, *found;
12370 +       const void *h_op = key->dk_op.dy_hop;
12371 +       int i;
12372 +
12373 +       found = NULL;
12374 +       k = br->br_dykey;
12375 +       for (i = 0; i < AuBrDynOp; i++)
12376 +               if (k[i]) {
12377 +                       if (k[i]->dk_op.dy_hop == h_op) {
12378 +                               found = k[i];
12379 +                               break;
12380 +                       }
12381 +               } else
12382 +                       break;
12383 +       if (!found) {
12384 +               spin_lock(&br->br_dykey_lock);
12385 +               for (; i < AuBrDynOp; i++)
12386 +                       if (k[i]) {
12387 +                               if (k[i]->dk_op.dy_hop == h_op) {
12388 +                                       found = k[i];
12389 +                                       break;
12390 +                               }
12391 +                       } else {
12392 +                               k[i] = key;
12393 +                               break;
12394 +                       }
12395 +               spin_unlock(&br->br_dykey_lock);
12396 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12397 +       }
12398 +
12399 +       return found;
12400 +}
12401 +
12402 +/* kref_get() if @key is already added */
12403 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12404 +{
12405 +       struct au_dykey *tmp, *found;
12406 +       struct hlist_bl_node *pos;
12407 +       const void *h_op = key->dk_op.dy_hop;
12408 +
12409 +       found = NULL;
12410 +       hlist_bl_lock(hbl);
12411 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12412 +               if (tmp->dk_op.dy_hop == h_op) {
12413 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12414 +                               found = tmp;
12415 +                       break;
12416 +               }
12417 +       if (!found)
12418 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12419 +       hlist_bl_unlock(hbl);
12420 +
12421 +       if (!found)
12422 +               DyPrSym(key);
12423 +       return found;
12424 +}
12425 +
12426 +static void dy_free_rcu(struct rcu_head *rcu)
12427 +{
12428 +       struct au_dykey *key;
12429 +
12430 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12431 +       DyPrSym(key);
12432 +       kfree(key);
12433 +}
12434 +
12435 +static void dy_free(struct kref *kref)
12436 +{
12437 +       struct au_dykey *key;
12438 +       struct hlist_bl_head *hbl;
12439 +
12440 +       key = container_of(kref, struct au_dykey, dk_kref);
12441 +       hbl = dynop + key->dk_op.dy_type;
12442 +       au_hbl_del(&key->dk_hnode, hbl);
12443 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12444 +}
12445 +
12446 +void au_dy_put(struct au_dykey *key)
12447 +{
12448 +       kref_put(&key->dk_kref, dy_free);
12449 +}
12450 +
12451 +/* ---------------------------------------------------------------------- */
12452 +
12453 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12454 +
12455 +#ifdef CONFIG_AUFS_DEBUG
12456 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12457 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12458 +#else
12459 +#define DyDbgDeclare(cnt)      do {} while (0)
12460 +#define DyDbgInc(cnt)          do {} while (0)
12461 +#endif
12462 +
12463 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12464 +       DyDbgInc(cnt);                                                  \
12465 +       if (h_op->func) {                                               \
12466 +               if (src.func)                                           \
12467 +                       dst.func = src.func;                            \
12468 +               else                                                    \
12469 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12470 +       }                                                               \
12471 +} while (0)
12472 +
12473 +#define DySetForce(func, dst, src) do {                \
12474 +       AuDebugOn(!src.func);                   \
12475 +       DyDbgInc(cnt);                          \
12476 +       dst.func = src.func;                    \
12477 +} while (0)
12478 +
12479 +#define DySetAop(func) \
12480 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12481 +#define DySetAopForce(func) \
12482 +       DySetForce(func, dyaop->da_op, aufs_aop)
12483 +
12484 +static void dy_aop(struct au_dykey *key, const void *h_op,
12485 +                  struct super_block *h_sb __maybe_unused)
12486 +{
12487 +       struct au_dyaop *dyaop = (void *)key;
12488 +       const struct address_space_operations *h_aop = h_op;
12489 +       DyDbgDeclare(cnt);
12490 +
12491 +       AuDbg("%s\n", au_sbtype(h_sb));
12492 +
12493 +       DySetAop(writepage);
12494 +       DySetAopForce(readpage);        /* force */
12495 +       DySetAop(writepages);
12496 +       DySetAop(set_page_dirty);
12497 +       DySetAop(readpages);
12498 +       DySetAop(readahead);
12499 +       DySetAop(write_begin);
12500 +       DySetAop(write_end);
12501 +       DySetAop(bmap);
12502 +       DySetAop(invalidatepage);
12503 +       DySetAop(releasepage);
12504 +       DySetAop(freepage);
12505 +       /* this one will be changed according to an aufs mount option */
12506 +       DySetAop(direct_IO);
12507 +       DySetAop(migratepage);
12508 +       DySetAop(isolate_page);
12509 +       DySetAop(putback_page);
12510 +       DySetAop(launder_page);
12511 +       DySetAop(is_partially_uptodate);
12512 +       DySetAop(is_dirty_writeback);
12513 +       DySetAop(error_remove_page);
12514 +       DySetAop(swap_activate);
12515 +       DySetAop(swap_deactivate);
12516 +
12517 +       DyDbgSize(cnt, *h_aop);
12518 +}
12519 +
12520 +/* ---------------------------------------------------------------------- */
12521 +
12522 +static void dy_bug(struct kref *kref)
12523 +{
12524 +       BUG();
12525 +}
12526 +
12527 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12528 +{
12529 +       struct au_dykey *key, *old;
12530 +       struct hlist_bl_head *hbl;
12531 +       struct op {
12532 +               unsigned int sz;
12533 +               void (*set)(struct au_dykey *key, const void *h_op,
12534 +                           struct super_block *h_sb __maybe_unused);
12535 +       };
12536 +       static const struct op a[] = {
12537 +               [AuDy_AOP] = {
12538 +                       .sz     = sizeof(struct au_dyaop),
12539 +                       .set    = dy_aop
12540 +               }
12541 +       };
12542 +       const struct op *p;
12543 +
12544 +       hbl = dynop + op->dy_type;
12545 +       key = dy_gfind_get(hbl, op->dy_hop);
12546 +       if (key)
12547 +               goto out_add; /* success */
12548 +
12549 +       p = a + op->dy_type;
12550 +       key = kzalloc(p->sz, GFP_NOFS);
12551 +       if (unlikely(!key)) {
12552 +               key = ERR_PTR(-ENOMEM);
12553 +               goto out;
12554 +       }
12555 +
12556 +       key->dk_op.dy_hop = op->dy_hop;
12557 +       kref_init(&key->dk_kref);
12558 +       p->set(key, op->dy_hop, au_br_sb(br));
12559 +       old = dy_gadd(hbl, key);
12560 +       if (old) {
12561 +               au_kfree_rcu(key);
12562 +               key = old;
12563 +       }
12564 +
12565 +out_add:
12566 +       old = dy_bradd(br, key);
12567 +       if (old)
12568 +               /* its ref-count should never be zero here */
12569 +               kref_put(&key->dk_kref, dy_bug);
12570 +out:
12571 +       return key;
12572 +}
12573 +
12574 +/* ---------------------------------------------------------------------- */
12575 +/*
12576 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12577 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12578 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12579 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12580 + * See the aufs manual in detail.
12581 + */
12582 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12583 +{
12584 +       if (!do_dx)
12585 +               dyaop->da_op.direct_IO = NULL;
12586 +       else
12587 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12588 +}
12589 +
12590 +static struct au_dyaop *dy_aget(struct au_branch *br,
12591 +                               const struct address_space_operations *h_aop,
12592 +                               int do_dx)
12593 +{
12594 +       struct au_dyaop *dyaop;
12595 +       struct au_dynop op;
12596 +
12597 +       op.dy_type = AuDy_AOP;
12598 +       op.dy_haop = h_aop;
12599 +       dyaop = (void *)dy_get(&op, br);
12600 +       if (IS_ERR(dyaop))
12601 +               goto out;
12602 +       dy_adx(dyaop, do_dx);
12603 +
12604 +out:
12605 +       return dyaop;
12606 +}
12607 +
12608 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12609 +               struct inode *h_inode)
12610 +{
12611 +       int err, do_dx;
12612 +       struct super_block *sb;
12613 +       struct au_branch *br;
12614 +       struct au_dyaop *dyaop;
12615 +
12616 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12617 +       IiMustWriteLock(inode);
12618 +
12619 +       sb = inode->i_sb;
12620 +       br = au_sbr(sb, bindex);
12621 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12622 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12623 +       err = PTR_ERR(dyaop);
12624 +       if (IS_ERR(dyaop))
12625 +               /* unnecessary to call dy_fput() */
12626 +               goto out;
12627 +
12628 +       err = 0;
12629 +       inode->i_mapping->a_ops = &dyaop->da_op;
12630 +
12631 +out:
12632 +       return err;
12633 +}
12634 +
12635 +/*
12636 + * Is it safe to replace a_ops during the inode/file is in operation?
12637 + * Yes, I hope so.
12638 + */
12639 +int au_dy_irefresh(struct inode *inode)
12640 +{
12641 +       int err;
12642 +       aufs_bindex_t btop;
12643 +       struct inode *h_inode;
12644 +
12645 +       err = 0;
12646 +       if (S_ISREG(inode->i_mode)) {
12647 +               btop = au_ibtop(inode);
12648 +               h_inode = au_h_iptr(inode, btop);
12649 +               err = au_dy_iaop(inode, btop, h_inode);
12650 +       }
12651 +       return err;
12652 +}
12653 +
12654 +void au_dy_arefresh(int do_dx)
12655 +{
12656 +       struct hlist_bl_head *hbl;
12657 +       struct hlist_bl_node *pos;
12658 +       struct au_dykey *key;
12659 +
12660 +       hbl = dynop + AuDy_AOP;
12661 +       hlist_bl_lock(hbl);
12662 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12663 +               dy_adx((void *)key, do_dx);
12664 +       hlist_bl_unlock(hbl);
12665 +}
12666 +
12667 +/* ---------------------------------------------------------------------- */
12668 +
12669 +void __init au_dy_init(void)
12670 +{
12671 +       int i;
12672 +
12673 +       for (i = 0; i < AuDyLast; i++)
12674 +               INIT_HLIST_BL_HEAD(dynop + i);
12675 +}
12676 +
12677 +void au_dy_fin(void)
12678 +{
12679 +       int i;
12680 +
12681 +       for (i = 0; i < AuDyLast; i++)
12682 +               WARN_ON(!hlist_bl_empty(dynop + i));
12683 +}
12684 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12685 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12686 +++ linux/fs/aufs/dynop.h       2021-12-03 15:38:59.933313976 +0100
12687 @@ -0,0 +1,77 @@
12688 +/* SPDX-License-Identifier: GPL-2.0 */
12689 +/*
12690 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12691 + *
12692 + * This program, aufs is free software; you can redistribute it and/or modify
12693 + * it under the terms of the GNU General Public License as published by
12694 + * the Free Software Foundation; either version 2 of the License, or
12695 + * (at your option) any later version.
12696 + *
12697 + * This program is distributed in the hope that it will be useful,
12698 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12699 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12700 + * GNU General Public License for more details.
12701 + *
12702 + * You should have received a copy of the GNU General Public License
12703 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12704 + */
12705 +
12706 +/*
12707 + * dynamically customizable operations (for regular files only)
12708 + */
12709 +
12710 +#ifndef __AUFS_DYNOP_H__
12711 +#define __AUFS_DYNOP_H__
12712 +
12713 +#ifdef __KERNEL__
12714 +
12715 +#include <linux/fs.h>
12716 +#include <linux/kref.h>
12717 +
12718 +enum {AuDy_AOP, AuDyLast};
12719 +
12720 +struct au_dynop {
12721 +       int                                             dy_type;
12722 +       union {
12723 +               const void                              *dy_hop;
12724 +               const struct address_space_operations   *dy_haop;
12725 +       };
12726 +};
12727 +
12728 +struct au_dykey {
12729 +       union {
12730 +               struct hlist_bl_node    dk_hnode;
12731 +               struct rcu_head         dk_rcu;
12732 +       };
12733 +       struct au_dynop         dk_op;
12734 +
12735 +       /*
12736 +        * during I am in the branch local array, kref is gotten. when the
12737 +        * branch is removed, kref is put.
12738 +        */
12739 +       struct kref             dk_kref;
12740 +};
12741 +
12742 +/* stop unioning since their sizes are very different from each other */
12743 +struct au_dyaop {
12744 +       struct au_dykey                 da_key;
12745 +       struct address_space_operations da_op; /* not const */
12746 +};
12747 +/* make sure that 'struct au_dykey *' can be any type */
12748 +static_assert(!offsetof(struct au_dyaop, da_key));
12749 +
12750 +/* ---------------------------------------------------------------------- */
12751 +
12752 +/* dynop.c */
12753 +struct au_branch;
12754 +void au_dy_put(struct au_dykey *key);
12755 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12756 +               struct inode *h_inode);
12757 +int au_dy_irefresh(struct inode *inode);
12758 +void au_dy_arefresh(int do_dio);
12759 +
12760 +void __init au_dy_init(void);
12761 +void au_dy_fin(void);
12762 +
12763 +#endif /* __KERNEL__ */
12764 +#endif /* __AUFS_DYNOP_H__ */
12765 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12766 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12767 +++ linux/fs/aufs/export.c      2021-12-03 15:40:58.233313963 +0100
12768 @@ -0,0 +1,831 @@
12769 +// SPDX-License-Identifier: GPL-2.0
12770 +/*
12771 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12772 + *
12773 + * This program, aufs is free software; you can redistribute it and/or modify
12774 + * it under the terms of the GNU General Public License as published by
12775 + * the Free Software Foundation; either version 2 of the License, or
12776 + * (at your option) any later version.
12777 + *
12778 + * This program is distributed in the hope that it will be useful,
12779 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12780 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12781 + * GNU General Public License for more details.
12782 + *
12783 + * You should have received a copy of the GNU General Public License
12784 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12785 + */
12786 +
12787 +/*
12788 + * export via nfs
12789 + */
12790 +
12791 +#include <linux/exportfs.h>
12792 +#include <linux/fs_struct.h>
12793 +#include <linux/namei.h>
12794 +#include <linux/nsproxy.h>
12795 +#include <linux/random.h>
12796 +#include <linux/writeback.h>
12797 +#include "aufs.h"
12798 +
12799 +union conv {
12800 +#ifdef CONFIG_AUFS_INO_T_64
12801 +       __u32 a[2];
12802 +#else
12803 +       __u32 a[1];
12804 +#endif
12805 +       ino_t ino;
12806 +};
12807 +
12808 +static ino_t decode_ino(__u32 *a)
12809 +{
12810 +       union conv u;
12811 +
12812 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12813 +       u.a[0] = a[0];
12814 +#ifdef CONFIG_AUFS_INO_T_64
12815 +       u.a[1] = a[1];
12816 +#endif
12817 +       return u.ino;
12818 +}
12819 +
12820 +static void encode_ino(__u32 *a, ino_t ino)
12821 +{
12822 +       union conv u;
12823 +
12824 +       u.ino = ino;
12825 +       a[0] = u.a[0];
12826 +#ifdef CONFIG_AUFS_INO_T_64
12827 +       a[1] = u.a[1];
12828 +#endif
12829 +}
12830 +
12831 +/* NFS file handle */
12832 +enum {
12833 +       Fh_br_id,
12834 +       Fh_sigen,
12835 +#ifdef CONFIG_AUFS_INO_T_64
12836 +       /* support 64bit inode number */
12837 +       Fh_ino1,
12838 +       Fh_ino2,
12839 +       Fh_dir_ino1,
12840 +       Fh_dir_ino2,
12841 +#else
12842 +       Fh_ino1,
12843 +       Fh_dir_ino1,
12844 +#endif
12845 +       Fh_igen,
12846 +       Fh_h_type,
12847 +       Fh_tail,
12848 +
12849 +       Fh_ino = Fh_ino1,
12850 +       Fh_dir_ino = Fh_dir_ino1
12851 +};
12852 +
12853 +static int au_test_anon(struct dentry *dentry)
12854 +{
12855 +       /* note: read d_flags without d_lock */
12856 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12857 +}
12858 +
12859 +int au_test_nfsd(void)
12860 +{
12861 +       int ret;
12862 +       struct task_struct *tsk = current;
12863 +       char comm[sizeof(tsk->comm)];
12864 +
12865 +       ret = 0;
12866 +       if (tsk->flags & PF_KTHREAD) {
12867 +               get_task_comm(comm, tsk);
12868 +               ret = !strcmp(comm, "nfsd");
12869 +       }
12870 +
12871 +       return ret;
12872 +}
12873 +
12874 +/* ---------------------------------------------------------------------- */
12875 +/* inode generation external table */
12876 +
12877 +void au_xigen_inc(struct inode *inode)
12878 +{
12879 +       loff_t pos;
12880 +       ssize_t sz;
12881 +       __u32 igen;
12882 +       struct super_block *sb;
12883 +       struct au_sbinfo *sbinfo;
12884 +
12885 +       sb = inode->i_sb;
12886 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12887 +
12888 +       sbinfo = au_sbi(sb);
12889 +       pos = inode->i_ino;
12890 +       pos *= sizeof(igen);
12891 +       igen = inode->i_generation + 1;
12892 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12893 +       if (sz == sizeof(igen))
12894 +               return; /* success */
12895 +
12896 +       if (unlikely(sz >= 0))
12897 +               AuIOErr("xigen error (%zd)\n", sz);
12898 +}
12899 +
12900 +int au_xigen_new(struct inode *inode)
12901 +{
12902 +       int err;
12903 +       loff_t pos;
12904 +       ssize_t sz;
12905 +       struct super_block *sb;
12906 +       struct au_sbinfo *sbinfo;
12907 +       struct file *file;
12908 +
12909 +       err = 0;
12910 +       /* todo: dirty, at mount time */
12911 +       if (inode->i_ino == AUFS_ROOT_INO)
12912 +               goto out;
12913 +       sb = inode->i_sb;
12914 +       SiMustAnyLock(sb);
12915 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12916 +               goto out;
12917 +
12918 +       err = -EFBIG;
12919 +       pos = inode->i_ino;
12920 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12921 +               AuIOErr1("too large i%lld\n", pos);
12922 +               goto out;
12923 +       }
12924 +       pos *= sizeof(inode->i_generation);
12925 +
12926 +       err = 0;
12927 +       sbinfo = au_sbi(sb);
12928 +       file = sbinfo->si_xigen;
12929 +       BUG_ON(!file);
12930 +
12931 +       if (vfsub_f_size_read(file)
12932 +           < pos + sizeof(inode->i_generation)) {
12933 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12934 +               sz = xino_fwrite(file, &inode->i_generation,
12935 +                                sizeof(inode->i_generation), &pos);
12936 +       } else
12937 +               sz = xino_fread(file, &inode->i_generation,
12938 +                               sizeof(inode->i_generation), &pos);
12939 +       if (sz == sizeof(inode->i_generation))
12940 +               goto out; /* success */
12941 +
12942 +       err = sz;
12943 +       if (unlikely(sz >= 0)) {
12944 +               err = -EIO;
12945 +               AuIOErr("xigen error (%zd)\n", sz);
12946 +       }
12947 +
12948 +out:
12949 +       return err;
12950 +}
12951 +
12952 +int au_xigen_set(struct super_block *sb, struct path *path)
12953 +{
12954 +       int err;
12955 +       struct au_sbinfo *sbinfo;
12956 +       struct file *file;
12957 +
12958 +       SiMustWriteLock(sb);
12959 +
12960 +       sbinfo = au_sbi(sb);
12961 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12962 +       err = PTR_ERR(file);
12963 +       if (IS_ERR(file))
12964 +               goto out;
12965 +       err = 0;
12966 +       if (sbinfo->si_xigen)
12967 +               fput(sbinfo->si_xigen);
12968 +       sbinfo->si_xigen = file;
12969 +
12970 +out:
12971 +       AuTraceErr(err);
12972 +       return err;
12973 +}
12974 +
12975 +void au_xigen_clr(struct super_block *sb)
12976 +{
12977 +       struct au_sbinfo *sbinfo;
12978 +
12979 +       SiMustWriteLock(sb);
12980 +
12981 +       sbinfo = au_sbi(sb);
12982 +       if (sbinfo->si_xigen) {
12983 +               fput(sbinfo->si_xigen);
12984 +               sbinfo->si_xigen = NULL;
12985 +       }
12986 +}
12987 +
12988 +/* ---------------------------------------------------------------------- */
12989 +
12990 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
12991 +                                   ino_t dir_ino)
12992 +{
12993 +       struct dentry *dentry, *d;
12994 +       struct inode *inode;
12995 +       unsigned int sigen;
12996 +
12997 +       dentry = NULL;
12998 +       inode = ilookup(sb, ino);
12999 +       if (!inode)
13000 +               goto out;
13001 +
13002 +       dentry = ERR_PTR(-ESTALE);
13003 +       sigen = au_sigen(sb);
13004 +       if (unlikely(au_is_bad_inode(inode)
13005 +                    || IS_DEADDIR(inode)
13006 +                    || sigen != au_iigen(inode, NULL)))
13007 +               goto out_iput;
13008 +
13009 +       dentry = NULL;
13010 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13011 +               dentry = d_find_alias(inode);
13012 +       else {
13013 +               spin_lock(&inode->i_lock);
13014 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13015 +                       spin_lock(&d->d_lock);
13016 +                       if (!au_test_anon(d)
13017 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13018 +                               dentry = dget_dlock(d);
13019 +                               spin_unlock(&d->d_lock);
13020 +                               break;
13021 +                       }
13022 +                       spin_unlock(&d->d_lock);
13023 +               }
13024 +               spin_unlock(&inode->i_lock);
13025 +       }
13026 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13027 +               /* need to refresh */
13028 +               dput(dentry);
13029 +               dentry = NULL;
13030 +       }
13031 +
13032 +out_iput:
13033 +       iput(inode);
13034 +out:
13035 +       AuTraceErrPtr(dentry);
13036 +       return dentry;
13037 +}
13038 +
13039 +/* ---------------------------------------------------------------------- */
13040 +
13041 +/* todo: dirty? */
13042 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13043 +
13044 +struct au_compare_mnt_args {
13045 +       /* input */
13046 +       struct super_block *sb;
13047 +
13048 +       /* output */
13049 +       struct vfsmount *mnt;
13050 +};
13051 +
13052 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13053 +{
13054 +       struct au_compare_mnt_args *a = arg;
13055 +
13056 +       if (mnt->mnt_sb != a->sb)
13057 +               return 0;
13058 +       a->mnt = mntget(mnt);
13059 +       return 1;
13060 +}
13061 +
13062 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13063 +{
13064 +       int err;
13065 +       struct path root;
13066 +       struct au_compare_mnt_args args = {
13067 +               .sb = sb
13068 +       };
13069 +
13070 +       get_fs_root(current->fs, &root);
13071 +       rcu_read_lock();
13072 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13073 +       rcu_read_unlock();
13074 +       path_put(&root);
13075 +       AuDebugOn(!err);
13076 +       AuDebugOn(!args.mnt);
13077 +       return args.mnt;
13078 +}
13079 +
13080 +struct au_nfsd_si_lock {
13081 +       unsigned int sigen;
13082 +       aufs_bindex_t bindex, br_id;
13083 +       unsigned char force_lock;
13084 +};
13085 +
13086 +static int si_nfsd_read_lock(struct super_block *sb,
13087 +                            struct au_nfsd_si_lock *nsi_lock)
13088 +{
13089 +       int err;
13090 +       aufs_bindex_t bindex;
13091 +
13092 +       si_read_lock(sb, AuLock_FLUSH);
13093 +
13094 +       /* branch id may be wrapped around */
13095 +       err = 0;
13096 +       bindex = au_br_index(sb, nsi_lock->br_id);
13097 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13098 +               goto out; /* success */
13099 +
13100 +       err = -ESTALE;
13101 +       bindex = -1;
13102 +       if (!nsi_lock->force_lock)
13103 +               si_read_unlock(sb);
13104 +
13105 +out:
13106 +       nsi_lock->bindex = bindex;
13107 +       return err;
13108 +}
13109 +
13110 +struct find_name_by_ino {
13111 +       struct dir_context ctx;
13112 +       int called, found;
13113 +       ino_t ino;
13114 +       char *name;
13115 +       int namelen;
13116 +};
13117 +
13118 +static int
13119 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13120 +                loff_t offset, u64 ino, unsigned int d_type)
13121 +{
13122 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13123 +                                                 ctx);
13124 +
13125 +       a->called++;
13126 +       if (a->ino != ino)
13127 +               return 0;
13128 +
13129 +       memcpy(a->name, name, namelen);
13130 +       a->namelen = namelen;
13131 +       a->found = 1;
13132 +       return 1;
13133 +}
13134 +
13135 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13136 +                                    struct au_nfsd_si_lock *nsi_lock)
13137 +{
13138 +       struct dentry *dentry, *parent;
13139 +       struct file *file;
13140 +       struct inode *dir;
13141 +       struct find_name_by_ino arg = {
13142 +               .ctx = {
13143 +                       .actor = find_name_by_ino
13144 +               }
13145 +       };
13146 +       int err;
13147 +
13148 +       parent = path->dentry;
13149 +       if (nsi_lock)
13150 +               si_read_unlock(parent->d_sb);
13151 +       file = vfsub_dentry_open(path, au_dir_roflags);
13152 +       dentry = (void *)file;
13153 +       if (IS_ERR(file))
13154 +               goto out;
13155 +
13156 +       dentry = ERR_PTR(-ENOMEM);
13157 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13158 +       if (unlikely(!arg.name))
13159 +               goto out_file;
13160 +       arg.ino = ino;
13161 +       arg.found = 0;
13162 +       do {
13163 +               arg.called = 0;
13164 +               /* smp_mb(); */
13165 +               err = vfsub_iterate_dir(file, &arg.ctx);
13166 +       } while (!err && !arg.found && arg.called);
13167 +       dentry = ERR_PTR(err);
13168 +       if (unlikely(err))
13169 +               goto out_name;
13170 +       /* instead of ENOENT */
13171 +       dentry = ERR_PTR(-ESTALE);
13172 +       if (!arg.found)
13173 +               goto out_name;
13174 +
13175 +       /* do not call vfsub_lkup_one() */
13176 +       dir = d_inode(parent);
13177 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13178 +       AuTraceErrPtr(dentry);
13179 +       if (IS_ERR(dentry))
13180 +               goto out_name;
13181 +       AuDebugOn(au_test_anon(dentry));
13182 +       if (unlikely(d_really_is_negative(dentry))) {
13183 +               dput(dentry);
13184 +               dentry = ERR_PTR(-ENOENT);
13185 +       }
13186 +
13187 +out_name:
13188 +       free_page((unsigned long)arg.name);
13189 +out_file:
13190 +       fput(file);
13191 +out:
13192 +       if (unlikely(nsi_lock
13193 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13194 +               if (!IS_ERR(dentry)) {
13195 +                       dput(dentry);
13196 +                       dentry = ERR_PTR(-ESTALE);
13197 +               }
13198 +       AuTraceErrPtr(dentry);
13199 +       return dentry;
13200 +}
13201 +
13202 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13203 +                                       ino_t dir_ino,
13204 +                                       struct au_nfsd_si_lock *nsi_lock)
13205 +{
13206 +       struct dentry *dentry;
13207 +       struct path path;
13208 +
13209 +       if (dir_ino != AUFS_ROOT_INO) {
13210 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13211 +               dentry = path.dentry;
13212 +               if (!path.dentry || IS_ERR(path.dentry))
13213 +                       goto out;
13214 +               AuDebugOn(au_test_anon(path.dentry));
13215 +       } else
13216 +               path.dentry = dget(sb->s_root);
13217 +
13218 +       path.mnt = au_mnt_get(sb);
13219 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13220 +       path_put(&path);
13221 +
13222 +out:
13223 +       AuTraceErrPtr(dentry);
13224 +       return dentry;
13225 +}
13226 +
13227 +/* ---------------------------------------------------------------------- */
13228 +
13229 +static int h_acceptable(void *expv, struct dentry *dentry)
13230 +{
13231 +       return 1;
13232 +}
13233 +
13234 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13235 +                          char *buf, int len, struct super_block *sb)
13236 +{
13237 +       char *p;
13238 +       int n;
13239 +       struct path path;
13240 +
13241 +       p = d_path(h_rootpath, buf, len);
13242 +       if (IS_ERR(p))
13243 +               goto out;
13244 +       n = strlen(p);
13245 +
13246 +       path.mnt = h_rootpath->mnt;
13247 +       path.dentry = h_parent;
13248 +       p = d_path(&path, buf, len);
13249 +       if (IS_ERR(p))
13250 +               goto out;
13251 +       if (n != 1)
13252 +               p += n;
13253 +
13254 +       path.mnt = au_mnt_get(sb);
13255 +       path.dentry = sb->s_root;
13256 +       p = d_path(&path, buf, len - strlen(p));
13257 +       mntput(path.mnt);
13258 +       if (IS_ERR(p))
13259 +               goto out;
13260 +       if (n != 1)
13261 +               p[strlen(p)] = '/';
13262 +
13263 +out:
13264 +       AuTraceErrPtr(p);
13265 +       return p;
13266 +}
13267 +
13268 +static
13269 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13270 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13271 +{
13272 +       struct dentry *dentry, *h_parent, *root;
13273 +       struct super_block *h_sb;
13274 +       char *pathname, *p;
13275 +       struct vfsmount *h_mnt;
13276 +       struct au_branch *br;
13277 +       int err;
13278 +       struct path path;
13279 +
13280 +       br = au_sbr(sb, nsi_lock->bindex);
13281 +       h_mnt = au_br_mnt(br);
13282 +       h_sb = h_mnt->mnt_sb;
13283 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13284 +       lockdep_off();
13285 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13286 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13287 +                                     h_acceptable, /*context*/NULL);
13288 +       lockdep_on();
13289 +       dentry = h_parent;
13290 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13291 +               AuWarn1("%s decode_fh failed, %ld\n",
13292 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13293 +               goto out;
13294 +       }
13295 +       dentry = NULL;
13296 +       if (unlikely(au_test_anon(h_parent))) {
13297 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13298 +                       au_sbtype(h_sb));
13299 +               goto out_h_parent;
13300 +       }
13301 +
13302 +       dentry = ERR_PTR(-ENOMEM);
13303 +       pathname = (void *)__get_free_page(GFP_NOFS);
13304 +       if (unlikely(!pathname))
13305 +               goto out_h_parent;
13306 +
13307 +       root = sb->s_root;
13308 +       path.mnt = h_mnt;
13309 +       di_read_lock_parent(root, !AuLock_IR);
13310 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13311 +       di_read_unlock(root, !AuLock_IR);
13312 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13313 +       dentry = (void *)p;
13314 +       if (IS_ERR(p))
13315 +               goto out_pathname;
13316 +
13317 +       si_read_unlock(sb);
13318 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13319 +       dentry = ERR_PTR(err);
13320 +       if (unlikely(err))
13321 +               goto out_relock;
13322 +
13323 +       dentry = ERR_PTR(-ENOENT);
13324 +       AuDebugOn(au_test_anon(path.dentry));
13325 +       if (unlikely(d_really_is_negative(path.dentry)))
13326 +               goto out_path;
13327 +
13328 +       if (ino != d_inode(path.dentry)->i_ino)
13329 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13330 +       else
13331 +               dentry = dget(path.dentry);
13332 +
13333 +out_path:
13334 +       path_put(&path);
13335 +out_relock:
13336 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13337 +               if (!IS_ERR(dentry)) {
13338 +                       dput(dentry);
13339 +                       dentry = ERR_PTR(-ESTALE);
13340 +               }
13341 +out_pathname:
13342 +       free_page((unsigned long)pathname);
13343 +out_h_parent:
13344 +       dput(h_parent);
13345 +out:
13346 +       AuTraceErrPtr(dentry);
13347 +       return dentry;
13348 +}
13349 +
13350 +/* ---------------------------------------------------------------------- */
13351 +
13352 +static struct dentry *
13353 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13354 +                 int fh_type)
13355 +{
13356 +       struct dentry *dentry;
13357 +       __u32 *fh = fid->raw;
13358 +       struct au_branch *br;
13359 +       ino_t ino, dir_ino;
13360 +       struct au_nfsd_si_lock nsi_lock = {
13361 +               .force_lock     = 0
13362 +       };
13363 +
13364 +       dentry = ERR_PTR(-ESTALE);
13365 +       /* it should never happen, but the file handle is unreliable */
13366 +       if (unlikely(fh_len < Fh_tail))
13367 +               goto out;
13368 +       nsi_lock.sigen = fh[Fh_sigen];
13369 +       nsi_lock.br_id = fh[Fh_br_id];
13370 +
13371 +       /* branch id may be wrapped around */
13372 +       br = NULL;
13373 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13374 +               goto out;
13375 +       nsi_lock.force_lock = 1;
13376 +
13377 +       /* is this inode still cached? */
13378 +       ino = decode_ino(fh + Fh_ino);
13379 +       /* it should never happen */
13380 +       if (unlikely(ino == AUFS_ROOT_INO))
13381 +               goto out_unlock;
13382 +
13383 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13384 +       dentry = decode_by_ino(sb, ino, dir_ino);
13385 +       if (IS_ERR(dentry))
13386 +               goto out_unlock;
13387 +       if (dentry)
13388 +               goto accept;
13389 +
13390 +       /* is the parent dir cached? */
13391 +       br = au_sbr(sb, nsi_lock.bindex);
13392 +       au_lcnt_inc(&br->br_nfiles);
13393 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13394 +       if (IS_ERR(dentry))
13395 +               goto out_unlock;
13396 +       if (dentry)
13397 +               goto accept;
13398 +
13399 +       /* lookup path */
13400 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13401 +       if (IS_ERR(dentry))
13402 +               goto out_unlock;
13403 +       if (unlikely(!dentry))
13404 +               /* todo?: make it ESTALE */
13405 +               goto out_unlock;
13406 +
13407 +accept:
13408 +       if (!au_digen_test(dentry, au_sigen(sb))
13409 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13410 +               goto out_unlock; /* success */
13411 +
13412 +       dput(dentry);
13413 +       dentry = ERR_PTR(-ESTALE);
13414 +out_unlock:
13415 +       if (br)
13416 +               au_lcnt_dec(&br->br_nfiles);
13417 +       si_read_unlock(sb);
13418 +out:
13419 +       AuTraceErrPtr(dentry);
13420 +       return dentry;
13421 +}
13422 +
13423 +#if 0 /* reserved for future use */
13424 +/* support subtreecheck option */
13425 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13426 +                                       int fh_len, int fh_type)
13427 +{
13428 +       struct dentry *parent;
13429 +       __u32 *fh = fid->raw;
13430 +       ino_t dir_ino;
13431 +
13432 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13433 +       parent = decode_by_ino(sb, dir_ino, 0);
13434 +       if (IS_ERR(parent))
13435 +               goto out;
13436 +       if (!parent)
13437 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13438 +                                       dir_ino, fh, fh_len);
13439 +
13440 +out:
13441 +       AuTraceErrPtr(parent);
13442 +       return parent;
13443 +}
13444 +#endif
13445 +
13446 +/* ---------------------------------------------------------------------- */
13447 +
13448 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13449 +                         struct inode *dir)
13450 +{
13451 +       int err;
13452 +       aufs_bindex_t bindex;
13453 +       struct super_block *sb, *h_sb;
13454 +       struct dentry *dentry, *parent, *h_parent;
13455 +       struct inode *h_dir;
13456 +       struct au_branch *br;
13457 +
13458 +       err = -ENOSPC;
13459 +       if (unlikely(*max_len <= Fh_tail)) {
13460 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13461 +               goto out;
13462 +       }
13463 +
13464 +       err = FILEID_ROOT;
13465 +       if (inode->i_ino == AUFS_ROOT_INO) {
13466 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13467 +               goto out;
13468 +       }
13469 +
13470 +       h_parent = NULL;
13471 +       sb = inode->i_sb;
13472 +       err = si_read_lock(sb, AuLock_FLUSH);
13473 +       if (unlikely(err))
13474 +               goto out;
13475 +
13476 +#ifdef CONFIG_AUFS_DEBUG
13477 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13478 +               AuWarn1("NFS-exporting requires xino\n");
13479 +#endif
13480 +       err = -EIO;
13481 +       parent = NULL;
13482 +       ii_read_lock_child(inode);
13483 +       bindex = au_ibtop(inode);
13484 +       if (!dir) {
13485 +               dentry = d_find_any_alias(inode);
13486 +               if (unlikely(!dentry))
13487 +                       goto out_unlock;
13488 +               AuDebugOn(au_test_anon(dentry));
13489 +               parent = dget_parent(dentry);
13490 +               dput(dentry);
13491 +               if (unlikely(!parent))
13492 +                       goto out_unlock;
13493 +               if (d_really_is_positive(parent))
13494 +                       dir = d_inode(parent);
13495 +       }
13496 +
13497 +       ii_read_lock_parent(dir);
13498 +       h_dir = au_h_iptr(dir, bindex);
13499 +       ii_read_unlock(dir);
13500 +       if (unlikely(!h_dir))
13501 +               goto out_parent;
13502 +       h_parent = d_find_any_alias(h_dir);
13503 +       if (unlikely(!h_parent))
13504 +               goto out_hparent;
13505 +
13506 +       err = -EPERM;
13507 +       br = au_sbr(sb, bindex);
13508 +       h_sb = au_br_sb(br);
13509 +       if (unlikely(!h_sb->s_export_op)) {
13510 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13511 +               goto out_hparent;
13512 +       }
13513 +
13514 +       fh[Fh_br_id] = br->br_id;
13515 +       fh[Fh_sigen] = au_sigen(sb);
13516 +       encode_ino(fh + Fh_ino, inode->i_ino);
13517 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13518 +       fh[Fh_igen] = inode->i_generation;
13519 +
13520 +       *max_len -= Fh_tail;
13521 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13522 +                                          max_len,
13523 +                                          /*connectable or subtreecheck*/0);
13524 +       err = fh[Fh_h_type];
13525 +       *max_len += Fh_tail;
13526 +       /* todo: macros? */
13527 +       if (err != FILEID_INVALID)
13528 +               err = 99;
13529 +       else
13530 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13531 +
13532 +out_hparent:
13533 +       dput(h_parent);
13534 +out_parent:
13535 +       dput(parent);
13536 +out_unlock:
13537 +       ii_read_unlock(inode);
13538 +       si_read_unlock(sb);
13539 +out:
13540 +       if (unlikely(err < 0))
13541 +               err = FILEID_INVALID;
13542 +       return err;
13543 +}
13544 +
13545 +/* ---------------------------------------------------------------------- */
13546 +
13547 +static int aufs_commit_metadata(struct inode *inode)
13548 +{
13549 +       int err;
13550 +       aufs_bindex_t bindex;
13551 +       struct super_block *sb;
13552 +       struct inode *h_inode;
13553 +       int (*f)(struct inode *inode);
13554 +
13555 +       sb = inode->i_sb;
13556 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13557 +       ii_write_lock_child(inode);
13558 +       bindex = au_ibtop(inode);
13559 +       AuDebugOn(bindex < 0);
13560 +       h_inode = au_h_iptr(inode, bindex);
13561 +
13562 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13563 +       if (f)
13564 +               err = f(h_inode);
13565 +       else
13566 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13567 +
13568 +       au_cpup_attr_timesizes(inode);
13569 +       ii_write_unlock(inode);
13570 +       si_read_unlock(sb);
13571 +       return err;
13572 +}
13573 +
13574 +/* ---------------------------------------------------------------------- */
13575 +
13576 +static struct export_operations aufs_export_op = {
13577 +       .fh_to_dentry           = aufs_fh_to_dentry,
13578 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13579 +       .encode_fh              = aufs_encode_fh,
13580 +       .commit_metadata        = aufs_commit_metadata
13581 +};
13582 +
13583 +void au_export_init(struct super_block *sb)
13584 +{
13585 +       struct au_sbinfo *sbinfo;
13586 +       __u32 u;
13587 +
13588 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13589 +                        && IS_MODULE(CONFIG_EXPORTFS),
13590 +                        AUFS_NAME ": unsupported configuration "
13591 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13592 +
13593 +       sb->s_export_op = &aufs_export_op;
13594 +       sbinfo = au_sbi(sb);
13595 +       sbinfo->si_xigen = NULL;
13596 +       get_random_bytes(&u, sizeof(u));
13597 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13598 +       atomic_set(&sbinfo->si_xigen_next, u);
13599 +}
13600 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13601 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13602 +++ linux/fs/aufs/fhsm.c        2021-12-03 15:38:59.933313976 +0100
13603 @@ -0,0 +1,427 @@
13604 +// SPDX-License-Identifier: GPL-2.0
13605 +/*
13606 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13607 + *
13608 + * This program, aufs is free software; you can redistribute it and/or modify
13609 + * it under the terms of the GNU General Public License as published by
13610 + * the Free Software Foundation; either version 2 of the License, or
13611 + * (at your option) any later version.
13612 + *
13613 + * This program is distributed in the hope that it will be useful,
13614 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13615 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13616 + * GNU General Public License for more details.
13617 + *
13618 + * You should have received a copy of the GNU General Public License
13619 + * along with this program; if not, write to the Free Software
13620 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13621 + */
13622 +
13623 +/*
13624 + * File-based Hierarchy Storage Management
13625 + */
13626 +
13627 +#include <linux/anon_inodes.h>
13628 +#include <linux/poll.h>
13629 +#include <linux/seq_file.h>
13630 +#include <linux/statfs.h>
13631 +#include "aufs.h"
13632 +
13633 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13634 +{
13635 +       struct au_sbinfo *sbinfo;
13636 +       struct au_fhsm *fhsm;
13637 +
13638 +       SiMustAnyLock(sb);
13639 +
13640 +       sbinfo = au_sbi(sb);
13641 +       fhsm = &sbinfo->si_fhsm;
13642 +       AuDebugOn(!fhsm);
13643 +       return fhsm->fhsm_bottom;
13644 +}
13645 +
13646 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13647 +{
13648 +       struct au_sbinfo *sbinfo;
13649 +       struct au_fhsm *fhsm;
13650 +
13651 +       SiMustWriteLock(sb);
13652 +
13653 +       sbinfo = au_sbi(sb);
13654 +       fhsm = &sbinfo->si_fhsm;
13655 +       AuDebugOn(!fhsm);
13656 +       fhsm->fhsm_bottom = bindex;
13657 +}
13658 +
13659 +/* ---------------------------------------------------------------------- */
13660 +
13661 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13662 +{
13663 +       struct au_br_fhsm *bf;
13664 +
13665 +       bf = br->br_fhsm;
13666 +       MtxMustLock(&bf->bf_lock);
13667 +
13668 +       return !bf->bf_readable
13669 +               || time_after(jiffies,
13670 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13671 +}
13672 +
13673 +/* ---------------------------------------------------------------------- */
13674 +
13675 +static void au_fhsm_notify(struct super_block *sb, int val)
13676 +{
13677 +       struct au_sbinfo *sbinfo;
13678 +       struct au_fhsm *fhsm;
13679 +
13680 +       SiMustAnyLock(sb);
13681 +
13682 +       sbinfo = au_sbi(sb);
13683 +       fhsm = &sbinfo->si_fhsm;
13684 +       if (au_fhsm_pid(fhsm)
13685 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13686 +               atomic_set(&fhsm->fhsm_readable, val);
13687 +               if (val)
13688 +                       wake_up(&fhsm->fhsm_wqh);
13689 +       }
13690 +}
13691 +
13692 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13693 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13694 +{
13695 +       int err;
13696 +       struct au_branch *br;
13697 +       struct au_br_fhsm *bf;
13698 +
13699 +       br = au_sbr(sb, bindex);
13700 +       AuDebugOn(au_br_rdonly(br));
13701 +       bf = br->br_fhsm;
13702 +       AuDebugOn(!bf);
13703 +
13704 +       if (do_lock)
13705 +               mutex_lock(&bf->bf_lock);
13706 +       else
13707 +               MtxMustLock(&bf->bf_lock);
13708 +
13709 +       /* sb->s_root for NFS is unreliable */
13710 +       err = au_br_stfs(br, &bf->bf_stfs);
13711 +       if (unlikely(err)) {
13712 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13713 +               goto out;
13714 +       }
13715 +
13716 +       bf->bf_jiffy = jiffies;
13717 +       bf->bf_readable = 1;
13718 +       if (do_notify)
13719 +               au_fhsm_notify(sb, /*val*/1);
13720 +       if (rstfs)
13721 +               *rstfs = bf->bf_stfs;
13722 +
13723 +out:
13724 +       if (do_lock)
13725 +               mutex_unlock(&bf->bf_lock);
13726 +       au_fhsm_notify(sb, /*val*/1);
13727 +
13728 +       return err;
13729 +}
13730 +
13731 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13732 +{
13733 +       int err;
13734 +       struct au_sbinfo *sbinfo;
13735 +       struct au_fhsm *fhsm;
13736 +       struct au_branch *br;
13737 +       struct au_br_fhsm *bf;
13738 +
13739 +       AuDbg("b%d, force %d\n", bindex, force);
13740 +       SiMustAnyLock(sb);
13741 +
13742 +       sbinfo = au_sbi(sb);
13743 +       fhsm = &sbinfo->si_fhsm;
13744 +       if (!au_ftest_si(sbinfo, FHSM)
13745 +           || fhsm->fhsm_bottom == bindex)
13746 +               return;
13747 +
13748 +       br = au_sbr(sb, bindex);
13749 +       bf = br->br_fhsm;
13750 +       AuDebugOn(!bf);
13751 +       mutex_lock(&bf->bf_lock);
13752 +       if (force
13753 +           || au_fhsm_pid(fhsm)
13754 +           || au_fhsm_test_jiffy(sbinfo, br))
13755 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13756 +                                 /*do_notify*/1);
13757 +       mutex_unlock(&bf->bf_lock);
13758 +}
13759 +
13760 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13761 +{
13762 +       aufs_bindex_t bindex, bbot;
13763 +       struct au_branch *br;
13764 +
13765 +       /* exclude the bottom */
13766 +       bbot = au_fhsm_bottom(sb);
13767 +       for (bindex = 0; bindex < bbot; bindex++) {
13768 +               br = au_sbr(sb, bindex);
13769 +               if (au_br_fhsm(br->br_perm))
13770 +                       au_fhsm_wrote(sb, bindex, force);
13771 +       }
13772 +}
13773 +
13774 +/* ---------------------------------------------------------------------- */
13775 +
13776 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13777 +{
13778 +       __poll_t mask;
13779 +       struct au_sbinfo *sbinfo;
13780 +       struct au_fhsm *fhsm;
13781 +
13782 +       mask = 0;
13783 +       sbinfo = file->private_data;
13784 +       fhsm = &sbinfo->si_fhsm;
13785 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13786 +       if (atomic_read(&fhsm->fhsm_readable))
13787 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13788 +
13789 +       if (!mask)
13790 +               AuDbg("mask 0x%x\n", mask);
13791 +       return mask;
13792 +}
13793 +
13794 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13795 +                             struct aufs_stfs *stfs, __s16 brid)
13796 +{
13797 +       int err;
13798 +
13799 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13800 +       if (!err)
13801 +               err = __put_user(brid, &stbr->brid);
13802 +       if (unlikely(err))
13803 +               err = -EFAULT;
13804 +
13805 +       return err;
13806 +}
13807 +
13808 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13809 +                              struct aufs_stbr __user *stbr, size_t count)
13810 +{
13811 +       ssize_t err;
13812 +       int nstbr;
13813 +       aufs_bindex_t bindex, bbot;
13814 +       struct au_branch *br;
13815 +       struct au_br_fhsm *bf;
13816 +
13817 +       /* except the bottom branch */
13818 +       err = 0;
13819 +       nstbr = 0;
13820 +       bbot = au_fhsm_bottom(sb);
13821 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13822 +               br = au_sbr(sb, bindex);
13823 +               if (!au_br_fhsm(br->br_perm))
13824 +                       continue;
13825 +
13826 +               bf = br->br_fhsm;
13827 +               mutex_lock(&bf->bf_lock);
13828 +               if (bf->bf_readable) {
13829 +                       err = -EFAULT;
13830 +                       if (count >= sizeof(*stbr))
13831 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13832 +                                                         br->br_id);
13833 +                       if (!err) {
13834 +                               bf->bf_readable = 0;
13835 +                               count -= sizeof(*stbr);
13836 +                               nstbr++;
13837 +                       }
13838 +               }
13839 +               mutex_unlock(&bf->bf_lock);
13840 +       }
13841 +       if (!err)
13842 +               err = sizeof(*stbr) * nstbr;
13843 +
13844 +       return err;
13845 +}
13846 +
13847 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13848 +                          loff_t *pos)
13849 +{
13850 +       ssize_t err;
13851 +       int readable;
13852 +       aufs_bindex_t nfhsm, bindex, bbot;
13853 +       struct au_sbinfo *sbinfo;
13854 +       struct au_fhsm *fhsm;
13855 +       struct au_branch *br;
13856 +       struct super_block *sb;
13857 +
13858 +       err = 0;
13859 +       sbinfo = file->private_data;
13860 +       fhsm = &sbinfo->si_fhsm;
13861 +need_data:
13862 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13863 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13864 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13865 +                       err = -EAGAIN;
13866 +               else
13867 +                       err = wait_event_interruptible_locked_irq
13868 +                               (fhsm->fhsm_wqh,
13869 +                                atomic_read(&fhsm->fhsm_readable));
13870 +       }
13871 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13872 +       if (unlikely(err))
13873 +               goto out;
13874 +
13875 +       /* sb may already be dead */
13876 +       au_rw_read_lock(&sbinfo->si_rwsem);
13877 +       readable = atomic_read(&fhsm->fhsm_readable);
13878 +       if (readable > 0) {
13879 +               sb = sbinfo->si_sb;
13880 +               AuDebugOn(!sb);
13881 +               /* exclude the bottom branch */
13882 +               nfhsm = 0;
13883 +               bbot = au_fhsm_bottom(sb);
13884 +               for (bindex = 0; bindex < bbot; bindex++) {
13885 +                       br = au_sbr(sb, bindex);
13886 +                       if (au_br_fhsm(br->br_perm))
13887 +                               nfhsm++;
13888 +               }
13889 +               err = -EMSGSIZE;
13890 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13891 +                       atomic_set(&fhsm->fhsm_readable, 0);
13892 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13893 +                                            count);
13894 +               }
13895 +       }
13896 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13897 +       if (!readable)
13898 +               goto need_data;
13899 +
13900 +out:
13901 +       return err;
13902 +}
13903 +
13904 +static int au_fhsm_release(struct inode *inode, struct file *file)
13905 +{
13906 +       struct au_sbinfo *sbinfo;
13907 +       struct au_fhsm *fhsm;
13908 +
13909 +       /* sb may already be dead */
13910 +       sbinfo = file->private_data;
13911 +       fhsm = &sbinfo->si_fhsm;
13912 +       spin_lock(&fhsm->fhsm_spin);
13913 +       fhsm->fhsm_pid = 0;
13914 +       spin_unlock(&fhsm->fhsm_spin);
13915 +       kobject_put(&sbinfo->si_kobj);
13916 +
13917 +       return 0;
13918 +}
13919 +
13920 +static const struct file_operations au_fhsm_fops = {
13921 +       .owner          = THIS_MODULE,
13922 +       .llseek         = noop_llseek,
13923 +       .read           = au_fhsm_read,
13924 +       .poll           = au_fhsm_poll,
13925 +       .release        = au_fhsm_release
13926 +};
13927 +
13928 +int au_fhsm_fd(struct super_block *sb, int oflags)
13929 +{
13930 +       int err, fd;
13931 +       struct au_sbinfo *sbinfo;
13932 +       struct au_fhsm *fhsm;
13933 +
13934 +       err = -EPERM;
13935 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13936 +               goto out;
13937 +
13938 +       err = -EINVAL;
13939 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13940 +               goto out;
13941 +
13942 +       err = 0;
13943 +       sbinfo = au_sbi(sb);
13944 +       fhsm = &sbinfo->si_fhsm;
13945 +       spin_lock(&fhsm->fhsm_spin);
13946 +       if (!fhsm->fhsm_pid)
13947 +               fhsm->fhsm_pid = current->pid;
13948 +       else
13949 +               err = -EBUSY;
13950 +       spin_unlock(&fhsm->fhsm_spin);
13951 +       if (unlikely(err))
13952 +               goto out;
13953 +
13954 +       oflags |= O_RDONLY;
13955 +       /* oflags |= FMODE_NONOTIFY; */
13956 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13957 +       err = fd;
13958 +       if (unlikely(fd < 0))
13959 +               goto out_pid;
13960 +
13961 +       /* succeed regardless 'fhsm' status */
13962 +       kobject_get(&sbinfo->si_kobj);
13963 +       si_noflush_read_lock(sb);
13964 +       if (au_ftest_si(sbinfo, FHSM))
13965 +               au_fhsm_wrote_all(sb, /*force*/0);
13966 +       si_read_unlock(sb);
13967 +       goto out; /* success */
13968 +
13969 +out_pid:
13970 +       spin_lock(&fhsm->fhsm_spin);
13971 +       fhsm->fhsm_pid = 0;
13972 +       spin_unlock(&fhsm->fhsm_spin);
13973 +out:
13974 +       AuTraceErr(err);
13975 +       return err;
13976 +}
13977 +
13978 +/* ---------------------------------------------------------------------- */
13979 +
13980 +int au_fhsm_br_alloc(struct au_branch *br)
13981 +{
13982 +       int err;
13983 +
13984 +       err = 0;
13985 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13986 +       if (br->br_fhsm)
13987 +               au_br_fhsm_init(br->br_fhsm);
13988 +       else
13989 +               err = -ENOMEM;
13990 +
13991 +       return err;
13992 +}
13993 +
13994 +/* ---------------------------------------------------------------------- */
13995 +
13996 +void au_fhsm_fin(struct super_block *sb)
13997 +{
13998 +       au_fhsm_notify(sb, /*val*/-1);
13999 +}
14000 +
14001 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14002 +{
14003 +       struct au_fhsm *fhsm;
14004 +
14005 +       fhsm = &sbinfo->si_fhsm;
14006 +       spin_lock_init(&fhsm->fhsm_spin);
14007 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14008 +       atomic_set(&fhsm->fhsm_readable, 0);
14009 +       fhsm->fhsm_expire
14010 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14011 +       fhsm->fhsm_bottom = -1;
14012 +}
14013 +
14014 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14015 +{
14016 +       sbinfo->si_fhsm.fhsm_expire
14017 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14018 +}
14019 +
14020 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14021 +{
14022 +       unsigned int u;
14023 +
14024 +       if (!au_ftest_si(sbinfo, FHSM))
14025 +               return;
14026 +
14027 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14028 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14029 +               seq_printf(seq, ",fhsm_sec=%u", u);
14030 +}
14031 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14032 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14033 +++ linux/fs/aufs/file.c        2021-12-03 15:38:59.936647310 +0100
14034 @@ -0,0 +1,863 @@
14035 +// SPDX-License-Identifier: GPL-2.0
14036 +/*
14037 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14038 + *
14039 + * This program, aufs is free software; you can redistribute it and/or modify
14040 + * it under the terms of the GNU General Public License as published by
14041 + * the Free Software Foundation; either version 2 of the License, or
14042 + * (at your option) any later version.
14043 + *
14044 + * This program is distributed in the hope that it will be useful,
14045 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14046 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14047 + * GNU General Public License for more details.
14048 + *
14049 + * You should have received a copy of the GNU General Public License
14050 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14051 + */
14052 +
14053 +/*
14054 + * handling file/dir, and address_space operation
14055 + */
14056 +
14057 +#ifdef CONFIG_AUFS_DEBUG
14058 +#include <linux/migrate.h>
14059 +#endif
14060 +#include <linux/pagemap.h>
14061 +#include "aufs.h"
14062 +
14063 +/* drop flags for writing */
14064 +unsigned int au_file_roflags(unsigned int flags)
14065 +{
14066 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14067 +       flags |= O_RDONLY | O_NOATIME;
14068 +       return flags;
14069 +}
14070 +
14071 +/* common functions to regular file and dir */
14072 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14073 +                      struct file *file, int force_wr)
14074 +{
14075 +       struct file *h_file;
14076 +       struct dentry *h_dentry;
14077 +       struct inode *h_inode;
14078 +       struct super_block *sb;
14079 +       struct au_branch *br;
14080 +       struct path h_path;
14081 +       int err;
14082 +
14083 +       /* a race condition can happen between open and unlink/rmdir */
14084 +       h_file = ERR_PTR(-ENOENT);
14085 +       h_dentry = au_h_dptr(dentry, bindex);
14086 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14087 +               goto out;
14088 +       h_inode = d_inode(h_dentry);
14089 +       spin_lock(&h_dentry->d_lock);
14090 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14091 +               /* || !d_inode(dentry)->i_nlink */
14092 +               ;
14093 +       spin_unlock(&h_dentry->d_lock);
14094 +       if (unlikely(err))
14095 +               goto out;
14096 +
14097 +       sb = dentry->d_sb;
14098 +       br = au_sbr(sb, bindex);
14099 +       err = au_br_test_oflag(flags, br);
14100 +       h_file = ERR_PTR(err);
14101 +       if (unlikely(err))
14102 +               goto out;
14103 +
14104 +       /* drop flags for writing */
14105 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14106 +               if (force_wr && !(flags & O_WRONLY))
14107 +                       force_wr = 0;
14108 +               flags = au_file_roflags(flags);
14109 +               if (force_wr) {
14110 +                       h_file = ERR_PTR(-EROFS);
14111 +                       flags = au_file_roflags(flags);
14112 +                       if (unlikely(vfsub_native_ro(h_inode)
14113 +                                    || IS_APPEND(h_inode)))
14114 +                               goto out;
14115 +                       flags &= ~O_ACCMODE;
14116 +                       flags |= O_WRONLY;
14117 +               }
14118 +       }
14119 +       flags &= ~O_CREAT;
14120 +       au_lcnt_inc(&br->br_nfiles);
14121 +       h_path.dentry = h_dentry;
14122 +       h_path.mnt = au_br_mnt(br);
14123 +       h_file = vfsub_dentry_open(&h_path, flags);
14124 +       if (IS_ERR(h_file))
14125 +               goto out_br;
14126 +
14127 +       if (flags & __FMODE_EXEC) {
14128 +               err = deny_write_access(h_file);
14129 +               if (unlikely(err)) {
14130 +                       fput(h_file);
14131 +                       h_file = ERR_PTR(err);
14132 +                       goto out_br;
14133 +               }
14134 +       }
14135 +       fsnotify_open(h_file);
14136 +       goto out; /* success */
14137 +
14138 +out_br:
14139 +       au_lcnt_dec(&br->br_nfiles);
14140 +out:
14141 +       return h_file;
14142 +}
14143 +
14144 +static int au_cmoo(struct dentry *dentry)
14145 +{
14146 +       int err, cmoo, matched;
14147 +       unsigned int udba;
14148 +       struct path h_path;
14149 +       struct au_pin pin;
14150 +       struct au_cp_generic cpg = {
14151 +               .dentry = dentry,
14152 +               .bdst   = -1,
14153 +               .bsrc   = -1,
14154 +               .len    = -1,
14155 +               .pin    = &pin,
14156 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14157 +       };
14158 +       struct inode *delegated;
14159 +       struct super_block *sb;
14160 +       struct au_sbinfo *sbinfo;
14161 +       struct au_fhsm *fhsm;
14162 +       pid_t pid;
14163 +       struct au_branch *br;
14164 +       struct dentry *parent;
14165 +       struct au_hinode *hdir;
14166 +
14167 +       DiMustWriteLock(dentry);
14168 +       IiMustWriteLock(d_inode(dentry));
14169 +
14170 +       err = 0;
14171 +       if (IS_ROOT(dentry))
14172 +               goto out;
14173 +       cpg.bsrc = au_dbtop(dentry);
14174 +       if (!cpg.bsrc)
14175 +               goto out;
14176 +
14177 +       sb = dentry->d_sb;
14178 +       sbinfo = au_sbi(sb);
14179 +       fhsm = &sbinfo->si_fhsm;
14180 +       pid = au_fhsm_pid(fhsm);
14181 +       rcu_read_lock();
14182 +       matched = (pid
14183 +                  && (current->pid == pid
14184 +                      || rcu_dereference(current->real_parent)->pid == pid));
14185 +       rcu_read_unlock();
14186 +       if (matched)
14187 +               goto out;
14188 +
14189 +       br = au_sbr(sb, cpg.bsrc);
14190 +       cmoo = au_br_cmoo(br->br_perm);
14191 +       if (!cmoo)
14192 +               goto out;
14193 +       if (!d_is_reg(dentry))
14194 +               cmoo &= AuBrAttr_COO_ALL;
14195 +       if (!cmoo)
14196 +               goto out;
14197 +
14198 +       parent = dget_parent(dentry);
14199 +       di_write_lock_parent(parent);
14200 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14201 +       cpg.bdst = err;
14202 +       if (unlikely(err < 0)) {
14203 +               err = 0;        /* there is no upper writable branch */
14204 +               goto out_dgrade;
14205 +       }
14206 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14207 +
14208 +       /* do not respect the coo attrib for the target branch */
14209 +       err = au_cpup_dirs(dentry, cpg.bdst);
14210 +       if (unlikely(err))
14211 +               goto out_dgrade;
14212 +
14213 +       di_downgrade_lock(parent, AuLock_IR);
14214 +       udba = au_opt_udba(sb);
14215 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14216 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14217 +       if (unlikely(err))
14218 +               goto out_parent;
14219 +
14220 +       err = au_sio_cpup_simple(&cpg);
14221 +       au_unpin(&pin);
14222 +       if (unlikely(err))
14223 +               goto out_parent;
14224 +       if (!(cmoo & AuBrWAttr_MOO))
14225 +               goto out_parent; /* success */
14226 +
14227 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14228 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14229 +       if (unlikely(err))
14230 +               goto out_parent;
14231 +
14232 +       h_path.mnt = au_br_mnt(br);
14233 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14234 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14235 +       delegated = NULL;
14236 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14237 +       au_unpin(&pin);
14238 +       /* todo: keep h_dentry or not? */
14239 +       if (unlikely(err == -EWOULDBLOCK)) {
14240 +               pr_warn("cannot retry for NFSv4 delegation"
14241 +                       " for an internal unlink\n");
14242 +               iput(delegated);
14243 +       }
14244 +       if (unlikely(err)) {
14245 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14246 +                      dentry, err);
14247 +               err = 0;
14248 +       }
14249 +       goto out_parent; /* success */
14250 +
14251 +out_dgrade:
14252 +       di_downgrade_lock(parent, AuLock_IR);
14253 +out_parent:
14254 +       di_read_unlock(parent, AuLock_IR);
14255 +       dput(parent);
14256 +out:
14257 +       AuTraceErr(err);
14258 +       return err;
14259 +}
14260 +
14261 +int au_do_open(struct file *file, struct au_do_open_args *args)
14262 +{
14263 +       int err, aopen = args->aopen;
14264 +       struct dentry *dentry;
14265 +       struct au_finfo *finfo;
14266 +
14267 +       if (!aopen)
14268 +               err = au_finfo_init(file, args->fidir);
14269 +       else {
14270 +               lockdep_off();
14271 +               err = au_finfo_init(file, args->fidir);
14272 +               lockdep_on();
14273 +       }
14274 +       if (unlikely(err))
14275 +               goto out;
14276 +
14277 +       dentry = file->f_path.dentry;
14278 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14279 +       di_write_lock_child(dentry);
14280 +       err = au_cmoo(dentry);
14281 +       di_downgrade_lock(dentry, AuLock_IR);
14282 +       if (!err) {
14283 +               if (!aopen)
14284 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14285 +               else {
14286 +                       lockdep_off();
14287 +                       err = args->open(file, vfsub_file_flags(file),
14288 +                                        args->h_file);
14289 +                       lockdep_on();
14290 +               }
14291 +       }
14292 +       di_read_unlock(dentry, AuLock_IR);
14293 +
14294 +       finfo = au_fi(file);
14295 +       if (!err) {
14296 +               finfo->fi_file = file;
14297 +               au_hbl_add(&finfo->fi_hlist,
14298 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14299 +       }
14300 +       if (!aopen)
14301 +               fi_write_unlock(file);
14302 +       else {
14303 +               lockdep_off();
14304 +               fi_write_unlock(file);
14305 +               lockdep_on();
14306 +       }
14307 +       if (unlikely(err)) {
14308 +               finfo->fi_hdir = NULL;
14309 +               au_finfo_fin(file);
14310 +       }
14311 +
14312 +out:
14313 +       AuTraceErr(err);
14314 +       return err;
14315 +}
14316 +
14317 +int au_reopen_nondir(struct file *file)
14318 +{
14319 +       int err;
14320 +       aufs_bindex_t btop;
14321 +       struct dentry *dentry;
14322 +       struct au_branch *br;
14323 +       struct file *h_file, *h_file_tmp;
14324 +
14325 +       dentry = file->f_path.dentry;
14326 +       btop = au_dbtop(dentry);
14327 +       br = au_sbr(dentry->d_sb, btop);
14328 +       h_file_tmp = NULL;
14329 +       if (au_fbtop(file) == btop) {
14330 +               h_file = au_hf_top(file);
14331 +               if (file->f_mode == h_file->f_mode)
14332 +                       return 0; /* success */
14333 +               h_file_tmp = h_file;
14334 +               get_file(h_file_tmp);
14335 +               au_lcnt_inc(&br->br_nfiles);
14336 +               au_set_h_fptr(file, btop, NULL);
14337 +       }
14338 +       AuDebugOn(au_fi(file)->fi_hdir);
14339 +       /*
14340 +        * it can happen
14341 +        * file exists on both of rw and ro
14342 +        * open --> dbtop and fbtop are both 0
14343 +        * prepend a branch as rw, "rw" become ro
14344 +        * remove rw/file
14345 +        * delete the top branch, "rw" becomes rw again
14346 +        *      --> dbtop is 1, fbtop is still 0
14347 +        * write --> fbtop is 0 but dbtop is 1
14348 +        */
14349 +       /* AuDebugOn(au_fbtop(file) < btop); */
14350 +
14351 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14352 +                          file, /*force_wr*/0);
14353 +       err = PTR_ERR(h_file);
14354 +       if (IS_ERR(h_file)) {
14355 +               if (h_file_tmp) {
14356 +                       /* revert */
14357 +                       au_set_h_fptr(file, btop, h_file_tmp);
14358 +                       h_file_tmp = NULL;
14359 +               }
14360 +               goto out; /* todo: close all? */
14361 +       }
14362 +
14363 +       err = 0;
14364 +       au_set_fbtop(file, btop);
14365 +       au_set_h_fptr(file, btop, h_file);
14366 +       au_update_figen(file);
14367 +       /* todo: necessary? */
14368 +       /* file->f_ra = h_file->f_ra; */
14369 +
14370 +out:
14371 +       if (h_file_tmp) {
14372 +               fput(h_file_tmp);
14373 +               au_lcnt_dec(&br->br_nfiles);
14374 +       }
14375 +       return err;
14376 +}
14377 +
14378 +/* ---------------------------------------------------------------------- */
14379 +
14380 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14381 +                       struct dentry *hi_wh)
14382 +{
14383 +       int err;
14384 +       aufs_bindex_t btop;
14385 +       struct au_dinfo *dinfo;
14386 +       struct dentry *h_dentry;
14387 +       struct au_hdentry *hdp;
14388 +
14389 +       dinfo = au_di(file->f_path.dentry);
14390 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14391 +
14392 +       btop = dinfo->di_btop;
14393 +       dinfo->di_btop = btgt;
14394 +       hdp = au_hdentry(dinfo, btgt);
14395 +       h_dentry = hdp->hd_dentry;
14396 +       hdp->hd_dentry = hi_wh;
14397 +       err = au_reopen_nondir(file);
14398 +       hdp->hd_dentry = h_dentry;
14399 +       dinfo->di_btop = btop;
14400 +
14401 +       return err;
14402 +}
14403 +
14404 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14405 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14406 +{
14407 +       int err;
14408 +       struct inode *inode, *h_inode;
14409 +       struct dentry *h_dentry, *hi_wh;
14410 +       struct au_cp_generic cpg = {
14411 +               .dentry = file->f_path.dentry,
14412 +               .bdst   = bcpup,
14413 +               .bsrc   = -1,
14414 +               .len    = len,
14415 +               .pin    = pin
14416 +       };
14417 +
14418 +       au_update_dbtop(cpg.dentry);
14419 +       inode = d_inode(cpg.dentry);
14420 +       h_inode = NULL;
14421 +       if (au_dbtop(cpg.dentry) <= bcpup
14422 +           && au_dbbot(cpg.dentry) >= bcpup) {
14423 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14424 +               if (h_dentry && d_is_positive(h_dentry))
14425 +                       h_inode = d_inode(h_dentry);
14426 +       }
14427 +       hi_wh = au_hi_wh(inode, bcpup);
14428 +       if (!hi_wh && !h_inode)
14429 +               err = au_sio_cpup_wh(&cpg, file);
14430 +       else
14431 +               /* already copied-up after unlink */
14432 +               err = au_reopen_wh(file, bcpup, hi_wh);
14433 +
14434 +       if (!err
14435 +           && (inode->i_nlink > 1
14436 +               || (inode->i_state & I_LINKABLE))
14437 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14438 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14439 +
14440 +       return err;
14441 +}
14442 +
14443 +/*
14444 + * prepare the @file for writing.
14445 + */
14446 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14447 +{
14448 +       int err;
14449 +       aufs_bindex_t dbtop;
14450 +       struct dentry *parent;
14451 +       struct inode *inode;
14452 +       struct super_block *sb;
14453 +       struct file *h_file;
14454 +       struct au_cp_generic cpg = {
14455 +               .dentry = file->f_path.dentry,
14456 +               .bdst   = -1,
14457 +               .bsrc   = -1,
14458 +               .len    = len,
14459 +               .pin    = pin,
14460 +               .flags  = AuCpup_DTIME
14461 +       };
14462 +
14463 +       sb = cpg.dentry->d_sb;
14464 +       inode = d_inode(cpg.dentry);
14465 +       cpg.bsrc = au_fbtop(file);
14466 +       err = au_test_ro(sb, cpg.bsrc, inode);
14467 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14468 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14469 +                            /*flags*/0);
14470 +               goto out;
14471 +       }
14472 +
14473 +       /* need to cpup or reopen */
14474 +       parent = dget_parent(cpg.dentry);
14475 +       di_write_lock_parent(parent);
14476 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14477 +       cpg.bdst = err;
14478 +       if (unlikely(err < 0))
14479 +               goto out_dgrade;
14480 +       err = 0;
14481 +
14482 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14483 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14484 +               if (unlikely(err))
14485 +                       goto out_dgrade;
14486 +       }
14487 +
14488 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14489 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14490 +       if (unlikely(err))
14491 +               goto out_dgrade;
14492 +
14493 +       dbtop = au_dbtop(cpg.dentry);
14494 +       if (dbtop <= cpg.bdst)
14495 +               cpg.bsrc = cpg.bdst;
14496 +
14497 +       if (dbtop <= cpg.bdst           /* just reopen */
14498 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14499 +               ) {
14500 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14501 +               if (IS_ERR(h_file))
14502 +                       err = PTR_ERR(h_file);
14503 +               else {
14504 +                       di_downgrade_lock(parent, AuLock_IR);
14505 +                       if (dbtop > cpg.bdst)
14506 +                               err = au_sio_cpup_simple(&cpg);
14507 +                       if (!err)
14508 +                               err = au_reopen_nondir(file);
14509 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14510 +               }
14511 +       } else {                        /* copyup as wh and reopen */
14512 +               /*
14513 +                * since writable hfsplus branch is not supported,
14514 +                * h_open_pre/post() are unnecessary.
14515 +                */
14516 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14517 +               di_downgrade_lock(parent, AuLock_IR);
14518 +       }
14519 +
14520 +       if (!err) {
14521 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14522 +               goto out_dput; /* success */
14523 +       }
14524 +       au_unpin(pin);
14525 +       goto out_unlock;
14526 +
14527 +out_dgrade:
14528 +       di_downgrade_lock(parent, AuLock_IR);
14529 +out_unlock:
14530 +       di_read_unlock(parent, AuLock_IR);
14531 +out_dput:
14532 +       dput(parent);
14533 +out:
14534 +       return err;
14535 +}
14536 +
14537 +/* ---------------------------------------------------------------------- */
14538 +
14539 +int au_do_flush(struct file *file, fl_owner_t id,
14540 +               int (*flush)(struct file *file, fl_owner_t id))
14541 +{
14542 +       int err;
14543 +       struct super_block *sb;
14544 +       struct inode *inode;
14545 +
14546 +       inode = file_inode(file);
14547 +       sb = inode->i_sb;
14548 +       si_noflush_read_lock(sb);
14549 +       fi_read_lock(file);
14550 +       ii_read_lock_child(inode);
14551 +
14552 +       err = flush(file, id);
14553 +       au_cpup_attr_timesizes(inode);
14554 +
14555 +       ii_read_unlock(inode);
14556 +       fi_read_unlock(file);
14557 +       si_read_unlock(sb);
14558 +       return err;
14559 +}
14560 +
14561 +/* ---------------------------------------------------------------------- */
14562 +
14563 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14564 +{
14565 +       int err;
14566 +       struct au_pin pin;
14567 +       struct au_finfo *finfo;
14568 +       struct dentry *parent, *hi_wh;
14569 +       struct inode *inode;
14570 +       struct super_block *sb;
14571 +       struct au_cp_generic cpg = {
14572 +               .dentry = file->f_path.dentry,
14573 +               .bdst   = -1,
14574 +               .bsrc   = -1,
14575 +               .len    = -1,
14576 +               .pin    = &pin,
14577 +               .flags  = AuCpup_DTIME
14578 +       };
14579 +
14580 +       FiMustWriteLock(file);
14581 +
14582 +       err = 0;
14583 +       finfo = au_fi(file);
14584 +       sb = cpg.dentry->d_sb;
14585 +       inode = d_inode(cpg.dentry);
14586 +       cpg.bdst = au_ibtop(inode);
14587 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14588 +               goto out;
14589 +
14590 +       parent = dget_parent(cpg.dentry);
14591 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14592 +               di_read_lock_parent(parent, !AuLock_IR);
14593 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14594 +               cpg.bdst = err;
14595 +               di_read_unlock(parent, !AuLock_IR);
14596 +               if (unlikely(err < 0))
14597 +                       goto out_parent;
14598 +               err = 0;
14599 +       }
14600 +
14601 +       di_read_lock_parent(parent, AuLock_IR);
14602 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14603 +       if (!S_ISDIR(inode->i_mode)
14604 +           && au_opt_test(au_mntflags(sb), PLINK)
14605 +           && au_plink_test(inode)
14606 +           && !d_unhashed(cpg.dentry)
14607 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14608 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14609 +               if (unlikely(err))
14610 +                       goto out_unlock;
14611 +
14612 +               /* always superio. */
14613 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14614 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14615 +               if (!err) {
14616 +                       err = au_sio_cpup_simple(&cpg);
14617 +                       au_unpin(&pin);
14618 +               }
14619 +       } else if (hi_wh) {
14620 +               /* already copied-up after unlink */
14621 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14622 +               *need_reopen = 0;
14623 +       }
14624 +
14625 +out_unlock:
14626 +       di_read_unlock(parent, AuLock_IR);
14627 +out_parent:
14628 +       dput(parent);
14629 +out:
14630 +       return err;
14631 +}
14632 +
14633 +static void au_do_refresh_dir(struct file *file)
14634 +{
14635 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14636 +       struct au_hfile *p, tmp, *q;
14637 +       struct au_finfo *finfo;
14638 +       struct super_block *sb;
14639 +       struct au_fidir *fidir;
14640 +
14641 +       FiMustWriteLock(file);
14642 +
14643 +       sb = file->f_path.dentry->d_sb;
14644 +       finfo = au_fi(file);
14645 +       fidir = finfo->fi_hdir;
14646 +       AuDebugOn(!fidir);
14647 +       p = fidir->fd_hfile + finfo->fi_btop;
14648 +       brid = p->hf_br->br_id;
14649 +       bbot = fidir->fd_bbot;
14650 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14651 +               if (!p->hf_file)
14652 +                       continue;
14653 +
14654 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14655 +               if (new_bindex == bindex)
14656 +                       continue;
14657 +               if (new_bindex < 0) {
14658 +                       au_set_h_fptr(file, bindex, NULL);
14659 +                       continue;
14660 +               }
14661 +
14662 +               /* swap two lower inode, and loop again */
14663 +               q = fidir->fd_hfile + new_bindex;
14664 +               tmp = *q;
14665 +               *q = *p;
14666 +               *p = tmp;
14667 +               if (tmp.hf_file) {
14668 +                       bindex--;
14669 +                       p--;
14670 +               }
14671 +       }
14672 +
14673 +       p = fidir->fd_hfile;
14674 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14675 +               bbot = au_sbbot(sb);
14676 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14677 +                    finfo->fi_btop++, p++)
14678 +                       if (p->hf_file) {
14679 +                               if (file_inode(p->hf_file))
14680 +                                       break;
14681 +                               au_hfput(p, /*execed*/0);
14682 +                       }
14683 +       } else {
14684 +               bbot = au_br_index(sb, brid);
14685 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14686 +                    finfo->fi_btop++, p++)
14687 +                       if (p->hf_file)
14688 +                               au_hfput(p, /*execed*/0);
14689 +               bbot = au_sbbot(sb);
14690 +       }
14691 +
14692 +       p = fidir->fd_hfile + bbot;
14693 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14694 +            fidir->fd_bbot--, p--)
14695 +               if (p->hf_file) {
14696 +                       if (file_inode(p->hf_file))
14697 +                               break;
14698 +                       au_hfput(p, /*execed*/0);
14699 +               }
14700 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14701 +}
14702 +
14703 +/*
14704 + * after branch manipulating, refresh the file.
14705 + */
14706 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14707 +{
14708 +       int err, need_reopen, nbr;
14709 +       aufs_bindex_t bbot, bindex;
14710 +       struct dentry *dentry;
14711 +       struct super_block *sb;
14712 +       struct au_finfo *finfo;
14713 +       struct au_hfile *hfile;
14714 +
14715 +       dentry = file->f_path.dentry;
14716 +       sb = dentry->d_sb;
14717 +       nbr = au_sbbot(sb) + 1;
14718 +       finfo = au_fi(file);
14719 +       if (!finfo->fi_hdir) {
14720 +               hfile = &finfo->fi_htop;
14721 +               AuDebugOn(!hfile->hf_file);
14722 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14723 +               AuDebugOn(bindex < 0);
14724 +               if (bindex != finfo->fi_btop)
14725 +                       au_set_fbtop(file, bindex);
14726 +       } else {
14727 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14728 +               if (unlikely(err))
14729 +                       goto out;
14730 +               au_do_refresh_dir(file);
14731 +       }
14732 +
14733 +       err = 0;
14734 +       need_reopen = 1;
14735 +       if (!au_test_mmapped(file))
14736 +               err = au_file_refresh_by_inode(file, &need_reopen);
14737 +       if (finfo->fi_hdir)
14738 +               /* harmless if err */
14739 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14740 +       if (!err && need_reopen && !d_unlinked(dentry))
14741 +               err = reopen(file);
14742 +       if (!err) {
14743 +               au_update_figen(file);
14744 +               goto out; /* success */
14745 +       }
14746 +
14747 +       /* error, close all lower files */
14748 +       if (finfo->fi_hdir) {
14749 +               bbot = au_fbbot_dir(file);
14750 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14751 +                       au_set_h_fptr(file, bindex, NULL);
14752 +       }
14753 +
14754 +out:
14755 +       return err;
14756 +}
14757 +
14758 +/* common function to regular file and dir */
14759 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14760 +                         int wlock, unsigned int fi_lsc)
14761 +{
14762 +       int err;
14763 +       unsigned int sigen, figen;
14764 +       aufs_bindex_t btop;
14765 +       unsigned char pseudo_link;
14766 +       struct dentry *dentry;
14767 +       struct inode *inode;
14768 +
14769 +       err = 0;
14770 +       dentry = file->f_path.dentry;
14771 +       inode = d_inode(dentry);
14772 +       sigen = au_sigen(dentry->d_sb);
14773 +       fi_write_lock_nested(file, fi_lsc);
14774 +       figen = au_figen(file);
14775 +       if (!fi_lsc)
14776 +               di_write_lock_child(dentry);
14777 +       else
14778 +               di_write_lock_child2(dentry);
14779 +       btop = au_dbtop(dentry);
14780 +       pseudo_link = (btop != au_ibtop(inode));
14781 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14782 +               if (!wlock) {
14783 +                       di_downgrade_lock(dentry, AuLock_IR);
14784 +                       fi_downgrade_lock(file);
14785 +               }
14786 +               goto out; /* success */
14787 +       }
14788 +
14789 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14790 +       if (au_digen_test(dentry, sigen)) {
14791 +               err = au_reval_dpath(dentry, sigen);
14792 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14793 +       }
14794 +
14795 +       if (!err)
14796 +               err = refresh_file(file, reopen);
14797 +       if (!err) {
14798 +               if (!wlock) {
14799 +                       di_downgrade_lock(dentry, AuLock_IR);
14800 +                       fi_downgrade_lock(file);
14801 +               }
14802 +       } else {
14803 +               di_write_unlock(dentry);
14804 +               fi_write_unlock(file);
14805 +       }
14806 +
14807 +out:
14808 +       return err;
14809 +}
14810 +
14811 +/* ---------------------------------------------------------------------- */
14812 +
14813 +/* cf. aufs_nopage() */
14814 +/* for madvise(2) */
14815 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14816 +{
14817 +       unlock_page(page);
14818 +       return 0;
14819 +}
14820 +
14821 +/* it will never be called, but necessary to support O_DIRECT */
14822 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14823 +{ BUG(); return 0; }
14824 +
14825 +/* they will never be called. */
14826 +#ifdef CONFIG_AUFS_DEBUG
14827 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14828 +                           loff_t pos, unsigned len, unsigned flags,
14829 +                           struct page **pagep, void **fsdata)
14830 +{ AuUnsupport(); return 0; }
14831 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14832 +                         loff_t pos, unsigned len, unsigned copied,
14833 +                         struct page *page, void *fsdata)
14834 +{ AuUnsupport(); return 0; }
14835 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14836 +{ AuUnsupport(); return 0; }
14837 +
14838 +static int aufs_set_page_dirty(struct page *page)
14839 +{ AuUnsupport(); return 0; }
14840 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14841 +                               unsigned int length)
14842 +{ AuUnsupport(); }
14843 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14844 +{ AuUnsupport(); return 0; }
14845 +#if 0 /* called by memory compaction regardless file */
14846 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14847 +                           struct page *page, enum migrate_mode mode)
14848 +{ AuUnsupport(); return 0; }
14849 +#endif
14850 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14851 +{ AuUnsupport(); return true; }
14852 +static void aufs_putback_page(struct page *page)
14853 +{ AuUnsupport(); }
14854 +static int aufs_launder_page(struct page *page)
14855 +{ AuUnsupport(); return 0; }
14856 +static int aufs_is_partially_uptodate(struct page *page,
14857 +                                     unsigned long from,
14858 +                                     unsigned long count)
14859 +{ AuUnsupport(); return 0; }
14860 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14861 +                                   bool *writeback)
14862 +{ AuUnsupport(); }
14863 +static int aufs_error_remove_page(struct address_space *mapping,
14864 +                                 struct page *page)
14865 +{ AuUnsupport(); return 0; }
14866 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14867 +                             sector_t *span)
14868 +{ AuUnsupport(); return 0; }
14869 +static void aufs_swap_deactivate(struct file *file)
14870 +{ AuUnsupport(); }
14871 +#endif /* CONFIG_AUFS_DEBUG */
14872 +
14873 +const struct address_space_operations aufs_aop = {
14874 +       .readpage               = aufs_readpage,
14875 +       .direct_IO              = aufs_direct_IO,
14876 +#ifdef CONFIG_AUFS_DEBUG
14877 +       .writepage              = aufs_writepage,
14878 +       /* no writepages, because of writepage */
14879 +       .set_page_dirty         = aufs_set_page_dirty,
14880 +       /* no readpages, because of readpage */
14881 +       .write_begin            = aufs_write_begin,
14882 +       .write_end              = aufs_write_end,
14883 +       /* no bmap, no block device */
14884 +       .invalidatepage         = aufs_invalidatepage,
14885 +       .releasepage            = aufs_releasepage,
14886 +       /* is fallback_migrate_page ok? */
14887 +       /* .migratepage         = aufs_migratepage, */
14888 +       .isolate_page           = aufs_isolate_page,
14889 +       .putback_page           = aufs_putback_page,
14890 +       .launder_page           = aufs_launder_page,
14891 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14892 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14893 +       .error_remove_page      = aufs_error_remove_page,
14894 +       .swap_activate          = aufs_swap_activate,
14895 +       .swap_deactivate        = aufs_swap_deactivate
14896 +#endif /* CONFIG_AUFS_DEBUG */
14897 +};
14898 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14899 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14900 +++ linux/fs/aufs/file.h        2021-12-03 15:38:59.936647310 +0100
14901 @@ -0,0 +1,342 @@
14902 +/* SPDX-License-Identifier: GPL-2.0 */
14903 +/*
14904 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14905 + *
14906 + * This program, aufs is free software; you can redistribute it and/or modify
14907 + * it under the terms of the GNU General Public License as published by
14908 + * the Free Software Foundation; either version 2 of the License, or
14909 + * (at your option) any later version.
14910 + *
14911 + * This program is distributed in the hope that it will be useful,
14912 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14913 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14914 + * GNU General Public License for more details.
14915 + *
14916 + * You should have received a copy of the GNU General Public License
14917 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14918 + */
14919 +
14920 +/*
14921 + * file operations
14922 + */
14923 +
14924 +#ifndef __AUFS_FILE_H__
14925 +#define __AUFS_FILE_H__
14926 +
14927 +#ifdef __KERNEL__
14928 +
14929 +#include <linux/file.h>
14930 +#include <linux/fs.h>
14931 +#include <linux/mm_types.h>
14932 +#include <linux/poll.h>
14933 +#include "rwsem.h"
14934 +
14935 +struct au_branch;
14936 +struct au_hfile {
14937 +       struct file             *hf_file;
14938 +       struct au_branch        *hf_br;
14939 +};
14940 +
14941 +struct au_vdir;
14942 +struct au_fidir {
14943 +       aufs_bindex_t           fd_bbot;
14944 +       aufs_bindex_t           fd_nent;
14945 +       struct au_vdir          *fd_vdir_cache;
14946 +       struct au_hfile         fd_hfile[];
14947 +};
14948 +
14949 +static inline int au_fidir_sz(int nent)
14950 +{
14951 +       AuDebugOn(nent < 0);
14952 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14953 +}
14954 +
14955 +struct au_finfo {
14956 +       atomic_t                fi_generation;
14957 +
14958 +       struct au_rwsem         fi_rwsem;
14959 +       aufs_bindex_t           fi_btop;
14960 +
14961 +       /* do not union them */
14962 +       struct {                                /* for non-dir */
14963 +               struct au_hfile                 fi_htop;
14964 +               atomic_t                        fi_mmapped;
14965 +       };
14966 +       struct au_fidir         *fi_hdir;       /* for dir only */
14967 +
14968 +       struct hlist_bl_node    fi_hlist;
14969 +       struct file             *fi_file;       /* very ugly */
14970 +       struct rcu_head         rcu;
14971 +} ____cacheline_aligned_in_smp;
14972 +
14973 +/* ---------------------------------------------------------------------- */
14974 +
14975 +/* file.c */
14976 +extern const struct address_space_operations aufs_aop;
14977 +unsigned int au_file_roflags(unsigned int flags);
14978 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14979 +                      struct file *file, int force_wr);
14980 +struct au_do_open_args {
14981 +       int             aopen;
14982 +       int             (*open)(struct file *file, int flags,
14983 +                               struct file *h_file);
14984 +       struct au_fidir *fidir;
14985 +       struct file     *h_file;
14986 +};
14987 +int au_do_open(struct file *file, struct au_do_open_args *args);
14988 +int au_reopen_nondir(struct file *file);
14989 +struct au_pin;
14990 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
14991 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14992 +                         int wlock, unsigned int fi_lsc);
14993 +int au_do_flush(struct file *file, fl_owner_t id,
14994 +               int (*flush)(struct file *file, fl_owner_t id));
14995 +
14996 +/* poll.c */
14997 +#ifdef CONFIG_AUFS_POLL
14998 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
14999 +#endif
15000 +
15001 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15002 +/* hfsplus.c */
15003 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15004 +                          int force_wr);
15005 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15006 +                   struct file *h_file);
15007 +#else
15008 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15009 +       aufs_bindex_t bindex, int force_wr)
15010 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15011 +          struct file *h_file);
15012 +#endif
15013 +
15014 +/* f_op.c */
15015 +extern const struct file_operations aufs_file_fop;
15016 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15017 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15018 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15019 +
15020 +/* finfo.c */
15021 +void au_hfput(struct au_hfile *hf, int execed);
15022 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15023 +                  struct file *h_file);
15024 +
15025 +void au_update_figen(struct file *file);
15026 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15027 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15028 +
15029 +void au_fi_init_once(void *_fi);
15030 +void au_finfo_fin(struct file *file);
15031 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15032 +
15033 +/* ioctl.c */
15034 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15035 +#ifdef CONFIG_COMPAT
15036 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15037 +                          unsigned long arg);
15038 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15039 +                             unsigned long arg);
15040 +#endif
15041 +
15042 +/* ---------------------------------------------------------------------- */
15043 +
15044 +static inline struct au_finfo *au_fi(struct file *file)
15045 +{
15046 +       return file->private_data;
15047 +}
15048 +
15049 +/* ---------------------------------------------------------------------- */
15050 +
15051 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15052 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15053 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15054 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15055 +/*
15056 +#define fi_read_trylock_nested(f) \
15057 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15058 +#define fi_write_trylock_nested(f) \
15059 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15060 +*/
15061 +
15062 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15063 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15064 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15065 +
15066 +/* lock subclass for finfo */
15067 +enum {
15068 +       AuLsc_FI_1,
15069 +       AuLsc_FI_2
15070 +};
15071 +
15072 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15073 +{
15074 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15075 +}
15076 +
15077 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15078 +{
15079 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15080 +}
15081 +
15082 +/*
15083 + * fi_read_lock_1, fi_write_lock_1,
15084 + * fi_read_lock_2, fi_write_lock_2
15085 + */
15086 +#define AuReadLockFunc(name) \
15087 +static inline void fi_read_lock_##name(struct file *f) \
15088 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15089 +
15090 +#define AuWriteLockFunc(name) \
15091 +static inline void fi_write_lock_##name(struct file *f) \
15092 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15093 +
15094 +#define AuRWLockFuncs(name) \
15095 +       AuReadLockFunc(name) \
15096 +       AuWriteLockFunc(name)
15097 +
15098 +AuRWLockFuncs(1);
15099 +AuRWLockFuncs(2);
15100 +
15101 +#undef AuReadLockFunc
15102 +#undef AuWriteLockFunc
15103 +#undef AuRWLockFuncs
15104 +
15105 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15106 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15107 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15108 +
15109 +/* ---------------------------------------------------------------------- */
15110 +
15111 +/* todo: hard/soft set? */
15112 +static inline aufs_bindex_t au_fbtop(struct file *file)
15113 +{
15114 +       FiMustAnyLock(file);
15115 +       return au_fi(file)->fi_btop;
15116 +}
15117 +
15118 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15119 +{
15120 +       FiMustAnyLock(file);
15121 +       AuDebugOn(!au_fi(file)->fi_hdir);
15122 +       return au_fi(file)->fi_hdir->fd_bbot;
15123 +}
15124 +
15125 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15126 +{
15127 +       FiMustAnyLock(file);
15128 +       AuDebugOn(!au_fi(file)->fi_hdir);
15129 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15130 +}
15131 +
15132 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15133 +{
15134 +       FiMustWriteLock(file);
15135 +       au_fi(file)->fi_btop = bindex;
15136 +}
15137 +
15138 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15139 +{
15140 +       FiMustWriteLock(file);
15141 +       AuDebugOn(!au_fi(file)->fi_hdir);
15142 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15143 +}
15144 +
15145 +static inline void au_set_fvdir_cache(struct file *file,
15146 +                                     struct au_vdir *vdir_cache)
15147 +{
15148 +       FiMustWriteLock(file);
15149 +       AuDebugOn(!au_fi(file)->fi_hdir);
15150 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15151 +}
15152 +
15153 +static inline struct file *au_hf_top(struct file *file)
15154 +{
15155 +       FiMustAnyLock(file);
15156 +       AuDebugOn(au_fi(file)->fi_hdir);
15157 +       return au_fi(file)->fi_htop.hf_file;
15158 +}
15159 +
15160 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15161 +{
15162 +       FiMustAnyLock(file);
15163 +       AuDebugOn(!au_fi(file)->fi_hdir);
15164 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15165 +}
15166 +
15167 +/* todo: memory barrier? */
15168 +static inline unsigned int au_figen(struct file *f)
15169 +{
15170 +       return atomic_read(&au_fi(f)->fi_generation);
15171 +}
15172 +
15173 +static inline void au_set_mmapped(struct file *f)
15174 +{
15175 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15176 +               return;
15177 +       pr_warn("fi_mmapped wrapped around\n");
15178 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15179 +               ;
15180 +}
15181 +
15182 +static inline void au_unset_mmapped(struct file *f)
15183 +{
15184 +       atomic_dec(&au_fi(f)->fi_mmapped);
15185 +}
15186 +
15187 +static inline int au_test_mmapped(struct file *f)
15188 +{
15189 +       return atomic_read(&au_fi(f)->fi_mmapped);
15190 +}
15191 +
15192 +/* customize vma->vm_file */
15193 +
15194 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15195 +                                      struct file *file)
15196 +{
15197 +       struct file *f;
15198 +
15199 +       f = vma->vm_file;
15200 +       get_file(file);
15201 +       vma->vm_file = file;
15202 +       fput(f);
15203 +}
15204 +
15205 +#ifdef CONFIG_MMU
15206 +#define AuDbgVmRegion(file, vma) do {} while (0)
15207 +
15208 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15209 +                                   struct file *file)
15210 +{
15211 +       au_do_vm_file_reset(vma, file);
15212 +}
15213 +#else
15214 +#define AuDbgVmRegion(file, vma) \
15215 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15216 +
15217 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15218 +                                   struct file *file)
15219 +{
15220 +       struct file *f;
15221 +
15222 +       au_do_vm_file_reset(vma, file);
15223 +       f = vma->vm_region->vm_file;
15224 +       get_file(file);
15225 +       vma->vm_region->vm_file = file;
15226 +       fput(f);
15227 +}
15228 +#endif /* CONFIG_MMU */
15229 +
15230 +/* handle vma->vm_prfile */
15231 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15232 +                                   struct file *file)
15233 +{
15234 +       get_file(file);
15235 +       vma->vm_prfile = file;
15236 +#ifndef CONFIG_MMU
15237 +       get_file(file);
15238 +       vma->vm_region->vm_prfile = file;
15239 +#endif
15240 +}
15241 +
15242 +#endif /* __KERNEL__ */
15243 +#endif /* __AUFS_FILE_H__ */
15244 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15245 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15246 +++ linux/fs/aufs/finfo.c       2021-12-03 15:38:59.936647310 +0100
15247 @@ -0,0 +1,149 @@
15248 +// SPDX-License-Identifier: GPL-2.0
15249 +/*
15250 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15251 + *
15252 + * This program, aufs is free software; you can redistribute it and/or modify
15253 + * it under the terms of the GNU General Public License as published by
15254 + * the Free Software Foundation; either version 2 of the License, or
15255 + * (at your option) any later version.
15256 + *
15257 + * This program is distributed in the hope that it will be useful,
15258 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15259 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15260 + * GNU General Public License for more details.
15261 + *
15262 + * You should have received a copy of the GNU General Public License
15263 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15264 + */
15265 +
15266 +/*
15267 + * file private data
15268 + */
15269 +
15270 +#include "aufs.h"
15271 +
15272 +void au_hfput(struct au_hfile *hf, int execed)
15273 +{
15274 +       if (execed)
15275 +               allow_write_access(hf->hf_file);
15276 +       fput(hf->hf_file);
15277 +       hf->hf_file = NULL;
15278 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15279 +       hf->hf_br = NULL;
15280 +}
15281 +
15282 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15283 +{
15284 +       struct au_finfo *finfo = au_fi(file);
15285 +       struct au_hfile *hf;
15286 +       struct au_fidir *fidir;
15287 +
15288 +       fidir = finfo->fi_hdir;
15289 +       if (!fidir) {
15290 +               AuDebugOn(finfo->fi_btop != bindex);
15291 +               hf = &finfo->fi_htop;
15292 +       } else
15293 +               hf = fidir->fd_hfile + bindex;
15294 +
15295 +       if (hf && hf->hf_file)
15296 +               au_hfput(hf, vfsub_file_execed(file));
15297 +       if (val) {
15298 +               FiMustWriteLock(file);
15299 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15300 +               hf->hf_file = val;
15301 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15302 +       }
15303 +}
15304 +
15305 +void au_update_figen(struct file *file)
15306 +{
15307 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15308 +       /* smp_mb(); */ /* atomic_set */
15309 +}
15310 +
15311 +/* ---------------------------------------------------------------------- */
15312 +
15313 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15314 +{
15315 +       struct au_fidir *fidir;
15316 +       int nbr;
15317 +
15318 +       nbr = au_sbbot(sb) + 1;
15319 +       if (nbr < 2)
15320 +               nbr = 2; /* initial allocate for 2 branches */
15321 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15322 +       if (fidir) {
15323 +               fidir->fd_bbot = -1;
15324 +               fidir->fd_nent = nbr;
15325 +       }
15326 +
15327 +       return fidir;
15328 +}
15329 +
15330 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15331 +{
15332 +       int err;
15333 +       struct au_fidir *fidir, *p;
15334 +
15335 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15336 +       fidir = finfo->fi_hdir;
15337 +       AuDebugOn(!fidir);
15338 +
15339 +       err = -ENOMEM;
15340 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15341 +                        GFP_NOFS, may_shrink);
15342 +       if (p) {
15343 +               p->fd_nent = nbr;
15344 +               finfo->fi_hdir = p;
15345 +               err = 0;
15346 +       }
15347 +
15348 +       return err;
15349 +}
15350 +
15351 +/* ---------------------------------------------------------------------- */
15352 +
15353 +void au_finfo_fin(struct file *file)
15354 +{
15355 +       struct au_finfo *finfo;
15356 +
15357 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15358 +
15359 +       finfo = au_fi(file);
15360 +       AuDebugOn(finfo->fi_hdir);
15361 +       AuRwDestroy(&finfo->fi_rwsem);
15362 +       au_cache_free_finfo(finfo);
15363 +}
15364 +
15365 +void au_fi_init_once(void *_finfo)
15366 +{
15367 +       struct au_finfo *finfo = _finfo;
15368 +
15369 +       au_rw_init(&finfo->fi_rwsem);
15370 +}
15371 +
15372 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15373 +{
15374 +       int err;
15375 +       struct au_finfo *finfo;
15376 +       struct dentry *dentry;
15377 +
15378 +       err = -ENOMEM;
15379 +       dentry = file->f_path.dentry;
15380 +       finfo = au_cache_alloc_finfo();
15381 +       if (unlikely(!finfo))
15382 +               goto out;
15383 +
15384 +       err = 0;
15385 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15386 +       au_rw_write_lock(&finfo->fi_rwsem);
15387 +       finfo->fi_btop = -1;
15388 +       finfo->fi_hdir = fidir;
15389 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15390 +       /* smp_mb(); */ /* atomic_set */
15391 +
15392 +       file->private_data = finfo;
15393 +
15394 +out:
15395 +       return err;
15396 +}
15397 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15398 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15399 +++ linux/fs/aufs/f_op.c        2021-12-03 15:38:59.933313976 +0100
15400 @@ -0,0 +1,771 @@
15401 +// SPDX-License-Identifier: GPL-2.0
15402 +/*
15403 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15404 + *
15405 + * This program, aufs is free software; you can redistribute it and/or modify
15406 + * it under the terms of the GNU General Public License as published by
15407 + * the Free Software Foundation; either version 2 of the License, or
15408 + * (at your option) any later version.
15409 + *
15410 + * This program is distributed in the hope that it will be useful,
15411 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15412 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15413 + * GNU General Public License for more details.
15414 + *
15415 + * You should have received a copy of the GNU General Public License
15416 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15417 + */
15418 +
15419 +/*
15420 + * file and vm operations
15421 + */
15422 +
15423 +#include <linux/aio.h>
15424 +#include <linux/fs_stack.h>
15425 +#include <linux/mman.h>
15426 +#include <linux/security.h>
15427 +#include "aufs.h"
15428 +
15429 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15430 +{
15431 +       int err;
15432 +       aufs_bindex_t bindex;
15433 +       struct dentry *dentry, *h_dentry;
15434 +       struct au_finfo *finfo;
15435 +       struct inode *h_inode;
15436 +
15437 +       FiMustWriteLock(file);
15438 +
15439 +       err = 0;
15440 +       dentry = file->f_path.dentry;
15441 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15442 +       finfo = au_fi(file);
15443 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15444 +       atomic_set(&finfo->fi_mmapped, 0);
15445 +       bindex = au_dbtop(dentry);
15446 +       if (!h_file) {
15447 +               h_dentry = au_h_dptr(dentry, bindex);
15448 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15449 +               if (unlikely(err))
15450 +                       goto out;
15451 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15452 +               if (IS_ERR(h_file)) {
15453 +                       err = PTR_ERR(h_file);
15454 +                       goto out;
15455 +               }
15456 +       } else {
15457 +               h_dentry = h_file->f_path.dentry;
15458 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15459 +               if (unlikely(err))
15460 +                       goto out;
15461 +               /* br ref is already inc-ed */
15462 +       }
15463 +
15464 +       if ((flags & __O_TMPFILE)
15465 +           && !(flags & O_EXCL)) {
15466 +               h_inode = file_inode(h_file);
15467 +               spin_lock(&h_inode->i_lock);
15468 +               h_inode->i_state |= I_LINKABLE;
15469 +               spin_unlock(&h_inode->i_lock);
15470 +       }
15471 +       au_set_fbtop(file, bindex);
15472 +       au_set_h_fptr(file, bindex, h_file);
15473 +       au_update_figen(file);
15474 +       /* todo: necessary? */
15475 +       /* file->f_ra = h_file->f_ra; */
15476 +
15477 +out:
15478 +       return err;
15479 +}
15480 +
15481 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15482 +                           struct file *file)
15483 +{
15484 +       int err;
15485 +       struct super_block *sb;
15486 +       struct au_do_open_args args = {
15487 +               .open   = au_do_open_nondir
15488 +       };
15489 +
15490 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15491 +             file, vfsub_file_flags(file), file->f_mode);
15492 +
15493 +       sb = file->f_path.dentry->d_sb;
15494 +       si_read_lock(sb, AuLock_FLUSH);
15495 +       err = au_do_open(file, &args);
15496 +       si_read_unlock(sb);
15497 +       return err;
15498 +}
15499 +
15500 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15501 +{
15502 +       struct au_finfo *finfo;
15503 +       aufs_bindex_t bindex;
15504 +
15505 +       finfo = au_fi(file);
15506 +       au_hbl_del(&finfo->fi_hlist,
15507 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15508 +       bindex = finfo->fi_btop;
15509 +       if (bindex >= 0)
15510 +               au_set_h_fptr(file, bindex, NULL);
15511 +
15512 +       au_finfo_fin(file);
15513 +       return 0;
15514 +}
15515 +
15516 +/* ---------------------------------------------------------------------- */
15517 +
15518 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15519 +{
15520 +       int err;
15521 +       struct file *h_file;
15522 +
15523 +       err = 0;
15524 +       h_file = au_hf_top(file);
15525 +       if (h_file)
15526 +               err = vfsub_flush(h_file, id);
15527 +       return err;
15528 +}
15529 +
15530 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15531 +{
15532 +       return au_do_flush(file, id, au_do_flush_nondir);
15533 +}
15534 +
15535 +/* ---------------------------------------------------------------------- */
15536 +/*
15537 + * read and write functions acquire [fdi]_rwsem once, but release before
15538 + * mmap_sem. This is because to stop a race condition between mmap(2).
15539 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15540 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15541 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15542 + */
15543 +
15544 +/* Callers should call au_read_post() or fput() in the end */
15545 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15546 +{
15547 +       struct file *h_file;
15548 +       int err;
15549 +
15550 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15551 +       if (!err) {
15552 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15553 +               h_file = au_hf_top(file);
15554 +               get_file(h_file);
15555 +               if (!keep_fi)
15556 +                       fi_read_unlock(file);
15557 +       } else
15558 +               h_file = ERR_PTR(err);
15559 +
15560 +       return h_file;
15561 +}
15562 +
15563 +static void au_read_post(struct inode *inode, struct file *h_file)
15564 +{
15565 +       /* update without lock, I don't think it a problem */
15566 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15567 +       fput(h_file);
15568 +}
15569 +
15570 +struct au_write_pre {
15571 +       /* input */
15572 +       unsigned int lsc;
15573 +
15574 +       /* output */
15575 +       blkcnt_t blks;
15576 +       aufs_bindex_t btop;
15577 +};
15578 +
15579 +/*
15580 + * return with iinfo is write-locked
15581 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15582 + * end
15583 + */
15584 +static struct file *au_write_pre(struct file *file, int do_ready,
15585 +                                struct au_write_pre *wpre)
15586 +{
15587 +       struct file *h_file;
15588 +       struct dentry *dentry;
15589 +       int err;
15590 +       unsigned int lsc;
15591 +       struct au_pin pin;
15592 +
15593 +       lsc = 0;
15594 +       if (wpre)
15595 +               lsc = wpre->lsc;
15596 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15597 +       h_file = ERR_PTR(err);
15598 +       if (unlikely(err))
15599 +               goto out;
15600 +
15601 +       dentry = file->f_path.dentry;
15602 +       if (do_ready) {
15603 +               err = au_ready_to_write(file, -1, &pin);
15604 +               if (unlikely(err)) {
15605 +                       h_file = ERR_PTR(err);
15606 +                       di_write_unlock(dentry);
15607 +                       goto out_fi;
15608 +               }
15609 +       }
15610 +
15611 +       di_downgrade_lock(dentry, /*flags*/0);
15612 +       if (wpre)
15613 +               wpre->btop = au_fbtop(file);
15614 +       h_file = au_hf_top(file);
15615 +       get_file(h_file);
15616 +       if (wpre)
15617 +               wpre->blks = file_inode(h_file)->i_blocks;
15618 +       if (do_ready)
15619 +               au_unpin(&pin);
15620 +       di_read_unlock(dentry, /*flags*/0);
15621 +
15622 +out_fi:
15623 +       fi_write_unlock(file);
15624 +out:
15625 +       return h_file;
15626 +}
15627 +
15628 +static void au_write_post(struct inode *inode, struct file *h_file,
15629 +                         struct au_write_pre *wpre, ssize_t written)
15630 +{
15631 +       struct inode *h_inode;
15632 +
15633 +       au_cpup_attr_timesizes(inode);
15634 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15635 +       h_inode = file_inode(h_file);
15636 +       inode->i_mode = h_inode->i_mode;
15637 +       ii_write_unlock(inode);
15638 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15639 +       if (written > 0)
15640 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15641 +                             /*force*/h_inode->i_blocks > wpre->blks);
15642 +       fput(h_file);
15643 +}
15644 +
15645 +/*
15646 + * todo: very ugly
15647 + * it locks both of i_mutex and si_rwsem for read in safe.
15648 + * if the plink maintenance mode continues forever (that is the problem),
15649 + * may loop forever.
15650 + */
15651 +static void au_mtx_and_read_lock(struct inode *inode)
15652 +{
15653 +       int err;
15654 +       struct super_block *sb = inode->i_sb;
15655 +
15656 +       while (1) {
15657 +               inode_lock(inode);
15658 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15659 +               if (!err)
15660 +                       break;
15661 +               inode_unlock(inode);
15662 +               si_read_lock(sb, AuLock_NOPLMW);
15663 +               si_read_unlock(sb);
15664 +       }
15665 +}
15666 +
15667 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15668 +                         struct iov_iter *iov_iter)
15669 +{
15670 +       ssize_t err;
15671 +       struct file *file;
15672 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15673 +
15674 +       err = security_file_permission(h_file, rw);
15675 +       if (unlikely(err))
15676 +               goto out;
15677 +
15678 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15679 +       iter = NULL;
15680 +       if (rw == MAY_READ)
15681 +               iter = h_file->f_op->read_iter;
15682 +       else if (rw == MAY_WRITE)
15683 +               iter = h_file->f_op->write_iter;
15684 +
15685 +       file = kio->ki_filp;
15686 +       kio->ki_filp = h_file;
15687 +       if (iter) {
15688 +               lockdep_off();
15689 +               err = iter(kio, iov_iter);
15690 +               lockdep_on();
15691 +       } else
15692 +               /* currently there is no such fs */
15693 +               WARN_ON_ONCE(1);
15694 +       kio->ki_filp = file;
15695 +
15696 +out:
15697 +       return err;
15698 +}
15699 +
15700 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15701 +{
15702 +       ssize_t err;
15703 +       struct file *file, *h_file;
15704 +       struct inode *inode;
15705 +       struct super_block *sb;
15706 +
15707 +       file = kio->ki_filp;
15708 +       inode = file_inode(file);
15709 +       sb = inode->i_sb;
15710 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15711 +
15712 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15713 +       err = PTR_ERR(h_file);
15714 +       if (IS_ERR(h_file))
15715 +               goto out;
15716 +
15717 +       if (au_test_loopback_kthread()) {
15718 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15719 +               if (file->f_mapping != h_file->f_mapping) {
15720 +                       file->f_mapping = h_file->f_mapping;
15721 +                       smp_mb(); /* unnecessary? */
15722 +               }
15723 +       }
15724 +       fi_read_unlock(file);
15725 +
15726 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15727 +       /* todo: necessary? */
15728 +       /* file->f_ra = h_file->f_ra; */
15729 +       au_read_post(inode, h_file);
15730 +
15731 +out:
15732 +       si_read_unlock(sb);
15733 +       return err;
15734 +}
15735 +
15736 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15737 +{
15738 +       ssize_t err;
15739 +       struct au_write_pre wpre;
15740 +       struct inode *inode;
15741 +       struct file *file, *h_file;
15742 +
15743 +       file = kio->ki_filp;
15744 +       inode = file_inode(file);
15745 +       au_mtx_and_read_lock(inode);
15746 +
15747 +       wpre.lsc = 0;
15748 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15749 +       err = PTR_ERR(h_file);
15750 +       if (IS_ERR(h_file))
15751 +               goto out;
15752 +
15753 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15754 +       au_write_post(inode, h_file, &wpre, err);
15755 +
15756 +out:
15757 +       si_read_unlock(inode->i_sb);
15758 +       inode_unlock(inode);
15759 +       return err;
15760 +}
15761 +
15762 +/*
15763 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15764 + * don't have their own .splice_{read,write} implimentations, and they use
15765 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15766 + * simple converters to f_op->iter_read() and ->iter_write().
15767 + * But we keep our own implementations because some non-mainlined FSes may have
15768 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15769 + * away an opportunity to co-work with aufs from them.
15770 + */
15771 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15772 +                               struct pipe_inode_info *pipe, size_t len,
15773 +                               unsigned int flags)
15774 +{
15775 +       ssize_t err;
15776 +       struct file *h_file;
15777 +       struct inode *inode;
15778 +       struct super_block *sb;
15779 +
15780 +       inode = file_inode(file);
15781 +       sb = inode->i_sb;
15782 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15783 +
15784 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15785 +       err = PTR_ERR(h_file);
15786 +       if (IS_ERR(h_file))
15787 +               goto out;
15788 +
15789 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15790 +       /* todo: necessary? */
15791 +       /* file->f_ra = h_file->f_ra; */
15792 +       au_read_post(inode, h_file);
15793 +
15794 +out:
15795 +       si_read_unlock(sb);
15796 +       return err;
15797 +}
15798 +
15799 +static ssize_t
15800 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15801 +                 size_t len, unsigned int flags)
15802 +{
15803 +       ssize_t err;
15804 +       struct au_write_pre wpre;
15805 +       struct inode *inode;
15806 +       struct file *h_file;
15807 +
15808 +       inode = file_inode(file);
15809 +       au_mtx_and_read_lock(inode);
15810 +
15811 +       wpre.lsc = 0;
15812 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15813 +       err = PTR_ERR(h_file);
15814 +       if (IS_ERR(h_file))
15815 +               goto out;
15816 +
15817 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15818 +       au_write_post(inode, h_file, &wpre, err);
15819 +
15820 +out:
15821 +       si_read_unlock(inode->i_sb);
15822 +       inode_unlock(inode);
15823 +       return err;
15824 +}
15825 +
15826 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15827 +                          loff_t len)
15828 +{
15829 +       long err;
15830 +       struct au_write_pre wpre;
15831 +       struct inode *inode;
15832 +       struct file *h_file;
15833 +
15834 +       inode = file_inode(file);
15835 +       au_mtx_and_read_lock(inode);
15836 +
15837 +       wpre.lsc = 0;
15838 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15839 +       err = PTR_ERR(h_file);
15840 +       if (IS_ERR(h_file))
15841 +               goto out;
15842 +
15843 +       lockdep_off();
15844 +       err = vfs_fallocate(h_file, mode, offset, len);
15845 +       lockdep_on();
15846 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15847 +
15848 +out:
15849 +       si_read_unlock(inode->i_sb);
15850 +       inode_unlock(inode);
15851 +       return err;
15852 +}
15853 +
15854 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15855 +                                   struct file *dst, loff_t dst_pos,
15856 +                                   size_t len, unsigned int flags)
15857 +{
15858 +       ssize_t err;
15859 +       struct au_write_pre wpre;
15860 +       enum { SRC, DST };
15861 +       struct {
15862 +               struct inode *inode;
15863 +               struct file *h_file;
15864 +               struct super_block *h_sb;
15865 +       } a[2];
15866 +#define a_src  a[SRC]
15867 +#define a_dst  a[DST]
15868 +
15869 +       err = -EINVAL;
15870 +       a_src.inode = file_inode(src);
15871 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15872 +               goto out;
15873 +       a_dst.inode = file_inode(dst);
15874 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15875 +               goto out;
15876 +
15877 +       au_mtx_and_read_lock(a_dst.inode);
15878 +       /*
15879 +        * in order to match the order in di_write_lock2_{child,parent}(),
15880 +        * use f_path.dentry for this comparison.
15881 +        */
15882 +       if (src->f_path.dentry < dst->f_path.dentry) {
15883 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15884 +               err = PTR_ERR(a_src.h_file);
15885 +               if (IS_ERR(a_src.h_file))
15886 +                       goto out_si;
15887 +
15888 +               wpre.lsc = AuLsc_FI_2;
15889 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15890 +               err = PTR_ERR(a_dst.h_file);
15891 +               if (IS_ERR(a_dst.h_file)) {
15892 +                       au_read_post(a_src.inode, a_src.h_file);
15893 +                       goto out_si;
15894 +               }
15895 +       } else {
15896 +               wpre.lsc = AuLsc_FI_1;
15897 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15898 +               err = PTR_ERR(a_dst.h_file);
15899 +               if (IS_ERR(a_dst.h_file))
15900 +                       goto out_si;
15901 +
15902 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15903 +               err = PTR_ERR(a_src.h_file);
15904 +               if (IS_ERR(a_src.h_file)) {
15905 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15906 +                                     /*written*/0);
15907 +                       goto out_si;
15908 +               }
15909 +       }
15910 +
15911 +       err = -EXDEV;
15912 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15913 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15914 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15915 +               AuDbgFile(src);
15916 +               AuDbgFile(dst);
15917 +               goto out_file;
15918 +       }
15919 +
15920 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15921 +                                   dst_pos, len, flags);
15922 +
15923 +out_file:
15924 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15925 +       fi_read_unlock(src);
15926 +       au_read_post(a_src.inode, a_src.h_file);
15927 +out_si:
15928 +       si_read_unlock(a_dst.inode->i_sb);
15929 +       inode_unlock(a_dst.inode);
15930 +out:
15931 +       return err;
15932 +#undef a_src
15933 +#undef a_dst
15934 +}
15935 +
15936 +/* ---------------------------------------------------------------------- */
15937 +
15938 +/*
15939 + * The locking order around current->mmap_sem.
15940 + * - in most and regular cases
15941 + *   file I/O syscall -- aufs_read() or something
15942 + *     -- si_rwsem for read -- mmap_sem
15943 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15944 + * - in mmap case
15945 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15946 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15947 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15948 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15949 + * It means that when aufs acquires si_rwsem for write, the process should never
15950 + * acquire mmap_sem.
15951 + *
15952 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15953 + * problem either since any directory is not able to be mmap-ed.
15954 + * The similar scenario is applied to aufs_readlink() too.
15955 + */
15956 +
15957 +#if 0 /* stop calling security_file_mmap() */
15958 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15959 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15960 +
15961 +static unsigned long au_arch_prot_conv(unsigned long flags)
15962 +{
15963 +       /* currently ppc64 only */
15964 +#ifdef CONFIG_PPC64
15965 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15966 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15967 +       return AuConv_VM_PROT(flags, SAO);
15968 +#else
15969 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15970 +       return 0;
15971 +#endif
15972 +}
15973 +
15974 +static unsigned long au_prot_conv(unsigned long flags)
15975 +{
15976 +       return AuConv_VM_PROT(flags, READ)
15977 +               | AuConv_VM_PROT(flags, WRITE)
15978 +               | AuConv_VM_PROT(flags, EXEC)
15979 +               | au_arch_prot_conv(flags);
15980 +}
15981 +
15982 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15983 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15984 +
15985 +static unsigned long au_flag_conv(unsigned long flags)
15986 +{
15987 +       return AuConv_VM_MAP(flags, GROWSDOWN)
15988 +               | AuConv_VM_MAP(flags, DENYWRITE)
15989 +               | AuConv_VM_MAP(flags, LOCKED);
15990 +}
15991 +#endif
15992 +
15993 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
15994 +{
15995 +       int err;
15996 +       const unsigned char wlock
15997 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
15998 +       struct super_block *sb;
15999 +       struct file *h_file;
16000 +       struct inode *inode;
16001 +
16002 +       AuDbgVmRegion(file, vma);
16003 +
16004 +       inode = file_inode(file);
16005 +       sb = inode->i_sb;
16006 +       lockdep_off();
16007 +       si_read_lock(sb, AuLock_NOPLMW);
16008 +
16009 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16010 +       lockdep_on();
16011 +       err = PTR_ERR(h_file);
16012 +       if (IS_ERR(h_file))
16013 +               goto out;
16014 +
16015 +       err = 0;
16016 +       au_set_mmapped(file);
16017 +       au_vm_file_reset(vma, h_file);
16018 +       /*
16019 +        * we cannot call security_mmap_file() here since it may acquire
16020 +        * mmap_sem or i_mutex.
16021 +        *
16022 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16023 +        *                       au_flag_conv(vma->vm_flags));
16024 +        */
16025 +       if (!err)
16026 +               err = call_mmap(h_file, vma);
16027 +       if (!err) {
16028 +               au_vm_prfile_set(vma, file);
16029 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16030 +               goto out_fput; /* success */
16031 +       }
16032 +       au_unset_mmapped(file);
16033 +       au_vm_file_reset(vma, file);
16034 +
16035 +out_fput:
16036 +       lockdep_off();
16037 +       ii_write_unlock(inode);
16038 +       lockdep_on();
16039 +       fput(h_file);
16040 +out:
16041 +       lockdep_off();
16042 +       si_read_unlock(sb);
16043 +       lockdep_on();
16044 +       AuTraceErr(err);
16045 +       return err;
16046 +}
16047 +
16048 +/* ---------------------------------------------------------------------- */
16049 +
16050 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16051 +                            int datasync)
16052 +{
16053 +       int err;
16054 +       struct au_write_pre wpre;
16055 +       struct inode *inode;
16056 +       struct file *h_file;
16057 +
16058 +       err = 0; /* -EBADF; */ /* posix? */
16059 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16060 +               goto out;
16061 +
16062 +       inode = file_inode(file);
16063 +       au_mtx_and_read_lock(inode);
16064 +
16065 +       wpre.lsc = 0;
16066 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16067 +       err = PTR_ERR(h_file);
16068 +       if (IS_ERR(h_file))
16069 +               goto out_unlock;
16070 +
16071 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16072 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16073 +
16074 +out_unlock:
16075 +       si_read_unlock(inode->i_sb);
16076 +       inode_unlock(inode);
16077 +out:
16078 +       return err;
16079 +}
16080 +
16081 +static int aufs_fasync(int fd, struct file *file, int flag)
16082 +{
16083 +       int err;
16084 +       struct file *h_file;
16085 +       struct super_block *sb;
16086 +
16087 +       sb = file->f_path.dentry->d_sb;
16088 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16089 +
16090 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16091 +       err = PTR_ERR(h_file);
16092 +       if (IS_ERR(h_file))
16093 +               goto out;
16094 +
16095 +       if (h_file->f_op->fasync)
16096 +               err = h_file->f_op->fasync(fd, h_file, flag);
16097 +       fput(h_file); /* instead of au_read_post() */
16098 +
16099 +out:
16100 +       si_read_unlock(sb);
16101 +       return err;
16102 +}
16103 +
16104 +static int aufs_setfl(struct file *file, unsigned long arg)
16105 +{
16106 +       int err;
16107 +       struct file *h_file;
16108 +       struct super_block *sb;
16109 +
16110 +       sb = file->f_path.dentry->d_sb;
16111 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16112 +
16113 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16114 +       err = PTR_ERR(h_file);
16115 +       if (IS_ERR(h_file))
16116 +               goto out;
16117 +
16118 +       /* stop calling h_file->fasync */
16119 +       arg |= vfsub_file_flags(file) & FASYNC;
16120 +       err = setfl(/*unused fd*/-1, h_file, arg);
16121 +       fput(h_file); /* instead of au_read_post() */
16122 +
16123 +out:
16124 +       si_read_unlock(sb);
16125 +       return err;
16126 +}
16127 +
16128 +/* ---------------------------------------------------------------------- */
16129 +
16130 +/* no one supports this operation, currently */
16131 +#if 0 /* reserved for future use */
16132 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16133 +                            size_t len, loff_t *pos, int more)
16134 +{
16135 +}
16136 +#endif
16137 +
16138 +/* ---------------------------------------------------------------------- */
16139 +
16140 +const struct file_operations aufs_file_fop = {
16141 +       .owner          = THIS_MODULE,
16142 +
16143 +       .llseek         = default_llseek,
16144 +
16145 +       .read_iter      = aufs_read_iter,
16146 +       .write_iter     = aufs_write_iter,
16147 +
16148 +#ifdef CONFIG_AUFS_POLL
16149 +       .poll           = aufs_poll,
16150 +#endif
16151 +       .unlocked_ioctl = aufs_ioctl_nondir,
16152 +#ifdef CONFIG_COMPAT
16153 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16154 +#endif
16155 +       .mmap           = aufs_mmap,
16156 +       .open           = aufs_open_nondir,
16157 +       .flush          = aufs_flush_nondir,
16158 +       .release        = aufs_release_nondir,
16159 +       .fsync          = aufs_fsync_nondir,
16160 +       .fasync         = aufs_fasync,
16161 +       /* .sendpage    = aufs_sendpage, */
16162 +       .setfl          = aufs_setfl,
16163 +       .splice_write   = aufs_splice_write,
16164 +       .splice_read    = aufs_splice_read,
16165 +#if 0 /* reserved for future use */
16166 +       .aio_splice_write = aufs_aio_splice_write,
16167 +       .aio_splice_read  = aufs_aio_splice_read,
16168 +#endif
16169 +       .fallocate      = aufs_fallocate,
16170 +       .copy_file_range = aufs_copy_file_range
16171 +};
16172 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16173 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16174 +++ linux/fs/aufs/fstype.h      2021-12-03 15:38:59.936647310 +0100
16175 @@ -0,0 +1,401 @@
16176 +/* SPDX-License-Identifier: GPL-2.0 */
16177 +/*
16178 + * Copyright (C) 2005-2021 Junjiro R. Okajima
16179 + *
16180 + * This program, aufs is free software; you can redistribute it and/or modify
16181 + * it under the terms of the GNU General Public License as published by
16182 + * the Free Software Foundation; either version 2 of the License, or
16183 + * (at your option) any later version.
16184 + *
16185 + * This program is distributed in the hope that it will be useful,
16186 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16187 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16188 + * GNU General Public License for more details.
16189 + *
16190 + * You should have received a copy of the GNU General Public License
16191 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16192 + */
16193 +
16194 +/*
16195 + * judging filesystem type
16196 + */
16197 +
16198 +#ifndef __AUFS_FSTYPE_H__
16199 +#define __AUFS_FSTYPE_H__
16200 +
16201 +#ifdef __KERNEL__
16202 +
16203 +#include <linux/fs.h>
16204 +#include <linux/magic.h>
16205 +#include <linux/nfs_fs.h>
16206 +#include <linux/romfs_fs.h>
16207 +
16208 +static inline int au_test_aufs(struct super_block *sb)
16209 +{
16210 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16211 +}
16212 +
16213 +static inline const char *au_sbtype(struct super_block *sb)
16214 +{
16215 +       return sb->s_type->name;
16216 +}
16217 +
16218 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16219 +{
16220 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16221 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16222 +#else
16223 +       return 0;
16224 +#endif
16225 +}
16226 +
16227 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16228 +{
16229 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16230 +       return sb->s_magic == ROMFS_MAGIC;
16231 +#else
16232 +       return 0;
16233 +#endif
16234 +}
16235 +
16236 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16237 +{
16238 +#if IS_ENABLED(CONFIG_CRAMFS)
16239 +       return sb->s_magic == CRAMFS_MAGIC;
16240 +#endif
16241 +       return 0;
16242 +}
16243 +
16244 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16245 +{
16246 +#if IS_ENABLED(CONFIG_NFS_FS)
16247 +       return sb->s_magic == NFS_SUPER_MAGIC;
16248 +#else
16249 +       return 0;
16250 +#endif
16251 +}
16252 +
16253 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16254 +{
16255 +#if IS_ENABLED(CONFIG_FUSE_FS)
16256 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16257 +#else
16258 +       return 0;
16259 +#endif
16260 +}
16261 +
16262 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16263 +{
16264 +#if IS_ENABLED(CONFIG_XFS_FS)
16265 +       return sb->s_magic == XFS_SB_MAGIC;
16266 +#else
16267 +       return 0;
16268 +#endif
16269 +}
16270 +
16271 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16272 +{
16273 +#ifdef CONFIG_TMPFS
16274 +       return sb->s_magic == TMPFS_MAGIC;
16275 +#else
16276 +       return 0;
16277 +#endif
16278 +}
16279 +
16280 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16281 +{
16282 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16283 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16284 +#else
16285 +       return 0;
16286 +#endif
16287 +}
16288 +
16289 +static inline int au_test_ramfs(struct super_block *sb)
16290 +{
16291 +       return sb->s_magic == RAMFS_MAGIC;
16292 +}
16293 +
16294 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16295 +{
16296 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16297 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16298 +#else
16299 +       return 0;
16300 +#endif
16301 +}
16302 +
16303 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16304 +{
16305 +#ifdef CONFIG_PROC_FS
16306 +       return sb->s_magic == PROC_SUPER_MAGIC;
16307 +#else
16308 +       return 0;
16309 +#endif
16310 +}
16311 +
16312 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16313 +{
16314 +#ifdef CONFIG_SYSFS
16315 +       return sb->s_magic == SYSFS_MAGIC;
16316 +#else
16317 +       return 0;
16318 +#endif
16319 +}
16320 +
16321 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16322 +{
16323 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16324 +       return sb->s_magic == CONFIGFS_MAGIC;
16325 +#else
16326 +       return 0;
16327 +#endif
16328 +}
16329 +
16330 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16331 +{
16332 +#if IS_ENABLED(CONFIG_MINIX_FS)
16333 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16334 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16335 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16336 +               || sb->s_magic == MINIX_SUPER_MAGIC
16337 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16338 +#else
16339 +       return 0;
16340 +#endif
16341 +}
16342 +
16343 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16344 +{
16345 +#if IS_ENABLED(CONFIG_FAT_FS)
16346 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16347 +#else
16348 +       return 0;
16349 +#endif
16350 +}
16351 +
16352 +static inline int au_test_msdos(struct super_block *sb)
16353 +{
16354 +       return au_test_fat(sb);
16355 +}
16356 +
16357 +static inline int au_test_vfat(struct super_block *sb)
16358 +{
16359 +       return au_test_fat(sb);
16360 +}
16361 +
16362 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16363 +{
16364 +#ifdef CONFIG_SECURITYFS
16365 +       return sb->s_magic == SECURITYFS_MAGIC;
16366 +#else
16367 +       return 0;
16368 +#endif
16369 +}
16370 +
16371 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16372 +{
16373 +#if IS_ENABLED(CONFIG_SQUASHFS)
16374 +       return sb->s_magic == SQUASHFS_MAGIC;
16375 +#else
16376 +       return 0;
16377 +#endif
16378 +}
16379 +
16380 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16381 +{
16382 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16383 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16384 +#else
16385 +       return 0;
16386 +#endif
16387 +}
16388 +
16389 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16390 +{
16391 +#if IS_ENABLED(CONFIG_XENFS)
16392 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16393 +#else
16394 +       return 0;
16395 +#endif
16396 +}
16397 +
16398 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16399 +{
16400 +#ifdef CONFIG_DEBUG_FS
16401 +       return sb->s_magic == DEBUGFS_MAGIC;
16402 +#else
16403 +       return 0;
16404 +#endif
16405 +}
16406 +
16407 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16408 +{
16409 +#if IS_ENABLED(CONFIG_NILFS)
16410 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16411 +#else
16412 +       return 0;
16413 +#endif
16414 +}
16415 +
16416 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16417 +{
16418 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16419 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16420 +#else
16421 +       return 0;
16422 +#endif
16423 +}
16424 +
16425 +/* ---------------------------------------------------------------------- */
16426 +/*
16427 + * they can't be an aufs branch.
16428 + */
16429 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16430 +{
16431 +       return
16432 +#ifndef CONFIG_AUFS_BR_RAMFS
16433 +               au_test_ramfs(sb) ||
16434 +#endif
16435 +               au_test_procfs(sb)
16436 +               || au_test_sysfs(sb)
16437 +               || au_test_configfs(sb)
16438 +               || au_test_debugfs(sb)
16439 +               || au_test_securityfs(sb)
16440 +               || au_test_xenfs(sb)
16441 +               || au_test_ecryptfs(sb)
16442 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16443 +               || au_test_aufs(sb); /* will be supported in next version */
16444 +}
16445 +
16446 +static inline int au_test_fs_remote(struct super_block *sb)
16447 +{
16448 +       return !au_test_tmpfs(sb)
16449 +#ifdef CONFIG_AUFS_BR_RAMFS
16450 +               && !au_test_ramfs(sb)
16451 +#endif
16452 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16453 +}
16454 +
16455 +/* ---------------------------------------------------------------------- */
16456 +
16457 +/*
16458 + * Note: these functions (below) are created after reading ->getattr() in all
16459 + * filesystems under linux/fs. it means we have to do so in every update...
16460 + */
16461 +
16462 +/*
16463 + * some filesystems require getattr to refresh the inode attributes before
16464 + * referencing.
16465 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16466 + * and leave the work for d_revalidate()
16467 + */
16468 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16469 +{
16470 +       return au_test_nfs(sb)
16471 +               || au_test_fuse(sb)
16472 +               /* || au_test_btrfs(sb) */      /* untested */
16473 +               ;
16474 +}
16475 +
16476 +/*
16477 + * filesystems which don't maintain i_size or i_blocks.
16478 + */
16479 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16480 +{
16481 +       return au_test_xfs(sb)
16482 +               || au_test_btrfs(sb)
16483 +               || au_test_ubifs(sb)
16484 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16485 +               /* || au_test_minix(sb) */      /* untested */
16486 +               ;
16487 +}
16488 +
16489 +/*
16490 + * filesystems which don't store the correct value in some of their inode
16491 + * attributes.
16492 + */
16493 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16494 +{
16495 +       return au_test_fs_bad_iattr_size(sb)
16496 +               || au_test_fat(sb)
16497 +               || au_test_msdos(sb)
16498 +               || au_test_vfat(sb);
16499 +}
16500 +
16501 +/* they don't check i_nlink in link(2) */
16502 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16503 +{
16504 +       return au_test_tmpfs(sb)
16505 +#ifdef CONFIG_AUFS_BR_RAMFS
16506 +               || au_test_ramfs(sb)
16507 +#endif
16508 +               || au_test_ubifs(sb)
16509 +               || au_test_hfsplus(sb);
16510 +}
16511 +
16512 +/*
16513 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16514 + */
16515 +static inline int au_test_fs_notime(struct super_block *sb)
16516 +{
16517 +       return au_test_nfs(sb)
16518 +               || au_test_fuse(sb)
16519 +               || au_test_ubifs(sb)
16520 +               ;
16521 +}
16522 +
16523 +/* temporary support for i#1 in cramfs */
16524 +static inline int au_test_fs_unique_ino(struct inode *inode)
16525 +{
16526 +       if (au_test_cramfs(inode->i_sb))
16527 +               return inode->i_ino != 1;
16528 +       return 1;
16529 +}
16530 +
16531 +/* ---------------------------------------------------------------------- */
16532 +
16533 +/*
16534 + * the filesystem where the xino files placed must support i/o after unlink and
16535 + * maintain i_size and i_blocks.
16536 + */
16537 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16538 +{
16539 +       return au_test_fs_remote(sb)
16540 +               || au_test_fs_bad_iattr_size(sb)
16541 +               /* don't want unnecessary work for xino */
16542 +               || au_test_aufs(sb)
16543 +               || au_test_ecryptfs(sb)
16544 +               || au_test_nilfs(sb);
16545 +}
16546 +
16547 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16548 +{
16549 +       return au_test_tmpfs(sb)
16550 +               || au_test_ramfs(sb);
16551 +}
16552 +
16553 +/*
16554 + * test if the @sb is real-readonly.
16555 + */
16556 +static inline int au_test_fs_rr(struct super_block *sb)
16557 +{
16558 +       return au_test_squashfs(sb)
16559 +               || au_test_iso9660(sb)
16560 +               || au_test_cramfs(sb)
16561 +               || au_test_romfs(sb);
16562 +}
16563 +
16564 +/*
16565 + * test if the @inode is nfs with 'noacl' option
16566 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16567 + */
16568 +static inline int au_test_nfs_noacl(struct inode *inode)
16569 +{
16570 +       return au_test_nfs(inode->i_sb)
16571 +               /* && IS_POSIXACL(inode) */
16572 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16573 +}
16574 +
16575 +#endif /* __KERNEL__ */
16576 +#endif /* __AUFS_FSTYPE_H__ */
16577 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16578 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16579 +++ linux/fs/aufs/hbl.h 2021-12-03 15:38:59.936647310 +0100
16580 @@ -0,0 +1,65 @@
16581 +/* SPDX-License-Identifier: GPL-2.0 */
16582 +/*
16583 + * Copyright (C) 2017-2021 Junjiro R. Okajima
16584 + *
16585 + * This program, aufs is free software; you can redistribute it and/or modify
16586 + * it under the terms of the GNU General Public License as published by
16587 + * the Free Software Foundation; either version 2 of the License, or
16588 + * (at your option) any later version.
16589 + *
16590 + * This program is distributed in the hope that it will be useful,
16591 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16592 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16593 + * GNU General Public License for more details.
16594 + *
16595 + * You should have received a copy of the GNU General Public License
16596 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16597 + */
16598 +
16599 +/*
16600 + * helpers for hlist_bl.h
16601 + */
16602 +
16603 +#ifndef __AUFS_HBL_H__
16604 +#define __AUFS_HBL_H__
16605 +
16606 +#ifdef __KERNEL__
16607 +
16608 +#include <linux/list_bl.h>
16609 +
16610 +static inline void au_hbl_add(struct hlist_bl_node *node,
16611 +                             struct hlist_bl_head *hbl)
16612 +{
16613 +       hlist_bl_lock(hbl);
16614 +       hlist_bl_add_head(node, hbl);
16615 +       hlist_bl_unlock(hbl);
16616 +}
16617 +
16618 +static inline void au_hbl_del(struct hlist_bl_node *node,
16619 +                             struct hlist_bl_head *hbl)
16620 +{
16621 +       hlist_bl_lock(hbl);
16622 +       hlist_bl_del(node);
16623 +       hlist_bl_unlock(hbl);
16624 +}
16625 +
16626 +#define au_hbl_for_each(pos, head)                                     \
16627 +       for (pos = hlist_bl_first(head);                                \
16628 +            pos;                                                       \
16629 +            pos = pos->next)
16630 +
16631 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16632 +{
16633 +       unsigned long cnt;
16634 +       struct hlist_bl_node *pos;
16635 +
16636 +       cnt = 0;
16637 +       hlist_bl_lock(hbl);
16638 +       au_hbl_for_each(pos, hbl)
16639 +               cnt++;
16640 +       hlist_bl_unlock(hbl);
16641 +       return cnt;
16642 +}
16643 +
16644 +#endif /* __KERNEL__ */
16645 +#endif /* __AUFS_HBL_H__ */
16646 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16647 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16648 +++ linux/fs/aufs/hfsnotify.c   2022-01-13 12:42:15.929566135 +0100
16649 @@ -0,0 +1,289 @@
16650 +// SPDX-License-Identifier: GPL-2.0
16651 +/*
16652 + * Copyright (C) 2005-2021 Junjiro R. Okajima
16653 + *
16654 + * This program, aufs is free software; you can redistribute it and/or modify
16655 + * it under the terms of the GNU General Public License as published by
16656 + * the Free Software Foundation; either version 2 of the License, or
16657 + * (at your option) any later version.
16658 + *
16659 + * This program is distributed in the hope that it will be useful,
16660 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16661 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16662 + * GNU General Public License for more details.
16663 + *
16664 + * You should have received a copy of the GNU General Public License
16665 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16666 + */
16667 +
16668 +/*
16669 + * fsnotify for the lower directories
16670 + */
16671 +
16672 +#include "aufs.h"
16673 +
16674 +/* FS_IN_IGNORED is unnecessary */
16675 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16676 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16677 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16678 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16679 +
16680 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16681 +{
16682 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16683 +                                            hn_mark);
16684 +       /* AuDbg("here\n"); */
16685 +       au_cache_free_hnotify(hn);
16686 +       smp_mb__before_atomic(); /* for atomic64_dec */
16687 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16688 +               wake_up(&au_hfsn_wq);
16689 +}
16690 +
16691 +static int au_hfsn_alloc(struct au_hinode *hinode)
16692 +{
16693 +       int err;
16694 +       struct au_hnotify *hn;
16695 +       struct super_block *sb;
16696 +       struct au_branch *br;
16697 +       struct fsnotify_mark *mark;
16698 +       aufs_bindex_t bindex;
16699 +
16700 +       hn = hinode->hi_notify;
16701 +       sb = hn->hn_aufs_inode->i_sb;
16702 +       bindex = au_br_index(sb, hinode->hi_id);
16703 +       br = au_sbr(sb, bindex);
16704 +       AuDebugOn(!br->br_hfsn);
16705 +
16706 +       mark = &hn->hn_mark;
16707 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16708 +       mark->mask = AuHfsnMask;
16709 +       /*
16710 +        * by udba rename or rmdir, aufs assign a new inode to the known
16711 +        * h_inode, so specify 1 to allow dups.
16712 +        */
16713 +       lockdep_off();
16714 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16715 +       lockdep_on();
16716 +
16717 +       return err;
16718 +}
16719 +
16720 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16721 +{
16722 +       struct fsnotify_mark *mark;
16723 +       unsigned long long ull;
16724 +       struct fsnotify_group *group;
16725 +
16726 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16727 +       BUG_ON(!ull);
16728 +
16729 +       mark = &hn->hn_mark;
16730 +       spin_lock(&mark->lock);
16731 +       group = mark->group;
16732 +       fsnotify_get_group(group);
16733 +       spin_unlock(&mark->lock);
16734 +       lockdep_off();
16735 +       fsnotify_destroy_mark(mark, group);
16736 +       fsnotify_put_mark(mark);
16737 +       fsnotify_put_group(group);
16738 +       lockdep_on();
16739 +
16740 +       /* free hn by myself */
16741 +       return 0;
16742 +}
16743 +
16744 +/* ---------------------------------------------------------------------- */
16745 +
16746 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16747 +{
16748 +       struct fsnotify_mark *mark;
16749 +
16750 +       mark = &hinode->hi_notify->hn_mark;
16751 +       spin_lock(&mark->lock);
16752 +       if (do_set) {
16753 +               AuDebugOn(mark->mask & AuHfsnMask);
16754 +               mark->mask |= AuHfsnMask;
16755 +       } else {
16756 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16757 +               mark->mask &= ~AuHfsnMask;
16758 +       }
16759 +       spin_unlock(&mark->lock);
16760 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16761 +}
16762 +
16763 +/* ---------------------------------------------------------------------- */
16764 +
16765 +/* #define AuDbgHnotify */
16766 +#ifdef AuDbgHnotify
16767 +static char *au_hfsn_name(u32 mask)
16768 +{
16769 +#ifdef CONFIG_AUFS_DEBUG
16770 +#define test_ret(flag)                         \
16771 +       do {                                    \
16772 +               if (mask & flag)                \
16773 +                       return #flag;           \
16774 +       } while (0)
16775 +       test_ret(FS_ACCESS);
16776 +       test_ret(FS_MODIFY);
16777 +       test_ret(FS_ATTRIB);
16778 +       test_ret(FS_CLOSE_WRITE);
16779 +       test_ret(FS_CLOSE_NOWRITE);
16780 +       test_ret(FS_OPEN);
16781 +       test_ret(FS_MOVED_FROM);
16782 +       test_ret(FS_MOVED_TO);
16783 +       test_ret(FS_CREATE);
16784 +       test_ret(FS_DELETE);
16785 +       test_ret(FS_DELETE_SELF);
16786 +       test_ret(FS_MOVE_SELF);
16787 +       test_ret(FS_UNMOUNT);
16788 +       test_ret(FS_Q_OVERFLOW);
16789 +       test_ret(FS_IN_IGNORED);
16790 +       test_ret(FS_ISDIR);
16791 +       test_ret(FS_IN_ONESHOT);
16792 +       test_ret(FS_EVENT_ON_CHILD);
16793 +       return "";
16794 +#undef test_ret
16795 +#else
16796 +       return "??";
16797 +#endif
16798 +}
16799 +#endif
16800 +
16801 +/* ---------------------------------------------------------------------- */
16802 +
16803 +static void au_hfsn_free_group(struct fsnotify_group *group)
16804 +{
16805 +       struct au_br_hfsnotify *hfsn = group->private;
16806 +
16807 +       /* AuDbg("here\n"); */
16808 +       au_kfree_try_rcu(hfsn);
16809 +}
16810 +
16811 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16812 +                               u32 mask, const void *data, int data_type,
16813 +                               struct inode *dir,
16814 +                               const struct qstr *file_name, u32 cookie,
16815 +                               struct fsnotify_iter_info *iter_info)
16816 +{
16817 +       int err;
16818 +       struct au_hnotify *hnotify;
16819 +       struct inode *h_dir, *h_inode;
16820 +       struct fsnotify_mark *inode_mark;
16821 +
16822 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
16823 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
16824 +
16825 +       err = 0;
16826 +       /* if FS_UNMOUNT happens, there must be another bug */
16827 +       AuDebugOn(mask & FS_UNMOUNT);
16828 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16829 +               goto out;
16830 +
16831 +       h_dir = dir;
16832 +       h_inode = NULL;
16833 +#ifdef AuDbgHnotify
16834 +       au_debug_on();
16835 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16836 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16837 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16838 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16839 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16840 +               /* WARN_ON(1); */
16841 +       }
16842 +       au_debug_off();
16843 +#endif
16844 +
16845 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16846 +       AuDebugOn(!inode_mark);
16847 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16848 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16849 +
16850 +out:
16851 +       return err;
16852 +}
16853 +
16854 +static struct fsnotify_ops au_hfsn_ops = {
16855 +       .handle_event           = au_hfsn_handle_event,
16856 +       .free_group_priv        = au_hfsn_free_group,
16857 +       .free_mark              = au_hfsn_free_mark
16858 +};
16859 +
16860 +/* ---------------------------------------------------------------------- */
16861 +
16862 +static void au_hfsn_fin_br(struct au_branch *br)
16863 +{
16864 +       struct au_br_hfsnotify *hfsn;
16865 +
16866 +       hfsn = br->br_hfsn;
16867 +       if (hfsn) {
16868 +               lockdep_off();
16869 +               fsnotify_put_group(hfsn->hfsn_group);
16870 +               lockdep_on();
16871 +       }
16872 +}
16873 +
16874 +static int au_hfsn_init_br(struct au_branch *br, int perm)
16875 +{
16876 +       int err;
16877 +       struct fsnotify_group *group;
16878 +       struct au_br_hfsnotify *hfsn;
16879 +
16880 +       err = 0;
16881 +       br->br_hfsn = NULL;
16882 +       if (!au_br_hnotifyable(perm))
16883 +               goto out;
16884 +
16885 +       err = -ENOMEM;
16886 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16887 +       if (unlikely(!hfsn))
16888 +               goto out;
16889 +
16890 +       err = 0;
16891 +       group = fsnotify_alloc_group(&au_hfsn_ops);
16892 +       if (IS_ERR(group)) {
16893 +               err = PTR_ERR(group);
16894 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
16895 +               goto out_hfsn;
16896 +       }
16897 +
16898 +       group->private = hfsn;
16899 +       hfsn->hfsn_group = group;
16900 +       br->br_hfsn = hfsn;
16901 +       goto out; /* success */
16902 +
16903 +out_hfsn:
16904 +       au_kfree_try_rcu(hfsn);
16905 +out:
16906 +       return err;
16907 +}
16908 +
16909 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16910 +{
16911 +       int err;
16912 +
16913 +       err = 0;
16914 +       if (!br->br_hfsn)
16915 +               err = au_hfsn_init_br(br, perm);
16916 +
16917 +       return err;
16918 +}
16919 +
16920 +/* ---------------------------------------------------------------------- */
16921 +
16922 +static void au_hfsn_fin(void)
16923 +{
16924 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16925 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16926 +}
16927 +
16928 +const struct au_hnotify_op au_hnotify_op = {
16929 +       .ctl            = au_hfsn_ctl,
16930 +       .alloc          = au_hfsn_alloc,
16931 +       .free           = au_hfsn_free,
16932 +
16933 +       .fin            = au_hfsn_fin,
16934 +
16935 +       .reset_br       = au_hfsn_reset_br,
16936 +       .fin_br         = au_hfsn_fin_br,
16937 +       .init_br        = au_hfsn_init_br
16938 +};
16939 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16940 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
16941 +++ linux/fs/aufs/hfsplus.c     2021-12-03 15:38:59.936647310 +0100
16942 @@ -0,0 +1,60 @@
16943 +// SPDX-License-Identifier: GPL-2.0
16944 +/*
16945 + * Copyright (C) 2010-2021 Junjiro R. Okajima
16946 + *
16947 + * This program, aufs is free software; you can redistribute it and/or modify
16948 + * it under the terms of the GNU General Public License as published by
16949 + * the Free Software Foundation; either version 2 of the License, or
16950 + * (at your option) any later version.
16951 + *
16952 + * This program is distributed in the hope that it will be useful,
16953 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16954 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16955 + * GNU General Public License for more details.
16956 + *
16957 + * You should have received a copy of the GNU General Public License
16958 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16959 + */
16960 +
16961 +/*
16962 + * special support for filesystems which acquires an inode mutex
16963 + * at final closing a file, eg, hfsplus.
16964 + *
16965 + * This trick is very simple and stupid, just to open the file before really
16966 + * necessary open to tell hfsplus that this is not the final closing.
16967 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
16968 + * and au_h_open_post() after releasing it.
16969 + */
16970 +
16971 +#include "aufs.h"
16972 +
16973 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
16974 +                          int force_wr)
16975 +{
16976 +       struct file *h_file;
16977 +       struct dentry *h_dentry;
16978 +
16979 +       h_dentry = au_h_dptr(dentry, bindex);
16980 +       AuDebugOn(!h_dentry);
16981 +       AuDebugOn(d_is_negative(h_dentry));
16982 +
16983 +       h_file = NULL;
16984 +       if (au_test_hfsplus(h_dentry->d_sb)
16985 +           && d_is_reg(h_dentry))
16986 +               h_file = au_h_open(dentry, bindex,
16987 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
16988 +                                  /*file*/NULL, force_wr);
16989 +       return h_file;
16990 +}
16991 +
16992 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
16993 +                   struct file *h_file)
16994 +{
16995 +       struct au_branch *br;
16996 +
16997 +       if (h_file) {
16998 +               fput(h_file);
16999 +               br = au_sbr(dentry->d_sb, bindex);
17000 +               au_lcnt_dec(&br->br_nfiles);
17001 +       }
17002 +}
17003 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17004 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17005 +++ linux/fs/aufs/hnotify.c     2021-12-03 15:38:59.936647310 +0100
17006 @@ -0,0 +1,715 @@
17007 +// SPDX-License-Identifier: GPL-2.0
17008 +/*
17009 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17010 + *
17011 + * This program, aufs is free software; you can redistribute it and/or modify
17012 + * it under the terms of the GNU General Public License as published by
17013 + * the Free Software Foundation; either version 2 of the License, or
17014 + * (at your option) any later version.
17015 + *
17016 + * This program is distributed in the hope that it will be useful,
17017 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17018 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17019 + * GNU General Public License for more details.
17020 + *
17021 + * You should have received a copy of the GNU General Public License
17022 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17023 + */
17024 +
17025 +/*
17026 + * abstraction to notify the direct changes on lower directories
17027 + */
17028 +
17029 +/* #include <linux/iversion.h> */
17030 +#include "aufs.h"
17031 +
17032 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17033 +{
17034 +       int err;
17035 +       struct au_hnotify *hn;
17036 +
17037 +       err = -ENOMEM;
17038 +       hn = au_cache_alloc_hnotify();
17039 +       if (hn) {
17040 +               hn->hn_aufs_inode = inode;
17041 +               hinode->hi_notify = hn;
17042 +               err = au_hnotify_op.alloc(hinode);
17043 +               AuTraceErr(err);
17044 +               if (unlikely(err)) {
17045 +                       hinode->hi_notify = NULL;
17046 +                       au_cache_free_hnotify(hn);
17047 +                       /*
17048 +                        * The upper dir was removed by udba, but the same named
17049 +                        * dir left. In this case, aufs assigns a new inode
17050 +                        * number and set the monitor again.
17051 +                        * For the lower dir, the old monitor is still left.
17052 +                        */
17053 +                       if (err == -EEXIST)
17054 +                               err = 0;
17055 +               }
17056 +       }
17057 +
17058 +       AuTraceErr(err);
17059 +       return err;
17060 +}
17061 +
17062 +void au_hn_free(struct au_hinode *hinode)
17063 +{
17064 +       struct au_hnotify *hn;
17065 +
17066 +       hn = hinode->hi_notify;
17067 +       if (hn) {
17068 +               hinode->hi_notify = NULL;
17069 +               if (au_hnotify_op.free(hinode, hn))
17070 +                       au_cache_free_hnotify(hn);
17071 +       }
17072 +}
17073 +
17074 +/* ---------------------------------------------------------------------- */
17075 +
17076 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17077 +{
17078 +       if (hinode->hi_notify)
17079 +               au_hnotify_op.ctl(hinode, do_set);
17080 +}
17081 +
17082 +void au_hn_reset(struct inode *inode, unsigned int flags)
17083 +{
17084 +       aufs_bindex_t bindex, bbot;
17085 +       struct inode *hi;
17086 +       struct dentry *iwhdentry;
17087 +
17088 +       bbot = au_ibbot(inode);
17089 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17090 +               hi = au_h_iptr(inode, bindex);
17091 +               if (!hi)
17092 +                       continue;
17093 +
17094 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17095 +               iwhdentry = au_hi_wh(inode, bindex);
17096 +               if (iwhdentry)
17097 +                       dget(iwhdentry);
17098 +               au_igrab(hi);
17099 +               au_set_h_iptr(inode, bindex, NULL, 0);
17100 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17101 +                             flags & ~AuHi_XINO);
17102 +               iput(hi);
17103 +               dput(iwhdentry);
17104 +               /* inode_unlock(hi); */
17105 +       }
17106 +}
17107 +
17108 +/* ---------------------------------------------------------------------- */
17109 +
17110 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17111 +{
17112 +       int err;
17113 +       aufs_bindex_t bindex, bbot, bfound, btop;
17114 +       struct inode *h_i;
17115 +
17116 +       err = 0;
17117 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17118 +               pr_warn("branch root dir was changed\n");
17119 +               goto out;
17120 +       }
17121 +
17122 +       bfound = -1;
17123 +       bbot = au_ibbot(inode);
17124 +       btop = au_ibtop(inode);
17125 +#if 0 /* reserved for future use */
17126 +       if (bindex == bbot) {
17127 +               /* keep this ino in rename case */
17128 +               goto out;
17129 +       }
17130 +#endif
17131 +       for (bindex = btop; bindex <= bbot; bindex++)
17132 +               if (au_h_iptr(inode, bindex) == h_inode) {
17133 +                       bfound = bindex;
17134 +                       break;
17135 +               }
17136 +       if (bfound < 0)
17137 +               goto out;
17138 +
17139 +       for (bindex = btop; bindex <= bbot; bindex++) {
17140 +               h_i = au_h_iptr(inode, bindex);
17141 +               if (!h_i)
17142 +                       continue;
17143 +
17144 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17145 +               /* ignore this error */
17146 +               /* bad action? */
17147 +       }
17148 +
17149 +       /* children inode number will be broken */
17150 +
17151 +out:
17152 +       AuTraceErr(err);
17153 +       return err;
17154 +}
17155 +
17156 +static int hn_gen_tree(struct dentry *dentry)
17157 +{
17158 +       int err, i, j, ndentry;
17159 +       struct au_dcsub_pages dpages;
17160 +       struct au_dpage *dpage;
17161 +       struct dentry **dentries;
17162 +
17163 +       err = au_dpages_init(&dpages, GFP_NOFS);
17164 +       if (unlikely(err))
17165 +               goto out;
17166 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17167 +       if (unlikely(err))
17168 +               goto out_dpages;
17169 +
17170 +       for (i = 0; i < dpages.ndpage; i++) {
17171 +               dpage = dpages.dpages + i;
17172 +               dentries = dpage->dentries;
17173 +               ndentry = dpage->ndentry;
17174 +               for (j = 0; j < ndentry; j++) {
17175 +                       struct dentry *d;
17176 +
17177 +                       d = dentries[j];
17178 +                       if (IS_ROOT(d))
17179 +                               continue;
17180 +
17181 +                       au_digen_dec(d);
17182 +                       if (d_really_is_positive(d))
17183 +                               /* todo: reset children xino?
17184 +                                  cached children only? */
17185 +                               au_iigen_dec(d_inode(d));
17186 +               }
17187 +       }
17188 +
17189 +out_dpages:
17190 +       au_dpages_free(&dpages);
17191 +out:
17192 +       return err;
17193 +}
17194 +
17195 +/*
17196 + * return 0 if processed.
17197 + */
17198 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17199 +                          const unsigned int isdir)
17200 +{
17201 +       int err;
17202 +       struct dentry *d;
17203 +       struct qstr *dname;
17204 +
17205 +       err = 1;
17206 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17207 +               pr_warn("branch root dir was changed\n");
17208 +               err = 0;
17209 +               goto out;
17210 +       }
17211 +
17212 +       if (!isdir) {
17213 +               AuDebugOn(!name);
17214 +               au_iigen_dec(inode);
17215 +               spin_lock(&inode->i_lock);
17216 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17217 +                       spin_lock(&d->d_lock);
17218 +                       dname = &d->d_name;
17219 +                       if (dname->len != nlen
17220 +                           && memcmp(dname->name, name, nlen)) {
17221 +                               spin_unlock(&d->d_lock);
17222 +                               continue;
17223 +                       }
17224 +                       err = 0;
17225 +                       au_digen_dec(d);
17226 +                       spin_unlock(&d->d_lock);
17227 +                       break;
17228 +               }
17229 +               spin_unlock(&inode->i_lock);
17230 +       } else {
17231 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17232 +               d = d_find_any_alias(inode);
17233 +               if (!d) {
17234 +                       au_iigen_dec(inode);
17235 +                       goto out;
17236 +               }
17237 +
17238 +               spin_lock(&d->d_lock);
17239 +               dname = &d->d_name;
17240 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17241 +                       spin_unlock(&d->d_lock);
17242 +                       err = hn_gen_tree(d);
17243 +                       spin_lock(&d->d_lock);
17244 +               }
17245 +               spin_unlock(&d->d_lock);
17246 +               dput(d);
17247 +       }
17248 +
17249 +out:
17250 +       AuTraceErr(err);
17251 +       return err;
17252 +}
17253 +
17254 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17255 +{
17256 +       int err;
17257 +
17258 +       if (IS_ROOT(dentry)) {
17259 +               pr_warn("branch root dir was changed\n");
17260 +               return 0;
17261 +       }
17262 +
17263 +       err = 0;
17264 +       if (!isdir) {
17265 +               au_digen_dec(dentry);
17266 +               if (d_really_is_positive(dentry))
17267 +                       au_iigen_dec(d_inode(dentry));
17268 +       } else {
17269 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17270 +               if (d_really_is_positive(dentry))
17271 +                       err = hn_gen_tree(dentry);
17272 +       }
17273 +
17274 +       AuTraceErr(err);
17275 +       return err;
17276 +}
17277 +
17278 +/* ---------------------------------------------------------------------- */
17279 +
17280 +/* hnotify job flags */
17281 +#define AuHnJob_XINO0          1
17282 +#define AuHnJob_GEN            (1 << 1)
17283 +#define AuHnJob_DIRENT         (1 << 2)
17284 +#define AuHnJob_ISDIR          (1 << 3)
17285 +#define AuHnJob_TRYXINO0       (1 << 4)
17286 +#define AuHnJob_MNTPNT         (1 << 5)
17287 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17288 +#define au_fset_hnjob(flags, name) \
17289 +       do { (flags) |= AuHnJob_##name; } while (0)
17290 +#define au_fclr_hnjob(flags, name) \
17291 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17292 +
17293 +enum {
17294 +       AuHn_CHILD,
17295 +       AuHn_PARENT,
17296 +       AuHnLast
17297 +};
17298 +
17299 +struct au_hnotify_args {
17300 +       struct inode *h_dir, *dir, *h_child_inode;
17301 +       u32 mask;
17302 +       unsigned int flags[AuHnLast];
17303 +       unsigned int h_child_nlen;
17304 +       char h_child_name[];
17305 +};
17306 +
17307 +struct hn_job_args {
17308 +       unsigned int flags;
17309 +       struct inode *inode, *h_inode, *dir, *h_dir;
17310 +       struct dentry *dentry;
17311 +       char *h_name;
17312 +       int h_nlen;
17313 +};
17314 +
17315 +static int hn_job(struct hn_job_args *a)
17316 +{
17317 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17318 +       int e;
17319 +
17320 +       /* reset xino */
17321 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17322 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17323 +
17324 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17325 +           && a->inode
17326 +           && a->h_inode) {
17327 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17328 +               if (!a->h_inode->i_nlink
17329 +                   && !(a->h_inode->i_state & I_LINKABLE))
17330 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17331 +               inode_unlock_shared(a->h_inode);
17332 +       }
17333 +
17334 +       /* make the generation obsolete */
17335 +       if (au_ftest_hnjob(a->flags, GEN)) {
17336 +               e = -1;
17337 +               if (a->inode)
17338 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17339 +                                             isdir);
17340 +               if (e && a->dentry)
17341 +                       hn_gen_by_name(a->dentry, isdir);
17342 +               /* ignore this error */
17343 +       }
17344 +
17345 +       /* make dir entries obsolete */
17346 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17347 +               struct au_vdir *vdir;
17348 +
17349 +               vdir = au_ivdir(a->inode);
17350 +               if (vdir)
17351 +                       vdir->vd_jiffy = 0;
17352 +               /* IMustLock(a->inode); */
17353 +               /* inode_inc_iversion(a->inode); */
17354 +       }
17355 +
17356 +       /* can do nothing but warn */
17357 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17358 +           && a->dentry
17359 +           && d_mountpoint(a->dentry))
17360 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17361 +
17362 +       return 0;
17363 +}
17364 +
17365 +/* ---------------------------------------------------------------------- */
17366 +
17367 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17368 +                                          struct inode *dir)
17369 +{
17370 +       struct dentry *dentry, *d, *parent;
17371 +       struct qstr *dname;
17372 +
17373 +       parent = d_find_any_alias(dir);
17374 +       if (!parent)
17375 +               return NULL;
17376 +
17377 +       dentry = NULL;
17378 +       spin_lock(&parent->d_lock);
17379 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17380 +               /* AuDbg("%pd\n", d); */
17381 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17382 +               dname = &d->d_name;
17383 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17384 +                       goto cont_unlock;
17385 +               if (au_di(d))
17386 +                       au_digen_dec(d);
17387 +               else
17388 +                       goto cont_unlock;
17389 +               if (au_dcount(d) > 0) {
17390 +                       dentry = dget_dlock(d);
17391 +                       spin_unlock(&d->d_lock);
17392 +                       break;
17393 +               }
17394 +
17395 +cont_unlock:
17396 +               spin_unlock(&d->d_lock);
17397 +       }
17398 +       spin_unlock(&parent->d_lock);
17399 +       dput(parent);
17400 +
17401 +       if (dentry)
17402 +               di_write_lock_child(dentry);
17403 +
17404 +       return dentry;
17405 +}
17406 +
17407 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17408 +                                        aufs_bindex_t bindex, ino_t h_ino)
17409 +{
17410 +       struct inode *inode;
17411 +       ino_t ino;
17412 +       int err;
17413 +
17414 +       inode = NULL;
17415 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17416 +       if (!err && ino)
17417 +               inode = ilookup(sb, ino);
17418 +       if (!inode)
17419 +               goto out;
17420 +
17421 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17422 +               pr_warn("wrong root branch\n");
17423 +               iput(inode);
17424 +               inode = NULL;
17425 +               goto out;
17426 +       }
17427 +
17428 +       ii_write_lock_child(inode);
17429 +
17430 +out:
17431 +       return inode;
17432 +}
17433 +
17434 +static void au_hn_bh(void *_args)
17435 +{
17436 +       struct au_hnotify_args *a = _args;
17437 +       struct super_block *sb;
17438 +       aufs_bindex_t bindex, bbot, bfound;
17439 +       unsigned char xino, try_iput;
17440 +       int err;
17441 +       struct inode *inode;
17442 +       ino_t h_ino;
17443 +       struct hn_job_args args;
17444 +       struct dentry *dentry;
17445 +       struct au_sbinfo *sbinfo;
17446 +
17447 +       AuDebugOn(!_args);
17448 +       AuDebugOn(!a->h_dir);
17449 +       AuDebugOn(!a->dir);
17450 +       AuDebugOn(!a->mask);
17451 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17452 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17453 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17454 +
17455 +       inode = NULL;
17456 +       dentry = NULL;
17457 +       /*
17458 +        * do not lock a->dir->i_mutex here
17459 +        * because of d_revalidate() may cause a deadlock.
17460 +        */
17461 +       sb = a->dir->i_sb;
17462 +       AuDebugOn(!sb);
17463 +       sbinfo = au_sbi(sb);
17464 +       AuDebugOn(!sbinfo);
17465 +       si_write_lock(sb, AuLock_NOPLMW);
17466 +
17467 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17468 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17469 +               case FS_MOVED_FROM:
17470 +               case FS_MOVED_TO:
17471 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17472 +                               "for the direct rename(2)\n");
17473 +               }
17474 +
17475 +       ii_read_lock_parent(a->dir);
17476 +       bfound = -1;
17477 +       bbot = au_ibbot(a->dir);
17478 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17479 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17480 +                       bfound = bindex;
17481 +                       break;
17482 +               }
17483 +       ii_read_unlock(a->dir);
17484 +       if (unlikely(bfound < 0))
17485 +               goto out;
17486 +
17487 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17488 +       h_ino = 0;
17489 +       if (a->h_child_inode)
17490 +               h_ino = a->h_child_inode->i_ino;
17491 +
17492 +       if (a->h_child_nlen
17493 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17494 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17495 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17496 +                                             a->dir);
17497 +       try_iput = 0;
17498 +       if (dentry && d_really_is_positive(dentry))
17499 +               inode = d_inode(dentry);
17500 +       if (xino && !inode && h_ino
17501 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17502 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17503 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17504 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17505 +               try_iput = 1;
17506 +       }
17507 +
17508 +       args.flags = a->flags[AuHn_CHILD];
17509 +       args.dentry = dentry;
17510 +       args.inode = inode;
17511 +       args.h_inode = a->h_child_inode;
17512 +       args.dir = a->dir;
17513 +       args.h_dir = a->h_dir;
17514 +       args.h_name = a->h_child_name;
17515 +       args.h_nlen = a->h_child_nlen;
17516 +       err = hn_job(&args);
17517 +       if (dentry) {
17518 +               if (au_di(dentry))
17519 +                       di_write_unlock(dentry);
17520 +               dput(dentry);
17521 +       }
17522 +       if (inode && try_iput) {
17523 +               ii_write_unlock(inode);
17524 +               iput(inode);
17525 +       }
17526 +
17527 +       ii_write_lock_parent(a->dir);
17528 +       args.flags = a->flags[AuHn_PARENT];
17529 +       args.dentry = NULL;
17530 +       args.inode = a->dir;
17531 +       args.h_inode = a->h_dir;
17532 +       args.dir = NULL;
17533 +       args.h_dir = NULL;
17534 +       args.h_name = NULL;
17535 +       args.h_nlen = 0;
17536 +       err = hn_job(&args);
17537 +       ii_write_unlock(a->dir);
17538 +
17539 +out:
17540 +       iput(a->h_child_inode);
17541 +       iput(a->h_dir);
17542 +       iput(a->dir);
17543 +       si_write_unlock(sb);
17544 +       au_nwt_done(&sbinfo->si_nowait);
17545 +       au_kfree_rcu(a);
17546 +}
17547 +
17548 +/* ---------------------------------------------------------------------- */
17549 +
17550 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17551 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17552 +{
17553 +       int err, len;
17554 +       unsigned int flags[AuHnLast], f;
17555 +       unsigned char isdir, isroot, wh;
17556 +       struct inode *dir;
17557 +       struct au_hnotify_args *args;
17558 +       char *p, *h_child_name;
17559 +
17560 +       err = 0;
17561 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17562 +       dir = igrab(hnotify->hn_aufs_inode);
17563 +       if (!dir)
17564 +               goto out;
17565 +
17566 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17567 +       wh = 0;
17568 +       h_child_name = (void *)h_child_qstr->name;
17569 +       len = h_child_qstr->len;
17570 +       if (h_child_name) {
17571 +               if (len > AUFS_WH_PFX_LEN
17572 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17573 +                       h_child_name += AUFS_WH_PFX_LEN;
17574 +                       len -= AUFS_WH_PFX_LEN;
17575 +                       wh = 1;
17576 +               }
17577 +       }
17578 +
17579 +       isdir = 0;
17580 +       if (h_child_inode)
17581 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17582 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17583 +       flags[AuHn_CHILD] = 0;
17584 +       if (isdir)
17585 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17586 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17587 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17588 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17589 +       case FS_MOVED_FROM:
17590 +       case FS_MOVED_TO:
17591 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17592 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17593 +               fallthrough;
17594 +       case FS_CREATE:
17595 +               AuDebugOn(!h_child_name);
17596 +               break;
17597 +
17598 +       case FS_DELETE:
17599 +               /*
17600 +                * aufs never be able to get this child inode.
17601 +                * revalidation should be in d_revalidate()
17602 +                * by checking i_nlink, i_generation or d_unhashed().
17603 +                */
17604 +               AuDebugOn(!h_child_name);
17605 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17606 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17607 +               break;
17608 +
17609 +       default:
17610 +               AuDebugOn(1);
17611 +       }
17612 +
17613 +       if (wh)
17614 +               h_child_inode = NULL;
17615 +
17616 +       err = -ENOMEM;
17617 +       /* iput() and kfree() will be called in au_hnotify() */
17618 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17619 +       if (unlikely(!args)) {
17620 +               AuErr1("no memory\n");
17621 +               iput(dir);
17622 +               goto out;
17623 +       }
17624 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17625 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17626 +       args->mask = mask;
17627 +       args->dir = dir;
17628 +       args->h_dir = igrab(h_dir);
17629 +       if (h_child_inode)
17630 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17631 +       args->h_child_inode = h_child_inode;
17632 +       args->h_child_nlen = len;
17633 +       if (len) {
17634 +               p = (void *)args;
17635 +               p += sizeof(*args);
17636 +               memcpy(p, h_child_name, len);
17637 +               p[len] = 0;
17638 +       }
17639 +
17640 +       /* NFS fires the event for silly-renamed one from kworker */
17641 +       f = 0;
17642 +       if (!dir->i_nlink
17643 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17644 +               f = AuWkq_NEST;
17645 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17646 +       if (unlikely(err)) {
17647 +               pr_err("wkq %d\n", err);
17648 +               iput(args->h_child_inode);
17649 +               iput(args->h_dir);
17650 +               iput(args->dir);
17651 +               au_kfree_rcu(args);
17652 +       }
17653 +
17654 +out:
17655 +       return err;
17656 +}
17657 +
17658 +/* ---------------------------------------------------------------------- */
17659 +
17660 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17661 +{
17662 +       int err;
17663 +
17664 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17665 +
17666 +       err = 0;
17667 +       if (au_hnotify_op.reset_br)
17668 +               err = au_hnotify_op.reset_br(udba, br, perm);
17669 +
17670 +       return err;
17671 +}
17672 +
17673 +int au_hnotify_init_br(struct au_branch *br, int perm)
17674 +{
17675 +       int err;
17676 +
17677 +       err = 0;
17678 +       if (au_hnotify_op.init_br)
17679 +               err = au_hnotify_op.init_br(br, perm);
17680 +
17681 +       return err;
17682 +}
17683 +
17684 +void au_hnotify_fin_br(struct au_branch *br)
17685 +{
17686 +       if (au_hnotify_op.fin_br)
17687 +               au_hnotify_op.fin_br(br);
17688 +}
17689 +
17690 +static void au_hn_destroy_cache(void)
17691 +{
17692 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17693 +       au_cache[AuCache_HNOTIFY] = NULL;
17694 +}
17695 +
17696 +int __init au_hnotify_init(void)
17697 +{
17698 +       int err;
17699 +
17700 +       err = -ENOMEM;
17701 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17702 +       if (au_cache[AuCache_HNOTIFY]) {
17703 +               err = 0;
17704 +               if (au_hnotify_op.init)
17705 +                       err = au_hnotify_op.init();
17706 +               if (unlikely(err))
17707 +                       au_hn_destroy_cache();
17708 +       }
17709 +       AuTraceErr(err);
17710 +       return err;
17711 +}
17712 +
17713 +void au_hnotify_fin(void)
17714 +{
17715 +       if (au_hnotify_op.fin)
17716 +               au_hnotify_op.fin();
17717 +
17718 +       /* cf. au_cache_fin() */
17719 +       if (au_cache[AuCache_HNOTIFY])
17720 +               au_hn_destroy_cache();
17721 +}
17722 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17723 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17724 +++ linux/fs/aufs/iinfo.c       2021-12-03 15:38:59.936647310 +0100
17725 @@ -0,0 +1,286 @@
17726 +// SPDX-License-Identifier: GPL-2.0
17727 +/*
17728 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17729 + *
17730 + * This program, aufs is free software; you can redistribute it and/or modify
17731 + * it under the terms of the GNU General Public License as published by
17732 + * the Free Software Foundation; either version 2 of the License, or
17733 + * (at your option) any later version.
17734 + *
17735 + * This program is distributed in the hope that it will be useful,
17736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17738 + * GNU General Public License for more details.
17739 + *
17740 + * You should have received a copy of the GNU General Public License
17741 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17742 + */
17743 +
17744 +/*
17745 + * inode private data
17746 + */
17747 +
17748 +#include "aufs.h"
17749 +
17750 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17751 +{
17752 +       struct inode *h_inode;
17753 +       struct au_hinode *hinode;
17754 +
17755 +       IiMustAnyLock(inode);
17756 +
17757 +       hinode = au_hinode(au_ii(inode), bindex);
17758 +       h_inode = hinode->hi_inode;
17759 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17760 +       return h_inode;
17761 +}
17762 +
17763 +/* todo: hard/soft set? */
17764 +void au_hiput(struct au_hinode *hinode)
17765 +{
17766 +       au_hn_free(hinode);
17767 +       dput(hinode->hi_whdentry);
17768 +       iput(hinode->hi_inode);
17769 +}
17770 +
17771 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17772 +{
17773 +       unsigned int flags;
17774 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17775 +
17776 +       flags = 0;
17777 +       if (au_opt_test(mnt_flags, XINO))
17778 +               au_fset_hi(flags, XINO);
17779 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17780 +               au_fset_hi(flags, HNOTIFY);
17781 +       return flags;
17782 +}
17783 +
17784 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17785 +                  struct inode *h_inode, unsigned int flags)
17786 +{
17787 +       struct au_hinode *hinode;
17788 +       struct inode *hi;
17789 +       struct au_iinfo *iinfo = au_ii(inode);
17790 +
17791 +       IiMustWriteLock(inode);
17792 +
17793 +       hinode = au_hinode(iinfo, bindex);
17794 +       hi = hinode->hi_inode;
17795 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17796 +
17797 +       if (hi)
17798 +               au_hiput(hinode);
17799 +       hinode->hi_inode = h_inode;
17800 +       if (h_inode) {
17801 +               int err;
17802 +               struct super_block *sb = inode->i_sb;
17803 +               struct au_branch *br;
17804 +
17805 +               AuDebugOn(inode->i_mode
17806 +                         && (h_inode->i_mode & S_IFMT)
17807 +                         != (inode->i_mode & S_IFMT));
17808 +               if (bindex == iinfo->ii_btop)
17809 +                       au_cpup_igen(inode, h_inode);
17810 +               br = au_sbr(sb, bindex);
17811 +               hinode->hi_id = br->br_id;
17812 +               if (au_ftest_hi(flags, XINO)) {
17813 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17814 +                                           inode->i_ino);
17815 +                       if (unlikely(err))
17816 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17817 +               }
17818 +
17819 +               if (au_ftest_hi(flags, HNOTIFY)
17820 +                   && au_br_hnotifyable(br->br_perm)) {
17821 +                       err = au_hn_alloc(hinode, inode);
17822 +                       if (unlikely(err))
17823 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17824 +               }
17825 +       }
17826 +}
17827 +
17828 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17829 +                 struct dentry *h_wh)
17830 +{
17831 +       struct au_hinode *hinode;
17832 +
17833 +       IiMustWriteLock(inode);
17834 +
17835 +       hinode = au_hinode(au_ii(inode), bindex);
17836 +       AuDebugOn(hinode->hi_whdentry);
17837 +       hinode->hi_whdentry = h_wh;
17838 +}
17839 +
17840 +void au_update_iigen(struct inode *inode, int half)
17841 +{
17842 +       struct au_iinfo *iinfo;
17843 +       struct au_iigen *iigen;
17844 +       unsigned int sigen;
17845 +
17846 +       sigen = au_sigen(inode->i_sb);
17847 +       iinfo = au_ii(inode);
17848 +       iigen = &iinfo->ii_generation;
17849 +       spin_lock(&iigen->ig_spin);
17850 +       iigen->ig_generation = sigen;
17851 +       if (half)
17852 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17853 +       else
17854 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17855 +       spin_unlock(&iigen->ig_spin);
17856 +}
17857 +
17858 +/* it may be called at remount time, too */
17859 +void au_update_ibrange(struct inode *inode, int do_put_zero)
17860 +{
17861 +       struct au_iinfo *iinfo;
17862 +       aufs_bindex_t bindex, bbot;
17863 +
17864 +       AuDebugOn(au_is_bad_inode(inode));
17865 +       IiMustWriteLock(inode);
17866 +
17867 +       iinfo = au_ii(inode);
17868 +       if (do_put_zero && iinfo->ii_btop >= 0) {
17869 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17870 +                    bindex++) {
17871 +                       struct inode *h_i;
17872 +
17873 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
17874 +                       if (h_i
17875 +                           && !h_i->i_nlink
17876 +                           && !(h_i->i_state & I_LINKABLE))
17877 +                               au_set_h_iptr(inode, bindex, NULL, 0);
17878 +               }
17879 +       }
17880 +
17881 +       iinfo->ii_btop = -1;
17882 +       iinfo->ii_bbot = -1;
17883 +       bbot = au_sbbot(inode->i_sb);
17884 +       for (bindex = 0; bindex <= bbot; bindex++)
17885 +               if (au_hinode(iinfo, bindex)->hi_inode) {
17886 +                       iinfo->ii_btop = bindex;
17887 +                       break;
17888 +               }
17889 +       if (iinfo->ii_btop >= 0)
17890 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17891 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
17892 +                               iinfo->ii_bbot = bindex;
17893 +                               break;
17894 +                       }
17895 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17896 +}
17897 +
17898 +/* ---------------------------------------------------------------------- */
17899 +
17900 +void au_icntnr_init_once(void *_c)
17901 +{
17902 +       struct au_icntnr *c = _c;
17903 +       struct au_iinfo *iinfo = &c->iinfo;
17904 +
17905 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
17906 +       au_rw_init(&iinfo->ii_rwsem);
17907 +       inode_init_once(&c->vfs_inode);
17908 +}
17909 +
17910 +void au_hinode_init(struct au_hinode *hinode)
17911 +{
17912 +       hinode->hi_inode = NULL;
17913 +       hinode->hi_id = -1;
17914 +       au_hn_init(hinode);
17915 +       hinode->hi_whdentry = NULL;
17916 +}
17917 +
17918 +int au_iinfo_init(struct inode *inode)
17919 +{
17920 +       struct au_iinfo *iinfo;
17921 +       struct super_block *sb;
17922 +       struct au_hinode *hi;
17923 +       int nbr, i;
17924 +
17925 +       sb = inode->i_sb;
17926 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17927 +       nbr = au_sbbot(sb) + 1;
17928 +       if (unlikely(nbr <= 0))
17929 +               nbr = 1;
17930 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17931 +       if (hi) {
17932 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17933 +
17934 +               iinfo->ii_hinode = hi;
17935 +               for (i = 0; i < nbr; i++, hi++)
17936 +                       au_hinode_init(hi);
17937 +
17938 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
17939 +               iinfo->ii_btop = -1;
17940 +               iinfo->ii_bbot = -1;
17941 +               iinfo->ii_vdir = NULL;
17942 +               return 0;
17943 +       }
17944 +       return -ENOMEM;
17945 +}
17946 +
17947 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17948 +{
17949 +       int err, i;
17950 +       struct au_hinode *hip;
17951 +
17952 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
17953 +
17954 +       err = -ENOMEM;
17955 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17956 +                         may_shrink);
17957 +       if (hip) {
17958 +               iinfo->ii_hinode = hip;
17959 +               i = iinfo->ii_bbot + 1;
17960 +               hip += i;
17961 +               for (; i < nbr; i++, hip++)
17962 +                       au_hinode_init(hip);
17963 +               err = 0;
17964 +       }
17965 +
17966 +       return err;
17967 +}
17968 +
17969 +void au_iinfo_fin(struct inode *inode)
17970 +{
17971 +       struct au_iinfo *iinfo;
17972 +       struct au_hinode *hi;
17973 +       struct super_block *sb;
17974 +       aufs_bindex_t bindex, bbot;
17975 +       const unsigned char unlinked = !inode->i_nlink;
17976 +
17977 +       AuDebugOn(au_is_bad_inode(inode));
17978 +
17979 +       sb = inode->i_sb;
17980 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
17981 +       if (si_pid_test(sb))
17982 +               au_xino_delete_inode(inode, unlinked);
17983 +       else {
17984 +               /*
17985 +                * it is safe to hide the dependency between sbinfo and
17986 +                * sb->s_umount.
17987 +                */
17988 +               lockdep_off();
17989 +               si_noflush_read_lock(sb);
17990 +               au_xino_delete_inode(inode, unlinked);
17991 +               si_read_unlock(sb);
17992 +               lockdep_on();
17993 +       }
17994 +
17995 +       iinfo = au_ii(inode);
17996 +       if (iinfo->ii_vdir)
17997 +               au_vdir_free(iinfo->ii_vdir);
17998 +
17999 +       bindex = iinfo->ii_btop;
18000 +       if (bindex >= 0) {
18001 +               hi = au_hinode(iinfo, bindex);
18002 +               bbot = iinfo->ii_bbot;
18003 +               while (bindex++ <= bbot) {
18004 +                       if (hi->hi_inode)
18005 +                               au_hiput(hi);
18006 +                       hi++;
18007 +               }
18008 +       }
18009 +       au_kfree_rcu(iinfo->ii_hinode);
18010 +       AuRwDestroy(&iinfo->ii_rwsem);
18011 +}
18012 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18013 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18014 +++ linux/fs/aufs/inode.c       2021-12-03 15:40:58.236647297 +0100
18015 @@ -0,0 +1,531 @@
18016 +// SPDX-License-Identifier: GPL-2.0
18017 +/*
18018 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18019 + *
18020 + * This program, aufs is free software; you can redistribute it and/or modify
18021 + * it under the terms of the GNU General Public License as published by
18022 + * the Free Software Foundation; either version 2 of the License, or
18023 + * (at your option) any later version.
18024 + *
18025 + * This program is distributed in the hope that it will be useful,
18026 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18027 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18028 + * GNU General Public License for more details.
18029 + *
18030 + * You should have received a copy of the GNU General Public License
18031 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18032 + */
18033 +
18034 +/*
18035 + * inode functions
18036 + */
18037 +
18038 +#include <linux/iversion.h>
18039 +#include "aufs.h"
18040 +
18041 +struct inode *au_igrab(struct inode *inode)
18042 +{
18043 +       if (inode) {
18044 +               AuDebugOn(!atomic_read(&inode->i_count));
18045 +               ihold(inode);
18046 +       }
18047 +       return inode;
18048 +}
18049 +
18050 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18051 +{
18052 +       au_cpup_attr_all(inode, /*force*/0);
18053 +       au_update_iigen(inode, /*half*/1);
18054 +       if (do_version)
18055 +               inode_inc_iversion(inode);
18056 +}
18057 +
18058 +static int au_ii_refresh(struct inode *inode, int *update)
18059 +{
18060 +       int err, e, nbr;
18061 +       umode_t type;
18062 +       aufs_bindex_t bindex, new_bindex;
18063 +       struct super_block *sb;
18064 +       struct au_iinfo *iinfo;
18065 +       struct au_hinode *p, *q, tmp;
18066 +
18067 +       AuDebugOn(au_is_bad_inode(inode));
18068 +       IiMustWriteLock(inode);
18069 +
18070 +       *update = 0;
18071 +       sb = inode->i_sb;
18072 +       nbr = au_sbbot(sb) + 1;
18073 +       type = inode->i_mode & S_IFMT;
18074 +       iinfo = au_ii(inode);
18075 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18076 +       if (unlikely(err))
18077 +               goto out;
18078 +
18079 +       AuDebugOn(iinfo->ii_btop < 0);
18080 +       p = au_hinode(iinfo, iinfo->ii_btop);
18081 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18082 +            bindex++, p++) {
18083 +               if (!p->hi_inode)
18084 +                       continue;
18085 +
18086 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18087 +               new_bindex = au_br_index(sb, p->hi_id);
18088 +               if (new_bindex == bindex)
18089 +                       continue;
18090 +
18091 +               if (new_bindex < 0) {
18092 +                       *update = 1;
18093 +                       au_hiput(p);
18094 +                       p->hi_inode = NULL;
18095 +                       continue;
18096 +               }
18097 +
18098 +               if (new_bindex < iinfo->ii_btop)
18099 +                       iinfo->ii_btop = new_bindex;
18100 +               if (iinfo->ii_bbot < new_bindex)
18101 +                       iinfo->ii_bbot = new_bindex;
18102 +               /* swap two lower inode, and loop again */
18103 +               q = au_hinode(iinfo, new_bindex);
18104 +               tmp = *q;
18105 +               *q = *p;
18106 +               *p = tmp;
18107 +               if (tmp.hi_inode) {
18108 +                       bindex--;
18109 +                       p--;
18110 +               }
18111 +       }
18112 +       au_update_ibrange(inode, /*do_put_zero*/0);
18113 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18114 +       e = au_dy_irefresh(inode);
18115 +       if (unlikely(e && !err))
18116 +               err = e;
18117 +
18118 +out:
18119 +       AuTraceErr(err);
18120 +       return err;
18121 +}
18122 +
18123 +void au_refresh_iop(struct inode *inode, int force_getattr)
18124 +{
18125 +       int type;
18126 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18127 +       const struct inode_operations *iop
18128 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18129 +
18130 +       if (inode->i_op == iop)
18131 +               return;
18132 +
18133 +       switch (inode->i_mode & S_IFMT) {
18134 +       case S_IFDIR:
18135 +               type = AuIop_DIR;
18136 +               break;
18137 +       case S_IFLNK:
18138 +               type = AuIop_SYMLINK;
18139 +               break;
18140 +       default:
18141 +               type = AuIop_OTHER;
18142 +               break;
18143 +       }
18144 +
18145 +       inode->i_op = iop + type;
18146 +       /* unnecessary smp_wmb() */
18147 +}
18148 +
18149 +int au_refresh_hinode_self(struct inode *inode)
18150 +{
18151 +       int err, update;
18152 +
18153 +       err = au_ii_refresh(inode, &update);
18154 +       if (!err)
18155 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18156 +
18157 +       AuTraceErr(err);
18158 +       return err;
18159 +}
18160 +
18161 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18162 +{
18163 +       int err, e, update;
18164 +       unsigned int flags;
18165 +       umode_t mode;
18166 +       aufs_bindex_t bindex, bbot;
18167 +       unsigned char isdir;
18168 +       struct au_hinode *p;
18169 +       struct au_iinfo *iinfo;
18170 +
18171 +       err = au_ii_refresh(inode, &update);
18172 +       if (unlikely(err))
18173 +               goto out;
18174 +
18175 +       update = 0;
18176 +       iinfo = au_ii(inode);
18177 +       p = au_hinode(iinfo, iinfo->ii_btop);
18178 +       mode = (inode->i_mode & S_IFMT);
18179 +       isdir = S_ISDIR(mode);
18180 +       flags = au_hi_flags(inode, isdir);
18181 +       bbot = au_dbbot(dentry);
18182 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18183 +               struct inode *h_i, *h_inode;
18184 +               struct dentry *h_d;
18185 +
18186 +               h_d = au_h_dptr(dentry, bindex);
18187 +               if (!h_d || d_is_negative(h_d))
18188 +                       continue;
18189 +
18190 +               h_inode = d_inode(h_d);
18191 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18192 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18193 +                       h_i = au_h_iptr(inode, bindex);
18194 +                       if (h_i) {
18195 +                               if (h_i == h_inode)
18196 +                                       continue;
18197 +                               err = -EIO;
18198 +                               break;
18199 +                       }
18200 +               }
18201 +               if (bindex < iinfo->ii_btop)
18202 +                       iinfo->ii_btop = bindex;
18203 +               if (iinfo->ii_bbot < bindex)
18204 +                       iinfo->ii_bbot = bindex;
18205 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18206 +               update = 1;
18207 +       }
18208 +       au_update_ibrange(inode, /*do_put_zero*/0);
18209 +       e = au_dy_irefresh(inode);
18210 +       if (unlikely(e && !err))
18211 +               err = e;
18212 +       if (!err)
18213 +               au_refresh_hinode_attr(inode, update && isdir);
18214 +
18215 +out:
18216 +       AuTraceErr(err);
18217 +       return err;
18218 +}
18219 +
18220 +static int set_inode(struct inode *inode, struct dentry *dentry)
18221 +{
18222 +       int err;
18223 +       unsigned int flags;
18224 +       umode_t mode;
18225 +       aufs_bindex_t bindex, btop, btail;
18226 +       unsigned char isdir;
18227 +       struct dentry *h_dentry;
18228 +       struct inode *h_inode;
18229 +       struct au_iinfo *iinfo;
18230 +       const struct inode_operations *iop;
18231 +
18232 +       IiMustWriteLock(inode);
18233 +
18234 +       err = 0;
18235 +       isdir = 0;
18236 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18237 +       btop = au_dbtop(dentry);
18238 +       h_dentry = au_h_dptr(dentry, btop);
18239 +       h_inode = d_inode(h_dentry);
18240 +       mode = h_inode->i_mode;
18241 +       switch (mode & S_IFMT) {
18242 +       case S_IFREG:
18243 +               btail = au_dbtail(dentry);
18244 +               inode->i_op = iop + AuIop_OTHER;
18245 +               inode->i_fop = &aufs_file_fop;
18246 +               err = au_dy_iaop(inode, btop, h_inode);
18247 +               if (unlikely(err))
18248 +                       goto out;
18249 +               break;
18250 +       case S_IFDIR:
18251 +               isdir = 1;
18252 +               btail = au_dbtaildir(dentry);
18253 +               inode->i_op = iop + AuIop_DIR;
18254 +               inode->i_fop = &aufs_dir_fop;
18255 +               break;
18256 +       case S_IFLNK:
18257 +               btail = au_dbtail(dentry);
18258 +               inode->i_op = iop + AuIop_SYMLINK;
18259 +               break;
18260 +       case S_IFBLK:
18261 +       case S_IFCHR:
18262 +       case S_IFIFO:
18263 +       case S_IFSOCK:
18264 +               btail = au_dbtail(dentry);
18265 +               inode->i_op = iop + AuIop_OTHER;
18266 +               init_special_inode(inode, mode, h_inode->i_rdev);
18267 +               break;
18268 +       default:
18269 +               AuIOErr("Unknown file type 0%o\n", mode);
18270 +               err = -EIO;
18271 +               goto out;
18272 +       }
18273 +
18274 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18275 +       flags = au_hi_flags(inode, isdir);
18276 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18277 +           && au_ftest_hi(flags, HNOTIFY)
18278 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18279 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18280 +               au_fclr_hi(flags, HNOTIFY);
18281 +       iinfo = au_ii(inode);
18282 +       iinfo->ii_btop = btop;
18283 +       iinfo->ii_bbot = btail;
18284 +       for (bindex = btop; bindex <= btail; bindex++) {
18285 +               h_dentry = au_h_dptr(dentry, bindex);
18286 +               if (h_dentry)
18287 +                       au_set_h_iptr(inode, bindex,
18288 +                                     au_igrab(d_inode(h_dentry)), flags);
18289 +       }
18290 +       au_cpup_attr_all(inode, /*force*/1);
18291 +       /*
18292 +        * to force calling aufs_get_acl() every time,
18293 +        * do not call cache_no_acl() for aufs inode.
18294 +        */
18295 +
18296 +out:
18297 +       return err;
18298 +}
18299 +
18300 +/*
18301 + * successful returns with iinfo write_locked
18302 + * minus: errno
18303 + * zero: success, matched
18304 + * plus: no error, but unmatched
18305 + */
18306 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18307 +{
18308 +       int err;
18309 +       unsigned int gen, igflags;
18310 +       aufs_bindex_t bindex, bbot;
18311 +       struct inode *h_inode, *h_dinode;
18312 +       struct dentry *h_dentry;
18313 +
18314 +       /*
18315 +        * before this function, if aufs got any iinfo lock, it must be only
18316 +        * one, the parent dir.
18317 +        * it can happen by UDBA and the obsoleted inode number.
18318 +        */
18319 +       err = -EIO;
18320 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18321 +               goto out;
18322 +
18323 +       err = 1;
18324 +       ii_write_lock_new_child(inode);
18325 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18326 +       h_dinode = d_inode(h_dentry);
18327 +       bbot = au_ibbot(inode);
18328 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18329 +               h_inode = au_h_iptr(inode, bindex);
18330 +               if (!h_inode || h_inode != h_dinode)
18331 +                       continue;
18332 +
18333 +               err = 0;
18334 +               gen = au_iigen(inode, &igflags);
18335 +               if (gen == au_digen(dentry)
18336 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18337 +                       break;
18338 +
18339 +               /* fully refresh inode using dentry */
18340 +               err = au_refresh_hinode(inode, dentry);
18341 +               if (!err)
18342 +                       au_update_iigen(inode, /*half*/0);
18343 +               break;
18344 +       }
18345 +
18346 +       if (unlikely(err))
18347 +               ii_write_unlock(inode);
18348 +out:
18349 +       return err;
18350 +}
18351 +
18352 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18353 +          unsigned int d_type, ino_t *ino)
18354 +{
18355 +       int err, idx;
18356 +       const int isnondir = d_type != DT_DIR;
18357 +
18358 +       /* prevent hardlinked inode number from race condition */
18359 +       if (isnondir) {
18360 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18361 +               if (unlikely(err))
18362 +                       goto out;
18363 +       }
18364 +
18365 +       err = au_xino_read(sb, bindex, h_ino, ino);
18366 +       if (unlikely(err))
18367 +               goto out_xinondir;
18368 +
18369 +       if (!*ino) {
18370 +               err = -EIO;
18371 +               *ino = au_xino_new_ino(sb);
18372 +               if (unlikely(!*ino))
18373 +                       goto out_xinondir;
18374 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18375 +               if (unlikely(err))
18376 +                       goto out_xinondir;
18377 +       }
18378 +
18379 +out_xinondir:
18380 +       if (isnondir && idx >= 0)
18381 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18382 +out:
18383 +       return err;
18384 +}
18385 +
18386 +/* successful returns with iinfo write_locked */
18387 +/* todo: return with unlocked? */
18388 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18389 +{
18390 +       struct inode *inode, *h_inode;
18391 +       struct dentry *h_dentry;
18392 +       struct super_block *sb;
18393 +       ino_t h_ino, ino;
18394 +       int err, idx, hlinked;
18395 +       aufs_bindex_t btop;
18396 +
18397 +       sb = dentry->d_sb;
18398 +       btop = au_dbtop(dentry);
18399 +       h_dentry = au_h_dptr(dentry, btop);
18400 +       h_inode = d_inode(h_dentry);
18401 +       h_ino = h_inode->i_ino;
18402 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18403 +
18404 +new_ino:
18405 +       /*
18406 +        * stop 'race'-ing between hardlinks under different
18407 +        * parents.
18408 +        */
18409 +       if (hlinked) {
18410 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18411 +               inode = ERR_PTR(err);
18412 +               if (unlikely(err))
18413 +                       goto out;
18414 +       }
18415 +
18416 +       err = au_xino_read(sb, btop, h_ino, &ino);
18417 +       inode = ERR_PTR(err);
18418 +       if (unlikely(err))
18419 +               goto out_xinondir;
18420 +
18421 +       if (!ino) {
18422 +               ino = au_xino_new_ino(sb);
18423 +               if (unlikely(!ino)) {
18424 +                       inode = ERR_PTR(-EIO);
18425 +                       goto out_xinondir;
18426 +               }
18427 +       }
18428 +
18429 +       AuDbg("i%lu\n", (unsigned long)ino);
18430 +       inode = au_iget_locked(sb, ino);
18431 +       err = PTR_ERR(inode);
18432 +       if (IS_ERR(inode))
18433 +               goto out_xinondir;
18434 +
18435 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18436 +       if (inode->i_state & I_NEW) {
18437 +               ii_write_lock_new_child(inode);
18438 +               err = set_inode(inode, dentry);
18439 +               if (!err) {
18440 +                       unlock_new_inode(inode);
18441 +                       goto out_xinondir; /* success */
18442 +               }
18443 +
18444 +               /*
18445 +                * iget_failed() calls iput(), but we need to call
18446 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18447 +                * i_count.
18448 +                */
18449 +               atomic_inc(&inode->i_count);
18450 +               iget_failed(inode);
18451 +               ii_write_unlock(inode);
18452 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18453 +               /* ignore this error */
18454 +               goto out_iput;
18455 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18456 +               /*
18457 +                * horrible race condition between lookup, readdir and copyup
18458 +                * (or something).
18459 +                */
18460 +               if (hlinked && idx >= 0)
18461 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18462 +               err = reval_inode(inode, dentry);
18463 +               if (unlikely(err < 0)) {
18464 +                       hlinked = 0;
18465 +                       goto out_iput;
18466 +               }
18467 +               if (!err)
18468 +                       goto out; /* success */
18469 +               else if (hlinked && idx >= 0) {
18470 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18471 +                       if (unlikely(err)) {
18472 +                               iput(inode);
18473 +                               inode = ERR_PTR(err);
18474 +                               goto out;
18475 +                       }
18476 +               }
18477 +       }
18478 +
18479 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18480 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18481 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18482 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18483 +                       (unsigned long)h_ino, (unsigned long)ino);
18484 +       ino = 0;
18485 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18486 +       if (!err) {
18487 +               iput(inode);
18488 +               if (hlinked && idx >= 0)
18489 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18490 +               goto new_ino;
18491 +       }
18492 +
18493 +out_iput:
18494 +       iput(inode);
18495 +       inode = ERR_PTR(err);
18496 +out_xinondir:
18497 +       if (hlinked && idx >= 0)
18498 +               au_xinondir_leave(sb, btop, h_ino, idx);
18499 +out:
18500 +       return inode;
18501 +}
18502 +
18503 +/* ---------------------------------------------------------------------- */
18504 +
18505 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18506 +              struct inode *inode)
18507 +{
18508 +       int err;
18509 +       struct inode *hi;
18510 +
18511 +       err = au_br_rdonly(au_sbr(sb, bindex));
18512 +
18513 +       /* pseudo-link after flushed may happen out of bounds */
18514 +       if (!err
18515 +           && inode
18516 +           && au_ibtop(inode) <= bindex
18517 +           && bindex <= au_ibbot(inode)) {
18518 +               /*
18519 +                * permission check is unnecessary since vfsub routine
18520 +                * will be called later
18521 +                */
18522 +               hi = au_h_iptr(inode, bindex);
18523 +               if (hi)
18524 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18525 +       }
18526 +
18527 +       return err;
18528 +}
18529 +
18530 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18531 +                  int mask)
18532 +{
18533 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18534 +               return 0;
18535 +       return inode_permission(h_userns, h_inode, mask);
18536 +}
18537 +
18538 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18539 +                      int mask)
18540 +{
18541 +       if (au_test_nfs(h_inode->i_sb)
18542 +           && (mask & MAY_WRITE)
18543 +           && S_ISDIR(h_inode->i_mode))
18544 +               mask |= MAY_READ; /* force permission check */
18545 +       return au_test_h_perm(h_userns, h_inode, mask);
18546 +}
18547 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18548 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18549 +++ linux/fs/aufs/inode.h       2021-12-03 15:40:58.236647297 +0100
18550 @@ -0,0 +1,705 @@
18551 +/* SPDX-License-Identifier: GPL-2.0 */
18552 +/*
18553 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18554 + *
18555 + * This program, aufs is free software; you can redistribute it and/or modify
18556 + * it under the terms of the GNU General Public License as published by
18557 + * the Free Software Foundation; either version 2 of the License, or
18558 + * (at your option) any later version.
18559 + *
18560 + * This program is distributed in the hope that it will be useful,
18561 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18562 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18563 + * GNU General Public License for more details.
18564 + *
18565 + * You should have received a copy of the GNU General Public License
18566 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18567 + */
18568 +
18569 +/*
18570 + * inode operations
18571 + */
18572 +
18573 +#ifndef __AUFS_INODE_H__
18574 +#define __AUFS_INODE_H__
18575 +
18576 +#ifdef __KERNEL__
18577 +
18578 +#include <linux/fsnotify.h>
18579 +#include "rwsem.h"
18580 +
18581 +struct vfsmount;
18582 +
18583 +struct au_hnotify {
18584 +#ifdef CONFIG_AUFS_HNOTIFY
18585 +#ifdef CONFIG_AUFS_HFSNOTIFY
18586 +       /* never use fsnotify_add_vfsmount_mark() */
18587 +       struct fsnotify_mark            hn_mark;
18588 +#endif
18589 +       struct inode            *hn_aufs_inode; /* no get/put */
18590 +       struct rcu_head         rcu;
18591 +#endif
18592 +} ____cacheline_aligned_in_smp;
18593 +
18594 +struct au_hinode {
18595 +       struct inode            *hi_inode;
18596 +       aufs_bindex_t           hi_id;
18597 +#ifdef CONFIG_AUFS_HNOTIFY
18598 +       struct au_hnotify       *hi_notify;
18599 +#endif
18600 +
18601 +       /* reference to the copied-up whiteout with get/put */
18602 +       struct dentry           *hi_whdentry;
18603 +};
18604 +
18605 +/* ig_flags */
18606 +#define AuIG_HALF_REFRESHED            1
18607 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18608 +#define au_ig_fset(flags, name) \
18609 +       do { (flags) |= AuIG_##name; } while (0)
18610 +#define au_ig_fclr(flags, name) \
18611 +       do { (flags) &= ~AuIG_##name; } while (0)
18612 +
18613 +struct au_iigen {
18614 +       spinlock_t      ig_spin;
18615 +       __u32           ig_generation, ig_flags;
18616 +};
18617 +
18618 +struct au_vdir;
18619 +struct au_iinfo {
18620 +       struct au_iigen         ii_generation;
18621 +       struct super_block      *ii_hsb1;       /* no get/put */
18622 +
18623 +       struct au_rwsem         ii_rwsem;
18624 +       aufs_bindex_t           ii_btop, ii_bbot;
18625 +       __u32                   ii_higen;
18626 +       struct au_hinode        *ii_hinode;
18627 +       struct au_vdir          *ii_vdir;
18628 +};
18629 +
18630 +struct au_icntnr {
18631 +       struct au_iinfo         iinfo;
18632 +       struct inode            vfs_inode;
18633 +       struct hlist_bl_node    plink;
18634 +       struct rcu_head         rcu;
18635 +} ____cacheline_aligned_in_smp;
18636 +
18637 +/* au_pin flags */
18638 +#define AuPin_DI_LOCKED                1
18639 +#define AuPin_MNT_WRITE                (1 << 1)
18640 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18641 +#define au_fset_pin(flags, name) \
18642 +       do { (flags) |= AuPin_##name; } while (0)
18643 +#define au_fclr_pin(flags, name) \
18644 +       do { (flags) &= ~AuPin_##name; } while (0)
18645 +
18646 +struct au_pin {
18647 +       /* input */
18648 +       struct dentry *dentry;
18649 +       unsigned int udba;
18650 +       unsigned char lsc_di, lsc_hi, flags;
18651 +       aufs_bindex_t bindex;
18652 +
18653 +       /* output */
18654 +       struct dentry *parent;
18655 +       struct au_hinode *hdir;
18656 +       struct vfsmount *h_mnt;
18657 +
18658 +       /* temporary unlock/relock for copyup */
18659 +       struct dentry *h_dentry, *h_parent;
18660 +       struct au_branch *br;
18661 +       struct task_struct *task;
18662 +};
18663 +
18664 +void au_pin_hdir_unlock(struct au_pin *p);
18665 +int au_pin_hdir_lock(struct au_pin *p);
18666 +int au_pin_hdir_relock(struct au_pin *p);
18667 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18668 +void au_pin_hdir_release(struct au_pin *p);
18669 +
18670 +/* ---------------------------------------------------------------------- */
18671 +
18672 +static inline struct au_iinfo *au_ii(struct inode *inode)
18673 +{
18674 +       BUG_ON(is_bad_inode(inode));
18675 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18676 +}
18677 +
18678 +/* ---------------------------------------------------------------------- */
18679 +
18680 +/* inode.c */
18681 +struct inode *au_igrab(struct inode *inode);
18682 +void au_refresh_iop(struct inode *inode, int force_getattr);
18683 +int au_refresh_hinode_self(struct inode *inode);
18684 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18685 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18686 +          unsigned int d_type, ino_t *ino);
18687 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18688 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18689 +              struct inode *inode);
18690 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18691 +                  int mask);
18692 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18693 +                      int mask);
18694 +
18695 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18696 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18697 +{
18698 +#ifdef CONFIG_AUFS_SHWH
18699 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18700 +#else
18701 +       return 0;
18702 +#endif
18703 +}
18704 +
18705 +/* i_op.c */
18706 +enum {
18707 +       AuIop_SYMLINK,
18708 +       AuIop_DIR,
18709 +       AuIop_OTHER,
18710 +       AuIop_Last
18711 +};
18712 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18713 +       aufs_iop_nogetattr[AuIop_Last];
18714 +
18715 +/* au_wr_dir flags */
18716 +#define AuWrDir_ADD_ENTRY      1
18717 +#define AuWrDir_ISDIR          (1 << 1)
18718 +#define AuWrDir_TMPFILE                (1 << 2)
18719 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18720 +#define au_fset_wrdir(flags, name) \
18721 +       do { (flags) |= AuWrDir_##name; } while (0)
18722 +#define au_fclr_wrdir(flags, name) \
18723 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18724 +
18725 +struct au_wr_dir_args {
18726 +       aufs_bindex_t force_btgt;
18727 +       unsigned char flags;
18728 +};
18729 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18730 +             struct au_wr_dir_args *args);
18731 +
18732 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18733 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18734 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18735 +                unsigned int udba, unsigned char flags);
18736 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18737 +          unsigned int udba, unsigned char flags) __must_check;
18738 +int au_do_pin(struct au_pin *pin) __must_check;
18739 +void au_unpin(struct au_pin *pin);
18740 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18741 +
18742 +#define AuIcpup_DID_CPUP       1
18743 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18744 +#define au_fset_icpup(flags, name) \
18745 +       do { (flags) |= AuIcpup_##name; } while (0)
18746 +#define au_fclr_icpup(flags, name) \
18747 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18748 +
18749 +struct au_icpup_args {
18750 +       unsigned char flags;
18751 +       unsigned char pin_flags;
18752 +       aufs_bindex_t btgt;
18753 +       unsigned int udba;
18754 +       struct au_pin pin;
18755 +       struct path h_path;
18756 +       struct inode *h_inode;
18757 +};
18758 +
18759 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18760 +                    struct au_icpup_args *a);
18761 +
18762 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18763 +                     struct path *h_path, int locked);
18764 +
18765 +/* i_op_add.c */
18766 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18767 +              struct dentry *h_parent, int isdir);
18768 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
18769 +              struct dentry *dentry, umode_t mode, dev_t dev);
18770 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
18771 +                struct dentry *dentry, const char *symname);
18772 +int aufs_create(struct user_namespace *userns, struct inode *dir,
18773 +               struct dentry *dentry, umode_t mode, bool want_excl);
18774 +struct vfsub_aopen_args;
18775 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18776 +                      struct vfsub_aopen_args *args);
18777 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
18778 +                struct dentry *dentry, umode_t mode);
18779 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18780 +             struct dentry *dentry);
18781 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
18782 +              struct dentry *dentry, umode_t mode);
18783 +
18784 +/* i_op_del.c */
18785 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18786 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18787 +              struct dentry *h_parent, int isdir);
18788 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18789 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18790 +
18791 +/* i_op_ren.c */
18792 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18793 +int aufs_rename(struct user_namespace *userns,
18794 +               struct inode *_src_dir, struct dentry *_src_dentry,
18795 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
18796 +               unsigned int _flags);
18797 +
18798 +/* iinfo.c */
18799 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18800 +void au_hiput(struct au_hinode *hinode);
18801 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18802 +                 struct dentry *h_wh);
18803 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18804 +
18805 +/* hinode flags */
18806 +#define AuHi_XINO      1
18807 +#define AuHi_HNOTIFY   (1 << 1)
18808 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18809 +#define au_fset_hi(flags, name) \
18810 +       do { (flags) |= AuHi_##name; } while (0)
18811 +#define au_fclr_hi(flags, name) \
18812 +       do { (flags) &= ~AuHi_##name; } while (0)
18813 +
18814 +#ifndef CONFIG_AUFS_HNOTIFY
18815 +#undef AuHi_HNOTIFY
18816 +#define AuHi_HNOTIFY   0
18817 +#endif
18818 +
18819 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18820 +                  struct inode *h_inode, unsigned int flags);
18821 +
18822 +void au_update_iigen(struct inode *inode, int half);
18823 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18824 +
18825 +void au_icntnr_init_once(void *_c);
18826 +void au_hinode_init(struct au_hinode *hinode);
18827 +int au_iinfo_init(struct inode *inode);
18828 +void au_iinfo_fin(struct inode *inode);
18829 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18830 +
18831 +#ifdef CONFIG_PROC_FS
18832 +/* plink.c */
18833 +int au_plink_maint(struct super_block *sb, int flags);
18834 +struct au_sbinfo;
18835 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18836 +int au_plink_maint_enter(struct super_block *sb);
18837 +#ifdef CONFIG_AUFS_DEBUG
18838 +void au_plink_list(struct super_block *sb);
18839 +#else
18840 +AuStubVoid(au_plink_list, struct super_block *sb)
18841 +#endif
18842 +int au_plink_test(struct inode *inode);
18843 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18844 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18845 +                    struct dentry *h_dentry);
18846 +void au_plink_put(struct super_block *sb, int verbose);
18847 +void au_plink_clean(struct super_block *sb, int verbose);
18848 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18849 +#else
18850 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18851 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18852 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18853 +AuStubVoid(au_plink_list, struct super_block *sb);
18854 +AuStubInt0(au_plink_test, struct inode *inode);
18855 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18856 +       struct inode *inode, aufs_bindex_t bindex);
18857 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18858 +          struct dentry *h_dentry);
18859 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18860 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18861 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18862 +#endif /* CONFIG_PROC_FS */
18863 +
18864 +#ifdef CONFIG_AUFS_XATTR
18865 +/* xattr.c */
18866 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
18867 +                 unsigned int verbose);
18868 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18869 +void au_xattr_init(struct super_block *sb);
18870 +#else
18871 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
18872 +          int ignore_flags, unsigned int verbose);
18873 +AuStubVoid(au_xattr_init, struct super_block *sb);
18874 +#endif
18875 +
18876 +#ifdef CONFIG_FS_POSIX_ACL
18877 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
18878 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
18879 +                struct posix_acl *acl, int type);
18880 +#endif
18881 +
18882 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18883 +enum {
18884 +       AU_XATTR_SET,
18885 +       AU_ACL_SET
18886 +};
18887 +
18888 +struct au_sxattr {
18889 +       int type;
18890 +       union {
18891 +               struct {
18892 +                       const char      *name;
18893 +                       const void      *value;
18894 +                       size_t          size;
18895 +                       int             flags;
18896 +               } set;
18897 +               struct {
18898 +                       struct posix_acl *acl;
18899 +                       int             type;
18900 +               } acl_set;
18901 +       } u;
18902 +};
18903 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18904 +                 struct au_sxattr *arg);
18905 +#endif
18906 +
18907 +/* ---------------------------------------------------------------------- */
18908 +
18909 +/* lock subclass for iinfo */
18910 +enum {
18911 +       AuLsc_II_CHILD,         /* child first */
18912 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
18913 +       AuLsc_II_CHILD3,        /* copyup dirs */
18914 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
18915 +       AuLsc_II_PARENT2,
18916 +       AuLsc_II_PARENT3,       /* copyup dirs */
18917 +       AuLsc_II_NEW_CHILD
18918 +};
18919 +
18920 +/*
18921 + * ii_read_lock_child, ii_write_lock_child,
18922 + * ii_read_lock_child2, ii_write_lock_child2,
18923 + * ii_read_lock_child3, ii_write_lock_child3,
18924 + * ii_read_lock_parent, ii_write_lock_parent,
18925 + * ii_read_lock_parent2, ii_write_lock_parent2,
18926 + * ii_read_lock_parent3, ii_write_lock_parent3,
18927 + * ii_read_lock_new_child, ii_write_lock_new_child,
18928 + */
18929 +#define AuReadLockFunc(name, lsc) \
18930 +static inline void ii_read_lock_##name(struct inode *i) \
18931 +{ \
18932 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18933 +}
18934 +
18935 +#define AuWriteLockFunc(name, lsc) \
18936 +static inline void ii_write_lock_##name(struct inode *i) \
18937 +{ \
18938 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18939 +}
18940 +
18941 +#define AuRWLockFuncs(name, lsc) \
18942 +       AuReadLockFunc(name, lsc) \
18943 +       AuWriteLockFunc(name, lsc)
18944 +
18945 +AuRWLockFuncs(child, CHILD);
18946 +AuRWLockFuncs(child2, CHILD2);
18947 +AuRWLockFuncs(child3, CHILD3);
18948 +AuRWLockFuncs(parent, PARENT);
18949 +AuRWLockFuncs(parent2, PARENT2);
18950 +AuRWLockFuncs(parent3, PARENT3);
18951 +AuRWLockFuncs(new_child, NEW_CHILD);
18952 +
18953 +#undef AuReadLockFunc
18954 +#undef AuWriteLockFunc
18955 +#undef AuRWLockFuncs
18956 +
18957 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
18958 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
18959 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
18960 +
18961 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
18962 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
18963 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
18964 +
18965 +/* ---------------------------------------------------------------------- */
18966 +
18967 +static inline void au_icntnr_init(struct au_icntnr *c)
18968 +{
18969 +#ifdef CONFIG_AUFS_DEBUG
18970 +       c->vfs_inode.i_mode = 0;
18971 +#endif
18972 +}
18973 +
18974 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
18975 +{
18976 +       unsigned int gen;
18977 +       struct au_iinfo *iinfo;
18978 +       struct au_iigen *iigen;
18979 +
18980 +       iinfo = au_ii(inode);
18981 +       iigen = &iinfo->ii_generation;
18982 +       spin_lock(&iigen->ig_spin);
18983 +       if (igflags)
18984 +               *igflags = iigen->ig_flags;
18985 +       gen = iigen->ig_generation;
18986 +       spin_unlock(&iigen->ig_spin);
18987 +
18988 +       return gen;
18989 +}
18990 +
18991 +/* tiny test for inode number */
18992 +/* tmpfs generation is too rough */
18993 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
18994 +{
18995 +       struct au_iinfo *iinfo;
18996 +
18997 +       iinfo = au_ii(inode);
18998 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
18999 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19000 +                && iinfo->ii_higen == h_inode->i_generation);
19001 +}
19002 +
19003 +static inline void au_iigen_dec(struct inode *inode)
19004 +{
19005 +       struct au_iinfo *iinfo;
19006 +       struct au_iigen *iigen;
19007 +
19008 +       iinfo = au_ii(inode);
19009 +       iigen = &iinfo->ii_generation;
19010 +       spin_lock(&iigen->ig_spin);
19011 +       iigen->ig_generation--;
19012 +       spin_unlock(&iigen->ig_spin);
19013 +}
19014 +
19015 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19016 +{
19017 +       int err;
19018 +
19019 +       err = 0;
19020 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19021 +               err = -EIO;
19022 +
19023 +       return err;
19024 +}
19025 +
19026 +/* ---------------------------------------------------------------------- */
19027 +
19028 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19029 +                                         aufs_bindex_t bindex)
19030 +{
19031 +       return iinfo->ii_hinode + bindex;
19032 +}
19033 +
19034 +static inline int au_is_bad_inode(struct inode *inode)
19035 +{
19036 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19037 +}
19038 +
19039 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19040 +                                       aufs_bindex_t bindex)
19041 +{
19042 +       IiMustAnyLock(inode);
19043 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19044 +}
19045 +
19046 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19047 +{
19048 +       IiMustAnyLock(inode);
19049 +       return au_ii(inode)->ii_btop;
19050 +}
19051 +
19052 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19053 +{
19054 +       IiMustAnyLock(inode);
19055 +       return au_ii(inode)->ii_bbot;
19056 +}
19057 +
19058 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19059 +{
19060 +       IiMustAnyLock(inode);
19061 +       return au_ii(inode)->ii_vdir;
19062 +}
19063 +
19064 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19065 +{
19066 +       IiMustAnyLock(inode);
19067 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19068 +}
19069 +
19070 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19071 +{
19072 +       IiMustWriteLock(inode);
19073 +       au_ii(inode)->ii_btop = bindex;
19074 +}
19075 +
19076 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19077 +{
19078 +       IiMustWriteLock(inode);
19079 +       au_ii(inode)->ii_bbot = bindex;
19080 +}
19081 +
19082 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19083 +{
19084 +       IiMustWriteLock(inode);
19085 +       au_ii(inode)->ii_vdir = vdir;
19086 +}
19087 +
19088 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19089 +{
19090 +       IiMustAnyLock(inode);
19091 +       return au_hinode(au_ii(inode), bindex);
19092 +}
19093 +
19094 +/* ---------------------------------------------------------------------- */
19095 +
19096 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19097 +{
19098 +       if (pin)
19099 +               return pin->parent;
19100 +       return NULL;
19101 +}
19102 +
19103 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19104 +{
19105 +       if (pin && pin->hdir)
19106 +               return pin->hdir->hi_inode;
19107 +       return NULL;
19108 +}
19109 +
19110 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19111 +{
19112 +       if (pin)
19113 +               return pin->hdir;
19114 +       return NULL;
19115 +}
19116 +
19117 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19118 +{
19119 +       if (pin)
19120 +               pin->dentry = dentry;
19121 +}
19122 +
19123 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19124 +                                          unsigned char lflag)
19125 +{
19126 +       if (pin) {
19127 +               if (lflag)
19128 +                       au_fset_pin(pin->flags, DI_LOCKED);
19129 +               else
19130 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19131 +       }
19132 +}
19133 +
19134 +#if 0 /* reserved */
19135 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19136 +{
19137 +       if (pin) {
19138 +               dput(pin->parent);
19139 +               pin->parent = dget(parent);
19140 +       }
19141 +}
19142 +#endif
19143 +
19144 +/* ---------------------------------------------------------------------- */
19145 +
19146 +struct au_branch;
19147 +#ifdef CONFIG_AUFS_HNOTIFY
19148 +struct au_hnotify_op {
19149 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19150 +       int (*alloc)(struct au_hinode *hinode);
19151 +
19152 +       /*
19153 +        * if it returns true, the caller should free hinode->hi_notify,
19154 +        * otherwise ->free() frees it.
19155 +        */
19156 +       int (*free)(struct au_hinode *hinode,
19157 +                   struct au_hnotify *hn) __must_check;
19158 +
19159 +       void (*fin)(void);
19160 +       int (*init)(void);
19161 +
19162 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19163 +       void (*fin_br)(struct au_branch *br);
19164 +       int (*init_br)(struct au_branch *br, int perm);
19165 +};
19166 +
19167 +/* hnotify.c */
19168 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19169 +void au_hn_free(struct au_hinode *hinode);
19170 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19171 +void au_hn_reset(struct inode *inode, unsigned int flags);
19172 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19173 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19174 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19175 +int au_hnotify_init_br(struct au_branch *br, int perm);
19176 +void au_hnotify_fin_br(struct au_branch *br);
19177 +int __init au_hnotify_init(void);
19178 +void au_hnotify_fin(void);
19179 +
19180 +/* hfsnotify.c */
19181 +extern const struct au_hnotify_op au_hnotify_op;
19182 +
19183 +static inline
19184 +void au_hn_init(struct au_hinode *hinode)
19185 +{
19186 +       hinode->hi_notify = NULL;
19187 +}
19188 +
19189 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19190 +{
19191 +       return hinode->hi_notify;
19192 +}
19193 +
19194 +#else
19195 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19196 +       struct au_hinode *hinode __maybe_unused,
19197 +       struct inode *inode __maybe_unused)
19198 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19199 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19200 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19201 +          int do_set __maybe_unused)
19202 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19203 +          unsigned int flags __maybe_unused)
19204 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19205 +          struct au_branch *br __maybe_unused,
19206 +          int perm __maybe_unused)
19207 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19208 +          int perm __maybe_unused)
19209 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19210 +AuStubInt0(__init au_hnotify_init, void)
19211 +AuStubVoid(au_hnotify_fin, void)
19212 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19213 +#endif /* CONFIG_AUFS_HNOTIFY */
19214 +
19215 +static inline void au_hn_suspend(struct au_hinode *hdir)
19216 +{
19217 +       au_hn_ctl(hdir, /*do_set*/0);
19218 +}
19219 +
19220 +static inline void au_hn_resume(struct au_hinode *hdir)
19221 +{
19222 +       au_hn_ctl(hdir, /*do_set*/1);
19223 +}
19224 +
19225 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19226 +{
19227 +       inode_lock(hdir->hi_inode);
19228 +       au_hn_suspend(hdir);
19229 +}
19230 +
19231 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19232 +                                         unsigned int sc __maybe_unused)
19233 +{
19234 +       inode_lock_nested(hdir->hi_inode, sc);
19235 +       au_hn_suspend(hdir);
19236 +}
19237 +
19238 +#if 0 /* unused */
19239 +#include "vfsub.h"
19240 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19241 +                                                 unsigned int sc)
19242 +{
19243 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19244 +       au_hn_suspend(hdir);
19245 +}
19246 +#endif
19247 +
19248 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19249 +{
19250 +       au_hn_resume(hdir);
19251 +       inode_unlock(hdir->hi_inode);
19252 +}
19253 +
19254 +#endif /* __KERNEL__ */
19255 +#endif /* __AUFS_INODE_H__ */
19256 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19257 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19258 +++ linux/fs/aufs/ioctl.c       2021-12-03 15:38:59.936647310 +0100
19259 @@ -0,0 +1,220 @@
19260 +// SPDX-License-Identifier: GPL-2.0
19261 +/*
19262 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19263 + *
19264 + * This program, aufs is free software; you can redistribute it and/or modify
19265 + * it under the terms of the GNU General Public License as published by
19266 + * the Free Software Foundation; either version 2 of the License, or
19267 + * (at your option) any later version.
19268 + *
19269 + * This program is distributed in the hope that it will be useful,
19270 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19271 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19272 + * GNU General Public License for more details.
19273 + *
19274 + * You should have received a copy of the GNU General Public License
19275 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19276 + */
19277 +
19278 +/*
19279 + * ioctl
19280 + * plink-management and readdir in userspace.
19281 + * assist the pathconf(3) wrapper library.
19282 + * move-down
19283 + * File-based Hierarchical Storage Management.
19284 + */
19285 +
19286 +#include <linux/compat.h>
19287 +#include <linux/file.h>
19288 +#include "aufs.h"
19289 +
19290 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19291 +{
19292 +       int err, fd;
19293 +       aufs_bindex_t wbi, bindex, bbot;
19294 +       struct file *h_file;
19295 +       struct super_block *sb;
19296 +       struct dentry *root;
19297 +       struct au_branch *br;
19298 +       struct aufs_wbr_fd wbrfd = {
19299 +               .oflags = au_dir_roflags,
19300 +               .brid   = -1
19301 +       };
19302 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19303 +               | O_NOATIME | O_CLOEXEC;
19304 +
19305 +       AuDebugOn(wbrfd.oflags & ~valid);
19306 +
19307 +       if (arg) {
19308 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19309 +               if (unlikely(err)) {
19310 +                       err = -EFAULT;
19311 +                       goto out;
19312 +               }
19313 +
19314 +               err = -EINVAL;
19315 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19316 +               wbrfd.oflags |= au_dir_roflags;
19317 +               AuDbg("0%o\n", wbrfd.oflags);
19318 +               if (unlikely(wbrfd.oflags & ~valid))
19319 +                       goto out;
19320 +       }
19321 +
19322 +       fd = get_unused_fd_flags(0);
19323 +       err = fd;
19324 +       if (unlikely(fd < 0))
19325 +               goto out;
19326 +
19327 +       h_file = ERR_PTR(-EINVAL);
19328 +       wbi = 0;
19329 +       br = NULL;
19330 +       sb = path->dentry->d_sb;
19331 +       root = sb->s_root;
19332 +       aufs_read_lock(root, AuLock_IR);
19333 +       bbot = au_sbbot(sb);
19334 +       if (wbrfd.brid >= 0) {
19335 +               wbi = au_br_index(sb, wbrfd.brid);
19336 +               if (unlikely(wbi < 0 || wbi > bbot))
19337 +                       goto out_unlock;
19338 +       }
19339 +
19340 +       h_file = ERR_PTR(-ENOENT);
19341 +       br = au_sbr(sb, wbi);
19342 +       if (!au_br_writable(br->br_perm)) {
19343 +               if (arg)
19344 +                       goto out_unlock;
19345 +
19346 +               bindex = wbi + 1;
19347 +               wbi = -1;
19348 +               for (; bindex <= bbot; bindex++) {
19349 +                       br = au_sbr(sb, bindex);
19350 +                       if (au_br_writable(br->br_perm)) {
19351 +                               wbi = bindex;
19352 +                               br = au_sbr(sb, wbi);
19353 +                               break;
19354 +                       }
19355 +               }
19356 +       }
19357 +       AuDbg("wbi %d\n", wbi);
19358 +       if (wbi >= 0)
19359 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19360 +                                  /*force_wr*/0);
19361 +
19362 +out_unlock:
19363 +       aufs_read_unlock(root, AuLock_IR);
19364 +       err = PTR_ERR(h_file);
19365 +       if (IS_ERR(h_file))
19366 +               goto out_fd;
19367 +
19368 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19369 +       fd_install(fd, h_file);
19370 +       err = fd;
19371 +       goto out; /* success */
19372 +
19373 +out_fd:
19374 +       put_unused_fd(fd);
19375 +out:
19376 +       AuTraceErr(err);
19377 +       return err;
19378 +}
19379 +
19380 +/* ---------------------------------------------------------------------- */
19381 +
19382 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19383 +{
19384 +       long err;
19385 +       struct dentry *dentry;
19386 +
19387 +       switch (cmd) {
19388 +       case AUFS_CTL_RDU:
19389 +       case AUFS_CTL_RDU_INO:
19390 +               err = au_rdu_ioctl(file, cmd, arg);
19391 +               break;
19392 +
19393 +       case AUFS_CTL_WBR_FD:
19394 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19395 +               break;
19396 +
19397 +       case AUFS_CTL_IBUSY:
19398 +               err = au_ibusy_ioctl(file, arg);
19399 +               break;
19400 +
19401 +       case AUFS_CTL_BRINFO:
19402 +               err = au_brinfo_ioctl(file, arg);
19403 +               break;
19404 +
19405 +       case AUFS_CTL_FHSM_FD:
19406 +               dentry = file->f_path.dentry;
19407 +               if (IS_ROOT(dentry))
19408 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19409 +               else
19410 +                       err = -ENOTTY;
19411 +               break;
19412 +
19413 +       default:
19414 +               /* do not call the lower */
19415 +               AuDbg("0x%x\n", cmd);
19416 +               err = -ENOTTY;
19417 +       }
19418 +
19419 +       AuTraceErr(err);
19420 +       return err;
19421 +}
19422 +
19423 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19424 +{
19425 +       long err;
19426 +
19427 +       switch (cmd) {
19428 +       case AUFS_CTL_MVDOWN:
19429 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19430 +               break;
19431 +
19432 +       case AUFS_CTL_WBR_FD:
19433 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19434 +               break;
19435 +
19436 +       default:
19437 +               /* do not call the lower */
19438 +               AuDbg("0x%x\n", cmd);
19439 +               err = -ENOTTY;
19440 +       }
19441 +
19442 +       AuTraceErr(err);
19443 +       return err;
19444 +}
19445 +
19446 +#ifdef CONFIG_COMPAT
19447 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19448 +                          unsigned long arg)
19449 +{
19450 +       long err;
19451 +
19452 +       switch (cmd) {
19453 +       case AUFS_CTL_RDU:
19454 +       case AUFS_CTL_RDU_INO:
19455 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19456 +               break;
19457 +
19458 +       case AUFS_CTL_IBUSY:
19459 +               err = au_ibusy_compat_ioctl(file, arg);
19460 +               break;
19461 +
19462 +       case AUFS_CTL_BRINFO:
19463 +               err = au_brinfo_compat_ioctl(file, arg);
19464 +               break;
19465 +
19466 +       default:
19467 +               err = aufs_ioctl_dir(file, cmd, arg);
19468 +       }
19469 +
19470 +       AuTraceErr(err);
19471 +       return err;
19472 +}
19473 +
19474 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19475 +                             unsigned long arg)
19476 +{
19477 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19478 +}
19479 +#endif
19480 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19481 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19482 +++ linux/fs/aufs/i_op_add.c    2021-12-03 15:40:58.236647297 +0100
19483 @@ -0,0 +1,941 @@
19484 +// SPDX-License-Identifier: GPL-2.0
19485 +/*
19486 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19487 + *
19488 + * This program, aufs is free software; you can redistribute it and/or modify
19489 + * it under the terms of the GNU General Public License as published by
19490 + * the Free Software Foundation; either version 2 of the License, or
19491 + * (at your option) any later version.
19492 + *
19493 + * This program is distributed in the hope that it will be useful,
19494 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19495 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19496 + * GNU General Public License for more details.
19497 + *
19498 + * You should have received a copy of the GNU General Public License
19499 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19500 + */
19501 +
19502 +/*
19503 + * inode operations (add entry)
19504 + */
19505 +
19506 +#include <linux/iversion.h>
19507 +#include "aufs.h"
19508 +
19509 +/*
19510 + * final procedure of adding a new entry, except link(2).
19511 + * remove whiteout, instantiate, copyup the parent dir's times and size
19512 + * and update version.
19513 + * if it failed, re-create the removed whiteout.
19514 + */
19515 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19516 +                 struct dentry *wh_dentry, struct dentry *dentry)
19517 +{
19518 +       int err, rerr;
19519 +       aufs_bindex_t bwh;
19520 +       struct path h_path;
19521 +       struct super_block *sb;
19522 +       struct inode *inode, *h_dir;
19523 +       struct dentry *wh;
19524 +
19525 +       bwh = -1;
19526 +       sb = dir->i_sb;
19527 +       if (wh_dentry) {
19528 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19529 +               IMustLock(h_dir);
19530 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19531 +               bwh = au_dbwh(dentry);
19532 +               h_path.dentry = wh_dentry;
19533 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19534 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19535 +                                         dentry);
19536 +               if (unlikely(err))
19537 +                       goto out;
19538 +       }
19539 +
19540 +       inode = au_new_inode(dentry, /*must_new*/1);
19541 +       if (!IS_ERR(inode)) {
19542 +               d_instantiate(dentry, inode);
19543 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19544 +               IMustLock(dir);
19545 +               au_dir_ts(dir, bindex);
19546 +               inode_inc_iversion(dir);
19547 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19548 +               return 0; /* success */
19549 +       }
19550 +
19551 +       err = PTR_ERR(inode);
19552 +       if (!wh_dentry)
19553 +               goto out;
19554 +
19555 +       /* revert */
19556 +       /* dir inode is locked */
19557 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19558 +       rerr = PTR_ERR(wh);
19559 +       if (IS_ERR(wh)) {
19560 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19561 +                       dentry, err, rerr);
19562 +               err = -EIO;
19563 +       } else
19564 +               dput(wh);
19565 +
19566 +out:
19567 +       return err;
19568 +}
19569 +
19570 +static int au_d_may_add(struct dentry *dentry)
19571 +{
19572 +       int err;
19573 +
19574 +       err = 0;
19575 +       if (unlikely(d_unhashed(dentry)))
19576 +               err = -ENOENT;
19577 +       if (unlikely(d_really_is_positive(dentry)))
19578 +               err = -EEXIST;
19579 +       return err;
19580 +}
19581 +
19582 +/*
19583 + * simple tests for the adding inode operations.
19584 + * following the checks in vfs, plus the parent-child relationship.
19585 + */
19586 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19587 +              struct dentry *h_parent, int isdir)
19588 +{
19589 +       int err;
19590 +       umode_t h_mode;
19591 +       struct dentry *h_dentry;
19592 +       struct inode *h_inode;
19593 +
19594 +       err = -ENAMETOOLONG;
19595 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19596 +               goto out;
19597 +
19598 +       h_dentry = au_h_dptr(dentry, bindex);
19599 +       if (d_really_is_negative(dentry)) {
19600 +               err = -EEXIST;
19601 +               if (unlikely(d_is_positive(h_dentry)))
19602 +                       goto out;
19603 +       } else {
19604 +               /* rename(2) case */
19605 +               err = -EIO;
19606 +               if (unlikely(d_is_negative(h_dentry)))
19607 +                       goto out;
19608 +               h_inode = d_inode(h_dentry);
19609 +               if (unlikely(!h_inode->i_nlink))
19610 +                       goto out;
19611 +
19612 +               h_mode = h_inode->i_mode;
19613 +               if (!isdir) {
19614 +                       err = -EISDIR;
19615 +                       if (unlikely(S_ISDIR(h_mode)))
19616 +                               goto out;
19617 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19618 +                       err = -ENOTDIR;
19619 +                       goto out;
19620 +               }
19621 +       }
19622 +
19623 +       err = 0;
19624 +       /* expected parent dir is locked */
19625 +       if (unlikely(h_parent != h_dentry->d_parent))
19626 +               err = -EIO;
19627 +
19628 +out:
19629 +       AuTraceErr(err);
19630 +       return err;
19631 +}
19632 +
19633 +/*
19634 + * initial procedure of adding a new entry.
19635 + * prepare writable branch and the parent dir, lock it,
19636 + * and lookup whiteout for the new entry.
19637 + */
19638 +static struct dentry*
19639 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19640 +                 struct dentry *src_dentry, struct au_pin *pin,
19641 +                 struct au_wr_dir_args *wr_dir_args)
19642 +{
19643 +       struct dentry *wh_dentry, *h_parent;
19644 +       struct super_block *sb;
19645 +       struct au_branch *br;
19646 +       int err;
19647 +       unsigned int udba;
19648 +       aufs_bindex_t bcpup;
19649 +
19650 +       AuDbg("%pd\n", dentry);
19651 +
19652 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19653 +       bcpup = err;
19654 +       wh_dentry = ERR_PTR(err);
19655 +       if (unlikely(err < 0))
19656 +               goto out;
19657 +
19658 +       sb = dentry->d_sb;
19659 +       udba = au_opt_udba(sb);
19660 +       err = au_pin(pin, dentry, bcpup, udba,
19661 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19662 +       wh_dentry = ERR_PTR(err);
19663 +       if (unlikely(err))
19664 +               goto out;
19665 +
19666 +       h_parent = au_pinned_h_parent(pin);
19667 +       if (udba != AuOpt_UDBA_NONE
19668 +           && au_dbtop(dentry) == bcpup)
19669 +               err = au_may_add(dentry, bcpup, h_parent,
19670 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19671 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19672 +               err = -ENAMETOOLONG;
19673 +       wh_dentry = ERR_PTR(err);
19674 +       if (unlikely(err))
19675 +               goto out_unpin;
19676 +
19677 +       br = au_sbr(sb, bcpup);
19678 +       if (dt) {
19679 +               struct path tmp = {
19680 +                       .dentry = h_parent,
19681 +                       .mnt    = au_br_mnt(br)
19682 +               };
19683 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19684 +       }
19685 +
19686 +       wh_dentry = NULL;
19687 +       if (bcpup != au_dbwh(dentry))
19688 +               goto out; /* success */
19689 +
19690 +       /*
19691 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19692 +        * would not be able to removed in the future. So we don't allow such
19693 +        * name here and we don't handle ENAMETOOLONG differently here.
19694 +        */
19695 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19696 +
19697 +out_unpin:
19698 +       if (IS_ERR(wh_dentry))
19699 +               au_unpin(pin);
19700 +out:
19701 +       return wh_dentry;
19702 +}
19703 +
19704 +/* ---------------------------------------------------------------------- */
19705 +
19706 +enum { Mknod, Symlink, Creat };
19707 +struct simple_arg {
19708 +       int type;
19709 +       union {
19710 +               struct {
19711 +                       umode_t                 mode;
19712 +                       bool                    want_excl;
19713 +                       bool                    try_aopen;
19714 +                       struct vfsub_aopen_args *aopen;
19715 +               } c;
19716 +               struct {
19717 +                       const char *symname;
19718 +               } s;
19719 +               struct {
19720 +                       umode_t mode;
19721 +                       dev_t dev;
19722 +               } m;
19723 +       } u;
19724 +};
19725 +
19726 +static int add_simple(struct inode *dir, struct dentry *dentry,
19727 +                     struct simple_arg *arg)
19728 +{
19729 +       int err, rerr;
19730 +       aufs_bindex_t btop;
19731 +       unsigned char created;
19732 +       const unsigned char try_aopen
19733 +               = (arg->type == Creat && arg->u.c.try_aopen);
19734 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19735 +       struct dentry *wh_dentry, *parent;
19736 +       struct inode *h_dir;
19737 +       struct super_block *sb;
19738 +       struct au_branch *br;
19739 +       /* to reduce stack size */
19740 +       struct {
19741 +               struct au_dtime dt;
19742 +               struct au_pin pin;
19743 +               struct path h_path;
19744 +               struct au_wr_dir_args wr_dir_args;
19745 +       } *a;
19746 +
19747 +       AuDbg("%pd\n", dentry);
19748 +       IMustLock(dir);
19749 +
19750 +       err = -ENOMEM;
19751 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19752 +       if (unlikely(!a))
19753 +               goto out;
19754 +       a->wr_dir_args.force_btgt = -1;
19755 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19756 +
19757 +       parent = dentry->d_parent; /* dir inode is locked */
19758 +       if (!try_aopen) {
19759 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19760 +               if (unlikely(err))
19761 +                       goto out_free;
19762 +       }
19763 +       err = au_d_may_add(dentry);
19764 +       if (unlikely(err))
19765 +               goto out_unlock;
19766 +       if (!try_aopen)
19767 +               di_write_lock_parent(parent);
19768 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19769 +                                     &a->pin, &a->wr_dir_args);
19770 +       err = PTR_ERR(wh_dentry);
19771 +       if (IS_ERR(wh_dentry))
19772 +               goto out_parent;
19773 +
19774 +       btop = au_dbtop(dentry);
19775 +       sb = dentry->d_sb;
19776 +       br = au_sbr(sb, btop);
19777 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19778 +       a->h_path.mnt = au_br_mnt(br);
19779 +       h_dir = au_pinned_h_dir(&a->pin);
19780 +       switch (arg->type) {
19781 +       case Creat:
19782 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19783 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19784 +                                          arg->u.c.want_excl);
19785 +                       created = !err;
19786 +                       if (!err && try_aopen)
19787 +                               aopen->file->f_mode |= FMODE_CREATED;
19788 +               } else {
19789 +                       aopen->br = br;
19790 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19791 +                       AuDbg("err %d\n", err);
19792 +                       AuDbgFile(aopen->file);
19793 +                       created = err >= 0
19794 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19795 +               }
19796 +               break;
19797 +       case Symlink:
19798 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19799 +               created = !err;
19800 +               break;
19801 +       case Mknod:
19802 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19803 +                                 arg->u.m.dev);
19804 +               created = !err;
19805 +               break;
19806 +       default:
19807 +               BUG();
19808 +       }
19809 +       if (unlikely(err < 0))
19810 +               goto out_unpin;
19811 +
19812 +       err = epilog(dir, btop, wh_dentry, dentry);
19813 +       if (!err)
19814 +               goto out_unpin; /* success */
19815 +
19816 +       /* revert */
19817 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19818 +               /* no delegation since it is just created */
19819 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19820 +                                   /*force*/0);
19821 +               if (rerr) {
19822 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19823 +                               dentry, err, rerr);
19824 +                       err = -EIO;
19825 +               }
19826 +               au_dtime_revert(&a->dt);
19827 +       }
19828 +       if (try_aopen && h_dir->i_op->atomic_open
19829 +           && (aopen->file->f_mode & FMODE_OPENED))
19830 +               /* aopen->file is still opened */
19831 +               au_lcnt_dec(&aopen->br->br_nfiles);
19832 +
19833 +out_unpin:
19834 +       au_unpin(&a->pin);
19835 +       dput(wh_dentry);
19836 +out_parent:
19837 +       if (!try_aopen)
19838 +               di_write_unlock(parent);
19839 +out_unlock:
19840 +       if (unlikely(err)) {
19841 +               au_update_dbtop(dentry);
19842 +               d_drop(dentry);
19843 +       }
19844 +       if (!try_aopen)
19845 +               aufs_read_unlock(dentry, AuLock_DW);
19846 +out_free:
19847 +       au_kfree_rcu(a);
19848 +out:
19849 +       return err;
19850 +}
19851 +
19852 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
19853 +              struct dentry *dentry, umode_t mode, dev_t dev)
19854 +{
19855 +       struct simple_arg arg = {
19856 +               .type = Mknod,
19857 +               .u.m = {
19858 +                       .mode   = mode,
19859 +                       .dev    = dev
19860 +               }
19861 +       };
19862 +       return add_simple(dir, dentry, &arg);
19863 +}
19864 +
19865 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
19866 +                struct dentry *dentry, const char *symname)
19867 +{
19868 +       struct simple_arg arg = {
19869 +               .type = Symlink,
19870 +               .u.s.symname = symname
19871 +       };
19872 +       return add_simple(dir, dentry, &arg);
19873 +}
19874 +
19875 +int aufs_create(struct user_namespace *userns, struct inode *dir,
19876 +               struct dentry *dentry, umode_t mode, bool want_excl)
19877 +{
19878 +       struct simple_arg arg = {
19879 +               .type = Creat,
19880 +               .u.c = {
19881 +                       .mode           = mode,
19882 +                       .want_excl      = want_excl
19883 +               }
19884 +       };
19885 +       return add_simple(dir, dentry, &arg);
19886 +}
19887 +
19888 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19889 +                      struct vfsub_aopen_args *aopen_args)
19890 +{
19891 +       struct simple_arg arg = {
19892 +               .type = Creat,
19893 +               .u.c = {
19894 +                       .mode           = aopen_args->create_mode,
19895 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
19896 +                       .try_aopen      = true,
19897 +                       .aopen          = aopen_args
19898 +               }
19899 +       };
19900 +       return add_simple(dir, dentry, &arg);
19901 +}
19902 +
19903 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
19904 +                struct dentry *dentry, umode_t mode)
19905 +{
19906 +       int err;
19907 +       aufs_bindex_t bindex;
19908 +       struct super_block *sb;
19909 +       struct dentry *parent, *h_parent, *h_dentry;
19910 +       struct inode *h_dir, *inode;
19911 +       struct vfsmount *h_mnt;
19912 +       struct user_namespace *h_userns;
19913 +       struct au_wr_dir_args wr_dir_args = {
19914 +               .force_btgt     = -1,
19915 +               .flags          = AuWrDir_TMPFILE
19916 +       };
19917 +
19918 +       /* copy-up may happen */
19919 +       inode_lock(dir);
19920 +
19921 +       sb = dir->i_sb;
19922 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19923 +       if (unlikely(err))
19924 +               goto out;
19925 +
19926 +       err = au_di_init(dentry);
19927 +       if (unlikely(err))
19928 +               goto out_si;
19929 +
19930 +       err = -EBUSY;
19931 +       parent = d_find_any_alias(dir);
19932 +       AuDebugOn(!parent);
19933 +       di_write_lock_parent(parent);
19934 +       if (unlikely(d_inode(parent) != dir))
19935 +               goto out_parent;
19936 +
19937 +       err = au_digen_test(parent, au_sigen(sb));
19938 +       if (unlikely(err))
19939 +               goto out_parent;
19940 +
19941 +       bindex = au_dbtop(parent);
19942 +       au_set_dbtop(dentry, bindex);
19943 +       au_set_dbbot(dentry, bindex);
19944 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19945 +       bindex = err;
19946 +       if (unlikely(err < 0))
19947 +               goto out_parent;
19948 +
19949 +       err = -EOPNOTSUPP;
19950 +       h_dir = au_h_iptr(dir, bindex);
19951 +       if (unlikely(!h_dir->i_op->tmpfile))
19952 +               goto out_parent;
19953 +
19954 +       h_mnt = au_sbr_mnt(sb, bindex);
19955 +       err = vfsub_mnt_want_write(h_mnt);
19956 +       if (unlikely(err))
19957 +               goto out_parent;
19958 +
19959 +       h_userns = mnt_user_ns(h_mnt);
19960 +       h_parent = au_h_dptr(parent, bindex);
19961 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
19962 +       if (IS_ERR(h_dentry)) {
19963 +               err = PTR_ERR(h_dentry);
19964 +               goto out_mnt;
19965 +       }
19966 +
19967 +       au_set_dbtop(dentry, bindex);
19968 +       au_set_dbbot(dentry, bindex);
19969 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
19970 +       inode = au_new_inode(dentry, /*must_new*/1);
19971 +       if (IS_ERR(inode)) {
19972 +               err = PTR_ERR(inode);
19973 +               au_set_h_dptr(dentry, bindex, NULL);
19974 +               au_set_dbtop(dentry, -1);
19975 +               au_set_dbbot(dentry, -1);
19976 +       } else {
19977 +               if (!inode->i_nlink)
19978 +                       set_nlink(inode, 1);
19979 +               d_tmpfile(dentry, inode);
19980 +               au_di(dentry)->di_tmpfile = 1;
19981 +
19982 +               /* update without i_mutex */
19983 +               if (au_ibtop(dir) == au_dbtop(dentry))
19984 +                       au_cpup_attr_timesizes(dir);
19985 +       }
19986 +       dput(h_dentry);
19987 +
19988 +out_mnt:
19989 +       vfsub_mnt_drop_write(h_mnt);
19990 +out_parent:
19991 +       di_write_unlock(parent);
19992 +       dput(parent);
19993 +       di_write_unlock(dentry);
19994 +       if (unlikely(err)) {
19995 +               au_di_fin(dentry);
19996 +               dentry->d_fsdata = NULL;
19997 +       }
19998 +out_si:
19999 +       si_read_unlock(sb);
20000 +out:
20001 +       inode_unlock(dir);
20002 +       return err;
20003 +}
20004 +
20005 +/* ---------------------------------------------------------------------- */
20006 +
20007 +struct au_link_args {
20008 +       aufs_bindex_t bdst, bsrc;
20009 +       struct au_pin pin;
20010 +       struct path h_path;
20011 +       struct dentry *src_parent, *parent;
20012 +};
20013 +
20014 +static int au_cpup_before_link(struct dentry *src_dentry,
20015 +                              struct au_link_args *a)
20016 +{
20017 +       int err;
20018 +       struct dentry *h_src_dentry;
20019 +       struct au_cp_generic cpg = {
20020 +               .dentry = src_dentry,
20021 +               .bdst   = a->bdst,
20022 +               .bsrc   = a->bsrc,
20023 +               .len    = -1,
20024 +               .pin    = &a->pin,
20025 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20026 +       };
20027 +
20028 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20029 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20030 +       if (unlikely(err))
20031 +               goto out;
20032 +
20033 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20034 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20035 +                    au_opt_udba(src_dentry->d_sb),
20036 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20037 +       if (unlikely(err))
20038 +               goto out;
20039 +
20040 +       err = au_sio_cpup_simple(&cpg);
20041 +       au_unpin(&a->pin);
20042 +
20043 +out:
20044 +       di_read_unlock(a->src_parent, AuLock_IR);
20045 +       return err;
20046 +}
20047 +
20048 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20049 +                          struct au_link_args *a)
20050 +{
20051 +       int err;
20052 +       unsigned char plink;
20053 +       aufs_bindex_t bbot;
20054 +       struct dentry *h_src_dentry;
20055 +       struct inode *h_inode, *inode, *delegated;
20056 +       struct super_block *sb;
20057 +       struct file *h_file;
20058 +
20059 +       plink = 0;
20060 +       h_inode = NULL;
20061 +       sb = src_dentry->d_sb;
20062 +       inode = d_inode(src_dentry);
20063 +       if (au_ibtop(inode) <= a->bdst)
20064 +               h_inode = au_h_iptr(inode, a->bdst);
20065 +       if (!h_inode || !h_inode->i_nlink) {
20066 +               /* copyup src_dentry as the name of dentry. */
20067 +               bbot = au_dbbot(dentry);
20068 +               if (bbot < a->bsrc)
20069 +                       au_set_dbbot(dentry, a->bsrc);
20070 +               au_set_h_dptr(dentry, a->bsrc,
20071 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20072 +               dget(a->h_path.dentry);
20073 +               au_set_h_dptr(dentry, a->bdst, NULL);
20074 +               AuDbg("temporary d_inode...\n");
20075 +               spin_lock(&dentry->d_lock);
20076 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20077 +               spin_unlock(&dentry->d_lock);
20078 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20079 +               if (IS_ERR(h_file))
20080 +                       err = PTR_ERR(h_file);
20081 +               else {
20082 +                       struct au_cp_generic cpg = {
20083 +                               .dentry = dentry,
20084 +                               .bdst   = a->bdst,
20085 +                               .bsrc   = -1,
20086 +                               .len    = -1,
20087 +                               .pin    = &a->pin,
20088 +                               .flags  = AuCpup_KEEPLINO
20089 +                       };
20090 +                       err = au_sio_cpup_simple(&cpg);
20091 +                       au_h_open_post(dentry, a->bsrc, h_file);
20092 +                       if (!err) {
20093 +                               dput(a->h_path.dentry);
20094 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20095 +                       } else
20096 +                               au_set_h_dptr(dentry, a->bdst,
20097 +                                             a->h_path.dentry);
20098 +               }
20099 +               spin_lock(&dentry->d_lock);
20100 +               dentry->d_inode = NULL; /* restore */
20101 +               spin_unlock(&dentry->d_lock);
20102 +               AuDbg("temporary d_inode...done\n");
20103 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20104 +               au_set_dbbot(dentry, bbot);
20105 +       } else {
20106 +               /* the inode of src_dentry already exists on a.bdst branch */
20107 +               h_src_dentry = d_find_alias(h_inode);
20108 +               if (!h_src_dentry && au_plink_test(inode)) {
20109 +                       plink = 1;
20110 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20111 +                       err = PTR_ERR(h_src_dentry);
20112 +                       if (IS_ERR(h_src_dentry))
20113 +                               goto out;
20114 +
20115 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20116 +                               dput(h_src_dentry);
20117 +                               h_src_dentry = NULL;
20118 +                       }
20119 +
20120 +               }
20121 +               if (h_src_dentry) {
20122 +                       delegated = NULL;
20123 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20124 +                                        &a->h_path, &delegated);
20125 +                       if (unlikely(err == -EWOULDBLOCK)) {
20126 +                               pr_warn("cannot retry for NFSv4 delegation"
20127 +                                       " for an internal link\n");
20128 +                               iput(delegated);
20129 +                       }
20130 +                       dput(h_src_dentry);
20131 +               } else {
20132 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20133 +                               h_inode->i_ino, a->bdst);
20134 +                       err = -EIO;
20135 +               }
20136 +       }
20137 +
20138 +       if (!err && !plink)
20139 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20140 +
20141 +out:
20142 +       AuTraceErr(err);
20143 +       return err;
20144 +}
20145 +
20146 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20147 +             struct dentry *dentry)
20148 +{
20149 +       int err, rerr;
20150 +       struct au_dtime dt;
20151 +       struct au_link_args *a;
20152 +       struct dentry *wh_dentry, *h_src_dentry;
20153 +       struct inode *inode, *delegated;
20154 +       struct super_block *sb;
20155 +       struct au_wr_dir_args wr_dir_args = {
20156 +               /* .force_btgt  = -1, */
20157 +               .flags          = AuWrDir_ADD_ENTRY
20158 +       };
20159 +
20160 +       IMustLock(dir);
20161 +       inode = d_inode(src_dentry);
20162 +       IMustLock(inode);
20163 +
20164 +       err = -ENOMEM;
20165 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20166 +       if (unlikely(!a))
20167 +               goto out;
20168 +
20169 +       a->parent = dentry->d_parent; /* dir inode is locked */
20170 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20171 +                                       AuLock_NOPLM | AuLock_GEN);
20172 +       if (unlikely(err))
20173 +               goto out_kfree;
20174 +       err = au_d_linkable(src_dentry);
20175 +       if (unlikely(err))
20176 +               goto out_unlock;
20177 +       err = au_d_may_add(dentry);
20178 +       if (unlikely(err))
20179 +               goto out_unlock;
20180 +
20181 +       a->src_parent = dget_parent(src_dentry);
20182 +       wr_dir_args.force_btgt = au_ibtop(inode);
20183 +
20184 +       di_write_lock_parent(a->parent);
20185 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20186 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20187 +                                     &wr_dir_args);
20188 +       err = PTR_ERR(wh_dentry);
20189 +       if (IS_ERR(wh_dentry))
20190 +               goto out_parent;
20191 +
20192 +       err = 0;
20193 +       sb = dentry->d_sb;
20194 +       a->bdst = au_dbtop(dentry);
20195 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20196 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20197 +       a->bsrc = au_ibtop(inode);
20198 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20199 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20200 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20201 +       if (!h_src_dentry) {
20202 +               a->bsrc = au_dbtop(src_dentry);
20203 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20204 +               AuDebugOn(!h_src_dentry);
20205 +       } else if (IS_ERR(h_src_dentry)) {
20206 +               err = PTR_ERR(h_src_dentry);
20207 +               goto out_parent;
20208 +       }
20209 +
20210 +       /*
20211 +        * aufs doesn't touch the credential so
20212 +        * security_dentry_create_files_as() is unnecessary.
20213 +        */
20214 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20215 +               if (a->bdst < a->bsrc
20216 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20217 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20218 +               else {
20219 +                       delegated = NULL;
20220 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20221 +                                        &a->h_path, &delegated);
20222 +                       if (unlikely(err == -EWOULDBLOCK)) {
20223 +                               pr_warn("cannot retry for NFSv4 delegation"
20224 +                                       " for an internal link\n");
20225 +                               iput(delegated);
20226 +                       }
20227 +               }
20228 +               dput(h_src_dentry);
20229 +       } else {
20230 +               /*
20231 +                * copyup src_dentry to the branch we process,
20232 +                * and then link(2) to it.
20233 +                */
20234 +               dput(h_src_dentry);
20235 +               if (a->bdst < a->bsrc
20236 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20237 +                       au_unpin(&a->pin);
20238 +                       di_write_unlock(a->parent);
20239 +                       err = au_cpup_before_link(src_dentry, a);
20240 +                       di_write_lock_parent(a->parent);
20241 +                       if (!err)
20242 +                               err = au_pin(&a->pin, dentry, a->bdst,
20243 +                                            au_opt_udba(sb),
20244 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20245 +                       if (unlikely(err))
20246 +                               goto out_wh;
20247 +               }
20248 +               if (!err) {
20249 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20250 +                       err = -ENOENT;
20251 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20252 +                               delegated = NULL;
20253 +                               err = vfsub_link(h_src_dentry,
20254 +                                                au_pinned_h_dir(&a->pin),
20255 +                                                &a->h_path, &delegated);
20256 +                               if (unlikely(err == -EWOULDBLOCK)) {
20257 +                                       pr_warn("cannot retry"
20258 +                                               " for NFSv4 delegation"
20259 +                                               " for an internal link\n");
20260 +                                       iput(delegated);
20261 +                               }
20262 +                       }
20263 +               }
20264 +       }
20265 +       if (unlikely(err))
20266 +               goto out_unpin;
20267 +
20268 +       if (wh_dentry) {
20269 +               a->h_path.dentry = wh_dentry;
20270 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20271 +                                         dentry);
20272 +               if (unlikely(err))
20273 +                       goto out_revert;
20274 +       }
20275 +
20276 +       au_dir_ts(dir, a->bdst);
20277 +       inode_inc_iversion(dir);
20278 +       inc_nlink(inode);
20279 +       inode->i_ctime = dir->i_ctime;
20280 +       d_instantiate(dentry, au_igrab(inode));
20281 +       if (d_unhashed(a->h_path.dentry))
20282 +               /* some filesystem calls d_drop() */
20283 +               d_drop(dentry);
20284 +       /* some filesystems consume an inode even hardlink */
20285 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20286 +       goto out_unpin; /* success */
20287 +
20288 +out_revert:
20289 +       /* no delegation since it is just created */
20290 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20291 +                           /*delegated*/NULL, /*force*/0);
20292 +       if (unlikely(rerr)) {
20293 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20294 +               err = -EIO;
20295 +       }
20296 +       au_dtime_revert(&dt);
20297 +out_unpin:
20298 +       au_unpin(&a->pin);
20299 +out_wh:
20300 +       dput(wh_dentry);
20301 +out_parent:
20302 +       di_write_unlock(a->parent);
20303 +       dput(a->src_parent);
20304 +out_unlock:
20305 +       if (unlikely(err)) {
20306 +               au_update_dbtop(dentry);
20307 +               d_drop(dentry);
20308 +       }
20309 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20310 +out_kfree:
20311 +       au_kfree_rcu(a);
20312 +out:
20313 +       AuTraceErr(err);
20314 +       return err;
20315 +}
20316 +
20317 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20318 +              struct dentry *dentry, umode_t mode)
20319 +{
20320 +       int err, rerr;
20321 +       aufs_bindex_t bindex;
20322 +       unsigned char diropq;
20323 +       struct path h_path;
20324 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20325 +       struct inode *h_inode;
20326 +       struct super_block *sb;
20327 +       struct {
20328 +               struct au_pin pin;
20329 +               struct au_dtime dt;
20330 +       } *a; /* reduce the stack usage */
20331 +       struct au_wr_dir_args wr_dir_args = {
20332 +               .force_btgt     = -1,
20333 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20334 +       };
20335 +
20336 +       IMustLock(dir);
20337 +
20338 +       err = -ENOMEM;
20339 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20340 +       if (unlikely(!a))
20341 +               goto out;
20342 +
20343 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20344 +       if (unlikely(err))
20345 +               goto out_free;
20346 +       err = au_d_may_add(dentry);
20347 +       if (unlikely(err))
20348 +               goto out_unlock;
20349 +
20350 +       parent = dentry->d_parent; /* dir inode is locked */
20351 +       di_write_lock_parent(parent);
20352 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20353 +                                     &a->pin, &wr_dir_args);
20354 +       err = PTR_ERR(wh_dentry);
20355 +       if (IS_ERR(wh_dentry))
20356 +               goto out_parent;
20357 +
20358 +       sb = dentry->d_sb;
20359 +       bindex = au_dbtop(dentry);
20360 +       h_path.dentry = au_h_dptr(dentry, bindex);
20361 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20362 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20363 +       if (unlikely(err))
20364 +               goto out_unpin;
20365 +
20366 +       /* make the dir opaque */
20367 +       diropq = 0;
20368 +       h_inode = d_inode(h_path.dentry);
20369 +       if (wh_dentry
20370 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20371 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20372 +               opq_dentry = au_diropq_create(dentry, bindex);
20373 +               inode_unlock(h_inode);
20374 +               err = PTR_ERR(opq_dentry);
20375 +               if (IS_ERR(opq_dentry))
20376 +                       goto out_dir;
20377 +               dput(opq_dentry);
20378 +               diropq = 1;
20379 +       }
20380 +
20381 +       err = epilog(dir, bindex, wh_dentry, dentry);
20382 +       if (!err) {
20383 +               inc_nlink(dir);
20384 +               goto out_unpin; /* success */
20385 +       }
20386 +
20387 +       /* revert */
20388 +       if (diropq) {
20389 +               AuLabel(revert opq);
20390 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20391 +               rerr = au_diropq_remove(dentry, bindex);
20392 +               inode_unlock(h_inode);
20393 +               if (rerr) {
20394 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20395 +                               dentry, err, rerr);
20396 +                       err = -EIO;
20397 +               }
20398 +       }
20399 +
20400 +out_dir:
20401 +       AuLabel(revert dir);
20402 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20403 +       if (rerr) {
20404 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20405 +                       dentry, err, rerr);
20406 +               err = -EIO;
20407 +       }
20408 +       au_dtime_revert(&a->dt);
20409 +out_unpin:
20410 +       au_unpin(&a->pin);
20411 +       dput(wh_dentry);
20412 +out_parent:
20413 +       di_write_unlock(parent);
20414 +out_unlock:
20415 +       if (unlikely(err)) {
20416 +               au_update_dbtop(dentry);
20417 +               d_drop(dentry);
20418 +       }
20419 +       aufs_read_unlock(dentry, AuLock_DW);
20420 +out_free:
20421 +       au_kfree_rcu(a);
20422 +out:
20423 +       return err;
20424 +}
20425 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20426 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20427 +++ linux/fs/aufs/i_op.c        2021-12-03 15:40:58.236647297 +0100
20428 @@ -0,0 +1,1513 @@
20429 +// SPDX-License-Identifier: GPL-2.0
20430 +/*
20431 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20432 + *
20433 + * This program, aufs is free software; you can redistribute it and/or modify
20434 + * it under the terms of the GNU General Public License as published by
20435 + * the Free Software Foundation; either version 2 of the License, or
20436 + * (at your option) any later version.
20437 + *
20438 + * This program is distributed in the hope that it will be useful,
20439 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20440 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20441 + * GNU General Public License for more details.
20442 + *
20443 + * You should have received a copy of the GNU General Public License
20444 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20445 + */
20446 +
20447 +/*
20448 + * inode operations (except add/del/rename)
20449 + */
20450 +
20451 +#include <linux/device_cgroup.h>
20452 +#include <linux/fs_stack.h>
20453 +#include <linux/iversion.h>
20454 +#include <linux/namei.h>
20455 +#include <linux/security.h>
20456 +#include "aufs.h"
20457 +
20458 +static int h_permission(struct inode *h_inode, int mask,
20459 +                       struct path *h_path, int brperm)
20460 +{
20461 +       int err;
20462 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20463 +       struct user_namespace *h_userns;
20464 +
20465 +       err = -EPERM;
20466 +       if (write_mask && IS_IMMUTABLE(h_inode))
20467 +               goto out;
20468 +
20469 +       err = -EACCES;
20470 +       if (((mask & MAY_EXEC)
20471 +            && S_ISREG(h_inode->i_mode)
20472 +            && (path_noexec(h_path)
20473 +                || !(h_inode->i_mode & 0111))))
20474 +               goto out;
20475 +
20476 +       /*
20477 +        * - skip the lower fs test in the case of write to ro branch.
20478 +        * - nfs dir permission write check is optimized, but a policy for
20479 +        *   link/rename requires a real check.
20480 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20481 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20482 +        */
20483 +       h_userns = mnt_user_ns(h_path->mnt);
20484 +       if ((write_mask && !au_br_writable(brperm))
20485 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20486 +               && write_mask && !(mask & MAY_READ))
20487 +           || !h_inode->i_op->permission) {
20488 +               /* AuLabel(generic_permission); */
20489 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20490 +               err = generic_permission(h_userns, h_inode, mask);
20491 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20492 +                       err = h_inode->i_op->permission(h_userns, h_inode,
20493 +                                                       mask);
20494 +               AuTraceErr(err);
20495 +       } else {
20496 +               /* AuLabel(h_inode->permission); */
20497 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
20498 +               AuTraceErr(err);
20499 +       }
20500 +
20501 +       if (!err)
20502 +               err = devcgroup_inode_permission(h_inode, mask);
20503 +       if (!err)
20504 +               err = security_inode_permission(h_inode, mask);
20505 +
20506 +out:
20507 +       return err;
20508 +}
20509 +
20510 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
20511 +                          int mask)
20512 +{
20513 +       int err;
20514 +       aufs_bindex_t bindex, bbot;
20515 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20516 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20517 +       struct inode *h_inode;
20518 +       struct super_block *sb;
20519 +       struct au_branch *br;
20520 +
20521 +       /* todo: support rcu-walk? */
20522 +       if (mask & MAY_NOT_BLOCK)
20523 +               return -ECHILD;
20524 +
20525 +       sb = inode->i_sb;
20526 +       si_read_lock(sb, AuLock_FLUSH);
20527 +       ii_read_lock_child(inode);
20528 +#if 0 /* reserved for future use */
20529 +       /*
20530 +        * This test may be rather 'too much' since the test is essentially done
20531 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20532 +        * generation doesn't match to the superblock's here.  But it isn't a
20533 +        * big deal I suppose.
20534 +        */
20535 +       err = au_iigen_test(inode, au_sigen(sb));
20536 +       if (unlikely(err))
20537 +               goto out;
20538 +#endif
20539 +
20540 +       if (!isdir
20541 +           || write_mask
20542 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20543 +               err = au_busy_or_stale();
20544 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20545 +               if (unlikely(!h_inode
20546 +                            || (h_inode->i_mode & S_IFMT)
20547 +                            != (inode->i_mode & S_IFMT)))
20548 +                       goto out;
20549 +
20550 +               err = 0;
20551 +               bindex = au_ibtop(inode);
20552 +               br = au_sbr(sb, bindex);
20553 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20554 +               if (write_mask
20555 +                   && !err
20556 +                   && !special_file(h_inode->i_mode)) {
20557 +                       /* test whether the upper writable branch exists */
20558 +                       err = -EROFS;
20559 +                       for (; bindex >= 0; bindex--)
20560 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20561 +                                       err = 0;
20562 +                                       break;
20563 +                               }
20564 +               }
20565 +               goto out;
20566 +       }
20567 +
20568 +       /* non-write to dir */
20569 +       err = 0;
20570 +       bbot = au_ibbot(inode);
20571 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20572 +               h_inode = au_h_iptr(inode, bindex);
20573 +               if (h_inode) {
20574 +                       err = au_busy_or_stale();
20575 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20576 +                               break;
20577 +
20578 +                       br = au_sbr(sb, bindex);
20579 +                       err = h_permission(h_inode, mask, &br->br_path,
20580 +                                          br->br_perm);
20581 +               }
20582 +       }
20583 +
20584 +out:
20585 +       ii_read_unlock(inode);
20586 +       si_read_unlock(sb);
20587 +       return err;
20588 +}
20589 +
20590 +/* ---------------------------------------------------------------------- */
20591 +
20592 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20593 +                                 unsigned int flags)
20594 +{
20595 +       struct dentry *ret, *parent;
20596 +       struct inode *inode;
20597 +       struct super_block *sb;
20598 +       int err, npositive;
20599 +
20600 +       IMustLock(dir);
20601 +
20602 +       /* todo: support rcu-walk? */
20603 +       ret = ERR_PTR(-ECHILD);
20604 +       if (flags & LOOKUP_RCU)
20605 +               goto out;
20606 +
20607 +       ret = ERR_PTR(-ENAMETOOLONG);
20608 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20609 +               goto out;
20610 +
20611 +       sb = dir->i_sb;
20612 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20613 +       ret = ERR_PTR(err);
20614 +       if (unlikely(err))
20615 +               goto out;
20616 +
20617 +       err = au_di_init(dentry);
20618 +       ret = ERR_PTR(err);
20619 +       if (unlikely(err))
20620 +               goto out_si;
20621 +
20622 +       inode = NULL;
20623 +       npositive = 0; /* suppress a warning */
20624 +       parent = dentry->d_parent; /* dir inode is locked */
20625 +       di_read_lock_parent(parent, AuLock_IR);
20626 +       err = au_alive_dir(parent);
20627 +       if (!err)
20628 +               err = au_digen_test(parent, au_sigen(sb));
20629 +       if (!err) {
20630 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20631 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20632 +                                          AuLkup_ALLOW_NEG);
20633 +               err = npositive;
20634 +       }
20635 +       di_read_unlock(parent, AuLock_IR);
20636 +       ret = ERR_PTR(err);
20637 +       if (unlikely(err < 0))
20638 +               goto out_unlock;
20639 +
20640 +       if (npositive) {
20641 +               inode = au_new_inode(dentry, /*must_new*/0);
20642 +               if (IS_ERR(inode)) {
20643 +                       ret = (void *)inode;
20644 +                       inode = NULL;
20645 +                       goto out_unlock;
20646 +               }
20647 +       }
20648 +
20649 +       if (inode)
20650 +               atomic_inc(&inode->i_count);
20651 +       ret = d_splice_alias(inode, dentry);
20652 +#if 0 /* reserved for future use */
20653 +       if (unlikely(d_need_lookup(dentry))) {
20654 +               spin_lock(&dentry->d_lock);
20655 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20656 +               spin_unlock(&dentry->d_lock);
20657 +       } else
20658 +#endif
20659 +       if (inode) {
20660 +               if (!IS_ERR(ret)) {
20661 +                       iput(inode);
20662 +                       if (ret && ret != dentry)
20663 +                               ii_write_unlock(inode);
20664 +               } else {
20665 +                       ii_write_unlock(inode);
20666 +                       iput(inode);
20667 +                       inode = NULL;
20668 +               }
20669 +       }
20670 +
20671 +out_unlock:
20672 +       di_write_unlock(dentry);
20673 +out_si:
20674 +       si_read_unlock(sb);
20675 +out:
20676 +       return ret;
20677 +}
20678 +
20679 +/* ---------------------------------------------------------------------- */
20680 +
20681 +/*
20682 + * very dirty and complicated aufs ->atomic_open().
20683 + * aufs_atomic_open()
20684 + * + au_aopen_or_create()
20685 + *   + add_simple()
20686 + *     + vfsub_atomic_open()
20687 + *       + branch fs ->atomic_open()
20688 + *        may call the actual 'open' for h_file
20689 + *       + inc br_nfiles only if opened
20690 + * + au_aopen_no_open() or au_aopen_do_open()
20691 + *
20692 + * au_aopen_do_open()
20693 + * + finish_open()
20694 + *   + au_do_aopen()
20695 + *     + au_do_open() the body of all 'open'
20696 + *       + au_do_open_nondir()
20697 + *        set the passed h_file
20698 + *
20699 + * au_aopen_no_open()
20700 + * + finish_no_open()
20701 + */
20702 +
20703 +struct aopen_node {
20704 +       struct hlist_bl_node hblist;
20705 +       struct file *file, *h_file;
20706 +};
20707 +
20708 +static int au_do_aopen(struct inode *inode, struct file *file)
20709 +{
20710 +       struct hlist_bl_head *aopen;
20711 +       struct hlist_bl_node *pos;
20712 +       struct aopen_node *node;
20713 +       struct au_do_open_args args = {
20714 +               .aopen  = 1,
20715 +               .open   = au_do_open_nondir
20716 +       };
20717 +
20718 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20719 +       hlist_bl_lock(aopen);
20720 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20721 +               if (node->file == file) {
20722 +                       args.h_file = node->h_file;
20723 +                       break;
20724 +               }
20725 +       hlist_bl_unlock(aopen);
20726 +       /* AuDebugOn(!args.h_file); */
20727 +
20728 +       return au_do_open(file, &args);
20729 +}
20730 +
20731 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20732 +                           struct aopen_node *aopen_node)
20733 +{
20734 +       int err;
20735 +       struct hlist_bl_head *aopen;
20736 +
20737 +       AuLabel(here);
20738 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20739 +       au_hbl_add(&aopen_node->hblist, aopen);
20740 +       err = finish_open(file, dentry, au_do_aopen);
20741 +       au_hbl_del(&aopen_node->hblist, aopen);
20742 +       /* AuDbgFile(file); */
20743 +       AuDbg("%pd%s%s\n", dentry,
20744 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20745 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20746 +
20747 +       AuTraceErr(err);
20748 +       return err;
20749 +}
20750 +
20751 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20752 +{
20753 +       int err;
20754 +
20755 +       AuLabel(here);
20756 +       dget(dentry);
20757 +       err = finish_no_open(file, dentry);
20758 +
20759 +       AuTraceErr(err);
20760 +       return err;
20761 +}
20762 +
20763 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20764 +                           struct file *file, unsigned int open_flag,
20765 +                           umode_t create_mode)
20766 +{
20767 +       int err, did_open;
20768 +       unsigned int lkup_flags;
20769 +       aufs_bindex_t bindex;
20770 +       struct super_block *sb;
20771 +       struct dentry *parent, *d;
20772 +       struct vfsub_aopen_args args = {
20773 +               .open_flag      = open_flag,
20774 +               .create_mode    = create_mode
20775 +       };
20776 +       struct aopen_node aopen_node = {
20777 +               .file   = file
20778 +       };
20779 +
20780 +       IMustLock(dir);
20781 +       AuDbg("open_flag 0%o\n", open_flag);
20782 +       AuDbgDentry(dentry);
20783 +
20784 +       err = 0;
20785 +       if (!au_di(dentry)) {
20786 +               lkup_flags = LOOKUP_OPEN;
20787 +               if (open_flag & O_CREAT)
20788 +                       lkup_flags |= LOOKUP_CREATE;
20789 +               d = aufs_lookup(dir, dentry, lkup_flags);
20790 +               if (IS_ERR(d)) {
20791 +                       err = PTR_ERR(d);
20792 +                       AuTraceErr(err);
20793 +                       goto out;
20794 +               } else if (d) {
20795 +                       /*
20796 +                        * obsoleted dentry found.
20797 +                        * another error will be returned later.
20798 +                        */
20799 +                       d_drop(d);
20800 +                       AuDbgDentry(d);
20801 +                       dput(d);
20802 +               }
20803 +               AuDbgDentry(dentry);
20804 +       }
20805 +
20806 +       if (d_is_positive(dentry)
20807 +           || d_unhashed(dentry)
20808 +           || d_unlinked(dentry)
20809 +           || !(open_flag & O_CREAT)) {
20810 +               err = au_aopen_no_open(file, dentry);
20811 +               goto out; /* success */
20812 +       }
20813 +
20814 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20815 +       if (unlikely(err))
20816 +               goto out;
20817 +
20818 +       sb = dentry->d_sb;
20819 +       parent = dentry->d_parent;      /* dir is locked */
20820 +       di_write_lock_parent(parent);
20821 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20822 +       if (unlikely(err < 0))
20823 +               goto out_parent;
20824 +
20825 +       AuDbgDentry(dentry);
20826 +       if (d_is_positive(dentry)) {
20827 +               err = au_aopen_no_open(file, dentry);
20828 +               goto out_parent; /* success */
20829 +       }
20830 +
20831 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20832 +       err = PTR_ERR(args.file);
20833 +       if (IS_ERR(args.file))
20834 +               goto out_parent;
20835 +
20836 +       bindex = au_dbtop(dentry);
20837 +       err = au_aopen_or_create(dir, dentry, &args);
20838 +       AuTraceErr(err);
20839 +       AuDbgFile(args.file);
20840 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20841 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20842 +       if (!did_open) {
20843 +               fput(args.file);
20844 +               args.file = NULL;
20845 +       }
20846 +       di_write_unlock(parent);
20847 +       di_write_unlock(dentry);
20848 +       if (unlikely(err < 0)) {
20849 +               if (args.file)
20850 +                       fput(args.file);
20851 +               goto out_sb;
20852 +       }
20853 +
20854 +       if (!did_open)
20855 +               err = au_aopen_no_open(file, dentry);
20856 +       else {
20857 +               aopen_node.h_file = args.file;
20858 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20859 +       }
20860 +       if (unlikely(err < 0)) {
20861 +               if (args.file)
20862 +                       fput(args.file);
20863 +               if (did_open)
20864 +                       au_lcnt_dec(&args.br->br_nfiles);
20865 +       }
20866 +       goto out_sb; /* success */
20867 +
20868 +out_parent:
20869 +       di_write_unlock(parent);
20870 +       di_write_unlock(dentry);
20871 +out_sb:
20872 +       si_read_unlock(sb);
20873 +out:
20874 +       AuTraceErr(err);
20875 +       AuDbgFile(file);
20876 +       return err;
20877 +}
20878 +
20879 +
20880 +/* ---------------------------------------------------------------------- */
20881 +
20882 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20883 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
20884 +                         aufs_bindex_t btop)
20885 +{
20886 +       int err;
20887 +       struct dentry *h_parent;
20888 +       struct inode *h_dir;
20889 +
20890 +       if (add_entry)
20891 +               IMustLock(d_inode(parent));
20892 +       else
20893 +               di_write_lock_parent(parent);
20894 +
20895 +       err = 0;
20896 +       if (!au_h_dptr(parent, bcpup)) {
20897 +               if (btop > bcpup)
20898 +                       err = au_cpup_dirs(dentry, bcpup);
20899 +               else if (btop < bcpup)
20900 +                       err = au_cpdown_dirs(dentry, bcpup);
20901 +               else
20902 +                       BUG();
20903 +       }
20904 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20905 +               h_parent = au_h_dptr(parent, bcpup);
20906 +               h_dir = d_inode(h_parent);
20907 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20908 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20909 +               /* todo: no unlock here */
20910 +               inode_unlock_shared(h_dir);
20911 +
20912 +               AuDbg("bcpup %d\n", bcpup);
20913 +               if (!err) {
20914 +                       if (d_really_is_negative(dentry))
20915 +                               au_set_h_dptr(dentry, btop, NULL);
20916 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
20917 +               }
20918 +       }
20919 +
20920 +       if (!add_entry)
20921 +               di_write_unlock(parent);
20922 +       if (!err)
20923 +               err = bcpup; /* success */
20924 +
20925 +       AuTraceErr(err);
20926 +       return err;
20927 +}
20928 +
20929 +/*
20930 + * decide the branch and the parent dir where we will create a new entry.
20931 + * returns new bindex or an error.
20932 + * copyup the parent dir if needed.
20933 + */
20934 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20935 +             struct au_wr_dir_args *args)
20936 +{
20937 +       int err;
20938 +       unsigned int flags;
20939 +       aufs_bindex_t bcpup, btop, src_btop;
20940 +       const unsigned char add_entry
20941 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
20942 +               | au_ftest_wrdir(args->flags, TMPFILE);
20943 +       struct super_block *sb;
20944 +       struct dentry *parent;
20945 +       struct au_sbinfo *sbinfo;
20946 +
20947 +       sb = dentry->d_sb;
20948 +       sbinfo = au_sbi(sb);
20949 +       parent = dget_parent(dentry);
20950 +       btop = au_dbtop(dentry);
20951 +       bcpup = btop;
20952 +       if (args->force_btgt < 0) {
20953 +               if (src_dentry) {
20954 +                       src_btop = au_dbtop(src_dentry);
20955 +                       if (src_btop < btop)
20956 +                               bcpup = src_btop;
20957 +               } else if (add_entry) {
20958 +                       flags = 0;
20959 +                       if (au_ftest_wrdir(args->flags, ISDIR))
20960 +                               au_fset_wbr(flags, DIR);
20961 +                       err = AuWbrCreate(sbinfo, dentry, flags);
20962 +                       bcpup = err;
20963 +               }
20964 +
20965 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
20966 +                       if (add_entry)
20967 +                               err = AuWbrCopyup(sbinfo, dentry);
20968 +                       else {
20969 +                               if (!IS_ROOT(dentry)) {
20970 +                                       di_read_lock_parent(parent, !AuLock_IR);
20971 +                                       err = AuWbrCopyup(sbinfo, dentry);
20972 +                                       di_read_unlock(parent, !AuLock_IR);
20973 +                               } else
20974 +                                       err = AuWbrCopyup(sbinfo, dentry);
20975 +                       }
20976 +                       bcpup = err;
20977 +                       if (unlikely(err < 0))
20978 +                               goto out;
20979 +               }
20980 +       } else {
20981 +               bcpup = args->force_btgt;
20982 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
20983 +       }
20984 +
20985 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
20986 +       err = bcpup;
20987 +       if (bcpup == btop)
20988 +               goto out; /* success */
20989 +
20990 +       /* copyup the new parent into the branch we process */
20991 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
20992 +       if (err >= 0) {
20993 +               if (d_really_is_negative(dentry)) {
20994 +                       au_set_h_dptr(dentry, btop, NULL);
20995 +                       au_set_dbtop(dentry, bcpup);
20996 +                       au_set_dbbot(dentry, bcpup);
20997 +               }
20998 +               AuDebugOn(add_entry
20999 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21000 +                         && !au_h_dptr(dentry, bcpup));
21001 +       }
21002 +
21003 +out:
21004 +       dput(parent);
21005 +       return err;
21006 +}
21007 +
21008 +/* ---------------------------------------------------------------------- */
21009 +
21010 +void au_pin_hdir_unlock(struct au_pin *p)
21011 +{
21012 +       if (p->hdir)
21013 +               au_hn_inode_unlock(p->hdir);
21014 +}
21015 +
21016 +int au_pin_hdir_lock(struct au_pin *p)
21017 +{
21018 +       int err;
21019 +
21020 +       err = 0;
21021 +       if (!p->hdir)
21022 +               goto out;
21023 +
21024 +       /* even if an error happens later, keep this lock */
21025 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21026 +
21027 +       err = -EBUSY;
21028 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21029 +               goto out;
21030 +
21031 +       err = 0;
21032 +       if (p->h_dentry)
21033 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21034 +                                 p->h_parent, p->br);
21035 +
21036 +out:
21037 +       return err;
21038 +}
21039 +
21040 +int au_pin_hdir_relock(struct au_pin *p)
21041 +{
21042 +       int err, i;
21043 +       struct inode *h_i;
21044 +       struct dentry *h_d[] = {
21045 +               p->h_dentry,
21046 +               p->h_parent
21047 +       };
21048 +
21049 +       err = au_pin_hdir_lock(p);
21050 +       if (unlikely(err))
21051 +               goto out;
21052 +
21053 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21054 +               if (!h_d[i])
21055 +                       continue;
21056 +               if (d_is_positive(h_d[i])) {
21057 +                       h_i = d_inode(h_d[i]);
21058 +                       err = !h_i->i_nlink;
21059 +               }
21060 +       }
21061 +
21062 +out:
21063 +       return err;
21064 +}
21065 +
21066 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21067 +{
21068 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21069 +}
21070 +
21071 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21072 +{
21073 +       if (p->hdir) {
21074 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21075 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21076 +               au_pin_hdir_set_owner(p, current);
21077 +       }
21078 +}
21079 +
21080 +void au_pin_hdir_release(struct au_pin *p)
21081 +{
21082 +       if (p->hdir) {
21083 +               au_pin_hdir_set_owner(p, p->task);
21084 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21085 +       }
21086 +}
21087 +
21088 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21089 +{
21090 +       if (pin && pin->parent)
21091 +               return au_h_dptr(pin->parent, pin->bindex);
21092 +       return NULL;
21093 +}
21094 +
21095 +void au_unpin(struct au_pin *p)
21096 +{
21097 +       if (p->hdir)
21098 +               au_pin_hdir_unlock(p);
21099 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21100 +               vfsub_mnt_drop_write(p->h_mnt);
21101 +       if (!p->hdir)
21102 +               return;
21103 +
21104 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21105 +               di_read_unlock(p->parent, AuLock_IR);
21106 +       iput(p->hdir->hi_inode);
21107 +       dput(p->parent);
21108 +       p->parent = NULL;
21109 +       p->hdir = NULL;
21110 +       p->h_mnt = NULL;
21111 +       /* do not clear p->task */
21112 +}
21113 +
21114 +int au_do_pin(struct au_pin *p)
21115 +{
21116 +       int err;
21117 +       struct super_block *sb;
21118 +       struct inode *h_dir;
21119 +
21120 +       err = 0;
21121 +       sb = p->dentry->d_sb;
21122 +       p->br = au_sbr(sb, p->bindex);
21123 +       if (IS_ROOT(p->dentry)) {
21124 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21125 +                       p->h_mnt = au_br_mnt(p->br);
21126 +                       err = vfsub_mnt_want_write(p->h_mnt);
21127 +                       if (unlikely(err)) {
21128 +                               au_fclr_pin(p->flags, MNT_WRITE);
21129 +                               goto out_err;
21130 +                       }
21131 +               }
21132 +               goto out;
21133 +       }
21134 +
21135 +       p->h_dentry = NULL;
21136 +       if (p->bindex <= au_dbbot(p->dentry))
21137 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21138 +
21139 +       p->parent = dget_parent(p->dentry);
21140 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21141 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21142 +
21143 +       h_dir = NULL;
21144 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21145 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21146 +       if (p->hdir)
21147 +               h_dir = p->hdir->hi_inode;
21148 +
21149 +       /*
21150 +        * udba case, or
21151 +        * if DI_LOCKED is not set, then p->parent may be different
21152 +        * and h_parent can be NULL.
21153 +        */
21154 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21155 +               err = -EBUSY;
21156 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21157 +                       di_read_unlock(p->parent, AuLock_IR);
21158 +               dput(p->parent);
21159 +               p->parent = NULL;
21160 +               goto out_err;
21161 +       }
21162 +
21163 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21164 +               p->h_mnt = au_br_mnt(p->br);
21165 +               err = vfsub_mnt_want_write(p->h_mnt);
21166 +               if (unlikely(err)) {
21167 +                       au_fclr_pin(p->flags, MNT_WRITE);
21168 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21169 +                               di_read_unlock(p->parent, AuLock_IR);
21170 +                       dput(p->parent);
21171 +                       p->parent = NULL;
21172 +                       goto out_err;
21173 +               }
21174 +       }
21175 +
21176 +       au_igrab(h_dir);
21177 +       err = au_pin_hdir_lock(p);
21178 +       if (!err)
21179 +               goto out; /* success */
21180 +
21181 +       au_unpin(p);
21182 +
21183 +out_err:
21184 +       pr_err("err %d\n", err);
21185 +       err = au_busy_or_stale();
21186 +out:
21187 +       return err;
21188 +}
21189 +
21190 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21191 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21192 +                unsigned int udba, unsigned char flags)
21193 +{
21194 +       p->dentry = dentry;
21195 +       p->udba = udba;
21196 +       p->lsc_di = lsc_di;
21197 +       p->lsc_hi = lsc_hi;
21198 +       p->flags = flags;
21199 +       p->bindex = bindex;
21200 +
21201 +       p->parent = NULL;
21202 +       p->hdir = NULL;
21203 +       p->h_mnt = NULL;
21204 +
21205 +       p->h_dentry = NULL;
21206 +       p->h_parent = NULL;
21207 +       p->br = NULL;
21208 +       p->task = current;
21209 +}
21210 +
21211 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21212 +          unsigned int udba, unsigned char flags)
21213 +{
21214 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21215 +                   udba, flags);
21216 +       return au_do_pin(pin);
21217 +}
21218 +
21219 +/* ---------------------------------------------------------------------- */
21220 +
21221 +/*
21222 + * ->setattr() and ->getattr() are called in various cases.
21223 + * chmod, stat: dentry is revalidated.
21224 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21225 + *               unhashed.
21226 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21227 + */
21228 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21229 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21230 +{
21231 +       int err;
21232 +       struct dentry *parent;
21233 +
21234 +       err = 0;
21235 +       if (au_digen_test(dentry, sigen)) {
21236 +               parent = dget_parent(dentry);
21237 +               di_read_lock_parent(parent, AuLock_IR);
21238 +               err = au_refresh_dentry(dentry, parent);
21239 +               di_read_unlock(parent, AuLock_IR);
21240 +               dput(parent);
21241 +       }
21242 +
21243 +       AuTraceErr(err);
21244 +       return err;
21245 +}
21246 +
21247 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21248 +                    struct au_icpup_args *a)
21249 +{
21250 +       int err;
21251 +       loff_t sz;
21252 +       aufs_bindex_t btop, ibtop;
21253 +       struct dentry *hi_wh, *parent;
21254 +       struct inode *inode;
21255 +       struct au_wr_dir_args wr_dir_args = {
21256 +               .force_btgt     = -1,
21257 +               .flags          = 0
21258 +       };
21259 +
21260 +       if (d_is_dir(dentry))
21261 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21262 +       /* plink or hi_wh() case */
21263 +       btop = au_dbtop(dentry);
21264 +       inode = d_inode(dentry);
21265 +       ibtop = au_ibtop(inode);
21266 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21267 +               wr_dir_args.force_btgt = ibtop;
21268 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21269 +       if (unlikely(err < 0))
21270 +               goto out;
21271 +       a->btgt = err;
21272 +       if (err != btop)
21273 +               au_fset_icpup(a->flags, DID_CPUP);
21274 +
21275 +       err = 0;
21276 +       a->pin_flags = AuPin_MNT_WRITE;
21277 +       parent = NULL;
21278 +       if (!IS_ROOT(dentry)) {
21279 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21280 +               parent = dget_parent(dentry);
21281 +               di_write_lock_parent(parent);
21282 +       }
21283 +
21284 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21285 +       if (unlikely(err))
21286 +               goto out_parent;
21287 +
21288 +       sz = -1;
21289 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21290 +       a->h_inode = d_inode(a->h_path.dentry);
21291 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21292 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21293 +               if (ia->ia_size < i_size_read(a->h_inode))
21294 +                       sz = ia->ia_size;
21295 +               inode_unlock_shared(a->h_inode);
21296 +       }
21297 +
21298 +       hi_wh = NULL;
21299 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21300 +               hi_wh = au_hi_wh(inode, a->btgt);
21301 +               if (!hi_wh) {
21302 +                       struct au_cp_generic cpg = {
21303 +                               .dentry = dentry,
21304 +                               .bdst   = a->btgt,
21305 +                               .bsrc   = -1,
21306 +                               .len    = sz,
21307 +                               .pin    = &a->pin
21308 +                       };
21309 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21310 +                       if (unlikely(err))
21311 +                               goto out_unlock;
21312 +                       hi_wh = au_hi_wh(inode, a->btgt);
21313 +                       /* todo: revalidate hi_wh? */
21314 +               }
21315 +       }
21316 +
21317 +       if (parent) {
21318 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21319 +               di_downgrade_lock(parent, AuLock_IR);
21320 +               dput(parent);
21321 +               parent = NULL;
21322 +       }
21323 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21324 +               goto out; /* success */
21325 +
21326 +       if (!d_unhashed(dentry)) {
21327 +               struct au_cp_generic cpg = {
21328 +                       .dentry = dentry,
21329 +                       .bdst   = a->btgt,
21330 +                       .bsrc   = btop,
21331 +                       .len    = sz,
21332 +                       .pin    = &a->pin,
21333 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21334 +               };
21335 +               err = au_sio_cpup_simple(&cpg);
21336 +               if (!err)
21337 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21338 +       } else if (!hi_wh)
21339 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21340 +       else
21341 +               a->h_path.dentry = hi_wh; /* do not dget here */
21342 +
21343 +out_unlock:
21344 +       a->h_inode = d_inode(a->h_path.dentry);
21345 +       if (!err)
21346 +               goto out; /* success */
21347 +       au_unpin(&a->pin);
21348 +out_parent:
21349 +       if (parent) {
21350 +               di_write_unlock(parent);
21351 +               dput(parent);
21352 +       }
21353 +out:
21354 +       if (!err)
21355 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21356 +       return err;
21357 +}
21358 +
21359 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
21360 +                       struct iattr *ia)
21361 +{
21362 +       int err;
21363 +       struct inode *inode, *delegated;
21364 +       struct super_block *sb;
21365 +       struct file *file;
21366 +       struct au_icpup_args *a;
21367 +       struct user_namespace *h_userns;
21368 +
21369 +       inode = d_inode(dentry);
21370 +       IMustLock(inode);
21371 +
21372 +       err = setattr_prepare(userns, dentry, ia);
21373 +       if (unlikely(err))
21374 +               goto out;
21375 +
21376 +       err = -ENOMEM;
21377 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21378 +       if (unlikely(!a))
21379 +               goto out;
21380 +
21381 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21382 +               ia->ia_valid &= ~ATTR_MODE;
21383 +
21384 +       file = NULL;
21385 +       sb = dentry->d_sb;
21386 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21387 +       if (unlikely(err))
21388 +               goto out_kfree;
21389 +
21390 +       if (ia->ia_valid & ATTR_FILE) {
21391 +               /* currently ftruncate(2) only */
21392 +               AuDebugOn(!d_is_reg(dentry));
21393 +               file = ia->ia_file;
21394 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21395 +                                           /*fi_lsc*/0);
21396 +               if (unlikely(err))
21397 +                       goto out_si;
21398 +               ia->ia_file = au_hf_top(file);
21399 +               a->udba = AuOpt_UDBA_NONE;
21400 +       } else {
21401 +               /* fchmod() doesn't pass ia_file */
21402 +               a->udba = au_opt_udba(sb);
21403 +               di_write_lock_child(dentry);
21404 +               /* no d_unlinked(), to set UDBA_NONE for root */
21405 +               if (d_unhashed(dentry))
21406 +                       a->udba = AuOpt_UDBA_NONE;
21407 +               if (a->udba != AuOpt_UDBA_NONE) {
21408 +                       AuDebugOn(IS_ROOT(dentry));
21409 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21410 +                       if (unlikely(err))
21411 +                               goto out_dentry;
21412 +               }
21413 +       }
21414 +
21415 +       err = au_pin_and_icpup(dentry, ia, a);
21416 +       if (unlikely(err < 0))
21417 +               goto out_dentry;
21418 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21419 +               ia->ia_file = NULL;
21420 +               ia->ia_valid &= ~ATTR_FILE;
21421 +       }
21422 +
21423 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21424 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21425 +           == (ATTR_MODE | ATTR_CTIME)) {
21426 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21427 +               if (unlikely(err))
21428 +                       goto out_unlock;
21429 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21430 +                  && (ia->ia_valid & ATTR_CTIME)) {
21431 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21432 +               if (unlikely(err))
21433 +                       goto out_unlock;
21434 +       }
21435 +
21436 +       if (ia->ia_valid & ATTR_SIZE) {
21437 +               struct file *f;
21438 +
21439 +               if (ia->ia_size < i_size_read(inode))
21440 +                       /* unmap only */
21441 +                       truncate_setsize(inode, ia->ia_size);
21442 +
21443 +               f = NULL;
21444 +               if (ia->ia_valid & ATTR_FILE)
21445 +                       f = ia->ia_file;
21446 +               inode_unlock(a->h_inode);
21447 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21448 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21449 +       } else {
21450 +               delegated = NULL;
21451 +               while (1) {
21452 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21453 +                       if (delegated) {
21454 +                               err = break_deleg_wait(&delegated);
21455 +                               if (!err)
21456 +                                       continue;
21457 +                       }
21458 +                       break;
21459 +               }
21460 +       }
21461 +       /*
21462 +        * regardless aufs 'acl' option setting.
21463 +        * why don't all acl-aware fs call this func from their ->setattr()?
21464 +        */
21465 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
21466 +               h_userns = mnt_user_ns(a->h_path.mnt);
21467 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
21468 +       }
21469 +       if (!err)
21470 +               au_cpup_attr_changeable(inode);
21471 +
21472 +out_unlock:
21473 +       inode_unlock(a->h_inode);
21474 +       au_unpin(&a->pin);
21475 +       if (unlikely(err))
21476 +               au_update_dbtop(dentry);
21477 +out_dentry:
21478 +       di_write_unlock(dentry);
21479 +       if (file) {
21480 +               fi_write_unlock(file);
21481 +               ia->ia_file = file;
21482 +               ia->ia_valid |= ATTR_FILE;
21483 +       }
21484 +out_si:
21485 +       si_read_unlock(sb);
21486 +out_kfree:
21487 +       au_kfree_rcu(a);
21488 +out:
21489 +       AuTraceErr(err);
21490 +       return err;
21491 +}
21492 +
21493 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21494 +static int au_h_path_to_set_attr(struct dentry *dentry,
21495 +                                struct au_icpup_args *a, struct path *h_path)
21496 +{
21497 +       int err;
21498 +       struct super_block *sb;
21499 +
21500 +       sb = dentry->d_sb;
21501 +       a->udba = au_opt_udba(sb);
21502 +       /* no d_unlinked(), to set UDBA_NONE for root */
21503 +       if (d_unhashed(dentry))
21504 +               a->udba = AuOpt_UDBA_NONE;
21505 +       if (a->udba != AuOpt_UDBA_NONE) {
21506 +               AuDebugOn(IS_ROOT(dentry));
21507 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21508 +               if (unlikely(err))
21509 +                       goto out;
21510 +       }
21511 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21512 +       if (unlikely(err < 0))
21513 +               goto out;
21514 +
21515 +       h_path->dentry = a->h_path.dentry;
21516 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21517 +
21518 +out:
21519 +       return err;
21520 +}
21521 +
21522 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21523 +                 struct au_sxattr *arg)
21524 +{
21525 +       int err;
21526 +       struct path h_path;
21527 +       struct super_block *sb;
21528 +       struct au_icpup_args *a;
21529 +       struct inode *h_inode;
21530 +       struct user_namespace *h_userns;
21531 +
21532 +       IMustLock(inode);
21533 +
21534 +       err = -ENOMEM;
21535 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21536 +       if (unlikely(!a))
21537 +               goto out;
21538 +
21539 +       sb = dentry->d_sb;
21540 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21541 +       if (unlikely(err))
21542 +               goto out_kfree;
21543 +
21544 +       h_path.dentry = NULL;   /* silence gcc */
21545 +       di_write_lock_child(dentry);
21546 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21547 +       if (unlikely(err))
21548 +               goto out_di;
21549 +       h_userns = mnt_user_ns(h_path.mnt);
21550 +
21551 +       inode_unlock(a->h_inode);
21552 +       switch (arg->type) {
21553 +       case AU_XATTR_SET:
21554 +               AuDebugOn(d_is_negative(h_path.dentry));
21555 +               err = vfsub_setxattr(h_userns, h_path.dentry,
21556 +                                    arg->u.set.name, arg->u.set.value,
21557 +                                    arg->u.set.size, arg->u.set.flags);
21558 +               break;
21559 +       case AU_ACL_SET:
21560 +               err = -EOPNOTSUPP;
21561 +               h_inode = d_inode(h_path.dentry);
21562 +               if (h_inode->i_op->set_acl) {
21563 +                       /* this will call posix_acl_update_mode */
21564 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
21565 +                                                    arg->u.acl_set.acl,
21566 +                                                    arg->u.acl_set.type);
21567 +               }
21568 +               break;
21569 +       }
21570 +       if (!err)
21571 +               au_cpup_attr_timesizes(inode);
21572 +
21573 +       au_unpin(&a->pin);
21574 +       if (unlikely(err))
21575 +               au_update_dbtop(dentry);
21576 +
21577 +out_di:
21578 +       di_write_unlock(dentry);
21579 +       si_read_unlock(sb);
21580 +out_kfree:
21581 +       au_kfree_rcu(a);
21582 +out:
21583 +       AuTraceErr(err);
21584 +       return err;
21585 +}
21586 +#endif
21587 +
21588 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21589 +                            unsigned int nlink)
21590 +{
21591 +       unsigned int n;
21592 +
21593 +       inode->i_mode = st->mode;
21594 +       /* don't i_[ug]id_write() here */
21595 +       inode->i_uid = st->uid;
21596 +       inode->i_gid = st->gid;
21597 +       inode->i_atime = st->atime;
21598 +       inode->i_mtime = st->mtime;
21599 +       inode->i_ctime = st->ctime;
21600 +
21601 +       au_cpup_attr_nlink(inode, /*force*/0);
21602 +       if (S_ISDIR(inode->i_mode)) {
21603 +               n = inode->i_nlink;
21604 +               n -= nlink;
21605 +               n += st->nlink;
21606 +               smp_mb(); /* for i_nlink */
21607 +               /* 0 can happen */
21608 +               set_nlink(inode, n);
21609 +       }
21610 +
21611 +       spin_lock(&inode->i_lock);
21612 +       inode->i_blocks = st->blocks;
21613 +       i_size_write(inode, st->size);
21614 +       spin_unlock(&inode->i_lock);
21615 +}
21616 +
21617 +/*
21618 + * common routine for aufs_getattr() and au_getxattr().
21619 + * returns zero or negative (an error).
21620 + * @dentry will be read-locked in success.
21621 + */
21622 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21623 +                     struct path *h_path, int locked)
21624 +{
21625 +       int err;
21626 +       unsigned int mnt_flags, sigen;
21627 +       unsigned char udba_none;
21628 +       aufs_bindex_t bindex;
21629 +       struct super_block *sb, *h_sb;
21630 +
21631 +       h_path->mnt = NULL;
21632 +       h_path->dentry = NULL;
21633 +
21634 +       err = 0;
21635 +       sb = dentry->d_sb;
21636 +       mnt_flags = au_mntflags(sb);
21637 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21638 +
21639 +       if (unlikely(locked))
21640 +               goto body; /* skip locking dinfo */
21641 +
21642 +       /* support fstat(2) */
21643 +       if (!d_unlinked(dentry) && !udba_none) {
21644 +               sigen = au_sigen(sb);
21645 +               err = au_digen_test(dentry, sigen);
21646 +               if (!err) {
21647 +                       di_read_lock_child(dentry, AuLock_IR);
21648 +                       err = au_dbrange_test(dentry);
21649 +                       if (unlikely(err)) {
21650 +                               di_read_unlock(dentry, AuLock_IR);
21651 +                               goto out;
21652 +                       }
21653 +               } else {
21654 +                       AuDebugOn(IS_ROOT(dentry));
21655 +                       di_write_lock_child(dentry);
21656 +                       err = au_dbrange_test(dentry);
21657 +                       if (!err)
21658 +                               err = au_reval_for_attr(dentry, sigen);
21659 +                       if (!err)
21660 +                               di_downgrade_lock(dentry, AuLock_IR);
21661 +                       else {
21662 +                               di_write_unlock(dentry);
21663 +                               goto out;
21664 +                       }
21665 +               }
21666 +       } else
21667 +               di_read_lock_child(dentry, AuLock_IR);
21668 +
21669 +body:
21670 +       if (!inode) {
21671 +               inode = d_inode(dentry);
21672 +               if (unlikely(!inode))
21673 +                       goto out;
21674 +       }
21675 +       bindex = au_ibtop(inode);
21676 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21677 +       h_sb = h_path->mnt->mnt_sb;
21678 +       if (!force
21679 +           && !au_test_fs_bad_iattr(h_sb)
21680 +           && udba_none)
21681 +               goto out; /* success */
21682 +
21683 +       if (au_dbtop(dentry) == bindex)
21684 +               h_path->dentry = au_h_dptr(dentry, bindex);
21685 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21686 +               h_path->dentry = au_plink_lkup(inode, bindex);
21687 +               if (IS_ERR(h_path->dentry))
21688 +                       /* pretending success */
21689 +                       h_path->dentry = NULL;
21690 +               else
21691 +                       dput(h_path->dentry);
21692 +       }
21693 +
21694 +out:
21695 +       return err;
21696 +}
21697 +
21698 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
21699 +                       struct kstat *st, u32 request, unsigned int query)
21700 +{
21701 +       int err;
21702 +       unsigned char positive;
21703 +       struct path h_path;
21704 +       struct dentry *dentry;
21705 +       struct inode *inode;
21706 +       struct super_block *sb;
21707 +
21708 +       dentry = path->dentry;
21709 +       inode = d_inode(dentry);
21710 +       sb = dentry->d_sb;
21711 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21712 +       if (unlikely(err))
21713 +               goto out;
21714 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21715 +                               /*locked*/0);
21716 +       if (unlikely(err))
21717 +               goto out_si;
21718 +       if (unlikely(!h_path.dentry))
21719 +               /* illegally overlapped or something */
21720 +               goto out_fill; /* pretending success */
21721 +
21722 +       positive = d_is_positive(h_path.dentry);
21723 +       if (positive)
21724 +               /* no vfsub version */
21725 +               err = vfs_getattr(&h_path, st, request, query);
21726 +       if (!err) {
21727 +               if (positive)
21728 +                       au_refresh_iattr(inode, st,
21729 +                                        d_inode(h_path.dentry)->i_nlink);
21730 +               goto out_fill; /* success */
21731 +       }
21732 +       AuTraceErr(err);
21733 +       goto out_di;
21734 +
21735 +out_fill:
21736 +       generic_fillattr(userns, inode, st);
21737 +out_di:
21738 +       di_read_unlock(dentry, AuLock_IR);
21739 +out_si:
21740 +       si_read_unlock(sb);
21741 +out:
21742 +       AuTraceErr(err);
21743 +       return err;
21744 +}
21745 +
21746 +/* ---------------------------------------------------------------------- */
21747 +
21748 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21749 +                                struct delayed_call *done)
21750 +{
21751 +       const char *ret;
21752 +       struct dentry *h_dentry;
21753 +       struct inode *h_inode;
21754 +       int err;
21755 +       aufs_bindex_t bindex;
21756 +
21757 +       ret = NULL; /* suppress a warning */
21758 +       err = -ECHILD;
21759 +       if (!dentry)
21760 +               goto out;
21761 +
21762 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21763 +       if (unlikely(err))
21764 +               goto out;
21765 +
21766 +       err = au_d_hashed_positive(dentry);
21767 +       if (unlikely(err))
21768 +               goto out_unlock;
21769 +
21770 +       err = -EINVAL;
21771 +       inode = d_inode(dentry);
21772 +       bindex = au_ibtop(inode);
21773 +       h_inode = au_h_iptr(inode, bindex);
21774 +       if (unlikely(!h_inode->i_op->get_link))
21775 +               goto out_unlock;
21776 +
21777 +       err = -EBUSY;
21778 +       h_dentry = NULL;
21779 +       if (au_dbtop(dentry) <= bindex) {
21780 +               h_dentry = au_h_dptr(dentry, bindex);
21781 +               if (h_dentry)
21782 +                       dget(h_dentry);
21783 +       }
21784 +       if (!h_dentry) {
21785 +               h_dentry = d_find_any_alias(h_inode);
21786 +               if (IS_ERR(h_dentry)) {
21787 +                       err = PTR_ERR(h_dentry);
21788 +                       goto out_unlock;
21789 +               }
21790 +       }
21791 +       if (unlikely(!h_dentry))
21792 +               goto out_unlock;
21793 +
21794 +       err = 0;
21795 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21796 +       AuDbgDentry(h_dentry);
21797 +       ret = vfs_get_link(h_dentry, done);
21798 +       dput(h_dentry);
21799 +       if (IS_ERR(ret))
21800 +               err = PTR_ERR(ret);
21801 +
21802 +out_unlock:
21803 +       aufs_read_unlock(dentry, AuLock_IR);
21804 +out:
21805 +       if (unlikely(err))
21806 +               ret = ERR_PTR(err);
21807 +       AuTraceErrPtr(ret);
21808 +       return ret;
21809 +}
21810 +
21811 +/* ---------------------------------------------------------------------- */
21812 +
21813 +static int au_is_special(struct inode *inode)
21814 +{
21815 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21816 +}
21817 +
21818 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21819 +                           int flags)
21820 +{
21821 +       int err;
21822 +       aufs_bindex_t bindex;
21823 +       struct super_block *sb;
21824 +       struct inode *h_inode;
21825 +       struct vfsmount *h_mnt;
21826 +
21827 +       sb = inode->i_sb;
21828 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21829 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21830 +
21831 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21832 +       lockdep_off();
21833 +       si_read_lock(sb, AuLock_FLUSH);
21834 +       ii_write_lock_child(inode);
21835 +
21836 +       err = 0;
21837 +       bindex = au_ibtop(inode);
21838 +       h_inode = au_h_iptr(inode, bindex);
21839 +       if (!au_test_ro(sb, bindex, inode)) {
21840 +               h_mnt = au_sbr_mnt(sb, bindex);
21841 +               err = vfsub_mnt_want_write(h_mnt);
21842 +               if (!err) {
21843 +                       err = vfsub_update_time(h_inode, ts, flags);
21844 +                       vfsub_mnt_drop_write(h_mnt);
21845 +               }
21846 +       } else if (au_is_special(h_inode)) {
21847 +               /*
21848 +                * Never copy-up here.
21849 +                * These special files may already be opened and used for
21850 +                * communicating. If we copied it up, then the communication
21851 +                * would be corrupted.
21852 +                */
21853 +               AuWarn1("timestamps for i%lu are ignored "
21854 +                       "since it is on readonly branch (hi%lu).\n",
21855 +                       inode->i_ino, h_inode->i_ino);
21856 +       } else if (flags & ~S_ATIME) {
21857 +               err = -EIO;
21858 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21859 +               AuDebugOn(1);
21860 +       }
21861 +
21862 +       if (!err)
21863 +               au_cpup_attr_timesizes(inode);
21864 +       ii_write_unlock(inode);
21865 +       si_read_unlock(sb);
21866 +       lockdep_on();
21867 +
21868 +       if (!err && (flags & S_VERSION))
21869 +               inode_inc_iversion(inode);
21870 +
21871 +       return err;
21872 +}
21873 +
21874 +/* ---------------------------------------------------------------------- */
21875 +
21876 +/* no getattr version will be set by module.c:aufs_init() */
21877 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21878 +       aufs_iop[] = {
21879 +       [AuIop_SYMLINK] = {
21880 +               .permission     = aufs_permission,
21881 +#ifdef CONFIG_FS_POSIX_ACL
21882 +               .get_acl        = aufs_get_acl,
21883 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
21884 +#endif
21885 +
21886 +               .setattr        = aufs_setattr,
21887 +               .getattr        = aufs_getattr,
21888 +
21889 +#ifdef CONFIG_AUFS_XATTR
21890 +               .listxattr      = aufs_listxattr,
21891 +#endif
21892 +
21893 +               .get_link       = aufs_get_link,
21894 +
21895 +               /* .update_time = aufs_update_time */
21896 +       },
21897 +       [AuIop_DIR] = {
21898 +               .create         = aufs_create,
21899 +               .lookup         = aufs_lookup,
21900 +               .link           = aufs_link,
21901 +               .unlink         = aufs_unlink,
21902 +               .symlink        = aufs_symlink,
21903 +               .mkdir          = aufs_mkdir,
21904 +               .rmdir          = aufs_rmdir,
21905 +               .mknod          = aufs_mknod,
21906 +               .rename         = aufs_rename,
21907 +
21908 +               .permission     = aufs_permission,
21909 +#ifdef CONFIG_FS_POSIX_ACL
21910 +               .get_acl        = aufs_get_acl,
21911 +               .set_acl        = aufs_set_acl,
21912 +#endif
21913 +
21914 +               .setattr        = aufs_setattr,
21915 +               .getattr        = aufs_getattr,
21916 +
21917 +#ifdef CONFIG_AUFS_XATTR
21918 +               .listxattr      = aufs_listxattr,
21919 +#endif
21920 +
21921 +               .update_time    = aufs_update_time,
21922 +               .atomic_open    = aufs_atomic_open,
21923 +               .tmpfile        = aufs_tmpfile
21924 +       },
21925 +       [AuIop_OTHER] = {
21926 +               .permission     = aufs_permission,
21927 +#ifdef CONFIG_FS_POSIX_ACL
21928 +               .get_acl        = aufs_get_acl,
21929 +               .set_acl        = aufs_set_acl,
21930 +#endif
21931 +
21932 +               .setattr        = aufs_setattr,
21933 +               .getattr        = aufs_getattr,
21934 +
21935 +#ifdef CONFIG_AUFS_XATTR
21936 +               .listxattr      = aufs_listxattr,
21937 +#endif
21938 +
21939 +               .update_time    = aufs_update_time
21940 +       }
21941 +};
21942 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21943 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21944 +++ linux/fs/aufs/i_op_del.c    2021-12-03 15:40:58.236647297 +0100
21945 @@ -0,0 +1,522 @@
21946 +// SPDX-License-Identifier: GPL-2.0
21947 +/*
21948 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21949 + *
21950 + * This program, aufs is free software; you can redistribute it and/or modify
21951 + * it under the terms of the GNU General Public License as published by
21952 + * the Free Software Foundation; either version 2 of the License, or
21953 + * (at your option) any later version.
21954 + *
21955 + * This program is distributed in the hope that it will be useful,
21956 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21957 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21958 + * GNU General Public License for more details.
21959 + *
21960 + * You should have received a copy of the GNU General Public License
21961 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21962 + */
21963 +
21964 +/*
21965 + * inode operations (del entry)
21966 + */
21967 +
21968 +#include <linux/iversion.h>
21969 +#include "aufs.h"
21970 +
21971 +/*
21972 + * decide if a new whiteout for @dentry is necessary or not.
21973 + * when it is necessary, prepare the parent dir for the upper branch whose
21974 + * branch index is @bcpup for creation. the actual creation of the whiteout will
21975 + * be done by caller.
21976 + * return value:
21977 + * 0: wh is unnecessary
21978 + * plus: wh is necessary
21979 + * minus: error
21980 + */
21981 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
21982 +{
21983 +       int need_wh, err;
21984 +       aufs_bindex_t btop;
21985 +       struct super_block *sb;
21986 +
21987 +       sb = dentry->d_sb;
21988 +       btop = au_dbtop(dentry);
21989 +       if (*bcpup < 0) {
21990 +               *bcpup = btop;
21991 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
21992 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
21993 +                       *bcpup = err;
21994 +                       if (unlikely(err < 0))
21995 +                               goto out;
21996 +               }
21997 +       } else
21998 +               AuDebugOn(btop < *bcpup
21999 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22000 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22001 +
22002 +       if (*bcpup != btop) {
22003 +               err = au_cpup_dirs(dentry, *bcpup);
22004 +               if (unlikely(err))
22005 +                       goto out;
22006 +               need_wh = 1;
22007 +       } else {
22008 +               struct au_dinfo *dinfo, *tmp;
22009 +
22010 +               need_wh = -ENOMEM;
22011 +               dinfo = au_di(dentry);
22012 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22013 +               if (tmp) {
22014 +                       au_di_cp(tmp, dinfo);
22015 +                       au_di_swap(tmp, dinfo);
22016 +                       /* returns the number of positive dentries */
22017 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22018 +                                                /* AuLkup_IGNORE_PERM */ 0);
22019 +                       au_di_swap(tmp, dinfo);
22020 +                       au_rw_write_unlock(&tmp->di_rwsem);
22021 +                       au_di_free(tmp);
22022 +               }
22023 +       }
22024 +       AuDbg("need_wh %d\n", need_wh);
22025 +       err = need_wh;
22026 +
22027 +out:
22028 +       return err;
22029 +}
22030 +
22031 +/*
22032 + * simple tests for the del-entry operations.
22033 + * following the checks in vfs, plus the parent-child relationship.
22034 + */
22035 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22036 +              struct dentry *h_parent, int isdir)
22037 +{
22038 +       int err;
22039 +       umode_t h_mode;
22040 +       struct dentry *h_dentry, *h_latest;
22041 +       struct inode *h_inode;
22042 +       struct path h_ppath;
22043 +       struct super_block *sb;
22044 +       struct au_branch *br;
22045 +       struct user_namespace *h_userns;
22046 +
22047 +       h_dentry = au_h_dptr(dentry, bindex);
22048 +       if (d_really_is_positive(dentry)) {
22049 +               err = -ENOENT;
22050 +               if (unlikely(d_is_negative(h_dentry)))
22051 +                       goto out;
22052 +               h_inode = d_inode(h_dentry);
22053 +               if (unlikely(!h_inode->i_nlink))
22054 +                       goto out;
22055 +
22056 +               h_mode = h_inode->i_mode;
22057 +               if (!isdir) {
22058 +                       err = -EISDIR;
22059 +                       if (unlikely(S_ISDIR(h_mode)))
22060 +                               goto out;
22061 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22062 +                       err = -ENOTDIR;
22063 +                       goto out;
22064 +               }
22065 +       } else {
22066 +               /* rename(2) case */
22067 +               err = -EIO;
22068 +               if (unlikely(d_is_positive(h_dentry)))
22069 +                       goto out;
22070 +       }
22071 +
22072 +       err = -ENOENT;
22073 +       /* expected parent dir is locked */
22074 +       if (unlikely(h_parent != h_dentry->d_parent))
22075 +               goto out;
22076 +       err = 0;
22077 +
22078 +       /*
22079 +        * rmdir a dir may break the consistency on some filesystem.
22080 +        * let's try heavy test.
22081 +        */
22082 +       err = -EACCES;
22083 +       sb = dentry->d_sb;
22084 +       br = au_sbr(sb, bindex);
22085 +       h_userns = au_br_userns(br);
22086 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
22087 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
22088 +                                      MAY_EXEC | MAY_WRITE)))
22089 +               goto out;
22090 +
22091 +       h_ppath.dentry = h_parent;
22092 +       h_ppath.mnt = au_br_mnt(br);
22093 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
22094 +       err = -EIO;
22095 +       if (IS_ERR(h_latest))
22096 +               goto out;
22097 +       if (h_latest == h_dentry)
22098 +               err = 0;
22099 +       dput(h_latest);
22100 +
22101 +out:
22102 +       return err;
22103 +}
22104 +
22105 +/*
22106 + * decide the branch where we operate for @dentry. the branch index will be set
22107 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22108 + * dir for reverting.
22109 + * when a new whiteout is necessary, create it.
22110 + */
22111 +static struct dentry*
22112 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22113 +                   struct au_dtime *dt, struct au_pin *pin)
22114 +{
22115 +       struct dentry *wh_dentry;
22116 +       struct super_block *sb;
22117 +       struct path h_path;
22118 +       int err, need_wh;
22119 +       unsigned int udba;
22120 +       aufs_bindex_t bcpup;
22121 +
22122 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22123 +       wh_dentry = ERR_PTR(need_wh);
22124 +       if (unlikely(need_wh < 0))
22125 +               goto out;
22126 +
22127 +       sb = dentry->d_sb;
22128 +       udba = au_opt_udba(sb);
22129 +       bcpup = *rbcpup;
22130 +       err = au_pin(pin, dentry, bcpup, udba,
22131 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22132 +       wh_dentry = ERR_PTR(err);
22133 +       if (unlikely(err))
22134 +               goto out;
22135 +
22136 +       h_path.dentry = au_pinned_h_parent(pin);
22137 +       if (udba != AuOpt_UDBA_NONE
22138 +           && au_dbtop(dentry) == bcpup) {
22139 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22140 +               wh_dentry = ERR_PTR(err);
22141 +               if (unlikely(err))
22142 +                       goto out_unpin;
22143 +       }
22144 +
22145 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22146 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22147 +       wh_dentry = NULL;
22148 +       if (!need_wh)
22149 +               goto out; /* success, no need to create whiteout */
22150 +
22151 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22152 +       if (IS_ERR(wh_dentry))
22153 +               goto out_unpin;
22154 +
22155 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22156 +       goto out; /* success */
22157 +
22158 +out_unpin:
22159 +       au_unpin(pin);
22160 +out:
22161 +       return wh_dentry;
22162 +}
22163 +
22164 +/*
22165 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22166 + * in order to be revertible and save time for removing many child whiteouts
22167 + * under the dir.
22168 + * returns 1 when there are too many child whiteout and caller should remove
22169 + * them asynchronously. returns 0 when the number of children is enough small to
22170 + * remove now or the branch fs is a remote fs.
22171 + * otherwise return an error.
22172 + */
22173 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22174 +                          struct au_nhash *whlist, struct inode *dir)
22175 +{
22176 +       int rmdir_later, err, dirwh;
22177 +       struct dentry *h_dentry;
22178 +       struct super_block *sb;
22179 +       struct inode *inode;
22180 +
22181 +       sb = dentry->d_sb;
22182 +       SiMustAnyLock(sb);
22183 +       h_dentry = au_h_dptr(dentry, bindex);
22184 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22185 +       if (unlikely(err))
22186 +               goto out;
22187 +
22188 +       /* stop monitoring */
22189 +       inode = d_inode(dentry);
22190 +       au_hn_free(au_hi(inode, bindex));
22191 +
22192 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22193 +               dirwh = au_sbi(sb)->si_dirwh;
22194 +               rmdir_later = (dirwh <= 1);
22195 +               if (!rmdir_later)
22196 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22197 +                                                             dirwh);
22198 +               if (rmdir_later)
22199 +                       return rmdir_later;
22200 +       }
22201 +
22202 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22203 +       if (unlikely(err)) {
22204 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22205 +                       h_dentry, bindex, err);
22206 +               err = 0;
22207 +       }
22208 +
22209 +out:
22210 +       AuTraceErr(err);
22211 +       return err;
22212 +}
22213 +
22214 +/*
22215 + * final procedure for deleting a entry.
22216 + * maintain dentry and iattr.
22217 + */
22218 +static void epilog(struct inode *dir, struct dentry *dentry,
22219 +                  aufs_bindex_t bindex)
22220 +{
22221 +       struct inode *inode;
22222 +
22223 +       inode = d_inode(dentry);
22224 +       d_drop(dentry);
22225 +       inode->i_ctime = dir->i_ctime;
22226 +
22227 +       au_dir_ts(dir, bindex);
22228 +       inode_inc_iversion(dir);
22229 +}
22230 +
22231 +/*
22232 + * when an error happened, remove the created whiteout and revert everything.
22233 + */
22234 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22235 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22236 +                    struct dentry *dentry, struct au_dtime *dt)
22237 +{
22238 +       int rerr;
22239 +       struct path h_path = {
22240 +               .dentry = wh_dentry,
22241 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22242 +       };
22243 +
22244 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22245 +       if (!rerr) {
22246 +               au_set_dbwh(dentry, bwh);
22247 +               au_dtime_revert(dt);
22248 +               return 0;
22249 +       }
22250 +
22251 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22252 +       return -EIO;
22253 +}
22254 +
22255 +/* ---------------------------------------------------------------------- */
22256 +
22257 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22258 +{
22259 +       int err;
22260 +       aufs_bindex_t bwh, bindex, btop;
22261 +       struct inode *inode, *h_dir, *delegated;
22262 +       struct dentry *parent, *wh_dentry;
22263 +       /* to reduce stack size */
22264 +       struct {
22265 +               struct au_dtime dt;
22266 +               struct au_pin pin;
22267 +               struct path h_path;
22268 +       } *a;
22269 +
22270 +       IMustLock(dir);
22271 +
22272 +       err = -ENOMEM;
22273 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22274 +       if (unlikely(!a))
22275 +               goto out;
22276 +
22277 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22278 +       if (unlikely(err))
22279 +               goto out_free;
22280 +       err = au_d_hashed_positive(dentry);
22281 +       if (unlikely(err))
22282 +               goto out_unlock;
22283 +       inode = d_inode(dentry);
22284 +       IMustLock(inode);
22285 +       err = -EISDIR;
22286 +       if (unlikely(d_is_dir(dentry)))
22287 +               goto out_unlock; /* possible? */
22288 +
22289 +       btop = au_dbtop(dentry);
22290 +       bwh = au_dbwh(dentry);
22291 +       bindex = -1;
22292 +       parent = dentry->d_parent; /* dir inode is locked */
22293 +       di_write_lock_parent(parent);
22294 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22295 +                                       &a->pin);
22296 +       err = PTR_ERR(wh_dentry);
22297 +       if (IS_ERR(wh_dentry))
22298 +               goto out_parent;
22299 +
22300 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22301 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22302 +       dget(a->h_path.dentry);
22303 +       if (bindex == btop) {
22304 +               h_dir = au_pinned_h_dir(&a->pin);
22305 +               delegated = NULL;
22306 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22307 +               if (unlikely(err == -EWOULDBLOCK)) {
22308 +                       pr_warn("cannot retry for NFSv4 delegation"
22309 +                               " for an internal unlink\n");
22310 +                       iput(delegated);
22311 +               }
22312 +       } else {
22313 +               /* dir inode is locked */
22314 +               h_dir = d_inode(wh_dentry->d_parent);
22315 +               IMustLock(h_dir);
22316 +               err = 0;
22317 +       }
22318 +
22319 +       if (!err) {
22320 +               vfsub_drop_nlink(inode);
22321 +               epilog(dir, dentry, bindex);
22322 +
22323 +               /* update target timestamps */
22324 +               if (bindex == btop) {
22325 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22326 +                       /*ignore*/
22327 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22328 +               } else
22329 +                       /* todo: this timestamp may be reverted later */
22330 +                       inode->i_ctime = h_dir->i_ctime;
22331 +               goto out_unpin; /* success */
22332 +       }
22333 +
22334 +       /* revert */
22335 +       if (wh_dentry) {
22336 +               int rerr;
22337 +
22338 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22339 +                                &a->dt);
22340 +               if (rerr)
22341 +                       err = rerr;
22342 +       }
22343 +
22344 +out_unpin:
22345 +       au_unpin(&a->pin);
22346 +       dput(wh_dentry);
22347 +       dput(a->h_path.dentry);
22348 +out_parent:
22349 +       di_write_unlock(parent);
22350 +out_unlock:
22351 +       aufs_read_unlock(dentry, AuLock_DW);
22352 +out_free:
22353 +       au_kfree_rcu(a);
22354 +out:
22355 +       return err;
22356 +}
22357 +
22358 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22359 +{
22360 +       int err, rmdir_later;
22361 +       aufs_bindex_t bwh, bindex, btop;
22362 +       struct inode *inode;
22363 +       struct dentry *parent, *wh_dentry, *h_dentry;
22364 +       struct au_whtmp_rmdir *args;
22365 +       /* to reduce stack size */
22366 +       struct {
22367 +               struct au_dtime dt;
22368 +               struct au_pin pin;
22369 +       } *a;
22370 +
22371 +       IMustLock(dir);
22372 +
22373 +       err = -ENOMEM;
22374 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22375 +       if (unlikely(!a))
22376 +               goto out;
22377 +
22378 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22379 +       if (unlikely(err))
22380 +               goto out_free;
22381 +       err = au_alive_dir(dentry);
22382 +       if (unlikely(err))
22383 +               goto out_unlock;
22384 +       inode = d_inode(dentry);
22385 +       IMustLock(inode);
22386 +       err = -ENOTDIR;
22387 +       if (unlikely(!d_is_dir(dentry)))
22388 +               goto out_unlock; /* possible? */
22389 +
22390 +       err = -ENOMEM;
22391 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22392 +       if (unlikely(!args))
22393 +               goto out_unlock;
22394 +
22395 +       parent = dentry->d_parent; /* dir inode is locked */
22396 +       di_write_lock_parent(parent);
22397 +       err = au_test_empty(dentry, &args->whlist);
22398 +       if (unlikely(err))
22399 +               goto out_parent;
22400 +
22401 +       btop = au_dbtop(dentry);
22402 +       bwh = au_dbwh(dentry);
22403 +       bindex = -1;
22404 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22405 +                                       &a->pin);
22406 +       err = PTR_ERR(wh_dentry);
22407 +       if (IS_ERR(wh_dentry))
22408 +               goto out_parent;
22409 +
22410 +       h_dentry = au_h_dptr(dentry, btop);
22411 +       dget(h_dentry);
22412 +       rmdir_later = 0;
22413 +       if (bindex == btop) {
22414 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22415 +               if (err > 0) {
22416 +                       rmdir_later = err;
22417 +                       err = 0;
22418 +               }
22419 +       } else {
22420 +               /* stop monitoring */
22421 +               au_hn_free(au_hi(inode, btop));
22422 +
22423 +               /* dir inode is locked */
22424 +               IMustLock(d_inode(wh_dentry->d_parent));
22425 +               err = 0;
22426 +       }
22427 +
22428 +       if (!err) {
22429 +               vfsub_dead_dir(inode);
22430 +               au_set_dbdiropq(dentry, -1);
22431 +               epilog(dir, dentry, bindex);
22432 +
22433 +               if (rmdir_later) {
22434 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22435 +                       args = NULL;
22436 +               }
22437 +
22438 +               goto out_unpin; /* success */
22439 +       }
22440 +
22441 +       /* revert */
22442 +       AuLabel(revert);
22443 +       if (wh_dentry) {
22444 +               int rerr;
22445 +
22446 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22447 +                                &a->dt);
22448 +               if (rerr)
22449 +                       err = rerr;
22450 +       }
22451 +
22452 +out_unpin:
22453 +       au_unpin(&a->pin);
22454 +       dput(wh_dentry);
22455 +       dput(h_dentry);
22456 +out_parent:
22457 +       di_write_unlock(parent);
22458 +       if (args)
22459 +               au_whtmp_rmdir_free(args);
22460 +out_unlock:
22461 +       aufs_read_unlock(dentry, AuLock_DW);
22462 +out_free:
22463 +       au_kfree_rcu(a);
22464 +out:
22465 +       AuTraceErr(err);
22466 +       return err;
22467 +}
22468 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22469 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22470 +++ linux/fs/aufs/i_op_ren.c    2021-12-03 15:40:58.236647297 +0100
22471 @@ -0,0 +1,1257 @@
22472 +// SPDX-License-Identifier: GPL-2.0
22473 +/*
22474 + * Copyright (C) 2005-2021 Junjiro R. Okajima
22475 + *
22476 + * This program, aufs is free software; you can redistribute it and/or modify
22477 + * it under the terms of the GNU General Public License as published by
22478 + * the Free Software Foundation; either version 2 of the License, or
22479 + * (at your option) any later version.
22480 + *
22481 + * This program is distributed in the hope that it will be useful,
22482 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22483 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22484 + * GNU General Public License for more details.
22485 + *
22486 + * You should have received a copy of the GNU General Public License
22487 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22488 + */
22489 +
22490 +/*
22491 + * inode operation (rename entry)
22492 + * todo: this is crazy monster
22493 + */
22494 +
22495 +#include <linux/iversion.h>
22496 +#include "aufs.h"
22497 +
22498 +enum { AuSRC, AuDST, AuSrcDst };
22499 +enum { AuPARENT, AuCHILD, AuParentChild };
22500 +
22501 +#define AuRen_ISDIR_SRC                1
22502 +#define AuRen_ISDIR_DST                (1 << 1)
22503 +#define AuRen_ISSAMEDIR                (1 << 2)
22504 +#define AuRen_WHSRC            (1 << 3)
22505 +#define AuRen_WHDST            (1 << 4)
22506 +#define AuRen_MNT_WRITE                (1 << 5)
22507 +#define AuRen_DT_DSTDIR                (1 << 6)
22508 +#define AuRen_DIROPQ_SRC       (1 << 7)
22509 +#define AuRen_DIROPQ_DST       (1 << 8)
22510 +#define AuRen_DIRREN           (1 << 9)
22511 +#define AuRen_DROPPED_SRC      (1 << 10)
22512 +#define AuRen_DROPPED_DST      (1 << 11)
22513 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22514 +#define au_fset_ren(flags, name) \
22515 +       do { (flags) |= AuRen_##name; } while (0)
22516 +#define au_fclr_ren(flags, name) \
22517 +       do { (flags) &= ~AuRen_##name; } while (0)
22518 +
22519 +#ifndef CONFIG_AUFS_DIRREN
22520 +#undef AuRen_DIRREN
22521 +#define AuRen_DIRREN           0
22522 +#endif
22523 +
22524 +struct au_ren_args {
22525 +       struct {
22526 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22527 +                       *wh_dentry;
22528 +               struct inode *dir, *inode;
22529 +               struct au_hinode *hdir, *hinode;
22530 +               struct au_dtime dt[AuParentChild];
22531 +               aufs_bindex_t btop, bdiropq;
22532 +       } sd[AuSrcDst];
22533 +
22534 +#define src_dentry     sd[AuSRC].dentry
22535 +#define src_dir                sd[AuSRC].dir
22536 +#define src_inode      sd[AuSRC].inode
22537 +#define src_h_dentry   sd[AuSRC].h_dentry
22538 +#define src_parent     sd[AuSRC].parent
22539 +#define src_h_parent   sd[AuSRC].h_parent
22540 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22541 +#define src_hdir       sd[AuSRC].hdir
22542 +#define src_hinode     sd[AuSRC].hinode
22543 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22544 +#define src_dt         sd[AuSRC].dt
22545 +#define src_btop       sd[AuSRC].btop
22546 +#define src_bdiropq    sd[AuSRC].bdiropq
22547 +
22548 +#define dst_dentry     sd[AuDST].dentry
22549 +#define dst_dir                sd[AuDST].dir
22550 +#define dst_inode      sd[AuDST].inode
22551 +#define dst_h_dentry   sd[AuDST].h_dentry
22552 +#define dst_parent     sd[AuDST].parent
22553 +#define dst_h_parent   sd[AuDST].h_parent
22554 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22555 +#define dst_hdir       sd[AuDST].hdir
22556 +#define dst_hinode     sd[AuDST].hinode
22557 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22558 +#define dst_dt         sd[AuDST].dt
22559 +#define dst_btop       sd[AuDST].btop
22560 +#define dst_bdiropq    sd[AuDST].bdiropq
22561 +
22562 +       struct dentry *h_trap;
22563 +       struct au_branch *br;
22564 +       struct path h_path;
22565 +       struct au_nhash whlist;
22566 +       aufs_bindex_t btgt, src_bwh;
22567 +
22568 +       struct {
22569 +               unsigned short auren_flags;
22570 +               unsigned char flags;    /* syscall parameter */
22571 +               unsigned char exchange;
22572 +       } __packed;
22573 +
22574 +       struct au_whtmp_rmdir *thargs;
22575 +       struct dentry *h_dst;
22576 +       struct au_hinode *h_root;
22577 +};
22578 +
22579 +/* ---------------------------------------------------------------------- */
22580 +
22581 +/*
22582 + * functions for reverting.
22583 + * when an error happened in a single rename systemcall, we should revert
22584 + * everything as if nothing happened.
22585 + * we don't need to revert the copied-up/down the parent dir since they are
22586 + * harmless.
22587 + */
22588 +
22589 +#define RevertFailure(fmt, ...) do { \
22590 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22591 +               ##__VA_ARGS__, err, rerr); \
22592 +       err = -EIO; \
22593 +} while (0)
22594 +
22595 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22596 +{
22597 +       int rerr;
22598 +       struct dentry *d;
22599 +#define src_or_dst(member) a->sd[idx].member
22600 +
22601 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22602 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22603 +       rerr = au_diropq_remove(d, a->btgt);
22604 +       au_hn_inode_unlock(src_or_dst(hinode));
22605 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22606 +       if (rerr)
22607 +               RevertFailure("remove diropq %pd", d);
22608 +
22609 +#undef src_or_dst_
22610 +}
22611 +
22612 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22613 +{
22614 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22615 +               au_ren_do_rev_diropq(err, a, AuSRC);
22616 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22617 +               au_ren_do_rev_diropq(err, a, AuDST);
22618 +}
22619 +
22620 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22621 +{
22622 +       int rerr;
22623 +       struct inode *delegated;
22624 +       struct path h_ppath = {
22625 +               .dentry = a->src_h_parent,
22626 +               .mnt    = a->h_path.mnt
22627 +       };
22628 +
22629 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
22630 +       rerr = PTR_ERR(a->h_path.dentry);
22631 +       if (IS_ERR(a->h_path.dentry)) {
22632 +               RevertFailure("lkup one %pd", a->src_dentry);
22633 +               return;
22634 +       }
22635 +
22636 +       delegated = NULL;
22637 +       rerr = vfsub_rename(a->dst_h_dir,
22638 +                           au_h_dptr(a->src_dentry, a->btgt),
22639 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22640 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22641 +               pr_warn("cannot retry for NFSv4 delegation"
22642 +                       " for an internal rename\n");
22643 +               iput(delegated);
22644 +       }
22645 +       d_drop(a->h_path.dentry);
22646 +       dput(a->h_path.dentry);
22647 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22648 +       if (rerr)
22649 +               RevertFailure("rename %pd", a->src_dentry);
22650 +}
22651 +
22652 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22653 +{
22654 +       int rerr;
22655 +       struct inode *delegated;
22656 +       struct path h_ppath = {
22657 +               .dentry = a->dst_h_parent,
22658 +               .mnt    = a->h_path.mnt
22659 +       };
22660 +
22661 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
22662 +       rerr = PTR_ERR(a->h_path.dentry);
22663 +       if (IS_ERR(a->h_path.dentry)) {
22664 +               RevertFailure("lkup one %pd", a->dst_dentry);
22665 +               return;
22666 +       }
22667 +       if (d_is_positive(a->h_path.dentry)) {
22668 +               d_drop(a->h_path.dentry);
22669 +               dput(a->h_path.dentry);
22670 +               return;
22671 +       }
22672 +
22673 +       delegated = NULL;
22674 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22675 +                           &delegated, a->flags);
22676 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22677 +               pr_warn("cannot retry for NFSv4 delegation"
22678 +                       " for an internal rename\n");
22679 +               iput(delegated);
22680 +       }
22681 +       d_drop(a->h_path.dentry);
22682 +       dput(a->h_path.dentry);
22683 +       if (!rerr)
22684 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22685 +       else
22686 +               RevertFailure("rename %pd", a->h_dst);
22687 +}
22688 +
22689 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22690 +{
22691 +       int rerr;
22692 +
22693 +       a->h_path.dentry = a->src_wh_dentry;
22694 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22695 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22696 +       if (rerr)
22697 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22698 +}
22699 +#undef RevertFailure
22700 +
22701 +/* ---------------------------------------------------------------------- */
22702 +
22703 +/*
22704 + * when we have to copyup the renaming entry, do it with the rename-target name
22705 + * in order to minimize the cost (the later actual rename is unnecessary).
22706 + * otherwise rename it on the target branch.
22707 + */
22708 +static int au_ren_or_cpup(struct au_ren_args *a)
22709 +{
22710 +       int err;
22711 +       struct dentry *d;
22712 +       struct inode *delegated;
22713 +
22714 +       d = a->src_dentry;
22715 +       if (au_dbtop(d) == a->btgt) {
22716 +               a->h_path.dentry = a->dst_h_dentry;
22717 +               AuDebugOn(au_dbtop(d) != a->btgt);
22718 +               delegated = NULL;
22719 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22720 +                                  a->dst_h_dir, &a->h_path, &delegated,
22721 +                                  a->flags);
22722 +               if (unlikely(err == -EWOULDBLOCK)) {
22723 +                       pr_warn("cannot retry for NFSv4 delegation"
22724 +                               " for an internal rename\n");
22725 +                       iput(delegated);
22726 +               }
22727 +       } else
22728 +               BUG();
22729 +
22730 +       if (!err && a->h_dst)
22731 +               /* it will be set to dinfo later */
22732 +               dget(a->h_dst);
22733 +
22734 +       return err;
22735 +}
22736 +
22737 +/* cf. aufs_rmdir() */
22738 +static int au_ren_del_whtmp(struct au_ren_args *a)
22739 +{
22740 +       int err;
22741 +       struct inode *dir;
22742 +
22743 +       dir = a->dst_dir;
22744 +       SiMustAnyLock(dir->i_sb);
22745 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22746 +                                    au_sbi(dir->i_sb)->si_dirwh)
22747 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22748 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22749 +               if (unlikely(err))
22750 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22751 +                               "ignored.\n", a->h_dst, err);
22752 +       } else {
22753 +               au_nhash_wh_free(&a->thargs->whlist);
22754 +               a->thargs->whlist = a->whlist;
22755 +               a->whlist.nh_num = 0;
22756 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22757 +               dput(a->h_dst);
22758 +               a->thargs = NULL;
22759 +       }
22760 +
22761 +       return 0;
22762 +}
22763 +
22764 +/* make it 'opaque' dir. */
22765 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22766 +{
22767 +       int err;
22768 +       struct dentry *d, *diropq;
22769 +#define src_or_dst(member) a->sd[idx].member
22770 +
22771 +       err = 0;
22772 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22773 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22774 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22775 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22776 +       diropq = au_diropq_create(d, a->btgt);
22777 +       au_hn_inode_unlock(src_or_dst(hinode));
22778 +       if (IS_ERR(diropq))
22779 +               err = PTR_ERR(diropq);
22780 +       else
22781 +               dput(diropq);
22782 +
22783 +#undef src_or_dst_
22784 +       return err;
22785 +}
22786 +
22787 +static int au_ren_diropq(struct au_ren_args *a)
22788 +{
22789 +       int err;
22790 +       unsigned char always;
22791 +       struct dentry *d;
22792 +
22793 +       err = 0;
22794 +       d = a->dst_dentry; /* already renamed on the branch */
22795 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22796 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22797 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22798 +           && a->btgt != au_dbdiropq(a->src_dentry)
22799 +           && (a->dst_wh_dentry
22800 +               || a->btgt <= au_dbdiropq(d)
22801 +               /* hide the lower to keep xino */
22802 +               /* the lowers may not be a dir, but we hide them anyway */
22803 +               || a->btgt < au_dbbot(d)
22804 +               || always)) {
22805 +               AuDbg("here\n");
22806 +               err = au_ren_do_diropq(a, AuSRC);
22807 +               if (unlikely(err))
22808 +                       goto out;
22809 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22810 +       }
22811 +       if (!a->exchange)
22812 +               goto out; /* success */
22813 +
22814 +       d = a->src_dentry; /* already renamed on the branch */
22815 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22816 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22817 +           && (a->btgt < au_dbdiropq(d)
22818 +               || a->btgt < au_dbbot(d)
22819 +               || always)) {
22820 +               AuDbgDentry(a->src_dentry);
22821 +               AuDbgDentry(a->dst_dentry);
22822 +               err = au_ren_do_diropq(a, AuDST);
22823 +               if (unlikely(err))
22824 +                       goto out_rev_src;
22825 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22826 +       }
22827 +       goto out; /* success */
22828 +
22829 +out_rev_src:
22830 +       AuDbg("err %d, reverting src\n", err);
22831 +       au_ren_rev_diropq(err, a);
22832 +out:
22833 +       return err;
22834 +}
22835 +
22836 +static int do_rename(struct au_ren_args *a)
22837 +{
22838 +       int err;
22839 +       struct dentry *d, *h_d;
22840 +
22841 +       if (!a->exchange) {
22842 +               /* prepare workqueue args for asynchronous rmdir */
22843 +               h_d = a->dst_h_dentry;
22844 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22845 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22846 +                   && d_is_positive(h_d)) {
22847 +                       err = -ENOMEM;
22848 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22849 +                                                        GFP_NOFS);
22850 +                       if (unlikely(!a->thargs))
22851 +                               goto out;
22852 +                       a->h_dst = dget(h_d);
22853 +               }
22854 +
22855 +               /* create whiteout for src_dentry */
22856 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22857 +                       a->src_bwh = au_dbwh(a->src_dentry);
22858 +                       AuDebugOn(a->src_bwh >= 0);
22859 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22860 +                                                       a->src_h_parent);
22861 +                       err = PTR_ERR(a->src_wh_dentry);
22862 +                       if (IS_ERR(a->src_wh_dentry))
22863 +                               goto out_thargs;
22864 +               }
22865 +
22866 +               /* lookup whiteout for dentry */
22867 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22868 +                       h_d = au_wh_lkup(a->dst_h_parent,
22869 +                                        &a->dst_dentry->d_name, a->br);
22870 +                       err = PTR_ERR(h_d);
22871 +                       if (IS_ERR(h_d))
22872 +                               goto out_whsrc;
22873 +                       if (d_is_negative(h_d))
22874 +                               dput(h_d);
22875 +                       else
22876 +                               a->dst_wh_dentry = h_d;
22877 +               }
22878 +
22879 +               /* rename dentry to tmpwh */
22880 +               if (a->thargs) {
22881 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22882 +                       if (unlikely(err))
22883 +                               goto out_whdst;
22884 +
22885 +                       d = a->dst_dentry;
22886 +                       au_set_h_dptr(d, a->btgt, NULL);
22887 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22888 +                       if (unlikely(err))
22889 +                               goto out_whtmp;
22890 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22891 +               }
22892 +       }
22893 +
22894 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22895 +#if 0 /* debugging */
22896 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22897 +              && d_is_positive(a->dst_h_dentry)
22898 +              && a->src_btop != a->btgt);
22899 +#endif
22900 +
22901 +       /* rename by vfs_rename or cpup */
22902 +       err = au_ren_or_cpup(a);
22903 +       if (unlikely(err))
22904 +               /* leave the copied-up one */
22905 +               goto out_whtmp;
22906 +
22907 +       /* make dir opaque */
22908 +       err = au_ren_diropq(a);
22909 +       if (unlikely(err))
22910 +               goto out_rename;
22911 +
22912 +       /* update target timestamps */
22913 +       if (a->exchange) {
22914 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22915 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22916 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22917 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22918 +       }
22919 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22920 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22921 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22922 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22923 +
22924 +       if (!a->exchange) {
22925 +               /* remove whiteout for dentry */
22926 +               if (a->dst_wh_dentry) {
22927 +                       a->h_path.dentry = a->dst_wh_dentry;
22928 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22929 +                                                 a->dst_dentry);
22930 +                       if (unlikely(err))
22931 +                               goto out_diropq;
22932 +               }
22933 +
22934 +               /* remove whtmp */
22935 +               if (a->thargs)
22936 +                       au_ren_del_whtmp(a); /* ignore this error */
22937 +
22938 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22939 +       }
22940 +       err = 0;
22941 +       goto out_success;
22942 +
22943 +out_diropq:
22944 +       au_ren_rev_diropq(err, a);
22945 +out_rename:
22946 +       au_ren_rev_rename(err, a);
22947 +       dput(a->h_dst);
22948 +out_whtmp:
22949 +       if (a->thargs)
22950 +               au_ren_rev_whtmp(err, a);
22951 +out_whdst:
22952 +       dput(a->dst_wh_dentry);
22953 +       a->dst_wh_dentry = NULL;
22954 +out_whsrc:
22955 +       if (a->src_wh_dentry)
22956 +               au_ren_rev_whsrc(err, a);
22957 +out_success:
22958 +       dput(a->src_wh_dentry);
22959 +       dput(a->dst_wh_dentry);
22960 +out_thargs:
22961 +       if (a->thargs) {
22962 +               dput(a->h_dst);
22963 +               au_whtmp_rmdir_free(a->thargs);
22964 +               a->thargs = NULL;
22965 +       }
22966 +out:
22967 +       return err;
22968 +}
22969 +
22970 +/* ---------------------------------------------------------------------- */
22971 +
22972 +/*
22973 + * test if @dentry dir can be rename destination or not.
22974 + * success means, it is a logically empty dir.
22975 + */
22976 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
22977 +{
22978 +       return au_test_empty(dentry, whlist);
22979 +}
22980 +
22981 +/*
22982 + * test if @a->src_dentry dir can be rename source or not.
22983 + * if it can, return 0.
22984 + * success means,
22985 + * - it is a logically empty dir.
22986 + * - or, it exists on writable branch and has no children including whiteouts
22987 + *   on the lower branch unless DIRREN is on.
22988 + */
22989 +static int may_rename_srcdir(struct au_ren_args *a)
22990 +{
22991 +       int err;
22992 +       unsigned int rdhash;
22993 +       aufs_bindex_t btop, btgt;
22994 +       struct dentry *dentry;
22995 +       struct super_block *sb;
22996 +       struct au_sbinfo *sbinfo;
22997 +
22998 +       dentry = a->src_dentry;
22999 +       sb = dentry->d_sb;
23000 +       sbinfo = au_sbi(sb);
23001 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23002 +               au_fset_ren(a->auren_flags, DIRREN);
23003 +
23004 +       btgt = a->btgt;
23005 +       btop = au_dbtop(dentry);
23006 +       if (btop != btgt) {
23007 +               struct au_nhash whlist;
23008 +
23009 +               SiMustAnyLock(sb);
23010 +               rdhash = sbinfo->si_rdhash;
23011 +               if (!rdhash)
23012 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23013 +                                                          dentry));
23014 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23015 +               if (unlikely(err))
23016 +                       goto out;
23017 +               err = au_test_empty(dentry, &whlist);
23018 +               au_nhash_wh_free(&whlist);
23019 +               goto out;
23020 +       }
23021 +
23022 +       if (btop == au_dbtaildir(dentry))
23023 +               return 0; /* success */
23024 +
23025 +       err = au_test_empty_lower(dentry);
23026 +
23027 +out:
23028 +       if (err == -ENOTEMPTY) {
23029 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23030 +                       err = 0;
23031 +               } else {
23032 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23033 +                               "branches, is not supported\n");
23034 +                       err = -EXDEV;
23035 +               }
23036 +       }
23037 +       return err;
23038 +}
23039 +
23040 +/* side effect: sets whlist and h_dentry */
23041 +static int au_ren_may_dir(struct au_ren_args *a)
23042 +{
23043 +       int err;
23044 +       unsigned int rdhash;
23045 +       struct dentry *d;
23046 +
23047 +       d = a->dst_dentry;
23048 +       SiMustAnyLock(d->d_sb);
23049 +
23050 +       err = 0;
23051 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23052 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23053 +               if (!rdhash)
23054 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23055 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23056 +               if (unlikely(err))
23057 +                       goto out;
23058 +
23059 +               if (!a->exchange) {
23060 +                       au_set_dbtop(d, a->dst_btop);
23061 +                       err = may_rename_dstdir(d, &a->whlist);
23062 +                       au_set_dbtop(d, a->btgt);
23063 +               } else
23064 +                       err = may_rename_srcdir(a);
23065 +       }
23066 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23067 +       if (unlikely(err))
23068 +               goto out;
23069 +
23070 +       d = a->src_dentry;
23071 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23072 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23073 +               err = may_rename_srcdir(a);
23074 +               if (unlikely(err)) {
23075 +                       au_nhash_wh_free(&a->whlist);
23076 +                       a->whlist.nh_num = 0;
23077 +               }
23078 +       }
23079 +out:
23080 +       return err;
23081 +}
23082 +
23083 +/* ---------------------------------------------------------------------- */
23084 +
23085 +/*
23086 + * simple tests for rename.
23087 + * following the checks in vfs, plus the parent-child relationship.
23088 + */
23089 +static int au_may_ren(struct au_ren_args *a)
23090 +{
23091 +       int err, isdir;
23092 +       struct inode *h_inode;
23093 +
23094 +       if (a->src_btop == a->btgt) {
23095 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23096 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23097 +               if (unlikely(err))
23098 +                       goto out;
23099 +               err = -EINVAL;
23100 +               if (unlikely(a->src_h_dentry == a->h_trap))
23101 +                       goto out;
23102 +       }
23103 +
23104 +       err = 0;
23105 +       if (a->dst_btop != a->btgt)
23106 +               goto out;
23107 +
23108 +       err = -ENOTEMPTY;
23109 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23110 +               goto out;
23111 +
23112 +       err = -EIO;
23113 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23114 +       if (d_really_is_negative(a->dst_dentry)) {
23115 +               if (d_is_negative(a->dst_h_dentry))
23116 +                       err = au_may_add(a->dst_dentry, a->btgt,
23117 +                                        a->dst_h_parent, isdir);
23118 +       } else {
23119 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23120 +                       goto out;
23121 +               h_inode = d_inode(a->dst_h_dentry);
23122 +               if (h_inode->i_nlink)
23123 +                       err = au_may_del(a->dst_dentry, a->btgt,
23124 +                                        a->dst_h_parent, isdir);
23125 +       }
23126 +
23127 +out:
23128 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23129 +               err = -EIO;
23130 +       AuTraceErr(err);
23131 +       return err;
23132 +}
23133 +
23134 +/* ---------------------------------------------------------------------- */
23135 +
23136 +/*
23137 + * locking order
23138 + * (VFS)
23139 + * - src_dir and dir by lock_rename()
23140 + * - inode if exists
23141 + * (aufs)
23142 + * - lock all
23143 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23144 + *     + si_read_lock
23145 + *     + di_write_lock2_child()
23146 + *       + di_write_lock_child()
23147 + *        + ii_write_lock_child()
23148 + *       + di_write_lock_child2()
23149 + *        + ii_write_lock_child2()
23150 + *     + src_parent and parent
23151 + *       + di_write_lock_parent()
23152 + *        + ii_write_lock_parent()
23153 + *       + di_write_lock_parent2()
23154 + *        + ii_write_lock_parent2()
23155 + *   + lower src_dir and dir by vfsub_lock_rename()
23156 + *   + verify the every relationships between child and parent. if any
23157 + *     of them failed, unlock all and return -EBUSY.
23158 + */
23159 +static void au_ren_unlock(struct au_ren_args *a)
23160 +{
23161 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23162 +                           a->dst_h_parent, a->dst_hdir);
23163 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23164 +           && a->h_root)
23165 +               au_hn_inode_unlock(a->h_root);
23166 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23167 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23168 +}
23169 +
23170 +static int au_ren_lock(struct au_ren_args *a)
23171 +{
23172 +       int err;
23173 +       unsigned int udba;
23174 +
23175 +       err = 0;
23176 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23177 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23178 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23179 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23180 +
23181 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23182 +       if (unlikely(err))
23183 +               goto out;
23184 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23185 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23186 +               struct dentry *root;
23187 +               struct inode *dir;
23188 +
23189 +               /*
23190 +                * sbinfo is already locked, so this ii_read_lock is
23191 +                * unnecessary. but our debugging feature checks it.
23192 +                */
23193 +               root = a->src_inode->i_sb->s_root;
23194 +               if (root != a->src_parent && root != a->dst_parent) {
23195 +                       dir = d_inode(root);
23196 +                       ii_read_lock_parent3(dir);
23197 +                       a->h_root = au_hi(dir, a->btgt);
23198 +                       ii_read_unlock(dir);
23199 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23200 +               }
23201 +       }
23202 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23203 +                                     a->dst_h_parent, a->dst_hdir);
23204 +       udba = au_opt_udba(a->src_dentry->d_sb);
23205 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23206 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23207 +               err = au_busy_or_stale();
23208 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23209 +               err = au_h_verify(a->src_h_dentry, udba,
23210 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23211 +                                 a->br);
23212 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23213 +               err = au_h_verify(a->dst_h_dentry, udba,
23214 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23215 +                                 a->br);
23216 +       if (!err)
23217 +               goto out; /* success */
23218 +
23219 +       err = au_busy_or_stale();
23220 +       au_ren_unlock(a);
23221 +
23222 +out:
23223 +       return err;
23224 +}
23225 +
23226 +/* ---------------------------------------------------------------------- */
23227 +
23228 +static void au_ren_refresh_dir(struct au_ren_args *a)
23229 +{
23230 +       struct inode *dir;
23231 +
23232 +       dir = a->dst_dir;
23233 +       inode_inc_iversion(dir);
23234 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23235 +               /* is this updating defined in POSIX? */
23236 +               au_cpup_attr_timesizes(a->src_inode);
23237 +               au_cpup_attr_nlink(dir, /*force*/1);
23238 +       }
23239 +       au_dir_ts(dir, a->btgt);
23240 +
23241 +       if (a->exchange) {
23242 +               dir = a->src_dir;
23243 +               inode_inc_iversion(dir);
23244 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23245 +                       /* is this updating defined in POSIX? */
23246 +                       au_cpup_attr_timesizes(a->dst_inode);
23247 +                       au_cpup_attr_nlink(dir, /*force*/1);
23248 +               }
23249 +               au_dir_ts(dir, a->btgt);
23250 +       }
23251 +
23252 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23253 +               return;
23254 +
23255 +       dir = a->src_dir;
23256 +       inode_inc_iversion(dir);
23257 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23258 +               au_cpup_attr_nlink(dir, /*force*/1);
23259 +       au_dir_ts(dir, a->btgt);
23260 +}
23261 +
23262 +static void au_ren_refresh(struct au_ren_args *a)
23263 +{
23264 +       aufs_bindex_t bbot, bindex;
23265 +       struct dentry *d, *h_d;
23266 +       struct inode *i, *h_i;
23267 +       struct super_block *sb;
23268 +
23269 +       d = a->dst_dentry;
23270 +       d_drop(d);
23271 +       if (a->h_dst)
23272 +               /* already dget-ed by au_ren_or_cpup() */
23273 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23274 +
23275 +       i = a->dst_inode;
23276 +       if (i) {
23277 +               if (!a->exchange) {
23278 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23279 +                               vfsub_drop_nlink(i);
23280 +                       else {
23281 +                               vfsub_dead_dir(i);
23282 +                               au_cpup_attr_timesizes(i);
23283 +                       }
23284 +                       au_update_dbrange(d, /*do_put_zero*/1);
23285 +               } else
23286 +                       au_cpup_attr_nlink(i, /*force*/1);
23287 +       } else {
23288 +               bbot = a->btgt;
23289 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23290 +                       au_set_h_dptr(d, bindex, NULL);
23291 +               bbot = au_dbbot(d);
23292 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23293 +                       au_set_h_dptr(d, bindex, NULL);
23294 +               au_update_dbrange(d, /*do_put_zero*/0);
23295 +       }
23296 +
23297 +       if (a->exchange
23298 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23299 +               d_drop(a->src_dentry);
23300 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23301 +                       au_set_dbwh(a->src_dentry, -1);
23302 +               return;
23303 +       }
23304 +
23305 +       d = a->src_dentry;
23306 +       au_set_dbwh(d, -1);
23307 +       bbot = au_dbbot(d);
23308 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23309 +               h_d = au_h_dptr(d, bindex);
23310 +               if (h_d)
23311 +                       au_set_h_dptr(d, bindex, NULL);
23312 +       }
23313 +       au_set_dbbot(d, a->btgt);
23314 +
23315 +       sb = d->d_sb;
23316 +       i = a->src_inode;
23317 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23318 +               return; /* success */
23319 +
23320 +       bbot = au_ibbot(i);
23321 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23322 +               h_i = au_h_iptr(i, bindex);
23323 +               if (h_i) {
23324 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23325 +                       /* ignore this error */
23326 +                       au_set_h_iptr(i, bindex, NULL, 0);
23327 +               }
23328 +       }
23329 +       au_set_ibbot(i, a->btgt);
23330 +}
23331 +
23332 +/* ---------------------------------------------------------------------- */
23333 +
23334 +/* mainly for link(2) and rename(2) */
23335 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23336 +{
23337 +       aufs_bindex_t bdiropq, bwh;
23338 +       struct dentry *parent;
23339 +       struct au_branch *br;
23340 +
23341 +       parent = dentry->d_parent;
23342 +       IMustLock(d_inode(parent)); /* dir is locked */
23343 +
23344 +       bdiropq = au_dbdiropq(parent);
23345 +       bwh = au_dbwh(dentry);
23346 +       br = au_sbr(dentry->d_sb, btgt);
23347 +       if (au_br_rdonly(br)
23348 +           || (0 <= bdiropq && bdiropq < btgt)
23349 +           || (0 <= bwh && bwh < btgt))
23350 +               btgt = -1;
23351 +
23352 +       AuDbg("btgt %d\n", btgt);
23353 +       return btgt;
23354 +}
23355 +
23356 +/* sets src_btop, dst_btop and btgt */
23357 +static int au_ren_wbr(struct au_ren_args *a)
23358 +{
23359 +       int err;
23360 +       struct au_wr_dir_args wr_dir_args = {
23361 +               /* .force_btgt  = -1, */
23362 +               .flags          = AuWrDir_ADD_ENTRY
23363 +       };
23364 +
23365 +       a->src_btop = au_dbtop(a->src_dentry);
23366 +       a->dst_btop = au_dbtop(a->dst_dentry);
23367 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23368 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23369 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23370 +       wr_dir_args.force_btgt = a->src_btop;
23371 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23372 +               wr_dir_args.force_btgt = a->dst_btop;
23373 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23374 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23375 +       a->btgt = err;
23376 +       if (a->exchange)
23377 +               au_update_dbtop(a->dst_dentry);
23378 +
23379 +       return err;
23380 +}
23381 +
23382 +static void au_ren_dt(struct au_ren_args *a)
23383 +{
23384 +       a->h_path.dentry = a->src_h_parent;
23385 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23386 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23387 +               a->h_path.dentry = a->dst_h_parent;
23388 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23389 +       }
23390 +
23391 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23392 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23393 +           && !a->exchange)
23394 +               return;
23395 +
23396 +       a->h_path.dentry = a->src_h_dentry;
23397 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23398 +       if (d_is_positive(a->dst_h_dentry)) {
23399 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23400 +               a->h_path.dentry = a->dst_h_dentry;
23401 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23402 +       }
23403 +}
23404 +
23405 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23406 +{
23407 +       struct dentry *h_d;
23408 +       struct inode *h_inode;
23409 +
23410 +       au_dtime_revert(a->src_dt + AuPARENT);
23411 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23412 +               au_dtime_revert(a->dst_dt + AuPARENT);
23413 +
23414 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23415 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23416 +               h_inode = d_inode(h_d);
23417 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23418 +               au_dtime_revert(a->src_dt + AuCHILD);
23419 +               inode_unlock(h_inode);
23420 +
23421 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23422 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23423 +                       h_inode = d_inode(h_d);
23424 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23425 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23426 +                       inode_unlock(h_inode);
23427 +               }
23428 +       }
23429 +}
23430 +
23431 +/* ---------------------------------------------------------------------- */
23432 +
23433 +int aufs_rename(struct user_namespace *userns,
23434 +               struct inode *_src_dir, struct dentry *_src_dentry,
23435 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23436 +               unsigned int _flags)
23437 +{
23438 +       int err, lock_flags;
23439 +       void *rev;
23440 +       /* reduce stack space */
23441 +       struct au_ren_args *a;
23442 +       struct au_pin pin;
23443 +
23444 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23445 +       IMustLock(_src_dir);
23446 +       IMustLock(_dst_dir);
23447 +
23448 +       err = -EINVAL;
23449 +       if (unlikely(_flags & RENAME_WHITEOUT))
23450 +               goto out;
23451 +
23452 +       err = -ENOMEM;
23453 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23454 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23455 +       if (unlikely(!a))
23456 +               goto out;
23457 +
23458 +       a->flags = _flags;
23459 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23460 +                    && RENAME_EXCHANGE > U8_MAX);
23461 +       a->exchange = _flags & RENAME_EXCHANGE;
23462 +       a->src_dir = _src_dir;
23463 +       a->src_dentry = _src_dentry;
23464 +       a->src_inode = NULL;
23465 +       if (d_really_is_positive(a->src_dentry))
23466 +               a->src_inode = d_inode(a->src_dentry);
23467 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23468 +       a->dst_dir = _dst_dir;
23469 +       a->dst_dentry = _dst_dentry;
23470 +       a->dst_inode = NULL;
23471 +       if (d_really_is_positive(a->dst_dentry))
23472 +               a->dst_inode = d_inode(a->dst_dentry);
23473 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23474 +       if (a->dst_inode) {
23475 +               /*
23476 +                * if EXCHANGE && src is non-dir && dst is dir,
23477 +                * dst is not locked.
23478 +                */
23479 +               /* IMustLock(a->dst_inode); */
23480 +               au_igrab(a->dst_inode);
23481 +       }
23482 +
23483 +       err = -ENOTDIR;
23484 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23485 +       if (d_is_dir(a->src_dentry)) {
23486 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23487 +               if (unlikely(!a->exchange
23488 +                            && d_really_is_positive(a->dst_dentry)
23489 +                            && !d_is_dir(a->dst_dentry)))
23490 +                       goto out_free;
23491 +               lock_flags |= AuLock_DIRS;
23492 +       }
23493 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23494 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23495 +               if (unlikely(!a->exchange
23496 +                            && d_really_is_positive(a->src_dentry)
23497 +                            && !d_is_dir(a->src_dentry)))
23498 +                       goto out_free;
23499 +               lock_flags |= AuLock_DIRS;
23500 +       }
23501 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23502 +                                       lock_flags);
23503 +       if (unlikely(err))
23504 +               goto out_free;
23505 +
23506 +       err = au_d_hashed_positive(a->src_dentry);
23507 +       if (unlikely(err))
23508 +               goto out_unlock;
23509 +       err = -ENOENT;
23510 +       if (a->dst_inode) {
23511 +               /*
23512 +                * If it is a dir, VFS unhash it before this
23513 +                * function. It means we cannot rely upon d_unhashed().
23514 +                */
23515 +               if (unlikely(!a->dst_inode->i_nlink))
23516 +                       goto out_unlock;
23517 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23518 +                       err = au_d_hashed_positive(a->dst_dentry);
23519 +                       if (unlikely(err && !a->exchange))
23520 +                               goto out_unlock;
23521 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23522 +                       goto out_unlock;
23523 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23524 +               goto out_unlock;
23525 +
23526 +       /*
23527 +        * is it possible?
23528 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23529 +        * there may exist a problem somewhere else.
23530 +        */
23531 +       err = -EINVAL;
23532 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23533 +               goto out_unlock;
23534 +
23535 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23536 +       di_write_lock_parent(a->dst_parent);
23537 +
23538 +       /* which branch we process */
23539 +       err = au_ren_wbr(a);
23540 +       if (unlikely(err < 0))
23541 +               goto out_parent;
23542 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23543 +       a->h_path.mnt = au_br_mnt(a->br);
23544 +
23545 +       /* are they available to be renamed */
23546 +       err = au_ren_may_dir(a);
23547 +       if (unlikely(err))
23548 +               goto out_children;
23549 +
23550 +       /* prepare the writable parent dir on the same branch */
23551 +       if (a->dst_btop == a->btgt) {
23552 +               au_fset_ren(a->auren_flags, WHDST);
23553 +       } else {
23554 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23555 +               if (unlikely(err))
23556 +                       goto out_children;
23557 +       }
23558 +
23559 +       err = 0;
23560 +       if (!a->exchange) {
23561 +               if (a->src_dir != a->dst_dir) {
23562 +                       /*
23563 +                        * this temporary unlock is safe,
23564 +                        * because both dir->i_mutex are locked.
23565 +                        */
23566 +                       di_write_unlock(a->dst_parent);
23567 +                       di_write_lock_parent(a->src_parent);
23568 +                       err = au_wr_dir_need_wh(a->src_dentry,
23569 +                                               au_ftest_ren(a->auren_flags,
23570 +                                                            ISDIR_SRC),
23571 +                                               &a->btgt);
23572 +                       di_write_unlock(a->src_parent);
23573 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23574 +                                             /*isdir*/1);
23575 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23576 +               } else
23577 +                       err = au_wr_dir_need_wh(a->src_dentry,
23578 +                                               au_ftest_ren(a->auren_flags,
23579 +                                                            ISDIR_SRC),
23580 +                                               &a->btgt);
23581 +       }
23582 +       if (unlikely(err < 0))
23583 +               goto out_children;
23584 +       if (err)
23585 +               au_fset_ren(a->auren_flags, WHSRC);
23586 +
23587 +       /* cpup src */
23588 +       if (a->src_btop != a->btgt) {
23589 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23590 +                            au_opt_udba(a->src_dentry->d_sb),
23591 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23592 +               if (!err) {
23593 +                       struct au_cp_generic cpg = {
23594 +                               .dentry = a->src_dentry,
23595 +                               .bdst   = a->btgt,
23596 +                               .bsrc   = a->src_btop,
23597 +                               .len    = -1,
23598 +                               .pin    = &pin,
23599 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23600 +                       };
23601 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23602 +                       err = au_sio_cpup_simple(&cpg);
23603 +                       au_unpin(&pin);
23604 +               }
23605 +               if (unlikely(err))
23606 +                       goto out_children;
23607 +               a->src_btop = a->btgt;
23608 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23609 +               if (!a->exchange)
23610 +                       au_fset_ren(a->auren_flags, WHSRC);
23611 +       }
23612 +
23613 +       /* cpup dst */
23614 +       if (a->exchange && a->dst_inode
23615 +           && a->dst_btop != a->btgt) {
23616 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23617 +                            au_opt_udba(a->dst_dentry->d_sb),
23618 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23619 +               if (!err) {
23620 +                       struct au_cp_generic cpg = {
23621 +                               .dentry = a->dst_dentry,
23622 +                               .bdst   = a->btgt,
23623 +                               .bsrc   = a->dst_btop,
23624 +                               .len    = -1,
23625 +                               .pin    = &pin,
23626 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23627 +                       };
23628 +                       err = au_sio_cpup_simple(&cpg);
23629 +                       au_unpin(&pin);
23630 +               }
23631 +               if (unlikely(err))
23632 +                       goto out_children;
23633 +               a->dst_btop = a->btgt;
23634 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23635 +       }
23636 +
23637 +       /* lock them all */
23638 +       err = au_ren_lock(a);
23639 +       if (unlikely(err))
23640 +               /* leave the copied-up one */
23641 +               goto out_children;
23642 +
23643 +       if (!a->exchange) {
23644 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23645 +                       err = au_may_ren(a);
23646 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23647 +                       err = -ENAMETOOLONG;
23648 +               if (unlikely(err))
23649 +                       goto out_hdir;
23650 +       }
23651 +
23652 +       /* store timestamps to be revertible */
23653 +       au_ren_dt(a);
23654 +
23655 +       /* store dirren info */
23656 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23657 +               err = au_dr_rename(a->src_dentry, a->btgt,
23658 +                                  &a->dst_dentry->d_name, &rev);
23659 +               AuTraceErr(err);
23660 +               if (unlikely(err))
23661 +                       goto out_dt;
23662 +       }
23663 +
23664 +       /* here we go */
23665 +       err = do_rename(a);
23666 +       if (unlikely(err))
23667 +               goto out_dirren;
23668 +
23669 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23670 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23671 +
23672 +       /* update dir attributes */
23673 +       au_ren_refresh_dir(a);
23674 +
23675 +       /* dput/iput all lower dentries */
23676 +       au_ren_refresh(a);
23677 +
23678 +       goto out_hdir; /* success */
23679 +
23680 +out_dirren:
23681 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23682 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23683 +out_dt:
23684 +       au_ren_rev_dt(err, a);
23685 +out_hdir:
23686 +       au_ren_unlock(a);
23687 +out_children:
23688 +       au_nhash_wh_free(&a->whlist);
23689 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23690 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23691 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23692 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23693 +       }
23694 +out_parent:
23695 +       if (!err) {
23696 +               if (d_unhashed(a->src_dentry))
23697 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23698 +               if (d_unhashed(a->dst_dentry))
23699 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23700 +               if (!a->exchange)
23701 +                       d_move(a->src_dentry, a->dst_dentry);
23702 +               else {
23703 +                       d_exchange(a->src_dentry, a->dst_dentry);
23704 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23705 +                               d_drop(a->dst_dentry);
23706 +               }
23707 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23708 +                       d_drop(a->src_dentry);
23709 +       } else {
23710 +               au_update_dbtop(a->dst_dentry);
23711 +               if (!a->dst_inode)
23712 +                       d_drop(a->dst_dentry);
23713 +       }
23714 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23715 +               di_write_unlock(a->dst_parent);
23716 +       else
23717 +               di_write_unlock2(a->src_parent, a->dst_parent);
23718 +out_unlock:
23719 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23720 +out_free:
23721 +       iput(a->dst_inode);
23722 +       if (a->thargs)
23723 +               au_whtmp_rmdir_free(a->thargs);
23724 +       au_kfree_rcu(a);
23725 +out:
23726 +       AuTraceErr(err);
23727 +       return err;
23728 +}
23729 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23730 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23731 +++ linux/fs/aufs/Kconfig       2021-12-03 15:38:59.929980643 +0100
23732 @@ -0,0 +1,199 @@
23733 +# SPDX-License-Identifier: GPL-2.0
23734 +config AUFS_FS
23735 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23736 +       help
23737 +       Aufs is a stackable unification filesystem such as Unionfs,
23738 +       which unifies several directories and provides a merged single
23739 +       directory.
23740 +       In the early days, aufs was entirely re-designed and
23741 +       re-implemented Unionfs Version 1.x series. Introducing many
23742 +       original ideas, approaches and improvements, it becomes totally
23743 +       different from Unionfs while keeping the basic features.
23744 +
23745 +if AUFS_FS
23746 +choice
23747 +       prompt "Maximum number of branches"
23748 +       default AUFS_BRANCH_MAX_127
23749 +       help
23750 +       Specifies the maximum number of branches (or member directories)
23751 +       in a single aufs. The larger value consumes more system
23752 +       resources and has a minor impact to performance.
23753 +config AUFS_BRANCH_MAX_127
23754 +       bool "127"
23755 +       help
23756 +       Specifies the maximum number of branches (or member directories)
23757 +       in a single aufs. The larger value consumes more system
23758 +       resources and has a minor impact to performance.
23759 +config AUFS_BRANCH_MAX_511
23760 +       bool "511"
23761 +       help
23762 +       Specifies the maximum number of branches (or member directories)
23763 +       in a single aufs. The larger value consumes more system
23764 +       resources and has a minor impact to performance.
23765 +config AUFS_BRANCH_MAX_1023
23766 +       bool "1023"
23767 +       help
23768 +       Specifies the maximum number of branches (or member directories)
23769 +       in a single aufs. The larger value consumes more system
23770 +       resources and has a minor impact to performance.
23771 +config AUFS_BRANCH_MAX_32767
23772 +       bool "32767"
23773 +       help
23774 +       Specifies the maximum number of branches (or member directories)
23775 +       in a single aufs. The larger value consumes more system
23776 +       resources and has a minor impact to performance.
23777 +endchoice
23778 +
23779 +config AUFS_SBILIST
23780 +       bool
23781 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23782 +       default y
23783 +       help
23784 +       Automatic configuration for internal use.
23785 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23786 +
23787 +config AUFS_HNOTIFY
23788 +       bool "Detect direct branch access (bypassing aufs)"
23789 +       help
23790 +       If you want to modify files on branches directly, eg. bypassing aufs,
23791 +       and want aufs to detect the changes of them fully, then enable this
23792 +       option and use 'udba=notify' mount option.
23793 +       Currently there is only one available configuration, "fsnotify".
23794 +       It will have a negative impact to the performance.
23795 +       See detail in aufs.5.
23796 +
23797 +choice
23798 +       prompt "method" if AUFS_HNOTIFY
23799 +       default AUFS_HFSNOTIFY
23800 +config AUFS_HFSNOTIFY
23801 +       bool "fsnotify"
23802 +       select FSNOTIFY
23803 +endchoice
23804 +
23805 +config AUFS_EXPORT
23806 +       bool "NFS-exportable aufs"
23807 +       depends on EXPORTFS
23808 +       help
23809 +       If you want to export your mounted aufs via NFS, then enable this
23810 +       option. There are several requirements for this configuration.
23811 +       See detail in aufs.5.
23812 +
23813 +config AUFS_INO_T_64
23814 +       bool
23815 +       depends on AUFS_EXPORT
23816 +       depends on 64BIT && !(ALPHA || S390)
23817 +       default y
23818 +       help
23819 +       Automatic configuration for internal use.
23820 +       /* typedef unsigned long/int __kernel_ino_t */
23821 +       /* alpha and s390x are int */
23822 +
23823 +config AUFS_XATTR
23824 +       bool "support for XATTR/EA (including Security Labels)"
23825 +       help
23826 +       If your branch fs supports XATTR/EA and you want to make them
23827 +       available in aufs too, then enable this opsion and specify the
23828 +       branch attributes for EA.
23829 +       See detail in aufs.5.
23830 +
23831 +config AUFS_FHSM
23832 +       bool "File-based Hierarchical Storage Management"
23833 +       help
23834 +       Hierarchical Storage Management (or HSM) is a well-known feature
23835 +       in the storage world. Aufs provides this feature as file-based.
23836 +       with multiple branches.
23837 +       These multiple branches are prioritized, ie. the topmost one
23838 +       should be the fastest drive and be used heavily.
23839 +
23840 +config AUFS_RDU
23841 +       bool "Readdir in userspace"
23842 +       help
23843 +       Aufs has two methods to provide a merged view for a directory,
23844 +       by a user-space library and by kernel-space natively. The latter
23845 +       is always enabled but sometimes large and slow.
23846 +       If you enable this option, install the library in aufs2-util
23847 +       package, and set some environment variables for your readdir(3),
23848 +       then the work will be handled in user-space which generally
23849 +       shows better performance in most cases.
23850 +       See detail in aufs.5.
23851 +
23852 +config AUFS_DIRREN
23853 +       bool "Workaround for rename(2)-ing a directory"
23854 +       help
23855 +       By default, aufs returns EXDEV error in renameing a dir who has
23856 +       his child on the lower branch, since it is a bad idea to issue
23857 +       rename(2) internally for every lower branch. But user may not
23858 +       accept this behaviour. So here is a workaround to allow such
23859 +       rename(2) and store some extra infromation on the writable
23860 +       branch. Obviously this costs high (and I don't like it).
23861 +       To use this feature, you need to enable this configuration AND
23862 +       to specify the mount option `dirren.'
23863 +       See details in aufs.5 and the design documents.
23864 +
23865 +config AUFS_SHWH
23866 +       bool "Show whiteouts"
23867 +       help
23868 +       If you want to make the whiteouts in aufs visible, then enable
23869 +       this option and specify 'shwh' mount option. Although it may
23870 +       sounds like philosophy or something, but in technically it
23871 +       simply shows the name of whiteout with keeping its behaviour.
23872 +
23873 +config AUFS_BR_RAMFS
23874 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23875 +       help
23876 +       If you want to use ramfs as an aufs branch fs, then enable this
23877 +       option. Generally tmpfs is recommended.
23878 +       Aufs prohibited them to be a branch fs by default, because
23879 +       initramfs becomes unusable after switch_root or something
23880 +       generally. If you sets initramfs as an aufs branch and boot your
23881 +       system by switch_root, you will meet a problem easily since the
23882 +       files in initramfs may be inaccessible.
23883 +       Unless you are going to use ramfs as an aufs branch fs without
23884 +       switch_root or something, leave it N.
23885 +
23886 +config AUFS_BR_FUSE
23887 +       bool "Fuse fs as an aufs branch"
23888 +       depends on FUSE_FS
23889 +       select AUFS_POLL
23890 +       help
23891 +       If you want to use fuse-based userspace filesystem as an aufs
23892 +       branch fs, then enable this option.
23893 +       It implements the internal poll(2) operation which is
23894 +       implemented by fuse only (curretnly).
23895 +
23896 +config AUFS_POLL
23897 +       bool
23898 +       help
23899 +       Automatic configuration for internal use.
23900 +
23901 +config AUFS_BR_HFSPLUS
23902 +       bool "Hfsplus as an aufs branch"
23903 +       depends on HFSPLUS_FS
23904 +       default y
23905 +       help
23906 +       If you want to use hfsplus fs as an aufs branch fs, then enable
23907 +       this option. This option introduces a small overhead at
23908 +       copying-up a file on hfsplus.
23909 +
23910 +config AUFS_BDEV_LOOP
23911 +       bool
23912 +       depends on BLK_DEV_LOOP
23913 +       default y
23914 +       help
23915 +       Automatic configuration for internal use.
23916 +       Convert =[ym] into =y.
23917 +
23918 +config AUFS_DEBUG
23919 +       bool "Debug aufs"
23920 +       help
23921 +       Enable this to compile aufs internal debug code.
23922 +       It will have a negative impact to the performance.
23923 +
23924 +config AUFS_MAGIC_SYSRQ
23925 +       bool
23926 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
23927 +       default y
23928 +       help
23929 +       Automatic configuration for internal use.
23930 +       When aufs supports Magic SysRq, enabled automatically.
23931 +endif
23932 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23933 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
23934 +++ linux/fs/aufs/lcnt.h        2021-12-03 15:38:59.936647310 +0100
23935 @@ -0,0 +1,186 @@
23936 +/* SPDX-License-Identifier: GPL-2.0 */
23937 +/*
23938 + * Copyright (C) 2018-2021 Junjiro R. Okajima
23939 + *
23940 + * This program, aufs is free software; you can redistribute it and/or modify
23941 + * it under the terms of the GNU General Public License as published by
23942 + * the Free Software Foundation; either version 2 of the License, or
23943 + * (at your option) any later version.
23944 + *
23945 + * This program is distributed in the hope that it will be useful,
23946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23948 + * GNU General Public License for more details.
23949 + *
23950 + * You should have received a copy of the GNU General Public License
23951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23952 + */
23953 +
23954 +/*
23955 + * simple long counter wrapper
23956 + */
23957 +
23958 +#ifndef __AUFS_LCNT_H__
23959 +#define __AUFS_LCNT_H__
23960 +
23961 +#ifdef __KERNEL__
23962 +
23963 +#include "debug.h"
23964 +
23965 +#define AuLCntATOMIC   1
23966 +#define AuLCntPCPUCNT  2
23967 +/*
23968 + * why does percpu_refcount require extra synchronize_rcu()s in
23969 + * au_br_do_free()
23970 + */
23971 +#define AuLCntPCPUREF  3
23972 +
23973 +/* #define AuLCntChosen        AuLCntATOMIC */
23974 +#define AuLCntChosen   AuLCntPCPUCNT
23975 +/* #define AuLCntChosen        AuLCntPCPUREF */
23976 +
23977 +#if AuLCntChosen == AuLCntATOMIC
23978 +#include <linux/atomic.h>
23979 +
23980 +typedef atomic_long_t au_lcnt_t;
23981 +
23982 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23983 +{
23984 +       atomic_long_set(cnt, 0);
23985 +       return 0;
23986 +}
23987 +
23988 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
23989 +{
23990 +       /* empty */
23991 +}
23992 +
23993 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
23994 +                              int do_sync __maybe_unused)
23995 +{
23996 +       /* empty */
23997 +}
23998 +
23999 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24000 +{
24001 +       atomic_long_inc(cnt);
24002 +}
24003 +
24004 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24005 +{
24006 +       atomic_long_dec(cnt);
24007 +}
24008 +
24009 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24010 +{
24011 +       return atomic_long_read(cnt);
24012 +}
24013 +#endif
24014 +
24015 +#if AuLCntChosen == AuLCntPCPUCNT
24016 +#include <linux/percpu_counter.h>
24017 +
24018 +typedef struct percpu_counter au_lcnt_t;
24019 +
24020 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24021 +{
24022 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24023 +}
24024 +
24025 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24026 +{
24027 +       /* empty */
24028 +}
24029 +
24030 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24031 +{
24032 +       percpu_counter_destroy(cnt);
24033 +}
24034 +
24035 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24036 +{
24037 +       percpu_counter_inc(cnt);
24038 +}
24039 +
24040 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24041 +{
24042 +       percpu_counter_dec(cnt);
24043 +}
24044 +
24045 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24046 +{
24047 +       s64 n;
24048 +
24049 +       n = percpu_counter_sum(cnt);
24050 +       BUG_ON(n < 0);
24051 +       if (LONG_MAX != LLONG_MAX
24052 +           && n > LONG_MAX)
24053 +               AuWarn1("%s\n", "wrap-around");
24054 +
24055 +       return n;
24056 +}
24057 +#endif
24058 +
24059 +#if AuLCntChosen == AuLCntPCPUREF
24060 +#include <linux/percpu-refcount.h>
24061 +
24062 +typedef struct percpu_ref au_lcnt_t;
24063 +
24064 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24065 +{
24066 +       if (!release)
24067 +               release = percpu_ref_exit;
24068 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24069 +}
24070 +
24071 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24072 +{
24073 +       synchronize_rcu();
24074 +}
24075 +
24076 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24077 +{
24078 +       percpu_ref_kill(cnt);
24079 +       if (do_sync)
24080 +               au_lcnt_wait_for_fin(cnt);
24081 +}
24082 +
24083 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24084 +{
24085 +       percpu_ref_get(cnt);
24086 +}
24087 +
24088 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24089 +{
24090 +       percpu_ref_put(cnt);
24091 +}
24092 +
24093 +/*
24094 + * avoid calling this func as possible.
24095 + */
24096 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24097 +{
24098 +       long l;
24099 +
24100 +       percpu_ref_switch_to_atomic_sync(cnt);
24101 +       l = atomic_long_read(&cnt->count);
24102 +       if (do_rev)
24103 +               percpu_ref_switch_to_percpu(cnt);
24104 +
24105 +       /* percpu_ref is initialized by 1 instead of 0 */
24106 +       return l - 1;
24107 +}
24108 +#endif
24109 +
24110 +#ifdef CONFIG_AUFS_DEBUG
24111 +#define AuLCntZero(val) do {                   \
24112 +       long l = val;                           \
24113 +       if (l)                                  \
24114 +               AuDbg("%s = %ld\n", #val, l);   \
24115 +} while (0)
24116 +#else
24117 +#define AuLCntZero(val)                do {} while (0)
24118 +#endif
24119 +
24120 +#endif /* __KERNEL__ */
24121 +#endif /* __AUFS_LCNT_H__ */
24122 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24123 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24124 +++ linux/fs/aufs/loop.c        2021-12-03 15:38:59.936647310 +0100
24125 @@ -0,0 +1,148 @@
24126 +// SPDX-License-Identifier: GPL-2.0
24127 +/*
24128 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24129 + *
24130 + * This program, aufs is free software; you can redistribute it and/or modify
24131 + * it under the terms of the GNU General Public License as published by
24132 + * the Free Software Foundation; either version 2 of the License, or
24133 + * (at your option) any later version.
24134 + *
24135 + * This program is distributed in the hope that it will be useful,
24136 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24137 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24138 + * GNU General Public License for more details.
24139 + *
24140 + * You should have received a copy of the GNU General Public License
24141 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24142 + */
24143 +
24144 +/*
24145 + * support for loopback block device as a branch
24146 + */
24147 +
24148 +#include "aufs.h"
24149 +
24150 +/* added into drivers/block/loop.c */
24151 +static struct file *(*backing_file_func)(struct super_block *sb);
24152 +
24153 +/*
24154 + * test if two lower dentries have overlapping branches.
24155 + */
24156 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24157 +{
24158 +       struct super_block *h_sb;
24159 +       struct file *backing_file;
24160 +
24161 +       if (unlikely(!backing_file_func)) {
24162 +               /* don't load "loop" module here */
24163 +               backing_file_func = symbol_get(loop_backing_file);
24164 +               if (unlikely(!backing_file_func))
24165 +                       /* "loop" module is not loaded */
24166 +                       return 0;
24167 +       }
24168 +
24169 +       h_sb = h_adding->d_sb;
24170 +       backing_file = backing_file_func(h_sb);
24171 +       if (!backing_file)
24172 +               return 0;
24173 +
24174 +       h_adding = backing_file->f_path.dentry;
24175 +       /*
24176 +        * h_adding can be local NFS.
24177 +        * in this case aufs cannot detect the loop.
24178 +        */
24179 +       if (unlikely(h_adding->d_sb == sb))
24180 +               return 1;
24181 +       return !!au_test_subdir(h_adding, sb->s_root);
24182 +}
24183 +
24184 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24185 +int au_test_loopback_kthread(void)
24186 +{
24187 +       int ret;
24188 +       struct task_struct *tsk = current;
24189 +       char c, comm[sizeof(tsk->comm)];
24190 +
24191 +       ret = 0;
24192 +       if (tsk->flags & PF_KTHREAD) {
24193 +               get_task_comm(comm, tsk);
24194 +               c = comm[4];
24195 +               ret = ('0' <= c && c <= '9'
24196 +                      && !strncmp(comm, "loop", 4));
24197 +       }
24198 +
24199 +       return ret;
24200 +}
24201 +
24202 +/* ---------------------------------------------------------------------- */
24203 +
24204 +#define au_warn_loopback_step  16
24205 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24206 +static unsigned long *au_warn_loopback_array;
24207 +
24208 +void au_warn_loopback(struct super_block *h_sb)
24209 +{
24210 +       int i, new_nelem;
24211 +       unsigned long *a, magic;
24212 +       static DEFINE_SPINLOCK(spin);
24213 +
24214 +       magic = h_sb->s_magic;
24215 +       spin_lock(&spin);
24216 +       a = au_warn_loopback_array;
24217 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24218 +               if (a[i] == magic) {
24219 +                       spin_unlock(&spin);
24220 +                       return;
24221 +               }
24222 +
24223 +       /* h_sb is new to us, print it */
24224 +       if (i < au_warn_loopback_nelem) {
24225 +               a[i] = magic;
24226 +               goto pr;
24227 +       }
24228 +
24229 +       /* expand the array */
24230 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24231 +       a = au_kzrealloc(au_warn_loopback_array,
24232 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24233 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24234 +                        /*may_shrink*/0);
24235 +       if (a) {
24236 +               au_warn_loopback_nelem = new_nelem;
24237 +               au_warn_loopback_array = a;
24238 +               a[i] = magic;
24239 +               goto pr;
24240 +       }
24241 +
24242 +       spin_unlock(&spin);
24243 +       AuWarn1("realloc failed, ignored\n");
24244 +       return;
24245 +
24246 +pr:
24247 +       spin_unlock(&spin);
24248 +       pr_warn("you may want to try another patch for loopback file "
24249 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24250 +}
24251 +
24252 +int au_loopback_init(void)
24253 +{
24254 +       int err;
24255 +       struct super_block *sb __maybe_unused;
24256 +
24257 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24258 +
24259 +       err = 0;
24260 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24261 +                                        sizeof(unsigned long), GFP_NOFS);
24262 +       if (unlikely(!au_warn_loopback_array))
24263 +               err = -ENOMEM;
24264 +
24265 +       return err;
24266 +}
24267 +
24268 +void au_loopback_fin(void)
24269 +{
24270 +       if (backing_file_func)
24271 +               symbol_put(loop_backing_file);
24272 +       au_kfree_try_rcu(au_warn_loopback_array);
24273 +}
24274 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24275 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24276 +++ linux/fs/aufs/loop.h        2021-12-03 15:38:59.936647310 +0100
24277 @@ -0,0 +1,55 @@
24278 +/* SPDX-License-Identifier: GPL-2.0 */
24279 +/*
24280 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24281 + *
24282 + * This program, aufs is free software; you can redistribute it and/or modify
24283 + * it under the terms of the GNU General Public License as published by
24284 + * the Free Software Foundation; either version 2 of the License, or
24285 + * (at your option) any later version.
24286 + *
24287 + * This program is distributed in the hope that it will be useful,
24288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24290 + * GNU General Public License for more details.
24291 + *
24292 + * You should have received a copy of the GNU General Public License
24293 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24294 + */
24295 +
24296 +/*
24297 + * support for loopback mount as a branch
24298 + */
24299 +
24300 +#ifndef __AUFS_LOOP_H__
24301 +#define __AUFS_LOOP_H__
24302 +
24303 +#ifdef __KERNEL__
24304 +
24305 +struct dentry;
24306 +struct super_block;
24307 +
24308 +#ifdef CONFIG_AUFS_BDEV_LOOP
24309 +/* drivers/block/loop.c */
24310 +struct file *loop_backing_file(struct super_block *sb);
24311 +
24312 +/* loop.c */
24313 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24314 +int au_test_loopback_kthread(void);
24315 +void au_warn_loopback(struct super_block *h_sb);
24316 +
24317 +int au_loopback_init(void);
24318 +void au_loopback_fin(void);
24319 +#else
24320 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24321 +
24322 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24323 +          struct dentry *h_adding)
24324 +AuStubInt0(au_test_loopback_kthread, void)
24325 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24326 +
24327 +AuStubInt0(au_loopback_init, void)
24328 +AuStubVoid(au_loopback_fin, void)
24329 +#endif /* BLK_DEV_LOOP */
24330 +
24331 +#endif /* __KERNEL__ */
24332 +#endif /* __AUFS_LOOP_H__ */
24333 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24334 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24335 +++ linux/fs/aufs/magic.mk      2021-12-03 15:38:59.936647310 +0100
24336 @@ -0,0 +1,31 @@
24337 +# SPDX-License-Identifier: GPL-2.0
24338 +
24339 +# defined in ${srctree}/fs/fuse/inode.c
24340 +# tristate
24341 +ifdef CONFIG_FUSE_FS
24342 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24343 +endif
24344 +
24345 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24346 +# tristate
24347 +ifdef CONFIG_XFS_FS
24348 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24349 +endif
24350 +
24351 +# defined in ${srctree}/fs/configfs/mount.c
24352 +# tristate
24353 +ifdef CONFIG_CONFIGFS_FS
24354 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24355 +endif
24356 +
24357 +# defined in ${srctree}/fs/ubifs/ubifs.h
24358 +# tristate
24359 +ifdef CONFIG_UBIFS_FS
24360 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24361 +endif
24362 +
24363 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24364 +# tristate
24365 +ifdef CONFIG_HFSPLUS_FS
24366 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24367 +endif
24368 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24369 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24370 +++ linux/fs/aufs/Makefile      2021-12-03 15:38:59.929980643 +0100
24371 @@ -0,0 +1,46 @@
24372 +# SPDX-License-Identifier: GPL-2.0
24373 +
24374 +include ${src}/magic.mk
24375 +ifeq (${CONFIG_AUFS_FS},m)
24376 +include ${src}/conf.mk
24377 +endif
24378 +-include ${src}/priv_def.mk
24379 +
24380 +# cf. include/linux/kernel.h
24381 +# enable pr_debug
24382 +ccflags-y += -DDEBUG
24383 +# sparse requires the full pathname
24384 +ifdef M
24385 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24386 +else
24387 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24388 +endif
24389 +
24390 +obj-$(CONFIG_AUFS_FS) += aufs.o
24391 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24392 +       wkq.o vfsub.o dcsub.o \
24393 +       cpup.o whout.o wbr_policy.o \
24394 +       dinfo.o dentry.o \
24395 +       dynop.o \
24396 +       finfo.o file.o f_op.o \
24397 +       dir.o vdir.o \
24398 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24399 +       mvdown.o ioctl.o
24400 +
24401 +# all are boolean
24402 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24403 +aufs-$(CONFIG_SYSFS) += sysfs.o
24404 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24405 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24406 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24407 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24408 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24409 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24410 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24411 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24412 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24413 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24414 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24415 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24416 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24417 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24418 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24419 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24420 +++ linux/fs/aufs/module.c      2021-12-03 15:38:59.936647310 +0100
24421 @@ -0,0 +1,273 @@
24422 +// SPDX-License-Identifier: GPL-2.0
24423 +/*
24424 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24425 + *
24426 + * This program, aufs is free software; you can redistribute it and/or modify
24427 + * it under the terms of the GNU General Public License as published by
24428 + * the Free Software Foundation; either version 2 of the License, or
24429 + * (at your option) any later version.
24430 + *
24431 + * This program is distributed in the hope that it will be useful,
24432 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24433 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24434 + * GNU General Public License for more details.
24435 + *
24436 + * You should have received a copy of the GNU General Public License
24437 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24438 + */
24439 +
24440 +/*
24441 + * module global variables and operations
24442 + */
24443 +
24444 +#include <linux/module.h>
24445 +#include <linux/seq_file.h>
24446 +#include "aufs.h"
24447 +
24448 +/* shrinkable realloc */
24449 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24450 +{
24451 +       size_t sz;
24452 +       int diff;
24453 +
24454 +       sz = 0;
24455 +       diff = -1;
24456 +       if (p) {
24457 +#if 0 /* unused */
24458 +               if (!new_sz) {
24459 +                       au_kfree_rcu(p);
24460 +                       p = NULL;
24461 +                       goto out;
24462 +               }
24463 +#else
24464 +               AuDebugOn(!new_sz);
24465 +#endif
24466 +               sz = ksize(p);
24467 +               diff = au_kmidx_sub(sz, new_sz);
24468 +       }
24469 +       if (sz && !diff)
24470 +               goto out;
24471 +
24472 +       if (sz < new_sz)
24473 +               /* expand or SLOB */
24474 +               p = krealloc(p, new_sz, gfp);
24475 +       else if (new_sz < sz && may_shrink) {
24476 +               /* shrink */
24477 +               void *q;
24478 +
24479 +               q = kmalloc(new_sz, gfp);
24480 +               if (q) {
24481 +                       if (p) {
24482 +                               memcpy(q, p, new_sz);
24483 +                               au_kfree_try_rcu(p);
24484 +                       }
24485 +                       p = q;
24486 +               } else
24487 +                       p = NULL;
24488 +       }
24489 +
24490 +out:
24491 +       return p;
24492 +}
24493 +
24494 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24495 +                  int may_shrink)
24496 +{
24497 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24498 +       if (p && new_sz > nused)
24499 +               memset(p + nused, 0, new_sz - nused);
24500 +       return p;
24501 +}
24502 +
24503 +/* ---------------------------------------------------------------------- */
24504 +/*
24505 + * aufs caches
24506 + */
24507 +struct kmem_cache *au_cache[AuCache_Last];
24508 +
24509 +static void au_cache_fin(void)
24510 +{
24511 +       int i;
24512 +
24513 +       /*
24514 +        * Make sure all delayed rcu free inodes are flushed before we
24515 +        * destroy cache.
24516 +        */
24517 +       rcu_barrier();
24518 +
24519 +       /* excluding AuCache_HNOTIFY */
24520 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24521 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24522 +               kmem_cache_destroy(au_cache[i]);
24523 +               au_cache[i] = NULL;
24524 +       }
24525 +}
24526 +
24527 +static int __init au_cache_init(void)
24528 +{
24529 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24530 +       if (au_cache[AuCache_DINFO])
24531 +               /* SLAB_DESTROY_BY_RCU */
24532 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24533 +                                                      au_icntnr_init_once);
24534 +       if (au_cache[AuCache_ICNTNR])
24535 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24536 +                                                     au_fi_init_once);
24537 +       if (au_cache[AuCache_FINFO])
24538 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24539 +       if (au_cache[AuCache_VDIR])
24540 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24541 +       if (au_cache[AuCache_DEHSTR])
24542 +               return 0;
24543 +
24544 +       au_cache_fin();
24545 +       return -ENOMEM;
24546 +}
24547 +
24548 +/* ---------------------------------------------------------------------- */
24549 +
24550 +int au_dir_roflags;
24551 +
24552 +#ifdef CONFIG_AUFS_SBILIST
24553 +/*
24554 + * iterate_supers_type() doesn't protect us from
24555 + * remounting (branch management)
24556 + */
24557 +struct hlist_bl_head au_sbilist;
24558 +#endif
24559 +
24560 +/*
24561 + * functions for module interface.
24562 + */
24563 +MODULE_LICENSE("GPL");
24564 +/* MODULE_LICENSE("GPL v2"); */
24565 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24566 +MODULE_DESCRIPTION(AUFS_NAME
24567 +       " -- Advanced multi layered unification filesystem");
24568 +MODULE_VERSION(AUFS_VERSION);
24569 +MODULE_ALIAS_FS(AUFS_NAME);
24570 +
24571 +/* this module parameter has no meaning when SYSFS is disabled */
24572 +int sysaufs_brs = 1;
24573 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24574 +module_param_named(brs, sysaufs_brs, int, 0444);
24575 +
24576 +/* this module parameter has no meaning when USER_NS is disabled */
24577 +bool au_userns;
24578 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24579 +module_param_named(allow_userns, au_userns, bool, 0444);
24580 +
24581 +/* ---------------------------------------------------------------------- */
24582 +
24583 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24584 +
24585 +int au_seq_path(struct seq_file *seq, struct path *path)
24586 +{
24587 +       int err;
24588 +
24589 +       err = seq_path(seq, path, au_esc_chars);
24590 +       if (err >= 0)
24591 +               err = 0;
24592 +       else
24593 +               err = -ENOMEM;
24594 +
24595 +       return err;
24596 +}
24597 +
24598 +/* ---------------------------------------------------------------------- */
24599 +
24600 +static int __init aufs_init(void)
24601 +{
24602 +       int err, i;
24603 +       char *p;
24604 +
24605 +       p = au_esc_chars;
24606 +       for (i = 1; i <= ' '; i++)
24607 +               *p++ = i;
24608 +       *p++ = '\\';
24609 +       *p++ = '\x7f';
24610 +       *p = 0;
24611 +
24612 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24613 +
24614 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24615 +       for (i = 0; i < AuIop_Last; i++)
24616 +               aufs_iop_nogetattr[i].getattr = NULL;
24617 +
24618 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24619 +
24620 +       au_sbilist_init();
24621 +       sysaufs_brs_init();
24622 +       au_debug_init();
24623 +       au_dy_init();
24624 +       err = sysaufs_init();
24625 +       if (unlikely(err))
24626 +               goto out;
24627 +       err = dbgaufs_init();
24628 +       if (unlikely(err))
24629 +               goto out_sysaufs;
24630 +       err = au_procfs_init();
24631 +       if (unlikely(err))
24632 +               goto out_dbgaufs;
24633 +       err = au_wkq_init();
24634 +       if (unlikely(err))
24635 +               goto out_procfs;
24636 +       err = au_loopback_init();
24637 +       if (unlikely(err))
24638 +               goto out_wkq;
24639 +       err = au_hnotify_init();
24640 +       if (unlikely(err))
24641 +               goto out_loopback;
24642 +       err = au_sysrq_init();
24643 +       if (unlikely(err))
24644 +               goto out_hin;
24645 +       err = au_cache_init();
24646 +       if (unlikely(err))
24647 +               goto out_sysrq;
24648 +
24649 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24650 +       err = register_filesystem(&aufs_fs_type);
24651 +       if (unlikely(err))
24652 +               goto out_cache;
24653 +
24654 +       /* since we define pr_fmt, call printk directly */
24655 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24656 +       goto out; /* success */
24657 +
24658 +out_cache:
24659 +       au_cache_fin();
24660 +out_sysrq:
24661 +       au_sysrq_fin();
24662 +out_hin:
24663 +       au_hnotify_fin();
24664 +out_loopback:
24665 +       au_loopback_fin();
24666 +out_wkq:
24667 +       au_wkq_fin();
24668 +out_procfs:
24669 +       au_procfs_fin();
24670 +out_dbgaufs:
24671 +       dbgaufs_fin();
24672 +out_sysaufs:
24673 +       sysaufs_fin();
24674 +       au_dy_fin();
24675 +out:
24676 +       return err;
24677 +}
24678 +
24679 +static void __exit aufs_exit(void)
24680 +{
24681 +       unregister_filesystem(&aufs_fs_type);
24682 +       au_cache_fin();
24683 +       au_sysrq_fin();
24684 +       au_hnotify_fin();
24685 +       au_loopback_fin();
24686 +       au_wkq_fin();
24687 +       au_procfs_fin();
24688 +       dbgaufs_fin();
24689 +       sysaufs_fin();
24690 +       au_dy_fin();
24691 +}
24692 +
24693 +module_init(aufs_init);
24694 +module_exit(aufs_exit);
24695 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24696 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24697 +++ linux/fs/aufs/module.h      2021-12-03 15:40:58.236647297 +0100
24698 @@ -0,0 +1,166 @@
24699 +/* SPDX-License-Identifier: GPL-2.0 */
24700 +/*
24701 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24702 + *
24703 + * This program, aufs is free software; you can redistribute it and/or modify
24704 + * it under the terms of the GNU General Public License as published by
24705 + * the Free Software Foundation; either version 2 of the License, or
24706 + * (at your option) any later version.
24707 + *
24708 + * This program is distributed in the hope that it will be useful,
24709 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24710 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24711 + * GNU General Public License for more details.
24712 + *
24713 + * You should have received a copy of the GNU General Public License
24714 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24715 + */
24716 +
24717 +/*
24718 + * module initialization and module-global
24719 + */
24720 +
24721 +#ifndef __AUFS_MODULE_H__
24722 +#define __AUFS_MODULE_H__
24723 +
24724 +#ifdef __KERNEL__
24725 +
24726 +#include <linux/slab.h>
24727 +#include "debug.h"
24728 +#include "dentry.h"
24729 +#include "dir.h"
24730 +#include "file.h"
24731 +#include "inode.h"
24732 +
24733 +struct path;
24734 +struct seq_file;
24735 +
24736 +/* module parameters */
24737 +extern int sysaufs_brs;
24738 +extern bool au_userns;
24739 +
24740 +/* ---------------------------------------------------------------------- */
24741 +
24742 +extern int au_dir_roflags;
24743 +
24744 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24745 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24746 +                  int may_shrink);
24747 +
24748 +/*
24749 + * Comparing the size of the object with sizeof(struct rcu_head)
24750 + * case 1: object is always larger
24751 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24752 + * case 2: object is always smaller
24753 + *     --> au_kfree_small()
24754 + * case 3: object can be any size
24755 + *     --> au_kfree_try_rcu()
24756 + */
24757 +
24758 +static inline void au_kfree_do_rcu(const void *p)
24759 +{
24760 +       struct {
24761 +               struct rcu_head rcu;
24762 +       } *a = (void *)p;
24763 +
24764 +       kfree_rcu(a, rcu);
24765 +}
24766 +
24767 +#define au_kfree_rcu(_p) do {                                          \
24768 +               typeof(_p) p = (_p);                                    \
24769 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24770 +               if (p)                                                  \
24771 +                       au_kfree_do_rcu(p);                             \
24772 +       } while (0)
24773 +
24774 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24775 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24776 +
24777 +static inline void au_kfree_try_rcu(const void *p)
24778 +{
24779 +       if (!p)
24780 +               return;
24781 +       if (au_kfree_sz_test(p))
24782 +               au_kfree_do_rcu(p);
24783 +       else
24784 +               kfree(p);
24785 +}
24786 +
24787 +static inline void au_kfree_small(const void *p)
24788 +{
24789 +       if (!p)
24790 +               return;
24791 +       AuDebugOn(au_kfree_sz_test(p));
24792 +       kfree(p);
24793 +}
24794 +
24795 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24796 +{
24797 +#ifndef CONFIG_SLOB
24798 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
24799 +#else
24800 +       return -1; /* SLOB is untested */
24801 +#endif
24802 +}
24803 +
24804 +int au_seq_path(struct seq_file *seq, struct path *path);
24805 +
24806 +#ifdef CONFIG_PROC_FS
24807 +/* procfs.c */
24808 +int __init au_procfs_init(void);
24809 +void au_procfs_fin(void);
24810 +#else
24811 +AuStubInt0(au_procfs_init, void);
24812 +AuStubVoid(au_procfs_fin, void);
24813 +#endif
24814 +
24815 +/* ---------------------------------------------------------------------- */
24816 +
24817 +/* kmem cache */
24818 +enum {
24819 +       AuCache_DINFO,
24820 +       AuCache_ICNTNR,
24821 +       AuCache_FINFO,
24822 +       AuCache_VDIR,
24823 +       AuCache_DEHSTR,
24824 +       AuCache_HNOTIFY, /* must be last */
24825 +       AuCache_Last
24826 +};
24827 +
24828 +extern struct kmem_cache *au_cache[AuCache_Last];
24829 +
24830 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24831 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24832 +#define AuCacheCtor(type, ctor)        \
24833 +       kmem_cache_create(#type, sizeof(struct type), \
24834 +                         __alignof__(struct type), AuCacheFlags, ctor)
24835 +
24836 +#define AuCacheFuncs(name, index)                                      \
24837 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24838 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24839 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24840 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24841 +                                                                       \
24842 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24843 +       { void *p = rcu;                                                \
24844 +               p -= offsetof(struct au_##name, rcu);                   \
24845 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24846 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24847 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24848 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24849 +                                                                       \
24850 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24851 +       { /* au_cache_free_##name##_norcu(p); */                        \
24852 +               au_cache_free_##name##_rcu(p); }
24853 +
24854 +AuCacheFuncs(dinfo, DINFO);
24855 +AuCacheFuncs(icntnr, ICNTNR);
24856 +AuCacheFuncs(finfo, FINFO);
24857 +AuCacheFuncs(vdir, VDIR);
24858 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24859 +#ifdef CONFIG_AUFS_HNOTIFY
24860 +AuCacheFuncs(hnotify, HNOTIFY);
24861 +#endif
24862 +
24863 +#endif /* __KERNEL__ */
24864 +#endif /* __AUFS_MODULE_H__ */
24865 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24866 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24867 +++ linux/fs/aufs/mvdown.c      2021-12-03 15:38:59.939980643 +0100
24868 @@ -0,0 +1,706 @@
24869 +// SPDX-License-Identifier: GPL-2.0
24870 +/*
24871 + * Copyright (C) 2011-2021 Junjiro R. Okajima
24872 + *
24873 + * This program, aufs is free software; you can redistribute it and/or modify
24874 + * it under the terms of the GNU General Public License as published by
24875 + * the Free Software Foundation; either version 2 of the License, or
24876 + * (at your option) any later version.
24877 + *
24878 + * This program is distributed in the hope that it will be useful,
24879 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24880 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24881 + * GNU General Public License for more details.
24882 + *
24883 + * You should have received a copy of the GNU General Public License
24884 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24885 + */
24886 +
24887 +/*
24888 + * move-down, opposite of copy-up
24889 + */
24890 +
24891 +#include "aufs.h"
24892 +
24893 +struct au_mvd_args {
24894 +       struct {
24895 +               struct super_block *h_sb;
24896 +               struct dentry *h_parent;
24897 +               struct au_hinode *hdir;
24898 +               struct inode *h_dir, *h_inode;
24899 +               struct au_pin pin;
24900 +       } info[AUFS_MVDOWN_NARRAY];
24901 +
24902 +       struct aufs_mvdown mvdown;
24903 +       struct dentry *dentry, *parent;
24904 +       struct inode *inode, *dir;
24905 +       struct super_block *sb;
24906 +       aufs_bindex_t bopq, bwh, bfound;
24907 +       unsigned char rename_lock;
24908 +};
24909 +
24910 +#define mvd_errno              mvdown.au_errno
24911 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24912 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24913 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24914 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24915 +
24916 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24917 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24918 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24919 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24920 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24921 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24922 +
24923 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24924 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24925 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24926 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24927 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24928 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24929 +
24930 +#define AU_MVD_PR(flag, ...) do {                      \
24931 +               if (flag)                               \
24932 +                       pr_err(__VA_ARGS__);            \
24933 +       } while (0)
24934 +
24935 +static int find_lower_writable(struct au_mvd_args *a)
24936 +{
24937 +       struct super_block *sb;
24938 +       aufs_bindex_t bindex, bbot;
24939 +       struct au_branch *br;
24940 +
24941 +       sb = a->sb;
24942 +       bindex = a->mvd_bsrc;
24943 +       bbot = au_sbbot(sb);
24944 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24945 +               for (bindex++; bindex <= bbot; bindex++) {
24946 +                       br = au_sbr(sb, bindex);
24947 +                       if (au_br_fhsm(br->br_perm)
24948 +                           && !sb_rdonly(au_br_sb(br)))
24949 +                               return bindex;
24950 +               }
24951 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24952 +               for (bindex++; bindex <= bbot; bindex++) {
24953 +                       br = au_sbr(sb, bindex);
24954 +                       if (!au_br_rdonly(br))
24955 +                               return bindex;
24956 +               }
24957 +       else
24958 +               for (bindex++; bindex <= bbot; bindex++) {
24959 +                       br = au_sbr(sb, bindex);
24960 +                       if (!sb_rdonly(au_br_sb(br))) {
24961 +                               if (au_br_rdonly(br))
24962 +                                       a->mvdown.flags
24963 +                                               |= AUFS_MVDOWN_ROLOWER_R;
24964 +                               return bindex;
24965 +                       }
24966 +               }
24967 +
24968 +       return -1;
24969 +}
24970 +
24971 +/* make the parent dir on bdst */
24972 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24973 +{
24974 +       int err;
24975 +
24976 +       err = 0;
24977 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24978 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24979 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24980 +       a->mvd_h_dst_parent = NULL;
24981 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
24982 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24983 +       if (!a->mvd_h_dst_parent) {
24984 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24985 +               if (unlikely(err)) {
24986 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
24987 +                       goto out;
24988 +               }
24989 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24990 +       }
24991 +
24992 +out:
24993 +       AuTraceErr(err);
24994 +       return err;
24995 +}
24996 +
24997 +/* lock them all */
24998 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
24999 +{
25000 +       int err;
25001 +       struct dentry *h_trap;
25002 +
25003 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25004 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25005 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25006 +                    au_opt_udba(a->sb),
25007 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25008 +       AuTraceErr(err);
25009 +       if (unlikely(err)) {
25010 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25011 +               goto out;
25012 +       }
25013 +
25014 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25015 +               a->rename_lock = 0;
25016 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25017 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25018 +                           au_opt_udba(a->sb),
25019 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25020 +               err = au_do_pin(&a->mvd_pin_src);
25021 +               AuTraceErr(err);
25022 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25023 +               if (unlikely(err)) {
25024 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25025 +                       goto out_dst;
25026 +               }
25027 +               goto out; /* success */
25028 +       }
25029 +
25030 +       a->rename_lock = 1;
25031 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25032 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25033 +                    au_opt_udba(a->sb),
25034 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25035 +       AuTraceErr(err);
25036 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25037 +       if (unlikely(err)) {
25038 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25039 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25040 +               goto out_dst;
25041 +       }
25042 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25043 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25044 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25045 +       if (h_trap) {
25046 +               err = (h_trap != a->mvd_h_src_parent);
25047 +               if (err)
25048 +                       err = (h_trap != a->mvd_h_dst_parent);
25049 +       }
25050 +       BUG_ON(err); /* it should never happen */
25051 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25052 +               err = -EBUSY;
25053 +               AuTraceErr(err);
25054 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25055 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25056 +               au_pin_hdir_lock(&a->mvd_pin_src);
25057 +               au_unpin(&a->mvd_pin_src);
25058 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25059 +               goto out_dst;
25060 +       }
25061 +       goto out; /* success */
25062 +
25063 +out_dst:
25064 +       au_unpin(&a->mvd_pin_dst);
25065 +out:
25066 +       AuTraceErr(err);
25067 +       return err;
25068 +}
25069 +
25070 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25071 +{
25072 +       if (!a->rename_lock)
25073 +               au_unpin(&a->mvd_pin_src);
25074 +       else {
25075 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25076 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25077 +               au_pin_hdir_lock(&a->mvd_pin_src);
25078 +               au_unpin(&a->mvd_pin_src);
25079 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25080 +       }
25081 +       au_unpin(&a->mvd_pin_dst);
25082 +}
25083 +
25084 +/* copy-down the file */
25085 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25086 +{
25087 +       int err;
25088 +       struct au_cp_generic cpg = {
25089 +               .dentry = a->dentry,
25090 +               .bdst   = a->mvd_bdst,
25091 +               .bsrc   = a->mvd_bsrc,
25092 +               .len    = -1,
25093 +               .pin    = &a->mvd_pin_dst,
25094 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25095 +       };
25096 +
25097 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25098 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25099 +               au_fset_cpup(cpg.flags, OVERWRITE);
25100 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25101 +               au_fset_cpup(cpg.flags, RWDST);
25102 +       err = au_sio_cpdown_simple(&cpg);
25103 +       if (unlikely(err))
25104 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25105 +
25106 +       AuTraceErr(err);
25107 +       return err;
25108 +}
25109 +
25110 +/*
25111 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25112 + * were sleeping
25113 + */
25114 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25115 +{
25116 +       int err;
25117 +       struct path h_path;
25118 +       struct au_branch *br;
25119 +       struct inode *delegated;
25120 +
25121 +       br = au_sbr(a->sb, a->mvd_bdst);
25122 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25123 +       err = PTR_ERR(h_path.dentry);
25124 +       if (IS_ERR(h_path.dentry)) {
25125 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25126 +               goto out;
25127 +       }
25128 +
25129 +       err = 0;
25130 +       if (d_is_positive(h_path.dentry)) {
25131 +               h_path.mnt = au_br_mnt(br);
25132 +               delegated = NULL;
25133 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25134 +                                  &delegated, /*force*/0);
25135 +               if (unlikely(err == -EWOULDBLOCK)) {
25136 +                       pr_warn("cannot retry for NFSv4 delegation"
25137 +                               " for an internal unlink\n");
25138 +                       iput(delegated);
25139 +               }
25140 +               if (unlikely(err))
25141 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25142 +       }
25143 +       dput(h_path.dentry);
25144 +
25145 +out:
25146 +       AuTraceErr(err);
25147 +       return err;
25148 +}
25149 +
25150 +/*
25151 + * unlink the topmost h_dentry
25152 + */
25153 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25154 +{
25155 +       int err;
25156 +       struct path h_path;
25157 +       struct inode *delegated;
25158 +
25159 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25160 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25161 +       delegated = NULL;
25162 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25163 +       if (unlikely(err == -EWOULDBLOCK)) {
25164 +               pr_warn("cannot retry for NFSv4 delegation"
25165 +                       " for an internal unlink\n");
25166 +               iput(delegated);
25167 +       }
25168 +       if (unlikely(err))
25169 +               AU_MVD_PR(dmsg, "unlink failed\n");
25170 +
25171 +       AuTraceErr(err);
25172 +       return err;
25173 +}
25174 +
25175 +/* Since mvdown succeeded, we ignore an error of this function */
25176 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25177 +{
25178 +       int err;
25179 +       struct au_branch *br;
25180 +
25181 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25182 +       br = au_sbr(a->sb, a->mvd_bsrc);
25183 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25184 +       if (!err) {
25185 +               br = au_sbr(a->sb, a->mvd_bdst);
25186 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25187 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25188 +       }
25189 +       if (!err)
25190 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25191 +       else
25192 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25193 +}
25194 +
25195 +/*
25196 + * copy-down the file and unlink the bsrc file.
25197 + * - unlink the bdst whout if exist
25198 + * - copy-down the file (with whtmp name and rename)
25199 + * - unlink the bsrc file
25200 + */
25201 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25202 +{
25203 +       int err;
25204 +
25205 +       err = au_do_mkdir(dmsg, a);
25206 +       if (!err)
25207 +               err = au_do_lock(dmsg, a);
25208 +       if (unlikely(err))
25209 +               goto out;
25210 +
25211 +       /*
25212 +        * do not revert the activities we made on bdst since they should be
25213 +        * harmless in aufs.
25214 +        */
25215 +
25216 +       err = au_do_cpdown(dmsg, a);
25217 +       if (!err)
25218 +               err = au_do_unlink_wh(dmsg, a);
25219 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25220 +               err = au_do_unlink(dmsg, a);
25221 +       if (unlikely(err))
25222 +               goto out_unlock;
25223 +
25224 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25225 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25226 +       if (find_lower_writable(a) < 0)
25227 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25228 +
25229 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25230 +               au_do_stfs(dmsg, a);
25231 +
25232 +       /* maintain internal array */
25233 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25234 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25235 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25236 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25237 +               au_set_ibtop(a->inode, a->mvd_bdst);
25238 +       } else {
25239 +               /* hide the lower */
25240 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25241 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25242 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25243 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25244 +       }
25245 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25246 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25247 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25248 +               au_set_ibbot(a->inode, a->mvd_bdst);
25249 +
25250 +out_unlock:
25251 +       au_do_unlock(dmsg, a);
25252 +out:
25253 +       AuTraceErr(err);
25254 +       return err;
25255 +}
25256 +
25257 +/* ---------------------------------------------------------------------- */
25258 +
25259 +/* make sure the file is idle */
25260 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25261 +{
25262 +       int err, plinked;
25263 +
25264 +       err = 0;
25265 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25266 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25267 +           && au_dcount(a->dentry) == 1
25268 +           && atomic_read(&a->inode->i_count) == 1
25269 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25270 +           && (!plinked || !au_plink_test(a->inode))
25271 +           && a->inode->i_nlink == 1)
25272 +               goto out;
25273 +
25274 +       err = -EBUSY;
25275 +       AU_MVD_PR(dmsg,
25276 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25277 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25278 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25279 +                 a->mvd_h_src_inode->i_nlink,
25280 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25281 +
25282 +out:
25283 +       AuTraceErr(err);
25284 +       return err;
25285 +}
25286 +
25287 +/* make sure the parent dir is fine */
25288 +static int au_mvd_args_parent(const unsigned char dmsg,
25289 +                             struct au_mvd_args *a)
25290 +{
25291 +       int err;
25292 +       aufs_bindex_t bindex;
25293 +
25294 +       err = 0;
25295 +       if (unlikely(au_alive_dir(a->parent))) {
25296 +               err = -ENOENT;
25297 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25298 +               goto out;
25299 +       }
25300 +
25301 +       a->bopq = au_dbdiropq(a->parent);
25302 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25303 +       AuDbg("b%d\n", bindex);
25304 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25305 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25306 +               err = -EINVAL;
25307 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25308 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25309 +                         a->bopq, a->mvd_bdst);
25310 +       }
25311 +
25312 +out:
25313 +       AuTraceErr(err);
25314 +       return err;
25315 +}
25316 +
25317 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25318 +                                   struct au_mvd_args *a)
25319 +{
25320 +       int err;
25321 +       struct au_dinfo *dinfo, *tmp;
25322 +
25323 +       /* lookup the next lower positive entry */
25324 +       err = -ENOMEM;
25325 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25326 +       if (unlikely(!tmp))
25327 +               goto out;
25328 +
25329 +       a->bfound = -1;
25330 +       a->bwh = -1;
25331 +       dinfo = au_di(a->dentry);
25332 +       au_di_cp(tmp, dinfo);
25333 +       au_di_swap(tmp, dinfo);
25334 +
25335 +       /* returns the number of positive dentries */
25336 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25337 +                            /* AuLkup_IGNORE_PERM */ 0);
25338 +       if (!err)
25339 +               a->bwh = au_dbwh(a->dentry);
25340 +       else if (err > 0)
25341 +               a->bfound = au_dbtop(a->dentry);
25342 +
25343 +       au_di_swap(tmp, dinfo);
25344 +       au_rw_write_unlock(&tmp->di_rwsem);
25345 +       au_di_free(tmp);
25346 +       if (unlikely(err < 0))
25347 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25348 +
25349 +       /*
25350 +        * here, we have these cases.
25351 +        * bfound == -1
25352 +        *      no positive dentry under bsrc. there are more sub-cases.
25353 +        *      bwh < 0
25354 +        *              there no whiteout, we can safely move-down.
25355 +        *      bwh <= bsrc
25356 +        *              impossible
25357 +        *      bsrc < bwh && bwh < bdst
25358 +        *              there is a whiteout on RO branch. cannot proceed.
25359 +        *      bwh == bdst
25360 +        *              there is a whiteout on the RW target branch. it should
25361 +        *              be removed.
25362 +        *      bdst < bwh
25363 +        *              there is a whiteout somewhere unrelated branch.
25364 +        * -1 < bfound && bfound <= bsrc
25365 +        *      impossible.
25366 +        * bfound < bdst
25367 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25368 +        *      proceed.
25369 +        * bfound == bdst
25370 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25371 +        *      error.
25372 +        * bdst < bfound
25373 +        *      found, after we create the file on bdst, it will be hidden.
25374 +        */
25375 +
25376 +       AuDebugOn(a->bfound == -1
25377 +                 && a->bwh != -1
25378 +                 && a->bwh <= a->mvd_bsrc);
25379 +       AuDebugOn(-1 < a->bfound
25380 +                 && a->bfound <= a->mvd_bsrc);
25381 +
25382 +       err = -EINVAL;
25383 +       if (a->bfound == -1
25384 +           && a->mvd_bsrc < a->bwh
25385 +           && a->bwh != -1
25386 +           && a->bwh < a->mvd_bdst) {
25387 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25388 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25389 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25390 +               goto out;
25391 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25392 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25393 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25394 +                         a->mvd_bdst, a->bfound);
25395 +               goto out;
25396 +       }
25397 +
25398 +       err = 0; /* success */
25399 +
25400 +out:
25401 +       AuTraceErr(err);
25402 +       return err;
25403 +}
25404 +
25405 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25406 +{
25407 +       int err;
25408 +
25409 +       err = 0;
25410 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25411 +           && a->bfound == a->mvd_bdst)
25412 +               err = -EEXIST;
25413 +       AuTraceErr(err);
25414 +       return err;
25415 +}
25416 +
25417 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25418 +{
25419 +       int err;
25420 +       struct au_branch *br;
25421 +
25422 +       err = -EISDIR;
25423 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25424 +               goto out;
25425 +
25426 +       err = -EINVAL;
25427 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25428 +               a->mvd_bsrc = au_ibtop(a->inode);
25429 +       else {
25430 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25431 +               if (unlikely(a->mvd_bsrc < 0
25432 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25433 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25434 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25435 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25436 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25437 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25438 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25439 +                       AU_MVD_PR(dmsg, "no upper\n");
25440 +                       goto out;
25441 +               }
25442 +       }
25443 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25444 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25445 +               AU_MVD_PR(dmsg, "on the bottom\n");
25446 +               goto out;
25447 +       }
25448 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25449 +       br = au_sbr(a->sb, a->mvd_bsrc);
25450 +       err = au_br_rdonly(br);
25451 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25452 +               if (unlikely(err))
25453 +                       goto out;
25454 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25455 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25456 +               if (err)
25457 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25458 +               /* go on */
25459 +       } else
25460 +               goto out;
25461 +
25462 +       err = -EINVAL;
25463 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25464 +               a->mvd_bdst = find_lower_writable(a);
25465 +               if (unlikely(a->mvd_bdst < 0)) {
25466 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25467 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25468 +                       goto out;
25469 +               }
25470 +       } else {
25471 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25472 +               if (unlikely(a->mvd_bdst < 0
25473 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25474 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25475 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25476 +                       goto out;
25477 +               }
25478 +       }
25479 +
25480 +       err = au_mvd_args_busy(dmsg, a);
25481 +       if (!err)
25482 +               err = au_mvd_args_parent(dmsg, a);
25483 +       if (!err)
25484 +               err = au_mvd_args_intermediate(dmsg, a);
25485 +       if (!err)
25486 +               err = au_mvd_args_exist(dmsg, a);
25487 +       if (!err)
25488 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25489 +
25490 +out:
25491 +       AuTraceErr(err);
25492 +       return err;
25493 +}
25494 +
25495 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25496 +{
25497 +       int err, e;
25498 +       unsigned char dmsg;
25499 +       struct au_mvd_args *args;
25500 +       struct inode *inode;
25501 +
25502 +       inode = d_inode(dentry);
25503 +       err = -EPERM;
25504 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25505 +               goto out;
25506 +
25507 +       err = -ENOMEM;
25508 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25509 +       if (unlikely(!args))
25510 +               goto out;
25511 +
25512 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25513 +       if (!err)
25514 +               /* VERIFY_WRITE */
25515 +               err = !access_ok(uarg, sizeof(*uarg));
25516 +       if (unlikely(err)) {
25517 +               err = -EFAULT;
25518 +               AuTraceErr(err);
25519 +               goto out_free;
25520 +       }
25521 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25522 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25523 +       args->mvdown.au_errno = 0;
25524 +       args->dentry = dentry;
25525 +       args->inode = inode;
25526 +       args->sb = dentry->d_sb;
25527 +
25528 +       err = -ENOENT;
25529 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25530 +       args->parent = dget_parent(dentry);
25531 +       args->dir = d_inode(args->parent);
25532 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25533 +       dput(args->parent);
25534 +       if (unlikely(args->parent != dentry->d_parent)) {
25535 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25536 +               goto out_dir;
25537 +       }
25538 +
25539 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25540 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25541 +       if (unlikely(err))
25542 +               goto out_inode;
25543 +
25544 +       di_write_lock_parent(args->parent);
25545 +       err = au_mvd_args(dmsg, args);
25546 +       if (unlikely(err))
25547 +               goto out_parent;
25548 +
25549 +       err = au_do_mvdown(dmsg, args);
25550 +       if (unlikely(err))
25551 +               goto out_parent;
25552 +
25553 +       au_cpup_attr_timesizes(args->dir);
25554 +       au_cpup_attr_timesizes(inode);
25555 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25556 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25557 +       /* au_digen_dec(dentry); */
25558 +
25559 +out_parent:
25560 +       di_write_unlock(args->parent);
25561 +       aufs_read_unlock(dentry, AuLock_DW);
25562 +out_inode:
25563 +       inode_unlock(inode);
25564 +out_dir:
25565 +       inode_unlock(args->dir);
25566 +out_free:
25567 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25568 +       if (unlikely(e))
25569 +               err = -EFAULT;
25570 +       au_kfree_rcu(args);
25571 +out:
25572 +       AuTraceErr(err);
25573 +       return err;
25574 +}
25575 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25576 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25577 +++ linux/fs/aufs/opts.c        2021-12-03 15:38:59.939980643 +0100
25578 @@ -0,0 +1,1880 @@
25579 +// SPDX-License-Identifier: GPL-2.0
25580 +/*
25581 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25582 + *
25583 + * This program, aufs is free software; you can redistribute it and/or modify
25584 + * it under the terms of the GNU General Public License as published by
25585 + * the Free Software Foundation; either version 2 of the License, or
25586 + * (at your option) any later version.
25587 + *
25588 + * This program is distributed in the hope that it will be useful,
25589 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25590 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25591 + * GNU General Public License for more details.
25592 + *
25593 + * You should have received a copy of the GNU General Public License
25594 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25595 + */
25596 +
25597 +/*
25598 + * mount options/flags
25599 + */
25600 +
25601 +#include <linux/namei.h>
25602 +#include <linux/types.h> /* a distribution requires */
25603 +#include <linux/parser.h>
25604 +#include "aufs.h"
25605 +
25606 +/* ---------------------------------------------------------------------- */
25607 +
25608 +enum {
25609 +       Opt_br,
25610 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25611 +       Opt_idel, Opt_imod,
25612 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25613 +       Opt_rdblk_def, Opt_rdhash_def,
25614 +       Opt_xino, Opt_noxino,
25615 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25616 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25617 +       Opt_trunc_xib, Opt_notrunc_xib,
25618 +       Opt_shwh, Opt_noshwh,
25619 +       Opt_plink, Opt_noplink, Opt_list_plink,
25620 +       Opt_udba,
25621 +       Opt_dio, Opt_nodio,
25622 +       Opt_diropq_a, Opt_diropq_w,
25623 +       Opt_warn_perm, Opt_nowarn_perm,
25624 +       Opt_wbr_copyup, Opt_wbr_create,
25625 +       Opt_fhsm_sec,
25626 +       Opt_verbose, Opt_noverbose,
25627 +       Opt_sum, Opt_nosum, Opt_wsum,
25628 +       Opt_dirperm1, Opt_nodirperm1,
25629 +       Opt_dirren, Opt_nodirren,
25630 +       Opt_acl, Opt_noacl,
25631 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25632 +};
25633 +
25634 +static match_table_t options = {
25635 +       {Opt_br, "br=%s"},
25636 +       {Opt_br, "br:%s"},
25637 +
25638 +       {Opt_add, "add=%d:%s"},
25639 +       {Opt_add, "add:%d:%s"},
25640 +       {Opt_add, "ins=%d:%s"},
25641 +       {Opt_add, "ins:%d:%s"},
25642 +       {Opt_append, "append=%s"},
25643 +       {Opt_append, "append:%s"},
25644 +       {Opt_prepend, "prepend=%s"},
25645 +       {Opt_prepend, "prepend:%s"},
25646 +
25647 +       {Opt_del, "del=%s"},
25648 +       {Opt_del, "del:%s"},
25649 +       /* {Opt_idel, "idel:%d"}, */
25650 +       {Opt_mod, "mod=%s"},
25651 +       {Opt_mod, "mod:%s"},
25652 +       /* {Opt_imod, "imod:%d:%s"}, */
25653 +
25654 +       {Opt_dirwh, "dirwh=%d"},
25655 +
25656 +       {Opt_xino, "xino=%s"},
25657 +       {Opt_noxino, "noxino"},
25658 +       {Opt_trunc_xino, "trunc_xino"},
25659 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25660 +       {Opt_notrunc_xino, "notrunc_xino"},
25661 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25662 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25663 +       /* {Opt_zxino, "zxino=%s"}, */
25664 +       {Opt_trunc_xib, "trunc_xib"},
25665 +       {Opt_notrunc_xib, "notrunc_xib"},
25666 +
25667 +#ifdef CONFIG_PROC_FS
25668 +       {Opt_plink, "plink"},
25669 +#else
25670 +       {Opt_ignore_silent, "plink"},
25671 +#endif
25672 +
25673 +       {Opt_noplink, "noplink"},
25674 +
25675 +#ifdef CONFIG_AUFS_DEBUG
25676 +       {Opt_list_plink, "list_plink"},
25677 +#endif
25678 +
25679 +       {Opt_udba, "udba=%s"},
25680 +
25681 +       {Opt_dio, "dio"},
25682 +       {Opt_nodio, "nodio"},
25683 +
25684 +#ifdef CONFIG_AUFS_DIRREN
25685 +       {Opt_dirren, "dirren"},
25686 +       {Opt_nodirren, "nodirren"},
25687 +#else
25688 +       {Opt_ignore, "dirren"},
25689 +       {Opt_ignore_silent, "nodirren"},
25690 +#endif
25691 +
25692 +#ifdef CONFIG_AUFS_FHSM
25693 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25694 +#else
25695 +       {Opt_ignore, "fhsm_sec=%d"},
25696 +#endif
25697 +
25698 +       {Opt_diropq_a, "diropq=always"},
25699 +       {Opt_diropq_a, "diropq=a"},
25700 +       {Opt_diropq_w, "diropq=whiteouted"},
25701 +       {Opt_diropq_w, "diropq=w"},
25702 +
25703 +       {Opt_warn_perm, "warn_perm"},
25704 +       {Opt_nowarn_perm, "nowarn_perm"},
25705 +
25706 +       /* keep them temporary */
25707 +       {Opt_ignore_silent, "nodlgt"},
25708 +       {Opt_ignore, "clean_plink"},
25709 +
25710 +#ifdef CONFIG_AUFS_SHWH
25711 +       {Opt_shwh, "shwh"},
25712 +#endif
25713 +       {Opt_noshwh, "noshwh"},
25714 +
25715 +       {Opt_dirperm1, "dirperm1"},
25716 +       {Opt_nodirperm1, "nodirperm1"},
25717 +
25718 +       {Opt_verbose, "verbose"},
25719 +       {Opt_verbose, "v"},
25720 +       {Opt_noverbose, "noverbose"},
25721 +       {Opt_noverbose, "quiet"},
25722 +       {Opt_noverbose, "q"},
25723 +       {Opt_noverbose, "silent"},
25724 +
25725 +       {Opt_sum, "sum"},
25726 +       {Opt_nosum, "nosum"},
25727 +       {Opt_wsum, "wsum"},
25728 +
25729 +       {Opt_rdcache, "rdcache=%d"},
25730 +       {Opt_rdblk, "rdblk=%d"},
25731 +       {Opt_rdblk_def, "rdblk=def"},
25732 +       {Opt_rdhash, "rdhash=%d"},
25733 +       {Opt_rdhash_def, "rdhash=def"},
25734 +
25735 +       {Opt_wbr_create, "create=%s"},
25736 +       {Opt_wbr_create, "create_policy=%s"},
25737 +       {Opt_wbr_copyup, "cpup=%s"},
25738 +       {Opt_wbr_copyup, "copyup=%s"},
25739 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25740 +
25741 +       /* generic VFS flag */
25742 +#ifdef CONFIG_FS_POSIX_ACL
25743 +       {Opt_acl, "acl"},
25744 +       {Opt_noacl, "noacl"},
25745 +#else
25746 +       {Opt_ignore, "acl"},
25747 +       {Opt_ignore_silent, "noacl"},
25748 +#endif
25749 +
25750 +       /* internal use for the scripts */
25751 +       {Opt_ignore_silent, "si=%s"},
25752 +
25753 +       {Opt_br, "dirs=%s"},
25754 +       {Opt_ignore, "debug=%d"},
25755 +       {Opt_ignore, "delete=whiteout"},
25756 +       {Opt_ignore, "delete=all"},
25757 +       {Opt_ignore, "imap=%s"},
25758 +
25759 +       /* temporary workaround, due to old mount(8)? */
25760 +       {Opt_ignore_silent, "relatime"},
25761 +
25762 +       {Opt_err, NULL}
25763 +};
25764 +
25765 +/* ---------------------------------------------------------------------- */
25766 +
25767 +static const char *au_parser_pattern(int val, match_table_t tbl)
25768 +{
25769 +       struct match_token *p;
25770 +
25771 +       p = tbl;
25772 +       while (p->pattern) {
25773 +               if (p->token == val)
25774 +                       return p->pattern;
25775 +               p++;
25776 +       }
25777 +       BUG();
25778 +       return "??";
25779 +}
25780 +
25781 +static const char *au_optstr(int *val, match_table_t tbl)
25782 +{
25783 +       struct match_token *p;
25784 +       int v;
25785 +
25786 +       v = *val;
25787 +       if (!v)
25788 +               goto out;
25789 +       p = tbl;
25790 +       while (p->pattern) {
25791 +               if (p->token
25792 +                   && (v & p->token) == p->token) {
25793 +                       *val &= ~p->token;
25794 +                       return p->pattern;
25795 +               }
25796 +               p++;
25797 +       }
25798 +
25799 +out:
25800 +       return NULL;
25801 +}
25802 +
25803 +/* ---------------------------------------------------------------------- */
25804 +
25805 +static match_table_t brperm = {
25806 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25807 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25808 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25809 +       {0, NULL}
25810 +};
25811 +
25812 +static match_table_t brattr = {
25813 +       /* general */
25814 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25815 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25816 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25817 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25818 +#ifdef CONFIG_AUFS_FHSM
25819 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25820 +#endif
25821 +#ifdef CONFIG_AUFS_XATTR
25822 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25823 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25824 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25825 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25826 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25827 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25828 +#endif
25829 +
25830 +       /* ro/rr branch */
25831 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25832 +
25833 +       /* rw branch */
25834 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25835 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25836 +
25837 +       {0, NULL}
25838 +};
25839 +
25840 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25841 +{
25842 +       int attr, v;
25843 +       char *p;
25844 +
25845 +       attr = 0;
25846 +       do {
25847 +               p = strchr(str, '+');
25848 +               if (p)
25849 +                       *p = 0;
25850 +               v = match_token(str, table, args);
25851 +               if (v) {
25852 +                       if (v & AuBrAttr_CMOO_Mask)
25853 +                               attr &= ~AuBrAttr_CMOO_Mask;
25854 +                       attr |= v;
25855 +               } else {
25856 +                       if (p)
25857 +                               *p = '+';
25858 +                       pr_warn("ignored branch attribute %s\n", str);
25859 +                       break;
25860 +               }
25861 +               if (p)
25862 +                       str = p + 1;
25863 +       } while (p);
25864 +
25865 +       return attr;
25866 +}
25867 +
25868 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25869 +{
25870 +       int sz;
25871 +       const char *p;
25872 +       char *q;
25873 +
25874 +       q = str->a;
25875 +       *q = 0;
25876 +       p = au_optstr(&perm, brattr);
25877 +       if (p) {
25878 +               sz = strlen(p);
25879 +               memcpy(q, p, sz + 1);
25880 +               q += sz;
25881 +       } else
25882 +               goto out;
25883 +
25884 +       do {
25885 +               p = au_optstr(&perm, brattr);
25886 +               if (p) {
25887 +                       *q++ = '+';
25888 +                       sz = strlen(p);
25889 +                       memcpy(q, p, sz + 1);
25890 +                       q += sz;
25891 +               }
25892 +       } while (p);
25893 +
25894 +out:
25895 +       return q - str->a;
25896 +}
25897 +
25898 +static int noinline_for_stack br_perm_val(char *perm)
25899 +{
25900 +       int val, bad, sz;
25901 +       char *p;
25902 +       substring_t args[MAX_OPT_ARGS];
25903 +       au_br_perm_str_t attr;
25904 +
25905 +       p = strchr(perm, '+');
25906 +       if (p)
25907 +               *p = 0;
25908 +       val = match_token(perm, brperm, args);
25909 +       if (!val) {
25910 +               if (p)
25911 +                       *p = '+';
25912 +               pr_warn("ignored branch permission %s\n", perm);
25913 +               val = AuBrPerm_RO;
25914 +               goto out;
25915 +       }
25916 +       if (!p)
25917 +               goto out;
25918 +
25919 +       val |= br_attr_val(p + 1, brattr, args);
25920 +
25921 +       bad = 0;
25922 +       switch (val & AuBrPerm_Mask) {
25923 +       case AuBrPerm_RO:
25924 +       case AuBrPerm_RR:
25925 +               bad = val & AuBrWAttr_Mask;
25926 +               val &= ~AuBrWAttr_Mask;
25927 +               break;
25928 +       case AuBrPerm_RW:
25929 +               bad = val & AuBrRAttr_Mask;
25930 +               val &= ~AuBrRAttr_Mask;
25931 +               break;
25932 +       }
25933 +
25934 +       /*
25935 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25936 +        * does not treat it as an error, just warning.
25937 +        * this is a tiny guard for the user operation.
25938 +        */
25939 +       if (val & AuBrAttr_UNPIN) {
25940 +               bad |= AuBrAttr_UNPIN;
25941 +               val &= ~AuBrAttr_UNPIN;
25942 +       }
25943 +
25944 +       if (unlikely(bad)) {
25945 +               sz = au_do_optstr_br_attr(&attr, bad);
25946 +               AuDebugOn(!sz);
25947 +               pr_warn("ignored branch attribute %s\n", attr.a);
25948 +       }
25949 +
25950 +out:
25951 +       return val;
25952 +}
25953 +
25954 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25955 +{
25956 +       au_br_perm_str_t attr;
25957 +       const char *p;
25958 +       char *q;
25959 +       int sz;
25960 +
25961 +       q = str->a;
25962 +       p = au_optstr(&perm, brperm);
25963 +       AuDebugOn(!p || !*p);
25964 +       sz = strlen(p);
25965 +       memcpy(q, p, sz + 1);
25966 +       q += sz;
25967 +
25968 +       sz = au_do_optstr_br_attr(&attr, perm);
25969 +       if (sz) {
25970 +               *q++ = '+';
25971 +               memcpy(q, attr.a, sz + 1);
25972 +       }
25973 +
25974 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
25975 +}
25976 +
25977 +/* ---------------------------------------------------------------------- */
25978 +
25979 +static match_table_t udbalevel = {
25980 +       {AuOpt_UDBA_REVAL, "reval"},
25981 +       {AuOpt_UDBA_NONE, "none"},
25982 +#ifdef CONFIG_AUFS_HNOTIFY
25983 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25984 +#ifdef CONFIG_AUFS_HFSNOTIFY
25985 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
25986 +#endif
25987 +#endif
25988 +       {-1, NULL}
25989 +};
25990 +
25991 +static int noinline_for_stack udba_val(char *str)
25992 +{
25993 +       substring_t args[MAX_OPT_ARGS];
25994 +
25995 +       return match_token(str, udbalevel, args);
25996 +}
25997 +
25998 +const char *au_optstr_udba(int udba)
25999 +{
26000 +       return au_parser_pattern(udba, udbalevel);
26001 +}
26002 +
26003 +/* ---------------------------------------------------------------------- */
26004 +
26005 +static match_table_t au_wbr_create_policy = {
26006 +       {AuWbrCreate_TDP, "tdp"},
26007 +       {AuWbrCreate_TDP, "top-down-parent"},
26008 +       {AuWbrCreate_RR, "rr"},
26009 +       {AuWbrCreate_RR, "round-robin"},
26010 +       {AuWbrCreate_MFS, "mfs"},
26011 +       {AuWbrCreate_MFS, "most-free-space"},
26012 +       {AuWbrCreate_MFSV, "mfs:%d"},
26013 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26014 +
26015 +       /* top-down regardless the parent, and then mfs */
26016 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26017 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26018 +
26019 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26020 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26021 +       {AuWbrCreate_PMFS, "pmfs"},
26022 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26023 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26024 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26025 +
26026 +       {-1, NULL}
26027 +};
26028 +
26029 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26030 +                           struct au_opt_wbr_create *create)
26031 +{
26032 +       int err;
26033 +       unsigned long long ull;
26034 +
26035 +       err = 0;
26036 +       if (!match_u64(arg, &ull))
26037 +               create->mfsrr_watermark = ull;
26038 +       else {
26039 +               pr_err("bad integer in %s\n", str);
26040 +               err = -EINVAL;
26041 +       }
26042 +
26043 +       return err;
26044 +}
26045 +
26046 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26047 +                         struct au_opt_wbr_create *create)
26048 +{
26049 +       int n, err;
26050 +
26051 +       err = 0;
26052 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26053 +               create->mfs_second = n;
26054 +       else {
26055 +               pr_err("bad integer in %s\n", str);
26056 +               err = -EINVAL;
26057 +       }
26058 +
26059 +       return err;
26060 +}
26061 +
26062 +static int noinline_for_stack
26063 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26064 +{
26065 +       int err, e;
26066 +       substring_t args[MAX_OPT_ARGS];
26067 +
26068 +       err = match_token(str, au_wbr_create_policy, args);
26069 +       create->wbr_create = err;
26070 +       switch (err) {
26071 +       case AuWbrCreate_MFSRRV:
26072 +       case AuWbrCreate_TDMFSV:
26073 +       case AuWbrCreate_PMFSRRV:
26074 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26075 +               if (!e)
26076 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26077 +               if (unlikely(e))
26078 +                       err = e;
26079 +               break;
26080 +       case AuWbrCreate_MFSRR:
26081 +       case AuWbrCreate_TDMFS:
26082 +       case AuWbrCreate_PMFSRR:
26083 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26084 +               if (unlikely(e)) {
26085 +                       err = e;
26086 +                       break;
26087 +               }
26088 +               fallthrough;
26089 +       case AuWbrCreate_MFS:
26090 +       case AuWbrCreate_PMFS:
26091 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26092 +               break;
26093 +       case AuWbrCreate_MFSV:
26094 +       case AuWbrCreate_PMFSV:
26095 +               e = au_wbr_mfs_sec(&args[0], str, create);
26096 +               if (unlikely(e))
26097 +                       err = e;
26098 +               break;
26099 +       }
26100 +
26101 +       return err;
26102 +}
26103 +
26104 +const char *au_optstr_wbr_create(int wbr_create)
26105 +{
26106 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26107 +}
26108 +
26109 +static match_table_t au_wbr_copyup_policy = {
26110 +       {AuWbrCopyup_TDP, "tdp"},
26111 +       {AuWbrCopyup_TDP, "top-down-parent"},
26112 +       {AuWbrCopyup_BUP, "bup"},
26113 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26114 +       {AuWbrCopyup_BU, "bu"},
26115 +       {AuWbrCopyup_BU, "bottom-up"},
26116 +       {-1, NULL}
26117 +};
26118 +
26119 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26120 +{
26121 +       substring_t args[MAX_OPT_ARGS];
26122 +
26123 +       return match_token(str, au_wbr_copyup_policy, args);
26124 +}
26125 +
26126 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26127 +{
26128 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26129 +}
26130 +
26131 +/* ---------------------------------------------------------------------- */
26132 +
26133 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26134 +
26135 +static void dump_opts(struct au_opts *opts)
26136 +{
26137 +#ifdef CONFIG_AUFS_DEBUG
26138 +       /* reduce stack space */
26139 +       union {
26140 +               struct au_opt_add *add;
26141 +               struct au_opt_del *del;
26142 +               struct au_opt_mod *mod;
26143 +               struct au_opt_xino *xino;
26144 +               struct au_opt_xino_itrunc *xino_itrunc;
26145 +               struct au_opt_wbr_create *create;
26146 +       } u;
26147 +       struct au_opt *opt;
26148 +
26149 +       opt = opts->opt;
26150 +       while (opt->type != Opt_tail) {
26151 +               switch (opt->type) {
26152 +               case Opt_add:
26153 +                       u.add = &opt->add;
26154 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26155 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26156 +                                 u.add->path.dentry);
26157 +                       break;
26158 +               case Opt_del:
26159 +               case Opt_idel:
26160 +                       u.del = &opt->del;
26161 +                       AuDbg("del {%s, %p}\n",
26162 +                             u.del->pathname, u.del->h_path.dentry);
26163 +                       break;
26164 +               case Opt_mod:
26165 +               case Opt_imod:
26166 +                       u.mod = &opt->mod;
26167 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26168 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26169 +                       break;
26170 +               case Opt_append:
26171 +                       u.add = &opt->add;
26172 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26173 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26174 +                                 u.add->path.dentry);
26175 +                       break;
26176 +               case Opt_prepend:
26177 +                       u.add = &opt->add;
26178 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26179 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26180 +                                 u.add->path.dentry);
26181 +                       break;
26182 +               case Opt_dirwh:
26183 +                       AuDbg("dirwh %d\n", opt->dirwh);
26184 +                       break;
26185 +               case Opt_rdcache:
26186 +                       AuDbg("rdcache %d\n", opt->rdcache);
26187 +                       break;
26188 +               case Opt_rdblk:
26189 +                       AuDbg("rdblk %u\n", opt->rdblk);
26190 +                       break;
26191 +               case Opt_rdblk_def:
26192 +                       AuDbg("rdblk_def\n");
26193 +                       break;
26194 +               case Opt_rdhash:
26195 +                       AuDbg("rdhash %u\n", opt->rdhash);
26196 +                       break;
26197 +               case Opt_rdhash_def:
26198 +                       AuDbg("rdhash_def\n");
26199 +                       break;
26200 +               case Opt_xino:
26201 +                       u.xino = &opt->xino;
26202 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26203 +                       break;
26204 +               case Opt_trunc_xino:
26205 +                       AuLabel(trunc_xino);
26206 +                       break;
26207 +               case Opt_notrunc_xino:
26208 +                       AuLabel(notrunc_xino);
26209 +                       break;
26210 +               case Opt_trunc_xino_path:
26211 +               case Opt_itrunc_xino:
26212 +                       u.xino_itrunc = &opt->xino_itrunc;
26213 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26214 +                       break;
26215 +               case Opt_noxino:
26216 +                       AuLabel(noxino);
26217 +                       break;
26218 +               case Opt_trunc_xib:
26219 +                       AuLabel(trunc_xib);
26220 +                       break;
26221 +               case Opt_notrunc_xib:
26222 +                       AuLabel(notrunc_xib);
26223 +                       break;
26224 +               case Opt_shwh:
26225 +                       AuLabel(shwh);
26226 +                       break;
26227 +               case Opt_noshwh:
26228 +                       AuLabel(noshwh);
26229 +                       break;
26230 +               case Opt_dirperm1:
26231 +                       AuLabel(dirperm1);
26232 +                       break;
26233 +               case Opt_nodirperm1:
26234 +                       AuLabel(nodirperm1);
26235 +                       break;
26236 +               case Opt_plink:
26237 +                       AuLabel(plink);
26238 +                       break;
26239 +               case Opt_noplink:
26240 +                       AuLabel(noplink);
26241 +                       break;
26242 +               case Opt_list_plink:
26243 +                       AuLabel(list_plink);
26244 +                       break;
26245 +               case Opt_udba:
26246 +                       AuDbg("udba %d, %s\n",
26247 +                                 opt->udba, au_optstr_udba(opt->udba));
26248 +                       break;
26249 +               case Opt_dio:
26250 +                       AuLabel(dio);
26251 +                       break;
26252 +               case Opt_nodio:
26253 +                       AuLabel(nodio);
26254 +                       break;
26255 +               case Opt_diropq_a:
26256 +                       AuLabel(diropq_a);
26257 +                       break;
26258 +               case Opt_diropq_w:
26259 +                       AuLabel(diropq_w);
26260 +                       break;
26261 +               case Opt_warn_perm:
26262 +                       AuLabel(warn_perm);
26263 +                       break;
26264 +               case Opt_nowarn_perm:
26265 +                       AuLabel(nowarn_perm);
26266 +                       break;
26267 +               case Opt_verbose:
26268 +                       AuLabel(verbose);
26269 +                       break;
26270 +               case Opt_noverbose:
26271 +                       AuLabel(noverbose);
26272 +                       break;
26273 +               case Opt_sum:
26274 +                       AuLabel(sum);
26275 +                       break;
26276 +               case Opt_nosum:
26277 +                       AuLabel(nosum);
26278 +                       break;
26279 +               case Opt_wsum:
26280 +                       AuLabel(wsum);
26281 +                       break;
26282 +               case Opt_wbr_create:
26283 +                       u.create = &opt->wbr_create;
26284 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26285 +                                 au_optstr_wbr_create(u.create->wbr_create));
26286 +                       switch (u.create->wbr_create) {
26287 +                       case AuWbrCreate_MFSV:
26288 +                       case AuWbrCreate_PMFSV:
26289 +                               AuDbg("%d sec\n", u.create->mfs_second);
26290 +                               break;
26291 +                       case AuWbrCreate_MFSRR:
26292 +                       case AuWbrCreate_TDMFS:
26293 +                               AuDbg("%llu watermark\n",
26294 +                                         u.create->mfsrr_watermark);
26295 +                               break;
26296 +                       case AuWbrCreate_MFSRRV:
26297 +                       case AuWbrCreate_TDMFSV:
26298 +                       case AuWbrCreate_PMFSRRV:
26299 +                               AuDbg("%llu watermark, %d sec\n",
26300 +                                         u.create->mfsrr_watermark,
26301 +                                         u.create->mfs_second);
26302 +                               break;
26303 +                       }
26304 +                       break;
26305 +               case Opt_wbr_copyup:
26306 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26307 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26308 +                       break;
26309 +               case Opt_fhsm_sec:
26310 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26311 +                       break;
26312 +               case Opt_dirren:
26313 +                       AuLabel(dirren);
26314 +                       break;
26315 +               case Opt_nodirren:
26316 +                       AuLabel(nodirren);
26317 +                       break;
26318 +               case Opt_acl:
26319 +                       AuLabel(acl);
26320 +                       break;
26321 +               case Opt_noacl:
26322 +                       AuLabel(noacl);
26323 +                       break;
26324 +               default:
26325 +                       BUG();
26326 +               }
26327 +               opt++;
26328 +       }
26329 +#endif
26330 +}
26331 +
26332 +void au_opts_free(struct au_opts *opts)
26333 +{
26334 +       struct au_opt *opt;
26335 +
26336 +       opt = opts->opt;
26337 +       while (opt->type != Opt_tail) {
26338 +               switch (opt->type) {
26339 +               case Opt_add:
26340 +               case Opt_append:
26341 +               case Opt_prepend:
26342 +                       path_put(&opt->add.path);
26343 +                       break;
26344 +               case Opt_del:
26345 +               case Opt_idel:
26346 +                       path_put(&opt->del.h_path);
26347 +                       break;
26348 +               case Opt_mod:
26349 +               case Opt_imod:
26350 +                       dput(opt->mod.h_root);
26351 +                       break;
26352 +               case Opt_xino:
26353 +                       fput(opt->xino.file);
26354 +                       break;
26355 +               }
26356 +               opt++;
26357 +       }
26358 +}
26359 +
26360 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26361 +                  aufs_bindex_t bindex)
26362 +{
26363 +       int err;
26364 +       struct au_opt_add *add = &opt->add;
26365 +       char *p;
26366 +
26367 +       add->bindex = bindex;
26368 +       add->perm = AuBrPerm_RO;
26369 +       add->pathname = opt_str;
26370 +       p = strchr(opt_str, '=');
26371 +       if (p) {
26372 +               *p++ = 0;
26373 +               if (*p)
26374 +                       add->perm = br_perm_val(p);
26375 +       }
26376 +
26377 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26378 +       if (!err) {
26379 +               if (!p) {
26380 +                       add->perm = AuBrPerm_RO;
26381 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26382 +                               add->perm = AuBrPerm_RR;
26383 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26384 +                               add->perm = AuBrPerm_RW;
26385 +               }
26386 +               opt->type = Opt_add;
26387 +               goto out;
26388 +       }
26389 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26390 +       err = -EINVAL;
26391 +
26392 +out:
26393 +       return err;
26394 +}
26395 +
26396 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26397 +{
26398 +       int err;
26399 +
26400 +       del->pathname = args[0].from;
26401 +       AuDbg("del path %s\n", del->pathname);
26402 +
26403 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26404 +       if (unlikely(err))
26405 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26406 +
26407 +       return err;
26408 +}
26409 +
26410 +#if 0 /* reserved for future use */
26411 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26412 +                             struct au_opt_del *del, substring_t args[])
26413 +{
26414 +       int err;
26415 +       struct dentry *root;
26416 +
26417 +       err = -EINVAL;
26418 +       root = sb->s_root;
26419 +       aufs_read_lock(root, AuLock_FLUSH);
26420 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26421 +               pr_err("out of bounds, %d\n", bindex);
26422 +               goto out;
26423 +       }
26424 +
26425 +       err = 0;
26426 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26427 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26428 +
26429 +out:
26430 +       aufs_read_unlock(root, !AuLock_IR);
26431 +       return err;
26432 +}
26433 +#endif
26434 +
26435 +static int noinline_for_stack
26436 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26437 +{
26438 +       int err;
26439 +       struct path path;
26440 +       char *p;
26441 +
26442 +       err = -EINVAL;
26443 +       mod->path = args[0].from;
26444 +       p = strchr(mod->path, '=');
26445 +       if (unlikely(!p)) {
26446 +               pr_err("no permission %s\n", args[0].from);
26447 +               goto out;
26448 +       }
26449 +
26450 +       *p++ = 0;
26451 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26452 +       if (unlikely(err)) {
26453 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26454 +               goto out;
26455 +       }
26456 +
26457 +       mod->perm = br_perm_val(p);
26458 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26459 +       mod->h_root = dget(path.dentry);
26460 +       path_put(&path);
26461 +
26462 +out:
26463 +       return err;
26464 +}
26465 +
26466 +#if 0 /* reserved for future use */
26467 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26468 +                             struct au_opt_mod *mod, substring_t args[])
26469 +{
26470 +       int err;
26471 +       struct dentry *root;
26472 +
26473 +       err = -EINVAL;
26474 +       root = sb->s_root;
26475 +       aufs_read_lock(root, AuLock_FLUSH);
26476 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26477 +               pr_err("out of bounds, %d\n", bindex);
26478 +               goto out;
26479 +       }
26480 +
26481 +       err = 0;
26482 +       mod->perm = br_perm_val(args[1].from);
26483 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26484 +             mod->path, mod->perm, args[1].from);
26485 +       mod->h_root = dget(au_h_dptr(root, bindex));
26486 +
26487 +out:
26488 +       aufs_read_unlock(root, !AuLock_IR);
26489 +       return err;
26490 +}
26491 +#endif
26492 +
26493 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26494 +                             substring_t args[])
26495 +{
26496 +       int err;
26497 +       struct file *file;
26498 +
26499 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26500 +       err = PTR_ERR(file);
26501 +       if (IS_ERR(file))
26502 +               goto out;
26503 +
26504 +       err = -EINVAL;
26505 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26506 +               fput(file);
26507 +               pr_err("%s must be outside\n", args[0].from);
26508 +               goto out;
26509 +       }
26510 +
26511 +       err = 0;
26512 +       xino->file = file;
26513 +       xino->path = args[0].from;
26514 +
26515 +out:
26516 +       return err;
26517 +}
26518 +
26519 +static int noinline_for_stack
26520 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26521 +                              struct au_opt_xino_itrunc *xino_itrunc,
26522 +                              substring_t args[])
26523 +{
26524 +       int err;
26525 +       aufs_bindex_t bbot, bindex;
26526 +       struct path path;
26527 +       struct dentry *root;
26528 +
26529 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26530 +       if (unlikely(err)) {
26531 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26532 +               goto out;
26533 +       }
26534 +
26535 +       xino_itrunc->bindex = -1;
26536 +       root = sb->s_root;
26537 +       aufs_read_lock(root, AuLock_FLUSH);
26538 +       bbot = au_sbbot(sb);
26539 +       for (bindex = 0; bindex <= bbot; bindex++) {
26540 +               if (au_h_dptr(root, bindex) == path.dentry) {
26541 +                       xino_itrunc->bindex = bindex;
26542 +                       break;
26543 +               }
26544 +       }
26545 +       aufs_read_unlock(root, !AuLock_IR);
26546 +       path_put(&path);
26547 +
26548 +       if (unlikely(xino_itrunc->bindex < 0)) {
26549 +               pr_err("no such branch %s\n", args[0].from);
26550 +               err = -EINVAL;
26551 +       }
26552 +
26553 +out:
26554 +       return err;
26555 +}
26556 +
26557 +/* called without aufs lock */
26558 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26559 +{
26560 +       int err, n, token;
26561 +       aufs_bindex_t bindex;
26562 +       unsigned char skipped;
26563 +       struct dentry *root;
26564 +       struct au_opt *opt, *opt_tail;
26565 +       char *opt_str;
26566 +       /* reduce the stack space */
26567 +       union {
26568 +               struct au_opt_xino_itrunc *xino_itrunc;
26569 +               struct au_opt_wbr_create *create;
26570 +       } u;
26571 +       struct {
26572 +               substring_t args[MAX_OPT_ARGS];
26573 +       } *a;
26574 +
26575 +       err = -ENOMEM;
26576 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26577 +       if (unlikely(!a))
26578 +               goto out;
26579 +
26580 +       root = sb->s_root;
26581 +       err = 0;
26582 +       bindex = 0;
26583 +       opt = opts->opt;
26584 +       opt_tail = opt + opts->max_opt - 1;
26585 +       opt->type = Opt_tail;
26586 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26587 +               err = -EINVAL;
26588 +               skipped = 0;
26589 +               token = match_token(opt_str, options, a->args);
26590 +               switch (token) {
26591 +               case Opt_br:
26592 +                       err = 0;
26593 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26594 +                              && *opt_str) {
26595 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26596 +                                             bindex++);
26597 +                               if (unlikely(!err && ++opt > opt_tail)) {
26598 +                                       err = -E2BIG;
26599 +                                       break;
26600 +                               }
26601 +                               opt->type = Opt_tail;
26602 +                               skipped = 1;
26603 +                       }
26604 +                       break;
26605 +               case Opt_add:
26606 +                       if (unlikely(match_int(&a->args[0], &n))) {
26607 +                               pr_err("bad integer in %s\n", opt_str);
26608 +                               break;
26609 +                       }
26610 +                       bindex = n;
26611 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26612 +                                     bindex);
26613 +                       if (!err)
26614 +                               opt->type = token;
26615 +                       break;
26616 +               case Opt_append:
26617 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26618 +                                     /*dummy bindex*/1);
26619 +                       if (!err)
26620 +                               opt->type = token;
26621 +                       break;
26622 +               case Opt_prepend:
26623 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26624 +                                     /*bindex*/0);
26625 +                       if (!err)
26626 +                               opt->type = token;
26627 +                       break;
26628 +               case Opt_del:
26629 +                       err = au_opts_parse_del(&opt->del, a->args);
26630 +                       if (!err)
26631 +                               opt->type = token;
26632 +                       break;
26633 +#if 0 /* reserved for future use */
26634 +               case Opt_idel:
26635 +                       del->pathname = "(indexed)";
26636 +                       if (unlikely(match_int(&args[0], &n))) {
26637 +                               pr_err("bad integer in %s\n", opt_str);
26638 +                               break;
26639 +                       }
26640 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26641 +                       if (!err)
26642 +                               opt->type = token;
26643 +                       break;
26644 +#endif
26645 +               case Opt_mod:
26646 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26647 +                       if (!err)
26648 +                               opt->type = token;
26649 +                       break;
26650 +#ifdef IMOD /* reserved for future use */
26651 +               case Opt_imod:
26652 +                       u.mod->path = "(indexed)";
26653 +                       if (unlikely(match_int(&a->args[0], &n))) {
26654 +                               pr_err("bad integer in %s\n", opt_str);
26655 +                               break;
26656 +                       }
26657 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26658 +                       if (!err)
26659 +                               opt->type = token;
26660 +                       break;
26661 +#endif
26662 +               case Opt_xino:
26663 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26664 +                       if (!err)
26665 +                               opt->type = token;
26666 +                       break;
26667 +
26668 +               case Opt_trunc_xino_path:
26669 +                       err = au_opts_parse_xino_itrunc_path
26670 +                               (sb, &opt->xino_itrunc, a->args);
26671 +                       if (!err)
26672 +                               opt->type = token;
26673 +                       break;
26674 +
26675 +               case Opt_itrunc_xino:
26676 +                       u.xino_itrunc = &opt->xino_itrunc;
26677 +                       if (unlikely(match_int(&a->args[0], &n))) {
26678 +                               pr_err("bad integer in %s\n", opt_str);
26679 +                               break;
26680 +                       }
26681 +                       u.xino_itrunc->bindex = n;
26682 +                       aufs_read_lock(root, AuLock_FLUSH);
26683 +                       if (n < 0 || au_sbbot(sb) < n) {
26684 +                               pr_err("out of bounds, %d\n", n);
26685 +                               aufs_read_unlock(root, !AuLock_IR);
26686 +                               break;
26687 +                       }
26688 +                       aufs_read_unlock(root, !AuLock_IR);
26689 +                       err = 0;
26690 +                       opt->type = token;
26691 +                       break;
26692 +
26693 +               case Opt_dirwh:
26694 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26695 +                               break;
26696 +                       err = 0;
26697 +                       opt->type = token;
26698 +                       break;
26699 +
26700 +               case Opt_rdcache:
26701 +                       if (unlikely(match_int(&a->args[0], &n))) {
26702 +                               pr_err("bad integer in %s\n", opt_str);
26703 +                               break;
26704 +                       }
26705 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26706 +                               pr_err("rdcache must be smaller than %d\n",
26707 +                                      AUFS_RDCACHE_MAX);
26708 +                               break;
26709 +                       }
26710 +                       opt->rdcache = n;
26711 +                       err = 0;
26712 +                       opt->type = token;
26713 +                       break;
26714 +               case Opt_rdblk:
26715 +                       if (unlikely(match_int(&a->args[0], &n)
26716 +                                    || n < 0
26717 +                                    || n > KMALLOC_MAX_SIZE)) {
26718 +                               pr_err("bad integer in %s\n", opt_str);
26719 +                               break;
26720 +                       }
26721 +                       if (unlikely(n && n < NAME_MAX)) {
26722 +                               pr_err("rdblk must be larger than %d\n",
26723 +                                      NAME_MAX);
26724 +                               break;
26725 +                       }
26726 +                       opt->rdblk = n;
26727 +                       err = 0;
26728 +                       opt->type = token;
26729 +                       break;
26730 +               case Opt_rdhash:
26731 +                       if (unlikely(match_int(&a->args[0], &n)
26732 +                                    || n < 0
26733 +                                    || n * sizeof(struct hlist_head)
26734 +                                    > KMALLOC_MAX_SIZE)) {
26735 +                               pr_err("bad integer in %s\n", opt_str);
26736 +                               break;
26737 +                       }
26738 +                       opt->rdhash = n;
26739 +                       err = 0;
26740 +                       opt->type = token;
26741 +                       break;
26742 +
26743 +               case Opt_trunc_xino:
26744 +               case Opt_notrunc_xino:
26745 +               case Opt_noxino:
26746 +               case Opt_trunc_xib:
26747 +               case Opt_notrunc_xib:
26748 +               case Opt_shwh:
26749 +               case Opt_noshwh:
26750 +               case Opt_dirperm1:
26751 +               case Opt_nodirperm1:
26752 +               case Opt_plink:
26753 +               case Opt_noplink:
26754 +               case Opt_list_plink:
26755 +               case Opt_dio:
26756 +               case Opt_nodio:
26757 +               case Opt_diropq_a:
26758 +               case Opt_diropq_w:
26759 +               case Opt_warn_perm:
26760 +               case Opt_nowarn_perm:
26761 +               case Opt_verbose:
26762 +               case Opt_noverbose:
26763 +               case Opt_sum:
26764 +               case Opt_nosum:
26765 +               case Opt_wsum:
26766 +               case Opt_rdblk_def:
26767 +               case Opt_rdhash_def:
26768 +               case Opt_dirren:
26769 +               case Opt_nodirren:
26770 +               case Opt_acl:
26771 +               case Opt_noacl:
26772 +                       err = 0;
26773 +                       opt->type = token;
26774 +                       break;
26775 +
26776 +               case Opt_udba:
26777 +                       opt->udba = udba_val(a->args[0].from);
26778 +                       if (opt->udba >= 0) {
26779 +                               err = 0;
26780 +                               opt->type = token;
26781 +                       } else
26782 +                               pr_err("wrong value, %s\n", opt_str);
26783 +                       break;
26784 +
26785 +               case Opt_wbr_create:
26786 +                       u.create = &opt->wbr_create;
26787 +                       u.create->wbr_create
26788 +                               = au_wbr_create_val(a->args[0].from, u.create);
26789 +                       if (u.create->wbr_create >= 0) {
26790 +                               err = 0;
26791 +                               opt->type = token;
26792 +                       } else
26793 +                               pr_err("wrong value, %s\n", opt_str);
26794 +                       break;
26795 +               case Opt_wbr_copyup:
26796 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26797 +                       if (opt->wbr_copyup >= 0) {
26798 +                               err = 0;
26799 +                               opt->type = token;
26800 +                       } else
26801 +                               pr_err("wrong value, %s\n", opt_str);
26802 +                       break;
26803 +
26804 +               case Opt_fhsm_sec:
26805 +                       if (unlikely(match_int(&a->args[0], &n)
26806 +                                    || n < 0)) {
26807 +                               pr_err("bad integer in %s\n", opt_str);
26808 +                               break;
26809 +                       }
26810 +                       if (sysaufs_brs) {
26811 +                               opt->fhsm_second = n;
26812 +                               opt->type = token;
26813 +                       } else
26814 +                               pr_warn("ignored %s\n", opt_str);
26815 +                       err = 0;
26816 +                       break;
26817 +
26818 +               case Opt_ignore:
26819 +                       pr_warn("ignored %s\n", opt_str);
26820 +                       fallthrough;
26821 +               case Opt_ignore_silent:
26822 +                       skipped = 1;
26823 +                       err = 0;
26824 +                       break;
26825 +               case Opt_err:
26826 +                       pr_err("unknown option %s\n", opt_str);
26827 +                       break;
26828 +               }
26829 +
26830 +               if (!err && !skipped) {
26831 +                       if (unlikely(++opt > opt_tail)) {
26832 +                               err = -E2BIG;
26833 +                               opt--;
26834 +                               opt->type = Opt_tail;
26835 +                               break;
26836 +                       }
26837 +                       opt->type = Opt_tail;
26838 +               }
26839 +       }
26840 +
26841 +       au_kfree_rcu(a);
26842 +       dump_opts(opts);
26843 +       if (unlikely(err))
26844 +               au_opts_free(opts);
26845 +
26846 +out:
26847 +       return err;
26848 +}
26849 +
26850 +static int au_opt_wbr_create(struct super_block *sb,
26851 +                            struct au_opt_wbr_create *create)
26852 +{
26853 +       int err;
26854 +       struct au_sbinfo *sbinfo;
26855 +
26856 +       SiMustWriteLock(sb);
26857 +
26858 +       err = 1; /* handled */
26859 +       sbinfo = au_sbi(sb);
26860 +       if (sbinfo->si_wbr_create_ops->fin) {
26861 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26862 +               if (!err)
26863 +                       err = 1;
26864 +       }
26865 +
26866 +       sbinfo->si_wbr_create = create->wbr_create;
26867 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26868 +       switch (create->wbr_create) {
26869 +       case AuWbrCreate_MFSRRV:
26870 +       case AuWbrCreate_MFSRR:
26871 +       case AuWbrCreate_TDMFS:
26872 +       case AuWbrCreate_TDMFSV:
26873 +       case AuWbrCreate_PMFSRR:
26874 +       case AuWbrCreate_PMFSRRV:
26875 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26876 +               fallthrough;
26877 +       case AuWbrCreate_MFS:
26878 +       case AuWbrCreate_MFSV:
26879 +       case AuWbrCreate_PMFS:
26880 +       case AuWbrCreate_PMFSV:
26881 +               sbinfo->si_wbr_mfs.mfs_expire
26882 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26883 +               break;
26884 +       }
26885 +
26886 +       if (sbinfo->si_wbr_create_ops->init)
26887 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26888 +
26889 +       return err;
26890 +}
26891 +
26892 +/*
26893 + * returns,
26894 + * plus: processed without an error
26895 + * zero: unprocessed
26896 + */
26897 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26898 +                        struct au_opts *opts)
26899 +{
26900 +       int err;
26901 +       struct au_sbinfo *sbinfo;
26902 +
26903 +       SiMustWriteLock(sb);
26904 +
26905 +       err = 1; /* handled */
26906 +       sbinfo = au_sbi(sb);
26907 +       switch (opt->type) {
26908 +       case Opt_udba:
26909 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26910 +               sbinfo->si_mntflags |= opt->udba;
26911 +               opts->given_udba |= opt->udba;
26912 +               break;
26913 +
26914 +       case Opt_plink:
26915 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26916 +               break;
26917 +       case Opt_noplink:
26918 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26919 +                       au_plink_put(sb, /*verbose*/1);
26920 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26921 +               break;
26922 +       case Opt_list_plink:
26923 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26924 +                       au_plink_list(sb);
26925 +               break;
26926 +
26927 +       case Opt_dio:
26928 +               au_opt_set(sbinfo->si_mntflags, DIO);
26929 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26930 +               break;
26931 +       case Opt_nodio:
26932 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26933 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26934 +               break;
26935 +
26936 +       case Opt_fhsm_sec:
26937 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26938 +               break;
26939 +
26940 +       case Opt_diropq_a:
26941 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26942 +               break;
26943 +       case Opt_diropq_w:
26944 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26945 +               break;
26946 +
26947 +       case Opt_warn_perm:
26948 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26949 +               break;
26950 +       case Opt_nowarn_perm:
26951 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26952 +               break;
26953 +
26954 +       case Opt_verbose:
26955 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
26956 +               break;
26957 +       case Opt_noverbose:
26958 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26959 +               break;
26960 +
26961 +       case Opt_sum:
26962 +               au_opt_set(sbinfo->si_mntflags, SUM);
26963 +               break;
26964 +       case Opt_wsum:
26965 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26966 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
26967 +               break;
26968 +       case Opt_nosum:
26969 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26970 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
26971 +               break;
26972 +
26973 +       case Opt_wbr_create:
26974 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
26975 +               break;
26976 +       case Opt_wbr_copyup:
26977 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
26978 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26979 +               break;
26980 +
26981 +       case Opt_dirwh:
26982 +               sbinfo->si_dirwh = opt->dirwh;
26983 +               break;
26984 +
26985 +       case Opt_rdcache:
26986 +               sbinfo->si_rdcache
26987 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26988 +               break;
26989 +       case Opt_rdblk:
26990 +               sbinfo->si_rdblk = opt->rdblk;
26991 +               break;
26992 +       case Opt_rdblk_def:
26993 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26994 +               break;
26995 +       case Opt_rdhash:
26996 +               sbinfo->si_rdhash = opt->rdhash;
26997 +               break;
26998 +       case Opt_rdhash_def:
26999 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27000 +               break;
27001 +
27002 +       case Opt_shwh:
27003 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27004 +               break;
27005 +       case Opt_noshwh:
27006 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27007 +               break;
27008 +
27009 +       case Opt_dirperm1:
27010 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27011 +               break;
27012 +       case Opt_nodirperm1:
27013 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27014 +               break;
27015 +
27016 +       case Opt_trunc_xino:
27017 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27018 +               break;
27019 +       case Opt_notrunc_xino:
27020 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27021 +               break;
27022 +
27023 +       case Opt_trunc_xino_path:
27024 +       case Opt_itrunc_xino:
27025 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27026 +                                   /*idx_begin*/0);
27027 +               if (!err)
27028 +                       err = 1;
27029 +               break;
27030 +
27031 +       case Opt_trunc_xib:
27032 +               au_fset_opts(opts->flags, TRUNC_XIB);
27033 +               break;
27034 +       case Opt_notrunc_xib:
27035 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27036 +               break;
27037 +
27038 +       case Opt_dirren:
27039 +               err = 1;
27040 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27041 +                       err = au_dr_opt_set(sb);
27042 +                       if (!err)
27043 +                               err = 1;
27044 +               }
27045 +               if (err == 1)
27046 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27047 +               break;
27048 +       case Opt_nodirren:
27049 +               err = 1;
27050 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27051 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27052 +                                                             DR_FLUSHED));
27053 +                       if (!err)
27054 +                               err = 1;
27055 +               }
27056 +               if (err == 1)
27057 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27058 +               break;
27059 +
27060 +       case Opt_acl:
27061 +               sb->s_flags |= SB_POSIXACL;
27062 +               break;
27063 +       case Opt_noacl:
27064 +               sb->s_flags &= ~SB_POSIXACL;
27065 +               break;
27066 +
27067 +       default:
27068 +               err = 0;
27069 +               break;
27070 +       }
27071 +
27072 +       return err;
27073 +}
27074 +
27075 +/*
27076 + * returns tri-state.
27077 + * plus: processed without an error
27078 + * zero: unprocessed
27079 + * minus: error
27080 + */
27081 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27082 +                    struct au_opts *opts)
27083 +{
27084 +       int err, do_refresh;
27085 +
27086 +       err = 0;
27087 +       switch (opt->type) {
27088 +       case Opt_append:
27089 +               opt->add.bindex = au_sbbot(sb) + 1;
27090 +               if (opt->add.bindex < 0)
27091 +                       opt->add.bindex = 0;
27092 +               goto add;
27093 +               /* Always goto add, not fallthrough */
27094 +       case Opt_prepend:
27095 +               opt->add.bindex = 0;
27096 +               fallthrough;
27097 +       add: /* indented label */
27098 +       case Opt_add:
27099 +               err = au_br_add(sb, &opt->add,
27100 +                               au_ftest_opts(opts->flags, REMOUNT));
27101 +               if (!err) {
27102 +                       err = 1;
27103 +                       au_fset_opts(opts->flags, REFRESH);
27104 +               }
27105 +               break;
27106 +
27107 +       case Opt_del:
27108 +       case Opt_idel:
27109 +               err = au_br_del(sb, &opt->del,
27110 +                               au_ftest_opts(opts->flags, REMOUNT));
27111 +               if (!err) {
27112 +                       err = 1;
27113 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27114 +                       au_fset_opts(opts->flags, REFRESH);
27115 +               }
27116 +               break;
27117 +
27118 +       case Opt_mod:
27119 +       case Opt_imod:
27120 +               err = au_br_mod(sb, &opt->mod,
27121 +                               au_ftest_opts(opts->flags, REMOUNT),
27122 +                               &do_refresh);
27123 +               if (!err) {
27124 +                       err = 1;
27125 +                       if (do_refresh)
27126 +                               au_fset_opts(opts->flags, REFRESH);
27127 +               }
27128 +               break;
27129 +       }
27130 +       return err;
27131 +}
27132 +
27133 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27134 +                      struct au_opt_xino **opt_xino,
27135 +                      struct au_opts *opts)
27136 +{
27137 +       int err;
27138 +
27139 +       err = 0;
27140 +       switch (opt->type) {
27141 +       case Opt_xino:
27142 +               err = au_xino_set(sb, &opt->xino,
27143 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27144 +               if (unlikely(err))
27145 +                       break;
27146 +
27147 +               *opt_xino = &opt->xino;
27148 +               break;
27149 +
27150 +       case Opt_noxino:
27151 +               au_xino_clr(sb);
27152 +               *opt_xino = (void *)-1;
27153 +               break;
27154 +       }
27155 +
27156 +       return err;
27157 +}
27158 +
27159 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27160 +                  unsigned int pending)
27161 +{
27162 +       int err, fhsm;
27163 +       aufs_bindex_t bindex, bbot;
27164 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27165 +       struct au_branch *br;
27166 +       struct au_wbr *wbr;
27167 +       struct dentry *root, *dentry;
27168 +       struct inode *dir, *h_dir;
27169 +       struct au_sbinfo *sbinfo;
27170 +       struct au_hinode *hdir;
27171 +
27172 +       SiMustAnyLock(sb);
27173 +
27174 +       sbinfo = au_sbi(sb);
27175 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27176 +
27177 +       if (!(sb_flags & SB_RDONLY)) {
27178 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27179 +                       pr_warn("first branch should be rw\n");
27180 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27181 +                       pr_warn_once("shwh should be used with ro\n");
27182 +       }
27183 +
27184 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27185 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27186 +               pr_warn_once("udba=*notify requires xino\n");
27187 +
27188 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27189 +               pr_warn_once("dirperm1 breaks the protection"
27190 +                            " by the permission bits on the lower branch\n");
27191 +
27192 +       err = 0;
27193 +       fhsm = 0;
27194 +       root = sb->s_root;
27195 +       dir = d_inode(root);
27196 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27197 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27198 +                                     UDBA_NONE);
27199 +       bbot = au_sbbot(sb);
27200 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27201 +               skip = 0;
27202 +               h_dir = au_h_iptr(dir, bindex);
27203 +               br = au_sbr(sb, bindex);
27204 +
27205 +               if ((br->br_perm & AuBrAttr_ICEX)
27206 +                   && !h_dir->i_op->listxattr)
27207 +                       br->br_perm &= ~AuBrAttr_ICEX;
27208 +#if 0 /* untested */
27209 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27210 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27211 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27212 +#endif
27213 +
27214 +               do_free = 0;
27215 +               wbr = br->br_wbr;
27216 +               if (wbr)
27217 +                       wbr_wh_read_lock(wbr);
27218 +
27219 +               if (!au_br_writable(br->br_perm)) {
27220 +                       do_free = !!wbr;
27221 +                       skip = (!wbr
27222 +                               || (!wbr->wbr_whbase
27223 +                                   && !wbr->wbr_plink
27224 +                                   && !wbr->wbr_orph));
27225 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27226 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27227 +                       skip = (!wbr || !wbr->wbr_whbase);
27228 +                       if (skip && wbr) {
27229 +                               if (do_plink)
27230 +                                       skip = !!wbr->wbr_plink;
27231 +                               else
27232 +                                       skip = !wbr->wbr_plink;
27233 +                       }
27234 +               } else {
27235 +                       /* skip = (br->br_whbase && br->br_ohph); */
27236 +                       skip = (wbr && wbr->wbr_whbase);
27237 +                       if (skip) {
27238 +                               if (do_plink)
27239 +                                       skip = !!wbr->wbr_plink;
27240 +                               else
27241 +                                       skip = !wbr->wbr_plink;
27242 +                       }
27243 +               }
27244 +               if (wbr)
27245 +                       wbr_wh_read_unlock(wbr);
27246 +
27247 +               if (can_no_dreval) {
27248 +                       dentry = br->br_path.dentry;
27249 +                       spin_lock(&dentry->d_lock);
27250 +                       if (dentry->d_flags &
27251 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27252 +                               can_no_dreval = 0;
27253 +                       spin_unlock(&dentry->d_lock);
27254 +               }
27255 +
27256 +               if (au_br_fhsm(br->br_perm)) {
27257 +                       fhsm++;
27258 +                       AuDebugOn(!br->br_fhsm);
27259 +               }
27260 +
27261 +               if (skip)
27262 +                       continue;
27263 +
27264 +               hdir = au_hi(dir, bindex);
27265 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27266 +               if (wbr)
27267 +                       wbr_wh_write_lock(wbr);
27268 +               err = au_wh_init(br, sb);
27269 +               if (wbr)
27270 +                       wbr_wh_write_unlock(wbr);
27271 +               au_hn_inode_unlock(hdir);
27272 +
27273 +               if (!err && do_free) {
27274 +                       au_kfree_rcu(wbr);
27275 +                       br->br_wbr = NULL;
27276 +               }
27277 +       }
27278 +
27279 +       if (can_no_dreval)
27280 +               au_fset_si(sbinfo, NO_DREVAL);
27281 +       else
27282 +               au_fclr_si(sbinfo, NO_DREVAL);
27283 +
27284 +       if (fhsm >= 2) {
27285 +               au_fset_si(sbinfo, FHSM);
27286 +               for (bindex = bbot; bindex >= 0; bindex--) {
27287 +                       br = au_sbr(sb, bindex);
27288 +                       if (au_br_fhsm(br->br_perm)) {
27289 +                               au_fhsm_set_bottom(sb, bindex);
27290 +                               break;
27291 +                       }
27292 +               }
27293 +       } else {
27294 +               au_fclr_si(sbinfo, FHSM);
27295 +               au_fhsm_set_bottom(sb, -1);
27296 +       }
27297 +
27298 +       return err;
27299 +}
27300 +
27301 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27302 +{
27303 +       int err;
27304 +       unsigned int tmp;
27305 +       aufs_bindex_t bindex, bbot;
27306 +       struct au_opt *opt;
27307 +       struct au_opt_xino *opt_xino, xino;
27308 +       struct au_sbinfo *sbinfo;
27309 +       struct au_branch *br;
27310 +       struct inode *dir;
27311 +
27312 +       SiMustWriteLock(sb);
27313 +
27314 +       err = 0;
27315 +       opt_xino = NULL;
27316 +       opt = opts->opt;
27317 +       while (err >= 0 && opt->type != Opt_tail)
27318 +               err = au_opt_simple(sb, opt++, opts);
27319 +       if (err > 0)
27320 +               err = 0;
27321 +       else if (unlikely(err < 0))
27322 +               goto out;
27323 +
27324 +       /* disable xino and udba temporary */
27325 +       sbinfo = au_sbi(sb);
27326 +       tmp = sbinfo->si_mntflags;
27327 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27328 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27329 +
27330 +       opt = opts->opt;
27331 +       while (err >= 0 && opt->type != Opt_tail)
27332 +               err = au_opt_br(sb, opt++, opts);
27333 +       if (err > 0)
27334 +               err = 0;
27335 +       else if (unlikely(err < 0))
27336 +               goto out;
27337 +
27338 +       bbot = au_sbbot(sb);
27339 +       if (unlikely(bbot < 0)) {
27340 +               err = -EINVAL;
27341 +               pr_err("no branches\n");
27342 +               goto out;
27343 +       }
27344 +
27345 +       if (au_opt_test(tmp, XINO))
27346 +               au_opt_set(sbinfo->si_mntflags, XINO);
27347 +       opt = opts->opt;
27348 +       while (!err && opt->type != Opt_tail)
27349 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27350 +       if (unlikely(err))
27351 +               goto out;
27352 +
27353 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27354 +       if (unlikely(err))
27355 +               goto out;
27356 +
27357 +       /* restore xino */
27358 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27359 +               xino.file = au_xino_def(sb);
27360 +               err = PTR_ERR(xino.file);
27361 +               if (IS_ERR(xino.file))
27362 +                       goto out;
27363 +
27364 +               err = au_xino_set(sb, &xino, /*remount*/0);
27365 +               fput(xino.file);
27366 +               if (unlikely(err))
27367 +                       goto out;
27368 +       }
27369 +
27370 +       /* restore udba */
27371 +       tmp &= AuOptMask_UDBA;
27372 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27373 +       sbinfo->si_mntflags |= tmp;
27374 +       bbot = au_sbbot(sb);
27375 +       for (bindex = 0; bindex <= bbot; bindex++) {
27376 +               br = au_sbr(sb, bindex);
27377 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27378 +               if (unlikely(err))
27379 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27380 +                               bindex, err);
27381 +               /* go on even if err */
27382 +       }
27383 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27384 +               dir = d_inode(sb->s_root);
27385 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27386 +       }
27387 +
27388 +out:
27389 +       return err;
27390 +}
27391 +
27392 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27393 +{
27394 +       int err, rerr;
27395 +       unsigned char no_dreval;
27396 +       struct inode *dir;
27397 +       struct au_opt_xino *opt_xino;
27398 +       struct au_opt *opt;
27399 +       struct au_sbinfo *sbinfo;
27400 +
27401 +       SiMustWriteLock(sb);
27402 +
27403 +       err = au_dr_opt_flush(sb);
27404 +       if (unlikely(err))
27405 +               goto out;
27406 +       au_fset_opts(opts->flags, DR_FLUSHED);
27407 +
27408 +       dir = d_inode(sb->s_root);
27409 +       sbinfo = au_sbi(sb);
27410 +       opt_xino = NULL;
27411 +       opt = opts->opt;
27412 +       while (err >= 0 && opt->type != Opt_tail) {
27413 +               err = au_opt_simple(sb, opt, opts);
27414 +               if (!err)
27415 +                       err = au_opt_br(sb, opt, opts);
27416 +               if (!err)
27417 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27418 +               opt++;
27419 +       }
27420 +       if (err > 0)
27421 +               err = 0;
27422 +       AuTraceErr(err);
27423 +       /* go on even err */
27424 +
27425 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27426 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27427 +       if (unlikely(rerr && !err))
27428 +               err = rerr;
27429 +
27430 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27431 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27432 +
27433 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27434 +               rerr = au_xib_trunc(sb);
27435 +               if (unlikely(rerr && !err))
27436 +                       err = rerr;
27437 +       }
27438 +
27439 +       /* will be handled by the caller */
27440 +       if (!au_ftest_opts(opts->flags, REFRESH)
27441 +           && (opts->given_udba
27442 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27443 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27444 +                   ))
27445 +               au_fset_opts(opts->flags, REFRESH);
27446 +
27447 +       AuDbg("status 0x%x\n", opts->flags);
27448 +
27449 +out:
27450 +       return err;
27451 +}
27452 +
27453 +/* ---------------------------------------------------------------------- */
27454 +
27455 +unsigned int au_opt_udba(struct super_block *sb)
27456 +{
27457 +       return au_mntflags(sb) & AuOptMask_UDBA;
27458 +}
27459 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27460 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27461 +++ linux/fs/aufs/opts.h        2021-12-03 15:38:59.939980643 +0100
27462 @@ -0,0 +1,225 @@
27463 +/* SPDX-License-Identifier: GPL-2.0 */
27464 +/*
27465 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27466 + *
27467 + * This program, aufs is free software; you can redistribute it and/or modify
27468 + * it under the terms of the GNU General Public License as published by
27469 + * the Free Software Foundation; either version 2 of the License, or
27470 + * (at your option) any later version.
27471 + *
27472 + * This program is distributed in the hope that it will be useful,
27473 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27474 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27475 + * GNU General Public License for more details.
27476 + *
27477 + * You should have received a copy of the GNU General Public License
27478 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27479 + */
27480 +
27481 +/*
27482 + * mount options/flags
27483 + */
27484 +
27485 +#ifndef __AUFS_OPTS_H__
27486 +#define __AUFS_OPTS_H__
27487 +
27488 +#ifdef __KERNEL__
27489 +
27490 +#include <linux/path.h>
27491 +
27492 +struct file;
27493 +
27494 +/* ---------------------------------------------------------------------- */
27495 +
27496 +/* mount flags */
27497 +#define AuOpt_XINO             1               /* external inode number bitmap
27498 +                                                  and translation table */
27499 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27500 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27501 +#define AuOpt_UDBA_REVAL       (1 << 3)
27502 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27503 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27504 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27505 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27506 +                                                  bits */
27507 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27508 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27509 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27510 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27511 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27512 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27513 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27514 +
27515 +#ifndef CONFIG_AUFS_HNOTIFY
27516 +#undef AuOpt_UDBA_HNOTIFY
27517 +#define AuOpt_UDBA_HNOTIFY     0
27518 +#endif
27519 +#ifndef CONFIG_AUFS_DIRREN
27520 +#undef AuOpt_DIRREN
27521 +#define AuOpt_DIRREN           0
27522 +#endif
27523 +#ifndef CONFIG_AUFS_SHWH
27524 +#undef AuOpt_SHWH
27525 +#define AuOpt_SHWH             0
27526 +#endif
27527 +
27528 +#define AuOpt_Def      (AuOpt_XINO \
27529 +                        | AuOpt_UDBA_REVAL \
27530 +                        | AuOpt_PLINK \
27531 +                        /* | AuOpt_DIRPERM1 */ \
27532 +                        | AuOpt_WARN_PERM)
27533 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27534 +                        | AuOpt_UDBA_REVAL \
27535 +                        | AuOpt_UDBA_HNOTIFY)
27536 +
27537 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27538 +#define au_opt_set(flags, name) do { \
27539 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27540 +       ((flags) |= AuOpt_##name); \
27541 +} while (0)
27542 +#define au_opt_set_udba(flags, name) do { \
27543 +       (flags) &= ~AuOptMask_UDBA; \
27544 +       ((flags) |= AuOpt_##name); \
27545 +} while (0)
27546 +#define au_opt_clr(flags, name) do { \
27547 +       ((flags) &= ~AuOpt_##name); \
27548 +} while (0)
27549 +
27550 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27551 +{
27552 +#ifdef CONFIG_PROC_FS
27553 +       return mntflags;
27554 +#else
27555 +       return mntflags & ~AuOpt_PLINK;
27556 +#endif
27557 +}
27558 +
27559 +/* ---------------------------------------------------------------------- */
27560 +
27561 +/* policies to select one among multiple writable branches */
27562 +enum {
27563 +       AuWbrCreate_TDP,        /* top down parent */
27564 +       AuWbrCreate_RR,         /* round robin */
27565 +       AuWbrCreate_MFS,        /* most free space */
27566 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27567 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27568 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27569 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27570 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27571 +       AuWbrCreate_PMFS,       /* parent and mfs */
27572 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27573 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27574 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27575 +
27576 +       AuWbrCreate_Def = AuWbrCreate_TDP
27577 +};
27578 +
27579 +enum {
27580 +       AuWbrCopyup_TDP,        /* top down parent */
27581 +       AuWbrCopyup_BUP,        /* bottom up parent */
27582 +       AuWbrCopyup_BU,         /* bottom up */
27583 +
27584 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27585 +};
27586 +
27587 +/* ---------------------------------------------------------------------- */
27588 +
27589 +struct au_opt_add {
27590 +       aufs_bindex_t   bindex;
27591 +       char            *pathname;
27592 +       int             perm;
27593 +       struct path     path;
27594 +};
27595 +
27596 +struct au_opt_del {
27597 +       char            *pathname;
27598 +       struct path     h_path;
27599 +};
27600 +
27601 +struct au_opt_mod {
27602 +       char            *path;
27603 +       int             perm;
27604 +       struct dentry   *h_root;
27605 +};
27606 +
27607 +struct au_opt_xino {
27608 +       char            *path;
27609 +       struct file     *file;
27610 +};
27611 +
27612 +struct au_opt_xino_itrunc {
27613 +       aufs_bindex_t   bindex;
27614 +};
27615 +
27616 +struct au_opt_wbr_create {
27617 +       int                     wbr_create;
27618 +       int                     mfs_second;
27619 +       unsigned long long      mfsrr_watermark;
27620 +};
27621 +
27622 +struct au_opt {
27623 +       int type;
27624 +       union {
27625 +               struct au_opt_xino      xino;
27626 +               struct au_opt_xino_itrunc xino_itrunc;
27627 +               struct au_opt_add       add;
27628 +               struct au_opt_del       del;
27629 +               struct au_opt_mod       mod;
27630 +               int                     dirwh;
27631 +               int                     rdcache;
27632 +               unsigned int            rdblk;
27633 +               unsigned int            rdhash;
27634 +               int                     udba;
27635 +               struct au_opt_wbr_create wbr_create;
27636 +               int                     wbr_copyup;
27637 +               unsigned int            fhsm_second;
27638 +       };
27639 +};
27640 +
27641 +/* opts flags */
27642 +#define AuOpts_REMOUNT         1
27643 +#define AuOpts_REFRESH         (1 << 1)
27644 +#define AuOpts_TRUNC_XIB       (1 << 2)
27645 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27646 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27647 +#define AuOpts_DR_FLUSHED      (1 << 5)
27648 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27649 +#define au_fset_opts(flags, name) \
27650 +       do { (flags) |= AuOpts_##name; } while (0)
27651 +#define au_fclr_opts(flags, name) \
27652 +       do { (flags) &= ~AuOpts_##name; } while (0)
27653 +
27654 +#ifndef CONFIG_AUFS_DIRREN
27655 +#undef AuOpts_DR_FLUSHED
27656 +#define AuOpts_DR_FLUSHED      0
27657 +#endif
27658 +
27659 +struct au_opts {
27660 +       struct au_opt   *opt;
27661 +       int             max_opt;
27662 +
27663 +       unsigned int    given_udba;
27664 +       unsigned int    flags;
27665 +       unsigned long   sb_flags;
27666 +};
27667 +
27668 +/* ---------------------------------------------------------------------- */
27669 +
27670 +/* opts.c */
27671 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27672 +const char *au_optstr_udba(int udba);
27673 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27674 +const char *au_optstr_wbr_create(int wbr_create);
27675 +
27676 +void au_opts_free(struct au_opts *opts);
27677 +struct super_block;
27678 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27679 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27680 +                  unsigned int pending);
27681 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27682 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27683 +
27684 +unsigned int au_opt_udba(struct super_block *sb);
27685 +
27686 +#endif /* __KERNEL__ */
27687 +#endif /* __AUFS_OPTS_H__ */
27688 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27689 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27690 +++ linux/fs/aufs/plink.c       2021-12-03 15:38:59.939980643 +0100
27691 @@ -0,0 +1,516 @@
27692 +// SPDX-License-Identifier: GPL-2.0
27693 +/*
27694 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27695 + *
27696 + * This program, aufs is free software; you can redistribute it and/or modify
27697 + * it under the terms of the GNU General Public License as published by
27698 + * the Free Software Foundation; either version 2 of the License, or
27699 + * (at your option) any later version.
27700 + *
27701 + * This program is distributed in the hope that it will be useful,
27702 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27703 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27704 + * GNU General Public License for more details.
27705 + *
27706 + * You should have received a copy of the GNU General Public License
27707 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27708 + */
27709 +
27710 +/*
27711 + * pseudo-link
27712 + */
27713 +
27714 +#include "aufs.h"
27715 +
27716 +/*
27717 + * the pseudo-link maintenance mode.
27718 + * during a user process maintains the pseudo-links,
27719 + * prohibit adding a new plink and branch manipulation.
27720 + *
27721 + * Flags
27722 + * NOPLM:
27723 + *     For entry functions which will handle plink, and i_mutex is already held
27724 + *     in VFS.
27725 + *     They cannot wait and should return an error at once.
27726 + *     Callers has to check the error.
27727 + * NOPLMW:
27728 + *     For entry functions which will handle plink, but i_mutex is not held
27729 + *     in VFS.
27730 + *     They can wait the plink maintenance mode to finish.
27731 + *
27732 + * They behave like F_SETLK and F_SETLKW.
27733 + * If the caller never handle plink, then both flags are unnecessary.
27734 + */
27735 +
27736 +int au_plink_maint(struct super_block *sb, int flags)
27737 +{
27738 +       int err;
27739 +       pid_t pid, ppid;
27740 +       struct task_struct *parent, *prev;
27741 +       struct au_sbinfo *sbi;
27742 +
27743 +       SiMustAnyLock(sb);
27744 +
27745 +       err = 0;
27746 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27747 +               goto out;
27748 +
27749 +       sbi = au_sbi(sb);
27750 +       pid = sbi->si_plink_maint_pid;
27751 +       if (!pid || pid == current->pid)
27752 +               goto out;
27753 +
27754 +       /* todo: it highly depends upon /sbin/mount.aufs */
27755 +       prev = NULL;
27756 +       parent = current;
27757 +       ppid = 0;
27758 +       rcu_read_lock();
27759 +       while (1) {
27760 +               parent = rcu_dereference(parent->real_parent);
27761 +               if (parent == prev)
27762 +                       break;
27763 +               ppid = task_pid_vnr(parent);
27764 +               if (pid == ppid) {
27765 +                       rcu_read_unlock();
27766 +                       goto out;
27767 +               }
27768 +               prev = parent;
27769 +       }
27770 +       rcu_read_unlock();
27771 +
27772 +       if (au_ftest_lock(flags, NOPLMW)) {
27773 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27774 +               /* AuDebugOn(!lockdep_depth(current)); */
27775 +               while (sbi->si_plink_maint_pid) {
27776 +                       si_read_unlock(sb);
27777 +                       /* gave up wake_up_bit() */
27778 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27779 +
27780 +                       if (au_ftest_lock(flags, FLUSH))
27781 +                               au_nwt_flush(&sbi->si_nowait);
27782 +                       si_noflush_read_lock(sb);
27783 +               }
27784 +       } else if (au_ftest_lock(flags, NOPLM)) {
27785 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27786 +               err = -EAGAIN;
27787 +       }
27788 +
27789 +out:
27790 +       return err;
27791 +}
27792 +
27793 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27794 +{
27795 +       spin_lock(&sbinfo->si_plink_maint_lock);
27796 +       sbinfo->si_plink_maint_pid = 0;
27797 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27798 +       wake_up_all(&sbinfo->si_plink_wq);
27799 +}
27800 +
27801 +int au_plink_maint_enter(struct super_block *sb)
27802 +{
27803 +       int err;
27804 +       struct au_sbinfo *sbinfo;
27805 +
27806 +       err = 0;
27807 +       sbinfo = au_sbi(sb);
27808 +       /* make sure i am the only one in this fs */
27809 +       si_write_lock(sb, AuLock_FLUSH);
27810 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27811 +               spin_lock(&sbinfo->si_plink_maint_lock);
27812 +               if (!sbinfo->si_plink_maint_pid)
27813 +                       sbinfo->si_plink_maint_pid = current->pid;
27814 +               else
27815 +                       err = -EBUSY;
27816 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27817 +       }
27818 +       si_write_unlock(sb);
27819 +
27820 +       return err;
27821 +}
27822 +
27823 +/* ---------------------------------------------------------------------- */
27824 +
27825 +#ifdef CONFIG_AUFS_DEBUG
27826 +void au_plink_list(struct super_block *sb)
27827 +{
27828 +       int i;
27829 +       struct au_sbinfo *sbinfo;
27830 +       struct hlist_bl_head *hbl;
27831 +       struct hlist_bl_node *pos;
27832 +       struct au_icntnr *icntnr;
27833 +
27834 +       SiMustAnyLock(sb);
27835 +
27836 +       sbinfo = au_sbi(sb);
27837 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27838 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27839 +
27840 +       for (i = 0; i < AuPlink_NHASH; i++) {
27841 +               hbl = sbinfo->si_plink + i;
27842 +               hlist_bl_lock(hbl);
27843 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27844 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27845 +               hlist_bl_unlock(hbl);
27846 +       }
27847 +}
27848 +#endif
27849 +
27850 +/* is the inode pseudo-linked? */
27851 +int au_plink_test(struct inode *inode)
27852 +{
27853 +       int found, i;
27854 +       struct au_sbinfo *sbinfo;
27855 +       struct hlist_bl_head *hbl;
27856 +       struct hlist_bl_node *pos;
27857 +       struct au_icntnr *icntnr;
27858 +
27859 +       sbinfo = au_sbi(inode->i_sb);
27860 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27861 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27862 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27863 +
27864 +       found = 0;
27865 +       i = au_plink_hash(inode->i_ino);
27866 +       hbl =  sbinfo->si_plink + i;
27867 +       hlist_bl_lock(hbl);
27868 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27869 +               if (&icntnr->vfs_inode == inode) {
27870 +                       found = 1;
27871 +                       break;
27872 +               }
27873 +       hlist_bl_unlock(hbl);
27874 +       return found;
27875 +}
27876 +
27877 +/* ---------------------------------------------------------------------- */
27878 +
27879 +/*
27880 + * generate a name for plink.
27881 + * the file will be stored under AUFS_WH_PLINKDIR.
27882 + */
27883 +/* 20 is max digits length of ulong 64 */
27884 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27885 +
27886 +static int plink_name(char *name, int len, struct inode *inode,
27887 +                     aufs_bindex_t bindex)
27888 +{
27889 +       int rlen;
27890 +       struct inode *h_inode;
27891 +
27892 +       h_inode = au_h_iptr(inode, bindex);
27893 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27894 +       return rlen;
27895 +}
27896 +
27897 +struct au_do_plink_lkup_args {
27898 +       struct dentry **errp;
27899 +       struct qstr *tgtname;
27900 +       struct path *h_ppath;
27901 +};
27902 +
27903 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27904 +                                      struct path *h_ppath)
27905 +{
27906 +       struct dentry *h_dentry;
27907 +       struct inode *h_inode;
27908 +
27909 +       h_inode = d_inode(h_ppath->dentry);
27910 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27911 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
27912 +       inode_unlock_shared(h_inode);
27913 +
27914 +       return h_dentry;
27915 +}
27916 +
27917 +static void au_call_do_plink_lkup(void *args)
27918 +{
27919 +       struct au_do_plink_lkup_args *a = args;
27920 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
27921 +}
27922 +
27923 +/* lookup the plink-ed @inode under the branch at @bindex */
27924 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27925 +{
27926 +       struct dentry *h_dentry;
27927 +       struct au_branch *br;
27928 +       struct path h_ppath;
27929 +       int wkq_err;
27930 +       char a[PLINK_NAME_LEN];
27931 +       struct qstr tgtname = QSTR_INIT(a, 0);
27932 +
27933 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27934 +
27935 +       br = au_sbr(inode->i_sb, bindex);
27936 +       h_ppath.dentry = br->br_wbr->wbr_plink;
27937 +       h_ppath.mnt = au_br_mnt(br);
27938 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27939 +
27940 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27941 +               struct au_do_plink_lkup_args args = {
27942 +                       .errp           = &h_dentry,
27943 +                       .tgtname        = &tgtname,
27944 +                       .h_ppath        = &h_ppath
27945 +               };
27946 +
27947 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27948 +               if (unlikely(wkq_err))
27949 +                       h_dentry = ERR_PTR(wkq_err);
27950 +       } else
27951 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
27952 +
27953 +       return h_dentry;
27954 +}
27955 +
27956 +/* create a pseudo-link */
27957 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
27958 +                     struct dentry *h_dentry)
27959 +{
27960 +       int err;
27961 +       struct path h_path;
27962 +       struct inode *h_dir, *delegated;
27963 +
27964 +       h_dir = d_inode(h_ppath->dentry);
27965 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27966 +       h_path.mnt = h_ppath->mnt;
27967 +again:
27968 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
27969 +       err = PTR_ERR(h_path.dentry);
27970 +       if (IS_ERR(h_path.dentry))
27971 +               goto out;
27972 +
27973 +       err = 0;
27974 +       /* wh.plink dir is not monitored */
27975 +       /* todo: is it really safe? */
27976 +       if (d_is_positive(h_path.dentry)
27977 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27978 +               delegated = NULL;
27979 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27980 +               if (unlikely(err == -EWOULDBLOCK)) {
27981 +                       pr_warn("cannot retry for NFSv4 delegation"
27982 +                               " for an internal unlink\n");
27983 +                       iput(delegated);
27984 +               }
27985 +               dput(h_path.dentry);
27986 +               h_path.dentry = NULL;
27987 +               if (!err)
27988 +                       goto again;
27989 +       }
27990 +       if (!err && d_is_negative(h_path.dentry)) {
27991 +               delegated = NULL;
27992 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27993 +               if (unlikely(err == -EWOULDBLOCK)) {
27994 +                       pr_warn("cannot retry for NFSv4 delegation"
27995 +                               " for an internal link\n");
27996 +                       iput(delegated);
27997 +               }
27998 +       }
27999 +       dput(h_path.dentry);
28000 +
28001 +out:
28002 +       inode_unlock(h_dir);
28003 +       return err;
28004 +}
28005 +
28006 +struct do_whplink_args {
28007 +       int *errp;
28008 +       struct qstr *tgt;
28009 +       struct path *h_ppath;
28010 +       struct dentry *h_dentry;
28011 +};
28012 +
28013 +static void call_do_whplink(void *args)
28014 +{
28015 +       struct do_whplink_args *a = args;
28016 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28017 +}
28018 +
28019 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28020 +                  aufs_bindex_t bindex)
28021 +{
28022 +       int err, wkq_err;
28023 +       struct au_branch *br;
28024 +       struct au_wbr *wbr;
28025 +       struct path h_ppath;
28026 +       char a[PLINK_NAME_LEN];
28027 +       struct qstr tgtname = QSTR_INIT(a, 0);
28028 +
28029 +       br = au_sbr(inode->i_sb, bindex);
28030 +       wbr = br->br_wbr;
28031 +       h_ppath.dentry = wbr->wbr_plink;
28032 +       h_ppath.mnt = au_br_mnt(br);
28033 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28034 +
28035 +       /* always superio. */
28036 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28037 +               struct do_whplink_args args = {
28038 +                       .errp           = &err,
28039 +                       .tgt            = &tgtname,
28040 +                       .h_ppath        = &h_ppath,
28041 +                       .h_dentry       = h_dentry
28042 +               };
28043 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28044 +               if (unlikely(wkq_err))
28045 +                       err = wkq_err;
28046 +       } else
28047 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28048 +
28049 +       return err;
28050 +}
28051 +
28052 +/*
28053 + * create a new pseudo-link for @h_dentry on @bindex.
28054 + * the linked inode is held in aufs @inode.
28055 + */
28056 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28057 +                    struct dentry *h_dentry)
28058 +{
28059 +       struct super_block *sb;
28060 +       struct au_sbinfo *sbinfo;
28061 +       struct hlist_bl_head *hbl;
28062 +       struct hlist_bl_node *pos;
28063 +       struct au_icntnr *icntnr;
28064 +       int found, err, cnt, i;
28065 +
28066 +       sb = inode->i_sb;
28067 +       sbinfo = au_sbi(sb);
28068 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28069 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28070 +
28071 +       found = au_plink_test(inode);
28072 +       if (found)
28073 +               return;
28074 +
28075 +       i = au_plink_hash(inode->i_ino);
28076 +       hbl = sbinfo->si_plink + i;
28077 +       au_igrab(inode);
28078 +
28079 +       hlist_bl_lock(hbl);
28080 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28081 +               if (&icntnr->vfs_inode == inode) {
28082 +                       found = 1;
28083 +                       break;
28084 +               }
28085 +       }
28086 +       if (!found) {
28087 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28088 +               hlist_bl_add_head(&icntnr->plink, hbl);
28089 +       }
28090 +       hlist_bl_unlock(hbl);
28091 +       if (!found) {
28092 +               cnt = au_hbl_count(hbl);
28093 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28094 +               if (cnt > AUFS_PLINK_WARN)
28095 +                       AuWarn1(msg ", %d\n", cnt);
28096 +#undef msg
28097 +               err = whplink(h_dentry, inode, bindex);
28098 +               if (unlikely(err)) {
28099 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28100 +                       au_hbl_del(&icntnr->plink, hbl);
28101 +                       iput(&icntnr->vfs_inode);
28102 +               }
28103 +       } else
28104 +               iput(&icntnr->vfs_inode);
28105 +}
28106 +
28107 +/* free all plinks */
28108 +void au_plink_put(struct super_block *sb, int verbose)
28109 +{
28110 +       int i, warned;
28111 +       struct au_sbinfo *sbinfo;
28112 +       struct hlist_bl_head *hbl;
28113 +       struct hlist_bl_node *pos, *tmp;
28114 +       struct au_icntnr *icntnr;
28115 +
28116 +       SiMustWriteLock(sb);
28117 +
28118 +       sbinfo = au_sbi(sb);
28119 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28120 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28121 +
28122 +       /* no spin_lock since sbinfo is write-locked */
28123 +       warned = 0;
28124 +       for (i = 0; i < AuPlink_NHASH; i++) {
28125 +               hbl = sbinfo->si_plink + i;
28126 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28127 +                       pr_warn("pseudo-link is not flushed");
28128 +                       warned = 1;
28129 +               }
28130 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28131 +                       iput(&icntnr->vfs_inode);
28132 +               INIT_HLIST_BL_HEAD(hbl);
28133 +       }
28134 +}
28135 +
28136 +void au_plink_clean(struct super_block *sb, int verbose)
28137 +{
28138 +       struct dentry *root;
28139 +
28140 +       root = sb->s_root;
28141 +       aufs_write_lock(root);
28142 +       if (au_opt_test(au_mntflags(sb), PLINK))
28143 +               au_plink_put(sb, verbose);
28144 +       aufs_write_unlock(root);
28145 +}
28146 +
28147 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28148 +{
28149 +       int do_put;
28150 +       aufs_bindex_t btop, bbot, bindex;
28151 +
28152 +       do_put = 0;
28153 +       btop = au_ibtop(inode);
28154 +       bbot = au_ibbot(inode);
28155 +       if (btop >= 0) {
28156 +               for (bindex = btop; bindex <= bbot; bindex++) {
28157 +                       if (!au_h_iptr(inode, bindex)
28158 +                           || au_ii_br_id(inode, bindex) != br_id)
28159 +                               continue;
28160 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28161 +                       do_put = 1;
28162 +                       break;
28163 +               }
28164 +               if (do_put)
28165 +                       for (bindex = btop; bindex <= bbot; bindex++)
28166 +                               if (au_h_iptr(inode, bindex)) {
28167 +                                       do_put = 0;
28168 +                                       break;
28169 +                               }
28170 +       } else
28171 +               do_put = 1;
28172 +
28173 +       return do_put;
28174 +}
28175 +
28176 +/* free the plinks on a branch specified by @br_id */
28177 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28178 +{
28179 +       struct au_sbinfo *sbinfo;
28180 +       struct hlist_bl_head *hbl;
28181 +       struct hlist_bl_node *pos, *tmp;
28182 +       struct au_icntnr *icntnr;
28183 +       struct inode *inode;
28184 +       int i, do_put;
28185 +
28186 +       SiMustWriteLock(sb);
28187 +
28188 +       sbinfo = au_sbi(sb);
28189 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28190 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28191 +
28192 +       /* no bit_lock since sbinfo is write-locked */
28193 +       for (i = 0; i < AuPlink_NHASH; i++) {
28194 +               hbl = sbinfo->si_plink + i;
28195 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28196 +                       inode = au_igrab(&icntnr->vfs_inode);
28197 +                       ii_write_lock_child(inode);
28198 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28199 +                       if (do_put) {
28200 +                               hlist_bl_del(&icntnr->plink);
28201 +                               iput(inode);
28202 +                       }
28203 +                       ii_write_unlock(inode);
28204 +                       iput(inode);
28205 +               }
28206 +       }
28207 +}
28208 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28209 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28210 +++ linux/fs/aufs/poll.c        2021-12-03 15:38:59.939980643 +0100
28211 @@ -0,0 +1,51 @@
28212 +// SPDX-License-Identifier: GPL-2.0
28213 +/*
28214 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28215 + *
28216 + * This program, aufs is free software; you can redistribute it and/or modify
28217 + * it under the terms of the GNU General Public License as published by
28218 + * the Free Software Foundation; either version 2 of the License, or
28219 + * (at your option) any later version.
28220 + *
28221 + * This program is distributed in the hope that it will be useful,
28222 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28223 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28224 + * GNU General Public License for more details.
28225 + *
28226 + * You should have received a copy of the GNU General Public License
28227 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28228 + */
28229 +
28230 +/*
28231 + * poll operation
28232 + * There is only one filesystem which implements ->poll operation, currently.
28233 + */
28234 +
28235 +#include "aufs.h"
28236 +
28237 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28238 +{
28239 +       __poll_t mask;
28240 +       struct file *h_file;
28241 +       struct super_block *sb;
28242 +
28243 +       /* We should pretend an error happened. */
28244 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28245 +       sb = file->f_path.dentry->d_sb;
28246 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28247 +
28248 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28249 +       if (IS_ERR(h_file)) {
28250 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28251 +               goto out;
28252 +       }
28253 +
28254 +       mask = vfs_poll(h_file, pt);
28255 +       fput(h_file); /* instead of au_read_post() */
28256 +
28257 +out:
28258 +       si_read_unlock(sb);
28259 +       if (mask & EPOLLERR)
28260 +               AuDbg("mask 0x%x\n", mask);
28261 +       return mask;
28262 +}
28263 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28264 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28265 +++ linux/fs/aufs/posix_acl.c   2021-12-03 15:40:58.236647297 +0100
28266 @@ -0,0 +1,111 @@
28267 +// SPDX-License-Identifier: GPL-2.0
28268 +/*
28269 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28270 + *
28271 + * This program, aufs is free software; you can redistribute it and/or modify
28272 + * it under the terms of the GNU General Public License as published by
28273 + * the Free Software Foundation; either version 2 of the License, or
28274 + * (at your option) any later version.
28275 + *
28276 + * This program is distributed in the hope that it will be useful,
28277 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28278 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28279 + * GNU General Public License for more details.
28280 + *
28281 + * You should have received a copy of the GNU General Public License
28282 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28283 + */
28284 +
28285 +/*
28286 + * posix acl operations
28287 + */
28288 +
28289 +#include <linux/fs.h>
28290 +#include "aufs.h"
28291 +
28292 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28293 +{
28294 +       struct posix_acl *acl;
28295 +       int err;
28296 +       aufs_bindex_t bindex;
28297 +       struct inode *h_inode;
28298 +       struct super_block *sb;
28299 +
28300 +       acl = ERR_PTR(-ECHILD);
28301 +       if (rcu)
28302 +               goto out;
28303 +
28304 +       acl = NULL;
28305 +       sb = inode->i_sb;
28306 +       si_read_lock(sb, AuLock_FLUSH);
28307 +       ii_read_lock_child(inode);
28308 +       if (!(sb->s_flags & SB_POSIXACL))
28309 +               goto unlock;
28310 +
28311 +       bindex = au_ibtop(inode);
28312 +       h_inode = au_h_iptr(inode, bindex);
28313 +       if (unlikely(!h_inode
28314 +                    || ((h_inode->i_mode & S_IFMT)
28315 +                        != (inode->i_mode & S_IFMT)))) {
28316 +               err = au_busy_or_stale();
28317 +               acl = ERR_PTR(err);
28318 +               goto unlock;
28319 +       }
28320 +
28321 +       /* always topmost only */
28322 +       acl = get_acl(h_inode, type);
28323 +       if (IS_ERR(acl))
28324 +               forget_cached_acl(inode, type);
28325 +       else
28326 +               set_cached_acl(inode, type, acl);
28327 +
28328 +unlock:
28329 +       ii_read_unlock(inode);
28330 +       si_read_unlock(sb);
28331 +
28332 +out:
28333 +       AuTraceErrPtr(acl);
28334 +       return acl;
28335 +}
28336 +
28337 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28338 +                struct posix_acl *acl, int type)
28339 +{
28340 +       int err;
28341 +       ssize_t ssz;
28342 +       struct dentry *dentry;
28343 +       struct au_sxattr arg = {
28344 +               .type = AU_ACL_SET,
28345 +               .u.acl_set = {
28346 +                       .acl    = acl,
28347 +                       .type   = type
28348 +               },
28349 +       };
28350 +
28351 +       IMustLock(inode);
28352 +
28353 +       if (inode->i_ino == AUFS_ROOT_INO)
28354 +               dentry = dget(inode->i_sb->s_root);
28355 +       else {
28356 +               dentry = d_find_alias(inode);
28357 +               if (!dentry)
28358 +                       dentry = d_find_any_alias(inode);
28359 +               if (!dentry) {
28360 +                       pr_warn("cannot handle this inode, "
28361 +                               "please report to aufs-users ML\n");
28362 +                       err = -ENOENT;
28363 +                       goto out;
28364 +               }
28365 +       }
28366 +
28367 +       ssz = au_sxattr(dentry, inode, &arg);
28368 +       /* forget even it if succeeds since the branch might set differently */
28369 +       forget_cached_acl(inode, type);
28370 +       dput(dentry);
28371 +       err = ssz;
28372 +       if (ssz >= 0)
28373 +               err = 0;
28374 +
28375 +out:
28376 +       return err;
28377 +}
28378 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28379 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28380 +++ linux/fs/aufs/procfs.c      2021-12-03 15:38:59.939980643 +0100
28381 @@ -0,0 +1,170 @@
28382 +// SPDX-License-Identifier: GPL-2.0
28383 +/*
28384 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28385 + *
28386 + * This program, aufs is free software; you can redistribute it and/or modify
28387 + * it under the terms of the GNU General Public License as published by
28388 + * the Free Software Foundation; either version 2 of the License, or
28389 + * (at your option) any later version.
28390 + *
28391 + * This program is distributed in the hope that it will be useful,
28392 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28393 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28394 + * GNU General Public License for more details.
28395 + *
28396 + * You should have received a copy of the GNU General Public License
28397 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28398 + */
28399 +
28400 +/*
28401 + * procfs interfaces
28402 + */
28403 +
28404 +#include <linux/proc_fs.h>
28405 +#include "aufs.h"
28406 +
28407 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28408 +{
28409 +       struct au_sbinfo *sbinfo;
28410 +
28411 +       sbinfo = file->private_data;
28412 +       if (sbinfo) {
28413 +               au_plink_maint_leave(sbinfo);
28414 +               kobject_put(&sbinfo->si_kobj);
28415 +       }
28416 +
28417 +       return 0;
28418 +}
28419 +
28420 +static void au_procfs_plm_write_clean(struct file *file)
28421 +{
28422 +       struct au_sbinfo *sbinfo;
28423 +
28424 +       sbinfo = file->private_data;
28425 +       if (sbinfo)
28426 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28427 +}
28428 +
28429 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28430 +{
28431 +       int err;
28432 +       struct super_block *sb;
28433 +       struct au_sbinfo *sbinfo;
28434 +       struct hlist_bl_node *pos;
28435 +
28436 +       err = -EBUSY;
28437 +       if (unlikely(file->private_data))
28438 +               goto out;
28439 +
28440 +       sb = NULL;
28441 +       /* don't use au_sbilist_lock() here */
28442 +       hlist_bl_lock(&au_sbilist);
28443 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28444 +               if (id == sysaufs_si_id(sbinfo)) {
28445 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28446 +                               sb = sbinfo->si_sb;
28447 +                       break;
28448 +               }
28449 +       hlist_bl_unlock(&au_sbilist);
28450 +
28451 +       err = -EINVAL;
28452 +       if (unlikely(!sb))
28453 +               goto out;
28454 +
28455 +       err = au_plink_maint_enter(sb);
28456 +       if (!err)
28457 +               /* keep kobject_get() */
28458 +               file->private_data = sbinfo;
28459 +       else
28460 +               kobject_put(&sbinfo->si_kobj);
28461 +out:
28462 +       return err;
28463 +}
28464 +
28465 +/*
28466 + * Accept a valid "si=xxxx" only.
28467 + * Once it is accepted successfully, accept "clean" too.
28468 + */
28469 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28470 +                                  size_t count, loff_t *ppos)
28471 +{
28472 +       ssize_t err;
28473 +       unsigned long id;
28474 +       /* last newline is allowed */
28475 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28476 +
28477 +       err = -EACCES;
28478 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28479 +               goto out;
28480 +
28481 +       err = -EINVAL;
28482 +       if (unlikely(count > sizeof(buf)))
28483 +               goto out;
28484 +
28485 +       err = copy_from_user(buf, ubuf, count);
28486 +       if (unlikely(err)) {
28487 +               err = -EFAULT;
28488 +               goto out;
28489 +       }
28490 +       buf[count] = 0;
28491 +
28492 +       err = -EINVAL;
28493 +       if (!strcmp("clean", buf)) {
28494 +               au_procfs_plm_write_clean(file);
28495 +               goto out_success;
28496 +       } else if (unlikely(strncmp("si=", buf, 3)))
28497 +               goto out;
28498 +
28499 +       err = kstrtoul(buf + 3, 16, &id);
28500 +       if (unlikely(err))
28501 +               goto out;
28502 +
28503 +       err = au_procfs_plm_write_si(file, id);
28504 +       if (unlikely(err))
28505 +               goto out;
28506 +
28507 +out_success:
28508 +       err = count; /* success */
28509 +out:
28510 +       return err;
28511 +}
28512 +
28513 +static const struct proc_ops au_procfs_plm_op = {
28514 +       .proc_write     = au_procfs_plm_write,
28515 +       .proc_release   = au_procfs_plm_release
28516 +};
28517 +
28518 +/* ---------------------------------------------------------------------- */
28519 +
28520 +static struct proc_dir_entry *au_procfs_dir;
28521 +
28522 +void au_procfs_fin(void)
28523 +{
28524 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28525 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28526 +}
28527 +
28528 +int __init au_procfs_init(void)
28529 +{
28530 +       int err;
28531 +       struct proc_dir_entry *entry;
28532 +
28533 +       err = -ENOMEM;
28534 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28535 +       if (unlikely(!au_procfs_dir))
28536 +               goto out;
28537 +
28538 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28539 +                           au_procfs_dir, &au_procfs_plm_op);
28540 +       if (unlikely(!entry))
28541 +               goto out_dir;
28542 +
28543 +       err = 0;
28544 +       goto out; /* success */
28545 +
28546 +
28547 +out_dir:
28548 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28549 +out:
28550 +       return err;
28551 +}
28552 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28553 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28554 +++ linux/fs/aufs/rdu.c 2021-12-03 15:38:59.939980643 +0100
28555 @@ -0,0 +1,384 @@
28556 +// SPDX-License-Identifier: GPL-2.0
28557 +/*
28558 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28559 + *
28560 + * This program, aufs is free software; you can redistribute it and/or modify
28561 + * it under the terms of the GNU General Public License as published by
28562 + * the Free Software Foundation; either version 2 of the License, or
28563 + * (at your option) any later version.
28564 + *
28565 + * This program is distributed in the hope that it will be useful,
28566 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28567 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28568 + * GNU General Public License for more details.
28569 + *
28570 + * You should have received a copy of the GNU General Public License
28571 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28572 + */
28573 +
28574 +/*
28575 + * readdir in userspace.
28576 + */
28577 +
28578 +#include <linux/compat.h>
28579 +#include <linux/fs_stack.h>
28580 +#include <linux/security.h>
28581 +#include "aufs.h"
28582 +
28583 +/* bits for struct aufs_rdu.flags */
28584 +#define        AuRdu_CALLED    1
28585 +#define        AuRdu_CONT      (1 << 1)
28586 +#define        AuRdu_FULL      (1 << 2)
28587 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28588 +#define au_fset_rdu(flags, name) \
28589 +       do { (flags) |= AuRdu_##name; } while (0)
28590 +#define au_fclr_rdu(flags, name) \
28591 +       do { (flags) &= ~AuRdu_##name; } while (0)
28592 +
28593 +struct au_rdu_arg {
28594 +       struct dir_context              ctx;
28595 +       struct aufs_rdu                 *rdu;
28596 +       union au_rdu_ent_ul             ent;
28597 +       unsigned long                   end;
28598 +
28599 +       struct super_block              *sb;
28600 +       int                             err;
28601 +};
28602 +
28603 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28604 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28605 +{
28606 +       int err, len;
28607 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28608 +       struct aufs_rdu *rdu = arg->rdu;
28609 +       struct au_rdu_ent ent;
28610 +
28611 +       err = 0;
28612 +       arg->err = 0;
28613 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28614 +       len = au_rdu_len(nlen);
28615 +       if (arg->ent.ul + len  < arg->end) {
28616 +               ent.ino = h_ino;
28617 +               ent.bindex = rdu->cookie.bindex;
28618 +               ent.type = d_type;
28619 +               ent.nlen = nlen;
28620 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28621 +                       ent.type = DT_UNKNOWN;
28622 +
28623 +               /* unnecessary to support mmap_sem since this is a dir */
28624 +               err = -EFAULT;
28625 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28626 +                       goto out;
28627 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28628 +                       goto out;
28629 +               /* the terminating NULL */
28630 +               if (__put_user(0, arg->ent.e->name + nlen))
28631 +                       goto out;
28632 +               err = 0;
28633 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28634 +               arg->ent.ul += len;
28635 +               rdu->rent++;
28636 +       } else {
28637 +               err = -EFAULT;
28638 +               au_fset_rdu(rdu->cookie.flags, FULL);
28639 +               rdu->full = 1;
28640 +               rdu->tail = arg->ent;
28641 +       }
28642 +
28643 +out:
28644 +       /* AuTraceErr(err); */
28645 +       return err;
28646 +}
28647 +
28648 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28649 +{
28650 +       int err;
28651 +       loff_t offset;
28652 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28653 +
28654 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28655 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28656 +       err = offset;
28657 +       if (unlikely(offset != cookie->h_pos))
28658 +               goto out;
28659 +
28660 +       err = 0;
28661 +       do {
28662 +               arg->err = 0;
28663 +               au_fclr_rdu(cookie->flags, CALLED);
28664 +               /* smp_mb(); */
28665 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28666 +               if (err >= 0)
28667 +                       err = arg->err;
28668 +       } while (!err
28669 +                && au_ftest_rdu(cookie->flags, CALLED)
28670 +                && !au_ftest_rdu(cookie->flags, FULL));
28671 +       cookie->h_pos = h_file->f_pos;
28672 +
28673 +out:
28674 +       AuTraceErr(err);
28675 +       return err;
28676 +}
28677 +
28678 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28679 +{
28680 +       int err;
28681 +       aufs_bindex_t bbot;
28682 +       struct au_rdu_arg arg = {
28683 +               .ctx = {
28684 +                       .actor = au_rdu_fill
28685 +               }
28686 +       };
28687 +       struct dentry *dentry;
28688 +       struct inode *inode;
28689 +       struct file *h_file;
28690 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28691 +
28692 +       /* VERIFY_WRITE */
28693 +       err = !access_ok(rdu->ent.e, rdu->sz);
28694 +       if (unlikely(err)) {
28695 +               err = -EFAULT;
28696 +               AuTraceErr(err);
28697 +               goto out;
28698 +       }
28699 +       rdu->rent = 0;
28700 +       rdu->tail = rdu->ent;
28701 +       rdu->full = 0;
28702 +       arg.rdu = rdu;
28703 +       arg.ent = rdu->ent;
28704 +       arg.end = arg.ent.ul;
28705 +       arg.end += rdu->sz;
28706 +
28707 +       err = -ENOTDIR;
28708 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28709 +               goto out;
28710 +
28711 +       err = security_file_permission(file, MAY_READ);
28712 +       AuTraceErr(err);
28713 +       if (unlikely(err))
28714 +               goto out;
28715 +
28716 +       dentry = file->f_path.dentry;
28717 +       inode = d_inode(dentry);
28718 +       inode_lock_shared(inode);
28719 +
28720 +       arg.sb = inode->i_sb;
28721 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28722 +       if (unlikely(err))
28723 +               goto out_mtx;
28724 +       err = au_alive_dir(dentry);
28725 +       if (unlikely(err))
28726 +               goto out_si;
28727 +       /* todo: reval? */
28728 +       fi_read_lock(file);
28729 +
28730 +       err = -EAGAIN;
28731 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28732 +                    && cookie->generation != au_figen(file)))
28733 +               goto out_unlock;
28734 +
28735 +       err = 0;
28736 +       if (!rdu->blk) {
28737 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28738 +               if (!rdu->blk)
28739 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28740 +       }
28741 +       bbot = au_fbtop(file);
28742 +       if (cookie->bindex < bbot)
28743 +               cookie->bindex = bbot;
28744 +       bbot = au_fbbot_dir(file);
28745 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28746 +       for (; !err && cookie->bindex <= bbot;
28747 +            cookie->bindex++, cookie->h_pos = 0) {
28748 +               h_file = au_hf_dir(file, cookie->bindex);
28749 +               if (!h_file)
28750 +                       continue;
28751 +
28752 +               au_fclr_rdu(cookie->flags, FULL);
28753 +               err = au_rdu_do(h_file, &arg);
28754 +               AuTraceErr(err);
28755 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28756 +                       break;
28757 +       }
28758 +       AuDbg("rent %llu\n", rdu->rent);
28759 +
28760 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28761 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28762 +               au_fset_rdu(cookie->flags, CONT);
28763 +               cookie->generation = au_figen(file);
28764 +       }
28765 +
28766 +       ii_read_lock_child(inode);
28767 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28768 +       ii_read_unlock(inode);
28769 +
28770 +out_unlock:
28771 +       fi_read_unlock(file);
28772 +out_si:
28773 +       si_read_unlock(arg.sb);
28774 +out_mtx:
28775 +       inode_unlock_shared(inode);
28776 +out:
28777 +       AuTraceErr(err);
28778 +       return err;
28779 +}
28780 +
28781 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28782 +{
28783 +       int err;
28784 +       ino_t ino;
28785 +       unsigned long long nent;
28786 +       union au_rdu_ent_ul *u;
28787 +       struct au_rdu_ent ent;
28788 +       struct super_block *sb;
28789 +
28790 +       err = 0;
28791 +       nent = rdu->nent;
28792 +       u = &rdu->ent;
28793 +       sb = file->f_path.dentry->d_sb;
28794 +       si_read_lock(sb, AuLock_FLUSH);
28795 +       while (nent-- > 0) {
28796 +               /* unnecessary to support mmap_sem since this is a dir */
28797 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28798 +               if (!err)
28799 +                       /* VERIFY_WRITE */
28800 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28801 +               if (unlikely(err)) {
28802 +                       err = -EFAULT;
28803 +                       AuTraceErr(err);
28804 +                       break;
28805 +               }
28806 +
28807 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28808 +               if (!ent.wh)
28809 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28810 +               else
28811 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28812 +                                       &ino);
28813 +               if (unlikely(err)) {
28814 +                       AuTraceErr(err);
28815 +                       break;
28816 +               }
28817 +
28818 +               err = __put_user(ino, &u->e->ino);
28819 +               if (unlikely(err)) {
28820 +                       err = -EFAULT;
28821 +                       AuTraceErr(err);
28822 +                       break;
28823 +               }
28824 +               u->ul += au_rdu_len(ent.nlen);
28825 +       }
28826 +       si_read_unlock(sb);
28827 +
28828 +       return err;
28829 +}
28830 +
28831 +/* ---------------------------------------------------------------------- */
28832 +
28833 +static int au_rdu_verify(struct aufs_rdu *rdu)
28834 +{
28835 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28836 +             "%llu, b%d, 0x%x, g%u}\n",
28837 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28838 +             rdu->blk,
28839 +             rdu->rent, rdu->shwh, rdu->full,
28840 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28841 +             rdu->cookie.generation);
28842 +
28843 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28844 +               return 0;
28845 +
28846 +       AuDbg("%u:%u\n",
28847 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28848 +       return -EINVAL;
28849 +}
28850 +
28851 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28852 +{
28853 +       long err, e;
28854 +       struct aufs_rdu rdu;
28855 +       void __user *p = (void __user *)arg;
28856 +
28857 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28858 +       if (unlikely(err)) {
28859 +               err = -EFAULT;
28860 +               AuTraceErr(err);
28861 +               goto out;
28862 +       }
28863 +       err = au_rdu_verify(&rdu);
28864 +       if (unlikely(err))
28865 +               goto out;
28866 +
28867 +       switch (cmd) {
28868 +       case AUFS_CTL_RDU:
28869 +               err = au_rdu(file, &rdu);
28870 +               if (unlikely(err))
28871 +                       break;
28872 +
28873 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28874 +               if (unlikely(e)) {
28875 +                       err = -EFAULT;
28876 +                       AuTraceErr(err);
28877 +               }
28878 +               break;
28879 +       case AUFS_CTL_RDU_INO:
28880 +               err = au_rdu_ino(file, &rdu);
28881 +               break;
28882 +
28883 +       default:
28884 +               /* err = -ENOTTY; */
28885 +               err = -EINVAL;
28886 +       }
28887 +
28888 +out:
28889 +       AuTraceErr(err);
28890 +       return err;
28891 +}
28892 +
28893 +#ifdef CONFIG_COMPAT
28894 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28895 +{
28896 +       long err, e;
28897 +       struct aufs_rdu rdu;
28898 +       void __user *p = compat_ptr(arg);
28899 +
28900 +       /* todo: get_user()? */
28901 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28902 +       if (unlikely(err)) {
28903 +               err = -EFAULT;
28904 +               AuTraceErr(err);
28905 +               goto out;
28906 +       }
28907 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28908 +       err = au_rdu_verify(&rdu);
28909 +       if (unlikely(err))
28910 +               goto out;
28911 +
28912 +       switch (cmd) {
28913 +       case AUFS_CTL_RDU:
28914 +               err = au_rdu(file, &rdu);
28915 +               if (unlikely(err))
28916 +                       break;
28917 +
28918 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28919 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28920 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28921 +               if (unlikely(e)) {
28922 +                       err = -EFAULT;
28923 +                       AuTraceErr(err);
28924 +               }
28925 +               break;
28926 +       case AUFS_CTL_RDU_INO:
28927 +               err = au_rdu_ino(file, &rdu);
28928 +               break;
28929 +
28930 +       default:
28931 +               /* err = -ENOTTY; */
28932 +               err = -EINVAL;
28933 +       }
28934 +
28935 +out:
28936 +       AuTraceErr(err);
28937 +       return err;
28938 +}
28939 +#endif
28940 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28941 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28942 +++ linux/fs/aufs/rwsem.h       2021-12-03 15:40:58.236647297 +0100
28943 @@ -0,0 +1,85 @@
28944 +/* SPDX-License-Identifier: GPL-2.0 */
28945 +/*
28946 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28947 + *
28948 + * This program, aufs is free software; you can redistribute it and/or modify
28949 + * it under the terms of the GNU General Public License as published by
28950 + * the Free Software Foundation; either version 2 of the License, or
28951 + * (at your option) any later version.
28952 + *
28953 + * This program is distributed in the hope that it will be useful,
28954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28956 + * GNU General Public License for more details.
28957 + *
28958 + * You should have received a copy of the GNU General Public License
28959 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28960 + */
28961 +
28962 +/*
28963 + * simple read-write semaphore wrappers
28964 + */
28965 +
28966 +#ifndef __AUFS_RWSEM_H__
28967 +#define __AUFS_RWSEM_H__
28968 +
28969 +#ifdef __KERNEL__
28970 +
28971 +#include "debug.h"
28972 +
28973 +/* in the future, the name 'au_rwsem' will be totally gone */
28974 +#define au_rwsem       rw_semaphore
28975 +
28976 +/* to debug easier, do not make them inlined functions */
28977 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
28978 +
28979 +#ifdef CONFIG_LOCKDEP
28980 +/* rwsem_is_locked() is unusable */
28981 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28982 +                                         && !lockdep_recursing(current) \
28983 +                                         && debug_locks                \
28984 +                                         && !lockdep_is_held_type(rw, 1))
28985 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28986 +                                         && !lockdep_recursing(current) \
28987 +                                         && debug_locks                \
28988 +                                         && !lockdep_is_held_type(rw, 0))
28989 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28990 +                                         && !lockdep_recursing(current) \
28991 +                                         && debug_locks                \
28992 +                                         && !lockdep_is_held(rw))
28993 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28994 +                                         && !lockdep_recursing(current) \
28995 +                                         && debug_locks                \
28996 +                                         && lockdep_is_held(rw))
28997 +#else
28998 +#define AuRwMustReadLock(rw)   do {} while (0)
28999 +#define AuRwMustWriteLock(rw)  do {} while (0)
29000 +#define AuRwMustAnyLock(rw)    do {} while (0)
29001 +#define AuRwDestroy(rw)                do {} while (0)
29002 +#endif
29003 +
29004 +#define au_rw_init(rw) init_rwsem(rw)
29005 +
29006 +#define au_rw_init_wlock(rw) do {              \
29007 +               au_rw_init(rw);                 \
29008 +               down_write(rw);                 \
29009 +       } while (0)
29010 +
29011 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29012 +               au_rw_init(rw);                 \
29013 +               down_write_nested(rw, lsc);     \
29014 +       } while (0)
29015 +
29016 +#define au_rw_read_lock(rw)            down_read(rw)
29017 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29018 +#define au_rw_read_unlock(rw)          up_read(rw)
29019 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29020 +#define au_rw_write_lock(rw)           down_write(rw)
29021 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29022 +#define au_rw_write_unlock(rw)         up_write(rw)
29023 +/* why is not _nested version defined? */
29024 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29025 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29026 +
29027 +#endif /* __KERNEL__ */
29028 +#endif /* __AUFS_RWSEM_H__ */
29029 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29030 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29031 +++ linux/fs/aufs/sbinfo.c      2021-12-03 15:38:59.939980643 +0100
29032 @@ -0,0 +1,314 @@
29033 +// SPDX-License-Identifier: GPL-2.0
29034 +/*
29035 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29036 + *
29037 + * This program, aufs is free software; you can redistribute it and/or modify
29038 + * it under the terms of the GNU General Public License as published by
29039 + * the Free Software Foundation; either version 2 of the License, or
29040 + * (at your option) any later version.
29041 + *
29042 + * This program is distributed in the hope that it will be useful,
29043 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29044 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29045 + * GNU General Public License for more details.
29046 + *
29047 + * You should have received a copy of the GNU General Public License
29048 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29049 + */
29050 +
29051 +/*
29052 + * superblock private data
29053 + */
29054 +
29055 +#include <linux/iversion.h>
29056 +#include "aufs.h"
29057 +
29058 +/*
29059 + * they are necessary regardless sysfs is disabled.
29060 + */
29061 +void au_si_free(struct kobject *kobj)
29062 +{
29063 +       int i;
29064 +       struct au_sbinfo *sbinfo;
29065 +       char *locked __maybe_unused; /* debug only */
29066 +
29067 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29068 +       for (i = 0; i < AuPlink_NHASH; i++)
29069 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29070 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29071 +
29072 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29073 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29074 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29075 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29076 +
29077 +       dbgaufs_si_fin(sbinfo);
29078 +       au_rw_write_lock(&sbinfo->si_rwsem);
29079 +       au_br_free(sbinfo);
29080 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29081 +
29082 +       au_kfree_try_rcu(sbinfo->si_branch);
29083 +       mutex_destroy(&sbinfo->si_xib_mtx);
29084 +       AuRwDestroy(&sbinfo->si_rwsem);
29085 +
29086 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29087 +       /* si_nfiles is waited too */
29088 +       au_kfree_rcu(sbinfo);
29089 +}
29090 +
29091 +int au_si_alloc(struct super_block *sb)
29092 +{
29093 +       int err, i;
29094 +       struct au_sbinfo *sbinfo;
29095 +
29096 +       err = -ENOMEM;
29097 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29098 +       if (unlikely(!sbinfo))
29099 +               goto out;
29100 +
29101 +       /* will be reallocated separately */
29102 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29103 +       if (unlikely(!sbinfo->si_branch))
29104 +               goto out_sbinfo;
29105 +
29106 +       err = sysaufs_si_init(sbinfo);
29107 +       if (!err) {
29108 +               dbgaufs_si_null(sbinfo);
29109 +               err = dbgaufs_si_init(sbinfo);
29110 +               if (unlikely(err))
29111 +                       kobject_put(&sbinfo->si_kobj);
29112 +       }
29113 +       if (unlikely(err))
29114 +               goto out_br;
29115 +
29116 +       au_nwt_init(&sbinfo->si_nowait);
29117 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29118 +
29119 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29120 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29121 +
29122 +       sbinfo->si_bbot = -1;
29123 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29124 +
29125 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29126 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29127 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29128 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29129 +
29130 +       au_fhsm_init(sbinfo);
29131 +
29132 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29133 +
29134 +       sbinfo->si_xino_jiffy = jiffies;
29135 +       sbinfo->si_xino_expire
29136 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29137 +       mutex_init(&sbinfo->si_xib_mtx);
29138 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29139 +
29140 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29141 +
29142 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29143 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29144 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29145 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29146 +
29147 +       for (i = 0; i < AuPlink_NHASH; i++)
29148 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29149 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29150 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29151 +
29152 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29153 +
29154 +       /* with getattr by default */
29155 +       sbinfo->si_iop_array = aufs_iop;
29156 +
29157 +       /* leave other members for sysaufs and si_mnt. */
29158 +       sbinfo->si_sb = sb;
29159 +       sb->s_fs_info = sbinfo;
29160 +       si_pid_set(sb);
29161 +       return 0; /* success */
29162 +
29163 +out_br:
29164 +       au_kfree_try_rcu(sbinfo->si_branch);
29165 +out_sbinfo:
29166 +       au_kfree_rcu(sbinfo);
29167 +out:
29168 +       return err;
29169 +}
29170 +
29171 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29172 +{
29173 +       int err, sz;
29174 +       struct au_branch **brp;
29175 +
29176 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29177 +
29178 +       err = -ENOMEM;
29179 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29180 +       if (unlikely(!sz))
29181 +               sz = sizeof(*brp);
29182 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29183 +                          may_shrink);
29184 +       if (brp) {
29185 +               sbinfo->si_branch = brp;
29186 +               err = 0;
29187 +       }
29188 +
29189 +       return err;
29190 +}
29191 +
29192 +/* ---------------------------------------------------------------------- */
29193 +
29194 +unsigned int au_sigen_inc(struct super_block *sb)
29195 +{
29196 +       unsigned int gen;
29197 +       struct inode *inode;
29198 +
29199 +       SiMustWriteLock(sb);
29200 +
29201 +       gen = ++au_sbi(sb)->si_generation;
29202 +       au_update_digen(sb->s_root);
29203 +       inode = d_inode(sb->s_root);
29204 +       au_update_iigen(inode, /*half*/0);
29205 +       inode_inc_iversion(inode);
29206 +       return gen;
29207 +}
29208 +
29209 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29210 +{
29211 +       aufs_bindex_t br_id;
29212 +       int i;
29213 +       struct au_sbinfo *sbinfo;
29214 +
29215 +       SiMustWriteLock(sb);
29216 +
29217 +       sbinfo = au_sbi(sb);
29218 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29219 +               br_id = ++sbinfo->si_last_br_id;
29220 +               AuDebugOn(br_id < 0);
29221 +               if (br_id && au_br_index(sb, br_id) < 0)
29222 +                       return br_id;
29223 +       }
29224 +
29225 +       return -1;
29226 +}
29227 +
29228 +/* ---------------------------------------------------------------------- */
29229 +
29230 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29231 +int si_read_lock(struct super_block *sb, int flags)
29232 +{
29233 +       int err;
29234 +
29235 +       err = 0;
29236 +       if (au_ftest_lock(flags, FLUSH))
29237 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29238 +
29239 +       si_noflush_read_lock(sb);
29240 +       err = au_plink_maint(sb, flags);
29241 +       if (unlikely(err))
29242 +               si_read_unlock(sb);
29243 +
29244 +       return err;
29245 +}
29246 +
29247 +int si_write_lock(struct super_block *sb, int flags)
29248 +{
29249 +       int err;
29250 +
29251 +       if (au_ftest_lock(flags, FLUSH))
29252 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29253 +
29254 +       si_noflush_write_lock(sb);
29255 +       err = au_plink_maint(sb, flags);
29256 +       if (unlikely(err))
29257 +               si_write_unlock(sb);
29258 +
29259 +       return err;
29260 +}
29261 +
29262 +/* dentry and super_block lock. call at entry point */
29263 +int aufs_read_lock(struct dentry *dentry, int flags)
29264 +{
29265 +       int err;
29266 +       struct super_block *sb;
29267 +
29268 +       sb = dentry->d_sb;
29269 +       err = si_read_lock(sb, flags);
29270 +       if (unlikely(err))
29271 +               goto out;
29272 +
29273 +       if (au_ftest_lock(flags, DW))
29274 +               di_write_lock_child(dentry);
29275 +       else
29276 +               di_read_lock_child(dentry, flags);
29277 +
29278 +       if (au_ftest_lock(flags, GEN)) {
29279 +               err = au_digen_test(dentry, au_sigen(sb));
29280 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29281 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29282 +               else if (!err)
29283 +                       err = au_dbrange_test(dentry);
29284 +               if (unlikely(err))
29285 +                       aufs_read_unlock(dentry, flags);
29286 +       }
29287 +
29288 +out:
29289 +       return err;
29290 +}
29291 +
29292 +void aufs_read_unlock(struct dentry *dentry, int flags)
29293 +{
29294 +       if (au_ftest_lock(flags, DW))
29295 +               di_write_unlock(dentry);
29296 +       else
29297 +               di_read_unlock(dentry, flags);
29298 +       si_read_unlock(dentry->d_sb);
29299 +}
29300 +
29301 +void aufs_write_lock(struct dentry *dentry)
29302 +{
29303 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29304 +       di_write_lock_child(dentry);
29305 +}
29306 +
29307 +void aufs_write_unlock(struct dentry *dentry)
29308 +{
29309 +       di_write_unlock(dentry);
29310 +       si_write_unlock(dentry->d_sb);
29311 +}
29312 +
29313 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29314 +{
29315 +       int err;
29316 +       unsigned int sigen;
29317 +       struct super_block *sb;
29318 +
29319 +       sb = d1->d_sb;
29320 +       err = si_read_lock(sb, flags);
29321 +       if (unlikely(err))
29322 +               goto out;
29323 +
29324 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29325 +
29326 +       if (au_ftest_lock(flags, GEN)) {
29327 +               sigen = au_sigen(sb);
29328 +               err = au_digen_test(d1, sigen);
29329 +               AuDebugOn(!err && au_dbrange_test(d1));
29330 +               if (!err) {
29331 +                       err = au_digen_test(d2, sigen);
29332 +                       AuDebugOn(!err && au_dbrange_test(d2));
29333 +               }
29334 +               if (unlikely(err))
29335 +                       aufs_read_and_write_unlock2(d1, d2);
29336 +       }
29337 +
29338 +out:
29339 +       return err;
29340 +}
29341 +
29342 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29343 +{
29344 +       di_write_unlock2(d1, d2);
29345 +       si_read_unlock(d1->d_sb);
29346 +}
29347 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29348 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29349 +++ linux/fs/aufs/super.c       2022-01-13 12:42:15.932899468 +0100
29350 @@ -0,0 +1,1050 @@
29351 +// SPDX-License-Identifier: GPL-2.0
29352 +/*
29353 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29354 + *
29355 + * This program, aufs is free software; you can redistribute it and/or modify
29356 + * it under the terms of the GNU General Public License as published by
29357 + * the Free Software Foundation; either version 2 of the License, or
29358 + * (at your option) any later version.
29359 + *
29360 + * This program is distributed in the hope that it will be useful,
29361 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29362 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29363 + * GNU General Public License for more details.
29364 + *
29365 + * You should have received a copy of the GNU General Public License
29366 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29367 + */
29368 +
29369 +/*
29370 + * mount and super_block operations
29371 + */
29372 +
29373 +#include <linux/iversion.h>
29374 +#include <linux/mm.h>
29375 +#include <linux/seq_file.h>
29376 +#include <linux/statfs.h>
29377 +#include <linux/vmalloc.h>
29378 +#include "aufs.h"
29379 +
29380 +/*
29381 + * super_operations
29382 + */
29383 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29384 +{
29385 +       struct au_icntnr *c;
29386 +
29387 +       c = au_cache_alloc_icntnr();
29388 +       if (c) {
29389 +               au_icntnr_init(c);
29390 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29391 +               c->iinfo.ii_hinode = NULL;
29392 +               return &c->vfs_inode;
29393 +       }
29394 +       return NULL;
29395 +}
29396 +
29397 +static void aufs_destroy_inode(struct inode *inode)
29398 +{
29399 +       if (!au_is_bad_inode(inode))
29400 +               au_iinfo_fin(inode);
29401 +}
29402 +
29403 +static void aufs_free_inode(struct inode *inode)
29404 +{
29405 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29406 +}
29407 +
29408 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29409 +{
29410 +       struct inode *inode;
29411 +       int err;
29412 +
29413 +       inode = iget_locked(sb, ino);
29414 +       if (unlikely(!inode)) {
29415 +               inode = ERR_PTR(-ENOMEM);
29416 +               goto out;
29417 +       }
29418 +       if (!(inode->i_state & I_NEW))
29419 +               goto out;
29420 +
29421 +       err = au_xigen_new(inode);
29422 +       if (!err)
29423 +               err = au_iinfo_init(inode);
29424 +       if (!err)
29425 +               inode_inc_iversion(inode);
29426 +       else {
29427 +               iget_failed(inode);
29428 +               inode = ERR_PTR(err);
29429 +       }
29430 +
29431 +out:
29432 +       /* never return NULL */
29433 +       AuDebugOn(!inode);
29434 +       AuTraceErrPtr(inode);
29435 +       return inode;
29436 +}
29437 +
29438 +/* lock free root dinfo */
29439 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29440 +{
29441 +       int err;
29442 +       aufs_bindex_t bindex, bbot;
29443 +       struct path path;
29444 +       struct au_hdentry *hdp;
29445 +       struct au_branch *br;
29446 +       au_br_perm_str_t perm;
29447 +
29448 +       err = 0;
29449 +       bbot = au_sbbot(sb);
29450 +       bindex = 0;
29451 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29452 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29453 +               br = au_sbr(sb, bindex);
29454 +               path.mnt = au_br_mnt(br);
29455 +               path.dentry = hdp->hd_dentry;
29456 +               err = au_seq_path(seq, &path);
29457 +               if (!err) {
29458 +                       au_optstr_br_perm(&perm, br->br_perm);
29459 +                       seq_printf(seq, "=%s", perm.a);
29460 +                       if (bindex != bbot)
29461 +                               seq_putc(seq, ':');
29462 +               }
29463 +       }
29464 +       if (unlikely(err || seq_has_overflowed(seq)))
29465 +               err = -E2BIG;
29466 +
29467 +       return err;
29468 +}
29469 +
29470 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29471 +                      const char *append)
29472 +{
29473 +       char *p;
29474 +
29475 +       p = fmt;
29476 +       while (*pat != ':')
29477 +               *p++ = *pat++;
29478 +       *p++ = *pat++;
29479 +       strcpy(p, append);
29480 +       AuDebugOn(strlen(fmt) >= len);
29481 +}
29482 +
29483 +static void au_show_wbr_create(struct seq_file *m, int v,
29484 +                              struct au_sbinfo *sbinfo)
29485 +{
29486 +       const char *pat;
29487 +       char fmt[32];
29488 +       struct au_wbr_mfs *mfs;
29489 +
29490 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29491 +
29492 +       seq_puts(m, ",create=");
29493 +       pat = au_optstr_wbr_create(v);
29494 +       mfs = &sbinfo->si_wbr_mfs;
29495 +       switch (v) {
29496 +       case AuWbrCreate_TDP:
29497 +       case AuWbrCreate_RR:
29498 +       case AuWbrCreate_MFS:
29499 +       case AuWbrCreate_PMFS:
29500 +               seq_puts(m, pat);
29501 +               break;
29502 +       case AuWbrCreate_MFSRR:
29503 +       case AuWbrCreate_TDMFS:
29504 +       case AuWbrCreate_PMFSRR:
29505 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29506 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29507 +               break;
29508 +       case AuWbrCreate_MFSV:
29509 +       case AuWbrCreate_PMFSV:
29510 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29511 +               seq_printf(m, fmt,
29512 +                          jiffies_to_msecs(mfs->mfs_expire)
29513 +                          / MSEC_PER_SEC);
29514 +               break;
29515 +       case AuWbrCreate_MFSRRV:
29516 +       case AuWbrCreate_TDMFSV:
29517 +       case AuWbrCreate_PMFSRRV:
29518 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29519 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29520 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29521 +               break;
29522 +       default:
29523 +               BUG();
29524 +       }
29525 +}
29526 +
29527 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29528 +{
29529 +#ifdef CONFIG_SYSFS
29530 +       return 0;
29531 +#else
29532 +       int err;
29533 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29534 +       aufs_bindex_t bindex, brid;
29535 +       struct qstr *name;
29536 +       struct file *f;
29537 +       struct dentry *d, *h_root;
29538 +       struct au_branch *br;
29539 +
29540 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29541 +
29542 +       err = 0;
29543 +       f = au_sbi(sb)->si_xib;
29544 +       if (!f)
29545 +               goto out;
29546 +
29547 +       /* stop printing the default xino path on the first writable branch */
29548 +       h_root = NULL;
29549 +       bindex = au_xi_root(sb, f->f_path.dentry);
29550 +       if (bindex >= 0) {
29551 +               br = au_sbr_sb(sb, bindex);
29552 +               h_root = au_br_dentry(br);
29553 +       }
29554 +
29555 +       d = f->f_path.dentry;
29556 +       name = &d->d_name;
29557 +       /* safe ->d_parent because the file is unlinked */
29558 +       if (d->d_parent == h_root
29559 +           && name->len == len
29560 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29561 +               goto out;
29562 +
29563 +       seq_puts(seq, ",xino=");
29564 +       err = au_xino_path(seq, f);
29565 +
29566 +out:
29567 +       return err;
29568 +#endif
29569 +}
29570 +
29571 +/* seq_file will re-call me in case of too long string */
29572 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29573 +{
29574 +       int err;
29575 +       unsigned int mnt_flags, v;
29576 +       struct super_block *sb;
29577 +       struct au_sbinfo *sbinfo;
29578 +
29579 +#define AuBool(name, str) do { \
29580 +       v = au_opt_test(mnt_flags, name); \
29581 +       if (v != au_opt_test(AuOpt_Def, name)) \
29582 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29583 +} while (0)
29584 +
29585 +#define AuStr(name, str) do { \
29586 +       v = mnt_flags & AuOptMask_##name; \
29587 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29588 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29589 +} while (0)
29590 +
29591 +#define AuUInt(name, str, val) do { \
29592 +       if (val != AUFS_##name##_DEF) \
29593 +               seq_printf(m, "," #str "=%u", val); \
29594 +} while (0)
29595 +
29596 +       sb = dentry->d_sb;
29597 +       if (sb->s_flags & SB_POSIXACL)
29598 +               seq_puts(m, ",acl");
29599 +#if 0 /* reserved for future use */
29600 +       if (sb->s_flags & SB_I_VERSION)
29601 +               seq_puts(m, ",i_version");
29602 +#endif
29603 +
29604 +       /* lock free root dinfo */
29605 +       si_noflush_read_lock(sb);
29606 +       sbinfo = au_sbi(sb);
29607 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29608 +
29609 +       mnt_flags = au_mntflags(sb);
29610 +       if (au_opt_test(mnt_flags, XINO)) {
29611 +               err = au_show_xino(m, sb);
29612 +               if (unlikely(err))
29613 +                       goto out;
29614 +       } else
29615 +               seq_puts(m, ",noxino");
29616 +
29617 +       AuBool(TRUNC_XINO, trunc_xino);
29618 +       AuStr(UDBA, udba);
29619 +       AuBool(SHWH, shwh);
29620 +       AuBool(PLINK, plink);
29621 +       AuBool(DIO, dio);
29622 +       AuBool(DIRPERM1, dirperm1);
29623 +
29624 +       v = sbinfo->si_wbr_create;
29625 +       if (v != AuWbrCreate_Def)
29626 +               au_show_wbr_create(m, v, sbinfo);
29627 +
29628 +       v = sbinfo->si_wbr_copyup;
29629 +       if (v != AuWbrCopyup_Def)
29630 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29631 +
29632 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29633 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29634 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29635 +
29636 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29637 +
29638 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29639 +       AuUInt(RDCACHE, rdcache, v);
29640 +
29641 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29642 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29643 +
29644 +       au_fhsm_show(m, sbinfo);
29645 +
29646 +       AuBool(DIRREN, dirren);
29647 +       AuBool(SUM, sum);
29648 +       /* AuBool(SUM_W, wsum); */
29649 +       AuBool(WARN_PERM, warn_perm);
29650 +       AuBool(VERBOSE, verbose);
29651 +
29652 +out:
29653 +       /* be sure to print "br:" last */
29654 +       if (!sysaufs_brs) {
29655 +               seq_puts(m, ",br:");
29656 +               au_show_brs(m, sb);
29657 +       }
29658 +       si_read_unlock(sb);
29659 +       return 0;
29660 +
29661 +#undef AuBool
29662 +#undef AuStr
29663 +#undef AuUInt
29664 +}
29665 +
29666 +/* ---------------------------------------------------------------------- */
29667 +
29668 +/* sum mode which returns the summation for statfs(2) */
29669 +
29670 +static u64 au_add_till_max(u64 a, u64 b)
29671 +{
29672 +       u64 old;
29673 +
29674 +       old = a;
29675 +       a += b;
29676 +       if (old <= a)
29677 +               return a;
29678 +       return ULLONG_MAX;
29679 +}
29680 +
29681 +static u64 au_mul_till_max(u64 a, long mul)
29682 +{
29683 +       u64 old;
29684 +
29685 +       old = a;
29686 +       a *= mul;
29687 +       if (old <= a)
29688 +               return a;
29689 +       return ULLONG_MAX;
29690 +}
29691 +
29692 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29693 +{
29694 +       int err;
29695 +       long bsize, factor;
29696 +       u64 blocks, bfree, bavail, files, ffree;
29697 +       aufs_bindex_t bbot, bindex, i;
29698 +       unsigned char shared;
29699 +       struct path h_path;
29700 +       struct super_block *h_sb;
29701 +
29702 +       err = 0;
29703 +       bsize = LONG_MAX;
29704 +       files = 0;
29705 +       ffree = 0;
29706 +       blocks = 0;
29707 +       bfree = 0;
29708 +       bavail = 0;
29709 +       bbot = au_sbbot(sb);
29710 +       for (bindex = 0; bindex <= bbot; bindex++) {
29711 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29712 +               h_sb = h_path.mnt->mnt_sb;
29713 +               shared = 0;
29714 +               for (i = 0; !shared && i < bindex; i++)
29715 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29716 +               if (shared)
29717 +                       continue;
29718 +
29719 +               /* sb->s_root for NFS is unreliable */
29720 +               h_path.dentry = h_path.mnt->mnt_root;
29721 +               err = vfs_statfs(&h_path, buf);
29722 +               if (unlikely(err))
29723 +                       goto out;
29724 +
29725 +               if (bsize > buf->f_bsize) {
29726 +                       /*
29727 +                        * we will reduce bsize, so we have to expand blocks
29728 +                        * etc. to match them again
29729 +                        */
29730 +                       factor = (bsize / buf->f_bsize);
29731 +                       blocks = au_mul_till_max(blocks, factor);
29732 +                       bfree = au_mul_till_max(bfree, factor);
29733 +                       bavail = au_mul_till_max(bavail, factor);
29734 +                       bsize = buf->f_bsize;
29735 +               }
29736 +
29737 +               factor = (buf->f_bsize / bsize);
29738 +               blocks = au_add_till_max(blocks,
29739 +                               au_mul_till_max(buf->f_blocks, factor));
29740 +               bfree = au_add_till_max(bfree,
29741 +                               au_mul_till_max(buf->f_bfree, factor));
29742 +               bavail = au_add_till_max(bavail,
29743 +                               au_mul_till_max(buf->f_bavail, factor));
29744 +               files = au_add_till_max(files, buf->f_files);
29745 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29746 +       }
29747 +
29748 +       buf->f_bsize = bsize;
29749 +       buf->f_blocks = blocks;
29750 +       buf->f_bfree = bfree;
29751 +       buf->f_bavail = bavail;
29752 +       buf->f_files = files;
29753 +       buf->f_ffree = ffree;
29754 +       buf->f_frsize = 0;
29755 +
29756 +out:
29757 +       return err;
29758 +}
29759 +
29760 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29761 +{
29762 +       int err;
29763 +       struct path h_path;
29764 +       struct super_block *sb;
29765 +
29766 +       /* lock free root dinfo */
29767 +       sb = dentry->d_sb;
29768 +       si_noflush_read_lock(sb);
29769 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29770 +               /* sb->s_root for NFS is unreliable */
29771 +               h_path.mnt = au_sbr_mnt(sb, 0);
29772 +               h_path.dentry = h_path.mnt->mnt_root;
29773 +               err = vfs_statfs(&h_path, buf);
29774 +       } else
29775 +               err = au_statfs_sum(sb, buf);
29776 +       si_read_unlock(sb);
29777 +
29778 +       if (!err) {
29779 +               buf->f_type = AUFS_SUPER_MAGIC;
29780 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29781 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29782 +       }
29783 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29784 +
29785 +       return err;
29786 +}
29787 +
29788 +/* ---------------------------------------------------------------------- */
29789 +
29790 +static int aufs_sync_fs(struct super_block *sb, int wait)
29791 +{
29792 +       int err, e;
29793 +       aufs_bindex_t bbot, bindex;
29794 +       struct au_branch *br;
29795 +       struct super_block *h_sb;
29796 +
29797 +       err = 0;
29798 +       si_noflush_read_lock(sb);
29799 +       bbot = au_sbbot(sb);
29800 +       for (bindex = 0; bindex <= bbot; bindex++) {
29801 +               br = au_sbr(sb, bindex);
29802 +               if (!au_br_writable(br->br_perm))
29803 +                       continue;
29804 +
29805 +               h_sb = au_sbr_sb(sb, bindex);
29806 +               e = vfsub_sync_filesystem(h_sb);
29807 +               if (unlikely(e && !err))
29808 +                       err = e;
29809 +               /* go on even if an error happens */
29810 +       }
29811 +       si_read_unlock(sb);
29812 +
29813 +       return err;
29814 +}
29815 +
29816 +/* ---------------------------------------------------------------------- */
29817 +
29818 +/* final actions when unmounting a file system */
29819 +static void aufs_put_super(struct super_block *sb)
29820 +{
29821 +       struct au_sbinfo *sbinfo;
29822 +
29823 +       sbinfo = au_sbi(sb);
29824 +       if (sbinfo)
29825 +               kobject_put(&sbinfo->si_kobj);
29826 +}
29827 +
29828 +/* ---------------------------------------------------------------------- */
29829 +
29830 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29831 +                    struct super_block *sb, void *arg)
29832 +{
29833 +       void *array;
29834 +       unsigned long long n, sz;
29835 +
29836 +       array = NULL;
29837 +       n = 0;
29838 +       if (!*hint)
29839 +               goto out;
29840 +
29841 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29842 +               array = ERR_PTR(-EMFILE);
29843 +               pr_err("hint %llu\n", *hint);
29844 +               goto out;
29845 +       }
29846 +
29847 +       sz = sizeof(array) * *hint;
29848 +       array = kzalloc(sz, GFP_NOFS);
29849 +       if (unlikely(!array))
29850 +               array = vzalloc(sz);
29851 +       if (unlikely(!array)) {
29852 +               array = ERR_PTR(-ENOMEM);
29853 +               goto out;
29854 +       }
29855 +
29856 +       n = cb(sb, array, *hint, arg);
29857 +       AuDebugOn(n > *hint);
29858 +
29859 +out:
29860 +       *hint = n;
29861 +       return array;
29862 +}
29863 +
29864 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29865 +                                      unsigned long long max __maybe_unused,
29866 +                                      void *arg)
29867 +{
29868 +       unsigned long long n;
29869 +       struct inode **p, *inode;
29870 +       struct list_head *head;
29871 +
29872 +       n = 0;
29873 +       p = a;
29874 +       head = arg;
29875 +       spin_lock(&sb->s_inode_list_lock);
29876 +       list_for_each_entry(inode, head, i_sb_list) {
29877 +               if (!au_is_bad_inode(inode)
29878 +                   && au_ii(inode)->ii_btop >= 0) {
29879 +                       spin_lock(&inode->i_lock);
29880 +                       if (atomic_read(&inode->i_count)) {
29881 +                               au_igrab(inode);
29882 +                               *p++ = inode;
29883 +                               n++;
29884 +                               AuDebugOn(n > max);
29885 +                       }
29886 +                       spin_unlock(&inode->i_lock);
29887 +               }
29888 +       }
29889 +       spin_unlock(&sb->s_inode_list_lock);
29890 +
29891 +       return n;
29892 +}
29893 +
29894 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29895 +{
29896 +       struct au_sbinfo *sbi;
29897 +
29898 +       sbi = au_sbi(sb);
29899 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29900 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29901 +}
29902 +
29903 +void au_iarray_free(struct inode **a, unsigned long long max)
29904 +{
29905 +       unsigned long long ull;
29906 +
29907 +       for (ull = 0; ull < max; ull++)
29908 +               iput(a[ull]);
29909 +       kvfree(a);
29910 +}
29911 +
29912 +/* ---------------------------------------------------------------------- */
29913 +
29914 +/*
29915 + * refresh dentry and inode at remount time.
29916 + */
29917 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29918 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29919 +                     struct dentry *parent)
29920 +{
29921 +       int err;
29922 +
29923 +       di_write_lock_child(dentry);
29924 +       di_read_lock_parent(parent, AuLock_IR);
29925 +       err = au_refresh_dentry(dentry, parent);
29926 +       if (!err && dir_flags)
29927 +               au_hn_reset(d_inode(dentry), dir_flags);
29928 +       di_read_unlock(parent, AuLock_IR);
29929 +       di_write_unlock(dentry);
29930 +
29931 +       return err;
29932 +}
29933 +
29934 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29935 +                          struct au_sbinfo *sbinfo,
29936 +                          const unsigned int dir_flags, unsigned int do_idop)
29937 +{
29938 +       int err;
29939 +       struct dentry *parent;
29940 +
29941 +       err = 0;
29942 +       parent = dget_parent(dentry);
29943 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29944 +               if (d_really_is_positive(dentry)) {
29945 +                       if (!d_is_dir(dentry))
29946 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29947 +                                                parent);
29948 +                       else {
29949 +                               err = au_do_refresh(dentry, dir_flags, parent);
29950 +                               if (unlikely(err))
29951 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29952 +                       }
29953 +               } else
29954 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29955 +               AuDbgDentry(dentry);
29956 +       }
29957 +       dput(parent);
29958 +
29959 +       if (!err) {
29960 +               if (do_idop)
29961 +                       au_refresh_dop(dentry, /*force_reval*/0);
29962 +       } else
29963 +               au_refresh_dop(dentry, /*force_reval*/1);
29964 +
29965 +       AuTraceErr(err);
29966 +       return err;
29967 +}
29968 +
29969 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29970 +{
29971 +       int err, i, j, ndentry, e;
29972 +       unsigned int sigen;
29973 +       struct au_dcsub_pages dpages;
29974 +       struct au_dpage *dpage;
29975 +       struct dentry **dentries, *d;
29976 +       struct au_sbinfo *sbinfo;
29977 +       struct dentry *root = sb->s_root;
29978 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29979 +
29980 +       if (do_idop)
29981 +               au_refresh_dop(root, /*force_reval*/0);
29982 +
29983 +       err = au_dpages_init(&dpages, GFP_NOFS);
29984 +       if (unlikely(err))
29985 +               goto out;
29986 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
29987 +       if (unlikely(err))
29988 +               goto out_dpages;
29989 +
29990 +       sigen = au_sigen(sb);
29991 +       sbinfo = au_sbi(sb);
29992 +       for (i = 0; i < dpages.ndpage; i++) {
29993 +               dpage = dpages.dpages + i;
29994 +               dentries = dpage->dentries;
29995 +               ndentry = dpage->ndentry;
29996 +               for (j = 0; j < ndentry; j++) {
29997 +                       d = dentries[j];
29998 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
29999 +                                           do_idop);
30000 +                       if (unlikely(e && !err))
30001 +                               err = e;
30002 +                       /* go on even err */
30003 +               }
30004 +       }
30005 +
30006 +out_dpages:
30007 +       au_dpages_free(&dpages);
30008 +out:
30009 +       return err;
30010 +}
30011 +
30012 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30013 +{
30014 +       int err, e;
30015 +       unsigned int sigen;
30016 +       unsigned long long max, ull;
30017 +       struct inode *inode, **array;
30018 +
30019 +       array = au_iarray_alloc(sb, &max);
30020 +       err = PTR_ERR(array);
30021 +       if (IS_ERR(array))
30022 +               goto out;
30023 +
30024 +       err = 0;
30025 +       sigen = au_sigen(sb);
30026 +       for (ull = 0; ull < max; ull++) {
30027 +               inode = array[ull];
30028 +               if (unlikely(!inode))
30029 +                       break;
30030 +
30031 +               e = 0;
30032 +               ii_write_lock_child(inode);
30033 +               if (au_iigen(inode, NULL) != sigen) {
30034 +                       e = au_refresh_hinode_self(inode);
30035 +                       if (unlikely(e)) {
30036 +                               au_refresh_iop(inode, /*force_getattr*/1);
30037 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30038 +                               if (!err)
30039 +                                       err = e;
30040 +                               /* go on even if err */
30041 +                       }
30042 +               }
30043 +               if (!e && do_idop)
30044 +                       au_refresh_iop(inode, /*force_getattr*/0);
30045 +               ii_write_unlock(inode);
30046 +       }
30047 +
30048 +       au_iarray_free(array, max);
30049 +
30050 +out:
30051 +       return err;
30052 +}
30053 +
30054 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30055 +{
30056 +       int err, e;
30057 +       unsigned int udba;
30058 +       aufs_bindex_t bindex, bbot;
30059 +       struct dentry *root;
30060 +       struct inode *inode;
30061 +       struct au_branch *br;
30062 +       struct au_sbinfo *sbi;
30063 +
30064 +       au_sigen_inc(sb);
30065 +       sbi = au_sbi(sb);
30066 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30067 +
30068 +       root = sb->s_root;
30069 +       DiMustNoWaiters(root);
30070 +       inode = d_inode(root);
30071 +       IiMustNoWaiters(inode);
30072 +
30073 +       udba = au_opt_udba(sb);
30074 +       bbot = au_sbbot(sb);
30075 +       for (bindex = 0; bindex <= bbot; bindex++) {
30076 +               br = au_sbr(sb, bindex);
30077 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30078 +               if (unlikely(err))
30079 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30080 +                               bindex, err);
30081 +               /* go on even if err */
30082 +       }
30083 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30084 +
30085 +       if (do_idop) {
30086 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30087 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30088 +                       sb->s_d_op = &aufs_dop_noreval;
30089 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30090 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30091 +               } else {
30092 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30093 +                       sb->s_d_op = &aufs_dop;
30094 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30095 +                       sbi->si_iop_array = aufs_iop;
30096 +               }
30097 +               pr_info("reset to %ps and %ps\n",
30098 +                       sb->s_d_op, sbi->si_iop_array);
30099 +       }
30100 +
30101 +       di_write_unlock(root);
30102 +       err = au_refresh_d(sb, do_idop);
30103 +       e = au_refresh_i(sb, do_idop);
30104 +       if (unlikely(e && !err))
30105 +               err = e;
30106 +       /* aufs_write_lock() calls ..._child() */
30107 +       di_write_lock_child(root);
30108 +
30109 +       au_cpup_attr_all(inode, /*force*/1);
30110 +
30111 +       if (unlikely(err))
30112 +               AuIOErr("refresh failed, ignored, %d\n", err);
30113 +}
30114 +
30115 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30116 +static int cvt_err(int err)
30117 +{
30118 +       AuTraceErr(err);
30119 +
30120 +       switch (err) {
30121 +       case -ENOENT:
30122 +       case -ENOTDIR:
30123 +       case -EEXIST:
30124 +       case -EIO:
30125 +               err = -EINVAL;
30126 +       }
30127 +       return err;
30128 +}
30129 +
30130 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30131 +{
30132 +       int err, do_dx;
30133 +       unsigned int mntflags;
30134 +       struct au_opts opts = {
30135 +               .opt = NULL
30136 +       };
30137 +       struct dentry *root;
30138 +       struct inode *inode;
30139 +       struct au_sbinfo *sbinfo;
30140 +
30141 +       err = 0;
30142 +       root = sb->s_root;
30143 +       if (!data || !*data) {
30144 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30145 +               if (!err) {
30146 +                       di_write_lock_child(root);
30147 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30148 +                       aufs_write_unlock(root);
30149 +               }
30150 +               goto out;
30151 +       }
30152 +
30153 +       err = -ENOMEM;
30154 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30155 +       if (unlikely(!opts.opt))
30156 +               goto out;
30157 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30158 +       opts.flags = AuOpts_REMOUNT;
30159 +       opts.sb_flags = *flags;
30160 +
30161 +       /* parse it before aufs lock */
30162 +       err = au_opts_parse(sb, data, &opts);
30163 +       if (unlikely(err))
30164 +               goto out_opts;
30165 +
30166 +       sbinfo = au_sbi(sb);
30167 +       inode = d_inode(root);
30168 +       inode_lock(inode);
30169 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30170 +       if (unlikely(err))
30171 +               goto out_mtx;
30172 +       di_write_lock_child(root);
30173 +
30174 +       /* au_opts_remount() may return an error */
30175 +       err = au_opts_remount(sb, &opts);
30176 +       au_opts_free(&opts);
30177 +
30178 +       if (au_ftest_opts(opts.flags, REFRESH))
30179 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30180 +
30181 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30182 +               mntflags = au_mntflags(sb);
30183 +               do_dx = !!au_opt_test(mntflags, DIO);
30184 +               au_dy_arefresh(do_dx);
30185 +       }
30186 +
30187 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30188 +       aufs_write_unlock(root);
30189 +
30190 +out_mtx:
30191 +       inode_unlock(inode);
30192 +out_opts:
30193 +       free_page((unsigned long)opts.opt);
30194 +out:
30195 +       err = cvt_err(err);
30196 +       AuTraceErr(err);
30197 +       return err;
30198 +}
30199 +
30200 +static const struct super_operations aufs_sop = {
30201 +       .alloc_inode    = aufs_alloc_inode,
30202 +       .destroy_inode  = aufs_destroy_inode,
30203 +       .free_inode     = aufs_free_inode,
30204 +       /* always deleting, no clearing */
30205 +       .drop_inode     = generic_delete_inode,
30206 +       .show_options   = aufs_show_options,
30207 +       .statfs         = aufs_statfs,
30208 +       .put_super      = aufs_put_super,
30209 +       .sync_fs        = aufs_sync_fs,
30210 +       .remount_fs     = aufs_remount_fs
30211 +};
30212 +
30213 +/* ---------------------------------------------------------------------- */
30214 +
30215 +static int alloc_root(struct super_block *sb)
30216 +{
30217 +       int err;
30218 +       struct inode *inode;
30219 +       struct dentry *root;
30220 +
30221 +       err = -ENOMEM;
30222 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30223 +       err = PTR_ERR(inode);
30224 +       if (IS_ERR(inode))
30225 +               goto out;
30226 +
30227 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30228 +       inode->i_fop = &aufs_dir_fop;
30229 +       inode->i_mode = S_IFDIR;
30230 +       set_nlink(inode, 2);
30231 +       unlock_new_inode(inode);
30232 +
30233 +       root = d_make_root(inode);
30234 +       if (unlikely(!root))
30235 +               goto out;
30236 +       err = PTR_ERR(root);
30237 +       if (IS_ERR(root))
30238 +               goto out;
30239 +
30240 +       err = au_di_init(root);
30241 +       if (!err) {
30242 +               sb->s_root = root;
30243 +               return 0; /* success */
30244 +       }
30245 +       dput(root);
30246 +
30247 +out:
30248 +       return err;
30249 +}
30250 +
30251 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30252 +                          int silent __maybe_unused)
30253 +{
30254 +       int err;
30255 +       struct au_opts opts = {
30256 +               .opt = NULL
30257 +       };
30258 +       struct au_sbinfo *sbinfo;
30259 +       struct dentry *root;
30260 +       struct inode *inode;
30261 +       char *arg = raw_data;
30262 +
30263 +       if (unlikely(!arg || !*arg)) {
30264 +               err = -EINVAL;
30265 +               pr_err("no arg\n");
30266 +               goto out;
30267 +       }
30268 +
30269 +       err = -ENOMEM;
30270 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30271 +       if (unlikely(!opts.opt))
30272 +               goto out;
30273 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30274 +       opts.sb_flags = sb->s_flags;
30275 +
30276 +       err = au_si_alloc(sb);
30277 +       if (unlikely(err))
30278 +               goto out_opts;
30279 +       sbinfo = au_sbi(sb);
30280 +
30281 +       /* all timestamps always follow the ones on the branch */
30282 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30283 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30284 +       sb->s_op = &aufs_sop;
30285 +       sb->s_d_op = &aufs_dop;
30286 +       sb->s_magic = AUFS_SUPER_MAGIC;
30287 +       sb->s_maxbytes = 0;
30288 +       sb->s_stack_depth = 1;
30289 +       au_export_init(sb);
30290 +       au_xattr_init(sb);
30291 +
30292 +       err = alloc_root(sb);
30293 +       if (unlikely(err)) {
30294 +               si_write_unlock(sb);
30295 +               goto out_info;
30296 +       }
30297 +       root = sb->s_root;
30298 +       inode = d_inode(root);
30299 +
30300 +       /*
30301 +        * actually we can parse options regardless aufs lock here.
30302 +        * but at remount time, parsing must be done before aufs lock.
30303 +        * so we follow the same rule.
30304 +        */
30305 +       ii_write_lock_parent(inode);
30306 +       aufs_write_unlock(root);
30307 +       err = au_opts_parse(sb, arg, &opts);
30308 +       if (unlikely(err))
30309 +               goto out_root;
30310 +
30311 +       /* lock vfs_inode first, then aufs. */
30312 +       inode_lock(inode);
30313 +       aufs_write_lock(root);
30314 +       err = au_opts_mount(sb, &opts);
30315 +       au_opts_free(&opts);
30316 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30317 +               sb->s_d_op = &aufs_dop_noreval;
30318 +               pr_info("%ps\n", sb->s_d_op);
30319 +               au_refresh_dop(root, /*force_reval*/0);
30320 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30321 +               au_refresh_iop(inode, /*force_getattr*/0);
30322 +       }
30323 +       aufs_write_unlock(root);
30324 +       inode_unlock(inode);
30325 +       if (!err)
30326 +               goto out_opts; /* success */
30327 +
30328 +out_root:
30329 +       dput(root);
30330 +       sb->s_root = NULL;
30331 +out_info:
30332 +       kobject_put(&sbinfo->si_kobj);
30333 +       sb->s_fs_info = NULL;
30334 +out_opts:
30335 +       free_page((unsigned long)opts.opt);
30336 +out:
30337 +       AuTraceErr(err);
30338 +       err = cvt_err(err);
30339 +       AuTraceErr(err);
30340 +       return err;
30341 +}
30342 +
30343 +/* ---------------------------------------------------------------------- */
30344 +
30345 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30346 +                                const char *dev_name __maybe_unused,
30347 +                                void *raw_data)
30348 +{
30349 +       struct dentry *root;
30350 +
30351 +       /* all timestamps always follow the ones on the branch */
30352 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30353 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30354 +       if (IS_ERR(root))
30355 +               goto out;
30356 +
30357 +       au_sbilist_add(root->d_sb);
30358 +
30359 +out:
30360 +       return root;
30361 +}
30362 +
30363 +static void aufs_kill_sb(struct super_block *sb)
30364 +{
30365 +       struct au_sbinfo *sbinfo;
30366 +
30367 +       sbinfo = au_sbi(sb);
30368 +       if (sbinfo) {
30369 +               au_sbilist_del(sb);
30370 +               aufs_write_lock(sb->s_root);
30371 +               au_fhsm_fin(sb);
30372 +               if (sbinfo->si_wbr_create_ops->fin)
30373 +                       sbinfo->si_wbr_create_ops->fin(sb);
30374 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30375 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30376 +                       au_remount_refresh(sb, /*do_idop*/0);
30377 +               }
30378 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30379 +                       au_plink_put(sb, /*verbose*/1);
30380 +               au_xino_clr(sb);
30381 +               au_dr_opt_flush(sb);
30382 +               sbinfo->si_sb = NULL;
30383 +               aufs_write_unlock(sb->s_root);
30384 +               au_nwt_flush(&sbinfo->si_nowait);
30385 +       }
30386 +       kill_anon_super(sb);
30387 +}
30388 +
30389 +struct file_system_type aufs_fs_type = {
30390 +       .name           = AUFS_FSTYPE,
30391 +       /* a race between rename and others */
30392 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30393 +                               /* untested */
30394 +                               /*| FS_ALLOW_IDMAP*/
30395 +                               ,
30396 +       .mount          = aufs_mount,
30397 +       .kill_sb        = aufs_kill_sb,
30398 +       /* no need to __module_get() and module_put(). */
30399 +       .owner          = THIS_MODULE,
30400 +};
30401 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30402 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30403 +++ linux/fs/aufs/super.h       2021-12-03 15:38:59.939980643 +0100
30404 @@ -0,0 +1,587 @@
30405 +/* SPDX-License-Identifier: GPL-2.0 */
30406 +/*
30407 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30408 + *
30409 + * This program, aufs is free software; you can redistribute it and/or modify
30410 + * it under the terms of the GNU General Public License as published by
30411 + * the Free Software Foundation; either version 2 of the License, or
30412 + * (at your option) any later version.
30413 + *
30414 + * This program is distributed in the hope that it will be useful,
30415 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30416 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30417 + * GNU General Public License for more details.
30418 + *
30419 + * You should have received a copy of the GNU General Public License
30420 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30421 + */
30422 +
30423 +/*
30424 + * super_block operations
30425 + */
30426 +
30427 +#ifndef __AUFS_SUPER_H__
30428 +#define __AUFS_SUPER_H__
30429 +
30430 +#ifdef __KERNEL__
30431 +
30432 +#include <linux/fs.h>
30433 +#include <linux/kobject.h>
30434 +#include "hbl.h"
30435 +#include "lcnt.h"
30436 +#include "rwsem.h"
30437 +#include "wkq.h"
30438 +
30439 +/* policies to select one among multiple writable branches */
30440 +struct au_wbr_copyup_operations {
30441 +       int (*copyup)(struct dentry *dentry);
30442 +};
30443 +
30444 +#define AuWbr_DIR      1               /* target is a dir */
30445 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30446 +
30447 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30448 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30449 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30450 +
30451 +struct au_wbr_create_operations {
30452 +       int (*create)(struct dentry *dentry, unsigned int flags);
30453 +       int (*init)(struct super_block *sb);
30454 +       int (*fin)(struct super_block *sb);
30455 +};
30456 +
30457 +struct au_wbr_mfs {
30458 +       struct mutex    mfs_lock; /* protect this structure */
30459 +       unsigned long   mfs_jiffy;
30460 +       unsigned long   mfs_expire;
30461 +       aufs_bindex_t   mfs_bindex;
30462 +
30463 +       unsigned long long      mfsrr_bytes;
30464 +       unsigned long long      mfsrr_watermark;
30465 +};
30466 +
30467 +#define AuPlink_NHASH 100
30468 +static inline int au_plink_hash(ino_t ino)
30469 +{
30470 +       return ino % AuPlink_NHASH;
30471 +}
30472 +
30473 +/* File-based Hierarchical Storage Management */
30474 +struct au_fhsm {
30475 +#ifdef CONFIG_AUFS_FHSM
30476 +       /* allow only one process who can receive the notification */
30477 +       spinlock_t              fhsm_spin;
30478 +       pid_t                   fhsm_pid;
30479 +       wait_queue_head_t       fhsm_wqh;
30480 +       atomic_t                fhsm_readable;
30481 +
30482 +       /* these are protected by si_rwsem */
30483 +       unsigned long           fhsm_expire;
30484 +       aufs_bindex_t           fhsm_bottom;
30485 +#endif
30486 +};
30487 +
30488 +struct au_branch;
30489 +struct au_sbinfo {
30490 +       /* nowait tasks in the system-wide workqueue */
30491 +       struct au_nowait_tasks  si_nowait;
30492 +
30493 +       /*
30494 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30495 +        * rwsem for au_sbinfo is necessary.
30496 +        */
30497 +       struct au_rwsem         si_rwsem;
30498 +
30499 +       /*
30500 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30501 +        * remount.
30502 +        */
30503 +       au_lcnt_t               si_ninodes, si_nfiles;
30504 +
30505 +       /* branch management */
30506 +       unsigned int            si_generation;
30507 +
30508 +       /* see AuSi_ flags */
30509 +       unsigned char           au_si_status;
30510 +
30511 +       aufs_bindex_t           si_bbot;
30512 +
30513 +       /* dirty trick to keep br_id plus */
30514 +       unsigned int            si_last_br_id :
30515 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30516 +       struct au_branch        **si_branch;
30517 +
30518 +       /* policy to select a writable branch */
30519 +       unsigned char           si_wbr_copyup;
30520 +       unsigned char           si_wbr_create;
30521 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30522 +       struct au_wbr_create_operations *si_wbr_create_ops;
30523 +
30524 +       /* round robin */
30525 +       atomic_t                si_wbr_rr_next;
30526 +
30527 +       /* most free space */
30528 +       struct au_wbr_mfs       si_wbr_mfs;
30529 +
30530 +       /* File-based Hierarchical Storage Management */
30531 +       struct au_fhsm          si_fhsm;
30532 +
30533 +       /* mount flags */
30534 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30535 +       unsigned int            si_mntflags;
30536 +
30537 +       /* external inode number (bitmap and translation table) */
30538 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30539 +
30540 +       struct file             *si_xib;
30541 +       struct mutex            si_xib_mtx; /* protect xib members */
30542 +       unsigned long           *si_xib_buf;
30543 +       unsigned long           si_xib_last_pindex;
30544 +       int                     si_xib_next_bit;
30545 +
30546 +       unsigned long           si_xino_jiffy;
30547 +       unsigned long           si_xino_expire;
30548 +       /* reserved for future use */
30549 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30550 +
30551 +#ifdef CONFIG_AUFS_EXPORT
30552 +       /* i_generation */
30553 +       /* todo: make xigen file an array to support many inode numbers */
30554 +       struct file             *si_xigen;
30555 +       atomic_t                si_xigen_next;
30556 +#endif
30557 +
30558 +       /* dirty trick to support atomic_open */
30559 +       struct hlist_bl_head    si_aopen;
30560 +
30561 +       /* vdir parameters */
30562 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30563 +       unsigned int            si_rdblk;       /* deblk size */
30564 +       unsigned int            si_rdhash;      /* hash size */
30565 +
30566 +       /*
30567 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30568 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30569 +        * future fsck.aufs or kernel thread will remove them later.
30570 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30571 +        */
30572 +       unsigned int            si_dirwh;
30573 +
30574 +       /* pseudo_link list */
30575 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30576 +       wait_queue_head_t       si_plink_wq;
30577 +       spinlock_t              si_plink_maint_lock;
30578 +       pid_t                   si_plink_maint_pid;
30579 +
30580 +       /* file list */
30581 +       struct hlist_bl_head    si_files;
30582 +
30583 +       /* with/without getattr, brother of sb->s_d_op */
30584 +       const struct inode_operations *si_iop_array;
30585 +
30586 +       /*
30587 +        * sysfs and lifetime management.
30588 +        * this is not a small structure and it may be a waste of memory in case
30589 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30590 +        * but using sysfs is majority.
30591 +        */
30592 +       struct kobject          si_kobj;
30593 +#ifdef CONFIG_DEBUG_FS
30594 +       struct dentry            *si_dbgaufs;
30595 +       struct dentry            *si_dbgaufs_plink;
30596 +       struct dentry            *si_dbgaufs_xib;
30597 +#ifdef CONFIG_AUFS_EXPORT
30598 +       struct dentry            *si_dbgaufs_xigen;
30599 +#endif
30600 +#endif
30601 +
30602 +#ifdef CONFIG_AUFS_SBILIST
30603 +       struct hlist_bl_node    si_list;
30604 +#endif
30605 +
30606 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30607 +       struct super_block      *si_sb;
30608 +};
30609 +
30610 +/* sbinfo status flags */
30611 +/*
30612 + * set true when refresh_dirs() failed at remount time.
30613 + * then try refreshing dirs at access time again.
30614 + * if it is false, refreshing dirs at access time is unnecessary
30615 + */
30616 +#define AuSi_FAILED_REFRESH_DIR        1
30617 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30618 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30619 +
30620 +#ifndef CONFIG_AUFS_FHSM
30621 +#undef AuSi_FHSM
30622 +#define AuSi_FHSM              0
30623 +#endif
30624 +
30625 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30626 +                                          unsigned int flag)
30627 +{
30628 +       AuRwMustAnyLock(&sbi->si_rwsem);
30629 +       return sbi->au_si_status & flag;
30630 +}
30631 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30632 +#define au_fset_si(sbinfo, name) do { \
30633 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30634 +       (sbinfo)->au_si_status |= AuSi_##name; \
30635 +} while (0)
30636 +#define au_fclr_si(sbinfo, name) do { \
30637 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30638 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30639 +} while (0)
30640 +
30641 +/* ---------------------------------------------------------------------- */
30642 +
30643 +/* policy to select one among writable branches */
30644 +#define AuWbrCopyup(sbinfo, ...) \
30645 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30646 +#define AuWbrCreate(sbinfo, ...) \
30647 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30648 +
30649 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30650 +#define AuLock_DW              1               /* write-lock dentry */
30651 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30652 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30653 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30654 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30655 +                                               /* except RENAME_EXCHANGE */
30656 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30657 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30658 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30659 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30660 +#define au_fset_lock(flags, name) \
30661 +       do { (flags) |= AuLock_##name; } while (0)
30662 +#define au_fclr_lock(flags, name) \
30663 +       do { (flags) &= ~AuLock_##name; } while (0)
30664 +
30665 +/* ---------------------------------------------------------------------- */
30666 +
30667 +/* super.c */
30668 +extern struct file_system_type aufs_fs_type;
30669 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30670 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30671 +                                          unsigned long long max, void *arg);
30672 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30673 +                    struct super_block *sb, void *arg);
30674 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30675 +void au_iarray_free(struct inode **a, unsigned long long max);
30676 +
30677 +/* sbinfo.c */
30678 +void au_si_free(struct kobject *kobj);
30679 +int au_si_alloc(struct super_block *sb);
30680 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30681 +
30682 +unsigned int au_sigen_inc(struct super_block *sb);
30683 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30684 +
30685 +int si_read_lock(struct super_block *sb, int flags);
30686 +int si_write_lock(struct super_block *sb, int flags);
30687 +int aufs_read_lock(struct dentry *dentry, int flags);
30688 +void aufs_read_unlock(struct dentry *dentry, int flags);
30689 +void aufs_write_lock(struct dentry *dentry);
30690 +void aufs_write_unlock(struct dentry *dentry);
30691 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30692 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30693 +
30694 +/* wbr_policy.c */
30695 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30696 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30697 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30698 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30699 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30700 +
30701 +/* mvdown.c */
30702 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30703 +
30704 +#ifdef CONFIG_AUFS_FHSM
30705 +/* fhsm.c */
30706 +
30707 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30708 +{
30709 +       pid_t pid;
30710 +
30711 +       spin_lock(&fhsm->fhsm_spin);
30712 +       pid = fhsm->fhsm_pid;
30713 +       spin_unlock(&fhsm->fhsm_spin);
30714 +
30715 +       return pid;
30716 +}
30717 +
30718 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30719 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30720 +int au_fhsm_fd(struct super_block *sb, int oflags);
30721 +int au_fhsm_br_alloc(struct au_branch *br);
30722 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30723 +void au_fhsm_fin(struct super_block *sb);
30724 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30725 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30726 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30727 +#else
30728 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30729 +          int force)
30730 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30731 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30732 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30733 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30734 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30735 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30736 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30737 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30738 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30739 +#endif
30740 +
30741 +/* ---------------------------------------------------------------------- */
30742 +
30743 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30744 +{
30745 +       return sb->s_fs_info;
30746 +}
30747 +
30748 +/* ---------------------------------------------------------------------- */
30749 +
30750 +#ifdef CONFIG_AUFS_EXPORT
30751 +int au_test_nfsd(void);
30752 +void au_export_init(struct super_block *sb);
30753 +void au_xigen_inc(struct inode *inode);
30754 +int au_xigen_new(struct inode *inode);
30755 +int au_xigen_set(struct super_block *sb, struct path *path);
30756 +void au_xigen_clr(struct super_block *sb);
30757 +
30758 +static inline int au_busy_or_stale(void)
30759 +{
30760 +       if (!au_test_nfsd())
30761 +               return -EBUSY;
30762 +       return -ESTALE;
30763 +}
30764 +#else
30765 +AuStubInt0(au_test_nfsd, void)
30766 +AuStubVoid(au_export_init, struct super_block *sb)
30767 +AuStubVoid(au_xigen_inc, struct inode *inode)
30768 +AuStubInt0(au_xigen_new, struct inode *inode)
30769 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30770 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30771 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30772 +#endif /* CONFIG_AUFS_EXPORT */
30773 +
30774 +/* ---------------------------------------------------------------------- */
30775 +
30776 +#ifdef CONFIG_AUFS_SBILIST
30777 +/* module.c */
30778 +extern struct hlist_bl_head au_sbilist;
30779 +
30780 +static inline void au_sbilist_init(void)
30781 +{
30782 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30783 +}
30784 +
30785 +static inline void au_sbilist_add(struct super_block *sb)
30786 +{
30787 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30788 +}
30789 +
30790 +static inline void au_sbilist_del(struct super_block *sb)
30791 +{
30792 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30793 +}
30794 +
30795 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30796 +static inline void au_sbilist_lock(void)
30797 +{
30798 +       hlist_bl_lock(&au_sbilist);
30799 +}
30800 +
30801 +static inline void au_sbilist_unlock(void)
30802 +{
30803 +       hlist_bl_unlock(&au_sbilist);
30804 +}
30805 +#define AuGFP_SBILIST  GFP_ATOMIC
30806 +#else
30807 +AuStubVoid(au_sbilist_lock, void)
30808 +AuStubVoid(au_sbilist_unlock, void)
30809 +#define AuGFP_SBILIST  GFP_NOFS
30810 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30811 +#else
30812 +AuStubVoid(au_sbilist_init, void)
30813 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30814 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30815 +AuStubVoid(au_sbilist_lock, void)
30816 +AuStubVoid(au_sbilist_unlock, void)
30817 +#define AuGFP_SBILIST  GFP_NOFS
30818 +#endif
30819 +
30820 +/* ---------------------------------------------------------------------- */
30821 +
30822 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30823 +{
30824 +       /*
30825 +        * This function is a dynamic '__init' function actually,
30826 +        * so the tiny check for si_rwsem is unnecessary.
30827 +        */
30828 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30829 +#ifdef CONFIG_DEBUG_FS
30830 +       sbinfo->si_dbgaufs = NULL;
30831 +       sbinfo->si_dbgaufs_plink = NULL;
30832 +       sbinfo->si_dbgaufs_xib = NULL;
30833 +#ifdef CONFIG_AUFS_EXPORT
30834 +       sbinfo->si_dbgaufs_xigen = NULL;
30835 +#endif
30836 +#endif
30837 +}
30838 +
30839 +/* ---------------------------------------------------------------------- */
30840 +
30841 +/* current->atomic_flags */
30842 +/* this value should never corrupt the ones defined in linux/sched.h */
30843 +#define PFA_AUFS       0x10
30844 +
30845 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30846 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30847 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30848 +
30849 +static inline int si_pid_test(struct super_block *sb)
30850 +{
30851 +       return !!task_test_aufs(current);
30852 +}
30853 +
30854 +static inline void si_pid_clr(struct super_block *sb)
30855 +{
30856 +       AuDebugOn(!task_test_aufs(current));
30857 +       task_clear_aufs(current);
30858 +}
30859 +
30860 +static inline void si_pid_set(struct super_block *sb)
30861 +{
30862 +       AuDebugOn(task_test_aufs(current));
30863 +       task_set_aufs(current);
30864 +}
30865 +
30866 +/* ---------------------------------------------------------------------- */
30867 +
30868 +/* lock superblock. mainly for entry point functions */
30869 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30870 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30871 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30872 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30873 +/*
30874 +#define __si_read_trylock_nested(sb) \
30875 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30876 +#define __si_write_trylock_nested(sb) \
30877 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30878 +*/
30879 +
30880 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30881 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30882 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30883 +
30884 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30885 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30886 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30887 +
30888 +static inline void si_noflush_read_lock(struct super_block *sb)
30889 +{
30890 +       __si_read_lock(sb);
30891 +       si_pid_set(sb);
30892 +}
30893 +
30894 +static inline int si_noflush_read_trylock(struct super_block *sb)
30895 +{
30896 +       int locked;
30897 +
30898 +       locked = __si_read_trylock(sb);
30899 +       if (locked)
30900 +               si_pid_set(sb);
30901 +       return locked;
30902 +}
30903 +
30904 +static inline void si_noflush_write_lock(struct super_block *sb)
30905 +{
30906 +       __si_write_lock(sb);
30907 +       si_pid_set(sb);
30908 +}
30909 +
30910 +static inline int si_noflush_write_trylock(struct super_block *sb)
30911 +{
30912 +       int locked;
30913 +
30914 +       locked = __si_write_trylock(sb);
30915 +       if (locked)
30916 +               si_pid_set(sb);
30917 +       return locked;
30918 +}
30919 +
30920 +#if 0 /* reserved */
30921 +static inline int si_read_trylock(struct super_block *sb, int flags)
30922 +{
30923 +       if (au_ftest_lock(flags, FLUSH))
30924 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30925 +       return si_noflush_read_trylock(sb);
30926 +}
30927 +#endif
30928 +
30929 +static inline void si_read_unlock(struct super_block *sb)
30930 +{
30931 +       si_pid_clr(sb);
30932 +       __si_read_unlock(sb);
30933 +}
30934 +
30935 +#if 0 /* reserved */
30936 +static inline int si_write_trylock(struct super_block *sb, int flags)
30937 +{
30938 +       if (au_ftest_lock(flags, FLUSH))
30939 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30940 +       return si_noflush_write_trylock(sb);
30941 +}
30942 +#endif
30943 +
30944 +static inline void si_write_unlock(struct super_block *sb)
30945 +{
30946 +       si_pid_clr(sb);
30947 +       __si_write_unlock(sb);
30948 +}
30949 +
30950 +#if 0 /* reserved */
30951 +static inline void si_downgrade_lock(struct super_block *sb)
30952 +{
30953 +       __si_downgrade_lock(sb);
30954 +}
30955 +#endif
30956 +
30957 +/* ---------------------------------------------------------------------- */
30958 +
30959 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30960 +{
30961 +       SiMustAnyLock(sb);
30962 +       return au_sbi(sb)->si_bbot;
30963 +}
30964 +
30965 +static inline unsigned int au_mntflags(struct super_block *sb)
30966 +{
30967 +       SiMustAnyLock(sb);
30968 +       return au_sbi(sb)->si_mntflags;
30969 +}
30970 +
30971 +static inline unsigned int au_sigen(struct super_block *sb)
30972 +{
30973 +       SiMustAnyLock(sb);
30974 +       return au_sbi(sb)->si_generation;
30975 +}
30976 +
30977 +static inline struct au_branch *au_sbr(struct super_block *sb,
30978 +                                      aufs_bindex_t bindex)
30979 +{
30980 +       SiMustAnyLock(sb);
30981 +       return au_sbi(sb)->si_branch[0 + bindex];
30982 +}
30983 +
30984 +static inline loff_t au_xi_maxent(struct super_block *sb)
30985 +{
30986 +       SiMustAnyLock(sb);
30987 +       return au_sbi(sb)->si_ximaxent;
30988 +}
30989 +
30990 +#endif /* __KERNEL__ */
30991 +#endif /* __AUFS_SUPER_H__ */
30992 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30993 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
30994 +++ linux/fs/aufs/sysaufs.c     2021-12-03 15:38:59.939980643 +0100
30995 @@ -0,0 +1,93 @@
30996 +// SPDX-License-Identifier: GPL-2.0
30997 +/*
30998 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30999 + *
31000 + * This program, aufs is free software; you can redistribute it and/or modify
31001 + * it under the terms of the GNU General Public License as published by
31002 + * the Free Software Foundation; either version 2 of the License, or
31003 + * (at your option) any later version.
31004 + *
31005 + * This program is distributed in the hope that it will be useful,
31006 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31007 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31008 + * GNU General Public License for more details.
31009 + *
31010 + * You should have received a copy of the GNU General Public License
31011 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31012 + */
31013 +
31014 +/*
31015 + * sysfs interface and lifetime management
31016 + * they are necessary regardless sysfs is disabled.
31017 + */
31018 +
31019 +#include <linux/random.h>
31020 +#include "aufs.h"
31021 +
31022 +unsigned long sysaufs_si_mask;
31023 +struct kset *sysaufs_kset;
31024 +
31025 +#define AuSiAttr(_name) { \
31026 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31027 +       .show   = sysaufs_si_##_name,                           \
31028 +}
31029 +
31030 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31031 +struct attribute *sysaufs_si_attrs[] = {
31032 +       &sysaufs_si_attr_xi_path.attr,
31033 +       NULL,
31034 +};
31035 +
31036 +static const struct sysfs_ops au_sbi_ops = {
31037 +       .show   = sysaufs_si_show
31038 +};
31039 +
31040 +static struct kobj_type au_sbi_ktype = {
31041 +       .release        = au_si_free,
31042 +       .sysfs_ops      = &au_sbi_ops,
31043 +       .default_attrs  = sysaufs_si_attrs
31044 +};
31045 +
31046 +/* ---------------------------------------------------------------------- */
31047 +
31048 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31049 +{
31050 +       int err;
31051 +
31052 +       sbinfo->si_kobj.kset = sysaufs_kset;
31053 +       /* cf. sysaufs_name() */
31054 +       err = kobject_init_and_add
31055 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31056 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31057 +
31058 +       return err;
31059 +}
31060 +
31061 +void sysaufs_fin(void)
31062 +{
31063 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31064 +       kset_unregister(sysaufs_kset);
31065 +}
31066 +
31067 +int __init sysaufs_init(void)
31068 +{
31069 +       int err;
31070 +
31071 +       do {
31072 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31073 +       } while (!sysaufs_si_mask);
31074 +
31075 +       err = -EINVAL;
31076 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31077 +       if (unlikely(!sysaufs_kset))
31078 +               goto out;
31079 +       err = PTR_ERR(sysaufs_kset);
31080 +       if (IS_ERR(sysaufs_kset))
31081 +               goto out;
31082 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31083 +       if (unlikely(err))
31084 +               kset_unregister(sysaufs_kset);
31085 +
31086 +out:
31087 +       return err;
31088 +}
31089 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31090 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31091 +++ linux/fs/aufs/sysaufs.h     2021-12-03 15:38:59.939980643 +0100
31092 @@ -0,0 +1,102 @@
31093 +/* SPDX-License-Identifier: GPL-2.0 */
31094 +/*
31095 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31096 + *
31097 + * This program, aufs is free software; you can redistribute it and/or modify
31098 + * it under the terms of the GNU General Public License as published by
31099 + * the Free Software Foundation; either version 2 of the License, or
31100 + * (at your option) any later version.
31101 + *
31102 + * This program is distributed in the hope that it will be useful,
31103 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31104 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31105 + * GNU General Public License for more details.
31106 + *
31107 + * You should have received a copy of the GNU General Public License
31108 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31109 + */
31110 +
31111 +/*
31112 + * sysfs interface and mount lifetime management
31113 + */
31114 +
31115 +#ifndef __SYSAUFS_H__
31116 +#define __SYSAUFS_H__
31117 +
31118 +#ifdef __KERNEL__
31119 +
31120 +#include <linux/sysfs.h>
31121 +#include "module.h"
31122 +
31123 +struct super_block;
31124 +struct au_sbinfo;
31125 +
31126 +struct sysaufs_si_attr {
31127 +       struct attribute attr;
31128 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31129 +};
31130 +
31131 +/* ---------------------------------------------------------------------- */
31132 +
31133 +/* sysaufs.c */
31134 +extern unsigned long sysaufs_si_mask;
31135 +extern struct kset *sysaufs_kset;
31136 +extern struct attribute *sysaufs_si_attrs[];
31137 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31138 +int __init sysaufs_init(void);
31139 +void sysaufs_fin(void);
31140 +
31141 +/* ---------------------------------------------------------------------- */
31142 +
31143 +/* some people doesn't like to show a pointer in kernel */
31144 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31145 +{
31146 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31147 +}
31148 +
31149 +#define SysaufsSiNamePrefix    "si_"
31150 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31151 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31152 +{
31153 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31154 +                sysaufs_si_id(sbinfo));
31155 +}
31156 +
31157 +struct au_branch;
31158 +#ifdef CONFIG_SYSFS
31159 +/* sysfs.c */
31160 +extern struct attribute_group *sysaufs_attr_group;
31161 +
31162 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31163 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31164 +                        char *buf);
31165 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31166 +#ifdef CONFIG_COMPAT
31167 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31168 +#endif
31169 +
31170 +void sysaufs_br_init(struct au_branch *br);
31171 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31172 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31173 +
31174 +#define sysaufs_brs_init()     do {} while (0)
31175 +
31176 +#else
31177 +#define sysaufs_attr_group     NULL
31178 +
31179 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31180 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31181 +       struct attribute *attr, char *buf)
31182 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31183 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31184 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31185 +
31186 +static inline void sysaufs_brs_init(void)
31187 +{
31188 +       sysaufs_brs = 0;
31189 +}
31190 +
31191 +#endif /* CONFIG_SYSFS */
31192 +
31193 +#endif /* __KERNEL__ */
31194 +#endif /* __SYSAUFS_H__ */
31195 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31196 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31197 +++ linux/fs/aufs/sysfs.c       2021-12-03 15:38:59.939980643 +0100
31198 @@ -0,0 +1,374 @@
31199 +// SPDX-License-Identifier: GPL-2.0
31200 +/*
31201 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31202 + *
31203 + * This program, aufs is free software; you can redistribute it and/or modify
31204 + * it under the terms of the GNU General Public License as published by
31205 + * the Free Software Foundation; either version 2 of the License, or
31206 + * (at your option) any later version.
31207 + *
31208 + * This program is distributed in the hope that it will be useful,
31209 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31210 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31211 + * GNU General Public License for more details.
31212 + *
31213 + * You should have received a copy of the GNU General Public License
31214 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31215 + */
31216 +
31217 +/*
31218 + * sysfs interface
31219 + */
31220 +
31221 +#include <linux/compat.h>
31222 +#include <linux/seq_file.h>
31223 +#include "aufs.h"
31224 +
31225 +#ifdef CONFIG_AUFS_FS_MODULE
31226 +/* this entry violates the "one line per file" policy of sysfs */
31227 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31228 +                          char *buf)
31229 +{
31230 +       ssize_t err;
31231 +       static char *conf =
31232 +/* this file is generated at compiling */
31233 +#include "conf.str"
31234 +               ;
31235 +
31236 +       err = snprintf(buf, PAGE_SIZE, conf);
31237 +       if (unlikely(err >= PAGE_SIZE))
31238 +               err = -EFBIG;
31239 +       return err;
31240 +}
31241 +
31242 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31243 +#endif
31244 +
31245 +static struct attribute *au_attr[] = {
31246 +#ifdef CONFIG_AUFS_FS_MODULE
31247 +       &au_config_attr.attr,
31248 +#endif
31249 +       NULL,   /* need to NULL terminate the list of attributes */
31250 +};
31251 +
31252 +static struct attribute_group sysaufs_attr_group_body = {
31253 +       .attrs = au_attr
31254 +};
31255 +
31256 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31257 +
31258 +/* ---------------------------------------------------------------------- */
31259 +
31260 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31261 +{
31262 +       int err;
31263 +
31264 +       SiMustAnyLock(sb);
31265 +
31266 +       err = 0;
31267 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31268 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31269 +               seq_putc(seq, '\n');
31270 +       }
31271 +       return err;
31272 +}
31273 +
31274 +/*
31275 + * the lifetime of branch is independent from the entry under sysfs.
31276 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31277 + * unlinked.
31278 + */
31279 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31280 +                        aufs_bindex_t bindex, int idx)
31281 +{
31282 +       int err;
31283 +       struct path path;
31284 +       struct dentry *root;
31285 +       struct au_branch *br;
31286 +       au_br_perm_str_t perm;
31287 +
31288 +       AuDbg("b%d\n", bindex);
31289 +
31290 +       err = 0;
31291 +       root = sb->s_root;
31292 +       di_read_lock_parent(root, !AuLock_IR);
31293 +       br = au_sbr(sb, bindex);
31294 +
31295 +       switch (idx) {
31296 +       case AuBrSysfs_BR:
31297 +               path.mnt = au_br_mnt(br);
31298 +               path.dentry = au_h_dptr(root, bindex);
31299 +               err = au_seq_path(seq, &path);
31300 +               if (!err) {
31301 +                       au_optstr_br_perm(&perm, br->br_perm);
31302 +                       seq_printf(seq, "=%s\n", perm.a);
31303 +               }
31304 +               break;
31305 +       case AuBrSysfs_BRID:
31306 +               seq_printf(seq, "%d\n", br->br_id);
31307 +               break;
31308 +       }
31309 +       di_read_unlock(root, !AuLock_IR);
31310 +       if (unlikely(err || seq_has_overflowed(seq)))
31311 +               err = -E2BIG;
31312 +
31313 +       return err;
31314 +}
31315 +
31316 +/* ---------------------------------------------------------------------- */
31317 +
31318 +static struct seq_file *au_seq(char *p, ssize_t len)
31319 +{
31320 +       struct seq_file *seq;
31321 +
31322 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31323 +       if (seq) {
31324 +               /* mutex_init(&seq.lock); */
31325 +               seq->buf = p;
31326 +               seq->size = len;
31327 +               return seq; /* success */
31328 +       }
31329 +
31330 +       seq = ERR_PTR(-ENOMEM);
31331 +       return seq;
31332 +}
31333 +
31334 +#define SysaufsBr_PREFIX       "br"
31335 +#define SysaufsBrid_PREFIX     "brid"
31336 +
31337 +/* todo: file size may exceed PAGE_SIZE */
31338 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31339 +                       char *buf)
31340 +{
31341 +       ssize_t err;
31342 +       int idx;
31343 +       long l;
31344 +       aufs_bindex_t bbot;
31345 +       struct au_sbinfo *sbinfo;
31346 +       struct super_block *sb;
31347 +       struct seq_file *seq;
31348 +       char *name;
31349 +       struct attribute **cattr;
31350 +
31351 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31352 +       sb = sbinfo->si_sb;
31353 +
31354 +       /*
31355 +        * prevent a race condition between sysfs and aufs.
31356 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31357 +        * prohibits maintaining the sysfs entries.
31358 +        * hew we acquire read lock after sysfs_get_active_two().
31359 +        * on the other hand, the remount process may maintain the sysfs/aufs
31360 +        * entries after acquiring write lock.
31361 +        * it can cause a deadlock.
31362 +        * simply we gave up processing read here.
31363 +        */
31364 +       err = -EBUSY;
31365 +       if (unlikely(!si_noflush_read_trylock(sb)))
31366 +               goto out;
31367 +
31368 +       seq = au_seq(buf, PAGE_SIZE);
31369 +       err = PTR_ERR(seq);
31370 +       if (IS_ERR(seq))
31371 +               goto out_unlock;
31372 +
31373 +       name = (void *)attr->name;
31374 +       cattr = sysaufs_si_attrs;
31375 +       while (*cattr) {
31376 +               if (!strcmp(name, (*cattr)->name)) {
31377 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31378 +                               ->show(seq, sb);
31379 +                       goto out_seq;
31380 +               }
31381 +               cattr++;
31382 +       }
31383 +
31384 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31385 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31386 +               idx = AuBrSysfs_BRID;
31387 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31388 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31389 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31390 +               idx = AuBrSysfs_BR;
31391 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31392 +       } else
31393 +                 BUG();
31394 +
31395 +       err = kstrtol(name, 10, &l);
31396 +       if (!err) {
31397 +               bbot = au_sbbot(sb);
31398 +               if (l <= bbot)
31399 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31400 +               else
31401 +                       err = -ENOENT;
31402 +       }
31403 +
31404 +out_seq:
31405 +       if (!err) {
31406 +               err = seq->count;
31407 +               /* sysfs limit */
31408 +               if (unlikely(err == PAGE_SIZE))
31409 +                       err = -EFBIG;
31410 +       }
31411 +       au_kfree_rcu(seq);
31412 +out_unlock:
31413 +       si_read_unlock(sb);
31414 +out:
31415 +       return err;
31416 +}
31417 +
31418 +/* ---------------------------------------------------------------------- */
31419 +
31420 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31421 +{
31422 +       int err;
31423 +       int16_t brid;
31424 +       aufs_bindex_t bindex, bbot;
31425 +       size_t sz;
31426 +       char *buf;
31427 +       struct seq_file *seq;
31428 +       struct au_branch *br;
31429 +
31430 +       si_read_lock(sb, AuLock_FLUSH);
31431 +       bbot = au_sbbot(sb);
31432 +       err = bbot + 1;
31433 +       if (!arg)
31434 +               goto out;
31435 +
31436 +       err = -ENOMEM;
31437 +       buf = (void *)__get_free_page(GFP_NOFS);
31438 +       if (unlikely(!buf))
31439 +               goto out;
31440 +
31441 +       seq = au_seq(buf, PAGE_SIZE);
31442 +       err = PTR_ERR(seq);
31443 +       if (IS_ERR(seq))
31444 +               goto out_buf;
31445 +
31446 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31447 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31448 +               /* VERIFY_WRITE */
31449 +               err = !access_ok(arg, sizeof(*arg));
31450 +               if (unlikely(err))
31451 +                       break;
31452 +
31453 +               br = au_sbr(sb, bindex);
31454 +               brid = br->br_id;
31455 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31456 +               err = __put_user(brid, &arg->id);
31457 +               if (unlikely(err))
31458 +                       break;
31459 +
31460 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31461 +               err = __put_user(br->br_perm, &arg->perm);
31462 +               if (unlikely(err))
31463 +                       break;
31464 +
31465 +               err = au_seq_path(seq, &br->br_path);
31466 +               if (unlikely(err))
31467 +                       break;
31468 +               seq_putc(seq, '\0');
31469 +               if (!seq_has_overflowed(seq)) {
31470 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31471 +                       seq->count = 0;
31472 +                       if (unlikely(err))
31473 +                               break;
31474 +               } else {
31475 +                       err = -E2BIG;
31476 +                       goto out_seq;
31477 +               }
31478 +       }
31479 +       if (unlikely(err))
31480 +               err = -EFAULT;
31481 +
31482 +out_seq:
31483 +       au_kfree_rcu(seq);
31484 +out_buf:
31485 +       free_page((unsigned long)buf);
31486 +out:
31487 +       si_read_unlock(sb);
31488 +       return err;
31489 +}
31490 +
31491 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31492 +{
31493 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31494 +}
31495 +
31496 +#ifdef CONFIG_COMPAT
31497 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31498 +{
31499 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31500 +}
31501 +#endif
31502 +
31503 +/* ---------------------------------------------------------------------- */
31504 +
31505 +void sysaufs_br_init(struct au_branch *br)
31506 +{
31507 +       int i;
31508 +       struct au_brsysfs *br_sysfs;
31509 +       struct attribute *attr;
31510 +
31511 +       br_sysfs = br->br_sysfs;
31512 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31513 +               attr = &br_sysfs->attr;
31514 +               sysfs_attr_init(attr);
31515 +               attr->name = br_sysfs->name;
31516 +               attr->mode = 0444;
31517 +               br_sysfs++;
31518 +       }
31519 +}
31520 +
31521 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31522 +{
31523 +       struct au_branch *br;
31524 +       struct kobject *kobj;
31525 +       struct au_brsysfs *br_sysfs;
31526 +       int i;
31527 +       aufs_bindex_t bbot;
31528 +
31529 +       if (!sysaufs_brs)
31530 +               return;
31531 +
31532 +       kobj = &au_sbi(sb)->si_kobj;
31533 +       bbot = au_sbbot(sb);
31534 +       for (; bindex <= bbot; bindex++) {
31535 +               br = au_sbr(sb, bindex);
31536 +               br_sysfs = br->br_sysfs;
31537 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31538 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31539 +                       br_sysfs++;
31540 +               }
31541 +       }
31542 +}
31543 +
31544 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31545 +{
31546 +       int err, i;
31547 +       aufs_bindex_t bbot;
31548 +       struct kobject *kobj;
31549 +       struct au_branch *br;
31550 +       struct au_brsysfs *br_sysfs;
31551 +
31552 +       if (!sysaufs_brs)
31553 +               return;
31554 +
31555 +       kobj = &au_sbi(sb)->si_kobj;
31556 +       bbot = au_sbbot(sb);
31557 +       for (; bindex <= bbot; bindex++) {
31558 +               br = au_sbr(sb, bindex);
31559 +               br_sysfs = br->br_sysfs;
31560 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31561 +                        SysaufsBr_PREFIX "%d", bindex);
31562 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31563 +                        SysaufsBrid_PREFIX "%d", bindex);
31564 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31565 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31566 +                       if (unlikely(err))
31567 +                               pr_warn("failed %s under sysfs(%d)\n",
31568 +                                       br_sysfs->name, err);
31569 +                       br_sysfs++;
31570 +               }
31571 +       }
31572 +}
31573 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31574 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31575 +++ linux/fs/aufs/sysrq.c       2021-12-03 15:38:59.939980643 +0100
31576 @@ -0,0 +1,149 @@
31577 +// SPDX-License-Identifier: GPL-2.0
31578 +/*
31579 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31580 + *
31581 + * This program, aufs is free software; you can redistribute it and/or modify
31582 + * it under the terms of the GNU General Public License as published by
31583 + * the Free Software Foundation; either version 2 of the License, or
31584 + * (at your option) any later version.
31585 + *
31586 + * This program is distributed in the hope that it will be useful,
31587 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31588 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31589 + * GNU General Public License for more details.
31590 + *
31591 + * You should have received a copy of the GNU General Public License
31592 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31593 + */
31594 +
31595 +/*
31596 + * magic sysrq handler
31597 + */
31598 +
31599 +/* #include <linux/sysrq.h> */
31600 +#include <linux/writeback.h>
31601 +#include "aufs.h"
31602 +
31603 +/* ---------------------------------------------------------------------- */
31604 +
31605 +static void sysrq_sb(struct super_block *sb)
31606 +{
31607 +       char *plevel;
31608 +       struct au_sbinfo *sbinfo;
31609 +       struct file *file;
31610 +       struct hlist_bl_head *files;
31611 +       struct hlist_bl_node *pos;
31612 +       struct au_finfo *finfo;
31613 +       struct inode *i;
31614 +
31615 +       plevel = au_plevel;
31616 +       au_plevel = KERN_WARNING;
31617 +
31618 +       /* since we define pr_fmt, call printk directly */
31619 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31620 +
31621 +       sbinfo = au_sbi(sb);
31622 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31623 +       pr("superblock\n");
31624 +       au_dpri_sb(sb);
31625 +
31626 +#if 0 /* reserved */
31627 +       do {
31628 +               int err, i, j, ndentry;
31629 +               struct au_dcsub_pages dpages;
31630 +               struct au_dpage *dpage;
31631 +
31632 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31633 +               if (unlikely(err))
31634 +                       break;
31635 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31636 +               if (!err)
31637 +                       for (i = 0; i < dpages.ndpage; i++) {
31638 +                               dpage = dpages.dpages + i;
31639 +                               ndentry = dpage->ndentry;
31640 +                               for (j = 0; j < ndentry; j++)
31641 +                                       au_dpri_dentry(dpage->dentries[j]);
31642 +                       }
31643 +               au_dpages_free(&dpages);
31644 +       } while (0);
31645 +#endif
31646 +
31647 +       pr("isolated inode\n");
31648 +       spin_lock(&sb->s_inode_list_lock);
31649 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31650 +               spin_lock(&i->i_lock);
31651 +               if (hlist_empty(&i->i_dentry))
31652 +                       au_dpri_inode(i);
31653 +               spin_unlock(&i->i_lock);
31654 +       }
31655 +       spin_unlock(&sb->s_inode_list_lock);
31656 +
31657 +       pr("files\n");
31658 +       files = &au_sbi(sb)->si_files;
31659 +       hlist_bl_lock(files);
31660 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31661 +               umode_t mode;
31662 +
31663 +               file = finfo->fi_file;
31664 +               mode = file_inode(file)->i_mode;
31665 +               if (!special_file(mode))
31666 +                       au_dpri_file(file);
31667 +       }
31668 +       hlist_bl_unlock(files);
31669 +       pr("done\n");
31670 +
31671 +#undef pr
31672 +       au_plevel = plevel;
31673 +}
31674 +
31675 +/* ---------------------------------------------------------------------- */
31676 +
31677 +/* module parameter */
31678 +static char *aufs_sysrq_key = "a";
31679 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31680 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31681 +
31682 +static void au_sysrq(int key __maybe_unused)
31683 +{
31684 +       struct au_sbinfo *sbinfo;
31685 +       struct hlist_bl_node *pos;
31686 +
31687 +       lockdep_off();
31688 +       au_sbilist_lock();
31689 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31690 +               sysrq_sb(sbinfo->si_sb);
31691 +       au_sbilist_unlock();
31692 +       lockdep_on();
31693 +}
31694 +
31695 +static struct sysrq_key_op au_sysrq_op = {
31696 +       .handler        = au_sysrq,
31697 +       .help_msg       = "Aufs",
31698 +       .action_msg     = "Aufs",
31699 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31700 +};
31701 +
31702 +/* ---------------------------------------------------------------------- */
31703 +
31704 +int __init au_sysrq_init(void)
31705 +{
31706 +       int err;
31707 +       char key;
31708 +
31709 +       err = -1;
31710 +       key = *aufs_sysrq_key;
31711 +       if ('a' <= key && key <= 'z')
31712 +               err = register_sysrq_key(key, &au_sysrq_op);
31713 +       if (unlikely(err))
31714 +               pr_err("err %d, sysrq=%c\n", err, key);
31715 +       return err;
31716 +}
31717 +
31718 +void au_sysrq_fin(void)
31719 +{
31720 +       int err;
31721 +
31722 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31723 +       if (unlikely(err))
31724 +               pr_err("err %d (ignored)\n", err);
31725 +}
31726 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31727 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31728 +++ linux/fs/aufs/vdir.c        2021-12-03 15:38:59.939980643 +0100
31729 @@ -0,0 +1,896 @@
31730 +// SPDX-License-Identifier: GPL-2.0
31731 +/*
31732 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31733 + *
31734 + * This program, aufs is free software; you can redistribute it and/or modify
31735 + * it under the terms of the GNU General Public License as published by
31736 + * the Free Software Foundation; either version 2 of the License, or
31737 + * (at your option) any later version.
31738 + *
31739 + * This program is distributed in the hope that it will be useful,
31740 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31741 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31742 + * GNU General Public License for more details.
31743 + *
31744 + * You should have received a copy of the GNU General Public License
31745 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31746 + */
31747 +
31748 +/*
31749 + * virtual or vertical directory
31750 + */
31751 +
31752 +#include <linux/iversion.h>
31753 +#include "aufs.h"
31754 +
31755 +static unsigned int calc_size(int nlen)
31756 +{
31757 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31758 +}
31759 +
31760 +static int set_deblk_end(union au_vdir_deblk_p *p,
31761 +                        union au_vdir_deblk_p *deblk_end)
31762 +{
31763 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31764 +               p->de->de_str.len = 0;
31765 +               /* smp_mb(); */
31766 +               return 0;
31767 +       }
31768 +       return -1; /* error */
31769 +}
31770 +
31771 +/* returns true or false */
31772 +static int is_deblk_end(union au_vdir_deblk_p *p,
31773 +                       union au_vdir_deblk_p *deblk_end)
31774 +{
31775 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31776 +               return !p->de->de_str.len;
31777 +       return 1;
31778 +}
31779 +
31780 +static unsigned char *last_deblk(struct au_vdir *vdir)
31781 +{
31782 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31783 +}
31784 +
31785 +/* ---------------------------------------------------------------------- */
31786 +
31787 +/* estimate the appropriate size for name hash table */
31788 +unsigned int au_rdhash_est(loff_t sz)
31789 +{
31790 +       unsigned int n;
31791 +
31792 +       n = UINT_MAX;
31793 +       sz >>= 10;
31794 +       if (sz < n)
31795 +               n = sz;
31796 +       if (sz < AUFS_RDHASH_DEF)
31797 +               n = AUFS_RDHASH_DEF;
31798 +       /* pr_info("n %u\n", n); */
31799 +       return n;
31800 +}
31801 +
31802 +/*
31803 + * the allocated memory has to be freed by
31804 + * au_nhash_wh_free() or au_nhash_de_free().
31805 + */
31806 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31807 +{
31808 +       struct hlist_head *head;
31809 +       unsigned int u;
31810 +       size_t sz;
31811 +
31812 +       sz = sizeof(*nhash->nh_head) * num_hash;
31813 +       head = kmalloc(sz, gfp);
31814 +       if (head) {
31815 +               nhash->nh_num = num_hash;
31816 +               nhash->nh_head = head;
31817 +               for (u = 0; u < num_hash; u++)
31818 +                       INIT_HLIST_HEAD(head++);
31819 +               return 0; /* success */
31820 +       }
31821 +
31822 +       return -ENOMEM;
31823 +}
31824 +
31825 +static void nhash_count(struct hlist_head *head)
31826 +{
31827 +#if 0 /* debugging */
31828 +       unsigned long n;
31829 +       struct hlist_node *pos;
31830 +
31831 +       n = 0;
31832 +       hlist_for_each(pos, head)
31833 +               n++;
31834 +       pr_info("%lu\n", n);
31835 +#endif
31836 +}
31837 +
31838 +static void au_nhash_wh_do_free(struct hlist_head *head)
31839 +{
31840 +       struct au_vdir_wh *pos;
31841 +       struct hlist_node *node;
31842 +
31843 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31844 +               au_kfree_rcu(pos);
31845 +}
31846 +
31847 +static void au_nhash_de_do_free(struct hlist_head *head)
31848 +{
31849 +       struct au_vdir_dehstr *pos;
31850 +       struct hlist_node *node;
31851 +
31852 +       hlist_for_each_entry_safe(pos, node, head, hash)
31853 +               au_cache_free_vdir_dehstr(pos);
31854 +}
31855 +
31856 +static void au_nhash_do_free(struct au_nhash *nhash,
31857 +                            void (*free)(struct hlist_head *head))
31858 +{
31859 +       unsigned int n;
31860 +       struct hlist_head *head;
31861 +
31862 +       n = nhash->nh_num;
31863 +       if (!n)
31864 +               return;
31865 +
31866 +       head = nhash->nh_head;
31867 +       while (n-- > 0) {
31868 +               nhash_count(head);
31869 +               free(head++);
31870 +       }
31871 +       au_kfree_try_rcu(nhash->nh_head);
31872 +}
31873 +
31874 +void au_nhash_wh_free(struct au_nhash *whlist)
31875 +{
31876 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31877 +}
31878 +
31879 +static void au_nhash_de_free(struct au_nhash *delist)
31880 +{
31881 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31882 +}
31883 +
31884 +/* ---------------------------------------------------------------------- */
31885 +
31886 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31887 +                           int limit)
31888 +{
31889 +       int num;
31890 +       unsigned int u, n;
31891 +       struct hlist_head *head;
31892 +       struct au_vdir_wh *pos;
31893 +
31894 +       num = 0;
31895 +       n = whlist->nh_num;
31896 +       head = whlist->nh_head;
31897 +       for (u = 0; u < n; u++, head++)
31898 +               hlist_for_each_entry(pos, head, wh_hash)
31899 +                       if (pos->wh_bindex == btgt && ++num > limit)
31900 +                               return 1;
31901 +       return 0;
31902 +}
31903 +
31904 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31905 +                                      unsigned char *name,
31906 +                                      unsigned int len)
31907 +{
31908 +       unsigned int v;
31909 +       /* const unsigned int magic_bit = 12; */
31910 +
31911 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31912 +
31913 +       v = 0;
31914 +       if (len > 8)
31915 +               len = 8;
31916 +       while (len--)
31917 +               v += *name++;
31918 +       /* v = hash_long(v, magic_bit); */
31919 +       v %= nhash->nh_num;
31920 +       return nhash->nh_head + v;
31921 +}
31922 +
31923 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31924 +                             int nlen)
31925 +{
31926 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31927 +}
31928 +
31929 +/* returns found or not */
31930 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31931 +{
31932 +       struct hlist_head *head;
31933 +       struct au_vdir_wh *pos;
31934 +       struct au_vdir_destr *str;
31935 +
31936 +       head = au_name_hash(whlist, name, nlen);
31937 +       hlist_for_each_entry(pos, head, wh_hash) {
31938 +               str = &pos->wh_str;
31939 +               AuDbg("%.*s\n", str->len, str->name);
31940 +               if (au_nhash_test_name(str, name, nlen))
31941 +                       return 1;
31942 +       }
31943 +       return 0;
31944 +}
31945 +
31946 +/* returns found(true) or not */
31947 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31948 +{
31949 +       struct hlist_head *head;
31950 +       struct au_vdir_dehstr *pos;
31951 +       struct au_vdir_destr *str;
31952 +
31953 +       head = au_name_hash(delist, name, nlen);
31954 +       hlist_for_each_entry(pos, head, hash) {
31955 +               str = pos->str;
31956 +               AuDbg("%.*s\n", str->len, str->name);
31957 +               if (au_nhash_test_name(str, name, nlen))
31958 +                       return 1;
31959 +       }
31960 +       return 0;
31961 +}
31962 +
31963 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31964 +                           unsigned char d_type)
31965 +{
31966 +#ifdef CONFIG_AUFS_SHWH
31967 +       wh->wh_ino = ino;
31968 +       wh->wh_type = d_type;
31969 +#endif
31970 +}
31971 +
31972 +/* ---------------------------------------------------------------------- */
31973 +
31974 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31975 +                      unsigned int d_type, aufs_bindex_t bindex,
31976 +                      unsigned char shwh)
31977 +{
31978 +       int err;
31979 +       struct au_vdir_destr *str;
31980 +       struct au_vdir_wh *wh;
31981 +
31982 +       AuDbg("%.*s\n", nlen, name);
31983 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31984 +
31985 +       err = -ENOMEM;
31986 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31987 +       if (unlikely(!wh))
31988 +               goto out;
31989 +
31990 +       err = 0;
31991 +       wh->wh_bindex = bindex;
31992 +       if (shwh)
31993 +               au_shwh_init_wh(wh, ino, d_type);
31994 +       str = &wh->wh_str;
31995 +       str->len = nlen;
31996 +       memcpy(str->name, name, nlen);
31997 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31998 +       /* smp_mb(); */
31999 +
32000 +out:
32001 +       return err;
32002 +}
32003 +
32004 +static int append_deblk(struct au_vdir *vdir)
32005 +{
32006 +       int err;
32007 +       unsigned long ul;
32008 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32009 +       union au_vdir_deblk_p p, deblk_end;
32010 +       unsigned char **o;
32011 +
32012 +       err = -ENOMEM;
32013 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32014 +                       GFP_NOFS, /*may_shrink*/0);
32015 +       if (unlikely(!o))
32016 +               goto out;
32017 +
32018 +       vdir->vd_deblk = o;
32019 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32020 +       if (p.deblk) {
32021 +               ul = vdir->vd_nblk++;
32022 +               vdir->vd_deblk[ul] = p.deblk;
32023 +               vdir->vd_last.ul = ul;
32024 +               vdir->vd_last.p.deblk = p.deblk;
32025 +               deblk_end.deblk = p.deblk + deblk_sz;
32026 +               err = set_deblk_end(&p, &deblk_end);
32027 +       }
32028 +
32029 +out:
32030 +       return err;
32031 +}
32032 +
32033 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32034 +                    unsigned int d_type, struct au_nhash *delist)
32035 +{
32036 +       int err;
32037 +       unsigned int sz;
32038 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32039 +       union au_vdir_deblk_p p, *room, deblk_end;
32040 +       struct au_vdir_dehstr *dehstr;
32041 +
32042 +       p.deblk = last_deblk(vdir);
32043 +       deblk_end.deblk = p.deblk + deblk_sz;
32044 +       room = &vdir->vd_last.p;
32045 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32046 +                 || !is_deblk_end(room, &deblk_end));
32047 +
32048 +       sz = calc_size(nlen);
32049 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32050 +               err = append_deblk(vdir);
32051 +               if (unlikely(err))
32052 +                       goto out;
32053 +
32054 +               p.deblk = last_deblk(vdir);
32055 +               deblk_end.deblk = p.deblk + deblk_sz;
32056 +               /* smp_mb(); */
32057 +               AuDebugOn(room->deblk != p.deblk);
32058 +       }
32059 +
32060 +       err = -ENOMEM;
32061 +       dehstr = au_cache_alloc_vdir_dehstr();
32062 +       if (unlikely(!dehstr))
32063 +               goto out;
32064 +
32065 +       dehstr->str = &room->de->de_str;
32066 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32067 +       room->de->de_ino = ino;
32068 +       room->de->de_type = d_type;
32069 +       room->de->de_str.len = nlen;
32070 +       memcpy(room->de->de_str.name, name, nlen);
32071 +
32072 +       err = 0;
32073 +       room->deblk += sz;
32074 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32075 +               err = append_deblk(vdir);
32076 +       /* smp_mb(); */
32077 +
32078 +out:
32079 +       return err;
32080 +}
32081 +
32082 +/* ---------------------------------------------------------------------- */
32083 +
32084 +void au_vdir_free(struct au_vdir *vdir)
32085 +{
32086 +       unsigned char **deblk;
32087 +
32088 +       deblk = vdir->vd_deblk;
32089 +       while (vdir->vd_nblk--)
32090 +               au_kfree_try_rcu(*deblk++);
32091 +       au_kfree_try_rcu(vdir->vd_deblk);
32092 +       au_cache_free_vdir(vdir);
32093 +}
32094 +
32095 +static struct au_vdir *alloc_vdir(struct file *file)
32096 +{
32097 +       struct au_vdir *vdir;
32098 +       struct super_block *sb;
32099 +       int err;
32100 +
32101 +       sb = file->f_path.dentry->d_sb;
32102 +       SiMustAnyLock(sb);
32103 +
32104 +       err = -ENOMEM;
32105 +       vdir = au_cache_alloc_vdir();
32106 +       if (unlikely(!vdir))
32107 +               goto out;
32108 +
32109 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32110 +       if (unlikely(!vdir->vd_deblk))
32111 +               goto out_free;
32112 +
32113 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32114 +       if (!vdir->vd_deblk_sz) {
32115 +               /* estimate the appropriate size for deblk */
32116 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32117 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32118 +       }
32119 +       vdir->vd_nblk = 0;
32120 +       vdir->vd_version = 0;
32121 +       vdir->vd_jiffy = 0;
32122 +       err = append_deblk(vdir);
32123 +       if (!err)
32124 +               return vdir; /* success */
32125 +
32126 +       au_kfree_try_rcu(vdir->vd_deblk);
32127 +
32128 +out_free:
32129 +       au_cache_free_vdir(vdir);
32130 +out:
32131 +       vdir = ERR_PTR(err);
32132 +       return vdir;
32133 +}
32134 +
32135 +static int reinit_vdir(struct au_vdir *vdir)
32136 +{
32137 +       int err;
32138 +       union au_vdir_deblk_p p, deblk_end;
32139 +
32140 +       while (vdir->vd_nblk > 1) {
32141 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32142 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32143 +               vdir->vd_nblk--;
32144 +       }
32145 +       p.deblk = vdir->vd_deblk[0];
32146 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32147 +       err = set_deblk_end(&p, &deblk_end);
32148 +       /* keep vd_dblk_sz */
32149 +       vdir->vd_last.ul = 0;
32150 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32151 +       vdir->vd_version = 0;
32152 +       vdir->vd_jiffy = 0;
32153 +       /* smp_mb(); */
32154 +       return err;
32155 +}
32156 +
32157 +/* ---------------------------------------------------------------------- */
32158 +
32159 +#define AuFillVdir_CALLED      1
32160 +#define AuFillVdir_WHABLE      (1 << 1)
32161 +#define AuFillVdir_SHWH                (1 << 2)
32162 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32163 +#define au_fset_fillvdir(flags, name) \
32164 +       do { (flags) |= AuFillVdir_##name; } while (0)
32165 +#define au_fclr_fillvdir(flags, name) \
32166 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32167 +
32168 +#ifndef CONFIG_AUFS_SHWH
32169 +#undef AuFillVdir_SHWH
32170 +#define AuFillVdir_SHWH                0
32171 +#endif
32172 +
32173 +struct fillvdir_arg {
32174 +       struct dir_context      ctx;
32175 +       struct file             *file;
32176 +       struct au_vdir          *vdir;
32177 +       struct au_nhash         delist;
32178 +       struct au_nhash         whlist;
32179 +       aufs_bindex_t           bindex;
32180 +       unsigned int            flags;
32181 +       int                     err;
32182 +};
32183 +
32184 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32185 +                   loff_t offset __maybe_unused, u64 h_ino,
32186 +                   unsigned int d_type)
32187 +{
32188 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32189 +       char *name = (void *)__name;
32190 +       struct super_block *sb;
32191 +       ino_t ino;
32192 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32193 +
32194 +       arg->err = 0;
32195 +       sb = arg->file->f_path.dentry->d_sb;
32196 +       au_fset_fillvdir(arg->flags, CALLED);
32197 +       /* smp_mb(); */
32198 +       if (nlen <= AUFS_WH_PFX_LEN
32199 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32200 +               if (test_known(&arg->delist, name, nlen)
32201 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32202 +                       goto out; /* already exists or whiteouted */
32203 +
32204 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32205 +               if (!arg->err) {
32206 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32207 +                               d_type = DT_UNKNOWN;
32208 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32209 +                                            d_type, &arg->delist);
32210 +               }
32211 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32212 +               name += AUFS_WH_PFX_LEN;
32213 +               nlen -= AUFS_WH_PFX_LEN;
32214 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32215 +                       goto out; /* already whiteouted */
32216 +
32217 +               ino = 0; /* just to suppress a warning */
32218 +               if (shwh)
32219 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32220 +                                            &ino);
32221 +               if (!arg->err) {
32222 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32223 +                               d_type = DT_UNKNOWN;
32224 +                       arg->err = au_nhash_append_wh
32225 +                               (&arg->whlist, name, nlen, ino, d_type,
32226 +                                arg->bindex, shwh);
32227 +               }
32228 +       }
32229 +
32230 +out:
32231 +       if (!arg->err)
32232 +               arg->vdir->vd_jiffy = jiffies;
32233 +       /* smp_mb(); */
32234 +       AuTraceErr(arg->err);
32235 +       return arg->err;
32236 +}
32237 +
32238 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32239 +                         struct au_nhash *whlist, struct au_nhash *delist)
32240 +{
32241 +#ifdef CONFIG_AUFS_SHWH
32242 +       int err;
32243 +       unsigned int nh, u;
32244 +       struct hlist_head *head;
32245 +       struct au_vdir_wh *pos;
32246 +       struct hlist_node *n;
32247 +       char *p, *o;
32248 +       struct au_vdir_destr *destr;
32249 +
32250 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32251 +
32252 +       err = -ENOMEM;
32253 +       o = p = (void *)__get_free_page(GFP_NOFS);
32254 +       if (unlikely(!p))
32255 +               goto out;
32256 +
32257 +       err = 0;
32258 +       nh = whlist->nh_num;
32259 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32260 +       p += AUFS_WH_PFX_LEN;
32261 +       for (u = 0; u < nh; u++) {
32262 +               head = whlist->nh_head + u;
32263 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32264 +                       destr = &pos->wh_str;
32265 +                       memcpy(p, destr->name, destr->len);
32266 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32267 +                                       pos->wh_ino, pos->wh_type, delist);
32268 +                       if (unlikely(err))
32269 +                               break;
32270 +               }
32271 +       }
32272 +
32273 +       free_page((unsigned long)o);
32274 +
32275 +out:
32276 +       AuTraceErr(err);
32277 +       return err;
32278 +#else
32279 +       return 0;
32280 +#endif
32281 +}
32282 +
32283 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32284 +{
32285 +       int err;
32286 +       unsigned int rdhash;
32287 +       loff_t offset;
32288 +       aufs_bindex_t bbot, bindex, btop;
32289 +       unsigned char shwh;
32290 +       struct file *hf, *file;
32291 +       struct super_block *sb;
32292 +
32293 +       file = arg->file;
32294 +       sb = file->f_path.dentry->d_sb;
32295 +       SiMustAnyLock(sb);
32296 +
32297 +       rdhash = au_sbi(sb)->si_rdhash;
32298 +       if (!rdhash)
32299 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32300 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32301 +       if (unlikely(err))
32302 +               goto out;
32303 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32304 +       if (unlikely(err))
32305 +               goto out_delist;
32306 +
32307 +       err = 0;
32308 +       arg->flags = 0;
32309 +       shwh = 0;
32310 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32311 +               shwh = 1;
32312 +               au_fset_fillvdir(arg->flags, SHWH);
32313 +       }
32314 +       btop = au_fbtop(file);
32315 +       bbot = au_fbbot_dir(file);
32316 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32317 +               hf = au_hf_dir(file, bindex);
32318 +               if (!hf)
32319 +                       continue;
32320 +
32321 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32322 +               err = offset;
32323 +               if (unlikely(offset))
32324 +                       break;
32325 +
32326 +               arg->bindex = bindex;
32327 +               au_fclr_fillvdir(arg->flags, WHABLE);
32328 +               if (shwh
32329 +                   || (bindex != bbot
32330 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32331 +                       au_fset_fillvdir(arg->flags, WHABLE);
32332 +               do {
32333 +                       arg->err = 0;
32334 +                       au_fclr_fillvdir(arg->flags, CALLED);
32335 +                       /* smp_mb(); */
32336 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32337 +                       if (err >= 0)
32338 +                               err = arg->err;
32339 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32340 +
32341 +               /*
32342 +                * dir_relax() may be good for concurrency, but aufs should not
32343 +                * use it since it will cause a lockdep problem.
32344 +                */
32345 +       }
32346 +
32347 +       if (!err && shwh)
32348 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32349 +
32350 +       au_nhash_wh_free(&arg->whlist);
32351 +
32352 +out_delist:
32353 +       au_nhash_de_free(&arg->delist);
32354 +out:
32355 +       return err;
32356 +}
32357 +
32358 +static int read_vdir(struct file *file, int may_read)
32359 +{
32360 +       int err;
32361 +       unsigned long expire;
32362 +       unsigned char do_read;
32363 +       struct fillvdir_arg arg = {
32364 +               .ctx = {
32365 +                       .actor = fillvdir
32366 +               }
32367 +       };
32368 +       struct inode *inode;
32369 +       struct au_vdir *vdir, *allocated;
32370 +
32371 +       err = 0;
32372 +       inode = file_inode(file);
32373 +       IMustLock(inode);
32374 +       IiMustWriteLock(inode);
32375 +       SiMustAnyLock(inode->i_sb);
32376 +
32377 +       allocated = NULL;
32378 +       do_read = 0;
32379 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32380 +       vdir = au_ivdir(inode);
32381 +       if (!vdir) {
32382 +               do_read = 1;
32383 +               vdir = alloc_vdir(file);
32384 +               err = PTR_ERR(vdir);
32385 +               if (IS_ERR(vdir))
32386 +                       goto out;
32387 +               err = 0;
32388 +               allocated = vdir;
32389 +       } else if (may_read
32390 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32391 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32392 +               do_read = 1;
32393 +               err = reinit_vdir(vdir);
32394 +               if (unlikely(err))
32395 +                       goto out;
32396 +       }
32397 +
32398 +       if (!do_read)
32399 +               return 0; /* success */
32400 +
32401 +       arg.file = file;
32402 +       arg.vdir = vdir;
32403 +       err = au_do_read_vdir(&arg);
32404 +       if (!err) {
32405 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32406 +               vdir->vd_version = inode_query_iversion(inode);
32407 +               vdir->vd_last.ul = 0;
32408 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32409 +               if (allocated)
32410 +                       au_set_ivdir(inode, allocated);
32411 +       } else if (allocated)
32412 +               au_vdir_free(allocated);
32413 +
32414 +out:
32415 +       return err;
32416 +}
32417 +
32418 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32419 +{
32420 +       int err, rerr;
32421 +       unsigned long ul, n;
32422 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32423 +
32424 +       AuDebugOn(tgt->vd_nblk != 1);
32425 +
32426 +       err = -ENOMEM;
32427 +       if (tgt->vd_nblk < src->vd_nblk) {
32428 +               unsigned char **p;
32429 +
32430 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32431 +                               GFP_NOFS, /*may_shrink*/0);
32432 +               if (unlikely(!p))
32433 +                       goto out;
32434 +               tgt->vd_deblk = p;
32435 +       }
32436 +
32437 +       if (tgt->vd_deblk_sz != deblk_sz) {
32438 +               unsigned char *p;
32439 +
32440 +               tgt->vd_deblk_sz = deblk_sz;
32441 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32442 +                               /*may_shrink*/1);
32443 +               if (unlikely(!p))
32444 +                       goto out;
32445 +               tgt->vd_deblk[0] = p;
32446 +       }
32447 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32448 +       tgt->vd_version = src->vd_version;
32449 +       tgt->vd_jiffy = src->vd_jiffy;
32450 +
32451 +       n = src->vd_nblk;
32452 +       for (ul = 1; ul < n; ul++) {
32453 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32454 +                                           GFP_NOFS);
32455 +               if (unlikely(!tgt->vd_deblk[ul]))
32456 +                       goto out;
32457 +               tgt->vd_nblk++;
32458 +       }
32459 +       tgt->vd_nblk = n;
32460 +       tgt->vd_last.ul = tgt->vd_last.ul;
32461 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32462 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32463 +               - src->vd_deblk[src->vd_last.ul];
32464 +       /* smp_mb(); */
32465 +       return 0; /* success */
32466 +
32467 +out:
32468 +       rerr = reinit_vdir(tgt);
32469 +       BUG_ON(rerr);
32470 +       return err;
32471 +}
32472 +
32473 +int au_vdir_init(struct file *file)
32474 +{
32475 +       int err;
32476 +       struct inode *inode;
32477 +       struct au_vdir *vdir_cache, *allocated;
32478 +
32479 +       /* test file->f_pos here instead of ctx->pos */
32480 +       err = read_vdir(file, !file->f_pos);
32481 +       if (unlikely(err))
32482 +               goto out;
32483 +
32484 +       allocated = NULL;
32485 +       vdir_cache = au_fvdir_cache(file);
32486 +       if (!vdir_cache) {
32487 +               vdir_cache = alloc_vdir(file);
32488 +               err = PTR_ERR(vdir_cache);
32489 +               if (IS_ERR(vdir_cache))
32490 +                       goto out;
32491 +               allocated = vdir_cache;
32492 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32493 +               /* test file->f_pos here instead of ctx->pos */
32494 +               err = reinit_vdir(vdir_cache);
32495 +               if (unlikely(err))
32496 +                       goto out;
32497 +       } else
32498 +               return 0; /* success */
32499 +
32500 +       inode = file_inode(file);
32501 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32502 +       if (!err) {
32503 +               file->f_version = inode_query_iversion(inode);
32504 +               if (allocated)
32505 +                       au_set_fvdir_cache(file, allocated);
32506 +       } else if (allocated)
32507 +               au_vdir_free(allocated);
32508 +
32509 +out:
32510 +       return err;
32511 +}
32512 +
32513 +static loff_t calc_offset(struct au_vdir *vdir)
32514 +{
32515 +       loff_t offset;
32516 +       union au_vdir_deblk_p p;
32517 +
32518 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32519 +       offset = vdir->vd_last.p.deblk - p.deblk;
32520 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32521 +       return offset;
32522 +}
32523 +
32524 +/* returns true or false */
32525 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32526 +{
32527 +       int valid;
32528 +       unsigned int deblk_sz;
32529 +       unsigned long ul, n;
32530 +       loff_t offset;
32531 +       union au_vdir_deblk_p p, deblk_end;
32532 +       struct au_vdir *vdir_cache;
32533 +
32534 +       valid = 1;
32535 +       vdir_cache = au_fvdir_cache(file);
32536 +       offset = calc_offset(vdir_cache);
32537 +       AuDbg("offset %lld\n", offset);
32538 +       if (ctx->pos == offset)
32539 +               goto out;
32540 +
32541 +       vdir_cache->vd_last.ul = 0;
32542 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32543 +       if (!ctx->pos)
32544 +               goto out;
32545 +
32546 +       valid = 0;
32547 +       deblk_sz = vdir_cache->vd_deblk_sz;
32548 +       ul = div64_u64(ctx->pos, deblk_sz);
32549 +       AuDbg("ul %lu\n", ul);
32550 +       if (ul >= vdir_cache->vd_nblk)
32551 +               goto out;
32552 +
32553 +       n = vdir_cache->vd_nblk;
32554 +       for (; ul < n; ul++) {
32555 +               p.deblk = vdir_cache->vd_deblk[ul];
32556 +               deblk_end.deblk = p.deblk + deblk_sz;
32557 +               offset = ul;
32558 +               offset *= deblk_sz;
32559 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32560 +                       unsigned int l;
32561 +
32562 +                       l = calc_size(p.de->de_str.len);
32563 +                       offset += l;
32564 +                       p.deblk += l;
32565 +               }
32566 +               if (!is_deblk_end(&p, &deblk_end)) {
32567 +                       valid = 1;
32568 +                       vdir_cache->vd_last.ul = ul;
32569 +                       vdir_cache->vd_last.p = p;
32570 +                       break;
32571 +               }
32572 +       }
32573 +
32574 +out:
32575 +       /* smp_mb(); */
32576 +       if (!valid)
32577 +               AuDbg("valid %d\n", !valid);
32578 +       return valid;
32579 +}
32580 +
32581 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32582 +{
32583 +       unsigned int l, deblk_sz;
32584 +       union au_vdir_deblk_p deblk_end;
32585 +       struct au_vdir *vdir_cache;
32586 +       struct au_vdir_de *de;
32587 +
32588 +       if (!seek_vdir(file, ctx))
32589 +               return 0;
32590 +
32591 +       vdir_cache = au_fvdir_cache(file);
32592 +       deblk_sz = vdir_cache->vd_deblk_sz;
32593 +       while (1) {
32594 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32595 +               deblk_end.deblk += deblk_sz;
32596 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32597 +                       de = vdir_cache->vd_last.p.de;
32598 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32599 +                             de->de_str.len, de->de_str.name, ctx->pos,
32600 +                             (unsigned long)de->de_ino, de->de_type);
32601 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32602 +                                              de->de_str.len, de->de_ino,
32603 +                                              de->de_type))) {
32604 +                               /* todo: ignore the error caused by udba? */
32605 +                               /* return err; */
32606 +                               return 0;
32607 +                       }
32608 +
32609 +                       l = calc_size(de->de_str.len);
32610 +                       vdir_cache->vd_last.p.deblk += l;
32611 +                       ctx->pos += l;
32612 +               }
32613 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32614 +                       vdir_cache->vd_last.ul++;
32615 +                       vdir_cache->vd_last.p.deblk
32616 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32617 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32618 +                       continue;
32619 +               }
32620 +               break;
32621 +       }
32622 +
32623 +       /* smp_mb(); */
32624 +       return 0;
32625 +}
32626 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32627 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32628 +++ linux/fs/aufs/vfsub.c       2022-01-13 12:42:15.932899468 +0100
32629 @@ -0,0 +1,919 @@
32630 +// SPDX-License-Identifier: GPL-2.0
32631 +/*
32632 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32633 + *
32634 + * This program, aufs is free software; you can redistribute it and/or modify
32635 + * it under the terms of the GNU General Public License as published by
32636 + * the Free Software Foundation; either version 2 of the License, or
32637 + * (at your option) any later version.
32638 + *
32639 + * This program is distributed in the hope that it will be useful,
32640 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32641 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32642 + * GNU General Public License for more details.
32643 + *
32644 + * You should have received a copy of the GNU General Public License
32645 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32646 + */
32647 +
32648 +/*
32649 + * sub-routines for VFS
32650 + */
32651 +
32652 +#include <linux/mnt_namespace.h>
32653 +#include <linux/namei.h>
32654 +#include <linux/nsproxy.h>
32655 +#include <linux/security.h>
32656 +#include <linux/splice.h>
32657 +#include "aufs.h"
32658 +
32659 +#ifdef CONFIG_AUFS_BR_FUSE
32660 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32661 +{
32662 +       if (!au_test_fuse(h_sb) || !au_userns)
32663 +               return 0;
32664 +
32665 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32666 +}
32667 +#endif
32668 +
32669 +int vfsub_sync_filesystem(struct super_block *h_sb)
32670 +{
32671 +       int err;
32672 +
32673 +       lockdep_off();
32674 +       down_read(&h_sb->s_umount);
32675 +       err = sync_filesystem(h_sb);
32676 +       up_read(&h_sb->s_umount);
32677 +       lockdep_on();
32678 +
32679 +       return err;
32680 +}
32681 +
32682 +/* ---------------------------------------------------------------------- */
32683 +
32684 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32685 +{
32686 +       int err;
32687 +       struct kstat st;
32688 +       struct super_block *h_sb;
32689 +
32690 +       /*
32691 +        * Always needs h_path->mnt for LSM or FUSE branch.
32692 +        */
32693 +       AuDebugOn(!h_path->mnt);
32694 +
32695 +       /* for remote fs, leave work for its getattr or d_revalidate */
32696 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32697 +       /* still some fs may acquire i_mutex. we need to skip them */
32698 +       err = 0;
32699 +       if (!did)
32700 +               did = &err;
32701 +       h_sb = h_path->dentry->d_sb;
32702 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32703 +       if (*did)
32704 +               err = vfsub_getattr(h_path, &st);
32705 +
32706 +       return err;
32707 +}
32708 +
32709 +/* ---------------------------------------------------------------------- */
32710 +
32711 +struct file *vfsub_dentry_open(struct path *path, int flags)
32712 +{
32713 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32714 +                          current_cred());
32715 +}
32716 +
32717 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32718 +{
32719 +       struct file *file;
32720 +
32721 +       lockdep_off();
32722 +       file = filp_open(path,
32723 +                        oflags /* | __FMODE_NONOTIFY */,
32724 +                        mode);
32725 +       lockdep_on();
32726 +       if (IS_ERR(file))
32727 +               goto out;
32728 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32729 +
32730 +out:
32731 +       return file;
32732 +}
32733 +
32734 +/*
32735 + * Ideally this function should call VFS:do_last() in order to keep all its
32736 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32737 + * structure such as nameidata. This is a second (or third) best approach.
32738 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32739 + */
32740 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32741 +                     struct vfsub_aopen_args *args)
32742 +{
32743 +       int err;
32744 +       struct au_branch *br = args->br;
32745 +       struct file *file = args->file;
32746 +       /* copied from linux/fs/namei.c:atomic_open() */
32747 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32748 +
32749 +       IMustLock(dir);
32750 +       AuDebugOn(!dir->i_op->atomic_open);
32751 +
32752 +       err = au_br_test_oflag(args->open_flag, br);
32753 +       if (unlikely(err))
32754 +               goto out;
32755 +
32756 +       au_lcnt_inc(&br->br_nfiles);
32757 +       file->f_path.dentry = DENTRY_NOT_SET;
32758 +       file->f_path.mnt = au_br_mnt(br);
32759 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32760 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32761 +                                    args->create_mode);
32762 +       if (unlikely(err < 0)) {
32763 +               au_lcnt_dec(&br->br_nfiles);
32764 +               goto out;
32765 +       }
32766 +
32767 +       /* temporary workaround for nfsv4 branch */
32768 +       if (au_test_nfs(dir->i_sb))
32769 +               nfs_mark_for_revalidate(dir);
32770 +
32771 +       if (file->f_mode & FMODE_CREATED)
32772 +               fsnotify_create(dir, dentry);
32773 +       if (!(file->f_mode & FMODE_OPENED)) {
32774 +               au_lcnt_dec(&br->br_nfiles);
32775 +               goto out;
32776 +       }
32777 +
32778 +       /* todo: call VFS:may_open() here */
32779 +       /* todo: ima_file_check() too? */
32780 +       if (!err && (args->open_flag & __FMODE_EXEC))
32781 +               err = deny_write_access(file);
32782 +       if (!err)
32783 +               fsnotify_open(file);
32784 +       else
32785 +               au_lcnt_dec(&br->br_nfiles);
32786 +       /* note that the file is created and still opened */
32787 +
32788 +out:
32789 +       return err;
32790 +}
32791 +
32792 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32793 +{
32794 +       int err;
32795 +
32796 +       err = kern_path(name, flags, path);
32797 +       if (!err && d_is_positive(path->dentry))
32798 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32799 +       return err;
32800 +}
32801 +
32802 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32803 +                                            struct path *ppath, int len)
32804 +{
32805 +       struct path path;
32806 +
32807 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
32808 +       if (IS_ERR(path.dentry))
32809 +               goto out;
32810 +       if (d_is_positive(path.dentry)) {
32811 +               path.mnt = ppath->mnt;
32812 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32813 +       }
32814 +
32815 +out:
32816 +       AuTraceErrPtr(path.dentry);
32817 +       return path.dentry;
32818 +}
32819 +
32820 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
32821 +                                   int len)
32822 +{
32823 +       struct path path;
32824 +
32825 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32826 +       IMustLock(d_inode(ppath->dentry));
32827 +
32828 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
32829 +       if (IS_ERR(path.dentry))
32830 +               goto out;
32831 +       if (d_is_positive(path.dentry)) {
32832 +               path.mnt = ppath->mnt;
32833 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32834 +       }
32835 +
32836 +out:
32837 +       AuTraceErrPtr(path.dentry);
32838 +       return path.dentry;
32839 +}
32840 +
32841 +void vfsub_call_lkup_one(void *args)
32842 +{
32843 +       struct vfsub_lkup_one_args *a = args;
32844 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
32845 +}
32846 +
32847 +/* ---------------------------------------------------------------------- */
32848 +
32849 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32850 +                                struct dentry *d2, struct au_hinode *hdir2)
32851 +{
32852 +       struct dentry *d;
32853 +
32854 +       lockdep_off();
32855 +       d = lock_rename(d1, d2);
32856 +       lockdep_on();
32857 +       au_hn_suspend(hdir1);
32858 +       if (hdir1 != hdir2)
32859 +               au_hn_suspend(hdir2);
32860 +
32861 +       return d;
32862 +}
32863 +
32864 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32865 +                        struct dentry *d2, struct au_hinode *hdir2)
32866 +{
32867 +       au_hn_resume(hdir1);
32868 +       if (hdir1 != hdir2)
32869 +               au_hn_resume(hdir2);
32870 +       lockdep_off();
32871 +       unlock_rename(d1, d2);
32872 +       lockdep_on();
32873 +}
32874 +
32875 +/* ---------------------------------------------------------------------- */
32876 +
32877 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32878 +{
32879 +       int err;
32880 +       struct dentry *d;
32881 +       struct user_namespace *userns;
32882 +
32883 +       IMustLock(dir);
32884 +
32885 +       d = path->dentry;
32886 +       path->dentry = d->d_parent;
32887 +       err = security_path_mknod(path, d, mode, 0);
32888 +       path->dentry = d;
32889 +       if (unlikely(err))
32890 +               goto out;
32891 +       userns = mnt_user_ns(path->mnt);
32892 +
32893 +       lockdep_off();
32894 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
32895 +       lockdep_on();
32896 +       if (!err) {
32897 +               struct path tmp = *path;
32898 +               int did;
32899 +
32900 +               vfsub_update_h_iattr(&tmp, &did);
32901 +               if (did) {
32902 +                       tmp.dentry = path->dentry->d_parent;
32903 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32904 +               }
32905 +               /*ignore*/
32906 +       }
32907 +
32908 +out:
32909 +       return err;
32910 +}
32911 +
32912 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32913 +{
32914 +       int err;
32915 +       struct dentry *d;
32916 +       struct user_namespace *userns;
32917 +
32918 +       IMustLock(dir);
32919 +
32920 +       d = path->dentry;
32921 +       path->dentry = d->d_parent;
32922 +       err = security_path_symlink(path, d, symname);
32923 +       path->dentry = d;
32924 +       if (unlikely(err))
32925 +               goto out;
32926 +       userns = mnt_user_ns(path->mnt);
32927 +
32928 +       lockdep_off();
32929 +       err = vfs_symlink(userns, dir, path->dentry, symname);
32930 +       lockdep_on();
32931 +       if (!err) {
32932 +               struct path tmp = *path;
32933 +               int did;
32934 +
32935 +               vfsub_update_h_iattr(&tmp, &did);
32936 +               if (did) {
32937 +                       tmp.dentry = path->dentry->d_parent;
32938 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32939 +               }
32940 +               /*ignore*/
32941 +       }
32942 +
32943 +out:
32944 +       return err;
32945 +}
32946 +
32947 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32948 +{
32949 +       int err;
32950 +       struct dentry *d;
32951 +       struct user_namespace *userns;
32952 +
32953 +       IMustLock(dir);
32954 +
32955 +       d = path->dentry;
32956 +       path->dentry = d->d_parent;
32957 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32958 +       path->dentry = d;
32959 +       if (unlikely(err))
32960 +               goto out;
32961 +       userns = mnt_user_ns(path->mnt);
32962 +
32963 +       lockdep_off();
32964 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
32965 +       lockdep_on();
32966 +       if (!err) {
32967 +               struct path tmp = *path;
32968 +               int did;
32969 +
32970 +               vfsub_update_h_iattr(&tmp, &did);
32971 +               if (did) {
32972 +                       tmp.dentry = path->dentry->d_parent;
32973 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32974 +               }
32975 +               /*ignore*/
32976 +       }
32977 +
32978 +out:
32979 +       return err;
32980 +}
32981 +
32982 +static int au_test_nlink(struct inode *inode)
32983 +{
32984 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32985 +
32986 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
32987 +           || inode->i_nlink < link_max)
32988 +               return 0;
32989 +       return -EMLINK;
32990 +}
32991 +
32992 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32993 +              struct inode **delegated_inode)
32994 +{
32995 +       int err;
32996 +       struct dentry *d;
32997 +       struct user_namespace *userns;
32998 +
32999 +       IMustLock(dir);
33000 +
33001 +       err = au_test_nlink(d_inode(src_dentry));
33002 +       if (unlikely(err))
33003 +               return err;
33004 +
33005 +       /* we don't call may_linkat() */
33006 +       d = path->dentry;
33007 +       path->dentry = d->d_parent;
33008 +       err = security_path_link(src_dentry, path, d);
33009 +       path->dentry = d;
33010 +       if (unlikely(err))
33011 +               goto out;
33012 +       userns = mnt_user_ns(path->mnt);
33013 +
33014 +       lockdep_off();
33015 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33016 +       lockdep_on();
33017 +       if (!err) {
33018 +               struct path tmp = *path;
33019 +               int did;
33020 +
33021 +               /* fuse has different memory inode for the same inumber */
33022 +               vfsub_update_h_iattr(&tmp, &did);
33023 +               if (did) {
33024 +                       tmp.dentry = path->dentry->d_parent;
33025 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33026 +                       tmp.dentry = src_dentry;
33027 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33028 +               }
33029 +               /*ignore*/
33030 +       }
33031 +
33032 +out:
33033 +       return err;
33034 +}
33035 +
33036 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33037 +                struct inode *dir, struct path *path,
33038 +                struct inode **delegated_inode, unsigned int flags)
33039 +{
33040 +       int err;
33041 +       struct renamedata rd;
33042 +       struct path tmp = {
33043 +               .mnt    = path->mnt
33044 +       };
33045 +       struct dentry *d;
33046 +
33047 +       IMustLock(dir);
33048 +       IMustLock(src_dir);
33049 +
33050 +       d = path->dentry;
33051 +       path->dentry = d->d_parent;
33052 +       tmp.dentry = src_dentry->d_parent;
33053 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33054 +       path->dentry = d;
33055 +       if (unlikely(err))
33056 +               goto out;
33057 +
33058 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33059 +       rd.old_dir = src_dir;
33060 +       rd.old_dentry = src_dentry;
33061 +       rd.new_mnt_userns = rd.old_mnt_userns;
33062 +       rd.new_dir = dir;
33063 +       rd.new_dentry = path->dentry;
33064 +       rd.delegated_inode = delegated_inode;
33065 +       rd.flags = flags;
33066 +       lockdep_off();
33067 +       err = vfs_rename(&rd);
33068 +       lockdep_on();
33069 +       if (!err) {
33070 +               int did;
33071 +
33072 +               tmp.dentry = d->d_parent;
33073 +               vfsub_update_h_iattr(&tmp, &did);
33074 +               if (did) {
33075 +                       tmp.dentry = src_dentry;
33076 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33077 +                       tmp.dentry = src_dentry->d_parent;
33078 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33079 +               }
33080 +               /*ignore*/
33081 +       }
33082 +
33083 +out:
33084 +       return err;
33085 +}
33086 +
33087 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33088 +{
33089 +       int err;
33090 +       struct dentry *d;
33091 +       struct user_namespace *userns;
33092 +
33093 +       IMustLock(dir);
33094 +
33095 +       d = path->dentry;
33096 +       path->dentry = d->d_parent;
33097 +       err = security_path_mkdir(path, d, mode);
33098 +       path->dentry = d;
33099 +       if (unlikely(err))
33100 +               goto out;
33101 +       userns = mnt_user_ns(path->mnt);
33102 +
33103 +       lockdep_off();
33104 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33105 +       lockdep_on();
33106 +       if (!err) {
33107 +               struct path tmp = *path;
33108 +               int did;
33109 +
33110 +               vfsub_update_h_iattr(&tmp, &did);
33111 +               if (did) {
33112 +                       tmp.dentry = path->dentry->d_parent;
33113 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33114 +               }
33115 +               /*ignore*/
33116 +       }
33117 +
33118 +out:
33119 +       return err;
33120 +}
33121 +
33122 +int vfsub_rmdir(struct inode *dir, struct path *path)
33123 +{
33124 +       int err;
33125 +       struct dentry *d;
33126 +       struct user_namespace *userns;
33127 +
33128 +       IMustLock(dir);
33129 +
33130 +       d = path->dentry;
33131 +       path->dentry = d->d_parent;
33132 +       err = security_path_rmdir(path, d);
33133 +       path->dentry = d;
33134 +       if (unlikely(err))
33135 +               goto out;
33136 +       userns = mnt_user_ns(path->mnt);
33137 +
33138 +       lockdep_off();
33139 +       err = vfs_rmdir(userns, dir, path->dentry);
33140 +       lockdep_on();
33141 +       if (!err) {
33142 +               struct path tmp = {
33143 +                       .dentry = path->dentry->d_parent,
33144 +                       .mnt    = path->mnt
33145 +               };
33146 +
33147 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33148 +       }
33149 +
33150 +out:
33151 +       return err;
33152 +}
33153 +
33154 +/* ---------------------------------------------------------------------- */
33155 +
33156 +/* todo: support mmap_sem? */
33157 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33158 +                    loff_t *ppos)
33159 +{
33160 +       ssize_t err;
33161 +
33162 +       lockdep_off();
33163 +       err = vfs_read(file, ubuf, count, ppos);
33164 +       lockdep_on();
33165 +       if (err >= 0)
33166 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33167 +       return err;
33168 +}
33169 +
33170 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33171 +                    loff_t *ppos)
33172 +{
33173 +       ssize_t err;
33174 +
33175 +       lockdep_off();
33176 +       err = kernel_read(file, kbuf, count, ppos);
33177 +       lockdep_on();
33178 +       AuTraceErr(err);
33179 +       if (err >= 0)
33180 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33181 +       return err;
33182 +}
33183 +
33184 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33185 +                     loff_t *ppos)
33186 +{
33187 +       ssize_t err;
33188 +
33189 +       lockdep_off();
33190 +       err = vfs_write(file, ubuf, count, ppos);
33191 +       lockdep_on();
33192 +       if (err >= 0)
33193 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33194 +       return err;
33195 +}
33196 +
33197 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33198 +{
33199 +       ssize_t err;
33200 +
33201 +       lockdep_off();
33202 +       err = kernel_write(file, kbuf, count, ppos);
33203 +       lockdep_on();
33204 +       if (err >= 0)
33205 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33206 +       return err;
33207 +}
33208 +
33209 +int vfsub_flush(struct file *file, fl_owner_t id)
33210 +{
33211 +       int err;
33212 +
33213 +       err = 0;
33214 +       if (file->f_op->flush) {
33215 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33216 +                       err = file->f_op->flush(file, id);
33217 +               else {
33218 +                       lockdep_off();
33219 +                       err = file->f_op->flush(file, id);
33220 +                       lockdep_on();
33221 +               }
33222 +               if (!err)
33223 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33224 +               /*ignore*/
33225 +       }
33226 +       return err;
33227 +}
33228 +
33229 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33230 +{
33231 +       int err;
33232 +
33233 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33234 +
33235 +       lockdep_off();
33236 +       err = iterate_dir(file, ctx);
33237 +       lockdep_on();
33238 +       if (err >= 0)
33239 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33240 +
33241 +       return err;
33242 +}
33243 +
33244 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33245 +                    struct pipe_inode_info *pipe, size_t len,
33246 +                    unsigned int flags)
33247 +{
33248 +       long err;
33249 +
33250 +       lockdep_off();
33251 +       err = do_splice_to(in, ppos, pipe, len, flags);
33252 +       lockdep_on();
33253 +       file_accessed(in);
33254 +       if (err >= 0)
33255 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33256 +       return err;
33257 +}
33258 +
33259 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33260 +                      loff_t *ppos, size_t len, unsigned int flags)
33261 +{
33262 +       long err;
33263 +
33264 +       lockdep_off();
33265 +       err = do_splice_from(pipe, out, ppos, len, flags);
33266 +       lockdep_on();
33267 +       if (err >= 0)
33268 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33269 +       return err;
33270 +}
33271 +
33272 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33273 +{
33274 +       int err;
33275 +
33276 +       /* file can be NULL */
33277 +       lockdep_off();
33278 +       err = vfs_fsync(file, datasync);
33279 +       lockdep_on();
33280 +       if (!err) {
33281 +               if (!path) {
33282 +                       AuDebugOn(!file);
33283 +                       path = &file->f_path;
33284 +               }
33285 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33286 +       }
33287 +       return err;
33288 +}
33289 +
33290 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33291 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33292 +               struct file *h_file)
33293 +{
33294 +       int err;
33295 +       struct inode *h_inode;
33296 +       struct super_block *h_sb;
33297 +       struct user_namespace *h_userns;
33298 +
33299 +       if (!h_file) {
33300 +               err = vfsub_truncate(h_path, length);
33301 +               goto out;
33302 +       }
33303 +
33304 +       h_inode = d_inode(h_path->dentry);
33305 +       h_sb = h_inode->i_sb;
33306 +       lockdep_off();
33307 +       sb_start_write(h_sb);
33308 +       lockdep_on();
33309 +       err = security_path_truncate(h_path);
33310 +       if (!err) {
33311 +               h_userns = mnt_user_ns(h_path->mnt);
33312 +               lockdep_off();
33313 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33314 +                                 h_file);
33315 +               lockdep_on();
33316 +       }
33317 +       lockdep_off();
33318 +       sb_end_write(h_sb);
33319 +       lockdep_on();
33320 +
33321 +out:
33322 +       return err;
33323 +}
33324 +
33325 +/* ---------------------------------------------------------------------- */
33326 +
33327 +struct au_vfsub_mkdir_args {
33328 +       int *errp;
33329 +       struct inode *dir;
33330 +       struct path *path;
33331 +       int mode;
33332 +};
33333 +
33334 +static void au_call_vfsub_mkdir(void *args)
33335 +{
33336 +       struct au_vfsub_mkdir_args *a = args;
33337 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33338 +}
33339 +
33340 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33341 +{
33342 +       int err, do_sio, wkq_err;
33343 +       struct user_namespace *userns;
33344 +
33345 +       userns = mnt_user_ns(path->mnt);
33346 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33347 +       if (!do_sio) {
33348 +               lockdep_off();
33349 +               err = vfsub_mkdir(dir, path, mode);
33350 +               lockdep_on();
33351 +       } else {
33352 +               struct au_vfsub_mkdir_args args = {
33353 +                       .errp   = &err,
33354 +                       .dir    = dir,
33355 +                       .path   = path,
33356 +                       .mode   = mode
33357 +               };
33358 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33359 +               if (unlikely(wkq_err))
33360 +                       err = wkq_err;
33361 +       }
33362 +
33363 +       return err;
33364 +}
33365 +
33366 +struct au_vfsub_rmdir_args {
33367 +       int *errp;
33368 +       struct inode *dir;
33369 +       struct path *path;
33370 +};
33371 +
33372 +static void au_call_vfsub_rmdir(void *args)
33373 +{
33374 +       struct au_vfsub_rmdir_args *a = args;
33375 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33376 +}
33377 +
33378 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33379 +{
33380 +       int err, do_sio, wkq_err;
33381 +       struct user_namespace *userns;
33382 +
33383 +       userns = mnt_user_ns(path->mnt);
33384 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33385 +       if (!do_sio) {
33386 +               lockdep_off();
33387 +               err = vfsub_rmdir(dir, path);
33388 +               lockdep_on();
33389 +       } else {
33390 +               struct au_vfsub_rmdir_args args = {
33391 +                       .errp   = &err,
33392 +                       .dir    = dir,
33393 +                       .path   = path
33394 +               };
33395 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33396 +               if (unlikely(wkq_err))
33397 +                       err = wkq_err;
33398 +       }
33399 +
33400 +       return err;
33401 +}
33402 +
33403 +/* ---------------------------------------------------------------------- */
33404 +
33405 +struct notify_change_args {
33406 +       int *errp;
33407 +       struct path *path;
33408 +       struct iattr *ia;
33409 +       struct inode **delegated_inode;
33410 +};
33411 +
33412 +static void call_notify_change(void *args)
33413 +{
33414 +       struct notify_change_args *a = args;
33415 +       struct inode *h_inode;
33416 +       struct user_namespace *userns;
33417 +
33418 +       h_inode = d_inode(a->path->dentry);
33419 +       IMustLock(h_inode);
33420 +
33421 +       *a->errp = -EPERM;
33422 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33423 +               userns = mnt_user_ns(a->path->mnt);
33424 +               lockdep_off();
33425 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33426 +                                        a->delegated_inode);
33427 +               lockdep_on();
33428 +               if (!*a->errp)
33429 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33430 +       }
33431 +       AuTraceErr(*a->errp);
33432 +}
33433 +
33434 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33435 +                       struct inode **delegated_inode)
33436 +{
33437 +       int err;
33438 +       struct notify_change_args args = {
33439 +               .errp                   = &err,
33440 +               .path                   = path,
33441 +               .ia                     = ia,
33442 +               .delegated_inode        = delegated_inode
33443 +       };
33444 +
33445 +       call_notify_change(&args);
33446 +
33447 +       return err;
33448 +}
33449 +
33450 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33451 +                           struct inode **delegated_inode)
33452 +{
33453 +       int err, wkq_err;
33454 +       struct notify_change_args args = {
33455 +               .errp                   = &err,
33456 +               .path                   = path,
33457 +               .ia                     = ia,
33458 +               .delegated_inode        = delegated_inode
33459 +       };
33460 +
33461 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33462 +       if (unlikely(wkq_err))
33463 +               err = wkq_err;
33464 +
33465 +       return err;
33466 +}
33467 +
33468 +/* ---------------------------------------------------------------------- */
33469 +
33470 +struct unlink_args {
33471 +       int *errp;
33472 +       struct inode *dir;
33473 +       struct path *path;
33474 +       struct inode **delegated_inode;
33475 +};
33476 +
33477 +static void call_unlink(void *args)
33478 +{
33479 +       struct unlink_args *a = args;
33480 +       struct dentry *d = a->path->dentry;
33481 +       struct inode *h_inode;
33482 +       struct user_namespace *userns;
33483 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33484 +                                     && au_dcount(d) == 1);
33485 +
33486 +       IMustLock(a->dir);
33487 +
33488 +       a->path->dentry = d->d_parent;
33489 +       *a->errp = security_path_unlink(a->path, d);
33490 +       a->path->dentry = d;
33491 +       if (unlikely(*a->errp))
33492 +               return;
33493 +
33494 +       if (!stop_sillyrename)
33495 +               dget(d);
33496 +       h_inode = NULL;
33497 +       if (d_is_positive(d)) {
33498 +               h_inode = d_inode(d);
33499 +               ihold(h_inode);
33500 +       }
33501 +
33502 +       userns = mnt_user_ns(a->path->mnt);
33503 +       lockdep_off();
33504 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33505 +       lockdep_on();
33506 +       if (!*a->errp) {
33507 +               struct path tmp = {
33508 +                       .dentry = d->d_parent,
33509 +                       .mnt    = a->path->mnt
33510 +               };
33511 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33512 +       }
33513 +
33514 +       if (!stop_sillyrename)
33515 +               dput(d);
33516 +       if (h_inode)
33517 +               iput(h_inode);
33518 +
33519 +       AuTraceErr(*a->errp);
33520 +}
33521 +
33522 +/*
33523 + * @dir: must be locked.
33524 + * @dentry: target dentry.
33525 + */
33526 +int vfsub_unlink(struct inode *dir, struct path *path,
33527 +                struct inode **delegated_inode, int force)
33528 +{
33529 +       int err;
33530 +       struct unlink_args args = {
33531 +               .errp                   = &err,
33532 +               .dir                    = dir,
33533 +               .path                   = path,
33534 +               .delegated_inode        = delegated_inode
33535 +       };
33536 +
33537 +       if (!force)
33538 +               call_unlink(&args);
33539 +       else {
33540 +               int wkq_err;
33541 +
33542 +               wkq_err = au_wkq_wait(call_unlink, &args);
33543 +               if (unlikely(wkq_err))
33544 +                       err = wkq_err;
33545 +       }
33546 +
33547 +       return err;
33548 +}
33549 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33550 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33551 +++ linux/fs/aufs/vfsub.h       2022-01-13 12:42:15.932899468 +0100
33552 @@ -0,0 +1,358 @@
33553 +/* SPDX-License-Identifier: GPL-2.0 */
33554 +/*
33555 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33556 + *
33557 + * This program, aufs is free software; you can redistribute it and/or modify
33558 + * it under the terms of the GNU General Public License as published by
33559 + * the Free Software Foundation; either version 2 of the License, or
33560 + * (at your option) any later version.
33561 + *
33562 + * This program is distributed in the hope that it will be useful,
33563 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33564 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33565 + * GNU General Public License for more details.
33566 + *
33567 + * You should have received a copy of the GNU General Public License
33568 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33569 + */
33570 +
33571 +/*
33572 + * sub-routines for VFS
33573 + */
33574 +
33575 +#ifndef __AUFS_VFSUB_H__
33576 +#define __AUFS_VFSUB_H__
33577 +
33578 +#ifdef __KERNEL__
33579 +
33580 +#include <linux/fs.h>
33581 +#include <linux/mount.h>
33582 +#include <linux/posix_acl.h>
33583 +#include <linux/xattr.h>
33584 +#include "debug.h"
33585 +
33586 +/* copied from linux/fs/internal.h */
33587 +/* todo: BAD approach!! */
33588 +extern void __mnt_drop_write(struct vfsmount *);
33589 +extern struct file *alloc_empty_file(int, const struct cred *);
33590 +
33591 +/* ---------------------------------------------------------------------- */
33592 +
33593 +/* lock subclass for lower inode */
33594 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33595 +/* reduce? gave up. */
33596 +enum {
33597 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33598 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33599 +       AuLsc_I_PARENT2,        /* copyup dirs */
33600 +       AuLsc_I_PARENT3,        /* copyup wh */
33601 +       AuLsc_I_CHILD,
33602 +       AuLsc_I_CHILD2,
33603 +       AuLsc_I_End
33604 +};
33605 +
33606 +/* to debug easier, do not make them inlined functions */
33607 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33608 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33609 +
33610 +/* ---------------------------------------------------------------------- */
33611 +
33612 +static inline void vfsub_drop_nlink(struct inode *inode)
33613 +{
33614 +       AuDebugOn(!inode->i_nlink);
33615 +       drop_nlink(inode);
33616 +}
33617 +
33618 +static inline void vfsub_dead_dir(struct inode *inode)
33619 +{
33620 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33621 +       inode->i_flags |= S_DEAD;
33622 +       clear_nlink(inode);
33623 +}
33624 +
33625 +static inline int vfsub_native_ro(struct inode *inode)
33626 +{
33627 +       return sb_rdonly(inode->i_sb)
33628 +               || IS_RDONLY(inode)
33629 +               /* || IS_APPEND(inode) */
33630 +               || IS_IMMUTABLE(inode);
33631 +}
33632 +
33633 +#ifdef CONFIG_AUFS_BR_FUSE
33634 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33635 +#else
33636 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33637 +#endif
33638 +
33639 +int vfsub_sync_filesystem(struct super_block *h_sb);
33640 +
33641 +/* ---------------------------------------------------------------------- */
33642 +
33643 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33644 +struct file *vfsub_dentry_open(struct path *path, int flags);
33645 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33646 +struct au_branch;
33647 +struct vfsub_aopen_args {
33648 +       struct file             *file;
33649 +       unsigned int            open_flag;
33650 +       umode_t                 create_mode;
33651 +       struct au_branch        *br;
33652 +};
33653 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33654 +                     struct vfsub_aopen_args *args);
33655 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33656 +
33657 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33658 +                                            struct path *ppath, int len);
33659 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33660 +                                   int len);
33661 +
33662 +struct vfsub_lkup_one_args {
33663 +       struct dentry **errp;
33664 +       struct qstr *name;
33665 +       struct path *ppath;
33666 +};
33667 +
33668 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33669 +                                           struct path *ppath)
33670 +{
33671 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33672 +}
33673 +
33674 +void vfsub_call_lkup_one(void *args);
33675 +
33676 +/* ---------------------------------------------------------------------- */
33677 +
33678 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33679 +{
33680 +       int err;
33681 +
33682 +       lockdep_off();
33683 +       err = mnt_want_write(mnt);
33684 +       lockdep_on();
33685 +       return err;
33686 +}
33687 +
33688 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33689 +{
33690 +       lockdep_off();
33691 +       mnt_drop_write(mnt);
33692 +       lockdep_on();
33693 +}
33694 +
33695 +#if 0 /* reserved */
33696 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33697 +{
33698 +       lockdep_off();
33699 +       mnt_drop_write_file(file);
33700 +       lockdep_on();
33701 +}
33702 +#endif
33703 +
33704 +/* ---------------------------------------------------------------------- */
33705 +
33706 +struct au_hinode;
33707 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33708 +                                struct dentry *d2, struct au_hinode *hdir2);
33709 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33710 +                        struct dentry *d2, struct au_hinode *hdir2);
33711 +
33712 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33713 +                bool want_excl);
33714 +int vfsub_symlink(struct inode *dir, struct path *path,
33715 +                 const char *symname);
33716 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33717 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33718 +              struct path *path, struct inode **delegated_inode);
33719 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33720 +                struct inode *hdir, struct path *path,
33721 +                struct inode **delegated_inode, unsigned int flags);
33722 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33723 +int vfsub_rmdir(struct inode *dir, struct path *path);
33724 +
33725 +/* ---------------------------------------------------------------------- */
33726 +
33727 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33728 +                    loff_t *ppos);
33729 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33730 +                       loff_t *ppos);
33731 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33732 +                     loff_t *ppos);
33733 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33734 +                     loff_t *ppos);
33735 +int vfsub_flush(struct file *file, fl_owner_t id);
33736 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33737 +
33738 +static inline loff_t vfsub_f_size_read(struct file *file)
33739 +{
33740 +       return i_size_read(file_inode(file));
33741 +}
33742 +
33743 +static inline unsigned int vfsub_file_flags(struct file *file)
33744 +{
33745 +       unsigned int flags;
33746 +
33747 +       spin_lock(&file->f_lock);
33748 +       flags = file->f_flags;
33749 +       spin_unlock(&file->f_lock);
33750 +
33751 +       return flags;
33752 +}
33753 +
33754 +static inline int vfsub_file_execed(struct file *file)
33755 +{
33756 +       /* todo: direct access f_flags */
33757 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33758 +}
33759 +
33760 +#if 0 /* reserved */
33761 +static inline void vfsub_file_accessed(struct file *h_file)
33762 +{
33763 +       file_accessed(h_file);
33764 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33765 +}
33766 +#endif
33767 +
33768 +#if 0 /* reserved */
33769 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33770 +                                    struct dentry *h_dentry)
33771 +{
33772 +       struct path h_path = {
33773 +               .dentry = h_dentry,
33774 +               .mnt    = h_mnt
33775 +       };
33776 +       touch_atime(&h_path);
33777 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33778 +}
33779 +#endif
33780 +
33781 +static inline int vfsub_update_time(struct inode *h_inode,
33782 +                                   struct timespec64 *ts, int flags)
33783 +{
33784 +       return inode_update_time(h_inode, ts, flags);
33785 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33786 +}
33787 +
33788 +#ifdef CONFIG_FS_POSIX_ACL
33789 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
33790 +                                 struct inode *h_inode, umode_t h_mode)
33791 +{
33792 +       int err;
33793 +
33794 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
33795 +       if (err == -EOPNOTSUPP)
33796 +               err = 0;
33797 +       return err;
33798 +}
33799 +#else
33800 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
33801 +          struct inode *h_inode, umode_t h_mode);
33802 +#endif
33803 +
33804 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33805 +                    struct pipe_inode_info *pipe, size_t len,
33806 +                    unsigned int flags);
33807 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33808 +                      loff_t *ppos, size_t len, unsigned int flags);
33809 +
33810 +static inline long vfsub_truncate(struct path *path, loff_t length)
33811 +{
33812 +       long err;
33813 +
33814 +       lockdep_off();
33815 +       err = vfs_truncate(path, length);
33816 +       lockdep_on();
33817 +       return err;
33818 +}
33819 +
33820 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33821 +               struct file *h_file);
33822 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33823 +
33824 +/*
33825 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33826 + * ioctl.
33827 + */
33828 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33829 +                                           loff_t len)
33830 +{
33831 +       loff_t err;
33832 +
33833 +       lockdep_off();
33834 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33835 +       lockdep_on();
33836 +
33837 +       return err;
33838 +}
33839 +
33840 +/* copy_file_range(2) is a systemcall */
33841 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33842 +                                           struct file *dst, loff_t dst_pos,
33843 +                                           size_t len, unsigned int flags)
33844 +{
33845 +       ssize_t ssz;
33846 +
33847 +       lockdep_off();
33848 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33849 +       lockdep_on();
33850 +
33851 +       return ssz;
33852 +}
33853 +
33854 +/* ---------------------------------------------------------------------- */
33855 +
33856 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33857 +{
33858 +       loff_t err;
33859 +
33860 +       lockdep_off();
33861 +       err = vfs_llseek(file, offset, origin);
33862 +       lockdep_on();
33863 +       return err;
33864 +}
33865 +
33866 +/* ---------------------------------------------------------------------- */
33867 +
33868 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33869 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33870 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33871 +                           struct inode **delegated_inode);
33872 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33873 +                       struct inode **delegated_inode);
33874 +int vfsub_unlink(struct inode *dir, struct path *path,
33875 +                struct inode **delegated_inode, int force);
33876 +
33877 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33878 +{
33879 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33880 +}
33881 +
33882 +/* ---------------------------------------------------------------------- */
33883 +
33884 +static inline int vfsub_setxattr(struct user_namespace *userns,
33885 +                                struct dentry *dentry, const char *name,
33886 +                                const void *value, size_t size, int flags)
33887 +{
33888 +       int err;
33889 +
33890 +       lockdep_off();
33891 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
33892 +       lockdep_on();
33893 +
33894 +       return err;
33895 +}
33896 +
33897 +static inline int vfsub_removexattr(struct user_namespace *userns,
33898 +                                   struct dentry *dentry, const char *name)
33899 +{
33900 +       int err;
33901 +
33902 +       lockdep_off();
33903 +       err = vfs_removexattr(userns, dentry, name);
33904 +       lockdep_on();
33905 +
33906 +       return err;
33907 +}
33908 +
33909 +#endif /* __KERNEL__ */
33910 +#endif /* __AUFS_VFSUB_H__ */
33911 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33912 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33913 +++ linux/fs/aufs/wbr_policy.c  2021-12-03 15:38:59.939980643 +0100
33914 @@ -0,0 +1,830 @@
33915 +// SPDX-License-Identifier: GPL-2.0
33916 +/*
33917 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33918 + *
33919 + * This program, aufs is free software; you can redistribute it and/or modify
33920 + * it under the terms of the GNU General Public License as published by
33921 + * the Free Software Foundation; either version 2 of the License, or
33922 + * (at your option) any later version.
33923 + *
33924 + * This program is distributed in the hope that it will be useful,
33925 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33926 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33927 + * GNU General Public License for more details.
33928 + *
33929 + * You should have received a copy of the GNU General Public License
33930 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33931 + */
33932 +
33933 +/*
33934 + * policies for selecting one among multiple writable branches
33935 + */
33936 +
33937 +#include <linux/statfs.h>
33938 +#include "aufs.h"
33939 +
33940 +/* subset of cpup_attr() */
33941 +static noinline_for_stack
33942 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33943 +{
33944 +       int err, sbits;
33945 +       struct iattr ia;
33946 +       struct inode *h_isrc;
33947 +
33948 +       h_isrc = d_inode(h_src);
33949 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33950 +       ia.ia_mode = h_isrc->i_mode;
33951 +       ia.ia_uid = h_isrc->i_uid;
33952 +       ia.ia_gid = h_isrc->i_gid;
33953 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33954 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33955 +       /* no delegation since it is just created */
33956 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33957 +
33958 +       /* is this nfs only? */
33959 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33960 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33961 +               ia.ia_mode = h_isrc->i_mode;
33962 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33963 +       }
33964 +
33965 +       return err;
33966 +}
33967 +
33968 +#define AuCpdown_PARENT_OPQ    1
33969 +#define AuCpdown_WHED          (1 << 1)
33970 +#define AuCpdown_MADE_DIR      (1 << 2)
33971 +#define AuCpdown_DIROPQ                (1 << 3)
33972 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
33973 +#define au_fset_cpdown(flags, name) \
33974 +       do { (flags) |= AuCpdown_##name; } while (0)
33975 +#define au_fclr_cpdown(flags, name) \
33976 +       do { (flags) &= ~AuCpdown_##name; } while (0)
33977 +
33978 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33979 +                            unsigned int *flags)
33980 +{
33981 +       int err;
33982 +       struct dentry *opq_dentry;
33983 +
33984 +       opq_dentry = au_diropq_create(dentry, bdst);
33985 +       err = PTR_ERR(opq_dentry);
33986 +       if (IS_ERR(opq_dentry))
33987 +               goto out;
33988 +       dput(opq_dentry);
33989 +       au_fset_cpdown(*flags, DIROPQ);
33990 +
33991 +out:
33992 +       return err;
33993 +}
33994 +
33995 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33996 +                           struct inode *dir, aufs_bindex_t bdst)
33997 +{
33998 +       int err;
33999 +       struct path h_path;
34000 +       struct au_branch *br;
34001 +
34002 +       br = au_sbr(dentry->d_sb, bdst);
34003 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34004 +       err = PTR_ERR(h_path.dentry);
34005 +       if (IS_ERR(h_path.dentry))
34006 +               goto out;
34007 +
34008 +       err = 0;
34009 +       if (d_is_positive(h_path.dentry)) {
34010 +               h_path.mnt = au_br_mnt(br);
34011 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34012 +                                         dentry);
34013 +       }
34014 +       dput(h_path.dentry);
34015 +
34016 +out:
34017 +       return err;
34018 +}
34019 +
34020 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34021 +                        struct au_pin *pin,
34022 +                        struct dentry *h_parent, void *arg)
34023 +{
34024 +       int err, rerr;
34025 +       aufs_bindex_t bopq, btop;
34026 +       struct path h_path;
34027 +       struct dentry *parent;
34028 +       struct inode *h_dir, *h_inode, *inode, *dir;
34029 +       unsigned int *flags = arg;
34030 +
34031 +       btop = au_dbtop(dentry);
34032 +       /* dentry is di-locked */
34033 +       parent = dget_parent(dentry);
34034 +       dir = d_inode(parent);
34035 +       h_dir = d_inode(h_parent);
34036 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34037 +       IMustLock(h_dir);
34038 +
34039 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34040 +       if (unlikely(err < 0))
34041 +               goto out;
34042 +       h_path.dentry = au_h_dptr(dentry, bdst);
34043 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34044 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34045 +       if (unlikely(err))
34046 +               goto out_put;
34047 +       au_fset_cpdown(*flags, MADE_DIR);
34048 +
34049 +       bopq = au_dbdiropq(dentry);
34050 +       au_fclr_cpdown(*flags, WHED);
34051 +       au_fclr_cpdown(*flags, DIROPQ);
34052 +       if (au_dbwh(dentry) == bdst)
34053 +               au_fset_cpdown(*flags, WHED);
34054 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34055 +               au_fset_cpdown(*flags, PARENT_OPQ);
34056 +       h_inode = d_inode(h_path.dentry);
34057 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34058 +       if (au_ftest_cpdown(*flags, WHED)) {
34059 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34060 +               if (unlikely(err)) {
34061 +                       inode_unlock(h_inode);
34062 +                       goto out_dir;
34063 +               }
34064 +       }
34065 +
34066 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34067 +       inode_unlock(h_inode);
34068 +       if (unlikely(err))
34069 +               goto out_opq;
34070 +
34071 +       if (au_ftest_cpdown(*flags, WHED)) {
34072 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34073 +               if (unlikely(err))
34074 +                       goto out_opq;
34075 +       }
34076 +
34077 +       inode = d_inode(dentry);
34078 +       if (au_ibbot(inode) < bdst)
34079 +               au_set_ibbot(inode, bdst);
34080 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34081 +                     au_hi_flags(inode, /*isdir*/1));
34082 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34083 +       goto out; /* success */
34084 +
34085 +       /* revert */
34086 +out_opq:
34087 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34088 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34089 +               rerr = au_diropq_remove(dentry, bdst);
34090 +               inode_unlock(h_inode);
34091 +               if (unlikely(rerr)) {
34092 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34093 +                               dentry, bdst, rerr);
34094 +                       err = -EIO;
34095 +                       goto out;
34096 +               }
34097 +       }
34098 +out_dir:
34099 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34100 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34101 +               if (unlikely(rerr)) {
34102 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34103 +                               dentry, bdst, rerr);
34104 +                       err = -EIO;
34105 +               }
34106 +       }
34107 +out_put:
34108 +       au_set_h_dptr(dentry, bdst, NULL);
34109 +       if (au_dbbot(dentry) == bdst)
34110 +               au_update_dbbot(dentry);
34111 +out:
34112 +       dput(parent);
34113 +       return err;
34114 +}
34115 +
34116 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34117 +{
34118 +       int err;
34119 +       unsigned int flags;
34120 +
34121 +       flags = 0;
34122 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34123 +
34124 +       return err;
34125 +}
34126 +
34127 +/* ---------------------------------------------------------------------- */
34128 +
34129 +/* policies for create */
34130 +
34131 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34132 +{
34133 +       int err, i, j, ndentry;
34134 +       aufs_bindex_t bopq;
34135 +       struct au_dcsub_pages dpages;
34136 +       struct au_dpage *dpage;
34137 +       struct dentry **dentries, *parent, *d;
34138 +
34139 +       err = au_dpages_init(&dpages, GFP_NOFS);
34140 +       if (unlikely(err))
34141 +               goto out;
34142 +       parent = dget_parent(dentry);
34143 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34144 +       if (unlikely(err))
34145 +               goto out_free;
34146 +
34147 +       err = bindex;
34148 +       for (i = 0; i < dpages.ndpage; i++) {
34149 +               dpage = dpages.dpages + i;
34150 +               dentries = dpage->dentries;
34151 +               ndentry = dpage->ndentry;
34152 +               for (j = 0; j < ndentry; j++) {
34153 +                       d = dentries[j];
34154 +                       di_read_lock_parent2(d, !AuLock_IR);
34155 +                       bopq = au_dbdiropq(d);
34156 +                       di_read_unlock(d, !AuLock_IR);
34157 +                       if (bopq >= 0 && bopq < err)
34158 +                               err = bopq;
34159 +               }
34160 +       }
34161 +
34162 +out_free:
34163 +       dput(parent);
34164 +       au_dpages_free(&dpages);
34165 +out:
34166 +       return err;
34167 +}
34168 +
34169 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34170 +{
34171 +       for (; bindex >= 0; bindex--)
34172 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34173 +                       return bindex;
34174 +       return -EROFS;
34175 +}
34176 +
34177 +/* top down parent */
34178 +static int au_wbr_create_tdp(struct dentry *dentry,
34179 +                            unsigned int flags __maybe_unused)
34180 +{
34181 +       int err;
34182 +       aufs_bindex_t btop, bindex;
34183 +       struct super_block *sb;
34184 +       struct dentry *parent, *h_parent;
34185 +
34186 +       sb = dentry->d_sb;
34187 +       btop = au_dbtop(dentry);
34188 +       err = btop;
34189 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34190 +               goto out;
34191 +
34192 +       err = -EROFS;
34193 +       parent = dget_parent(dentry);
34194 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34195 +               h_parent = au_h_dptr(parent, bindex);
34196 +               if (!h_parent || d_is_negative(h_parent))
34197 +                       continue;
34198 +
34199 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34200 +                       err = bindex;
34201 +                       break;
34202 +               }
34203 +       }
34204 +       dput(parent);
34205 +
34206 +       /* bottom up here */
34207 +       if (unlikely(err < 0)) {
34208 +               err = au_wbr_bu(sb, btop - 1);
34209 +               if (err >= 0)
34210 +                       err = au_wbr_nonopq(dentry, err);
34211 +       }
34212 +
34213 +out:
34214 +       AuDbg("b%d\n", err);
34215 +       return err;
34216 +}
34217 +
34218 +/* ---------------------------------------------------------------------- */
34219 +
34220 +/* an exception for the policy other than tdp */
34221 +static int au_wbr_create_exp(struct dentry *dentry)
34222 +{
34223 +       int err;
34224 +       aufs_bindex_t bwh, bdiropq;
34225 +       struct dentry *parent;
34226 +
34227 +       err = -1;
34228 +       bwh = au_dbwh(dentry);
34229 +       parent = dget_parent(dentry);
34230 +       bdiropq = au_dbdiropq(parent);
34231 +       if (bwh >= 0) {
34232 +               if (bdiropq >= 0)
34233 +                       err = min(bdiropq, bwh);
34234 +               else
34235 +                       err = bwh;
34236 +               AuDbg("%d\n", err);
34237 +       } else if (bdiropq >= 0) {
34238 +               err = bdiropq;
34239 +               AuDbg("%d\n", err);
34240 +       }
34241 +       dput(parent);
34242 +
34243 +       if (err >= 0)
34244 +               err = au_wbr_nonopq(dentry, err);
34245 +
34246 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34247 +               err = -1;
34248 +
34249 +       AuDbg("%d\n", err);
34250 +       return err;
34251 +}
34252 +
34253 +/* ---------------------------------------------------------------------- */
34254 +
34255 +/* round robin */
34256 +static int au_wbr_create_init_rr(struct super_block *sb)
34257 +{
34258 +       int err;
34259 +
34260 +       err = au_wbr_bu(sb, au_sbbot(sb));
34261 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34262 +       /* smp_mb(); */
34263 +
34264 +       AuDbg("b%d\n", err);
34265 +       return err;
34266 +}
34267 +
34268 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34269 +{
34270 +       int err, nbr;
34271 +       unsigned int u;
34272 +       aufs_bindex_t bindex, bbot;
34273 +       struct super_block *sb;
34274 +       atomic_t *next;
34275 +
34276 +       err = au_wbr_create_exp(dentry);
34277 +       if (err >= 0)
34278 +               goto out;
34279 +
34280 +       sb = dentry->d_sb;
34281 +       next = &au_sbi(sb)->si_wbr_rr_next;
34282 +       bbot = au_sbbot(sb);
34283 +       nbr = bbot + 1;
34284 +       for (bindex = 0; bindex <= bbot; bindex++) {
34285 +               if (!au_ftest_wbr(flags, DIR)) {
34286 +                       err = atomic_dec_return(next) + 1;
34287 +                       /* modulo for 0 is meaningless */
34288 +                       if (unlikely(!err))
34289 +                               err = atomic_dec_return(next) + 1;
34290 +               } else
34291 +                       err = atomic_read(next);
34292 +               AuDbg("%d\n", err);
34293 +               u = err;
34294 +               err = u % nbr;
34295 +               AuDbg("%d\n", err);
34296 +               if (!au_br_rdonly(au_sbr(sb, err)))
34297 +                       break;
34298 +               err = -EROFS;
34299 +       }
34300 +
34301 +       if (err >= 0)
34302 +               err = au_wbr_nonopq(dentry, err);
34303 +
34304 +out:
34305 +       AuDbg("%d\n", err);
34306 +       return err;
34307 +}
34308 +
34309 +/* ---------------------------------------------------------------------- */
34310 +
34311 +/* most free space */
34312 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34313 +{
34314 +       struct super_block *sb;
34315 +       struct au_branch *br;
34316 +       struct au_wbr_mfs *mfs;
34317 +       struct dentry *h_parent;
34318 +       aufs_bindex_t bindex, bbot;
34319 +       int err;
34320 +       unsigned long long b, bavail;
34321 +       struct path h_path;
34322 +       /* reduce the stack usage */
34323 +       struct kstatfs *st;
34324 +
34325 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34326 +       if (unlikely(!st)) {
34327 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34328 +               return;
34329 +       }
34330 +
34331 +       bavail = 0;
34332 +       sb = dentry->d_sb;
34333 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34334 +       MtxMustLock(&mfs->mfs_lock);
34335 +       mfs->mfs_bindex = -EROFS;
34336 +       mfs->mfsrr_bytes = 0;
34337 +       if (!parent) {
34338 +               bindex = 0;
34339 +               bbot = au_sbbot(sb);
34340 +       } else {
34341 +               bindex = au_dbtop(parent);
34342 +               bbot = au_dbtaildir(parent);
34343 +       }
34344 +
34345 +       for (; bindex <= bbot; bindex++) {
34346 +               if (parent) {
34347 +                       h_parent = au_h_dptr(parent, bindex);
34348 +                       if (!h_parent || d_is_negative(h_parent))
34349 +                               continue;
34350 +               }
34351 +               br = au_sbr(sb, bindex);
34352 +               if (au_br_rdonly(br))
34353 +                       continue;
34354 +
34355 +               /* sb->s_root for NFS is unreliable */
34356 +               h_path.mnt = au_br_mnt(br);
34357 +               h_path.dentry = h_path.mnt->mnt_root;
34358 +               err = vfs_statfs(&h_path, st);
34359 +               if (unlikely(err)) {
34360 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34361 +                       continue;
34362 +               }
34363 +
34364 +               /* when the available size is equal, select the lower one */
34365 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34366 +                            || sizeof(b) < sizeof(st->f_bsize));
34367 +               b = st->f_bavail * st->f_bsize;
34368 +               br->br_wbr->wbr_bytes = b;
34369 +               if (b >= bavail) {
34370 +                       bavail = b;
34371 +                       mfs->mfs_bindex = bindex;
34372 +                       mfs->mfs_jiffy = jiffies;
34373 +               }
34374 +       }
34375 +
34376 +       mfs->mfsrr_bytes = bavail;
34377 +       AuDbg("b%d\n", mfs->mfs_bindex);
34378 +       au_kfree_rcu(st);
34379 +}
34380 +
34381 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34382 +{
34383 +       int err;
34384 +       struct dentry *parent;
34385 +       struct super_block *sb;
34386 +       struct au_wbr_mfs *mfs;
34387 +
34388 +       err = au_wbr_create_exp(dentry);
34389 +       if (err >= 0)
34390 +               goto out;
34391 +
34392 +       sb = dentry->d_sb;
34393 +       parent = NULL;
34394 +       if (au_ftest_wbr(flags, PARENT))
34395 +               parent = dget_parent(dentry);
34396 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34397 +       mutex_lock(&mfs->mfs_lock);
34398 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34399 +           || mfs->mfs_bindex < 0
34400 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34401 +               au_mfs(dentry, parent);
34402 +       mutex_unlock(&mfs->mfs_lock);
34403 +       err = mfs->mfs_bindex;
34404 +       dput(parent);
34405 +
34406 +       if (err >= 0)
34407 +               err = au_wbr_nonopq(dentry, err);
34408 +
34409 +out:
34410 +       AuDbg("b%d\n", err);
34411 +       return err;
34412 +}
34413 +
34414 +static int au_wbr_create_init_mfs(struct super_block *sb)
34415 +{
34416 +       struct au_wbr_mfs *mfs;
34417 +
34418 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34419 +       mutex_init(&mfs->mfs_lock);
34420 +       mfs->mfs_jiffy = 0;
34421 +       mfs->mfs_bindex = -EROFS;
34422 +
34423 +       return 0;
34424 +}
34425 +
34426 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34427 +{
34428 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34429 +       return 0;
34430 +}
34431 +
34432 +/* ---------------------------------------------------------------------- */
34433 +
34434 +/* top down regardless parent, and then mfs */
34435 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34436 +                              unsigned int flags __maybe_unused)
34437 +{
34438 +       int err;
34439 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34440 +       unsigned long long watermark;
34441 +       struct super_block *sb;
34442 +       struct au_wbr_mfs *mfs;
34443 +       struct au_branch *br;
34444 +       struct dentry *parent;
34445 +
34446 +       sb = dentry->d_sb;
34447 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34448 +       mutex_lock(&mfs->mfs_lock);
34449 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34450 +           || mfs->mfs_bindex < 0)
34451 +               au_mfs(dentry, /*parent*/NULL);
34452 +       watermark = mfs->mfsrr_watermark;
34453 +       bmfs = mfs->mfs_bindex;
34454 +       mutex_unlock(&mfs->mfs_lock);
34455 +
34456 +       /* another style of au_wbr_create_exp() */
34457 +       bwh = au_dbwh(dentry);
34458 +       parent = dget_parent(dentry);
34459 +       btail = au_dbtaildir(parent);
34460 +       if (bwh >= 0 && bwh < btail)
34461 +               btail = bwh;
34462 +
34463 +       err = au_wbr_nonopq(dentry, btail);
34464 +       if (unlikely(err < 0))
34465 +               goto out;
34466 +       btail = err;
34467 +       bfound = -1;
34468 +       for (bindex = 0; bindex <= btail; bindex++) {
34469 +               br = au_sbr(sb, bindex);
34470 +               if (au_br_rdonly(br))
34471 +                       continue;
34472 +               if (br->br_wbr->wbr_bytes > watermark) {
34473 +                       bfound = bindex;
34474 +                       break;
34475 +               }
34476 +       }
34477 +       err = bfound;
34478 +       if (err < 0)
34479 +               err = bmfs;
34480 +
34481 +out:
34482 +       dput(parent);
34483 +       AuDbg("b%d\n", err);
34484 +       return err;
34485 +}
34486 +
34487 +/* ---------------------------------------------------------------------- */
34488 +
34489 +/* most free space and then round robin */
34490 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34491 +{
34492 +       int err;
34493 +       struct au_wbr_mfs *mfs;
34494 +
34495 +       err = au_wbr_create_mfs(dentry, flags);
34496 +       if (err >= 0) {
34497 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34498 +               mutex_lock(&mfs->mfs_lock);
34499 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34500 +                       err = au_wbr_create_rr(dentry, flags);
34501 +               mutex_unlock(&mfs->mfs_lock);
34502 +       }
34503 +
34504 +       AuDbg("b%d\n", err);
34505 +       return err;
34506 +}
34507 +
34508 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34509 +{
34510 +       int err;
34511 +
34512 +       au_wbr_create_init_mfs(sb); /* ignore */
34513 +       err = au_wbr_create_init_rr(sb);
34514 +
34515 +       return err;
34516 +}
34517 +
34518 +/* ---------------------------------------------------------------------- */
34519 +
34520 +/* top down parent and most free space */
34521 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34522 +{
34523 +       int err, e2;
34524 +       unsigned long long b;
34525 +       aufs_bindex_t bindex, btop, bbot;
34526 +       struct super_block *sb;
34527 +       struct dentry *parent, *h_parent;
34528 +       struct au_branch *br;
34529 +
34530 +       err = au_wbr_create_tdp(dentry, flags);
34531 +       if (unlikely(err < 0))
34532 +               goto out;
34533 +       parent = dget_parent(dentry);
34534 +       btop = au_dbtop(parent);
34535 +       bbot = au_dbtaildir(parent);
34536 +       if (btop == bbot)
34537 +               goto out_parent; /* success */
34538 +
34539 +       e2 = au_wbr_create_mfs(dentry, flags);
34540 +       if (e2 < 0)
34541 +               goto out_parent; /* success */
34542 +
34543 +       /* when the available size is equal, select upper one */
34544 +       sb = dentry->d_sb;
34545 +       br = au_sbr(sb, err);
34546 +       b = br->br_wbr->wbr_bytes;
34547 +       AuDbg("b%d, %llu\n", err, b);
34548 +
34549 +       for (bindex = btop; bindex <= bbot; bindex++) {
34550 +               h_parent = au_h_dptr(parent, bindex);
34551 +               if (!h_parent || d_is_negative(h_parent))
34552 +                       continue;
34553 +
34554 +               br = au_sbr(sb, bindex);
34555 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34556 +                       b = br->br_wbr->wbr_bytes;
34557 +                       err = bindex;
34558 +                       AuDbg("b%d, %llu\n", err, b);
34559 +               }
34560 +       }
34561 +
34562 +       if (err >= 0)
34563 +               err = au_wbr_nonopq(dentry, err);
34564 +
34565 +out_parent:
34566 +       dput(parent);
34567 +out:
34568 +       AuDbg("b%d\n", err);
34569 +       return err;
34570 +}
34571 +
34572 +/* ---------------------------------------------------------------------- */
34573 +
34574 +/*
34575 + * - top down parent
34576 + * - most free space with parent
34577 + * - most free space round-robin regardless parent
34578 + */
34579 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34580 +{
34581 +       int err;
34582 +       unsigned long long watermark;
34583 +       struct super_block *sb;
34584 +       struct au_branch *br;
34585 +       struct au_wbr_mfs *mfs;
34586 +
34587 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34588 +       if (unlikely(err < 0))
34589 +               goto out;
34590 +
34591 +       sb = dentry->d_sb;
34592 +       br = au_sbr(sb, err);
34593 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34594 +       mutex_lock(&mfs->mfs_lock);
34595 +       watermark = mfs->mfsrr_watermark;
34596 +       mutex_unlock(&mfs->mfs_lock);
34597 +       if (br->br_wbr->wbr_bytes < watermark)
34598 +               /* regardless the parent dir */
34599 +               err = au_wbr_create_mfsrr(dentry, flags);
34600 +
34601 +out:
34602 +       AuDbg("b%d\n", err);
34603 +       return err;
34604 +}
34605 +
34606 +/* ---------------------------------------------------------------------- */
34607 +
34608 +/* policies for copyup */
34609 +
34610 +/* top down parent */
34611 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34612 +{
34613 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34614 +}
34615 +
34616 +/* bottom up parent */
34617 +static int au_wbr_copyup_bup(struct dentry *dentry)
34618 +{
34619 +       int err;
34620 +       aufs_bindex_t bindex, btop;
34621 +       struct dentry *parent, *h_parent;
34622 +       struct super_block *sb;
34623 +
34624 +       err = -EROFS;
34625 +       sb = dentry->d_sb;
34626 +       parent = dget_parent(dentry);
34627 +       btop = au_dbtop(parent);
34628 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34629 +               h_parent = au_h_dptr(parent, bindex);
34630 +               if (!h_parent || d_is_negative(h_parent))
34631 +                       continue;
34632 +
34633 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34634 +                       err = bindex;
34635 +                       break;
34636 +               }
34637 +       }
34638 +       dput(parent);
34639 +
34640 +       /* bottom up here */
34641 +       if (unlikely(err < 0))
34642 +               err = au_wbr_bu(sb, btop - 1);
34643 +
34644 +       AuDbg("b%d\n", err);
34645 +       return err;
34646 +}
34647 +
34648 +/* bottom up */
34649 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34650 +{
34651 +       int err;
34652 +
34653 +       err = au_wbr_bu(dentry->d_sb, btop);
34654 +       AuDbg("b%d\n", err);
34655 +       if (err > btop)
34656 +               err = au_wbr_nonopq(dentry, err);
34657 +
34658 +       AuDbg("b%d\n", err);
34659 +       return err;
34660 +}
34661 +
34662 +static int au_wbr_copyup_bu(struct dentry *dentry)
34663 +{
34664 +       int err;
34665 +       aufs_bindex_t btop;
34666 +
34667 +       btop = au_dbtop(dentry);
34668 +       err = au_wbr_do_copyup_bu(dentry, btop);
34669 +       return err;
34670 +}
34671 +
34672 +/* ---------------------------------------------------------------------- */
34673 +
34674 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34675 +       [AuWbrCopyup_TDP] = {
34676 +               .copyup = au_wbr_copyup_tdp
34677 +       },
34678 +       [AuWbrCopyup_BUP] = {
34679 +               .copyup = au_wbr_copyup_bup
34680 +       },
34681 +       [AuWbrCopyup_BU] = {
34682 +               .copyup = au_wbr_copyup_bu
34683 +       }
34684 +};
34685 +
34686 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34687 +       [AuWbrCreate_TDP] = {
34688 +               .create = au_wbr_create_tdp
34689 +       },
34690 +       [AuWbrCreate_RR] = {
34691 +               .create = au_wbr_create_rr,
34692 +               .init   = au_wbr_create_init_rr
34693 +       },
34694 +       [AuWbrCreate_MFS] = {
34695 +               .create = au_wbr_create_mfs,
34696 +               .init   = au_wbr_create_init_mfs,
34697 +               .fin    = au_wbr_create_fin_mfs
34698 +       },
34699 +       [AuWbrCreate_MFSV] = {
34700 +               .create = au_wbr_create_mfs,
34701 +               .init   = au_wbr_create_init_mfs,
34702 +               .fin    = au_wbr_create_fin_mfs
34703 +       },
34704 +       [AuWbrCreate_MFSRR] = {
34705 +               .create = au_wbr_create_mfsrr,
34706 +               .init   = au_wbr_create_init_mfsrr,
34707 +               .fin    = au_wbr_create_fin_mfs
34708 +       },
34709 +       [AuWbrCreate_MFSRRV] = {
34710 +               .create = au_wbr_create_mfsrr,
34711 +               .init   = au_wbr_create_init_mfsrr,
34712 +               .fin    = au_wbr_create_fin_mfs
34713 +       },
34714 +       [AuWbrCreate_TDMFS] = {
34715 +               .create = au_wbr_create_tdmfs,
34716 +               .init   = au_wbr_create_init_mfs,
34717 +               .fin    = au_wbr_create_fin_mfs
34718 +       },
34719 +       [AuWbrCreate_TDMFSV] = {
34720 +               .create = au_wbr_create_tdmfs,
34721 +               .init   = au_wbr_create_init_mfs,
34722 +               .fin    = au_wbr_create_fin_mfs
34723 +       },
34724 +       [AuWbrCreate_PMFS] = {
34725 +               .create = au_wbr_create_pmfs,
34726 +               .init   = au_wbr_create_init_mfs,
34727 +               .fin    = au_wbr_create_fin_mfs
34728 +       },
34729 +       [AuWbrCreate_PMFSV] = {
34730 +               .create = au_wbr_create_pmfs,
34731 +               .init   = au_wbr_create_init_mfs,
34732 +               .fin    = au_wbr_create_fin_mfs
34733 +       },
34734 +       [AuWbrCreate_PMFSRR] = {
34735 +               .create = au_wbr_create_pmfsrr,
34736 +               .init   = au_wbr_create_init_mfsrr,
34737 +               .fin    = au_wbr_create_fin_mfs
34738 +       },
34739 +       [AuWbrCreate_PMFSRRV] = {
34740 +               .create = au_wbr_create_pmfsrr,
34741 +               .init   = au_wbr_create_init_mfsrr,
34742 +               .fin    = au_wbr_create_fin_mfs
34743 +       }
34744 +};
34745 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34746 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34747 +++ linux/fs/aufs/whout.c       2021-12-03 15:40:58.236647297 +0100
34748 @@ -0,0 +1,1072 @@
34749 +// SPDX-License-Identifier: GPL-2.0
34750 +/*
34751 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34752 + *
34753 + * This program, aufs is free software; you can redistribute it and/or modify
34754 + * it under the terms of the GNU General Public License as published by
34755 + * the Free Software Foundation; either version 2 of the License, or
34756 + * (at your option) any later version.
34757 + *
34758 + * This program is distributed in the hope that it will be useful,
34759 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34760 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34761 + * GNU General Public License for more details.
34762 + *
34763 + * You should have received a copy of the GNU General Public License
34764 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34765 + */
34766 +
34767 +/*
34768 + * whiteout for logical deletion and opaque directory
34769 + */
34770 +
34771 +#include "aufs.h"
34772 +
34773 +#define WH_MASK                        0444
34774 +
34775 +/*
34776 + * If a directory contains this file, then it is opaque.  We start with the
34777 + * .wh. flag so that it is blocked by lookup.
34778 + */
34779 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34780 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34781 +
34782 +/*
34783 + * generate whiteout name, which is NOT terminated by NULL.
34784 + * @name: original d_name.name
34785 + * @len: original d_name.len
34786 + * @wh: whiteout qstr
34787 + * returns zero when succeeds, otherwise error.
34788 + * succeeded value as wh->name should be freed by kfree().
34789 + */
34790 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34791 +{
34792 +       char *p;
34793 +
34794 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34795 +               return -ENAMETOOLONG;
34796 +
34797 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34798 +       p = kmalloc(wh->len, GFP_NOFS);
34799 +       wh->name = p;
34800 +       if (p) {
34801 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34802 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34803 +               /* smp_mb(); */
34804 +               return 0;
34805 +       }
34806 +       return -ENOMEM;
34807 +}
34808 +
34809 +/* ---------------------------------------------------------------------- */
34810 +
34811 +/*
34812 + * test if the @wh_name exists under @h_ppath.
34813 + * @try_sio specifies the necessary of super-io.
34814 + */
34815 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
34816 +              struct qstr *wh_name, int try_sio)
34817 +{
34818 +       int err;
34819 +       struct dentry *wh_dentry;
34820 +
34821 +       if (!try_sio)
34822 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
34823 +       else
34824 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
34825 +       err = PTR_ERR(wh_dentry);
34826 +       if (IS_ERR(wh_dentry)) {
34827 +               if (err == -ENAMETOOLONG)
34828 +                       err = 0;
34829 +               goto out;
34830 +       }
34831 +
34832 +       err = 0;
34833 +       if (d_is_negative(wh_dentry))
34834 +               goto out_wh; /* success */
34835 +
34836 +       err = 1;
34837 +       if (d_is_reg(wh_dentry))
34838 +               goto out_wh; /* success */
34839 +
34840 +       err = -EIO;
34841 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34842 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34843 +
34844 +out_wh:
34845 +       dput(wh_dentry);
34846 +out:
34847 +       return err;
34848 +}
34849 +
34850 +/*
34851 + * test if the @h_path->dentry sets opaque or not.
34852 + */
34853 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
34854 +{
34855 +       int err;
34856 +       struct inode *h_dir;
34857 +
34858 +       h_dir = d_inode(h_path->dentry);
34859 +       err = au_wh_test(h_userns, h_path, &diropq_name,
34860 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
34861 +       return err;
34862 +}
34863 +
34864 +/*
34865 + * returns a negative dentry whose name is unique and temporary.
34866 + */
34867 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34868 +                            struct qstr *prefix)
34869 +{
34870 +       struct dentry *dentry;
34871 +       int i;
34872 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34873 +               *name, *p;
34874 +       /* strict atomic_t is unnecessary here */
34875 +       static unsigned short cnt;
34876 +       struct qstr qs;
34877 +       struct path h_ppath;
34878 +       struct user_namespace *h_userns;
34879 +
34880 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34881 +
34882 +       name = defname;
34883 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34884 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34885 +               dentry = ERR_PTR(-ENAMETOOLONG);
34886 +               if (unlikely(qs.len > NAME_MAX))
34887 +                       goto out;
34888 +               dentry = ERR_PTR(-ENOMEM);
34889 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34890 +               if (unlikely(!name))
34891 +                       goto out;
34892 +       }
34893 +
34894 +       /* doubly whiteout-ed */
34895 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34896 +       p = name + AUFS_WH_PFX_LEN * 2;
34897 +       memcpy(p, prefix->name, prefix->len);
34898 +       p += prefix->len;
34899 +       *p++ = '.';
34900 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34901 +
34902 +       h_ppath.dentry = h_parent;
34903 +       h_ppath.mnt = au_br_mnt(br);
34904 +       h_userns = au_br_userns(br);
34905 +       qs.name = name;
34906 +       for (i = 0; i < 3; i++) {
34907 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34908 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
34909 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34910 +                       goto out_name;
34911 +               dput(dentry);
34912 +       }
34913 +       /* pr_warn("could not get random name\n"); */
34914 +       dentry = ERR_PTR(-EEXIST);
34915 +       AuDbg("%.*s\n", AuLNPair(&qs));
34916 +       BUG();
34917 +
34918 +out_name:
34919 +       if (name != defname)
34920 +               au_kfree_try_rcu(name);
34921 +out:
34922 +       AuTraceErrPtr(dentry);
34923 +       return dentry;
34924 +}
34925 +
34926 +/*
34927 + * rename the @h_dentry on @br to the whiteouted temporary name.
34928 + */
34929 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34930 +{
34931 +       int err;
34932 +       struct path h_path = {
34933 +               .mnt = au_br_mnt(br)
34934 +       };
34935 +       struct inode *h_dir, *delegated;
34936 +       struct dentry *h_parent;
34937 +
34938 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34939 +       h_dir = d_inode(h_parent);
34940 +       IMustLock(h_dir);
34941 +
34942 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34943 +       err = PTR_ERR(h_path.dentry);
34944 +       if (IS_ERR(h_path.dentry))
34945 +               goto out;
34946 +
34947 +       /* under the same dir, no need to lock_rename() */
34948 +       delegated = NULL;
34949 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34950 +                          /*flags*/0);
34951 +       AuTraceErr(err);
34952 +       if (unlikely(err == -EWOULDBLOCK)) {
34953 +               pr_warn("cannot retry for NFSv4 delegation"
34954 +                       " for an internal rename\n");
34955 +               iput(delegated);
34956 +       }
34957 +       dput(h_path.dentry);
34958 +
34959 +out:
34960 +       AuTraceErr(err);
34961 +       return err;
34962 +}
34963 +
34964 +/* ---------------------------------------------------------------------- */
34965 +/*
34966 + * functions for removing a whiteout
34967 + */
34968 +
34969 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34970 +{
34971 +       int err, force;
34972 +       struct inode *delegated;
34973 +
34974 +       /*
34975 +        * forces superio when the dir has a sticky bit.
34976 +        * this may be a violation of unix fs semantics.
34977 +        */
34978 +       force = (h_dir->i_mode & S_ISVTX)
34979 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34980 +       delegated = NULL;
34981 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
34982 +       if (unlikely(err == -EWOULDBLOCK)) {
34983 +               pr_warn("cannot retry for NFSv4 delegation"
34984 +                       " for an internal unlink\n");
34985 +               iput(delegated);
34986 +       }
34987 +       return err;
34988 +}
34989 +
34990 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34991 +                       struct dentry *dentry)
34992 +{
34993 +       int err;
34994 +
34995 +       err = do_unlink_wh(h_dir, h_path);
34996 +       if (!err && dentry)
34997 +               au_set_dbwh(dentry, -1);
34998 +
34999 +       return err;
35000 +}
35001 +
35002 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35003 +{
35004 +       int err;
35005 +       struct path h_path;
35006 +
35007 +       err = 0;
35008 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35009 +       if (IS_ERR(h_path.dentry))
35010 +               err = PTR_ERR(h_path.dentry);
35011 +       else {
35012 +               if (d_is_reg(h_path.dentry)) {
35013 +                       h_path.mnt = h_ppath->mnt;
35014 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35015 +               }
35016 +               dput(h_path.dentry);
35017 +       }
35018 +
35019 +       return err;
35020 +}
35021 +
35022 +/* ---------------------------------------------------------------------- */
35023 +/*
35024 + * initialize/clean whiteout for a branch
35025 + */
35026 +
35027 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35028 +                       const int isdir)
35029 +{
35030 +       int err;
35031 +       struct inode *delegated;
35032 +
35033 +       if (d_is_negative(whpath->dentry))
35034 +               return;
35035 +
35036 +       if (isdir)
35037 +               err = vfsub_rmdir(h_dir, whpath);
35038 +       else {
35039 +               delegated = NULL;
35040 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35041 +               if (unlikely(err == -EWOULDBLOCK)) {
35042 +                       pr_warn("cannot retry for NFSv4 delegation"
35043 +                               " for an internal unlink\n");
35044 +                       iput(delegated);
35045 +               }
35046 +       }
35047 +       if (unlikely(err))
35048 +               pr_warn("failed removing %pd (%d), ignored.\n",
35049 +                       whpath->dentry, err);
35050 +}
35051 +
35052 +static int test_linkable(struct dentry *h_root)
35053 +{
35054 +       struct inode *h_dir = d_inode(h_root);
35055 +
35056 +       if (h_dir->i_op->link)
35057 +               return 0;
35058 +
35059 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35060 +              h_root, au_sbtype(h_root->d_sb));
35061 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35062 +}
35063 +
35064 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35065 +static int au_whdir(struct inode *h_dir, struct path *path)
35066 +{
35067 +       int err;
35068 +
35069 +       err = -EEXIST;
35070 +       if (d_is_negative(path->dentry)) {
35071 +               int mode = 0700;
35072 +
35073 +               if (au_test_nfs(path->dentry->d_sb))
35074 +                       mode |= 0111;
35075 +               err = vfsub_mkdir(h_dir, path, mode);
35076 +       } else if (d_is_dir(path->dentry))
35077 +               err = 0;
35078 +       else
35079 +               pr_err("unknown %pd exists\n", path->dentry);
35080 +
35081 +       return err;
35082 +}
35083 +
35084 +struct au_wh_base {
35085 +       const struct qstr *name;
35086 +       struct dentry *dentry;
35087 +};
35088 +
35089 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35090 +                         struct path *h_path)
35091 +{
35092 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35093 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35094 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35095 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35096 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35097 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35098 +}
35099 +
35100 +/*
35101 + * returns tri-state,
35102 + * minus: error, caller should print the message
35103 + * zero: success
35104 + * plus: error, caller should NOT print the message
35105 + */
35106 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35107 +                               int do_plink, struct au_wh_base base[],
35108 +                               struct path *h_path)
35109 +{
35110 +       int err;
35111 +       struct inode *h_dir;
35112 +
35113 +       h_dir = d_inode(h_root);
35114 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35115 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35116 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35117 +       if (do_plink) {
35118 +               err = test_linkable(h_root);
35119 +               if (unlikely(err)) {
35120 +                       err = 1;
35121 +                       goto out;
35122 +               }
35123 +
35124 +               err = au_whdir(h_dir, h_path);
35125 +               if (unlikely(err))
35126 +                       goto out;
35127 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35128 +       } else
35129 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35130 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35131 +       err = au_whdir(h_dir, h_path);
35132 +       if (unlikely(err))
35133 +               goto out;
35134 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35135 +
35136 +out:
35137 +       return err;
35138 +}
35139 +
35140 +/*
35141 + * for the moment, aufs supports the branch filesystem which does not support
35142 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35143 + * copyup failed. finally, such filesystem will not be used as the writable
35144 + * branch.
35145 + *
35146 + * returns tri-state, see above.
35147 + */
35148 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35149 +                        int do_plink, struct au_wh_base base[],
35150 +                        struct path *h_path)
35151 +{
35152 +       int err;
35153 +       struct inode *h_dir;
35154 +
35155 +       WbrWhMustWriteLock(wbr);
35156 +
35157 +       err = test_linkable(h_root);
35158 +       if (unlikely(err)) {
35159 +               err = 1;
35160 +               goto out;
35161 +       }
35162 +
35163 +       /*
35164 +        * todo: should this create be done in /sbin/mount.aufs helper?
35165 +        */
35166 +       err = -EEXIST;
35167 +       h_dir = d_inode(h_root);
35168 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35169 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35170 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35171 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35172 +               err = 0;
35173 +       else
35174 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35175 +       if (unlikely(err))
35176 +               goto out;
35177 +
35178 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35179 +       if (do_plink) {
35180 +               err = au_whdir(h_dir, h_path);
35181 +               if (unlikely(err))
35182 +                       goto out;
35183 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35184 +       } else
35185 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35186 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35187 +
35188 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35189 +       err = au_whdir(h_dir, h_path);
35190 +       if (unlikely(err))
35191 +               goto out;
35192 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35193 +
35194 +out:
35195 +       return err;
35196 +}
35197 +
35198 +/*
35199 + * initialize the whiteout base file/dir for @br.
35200 + */
35201 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35202 +{
35203 +       int err, i;
35204 +       const unsigned char do_plink
35205 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35206 +       struct inode *h_dir;
35207 +       struct path path = br->br_path;
35208 +       struct dentry *h_root = path.dentry;
35209 +       struct au_wbr *wbr = br->br_wbr;
35210 +       static const struct qstr base_name[] = {
35211 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35212 +                                         sizeof(AUFS_BASE_NAME) - 1),
35213 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35214 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35215 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35216 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35217 +       };
35218 +       struct au_wh_base base[] = {
35219 +               [AuBrWh_BASE] = {
35220 +                       .name   = base_name + AuBrWh_BASE,
35221 +                       .dentry = NULL
35222 +               },
35223 +               [AuBrWh_PLINK] = {
35224 +                       .name   = base_name + AuBrWh_PLINK,
35225 +                       .dentry = NULL
35226 +               },
35227 +               [AuBrWh_ORPH] = {
35228 +                       .name   = base_name + AuBrWh_ORPH,
35229 +                       .dentry = NULL
35230 +               }
35231 +       };
35232 +
35233 +       if (wbr)
35234 +               WbrWhMustWriteLock(wbr);
35235 +
35236 +       for (i = 0; i < AuBrWh_Last; i++) {
35237 +               /* doubly whiteouted */
35238 +               struct dentry *d;
35239 +
35240 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35241 +               err = PTR_ERR(d);
35242 +               if (IS_ERR(d))
35243 +                       goto out;
35244 +
35245 +               base[i].dentry = d;
35246 +               AuDebugOn(wbr
35247 +                         && wbr->wbr_wh[i]
35248 +                         && wbr->wbr_wh[i] != base[i].dentry);
35249 +       }
35250 +
35251 +       if (wbr)
35252 +               for (i = 0; i < AuBrWh_Last; i++) {
35253 +                       dput(wbr->wbr_wh[i]);
35254 +                       wbr->wbr_wh[i] = NULL;
35255 +               }
35256 +
35257 +       err = 0;
35258 +       if (!au_br_writable(br->br_perm)) {
35259 +               h_dir = d_inode(h_root);
35260 +               au_wh_init_ro(h_dir, base, &path);
35261 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35262 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35263 +               if (err > 0)
35264 +                       goto out;
35265 +               else if (err)
35266 +                       goto out_err;
35267 +       } else {
35268 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35269 +               if (err > 0)
35270 +                       goto out;
35271 +               else if (err)
35272 +                       goto out_err;
35273 +       }
35274 +       goto out; /* success */
35275 +
35276 +out_err:
35277 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35278 +              err, h_root, au_sbtype(h_root->d_sb));
35279 +out:
35280 +       for (i = 0; i < AuBrWh_Last; i++)
35281 +               dput(base[i].dentry);
35282 +       return err;
35283 +}
35284 +
35285 +/* ---------------------------------------------------------------------- */
35286 +/*
35287 + * whiteouts are all hard-linked usually.
35288 + * when its link count reaches a ceiling, we create a new whiteout base
35289 + * asynchronously.
35290 + */
35291 +
35292 +struct reinit_br_wh {
35293 +       struct super_block *sb;
35294 +       struct au_branch *br;
35295 +};
35296 +
35297 +static void reinit_br_wh(void *arg)
35298 +{
35299 +       int err;
35300 +       aufs_bindex_t bindex;
35301 +       struct path h_path;
35302 +       struct reinit_br_wh *a = arg;
35303 +       struct au_wbr *wbr;
35304 +       struct inode *dir, *delegated;
35305 +       struct dentry *h_root;
35306 +       struct au_hinode *hdir;
35307 +
35308 +       err = 0;
35309 +       wbr = a->br->br_wbr;
35310 +       /* big aufs lock */
35311 +       si_noflush_write_lock(a->sb);
35312 +       if (!au_br_writable(a->br->br_perm))
35313 +               goto out;
35314 +       bindex = au_br_index(a->sb, a->br->br_id);
35315 +       if (unlikely(bindex < 0))
35316 +               goto out;
35317 +
35318 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35319 +       dir = d_inode(a->sb->s_root);
35320 +       hdir = au_hi(dir, bindex);
35321 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35322 +       AuDebugOn(h_root != au_br_dentry(a->br));
35323 +
35324 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35325 +       wbr_wh_write_lock(wbr);
35326 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35327 +                         h_root, a->br);
35328 +       if (!err) {
35329 +               h_path.dentry = wbr->wbr_whbase;
35330 +               h_path.mnt = au_br_mnt(a->br);
35331 +               delegated = NULL;
35332 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35333 +                                  /*force*/0);
35334 +               if (unlikely(err == -EWOULDBLOCK)) {
35335 +                       pr_warn("cannot retry for NFSv4 delegation"
35336 +                               " for an internal unlink\n");
35337 +                       iput(delegated);
35338 +               }
35339 +       } else {
35340 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35341 +               err = 0;
35342 +       }
35343 +       dput(wbr->wbr_whbase);
35344 +       wbr->wbr_whbase = NULL;
35345 +       if (!err)
35346 +               err = au_wh_init(a->br, a->sb);
35347 +       wbr_wh_write_unlock(wbr);
35348 +       au_hn_inode_unlock(hdir);
35349 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35350 +       if (!err)
35351 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35352 +
35353 +out:
35354 +       if (wbr)
35355 +               atomic_dec(&wbr->wbr_wh_running);
35356 +       au_lcnt_dec(&a->br->br_count);
35357 +       si_write_unlock(a->sb);
35358 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35359 +       au_kfree_rcu(a);
35360 +       if (unlikely(err))
35361 +               AuIOErr("err %d\n", err);
35362 +}
35363 +
35364 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35365 +{
35366 +       int do_dec, wkq_err;
35367 +       struct reinit_br_wh *arg;
35368 +
35369 +       do_dec = 1;
35370 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35371 +               goto out;
35372 +
35373 +       /* ignore ENOMEM */
35374 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35375 +       if (arg) {
35376 +               /*
35377 +                * dec(wh_running), kfree(arg) and dec(br_count)
35378 +                * in reinit function
35379 +                */
35380 +               arg->sb = sb;
35381 +               arg->br = br;
35382 +               au_lcnt_inc(&br->br_count);
35383 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35384 +               if (unlikely(wkq_err)) {
35385 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35386 +                       au_lcnt_dec(&br->br_count);
35387 +                       au_kfree_rcu(arg);
35388 +               }
35389 +               do_dec = 0;
35390 +       }
35391 +
35392 +out:
35393 +       if (do_dec)
35394 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35395 +}
35396 +
35397 +/* ---------------------------------------------------------------------- */
35398 +
35399 +/*
35400 + * create the whiteout @wh.
35401 + */
35402 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35403 +                            struct dentry *wh)
35404 +{
35405 +       int err;
35406 +       struct path h_path = {
35407 +               .dentry = wh
35408 +       };
35409 +       struct au_branch *br;
35410 +       struct au_wbr *wbr;
35411 +       struct dentry *h_parent;
35412 +       struct inode *h_dir, *delegated;
35413 +
35414 +       h_parent = wh->d_parent; /* dir inode is locked */
35415 +       h_dir = d_inode(h_parent);
35416 +       IMustLock(h_dir);
35417 +
35418 +       br = au_sbr(sb, bindex);
35419 +       h_path.mnt = au_br_mnt(br);
35420 +       wbr = br->br_wbr;
35421 +       wbr_wh_read_lock(wbr);
35422 +       if (wbr->wbr_whbase) {
35423 +               delegated = NULL;
35424 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35425 +               if (unlikely(err == -EWOULDBLOCK)) {
35426 +                       pr_warn("cannot retry for NFSv4 delegation"
35427 +                               " for an internal link\n");
35428 +                       iput(delegated);
35429 +               }
35430 +               if (!err || err != -EMLINK)
35431 +                       goto out;
35432 +
35433 +               /* link count full. re-initialize br_whbase. */
35434 +               kick_reinit_br_wh(sb, br);
35435 +       }
35436 +
35437 +       /* return this error in this context */
35438 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35439 +       if (!err)
35440 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35441 +
35442 +out:
35443 +       wbr_wh_read_unlock(wbr);
35444 +       return err;
35445 +}
35446 +
35447 +/* ---------------------------------------------------------------------- */
35448 +
35449 +/*
35450 + * create or remove the diropq.
35451 + */
35452 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35453 +                               unsigned int flags)
35454 +{
35455 +       struct dentry *opq_dentry;
35456 +       struct super_block *sb;
35457 +       struct au_branch *br;
35458 +       struct path h_path;
35459 +       int err;
35460 +
35461 +       sb = dentry->d_sb;
35462 +       br = au_sbr(sb, bindex);
35463 +       h_path.dentry = au_h_dptr(dentry, bindex);
35464 +       h_path.mnt = au_br_mnt(br);
35465 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35466 +       if (IS_ERR(opq_dentry))
35467 +               goto out;
35468 +
35469 +       if (au_ftest_diropq(flags, CREATE)) {
35470 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35471 +               if (!err) {
35472 +                       au_set_dbdiropq(dentry, bindex);
35473 +                       goto out; /* success */
35474 +               }
35475 +       } else {
35476 +               h_path.dentry = opq_dentry;
35477 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35478 +               if (!err)
35479 +                       au_set_dbdiropq(dentry, -1);
35480 +       }
35481 +       dput(opq_dentry);
35482 +       opq_dentry = ERR_PTR(err);
35483 +
35484 +out:
35485 +       return opq_dentry;
35486 +}
35487 +
35488 +struct do_diropq_args {
35489 +       struct dentry **errp;
35490 +       struct dentry *dentry;
35491 +       aufs_bindex_t bindex;
35492 +       unsigned int flags;
35493 +};
35494 +
35495 +static void call_do_diropq(void *args)
35496 +{
35497 +       struct do_diropq_args *a = args;
35498 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35499 +}
35500 +
35501 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35502 +                            unsigned int flags)
35503 +{
35504 +       struct dentry *diropq, *h_dentry;
35505 +       struct user_namespace *h_userns;
35506 +
35507 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35508 +       h_dentry = au_h_dptr(dentry, bindex);
35509 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35510 +                               MAY_EXEC | MAY_WRITE))
35511 +               diropq = do_diropq(dentry, bindex, flags);
35512 +       else {
35513 +               int wkq_err;
35514 +               struct do_diropq_args args = {
35515 +                       .errp           = &diropq,
35516 +                       .dentry         = dentry,
35517 +                       .bindex         = bindex,
35518 +                       .flags          = flags
35519 +               };
35520 +
35521 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35522 +               if (unlikely(wkq_err))
35523 +                       diropq = ERR_PTR(wkq_err);
35524 +       }
35525 +
35526 +       return diropq;
35527 +}
35528 +
35529 +/* ---------------------------------------------------------------------- */
35530 +
35531 +/*
35532 + * lookup whiteout dentry.
35533 + * @h_parent: lower parent dentry which must exist and be locked
35534 + * @base_name: name of dentry which will be whiteouted
35535 + * returns dentry for whiteout.
35536 + */
35537 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35538 +                         struct au_branch *br)
35539 +{
35540 +       int err;
35541 +       struct qstr wh_name;
35542 +       struct dentry *wh_dentry;
35543 +       struct path h_path;
35544 +
35545 +       err = au_wh_name_alloc(&wh_name, base_name);
35546 +       wh_dentry = ERR_PTR(err);
35547 +       if (!err) {
35548 +               h_path.dentry = h_parent;
35549 +               h_path.mnt = au_br_mnt(br);
35550 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35551 +               au_kfree_try_rcu(wh_name.name);
35552 +       }
35553 +       return wh_dentry;
35554 +}
35555 +
35556 +/*
35557 + * link/create a whiteout for @dentry on @bindex.
35558 + */
35559 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35560 +                           struct dentry *h_parent)
35561 +{
35562 +       struct dentry *wh_dentry;
35563 +       struct super_block *sb;
35564 +       int err;
35565 +
35566 +       sb = dentry->d_sb;
35567 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35568 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35569 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35570 +               if (!err) {
35571 +                       au_set_dbwh(dentry, bindex);
35572 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35573 +               } else {
35574 +                       dput(wh_dentry);
35575 +                       wh_dentry = ERR_PTR(err);
35576 +               }
35577 +       }
35578 +
35579 +       return wh_dentry;
35580 +}
35581 +
35582 +/* ---------------------------------------------------------------------- */
35583 +
35584 +/* Delete all whiteouts in this directory on branch bindex. */
35585 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35586 +                          aufs_bindex_t bindex)
35587 +{
35588 +       int err;
35589 +       unsigned long ul, n;
35590 +       struct qstr wh_name;
35591 +       char *p;
35592 +       struct hlist_head *head;
35593 +       struct au_vdir_wh *pos;
35594 +       struct au_vdir_destr *str;
35595 +
35596 +       err = -ENOMEM;
35597 +       p = (void *)__get_free_page(GFP_NOFS);
35598 +       wh_name.name = p;
35599 +       if (unlikely(!wh_name.name))
35600 +               goto out;
35601 +
35602 +       err = 0;
35603 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35604 +       p += AUFS_WH_PFX_LEN;
35605 +       n = whlist->nh_num;
35606 +       head = whlist->nh_head;
35607 +       for (ul = 0; !err && ul < n; ul++, head++) {
35608 +               hlist_for_each_entry(pos, head, wh_hash) {
35609 +                       if (pos->wh_bindex != bindex)
35610 +                               continue;
35611 +
35612 +                       str = &pos->wh_str;
35613 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35614 +                               memcpy(p, str->name, str->len);
35615 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35616 +                               err = unlink_wh_name(h_path, &wh_name);
35617 +                               if (!err)
35618 +                                       continue;
35619 +                               break;
35620 +                       }
35621 +                       AuIOErr("whiteout name too long %.*s\n",
35622 +                               str->len, str->name);
35623 +                       err = -EIO;
35624 +                       break;
35625 +               }
35626 +       }
35627 +       free_page((unsigned long)wh_name.name);
35628 +
35629 +out:
35630 +       return err;
35631 +}
35632 +
35633 +struct del_wh_children_args {
35634 +       int *errp;
35635 +       struct path *h_path;
35636 +       struct au_nhash *whlist;
35637 +       aufs_bindex_t bindex;
35638 +};
35639 +
35640 +static void call_del_wh_children(void *args)
35641 +{
35642 +       struct del_wh_children_args *a = args;
35643 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35644 +}
35645 +
35646 +/* ---------------------------------------------------------------------- */
35647 +
35648 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35649 +{
35650 +       struct au_whtmp_rmdir *whtmp;
35651 +       int err;
35652 +       unsigned int rdhash;
35653 +
35654 +       SiMustAnyLock(sb);
35655 +
35656 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35657 +       if (unlikely(!whtmp)) {
35658 +               whtmp = ERR_PTR(-ENOMEM);
35659 +               goto out;
35660 +       }
35661 +
35662 +       /* no estimation for dir size */
35663 +       rdhash = au_sbi(sb)->si_rdhash;
35664 +       if (!rdhash)
35665 +               rdhash = AUFS_RDHASH_DEF;
35666 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35667 +       if (unlikely(err)) {
35668 +               au_kfree_rcu(whtmp);
35669 +               whtmp = ERR_PTR(err);
35670 +       }
35671 +
35672 +out:
35673 +       return whtmp;
35674 +}
35675 +
35676 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35677 +{
35678 +       if (whtmp->br)
35679 +               au_lcnt_dec(&whtmp->br->br_count);
35680 +       dput(whtmp->wh_dentry);
35681 +       iput(whtmp->dir);
35682 +       au_nhash_wh_free(&whtmp->whlist);
35683 +       au_kfree_rcu(whtmp);
35684 +}
35685 +
35686 +/*
35687 + * rmdir the whiteouted temporary named dir @h_dentry.
35688 + * @whlist: whiteouted children.
35689 + */
35690 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35691 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35692 +{
35693 +       int err;
35694 +       unsigned int h_nlink;
35695 +       struct path wh_path;
35696 +       struct inode *wh_inode, *h_dir;
35697 +       struct au_branch *br;
35698 +       struct user_namespace *h_userns;
35699 +
35700 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35701 +       IMustLock(h_dir);
35702 +
35703 +       br = au_sbr(dir->i_sb, bindex);
35704 +       wh_path.dentry = wh_dentry;
35705 +       wh_path.mnt = au_br_mnt(br);
35706 +       h_userns = au_br_userns(br);
35707 +       wh_inode = d_inode(wh_dentry);
35708 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35709 +
35710 +       /*
35711 +        * someone else might change some whiteouts while we were sleeping.
35712 +        * it means this whlist may have an obsoleted entry.
35713 +        */
35714 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35715 +               err = del_wh_children(&wh_path, whlist, bindex);
35716 +       else {
35717 +               int wkq_err;
35718 +               struct del_wh_children_args args = {
35719 +                       .errp           = &err,
35720 +                       .h_path         = &wh_path,
35721 +                       .whlist         = whlist,
35722 +                       .bindex         = bindex
35723 +               };
35724 +
35725 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35726 +               if (unlikely(wkq_err))
35727 +                       err = wkq_err;
35728 +       }
35729 +       inode_unlock(wh_inode);
35730 +
35731 +       if (!err) {
35732 +               h_nlink = h_dir->i_nlink;
35733 +               err = vfsub_rmdir(h_dir, &wh_path);
35734 +               /* some fs doesn't change the parent nlink in some cases */
35735 +               h_nlink -= h_dir->i_nlink;
35736 +       }
35737 +
35738 +       if (!err) {
35739 +               if (au_ibtop(dir) == bindex) {
35740 +                       /* todo: dir->i_mutex is necessary */
35741 +                       au_cpup_attr_timesizes(dir);
35742 +                       if (h_nlink)
35743 +                               vfsub_drop_nlink(dir);
35744 +               }
35745 +               return 0; /* success */
35746 +       }
35747 +
35748 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35749 +       return err;
35750 +}
35751 +
35752 +static void call_rmdir_whtmp(void *args)
35753 +{
35754 +       int err;
35755 +       aufs_bindex_t bindex;
35756 +       struct au_whtmp_rmdir *a = args;
35757 +       struct super_block *sb;
35758 +       struct dentry *h_parent;
35759 +       struct inode *h_dir;
35760 +       struct au_hinode *hdir;
35761 +
35762 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35763 +       /* inode_lock(a->dir); */
35764 +       err = -EROFS;
35765 +       sb = a->dir->i_sb;
35766 +       si_read_lock(sb, !AuLock_FLUSH);
35767 +       if (!au_br_writable(a->br->br_perm))
35768 +               goto out;
35769 +       bindex = au_br_index(sb, a->br->br_id);
35770 +       if (unlikely(bindex < 0))
35771 +               goto out;
35772 +
35773 +       err = -EIO;
35774 +       ii_write_lock_parent(a->dir);
35775 +       h_parent = dget_parent(a->wh_dentry);
35776 +       h_dir = d_inode(h_parent);
35777 +       hdir = au_hi(a->dir, bindex);
35778 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35779 +       if (unlikely(err))
35780 +               goto out_mnt;
35781 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35782 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35783 +                         a->br);
35784 +       if (!err)
35785 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35786 +       au_hn_inode_unlock(hdir);
35787 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35788 +
35789 +out_mnt:
35790 +       dput(h_parent);
35791 +       ii_write_unlock(a->dir);
35792 +out:
35793 +       /* inode_unlock(a->dir); */
35794 +       au_whtmp_rmdir_free(a);
35795 +       si_read_unlock(sb);
35796 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35797 +       if (unlikely(err))
35798 +               AuIOErr("err %d\n", err);
35799 +}
35800 +
35801 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35802 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35803 +{
35804 +       int wkq_err;
35805 +       struct super_block *sb;
35806 +
35807 +       IMustLock(dir);
35808 +
35809 +       /* all post-process will be done in do_rmdir_whtmp(). */
35810 +       sb = dir->i_sb;
35811 +       args->dir = au_igrab(dir);
35812 +       args->br = au_sbr(sb, bindex);
35813 +       au_lcnt_inc(&args->br->br_count);
35814 +       args->wh_dentry = dget(wh_dentry);
35815 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35816 +       if (unlikely(wkq_err)) {
35817 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35818 +               au_whtmp_rmdir_free(args);
35819 +       }
35820 +}
35821 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35822 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35823 +++ linux/fs/aufs/whout.h       2021-12-03 15:40:58.236647297 +0100
35824 @@ -0,0 +1,87 @@
35825 +/* SPDX-License-Identifier: GPL-2.0 */
35826 +/*
35827 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35828 + *
35829 + * This program, aufs is free software; you can redistribute it and/or modify
35830 + * it under the terms of the GNU General Public License as published by
35831 + * the Free Software Foundation; either version 2 of the License, or
35832 + * (at your option) any later version.
35833 + *
35834 + * This program is distributed in the hope that it will be useful,
35835 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35836 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35837 + * GNU General Public License for more details.
35838 + *
35839 + * You should have received a copy of the GNU General Public License
35840 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35841 + */
35842 +
35843 +/*
35844 + * whiteout for logical deletion and opaque directory
35845 + */
35846 +
35847 +#ifndef __AUFS_WHOUT_H__
35848 +#define __AUFS_WHOUT_H__
35849 +
35850 +#ifdef __KERNEL__
35851 +
35852 +#include "dir.h"
35853 +
35854 +/* whout.c */
35855 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35856 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35857 +              struct qstr *wh_name, int try_sio);
35858 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
35859 +struct au_branch;
35860 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35861 +                            struct qstr *prefix);
35862 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35863 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35864 +                       struct dentry *dentry);
35865 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35866 +
35867 +/* diropq flags */
35868 +#define AuDiropq_CREATE        1
35869 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35870 +#define au_fset_diropq(flags, name) \
35871 +       do { (flags) |= AuDiropq_##name; } while (0)
35872 +#define au_fclr_diropq(flags, name) \
35873 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35874 +
35875 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35876 +                            unsigned int flags);
35877 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35878 +                         struct au_branch *br);
35879 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35880 +                           struct dentry *h_parent);
35881 +
35882 +/* real rmdir for the whiteout-ed dir */
35883 +struct au_whtmp_rmdir {
35884 +       struct inode *dir;
35885 +       struct au_branch *br;
35886 +       struct dentry *wh_dentry;
35887 +       struct au_nhash whlist;
35888 +};
35889 +
35890 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35891 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35892 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35893 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35894 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35895 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35896 +
35897 +/* ---------------------------------------------------------------------- */
35898 +
35899 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35900 +                                             aufs_bindex_t bindex)
35901 +{
35902 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35903 +}
35904 +
35905 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35906 +{
35907 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35908 +}
35909 +
35910 +#endif /* __KERNEL__ */
35911 +#endif /* __AUFS_WHOUT_H__ */
35912 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35913 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35914 +++ linux/fs/aufs/wkq.c 2021-12-03 15:38:59.943313977 +0100
35915 @@ -0,0 +1,372 @@
35916 +// SPDX-License-Identifier: GPL-2.0
35917 +/*
35918 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35919 + *
35920 + * This program, aufs is free software; you can redistribute it and/or modify
35921 + * it under the terms of the GNU General Public License as published by
35922 + * the Free Software Foundation; either version 2 of the License, or
35923 + * (at your option) any later version.
35924 + *
35925 + * This program is distributed in the hope that it will be useful,
35926 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35927 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35928 + * GNU General Public License for more details.
35929 + *
35930 + * You should have received a copy of the GNU General Public License
35931 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35932 + */
35933 +
35934 +/*
35935 + * workqueue for asynchronous/super-io operations
35936 + * todo: try new credential scheme
35937 + */
35938 +
35939 +#include <linux/module.h>
35940 +#include "aufs.h"
35941 +
35942 +/* internal workqueue named AUFS_WKQ_NAME */
35943 +
35944 +static struct workqueue_struct *au_wkq;
35945 +
35946 +struct au_wkinfo {
35947 +       struct work_struct wk;
35948 +       struct kobject *kobj;
35949 +
35950 +       unsigned int flags; /* see wkq.h */
35951 +
35952 +       au_wkq_func_t func;
35953 +       void *args;
35954 +
35955 +#ifdef CONFIG_LOCKDEP
35956 +       int dont_check;
35957 +       struct held_lock **hlock;
35958 +#endif
35959 +
35960 +       struct completion *comp;
35961 +};
35962 +
35963 +/* ---------------------------------------------------------------------- */
35964 +/*
35965 + * Aufs passes some operations to the workqueue such as the internal copyup.
35966 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35967 + * job run by workqueue depends upon the locks acquired in the other task.
35968 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35969 + * information too. And the job in the workqueue restores the info in order to
35970 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35971 + * correctly and expectedly.
35972 + */
35973 +
35974 +#ifndef CONFIG_LOCKDEP
35975 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35976 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35977 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35978 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35979 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35980 +#else
35981 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35982 +{
35983 +       wkinfo->hlock = NULL;
35984 +       wkinfo->dont_check = 0;
35985 +}
35986 +
35987 +/*
35988 + * 1: matched
35989 + * 0: unmatched
35990 + */
35991 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35992 +{
35993 +       static DEFINE_SPINLOCK(spin);
35994 +       static struct {
35995 +               char *name;
35996 +               struct lock_class_key *key;
35997 +       } a[] = {
35998 +               { .name = "&sbinfo->si_rwsem" },
35999 +               { .name = "&finfo->fi_rwsem" },
36000 +               { .name = "&dinfo->di_rwsem" },
36001 +               { .name = "&iinfo->ii_rwsem" }
36002 +       };
36003 +       static int set;
36004 +       int i;
36005 +
36006 +       /* lockless read from 'set.' see below */
36007 +       if (set == ARRAY_SIZE(a)) {
36008 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36009 +                       if (a[i].key == key)
36010 +                               goto match;
36011 +               goto unmatch;
36012 +       }
36013 +
36014 +       spin_lock(&spin);
36015 +       if (set)
36016 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36017 +                       if (a[i].key == key) {
36018 +                               spin_unlock(&spin);
36019 +                               goto match;
36020 +                       }
36021 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36022 +               if (a[i].key) {
36023 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36024 +                               spin_unlock(&spin);
36025 +                               goto match;
36026 +                       } else
36027 +                               continue;
36028 +               }
36029 +               if (strstr(a[i].name, name)) {
36030 +                       /*
36031 +                        * the order of these three lines is important for the
36032 +                        * lockless read above.
36033 +                        */
36034 +                       a[i].key = key;
36035 +                       spin_unlock(&spin);
36036 +                       set++;
36037 +                       /* AuDbg("%d, %s\n", set, name); */
36038 +                       goto match;
36039 +               }
36040 +       }
36041 +       spin_unlock(&spin);
36042 +       goto unmatch;
36043 +
36044 +match:
36045 +       return 1;
36046 +unmatch:
36047 +       return 0;
36048 +}
36049 +
36050 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36051 +{
36052 +       int err, n;
36053 +       struct task_struct *curr;
36054 +       struct held_lock **hl, *held_locks, *p;
36055 +
36056 +       err = 0;
36057 +       curr = current;
36058 +       wkinfo->dont_check = lockdep_recursing(curr);
36059 +       if (wkinfo->dont_check)
36060 +               goto out;
36061 +       n = curr->lockdep_depth;
36062 +       if (!n)
36063 +               goto out;
36064 +
36065 +       err = -ENOMEM;
36066 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36067 +       if (unlikely(!wkinfo->hlock))
36068 +               goto out;
36069 +
36070 +       err = 0;
36071 +#if 0 /* left for debugging */
36072 +       if (0 && au_debug_test())
36073 +               lockdep_print_held_locks(curr);
36074 +#endif
36075 +       held_locks = curr->held_locks;
36076 +       hl = wkinfo->hlock;
36077 +       while (n--) {
36078 +               p = held_locks++;
36079 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36080 +                       *hl++ = p;
36081 +       }
36082 +       *hl = NULL;
36083 +
36084 +out:
36085 +       return err;
36086 +}
36087 +
36088 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36089 +{
36090 +       au_kfree_try_rcu(wkinfo->hlock);
36091 +}
36092 +
36093 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36094 +{
36095 +       struct held_lock *p, **hl = wkinfo->hlock;
36096 +       int subclass;
36097 +
36098 +       if (wkinfo->dont_check)
36099 +               lockdep_off();
36100 +       if (!hl)
36101 +               return;
36102 +       while ((p = *hl++)) { /* assignment */
36103 +               subclass = lockdep_hlock_class(p)->subclass;
36104 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36105 +               if (p->read)
36106 +                       rwsem_acquire_read(p->instance, subclass, 0,
36107 +                                          /*p->acquire_ip*/_RET_IP_);
36108 +               else
36109 +                       rwsem_acquire(p->instance, subclass, 0,
36110 +                                     /*p->acquire_ip*/_RET_IP_);
36111 +       }
36112 +}
36113 +
36114 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36115 +{
36116 +       struct held_lock *p, **hl = wkinfo->hlock;
36117 +
36118 +       if (wkinfo->dont_check)
36119 +               lockdep_on();
36120 +       if (!hl)
36121 +               return;
36122 +       while ((p = *hl++)) /* assignment */
36123 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36124 +}
36125 +#endif
36126 +
36127 +static void wkq_func(struct work_struct *wk)
36128 +{
36129 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36130 +
36131 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36132 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36133 +
36134 +       au_wkq_lockdep_pre(wkinfo);
36135 +       wkinfo->func(wkinfo->args);
36136 +       au_wkq_lockdep_post(wkinfo);
36137 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36138 +               complete(wkinfo->comp);
36139 +       else {
36140 +               kobject_put(wkinfo->kobj);
36141 +               module_put(THIS_MODULE); /* todo: ?? */
36142 +               au_kfree_rcu(wkinfo);
36143 +       }
36144 +}
36145 +
36146 +/*
36147 + * Since struct completion is large, try allocating it dynamically.
36148 + */
36149 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36150 +
36151 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36152 +{
36153 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36154 +       if (*comp) {
36155 +               init_completion(*comp);
36156 +               wkinfo->comp = *comp;
36157 +               return 0;
36158 +       }
36159 +       return -ENOMEM;
36160 +}
36161 +
36162 +static void au_wkq_comp_free(struct completion *comp)
36163 +{
36164 +       au_kfree_rcu(comp);
36165 +}
36166 +
36167 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36168 +{
36169 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36170 +               if (au_wkq_test()) {
36171 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36172 +                               " due to a dead dir by UDBA,"
36173 +                               " or async xino write?\n");
36174 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36175 +               }
36176 +       } else
36177 +               au_dbg_verify_kthread();
36178 +
36179 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36180 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36181 +               queue_work(au_wkq, &wkinfo->wk);
36182 +       } else {
36183 +               INIT_WORK(&wkinfo->wk, wkq_func);
36184 +               schedule_work(&wkinfo->wk);
36185 +       }
36186 +}
36187 +
36188 +/*
36189 + * Be careful. It is easy to make deadlock happen.
36190 + * processA: lock, wkq and wait
36191 + * processB: wkq and wait, lock in wkq
36192 + * --> deadlock
36193 + */
36194 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36195 +{
36196 +       int err;
36197 +       AuWkqCompDeclare(comp);
36198 +       struct au_wkinfo wkinfo = {
36199 +               .flags  = flags,
36200 +               .func   = func,
36201 +               .args   = args
36202 +       };
36203 +
36204 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36205 +       if (unlikely(err))
36206 +               goto out;
36207 +       err = au_wkq_lockdep_alloc(&wkinfo);
36208 +       if (unlikely(err))
36209 +               goto out_comp;
36210 +       if (!err) {
36211 +               au_wkq_run(&wkinfo);
36212 +               /* no timeout, no interrupt */
36213 +               wait_for_completion(wkinfo.comp);
36214 +       }
36215 +       au_wkq_lockdep_free(&wkinfo);
36216 +
36217 +out_comp:
36218 +       au_wkq_comp_free(comp);
36219 +out:
36220 +       destroy_work_on_stack(&wkinfo.wk);
36221 +       return err;
36222 +}
36223 +
36224 +/*
36225 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36226 + * problem in a concurrent umounting.
36227 + */
36228 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36229 +                 unsigned int flags)
36230 +{
36231 +       int err;
36232 +       struct au_wkinfo *wkinfo;
36233 +
36234 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36235 +
36236 +       /*
36237 +        * wkq_func() must free this wkinfo.
36238 +        * it highly depends upon the implementation of workqueue.
36239 +        */
36240 +       err = 0;
36241 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36242 +       if (wkinfo) {
36243 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36244 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36245 +               wkinfo->func = func;
36246 +               wkinfo->args = args;
36247 +               wkinfo->comp = NULL;
36248 +               au_wkq_lockdep_init(wkinfo);
36249 +               kobject_get(wkinfo->kobj);
36250 +               __module_get(THIS_MODULE); /* todo: ?? */
36251 +
36252 +               au_wkq_run(wkinfo);
36253 +       } else {
36254 +               err = -ENOMEM;
36255 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36256 +       }
36257 +
36258 +       return err;
36259 +}
36260 +
36261 +/* ---------------------------------------------------------------------- */
36262 +
36263 +void au_nwt_init(struct au_nowait_tasks *nwt)
36264 +{
36265 +       atomic_set(&nwt->nw_len, 0);
36266 +       /* smp_mb(); */ /* atomic_set */
36267 +       init_waitqueue_head(&nwt->nw_wq);
36268 +}
36269 +
36270 +void au_wkq_fin(void)
36271 +{
36272 +       destroy_workqueue(au_wkq);
36273 +}
36274 +
36275 +int __init au_wkq_init(void)
36276 +{
36277 +       int err;
36278 +
36279 +       err = 0;
36280 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36281 +       if (IS_ERR(au_wkq))
36282 +               err = PTR_ERR(au_wkq);
36283 +       else if (!au_wkq)
36284 +               err = -ENOMEM;
36285 +
36286 +       return err;
36287 +}
36288 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36289 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36290 +++ linux/fs/aufs/wkq.h 2021-12-03 15:38:59.943313977 +0100
36291 @@ -0,0 +1,89 @@
36292 +/* SPDX-License-Identifier: GPL-2.0 */
36293 +/*
36294 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36295 + *
36296 + * This program, aufs is free software; you can redistribute it and/or modify
36297 + * it under the terms of the GNU General Public License as published by
36298 + * the Free Software Foundation; either version 2 of the License, or
36299 + * (at your option) any later version.
36300 + *
36301 + * This program is distributed in the hope that it will be useful,
36302 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36303 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36304 + * GNU General Public License for more details.
36305 + *
36306 + * You should have received a copy of the GNU General Public License
36307 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36308 + */
36309 +
36310 +/*
36311 + * workqueue for asynchronous/super-io operations
36312 + * todo: try new credentials management scheme
36313 + */
36314 +
36315 +#ifndef __AUFS_WKQ_H__
36316 +#define __AUFS_WKQ_H__
36317 +
36318 +#ifdef __KERNEL__
36319 +
36320 +#include <linux/wait.h>
36321 +
36322 +struct super_block;
36323 +
36324 +/* ---------------------------------------------------------------------- */
36325 +
36326 +/*
36327 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36328 + */
36329 +struct au_nowait_tasks {
36330 +       atomic_t                nw_len;
36331 +       wait_queue_head_t       nw_wq;
36332 +};
36333 +
36334 +/* ---------------------------------------------------------------------- */
36335 +
36336 +typedef void (*au_wkq_func_t)(void *args);
36337 +
36338 +/* wkq flags */
36339 +#define AuWkq_WAIT     1
36340 +#define AuWkq_NEST     (1 << 1)
36341 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36342 +#define au_fset_wkq(flags, name) \
36343 +       do { (flags) |= AuWkq_##name; } while (0)
36344 +#define au_fclr_wkq(flags, name) \
36345 +       do { (flags) &= ~AuWkq_##name; } while (0)
36346 +
36347 +/* wkq.c */
36348 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36349 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36350 +                 unsigned int flags);
36351 +void au_nwt_init(struct au_nowait_tasks *nwt);
36352 +int __init au_wkq_init(void);
36353 +void au_wkq_fin(void);
36354 +
36355 +/* ---------------------------------------------------------------------- */
36356 +
36357 +static inline int au_wkq_test(void)
36358 +{
36359 +       return current->flags & PF_WQ_WORKER;
36360 +}
36361 +
36362 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36363 +{
36364 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36365 +}
36366 +
36367 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36368 +{
36369 +       if (atomic_dec_and_test(&nwt->nw_len))
36370 +               wake_up_all(&nwt->nw_wq);
36371 +}
36372 +
36373 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36374 +{
36375 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36376 +       return 0;
36377 +}
36378 +
36379 +#endif /* __KERNEL__ */
36380 +#endif /* __AUFS_WKQ_H__ */
36381 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36382 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36383 +++ linux/fs/aufs/xattr.c       2021-12-03 15:40:58.236647297 +0100
36384 @@ -0,0 +1,368 @@
36385 +// SPDX-License-Identifier: GPL-2.0
36386 +/*
36387 + * Copyright (C) 2014-2021 Junjiro R. Okajima
36388 + *
36389 + * This program, aufs is free software; you can redistribute it and/or modify
36390 + * it under the terms of the GNU General Public License as published by
36391 + * the Free Software Foundation; either version 2 of the License, or
36392 + * (at your option) any later version.
36393 + *
36394 + * This program is distributed in the hope that it will be useful,
36395 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36396 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36397 + * GNU General Public License for more details.
36398 + *
36399 + * You should have received a copy of the GNU General Public License
36400 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36401 + */
36402 +
36403 +/*
36404 + * handling xattr functions
36405 + */
36406 +
36407 +#include <linux/fs.h>
36408 +#include <linux/posix_acl_xattr.h>
36409 +#include <linux/xattr.h>
36410 +#include "aufs.h"
36411 +
36412 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36413 +{
36414 +       if (!ignore_flags)
36415 +               goto out;
36416 +       switch (err) {
36417 +       case -ENOMEM:
36418 +       case -EDQUOT:
36419 +               goto out;
36420 +       }
36421 +
36422 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36423 +               err = 0;
36424 +               goto out;
36425 +       }
36426 +
36427 +#define cmp(brattr, prefix) do {                                       \
36428 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36429 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36430 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36431 +                               err = 0;                                \
36432 +                       goto out;                                       \
36433 +               }                                                       \
36434 +       } while (0)
36435 +
36436 +       cmp(SEC, SECURITY);
36437 +       cmp(SYS, SYSTEM);
36438 +       cmp(TR, TRUSTED);
36439 +       cmp(USR, USER);
36440 +#undef cmp
36441 +
36442 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36443 +               err = 0;
36444 +
36445 +out:
36446 +       return err;
36447 +}
36448 +
36449 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36450 +
36451 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36452 +                           char *name, char **buf, unsigned int ignore_flags,
36453 +                           unsigned int verbose)
36454 +{
36455 +       int err;
36456 +       ssize_t ssz;
36457 +       struct inode *h_idst;
36458 +       struct dentry *h_dst_dentry, *h_src_dentry;
36459 +       struct user_namespace *h_dst_userns, *h_src_userns;
36460 +
36461 +       h_src_userns = mnt_user_ns(h_src->mnt);
36462 +       h_src_dentry = h_src->dentry;
36463 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36464 +                                GFP_NOFS);
36465 +       err = ssz;
36466 +       if (unlikely(err <= 0)) {
36467 +               if (err == -ENODATA
36468 +                   || (err == -EOPNOTSUPP
36469 +                       && ((ignore_flags & au_xattr_out_of_list)
36470 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36471 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36472 +                                   || !strcmp(name,
36473 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36474 +                           ))
36475 +                       err = 0;
36476 +               if (err && (verbose || au_debug_test()))
36477 +                       pr_err("%s, err %d\n", name, err);
36478 +               goto out;
36479 +       }
36480 +
36481 +       /* unlock it temporary */
36482 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36483 +       h_dst_dentry = h_dst->dentry;
36484 +       h_idst = d_inode(h_dst_dentry);
36485 +       inode_unlock(h_idst);
36486 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36487 +                            /*flags*/0);
36488 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36489 +       if (unlikely(err)) {
36490 +               if (verbose || au_debug_test())
36491 +                       pr_err("%s, err %d\n", name, err);
36492 +               err = au_xattr_ignore(err, name, ignore_flags);
36493 +       }
36494 +
36495 +out:
36496 +       return err;
36497 +}
36498 +
36499 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36500 +                 unsigned int verbose)
36501 +{
36502 +       int err, unlocked, acl_access, acl_default;
36503 +       ssize_t ssz;
36504 +       struct dentry *h_dst_dentry, *h_src_dentry;
36505 +       struct inode *h_isrc, *h_idst;
36506 +       char *value, *p, *o, *e;
36507 +
36508 +       /* try stopping to update the source inode while we are referencing */
36509 +       /* there should not be the parent-child relationship between them */
36510 +       h_dst_dentry = h_dst->dentry;
36511 +       h_idst = d_inode(h_dst_dentry);
36512 +       h_src_dentry = h_src->dentry;
36513 +       h_isrc = d_inode(h_src_dentry);
36514 +       inode_unlock(h_idst);
36515 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36516 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36517 +       unlocked = 0;
36518 +
36519 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36520 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36521 +       err = ssz;
36522 +       if (unlikely(err < 0)) {
36523 +               AuTraceErr(err);
36524 +               if (err == -ENODATA
36525 +                   || err == -EOPNOTSUPP)
36526 +                       err = 0;        /* ignore */
36527 +               goto out;
36528 +       }
36529 +
36530 +       err = 0;
36531 +       p = NULL;
36532 +       o = NULL;
36533 +       if (ssz) {
36534 +               err = -ENOMEM;
36535 +               p = kmalloc(ssz, GFP_NOFS);
36536 +               o = p;
36537 +               if (unlikely(!p))
36538 +                       goto out;
36539 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36540 +       }
36541 +       inode_unlock_shared(h_isrc);
36542 +       unlocked = 1;
36543 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36544 +       if (unlikely(err < 0))
36545 +               goto out_free;
36546 +
36547 +       err = 0;
36548 +       e = p + ssz;
36549 +       value = NULL;
36550 +       acl_access = 0;
36551 +       acl_default = 0;
36552 +       while (!err && p < e) {
36553 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36554 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36555 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36556 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36557 +                                       - 1);
36558 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36559 +                                      verbose);
36560 +               p += strlen(p) + 1;
36561 +       }
36562 +       AuTraceErr(err);
36563 +       ignore_flags |= au_xattr_out_of_list;
36564 +       if (!err && !acl_access) {
36565 +               err = au_do_cpup_xattr(h_dst, h_src,
36566 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36567 +                                      ignore_flags, verbose);
36568 +               AuTraceErr(err);
36569 +       }
36570 +       if (!err && !acl_default) {
36571 +               err = au_do_cpup_xattr(h_dst, h_src,
36572 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36573 +                                      ignore_flags, verbose);
36574 +               AuTraceErr(err);
36575 +       }
36576 +
36577 +       au_kfree_try_rcu(value);
36578 +
36579 +out_free:
36580 +       au_kfree_try_rcu(o);
36581 +out:
36582 +       if (!unlocked)
36583 +               inode_unlock_shared(h_isrc);
36584 +       AuTraceErr(err);
36585 +       return err;
36586 +}
36587 +
36588 +/* ---------------------------------------------------------------------- */
36589 +
36590 +static int au_smack_reentering(struct super_block *sb)
36591 +{
36592 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36593 +       /*
36594 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36595 +        * i_op->getxattr(). ouch.
36596 +        */
36597 +       return si_pid_test(sb);
36598 +#else
36599 +       return 0;
36600 +#endif
36601 +}
36602 +
36603 +enum {
36604 +       AU_XATTR_LIST,
36605 +       AU_XATTR_GET
36606 +};
36607 +
36608 +struct au_lgxattr {
36609 +       int type;
36610 +       union {
36611 +               struct {
36612 +                       char    *list;
36613 +                       size_t  size;
36614 +               } list;
36615 +               struct {
36616 +                       const char      *name;
36617 +                       void            *value;
36618 +                       size_t          size;
36619 +               } get;
36620 +       } u;
36621 +};
36622 +
36623 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36624 +                         struct au_lgxattr *arg)
36625 +{
36626 +       ssize_t err;
36627 +       int reenter;
36628 +       struct path h_path;
36629 +       struct super_block *sb;
36630 +
36631 +       sb = dentry->d_sb;
36632 +       reenter = au_smack_reentering(sb);
36633 +       if (!reenter) {
36634 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36635 +               if (unlikely(err))
36636 +                       goto out;
36637 +       }
36638 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36639 +       if (unlikely(err))
36640 +               goto out_si;
36641 +       if (unlikely(!h_path.dentry))
36642 +               /* illegally overlapped or something */
36643 +               goto out_di; /* pretending success */
36644 +
36645 +       /* always topmost entry only */
36646 +       switch (arg->type) {
36647 +       case AU_XATTR_LIST:
36648 +               err = vfs_listxattr(h_path.dentry,
36649 +                                   arg->u.list.list, arg->u.list.size);
36650 +               break;
36651 +       case AU_XATTR_GET:
36652 +               AuDebugOn(d_is_negative(h_path.dentry));
36653 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36654 +                                  arg->u.get.name, arg->u.get.value,
36655 +                                  arg->u.get.size);
36656 +               break;
36657 +       }
36658 +
36659 +out_di:
36660 +       if (!reenter)
36661 +               di_read_unlock(dentry, AuLock_IR);
36662 +out_si:
36663 +       if (!reenter)
36664 +               si_read_unlock(sb);
36665 +out:
36666 +       AuTraceErr(err);
36667 +       return err;
36668 +}
36669 +
36670 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36671 +{
36672 +       struct au_lgxattr arg = {
36673 +               .type = AU_XATTR_LIST,
36674 +               .u.list = {
36675 +                       .list   = list,
36676 +                       .size   = size
36677 +               },
36678 +       };
36679 +
36680 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36681 +}
36682 +
36683 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36684 +                          const char *name, void *value, size_t size)
36685 +{
36686 +       struct au_lgxattr arg = {
36687 +               .type = AU_XATTR_GET,
36688 +               .u.get = {
36689 +                       .name   = name,
36690 +                       .value  = value,
36691 +                       .size   = size
36692 +               },
36693 +       };
36694 +
36695 +       return au_lgxattr(dentry, inode, &arg);
36696 +}
36697 +
36698 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36699 +                      const char *name, const void *value, size_t size,
36700 +                      int flags)
36701 +{
36702 +       struct au_sxattr arg = {
36703 +               .type = AU_XATTR_SET,
36704 +               .u.set = {
36705 +                       .name   = name,
36706 +                       .value  = value,
36707 +                       .size   = size,
36708 +                       .flags  = flags
36709 +               },
36710 +       };
36711 +
36712 +       return au_sxattr(dentry, inode, &arg);
36713 +}
36714 +
36715 +/* ---------------------------------------------------------------------- */
36716 +
36717 +static int au_xattr_get(const struct xattr_handler *handler,
36718 +                       struct dentry *dentry, struct inode *inode,
36719 +                       const char *name, void *buffer, size_t size)
36720 +{
36721 +       return au_getxattr(dentry, inode, name, buffer, size);
36722 +}
36723 +
36724 +static int au_xattr_set(const struct xattr_handler *handler,
36725 +                       struct user_namespace *userns,
36726 +                       struct dentry *dentry, struct inode *inode,
36727 +                       const char *name, const void *value, size_t size,
36728 +                       int flags)
36729 +{
36730 +       return au_setxattr(dentry, inode, name, value, size, flags);
36731 +}
36732 +
36733 +static const struct xattr_handler au_xattr_handler = {
36734 +       .name   = "",
36735 +       .prefix = "",
36736 +       .get    = au_xattr_get,
36737 +       .set    = au_xattr_set
36738 +};
36739 +
36740 +static const struct xattr_handler *au_xattr_handlers[] = {
36741 +#ifdef CONFIG_FS_POSIX_ACL
36742 +       &posix_acl_access_xattr_handler,
36743 +       &posix_acl_default_xattr_handler,
36744 +#endif
36745 +       &au_xattr_handler, /* must be last */
36746 +       NULL
36747 +};
36748 +
36749 +void au_xattr_init(struct super_block *sb)
36750 +{
36751 +       sb->s_xattr = au_xattr_handlers;
36752 +}
36753 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36754 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36755 +++ linux/fs/aufs/xino.c        2021-12-03 15:40:58.236647297 +0100
36756 @@ -0,0 +1,1926 @@
36757 +// SPDX-License-Identifier: GPL-2.0
36758 +/*
36759 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36760 + *
36761 + * This program, aufs is free software; you can redistribute it and/or modify
36762 + * it under the terms of the GNU General Public License as published by
36763 + * the Free Software Foundation; either version 2 of the License, or
36764 + * (at your option) any later version.
36765 + *
36766 + * This program is distributed in the hope that it will be useful,
36767 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36768 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36769 + * GNU General Public License for more details.
36770 + *
36771 + * You should have received a copy of the GNU General Public License
36772 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36773 + */
36774 +
36775 +/*
36776 + * external inode number translation table and bitmap
36777 + *
36778 + * things to consider
36779 + * - the lifetime
36780 + *   + au_xino object
36781 + *   + XINO files (xino, xib, xigen)
36782 + *   + dynamic debugfs entries (xiN)
36783 + *   + static debugfs entries (xib, xigen)
36784 + *   + static sysfs entry (xi_path)
36785 + * - several entry points to handle them.
36786 + *   + mount(2) without xino option (default)
36787 + *   + mount(2) with xino option
36788 + *   + mount(2) with noxino option
36789 + *   + umount(2)
36790 + *   + remount with add/del branches
36791 + *   + remount with xino/noxino options
36792 + */
36793 +
36794 +#include <linux/seq_file.h>
36795 +#include <linux/statfs.h>
36796 +#include "aufs.h"
36797 +
36798 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36799 +                                    aufs_bindex_t bbot,
36800 +                                    struct super_block *h_sb)
36801 +{
36802 +       /* todo: try binary-search if the branches are many */
36803 +       for (; btop <= bbot; btop++)
36804 +               if (h_sb == au_sbr_sb(sb, btop))
36805 +                       return btop;
36806 +       return -1;
36807 +}
36808 +
36809 +/*
36810 + * find another branch who is on the same filesystem of the specified
36811 + * branch{@btgt}. search until @bbot.
36812 + */
36813 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36814 +                                 aufs_bindex_t bbot)
36815 +{
36816 +       aufs_bindex_t bindex;
36817 +       struct super_block *tgt_sb;
36818 +
36819 +       tgt_sb = au_sbr_sb(sb, btgt);
36820 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36821 +       if (bindex < 0)
36822 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36823 +
36824 +       return bindex;
36825 +}
36826 +
36827 +/* ---------------------------------------------------------------------- */
36828 +
36829 +/*
36830 + * stop unnecessary notify events at creating xino files
36831 + */
36832 +
36833 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36834 +{
36835 +       aufs_bindex_t bfound, bindex, bbot;
36836 +       struct dentry *parent;
36837 +       struct au_branch *br;
36838 +
36839 +       bfound = -1;
36840 +       parent = dentry->d_parent; /* safe d_parent access */
36841 +       bbot = au_sbbot(sb);
36842 +       for (bindex = 0; bindex <= bbot; bindex++) {
36843 +               br = au_sbr(sb, bindex);
36844 +               if (au_br_dentry(br) == parent) {
36845 +                       bfound = bindex;
36846 +                       break;
36847 +               }
36848 +       }
36849 +
36850 +       AuDbg("bfound b%d\n", bfound);
36851 +       return bfound;
36852 +}
36853 +
36854 +struct au_xino_lock_dir {
36855 +       struct au_hinode *hdir;
36856 +       struct dentry *parent;
36857 +       struct inode *dir;
36858 +};
36859 +
36860 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36861 +                                         unsigned int lsc)
36862 +{
36863 +       struct dentry *parent;
36864 +       struct inode *dir;
36865 +
36866 +       parent = dget_parent(dentry);
36867 +       dir = d_inode(parent);
36868 +       inode_lock_nested(dir, lsc);
36869 +#if 0 /* it should not happen */
36870 +       spin_lock(&dentry->d_lock);
36871 +       if (unlikely(dentry->d_parent != parent)) {
36872 +               spin_unlock(&dentry->d_lock);
36873 +               inode_unlock(dir);
36874 +               dput(parent);
36875 +               parent = NULL;
36876 +               goto out;
36877 +       }
36878 +       spin_unlock(&dentry->d_lock);
36879 +
36880 +out:
36881 +#endif
36882 +       return parent;
36883 +}
36884 +
36885 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36886 +                            struct au_xino_lock_dir *ldir)
36887 +{
36888 +       aufs_bindex_t bindex;
36889 +
36890 +       ldir->hdir = NULL;
36891 +       bindex = au_xi_root(sb, xipath->dentry);
36892 +       if (bindex >= 0) {
36893 +               /* rw branch root */
36894 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36895 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36896 +       } else {
36897 +               /* other */
36898 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36899 +                                                  AuLsc_I_PARENT);
36900 +               ldir->dir = d_inode(ldir->parent);
36901 +       }
36902 +}
36903 +
36904 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36905 +{
36906 +       if (ldir->hdir)
36907 +               au_hn_inode_unlock(ldir->hdir);
36908 +       else {
36909 +               inode_unlock(ldir->dir);
36910 +               dput(ldir->parent);
36911 +       }
36912 +}
36913 +
36914 +/* ---------------------------------------------------------------------- */
36915 +
36916 +/*
36917 + * create and set a new xino file
36918 + */
36919 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36920 +                           int wbrtop)
36921 +{
36922 +       struct file *file;
36923 +       struct dentry *h_parent, *d;
36924 +       struct inode *h_dir, *inode;
36925 +       int err;
36926 +       static DEFINE_MUTEX(mtx);
36927 +
36928 +       /*
36929 +        * at mount-time, and the xino file is the default path,
36930 +        * hnotify is disabled so we have no notify events to ignore.
36931 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36932 +        */
36933 +       if (!wbrtop)
36934 +               mutex_lock(&mtx);
36935 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36936 +                              /* | __FMODE_NONOTIFY */,
36937 +                              0666);
36938 +       if (IS_ERR(file)) {
36939 +               if (!wbrtop)
36940 +                       mutex_unlock(&mtx);
36941 +               if (!silent)
36942 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36943 +               return file;
36944 +       }
36945 +
36946 +       /* keep file count */
36947 +       err = 0;
36948 +       d = file->f_path.dentry;
36949 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36950 +       if (!wbrtop)
36951 +               mutex_unlock(&mtx);
36952 +       /* mnt_want_write() is unnecessary here */
36953 +       h_dir = d_inode(h_parent);
36954 +       inode = file_inode(file);
36955 +       /* no delegation since it is just created */
36956 +       if (inode->i_nlink)
36957 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36958 +                                  /*force*/0);
36959 +       inode_unlock(h_dir);
36960 +       dput(h_parent);
36961 +       if (unlikely(err)) {
36962 +               if (!silent)
36963 +                       pr_err("unlink %s(%d)\n", fpath, err);
36964 +               goto out;
36965 +       }
36966 +
36967 +       err = -EINVAL;
36968 +       if (unlikely(sb == d->d_sb)) {
36969 +               if (!silent)
36970 +                       pr_err("%s must be outside\n", fpath);
36971 +               goto out;
36972 +       }
36973 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36974 +               if (!silent)
36975 +                       pr_err("xino doesn't support %s(%s)\n",
36976 +                              fpath, au_sbtype(d->d_sb));
36977 +               goto out;
36978 +       }
36979 +       return file; /* success */
36980 +
36981 +out:
36982 +       fput(file);
36983 +       file = ERR_PTR(err);
36984 +       return file;
36985 +}
36986 +
36987 +/*
36988 + * create a new xinofile at the same place/path as @base.
36989 + */
36990 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
36991 +                            struct file *copy_src)
36992 +{
36993 +       struct file *file;
36994 +       struct dentry *dentry;
36995 +       struct inode *dir, *delegated;
36996 +       struct qstr *name;
36997 +       struct path ppath, path;
36998 +       int err, do_unlock;
36999 +       struct au_xino_lock_dir ldir;
37000 +
37001 +       do_unlock = 1;
37002 +       au_xino_lock_dir(sb, base, &ldir);
37003 +       dentry = base->dentry;
37004 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37005 +       ppath.mnt = base->mnt;
37006 +       dir = d_inode(ppath.dentry);
37007 +       IMustLock(dir);
37008 +
37009 +       name = &dentry->d_name;
37010 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37011 +       if (IS_ERR(path.dentry)) {
37012 +               file = (void *)path.dentry;
37013 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37014 +               goto out;
37015 +       }
37016 +
37017 +       /* no need to mnt_want_write() since we call dentry_open() later */
37018 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37019 +       if (unlikely(err)) {
37020 +               file = ERR_PTR(err);
37021 +               pr_err("%pd create err %d\n", dentry, err);
37022 +               goto out_dput;
37023 +       }
37024 +
37025 +       path.mnt = base->mnt;
37026 +       file = vfsub_dentry_open(&path,
37027 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37028 +                                /* | __FMODE_NONOTIFY */);
37029 +       if (IS_ERR(file)) {
37030 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37031 +               goto out_dput;
37032 +       }
37033 +
37034 +       delegated = NULL;
37035 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37036 +       au_xino_unlock_dir(&ldir);
37037 +       do_unlock = 0;
37038 +       if (unlikely(err == -EWOULDBLOCK)) {
37039 +               pr_warn("cannot retry for NFSv4 delegation"
37040 +                       " for an internal unlink\n");
37041 +               iput(delegated);
37042 +       }
37043 +       if (unlikely(err)) {
37044 +               pr_err("%pd unlink err %d\n", dentry, err);
37045 +               goto out_fput;
37046 +       }
37047 +
37048 +       if (copy_src) {
37049 +               /* no one can touch copy_src xino */
37050 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37051 +               if (unlikely(err)) {
37052 +                       pr_err("%pd copy err %d\n", dentry, err);
37053 +                       goto out_fput;
37054 +               }
37055 +       }
37056 +       goto out_dput; /* success */
37057 +
37058 +out_fput:
37059 +       fput(file);
37060 +       file = ERR_PTR(err);
37061 +out_dput:
37062 +       dput(path.dentry);
37063 +out:
37064 +       if (do_unlock)
37065 +               au_xino_unlock_dir(&ldir);
37066 +       return file;
37067 +}
37068 +
37069 +struct file *au_xino_file1(struct au_xino *xi)
37070 +{
37071 +       struct file *file;
37072 +       unsigned int u, nfile;
37073 +
37074 +       file = NULL;
37075 +       nfile = xi->xi_nfile;
37076 +       for (u = 0; u < nfile; u++) {
37077 +               file = xi->xi_file[u];
37078 +               if (file)
37079 +                       break;
37080 +       }
37081 +
37082 +       return file;
37083 +}
37084 +
37085 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37086 +{
37087 +       int err;
37088 +       struct file *f;
37089 +       void *p;
37090 +
37091 +       if (file)
37092 +               get_file(file);
37093 +
37094 +       err = 0;
37095 +       f = NULL;
37096 +       if (idx < xi->xi_nfile) {
37097 +               f = xi->xi_file[idx];
37098 +               if (f)
37099 +                       fput(f);
37100 +       } else {
37101 +               p = au_kzrealloc(xi->xi_file,
37102 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37103 +                                sizeof(*xi->xi_file) * (idx + 1),
37104 +                                GFP_NOFS, /*may_shrink*/0);
37105 +               if (p) {
37106 +                       MtxMustLock(&xi->xi_mtx);
37107 +                       xi->xi_file = p;
37108 +                       xi->xi_nfile = idx + 1;
37109 +               } else {
37110 +                       err = -ENOMEM;
37111 +                       if (file)
37112 +                               fput(file);
37113 +                       goto out;
37114 +               }
37115 +       }
37116 +       xi->xi_file[idx] = file;
37117 +
37118 +out:
37119 +       return err;
37120 +}
37121 +
37122 +/*
37123 + * if @xinew->xi is not set, then create new xigen file.
37124 + */
37125 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37126 +{
37127 +       struct file *file;
37128 +       int err;
37129 +
37130 +       SiMustAnyLock(sb);
37131 +
37132 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37133 +       if (IS_ERR(file)) {
37134 +               err = PTR_ERR(file);
37135 +               pr_err("%s[%d], err %d\n",
37136 +                      xinew->xi ? "xino" : "xigen",
37137 +                      xinew->idx, err);
37138 +               goto out;
37139 +       }
37140 +
37141 +       if (xinew->xi)
37142 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37143 +       else {
37144 +               BUG();
37145 +               /* todo: make xigen file an array */
37146 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37147 +       }
37148 +       fput(file);
37149 +       if (unlikely(err))
37150 +               file = ERR_PTR(err);
37151 +
37152 +out:
37153 +       return file;
37154 +}
37155 +
37156 +/* ---------------------------------------------------------------------- */
37157 +
37158 +/*
37159 + * truncate xino files
37160 + */
37161 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37162 +                           int idx, struct kstatfs *st)
37163 +{
37164 +       int err;
37165 +       blkcnt_t blocks;
37166 +       struct file *file, *new_xino;
37167 +       struct au_xi_new xinew = {
37168 +               .idx = idx
37169 +       };
37170 +
37171 +       err = 0;
37172 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37173 +       file = au_xino_file(xinew.xi, idx);
37174 +       if (!file)
37175 +               goto out;
37176 +
37177 +       xinew.base = &file->f_path;
37178 +       err = vfs_statfs(xinew.base, st);
37179 +       if (unlikely(err)) {
37180 +               AuErr1("statfs err %d, ignored\n", err);
37181 +               err = 0;
37182 +               goto out;
37183 +       }
37184 +
37185 +       blocks = file_inode(file)->i_blocks;
37186 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37187 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37188 +
37189 +       xinew.copy_src = file;
37190 +       new_xino = au_xi_new(sb, &xinew);
37191 +       if (IS_ERR(new_xino)) {
37192 +               err = PTR_ERR(new_xino);
37193 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37194 +               goto out;
37195 +       }
37196 +
37197 +       err = vfs_statfs(&new_xino->f_path, st);
37198 +       if (!err)
37199 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37200 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37201 +                       st->f_bfree, st->f_blocks);
37202 +       else {
37203 +               AuErr1("statfs err %d, ignored\n", err);
37204 +               err = 0;
37205 +       }
37206 +
37207 +out:
37208 +       return err;
37209 +}
37210 +
37211 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37212 +{
37213 +       int err, i;
37214 +       unsigned long jiffy;
37215 +       aufs_bindex_t bbot;
37216 +       struct kstatfs *st;
37217 +       struct au_branch *br;
37218 +       struct au_xino *xi;
37219 +
37220 +       err = -ENOMEM;
37221 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37222 +       if (unlikely(!st))
37223 +               goto out;
37224 +
37225 +       err = -EINVAL;
37226 +       bbot = au_sbbot(sb);
37227 +       if (unlikely(bindex < 0 || bbot < bindex))
37228 +               goto out_st;
37229 +
37230 +       err = 0;
37231 +       jiffy = jiffies;
37232 +       br = au_sbr(sb, bindex);
37233 +       xi = br->br_xino;
37234 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37235 +               err = au_xino_do_trunc(sb, bindex, i, st);
37236 +       if (!err)
37237 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37238 +
37239 +out_st:
37240 +       au_kfree_rcu(st);
37241 +out:
37242 +       return err;
37243 +}
37244 +
37245 +struct xino_do_trunc_args {
37246 +       struct super_block *sb;
37247 +       struct au_branch *br;
37248 +       int idx;
37249 +};
37250 +
37251 +static void xino_do_trunc(void *_args)
37252 +{
37253 +       struct xino_do_trunc_args *args = _args;
37254 +       struct super_block *sb;
37255 +       struct au_branch *br;
37256 +       struct inode *dir;
37257 +       int err, idx;
37258 +       aufs_bindex_t bindex;
37259 +
37260 +       err = 0;
37261 +       sb = args->sb;
37262 +       dir = d_inode(sb->s_root);
37263 +       br = args->br;
37264 +       idx = args->idx;
37265 +
37266 +       si_noflush_write_lock(sb);
37267 +       ii_read_lock_parent(dir);
37268 +       bindex = au_br_index(sb, br->br_id);
37269 +       err = au_xino_trunc(sb, bindex, idx);
37270 +       ii_read_unlock(dir);
37271 +       if (unlikely(err))
37272 +               pr_warn("err b%d, (%d)\n", bindex, err);
37273 +       atomic_dec(&br->br_xino->xi_truncating);
37274 +       au_lcnt_dec(&br->br_count);
37275 +       si_write_unlock(sb);
37276 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37277 +       au_kfree_rcu(args);
37278 +}
37279 +
37280 +/*
37281 + * returns the index in the xi_file array whose corresponding file is necessary
37282 + * to truncate, or -1 which means no need to truncate.
37283 + */
37284 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37285 +{
37286 +       int err;
37287 +       unsigned int u;
37288 +       struct kstatfs st;
37289 +       struct au_sbinfo *sbinfo;
37290 +       struct au_xino *xi;
37291 +       struct file *file;
37292 +
37293 +       /* todo: si_xino_expire and the ratio should be customizable */
37294 +       sbinfo = au_sbi(sb);
37295 +       if (time_before(jiffies,
37296 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37297 +               return -1;
37298 +
37299 +       /* truncation border */
37300 +       xi = br->br_xino;
37301 +       for (u = 0; u < xi->xi_nfile; u++) {
37302 +               file = au_xino_file(xi, u);
37303 +               if (!file)
37304 +                       continue;
37305 +
37306 +               err = vfs_statfs(&file->f_path, &st);
37307 +               if (unlikely(err)) {
37308 +                       AuErr1("statfs err %d, ignored\n", err);
37309 +                       return -1;
37310 +               }
37311 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37312 +                   >= AUFS_XINO_DEF_TRUNC)
37313 +                       return u;
37314 +       }
37315 +
37316 +       return -1;
37317 +}
37318 +
37319 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37320 +{
37321 +       int idx;
37322 +       struct xino_do_trunc_args *args;
37323 +       int wkq_err;
37324 +
37325 +       idx = xino_trunc_test(sb, br);
37326 +       if (idx < 0)
37327 +               return;
37328 +
37329 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37330 +               goto out;
37331 +
37332 +       /* lock and kfree() will be called in trunc_xino() */
37333 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37334 +       if (unlikely(!args)) {
37335 +               AuErr1("no memory\n");
37336 +               goto out;
37337 +       }
37338 +
37339 +       au_lcnt_inc(&br->br_count);
37340 +       args->sb = sb;
37341 +       args->br = br;
37342 +       args->idx = idx;
37343 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37344 +       if (!wkq_err)
37345 +               return; /* success */
37346 +
37347 +       pr_err("wkq %d\n", wkq_err);
37348 +       au_lcnt_dec(&br->br_count);
37349 +       au_kfree_rcu(args);
37350 +
37351 +out:
37352 +       atomic_dec(&br->br_xino->xi_truncating);
37353 +}
37354 +
37355 +/* ---------------------------------------------------------------------- */
37356 +
37357 +struct au_xi_calc {
37358 +       int idx;
37359 +       loff_t pos;
37360 +};
37361 +
37362 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37363 +                      struct au_xi_calc *calc)
37364 +{
37365 +       loff_t maxent;
37366 +
37367 +       maxent = au_xi_maxent(sb);
37368 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37369 +       calc->pos *= sizeof(ino_t);
37370 +}
37371 +
37372 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37373 +                               struct au_xi_calc *calc)
37374 +{
37375 +       int err;
37376 +       struct file *file;
37377 +       struct au_xino *xi = br->br_xino;
37378 +       struct au_xi_new xinew = {
37379 +               .xi = xi
37380 +       };
37381 +
37382 +       SiMustAnyLock(sb);
37383 +
37384 +       err = 0;
37385 +       if (!xi)
37386 +               goto out;
37387 +
37388 +       mutex_lock(&xi->xi_mtx);
37389 +       file = au_xino_file(xi, calc->idx);
37390 +       if (file)
37391 +               goto out_mtx;
37392 +
37393 +       file = au_xino_file(xi, /*idx*/-1);
37394 +       AuDebugOn(!file);
37395 +       xinew.idx = calc->idx;
37396 +       xinew.base = &file->f_path;
37397 +       /* xinew.copy_src = NULL; */
37398 +       file = au_xi_new(sb, &xinew);
37399 +       if (IS_ERR(file))
37400 +               err = PTR_ERR(file);
37401 +
37402 +out_mtx:
37403 +       mutex_unlock(&xi->xi_mtx);
37404 +out:
37405 +       return err;
37406 +}
37407 +
37408 +struct au_xino_do_new_async_args {
37409 +       struct super_block *sb;
37410 +       struct au_branch *br;
37411 +       struct au_xi_calc calc;
37412 +       ino_t ino;
37413 +};
37414 +
37415 +struct au_xi_writing {
37416 +       struct hlist_bl_node node;
37417 +       ino_t h_ino, ino;
37418 +};
37419 +
37420 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37421 +                           ino_t ino);
37422 +
37423 +static void au_xino_call_do_new_async(void *args)
37424 +{
37425 +       struct au_xino_do_new_async_args *a = args;
37426 +       struct au_branch *br;
37427 +       struct super_block *sb;
37428 +       struct au_sbinfo *sbi;
37429 +       struct inode *root;
37430 +       struct file *file;
37431 +       struct au_xi_writing *del, *p;
37432 +       struct hlist_bl_head *hbl;
37433 +       struct hlist_bl_node *pos;
37434 +       int err;
37435 +
37436 +       br = a->br;
37437 +       sb = a->sb;
37438 +       sbi = au_sbi(sb);
37439 +       si_noflush_read_lock(sb);
37440 +       root = d_inode(sb->s_root);
37441 +       ii_read_lock_child(root);
37442 +       err = au_xino_do_new_async(sb, br, &a->calc);
37443 +       if (unlikely(err)) {
37444 +               AuIOErr("err %d\n", err);
37445 +               goto out;
37446 +       }
37447 +
37448 +       file = au_xino_file(br->br_xino, a->calc.idx);
37449 +       AuDebugOn(!file);
37450 +       err = au_xino_do_write(file, &a->calc, a->ino);
37451 +       if (unlikely(err)) {
37452 +               AuIOErr("err %d\n", err);
37453 +               goto out;
37454 +       }
37455 +
37456 +       del = NULL;
37457 +       hbl = &br->br_xino->xi_writing;
37458 +       hlist_bl_lock(hbl);
37459 +       au_hbl_for_each(pos, hbl) {
37460 +               p = container_of(pos, struct au_xi_writing, node);
37461 +               if (p->ino == a->ino) {
37462 +                       del = p;
37463 +                       hlist_bl_del(&p->node);
37464 +                       break;
37465 +               }
37466 +       }
37467 +       hlist_bl_unlock(hbl);
37468 +       au_kfree_rcu(del);
37469 +
37470 +out:
37471 +       au_lcnt_dec(&br->br_count);
37472 +       ii_read_unlock(root);
37473 +       si_read_unlock(sb);
37474 +       au_nwt_done(&sbi->si_nowait);
37475 +       au_kfree_rcu(a);
37476 +}
37477 +
37478 +/*
37479 + * create a new xino file asynchronously
37480 + */
37481 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37482 +                            struct au_xi_calc *calc, ino_t ino)
37483 +{
37484 +       int err;
37485 +       struct au_xino_do_new_async_args *arg;
37486 +
37487 +       err = -ENOMEM;
37488 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37489 +       if (unlikely(!arg))
37490 +               goto out;
37491 +
37492 +       arg->sb = sb;
37493 +       arg->br = br;
37494 +       arg->calc = *calc;
37495 +       arg->ino = ino;
37496 +       au_lcnt_inc(&br->br_count);
37497 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37498 +       if (unlikely(err)) {
37499 +               pr_err("wkq %d\n", err);
37500 +               au_lcnt_dec(&br->br_count);
37501 +               au_kfree_rcu(arg);
37502 +       }
37503 +
37504 +out:
37505 +       return err;
37506 +}
37507 +
37508 +/*
37509 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37510 + * at the position of @h_ino.
37511 + */
37512 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37513 +                ino_t *ino)
37514 +{
37515 +       int err;
37516 +       ssize_t sz;
37517 +       struct au_xi_calc calc;
37518 +       struct au_sbinfo *sbinfo;
37519 +       struct file *file;
37520 +       struct au_xino *xi;
37521 +       struct hlist_bl_head *hbl;
37522 +       struct hlist_bl_node *pos;
37523 +       struct au_xi_writing *p;
37524 +
37525 +       *ino = 0;
37526 +       if (!au_opt_test(au_mntflags(sb), XINO))
37527 +               return 0; /* no xino */
37528 +
37529 +       err = 0;
37530 +       au_xi_calc(sb, h_ino, &calc);
37531 +       xi = au_sbr(sb, bindex)->br_xino;
37532 +       file = au_xino_file(xi, calc.idx);
37533 +       if (!file) {
37534 +               hbl = &xi->xi_writing;
37535 +               hlist_bl_lock(hbl);
37536 +               au_hbl_for_each(pos, hbl) {
37537 +                       p = container_of(pos, struct au_xi_writing, node);
37538 +                       if (p->h_ino == h_ino) {
37539 +                               AuDbg("hi%llu, i%llu, found\n",
37540 +                                     (u64)p->h_ino, (u64)p->ino);
37541 +                               *ino = p->ino;
37542 +                               break;
37543 +                       }
37544 +               }
37545 +               hlist_bl_unlock(hbl);
37546 +               return 0;
37547 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37548 +               return 0; /* no xino */
37549 +
37550 +       sbinfo = au_sbi(sb);
37551 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37552 +       if (sz == sizeof(*ino))
37553 +               return 0; /* success */
37554 +
37555 +       err = sz;
37556 +       if (unlikely(sz >= 0)) {
37557 +               err = -EIO;
37558 +               AuIOErr("xino read error (%zd)\n", sz);
37559 +       }
37560 +       return err;
37561 +}
37562 +
37563 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37564 +                           ino_t ino)
37565 +{
37566 +       ssize_t sz;
37567 +
37568 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37569 +       if (sz == sizeof(ino))
37570 +               return 0; /* success */
37571 +
37572 +       AuIOErr("write failed (%zd)\n", sz);
37573 +       return -EIO;
37574 +}
37575 +
37576 +/*
37577 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37578 + * at the position of @h_ino.
37579 + * even if @ino is zero, it is written to the xinofile and means no entry.
37580 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37581 + * try truncating it.
37582 + */
37583 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37584 +                 ino_t ino)
37585 +{
37586 +       int err;
37587 +       unsigned int mnt_flags;
37588 +       struct au_xi_calc calc;
37589 +       struct file *file;
37590 +       struct au_branch *br;
37591 +       struct au_xino *xi;
37592 +       struct au_xi_writing *p;
37593 +
37594 +       SiMustAnyLock(sb);
37595 +
37596 +       mnt_flags = au_mntflags(sb);
37597 +       if (!au_opt_test(mnt_flags, XINO))
37598 +               return 0;
37599 +
37600 +       au_xi_calc(sb, h_ino, &calc);
37601 +       br = au_sbr(sb, bindex);
37602 +       xi = br->br_xino;
37603 +       file = au_xino_file(xi, calc.idx);
37604 +       if (!file) {
37605 +               /* store the inum pair into the list */
37606 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37607 +               p->h_ino = h_ino;
37608 +               p->ino = ino;
37609 +               au_hbl_add(&p->node, &xi->xi_writing);
37610 +
37611 +               /* create and write a new xino file asynchronously */
37612 +               err = au_xino_new_async(sb, br, &calc, ino);
37613 +               if (!err)
37614 +                       return 0; /* success */
37615 +               goto out;
37616 +       }
37617 +
37618 +       err = au_xino_do_write(file, &calc, ino);
37619 +       if (!err) {
37620 +               br = au_sbr(sb, bindex);
37621 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37622 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37623 +                       xino_try_trunc(sb, br);
37624 +               return 0; /* success */
37625 +       }
37626 +
37627 +out:
37628 +       AuIOErr("write failed (%d)\n", err);
37629 +       return -EIO;
37630 +}
37631 +
37632 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37633 +                             loff_t *pos);
37634 +
37635 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37636 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37637 +{
37638 +       ssize_t err;
37639 +       int i;
37640 +       const int prevent_endless = 10;
37641 +
37642 +       i = 0;
37643 +       do {
37644 +               err = vfsub_read_k(file, kbuf, size, pos);
37645 +               if (err == -EINTR
37646 +                   && !au_wkq_test()
37647 +                   && fatal_signal_pending(current)) {
37648 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37649 +                       BUG_ON(err == -EINTR);
37650 +               }
37651 +       } while (i++ < prevent_endless
37652 +                && (err == -EAGAIN || err == -EINTR));
37653 +
37654 +#if 0 /* reserved for future use */
37655 +       if (err > 0)
37656 +               fsnotify_access(file->f_path.dentry);
37657 +#endif
37658 +
37659 +       return err;
37660 +}
37661 +
37662 +struct xino_fread_args {
37663 +       ssize_t *errp;
37664 +       struct file *file;
37665 +       void *buf;
37666 +       size_t size;
37667 +       loff_t *pos;
37668 +};
37669 +
37670 +static void call_xino_fread(void *args)
37671 +{
37672 +       struct xino_fread_args *a = args;
37673 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37674 +}
37675 +
37676 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37677 +                             loff_t *pos)
37678 +{
37679 +       ssize_t err;
37680 +       int wkq_err;
37681 +       struct xino_fread_args args = {
37682 +               .errp   = &err,
37683 +               .file   = file,
37684 +               .buf    = buf,
37685 +               .size   = size,
37686 +               .pos    = pos
37687 +       };
37688 +
37689 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37690 +       if (unlikely(wkq_err))
37691 +               err = wkq_err;
37692 +
37693 +       return err;
37694 +}
37695 +
37696 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37697 +                              loff_t *pos);
37698 +
37699 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37700 +                             loff_t *pos)
37701 +{
37702 +       ssize_t err;
37703 +       int i;
37704 +       const int prevent_endless = 10;
37705 +
37706 +       i = 0;
37707 +       do {
37708 +               err = vfsub_write_k(file, kbuf, size, pos);
37709 +               if (err == -EINTR
37710 +                   && !au_wkq_test()
37711 +                   && fatal_signal_pending(current)) {
37712 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37713 +                       BUG_ON(err == -EINTR);
37714 +               }
37715 +       } while (i++ < prevent_endless
37716 +                && (err == -EAGAIN || err == -EINTR));
37717 +
37718 +#if 0 /* reserved for future use */
37719 +       if (err > 0)
37720 +               fsnotify_modify(file->f_path.dentry);
37721 +#endif
37722 +
37723 +       return err;
37724 +}
37725 +
37726 +struct do_xino_fwrite_args {
37727 +       ssize_t *errp;
37728 +       struct file *file;
37729 +       void *buf;
37730 +       size_t size;
37731 +       loff_t *pos;
37732 +};
37733 +
37734 +static void call_do_xino_fwrite(void *args)
37735 +{
37736 +       struct do_xino_fwrite_args *a = args;
37737 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37738 +}
37739 +
37740 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37741 +                              loff_t *pos)
37742 +{
37743 +       ssize_t err;
37744 +       int wkq_err;
37745 +       struct do_xino_fwrite_args args = {
37746 +               .errp   = &err,
37747 +               .file   = file,
37748 +               .buf    = buf,
37749 +               .size   = size,
37750 +               .pos    = pos
37751 +       };
37752 +
37753 +       /*
37754 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37755 +        * users should care about quota and real 'filesystem full.'
37756 +        */
37757 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37758 +       if (unlikely(wkq_err))
37759 +               err = wkq_err;
37760 +
37761 +       return err;
37762 +}
37763 +
37764 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37765 +{
37766 +       ssize_t err;
37767 +
37768 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37769 +               lockdep_off();
37770 +               err = do_xino_fwrite(file, buf, size, pos);
37771 +               lockdep_on();
37772 +       } else {
37773 +               lockdep_off();
37774 +               err = xino_fwrite_wkq(file, buf, size, pos);
37775 +               lockdep_on();
37776 +       }
37777 +
37778 +       return err;
37779 +}
37780 +
37781 +/* ---------------------------------------------------------------------- */
37782 +
37783 +/*
37784 + * inode number bitmap
37785 + */
37786 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37787 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37788 +{
37789 +       ino_t ino;
37790 +
37791 +       AuDebugOn(bit < 0 || page_bits <= bit);
37792 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37793 +       return ino;
37794 +}
37795 +
37796 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37797 +{
37798 +       AuDebugOn(ino < AUFS_FIRST_INO);
37799 +       ino -= AUFS_FIRST_INO;
37800 +       *pindex = ino / page_bits;
37801 +       *bit = ino % page_bits;
37802 +}
37803 +
37804 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37805 +{
37806 +       int err;
37807 +       loff_t pos;
37808 +       ssize_t sz;
37809 +       struct au_sbinfo *sbinfo;
37810 +       struct file *xib;
37811 +       unsigned long *p;
37812 +
37813 +       sbinfo = au_sbi(sb);
37814 +       MtxMustLock(&sbinfo->si_xib_mtx);
37815 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37816 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37817 +
37818 +       if (pindex == sbinfo->si_xib_last_pindex)
37819 +               return 0;
37820 +
37821 +       xib = sbinfo->si_xib;
37822 +       p = sbinfo->si_xib_buf;
37823 +       pos = sbinfo->si_xib_last_pindex;
37824 +       pos *= PAGE_SIZE;
37825 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37826 +       if (unlikely(sz != PAGE_SIZE))
37827 +               goto out;
37828 +
37829 +       pos = pindex;
37830 +       pos *= PAGE_SIZE;
37831 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37832 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37833 +       else {
37834 +               memset(p, 0, PAGE_SIZE);
37835 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37836 +       }
37837 +       if (sz == PAGE_SIZE) {
37838 +               sbinfo->si_xib_last_pindex = pindex;
37839 +               return 0; /* success */
37840 +       }
37841 +
37842 +out:
37843 +       AuIOErr1("write failed (%zd)\n", sz);
37844 +       err = sz;
37845 +       if (sz >= 0)
37846 +               err = -EIO;
37847 +       return err;
37848 +}
37849 +
37850 +static void au_xib_clear_bit(struct inode *inode)
37851 +{
37852 +       int err, bit;
37853 +       unsigned long pindex;
37854 +       struct super_block *sb;
37855 +       struct au_sbinfo *sbinfo;
37856 +
37857 +       AuDebugOn(inode->i_nlink);
37858 +
37859 +       sb = inode->i_sb;
37860 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37861 +       AuDebugOn(page_bits <= bit);
37862 +       sbinfo = au_sbi(sb);
37863 +       mutex_lock(&sbinfo->si_xib_mtx);
37864 +       err = xib_pindex(sb, pindex);
37865 +       if (!err) {
37866 +               clear_bit(bit, sbinfo->si_xib_buf);
37867 +               sbinfo->si_xib_next_bit = bit;
37868 +       }
37869 +       mutex_unlock(&sbinfo->si_xib_mtx);
37870 +}
37871 +
37872 +/* ---------------------------------------------------------------------- */
37873 +
37874 +/*
37875 + * truncate a xino bitmap file
37876 + */
37877 +
37878 +/* todo: slow */
37879 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37880 +{
37881 +       int err, bit;
37882 +       ssize_t sz;
37883 +       unsigned long pindex;
37884 +       loff_t pos, pend;
37885 +       struct au_sbinfo *sbinfo;
37886 +       ino_t *ino;
37887 +       unsigned long *p;
37888 +
37889 +       err = 0;
37890 +       sbinfo = au_sbi(sb);
37891 +       MtxMustLock(&sbinfo->si_xib_mtx);
37892 +       p = sbinfo->si_xib_buf;
37893 +       pend = vfsub_f_size_read(file);
37894 +       pos = 0;
37895 +       while (pos < pend) {
37896 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
37897 +               err = sz;
37898 +               if (unlikely(sz <= 0))
37899 +                       goto out;
37900 +
37901 +               err = 0;
37902 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37903 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37904 +                               continue;
37905 +
37906 +                       xib_calc_bit(*ino, &pindex, &bit);
37907 +                       AuDebugOn(page_bits <= bit);
37908 +                       err = xib_pindex(sb, pindex);
37909 +                       if (!err)
37910 +                               set_bit(bit, p);
37911 +                       else
37912 +                               goto out;
37913 +               }
37914 +       }
37915 +
37916 +out:
37917 +       return err;
37918 +}
37919 +
37920 +static int xib_restore(struct super_block *sb)
37921 +{
37922 +       int err, i;
37923 +       unsigned int nfile;
37924 +       aufs_bindex_t bindex, bbot;
37925 +       void *page;
37926 +       struct au_branch *br;
37927 +       struct au_xino *xi;
37928 +       struct file *file;
37929 +
37930 +       err = -ENOMEM;
37931 +       page = (void *)__get_free_page(GFP_NOFS);
37932 +       if (unlikely(!page))
37933 +               goto out;
37934 +
37935 +       err = 0;
37936 +       bbot = au_sbbot(sb);
37937 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37938 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37939 +                       br = au_sbr(sb, bindex);
37940 +                       xi = br->br_xino;
37941 +                       nfile = xi->xi_nfile;
37942 +                       for (i = 0; i < nfile; i++) {
37943 +                               file = au_xino_file(xi, i);
37944 +                               if (file)
37945 +                                       err = do_xib_restore(sb, file, page);
37946 +                       }
37947 +               } else
37948 +                       AuDbg("skip shared b%d\n", bindex);
37949 +       free_page((unsigned long)page);
37950 +
37951 +out:
37952 +       return err;
37953 +}
37954 +
37955 +int au_xib_trunc(struct super_block *sb)
37956 +{
37957 +       int err;
37958 +       ssize_t sz;
37959 +       loff_t pos;
37960 +       struct au_sbinfo *sbinfo;
37961 +       unsigned long *p;
37962 +       struct file *file;
37963 +
37964 +       SiMustWriteLock(sb);
37965 +
37966 +       err = 0;
37967 +       sbinfo = au_sbi(sb);
37968 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
37969 +               goto out;
37970 +
37971 +       file = sbinfo->si_xib;
37972 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
37973 +               goto out;
37974 +
37975 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37976 +       err = PTR_ERR(file);
37977 +       if (IS_ERR(file))
37978 +               goto out;
37979 +       fput(sbinfo->si_xib);
37980 +       sbinfo->si_xib = file;
37981 +
37982 +       p = sbinfo->si_xib_buf;
37983 +       memset(p, 0, PAGE_SIZE);
37984 +       pos = 0;
37985 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
37986 +       if (unlikely(sz != PAGE_SIZE)) {
37987 +               err = sz;
37988 +               AuIOErr("err %d\n", err);
37989 +               if (sz >= 0)
37990 +                       err = -EIO;
37991 +               goto out;
37992 +       }
37993 +
37994 +       mutex_lock(&sbinfo->si_xib_mtx);
37995 +       /* mnt_want_write() is unnecessary here */
37996 +       err = xib_restore(sb);
37997 +       mutex_unlock(&sbinfo->si_xib_mtx);
37998 +
37999 +out:
38000 +       return err;
38001 +}
38002 +
38003 +/* ---------------------------------------------------------------------- */
38004 +
38005 +struct au_xino *au_xino_alloc(unsigned int nfile)
38006 +{
38007 +       struct au_xino *xi;
38008 +
38009 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38010 +       if (unlikely(!xi))
38011 +               goto out;
38012 +       xi->xi_nfile = nfile;
38013 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38014 +       if (unlikely(!xi->xi_file))
38015 +               goto out_free;
38016 +
38017 +       xi->xi_nondir.total = 8; /* initial size */
38018 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38019 +                                     GFP_NOFS);
38020 +       if (unlikely(!xi->xi_nondir.array))
38021 +               goto out_file;
38022 +
38023 +       spin_lock_init(&xi->xi_nondir.spin);
38024 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38025 +       mutex_init(&xi->xi_mtx);
38026 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38027 +       atomic_set(&xi->xi_truncating, 0);
38028 +       kref_init(&xi->xi_kref);
38029 +       goto out; /* success */
38030 +
38031 +out_file:
38032 +       au_kfree_try_rcu(xi->xi_file);
38033 +out_free:
38034 +       au_kfree_rcu(xi);
38035 +       xi = NULL;
38036 +out:
38037 +       return xi;
38038 +}
38039 +
38040 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38041 +{
38042 +       int err;
38043 +       struct au_xino *xi;
38044 +
38045 +       err = 0;
38046 +       xi = au_xino_alloc(idx + 1);
38047 +       if (unlikely(!xi)) {
38048 +               err = -ENOMEM;
38049 +               goto out;
38050 +       }
38051 +
38052 +       if (file)
38053 +               get_file(file);
38054 +       xi->xi_file[idx] = file;
38055 +       AuDebugOn(br->br_xino);
38056 +       br->br_xino = xi;
38057 +
38058 +out:
38059 +       return err;
38060 +}
38061 +
38062 +static void au_xino_release(struct kref *kref)
38063 +{
38064 +       struct au_xino *xi;
38065 +       int i;
38066 +       unsigned long ul;
38067 +       struct hlist_bl_head *hbl;
38068 +       struct hlist_bl_node *pos, *n;
38069 +       struct au_xi_writing *p;
38070 +
38071 +       xi = container_of(kref, struct au_xino, xi_kref);
38072 +       for (i = 0; i < xi->xi_nfile; i++)
38073 +               if (xi->xi_file[i])
38074 +                       fput(xi->xi_file[i]);
38075 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38076 +               AuDebugOn(xi->xi_nondir.array[i]);
38077 +       mutex_destroy(&xi->xi_mtx);
38078 +       hbl = &xi->xi_writing;
38079 +       ul = au_hbl_count(hbl);
38080 +       if (unlikely(ul)) {
38081 +               pr_warn("xi_writing %lu\n", ul);
38082 +               hlist_bl_lock(hbl);
38083 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38084 +                       hlist_bl_del(&p->node);
38085 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38086 +                       kfree(p);
38087 +               }
38088 +               hlist_bl_unlock(hbl);
38089 +       }
38090 +       au_kfree_try_rcu(xi->xi_file);
38091 +       au_kfree_try_rcu(xi->xi_nondir.array);
38092 +       au_kfree_rcu(xi);
38093 +}
38094 +
38095 +int au_xino_put(struct au_branch *br)
38096 +{
38097 +       int ret;
38098 +       struct au_xino *xi;
38099 +
38100 +       ret = 0;
38101 +       xi = br->br_xino;
38102 +       if (xi) {
38103 +               br->br_xino = NULL;
38104 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38105 +       }
38106 +
38107 +       return ret;
38108 +}
38109 +
38110 +/* ---------------------------------------------------------------------- */
38111 +
38112 +/*
38113 + * xino mount option handlers
38114 + */
38115 +
38116 +/* xino bitmap */
38117 +static void xino_clear_xib(struct super_block *sb)
38118 +{
38119 +       struct au_sbinfo *sbinfo;
38120 +
38121 +       SiMustWriteLock(sb);
38122 +
38123 +       sbinfo = au_sbi(sb);
38124 +       if (sbinfo->si_xib)
38125 +               fput(sbinfo->si_xib);
38126 +       sbinfo->si_xib = NULL;
38127 +       if (sbinfo->si_xib_buf)
38128 +               free_page((unsigned long)sbinfo->si_xib_buf);
38129 +       sbinfo->si_xib_buf = NULL;
38130 +}
38131 +
38132 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38133 +{
38134 +       int err;
38135 +       loff_t pos;
38136 +       struct au_sbinfo *sbinfo;
38137 +       struct file *file;
38138 +       struct super_block *xi_sb;
38139 +
38140 +       SiMustWriteLock(sb);
38141 +
38142 +       sbinfo = au_sbi(sb);
38143 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38144 +       err = PTR_ERR(file);
38145 +       if (IS_ERR(file))
38146 +               goto out;
38147 +       if (sbinfo->si_xib)
38148 +               fput(sbinfo->si_xib);
38149 +       sbinfo->si_xib = file;
38150 +       xi_sb = file_inode(file)->i_sb;
38151 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38152 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38153 +               err = -EIO;
38154 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38155 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38156 +               goto out_unset;
38157 +       }
38158 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38159 +
38160 +       err = -ENOMEM;
38161 +       if (!sbinfo->si_xib_buf)
38162 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38163 +       if (unlikely(!sbinfo->si_xib_buf))
38164 +               goto out_unset;
38165 +
38166 +       sbinfo->si_xib_last_pindex = 0;
38167 +       sbinfo->si_xib_next_bit = 0;
38168 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38169 +               pos = 0;
38170 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38171 +               if (unlikely(err != PAGE_SIZE))
38172 +                       goto out_free;
38173 +       }
38174 +       err = 0;
38175 +       goto out; /* success */
38176 +
38177 +out_free:
38178 +       if (sbinfo->si_xib_buf)
38179 +               free_page((unsigned long)sbinfo->si_xib_buf);
38180 +       sbinfo->si_xib_buf = NULL;
38181 +       if (err >= 0)
38182 +               err = -EIO;
38183 +out_unset:
38184 +       fput(sbinfo->si_xib);
38185 +       sbinfo->si_xib = NULL;
38186 +out:
38187 +       AuTraceErr(err);
38188 +       return err;
38189 +}
38190 +
38191 +/* xino for each branch */
38192 +static void xino_clear_br(struct super_block *sb)
38193 +{
38194 +       aufs_bindex_t bindex, bbot;
38195 +       struct au_branch *br;
38196 +
38197 +       bbot = au_sbbot(sb);
38198 +       for (bindex = 0; bindex <= bbot; bindex++) {
38199 +               br = au_sbr(sb, bindex);
38200 +               AuDebugOn(!br);
38201 +               au_xino_put(br);
38202 +       }
38203 +}
38204 +
38205 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38206 +                                 aufs_bindex_t bshared)
38207 +{
38208 +       struct au_branch *brshared;
38209 +
38210 +       brshared = au_sbr(sb, bshared);
38211 +       AuDebugOn(!brshared->br_xino);
38212 +       AuDebugOn(!brshared->br_xino->xi_file);
38213 +       if (br->br_xino != brshared->br_xino) {
38214 +               au_xino_get(brshared);
38215 +               au_xino_put(br);
38216 +               br->br_xino = brshared->br_xino;
38217 +       }
38218 +}
38219 +
38220 +struct au_xino_do_set_br {
38221 +       struct au_branch *br;
38222 +       ino_t h_ino;
38223 +       aufs_bindex_t bshared;
38224 +};
38225 +
38226 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38227 +                            struct au_xino_do_set_br *args)
38228 +{
38229 +       int err;
38230 +       struct au_xi_calc calc;
38231 +       struct file *file;
38232 +       struct au_branch *br;
38233 +       struct au_xi_new xinew = {
38234 +               .base = path
38235 +       };
38236 +
38237 +       br = args->br;
38238 +       xinew.xi = br->br_xino;
38239 +       au_xi_calc(sb, args->h_ino, &calc);
38240 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38241 +       if (args->bshared >= 0)
38242 +               /* shared xino */
38243 +               au_xino_set_br_shared(sb, br, args->bshared);
38244 +       else if (!xinew.xi) {
38245 +               /* new xino */
38246 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38247 +               if (unlikely(err))
38248 +                       goto out;
38249 +       }
38250 +
38251 +       /* force re-creating */
38252 +       xinew.xi = br->br_xino;
38253 +       xinew.idx = calc.idx;
38254 +       mutex_lock(&xinew.xi->xi_mtx);
38255 +       file = au_xi_new(sb, &xinew);
38256 +       mutex_unlock(&xinew.xi->xi_mtx);
38257 +       err = PTR_ERR(file);
38258 +       if (IS_ERR(file))
38259 +               goto out;
38260 +       AuDebugOn(!file);
38261 +
38262 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38263 +       if (unlikely(err))
38264 +               au_xino_put(br);
38265 +
38266 +out:
38267 +       AuTraceErr(err);
38268 +       return err;
38269 +}
38270 +
38271 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38272 +{
38273 +       int err;
38274 +       aufs_bindex_t bindex, bbot;
38275 +       struct au_xino_do_set_br args;
38276 +       struct inode *inode;
38277 +
38278 +       SiMustWriteLock(sb);
38279 +
38280 +       bbot = au_sbbot(sb);
38281 +       inode = d_inode(sb->s_root);
38282 +       for (bindex = 0; bindex <= bbot; bindex++) {
38283 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38284 +               args.br = au_sbr(sb, bindex);
38285 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38286 +               err = au_xino_do_set_br(sb, path, &args);
38287 +               if (unlikely(err))
38288 +                       break;
38289 +       }
38290 +
38291 +       AuTraceErr(err);
38292 +       return err;
38293 +}
38294 +
38295 +void au_xino_clr(struct super_block *sb)
38296 +{
38297 +       struct au_sbinfo *sbinfo;
38298 +
38299 +       au_xigen_clr(sb);
38300 +       xino_clear_xib(sb);
38301 +       xino_clear_br(sb);
38302 +       dbgaufs_brs_del(sb, 0);
38303 +       sbinfo = au_sbi(sb);
38304 +       /* lvalue, do not call au_mntflags() */
38305 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38306 +}
38307 +
38308 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38309 +{
38310 +       int err, skip;
38311 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38312 +       struct qstr *dname, *cur_name;
38313 +       struct file *cur_xino;
38314 +       struct au_sbinfo *sbinfo;
38315 +       struct path *path, *cur_path;
38316 +
38317 +       SiMustWriteLock(sb);
38318 +
38319 +       err = 0;
38320 +       sbinfo = au_sbi(sb);
38321 +       path = &xiopt->file->f_path;
38322 +       dentry = path->dentry;
38323 +       parent = dget_parent(dentry);
38324 +       if (remount) {
38325 +               skip = 0;
38326 +               cur_xino = sbinfo->si_xib;
38327 +               if (cur_xino) {
38328 +                       cur_path = &cur_xino->f_path;
38329 +                       cur_dentry = cur_path->dentry;
38330 +                       cur_parent = dget_parent(cur_dentry);
38331 +                       cur_name = &cur_dentry->d_name;
38332 +                       dname = &dentry->d_name;
38333 +                       skip = (cur_parent == parent
38334 +                               && au_qstreq(dname, cur_name));
38335 +                       dput(cur_parent);
38336 +               }
38337 +               if (skip)
38338 +                       goto out;
38339 +       }
38340 +
38341 +       au_opt_set(sbinfo->si_mntflags, XINO);
38342 +       err = au_xino_set_xib(sb, path);
38343 +       /* si_x{read,write} are set */
38344 +       if (!err)
38345 +               err = au_xigen_set(sb, path);
38346 +       if (!err)
38347 +               err = au_xino_set_br(sb, path);
38348 +       if (!err) {
38349 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38350 +               goto out; /* success */
38351 +       }
38352 +
38353 +       /* reset all */
38354 +       AuIOErr("failed setting xino(%d).\n", err);
38355 +       au_xino_clr(sb);
38356 +
38357 +out:
38358 +       dput(parent);
38359 +       return err;
38360 +}
38361 +
38362 +/*
38363 + * create a xinofile at the default place/path.
38364 + */
38365 +struct file *au_xino_def(struct super_block *sb)
38366 +{
38367 +       struct file *file;
38368 +       char *page, *p;
38369 +       struct au_branch *br;
38370 +       struct super_block *h_sb;
38371 +       struct path path;
38372 +       aufs_bindex_t bbot, bindex, bwr;
38373 +
38374 +       br = NULL;
38375 +       bbot = au_sbbot(sb);
38376 +       bwr = -1;
38377 +       for (bindex = 0; bindex <= bbot; bindex++) {
38378 +               br = au_sbr(sb, bindex);
38379 +               if (au_br_writable(br->br_perm)
38380 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38381 +                       bwr = bindex;
38382 +                       break;
38383 +               }
38384 +       }
38385 +
38386 +       if (bwr >= 0) {
38387 +               file = ERR_PTR(-ENOMEM);
38388 +               page = (void *)__get_free_page(GFP_NOFS);
38389 +               if (unlikely(!page))
38390 +                       goto out;
38391 +               path.mnt = au_br_mnt(br);
38392 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38393 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38394 +               file = (void *)p;
38395 +               if (!IS_ERR(p)) {
38396 +                       strcat(p, "/" AUFS_XINO_FNAME);
38397 +                       AuDbg("%s\n", p);
38398 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38399 +               }
38400 +               free_page((unsigned long)page);
38401 +       } else {
38402 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38403 +                                     /*wbrtop*/0);
38404 +               if (IS_ERR(file))
38405 +                       goto out;
38406 +               h_sb = file->f_path.dentry->d_sb;
38407 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38408 +                       pr_err("xino doesn't support %s(%s)\n",
38409 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38410 +                       fput(file);
38411 +                       file = ERR_PTR(-EINVAL);
38412 +               }
38413 +       }
38414 +
38415 +out:
38416 +       return file;
38417 +}
38418 +
38419 +/* ---------------------------------------------------------------------- */
38420 +
38421 +/*
38422 + * initialize the xinofile for the specified branch @br
38423 + * at the place/path where @base_file indicates.
38424 + * test whether another branch is on the same filesystem or not,
38425 + * if found then share the xinofile with another branch.
38426 + */
38427 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38428 +                   struct path *base)
38429 +{
38430 +       int err;
38431 +       struct au_xino_do_set_br args = {
38432 +               .h_ino  = h_ino,
38433 +               .br     = br
38434 +       };
38435 +
38436 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38437 +                                      au_br_sb(br));
38438 +       err = au_xino_do_set_br(sb, base, &args);
38439 +       if (unlikely(err))
38440 +               au_xino_put(br);
38441 +
38442 +       return err;
38443 +}
38444 +
38445 +/* ---------------------------------------------------------------------- */
38446 +
38447 +/*
38448 + * get an unused inode number from bitmap
38449 + */
38450 +ino_t au_xino_new_ino(struct super_block *sb)
38451 +{
38452 +       ino_t ino;
38453 +       unsigned long *p, pindex, ul, pend;
38454 +       struct au_sbinfo *sbinfo;
38455 +       struct file *file;
38456 +       int free_bit, err;
38457 +
38458 +       if (!au_opt_test(au_mntflags(sb), XINO))
38459 +               return iunique(sb, AUFS_FIRST_INO);
38460 +
38461 +       sbinfo = au_sbi(sb);
38462 +       mutex_lock(&sbinfo->si_xib_mtx);
38463 +       p = sbinfo->si_xib_buf;
38464 +       free_bit = sbinfo->si_xib_next_bit;
38465 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38466 +               goto out; /* success */
38467 +       free_bit = find_first_zero_bit(p, page_bits);
38468 +       if (free_bit < page_bits)
38469 +               goto out; /* success */
38470 +
38471 +       pindex = sbinfo->si_xib_last_pindex;
38472 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38473 +               err = xib_pindex(sb, ul);
38474 +               if (unlikely(err))
38475 +                       goto out_err;
38476 +               free_bit = find_first_zero_bit(p, page_bits);
38477 +               if (free_bit < page_bits)
38478 +                       goto out; /* success */
38479 +       }
38480 +
38481 +       file = sbinfo->si_xib;
38482 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38483 +       for (ul = pindex + 1; ul <= pend; ul++) {
38484 +               err = xib_pindex(sb, ul);
38485 +               if (unlikely(err))
38486 +                       goto out_err;
38487 +               free_bit = find_first_zero_bit(p, page_bits);
38488 +               if (free_bit < page_bits)
38489 +                       goto out; /* success */
38490 +       }
38491 +       BUG();
38492 +
38493 +out:
38494 +       set_bit(free_bit, p);
38495 +       sbinfo->si_xib_next_bit = free_bit + 1;
38496 +       pindex = sbinfo->si_xib_last_pindex;
38497 +       mutex_unlock(&sbinfo->si_xib_mtx);
38498 +       ino = xib_calc_ino(pindex, free_bit);
38499 +       AuDbg("i%lu\n", (unsigned long)ino);
38500 +       return ino;
38501 +out_err:
38502 +       mutex_unlock(&sbinfo->si_xib_mtx);
38503 +       AuDbg("i0\n");
38504 +       return 0;
38505 +}
38506 +
38507 +/* for s_op->delete_inode() */
38508 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38509 +{
38510 +       int err;
38511 +       unsigned int mnt_flags;
38512 +       aufs_bindex_t bindex, bbot, bi;
38513 +       unsigned char try_trunc;
38514 +       struct au_iinfo *iinfo;
38515 +       struct super_block *sb;
38516 +       struct au_hinode *hi;
38517 +       struct inode *h_inode;
38518 +       struct au_branch *br;
38519 +       struct au_xi_calc calc;
38520 +       struct file *file;
38521 +
38522 +       AuDebugOn(au_is_bad_inode(inode));
38523 +
38524 +       sb = inode->i_sb;
38525 +       mnt_flags = au_mntflags(sb);
38526 +       if (!au_opt_test(mnt_flags, XINO)
38527 +           || inode->i_ino == AUFS_ROOT_INO)
38528 +               return;
38529 +
38530 +       if (unlinked) {
38531 +               au_xigen_inc(inode);
38532 +               au_xib_clear_bit(inode);
38533 +       }
38534 +
38535 +       iinfo = au_ii(inode);
38536 +       bindex = iinfo->ii_btop;
38537 +       if (bindex < 0)
38538 +               return;
38539 +
38540 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38541 +       hi = au_hinode(iinfo, bindex);
38542 +       bbot = iinfo->ii_bbot;
38543 +       for (; bindex <= bbot; bindex++, hi++) {
38544 +               h_inode = hi->hi_inode;
38545 +               if (!h_inode
38546 +                   || (!unlinked && h_inode->i_nlink))
38547 +                       continue;
38548 +
38549 +               /* inode may not be revalidated */
38550 +               bi = au_br_index(sb, hi->hi_id);
38551 +               if (bi < 0)
38552 +                       continue;
38553 +
38554 +               br = au_sbr(sb, bi);
38555 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38556 +               file = au_xino_file(br->br_xino, calc.idx);
38557 +               if (IS_ERR_OR_NULL(file))
38558 +                       continue;
38559 +
38560 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38561 +               if (!err && try_trunc
38562 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38563 +                       xino_try_trunc(sb, br);
38564 +       }
38565 +}
38566 +
38567 +/* ---------------------------------------------------------------------- */
38568 +
38569 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38570 +{
38571 +       int found, total, i;
38572 +
38573 +       found = -1;
38574 +       total = xi->xi_nondir.total;
38575 +       for (i = 0; i < total; i++) {
38576 +               if (xi->xi_nondir.array[i] != h_ino)
38577 +                       continue;
38578 +               found = i;
38579 +               break;
38580 +       }
38581 +
38582 +       return found;
38583 +}
38584 +
38585 +static int au_xinondir_expand(struct au_xino *xi)
38586 +{
38587 +       int err, sz;
38588 +       ino_t *p;
38589 +
38590 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38591 +
38592 +       err = -ENOMEM;
38593 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38594 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38595 +               goto out;
38596 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38597 +                        /*may_shrink*/0);
38598 +       if (p) {
38599 +               xi->xi_nondir.array = p;
38600 +               xi->xi_nondir.total <<= 1;
38601 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38602 +               err = 0;
38603 +       }
38604 +
38605 +out:
38606 +       return err;
38607 +}
38608 +
38609 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38610 +                      ino_t h_ino, int idx)
38611 +{
38612 +       struct au_xino *xi;
38613 +
38614 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38615 +       xi = au_sbr(sb, bindex)->br_xino;
38616 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38617 +
38618 +       spin_lock(&xi->xi_nondir.spin);
38619 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38620 +       xi->xi_nondir.array[idx] = 0;
38621 +       spin_unlock(&xi->xi_nondir.spin);
38622 +       wake_up_all(&xi->xi_nondir.wqh);
38623 +}
38624 +
38625 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38626 +                     int *idx)
38627 +{
38628 +       int err, found, empty;
38629 +       struct au_xino *xi;
38630 +
38631 +       err = 0;
38632 +       *idx = -1;
38633 +       if (!au_opt_test(au_mntflags(sb), XINO))
38634 +               goto out; /* no xino */
38635 +
38636 +       xi = au_sbr(sb, bindex)->br_xino;
38637 +
38638 +again:
38639 +       spin_lock(&xi->xi_nondir.spin);
38640 +       found = au_xinondir_find(xi, h_ino);
38641 +       if (found == -1) {
38642 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38643 +               if (empty == -1) {
38644 +                       empty = xi->xi_nondir.total;
38645 +                       err = au_xinondir_expand(xi);
38646 +                       if (unlikely(err))
38647 +                               goto out_unlock;
38648 +               }
38649 +               xi->xi_nondir.array[empty] = h_ino;
38650 +               *idx = empty;
38651 +       } else {
38652 +               spin_unlock(&xi->xi_nondir.spin);
38653 +               wait_event(xi->xi_nondir.wqh,
38654 +                          xi->xi_nondir.array[found] != h_ino);
38655 +               goto again;
38656 +       }
38657 +
38658 +out_unlock:
38659 +       spin_unlock(&xi->xi_nondir.spin);
38660 +out:
38661 +       return err;
38662 +}
38663 +
38664 +/* ---------------------------------------------------------------------- */
38665 +
38666 +int au_xino_path(struct seq_file *seq, struct file *file)
38667 +{
38668 +       int err;
38669 +
38670 +       err = au_seq_path(seq, &file->f_path);
38671 +       if (unlikely(err))
38672 +               goto out;
38673 +
38674 +#define Deleted "\\040(deleted)"
38675 +       seq->count -= sizeof(Deleted) - 1;
38676 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38677 +                        sizeof(Deleted) - 1));
38678 +#undef Deleted
38679 +
38680 +out:
38681 +       return err;
38682 +}
38683 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38684 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38685 +++ linux/include/uapi/linux/aufs_type.h        2022-01-13 12:42:15.932899468 +0100
38686 @@ -0,0 +1,452 @@
38687 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38688 +/*
38689 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38690 + *
38691 + * This program, aufs is free software; you can redistribute it and/or modify
38692 + * it under the terms of the GNU General Public License as published by
38693 + * the Free Software Foundation; either version 2 of the License, or
38694 + * (at your option) any later version.
38695 + *
38696 + * This program is distributed in the hope that it will be useful,
38697 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38698 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38699 + * GNU General Public License for more details.
38700 + *
38701 + * You should have received a copy of the GNU General Public License
38702 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38703 + */
38704 +
38705 +#ifndef __AUFS_TYPE_H__
38706 +#define __AUFS_TYPE_H__
38707 +
38708 +#define AUFS_NAME      "aufs"
38709 +
38710 +#ifdef __KERNEL__
38711 +/*
38712 + * define it before including all other headers.
38713 + * sched.h may use pr_* macros before defining "current", so define the
38714 + * no-current version first, and re-define later.
38715 + */
38716 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38717 +#include <linux/sched.h>
38718 +#undef pr_fmt
38719 +#define pr_fmt(fmt) \
38720 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38721 +               (int)sizeof(current->comm), current->comm, current->pid
38722 +#include <linux/limits.h>
38723 +#else
38724 +#include <stdint.h>
38725 +#include <sys/types.h>
38726 +#include <limits.h>
38727 +#endif /* __KERNEL__ */
38728 +
38729 +#define AUFS_VERSION   "5.x-rcN-20211129"
38730 +
38731 +/* todo? move this to linux-2.6.19/include/magic.h */
38732 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38733 +
38734 +/* ---------------------------------------------------------------------- */
38735 +
38736 +#ifdef __KERNEL__
38737 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38738 +typedef int8_t aufs_bindex_t;
38739 +#define AUFS_BRANCH_MAX 127
38740 +#else
38741 +typedef int16_t aufs_bindex_t;
38742 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38743 +#define AUFS_BRANCH_MAX 511
38744 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38745 +#define AUFS_BRANCH_MAX 1023
38746 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38747 +#define AUFS_BRANCH_MAX 32767
38748 +#endif
38749 +#endif
38750 +
38751 +#ifndef AUFS_BRANCH_MAX
38752 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38753 +#endif
38754 +#endif /* __KERNEL__ */
38755 +
38756 +/* ---------------------------------------------------------------------- */
38757 +
38758 +#define AUFS_FSTYPE            AUFS_NAME
38759 +
38760 +#define AUFS_ROOT_INO          2
38761 +#define AUFS_FIRST_INO         11
38762 +
38763 +#define AUFS_WH_PFX            ".wh."
38764 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38765 +#define AUFS_WH_TMP_LEN                4
38766 +/* a limit for rmdir/rename a dir and copyup */
38767 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38768 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38769 +                               - 1                     /* dot */\
38770 +                               - AUFS_WH_TMP_LEN)      /* hex */
38771 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38772 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38773 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38774 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38775 +#define AUFS_DIRWH_DEF         3
38776 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38777 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38778 +#define AUFS_RDBLK_DEF         512 /* bytes */
38779 +#define AUFS_RDHASH_DEF                32
38780 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38781 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38782 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38783 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38784 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38785 +
38786 +/* pseudo-link maintenace under /proc */
38787 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38788 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38789 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38790 +
38791 +/* dirren, renamed dir */
38792 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38793 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38794 +/* whiteouted doubly */
38795 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38796 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38797 +
38798 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38799 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38800 +
38801 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38802 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38803 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38804 +
38805 +/* doubly whiteouted */
38806 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38807 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38808 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38809 +
38810 +/* branch permissions and attributes */
38811 +#define AUFS_BRPERM_RW         "rw"
38812 +#define AUFS_BRPERM_RO         "ro"
38813 +#define AUFS_BRPERM_RR         "rr"
38814 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38815 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38816 +#define AUFS_BRATTR_FHSM       "fhsm"
38817 +#define AUFS_BRATTR_UNPIN      "unpin"
38818 +#define AUFS_BRATTR_ICEX       "icex"
38819 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38820 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38821 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38822 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38823 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38824 +#define AUFS_BRRATTR_WH                "wh"
38825 +#define AUFS_BRWATTR_NLWH      "nolwh"
38826 +#define AUFS_BRWATTR_MOO       "moo"
38827 +
38828 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38829 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38830 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38831 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38832 +
38833 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38834 +#define AuBrAttr_COO_ALL       (1 << 4)
38835 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38836 +
38837 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38838 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38839 +                                                  branch. meaningless since
38840 +                                                  linux-3.18-rc1 */
38841 +
38842 +/* ignore error in copying XATTR */
38843 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38844 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38845 +#define AuBrAttr_ICEX_TR       (1 << 9)
38846 +#define AuBrAttr_ICEX_USR      (1 << 10)
38847 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38848 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38849 +                                | AuBrAttr_ICEX_SYS    \
38850 +                                | AuBrAttr_ICEX_TR     \
38851 +                                | AuBrAttr_ICEX_USR    \
38852 +                                | AuBrAttr_ICEX_OTH)
38853 +
38854 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38855 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38856 +
38857 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38858 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38859 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38860 +
38861 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38862 +
38863 +/* #warning test userspace */
38864 +#ifdef __KERNEL__
38865 +#ifndef CONFIG_AUFS_FHSM
38866 +#undef AuBrAttr_FHSM
38867 +#define AuBrAttr_FHSM          0
38868 +#endif
38869 +#ifndef CONFIG_AUFS_XATTR
38870 +#undef AuBrAttr_ICEX
38871 +#define AuBrAttr_ICEX          0
38872 +#undef AuBrAttr_ICEX_SEC
38873 +#define AuBrAttr_ICEX_SEC      0
38874 +#undef AuBrAttr_ICEX_SYS
38875 +#define AuBrAttr_ICEX_SYS      0
38876 +#undef AuBrAttr_ICEX_TR
38877 +#define AuBrAttr_ICEX_TR       0
38878 +#undef AuBrAttr_ICEX_USR
38879 +#define AuBrAttr_ICEX_USR      0
38880 +#undef AuBrAttr_ICEX_OTH
38881 +#define AuBrAttr_ICEX_OTH      0
38882 +#endif
38883 +#endif
38884 +
38885 +/* the longest combination */
38886 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38887 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38888 +                              "+" AUFS_BRATTR_COO_REG          \
38889 +                              "+" AUFS_BRATTR_FHSM             \
38890 +                              "+" AUFS_BRATTR_UNPIN            \
38891 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38892 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38893 +                              "+" AUFS_BRATTR_ICEX_USR         \
38894 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38895 +                              "+" AUFS_BRWATTR_NLWH)
38896 +
38897 +typedef struct {
38898 +       char a[AuBrPermStrSz];
38899 +} au_br_perm_str_t;
38900 +
38901 +static inline int au_br_writable(int brperm)
38902 +{
38903 +       return brperm & AuBrPerm_RW;
38904 +}
38905 +
38906 +static inline int au_br_whable(int brperm)
38907 +{
38908 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38909 +}
38910 +
38911 +static inline int au_br_wh_linkable(int brperm)
38912 +{
38913 +       return !(brperm & AuBrWAttr_NoLinkWH);
38914 +}
38915 +
38916 +static inline int au_br_cmoo(int brperm)
38917 +{
38918 +       return brperm & AuBrAttr_CMOO_Mask;
38919 +}
38920 +
38921 +static inline int au_br_fhsm(int brperm)
38922 +{
38923 +       return brperm & AuBrAttr_FHSM;
38924 +}
38925 +
38926 +/* ---------------------------------------------------------------------- */
38927 +
38928 +/* ioctl */
38929 +enum {
38930 +       /* readdir in userspace */
38931 +       AuCtl_RDU,
38932 +       AuCtl_RDU_INO,
38933 +
38934 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38935 +       AuCtl_IBUSY,    /* busy inode */
38936 +       AuCtl_MVDOWN,   /* move-down */
38937 +       AuCtl_BR,       /* info about branches */
38938 +       AuCtl_FHSM_FD   /* connection for fhsm */
38939 +};
38940 +
38941 +/* borrowed from linux/include/linux/kernel.h */
38942 +#ifndef ALIGN
38943 +#ifdef _GNU_SOURCE
38944 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38945 +#else
38946 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
38947 +#endif
38948 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38949 +#endif
38950 +
38951 +/* borrowed from linux/include/linux/compiler-gcc3.h */
38952 +#ifndef __aligned
38953 +#define __aligned(x)                   __attribute__((aligned(x)))
38954 +#endif
38955 +
38956 +#ifdef __KERNEL__
38957 +#ifndef __packed
38958 +#define __packed                       __attribute__((packed))
38959 +#endif
38960 +#endif
38961 +
38962 +struct au_rdu_cookie {
38963 +       uint64_t        h_pos;
38964 +       int16_t         bindex;
38965 +       uint8_t         flags;
38966 +       uint8_t         pad;
38967 +       uint32_t        generation;
38968 +} __aligned(8);
38969 +
38970 +struct au_rdu_ent {
38971 +       uint64_t        ino;
38972 +       int16_t         bindex;
38973 +       uint8_t         type;
38974 +       uint8_t         nlen;
38975 +       uint8_t         wh;
38976 +       char            name[];
38977 +} __aligned(8);
38978 +
38979 +static inline int au_rdu_len(int nlen)
38980 +{
38981 +       /* include the terminating NULL */
38982 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38983 +                    sizeof(uint64_t));
38984 +}
38985 +
38986 +union au_rdu_ent_ul {
38987 +       struct au_rdu_ent __user        *e;
38988 +       uint64_t                        ul;
38989 +};
38990 +
38991 +enum {
38992 +       AufsCtlRduV_SZ,
38993 +       AufsCtlRduV_End
38994 +};
38995 +
38996 +struct aufs_rdu {
38997 +       /* input */
38998 +       union {
38999 +               uint64_t        sz;     /* AuCtl_RDU */
39000 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39001 +       };
39002 +       union au_rdu_ent_ul     ent;
39003 +       uint16_t                verify[AufsCtlRduV_End];
39004 +
39005 +       /* input/output */
39006 +       uint32_t                blk;
39007 +
39008 +       /* output */
39009 +       union au_rdu_ent_ul     tail;
39010 +       /* number of entries which were added in a single call */
39011 +       uint64_t                rent;
39012 +       uint8_t                 full;
39013 +       uint8_t                 shwh;
39014 +
39015 +       struct au_rdu_cookie    cookie;
39016 +} __aligned(8);
39017 +
39018 +/* ---------------------------------------------------------------------- */
39019 +
39020 +/* dirren. the branch is identified by the filename who contains this */
39021 +struct au_drinfo {
39022 +       uint64_t ino;
39023 +       union {
39024 +               uint8_t oldnamelen;
39025 +               uint64_t _padding;
39026 +       };
39027 +       uint8_t oldname[];
39028 +} __aligned(8);
39029 +
39030 +struct au_drinfo_fdata {
39031 +       uint32_t magic;
39032 +       struct au_drinfo drinfo;
39033 +} __aligned(8);
39034 +
39035 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39036 +/* future */
39037 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39038 +
39039 +/* ---------------------------------------------------------------------- */
39040 +
39041 +struct aufs_wbr_fd {
39042 +       uint32_t        oflags;
39043 +       int16_t         brid;
39044 +} __aligned(8);
39045 +
39046 +/* ---------------------------------------------------------------------- */
39047 +
39048 +struct aufs_ibusy {
39049 +       uint64_t        ino, h_ino;
39050 +       int16_t         bindex;
39051 +} __aligned(8);
39052 +
39053 +/* ---------------------------------------------------------------------- */
39054 +
39055 +/* error code for move-down */
39056 +/* the actual message strings are implemented in aufs-util.git */
39057 +enum {
39058 +       EAU_MVDOWN_OPAQUE = 1,
39059 +       EAU_MVDOWN_WHITEOUT,
39060 +       EAU_MVDOWN_UPPER,
39061 +       EAU_MVDOWN_BOTTOM,
39062 +       EAU_MVDOWN_NOUPPER,
39063 +       EAU_MVDOWN_NOLOWERBR,
39064 +       EAU_Last
39065 +};
39066 +
39067 +/* flags for move-down */
39068 +#define AUFS_MVDOWN_DMSG       1
39069 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39070 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39071 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39072 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39073 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39074 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39075 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39076 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39077 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39078 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39079 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39080 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39081 +
39082 +/* index for move-down */
39083 +enum {
39084 +       AUFS_MVDOWN_UPPER,
39085 +       AUFS_MVDOWN_LOWER,
39086 +       AUFS_MVDOWN_NARRAY
39087 +};
39088 +
39089 +/*
39090 + * additional info of move-down
39091 + * number of free blocks and inodes.
39092 + * subset of struct kstatfs, but smaller and always 64bit.
39093 + */
39094 +struct aufs_stfs {
39095 +       uint64_t        f_blocks;
39096 +       uint64_t        f_bavail;
39097 +       uint64_t        f_files;
39098 +       uint64_t        f_ffree;
39099 +};
39100 +
39101 +struct aufs_stbr {
39102 +       int16_t                 brid;   /* optional input */
39103 +       int16_t                 bindex; /* output */
39104 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39105 +} __aligned(8);
39106 +
39107 +struct aufs_mvdown {
39108 +       uint32_t                flags;                  /* input/output */
39109 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39110 +       int8_t                  au_errno;               /* output */
39111 +} __aligned(8);
39112 +
39113 +/* ---------------------------------------------------------------------- */
39114 +
39115 +union aufs_brinfo {
39116 +       /* PATH_MAX may differ between kernel-space and user-space */
39117 +       char    _spacer[4096];
39118 +       struct {
39119 +               int16_t id;
39120 +               int     perm;
39121 +               char    path[];
39122 +       };
39123 +} __aligned(8);
39124 +
39125 +/* ---------------------------------------------------------------------- */
39126 +
39127 +#define AuCtlType              'A'
39128 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39129 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39130 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39131 +                                    struct aufs_wbr_fd)
39132 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39133 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39134 +                                     struct aufs_mvdown)
39135 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39136 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39137 +
39138 +#endif /* __AUFS_TYPE_H__ */
39139 SPDX-License-Identifier: GPL-2.0
39140 aufs5.x-rcN loopback patch
39141
39142 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39143 index 52776d48e306a..64112f1cf8b18 100644
39144 --- a/drivers/block/loop.c
39145 +++ b/drivers/block/loop.c
39146 @@ -509,6 +509,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39147                                 lo->use_dio);
39148  }
39149  
39150 +static struct file *loop_real_file(struct file *file)
39151 +{
39152 +       struct file *f = NULL;
39153 +
39154 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39155 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39156 +       return f;
39157 +}
39158 +
39159  static void loop_reread_partitions(struct loop_device *lo)
39160  {
39161         int rc;
39162 @@ -566,6 +575,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39163  {
39164         struct file *file = fget(arg);
39165         struct file *old_file;
39166 +       struct file *f, *virt_file = NULL, *old_virt_file;
39167         int error;
39168         bool partscan;
39169         bool is_loop;
39170 @@ -585,11 +595,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39171         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39172                 goto out_err;
39173  
39174 +       f = loop_real_file(file);
39175 +       if (f) {
39176 +               virt_file = file;
39177 +               file = f;
39178 +               get_file(file);
39179 +       }
39180 +
39181         error = loop_validate_file(file, bdev);
39182         if (error)
39183                 goto out_err;
39184  
39185         old_file = lo->lo_backing_file;
39186 +       old_virt_file = lo->lo_backing_virt_file;
39187  
39188         error = -EINVAL;
39189  
39190 @@ -602,6 +620,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39191         blk_mq_freeze_queue(lo->lo_queue);
39192         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39193         lo->lo_backing_file = file;
39194 +       lo->lo_backing_virt_file = virt_file;
39195         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39196         mapping_set_gfp_mask(file->f_mapping,
39197                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39198 @@ -624,6 +643,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39199          * dependency.
39200          */
39201         fput(old_file);
39202 +       if (old_virt_file)
39203 +               fput(old_virt_file);
39204         if (partscan)
39205                 loop_reread_partitions(lo);
39206         return 0;
39207 @@ -632,6 +653,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39208         loop_global_unlock(lo, is_loop);
39209  out_putf:
39210         fput(file);
39211 +       if (virt_file)
39212 +               fput(virt_file);
39213         return error;
39214  }
39215  
39216 @@ -965,6 +988,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39217                           const struct loop_config *config)
39218  {
39219         struct file *file = fget(config->fd);
39220 +       struct file *f, *virt_file = NULL;
39221         struct inode *inode;
39222         struct address_space *mapping;
39223         int error;
39224 @@ -980,6 +1004,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39225         /* This is safe, since we have a reference from open(). */
39226         __module_get(THIS_MODULE);
39227  
39228 +       f = loop_real_file(file);
39229 +       if (f) {
39230 +               virt_file = file;
39231 +               file = f;
39232 +               get_file(file);
39233 +       }
39234 +
39235         /*
39236          * If we don't hold exclusive handle for the device, upgrade to it
39237          * here to avoid changing device under exclusive owner.
39238 @@ -1045,6 +1076,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39239         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39240         lo->lo_device = bdev;
39241         lo->lo_backing_file = file;
39242 +       lo->lo_backing_virt_file = virt_file;
39243         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39244         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39245  
39246 @@ -1095,6 +1127,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39247                 bd_abort_claiming(bdev, loop_configure);
39248  out_putf:
39249         fput(file);
39250 +       if (virt_file)
39251 +               fput(virt_file);
39252         /* This is safe: open() is still holding a reference. */
39253         module_put(THIS_MODULE);
39254         return error;
39255 @@ -1103,6 +1137,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39256  static int __loop_clr_fd(struct loop_device *lo, bool release)
39257  {
39258         struct file *filp = NULL;
39259 +       struct file *virt_filp = lo->lo_backing_virt_file;
39260         gfp_t gfp = lo->old_gfp_mask;
39261         int err = 0;
39262         bool partscan = false;
39263 @@ -1153,6 +1188,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39264  
39265         spin_lock_irq(&lo->lo_lock);
39266         lo->lo_backing_file = NULL;
39267 +       lo->lo_backing_virt_file = NULL;
39268         spin_unlock_irq(&lo->lo_lock);
39269  
39270         lo->lo_device = NULL;
39271 @@ -1220,6 +1256,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39272          */
39273         if (filp)
39274                 fput(filp);
39275 +       if (virt_filp)
39276 +               fput(virt_filp);
39277         return err;
39278  }
39279  
39280 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39281 index 082d4b6bfc6a6..61feca64688df 100644
39282 --- a/drivers/block/loop.h
39283 +++ b/drivers/block/loop.h
39284 @@ -34,7 +34,7 @@ struct loop_device {
39285         int             lo_flags;
39286         char            lo_file_name[LO_NAME_SIZE];
39287  
39288 -       struct file *   lo_backing_file;
39289 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39290         struct block_device *lo_device;
39291  
39292         gfp_t           old_gfp_mask;
39293 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39294 index 99ceca1440449..e49dfe8550329 100644
39295 --- a/fs/aufs/f_op.c
39296 +++ b/fs/aufs/f_op.c
39297 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39298         if (IS_ERR(h_file))
39299                 goto out;
39300  
39301 -       if (au_test_loopback_kthread()) {
39302 +       if (0 && au_test_loopback_kthread()) {
39303                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39304                 if (file->f_mapping != h_file->f_mapping) {
39305                         file->f_mapping = h_file->f_mapping;
39306 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39307 index 74347bd75b380..5ef888a1d53f4 100644
39308 --- a/fs/aufs/loop.c
39309 +++ b/fs/aufs/loop.c
39310 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39311                 symbol_put(loop_backing_file);
39312         au_kfree_try_rcu(au_warn_loopback_array);
39313  }
39314 +
39315 +/* ---------------------------------------------------------------------- */
39316 +
39317 +/* support the loopback block device insude aufs */
39318 +
39319 +struct file *aufs_real_loop(struct file *file)
39320 +{
39321 +       struct file *f;
39322 +
39323 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39324 +       fi_read_lock(file);
39325 +       f = au_hf_top(file);
39326 +       fi_read_unlock(file);
39327 +       AuDebugOn(!f);
39328 +       return f;
39329 +}
39330 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39331 index 7293bee427f96..3345c098d0d47 100644
39332 --- a/fs/aufs/loop.h
39333 +++ b/fs/aufs/loop.h
39334 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39335  
39336  int au_loopback_init(void);
39337  void au_loopback_fin(void);
39338 +
39339 +struct file *aufs_real_loop(struct file *file);
39340  #else
39341  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39342  
39343 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39344  
39345  AuStubInt0(au_loopback_init, void)
39346  AuStubVoid(au_loopback_fin, void)
39347 +
39348 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39349  #endif /* BLK_DEV_LOOP */
39350  
39351  #endif /* __KERNEL__ */
39352 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39353 index cfa90537d9d38..028ee1ceeed1a 100644
39354 --- a/fs/aufs/super.c
39355 +++ b/fs/aufs/super.c
39356 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39357         .statfs         = aufs_statfs,
39358         .put_super      = aufs_put_super,
39359         .sync_fs        = aufs_sync_fs,
39360 -       .remount_fs     = aufs_remount_fs
39361 +       .remount_fs     = aufs_remount_fs,
39362 +#ifdef CONFIG_AUFS_BDEV_LOOP
39363 +       .real_loop      = aufs_real_loop
39364 +#endif
39365  };
39366  
39367  /* ---------------------------------------------------------------------- */
39368 diff --git a/include/linux/fs.h b/include/linux/fs.h
39369 index 02ce64c28ef15..eed5ad4a414ac 100644
39370 --- a/include/linux/fs.h
39371 +++ b/include/linux/fs.h
39372 @@ -2225,6 +2225,10 @@ struct super_operations {
39373                                   struct shrink_control *);
39374         long (*free_cached_objects)(struct super_block *,
39375                                     struct shrink_control *);
39376 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39377 +       /* and aufs */
39378 +       struct file *(*real_loop)(struct file *);
39379 +#endif
39380  };
39381  
39382  /*
This page took 3.076746 seconds and 3 git commands to generate.