]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
- up to 5.11.8
[packages/kernel.git] / kernel-aufs5.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs5.11 kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index da524c4d7b7e0..50ab89368c2b5 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -288,6 +288,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 999d1a23f036c..0cd76857ca764 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -136,3 +136,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.11 base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index bfc1b86e3e733..cf773a5624338 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3017,6 +3017,19 @@ F:       include/linux/audit.h
33  F:     include/uapi/linux/audit.h
34  F:     kernel/audit*
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 Sandonis <miguel.ojeda.sandonis@gmail.com>
51  S:     Maintained
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index e5ff328f09175..39d539df0349d 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -761,6 +761,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 97e81a844a966..a7522ebbca659 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1292,7 +1292,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 05b36b28f2e87..e747a47a97da4 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -32,7 +32,7 @@
99  
100  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101  
102 -static int setfl(int fd, struct file * filp, unsigned long arg)
103 +int setfl(int fd, struct file *filp, unsigned long arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/inode.c b/fs/inode.c
117 index 6442d97d9a4ab..e4a25c3fa9e55 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1769,7 +1769,7 @@ EXPORT_SYMBOL(generic_update_time);
121   * This does the actual work of updating an inodes time or version.  Must have
122   * had called mnt_want_write() before calling this.
123   */
124 -static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125 +int update_time(struct inode *inode, struct timespec64 *time, int flags)
126  {
127         if (inode->i_op->update_time)
128                 return inode->i_op->update_time(inode, time, flags);
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index 9d33909d0f9e3..3e16fc64df8b8 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -792,6 +792,12 @@ static inline int check_mnt(struct mount *mnt)
134         return mnt->mnt_ns == current->nsproxy->mnt_ns;
135  }
136  
137 +/* for aufs, CONFIG_AUFS_BR_FUSE */
138 +int is_current_mnt_ns(struct vfsmount *mnt)
139 +{
140 +       return check_mnt(real_mount(mnt));
141 +}
142 +
143  /*
144   * vfsmount lock must be held for write
145   */
146 diff --git a/fs/splice.c b/fs/splice.c
147 index 866d5c2367b23..55b5356262085 100644
148 --- a/fs/splice.c
149 +++ b/fs/splice.c
150 @@ -756,8 +756,8 @@ static int warn_unsupported(struct file *file, const char *op)
151  /*
152   * Attempt to initiate a splice from pipe to file.
153   */
154 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155 -                          loff_t *ppos, size_t len, unsigned int flags)
156 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
157 +                   loff_t *ppos, size_t len, unsigned int flags)
158  {
159         if (unlikely(!out->f_op->splice_write))
160                 return warn_unsupported(out, "write");
161 @@ -767,9 +767,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
162  /*
163   * Attempt to initiate a splice from a file to a pipe.
164   */
165 -static long do_splice_to(struct file *in, loff_t *ppos,
166 -                        struct pipe_inode_info *pipe, size_t len,
167 -                        unsigned int flags)
168 +long do_splice_to(struct file *in, loff_t *ppos,
169 +                 struct pipe_inode_info *pipe, size_t len,
170 +                 unsigned int flags)
171  {
172         int ret;
173  
174 diff --git a/fs/sync.c b/fs/sync.c
175 index 1373a610dc784..b7b5a0a0df6ff 100644
176 --- a/fs/sync.c
177 +++ b/fs/sync.c
178 @@ -28,7 +28,7 @@
179   * wait == 1 case since in that case write_inode() functions do
180   * sync_dirty_buffer() and thus effectively write one block at a time.
181   */
182 -static int __sync_filesystem(struct super_block *sb, int wait)
183 +int __sync_filesystem(struct super_block *sb, int wait)
184  {
185         if (wait)
186                 sync_inodes_sb(sb);
187 diff --git a/include/linux/fs.h b/include/linux/fs.h
188 index fd47deea7c176..91f3fbe5b57ff 100644
189 --- a/include/linux/fs.h
190 +++ b/include/linux/fs.h
191 @@ -1330,6 +1330,7 @@ extern void fasync_free(struct fasync_struct *);
192  /* can be called from interrupts */
193  extern void kill_fasync(struct fasync_struct **, int, int);
194  
195 +extern int setfl(int fd, struct file *filp, unsigned long arg);
196  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
197  extern int f_setown(struct file *filp, unsigned long arg, int force);
198  extern void f_delown(struct file *filp);
199 @@ -1841,6 +1842,7 @@ struct file_operations {
200         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
201         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
202         int (*check_flags)(int);
203 +       int (*setfl)(struct file *, unsigned long);
204         int (*flock) (struct file *, int, struct file_lock *);
205         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
206         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
207 @@ -2326,6 +2328,7 @@ extern int current_umask(void);
208  extern void ihold(struct inode * inode);
209  extern void iput(struct inode *);
210  extern int generic_update_time(struct inode *, struct timespec64 *, int);
211 +extern int update_time(struct inode *, struct timespec64 *, int);
212  
213  /* /sys/fs */
214  extern struct kobject *fs_kobj;
215 @@ -2562,6 +2565,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
216  }
217  
218  void emergency_thaw_all(void);
219 +extern int __sync_filesystem(struct super_block *, int);
220  extern int sync_filesystem(struct super_block *);
221  extern const struct file_operations def_blk_fops;
222  extern const struct file_operations def_chr_fops;
223 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
224 index b9e9adec73e8b..e152c49cc3163 100644
225 --- a/include/linux/lockdep.h
226 +++ b/include/linux/lockdep.h
227 @@ -241,6 +241,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
228         return lock->key == key;
229  }
230  
231 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
232 +
233  /*
234   * Acquire a lock.
235   *
236 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
237 index 8f882f5881e87..6b9808f098435 100644
238 --- a/include/linux/mnt_namespace.h
239 +++ b/include/linux/mnt_namespace.h
240 @@ -7,12 +7,15 @@ struct mnt_namespace;
241  struct fs_struct;
242  struct user_namespace;
243  struct ns_common;
244 +struct vfsmount;
245  
246  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
247                 struct user_namespace *, struct fs_struct *);
248  extern void put_mnt_ns(struct mnt_namespace *ns);
249  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
250  
251 +extern int is_current_mnt_ns(struct vfsmount *mnt);
252 +
253  extern const struct file_operations proc_mounts_operations;
254  extern const struct file_operations proc_mountinfo_operations;
255  extern const struct file_operations proc_mountstats_operations;
256 diff --git a/include/linux/splice.h b/include/linux/splice.h
257 index a55179fd60fc3..8e21c53cf8831 100644
258 --- a/include/linux/splice.h
259 +++ b/include/linux/splice.h
260 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
261  
262  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
263  extern const struct pipe_buf_operations default_pipe_buf_ops;
264 +
265 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
266 +                          loff_t *ppos, size_t len, unsigned int flags);
267 +extern long do_splice_to(struct file *in, loff_t *ppos,
268 +                        struct pipe_inode_info *pipe, size_t len,
269 +                        unsigned int flags);
270  #endif
271 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
272 index bdaf4829098c0..5c3bff75de111 100644
273 --- a/kernel/locking/lockdep.c
274 +++ b/kernel/locking/lockdep.c
275 @@ -188,7 +188,7 @@ static
276  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
277  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
278  
279 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
280 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
281  {
282         unsigned int class_idx = hlock->class_idx;
283  
284 @@ -209,6 +209,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
285          */
286         return lock_classes + class_idx;
287  }
288 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
289  
290  #ifdef CONFIG_LOCK_STAT
291  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
292 SPDX-License-Identifier: GPL-2.0
293 aufs5.11 mmap patch
294
295 diff --git a/fs/proc/base.c b/fs/proc/base.c
296 index b3422cda2a91e..bda8e8ece7208 100644
297 --- a/fs/proc/base.c
298 +++ b/fs/proc/base.c
299 @@ -2184,7 +2184,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
300         rc = -ENOENT;
301         vma = find_exact_vma(mm, vm_start, vm_end);
302         if (vma && vma->vm_file) {
303 -               *path = vma->vm_file->f_path;
304 +               *path = vma_pr_or_file(vma)->f_path;
305                 path_get(path);
306                 rc = 0;
307         }
308 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
309 index 13452b32e2bd5..38acccfef9d49 100644
310 --- a/fs/proc/nommu.c
311 +++ b/fs/proc/nommu.c
312 @@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
313         file = region->vm_file;
314  
315         if (file) {
316 -               struct inode *inode = file_inode(region->vm_file);
317 +               struct inode *inode;
318 +
319 +               file = vmr_pr_or_file(region);
320 +               inode = file_inode(file);
321                 dev = inode->i_sb->s_dev;
322                 ino = inode->i_ino;
323         }
324 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
325 index 602e3a52884d8..a36614b84de16 100644
326 --- a/fs/proc/task_mmu.c
327 +++ b/fs/proc/task_mmu.c
328 @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
329         const char *name = NULL;
330  
331         if (file) {
332 -               struct inode *inode = file_inode(vma->vm_file);
333 +               struct inode *inode;
334 +
335 +               file = vma_pr_or_file(vma);
336 +               inode = file_inode(file);
337                 dev = inode->i_sb->s_dev;
338                 ino = inode->i_ino;
339                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
340 @@ -1862,7 +1865,7 @@ static int show_numa_map(struct seq_file *m, void *v)
341         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
342         struct vm_area_struct *vma = v;
343         struct numa_maps *md = &numa_priv->md;
344 -       struct file *file = vma->vm_file;
345 +       struct file *file = vma_pr_or_file(vma);
346         struct mm_struct *mm = vma->vm_mm;
347         struct mempolicy *pol;
348         char buffer[64];
349 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
350 index a6d21fc0033c6..02c2de31196e0 100644
351 --- a/fs/proc/task_nommu.c
352 +++ b/fs/proc/task_nommu.c
353 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
354         file = vma->vm_file;
355  
356         if (file) {
357 -               struct inode *inode = file_inode(vma->vm_file);
358 +               struct inode *inode;
359 +
360 +               file = vma_pr_or_file(vma);
361 +               inode = file_inode(file);
362                 dev = inode->i_sb->s_dev;
363                 ino = inode->i_ino;
364                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
365 diff --git a/include/linux/mm.h b/include/linux/mm.h
366 index ecdf8a8cd6aeb..d9b3ec02ecbef 100644
367 --- a/include/linux/mm.h
368 +++ b/include/linux/mm.h
369 @@ -1715,6 +1715,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
370         unmap_mapping_range(mapping, holebegin, holelen, 0);
371  }
372  
373 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
374 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
375 +                                     int);
376 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
377 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
378 +
379 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
380 +                                                               __LINE__)
381 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
382 +                                                         __LINE__)
383 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
384 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
385 +
386 +#ifndef CONFIG_MMU
387 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
388 +extern void vmr_do_fput(struct vm_region *, const char[], int);
389 +
390 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
391 +                                                         __LINE__)
392 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
393 +#endif /* !CONFIG_MMU */
394 +
395  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
396                 void *buf, int len, unsigned int gup_flags);
397  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
398 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
399 index 07d9acb5b19c4..2f395ab624f38 100644
400 --- a/include/linux/mm_types.h
401 +++ b/include/linux/mm_types.h
402 @@ -278,6 +278,7 @@ struct vm_region {
403         unsigned long   vm_top;         /* region allocated to here */
404         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
405         struct file     *vm_file;       /* the backing file or NULL */
406 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
407  
408         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
409         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
410 @@ -357,6 +358,7 @@ struct vm_area_struct {
411         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
412                                            units */
413         struct file * vm_file;          /* File we map to (can be NULL). */
414 +       struct file *vm_prfile;         /* shadow of vm_file */
415         void * vm_private_data;         /* was vm_pte (shared mem) */
416  
417  #ifdef CONFIG_SWAP
418 diff --git a/kernel/fork.c b/kernel/fork.c
419 index d66cd1014211b..a2addc21d63fc 100644
420 --- a/kernel/fork.c
421 +++ b/kernel/fork.c
422 @@ -555,7 +555,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
423                         struct inode *inode = file_inode(file);
424                         struct address_space *mapping = file->f_mapping;
425  
426 -                       get_file(file);
427 +                       vma_get_file(tmp);
428                         if (tmp->vm_flags & VM_DENYWRITE)
429                                 put_write_access(inode);
430                         i_mmap_lock_write(mapping);
431 diff --git a/mm/Makefile b/mm/Makefile
432 index b6cd2fffa4922..784e2cebe1eac 100644
433 --- a/mm/Makefile
434 +++ b/mm/Makefile
435 @@ -52,7 +52,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
436                            mm_init.o percpu.o slab_common.o \
437                            compaction.o vmacache.o \
438                            interval_tree.o list_lru.o workingset.o \
439 -                          debug.o gup.o mmap_lock.o $(mmu-y)
440 +                          prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
441  
442  # Give 'page_alloc' its own module-parameter namespace
443  page-alloc-y := page_alloc.o
444 diff --git a/mm/filemap.c b/mm/filemap.c
445 index aa0e0fb046700..d883a19f4237e 100644
446 --- a/mm/filemap.c
447 +++ b/mm/filemap.c
448 @@ -2993,7 +2993,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
449         vm_fault_t ret = VM_FAULT_LOCKED;
450  
451         sb_start_pagefault(mapping->host->i_sb);
452 -       file_update_time(vmf->vma->vm_file);
453 +       vma_file_update_time(vmf->vma);
454         lock_page(page);
455         if (page->mapping != mapping) {
456                 unlock_page(page);
457 diff --git a/mm/mmap.c b/mm/mmap.c
458 index dc7206032387c..09b10e20c097c 100644
459 --- a/mm/mmap.c
460 +++ b/mm/mmap.c
461 @@ -179,7 +179,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
462         if (vma->vm_ops && vma->vm_ops->close)
463                 vma->vm_ops->close(vma);
464         if (vma->vm_file)
465 -               fput(vma->vm_file);
466 +               vma_fput(vma);
467         mpol_put(vma_policy(vma));
468         vm_area_free(vma);
469         return next;
470 @@ -951,7 +951,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
471         if (remove_next) {
472                 if (file) {
473                         uprobe_munmap(next, next->vm_start, next->vm_end);
474 -                       fput(file);
475 +                       vma_fput(vma);
476                 }
477                 if (next->anon_vma)
478                         anon_vma_merge(vma, next);
479 @@ -1897,7 +1897,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
480         return addr;
481  
482  unmap_and_free_vma:
483 -       fput(vma->vm_file);
484 +       vma_fput(vma);
485         vma->vm_file = NULL;
486  
487         /* Undo any partial mapping done by a device driver. */
488 @@ -2757,7 +2757,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
489                 goto out_free_mpol;
490  
491         if (new->vm_file)
492 -               get_file(new->vm_file);
493 +               vma_get_file(new);
494  
495         if (new->vm_ops && new->vm_ops->open)
496                 new->vm_ops->open(new);
497 @@ -2776,7 +2776,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
498         if (new->vm_ops && new->vm_ops->close)
499                 new->vm_ops->close(new);
500         if (new->vm_file)
501 -               fput(new->vm_file);
502 +               vma_fput(new);
503         unlink_anon_vmas(new);
504   out_free_mpol:
505         mpol_put(vma_policy(new));
506 @@ -2969,7 +2969,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
507         struct vm_area_struct *vma;
508         unsigned long populate = 0;
509         unsigned long ret = -EINVAL;
510 -       struct file *file;
511 +       struct file *file, *prfile;
512  
513         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
514                      current->comm, current->pid);
515 @@ -3044,10 +3044,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
516                 }
517         }
518  
519 -       file = get_file(vma->vm_file);
520 +       vma_get_file(vma);
521 +       file = vma->vm_file;
522 +       prfile = vma->vm_prfile;
523         ret = do_mmap(vma->vm_file, start, size,
524                         prot, flags, pgoff, &populate, NULL);
525 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
526 +               struct vm_area_struct *new_vma;
527 +
528 +               new_vma = find_vma(mm, ret);
529 +               if (!new_vma->vm_prfile)
530 +                       new_vma->vm_prfile = prfile;
531 +               if (new_vma != vma)
532 +                       get_file(prfile);
533 +       }
534 +       /*
535 +        * two fput()s instead of vma_fput(vma),
536 +        * coz vma may not be available anymore.
537 +        */
538         fput(file);
539 +       if (prfile)
540 +               fput(prfile);
541  out:
542         mmap_write_unlock(mm);
543         if (populate)
544 @@ -3334,7 +3351,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
545                 if (anon_vma_clone(new_vma, vma))
546                         goto out_free_mempol;
547                 if (new_vma->vm_file)
548 -                       get_file(new_vma->vm_file);
549 +                       vma_get_file(new_vma);
550                 if (new_vma->vm_ops && new_vma->vm_ops->open)
551                         new_vma->vm_ops->open(new_vma);
552                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
553 diff --git a/mm/nommu.c b/mm/nommu.c
554 index 870fea12823e6..edbc99eee5b6b 100644
555 --- a/mm/nommu.c
556 +++ b/mm/nommu.c
557 @@ -533,7 +533,7 @@ static void __put_nommu_region(struct vm_region *region)
558                 up_write(&nommu_region_sem);
559  
560                 if (region->vm_file)
561 -                       fput(region->vm_file);
562 +                       vmr_fput(region);
563  
564                 /* IO memory and memory shared directly out of the pagecache
565                  * from ramfs/tmpfs mustn't be released here */
566 @@ -665,7 +665,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
567         if (vma->vm_ops && vma->vm_ops->close)
568                 vma->vm_ops->close(vma);
569         if (vma->vm_file)
570 -               fput(vma->vm_file);
571 +               vma_fput(vma);
572         put_nommu_region(vma->vm_region);
573         vm_area_free(vma);
574  }
575 @@ -1188,7 +1188,7 @@ unsigned long do_mmap(struct file *file,
576                                         goto error_just_free;
577                                 }
578                         }
579 -                       fput(region->vm_file);
580 +                       vmr_fput(region);
581                         kmem_cache_free(vm_region_jar, region);
582                         region = pregion;
583                         result = start;
584 @@ -1265,10 +1265,10 @@ unsigned long do_mmap(struct file *file,
585         up_write(&nommu_region_sem);
586  error:
587         if (region->vm_file)
588 -               fput(region->vm_file);
589 +               vmr_fput(region);
590         kmem_cache_free(vm_region_jar, region);
591         if (vma->vm_file)
592 -               fput(vma->vm_file);
593 +               vma_fput(vma);
594         vm_area_free(vma);
595         return ret;
596  
597 diff --git a/mm/prfile.c b/mm/prfile.c
598 new file mode 100644
599 index 0000000000000..00d51187c3250
600 --- /dev/null
601 +++ b/mm/prfile.c
602 @@ -0,0 +1,86 @@
603 +// SPDX-License-Identifier: GPL-2.0
604 +/*
605 + * Mainly for aufs which mmap(2) different file and wants to print different
606 + * path in /proc/PID/maps.
607 + * Call these functions via macros defined in linux/mm.h.
608 + *
609 + * See Documentation/filesystems/aufs/design/06mmap.txt
610 + *
611 + * Copyright (c) 2014-2020 Junjro R. Okajima
612 + * Copyright (c) 2014 Ian Campbell
613 + */
614 +
615 +#include <linux/mm.h>
616 +#include <linux/file.h>
617 +#include <linux/fs.h>
618 +
619 +/* #define PRFILE_TRACE */
620 +static inline void prfile_trace(struct file *f, struct file *pr,
621 +                             const char func[], int line, const char func2[])
622 +{
623 +#ifdef PRFILE_TRACE
624 +       if (pr)
625 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
626 +#endif
627 +}
628 +
629 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
630 +                            int line)
631 +{
632 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       file_update_time(f);
636 +       if (f && pr)
637 +               file_update_time(pr);
638 +}
639 +
640 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
641 +                              int line)
642 +{
643 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
644 +
645 +       prfile_trace(f, pr, func, line, __func__);
646 +       return (f && pr) ? pr : f;
647 +}
648 +
649 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
650 +{
651 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
652 +
653 +       prfile_trace(f, pr, func, line, __func__);
654 +       get_file(f);
655 +       if (f && pr)
656 +               get_file(pr);
657 +}
658 +
659 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
660 +{
661 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
662 +
663 +       prfile_trace(f, pr, func, line, __func__);
664 +       fput(f);
665 +       if (f && pr)
666 +               fput(pr);
667 +}
668 +
669 +#ifndef CONFIG_MMU
670 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
671 +                              int line)
672 +{
673 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
674 +
675 +       prfile_trace(f, pr, func, line, __func__);
676 +       return (f && pr) ? pr : f;
677 +}
678 +
679 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
680 +{
681 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
682 +
683 +       prfile_trace(f, pr, func, line, __func__);
684 +       fput(f);
685 +       if (f && pr)
686 +               fput(pr);
687 +}
688 +#endif /* !CONFIG_MMU */
689 SPDX-License-Identifier: GPL-2.0
690 aufs5.11 standalone patch
691
692 diff --git a/fs/dcache.c b/fs/dcache.c
693 index a7522ebbca659..d429c984133ca 100644
694 --- a/fs/dcache.c
695 +++ b/fs/dcache.c
696 @@ -1397,6 +1397,7 @@ void d_walk(struct dentry *parent, void *data,
697         seq = 1;
698         goto again;
699  }
700 +EXPORT_SYMBOL_GPL(d_walk);
701  
702  struct check_mount {
703         struct vfsmount *mnt;
704 @@ -2942,6 +2943,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
705  
706         write_sequnlock(&rename_lock);
707  }
708 +EXPORT_SYMBOL_GPL(d_exchange);
709  
710  /**
711   * d_ancestor - search for an ancestor
712 diff --git a/fs/exec.c b/fs/exec.c
713 index 5d4d52039105c..adc75877cdee7 100644
714 --- a/fs/exec.c
715 +++ b/fs/exec.c
716 @@ -114,6 +114,7 @@ bool path_noexec(const struct path *path)
717         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
718                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
719  }
720 +EXPORT_SYMBOL_GPL(path_noexec);
721  
722  #ifdef CONFIG_USELIB
723  /*
724 diff --git a/fs/fcntl.c b/fs/fcntl.c
725 index e747a47a97da4..d6211ff25c9a4 100644
726 --- a/fs/fcntl.c
727 +++ b/fs/fcntl.c
728 @@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
729   out:
730         return error;
731  }
732 +EXPORT_SYMBOL_GPL(setfl);
733  
734  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
735                       int force)
736 diff --git a/fs/file_table.c b/fs/file_table.c
737 index 45437f8e1003e..786af52904fcf 100644
738 --- a/fs/file_table.c
739 +++ b/fs/file_table.c
740 @@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
741         }
742         return ERR_PTR(-ENFILE);
743  }
744 +EXPORT_SYMBOL_GPL(alloc_empty_file);
745  
746  /*
747   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
748 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
749  }
750  
751  EXPORT_SYMBOL(fput);
752 +EXPORT_SYMBOL_GPL(__fput_sync);
753  
754  void __init files_init(void)
755  {
756 diff --git a/fs/inode.c b/fs/inode.c
757 index e4a25c3fa9e55..497326faa1247 100644
758 --- a/fs/inode.c
759 +++ b/fs/inode.c
760 @@ -1775,6 +1775,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
761                 return inode->i_op->update_time(inode, time, flags);
762         return generic_update_time(inode, time, flags);
763  }
764 +EXPORT_SYMBOL_GPL(update_time);
765  
766  /**
767   *     touch_atime     -       update the access time
768 diff --git a/fs/namespace.c b/fs/namespace.c
769 index 3e16fc64df8b8..eed3453ec40a8 100644
770 --- a/fs/namespace.c
771 +++ b/fs/namespace.c
772 @@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
773         mnt_dec_writers(real_mount(mnt));
774         preempt_enable();
775  }
776 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
777  
778  /**
779   * mnt_drop_write - give up write access to a mount
780 @@ -797,6 +798,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
781  {
782         return check_mnt(real_mount(mnt));
783  }
784 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
785  
786  /*
787   * vfsmount lock must be held for write
788 @@ -1967,6 +1969,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
789         }
790         return 0;
791  }
792 +EXPORT_SYMBOL_GPL(iterate_mounts);
793  
794  static void lock_mnt_tree(struct mount *mnt)
795  {
796 diff --git a/fs/notify/group.c b/fs/notify/group.c
797 index a4a4b1c64d32a..86dc2efb1850c 100644
798 --- a/fs/notify/group.c
799 +++ b/fs/notify/group.c
800 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
801  {
802         refcount_inc(&group->refcnt);
803  }
804 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
805  
806  /*
807   * Drop a reference to a group.  Free it if it's through.
808 diff --git a/fs/open.c b/fs/open.c
809 index 1e06e443a5651..c3bbb8aafcd1a 100644
810 --- a/fs/open.c
811 +++ b/fs/open.c
812 @@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
813         inode_unlock(dentry->d_inode);
814         return ret;
815  }
816 +EXPORT_SYMBOL_GPL(do_truncate);
817  
818  long vfs_truncate(const struct path *path, loff_t length)
819  {
820 diff --git a/fs/read_write.c b/fs/read_write.c
821 index 75f764b434184..7582bb3fb634b 100644
822 --- a/fs/read_write.c
823 +++ b/fs/read_write.c
824 @@ -503,6 +503,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
825         inc_syscr(current);
826         return ret;
827  }
828 +EXPORT_SYMBOL_GPL(vfs_read);
829  
830  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
831  {
832 @@ -613,6 +614,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
833         file_end_write(file);
834         return ret;
835  }
836 +EXPORT_SYMBOL_GPL(vfs_write);
837  
838  /* file_ppos returns &file->f_pos or NULL if file is stream */
839  static inline loff_t *file_ppos(struct file *file)
840 diff --git a/fs/splice.c b/fs/splice.c
841 index 55b5356262085..c13ac0fbac318 100644
842 --- a/fs/splice.c
843 +++ b/fs/splice.c
844 @@ -763,6 +763,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
845                 return warn_unsupported(out, "write");
846         return out->f_op->splice_write(pipe, out, ppos, len, flags);
847  }
848 +EXPORT_SYMBOL_GPL(do_splice_from);
849  
850  /*
851   * Attempt to initiate a splice from a file to a pipe.
852 @@ -787,6 +788,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
853                 return warn_unsupported(in, "read");
854         return in->f_op->splice_read(in, ppos, pipe, len, flags);
855  }
856 +EXPORT_SYMBOL_GPL(do_splice_to);
857  
858  /**
859   * splice_direct_to_actor - splices data directly between two non-pipes
860 diff --git a/fs/sync.c b/fs/sync.c
861 index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
862 --- a/fs/sync.c
863 +++ b/fs/sync.c
864 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
865                 sb->s_op->sync_fs(sb, wait);
866         return __sync_blockdev(sb->s_bdev, wait);
867  }
868 +EXPORT_SYMBOL_GPL(__sync_filesystem);
869  
870  /*
871   * Write out and wait upon all dirty data associated with this
872 diff --git a/fs/xattr.c b/fs/xattr.c
873 index fd57153b1f617..d8e9d0d6853ef 100644
874 --- a/fs/xattr.c
875 +++ b/fs/xattr.c
876 @@ -371,6 +371,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
877         *xattr_value = value;
878         return error;
879  }
880 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
881  
882  ssize_t
883  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
884 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
885 index 5c3bff75de111..ff00031462623 100644
886 --- a/kernel/locking/lockdep.c
887 +++ b/kernel/locking/lockdep.c
888 @@ -209,6 +209,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
889          */
890         return lock_classes + class_idx;
891  }
892 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
893  #define hlock_class(hlock) lockdep_hlock_class(hlock)
894  
895  #ifdef CONFIG_LOCK_STAT
896 diff --git a/kernel/task_work.c b/kernel/task_work.c
897 index 9cde961875c0a..ac8c79dc81813 100644
898 --- a/kernel/task_work.c
899 +++ b/kernel/task_work.c
900 @@ -143,3 +143,4 @@ void task_work_run(void)
901                 } while (work);
902         }
903  }
904 +EXPORT_SYMBOL_GPL(task_work_run);
905 diff --git a/security/security.c b/security/security.c
906 index 7b09cfbae94f7..3e060cc948262 100644
907 --- a/security/security.c
908 +++ b/security/security.c
909 @@ -1094,6 +1094,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
910                 return 0;
911         return call_int_hook(path_rmdir, 0, dir, dentry);
912  }
913 +EXPORT_SYMBOL_GPL(security_path_rmdir);
914  
915  int security_path_unlink(const struct path *dir, struct dentry *dentry)
916  {
917 @@ -1110,6 +1111,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
918                 return 0;
919         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
920  }
921 +EXPORT_SYMBOL_GPL(security_path_symlink);
922  
923  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
924                        struct dentry *new_dentry)
925 @@ -1118,6 +1120,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
926                 return 0;
927         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
928  }
929 +EXPORT_SYMBOL_GPL(security_path_link);
930  
931  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
932                          const struct path *new_dir, struct dentry *new_dentry,
933 @@ -1145,6 +1148,7 @@ int security_path_truncate(const struct path *path)
934                 return 0;
935         return call_int_hook(path_truncate, 0, path);
936  }
937 +EXPORT_SYMBOL_GPL(security_path_truncate);
938  
939  int security_path_chmod(const struct path *path, umode_t mode)
940  {
941 @@ -1152,6 +1156,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
942                 return 0;
943         return call_int_hook(path_chmod, 0, path, mode);
944  }
945 +EXPORT_SYMBOL_GPL(security_path_chmod);
946  
947  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
948  {
949 @@ -1159,6 +1164,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
950                 return 0;
951         return call_int_hook(path_chown, 0, path, uid, gid);
952  }
953 +EXPORT_SYMBOL_GPL(security_path_chown);
954  
955  int security_path_chroot(const struct path *path)
956  {
957 @@ -1259,6 +1265,7 @@ int security_inode_permission(struct inode *inode, int mask)
958                 return 0;
959         return call_int_hook(inode_permission, 0, inode, mask);
960  }
961 +EXPORT_SYMBOL_GPL(security_inode_permission);
962  
963  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
964  {
965 @@ -1451,6 +1458,7 @@ int security_file_permission(struct file *file, int mask)
966  
967         return fsnotify_perm(file, mask);
968  }
969 +EXPORT_SYMBOL_GPL(security_file_permission);
970  
971  int security_file_alloc(struct file *file)
972  {
973 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
974 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
975 +++ linux/Documentation/ABI/testing/debugfs-aufs        2021-02-24 13:33:42.737680181 +0100
976 @@ -0,0 +1,55 @@
977 +What:          /debug/aufs/si_<id>/
978 +Date:          March 2009
979 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
980 +Description:
981 +               Under /debug/aufs, a directory named si_<id> is created
982 +               per aufs mount, where <id> is a unique id generated
983 +               internally.
984 +
985 +What:          /debug/aufs/si_<id>/plink
986 +Date:          Apr 2013
987 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
988 +Description:
989 +               It has three lines and shows the information about the
990 +               pseudo-link. The first line is a single number
991 +               representing a number of buckets. The second line is a
992 +               number of pseudo-links per buckets (separated by a
993 +               blank). The last line is a single number representing a
994 +               total number of psedo-links.
995 +               When the aufs mount option 'noplink' is specified, it
996 +               will show "1\n0\n0\n".
997 +
998 +What:          /debug/aufs/si_<id>/xib
999 +Date:          March 2009
1000 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1001 +Description:
1002 +               It shows the consumed blocks by xib (External Inode Number
1003 +               Bitmap), its block size and file size.
1004 +               When the aufs mount option 'noxino' is specified, it
1005 +               will be empty. About XINO files, see the aufs manual.
1006 +
1007 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1008 +Date:          March 2009
1009 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1010 +Description:
1011 +               It shows the consumed blocks by xino (External Inode Number
1012 +               Translation Table), its link count, block size and file
1013 +               size.
1014 +               Due to the file size limit, there may exist multiple
1015 +               xino files per branch.  In this case, "-N" is added to
1016 +               the filename and it corresponds to the index of the
1017 +               internal xino array.  "-0" is omitted.
1018 +               When the aufs mount option 'noxino' is specified, Those
1019 +               entries won't exist.  About XINO files, see the aufs
1020 +               manual.
1021 +
1022 +What:          /debug/aufs/si_<id>/xigen
1023 +Date:          March 2009
1024 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1025 +Description:
1026 +               It shows the consumed blocks by xigen (External Inode
1027 +               Generation Table), its block size and file size.
1028 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1029 +               be created.
1030 +               When the aufs mount option 'noxino' is specified, it
1031 +               will be empty. About XINO files, see the aufs manual.
1032 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1033 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1034 +++ linux/Documentation/ABI/testing/sysfs-aufs  2021-02-24 13:33:42.737680181 +0100
1035 @@ -0,0 +1,31 @@
1036 +What:          /sys/fs/aufs/si_<id>/
1037 +Date:          March 2009
1038 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1039 +Description:
1040 +               Under /sys/fs/aufs, a directory named si_<id> is created
1041 +               per aufs mount, where <id> is a unique id generated
1042 +               internally.
1043 +
1044 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1045 +Date:          March 2009
1046 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1047 +Description:
1048 +               It shows the abolute path of a member directory (which
1049 +               is called branch) in aufs, and its permission.
1050 +
1051 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1052 +Date:          July 2013
1053 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1054 +Description:
1055 +               It shows the id of a member directory (which is called
1056 +               branch) in aufs.
1057 +
1058 +What:          /sys/fs/aufs/si_<id>/xi_path
1059 +Date:          March 2009
1060 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1061 +Description:
1062 +               It shows the abolute path of XINO (External Inode Number
1063 +               Bitmap, Translation Table and Generation Table) file
1064 +               even if it is the default path.
1065 +               When the aufs mount option 'noxino' is specified, it
1066 +               will be empty. About XINO files, see the aufs manual.
1067 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1068 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1069 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2021-02-24 13:33:42.737680181 +0100
1070 @@ -0,0 +1,171 @@
1071 +
1072 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1073 +# 
1074 +# This program is free software; you can redistribute it and/or modify
1075 +# it under the terms of the GNU General Public License as published by
1076 +# the Free Software Foundation; either version 2 of the License, or
1077 +# (at your option) any later version.
1078 +# 
1079 +# This program is distributed in the hope that it will be useful,
1080 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1081 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1082 +# GNU General Public License for more details.
1083 +# 
1084 +# You should have received a copy of the GNU General Public License
1085 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1086 +
1087 +Introduction
1088 +----------------------------------------
1089 +
1090 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1091 +1. abbrev. for "advanced multi-layered unification filesystem".
1092 +2. abbrev. for "another unionfs".
1093 +3. abbrev. for "auf das" in German which means "on the" in English.
1094 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1095 +   But "Filesystem aufs Filesystem" is hard to understand.
1096 +4. abbrev. for "African Urban Fashion Show".
1097 +
1098 +AUFS is a filesystem with features:
1099 +- multi layered stackable unification filesystem, the member directory
1100 +  is called as a branch.
1101 +- branch permission and attribute, 'readonly', 'real-readonly',
1102 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1103 +  combination.
1104 +- internal "file copy-on-write".
1105 +- logical deletion, whiteout.
1106 +- dynamic branch manipulation, adding, deleting and changing permission.
1107 +- allow bypassing aufs, user's direct branch access.
1108 +- external inode number translation table and bitmap which maintains the
1109 +  persistent aufs inode number.
1110 +- seekable directory, including NFS readdir.
1111 +- file mapping, mmap and sharing pages.
1112 +- pseudo-link, hardlink over branches.
1113 +- loopback mounted filesystem as a branch.
1114 +- several policies to select one among multiple writable branches.
1115 +- revert a single systemcall when an error occurs in aufs.
1116 +- and more...
1117 +
1118 +
1119 +Multi Layered Stackable Unification Filesystem
1120 +----------------------------------------------------------------------
1121 +Most people already knows what it is.
1122 +It is a filesystem which unifies several directories and provides a
1123 +merged single directory. When users access a file, the access will be
1124 +passed/re-directed/converted (sorry, I am not sure which English word is
1125 +correct) to the real file on the member filesystem. The member
1126 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1127 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1128 +readonly branch is handled by creating 'whiteout' on the upper writable
1129 +branch.
1130 +
1131 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1132 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1133 +different approaches to implement the merged-view.
1134 +The former tries putting it into VFS, and the latter implements as a
1135 +separate filesystem.
1136 +(If I misunderstand about these implementations, please let me know and
1137 +I shall correct it. Because it is a long time ago when I read their
1138 +source files last time).
1139 +
1140 +UnionMount's approach will be able to small, but may be hard to share
1141 +branches between several UnionMount since the whiteout in it is
1142 +implemented in the inode on branch filesystem and always
1143 +shared. According to Bharata's post, readdir does not seems to be
1144 +finished yet.
1145 +There are several missing features known in this implementations such as
1146 +- for users, the inode number may change silently. eg. copy-up.
1147 +- link(2) may break by copy-up.
1148 +- read(2) may get an obsoleted filedata (fstat(2) too).
1149 +- fcntl(F_SETLK) may be broken by copy-up.
1150 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1151 +  open(O_RDWR).
1152 +
1153 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1154 +merged into mainline. This is another implementation of UnionMount as a
1155 +separated filesystem. All the limitations and known problems which
1156 +UnionMount are equally inherited to "overlay" filesystem.
1157 +
1158 +Unionfs has a longer history. When I started implementing a stackable
1159 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1160 +inode, dentry and file objects and they have an array pointing lower
1161 +same kind objects. After contributing many patches for Unionfs, I
1162 +re-started my project AUFS (Jun 2006).
1163 +
1164 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1165 +implemented my own ideas, approaches and enhancements and it became
1166 +totally different one.
1167 +
1168 +Comparing DM snapshot and fs based implementation
1169 +- the number of bytes to be copied between devices is much smaller.
1170 +- the type of filesystem must be one and only.
1171 +- the fs must be writable, no readonly fs, even for the lower original
1172 +  device. so the compression fs will not be usable. but if we use
1173 +  loopback mount, we may address this issue.
1174 +  for instance,
1175 +       mount /cdrom/squashfs.img /sq
1176 +       losetup /sq/ext2.img
1177 +       losetup /somewhere/cow
1178 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1179 +- it will be difficult (or needs more operations) to extract the
1180 +  difference between the original device and COW.
1181 +- DM snapshot-merge may help a lot when users try merging. in the
1182 +  fs-layer union, users will use rsync(1).
1183 +
1184 +You may want to read my old paper "Filesystems in LiveCD"
1185 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1186 +
1187 +
1188 +Several characters/aspects/persona of aufs
1189 +----------------------------------------------------------------------
1190 +
1191 +Aufs has several characters, aspects or persona.
1192 +1. a filesystem, callee of VFS helper
1193 +2. sub-VFS, caller of VFS helper for branches
1194 +3. a virtual filesystem which maintains persistent inode number
1195 +4. reader/writer of files on branches such like an application
1196 +
1197 +1. Callee of VFS Helper
1198 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1199 +unlink(2) from an application reaches sys_unlink() kernel function and
1200 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1201 +calls filesystem specific unlink operation. Actually aufs implements the
1202 +unlink operation but it behaves like a redirector.
1203 +
1204 +2. Caller of VFS Helper for Branches
1205 +aufs_unlink() passes the unlink request to the branch filesystem as if
1206 +it were called from VFS. So the called unlink operation of the branch
1207 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1208 +every necessary pre/post operation for the branch filesystem.
1209 +- acquire the lock for the parent dir on a branch
1210 +- lookup in a branch
1211 +- revalidate dentry on a branch
1212 +- mnt_want_write() for a branch
1213 +- vfs_unlink() for a branch
1214 +- mnt_drop_write() for a branch
1215 +- release the lock on a branch
1216 +
1217 +3. Persistent Inode Number
1218 +One of the most important issue for a filesystem is to maintain inode
1219 +numbers. This is particularly important to support exporting a
1220 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1221 +backend block device for its own. But some storage is necessary to
1222 +keep and maintain the inode numbers. It may be a large space and may not
1223 +suit to keep in memory. Aufs rents some space from its first writable
1224 +branch filesystem (by default) and creates file(s) on it. These files
1225 +are created by aufs internally and removed soon (currently) keeping
1226 +opened.
1227 +Note: Because these files are removed, they are totally gone after
1228 +      unmounting aufs. It means the inode numbers are not persistent
1229 +      across unmount or reboot. I have a plan to make them really
1230 +      persistent which will be important for aufs on NFS server.
1231 +
1232 +4. Read/Write Files Internally (copy-on-write)
1233 +Because a branch can be readonly, when you write a file on it, aufs will
1234 +"copy-up" it to the upper writable branch internally. And then write the
1235 +originally requested thing to the file. Generally kernel doesn't
1236 +open/read/write file actively. In aufs, even a single write may cause a
1237 +internal "file copy". This behaviour is very similar to cp(1) command.
1238 +
1239 +Some people may think it is better to pass such work to user space
1240 +helper, instead of doing in kernel space. Actually I am still thinking
1241 +about it. But currently I have implemented it in kernel space.
1242 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1243 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1244 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2021-02-24 13:33:42.737680181 +0100
1245 @@ -0,0 +1,258 @@
1246 +
1247 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1248 +# 
1249 +# This program is free software; you can redistribute it and/or modify
1250 +# it under the terms of the GNU General Public License as published by
1251 +# the Free Software Foundation; either version 2 of the License, or
1252 +# (at your option) any later version.
1253 +# 
1254 +# This program is distributed in the hope that it will be useful,
1255 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1256 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1257 +# GNU General Public License for more details.
1258 +# 
1259 +# You should have received a copy of the GNU General Public License
1260 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1261 +
1262 +Basic Aufs Internal Structure
1263 +
1264 +Superblock/Inode/Dentry/File Objects
1265 +----------------------------------------------------------------------
1266 +As like an ordinary filesystem, aufs has its own
1267 +superblock/inode/dentry/file objects. All these objects have a
1268 +dynamically allocated array and store the same kind of pointers to the
1269 +lower filesystem, branch.
1270 +For example, when you build a union with one readwrite branch and one
1271 +readonly, mounted /au, /rw and /ro respectively.
1272 +- /au = /rw + /ro
1273 +- /ro/fileA exists but /rw/fileA
1274 +
1275 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1276 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1277 +- [0] = NULL (because /rw/fileA doesn't exist)
1278 +- [1] = /ro/fileA
1279 +
1280 +This style of an array is essentially same to the aufs
1281 +superblock/inode/dentry/file objects.
1282 +
1283 +Because aufs supports manipulating branches, ie. add/delete/change
1284 +branches dynamically, these objects has its own generation. When
1285 +branches are changed, the generation in aufs superblock is
1286 +incremented. And a generation in other object are compared when it is
1287 +accessed. When a generation in other objects are obsoleted, aufs
1288 +refreshes the internal array.
1289 +
1290 +
1291 +Superblock
1292 +----------------------------------------------------------------------
1293 +Additionally aufs superblock has some data for policies to select one
1294 +among multiple writable branches, XIB files, pseudo-links and kobject.
1295 +See below in detail.
1296 +About the policies which supports copy-down a directory, see
1297 +wbr_policy.txt too.
1298 +
1299 +
1300 +Branch and XINO(External Inode Number Translation Table)
1301 +----------------------------------------------------------------------
1302 +Every branch has its own xino (external inode number translation table)
1303 +file. The xino file is created and unlinked by aufs internally. When two
1304 +members of a union exist on the same filesystem, they share the single
1305 +xino file.
1306 +The struct of a xino file is simple, just a sequence of aufs inode
1307 +numbers which is indexed by the lower inode number.
1308 +In the above sample, assume the inode number of /ro/fileA is i111 and
1309 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1310 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1311 +
1312 +When the inode numbers are not contiguous, the xino file will be sparse
1313 +which has a hole in it and doesn't consume as much disk space as it
1314 +might appear. If your branch filesystem consumes disk space for such
1315 +holes, then you should specify 'xino=' option at mounting aufs.
1316 +
1317 +Aufs has a mount option to free the disk blocks for such holes in XINO
1318 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1319 +meet a problem of disk shortage due to XINO files, then you should try
1320 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1321 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1322 +the holes in XINO files.
1323 +
1324 +Also a writable branch has three kinds of "whiteout bases". All these
1325 +are existed when the branch is joined to aufs, and their names are
1326 +whiteout-ed doubly, so that users will never see their names in aufs
1327 +hierarchy.
1328 +1. a regular file which will be hardlinked to all whiteouts.
1329 +2. a directory to store a pseudo-link.
1330 +3. a directory to store an "orphan"-ed file temporary.
1331 +
1332 +1. Whiteout Base
1333 +   When you remove a file on a readonly branch, aufs handles it as a
1334 +   logical deletion and creates a whiteout on the upper writable branch
1335 +   as a hardlink of this file in order not to consume inode on the
1336 +   writable branch.
1337 +2. Pseudo-link Dir
1338 +   See below, Pseudo-link.
1339 +3. Step-Parent Dir
1340 +   When "fileC" exists on the lower readonly branch only and it is
1341 +   opened and removed with its parent dir, and then user writes
1342 +   something into it, then aufs copies-up fileC to this
1343 +   directory. Because there is no other dir to store fileC. After
1344 +   creating a file under this dir, the file is unlinked.
1345 +
1346 +Because aufs supports manipulating branches, ie. add/delete/change
1347 +dynamically, a branch has its own id. When the branch order changes,
1348 +aufs finds the new index by searching the branch id.
1349 +
1350 +
1351 +Pseudo-link
1352 +----------------------------------------------------------------------
1353 +Assume "fileA" exists on the lower readonly branch only and it is
1354 +hardlinked to "fileB" on the branch. When you write something to fileA,
1355 +aufs copies-up it to the upper writable branch. Additionally aufs
1356 +creates a hardlink under the Pseudo-link Directory of the writable
1357 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1358 +simple list. If fileB is read after unlinking fileA, aufs returns
1359 +filedata from the pseudo-link instead of the lower readonly
1360 +branch. Because the pseudo-link is based upon the inode, to keep the
1361 +inode number by xino (see above) is essentially necessary.
1362 +
1363 +All the hardlinks under the Pseudo-link Directory of the writable branch
1364 +should be restored in a proper location later. Aufs provides a utility
1365 +to do this. The userspace helpers executed at remounting and unmounting
1366 +aufs by default.
1367 +During this utility is running, it puts aufs into the pseudo-link
1368 +maintenance mode. In this mode, only the process which began the
1369 +maintenance mode (and its child processes) is allowed to operate in
1370 +aufs. Some other processes which are not related to the pseudo-link will
1371 +be allowed to run too, but the rest have to return an error or wait
1372 +until the maintenance mode ends. If a process already acquires an inode
1373 +mutex (in VFS), it has to return an error.
1374 +
1375 +
1376 +XIB(external inode number bitmap)
1377 +----------------------------------------------------------------------
1378 +Addition to the xino file per a branch, aufs has an external inode number
1379 +bitmap in a superblock object. It is also an internal file such like a
1380 +xino file.
1381 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1382 +not.
1383 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1384 +
1385 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1386 +reduce the number of consumed disk blocks for these files.
1387 +
1388 +
1389 +Virtual or Vertical Dir, and Readdir in Userspace
1390 +----------------------------------------------------------------------
1391 +In order to support multiple layers (branches), aufs readdir operation
1392 +constructs a virtual dir block on memory. For readdir, aufs calls
1393 +vfs_readdir() internally for each dir on branches, merges their entries
1394 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1395 +object. So the file object has its entry list until it is closed. The
1396 +entry list will be updated when the file position is zero and becomes
1397 +obsoleted. This decision is made in aufs automatically.
1398 +
1399 +The dynamically allocated memory block for the name of entries has a
1400 +unit of 512 bytes (by default) and stores the names contiguously (no
1401 +padding). Another block for each entry is handled by kmem_cache too.
1402 +During building dir blocks, aufs creates hash list and judging whether
1403 +the entry is whiteouted by its upper branch or already listed.
1404 +The merged result is cached in the corresponding inode object and
1405 +maintained by a customizable life-time option.
1406 +
1407 +Some people may call it can be a security hole or invite DoS attack
1408 +since the opened and once readdir-ed dir (file object) holds its entry
1409 +list and becomes a pressure for system memory. But I'd say it is similar
1410 +to files under /proc or /sys. The virtual files in them also holds a
1411 +memory page (generally) while they are opened. When an idea to reduce
1412 +memory for them is introduced, it will be applied to aufs too.
1413 +For those who really hate this situation, I've developed readdir(3)
1414 +library which operates this merging in userspace. You just need to set
1415 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1416 +kernel space for readdir(3).
1417 +
1418 +
1419 +Workqueue
1420 +----------------------------------------------------------------------
1421 +Aufs sometimes requires privilege access to a branch. For instance,
1422 +in copy-up/down operation. When a user process is going to make changes
1423 +to a file which exists in the lower readonly branch only, and the mode
1424 +of one of ancestor directories may not be writable by a user
1425 +process. Here aufs copy-up the file with its ancestors and they may
1426 +require privilege to set its owner/group/mode/etc.
1427 +This is a typical case of a application character of aufs (see
1428 +Introduction).
1429 +
1430 +Aufs uses workqueue synchronously for this case. It creates its own
1431 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1432 +passes the request to call mkdir or write (for example), and wait for
1433 +its completion. This approach solves a problem of a signal handler
1434 +simply.
1435 +If aufs didn't adopt the workqueue and changed the privilege of the
1436 +process, then the process may receive the unexpected SIGXFSZ or other
1437 +signals.
1438 +
1439 +Also aufs uses the system global workqueue ("events" kernel thread) too
1440 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1441 +whiteout base and etc. This is unrelated to a privilege.
1442 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1443 +superblock at the beginning, at the same time waits for the completion
1444 +of all queued asynchronous tasks.
1445 +
1446 +
1447 +Whiteout
1448 +----------------------------------------------------------------------
1449 +The whiteout in aufs is very similar to Unionfs's. That is represented
1450 +by its filename. UnionMount takes an approach of a file mode, but I am
1451 +afraid several utilities (find(1) or something) will have to support it.
1452 +
1453 +Basically the whiteout represents "logical deletion" which stops aufs to
1454 +lookup further, but also it represents "dir is opaque" which also stop
1455 +further lookup.
1456 +
1457 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1458 +In order to make several functions in a single systemcall to be
1459 +revertible, aufs adopts an approach to rename a directory to a temporary
1460 +unique whiteouted name.
1461 +For example, in rename(2) dir where the target dir already existed, aufs
1462 +renames the target dir to a temporary unique whiteouted name before the
1463 +actual rename on a branch, and then handles other actions (make it opaque,
1464 +update the attributes, etc). If an error happens in these actions, aufs
1465 +simply renames the whiteouted name back and returns an error. If all are
1466 +succeeded, aufs registers a function to remove the whiteouted unique
1467 +temporary name completely and asynchronously to the system global
1468 +workqueue.
1469 +
1470 +
1471 +Copy-up
1472 +----------------------------------------------------------------------
1473 +It is a well-known feature or concept.
1474 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1475 +internally and makes change to the new file on the upper writable branch.
1476 +When the trigger systemcall does not update the timestamps of the parent
1477 +dir, aufs reverts it after copy-up.
1478 +
1479 +
1480 +Move-down (aufs3.9 and later)
1481 +----------------------------------------------------------------------
1482 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1483 +the lower readonly branch to the upper writable branch when a user
1484 +changes something about the file.
1485 +"Move-down" is an opposite action of copy-up. Basically this action is
1486 +ran manually instead of automatically and internally.
1487 +For desgin and implementation, aufs has to consider these issues.
1488 +- whiteout for the file may exist on the lower branch.
1489 +- ancestor directories may not exist on the lower branch.
1490 +- diropq for the ancestor directories may exist on the upper branch.
1491 +- free space on the lower branch will reduce.
1492 +- another access to the file may happen during moving-down, including
1493 +  UDBA (see "Revalidate Dentry and UDBA").
1494 +- the file should not be hard-linked nor pseudo-linked. they should be
1495 +  handled by auplink utility later.
1496 +
1497 +Sometimes users want to move-down a file from the upper writable branch
1498 +to the lower readonly or writable branch. For instance,
1499 +- the free space of the upper writable branch is going to run out.
1500 +- create a new intermediate branch between the upper and lower branch.
1501 +- etc.
1502 +
1503 +For this purpose, use "aumvdown" command in aufs-util.git.
1504 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1505 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1506 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2021-02-24 13:33:42.737680181 +0100
1507 @@ -0,0 +1,85 @@
1508 +
1509 +# Copyright (C) 2015-2020 Junjiro R. Okajima
1510 +# 
1511 +# This program is free software; you can redistribute it and/or modify
1512 +# it under the terms of the GNU General Public License as published by
1513 +# the Free Software Foundation; either version 2 of the License, or
1514 +# (at your option) any later version.
1515 +# 
1516 +# This program is distributed in the hope that it will be useful,
1517 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1518 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1519 +# GNU General Public License for more details.
1520 +# 
1521 +# You should have received a copy of the GNU General Public License
1522 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1523 +
1524 +Support for a branch who has its ->atomic_open()
1525 +----------------------------------------------------------------------
1526 +The filesystems who implement its ->atomic_open() are not majority. For
1527 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1528 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1529 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1530 +sure whether all filesystems who have ->atomic_open() behave like this,
1531 +but NFSv4 surely returns the error.
1532 +
1533 +In order to support ->atomic_open() for aufs, there are a few
1534 +approaches.
1535 +
1536 +A. Introduce aufs_atomic_open()
1537 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1538 +     branch fs.
1539 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1540 +   an aufs user Pip Cet's approach
1541 +   - calls aufs_create(), VFS finish_open() and notify_change().
1542 +   - pass fake-mode to finish_open(), and then correct the mode by
1543 +     notify_change().
1544 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1545 +   - no aufs_atomic_open().
1546 +   - aufs_lookup() registers the TID to an aufs internal object.
1547 +   - aufs_create() does nothing when the matching TID is registered, but
1548 +     registers the mode.
1549 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1550 +     TID is registered.
1551 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1552 +   credential
1553 +   - no aufs_atomic_open().
1554 +   - aufs_create() registers the TID to an internal object. this info
1555 +     represents "this process created this file just now."
1556 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1557 +     registered TID and re-try open() with superuser's credential.
1558 +
1559 +Pros and cons for each approach.
1560 +
1561 +A.
1562 +   - straightforward but highly depends upon VFS internal.
1563 +   - the atomic behavaiour is kept.
1564 +   - some of parameters such as nameidata are hard to reproduce for
1565 +     branch fs.
1566 +   - large overhead.
1567 +B.
1568 +   - easy to implement.
1569 +   - the atomic behavaiour is lost.
1570 +C.
1571 +   - the atomic behavaiour is kept.
1572 +   - dirty and tricky.
1573 +   - VFS checks whether the file is created correctly after calling
1574 +     ->create(), which means this approach doesn't work.
1575 +D.
1576 +   - easy to implement.
1577 +   - the atomic behavaiour is lost.
1578 +   - to open a file with superuser's credential and give it to a user
1579 +     process is a bad idea, since the file object keeps the credential
1580 +     in it. It may affect LSM or something. This approach doesn't work
1581 +     either.
1582 +
1583 +The approach A is ideal, but it hard to implement. So here is a
1584 +variation of A, which is to be implemented.
1585 +
1586 +A-1. Introduce aufs_atomic_open()
1587 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1588 +       vfs_create() and finish_open().
1589 +     - the demerit is that the several checks after branch fs
1590 +       ->atomic_open() are lost. in the ordinary case, the checks are
1591 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1592 +       be implemented in aufs, but not all I am afraid.
1593 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1594 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1595 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2021-02-24 13:33:42.737680181 +0100
1596 @@ -0,0 +1,113 @@
1597 +
1598 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1599 +# 
1600 +# This program is free software; you can redistribute it and/or modify
1601 +# it under the terms of the GNU General Public License as published by
1602 +# the Free Software Foundation; either version 2 of the License, or
1603 +# (at your option) any later version.
1604 +# 
1605 +# This program is distributed in the hope that it will be useful,
1606 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1607 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1608 +# GNU General Public License for more details.
1609 +# 
1610 +# You should have received a copy of the GNU General Public License
1611 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1612 +
1613 +Lookup in a Branch
1614 +----------------------------------------------------------------------
1615 +Since aufs has a character of sub-VFS (see Introduction), it operates
1616 +lookup for branches as VFS does. It may be a heavy work. But almost all
1617 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1618 +directly connected to its parent. Digging down the directory hierarchy
1619 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1620 +aufs calls it.
1621 +
1622 +When a branch is a remote filesystem, aufs basically relies upon its
1623 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1624 +them.
1625 +For d_revalidate, aufs implements three levels of revalidate tests. See
1626 +"Revalidate Dentry and UDBA" in detail.
1627 +
1628 +
1629 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1630 +----------------------------------------------------------------------
1631 +Let's try case study.
1632 +- aufs has two branches, upper readwrite and lower readonly.
1633 +  /au = /rw + /ro
1634 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1635 +- user invoked "chmod a+rx /au/dirA"
1636 +- the internal copy-up is activated and "/rw/dirA" is created and its
1637 +  permission bits are set to world readable.
1638 +- then "/au/dirA" becomes world readable?
1639 +
1640 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1641 +or it may be a natively readonly filesystem. If aufs respects the lower
1642 +branch, it should not respond readdir request from other users. But user
1643 +allowed it by chmod. Should really aufs rejects showing the entries
1644 +under /ro/dirA?
1645 +
1646 +To be honest, I don't have a good solution for this case. So aufs
1647 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1648 +users.
1649 +When dirperm1 is specified, aufs checks only the highest one for the
1650 +directory permission, and shows the entries. Otherwise, as usual, checks
1651 +every dir existing on all branches and rejects the request.
1652 +
1653 +As a side effect, dirperm1 option improves the performance of aufs
1654 +because the number of permission check is reduced when the number of
1655 +branch is many.
1656 +
1657 +
1658 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1659 +----------------------------------------------------------------------
1660 +Generally VFS helpers re-validate a dentry as a part of lookup.
1661 +0. digging down the directory hierarchy.
1662 +1. lock the parent dir by its i_mutex.
1663 +2. lookup the final (child) entry.
1664 +3. revalidate it.
1665 +4. call the actual operation (create, unlink, etc.)
1666 +5. unlock the parent dir
1667 +
1668 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1669 +called. Actually aufs implements it and checks the dentry on a branch is
1670 +still valid.
1671 +But it is not enough. Because aufs has to release the lock for the
1672 +parent dir on a branch at the end of ->lookup() (step 2) and
1673 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1674 +held by VFS.
1675 +If the file on a branch is changed directly, eg. bypassing aufs, after
1676 +aufs released the lock, then the subsequent operation may cause
1677 +something unpleasant result.
1678 +
1679 +This situation is a result of VFS architecture, ->lookup() and
1680 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1681 +design from VFS's point of view. It is just not suitable for sub-VFS
1682 +character in aufs.
1683 +
1684 +Aufs supports such case by three level of revalidation which is
1685 +selectable by user.
1686 +1. Simple Revalidate
1687 +   Addition to the native flow in VFS's, confirm the child-parent
1688 +   relationship on the branch just after locking the parent dir on the
1689 +   branch in the "actual operation" (step 4). When this validation
1690 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1691 +   checks the validation of the dentry on branches.
1692 +2. Monitor Changes Internally by Inotify/Fsnotify
1693 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1694 +   the dentry on the branch, and returns EBUSY if it finds different
1695 +   dentry.
1696 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1697 +   during it is in cache. When the event is notified, aufs registers a
1698 +   function to kernel 'events' thread by schedule_work(). And the
1699 +   function sets some special status to the cached aufs dentry and inode
1700 +   private data. If they are not cached, then aufs has nothing to
1701 +   do. When the same file is accessed through aufs (step 0-3) later,
1702 +   aufs will detect the status and refresh all necessary data.
1703 +   In this mode, aufs has to ignore the event which is fired by aufs
1704 +   itself.
1705 +3. No Extra Validation
1706 +   This is the simplest test and doesn't add any additional revalidation
1707 +   test, and skip the revalidation in step 4. It is useful and improves
1708 +   aufs performance when system surely hide the aufs branches from user,
1709 +   by over-mounting something (or another method).
1710 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1711 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1712 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2021-02-24 13:33:42.737680181 +0100
1713 @@ -0,0 +1,74 @@
1714 +
1715 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1716 +# 
1717 +# This program is free software; you can redistribute it and/or modify
1718 +# it under the terms of the GNU General Public License as published by
1719 +# the Free Software Foundation; either version 2 of the License, or
1720 +# (at your option) any later version.
1721 +# 
1722 +# This program is distributed in the hope that it will be useful,
1723 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1724 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1725 +# GNU General Public License for more details.
1726 +# 
1727 +# You should have received a copy of the GNU General Public License
1728 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1729 +
1730 +Branch Manipulation
1731 +
1732 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1733 +and changing its permission/attribute, there are a lot of works to do.
1734 +
1735 +
1736 +Add a Branch
1737 +----------------------------------------------------------------------
1738 +o Confirm the adding dir exists outside of aufs, including loopback
1739 +  mount, and its various attributes.
1740 +o Initialize the xino file and whiteout bases if necessary.
1741 +  See struct.txt.
1742 +
1743 +o Check the owner/group/mode of the directory
1744 +  When the owner/group/mode of the adding directory differs from the
1745 +  existing branch, aufs issues a warning because it may impose a
1746 +  security risk.
1747 +  For example, when a upper writable branch has a world writable empty
1748 +  top directory, a malicious user can create any files on the writable
1749 +  branch directly, like copy-up and modify manually. If something like
1750 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1751 +  writable branch, and the writable branch is world-writable, then a
1752 +  malicious guy may create /etc/passwd on the writable branch directly
1753 +  and the infected file will be valid in aufs.
1754 +  I am afraid it can be a security issue, but aufs can do nothing except
1755 +  producing a warning.
1756 +
1757 +
1758 +Delete a Branch
1759 +----------------------------------------------------------------------
1760 +o Confirm the deleting branch is not busy
1761 +  To be general, there is one merit to adopt "remount" interface to
1762 +  manipulate branches. It is to discard caches. At deleting a branch,
1763 +  aufs checks the still cached (and connected) dentries and inodes. If
1764 +  there are any, then they are all in-use. An inode without its
1765 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1766 +
1767 +  For the cached one, aufs checks whether the same named entry exists on
1768 +  other branches.
1769 +  If the cached one is a directory, because aufs provides a merged view
1770 +  to users, as long as one dir is left on any branch aufs can show the
1771 +  dir to users. In this case, the branch can be removed from aufs.
1772 +  Otherwise aufs rejects deleting the branch.
1773 +
1774 +  If any file on the deleting branch is opened by aufs, then aufs
1775 +  rejects deleting.
1776 +
1777 +
1778 +Modify the Permission of a Branch
1779 +----------------------------------------------------------------------
1780 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1781 +  See struct.txt.
1782 +
1783 +o rw --> ro: Confirm the modifying branch is not busy
1784 +  Aufs rejects the request if any of these conditions are true.
1785 +  - a file on the branch is mmap-ed.
1786 +  - a regular file on the branch is opened for write and there is no
1787 +    same named entry on the upper branch.
1788 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1789 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1790 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2021-02-24 13:33:42.737680181 +0100
1791 @@ -0,0 +1,64 @@
1792 +
1793 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1794 +# 
1795 +# This program is free software; you can redistribute it and/or modify
1796 +# it under the terms of the GNU General Public License as published by
1797 +# the Free Software Foundation; either version 2 of the License, or
1798 +# (at your option) any later version.
1799 +# 
1800 +# This program is distributed in the hope that it will be useful,
1801 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1802 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1803 +# GNU General Public License for more details.
1804 +# 
1805 +# You should have received a copy of the GNU General Public License
1806 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1807 +
1808 +Policies to Select One among Multiple Writable Branches
1809 +----------------------------------------------------------------------
1810 +When the number of writable branch is more than one, aufs has to decide
1811 +the target branch for file creation or copy-up. By default, the highest
1812 +writable branch which has the parent (or ancestor) dir of the target
1813 +file is chosen (top-down-parent policy).
1814 +By user's request, aufs implements some other policies to select the
1815 +writable branch, for file creation several policies, round-robin,
1816 +most-free-space, and other policies. For copy-up, top-down-parent,
1817 +bottom-up-parent, bottom-up and others.
1818 +
1819 +As expected, the round-robin policy selects the branch in circular. When
1820 +you have two writable branches and creates 10 new files, 5 files will be
1821 +created for each branch. mkdir(2) systemcall is an exception. When you
1822 +create 10 new directories, all will be created on the same branch.
1823 +And the most-free-space policy selects the one which has most free
1824 +space among the writable branches. The amount of free space will be
1825 +checked by aufs internally, and users can specify its time interval.
1826 +
1827 +The policies for copy-up is more simple,
1828 +top-down-parent is equivalent to the same named on in create policy,
1829 +bottom-up-parent selects the writable branch where the parent dir
1830 +exists and the nearest upper one from the copyup-source,
1831 +bottom-up selects the nearest upper writable branch from the
1832 +copyup-source, regardless the existence of the parent dir.
1833 +
1834 +There are some rules or exceptions to apply these policies.
1835 +- If there is a readonly branch above the policy-selected branch and
1836 +  the parent dir is marked as opaque (a variation of whiteout), or the
1837 +  target (creating) file is whiteout-ed on the upper readonly branch,
1838 +  then the result of the policy is ignored and the target file will be
1839 +  created on the nearest upper writable branch than the readonly branch.
1840 +- If there is a writable branch above the policy-selected branch and
1841 +  the parent dir is marked as opaque or the target file is whiteouted
1842 +  on the branch, then the result of the policy is ignored and the target
1843 +  file will be created on the highest one among the upper writable
1844 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1845 +  it as usual.
1846 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1847 +  They try selecting the branch where the source exists as possible
1848 +  since copyup a large file will take long time. If it can't be,
1849 +  ie. the branch where the source exists is readonly, then they will
1850 +  follow the copyup policy.
1851 +- There is an exception for rename(2) when the target exists.
1852 +  If the rename target exists, aufs compares the index of the branches
1853 +  where the source and the target exists and selects the higher
1854 +  one. If the selected branch is readonly, then aufs follows the
1855 +  copyup policy.
1856 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1857 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1858 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2021-02-24 13:33:42.737680181 +0100
1859 @@ -0,0 +1,31 @@
1860 +
1861 +// to view this graph, run dot(1) command in GRAPHVIZ.
1862 +
1863 +digraph G {
1864 +node [shape=box];
1865 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1866 +
1867 +node [shape=oval];
1868 +
1869 +aufs_rename -> whinfo [label="store/remove"];
1870 +
1871 +node [shape=oval];
1872 +inode_list [label="h_inum list in branch\ncache"];
1873 +
1874 +node [shape=box];
1875 +whinode [label="h_inum list file"];
1876 +
1877 +node [shape=oval];
1878 +brmgmt [label="br_add/del/mod/umount"];
1879 +
1880 +brmgmt -> inode_list [label="create/remove"];
1881 +brmgmt -> whinode [label="load/store"];
1882 +
1883 +inode_list -> whinode [style=dashed,dir=both];
1884 +
1885 +aufs_rename -> inode_list [label="add/del"];
1886 +
1887 +aufs_lookup -> inode_list [label="search"];
1888 +
1889 +aufs_lookup -> whinfo [label="load/remove"];
1890 +}
1891 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1892 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1893 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2021-02-24 13:33:42.737680181 +0100
1894 @@ -0,0 +1,102 @@
1895 +
1896 +# Copyright (C) 2017-2020 Junjiro R. Okajima
1897 +#
1898 +# This program is free software; you can redistribute it and/or modify
1899 +# it under the terms of the GNU General Public License as published by
1900 +# the Free Software Foundation; either version 2 of the License, or
1901 +# (at your option) any later version.
1902 +#
1903 +# This program is distributed in the hope that it will be useful,
1904 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1905 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1906 +# GNU General Public License for more details.
1907 +#
1908 +# You should have received a copy of the GNU General Public License
1909 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1910 +
1911 +Special handling for renaming a directory (DIRREN)
1912 +----------------------------------------------------------------------
1913 +First, let's assume we have a simple usecase.
1914 +
1915 +- /u = /rw + /ro
1916 +- /rw/dirA exists
1917 +- /ro/dirA and /ro/dirA/file exist too
1918 +- there is no dirB on both branches
1919 +- a user issues rename("dirA", "dirB")
1920 +
1921 +Now, what should aufs behave against this rename(2)?
1922 +There are a few possible cases.
1923 +
1924 +A. returns EROFS.
1925 +   since dirA exists on a readonly branch which cannot be renamed.
1926 +B. returns EXDEV.
1927 +   it is possible to copy-up dirA (only the dir itself), but the child
1928 +   entries ("file" in this case) should not be. it must be a bad
1929 +   approach to copy-up recursively.
1930 +C. returns a success.
1931 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1932 +   is a violation of aufs' policy.
1933 +D. construct an extra information which indicates that /ro/dirA should
1934 +   be handled as the name of dirB.
1935 +   overlayfs has a similar feature called REDIRECT.
1936 +
1937 +Until now, aufs implements the case B only which returns EXDEV, and
1938 +expects the userspace application behaves like mv(1) which tries
1939 +issueing rename(2) recursively.
1940 +
1941 +A new aufs feature called DIRREN is introduced which implements the case
1942 +D. There are several "extra information" added.
1943 +
1944 +1. detailed info per renamed directory
1945 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1946 +2. the inode-number list of directories on a branch
1947 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1948 +
1949 +The filename of "detailed info per directory" represents the lower
1950 +branch, and its format is
1951 +- a type of the branch id
1952 +  one of these.
1953 +  + uuid (not implemented yet)
1954 +  + fsid
1955 +  + dev
1956 +- the inode-number of the branch root dir
1957 +
1958 +And it contains these info in a single regular file.
1959 +- magic number
1960 +- branch's inode-number of the logically renamed dir
1961 +- the name of the before-renamed dir
1962 +
1963 +The "detailed info per directory" file is created in aufs rename(2), and
1964 +loaded in any lookup.
1965 +The info is considered in lookup for the matching case only. Here
1966 +"matching" means that the root of branch (in the info filename) is same
1967 +to the current looking-up branch. After looking-up the before-renamed
1968 +name, the inode-number is compared. And the matched dentry is used.
1969 +
1970 +The "inode-number list of directories" is a regular file which contains
1971 +simply the inode-numbers on the branch. The file is created or updated
1972 +in removing the branch, and loaded in adding the branch. Its lifetime is
1973 +equal to the branch.
1974 +The list is refered in lookup, and when the current target inode is
1975 +found in the list, the aufs tries loading the "detailed info per
1976 +directory" and get the changed and valid name of the dir.
1977 +
1978 +Theoretically these "extra informaiton" may be able to be put into XATTR
1979 +in the dir inode. But aufs doesn't choose this way because
1980 +1. XATTR may not be supported by the branch (or its configuration)
1981 +2. XATTR may have its size limit.
1982 +3. XATTR may be less easy to convert than a regular file, when the
1983 +   format of the info is changed in the future.
1984 +At the same time, I agree that the regular file approach is much slower
1985 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1986 +better approach.
1987 +
1988 +This DIRREN feature is enabled by aufs configuration, and is activated
1989 +by a new mount option.
1990 +
1991 +For the more complicated case, there is a work with UDBA option, which
1992 +is to dected the direct access to the branches (by-passing aufs) and to
1993 +maintain the cashes in aufs. Since a single cached aufs dentry may
1994 +contains two names, before- and after-rename, the name comparision in
1995 +UDBA handler may not work correctly. In this case, the behaviour will be
1996 +equivalen to udba=reval case.
1997 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1998 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1999 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2021-02-24 13:33:42.737680181 +0100
2000 @@ -0,0 +1,120 @@
2001 +
2002 +# Copyright (C) 2011-2020 Junjiro R. Okajima
2003 +# 
2004 +# This program is free software; you can redistribute it and/or modify
2005 +# it under the terms of the GNU General Public License as published by
2006 +# the Free Software Foundation; either version 2 of the License, or
2007 +# (at your option) any later version.
2008 +# 
2009 +# This program is distributed in the hope that it will be useful,
2010 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2011 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2012 +# GNU General Public License for more details.
2013 +# 
2014 +# You should have received a copy of the GNU General Public License
2015 +# along with this program; if not, write to the Free Software
2016 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2017 +
2018 +
2019 +File-based Hierarchical Storage Management (FHSM)
2020 +----------------------------------------------------------------------
2021 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2022 +storage world. Aufs provides this feature as file-based with multiple
2023 +writable branches, based upon the principle of "Colder, the Lower".
2024 +Here the word "colder" means that the less used files, and "lower" means
2025 +that the position in the order of the stacked branches vertically.
2026 +These multiple writable branches are prioritized, ie. the topmost one
2027 +should be the fastest drive and be used heavily.
2028 +
2029 +o Characters in aufs FHSM story
2030 +- aufs itself and a new branch attribute.
2031 +- a new ioctl interface to move-down and to establish a connection with
2032 +  the daemon ("move-down" is a converse of "copy-up").
2033 +- userspace tool and daemon.
2034 +
2035 +The userspace daemon establishes a connection with aufs and waits for
2036 +the notification. The notified information is very similar to struct
2037 +statfs containing the number of consumed blocks and inodes.
2038 +When the consumed blocks/inodes of a branch exceeds the user-specified
2039 +upper watermark, the daemon activates its move-down process until the
2040 +consumed blocks/inodes reaches the user-specified lower watermark.
2041 +
2042 +The actual move-down is done by aufs based upon the request from
2043 +user-space since we need to maintain the inode number and the internal
2044 +pointer arrays in aufs.
2045 +
2046 +Currently aufs FHSM handles the regular files only. Additionally they
2047 +must not be hard-linked nor pseudo-linked.
2048 +
2049 +
2050 +o Cowork of aufs and the user-space daemon
2051 +  During the userspace daemon established the connection, aufs sends a
2052 +  small notification to it whenever aufs writes something into the
2053 +  writable branch. But it may cost high since aufs issues statfs(2)
2054 +  internally. So user can specify a new option to cache the
2055 +  info. Actually the notification is controlled by these factors.
2056 +  + the specified cache time.
2057 +  + classified as "force" by aufs internally.
2058 +  Until the specified time expires, aufs doesn't send the info
2059 +  except the forced cases. When aufs decide forcing, the info is always
2060 +  notified to userspace.
2061 +  For example, the number of free inodes is generally large enough and
2062 +  the shortage of it happens rarely. So aufs doesn't force the
2063 +  notification when creating a new file, directory and others. This is
2064 +  the typical case which aufs doesn't force.
2065 +  When aufs writes the actual filedata and the files consumes any of new
2066 +  blocks, the aufs forces notifying.
2067 +
2068 +
2069 +o Interfaces in aufs
2070 +- New branch attribute.
2071 +  + fhsm
2072 +    Specifies that the branch is managed by FHSM feature. In other word,
2073 +    participant in the FHSM.
2074 +    When nofhsm is set to the branch, it will not be the source/target
2075 +    branch of the move-down operation. This attribute is set
2076 +    independently from coo and moo attributes, and if you want full
2077 +    FHSM, you should specify them as well.
2078 +- New mount option.
2079 +  + fhsm_sec
2080 +    Specifies a second to suppress many less important info to be
2081 +    notified.
2082 +- New ioctl.
2083 +  + AUFS_CTL_FHSM_FD
2084 +    create a new file descriptor which userspace can read the notification
2085 +    (a subset of struct statfs) from aufs.
2086 +- Module parameter 'brs'
2087 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2088 +  be set.
2089 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2090 +  When there are two or more branches with fhsm attributes,
2091 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2092 +  terminates it. As a result of remounting and branch-manipulation, the
2093 +  number of branches with fhsm attribute can be one. In this case,
2094 +  /sbin/mount.aufs will terminate the user-space daemon.
2095 +
2096 +
2097 +Finally the operation is done as these steps in kernel-space.
2098 +- make sure that,
2099 +  + no one else is using the file.
2100 +  + the file is not hard-linked.
2101 +  + the file is not pseudo-linked.
2102 +  + the file is a regular file.
2103 +  + the parent dir is not opaqued.
2104 +- find the target writable branch.
2105 +- make sure the file is not whiteout-ed by the upper (than the target)
2106 +  branch.
2107 +- make the parent dir on the target branch.
2108 +- mutex lock the inode on the branch.
2109 +- unlink the whiteout on the target branch (if exists).
2110 +- lookup and create the whiteout-ed temporary name on the target branch.
2111 +- copy the file as the whiteout-ed temporary name on the target branch.
2112 +- rename the whiteout-ed temporary name to the original name.
2113 +- unlink the file on the source branch.
2114 +- maintain the internal pointer array and the external inode number
2115 +  table (XINO).
2116 +- maintain the timestamps and other attributes of the parent dir and the
2117 +  file.
2118 +
2119 +And of course, in every step, an error may happen. So the operation
2120 +should restore the original file state after an error happens.
2121 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2122 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2123 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2021-02-24 13:33:42.737680181 +0100
2124 @@ -0,0 +1,72 @@
2125 +
2126 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2127 +# 
2128 +# This program is free software; you can redistribute it and/or modify
2129 +# it under the terms of the GNU General Public License as published by
2130 +# the Free Software Foundation; either version 2 of the License, or
2131 +# (at your option) any later version.
2132 +# 
2133 +# This program is distributed in the hope that it will be useful,
2134 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2135 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2136 +# GNU General Public License for more details.
2137 +# 
2138 +# You should have received a copy of the GNU General Public License
2139 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2140 +
2141 +mmap(2) -- File Memory Mapping
2142 +----------------------------------------------------------------------
2143 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2144 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2145 +->mmap().
2146 +This approach is simple and good, but there is one problem.
2147 +Under /proc, several entries show the mmapped files by its path (with
2148 +device and inode number), and the printed path will be the path on the
2149 +branch fs's instead of virtual aufs's.
2150 +This is not a problem in most cases, but some utilities lsof(1) (and its
2151 +user) may expect the path on aufs.
2152 +
2153 +To address this issue, aufs adds a new member called vm_prfile in struct
2154 +vm_area_struct (and struct vm_region). The original vm_file points to
2155 +the file on the branch fs in order to handle everything correctly as
2156 +usual. The new vm_prfile points to a virtual file in aufs, and the
2157 +show-functions in procfs refers to vm_prfile if it is set.
2158 +Also we need to maintain several other places where touching vm_file
2159 +such like
2160 +- fork()/clone() copies vma and the reference count of vm_file is
2161 +  incremented.
2162 +- merging vma maintains the ref count too.
2163 +
2164 +This is not a good approach. It just fakes the printed path. But it
2165 +leaves all behaviour around f_mapping unchanged. This is surely an
2166 +advantage.
2167 +Actually aufs had adopted another complicated approach which calls
2168 +generic_file_mmap() and handles struct vm_operations_struct. In this
2169 +approach, aufs met a hard problem and I could not solve it without
2170 +switching the approach.
2171 +
2172 +There may be one more another approach which is
2173 +- bind-mount the branch-root onto the aufs-root internally
2174 +- grab the new vfsmount (ie. struct mount)
2175 +- lazy-umount the branch-root internally
2176 +- in open(2) the aufs-file, open the branch-file with the hidden
2177 +  vfsmount (instead of the original branch's vfsmount)
2178 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2179 +  but it may be possible from userspace by the mount helper.
2180 +
2181 +Adding the internal hidden vfsmount and using it in opening a file, the
2182 +file path under /proc will be printed correctly. This approach looks
2183 +smarter, but is not possible I am afraid.
2184 +- aufs-root may be bind-mount later. when it happens, another hidden
2185 +  vfsmount will be required.
2186 +- it is hard to get the chance to bind-mount and lazy-umount
2187 +  + in kernel-space, FS can have vfsmount in open(2) via
2188 +    file->f_path, and aufs can know its vfsmount. But several locks are
2189 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2190 +    here, then it may cause a deadlock.
2191 +  + in user-space, bind-mount doesn't invoke the mount helper.
2192 +- since /proc shows dev and ino, aufs has to give vma these info. it
2193 +  means a new member vm_prinode will be necessary. this is essentially
2194 +  equivalent to vm_prfile described above.
2195 +
2196 +I have to give up this "looks-smater" approach.
2197 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2198 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2199 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2021-02-24 13:33:42.737680181 +0100
2200 @@ -0,0 +1,96 @@
2201 +
2202 +# Copyright (C) 2014-2020 Junjiro R. Okajima
2203 +#
2204 +# This program is free software; you can redistribute it and/or modify
2205 +# it under the terms of the GNU General Public License as published by
2206 +# the Free Software Foundation; either version 2 of the License, or
2207 +# (at your option) any later version.
2208 +#
2209 +# This program is distributed in the hope that it will be useful,
2210 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2211 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2212 +# GNU General Public License for more details.
2213 +#
2214 +# You should have received a copy of the GNU General Public License
2215 +# along with this program; if not, write to the Free Software
2216 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2217 +
2218 +
2219 +Listing XATTR/EA and getting the value
2220 +----------------------------------------------------------------------
2221 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2222 +shows the values from the topmost existing file. This behaviour is good
2223 +for the non-dir entries since the bahaviour exactly matches the shown
2224 +information. But for the directories, aufs considers all the same named
2225 +entries on the lower branches. Which means, if one of the lower entry
2226 +rejects readdir call, then aufs returns an error even if the topmost
2227 +entry allows it. This behaviour is necessary to respect the branch fs's
2228 +security, but can make users confused since the user-visible standard
2229 +attributes don't match the behaviour.
2230 +To address this issue, aufs has a mount option called dirperm1 which
2231 +checks the permission for the topmost entry only, and ignores the lower
2232 +entry's permission.
2233 +
2234 +A similar issue can happen around XATTR.
2235 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2236 +always set. Otherwise these very unpleasant situation would happen.
2237 +- listxattr(2) may return the duplicated entries.
2238 +- users may not be able to remove or reset the XATTR forever,
2239 +
2240 +
2241 +XATTR/EA support in the internal (copy,move)-(up,down)
2242 +----------------------------------------------------------------------
2243 +Generally the extended attributes of inode are categorized as these.
2244 +- "security" for LSM and capability.
2245 +- "system" for posix ACL, 'acl' mount option is required for the branch
2246 +  fs generally.
2247 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2248 +- "user" for userspace, 'user_xattr' mount option is required for the
2249 +  branch fs generally.
2250 +
2251 +Moreover there are some other categories. Aufs handles these rather
2252 +unpopular categories as the ordinary ones, ie. there is no special
2253 +condition nor exception.
2254 +
2255 +In copy-up, the support for XATTR on the dst branch may differ from the
2256 +src branch. In this case, the copy-up operation will get an error and
2257 +the original user operation which triggered the copy-up will fail. It
2258 +can happen that even all copy-up will fail.
2259 +When both of src and dst branches support XATTR and if an error occurs
2260 +during copying XATTR, then the copy-up should fail obviously. That is a
2261 +good reason and aufs should return an error to userspace. But when only
2262 +the src branch support that XATTR, aufs should not return an error.
2263 +For example, the src branch supports ACL but the dst branch doesn't
2264 +because the dst branch may natively un-support it or temporary
2265 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2266 +may NOT return an error even if the XATTR is not supported. It is
2267 +totally up to the branch fs.
2268 +
2269 +Anyway when the aufs internal copy-up gets an error from the dst branch
2270 +fs, then aufs tries removing the just copied entry and returns the error
2271 +to the userspace. The worst case of this situation will be all copy-up
2272 +will fail.
2273 +
2274 +For the copy-up operation, there two basic approaches.
2275 +- copy the specified XATTR only (by category above), and return the
2276 +  error unconditionally if it happens.
2277 +- copy all XATTR, and ignore the error on the specified category only.
2278 +
2279 +In order to support XATTR and to implement the correct behaviour, aufs
2280 +chooses the latter approach and introduces some new branch attributes,
2281 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2282 +They correspond to the XATTR namespaces (see above). Additionally, to be
2283 +convenient, "icex" is also provided which means all "icex*" attributes
2284 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2285 +
2286 +The meaning of these attributes is to ignore the error from setting
2287 +XATTR on that branch.
2288 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2289 +error from the dst branch according to the specified attributes.
2290 +
2291 +Some XATTR may have its default value. The default value may come from
2292 +the parent dir or the environment. If the default value is set at the
2293 +file creating-time, it will be overwritten by copy-up.
2294 +Some contradiction may happen I am afraid.
2295 +Do we need another attribute to stop copying XATTR? I am unsure. For
2296 +now, aufs implements the branch attributes to ignore the error.
2297 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2298 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2299 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2021-02-24 13:33:42.737680181 +0100
2300 @@ -0,0 +1,58 @@
2301 +
2302 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2303 +# 
2304 +# This program is free software; you can redistribute it and/or modify
2305 +# it under the terms of the GNU General Public License as published by
2306 +# the Free Software Foundation; either version 2 of the License, or
2307 +# (at your option) any later version.
2308 +# 
2309 +# This program is distributed in the hope that it will be useful,
2310 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2311 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2312 +# GNU General Public License for more details.
2313 +# 
2314 +# You should have received a copy of the GNU General Public License
2315 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2316 +
2317 +Export Aufs via NFS
2318 +----------------------------------------------------------------------
2319 +Here is an approach.
2320 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2321 +  generation.
2322 +- iget_locked(): initialize aufs inode generation for a new inode, and
2323 +  store it in xigen file.
2324 +- destroy_inode(): increment aufs inode generation and store it in xigen
2325 +  file. it is necessary even if it is not unlinked, because any data of
2326 +  inode may be changed by UDBA.
2327 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2328 +  build file handle by
2329 +  + branch id (4 bytes)
2330 +  + superblock generation (4 bytes)
2331 +  + inode number (4 or 8 bytes)
2332 +  + parent dir inode number (4 or 8 bytes)
2333 +  + inode generation (4 bytes))
2334 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2335 +    bytes)
2336 +  + file handle for a branch (by exportfs_encode_fh())
2337 +- fh_to_dentry():
2338 +  + find the index of a branch from its id in handle, and check it is
2339 +    still exist in aufs.
2340 +  + 1st level: get the inode number from handle and search it in cache.
2341 +  + 2nd level: if not found in cache, get the parent inode number from
2342 +    the handle and search it in cache. and then open the found parent
2343 +    dir, find the matching inode number by vfs_readdir() and get its
2344 +    name, and call lookup_one_len() for the target dentry.
2345 +  + 3rd level: if the parent dir is not cached, call
2346 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2347 +    build a pathname of it, convert it a pathname in aufs, call
2348 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2349 +    the 2nd level.
2350 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2351 +    for every branch, but not itself. to get this, (currently) aufs
2352 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2353 +    idea, but I didn't get other approach.
2354 +  + test the generation of the gotten inode.
2355 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2356 +  convert it into ESTALE for NFSD.
2357 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2358 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2359 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2360 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2361 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2021-02-24 13:33:42.737680181 +0100
2362 @@ -0,0 +1,52 @@
2363 +
2364 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2365 +# 
2366 +# This program is free software; you can redistribute it and/or modify
2367 +# it under the terms of the GNU General Public License as published by
2368 +# the Free Software Foundation; either version 2 of the License, or
2369 +# (at your option) any later version.
2370 +# 
2371 +# This program is distributed in the hope that it will be useful,
2372 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2373 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2374 +# GNU General Public License for more details.
2375 +# 
2376 +# You should have received a copy of the GNU General Public License
2377 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2378 +
2379 +Show Whiteout Mode (shwh)
2380 +----------------------------------------------------------------------
2381 +Generally aufs hides the name of whiteouts. But in some cases, to show
2382 +them is very useful for users. For instance, creating a new middle layer
2383 +(branch) by merging existing layers.
2384 +
2385 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2386 +When you have three branches,
2387 +- Bottom: 'system', squashfs (underlying base system), read-only
2388 +- Middle: 'mods', squashfs, read-only
2389 +- Top: 'overlay', ram (tmpfs), read-write
2390 +
2391 +The top layer is loaded at boot time and saved at shutdown, to preserve
2392 +the changes made to the system during the session.
2393 +When larger changes have been made, or smaller changes have accumulated,
2394 +the size of the saved top layer data grows. At this point, it would be
2395 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2396 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2397 +restoring save and load speed.
2398 +
2399 +This merging is simplified by the use of another aufs mount, of just the
2400 +two overlay branches using the 'shwh' option.
2401 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2402 +       aufs /livesys/merge_union
2403 +
2404 +A merged view of these two branches is then available at
2405 +/livesys/merge_union, and the new feature is that the whiteouts are
2406 +visible!
2407 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2408 +writing to all branches. Also the default mode for all branches is 'ro'.
2409 +It is now possible to save the combined contents of the two overlay
2410 +branches to a new squashfs, e.g.:
2411 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2412 +
2413 +This new squashfs archive can be stored on the boot device and the
2414 +initramfs will use it to replace the old one at the next boot.
2415 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2416 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2417 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2021-02-24 13:33:42.737680181 +0100
2418 @@ -0,0 +1,47 @@
2419 +
2420 +# Copyright (C) 2010-2020 Junjiro R. Okajima
2421 +#
2422 +# This program is free software; you can redistribute it and/or modify
2423 +# it under the terms of the GNU General Public License as published by
2424 +# the Free Software Foundation; either version 2 of the License, or
2425 +# (at your option) any later version.
2426 +#
2427 +# This program is distributed in the hope that it will be useful,
2428 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2429 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2430 +# GNU General Public License for more details.
2431 +#
2432 +# You should have received a copy of the GNU General Public License
2433 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2434 +
2435 +Dynamically customizable FS operations
2436 +----------------------------------------------------------------------
2437 +Generally FS operations (struct inode_operations, struct
2438 +address_space_operations, struct file_operations, etc.) are defined as
2439 +"static const", but it never means that FS have only one set of
2440 +operation. Some FS have multiple sets of them. For instance, ext2 has
2441 +three sets, one for XIP, for NOBH, and for normal.
2442 +Since aufs overrides and redirects these operations, sometimes aufs has
2443 +to change its behaviour according to the branch FS type. More importantly
2444 +VFS acts differently if a function (member in the struct) is set or
2445 +not. It means aufs should have several sets of operations and select one
2446 +among them according to the branch FS definition.
2447 +
2448 +In order to solve this problem and not to affect the behaviour of VFS,
2449 +aufs defines these operations dynamically. For instance, aufs defines
2450 +dummy direct_IO function for struct address_space_operations, but it may
2451 +not be set to the address_space_operations actually. When the branch FS
2452 +doesn't have it, aufs doesn't set it to its address_space_operations
2453 +while the function definition itself is still alive. So the behaviour
2454 +itself will not change, and it will return an error when direct_IO is
2455 +not set.
2456 +
2457 +The lifetime of these dynamically generated operation object is
2458 +maintained by aufs branch object. When the branch is removed from aufs,
2459 +the reference counter of the object is decremented. When it reaches
2460 +zero, the dynamically generated operation object will be freed.
2461 +
2462 +This approach is designed to support AIO (io_submit), Direct I/O and
2463 +XIP (DAX) mainly.
2464 +Currently this approach is applied to address_space_operations for
2465 +regular files only.
2466 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2467 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2468 +++ linux/Documentation/filesystems/aufs/README 2021-02-24 13:33:42.737680181 +0100
2469 @@ -0,0 +1,401 @@
2470 +
2471 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2472 +http://aufs.sf.net
2473 +Junjiro R. Okajima
2474 +
2475 +
2476 +0. Introduction
2477 +----------------------------------------
2478 +In the early days, aufs was entirely re-designed and re-implemented
2479 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2480 +improvements and implementations, it became totally different from
2481 +Unionfs while keeping the basic features.
2482 +Later, Unionfs Version 2.x series began taking some of the same
2483 +approaches to aufs1's.
2484 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2485 +University and his team.
2486 +
2487 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2488 +support,
2489 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2490 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2491 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2492 +  or aufs1 from CVS on SourceForge.
2493 +
2494 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2495 +      According to Christoph Hellwig, linux rejects all union-type
2496 +      filesystems but UnionMount.
2497 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2498 +
2499 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2500 +    UnionMount, and he pointed out an issue around a directory mutex
2501 +    lock and aufs addressed it. But it is still unsure whether aufs will
2502 +    be merged (or any other union solution).
2503 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2504 +
2505 +
2506 +1. Features
2507 +----------------------------------------
2508 +- unite several directories into a single virtual filesystem. The member
2509 +  directory is called as a branch.
2510 +- you can specify the permission flags to the branch, which are 'readonly',
2511 +  'readwrite' and 'whiteout-able.'
2512 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2513 +  readonly branch are modifiable logically.
2514 +- dynamic branch manipulation, add, del.
2515 +- etc...
2516 +
2517 +Also there are many enhancements in aufs, such as:
2518 +- test only the highest one for the directory permission (dirperm1)
2519 +- copyup on open (coo=)
2520 +- 'move' policy for copy-up between two writable branches, after
2521 +  checking free space.
2522 +- xattr, acl
2523 +- readdir(3) in userspace.
2524 +- keep inode number by external inode number table
2525 +- keep the timestamps of file/dir in internal copyup operation
2526 +- seekable directory, supporting NFS readdir.
2527 +- whiteout is hardlinked in order to reduce the consumption of inodes
2528 +  on branch
2529 +- do not copyup, nor create a whiteout when it is unnecessary
2530 +- revert a single systemcall when an error occurs in aufs
2531 +- remount interface instead of ioctl
2532 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2533 +- loopback mounted filesystem as a branch
2534 +- kernel thread for removing the dir who has a plenty of whiteouts
2535 +- support copyup sparse file (a file which has a 'hole' in it)
2536 +- default permission flags for branches
2537 +- selectable permission flags for ro branch, whether whiteout can
2538 +  exist or not
2539 +- export via NFS.
2540 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2541 +- support multiple writable branches, some policies to select one
2542 +  among multiple writable branches.
2543 +- a new semantics for link(2) and rename(2) to support multiple
2544 +  writable branches.
2545 +- no glibc changes are required.
2546 +- pseudo hardlink (hardlink over branches)
2547 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2548 +  including NFS or remote filesystem branch.
2549 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2550 +- and more...
2551 +
2552 +Currently these features are dropped temporary from aufs5.
2553 +See design/08plan.txt in detail.
2554 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2555 +  (robr)
2556 +- statistics of aufs thread (/sys/fs/aufs/stat)
2557 +
2558 +Features or just an idea in the future (see also design/*.txt),
2559 +- reorder the branch index without del/re-add.
2560 +- permanent xino files for NFSD
2561 +- an option for refreshing the opened files after add/del branches
2562 +- light version, without branch manipulation. (unnecessary?)
2563 +- copyup in userspace
2564 +- inotify in userspace
2565 +- readv/writev
2566 +
2567 +
2568 +2. Download
2569 +----------------------------------------
2570 +There are three GIT trees for aufs5, aufs5-linux.git,
2571 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2572 +"aufs-util.git."
2573 +While the aufs-util is always necessary, you need either of aufs5-linux
2574 +or aufs5-standalone.
2575 +
2576 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2577 +git://git.kernel.org/.../torvalds/linux.git.
2578 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2579 +build aufs5 as an external kernel module.
2580 +Several extra patches are not included in this tree. Only
2581 +aufs5-standalone tree contains them. They are described in the later
2582 +section "Configuration and Compilation."
2583 +
2584 +On the other hand, the aufs5-standalone tree has only aufs source files
2585 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2586 +But you need to apply all aufs patches manually.
2587 +
2588 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2589 +represents the linux kernel version, "linux-5.x". For instance,
2590 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2591 +"aufs5.x-rcN" branch.
2592 +
2593 +o aufs5-linux tree
2594 +$ git clone --reference /your/linux/git/tree \
2595 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2596 +- if you don't have linux GIT tree, then remove "--reference ..."
2597 +$ cd aufs5-linux.git
2598 +$ git checkout origin/aufs5.0
2599 +
2600 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2601 +leave the patch-work to GIT.
2602 +$ cd /your/linux/git/tree
2603 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2604 +$ git fetch aufs5
2605 +$ git checkout -b my5.0 v5.0
2606 +$ (add your local change...)
2607 +$ git pull aufs5 aufs5.0
2608 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2609 +- you may need to solve some conflicts between your_changes and
2610 +  aufs5.0. in this case, git-rerere is recommended so that you can
2611 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2612 +  later in the future.
2613 +
2614 +o aufs5-standalone tree
2615 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2616 +$ cd aufs5-standalone.git
2617 +$ git checkout origin/aufs5.0
2618 +
2619 +o aufs-util tree
2620 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2621 +- note that the public aufs-util.git is on SourceForge instead of
2622 +  GitHUB.
2623 +$ cd aufs-util.git
2624 +$ git checkout origin/aufs5.0
2625 +
2626 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2627 +The minor version number, 'x' in '5.x', of aufs may not always
2628 +follow the minor version number of the kernel.
2629 +Because changes in the kernel that cause the use of a new
2630 +minor version number do not always require changes to aufs-util.
2631 +
2632 +Since aufs-util has its own minor version number, you may not be
2633 +able to find a GIT branch in aufs-util for your kernel's
2634 +exact minor version number.
2635 +In this case, you should git-checkout the branch for the
2636 +nearest lower number.
2637 +
2638 +For (an unreleased) example:
2639 +If you are using "linux-5.10" and the "aufs5.10" branch
2640 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2641 +or something numerically smaller is the branch for your kernel.
2642 +
2643 +Also you can view all branches by
2644 +       $ git branch -a
2645 +
2646 +
2647 +3. Configuration and Compilation
2648 +----------------------------------------
2649 +Make sure you have git-checkout'ed the correct branch.
2650 +
2651 +For aufs5-linux tree,
2652 +- enable CONFIG_AUFS_FS.
2653 +- set other aufs configurations if necessary.
2654 +
2655 +For aufs5-standalone tree,
2656 +There are several ways to build.
2657 +
2658 +1.
2659 +- apply ./aufs5-kbuild.patch to your kernel source files.
2660 +- apply ./aufs5-base.patch too.
2661 +- apply ./aufs5-mmap.patch too.
2662 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2663 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2664 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2665 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2666 +- enable CONFIG_AUFS_FS, you can select either
2667 +  =m or =y.
2668 +- and build your kernel as usual.
2669 +- install the built kernel.
2670 +- install the header files too by "make headers_install" to the
2671 +  directory where you specify. By default, it is $PWD/usr.
2672 +  "make help" shows a brief note for headers_install.
2673 +- and reboot your system.
2674 +
2675 +2.
2676 +- module only (CONFIG_AUFS_FS=m).
2677 +- apply ./aufs5-base.patch to your kernel source files.
2678 +- apply ./aufs5-mmap.patch too.
2679 +- apply ./aufs5-standalone.patch too.
2680 +- build your kernel, don't forget "make headers_install", and reboot.
2681 +- edit ./config.mk and set other aufs configurations if necessary.
2682 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2683 +  every aufs configurations.
2684 +- build the module by simple "make".
2685 +- you can specify ${KDIR} make variable which points to your kernel
2686 +  source tree.
2687 +- install the files
2688 +  + run "make install" to install the aufs module, or copy the built
2689 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2690 +  + run "make install_headers" (instead of headers_install) to install
2691 +    the modified aufs header file (you can specify DESTDIR which is
2692 +    available in aufs standalone version's Makefile only), or copy
2693 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2694 +    you like manually. By default, the target directory is $PWD/usr.
2695 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2696 +  kernel source tree.
2697 +
2698 +Note: The header file aufs_type.h is necessary to build aufs-util
2699 +      as well as "make headers_install" in the kernel source tree.
2700 +      headers_install is subject to be forgotten, but it is essentially
2701 +      necessary, not only for building aufs-util.
2702 +      You may not meet problems without headers_install in some older
2703 +      version though.
2704 +
2705 +And then,
2706 +- read README in aufs-util, build and install it
2707 +- note that your distribution may contain an obsoleted version of
2708 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2709 +  utilities, make sure that your compiler refers the correct aufs header
2710 +  file which is built by "make headers_install."
2711 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2712 +  then run "make install_ulib" too. And refer to the aufs manual in
2713 +  detail.
2714 +
2715 +There several other patches in aufs5-standalone.git. They are all
2716 +optional. When you meet some problems, they will help you.
2717 +- aufs5-loopback.patch
2718 +  Supports a nested loopback mount in a branch-fs. This patch is
2719 +  unnecessary until aufs produces a message like "you may want to try
2720 +  another patch for loopback file".
2721 +- proc_mounts.patch
2722 +  When there are many mountpoints and many mount(2)/umount(2) are
2723 +  running, then /proc/mounts may not show the all mountpoints.  This
2724 +  patch makes /proc/mounts always show the full mountpoints list.
2725 +  If you don't want to apply this patch and meet such problem, then you
2726 +  need to increase the value of 'ProcMounts_Times' make-variable in
2727 +  aufs-util.git as a second best solution.
2728 +- vfs-ino.patch
2729 +  Modifies a system global kernel internal function get_next_ino() in
2730 +  order to stop assigning 0 for an inode-number. Not directly related to
2731 +  aufs, but recommended generally.
2732 +- tmpfs-idr.patch
2733 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2734 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2735 +  duplication of inode number, which is important for backup tools and
2736 +  other utilities. When you find aufs XINO files for tmpfs branch
2737 +  growing too much, try this patch.
2738 +- lockdep-debug.patch
2739 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2740 +  also a caller of VFS functions for branch filesystems, subclassing of
2741 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2742 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2743 +  need to apply this debug patch to expand several constant values.
2744 +  If you don't know what LOCKDEP is, then you don't have apply this
2745 +  patch.
2746 +
2747 +
2748 +4. Usage
2749 +----------------------------------------
2750 +At first, make sure aufs-util are installed, and please read the aufs
2751 +manual, aufs.5 in aufs-util.git tree.
2752 +$ man -l aufs.5
2753 +
2754 +And then,
2755 +$ mkdir /tmp/rw /tmp/aufs
2756 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2757 +
2758 +Here is another example. The result is equivalent.
2759 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2760 +  Or
2761 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2762 +# mount -o remount,append:${HOME} /tmp/aufs
2763 +
2764 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2765 +you modify a file under /tmp/aufs, the one on your home directory is
2766 +not affected, instead the same named file will be newly created under
2767 +/tmp/rw. And all of your modification to a file will be applied to
2768 +the one under /tmp/rw. This is called the file based Copy on Write
2769 +(COW) method.
2770 +Aufs mount options are described in aufs.5.
2771 +If you run chroot or something and make your aufs as a root directory,
2772 +then you need to customize the shutdown script. See the aufs manual in
2773 +detail.
2774 +
2775 +Additionally, there are some sample usages of aufs which are a
2776 +diskless system with network booting, and LiveCD over NFS.
2777 +See sample dir in CVS tree on SourceForge.
2778 +
2779 +
2780 +5. Contact
2781 +----------------------------------------
2782 +When you have any problems or strange behaviour in aufs, please let me
2783 +know with:
2784 +- /proc/mounts (instead of the output of mount(8))
2785 +- /sys/module/aufs/*
2786 +- /sys/fs/aufs/* (if you have them)
2787 +- /debug/aufs/* (if you have them)
2788 +- linux kernel version
2789 +  if your kernel is not plain, for example modified by distributor,
2790 +  the url where i can download its source is necessary too.
2791 +- aufs version which was printed at loading the module or booting the
2792 +  system, instead of the date you downloaded.
2793 +- configuration (define/undefine CONFIG_AUFS_xxx)
2794 +- kernel configuration or /proc/config.gz (if you have it)
2795 +- LSM (linux security module, if you are using)
2796 +- behaviour which you think to be incorrect
2797 +- actual operation, reproducible one is better
2798 +- mailto: aufs-users at lists.sourceforge.net
2799 +
2800 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2801 +and Feature Requests) on SourceForge. Please join and write to
2802 +aufs-users ML.
2803 +
2804 +
2805 +6. Acknowledgements
2806 +----------------------------------------
2807 +Thanks to everyone who have tried and are using aufs, whoever
2808 +have reported a bug or any feedback.
2809 +
2810 +Especially donators:
2811 +Tomas Matejicek(slax.org) made a donation (much more than once).
2812 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2813 +       scripts) is making "doubling" donations.
2814 +       Unfortunately I cannot list all of the donators, but I really
2815 +       appreciate.
2816 +       It ends Aug 2010, but the ordinary donation URL is still available.
2817 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2818 +Dai Itasaka made a donation (2007/8).
2819 +Chuck Smith made a donation (2008/4, 10 and 12).
2820 +Henk Schoneveld made a donation (2008/9).
2821 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2822 +Francois Dupoux made a donation (2008/11).
2823 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2824 +       aufs2 GIT tree (2009/2).
2825 +William Grant made a donation (2009/3).
2826 +Patrick Lane made a donation (2009/4).
2827 +The Mail Archive (mail-archive.com) made donations (2009/5).
2828 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2829 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2830 +Pavel Pronskiy made a donation (2011/2).
2831 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2832 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2833 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2834 +11).
2835 +Sam Liddicott made a donation (2011/9).
2836 +Era Scarecrow made a donation (2013/4).
2837 +Bor Ratajc made a donation (2013/4).
2838 +Alessandro Gorreta made a donation (2013/4).
2839 +POIRETTE Marc made a donation (2013/4).
2840 +Alessandro Gorreta made a donation (2013/4).
2841 +lauri kasvandik made a donation (2013/5).
2842 +"pemasu from Finland" made a donation (2013/7).
2843 +The Parted Magic Project made a donation (2013/9 and 11).
2844 +Pavel Barta made a donation (2013/10).
2845 +Nikolay Pertsev made a donation (2014/5).
2846 +James B made a donation (2014/7 and 2015/7).
2847 +Stefano Di Biase made a donation (2014/8).
2848 +Daniel Epellei made a donation (2015/1).
2849 +OmegaPhil made a donation (2016/1, 2018/4).
2850 +Tomasz Szewczyk made a donation (2016/4).
2851 +James Burry made a donation (2016/12).
2852 +Carsten Rose made a donation (2018/9).
2853 +Porteus Kiosk made a donation (2018/10).
2854 +
2855 +Thank you very much.
2856 +Donations are always, including future donations, very important and
2857 +helpful for me to keep on developing aufs.
2858 +
2859 +
2860 +7.
2861 +----------------------------------------
2862 +If you are an experienced user, no explanation is needed. Aufs is
2863 +just a linux filesystem.
2864 +
2865 +
2866 +Enjoy!
2867 +
2868 +# Local variables: ;
2869 +# mode: text;
2870 +# End: ;
2871 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2872 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2873 +++ linux/fs/aufs/aufs.h        2021-02-24 13:33:42.741013619 +0100
2874 @@ -0,0 +1,62 @@
2875 +/* SPDX-License-Identifier: GPL-2.0 */
2876 +/*
2877 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2878 + *
2879 + * This program, aufs is free software; you can redistribute it and/or modify
2880 + * it under the terms of the GNU General Public License as published by
2881 + * the Free Software Foundation; either version 2 of the License, or
2882 + * (at your option) any later version.
2883 + *
2884 + * This program is distributed in the hope that it will be useful,
2885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2887 + * GNU General Public License for more details.
2888 + *
2889 + * You should have received a copy of the GNU General Public License
2890 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2891 + */
2892 +
2893 +/*
2894 + * all header files
2895 + */
2896 +
2897 +#ifndef __AUFS_H__
2898 +#define __AUFS_H__
2899 +
2900 +#ifdef __KERNEL__
2901 +
2902 +#define AuStub(type, name, body, ...) \
2903 +       static inline type name(__VA_ARGS__) { body; }
2904 +
2905 +#define AuStubVoid(name, ...) \
2906 +       AuStub(void, name, , __VA_ARGS__)
2907 +#define AuStubInt0(name, ...) \
2908 +       AuStub(int, name, return 0, __VA_ARGS__)
2909 +
2910 +#include "debug.h"
2911 +
2912 +#include "branch.h"
2913 +#include "cpup.h"
2914 +#include "dcsub.h"
2915 +#include "dbgaufs.h"
2916 +#include "dentry.h"
2917 +#include "dir.h"
2918 +#include "dirren.h"
2919 +#include "dynop.h"
2920 +#include "file.h"
2921 +#include "fstype.h"
2922 +#include "hbl.h"
2923 +#include "inode.h"
2924 +#include "lcnt.h"
2925 +#include "loop.h"
2926 +#include "module.h"
2927 +#include "opts.h"
2928 +#include "rwsem.h"
2929 +#include "super.h"
2930 +#include "sysaufs.h"
2931 +#include "vfsub.h"
2932 +#include "whout.h"
2933 +#include "wkq.h"
2934 +
2935 +#endif /* __KERNEL__ */
2936 +#endif /* __AUFS_H__ */
2937 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2938 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2939 +++ linux/fs/aufs/branch.c      2021-02-24 13:33:42.741013619 +0100
2940 @@ -0,0 +1,1427 @@
2941 +// SPDX-License-Identifier: GPL-2.0
2942 +/*
2943 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2944 + *
2945 + * This program, aufs is free software; you can redistribute it and/or modify
2946 + * it under the terms of the GNU General Public License as published by
2947 + * the Free Software Foundation; either version 2 of the License, or
2948 + * (at your option) any later version.
2949 + *
2950 + * This program is distributed in the hope that it will be useful,
2951 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2952 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2953 + * GNU General Public License for more details.
2954 + *
2955 + * You should have received a copy of the GNU General Public License
2956 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2957 + */
2958 +
2959 +/*
2960 + * branch management
2961 + */
2962 +
2963 +#include <linux/compat.h>
2964 +#include <linux/statfs.h>
2965 +#include "aufs.h"
2966 +
2967 +/*
2968 + * free a single branch
2969 + */
2970 +static void au_br_do_free(struct au_branch *br)
2971 +{
2972 +       int i;
2973 +       struct au_wbr *wbr;
2974 +       struct au_dykey **key;
2975 +
2976 +       au_hnotify_fin_br(br);
2977 +       /* always, regardless the mount option */
2978 +       au_dr_hino_free(&br->br_dirren);
2979 +       au_xino_put(br);
2980 +
2981 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2982 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2983 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2984 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2985 +
2986 +       wbr = br->br_wbr;
2987 +       if (wbr) {
2988 +               for (i = 0; i < AuBrWh_Last; i++)
2989 +                       dput(wbr->wbr_wh[i]);
2990 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2991 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2992 +       }
2993 +
2994 +       if (br->br_fhsm) {
2995 +               au_br_fhsm_fin(br->br_fhsm);
2996 +               au_kfree_try_rcu(br->br_fhsm);
2997 +       }
2998 +
2999 +       key = br->br_dykey;
3000 +       for (i = 0; i < AuBrDynOp; i++, key++)
3001 +               if (*key)
3002 +                       au_dy_put(*key);
3003 +               else
3004 +                       break;
3005 +
3006 +       /* recursive lock, s_umount of branch's */
3007 +       /* synchronize_rcu(); */ /* why? */
3008 +       lockdep_off();
3009 +       path_put(&br->br_path);
3010 +       lockdep_on();
3011 +       au_kfree_rcu(wbr);
3012 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3013 +       au_lcnt_wait_for_fin(&br->br_count);
3014 +       /* I don't know why, but percpu_refcount requires this */
3015 +       /* synchronize_rcu(); */
3016 +       au_kfree_rcu(br);
3017 +}
3018 +
3019 +/*
3020 + * frees all branches
3021 + */
3022 +void au_br_free(struct au_sbinfo *sbinfo)
3023 +{
3024 +       aufs_bindex_t bmax;
3025 +       struct au_branch **br;
3026 +
3027 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3028 +
3029 +       bmax = sbinfo->si_bbot + 1;
3030 +       br = sbinfo->si_branch;
3031 +       while (bmax--)
3032 +               au_br_do_free(*br++);
3033 +}
3034 +
3035 +/*
3036 + * find the index of a branch which is specified by @br_id.
3037 + */
3038 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3039 +{
3040 +       aufs_bindex_t bindex, bbot;
3041 +
3042 +       bbot = au_sbbot(sb);
3043 +       for (bindex = 0; bindex <= bbot; bindex++)
3044 +               if (au_sbr_id(sb, bindex) == br_id)
3045 +                       return bindex;
3046 +       return -1;
3047 +}
3048 +
3049 +/* ---------------------------------------------------------------------- */
3050 +
3051 +/*
3052 + * add a branch
3053 + */
3054 +
3055 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3056 +                       struct dentry *h_root)
3057 +{
3058 +       if (unlikely(h_adding == h_root
3059 +                    || au_test_loopback_overlap(sb, h_adding)))
3060 +               return 1;
3061 +       if (h_adding->d_sb != h_root->d_sb)
3062 +               return 0;
3063 +       return au_test_subdir(h_adding, h_root)
3064 +               || au_test_subdir(h_root, h_adding);
3065 +}
3066 +
3067 +/*
3068 + * returns a newly allocated branch. @new_nbranch is a number of branches
3069 + * after adding a branch.
3070 + */
3071 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3072 +                                    int perm)
3073 +{
3074 +       struct au_branch *add_branch;
3075 +       struct dentry *root;
3076 +       struct inode *inode;
3077 +       int err;
3078 +
3079 +       err = -ENOMEM;
3080 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3081 +       if (unlikely(!add_branch))
3082 +               goto out;
3083 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3084 +       if (unlikely(!add_branch->br_xino))
3085 +               goto out_br;
3086 +       err = au_hnotify_init_br(add_branch, perm);
3087 +       if (unlikely(err))
3088 +               goto out_xino;
3089 +
3090 +       if (au_br_writable(perm)) {
3091 +               /* may be freed separately at changing the branch permission */
3092 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3093 +                                            GFP_NOFS);
3094 +               if (unlikely(!add_branch->br_wbr))
3095 +                       goto out_hnotify;
3096 +       }
3097 +
3098 +       if (au_br_fhsm(perm)) {
3099 +               err = au_fhsm_br_alloc(add_branch);
3100 +               if (unlikely(err))
3101 +                       goto out_wbr;
3102 +       }
3103 +
3104 +       root = sb->s_root;
3105 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3106 +       if (!err)
3107 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3108 +       if (!err) {
3109 +               inode = d_inode(root);
3110 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3111 +                                       /*may_shrink*/0);
3112 +       }
3113 +       if (!err)
3114 +               return add_branch; /* success */
3115 +
3116 +out_wbr:
3117 +       au_kfree_rcu(add_branch->br_wbr);
3118 +out_hnotify:
3119 +       au_hnotify_fin_br(add_branch);
3120 +out_xino:
3121 +       au_xino_put(add_branch);
3122 +out_br:
3123 +       au_kfree_rcu(add_branch);
3124 +out:
3125 +       return ERR_PTR(err);
3126 +}
3127 +
3128 +/*
3129 + * test if the branch permission is legal or not.
3130 + */
3131 +static int test_br(struct inode *inode, int brperm, char *path)
3132 +{
3133 +       int err;
3134 +
3135 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3136 +       if (!err)
3137 +               goto out;
3138 +
3139 +       err = -EINVAL;
3140 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3141 +
3142 +out:
3143 +       return err;
3144 +}
3145 +
3146 +/*
3147 + * returns:
3148 + * 0: success, the caller will add it
3149 + * plus: success, it is already unified, the caller should ignore it
3150 + * minus: error
3151 + */
3152 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3153 +{
3154 +       int err;
3155 +       aufs_bindex_t bbot, bindex;
3156 +       struct dentry *root, *h_dentry;
3157 +       struct inode *inode, *h_inode;
3158 +
3159 +       root = sb->s_root;
3160 +       bbot = au_sbbot(sb);
3161 +       if (unlikely(bbot >= 0
3162 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3163 +               err = 1;
3164 +               if (!remount) {
3165 +                       err = -EINVAL;
3166 +                       pr_err("%s duplicated\n", add->pathname);
3167 +               }
3168 +               goto out;
3169 +       }
3170 +
3171 +       err = -ENOSPC; /* -E2BIG; */
3172 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3173 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3174 +               pr_err("number of branches exceeded %s\n", add->pathname);
3175 +               goto out;
3176 +       }
3177 +
3178 +       err = -EDOM;
3179 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3180 +               pr_err("bad index %d\n", add->bindex);
3181 +               goto out;
3182 +       }
3183 +
3184 +       inode = d_inode(add->path.dentry);
3185 +       err = -ENOENT;
3186 +       if (unlikely(!inode->i_nlink)) {
3187 +               pr_err("no existence %s\n", add->pathname);
3188 +               goto out;
3189 +       }
3190 +
3191 +       err = -EINVAL;
3192 +       if (unlikely(inode->i_sb == sb)) {
3193 +               pr_err("%s must be outside\n", add->pathname);
3194 +               goto out;
3195 +       }
3196 +
3197 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3198 +               pr_err("unsupported filesystem, %s (%s)\n",
3199 +                      add->pathname, au_sbtype(inode->i_sb));
3200 +               goto out;
3201 +       }
3202 +
3203 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3204 +               pr_err("already stacked, %s (%s)\n",
3205 +                      add->pathname, au_sbtype(inode->i_sb));
3206 +               goto out;
3207 +       }
3208 +
3209 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3210 +       if (unlikely(err))
3211 +               goto out;
3212 +
3213 +       if (bbot < 0)
3214 +               return 0; /* success */
3215 +
3216 +       err = -EINVAL;
3217 +       for (bindex = 0; bindex <= bbot; bindex++)
3218 +               if (unlikely(test_overlap(sb, add->path.dentry,
3219 +                                         au_h_dptr(root, bindex)))) {
3220 +                       pr_err("%s is overlapped\n", add->pathname);
3221 +                       goto out;
3222 +               }
3223 +
3224 +       err = 0;
3225 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3226 +               h_dentry = au_h_dptr(root, 0);
3227 +               h_inode = d_inode(h_dentry);
3228 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3229 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3230 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3231 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3232 +                               add->pathname,
3233 +                               i_uid_read(inode), i_gid_read(inode),
3234 +                               (inode->i_mode & S_IALLUGO),
3235 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3236 +                               (h_inode->i_mode & S_IALLUGO));
3237 +       }
3238 +
3239 +out:
3240 +       return err;
3241 +}
3242 +
3243 +/*
3244 + * initialize or clean the whiteouts for an adding branch
3245 + */
3246 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3247 +                        int new_perm)
3248 +{
3249 +       int err, old_perm;
3250 +       aufs_bindex_t bindex;
3251 +       struct inode *h_inode;
3252 +       struct au_wbr *wbr;
3253 +       struct au_hinode *hdir;
3254 +       struct dentry *h_dentry;
3255 +
3256 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3257 +       if (unlikely(err))
3258 +               goto out;
3259 +
3260 +       wbr = br->br_wbr;
3261 +       old_perm = br->br_perm;
3262 +       br->br_perm = new_perm;
3263 +       hdir = NULL;
3264 +       h_inode = NULL;
3265 +       bindex = au_br_index(sb, br->br_id);
3266 +       if (0 <= bindex) {
3267 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3268 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3269 +       } else {
3270 +               h_dentry = au_br_dentry(br);
3271 +               h_inode = d_inode(h_dentry);
3272 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3273 +       }
3274 +       if (!wbr)
3275 +               err = au_wh_init(br, sb);
3276 +       else {
3277 +               wbr_wh_write_lock(wbr);
3278 +               err = au_wh_init(br, sb);
3279 +               wbr_wh_write_unlock(wbr);
3280 +       }
3281 +       if (hdir)
3282 +               au_hn_inode_unlock(hdir);
3283 +       else
3284 +               inode_unlock(h_inode);
3285 +       vfsub_mnt_drop_write(au_br_mnt(br));
3286 +       br->br_perm = old_perm;
3287 +
3288 +       if (!err && wbr && !au_br_writable(new_perm)) {
3289 +               au_kfree_rcu(wbr);
3290 +               br->br_wbr = NULL;
3291 +       }
3292 +
3293 +out:
3294 +       return err;
3295 +}
3296 +
3297 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3298 +                      int perm)
3299 +{
3300 +       int err;
3301 +       struct kstatfs kst;
3302 +       struct au_wbr *wbr;
3303 +
3304 +       wbr = br->br_wbr;
3305 +       au_rw_init(&wbr->wbr_wh_rwsem);
3306 +       atomic_set(&wbr->wbr_wh_running, 0);
3307 +
3308 +       /*
3309 +        * a limit for rmdir/rename a dir
3310 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3311 +        */
3312 +       err = vfs_statfs(&br->br_path, &kst);
3313 +       if (unlikely(err))
3314 +               goto out;
3315 +       err = -EINVAL;
3316 +       if (kst.f_namelen >= NAME_MAX)
3317 +               err = au_br_init_wh(sb, br, perm);
3318 +       else
3319 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3320 +                      au_br_dentry(br),
3321 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3322 +
3323 +out:
3324 +       return err;
3325 +}
3326 +
3327 +/* initialize a new branch */
3328 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3329 +                     struct au_opt_add *add)
3330 +{
3331 +       int err;
3332 +       struct au_branch *brbase;
3333 +       struct file *xf;
3334 +       struct inode *h_inode;
3335 +
3336 +       err = 0;
3337 +       br->br_perm = add->perm;
3338 +       br->br_path = add->path; /* set first, path_get() later */
3339 +       spin_lock_init(&br->br_dykey_lock);
3340 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3341 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3342 +       br->br_id = au_new_br_id(sb);
3343 +       AuDebugOn(br->br_id < 0);
3344 +
3345 +       /* always, regardless the given option */
3346 +       err = au_dr_br_init(sb, br, &add->path);
3347 +       if (unlikely(err))
3348 +               goto out_err;
3349 +
3350 +       if (au_br_writable(add->perm)) {
3351 +               err = au_wbr_init(br, sb, add->perm);
3352 +               if (unlikely(err))
3353 +                       goto out_err;
3354 +       }
3355 +
3356 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3357 +               brbase = au_sbr(sb, 0);
3358 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3359 +               AuDebugOn(!xf);
3360 +               h_inode = d_inode(add->path.dentry);
3361 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3362 +               if (unlikely(err)) {
3363 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3364 +                       goto out_err;
3365 +               }
3366 +       }
3367 +
3368 +       sysaufs_br_init(br);
3369 +       path_get(&br->br_path);
3370 +       goto out; /* success */
3371 +
3372 +out_err:
3373 +       memset(&br->br_path, 0, sizeof(br->br_path));
3374 +out:
3375 +       return err;
3376 +}
3377 +
3378 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3379 +                            struct au_branch *br, aufs_bindex_t bbot,
3380 +                            aufs_bindex_t amount)
3381 +{
3382 +       struct au_branch **brp;
3383 +
3384 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3385 +
3386 +       brp = sbinfo->si_branch + bindex;
3387 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3388 +       *brp = br;
3389 +       sbinfo->si_bbot++;
3390 +       if (unlikely(bbot < 0))
3391 +               sbinfo->si_bbot = 0;
3392 +}
3393 +
3394 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3395 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3396 +{
3397 +       struct au_hdentry *hdp;
3398 +
3399 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3400 +
3401 +       hdp = au_hdentry(dinfo, bindex);
3402 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3403 +       au_h_dentry_init(hdp);
3404 +       dinfo->di_bbot++;
3405 +       if (unlikely(bbot < 0))
3406 +               dinfo->di_btop = 0;
3407 +}
3408 +
3409 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3410 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3411 +{
3412 +       struct au_hinode *hip;
3413 +
3414 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3415 +
3416 +       hip = au_hinode(iinfo, bindex);
3417 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3418 +       au_hinode_init(hip);
3419 +       iinfo->ii_bbot++;
3420 +       if (unlikely(bbot < 0))
3421 +               iinfo->ii_btop = 0;
3422 +}
3423 +
3424 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3425 +                        aufs_bindex_t bindex)
3426 +{
3427 +       struct dentry *root, *h_dentry;
3428 +       struct inode *root_inode, *h_inode;
3429 +       aufs_bindex_t bbot, amount;
3430 +
3431 +       root = sb->s_root;
3432 +       root_inode = d_inode(root);
3433 +       bbot = au_sbbot(sb);
3434 +       amount = bbot + 1 - bindex;
3435 +       h_dentry = au_br_dentry(br);
3436 +       au_sbilist_lock();
3437 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3438 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3439 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3440 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3441 +       h_inode = d_inode(h_dentry);
3442 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3443 +       au_sbilist_unlock();
3444 +}
3445 +
3446 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3447 +{
3448 +       int err;
3449 +       aufs_bindex_t bbot, add_bindex;
3450 +       struct dentry *root, *h_dentry;
3451 +       struct inode *root_inode;
3452 +       struct au_branch *add_branch;
3453 +
3454 +       root = sb->s_root;
3455 +       root_inode = d_inode(root);
3456 +       IMustLock(root_inode);
3457 +       IiMustWriteLock(root_inode);
3458 +       err = test_add(sb, add, remount);
3459 +       if (unlikely(err < 0))
3460 +               goto out;
3461 +       if (err) {
3462 +               err = 0;
3463 +               goto out; /* success */
3464 +       }
3465 +
3466 +       bbot = au_sbbot(sb);
3467 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3468 +       err = PTR_ERR(add_branch);
3469 +       if (IS_ERR(add_branch))
3470 +               goto out;
3471 +
3472 +       err = au_br_init(add_branch, sb, add);
3473 +       if (unlikely(err)) {
3474 +               au_br_do_free(add_branch);
3475 +               goto out;
3476 +       }
3477 +
3478 +       add_bindex = add->bindex;
3479 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3480 +       au_br_do_add(sb, add_branch, add_bindex);
3481 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3482 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3483 +
3484 +       h_dentry = add->path.dentry;
3485 +       if (!add_bindex) {
3486 +               au_cpup_attr_all(root_inode, /*force*/1);
3487 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3488 +       } else
3489 +               au_add_nlink(root_inode, d_inode(h_dentry));
3490 +
3491 +out:
3492 +       return err;
3493 +}
3494 +
3495 +/* ---------------------------------------------------------------------- */
3496 +
3497 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3498 +                                      unsigned long long max __maybe_unused,
3499 +                                      void *arg)
3500 +{
3501 +       unsigned long long n;
3502 +       struct file **p, *f;
3503 +       struct hlist_bl_head *files;
3504 +       struct hlist_bl_node *pos;
3505 +       struct au_finfo *finfo;
3506 +
3507 +       n = 0;
3508 +       p = a;
3509 +       files = &au_sbi(sb)->si_files;
3510 +       hlist_bl_lock(files);
3511 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3512 +               f = finfo->fi_file;
3513 +               if (file_count(f)
3514 +                   && !special_file(file_inode(f)->i_mode)) {
3515 +                       get_file(f);
3516 +                       *p++ = f;
3517 +                       n++;
3518 +                       AuDebugOn(n > max);
3519 +               }
3520 +       }
3521 +       hlist_bl_unlock(files);
3522 +
3523 +       return n;
3524 +}
3525 +
3526 +static struct file **au_farray_alloc(struct super_block *sb,
3527 +                                    unsigned long long *max)
3528 +{
3529 +       struct au_sbinfo *sbi;
3530 +
3531 +       sbi = au_sbi(sb);
3532 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3533 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3534 +}
3535 +
3536 +static void au_farray_free(struct file **a, unsigned long long max)
3537 +{
3538 +       unsigned long long ull;
3539 +
3540 +       for (ull = 0; ull < max; ull++)
3541 +               if (a[ull])
3542 +                       fput(a[ull]);
3543 +       kvfree(a);
3544 +}
3545 +
3546 +/* ---------------------------------------------------------------------- */
3547 +
3548 +/*
3549 + * delete a branch
3550 + */
3551 +
3552 +/* to show the line number, do not make it inlined function */
3553 +#define AuVerbose(do_info, fmt, ...) do { \
3554 +       if (do_info) \
3555 +               pr_info(fmt, ##__VA_ARGS__); \
3556 +} while (0)
3557 +
3558 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3559 +                        aufs_bindex_t bbot)
3560 +{
3561 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3562 +}
3563 +
3564 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3565 +                        aufs_bindex_t bbot)
3566 +{
3567 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3568 +}
3569 +
3570 +/*
3571 + * test if the branch is deletable or not.
3572 + */
3573 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3574 +                           unsigned int sigen, const unsigned int verbose)
3575 +{
3576 +       int err, i, j, ndentry;
3577 +       aufs_bindex_t btop, bbot;
3578 +       struct au_dcsub_pages dpages;
3579 +       struct au_dpage *dpage;
3580 +       struct dentry *d;
3581 +
3582 +       err = au_dpages_init(&dpages, GFP_NOFS);
3583 +       if (unlikely(err))
3584 +               goto out;
3585 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3586 +       if (unlikely(err))
3587 +               goto out_dpages;
3588 +
3589 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3590 +               dpage = dpages.dpages + i;
3591 +               ndentry = dpage->ndentry;
3592 +               for (j = 0; !err && j < ndentry; j++) {
3593 +                       d = dpage->dentries[j];
3594 +                       AuDebugOn(au_dcount(d) <= 0);
3595 +                       if (!au_digen_test(d, sigen)) {
3596 +                               di_read_lock_child(d, AuLock_IR);
3597 +                               if (unlikely(au_dbrange_test(d))) {
3598 +                                       di_read_unlock(d, AuLock_IR);
3599 +                                       continue;
3600 +                               }
3601 +                       } else {
3602 +                               di_write_lock_child(d);
3603 +                               if (unlikely(au_dbrange_test(d))) {
3604 +                                       di_write_unlock(d);
3605 +                                       continue;
3606 +                               }
3607 +                               err = au_reval_dpath(d, sigen);
3608 +                               if (!err)
3609 +                                       di_downgrade_lock(d, AuLock_IR);
3610 +                               else {
3611 +                                       di_write_unlock(d);
3612 +                                       break;
3613 +                               }
3614 +                       }
3615 +
3616 +                       /* AuDbgDentry(d); */
3617 +                       btop = au_dbtop(d);
3618 +                       bbot = au_dbbot(d);
3619 +                       if (btop <= bindex
3620 +                           && bindex <= bbot
3621 +                           && au_h_dptr(d, bindex)
3622 +                           && au_test_dbusy(d, btop, bbot)) {
3623 +                               err = -EBUSY;
3624 +                               AuVerbose(verbose, "busy %pd\n", d);
3625 +                               AuDbgDentry(d);
3626 +                       }
3627 +                       di_read_unlock(d, AuLock_IR);
3628 +               }
3629 +       }
3630 +
3631 +out_dpages:
3632 +       au_dpages_free(&dpages);
3633 +out:
3634 +       return err;
3635 +}
3636 +
3637 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3638 +                          unsigned int sigen, const unsigned int verbose)
3639 +{
3640 +       int err;
3641 +       unsigned long long max, ull;
3642 +       struct inode *i, **array;
3643 +       aufs_bindex_t btop, bbot;
3644 +
3645 +       array = au_iarray_alloc(sb, &max);
3646 +       err = PTR_ERR(array);
3647 +       if (IS_ERR(array))
3648 +               goto out;
3649 +
3650 +       err = 0;
3651 +       AuDbg("b%d\n", bindex);
3652 +       for (ull = 0; !err && ull < max; ull++) {
3653 +               i = array[ull];
3654 +               if (unlikely(!i))
3655 +                       break;
3656 +               if (i->i_ino == AUFS_ROOT_INO)
3657 +                       continue;
3658 +
3659 +               /* AuDbgInode(i); */
3660 +               if (au_iigen(i, NULL) == sigen)
3661 +                       ii_read_lock_child(i);
3662 +               else {
3663 +                       ii_write_lock_child(i);
3664 +                       err = au_refresh_hinode_self(i);
3665 +                       au_iigen_dec(i);
3666 +                       if (!err)
3667 +                               ii_downgrade_lock(i);
3668 +                       else {
3669 +                               ii_write_unlock(i);
3670 +                               break;
3671 +                       }
3672 +               }
3673 +
3674 +               btop = au_ibtop(i);
3675 +               bbot = au_ibbot(i);
3676 +               if (btop <= bindex
3677 +                   && bindex <= bbot
3678 +                   && au_h_iptr(i, bindex)
3679 +                   && au_test_ibusy(i, btop, bbot)) {
3680 +                       err = -EBUSY;
3681 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3682 +                       AuDbgInode(i);
3683 +               }
3684 +               ii_read_unlock(i);
3685 +       }
3686 +       au_iarray_free(array, max);
3687 +
3688 +out:
3689 +       return err;
3690 +}
3691 +
3692 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3693 +                             const unsigned int verbose)
3694 +{
3695 +       int err;
3696 +       unsigned int sigen;
3697 +
3698 +       sigen = au_sigen(root->d_sb);
3699 +       DiMustNoWaiters(root);
3700 +       IiMustNoWaiters(d_inode(root));
3701 +       di_write_unlock(root);
3702 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3703 +       if (!err)
3704 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3705 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3706 +
3707 +       return err;
3708 +}
3709 +
3710 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3711 +                        struct file **to_free, int *idx)
3712 +{
3713 +       int err;
3714 +       unsigned char matched, root;
3715 +       aufs_bindex_t bindex, bbot;
3716 +       struct au_fidir *fidir;
3717 +       struct au_hfile *hfile;
3718 +
3719 +       err = 0;
3720 +       root = IS_ROOT(file->f_path.dentry);
3721 +       if (root) {
3722 +               get_file(file);
3723 +               to_free[*idx] = file;
3724 +               (*idx)++;
3725 +               goto out;
3726 +       }
3727 +
3728 +       matched = 0;
3729 +       fidir = au_fi(file)->fi_hdir;
3730 +       AuDebugOn(!fidir);
3731 +       bbot = au_fbbot_dir(file);
3732 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3733 +               hfile = fidir->fd_hfile + bindex;
3734 +               if (!hfile->hf_file)
3735 +                       continue;
3736 +
3737 +               if (hfile->hf_br->br_id == br_id) {
3738 +                       matched = 1;
3739 +                       break;
3740 +               }
3741 +       }
3742 +       if (matched)
3743 +               err = -EBUSY;
3744 +
3745 +out:
3746 +       return err;
3747 +}
3748 +
3749 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3750 +                         struct file **to_free, int opened)
3751 +{
3752 +       int err, idx;
3753 +       unsigned long long ull, max;
3754 +       aufs_bindex_t btop;
3755 +       struct file *file, **array;
3756 +       struct dentry *root;
3757 +       struct au_hfile *hfile;
3758 +
3759 +       array = au_farray_alloc(sb, &max);
3760 +       err = PTR_ERR(array);
3761 +       if (IS_ERR(array))
3762 +               goto out;
3763 +
3764 +       err = 0;
3765 +       idx = 0;
3766 +       root = sb->s_root;
3767 +       di_write_unlock(root);
3768 +       for (ull = 0; ull < max; ull++) {
3769 +               file = array[ull];
3770 +               if (unlikely(!file))
3771 +                       break;
3772 +
3773 +               /* AuDbg("%pD\n", file); */
3774 +               fi_read_lock(file);
3775 +               btop = au_fbtop(file);
3776 +               if (!d_is_dir(file->f_path.dentry)) {
3777 +                       hfile = &au_fi(file)->fi_htop;
3778 +                       if (hfile->hf_br->br_id == br_id)
3779 +                               err = -EBUSY;
3780 +               } else
3781 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3782 +               fi_read_unlock(file);
3783 +               if (unlikely(err))
3784 +                       break;
3785 +       }
3786 +       di_write_lock_child(root);
3787 +       au_farray_free(array, max);
3788 +       AuDebugOn(idx > opened);
3789 +
3790 +out:
3791 +       return err;
3792 +}
3793 +
3794 +static void br_del_file(struct file **to_free, unsigned long long opened,
3795 +                       aufs_bindex_t br_id)
3796 +{
3797 +       unsigned long long ull;
3798 +       aufs_bindex_t bindex, btop, bbot, bfound;
3799 +       struct file *file;
3800 +       struct au_fidir *fidir;
3801 +       struct au_hfile *hfile;
3802 +
3803 +       for (ull = 0; ull < opened; ull++) {
3804 +               file = to_free[ull];
3805 +               if (unlikely(!file))
3806 +                       break;
3807 +
3808 +               /* AuDbg("%pD\n", file); */
3809 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3810 +               bfound = -1;
3811 +               fidir = au_fi(file)->fi_hdir;
3812 +               AuDebugOn(!fidir);
3813 +               fi_write_lock(file);
3814 +               btop = au_fbtop(file);
3815 +               bbot = au_fbbot_dir(file);
3816 +               for (bindex = btop; bindex <= bbot; bindex++) {
3817 +                       hfile = fidir->fd_hfile + bindex;
3818 +                       if (!hfile->hf_file)
3819 +                               continue;
3820 +
3821 +                       if (hfile->hf_br->br_id == br_id) {
3822 +                               bfound = bindex;
3823 +                               break;
3824 +                       }
3825 +               }
3826 +               AuDebugOn(bfound < 0);
3827 +               au_set_h_fptr(file, bfound, NULL);
3828 +               if (bfound == btop) {
3829 +                       for (btop++; btop <= bbot; btop++)
3830 +                               if (au_hf_dir(file, btop)) {
3831 +                                       au_set_fbtop(file, btop);
3832 +                                       break;
3833 +                               }
3834 +               }
3835 +               fi_write_unlock(file);
3836 +       }
3837 +}
3838 +
3839 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3840 +                            const aufs_bindex_t bindex,
3841 +                            const aufs_bindex_t bbot)
3842 +{
3843 +       struct au_branch **brp, **p;
3844 +
3845 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3846 +
3847 +       brp = sbinfo->si_branch + bindex;
3848 +       if (bindex < bbot)
3849 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3850 +       sbinfo->si_branch[0 + bbot] = NULL;
3851 +       sbinfo->si_bbot--;
3852 +
3853 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3854 +                       /*may_shrink*/1);
3855 +       if (p)
3856 +               sbinfo->si_branch = p;
3857 +       /* harmless error */
3858 +}
3859 +
3860 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3861 +                            const aufs_bindex_t bbot)
3862 +{
3863 +       struct au_hdentry *hdp, *p;
3864 +
3865 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3866 +
3867 +       hdp = au_hdentry(dinfo, bindex);
3868 +       if (bindex < bbot)
3869 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3870 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3871 +       dinfo->di_bbot--;
3872 +
3873 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3874 +                       /*may_shrink*/1);
3875 +       if (p)
3876 +               dinfo->di_hdentry = p;
3877 +       /* harmless error */
3878 +}
3879 +
3880 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3881 +                            const aufs_bindex_t bbot)
3882 +{
3883 +       struct au_hinode *hip, *p;
3884 +
3885 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3886 +
3887 +       hip = au_hinode(iinfo, bindex);
3888 +       if (bindex < bbot)
3889 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3890 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3891 +       iinfo->ii_bbot--;
3892 +
3893 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3894 +                       /*may_shrink*/1);
3895 +       if (p)
3896 +               iinfo->ii_hinode = p;
3897 +       /* harmless error */
3898 +}
3899 +
3900 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3901 +                        struct au_branch *br)
3902 +{
3903 +       aufs_bindex_t bbot;
3904 +       struct au_sbinfo *sbinfo;
3905 +       struct dentry *root, *h_root;
3906 +       struct inode *inode, *h_inode;
3907 +       struct au_hinode *hinode;
3908 +
3909 +       SiMustWriteLock(sb);
3910 +
3911 +       root = sb->s_root;
3912 +       inode = d_inode(root);
3913 +       sbinfo = au_sbi(sb);
3914 +       bbot = sbinfo->si_bbot;
3915 +
3916 +       h_root = au_h_dptr(root, bindex);
3917 +       hinode = au_hi(inode, bindex);
3918 +       h_inode = au_igrab(hinode->hi_inode);
3919 +       au_hiput(hinode);
3920 +
3921 +       au_sbilist_lock();
3922 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3923 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3924 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3925 +       au_sbilist_unlock();
3926 +
3927 +       /* ignore an error */
3928 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3929 +
3930 +       dput(h_root);
3931 +       iput(h_inode);
3932 +       au_br_do_free(br);
3933 +}
3934 +
3935 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3936 +                                  unsigned long long max, void *arg)
3937 +{
3938 +       return max;
3939 +}
3940 +
3941 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3942 +{
3943 +       int err, rerr, i;
3944 +       unsigned long long opened;
3945 +       unsigned int mnt_flags;
3946 +       aufs_bindex_t bindex, bbot, br_id;
3947 +       unsigned char do_wh, verbose;
3948 +       struct au_branch *br;
3949 +       struct au_wbr *wbr;
3950 +       struct dentry *root;
3951 +       struct file **to_free;
3952 +
3953 +       err = 0;
3954 +       opened = 0;
3955 +       to_free = NULL;
3956 +       root = sb->s_root;
3957 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3958 +       if (bindex < 0) {
3959 +               if (remount)
3960 +                       goto out; /* success */
3961 +               err = -ENOENT;
3962 +               pr_err("%s no such branch\n", del->pathname);
3963 +               goto out;
3964 +       }
3965 +       AuDbg("bindex b%d\n", bindex);
3966 +
3967 +       err = -EBUSY;
3968 +       mnt_flags = au_mntflags(sb);
3969 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3970 +       bbot = au_sbbot(sb);
3971 +       if (unlikely(!bbot)) {
3972 +               AuVerbose(verbose, "no more branches left\n");
3973 +               goto out;
3974 +       }
3975 +
3976 +       br = au_sbr(sb, bindex);
3977 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3978 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3979 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3980 +               goto out;
3981 +       }
3982 +
3983 +       br_id = br->br_id;
3984 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3985 +       if (unlikely(opened)) {
3986 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3987 +               err = PTR_ERR(to_free);
3988 +               if (IS_ERR(to_free))
3989 +                       goto out;
3990 +
3991 +               err = test_file_busy(sb, br_id, to_free, opened);
3992 +               if (unlikely(err)) {
3993 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3994 +                       goto out;
3995 +               }
3996 +       }
3997 +
3998 +       wbr = br->br_wbr;
3999 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4000 +       if (do_wh) {
4001 +               /* instead of WbrWhMustWriteLock(wbr) */
4002 +               SiMustWriteLock(sb);
4003 +               for (i = 0; i < AuBrWh_Last; i++) {
4004 +                       dput(wbr->wbr_wh[i]);
4005 +                       wbr->wbr_wh[i] = NULL;
4006 +               }
4007 +       }
4008 +
4009 +       err = test_children_busy(root, bindex, verbose);
4010 +       if (unlikely(err)) {
4011 +               if (do_wh)
4012 +                       goto out_wh;
4013 +               goto out;
4014 +       }
4015 +
4016 +       err = 0;
4017 +       if (to_free) {
4018 +               /*
4019 +                * now we confirmed the branch is deletable.
4020 +                * let's free the remaining opened dirs on the branch.
4021 +                */
4022 +               di_write_unlock(root);
4023 +               br_del_file(to_free, opened, br_id);
4024 +               di_write_lock_child(root);
4025 +       }
4026 +
4027 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4028 +       dbgaufs_xino_del(br);           /* remove one */
4029 +       au_br_do_del(sb, bindex, br);
4030 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4031 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4032 +
4033 +       if (!bindex) {
4034 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4035 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4036 +       } else
4037 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4038 +       if (au_opt_test(mnt_flags, PLINK))
4039 +               au_plink_half_refresh(sb, br_id);
4040 +
4041 +       goto out; /* success */
4042 +
4043 +out_wh:
4044 +       /* revert */
4045 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4046 +       if (rerr)
4047 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4048 +                       del->pathname, rerr);
4049 +out:
4050 +       if (to_free)
4051 +               au_farray_free(to_free, opened);
4052 +       return err;
4053 +}
4054 +
4055 +/* ---------------------------------------------------------------------- */
4056 +
4057 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4058 +{
4059 +       int err;
4060 +       aufs_bindex_t btop, bbot;
4061 +       struct aufs_ibusy ibusy;
4062 +       struct inode *inode, *h_inode;
4063 +
4064 +       err = -EPERM;
4065 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4066 +               goto out;
4067 +
4068 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4069 +       if (!err)
4070 +               /* VERIFY_WRITE */
4071 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4072 +       if (unlikely(err)) {
4073 +               err = -EFAULT;
4074 +               AuTraceErr(err);
4075 +               goto out;
4076 +       }
4077 +
4078 +       err = -EINVAL;
4079 +       si_read_lock(sb, AuLock_FLUSH);
4080 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4081 +               goto out_unlock;
4082 +
4083 +       err = 0;
4084 +       ibusy.h_ino = 0; /* invalid */
4085 +       inode = ilookup(sb, ibusy.ino);
4086 +       if (!inode
4087 +           || inode->i_ino == AUFS_ROOT_INO
4088 +           || au_is_bad_inode(inode))
4089 +               goto out_unlock;
4090 +
4091 +       ii_read_lock_child(inode);
4092 +       btop = au_ibtop(inode);
4093 +       bbot = au_ibbot(inode);
4094 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4095 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4096 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4097 +                       ibusy.h_ino = h_inode->i_ino;
4098 +       }
4099 +       ii_read_unlock(inode);
4100 +       iput(inode);
4101 +
4102 +out_unlock:
4103 +       si_read_unlock(sb);
4104 +       if (!err) {
4105 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4106 +               if (unlikely(err)) {
4107 +                       err = -EFAULT;
4108 +                       AuTraceErr(err);
4109 +               }
4110 +       }
4111 +out:
4112 +       return err;
4113 +}
4114 +
4115 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4116 +{
4117 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4118 +}
4119 +
4120 +#ifdef CONFIG_COMPAT
4121 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4122 +{
4123 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4124 +}
4125 +#endif
4126 +
4127 +/* ---------------------------------------------------------------------- */
4128 +
4129 +/*
4130 + * change a branch permission
4131 + */
4132 +
4133 +static void au_warn_ima(void)
4134 +{
4135 +#ifdef CONFIG_IMA
4136 +       /* since it doesn't support mark_files_ro() */
4137 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4138 +#endif
4139 +}
4140 +
4141 +static int do_need_sigen_inc(int a, int b)
4142 +{
4143 +       return au_br_whable(a) && !au_br_whable(b);
4144 +}
4145 +
4146 +static int need_sigen_inc(int old, int new)
4147 +{
4148 +       return do_need_sigen_inc(old, new)
4149 +               || do_need_sigen_inc(new, old);
4150 +}
4151 +
4152 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4153 +{
4154 +       int err, do_warn;
4155 +       unsigned int mnt_flags;
4156 +       unsigned long long ull, max;
4157 +       aufs_bindex_t br_id;
4158 +       unsigned char verbose, writer;
4159 +       struct file *file, *hf, **array;
4160 +       struct au_hfile *hfile;
4161 +       struct inode *h_inode;
4162 +
4163 +       mnt_flags = au_mntflags(sb);
4164 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4165 +
4166 +       array = au_farray_alloc(sb, &max);
4167 +       err = PTR_ERR(array);
4168 +       if (IS_ERR(array))
4169 +               goto out;
4170 +
4171 +       do_warn = 0;
4172 +       br_id = au_sbr_id(sb, bindex);
4173 +       for (ull = 0; ull < max; ull++) {
4174 +               file = array[ull];
4175 +               if (unlikely(!file))
4176 +                       break;
4177 +
4178 +               /* AuDbg("%pD\n", file); */
4179 +               fi_read_lock(file);
4180 +               if (unlikely(au_test_mmapped(file))) {
4181 +                       err = -EBUSY;
4182 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4183 +                       AuDbgFile(file);
4184 +                       FiMustNoWaiters(file);
4185 +                       fi_read_unlock(file);
4186 +                       goto out_array;
4187 +               }
4188 +
4189 +               hfile = &au_fi(file)->fi_htop;
4190 +               hf = hfile->hf_file;
4191 +               if (!d_is_reg(file->f_path.dentry)
4192 +                   || !(file->f_mode & FMODE_WRITE)
4193 +                   || hfile->hf_br->br_id != br_id
4194 +                   || !(hf->f_mode & FMODE_WRITE))
4195 +                       array[ull] = NULL;
4196 +               else {
4197 +                       do_warn = 1;
4198 +                       get_file(file);
4199 +               }
4200 +
4201 +               FiMustNoWaiters(file);
4202 +               fi_read_unlock(file);
4203 +               fput(file);
4204 +       }
4205 +
4206 +       err = 0;
4207 +       if (do_warn)
4208 +               au_warn_ima();
4209 +
4210 +       for (ull = 0; ull < max; ull++) {
4211 +               file = array[ull];
4212 +               if (!file)
4213 +                       continue;
4214 +
4215 +               /* todo: already flushed? */
4216 +               /*
4217 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4218 +                * approach which resets f_mode and calls mnt_drop_write() and
4219 +                * file_release_write() for each file, because the branch
4220 +                * attribute in aufs world is totally different from the native
4221 +                * fs rw/ro mode.
4222 +               */
4223 +               /* fi_read_lock(file); */
4224 +               hfile = &au_fi(file)->fi_htop;
4225 +               hf = hfile->hf_file;
4226 +               /* fi_read_unlock(file); */
4227 +               spin_lock(&hf->f_lock);
4228 +               writer = !!(hf->f_mode & FMODE_WRITER);
4229 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4230 +               spin_unlock(&hf->f_lock);
4231 +               if (writer) {
4232 +                       h_inode = file_inode(hf);
4233 +                       if (hf->f_mode & FMODE_READ)
4234 +                               i_readcount_inc(h_inode);
4235 +                       put_write_access(h_inode);
4236 +                       __mnt_drop_write(hf->f_path.mnt);
4237 +               }
4238 +       }
4239 +
4240 +out_array:
4241 +       au_farray_free(array, max);
4242 +out:
4243 +       AuTraceErr(err);
4244 +       return err;
4245 +}
4246 +
4247 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4248 +             int *do_refresh)
4249 +{
4250 +       int err, rerr;
4251 +       aufs_bindex_t bindex;
4252 +       struct dentry *root;
4253 +       struct au_branch *br;
4254 +       struct au_br_fhsm *bf;
4255 +
4256 +       root = sb->s_root;
4257 +       bindex = au_find_dbindex(root, mod->h_root);
4258 +       if (bindex < 0) {
4259 +               if (remount)
4260 +                       return 0; /* success */
4261 +               err = -ENOENT;
4262 +               pr_err("%s no such branch\n", mod->path);
4263 +               goto out;
4264 +       }
4265 +       AuDbg("bindex b%d\n", bindex);
4266 +
4267 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4268 +       if (unlikely(err))
4269 +               goto out;
4270 +
4271 +       br = au_sbr(sb, bindex);
4272 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4273 +       if (br->br_perm == mod->perm)
4274 +               return 0; /* success */
4275 +
4276 +       /* pre-allocate for non-fhsm --> fhsm */
4277 +       bf = NULL;
4278 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4279 +               err = au_fhsm_br_alloc(br);
4280 +               if (unlikely(err))
4281 +                       goto out;
4282 +               bf = br->br_fhsm;
4283 +               br->br_fhsm = NULL;
4284 +       }
4285 +
4286 +       if (au_br_writable(br->br_perm)) {
4287 +               /* remove whiteout base */
4288 +               err = au_br_init_wh(sb, br, mod->perm);
4289 +               if (unlikely(err))
4290 +                       goto out_bf;
4291 +
4292 +               if (!au_br_writable(mod->perm)) {
4293 +                       /* rw --> ro, file might be mmapped */
4294 +                       DiMustNoWaiters(root);
4295 +                       IiMustNoWaiters(d_inode(root));
4296 +                       di_write_unlock(root);
4297 +                       err = au_br_mod_files_ro(sb, bindex);
4298 +                       /* aufs_write_lock() calls ..._child() */
4299 +                       di_write_lock_child(root);
4300 +
4301 +                       if (unlikely(err)) {
4302 +                               rerr = -ENOMEM;
4303 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4304 +                                                    GFP_NOFS);
4305 +                               if (br->br_wbr)
4306 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4307 +                               if (unlikely(rerr)) {
4308 +                                       AuIOErr("nested error %d (%d)\n",
4309 +                                               rerr, err);
4310 +                                       br->br_perm = mod->perm;
4311 +                               }
4312 +                       }
4313 +               }
4314 +       } else if (au_br_writable(mod->perm)) {
4315 +               /* ro --> rw */
4316 +               err = -ENOMEM;
4317 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4318 +               if (br->br_wbr) {
4319 +                       err = au_wbr_init(br, sb, mod->perm);
4320 +                       if (unlikely(err)) {
4321 +                               au_kfree_rcu(br->br_wbr);
4322 +                               br->br_wbr = NULL;
4323 +                       }
4324 +               }
4325 +       }
4326 +       if (unlikely(err))
4327 +               goto out_bf;
4328 +
4329 +       if (au_br_fhsm(br->br_perm)) {
4330 +               if (!au_br_fhsm(mod->perm)) {
4331 +                       /* fhsm --> non-fhsm */
4332 +                       au_br_fhsm_fin(br->br_fhsm);
4333 +                       au_kfree_rcu(br->br_fhsm);
4334 +                       br->br_fhsm = NULL;
4335 +               }
4336 +       } else if (au_br_fhsm(mod->perm))
4337 +               /* non-fhsm --> fhsm */
4338 +               br->br_fhsm = bf;
4339 +
4340 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4341 +       br->br_perm = mod->perm;
4342 +       goto out; /* success */
4343 +
4344 +out_bf:
4345 +       au_kfree_try_rcu(bf);
4346 +out:
4347 +       AuTraceErr(err);
4348 +       return err;
4349 +}
4350 +
4351 +/* ---------------------------------------------------------------------- */
4352 +
4353 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4354 +{
4355 +       int err;
4356 +       struct kstatfs kstfs;
4357 +
4358 +       err = vfs_statfs(&br->br_path, &kstfs);
4359 +       if (!err) {
4360 +               stfs->f_blocks = kstfs.f_blocks;
4361 +               stfs->f_bavail = kstfs.f_bavail;
4362 +               stfs->f_files = kstfs.f_files;
4363 +               stfs->f_ffree = kstfs.f_ffree;
4364 +       }
4365 +
4366 +       return err;
4367 +}
4368 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4369 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4370 +++ linux/fs/aufs/branch.h      2021-02-24 13:33:42.741013619 +0100
4371 @@ -0,0 +1,364 @@
4372 +/* SPDX-License-Identifier: GPL-2.0 */
4373 +/*
4374 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4375 + *
4376 + * This program, aufs is free software; you can redistribute it and/or modify
4377 + * it under the terms of the GNU General Public License as published by
4378 + * the Free Software Foundation; either version 2 of the License, or
4379 + * (at your option) any later version.
4380 + *
4381 + * This program is distributed in the hope that it will be useful,
4382 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4383 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4384 + * GNU General Public License for more details.
4385 + *
4386 + * You should have received a copy of the GNU General Public License
4387 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4388 + */
4389 +
4390 +/*
4391 + * branch filesystems and xino for them
4392 + */
4393 +
4394 +#ifndef __AUFS_BRANCH_H__
4395 +#define __AUFS_BRANCH_H__
4396 +
4397 +#ifdef __KERNEL__
4398 +
4399 +#include <linux/mount.h>
4400 +#include "dirren.h"
4401 +#include "dynop.h"
4402 +#include "lcnt.h"
4403 +#include "rwsem.h"
4404 +#include "super.h"
4405 +
4406 +/* ---------------------------------------------------------------------- */
4407 +
4408 +/* a xino file */
4409 +struct au_xino {
4410 +       struct file             **xi_file;
4411 +       unsigned int            xi_nfile;
4412 +
4413 +       struct {
4414 +               spinlock_t              spin;
4415 +               ino_t                   *array;
4416 +               int                     total;
4417 +               /* reserved for future use */
4418 +               /* unsigned long        *bitmap; */
4419 +               wait_queue_head_t       wqh;
4420 +       } xi_nondir;
4421 +
4422 +       struct mutex            xi_mtx; /* protects xi_file array */
4423 +       struct hlist_bl_head    xi_writing;
4424 +
4425 +       atomic_t                xi_truncating;
4426 +
4427 +       struct kref             xi_kref;
4428 +};
4429 +
4430 +/* File-based Hierarchical Storage Management */
4431 +struct au_br_fhsm {
4432 +#ifdef CONFIG_AUFS_FHSM
4433 +       struct mutex            bf_lock;
4434 +       unsigned long           bf_jiffy;
4435 +       struct aufs_stfs        bf_stfs;
4436 +       int                     bf_readable;
4437 +#endif
4438 +};
4439 +
4440 +/* members for writable branch only */
4441 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4442 +struct au_wbr {
4443 +       struct au_rwsem         wbr_wh_rwsem;
4444 +       struct dentry           *wbr_wh[AuBrWh_Last];
4445 +       atomic_t                wbr_wh_running;
4446 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4447 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4448 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4449 +
4450 +       /* mfs mode */
4451 +       unsigned long long      wbr_bytes;
4452 +};
4453 +
4454 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4455 +#define AuBrDynOp (AuDyLast * 4)
4456 +
4457 +#ifdef CONFIG_AUFS_HFSNOTIFY
4458 +/* support for asynchronous destruction */
4459 +struct au_br_hfsnotify {
4460 +       struct fsnotify_group   *hfsn_group;
4461 +};
4462 +#endif
4463 +
4464 +/* sysfs entries */
4465 +struct au_brsysfs {
4466 +       char                    name[16];
4467 +       struct attribute        attr;
4468 +};
4469 +
4470 +enum {
4471 +       AuBrSysfs_BR,
4472 +       AuBrSysfs_BRID,
4473 +       AuBrSysfs_Last
4474 +};
4475 +
4476 +/* protected by superblock rwsem */
4477 +struct au_branch {
4478 +       struct au_xino          *br_xino;
4479 +
4480 +       aufs_bindex_t           br_id;
4481 +
4482 +       int                     br_perm;
4483 +       struct path             br_path;
4484 +       spinlock_t              br_dykey_lock;
4485 +       struct au_dykey         *br_dykey[AuBrDynOp];
4486 +       au_lcnt_t               br_nfiles;      /* opened files */
4487 +       au_lcnt_t               br_count;       /* in-use for other */
4488 +
4489 +       struct au_wbr           *br_wbr;
4490 +       struct au_br_fhsm       *br_fhsm;
4491 +
4492 +#ifdef CONFIG_AUFS_HFSNOTIFY
4493 +       struct au_br_hfsnotify  *br_hfsn;
4494 +#endif
4495 +
4496 +#ifdef CONFIG_SYSFS
4497 +       /* entries under sysfs per mount-point */
4498 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4499 +#endif
4500 +
4501 +#ifdef CONFIG_DEBUG_FS
4502 +       struct dentry            *br_dbgaufs; /* xino */
4503 +#endif
4504 +
4505 +       struct au_dr_br         br_dirren;
4506 +};
4507 +
4508 +/* ---------------------------------------------------------------------- */
4509 +
4510 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4511 +{
4512 +       return br->br_path.mnt;
4513 +}
4514 +
4515 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4516 +{
4517 +       return br->br_path.dentry;
4518 +}
4519 +
4520 +static inline struct super_block *au_br_sb(struct au_branch *br)
4521 +{
4522 +       return au_br_mnt(br)->mnt_sb;
4523 +}
4524 +
4525 +static inline int au_br_rdonly(struct au_branch *br)
4526 +{
4527 +       return (sb_rdonly(au_br_sb(br))
4528 +               || !au_br_writable(br->br_perm))
4529 +               ? -EROFS : 0;
4530 +}
4531 +
4532 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4533 +{
4534 +#ifdef CONFIG_AUFS_HNOTIFY
4535 +       return !(brperm & AuBrPerm_RR);
4536 +#else
4537 +       return 0;
4538 +#endif
4539 +}
4540 +
4541 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4542 +{
4543 +       int err, exec_flag;
4544 +
4545 +       err = 0;
4546 +       exec_flag = oflag & __FMODE_EXEC;
4547 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4548 +               err = -EACCES;
4549 +
4550 +       return err;
4551 +}
4552 +
4553 +static inline void au_xino_get(struct au_branch *br)
4554 +{
4555 +       struct au_xino *xi;
4556 +
4557 +       xi = br->br_xino;
4558 +       if (xi)
4559 +               kref_get(&xi->xi_kref);
4560 +}
4561 +
4562 +static inline int au_xino_count(struct au_branch *br)
4563 +{
4564 +       int v;
4565 +       struct au_xino *xi;
4566 +
4567 +       v = 0;
4568 +       xi = br->br_xino;
4569 +       if (xi)
4570 +               v = kref_read(&xi->xi_kref);
4571 +
4572 +       return v;
4573 +}
4574 +
4575 +/* ---------------------------------------------------------------------- */
4576 +
4577 +/* branch.c */
4578 +struct au_sbinfo;
4579 +void au_br_free(struct au_sbinfo *sinfo);
4580 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4581 +struct au_opt_add;
4582 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4583 +struct au_opt_del;
4584 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4585 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4586 +#ifdef CONFIG_COMPAT
4587 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4588 +#endif
4589 +struct au_opt_mod;
4590 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4591 +             int *do_refresh);
4592 +struct aufs_stfs;
4593 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4594 +
4595 +/* xino.c */
4596 +static const loff_t au_loff_max = LLONG_MAX;
4597 +
4598 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4599 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4600 +                           int wbrtop);
4601 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4602 +                            struct file *copy_src);
4603 +struct au_xi_new {
4604 +       struct au_xino *xi;     /* switch between xino and xigen */
4605 +       int idx;
4606 +       struct path *base;
4607 +       struct file *copy_src;
4608 +};
4609 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4610 +
4611 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4612 +                ino_t *ino);
4613 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4614 +                 ino_t ino);
4615 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4616 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4617 +
4618 +int au_xib_trunc(struct super_block *sb);
4619 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4620 +
4621 +struct au_xino *au_xino_alloc(unsigned int nfile);
4622 +int au_xino_put(struct au_branch *br);
4623 +struct file *au_xino_file1(struct au_xino *xi);
4624 +
4625 +struct au_opt_xino;
4626 +void au_xino_clr(struct super_block *sb);
4627 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4628 +struct file *au_xino_def(struct super_block *sb);
4629 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4630 +                   struct path *base);
4631 +
4632 +ino_t au_xino_new_ino(struct super_block *sb);
4633 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4634 +
4635 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4636 +                      ino_t h_ino, int idx);
4637 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4638 +                     int *idx);
4639 +
4640 +int au_xino_path(struct seq_file *seq, struct file *file);
4641 +
4642 +/* ---------------------------------------------------------------------- */
4643 +
4644 +/* @idx is signed to accept -1 meaning the first file */
4645 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4646 +{
4647 +       struct file *file;
4648 +
4649 +       file = NULL;
4650 +       if (!xi)
4651 +               goto out;
4652 +
4653 +       if (idx >= 0) {
4654 +               if (idx < xi->xi_nfile)
4655 +                       file = xi->xi_file[idx];
4656 +       } else
4657 +               file = au_xino_file1(xi);
4658 +
4659 +out:
4660 +       return file;
4661 +}
4662 +
4663 +/* ---------------------------------------------------------------------- */
4664 +
4665 +/* Superblock to branch */
4666 +static inline
4667 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4668 +{
4669 +       return au_sbr(sb, bindex)->br_id;
4670 +}
4671 +
4672 +static inline
4673 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4674 +{
4675 +       return au_br_mnt(au_sbr(sb, bindex));
4676 +}
4677 +
4678 +static inline
4679 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4680 +{
4681 +       return au_br_sb(au_sbr(sb, bindex));
4682 +}
4683 +
4684 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4685 +{
4686 +       return au_sbr(sb, bindex)->br_perm;
4687 +}
4688 +
4689 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4690 +{
4691 +       return au_br_whable(au_sbr_perm(sb, bindex));
4692 +}
4693 +
4694 +/* ---------------------------------------------------------------------- */
4695 +
4696 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4697 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4698 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4699 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4700 +/*
4701 +#define wbr_wh_read_trylock_nested(wbr) \
4702 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4703 +#define wbr_wh_write_trylock_nested(wbr) \
4704 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4705 +*/
4706 +
4707 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4708 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4709 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4710 +
4711 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4712 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4713 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4714 +
4715 +/* ---------------------------------------------------------------------- */
4716 +
4717 +#ifdef CONFIG_AUFS_FHSM
4718 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4719 +{
4720 +       mutex_init(&brfhsm->bf_lock);
4721 +       brfhsm->bf_jiffy = 0;
4722 +       brfhsm->bf_readable = 0;
4723 +}
4724 +
4725 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4726 +{
4727 +       mutex_destroy(&brfhsm->bf_lock);
4728 +}
4729 +#else
4730 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4731 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4732 +#endif
4733 +
4734 +#endif /* __KERNEL__ */
4735 +#endif /* __AUFS_BRANCH_H__ */
4736 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4737 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4738 +++ linux/fs/aufs/conf.mk       2021-02-24 13:33:42.741013619 +0100
4739 @@ -0,0 +1,40 @@
4740 +# SPDX-License-Identifier: GPL-2.0
4741 +
4742 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4743 +
4744 +define AuConf
4745 +ifdef ${1}
4746 +AuConfStr += ${1}=${${1}}
4747 +endif
4748 +endef
4749 +
4750 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4751 +       SBILIST \
4752 +       HNOTIFY HFSNOTIFY \
4753 +       EXPORT INO_T_64 \
4754 +       XATTR \
4755 +       FHSM \
4756 +       RDU \
4757 +       DIRREN \
4758 +       SHWH \
4759 +       BR_RAMFS \
4760 +       BR_FUSE POLL \
4761 +       BR_HFSPLUS \
4762 +       BDEV_LOOP \
4763 +       DEBUG MAGIC_SYSRQ
4764 +$(foreach i, ${AuConfAll}, \
4765 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4766 +
4767 +AuConfName = ${obj}/conf.str
4768 +${AuConfName}.tmp: FORCE
4769 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4770 +${AuConfName}: ${AuConfName}.tmp
4771 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4772 +       echo '  GEN    ' $@; \
4773 +       cp -p $< $@; \
4774 +       }
4775 +FORCE:
4776 +clean-files += ${AuConfName} ${AuConfName}.tmp
4777 +${obj}/sysfs.o: ${AuConfName}
4778 +
4779 +-include ${srctree}/${src}/conf_priv.mk
4780 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4781 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4782 +++ linux/fs/aufs/cpup.c        2021-02-24 13:33:42.741013619 +0100
4783 @@ -0,0 +1,1445 @@
4784 +// SPDX-License-Identifier: GPL-2.0
4785 +/*
4786 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4787 + *
4788 + * This program, aufs is free software; you can redistribute it and/or modify
4789 + * it under the terms of the GNU General Public License as published by
4790 + * the Free Software Foundation; either version 2 of the License, or
4791 + * (at your option) any later version.
4792 + *
4793 + * This program is distributed in the hope that it will be useful,
4794 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4795 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4796 + * GNU General Public License for more details.
4797 + *
4798 + * You should have received a copy of the GNU General Public License
4799 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4800 + */
4801 +
4802 +/*
4803 + * copy-up functions, see wbr_policy.c for copy-down
4804 + */
4805 +
4806 +#include <linux/fs_stack.h>
4807 +#include <linux/mm.h>
4808 +#include <linux/task_work.h>
4809 +#include "aufs.h"
4810 +
4811 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4812 +{
4813 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4814 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4815 +
4816 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4817 +
4818 +       dst->i_flags |= iflags & ~mask;
4819 +       if (au_test_fs_notime(dst->i_sb))
4820 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4821 +}
4822 +
4823 +void au_cpup_attr_timesizes(struct inode *inode)
4824 +{
4825 +       struct inode *h_inode;
4826 +
4827 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4828 +       fsstack_copy_attr_times(inode, h_inode);
4829 +       fsstack_copy_inode_size(inode, h_inode);
4830 +}
4831 +
4832 +void au_cpup_attr_nlink(struct inode *inode, int force)
4833 +{
4834 +       struct inode *h_inode;
4835 +       struct super_block *sb;
4836 +       aufs_bindex_t bindex, bbot;
4837 +
4838 +       sb = inode->i_sb;
4839 +       bindex = au_ibtop(inode);
4840 +       h_inode = au_h_iptr(inode, bindex);
4841 +       if (!force
4842 +           && !S_ISDIR(h_inode->i_mode)
4843 +           && au_opt_test(au_mntflags(sb), PLINK)
4844 +           && au_plink_test(inode))
4845 +               return;
4846 +
4847 +       /*
4848 +        * 0 can happen in revalidating.
4849 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4850 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4851 +        * case.
4852 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4853 +        *       the incorrect link count.
4854 +        */
4855 +       set_nlink(inode, h_inode->i_nlink);
4856 +
4857 +       /*
4858 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4859 +        * it may includes whplink directory.
4860 +        */
4861 +       if (S_ISDIR(h_inode->i_mode)) {
4862 +               bbot = au_ibbot(inode);
4863 +               for (bindex++; bindex <= bbot; bindex++) {
4864 +                       h_inode = au_h_iptr(inode, bindex);
4865 +                       if (h_inode)
4866 +                               au_add_nlink(inode, h_inode);
4867 +               }
4868 +       }
4869 +}
4870 +
4871 +void au_cpup_attr_changeable(struct inode *inode)
4872 +{
4873 +       struct inode *h_inode;
4874 +
4875 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4876 +       inode->i_mode = h_inode->i_mode;
4877 +       inode->i_uid = h_inode->i_uid;
4878 +       inode->i_gid = h_inode->i_gid;
4879 +       au_cpup_attr_timesizes(inode);
4880 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4881 +}
4882 +
4883 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4884 +{
4885 +       struct au_iinfo *iinfo = au_ii(inode);
4886 +
4887 +       IiMustWriteLock(inode);
4888 +
4889 +       iinfo->ii_higen = h_inode->i_generation;
4890 +       iinfo->ii_hsb1 = h_inode->i_sb;
4891 +}
4892 +
4893 +void au_cpup_attr_all(struct inode *inode, int force)
4894 +{
4895 +       struct inode *h_inode;
4896 +
4897 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4898 +       au_cpup_attr_changeable(inode);
4899 +       if (inode->i_nlink > 0)
4900 +               au_cpup_attr_nlink(inode, force);
4901 +       inode->i_rdev = h_inode->i_rdev;
4902 +       inode->i_blkbits = h_inode->i_blkbits;
4903 +       au_cpup_igen(inode, h_inode);
4904 +}
4905 +
4906 +/* ---------------------------------------------------------------------- */
4907 +
4908 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4909 +
4910 +/* keep the timestamps of the parent dir when cpup */
4911 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4912 +                   struct path *h_path)
4913 +{
4914 +       struct inode *h_inode;
4915 +
4916 +       dt->dt_dentry = dentry;
4917 +       dt->dt_h_path = *h_path;
4918 +       h_inode = d_inode(h_path->dentry);
4919 +       dt->dt_atime = h_inode->i_atime;
4920 +       dt->dt_mtime = h_inode->i_mtime;
4921 +       /* smp_mb(); */
4922 +}
4923 +
4924 +void au_dtime_revert(struct au_dtime *dt)
4925 +{
4926 +       struct iattr attr;
4927 +       int err;
4928 +
4929 +       attr.ia_atime = dt->dt_atime;
4930 +       attr.ia_mtime = dt->dt_mtime;
4931 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4932 +               | ATTR_ATIME | ATTR_ATIME_SET;
4933 +
4934 +       /* no delegation since this is a directory */
4935 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4936 +       if (unlikely(err))
4937 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4938 +}
4939 +
4940 +/* ---------------------------------------------------------------------- */
4941 +
4942 +/* internal use only */
4943 +struct au_cpup_reg_attr {
4944 +       int             valid;
4945 +       struct kstat    st;
4946 +       unsigned int    iflags; /* inode->i_flags */
4947 +};
4948 +
4949 +static noinline_for_stack
4950 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4951 +              struct au_cpup_reg_attr *h_src_attr)
4952 +{
4953 +       int err, sbits, icex;
4954 +       unsigned int mnt_flags;
4955 +       unsigned char verbose;
4956 +       struct iattr ia;
4957 +       struct path h_path;
4958 +       struct inode *h_isrc, *h_idst;
4959 +       struct kstat *h_st;
4960 +       struct au_branch *br;
4961 +
4962 +       h_path.dentry = au_h_dptr(dst, bindex);
4963 +       h_idst = d_inode(h_path.dentry);
4964 +       br = au_sbr(dst->d_sb, bindex);
4965 +       h_path.mnt = au_br_mnt(br);
4966 +       h_isrc = d_inode(h_src);
4967 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4968 +               | ATTR_ATIME | ATTR_MTIME
4969 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4970 +       if (h_src_attr && h_src_attr->valid) {
4971 +               h_st = &h_src_attr->st;
4972 +               ia.ia_uid = h_st->uid;
4973 +               ia.ia_gid = h_st->gid;
4974 +               ia.ia_atime = h_st->atime;
4975 +               ia.ia_mtime = h_st->mtime;
4976 +               if (h_idst->i_mode != h_st->mode
4977 +                   && !S_ISLNK(h_idst->i_mode)) {
4978 +                       ia.ia_valid |= ATTR_MODE;
4979 +                       ia.ia_mode = h_st->mode;
4980 +               }
4981 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4982 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4983 +       } else {
4984 +               ia.ia_uid = h_isrc->i_uid;
4985 +               ia.ia_gid = h_isrc->i_gid;
4986 +               ia.ia_atime = h_isrc->i_atime;
4987 +               ia.ia_mtime = h_isrc->i_mtime;
4988 +               if (h_idst->i_mode != h_isrc->i_mode
4989 +                   && !S_ISLNK(h_idst->i_mode)) {
4990 +                       ia.ia_valid |= ATTR_MODE;
4991 +                       ia.ia_mode = h_isrc->i_mode;
4992 +               }
4993 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4994 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4995 +       }
4996 +       /* no delegation since it is just created */
4997 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4998 +
4999 +       /* is this nfs only? */
5000 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5001 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5002 +               ia.ia_mode = h_isrc->i_mode;
5003 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5004 +       }
5005 +
5006 +       icex = br->br_perm & AuBrAttr_ICEX;
5007 +       if (!err) {
5008 +               mnt_flags = au_mntflags(dst->d_sb);
5009 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5010 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5011 +       }
5012 +
5013 +       return err;
5014 +}
5015 +
5016 +/* ---------------------------------------------------------------------- */
5017 +
5018 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5019 +                          char *buf, unsigned long blksize)
5020 +{
5021 +       int err;
5022 +       size_t sz, rbytes, wbytes;
5023 +       unsigned char all_zero;
5024 +       char *p, *zp;
5025 +       struct inode *h_inode;
5026 +       /* reduce stack usage */
5027 +       struct iattr *ia;
5028 +
5029 +       zp = page_address(ZERO_PAGE(0));
5030 +       if (unlikely(!zp))
5031 +               return -ENOMEM; /* possible? */
5032 +
5033 +       err = 0;
5034 +       all_zero = 0;
5035 +       while (len) {
5036 +               AuDbg("len %lld\n", len);
5037 +               sz = blksize;
5038 +               if (len < blksize)
5039 +                       sz = len;
5040 +
5041 +               rbytes = 0;
5042 +               /* todo: signal_pending? */
5043 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5044 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5045 +                       err = rbytes;
5046 +               }
5047 +               if (unlikely(err < 0))
5048 +                       break;
5049 +
5050 +               all_zero = 0;
5051 +               if (len >= rbytes && rbytes == blksize)
5052 +                       all_zero = !memcmp(buf, zp, rbytes);
5053 +               if (!all_zero) {
5054 +                       wbytes = rbytes;
5055 +                       p = buf;
5056 +                       while (wbytes) {
5057 +                               size_t b;
5058 +
5059 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5060 +                               err = b;
5061 +                               /* todo: signal_pending? */
5062 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5063 +                                       continue;
5064 +                               if (unlikely(err < 0))
5065 +                                       break;
5066 +                               wbytes -= b;
5067 +                               p += b;
5068 +                       }
5069 +                       if (unlikely(err < 0))
5070 +                               break;
5071 +               } else {
5072 +                       loff_t res;
5073 +
5074 +                       AuLabel(hole);
5075 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5076 +                       err = res;
5077 +                       if (unlikely(res < 0))
5078 +                               break;
5079 +               }
5080 +               len -= rbytes;
5081 +               err = 0;
5082 +       }
5083 +
5084 +       /* the last block may be a hole */
5085 +       if (!err && all_zero) {
5086 +               AuLabel(last hole);
5087 +
5088 +               err = 1;
5089 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5090 +                       /* nfs requires this step to make last hole */
5091 +                       /* is this only nfs? */
5092 +                       do {
5093 +                               /* todo: signal_pending? */
5094 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5095 +                       } while (err == -EAGAIN || err == -EINTR);
5096 +                       if (err == 1)
5097 +                               dst->f_pos--;
5098 +               }
5099 +
5100 +               if (err == 1) {
5101 +                       ia = (void *)buf;
5102 +                       ia->ia_size = dst->f_pos;
5103 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5104 +                       ia->ia_file = dst;
5105 +                       h_inode = file_inode(dst);
5106 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5107 +                       /* no delegation since it is just created */
5108 +                       err = vfsub_notify_change(&dst->f_path, ia,
5109 +                                                 /*delegated*/NULL);
5110 +                       inode_unlock(h_inode);
5111 +               }
5112 +       }
5113 +
5114 +       return err;
5115 +}
5116 +
5117 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5118 +{
5119 +       int err;
5120 +       unsigned long blksize;
5121 +       unsigned char do_kfree;
5122 +       char *buf;
5123 +       struct super_block *h_sb;
5124 +
5125 +       err = -ENOMEM;
5126 +       h_sb = file_inode(dst)->i_sb;
5127 +       blksize = h_sb->s_blocksize;
5128 +       if (!blksize || PAGE_SIZE < blksize)
5129 +               blksize = PAGE_SIZE;
5130 +       AuDbg("blksize %lu\n", blksize);
5131 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5132 +       if (do_kfree)
5133 +               buf = kmalloc(blksize, GFP_NOFS);
5134 +       else
5135 +               buf = (void *)__get_free_page(GFP_NOFS);
5136 +       if (unlikely(!buf))
5137 +               goto out;
5138 +
5139 +       if (len > (1 << 22))
5140 +               AuDbg("copying a large file %lld\n", (long long)len);
5141 +
5142 +       src->f_pos = 0;
5143 +       dst->f_pos = 0;
5144 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5145 +       if (do_kfree) {
5146 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5147 +               au_kfree_do_rcu(buf);
5148 +       } else
5149 +               free_page((unsigned long)buf);
5150 +
5151 +out:
5152 +       return err;
5153 +}
5154 +
5155 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5156 +{
5157 +       int err;
5158 +       struct super_block *h_src_sb;
5159 +       struct inode *h_src_inode;
5160 +
5161 +       h_src_inode = file_inode(src);
5162 +       h_src_sb = h_src_inode->i_sb;
5163 +
5164 +       /* XFS acquires inode_lock */
5165 +       if (!au_test_xfs(h_src_sb))
5166 +               err = au_copy_file(dst, src, len);
5167 +       else {
5168 +               inode_unlock_shared(h_src_inode);
5169 +               err = au_copy_file(dst, src, len);
5170 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5171 +       }
5172 +
5173 +       return err;
5174 +}
5175 +
5176 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5177 +{
5178 +       int err;
5179 +       loff_t lo;
5180 +       struct super_block *h_src_sb;
5181 +       struct inode *h_src_inode;
5182 +
5183 +       h_src_inode = file_inode(src);
5184 +       h_src_sb = h_src_inode->i_sb;
5185 +       if (h_src_sb != file_inode(dst)->i_sb
5186 +           || !dst->f_op->remap_file_range) {
5187 +               err = au_do_copy(dst, src, len);
5188 +               goto out;
5189 +       }
5190 +
5191 +       if (!au_test_nfs(h_src_sb)) {
5192 +               inode_unlock_shared(h_src_inode);
5193 +               lo = vfsub_clone_file_range(src, dst, len);
5194 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5195 +       } else
5196 +               lo = vfsub_clone_file_range(src, dst, len);
5197 +       if (lo == len) {
5198 +               err = 0;
5199 +               goto out; /* success */
5200 +       } else if (lo >= 0)
5201 +               /* todo: possible? */
5202 +               /* paritially succeeded */
5203 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5204 +       else if (lo != -EOPNOTSUPP) {
5205 +               /* older XFS has a condition in cloning */
5206 +               err = lo;
5207 +               goto out;
5208 +       }
5209 +
5210 +       /* the backend fs on NFS may not support cloning */
5211 +       err = au_do_copy(dst, src, len);
5212 +
5213 +out:
5214 +       AuTraceErr(err);
5215 +       return err;
5216 +}
5217 +
5218 +/*
5219 + * to support a sparse file which is opened with O_APPEND,
5220 + * we need to close the file.
5221 + */
5222 +static int au_cp_regular(struct au_cp_generic *cpg)
5223 +{
5224 +       int err, i;
5225 +       enum { SRC, DST };
5226 +       struct {
5227 +               aufs_bindex_t bindex;
5228 +               unsigned int flags;
5229 +               struct dentry *dentry;
5230 +               int force_wr;
5231 +               struct file *file;
5232 +       } *f, file[] = {
5233 +               {
5234 +                       .bindex = cpg->bsrc,
5235 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5236 +               },
5237 +               {
5238 +                       .bindex = cpg->bdst,
5239 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5240 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5241 +               }
5242 +       };
5243 +       struct au_branch *br;
5244 +       struct super_block *sb, *h_src_sb;
5245 +       struct inode *h_src_inode;
5246 +       struct task_struct *tsk = current;
5247 +
5248 +       /* bsrc branch can be ro/rw. */
5249 +       sb = cpg->dentry->d_sb;
5250 +       f = file;
5251 +       for (i = 0; i < 2; i++, f++) {
5252 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5253 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5254 +                                   /*file*/NULL, f->force_wr);
5255 +               if (IS_ERR(f->file)) {
5256 +                       err = PTR_ERR(f->file);
5257 +                       if (i == SRC)
5258 +                               goto out;
5259 +                       else
5260 +                               goto out_src;
5261 +               }
5262 +       }
5263 +
5264 +       /* try stopping to update while we copyup */
5265 +       h_src_inode = d_inode(file[SRC].dentry);
5266 +       h_src_sb = h_src_inode->i_sb;
5267 +       if (!au_test_nfs(h_src_sb))
5268 +               IMustLock(h_src_inode);
5269 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5270 +
5271 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5272 +       if (tsk->flags & PF_KTHREAD)
5273 +               __fput_sync(file[DST].file);
5274 +       else {
5275 +               /* it happened actually */
5276 +               fput(file[DST].file);
5277 +               /*
5278 +                * too bad.
5279 +                * we have to call both since we don't know which place the file
5280 +                * was added to.
5281 +                */
5282 +               task_work_run();
5283 +               flush_delayed_fput();
5284 +       }
5285 +       br = au_sbr(sb, file[DST].bindex);
5286 +       au_lcnt_dec(&br->br_nfiles);
5287 +
5288 +out_src:
5289 +       fput(file[SRC].file);
5290 +       br = au_sbr(sb, file[SRC].bindex);
5291 +       au_lcnt_dec(&br->br_nfiles);
5292 +out:
5293 +       return err;
5294 +}
5295 +
5296 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5297 +                             struct au_cpup_reg_attr *h_src_attr)
5298 +{
5299 +       int err, rerr;
5300 +       loff_t l;
5301 +       struct path h_path;
5302 +       struct inode *h_src_inode, *h_dst_inode;
5303 +
5304 +       err = 0;
5305 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5306 +       l = i_size_read(h_src_inode);
5307 +       if (cpg->len == -1 || l < cpg->len)
5308 +               cpg->len = l;
5309 +       if (cpg->len) {
5310 +               /* try stopping to update while we are referencing */
5311 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5312 +               au_pin_hdir_unlock(cpg->pin);
5313 +
5314 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5315 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5316 +               h_src_attr->iflags = h_src_inode->i_flags;
5317 +               if (!au_test_nfs(h_src_inode->i_sb))
5318 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5319 +               else {
5320 +                       inode_unlock_shared(h_src_inode);
5321 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5322 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5323 +               }
5324 +               if (unlikely(err)) {
5325 +                       inode_unlock_shared(h_src_inode);
5326 +                       goto out;
5327 +               }
5328 +               h_src_attr->valid = 1;
5329 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5330 +                       err = au_cp_regular(cpg);
5331 +                       inode_unlock_shared(h_src_inode);
5332 +               } else {
5333 +                       inode_unlock_shared(h_src_inode);
5334 +                       err = au_cp_regular(cpg);
5335 +               }
5336 +               rerr = au_pin_hdir_relock(cpg->pin);
5337 +               if (!err && rerr)
5338 +                       err = rerr;
5339 +       }
5340 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5341 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5342 +               h_dst_inode = d_inode(h_path.dentry);
5343 +               spin_lock(&h_dst_inode->i_lock);
5344 +               h_dst_inode->i_state |= I_LINKABLE;
5345 +               spin_unlock(&h_dst_inode->i_lock);
5346 +       }
5347 +
5348 +out:
5349 +       return err;
5350 +}
5351 +
5352 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5353 +                             struct inode *h_dir)
5354 +{
5355 +       int err;
5356 +       DEFINE_DELAYED_CALL(done);
5357 +       const char *sym;
5358 +
5359 +       sym = vfs_get_link(h_src, &done);
5360 +       err = PTR_ERR(sym);
5361 +       if (IS_ERR(sym))
5362 +               goto out;
5363 +
5364 +       err = vfsub_symlink(h_dir, h_path, sym);
5365 +
5366 +out:
5367 +       do_delayed_call(&done);
5368 +       return err;
5369 +}
5370 +
5371 +/*
5372 + * regardless 'acl' option, reset all ACL.
5373 + * All ACL will be copied up later from the original entry on the lower branch.
5374 + */
5375 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5376 +{
5377 +       int err;
5378 +       struct dentry *h_dentry;
5379 +       struct inode *h_inode;
5380 +
5381 +       h_dentry = h_path->dentry;
5382 +       h_inode = d_inode(h_dentry);
5383 +       /* forget_all_cached_acls(h_inode)); */
5384 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5385 +       AuTraceErr(err);
5386 +       if (err == -EOPNOTSUPP)
5387 +               err = 0;
5388 +       if (!err)
5389 +               err = vfsub_acl_chmod(h_inode, mode);
5390 +
5391 +       AuTraceErr(err);
5392 +       return err;
5393 +}
5394 +
5395 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5396 +                         struct inode *h_dir, struct path *h_path)
5397 +{
5398 +       int err;
5399 +       struct inode *dir, *inode;
5400 +
5401 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5402 +       AuTraceErr(err);
5403 +       if (err == -EOPNOTSUPP)
5404 +               err = 0;
5405 +       if (unlikely(err))
5406 +               goto out;
5407 +
5408 +       /*
5409 +        * strange behaviour from the users view,
5410 +        * particularly setattr case
5411 +        */
5412 +       dir = d_inode(dst_parent);
5413 +       if (au_ibtop(dir) == cpg->bdst)
5414 +               au_cpup_attr_nlink(dir, /*force*/1);
5415 +       inode = d_inode(cpg->dentry);
5416 +       au_cpup_attr_nlink(inode, /*force*/1);
5417 +
5418 +out:
5419 +       return err;
5420 +}
5421 +
5422 +static noinline_for_stack
5423 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5424 +              struct au_cpup_reg_attr *h_src_attr)
5425 +{
5426 +       int err;
5427 +       umode_t mode;
5428 +       unsigned int mnt_flags;
5429 +       unsigned char isdir, isreg, force;
5430 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5431 +       struct au_dtime dt;
5432 +       struct path h_path;
5433 +       struct dentry *h_src, *h_dst, *h_parent;
5434 +       struct inode *h_inode, *h_dir;
5435 +       struct super_block *sb;
5436 +
5437 +       /* bsrc branch can be ro/rw. */
5438 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5439 +       h_inode = d_inode(h_src);
5440 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5441 +
5442 +       /* try stopping to be referenced while we are creating */
5443 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5444 +       if (au_ftest_cpup(cpg->flags, RENAME))
5445 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5446 +                                 AUFS_WH_PFX_LEN));
5447 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5448 +       h_dir = d_inode(h_parent);
5449 +       IMustLock(h_dir);
5450 +       AuDebugOn(h_parent != h_dst->d_parent);
5451 +
5452 +       sb = cpg->dentry->d_sb;
5453 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5454 +       if (do_dt) {
5455 +               h_path.dentry = h_parent;
5456 +               au_dtime_store(&dt, dst_parent, &h_path);
5457 +       }
5458 +       h_path.dentry = h_dst;
5459 +
5460 +       isreg = 0;
5461 +       isdir = 0;
5462 +       mode = h_inode->i_mode;
5463 +       switch (mode & S_IFMT) {
5464 +       case S_IFREG:
5465 +               isreg = 1;
5466 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5467 +               if (!err)
5468 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5469 +               break;
5470 +       case S_IFDIR:
5471 +               isdir = 1;
5472 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5473 +               if (!err)
5474 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5475 +               break;
5476 +       case S_IFLNK:
5477 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5478 +               break;
5479 +       case S_IFCHR:
5480 +       case S_IFBLK:
5481 +               AuDebugOn(!capable(CAP_MKNOD));
5482 +               fallthrough;
5483 +       case S_IFIFO:
5484 +       case S_IFSOCK:
5485 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5486 +               break;
5487 +       default:
5488 +               AuIOErr("Unknown inode type 0%o\n", mode);
5489 +               err = -EIO;
5490 +       }
5491 +       if (!err)
5492 +               err = au_reset_acl(h_dir, &h_path, mode);
5493 +
5494 +       mnt_flags = au_mntflags(sb);
5495 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5496 +           && !isdir
5497 +           && au_opt_test(mnt_flags, XINO)
5498 +           && (h_inode->i_nlink == 1
5499 +               || (h_inode->i_state & I_LINKABLE))
5500 +           /* todo: unnecessary? */
5501 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5502 +           && cpg->bdst < cpg->bsrc
5503 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5504 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5505 +               /* ignore this error */
5506 +
5507 +       if (!err) {
5508 +               force = 0;
5509 +               if (isreg) {
5510 +                       force = !!cpg->len;
5511 +                       if (cpg->len == -1)
5512 +                               force = !!i_size_read(h_inode);
5513 +               }
5514 +               au_fhsm_wrote(sb, cpg->bdst, force);
5515 +       }
5516 +
5517 +       if (do_dt)
5518 +               au_dtime_revert(&dt);
5519 +       return err;
5520 +}
5521 +
5522 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5523 +{
5524 +       int err;
5525 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5526 +       struct inode *h_dir;
5527 +       aufs_bindex_t bdst;
5528 +
5529 +       dentry = cpg->dentry;
5530 +       bdst = cpg->bdst;
5531 +       h_dentry = au_h_dptr(dentry, bdst);
5532 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5533 +               dget(h_dentry);
5534 +               au_set_h_dptr(dentry, bdst, NULL);
5535 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5536 +               if (!err)
5537 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5538 +               au_set_h_dptr(dentry, bdst, h_dentry);
5539 +       } else {
5540 +               err = 0;
5541 +               parent = dget_parent(dentry);
5542 +               h_parent = au_h_dptr(parent, bdst);
5543 +               dput(parent);
5544 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5545 +               if (IS_ERR(h_path->dentry))
5546 +                       err = PTR_ERR(h_path->dentry);
5547 +       }
5548 +       if (unlikely(err))
5549 +               goto out;
5550 +
5551 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5552 +       h_dir = d_inode(h_parent);
5553 +       IMustLock(h_dir);
5554 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5555 +       /* no delegation since it is just created */
5556 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5557 +                          /*flags*/0);
5558 +       dput(h_path->dentry);
5559 +
5560 +out:
5561 +       return err;
5562 +}
5563 +
5564 +/*
5565 + * copyup the @dentry from @bsrc to @bdst.
5566 + * the caller must set the both of lower dentries.
5567 + * @len is for truncating when it is -1 copyup the entire file.
5568 + * in link/rename cases, @dst_parent may be different from the real one.
5569 + * basic->bsrc can be larger than basic->bdst.
5570 + * aufs doesn't touch the credential so
5571 + * security_inode_copy_up{,_xattr}() are unnecessary.
5572 + */
5573 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5574 +{
5575 +       int err, rerr;
5576 +       aufs_bindex_t old_ibtop;
5577 +       unsigned char isdir, plink;
5578 +       struct dentry *h_src, *h_dst, *h_parent;
5579 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5580 +       struct super_block *sb;
5581 +       struct au_branch *br;
5582 +       /* to reduce stack size */
5583 +       struct {
5584 +               struct au_dtime dt;
5585 +               struct path h_path;
5586 +               struct au_cpup_reg_attr h_src_attr;
5587 +       } *a;
5588 +
5589 +       err = -ENOMEM;
5590 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5591 +       if (unlikely(!a))
5592 +               goto out;
5593 +       a->h_src_attr.valid = 0;
5594 +
5595 +       sb = cpg->dentry->d_sb;
5596 +       br = au_sbr(sb, cpg->bdst);
5597 +       a->h_path.mnt = au_br_mnt(br);
5598 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5599 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5600 +       h_dir = d_inode(h_parent);
5601 +       IMustLock(h_dir);
5602 +
5603 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5604 +       inode = d_inode(cpg->dentry);
5605 +
5606 +       if (!dst_parent)
5607 +               dst_parent = dget_parent(cpg->dentry);
5608 +       else
5609 +               dget(dst_parent);
5610 +
5611 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5612 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5613 +       if (dst_inode) {
5614 +               if (unlikely(!plink)) {
5615 +                       err = -EIO;
5616 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5617 +                               "but plink is disabled\n",
5618 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5619 +                       goto out_parent;
5620 +               }
5621 +
5622 +               if (dst_inode->i_nlink) {
5623 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5624 +
5625 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5626 +                       err = PTR_ERR(h_src);
5627 +                       if (IS_ERR(h_src))
5628 +                               goto out_parent;
5629 +                       if (unlikely(d_is_negative(h_src))) {
5630 +                               err = -EIO;
5631 +                               AuIOErr("i%lu exists on b%d "
5632 +                                       "but not pseudo-linked\n",
5633 +                                       inode->i_ino, cpg->bdst);
5634 +                               dput(h_src);
5635 +                               goto out_parent;
5636 +                       }
5637 +
5638 +                       if (do_dt) {
5639 +                               a->h_path.dentry = h_parent;
5640 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5641 +                       }
5642 +
5643 +                       a->h_path.dentry = h_dst;
5644 +                       delegated = NULL;
5645 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5646 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5647 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5648 +                       if (do_dt)
5649 +                               au_dtime_revert(&a->dt);
5650 +                       if (unlikely(err == -EWOULDBLOCK)) {
5651 +                               pr_warn("cannot retry for NFSv4 delegation"
5652 +                                       " for an internal link\n");
5653 +                               iput(delegated);
5654 +                       }
5655 +                       dput(h_src);
5656 +                       goto out_parent;
5657 +               } else
5658 +                       /* todo: cpup_wh_file? */
5659 +                       /* udba work */
5660 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5661 +       }
5662 +
5663 +       isdir = S_ISDIR(inode->i_mode);
5664 +       old_ibtop = au_ibtop(inode);
5665 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5666 +       if (unlikely(err))
5667 +               goto out_rev;
5668 +       dst_inode = d_inode(h_dst);
5669 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5670 +       /* todo: necessary? */
5671 +       /* au_pin_hdir_unlock(cpg->pin); */
5672 +
5673 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5674 +       if (unlikely(err)) {
5675 +               /* todo: necessary? */
5676 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5677 +               inode_unlock(dst_inode);
5678 +               goto out_rev;
5679 +       }
5680 +
5681 +       if (cpg->bdst < old_ibtop) {
5682 +               if (S_ISREG(inode->i_mode)) {
5683 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5684 +                       if (unlikely(err)) {
5685 +                               /* ignore an error */
5686 +                               /* au_pin_hdir_relock(cpg->pin); */
5687 +                               inode_unlock(dst_inode);
5688 +                               goto out_rev;
5689 +                       }
5690 +               }
5691 +               au_set_ibtop(inode, cpg->bdst);
5692 +       } else
5693 +               au_set_ibbot(inode, cpg->bdst);
5694 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5695 +                     au_hi_flags(inode, isdir));
5696 +
5697 +       /* todo: necessary? */
5698 +       /* err = au_pin_hdir_relock(cpg->pin); */
5699 +       inode_unlock(dst_inode);
5700 +       if (unlikely(err))
5701 +               goto out_rev;
5702 +
5703 +       src_inode = d_inode(h_src);
5704 +       if (!isdir
5705 +           && (src_inode->i_nlink > 1
5706 +               || src_inode->i_state & I_LINKABLE)
5707 +           && plink)
5708 +               au_plink_append(inode, cpg->bdst, h_dst);
5709 +
5710 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5711 +               a->h_path.dentry = h_dst;
5712 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5713 +       }
5714 +       if (!err)
5715 +               goto out_parent; /* success */
5716 +
5717 +       /* revert */
5718 +out_rev:
5719 +       a->h_path.dentry = h_parent;
5720 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5721 +       a->h_path.dentry = h_dst;
5722 +       rerr = 0;
5723 +       if (d_is_positive(h_dst)) {
5724 +               if (!isdir) {
5725 +                       /* no delegation since it is just created */
5726 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5727 +                                           /*delegated*/NULL, /*force*/0);
5728 +               } else
5729 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5730 +       }
5731 +       au_dtime_revert(&a->dt);
5732 +       if (rerr) {
5733 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5734 +               err = -EIO;
5735 +       }
5736 +out_parent:
5737 +       dput(dst_parent);
5738 +       au_kfree_rcu(a);
5739 +out:
5740 +       return err;
5741 +}
5742 +
5743 +#if 0 /* reserved */
5744 +struct au_cpup_single_args {
5745 +       int *errp;
5746 +       struct au_cp_generic *cpg;
5747 +       struct dentry *dst_parent;
5748 +};
5749 +
5750 +static void au_call_cpup_single(void *args)
5751 +{
5752 +       struct au_cpup_single_args *a = args;
5753 +
5754 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5755 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5756 +       au_pin_hdir_release(a->cpg->pin);
5757 +}
5758 +#endif
5759 +
5760 +/*
5761 + * prevent SIGXFSZ in copy-up.
5762 + * testing CAP_MKNOD is for generic fs,
5763 + * but CAP_FSETID is for xfs only, currently.
5764 + */
5765 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5766 +{
5767 +       int do_sio;
5768 +       struct super_block *sb;
5769 +       struct inode *h_dir;
5770 +
5771 +       do_sio = 0;
5772 +       sb = au_pinned_parent(pin)->d_sb;
5773 +       if (!au_wkq_test()
5774 +           && (!au_sbi(sb)->si_plink_maint_pid
5775 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5776 +               switch (mode & S_IFMT) {
5777 +               case S_IFREG:
5778 +                       /* no condition about RLIMIT_FSIZE and the file size */
5779 +                       do_sio = 1;
5780 +                       break;
5781 +               case S_IFCHR:
5782 +               case S_IFBLK:
5783 +                       do_sio = !capable(CAP_MKNOD);
5784 +                       break;
5785 +               }
5786 +               if (!do_sio)
5787 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5788 +                                 && !capable(CAP_FSETID));
5789 +               /* this workaround may be removed in the future */
5790 +               if (!do_sio) {
5791 +                       h_dir = au_pinned_h_dir(pin);
5792 +                       do_sio = h_dir->i_mode & S_ISVTX;
5793 +               }
5794 +       }
5795 +
5796 +       return do_sio;
5797 +}
5798 +
5799 +#if 0 /* reserved */
5800 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5801 +{
5802 +       int err, wkq_err;
5803 +       struct dentry *h_dentry;
5804 +
5805 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5806 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5807 +               err = au_cpup_single(cpg, dst_parent);
5808 +       else {
5809 +               struct au_cpup_single_args args = {
5810 +                       .errp           = &err,
5811 +                       .cpg            = cpg,
5812 +                       .dst_parent     = dst_parent
5813 +               };
5814 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5815 +               if (unlikely(wkq_err))
5816 +                       err = wkq_err;
5817 +       }
5818 +
5819 +       return err;
5820 +}
5821 +#endif
5822 +
5823 +/*
5824 + * copyup the @dentry from the first active lower branch to @bdst,
5825 + * using au_cpup_single().
5826 + */
5827 +static int au_cpup_simple(struct au_cp_generic *cpg)
5828 +{
5829 +       int err;
5830 +       unsigned int flags_orig;
5831 +       struct dentry *dentry;
5832 +
5833 +       AuDebugOn(cpg->bsrc < 0);
5834 +
5835 +       dentry = cpg->dentry;
5836 +       DiMustWriteLock(dentry);
5837 +
5838 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5839 +       if (!err) {
5840 +               flags_orig = cpg->flags;
5841 +               au_fset_cpup(cpg->flags, RENAME);
5842 +               err = au_cpup_single(cpg, NULL);
5843 +               cpg->flags = flags_orig;
5844 +               if (!err)
5845 +                       return 0; /* success */
5846 +
5847 +               /* revert */
5848 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5849 +               au_set_dbtop(dentry, cpg->bsrc);
5850 +       }
5851 +
5852 +       return err;
5853 +}
5854 +
5855 +struct au_cpup_simple_args {
5856 +       int *errp;
5857 +       struct au_cp_generic *cpg;
5858 +};
5859 +
5860 +static void au_call_cpup_simple(void *args)
5861 +{
5862 +       struct au_cpup_simple_args *a = args;
5863 +
5864 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5865 +       *a->errp = au_cpup_simple(a->cpg);
5866 +       au_pin_hdir_release(a->cpg->pin);
5867 +}
5868 +
5869 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5870 +{
5871 +       int err, wkq_err;
5872 +       struct dentry *dentry, *parent;
5873 +       struct file *h_file;
5874 +       struct inode *h_dir;
5875 +
5876 +       dentry = cpg->dentry;
5877 +       h_file = NULL;
5878 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5879 +               AuDebugOn(cpg->bsrc < 0);
5880 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5881 +               err = PTR_ERR(h_file);
5882 +               if (IS_ERR(h_file))
5883 +                       goto out;
5884 +       }
5885 +
5886 +       parent = dget_parent(dentry);
5887 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5888 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5889 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5890 +               err = au_cpup_simple(cpg);
5891 +       else {
5892 +               struct au_cpup_simple_args args = {
5893 +                       .errp           = &err,
5894 +                       .cpg            = cpg
5895 +               };
5896 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5897 +               if (unlikely(wkq_err))
5898 +                       err = wkq_err;
5899 +       }
5900 +
5901 +       dput(parent);
5902 +       if (h_file)
5903 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5904 +
5905 +out:
5906 +       return err;
5907 +}
5908 +
5909 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5910 +{
5911 +       aufs_bindex_t bsrc, bbot;
5912 +       struct dentry *dentry, *h_dentry;
5913 +
5914 +       if (cpg->bsrc < 0) {
5915 +               dentry = cpg->dentry;
5916 +               bbot = au_dbbot(dentry);
5917 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5918 +                       h_dentry = au_h_dptr(dentry, bsrc);
5919 +                       if (h_dentry) {
5920 +                               AuDebugOn(d_is_negative(h_dentry));
5921 +                               break;
5922 +                       }
5923 +               }
5924 +               AuDebugOn(bsrc > bbot);
5925 +               cpg->bsrc = bsrc;
5926 +       }
5927 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5928 +       return au_do_sio_cpup_simple(cpg);
5929 +}
5930 +
5931 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5932 +{
5933 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5934 +       return au_do_sio_cpup_simple(cpg);
5935 +}
5936 +
5937 +/* ---------------------------------------------------------------------- */
5938 +
5939 +/*
5940 + * copyup the deleted file for writing.
5941 + */
5942 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5943 +                        struct file *file)
5944 +{
5945 +       int err;
5946 +       unsigned int flags_orig;
5947 +       aufs_bindex_t bsrc_orig;
5948 +       struct au_dinfo *dinfo;
5949 +       struct {
5950 +               struct au_hdentry *hd;
5951 +               struct dentry *h_dentry;
5952 +       } hdst, hsrc;
5953 +
5954 +       dinfo = au_di(cpg->dentry);
5955 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5956 +
5957 +       bsrc_orig = cpg->bsrc;
5958 +       cpg->bsrc = dinfo->di_btop;
5959 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5960 +       hdst.h_dentry = hdst.hd->hd_dentry;
5961 +       hdst.hd->hd_dentry = wh_dentry;
5962 +       dinfo->di_btop = cpg->bdst;
5963 +
5964 +       hsrc.h_dentry = NULL;
5965 +       if (file) {
5966 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5967 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5968 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5969 +       }
5970 +       flags_orig = cpg->flags;
5971 +       cpg->flags = !AuCpup_DTIME;
5972 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5973 +       cpg->flags = flags_orig;
5974 +       if (file) {
5975 +               if (!err)
5976 +                       err = au_reopen_nondir(file);
5977 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5978 +       }
5979 +       hdst.hd->hd_dentry = hdst.h_dentry;
5980 +       dinfo->di_btop = cpg->bsrc;
5981 +       cpg->bsrc = bsrc_orig;
5982 +
5983 +       return err;
5984 +}
5985 +
5986 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5987 +{
5988 +       int err;
5989 +       aufs_bindex_t bdst;
5990 +       struct au_dtime dt;
5991 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5992 +       struct au_branch *br;
5993 +       struct path h_path;
5994 +
5995 +       dentry = cpg->dentry;
5996 +       bdst = cpg->bdst;
5997 +       br = au_sbr(dentry->d_sb, bdst);
5998 +       parent = dget_parent(dentry);
5999 +       h_parent = au_h_dptr(parent, bdst);
6000 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6001 +       err = PTR_ERR(wh_dentry);
6002 +       if (IS_ERR(wh_dentry))
6003 +               goto out;
6004 +
6005 +       h_path.dentry = h_parent;
6006 +       h_path.mnt = au_br_mnt(br);
6007 +       au_dtime_store(&dt, parent, &h_path);
6008 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6009 +       if (unlikely(err))
6010 +               goto out_wh;
6011 +
6012 +       dget(wh_dentry);
6013 +       h_path.dentry = wh_dentry;
6014 +       if (!d_is_dir(wh_dentry)) {
6015 +               /* no delegation since it is just created */
6016 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6017 +                                  /*delegated*/NULL, /*force*/0);
6018 +       } else
6019 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6020 +       if (unlikely(err)) {
6021 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6022 +                       wh_dentry, err);
6023 +               err = -EIO;
6024 +       }
6025 +       au_dtime_revert(&dt);
6026 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6027 +
6028 +out_wh:
6029 +       dput(wh_dentry);
6030 +out:
6031 +       dput(parent);
6032 +       return err;
6033 +}
6034 +
6035 +struct au_cpup_wh_args {
6036 +       int *errp;
6037 +       struct au_cp_generic *cpg;
6038 +       struct file *file;
6039 +};
6040 +
6041 +static void au_call_cpup_wh(void *args)
6042 +{
6043 +       struct au_cpup_wh_args *a = args;
6044 +
6045 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6046 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6047 +       au_pin_hdir_release(a->cpg->pin);
6048 +}
6049 +
6050 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6051 +{
6052 +       int err, wkq_err;
6053 +       aufs_bindex_t bdst;
6054 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6055 +       struct inode *dir, *h_dir, *h_tmpdir;
6056 +       struct au_wbr *wbr;
6057 +       struct au_pin wh_pin, *pin_orig;
6058 +
6059 +       dentry = cpg->dentry;
6060 +       bdst = cpg->bdst;
6061 +       parent = dget_parent(dentry);
6062 +       dir = d_inode(parent);
6063 +       h_orph = NULL;
6064 +       h_parent = NULL;
6065 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6066 +       h_tmpdir = h_dir;
6067 +       pin_orig = NULL;
6068 +       if (!h_dir->i_nlink) {
6069 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6070 +               h_orph = wbr->wbr_orph;
6071 +
6072 +               h_parent = dget(au_h_dptr(parent, bdst));
6073 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6074 +               h_tmpdir = d_inode(h_orph);
6075 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6076 +
6077 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6078 +               /* todo: au_h_open_pre()? */
6079 +
6080 +               pin_orig = cpg->pin;
6081 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6082 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6083 +               cpg->pin = &wh_pin;
6084 +       }
6085 +
6086 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6087 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6088 +               err = au_cpup_wh(cpg, file);
6089 +       else {
6090 +               struct au_cpup_wh_args args = {
6091 +                       .errp   = &err,
6092 +                       .cpg    = cpg,
6093 +                       .file   = file
6094 +               };
6095 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6096 +               if (unlikely(wkq_err))
6097 +                       err = wkq_err;
6098 +       }
6099 +
6100 +       if (h_orph) {
6101 +               inode_unlock(h_tmpdir);
6102 +               /* todo: au_h_open_post()? */
6103 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6104 +               au_set_h_dptr(parent, bdst, h_parent);
6105 +               AuDebugOn(!pin_orig);
6106 +               cpg->pin = pin_orig;
6107 +       }
6108 +       iput(h_dir);
6109 +       dput(parent);
6110 +
6111 +       return err;
6112 +}
6113 +
6114 +/* ---------------------------------------------------------------------- */
6115 +
6116 +/*
6117 + * generic routine for both of copy-up and copy-down.
6118 + */
6119 +/* cf. revalidate function in file.c */
6120 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6121 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6122 +                        struct au_pin *pin,
6123 +                        struct dentry *h_parent, void *arg),
6124 +              void *arg)
6125 +{
6126 +       int err;
6127 +       struct au_pin pin;
6128 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6129 +
6130 +       err = 0;
6131 +       parent = dget_parent(dentry);
6132 +       if (IS_ROOT(parent))
6133 +               goto out;
6134 +
6135 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6136 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6137 +
6138 +       /* do not use au_dpage */
6139 +       real_parent = parent;
6140 +       while (1) {
6141 +               dput(parent);
6142 +               parent = dget_parent(dentry);
6143 +               h_parent = au_h_dptr(parent, bdst);
6144 +               if (h_parent)
6145 +                       goto out; /* success */
6146 +
6147 +               /* find top dir which is necessary to cpup */
6148 +               do {
6149 +                       d = parent;
6150 +                       dput(parent);
6151 +                       parent = dget_parent(d);
6152 +                       di_read_lock_parent3(parent, !AuLock_IR);
6153 +                       h_parent = au_h_dptr(parent, bdst);
6154 +                       di_read_unlock(parent, !AuLock_IR);
6155 +               } while (!h_parent);
6156 +
6157 +               if (d != real_parent)
6158 +                       di_write_lock_child3(d);
6159 +
6160 +               /* somebody else might create while we were sleeping */
6161 +               h_dentry = au_h_dptr(d, bdst);
6162 +               if (!h_dentry || d_is_negative(h_dentry)) {
6163 +                       if (h_dentry)
6164 +                               au_update_dbtop(d);
6165 +
6166 +                       au_pin_set_dentry(&pin, d);
6167 +                       err = au_do_pin(&pin);
6168 +                       if (!err) {
6169 +                               err = cp(d, bdst, &pin, h_parent, arg);
6170 +                               au_unpin(&pin);
6171 +                       }
6172 +               }
6173 +
6174 +               if (d != real_parent)
6175 +                       di_write_unlock(d);
6176 +               if (unlikely(err))
6177 +                       break;
6178 +       }
6179 +
6180 +out:
6181 +       dput(parent);
6182 +       return err;
6183 +}
6184 +
6185 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6186 +                      struct au_pin *pin,
6187 +                      struct dentry *h_parent __maybe_unused,
6188 +                      void *arg __maybe_unused)
6189 +{
6190 +       struct au_cp_generic cpg = {
6191 +               .dentry = dentry,
6192 +               .bdst   = bdst,
6193 +               .bsrc   = -1,
6194 +               .len    = 0,
6195 +               .pin    = pin,
6196 +               .flags  = AuCpup_DTIME
6197 +       };
6198 +       return au_sio_cpup_simple(&cpg);
6199 +}
6200 +
6201 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6202 +{
6203 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6204 +}
6205 +
6206 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6207 +{
6208 +       int err;
6209 +       struct dentry *parent;
6210 +       struct inode *dir;
6211 +
6212 +       parent = dget_parent(dentry);
6213 +       dir = d_inode(parent);
6214 +       err = 0;
6215 +       if (au_h_iptr(dir, bdst))
6216 +               goto out;
6217 +
6218 +       di_read_unlock(parent, AuLock_IR);
6219 +       di_write_lock_parent(parent);
6220 +       /* someone else might change our inode while we were sleeping */
6221 +       if (!au_h_iptr(dir, bdst))
6222 +               err = au_cpup_dirs(dentry, bdst);
6223 +       di_downgrade_lock(parent, AuLock_IR);
6224 +
6225 +out:
6226 +       dput(parent);
6227 +       return err;
6228 +}
6229 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6230 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6231 +++ linux/fs/aufs/cpup.h        2021-02-24 13:33:42.741013619 +0100
6232 @@ -0,0 +1,100 @@
6233 +/* SPDX-License-Identifier: GPL-2.0 */
6234 +/*
6235 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6236 + *
6237 + * This program, aufs is free software; you can redistribute it and/or modify
6238 + * it under the terms of the GNU General Public License as published by
6239 + * the Free Software Foundation; either version 2 of the License, or
6240 + * (at your option) any later version.
6241 + *
6242 + * This program is distributed in the hope that it will be useful,
6243 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6244 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6245 + * GNU General Public License for more details.
6246 + *
6247 + * You should have received a copy of the GNU General Public License
6248 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6249 + */
6250 +
6251 +/*
6252 + * copy-up/down functions
6253 + */
6254 +
6255 +#ifndef __AUFS_CPUP_H__
6256 +#define __AUFS_CPUP_H__
6257 +
6258 +#ifdef __KERNEL__
6259 +
6260 +#include <linux/path.h>
6261 +
6262 +struct inode;
6263 +struct file;
6264 +struct au_pin;
6265 +
6266 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6267 +void au_cpup_attr_timesizes(struct inode *inode);
6268 +void au_cpup_attr_nlink(struct inode *inode, int force);
6269 +void au_cpup_attr_changeable(struct inode *inode);
6270 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6271 +void au_cpup_attr_all(struct inode *inode, int force);
6272 +
6273 +/* ---------------------------------------------------------------------- */
6274 +
6275 +struct au_cp_generic {
6276 +       struct dentry   *dentry;
6277 +       aufs_bindex_t   bdst, bsrc;
6278 +       loff_t          len;
6279 +       struct au_pin   *pin;
6280 +       unsigned int    flags;
6281 +};
6282 +
6283 +/* cpup flags */
6284 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6285 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6286 +                                                  for link(2) */
6287 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6288 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6289 +                                                  cpup */
6290 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6291 +                                                  existing entry */
6292 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6293 +                                                  the branch is marked as RO */
6294 +
6295 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6296 +#undef AuCpup_HOPEN
6297 +#define AuCpup_HOPEN           0
6298 +#endif
6299 +
6300 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6301 +#define au_fset_cpup(flags, name) \
6302 +       do { (flags) |= AuCpup_##name; } while (0)
6303 +#define au_fclr_cpup(flags, name) \
6304 +       do { (flags) &= ~AuCpup_##name; } while (0)
6305 +
6306 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6307 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6308 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6309 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6310 +
6311 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6312 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6313 +                        struct au_pin *pin,
6314 +                        struct dentry *h_parent, void *arg),
6315 +              void *arg);
6316 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6317 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6318 +
6319 +/* ---------------------------------------------------------------------- */
6320 +
6321 +/* keep timestamps when copyup */
6322 +struct au_dtime {
6323 +       struct dentry *dt_dentry;
6324 +       struct path dt_h_path;
6325 +       struct timespec64 dt_atime, dt_mtime;
6326 +};
6327 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6328 +                   struct path *h_path);
6329 +void au_dtime_revert(struct au_dtime *dt);
6330 +
6331 +#endif /* __KERNEL__ */
6332 +#endif /* __AUFS_CPUP_H__ */
6333 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6334 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6335 +++ linux/fs/aufs/dbgaufs.c     2021-02-24 13:33:42.741013619 +0100
6336 @@ -0,0 +1,526 @@
6337 +// SPDX-License-Identifier: GPL-2.0
6338 +/*
6339 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6340 + *
6341 + * This program, aufs is free software; you can redistribute it and/or modify
6342 + * it under the terms of the GNU General Public License as published by
6343 + * the Free Software Foundation; either version 2 of the License, or
6344 + * (at your option) any later version.
6345 + *
6346 + * This program is distributed in the hope that it will be useful,
6347 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6348 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6349 + * GNU General Public License for more details.
6350 + *
6351 + * You should have received a copy of the GNU General Public License
6352 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6353 + */
6354 +
6355 +/*
6356 + * debugfs interface
6357 + */
6358 +
6359 +#include <linux/debugfs.h>
6360 +#include "aufs.h"
6361 +
6362 +#ifndef CONFIG_SYSFS
6363 +#error DEBUG_FS depends upon SYSFS
6364 +#endif
6365 +
6366 +static struct dentry *dbgaufs;
6367 +static const mode_t dbgaufs_mode = 0444;
6368 +
6369 +/* 20 is max digits length of ulong 64 */
6370 +struct dbgaufs_arg {
6371 +       int n;
6372 +       char a[20 * 4];
6373 +};
6374 +
6375 +/*
6376 + * common function for all XINO files
6377 + */
6378 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6379 +                             struct file *file)
6380 +{
6381 +       void *p;
6382 +
6383 +       p = file->private_data;
6384 +       if (p) {
6385 +               /* this is struct dbgaufs_arg */
6386 +               AuDebugOn(!au_kfree_sz_test(p));
6387 +               au_kfree_do_rcu(p);
6388 +       }
6389 +       return 0;
6390 +}
6391 +
6392 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6393 +                          int cnt)
6394 +{
6395 +       int err;
6396 +       struct kstat st;
6397 +       struct dbgaufs_arg *p;
6398 +
6399 +       err = -ENOMEM;
6400 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6401 +       if (unlikely(!p))
6402 +               goto out;
6403 +
6404 +       err = 0;
6405 +       p->n = 0;
6406 +       file->private_data = p;
6407 +       if (!xf)
6408 +               goto out;
6409 +
6410 +       err = vfsub_getattr(&xf->f_path, &st);
6411 +       if (!err) {
6412 +               if (do_fcnt)
6413 +                       p->n = snprintf
6414 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6415 +                                cnt, st.blocks, st.blksize,
6416 +                                (long long)st.size);
6417 +               else
6418 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6419 +                                       st.blocks, st.blksize,
6420 +                                       (long long)st.size);
6421 +               AuDebugOn(p->n >= sizeof(p->a));
6422 +       } else {
6423 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6424 +               err = 0;
6425 +       }
6426 +
6427 +out:
6428 +       return err;
6429 +}
6430 +
6431 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6432 +                              size_t count, loff_t *ppos)
6433 +{
6434 +       struct dbgaufs_arg *p;
6435 +
6436 +       p = file->private_data;
6437 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6438 +}
6439 +
6440 +/* ---------------------------------------------------------------------- */
6441 +
6442 +struct dbgaufs_plink_arg {
6443 +       int n;
6444 +       char a[];
6445 +};
6446 +
6447 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6448 +                                struct file *file)
6449 +{
6450 +       free_page((unsigned long)file->private_data);
6451 +       return 0;
6452 +}
6453 +
6454 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6455 +{
6456 +       int err, i, limit;
6457 +       unsigned long n, sum;
6458 +       struct dbgaufs_plink_arg *p;
6459 +       struct au_sbinfo *sbinfo;
6460 +       struct super_block *sb;
6461 +       struct hlist_bl_head *hbl;
6462 +
6463 +       err = -ENOMEM;
6464 +       p = (void *)get_zeroed_page(GFP_NOFS);
6465 +       if (unlikely(!p))
6466 +               goto out;
6467 +
6468 +       err = -EFBIG;
6469 +       sbinfo = inode->i_private;
6470 +       sb = sbinfo->si_sb;
6471 +       si_noflush_read_lock(sb);
6472 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6473 +               limit = PAGE_SIZE - sizeof(p->n);
6474 +
6475 +               /* the number of buckets */
6476 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6477 +               p->n += n;
6478 +               limit -= n;
6479 +
6480 +               sum = 0;
6481 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6482 +                    i++, hbl++) {
6483 +                       n = au_hbl_count(hbl);
6484 +                       sum += n;
6485 +
6486 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6487 +                       p->n += n;
6488 +                       limit -= n;
6489 +                       if (unlikely(limit <= 0))
6490 +                               goto out_free;
6491 +               }
6492 +               p->a[p->n - 1] = '\n';
6493 +
6494 +               /* the sum of plinks */
6495 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6496 +               p->n += n;
6497 +               limit -= n;
6498 +               if (unlikely(limit <= 0))
6499 +                       goto out_free;
6500 +       } else {
6501 +#define str "1\n0\n0\n"
6502 +               p->n = sizeof(str) - 1;
6503 +               strcpy(p->a, str);
6504 +#undef str
6505 +       }
6506 +       si_read_unlock(sb);
6507 +
6508 +       err = 0;
6509 +       file->private_data = p;
6510 +       goto out; /* success */
6511 +
6512 +out_free:
6513 +       free_page((unsigned long)p);
6514 +out:
6515 +       return err;
6516 +}
6517 +
6518 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6519 +                                 size_t count, loff_t *ppos)
6520 +{
6521 +       struct dbgaufs_plink_arg *p;
6522 +
6523 +       p = file->private_data;
6524 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6525 +}
6526 +
6527 +static const struct file_operations dbgaufs_plink_fop = {
6528 +       .owner          = THIS_MODULE,
6529 +       .open           = dbgaufs_plink_open,
6530 +       .release        = dbgaufs_plink_release,
6531 +       .read           = dbgaufs_plink_read
6532 +};
6533 +
6534 +/* ---------------------------------------------------------------------- */
6535 +
6536 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6537 +{
6538 +       int err;
6539 +       struct au_sbinfo *sbinfo;
6540 +       struct super_block *sb;
6541 +
6542 +       sbinfo = inode->i_private;
6543 +       sb = sbinfo->si_sb;
6544 +       si_noflush_read_lock(sb);
6545 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6546 +       si_read_unlock(sb);
6547 +       return err;
6548 +}
6549 +
6550 +static const struct file_operations dbgaufs_xib_fop = {
6551 +       .owner          = THIS_MODULE,
6552 +       .open           = dbgaufs_xib_open,
6553 +       .release        = dbgaufs_xi_release,
6554 +       .read           = dbgaufs_xi_read
6555 +};
6556 +
6557 +/* ---------------------------------------------------------------------- */
6558 +
6559 +#define DbgaufsXi_PREFIX "xi"
6560 +
6561 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6562 +{
6563 +       int err, idx;
6564 +       long l;
6565 +       aufs_bindex_t bindex;
6566 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6567 +       struct au_sbinfo *sbinfo;
6568 +       struct super_block *sb;
6569 +       struct au_xino *xi;
6570 +       struct file *xf;
6571 +       struct qstr *name;
6572 +       struct au_branch *br;
6573 +
6574 +       err = -ENOENT;
6575 +       name = &file->f_path.dentry->d_name;
6576 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6577 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6578 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6579 +               goto out;
6580 +
6581 +       AuDebugOn(name->len >= sizeof(a));
6582 +       memcpy(a, name->name, name->len);
6583 +       a[name->len] = '\0';
6584 +       p = strchr(a, '-');
6585 +       if (p)
6586 +               *p = '\0';
6587 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6588 +       if (unlikely(err))
6589 +               goto out;
6590 +       bindex = l;
6591 +       idx = 0;
6592 +       if (p) {
6593 +               err = kstrtol(p + 1, 10, &l);
6594 +               if (unlikely(err))
6595 +                       goto out;
6596 +               idx = l;
6597 +       }
6598 +
6599 +       err = -ENOENT;
6600 +       sbinfo = inode->i_private;
6601 +       sb = sbinfo->si_sb;
6602 +       si_noflush_read_lock(sb);
6603 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6604 +               goto out_si;
6605 +       br = au_sbr(sb, bindex);
6606 +       xi = br->br_xino;
6607 +       if (unlikely(idx >= xi->xi_nfile))
6608 +               goto out_si;
6609 +       xf = au_xino_file(xi, idx);
6610 +       if (xf)
6611 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6612 +                                     au_xino_count(br));
6613 +
6614 +out_si:
6615 +       si_read_unlock(sb);
6616 +out:
6617 +       AuTraceErr(err);
6618 +       return err;
6619 +}
6620 +
6621 +static const struct file_operations dbgaufs_xino_fop = {
6622 +       .owner          = THIS_MODULE,
6623 +       .open           = dbgaufs_xino_open,
6624 +       .release        = dbgaufs_xi_release,
6625 +       .read           = dbgaufs_xi_read
6626 +};
6627 +
6628 +void dbgaufs_xino_del(struct au_branch *br)
6629 +{
6630 +       struct dentry *dbgaufs;
6631 +
6632 +       dbgaufs = br->br_dbgaufs;
6633 +       if (!dbgaufs)
6634 +               return;
6635 +
6636 +       br->br_dbgaufs = NULL;
6637 +       /* debugfs acquires the parent i_mutex */
6638 +       lockdep_off();
6639 +       debugfs_remove(dbgaufs);
6640 +       lockdep_on();
6641 +}
6642 +
6643 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6644 +{
6645 +       aufs_bindex_t bbot;
6646 +       struct au_branch *br;
6647 +
6648 +       if (!au_sbi(sb)->si_dbgaufs)
6649 +               return;
6650 +
6651 +       bbot = au_sbbot(sb);
6652 +       for (; bindex <= bbot; bindex++) {
6653 +               br = au_sbr(sb, bindex);
6654 +               dbgaufs_xino_del(br);
6655 +       }
6656 +}
6657 +
6658 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6659 +                             unsigned int idx, struct dentry *parent,
6660 +                             struct au_sbinfo *sbinfo)
6661 +{
6662 +       struct au_branch *br;
6663 +       struct dentry *d;
6664 +       /* "xi" bindex(5) "-" idx(2) NULL */
6665 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6666 +
6667 +       if (!idx)
6668 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6669 +       else
6670 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6671 +                        bindex, idx);
6672 +       br = au_sbr(sb, bindex);
6673 +       if (br->br_dbgaufs) {
6674 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6675 +
6676 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6677 +                       /* debugfs acquires the parent i_mutex */
6678 +                       lockdep_off();
6679 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6680 +                                          name);
6681 +                       lockdep_on();
6682 +                       if (unlikely(!d))
6683 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6684 +                                       parent, name);
6685 +               }
6686 +       } else {
6687 +               lockdep_off();
6688 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6689 +                                                    sbinfo, &dbgaufs_xino_fop);
6690 +               lockdep_on();
6691 +               if (unlikely(!br->br_dbgaufs))
6692 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6693 +                               parent, name);
6694 +       }
6695 +}
6696 +
6697 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6698 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6699 +{
6700 +       struct au_branch *br;
6701 +       struct au_xino *xi;
6702 +       unsigned int u;
6703 +
6704 +       br = au_sbr(sb, bindex);
6705 +       xi = br->br_xino;
6706 +       for (u = 0; u < xi->xi_nfile; u++)
6707 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6708 +}
6709 +
6710 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6711 +{
6712 +       struct au_sbinfo *sbinfo;
6713 +       struct dentry *parent;
6714 +       aufs_bindex_t bbot;
6715 +
6716 +       if (!au_opt_test(au_mntflags(sb), XINO))
6717 +               return;
6718 +
6719 +       sbinfo = au_sbi(sb);
6720 +       parent = sbinfo->si_dbgaufs;
6721 +       if (!parent)
6722 +               return;
6723 +
6724 +       bbot = au_sbbot(sb);
6725 +       if (topdown)
6726 +               for (; bindex <= bbot; bindex++)
6727 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6728 +       else
6729 +               for (; bbot >= bindex; bbot--)
6730 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6731 +}
6732 +
6733 +/* ---------------------------------------------------------------------- */
6734 +
6735 +#ifdef CONFIG_AUFS_EXPORT
6736 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6737 +{
6738 +       int err;
6739 +       struct au_sbinfo *sbinfo;
6740 +       struct super_block *sb;
6741 +
6742 +       sbinfo = inode->i_private;
6743 +       sb = sbinfo->si_sb;
6744 +       si_noflush_read_lock(sb);
6745 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6746 +       si_read_unlock(sb);
6747 +       return err;
6748 +}
6749 +
6750 +static const struct file_operations dbgaufs_xigen_fop = {
6751 +       .owner          = THIS_MODULE,
6752 +       .open           = dbgaufs_xigen_open,
6753 +       .release        = dbgaufs_xi_release,
6754 +       .read           = dbgaufs_xi_read
6755 +};
6756 +
6757 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6758 +{
6759 +       int err;
6760 +
6761 +       /*
6762 +        * This function is a dynamic '__init' function actually,
6763 +        * so the tiny check for si_rwsem is unnecessary.
6764 +        */
6765 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6766 +
6767 +       err = -EIO;
6768 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6769 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6770 +                &dbgaufs_xigen_fop);
6771 +       if (sbinfo->si_dbgaufs_xigen)
6772 +               err = 0;
6773 +
6774 +       return err;
6775 +}
6776 +#else
6777 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6778 +{
6779 +       return 0;
6780 +}
6781 +#endif /* CONFIG_AUFS_EXPORT */
6782 +
6783 +/* ---------------------------------------------------------------------- */
6784 +
6785 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6786 +{
6787 +       /*
6788 +        * This function is a dynamic '__fin' function actually,
6789 +        * so the tiny check for si_rwsem is unnecessary.
6790 +        */
6791 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6792 +
6793 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6794 +       sbinfo->si_dbgaufs = NULL;
6795 +}
6796 +
6797 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6798 +{
6799 +       int err;
6800 +       char name[SysaufsSiNameLen];
6801 +
6802 +       /*
6803 +        * This function is a dynamic '__init' function actually,
6804 +        * so the tiny check for si_rwsem is unnecessary.
6805 +        */
6806 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6807 +
6808 +       err = -ENOENT;
6809 +       if (!dbgaufs) {
6810 +               AuErr1("/debug/aufs is uninitialized\n");
6811 +               goto out;
6812 +       }
6813 +
6814 +       err = -EIO;
6815 +       sysaufs_name(sbinfo, name);
6816 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6817 +       if (unlikely(!sbinfo->si_dbgaufs))
6818 +               goto out;
6819 +
6820 +       /* regardless plink/noplink option */
6821 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6822 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6823 +                &dbgaufs_plink_fop);
6824 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6825 +               goto out_dir;
6826 +
6827 +       /* regardless xino/noxino option */
6828 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6829 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6830 +                &dbgaufs_xib_fop);
6831 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6832 +               goto out_dir;
6833 +
6834 +       err = dbgaufs_xigen_init(sbinfo);
6835 +       if (!err)
6836 +               goto out; /* success */
6837 +
6838 +out_dir:
6839 +       dbgaufs_si_fin(sbinfo);
6840 +out:
6841 +       if (unlikely(err))
6842 +               pr_err("debugfs/aufs failed\n");
6843 +       return err;
6844 +}
6845 +
6846 +/* ---------------------------------------------------------------------- */
6847 +
6848 +void dbgaufs_fin(void)
6849 +{
6850 +       debugfs_remove(dbgaufs);
6851 +}
6852 +
6853 +int __init dbgaufs_init(void)
6854 +{
6855 +       int err;
6856 +
6857 +       err = -EIO;
6858 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6859 +       if (dbgaufs)
6860 +               err = 0;
6861 +       return err;
6862 +}
6863 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6864 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6865 +++ linux/fs/aufs/dbgaufs.h     2021-02-24 13:33:42.741013619 +0100
6866 @@ -0,0 +1,53 @@
6867 +/* SPDX-License-Identifier: GPL-2.0 */
6868 +/*
6869 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6870 + *
6871 + * This program, aufs is free software; you can redistribute it and/or modify
6872 + * it under the terms of the GNU General Public License as published by
6873 + * the Free Software Foundation; either version 2 of the License, or
6874 + * (at your option) any later version.
6875 + *
6876 + * This program is distributed in the hope that it will be useful,
6877 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6878 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6879 + * GNU General Public License for more details.
6880 + *
6881 + * You should have received a copy of the GNU General Public License
6882 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6883 + */
6884 +
6885 +/*
6886 + * debugfs interface
6887 + */
6888 +
6889 +#ifndef __DBGAUFS_H__
6890 +#define __DBGAUFS_H__
6891 +
6892 +#ifdef __KERNEL__
6893 +
6894 +struct super_block;
6895 +struct au_sbinfo;
6896 +struct au_branch;
6897 +
6898 +#ifdef CONFIG_DEBUG_FS
6899 +/* dbgaufs.c */
6900 +void dbgaufs_xino_del(struct au_branch *br);
6901 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6902 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6903 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6904 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6905 +void dbgaufs_fin(void);
6906 +int __init dbgaufs_init(void);
6907 +#else
6908 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6909 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6910 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6911 +          int topdown)
6912 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6913 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6914 +AuStubVoid(dbgaufs_fin, void)
6915 +AuStubInt0(__init dbgaufs_init, void)
6916 +#endif /* CONFIG_DEBUG_FS */
6917 +
6918 +#endif /* __KERNEL__ */
6919 +#endif /* __DBGAUFS_H__ */
6920 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6921 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6922 +++ linux/fs/aufs/dcsub.c       2021-02-24 13:33:42.741013619 +0100
6923 @@ -0,0 +1,225 @@
6924 +// SPDX-License-Identifier: GPL-2.0
6925 +/*
6926 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6927 + *
6928 + * This program, aufs is free software; you can redistribute it and/or modify
6929 + * it under the terms of the GNU General Public License as published by
6930 + * the Free Software Foundation; either version 2 of the License, or
6931 + * (at your option) any later version.
6932 + *
6933 + * This program is distributed in the hope that it will be useful,
6934 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6935 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6936 + * GNU General Public License for more details.
6937 + *
6938 + * You should have received a copy of the GNU General Public License
6939 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6940 + */
6941 +
6942 +/*
6943 + * sub-routines for dentry cache
6944 + */
6945 +
6946 +#include "aufs.h"
6947 +
6948 +static void au_dpage_free(struct au_dpage *dpage)
6949 +{
6950 +       int i;
6951 +       struct dentry **p;
6952 +
6953 +       p = dpage->dentries;
6954 +       for (i = 0; i < dpage->ndentry; i++)
6955 +               dput(*p++);
6956 +       free_page((unsigned long)dpage->dentries);
6957 +}
6958 +
6959 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6960 +{
6961 +       int err;
6962 +       void *p;
6963 +
6964 +       err = -ENOMEM;
6965 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6966 +       if (unlikely(!dpages->dpages))
6967 +               goto out;
6968 +
6969 +       p = (void *)__get_free_page(gfp);
6970 +       if (unlikely(!p))
6971 +               goto out_dpages;
6972 +
6973 +       dpages->dpages[0].ndentry = 0;
6974 +       dpages->dpages[0].dentries = p;
6975 +       dpages->ndpage = 1;
6976 +       return 0; /* success */
6977 +
6978 +out_dpages:
6979 +       au_kfree_try_rcu(dpages->dpages);
6980 +out:
6981 +       return err;
6982 +}
6983 +
6984 +void au_dpages_free(struct au_dcsub_pages *dpages)
6985 +{
6986 +       int i;
6987 +       struct au_dpage *p;
6988 +
6989 +       p = dpages->dpages;
6990 +       for (i = 0; i < dpages->ndpage; i++)
6991 +               au_dpage_free(p++);
6992 +       au_kfree_try_rcu(dpages->dpages);
6993 +}
6994 +
6995 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6996 +                           struct dentry *dentry, gfp_t gfp)
6997 +{
6998 +       int err, sz;
6999 +       struct au_dpage *dpage;
7000 +       void *p;
7001 +
7002 +       dpage = dpages->dpages + dpages->ndpage - 1;
7003 +       sz = PAGE_SIZE / sizeof(dentry);
7004 +       if (unlikely(dpage->ndentry >= sz)) {
7005 +               AuLabel(new dpage);
7006 +               err = -ENOMEM;
7007 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7008 +               p = au_kzrealloc(dpages->dpages, sz,
7009 +                                sz + sizeof(*dpages->dpages), gfp,
7010 +                                /*may_shrink*/0);
7011 +               if (unlikely(!p))
7012 +                       goto out;
7013 +
7014 +               dpages->dpages = p;
7015 +               dpage = dpages->dpages + dpages->ndpage;
7016 +               p = (void *)__get_free_page(gfp);
7017 +               if (unlikely(!p))
7018 +                       goto out;
7019 +
7020 +               dpage->ndentry = 0;
7021 +               dpage->dentries = p;
7022 +               dpages->ndpage++;
7023 +       }
7024 +
7025 +       AuDebugOn(au_dcount(dentry) <= 0);
7026 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7027 +       return 0; /* success */
7028 +
7029 +out:
7030 +       return err;
7031 +}
7032 +
7033 +/* todo: BAD approach */
7034 +/* copied from linux/fs/dcache.c */
7035 +enum d_walk_ret {
7036 +       D_WALK_CONTINUE,
7037 +       D_WALK_QUIT,
7038 +       D_WALK_NORETRY,
7039 +       D_WALK_SKIP,
7040 +};
7041 +
7042 +extern void d_walk(struct dentry *parent, void *data,
7043 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7044 +
7045 +struct ac_dpages_arg {
7046 +       int err;
7047 +       struct au_dcsub_pages *dpages;
7048 +       struct super_block *sb;
7049 +       au_dpages_test test;
7050 +       void *arg;
7051 +};
7052 +
7053 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7054 +{
7055 +       enum d_walk_ret ret;
7056 +       struct ac_dpages_arg *arg = _arg;
7057 +
7058 +       ret = D_WALK_CONTINUE;
7059 +       if (dentry->d_sb == arg->sb
7060 +           && !IS_ROOT(dentry)
7061 +           && au_dcount(dentry) > 0
7062 +           && au_di(dentry)
7063 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7064 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7065 +               if (unlikely(arg->err))
7066 +                       ret = D_WALK_QUIT;
7067 +       }
7068 +
7069 +       return ret;
7070 +}
7071 +
7072 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7073 +                  au_dpages_test test, void *arg)
7074 +{
7075 +       struct ac_dpages_arg args = {
7076 +               .err    = 0,
7077 +               .dpages = dpages,
7078 +               .sb     = root->d_sb,
7079 +               .test   = test,
7080 +               .arg    = arg
7081 +       };
7082 +
7083 +       d_walk(root, &args, au_call_dpages_append);
7084 +
7085 +       return args.err;
7086 +}
7087 +
7088 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7089 +                      int do_include, au_dpages_test test, void *arg)
7090 +{
7091 +       int err;
7092 +
7093 +       err = 0;
7094 +       write_seqlock(&rename_lock);
7095 +       spin_lock(&dentry->d_lock);
7096 +       if (do_include
7097 +           && au_dcount(dentry) > 0
7098 +           && (!test || test(dentry, arg)))
7099 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7100 +       spin_unlock(&dentry->d_lock);
7101 +       if (unlikely(err))
7102 +               goto out;
7103 +
7104 +       /*
7105 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7106 +        * mount
7107 +        */
7108 +       while (!IS_ROOT(dentry)) {
7109 +               dentry = dentry->d_parent; /* rename_lock is locked */
7110 +               spin_lock(&dentry->d_lock);
7111 +               if (au_dcount(dentry) > 0
7112 +                   && (!test || test(dentry, arg)))
7113 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7114 +               spin_unlock(&dentry->d_lock);
7115 +               if (unlikely(err))
7116 +                       break;
7117 +       }
7118 +
7119 +out:
7120 +       write_sequnlock(&rename_lock);
7121 +       return err;
7122 +}
7123 +
7124 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7125 +{
7126 +       return au_di(dentry) && dentry->d_sb == arg;
7127 +}
7128 +
7129 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7130 +                           struct dentry *dentry, int do_include)
7131 +{
7132 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7133 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7134 +}
7135 +
7136 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7137 +{
7138 +       struct path path[2] = {
7139 +               {
7140 +                       .dentry = d1
7141 +               },
7142 +               {
7143 +                       .dentry = d2
7144 +               }
7145 +       };
7146 +
7147 +       return path_is_under(path + 0, path + 1);
7148 +}
7149 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7150 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7151 +++ linux/fs/aufs/dcsub.h       2021-02-24 13:33:42.741013619 +0100
7152 @@ -0,0 +1,137 @@
7153 +/* SPDX-License-Identifier: GPL-2.0 */
7154 +/*
7155 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7156 + *
7157 + * This program, aufs is free software; you can redistribute it and/or modify
7158 + * it under the terms of the GNU General Public License as published by
7159 + * the Free Software Foundation; either version 2 of the License, or
7160 + * (at your option) any later version.
7161 + *
7162 + * This program is distributed in the hope that it will be useful,
7163 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7164 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7165 + * GNU General Public License for more details.
7166 + *
7167 + * You should have received a copy of the GNU General Public License
7168 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7169 + */
7170 +
7171 +/*
7172 + * sub-routines for dentry cache
7173 + */
7174 +
7175 +#ifndef __AUFS_DCSUB_H__
7176 +#define __AUFS_DCSUB_H__
7177 +
7178 +#ifdef __KERNEL__
7179 +
7180 +#include <linux/dcache.h>
7181 +#include <linux/fs.h>
7182 +
7183 +struct au_dpage {
7184 +       int ndentry;
7185 +       struct dentry **dentries;
7186 +};
7187 +
7188 +struct au_dcsub_pages {
7189 +       int ndpage;
7190 +       struct au_dpage *dpages;
7191 +};
7192 +
7193 +/* ---------------------------------------------------------------------- */
7194 +
7195 +/* dcsub.c */
7196 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7197 +void au_dpages_free(struct au_dcsub_pages *dpages);
7198 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7199 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7200 +                  au_dpages_test test, void *arg);
7201 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7202 +                      int do_include, au_dpages_test test, void *arg);
7203 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7204 +                           struct dentry *dentry, int do_include);
7205 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7206 +
7207 +/* ---------------------------------------------------------------------- */
7208 +
7209 +/*
7210 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7211 + * include/linux/dcache.h. Try them (in the future).
7212 + */
7213 +
7214 +static inline int au_d_hashed_positive(struct dentry *d)
7215 +{
7216 +       int err;
7217 +       struct inode *inode = d_inode(d);
7218 +
7219 +       err = 0;
7220 +       if (unlikely(d_unhashed(d)
7221 +                    || d_is_negative(d)
7222 +                    || !inode->i_nlink))
7223 +               err = -ENOENT;
7224 +       return err;
7225 +}
7226 +
7227 +static inline int au_d_linkable(struct dentry *d)
7228 +{
7229 +       int err;
7230 +       struct inode *inode = d_inode(d);
7231 +
7232 +       err = au_d_hashed_positive(d);
7233 +       if (err
7234 +           && d_is_positive(d)
7235 +           && (inode->i_state & I_LINKABLE))
7236 +               err = 0;
7237 +       return err;
7238 +}
7239 +
7240 +static inline int au_d_alive(struct dentry *d)
7241 +{
7242 +       int err;
7243 +       struct inode *inode;
7244 +
7245 +       err = 0;
7246 +       if (!IS_ROOT(d))
7247 +               err = au_d_hashed_positive(d);
7248 +       else {
7249 +               inode = d_inode(d);
7250 +               if (unlikely(d_unlinked(d)
7251 +                            || d_is_negative(d)
7252 +                            || !inode->i_nlink))
7253 +                       err = -ENOENT;
7254 +       }
7255 +       return err;
7256 +}
7257 +
7258 +static inline int au_alive_dir(struct dentry *d)
7259 +{
7260 +       int err;
7261 +
7262 +       err = au_d_alive(d);
7263 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7264 +               err = -ENOENT;
7265 +       return err;
7266 +}
7267 +
7268 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7269 +{
7270 +       return a->len == b->len
7271 +               && !memcmp(a->name, b->name, a->len);
7272 +}
7273 +
7274 +/*
7275 + * by the commit
7276 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7277 + *                     taking d_lock
7278 + * the type of d_lockref.count became int, but the inlined function d_count()
7279 + * still returns unsigned int.
7280 + * I don't know why. Maybe it is for every d_count() users?
7281 + * Anyway au_dcount() lives on.
7282 + */
7283 +static inline int au_dcount(struct dentry *d)
7284 +{
7285 +       return (int)d_count(d);
7286 +}
7287 +
7288 +#endif /* __KERNEL__ */
7289 +#endif /* __AUFS_DCSUB_H__ */
7290 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7291 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7292 +++ linux/fs/aufs/debug.c       2021-02-24 13:33:42.741013619 +0100
7293 @@ -0,0 +1,441 @@
7294 +// SPDX-License-Identifier: GPL-2.0
7295 +/*
7296 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7297 + *
7298 + * This program, aufs is free software; you can redistribute it and/or modify
7299 + * it under the terms of the GNU General Public License as published by
7300 + * the Free Software Foundation; either version 2 of the License, or
7301 + * (at your option) any later version.
7302 + *
7303 + * This program is distributed in the hope that it will be useful,
7304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7306 + * GNU General Public License for more details.
7307 + *
7308 + * You should have received a copy of the GNU General Public License
7309 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7310 + */
7311 +
7312 +/*
7313 + * debug print functions
7314 + */
7315 +
7316 +#include <linux/iversion.h>
7317 +#include "aufs.h"
7318 +
7319 +/* Returns 0, or -errno.  arg is in kp->arg. */
7320 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7321 +{
7322 +       int err, n;
7323 +
7324 +       err = kstrtoint(val, 0, &n);
7325 +       if (!err) {
7326 +               if (n > 0)
7327 +                       au_debug_on();
7328 +               else
7329 +                       au_debug_off();
7330 +       }
7331 +       return err;
7332 +}
7333 +
7334 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7335 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7336 +{
7337 +       atomic_t *a;
7338 +
7339 +       a = kp->arg;
7340 +       return sprintf(buffer, "%d", atomic_read(a));
7341 +}
7342 +
7343 +static struct kernel_param_ops param_ops_atomic_t = {
7344 +       .set = param_atomic_t_set,
7345 +       .get = param_atomic_t_get
7346 +       /* void (*free)(void *arg) */
7347 +};
7348 +
7349 +atomic_t aufs_debug = ATOMIC_INIT(0);
7350 +MODULE_PARM_DESC(debug, "debug print");
7351 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7352 +
7353 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7354 +char *au_plevel = KERN_DEBUG;
7355 +#define dpri(fmt, ...) do {                                    \
7356 +       if ((au_plevel                                          \
7357 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7358 +           || au_debug_test())                                 \
7359 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7360 +} while (0)
7361 +
7362 +/* ---------------------------------------------------------------------- */
7363 +
7364 +void au_dpri_whlist(struct au_nhash *whlist)
7365 +{
7366 +       unsigned long ul, n;
7367 +       struct hlist_head *head;
7368 +       struct au_vdir_wh *pos;
7369 +
7370 +       n = whlist->nh_num;
7371 +       head = whlist->nh_head;
7372 +       for (ul = 0; ul < n; ul++) {
7373 +               hlist_for_each_entry(pos, head, wh_hash)
7374 +                       dpri("b%d, %.*s, %d\n",
7375 +                            pos->wh_bindex,
7376 +                            pos->wh_str.len, pos->wh_str.name,
7377 +                            pos->wh_str.len);
7378 +               head++;
7379 +       }
7380 +}
7381 +
7382 +void au_dpri_vdir(struct au_vdir *vdir)
7383 +{
7384 +       unsigned long ul;
7385 +       union au_vdir_deblk_p p;
7386 +       unsigned char *o;
7387 +
7388 +       if (!vdir || IS_ERR(vdir)) {
7389 +               dpri("err %ld\n", PTR_ERR(vdir));
7390 +               return;
7391 +       }
7392 +
7393 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7394 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7395 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7396 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7397 +               p.deblk = vdir->vd_deblk[ul];
7398 +               o = p.deblk;
7399 +               dpri("[%lu]: %p\n", ul, o);
7400 +       }
7401 +}
7402 +
7403 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7404 +                       struct dentry *wh)
7405 +{
7406 +       char *n = NULL;
7407 +       int l = 0;
7408 +
7409 +       if (!inode || IS_ERR(inode)) {
7410 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7411 +               return -1;
7412 +       }
7413 +
7414 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7415 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7416 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7417 +       if (wh) {
7418 +               n = (void *)wh->d_name.name;
7419 +               l = wh->d_name.len;
7420 +       }
7421 +
7422 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7423 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7424 +            bindex, inode,
7425 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7426 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7427 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7428 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7429 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7430 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7431 +            inode->i_generation,
7432 +            l ? ", wh " : "", l, n);
7433 +       return 0;
7434 +}
7435 +
7436 +void au_dpri_inode(struct inode *inode)
7437 +{
7438 +       struct au_iinfo *iinfo;
7439 +       struct au_hinode *hi;
7440 +       aufs_bindex_t bindex;
7441 +       int err, hn;
7442 +
7443 +       err = do_pri_inode(-1, inode, -1, NULL);
7444 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7445 +               return;
7446 +
7447 +       iinfo = au_ii(inode);
7448 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7449 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7450 +       if (iinfo->ii_btop < 0)
7451 +               return;
7452 +       hn = 0;
7453 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7454 +               hi = au_hinode(iinfo, bindex);
7455 +               hn = !!au_hn(hi);
7456 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7457 +       }
7458 +}
7459 +
7460 +void au_dpri_dalias(struct inode *inode)
7461 +{
7462 +       struct dentry *d;
7463 +
7464 +       spin_lock(&inode->i_lock);
7465 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7466 +               au_dpri_dentry(d);
7467 +       spin_unlock(&inode->i_lock);
7468 +}
7469 +
7470 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7471 +{
7472 +       struct dentry *wh = NULL;
7473 +       int hn;
7474 +       struct inode *inode;
7475 +       struct au_iinfo *iinfo;
7476 +       struct au_hinode *hi;
7477 +
7478 +       if (!dentry || IS_ERR(dentry)) {
7479 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7480 +               return -1;
7481 +       }
7482 +       /* do not call dget_parent() here */
7483 +       /* note: access d_xxx without d_lock */
7484 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7485 +            bindex, dentry, dentry,
7486 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7487 +            au_dcount(dentry), dentry->d_flags,
7488 +            d_unhashed(dentry) ? "un" : "");
7489 +       hn = -1;
7490 +       inode = NULL;
7491 +       if (d_is_positive(dentry))
7492 +               inode = d_inode(dentry);
7493 +       if (inode
7494 +           && au_test_aufs(dentry->d_sb)
7495 +           && bindex >= 0
7496 +           && !au_is_bad_inode(inode)) {
7497 +               iinfo = au_ii(inode);
7498 +               hi = au_hinode(iinfo, bindex);
7499 +               hn = !!au_hn(hi);
7500 +               wh = hi->hi_whdentry;
7501 +       }
7502 +       do_pri_inode(bindex, inode, hn, wh);
7503 +       return 0;
7504 +}
7505 +
7506 +void au_dpri_dentry(struct dentry *dentry)
7507 +{
7508 +       struct au_dinfo *dinfo;
7509 +       aufs_bindex_t bindex;
7510 +       int err;
7511 +
7512 +       err = do_pri_dentry(-1, dentry);
7513 +       if (err || !au_test_aufs(dentry->d_sb))
7514 +               return;
7515 +
7516 +       dinfo = au_di(dentry);
7517 +       if (!dinfo)
7518 +               return;
7519 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7520 +            dinfo->di_btop, dinfo->di_bbot,
7521 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7522 +            dinfo->di_tmpfile);
7523 +       if (dinfo->di_btop < 0)
7524 +               return;
7525 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7526 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7527 +}
7528 +
7529 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7530 +{
7531 +       char a[32];
7532 +
7533 +       if (!file || IS_ERR(file)) {
7534 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7535 +               return -1;
7536 +       }
7537 +       a[0] = 0;
7538 +       if (bindex < 0
7539 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7540 +           && au_test_aufs(file->f_path.dentry->d_sb)
7541 +           && au_fi(file))
7542 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7543 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7544 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7545 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7546 +            file->f_version, file->f_pos, a);
7547 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7548 +               do_pri_dentry(bindex, file->f_path.dentry);
7549 +       return 0;
7550 +}
7551 +
7552 +void au_dpri_file(struct file *file)
7553 +{
7554 +       struct au_finfo *finfo;
7555 +       struct au_fidir *fidir;
7556 +       struct au_hfile *hfile;
7557 +       aufs_bindex_t bindex;
7558 +       int err;
7559 +
7560 +       err = do_pri_file(-1, file);
7561 +       if (err
7562 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7563 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7564 +               return;
7565 +
7566 +       finfo = au_fi(file);
7567 +       if (!finfo)
7568 +               return;
7569 +       if (finfo->fi_btop < 0)
7570 +               return;
7571 +       fidir = finfo->fi_hdir;
7572 +       if (!fidir)
7573 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7574 +       else
7575 +               for (bindex = finfo->fi_btop;
7576 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7577 +                    bindex++) {
7578 +                       hfile = fidir->fd_hfile + bindex;
7579 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7580 +               }
7581 +}
7582 +
7583 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7584 +{
7585 +       struct vfsmount *mnt;
7586 +       struct super_block *sb;
7587 +
7588 +       if (!br || IS_ERR(br))
7589 +               goto out;
7590 +       mnt = au_br_mnt(br);
7591 +       if (!mnt || IS_ERR(mnt))
7592 +               goto out;
7593 +       sb = mnt->mnt_sb;
7594 +       if (!sb || IS_ERR(sb))
7595 +               goto out;
7596 +
7597 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7598 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7599 +            "xino %d\n",
7600 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7601 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7602 +            sb->s_flags, sb->s_count,
7603 +            atomic_read(&sb->s_active),
7604 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7605 +       return 0;
7606 +
7607 +out:
7608 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7609 +       return -1;
7610 +}
7611 +
7612 +void au_dpri_sb(struct super_block *sb)
7613 +{
7614 +       struct au_sbinfo *sbinfo;
7615 +       aufs_bindex_t bindex;
7616 +       int err;
7617 +       /* to reduce stack size */
7618 +       struct {
7619 +               struct vfsmount mnt;
7620 +               struct au_branch fake;
7621 +       } *a;
7622 +
7623 +       /* this function can be called from magic sysrq */
7624 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7625 +       if (unlikely(!a)) {
7626 +               dpri("no memory\n");
7627 +               return;
7628 +       }
7629 +
7630 +       a->mnt.mnt_sb = sb;
7631 +       a->fake.br_path.mnt = &a->mnt;
7632 +       err = do_pri_br(-1, &a->fake);
7633 +       au_kfree_rcu(a);
7634 +       dpri("dev 0x%x\n", sb->s_dev);
7635 +       if (err || !au_test_aufs(sb))
7636 +               return;
7637 +
7638 +       sbinfo = au_sbi(sb);
7639 +       if (!sbinfo)
7640 +               return;
7641 +       dpri("nw %d, gen %u, kobj %d\n",
7642 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7643 +            kref_read(&sbinfo->si_kobj.kref));
7644 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7645 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7646 +}
7647 +
7648 +/* ---------------------------------------------------------------------- */
7649 +
7650 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7651 +{
7652 +       struct inode *h_inode, *inode = d_inode(dentry);
7653 +       struct dentry *h_dentry;
7654 +       aufs_bindex_t bindex, bbot, bi;
7655 +
7656 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7657 +               return;
7658 +
7659 +       bbot = au_dbbot(dentry);
7660 +       bi = au_ibbot(inode);
7661 +       if (bi < bbot)
7662 +               bbot = bi;
7663 +       bindex = au_dbtop(dentry);
7664 +       bi = au_ibtop(inode);
7665 +       if (bi > bindex)
7666 +               bindex = bi;
7667 +
7668 +       for (; bindex <= bbot; bindex++) {
7669 +               h_dentry = au_h_dptr(dentry, bindex);
7670 +               if (!h_dentry)
7671 +                       continue;
7672 +               h_inode = au_h_iptr(inode, bindex);
7673 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7674 +                       au_debug_on();
7675 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7676 +                       AuDbgDentry(dentry);
7677 +                       AuDbgInode(inode);
7678 +                       au_debug_off();
7679 +                       BUG();
7680 +               }
7681 +       }
7682 +}
7683 +
7684 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7685 +{
7686 +       int err, i, j;
7687 +       struct au_dcsub_pages dpages;
7688 +       struct au_dpage *dpage;
7689 +       struct dentry **dentries;
7690 +
7691 +       err = au_dpages_init(&dpages, GFP_NOFS);
7692 +       AuDebugOn(err);
7693 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7694 +       AuDebugOn(err);
7695 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7696 +               dpage = dpages.dpages + i;
7697 +               dentries = dpage->dentries;
7698 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7699 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7700 +       }
7701 +       au_dpages_free(&dpages);
7702 +}
7703 +
7704 +void au_dbg_verify_kthread(void)
7705 +{
7706 +       if (au_wkq_test()) {
7707 +               au_dbg_blocked();
7708 +               /*
7709 +                * It may be recursive, but udba=notify between two aufs mounts,
7710 +                * where a single ro branch is shared, is not a problem.
7711 +                */
7712 +               /* WARN_ON(1); */
7713 +       }
7714 +}
7715 +
7716 +/* ---------------------------------------------------------------------- */
7717 +
7718 +int __init au_debug_init(void)
7719 +{
7720 +       aufs_bindex_t bindex;
7721 +       struct au_vdir_destr destr;
7722 +
7723 +       bindex = -1;
7724 +       AuDebugOn(bindex >= 0);
7725 +
7726 +       destr.len = -1;
7727 +       AuDebugOn(destr.len < NAME_MAX);
7728 +
7729 +#ifdef CONFIG_4KSTACKS
7730 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7731 +#endif
7732 +
7733 +       return 0;
7734 +}
7735 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7736 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7737 +++ linux/fs/aufs/debug.h       2021-02-24 13:33:42.741013619 +0100
7738 @@ -0,0 +1,226 @@
7739 +/* SPDX-License-Identifier: GPL-2.0 */
7740 +/*
7741 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7742 + *
7743 + * This program, aufs is free software; you can redistribute it and/or modify
7744 + * it under the terms of the GNU General Public License as published by
7745 + * the Free Software Foundation; either version 2 of the License, or
7746 + * (at your option) any later version.
7747 + *
7748 + * This program is distributed in the hope that it will be useful,
7749 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7750 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7751 + * GNU General Public License for more details.
7752 + *
7753 + * You should have received a copy of the GNU General Public License
7754 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7755 + */
7756 +
7757 +/*
7758 + * debug print functions
7759 + */
7760 +
7761 +#ifndef __AUFS_DEBUG_H__
7762 +#define __AUFS_DEBUG_H__
7763 +
7764 +#ifdef __KERNEL__
7765 +
7766 +#include <linux/atomic.h>
7767 +#include <linux/module.h>
7768 +#include <linux/kallsyms.h>
7769 +#include <linux/sysrq.h>
7770 +
7771 +#ifdef CONFIG_AUFS_DEBUG
7772 +#define AuDebugOn(a)           BUG_ON(a)
7773 +
7774 +/* module parameter */
7775 +extern atomic_t aufs_debug;
7776 +static inline void au_debug_on(void)
7777 +{
7778 +       atomic_inc(&aufs_debug);
7779 +}
7780 +static inline void au_debug_off(void)
7781 +{
7782 +       atomic_dec_if_positive(&aufs_debug);
7783 +}
7784 +
7785 +static inline int au_debug_test(void)
7786 +{
7787 +       return atomic_read(&aufs_debug) > 0;
7788 +}
7789 +#else
7790 +#define AuDebugOn(a)           do {} while (0)
7791 +AuStubVoid(au_debug_on, void)
7792 +AuStubVoid(au_debug_off, void)
7793 +AuStubInt0(au_debug_test, void)
7794 +#endif /* CONFIG_AUFS_DEBUG */
7795 +
7796 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7797 +
7798 +/* ---------------------------------------------------------------------- */
7799 +
7800 +/* debug print */
7801 +
7802 +#define AuDbg(fmt, ...) do { \
7803 +       if (au_debug_test()) \
7804 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7805 +} while (0)
7806 +#define AuLabel(l)             AuDbg(#l "\n")
7807 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7808 +#define AuWarn1(fmt, ...) do { \
7809 +       static unsigned char _c; \
7810 +       if (!_c++) \
7811 +               pr_warn(fmt, ##__VA_ARGS__); \
7812 +} while (0)
7813 +
7814 +#define AuErr1(fmt, ...) do { \
7815 +       static unsigned char _c; \
7816 +       if (!_c++) \
7817 +               pr_err(fmt, ##__VA_ARGS__); \
7818 +} while (0)
7819 +
7820 +#define AuIOErr1(fmt, ...) do { \
7821 +       static unsigned char _c; \
7822 +       if (!_c++) \
7823 +               AuIOErr(fmt, ##__VA_ARGS__); \
7824 +} while (0)
7825 +
7826 +#define AuUnsupportMsg "This operation is not supported." \
7827 +                       " Please report this application to aufs-users ML."
7828 +#define AuUnsupport(fmt, ...) do { \
7829 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7830 +       dump_stack(); \
7831 +} while (0)
7832 +
7833 +#define AuTraceErr(e) do { \
7834 +       if (unlikely((e) < 0)) \
7835 +               AuDbg("err %d\n", (int)(e)); \
7836 +} while (0)
7837 +
7838 +#define AuTraceErrPtr(p) do { \
7839 +       if (IS_ERR(p)) \
7840 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7841 +} while (0)
7842 +
7843 +/* dirty macros for debug print, use with "%.*s" and caution */
7844 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7845 +
7846 +/* ---------------------------------------------------------------------- */
7847 +
7848 +struct dentry;
7849 +#ifdef CONFIG_AUFS_DEBUG
7850 +extern struct mutex au_dbg_mtx;
7851 +extern char *au_plevel;
7852 +struct au_nhash;
7853 +void au_dpri_whlist(struct au_nhash *whlist);
7854 +struct au_vdir;
7855 +void au_dpri_vdir(struct au_vdir *vdir);
7856 +struct inode;
7857 +void au_dpri_inode(struct inode *inode);
7858 +void au_dpri_dalias(struct inode *inode);
7859 +void au_dpri_dentry(struct dentry *dentry);
7860 +struct file;
7861 +void au_dpri_file(struct file *filp);
7862 +struct super_block;
7863 +void au_dpri_sb(struct super_block *sb);
7864 +
7865 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7866 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7867 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7868 +void au_dbg_verify_kthread(void);
7869 +
7870 +int __init au_debug_init(void);
7871 +
7872 +#define AuDbgWhlist(w) do { \
7873 +       mutex_lock(&au_dbg_mtx); \
7874 +       AuDbg(#w "\n"); \
7875 +       au_dpri_whlist(w); \
7876 +       mutex_unlock(&au_dbg_mtx); \
7877 +} while (0)
7878 +
7879 +#define AuDbgVdir(v) do { \
7880 +       mutex_lock(&au_dbg_mtx); \
7881 +       AuDbg(#v "\n"); \
7882 +       au_dpri_vdir(v); \
7883 +       mutex_unlock(&au_dbg_mtx); \
7884 +} while (0)
7885 +
7886 +#define AuDbgInode(i) do { \
7887 +       mutex_lock(&au_dbg_mtx); \
7888 +       AuDbg(#i "\n"); \
7889 +       au_dpri_inode(i); \
7890 +       mutex_unlock(&au_dbg_mtx); \
7891 +} while (0)
7892 +
7893 +#define AuDbgDAlias(i) do { \
7894 +       mutex_lock(&au_dbg_mtx); \
7895 +       AuDbg(#i "\n"); \
7896 +       au_dpri_dalias(i); \
7897 +       mutex_unlock(&au_dbg_mtx); \
7898 +} while (0)
7899 +
7900 +#define AuDbgDentry(d) do { \
7901 +       mutex_lock(&au_dbg_mtx); \
7902 +       AuDbg(#d "\n"); \
7903 +       au_dpri_dentry(d); \
7904 +       mutex_unlock(&au_dbg_mtx); \
7905 +} while (0)
7906 +
7907 +#define AuDbgFile(f) do { \
7908 +       mutex_lock(&au_dbg_mtx); \
7909 +       AuDbg(#f "\n"); \
7910 +       au_dpri_file(f); \
7911 +       mutex_unlock(&au_dbg_mtx); \
7912 +} while (0)
7913 +
7914 +#define AuDbgSb(sb) do { \
7915 +       mutex_lock(&au_dbg_mtx); \
7916 +       AuDbg(#sb "\n"); \
7917 +       au_dpri_sb(sb); \
7918 +       mutex_unlock(&au_dbg_mtx); \
7919 +} while (0)
7920 +
7921 +#define AuDbgSym(addr) do {                            \
7922 +       char sym[KSYM_SYMBOL_LEN];                      \
7923 +       sprint_symbol(sym, (unsigned long)addr);        \
7924 +       AuDbg("%s\n", sym);                             \
7925 +} while (0)
7926 +#else
7927 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7928 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7929 +AuStubVoid(au_dbg_verify_kthread, void)
7930 +AuStubInt0(__init au_debug_init, void)
7931 +
7932 +#define AuDbgWhlist(w)         do {} while (0)
7933 +#define AuDbgVdir(v)           do {} while (0)
7934 +#define AuDbgInode(i)          do {} while (0)
7935 +#define AuDbgDAlias(i)         do {} while (0)
7936 +#define AuDbgDentry(d)         do {} while (0)
7937 +#define AuDbgFile(f)           do {} while (0)
7938 +#define AuDbgSb(sb)            do {} while (0)
7939 +#define AuDbgSym(addr)         do {} while (0)
7940 +#endif /* CONFIG_AUFS_DEBUG */
7941 +
7942 +/* ---------------------------------------------------------------------- */
7943 +
7944 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7945 +int __init au_sysrq_init(void);
7946 +void au_sysrq_fin(void);
7947 +
7948 +#ifdef CONFIG_HW_CONSOLE
7949 +#define au_dbg_blocked() do { \
7950 +       WARN_ON(1); \
7951 +       handle_sysrq('w'); \
7952 +} while (0)
7953 +#else
7954 +AuStubVoid(au_dbg_blocked, void)
7955 +#endif
7956 +
7957 +#else
7958 +AuStubInt0(__init au_sysrq_init, void)
7959 +AuStubVoid(au_sysrq_fin, void)
7960 +AuStubVoid(au_dbg_blocked, void)
7961 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7962 +
7963 +#endif /* __KERNEL__ */
7964 +#endif /* __AUFS_DEBUG_H__ */
7965 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7966 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7967 +++ linux/fs/aufs/dentry.c      2021-02-24 13:33:42.741013619 +0100
7968 @@ -0,0 +1,1154 @@
7969 +// SPDX-License-Identifier: GPL-2.0
7970 +/*
7971 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7972 + *
7973 + * This program, aufs is free software; you can redistribute it and/or modify
7974 + * it under the terms of the GNU General Public License as published by
7975 + * the Free Software Foundation; either version 2 of the License, or
7976 + * (at your option) any later version.
7977 + *
7978 + * This program is distributed in the hope that it will be useful,
7979 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7980 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7981 + * GNU General Public License for more details.
7982 + *
7983 + * You should have received a copy of the GNU General Public License
7984 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7985 + */
7986 +
7987 +/*
7988 + * lookup and dentry operations
7989 + */
7990 +
7991 +#include <linux/iversion.h>
7992 +#include <linux/namei.h>
7993 +#include "aufs.h"
7994 +
7995 +/*
7996 + * returns positive/negative dentry, NULL or an error.
7997 + * NULL means whiteout-ed or not-found.
7998 + */
7999 +static struct dentry*
8000 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8001 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8002 +{
8003 +       struct dentry *h_dentry;
8004 +       struct inode *h_inode;
8005 +       struct au_branch *br;
8006 +       int wh_found, opq;
8007 +       unsigned char wh_able;
8008 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8009 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8010 +                                                         IGNORE_PERM);
8011 +
8012 +       wh_found = 0;
8013 +       br = au_sbr(dentry->d_sb, bindex);
8014 +       wh_able = !!au_br_whable(br->br_perm);
8015 +       if (wh_able)
8016 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8017 +       h_dentry = ERR_PTR(wh_found);
8018 +       if (!wh_found)
8019 +               goto real_lookup;
8020 +       if (unlikely(wh_found < 0))
8021 +               goto out;
8022 +
8023 +       /* We found a whiteout */
8024 +       /* au_set_dbbot(dentry, bindex); */
8025 +       au_set_dbwh(dentry, bindex);
8026 +       if (!allow_neg)
8027 +               return NULL; /* success */
8028 +
8029 +real_lookup:
8030 +       if (!ignore_perm)
8031 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8032 +       else
8033 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8034 +       if (IS_ERR(h_dentry)) {
8035 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8036 +                   && !allow_neg)
8037 +                       h_dentry = NULL;
8038 +               goto out;
8039 +       }
8040 +
8041 +       h_inode = d_inode(h_dentry);
8042 +       if (d_is_negative(h_dentry)) {
8043 +               if (!allow_neg)
8044 +                       goto out_neg;
8045 +       } else if (wh_found
8046 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8047 +               goto out_neg;
8048 +       else if (au_ftest_lkup(args->flags, DIRREN)
8049 +                /* && h_inode */
8050 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8051 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8052 +                     (unsigned long long)h_inode->i_ino);
8053 +               goto out_neg;
8054 +       }
8055 +
8056 +       if (au_dbbot(dentry) <= bindex)
8057 +               au_set_dbbot(dentry, bindex);
8058 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8059 +               au_set_dbtop(dentry, bindex);
8060 +       au_set_h_dptr(dentry, bindex, h_dentry);
8061 +
8062 +       if (!d_is_dir(h_dentry)
8063 +           || !wh_able
8064 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8065 +               goto out; /* success */
8066 +
8067 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8068 +       opq = au_diropq_test(h_dentry);
8069 +       inode_unlock_shared(h_inode);
8070 +       if (opq > 0)
8071 +               au_set_dbdiropq(dentry, bindex);
8072 +       else if (unlikely(opq < 0)) {
8073 +               au_set_h_dptr(dentry, bindex, NULL);
8074 +               h_dentry = ERR_PTR(opq);
8075 +       }
8076 +       goto out;
8077 +
8078 +out_neg:
8079 +       dput(h_dentry);
8080 +       h_dentry = NULL;
8081 +out:
8082 +       return h_dentry;
8083 +}
8084 +
8085 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8086 +{
8087 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8088 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8089 +               return -EPERM;
8090 +       return 0;
8091 +}
8092 +
8093 +/*
8094 + * returns the number of lower positive dentries,
8095 + * otherwise an error.
8096 + * can be called at unlinking with @type is zero.
8097 + */
8098 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8099 +                  unsigned int flags)
8100 +{
8101 +       int npositive, err;
8102 +       aufs_bindex_t bindex, btail, bdiropq;
8103 +       unsigned char isdir, dirperm1, dirren;
8104 +       struct au_do_lookup_args args = {
8105 +               .flags          = flags,
8106 +               .name           = &dentry->d_name
8107 +       };
8108 +       struct dentry *parent;
8109 +       struct super_block *sb;
8110 +
8111 +       sb = dentry->d_sb;
8112 +       err = au_test_shwh(sb, args.name);
8113 +       if (unlikely(err))
8114 +               goto out;
8115 +
8116 +       err = au_wh_name_alloc(&args.whname, args.name);
8117 +       if (unlikely(err))
8118 +               goto out;
8119 +
8120 +       isdir = !!d_is_dir(dentry);
8121 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8122 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8123 +       if (dirren)
8124 +               au_fset_lkup(args.flags, DIRREN);
8125 +
8126 +       npositive = 0;
8127 +       parent = dget_parent(dentry);
8128 +       btail = au_dbtaildir(parent);
8129 +       for (bindex = btop; bindex <= btail; bindex++) {
8130 +               struct dentry *h_parent, *h_dentry;
8131 +               struct inode *h_inode, *h_dir;
8132 +               struct au_branch *br;
8133 +
8134 +               h_dentry = au_h_dptr(dentry, bindex);
8135 +               if (h_dentry) {
8136 +                       if (d_is_positive(h_dentry))
8137 +                               npositive++;
8138 +                       break;
8139 +               }
8140 +               h_parent = au_h_dptr(parent, bindex);
8141 +               if (!h_parent || !d_is_dir(h_parent))
8142 +                       continue;
8143 +
8144 +               if (dirren) {
8145 +                       /* if the inum matches, then use the prepared name */
8146 +                       err = au_dr_lkup_name(&args, bindex);
8147 +                       if (unlikely(err))
8148 +                               goto out_parent;
8149 +               }
8150 +
8151 +               h_dir = d_inode(h_parent);
8152 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8153 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8154 +               inode_unlock_shared(h_dir);
8155 +               err = PTR_ERR(h_dentry);
8156 +               if (IS_ERR(h_dentry))
8157 +                       goto out_parent;
8158 +               if (h_dentry)
8159 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8160 +               if (dirperm1)
8161 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8162 +
8163 +               if (au_dbwh(dentry) == bindex)
8164 +                       break;
8165 +               if (!h_dentry)
8166 +                       continue;
8167 +               if (d_is_negative(h_dentry))
8168 +                       continue;
8169 +               h_inode = d_inode(h_dentry);
8170 +               npositive++;
8171 +               if (!args.type)
8172 +                       args.type = h_inode->i_mode & S_IFMT;
8173 +               if (args.type != S_IFDIR)
8174 +                       break;
8175 +               else if (isdir) {
8176 +                       /* the type of lower may be different */
8177 +                       bdiropq = au_dbdiropq(dentry);
8178 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8179 +                               break;
8180 +               }
8181 +               br = au_sbr(sb, bindex);
8182 +               if (dirren
8183 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8184 +                                          /*add_ent*/NULL)) {
8185 +                       /* prepare next name to lookup */
8186 +                       err = au_dr_lkup(&args, dentry, bindex);
8187 +                       if (unlikely(err))
8188 +                               goto out_parent;
8189 +               }
8190 +       }
8191 +
8192 +       if (npositive) {
8193 +               AuLabel(positive);
8194 +               au_update_dbtop(dentry);
8195 +       }
8196 +       err = npositive;
8197 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8198 +                    && au_dbtop(dentry) < 0)) {
8199 +               err = -EIO;
8200 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8201 +                       dentry, err);
8202 +       }
8203 +
8204 +out_parent:
8205 +       dput(parent);
8206 +       au_kfree_try_rcu(args.whname.name);
8207 +       if (dirren)
8208 +               au_dr_lkup_fin(&args);
8209 +out:
8210 +       return err;
8211 +}
8212 +
8213 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8214 +{
8215 +       struct dentry *dentry;
8216 +       int wkq_err;
8217 +
8218 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8219 +               dentry = vfsub_lkup_one(name, parent);
8220 +       else {
8221 +               struct vfsub_lkup_one_args args = {
8222 +                       .errp   = &dentry,
8223 +                       .name   = name,
8224 +                       .parent = parent
8225 +               };
8226 +
8227 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8228 +               if (unlikely(wkq_err))
8229 +                       dentry = ERR_PTR(wkq_err);
8230 +       }
8231 +
8232 +       return dentry;
8233 +}
8234 +
8235 +/*
8236 + * lookup @dentry on @bindex which should be negative.
8237 + */
8238 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8239 +{
8240 +       int err;
8241 +       struct dentry *parent, *h_parent, *h_dentry;
8242 +       struct au_branch *br;
8243 +
8244 +       parent = dget_parent(dentry);
8245 +       h_parent = au_h_dptr(parent, bindex);
8246 +       br = au_sbr(dentry->d_sb, bindex);
8247 +       if (wh)
8248 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8249 +       else
8250 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8251 +       err = PTR_ERR(h_dentry);
8252 +       if (IS_ERR(h_dentry))
8253 +               goto out;
8254 +       if (unlikely(d_is_positive(h_dentry))) {
8255 +               err = -EIO;
8256 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8257 +               dput(h_dentry);
8258 +               goto out;
8259 +       }
8260 +
8261 +       err = 0;
8262 +       if (bindex < au_dbtop(dentry))
8263 +               au_set_dbtop(dentry, bindex);
8264 +       if (au_dbbot(dentry) < bindex)
8265 +               au_set_dbbot(dentry, bindex);
8266 +       au_set_h_dptr(dentry, bindex, h_dentry);
8267 +
8268 +out:
8269 +       dput(parent);
8270 +       return err;
8271 +}
8272 +
8273 +/* ---------------------------------------------------------------------- */
8274 +
8275 +/* subset of struct inode */
8276 +struct au_iattr {
8277 +       unsigned long           i_ino;
8278 +       /* unsigned int         i_nlink; */
8279 +       kuid_t                  i_uid;
8280 +       kgid_t                  i_gid;
8281 +       u64                     i_version;
8282 +/*
8283 +       loff_t                  i_size;
8284 +       blkcnt_t                i_blocks;
8285 +*/
8286 +       umode_t                 i_mode;
8287 +};
8288 +
8289 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8290 +{
8291 +       ia->i_ino = h_inode->i_ino;
8292 +       /* ia->i_nlink = h_inode->i_nlink; */
8293 +       ia->i_uid = h_inode->i_uid;
8294 +       ia->i_gid = h_inode->i_gid;
8295 +       ia->i_version = inode_query_iversion(h_inode);
8296 +/*
8297 +       ia->i_size = h_inode->i_size;
8298 +       ia->i_blocks = h_inode->i_blocks;
8299 +*/
8300 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8301 +}
8302 +
8303 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8304 +{
8305 +       return ia->i_ino != h_inode->i_ino
8306 +               /* || ia->i_nlink != h_inode->i_nlink */
8307 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8308 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8309 +               || !inode_eq_iversion(h_inode, ia->i_version)
8310 +/*
8311 +               || ia->i_size != h_inode->i_size
8312 +               || ia->i_blocks != h_inode->i_blocks
8313 +*/
8314 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8315 +}
8316 +
8317 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8318 +                             struct au_branch *br)
8319 +{
8320 +       int err;
8321 +       struct au_iattr ia;
8322 +       struct inode *h_inode;
8323 +       struct dentry *h_d;
8324 +       struct super_block *h_sb;
8325 +
8326 +       err = 0;
8327 +       memset(&ia, -1, sizeof(ia));
8328 +       h_sb = h_dentry->d_sb;
8329 +       h_inode = NULL;
8330 +       if (d_is_positive(h_dentry)) {
8331 +               h_inode = d_inode(h_dentry);
8332 +               au_iattr_save(&ia, h_inode);
8333 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8334 +               /* nfs d_revalidate may return 0 for negative dentry */
8335 +               /* fuse d_revalidate always return 0 for negative dentry */
8336 +               goto out;
8337 +
8338 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8339 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8340 +       err = PTR_ERR(h_d);
8341 +       if (IS_ERR(h_d))
8342 +               goto out;
8343 +
8344 +       err = 0;
8345 +       if (unlikely(h_d != h_dentry
8346 +                    || d_inode(h_d) != h_inode
8347 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8348 +               err = au_busy_or_stale();
8349 +       dput(h_d);
8350 +
8351 +out:
8352 +       AuTraceErr(err);
8353 +       return err;
8354 +}
8355 +
8356 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8357 +               struct dentry *h_parent, struct au_branch *br)
8358 +{
8359 +       int err;
8360 +
8361 +       err = 0;
8362 +       if (udba == AuOpt_UDBA_REVAL
8363 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8364 +               IMustLock(h_dir);
8365 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8366 +       } else if (udba != AuOpt_UDBA_NONE)
8367 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8368 +
8369 +       return err;
8370 +}
8371 +
8372 +/* ---------------------------------------------------------------------- */
8373 +
8374 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8375 +{
8376 +       int err;
8377 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8378 +       struct au_hdentry tmp, *p, *q;
8379 +       struct au_dinfo *dinfo;
8380 +       struct super_block *sb;
8381 +
8382 +       DiMustWriteLock(dentry);
8383 +
8384 +       sb = dentry->d_sb;
8385 +       dinfo = au_di(dentry);
8386 +       bbot = dinfo->di_bbot;
8387 +       bwh = dinfo->di_bwh;
8388 +       bdiropq = dinfo->di_bdiropq;
8389 +       bindex = dinfo->di_btop;
8390 +       p = au_hdentry(dinfo, bindex);
8391 +       for (; bindex <= bbot; bindex++, p++) {
8392 +               if (!p->hd_dentry)
8393 +                       continue;
8394 +
8395 +               new_bindex = au_br_index(sb, p->hd_id);
8396 +               if (new_bindex == bindex)
8397 +                       continue;
8398 +
8399 +               if (dinfo->di_bwh == bindex)
8400 +                       bwh = new_bindex;
8401 +               if (dinfo->di_bdiropq == bindex)
8402 +                       bdiropq = new_bindex;
8403 +               if (new_bindex < 0) {
8404 +                       au_hdput(p);
8405 +                       p->hd_dentry = NULL;
8406 +                       continue;
8407 +               }
8408 +
8409 +               /* swap two lower dentries, and loop again */
8410 +               q = au_hdentry(dinfo, new_bindex);
8411 +               tmp = *q;
8412 +               *q = *p;
8413 +               *p = tmp;
8414 +               if (tmp.hd_dentry) {
8415 +                       bindex--;
8416 +                       p--;
8417 +               }
8418 +       }
8419 +
8420 +       dinfo->di_bwh = -1;
8421 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8422 +               dinfo->di_bwh = bwh;
8423 +
8424 +       dinfo->di_bdiropq = -1;
8425 +       if (bdiropq >= 0
8426 +           && bdiropq <= au_sbbot(sb)
8427 +           && au_sbr_whable(sb, bdiropq))
8428 +               dinfo->di_bdiropq = bdiropq;
8429 +
8430 +       err = -EIO;
8431 +       dinfo->di_btop = -1;
8432 +       dinfo->di_bbot = -1;
8433 +       bbot = au_dbbot(parent);
8434 +       bindex = 0;
8435 +       p = au_hdentry(dinfo, bindex);
8436 +       for (; bindex <= bbot; bindex++, p++)
8437 +               if (p->hd_dentry) {
8438 +                       dinfo->di_btop = bindex;
8439 +                       break;
8440 +               }
8441 +
8442 +       if (dinfo->di_btop >= 0) {
8443 +               bindex = bbot;
8444 +               p = au_hdentry(dinfo, bindex);
8445 +               for (; bindex >= 0; bindex--, p--)
8446 +                       if (p->hd_dentry) {
8447 +                               dinfo->di_bbot = bindex;
8448 +                               err = 0;
8449 +                               break;
8450 +                       }
8451 +       }
8452 +
8453 +       return err;
8454 +}
8455 +
8456 +static void au_do_hide(struct dentry *dentry)
8457 +{
8458 +       struct inode *inode;
8459 +
8460 +       if (d_really_is_positive(dentry)) {
8461 +               inode = d_inode(dentry);
8462 +               if (!d_is_dir(dentry)) {
8463 +                       if (inode->i_nlink && !d_unhashed(dentry))
8464 +                               drop_nlink(inode);
8465 +               } else {
8466 +                       clear_nlink(inode);
8467 +                       /* stop next lookup */
8468 +                       inode->i_flags |= S_DEAD;
8469 +               }
8470 +               smp_mb(); /* necessary? */
8471 +       }
8472 +       d_drop(dentry);
8473 +}
8474 +
8475 +static int au_hide_children(struct dentry *parent)
8476 +{
8477 +       int err, i, j, ndentry;
8478 +       struct au_dcsub_pages dpages;
8479 +       struct au_dpage *dpage;
8480 +       struct dentry *dentry;
8481 +
8482 +       err = au_dpages_init(&dpages, GFP_NOFS);
8483 +       if (unlikely(err))
8484 +               goto out;
8485 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8486 +       if (unlikely(err))
8487 +               goto out_dpages;
8488 +
8489 +       /* in reverse order */
8490 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8491 +               dpage = dpages.dpages + i;
8492 +               ndentry = dpage->ndentry;
8493 +               for (j = ndentry - 1; j >= 0; j--) {
8494 +                       dentry = dpage->dentries[j];
8495 +                       if (dentry != parent)
8496 +                               au_do_hide(dentry);
8497 +               }
8498 +       }
8499 +
8500 +out_dpages:
8501 +       au_dpages_free(&dpages);
8502 +out:
8503 +       return err;
8504 +}
8505 +
8506 +static void au_hide(struct dentry *dentry)
8507 +{
8508 +       int err;
8509 +
8510 +       AuDbgDentry(dentry);
8511 +       if (d_is_dir(dentry)) {
8512 +               /* shrink_dcache_parent(dentry); */
8513 +               err = au_hide_children(dentry);
8514 +               if (unlikely(err))
8515 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8516 +                               dentry, err);
8517 +       }
8518 +       au_do_hide(dentry);
8519 +}
8520 +
8521 +/*
8522 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8523 + *
8524 + * a dirty branch is added
8525 + * - on the top of layers
8526 + * - in the middle of layers
8527 + * - to the bottom of layers
8528 + *
8529 + * on the added branch there exists
8530 + * - a whiteout
8531 + * - a diropq
8532 + * - a same named entry
8533 + *   + exist
8534 + *     * negative --> positive
8535 + *     * positive --> positive
8536 + *      - type is unchanged
8537 + *      - type is changed
8538 + *   + doesn't exist
8539 + *     * negative --> negative
8540 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8541 + * - none
8542 + */
8543 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8544 +                              struct au_dinfo *tmp)
8545 +{
8546 +       int err;
8547 +       aufs_bindex_t bindex, bbot;
8548 +       struct {
8549 +               struct dentry *dentry;
8550 +               struct inode *inode;
8551 +               mode_t mode;
8552 +       } orig_h, tmp_h = {
8553 +               .dentry = NULL
8554 +       };
8555 +       struct au_hdentry *hd;
8556 +       struct inode *inode, *h_inode;
8557 +       struct dentry *h_dentry;
8558 +
8559 +       err = 0;
8560 +       AuDebugOn(dinfo->di_btop < 0);
8561 +       orig_h.mode = 0;
8562 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8563 +       orig_h.inode = NULL;
8564 +       if (d_is_positive(orig_h.dentry)) {
8565 +               orig_h.inode = d_inode(orig_h.dentry);
8566 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8567 +       }
8568 +       if (tmp->di_btop >= 0) {
8569 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8570 +               if (d_is_positive(tmp_h.dentry)) {
8571 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8572 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8573 +               }
8574 +       }
8575 +
8576 +       inode = NULL;
8577 +       if (d_really_is_positive(dentry))
8578 +               inode = d_inode(dentry);
8579 +       if (!orig_h.inode) {
8580 +               AuDbg("negative originally\n");
8581 +               if (inode) {
8582 +                       au_hide(dentry);
8583 +                       goto out;
8584 +               }
8585 +               AuDebugOn(inode);
8586 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8587 +               AuDebugOn(dinfo->di_bdiropq != -1);
8588 +
8589 +               if (!tmp_h.inode) {
8590 +                       AuDbg("negative --> negative\n");
8591 +                       /* should have only one negative lower */
8592 +                       if (tmp->di_btop >= 0
8593 +                           && tmp->di_btop < dinfo->di_btop) {
8594 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8595 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8596 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8597 +                               au_di_cp(dinfo, tmp);
8598 +                               hd = au_hdentry(tmp, tmp->di_btop);
8599 +                               au_set_h_dptr(dentry, tmp->di_btop,
8600 +                                             dget(hd->hd_dentry));
8601 +                       }
8602 +                       au_dbg_verify_dinode(dentry);
8603 +               } else {
8604 +                       AuDbg("negative --> positive\n");
8605 +                       /*
8606 +                        * similar to the behaviour of creating with bypassing
8607 +                        * aufs.
8608 +                        * unhash it in order to force an error in the
8609 +                        * succeeding create operation.
8610 +                        * we should not set S_DEAD here.
8611 +                        */
8612 +                       d_drop(dentry);
8613 +                       /* au_di_swap(tmp, dinfo); */
8614 +                       au_dbg_verify_dinode(dentry);
8615 +               }
8616 +       } else {
8617 +               AuDbg("positive originally\n");
8618 +               /* inode may be NULL */
8619 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8620 +               if (!tmp_h.inode) {
8621 +                       AuDbg("positive --> negative\n");
8622 +                       /* or bypassing aufs */
8623 +                       au_hide(dentry);
8624 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8625 +                               dinfo->di_bwh = tmp->di_bwh;
8626 +                       if (inode)
8627 +                               err = au_refresh_hinode_self(inode);
8628 +                       au_dbg_verify_dinode(dentry);
8629 +               } else if (orig_h.mode == tmp_h.mode) {
8630 +                       AuDbg("positive --> positive, same type\n");
8631 +                       if (!S_ISDIR(orig_h.mode)
8632 +                           && dinfo->di_btop > tmp->di_btop) {
8633 +                               /*
8634 +                                * similar to the behaviour of removing and
8635 +                                * creating.
8636 +                                */
8637 +                               au_hide(dentry);
8638 +                               if (inode)
8639 +                                       err = au_refresh_hinode_self(inode);
8640 +                               au_dbg_verify_dinode(dentry);
8641 +                       } else {
8642 +                               /* fill empty slots */
8643 +                               if (dinfo->di_btop > tmp->di_btop)
8644 +                                       dinfo->di_btop = tmp->di_btop;
8645 +                               if (dinfo->di_bbot < tmp->di_bbot)
8646 +                                       dinfo->di_bbot = tmp->di_bbot;
8647 +                               dinfo->di_bwh = tmp->di_bwh;
8648 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8649 +                               bbot = dinfo->di_bbot;
8650 +                               bindex = tmp->di_btop;
8651 +                               hd = au_hdentry(tmp, bindex);
8652 +                               for (; bindex <= bbot; bindex++, hd++) {
8653 +                                       if (au_h_dptr(dentry, bindex))
8654 +                                               continue;
8655 +                                       h_dentry = hd->hd_dentry;
8656 +                                       if (!h_dentry)
8657 +                                               continue;
8658 +                                       AuDebugOn(d_is_negative(h_dentry));
8659 +                                       h_inode = d_inode(h_dentry);
8660 +                                       AuDebugOn(orig_h.mode
8661 +                                                 != (h_inode->i_mode
8662 +                                                     & S_IFMT));
8663 +                                       au_set_h_dptr(dentry, bindex,
8664 +                                                     dget(h_dentry));
8665 +                               }
8666 +                               if (inode)
8667 +                                       err = au_refresh_hinode(inode, dentry);
8668 +                               au_dbg_verify_dinode(dentry);
8669 +                       }
8670 +               } else {
8671 +                       AuDbg("positive --> positive, different type\n");
8672 +                       /* similar to the behaviour of removing and creating */
8673 +                       au_hide(dentry);
8674 +                       if (inode)
8675 +                               err = au_refresh_hinode_self(inode);
8676 +                       au_dbg_verify_dinode(dentry);
8677 +               }
8678 +       }
8679 +
8680 +out:
8681 +       return err;
8682 +}
8683 +
8684 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8685 +{
8686 +       const struct dentry_operations *dop
8687 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8688 +       static const unsigned int mask
8689 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8690 +
8691 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8692 +
8693 +       if (dentry->d_op == dop)
8694 +               return;
8695 +
8696 +       AuDbg("%pd\n", dentry);
8697 +       spin_lock(&dentry->d_lock);
8698 +       if (dop == &aufs_dop)
8699 +               dentry->d_flags |= mask;
8700 +       else
8701 +               dentry->d_flags &= ~mask;
8702 +       dentry->d_op = dop;
8703 +       spin_unlock(&dentry->d_lock);
8704 +}
8705 +
8706 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8707 +{
8708 +       int err, ebrange, nbr;
8709 +       unsigned int sigen;
8710 +       struct au_dinfo *dinfo, *tmp;
8711 +       struct super_block *sb;
8712 +       struct inode *inode;
8713 +
8714 +       DiMustWriteLock(dentry);
8715 +       AuDebugOn(IS_ROOT(dentry));
8716 +       AuDebugOn(d_really_is_negative(parent));
8717 +
8718 +       sb = dentry->d_sb;
8719 +       sigen = au_sigen(sb);
8720 +       err = au_digen_test(parent, sigen);
8721 +       if (unlikely(err))
8722 +               goto out;
8723 +
8724 +       nbr = au_sbbot(sb) + 1;
8725 +       dinfo = au_di(dentry);
8726 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8727 +       if (unlikely(err))
8728 +               goto out;
8729 +       ebrange = au_dbrange_test(dentry);
8730 +       if (!ebrange)
8731 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8732 +
8733 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8734 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8735 +               if (d_really_is_positive(dentry)) {
8736 +                       inode = d_inode(dentry);
8737 +                       err = au_refresh_hinode_self(inode);
8738 +               }
8739 +               au_dbg_verify_dinode(dentry);
8740 +               if (!err)
8741 +                       goto out_dgen; /* success */
8742 +               goto out;
8743 +       }
8744 +
8745 +       /* temporary dinfo */
8746 +       AuDbgDentry(dentry);
8747 +       err = -ENOMEM;
8748 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8749 +       if (unlikely(!tmp))
8750 +               goto out;
8751 +       au_di_swap(tmp, dinfo);
8752 +       /* returns the number of positive dentries */
8753 +       /*
8754 +        * if current working dir is removed, it returns an error.
8755 +        * but the dentry is legal.
8756 +        */
8757 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8758 +       AuDbgDentry(dentry);
8759 +       au_di_swap(tmp, dinfo);
8760 +       if (err == -ENOENT)
8761 +               err = 0;
8762 +       if (err >= 0) {
8763 +               /* compare/refresh by dinfo */
8764 +               AuDbgDentry(dentry);
8765 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8766 +               au_dbg_verify_dinode(dentry);
8767 +               AuTraceErr(err);
8768 +       }
8769 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8770 +       au_rw_write_unlock(&tmp->di_rwsem);
8771 +       au_di_free(tmp);
8772 +       if (unlikely(err))
8773 +               goto out;
8774 +
8775 +out_dgen:
8776 +       au_update_digen(dentry);
8777 +out:
8778 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8779 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8780 +               AuDbgDentry(dentry);
8781 +       }
8782 +       AuTraceErr(err);
8783 +       return err;
8784 +}
8785 +
8786 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8787 +                          struct dentry *dentry, aufs_bindex_t bindex)
8788 +{
8789 +       int err, valid;
8790 +
8791 +       err = 0;
8792 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8793 +               goto out;
8794 +
8795 +       AuDbg("b%d\n", bindex);
8796 +       /*
8797 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8798 +        * due to whiteout and branch permission.
8799 +        */
8800 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8801 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8802 +       /* it may return tri-state */
8803 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8804 +
8805 +       if (unlikely(valid < 0))
8806 +               err = valid;
8807 +       else if (!valid)
8808 +               err = -EINVAL;
8809 +
8810 +out:
8811 +       AuTraceErr(err);
8812 +       return err;
8813 +}
8814 +
8815 +/* todo: remove this */
8816 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8817 +                         unsigned int flags, int do_udba, int dirren)
8818 +{
8819 +       int err;
8820 +       umode_t mode, h_mode;
8821 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8822 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8823 +       struct inode *h_inode, *h_cached_inode;
8824 +       struct dentry *h_dentry;
8825 +       struct qstr *name, *h_name;
8826 +
8827 +       err = 0;
8828 +       plus = 0;
8829 +       mode = 0;
8830 +       ibs = -1;
8831 +       ibe = -1;
8832 +       unhashed = !!d_unhashed(dentry);
8833 +       is_root = !!IS_ROOT(dentry);
8834 +       name = &dentry->d_name;
8835 +       tmpfile = au_di(dentry)->di_tmpfile;
8836 +
8837 +       /*
8838 +        * Theoretically, REVAL test should be unnecessary in case of
8839 +        * {FS,I}NOTIFY.
8840 +        * But {fs,i}notify doesn't fire some necessary events,
8841 +        *      IN_ATTRIB for atime/nlink/pageio
8842 +        * Let's do REVAL test too.
8843 +        */
8844 +       if (do_udba && inode) {
8845 +               mode = (inode->i_mode & S_IFMT);
8846 +               plus = (inode->i_nlink > 0);
8847 +               ibs = au_ibtop(inode);
8848 +               ibe = au_ibbot(inode);
8849 +       }
8850 +
8851 +       btop = au_dbtop(dentry);
8852 +       btail = btop;
8853 +       if (inode && S_ISDIR(inode->i_mode))
8854 +               btail = au_dbtaildir(dentry);
8855 +       for (bindex = btop; bindex <= btail; bindex++) {
8856 +               h_dentry = au_h_dptr(dentry, bindex);
8857 +               if (!h_dentry)
8858 +                       continue;
8859 +
8860 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8861 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8862 +               spin_lock(&h_dentry->d_lock);
8863 +               h_name = &h_dentry->d_name;
8864 +               if (unlikely(do_udba
8865 +                            && !is_root
8866 +                            && ((!h_nfs
8867 +                                 && (unhashed != !!d_unhashed(h_dentry)
8868 +                                     || (!tmpfile && !dirren
8869 +                                         && !au_qstreq(name, h_name))
8870 +                                         ))
8871 +                                || (h_nfs
8872 +                                    && !(flags & LOOKUP_OPEN)
8873 +                                    && (h_dentry->d_flags
8874 +                                        & DCACHE_NFSFS_RENAMED)))
8875 +                           )) {
8876 +                       int h_unhashed;
8877 +
8878 +                       h_unhashed = d_unhashed(h_dentry);
8879 +                       spin_unlock(&h_dentry->d_lock);
8880 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8881 +                             unhashed, h_unhashed, dentry, h_dentry);
8882 +                       goto err;
8883 +               }
8884 +               spin_unlock(&h_dentry->d_lock);
8885 +
8886 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8887 +               if (unlikely(err))
8888 +                       /* do not goto err, to keep the errno */
8889 +                       break;
8890 +
8891 +               /* todo: plink too? */
8892 +               if (!do_udba)
8893 +                       continue;
8894 +
8895 +               /* UDBA tests */
8896 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8897 +                       goto err;
8898 +
8899 +               h_inode = NULL;
8900 +               if (d_is_positive(h_dentry))
8901 +                       h_inode = d_inode(h_dentry);
8902 +               h_plus = plus;
8903 +               h_mode = mode;
8904 +               h_cached_inode = h_inode;
8905 +               if (h_inode) {
8906 +                       h_mode = (h_inode->i_mode & S_IFMT);
8907 +                       h_plus = (h_inode->i_nlink > 0);
8908 +               }
8909 +               if (inode && ibs <= bindex && bindex <= ibe)
8910 +                       h_cached_inode = au_h_iptr(inode, bindex);
8911 +
8912 +               if (!h_nfs) {
8913 +                       if (unlikely(plus != h_plus && !tmpfile))
8914 +                               goto err;
8915 +               } else {
8916 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8917 +                                    && !is_root
8918 +                                    && !IS_ROOT(h_dentry)
8919 +                                    && unhashed != d_unhashed(h_dentry)))
8920 +                               goto err;
8921 +               }
8922 +               if (unlikely(mode != h_mode
8923 +                            || h_cached_inode != h_inode))
8924 +                       goto err;
8925 +               continue;
8926 +
8927 +err:
8928 +               err = -EINVAL;
8929 +               break;
8930 +       }
8931 +
8932 +       AuTraceErr(err);
8933 +       return err;
8934 +}
8935 +
8936 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8937 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8938 +{
8939 +       int err;
8940 +       struct dentry *parent;
8941 +
8942 +       if (!au_digen_test(dentry, sigen))
8943 +               return 0;
8944 +
8945 +       parent = dget_parent(dentry);
8946 +       di_read_lock_parent(parent, AuLock_IR);
8947 +       AuDebugOn(au_digen_test(parent, sigen));
8948 +       au_dbg_verify_gen(parent, sigen);
8949 +       err = au_refresh_dentry(dentry, parent);
8950 +       di_read_unlock(parent, AuLock_IR);
8951 +       dput(parent);
8952 +       AuTraceErr(err);
8953 +       return err;
8954 +}
8955 +
8956 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8957 +{
8958 +       int err;
8959 +       struct dentry *d, *parent;
8960 +
8961 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8962 +               return simple_reval_dpath(dentry, sigen);
8963 +
8964 +       /* slow loop, keep it simple and stupid */
8965 +       /* cf: au_cpup_dirs() */
8966 +       err = 0;
8967 +       parent = NULL;
8968 +       while (au_digen_test(dentry, sigen)) {
8969 +               d = dentry;
8970 +               while (1) {
8971 +                       dput(parent);
8972 +                       parent = dget_parent(d);
8973 +                       if (!au_digen_test(parent, sigen))
8974 +                               break;
8975 +                       d = parent;
8976 +               }
8977 +
8978 +               if (d != dentry)
8979 +                       di_write_lock_child2(d);
8980 +
8981 +               /* someone might update our dentry while we were sleeping */
8982 +               if (au_digen_test(d, sigen)) {
8983 +                       /*
8984 +                        * todo: consolidate with simple_reval_dpath(),
8985 +                        * do_refresh() and au_reval_for_attr().
8986 +                        */
8987 +                       di_read_lock_parent(parent, AuLock_IR);
8988 +                       err = au_refresh_dentry(d, parent);
8989 +                       di_read_unlock(parent, AuLock_IR);
8990 +               }
8991 +
8992 +               if (d != dentry)
8993 +                       di_write_unlock(d);
8994 +               dput(parent);
8995 +               if (unlikely(err))
8996 +                       break;
8997 +       }
8998 +
8999 +       return err;
9000 +}
9001 +
9002 +/*
9003 + * if valid returns 1, otherwise 0.
9004 + */
9005 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9006 +{
9007 +       int valid, err;
9008 +       unsigned int sigen;
9009 +       unsigned char do_udba, dirren;
9010 +       struct super_block *sb;
9011 +       struct inode *inode;
9012 +
9013 +       /* todo: support rcu-walk? */
9014 +       if (flags & LOOKUP_RCU)
9015 +               return -ECHILD;
9016 +
9017 +       valid = 0;
9018 +       if (unlikely(!au_di(dentry)))
9019 +               goto out;
9020 +
9021 +       valid = 1;
9022 +       sb = dentry->d_sb;
9023 +       /*
9024 +        * todo: very ugly
9025 +        * i_mutex of parent dir may be held,
9026 +        * but we should not return 'invalid' due to busy.
9027 +        */
9028 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9029 +       if (unlikely(err)) {
9030 +               valid = err;
9031 +               AuTraceErr(err);
9032 +               goto out;
9033 +       }
9034 +       inode = NULL;
9035 +       if (d_really_is_positive(dentry))
9036 +               inode = d_inode(dentry);
9037 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9038 +               err = -EINVAL;
9039 +               AuTraceErr(err);
9040 +               goto out_dgrade;
9041 +       }
9042 +       if (unlikely(au_dbrange_test(dentry))) {
9043 +               err = -EINVAL;
9044 +               AuTraceErr(err);
9045 +               goto out_dgrade;
9046 +       }
9047 +
9048 +       sigen = au_sigen(sb);
9049 +       if (au_digen_test(dentry, sigen)) {
9050 +               AuDebugOn(IS_ROOT(dentry));
9051 +               err = au_reval_dpath(dentry, sigen);
9052 +               if (unlikely(err)) {
9053 +                       AuTraceErr(err);
9054 +                       goto out_dgrade;
9055 +               }
9056 +       }
9057 +       di_downgrade_lock(dentry, AuLock_IR);
9058 +
9059 +       err = -EINVAL;
9060 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9061 +           && inode
9062 +           && !(inode->i_state && I_LINKABLE)
9063 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9064 +               AuTraceErr(err);
9065 +               goto out_inval;
9066 +       }
9067 +
9068 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9069 +       if (do_udba && inode) {
9070 +               aufs_bindex_t btop = au_ibtop(inode);
9071 +               struct inode *h_inode;
9072 +
9073 +               if (btop >= 0) {
9074 +                       h_inode = au_h_iptr(inode, btop);
9075 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9076 +                               AuTraceErr(err);
9077 +                               goto out_inval;
9078 +                       }
9079 +               }
9080 +       }
9081 +
9082 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9083 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9084 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9085 +               err = -EIO;
9086 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9087 +                     dentry, err);
9088 +       }
9089 +       goto out_inval;
9090 +
9091 +out_dgrade:
9092 +       di_downgrade_lock(dentry, AuLock_IR);
9093 +out_inval:
9094 +       aufs_read_unlock(dentry, AuLock_IR);
9095 +       AuTraceErr(err);
9096 +       valid = !err;
9097 +out:
9098 +       if (!valid) {
9099 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9100 +               d_drop(dentry);
9101 +       }
9102 +       return valid;
9103 +}
9104 +
9105 +static void aufs_d_release(struct dentry *dentry)
9106 +{
9107 +       if (au_di(dentry)) {
9108 +               au_di_fin(dentry);
9109 +               au_hn_di_reinit(dentry);
9110 +       }
9111 +}
9112 +
9113 +const struct dentry_operations aufs_dop = {
9114 +       .d_revalidate           = aufs_d_revalidate,
9115 +       .d_weak_revalidate      = aufs_d_revalidate,
9116 +       .d_release              = aufs_d_release
9117 +};
9118 +
9119 +/* aufs_dop without d_revalidate */
9120 +const struct dentry_operations aufs_dop_noreval = {
9121 +       .d_release              = aufs_d_release
9122 +};
9123 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9124 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9125 +++ linux/fs/aufs/dentry.h      2021-02-24 13:33:42.741013619 +0100
9126 @@ -0,0 +1,268 @@
9127 +/* SPDX-License-Identifier: GPL-2.0 */
9128 +/*
9129 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9130 + *
9131 + * This program, aufs is free software; you can redistribute it and/or modify
9132 + * it under the terms of the GNU General Public License as published by
9133 + * the Free Software Foundation; either version 2 of the License, or
9134 + * (at your option) any later version.
9135 + *
9136 + * This program is distributed in the hope that it will be useful,
9137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9139 + * GNU General Public License for more details.
9140 + *
9141 + * You should have received a copy of the GNU General Public License
9142 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9143 + */
9144 +
9145 +/*
9146 + * lookup and dentry operations
9147 + */
9148 +
9149 +#ifndef __AUFS_DENTRY_H__
9150 +#define __AUFS_DENTRY_H__
9151 +
9152 +#ifdef __KERNEL__
9153 +
9154 +#include <linux/dcache.h>
9155 +#include "dirren.h"
9156 +#include "rwsem.h"
9157 +
9158 +struct au_hdentry {
9159 +       struct dentry           *hd_dentry;
9160 +       aufs_bindex_t           hd_id;
9161 +};
9162 +
9163 +struct au_dinfo {
9164 +       atomic_t                di_generation;
9165 +
9166 +       struct au_rwsem         di_rwsem;
9167 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9168 +       unsigned char           di_tmpfile; /* to allow the different name */
9169 +       struct au_hdentry       *di_hdentry;
9170 +       struct rcu_head         rcu;
9171 +} ____cacheline_aligned_in_smp;
9172 +
9173 +/* ---------------------------------------------------------------------- */
9174 +
9175 +/* flags for au_lkup_dentry() */
9176 +#define AuLkup_ALLOW_NEG       1
9177 +#define AuLkup_IGNORE_PERM     (1 << 1)
9178 +#define AuLkup_DIRREN          (1 << 2)
9179 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9180 +#define au_fset_lkup(flags, name) \
9181 +       do { (flags) |= AuLkup_##name; } while (0)
9182 +#define au_fclr_lkup(flags, name) \
9183 +       do { (flags) &= ~AuLkup_##name; } while (0)
9184 +
9185 +#ifndef CONFIG_AUFS_DIRREN
9186 +#undef AuLkup_DIRREN
9187 +#define AuLkup_DIRREN 0
9188 +#endif
9189 +
9190 +struct au_do_lookup_args {
9191 +       unsigned int            flags;
9192 +       mode_t                  type;
9193 +       struct qstr             whname, *name;
9194 +       struct au_dr_lookup     dirren;
9195 +};
9196 +
9197 +/* ---------------------------------------------------------------------- */
9198 +
9199 +/* dentry.c */
9200 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9201 +struct au_branch;
9202 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9203 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9204 +               struct dentry *h_parent, struct au_branch *br);
9205 +
9206 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9207 +                  unsigned int flags);
9208 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9209 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9210 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9211 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9212 +
9213 +/* dinfo.c */
9214 +void au_di_init_once(void *_di);
9215 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9216 +void au_di_free(struct au_dinfo *dinfo);
9217 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9218 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9219 +int au_di_init(struct dentry *dentry);
9220 +void au_di_fin(struct dentry *dentry);
9221 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9222 +
9223 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9224 +void di_read_unlock(struct dentry *d, int flags);
9225 +void di_downgrade_lock(struct dentry *d, int flags);
9226 +void di_write_lock(struct dentry *d, unsigned int lsc);
9227 +void di_write_unlock(struct dentry *d);
9228 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9229 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9230 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9231 +
9232 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9233 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9234 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9235 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9236 +
9237 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9238 +                  struct dentry *h_dentry);
9239 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9240 +int au_dbrange_test(struct dentry *dentry);
9241 +void au_update_digen(struct dentry *dentry);
9242 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9243 +void au_update_dbtop(struct dentry *dentry);
9244 +void au_update_dbbot(struct dentry *dentry);
9245 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9246 +
9247 +/* ---------------------------------------------------------------------- */
9248 +
9249 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9250 +{
9251 +       return dentry->d_fsdata;
9252 +}
9253 +
9254 +/* ---------------------------------------------------------------------- */
9255 +
9256 +/* lock subclass for dinfo */
9257 +enum {
9258 +       AuLsc_DI_CHILD,         /* child first */
9259 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9260 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9261 +       AuLsc_DI_PARENT,
9262 +       AuLsc_DI_PARENT2,
9263 +       AuLsc_DI_PARENT3,
9264 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9265 +};
9266 +
9267 +/*
9268 + * di_read_lock_child, di_write_lock_child,
9269 + * di_read_lock_child2, di_write_lock_child2,
9270 + * di_read_lock_child3, di_write_lock_child3,
9271 + * di_read_lock_parent, di_write_lock_parent,
9272 + * di_read_lock_parent2, di_write_lock_parent2,
9273 + * di_read_lock_parent3, di_write_lock_parent3,
9274 + */
9275 +#define AuReadLockFunc(name, lsc) \
9276 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9277 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9278 +
9279 +#define AuWriteLockFunc(name, lsc) \
9280 +static inline void di_write_lock_##name(struct dentry *d) \
9281 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9282 +
9283 +#define AuRWLockFuncs(name, lsc) \
9284 +       AuReadLockFunc(name, lsc) \
9285 +       AuWriteLockFunc(name, lsc)
9286 +
9287 +AuRWLockFuncs(child, CHILD);
9288 +AuRWLockFuncs(child2, CHILD2);
9289 +AuRWLockFuncs(child3, CHILD3);
9290 +AuRWLockFuncs(parent, PARENT);
9291 +AuRWLockFuncs(parent2, PARENT2);
9292 +AuRWLockFuncs(parent3, PARENT3);
9293 +
9294 +#undef AuReadLockFunc
9295 +#undef AuWriteLockFunc
9296 +#undef AuRWLockFuncs
9297 +
9298 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9299 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9300 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9301 +
9302 +/* ---------------------------------------------------------------------- */
9303 +
9304 +/* todo: memory barrier? */
9305 +static inline unsigned int au_digen(struct dentry *d)
9306 +{
9307 +       return atomic_read(&au_di(d)->di_generation);
9308 +}
9309 +
9310 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9311 +{
9312 +       hdentry->hd_dentry = NULL;
9313 +}
9314 +
9315 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9316 +                                           aufs_bindex_t bindex)
9317 +{
9318 +       return di->di_hdentry + bindex;
9319 +}
9320 +
9321 +static inline void au_hdput(struct au_hdentry *hd)
9322 +{
9323 +       if (hd)
9324 +               dput(hd->hd_dentry);
9325 +}
9326 +
9327 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9328 +{
9329 +       DiMustAnyLock(dentry);
9330 +       return au_di(dentry)->di_btop;
9331 +}
9332 +
9333 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9334 +{
9335 +       DiMustAnyLock(dentry);
9336 +       return au_di(dentry)->di_bbot;
9337 +}
9338 +
9339 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9340 +{
9341 +       DiMustAnyLock(dentry);
9342 +       return au_di(dentry)->di_bwh;
9343 +}
9344 +
9345 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9346 +{
9347 +       DiMustAnyLock(dentry);
9348 +       return au_di(dentry)->di_bdiropq;
9349 +}
9350 +
9351 +/* todo: hard/soft set? */
9352 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9353 +{
9354 +       DiMustWriteLock(dentry);
9355 +       au_di(dentry)->di_btop = bindex;
9356 +}
9357 +
9358 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9359 +{
9360 +       DiMustWriteLock(dentry);
9361 +       au_di(dentry)->di_bbot = bindex;
9362 +}
9363 +
9364 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9365 +{
9366 +       DiMustWriteLock(dentry);
9367 +       /* dbwh can be outside of btop - bbot range */
9368 +       au_di(dentry)->di_bwh = bindex;
9369 +}
9370 +
9371 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9372 +{
9373 +       DiMustWriteLock(dentry);
9374 +       au_di(dentry)->di_bdiropq = bindex;
9375 +}
9376 +
9377 +/* ---------------------------------------------------------------------- */
9378 +
9379 +#ifdef CONFIG_AUFS_HNOTIFY
9380 +static inline void au_digen_dec(struct dentry *d)
9381 +{
9382 +       atomic_dec(&au_di(d)->di_generation);
9383 +}
9384 +
9385 +static inline void au_hn_di_reinit(struct dentry *dentry)
9386 +{
9387 +       dentry->d_fsdata = NULL;
9388 +}
9389 +#else
9390 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9391 +#endif /* CONFIG_AUFS_HNOTIFY */
9392 +
9393 +#endif /* __KERNEL__ */
9394 +#endif /* __AUFS_DENTRY_H__ */
9395 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9396 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9397 +++ linux/fs/aufs/dinfo.c       2021-02-24 13:33:42.741013619 +0100
9398 @@ -0,0 +1,554 @@
9399 +// SPDX-License-Identifier: GPL-2.0
9400 +/*
9401 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9402 + *
9403 + * This program, aufs is free software; you can redistribute it and/or modify
9404 + * it under the terms of the GNU General Public License as published by
9405 + * the Free Software Foundation; either version 2 of the License, or
9406 + * (at your option) any later version.
9407 + *
9408 + * This program is distributed in the hope that it will be useful,
9409 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9410 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9411 + * GNU General Public License for more details.
9412 + *
9413 + * You should have received a copy of the GNU General Public License
9414 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9415 + */
9416 +
9417 +/*
9418 + * dentry private data
9419 + */
9420 +
9421 +#include "aufs.h"
9422 +
9423 +void au_di_init_once(void *_dinfo)
9424 +{
9425 +       struct au_dinfo *dinfo = _dinfo;
9426 +
9427 +       au_rw_init(&dinfo->di_rwsem);
9428 +}
9429 +
9430 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9431 +{
9432 +       struct au_dinfo *dinfo;
9433 +       int nbr, i;
9434 +
9435 +       dinfo = au_cache_alloc_dinfo();
9436 +       if (unlikely(!dinfo))
9437 +               goto out;
9438 +
9439 +       nbr = au_sbbot(sb) + 1;
9440 +       if (nbr <= 0)
9441 +               nbr = 1;
9442 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9443 +       if (dinfo->di_hdentry) {
9444 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9445 +               dinfo->di_btop = -1;
9446 +               dinfo->di_bbot = -1;
9447 +               dinfo->di_bwh = -1;
9448 +               dinfo->di_bdiropq = -1;
9449 +               dinfo->di_tmpfile = 0;
9450 +               for (i = 0; i < nbr; i++)
9451 +                       dinfo->di_hdentry[i].hd_id = -1;
9452 +               goto out;
9453 +       }
9454 +
9455 +       au_cache_free_dinfo(dinfo);
9456 +       dinfo = NULL;
9457 +
9458 +out:
9459 +       return dinfo;
9460 +}
9461 +
9462 +void au_di_free(struct au_dinfo *dinfo)
9463 +{
9464 +       struct au_hdentry *p;
9465 +       aufs_bindex_t bbot, bindex;
9466 +
9467 +       /* dentry may not be revalidated */
9468 +       bindex = dinfo->di_btop;
9469 +       if (bindex >= 0) {
9470 +               bbot = dinfo->di_bbot;
9471 +               p = au_hdentry(dinfo, bindex);
9472 +               while (bindex++ <= bbot)
9473 +                       au_hdput(p++);
9474 +       }
9475 +       au_kfree_try_rcu(dinfo->di_hdentry);
9476 +       au_cache_free_dinfo(dinfo);
9477 +}
9478 +
9479 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9480 +{
9481 +       struct au_hdentry *p;
9482 +       aufs_bindex_t bi;
9483 +
9484 +       AuRwMustWriteLock(&a->di_rwsem);
9485 +       AuRwMustWriteLock(&b->di_rwsem);
9486 +
9487 +#define DiSwap(v, name)                                \
9488 +       do {                                    \
9489 +               v = a->di_##name;               \
9490 +               a->di_##name = b->di_##name;    \
9491 +               b->di_##name = v;               \
9492 +       } while (0)
9493 +
9494 +       DiSwap(p, hdentry);
9495 +       DiSwap(bi, btop);
9496 +       DiSwap(bi, bbot);
9497 +       DiSwap(bi, bwh);
9498 +       DiSwap(bi, bdiropq);
9499 +       /* smp_mb(); */
9500 +
9501 +#undef DiSwap
9502 +}
9503 +
9504 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9505 +{
9506 +       AuRwMustWriteLock(&dst->di_rwsem);
9507 +       AuRwMustWriteLock(&src->di_rwsem);
9508 +
9509 +       dst->di_btop = src->di_btop;
9510 +       dst->di_bbot = src->di_bbot;
9511 +       dst->di_bwh = src->di_bwh;
9512 +       dst->di_bdiropq = src->di_bdiropq;
9513 +       /* smp_mb(); */
9514 +}
9515 +
9516 +int au_di_init(struct dentry *dentry)
9517 +{
9518 +       int err;
9519 +       struct super_block *sb;
9520 +       struct au_dinfo *dinfo;
9521 +
9522 +       err = 0;
9523 +       sb = dentry->d_sb;
9524 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9525 +       if (dinfo) {
9526 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9527 +               /* smp_mb(); */ /* atomic_set */
9528 +               dentry->d_fsdata = dinfo;
9529 +       } else
9530 +               err = -ENOMEM;
9531 +
9532 +       return err;
9533 +}
9534 +
9535 +void au_di_fin(struct dentry *dentry)
9536 +{
9537 +       struct au_dinfo *dinfo;
9538 +
9539 +       dinfo = au_di(dentry);
9540 +       AuRwDestroy(&dinfo->di_rwsem);
9541 +       au_di_free(dinfo);
9542 +}
9543 +
9544 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9545 +{
9546 +       int err, sz;
9547 +       struct au_hdentry *hdp;
9548 +
9549 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9550 +
9551 +       err = -ENOMEM;
9552 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9553 +       if (!sz)
9554 +               sz = sizeof(*hdp);
9555 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9556 +                          may_shrink);
9557 +       if (hdp) {
9558 +               dinfo->di_hdentry = hdp;
9559 +               err = 0;
9560 +       }
9561 +
9562 +       return err;
9563 +}
9564 +
9565 +/* ---------------------------------------------------------------------- */
9566 +
9567 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9568 +{
9569 +       switch (lsc) {
9570 +       case AuLsc_DI_CHILD:
9571 +               ii_write_lock_child(inode);
9572 +               break;
9573 +       case AuLsc_DI_CHILD2:
9574 +               ii_write_lock_child2(inode);
9575 +               break;
9576 +       case AuLsc_DI_CHILD3:
9577 +               ii_write_lock_child3(inode);
9578 +               break;
9579 +       case AuLsc_DI_PARENT:
9580 +               ii_write_lock_parent(inode);
9581 +               break;
9582 +       case AuLsc_DI_PARENT2:
9583 +               ii_write_lock_parent2(inode);
9584 +               break;
9585 +       case AuLsc_DI_PARENT3:
9586 +               ii_write_lock_parent3(inode);
9587 +               break;
9588 +       default:
9589 +               BUG();
9590 +       }
9591 +}
9592 +
9593 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9594 +{
9595 +       switch (lsc) {
9596 +       case AuLsc_DI_CHILD:
9597 +               ii_read_lock_child(inode);
9598 +               break;
9599 +       case AuLsc_DI_CHILD2:
9600 +               ii_read_lock_child2(inode);
9601 +               break;
9602 +       case AuLsc_DI_CHILD3:
9603 +               ii_read_lock_child3(inode);
9604 +               break;
9605 +       case AuLsc_DI_PARENT:
9606 +               ii_read_lock_parent(inode);
9607 +               break;
9608 +       case AuLsc_DI_PARENT2:
9609 +               ii_read_lock_parent2(inode);
9610 +               break;
9611 +       case AuLsc_DI_PARENT3:
9612 +               ii_read_lock_parent3(inode);
9613 +               break;
9614 +       default:
9615 +               BUG();
9616 +       }
9617 +}
9618 +
9619 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9620 +{
9621 +       struct inode *inode;
9622 +
9623 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9624 +       if (d_really_is_positive(d)) {
9625 +               inode = d_inode(d);
9626 +               if (au_ftest_lock(flags, IW))
9627 +                       do_ii_write_lock(inode, lsc);
9628 +               else if (au_ftest_lock(flags, IR))
9629 +                       do_ii_read_lock(inode, lsc);
9630 +       }
9631 +}
9632 +
9633 +void di_read_unlock(struct dentry *d, int flags)
9634 +{
9635 +       struct inode *inode;
9636 +
9637 +       if (d_really_is_positive(d)) {
9638 +               inode = d_inode(d);
9639 +               if (au_ftest_lock(flags, IW)) {
9640 +                       au_dbg_verify_dinode(d);
9641 +                       ii_write_unlock(inode);
9642 +               } else if (au_ftest_lock(flags, IR)) {
9643 +                       au_dbg_verify_dinode(d);
9644 +                       ii_read_unlock(inode);
9645 +               }
9646 +       }
9647 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9648 +}
9649 +
9650 +void di_downgrade_lock(struct dentry *d, int flags)
9651 +{
9652 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9653 +               ii_downgrade_lock(d_inode(d));
9654 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9655 +}
9656 +
9657 +void di_write_lock(struct dentry *d, unsigned int lsc)
9658 +{
9659 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9660 +       if (d_really_is_positive(d))
9661 +               do_ii_write_lock(d_inode(d), lsc);
9662 +}
9663 +
9664 +void di_write_unlock(struct dentry *d)
9665 +{
9666 +       au_dbg_verify_dinode(d);
9667 +       if (d_really_is_positive(d))
9668 +               ii_write_unlock(d_inode(d));
9669 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9670 +}
9671 +
9672 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9673 +{
9674 +       AuDebugOn(d1 == d2
9675 +                 || d_inode(d1) == d_inode(d2)
9676 +                 || d1->d_sb != d2->d_sb);
9677 +
9678 +       if ((isdir && au_test_subdir(d1, d2))
9679 +           || d1 < d2) {
9680 +               di_write_lock_child(d1);
9681 +               di_write_lock_child2(d2);
9682 +       } else {
9683 +               di_write_lock_child(d2);
9684 +               di_write_lock_child2(d1);
9685 +       }
9686 +}
9687 +
9688 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9689 +{
9690 +       AuDebugOn(d1 == d2
9691 +                 || d_inode(d1) == d_inode(d2)
9692 +                 || d1->d_sb != d2->d_sb);
9693 +
9694 +       if ((isdir && au_test_subdir(d1, d2))
9695 +           || d1 < d2) {
9696 +               di_write_lock_parent(d1);
9697 +               di_write_lock_parent2(d2);
9698 +       } else {
9699 +               di_write_lock_parent(d2);
9700 +               di_write_lock_parent2(d1);
9701 +       }
9702 +}
9703 +
9704 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9705 +{
9706 +       di_write_unlock(d1);
9707 +       if (d_inode(d1) == d_inode(d2))
9708 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9709 +       else
9710 +               di_write_unlock(d2);
9711 +}
9712 +
9713 +/* ---------------------------------------------------------------------- */
9714 +
9715 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9716 +{
9717 +       struct dentry *d;
9718 +
9719 +       DiMustAnyLock(dentry);
9720 +
9721 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9722 +               return NULL;
9723 +       AuDebugOn(bindex < 0);
9724 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9725 +       AuDebugOn(d && au_dcount(d) <= 0);
9726 +       return d;
9727 +}
9728 +
9729 +/*
9730 + * extended version of au_h_dptr().
9731 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9732 + * error.
9733 + */
9734 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9735 +{
9736 +       struct dentry *h_dentry;
9737 +       struct inode *inode, *h_inode;
9738 +
9739 +       AuDebugOn(d_really_is_negative(dentry));
9740 +
9741 +       h_dentry = NULL;
9742 +       if (au_dbtop(dentry) <= bindex
9743 +           && bindex <= au_dbbot(dentry))
9744 +               h_dentry = au_h_dptr(dentry, bindex);
9745 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9746 +               dget(h_dentry);
9747 +               goto out; /* success */
9748 +       }
9749 +
9750 +       inode = d_inode(dentry);
9751 +       AuDebugOn(bindex < au_ibtop(inode));
9752 +       AuDebugOn(au_ibbot(inode) < bindex);
9753 +       h_inode = au_h_iptr(inode, bindex);
9754 +       h_dentry = d_find_alias(h_inode);
9755 +       if (h_dentry) {
9756 +               if (!IS_ERR(h_dentry)) {
9757 +                       if (!au_d_linkable(h_dentry))
9758 +                               goto out; /* success */
9759 +                       dput(h_dentry);
9760 +               } else
9761 +                       goto out;
9762 +       }
9763 +
9764 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9765 +               h_dentry = au_plink_lkup(inode, bindex);
9766 +               AuDebugOn(!h_dentry);
9767 +               if (!IS_ERR(h_dentry)) {
9768 +                       if (!au_d_hashed_positive(h_dentry))
9769 +                               goto out; /* success */
9770 +                       dput(h_dentry);
9771 +                       h_dentry = NULL;
9772 +               }
9773 +       }
9774 +
9775 +out:
9776 +       AuDbgDentry(h_dentry);
9777 +       return h_dentry;
9778 +}
9779 +
9780 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9781 +{
9782 +       aufs_bindex_t bbot, bwh;
9783 +
9784 +       bbot = au_dbbot(dentry);
9785 +       if (0 <= bbot) {
9786 +               bwh = au_dbwh(dentry);
9787 +               if (!bwh)
9788 +                       return bwh;
9789 +               if (0 < bwh && bwh < bbot)
9790 +                       return bwh - 1;
9791 +       }
9792 +       return bbot;
9793 +}
9794 +
9795 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9796 +{
9797 +       aufs_bindex_t bbot, bopq;
9798 +
9799 +       bbot = au_dbtail(dentry);
9800 +       if (0 <= bbot) {
9801 +               bopq = au_dbdiropq(dentry);
9802 +               if (0 <= bopq && bopq < bbot)
9803 +                       bbot = bopq;
9804 +       }
9805 +       return bbot;
9806 +}
9807 +
9808 +/* ---------------------------------------------------------------------- */
9809 +
9810 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9811 +                  struct dentry *h_dentry)
9812 +{
9813 +       struct au_dinfo *dinfo;
9814 +       struct au_hdentry *hd;
9815 +       struct au_branch *br;
9816 +
9817 +       DiMustWriteLock(dentry);
9818 +
9819 +       dinfo = au_di(dentry);
9820 +       hd = au_hdentry(dinfo, bindex);
9821 +       au_hdput(hd);
9822 +       hd->hd_dentry = h_dentry;
9823 +       if (h_dentry) {
9824 +               br = au_sbr(dentry->d_sb, bindex);
9825 +               hd->hd_id = br->br_id;
9826 +       }
9827 +}
9828 +
9829 +int au_dbrange_test(struct dentry *dentry)
9830 +{
9831 +       int err;
9832 +       aufs_bindex_t btop, bbot;
9833 +
9834 +       err = 0;
9835 +       btop = au_dbtop(dentry);
9836 +       bbot = au_dbbot(dentry);
9837 +       if (btop >= 0)
9838 +               AuDebugOn(bbot < 0 && btop > bbot);
9839 +       else {
9840 +               err = -EIO;
9841 +               AuDebugOn(bbot >= 0);
9842 +       }
9843 +
9844 +       return err;
9845 +}
9846 +
9847 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9848 +{
9849 +       int err;
9850 +
9851 +       err = 0;
9852 +       if (unlikely(au_digen(dentry) != sigen
9853 +                    || au_iigen_test(d_inode(dentry), sigen)))
9854 +               err = -EIO;
9855 +
9856 +       return err;
9857 +}
9858 +
9859 +void au_update_digen(struct dentry *dentry)
9860 +{
9861 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9862 +       /* smp_mb(); */ /* atomic_set */
9863 +}
9864 +
9865 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9866 +{
9867 +       struct au_dinfo *dinfo;
9868 +       struct dentry *h_d;
9869 +       struct au_hdentry *hdp;
9870 +       aufs_bindex_t bindex, bbot;
9871 +
9872 +       DiMustWriteLock(dentry);
9873 +
9874 +       dinfo = au_di(dentry);
9875 +       if (!dinfo || dinfo->di_btop < 0)
9876 +               return;
9877 +
9878 +       if (do_put_zero) {
9879 +               bbot = dinfo->di_bbot;
9880 +               bindex = dinfo->di_btop;
9881 +               hdp = au_hdentry(dinfo, bindex);
9882 +               for (; bindex <= bbot; bindex++, hdp++) {
9883 +                       h_d = hdp->hd_dentry;
9884 +                       if (h_d && d_is_negative(h_d))
9885 +                               au_set_h_dptr(dentry, bindex, NULL);
9886 +               }
9887 +       }
9888 +
9889 +       dinfo->di_btop = 0;
9890 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9891 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9892 +               if (hdp->hd_dentry)
9893 +                       break;
9894 +       if (dinfo->di_btop > dinfo->di_bbot) {
9895 +               dinfo->di_btop = -1;
9896 +               dinfo->di_bbot = -1;
9897 +               return;
9898 +       }
9899 +
9900 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9901 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9902 +               if (hdp->hd_dentry)
9903 +                       break;
9904 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9905 +}
9906 +
9907 +void au_update_dbtop(struct dentry *dentry)
9908 +{
9909 +       aufs_bindex_t bindex, bbot;
9910 +       struct dentry *h_dentry;
9911 +
9912 +       bbot = au_dbbot(dentry);
9913 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9914 +               h_dentry = au_h_dptr(dentry, bindex);
9915 +               if (!h_dentry)
9916 +                       continue;
9917 +               if (d_is_positive(h_dentry)) {
9918 +                       au_set_dbtop(dentry, bindex);
9919 +                       return;
9920 +               }
9921 +               au_set_h_dptr(dentry, bindex, NULL);
9922 +       }
9923 +}
9924 +
9925 +void au_update_dbbot(struct dentry *dentry)
9926 +{
9927 +       aufs_bindex_t bindex, btop;
9928 +       struct dentry *h_dentry;
9929 +
9930 +       btop = au_dbtop(dentry);
9931 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9932 +               h_dentry = au_h_dptr(dentry, bindex);
9933 +               if (!h_dentry)
9934 +                       continue;
9935 +               if (d_is_positive(h_dentry)) {
9936 +                       au_set_dbbot(dentry, bindex);
9937 +                       return;
9938 +               }
9939 +               au_set_h_dptr(dentry, bindex, NULL);
9940 +       }
9941 +}
9942 +
9943 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9944 +{
9945 +       aufs_bindex_t bindex, bbot;
9946 +
9947 +       bbot = au_dbbot(dentry);
9948 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9949 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9950 +                       return bindex;
9951 +       return -1;
9952 +}
9953 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9954 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9955 +++ linux/fs/aufs/dir.c 2021-02-24 13:33:42.741013619 +0100
9956 @@ -0,0 +1,763 @@
9957 +// SPDX-License-Identifier: GPL-2.0
9958 +/*
9959 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9960 + *
9961 + * This program, aufs is free software; you can redistribute it and/or modify
9962 + * it under the terms of the GNU General Public License as published by
9963 + * the Free Software Foundation; either version 2 of the License, or
9964 + * (at your option) any later version.
9965 + *
9966 + * This program is distributed in the hope that it will be useful,
9967 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9968 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9969 + * GNU General Public License for more details.
9970 + *
9971 + * You should have received a copy of the GNU General Public License
9972 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9973 + */
9974 +
9975 +/*
9976 + * directory operations
9977 + */
9978 +
9979 +#include <linux/fs_stack.h>
9980 +#include <linux/iversion.h>
9981 +#include "aufs.h"
9982 +
9983 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9984 +{
9985 +       unsigned int nlink;
9986 +
9987 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9988 +
9989 +       nlink = dir->i_nlink;
9990 +       nlink += h_dir->i_nlink - 2;
9991 +       if (h_dir->i_nlink < 2)
9992 +               nlink += 2;
9993 +       smp_mb(); /* for i_nlink */
9994 +       /* 0 can happen in revaliding */
9995 +       set_nlink(dir, nlink);
9996 +}
9997 +
9998 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9999 +{
10000 +       unsigned int nlink;
10001 +
10002 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10003 +
10004 +       nlink = dir->i_nlink;
10005 +       nlink -= h_dir->i_nlink - 2;
10006 +       if (h_dir->i_nlink < 2)
10007 +               nlink -= 2;
10008 +       smp_mb(); /* for i_nlink */
10009 +       /* nlink == 0 means the branch-fs is broken */
10010 +       set_nlink(dir, nlink);
10011 +}
10012 +
10013 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10014 +{
10015 +       loff_t sz;
10016 +       aufs_bindex_t bindex, bbot;
10017 +       struct file *h_file;
10018 +       struct dentry *h_dentry;
10019 +
10020 +       sz = 0;
10021 +       if (file) {
10022 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10023 +
10024 +               bbot = au_fbbot_dir(file);
10025 +               for (bindex = au_fbtop(file);
10026 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10027 +                    bindex++) {
10028 +                       h_file = au_hf_dir(file, bindex);
10029 +                       if (h_file && file_inode(h_file))
10030 +                               sz += vfsub_f_size_read(h_file);
10031 +               }
10032 +       } else {
10033 +               AuDebugOn(!dentry);
10034 +               AuDebugOn(!d_is_dir(dentry));
10035 +
10036 +               bbot = au_dbtaildir(dentry);
10037 +               for (bindex = au_dbtop(dentry);
10038 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10039 +                    bindex++) {
10040 +                       h_dentry = au_h_dptr(dentry, bindex);
10041 +                       if (h_dentry && d_is_positive(h_dentry))
10042 +                               sz += i_size_read(d_inode(h_dentry));
10043 +               }
10044 +       }
10045 +       if (sz < KMALLOC_MAX_SIZE)
10046 +               sz = roundup_pow_of_two(sz);
10047 +       if (sz > KMALLOC_MAX_SIZE)
10048 +               sz = KMALLOC_MAX_SIZE;
10049 +       else if (sz < NAME_MAX) {
10050 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10051 +               sz = AUFS_RDBLK_DEF;
10052 +       }
10053 +       return sz;
10054 +}
10055 +
10056 +struct au_dir_ts_arg {
10057 +       struct dentry *dentry;
10058 +       aufs_bindex_t brid;
10059 +};
10060 +
10061 +static void au_do_dir_ts(void *arg)
10062 +{
10063 +       struct au_dir_ts_arg *a = arg;
10064 +       struct au_dtime dt;
10065 +       struct path h_path;
10066 +       struct inode *dir, *h_dir;
10067 +       struct super_block *sb;
10068 +       struct au_branch *br;
10069 +       struct au_hinode *hdir;
10070 +       int err;
10071 +       aufs_bindex_t btop, bindex;
10072 +
10073 +       sb = a->dentry->d_sb;
10074 +       if (d_really_is_negative(a->dentry))
10075 +               goto out;
10076 +       /* no dir->i_mutex lock */
10077 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10078 +
10079 +       dir = d_inode(a->dentry);
10080 +       btop = au_ibtop(dir);
10081 +       bindex = au_br_index(sb, a->brid);
10082 +       if (bindex < btop)
10083 +               goto out_unlock;
10084 +
10085 +       br = au_sbr(sb, bindex);
10086 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10087 +       if (!h_path.dentry)
10088 +               goto out_unlock;
10089 +       h_path.mnt = au_br_mnt(br);
10090 +       au_dtime_store(&dt, a->dentry, &h_path);
10091 +
10092 +       br = au_sbr(sb, btop);
10093 +       if (!au_br_writable(br->br_perm))
10094 +               goto out_unlock;
10095 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10096 +       h_path.mnt = au_br_mnt(br);
10097 +       err = vfsub_mnt_want_write(h_path.mnt);
10098 +       if (err)
10099 +               goto out_unlock;
10100 +       hdir = au_hi(dir, btop);
10101 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10102 +       h_dir = au_h_iptr(dir, btop);
10103 +       if (h_dir->i_nlink
10104 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10105 +               dt.dt_h_path = h_path;
10106 +               au_dtime_revert(&dt);
10107 +       }
10108 +       au_hn_inode_unlock(hdir);
10109 +       vfsub_mnt_drop_write(h_path.mnt);
10110 +       au_cpup_attr_timesizes(dir);
10111 +
10112 +out_unlock:
10113 +       aufs_read_unlock(a->dentry, AuLock_DW);
10114 +out:
10115 +       dput(a->dentry);
10116 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10117 +       au_kfree_try_rcu(arg);
10118 +}
10119 +
10120 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10121 +{
10122 +       int perm, wkq_err;
10123 +       aufs_bindex_t btop;
10124 +       struct au_dir_ts_arg *arg;
10125 +       struct dentry *dentry;
10126 +       struct super_block *sb;
10127 +
10128 +       IMustLock(dir);
10129 +
10130 +       dentry = d_find_any_alias(dir);
10131 +       AuDebugOn(!dentry);
10132 +       sb = dentry->d_sb;
10133 +       btop = au_ibtop(dir);
10134 +       if (btop == bindex) {
10135 +               au_cpup_attr_timesizes(dir);
10136 +               goto out;
10137 +       }
10138 +
10139 +       perm = au_sbr_perm(sb, btop);
10140 +       if (!au_br_writable(perm))
10141 +               goto out;
10142 +
10143 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10144 +       if (!arg)
10145 +               goto out;
10146 +
10147 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10148 +       arg->brid = au_sbr_id(sb, bindex);
10149 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10150 +       if (unlikely(wkq_err)) {
10151 +               pr_err("wkq %d\n", wkq_err);
10152 +               dput(dentry);
10153 +               au_kfree_try_rcu(arg);
10154 +       }
10155 +
10156 +out:
10157 +       dput(dentry);
10158 +}
10159 +
10160 +/* ---------------------------------------------------------------------- */
10161 +
10162 +static int reopen_dir(struct file *file)
10163 +{
10164 +       int err;
10165 +       unsigned int flags;
10166 +       aufs_bindex_t bindex, btail, btop;
10167 +       struct dentry *dentry, *h_dentry;
10168 +       struct file *h_file;
10169 +
10170 +       /* open all lower dirs */
10171 +       dentry = file->f_path.dentry;
10172 +       btop = au_dbtop(dentry);
10173 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10174 +               au_set_h_fptr(file, bindex, NULL);
10175 +       au_set_fbtop(file, btop);
10176 +
10177 +       btail = au_dbtaildir(dentry);
10178 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10179 +               au_set_h_fptr(file, bindex, NULL);
10180 +       au_set_fbbot_dir(file, btail);
10181 +
10182 +       flags = vfsub_file_flags(file);
10183 +       for (bindex = btop; bindex <= btail; bindex++) {
10184 +               h_dentry = au_h_dptr(dentry, bindex);
10185 +               if (!h_dentry)
10186 +                       continue;
10187 +               h_file = au_hf_dir(file, bindex);
10188 +               if (h_file)
10189 +                       continue;
10190 +
10191 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10192 +               err = PTR_ERR(h_file);
10193 +               if (IS_ERR(h_file))
10194 +                       goto out; /* close all? */
10195 +               au_set_h_fptr(file, bindex, h_file);
10196 +       }
10197 +       au_update_figen(file);
10198 +       /* todo: necessary? */
10199 +       /* file->f_ra = h_file->f_ra; */
10200 +       err = 0;
10201 +
10202 +out:
10203 +       return err;
10204 +}
10205 +
10206 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10207 +{
10208 +       int err;
10209 +       aufs_bindex_t bindex, btail;
10210 +       struct dentry *dentry, *h_dentry;
10211 +       struct vfsmount *mnt;
10212 +
10213 +       FiMustWriteLock(file);
10214 +       AuDebugOn(h_file);
10215 +
10216 +       err = 0;
10217 +       mnt = file->f_path.mnt;
10218 +       dentry = file->f_path.dentry;
10219 +       file->f_version = inode_query_iversion(d_inode(dentry));
10220 +       bindex = au_dbtop(dentry);
10221 +       au_set_fbtop(file, bindex);
10222 +       btail = au_dbtaildir(dentry);
10223 +       au_set_fbbot_dir(file, btail);
10224 +       for (; !err && bindex <= btail; bindex++) {
10225 +               h_dentry = au_h_dptr(dentry, bindex);
10226 +               if (!h_dentry)
10227 +                       continue;
10228 +
10229 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10230 +               if (unlikely(err))
10231 +                       break;
10232 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10233 +               if (IS_ERR(h_file)) {
10234 +                       err = PTR_ERR(h_file);
10235 +                       break;
10236 +               }
10237 +               au_set_h_fptr(file, bindex, h_file);
10238 +       }
10239 +       au_update_figen(file);
10240 +       /* todo: necessary? */
10241 +       /* file->f_ra = h_file->f_ra; */
10242 +       if (!err)
10243 +               return 0; /* success */
10244 +
10245 +       /* close all */
10246 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10247 +               au_set_h_fptr(file, bindex, NULL);
10248 +       au_set_fbtop(file, -1);
10249 +       au_set_fbbot_dir(file, -1);
10250 +
10251 +       return err;
10252 +}
10253 +
10254 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10255 +                        struct file *file)
10256 +{
10257 +       int err;
10258 +       struct super_block *sb;
10259 +       struct au_fidir *fidir;
10260 +
10261 +       err = -ENOMEM;
10262 +       sb = file->f_path.dentry->d_sb;
10263 +       si_read_lock(sb, AuLock_FLUSH);
10264 +       fidir = au_fidir_alloc(sb);
10265 +       if (fidir) {
10266 +               struct au_do_open_args args = {
10267 +                       .open   = do_open_dir,
10268 +                       .fidir  = fidir
10269 +               };
10270 +               err = au_do_open(file, &args);
10271 +               if (unlikely(err))
10272 +                       au_kfree_rcu(fidir);
10273 +       }
10274 +       si_read_unlock(sb);
10275 +       return err;
10276 +}
10277 +
10278 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10279 +                           struct file *file)
10280 +{
10281 +       struct au_vdir *vdir_cache;
10282 +       struct au_finfo *finfo;
10283 +       struct au_fidir *fidir;
10284 +       struct au_hfile *hf;
10285 +       aufs_bindex_t bindex, bbot;
10286 +
10287 +       finfo = au_fi(file);
10288 +       fidir = finfo->fi_hdir;
10289 +       if (fidir) {
10290 +               au_hbl_del(&finfo->fi_hlist,
10291 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10292 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10293 +               if (vdir_cache)
10294 +                       au_vdir_free(vdir_cache);
10295 +
10296 +               bindex = finfo->fi_btop;
10297 +               if (bindex >= 0) {
10298 +                       hf = fidir->fd_hfile + bindex;
10299 +                       /*
10300 +                        * calls fput() instead of filp_close(),
10301 +                        * since no dnotify or lock for the lower file.
10302 +                        */
10303 +                       bbot = fidir->fd_bbot;
10304 +                       for (; bindex <= bbot; bindex++, hf++)
10305 +                               if (hf->hf_file)
10306 +                                       au_hfput(hf, /*execed*/0);
10307 +               }
10308 +               au_kfree_rcu(fidir);
10309 +               finfo->fi_hdir = NULL;
10310 +       }
10311 +       au_finfo_fin(file);
10312 +       return 0;
10313 +}
10314 +
10315 +/* ---------------------------------------------------------------------- */
10316 +
10317 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10318 +{
10319 +       int err;
10320 +       aufs_bindex_t bindex, bbot;
10321 +       struct file *h_file;
10322 +
10323 +       err = 0;
10324 +       bbot = au_fbbot_dir(file);
10325 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10326 +               h_file = au_hf_dir(file, bindex);
10327 +               if (h_file)
10328 +                       err = vfsub_flush(h_file, id);
10329 +       }
10330 +       return err;
10331 +}
10332 +
10333 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10334 +{
10335 +       return au_do_flush(file, id, au_do_flush_dir);
10336 +}
10337 +
10338 +/* ---------------------------------------------------------------------- */
10339 +
10340 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10341 +{
10342 +       int err;
10343 +       aufs_bindex_t bbot, bindex;
10344 +       struct inode *inode;
10345 +       struct super_block *sb;
10346 +
10347 +       err = 0;
10348 +       sb = dentry->d_sb;
10349 +       inode = d_inode(dentry);
10350 +       IMustLock(inode);
10351 +       bbot = au_dbbot(dentry);
10352 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10353 +               struct path h_path;
10354 +
10355 +               if (au_test_ro(sb, bindex, inode))
10356 +                       continue;
10357 +               h_path.dentry = au_h_dptr(dentry, bindex);
10358 +               if (!h_path.dentry)
10359 +                       continue;
10360 +
10361 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10362 +               err = vfsub_fsync(NULL, &h_path, datasync);
10363 +       }
10364 +
10365 +       return err;
10366 +}
10367 +
10368 +static int au_do_fsync_dir(struct file *file, int datasync)
10369 +{
10370 +       int err;
10371 +       aufs_bindex_t bbot, bindex;
10372 +       struct file *h_file;
10373 +       struct super_block *sb;
10374 +       struct inode *inode;
10375 +
10376 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10377 +       if (unlikely(err))
10378 +               goto out;
10379 +
10380 +       inode = file_inode(file);
10381 +       sb = inode->i_sb;
10382 +       bbot = au_fbbot_dir(file);
10383 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10384 +               h_file = au_hf_dir(file, bindex);
10385 +               if (!h_file || au_test_ro(sb, bindex, inode))
10386 +                       continue;
10387 +
10388 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10389 +       }
10390 +
10391 +out:
10392 +       return err;
10393 +}
10394 +
10395 +/*
10396 + * @file may be NULL
10397 + */
10398 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10399 +                         int datasync)
10400 +{
10401 +       int err;
10402 +       struct dentry *dentry;
10403 +       struct inode *inode;
10404 +       struct super_block *sb;
10405 +
10406 +       err = 0;
10407 +       dentry = file->f_path.dentry;
10408 +       inode = d_inode(dentry);
10409 +       inode_lock(inode);
10410 +       sb = dentry->d_sb;
10411 +       si_noflush_read_lock(sb);
10412 +       if (file)
10413 +               err = au_do_fsync_dir(file, datasync);
10414 +       else {
10415 +               di_write_lock_child(dentry);
10416 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10417 +       }
10418 +       au_cpup_attr_timesizes(inode);
10419 +       di_write_unlock(dentry);
10420 +       if (file)
10421 +               fi_write_unlock(file);
10422 +
10423 +       si_read_unlock(sb);
10424 +       inode_unlock(inode);
10425 +       return err;
10426 +}
10427 +
10428 +/* ---------------------------------------------------------------------- */
10429 +
10430 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10431 +{
10432 +       int err;
10433 +       struct dentry *dentry;
10434 +       struct inode *inode, *h_inode;
10435 +       struct super_block *sb;
10436 +
10437 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10438 +
10439 +       dentry = file->f_path.dentry;
10440 +       inode = d_inode(dentry);
10441 +       IMustLock(inode);
10442 +
10443 +       sb = dentry->d_sb;
10444 +       si_read_lock(sb, AuLock_FLUSH);
10445 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10446 +       if (unlikely(err))
10447 +               goto out;
10448 +       err = au_alive_dir(dentry);
10449 +       if (!err)
10450 +               err = au_vdir_init(file);
10451 +       di_downgrade_lock(dentry, AuLock_IR);
10452 +       if (unlikely(err))
10453 +               goto out_unlock;
10454 +
10455 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10456 +       if (!au_test_nfsd()) {
10457 +               err = au_vdir_fill_de(file, ctx);
10458 +               fsstack_copy_attr_atime(inode, h_inode);
10459 +       } else {
10460 +               /*
10461 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10462 +                * encode_fh() and others.
10463 +                */
10464 +               atomic_inc(&h_inode->i_count);
10465 +               di_read_unlock(dentry, AuLock_IR);
10466 +               si_read_unlock(sb);
10467 +               err = au_vdir_fill_de(file, ctx);
10468 +               fsstack_copy_attr_atime(inode, h_inode);
10469 +               fi_write_unlock(file);
10470 +               iput(h_inode);
10471 +
10472 +               AuTraceErr(err);
10473 +               return err;
10474 +       }
10475 +
10476 +out_unlock:
10477 +       di_read_unlock(dentry, AuLock_IR);
10478 +       fi_write_unlock(file);
10479 +out:
10480 +       si_read_unlock(sb);
10481 +       return err;
10482 +}
10483 +
10484 +/* ---------------------------------------------------------------------- */
10485 +
10486 +#define AuTestEmpty_WHONLY     1
10487 +#define AuTestEmpty_CALLED     (1 << 1)
10488 +#define AuTestEmpty_SHWH       (1 << 2)
10489 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10490 +#define au_fset_testempty(flags, name) \
10491 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10492 +#define au_fclr_testempty(flags, name) \
10493 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10494 +
10495 +#ifndef CONFIG_AUFS_SHWH
10496 +#undef AuTestEmpty_SHWH
10497 +#define AuTestEmpty_SHWH       0
10498 +#endif
10499 +
10500 +struct test_empty_arg {
10501 +       struct dir_context ctx;
10502 +       struct au_nhash *whlist;
10503 +       unsigned int flags;
10504 +       int err;
10505 +       aufs_bindex_t bindex;
10506 +};
10507 +
10508 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10509 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10510 +                        unsigned int d_type)
10511 +{
10512 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10513 +                                                 ctx);
10514 +       char *name = (void *)__name;
10515 +
10516 +       arg->err = 0;
10517 +       au_fset_testempty(arg->flags, CALLED);
10518 +       /* smp_mb(); */
10519 +       if (name[0] == '.'
10520 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10521 +               goto out; /* success */
10522 +
10523 +       if (namelen <= AUFS_WH_PFX_LEN
10524 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10525 +               if (au_ftest_testempty(arg->flags, WHONLY)
10526 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10527 +                       arg->err = -ENOTEMPTY;
10528 +               goto out;
10529 +       }
10530 +
10531 +       name += AUFS_WH_PFX_LEN;
10532 +       namelen -= AUFS_WH_PFX_LEN;
10533 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10534 +               arg->err = au_nhash_append_wh
10535 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10536 +                        au_ftest_testempty(arg->flags, SHWH));
10537 +
10538 +out:
10539 +       /* smp_mb(); */
10540 +       AuTraceErr(arg->err);
10541 +       return arg->err;
10542 +}
10543 +
10544 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10545 +{
10546 +       int err;
10547 +       struct file *h_file;
10548 +       struct au_branch *br;
10549 +
10550 +       h_file = au_h_open(dentry, arg->bindex,
10551 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10552 +                          /*file*/NULL, /*force_wr*/0);
10553 +       err = PTR_ERR(h_file);
10554 +       if (IS_ERR(h_file))
10555 +               goto out;
10556 +
10557 +       err = 0;
10558 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10559 +           && !file_inode(h_file)->i_nlink)
10560 +               goto out_put;
10561 +
10562 +       do {
10563 +               arg->err = 0;
10564 +               au_fclr_testempty(arg->flags, CALLED);
10565 +               /* smp_mb(); */
10566 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10567 +               if (err >= 0)
10568 +                       err = arg->err;
10569 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10570 +
10571 +out_put:
10572 +       fput(h_file);
10573 +       br = au_sbr(dentry->d_sb, arg->bindex);
10574 +       au_lcnt_dec(&br->br_nfiles);
10575 +out:
10576 +       return err;
10577 +}
10578 +
10579 +struct do_test_empty_args {
10580 +       int *errp;
10581 +       struct dentry *dentry;
10582 +       struct test_empty_arg *arg;
10583 +};
10584 +
10585 +static void call_do_test_empty(void *args)
10586 +{
10587 +       struct do_test_empty_args *a = args;
10588 +       *a->errp = do_test_empty(a->dentry, a->arg);
10589 +}
10590 +
10591 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10592 +{
10593 +       int err, wkq_err;
10594 +       struct dentry *h_dentry;
10595 +       struct inode *h_inode;
10596 +
10597 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10598 +       h_inode = d_inode(h_dentry);
10599 +       /* todo: i_mode changes anytime? */
10600 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10601 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10602 +       inode_unlock_shared(h_inode);
10603 +       if (!err)
10604 +               err = do_test_empty(dentry, arg);
10605 +       else {
10606 +               struct do_test_empty_args args = {
10607 +                       .errp   = &err,
10608 +                       .dentry = dentry,
10609 +                       .arg    = arg
10610 +               };
10611 +               unsigned int flags = arg->flags;
10612 +
10613 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10614 +               if (unlikely(wkq_err))
10615 +                       err = wkq_err;
10616 +               arg->flags = flags;
10617 +       }
10618 +
10619 +       return err;
10620 +}
10621 +
10622 +int au_test_empty_lower(struct dentry *dentry)
10623 +{
10624 +       int err;
10625 +       unsigned int rdhash;
10626 +       aufs_bindex_t bindex, btop, btail;
10627 +       struct au_nhash whlist;
10628 +       struct test_empty_arg arg = {
10629 +               .ctx = {
10630 +                       .actor = test_empty_cb
10631 +               }
10632 +       };
10633 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10634 +
10635 +       SiMustAnyLock(dentry->d_sb);
10636 +
10637 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10638 +       if (!rdhash)
10639 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10640 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10641 +       if (unlikely(err))
10642 +               goto out;
10643 +
10644 +       arg.flags = 0;
10645 +       arg.whlist = &whlist;
10646 +       btop = au_dbtop(dentry);
10647 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10648 +               au_fset_testempty(arg.flags, SHWH);
10649 +       test_empty = do_test_empty;
10650 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10651 +               test_empty = sio_test_empty;
10652 +       arg.bindex = btop;
10653 +       err = test_empty(dentry, &arg);
10654 +       if (unlikely(err))
10655 +               goto out_whlist;
10656 +
10657 +       au_fset_testempty(arg.flags, WHONLY);
10658 +       btail = au_dbtaildir(dentry);
10659 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10660 +               struct dentry *h_dentry;
10661 +
10662 +               h_dentry = au_h_dptr(dentry, bindex);
10663 +               if (h_dentry && d_is_positive(h_dentry)) {
10664 +                       arg.bindex = bindex;
10665 +                       err = test_empty(dentry, &arg);
10666 +               }
10667 +       }
10668 +
10669 +out_whlist:
10670 +       au_nhash_wh_free(&whlist);
10671 +out:
10672 +       return err;
10673 +}
10674 +
10675 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10676 +{
10677 +       int err;
10678 +       struct test_empty_arg arg = {
10679 +               .ctx = {
10680 +                       .actor = test_empty_cb
10681 +               }
10682 +       };
10683 +       aufs_bindex_t bindex, btail;
10684 +
10685 +       err = 0;
10686 +       arg.whlist = whlist;
10687 +       arg.flags = AuTestEmpty_WHONLY;
10688 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10689 +               au_fset_testempty(arg.flags, SHWH);
10690 +       btail = au_dbtaildir(dentry);
10691 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10692 +               struct dentry *h_dentry;
10693 +
10694 +               h_dentry = au_h_dptr(dentry, bindex);
10695 +               if (h_dentry && d_is_positive(h_dentry)) {
10696 +                       arg.bindex = bindex;
10697 +                       err = sio_test_empty(dentry, &arg);
10698 +               }
10699 +       }
10700 +
10701 +       return err;
10702 +}
10703 +
10704 +/* ---------------------------------------------------------------------- */
10705 +
10706 +const struct file_operations aufs_dir_fop = {
10707 +       .owner          = THIS_MODULE,
10708 +       .llseek         = default_llseek,
10709 +       .read           = generic_read_dir,
10710 +       .iterate_shared = aufs_iterate_shared,
10711 +       .unlocked_ioctl = aufs_ioctl_dir,
10712 +#ifdef CONFIG_COMPAT
10713 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10714 +#endif
10715 +       .open           = aufs_open_dir,
10716 +       .release        = aufs_release_dir,
10717 +       .flush          = aufs_flush_dir,
10718 +       .fsync          = aufs_fsync_dir
10719 +};
10720 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10721 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10722 +++ linux/fs/aufs/dir.h 2021-02-24 13:33:42.741013619 +0100
10723 @@ -0,0 +1,134 @@
10724 +/* SPDX-License-Identifier: GPL-2.0 */
10725 +/*
10726 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10727 + *
10728 + * This program, aufs is free software; you can redistribute it and/or modify
10729 + * it under the terms of the GNU General Public License as published by
10730 + * the Free Software Foundation; either version 2 of the License, or
10731 + * (at your option) any later version.
10732 + *
10733 + * This program is distributed in the hope that it will be useful,
10734 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10735 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10736 + * GNU General Public License for more details.
10737 + *
10738 + * You should have received a copy of the GNU General Public License
10739 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10740 + */
10741 +
10742 +/*
10743 + * directory operations
10744 + */
10745 +
10746 +#ifndef __AUFS_DIR_H__
10747 +#define __AUFS_DIR_H__
10748 +
10749 +#ifdef __KERNEL__
10750 +
10751 +#include <linux/fs.h>
10752 +
10753 +/* ---------------------------------------------------------------------- */
10754 +
10755 +/* need to be faster and smaller */
10756 +
10757 +struct au_nhash {
10758 +       unsigned int            nh_num;
10759 +       struct hlist_head       *nh_head;
10760 +};
10761 +
10762 +struct au_vdir_destr {
10763 +       unsigned char   len;
10764 +       unsigned char   name[];
10765 +} __packed;
10766 +
10767 +struct au_vdir_dehstr {
10768 +       struct hlist_node       hash;
10769 +       struct au_vdir_destr    *str;
10770 +       struct rcu_head         rcu;
10771 +} ____cacheline_aligned_in_smp;
10772 +
10773 +struct au_vdir_de {
10774 +       ino_t                   de_ino;
10775 +       unsigned char           de_type;
10776 +       /* caution: packed */
10777 +       struct au_vdir_destr    de_str;
10778 +} __packed;
10779 +
10780 +struct au_vdir_wh {
10781 +       struct hlist_node       wh_hash;
10782 +#ifdef CONFIG_AUFS_SHWH
10783 +       ino_t                   wh_ino;
10784 +       aufs_bindex_t           wh_bindex;
10785 +       unsigned char           wh_type;
10786 +#else
10787 +       aufs_bindex_t           wh_bindex;
10788 +#endif
10789 +       /* caution: packed */
10790 +       struct au_vdir_destr    wh_str;
10791 +} __packed;
10792 +
10793 +union au_vdir_deblk_p {
10794 +       unsigned char           *deblk;
10795 +       struct au_vdir_de       *de;
10796 +};
10797 +
10798 +struct au_vdir {
10799 +       unsigned char   **vd_deblk;
10800 +       unsigned long   vd_nblk;
10801 +       struct {
10802 +               unsigned long           ul;
10803 +               union au_vdir_deblk_p   p;
10804 +       } vd_last;
10805 +
10806 +       u64             vd_version;
10807 +       unsigned int    vd_deblk_sz;
10808 +       unsigned long   vd_jiffy;
10809 +       struct rcu_head rcu;
10810 +} ____cacheline_aligned_in_smp;
10811 +
10812 +/* ---------------------------------------------------------------------- */
10813 +
10814 +/* dir.c */
10815 +extern const struct file_operations aufs_dir_fop;
10816 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10817 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10818 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10819 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10820 +int au_test_empty_lower(struct dentry *dentry);
10821 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10822 +
10823 +/* vdir.c */
10824 +unsigned int au_rdhash_est(loff_t sz);
10825 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10826 +void au_nhash_wh_free(struct au_nhash *whlist);
10827 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10828 +                           int limit);
10829 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10830 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10831 +                      unsigned int d_type, aufs_bindex_t bindex,
10832 +                      unsigned char shwh);
10833 +void au_vdir_free(struct au_vdir *vdir);
10834 +int au_vdir_init(struct file *file);
10835 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10836 +
10837 +/* ioctl.c */
10838 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10839 +
10840 +#ifdef CONFIG_AUFS_RDU
10841 +/* rdu.c */
10842 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10843 +#ifdef CONFIG_COMPAT
10844 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10845 +                        unsigned long arg);
10846 +#endif
10847 +#else
10848 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10849 +       unsigned int cmd, unsigned long arg)
10850 +#ifdef CONFIG_COMPAT
10851 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10852 +       unsigned int cmd, unsigned long arg)
10853 +#endif
10854 +#endif
10855 +
10856 +#endif /* __KERNEL__ */
10857 +#endif /* __AUFS_DIR_H__ */
10858 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10859 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10860 +++ linux/fs/aufs/dirren.c      2021-02-24 13:33:42.741013619 +0100
10861 @@ -0,0 +1,1316 @@
10862 +// SPDX-License-Identifier: GPL-2.0
10863 +/*
10864 + * Copyright (C) 2017-2020 Junjiro R. Okajima
10865 + *
10866 + * This program, aufs is free software; you can redistribute it and/or modify
10867 + * it under the terms of the GNU General Public License as published by
10868 + * the Free Software Foundation; either version 2 of the License, or
10869 + * (at your option) any later version.
10870 + *
10871 + * This program is distributed in the hope that it will be useful,
10872 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10873 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10874 + * GNU General Public License for more details.
10875 + *
10876 + * You should have received a copy of the GNU General Public License
10877 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10878 + */
10879 +
10880 +/*
10881 + * special handling in renaming a directory
10882 + * in order to support looking-up the before-renamed name on the lower readonly
10883 + * branches
10884 + */
10885 +
10886 +#include <linux/byteorder/generic.h>
10887 +#include "aufs.h"
10888 +
10889 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10890 +{
10891 +       int idx;
10892 +
10893 +       idx = au_dr_ihash(ent->dr_h_ino);
10894 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10895 +}
10896 +
10897 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10898 +{
10899 +       int ret, i;
10900 +       struct hlist_bl_head *hbl;
10901 +
10902 +       ret = 1;
10903 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10904 +               hbl = dr->dr_h_ino + i;
10905 +               hlist_bl_lock(hbl);
10906 +               ret &= hlist_bl_empty(hbl);
10907 +               hlist_bl_unlock(hbl);
10908 +       }
10909 +
10910 +       return ret;
10911 +}
10912 +
10913 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10914 +{
10915 +       struct au_dr_hino *found, *ent;
10916 +       struct hlist_bl_head *hbl;
10917 +       struct hlist_bl_node *pos;
10918 +       int idx;
10919 +
10920 +       found = NULL;
10921 +       idx = au_dr_ihash(ino);
10922 +       hbl = dr->dr_h_ino + idx;
10923 +       hlist_bl_lock(hbl);
10924 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10925 +               if (ent->dr_h_ino == ino) {
10926 +                       found = ent;
10927 +                       break;
10928 +               }
10929 +       hlist_bl_unlock(hbl);
10930 +
10931 +       return found;
10932 +}
10933 +
10934 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10935 +                       struct au_dr_hino *add_ent)
10936 +{
10937 +       int found, idx;
10938 +       struct hlist_bl_head *hbl;
10939 +       struct hlist_bl_node *pos;
10940 +       struct au_dr_hino *ent;
10941 +
10942 +       found = 0;
10943 +       idx = au_dr_ihash(ino);
10944 +       hbl = dr->dr_h_ino + idx;
10945 +#if 0 /* debug print */
10946 +       {
10947 +               struct hlist_bl_node *tmp;
10948 +
10949 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10950 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10951 +       }
10952 +#endif
10953 +       hlist_bl_lock(hbl);
10954 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10955 +               if (ent->dr_h_ino == ino) {
10956 +                       found = 1;
10957 +                       break;
10958 +               }
10959 +       if (!found && add_ent)
10960 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10961 +       hlist_bl_unlock(hbl);
10962 +
10963 +       if (!found && add_ent)
10964 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10965 +
10966 +       return found;
10967 +}
10968 +
10969 +void au_dr_hino_free(struct au_dr_br *dr)
10970 +{
10971 +       int i;
10972 +       struct hlist_bl_head *hbl;
10973 +       struct hlist_bl_node *pos, *tmp;
10974 +       struct au_dr_hino *ent;
10975 +
10976 +       /* SiMustWriteLock(sb); */
10977 +
10978 +       for (i = 0; i < AuDirren_NHASH; i++) {
10979 +               hbl = dr->dr_h_ino + i;
10980 +               /* no spinlock since sbinfo must be write-locked */
10981 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10982 +                       au_kfree_rcu(ent);
10983 +               INIT_HLIST_BL_HEAD(hbl);
10984 +       }
10985 +}
10986 +
10987 +/* returns the number of inodes or an error */
10988 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10989 +                           struct file *hinofile)
10990 +{
10991 +       int err, i;
10992 +       ssize_t ssz;
10993 +       loff_t pos, oldsize;
10994 +       __be64 u64;
10995 +       struct inode *hinoinode;
10996 +       struct hlist_bl_head *hbl;
10997 +       struct hlist_bl_node *n1, *n2;
10998 +       struct au_dr_hino *ent;
10999 +
11000 +       SiMustWriteLock(sb);
11001 +       AuDebugOn(!au_br_writable(br->br_perm));
11002 +
11003 +       hinoinode = file_inode(hinofile);
11004 +       oldsize = i_size_read(hinoinode);
11005 +
11006 +       err = 0;
11007 +       pos = 0;
11008 +       hbl = br->br_dirren.dr_h_ino;
11009 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11010 +               /* no bit-lock since sbinfo must be write-locked */
11011 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11012 +                       AuDbg("hi%llu, %pD2\n",
11013 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11014 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11015 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11016 +                       if (ssz == sizeof(u64))
11017 +                               continue;
11018 +
11019 +                       /* write error */
11020 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11021 +                       err = -ENOSPC;
11022 +                       if (ssz < 0)
11023 +                               err = ssz;
11024 +                       break;
11025 +               }
11026 +       }
11027 +       /* regardless the error */
11028 +       if (pos < oldsize) {
11029 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11030 +               AuTraceErr(err);
11031 +       }
11032 +
11033 +       AuTraceErr(err);
11034 +       return err;
11035 +}
11036 +
11037 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11038 +{
11039 +       int err, hidx;
11040 +       ssize_t ssz;
11041 +       size_t sz, n;
11042 +       loff_t pos;
11043 +       uint64_t u64;
11044 +       struct au_dr_hino *ent;
11045 +       struct inode *hinoinode;
11046 +       struct hlist_bl_head *hbl;
11047 +
11048 +       err = 0;
11049 +       pos = 0;
11050 +       hbl = dr->dr_h_ino;
11051 +       hinoinode = file_inode(hinofile);
11052 +       sz = i_size_read(hinoinode);
11053 +       AuDebugOn(sz % sizeof(u64));
11054 +       n = sz / sizeof(u64);
11055 +       while (n--) {
11056 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11057 +               if (unlikely(ssz != sizeof(u64))) {
11058 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11059 +                       err = -EINVAL;
11060 +                       if (ssz < 0)
11061 +                               err = ssz;
11062 +                       goto out_free;
11063 +               }
11064 +
11065 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11066 +               if (!ent) {
11067 +                       err = -ENOMEM;
11068 +                       AuTraceErr(err);
11069 +                       goto out_free;
11070 +               }
11071 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11072 +               AuDbg("hi%llu, %pD2\n",
11073 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11074 +               hidx = au_dr_ihash(ent->dr_h_ino);
11075 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11076 +       }
11077 +       goto out; /* success */
11078 +
11079 +out_free:
11080 +       au_dr_hino_free(dr);
11081 +out:
11082 +       AuTraceErr(err);
11083 +       return err;
11084 +}
11085 +
11086 +/*
11087 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11088 + * @path is a switch to distinguish load and store.
11089 + */
11090 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11091 +                     struct au_branch *br, const struct path *path)
11092 +{
11093 +       int err, flags;
11094 +       unsigned char load, suspend;
11095 +       struct file *hinofile;
11096 +       struct au_hinode *hdir;
11097 +       struct inode *dir, *delegated;
11098 +       struct path hinopath;
11099 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11100 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11101 +
11102 +       AuDebugOn(bindex < 0 && !br);
11103 +       AuDebugOn(bindex >= 0 && br);
11104 +
11105 +       err = -EINVAL;
11106 +       suspend = !br;
11107 +       if (suspend)
11108 +               br = au_sbr(sb, bindex);
11109 +       load = !!path;
11110 +       if (!load) {
11111 +               path = &br->br_path;
11112 +               AuDebugOn(!au_br_writable(br->br_perm));
11113 +               if (unlikely(!au_br_writable(br->br_perm)))
11114 +                       goto out;
11115 +       }
11116 +
11117 +       hdir = NULL;
11118 +       if (suspend) {
11119 +               dir = d_inode(sb->s_root);
11120 +               hdir = au_hinode(au_ii(dir), bindex);
11121 +               dir = hdir->hi_inode;
11122 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11123 +       } else {
11124 +               dir = d_inode(path->dentry);
11125 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11126 +       }
11127 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11128 +       err = PTR_ERR(hinopath.dentry);
11129 +       if (IS_ERR(hinopath.dentry))
11130 +               goto out_unlock;
11131 +
11132 +       err = 0;
11133 +       flags = O_RDONLY;
11134 +       if (load) {
11135 +               if (d_is_negative(hinopath.dentry))
11136 +                       goto out_dput; /* success */
11137 +       } else {
11138 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11139 +                       if (d_is_positive(hinopath.dentry)) {
11140 +                               delegated = NULL;
11141 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11142 +                                                  /*force*/0);
11143 +                               AuTraceErr(err);
11144 +                               if (unlikely(err))
11145 +                                       pr_err("ignored err %d, %pd2\n",
11146 +                                              err, hinopath.dentry);
11147 +                               if (unlikely(err == -EWOULDBLOCK))
11148 +                                       iput(delegated);
11149 +                               err = 0;
11150 +                       }
11151 +                       goto out_dput;
11152 +               } else if (!d_is_positive(hinopath.dentry)) {
11153 +                       err = vfsub_create(dir, &hinopath, 0600,
11154 +                                          /*want_excl*/false);
11155 +                       AuTraceErr(err);
11156 +                       if (unlikely(err))
11157 +                               goto out_dput;
11158 +               }
11159 +               flags = O_WRONLY;
11160 +       }
11161 +       hinopath.mnt = path->mnt;
11162 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11163 +       if (suspend)
11164 +               au_hn_inode_unlock(hdir);
11165 +       else
11166 +               inode_unlock(dir);
11167 +       dput(hinopath.dentry);
11168 +       AuTraceErrPtr(hinofile);
11169 +       if (IS_ERR(hinofile)) {
11170 +               err = PTR_ERR(hinofile);
11171 +               goto out;
11172 +       }
11173 +
11174 +       if (load)
11175 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11176 +       else
11177 +               err = au_dr_hino_store(sb, br, hinofile);
11178 +       fput(hinofile);
11179 +       goto out;
11180 +
11181 +out_dput:
11182 +       dput(hinopath.dentry);
11183 +out_unlock:
11184 +       if (suspend)
11185 +               au_hn_inode_unlock(hdir);
11186 +       else
11187 +               inode_unlock(dir);
11188 +out:
11189 +       AuTraceErr(err);
11190 +       return err;
11191 +}
11192 +
11193 +/* ---------------------------------------------------------------------- */
11194 +
11195 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11196 +{
11197 +       int err;
11198 +       struct kstatfs kstfs;
11199 +       dev_t dev;
11200 +       struct dentry *dentry;
11201 +       struct super_block *sb;
11202 +
11203 +       err = vfs_statfs((void *)path, &kstfs);
11204 +       AuTraceErr(err);
11205 +       if (unlikely(err))
11206 +               goto out;
11207 +
11208 +       /* todo: support for UUID */
11209 +
11210 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11211 +               brid->type = AuBrid_FSID;
11212 +               brid->fsid = kstfs.f_fsid;
11213 +       } else {
11214 +               dentry = path->dentry;
11215 +               sb = dentry->d_sb;
11216 +               dev = sb->s_dev;
11217 +               if (dev) {
11218 +                       brid->type = AuBrid_DEV;
11219 +                       brid->dev = dev;
11220 +               }
11221 +       }
11222 +
11223 +out:
11224 +       return err;
11225 +}
11226 +
11227 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11228 +                 const struct path *path)
11229 +{
11230 +       int err, i;
11231 +       struct au_dr_br *dr;
11232 +       struct hlist_bl_head *hbl;
11233 +
11234 +       dr = &br->br_dirren;
11235 +       hbl = dr->dr_h_ino;
11236 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11237 +               INIT_HLIST_BL_HEAD(hbl);
11238 +
11239 +       err = au_dr_brid_init(&dr->dr_brid, path);
11240 +       if (unlikely(err))
11241 +               goto out;
11242 +
11243 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11244 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11245 +
11246 +out:
11247 +       AuTraceErr(err);
11248 +       return err;
11249 +}
11250 +
11251 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11252 +{
11253 +       int err;
11254 +
11255 +       err = 0;
11256 +       if (au_br_writable(br->br_perm))
11257 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11258 +       if (!err)
11259 +               au_dr_hino_free(&br->br_dirren);
11260 +
11261 +       return err;
11262 +}
11263 +
11264 +/* ---------------------------------------------------------------------- */
11265 +
11266 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11267 +                      char *buf, size_t sz)
11268 +{
11269 +       int err;
11270 +       unsigned int major, minor;
11271 +       char *p;
11272 +
11273 +       p = buf;
11274 +       err = snprintf(p, sz, "%d_", brid->type);
11275 +       AuDebugOn(err > sz);
11276 +       p += err;
11277 +       sz -= err;
11278 +       switch (brid->type) {
11279 +       case AuBrid_Unset:
11280 +               return -EINVAL;
11281 +       case AuBrid_UUID:
11282 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11283 +               break;
11284 +       case AuBrid_FSID:
11285 +               err = snprintf(p, sz, "%08x-%08x",
11286 +                              brid->fsid.val[0], brid->fsid.val[1]);
11287 +               break;
11288 +       case AuBrid_DEV:
11289 +               major = MAJOR(brid->dev);
11290 +               minor = MINOR(brid->dev);
11291 +               if (major <= 0xff && minor <= 0xff)
11292 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11293 +               else
11294 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11295 +               break;
11296 +       }
11297 +       AuDebugOn(err > sz);
11298 +       p += err;
11299 +       sz -= err;
11300 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11301 +       AuDebugOn(err > sz);
11302 +       p += err;
11303 +       sz -= err;
11304 +
11305 +       return p - buf;
11306 +}
11307 +
11308 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11309 +{
11310 +       int rlen;
11311 +       struct dentry *br_dentry;
11312 +       struct inode *br_inode;
11313 +
11314 +       br_dentry = au_br_dentry(br);
11315 +       br_inode = d_inode(br_dentry);
11316 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11317 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11318 +       AuDebugOn(rlen > len);
11319 +
11320 +       return rlen;
11321 +}
11322 +
11323 +/* ---------------------------------------------------------------------- */
11324 +
11325 +/*
11326 + * from the given @h_dentry, construct drinfo at @*fdata.
11327 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11328 + * @allocated.
11329 + */
11330 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11331 +                              struct dentry *h_dentry,
11332 +                              unsigned char *allocated)
11333 +{
11334 +       int err, v;
11335 +       struct au_drinfo_fdata *f, *p;
11336 +       struct au_drinfo *drinfo;
11337 +       struct inode *h_inode;
11338 +       struct qstr *qname;
11339 +
11340 +       err = 0;
11341 +       f = *fdata;
11342 +       h_inode = d_inode(h_dentry);
11343 +       qname = &h_dentry->d_name;
11344 +       drinfo = &f->drinfo;
11345 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11346 +       drinfo->oldnamelen = qname->len;
11347 +       if (*allocated < sizeof(*f) + qname->len) {
11348 +               v = roundup_pow_of_two(*allocated + qname->len);
11349 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11350 +               if (unlikely(!p)) {
11351 +                       err = -ENOMEM;
11352 +                       AuTraceErr(err);
11353 +                       goto out;
11354 +               }
11355 +               f = p;
11356 +               *fdata = f;
11357 +               *allocated = v;
11358 +               drinfo = &f->drinfo;
11359 +       }
11360 +       memcpy(drinfo->oldname, qname->name, qname->len);
11361 +       AuDbg("i%llu, %.*s\n",
11362 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11363 +             drinfo->oldname);
11364 +
11365 +out:
11366 +       AuTraceErr(err);
11367 +       return err;
11368 +}
11369 +
11370 +/* callers have to free the return value */
11371 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11372 +{
11373 +       struct au_drinfo *ret, *drinfo;
11374 +       struct au_drinfo_fdata fdata;
11375 +       int len;
11376 +       loff_t pos;
11377 +       ssize_t ssz;
11378 +
11379 +       ret = ERR_PTR(-EIO);
11380 +       pos = 0;
11381 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11382 +       if (unlikely(ssz != sizeof(fdata))) {
11383 +               AuIOErr("ssz %zd, %u, %pD2\n",
11384 +                       ssz, (unsigned int)sizeof(fdata), file);
11385 +               goto out;
11386 +       }
11387 +
11388 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11389 +       switch (fdata.magic) {
11390 +       case AUFS_DRINFO_MAGIC_V1:
11391 +               break;
11392 +       default:
11393 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11394 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11395 +               goto out;
11396 +       }
11397 +
11398 +       drinfo = &fdata.drinfo;
11399 +       len = drinfo->oldnamelen;
11400 +       if (!len) {
11401 +               AuIOErr("broken drinfo %pD2\n", file);
11402 +               goto out;
11403 +       }
11404 +
11405 +       ret = NULL;
11406 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11407 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11408 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11409 +                     (unsigned long long)drinfo->ino,
11410 +                     (unsigned long long)h_ino, file);
11411 +               goto out; /* success */
11412 +       }
11413 +
11414 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11415 +       if (unlikely(!ret)) {
11416 +               ret = ERR_PTR(-ENOMEM);
11417 +               AuTraceErrPtr(ret);
11418 +               goto out;
11419 +       }
11420 +
11421 +       *ret = *drinfo;
11422 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11423 +       if (unlikely(ssz != len)) {
11424 +               au_kfree_rcu(ret);
11425 +               ret = ERR_PTR(-EIO);
11426 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11427 +               goto out;
11428 +       }
11429 +
11430 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11431 +
11432 +out:
11433 +       return ret;
11434 +}
11435 +
11436 +/* ---------------------------------------------------------------------- */
11437 +
11438 +/* in order to be revertible */
11439 +struct au_drinfo_rev_elm {
11440 +       int                     created;
11441 +       struct dentry           *info_dentry;
11442 +       struct au_drinfo        *info_last;
11443 +};
11444 +
11445 +struct au_drinfo_rev {
11446 +       unsigned char                   already;
11447 +       aufs_bindex_t                   nelm;
11448 +       struct au_drinfo_rev_elm        elm[];
11449 +};
11450 +
11451 +/* todo: isn't it too large? */
11452 +struct au_drinfo_store {
11453 +       struct path h_ppath;
11454 +       struct dentry *h_dentry;
11455 +       struct au_drinfo_fdata *fdata;
11456 +       char *infoname;                 /* inside of whname, just after PFX */
11457 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11458 +       aufs_bindex_t btgt, btail;
11459 +       unsigned char no_sio,
11460 +               allocated,              /* current size of *fdata */
11461 +               infonamelen,            /* room size for p */
11462 +               whnamelen,              /* length of the generated name */
11463 +               renameback;             /* renamed back */
11464 +};
11465 +
11466 +/* on rename(2) error, the caller should revert it using @elm */
11467 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11468 +                             struct au_drinfo_rev_elm *elm)
11469 +{
11470 +       int err, len;
11471 +       ssize_t ssz;
11472 +       loff_t pos;
11473 +       struct path infopath = {
11474 +               .mnt = w->h_ppath.mnt
11475 +       };
11476 +       struct inode *h_dir, *h_inode, *delegated;
11477 +       struct file *infofile;
11478 +       struct qstr *qname;
11479 +
11480 +       AuDebugOn(elm
11481 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11482 +
11483 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11484 +                                              w->whnamelen);
11485 +       AuTraceErrPtr(infopath.dentry);
11486 +       if (IS_ERR(infopath.dentry)) {
11487 +               err = PTR_ERR(infopath.dentry);
11488 +               goto out;
11489 +       }
11490 +
11491 +       err = 0;
11492 +       h_dir = d_inode(w->h_ppath.dentry);
11493 +       if (elm && d_is_negative(infopath.dentry)) {
11494 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11495 +               AuTraceErr(err);
11496 +               if (unlikely(err))
11497 +                       goto out_dput;
11498 +               elm->created = 1;
11499 +               elm->info_dentry = dget(infopath.dentry);
11500 +       }
11501 +
11502 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11503 +       AuTraceErrPtr(infofile);
11504 +       if (IS_ERR(infofile)) {
11505 +               err = PTR_ERR(infofile);
11506 +               goto out_dput;
11507 +       }
11508 +
11509 +       h_inode = d_inode(infopath.dentry);
11510 +       if (elm && i_size_read(h_inode)) {
11511 +               h_inode = d_inode(w->h_dentry);
11512 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11513 +               AuTraceErrPtr(elm->info_last);
11514 +               if (IS_ERR(elm->info_last)) {
11515 +                       err = PTR_ERR(elm->info_last);
11516 +                       elm->info_last = NULL;
11517 +                       AuDebugOn(elm->info_dentry);
11518 +                       goto out_fput;
11519 +               }
11520 +       }
11521 +
11522 +       if (elm && w->renameback) {
11523 +               delegated = NULL;
11524 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11525 +               AuTraceErr(err);
11526 +               if (unlikely(err == -EWOULDBLOCK))
11527 +                       iput(delegated);
11528 +               goto out_fput;
11529 +       }
11530 +
11531 +       pos = 0;
11532 +       qname = &w->h_dentry->d_name;
11533 +       len = sizeof(*w->fdata) + qname->len;
11534 +       if (!elm)
11535 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11536 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11537 +       if (ssz == len) {
11538 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11539 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11540 +               goto out_fput; /* success */
11541 +       } else {
11542 +               err = -EIO;
11543 +               if (ssz < 0)
11544 +                       err = ssz;
11545 +               /* the caller should revert it using @elm */
11546 +       }
11547 +
11548 +out_fput:
11549 +       fput(infofile);
11550 +out_dput:
11551 +       dput(infopath.dentry);
11552 +out:
11553 +       AuTraceErr(err);
11554 +       return err;
11555 +}
11556 +
11557 +struct au_call_drinfo_do_store_args {
11558 +       int *errp;
11559 +       struct au_drinfo_store *w;
11560 +       struct au_drinfo_rev_elm *elm;
11561 +};
11562 +
11563 +static void au_call_drinfo_do_store(void *args)
11564 +{
11565 +       struct au_call_drinfo_do_store_args *a = args;
11566 +
11567 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11568 +}
11569 +
11570 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11571 +                              struct au_drinfo_rev_elm *elm)
11572 +{
11573 +       int err, wkq_err;
11574 +
11575 +       if (w->no_sio)
11576 +               err = au_drinfo_do_store(w, elm);
11577 +       else {
11578 +               struct au_call_drinfo_do_store_args a = {
11579 +                       .errp   = &err,
11580 +                       .w      = w,
11581 +                       .elm    = elm
11582 +               };
11583 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11584 +               if (unlikely(wkq_err))
11585 +                       err = wkq_err;
11586 +       }
11587 +       AuTraceErr(err);
11588 +
11589 +       return err;
11590 +}
11591 +
11592 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11593 +                                    aufs_bindex_t btgt)
11594 +{
11595 +       int err;
11596 +
11597 +       memset(w, 0, sizeof(*w));
11598 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11599 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11600 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11601 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11602 +       w->btgt = btgt;
11603 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11604 +
11605 +       err = -ENOMEM;
11606 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11607 +       if (unlikely(!w->fdata)) {
11608 +               AuTraceErr(err);
11609 +               goto out;
11610 +       }
11611 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11612 +       err = 0;
11613 +
11614 +out:
11615 +       return err;
11616 +}
11617 +
11618 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11619 +{
11620 +       au_kfree_rcu(w->fdata);
11621 +}
11622 +
11623 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11624 +                               struct au_drinfo_store *w)
11625 +{
11626 +       struct au_drinfo_rev_elm *elm;
11627 +       struct inode *h_dir, *delegated;
11628 +       int err, nelm;
11629 +       struct path infopath = {
11630 +               .mnt = w->h_ppath.mnt
11631 +       };
11632 +
11633 +       h_dir = d_inode(w->h_ppath.dentry);
11634 +       IMustLock(h_dir);
11635 +
11636 +       err = 0;
11637 +       elm = rev->elm;
11638 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11639 +               AuDebugOn(elm->created && elm->info_last);
11640 +               if (elm->created) {
11641 +                       AuDbg("here\n");
11642 +                       delegated = NULL;
11643 +                       infopath.dentry = elm->info_dentry;
11644 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11645 +                                          !w->no_sio);
11646 +                       AuTraceErr(err);
11647 +                       if (unlikely(err == -EWOULDBLOCK))
11648 +                               iput(delegated);
11649 +                       dput(elm->info_dentry);
11650 +               } else if (elm->info_last) {
11651 +                       AuDbg("here\n");
11652 +                       w->fdata->drinfo = *elm->info_last;
11653 +                       memcpy(w->fdata->drinfo.oldname,
11654 +                              elm->info_last->oldname,
11655 +                              elm->info_last->oldnamelen);
11656 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11657 +                       au_kfree_rcu(elm->info_last);
11658 +               }
11659 +               if (unlikely(err))
11660 +                       AuIOErr("%d, %s\n", err, w->whname);
11661 +               /* go on even if err */
11662 +       }
11663 +}
11664 +
11665 +/* caller has to call au_dr_rename_fin() later */
11666 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11667 +                          struct qstr *dst_name, void *_rev)
11668 +{
11669 +       int err, sz, nelm;
11670 +       aufs_bindex_t bindex, btail;
11671 +       struct au_drinfo_store work;
11672 +       struct au_drinfo_rev *rev, **p;
11673 +       struct au_drinfo_rev_elm *elm;
11674 +       struct super_block *sb;
11675 +       struct au_branch *br;
11676 +       struct au_hinode *hdir;
11677 +
11678 +       err = au_drinfo_store_work_init(&work, btgt);
11679 +       AuTraceErr(err);
11680 +       if (unlikely(err))
11681 +               goto out;
11682 +
11683 +       err = -ENOMEM;
11684 +       btail = au_dbtaildir(dentry);
11685 +       nelm = btail - btgt;
11686 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11687 +       rev = kcalloc(1, sz, GFP_NOFS);
11688 +       if (unlikely(!rev)) {
11689 +               AuTraceErr(err);
11690 +               goto out_args;
11691 +       }
11692 +       rev->nelm = nelm;
11693 +       elm = rev->elm;
11694 +       p = _rev;
11695 +       *p = rev;
11696 +
11697 +       err = 0;
11698 +       sb = dentry->d_sb;
11699 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11700 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11701 +       hdir = au_hi(d_inode(dentry), btgt);
11702 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11703 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11704 +               work.h_dentry = au_h_dptr(dentry, bindex);
11705 +               if (!work.h_dentry)
11706 +                       continue;
11707 +
11708 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11709 +                                         &work.allocated);
11710 +               AuTraceErr(err);
11711 +               if (unlikely(err))
11712 +                       break;
11713 +
11714 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11715 +               br = au_sbr(sb, bindex);
11716 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11717 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11718 +                                                work.infonamelen);
11719 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11720 +                     work.whnamelen, work.whname,
11721 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11722 +                     work.fdata->drinfo.oldnamelen,
11723 +                     work.fdata->drinfo.oldname);
11724 +
11725 +               err = au_drinfo_store_sio(&work, elm);
11726 +               AuTraceErr(err);
11727 +               if (unlikely(err))
11728 +                       break;
11729 +       }
11730 +       if (unlikely(err)) {
11731 +               /* revert all drinfo */
11732 +               au_drinfo_store_rev(rev, &work);
11733 +               au_kfree_try_rcu(rev);
11734 +               *p = NULL;
11735 +       }
11736 +       au_hn_inode_unlock(hdir);
11737 +
11738 +out_args:
11739 +       au_drinfo_store_work_fin(&work);
11740 +out:
11741 +       return err;
11742 +}
11743 +
11744 +/* ---------------------------------------------------------------------- */
11745 +
11746 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11747 +                struct qstr *dst_name, void *_rev)
11748 +{
11749 +       int err, already;
11750 +       ino_t ino;
11751 +       struct super_block *sb;
11752 +       struct au_branch *br;
11753 +       struct au_dr_br *dr;
11754 +       struct dentry *h_dentry;
11755 +       struct inode *h_inode;
11756 +       struct au_dr_hino *ent;
11757 +       struct au_drinfo_rev *rev, **p;
11758 +
11759 +       AuDbg("bindex %d\n", bindex);
11760 +
11761 +       err = -ENOMEM;
11762 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11763 +       if (unlikely(!ent))
11764 +               goto out;
11765 +
11766 +       sb = src->d_sb;
11767 +       br = au_sbr(sb, bindex);
11768 +       dr = &br->br_dirren;
11769 +       h_dentry = au_h_dptr(src, bindex);
11770 +       h_inode = d_inode(h_dentry);
11771 +       ino = h_inode->i_ino;
11772 +       ent->dr_h_ino = ino;
11773 +       already = au_dr_hino_test_add(dr, ino, ent);
11774 +       AuDbg("b%d, hi%llu, already %d\n",
11775 +             bindex, (unsigned long long)ino, already);
11776 +
11777 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11778 +       AuTraceErr(err);
11779 +       if (!err) {
11780 +               p = _rev;
11781 +               rev = *p;
11782 +               rev->already = already;
11783 +               goto out; /* success */
11784 +       }
11785 +
11786 +       /* revert */
11787 +       if (!already)
11788 +               au_dr_hino_del(dr, ent);
11789 +       au_kfree_rcu(ent);
11790 +
11791 +out:
11792 +       AuTraceErr(err);
11793 +       return err;
11794 +}
11795 +
11796 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11797 +{
11798 +       struct au_drinfo_rev *rev;
11799 +       struct au_drinfo_rev_elm *elm;
11800 +       int nelm;
11801 +
11802 +       rev = _rev;
11803 +       elm = rev->elm;
11804 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11805 +               dput(elm->info_dentry);
11806 +               au_kfree_rcu(elm->info_last);
11807 +       }
11808 +       au_kfree_try_rcu(rev);
11809 +}
11810 +
11811 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11812 +{
11813 +       int err;
11814 +       struct au_drinfo_store work;
11815 +       struct au_drinfo_rev *rev = _rev;
11816 +       struct super_block *sb;
11817 +       struct au_branch *br;
11818 +       struct inode *h_inode;
11819 +       struct au_dr_br *dr;
11820 +       struct au_dr_hino *ent;
11821 +
11822 +       err = au_drinfo_store_work_init(&work, btgt);
11823 +       if (unlikely(err))
11824 +               goto out;
11825 +
11826 +       sb = src->d_sb;
11827 +       br = au_sbr(sb, btgt);
11828 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11829 +       work.h_ppath.mnt = au_br_mnt(br);
11830 +       au_drinfo_store_rev(rev, &work);
11831 +       au_drinfo_store_work_fin(&work);
11832 +       if (rev->already)
11833 +               goto out;
11834 +
11835 +       dr = &br->br_dirren;
11836 +       h_inode = d_inode(work.h_ppath.dentry);
11837 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11838 +       BUG_ON(!ent);
11839 +       au_dr_hino_del(dr, ent);
11840 +       au_kfree_rcu(ent);
11841 +
11842 +out:
11843 +       au_kfree_try_rcu(rev);
11844 +       if (unlikely(err))
11845 +               pr_err("failed to remove dirren info\n");
11846 +}
11847 +
11848 +/* ---------------------------------------------------------------------- */
11849 +
11850 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11851 +                                          char *whname, int whnamelen,
11852 +                                          struct dentry **info_dentry)
11853 +{
11854 +       struct au_drinfo *drinfo;
11855 +       struct file *f;
11856 +       struct inode *h_dir;
11857 +       struct path infopath;
11858 +       int unlocked;
11859 +
11860 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11861 +
11862 +       *info_dentry = NULL;
11863 +       drinfo = NULL;
11864 +       unlocked = 0;
11865 +       h_dir = d_inode(h_ppath->dentry);
11866 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11867 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11868 +                                              whnamelen);
11869 +       if (IS_ERR(infopath.dentry)) {
11870 +               drinfo = (void *)infopath.dentry;
11871 +               goto out;
11872 +       }
11873 +
11874 +       if (d_is_negative(infopath.dentry))
11875 +               goto out_dput; /* success */
11876 +
11877 +       infopath.mnt = h_ppath->mnt;
11878 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11879 +       inode_unlock_shared(h_dir);
11880 +       unlocked = 1;
11881 +       if (IS_ERR(f)) {
11882 +               drinfo = (void *)f;
11883 +               goto out_dput;
11884 +       }
11885 +
11886 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11887 +       if (IS_ERR_OR_NULL(drinfo))
11888 +               goto out_fput;
11889 +
11890 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11891 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11892 +
11893 +out_fput:
11894 +       fput(f);
11895 +out_dput:
11896 +       dput(infopath.dentry);
11897 +out:
11898 +       if (!unlocked)
11899 +               inode_unlock_shared(h_dir);
11900 +       AuTraceErrPtr(drinfo);
11901 +       return drinfo;
11902 +}
11903 +
11904 +struct au_drinfo_do_load_args {
11905 +       struct au_drinfo **drinfop;
11906 +       struct path *h_ppath;
11907 +       char *whname;
11908 +       int whnamelen;
11909 +       struct dentry **info_dentry;
11910 +};
11911 +
11912 +static void au_call_drinfo_do_load(void *args)
11913 +{
11914 +       struct au_drinfo_do_load_args *a = args;
11915 +
11916 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11917 +                                       a->info_dentry);
11918 +}
11919 +
11920 +struct au_drinfo_load {
11921 +       struct path h_ppath;
11922 +       struct qstr *qname;
11923 +       unsigned char no_sio;
11924 +
11925 +       aufs_bindex_t ninfo;
11926 +       struct au_drinfo **drinfo;
11927 +};
11928 +
11929 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11930 +                         struct au_branch *br)
11931 +{
11932 +       int err, wkq_err, whnamelen, e;
11933 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11934 +               = AUFS_WH_DR_INFO_PFX;
11935 +       struct au_drinfo *drinfo;
11936 +       struct qstr oldname;
11937 +       struct inode *h_dir, *delegated;
11938 +       struct dentry *info_dentry;
11939 +       struct path infopath;
11940 +
11941 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11942 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11943 +                                   sizeof(whname) - whnamelen);
11944 +       if (w->no_sio)
11945 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11946 +                                          &info_dentry);
11947 +       else {
11948 +               struct au_drinfo_do_load_args args = {
11949 +                       .drinfop        = &drinfo,
11950 +                       .h_ppath        = &w->h_ppath,
11951 +                       .whname         = whname,
11952 +                       .whnamelen      = whnamelen,
11953 +                       .info_dentry    = &info_dentry
11954 +               };
11955 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11956 +               if (unlikely(wkq_err))
11957 +                       drinfo = ERR_PTR(wkq_err);
11958 +       }
11959 +       err = PTR_ERR(drinfo);
11960 +       if (IS_ERR_OR_NULL(drinfo))
11961 +               goto out;
11962 +
11963 +       err = 0;
11964 +       oldname.len = drinfo->oldnamelen;
11965 +       oldname.name = drinfo->oldname;
11966 +       if (au_qstreq(w->qname, &oldname)) {
11967 +               /* the name is renamed back */
11968 +               au_kfree_rcu(drinfo);
11969 +               drinfo = NULL;
11970 +
11971 +               infopath.dentry = info_dentry;
11972 +               infopath.mnt = w->h_ppath.mnt;
11973 +               h_dir = d_inode(w->h_ppath.dentry);
11974 +               delegated = NULL;
11975 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11976 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11977 +               inode_unlock(h_dir);
11978 +               if (unlikely(e))
11979 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11980 +               if (unlikely(e == -EWOULDBLOCK))
11981 +                       iput(delegated);
11982 +       }
11983 +       au_kfree_rcu(w->drinfo[bindex]);
11984 +       w->drinfo[bindex] = drinfo;
11985 +       dput(info_dentry);
11986 +
11987 +out:
11988 +       AuTraceErr(err);
11989 +       return err;
11990 +}
11991 +
11992 +/* ---------------------------------------------------------------------- */
11993 +
11994 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11995 +{
11996 +       struct au_drinfo **p = drinfo;
11997 +
11998 +       while (n-- > 0)
11999 +               au_kfree_rcu(*drinfo++);
12000 +       au_kfree_try_rcu(p);
12001 +}
12002 +
12003 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12004 +              aufs_bindex_t btgt)
12005 +{
12006 +       int err, ninfo;
12007 +       struct au_drinfo_load w;
12008 +       aufs_bindex_t bindex, bbot;
12009 +       struct au_branch *br;
12010 +       struct inode *h_dir;
12011 +       struct au_dr_hino *ent;
12012 +       struct super_block *sb;
12013 +
12014 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12015 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12016 +             AuLNPair(&lkup->whname), btgt);
12017 +
12018 +       sb = dentry->d_sb;
12019 +       bbot = au_sbbot(sb);
12020 +       w.ninfo = bbot + 1;
12021 +       if (!lkup->dirren.drinfo) {
12022 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12023 +                                             sizeof(*lkup->dirren.drinfo),
12024 +                                             GFP_NOFS);
12025 +               if (unlikely(!lkup->dirren.drinfo)) {
12026 +                       err = -ENOMEM;
12027 +                       goto out;
12028 +               }
12029 +               lkup->dirren.ninfo = w.ninfo;
12030 +       }
12031 +       w.drinfo = lkup->dirren.drinfo;
12032 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12033 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12034 +       AuDebugOn(!w.h_ppath.dentry);
12035 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12036 +       w.qname = &dentry->d_name;
12037 +
12038 +       ninfo = 0;
12039 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12040 +               br = au_sbr(sb, bindex);
12041 +               err = au_drinfo_load(&w, bindex, br);
12042 +               if (unlikely(err))
12043 +                       goto out_free;
12044 +               if (w.drinfo[bindex])
12045 +                       ninfo++;
12046 +       }
12047 +       if (!ninfo) {
12048 +               br = au_sbr(sb, btgt);
12049 +               h_dir = d_inode(w.h_ppath.dentry);
12050 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12051 +               AuDebugOn(!ent);
12052 +               au_dr_hino_del(&br->br_dirren, ent);
12053 +               au_kfree_rcu(ent);
12054 +       }
12055 +       goto out; /* success */
12056 +
12057 +out_free:
12058 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12059 +       lkup->dirren.ninfo = 0;
12060 +       lkup->dirren.drinfo = NULL;
12061 +out:
12062 +       AuTraceErr(err);
12063 +       return err;
12064 +}
12065 +
12066 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12067 +{
12068 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12069 +}
12070 +
12071 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12072 +{
12073 +       int err;
12074 +       struct au_drinfo *drinfo;
12075 +
12076 +       err = 0;
12077 +       if (!lkup->dirren.drinfo)
12078 +               goto out;
12079 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12080 +       drinfo = lkup->dirren.drinfo[btgt];
12081 +       if (!drinfo)
12082 +               goto out;
12083 +
12084 +       au_kfree_try_rcu(lkup->whname.name);
12085 +       lkup->whname.name = NULL;
12086 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12087 +       lkup->dirren.dr_name.name = drinfo->oldname;
12088 +       lkup->name = &lkup->dirren.dr_name;
12089 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12090 +       if (!err)
12091 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12092 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12093 +                     btgt);
12094 +
12095 +out:
12096 +       AuTraceErr(err);
12097 +       return err;
12098 +}
12099 +
12100 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12101 +                    ino_t h_ino)
12102 +{
12103 +       int match;
12104 +       struct au_drinfo *drinfo;
12105 +
12106 +       match = 1;
12107 +       if (!lkup->dirren.drinfo)
12108 +               goto out;
12109 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12110 +       drinfo = lkup->dirren.drinfo[bindex];
12111 +       if (!drinfo)
12112 +               goto out;
12113 +
12114 +       match = (drinfo->ino == h_ino);
12115 +       AuDbg("match %d\n", match);
12116 +
12117 +out:
12118 +       return match;
12119 +}
12120 +
12121 +/* ---------------------------------------------------------------------- */
12122 +
12123 +int au_dr_opt_set(struct super_block *sb)
12124 +{
12125 +       int err;
12126 +       aufs_bindex_t bindex, bbot;
12127 +       struct au_branch *br;
12128 +
12129 +       err = 0;
12130 +       bbot = au_sbbot(sb);
12131 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12132 +               br = au_sbr(sb, bindex);
12133 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12134 +       }
12135 +
12136 +       return err;
12137 +}
12138 +
12139 +int au_dr_opt_flush(struct super_block *sb)
12140 +{
12141 +       int err;
12142 +       aufs_bindex_t bindex, bbot;
12143 +       struct au_branch *br;
12144 +
12145 +       err = 0;
12146 +       bbot = au_sbbot(sb);
12147 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12148 +               br = au_sbr(sb, bindex);
12149 +               if (au_br_writable(br->br_perm))
12150 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12151 +       }
12152 +
12153 +       return err;
12154 +}
12155 +
12156 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12157 +{
12158 +       int err;
12159 +       aufs_bindex_t bindex, bbot;
12160 +       struct au_branch *br;
12161 +
12162 +       err = 0;
12163 +       if (!no_flush) {
12164 +               err = au_dr_opt_flush(sb);
12165 +               if (unlikely(err))
12166 +                       goto out;
12167 +       }
12168 +
12169 +       bbot = au_sbbot(sb);
12170 +       for (bindex = 0; bindex <= bbot; bindex++) {
12171 +               br = au_sbr(sb, bindex);
12172 +               au_dr_hino_free(&br->br_dirren);
12173 +       }
12174 +
12175 +out:
12176 +       return err;
12177 +}
12178 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12179 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12180 +++ linux/fs/aufs/dirren.h      2021-02-24 13:33:42.741013619 +0100
12181 @@ -0,0 +1,140 @@
12182 +/* SPDX-License-Identifier: GPL-2.0 */
12183 +/*
12184 + * Copyright (C) 2017-2020 Junjiro R. Okajima
12185 + *
12186 + * This program, aufs is free software; you can redistribute it and/or modify
12187 + * it under the terms of the GNU General Public License as published by
12188 + * the Free Software Foundation; either version 2 of the License, or
12189 + * (at your option) any later version.
12190 + *
12191 + * This program is distributed in the hope that it will be useful,
12192 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12193 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12194 + * GNU General Public License for more details.
12195 + *
12196 + * You should have received a copy of the GNU General Public License
12197 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12198 + */
12199 +
12200 +/*
12201 + * renamed dir info
12202 + */
12203 +
12204 +#ifndef __AUFS_DIRREN_H__
12205 +#define __AUFS_DIRREN_H__
12206 +
12207 +#ifdef __KERNEL__
12208 +
12209 +#include <linux/dcache.h>
12210 +#include <linux/statfs.h>
12211 +#include <linux/uuid.h>
12212 +#include "hbl.h"
12213 +
12214 +#define AuDirren_NHASH 100
12215 +
12216 +#ifdef CONFIG_AUFS_DIRREN
12217 +enum au_brid_type {
12218 +       AuBrid_Unset,
12219 +       AuBrid_UUID,
12220 +       AuBrid_FSID,
12221 +       AuBrid_DEV
12222 +};
12223 +
12224 +struct au_dr_brid {
12225 +       enum au_brid_type       type;
12226 +       union {
12227 +               uuid_t  uuid;   /* unimplemented yet */
12228 +               fsid_t  fsid;
12229 +               dev_t   dev;
12230 +       };
12231 +};
12232 +
12233 +/* 20 is the max digits length of ulong 64 */
12234 +/* brid-type "_" uuid "_" inum */
12235 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12236 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12237 +
12238 +struct au_dr_hino {
12239 +       struct hlist_bl_node    dr_hnode;
12240 +       ino_t                   dr_h_ino;
12241 +};
12242 +
12243 +struct au_dr_br {
12244 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12245 +       struct au_dr_brid       dr_brid;
12246 +};
12247 +
12248 +struct au_dr_lookup {
12249 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12250 +       struct qstr             dr_name; /* subset of dr_info */
12251 +       aufs_bindex_t           ninfo;
12252 +       struct au_drinfo        **drinfo;
12253 +};
12254 +#else
12255 +struct au_dr_hino;
12256 +/* empty */
12257 +struct au_dr_br { };
12258 +struct au_dr_lookup { };
12259 +#endif
12260 +
12261 +/* ---------------------------------------------------------------------- */
12262 +
12263 +struct au_branch;
12264 +struct au_do_lookup_args;
12265 +struct au_hinode;
12266 +#ifdef CONFIG_AUFS_DIRREN
12267 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12268 +                       struct au_dr_hino *add_ent);
12269 +void au_dr_hino_free(struct au_dr_br *dr);
12270 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12271 +                 const struct path *path);
12272 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12273 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12274 +                struct qstr *dst_name, void *_rev);
12275 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12276 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12277 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12278 +              aufs_bindex_t bindex);
12279 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12280 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12281 +                    ino_t h_ino);
12282 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12283 +int au_dr_opt_set(struct super_block *sb);
12284 +int au_dr_opt_flush(struct super_block *sb);
12285 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12286 +#else
12287 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12288 +          struct au_dr_hino *add_ent);
12289 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12290 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12291 +          const struct path *path);
12292 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12293 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12294 +          struct qstr *dst_name, void *_rev);
12295 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12296 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12297 +          void *rev);
12298 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12299 +          aufs_bindex_t bindex);
12300 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12301 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12302 +          aufs_bindex_t bindex, ino_t h_ino);
12303 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12304 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12305 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12306 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12307 +#endif
12308 +
12309 +/* ---------------------------------------------------------------------- */
12310 +
12311 +#ifdef CONFIG_AUFS_DIRREN
12312 +static inline int au_dr_ihash(ino_t h_ino)
12313 +{
12314 +       return h_ino % AuDirren_NHASH;
12315 +}
12316 +#else
12317 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12318 +#endif
12319 +
12320 +#endif /* __KERNEL__ */
12321 +#endif /* __AUFS_DIRREN_H__ */
12322 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12323 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12324 +++ linux/fs/aufs/dynop.c       2021-02-24 13:33:42.744347058 +0100
12325 @@ -0,0 +1,368 @@
12326 +// SPDX-License-Identifier: GPL-2.0
12327 +/*
12328 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12329 + *
12330 + * This program, aufs is free software; you can redistribute it and/or modify
12331 + * it under the terms of the GNU General Public License as published by
12332 + * the Free Software Foundation; either version 2 of the License, or
12333 + * (at your option) any later version.
12334 + *
12335 + * This program is distributed in the hope that it will be useful,
12336 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12337 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12338 + * GNU General Public License for more details.
12339 + *
12340 + * You should have received a copy of the GNU General Public License
12341 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12342 + */
12343 +
12344 +/*
12345 + * dynamically customizable operations for regular files
12346 + */
12347 +
12348 +#include "aufs.h"
12349 +
12350 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12351 +
12352 +/*
12353 + * How large will these lists be?
12354 + * Usually just a few elements, 20-30 at most for each, I guess.
12355 + */
12356 +static struct hlist_bl_head dynop[AuDyLast];
12357 +
12358 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12359 +                                    const void *h_op)
12360 +{
12361 +       struct au_dykey *key, *tmp;
12362 +       struct hlist_bl_node *pos;
12363 +
12364 +       key = NULL;
12365 +       hlist_bl_lock(hbl);
12366 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12367 +               if (tmp->dk_op.dy_hop == h_op) {
12368 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12369 +                               key = tmp;
12370 +                       break;
12371 +               }
12372 +       hlist_bl_unlock(hbl);
12373 +
12374 +       return key;
12375 +}
12376 +
12377 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12378 +{
12379 +       struct au_dykey **k, *found;
12380 +       const void *h_op = key->dk_op.dy_hop;
12381 +       int i;
12382 +
12383 +       found = NULL;
12384 +       k = br->br_dykey;
12385 +       for (i = 0; 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 +                       break;
12393 +       if (!found) {
12394 +               spin_lock(&br->br_dykey_lock);
12395 +               for (; i < AuBrDynOp; i++)
12396 +                       if (k[i]) {
12397 +                               if (k[i]->dk_op.dy_hop == h_op) {
12398 +                                       found = k[i];
12399 +                                       break;
12400 +                               }
12401 +                       } else {
12402 +                               k[i] = key;
12403 +                               break;
12404 +                       }
12405 +               spin_unlock(&br->br_dykey_lock);
12406 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12407 +       }
12408 +
12409 +       return found;
12410 +}
12411 +
12412 +/* kref_get() if @key is already added */
12413 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12414 +{
12415 +       struct au_dykey *tmp, *found;
12416 +       struct hlist_bl_node *pos;
12417 +       const void *h_op = key->dk_op.dy_hop;
12418 +
12419 +       found = NULL;
12420 +       hlist_bl_lock(hbl);
12421 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12422 +               if (tmp->dk_op.dy_hop == h_op) {
12423 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12424 +                               found = tmp;
12425 +                       break;
12426 +               }
12427 +       if (!found)
12428 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12429 +       hlist_bl_unlock(hbl);
12430 +
12431 +       if (!found)
12432 +               DyPrSym(key);
12433 +       return found;
12434 +}
12435 +
12436 +static void dy_free_rcu(struct rcu_head *rcu)
12437 +{
12438 +       struct au_dykey *key;
12439 +
12440 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12441 +       DyPrSym(key);
12442 +       kfree(key);
12443 +}
12444 +
12445 +static void dy_free(struct kref *kref)
12446 +{
12447 +       struct au_dykey *key;
12448 +       struct hlist_bl_head *hbl;
12449 +
12450 +       key = container_of(kref, struct au_dykey, dk_kref);
12451 +       hbl = dynop + key->dk_op.dy_type;
12452 +       au_hbl_del(&key->dk_hnode, hbl);
12453 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12454 +}
12455 +
12456 +void au_dy_put(struct au_dykey *key)
12457 +{
12458 +       kref_put(&key->dk_kref, dy_free);
12459 +}
12460 +
12461 +/* ---------------------------------------------------------------------- */
12462 +
12463 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12464 +
12465 +#ifdef CONFIG_AUFS_DEBUG
12466 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12467 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12468 +#else
12469 +#define DyDbgDeclare(cnt)      do {} while (0)
12470 +#define DyDbgInc(cnt)          do {} while (0)
12471 +#endif
12472 +
12473 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12474 +       DyDbgInc(cnt);                                                  \
12475 +       if (h_op->func) {                                               \
12476 +               if (src.func)                                           \
12477 +                       dst.func = src.func;                            \
12478 +               else                                                    \
12479 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12480 +       }                                                               \
12481 +} while (0)
12482 +
12483 +#define DySetForce(func, dst, src) do {                \
12484 +       AuDebugOn(!src.func);                   \
12485 +       DyDbgInc(cnt);                          \
12486 +       dst.func = src.func;                    \
12487 +} while (0)
12488 +
12489 +#define DySetAop(func) \
12490 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12491 +#define DySetAopForce(func) \
12492 +       DySetForce(func, dyaop->da_op, aufs_aop)
12493 +
12494 +static void dy_aop(struct au_dykey *key, const void *h_op,
12495 +                  struct super_block *h_sb __maybe_unused)
12496 +{
12497 +       struct au_dyaop *dyaop = (void *)key;
12498 +       const struct address_space_operations *h_aop = h_op;
12499 +       DyDbgDeclare(cnt);
12500 +
12501 +       AuDbg("%s\n", au_sbtype(h_sb));
12502 +
12503 +       DySetAop(writepage);
12504 +       DySetAopForce(readpage);        /* force */
12505 +       DySetAop(writepages);
12506 +       DySetAop(set_page_dirty);
12507 +       DySetAop(readpages);
12508 +       DySetAop(readahead);
12509 +       DySetAop(write_begin);
12510 +       DySetAop(write_end);
12511 +       DySetAop(bmap);
12512 +       DySetAop(invalidatepage);
12513 +       DySetAop(releasepage);
12514 +       DySetAop(freepage);
12515 +       /* this one will be changed according to an aufs mount option */
12516 +       DySetAop(direct_IO);
12517 +       DySetAop(migratepage);
12518 +       DySetAop(isolate_page);
12519 +       DySetAop(putback_page);
12520 +       DySetAop(launder_page);
12521 +       DySetAop(is_partially_uptodate);
12522 +       DySetAop(is_dirty_writeback);
12523 +       DySetAop(error_remove_page);
12524 +       DySetAop(swap_activate);
12525 +       DySetAop(swap_deactivate);
12526 +
12527 +       DyDbgSize(cnt, *h_aop);
12528 +}
12529 +
12530 +/* ---------------------------------------------------------------------- */
12531 +
12532 +static void dy_bug(struct kref *kref)
12533 +{
12534 +       BUG();
12535 +}
12536 +
12537 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12538 +{
12539 +       struct au_dykey *key, *old;
12540 +       struct hlist_bl_head *hbl;
12541 +       struct op {
12542 +               unsigned int sz;
12543 +               void (*set)(struct au_dykey *key, const void *h_op,
12544 +                           struct super_block *h_sb __maybe_unused);
12545 +       };
12546 +       static const struct op a[] = {
12547 +               [AuDy_AOP] = {
12548 +                       .sz     = sizeof(struct au_dyaop),
12549 +                       .set    = dy_aop
12550 +               }
12551 +       };
12552 +       const struct op *p;
12553 +
12554 +       hbl = dynop + op->dy_type;
12555 +       key = dy_gfind_get(hbl, op->dy_hop);
12556 +       if (key)
12557 +               goto out_add; /* success */
12558 +
12559 +       p = a + op->dy_type;
12560 +       key = kzalloc(p->sz, GFP_NOFS);
12561 +       if (unlikely(!key)) {
12562 +               key = ERR_PTR(-ENOMEM);
12563 +               goto out;
12564 +       }
12565 +
12566 +       key->dk_op.dy_hop = op->dy_hop;
12567 +       kref_init(&key->dk_kref);
12568 +       p->set(key, op->dy_hop, au_br_sb(br));
12569 +       old = dy_gadd(hbl, key);
12570 +       if (old) {
12571 +               au_kfree_rcu(key);
12572 +               key = old;
12573 +       }
12574 +
12575 +out_add:
12576 +       old = dy_bradd(br, key);
12577 +       if (old)
12578 +               /* its ref-count should never be zero here */
12579 +               kref_put(&key->dk_kref, dy_bug);
12580 +out:
12581 +       return key;
12582 +}
12583 +
12584 +/* ---------------------------------------------------------------------- */
12585 +/*
12586 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12587 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12588 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12589 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12590 + * See the aufs manual in detail.
12591 + */
12592 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12593 +{
12594 +       if (!do_dx)
12595 +               dyaop->da_op.direct_IO = NULL;
12596 +       else
12597 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12598 +}
12599 +
12600 +static struct au_dyaop *dy_aget(struct au_branch *br,
12601 +                               const struct address_space_operations *h_aop,
12602 +                               int do_dx)
12603 +{
12604 +       struct au_dyaop *dyaop;
12605 +       struct au_dynop op;
12606 +
12607 +       op.dy_type = AuDy_AOP;
12608 +       op.dy_haop = h_aop;
12609 +       dyaop = (void *)dy_get(&op, br);
12610 +       if (IS_ERR(dyaop))
12611 +               goto out;
12612 +       dy_adx(dyaop, do_dx);
12613 +
12614 +out:
12615 +       return dyaop;
12616 +}
12617 +
12618 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12619 +               struct inode *h_inode)
12620 +{
12621 +       int err, do_dx;
12622 +       struct super_block *sb;
12623 +       struct au_branch *br;
12624 +       struct au_dyaop *dyaop;
12625 +
12626 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12627 +       IiMustWriteLock(inode);
12628 +
12629 +       sb = inode->i_sb;
12630 +       br = au_sbr(sb, bindex);
12631 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12632 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12633 +       err = PTR_ERR(dyaop);
12634 +       if (IS_ERR(dyaop))
12635 +               /* unnecessary to call dy_fput() */
12636 +               goto out;
12637 +
12638 +       err = 0;
12639 +       inode->i_mapping->a_ops = &dyaop->da_op;
12640 +
12641 +out:
12642 +       return err;
12643 +}
12644 +
12645 +/*
12646 + * Is it safe to replace a_ops during the inode/file is in operation?
12647 + * Yes, I hope so.
12648 + */
12649 +int au_dy_irefresh(struct inode *inode)
12650 +{
12651 +       int err;
12652 +       aufs_bindex_t btop;
12653 +       struct inode *h_inode;
12654 +
12655 +       err = 0;
12656 +       if (S_ISREG(inode->i_mode)) {
12657 +               btop = au_ibtop(inode);
12658 +               h_inode = au_h_iptr(inode, btop);
12659 +               err = au_dy_iaop(inode, btop, h_inode);
12660 +       }
12661 +       return err;
12662 +}
12663 +
12664 +void au_dy_arefresh(int do_dx)
12665 +{
12666 +       struct hlist_bl_head *hbl;
12667 +       struct hlist_bl_node *pos;
12668 +       struct au_dykey *key;
12669 +
12670 +       hbl = dynop + AuDy_AOP;
12671 +       hlist_bl_lock(hbl);
12672 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12673 +               dy_adx((void *)key, do_dx);
12674 +       hlist_bl_unlock(hbl);
12675 +}
12676 +
12677 +/* ---------------------------------------------------------------------- */
12678 +
12679 +void __init au_dy_init(void)
12680 +{
12681 +       int i;
12682 +
12683 +       for (i = 0; i < AuDyLast; i++)
12684 +               INIT_HLIST_BL_HEAD(dynop + i);
12685 +}
12686 +
12687 +void au_dy_fin(void)
12688 +{
12689 +       int i;
12690 +
12691 +       for (i = 0; i < AuDyLast; i++)
12692 +               WARN_ON(!hlist_bl_empty(dynop + i));
12693 +}
12694 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12695 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12696 +++ linux/fs/aufs/dynop.h       2021-02-24 13:33:42.744347058 +0100
12697 @@ -0,0 +1,77 @@
12698 +/* SPDX-License-Identifier: GPL-2.0 */
12699 +/*
12700 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12701 + *
12702 + * This program, aufs is free software; you can redistribute it and/or modify
12703 + * it under the terms of the GNU General Public License as published by
12704 + * the Free Software Foundation; either version 2 of the License, or
12705 + * (at your option) any later version.
12706 + *
12707 + * This program is distributed in the hope that it will be useful,
12708 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12709 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12710 + * GNU General Public License for more details.
12711 + *
12712 + * You should have received a copy of the GNU General Public License
12713 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12714 + */
12715 +
12716 +/*
12717 + * dynamically customizable operations (for regular files only)
12718 + */
12719 +
12720 +#ifndef __AUFS_DYNOP_H__
12721 +#define __AUFS_DYNOP_H__
12722 +
12723 +#ifdef __KERNEL__
12724 +
12725 +#include <linux/fs.h>
12726 +#include <linux/kref.h>
12727 +
12728 +enum {AuDy_AOP, AuDyLast};
12729 +
12730 +struct au_dynop {
12731 +       int                                             dy_type;
12732 +       union {
12733 +               const void                              *dy_hop;
12734 +               const struct address_space_operations   *dy_haop;
12735 +       };
12736 +};
12737 +
12738 +struct au_dykey {
12739 +       union {
12740 +               struct hlist_bl_node    dk_hnode;
12741 +               struct rcu_head         dk_rcu;
12742 +       };
12743 +       struct au_dynop         dk_op;
12744 +
12745 +       /*
12746 +        * during I am in the branch local array, kref is gotten. when the
12747 +        * branch is removed, kref is put.
12748 +        */
12749 +       struct kref             dk_kref;
12750 +};
12751 +
12752 +/* stop unioning since their sizes are very different from each other */
12753 +struct au_dyaop {
12754 +       struct au_dykey                 da_key;
12755 +       struct address_space_operations da_op; /* not const */
12756 +};
12757 +/* make sure that 'struct au_dykey *' can be any type */
12758 +static_assert(!offsetof(struct au_dyaop, da_key));
12759 +
12760 +/* ---------------------------------------------------------------------- */
12761 +
12762 +/* dynop.c */
12763 +struct au_branch;
12764 +void au_dy_put(struct au_dykey *key);
12765 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12766 +               struct inode *h_inode);
12767 +int au_dy_irefresh(struct inode *inode);
12768 +void au_dy_arefresh(int do_dio);
12769 +
12770 +void __init au_dy_init(void);
12771 +void au_dy_fin(void);
12772 +
12773 +#endif /* __KERNEL__ */
12774 +#endif /* __AUFS_DYNOP_H__ */
12775 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12776 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12777 +++ linux/fs/aufs/export.c      2021-02-24 13:33:42.744347058 +0100
12778 @@ -0,0 +1,837 @@
12779 +// SPDX-License-Identifier: GPL-2.0
12780 +/*
12781 + * Copyright (C) 2005-2020 Junjiro R. Okajima
12782 + *
12783 + * This program, aufs is free software; you can redistribute it and/or modify
12784 + * it under the terms of the GNU General Public License as published by
12785 + * the Free Software Foundation; either version 2 of the License, or
12786 + * (at your option) any later version.
12787 + *
12788 + * This program is distributed in the hope that it will be useful,
12789 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12790 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12791 + * GNU General Public License for more details.
12792 + *
12793 + * You should have received a copy of the GNU General Public License
12794 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12795 + */
12796 +
12797 +/*
12798 + * export via nfs
12799 + */
12800 +
12801 +#include <linux/exportfs.h>
12802 +#include <linux/fs_struct.h>
12803 +#include <linux/namei.h>
12804 +#include <linux/nsproxy.h>
12805 +#include <linux/random.h>
12806 +#include <linux/writeback.h>
12807 +#include "aufs.h"
12808 +
12809 +union conv {
12810 +#ifdef CONFIG_AUFS_INO_T_64
12811 +       __u32 a[2];
12812 +#else
12813 +       __u32 a[1];
12814 +#endif
12815 +       ino_t ino;
12816 +};
12817 +
12818 +static ino_t decode_ino(__u32 *a)
12819 +{
12820 +       union conv u;
12821 +
12822 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12823 +       u.a[0] = a[0];
12824 +#ifdef CONFIG_AUFS_INO_T_64
12825 +       u.a[1] = a[1];
12826 +#endif
12827 +       return u.ino;
12828 +}
12829 +
12830 +static void encode_ino(__u32 *a, ino_t ino)
12831 +{
12832 +       union conv u;
12833 +
12834 +       u.ino = ino;
12835 +       a[0] = u.a[0];
12836 +#ifdef CONFIG_AUFS_INO_T_64
12837 +       a[1] = u.a[1];
12838 +#endif
12839 +}
12840 +
12841 +/* NFS file handle */
12842 +enum {
12843 +       Fh_br_id,
12844 +       Fh_sigen,
12845 +#ifdef CONFIG_AUFS_INO_T_64
12846 +       /* support 64bit inode number */
12847 +       Fh_ino1,
12848 +       Fh_ino2,
12849 +       Fh_dir_ino1,
12850 +       Fh_dir_ino2,
12851 +#else
12852 +       Fh_ino1,
12853 +       Fh_dir_ino1,
12854 +#endif
12855 +       Fh_igen,
12856 +       Fh_h_type,
12857 +       Fh_tail,
12858 +
12859 +       Fh_ino = Fh_ino1,
12860 +       Fh_dir_ino = Fh_dir_ino1
12861 +};
12862 +
12863 +static int au_test_anon(struct dentry *dentry)
12864 +{
12865 +       /* note: read d_flags without d_lock */
12866 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12867 +}
12868 +
12869 +int au_test_nfsd(void)
12870 +{
12871 +       int ret;
12872 +       struct task_struct *tsk = current;
12873 +       char comm[sizeof(tsk->comm)];
12874 +
12875 +       ret = 0;
12876 +       if (tsk->flags & PF_KTHREAD) {
12877 +               get_task_comm(comm, tsk);
12878 +               ret = !strcmp(comm, "nfsd");
12879 +       }
12880 +
12881 +       return ret;
12882 +}
12883 +
12884 +/* ---------------------------------------------------------------------- */
12885 +/* inode generation external table */
12886 +
12887 +void au_xigen_inc(struct inode *inode)
12888 +{
12889 +       loff_t pos;
12890 +       ssize_t sz;
12891 +       __u32 igen;
12892 +       struct super_block *sb;
12893 +       struct au_sbinfo *sbinfo;
12894 +
12895 +       sb = inode->i_sb;
12896 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12897 +
12898 +       sbinfo = au_sbi(sb);
12899 +       pos = inode->i_ino;
12900 +       pos *= sizeof(igen);
12901 +       igen = inode->i_generation + 1;
12902 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12903 +       if (sz == sizeof(igen))
12904 +               return; /* success */
12905 +
12906 +       if (unlikely(sz >= 0))
12907 +               AuIOErr("xigen error (%zd)\n", sz);
12908 +}
12909 +
12910 +int au_xigen_new(struct inode *inode)
12911 +{
12912 +       int err;
12913 +       loff_t pos;
12914 +       ssize_t sz;
12915 +       struct super_block *sb;
12916 +       struct au_sbinfo *sbinfo;
12917 +       struct file *file;
12918 +
12919 +       err = 0;
12920 +       /* todo: dirty, at mount time */
12921 +       if (inode->i_ino == AUFS_ROOT_INO)
12922 +               goto out;
12923 +       sb = inode->i_sb;
12924 +       SiMustAnyLock(sb);
12925 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12926 +               goto out;
12927 +
12928 +       err = -EFBIG;
12929 +       pos = inode->i_ino;
12930 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12931 +               AuIOErr1("too large i%lld\n", pos);
12932 +               goto out;
12933 +       }
12934 +       pos *= sizeof(inode->i_generation);
12935 +
12936 +       err = 0;
12937 +       sbinfo = au_sbi(sb);
12938 +       file = sbinfo->si_xigen;
12939 +       BUG_ON(!file);
12940 +
12941 +       if (vfsub_f_size_read(file)
12942 +           < pos + sizeof(inode->i_generation)) {
12943 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12944 +               sz = xino_fwrite(file, &inode->i_generation,
12945 +                                sizeof(inode->i_generation), &pos);
12946 +       } else
12947 +               sz = xino_fread(file, &inode->i_generation,
12948 +                               sizeof(inode->i_generation), &pos);
12949 +       if (sz == sizeof(inode->i_generation))
12950 +               goto out; /* success */
12951 +
12952 +       err = sz;
12953 +       if (unlikely(sz >= 0)) {
12954 +               err = -EIO;
12955 +               AuIOErr("xigen error (%zd)\n", sz);
12956 +       }
12957 +
12958 +out:
12959 +       return err;
12960 +}
12961 +
12962 +int au_xigen_set(struct super_block *sb, struct path *path)
12963 +{
12964 +       int err;
12965 +       struct au_sbinfo *sbinfo;
12966 +       struct file *file;
12967 +
12968 +       SiMustWriteLock(sb);
12969 +
12970 +       sbinfo = au_sbi(sb);
12971 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12972 +       err = PTR_ERR(file);
12973 +       if (IS_ERR(file))
12974 +               goto out;
12975 +       err = 0;
12976 +       if (sbinfo->si_xigen)
12977 +               fput(sbinfo->si_xigen);
12978 +       sbinfo->si_xigen = file;
12979 +
12980 +out:
12981 +       AuTraceErr(err);
12982 +       return err;
12983 +}
12984 +
12985 +void au_xigen_clr(struct super_block *sb)
12986 +{
12987 +       struct au_sbinfo *sbinfo;
12988 +
12989 +       SiMustWriteLock(sb);
12990 +
12991 +       sbinfo = au_sbi(sb);
12992 +       if (sbinfo->si_xigen) {
12993 +               fput(sbinfo->si_xigen);
12994 +               sbinfo->si_xigen = NULL;
12995 +       }
12996 +}
12997 +
12998 +/* ---------------------------------------------------------------------- */
12999 +
13000 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13001 +                                   ino_t dir_ino)
13002 +{
13003 +       struct dentry *dentry, *d;
13004 +       struct inode *inode;
13005 +       unsigned int sigen;
13006 +
13007 +       dentry = NULL;
13008 +       inode = ilookup(sb, ino);
13009 +       if (!inode)
13010 +               goto out;
13011 +
13012 +       dentry = ERR_PTR(-ESTALE);
13013 +       sigen = au_sigen(sb);
13014 +       if (unlikely(au_is_bad_inode(inode)
13015 +                    || IS_DEADDIR(inode)
13016 +                    || sigen != au_iigen(inode, NULL)))
13017 +               goto out_iput;
13018 +
13019 +       dentry = NULL;
13020 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13021 +               dentry = d_find_alias(inode);
13022 +       else {
13023 +               spin_lock(&inode->i_lock);
13024 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13025 +                       spin_lock(&d->d_lock);
13026 +                       if (!au_test_anon(d)
13027 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13028 +                               dentry = dget_dlock(d);
13029 +                               spin_unlock(&d->d_lock);
13030 +                               break;
13031 +                       }
13032 +                       spin_unlock(&d->d_lock);
13033 +               }
13034 +               spin_unlock(&inode->i_lock);
13035 +       }
13036 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13037 +               /* need to refresh */
13038 +               dput(dentry);
13039 +               dentry = NULL;
13040 +       }
13041 +
13042 +out_iput:
13043 +       iput(inode);
13044 +out:
13045 +       AuTraceErrPtr(dentry);
13046 +       return dentry;
13047 +}
13048 +
13049 +/* ---------------------------------------------------------------------- */
13050 +
13051 +/* todo: dirty? */
13052 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13053 +
13054 +struct au_compare_mnt_args {
13055 +       /* input */
13056 +       struct super_block *sb;
13057 +
13058 +       /* output */
13059 +       struct vfsmount *mnt;
13060 +};
13061 +
13062 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13063 +{
13064 +       struct au_compare_mnt_args *a = arg;
13065 +
13066 +       if (mnt->mnt_sb != a->sb)
13067 +               return 0;
13068 +       a->mnt = mntget(mnt);
13069 +       return 1;
13070 +}
13071 +
13072 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13073 +{
13074 +       int err;
13075 +       struct path root;
13076 +       struct au_compare_mnt_args args = {
13077 +               .sb = sb
13078 +       };
13079 +
13080 +       get_fs_root(current->fs, &root);
13081 +       rcu_read_lock();
13082 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13083 +       rcu_read_unlock();
13084 +       path_put(&root);
13085 +       AuDebugOn(!err);
13086 +       AuDebugOn(!args.mnt);
13087 +       return args.mnt;
13088 +}
13089 +
13090 +struct au_nfsd_si_lock {
13091 +       unsigned int sigen;
13092 +       aufs_bindex_t bindex, br_id;
13093 +       unsigned char force_lock;
13094 +};
13095 +
13096 +static int si_nfsd_read_lock(struct super_block *sb,
13097 +                            struct au_nfsd_si_lock *nsi_lock)
13098 +{
13099 +       int err;
13100 +       aufs_bindex_t bindex;
13101 +
13102 +       si_read_lock(sb, AuLock_FLUSH);
13103 +
13104 +       /* branch id may be wrapped around */
13105 +       err = 0;
13106 +       bindex = au_br_index(sb, nsi_lock->br_id);
13107 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13108 +               goto out; /* success */
13109 +
13110 +       err = -ESTALE;
13111 +       bindex = -1;
13112 +       if (!nsi_lock->force_lock)
13113 +               si_read_unlock(sb);
13114 +
13115 +out:
13116 +       nsi_lock->bindex = bindex;
13117 +       return err;
13118 +}
13119 +
13120 +struct find_name_by_ino {
13121 +       struct dir_context ctx;
13122 +       int called, found;
13123 +       ino_t ino;
13124 +       char *name;
13125 +       int namelen;
13126 +};
13127 +
13128 +static int
13129 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13130 +                loff_t offset, u64 ino, unsigned int d_type)
13131 +{
13132 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13133 +                                                 ctx);
13134 +
13135 +       a->called++;
13136 +       if (a->ino != ino)
13137 +               return 0;
13138 +
13139 +       memcpy(a->name, name, namelen);
13140 +       a->namelen = namelen;
13141 +       a->found = 1;
13142 +       return 1;
13143 +}
13144 +
13145 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13146 +                                    struct au_nfsd_si_lock *nsi_lock)
13147 +{
13148 +       struct dentry *dentry, *parent;
13149 +       struct file *file;
13150 +       struct inode *dir;
13151 +       struct find_name_by_ino arg = {
13152 +               .ctx = {
13153 +                       .actor = find_name_by_ino
13154 +               }
13155 +       };
13156 +       int err;
13157 +
13158 +       parent = path->dentry;
13159 +       if (nsi_lock)
13160 +               si_read_unlock(parent->d_sb);
13161 +       file = vfsub_dentry_open(path, au_dir_roflags);
13162 +       dentry = (void *)file;
13163 +       if (IS_ERR(file))
13164 +               goto out;
13165 +
13166 +       dentry = ERR_PTR(-ENOMEM);
13167 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13168 +       if (unlikely(!arg.name))
13169 +               goto out_file;
13170 +       arg.ino = ino;
13171 +       arg.found = 0;
13172 +       do {
13173 +               arg.called = 0;
13174 +               /* smp_mb(); */
13175 +               err = vfsub_iterate_dir(file, &arg.ctx);
13176 +       } while (!err && !arg.found && arg.called);
13177 +       dentry = ERR_PTR(err);
13178 +       if (unlikely(err))
13179 +               goto out_name;
13180 +       /* instead of ENOENT */
13181 +       dentry = ERR_PTR(-ESTALE);
13182 +       if (!arg.found)
13183 +               goto out_name;
13184 +
13185 +       /* do not call vfsub_lkup_one() */
13186 +       dir = d_inode(parent);
13187 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13188 +       AuTraceErrPtr(dentry);
13189 +       if (IS_ERR(dentry))
13190 +               goto out_name;
13191 +       AuDebugOn(au_test_anon(dentry));
13192 +       if (unlikely(d_really_is_negative(dentry))) {
13193 +               dput(dentry);
13194 +               dentry = ERR_PTR(-ENOENT);
13195 +       }
13196 +
13197 +out_name:
13198 +       free_page((unsigned long)arg.name);
13199 +out_file:
13200 +       fput(file);
13201 +out:
13202 +       if (unlikely(nsi_lock
13203 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13204 +               if (!IS_ERR(dentry)) {
13205 +                       dput(dentry);
13206 +                       dentry = ERR_PTR(-ESTALE);
13207 +               }
13208 +       AuTraceErrPtr(dentry);
13209 +       return dentry;
13210 +}
13211 +
13212 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13213 +                                       ino_t dir_ino,
13214 +                                       struct au_nfsd_si_lock *nsi_lock)
13215 +{
13216 +       struct dentry *dentry;
13217 +       struct path path;
13218 +
13219 +       if (dir_ino != AUFS_ROOT_INO) {
13220 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13221 +               dentry = path.dentry;
13222 +               if (!path.dentry || IS_ERR(path.dentry))
13223 +                       goto out;
13224 +               AuDebugOn(au_test_anon(path.dentry));
13225 +       } else
13226 +               path.dentry = dget(sb->s_root);
13227 +
13228 +       path.mnt = au_mnt_get(sb);
13229 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13230 +       path_put(&path);
13231 +
13232 +out:
13233 +       AuTraceErrPtr(dentry);
13234 +       return dentry;
13235 +}
13236 +
13237 +/* ---------------------------------------------------------------------- */
13238 +
13239 +static int h_acceptable(void *expv, struct dentry *dentry)
13240 +{
13241 +       return 1;
13242 +}
13243 +
13244 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13245 +                          char *buf, int len, struct super_block *sb)
13246 +{
13247 +       char *p;
13248 +       int n;
13249 +       struct path path;
13250 +
13251 +       p = d_path(h_rootpath, buf, len);
13252 +       if (IS_ERR(p))
13253 +               goto out;
13254 +       n = strlen(p);
13255 +
13256 +       path.mnt = h_rootpath->mnt;
13257 +       path.dentry = h_parent;
13258 +       p = d_path(&path, buf, len);
13259 +       if (IS_ERR(p))
13260 +               goto out;
13261 +       if (n != 1)
13262 +               p += n;
13263 +
13264 +       path.mnt = au_mnt_get(sb);
13265 +       path.dentry = sb->s_root;
13266 +       p = d_path(&path, buf, len - strlen(p));
13267 +       mntput(path.mnt);
13268 +       if (IS_ERR(p))
13269 +               goto out;
13270 +       if (n != 1)
13271 +               p[strlen(p)] = '/';
13272 +
13273 +out:
13274 +       AuTraceErrPtr(p);
13275 +       return p;
13276 +}
13277 +
13278 +static
13279 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13280 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13281 +{
13282 +       struct dentry *dentry, *h_parent, *root;
13283 +       struct super_block *h_sb;
13284 +       char *pathname, *p;
13285 +       struct vfsmount *h_mnt;
13286 +       struct au_branch *br;
13287 +       int err;
13288 +       struct path path;
13289 +
13290 +       br = au_sbr(sb, nsi_lock->bindex);
13291 +       h_mnt = au_br_mnt(br);
13292 +       h_sb = h_mnt->mnt_sb;
13293 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13294 +       lockdep_off();
13295 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13296 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13297 +                                     h_acceptable, /*context*/NULL);
13298 +       lockdep_on();
13299 +       dentry = h_parent;
13300 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13301 +               AuWarn1("%s decode_fh failed, %ld\n",
13302 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13303 +               goto out;
13304 +       }
13305 +       dentry = NULL;
13306 +       if (unlikely(au_test_anon(h_parent))) {
13307 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13308 +                       au_sbtype(h_sb));
13309 +               goto out_h_parent;
13310 +       }
13311 +
13312 +       dentry = ERR_PTR(-ENOMEM);
13313 +       pathname = (void *)__get_free_page(GFP_NOFS);
13314 +       if (unlikely(!pathname))
13315 +               goto out_h_parent;
13316 +
13317 +       root = sb->s_root;
13318 +       path.mnt = h_mnt;
13319 +       di_read_lock_parent(root, !AuLock_IR);
13320 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13321 +       di_read_unlock(root, !AuLock_IR);
13322 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13323 +       dentry = (void *)p;
13324 +       if (IS_ERR(p))
13325 +               goto out_pathname;
13326 +
13327 +       si_read_unlock(sb);
13328 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13329 +       dentry = ERR_PTR(err);
13330 +       if (unlikely(err))
13331 +               goto out_relock;
13332 +
13333 +       dentry = ERR_PTR(-ENOENT);
13334 +       AuDebugOn(au_test_anon(path.dentry));
13335 +       if (unlikely(d_really_is_negative(path.dentry)))
13336 +               goto out_path;
13337 +
13338 +       if (ino != d_inode(path.dentry)->i_ino)
13339 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13340 +       else
13341 +               dentry = dget(path.dentry);
13342 +
13343 +out_path:
13344 +       path_put(&path);
13345 +out_relock:
13346 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13347 +               if (!IS_ERR(dentry)) {
13348 +                       dput(dentry);
13349 +                       dentry = ERR_PTR(-ESTALE);
13350 +               }
13351 +out_pathname:
13352 +       free_page((unsigned long)pathname);
13353 +out_h_parent:
13354 +       dput(h_parent);
13355 +out:
13356 +       AuTraceErrPtr(dentry);
13357 +       return dentry;
13358 +}
13359 +
13360 +/* ---------------------------------------------------------------------- */
13361 +
13362 +static struct dentry *
13363 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13364 +                 int fh_type)
13365 +{
13366 +       struct dentry *dentry;
13367 +       __u32 *fh = fid->raw;
13368 +       struct au_branch *br;
13369 +       ino_t ino, dir_ino;
13370 +       struct au_nfsd_si_lock nsi_lock = {
13371 +               .force_lock     = 0
13372 +       };
13373 +
13374 +       dentry = ERR_PTR(-ESTALE);
13375 +       /* it should never happen, but the file handle is unreliable */
13376 +       if (unlikely(fh_len < Fh_tail))
13377 +               goto out;
13378 +       nsi_lock.sigen = fh[Fh_sigen];
13379 +       nsi_lock.br_id = fh[Fh_br_id];
13380 +
13381 +       /* branch id may be wrapped around */
13382 +       br = NULL;
13383 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13384 +               goto out;
13385 +       nsi_lock.force_lock = 1;
13386 +
13387 +       /* is this inode still cached? */
13388 +       ino = decode_ino(fh + Fh_ino);
13389 +       /* it should never happen */
13390 +       if (unlikely(ino == AUFS_ROOT_INO))
13391 +               goto out_unlock;
13392 +
13393 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13394 +       dentry = decode_by_ino(sb, ino, dir_ino);
13395 +       if (IS_ERR(dentry))
13396 +               goto out_unlock;
13397 +       if (dentry)
13398 +               goto accept;
13399 +
13400 +       /* is the parent dir cached? */
13401 +       br = au_sbr(sb, nsi_lock.bindex);
13402 +       au_lcnt_inc(&br->br_nfiles);
13403 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13404 +       if (IS_ERR(dentry))
13405 +               goto out_unlock;
13406 +       if (dentry)
13407 +               goto accept;
13408 +
13409 +       /* lookup path */
13410 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13411 +       if (IS_ERR(dentry))
13412 +               goto out_unlock;
13413 +       if (unlikely(!dentry))
13414 +               /* todo?: make it ESTALE */
13415 +               goto out_unlock;
13416 +
13417 +accept:
13418 +       if (!au_digen_test(dentry, au_sigen(sb))
13419 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13420 +               goto out_unlock; /* success */
13421 +
13422 +       dput(dentry);
13423 +       dentry = ERR_PTR(-ESTALE);
13424 +out_unlock:
13425 +       if (br)
13426 +               au_lcnt_dec(&br->br_nfiles);
13427 +       si_read_unlock(sb);
13428 +out:
13429 +       AuTraceErrPtr(dentry);
13430 +       return dentry;
13431 +}
13432 +
13433 +#if 0 /* reserved for future use */
13434 +/* support subtreecheck option */
13435 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13436 +                                       int fh_len, int fh_type)
13437 +{
13438 +       struct dentry *parent;
13439 +       __u32 *fh = fid->raw;
13440 +       ino_t dir_ino;
13441 +
13442 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13443 +       parent = decode_by_ino(sb, dir_ino, 0);
13444 +       if (IS_ERR(parent))
13445 +               goto out;
13446 +       if (!parent)
13447 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13448 +                                       dir_ino, fh, fh_len);
13449 +
13450 +out:
13451 +       AuTraceErrPtr(parent);
13452 +       return parent;
13453 +}
13454 +#endif
13455 +
13456 +/* ---------------------------------------------------------------------- */
13457 +
13458 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13459 +                         struct inode *dir)
13460 +{
13461 +       int err;
13462 +       aufs_bindex_t bindex;
13463 +       struct super_block *sb, *h_sb;
13464 +       struct dentry *dentry, *parent, *h_parent;
13465 +       struct inode *h_dir;
13466 +       struct au_branch *br;
13467 +
13468 +       err = -ENOSPC;
13469 +       if (unlikely(*max_len <= Fh_tail)) {
13470 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13471 +               goto out;
13472 +       }
13473 +
13474 +       err = FILEID_ROOT;
13475 +       if (inode->i_ino == AUFS_ROOT_INO) {
13476 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13477 +               goto out;
13478 +       }
13479 +
13480 +       h_parent = NULL;
13481 +       sb = inode->i_sb;
13482 +       err = si_read_lock(sb, AuLock_FLUSH);
13483 +       if (unlikely(err))
13484 +               goto out;
13485 +
13486 +#ifdef CONFIG_AUFS_DEBUG
13487 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13488 +               AuWarn1("NFS-exporting requires xino\n");
13489 +#endif
13490 +       err = -EIO;
13491 +       parent = NULL;
13492 +       ii_read_lock_child(inode);
13493 +       bindex = au_ibtop(inode);
13494 +       if (!dir) {
13495 +               dentry = d_find_any_alias(inode);
13496 +               if (unlikely(!dentry))
13497 +                       goto out_unlock;
13498 +               AuDebugOn(au_test_anon(dentry));
13499 +               parent = dget_parent(dentry);
13500 +               dput(dentry);
13501 +               if (unlikely(!parent))
13502 +                       goto out_unlock;
13503 +               if (d_really_is_positive(parent))
13504 +                       dir = d_inode(parent);
13505 +       }
13506 +
13507 +       ii_read_lock_parent(dir);
13508 +       h_dir = au_h_iptr(dir, bindex);
13509 +       ii_read_unlock(dir);
13510 +       if (unlikely(!h_dir))
13511 +               goto out_parent;
13512 +       h_parent = d_find_any_alias(h_dir);
13513 +       if (unlikely(!h_parent))
13514 +               goto out_hparent;
13515 +
13516 +       err = -EPERM;
13517 +       br = au_sbr(sb, bindex);
13518 +       h_sb = au_br_sb(br);
13519 +       if (unlikely(!h_sb->s_export_op)) {
13520 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13521 +               goto out_hparent;
13522 +       }
13523 +
13524 +       fh[Fh_br_id] = br->br_id;
13525 +       fh[Fh_sigen] = au_sigen(sb);
13526 +       encode_ino(fh + Fh_ino, inode->i_ino);
13527 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13528 +       fh[Fh_igen] = inode->i_generation;
13529 +
13530 +       *max_len -= Fh_tail;
13531 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13532 +                                          max_len,
13533 +                                          /*connectable or subtreecheck*/0);
13534 +       err = fh[Fh_h_type];
13535 +       *max_len += Fh_tail;
13536 +       /* todo: macros? */
13537 +       if (err != FILEID_INVALID)
13538 +               err = 99;
13539 +       else
13540 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13541 +
13542 +out_hparent:
13543 +       dput(h_parent);
13544 +out_parent:
13545 +       dput(parent);
13546 +out_unlock:
13547 +       ii_read_unlock(inode);
13548 +       si_read_unlock(sb);
13549 +out:
13550 +       if (unlikely(err < 0))
13551 +               err = FILEID_INVALID;
13552 +       return err;
13553 +}
13554 +
13555 +/* ---------------------------------------------------------------------- */
13556 +
13557 +static int aufs_commit_metadata(struct inode *inode)
13558 +{
13559 +       int err;
13560 +       aufs_bindex_t bindex;
13561 +       struct super_block *sb;
13562 +       struct inode *h_inode;
13563 +       int (*f)(struct inode *inode);
13564 +
13565 +       sb = inode->i_sb;
13566 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13567 +       ii_write_lock_child(inode);
13568 +       bindex = au_ibtop(inode);
13569 +       AuDebugOn(bindex < 0);
13570 +       h_inode = au_h_iptr(inode, bindex);
13571 +
13572 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13573 +       if (f)
13574 +               err = f(h_inode);
13575 +       else {
13576 +               struct writeback_control wbc = {
13577 +                       .sync_mode      = WB_SYNC_ALL,
13578 +                       .nr_to_write    = 0 /* metadata only */
13579 +               };
13580 +
13581 +               err = sync_inode(h_inode, &wbc);
13582 +       }
13583 +
13584 +       au_cpup_attr_timesizes(inode);
13585 +       ii_write_unlock(inode);
13586 +       si_read_unlock(sb);
13587 +       return err;
13588 +}
13589 +
13590 +/* ---------------------------------------------------------------------- */
13591 +
13592 +static struct export_operations aufs_export_op = {
13593 +       .fh_to_dentry           = aufs_fh_to_dentry,
13594 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13595 +       .encode_fh              = aufs_encode_fh,
13596 +       .commit_metadata        = aufs_commit_metadata
13597 +};
13598 +
13599 +void au_export_init(struct super_block *sb)
13600 +{
13601 +       struct au_sbinfo *sbinfo;
13602 +       __u32 u;
13603 +
13604 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13605 +                        && IS_MODULE(CONFIG_EXPORTFS),
13606 +                        AUFS_NAME ": unsupported configuration "
13607 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13608 +
13609 +       sb->s_export_op = &aufs_export_op;
13610 +       sbinfo = au_sbi(sb);
13611 +       sbinfo->si_xigen = NULL;
13612 +       get_random_bytes(&u, sizeof(u));
13613 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13614 +       atomic_set(&sbinfo->si_xigen_next, u);
13615 +}
13616 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13617 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13618 +++ linux/fs/aufs/fhsm.c        2021-02-24 13:33:42.744347058 +0100
13619 @@ -0,0 +1,427 @@
13620 +// SPDX-License-Identifier: GPL-2.0
13621 +/*
13622 + * Copyright (C) 2011-2020 Junjiro R. Okajima
13623 + *
13624 + * This program, aufs is free software; you can redistribute it and/or modify
13625 + * it under the terms of the GNU General Public License as published by
13626 + * the Free Software Foundation; either version 2 of the License, or
13627 + * (at your option) any later version.
13628 + *
13629 + * This program is distributed in the hope that it will be useful,
13630 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13631 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13632 + * GNU General Public License for more details.
13633 + *
13634 + * You should have received a copy of the GNU General Public License
13635 + * along with this program; if not, write to the Free Software
13636 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13637 + */
13638 +
13639 +/*
13640 + * File-based Hierarchy Storage Management
13641 + */
13642 +
13643 +#include <linux/anon_inodes.h>
13644 +#include <linux/poll.h>
13645 +#include <linux/seq_file.h>
13646 +#include <linux/statfs.h>
13647 +#include "aufs.h"
13648 +
13649 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13650 +{
13651 +       struct au_sbinfo *sbinfo;
13652 +       struct au_fhsm *fhsm;
13653 +
13654 +       SiMustAnyLock(sb);
13655 +
13656 +       sbinfo = au_sbi(sb);
13657 +       fhsm = &sbinfo->si_fhsm;
13658 +       AuDebugOn(!fhsm);
13659 +       return fhsm->fhsm_bottom;
13660 +}
13661 +
13662 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13663 +{
13664 +       struct au_sbinfo *sbinfo;
13665 +       struct au_fhsm *fhsm;
13666 +
13667 +       SiMustWriteLock(sb);
13668 +
13669 +       sbinfo = au_sbi(sb);
13670 +       fhsm = &sbinfo->si_fhsm;
13671 +       AuDebugOn(!fhsm);
13672 +       fhsm->fhsm_bottom = bindex;
13673 +}
13674 +
13675 +/* ---------------------------------------------------------------------- */
13676 +
13677 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13678 +{
13679 +       struct au_br_fhsm *bf;
13680 +
13681 +       bf = br->br_fhsm;
13682 +       MtxMustLock(&bf->bf_lock);
13683 +
13684 +       return !bf->bf_readable
13685 +               || time_after(jiffies,
13686 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13687 +}
13688 +
13689 +/* ---------------------------------------------------------------------- */
13690 +
13691 +static void au_fhsm_notify(struct super_block *sb, int val)
13692 +{
13693 +       struct au_sbinfo *sbinfo;
13694 +       struct au_fhsm *fhsm;
13695 +
13696 +       SiMustAnyLock(sb);
13697 +
13698 +       sbinfo = au_sbi(sb);
13699 +       fhsm = &sbinfo->si_fhsm;
13700 +       if (au_fhsm_pid(fhsm)
13701 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13702 +               atomic_set(&fhsm->fhsm_readable, val);
13703 +               if (val)
13704 +                       wake_up(&fhsm->fhsm_wqh);
13705 +       }
13706 +}
13707 +
13708 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13709 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13710 +{
13711 +       int err;
13712 +       struct au_branch *br;
13713 +       struct au_br_fhsm *bf;
13714 +
13715 +       br = au_sbr(sb, bindex);
13716 +       AuDebugOn(au_br_rdonly(br));
13717 +       bf = br->br_fhsm;
13718 +       AuDebugOn(!bf);
13719 +
13720 +       if (do_lock)
13721 +               mutex_lock(&bf->bf_lock);
13722 +       else
13723 +               MtxMustLock(&bf->bf_lock);
13724 +
13725 +       /* sb->s_root for NFS is unreliable */
13726 +       err = au_br_stfs(br, &bf->bf_stfs);
13727 +       if (unlikely(err)) {
13728 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13729 +               goto out;
13730 +       }
13731 +
13732 +       bf->bf_jiffy = jiffies;
13733 +       bf->bf_readable = 1;
13734 +       if (do_notify)
13735 +               au_fhsm_notify(sb, /*val*/1);
13736 +       if (rstfs)
13737 +               *rstfs = bf->bf_stfs;
13738 +
13739 +out:
13740 +       if (do_lock)
13741 +               mutex_unlock(&bf->bf_lock);
13742 +       au_fhsm_notify(sb, /*val*/1);
13743 +
13744 +       return err;
13745 +}
13746 +
13747 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13748 +{
13749 +       int err;
13750 +       struct au_sbinfo *sbinfo;
13751 +       struct au_fhsm *fhsm;
13752 +       struct au_branch *br;
13753 +       struct au_br_fhsm *bf;
13754 +
13755 +       AuDbg("b%d, force %d\n", bindex, force);
13756 +       SiMustAnyLock(sb);
13757 +
13758 +       sbinfo = au_sbi(sb);
13759 +       fhsm = &sbinfo->si_fhsm;
13760 +       if (!au_ftest_si(sbinfo, FHSM)
13761 +           || fhsm->fhsm_bottom == bindex)
13762 +               return;
13763 +
13764 +       br = au_sbr(sb, bindex);
13765 +       bf = br->br_fhsm;
13766 +       AuDebugOn(!bf);
13767 +       mutex_lock(&bf->bf_lock);
13768 +       if (force
13769 +           || au_fhsm_pid(fhsm)
13770 +           || au_fhsm_test_jiffy(sbinfo, br))
13771 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13772 +                                 /*do_notify*/1);
13773 +       mutex_unlock(&bf->bf_lock);
13774 +}
13775 +
13776 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13777 +{
13778 +       aufs_bindex_t bindex, bbot;
13779 +       struct au_branch *br;
13780 +
13781 +       /* exclude the bottom */
13782 +       bbot = au_fhsm_bottom(sb);
13783 +       for (bindex = 0; bindex < bbot; bindex++) {
13784 +               br = au_sbr(sb, bindex);
13785 +               if (au_br_fhsm(br->br_perm))
13786 +                       au_fhsm_wrote(sb, bindex, force);
13787 +       }
13788 +}
13789 +
13790 +/* ---------------------------------------------------------------------- */
13791 +
13792 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13793 +{
13794 +       __poll_t mask;
13795 +       struct au_sbinfo *sbinfo;
13796 +       struct au_fhsm *fhsm;
13797 +
13798 +       mask = 0;
13799 +       sbinfo = file->private_data;
13800 +       fhsm = &sbinfo->si_fhsm;
13801 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13802 +       if (atomic_read(&fhsm->fhsm_readable))
13803 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13804 +
13805 +       if (!mask)
13806 +               AuDbg("mask 0x%x\n", mask);
13807 +       return mask;
13808 +}
13809 +
13810 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13811 +                             struct aufs_stfs *stfs, __s16 brid)
13812 +{
13813 +       int err;
13814 +
13815 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13816 +       if (!err)
13817 +               err = __put_user(brid, &stbr->brid);
13818 +       if (unlikely(err))
13819 +               err = -EFAULT;
13820 +
13821 +       return err;
13822 +}
13823 +
13824 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13825 +                              struct aufs_stbr __user *stbr, size_t count)
13826 +{
13827 +       ssize_t err;
13828 +       int nstbr;
13829 +       aufs_bindex_t bindex, bbot;
13830 +       struct au_branch *br;
13831 +       struct au_br_fhsm *bf;
13832 +
13833 +       /* except the bottom branch */
13834 +       err = 0;
13835 +       nstbr = 0;
13836 +       bbot = au_fhsm_bottom(sb);
13837 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13838 +               br = au_sbr(sb, bindex);
13839 +               if (!au_br_fhsm(br->br_perm))
13840 +                       continue;
13841 +
13842 +               bf = br->br_fhsm;
13843 +               mutex_lock(&bf->bf_lock);
13844 +               if (bf->bf_readable) {
13845 +                       err = -EFAULT;
13846 +                       if (count >= sizeof(*stbr))
13847 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13848 +                                                         br->br_id);
13849 +                       if (!err) {
13850 +                               bf->bf_readable = 0;
13851 +                               count -= sizeof(*stbr);
13852 +                               nstbr++;
13853 +                       }
13854 +               }
13855 +               mutex_unlock(&bf->bf_lock);
13856 +       }
13857 +       if (!err)
13858 +               err = sizeof(*stbr) * nstbr;
13859 +
13860 +       return err;
13861 +}
13862 +
13863 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13864 +                          loff_t *pos)
13865 +{
13866 +       ssize_t err;
13867 +       int readable;
13868 +       aufs_bindex_t nfhsm, bindex, bbot;
13869 +       struct au_sbinfo *sbinfo;
13870 +       struct au_fhsm *fhsm;
13871 +       struct au_branch *br;
13872 +       struct super_block *sb;
13873 +
13874 +       err = 0;
13875 +       sbinfo = file->private_data;
13876 +       fhsm = &sbinfo->si_fhsm;
13877 +need_data:
13878 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13879 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13880 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13881 +                       err = -EAGAIN;
13882 +               else
13883 +                       err = wait_event_interruptible_locked_irq
13884 +                               (fhsm->fhsm_wqh,
13885 +                                atomic_read(&fhsm->fhsm_readable));
13886 +       }
13887 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13888 +       if (unlikely(err))
13889 +               goto out;
13890 +
13891 +       /* sb may already be dead */
13892 +       au_rw_read_lock(&sbinfo->si_rwsem);
13893 +       readable = atomic_read(&fhsm->fhsm_readable);
13894 +       if (readable > 0) {
13895 +               sb = sbinfo->si_sb;
13896 +               AuDebugOn(!sb);
13897 +               /* exclude the bottom branch */
13898 +               nfhsm = 0;
13899 +               bbot = au_fhsm_bottom(sb);
13900 +               for (bindex = 0; bindex < bbot; bindex++) {
13901 +                       br = au_sbr(sb, bindex);
13902 +                       if (au_br_fhsm(br->br_perm))
13903 +                               nfhsm++;
13904 +               }
13905 +               err = -EMSGSIZE;
13906 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13907 +                       atomic_set(&fhsm->fhsm_readable, 0);
13908 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13909 +                                            count);
13910 +               }
13911 +       }
13912 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13913 +       if (!readable)
13914 +               goto need_data;
13915 +
13916 +out:
13917 +       return err;
13918 +}
13919 +
13920 +static int au_fhsm_release(struct inode *inode, struct file *file)
13921 +{
13922 +       struct au_sbinfo *sbinfo;
13923 +       struct au_fhsm *fhsm;
13924 +
13925 +       /* sb may already be dead */
13926 +       sbinfo = file->private_data;
13927 +       fhsm = &sbinfo->si_fhsm;
13928 +       spin_lock(&fhsm->fhsm_spin);
13929 +       fhsm->fhsm_pid = 0;
13930 +       spin_unlock(&fhsm->fhsm_spin);
13931 +       kobject_put(&sbinfo->si_kobj);
13932 +
13933 +       return 0;
13934 +}
13935 +
13936 +static const struct file_operations au_fhsm_fops = {
13937 +       .owner          = THIS_MODULE,
13938 +       .llseek         = noop_llseek,
13939 +       .read           = au_fhsm_read,
13940 +       .poll           = au_fhsm_poll,
13941 +       .release        = au_fhsm_release
13942 +};
13943 +
13944 +int au_fhsm_fd(struct super_block *sb, int oflags)
13945 +{
13946 +       int err, fd;
13947 +       struct au_sbinfo *sbinfo;
13948 +       struct au_fhsm *fhsm;
13949 +
13950 +       err = -EPERM;
13951 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13952 +               goto out;
13953 +
13954 +       err = -EINVAL;
13955 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13956 +               goto out;
13957 +
13958 +       err = 0;
13959 +       sbinfo = au_sbi(sb);
13960 +       fhsm = &sbinfo->si_fhsm;
13961 +       spin_lock(&fhsm->fhsm_spin);
13962 +       if (!fhsm->fhsm_pid)
13963 +               fhsm->fhsm_pid = current->pid;
13964 +       else
13965 +               err = -EBUSY;
13966 +       spin_unlock(&fhsm->fhsm_spin);
13967 +       if (unlikely(err))
13968 +               goto out;
13969 +
13970 +       oflags |= O_RDONLY;
13971 +       /* oflags |= FMODE_NONOTIFY; */
13972 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13973 +       err = fd;
13974 +       if (unlikely(fd < 0))
13975 +               goto out_pid;
13976 +
13977 +       /* succeed regardless 'fhsm' status */
13978 +       kobject_get(&sbinfo->si_kobj);
13979 +       si_noflush_read_lock(sb);
13980 +       if (au_ftest_si(sbinfo, FHSM))
13981 +               au_fhsm_wrote_all(sb, /*force*/0);
13982 +       si_read_unlock(sb);
13983 +       goto out; /* success */
13984 +
13985 +out_pid:
13986 +       spin_lock(&fhsm->fhsm_spin);
13987 +       fhsm->fhsm_pid = 0;
13988 +       spin_unlock(&fhsm->fhsm_spin);
13989 +out:
13990 +       AuTraceErr(err);
13991 +       return err;
13992 +}
13993 +
13994 +/* ---------------------------------------------------------------------- */
13995 +
13996 +int au_fhsm_br_alloc(struct au_branch *br)
13997 +{
13998 +       int err;
13999 +
14000 +       err = 0;
14001 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14002 +       if (br->br_fhsm)
14003 +               au_br_fhsm_init(br->br_fhsm);
14004 +       else
14005 +               err = -ENOMEM;
14006 +
14007 +       return err;
14008 +}
14009 +
14010 +/* ---------------------------------------------------------------------- */
14011 +
14012 +void au_fhsm_fin(struct super_block *sb)
14013 +{
14014 +       au_fhsm_notify(sb, /*val*/-1);
14015 +}
14016 +
14017 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14018 +{
14019 +       struct au_fhsm *fhsm;
14020 +
14021 +       fhsm = &sbinfo->si_fhsm;
14022 +       spin_lock_init(&fhsm->fhsm_spin);
14023 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14024 +       atomic_set(&fhsm->fhsm_readable, 0);
14025 +       fhsm->fhsm_expire
14026 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14027 +       fhsm->fhsm_bottom = -1;
14028 +}
14029 +
14030 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14031 +{
14032 +       sbinfo->si_fhsm.fhsm_expire
14033 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14034 +}
14035 +
14036 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14037 +{
14038 +       unsigned int u;
14039 +
14040 +       if (!au_ftest_si(sbinfo, FHSM))
14041 +               return;
14042 +
14043 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14044 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14045 +               seq_printf(seq, ",fhsm_sec=%u", u);
14046 +}
14047 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14048 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14049 +++ linux/fs/aufs/file.c        2021-02-24 13:33:42.744347058 +0100
14050 @@ -0,0 +1,863 @@
14051 +// SPDX-License-Identifier: GPL-2.0
14052 +/*
14053 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14054 + *
14055 + * This program, aufs is free software; you can redistribute it and/or modify
14056 + * it under the terms of the GNU General Public License as published by
14057 + * the Free Software Foundation; either version 2 of the License, or
14058 + * (at your option) any later version.
14059 + *
14060 + * This program is distributed in the hope that it will be useful,
14061 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14062 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14063 + * GNU General Public License for more details.
14064 + *
14065 + * You should have received a copy of the GNU General Public License
14066 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14067 + */
14068 +
14069 +/*
14070 + * handling file/dir, and address_space operation
14071 + */
14072 +
14073 +#ifdef CONFIG_AUFS_DEBUG
14074 +#include <linux/migrate.h>
14075 +#endif
14076 +#include <linux/pagemap.h>
14077 +#include "aufs.h"
14078 +
14079 +/* drop flags for writing */
14080 +unsigned int au_file_roflags(unsigned int flags)
14081 +{
14082 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14083 +       flags |= O_RDONLY | O_NOATIME;
14084 +       return flags;
14085 +}
14086 +
14087 +/* common functions to regular file and dir */
14088 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14089 +                      struct file *file, int force_wr)
14090 +{
14091 +       struct file *h_file;
14092 +       struct dentry *h_dentry;
14093 +       struct inode *h_inode;
14094 +       struct super_block *sb;
14095 +       struct au_branch *br;
14096 +       struct path h_path;
14097 +       int err;
14098 +
14099 +       /* a race condition can happen between open and unlink/rmdir */
14100 +       h_file = ERR_PTR(-ENOENT);
14101 +       h_dentry = au_h_dptr(dentry, bindex);
14102 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14103 +               goto out;
14104 +       h_inode = d_inode(h_dentry);
14105 +       spin_lock(&h_dentry->d_lock);
14106 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14107 +               /* || !d_inode(dentry)->i_nlink */
14108 +               ;
14109 +       spin_unlock(&h_dentry->d_lock);
14110 +       if (unlikely(err))
14111 +               goto out;
14112 +
14113 +       sb = dentry->d_sb;
14114 +       br = au_sbr(sb, bindex);
14115 +       err = au_br_test_oflag(flags, br);
14116 +       h_file = ERR_PTR(err);
14117 +       if (unlikely(err))
14118 +               goto out;
14119 +
14120 +       /* drop flags for writing */
14121 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14122 +               if (force_wr && !(flags & O_WRONLY))
14123 +                       force_wr = 0;
14124 +               flags = au_file_roflags(flags);
14125 +               if (force_wr) {
14126 +                       h_file = ERR_PTR(-EROFS);
14127 +                       flags = au_file_roflags(flags);
14128 +                       if (unlikely(vfsub_native_ro(h_inode)
14129 +                                    || IS_APPEND(h_inode)))
14130 +                               goto out;
14131 +                       flags &= ~O_ACCMODE;
14132 +                       flags |= O_WRONLY;
14133 +               }
14134 +       }
14135 +       flags &= ~O_CREAT;
14136 +       au_lcnt_inc(&br->br_nfiles);
14137 +       h_path.dentry = h_dentry;
14138 +       h_path.mnt = au_br_mnt(br);
14139 +       h_file = vfsub_dentry_open(&h_path, flags);
14140 +       if (IS_ERR(h_file))
14141 +               goto out_br;
14142 +
14143 +       if (flags & __FMODE_EXEC) {
14144 +               err = deny_write_access(h_file);
14145 +               if (unlikely(err)) {
14146 +                       fput(h_file);
14147 +                       h_file = ERR_PTR(err);
14148 +                       goto out_br;
14149 +               }
14150 +       }
14151 +       fsnotify_open(h_file);
14152 +       goto out; /* success */
14153 +
14154 +out_br:
14155 +       au_lcnt_dec(&br->br_nfiles);
14156 +out:
14157 +       return h_file;
14158 +}
14159 +
14160 +static int au_cmoo(struct dentry *dentry)
14161 +{
14162 +       int err, cmoo, matched;
14163 +       unsigned int udba;
14164 +       struct path h_path;
14165 +       struct au_pin pin;
14166 +       struct au_cp_generic cpg = {
14167 +               .dentry = dentry,
14168 +               .bdst   = -1,
14169 +               .bsrc   = -1,
14170 +               .len    = -1,
14171 +               .pin    = &pin,
14172 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14173 +       };
14174 +       struct inode *delegated;
14175 +       struct super_block *sb;
14176 +       struct au_sbinfo *sbinfo;
14177 +       struct au_fhsm *fhsm;
14178 +       pid_t pid;
14179 +       struct au_branch *br;
14180 +       struct dentry *parent;
14181 +       struct au_hinode *hdir;
14182 +
14183 +       DiMustWriteLock(dentry);
14184 +       IiMustWriteLock(d_inode(dentry));
14185 +
14186 +       err = 0;
14187 +       if (IS_ROOT(dentry))
14188 +               goto out;
14189 +       cpg.bsrc = au_dbtop(dentry);
14190 +       if (!cpg.bsrc)
14191 +               goto out;
14192 +
14193 +       sb = dentry->d_sb;
14194 +       sbinfo = au_sbi(sb);
14195 +       fhsm = &sbinfo->si_fhsm;
14196 +       pid = au_fhsm_pid(fhsm);
14197 +       rcu_read_lock();
14198 +       matched = (pid
14199 +                  && (current->pid == pid
14200 +                      || rcu_dereference(current->real_parent)->pid == pid));
14201 +       rcu_read_unlock();
14202 +       if (matched)
14203 +               goto out;
14204 +
14205 +       br = au_sbr(sb, cpg.bsrc);
14206 +       cmoo = au_br_cmoo(br->br_perm);
14207 +       if (!cmoo)
14208 +               goto out;
14209 +       if (!d_is_reg(dentry))
14210 +               cmoo &= AuBrAttr_COO_ALL;
14211 +       if (!cmoo)
14212 +               goto out;
14213 +
14214 +       parent = dget_parent(dentry);
14215 +       di_write_lock_parent(parent);
14216 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14217 +       cpg.bdst = err;
14218 +       if (unlikely(err < 0)) {
14219 +               err = 0;        /* there is no upper writable branch */
14220 +               goto out_dgrade;
14221 +       }
14222 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14223 +
14224 +       /* do not respect the coo attrib for the target branch */
14225 +       err = au_cpup_dirs(dentry, cpg.bdst);
14226 +       if (unlikely(err))
14227 +               goto out_dgrade;
14228 +
14229 +       di_downgrade_lock(parent, AuLock_IR);
14230 +       udba = au_opt_udba(sb);
14231 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14232 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14233 +       if (unlikely(err))
14234 +               goto out_parent;
14235 +
14236 +       err = au_sio_cpup_simple(&cpg);
14237 +       au_unpin(&pin);
14238 +       if (unlikely(err))
14239 +               goto out_parent;
14240 +       if (!(cmoo & AuBrWAttr_MOO))
14241 +               goto out_parent; /* success */
14242 +
14243 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14244 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14245 +       if (unlikely(err))
14246 +               goto out_parent;
14247 +
14248 +       h_path.mnt = au_br_mnt(br);
14249 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14250 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14251 +       delegated = NULL;
14252 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14253 +       au_unpin(&pin);
14254 +       /* todo: keep h_dentry or not? */
14255 +       if (unlikely(err == -EWOULDBLOCK)) {
14256 +               pr_warn("cannot retry for NFSv4 delegation"
14257 +                       " for an internal unlink\n");
14258 +               iput(delegated);
14259 +       }
14260 +       if (unlikely(err)) {
14261 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14262 +                      dentry, err);
14263 +               err = 0;
14264 +       }
14265 +       goto out_parent; /* success */
14266 +
14267 +out_dgrade:
14268 +       di_downgrade_lock(parent, AuLock_IR);
14269 +out_parent:
14270 +       di_read_unlock(parent, AuLock_IR);
14271 +       dput(parent);
14272 +out:
14273 +       AuTraceErr(err);
14274 +       return err;
14275 +}
14276 +
14277 +int au_do_open(struct file *file, struct au_do_open_args *args)
14278 +{
14279 +       int err, aopen = args->aopen;
14280 +       struct dentry *dentry;
14281 +       struct au_finfo *finfo;
14282 +
14283 +       if (!aopen)
14284 +               err = au_finfo_init(file, args->fidir);
14285 +       else {
14286 +               lockdep_off();
14287 +               err = au_finfo_init(file, args->fidir);
14288 +               lockdep_on();
14289 +       }
14290 +       if (unlikely(err))
14291 +               goto out;
14292 +
14293 +       dentry = file->f_path.dentry;
14294 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14295 +       di_write_lock_child(dentry);
14296 +       err = au_cmoo(dentry);
14297 +       di_downgrade_lock(dentry, AuLock_IR);
14298 +       if (!err) {
14299 +               if (!aopen)
14300 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14301 +               else {
14302 +                       lockdep_off();
14303 +                       err = args->open(file, vfsub_file_flags(file),
14304 +                                        args->h_file);
14305 +                       lockdep_on();
14306 +               }
14307 +       }
14308 +       di_read_unlock(dentry, AuLock_IR);
14309 +
14310 +       finfo = au_fi(file);
14311 +       if (!err) {
14312 +               finfo->fi_file = file;
14313 +               au_hbl_add(&finfo->fi_hlist,
14314 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14315 +       }
14316 +       if (!aopen)
14317 +               fi_write_unlock(file);
14318 +       else {
14319 +               lockdep_off();
14320 +               fi_write_unlock(file);
14321 +               lockdep_on();
14322 +       }
14323 +       if (unlikely(err)) {
14324 +               finfo->fi_hdir = NULL;
14325 +               au_finfo_fin(file);
14326 +       }
14327 +
14328 +out:
14329 +       AuTraceErr(err);
14330 +       return err;
14331 +}
14332 +
14333 +int au_reopen_nondir(struct file *file)
14334 +{
14335 +       int err;
14336 +       aufs_bindex_t btop;
14337 +       struct dentry *dentry;
14338 +       struct au_branch *br;
14339 +       struct file *h_file, *h_file_tmp;
14340 +
14341 +       dentry = file->f_path.dentry;
14342 +       btop = au_dbtop(dentry);
14343 +       br = au_sbr(dentry->d_sb, btop);
14344 +       h_file_tmp = NULL;
14345 +       if (au_fbtop(file) == btop) {
14346 +               h_file = au_hf_top(file);
14347 +               if (file->f_mode == h_file->f_mode)
14348 +                       return 0; /* success */
14349 +               h_file_tmp = h_file;
14350 +               get_file(h_file_tmp);
14351 +               au_lcnt_inc(&br->br_nfiles);
14352 +               au_set_h_fptr(file, btop, NULL);
14353 +       }
14354 +       AuDebugOn(au_fi(file)->fi_hdir);
14355 +       /*
14356 +        * it can happen
14357 +        * file exists on both of rw and ro
14358 +        * open --> dbtop and fbtop are both 0
14359 +        * prepend a branch as rw, "rw" become ro
14360 +        * remove rw/file
14361 +        * delete the top branch, "rw" becomes rw again
14362 +        *      --> dbtop is 1, fbtop is still 0
14363 +        * write --> fbtop is 0 but dbtop is 1
14364 +        */
14365 +       /* AuDebugOn(au_fbtop(file) < btop); */
14366 +
14367 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14368 +                          file, /*force_wr*/0);
14369 +       err = PTR_ERR(h_file);
14370 +       if (IS_ERR(h_file)) {
14371 +               if (h_file_tmp) {
14372 +                       /* revert */
14373 +                       au_set_h_fptr(file, btop, h_file_tmp);
14374 +                       h_file_tmp = NULL;
14375 +               }
14376 +               goto out; /* todo: close all? */
14377 +       }
14378 +
14379 +       err = 0;
14380 +       au_set_fbtop(file, btop);
14381 +       au_set_h_fptr(file, btop, h_file);
14382 +       au_update_figen(file);
14383 +       /* todo: necessary? */
14384 +       /* file->f_ra = h_file->f_ra; */
14385 +
14386 +out:
14387 +       if (h_file_tmp) {
14388 +               fput(h_file_tmp);
14389 +               au_lcnt_dec(&br->br_nfiles);
14390 +       }
14391 +       return err;
14392 +}
14393 +
14394 +/* ---------------------------------------------------------------------- */
14395 +
14396 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14397 +                       struct dentry *hi_wh)
14398 +{
14399 +       int err;
14400 +       aufs_bindex_t btop;
14401 +       struct au_dinfo *dinfo;
14402 +       struct dentry *h_dentry;
14403 +       struct au_hdentry *hdp;
14404 +
14405 +       dinfo = au_di(file->f_path.dentry);
14406 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14407 +
14408 +       btop = dinfo->di_btop;
14409 +       dinfo->di_btop = btgt;
14410 +       hdp = au_hdentry(dinfo, btgt);
14411 +       h_dentry = hdp->hd_dentry;
14412 +       hdp->hd_dentry = hi_wh;
14413 +       err = au_reopen_nondir(file);
14414 +       hdp->hd_dentry = h_dentry;
14415 +       dinfo->di_btop = btop;
14416 +
14417 +       return err;
14418 +}
14419 +
14420 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14421 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14422 +{
14423 +       int err;
14424 +       struct inode *inode, *h_inode;
14425 +       struct dentry *h_dentry, *hi_wh;
14426 +       struct au_cp_generic cpg = {
14427 +               .dentry = file->f_path.dentry,
14428 +               .bdst   = bcpup,
14429 +               .bsrc   = -1,
14430 +               .len    = len,
14431 +               .pin    = pin
14432 +       };
14433 +
14434 +       au_update_dbtop(cpg.dentry);
14435 +       inode = d_inode(cpg.dentry);
14436 +       h_inode = NULL;
14437 +       if (au_dbtop(cpg.dentry) <= bcpup
14438 +           && au_dbbot(cpg.dentry) >= bcpup) {
14439 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14440 +               if (h_dentry && d_is_positive(h_dentry))
14441 +                       h_inode = d_inode(h_dentry);
14442 +       }
14443 +       hi_wh = au_hi_wh(inode, bcpup);
14444 +       if (!hi_wh && !h_inode)
14445 +               err = au_sio_cpup_wh(&cpg, file);
14446 +       else
14447 +               /* already copied-up after unlink */
14448 +               err = au_reopen_wh(file, bcpup, hi_wh);
14449 +
14450 +       if (!err
14451 +           && (inode->i_nlink > 1
14452 +               || (inode->i_state & I_LINKABLE))
14453 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14454 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14455 +
14456 +       return err;
14457 +}
14458 +
14459 +/*
14460 + * prepare the @file for writing.
14461 + */
14462 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14463 +{
14464 +       int err;
14465 +       aufs_bindex_t dbtop;
14466 +       struct dentry *parent;
14467 +       struct inode *inode;
14468 +       struct super_block *sb;
14469 +       struct file *h_file;
14470 +       struct au_cp_generic cpg = {
14471 +               .dentry = file->f_path.dentry,
14472 +               .bdst   = -1,
14473 +               .bsrc   = -1,
14474 +               .len    = len,
14475 +               .pin    = pin,
14476 +               .flags  = AuCpup_DTIME
14477 +       };
14478 +
14479 +       sb = cpg.dentry->d_sb;
14480 +       inode = d_inode(cpg.dentry);
14481 +       cpg.bsrc = au_fbtop(file);
14482 +       err = au_test_ro(sb, cpg.bsrc, inode);
14483 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14484 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14485 +                            /*flags*/0);
14486 +               goto out;
14487 +       }
14488 +
14489 +       /* need to cpup or reopen */
14490 +       parent = dget_parent(cpg.dentry);
14491 +       di_write_lock_parent(parent);
14492 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14493 +       cpg.bdst = err;
14494 +       if (unlikely(err < 0))
14495 +               goto out_dgrade;
14496 +       err = 0;
14497 +
14498 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14499 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14500 +               if (unlikely(err))
14501 +                       goto out_dgrade;
14502 +       }
14503 +
14504 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14505 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14506 +       if (unlikely(err))
14507 +               goto out_dgrade;
14508 +
14509 +       dbtop = au_dbtop(cpg.dentry);
14510 +       if (dbtop <= cpg.bdst)
14511 +               cpg.bsrc = cpg.bdst;
14512 +
14513 +       if (dbtop <= cpg.bdst           /* just reopen */
14514 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14515 +               ) {
14516 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14517 +               if (IS_ERR(h_file))
14518 +                       err = PTR_ERR(h_file);
14519 +               else {
14520 +                       di_downgrade_lock(parent, AuLock_IR);
14521 +                       if (dbtop > cpg.bdst)
14522 +                               err = au_sio_cpup_simple(&cpg);
14523 +                       if (!err)
14524 +                               err = au_reopen_nondir(file);
14525 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14526 +               }
14527 +       } else {                        /* copyup as wh and reopen */
14528 +               /*
14529 +                * since writable hfsplus branch is not supported,
14530 +                * h_open_pre/post() are unnecessary.
14531 +                */
14532 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14533 +               di_downgrade_lock(parent, AuLock_IR);
14534 +       }
14535 +
14536 +       if (!err) {
14537 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14538 +               goto out_dput; /* success */
14539 +       }
14540 +       au_unpin(pin);
14541 +       goto out_unlock;
14542 +
14543 +out_dgrade:
14544 +       di_downgrade_lock(parent, AuLock_IR);
14545 +out_unlock:
14546 +       di_read_unlock(parent, AuLock_IR);
14547 +out_dput:
14548 +       dput(parent);
14549 +out:
14550 +       return err;
14551 +}
14552 +
14553 +/* ---------------------------------------------------------------------- */
14554 +
14555 +int au_do_flush(struct file *file, fl_owner_t id,
14556 +               int (*flush)(struct file *file, fl_owner_t id))
14557 +{
14558 +       int err;
14559 +       struct super_block *sb;
14560 +       struct inode *inode;
14561 +
14562 +       inode = file_inode(file);
14563 +       sb = inode->i_sb;
14564 +       si_noflush_read_lock(sb);
14565 +       fi_read_lock(file);
14566 +       ii_read_lock_child(inode);
14567 +
14568 +       err = flush(file, id);
14569 +       au_cpup_attr_timesizes(inode);
14570 +
14571 +       ii_read_unlock(inode);
14572 +       fi_read_unlock(file);
14573 +       si_read_unlock(sb);
14574 +       return err;
14575 +}
14576 +
14577 +/* ---------------------------------------------------------------------- */
14578 +
14579 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14580 +{
14581 +       int err;
14582 +       struct au_pin pin;
14583 +       struct au_finfo *finfo;
14584 +       struct dentry *parent, *hi_wh;
14585 +       struct inode *inode;
14586 +       struct super_block *sb;
14587 +       struct au_cp_generic cpg = {
14588 +               .dentry = file->f_path.dentry,
14589 +               .bdst   = -1,
14590 +               .bsrc   = -1,
14591 +               .len    = -1,
14592 +               .pin    = &pin,
14593 +               .flags  = AuCpup_DTIME
14594 +       };
14595 +
14596 +       FiMustWriteLock(file);
14597 +
14598 +       err = 0;
14599 +       finfo = au_fi(file);
14600 +       sb = cpg.dentry->d_sb;
14601 +       inode = d_inode(cpg.dentry);
14602 +       cpg.bdst = au_ibtop(inode);
14603 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14604 +               goto out;
14605 +
14606 +       parent = dget_parent(cpg.dentry);
14607 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14608 +               di_read_lock_parent(parent, !AuLock_IR);
14609 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14610 +               cpg.bdst = err;
14611 +               di_read_unlock(parent, !AuLock_IR);
14612 +               if (unlikely(err < 0))
14613 +                       goto out_parent;
14614 +               err = 0;
14615 +       }
14616 +
14617 +       di_read_lock_parent(parent, AuLock_IR);
14618 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14619 +       if (!S_ISDIR(inode->i_mode)
14620 +           && au_opt_test(au_mntflags(sb), PLINK)
14621 +           && au_plink_test(inode)
14622 +           && !d_unhashed(cpg.dentry)
14623 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14624 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14625 +               if (unlikely(err))
14626 +                       goto out_unlock;
14627 +
14628 +               /* always superio. */
14629 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14630 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14631 +               if (!err) {
14632 +                       err = au_sio_cpup_simple(&cpg);
14633 +                       au_unpin(&pin);
14634 +               }
14635 +       } else if (hi_wh) {
14636 +               /* already copied-up after unlink */
14637 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14638 +               *need_reopen = 0;
14639 +       }
14640 +
14641 +out_unlock:
14642 +       di_read_unlock(parent, AuLock_IR);
14643 +out_parent:
14644 +       dput(parent);
14645 +out:
14646 +       return err;
14647 +}
14648 +
14649 +static void au_do_refresh_dir(struct file *file)
14650 +{
14651 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14652 +       struct au_hfile *p, tmp, *q;
14653 +       struct au_finfo *finfo;
14654 +       struct super_block *sb;
14655 +       struct au_fidir *fidir;
14656 +
14657 +       FiMustWriteLock(file);
14658 +
14659 +       sb = file->f_path.dentry->d_sb;
14660 +       finfo = au_fi(file);
14661 +       fidir = finfo->fi_hdir;
14662 +       AuDebugOn(!fidir);
14663 +       p = fidir->fd_hfile + finfo->fi_btop;
14664 +       brid = p->hf_br->br_id;
14665 +       bbot = fidir->fd_bbot;
14666 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14667 +               if (!p->hf_file)
14668 +                       continue;
14669 +
14670 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14671 +               if (new_bindex == bindex)
14672 +                       continue;
14673 +               if (new_bindex < 0) {
14674 +                       au_set_h_fptr(file, bindex, NULL);
14675 +                       continue;
14676 +               }
14677 +
14678 +               /* swap two lower inode, and loop again */
14679 +               q = fidir->fd_hfile + new_bindex;
14680 +               tmp = *q;
14681 +               *q = *p;
14682 +               *p = tmp;
14683 +               if (tmp.hf_file) {
14684 +                       bindex--;
14685 +                       p--;
14686 +               }
14687 +       }
14688 +
14689 +       p = fidir->fd_hfile;
14690 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14691 +               bbot = au_sbbot(sb);
14692 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14693 +                    finfo->fi_btop++, p++)
14694 +                       if (p->hf_file) {
14695 +                               if (file_inode(p->hf_file))
14696 +                                       break;
14697 +                               au_hfput(p, /*execed*/0);
14698 +                       }
14699 +       } else {
14700 +               bbot = au_br_index(sb, brid);
14701 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14702 +                    finfo->fi_btop++, p++)
14703 +                       if (p->hf_file)
14704 +                               au_hfput(p, /*execed*/0);
14705 +               bbot = au_sbbot(sb);
14706 +       }
14707 +
14708 +       p = fidir->fd_hfile + bbot;
14709 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14710 +            fidir->fd_bbot--, p--)
14711 +               if (p->hf_file) {
14712 +                       if (file_inode(p->hf_file))
14713 +                               break;
14714 +                       au_hfput(p, /*execed*/0);
14715 +               }
14716 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14717 +}
14718 +
14719 +/*
14720 + * after branch manipulating, refresh the file.
14721 + */
14722 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14723 +{
14724 +       int err, need_reopen, nbr;
14725 +       aufs_bindex_t bbot, bindex;
14726 +       struct dentry *dentry;
14727 +       struct super_block *sb;
14728 +       struct au_finfo *finfo;
14729 +       struct au_hfile *hfile;
14730 +
14731 +       dentry = file->f_path.dentry;
14732 +       sb = dentry->d_sb;
14733 +       nbr = au_sbbot(sb) + 1;
14734 +       finfo = au_fi(file);
14735 +       if (!finfo->fi_hdir) {
14736 +               hfile = &finfo->fi_htop;
14737 +               AuDebugOn(!hfile->hf_file);
14738 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14739 +               AuDebugOn(bindex < 0);
14740 +               if (bindex != finfo->fi_btop)
14741 +                       au_set_fbtop(file, bindex);
14742 +       } else {
14743 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14744 +               if (unlikely(err))
14745 +                       goto out;
14746 +               au_do_refresh_dir(file);
14747 +       }
14748 +
14749 +       err = 0;
14750 +       need_reopen = 1;
14751 +       if (!au_test_mmapped(file))
14752 +               err = au_file_refresh_by_inode(file, &need_reopen);
14753 +       if (finfo->fi_hdir)
14754 +               /* harmless if err */
14755 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14756 +       if (!err && need_reopen && !d_unlinked(dentry))
14757 +               err = reopen(file);
14758 +       if (!err) {
14759 +               au_update_figen(file);
14760 +               goto out; /* success */
14761 +       }
14762 +
14763 +       /* error, close all lower files */
14764 +       if (finfo->fi_hdir) {
14765 +               bbot = au_fbbot_dir(file);
14766 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14767 +                       au_set_h_fptr(file, bindex, NULL);
14768 +       }
14769 +
14770 +out:
14771 +       return err;
14772 +}
14773 +
14774 +/* common function to regular file and dir */
14775 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14776 +                         int wlock, unsigned int fi_lsc)
14777 +{
14778 +       int err;
14779 +       unsigned int sigen, figen;
14780 +       aufs_bindex_t btop;
14781 +       unsigned char pseudo_link;
14782 +       struct dentry *dentry;
14783 +       struct inode *inode;
14784 +
14785 +       err = 0;
14786 +       dentry = file->f_path.dentry;
14787 +       inode = d_inode(dentry);
14788 +       sigen = au_sigen(dentry->d_sb);
14789 +       fi_write_lock_nested(file, fi_lsc);
14790 +       figen = au_figen(file);
14791 +       if (!fi_lsc)
14792 +               di_write_lock_child(dentry);
14793 +       else
14794 +               di_write_lock_child2(dentry);
14795 +       btop = au_dbtop(dentry);
14796 +       pseudo_link = (btop != au_ibtop(inode));
14797 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14798 +               if (!wlock) {
14799 +                       di_downgrade_lock(dentry, AuLock_IR);
14800 +                       fi_downgrade_lock(file);
14801 +               }
14802 +               goto out; /* success */
14803 +       }
14804 +
14805 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14806 +       if (au_digen_test(dentry, sigen)) {
14807 +               err = au_reval_dpath(dentry, sigen);
14808 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14809 +       }
14810 +
14811 +       if (!err)
14812 +               err = refresh_file(file, reopen);
14813 +       if (!err) {
14814 +               if (!wlock) {
14815 +                       di_downgrade_lock(dentry, AuLock_IR);
14816 +                       fi_downgrade_lock(file);
14817 +               }
14818 +       } else {
14819 +               di_write_unlock(dentry);
14820 +               fi_write_unlock(file);
14821 +       }
14822 +
14823 +out:
14824 +       return err;
14825 +}
14826 +
14827 +/* ---------------------------------------------------------------------- */
14828 +
14829 +/* cf. aufs_nopage() */
14830 +/* for madvise(2) */
14831 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14832 +{
14833 +       unlock_page(page);
14834 +       return 0;
14835 +}
14836 +
14837 +/* it will never be called, but necessary to support O_DIRECT */
14838 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14839 +{ BUG(); return 0; }
14840 +
14841 +/* they will never be called. */
14842 +#ifdef CONFIG_AUFS_DEBUG
14843 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14844 +                           loff_t pos, unsigned len, unsigned flags,
14845 +                           struct page **pagep, void **fsdata)
14846 +{ AuUnsupport(); return 0; }
14847 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14848 +                         loff_t pos, unsigned len, unsigned copied,
14849 +                         struct page *page, void *fsdata)
14850 +{ AuUnsupport(); return 0; }
14851 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14852 +{ AuUnsupport(); return 0; }
14853 +
14854 +static int aufs_set_page_dirty(struct page *page)
14855 +{ AuUnsupport(); return 0; }
14856 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14857 +                               unsigned int length)
14858 +{ AuUnsupport(); }
14859 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14860 +{ AuUnsupport(); return 0; }
14861 +#if 0 /* called by memory compaction regardless file */
14862 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14863 +                           struct page *page, enum migrate_mode mode)
14864 +{ AuUnsupport(); return 0; }
14865 +#endif
14866 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14867 +{ AuUnsupport(); return true; }
14868 +static void aufs_putback_page(struct page *page)
14869 +{ AuUnsupport(); }
14870 +static int aufs_launder_page(struct page *page)
14871 +{ AuUnsupport(); return 0; }
14872 +static int aufs_is_partially_uptodate(struct page *page,
14873 +                                     unsigned long from,
14874 +                                     unsigned long count)
14875 +{ AuUnsupport(); return 0; }
14876 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14877 +                                   bool *writeback)
14878 +{ AuUnsupport(); }
14879 +static int aufs_error_remove_page(struct address_space *mapping,
14880 +                                 struct page *page)
14881 +{ AuUnsupport(); return 0; }
14882 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14883 +                             sector_t *span)
14884 +{ AuUnsupport(); return 0; }
14885 +static void aufs_swap_deactivate(struct file *file)
14886 +{ AuUnsupport(); }
14887 +#endif /* CONFIG_AUFS_DEBUG */
14888 +
14889 +const struct address_space_operations aufs_aop = {
14890 +       .readpage               = aufs_readpage,
14891 +       .direct_IO              = aufs_direct_IO,
14892 +#ifdef CONFIG_AUFS_DEBUG
14893 +       .writepage              = aufs_writepage,
14894 +       /* no writepages, because of writepage */
14895 +       .set_page_dirty         = aufs_set_page_dirty,
14896 +       /* no readpages, because of readpage */
14897 +       .write_begin            = aufs_write_begin,
14898 +       .write_end              = aufs_write_end,
14899 +       /* no bmap, no block device */
14900 +       .invalidatepage         = aufs_invalidatepage,
14901 +       .releasepage            = aufs_releasepage,
14902 +       /* is fallback_migrate_page ok? */
14903 +       /* .migratepage         = aufs_migratepage, */
14904 +       .isolate_page           = aufs_isolate_page,
14905 +       .putback_page           = aufs_putback_page,
14906 +       .launder_page           = aufs_launder_page,
14907 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14908 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14909 +       .error_remove_page      = aufs_error_remove_page,
14910 +       .swap_activate          = aufs_swap_activate,
14911 +       .swap_deactivate        = aufs_swap_deactivate
14912 +#endif /* CONFIG_AUFS_DEBUG */
14913 +};
14914 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14915 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14916 +++ linux/fs/aufs/file.h        2021-02-24 13:33:42.744347058 +0100
14917 @@ -0,0 +1,342 @@
14918 +/* SPDX-License-Identifier: GPL-2.0 */
14919 +/*
14920 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14921 + *
14922 + * This program, aufs is free software; you can redistribute it and/or modify
14923 + * it under the terms of the GNU General Public License as published by
14924 + * the Free Software Foundation; either version 2 of the License, or
14925 + * (at your option) any later version.
14926 + *
14927 + * This program is distributed in the hope that it will be useful,
14928 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14929 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14930 + * GNU General Public License for more details.
14931 + *
14932 + * You should have received a copy of the GNU General Public License
14933 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14934 + */
14935 +
14936 +/*
14937 + * file operations
14938 + */
14939 +
14940 +#ifndef __AUFS_FILE_H__
14941 +#define __AUFS_FILE_H__
14942 +
14943 +#ifdef __KERNEL__
14944 +
14945 +#include <linux/file.h>
14946 +#include <linux/fs.h>
14947 +#include <linux/mm_types.h>
14948 +#include <linux/poll.h>
14949 +#include "rwsem.h"
14950 +
14951 +struct au_branch;
14952 +struct au_hfile {
14953 +       struct file             *hf_file;
14954 +       struct au_branch        *hf_br;
14955 +};
14956 +
14957 +struct au_vdir;
14958 +struct au_fidir {
14959 +       aufs_bindex_t           fd_bbot;
14960 +       aufs_bindex_t           fd_nent;
14961 +       struct au_vdir          *fd_vdir_cache;
14962 +       struct au_hfile         fd_hfile[];
14963 +};
14964 +
14965 +static inline int au_fidir_sz(int nent)
14966 +{
14967 +       AuDebugOn(nent < 0);
14968 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14969 +}
14970 +
14971 +struct au_finfo {
14972 +       atomic_t                fi_generation;
14973 +
14974 +       struct au_rwsem         fi_rwsem;
14975 +       aufs_bindex_t           fi_btop;
14976 +
14977 +       /* do not union them */
14978 +       struct {                                /* for non-dir */
14979 +               struct au_hfile                 fi_htop;
14980 +               atomic_t                        fi_mmapped;
14981 +       };
14982 +       struct au_fidir         *fi_hdir;       /* for dir only */
14983 +
14984 +       struct hlist_bl_node    fi_hlist;
14985 +       struct file             *fi_file;       /* very ugly */
14986 +       struct rcu_head         rcu;
14987 +} ____cacheline_aligned_in_smp;
14988 +
14989 +/* ---------------------------------------------------------------------- */
14990 +
14991 +/* file.c */
14992 +extern const struct address_space_operations aufs_aop;
14993 +unsigned int au_file_roflags(unsigned int flags);
14994 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14995 +                      struct file *file, int force_wr);
14996 +struct au_do_open_args {
14997 +       int             aopen;
14998 +       int             (*open)(struct file *file, int flags,
14999 +                               struct file *h_file);
15000 +       struct au_fidir *fidir;
15001 +       struct file     *h_file;
15002 +};
15003 +int au_do_open(struct file *file, struct au_do_open_args *args);
15004 +int au_reopen_nondir(struct file *file);
15005 +struct au_pin;
15006 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15007 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15008 +                         int wlock, unsigned int fi_lsc);
15009 +int au_do_flush(struct file *file, fl_owner_t id,
15010 +               int (*flush)(struct file *file, fl_owner_t id));
15011 +
15012 +/* poll.c */
15013 +#ifdef CONFIG_AUFS_POLL
15014 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15015 +#endif
15016 +
15017 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15018 +/* hfsplus.c */
15019 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15020 +                          int force_wr);
15021 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15022 +                   struct file *h_file);
15023 +#else
15024 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15025 +       aufs_bindex_t bindex, int force_wr)
15026 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15027 +          struct file *h_file);
15028 +#endif
15029 +
15030 +/* f_op.c */
15031 +extern const struct file_operations aufs_file_fop;
15032 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15033 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15034 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15035 +
15036 +/* finfo.c */
15037 +void au_hfput(struct au_hfile *hf, int execed);
15038 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15039 +                  struct file *h_file);
15040 +
15041 +void au_update_figen(struct file *file);
15042 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15043 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15044 +
15045 +void au_fi_init_once(void *_fi);
15046 +void au_finfo_fin(struct file *file);
15047 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15048 +
15049 +/* ioctl.c */
15050 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15051 +#ifdef CONFIG_COMPAT
15052 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15053 +                          unsigned long arg);
15054 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15055 +                             unsigned long arg);
15056 +#endif
15057 +
15058 +/* ---------------------------------------------------------------------- */
15059 +
15060 +static inline struct au_finfo *au_fi(struct file *file)
15061 +{
15062 +       return file->private_data;
15063 +}
15064 +
15065 +/* ---------------------------------------------------------------------- */
15066 +
15067 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15068 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15069 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15070 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15071 +/*
15072 +#define fi_read_trylock_nested(f) \
15073 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15074 +#define fi_write_trylock_nested(f) \
15075 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15076 +*/
15077 +
15078 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15079 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15080 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15081 +
15082 +/* lock subclass for finfo */
15083 +enum {
15084 +       AuLsc_FI_1,
15085 +       AuLsc_FI_2
15086 +};
15087 +
15088 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15089 +{
15090 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15091 +}
15092 +
15093 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15094 +{
15095 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15096 +}
15097 +
15098 +/*
15099 + * fi_read_lock_1, fi_write_lock_1,
15100 + * fi_read_lock_2, fi_write_lock_2
15101 + */
15102 +#define AuReadLockFunc(name) \
15103 +static inline void fi_read_lock_##name(struct file *f) \
15104 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15105 +
15106 +#define AuWriteLockFunc(name) \
15107 +static inline void fi_write_lock_##name(struct file *f) \
15108 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15109 +
15110 +#define AuRWLockFuncs(name) \
15111 +       AuReadLockFunc(name) \
15112 +       AuWriteLockFunc(name)
15113 +
15114 +AuRWLockFuncs(1);
15115 +AuRWLockFuncs(2);
15116 +
15117 +#undef AuReadLockFunc
15118 +#undef AuWriteLockFunc
15119 +#undef AuRWLockFuncs
15120 +
15121 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15122 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15123 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15124 +
15125 +/* ---------------------------------------------------------------------- */
15126 +
15127 +/* todo: hard/soft set? */
15128 +static inline aufs_bindex_t au_fbtop(struct file *file)
15129 +{
15130 +       FiMustAnyLock(file);
15131 +       return au_fi(file)->fi_btop;
15132 +}
15133 +
15134 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15135 +{
15136 +       FiMustAnyLock(file);
15137 +       AuDebugOn(!au_fi(file)->fi_hdir);
15138 +       return au_fi(file)->fi_hdir->fd_bbot;
15139 +}
15140 +
15141 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15142 +{
15143 +       FiMustAnyLock(file);
15144 +       AuDebugOn(!au_fi(file)->fi_hdir);
15145 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15146 +}
15147 +
15148 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15149 +{
15150 +       FiMustWriteLock(file);
15151 +       au_fi(file)->fi_btop = bindex;
15152 +}
15153 +
15154 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15155 +{
15156 +       FiMustWriteLock(file);
15157 +       AuDebugOn(!au_fi(file)->fi_hdir);
15158 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15159 +}
15160 +
15161 +static inline void au_set_fvdir_cache(struct file *file,
15162 +                                     struct au_vdir *vdir_cache)
15163 +{
15164 +       FiMustWriteLock(file);
15165 +       AuDebugOn(!au_fi(file)->fi_hdir);
15166 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15167 +}
15168 +
15169 +static inline struct file *au_hf_top(struct file *file)
15170 +{
15171 +       FiMustAnyLock(file);
15172 +       AuDebugOn(au_fi(file)->fi_hdir);
15173 +       return au_fi(file)->fi_htop.hf_file;
15174 +}
15175 +
15176 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15177 +{
15178 +       FiMustAnyLock(file);
15179 +       AuDebugOn(!au_fi(file)->fi_hdir);
15180 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15181 +}
15182 +
15183 +/* todo: memory barrier? */
15184 +static inline unsigned int au_figen(struct file *f)
15185 +{
15186 +       return atomic_read(&au_fi(f)->fi_generation);
15187 +}
15188 +
15189 +static inline void au_set_mmapped(struct file *f)
15190 +{
15191 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15192 +               return;
15193 +       pr_warn("fi_mmapped wrapped around\n");
15194 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15195 +               ;
15196 +}
15197 +
15198 +static inline void au_unset_mmapped(struct file *f)
15199 +{
15200 +       atomic_dec(&au_fi(f)->fi_mmapped);
15201 +}
15202 +
15203 +static inline int au_test_mmapped(struct file *f)
15204 +{
15205 +       return atomic_read(&au_fi(f)->fi_mmapped);
15206 +}
15207 +
15208 +/* customize vma->vm_file */
15209 +
15210 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15211 +                                      struct file *file)
15212 +{
15213 +       struct file *f;
15214 +
15215 +       f = vma->vm_file;
15216 +       get_file(file);
15217 +       vma->vm_file = file;
15218 +       fput(f);
15219 +}
15220 +
15221 +#ifdef CONFIG_MMU
15222 +#define AuDbgVmRegion(file, vma) do {} while (0)
15223 +
15224 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15225 +                                   struct file *file)
15226 +{
15227 +       au_do_vm_file_reset(vma, file);
15228 +}
15229 +#else
15230 +#define AuDbgVmRegion(file, vma) \
15231 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15232 +
15233 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15234 +                                   struct file *file)
15235 +{
15236 +       struct file *f;
15237 +
15238 +       au_do_vm_file_reset(vma, file);
15239 +       f = vma->vm_region->vm_file;
15240 +       get_file(file);
15241 +       vma->vm_region->vm_file = file;
15242 +       fput(f);
15243 +}
15244 +#endif /* CONFIG_MMU */
15245 +
15246 +/* handle vma->vm_prfile */
15247 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15248 +                                   struct file *file)
15249 +{
15250 +       get_file(file);
15251 +       vma->vm_prfile = file;
15252 +#ifndef CONFIG_MMU
15253 +       get_file(file);
15254 +       vma->vm_region->vm_prfile = file;
15255 +#endif
15256 +}
15257 +
15258 +#endif /* __KERNEL__ */
15259 +#endif /* __AUFS_FILE_H__ */
15260 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15261 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15262 +++ linux/fs/aufs/finfo.c       2021-02-24 13:33:42.744347058 +0100
15263 @@ -0,0 +1,149 @@
15264 +// SPDX-License-Identifier: GPL-2.0
15265 +/*
15266 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15267 + *
15268 + * This program, aufs is free software; you can redistribute it and/or modify
15269 + * it under the terms of the GNU General Public License as published by
15270 + * the Free Software Foundation; either version 2 of the License, or
15271 + * (at your option) any later version.
15272 + *
15273 + * This program is distributed in the hope that it will be useful,
15274 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15275 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15276 + * GNU General Public License for more details.
15277 + *
15278 + * You should have received a copy of the GNU General Public License
15279 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15280 + */
15281 +
15282 +/*
15283 + * file private data
15284 + */
15285 +
15286 +#include "aufs.h"
15287 +
15288 +void au_hfput(struct au_hfile *hf, int execed)
15289 +{
15290 +       if (execed)
15291 +               allow_write_access(hf->hf_file);
15292 +       fput(hf->hf_file);
15293 +       hf->hf_file = NULL;
15294 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15295 +       hf->hf_br = NULL;
15296 +}
15297 +
15298 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15299 +{
15300 +       struct au_finfo *finfo = au_fi(file);
15301 +       struct au_hfile *hf;
15302 +       struct au_fidir *fidir;
15303 +
15304 +       fidir = finfo->fi_hdir;
15305 +       if (!fidir) {
15306 +               AuDebugOn(finfo->fi_btop != bindex);
15307 +               hf = &finfo->fi_htop;
15308 +       } else
15309 +               hf = fidir->fd_hfile + bindex;
15310 +
15311 +       if (hf && hf->hf_file)
15312 +               au_hfput(hf, vfsub_file_execed(file));
15313 +       if (val) {
15314 +               FiMustWriteLock(file);
15315 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15316 +               hf->hf_file = val;
15317 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15318 +       }
15319 +}
15320 +
15321 +void au_update_figen(struct file *file)
15322 +{
15323 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15324 +       /* smp_mb(); */ /* atomic_set */
15325 +}
15326 +
15327 +/* ---------------------------------------------------------------------- */
15328 +
15329 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15330 +{
15331 +       struct au_fidir *fidir;
15332 +       int nbr;
15333 +
15334 +       nbr = au_sbbot(sb) + 1;
15335 +       if (nbr < 2)
15336 +               nbr = 2; /* initial allocate for 2 branches */
15337 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15338 +       if (fidir) {
15339 +               fidir->fd_bbot = -1;
15340 +               fidir->fd_nent = nbr;
15341 +       }
15342 +
15343 +       return fidir;
15344 +}
15345 +
15346 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15347 +{
15348 +       int err;
15349 +       struct au_fidir *fidir, *p;
15350 +
15351 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15352 +       fidir = finfo->fi_hdir;
15353 +       AuDebugOn(!fidir);
15354 +
15355 +       err = -ENOMEM;
15356 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15357 +                        GFP_NOFS, may_shrink);
15358 +       if (p) {
15359 +               p->fd_nent = nbr;
15360 +               finfo->fi_hdir = p;
15361 +               err = 0;
15362 +       }
15363 +
15364 +       return err;
15365 +}
15366 +
15367 +/* ---------------------------------------------------------------------- */
15368 +
15369 +void au_finfo_fin(struct file *file)
15370 +{
15371 +       struct au_finfo *finfo;
15372 +
15373 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15374 +
15375 +       finfo = au_fi(file);
15376 +       AuDebugOn(finfo->fi_hdir);
15377 +       AuRwDestroy(&finfo->fi_rwsem);
15378 +       au_cache_free_finfo(finfo);
15379 +}
15380 +
15381 +void au_fi_init_once(void *_finfo)
15382 +{
15383 +       struct au_finfo *finfo = _finfo;
15384 +
15385 +       au_rw_init(&finfo->fi_rwsem);
15386 +}
15387 +
15388 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15389 +{
15390 +       int err;
15391 +       struct au_finfo *finfo;
15392 +       struct dentry *dentry;
15393 +
15394 +       err = -ENOMEM;
15395 +       dentry = file->f_path.dentry;
15396 +       finfo = au_cache_alloc_finfo();
15397 +       if (unlikely(!finfo))
15398 +               goto out;
15399 +
15400 +       err = 0;
15401 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15402 +       au_rw_write_lock(&finfo->fi_rwsem);
15403 +       finfo->fi_btop = -1;
15404 +       finfo->fi_hdir = fidir;
15405 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15406 +       /* smp_mb(); */ /* atomic_set */
15407 +
15408 +       file->private_data = finfo;
15409 +
15410 +out:
15411 +       return err;
15412 +}
15413 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15414 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15415 +++ linux/fs/aufs/f_op.c        2021-02-24 13:33:42.744347058 +0100
15416 @@ -0,0 +1,762 @@
15417 +// SPDX-License-Identifier: GPL-2.0
15418 +/*
15419 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15420 + *
15421 + * This program, aufs is free software; you can redistribute it and/or modify
15422 + * it under the terms of the GNU General Public License as published by
15423 + * the Free Software Foundation; either version 2 of the License, or
15424 + * (at your option) any later version.
15425 + *
15426 + * This program is distributed in the hope that it will be useful,
15427 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15428 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15429 + * GNU General Public License for more details.
15430 + *
15431 + * You should have received a copy of the GNU General Public License
15432 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15433 + */
15434 +
15435 +/*
15436 + * file and vm operations
15437 + */
15438 +
15439 +#include <linux/aio.h>
15440 +#include <linux/fs_stack.h>
15441 +#include <linux/mman.h>
15442 +#include <linux/security.h>
15443 +#include "aufs.h"
15444 +
15445 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15446 +{
15447 +       int err;
15448 +       aufs_bindex_t bindex;
15449 +       struct dentry *dentry, *h_dentry;
15450 +       struct au_finfo *finfo;
15451 +       struct inode *h_inode;
15452 +
15453 +       FiMustWriteLock(file);
15454 +
15455 +       err = 0;
15456 +       dentry = file->f_path.dentry;
15457 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15458 +       finfo = au_fi(file);
15459 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15460 +       atomic_set(&finfo->fi_mmapped, 0);
15461 +       bindex = au_dbtop(dentry);
15462 +       if (!h_file) {
15463 +               h_dentry = au_h_dptr(dentry, bindex);
15464 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15465 +               if (unlikely(err))
15466 +                       goto out;
15467 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15468 +               if (IS_ERR(h_file)) {
15469 +                       err = PTR_ERR(h_file);
15470 +                       goto out;
15471 +               }
15472 +       } else {
15473 +               h_dentry = h_file->f_path.dentry;
15474 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15475 +               if (unlikely(err))
15476 +                       goto out;
15477 +               /* br ref is already inc-ed */
15478 +       }
15479 +
15480 +       if ((flags & __O_TMPFILE)
15481 +           && !(flags & O_EXCL)) {
15482 +               h_inode = file_inode(h_file);
15483 +               spin_lock(&h_inode->i_lock);
15484 +               h_inode->i_state |= I_LINKABLE;
15485 +               spin_unlock(&h_inode->i_lock);
15486 +       }
15487 +       au_set_fbtop(file, bindex);
15488 +       au_set_h_fptr(file, bindex, h_file);
15489 +       au_update_figen(file);
15490 +       /* todo: necessary? */
15491 +       /* file->f_ra = h_file->f_ra; */
15492 +
15493 +out:
15494 +       return err;
15495 +}
15496 +
15497 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15498 +                           struct file *file)
15499 +{
15500 +       int err;
15501 +       struct super_block *sb;
15502 +       struct au_do_open_args args = {
15503 +               .open   = au_do_open_nondir
15504 +       };
15505 +
15506 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15507 +             file, vfsub_file_flags(file), file->f_mode);
15508 +
15509 +       sb = file->f_path.dentry->d_sb;
15510 +       si_read_lock(sb, AuLock_FLUSH);
15511 +       err = au_do_open(file, &args);
15512 +       si_read_unlock(sb);
15513 +       return err;
15514 +}
15515 +
15516 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15517 +{
15518 +       struct au_finfo *finfo;
15519 +       aufs_bindex_t bindex;
15520 +
15521 +       finfo = au_fi(file);
15522 +       au_hbl_del(&finfo->fi_hlist,
15523 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15524 +       bindex = finfo->fi_btop;
15525 +       if (bindex >= 0)
15526 +               au_set_h_fptr(file, bindex, NULL);
15527 +
15528 +       au_finfo_fin(file);
15529 +       return 0;
15530 +}
15531 +
15532 +/* ---------------------------------------------------------------------- */
15533 +
15534 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15535 +{
15536 +       int err;
15537 +       struct file *h_file;
15538 +
15539 +       err = 0;
15540 +       h_file = au_hf_top(file);
15541 +       if (h_file)
15542 +               err = vfsub_flush(h_file, id);
15543 +       return err;
15544 +}
15545 +
15546 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15547 +{
15548 +       return au_do_flush(file, id, au_do_flush_nondir);
15549 +}
15550 +
15551 +/* ---------------------------------------------------------------------- */
15552 +/*
15553 + * read and write functions acquire [fdi]_rwsem once, but release before
15554 + * mmap_sem. This is because to stop a race condition between mmap(2).
15555 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15556 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15557 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15558 + */
15559 +
15560 +/* Callers should call au_read_post() or fput() in the end */
15561 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15562 +{
15563 +       struct file *h_file;
15564 +       int err;
15565 +
15566 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15567 +       if (!err) {
15568 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15569 +               h_file = au_hf_top(file);
15570 +               get_file(h_file);
15571 +               if (!keep_fi)
15572 +                       fi_read_unlock(file);
15573 +       } else
15574 +               h_file = ERR_PTR(err);
15575 +
15576 +       return h_file;
15577 +}
15578 +
15579 +static void au_read_post(struct inode *inode, struct file *h_file)
15580 +{
15581 +       /* update without lock, I don't think it a problem */
15582 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15583 +       fput(h_file);
15584 +}
15585 +
15586 +struct au_write_pre {
15587 +       /* input */
15588 +       unsigned int lsc;
15589 +
15590 +       /* output */
15591 +       blkcnt_t blks;
15592 +       aufs_bindex_t btop;
15593 +};
15594 +
15595 +/*
15596 + * return with iinfo is write-locked
15597 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15598 + * end
15599 + */
15600 +static struct file *au_write_pre(struct file *file, int do_ready,
15601 +                                struct au_write_pre *wpre)
15602 +{
15603 +       struct file *h_file;
15604 +       struct dentry *dentry;
15605 +       int err;
15606 +       unsigned int lsc;
15607 +       struct au_pin pin;
15608 +
15609 +       lsc = 0;
15610 +       if (wpre)
15611 +               lsc = wpre->lsc;
15612 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15613 +       h_file = ERR_PTR(err);
15614 +       if (unlikely(err))
15615 +               goto out;
15616 +
15617 +       dentry = file->f_path.dentry;
15618 +       if (do_ready) {
15619 +               err = au_ready_to_write(file, -1, &pin);
15620 +               if (unlikely(err)) {
15621 +                       h_file = ERR_PTR(err);
15622 +                       di_write_unlock(dentry);
15623 +                       goto out_fi;
15624 +               }
15625 +       }
15626 +
15627 +       di_downgrade_lock(dentry, /*flags*/0);
15628 +       if (wpre)
15629 +               wpre->btop = au_fbtop(file);
15630 +       h_file = au_hf_top(file);
15631 +       get_file(h_file);
15632 +       if (wpre)
15633 +               wpre->blks = file_inode(h_file)->i_blocks;
15634 +       if (do_ready)
15635 +               au_unpin(&pin);
15636 +       di_read_unlock(dentry, /*flags*/0);
15637 +
15638 +out_fi:
15639 +       fi_write_unlock(file);
15640 +out:
15641 +       return h_file;
15642 +}
15643 +
15644 +static void au_write_post(struct inode *inode, struct file *h_file,
15645 +                         struct au_write_pre *wpre, ssize_t written)
15646 +{
15647 +       struct inode *h_inode;
15648 +
15649 +       au_cpup_attr_timesizes(inode);
15650 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15651 +       h_inode = file_inode(h_file);
15652 +       inode->i_mode = h_inode->i_mode;
15653 +       ii_write_unlock(inode);
15654 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15655 +       if (written > 0)
15656 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15657 +                             /*force*/h_inode->i_blocks > wpre->blks);
15658 +       fput(h_file);
15659 +}
15660 +
15661 +/*
15662 + * todo: very ugly
15663 + * it locks both of i_mutex and si_rwsem for read in safe.
15664 + * if the plink maintenance mode continues forever (that is the problem),
15665 + * may loop forever.
15666 + */
15667 +static void au_mtx_and_read_lock(struct inode *inode)
15668 +{
15669 +       int err;
15670 +       struct super_block *sb = inode->i_sb;
15671 +
15672 +       while (1) {
15673 +               inode_lock(inode);
15674 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15675 +               if (!err)
15676 +                       break;
15677 +               inode_unlock(inode);
15678 +               si_read_lock(sb, AuLock_NOPLMW);
15679 +               si_read_unlock(sb);
15680 +       }
15681 +}
15682 +
15683 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15684 +                         struct iov_iter *iov_iter)
15685 +{
15686 +       ssize_t err;
15687 +       struct file *file;
15688 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15689 +
15690 +       err = security_file_permission(h_file, rw);
15691 +       if (unlikely(err))
15692 +               goto out;
15693 +
15694 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15695 +       iter = NULL;
15696 +       if (rw == MAY_READ)
15697 +               iter = h_file->f_op->read_iter;
15698 +       else if (rw == MAY_WRITE)
15699 +               iter = h_file->f_op->write_iter;
15700 +
15701 +       file = kio->ki_filp;
15702 +       kio->ki_filp = h_file;
15703 +       if (iter) {
15704 +               lockdep_off();
15705 +               err = iter(kio, iov_iter);
15706 +               lockdep_on();
15707 +       } else
15708 +               /* currently there is no such fs */
15709 +               WARN_ON_ONCE(1);
15710 +       kio->ki_filp = file;
15711 +
15712 +out:
15713 +       return err;
15714 +}
15715 +
15716 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15717 +{
15718 +       ssize_t err;
15719 +       struct file *file, *h_file;
15720 +       struct inode *inode;
15721 +       struct super_block *sb;
15722 +
15723 +       file = kio->ki_filp;
15724 +       inode = file_inode(file);
15725 +       sb = inode->i_sb;
15726 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15727 +
15728 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15729 +       err = PTR_ERR(h_file);
15730 +       if (IS_ERR(h_file))
15731 +               goto out;
15732 +
15733 +       if (au_test_loopback_kthread()) {
15734 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15735 +               if (file->f_mapping != h_file->f_mapping) {
15736 +                       file->f_mapping = h_file->f_mapping;
15737 +                       smp_mb(); /* unnecessary? */
15738 +               }
15739 +       }
15740 +       fi_read_unlock(file);
15741 +
15742 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15743 +       /* todo: necessary? */
15744 +       /* file->f_ra = h_file->f_ra; */
15745 +       au_read_post(inode, h_file);
15746 +
15747 +out:
15748 +       si_read_unlock(sb);
15749 +       return err;
15750 +}
15751 +
15752 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15753 +{
15754 +       ssize_t err;
15755 +       struct au_write_pre wpre;
15756 +       struct inode *inode;
15757 +       struct file *file, *h_file;
15758 +
15759 +       file = kio->ki_filp;
15760 +       inode = file_inode(file);
15761 +       au_mtx_and_read_lock(inode);
15762 +
15763 +       wpre.lsc = 0;
15764 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15765 +       err = PTR_ERR(h_file);
15766 +       if (IS_ERR(h_file))
15767 +               goto out;
15768 +
15769 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15770 +       au_write_post(inode, h_file, &wpre, err);
15771 +
15772 +out:
15773 +       si_read_unlock(inode->i_sb);
15774 +       inode_unlock(inode);
15775 +       return err;
15776 +}
15777 +
15778 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15779 +                               struct pipe_inode_info *pipe, size_t len,
15780 +                               unsigned int flags)
15781 +{
15782 +       ssize_t err;
15783 +       struct file *h_file;
15784 +       struct inode *inode;
15785 +       struct super_block *sb;
15786 +
15787 +       inode = file_inode(file);
15788 +       sb = inode->i_sb;
15789 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15790 +
15791 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15792 +       err = PTR_ERR(h_file);
15793 +       if (IS_ERR(h_file))
15794 +               goto out;
15795 +
15796 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15797 +       /* todo: necessary? */
15798 +       /* file->f_ra = h_file->f_ra; */
15799 +       au_read_post(inode, h_file);
15800 +
15801 +out:
15802 +       si_read_unlock(sb);
15803 +       return err;
15804 +}
15805 +
15806 +static ssize_t
15807 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15808 +                 size_t len, unsigned int flags)
15809 +{
15810 +       ssize_t err;
15811 +       struct au_write_pre wpre;
15812 +       struct inode *inode;
15813 +       struct file *h_file;
15814 +
15815 +       inode = file_inode(file);
15816 +       au_mtx_and_read_lock(inode);
15817 +
15818 +       wpre.lsc = 0;
15819 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15820 +       err = PTR_ERR(h_file);
15821 +       if (IS_ERR(h_file))
15822 +               goto out;
15823 +
15824 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15825 +       au_write_post(inode, h_file, &wpre, err);
15826 +
15827 +out:
15828 +       si_read_unlock(inode->i_sb);
15829 +       inode_unlock(inode);
15830 +       return err;
15831 +}
15832 +
15833 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15834 +                          loff_t len)
15835 +{
15836 +       long err;
15837 +       struct au_write_pre wpre;
15838 +       struct inode *inode;
15839 +       struct file *h_file;
15840 +
15841 +       inode = file_inode(file);
15842 +       au_mtx_and_read_lock(inode);
15843 +
15844 +       wpre.lsc = 0;
15845 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15846 +       err = PTR_ERR(h_file);
15847 +       if (IS_ERR(h_file))
15848 +               goto out;
15849 +
15850 +       lockdep_off();
15851 +       err = vfs_fallocate(h_file, mode, offset, len);
15852 +       lockdep_on();
15853 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15854 +
15855 +out:
15856 +       si_read_unlock(inode->i_sb);
15857 +       inode_unlock(inode);
15858 +       return err;
15859 +}
15860 +
15861 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15862 +                                   struct file *dst, loff_t dst_pos,
15863 +                                   size_t len, unsigned int flags)
15864 +{
15865 +       ssize_t err;
15866 +       struct au_write_pre wpre;
15867 +       enum { SRC, DST };
15868 +       struct {
15869 +               struct inode *inode;
15870 +               struct file *h_file;
15871 +               struct super_block *h_sb;
15872 +       } a[2];
15873 +#define a_src  a[SRC]
15874 +#define a_dst  a[DST]
15875 +
15876 +       err = -EINVAL;
15877 +       a_src.inode = file_inode(src);
15878 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15879 +               goto out;
15880 +       a_dst.inode = file_inode(dst);
15881 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15882 +               goto out;
15883 +
15884 +       au_mtx_and_read_lock(a_dst.inode);
15885 +       /*
15886 +        * in order to match the order in di_write_lock2_{child,parent}(),
15887 +        * use f_path.dentry for this comparison.
15888 +        */
15889 +       if (src->f_path.dentry < dst->f_path.dentry) {
15890 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15891 +               err = PTR_ERR(a_src.h_file);
15892 +               if (IS_ERR(a_src.h_file))
15893 +                       goto out_si;
15894 +
15895 +               wpre.lsc = AuLsc_FI_2;
15896 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15897 +               err = PTR_ERR(a_dst.h_file);
15898 +               if (IS_ERR(a_dst.h_file)) {
15899 +                       au_read_post(a_src.inode, a_src.h_file);
15900 +                       goto out_si;
15901 +               }
15902 +       } else {
15903 +               wpre.lsc = AuLsc_FI_1;
15904 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15905 +               err = PTR_ERR(a_dst.h_file);
15906 +               if (IS_ERR(a_dst.h_file))
15907 +                       goto out_si;
15908 +
15909 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15910 +               err = PTR_ERR(a_src.h_file);
15911 +               if (IS_ERR(a_src.h_file)) {
15912 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15913 +                                     /*written*/0);
15914 +                       goto out_si;
15915 +               }
15916 +       }
15917 +
15918 +       err = -EXDEV;
15919 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15920 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15921 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15922 +               AuDbgFile(src);
15923 +               AuDbgFile(dst);
15924 +               goto out_file;
15925 +       }
15926 +
15927 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15928 +                                   dst_pos, len, flags);
15929 +
15930 +out_file:
15931 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15932 +       fi_read_unlock(src);
15933 +       au_read_post(a_src.inode, a_src.h_file);
15934 +out_si:
15935 +       si_read_unlock(a_dst.inode->i_sb);
15936 +       inode_unlock(a_dst.inode);
15937 +out:
15938 +       return err;
15939 +#undef a_src
15940 +#undef a_dst
15941 +}
15942 +
15943 +/* ---------------------------------------------------------------------- */
15944 +
15945 +/*
15946 + * The locking order around current->mmap_sem.
15947 + * - in most and regular cases
15948 + *   file I/O syscall -- aufs_read() or something
15949 + *     -- si_rwsem for read -- mmap_sem
15950 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15951 + * - in mmap case
15952 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15953 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15954 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15955 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15956 + * It means that when aufs acquires si_rwsem for write, the process should never
15957 + * acquire mmap_sem.
15958 + *
15959 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15960 + * problem either since any directory is not able to be mmap-ed.
15961 + * The similar scenario is applied to aufs_readlink() too.
15962 + */
15963 +
15964 +#if 0 /* stop calling security_file_mmap() */
15965 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15966 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15967 +
15968 +static unsigned long au_arch_prot_conv(unsigned long flags)
15969 +{
15970 +       /* currently ppc64 only */
15971 +#ifdef CONFIG_PPC64
15972 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15973 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15974 +       return AuConv_VM_PROT(flags, SAO);
15975 +#else
15976 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15977 +       return 0;
15978 +#endif
15979 +}
15980 +
15981 +static unsigned long au_prot_conv(unsigned long flags)
15982 +{
15983 +       return AuConv_VM_PROT(flags, READ)
15984 +               | AuConv_VM_PROT(flags, WRITE)
15985 +               | AuConv_VM_PROT(flags, EXEC)
15986 +               | au_arch_prot_conv(flags);
15987 +}
15988 +
15989 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15990 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15991 +
15992 +static unsigned long au_flag_conv(unsigned long flags)
15993 +{
15994 +       return AuConv_VM_MAP(flags, GROWSDOWN)
15995 +               | AuConv_VM_MAP(flags, DENYWRITE)
15996 +               | AuConv_VM_MAP(flags, LOCKED);
15997 +}
15998 +#endif
15999 +
16000 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16001 +{
16002 +       int err;
16003 +       const unsigned char wlock
16004 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16005 +       struct super_block *sb;
16006 +       struct file *h_file;
16007 +       struct inode *inode;
16008 +
16009 +       AuDbgVmRegion(file, vma);
16010 +
16011 +       inode = file_inode(file);
16012 +       sb = inode->i_sb;
16013 +       lockdep_off();
16014 +       si_read_lock(sb, AuLock_NOPLMW);
16015 +
16016 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16017 +       lockdep_on();
16018 +       err = PTR_ERR(h_file);
16019 +       if (IS_ERR(h_file))
16020 +               goto out;
16021 +
16022 +       err = 0;
16023 +       au_set_mmapped(file);
16024 +       au_vm_file_reset(vma, h_file);
16025 +       /*
16026 +        * we cannot call security_mmap_file() here since it may acquire
16027 +        * mmap_sem or i_mutex.
16028 +        *
16029 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16030 +        *                       au_flag_conv(vma->vm_flags));
16031 +        */
16032 +       if (!err)
16033 +               err = call_mmap(h_file, vma);
16034 +       if (!err) {
16035 +               au_vm_prfile_set(vma, file);
16036 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16037 +               goto out_fput; /* success */
16038 +       }
16039 +       au_unset_mmapped(file);
16040 +       au_vm_file_reset(vma, file);
16041 +
16042 +out_fput:
16043 +       lockdep_off();
16044 +       ii_write_unlock(inode);
16045 +       lockdep_on();
16046 +       fput(h_file);
16047 +out:
16048 +       lockdep_off();
16049 +       si_read_unlock(sb);
16050 +       lockdep_on();
16051 +       AuTraceErr(err);
16052 +       return err;
16053 +}
16054 +
16055 +/* ---------------------------------------------------------------------- */
16056 +
16057 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16058 +                            int datasync)
16059 +{
16060 +       int err;
16061 +       struct au_write_pre wpre;
16062 +       struct inode *inode;
16063 +       struct file *h_file;
16064 +
16065 +       err = 0; /* -EBADF; */ /* posix? */
16066 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16067 +               goto out;
16068 +
16069 +       inode = file_inode(file);
16070 +       au_mtx_and_read_lock(inode);
16071 +
16072 +       wpre.lsc = 0;
16073 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16074 +       err = PTR_ERR(h_file);
16075 +       if (IS_ERR(h_file))
16076 +               goto out_unlock;
16077 +
16078 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16079 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16080 +
16081 +out_unlock:
16082 +       si_read_unlock(inode->i_sb);
16083 +       inode_unlock(inode);
16084 +out:
16085 +       return err;
16086 +}
16087 +
16088 +static int aufs_fasync(int fd, struct file *file, int flag)
16089 +{
16090 +       int err;
16091 +       struct file *h_file;
16092 +       struct super_block *sb;
16093 +
16094 +       sb = file->f_path.dentry->d_sb;
16095 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16096 +
16097 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16098 +       err = PTR_ERR(h_file);
16099 +       if (IS_ERR(h_file))
16100 +               goto out;
16101 +
16102 +       if (h_file->f_op->fasync)
16103 +               err = h_file->f_op->fasync(fd, h_file, flag);
16104 +       fput(h_file); /* instead of au_read_post() */
16105 +
16106 +out:
16107 +       si_read_unlock(sb);
16108 +       return err;
16109 +}
16110 +
16111 +static int aufs_setfl(struct file *file, unsigned long arg)
16112 +{
16113 +       int err;
16114 +       struct file *h_file;
16115 +       struct super_block *sb;
16116 +
16117 +       sb = file->f_path.dentry->d_sb;
16118 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16119 +
16120 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16121 +       err = PTR_ERR(h_file);
16122 +       if (IS_ERR(h_file))
16123 +               goto out;
16124 +
16125 +       /* stop calling h_file->fasync */
16126 +       arg |= vfsub_file_flags(file) & FASYNC;
16127 +       err = setfl(/*unused fd*/-1, h_file, arg);
16128 +       fput(h_file); /* instead of au_read_post() */
16129 +
16130 +out:
16131 +       si_read_unlock(sb);
16132 +       return err;
16133 +}
16134 +
16135 +/* ---------------------------------------------------------------------- */
16136 +
16137 +/* no one supports this operation, currently */
16138 +#if 0 /* reserved for future use */
16139 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16140 +                            size_t len, loff_t *pos, int more)
16141 +{
16142 +}
16143 +#endif
16144 +
16145 +/* ---------------------------------------------------------------------- */
16146 +
16147 +const struct file_operations aufs_file_fop = {
16148 +       .owner          = THIS_MODULE,
16149 +
16150 +       .llseek         = default_llseek,
16151 +
16152 +       .read_iter      = aufs_read_iter,
16153 +       .write_iter     = aufs_write_iter,
16154 +
16155 +#ifdef CONFIG_AUFS_POLL
16156 +       .poll           = aufs_poll,
16157 +#endif
16158 +       .unlocked_ioctl = aufs_ioctl_nondir,
16159 +#ifdef CONFIG_COMPAT
16160 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16161 +#endif
16162 +       .mmap           = aufs_mmap,
16163 +       .open           = aufs_open_nondir,
16164 +       .flush          = aufs_flush_nondir,
16165 +       .release        = aufs_release_nondir,
16166 +       .fsync          = aufs_fsync_nondir,
16167 +       .fasync         = aufs_fasync,
16168 +       /* .sendpage    = aufs_sendpage, */
16169 +       .setfl          = aufs_setfl,
16170 +       .splice_write   = aufs_splice_write,
16171 +       .splice_read    = aufs_splice_read,
16172 +#if 0 /* reserved for future use */
16173 +       .aio_splice_write = aufs_aio_splice_write,
16174 +       .aio_splice_read  = aufs_aio_splice_read,
16175 +#endif
16176 +       .fallocate      = aufs_fallocate,
16177 +       .copy_file_range = aufs_copy_file_range
16178 +};
16179 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16180 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16181 +++ linux/fs/aufs/fstype.h      2021-02-24 13:33:42.744347058 +0100
16182 @@ -0,0 +1,401 @@
16183 +/* SPDX-License-Identifier: GPL-2.0 */
16184 +/*
16185 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16186 + *
16187 + * This program, aufs is free software; you can redistribute it and/or modify
16188 + * it under the terms of the GNU General Public License as published by
16189 + * the Free Software Foundation; either version 2 of the License, or
16190 + * (at your option) any later version.
16191 + *
16192 + * This program is distributed in the hope that it will be useful,
16193 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16194 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16195 + * GNU General Public License for more details.
16196 + *
16197 + * You should have received a copy of the GNU General Public License
16198 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16199 + */
16200 +
16201 +/*
16202 + * judging filesystem type
16203 + */
16204 +
16205 +#ifndef __AUFS_FSTYPE_H__
16206 +#define __AUFS_FSTYPE_H__
16207 +
16208 +#ifdef __KERNEL__
16209 +
16210 +#include <linux/fs.h>
16211 +#include <linux/magic.h>
16212 +#include <linux/nfs_fs.h>
16213 +#include <linux/romfs_fs.h>
16214 +
16215 +static inline int au_test_aufs(struct super_block *sb)
16216 +{
16217 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16218 +}
16219 +
16220 +static inline const char *au_sbtype(struct super_block *sb)
16221 +{
16222 +       return sb->s_type->name;
16223 +}
16224 +
16225 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16226 +{
16227 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16228 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16229 +#else
16230 +       return 0;
16231 +#endif
16232 +}
16233 +
16234 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16235 +{
16236 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16237 +       return sb->s_magic == ROMFS_MAGIC;
16238 +#else
16239 +       return 0;
16240 +#endif
16241 +}
16242 +
16243 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16244 +{
16245 +#if IS_ENABLED(CONFIG_CRAMFS)
16246 +       return sb->s_magic == CRAMFS_MAGIC;
16247 +#endif
16248 +       return 0;
16249 +}
16250 +
16251 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16252 +{
16253 +#if IS_ENABLED(CONFIG_NFS_FS)
16254 +       return sb->s_magic == NFS_SUPER_MAGIC;
16255 +#else
16256 +       return 0;
16257 +#endif
16258 +}
16259 +
16260 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16261 +{
16262 +#if IS_ENABLED(CONFIG_FUSE_FS)
16263 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16264 +#else
16265 +       return 0;
16266 +#endif
16267 +}
16268 +
16269 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16270 +{
16271 +#if IS_ENABLED(CONFIG_XFS_FS)
16272 +       return sb->s_magic == XFS_SB_MAGIC;
16273 +#else
16274 +       return 0;
16275 +#endif
16276 +}
16277 +
16278 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16279 +{
16280 +#ifdef CONFIG_TMPFS
16281 +       return sb->s_magic == TMPFS_MAGIC;
16282 +#else
16283 +       return 0;
16284 +#endif
16285 +}
16286 +
16287 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16288 +{
16289 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16290 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16291 +#else
16292 +       return 0;
16293 +#endif
16294 +}
16295 +
16296 +static inline int au_test_ramfs(struct super_block *sb)
16297 +{
16298 +       return sb->s_magic == RAMFS_MAGIC;
16299 +}
16300 +
16301 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16302 +{
16303 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16304 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16305 +#else
16306 +       return 0;
16307 +#endif
16308 +}
16309 +
16310 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16311 +{
16312 +#ifdef CONFIG_PROC_FS
16313 +       return sb->s_magic == PROC_SUPER_MAGIC;
16314 +#else
16315 +       return 0;
16316 +#endif
16317 +}
16318 +
16319 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16320 +{
16321 +#ifdef CONFIG_SYSFS
16322 +       return sb->s_magic == SYSFS_MAGIC;
16323 +#else
16324 +       return 0;
16325 +#endif
16326 +}
16327 +
16328 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16329 +{
16330 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16331 +       return sb->s_magic == CONFIGFS_MAGIC;
16332 +#else
16333 +       return 0;
16334 +#endif
16335 +}
16336 +
16337 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16338 +{
16339 +#if IS_ENABLED(CONFIG_MINIX_FS)
16340 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16341 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16342 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16343 +               || sb->s_magic == MINIX_SUPER_MAGIC
16344 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16345 +#else
16346 +       return 0;
16347 +#endif
16348 +}
16349 +
16350 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16351 +{
16352 +#if IS_ENABLED(CONFIG_FAT_FS)
16353 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16354 +#else
16355 +       return 0;
16356 +#endif
16357 +}
16358 +
16359 +static inline int au_test_msdos(struct super_block *sb)
16360 +{
16361 +       return au_test_fat(sb);
16362 +}
16363 +
16364 +static inline int au_test_vfat(struct super_block *sb)
16365 +{
16366 +       return au_test_fat(sb);
16367 +}
16368 +
16369 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16370 +{
16371 +#ifdef CONFIG_SECURITYFS
16372 +       return sb->s_magic == SECURITYFS_MAGIC;
16373 +#else
16374 +       return 0;
16375 +#endif
16376 +}
16377 +
16378 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16379 +{
16380 +#if IS_ENABLED(CONFIG_SQUASHFS)
16381 +       return sb->s_magic == SQUASHFS_MAGIC;
16382 +#else
16383 +       return 0;
16384 +#endif
16385 +}
16386 +
16387 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16388 +{
16389 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16390 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16391 +#else
16392 +       return 0;
16393 +#endif
16394 +}
16395 +
16396 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16397 +{
16398 +#if IS_ENABLED(CONFIG_XENFS)
16399 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16400 +#else
16401 +       return 0;
16402 +#endif
16403 +}
16404 +
16405 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16406 +{
16407 +#ifdef CONFIG_DEBUG_FS
16408 +       return sb->s_magic == DEBUGFS_MAGIC;
16409 +#else
16410 +       return 0;
16411 +#endif
16412 +}
16413 +
16414 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16415 +{
16416 +#if IS_ENABLED(CONFIG_NILFS)
16417 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16418 +#else
16419 +       return 0;
16420 +#endif
16421 +}
16422 +
16423 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16424 +{
16425 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16426 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16427 +#else
16428 +       return 0;
16429 +#endif
16430 +}
16431 +
16432 +/* ---------------------------------------------------------------------- */
16433 +/*
16434 + * they can't be an aufs branch.
16435 + */
16436 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16437 +{
16438 +       return
16439 +#ifndef CONFIG_AUFS_BR_RAMFS
16440 +               au_test_ramfs(sb) ||
16441 +#endif
16442 +               au_test_procfs(sb)
16443 +               || au_test_sysfs(sb)
16444 +               || au_test_configfs(sb)
16445 +               || au_test_debugfs(sb)
16446 +               || au_test_securityfs(sb)
16447 +               || au_test_xenfs(sb)
16448 +               || au_test_ecryptfs(sb)
16449 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16450 +               || au_test_aufs(sb); /* will be supported in next version */
16451 +}
16452 +
16453 +static inline int au_test_fs_remote(struct super_block *sb)
16454 +{
16455 +       return !au_test_tmpfs(sb)
16456 +#ifdef CONFIG_AUFS_BR_RAMFS
16457 +               && !au_test_ramfs(sb)
16458 +#endif
16459 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16460 +}
16461 +
16462 +/* ---------------------------------------------------------------------- */
16463 +
16464 +/*
16465 + * Note: these functions (below) are created after reading ->getattr() in all
16466 + * filesystems under linux/fs. it means we have to do so in every update...
16467 + */
16468 +
16469 +/*
16470 + * some filesystems require getattr to refresh the inode attributes before
16471 + * referencing.
16472 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16473 + * and leave the work for d_revalidate()
16474 + */
16475 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16476 +{
16477 +       return au_test_nfs(sb)
16478 +               || au_test_fuse(sb)
16479 +               /* || au_test_btrfs(sb) */      /* untested */
16480 +               ;
16481 +}
16482 +
16483 +/*
16484 + * filesystems which don't maintain i_size or i_blocks.
16485 + */
16486 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16487 +{
16488 +       return au_test_xfs(sb)
16489 +               || au_test_btrfs(sb)
16490 +               || au_test_ubifs(sb)
16491 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16492 +               /* || au_test_minix(sb) */      /* untested */
16493 +               ;
16494 +}
16495 +
16496 +/*
16497 + * filesystems which don't store the correct value in some of their inode
16498 + * attributes.
16499 + */
16500 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16501 +{
16502 +       return au_test_fs_bad_iattr_size(sb)
16503 +               || au_test_fat(sb)
16504 +               || au_test_msdos(sb)
16505 +               || au_test_vfat(sb);
16506 +}
16507 +
16508 +/* they don't check i_nlink in link(2) */
16509 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16510 +{
16511 +       return au_test_tmpfs(sb)
16512 +#ifdef CONFIG_AUFS_BR_RAMFS
16513 +               || au_test_ramfs(sb)
16514 +#endif
16515 +               || au_test_ubifs(sb)
16516 +               || au_test_hfsplus(sb);
16517 +}
16518 +
16519 +/*
16520 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16521 + */
16522 +static inline int au_test_fs_notime(struct super_block *sb)
16523 +{
16524 +       return au_test_nfs(sb)
16525 +               || au_test_fuse(sb)
16526 +               || au_test_ubifs(sb)
16527 +               ;
16528 +}
16529 +
16530 +/* temporary support for i#1 in cramfs */
16531 +static inline int au_test_fs_unique_ino(struct inode *inode)
16532 +{
16533 +       if (au_test_cramfs(inode->i_sb))
16534 +               return inode->i_ino != 1;
16535 +       return 1;
16536 +}
16537 +
16538 +/* ---------------------------------------------------------------------- */
16539 +
16540 +/*
16541 + * the filesystem where the xino files placed must support i/o after unlink and
16542 + * maintain i_size and i_blocks.
16543 + */
16544 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16545 +{
16546 +       return au_test_fs_remote(sb)
16547 +               || au_test_fs_bad_iattr_size(sb)
16548 +               /* don't want unnecessary work for xino */
16549 +               || au_test_aufs(sb)
16550 +               || au_test_ecryptfs(sb)
16551 +               || au_test_nilfs(sb);
16552 +}
16553 +
16554 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16555 +{
16556 +       return au_test_tmpfs(sb)
16557 +               || au_test_ramfs(sb);
16558 +}
16559 +
16560 +/*
16561 + * test if the @sb is real-readonly.
16562 + */
16563 +static inline int au_test_fs_rr(struct super_block *sb)
16564 +{
16565 +       return au_test_squashfs(sb)
16566 +               || au_test_iso9660(sb)
16567 +               || au_test_cramfs(sb)
16568 +               || au_test_romfs(sb);
16569 +}
16570 +
16571 +/*
16572 + * test if the @inode is nfs with 'noacl' option
16573 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16574 + */
16575 +static inline int au_test_nfs_noacl(struct inode *inode)
16576 +{
16577 +       return au_test_nfs(inode->i_sb)
16578 +               /* && IS_POSIXACL(inode) */
16579 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16580 +}
16581 +
16582 +#endif /* __KERNEL__ */
16583 +#endif /* __AUFS_FSTYPE_H__ */
16584 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16585 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16586 +++ linux/fs/aufs/hbl.h 2021-02-24 13:33:42.744347058 +0100
16587 @@ -0,0 +1,65 @@
16588 +/* SPDX-License-Identifier: GPL-2.0 */
16589 +/*
16590 + * Copyright (C) 2017-2020 Junjiro R. Okajima
16591 + *
16592 + * This program, aufs is free software; you can redistribute it and/or modify
16593 + * it under the terms of the GNU General Public License as published by
16594 + * the Free Software Foundation; either version 2 of the License, or
16595 + * (at your option) any later version.
16596 + *
16597 + * This program is distributed in the hope that it will be useful,
16598 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16599 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16600 + * GNU General Public License for more details.
16601 + *
16602 + * You should have received a copy of the GNU General Public License
16603 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16604 + */
16605 +
16606 +/*
16607 + * helpers for hlist_bl.h
16608 + */
16609 +
16610 +#ifndef __AUFS_HBL_H__
16611 +#define __AUFS_HBL_H__
16612 +
16613 +#ifdef __KERNEL__
16614 +
16615 +#include <linux/list_bl.h>
16616 +
16617 +static inline void au_hbl_add(struct hlist_bl_node *node,
16618 +                             struct hlist_bl_head *hbl)
16619 +{
16620 +       hlist_bl_lock(hbl);
16621 +       hlist_bl_add_head(node, hbl);
16622 +       hlist_bl_unlock(hbl);
16623 +}
16624 +
16625 +static inline void au_hbl_del(struct hlist_bl_node *node,
16626 +                             struct hlist_bl_head *hbl)
16627 +{
16628 +       hlist_bl_lock(hbl);
16629 +       hlist_bl_del(node);
16630 +       hlist_bl_unlock(hbl);
16631 +}
16632 +
16633 +#define au_hbl_for_each(pos, head)                                     \
16634 +       for (pos = hlist_bl_first(head);                                \
16635 +            pos;                                                       \
16636 +            pos = pos->next)
16637 +
16638 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16639 +{
16640 +       unsigned long cnt;
16641 +       struct hlist_bl_node *pos;
16642 +
16643 +       cnt = 0;
16644 +       hlist_bl_lock(hbl);
16645 +       au_hbl_for_each(pos, hbl)
16646 +               cnt++;
16647 +       hlist_bl_unlock(hbl);
16648 +       return cnt;
16649 +}
16650 +
16651 +#endif /* __KERNEL__ */
16652 +#endif /* __AUFS_HBL_H__ */
16653 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16654 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16655 +++ linux/fs/aufs/hfsnotify.c   2021-02-24 13:33:42.744347058 +0100
16656 @@ -0,0 +1,288 @@
16657 +// SPDX-License-Identifier: GPL-2.0
16658 +/*
16659 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16660 + *
16661 + * This program, aufs is free software; you can redistribute it and/or modify
16662 + * it under the terms of the GNU General Public License as published by
16663 + * the Free Software Foundation; either version 2 of the License, or
16664 + * (at your option) any later version.
16665 + *
16666 + * This program is distributed in the hope that it will be useful,
16667 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16668 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16669 + * GNU General Public License for more details.
16670 + *
16671 + * You should have received a copy of the GNU General Public License
16672 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16673 + */
16674 +
16675 +/*
16676 + * fsnotify for the lower directories
16677 + */
16678 +
16679 +#include "aufs.h"
16680 +
16681 +/* FS_IN_IGNORED is unnecessary */
16682 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16683 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16684 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16685 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16686 +
16687 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16688 +{
16689 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16690 +                                            hn_mark);
16691 +       /* AuDbg("here\n"); */
16692 +       au_cache_free_hnotify(hn);
16693 +       smp_mb__before_atomic(); /* for atomic64_dec */
16694 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16695 +               wake_up(&au_hfsn_wq);
16696 +}
16697 +
16698 +static int au_hfsn_alloc(struct au_hinode *hinode)
16699 +{
16700 +       int err;
16701 +       struct au_hnotify *hn;
16702 +       struct super_block *sb;
16703 +       struct au_branch *br;
16704 +       struct fsnotify_mark *mark;
16705 +       aufs_bindex_t bindex;
16706 +
16707 +       hn = hinode->hi_notify;
16708 +       sb = hn->hn_aufs_inode->i_sb;
16709 +       bindex = au_br_index(sb, hinode->hi_id);
16710 +       br = au_sbr(sb, bindex);
16711 +       AuDebugOn(!br->br_hfsn);
16712 +
16713 +       mark = &hn->hn_mark;
16714 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16715 +       mark->mask = AuHfsnMask;
16716 +       /*
16717 +        * by udba rename or rmdir, aufs assign a new inode to the known
16718 +        * h_inode, so specify 1 to allow dups.
16719 +        */
16720 +       lockdep_off();
16721 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16722 +       lockdep_on();
16723 +
16724 +       return err;
16725 +}
16726 +
16727 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16728 +{
16729 +       struct fsnotify_mark *mark;
16730 +       unsigned long long ull;
16731 +       struct fsnotify_group *group;
16732 +
16733 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16734 +       BUG_ON(!ull);
16735 +
16736 +       mark = &hn->hn_mark;
16737 +       spin_lock(&mark->lock);
16738 +       group = mark->group;
16739 +       fsnotify_get_group(group);
16740 +       spin_unlock(&mark->lock);
16741 +       lockdep_off();
16742 +       fsnotify_destroy_mark(mark, group);
16743 +       fsnotify_put_mark(mark);
16744 +       fsnotify_put_group(group);
16745 +       lockdep_on();
16746 +
16747 +       /* free hn by myself */
16748 +       return 0;
16749 +}
16750 +
16751 +/* ---------------------------------------------------------------------- */
16752 +
16753 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16754 +{
16755 +       struct fsnotify_mark *mark;
16756 +
16757 +       mark = &hinode->hi_notify->hn_mark;
16758 +       spin_lock(&mark->lock);
16759 +       if (do_set) {
16760 +               AuDebugOn(mark->mask & AuHfsnMask);
16761 +               mark->mask |= AuHfsnMask;
16762 +       } else {
16763 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16764 +               mark->mask &= ~AuHfsnMask;
16765 +       }
16766 +       spin_unlock(&mark->lock);
16767 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16768 +}
16769 +
16770 +/* ---------------------------------------------------------------------- */
16771 +
16772 +/* #define AuDbgHnotify */
16773 +#ifdef AuDbgHnotify
16774 +static char *au_hfsn_name(u32 mask)
16775 +{
16776 +#ifdef CONFIG_AUFS_DEBUG
16777 +#define test_ret(flag)                         \
16778 +       do {                                    \
16779 +               if (mask & flag)                \
16780 +                       return #flag;           \
16781 +       } while (0)
16782 +       test_ret(FS_ACCESS);
16783 +       test_ret(FS_MODIFY);
16784 +       test_ret(FS_ATTRIB);
16785 +       test_ret(FS_CLOSE_WRITE);
16786 +       test_ret(FS_CLOSE_NOWRITE);
16787 +       test_ret(FS_OPEN);
16788 +       test_ret(FS_MOVED_FROM);
16789 +       test_ret(FS_MOVED_TO);
16790 +       test_ret(FS_CREATE);
16791 +       test_ret(FS_DELETE);
16792 +       test_ret(FS_DELETE_SELF);
16793 +       test_ret(FS_MOVE_SELF);
16794 +       test_ret(FS_UNMOUNT);
16795 +       test_ret(FS_Q_OVERFLOW);
16796 +       test_ret(FS_IN_IGNORED);
16797 +       test_ret(FS_ISDIR);
16798 +       test_ret(FS_IN_ONESHOT);
16799 +       test_ret(FS_EVENT_ON_CHILD);
16800 +       return "";
16801 +#undef test_ret
16802 +#else
16803 +       return "??";
16804 +#endif
16805 +}
16806 +#endif
16807 +
16808 +/* ---------------------------------------------------------------------- */
16809 +
16810 +static void au_hfsn_free_group(struct fsnotify_group *group)
16811 +{
16812 +       struct au_br_hfsnotify *hfsn = group->private;
16813 +
16814 +       /* AuDbg("here\n"); */
16815 +       au_kfree_try_rcu(hfsn);
16816 +}
16817 +
16818 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16819 +                               u32 mask, const void *data, int data_type,
16820 +                               struct inode *dir,
16821 +                               const struct qstr *file_name, u32 cookie,
16822 +                               struct fsnotify_iter_info *iter_info)
16823 +{
16824 +       int err;
16825 +       struct au_hnotify *hnotify;
16826 +       struct inode *h_dir, *h_inode;
16827 +       struct fsnotify_mark *inode_mark;
16828 +
16829 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16830 +
16831 +       err = 0;
16832 +       /* if FS_UNMOUNT happens, there must be another bug */
16833 +       AuDebugOn(mask & FS_UNMOUNT);
16834 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16835 +               goto out;
16836 +
16837 +       h_dir = dir;
16838 +       h_inode = NULL;
16839 +#ifdef AuDbgHnotify
16840 +       au_debug_on();
16841 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16842 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16843 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16844 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16845 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16846 +               /* WARN_ON(1); */
16847 +       }
16848 +       au_debug_off();
16849 +#endif
16850 +
16851 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16852 +       AuDebugOn(!inode_mark);
16853 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16854 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16855 +
16856 +out:
16857 +       return err;
16858 +}
16859 +
16860 +static struct fsnotify_ops au_hfsn_ops = {
16861 +       .handle_event           = au_hfsn_handle_event,
16862 +       .free_group_priv        = au_hfsn_free_group,
16863 +       .free_mark              = au_hfsn_free_mark
16864 +};
16865 +
16866 +/* ---------------------------------------------------------------------- */
16867 +
16868 +static void au_hfsn_fin_br(struct au_branch *br)
16869 +{
16870 +       struct au_br_hfsnotify *hfsn;
16871 +
16872 +       hfsn = br->br_hfsn;
16873 +       if (hfsn) {
16874 +               lockdep_off();
16875 +               fsnotify_put_group(hfsn->hfsn_group);
16876 +               lockdep_on();
16877 +       }
16878 +}
16879 +
16880 +static int au_hfsn_init_br(struct au_branch *br, int perm)
16881 +{
16882 +       int err;
16883 +       struct fsnotify_group *group;
16884 +       struct au_br_hfsnotify *hfsn;
16885 +
16886 +       err = 0;
16887 +       br->br_hfsn = NULL;
16888 +       if (!au_br_hnotifyable(perm))
16889 +               goto out;
16890 +
16891 +       err = -ENOMEM;
16892 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16893 +       if (unlikely(!hfsn))
16894 +               goto out;
16895 +
16896 +       err = 0;
16897 +       group = fsnotify_alloc_group(&au_hfsn_ops);
16898 +       if (IS_ERR(group)) {
16899 +               err = PTR_ERR(group);
16900 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
16901 +               goto out_hfsn;
16902 +       }
16903 +
16904 +       group->private = hfsn;
16905 +       hfsn->hfsn_group = group;
16906 +       br->br_hfsn = hfsn;
16907 +       goto out; /* success */
16908 +
16909 +out_hfsn:
16910 +       au_kfree_try_rcu(hfsn);
16911 +out:
16912 +       return err;
16913 +}
16914 +
16915 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16916 +{
16917 +       int err;
16918 +
16919 +       err = 0;
16920 +       if (!br->br_hfsn)
16921 +               err = au_hfsn_init_br(br, perm);
16922 +
16923 +       return err;
16924 +}
16925 +
16926 +/* ---------------------------------------------------------------------- */
16927 +
16928 +static void au_hfsn_fin(void)
16929 +{
16930 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16931 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16932 +}
16933 +
16934 +const struct au_hnotify_op au_hnotify_op = {
16935 +       .ctl            = au_hfsn_ctl,
16936 +       .alloc          = au_hfsn_alloc,
16937 +       .free           = au_hfsn_free,
16938 +
16939 +       .fin            = au_hfsn_fin,
16940 +
16941 +       .reset_br       = au_hfsn_reset_br,
16942 +       .fin_br         = au_hfsn_fin_br,
16943 +       .init_br        = au_hfsn_init_br
16944 +};
16945 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16946 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
16947 +++ linux/fs/aufs/hfsplus.c     2021-02-24 13:33:42.744347058 +0100
16948 @@ -0,0 +1,60 @@
16949 +// SPDX-License-Identifier: GPL-2.0
16950 +/*
16951 + * Copyright (C) 2010-2020 Junjiro R. Okajima
16952 + *
16953 + * This program, aufs is free software; you can redistribute it and/or modify
16954 + * it under the terms of the GNU General Public License as published by
16955 + * the Free Software Foundation; either version 2 of the License, or
16956 + * (at your option) any later version.
16957 + *
16958 + * This program is distributed in the hope that it will be useful,
16959 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16960 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16961 + * GNU General Public License for more details.
16962 + *
16963 + * You should have received a copy of the GNU General Public License
16964 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16965 + */
16966 +
16967 +/*
16968 + * special support for filesystems which acquires an inode mutex
16969 + * at final closing a file, eg, hfsplus.
16970 + *
16971 + * This trick is very simple and stupid, just to open the file before really
16972 + * necessary open to tell hfsplus that this is not the final closing.
16973 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
16974 + * and au_h_open_post() after releasing it.
16975 + */
16976 +
16977 +#include "aufs.h"
16978 +
16979 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
16980 +                          int force_wr)
16981 +{
16982 +       struct file *h_file;
16983 +       struct dentry *h_dentry;
16984 +
16985 +       h_dentry = au_h_dptr(dentry, bindex);
16986 +       AuDebugOn(!h_dentry);
16987 +       AuDebugOn(d_is_negative(h_dentry));
16988 +
16989 +       h_file = NULL;
16990 +       if (au_test_hfsplus(h_dentry->d_sb)
16991 +           && d_is_reg(h_dentry))
16992 +               h_file = au_h_open(dentry, bindex,
16993 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
16994 +                                  /*file*/NULL, force_wr);
16995 +       return h_file;
16996 +}
16997 +
16998 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
16999 +                   struct file *h_file)
17000 +{
17001 +       struct au_branch *br;
17002 +
17003 +       if (h_file) {
17004 +               fput(h_file);
17005 +               br = au_sbr(dentry->d_sb, bindex);
17006 +               au_lcnt_dec(&br->br_nfiles);
17007 +       }
17008 +}
17009 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17010 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17011 +++ linux/fs/aufs/hnotify.c     2021-02-24 13:33:42.744347058 +0100
17012 @@ -0,0 +1,715 @@
17013 +// SPDX-License-Identifier: GPL-2.0
17014 +/*
17015 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17016 + *
17017 + * This program, aufs is free software; you can redistribute it and/or modify
17018 + * it under the terms of the GNU General Public License as published by
17019 + * the Free Software Foundation; either version 2 of the License, or
17020 + * (at your option) any later version.
17021 + *
17022 + * This program is distributed in the hope that it will be useful,
17023 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17024 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17025 + * GNU General Public License for more details.
17026 + *
17027 + * You should have received a copy of the GNU General Public License
17028 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17029 + */
17030 +
17031 +/*
17032 + * abstraction to notify the direct changes on lower directories
17033 + */
17034 +
17035 +/* #include <linux/iversion.h> */
17036 +#include "aufs.h"
17037 +
17038 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17039 +{
17040 +       int err;
17041 +       struct au_hnotify *hn;
17042 +
17043 +       err = -ENOMEM;
17044 +       hn = au_cache_alloc_hnotify();
17045 +       if (hn) {
17046 +               hn->hn_aufs_inode = inode;
17047 +               hinode->hi_notify = hn;
17048 +               err = au_hnotify_op.alloc(hinode);
17049 +               AuTraceErr(err);
17050 +               if (unlikely(err)) {
17051 +                       hinode->hi_notify = NULL;
17052 +                       au_cache_free_hnotify(hn);
17053 +                       /*
17054 +                        * The upper dir was removed by udba, but the same named
17055 +                        * dir left. In this case, aufs assigns a new inode
17056 +                        * number and set the monitor again.
17057 +                        * For the lower dir, the old monitor is still left.
17058 +                        */
17059 +                       if (err == -EEXIST)
17060 +                               err = 0;
17061 +               }
17062 +       }
17063 +
17064 +       AuTraceErr(err);
17065 +       return err;
17066 +}
17067 +
17068 +void au_hn_free(struct au_hinode *hinode)
17069 +{
17070 +       struct au_hnotify *hn;
17071 +
17072 +       hn = hinode->hi_notify;
17073 +       if (hn) {
17074 +               hinode->hi_notify = NULL;
17075 +               if (au_hnotify_op.free(hinode, hn))
17076 +                       au_cache_free_hnotify(hn);
17077 +       }
17078 +}
17079 +
17080 +/* ---------------------------------------------------------------------- */
17081 +
17082 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17083 +{
17084 +       if (hinode->hi_notify)
17085 +               au_hnotify_op.ctl(hinode, do_set);
17086 +}
17087 +
17088 +void au_hn_reset(struct inode *inode, unsigned int flags)
17089 +{
17090 +       aufs_bindex_t bindex, bbot;
17091 +       struct inode *hi;
17092 +       struct dentry *iwhdentry;
17093 +
17094 +       bbot = au_ibbot(inode);
17095 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17096 +               hi = au_h_iptr(inode, bindex);
17097 +               if (!hi)
17098 +                       continue;
17099 +
17100 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17101 +               iwhdentry = au_hi_wh(inode, bindex);
17102 +               if (iwhdentry)
17103 +                       dget(iwhdentry);
17104 +               au_igrab(hi);
17105 +               au_set_h_iptr(inode, bindex, NULL, 0);
17106 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17107 +                             flags & ~AuHi_XINO);
17108 +               iput(hi);
17109 +               dput(iwhdentry);
17110 +               /* inode_unlock(hi); */
17111 +       }
17112 +}
17113 +
17114 +/* ---------------------------------------------------------------------- */
17115 +
17116 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17117 +{
17118 +       int err;
17119 +       aufs_bindex_t bindex, bbot, bfound, btop;
17120 +       struct inode *h_i;
17121 +
17122 +       err = 0;
17123 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17124 +               pr_warn("branch root dir was changed\n");
17125 +               goto out;
17126 +       }
17127 +
17128 +       bfound = -1;
17129 +       bbot = au_ibbot(inode);
17130 +       btop = au_ibtop(inode);
17131 +#if 0 /* reserved for future use */
17132 +       if (bindex == bbot) {
17133 +               /* keep this ino in rename case */
17134 +               goto out;
17135 +       }
17136 +#endif
17137 +       for (bindex = btop; bindex <= bbot; bindex++)
17138 +               if (au_h_iptr(inode, bindex) == h_inode) {
17139 +                       bfound = bindex;
17140 +                       break;
17141 +               }
17142 +       if (bfound < 0)
17143 +               goto out;
17144 +
17145 +       for (bindex = btop; bindex <= bbot; bindex++) {
17146 +               h_i = au_h_iptr(inode, bindex);
17147 +               if (!h_i)
17148 +                       continue;
17149 +
17150 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17151 +               /* ignore this error */
17152 +               /* bad action? */
17153 +       }
17154 +
17155 +       /* children inode number will be broken */
17156 +
17157 +out:
17158 +       AuTraceErr(err);
17159 +       return err;
17160 +}
17161 +
17162 +static int hn_gen_tree(struct dentry *dentry)
17163 +{
17164 +       int err, i, j, ndentry;
17165 +       struct au_dcsub_pages dpages;
17166 +       struct au_dpage *dpage;
17167 +       struct dentry **dentries;
17168 +
17169 +       err = au_dpages_init(&dpages, GFP_NOFS);
17170 +       if (unlikely(err))
17171 +               goto out;
17172 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17173 +       if (unlikely(err))
17174 +               goto out_dpages;
17175 +
17176 +       for (i = 0; i < dpages.ndpage; i++) {
17177 +               dpage = dpages.dpages + i;
17178 +               dentries = dpage->dentries;
17179 +               ndentry = dpage->ndentry;
17180 +               for (j = 0; j < ndentry; j++) {
17181 +                       struct dentry *d;
17182 +
17183 +                       d = dentries[j];
17184 +                       if (IS_ROOT(d))
17185 +                               continue;
17186 +
17187 +                       au_digen_dec(d);
17188 +                       if (d_really_is_positive(d))
17189 +                               /* todo: reset children xino?
17190 +                                  cached children only? */
17191 +                               au_iigen_dec(d_inode(d));
17192 +               }
17193 +       }
17194 +
17195 +out_dpages:
17196 +       au_dpages_free(&dpages);
17197 +out:
17198 +       return err;
17199 +}
17200 +
17201 +/*
17202 + * return 0 if processed.
17203 + */
17204 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17205 +                          const unsigned int isdir)
17206 +{
17207 +       int err;
17208 +       struct dentry *d;
17209 +       struct qstr *dname;
17210 +
17211 +       err = 1;
17212 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17213 +               pr_warn("branch root dir was changed\n");
17214 +               err = 0;
17215 +               goto out;
17216 +       }
17217 +
17218 +       if (!isdir) {
17219 +               AuDebugOn(!name);
17220 +               au_iigen_dec(inode);
17221 +               spin_lock(&inode->i_lock);
17222 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17223 +                       spin_lock(&d->d_lock);
17224 +                       dname = &d->d_name;
17225 +                       if (dname->len != nlen
17226 +                           && memcmp(dname->name, name, nlen)) {
17227 +                               spin_unlock(&d->d_lock);
17228 +                               continue;
17229 +                       }
17230 +                       err = 0;
17231 +                       au_digen_dec(d);
17232 +                       spin_unlock(&d->d_lock);
17233 +                       break;
17234 +               }
17235 +               spin_unlock(&inode->i_lock);
17236 +       } else {
17237 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17238 +               d = d_find_any_alias(inode);
17239 +               if (!d) {
17240 +                       au_iigen_dec(inode);
17241 +                       goto out;
17242 +               }
17243 +
17244 +               spin_lock(&d->d_lock);
17245 +               dname = &d->d_name;
17246 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17247 +                       spin_unlock(&d->d_lock);
17248 +                       err = hn_gen_tree(d);
17249 +                       spin_lock(&d->d_lock);
17250 +               }
17251 +               spin_unlock(&d->d_lock);
17252 +               dput(d);
17253 +       }
17254 +
17255 +out:
17256 +       AuTraceErr(err);
17257 +       return err;
17258 +}
17259 +
17260 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17261 +{
17262 +       int err;
17263 +
17264 +       if (IS_ROOT(dentry)) {
17265 +               pr_warn("branch root dir was changed\n");
17266 +               return 0;
17267 +       }
17268 +
17269 +       err = 0;
17270 +       if (!isdir) {
17271 +               au_digen_dec(dentry);
17272 +               if (d_really_is_positive(dentry))
17273 +                       au_iigen_dec(d_inode(dentry));
17274 +       } else {
17275 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17276 +               if (d_really_is_positive(dentry))
17277 +                       err = hn_gen_tree(dentry);
17278 +       }
17279 +
17280 +       AuTraceErr(err);
17281 +       return err;
17282 +}
17283 +
17284 +/* ---------------------------------------------------------------------- */
17285 +
17286 +/* hnotify job flags */
17287 +#define AuHnJob_XINO0          1
17288 +#define AuHnJob_GEN            (1 << 1)
17289 +#define AuHnJob_DIRENT         (1 << 2)
17290 +#define AuHnJob_ISDIR          (1 << 3)
17291 +#define AuHnJob_TRYXINO0       (1 << 4)
17292 +#define AuHnJob_MNTPNT         (1 << 5)
17293 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17294 +#define au_fset_hnjob(flags, name) \
17295 +       do { (flags) |= AuHnJob_##name; } while (0)
17296 +#define au_fclr_hnjob(flags, name) \
17297 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17298 +
17299 +enum {
17300 +       AuHn_CHILD,
17301 +       AuHn_PARENT,
17302 +       AuHnLast
17303 +};
17304 +
17305 +struct au_hnotify_args {
17306 +       struct inode *h_dir, *dir, *h_child_inode;
17307 +       u32 mask;
17308 +       unsigned int flags[AuHnLast];
17309 +       unsigned int h_child_nlen;
17310 +       char h_child_name[];
17311 +};
17312 +
17313 +struct hn_job_args {
17314 +       unsigned int flags;
17315 +       struct inode *inode, *h_inode, *dir, *h_dir;
17316 +       struct dentry *dentry;
17317 +       char *h_name;
17318 +       int h_nlen;
17319 +};
17320 +
17321 +static int hn_job(struct hn_job_args *a)
17322 +{
17323 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17324 +       int e;
17325 +
17326 +       /* reset xino */
17327 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17328 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17329 +
17330 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17331 +           && a->inode
17332 +           && a->h_inode) {
17333 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17334 +               if (!a->h_inode->i_nlink
17335 +                   && !(a->h_inode->i_state & I_LINKABLE))
17336 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17337 +               inode_unlock_shared(a->h_inode);
17338 +       }
17339 +
17340 +       /* make the generation obsolete */
17341 +       if (au_ftest_hnjob(a->flags, GEN)) {
17342 +               e = -1;
17343 +               if (a->inode)
17344 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17345 +                                             isdir);
17346 +               if (e && a->dentry)
17347 +                       hn_gen_by_name(a->dentry, isdir);
17348 +               /* ignore this error */
17349 +       }
17350 +
17351 +       /* make dir entries obsolete */
17352 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17353 +               struct au_vdir *vdir;
17354 +
17355 +               vdir = au_ivdir(a->inode);
17356 +               if (vdir)
17357 +                       vdir->vd_jiffy = 0;
17358 +               /* IMustLock(a->inode); */
17359 +               /* inode_inc_iversion(a->inode); */
17360 +       }
17361 +
17362 +       /* can do nothing but warn */
17363 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17364 +           && a->dentry
17365 +           && d_mountpoint(a->dentry))
17366 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17367 +
17368 +       return 0;
17369 +}
17370 +
17371 +/* ---------------------------------------------------------------------- */
17372 +
17373 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17374 +                                          struct inode *dir)
17375 +{
17376 +       struct dentry *dentry, *d, *parent;
17377 +       struct qstr *dname;
17378 +
17379 +       parent = d_find_any_alias(dir);
17380 +       if (!parent)
17381 +               return NULL;
17382 +
17383 +       dentry = NULL;
17384 +       spin_lock(&parent->d_lock);
17385 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17386 +               /* AuDbg("%pd\n", d); */
17387 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17388 +               dname = &d->d_name;
17389 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17390 +                       goto cont_unlock;
17391 +               if (au_di(d))
17392 +                       au_digen_dec(d);
17393 +               else
17394 +                       goto cont_unlock;
17395 +               if (au_dcount(d) > 0) {
17396 +                       dentry = dget_dlock(d);
17397 +                       spin_unlock(&d->d_lock);
17398 +                       break;
17399 +               }
17400 +
17401 +cont_unlock:
17402 +               spin_unlock(&d->d_lock);
17403 +       }
17404 +       spin_unlock(&parent->d_lock);
17405 +       dput(parent);
17406 +
17407 +       if (dentry)
17408 +               di_write_lock_child(dentry);
17409 +
17410 +       return dentry;
17411 +}
17412 +
17413 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17414 +                                        aufs_bindex_t bindex, ino_t h_ino)
17415 +{
17416 +       struct inode *inode;
17417 +       ino_t ino;
17418 +       int err;
17419 +
17420 +       inode = NULL;
17421 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17422 +       if (!err && ino)
17423 +               inode = ilookup(sb, ino);
17424 +       if (!inode)
17425 +               goto out;
17426 +
17427 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17428 +               pr_warn("wrong root branch\n");
17429 +               iput(inode);
17430 +               inode = NULL;
17431 +               goto out;
17432 +       }
17433 +
17434 +       ii_write_lock_child(inode);
17435 +
17436 +out:
17437 +       return inode;
17438 +}
17439 +
17440 +static void au_hn_bh(void *_args)
17441 +{
17442 +       struct au_hnotify_args *a = _args;
17443 +       struct super_block *sb;
17444 +       aufs_bindex_t bindex, bbot, bfound;
17445 +       unsigned char xino, try_iput;
17446 +       int err;
17447 +       struct inode *inode;
17448 +       ino_t h_ino;
17449 +       struct hn_job_args args;
17450 +       struct dentry *dentry;
17451 +       struct au_sbinfo *sbinfo;
17452 +
17453 +       AuDebugOn(!_args);
17454 +       AuDebugOn(!a->h_dir);
17455 +       AuDebugOn(!a->dir);
17456 +       AuDebugOn(!a->mask);
17457 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17458 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17459 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17460 +
17461 +       inode = NULL;
17462 +       dentry = NULL;
17463 +       /*
17464 +        * do not lock a->dir->i_mutex here
17465 +        * because of d_revalidate() may cause a deadlock.
17466 +        */
17467 +       sb = a->dir->i_sb;
17468 +       AuDebugOn(!sb);
17469 +       sbinfo = au_sbi(sb);
17470 +       AuDebugOn(!sbinfo);
17471 +       si_write_lock(sb, AuLock_NOPLMW);
17472 +
17473 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17474 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17475 +               case FS_MOVED_FROM:
17476 +               case FS_MOVED_TO:
17477 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17478 +                               "for the direct rename(2)\n");
17479 +               }
17480 +
17481 +       ii_read_lock_parent(a->dir);
17482 +       bfound = -1;
17483 +       bbot = au_ibbot(a->dir);
17484 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17485 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17486 +                       bfound = bindex;
17487 +                       break;
17488 +               }
17489 +       ii_read_unlock(a->dir);
17490 +       if (unlikely(bfound < 0))
17491 +               goto out;
17492 +
17493 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17494 +       h_ino = 0;
17495 +       if (a->h_child_inode)
17496 +               h_ino = a->h_child_inode->i_ino;
17497 +
17498 +       if (a->h_child_nlen
17499 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17500 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17501 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17502 +                                             a->dir);
17503 +       try_iput = 0;
17504 +       if (dentry && d_really_is_positive(dentry))
17505 +               inode = d_inode(dentry);
17506 +       if (xino && !inode && h_ino
17507 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17508 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17509 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17510 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17511 +               try_iput = 1;
17512 +       }
17513 +
17514 +       args.flags = a->flags[AuHn_CHILD];
17515 +       args.dentry = dentry;
17516 +       args.inode = inode;
17517 +       args.h_inode = a->h_child_inode;
17518 +       args.dir = a->dir;
17519 +       args.h_dir = a->h_dir;
17520 +       args.h_name = a->h_child_name;
17521 +       args.h_nlen = a->h_child_nlen;
17522 +       err = hn_job(&args);
17523 +       if (dentry) {
17524 +               if (au_di(dentry))
17525 +                       di_write_unlock(dentry);
17526 +               dput(dentry);
17527 +       }
17528 +       if (inode && try_iput) {
17529 +               ii_write_unlock(inode);
17530 +               iput(inode);
17531 +       }
17532 +
17533 +       ii_write_lock_parent(a->dir);
17534 +       args.flags = a->flags[AuHn_PARENT];
17535 +       args.dentry = NULL;
17536 +       args.inode = a->dir;
17537 +       args.h_inode = a->h_dir;
17538 +       args.dir = NULL;
17539 +       args.h_dir = NULL;
17540 +       args.h_name = NULL;
17541 +       args.h_nlen = 0;
17542 +       err = hn_job(&args);
17543 +       ii_write_unlock(a->dir);
17544 +
17545 +out:
17546 +       iput(a->h_child_inode);
17547 +       iput(a->h_dir);
17548 +       iput(a->dir);
17549 +       si_write_unlock(sb);
17550 +       au_nwt_done(&sbinfo->si_nowait);
17551 +       au_kfree_rcu(a);
17552 +}
17553 +
17554 +/* ---------------------------------------------------------------------- */
17555 +
17556 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17557 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17558 +{
17559 +       int err, len;
17560 +       unsigned int flags[AuHnLast], f;
17561 +       unsigned char isdir, isroot, wh;
17562 +       struct inode *dir;
17563 +       struct au_hnotify_args *args;
17564 +       char *p, *h_child_name;
17565 +
17566 +       err = 0;
17567 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17568 +       dir = igrab(hnotify->hn_aufs_inode);
17569 +       if (!dir)
17570 +               goto out;
17571 +
17572 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17573 +       wh = 0;
17574 +       h_child_name = (void *)h_child_qstr->name;
17575 +       len = h_child_qstr->len;
17576 +       if (h_child_name) {
17577 +               if (len > AUFS_WH_PFX_LEN
17578 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17579 +                       h_child_name += AUFS_WH_PFX_LEN;
17580 +                       len -= AUFS_WH_PFX_LEN;
17581 +                       wh = 1;
17582 +               }
17583 +       }
17584 +
17585 +       isdir = 0;
17586 +       if (h_child_inode)
17587 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17588 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17589 +       flags[AuHn_CHILD] = 0;
17590 +       if (isdir)
17591 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17592 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17593 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17594 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17595 +       case FS_MOVED_FROM:
17596 +       case FS_MOVED_TO:
17597 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17598 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17599 +               fallthrough;
17600 +       case FS_CREATE:
17601 +               AuDebugOn(!h_child_name);
17602 +               break;
17603 +
17604 +       case FS_DELETE:
17605 +               /*
17606 +                * aufs never be able to get this child inode.
17607 +                * revalidation should be in d_revalidate()
17608 +                * by checking i_nlink, i_generation or d_unhashed().
17609 +                */
17610 +               AuDebugOn(!h_child_name);
17611 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17612 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17613 +               break;
17614 +
17615 +       default:
17616 +               AuDebugOn(1);
17617 +       }
17618 +
17619 +       if (wh)
17620 +               h_child_inode = NULL;
17621 +
17622 +       err = -ENOMEM;
17623 +       /* iput() and kfree() will be called in au_hnotify() */
17624 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17625 +       if (unlikely(!args)) {
17626 +               AuErr1("no memory\n");
17627 +               iput(dir);
17628 +               goto out;
17629 +       }
17630 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17631 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17632 +       args->mask = mask;
17633 +       args->dir = dir;
17634 +       args->h_dir = igrab(h_dir);
17635 +       if (h_child_inode)
17636 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17637 +       args->h_child_inode = h_child_inode;
17638 +       args->h_child_nlen = len;
17639 +       if (len) {
17640 +               p = (void *)args;
17641 +               p += sizeof(*args);
17642 +               memcpy(p, h_child_name, len);
17643 +               p[len] = 0;
17644 +       }
17645 +
17646 +       /* NFS fires the event for silly-renamed one from kworker */
17647 +       f = 0;
17648 +       if (!dir->i_nlink
17649 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17650 +               f = AuWkq_NEST;
17651 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17652 +       if (unlikely(err)) {
17653 +               pr_err("wkq %d\n", err);
17654 +               iput(args->h_child_inode);
17655 +               iput(args->h_dir);
17656 +               iput(args->dir);
17657 +               au_kfree_rcu(args);
17658 +       }
17659 +
17660 +out:
17661 +       return err;
17662 +}
17663 +
17664 +/* ---------------------------------------------------------------------- */
17665 +
17666 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17667 +{
17668 +       int err;
17669 +
17670 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17671 +
17672 +       err = 0;
17673 +       if (au_hnotify_op.reset_br)
17674 +               err = au_hnotify_op.reset_br(udba, br, perm);
17675 +
17676 +       return err;
17677 +}
17678 +
17679 +int au_hnotify_init_br(struct au_branch *br, int perm)
17680 +{
17681 +       int err;
17682 +
17683 +       err = 0;
17684 +       if (au_hnotify_op.init_br)
17685 +               err = au_hnotify_op.init_br(br, perm);
17686 +
17687 +       return err;
17688 +}
17689 +
17690 +void au_hnotify_fin_br(struct au_branch *br)
17691 +{
17692 +       if (au_hnotify_op.fin_br)
17693 +               au_hnotify_op.fin_br(br);
17694 +}
17695 +
17696 +static void au_hn_destroy_cache(void)
17697 +{
17698 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17699 +       au_cache[AuCache_HNOTIFY] = NULL;
17700 +}
17701 +
17702 +int __init au_hnotify_init(void)
17703 +{
17704 +       int err;
17705 +
17706 +       err = -ENOMEM;
17707 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17708 +       if (au_cache[AuCache_HNOTIFY]) {
17709 +               err = 0;
17710 +               if (au_hnotify_op.init)
17711 +                       err = au_hnotify_op.init();
17712 +               if (unlikely(err))
17713 +                       au_hn_destroy_cache();
17714 +       }
17715 +       AuTraceErr(err);
17716 +       return err;
17717 +}
17718 +
17719 +void au_hnotify_fin(void)
17720 +{
17721 +       if (au_hnotify_op.fin)
17722 +               au_hnotify_op.fin();
17723 +
17724 +       /* cf. au_cache_fin() */
17725 +       if (au_cache[AuCache_HNOTIFY])
17726 +               au_hn_destroy_cache();
17727 +}
17728 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17729 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17730 +++ linux/fs/aufs/iinfo.c       2021-02-24 13:33:42.744347058 +0100
17731 @@ -0,0 +1,286 @@
17732 +// SPDX-License-Identifier: GPL-2.0
17733 +/*
17734 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17735 + *
17736 + * This program, aufs is free software; you can redistribute it and/or modify
17737 + * it under the terms of the GNU General Public License as published by
17738 + * the Free Software Foundation; either version 2 of the License, or
17739 + * (at your option) any later version.
17740 + *
17741 + * This program is distributed in the hope that it will be useful,
17742 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17743 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17744 + * GNU General Public License for more details.
17745 + *
17746 + * You should have received a copy of the GNU General Public License
17747 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17748 + */
17749 +
17750 +/*
17751 + * inode private data
17752 + */
17753 +
17754 +#include "aufs.h"
17755 +
17756 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17757 +{
17758 +       struct inode *h_inode;
17759 +       struct au_hinode *hinode;
17760 +
17761 +       IiMustAnyLock(inode);
17762 +
17763 +       hinode = au_hinode(au_ii(inode), bindex);
17764 +       h_inode = hinode->hi_inode;
17765 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17766 +       return h_inode;
17767 +}
17768 +
17769 +/* todo: hard/soft set? */
17770 +void au_hiput(struct au_hinode *hinode)
17771 +{
17772 +       au_hn_free(hinode);
17773 +       dput(hinode->hi_whdentry);
17774 +       iput(hinode->hi_inode);
17775 +}
17776 +
17777 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17778 +{
17779 +       unsigned int flags;
17780 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17781 +
17782 +       flags = 0;
17783 +       if (au_opt_test(mnt_flags, XINO))
17784 +               au_fset_hi(flags, XINO);
17785 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17786 +               au_fset_hi(flags, HNOTIFY);
17787 +       return flags;
17788 +}
17789 +
17790 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17791 +                  struct inode *h_inode, unsigned int flags)
17792 +{
17793 +       struct au_hinode *hinode;
17794 +       struct inode *hi;
17795 +       struct au_iinfo *iinfo = au_ii(inode);
17796 +
17797 +       IiMustWriteLock(inode);
17798 +
17799 +       hinode = au_hinode(iinfo, bindex);
17800 +       hi = hinode->hi_inode;
17801 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17802 +
17803 +       if (hi)
17804 +               au_hiput(hinode);
17805 +       hinode->hi_inode = h_inode;
17806 +       if (h_inode) {
17807 +               int err;
17808 +               struct super_block *sb = inode->i_sb;
17809 +               struct au_branch *br;
17810 +
17811 +               AuDebugOn(inode->i_mode
17812 +                         && (h_inode->i_mode & S_IFMT)
17813 +                         != (inode->i_mode & S_IFMT));
17814 +               if (bindex == iinfo->ii_btop)
17815 +                       au_cpup_igen(inode, h_inode);
17816 +               br = au_sbr(sb, bindex);
17817 +               hinode->hi_id = br->br_id;
17818 +               if (au_ftest_hi(flags, XINO)) {
17819 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17820 +                                           inode->i_ino);
17821 +                       if (unlikely(err))
17822 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17823 +               }
17824 +
17825 +               if (au_ftest_hi(flags, HNOTIFY)
17826 +                   && au_br_hnotifyable(br->br_perm)) {
17827 +                       err = au_hn_alloc(hinode, inode);
17828 +                       if (unlikely(err))
17829 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17830 +               }
17831 +       }
17832 +}
17833 +
17834 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17835 +                 struct dentry *h_wh)
17836 +{
17837 +       struct au_hinode *hinode;
17838 +
17839 +       IiMustWriteLock(inode);
17840 +
17841 +       hinode = au_hinode(au_ii(inode), bindex);
17842 +       AuDebugOn(hinode->hi_whdentry);
17843 +       hinode->hi_whdentry = h_wh;
17844 +}
17845 +
17846 +void au_update_iigen(struct inode *inode, int half)
17847 +{
17848 +       struct au_iinfo *iinfo;
17849 +       struct au_iigen *iigen;
17850 +       unsigned int sigen;
17851 +
17852 +       sigen = au_sigen(inode->i_sb);
17853 +       iinfo = au_ii(inode);
17854 +       iigen = &iinfo->ii_generation;
17855 +       spin_lock(&iigen->ig_spin);
17856 +       iigen->ig_generation = sigen;
17857 +       if (half)
17858 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17859 +       else
17860 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17861 +       spin_unlock(&iigen->ig_spin);
17862 +}
17863 +
17864 +/* it may be called at remount time, too */
17865 +void au_update_ibrange(struct inode *inode, int do_put_zero)
17866 +{
17867 +       struct au_iinfo *iinfo;
17868 +       aufs_bindex_t bindex, bbot;
17869 +
17870 +       AuDebugOn(au_is_bad_inode(inode));
17871 +       IiMustWriteLock(inode);
17872 +
17873 +       iinfo = au_ii(inode);
17874 +       if (do_put_zero && iinfo->ii_btop >= 0) {
17875 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17876 +                    bindex++) {
17877 +                       struct inode *h_i;
17878 +
17879 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
17880 +                       if (h_i
17881 +                           && !h_i->i_nlink
17882 +                           && !(h_i->i_state & I_LINKABLE))
17883 +                               au_set_h_iptr(inode, bindex, NULL, 0);
17884 +               }
17885 +       }
17886 +
17887 +       iinfo->ii_btop = -1;
17888 +       iinfo->ii_bbot = -1;
17889 +       bbot = au_sbbot(inode->i_sb);
17890 +       for (bindex = 0; bindex <= bbot; bindex++)
17891 +               if (au_hinode(iinfo, bindex)->hi_inode) {
17892 +                       iinfo->ii_btop = bindex;
17893 +                       break;
17894 +               }
17895 +       if (iinfo->ii_btop >= 0)
17896 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17897 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
17898 +                               iinfo->ii_bbot = bindex;
17899 +                               break;
17900 +                       }
17901 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17902 +}
17903 +
17904 +/* ---------------------------------------------------------------------- */
17905 +
17906 +void au_icntnr_init_once(void *_c)
17907 +{
17908 +       struct au_icntnr *c = _c;
17909 +       struct au_iinfo *iinfo = &c->iinfo;
17910 +
17911 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
17912 +       au_rw_init(&iinfo->ii_rwsem);
17913 +       inode_init_once(&c->vfs_inode);
17914 +}
17915 +
17916 +void au_hinode_init(struct au_hinode *hinode)
17917 +{
17918 +       hinode->hi_inode = NULL;
17919 +       hinode->hi_id = -1;
17920 +       au_hn_init(hinode);
17921 +       hinode->hi_whdentry = NULL;
17922 +}
17923 +
17924 +int au_iinfo_init(struct inode *inode)
17925 +{
17926 +       struct au_iinfo *iinfo;
17927 +       struct super_block *sb;
17928 +       struct au_hinode *hi;
17929 +       int nbr, i;
17930 +
17931 +       sb = inode->i_sb;
17932 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17933 +       nbr = au_sbbot(sb) + 1;
17934 +       if (unlikely(nbr <= 0))
17935 +               nbr = 1;
17936 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17937 +       if (hi) {
17938 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17939 +
17940 +               iinfo->ii_hinode = hi;
17941 +               for (i = 0; i < nbr; i++, hi++)
17942 +                       au_hinode_init(hi);
17943 +
17944 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
17945 +               iinfo->ii_btop = -1;
17946 +               iinfo->ii_bbot = -1;
17947 +               iinfo->ii_vdir = NULL;
17948 +               return 0;
17949 +       }
17950 +       return -ENOMEM;
17951 +}
17952 +
17953 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17954 +{
17955 +       int err, i;
17956 +       struct au_hinode *hip;
17957 +
17958 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
17959 +
17960 +       err = -ENOMEM;
17961 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17962 +                         may_shrink);
17963 +       if (hip) {
17964 +               iinfo->ii_hinode = hip;
17965 +               i = iinfo->ii_bbot + 1;
17966 +               hip += i;
17967 +               for (; i < nbr; i++, hip++)
17968 +                       au_hinode_init(hip);
17969 +               err = 0;
17970 +       }
17971 +
17972 +       return err;
17973 +}
17974 +
17975 +void au_iinfo_fin(struct inode *inode)
17976 +{
17977 +       struct au_iinfo *iinfo;
17978 +       struct au_hinode *hi;
17979 +       struct super_block *sb;
17980 +       aufs_bindex_t bindex, bbot;
17981 +       const unsigned char unlinked = !inode->i_nlink;
17982 +
17983 +       AuDebugOn(au_is_bad_inode(inode));
17984 +
17985 +       sb = inode->i_sb;
17986 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
17987 +       if (si_pid_test(sb))
17988 +               au_xino_delete_inode(inode, unlinked);
17989 +       else {
17990 +               /*
17991 +                * it is safe to hide the dependency between sbinfo and
17992 +                * sb->s_umount.
17993 +                */
17994 +               lockdep_off();
17995 +               si_noflush_read_lock(sb);
17996 +               au_xino_delete_inode(inode, unlinked);
17997 +               si_read_unlock(sb);
17998 +               lockdep_on();
17999 +       }
18000 +
18001 +       iinfo = au_ii(inode);
18002 +       if (iinfo->ii_vdir)
18003 +               au_vdir_free(iinfo->ii_vdir);
18004 +
18005 +       bindex = iinfo->ii_btop;
18006 +       if (bindex >= 0) {
18007 +               hi = au_hinode(iinfo, bindex);
18008 +               bbot = iinfo->ii_bbot;
18009 +               while (bindex++ <= bbot) {
18010 +                       if (hi->hi_inode)
18011 +                               au_hiput(hi);
18012 +                       hi++;
18013 +               }
18014 +       }
18015 +       au_kfree_rcu(iinfo->ii_hinode);
18016 +       AuRwDestroy(&iinfo->ii_rwsem);
18017 +}
18018 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18019 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18020 +++ linux/fs/aufs/inode.c       2021-02-24 13:33:42.744347058 +0100
18021 @@ -0,0 +1,529 @@
18022 +// SPDX-License-Identifier: GPL-2.0
18023 +/*
18024 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18025 + *
18026 + * This program, aufs is free software; you can redistribute it and/or modify
18027 + * it under the terms of the GNU General Public License as published by
18028 + * the Free Software Foundation; either version 2 of the License, or
18029 + * (at your option) any later version.
18030 + *
18031 + * This program is distributed in the hope that it will be useful,
18032 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18033 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18034 + * GNU General Public License for more details.
18035 + *
18036 + * You should have received a copy of the GNU General Public License
18037 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18038 + */
18039 +
18040 +/*
18041 + * inode functions
18042 + */
18043 +
18044 +#include <linux/iversion.h>
18045 +#include "aufs.h"
18046 +
18047 +struct inode *au_igrab(struct inode *inode)
18048 +{
18049 +       if (inode) {
18050 +               AuDebugOn(!atomic_read(&inode->i_count));
18051 +               ihold(inode);
18052 +       }
18053 +       return inode;
18054 +}
18055 +
18056 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18057 +{
18058 +       au_cpup_attr_all(inode, /*force*/0);
18059 +       au_update_iigen(inode, /*half*/1);
18060 +       if (do_version)
18061 +               inode_inc_iversion(inode);
18062 +}
18063 +
18064 +static int au_ii_refresh(struct inode *inode, int *update)
18065 +{
18066 +       int err, e, nbr;
18067 +       umode_t type;
18068 +       aufs_bindex_t bindex, new_bindex;
18069 +       struct super_block *sb;
18070 +       struct au_iinfo *iinfo;
18071 +       struct au_hinode *p, *q, tmp;
18072 +
18073 +       AuDebugOn(au_is_bad_inode(inode));
18074 +       IiMustWriteLock(inode);
18075 +
18076 +       *update = 0;
18077 +       sb = inode->i_sb;
18078 +       nbr = au_sbbot(sb) + 1;
18079 +       type = inode->i_mode & S_IFMT;
18080 +       iinfo = au_ii(inode);
18081 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18082 +       if (unlikely(err))
18083 +               goto out;
18084 +
18085 +       AuDebugOn(iinfo->ii_btop < 0);
18086 +       p = au_hinode(iinfo, iinfo->ii_btop);
18087 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18088 +            bindex++, p++) {
18089 +               if (!p->hi_inode)
18090 +                       continue;
18091 +
18092 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18093 +               new_bindex = au_br_index(sb, p->hi_id);
18094 +               if (new_bindex == bindex)
18095 +                       continue;
18096 +
18097 +               if (new_bindex < 0) {
18098 +                       *update = 1;
18099 +                       au_hiput(p);
18100 +                       p->hi_inode = NULL;
18101 +                       continue;
18102 +               }
18103 +
18104 +               if (new_bindex < iinfo->ii_btop)
18105 +                       iinfo->ii_btop = new_bindex;
18106 +               if (iinfo->ii_bbot < new_bindex)
18107 +                       iinfo->ii_bbot = new_bindex;
18108 +               /* swap two lower inode, and loop again */
18109 +               q = au_hinode(iinfo, new_bindex);
18110 +               tmp = *q;
18111 +               *q = *p;
18112 +               *p = tmp;
18113 +               if (tmp.hi_inode) {
18114 +                       bindex--;
18115 +                       p--;
18116 +               }
18117 +       }
18118 +       au_update_ibrange(inode, /*do_put_zero*/0);
18119 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18120 +       e = au_dy_irefresh(inode);
18121 +       if (unlikely(e && !err))
18122 +               err = e;
18123 +
18124 +out:
18125 +       AuTraceErr(err);
18126 +       return err;
18127 +}
18128 +
18129 +void au_refresh_iop(struct inode *inode, int force_getattr)
18130 +{
18131 +       int type;
18132 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18133 +       const struct inode_operations *iop
18134 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18135 +
18136 +       if (inode->i_op == iop)
18137 +               return;
18138 +
18139 +       switch (inode->i_mode & S_IFMT) {
18140 +       case S_IFDIR:
18141 +               type = AuIop_DIR;
18142 +               break;
18143 +       case S_IFLNK:
18144 +               type = AuIop_SYMLINK;
18145 +               break;
18146 +       default:
18147 +               type = AuIop_OTHER;
18148 +               break;
18149 +       }
18150 +
18151 +       inode->i_op = iop + type;
18152 +       /* unnecessary smp_wmb() */
18153 +}
18154 +
18155 +int au_refresh_hinode_self(struct inode *inode)
18156 +{
18157 +       int err, update;
18158 +
18159 +       err = au_ii_refresh(inode, &update);
18160 +       if (!err)
18161 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18162 +
18163 +       AuTraceErr(err);
18164 +       return err;
18165 +}
18166 +
18167 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18168 +{
18169 +       int err, e, update;
18170 +       unsigned int flags;
18171 +       umode_t mode;
18172 +       aufs_bindex_t bindex, bbot;
18173 +       unsigned char isdir;
18174 +       struct au_hinode *p;
18175 +       struct au_iinfo *iinfo;
18176 +
18177 +       err = au_ii_refresh(inode, &update);
18178 +       if (unlikely(err))
18179 +               goto out;
18180 +
18181 +       update = 0;
18182 +       iinfo = au_ii(inode);
18183 +       p = au_hinode(iinfo, iinfo->ii_btop);
18184 +       mode = (inode->i_mode & S_IFMT);
18185 +       isdir = S_ISDIR(mode);
18186 +       flags = au_hi_flags(inode, isdir);
18187 +       bbot = au_dbbot(dentry);
18188 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18189 +               struct inode *h_i, *h_inode;
18190 +               struct dentry *h_d;
18191 +
18192 +               h_d = au_h_dptr(dentry, bindex);
18193 +               if (!h_d || d_is_negative(h_d))
18194 +                       continue;
18195 +
18196 +               h_inode = d_inode(h_d);
18197 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18198 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18199 +                       h_i = au_h_iptr(inode, bindex);
18200 +                       if (h_i) {
18201 +                               if (h_i == h_inode)
18202 +                                       continue;
18203 +                               err = -EIO;
18204 +                               break;
18205 +                       }
18206 +               }
18207 +               if (bindex < iinfo->ii_btop)
18208 +                       iinfo->ii_btop = bindex;
18209 +               if (iinfo->ii_bbot < bindex)
18210 +                       iinfo->ii_bbot = bindex;
18211 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18212 +               update = 1;
18213 +       }
18214 +       au_update_ibrange(inode, /*do_put_zero*/0);
18215 +       e = au_dy_irefresh(inode);
18216 +       if (unlikely(e && !err))
18217 +               err = e;
18218 +       if (!err)
18219 +               au_refresh_hinode_attr(inode, update && isdir);
18220 +
18221 +out:
18222 +       AuTraceErr(err);
18223 +       return err;
18224 +}
18225 +
18226 +static int set_inode(struct inode *inode, struct dentry *dentry)
18227 +{
18228 +       int err;
18229 +       unsigned int flags;
18230 +       umode_t mode;
18231 +       aufs_bindex_t bindex, btop, btail;
18232 +       unsigned char isdir;
18233 +       struct dentry *h_dentry;
18234 +       struct inode *h_inode;
18235 +       struct au_iinfo *iinfo;
18236 +       const struct inode_operations *iop;
18237 +
18238 +       IiMustWriteLock(inode);
18239 +
18240 +       err = 0;
18241 +       isdir = 0;
18242 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18243 +       btop = au_dbtop(dentry);
18244 +       h_dentry = au_h_dptr(dentry, btop);
18245 +       h_inode = d_inode(h_dentry);
18246 +       mode = h_inode->i_mode;
18247 +       switch (mode & S_IFMT) {
18248 +       case S_IFREG:
18249 +               btail = au_dbtail(dentry);
18250 +               inode->i_op = iop + AuIop_OTHER;
18251 +               inode->i_fop = &aufs_file_fop;
18252 +               err = au_dy_iaop(inode, btop, h_inode);
18253 +               if (unlikely(err))
18254 +                       goto out;
18255 +               break;
18256 +       case S_IFDIR:
18257 +               isdir = 1;
18258 +               btail = au_dbtaildir(dentry);
18259 +               inode->i_op = iop + AuIop_DIR;
18260 +               inode->i_fop = &aufs_dir_fop;
18261 +               break;
18262 +       case S_IFLNK:
18263 +               btail = au_dbtail(dentry);
18264 +               inode->i_op = iop + AuIop_SYMLINK;
18265 +               break;
18266 +       case S_IFBLK:
18267 +       case S_IFCHR:
18268 +       case S_IFIFO:
18269 +       case S_IFSOCK:
18270 +               btail = au_dbtail(dentry);
18271 +               inode->i_op = iop + AuIop_OTHER;
18272 +               init_special_inode(inode, mode, h_inode->i_rdev);
18273 +               break;
18274 +       default:
18275 +               AuIOErr("Unknown file type 0%o\n", mode);
18276 +               err = -EIO;
18277 +               goto out;
18278 +       }
18279 +
18280 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18281 +       flags = au_hi_flags(inode, isdir);
18282 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18283 +           && au_ftest_hi(flags, HNOTIFY)
18284 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18285 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18286 +               au_fclr_hi(flags, HNOTIFY);
18287 +       iinfo = au_ii(inode);
18288 +       iinfo->ii_btop = btop;
18289 +       iinfo->ii_bbot = btail;
18290 +       for (bindex = btop; bindex <= btail; bindex++) {
18291 +               h_dentry = au_h_dptr(dentry, bindex);
18292 +               if (h_dentry)
18293 +                       au_set_h_iptr(inode, bindex,
18294 +                                     au_igrab(d_inode(h_dentry)), flags);
18295 +       }
18296 +       au_cpup_attr_all(inode, /*force*/1);
18297 +       /*
18298 +        * to force calling aufs_get_acl() every time,
18299 +        * do not call cache_no_acl() for aufs inode.
18300 +        */
18301 +
18302 +out:
18303 +       return err;
18304 +}
18305 +
18306 +/*
18307 + * successful returns with iinfo write_locked
18308 + * minus: errno
18309 + * zero: success, matched
18310 + * plus: no error, but unmatched
18311 + */
18312 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18313 +{
18314 +       int err;
18315 +       unsigned int gen, igflags;
18316 +       aufs_bindex_t bindex, bbot;
18317 +       struct inode *h_inode, *h_dinode;
18318 +       struct dentry *h_dentry;
18319 +
18320 +       /*
18321 +        * before this function, if aufs got any iinfo lock, it must be only
18322 +        * one, the parent dir.
18323 +        * it can happen by UDBA and the obsoleted inode number.
18324 +        */
18325 +       err = -EIO;
18326 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18327 +               goto out;
18328 +
18329 +       err = 1;
18330 +       ii_write_lock_new_child(inode);
18331 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18332 +       h_dinode = d_inode(h_dentry);
18333 +       bbot = au_ibbot(inode);
18334 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18335 +               h_inode = au_h_iptr(inode, bindex);
18336 +               if (!h_inode || h_inode != h_dinode)
18337 +                       continue;
18338 +
18339 +               err = 0;
18340 +               gen = au_iigen(inode, &igflags);
18341 +               if (gen == au_digen(dentry)
18342 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18343 +                       break;
18344 +
18345 +               /* fully refresh inode using dentry */
18346 +               err = au_refresh_hinode(inode, dentry);
18347 +               if (!err)
18348 +                       au_update_iigen(inode, /*half*/0);
18349 +               break;
18350 +       }
18351 +
18352 +       if (unlikely(err))
18353 +               ii_write_unlock(inode);
18354 +out:
18355 +       return err;
18356 +}
18357 +
18358 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18359 +          unsigned int d_type, ino_t *ino)
18360 +{
18361 +       int err, idx;
18362 +       const int isnondir = d_type != DT_DIR;
18363 +
18364 +       /* prevent hardlinked inode number from race condition */
18365 +       if (isnondir) {
18366 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18367 +               if (unlikely(err))
18368 +                       goto out;
18369 +       }
18370 +
18371 +       err = au_xino_read(sb, bindex, h_ino, ino);
18372 +       if (unlikely(err))
18373 +               goto out_xinondir;
18374 +
18375 +       if (!*ino) {
18376 +               err = -EIO;
18377 +               *ino = au_xino_new_ino(sb);
18378 +               if (unlikely(!*ino))
18379 +                       goto out_xinondir;
18380 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18381 +               if (unlikely(err))
18382 +                       goto out_xinondir;
18383 +       }
18384 +
18385 +out_xinondir:
18386 +       if (isnondir && idx >= 0)
18387 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18388 +out:
18389 +       return err;
18390 +}
18391 +
18392 +/* successful returns with iinfo write_locked */
18393 +/* todo: return with unlocked? */
18394 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18395 +{
18396 +       struct inode *inode, *h_inode;
18397 +       struct dentry *h_dentry;
18398 +       struct super_block *sb;
18399 +       ino_t h_ino, ino;
18400 +       int err, idx, hlinked;
18401 +       aufs_bindex_t btop;
18402 +
18403 +       sb = dentry->d_sb;
18404 +       btop = au_dbtop(dentry);
18405 +       h_dentry = au_h_dptr(dentry, btop);
18406 +       h_inode = d_inode(h_dentry);
18407 +       h_ino = h_inode->i_ino;
18408 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18409 +
18410 +new_ino:
18411 +       /*
18412 +        * stop 'race'-ing between hardlinks under different
18413 +        * parents.
18414 +        */
18415 +       if (hlinked) {
18416 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18417 +               inode = ERR_PTR(err);
18418 +               if (unlikely(err))
18419 +                       goto out;
18420 +       }
18421 +
18422 +       err = au_xino_read(sb, btop, h_ino, &ino);
18423 +       inode = ERR_PTR(err);
18424 +       if (unlikely(err))
18425 +               goto out_xinondir;
18426 +
18427 +       if (!ino) {
18428 +               ino = au_xino_new_ino(sb);
18429 +               if (unlikely(!ino)) {
18430 +                       inode = ERR_PTR(-EIO);
18431 +                       goto out_xinondir;
18432 +               }
18433 +       }
18434 +
18435 +       AuDbg("i%lu\n", (unsigned long)ino);
18436 +       inode = au_iget_locked(sb, ino);
18437 +       err = PTR_ERR(inode);
18438 +       if (IS_ERR(inode))
18439 +               goto out_xinondir;
18440 +
18441 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18442 +       if (inode->i_state & I_NEW) {
18443 +               ii_write_lock_new_child(inode);
18444 +               err = set_inode(inode, dentry);
18445 +               if (!err) {
18446 +                       unlock_new_inode(inode);
18447 +                       goto out_xinondir; /* success */
18448 +               }
18449 +
18450 +               /*
18451 +                * iget_failed() calls iput(), but we need to call
18452 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18453 +                * i_count.
18454 +                */
18455 +               atomic_inc(&inode->i_count);
18456 +               iget_failed(inode);
18457 +               ii_write_unlock(inode);
18458 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18459 +               /* ignore this error */
18460 +               goto out_iput;
18461 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18462 +               /*
18463 +                * horrible race condition between lookup, readdir and copyup
18464 +                * (or something).
18465 +                */
18466 +               if (hlinked && idx >= 0)
18467 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18468 +               err = reval_inode(inode, dentry);
18469 +               if (unlikely(err < 0)) {
18470 +                       hlinked = 0;
18471 +                       goto out_iput;
18472 +               }
18473 +               if (!err)
18474 +                       goto out; /* success */
18475 +               else if (hlinked && idx >= 0) {
18476 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18477 +                       if (unlikely(err)) {
18478 +                               iput(inode);
18479 +                               inode = ERR_PTR(err);
18480 +                               goto out;
18481 +                       }
18482 +               }
18483 +       }
18484 +
18485 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18486 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18487 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18488 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18489 +                       (unsigned long)h_ino, (unsigned long)ino);
18490 +       ino = 0;
18491 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18492 +       if (!err) {
18493 +               iput(inode);
18494 +               if (hlinked && idx >= 0)
18495 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18496 +               goto new_ino;
18497 +       }
18498 +
18499 +out_iput:
18500 +       iput(inode);
18501 +       inode = ERR_PTR(err);
18502 +out_xinondir:
18503 +       if (hlinked && idx >= 0)
18504 +               au_xinondir_leave(sb, btop, h_ino, idx);
18505 +out:
18506 +       return inode;
18507 +}
18508 +
18509 +/* ---------------------------------------------------------------------- */
18510 +
18511 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18512 +              struct inode *inode)
18513 +{
18514 +       int err;
18515 +       struct inode *hi;
18516 +
18517 +       err = au_br_rdonly(au_sbr(sb, bindex));
18518 +
18519 +       /* pseudo-link after flushed may happen out of bounds */
18520 +       if (!err
18521 +           && inode
18522 +           && au_ibtop(inode) <= bindex
18523 +           && bindex <= au_ibbot(inode)) {
18524 +               /*
18525 +                * permission check is unnecessary since vfsub routine
18526 +                * will be called later
18527 +                */
18528 +               hi = au_h_iptr(inode, bindex);
18529 +               if (hi)
18530 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18531 +       }
18532 +
18533 +       return err;
18534 +}
18535 +
18536 +int au_test_h_perm(struct inode *h_inode, int mask)
18537 +{
18538 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18539 +               return 0;
18540 +       return inode_permission(h_inode, mask);
18541 +}
18542 +
18543 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18544 +{
18545 +       if (au_test_nfs(h_inode->i_sb)
18546 +           && (mask & MAY_WRITE)
18547 +           && S_ISDIR(h_inode->i_mode))
18548 +               mask |= MAY_READ; /* force permission check */
18549 +       return au_test_h_perm(h_inode, mask);
18550 +}
18551 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18552 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18553 +++ linux/fs/aufs/inode.h       2021-02-24 13:33:42.744347058 +0100
18554 @@ -0,0 +1,698 @@
18555 +/* SPDX-License-Identifier: GPL-2.0 */
18556 +/*
18557 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18558 + *
18559 + * This program, aufs is free software; you can redistribute it and/or modify
18560 + * it under the terms of the GNU General Public License as published by
18561 + * the Free Software Foundation; either version 2 of the License, or
18562 + * (at your option) any later version.
18563 + *
18564 + * This program is distributed in the hope that it will be useful,
18565 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18566 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18567 + * GNU General Public License for more details.
18568 + *
18569 + * You should have received a copy of the GNU General Public License
18570 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18571 + */
18572 +
18573 +/*
18574 + * inode operations
18575 + */
18576 +
18577 +#ifndef __AUFS_INODE_H__
18578 +#define __AUFS_INODE_H__
18579 +
18580 +#ifdef __KERNEL__
18581 +
18582 +#include <linux/fsnotify.h>
18583 +#include "rwsem.h"
18584 +
18585 +struct vfsmount;
18586 +
18587 +struct au_hnotify {
18588 +#ifdef CONFIG_AUFS_HNOTIFY
18589 +#ifdef CONFIG_AUFS_HFSNOTIFY
18590 +       /* never use fsnotify_add_vfsmount_mark() */
18591 +       struct fsnotify_mark            hn_mark;
18592 +#endif
18593 +       struct inode            *hn_aufs_inode; /* no get/put */
18594 +       struct rcu_head         rcu;
18595 +#endif
18596 +} ____cacheline_aligned_in_smp;
18597 +
18598 +struct au_hinode {
18599 +       struct inode            *hi_inode;
18600 +       aufs_bindex_t           hi_id;
18601 +#ifdef CONFIG_AUFS_HNOTIFY
18602 +       struct au_hnotify       *hi_notify;
18603 +#endif
18604 +
18605 +       /* reference to the copied-up whiteout with get/put */
18606 +       struct dentry           *hi_whdentry;
18607 +};
18608 +
18609 +/* ig_flags */
18610 +#define AuIG_HALF_REFRESHED            1
18611 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18612 +#define au_ig_fset(flags, name) \
18613 +       do { (flags) |= AuIG_##name; } while (0)
18614 +#define au_ig_fclr(flags, name) \
18615 +       do { (flags) &= ~AuIG_##name; } while (0)
18616 +
18617 +struct au_iigen {
18618 +       spinlock_t      ig_spin;
18619 +       __u32           ig_generation, ig_flags;
18620 +};
18621 +
18622 +struct au_vdir;
18623 +struct au_iinfo {
18624 +       struct au_iigen         ii_generation;
18625 +       struct super_block      *ii_hsb1;       /* no get/put */
18626 +
18627 +       struct au_rwsem         ii_rwsem;
18628 +       aufs_bindex_t           ii_btop, ii_bbot;
18629 +       __u32                   ii_higen;
18630 +       struct au_hinode        *ii_hinode;
18631 +       struct au_vdir          *ii_vdir;
18632 +};
18633 +
18634 +struct au_icntnr {
18635 +       struct au_iinfo         iinfo;
18636 +       struct inode            vfs_inode;
18637 +       struct hlist_bl_node    plink;
18638 +       struct rcu_head         rcu;
18639 +} ____cacheline_aligned_in_smp;
18640 +
18641 +/* au_pin flags */
18642 +#define AuPin_DI_LOCKED                1
18643 +#define AuPin_MNT_WRITE                (1 << 1)
18644 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18645 +#define au_fset_pin(flags, name) \
18646 +       do { (flags) |= AuPin_##name; } while (0)
18647 +#define au_fclr_pin(flags, name) \
18648 +       do { (flags) &= ~AuPin_##name; } while (0)
18649 +
18650 +struct au_pin {
18651 +       /* input */
18652 +       struct dentry *dentry;
18653 +       unsigned int udba;
18654 +       unsigned char lsc_di, lsc_hi, flags;
18655 +       aufs_bindex_t bindex;
18656 +
18657 +       /* output */
18658 +       struct dentry *parent;
18659 +       struct au_hinode *hdir;
18660 +       struct vfsmount *h_mnt;
18661 +
18662 +       /* temporary unlock/relock for copyup */
18663 +       struct dentry *h_dentry, *h_parent;
18664 +       struct au_branch *br;
18665 +       struct task_struct *task;
18666 +};
18667 +
18668 +void au_pin_hdir_unlock(struct au_pin *p);
18669 +int au_pin_hdir_lock(struct au_pin *p);
18670 +int au_pin_hdir_relock(struct au_pin *p);
18671 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18672 +void au_pin_hdir_release(struct au_pin *p);
18673 +
18674 +/* ---------------------------------------------------------------------- */
18675 +
18676 +static inline struct au_iinfo *au_ii(struct inode *inode)
18677 +{
18678 +       BUG_ON(is_bad_inode(inode));
18679 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18680 +}
18681 +
18682 +/* ---------------------------------------------------------------------- */
18683 +
18684 +/* inode.c */
18685 +struct inode *au_igrab(struct inode *inode);
18686 +void au_refresh_iop(struct inode *inode, int force_getattr);
18687 +int au_refresh_hinode_self(struct inode *inode);
18688 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18689 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18690 +          unsigned int d_type, ino_t *ino);
18691 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18692 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18693 +              struct inode *inode);
18694 +int au_test_h_perm(struct inode *h_inode, int mask);
18695 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18696 +
18697 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18698 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18699 +{
18700 +#ifdef CONFIG_AUFS_SHWH
18701 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18702 +#else
18703 +       return 0;
18704 +#endif
18705 +}
18706 +
18707 +/* i_op.c */
18708 +enum {
18709 +       AuIop_SYMLINK,
18710 +       AuIop_DIR,
18711 +       AuIop_OTHER,
18712 +       AuIop_Last
18713 +};
18714 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18715 +       aufs_iop_nogetattr[AuIop_Last];
18716 +
18717 +/* au_wr_dir flags */
18718 +#define AuWrDir_ADD_ENTRY      1
18719 +#define AuWrDir_ISDIR          (1 << 1)
18720 +#define AuWrDir_TMPFILE                (1 << 2)
18721 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18722 +#define au_fset_wrdir(flags, name) \
18723 +       do { (flags) |= AuWrDir_##name; } while (0)
18724 +#define au_fclr_wrdir(flags, name) \
18725 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18726 +
18727 +struct au_wr_dir_args {
18728 +       aufs_bindex_t force_btgt;
18729 +       unsigned char flags;
18730 +};
18731 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18732 +             struct au_wr_dir_args *args);
18733 +
18734 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18735 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18736 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18737 +                unsigned int udba, unsigned char flags);
18738 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18739 +          unsigned int udba, unsigned char flags) __must_check;
18740 +int au_do_pin(struct au_pin *pin) __must_check;
18741 +void au_unpin(struct au_pin *pin);
18742 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18743 +
18744 +#define AuIcpup_DID_CPUP       1
18745 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18746 +#define au_fset_icpup(flags, name) \
18747 +       do { (flags) |= AuIcpup_##name; } while (0)
18748 +#define au_fclr_icpup(flags, name) \
18749 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18750 +
18751 +struct au_icpup_args {
18752 +       unsigned char flags;
18753 +       unsigned char pin_flags;
18754 +       aufs_bindex_t btgt;
18755 +       unsigned int udba;
18756 +       struct au_pin pin;
18757 +       struct path h_path;
18758 +       struct inode *h_inode;
18759 +};
18760 +
18761 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18762 +                    struct au_icpup_args *a);
18763 +
18764 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18765 +                     struct path *h_path, int locked);
18766 +
18767 +/* i_op_add.c */
18768 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18769 +              struct dentry *h_parent, int isdir);
18770 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18771 +              dev_t dev);
18772 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18773 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18774 +               bool want_excl);
18775 +struct vfsub_aopen_args;
18776 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18777 +                      struct vfsub_aopen_args *args);
18778 +int aufs_tmpfile(struct inode *dir, 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 inode *dir, struct dentry *dentry, umode_t mode);
18782 +
18783 +/* i_op_del.c */
18784 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18785 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18786 +              struct dentry *h_parent, int isdir);
18787 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18788 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18789 +
18790 +/* i_op_ren.c */
18791 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18792 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18793 +               struct inode *dir, struct dentry *dentry,
18794 +               unsigned int flags);
18795 +
18796 +/* iinfo.c */
18797 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18798 +void au_hiput(struct au_hinode *hinode);
18799 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18800 +                 struct dentry *h_wh);
18801 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18802 +
18803 +/* hinode flags */
18804 +#define AuHi_XINO      1
18805 +#define AuHi_HNOTIFY   (1 << 1)
18806 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18807 +#define au_fset_hi(flags, name) \
18808 +       do { (flags) |= AuHi_##name; } while (0)
18809 +#define au_fclr_hi(flags, name) \
18810 +       do { (flags) &= ~AuHi_##name; } while (0)
18811 +
18812 +#ifndef CONFIG_AUFS_HNOTIFY
18813 +#undef AuHi_HNOTIFY
18814 +#define AuHi_HNOTIFY   0
18815 +#endif
18816 +
18817 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18818 +                  struct inode *h_inode, unsigned int flags);
18819 +
18820 +void au_update_iigen(struct inode *inode, int half);
18821 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18822 +
18823 +void au_icntnr_init_once(void *_c);
18824 +void au_hinode_init(struct au_hinode *hinode);
18825 +int au_iinfo_init(struct inode *inode);
18826 +void au_iinfo_fin(struct inode *inode);
18827 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18828 +
18829 +#ifdef CONFIG_PROC_FS
18830 +/* plink.c */
18831 +int au_plink_maint(struct super_block *sb, int flags);
18832 +struct au_sbinfo;
18833 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18834 +int au_plink_maint_enter(struct super_block *sb);
18835 +#ifdef CONFIG_AUFS_DEBUG
18836 +void au_plink_list(struct super_block *sb);
18837 +#else
18838 +AuStubVoid(au_plink_list, struct super_block *sb)
18839 +#endif
18840 +int au_plink_test(struct inode *inode);
18841 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18842 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18843 +                    struct dentry *h_dentry);
18844 +void au_plink_put(struct super_block *sb, int verbose);
18845 +void au_plink_clean(struct super_block *sb, int verbose);
18846 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18847 +#else
18848 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18849 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18850 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18851 +AuStubVoid(au_plink_list, struct super_block *sb);
18852 +AuStubInt0(au_plink_test, struct inode *inode);
18853 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18854 +       struct inode *inode, aufs_bindex_t bindex);
18855 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18856 +          struct dentry *h_dentry);
18857 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18858 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18859 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18860 +#endif /* CONFIG_PROC_FS */
18861 +
18862 +#ifdef CONFIG_AUFS_XATTR
18863 +/* xattr.c */
18864 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
18865 +                 unsigned int verbose);
18866 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18867 +void au_xattr_init(struct super_block *sb);
18868 +#else
18869 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
18870 +          int ignore_flags, unsigned int verbose);
18871 +AuStubVoid(au_xattr_init, struct super_block *sb);
18872 +#endif
18873 +
18874 +#ifdef CONFIG_FS_POSIX_ACL
18875 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18876 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
18877 +#endif
18878 +
18879 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18880 +enum {
18881 +       AU_XATTR_SET,
18882 +       AU_ACL_SET
18883 +};
18884 +
18885 +struct au_sxattr {
18886 +       int type;
18887 +       union {
18888 +               struct {
18889 +                       const char      *name;
18890 +                       const void      *value;
18891 +                       size_t          size;
18892 +                       int             flags;
18893 +               } set;
18894 +               struct {
18895 +                       struct posix_acl *acl;
18896 +                       int             type;
18897 +               } acl_set;
18898 +       } u;
18899 +};
18900 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18901 +                 struct au_sxattr *arg);
18902 +#endif
18903 +
18904 +/* ---------------------------------------------------------------------- */
18905 +
18906 +/* lock subclass for iinfo */
18907 +enum {
18908 +       AuLsc_II_CHILD,         /* child first */
18909 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
18910 +       AuLsc_II_CHILD3,        /* copyup dirs */
18911 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
18912 +       AuLsc_II_PARENT2,
18913 +       AuLsc_II_PARENT3,       /* copyup dirs */
18914 +       AuLsc_II_NEW_CHILD
18915 +};
18916 +
18917 +/*
18918 + * ii_read_lock_child, ii_write_lock_child,
18919 + * ii_read_lock_child2, ii_write_lock_child2,
18920 + * ii_read_lock_child3, ii_write_lock_child3,
18921 + * ii_read_lock_parent, ii_write_lock_parent,
18922 + * ii_read_lock_parent2, ii_write_lock_parent2,
18923 + * ii_read_lock_parent3, ii_write_lock_parent3,
18924 + * ii_read_lock_new_child, ii_write_lock_new_child,
18925 + */
18926 +#define AuReadLockFunc(name, lsc) \
18927 +static inline void ii_read_lock_##name(struct inode *i) \
18928 +{ \
18929 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18930 +}
18931 +
18932 +#define AuWriteLockFunc(name, lsc) \
18933 +static inline void ii_write_lock_##name(struct inode *i) \
18934 +{ \
18935 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18936 +}
18937 +
18938 +#define AuRWLockFuncs(name, lsc) \
18939 +       AuReadLockFunc(name, lsc) \
18940 +       AuWriteLockFunc(name, lsc)
18941 +
18942 +AuRWLockFuncs(child, CHILD);
18943 +AuRWLockFuncs(child2, CHILD2);
18944 +AuRWLockFuncs(child3, CHILD3);
18945 +AuRWLockFuncs(parent, PARENT);
18946 +AuRWLockFuncs(parent2, PARENT2);
18947 +AuRWLockFuncs(parent3, PARENT3);
18948 +AuRWLockFuncs(new_child, NEW_CHILD);
18949 +
18950 +#undef AuReadLockFunc
18951 +#undef AuWriteLockFunc
18952 +#undef AuRWLockFuncs
18953 +
18954 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
18955 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
18956 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
18957 +
18958 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
18959 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
18960 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
18961 +
18962 +/* ---------------------------------------------------------------------- */
18963 +
18964 +static inline void au_icntnr_init(struct au_icntnr *c)
18965 +{
18966 +#ifdef CONFIG_AUFS_DEBUG
18967 +       c->vfs_inode.i_mode = 0;
18968 +#endif
18969 +}
18970 +
18971 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
18972 +{
18973 +       unsigned int gen;
18974 +       struct au_iinfo *iinfo;
18975 +       struct au_iigen *iigen;
18976 +
18977 +       iinfo = au_ii(inode);
18978 +       iigen = &iinfo->ii_generation;
18979 +       spin_lock(&iigen->ig_spin);
18980 +       if (igflags)
18981 +               *igflags = iigen->ig_flags;
18982 +       gen = iigen->ig_generation;
18983 +       spin_unlock(&iigen->ig_spin);
18984 +
18985 +       return gen;
18986 +}
18987 +
18988 +/* tiny test for inode number */
18989 +/* tmpfs generation is too rough */
18990 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
18991 +{
18992 +       struct au_iinfo *iinfo;
18993 +
18994 +       iinfo = au_ii(inode);
18995 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
18996 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
18997 +                && iinfo->ii_higen == h_inode->i_generation);
18998 +}
18999 +
19000 +static inline void au_iigen_dec(struct inode *inode)
19001 +{
19002 +       struct au_iinfo *iinfo;
19003 +       struct au_iigen *iigen;
19004 +
19005 +       iinfo = au_ii(inode);
19006 +       iigen = &iinfo->ii_generation;
19007 +       spin_lock(&iigen->ig_spin);
19008 +       iigen->ig_generation--;
19009 +       spin_unlock(&iigen->ig_spin);
19010 +}
19011 +
19012 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19013 +{
19014 +       int err;
19015 +
19016 +       err = 0;
19017 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19018 +               err = -EIO;
19019 +
19020 +       return err;
19021 +}
19022 +
19023 +/* ---------------------------------------------------------------------- */
19024 +
19025 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19026 +                                         aufs_bindex_t bindex)
19027 +{
19028 +       return iinfo->ii_hinode + bindex;
19029 +}
19030 +
19031 +static inline int au_is_bad_inode(struct inode *inode)
19032 +{
19033 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19034 +}
19035 +
19036 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19037 +                                       aufs_bindex_t bindex)
19038 +{
19039 +       IiMustAnyLock(inode);
19040 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19041 +}
19042 +
19043 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19044 +{
19045 +       IiMustAnyLock(inode);
19046 +       return au_ii(inode)->ii_btop;
19047 +}
19048 +
19049 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19050 +{
19051 +       IiMustAnyLock(inode);
19052 +       return au_ii(inode)->ii_bbot;
19053 +}
19054 +
19055 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19056 +{
19057 +       IiMustAnyLock(inode);
19058 +       return au_ii(inode)->ii_vdir;
19059 +}
19060 +
19061 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19062 +{
19063 +       IiMustAnyLock(inode);
19064 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19065 +}
19066 +
19067 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19068 +{
19069 +       IiMustWriteLock(inode);
19070 +       au_ii(inode)->ii_btop = bindex;
19071 +}
19072 +
19073 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19074 +{
19075 +       IiMustWriteLock(inode);
19076 +       au_ii(inode)->ii_bbot = bindex;
19077 +}
19078 +
19079 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19080 +{
19081 +       IiMustWriteLock(inode);
19082 +       au_ii(inode)->ii_vdir = vdir;
19083 +}
19084 +
19085 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19086 +{
19087 +       IiMustAnyLock(inode);
19088 +       return au_hinode(au_ii(inode), bindex);
19089 +}
19090 +
19091 +/* ---------------------------------------------------------------------- */
19092 +
19093 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19094 +{
19095 +       if (pin)
19096 +               return pin->parent;
19097 +       return NULL;
19098 +}
19099 +
19100 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19101 +{
19102 +       if (pin && pin->hdir)
19103 +               return pin->hdir->hi_inode;
19104 +       return NULL;
19105 +}
19106 +
19107 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19108 +{
19109 +       if (pin)
19110 +               return pin->hdir;
19111 +       return NULL;
19112 +}
19113 +
19114 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19115 +{
19116 +       if (pin)
19117 +               pin->dentry = dentry;
19118 +}
19119 +
19120 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19121 +                                          unsigned char lflag)
19122 +{
19123 +       if (pin) {
19124 +               if (lflag)
19125 +                       au_fset_pin(pin->flags, DI_LOCKED);
19126 +               else
19127 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19128 +       }
19129 +}
19130 +
19131 +#if 0 /* reserved */
19132 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19133 +{
19134 +       if (pin) {
19135 +               dput(pin->parent);
19136 +               pin->parent = dget(parent);
19137 +       }
19138 +}
19139 +#endif
19140 +
19141 +/* ---------------------------------------------------------------------- */
19142 +
19143 +struct au_branch;
19144 +#ifdef CONFIG_AUFS_HNOTIFY
19145 +struct au_hnotify_op {
19146 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19147 +       int (*alloc)(struct au_hinode *hinode);
19148 +
19149 +       /*
19150 +        * if it returns true, the caller should free hinode->hi_notify,
19151 +        * otherwise ->free() frees it.
19152 +        */
19153 +       int (*free)(struct au_hinode *hinode,
19154 +                   struct au_hnotify *hn) __must_check;
19155 +
19156 +       void (*fin)(void);
19157 +       int (*init)(void);
19158 +
19159 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19160 +       void (*fin_br)(struct au_branch *br);
19161 +       int (*init_br)(struct au_branch *br, int perm);
19162 +};
19163 +
19164 +/* hnotify.c */
19165 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19166 +void au_hn_free(struct au_hinode *hinode);
19167 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19168 +void au_hn_reset(struct inode *inode, unsigned int flags);
19169 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19170 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19171 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19172 +int au_hnotify_init_br(struct au_branch *br, int perm);
19173 +void au_hnotify_fin_br(struct au_branch *br);
19174 +int __init au_hnotify_init(void);
19175 +void au_hnotify_fin(void);
19176 +
19177 +/* hfsnotify.c */
19178 +extern const struct au_hnotify_op au_hnotify_op;
19179 +
19180 +static inline
19181 +void au_hn_init(struct au_hinode *hinode)
19182 +{
19183 +       hinode->hi_notify = NULL;
19184 +}
19185 +
19186 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19187 +{
19188 +       return hinode->hi_notify;
19189 +}
19190 +
19191 +#else
19192 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19193 +       struct au_hinode *hinode __maybe_unused,
19194 +       struct inode *inode __maybe_unused)
19195 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19196 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19197 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19198 +          int do_set __maybe_unused)
19199 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19200 +          unsigned int flags __maybe_unused)
19201 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19202 +          struct au_branch *br __maybe_unused,
19203 +          int perm __maybe_unused)
19204 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19205 +          int perm __maybe_unused)
19206 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19207 +AuStubInt0(__init au_hnotify_init, void)
19208 +AuStubVoid(au_hnotify_fin, void)
19209 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19210 +#endif /* CONFIG_AUFS_HNOTIFY */
19211 +
19212 +static inline void au_hn_suspend(struct au_hinode *hdir)
19213 +{
19214 +       au_hn_ctl(hdir, /*do_set*/0);
19215 +}
19216 +
19217 +static inline void au_hn_resume(struct au_hinode *hdir)
19218 +{
19219 +       au_hn_ctl(hdir, /*do_set*/1);
19220 +}
19221 +
19222 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19223 +{
19224 +       inode_lock(hdir->hi_inode);
19225 +       au_hn_suspend(hdir);
19226 +}
19227 +
19228 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19229 +                                         unsigned int sc __maybe_unused)
19230 +{
19231 +       inode_lock_nested(hdir->hi_inode, sc);
19232 +       au_hn_suspend(hdir);
19233 +}
19234 +
19235 +#if 0 /* unused */
19236 +#include "vfsub.h"
19237 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19238 +                                                 unsigned int sc)
19239 +{
19240 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19241 +       au_hn_suspend(hdir);
19242 +}
19243 +#endif
19244 +
19245 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19246 +{
19247 +       au_hn_resume(hdir);
19248 +       inode_unlock(hdir->hi_inode);
19249 +}
19250 +
19251 +#endif /* __KERNEL__ */
19252 +#endif /* __AUFS_INODE_H__ */
19253 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19254 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19255 +++ linux/fs/aufs/ioctl.c       2021-02-24 13:33:42.744347058 +0100
19256 @@ -0,0 +1,220 @@
19257 +// SPDX-License-Identifier: GPL-2.0
19258 +/*
19259 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19260 + *
19261 + * This program, aufs is free software; you can redistribute it and/or modify
19262 + * it under the terms of the GNU General Public License as published by
19263 + * the Free Software Foundation; either version 2 of the License, or
19264 + * (at your option) any later version.
19265 + *
19266 + * This program is distributed in the hope that it will be useful,
19267 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19268 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19269 + * GNU General Public License for more details.
19270 + *
19271 + * You should have received a copy of the GNU General Public License
19272 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19273 + */
19274 +
19275 +/*
19276 + * ioctl
19277 + * plink-management and readdir in userspace.
19278 + * assist the pathconf(3) wrapper library.
19279 + * move-down
19280 + * File-based Hierarchical Storage Management.
19281 + */
19282 +
19283 +#include <linux/compat.h>
19284 +#include <linux/file.h>
19285 +#include "aufs.h"
19286 +
19287 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19288 +{
19289 +       int err, fd;
19290 +       aufs_bindex_t wbi, bindex, bbot;
19291 +       struct file *h_file;
19292 +       struct super_block *sb;
19293 +       struct dentry *root;
19294 +       struct au_branch *br;
19295 +       struct aufs_wbr_fd wbrfd = {
19296 +               .oflags = au_dir_roflags,
19297 +               .brid   = -1
19298 +       };
19299 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19300 +               | O_NOATIME | O_CLOEXEC;
19301 +
19302 +       AuDebugOn(wbrfd.oflags & ~valid);
19303 +
19304 +       if (arg) {
19305 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19306 +               if (unlikely(err)) {
19307 +                       err = -EFAULT;
19308 +                       goto out;
19309 +               }
19310 +
19311 +               err = -EINVAL;
19312 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19313 +               wbrfd.oflags |= au_dir_roflags;
19314 +               AuDbg("0%o\n", wbrfd.oflags);
19315 +               if (unlikely(wbrfd.oflags & ~valid))
19316 +                       goto out;
19317 +       }
19318 +
19319 +       fd = get_unused_fd_flags(0);
19320 +       err = fd;
19321 +       if (unlikely(fd < 0))
19322 +               goto out;
19323 +
19324 +       h_file = ERR_PTR(-EINVAL);
19325 +       wbi = 0;
19326 +       br = NULL;
19327 +       sb = path->dentry->d_sb;
19328 +       root = sb->s_root;
19329 +       aufs_read_lock(root, AuLock_IR);
19330 +       bbot = au_sbbot(sb);
19331 +       if (wbrfd.brid >= 0) {
19332 +               wbi = au_br_index(sb, wbrfd.brid);
19333 +               if (unlikely(wbi < 0 || wbi > bbot))
19334 +                       goto out_unlock;
19335 +       }
19336 +
19337 +       h_file = ERR_PTR(-ENOENT);
19338 +       br = au_sbr(sb, wbi);
19339 +       if (!au_br_writable(br->br_perm)) {
19340 +               if (arg)
19341 +                       goto out_unlock;
19342 +
19343 +               bindex = wbi + 1;
19344 +               wbi = -1;
19345 +               for (; bindex <= bbot; bindex++) {
19346 +                       br = au_sbr(sb, bindex);
19347 +                       if (au_br_writable(br->br_perm)) {
19348 +                               wbi = bindex;
19349 +                               br = au_sbr(sb, wbi);
19350 +                               break;
19351 +                       }
19352 +               }
19353 +       }
19354 +       AuDbg("wbi %d\n", wbi);
19355 +       if (wbi >= 0)
19356 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19357 +                                  /*force_wr*/0);
19358 +
19359 +out_unlock:
19360 +       aufs_read_unlock(root, AuLock_IR);
19361 +       err = PTR_ERR(h_file);
19362 +       if (IS_ERR(h_file))
19363 +               goto out_fd;
19364 +
19365 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19366 +       fd_install(fd, h_file);
19367 +       err = fd;
19368 +       goto out; /* success */
19369 +
19370 +out_fd:
19371 +       put_unused_fd(fd);
19372 +out:
19373 +       AuTraceErr(err);
19374 +       return err;
19375 +}
19376 +
19377 +/* ---------------------------------------------------------------------- */
19378 +
19379 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19380 +{
19381 +       long err;
19382 +       struct dentry *dentry;
19383 +
19384 +       switch (cmd) {
19385 +       case AUFS_CTL_RDU:
19386 +       case AUFS_CTL_RDU_INO:
19387 +               err = au_rdu_ioctl(file, cmd, arg);
19388 +               break;
19389 +
19390 +       case AUFS_CTL_WBR_FD:
19391 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19392 +               break;
19393 +
19394 +       case AUFS_CTL_IBUSY:
19395 +               err = au_ibusy_ioctl(file, arg);
19396 +               break;
19397 +
19398 +       case AUFS_CTL_BRINFO:
19399 +               err = au_brinfo_ioctl(file, arg);
19400 +               break;
19401 +
19402 +       case AUFS_CTL_FHSM_FD:
19403 +               dentry = file->f_path.dentry;
19404 +               if (IS_ROOT(dentry))
19405 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19406 +               else
19407 +                       err = -ENOTTY;
19408 +               break;
19409 +
19410 +       default:
19411 +               /* do not call the lower */
19412 +               AuDbg("0x%x\n", cmd);
19413 +               err = -ENOTTY;
19414 +       }
19415 +
19416 +       AuTraceErr(err);
19417 +       return err;
19418 +}
19419 +
19420 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19421 +{
19422 +       long err;
19423 +
19424 +       switch (cmd) {
19425 +       case AUFS_CTL_MVDOWN:
19426 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19427 +               break;
19428 +
19429 +       case AUFS_CTL_WBR_FD:
19430 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19431 +               break;
19432 +
19433 +       default:
19434 +               /* do not call the lower */
19435 +               AuDbg("0x%x\n", cmd);
19436 +               err = -ENOTTY;
19437 +       }
19438 +
19439 +       AuTraceErr(err);
19440 +       return err;
19441 +}
19442 +
19443 +#ifdef CONFIG_COMPAT
19444 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19445 +                          unsigned long arg)
19446 +{
19447 +       long err;
19448 +
19449 +       switch (cmd) {
19450 +       case AUFS_CTL_RDU:
19451 +       case AUFS_CTL_RDU_INO:
19452 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19453 +               break;
19454 +
19455 +       case AUFS_CTL_IBUSY:
19456 +               err = au_ibusy_compat_ioctl(file, arg);
19457 +               break;
19458 +
19459 +       case AUFS_CTL_BRINFO:
19460 +               err = au_brinfo_compat_ioctl(file, arg);
19461 +               break;
19462 +
19463 +       default:
19464 +               err = aufs_ioctl_dir(file, cmd, arg);
19465 +       }
19466 +
19467 +       AuTraceErr(err);
19468 +       return err;
19469 +}
19470 +
19471 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19472 +                             unsigned long arg)
19473 +{
19474 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19475 +}
19476 +#endif
19477 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19478 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19479 +++ linux/fs/aufs/i_op_add.c    2021-02-24 13:33:42.744347058 +0100
19480 @@ -0,0 +1,936 @@
19481 +// SPDX-License-Identifier: GPL-2.0
19482 +/*
19483 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19484 + *
19485 + * This program, aufs is free software; you can redistribute it and/or modify
19486 + * it under the terms of the GNU General Public License as published by
19487 + * the Free Software Foundation; either version 2 of the License, or
19488 + * (at your option) any later version.
19489 + *
19490 + * This program is distributed in the hope that it will be useful,
19491 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19492 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19493 + * GNU General Public License for more details.
19494 + *
19495 + * You should have received a copy of the GNU General Public License
19496 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19497 + */
19498 +
19499 +/*
19500 + * inode operations (add entry)
19501 + */
19502 +
19503 +#include <linux/iversion.h>
19504 +#include "aufs.h"
19505 +
19506 +/*
19507 + * final procedure of adding a new entry, except link(2).
19508 + * remove whiteout, instantiate, copyup the parent dir's times and size
19509 + * and update version.
19510 + * if it failed, re-create the removed whiteout.
19511 + */
19512 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19513 +                 struct dentry *wh_dentry, struct dentry *dentry)
19514 +{
19515 +       int err, rerr;
19516 +       aufs_bindex_t bwh;
19517 +       struct path h_path;
19518 +       struct super_block *sb;
19519 +       struct inode *inode, *h_dir;
19520 +       struct dentry *wh;
19521 +
19522 +       bwh = -1;
19523 +       sb = dir->i_sb;
19524 +       if (wh_dentry) {
19525 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19526 +               IMustLock(h_dir);
19527 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19528 +               bwh = au_dbwh(dentry);
19529 +               h_path.dentry = wh_dentry;
19530 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19531 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19532 +                                         dentry);
19533 +               if (unlikely(err))
19534 +                       goto out;
19535 +       }
19536 +
19537 +       inode = au_new_inode(dentry, /*must_new*/1);
19538 +       if (!IS_ERR(inode)) {
19539 +               d_instantiate(dentry, inode);
19540 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19541 +               IMustLock(dir);
19542 +               au_dir_ts(dir, bindex);
19543 +               inode_inc_iversion(dir);
19544 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19545 +               return 0; /* success */
19546 +       }
19547 +
19548 +       err = PTR_ERR(inode);
19549 +       if (!wh_dentry)
19550 +               goto out;
19551 +
19552 +       /* revert */
19553 +       /* dir inode is locked */
19554 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19555 +       rerr = PTR_ERR(wh);
19556 +       if (IS_ERR(wh)) {
19557 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19558 +                       dentry, err, rerr);
19559 +               err = -EIO;
19560 +       } else
19561 +               dput(wh);
19562 +
19563 +out:
19564 +       return err;
19565 +}
19566 +
19567 +static int au_d_may_add(struct dentry *dentry)
19568 +{
19569 +       int err;
19570 +
19571 +       err = 0;
19572 +       if (unlikely(d_unhashed(dentry)))
19573 +               err = -ENOENT;
19574 +       if (unlikely(d_really_is_positive(dentry)))
19575 +               err = -EEXIST;
19576 +       return err;
19577 +}
19578 +
19579 +/*
19580 + * simple tests for the adding inode operations.
19581 + * following the checks in vfs, plus the parent-child relationship.
19582 + */
19583 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19584 +              struct dentry *h_parent, int isdir)
19585 +{
19586 +       int err;
19587 +       umode_t h_mode;
19588 +       struct dentry *h_dentry;
19589 +       struct inode *h_inode;
19590 +
19591 +       err = -ENAMETOOLONG;
19592 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19593 +               goto out;
19594 +
19595 +       h_dentry = au_h_dptr(dentry, bindex);
19596 +       if (d_really_is_negative(dentry)) {
19597 +               err = -EEXIST;
19598 +               if (unlikely(d_is_positive(h_dentry)))
19599 +                       goto out;
19600 +       } else {
19601 +               /* rename(2) case */
19602 +               err = -EIO;
19603 +               if (unlikely(d_is_negative(h_dentry)))
19604 +                       goto out;
19605 +               h_inode = d_inode(h_dentry);
19606 +               if (unlikely(!h_inode->i_nlink))
19607 +                       goto out;
19608 +
19609 +               h_mode = h_inode->i_mode;
19610 +               if (!isdir) {
19611 +                       err = -EISDIR;
19612 +                       if (unlikely(S_ISDIR(h_mode)))
19613 +                               goto out;
19614 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19615 +                       err = -ENOTDIR;
19616 +                       goto out;
19617 +               }
19618 +       }
19619 +
19620 +       err = 0;
19621 +       /* expected parent dir is locked */
19622 +       if (unlikely(h_parent != h_dentry->d_parent))
19623 +               err = -EIO;
19624 +
19625 +out:
19626 +       AuTraceErr(err);
19627 +       return err;
19628 +}
19629 +
19630 +/*
19631 + * initial procedure of adding a new entry.
19632 + * prepare writable branch and the parent dir, lock it,
19633 + * and lookup whiteout for the new entry.
19634 + */
19635 +static struct dentry*
19636 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19637 +                 struct dentry *src_dentry, struct au_pin *pin,
19638 +                 struct au_wr_dir_args *wr_dir_args)
19639 +{
19640 +       struct dentry *wh_dentry, *h_parent;
19641 +       struct super_block *sb;
19642 +       struct au_branch *br;
19643 +       int err;
19644 +       unsigned int udba;
19645 +       aufs_bindex_t bcpup;
19646 +
19647 +       AuDbg("%pd\n", dentry);
19648 +
19649 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19650 +       bcpup = err;
19651 +       wh_dentry = ERR_PTR(err);
19652 +       if (unlikely(err < 0))
19653 +               goto out;
19654 +
19655 +       sb = dentry->d_sb;
19656 +       udba = au_opt_udba(sb);
19657 +       err = au_pin(pin, dentry, bcpup, udba,
19658 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19659 +       wh_dentry = ERR_PTR(err);
19660 +       if (unlikely(err))
19661 +               goto out;
19662 +
19663 +       h_parent = au_pinned_h_parent(pin);
19664 +       if (udba != AuOpt_UDBA_NONE
19665 +           && au_dbtop(dentry) == bcpup)
19666 +               err = au_may_add(dentry, bcpup, h_parent,
19667 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19668 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19669 +               err = -ENAMETOOLONG;
19670 +       wh_dentry = ERR_PTR(err);
19671 +       if (unlikely(err))
19672 +               goto out_unpin;
19673 +
19674 +       br = au_sbr(sb, bcpup);
19675 +       if (dt) {
19676 +               struct path tmp = {
19677 +                       .dentry = h_parent,
19678 +                       .mnt    = au_br_mnt(br)
19679 +               };
19680 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19681 +       }
19682 +
19683 +       wh_dentry = NULL;
19684 +       if (bcpup != au_dbwh(dentry))
19685 +               goto out; /* success */
19686 +
19687 +       /*
19688 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19689 +        * would not be able to removed in the future. So we don't allow such
19690 +        * name here and we don't handle ENAMETOOLONG differently here.
19691 +        */
19692 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19693 +
19694 +out_unpin:
19695 +       if (IS_ERR(wh_dentry))
19696 +               au_unpin(pin);
19697 +out:
19698 +       return wh_dentry;
19699 +}
19700 +
19701 +/* ---------------------------------------------------------------------- */
19702 +
19703 +enum { Mknod, Symlink, Creat };
19704 +struct simple_arg {
19705 +       int type;
19706 +       union {
19707 +               struct {
19708 +                       umode_t                 mode;
19709 +                       bool                    want_excl;
19710 +                       bool                    try_aopen;
19711 +                       struct vfsub_aopen_args *aopen;
19712 +               } c;
19713 +               struct {
19714 +                       const char *symname;
19715 +               } s;
19716 +               struct {
19717 +                       umode_t mode;
19718 +                       dev_t dev;
19719 +               } m;
19720 +       } u;
19721 +};
19722 +
19723 +static int add_simple(struct inode *dir, struct dentry *dentry,
19724 +                     struct simple_arg *arg)
19725 +{
19726 +       int err, rerr;
19727 +       aufs_bindex_t btop;
19728 +       unsigned char created;
19729 +       const unsigned char try_aopen
19730 +               = (arg->type == Creat && arg->u.c.try_aopen);
19731 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19732 +       struct dentry *wh_dentry, *parent;
19733 +       struct inode *h_dir;
19734 +       struct super_block *sb;
19735 +       struct au_branch *br;
19736 +       /* to reduce stack size */
19737 +       struct {
19738 +               struct au_dtime dt;
19739 +               struct au_pin pin;
19740 +               struct path h_path;
19741 +               struct au_wr_dir_args wr_dir_args;
19742 +       } *a;
19743 +
19744 +       AuDbg("%pd\n", dentry);
19745 +       IMustLock(dir);
19746 +
19747 +       err = -ENOMEM;
19748 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19749 +       if (unlikely(!a))
19750 +               goto out;
19751 +       a->wr_dir_args.force_btgt = -1;
19752 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19753 +
19754 +       parent = dentry->d_parent; /* dir inode is locked */
19755 +       if (!try_aopen) {
19756 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19757 +               if (unlikely(err))
19758 +                       goto out_free;
19759 +       }
19760 +       err = au_d_may_add(dentry);
19761 +       if (unlikely(err))
19762 +               goto out_unlock;
19763 +       if (!try_aopen)
19764 +               di_write_lock_parent(parent);
19765 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19766 +                                     &a->pin, &a->wr_dir_args);
19767 +       err = PTR_ERR(wh_dentry);
19768 +       if (IS_ERR(wh_dentry))
19769 +               goto out_parent;
19770 +
19771 +       btop = au_dbtop(dentry);
19772 +       sb = dentry->d_sb;
19773 +       br = au_sbr(sb, btop);
19774 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19775 +       a->h_path.mnt = au_br_mnt(br);
19776 +       h_dir = au_pinned_h_dir(&a->pin);
19777 +       switch (arg->type) {
19778 +       case Creat:
19779 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19780 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19781 +                                          arg->u.c.want_excl);
19782 +                       created = !err;
19783 +                       if (!err && try_aopen)
19784 +                               aopen->file->f_mode |= FMODE_CREATED;
19785 +               } else {
19786 +                       aopen->br = br;
19787 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19788 +                       AuDbg("err %d\n", err);
19789 +                       AuDbgFile(aopen->file);
19790 +                       created = err >= 0
19791 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19792 +               }
19793 +               break;
19794 +       case Symlink:
19795 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19796 +               created = !err;
19797 +               break;
19798 +       case Mknod:
19799 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19800 +                                 arg->u.m.dev);
19801 +               created = !err;
19802 +               break;
19803 +       default:
19804 +               BUG();
19805 +       }
19806 +       if (unlikely(err < 0))
19807 +               goto out_unpin;
19808 +
19809 +       err = epilog(dir, btop, wh_dentry, dentry);
19810 +       if (!err)
19811 +               goto out_unpin; /* success */
19812 +
19813 +       /* revert */
19814 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19815 +               /* no delegation since it is just created */
19816 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19817 +                                   /*force*/0);
19818 +               if (rerr) {
19819 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19820 +                               dentry, err, rerr);
19821 +                       err = -EIO;
19822 +               }
19823 +               au_dtime_revert(&a->dt);
19824 +       }
19825 +       if (try_aopen && h_dir->i_op->atomic_open
19826 +           && (aopen->file->f_mode & FMODE_OPENED))
19827 +               /* aopen->file is still opened */
19828 +               au_lcnt_dec(&aopen->br->br_nfiles);
19829 +
19830 +out_unpin:
19831 +       au_unpin(&a->pin);
19832 +       dput(wh_dentry);
19833 +out_parent:
19834 +       if (!try_aopen)
19835 +               di_write_unlock(parent);
19836 +out_unlock:
19837 +       if (unlikely(err)) {
19838 +               au_update_dbtop(dentry);
19839 +               d_drop(dentry);
19840 +       }
19841 +       if (!try_aopen)
19842 +               aufs_read_unlock(dentry, AuLock_DW);
19843 +out_free:
19844 +       au_kfree_rcu(a);
19845 +out:
19846 +       return err;
19847 +}
19848 +
19849 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19850 +              dev_t dev)
19851 +{
19852 +       struct simple_arg arg = {
19853 +               .type = Mknod,
19854 +               .u.m = {
19855 +                       .mode   = mode,
19856 +                       .dev    = dev
19857 +               }
19858 +       };
19859 +       return add_simple(dir, dentry, &arg);
19860 +}
19861 +
19862 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19863 +{
19864 +       struct simple_arg arg = {
19865 +               .type = Symlink,
19866 +               .u.s.symname = symname
19867 +       };
19868 +       return add_simple(dir, dentry, &arg);
19869 +}
19870 +
19871 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
19872 +               bool want_excl)
19873 +{
19874 +       struct simple_arg arg = {
19875 +               .type = Creat,
19876 +               .u.c = {
19877 +                       .mode           = mode,
19878 +                       .want_excl      = want_excl
19879 +               }
19880 +       };
19881 +       return add_simple(dir, dentry, &arg);
19882 +}
19883 +
19884 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19885 +                      struct vfsub_aopen_args *aopen_args)
19886 +{
19887 +       struct simple_arg arg = {
19888 +               .type = Creat,
19889 +               .u.c = {
19890 +                       .mode           = aopen_args->create_mode,
19891 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
19892 +                       .try_aopen      = true,
19893 +                       .aopen          = aopen_args
19894 +               }
19895 +       };
19896 +       return add_simple(dir, dentry, &arg);
19897 +}
19898 +
19899 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
19900 +{
19901 +       int err;
19902 +       aufs_bindex_t bindex;
19903 +       struct super_block *sb;
19904 +       struct dentry *parent, *h_parent, *h_dentry;
19905 +       struct inode *h_dir, *inode;
19906 +       struct vfsmount *h_mnt;
19907 +       struct au_wr_dir_args wr_dir_args = {
19908 +               .force_btgt     = -1,
19909 +               .flags          = AuWrDir_TMPFILE
19910 +       };
19911 +
19912 +       /* copy-up may happen */
19913 +       inode_lock(dir);
19914 +
19915 +       sb = dir->i_sb;
19916 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19917 +       if (unlikely(err))
19918 +               goto out;
19919 +
19920 +       err = au_di_init(dentry);
19921 +       if (unlikely(err))
19922 +               goto out_si;
19923 +
19924 +       err = -EBUSY;
19925 +       parent = d_find_any_alias(dir);
19926 +       AuDebugOn(!parent);
19927 +       di_write_lock_parent(parent);
19928 +       if (unlikely(d_inode(parent) != dir))
19929 +               goto out_parent;
19930 +
19931 +       err = au_digen_test(parent, au_sigen(sb));
19932 +       if (unlikely(err))
19933 +               goto out_parent;
19934 +
19935 +       bindex = au_dbtop(parent);
19936 +       au_set_dbtop(dentry, bindex);
19937 +       au_set_dbbot(dentry, bindex);
19938 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19939 +       bindex = err;
19940 +       if (unlikely(err < 0))
19941 +               goto out_parent;
19942 +
19943 +       err = -EOPNOTSUPP;
19944 +       h_dir = au_h_iptr(dir, bindex);
19945 +       if (unlikely(!h_dir->i_op->tmpfile))
19946 +               goto out_parent;
19947 +
19948 +       h_mnt = au_sbr_mnt(sb, bindex);
19949 +       err = vfsub_mnt_want_write(h_mnt);
19950 +       if (unlikely(err))
19951 +               goto out_parent;
19952 +
19953 +       h_parent = au_h_dptr(parent, bindex);
19954 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
19955 +       if (IS_ERR(h_dentry)) {
19956 +               err = PTR_ERR(h_dentry);
19957 +               goto out_mnt;
19958 +       }
19959 +
19960 +       au_set_dbtop(dentry, bindex);
19961 +       au_set_dbbot(dentry, bindex);
19962 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
19963 +       inode = au_new_inode(dentry, /*must_new*/1);
19964 +       if (IS_ERR(inode)) {
19965 +               err = PTR_ERR(inode);
19966 +               au_set_h_dptr(dentry, bindex, NULL);
19967 +               au_set_dbtop(dentry, -1);
19968 +               au_set_dbbot(dentry, -1);
19969 +       } else {
19970 +               if (!inode->i_nlink)
19971 +                       set_nlink(inode, 1);
19972 +               d_tmpfile(dentry, inode);
19973 +               au_di(dentry)->di_tmpfile = 1;
19974 +
19975 +               /* update without i_mutex */
19976 +               if (au_ibtop(dir) == au_dbtop(dentry))
19977 +                       au_cpup_attr_timesizes(dir);
19978 +       }
19979 +       dput(h_dentry);
19980 +
19981 +out_mnt:
19982 +       vfsub_mnt_drop_write(h_mnt);
19983 +out_parent:
19984 +       di_write_unlock(parent);
19985 +       dput(parent);
19986 +       di_write_unlock(dentry);
19987 +       if (unlikely(err)) {
19988 +               au_di_fin(dentry);
19989 +               dentry->d_fsdata = NULL;
19990 +       }
19991 +out_si:
19992 +       si_read_unlock(sb);
19993 +out:
19994 +       inode_unlock(dir);
19995 +       return err;
19996 +}
19997 +
19998 +/* ---------------------------------------------------------------------- */
19999 +
20000 +struct au_link_args {
20001 +       aufs_bindex_t bdst, bsrc;
20002 +       struct au_pin pin;
20003 +       struct path h_path;
20004 +       struct dentry *src_parent, *parent;
20005 +};
20006 +
20007 +static int au_cpup_before_link(struct dentry *src_dentry,
20008 +                              struct au_link_args *a)
20009 +{
20010 +       int err;
20011 +       struct dentry *h_src_dentry;
20012 +       struct au_cp_generic cpg = {
20013 +               .dentry = src_dentry,
20014 +               .bdst   = a->bdst,
20015 +               .bsrc   = a->bsrc,
20016 +               .len    = -1,
20017 +               .pin    = &a->pin,
20018 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20019 +       };
20020 +
20021 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20022 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20023 +       if (unlikely(err))
20024 +               goto out;
20025 +
20026 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20027 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20028 +                    au_opt_udba(src_dentry->d_sb),
20029 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20030 +       if (unlikely(err))
20031 +               goto out;
20032 +
20033 +       err = au_sio_cpup_simple(&cpg);
20034 +       au_unpin(&a->pin);
20035 +
20036 +out:
20037 +       di_read_unlock(a->src_parent, AuLock_IR);
20038 +       return err;
20039 +}
20040 +
20041 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20042 +                          struct au_link_args *a)
20043 +{
20044 +       int err;
20045 +       unsigned char plink;
20046 +       aufs_bindex_t bbot;
20047 +       struct dentry *h_src_dentry;
20048 +       struct inode *h_inode, *inode, *delegated;
20049 +       struct super_block *sb;
20050 +       struct file *h_file;
20051 +
20052 +       plink = 0;
20053 +       h_inode = NULL;
20054 +       sb = src_dentry->d_sb;
20055 +       inode = d_inode(src_dentry);
20056 +       if (au_ibtop(inode) <= a->bdst)
20057 +               h_inode = au_h_iptr(inode, a->bdst);
20058 +       if (!h_inode || !h_inode->i_nlink) {
20059 +               /* copyup src_dentry as the name of dentry. */
20060 +               bbot = au_dbbot(dentry);
20061 +               if (bbot < a->bsrc)
20062 +                       au_set_dbbot(dentry, a->bsrc);
20063 +               au_set_h_dptr(dentry, a->bsrc,
20064 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20065 +               dget(a->h_path.dentry);
20066 +               au_set_h_dptr(dentry, a->bdst, NULL);
20067 +               AuDbg("temporary d_inode...\n");
20068 +               spin_lock(&dentry->d_lock);
20069 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20070 +               spin_unlock(&dentry->d_lock);
20071 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20072 +               if (IS_ERR(h_file))
20073 +                       err = PTR_ERR(h_file);
20074 +               else {
20075 +                       struct au_cp_generic cpg = {
20076 +                               .dentry = dentry,
20077 +                               .bdst   = a->bdst,
20078 +                               .bsrc   = -1,
20079 +                               .len    = -1,
20080 +                               .pin    = &a->pin,
20081 +                               .flags  = AuCpup_KEEPLINO
20082 +                       };
20083 +                       err = au_sio_cpup_simple(&cpg);
20084 +                       au_h_open_post(dentry, a->bsrc, h_file);
20085 +                       if (!err) {
20086 +                               dput(a->h_path.dentry);
20087 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20088 +                       } else
20089 +                               au_set_h_dptr(dentry, a->bdst,
20090 +                                             a->h_path.dentry);
20091 +               }
20092 +               spin_lock(&dentry->d_lock);
20093 +               dentry->d_inode = NULL; /* restore */
20094 +               spin_unlock(&dentry->d_lock);
20095 +               AuDbg("temporary d_inode...done\n");
20096 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20097 +               au_set_dbbot(dentry, bbot);
20098 +       } else {
20099 +               /* the inode of src_dentry already exists on a.bdst branch */
20100 +               h_src_dentry = d_find_alias(h_inode);
20101 +               if (!h_src_dentry && au_plink_test(inode)) {
20102 +                       plink = 1;
20103 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20104 +                       err = PTR_ERR(h_src_dentry);
20105 +                       if (IS_ERR(h_src_dentry))
20106 +                               goto out;
20107 +
20108 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20109 +                               dput(h_src_dentry);
20110 +                               h_src_dentry = NULL;
20111 +                       }
20112 +
20113 +               }
20114 +               if (h_src_dentry) {
20115 +                       delegated = NULL;
20116 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20117 +                                        &a->h_path, &delegated);
20118 +                       if (unlikely(err == -EWOULDBLOCK)) {
20119 +                               pr_warn("cannot retry for NFSv4 delegation"
20120 +                                       " for an internal link\n");
20121 +                               iput(delegated);
20122 +                       }
20123 +                       dput(h_src_dentry);
20124 +               } else {
20125 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20126 +                               h_inode->i_ino, a->bdst);
20127 +                       err = -EIO;
20128 +               }
20129 +       }
20130 +
20131 +       if (!err && !plink)
20132 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20133 +
20134 +out:
20135 +       AuTraceErr(err);
20136 +       return err;
20137 +}
20138 +
20139 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20140 +             struct dentry *dentry)
20141 +{
20142 +       int err, rerr;
20143 +       struct au_dtime dt;
20144 +       struct au_link_args *a;
20145 +       struct dentry *wh_dentry, *h_src_dentry;
20146 +       struct inode *inode, *delegated;
20147 +       struct super_block *sb;
20148 +       struct au_wr_dir_args wr_dir_args = {
20149 +               /* .force_btgt  = -1, */
20150 +               .flags          = AuWrDir_ADD_ENTRY
20151 +       };
20152 +
20153 +       IMustLock(dir);
20154 +       inode = d_inode(src_dentry);
20155 +       IMustLock(inode);
20156 +
20157 +       err = -ENOMEM;
20158 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20159 +       if (unlikely(!a))
20160 +               goto out;
20161 +
20162 +       a->parent = dentry->d_parent; /* dir inode is locked */
20163 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20164 +                                       AuLock_NOPLM | AuLock_GEN);
20165 +       if (unlikely(err))
20166 +               goto out_kfree;
20167 +       err = au_d_linkable(src_dentry);
20168 +       if (unlikely(err))
20169 +               goto out_unlock;
20170 +       err = au_d_may_add(dentry);
20171 +       if (unlikely(err))
20172 +               goto out_unlock;
20173 +
20174 +       a->src_parent = dget_parent(src_dentry);
20175 +       wr_dir_args.force_btgt = au_ibtop(inode);
20176 +
20177 +       di_write_lock_parent(a->parent);
20178 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20179 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20180 +                                     &wr_dir_args);
20181 +       err = PTR_ERR(wh_dentry);
20182 +       if (IS_ERR(wh_dentry))
20183 +               goto out_parent;
20184 +
20185 +       err = 0;
20186 +       sb = dentry->d_sb;
20187 +       a->bdst = au_dbtop(dentry);
20188 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20189 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20190 +       a->bsrc = au_ibtop(inode);
20191 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20192 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20193 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20194 +       if (!h_src_dentry) {
20195 +               a->bsrc = au_dbtop(src_dentry);
20196 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20197 +               AuDebugOn(!h_src_dentry);
20198 +       } else if (IS_ERR(h_src_dentry)) {
20199 +               err = PTR_ERR(h_src_dentry);
20200 +               goto out_parent;
20201 +       }
20202 +
20203 +       /*
20204 +        * aufs doesn't touch the credential so
20205 +        * security_dentry_create_files_as() is unnecessary.
20206 +        */
20207 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20208 +               if (a->bdst < a->bsrc
20209 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20210 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20211 +               else {
20212 +                       delegated = NULL;
20213 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20214 +                                        &a->h_path, &delegated);
20215 +                       if (unlikely(err == -EWOULDBLOCK)) {
20216 +                               pr_warn("cannot retry for NFSv4 delegation"
20217 +                                       " for an internal link\n");
20218 +                               iput(delegated);
20219 +                       }
20220 +               }
20221 +               dput(h_src_dentry);
20222 +       } else {
20223 +               /*
20224 +                * copyup src_dentry to the branch we process,
20225 +                * and then link(2) to it.
20226 +                */
20227 +               dput(h_src_dentry);
20228 +               if (a->bdst < a->bsrc
20229 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20230 +                       au_unpin(&a->pin);
20231 +                       di_write_unlock(a->parent);
20232 +                       err = au_cpup_before_link(src_dentry, a);
20233 +                       di_write_lock_parent(a->parent);
20234 +                       if (!err)
20235 +                               err = au_pin(&a->pin, dentry, a->bdst,
20236 +                                            au_opt_udba(sb),
20237 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20238 +                       if (unlikely(err))
20239 +                               goto out_wh;
20240 +               }
20241 +               if (!err) {
20242 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20243 +                       err = -ENOENT;
20244 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20245 +                               delegated = NULL;
20246 +                               err = vfsub_link(h_src_dentry,
20247 +                                                au_pinned_h_dir(&a->pin),
20248 +                                                &a->h_path, &delegated);
20249 +                               if (unlikely(err == -EWOULDBLOCK)) {
20250 +                                       pr_warn("cannot retry"
20251 +                                               " for NFSv4 delegation"
20252 +                                               " for an internal link\n");
20253 +                                       iput(delegated);
20254 +                               }
20255 +                       }
20256 +               }
20257 +       }
20258 +       if (unlikely(err))
20259 +               goto out_unpin;
20260 +
20261 +       if (wh_dentry) {
20262 +               a->h_path.dentry = wh_dentry;
20263 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20264 +                                         dentry);
20265 +               if (unlikely(err))
20266 +                       goto out_revert;
20267 +       }
20268 +
20269 +       au_dir_ts(dir, a->bdst);
20270 +       inode_inc_iversion(dir);
20271 +       inc_nlink(inode);
20272 +       inode->i_ctime = dir->i_ctime;
20273 +       d_instantiate(dentry, au_igrab(inode));
20274 +       if (d_unhashed(a->h_path.dentry))
20275 +               /* some filesystem calls d_drop() */
20276 +               d_drop(dentry);
20277 +       /* some filesystems consume an inode even hardlink */
20278 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20279 +       goto out_unpin; /* success */
20280 +
20281 +out_revert:
20282 +       /* no delegation since it is just created */
20283 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20284 +                           /*delegated*/NULL, /*force*/0);
20285 +       if (unlikely(rerr)) {
20286 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20287 +               err = -EIO;
20288 +       }
20289 +       au_dtime_revert(&dt);
20290 +out_unpin:
20291 +       au_unpin(&a->pin);
20292 +out_wh:
20293 +       dput(wh_dentry);
20294 +out_parent:
20295 +       di_write_unlock(a->parent);
20296 +       dput(a->src_parent);
20297 +out_unlock:
20298 +       if (unlikely(err)) {
20299 +               au_update_dbtop(dentry);
20300 +               d_drop(dentry);
20301 +       }
20302 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20303 +out_kfree:
20304 +       au_kfree_rcu(a);
20305 +out:
20306 +       AuTraceErr(err);
20307 +       return err;
20308 +}
20309 +
20310 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20311 +{
20312 +       int err, rerr;
20313 +       aufs_bindex_t bindex;
20314 +       unsigned char diropq;
20315 +       struct path h_path;
20316 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20317 +       struct inode *h_inode;
20318 +       struct super_block *sb;
20319 +       struct {
20320 +               struct au_pin pin;
20321 +               struct au_dtime dt;
20322 +       } *a; /* reduce the stack usage */
20323 +       struct au_wr_dir_args wr_dir_args = {
20324 +               .force_btgt     = -1,
20325 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20326 +       };
20327 +
20328 +       IMustLock(dir);
20329 +
20330 +       err = -ENOMEM;
20331 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20332 +       if (unlikely(!a))
20333 +               goto out;
20334 +
20335 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20336 +       if (unlikely(err))
20337 +               goto out_free;
20338 +       err = au_d_may_add(dentry);
20339 +       if (unlikely(err))
20340 +               goto out_unlock;
20341 +
20342 +       parent = dentry->d_parent; /* dir inode is locked */
20343 +       di_write_lock_parent(parent);
20344 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20345 +                                     &a->pin, &wr_dir_args);
20346 +       err = PTR_ERR(wh_dentry);
20347 +       if (IS_ERR(wh_dentry))
20348 +               goto out_parent;
20349 +
20350 +       sb = dentry->d_sb;
20351 +       bindex = au_dbtop(dentry);
20352 +       h_path.dentry = au_h_dptr(dentry, bindex);
20353 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20354 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20355 +       if (unlikely(err))
20356 +               goto out_unpin;
20357 +
20358 +       /* make the dir opaque */
20359 +       diropq = 0;
20360 +       h_inode = d_inode(h_path.dentry);
20361 +       if (wh_dentry
20362 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20363 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20364 +               opq_dentry = au_diropq_create(dentry, bindex);
20365 +               inode_unlock(h_inode);
20366 +               err = PTR_ERR(opq_dentry);
20367 +               if (IS_ERR(opq_dentry))
20368 +                       goto out_dir;
20369 +               dput(opq_dentry);
20370 +               diropq = 1;
20371 +       }
20372 +
20373 +       err = epilog(dir, bindex, wh_dentry, dentry);
20374 +       if (!err) {
20375 +               inc_nlink(dir);
20376 +               goto out_unpin; /* success */
20377 +       }
20378 +
20379 +       /* revert */
20380 +       if (diropq) {
20381 +               AuLabel(revert opq);
20382 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20383 +               rerr = au_diropq_remove(dentry, bindex);
20384 +               inode_unlock(h_inode);
20385 +               if (rerr) {
20386 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20387 +                               dentry, err, rerr);
20388 +                       err = -EIO;
20389 +               }
20390 +       }
20391 +
20392 +out_dir:
20393 +       AuLabel(revert dir);
20394 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20395 +       if (rerr) {
20396 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20397 +                       dentry, err, rerr);
20398 +               err = -EIO;
20399 +       }
20400 +       au_dtime_revert(&a->dt);
20401 +out_unpin:
20402 +       au_unpin(&a->pin);
20403 +       dput(wh_dentry);
20404 +out_parent:
20405 +       di_write_unlock(parent);
20406 +out_unlock:
20407 +       if (unlikely(err)) {
20408 +               au_update_dbtop(dentry);
20409 +               d_drop(dentry);
20410 +       }
20411 +       aufs_read_unlock(dentry, AuLock_DW);
20412 +out_free:
20413 +       au_kfree_rcu(a);
20414 +out:
20415 +       return err;
20416 +}
20417 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20418 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20419 +++ linux/fs/aufs/i_op.c        2021-02-24 13:33:42.744347058 +0100
20420 @@ -0,0 +1,1502 @@
20421 +// SPDX-License-Identifier: GPL-2.0
20422 +/*
20423 + * Copyright (C) 2005-2020 Junjiro R. Okajima
20424 + *
20425 + * This program, aufs is free software; you can redistribute it and/or modify
20426 + * it under the terms of the GNU General Public License as published by
20427 + * the Free Software Foundation; either version 2 of the License, or
20428 + * (at your option) any later version.
20429 + *
20430 + * This program is distributed in the hope that it will be useful,
20431 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20432 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20433 + * GNU General Public License for more details.
20434 + *
20435 + * You should have received a copy of the GNU General Public License
20436 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20437 + */
20438 +
20439 +/*
20440 + * inode operations (except add/del/rename)
20441 + */
20442 +
20443 +#include <linux/device_cgroup.h>
20444 +#include <linux/fs_stack.h>
20445 +#include <linux/iversion.h>
20446 +#include <linux/namei.h>
20447 +#include <linux/security.h>
20448 +#include "aufs.h"
20449 +
20450 +static int h_permission(struct inode *h_inode, int mask,
20451 +                       struct path *h_path, int brperm)
20452 +{
20453 +       int err;
20454 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20455 +
20456 +       err = -EPERM;
20457 +       if (write_mask && IS_IMMUTABLE(h_inode))
20458 +               goto out;
20459 +
20460 +       err = -EACCES;
20461 +       if (((mask & MAY_EXEC)
20462 +            && S_ISREG(h_inode->i_mode)
20463 +            && (path_noexec(h_path)
20464 +                || !(h_inode->i_mode & 0111))))
20465 +               goto out;
20466 +
20467 +       /*
20468 +        * - skip the lower fs test in the case of write to ro branch.
20469 +        * - nfs dir permission write check is optimized, but a policy for
20470 +        *   link/rename requires a real check.
20471 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20472 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20473 +        */
20474 +       if ((write_mask && !au_br_writable(brperm))
20475 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20476 +               && write_mask && !(mask & MAY_READ))
20477 +           || !h_inode->i_op->permission) {
20478 +               /* AuLabel(generic_permission); */
20479 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20480 +               err = generic_permission(h_inode, mask);
20481 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20482 +                       err = h_inode->i_op->permission(h_inode, mask);
20483 +               AuTraceErr(err);
20484 +       } else {
20485 +               /* AuLabel(h_inode->permission); */
20486 +               err = h_inode->i_op->permission(h_inode, mask);
20487 +               AuTraceErr(err);
20488 +       }
20489 +
20490 +       if (!err)
20491 +               err = devcgroup_inode_permission(h_inode, mask);
20492 +       if (!err)
20493 +               err = security_inode_permission(h_inode, mask);
20494 +
20495 +out:
20496 +       return err;
20497 +}
20498 +
20499 +static int aufs_permission(struct inode *inode, int mask)
20500 +{
20501 +       int err;
20502 +       aufs_bindex_t bindex, bbot;
20503 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20504 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20505 +       struct inode *h_inode;
20506 +       struct super_block *sb;
20507 +       struct au_branch *br;
20508 +
20509 +       /* todo: support rcu-walk? */
20510 +       if (mask & MAY_NOT_BLOCK)
20511 +               return -ECHILD;
20512 +
20513 +       sb = inode->i_sb;
20514 +       si_read_lock(sb, AuLock_FLUSH);
20515 +       ii_read_lock_child(inode);
20516 +#if 0 /* reserved for future use */
20517 +       /*
20518 +        * This test may be rather 'too much' since the test is essentially done
20519 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20520 +        * generation doesn't match to the superblock's here.  But it isn't a
20521 +        * big deal I suppose.
20522 +        */
20523 +       err = au_iigen_test(inode, au_sigen(sb));
20524 +       if (unlikely(err))
20525 +               goto out;
20526 +#endif
20527 +
20528 +       if (!isdir
20529 +           || write_mask
20530 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20531 +               err = au_busy_or_stale();
20532 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20533 +               if (unlikely(!h_inode
20534 +                            || (h_inode->i_mode & S_IFMT)
20535 +                            != (inode->i_mode & S_IFMT)))
20536 +                       goto out;
20537 +
20538 +               err = 0;
20539 +               bindex = au_ibtop(inode);
20540 +               br = au_sbr(sb, bindex);
20541 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20542 +               if (write_mask
20543 +                   && !err
20544 +                   && !special_file(h_inode->i_mode)) {
20545 +                       /* test whether the upper writable branch exists */
20546 +                       err = -EROFS;
20547 +                       for (; bindex >= 0; bindex--)
20548 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20549 +                                       err = 0;
20550 +                                       break;
20551 +                               }
20552 +               }
20553 +               goto out;
20554 +       }
20555 +
20556 +       /* non-write to dir */
20557 +       err = 0;
20558 +       bbot = au_ibbot(inode);
20559 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20560 +               h_inode = au_h_iptr(inode, bindex);
20561 +               if (h_inode) {
20562 +                       err = au_busy_or_stale();
20563 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20564 +                               break;
20565 +
20566 +                       br = au_sbr(sb, bindex);
20567 +                       err = h_permission(h_inode, mask, &br->br_path,
20568 +                                          br->br_perm);
20569 +               }
20570 +       }
20571 +
20572 +out:
20573 +       ii_read_unlock(inode);
20574 +       si_read_unlock(sb);
20575 +       return err;
20576 +}
20577 +
20578 +/* ---------------------------------------------------------------------- */
20579 +
20580 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20581 +                                 unsigned int flags)
20582 +{
20583 +       struct dentry *ret, *parent;
20584 +       struct inode *inode;
20585 +       struct super_block *sb;
20586 +       int err, npositive;
20587 +
20588 +       IMustLock(dir);
20589 +
20590 +       /* todo: support rcu-walk? */
20591 +       ret = ERR_PTR(-ECHILD);
20592 +       if (flags & LOOKUP_RCU)
20593 +               goto out;
20594 +
20595 +       ret = ERR_PTR(-ENAMETOOLONG);
20596 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20597 +               goto out;
20598 +
20599 +       sb = dir->i_sb;
20600 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20601 +       ret = ERR_PTR(err);
20602 +       if (unlikely(err))
20603 +               goto out;
20604 +
20605 +       err = au_di_init(dentry);
20606 +       ret = ERR_PTR(err);
20607 +       if (unlikely(err))
20608 +               goto out_si;
20609 +
20610 +       inode = NULL;
20611 +       npositive = 0; /* suppress a warning */
20612 +       parent = dentry->d_parent; /* dir inode is locked */
20613 +       di_read_lock_parent(parent, AuLock_IR);
20614 +       err = au_alive_dir(parent);
20615 +       if (!err)
20616 +               err = au_digen_test(parent, au_sigen(sb));
20617 +       if (!err) {
20618 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20619 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20620 +                                          AuLkup_ALLOW_NEG);
20621 +               err = npositive;
20622 +       }
20623 +       di_read_unlock(parent, AuLock_IR);
20624 +       ret = ERR_PTR(err);
20625 +       if (unlikely(err < 0))
20626 +               goto out_unlock;
20627 +
20628 +       if (npositive) {
20629 +               inode = au_new_inode(dentry, /*must_new*/0);
20630 +               if (IS_ERR(inode)) {
20631 +                       ret = (void *)inode;
20632 +                       inode = NULL;
20633 +                       goto out_unlock;
20634 +               }
20635 +       }
20636 +
20637 +       if (inode)
20638 +               atomic_inc(&inode->i_count);
20639 +       ret = d_splice_alias(inode, dentry);
20640 +#if 0 /* reserved for future use */
20641 +       if (unlikely(d_need_lookup(dentry))) {
20642 +               spin_lock(&dentry->d_lock);
20643 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20644 +               spin_unlock(&dentry->d_lock);
20645 +       } else
20646 +#endif
20647 +       if (inode) {
20648 +               if (!IS_ERR(ret)) {
20649 +                       iput(inode);
20650 +                       if (ret && ret != dentry)
20651 +                               ii_write_unlock(inode);
20652 +               } else {
20653 +                       ii_write_unlock(inode);
20654 +                       iput(inode);
20655 +                       inode = NULL;
20656 +               }
20657 +       }
20658 +
20659 +out_unlock:
20660 +       di_write_unlock(dentry);
20661 +out_si:
20662 +       si_read_unlock(sb);
20663 +out:
20664 +       return ret;
20665 +}
20666 +
20667 +/* ---------------------------------------------------------------------- */
20668 +
20669 +/*
20670 + * very dirty and complicated aufs ->atomic_open().
20671 + * aufs_atomic_open()
20672 + * + au_aopen_or_create()
20673 + *   + add_simple()
20674 + *     + vfsub_atomic_open()
20675 + *       + branch fs ->atomic_open()
20676 + *        may call the actual 'open' for h_file
20677 + *       + inc br_nfiles only if opened
20678 + * + au_aopen_no_open() or au_aopen_do_open()
20679 + *
20680 + * au_aopen_do_open()
20681 + * + finish_open()
20682 + *   + au_do_aopen()
20683 + *     + au_do_open() the body of all 'open'
20684 + *       + au_do_open_nondir()
20685 + *        set the passed h_file
20686 + *
20687 + * au_aopen_no_open()
20688 + * + finish_no_open()
20689 + */
20690 +
20691 +struct aopen_node {
20692 +       struct hlist_bl_node hblist;
20693 +       struct file *file, *h_file;
20694 +};
20695 +
20696 +static int au_do_aopen(struct inode *inode, struct file *file)
20697 +{
20698 +       struct hlist_bl_head *aopen;
20699 +       struct hlist_bl_node *pos;
20700 +       struct aopen_node *node;
20701 +       struct au_do_open_args args = {
20702 +               .aopen  = 1,
20703 +               .open   = au_do_open_nondir
20704 +       };
20705 +
20706 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20707 +       hlist_bl_lock(aopen);
20708 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20709 +               if (node->file == file) {
20710 +                       args.h_file = node->h_file;
20711 +                       break;
20712 +               }
20713 +       hlist_bl_unlock(aopen);
20714 +       /* AuDebugOn(!args.h_file); */
20715 +
20716 +       return au_do_open(file, &args);
20717 +}
20718 +
20719 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20720 +                           struct aopen_node *aopen_node)
20721 +{
20722 +       int err;
20723 +       struct hlist_bl_head *aopen;
20724 +
20725 +       AuLabel(here);
20726 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20727 +       au_hbl_add(&aopen_node->hblist, aopen);
20728 +       err = finish_open(file, dentry, au_do_aopen);
20729 +       au_hbl_del(&aopen_node->hblist, aopen);
20730 +       /* AuDbgFile(file); */
20731 +       AuDbg("%pd%s%s\n", dentry,
20732 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20733 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20734 +
20735 +       AuTraceErr(err);
20736 +       return err;
20737 +}
20738 +
20739 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20740 +{
20741 +       int err;
20742 +
20743 +       AuLabel(here);
20744 +       dget(dentry);
20745 +       err = finish_no_open(file, dentry);
20746 +
20747 +       AuTraceErr(err);
20748 +       return err;
20749 +}
20750 +
20751 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20752 +                           struct file *file, unsigned int open_flag,
20753 +                           umode_t create_mode)
20754 +{
20755 +       int err, did_open;
20756 +       unsigned int lkup_flags;
20757 +       aufs_bindex_t bindex;
20758 +       struct super_block *sb;
20759 +       struct dentry *parent, *d;
20760 +       struct vfsub_aopen_args args = {
20761 +               .open_flag      = open_flag,
20762 +               .create_mode    = create_mode
20763 +       };
20764 +       struct aopen_node aopen_node = {
20765 +               .file   = file
20766 +       };
20767 +
20768 +       IMustLock(dir);
20769 +       AuDbg("open_flag 0%o\n", open_flag);
20770 +       AuDbgDentry(dentry);
20771 +
20772 +       err = 0;
20773 +       if (!au_di(dentry)) {
20774 +               lkup_flags = LOOKUP_OPEN;
20775 +               if (open_flag & O_CREAT)
20776 +                       lkup_flags |= LOOKUP_CREATE;
20777 +               d = aufs_lookup(dir, dentry, lkup_flags);
20778 +               if (IS_ERR(d)) {
20779 +                       err = PTR_ERR(d);
20780 +                       AuTraceErr(err);
20781 +                       goto out;
20782 +               } else if (d) {
20783 +                       /*
20784 +                        * obsoleted dentry found.
20785 +                        * another error will be returned later.
20786 +                        */
20787 +                       d_drop(d);
20788 +                       AuDbgDentry(d);
20789 +                       dput(d);
20790 +               }
20791 +               AuDbgDentry(dentry);
20792 +       }
20793 +
20794 +       if (d_is_positive(dentry)
20795 +           || d_unhashed(dentry)
20796 +           || d_unlinked(dentry)
20797 +           || !(open_flag & O_CREAT)) {
20798 +               err = au_aopen_no_open(file, dentry);
20799 +               goto out; /* success */
20800 +       }
20801 +
20802 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20803 +       if (unlikely(err))
20804 +               goto out;
20805 +
20806 +       sb = dentry->d_sb;
20807 +       parent = dentry->d_parent;      /* dir is locked */
20808 +       di_write_lock_parent(parent);
20809 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20810 +       if (unlikely(err < 0))
20811 +               goto out_parent;
20812 +
20813 +       AuDbgDentry(dentry);
20814 +       if (d_is_positive(dentry)) {
20815 +               err = au_aopen_no_open(file, dentry);
20816 +               goto out_parent; /* success */
20817 +       }
20818 +
20819 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20820 +       err = PTR_ERR(args.file);
20821 +       if (IS_ERR(args.file))
20822 +               goto out_parent;
20823 +
20824 +       bindex = au_dbtop(dentry);
20825 +       err = au_aopen_or_create(dir, dentry, &args);
20826 +       AuTraceErr(err);
20827 +       AuDbgFile(args.file);
20828 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20829 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20830 +       if (!did_open) {
20831 +               fput(args.file);
20832 +               args.file = NULL;
20833 +       }
20834 +       di_write_unlock(parent);
20835 +       di_write_unlock(dentry);
20836 +       if (unlikely(err < 0)) {
20837 +               if (args.file)
20838 +                       fput(args.file);
20839 +               goto out_sb;
20840 +       }
20841 +
20842 +       if (!did_open)
20843 +               err = au_aopen_no_open(file, dentry);
20844 +       else {
20845 +               aopen_node.h_file = args.file;
20846 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20847 +       }
20848 +       if (unlikely(err < 0)) {
20849 +               if (args.file)
20850 +                       fput(args.file);
20851 +               if (did_open)
20852 +                       au_lcnt_dec(&args.br->br_nfiles);
20853 +       }
20854 +       goto out_sb; /* success */
20855 +
20856 +out_parent:
20857 +       di_write_unlock(parent);
20858 +       di_write_unlock(dentry);
20859 +out_sb:
20860 +       si_read_unlock(sb);
20861 +out:
20862 +       AuTraceErr(err);
20863 +       AuDbgFile(file);
20864 +       return err;
20865 +}
20866 +
20867 +
20868 +/* ---------------------------------------------------------------------- */
20869 +
20870 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20871 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
20872 +                         aufs_bindex_t btop)
20873 +{
20874 +       int err;
20875 +       struct dentry *h_parent;
20876 +       struct inode *h_dir;
20877 +
20878 +       if (add_entry)
20879 +               IMustLock(d_inode(parent));
20880 +       else
20881 +               di_write_lock_parent(parent);
20882 +
20883 +       err = 0;
20884 +       if (!au_h_dptr(parent, bcpup)) {
20885 +               if (btop > bcpup)
20886 +                       err = au_cpup_dirs(dentry, bcpup);
20887 +               else if (btop < bcpup)
20888 +                       err = au_cpdown_dirs(dentry, bcpup);
20889 +               else
20890 +                       BUG();
20891 +       }
20892 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20893 +               h_parent = au_h_dptr(parent, bcpup);
20894 +               h_dir = d_inode(h_parent);
20895 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20896 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20897 +               /* todo: no unlock here */
20898 +               inode_unlock_shared(h_dir);
20899 +
20900 +               AuDbg("bcpup %d\n", bcpup);
20901 +               if (!err) {
20902 +                       if (d_really_is_negative(dentry))
20903 +                               au_set_h_dptr(dentry, btop, NULL);
20904 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
20905 +               }
20906 +       }
20907 +
20908 +       if (!add_entry)
20909 +               di_write_unlock(parent);
20910 +       if (!err)
20911 +               err = bcpup; /* success */
20912 +
20913 +       AuTraceErr(err);
20914 +       return err;
20915 +}
20916 +
20917 +/*
20918 + * decide the branch and the parent dir where we will create a new entry.
20919 + * returns new bindex or an error.
20920 + * copyup the parent dir if needed.
20921 + */
20922 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20923 +             struct au_wr_dir_args *args)
20924 +{
20925 +       int err;
20926 +       unsigned int flags;
20927 +       aufs_bindex_t bcpup, btop, src_btop;
20928 +       const unsigned char add_entry
20929 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
20930 +               | au_ftest_wrdir(args->flags, TMPFILE);
20931 +       struct super_block *sb;
20932 +       struct dentry *parent;
20933 +       struct au_sbinfo *sbinfo;
20934 +
20935 +       sb = dentry->d_sb;
20936 +       sbinfo = au_sbi(sb);
20937 +       parent = dget_parent(dentry);
20938 +       btop = au_dbtop(dentry);
20939 +       bcpup = btop;
20940 +       if (args->force_btgt < 0) {
20941 +               if (src_dentry) {
20942 +                       src_btop = au_dbtop(src_dentry);
20943 +                       if (src_btop < btop)
20944 +                               bcpup = src_btop;
20945 +               } else if (add_entry) {
20946 +                       flags = 0;
20947 +                       if (au_ftest_wrdir(args->flags, ISDIR))
20948 +                               au_fset_wbr(flags, DIR);
20949 +                       err = AuWbrCreate(sbinfo, dentry, flags);
20950 +                       bcpup = err;
20951 +               }
20952 +
20953 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
20954 +                       if (add_entry)
20955 +                               err = AuWbrCopyup(sbinfo, dentry);
20956 +                       else {
20957 +                               if (!IS_ROOT(dentry)) {
20958 +                                       di_read_lock_parent(parent, !AuLock_IR);
20959 +                                       err = AuWbrCopyup(sbinfo, dentry);
20960 +                                       di_read_unlock(parent, !AuLock_IR);
20961 +                               } else
20962 +                                       err = AuWbrCopyup(sbinfo, dentry);
20963 +                       }
20964 +                       bcpup = err;
20965 +                       if (unlikely(err < 0))
20966 +                               goto out;
20967 +               }
20968 +       } else {
20969 +               bcpup = args->force_btgt;
20970 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
20971 +       }
20972 +
20973 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
20974 +       err = bcpup;
20975 +       if (bcpup == btop)
20976 +               goto out; /* success */
20977 +
20978 +       /* copyup the new parent into the branch we process */
20979 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
20980 +       if (err >= 0) {
20981 +               if (d_really_is_negative(dentry)) {
20982 +                       au_set_h_dptr(dentry, btop, NULL);
20983 +                       au_set_dbtop(dentry, bcpup);
20984 +                       au_set_dbbot(dentry, bcpup);
20985 +               }
20986 +               AuDebugOn(add_entry
20987 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
20988 +                         && !au_h_dptr(dentry, bcpup));
20989 +       }
20990 +
20991 +out:
20992 +       dput(parent);
20993 +       return err;
20994 +}
20995 +
20996 +/* ---------------------------------------------------------------------- */
20997 +
20998 +void au_pin_hdir_unlock(struct au_pin *p)
20999 +{
21000 +       if (p->hdir)
21001 +               au_hn_inode_unlock(p->hdir);
21002 +}
21003 +
21004 +int au_pin_hdir_lock(struct au_pin *p)
21005 +{
21006 +       int err;
21007 +
21008 +       err = 0;
21009 +       if (!p->hdir)
21010 +               goto out;
21011 +
21012 +       /* even if an error happens later, keep this lock */
21013 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21014 +
21015 +       err = -EBUSY;
21016 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21017 +               goto out;
21018 +
21019 +       err = 0;
21020 +       if (p->h_dentry)
21021 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21022 +                                 p->h_parent, p->br);
21023 +
21024 +out:
21025 +       return err;
21026 +}
21027 +
21028 +int au_pin_hdir_relock(struct au_pin *p)
21029 +{
21030 +       int err, i;
21031 +       struct inode *h_i;
21032 +       struct dentry *h_d[] = {
21033 +               p->h_dentry,
21034 +               p->h_parent
21035 +       };
21036 +
21037 +       err = au_pin_hdir_lock(p);
21038 +       if (unlikely(err))
21039 +               goto out;
21040 +
21041 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21042 +               if (!h_d[i])
21043 +                       continue;
21044 +               if (d_is_positive(h_d[i])) {
21045 +                       h_i = d_inode(h_d[i]);
21046 +                       err = !h_i->i_nlink;
21047 +               }
21048 +       }
21049 +
21050 +out:
21051 +       return err;
21052 +}
21053 +
21054 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21055 +{
21056 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21057 +}
21058 +
21059 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21060 +{
21061 +       if (p->hdir) {
21062 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21063 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21064 +               au_pin_hdir_set_owner(p, current);
21065 +       }
21066 +}
21067 +
21068 +void au_pin_hdir_release(struct au_pin *p)
21069 +{
21070 +       if (p->hdir) {
21071 +               au_pin_hdir_set_owner(p, p->task);
21072 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21073 +       }
21074 +}
21075 +
21076 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21077 +{
21078 +       if (pin && pin->parent)
21079 +               return au_h_dptr(pin->parent, pin->bindex);
21080 +       return NULL;
21081 +}
21082 +
21083 +void au_unpin(struct au_pin *p)
21084 +{
21085 +       if (p->hdir)
21086 +               au_pin_hdir_unlock(p);
21087 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21088 +               vfsub_mnt_drop_write(p->h_mnt);
21089 +       if (!p->hdir)
21090 +               return;
21091 +
21092 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21093 +               di_read_unlock(p->parent, AuLock_IR);
21094 +       iput(p->hdir->hi_inode);
21095 +       dput(p->parent);
21096 +       p->parent = NULL;
21097 +       p->hdir = NULL;
21098 +       p->h_mnt = NULL;
21099 +       /* do not clear p->task */
21100 +}
21101 +
21102 +int au_do_pin(struct au_pin *p)
21103 +{
21104 +       int err;
21105 +       struct super_block *sb;
21106 +       struct inode *h_dir;
21107 +
21108 +       err = 0;
21109 +       sb = p->dentry->d_sb;
21110 +       p->br = au_sbr(sb, p->bindex);
21111 +       if (IS_ROOT(p->dentry)) {
21112 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21113 +                       p->h_mnt = au_br_mnt(p->br);
21114 +                       err = vfsub_mnt_want_write(p->h_mnt);
21115 +                       if (unlikely(err)) {
21116 +                               au_fclr_pin(p->flags, MNT_WRITE);
21117 +                               goto out_err;
21118 +                       }
21119 +               }
21120 +               goto out;
21121 +       }
21122 +
21123 +       p->h_dentry = NULL;
21124 +       if (p->bindex <= au_dbbot(p->dentry))
21125 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21126 +
21127 +       p->parent = dget_parent(p->dentry);
21128 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21129 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21130 +
21131 +       h_dir = NULL;
21132 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21133 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21134 +       if (p->hdir)
21135 +               h_dir = p->hdir->hi_inode;
21136 +
21137 +       /*
21138 +        * udba case, or
21139 +        * if DI_LOCKED is not set, then p->parent may be different
21140 +        * and h_parent can be NULL.
21141 +        */
21142 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21143 +               err = -EBUSY;
21144 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21145 +                       di_read_unlock(p->parent, AuLock_IR);
21146 +               dput(p->parent);
21147 +               p->parent = NULL;
21148 +               goto out_err;
21149 +       }
21150 +
21151 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21152 +               p->h_mnt = au_br_mnt(p->br);
21153 +               err = vfsub_mnt_want_write(p->h_mnt);
21154 +               if (unlikely(err)) {
21155 +                       au_fclr_pin(p->flags, MNT_WRITE);
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 +
21164 +       au_igrab(h_dir);
21165 +       err = au_pin_hdir_lock(p);
21166 +       if (!err)
21167 +               goto out; /* success */
21168 +
21169 +       au_unpin(p);
21170 +
21171 +out_err:
21172 +       pr_err("err %d\n", err);
21173 +       err = au_busy_or_stale();
21174 +out:
21175 +       return err;
21176 +}
21177 +
21178 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21179 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21180 +                unsigned int udba, unsigned char flags)
21181 +{
21182 +       p->dentry = dentry;
21183 +       p->udba = udba;
21184 +       p->lsc_di = lsc_di;
21185 +       p->lsc_hi = lsc_hi;
21186 +       p->flags = flags;
21187 +       p->bindex = bindex;
21188 +
21189 +       p->parent = NULL;
21190 +       p->hdir = NULL;
21191 +       p->h_mnt = NULL;
21192 +
21193 +       p->h_dentry = NULL;
21194 +       p->h_parent = NULL;
21195 +       p->br = NULL;
21196 +       p->task = current;
21197 +}
21198 +
21199 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21200 +          unsigned int udba, unsigned char flags)
21201 +{
21202 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21203 +                   udba, flags);
21204 +       return au_do_pin(pin);
21205 +}
21206 +
21207 +/* ---------------------------------------------------------------------- */
21208 +
21209 +/*
21210 + * ->setattr() and ->getattr() are called in various cases.
21211 + * chmod, stat: dentry is revalidated.
21212 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21213 + *               unhashed.
21214 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21215 + */
21216 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21217 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21218 +{
21219 +       int err;
21220 +       struct dentry *parent;
21221 +
21222 +       err = 0;
21223 +       if (au_digen_test(dentry, sigen)) {
21224 +               parent = dget_parent(dentry);
21225 +               di_read_lock_parent(parent, AuLock_IR);
21226 +               err = au_refresh_dentry(dentry, parent);
21227 +               di_read_unlock(parent, AuLock_IR);
21228 +               dput(parent);
21229 +       }
21230 +
21231 +       AuTraceErr(err);
21232 +       return err;
21233 +}
21234 +
21235 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21236 +                    struct au_icpup_args *a)
21237 +{
21238 +       int err;
21239 +       loff_t sz;
21240 +       aufs_bindex_t btop, ibtop;
21241 +       struct dentry *hi_wh, *parent;
21242 +       struct inode *inode;
21243 +       struct au_wr_dir_args wr_dir_args = {
21244 +               .force_btgt     = -1,
21245 +               .flags          = 0
21246 +       };
21247 +
21248 +       if (d_is_dir(dentry))
21249 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21250 +       /* plink or hi_wh() case */
21251 +       btop = au_dbtop(dentry);
21252 +       inode = d_inode(dentry);
21253 +       ibtop = au_ibtop(inode);
21254 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21255 +               wr_dir_args.force_btgt = ibtop;
21256 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21257 +       if (unlikely(err < 0))
21258 +               goto out;
21259 +       a->btgt = err;
21260 +       if (err != btop)
21261 +               au_fset_icpup(a->flags, DID_CPUP);
21262 +
21263 +       err = 0;
21264 +       a->pin_flags = AuPin_MNT_WRITE;
21265 +       parent = NULL;
21266 +       if (!IS_ROOT(dentry)) {
21267 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21268 +               parent = dget_parent(dentry);
21269 +               di_write_lock_parent(parent);
21270 +       }
21271 +
21272 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21273 +       if (unlikely(err))
21274 +               goto out_parent;
21275 +
21276 +       sz = -1;
21277 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21278 +       a->h_inode = d_inode(a->h_path.dentry);
21279 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21280 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21281 +               if (ia->ia_size < i_size_read(a->h_inode))
21282 +                       sz = ia->ia_size;
21283 +               inode_unlock_shared(a->h_inode);
21284 +       }
21285 +
21286 +       hi_wh = NULL;
21287 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21288 +               hi_wh = au_hi_wh(inode, a->btgt);
21289 +               if (!hi_wh) {
21290 +                       struct au_cp_generic cpg = {
21291 +                               .dentry = dentry,
21292 +                               .bdst   = a->btgt,
21293 +                               .bsrc   = -1,
21294 +                               .len    = sz,
21295 +                               .pin    = &a->pin
21296 +                       };
21297 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21298 +                       if (unlikely(err))
21299 +                               goto out_unlock;
21300 +                       hi_wh = au_hi_wh(inode, a->btgt);
21301 +                       /* todo: revalidate hi_wh? */
21302 +               }
21303 +       }
21304 +
21305 +       if (parent) {
21306 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21307 +               di_downgrade_lock(parent, AuLock_IR);
21308 +               dput(parent);
21309 +               parent = NULL;
21310 +       }
21311 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21312 +               goto out; /* success */
21313 +
21314 +       if (!d_unhashed(dentry)) {
21315 +               struct au_cp_generic cpg = {
21316 +                       .dentry = dentry,
21317 +                       .bdst   = a->btgt,
21318 +                       .bsrc   = btop,
21319 +                       .len    = sz,
21320 +                       .pin    = &a->pin,
21321 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21322 +               };
21323 +               err = au_sio_cpup_simple(&cpg);
21324 +               if (!err)
21325 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21326 +       } else if (!hi_wh)
21327 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21328 +       else
21329 +               a->h_path.dentry = hi_wh; /* do not dget here */
21330 +
21331 +out_unlock:
21332 +       a->h_inode = d_inode(a->h_path.dentry);
21333 +       if (!err)
21334 +               goto out; /* success */
21335 +       au_unpin(&a->pin);
21336 +out_parent:
21337 +       if (parent) {
21338 +               di_write_unlock(parent);
21339 +               dput(parent);
21340 +       }
21341 +out:
21342 +       if (!err)
21343 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21344 +       return err;
21345 +}
21346 +
21347 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21348 +{
21349 +       int err;
21350 +       struct inode *inode, *delegated;
21351 +       struct super_block *sb;
21352 +       struct file *file;
21353 +       struct au_icpup_args *a;
21354 +
21355 +       inode = d_inode(dentry);
21356 +       IMustLock(inode);
21357 +
21358 +       err = setattr_prepare(dentry, ia);
21359 +       if (unlikely(err))
21360 +               goto out;
21361 +
21362 +       err = -ENOMEM;
21363 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21364 +       if (unlikely(!a))
21365 +               goto out;
21366 +
21367 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21368 +               ia->ia_valid &= ~ATTR_MODE;
21369 +
21370 +       file = NULL;
21371 +       sb = dentry->d_sb;
21372 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21373 +       if (unlikely(err))
21374 +               goto out_kfree;
21375 +
21376 +       if (ia->ia_valid & ATTR_FILE) {
21377 +               /* currently ftruncate(2) only */
21378 +               AuDebugOn(!d_is_reg(dentry));
21379 +               file = ia->ia_file;
21380 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21381 +                                           /*fi_lsc*/0);
21382 +               if (unlikely(err))
21383 +                       goto out_si;
21384 +               ia->ia_file = au_hf_top(file);
21385 +               a->udba = AuOpt_UDBA_NONE;
21386 +       } else {
21387 +               /* fchmod() doesn't pass ia_file */
21388 +               a->udba = au_opt_udba(sb);
21389 +               di_write_lock_child(dentry);
21390 +               /* no d_unlinked(), to set UDBA_NONE for root */
21391 +               if (d_unhashed(dentry))
21392 +                       a->udba = AuOpt_UDBA_NONE;
21393 +               if (a->udba != AuOpt_UDBA_NONE) {
21394 +                       AuDebugOn(IS_ROOT(dentry));
21395 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21396 +                       if (unlikely(err))
21397 +                               goto out_dentry;
21398 +               }
21399 +       }
21400 +
21401 +       err = au_pin_and_icpup(dentry, ia, a);
21402 +       if (unlikely(err < 0))
21403 +               goto out_dentry;
21404 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21405 +               ia->ia_file = NULL;
21406 +               ia->ia_valid &= ~ATTR_FILE;
21407 +       }
21408 +
21409 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21410 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21411 +           == (ATTR_MODE | ATTR_CTIME)) {
21412 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21413 +               if (unlikely(err))
21414 +                       goto out_unlock;
21415 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21416 +                  && (ia->ia_valid & ATTR_CTIME)) {
21417 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21418 +               if (unlikely(err))
21419 +                       goto out_unlock;
21420 +       }
21421 +
21422 +       if (ia->ia_valid & ATTR_SIZE) {
21423 +               struct file *f;
21424 +
21425 +               if (ia->ia_size < i_size_read(inode))
21426 +                       /* unmap only */
21427 +                       truncate_setsize(inode, ia->ia_size);
21428 +
21429 +               f = NULL;
21430 +               if (ia->ia_valid & ATTR_FILE)
21431 +                       f = ia->ia_file;
21432 +               inode_unlock(a->h_inode);
21433 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21434 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21435 +       } else {
21436 +               delegated = NULL;
21437 +               while (1) {
21438 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21439 +                       if (delegated) {
21440 +                               err = break_deleg_wait(&delegated);
21441 +                               if (!err)
21442 +                                       continue;
21443 +                       }
21444 +                       break;
21445 +               }
21446 +       }
21447 +       /*
21448 +        * regardless aufs 'acl' option setting.
21449 +        * why don't all acl-aware fs call this func from their ->setattr()?
21450 +        */
21451 +       if (!err && (ia->ia_valid & ATTR_MODE))
21452 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21453 +       if (!err)
21454 +               au_cpup_attr_changeable(inode);
21455 +
21456 +out_unlock:
21457 +       inode_unlock(a->h_inode);
21458 +       au_unpin(&a->pin);
21459 +       if (unlikely(err))
21460 +               au_update_dbtop(dentry);
21461 +out_dentry:
21462 +       di_write_unlock(dentry);
21463 +       if (file) {
21464 +               fi_write_unlock(file);
21465 +               ia->ia_file = file;
21466 +               ia->ia_valid |= ATTR_FILE;
21467 +       }
21468 +out_si:
21469 +       si_read_unlock(sb);
21470 +out_kfree:
21471 +       au_kfree_rcu(a);
21472 +out:
21473 +       AuTraceErr(err);
21474 +       return err;
21475 +}
21476 +
21477 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21478 +static int au_h_path_to_set_attr(struct dentry *dentry,
21479 +                                struct au_icpup_args *a, struct path *h_path)
21480 +{
21481 +       int err;
21482 +       struct super_block *sb;
21483 +
21484 +       sb = dentry->d_sb;
21485 +       a->udba = au_opt_udba(sb);
21486 +       /* no d_unlinked(), to set UDBA_NONE for root */
21487 +       if (d_unhashed(dentry))
21488 +               a->udba = AuOpt_UDBA_NONE;
21489 +       if (a->udba != AuOpt_UDBA_NONE) {
21490 +               AuDebugOn(IS_ROOT(dentry));
21491 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21492 +               if (unlikely(err))
21493 +                       goto out;
21494 +       }
21495 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21496 +       if (unlikely(err < 0))
21497 +               goto out;
21498 +
21499 +       h_path->dentry = a->h_path.dentry;
21500 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21501 +
21502 +out:
21503 +       return err;
21504 +}
21505 +
21506 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21507 +                 struct au_sxattr *arg)
21508 +{
21509 +       int err;
21510 +       struct path h_path;
21511 +       struct super_block *sb;
21512 +       struct au_icpup_args *a;
21513 +       struct inode *h_inode;
21514 +
21515 +       IMustLock(inode);
21516 +
21517 +       err = -ENOMEM;
21518 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21519 +       if (unlikely(!a))
21520 +               goto out;
21521 +
21522 +       sb = dentry->d_sb;
21523 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21524 +       if (unlikely(err))
21525 +               goto out_kfree;
21526 +
21527 +       h_path.dentry = NULL;   /* silence gcc */
21528 +       di_write_lock_child(dentry);
21529 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21530 +       if (unlikely(err))
21531 +               goto out_di;
21532 +
21533 +       inode_unlock(a->h_inode);
21534 +       switch (arg->type) {
21535 +       case AU_XATTR_SET:
21536 +               AuDebugOn(d_is_negative(h_path.dentry));
21537 +               err = vfsub_setxattr(h_path.dentry,
21538 +                                    arg->u.set.name, arg->u.set.value,
21539 +                                    arg->u.set.size, arg->u.set.flags);
21540 +               break;
21541 +       case AU_ACL_SET:
21542 +               err = -EOPNOTSUPP;
21543 +               h_inode = d_inode(h_path.dentry);
21544 +               if (h_inode->i_op->set_acl)
21545 +                       /* this will call posix_acl_update_mode */
21546 +                       err = h_inode->i_op->set_acl(h_inode,
21547 +                                                    arg->u.acl_set.acl,
21548 +                                                    arg->u.acl_set.type);
21549 +               break;
21550 +       }
21551 +       if (!err)
21552 +               au_cpup_attr_timesizes(inode);
21553 +
21554 +       au_unpin(&a->pin);
21555 +       if (unlikely(err))
21556 +               au_update_dbtop(dentry);
21557 +
21558 +out_di:
21559 +       di_write_unlock(dentry);
21560 +       si_read_unlock(sb);
21561 +out_kfree:
21562 +       au_kfree_rcu(a);
21563 +out:
21564 +       AuTraceErr(err);
21565 +       return err;
21566 +}
21567 +#endif
21568 +
21569 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21570 +                            unsigned int nlink)
21571 +{
21572 +       unsigned int n;
21573 +
21574 +       inode->i_mode = st->mode;
21575 +       /* don't i_[ug]id_write() here */
21576 +       inode->i_uid = st->uid;
21577 +       inode->i_gid = st->gid;
21578 +       inode->i_atime = st->atime;
21579 +       inode->i_mtime = st->mtime;
21580 +       inode->i_ctime = st->ctime;
21581 +
21582 +       au_cpup_attr_nlink(inode, /*force*/0);
21583 +       if (S_ISDIR(inode->i_mode)) {
21584 +               n = inode->i_nlink;
21585 +               n -= nlink;
21586 +               n += st->nlink;
21587 +               smp_mb(); /* for i_nlink */
21588 +               /* 0 can happen */
21589 +               set_nlink(inode, n);
21590 +       }
21591 +
21592 +       spin_lock(&inode->i_lock);
21593 +       inode->i_blocks = st->blocks;
21594 +       i_size_write(inode, st->size);
21595 +       spin_unlock(&inode->i_lock);
21596 +}
21597 +
21598 +/*
21599 + * common routine for aufs_getattr() and au_getxattr().
21600 + * returns zero or negative (an error).
21601 + * @dentry will be read-locked in success.
21602 + */
21603 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21604 +                     struct path *h_path, int locked)
21605 +{
21606 +       int err;
21607 +       unsigned int mnt_flags, sigen;
21608 +       unsigned char udba_none;
21609 +       aufs_bindex_t bindex;
21610 +       struct super_block *sb, *h_sb;
21611 +
21612 +       h_path->mnt = NULL;
21613 +       h_path->dentry = NULL;
21614 +
21615 +       err = 0;
21616 +       sb = dentry->d_sb;
21617 +       mnt_flags = au_mntflags(sb);
21618 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21619 +
21620 +       if (unlikely(locked))
21621 +               goto body; /* skip locking dinfo */
21622 +
21623 +       /* support fstat(2) */
21624 +       if (!d_unlinked(dentry) && !udba_none) {
21625 +               sigen = au_sigen(sb);
21626 +               err = au_digen_test(dentry, sigen);
21627 +               if (!err) {
21628 +                       di_read_lock_child(dentry, AuLock_IR);
21629 +                       err = au_dbrange_test(dentry);
21630 +                       if (unlikely(err)) {
21631 +                               di_read_unlock(dentry, AuLock_IR);
21632 +                               goto out;
21633 +                       }
21634 +               } else {
21635 +                       AuDebugOn(IS_ROOT(dentry));
21636 +                       di_write_lock_child(dentry);
21637 +                       err = au_dbrange_test(dentry);
21638 +                       if (!err)
21639 +                               err = au_reval_for_attr(dentry, sigen);
21640 +                       if (!err)
21641 +                               di_downgrade_lock(dentry, AuLock_IR);
21642 +                       else {
21643 +                               di_write_unlock(dentry);
21644 +                               goto out;
21645 +                       }
21646 +               }
21647 +       } else
21648 +               di_read_lock_child(dentry, AuLock_IR);
21649 +
21650 +body:
21651 +       if (!inode) {
21652 +               inode = d_inode(dentry);
21653 +               if (unlikely(!inode))
21654 +                       goto out;
21655 +       }
21656 +       bindex = au_ibtop(inode);
21657 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21658 +       h_sb = h_path->mnt->mnt_sb;
21659 +       if (!force
21660 +           && !au_test_fs_bad_iattr(h_sb)
21661 +           && udba_none)
21662 +               goto out; /* success */
21663 +
21664 +       if (au_dbtop(dentry) == bindex)
21665 +               h_path->dentry = au_h_dptr(dentry, bindex);
21666 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21667 +               h_path->dentry = au_plink_lkup(inode, bindex);
21668 +               if (IS_ERR(h_path->dentry))
21669 +                       /* pretending success */
21670 +                       h_path->dentry = NULL;
21671 +               else
21672 +                       dput(h_path->dentry);
21673 +       }
21674 +
21675 +out:
21676 +       return err;
21677 +}
21678 +
21679 +static int aufs_getattr(const struct path *path, struct kstat *st,
21680 +                       u32 request, unsigned int query)
21681 +{
21682 +       int err;
21683 +       unsigned char positive;
21684 +       struct path h_path;
21685 +       struct dentry *dentry;
21686 +       struct inode *inode;
21687 +       struct super_block *sb;
21688 +
21689 +       dentry = path->dentry;
21690 +       inode = d_inode(dentry);
21691 +       sb = dentry->d_sb;
21692 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21693 +       if (unlikely(err))
21694 +               goto out;
21695 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21696 +                               /*locked*/0);
21697 +       if (unlikely(err))
21698 +               goto out_si;
21699 +       if (unlikely(!h_path.dentry))
21700 +               /* illegally overlapped or something */
21701 +               goto out_fill; /* pretending success */
21702 +
21703 +       positive = d_is_positive(h_path.dentry);
21704 +       if (positive)
21705 +               /* no vfsub version */
21706 +               err = vfs_getattr(&h_path, st, request, query);
21707 +       if (!err) {
21708 +               if (positive)
21709 +                       au_refresh_iattr(inode, st,
21710 +                                        d_inode(h_path.dentry)->i_nlink);
21711 +               goto out_fill; /* success */
21712 +       }
21713 +       AuTraceErr(err);
21714 +       goto out_di;
21715 +
21716 +out_fill:
21717 +       generic_fillattr(inode, st);
21718 +out_di:
21719 +       di_read_unlock(dentry, AuLock_IR);
21720 +out_si:
21721 +       si_read_unlock(sb);
21722 +out:
21723 +       AuTraceErr(err);
21724 +       return err;
21725 +}
21726 +
21727 +/* ---------------------------------------------------------------------- */
21728 +
21729 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21730 +                                struct delayed_call *done)
21731 +{
21732 +       const char *ret;
21733 +       struct dentry *h_dentry;
21734 +       struct inode *h_inode;
21735 +       int err;
21736 +       aufs_bindex_t bindex;
21737 +
21738 +       ret = NULL; /* suppress a warning */
21739 +       err = -ECHILD;
21740 +       if (!dentry)
21741 +               goto out;
21742 +
21743 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21744 +       if (unlikely(err))
21745 +               goto out;
21746 +
21747 +       err = au_d_hashed_positive(dentry);
21748 +       if (unlikely(err))
21749 +               goto out_unlock;
21750 +
21751 +       err = -EINVAL;
21752 +       inode = d_inode(dentry);
21753 +       bindex = au_ibtop(inode);
21754 +       h_inode = au_h_iptr(inode, bindex);
21755 +       if (unlikely(!h_inode->i_op->get_link))
21756 +               goto out_unlock;
21757 +
21758 +       err = -EBUSY;
21759 +       h_dentry = NULL;
21760 +       if (au_dbtop(dentry) <= bindex) {
21761 +               h_dentry = au_h_dptr(dentry, bindex);
21762 +               if (h_dentry)
21763 +                       dget(h_dentry);
21764 +       }
21765 +       if (!h_dentry) {
21766 +               h_dentry = d_find_any_alias(h_inode);
21767 +               if (IS_ERR(h_dentry)) {
21768 +                       err = PTR_ERR(h_dentry);
21769 +                       goto out_unlock;
21770 +               }
21771 +       }
21772 +       if (unlikely(!h_dentry))
21773 +               goto out_unlock;
21774 +
21775 +       err = 0;
21776 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21777 +       AuDbgDentry(h_dentry);
21778 +       ret = vfs_get_link(h_dentry, done);
21779 +       dput(h_dentry);
21780 +       if (IS_ERR(ret))
21781 +               err = PTR_ERR(ret);
21782 +
21783 +out_unlock:
21784 +       aufs_read_unlock(dentry, AuLock_IR);
21785 +out:
21786 +       if (unlikely(err))
21787 +               ret = ERR_PTR(err);
21788 +       AuTraceErrPtr(ret);
21789 +       return ret;
21790 +}
21791 +
21792 +/* ---------------------------------------------------------------------- */
21793 +
21794 +static int au_is_special(struct inode *inode)
21795 +{
21796 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21797 +}
21798 +
21799 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21800 +                           int flags)
21801 +{
21802 +       int err;
21803 +       aufs_bindex_t bindex;
21804 +       struct super_block *sb;
21805 +       struct inode *h_inode;
21806 +       struct vfsmount *h_mnt;
21807 +
21808 +       sb = inode->i_sb;
21809 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21810 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21811 +
21812 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21813 +       lockdep_off();
21814 +       si_read_lock(sb, AuLock_FLUSH);
21815 +       ii_write_lock_child(inode);
21816 +
21817 +       err = 0;
21818 +       bindex = au_ibtop(inode);
21819 +       h_inode = au_h_iptr(inode, bindex);
21820 +       if (!au_test_ro(sb, bindex, inode)) {
21821 +               h_mnt = au_sbr_mnt(sb, bindex);
21822 +               err = vfsub_mnt_want_write(h_mnt);
21823 +               if (!err) {
21824 +                       err = vfsub_update_time(h_inode, ts, flags);
21825 +                       vfsub_mnt_drop_write(h_mnt);
21826 +               }
21827 +       } else if (au_is_special(h_inode)) {
21828 +               /*
21829 +                * Never copy-up here.
21830 +                * These special files may already be opened and used for
21831 +                * communicating. If we copied it up, then the communication
21832 +                * would be corrupted.
21833 +                */
21834 +               AuWarn1("timestamps for i%lu are ignored "
21835 +                       "since it is on readonly branch (hi%lu).\n",
21836 +                       inode->i_ino, h_inode->i_ino);
21837 +       } else if (flags & ~S_ATIME) {
21838 +               err = -EIO;
21839 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21840 +               AuDebugOn(1);
21841 +       }
21842 +
21843 +       if (!err)
21844 +               au_cpup_attr_timesizes(inode);
21845 +       ii_write_unlock(inode);
21846 +       si_read_unlock(sb);
21847 +       lockdep_on();
21848 +
21849 +       if (!err && (flags & S_VERSION))
21850 +               inode_inc_iversion(inode);
21851 +
21852 +       return err;
21853 +}
21854 +
21855 +/* ---------------------------------------------------------------------- */
21856 +
21857 +/* no getattr version will be set by module.c:aufs_init() */
21858 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21859 +       aufs_iop[] = {
21860 +       [AuIop_SYMLINK] = {
21861 +               .permission     = aufs_permission,
21862 +#ifdef CONFIG_FS_POSIX_ACL
21863 +               .get_acl        = aufs_get_acl,
21864 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
21865 +#endif
21866 +
21867 +               .setattr        = aufs_setattr,
21868 +               .getattr        = aufs_getattr,
21869 +
21870 +#ifdef CONFIG_AUFS_XATTR
21871 +               .listxattr      = aufs_listxattr,
21872 +#endif
21873 +
21874 +               .get_link       = aufs_get_link,
21875 +
21876 +               /* .update_time = aufs_update_time */
21877 +       },
21878 +       [AuIop_DIR] = {
21879 +               .create         = aufs_create,
21880 +               .lookup         = aufs_lookup,
21881 +               .link           = aufs_link,
21882 +               .unlink         = aufs_unlink,
21883 +               .symlink        = aufs_symlink,
21884 +               .mkdir          = aufs_mkdir,
21885 +               .rmdir          = aufs_rmdir,
21886 +               .mknod          = aufs_mknod,
21887 +               .rename         = aufs_rename,
21888 +
21889 +               .permission     = aufs_permission,
21890 +#ifdef CONFIG_FS_POSIX_ACL
21891 +               .get_acl        = aufs_get_acl,
21892 +               .set_acl        = aufs_set_acl,
21893 +#endif
21894 +
21895 +               .setattr        = aufs_setattr,
21896 +               .getattr        = aufs_getattr,
21897 +
21898 +#ifdef CONFIG_AUFS_XATTR
21899 +               .listxattr      = aufs_listxattr,
21900 +#endif
21901 +
21902 +               .update_time    = aufs_update_time,
21903 +               .atomic_open    = aufs_atomic_open,
21904 +               .tmpfile        = aufs_tmpfile
21905 +       },
21906 +       [AuIop_OTHER] = {
21907 +               .permission     = aufs_permission,
21908 +#ifdef CONFIG_FS_POSIX_ACL
21909 +               .get_acl        = aufs_get_acl,
21910 +               .set_acl        = aufs_set_acl,
21911 +#endif
21912 +
21913 +               .setattr        = aufs_setattr,
21914 +               .getattr        = aufs_getattr,
21915 +
21916 +#ifdef CONFIG_AUFS_XATTR
21917 +               .listxattr      = aufs_listxattr,
21918 +#endif
21919 +
21920 +               .update_time    = aufs_update_time
21921 +       }
21922 +};
21923 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21924 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21925 +++ linux/fs/aufs/i_op_del.c    2021-02-24 13:33:42.744347058 +0100
21926 @@ -0,0 +1,513 @@
21927 +// SPDX-License-Identifier: GPL-2.0
21928 +/*
21929 + * Copyright (C) 2005-2020 Junjiro R. Okajima
21930 + *
21931 + * This program, aufs is free software; you can redistribute it and/or modify
21932 + * it under the terms of the GNU General Public License as published by
21933 + * the Free Software Foundation; either version 2 of the License, or
21934 + * (at your option) any later version.
21935 + *
21936 + * This program is distributed in the hope that it will be useful,
21937 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21938 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21939 + * GNU General Public License for more details.
21940 + *
21941 + * You should have received a copy of the GNU General Public License
21942 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21943 + */
21944 +
21945 +/*
21946 + * inode operations (del entry)
21947 + */
21948 +
21949 +#include <linux/iversion.h>
21950 +#include "aufs.h"
21951 +
21952 +/*
21953 + * decide if a new whiteout for @dentry is necessary or not.
21954 + * when it is necessary, prepare the parent dir for the upper branch whose
21955 + * branch index is @bcpup for creation. the actual creation of the whiteout will
21956 + * be done by caller.
21957 + * return value:
21958 + * 0: wh is unnecessary
21959 + * plus: wh is necessary
21960 + * minus: error
21961 + */
21962 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
21963 +{
21964 +       int need_wh, err;
21965 +       aufs_bindex_t btop;
21966 +       struct super_block *sb;
21967 +
21968 +       sb = dentry->d_sb;
21969 +       btop = au_dbtop(dentry);
21970 +       if (*bcpup < 0) {
21971 +               *bcpup = btop;
21972 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
21973 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
21974 +                       *bcpup = err;
21975 +                       if (unlikely(err < 0))
21976 +                               goto out;
21977 +               }
21978 +       } else
21979 +               AuDebugOn(btop < *bcpup
21980 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
21981 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
21982 +
21983 +       if (*bcpup != btop) {
21984 +               err = au_cpup_dirs(dentry, *bcpup);
21985 +               if (unlikely(err))
21986 +                       goto out;
21987 +               need_wh = 1;
21988 +       } else {
21989 +               struct au_dinfo *dinfo, *tmp;
21990 +
21991 +               need_wh = -ENOMEM;
21992 +               dinfo = au_di(dentry);
21993 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
21994 +               if (tmp) {
21995 +                       au_di_cp(tmp, dinfo);
21996 +                       au_di_swap(tmp, dinfo);
21997 +                       /* returns the number of positive dentries */
21998 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
21999 +                                                /* AuLkup_IGNORE_PERM */ 0);
22000 +                       au_di_swap(tmp, dinfo);
22001 +                       au_rw_write_unlock(&tmp->di_rwsem);
22002 +                       au_di_free(tmp);
22003 +               }
22004 +       }
22005 +       AuDbg("need_wh %d\n", need_wh);
22006 +       err = need_wh;
22007 +
22008 +out:
22009 +       return err;
22010 +}
22011 +
22012 +/*
22013 + * simple tests for the del-entry operations.
22014 + * following the checks in vfs, plus the parent-child relationship.
22015 + */
22016 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22017 +              struct dentry *h_parent, int isdir)
22018 +{
22019 +       int err;
22020 +       umode_t h_mode;
22021 +       struct dentry *h_dentry, *h_latest;
22022 +       struct inode *h_inode;
22023 +
22024 +       h_dentry = au_h_dptr(dentry, bindex);
22025 +       if (d_really_is_positive(dentry)) {
22026 +               err = -ENOENT;
22027 +               if (unlikely(d_is_negative(h_dentry)))
22028 +                       goto out;
22029 +               h_inode = d_inode(h_dentry);
22030 +               if (unlikely(!h_inode->i_nlink))
22031 +                       goto out;
22032 +
22033 +               h_mode = h_inode->i_mode;
22034 +               if (!isdir) {
22035 +                       err = -EISDIR;
22036 +                       if (unlikely(S_ISDIR(h_mode)))
22037 +                               goto out;
22038 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22039 +                       err = -ENOTDIR;
22040 +                       goto out;
22041 +               }
22042 +       } else {
22043 +               /* rename(2) case */
22044 +               err = -EIO;
22045 +               if (unlikely(d_is_positive(h_dentry)))
22046 +                       goto out;
22047 +       }
22048 +
22049 +       err = -ENOENT;
22050 +       /* expected parent dir is locked */
22051 +       if (unlikely(h_parent != h_dentry->d_parent))
22052 +               goto out;
22053 +       err = 0;
22054 +
22055 +       /*
22056 +        * rmdir a dir may break the consistency on some filesystem.
22057 +        * let's try heavy test.
22058 +        */
22059 +       err = -EACCES;
22060 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22061 +                    && au_test_h_perm(d_inode(h_parent),
22062 +                                      MAY_EXEC | MAY_WRITE)))
22063 +               goto out;
22064 +
22065 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22066 +       err = -EIO;
22067 +       if (IS_ERR(h_latest))
22068 +               goto out;
22069 +       if (h_latest == h_dentry)
22070 +               err = 0;
22071 +       dput(h_latest);
22072 +
22073 +out:
22074 +       return err;
22075 +}
22076 +
22077 +/*
22078 + * decide the branch where we operate for @dentry. the branch index will be set
22079 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22080 + * dir for reverting.
22081 + * when a new whiteout is necessary, create it.
22082 + */
22083 +static struct dentry*
22084 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22085 +                   struct au_dtime *dt, struct au_pin *pin)
22086 +{
22087 +       struct dentry *wh_dentry;
22088 +       struct super_block *sb;
22089 +       struct path h_path;
22090 +       int err, need_wh;
22091 +       unsigned int udba;
22092 +       aufs_bindex_t bcpup;
22093 +
22094 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22095 +       wh_dentry = ERR_PTR(need_wh);
22096 +       if (unlikely(need_wh < 0))
22097 +               goto out;
22098 +
22099 +       sb = dentry->d_sb;
22100 +       udba = au_opt_udba(sb);
22101 +       bcpup = *rbcpup;
22102 +       err = au_pin(pin, dentry, bcpup, udba,
22103 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22104 +       wh_dentry = ERR_PTR(err);
22105 +       if (unlikely(err))
22106 +               goto out;
22107 +
22108 +       h_path.dentry = au_pinned_h_parent(pin);
22109 +       if (udba != AuOpt_UDBA_NONE
22110 +           && au_dbtop(dentry) == bcpup) {
22111 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22112 +               wh_dentry = ERR_PTR(err);
22113 +               if (unlikely(err))
22114 +                       goto out_unpin;
22115 +       }
22116 +
22117 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22118 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22119 +       wh_dentry = NULL;
22120 +       if (!need_wh)
22121 +               goto out; /* success, no need to create whiteout */
22122 +
22123 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22124 +       if (IS_ERR(wh_dentry))
22125 +               goto out_unpin;
22126 +
22127 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22128 +       goto out; /* success */
22129 +
22130 +out_unpin:
22131 +       au_unpin(pin);
22132 +out:
22133 +       return wh_dentry;
22134 +}
22135 +
22136 +/*
22137 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22138 + * in order to be revertible and save time for removing many child whiteouts
22139 + * under the dir.
22140 + * returns 1 when there are too many child whiteout and caller should remove
22141 + * them asynchronously. returns 0 when the number of children is enough small to
22142 + * remove now or the branch fs is a remote fs.
22143 + * otherwise return an error.
22144 + */
22145 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22146 +                          struct au_nhash *whlist, struct inode *dir)
22147 +{
22148 +       int rmdir_later, err, dirwh;
22149 +       struct dentry *h_dentry;
22150 +       struct super_block *sb;
22151 +       struct inode *inode;
22152 +
22153 +       sb = dentry->d_sb;
22154 +       SiMustAnyLock(sb);
22155 +       h_dentry = au_h_dptr(dentry, bindex);
22156 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22157 +       if (unlikely(err))
22158 +               goto out;
22159 +
22160 +       /* stop monitoring */
22161 +       inode = d_inode(dentry);
22162 +       au_hn_free(au_hi(inode, bindex));
22163 +
22164 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22165 +               dirwh = au_sbi(sb)->si_dirwh;
22166 +               rmdir_later = (dirwh <= 1);
22167 +               if (!rmdir_later)
22168 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22169 +                                                             dirwh);
22170 +               if (rmdir_later)
22171 +                       return rmdir_later;
22172 +       }
22173 +
22174 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22175 +       if (unlikely(err)) {
22176 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22177 +                       h_dentry, bindex, err);
22178 +               err = 0;
22179 +       }
22180 +
22181 +out:
22182 +       AuTraceErr(err);
22183 +       return err;
22184 +}
22185 +
22186 +/*
22187 + * final procedure for deleting a entry.
22188 + * maintain dentry and iattr.
22189 + */
22190 +static void epilog(struct inode *dir, struct dentry *dentry,
22191 +                  aufs_bindex_t bindex)
22192 +{
22193 +       struct inode *inode;
22194 +
22195 +       inode = d_inode(dentry);
22196 +       d_drop(dentry);
22197 +       inode->i_ctime = dir->i_ctime;
22198 +
22199 +       au_dir_ts(dir, bindex);
22200 +       inode_inc_iversion(dir);
22201 +}
22202 +
22203 +/*
22204 + * when an error happened, remove the created whiteout and revert everything.
22205 + */
22206 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22207 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22208 +                    struct dentry *dentry, struct au_dtime *dt)
22209 +{
22210 +       int rerr;
22211 +       struct path h_path = {
22212 +               .dentry = wh_dentry,
22213 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22214 +       };
22215 +
22216 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22217 +       if (!rerr) {
22218 +               au_set_dbwh(dentry, bwh);
22219 +               au_dtime_revert(dt);
22220 +               return 0;
22221 +       }
22222 +
22223 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22224 +       return -EIO;
22225 +}
22226 +
22227 +/* ---------------------------------------------------------------------- */
22228 +
22229 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22230 +{
22231 +       int err;
22232 +       aufs_bindex_t bwh, bindex, btop;
22233 +       struct inode *inode, *h_dir, *delegated;
22234 +       struct dentry *parent, *wh_dentry;
22235 +       /* to reduce stack size */
22236 +       struct {
22237 +               struct au_dtime dt;
22238 +               struct au_pin pin;
22239 +               struct path h_path;
22240 +       } *a;
22241 +
22242 +       IMustLock(dir);
22243 +
22244 +       err = -ENOMEM;
22245 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22246 +       if (unlikely(!a))
22247 +               goto out;
22248 +
22249 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22250 +       if (unlikely(err))
22251 +               goto out_free;
22252 +       err = au_d_hashed_positive(dentry);
22253 +       if (unlikely(err))
22254 +               goto out_unlock;
22255 +       inode = d_inode(dentry);
22256 +       IMustLock(inode);
22257 +       err = -EISDIR;
22258 +       if (unlikely(d_is_dir(dentry)))
22259 +               goto out_unlock; /* possible? */
22260 +
22261 +       btop = au_dbtop(dentry);
22262 +       bwh = au_dbwh(dentry);
22263 +       bindex = -1;
22264 +       parent = dentry->d_parent; /* dir inode is locked */
22265 +       di_write_lock_parent(parent);
22266 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22267 +                                       &a->pin);
22268 +       err = PTR_ERR(wh_dentry);
22269 +       if (IS_ERR(wh_dentry))
22270 +               goto out_parent;
22271 +
22272 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22273 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22274 +       dget(a->h_path.dentry);
22275 +       if (bindex == btop) {
22276 +               h_dir = au_pinned_h_dir(&a->pin);
22277 +               delegated = NULL;
22278 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22279 +               if (unlikely(err == -EWOULDBLOCK)) {
22280 +                       pr_warn("cannot retry for NFSv4 delegation"
22281 +                               " for an internal unlink\n");
22282 +                       iput(delegated);
22283 +               }
22284 +       } else {
22285 +               /* dir inode is locked */
22286 +               h_dir = d_inode(wh_dentry->d_parent);
22287 +               IMustLock(h_dir);
22288 +               err = 0;
22289 +       }
22290 +
22291 +       if (!err) {
22292 +               vfsub_drop_nlink(inode);
22293 +               epilog(dir, dentry, bindex);
22294 +
22295 +               /* update target timestamps */
22296 +               if (bindex == btop) {
22297 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22298 +                       /*ignore*/
22299 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22300 +               } else
22301 +                       /* todo: this timestamp may be reverted later */
22302 +                       inode->i_ctime = h_dir->i_ctime;
22303 +               goto out_unpin; /* success */
22304 +       }
22305 +
22306 +       /* revert */
22307 +       if (wh_dentry) {
22308 +               int rerr;
22309 +
22310 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22311 +                                &a->dt);
22312 +               if (rerr)
22313 +                       err = rerr;
22314 +       }
22315 +
22316 +out_unpin:
22317 +       au_unpin(&a->pin);
22318 +       dput(wh_dentry);
22319 +       dput(a->h_path.dentry);
22320 +out_parent:
22321 +       di_write_unlock(parent);
22322 +out_unlock:
22323 +       aufs_read_unlock(dentry, AuLock_DW);
22324 +out_free:
22325 +       au_kfree_rcu(a);
22326 +out:
22327 +       return err;
22328 +}
22329 +
22330 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22331 +{
22332 +       int err, rmdir_later;
22333 +       aufs_bindex_t bwh, bindex, btop;
22334 +       struct inode *inode;
22335 +       struct dentry *parent, *wh_dentry, *h_dentry;
22336 +       struct au_whtmp_rmdir *args;
22337 +       /* to reduce stack size */
22338 +       struct {
22339 +               struct au_dtime dt;
22340 +               struct au_pin pin;
22341 +       } *a;
22342 +
22343 +       IMustLock(dir);
22344 +
22345 +       err = -ENOMEM;
22346 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22347 +       if (unlikely(!a))
22348 +               goto out;
22349 +
22350 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22351 +       if (unlikely(err))
22352 +               goto out_free;
22353 +       err = au_alive_dir(dentry);
22354 +       if (unlikely(err))
22355 +               goto out_unlock;
22356 +       inode = d_inode(dentry);
22357 +       IMustLock(inode);
22358 +       err = -ENOTDIR;
22359 +       if (unlikely(!d_is_dir(dentry)))
22360 +               goto out_unlock; /* possible? */
22361 +
22362 +       err = -ENOMEM;
22363 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22364 +       if (unlikely(!args))
22365 +               goto out_unlock;
22366 +
22367 +       parent = dentry->d_parent; /* dir inode is locked */
22368 +       di_write_lock_parent(parent);
22369 +       err = au_test_empty(dentry, &args->whlist);
22370 +       if (unlikely(err))
22371 +               goto out_parent;
22372 +
22373 +       btop = au_dbtop(dentry);
22374 +       bwh = au_dbwh(dentry);
22375 +       bindex = -1;
22376 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22377 +                                       &a->pin);
22378 +       err = PTR_ERR(wh_dentry);
22379 +       if (IS_ERR(wh_dentry))
22380 +               goto out_parent;
22381 +
22382 +       h_dentry = au_h_dptr(dentry, btop);
22383 +       dget(h_dentry);
22384 +       rmdir_later = 0;
22385 +       if (bindex == btop) {
22386 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22387 +               if (err > 0) {
22388 +                       rmdir_later = err;
22389 +                       err = 0;
22390 +               }
22391 +       } else {
22392 +               /* stop monitoring */
22393 +               au_hn_free(au_hi(inode, btop));
22394 +
22395 +               /* dir inode is locked */
22396 +               IMustLock(d_inode(wh_dentry->d_parent));
22397 +               err = 0;
22398 +       }
22399 +
22400 +       if (!err) {
22401 +               vfsub_dead_dir(inode);
22402 +               au_set_dbdiropq(dentry, -1);
22403 +               epilog(dir, dentry, bindex);
22404 +
22405 +               if (rmdir_later) {
22406 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22407 +                       args = NULL;
22408 +               }
22409 +
22410 +               goto out_unpin; /* success */
22411 +       }
22412 +
22413 +       /* revert */
22414 +       AuLabel(revert);
22415 +       if (wh_dentry) {
22416 +               int rerr;
22417 +
22418 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22419 +                                &a->dt);
22420 +               if (rerr)
22421 +                       err = rerr;
22422 +       }
22423 +
22424 +out_unpin:
22425 +       au_unpin(&a->pin);
22426 +       dput(wh_dentry);
22427 +       dput(h_dentry);
22428 +out_parent:
22429 +       di_write_unlock(parent);
22430 +       if (args)
22431 +               au_whtmp_rmdir_free(args);
22432 +out_unlock:
22433 +       aufs_read_unlock(dentry, AuLock_DW);
22434 +out_free:
22435 +       au_kfree_rcu(a);
22436 +out:
22437 +       AuTraceErr(err);
22438 +       return err;
22439 +}
22440 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22441 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22442 +++ linux/fs/aufs/i_op_ren.c    2021-02-24 13:33:42.744347058 +0100
22443 @@ -0,0 +1,1250 @@
22444 +// SPDX-License-Identifier: GPL-2.0
22445 +/*
22446 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22447 + *
22448 + * This program, aufs is free software; you can redistribute it and/or modify
22449 + * it under the terms of the GNU General Public License as published by
22450 + * the Free Software Foundation; either version 2 of the License, or
22451 + * (at your option) any later version.
22452 + *
22453 + * This program is distributed in the hope that it will be useful,
22454 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22455 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22456 + * GNU General Public License for more details.
22457 + *
22458 + * You should have received a copy of the GNU General Public License
22459 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22460 + */
22461 +
22462 +/*
22463 + * inode operation (rename entry)
22464 + * todo: this is crazy monster
22465 + */
22466 +
22467 +#include <linux/iversion.h>
22468 +#include "aufs.h"
22469 +
22470 +enum { AuSRC, AuDST, AuSrcDst };
22471 +enum { AuPARENT, AuCHILD, AuParentChild };
22472 +
22473 +#define AuRen_ISDIR_SRC                1
22474 +#define AuRen_ISDIR_DST                (1 << 1)
22475 +#define AuRen_ISSAMEDIR                (1 << 2)
22476 +#define AuRen_WHSRC            (1 << 3)
22477 +#define AuRen_WHDST            (1 << 4)
22478 +#define AuRen_MNT_WRITE                (1 << 5)
22479 +#define AuRen_DT_DSTDIR                (1 << 6)
22480 +#define AuRen_DIROPQ_SRC       (1 << 7)
22481 +#define AuRen_DIROPQ_DST       (1 << 8)
22482 +#define AuRen_DIRREN           (1 << 9)
22483 +#define AuRen_DROPPED_SRC      (1 << 10)
22484 +#define AuRen_DROPPED_DST      (1 << 11)
22485 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22486 +#define au_fset_ren(flags, name) \
22487 +       do { (flags) |= AuRen_##name; } while (0)
22488 +#define au_fclr_ren(flags, name) \
22489 +       do { (flags) &= ~AuRen_##name; } while (0)
22490 +
22491 +#ifndef CONFIG_AUFS_DIRREN
22492 +#undef AuRen_DIRREN
22493 +#define AuRen_DIRREN           0
22494 +#endif
22495 +
22496 +struct au_ren_args {
22497 +       struct {
22498 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22499 +                       *wh_dentry;
22500 +               struct inode *dir, *inode;
22501 +               struct au_hinode *hdir, *hinode;
22502 +               struct au_dtime dt[AuParentChild];
22503 +               aufs_bindex_t btop, bdiropq;
22504 +       } sd[AuSrcDst];
22505 +
22506 +#define src_dentry     sd[AuSRC].dentry
22507 +#define src_dir                sd[AuSRC].dir
22508 +#define src_inode      sd[AuSRC].inode
22509 +#define src_h_dentry   sd[AuSRC].h_dentry
22510 +#define src_parent     sd[AuSRC].parent
22511 +#define src_h_parent   sd[AuSRC].h_parent
22512 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22513 +#define src_hdir       sd[AuSRC].hdir
22514 +#define src_hinode     sd[AuSRC].hinode
22515 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22516 +#define src_dt         sd[AuSRC].dt
22517 +#define src_btop       sd[AuSRC].btop
22518 +#define src_bdiropq    sd[AuSRC].bdiropq
22519 +
22520 +#define dst_dentry     sd[AuDST].dentry
22521 +#define dst_dir                sd[AuDST].dir
22522 +#define dst_inode      sd[AuDST].inode
22523 +#define dst_h_dentry   sd[AuDST].h_dentry
22524 +#define dst_parent     sd[AuDST].parent
22525 +#define dst_h_parent   sd[AuDST].h_parent
22526 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22527 +#define dst_hdir       sd[AuDST].hdir
22528 +#define dst_hinode     sd[AuDST].hinode
22529 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22530 +#define dst_dt         sd[AuDST].dt
22531 +#define dst_btop       sd[AuDST].btop
22532 +#define dst_bdiropq    sd[AuDST].bdiropq
22533 +
22534 +       struct dentry *h_trap;
22535 +       struct au_branch *br;
22536 +       struct path h_path;
22537 +       struct au_nhash whlist;
22538 +       aufs_bindex_t btgt, src_bwh;
22539 +
22540 +       struct {
22541 +               unsigned short auren_flags;
22542 +               unsigned char flags;    /* syscall parameter */
22543 +               unsigned char exchange;
22544 +       } __packed;
22545 +
22546 +       struct au_whtmp_rmdir *thargs;
22547 +       struct dentry *h_dst;
22548 +       struct au_hinode *h_root;
22549 +};
22550 +
22551 +/* ---------------------------------------------------------------------- */
22552 +
22553 +/*
22554 + * functions for reverting.
22555 + * when an error happened in a single rename systemcall, we should revert
22556 + * everything as if nothing happened.
22557 + * we don't need to revert the copied-up/down the parent dir since they are
22558 + * harmless.
22559 + */
22560 +
22561 +#define RevertFailure(fmt, ...) do { \
22562 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22563 +               ##__VA_ARGS__, err, rerr); \
22564 +       err = -EIO; \
22565 +} while (0)
22566 +
22567 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22568 +{
22569 +       int rerr;
22570 +       struct dentry *d;
22571 +#define src_or_dst(member) a->sd[idx].member
22572 +
22573 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22574 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22575 +       rerr = au_diropq_remove(d, a->btgt);
22576 +       au_hn_inode_unlock(src_or_dst(hinode));
22577 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22578 +       if (rerr)
22579 +               RevertFailure("remove diropq %pd", d);
22580 +
22581 +#undef src_or_dst_
22582 +}
22583 +
22584 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22585 +{
22586 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22587 +               au_ren_do_rev_diropq(err, a, AuSRC);
22588 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22589 +               au_ren_do_rev_diropq(err, a, AuDST);
22590 +}
22591 +
22592 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22593 +{
22594 +       int rerr;
22595 +       struct inode *delegated;
22596 +
22597 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22598 +                                         a->src_h_parent);
22599 +       rerr = PTR_ERR(a->h_path.dentry);
22600 +       if (IS_ERR(a->h_path.dentry)) {
22601 +               RevertFailure("lkup one %pd", a->src_dentry);
22602 +               return;
22603 +       }
22604 +
22605 +       delegated = NULL;
22606 +       rerr = vfsub_rename(a->dst_h_dir,
22607 +                           au_h_dptr(a->src_dentry, a->btgt),
22608 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22609 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22610 +               pr_warn("cannot retry for NFSv4 delegation"
22611 +                       " for an internal rename\n");
22612 +               iput(delegated);
22613 +       }
22614 +       d_drop(a->h_path.dentry);
22615 +       dput(a->h_path.dentry);
22616 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22617 +       if (rerr)
22618 +               RevertFailure("rename %pd", a->src_dentry);
22619 +}
22620 +
22621 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22622 +{
22623 +       int rerr;
22624 +       struct inode *delegated;
22625 +
22626 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22627 +                                         a->dst_h_parent);
22628 +       rerr = PTR_ERR(a->h_path.dentry);
22629 +       if (IS_ERR(a->h_path.dentry)) {
22630 +               RevertFailure("lkup one %pd", a->dst_dentry);
22631 +               return;
22632 +       }
22633 +       if (d_is_positive(a->h_path.dentry)) {
22634 +               d_drop(a->h_path.dentry);
22635 +               dput(a->h_path.dentry);
22636 +               return;
22637 +       }
22638 +
22639 +       delegated = NULL;
22640 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22641 +                           &delegated, a->flags);
22642 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22643 +               pr_warn("cannot retry for NFSv4 delegation"
22644 +                       " for an internal rename\n");
22645 +               iput(delegated);
22646 +       }
22647 +       d_drop(a->h_path.dentry);
22648 +       dput(a->h_path.dentry);
22649 +       if (!rerr)
22650 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22651 +       else
22652 +               RevertFailure("rename %pd", a->h_dst);
22653 +}
22654 +
22655 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22656 +{
22657 +       int rerr;
22658 +
22659 +       a->h_path.dentry = a->src_wh_dentry;
22660 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22661 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22662 +       if (rerr)
22663 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22664 +}
22665 +#undef RevertFailure
22666 +
22667 +/* ---------------------------------------------------------------------- */
22668 +
22669 +/*
22670 + * when we have to copyup the renaming entry, do it with the rename-target name
22671 + * in order to minimize the cost (the later actual rename is unnecessary).
22672 + * otherwise rename it on the target branch.
22673 + */
22674 +static int au_ren_or_cpup(struct au_ren_args *a)
22675 +{
22676 +       int err;
22677 +       struct dentry *d;
22678 +       struct inode *delegated;
22679 +
22680 +       d = a->src_dentry;
22681 +       if (au_dbtop(d) == a->btgt) {
22682 +               a->h_path.dentry = a->dst_h_dentry;
22683 +               AuDebugOn(au_dbtop(d) != a->btgt);
22684 +               delegated = NULL;
22685 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22686 +                                  a->dst_h_dir, &a->h_path, &delegated,
22687 +                                  a->flags);
22688 +               if (unlikely(err == -EWOULDBLOCK)) {
22689 +                       pr_warn("cannot retry for NFSv4 delegation"
22690 +                               " for an internal rename\n");
22691 +                       iput(delegated);
22692 +               }
22693 +       } else
22694 +               BUG();
22695 +
22696 +       if (!err && a->h_dst)
22697 +               /* it will be set to dinfo later */
22698 +               dget(a->h_dst);
22699 +
22700 +       return err;
22701 +}
22702 +
22703 +/* cf. aufs_rmdir() */
22704 +static int au_ren_del_whtmp(struct au_ren_args *a)
22705 +{
22706 +       int err;
22707 +       struct inode *dir;
22708 +
22709 +       dir = a->dst_dir;
22710 +       SiMustAnyLock(dir->i_sb);
22711 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22712 +                                    au_sbi(dir->i_sb)->si_dirwh)
22713 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22714 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22715 +               if (unlikely(err))
22716 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22717 +                               "ignored.\n", a->h_dst, err);
22718 +       } else {
22719 +               au_nhash_wh_free(&a->thargs->whlist);
22720 +               a->thargs->whlist = a->whlist;
22721 +               a->whlist.nh_num = 0;
22722 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22723 +               dput(a->h_dst);
22724 +               a->thargs = NULL;
22725 +       }
22726 +
22727 +       return 0;
22728 +}
22729 +
22730 +/* make it 'opaque' dir. */
22731 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22732 +{
22733 +       int err;
22734 +       struct dentry *d, *diropq;
22735 +#define src_or_dst(member) a->sd[idx].member
22736 +
22737 +       err = 0;
22738 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22739 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22740 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22741 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22742 +       diropq = au_diropq_create(d, a->btgt);
22743 +       au_hn_inode_unlock(src_or_dst(hinode));
22744 +       if (IS_ERR(diropq))
22745 +               err = PTR_ERR(diropq);
22746 +       else
22747 +               dput(diropq);
22748 +
22749 +#undef src_or_dst_
22750 +       return err;
22751 +}
22752 +
22753 +static int au_ren_diropq(struct au_ren_args *a)
22754 +{
22755 +       int err;
22756 +       unsigned char always;
22757 +       struct dentry *d;
22758 +
22759 +       err = 0;
22760 +       d = a->dst_dentry; /* already renamed on the branch */
22761 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22762 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22763 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22764 +           && a->btgt != au_dbdiropq(a->src_dentry)
22765 +           && (a->dst_wh_dentry
22766 +               || a->btgt <= au_dbdiropq(d)
22767 +               /* hide the lower to keep xino */
22768 +               /* the lowers may not be a dir, but we hide them anyway */
22769 +               || a->btgt < au_dbbot(d)
22770 +               || always)) {
22771 +               AuDbg("here\n");
22772 +               err = au_ren_do_diropq(a, AuSRC);
22773 +               if (unlikely(err))
22774 +                       goto out;
22775 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22776 +       }
22777 +       if (!a->exchange)
22778 +               goto out; /* success */
22779 +
22780 +       d = a->src_dentry; /* already renamed on the branch */
22781 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22782 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22783 +           && (a->btgt < au_dbdiropq(d)
22784 +               || a->btgt < au_dbbot(d)
22785 +               || always)) {
22786 +               AuDbgDentry(a->src_dentry);
22787 +               AuDbgDentry(a->dst_dentry);
22788 +               err = au_ren_do_diropq(a, AuDST);
22789 +               if (unlikely(err))
22790 +                       goto out_rev_src;
22791 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22792 +       }
22793 +       goto out; /* success */
22794 +
22795 +out_rev_src:
22796 +       AuDbg("err %d, reverting src\n", err);
22797 +       au_ren_rev_diropq(err, a);
22798 +out:
22799 +       return err;
22800 +}
22801 +
22802 +static int do_rename(struct au_ren_args *a)
22803 +{
22804 +       int err;
22805 +       struct dentry *d, *h_d;
22806 +
22807 +       if (!a->exchange) {
22808 +               /* prepare workqueue args for asynchronous rmdir */
22809 +               h_d = a->dst_h_dentry;
22810 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22811 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22812 +                   && d_is_positive(h_d)) {
22813 +                       err = -ENOMEM;
22814 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22815 +                                                        GFP_NOFS);
22816 +                       if (unlikely(!a->thargs))
22817 +                               goto out;
22818 +                       a->h_dst = dget(h_d);
22819 +               }
22820 +
22821 +               /* create whiteout for src_dentry */
22822 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22823 +                       a->src_bwh = au_dbwh(a->src_dentry);
22824 +                       AuDebugOn(a->src_bwh >= 0);
22825 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22826 +                                                       a->src_h_parent);
22827 +                       err = PTR_ERR(a->src_wh_dentry);
22828 +                       if (IS_ERR(a->src_wh_dentry))
22829 +                               goto out_thargs;
22830 +               }
22831 +
22832 +               /* lookup whiteout for dentry */
22833 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22834 +                       h_d = au_wh_lkup(a->dst_h_parent,
22835 +                                        &a->dst_dentry->d_name, a->br);
22836 +                       err = PTR_ERR(h_d);
22837 +                       if (IS_ERR(h_d))
22838 +                               goto out_whsrc;
22839 +                       if (d_is_negative(h_d))
22840 +                               dput(h_d);
22841 +                       else
22842 +                               a->dst_wh_dentry = h_d;
22843 +               }
22844 +
22845 +               /* rename dentry to tmpwh */
22846 +               if (a->thargs) {
22847 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22848 +                       if (unlikely(err))
22849 +                               goto out_whdst;
22850 +
22851 +                       d = a->dst_dentry;
22852 +                       au_set_h_dptr(d, a->btgt, NULL);
22853 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22854 +                       if (unlikely(err))
22855 +                               goto out_whtmp;
22856 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22857 +               }
22858 +       }
22859 +
22860 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22861 +#if 0 /* debugging */
22862 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22863 +              && d_is_positive(a->dst_h_dentry)
22864 +              && a->src_btop != a->btgt);
22865 +#endif
22866 +
22867 +       /* rename by vfs_rename or cpup */
22868 +       err = au_ren_or_cpup(a);
22869 +       if (unlikely(err))
22870 +               /* leave the copied-up one */
22871 +               goto out_whtmp;
22872 +
22873 +       /* make dir opaque */
22874 +       err = au_ren_diropq(a);
22875 +       if (unlikely(err))
22876 +               goto out_rename;
22877 +
22878 +       /* update target timestamps */
22879 +       if (a->exchange) {
22880 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22881 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22882 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22883 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22884 +       }
22885 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22886 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22887 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22888 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22889 +
22890 +       if (!a->exchange) {
22891 +               /* remove whiteout for dentry */
22892 +               if (a->dst_wh_dentry) {
22893 +                       a->h_path.dentry = a->dst_wh_dentry;
22894 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22895 +                                                 a->dst_dentry);
22896 +                       if (unlikely(err))
22897 +                               goto out_diropq;
22898 +               }
22899 +
22900 +               /* remove whtmp */
22901 +               if (a->thargs)
22902 +                       au_ren_del_whtmp(a); /* ignore this error */
22903 +
22904 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22905 +       }
22906 +       err = 0;
22907 +       goto out_success;
22908 +
22909 +out_diropq:
22910 +       au_ren_rev_diropq(err, a);
22911 +out_rename:
22912 +       au_ren_rev_rename(err, a);
22913 +       dput(a->h_dst);
22914 +out_whtmp:
22915 +       if (a->thargs)
22916 +               au_ren_rev_whtmp(err, a);
22917 +out_whdst:
22918 +       dput(a->dst_wh_dentry);
22919 +       a->dst_wh_dentry = NULL;
22920 +out_whsrc:
22921 +       if (a->src_wh_dentry)
22922 +               au_ren_rev_whsrc(err, a);
22923 +out_success:
22924 +       dput(a->src_wh_dentry);
22925 +       dput(a->dst_wh_dentry);
22926 +out_thargs:
22927 +       if (a->thargs) {
22928 +               dput(a->h_dst);
22929 +               au_whtmp_rmdir_free(a->thargs);
22930 +               a->thargs = NULL;
22931 +       }
22932 +out:
22933 +       return err;
22934 +}
22935 +
22936 +/* ---------------------------------------------------------------------- */
22937 +
22938 +/*
22939 + * test if @dentry dir can be rename destination or not.
22940 + * success means, it is a logically empty dir.
22941 + */
22942 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
22943 +{
22944 +       return au_test_empty(dentry, whlist);
22945 +}
22946 +
22947 +/*
22948 + * test if @a->src_dentry dir can be rename source or not.
22949 + * if it can, return 0.
22950 + * success means,
22951 + * - it is a logically empty dir.
22952 + * - or, it exists on writable branch and has no children including whiteouts
22953 + *   on the lower branch unless DIRREN is on.
22954 + */
22955 +static int may_rename_srcdir(struct au_ren_args *a)
22956 +{
22957 +       int err;
22958 +       unsigned int rdhash;
22959 +       aufs_bindex_t btop, btgt;
22960 +       struct dentry *dentry;
22961 +       struct super_block *sb;
22962 +       struct au_sbinfo *sbinfo;
22963 +
22964 +       dentry = a->src_dentry;
22965 +       sb = dentry->d_sb;
22966 +       sbinfo = au_sbi(sb);
22967 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22968 +               au_fset_ren(a->auren_flags, DIRREN);
22969 +
22970 +       btgt = a->btgt;
22971 +       btop = au_dbtop(dentry);
22972 +       if (btop != btgt) {
22973 +               struct au_nhash whlist;
22974 +
22975 +               SiMustAnyLock(sb);
22976 +               rdhash = sbinfo->si_rdhash;
22977 +               if (!rdhash)
22978 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
22979 +                                                          dentry));
22980 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
22981 +               if (unlikely(err))
22982 +                       goto out;
22983 +               err = au_test_empty(dentry, &whlist);
22984 +               au_nhash_wh_free(&whlist);
22985 +               goto out;
22986 +       }
22987 +
22988 +       if (btop == au_dbtaildir(dentry))
22989 +               return 0; /* success */
22990 +
22991 +       err = au_test_empty_lower(dentry);
22992 +
22993 +out:
22994 +       if (err == -ENOTEMPTY) {
22995 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
22996 +                       err = 0;
22997 +               } else {
22998 +                       AuWarn1("renaming dir who has child(ren) on multiple "
22999 +                               "branches, is not supported\n");
23000 +                       err = -EXDEV;
23001 +               }
23002 +       }
23003 +       return err;
23004 +}
23005 +
23006 +/* side effect: sets whlist and h_dentry */
23007 +static int au_ren_may_dir(struct au_ren_args *a)
23008 +{
23009 +       int err;
23010 +       unsigned int rdhash;
23011 +       struct dentry *d;
23012 +
23013 +       d = a->dst_dentry;
23014 +       SiMustAnyLock(d->d_sb);
23015 +
23016 +       err = 0;
23017 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23018 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23019 +               if (!rdhash)
23020 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23021 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23022 +               if (unlikely(err))
23023 +                       goto out;
23024 +
23025 +               if (!a->exchange) {
23026 +                       au_set_dbtop(d, a->dst_btop);
23027 +                       err = may_rename_dstdir(d, &a->whlist);
23028 +                       au_set_dbtop(d, a->btgt);
23029 +               } else
23030 +                       err = may_rename_srcdir(a);
23031 +       }
23032 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23033 +       if (unlikely(err))
23034 +               goto out;
23035 +
23036 +       d = a->src_dentry;
23037 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23038 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23039 +               err = may_rename_srcdir(a);
23040 +               if (unlikely(err)) {
23041 +                       au_nhash_wh_free(&a->whlist);
23042 +                       a->whlist.nh_num = 0;
23043 +               }
23044 +       }
23045 +out:
23046 +       return err;
23047 +}
23048 +
23049 +/* ---------------------------------------------------------------------- */
23050 +
23051 +/*
23052 + * simple tests for rename.
23053 + * following the checks in vfs, plus the parent-child relationship.
23054 + */
23055 +static int au_may_ren(struct au_ren_args *a)
23056 +{
23057 +       int err, isdir;
23058 +       struct inode *h_inode;
23059 +
23060 +       if (a->src_btop == a->btgt) {
23061 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23062 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23063 +               if (unlikely(err))
23064 +                       goto out;
23065 +               err = -EINVAL;
23066 +               if (unlikely(a->src_h_dentry == a->h_trap))
23067 +                       goto out;
23068 +       }
23069 +
23070 +       err = 0;
23071 +       if (a->dst_btop != a->btgt)
23072 +               goto out;
23073 +
23074 +       err = -ENOTEMPTY;
23075 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23076 +               goto out;
23077 +
23078 +       err = -EIO;
23079 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23080 +       if (d_really_is_negative(a->dst_dentry)) {
23081 +               if (d_is_negative(a->dst_h_dentry))
23082 +                       err = au_may_add(a->dst_dentry, a->btgt,
23083 +                                        a->dst_h_parent, isdir);
23084 +       } else {
23085 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23086 +                       goto out;
23087 +               h_inode = d_inode(a->dst_h_dentry);
23088 +               if (h_inode->i_nlink)
23089 +                       err = au_may_del(a->dst_dentry, a->btgt,
23090 +                                        a->dst_h_parent, isdir);
23091 +       }
23092 +
23093 +out:
23094 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23095 +               err = -EIO;
23096 +       AuTraceErr(err);
23097 +       return err;
23098 +}
23099 +
23100 +/* ---------------------------------------------------------------------- */
23101 +
23102 +/*
23103 + * locking order
23104 + * (VFS)
23105 + * - src_dir and dir by lock_rename()
23106 + * - inode if exists
23107 + * (aufs)
23108 + * - lock all
23109 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23110 + *     + si_read_lock
23111 + *     + di_write_lock2_child()
23112 + *       + di_write_lock_child()
23113 + *        + ii_write_lock_child()
23114 + *       + di_write_lock_child2()
23115 + *        + ii_write_lock_child2()
23116 + *     + src_parent and parent
23117 + *       + di_write_lock_parent()
23118 + *        + ii_write_lock_parent()
23119 + *       + di_write_lock_parent2()
23120 + *        + ii_write_lock_parent2()
23121 + *   + lower src_dir and dir by vfsub_lock_rename()
23122 + *   + verify the every relationships between child and parent. if any
23123 + *     of them failed, unlock all and return -EBUSY.
23124 + */
23125 +static void au_ren_unlock(struct au_ren_args *a)
23126 +{
23127 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23128 +                           a->dst_h_parent, a->dst_hdir);
23129 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23130 +           && a->h_root)
23131 +               au_hn_inode_unlock(a->h_root);
23132 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23133 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23134 +}
23135 +
23136 +static int au_ren_lock(struct au_ren_args *a)
23137 +{
23138 +       int err;
23139 +       unsigned int udba;
23140 +
23141 +       err = 0;
23142 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23143 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23144 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23145 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23146 +
23147 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23148 +       if (unlikely(err))
23149 +               goto out;
23150 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23151 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23152 +               struct dentry *root;
23153 +               struct inode *dir;
23154 +
23155 +               /*
23156 +                * sbinfo is already locked, so this ii_read_lock is
23157 +                * unnecessary. but our debugging feature checks it.
23158 +                */
23159 +               root = a->src_inode->i_sb->s_root;
23160 +               if (root != a->src_parent && root != a->dst_parent) {
23161 +                       dir = d_inode(root);
23162 +                       ii_read_lock_parent3(dir);
23163 +                       a->h_root = au_hi(dir, a->btgt);
23164 +                       ii_read_unlock(dir);
23165 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23166 +               }
23167 +       }
23168 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23169 +                                     a->dst_h_parent, a->dst_hdir);
23170 +       udba = au_opt_udba(a->src_dentry->d_sb);
23171 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23172 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23173 +               err = au_busy_or_stale();
23174 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23175 +               err = au_h_verify(a->src_h_dentry, udba,
23176 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23177 +                                 a->br);
23178 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23179 +               err = au_h_verify(a->dst_h_dentry, udba,
23180 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23181 +                                 a->br);
23182 +       if (!err)
23183 +               goto out; /* success */
23184 +
23185 +       err = au_busy_or_stale();
23186 +       au_ren_unlock(a);
23187 +
23188 +out:
23189 +       return err;
23190 +}
23191 +
23192 +/* ---------------------------------------------------------------------- */
23193 +
23194 +static void au_ren_refresh_dir(struct au_ren_args *a)
23195 +{
23196 +       struct inode *dir;
23197 +
23198 +       dir = a->dst_dir;
23199 +       inode_inc_iversion(dir);
23200 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23201 +               /* is this updating defined in POSIX? */
23202 +               au_cpup_attr_timesizes(a->src_inode);
23203 +               au_cpup_attr_nlink(dir, /*force*/1);
23204 +       }
23205 +       au_dir_ts(dir, a->btgt);
23206 +
23207 +       if (a->exchange) {
23208 +               dir = a->src_dir;
23209 +               inode_inc_iversion(dir);
23210 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23211 +                       /* is this updating defined in POSIX? */
23212 +                       au_cpup_attr_timesizes(a->dst_inode);
23213 +                       au_cpup_attr_nlink(dir, /*force*/1);
23214 +               }
23215 +               au_dir_ts(dir, a->btgt);
23216 +       }
23217 +
23218 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23219 +               return;
23220 +
23221 +       dir = a->src_dir;
23222 +       inode_inc_iversion(dir);
23223 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23224 +               au_cpup_attr_nlink(dir, /*force*/1);
23225 +       au_dir_ts(dir, a->btgt);
23226 +}
23227 +
23228 +static void au_ren_refresh(struct au_ren_args *a)
23229 +{
23230 +       aufs_bindex_t bbot, bindex;
23231 +       struct dentry *d, *h_d;
23232 +       struct inode *i, *h_i;
23233 +       struct super_block *sb;
23234 +
23235 +       d = a->dst_dentry;
23236 +       d_drop(d);
23237 +       if (a->h_dst)
23238 +               /* already dget-ed by au_ren_or_cpup() */
23239 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23240 +
23241 +       i = a->dst_inode;
23242 +       if (i) {
23243 +               if (!a->exchange) {
23244 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23245 +                               vfsub_drop_nlink(i);
23246 +                       else {
23247 +                               vfsub_dead_dir(i);
23248 +                               au_cpup_attr_timesizes(i);
23249 +                       }
23250 +                       au_update_dbrange(d, /*do_put_zero*/1);
23251 +               } else
23252 +                       au_cpup_attr_nlink(i, /*force*/1);
23253 +       } else {
23254 +               bbot = a->btgt;
23255 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23256 +                       au_set_h_dptr(d, bindex, NULL);
23257 +               bbot = au_dbbot(d);
23258 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23259 +                       au_set_h_dptr(d, bindex, NULL);
23260 +               au_update_dbrange(d, /*do_put_zero*/0);
23261 +       }
23262 +
23263 +       if (a->exchange
23264 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23265 +               d_drop(a->src_dentry);
23266 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23267 +                       au_set_dbwh(a->src_dentry, -1);
23268 +               return;
23269 +       }
23270 +
23271 +       d = a->src_dentry;
23272 +       au_set_dbwh(d, -1);
23273 +       bbot = au_dbbot(d);
23274 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23275 +               h_d = au_h_dptr(d, bindex);
23276 +               if (h_d)
23277 +                       au_set_h_dptr(d, bindex, NULL);
23278 +       }
23279 +       au_set_dbbot(d, a->btgt);
23280 +
23281 +       sb = d->d_sb;
23282 +       i = a->src_inode;
23283 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23284 +               return; /* success */
23285 +
23286 +       bbot = au_ibbot(i);
23287 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23288 +               h_i = au_h_iptr(i, bindex);
23289 +               if (h_i) {
23290 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23291 +                       /* ignore this error */
23292 +                       au_set_h_iptr(i, bindex, NULL, 0);
23293 +               }
23294 +       }
23295 +       au_set_ibbot(i, a->btgt);
23296 +}
23297 +
23298 +/* ---------------------------------------------------------------------- */
23299 +
23300 +/* mainly for link(2) and rename(2) */
23301 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23302 +{
23303 +       aufs_bindex_t bdiropq, bwh;
23304 +       struct dentry *parent;
23305 +       struct au_branch *br;
23306 +
23307 +       parent = dentry->d_parent;
23308 +       IMustLock(d_inode(parent)); /* dir is locked */
23309 +
23310 +       bdiropq = au_dbdiropq(parent);
23311 +       bwh = au_dbwh(dentry);
23312 +       br = au_sbr(dentry->d_sb, btgt);
23313 +       if (au_br_rdonly(br)
23314 +           || (0 <= bdiropq && bdiropq < btgt)
23315 +           || (0 <= bwh && bwh < btgt))
23316 +               btgt = -1;
23317 +
23318 +       AuDbg("btgt %d\n", btgt);
23319 +       return btgt;
23320 +}
23321 +
23322 +/* sets src_btop, dst_btop and btgt */
23323 +static int au_ren_wbr(struct au_ren_args *a)
23324 +{
23325 +       int err;
23326 +       struct au_wr_dir_args wr_dir_args = {
23327 +               /* .force_btgt  = -1, */
23328 +               .flags          = AuWrDir_ADD_ENTRY
23329 +       };
23330 +
23331 +       a->src_btop = au_dbtop(a->src_dentry);
23332 +       a->dst_btop = au_dbtop(a->dst_dentry);
23333 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23334 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23335 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23336 +       wr_dir_args.force_btgt = a->src_btop;
23337 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23338 +               wr_dir_args.force_btgt = a->dst_btop;
23339 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23340 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23341 +       a->btgt = err;
23342 +       if (a->exchange)
23343 +               au_update_dbtop(a->dst_dentry);
23344 +
23345 +       return err;
23346 +}
23347 +
23348 +static void au_ren_dt(struct au_ren_args *a)
23349 +{
23350 +       a->h_path.dentry = a->src_h_parent;
23351 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23352 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23353 +               a->h_path.dentry = a->dst_h_parent;
23354 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23355 +       }
23356 +
23357 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23358 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23359 +           && !a->exchange)
23360 +               return;
23361 +
23362 +       a->h_path.dentry = a->src_h_dentry;
23363 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23364 +       if (d_is_positive(a->dst_h_dentry)) {
23365 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23366 +               a->h_path.dentry = a->dst_h_dentry;
23367 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23368 +       }
23369 +}
23370 +
23371 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23372 +{
23373 +       struct dentry *h_d;
23374 +       struct inode *h_inode;
23375 +
23376 +       au_dtime_revert(a->src_dt + AuPARENT);
23377 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23378 +               au_dtime_revert(a->dst_dt + AuPARENT);
23379 +
23380 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23381 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23382 +               h_inode = d_inode(h_d);
23383 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23384 +               au_dtime_revert(a->src_dt + AuCHILD);
23385 +               inode_unlock(h_inode);
23386 +
23387 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23388 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23389 +                       h_inode = d_inode(h_d);
23390 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23391 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23392 +                       inode_unlock(h_inode);
23393 +               }
23394 +       }
23395 +}
23396 +
23397 +/* ---------------------------------------------------------------------- */
23398 +
23399 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23400 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23401 +               unsigned int _flags)
23402 +{
23403 +       int err, lock_flags;
23404 +       void *rev;
23405 +       /* reduce stack space */
23406 +       struct au_ren_args *a;
23407 +       struct au_pin pin;
23408 +
23409 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23410 +       IMustLock(_src_dir);
23411 +       IMustLock(_dst_dir);
23412 +
23413 +       err = -EINVAL;
23414 +       if (unlikely(_flags & RENAME_WHITEOUT))
23415 +               goto out;
23416 +
23417 +       err = -ENOMEM;
23418 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23419 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23420 +       if (unlikely(!a))
23421 +               goto out;
23422 +
23423 +       a->flags = _flags;
23424 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23425 +                    && RENAME_EXCHANGE > U8_MAX);
23426 +       a->exchange = _flags & RENAME_EXCHANGE;
23427 +       a->src_dir = _src_dir;
23428 +       a->src_dentry = _src_dentry;
23429 +       a->src_inode = NULL;
23430 +       if (d_really_is_positive(a->src_dentry))
23431 +               a->src_inode = d_inode(a->src_dentry);
23432 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23433 +       a->dst_dir = _dst_dir;
23434 +       a->dst_dentry = _dst_dentry;
23435 +       a->dst_inode = NULL;
23436 +       if (d_really_is_positive(a->dst_dentry))
23437 +               a->dst_inode = d_inode(a->dst_dentry);
23438 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23439 +       if (a->dst_inode) {
23440 +               /*
23441 +                * if EXCHANGE && src is non-dir && dst is dir,
23442 +                * dst is not locked.
23443 +                */
23444 +               /* IMustLock(a->dst_inode); */
23445 +               au_igrab(a->dst_inode);
23446 +       }
23447 +
23448 +       err = -ENOTDIR;
23449 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23450 +       if (d_is_dir(a->src_dentry)) {
23451 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23452 +               if (unlikely(!a->exchange
23453 +                            && d_really_is_positive(a->dst_dentry)
23454 +                            && !d_is_dir(a->dst_dentry)))
23455 +                       goto out_free;
23456 +               lock_flags |= AuLock_DIRS;
23457 +       }
23458 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23459 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23460 +               if (unlikely(!a->exchange
23461 +                            && d_really_is_positive(a->src_dentry)
23462 +                            && !d_is_dir(a->src_dentry)))
23463 +                       goto out_free;
23464 +               lock_flags |= AuLock_DIRS;
23465 +       }
23466 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23467 +                                       lock_flags);
23468 +       if (unlikely(err))
23469 +               goto out_free;
23470 +
23471 +       err = au_d_hashed_positive(a->src_dentry);
23472 +       if (unlikely(err))
23473 +               goto out_unlock;
23474 +       err = -ENOENT;
23475 +       if (a->dst_inode) {
23476 +               /*
23477 +                * If it is a dir, VFS unhash it before this
23478 +                * function. It means we cannot rely upon d_unhashed().
23479 +                */
23480 +               if (unlikely(!a->dst_inode->i_nlink))
23481 +                       goto out_unlock;
23482 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23483 +                       err = au_d_hashed_positive(a->dst_dentry);
23484 +                       if (unlikely(err && !a->exchange))
23485 +                               goto out_unlock;
23486 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23487 +                       goto out_unlock;
23488 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23489 +               goto out_unlock;
23490 +
23491 +       /*
23492 +        * is it possible?
23493 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23494 +        * there may exist a problem somewhere else.
23495 +        */
23496 +       err = -EINVAL;
23497 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23498 +               goto out_unlock;
23499 +
23500 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23501 +       di_write_lock_parent(a->dst_parent);
23502 +
23503 +       /* which branch we process */
23504 +       err = au_ren_wbr(a);
23505 +       if (unlikely(err < 0))
23506 +               goto out_parent;
23507 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23508 +       a->h_path.mnt = au_br_mnt(a->br);
23509 +
23510 +       /* are they available to be renamed */
23511 +       err = au_ren_may_dir(a);
23512 +       if (unlikely(err))
23513 +               goto out_children;
23514 +
23515 +       /* prepare the writable parent dir on the same branch */
23516 +       if (a->dst_btop == a->btgt) {
23517 +               au_fset_ren(a->auren_flags, WHDST);
23518 +       } else {
23519 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23520 +               if (unlikely(err))
23521 +                       goto out_children;
23522 +       }
23523 +
23524 +       err = 0;
23525 +       if (!a->exchange) {
23526 +               if (a->src_dir != a->dst_dir) {
23527 +                       /*
23528 +                        * this temporary unlock is safe,
23529 +                        * because both dir->i_mutex are locked.
23530 +                        */
23531 +                       di_write_unlock(a->dst_parent);
23532 +                       di_write_lock_parent(a->src_parent);
23533 +                       err = au_wr_dir_need_wh(a->src_dentry,
23534 +                                               au_ftest_ren(a->auren_flags,
23535 +                                                            ISDIR_SRC),
23536 +                                               &a->btgt);
23537 +                       di_write_unlock(a->src_parent);
23538 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23539 +                                             /*isdir*/1);
23540 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23541 +               } else
23542 +                       err = au_wr_dir_need_wh(a->src_dentry,
23543 +                                               au_ftest_ren(a->auren_flags,
23544 +                                                            ISDIR_SRC),
23545 +                                               &a->btgt);
23546 +       }
23547 +       if (unlikely(err < 0))
23548 +               goto out_children;
23549 +       if (err)
23550 +               au_fset_ren(a->auren_flags, WHSRC);
23551 +
23552 +       /* cpup src */
23553 +       if (a->src_btop != a->btgt) {
23554 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23555 +                            au_opt_udba(a->src_dentry->d_sb),
23556 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23557 +               if (!err) {
23558 +                       struct au_cp_generic cpg = {
23559 +                               .dentry = a->src_dentry,
23560 +                               .bdst   = a->btgt,
23561 +                               .bsrc   = a->src_btop,
23562 +                               .len    = -1,
23563 +                               .pin    = &pin,
23564 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23565 +                       };
23566 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23567 +                       err = au_sio_cpup_simple(&cpg);
23568 +                       au_unpin(&pin);
23569 +               }
23570 +               if (unlikely(err))
23571 +                       goto out_children;
23572 +               a->src_btop = a->btgt;
23573 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23574 +               if (!a->exchange)
23575 +                       au_fset_ren(a->auren_flags, WHSRC);
23576 +       }
23577 +
23578 +       /* cpup dst */
23579 +       if (a->exchange && a->dst_inode
23580 +           && a->dst_btop != a->btgt) {
23581 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23582 +                            au_opt_udba(a->dst_dentry->d_sb),
23583 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23584 +               if (!err) {
23585 +                       struct au_cp_generic cpg = {
23586 +                               .dentry = a->dst_dentry,
23587 +                               .bdst   = a->btgt,
23588 +                               .bsrc   = a->dst_btop,
23589 +                               .len    = -1,
23590 +                               .pin    = &pin,
23591 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23592 +                       };
23593 +                       err = au_sio_cpup_simple(&cpg);
23594 +                       au_unpin(&pin);
23595 +               }
23596 +               if (unlikely(err))
23597 +                       goto out_children;
23598 +               a->dst_btop = a->btgt;
23599 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23600 +       }
23601 +
23602 +       /* lock them all */
23603 +       err = au_ren_lock(a);
23604 +       if (unlikely(err))
23605 +               /* leave the copied-up one */
23606 +               goto out_children;
23607 +
23608 +       if (!a->exchange) {
23609 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23610 +                       err = au_may_ren(a);
23611 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23612 +                       err = -ENAMETOOLONG;
23613 +               if (unlikely(err))
23614 +                       goto out_hdir;
23615 +       }
23616 +
23617 +       /* store timestamps to be revertible */
23618 +       au_ren_dt(a);
23619 +
23620 +       /* store dirren info */
23621 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23622 +               err = au_dr_rename(a->src_dentry, a->btgt,
23623 +                                  &a->dst_dentry->d_name, &rev);
23624 +               AuTraceErr(err);
23625 +               if (unlikely(err))
23626 +                       goto out_dt;
23627 +       }
23628 +
23629 +       /* here we go */
23630 +       err = do_rename(a);
23631 +       if (unlikely(err))
23632 +               goto out_dirren;
23633 +
23634 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23635 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23636 +
23637 +       /* update dir attributes */
23638 +       au_ren_refresh_dir(a);
23639 +
23640 +       /* dput/iput all lower dentries */
23641 +       au_ren_refresh(a);
23642 +
23643 +       goto out_hdir; /* success */
23644 +
23645 +out_dirren:
23646 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23647 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23648 +out_dt:
23649 +       au_ren_rev_dt(err, a);
23650 +out_hdir:
23651 +       au_ren_unlock(a);
23652 +out_children:
23653 +       au_nhash_wh_free(&a->whlist);
23654 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23655 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23656 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23657 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23658 +       }
23659 +out_parent:
23660 +       if (!err) {
23661 +               if (d_unhashed(a->src_dentry))
23662 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23663 +               if (d_unhashed(a->dst_dentry))
23664 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23665 +               if (!a->exchange)
23666 +                       d_move(a->src_dentry, a->dst_dentry);
23667 +               else {
23668 +                       d_exchange(a->src_dentry, a->dst_dentry);
23669 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23670 +                               d_drop(a->dst_dentry);
23671 +               }
23672 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23673 +                       d_drop(a->src_dentry);
23674 +       } else {
23675 +               au_update_dbtop(a->dst_dentry);
23676 +               if (!a->dst_inode)
23677 +                       d_drop(a->dst_dentry);
23678 +       }
23679 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23680 +               di_write_unlock(a->dst_parent);
23681 +       else
23682 +               di_write_unlock2(a->src_parent, a->dst_parent);
23683 +out_unlock:
23684 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23685 +out_free:
23686 +       iput(a->dst_inode);
23687 +       if (a->thargs)
23688 +               au_whtmp_rmdir_free(a->thargs);
23689 +       au_kfree_rcu(a);
23690 +out:
23691 +       AuTraceErr(err);
23692 +       return err;
23693 +}
23694 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23695 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23696 +++ linux/fs/aufs/Kconfig       2021-02-24 13:33:42.741013619 +0100
23697 @@ -0,0 +1,199 @@
23698 +# SPDX-License-Identifier: GPL-2.0
23699 +config AUFS_FS
23700 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23701 +       help
23702 +       Aufs is a stackable unification filesystem such as Unionfs,
23703 +       which unifies several directories and provides a merged single
23704 +       directory.
23705 +       In the early days, aufs was entirely re-designed and
23706 +       re-implemented Unionfs Version 1.x series. Introducing many
23707 +       original ideas, approaches and improvements, it becomes totally
23708 +       different from Unionfs while keeping the basic features.
23709 +
23710 +if AUFS_FS
23711 +choice
23712 +       prompt "Maximum number of branches"
23713 +       default AUFS_BRANCH_MAX_127
23714 +       help
23715 +       Specifies the maximum number of branches (or member directories)
23716 +       in a single aufs. The larger value consumes more system
23717 +       resources and has a minor impact to performance.
23718 +config AUFS_BRANCH_MAX_127
23719 +       bool "127"
23720 +       help
23721 +       Specifies the maximum number of branches (or member directories)
23722 +       in a single aufs. The larger value consumes more system
23723 +       resources and has a minor impact to performance.
23724 +config AUFS_BRANCH_MAX_511
23725 +       bool "511"
23726 +       help
23727 +       Specifies the maximum number of branches (or member directories)
23728 +       in a single aufs. The larger value consumes more system
23729 +       resources and has a minor impact to performance.
23730 +config AUFS_BRANCH_MAX_1023
23731 +       bool "1023"
23732 +       help
23733 +       Specifies the maximum number of branches (or member directories)
23734 +       in a single aufs. The larger value consumes more system
23735 +       resources and has a minor impact to performance.
23736 +config AUFS_BRANCH_MAX_32767
23737 +       bool "32767"
23738 +       help
23739 +       Specifies the maximum number of branches (or member directories)
23740 +       in a single aufs. The larger value consumes more system
23741 +       resources and has a minor impact to performance.
23742 +endchoice
23743 +
23744 +config AUFS_SBILIST
23745 +       bool
23746 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23747 +       default y
23748 +       help
23749 +       Automatic configuration for internal use.
23750 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23751 +
23752 +config AUFS_HNOTIFY
23753 +       bool "Detect direct branch access (bypassing aufs)"
23754 +       help
23755 +       If you want to modify files on branches directly, eg. bypassing aufs,
23756 +       and want aufs to detect the changes of them fully, then enable this
23757 +       option and use 'udba=notify' mount option.
23758 +       Currently there is only one available configuration, "fsnotify".
23759 +       It will have a negative impact to the performance.
23760 +       See detail in aufs.5.
23761 +
23762 +choice
23763 +       prompt "method" if AUFS_HNOTIFY
23764 +       default AUFS_HFSNOTIFY
23765 +config AUFS_HFSNOTIFY
23766 +       bool "fsnotify"
23767 +       select FSNOTIFY
23768 +endchoice
23769 +
23770 +config AUFS_EXPORT
23771 +       bool "NFS-exportable aufs"
23772 +       depends on EXPORTFS
23773 +       help
23774 +       If you want to export your mounted aufs via NFS, then enable this
23775 +       option. There are several requirements for this configuration.
23776 +       See detail in aufs.5.
23777 +
23778 +config AUFS_INO_T_64
23779 +       bool
23780 +       depends on AUFS_EXPORT
23781 +       depends on 64BIT && !(ALPHA || S390)
23782 +       default y
23783 +       help
23784 +       Automatic configuration for internal use.
23785 +       /* typedef unsigned long/int __kernel_ino_t */
23786 +       /* alpha and s390x are int */
23787 +
23788 +config AUFS_XATTR
23789 +       bool "support for XATTR/EA (including Security Labels)"
23790 +       help
23791 +       If your branch fs supports XATTR/EA and you want to make them
23792 +       available in aufs too, then enable this opsion and specify the
23793 +       branch attributes for EA.
23794 +       See detail in aufs.5.
23795 +
23796 +config AUFS_FHSM
23797 +       bool "File-based Hierarchical Storage Management"
23798 +       help
23799 +       Hierarchical Storage Management (or HSM) is a well-known feature
23800 +       in the storage world. Aufs provides this feature as file-based.
23801 +       with multiple branches.
23802 +       These multiple branches are prioritized, ie. the topmost one
23803 +       should be the fastest drive and be used heavily.
23804 +
23805 +config AUFS_RDU
23806 +       bool "Readdir in userspace"
23807 +       help
23808 +       Aufs has two methods to provide a merged view for a directory,
23809 +       by a user-space library and by kernel-space natively. The latter
23810 +       is always enabled but sometimes large and slow.
23811 +       If you enable this option, install the library in aufs2-util
23812 +       package, and set some environment variables for your readdir(3),
23813 +       then the work will be handled in user-space which generally
23814 +       shows better performance in most cases.
23815 +       See detail in aufs.5.
23816 +
23817 +config AUFS_DIRREN
23818 +       bool "Workaround for rename(2)-ing a directory"
23819 +       help
23820 +       By default, aufs returns EXDEV error in renameing a dir who has
23821 +       his child on the lower branch, since it is a bad idea to issue
23822 +       rename(2) internally for every lower branch. But user may not
23823 +       accept this behaviour. So here is a workaround to allow such
23824 +       rename(2) and store some extra infromation on the writable
23825 +       branch. Obviously this costs high (and I don't like it).
23826 +       To use this feature, you need to enable this configuration AND
23827 +       to specify the mount option `dirren.'
23828 +       See details in aufs.5 and the design documents.
23829 +
23830 +config AUFS_SHWH
23831 +       bool "Show whiteouts"
23832 +       help
23833 +       If you want to make the whiteouts in aufs visible, then enable
23834 +       this option and specify 'shwh' mount option. Although it may
23835 +       sounds like philosophy or something, but in technically it
23836 +       simply shows the name of whiteout with keeping its behaviour.
23837 +
23838 +config AUFS_BR_RAMFS
23839 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23840 +       help
23841 +       If you want to use ramfs as an aufs branch fs, then enable this
23842 +       option. Generally tmpfs is recommended.
23843 +       Aufs prohibited them to be a branch fs by default, because
23844 +       initramfs becomes unusable after switch_root or something
23845 +       generally. If you sets initramfs as an aufs branch and boot your
23846 +       system by switch_root, you will meet a problem easily since the
23847 +       files in initramfs may be inaccessible.
23848 +       Unless you are going to use ramfs as an aufs branch fs without
23849 +       switch_root or something, leave it N.
23850 +
23851 +config AUFS_BR_FUSE
23852 +       bool "Fuse fs as an aufs branch"
23853 +       depends on FUSE_FS
23854 +       select AUFS_POLL
23855 +       help
23856 +       If you want to use fuse-based userspace filesystem as an aufs
23857 +       branch fs, then enable this option.
23858 +       It implements the internal poll(2) operation which is
23859 +       implemented by fuse only (curretnly).
23860 +
23861 +config AUFS_POLL
23862 +       bool
23863 +       help
23864 +       Automatic configuration for internal use.
23865 +
23866 +config AUFS_BR_HFSPLUS
23867 +       bool "Hfsplus as an aufs branch"
23868 +       depends on HFSPLUS_FS
23869 +       default y
23870 +       help
23871 +       If you want to use hfsplus fs as an aufs branch fs, then enable
23872 +       this option. This option introduces a small overhead at
23873 +       copying-up a file on hfsplus.
23874 +
23875 +config AUFS_BDEV_LOOP
23876 +       bool
23877 +       depends on BLK_DEV_LOOP
23878 +       default y
23879 +       help
23880 +       Automatic configuration for internal use.
23881 +       Convert =[ym] into =y.
23882 +
23883 +config AUFS_DEBUG
23884 +       bool "Debug aufs"
23885 +       help
23886 +       Enable this to compile aufs internal debug code.
23887 +       It will have a negative impact to the performance.
23888 +
23889 +config AUFS_MAGIC_SYSRQ
23890 +       bool
23891 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
23892 +       default y
23893 +       help
23894 +       Automatic configuration for internal use.
23895 +       When aufs supports Magic SysRq, enabled automatically.
23896 +endif
23897 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23898 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
23899 +++ linux/fs/aufs/lcnt.h        2021-02-24 13:33:42.747680497 +0100
23900 @@ -0,0 +1,186 @@
23901 +/* SPDX-License-Identifier: GPL-2.0 */
23902 +/*
23903 + * Copyright (C) 2018-2020 Junjiro R. Okajima
23904 + *
23905 + * This program, aufs is free software; you can redistribute it and/or modify
23906 + * it under the terms of the GNU General Public License as published by
23907 + * the Free Software Foundation; either version 2 of the License, or
23908 + * (at your option) any later version.
23909 + *
23910 + * This program is distributed in the hope that it will be useful,
23911 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23912 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23913 + * GNU General Public License for more details.
23914 + *
23915 + * You should have received a copy of the GNU General Public License
23916 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23917 + */
23918 +
23919 +/*
23920 + * simple long counter wrapper
23921 + */
23922 +
23923 +#ifndef __AUFS_LCNT_H__
23924 +#define __AUFS_LCNT_H__
23925 +
23926 +#ifdef __KERNEL__
23927 +
23928 +#include "debug.h"
23929 +
23930 +#define AuLCntATOMIC   1
23931 +#define AuLCntPCPUCNT  2
23932 +/*
23933 + * why does percpu_refcount require extra synchronize_rcu()s in
23934 + * au_br_do_free()
23935 + */
23936 +#define AuLCntPCPUREF  3
23937 +
23938 +/* #define AuLCntChosen        AuLCntATOMIC */
23939 +#define AuLCntChosen   AuLCntPCPUCNT
23940 +/* #define AuLCntChosen        AuLCntPCPUREF */
23941 +
23942 +#if AuLCntChosen == AuLCntATOMIC
23943 +#include <linux/atomic.h>
23944 +
23945 +typedef atomic_long_t au_lcnt_t;
23946 +
23947 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23948 +{
23949 +       atomic_long_set(cnt, 0);
23950 +       return 0;
23951 +}
23952 +
23953 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
23954 +{
23955 +       /* empty */
23956 +}
23957 +
23958 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
23959 +                              int do_sync __maybe_unused)
23960 +{
23961 +       /* empty */
23962 +}
23963 +
23964 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
23965 +{
23966 +       atomic_long_inc(cnt);
23967 +}
23968 +
23969 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
23970 +{
23971 +       atomic_long_dec(cnt);
23972 +}
23973 +
23974 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
23975 +{
23976 +       return atomic_long_read(cnt);
23977 +}
23978 +#endif
23979 +
23980 +#if AuLCntChosen == AuLCntPCPUCNT
23981 +#include <linux/percpu_counter.h>
23982 +
23983 +typedef struct percpu_counter au_lcnt_t;
23984 +
23985 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23986 +{
23987 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
23988 +}
23989 +
23990 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
23991 +{
23992 +       /* empty */
23993 +}
23994 +
23995 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
23996 +{
23997 +       percpu_counter_destroy(cnt);
23998 +}
23999 +
24000 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24001 +{
24002 +       percpu_counter_inc(cnt);
24003 +}
24004 +
24005 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24006 +{
24007 +       percpu_counter_dec(cnt);
24008 +}
24009 +
24010 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24011 +{
24012 +       s64 n;
24013 +
24014 +       n = percpu_counter_sum(cnt);
24015 +       BUG_ON(n < 0);
24016 +       if (LONG_MAX != LLONG_MAX
24017 +           && n > LONG_MAX)
24018 +               AuWarn1("%s\n", "wrap-around");
24019 +
24020 +       return n;
24021 +}
24022 +#endif
24023 +
24024 +#if AuLCntChosen == AuLCntPCPUREF
24025 +#include <linux/percpu-refcount.h>
24026 +
24027 +typedef struct percpu_ref au_lcnt_t;
24028 +
24029 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24030 +{
24031 +       if (!release)
24032 +               release = percpu_ref_exit;
24033 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24034 +}
24035 +
24036 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24037 +{
24038 +       synchronize_rcu();
24039 +}
24040 +
24041 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24042 +{
24043 +       percpu_ref_kill(cnt);
24044 +       if (do_sync)
24045 +               au_lcnt_wait_for_fin(cnt);
24046 +}
24047 +
24048 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24049 +{
24050 +       percpu_ref_get(cnt);
24051 +}
24052 +
24053 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24054 +{
24055 +       percpu_ref_put(cnt);
24056 +}
24057 +
24058 +/*
24059 + * avoid calling this func as possible.
24060 + */
24061 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24062 +{
24063 +       long l;
24064 +
24065 +       percpu_ref_switch_to_atomic_sync(cnt);
24066 +       l = atomic_long_read(&cnt->count);
24067 +       if (do_rev)
24068 +               percpu_ref_switch_to_percpu(cnt);
24069 +
24070 +       /* percpu_ref is initialized by 1 instead of 0 */
24071 +       return l - 1;
24072 +}
24073 +#endif
24074 +
24075 +#ifdef CONFIG_AUFS_DEBUG
24076 +#define AuLCntZero(val) do {                   \
24077 +       long l = val;                           \
24078 +       if (l)                                  \
24079 +               AuDbg("%s = %ld\n", #val, l);   \
24080 +} while (0)
24081 +#else
24082 +#define AuLCntZero(val)                do {} while (0)
24083 +#endif
24084 +
24085 +#endif /* __KERNEL__ */
24086 +#endif /* __AUFS_LCNT_H__ */
24087 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24088 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24089 +++ linux/fs/aufs/loop.c        2021-02-24 13:33:42.747680497 +0100
24090 @@ -0,0 +1,148 @@
24091 +// SPDX-License-Identifier: GPL-2.0
24092 +/*
24093 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24094 + *
24095 + * This program, aufs is free software; you can redistribute it and/or modify
24096 + * it under the terms of the GNU General Public License as published by
24097 + * the Free Software Foundation; either version 2 of the License, or
24098 + * (at your option) any later version.
24099 + *
24100 + * This program is distributed in the hope that it will be useful,
24101 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24102 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24103 + * GNU General Public License for more details.
24104 + *
24105 + * You should have received a copy of the GNU General Public License
24106 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24107 + */
24108 +
24109 +/*
24110 + * support for loopback block device as a branch
24111 + */
24112 +
24113 +#include "aufs.h"
24114 +
24115 +/* added into drivers/block/loop.c */
24116 +static struct file *(*backing_file_func)(struct super_block *sb);
24117 +
24118 +/*
24119 + * test if two lower dentries have overlapping branches.
24120 + */
24121 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24122 +{
24123 +       struct super_block *h_sb;
24124 +       struct file *backing_file;
24125 +
24126 +       if (unlikely(!backing_file_func)) {
24127 +               /* don't load "loop" module here */
24128 +               backing_file_func = symbol_get(loop_backing_file);
24129 +               if (unlikely(!backing_file_func))
24130 +                       /* "loop" module is not loaded */
24131 +                       return 0;
24132 +       }
24133 +
24134 +       h_sb = h_adding->d_sb;
24135 +       backing_file = backing_file_func(h_sb);
24136 +       if (!backing_file)
24137 +               return 0;
24138 +
24139 +       h_adding = backing_file->f_path.dentry;
24140 +       /*
24141 +        * h_adding can be local NFS.
24142 +        * in this case aufs cannot detect the loop.
24143 +        */
24144 +       if (unlikely(h_adding->d_sb == sb))
24145 +               return 1;
24146 +       return !!au_test_subdir(h_adding, sb->s_root);
24147 +}
24148 +
24149 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24150 +int au_test_loopback_kthread(void)
24151 +{
24152 +       int ret;
24153 +       struct task_struct *tsk = current;
24154 +       char c, comm[sizeof(tsk->comm)];
24155 +
24156 +       ret = 0;
24157 +       if (tsk->flags & PF_KTHREAD) {
24158 +               get_task_comm(comm, tsk);
24159 +               c = comm[4];
24160 +               ret = ('0' <= c && c <= '9'
24161 +                      && !strncmp(comm, "loop", 4));
24162 +       }
24163 +
24164 +       return ret;
24165 +}
24166 +
24167 +/* ---------------------------------------------------------------------- */
24168 +
24169 +#define au_warn_loopback_step  16
24170 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24171 +static unsigned long *au_warn_loopback_array;
24172 +
24173 +void au_warn_loopback(struct super_block *h_sb)
24174 +{
24175 +       int i, new_nelem;
24176 +       unsigned long *a, magic;
24177 +       static DEFINE_SPINLOCK(spin);
24178 +
24179 +       magic = h_sb->s_magic;
24180 +       spin_lock(&spin);
24181 +       a = au_warn_loopback_array;
24182 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24183 +               if (a[i] == magic) {
24184 +                       spin_unlock(&spin);
24185 +                       return;
24186 +               }
24187 +
24188 +       /* h_sb is new to us, print it */
24189 +       if (i < au_warn_loopback_nelem) {
24190 +               a[i] = magic;
24191 +               goto pr;
24192 +       }
24193 +
24194 +       /* expand the array */
24195 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24196 +       a = au_kzrealloc(au_warn_loopback_array,
24197 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24198 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24199 +                        /*may_shrink*/0);
24200 +       if (a) {
24201 +               au_warn_loopback_nelem = new_nelem;
24202 +               au_warn_loopback_array = a;
24203 +               a[i] = magic;
24204 +               goto pr;
24205 +       }
24206 +
24207 +       spin_unlock(&spin);
24208 +       AuWarn1("realloc failed, ignored\n");
24209 +       return;
24210 +
24211 +pr:
24212 +       spin_unlock(&spin);
24213 +       pr_warn("you may want to try another patch for loopback file "
24214 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24215 +}
24216 +
24217 +int au_loopback_init(void)
24218 +{
24219 +       int err;
24220 +       struct super_block *sb __maybe_unused;
24221 +
24222 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24223 +
24224 +       err = 0;
24225 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24226 +                                        sizeof(unsigned long), GFP_NOFS);
24227 +       if (unlikely(!au_warn_loopback_array))
24228 +               err = -ENOMEM;
24229 +
24230 +       return err;
24231 +}
24232 +
24233 +void au_loopback_fin(void)
24234 +{
24235 +       if (backing_file_func)
24236 +               symbol_put(loop_backing_file);
24237 +       au_kfree_try_rcu(au_warn_loopback_array);
24238 +}
24239 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24240 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24241 +++ linux/fs/aufs/loop.h        2021-02-24 13:33:42.747680497 +0100
24242 @@ -0,0 +1,55 @@
24243 +/* SPDX-License-Identifier: GPL-2.0 */
24244 +/*
24245 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24246 + *
24247 + * This program, aufs is free software; you can redistribute it and/or modify
24248 + * it under the terms of the GNU General Public License as published by
24249 + * the Free Software Foundation; either version 2 of the License, or
24250 + * (at your option) any later version.
24251 + *
24252 + * This program is distributed in the hope that it will be useful,
24253 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24254 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24255 + * GNU General Public License for more details.
24256 + *
24257 + * You should have received a copy of the GNU General Public License
24258 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24259 + */
24260 +
24261 +/*
24262 + * support for loopback mount as a branch
24263 + */
24264 +
24265 +#ifndef __AUFS_LOOP_H__
24266 +#define __AUFS_LOOP_H__
24267 +
24268 +#ifdef __KERNEL__
24269 +
24270 +struct dentry;
24271 +struct super_block;
24272 +
24273 +#ifdef CONFIG_AUFS_BDEV_LOOP
24274 +/* drivers/block/loop.c */
24275 +struct file *loop_backing_file(struct super_block *sb);
24276 +
24277 +/* loop.c */
24278 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24279 +int au_test_loopback_kthread(void);
24280 +void au_warn_loopback(struct super_block *h_sb);
24281 +
24282 +int au_loopback_init(void);
24283 +void au_loopback_fin(void);
24284 +#else
24285 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24286 +
24287 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24288 +          struct dentry *h_adding)
24289 +AuStubInt0(au_test_loopback_kthread, void)
24290 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24291 +
24292 +AuStubInt0(au_loopback_init, void)
24293 +AuStubVoid(au_loopback_fin, void)
24294 +#endif /* BLK_DEV_LOOP */
24295 +
24296 +#endif /* __KERNEL__ */
24297 +#endif /* __AUFS_LOOP_H__ */
24298 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24299 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24300 +++ linux/fs/aufs/magic.mk      2021-02-24 13:33:42.747680497 +0100
24301 @@ -0,0 +1,31 @@
24302 +# SPDX-License-Identifier: GPL-2.0
24303 +
24304 +# defined in ${srctree}/fs/fuse/inode.c
24305 +# tristate
24306 +ifdef CONFIG_FUSE_FS
24307 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24308 +endif
24309 +
24310 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24311 +# tristate
24312 +ifdef CONFIG_XFS_FS
24313 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24314 +endif
24315 +
24316 +# defined in ${srctree}/fs/configfs/mount.c
24317 +# tristate
24318 +ifdef CONFIG_CONFIGFS_FS
24319 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24320 +endif
24321 +
24322 +# defined in ${srctree}/fs/ubifs/ubifs.h
24323 +# tristate
24324 +ifdef CONFIG_UBIFS_FS
24325 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24326 +endif
24327 +
24328 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24329 +# tristate
24330 +ifdef CONFIG_HFSPLUS_FS
24331 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24332 +endif
24333 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24334 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24335 +++ linux/fs/aufs/Makefile      2021-02-24 13:33:42.741013619 +0100
24336 @@ -0,0 +1,46 @@
24337 +# SPDX-License-Identifier: GPL-2.0
24338 +
24339 +include ${src}/magic.mk
24340 +ifeq (${CONFIG_AUFS_FS},m)
24341 +include ${src}/conf.mk
24342 +endif
24343 +-include ${src}/priv_def.mk
24344 +
24345 +# cf. include/linux/kernel.h
24346 +# enable pr_debug
24347 +ccflags-y += -DDEBUG
24348 +# sparse requires the full pathname
24349 +ifdef M
24350 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24351 +else
24352 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24353 +endif
24354 +
24355 +obj-$(CONFIG_AUFS_FS) += aufs.o
24356 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24357 +       wkq.o vfsub.o dcsub.o \
24358 +       cpup.o whout.o wbr_policy.o \
24359 +       dinfo.o dentry.o \
24360 +       dynop.o \
24361 +       finfo.o file.o f_op.o \
24362 +       dir.o vdir.o \
24363 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24364 +       mvdown.o ioctl.o
24365 +
24366 +# all are boolean
24367 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24368 +aufs-$(CONFIG_SYSFS) += sysfs.o
24369 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24370 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24371 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24372 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24373 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24374 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24375 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24376 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24377 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24378 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24379 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24380 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24381 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24382 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24383 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24384 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24385 +++ linux/fs/aufs/module.c      2021-02-24 13:33:42.747680497 +0100
24386 @@ -0,0 +1,273 @@
24387 +// SPDX-License-Identifier: GPL-2.0
24388 +/*
24389 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24390 + *
24391 + * This program, aufs is free software; you can redistribute it and/or modify
24392 + * it under the terms of the GNU General Public License as published by
24393 + * the Free Software Foundation; either version 2 of the License, or
24394 + * (at your option) any later version.
24395 + *
24396 + * This program is distributed in the hope that it will be useful,
24397 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24398 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24399 + * GNU General Public License for more details.
24400 + *
24401 + * You should have received a copy of the GNU General Public License
24402 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24403 + */
24404 +
24405 +/*
24406 + * module global variables and operations
24407 + */
24408 +
24409 +#include <linux/module.h>
24410 +#include <linux/seq_file.h>
24411 +#include "aufs.h"
24412 +
24413 +/* shrinkable realloc */
24414 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24415 +{
24416 +       size_t sz;
24417 +       int diff;
24418 +
24419 +       sz = 0;
24420 +       diff = -1;
24421 +       if (p) {
24422 +#if 0 /* unused */
24423 +               if (!new_sz) {
24424 +                       au_kfree_rcu(p);
24425 +                       p = NULL;
24426 +                       goto out;
24427 +               }
24428 +#else
24429 +               AuDebugOn(!new_sz);
24430 +#endif
24431 +               sz = ksize(p);
24432 +               diff = au_kmidx_sub(sz, new_sz);
24433 +       }
24434 +       if (sz && !diff)
24435 +               goto out;
24436 +
24437 +       if (sz < new_sz)
24438 +               /* expand or SLOB */
24439 +               p = krealloc(p, new_sz, gfp);
24440 +       else if (new_sz < sz && may_shrink) {
24441 +               /* shrink */
24442 +               void *q;
24443 +
24444 +               q = kmalloc(new_sz, gfp);
24445 +               if (q) {
24446 +                       if (p) {
24447 +                               memcpy(q, p, new_sz);
24448 +                               au_kfree_try_rcu(p);
24449 +                       }
24450 +                       p = q;
24451 +               } else
24452 +                       p = NULL;
24453 +       }
24454 +
24455 +out:
24456 +       return p;
24457 +}
24458 +
24459 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24460 +                  int may_shrink)
24461 +{
24462 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24463 +       if (p && new_sz > nused)
24464 +               memset(p + nused, 0, new_sz - nused);
24465 +       return p;
24466 +}
24467 +
24468 +/* ---------------------------------------------------------------------- */
24469 +/*
24470 + * aufs caches
24471 + */
24472 +struct kmem_cache *au_cache[AuCache_Last];
24473 +
24474 +static void au_cache_fin(void)
24475 +{
24476 +       int i;
24477 +
24478 +       /*
24479 +        * Make sure all delayed rcu free inodes are flushed before we
24480 +        * destroy cache.
24481 +        */
24482 +       rcu_barrier();
24483 +
24484 +       /* excluding AuCache_HNOTIFY */
24485 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24486 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24487 +               kmem_cache_destroy(au_cache[i]);
24488 +               au_cache[i] = NULL;
24489 +       }
24490 +}
24491 +
24492 +static int __init au_cache_init(void)
24493 +{
24494 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24495 +       if (au_cache[AuCache_DINFO])
24496 +               /* SLAB_DESTROY_BY_RCU */
24497 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24498 +                                                      au_icntnr_init_once);
24499 +       if (au_cache[AuCache_ICNTNR])
24500 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24501 +                                                     au_fi_init_once);
24502 +       if (au_cache[AuCache_FINFO])
24503 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24504 +       if (au_cache[AuCache_VDIR])
24505 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24506 +       if (au_cache[AuCache_DEHSTR])
24507 +               return 0;
24508 +
24509 +       au_cache_fin();
24510 +       return -ENOMEM;
24511 +}
24512 +
24513 +/* ---------------------------------------------------------------------- */
24514 +
24515 +int au_dir_roflags;
24516 +
24517 +#ifdef CONFIG_AUFS_SBILIST
24518 +/*
24519 + * iterate_supers_type() doesn't protect us from
24520 + * remounting (branch management)
24521 + */
24522 +struct hlist_bl_head au_sbilist;
24523 +#endif
24524 +
24525 +/*
24526 + * functions for module interface.
24527 + */
24528 +MODULE_LICENSE("GPL");
24529 +/* MODULE_LICENSE("GPL v2"); */
24530 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24531 +MODULE_DESCRIPTION(AUFS_NAME
24532 +       " -- Advanced multi layered unification filesystem");
24533 +MODULE_VERSION(AUFS_VERSION);
24534 +MODULE_ALIAS_FS(AUFS_NAME);
24535 +
24536 +/* this module parameter has no meaning when SYSFS is disabled */
24537 +int sysaufs_brs = 1;
24538 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24539 +module_param_named(brs, sysaufs_brs, int, 0444);
24540 +
24541 +/* this module parameter has no meaning when USER_NS is disabled */
24542 +bool au_userns;
24543 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24544 +module_param_named(allow_userns, au_userns, bool, 0444);
24545 +
24546 +/* ---------------------------------------------------------------------- */
24547 +
24548 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24549 +
24550 +int au_seq_path(struct seq_file *seq, struct path *path)
24551 +{
24552 +       int err;
24553 +
24554 +       err = seq_path(seq, path, au_esc_chars);
24555 +       if (err >= 0)
24556 +               err = 0;
24557 +       else
24558 +               err = -ENOMEM;
24559 +
24560 +       return err;
24561 +}
24562 +
24563 +/* ---------------------------------------------------------------------- */
24564 +
24565 +static int __init aufs_init(void)
24566 +{
24567 +       int err, i;
24568 +       char *p;
24569 +
24570 +       p = au_esc_chars;
24571 +       for (i = 1; i <= ' '; i++)
24572 +               *p++ = i;
24573 +       *p++ = '\\';
24574 +       *p++ = '\x7f';
24575 +       *p = 0;
24576 +
24577 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24578 +
24579 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24580 +       for (i = 0; i < AuIop_Last; i++)
24581 +               aufs_iop_nogetattr[i].getattr = NULL;
24582 +
24583 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24584 +
24585 +       au_sbilist_init();
24586 +       sysaufs_brs_init();
24587 +       au_debug_init();
24588 +       au_dy_init();
24589 +       err = sysaufs_init();
24590 +       if (unlikely(err))
24591 +               goto out;
24592 +       err = dbgaufs_init();
24593 +       if (unlikely(err))
24594 +               goto out_sysaufs;
24595 +       err = au_procfs_init();
24596 +       if (unlikely(err))
24597 +               goto out_dbgaufs;
24598 +       err = au_wkq_init();
24599 +       if (unlikely(err))
24600 +               goto out_procfs;
24601 +       err = au_loopback_init();
24602 +       if (unlikely(err))
24603 +               goto out_wkq;
24604 +       err = au_hnotify_init();
24605 +       if (unlikely(err))
24606 +               goto out_loopback;
24607 +       err = au_sysrq_init();
24608 +       if (unlikely(err))
24609 +               goto out_hin;
24610 +       err = au_cache_init();
24611 +       if (unlikely(err))
24612 +               goto out_sysrq;
24613 +
24614 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24615 +       err = register_filesystem(&aufs_fs_type);
24616 +       if (unlikely(err))
24617 +               goto out_cache;
24618 +
24619 +       /* since we define pr_fmt, call printk directly */
24620 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24621 +       goto out; /* success */
24622 +
24623 +out_cache:
24624 +       au_cache_fin();
24625 +out_sysrq:
24626 +       au_sysrq_fin();
24627 +out_hin:
24628 +       au_hnotify_fin();
24629 +out_loopback:
24630 +       au_loopback_fin();
24631 +out_wkq:
24632 +       au_wkq_fin();
24633 +out_procfs:
24634 +       au_procfs_fin();
24635 +out_dbgaufs:
24636 +       dbgaufs_fin();
24637 +out_sysaufs:
24638 +       sysaufs_fin();
24639 +       au_dy_fin();
24640 +out:
24641 +       return err;
24642 +}
24643 +
24644 +static void __exit aufs_exit(void)
24645 +{
24646 +       unregister_filesystem(&aufs_fs_type);
24647 +       au_cache_fin();
24648 +       au_sysrq_fin();
24649 +       au_hnotify_fin();
24650 +       au_loopback_fin();
24651 +       au_wkq_fin();
24652 +       au_procfs_fin();
24653 +       dbgaufs_fin();
24654 +       sysaufs_fin();
24655 +       au_dy_fin();
24656 +}
24657 +
24658 +module_init(aufs_init);
24659 +module_exit(aufs_exit);
24660 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24661 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24662 +++ linux/fs/aufs/module.h      2021-02-24 13:33:42.747680497 +0100
24663 @@ -0,0 +1,166 @@
24664 +/* SPDX-License-Identifier: GPL-2.0 */
24665 +/*
24666 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24667 + *
24668 + * This program, aufs is free software; you can redistribute it and/or modify
24669 + * it under the terms of the GNU General Public License as published by
24670 + * the Free Software Foundation; either version 2 of the License, or
24671 + * (at your option) any later version.
24672 + *
24673 + * This program is distributed in the hope that it will be useful,
24674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24676 + * GNU General Public License for more details.
24677 + *
24678 + * You should have received a copy of the GNU General Public License
24679 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24680 + */
24681 +
24682 +/*
24683 + * module initialization and module-global
24684 + */
24685 +
24686 +#ifndef __AUFS_MODULE_H__
24687 +#define __AUFS_MODULE_H__
24688 +
24689 +#ifdef __KERNEL__
24690 +
24691 +#include <linux/slab.h>
24692 +#include "debug.h"
24693 +#include "dentry.h"
24694 +#include "dir.h"
24695 +#include "file.h"
24696 +#include "inode.h"
24697 +
24698 +struct path;
24699 +struct seq_file;
24700 +
24701 +/* module parameters */
24702 +extern int sysaufs_brs;
24703 +extern bool au_userns;
24704 +
24705 +/* ---------------------------------------------------------------------- */
24706 +
24707 +extern int au_dir_roflags;
24708 +
24709 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24710 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24711 +                  int may_shrink);
24712 +
24713 +/*
24714 + * Comparing the size of the object with sizeof(struct rcu_head)
24715 + * case 1: object is always larger
24716 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24717 + * case 2: object is always smaller
24718 + *     --> au_kfree_small()
24719 + * case 3: object can be any size
24720 + *     --> au_kfree_try_rcu()
24721 + */
24722 +
24723 +static inline void au_kfree_do_rcu(const void *p)
24724 +{
24725 +       struct {
24726 +               struct rcu_head rcu;
24727 +       } *a = (void *)p;
24728 +
24729 +       kfree_rcu(a, rcu);
24730 +}
24731 +
24732 +#define au_kfree_rcu(_p) do {                                          \
24733 +               typeof(_p) p = (_p);                                    \
24734 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24735 +               if (p)                                                  \
24736 +                       au_kfree_do_rcu(p);                             \
24737 +       } while (0)
24738 +
24739 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24740 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24741 +
24742 +static inline void au_kfree_try_rcu(const void *p)
24743 +{
24744 +       if (!p)
24745 +               return;
24746 +       if (au_kfree_sz_test(p))
24747 +               au_kfree_do_rcu(p);
24748 +       else
24749 +               kfree(p);
24750 +}
24751 +
24752 +static inline void au_kfree_small(const void *p)
24753 +{
24754 +       if (!p)
24755 +               return;
24756 +       AuDebugOn(au_kfree_sz_test(p));
24757 +       kfree(p);
24758 +}
24759 +
24760 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24761 +{
24762 +#ifndef CONFIG_SLOB
24763 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24764 +#else
24765 +       return -1; /* SLOB is untested */
24766 +#endif
24767 +}
24768 +
24769 +int au_seq_path(struct seq_file *seq, struct path *path);
24770 +
24771 +#ifdef CONFIG_PROC_FS
24772 +/* procfs.c */
24773 +int __init au_procfs_init(void);
24774 +void au_procfs_fin(void);
24775 +#else
24776 +AuStubInt0(au_procfs_init, void);
24777 +AuStubVoid(au_procfs_fin, void);
24778 +#endif
24779 +
24780 +/* ---------------------------------------------------------------------- */
24781 +
24782 +/* kmem cache */
24783 +enum {
24784 +       AuCache_DINFO,
24785 +       AuCache_ICNTNR,
24786 +       AuCache_FINFO,
24787 +       AuCache_VDIR,
24788 +       AuCache_DEHSTR,
24789 +       AuCache_HNOTIFY, /* must be last */
24790 +       AuCache_Last
24791 +};
24792 +
24793 +extern struct kmem_cache *au_cache[AuCache_Last];
24794 +
24795 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24796 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24797 +#define AuCacheCtor(type, ctor)        \
24798 +       kmem_cache_create(#type, sizeof(struct type), \
24799 +                         __alignof__(struct type), AuCacheFlags, ctor)
24800 +
24801 +#define AuCacheFuncs(name, index)                                      \
24802 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24803 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24804 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24805 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24806 +                                                                       \
24807 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24808 +       { void *p = rcu;                                                \
24809 +               p -= offsetof(struct au_##name, rcu);                   \
24810 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24811 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24812 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24813 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24814 +                                                                       \
24815 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24816 +       { /* au_cache_free_##name##_norcu(p); */                        \
24817 +               au_cache_free_##name##_rcu(p); }
24818 +
24819 +AuCacheFuncs(dinfo, DINFO);
24820 +AuCacheFuncs(icntnr, ICNTNR);
24821 +AuCacheFuncs(finfo, FINFO);
24822 +AuCacheFuncs(vdir, VDIR);
24823 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24824 +#ifdef CONFIG_AUFS_HNOTIFY
24825 +AuCacheFuncs(hnotify, HNOTIFY);
24826 +#endif
24827 +
24828 +#endif /* __KERNEL__ */
24829 +#endif /* __AUFS_MODULE_H__ */
24830 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24831 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24832 +++ linux/fs/aufs/mvdown.c      2021-02-24 13:33:42.747680497 +0100
24833 @@ -0,0 +1,706 @@
24834 +// SPDX-License-Identifier: GPL-2.0
24835 +/*
24836 + * Copyright (C) 2011-2020 Junjiro R. Okajima
24837 + *
24838 + * This program, aufs is free software; you can redistribute it and/or modify
24839 + * it under the terms of the GNU General Public License as published by
24840 + * the Free Software Foundation; either version 2 of the License, or
24841 + * (at your option) any later version.
24842 + *
24843 + * This program is distributed in the hope that it will be useful,
24844 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24845 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24846 + * GNU General Public License for more details.
24847 + *
24848 + * You should have received a copy of the GNU General Public License
24849 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24850 + */
24851 +
24852 +/*
24853 + * move-down, opposite of copy-up
24854 + */
24855 +
24856 +#include "aufs.h"
24857 +
24858 +struct au_mvd_args {
24859 +       struct {
24860 +               struct super_block *h_sb;
24861 +               struct dentry *h_parent;
24862 +               struct au_hinode *hdir;
24863 +               struct inode *h_dir, *h_inode;
24864 +               struct au_pin pin;
24865 +       } info[AUFS_MVDOWN_NARRAY];
24866 +
24867 +       struct aufs_mvdown mvdown;
24868 +       struct dentry *dentry, *parent;
24869 +       struct inode *inode, *dir;
24870 +       struct super_block *sb;
24871 +       aufs_bindex_t bopq, bwh, bfound;
24872 +       unsigned char rename_lock;
24873 +};
24874 +
24875 +#define mvd_errno              mvdown.au_errno
24876 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24877 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24878 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24879 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24880 +
24881 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24882 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24883 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24884 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24885 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24886 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24887 +
24888 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24889 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24890 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24891 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24892 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24893 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24894 +
24895 +#define AU_MVD_PR(flag, ...) do {                      \
24896 +               if (flag)                               \
24897 +                       pr_err(__VA_ARGS__);            \
24898 +       } while (0)
24899 +
24900 +static int find_lower_writable(struct au_mvd_args *a)
24901 +{
24902 +       struct super_block *sb;
24903 +       aufs_bindex_t bindex, bbot;
24904 +       struct au_branch *br;
24905 +
24906 +       sb = a->sb;
24907 +       bindex = a->mvd_bsrc;
24908 +       bbot = au_sbbot(sb);
24909 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24910 +               for (bindex++; bindex <= bbot; bindex++) {
24911 +                       br = au_sbr(sb, bindex);
24912 +                       if (au_br_fhsm(br->br_perm)
24913 +                           && !sb_rdonly(au_br_sb(br)))
24914 +                               return bindex;
24915 +               }
24916 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24917 +               for (bindex++; bindex <= bbot; bindex++) {
24918 +                       br = au_sbr(sb, bindex);
24919 +                       if (!au_br_rdonly(br))
24920 +                               return bindex;
24921 +               }
24922 +       else
24923 +               for (bindex++; bindex <= bbot; bindex++) {
24924 +                       br = au_sbr(sb, bindex);
24925 +                       if (!sb_rdonly(au_br_sb(br))) {
24926 +                               if (au_br_rdonly(br))
24927 +                                       a->mvdown.flags
24928 +                                               |= AUFS_MVDOWN_ROLOWER_R;
24929 +                               return bindex;
24930 +                       }
24931 +               }
24932 +
24933 +       return -1;
24934 +}
24935 +
24936 +/* make the parent dir on bdst */
24937 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24938 +{
24939 +       int err;
24940 +
24941 +       err = 0;
24942 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24943 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24944 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24945 +       a->mvd_h_dst_parent = NULL;
24946 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
24947 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24948 +       if (!a->mvd_h_dst_parent) {
24949 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24950 +               if (unlikely(err)) {
24951 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
24952 +                       goto out;
24953 +               }
24954 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24955 +       }
24956 +
24957 +out:
24958 +       AuTraceErr(err);
24959 +       return err;
24960 +}
24961 +
24962 +/* lock them all */
24963 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
24964 +{
24965 +       int err;
24966 +       struct dentry *h_trap;
24967 +
24968 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24969 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
24970 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24971 +                    au_opt_udba(a->sb),
24972 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24973 +       AuTraceErr(err);
24974 +       if (unlikely(err)) {
24975 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
24976 +               goto out;
24977 +       }
24978 +
24979 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24980 +               a->rename_lock = 0;
24981 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24982 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24983 +                           au_opt_udba(a->sb),
24984 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24985 +               err = au_do_pin(&a->mvd_pin_src);
24986 +               AuTraceErr(err);
24987 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
24988 +               if (unlikely(err)) {
24989 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
24990 +                       goto out_dst;
24991 +               }
24992 +               goto out; /* success */
24993 +       }
24994 +
24995 +       a->rename_lock = 1;
24996 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
24997 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24998 +                    au_opt_udba(a->sb),
24999 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25000 +       AuTraceErr(err);
25001 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25002 +       if (unlikely(err)) {
25003 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25004 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25005 +               goto out_dst;
25006 +       }
25007 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25008 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25009 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25010 +       if (h_trap) {
25011 +               err = (h_trap != a->mvd_h_src_parent);
25012 +               if (err)
25013 +                       err = (h_trap != a->mvd_h_dst_parent);
25014 +       }
25015 +       BUG_ON(err); /* it should never happen */
25016 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25017 +               err = -EBUSY;
25018 +               AuTraceErr(err);
25019 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25020 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25021 +               au_pin_hdir_lock(&a->mvd_pin_src);
25022 +               au_unpin(&a->mvd_pin_src);
25023 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25024 +               goto out_dst;
25025 +       }
25026 +       goto out; /* success */
25027 +
25028 +out_dst:
25029 +       au_unpin(&a->mvd_pin_dst);
25030 +out:
25031 +       AuTraceErr(err);
25032 +       return err;
25033 +}
25034 +
25035 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25036 +{
25037 +       if (!a->rename_lock)
25038 +               au_unpin(&a->mvd_pin_src);
25039 +       else {
25040 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25041 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25042 +               au_pin_hdir_lock(&a->mvd_pin_src);
25043 +               au_unpin(&a->mvd_pin_src);
25044 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25045 +       }
25046 +       au_unpin(&a->mvd_pin_dst);
25047 +}
25048 +
25049 +/* copy-down the file */
25050 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25051 +{
25052 +       int err;
25053 +       struct au_cp_generic cpg = {
25054 +               .dentry = a->dentry,
25055 +               .bdst   = a->mvd_bdst,
25056 +               .bsrc   = a->mvd_bsrc,
25057 +               .len    = -1,
25058 +               .pin    = &a->mvd_pin_dst,
25059 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25060 +       };
25061 +
25062 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25063 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25064 +               au_fset_cpup(cpg.flags, OVERWRITE);
25065 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25066 +               au_fset_cpup(cpg.flags, RWDST);
25067 +       err = au_sio_cpdown_simple(&cpg);
25068 +       if (unlikely(err))
25069 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25070 +
25071 +       AuTraceErr(err);
25072 +       return err;
25073 +}
25074 +
25075 +/*
25076 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25077 + * were sleeping
25078 + */
25079 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25080 +{
25081 +       int err;
25082 +       struct path h_path;
25083 +       struct au_branch *br;
25084 +       struct inode *delegated;
25085 +
25086 +       br = au_sbr(a->sb, a->mvd_bdst);
25087 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25088 +       err = PTR_ERR(h_path.dentry);
25089 +       if (IS_ERR(h_path.dentry)) {
25090 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25091 +               goto out;
25092 +       }
25093 +
25094 +       err = 0;
25095 +       if (d_is_positive(h_path.dentry)) {
25096 +               h_path.mnt = au_br_mnt(br);
25097 +               delegated = NULL;
25098 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25099 +                                  &delegated, /*force*/0);
25100 +               if (unlikely(err == -EWOULDBLOCK)) {
25101 +                       pr_warn("cannot retry for NFSv4 delegation"
25102 +                               " for an internal unlink\n");
25103 +                       iput(delegated);
25104 +               }
25105 +               if (unlikely(err))
25106 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25107 +       }
25108 +       dput(h_path.dentry);
25109 +
25110 +out:
25111 +       AuTraceErr(err);
25112 +       return err;
25113 +}
25114 +
25115 +/*
25116 + * unlink the topmost h_dentry
25117 + */
25118 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25119 +{
25120 +       int err;
25121 +       struct path h_path;
25122 +       struct inode *delegated;
25123 +
25124 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25125 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25126 +       delegated = NULL;
25127 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25128 +       if (unlikely(err == -EWOULDBLOCK)) {
25129 +               pr_warn("cannot retry for NFSv4 delegation"
25130 +                       " for an internal unlink\n");
25131 +               iput(delegated);
25132 +       }
25133 +       if (unlikely(err))
25134 +               AU_MVD_PR(dmsg, "unlink failed\n");
25135 +
25136 +       AuTraceErr(err);
25137 +       return err;
25138 +}
25139 +
25140 +/* Since mvdown succeeded, we ignore an error of this function */
25141 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25142 +{
25143 +       int err;
25144 +       struct au_branch *br;
25145 +
25146 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25147 +       br = au_sbr(a->sb, a->mvd_bsrc);
25148 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25149 +       if (!err) {
25150 +               br = au_sbr(a->sb, a->mvd_bdst);
25151 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25152 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25153 +       }
25154 +       if (!err)
25155 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25156 +       else
25157 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25158 +}
25159 +
25160 +/*
25161 + * copy-down the file and unlink the bsrc file.
25162 + * - unlink the bdst whout if exist
25163 + * - copy-down the file (with whtmp name and rename)
25164 + * - unlink the bsrc file
25165 + */
25166 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25167 +{
25168 +       int err;
25169 +
25170 +       err = au_do_mkdir(dmsg, a);
25171 +       if (!err)
25172 +               err = au_do_lock(dmsg, a);
25173 +       if (unlikely(err))
25174 +               goto out;
25175 +
25176 +       /*
25177 +        * do not revert the activities we made on bdst since they should be
25178 +        * harmless in aufs.
25179 +        */
25180 +
25181 +       err = au_do_cpdown(dmsg, a);
25182 +       if (!err)
25183 +               err = au_do_unlink_wh(dmsg, a);
25184 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25185 +               err = au_do_unlink(dmsg, a);
25186 +       if (unlikely(err))
25187 +               goto out_unlock;
25188 +
25189 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25190 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25191 +       if (find_lower_writable(a) < 0)
25192 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25193 +
25194 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25195 +               au_do_stfs(dmsg, a);
25196 +
25197 +       /* maintain internal array */
25198 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25199 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25200 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25201 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25202 +               au_set_ibtop(a->inode, a->mvd_bdst);
25203 +       } else {
25204 +               /* hide the lower */
25205 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25206 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25207 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25208 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25209 +       }
25210 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25211 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25212 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25213 +               au_set_ibbot(a->inode, a->mvd_bdst);
25214 +
25215 +out_unlock:
25216 +       au_do_unlock(dmsg, a);
25217 +out:
25218 +       AuTraceErr(err);
25219 +       return err;
25220 +}
25221 +
25222 +/* ---------------------------------------------------------------------- */
25223 +
25224 +/* make sure the file is idle */
25225 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25226 +{
25227 +       int err, plinked;
25228 +
25229 +       err = 0;
25230 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25231 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25232 +           && au_dcount(a->dentry) == 1
25233 +           && atomic_read(&a->inode->i_count) == 1
25234 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25235 +           && (!plinked || !au_plink_test(a->inode))
25236 +           && a->inode->i_nlink == 1)
25237 +               goto out;
25238 +
25239 +       err = -EBUSY;
25240 +       AU_MVD_PR(dmsg,
25241 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25242 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25243 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25244 +                 a->mvd_h_src_inode->i_nlink,
25245 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25246 +
25247 +out:
25248 +       AuTraceErr(err);
25249 +       return err;
25250 +}
25251 +
25252 +/* make sure the parent dir is fine */
25253 +static int au_mvd_args_parent(const unsigned char dmsg,
25254 +                             struct au_mvd_args *a)
25255 +{
25256 +       int err;
25257 +       aufs_bindex_t bindex;
25258 +
25259 +       err = 0;
25260 +       if (unlikely(au_alive_dir(a->parent))) {
25261 +               err = -ENOENT;
25262 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25263 +               goto out;
25264 +       }
25265 +
25266 +       a->bopq = au_dbdiropq(a->parent);
25267 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25268 +       AuDbg("b%d\n", bindex);
25269 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25270 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25271 +               err = -EINVAL;
25272 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25273 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25274 +                         a->bopq, a->mvd_bdst);
25275 +       }
25276 +
25277 +out:
25278 +       AuTraceErr(err);
25279 +       return err;
25280 +}
25281 +
25282 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25283 +                                   struct au_mvd_args *a)
25284 +{
25285 +       int err;
25286 +       struct au_dinfo *dinfo, *tmp;
25287 +
25288 +       /* lookup the next lower positive entry */
25289 +       err = -ENOMEM;
25290 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25291 +       if (unlikely(!tmp))
25292 +               goto out;
25293 +
25294 +       a->bfound = -1;
25295 +       a->bwh = -1;
25296 +       dinfo = au_di(a->dentry);
25297 +       au_di_cp(tmp, dinfo);
25298 +       au_di_swap(tmp, dinfo);
25299 +
25300 +       /* returns the number of positive dentries */
25301 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25302 +                            /* AuLkup_IGNORE_PERM */ 0);
25303 +       if (!err)
25304 +               a->bwh = au_dbwh(a->dentry);
25305 +       else if (err > 0)
25306 +               a->bfound = au_dbtop(a->dentry);
25307 +
25308 +       au_di_swap(tmp, dinfo);
25309 +       au_rw_write_unlock(&tmp->di_rwsem);
25310 +       au_di_free(tmp);
25311 +       if (unlikely(err < 0))
25312 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25313 +
25314 +       /*
25315 +        * here, we have these cases.
25316 +        * bfound == -1
25317 +        *      no positive dentry under bsrc. there are more sub-cases.
25318 +        *      bwh < 0
25319 +        *              there no whiteout, we can safely move-down.
25320 +        *      bwh <= bsrc
25321 +        *              impossible
25322 +        *      bsrc < bwh && bwh < bdst
25323 +        *              there is a whiteout on RO branch. cannot proceed.
25324 +        *      bwh == bdst
25325 +        *              there is a whiteout on the RW target branch. it should
25326 +        *              be removed.
25327 +        *      bdst < bwh
25328 +        *              there is a whiteout somewhere unrelated branch.
25329 +        * -1 < bfound && bfound <= bsrc
25330 +        *      impossible.
25331 +        * bfound < bdst
25332 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25333 +        *      proceed.
25334 +        * bfound == bdst
25335 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25336 +        *      error.
25337 +        * bdst < bfound
25338 +        *      found, after we create the file on bdst, it will be hidden.
25339 +        */
25340 +
25341 +       AuDebugOn(a->bfound == -1
25342 +                 && a->bwh != -1
25343 +                 && a->bwh <= a->mvd_bsrc);
25344 +       AuDebugOn(-1 < a->bfound
25345 +                 && a->bfound <= a->mvd_bsrc);
25346 +
25347 +       err = -EINVAL;
25348 +       if (a->bfound == -1
25349 +           && a->mvd_bsrc < a->bwh
25350 +           && a->bwh != -1
25351 +           && a->bwh < a->mvd_bdst) {
25352 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25353 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25354 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25355 +               goto out;
25356 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25357 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25358 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25359 +                         a->mvd_bdst, a->bfound);
25360 +               goto out;
25361 +       }
25362 +
25363 +       err = 0; /* success */
25364 +
25365 +out:
25366 +       AuTraceErr(err);
25367 +       return err;
25368 +}
25369 +
25370 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25371 +{
25372 +       int err;
25373 +
25374 +       err = 0;
25375 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25376 +           && a->bfound == a->mvd_bdst)
25377 +               err = -EEXIST;
25378 +       AuTraceErr(err);
25379 +       return err;
25380 +}
25381 +
25382 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25383 +{
25384 +       int err;
25385 +       struct au_branch *br;
25386 +
25387 +       err = -EISDIR;
25388 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25389 +               goto out;
25390 +
25391 +       err = -EINVAL;
25392 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25393 +               a->mvd_bsrc = au_ibtop(a->inode);
25394 +       else {
25395 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25396 +               if (unlikely(a->mvd_bsrc < 0
25397 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25398 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25399 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25400 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25401 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25402 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25403 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25404 +                       AU_MVD_PR(dmsg, "no upper\n");
25405 +                       goto out;
25406 +               }
25407 +       }
25408 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25409 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25410 +               AU_MVD_PR(dmsg, "on the bottom\n");
25411 +               goto out;
25412 +       }
25413 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25414 +       br = au_sbr(a->sb, a->mvd_bsrc);
25415 +       err = au_br_rdonly(br);
25416 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25417 +               if (unlikely(err))
25418 +                       goto out;
25419 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25420 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25421 +               if (err)
25422 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25423 +               /* go on */
25424 +       } else
25425 +               goto out;
25426 +
25427 +       err = -EINVAL;
25428 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25429 +               a->mvd_bdst = find_lower_writable(a);
25430 +               if (unlikely(a->mvd_bdst < 0)) {
25431 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25432 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25433 +                       goto out;
25434 +               }
25435 +       } else {
25436 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25437 +               if (unlikely(a->mvd_bdst < 0
25438 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25439 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25440 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25441 +                       goto out;
25442 +               }
25443 +       }
25444 +
25445 +       err = au_mvd_args_busy(dmsg, a);
25446 +       if (!err)
25447 +               err = au_mvd_args_parent(dmsg, a);
25448 +       if (!err)
25449 +               err = au_mvd_args_intermediate(dmsg, a);
25450 +       if (!err)
25451 +               err = au_mvd_args_exist(dmsg, a);
25452 +       if (!err)
25453 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25454 +
25455 +out:
25456 +       AuTraceErr(err);
25457 +       return err;
25458 +}
25459 +
25460 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25461 +{
25462 +       int err, e;
25463 +       unsigned char dmsg;
25464 +       struct au_mvd_args *args;
25465 +       struct inode *inode;
25466 +
25467 +       inode = d_inode(dentry);
25468 +       err = -EPERM;
25469 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25470 +               goto out;
25471 +
25472 +       err = -ENOMEM;
25473 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25474 +       if (unlikely(!args))
25475 +               goto out;
25476 +
25477 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25478 +       if (!err)
25479 +               /* VERIFY_WRITE */
25480 +               err = !access_ok(uarg, sizeof(*uarg));
25481 +       if (unlikely(err)) {
25482 +               err = -EFAULT;
25483 +               AuTraceErr(err);
25484 +               goto out_free;
25485 +       }
25486 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25487 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25488 +       args->mvdown.au_errno = 0;
25489 +       args->dentry = dentry;
25490 +       args->inode = inode;
25491 +       args->sb = dentry->d_sb;
25492 +
25493 +       err = -ENOENT;
25494 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25495 +       args->parent = dget_parent(dentry);
25496 +       args->dir = d_inode(args->parent);
25497 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25498 +       dput(args->parent);
25499 +       if (unlikely(args->parent != dentry->d_parent)) {
25500 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25501 +               goto out_dir;
25502 +       }
25503 +
25504 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25505 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25506 +       if (unlikely(err))
25507 +               goto out_inode;
25508 +
25509 +       di_write_lock_parent(args->parent);
25510 +       err = au_mvd_args(dmsg, args);
25511 +       if (unlikely(err))
25512 +               goto out_parent;
25513 +
25514 +       err = au_do_mvdown(dmsg, args);
25515 +       if (unlikely(err))
25516 +               goto out_parent;
25517 +
25518 +       au_cpup_attr_timesizes(args->dir);
25519 +       au_cpup_attr_timesizes(inode);
25520 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25521 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25522 +       /* au_digen_dec(dentry); */
25523 +
25524 +out_parent:
25525 +       di_write_unlock(args->parent);
25526 +       aufs_read_unlock(dentry, AuLock_DW);
25527 +out_inode:
25528 +       inode_unlock(inode);
25529 +out_dir:
25530 +       inode_unlock(args->dir);
25531 +out_free:
25532 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25533 +       if (unlikely(e))
25534 +               err = -EFAULT;
25535 +       au_kfree_rcu(args);
25536 +out:
25537 +       AuTraceErr(err);
25538 +       return err;
25539 +}
25540 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25541 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25542 +++ linux/fs/aufs/opts.c        2021-02-24 13:33:42.747680497 +0100
25543 @@ -0,0 +1,1880 @@
25544 +// SPDX-License-Identifier: GPL-2.0
25545 +/*
25546 + * Copyright (C) 2005-2020 Junjiro R. Okajima
25547 + *
25548 + * This program, aufs is free software; you can redistribute it and/or modify
25549 + * it under the terms of the GNU General Public License as published by
25550 + * the Free Software Foundation; either version 2 of the License, or
25551 + * (at your option) any later version.
25552 + *
25553 + * This program is distributed in the hope that it will be useful,
25554 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25555 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25556 + * GNU General Public License for more details.
25557 + *
25558 + * You should have received a copy of the GNU General Public License
25559 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25560 + */
25561 +
25562 +/*
25563 + * mount options/flags
25564 + */
25565 +
25566 +#include <linux/namei.h>
25567 +#include <linux/types.h> /* a distribution requires */
25568 +#include <linux/parser.h>
25569 +#include "aufs.h"
25570 +
25571 +/* ---------------------------------------------------------------------- */
25572 +
25573 +enum {
25574 +       Opt_br,
25575 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25576 +       Opt_idel, Opt_imod,
25577 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25578 +       Opt_rdblk_def, Opt_rdhash_def,
25579 +       Opt_xino, Opt_noxino,
25580 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25581 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25582 +       Opt_trunc_xib, Opt_notrunc_xib,
25583 +       Opt_shwh, Opt_noshwh,
25584 +       Opt_plink, Opt_noplink, Opt_list_plink,
25585 +       Opt_udba,
25586 +       Opt_dio, Opt_nodio,
25587 +       Opt_diropq_a, Opt_diropq_w,
25588 +       Opt_warn_perm, Opt_nowarn_perm,
25589 +       Opt_wbr_copyup, Opt_wbr_create,
25590 +       Opt_fhsm_sec,
25591 +       Opt_verbose, Opt_noverbose,
25592 +       Opt_sum, Opt_nosum, Opt_wsum,
25593 +       Opt_dirperm1, Opt_nodirperm1,
25594 +       Opt_dirren, Opt_nodirren,
25595 +       Opt_acl, Opt_noacl,
25596 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25597 +};
25598 +
25599 +static match_table_t options = {
25600 +       {Opt_br, "br=%s"},
25601 +       {Opt_br, "br:%s"},
25602 +
25603 +       {Opt_add, "add=%d:%s"},
25604 +       {Opt_add, "add:%d:%s"},
25605 +       {Opt_add, "ins=%d:%s"},
25606 +       {Opt_add, "ins:%d:%s"},
25607 +       {Opt_append, "append=%s"},
25608 +       {Opt_append, "append:%s"},
25609 +       {Opt_prepend, "prepend=%s"},
25610 +       {Opt_prepend, "prepend:%s"},
25611 +
25612 +       {Opt_del, "del=%s"},
25613 +       {Opt_del, "del:%s"},
25614 +       /* {Opt_idel, "idel:%d"}, */
25615 +       {Opt_mod, "mod=%s"},
25616 +       {Opt_mod, "mod:%s"},
25617 +       /* {Opt_imod, "imod:%d:%s"}, */
25618 +
25619 +       {Opt_dirwh, "dirwh=%d"},
25620 +
25621 +       {Opt_xino, "xino=%s"},
25622 +       {Opt_noxino, "noxino"},
25623 +       {Opt_trunc_xino, "trunc_xino"},
25624 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25625 +       {Opt_notrunc_xino, "notrunc_xino"},
25626 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25627 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25628 +       /* {Opt_zxino, "zxino=%s"}, */
25629 +       {Opt_trunc_xib, "trunc_xib"},
25630 +       {Opt_notrunc_xib, "notrunc_xib"},
25631 +
25632 +#ifdef CONFIG_PROC_FS
25633 +       {Opt_plink, "plink"},
25634 +#else
25635 +       {Opt_ignore_silent, "plink"},
25636 +#endif
25637 +
25638 +       {Opt_noplink, "noplink"},
25639 +
25640 +#ifdef CONFIG_AUFS_DEBUG
25641 +       {Opt_list_plink, "list_plink"},
25642 +#endif
25643 +
25644 +       {Opt_udba, "udba=%s"},
25645 +
25646 +       {Opt_dio, "dio"},
25647 +       {Opt_nodio, "nodio"},
25648 +
25649 +#ifdef CONFIG_AUFS_DIRREN
25650 +       {Opt_dirren, "dirren"},
25651 +       {Opt_nodirren, "nodirren"},
25652 +#else
25653 +       {Opt_ignore, "dirren"},
25654 +       {Opt_ignore_silent, "nodirren"},
25655 +#endif
25656 +
25657 +#ifdef CONFIG_AUFS_FHSM
25658 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25659 +#else
25660 +       {Opt_ignore, "fhsm_sec=%d"},
25661 +#endif
25662 +
25663 +       {Opt_diropq_a, "diropq=always"},
25664 +       {Opt_diropq_a, "diropq=a"},
25665 +       {Opt_diropq_w, "diropq=whiteouted"},
25666 +       {Opt_diropq_w, "diropq=w"},
25667 +
25668 +       {Opt_warn_perm, "warn_perm"},
25669 +       {Opt_nowarn_perm, "nowarn_perm"},
25670 +
25671 +       /* keep them temporary */
25672 +       {Opt_ignore_silent, "nodlgt"},
25673 +       {Opt_ignore, "clean_plink"},
25674 +
25675 +#ifdef CONFIG_AUFS_SHWH
25676 +       {Opt_shwh, "shwh"},
25677 +#endif
25678 +       {Opt_noshwh, "noshwh"},
25679 +
25680 +       {Opt_dirperm1, "dirperm1"},
25681 +       {Opt_nodirperm1, "nodirperm1"},
25682 +
25683 +       {Opt_verbose, "verbose"},
25684 +       {Opt_verbose, "v"},
25685 +       {Opt_noverbose, "noverbose"},
25686 +       {Opt_noverbose, "quiet"},
25687 +       {Opt_noverbose, "q"},
25688 +       {Opt_noverbose, "silent"},
25689 +
25690 +       {Opt_sum, "sum"},
25691 +       {Opt_nosum, "nosum"},
25692 +       {Opt_wsum, "wsum"},
25693 +
25694 +       {Opt_rdcache, "rdcache=%d"},
25695 +       {Opt_rdblk, "rdblk=%d"},
25696 +       {Opt_rdblk_def, "rdblk=def"},
25697 +       {Opt_rdhash, "rdhash=%d"},
25698 +       {Opt_rdhash_def, "rdhash=def"},
25699 +
25700 +       {Opt_wbr_create, "create=%s"},
25701 +       {Opt_wbr_create, "create_policy=%s"},
25702 +       {Opt_wbr_copyup, "cpup=%s"},
25703 +       {Opt_wbr_copyup, "copyup=%s"},
25704 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25705 +
25706 +       /* generic VFS flag */
25707 +#ifdef CONFIG_FS_POSIX_ACL
25708 +       {Opt_acl, "acl"},
25709 +       {Opt_noacl, "noacl"},
25710 +#else
25711 +       {Opt_ignore, "acl"},
25712 +       {Opt_ignore_silent, "noacl"},
25713 +#endif
25714 +
25715 +       /* internal use for the scripts */
25716 +       {Opt_ignore_silent, "si=%s"},
25717 +
25718 +       {Opt_br, "dirs=%s"},
25719 +       {Opt_ignore, "debug=%d"},
25720 +       {Opt_ignore, "delete=whiteout"},
25721 +       {Opt_ignore, "delete=all"},
25722 +       {Opt_ignore, "imap=%s"},
25723 +
25724 +       /* temporary workaround, due to old mount(8)? */
25725 +       {Opt_ignore_silent, "relatime"},
25726 +
25727 +       {Opt_err, NULL}
25728 +};
25729 +
25730 +/* ---------------------------------------------------------------------- */
25731 +
25732 +static const char *au_parser_pattern(int val, match_table_t tbl)
25733 +{
25734 +       struct match_token *p;
25735 +
25736 +       p = tbl;
25737 +       while (p->pattern) {
25738 +               if (p->token == val)
25739 +                       return p->pattern;
25740 +               p++;
25741 +       }
25742 +       BUG();
25743 +       return "??";
25744 +}
25745 +
25746 +static const char *au_optstr(int *val, match_table_t tbl)
25747 +{
25748 +       struct match_token *p;
25749 +       int v;
25750 +
25751 +       v = *val;
25752 +       if (!v)
25753 +               goto out;
25754 +       p = tbl;
25755 +       while (p->pattern) {
25756 +               if (p->token
25757 +                   && (v & p->token) == p->token) {
25758 +                       *val &= ~p->token;
25759 +                       return p->pattern;
25760 +               }
25761 +               p++;
25762 +       }
25763 +
25764 +out:
25765 +       return NULL;
25766 +}
25767 +
25768 +/* ---------------------------------------------------------------------- */
25769 +
25770 +static match_table_t brperm = {
25771 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25772 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25773 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25774 +       {0, NULL}
25775 +};
25776 +
25777 +static match_table_t brattr = {
25778 +       /* general */
25779 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25780 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25781 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25782 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25783 +#ifdef CONFIG_AUFS_FHSM
25784 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25785 +#endif
25786 +#ifdef CONFIG_AUFS_XATTR
25787 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25788 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25789 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25790 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25791 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25792 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25793 +#endif
25794 +
25795 +       /* ro/rr branch */
25796 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25797 +
25798 +       /* rw branch */
25799 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25800 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25801 +
25802 +       {0, NULL}
25803 +};
25804 +
25805 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25806 +{
25807 +       int attr, v;
25808 +       char *p;
25809 +
25810 +       attr = 0;
25811 +       do {
25812 +               p = strchr(str, '+');
25813 +               if (p)
25814 +                       *p = 0;
25815 +               v = match_token(str, table, args);
25816 +               if (v) {
25817 +                       if (v & AuBrAttr_CMOO_Mask)
25818 +                               attr &= ~AuBrAttr_CMOO_Mask;
25819 +                       attr |= v;
25820 +               } else {
25821 +                       if (p)
25822 +                               *p = '+';
25823 +                       pr_warn("ignored branch attribute %s\n", str);
25824 +                       break;
25825 +               }
25826 +               if (p)
25827 +                       str = p + 1;
25828 +       } while (p);
25829 +
25830 +       return attr;
25831 +}
25832 +
25833 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25834 +{
25835 +       int sz;
25836 +       const char *p;
25837 +       char *q;
25838 +
25839 +       q = str->a;
25840 +       *q = 0;
25841 +       p = au_optstr(&perm, brattr);
25842 +       if (p) {
25843 +               sz = strlen(p);
25844 +               memcpy(q, p, sz + 1);
25845 +               q += sz;
25846 +       } else
25847 +               goto out;
25848 +
25849 +       do {
25850 +               p = au_optstr(&perm, brattr);
25851 +               if (p) {
25852 +                       *q++ = '+';
25853 +                       sz = strlen(p);
25854 +                       memcpy(q, p, sz + 1);
25855 +                       q += sz;
25856 +               }
25857 +       } while (p);
25858 +
25859 +out:
25860 +       return q - str->a;
25861 +}
25862 +
25863 +static int noinline_for_stack br_perm_val(char *perm)
25864 +{
25865 +       int val, bad, sz;
25866 +       char *p;
25867 +       substring_t args[MAX_OPT_ARGS];
25868 +       au_br_perm_str_t attr;
25869 +
25870 +       p = strchr(perm, '+');
25871 +       if (p)
25872 +               *p = 0;
25873 +       val = match_token(perm, brperm, args);
25874 +       if (!val) {
25875 +               if (p)
25876 +                       *p = '+';
25877 +               pr_warn("ignored branch permission %s\n", perm);
25878 +               val = AuBrPerm_RO;
25879 +               goto out;
25880 +       }
25881 +       if (!p)
25882 +               goto out;
25883 +
25884 +       val |= br_attr_val(p + 1, brattr, args);
25885 +
25886 +       bad = 0;
25887 +       switch (val & AuBrPerm_Mask) {
25888 +       case AuBrPerm_RO:
25889 +       case AuBrPerm_RR:
25890 +               bad = val & AuBrWAttr_Mask;
25891 +               val &= ~AuBrWAttr_Mask;
25892 +               break;
25893 +       case AuBrPerm_RW:
25894 +               bad = val & AuBrRAttr_Mask;
25895 +               val &= ~AuBrRAttr_Mask;
25896 +               break;
25897 +       }
25898 +
25899 +       /*
25900 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25901 +        * does not treat it as an error, just warning.
25902 +        * this is a tiny guard for the user operation.
25903 +        */
25904 +       if (val & AuBrAttr_UNPIN) {
25905 +               bad |= AuBrAttr_UNPIN;
25906 +               val &= ~AuBrAttr_UNPIN;
25907 +       }
25908 +
25909 +       if (unlikely(bad)) {
25910 +               sz = au_do_optstr_br_attr(&attr, bad);
25911 +               AuDebugOn(!sz);
25912 +               pr_warn("ignored branch attribute %s\n", attr.a);
25913 +       }
25914 +
25915 +out:
25916 +       return val;
25917 +}
25918 +
25919 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25920 +{
25921 +       au_br_perm_str_t attr;
25922 +       const char *p;
25923 +       char *q;
25924 +       int sz;
25925 +
25926 +       q = str->a;
25927 +       p = au_optstr(&perm, brperm);
25928 +       AuDebugOn(!p || !*p);
25929 +       sz = strlen(p);
25930 +       memcpy(q, p, sz + 1);
25931 +       q += sz;
25932 +
25933 +       sz = au_do_optstr_br_attr(&attr, perm);
25934 +       if (sz) {
25935 +               *q++ = '+';
25936 +               memcpy(q, attr.a, sz + 1);
25937 +       }
25938 +
25939 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
25940 +}
25941 +
25942 +/* ---------------------------------------------------------------------- */
25943 +
25944 +static match_table_t udbalevel = {
25945 +       {AuOpt_UDBA_REVAL, "reval"},
25946 +       {AuOpt_UDBA_NONE, "none"},
25947 +#ifdef CONFIG_AUFS_HNOTIFY
25948 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25949 +#ifdef CONFIG_AUFS_HFSNOTIFY
25950 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
25951 +#endif
25952 +#endif
25953 +       {-1, NULL}
25954 +};
25955 +
25956 +static int noinline_for_stack udba_val(char *str)
25957 +{
25958 +       substring_t args[MAX_OPT_ARGS];
25959 +
25960 +       return match_token(str, udbalevel, args);
25961 +}
25962 +
25963 +const char *au_optstr_udba(int udba)
25964 +{
25965 +       return au_parser_pattern(udba, udbalevel);
25966 +}
25967 +
25968 +/* ---------------------------------------------------------------------- */
25969 +
25970 +static match_table_t au_wbr_create_policy = {
25971 +       {AuWbrCreate_TDP, "tdp"},
25972 +       {AuWbrCreate_TDP, "top-down-parent"},
25973 +       {AuWbrCreate_RR, "rr"},
25974 +       {AuWbrCreate_RR, "round-robin"},
25975 +       {AuWbrCreate_MFS, "mfs"},
25976 +       {AuWbrCreate_MFS, "most-free-space"},
25977 +       {AuWbrCreate_MFSV, "mfs:%d"},
25978 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
25979 +
25980 +       /* top-down regardless the parent, and then mfs */
25981 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
25982 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25983 +
25984 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
25985 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25986 +       {AuWbrCreate_PMFS, "pmfs"},
25987 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
25988 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
25989 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
25990 +
25991 +       {-1, NULL}
25992 +};
25993 +
25994 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
25995 +                           struct au_opt_wbr_create *create)
25996 +{
25997 +       int err;
25998 +       unsigned long long ull;
25999 +
26000 +       err = 0;
26001 +       if (!match_u64(arg, &ull))
26002 +               create->mfsrr_watermark = ull;
26003 +       else {
26004 +               pr_err("bad integer in %s\n", str);
26005 +               err = -EINVAL;
26006 +       }
26007 +
26008 +       return err;
26009 +}
26010 +
26011 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26012 +                         struct au_opt_wbr_create *create)
26013 +{
26014 +       int n, err;
26015 +
26016 +       err = 0;
26017 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26018 +               create->mfs_second = n;
26019 +       else {
26020 +               pr_err("bad integer in %s\n", str);
26021 +               err = -EINVAL;
26022 +       }
26023 +
26024 +       return err;
26025 +}
26026 +
26027 +static int noinline_for_stack
26028 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26029 +{
26030 +       int err, e;
26031 +       substring_t args[MAX_OPT_ARGS];
26032 +
26033 +       err = match_token(str, au_wbr_create_policy, args);
26034 +       create->wbr_create = err;
26035 +       switch (err) {
26036 +       case AuWbrCreate_MFSRRV:
26037 +       case AuWbrCreate_TDMFSV:
26038 +       case AuWbrCreate_PMFSRRV:
26039 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26040 +               if (!e)
26041 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26042 +               if (unlikely(e))
26043 +                       err = e;
26044 +               break;
26045 +       case AuWbrCreate_MFSRR:
26046 +       case AuWbrCreate_TDMFS:
26047 +       case AuWbrCreate_PMFSRR:
26048 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26049 +               if (unlikely(e)) {
26050 +                       err = e;
26051 +                       break;
26052 +               }
26053 +               fallthrough;
26054 +       case AuWbrCreate_MFS:
26055 +       case AuWbrCreate_PMFS:
26056 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26057 +               break;
26058 +       case AuWbrCreate_MFSV:
26059 +       case AuWbrCreate_PMFSV:
26060 +               e = au_wbr_mfs_sec(&args[0], str, create);
26061 +               if (unlikely(e))
26062 +                       err = e;
26063 +               break;
26064 +       }
26065 +
26066 +       return err;
26067 +}
26068 +
26069 +const char *au_optstr_wbr_create(int wbr_create)
26070 +{
26071 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26072 +}
26073 +
26074 +static match_table_t au_wbr_copyup_policy = {
26075 +       {AuWbrCopyup_TDP, "tdp"},
26076 +       {AuWbrCopyup_TDP, "top-down-parent"},
26077 +       {AuWbrCopyup_BUP, "bup"},
26078 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26079 +       {AuWbrCopyup_BU, "bu"},
26080 +       {AuWbrCopyup_BU, "bottom-up"},
26081 +       {-1, NULL}
26082 +};
26083 +
26084 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26085 +{
26086 +       substring_t args[MAX_OPT_ARGS];
26087 +
26088 +       return match_token(str, au_wbr_copyup_policy, args);
26089 +}
26090 +
26091 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26092 +{
26093 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26094 +}
26095 +
26096 +/* ---------------------------------------------------------------------- */
26097 +
26098 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26099 +
26100 +static void dump_opts(struct au_opts *opts)
26101 +{
26102 +#ifdef CONFIG_AUFS_DEBUG
26103 +       /* reduce stack space */
26104 +       union {
26105 +               struct au_opt_add *add;
26106 +               struct au_opt_del *del;
26107 +               struct au_opt_mod *mod;
26108 +               struct au_opt_xino *xino;
26109 +               struct au_opt_xino_itrunc *xino_itrunc;
26110 +               struct au_opt_wbr_create *create;
26111 +       } u;
26112 +       struct au_opt *opt;
26113 +
26114 +       opt = opts->opt;
26115 +       while (opt->type != Opt_tail) {
26116 +               switch (opt->type) {
26117 +               case Opt_add:
26118 +                       u.add = &opt->add;
26119 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26120 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26121 +                                 u.add->path.dentry);
26122 +                       break;
26123 +               case Opt_del:
26124 +               case Opt_idel:
26125 +                       u.del = &opt->del;
26126 +                       AuDbg("del {%s, %p}\n",
26127 +                             u.del->pathname, u.del->h_path.dentry);
26128 +                       break;
26129 +               case Opt_mod:
26130 +               case Opt_imod:
26131 +                       u.mod = &opt->mod;
26132 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26133 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26134 +                       break;
26135 +               case Opt_append:
26136 +                       u.add = &opt->add;
26137 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26138 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26139 +                                 u.add->path.dentry);
26140 +                       break;
26141 +               case Opt_prepend:
26142 +                       u.add = &opt->add;
26143 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26144 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26145 +                                 u.add->path.dentry);
26146 +                       break;
26147 +               case Opt_dirwh:
26148 +                       AuDbg("dirwh %d\n", opt->dirwh);
26149 +                       break;
26150 +               case Opt_rdcache:
26151 +                       AuDbg("rdcache %d\n", opt->rdcache);
26152 +                       break;
26153 +               case Opt_rdblk:
26154 +                       AuDbg("rdblk %u\n", opt->rdblk);
26155 +                       break;
26156 +               case Opt_rdblk_def:
26157 +                       AuDbg("rdblk_def\n");
26158 +                       break;
26159 +               case Opt_rdhash:
26160 +                       AuDbg("rdhash %u\n", opt->rdhash);
26161 +                       break;
26162 +               case Opt_rdhash_def:
26163 +                       AuDbg("rdhash_def\n");
26164 +                       break;
26165 +               case Opt_xino:
26166 +                       u.xino = &opt->xino;
26167 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26168 +                       break;
26169 +               case Opt_trunc_xino:
26170 +                       AuLabel(trunc_xino);
26171 +                       break;
26172 +               case Opt_notrunc_xino:
26173 +                       AuLabel(notrunc_xino);
26174 +                       break;
26175 +               case Opt_trunc_xino_path:
26176 +               case Opt_itrunc_xino:
26177 +                       u.xino_itrunc = &opt->xino_itrunc;
26178 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26179 +                       break;
26180 +               case Opt_noxino:
26181 +                       AuLabel(noxino);
26182 +                       break;
26183 +               case Opt_trunc_xib:
26184 +                       AuLabel(trunc_xib);
26185 +                       break;
26186 +               case Opt_notrunc_xib:
26187 +                       AuLabel(notrunc_xib);
26188 +                       break;
26189 +               case Opt_shwh:
26190 +                       AuLabel(shwh);
26191 +                       break;
26192 +               case Opt_noshwh:
26193 +                       AuLabel(noshwh);
26194 +                       break;
26195 +               case Opt_dirperm1:
26196 +                       AuLabel(dirperm1);
26197 +                       break;
26198 +               case Opt_nodirperm1:
26199 +                       AuLabel(nodirperm1);
26200 +                       break;
26201 +               case Opt_plink:
26202 +                       AuLabel(plink);
26203 +                       break;
26204 +               case Opt_noplink:
26205 +                       AuLabel(noplink);
26206 +                       break;
26207 +               case Opt_list_plink:
26208 +                       AuLabel(list_plink);
26209 +                       break;
26210 +               case Opt_udba:
26211 +                       AuDbg("udba %d, %s\n",
26212 +                                 opt->udba, au_optstr_udba(opt->udba));
26213 +                       break;
26214 +               case Opt_dio:
26215 +                       AuLabel(dio);
26216 +                       break;
26217 +               case Opt_nodio:
26218 +                       AuLabel(nodio);
26219 +                       break;
26220 +               case Opt_diropq_a:
26221 +                       AuLabel(diropq_a);
26222 +                       break;
26223 +               case Opt_diropq_w:
26224 +                       AuLabel(diropq_w);
26225 +                       break;
26226 +               case Opt_warn_perm:
26227 +                       AuLabel(warn_perm);
26228 +                       break;
26229 +               case Opt_nowarn_perm:
26230 +                       AuLabel(nowarn_perm);
26231 +                       break;
26232 +               case Opt_verbose:
26233 +                       AuLabel(verbose);
26234 +                       break;
26235 +               case Opt_noverbose:
26236 +                       AuLabel(noverbose);
26237 +                       break;
26238 +               case Opt_sum:
26239 +                       AuLabel(sum);
26240 +                       break;
26241 +               case Opt_nosum:
26242 +                       AuLabel(nosum);
26243 +                       break;
26244 +               case Opt_wsum:
26245 +                       AuLabel(wsum);
26246 +                       break;
26247 +               case Opt_wbr_create:
26248 +                       u.create = &opt->wbr_create;
26249 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26250 +                                 au_optstr_wbr_create(u.create->wbr_create));
26251 +                       switch (u.create->wbr_create) {
26252 +                       case AuWbrCreate_MFSV:
26253 +                       case AuWbrCreate_PMFSV:
26254 +                               AuDbg("%d sec\n", u.create->mfs_second);
26255 +                               break;
26256 +                       case AuWbrCreate_MFSRR:
26257 +                       case AuWbrCreate_TDMFS:
26258 +                               AuDbg("%llu watermark\n",
26259 +                                         u.create->mfsrr_watermark);
26260 +                               break;
26261 +                       case AuWbrCreate_MFSRRV:
26262 +                       case AuWbrCreate_TDMFSV:
26263 +                       case AuWbrCreate_PMFSRRV:
26264 +                               AuDbg("%llu watermark, %d sec\n",
26265 +                                         u.create->mfsrr_watermark,
26266 +                                         u.create->mfs_second);
26267 +                               break;
26268 +                       }
26269 +                       break;
26270 +               case Opt_wbr_copyup:
26271 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26272 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26273 +                       break;
26274 +               case Opt_fhsm_sec:
26275 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26276 +                       break;
26277 +               case Opt_dirren:
26278 +                       AuLabel(dirren);
26279 +                       break;
26280 +               case Opt_nodirren:
26281 +                       AuLabel(nodirren);
26282 +                       break;
26283 +               case Opt_acl:
26284 +                       AuLabel(acl);
26285 +                       break;
26286 +               case Opt_noacl:
26287 +                       AuLabel(noacl);
26288 +                       break;
26289 +               default:
26290 +                       BUG();
26291 +               }
26292 +               opt++;
26293 +       }
26294 +#endif
26295 +}
26296 +
26297 +void au_opts_free(struct au_opts *opts)
26298 +{
26299 +       struct au_opt *opt;
26300 +
26301 +       opt = opts->opt;
26302 +       while (opt->type != Opt_tail) {
26303 +               switch (opt->type) {
26304 +               case Opt_add:
26305 +               case Opt_append:
26306 +               case Opt_prepend:
26307 +                       path_put(&opt->add.path);
26308 +                       break;
26309 +               case Opt_del:
26310 +               case Opt_idel:
26311 +                       path_put(&opt->del.h_path);
26312 +                       break;
26313 +               case Opt_mod:
26314 +               case Opt_imod:
26315 +                       dput(opt->mod.h_root);
26316 +                       break;
26317 +               case Opt_xino:
26318 +                       fput(opt->xino.file);
26319 +                       break;
26320 +               }
26321 +               opt++;
26322 +       }
26323 +}
26324 +
26325 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26326 +                  aufs_bindex_t bindex)
26327 +{
26328 +       int err;
26329 +       struct au_opt_add *add = &opt->add;
26330 +       char *p;
26331 +
26332 +       add->bindex = bindex;
26333 +       add->perm = AuBrPerm_RO;
26334 +       add->pathname = opt_str;
26335 +       p = strchr(opt_str, '=');
26336 +       if (p) {
26337 +               *p++ = 0;
26338 +               if (*p)
26339 +                       add->perm = br_perm_val(p);
26340 +       }
26341 +
26342 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26343 +       if (!err) {
26344 +               if (!p) {
26345 +                       add->perm = AuBrPerm_RO;
26346 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26347 +                               add->perm = AuBrPerm_RR;
26348 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26349 +                               add->perm = AuBrPerm_RW;
26350 +               }
26351 +               opt->type = Opt_add;
26352 +               goto out;
26353 +       }
26354 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26355 +       err = -EINVAL;
26356 +
26357 +out:
26358 +       return err;
26359 +}
26360 +
26361 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26362 +{
26363 +       int err;
26364 +
26365 +       del->pathname = args[0].from;
26366 +       AuDbg("del path %s\n", del->pathname);
26367 +
26368 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26369 +       if (unlikely(err))
26370 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26371 +
26372 +       return err;
26373 +}
26374 +
26375 +#if 0 /* reserved for future use */
26376 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26377 +                             struct au_opt_del *del, substring_t args[])
26378 +{
26379 +       int err;
26380 +       struct dentry *root;
26381 +
26382 +       err = -EINVAL;
26383 +       root = sb->s_root;
26384 +       aufs_read_lock(root, AuLock_FLUSH);
26385 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26386 +               pr_err("out of bounds, %d\n", bindex);
26387 +               goto out;
26388 +       }
26389 +
26390 +       err = 0;
26391 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26392 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26393 +
26394 +out:
26395 +       aufs_read_unlock(root, !AuLock_IR);
26396 +       return err;
26397 +}
26398 +#endif
26399 +
26400 +static int noinline_for_stack
26401 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26402 +{
26403 +       int err;
26404 +       struct path path;
26405 +       char *p;
26406 +
26407 +       err = -EINVAL;
26408 +       mod->path = args[0].from;
26409 +       p = strchr(mod->path, '=');
26410 +       if (unlikely(!p)) {
26411 +               pr_err("no permission %s\n", args[0].from);
26412 +               goto out;
26413 +       }
26414 +
26415 +       *p++ = 0;
26416 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26417 +       if (unlikely(err)) {
26418 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26419 +               goto out;
26420 +       }
26421 +
26422 +       mod->perm = br_perm_val(p);
26423 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26424 +       mod->h_root = dget(path.dentry);
26425 +       path_put(&path);
26426 +
26427 +out:
26428 +       return err;
26429 +}
26430 +
26431 +#if 0 /* reserved for future use */
26432 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26433 +                             struct au_opt_mod *mod, substring_t args[])
26434 +{
26435 +       int err;
26436 +       struct dentry *root;
26437 +
26438 +       err = -EINVAL;
26439 +       root = sb->s_root;
26440 +       aufs_read_lock(root, AuLock_FLUSH);
26441 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26442 +               pr_err("out of bounds, %d\n", bindex);
26443 +               goto out;
26444 +       }
26445 +
26446 +       err = 0;
26447 +       mod->perm = br_perm_val(args[1].from);
26448 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26449 +             mod->path, mod->perm, args[1].from);
26450 +       mod->h_root = dget(au_h_dptr(root, bindex));
26451 +
26452 +out:
26453 +       aufs_read_unlock(root, !AuLock_IR);
26454 +       return err;
26455 +}
26456 +#endif
26457 +
26458 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26459 +                             substring_t args[])
26460 +{
26461 +       int err;
26462 +       struct file *file;
26463 +
26464 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26465 +       err = PTR_ERR(file);
26466 +       if (IS_ERR(file))
26467 +               goto out;
26468 +
26469 +       err = -EINVAL;
26470 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26471 +               fput(file);
26472 +               pr_err("%s must be outside\n", args[0].from);
26473 +               goto out;
26474 +       }
26475 +
26476 +       err = 0;
26477 +       xino->file = file;
26478 +       xino->path = args[0].from;
26479 +
26480 +out:
26481 +       return err;
26482 +}
26483 +
26484 +static int noinline_for_stack
26485 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26486 +                              struct au_opt_xino_itrunc *xino_itrunc,
26487 +                              substring_t args[])
26488 +{
26489 +       int err;
26490 +       aufs_bindex_t bbot, bindex;
26491 +       struct path path;
26492 +       struct dentry *root;
26493 +
26494 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26495 +       if (unlikely(err)) {
26496 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26497 +               goto out;
26498 +       }
26499 +
26500 +       xino_itrunc->bindex = -1;
26501 +       root = sb->s_root;
26502 +       aufs_read_lock(root, AuLock_FLUSH);
26503 +       bbot = au_sbbot(sb);
26504 +       for (bindex = 0; bindex <= bbot; bindex++) {
26505 +               if (au_h_dptr(root, bindex) == path.dentry) {
26506 +                       xino_itrunc->bindex = bindex;
26507 +                       break;
26508 +               }
26509 +       }
26510 +       aufs_read_unlock(root, !AuLock_IR);
26511 +       path_put(&path);
26512 +
26513 +       if (unlikely(xino_itrunc->bindex < 0)) {
26514 +               pr_err("no such branch %s\n", args[0].from);
26515 +               err = -EINVAL;
26516 +       }
26517 +
26518 +out:
26519 +       return err;
26520 +}
26521 +
26522 +/* called without aufs lock */
26523 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26524 +{
26525 +       int err, n, token;
26526 +       aufs_bindex_t bindex;
26527 +       unsigned char skipped;
26528 +       struct dentry *root;
26529 +       struct au_opt *opt, *opt_tail;
26530 +       char *opt_str;
26531 +       /* reduce the stack space */
26532 +       union {
26533 +               struct au_opt_xino_itrunc *xino_itrunc;
26534 +               struct au_opt_wbr_create *create;
26535 +       } u;
26536 +       struct {
26537 +               substring_t args[MAX_OPT_ARGS];
26538 +       } *a;
26539 +
26540 +       err = -ENOMEM;
26541 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26542 +       if (unlikely(!a))
26543 +               goto out;
26544 +
26545 +       root = sb->s_root;
26546 +       err = 0;
26547 +       bindex = 0;
26548 +       opt = opts->opt;
26549 +       opt_tail = opt + opts->max_opt - 1;
26550 +       opt->type = Opt_tail;
26551 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26552 +               err = -EINVAL;
26553 +               skipped = 0;
26554 +               token = match_token(opt_str, options, a->args);
26555 +               switch (token) {
26556 +               case Opt_br:
26557 +                       err = 0;
26558 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26559 +                              && *opt_str) {
26560 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26561 +                                             bindex++);
26562 +                               if (unlikely(!err && ++opt > opt_tail)) {
26563 +                                       err = -E2BIG;
26564 +                                       break;
26565 +                               }
26566 +                               opt->type = Opt_tail;
26567 +                               skipped = 1;
26568 +                       }
26569 +                       break;
26570 +               case Opt_add:
26571 +                       if (unlikely(match_int(&a->args[0], &n))) {
26572 +                               pr_err("bad integer in %s\n", opt_str);
26573 +                               break;
26574 +                       }
26575 +                       bindex = n;
26576 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26577 +                                     bindex);
26578 +                       if (!err)
26579 +                               opt->type = token;
26580 +                       break;
26581 +               case Opt_append:
26582 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26583 +                                     /*dummy bindex*/1);
26584 +                       if (!err)
26585 +                               opt->type = token;
26586 +                       break;
26587 +               case Opt_prepend:
26588 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26589 +                                     /*bindex*/0);
26590 +                       if (!err)
26591 +                               opt->type = token;
26592 +                       break;
26593 +               case Opt_del:
26594 +                       err = au_opts_parse_del(&opt->del, a->args);
26595 +                       if (!err)
26596 +                               opt->type = token;
26597 +                       break;
26598 +#if 0 /* reserved for future use */
26599 +               case Opt_idel:
26600 +                       del->pathname = "(indexed)";
26601 +                       if (unlikely(match_int(&args[0], &n))) {
26602 +                               pr_err("bad integer in %s\n", opt_str);
26603 +                               break;
26604 +                       }
26605 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26606 +                       if (!err)
26607 +                               opt->type = token;
26608 +                       break;
26609 +#endif
26610 +               case Opt_mod:
26611 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26612 +                       if (!err)
26613 +                               opt->type = token;
26614 +                       break;
26615 +#ifdef IMOD /* reserved for future use */
26616 +               case Opt_imod:
26617 +                       u.mod->path = "(indexed)";
26618 +                       if (unlikely(match_int(&a->args[0], &n))) {
26619 +                               pr_err("bad integer in %s\n", opt_str);
26620 +                               break;
26621 +                       }
26622 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26623 +                       if (!err)
26624 +                               opt->type = token;
26625 +                       break;
26626 +#endif
26627 +               case Opt_xino:
26628 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26629 +                       if (!err)
26630 +                               opt->type = token;
26631 +                       break;
26632 +
26633 +               case Opt_trunc_xino_path:
26634 +                       err = au_opts_parse_xino_itrunc_path
26635 +                               (sb, &opt->xino_itrunc, a->args);
26636 +                       if (!err)
26637 +                               opt->type = token;
26638 +                       break;
26639 +
26640 +               case Opt_itrunc_xino:
26641 +                       u.xino_itrunc = &opt->xino_itrunc;
26642 +                       if (unlikely(match_int(&a->args[0], &n))) {
26643 +                               pr_err("bad integer in %s\n", opt_str);
26644 +                               break;
26645 +                       }
26646 +                       u.xino_itrunc->bindex = n;
26647 +                       aufs_read_lock(root, AuLock_FLUSH);
26648 +                       if (n < 0 || au_sbbot(sb) < n) {
26649 +                               pr_err("out of bounds, %d\n", n);
26650 +                               aufs_read_unlock(root, !AuLock_IR);
26651 +                               break;
26652 +                       }
26653 +                       aufs_read_unlock(root, !AuLock_IR);
26654 +                       err = 0;
26655 +                       opt->type = token;
26656 +                       break;
26657 +
26658 +               case Opt_dirwh:
26659 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26660 +                               break;
26661 +                       err = 0;
26662 +                       opt->type = token;
26663 +                       break;
26664 +
26665 +               case Opt_rdcache:
26666 +                       if (unlikely(match_int(&a->args[0], &n))) {
26667 +                               pr_err("bad integer in %s\n", opt_str);
26668 +                               break;
26669 +                       }
26670 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26671 +                               pr_err("rdcache must be smaller than %d\n",
26672 +                                      AUFS_RDCACHE_MAX);
26673 +                               break;
26674 +                       }
26675 +                       opt->rdcache = n;
26676 +                       err = 0;
26677 +                       opt->type = token;
26678 +                       break;
26679 +               case Opt_rdblk:
26680 +                       if (unlikely(match_int(&a->args[0], &n)
26681 +                                    || n < 0
26682 +                                    || n > KMALLOC_MAX_SIZE)) {
26683 +                               pr_err("bad integer in %s\n", opt_str);
26684 +                               break;
26685 +                       }
26686 +                       if (unlikely(n && n < NAME_MAX)) {
26687 +                               pr_err("rdblk must be larger than %d\n",
26688 +                                      NAME_MAX);
26689 +                               break;
26690 +                       }
26691 +                       opt->rdblk = n;
26692 +                       err = 0;
26693 +                       opt->type = token;
26694 +                       break;
26695 +               case Opt_rdhash:
26696 +                       if (unlikely(match_int(&a->args[0], &n)
26697 +                                    || n < 0
26698 +                                    || n * sizeof(struct hlist_head)
26699 +                                    > KMALLOC_MAX_SIZE)) {
26700 +                               pr_err("bad integer in %s\n", opt_str);
26701 +                               break;
26702 +                       }
26703 +                       opt->rdhash = n;
26704 +                       err = 0;
26705 +                       opt->type = token;
26706 +                       break;
26707 +
26708 +               case Opt_trunc_xino:
26709 +               case Opt_notrunc_xino:
26710 +               case Opt_noxino:
26711 +               case Opt_trunc_xib:
26712 +               case Opt_notrunc_xib:
26713 +               case Opt_shwh:
26714 +               case Opt_noshwh:
26715 +               case Opt_dirperm1:
26716 +               case Opt_nodirperm1:
26717 +               case Opt_plink:
26718 +               case Opt_noplink:
26719 +               case Opt_list_plink:
26720 +               case Opt_dio:
26721 +               case Opt_nodio:
26722 +               case Opt_diropq_a:
26723 +               case Opt_diropq_w:
26724 +               case Opt_warn_perm:
26725 +               case Opt_nowarn_perm:
26726 +               case Opt_verbose:
26727 +               case Opt_noverbose:
26728 +               case Opt_sum:
26729 +               case Opt_nosum:
26730 +               case Opt_wsum:
26731 +               case Opt_rdblk_def:
26732 +               case Opt_rdhash_def:
26733 +               case Opt_dirren:
26734 +               case Opt_nodirren:
26735 +               case Opt_acl:
26736 +               case Opt_noacl:
26737 +                       err = 0;
26738 +                       opt->type = token;
26739 +                       break;
26740 +
26741 +               case Opt_udba:
26742 +                       opt->udba = udba_val(a->args[0].from);
26743 +                       if (opt->udba >= 0) {
26744 +                               err = 0;
26745 +                               opt->type = token;
26746 +                       } else
26747 +                               pr_err("wrong value, %s\n", opt_str);
26748 +                       break;
26749 +
26750 +               case Opt_wbr_create:
26751 +                       u.create = &opt->wbr_create;
26752 +                       u.create->wbr_create
26753 +                               = au_wbr_create_val(a->args[0].from, u.create);
26754 +                       if (u.create->wbr_create >= 0) {
26755 +                               err = 0;
26756 +                               opt->type = token;
26757 +                       } else
26758 +                               pr_err("wrong value, %s\n", opt_str);
26759 +                       break;
26760 +               case Opt_wbr_copyup:
26761 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26762 +                       if (opt->wbr_copyup >= 0) {
26763 +                               err = 0;
26764 +                               opt->type = token;
26765 +                       } else
26766 +                               pr_err("wrong value, %s\n", opt_str);
26767 +                       break;
26768 +
26769 +               case Opt_fhsm_sec:
26770 +                       if (unlikely(match_int(&a->args[0], &n)
26771 +                                    || n < 0)) {
26772 +                               pr_err("bad integer in %s\n", opt_str);
26773 +                               break;
26774 +                       }
26775 +                       if (sysaufs_brs) {
26776 +                               opt->fhsm_second = n;
26777 +                               opt->type = token;
26778 +                       } else
26779 +                               pr_warn("ignored %s\n", opt_str);
26780 +                       err = 0;
26781 +                       break;
26782 +
26783 +               case Opt_ignore:
26784 +                       pr_warn("ignored %s\n", opt_str);
26785 +                       fallthrough;
26786 +               case Opt_ignore_silent:
26787 +                       skipped = 1;
26788 +                       err = 0;
26789 +                       break;
26790 +               case Opt_err:
26791 +                       pr_err("unknown option %s\n", opt_str);
26792 +                       break;
26793 +               }
26794 +
26795 +               if (!err && !skipped) {
26796 +                       if (unlikely(++opt > opt_tail)) {
26797 +                               err = -E2BIG;
26798 +                               opt--;
26799 +                               opt->type = Opt_tail;
26800 +                               break;
26801 +                       }
26802 +                       opt->type = Opt_tail;
26803 +               }
26804 +       }
26805 +
26806 +       au_kfree_rcu(a);
26807 +       dump_opts(opts);
26808 +       if (unlikely(err))
26809 +               au_opts_free(opts);
26810 +
26811 +out:
26812 +       return err;
26813 +}
26814 +
26815 +static int au_opt_wbr_create(struct super_block *sb,
26816 +                            struct au_opt_wbr_create *create)
26817 +{
26818 +       int err;
26819 +       struct au_sbinfo *sbinfo;
26820 +
26821 +       SiMustWriteLock(sb);
26822 +
26823 +       err = 1; /* handled */
26824 +       sbinfo = au_sbi(sb);
26825 +       if (sbinfo->si_wbr_create_ops->fin) {
26826 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26827 +               if (!err)
26828 +                       err = 1;
26829 +       }
26830 +
26831 +       sbinfo->si_wbr_create = create->wbr_create;
26832 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26833 +       switch (create->wbr_create) {
26834 +       case AuWbrCreate_MFSRRV:
26835 +       case AuWbrCreate_MFSRR:
26836 +       case AuWbrCreate_TDMFS:
26837 +       case AuWbrCreate_TDMFSV:
26838 +       case AuWbrCreate_PMFSRR:
26839 +       case AuWbrCreate_PMFSRRV:
26840 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26841 +               fallthrough;
26842 +       case AuWbrCreate_MFS:
26843 +       case AuWbrCreate_MFSV:
26844 +       case AuWbrCreate_PMFS:
26845 +       case AuWbrCreate_PMFSV:
26846 +               sbinfo->si_wbr_mfs.mfs_expire
26847 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26848 +               break;
26849 +       }
26850 +
26851 +       if (sbinfo->si_wbr_create_ops->init)
26852 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26853 +
26854 +       return err;
26855 +}
26856 +
26857 +/*
26858 + * returns,
26859 + * plus: processed without an error
26860 + * zero: unprocessed
26861 + */
26862 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26863 +                        struct au_opts *opts)
26864 +{
26865 +       int err;
26866 +       struct au_sbinfo *sbinfo;
26867 +
26868 +       SiMustWriteLock(sb);
26869 +
26870 +       err = 1; /* handled */
26871 +       sbinfo = au_sbi(sb);
26872 +       switch (opt->type) {
26873 +       case Opt_udba:
26874 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26875 +               sbinfo->si_mntflags |= opt->udba;
26876 +               opts->given_udba |= opt->udba;
26877 +               break;
26878 +
26879 +       case Opt_plink:
26880 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26881 +               break;
26882 +       case Opt_noplink:
26883 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26884 +                       au_plink_put(sb, /*verbose*/1);
26885 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26886 +               break;
26887 +       case Opt_list_plink:
26888 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26889 +                       au_plink_list(sb);
26890 +               break;
26891 +
26892 +       case Opt_dio:
26893 +               au_opt_set(sbinfo->si_mntflags, DIO);
26894 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26895 +               break;
26896 +       case Opt_nodio:
26897 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26898 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26899 +               break;
26900 +
26901 +       case Opt_fhsm_sec:
26902 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26903 +               break;
26904 +
26905 +       case Opt_diropq_a:
26906 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26907 +               break;
26908 +       case Opt_diropq_w:
26909 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26910 +               break;
26911 +
26912 +       case Opt_warn_perm:
26913 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26914 +               break;
26915 +       case Opt_nowarn_perm:
26916 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26917 +               break;
26918 +
26919 +       case Opt_verbose:
26920 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
26921 +               break;
26922 +       case Opt_noverbose:
26923 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26924 +               break;
26925 +
26926 +       case Opt_sum:
26927 +               au_opt_set(sbinfo->si_mntflags, SUM);
26928 +               break;
26929 +       case Opt_wsum:
26930 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26931 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
26932 +               break;
26933 +       case Opt_nosum:
26934 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26935 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
26936 +               break;
26937 +
26938 +       case Opt_wbr_create:
26939 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
26940 +               break;
26941 +       case Opt_wbr_copyup:
26942 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
26943 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26944 +               break;
26945 +
26946 +       case Opt_dirwh:
26947 +               sbinfo->si_dirwh = opt->dirwh;
26948 +               break;
26949 +
26950 +       case Opt_rdcache:
26951 +               sbinfo->si_rdcache
26952 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26953 +               break;
26954 +       case Opt_rdblk:
26955 +               sbinfo->si_rdblk = opt->rdblk;
26956 +               break;
26957 +       case Opt_rdblk_def:
26958 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26959 +               break;
26960 +       case Opt_rdhash:
26961 +               sbinfo->si_rdhash = opt->rdhash;
26962 +               break;
26963 +       case Opt_rdhash_def:
26964 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26965 +               break;
26966 +
26967 +       case Opt_shwh:
26968 +               au_opt_set(sbinfo->si_mntflags, SHWH);
26969 +               break;
26970 +       case Opt_noshwh:
26971 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
26972 +               break;
26973 +
26974 +       case Opt_dirperm1:
26975 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26976 +               break;
26977 +       case Opt_nodirperm1:
26978 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26979 +               break;
26980 +
26981 +       case Opt_trunc_xino:
26982 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26983 +               break;
26984 +       case Opt_notrunc_xino:
26985 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26986 +               break;
26987 +
26988 +       case Opt_trunc_xino_path:
26989 +       case Opt_itrunc_xino:
26990 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
26991 +                                   /*idx_begin*/0);
26992 +               if (!err)
26993 +                       err = 1;
26994 +               break;
26995 +
26996 +       case Opt_trunc_xib:
26997 +               au_fset_opts(opts->flags, TRUNC_XIB);
26998 +               break;
26999 +       case Opt_notrunc_xib:
27000 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27001 +               break;
27002 +
27003 +       case Opt_dirren:
27004 +               err = 1;
27005 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27006 +                       err = au_dr_opt_set(sb);
27007 +                       if (!err)
27008 +                               err = 1;
27009 +               }
27010 +               if (err == 1)
27011 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27012 +               break;
27013 +       case Opt_nodirren:
27014 +               err = 1;
27015 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27016 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27017 +                                                             DR_FLUSHED));
27018 +                       if (!err)
27019 +                               err = 1;
27020 +               }
27021 +               if (err == 1)
27022 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27023 +               break;
27024 +
27025 +       case Opt_acl:
27026 +               sb->s_flags |= SB_POSIXACL;
27027 +               break;
27028 +       case Opt_noacl:
27029 +               sb->s_flags &= ~SB_POSIXACL;
27030 +               break;
27031 +
27032 +       default:
27033 +               err = 0;
27034 +               break;
27035 +       }
27036 +
27037 +       return err;
27038 +}
27039 +
27040 +/*
27041 + * returns tri-state.
27042 + * plus: processed without an error
27043 + * zero: unprocessed
27044 + * minus: error
27045 + */
27046 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27047 +                    struct au_opts *opts)
27048 +{
27049 +       int err, do_refresh;
27050 +
27051 +       err = 0;
27052 +       switch (opt->type) {
27053 +       case Opt_append:
27054 +               opt->add.bindex = au_sbbot(sb) + 1;
27055 +               if (opt->add.bindex < 0)
27056 +                       opt->add.bindex = 0;
27057 +               goto add;
27058 +               /* Always goto add, not fallthrough */
27059 +       case Opt_prepend:
27060 +               opt->add.bindex = 0;
27061 +               fallthrough;
27062 +       add: /* indented label */
27063 +       case Opt_add:
27064 +               err = au_br_add(sb, &opt->add,
27065 +                               au_ftest_opts(opts->flags, REMOUNT));
27066 +               if (!err) {
27067 +                       err = 1;
27068 +                       au_fset_opts(opts->flags, REFRESH);
27069 +               }
27070 +               break;
27071 +
27072 +       case Opt_del:
27073 +       case Opt_idel:
27074 +               err = au_br_del(sb, &opt->del,
27075 +                               au_ftest_opts(opts->flags, REMOUNT));
27076 +               if (!err) {
27077 +                       err = 1;
27078 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27079 +                       au_fset_opts(opts->flags, REFRESH);
27080 +               }
27081 +               break;
27082 +
27083 +       case Opt_mod:
27084 +       case Opt_imod:
27085 +               err = au_br_mod(sb, &opt->mod,
27086 +                               au_ftest_opts(opts->flags, REMOUNT),
27087 +                               &do_refresh);
27088 +               if (!err) {
27089 +                       err = 1;
27090 +                       if (do_refresh)
27091 +                               au_fset_opts(opts->flags, REFRESH);
27092 +               }
27093 +               break;
27094 +       }
27095 +       return err;
27096 +}
27097 +
27098 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27099 +                      struct au_opt_xino **opt_xino,
27100 +                      struct au_opts *opts)
27101 +{
27102 +       int err;
27103 +
27104 +       err = 0;
27105 +       switch (opt->type) {
27106 +       case Opt_xino:
27107 +               err = au_xino_set(sb, &opt->xino,
27108 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27109 +               if (unlikely(err))
27110 +                       break;
27111 +
27112 +               *opt_xino = &opt->xino;
27113 +               break;
27114 +
27115 +       case Opt_noxino:
27116 +               au_xino_clr(sb);
27117 +               *opt_xino = (void *)-1;
27118 +               break;
27119 +       }
27120 +
27121 +       return err;
27122 +}
27123 +
27124 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27125 +                  unsigned int pending)
27126 +{
27127 +       int err, fhsm;
27128 +       aufs_bindex_t bindex, bbot;
27129 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27130 +       struct au_branch *br;
27131 +       struct au_wbr *wbr;
27132 +       struct dentry *root, *dentry;
27133 +       struct inode *dir, *h_dir;
27134 +       struct au_sbinfo *sbinfo;
27135 +       struct au_hinode *hdir;
27136 +
27137 +       SiMustAnyLock(sb);
27138 +
27139 +       sbinfo = au_sbi(sb);
27140 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27141 +
27142 +       if (!(sb_flags & SB_RDONLY)) {
27143 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27144 +                       pr_warn("first branch should be rw\n");
27145 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27146 +                       pr_warn_once("shwh should be used with ro\n");
27147 +       }
27148 +
27149 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27150 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27151 +               pr_warn_once("udba=*notify requires xino\n");
27152 +
27153 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27154 +               pr_warn_once("dirperm1 breaks the protection"
27155 +                            " by the permission bits on the lower branch\n");
27156 +
27157 +       err = 0;
27158 +       fhsm = 0;
27159 +       root = sb->s_root;
27160 +       dir = d_inode(root);
27161 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27162 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27163 +                                     UDBA_NONE);
27164 +       bbot = au_sbbot(sb);
27165 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27166 +               skip = 0;
27167 +               h_dir = au_h_iptr(dir, bindex);
27168 +               br = au_sbr(sb, bindex);
27169 +
27170 +               if ((br->br_perm & AuBrAttr_ICEX)
27171 +                   && !h_dir->i_op->listxattr)
27172 +                       br->br_perm &= ~AuBrAttr_ICEX;
27173 +#if 0 /* untested */
27174 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27175 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27176 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27177 +#endif
27178 +
27179 +               do_free = 0;
27180 +               wbr = br->br_wbr;
27181 +               if (wbr)
27182 +                       wbr_wh_read_lock(wbr);
27183 +
27184 +               if (!au_br_writable(br->br_perm)) {
27185 +                       do_free = !!wbr;
27186 +                       skip = (!wbr
27187 +                               || (!wbr->wbr_whbase
27188 +                                   && !wbr->wbr_plink
27189 +                                   && !wbr->wbr_orph));
27190 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27191 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27192 +                       skip = (!wbr || !wbr->wbr_whbase);
27193 +                       if (skip && wbr) {
27194 +                               if (do_plink)
27195 +                                       skip = !!wbr->wbr_plink;
27196 +                               else
27197 +                                       skip = !wbr->wbr_plink;
27198 +                       }
27199 +               } else {
27200 +                       /* skip = (br->br_whbase && br->br_ohph); */
27201 +                       skip = (wbr && wbr->wbr_whbase);
27202 +                       if (skip) {
27203 +                               if (do_plink)
27204 +                                       skip = !!wbr->wbr_plink;
27205 +                               else
27206 +                                       skip = !wbr->wbr_plink;
27207 +                       }
27208 +               }
27209 +               if (wbr)
27210 +                       wbr_wh_read_unlock(wbr);
27211 +
27212 +               if (can_no_dreval) {
27213 +                       dentry = br->br_path.dentry;
27214 +                       spin_lock(&dentry->d_lock);
27215 +                       if (dentry->d_flags &
27216 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27217 +                               can_no_dreval = 0;
27218 +                       spin_unlock(&dentry->d_lock);
27219 +               }
27220 +
27221 +               if (au_br_fhsm(br->br_perm)) {
27222 +                       fhsm++;
27223 +                       AuDebugOn(!br->br_fhsm);
27224 +               }
27225 +
27226 +               if (skip)
27227 +                       continue;
27228 +
27229 +               hdir = au_hi(dir, bindex);
27230 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27231 +               if (wbr)
27232 +                       wbr_wh_write_lock(wbr);
27233 +               err = au_wh_init(br, sb);
27234 +               if (wbr)
27235 +                       wbr_wh_write_unlock(wbr);
27236 +               au_hn_inode_unlock(hdir);
27237 +
27238 +               if (!err && do_free) {
27239 +                       au_kfree_rcu(wbr);
27240 +                       br->br_wbr = NULL;
27241 +               }
27242 +       }
27243 +
27244 +       if (can_no_dreval)
27245 +               au_fset_si(sbinfo, NO_DREVAL);
27246 +       else
27247 +               au_fclr_si(sbinfo, NO_DREVAL);
27248 +
27249 +       if (fhsm >= 2) {
27250 +               au_fset_si(sbinfo, FHSM);
27251 +               for (bindex = bbot; bindex >= 0; bindex--) {
27252 +                       br = au_sbr(sb, bindex);
27253 +                       if (au_br_fhsm(br->br_perm)) {
27254 +                               au_fhsm_set_bottom(sb, bindex);
27255 +                               break;
27256 +                       }
27257 +               }
27258 +       } else {
27259 +               au_fclr_si(sbinfo, FHSM);
27260 +               au_fhsm_set_bottom(sb, -1);
27261 +       }
27262 +
27263 +       return err;
27264 +}
27265 +
27266 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27267 +{
27268 +       int err;
27269 +       unsigned int tmp;
27270 +       aufs_bindex_t bindex, bbot;
27271 +       struct au_opt *opt;
27272 +       struct au_opt_xino *opt_xino, xino;
27273 +       struct au_sbinfo *sbinfo;
27274 +       struct au_branch *br;
27275 +       struct inode *dir;
27276 +
27277 +       SiMustWriteLock(sb);
27278 +
27279 +       err = 0;
27280 +       opt_xino = NULL;
27281 +       opt = opts->opt;
27282 +       while (err >= 0 && opt->type != Opt_tail)
27283 +               err = au_opt_simple(sb, opt++, opts);
27284 +       if (err > 0)
27285 +               err = 0;
27286 +       else if (unlikely(err < 0))
27287 +               goto out;
27288 +
27289 +       /* disable xino and udba temporary */
27290 +       sbinfo = au_sbi(sb);
27291 +       tmp = sbinfo->si_mntflags;
27292 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27293 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27294 +
27295 +       opt = opts->opt;
27296 +       while (err >= 0 && opt->type != Opt_tail)
27297 +               err = au_opt_br(sb, opt++, opts);
27298 +       if (err > 0)
27299 +               err = 0;
27300 +       else if (unlikely(err < 0))
27301 +               goto out;
27302 +
27303 +       bbot = au_sbbot(sb);
27304 +       if (unlikely(bbot < 0)) {
27305 +               err = -EINVAL;
27306 +               pr_err("no branches\n");
27307 +               goto out;
27308 +       }
27309 +
27310 +       if (au_opt_test(tmp, XINO))
27311 +               au_opt_set(sbinfo->si_mntflags, XINO);
27312 +       opt = opts->opt;
27313 +       while (!err && opt->type != Opt_tail)
27314 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27315 +       if (unlikely(err))
27316 +               goto out;
27317 +
27318 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27319 +       if (unlikely(err))
27320 +               goto out;
27321 +
27322 +       /* restore xino */
27323 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27324 +               xino.file = au_xino_def(sb);
27325 +               err = PTR_ERR(xino.file);
27326 +               if (IS_ERR(xino.file))
27327 +                       goto out;
27328 +
27329 +               err = au_xino_set(sb, &xino, /*remount*/0);
27330 +               fput(xino.file);
27331 +               if (unlikely(err))
27332 +                       goto out;
27333 +       }
27334 +
27335 +       /* restore udba */
27336 +       tmp &= AuOptMask_UDBA;
27337 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27338 +       sbinfo->si_mntflags |= tmp;
27339 +       bbot = au_sbbot(sb);
27340 +       for (bindex = 0; bindex <= bbot; bindex++) {
27341 +               br = au_sbr(sb, bindex);
27342 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27343 +               if (unlikely(err))
27344 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27345 +                               bindex, err);
27346 +               /* go on even if err */
27347 +       }
27348 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27349 +               dir = d_inode(sb->s_root);
27350 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27351 +       }
27352 +
27353 +out:
27354 +       return err;
27355 +}
27356 +
27357 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27358 +{
27359 +       int err, rerr;
27360 +       unsigned char no_dreval;
27361 +       struct inode *dir;
27362 +       struct au_opt_xino *opt_xino;
27363 +       struct au_opt *opt;
27364 +       struct au_sbinfo *sbinfo;
27365 +
27366 +       SiMustWriteLock(sb);
27367 +
27368 +       err = au_dr_opt_flush(sb);
27369 +       if (unlikely(err))
27370 +               goto out;
27371 +       au_fset_opts(opts->flags, DR_FLUSHED);
27372 +
27373 +       dir = d_inode(sb->s_root);
27374 +       sbinfo = au_sbi(sb);
27375 +       opt_xino = NULL;
27376 +       opt = opts->opt;
27377 +       while (err >= 0 && opt->type != Opt_tail) {
27378 +               err = au_opt_simple(sb, opt, opts);
27379 +               if (!err)
27380 +                       err = au_opt_br(sb, opt, opts);
27381 +               if (!err)
27382 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27383 +               opt++;
27384 +       }
27385 +       if (err > 0)
27386 +               err = 0;
27387 +       AuTraceErr(err);
27388 +       /* go on even err */
27389 +
27390 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27391 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27392 +       if (unlikely(rerr && !err))
27393 +               err = rerr;
27394 +
27395 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27396 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27397 +
27398 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27399 +               rerr = au_xib_trunc(sb);
27400 +               if (unlikely(rerr && !err))
27401 +                       err = rerr;
27402 +       }
27403 +
27404 +       /* will be handled by the caller */
27405 +       if (!au_ftest_opts(opts->flags, REFRESH)
27406 +           && (opts->given_udba
27407 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27408 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27409 +                   ))
27410 +               au_fset_opts(opts->flags, REFRESH);
27411 +
27412 +       AuDbg("status 0x%x\n", opts->flags);
27413 +
27414 +out:
27415 +       return err;
27416 +}
27417 +
27418 +/* ---------------------------------------------------------------------- */
27419 +
27420 +unsigned int au_opt_udba(struct super_block *sb)
27421 +{
27422 +       return au_mntflags(sb) & AuOptMask_UDBA;
27423 +}
27424 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27425 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27426 +++ linux/fs/aufs/opts.h        2021-02-24 13:33:42.747680497 +0100
27427 @@ -0,0 +1,225 @@
27428 +/* SPDX-License-Identifier: GPL-2.0 */
27429 +/*
27430 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27431 + *
27432 + * This program, aufs is free software; you can redistribute it and/or modify
27433 + * it under the terms of the GNU General Public License as published by
27434 + * the Free Software Foundation; either version 2 of the License, or
27435 + * (at your option) any later version.
27436 + *
27437 + * This program is distributed in the hope that it will be useful,
27438 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27439 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27440 + * GNU General Public License for more details.
27441 + *
27442 + * You should have received a copy of the GNU General Public License
27443 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27444 + */
27445 +
27446 +/*
27447 + * mount options/flags
27448 + */
27449 +
27450 +#ifndef __AUFS_OPTS_H__
27451 +#define __AUFS_OPTS_H__
27452 +
27453 +#ifdef __KERNEL__
27454 +
27455 +#include <linux/path.h>
27456 +
27457 +struct file;
27458 +
27459 +/* ---------------------------------------------------------------------- */
27460 +
27461 +/* mount flags */
27462 +#define AuOpt_XINO             1               /* external inode number bitmap
27463 +                                                  and translation table */
27464 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27465 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27466 +#define AuOpt_UDBA_REVAL       (1 << 3)
27467 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27468 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27469 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27470 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27471 +                                                  bits */
27472 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27473 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27474 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27475 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27476 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27477 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27478 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27479 +
27480 +#ifndef CONFIG_AUFS_HNOTIFY
27481 +#undef AuOpt_UDBA_HNOTIFY
27482 +#define AuOpt_UDBA_HNOTIFY     0
27483 +#endif
27484 +#ifndef CONFIG_AUFS_DIRREN
27485 +#undef AuOpt_DIRREN
27486 +#define AuOpt_DIRREN           0
27487 +#endif
27488 +#ifndef CONFIG_AUFS_SHWH
27489 +#undef AuOpt_SHWH
27490 +#define AuOpt_SHWH             0
27491 +#endif
27492 +
27493 +#define AuOpt_Def      (AuOpt_XINO \
27494 +                        | AuOpt_UDBA_REVAL \
27495 +                        | AuOpt_PLINK \
27496 +                        /* | AuOpt_DIRPERM1 */ \
27497 +                        | AuOpt_WARN_PERM)
27498 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27499 +                        | AuOpt_UDBA_REVAL \
27500 +                        | AuOpt_UDBA_HNOTIFY)
27501 +
27502 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27503 +#define au_opt_set(flags, name) do { \
27504 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27505 +       ((flags) |= AuOpt_##name); \
27506 +} while (0)
27507 +#define au_opt_set_udba(flags, name) do { \
27508 +       (flags) &= ~AuOptMask_UDBA; \
27509 +       ((flags) |= AuOpt_##name); \
27510 +} while (0)
27511 +#define au_opt_clr(flags, name) do { \
27512 +       ((flags) &= ~AuOpt_##name); \
27513 +} while (0)
27514 +
27515 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27516 +{
27517 +#ifdef CONFIG_PROC_FS
27518 +       return mntflags;
27519 +#else
27520 +       return mntflags & ~AuOpt_PLINK;
27521 +#endif
27522 +}
27523 +
27524 +/* ---------------------------------------------------------------------- */
27525 +
27526 +/* policies to select one among multiple writable branches */
27527 +enum {
27528 +       AuWbrCreate_TDP,        /* top down parent */
27529 +       AuWbrCreate_RR,         /* round robin */
27530 +       AuWbrCreate_MFS,        /* most free space */
27531 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27532 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27533 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27534 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27535 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27536 +       AuWbrCreate_PMFS,       /* parent and mfs */
27537 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27538 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27539 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27540 +
27541 +       AuWbrCreate_Def = AuWbrCreate_TDP
27542 +};
27543 +
27544 +enum {
27545 +       AuWbrCopyup_TDP,        /* top down parent */
27546 +       AuWbrCopyup_BUP,        /* bottom up parent */
27547 +       AuWbrCopyup_BU,         /* bottom up */
27548 +
27549 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27550 +};
27551 +
27552 +/* ---------------------------------------------------------------------- */
27553 +
27554 +struct au_opt_add {
27555 +       aufs_bindex_t   bindex;
27556 +       char            *pathname;
27557 +       int             perm;
27558 +       struct path     path;
27559 +};
27560 +
27561 +struct au_opt_del {
27562 +       char            *pathname;
27563 +       struct path     h_path;
27564 +};
27565 +
27566 +struct au_opt_mod {
27567 +       char            *path;
27568 +       int             perm;
27569 +       struct dentry   *h_root;
27570 +};
27571 +
27572 +struct au_opt_xino {
27573 +       char            *path;
27574 +       struct file     *file;
27575 +};
27576 +
27577 +struct au_opt_xino_itrunc {
27578 +       aufs_bindex_t   bindex;
27579 +};
27580 +
27581 +struct au_opt_wbr_create {
27582 +       int                     wbr_create;
27583 +       int                     mfs_second;
27584 +       unsigned long long      mfsrr_watermark;
27585 +};
27586 +
27587 +struct au_opt {
27588 +       int type;
27589 +       union {
27590 +               struct au_opt_xino      xino;
27591 +               struct au_opt_xino_itrunc xino_itrunc;
27592 +               struct au_opt_add       add;
27593 +               struct au_opt_del       del;
27594 +               struct au_opt_mod       mod;
27595 +               int                     dirwh;
27596 +               int                     rdcache;
27597 +               unsigned int            rdblk;
27598 +               unsigned int            rdhash;
27599 +               int                     udba;
27600 +               struct au_opt_wbr_create wbr_create;
27601 +               int                     wbr_copyup;
27602 +               unsigned int            fhsm_second;
27603 +       };
27604 +};
27605 +
27606 +/* opts flags */
27607 +#define AuOpts_REMOUNT         1
27608 +#define AuOpts_REFRESH         (1 << 1)
27609 +#define AuOpts_TRUNC_XIB       (1 << 2)
27610 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27611 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27612 +#define AuOpts_DR_FLUSHED      (1 << 5)
27613 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27614 +#define au_fset_opts(flags, name) \
27615 +       do { (flags) |= AuOpts_##name; } while (0)
27616 +#define au_fclr_opts(flags, name) \
27617 +       do { (flags) &= ~AuOpts_##name; } while (0)
27618 +
27619 +#ifndef CONFIG_AUFS_DIRREN
27620 +#undef AuOpts_DR_FLUSHED
27621 +#define AuOpts_DR_FLUSHED      0
27622 +#endif
27623 +
27624 +struct au_opts {
27625 +       struct au_opt   *opt;
27626 +       int             max_opt;
27627 +
27628 +       unsigned int    given_udba;
27629 +       unsigned int    flags;
27630 +       unsigned long   sb_flags;
27631 +};
27632 +
27633 +/* ---------------------------------------------------------------------- */
27634 +
27635 +/* opts.c */
27636 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27637 +const char *au_optstr_udba(int udba);
27638 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27639 +const char *au_optstr_wbr_create(int wbr_create);
27640 +
27641 +void au_opts_free(struct au_opts *opts);
27642 +struct super_block;
27643 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27644 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27645 +                  unsigned int pending);
27646 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27647 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27648 +
27649 +unsigned int au_opt_udba(struct super_block *sb);
27650 +
27651 +#endif /* __KERNEL__ */
27652 +#endif /* __AUFS_OPTS_H__ */
27653 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27654 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27655 +++ linux/fs/aufs/plink.c       2021-02-24 13:33:42.747680497 +0100
27656 @@ -0,0 +1,516 @@
27657 +// SPDX-License-Identifier: GPL-2.0
27658 +/*
27659 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27660 + *
27661 + * This program, aufs is free software; you can redistribute it and/or modify
27662 + * it under the terms of the GNU General Public License as published by
27663 + * the Free Software Foundation; either version 2 of the License, or
27664 + * (at your option) any later version.
27665 + *
27666 + * This program is distributed in the hope that it will be useful,
27667 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27668 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27669 + * GNU General Public License for more details.
27670 + *
27671 + * You should have received a copy of the GNU General Public License
27672 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27673 + */
27674 +
27675 +/*
27676 + * pseudo-link
27677 + */
27678 +
27679 +#include "aufs.h"
27680 +
27681 +/*
27682 + * the pseudo-link maintenance mode.
27683 + * during a user process maintains the pseudo-links,
27684 + * prohibit adding a new plink and branch manipulation.
27685 + *
27686 + * Flags
27687 + * NOPLM:
27688 + *     For entry functions which will handle plink, and i_mutex is already held
27689 + *     in VFS.
27690 + *     They cannot wait and should return an error at once.
27691 + *     Callers has to check the error.
27692 + * NOPLMW:
27693 + *     For entry functions which will handle plink, but i_mutex is not held
27694 + *     in VFS.
27695 + *     They can wait the plink maintenance mode to finish.
27696 + *
27697 + * They behave like F_SETLK and F_SETLKW.
27698 + * If the caller never handle plink, then both flags are unnecessary.
27699 + */
27700 +
27701 +int au_plink_maint(struct super_block *sb, int flags)
27702 +{
27703 +       int err;
27704 +       pid_t pid, ppid;
27705 +       struct task_struct *parent, *prev;
27706 +       struct au_sbinfo *sbi;
27707 +
27708 +       SiMustAnyLock(sb);
27709 +
27710 +       err = 0;
27711 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27712 +               goto out;
27713 +
27714 +       sbi = au_sbi(sb);
27715 +       pid = sbi->si_plink_maint_pid;
27716 +       if (!pid || pid == current->pid)
27717 +               goto out;
27718 +
27719 +       /* todo: it highly depends upon /sbin/mount.aufs */
27720 +       prev = NULL;
27721 +       parent = current;
27722 +       ppid = 0;
27723 +       rcu_read_lock();
27724 +       while (1) {
27725 +               parent = rcu_dereference(parent->real_parent);
27726 +               if (parent == prev)
27727 +                       break;
27728 +               ppid = task_pid_vnr(parent);
27729 +               if (pid == ppid) {
27730 +                       rcu_read_unlock();
27731 +                       goto out;
27732 +               }
27733 +               prev = parent;
27734 +       }
27735 +       rcu_read_unlock();
27736 +
27737 +       if (au_ftest_lock(flags, NOPLMW)) {
27738 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27739 +               /* AuDebugOn(!lockdep_depth(current)); */
27740 +               while (sbi->si_plink_maint_pid) {
27741 +                       si_read_unlock(sb);
27742 +                       /* gave up wake_up_bit() */
27743 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27744 +
27745 +                       if (au_ftest_lock(flags, FLUSH))
27746 +                               au_nwt_flush(&sbi->si_nowait);
27747 +                       si_noflush_read_lock(sb);
27748 +               }
27749 +       } else if (au_ftest_lock(flags, NOPLM)) {
27750 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27751 +               err = -EAGAIN;
27752 +       }
27753 +
27754 +out:
27755 +       return err;
27756 +}
27757 +
27758 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27759 +{
27760 +       spin_lock(&sbinfo->si_plink_maint_lock);
27761 +       sbinfo->si_plink_maint_pid = 0;
27762 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27763 +       wake_up_all(&sbinfo->si_plink_wq);
27764 +}
27765 +
27766 +int au_plink_maint_enter(struct super_block *sb)
27767 +{
27768 +       int err;
27769 +       struct au_sbinfo *sbinfo;
27770 +
27771 +       err = 0;
27772 +       sbinfo = au_sbi(sb);
27773 +       /* make sure i am the only one in this fs */
27774 +       si_write_lock(sb, AuLock_FLUSH);
27775 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27776 +               spin_lock(&sbinfo->si_plink_maint_lock);
27777 +               if (!sbinfo->si_plink_maint_pid)
27778 +                       sbinfo->si_plink_maint_pid = current->pid;
27779 +               else
27780 +                       err = -EBUSY;
27781 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27782 +       }
27783 +       si_write_unlock(sb);
27784 +
27785 +       return err;
27786 +}
27787 +
27788 +/* ---------------------------------------------------------------------- */
27789 +
27790 +#ifdef CONFIG_AUFS_DEBUG
27791 +void au_plink_list(struct super_block *sb)
27792 +{
27793 +       int i;
27794 +       struct au_sbinfo *sbinfo;
27795 +       struct hlist_bl_head *hbl;
27796 +       struct hlist_bl_node *pos;
27797 +       struct au_icntnr *icntnr;
27798 +
27799 +       SiMustAnyLock(sb);
27800 +
27801 +       sbinfo = au_sbi(sb);
27802 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27803 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27804 +
27805 +       for (i = 0; i < AuPlink_NHASH; i++) {
27806 +               hbl = sbinfo->si_plink + i;
27807 +               hlist_bl_lock(hbl);
27808 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27809 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27810 +               hlist_bl_unlock(hbl);
27811 +       }
27812 +}
27813 +#endif
27814 +
27815 +/* is the inode pseudo-linked? */
27816 +int au_plink_test(struct inode *inode)
27817 +{
27818 +       int found, i;
27819 +       struct au_sbinfo *sbinfo;
27820 +       struct hlist_bl_head *hbl;
27821 +       struct hlist_bl_node *pos;
27822 +       struct au_icntnr *icntnr;
27823 +
27824 +       sbinfo = au_sbi(inode->i_sb);
27825 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27826 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27827 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27828 +
27829 +       found = 0;
27830 +       i = au_plink_hash(inode->i_ino);
27831 +       hbl =  sbinfo->si_plink + i;
27832 +       hlist_bl_lock(hbl);
27833 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27834 +               if (&icntnr->vfs_inode == inode) {
27835 +                       found = 1;
27836 +                       break;
27837 +               }
27838 +       hlist_bl_unlock(hbl);
27839 +       return found;
27840 +}
27841 +
27842 +/* ---------------------------------------------------------------------- */
27843 +
27844 +/*
27845 + * generate a name for plink.
27846 + * the file will be stored under AUFS_WH_PLINKDIR.
27847 + */
27848 +/* 20 is max digits length of ulong 64 */
27849 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27850 +
27851 +static int plink_name(char *name, int len, struct inode *inode,
27852 +                     aufs_bindex_t bindex)
27853 +{
27854 +       int rlen;
27855 +       struct inode *h_inode;
27856 +
27857 +       h_inode = au_h_iptr(inode, bindex);
27858 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27859 +       return rlen;
27860 +}
27861 +
27862 +struct au_do_plink_lkup_args {
27863 +       struct dentry **errp;
27864 +       struct qstr *tgtname;
27865 +       struct dentry *h_parent;
27866 +       struct au_branch *br;
27867 +};
27868 +
27869 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27870 +                                      struct dentry *h_parent,
27871 +                                      struct au_branch *br)
27872 +{
27873 +       struct dentry *h_dentry;
27874 +       struct inode *h_inode;
27875 +
27876 +       h_inode = d_inode(h_parent);
27877 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27878 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
27879 +       inode_unlock_shared(h_inode);
27880 +       return h_dentry;
27881 +}
27882 +
27883 +static void au_call_do_plink_lkup(void *args)
27884 +{
27885 +       struct au_do_plink_lkup_args *a = args;
27886 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27887 +}
27888 +
27889 +/* lookup the plink-ed @inode under the branch at @bindex */
27890 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27891 +{
27892 +       struct dentry *h_dentry, *h_parent;
27893 +       struct au_branch *br;
27894 +       int wkq_err;
27895 +       char a[PLINK_NAME_LEN];
27896 +       struct qstr tgtname = QSTR_INIT(a, 0);
27897 +
27898 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27899 +
27900 +       br = au_sbr(inode->i_sb, bindex);
27901 +       h_parent = br->br_wbr->wbr_plink;
27902 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27903 +
27904 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27905 +               struct au_do_plink_lkup_args args = {
27906 +                       .errp           = &h_dentry,
27907 +                       .tgtname        = &tgtname,
27908 +                       .h_parent       = h_parent,
27909 +                       .br             = br
27910 +               };
27911 +
27912 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27913 +               if (unlikely(wkq_err))
27914 +                       h_dentry = ERR_PTR(wkq_err);
27915 +       } else
27916 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27917 +
27918 +       return h_dentry;
27919 +}
27920 +
27921 +/* create a pseudo-link */
27922 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27923 +                     struct dentry *h_dentry, struct au_branch *br)
27924 +{
27925 +       int err;
27926 +       struct path h_path = {
27927 +               .mnt = au_br_mnt(br)
27928 +       };
27929 +       struct inode *h_dir, *delegated;
27930 +
27931 +       h_dir = d_inode(h_parent);
27932 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27933 +again:
27934 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
27935 +       err = PTR_ERR(h_path.dentry);
27936 +       if (IS_ERR(h_path.dentry))
27937 +               goto out;
27938 +
27939 +       err = 0;
27940 +       /* wh.plink dir is not monitored */
27941 +       /* todo: is it really safe? */
27942 +       if (d_is_positive(h_path.dentry)
27943 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27944 +               delegated = NULL;
27945 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27946 +               if (unlikely(err == -EWOULDBLOCK)) {
27947 +                       pr_warn("cannot retry for NFSv4 delegation"
27948 +                               " for an internal unlink\n");
27949 +                       iput(delegated);
27950 +               }
27951 +               dput(h_path.dentry);
27952 +               h_path.dentry = NULL;
27953 +               if (!err)
27954 +                       goto again;
27955 +       }
27956 +       if (!err && d_is_negative(h_path.dentry)) {
27957 +               delegated = NULL;
27958 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27959 +               if (unlikely(err == -EWOULDBLOCK)) {
27960 +                       pr_warn("cannot retry for NFSv4 delegation"
27961 +                               " for an internal link\n");
27962 +                       iput(delegated);
27963 +               }
27964 +       }
27965 +       dput(h_path.dentry);
27966 +
27967 +out:
27968 +       inode_unlock(h_dir);
27969 +       return err;
27970 +}
27971 +
27972 +struct do_whplink_args {
27973 +       int *errp;
27974 +       struct qstr *tgt;
27975 +       struct dentry *h_parent;
27976 +       struct dentry *h_dentry;
27977 +       struct au_branch *br;
27978 +};
27979 +
27980 +static void call_do_whplink(void *args)
27981 +{
27982 +       struct do_whplink_args *a = args;
27983 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27984 +}
27985 +
27986 +static int whplink(struct dentry *h_dentry, struct inode *inode,
27987 +                  aufs_bindex_t bindex, struct au_branch *br)
27988 +{
27989 +       int err, wkq_err;
27990 +       struct au_wbr *wbr;
27991 +       struct dentry *h_parent;
27992 +       char a[PLINK_NAME_LEN];
27993 +       struct qstr tgtname = QSTR_INIT(a, 0);
27994 +
27995 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
27996 +       h_parent = wbr->wbr_plink;
27997 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27998 +
27999 +       /* always superio. */
28000 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28001 +               struct do_whplink_args args = {
28002 +                       .errp           = &err,
28003 +                       .tgt            = &tgtname,
28004 +                       .h_parent       = h_parent,
28005 +                       .h_dentry       = h_dentry,
28006 +                       .br             = br
28007 +               };
28008 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28009 +               if (unlikely(wkq_err))
28010 +                       err = wkq_err;
28011 +       } else
28012 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28013 +
28014 +       return err;
28015 +}
28016 +
28017 +/*
28018 + * create a new pseudo-link for @h_dentry on @bindex.
28019 + * the linked inode is held in aufs @inode.
28020 + */
28021 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28022 +                    struct dentry *h_dentry)
28023 +{
28024 +       struct super_block *sb;
28025 +       struct au_sbinfo *sbinfo;
28026 +       struct hlist_bl_head *hbl;
28027 +       struct hlist_bl_node *pos;
28028 +       struct au_icntnr *icntnr;
28029 +       int found, err, cnt, i;
28030 +
28031 +       sb = inode->i_sb;
28032 +       sbinfo = au_sbi(sb);
28033 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28034 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28035 +
28036 +       found = au_plink_test(inode);
28037 +       if (found)
28038 +               return;
28039 +
28040 +       i = au_plink_hash(inode->i_ino);
28041 +       hbl = sbinfo->si_plink + i;
28042 +       au_igrab(inode);
28043 +
28044 +       hlist_bl_lock(hbl);
28045 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28046 +               if (&icntnr->vfs_inode == inode) {
28047 +                       found = 1;
28048 +                       break;
28049 +               }
28050 +       }
28051 +       if (!found) {
28052 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28053 +               hlist_bl_add_head(&icntnr->plink, hbl);
28054 +       }
28055 +       hlist_bl_unlock(hbl);
28056 +       if (!found) {
28057 +               cnt = au_hbl_count(hbl);
28058 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28059 +               if (cnt > AUFS_PLINK_WARN)
28060 +                       AuWarn1(msg ", %d\n", cnt);
28061 +#undef msg
28062 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28063 +               if (unlikely(err)) {
28064 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28065 +                       au_hbl_del(&icntnr->plink, hbl);
28066 +                       iput(&icntnr->vfs_inode);
28067 +               }
28068 +       } else
28069 +               iput(&icntnr->vfs_inode);
28070 +}
28071 +
28072 +/* free all plinks */
28073 +void au_plink_put(struct super_block *sb, int verbose)
28074 +{
28075 +       int i, warned;
28076 +       struct au_sbinfo *sbinfo;
28077 +       struct hlist_bl_head *hbl;
28078 +       struct hlist_bl_node *pos, *tmp;
28079 +       struct au_icntnr *icntnr;
28080 +
28081 +       SiMustWriteLock(sb);
28082 +
28083 +       sbinfo = au_sbi(sb);
28084 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28085 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28086 +
28087 +       /* no spin_lock since sbinfo is write-locked */
28088 +       warned = 0;
28089 +       for (i = 0; i < AuPlink_NHASH; i++) {
28090 +               hbl = sbinfo->si_plink + i;
28091 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28092 +                       pr_warn("pseudo-link is not flushed");
28093 +                       warned = 1;
28094 +               }
28095 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28096 +                       iput(&icntnr->vfs_inode);
28097 +               INIT_HLIST_BL_HEAD(hbl);
28098 +       }
28099 +}
28100 +
28101 +void au_plink_clean(struct super_block *sb, int verbose)
28102 +{
28103 +       struct dentry *root;
28104 +
28105 +       root = sb->s_root;
28106 +       aufs_write_lock(root);
28107 +       if (au_opt_test(au_mntflags(sb), PLINK))
28108 +               au_plink_put(sb, verbose);
28109 +       aufs_write_unlock(root);
28110 +}
28111 +
28112 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28113 +{
28114 +       int do_put;
28115 +       aufs_bindex_t btop, bbot, bindex;
28116 +
28117 +       do_put = 0;
28118 +       btop = au_ibtop(inode);
28119 +       bbot = au_ibbot(inode);
28120 +       if (btop >= 0) {
28121 +               for (bindex = btop; bindex <= bbot; bindex++) {
28122 +                       if (!au_h_iptr(inode, bindex)
28123 +                           || au_ii_br_id(inode, bindex) != br_id)
28124 +                               continue;
28125 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28126 +                       do_put = 1;
28127 +                       break;
28128 +               }
28129 +               if (do_put)
28130 +                       for (bindex = btop; bindex <= bbot; bindex++)
28131 +                               if (au_h_iptr(inode, bindex)) {
28132 +                                       do_put = 0;
28133 +                                       break;
28134 +                               }
28135 +       } else
28136 +               do_put = 1;
28137 +
28138 +       return do_put;
28139 +}
28140 +
28141 +/* free the plinks on a branch specified by @br_id */
28142 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28143 +{
28144 +       struct au_sbinfo *sbinfo;
28145 +       struct hlist_bl_head *hbl;
28146 +       struct hlist_bl_node *pos, *tmp;
28147 +       struct au_icntnr *icntnr;
28148 +       struct inode *inode;
28149 +       int i, do_put;
28150 +
28151 +       SiMustWriteLock(sb);
28152 +
28153 +       sbinfo = au_sbi(sb);
28154 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28155 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28156 +
28157 +       /* no bit_lock since sbinfo is write-locked */
28158 +       for (i = 0; i < AuPlink_NHASH; i++) {
28159 +               hbl = sbinfo->si_plink + i;
28160 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28161 +                       inode = au_igrab(&icntnr->vfs_inode);
28162 +                       ii_write_lock_child(inode);
28163 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28164 +                       if (do_put) {
28165 +                               hlist_bl_del(&icntnr->plink);
28166 +                               iput(inode);
28167 +                       }
28168 +                       ii_write_unlock(inode);
28169 +                       iput(inode);
28170 +               }
28171 +       }
28172 +}
28173 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28174 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28175 +++ linux/fs/aufs/poll.c        2021-02-24 13:33:42.747680497 +0100
28176 @@ -0,0 +1,51 @@
28177 +// SPDX-License-Identifier: GPL-2.0
28178 +/*
28179 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28180 + *
28181 + * This program, aufs is free software; you can redistribute it and/or modify
28182 + * it under the terms of the GNU General Public License as published by
28183 + * the Free Software Foundation; either version 2 of the License, or
28184 + * (at your option) any later version.
28185 + *
28186 + * This program is distributed in the hope that it will be useful,
28187 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28188 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28189 + * GNU General Public License for more details.
28190 + *
28191 + * You should have received a copy of the GNU General Public License
28192 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28193 + */
28194 +
28195 +/*
28196 + * poll operation
28197 + * There is only one filesystem which implements ->poll operation, currently.
28198 + */
28199 +
28200 +#include "aufs.h"
28201 +
28202 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28203 +{
28204 +       __poll_t mask;
28205 +       struct file *h_file;
28206 +       struct super_block *sb;
28207 +
28208 +       /* We should pretend an error happened. */
28209 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28210 +       sb = file->f_path.dentry->d_sb;
28211 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28212 +
28213 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28214 +       if (IS_ERR(h_file)) {
28215 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28216 +               goto out;
28217 +       }
28218 +
28219 +       mask = vfs_poll(h_file, pt);
28220 +       fput(h_file); /* instead of au_read_post() */
28221 +
28222 +out:
28223 +       si_read_unlock(sb);
28224 +       if (mask & EPOLLERR)
28225 +               AuDbg("mask 0x%x\n", mask);
28226 +       return mask;
28227 +}
28228 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28229 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28230 +++ linux/fs/aufs/posix_acl.c   2021-02-24 13:33:42.747680497 +0100
28231 @@ -0,0 +1,105 @@
28232 +// SPDX-License-Identifier: GPL-2.0
28233 +/*
28234 + * Copyright (C) 2014-2020 Junjiro R. Okajima
28235 + *
28236 + * This program, aufs is free software; you can redistribute it and/or modify
28237 + * it under the terms of the GNU General Public License as published by
28238 + * the Free Software Foundation; either version 2 of the License, or
28239 + * (at your option) any later version.
28240 + *
28241 + * This program is distributed in the hope that it will be useful,
28242 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28243 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28244 + * GNU General Public License for more details.
28245 + *
28246 + * You should have received a copy of the GNU General Public License
28247 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28248 + */
28249 +
28250 +/*
28251 + * posix acl operations
28252 + */
28253 +
28254 +#include <linux/fs.h>
28255 +#include "aufs.h"
28256 +
28257 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28258 +{
28259 +       struct posix_acl *acl;
28260 +       int err;
28261 +       aufs_bindex_t bindex;
28262 +       struct inode *h_inode;
28263 +       struct super_block *sb;
28264 +
28265 +       acl = NULL;
28266 +       sb = inode->i_sb;
28267 +       si_read_lock(sb, AuLock_FLUSH);
28268 +       ii_read_lock_child(inode);
28269 +       if (!(sb->s_flags & SB_POSIXACL))
28270 +               goto out;
28271 +
28272 +       bindex = au_ibtop(inode);
28273 +       h_inode = au_h_iptr(inode, bindex);
28274 +       if (unlikely(!h_inode
28275 +                    || ((h_inode->i_mode & S_IFMT)
28276 +                        != (inode->i_mode & S_IFMT)))) {
28277 +               err = au_busy_or_stale();
28278 +               acl = ERR_PTR(err);
28279 +               goto out;
28280 +       }
28281 +
28282 +       /* always topmost only */
28283 +       acl = get_acl(h_inode, type);
28284 +       if (IS_ERR(acl))
28285 +               forget_cached_acl(inode, type);
28286 +       else
28287 +               set_cached_acl(inode, type, acl);
28288 +
28289 +out:
28290 +       ii_read_unlock(inode);
28291 +       si_read_unlock(sb);
28292 +
28293 +       AuTraceErrPtr(acl);
28294 +       return acl;
28295 +}
28296 +
28297 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28298 +{
28299 +       int err;
28300 +       ssize_t ssz;
28301 +       struct dentry *dentry;
28302 +       struct au_sxattr arg = {
28303 +               .type = AU_ACL_SET,
28304 +               .u.acl_set = {
28305 +                       .acl    = acl,
28306 +                       .type   = type
28307 +               },
28308 +       };
28309 +
28310 +       IMustLock(inode);
28311 +
28312 +       if (inode->i_ino == AUFS_ROOT_INO)
28313 +               dentry = dget(inode->i_sb->s_root);
28314 +       else {
28315 +               dentry = d_find_alias(inode);
28316 +               if (!dentry)
28317 +                       dentry = d_find_any_alias(inode);
28318 +               if (!dentry) {
28319 +                       pr_warn("cannot handle this inode, "
28320 +                               "please report to aufs-users ML\n");
28321 +                       err = -ENOENT;
28322 +                       goto out;
28323 +               }
28324 +       }
28325 +
28326 +       ssz = au_sxattr(dentry, inode, &arg);
28327 +       /* forget even it if succeeds since the branch might set differently */
28328 +       forget_cached_acl(inode, type);
28329 +       dput(dentry);
28330 +       err = ssz;
28331 +       if (ssz >= 0)
28332 +               err = 0;
28333 +
28334 +out:
28335 +       return err;
28336 +}
28337 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28338 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28339 +++ linux/fs/aufs/procfs.c      2021-02-24 13:33:42.747680497 +0100
28340 @@ -0,0 +1,170 @@
28341 +// SPDX-License-Identifier: GPL-2.0
28342 +/*
28343 + * Copyright (C) 2010-2020 Junjiro R. Okajima
28344 + *
28345 + * This program, aufs is free software; you can redistribute it and/or modify
28346 + * it under the terms of the GNU General Public License as published by
28347 + * the Free Software Foundation; either version 2 of the License, or
28348 + * (at your option) any later version.
28349 + *
28350 + * This program is distributed in the hope that it will be useful,
28351 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28352 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28353 + * GNU General Public License for more details.
28354 + *
28355 + * You should have received a copy of the GNU General Public License
28356 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28357 + */
28358 +
28359 +/*
28360 + * procfs interfaces
28361 + */
28362 +
28363 +#include <linux/proc_fs.h>
28364 +#include "aufs.h"
28365 +
28366 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28367 +{
28368 +       struct au_sbinfo *sbinfo;
28369 +
28370 +       sbinfo = file->private_data;
28371 +       if (sbinfo) {
28372 +               au_plink_maint_leave(sbinfo);
28373 +               kobject_put(&sbinfo->si_kobj);
28374 +       }
28375 +
28376 +       return 0;
28377 +}
28378 +
28379 +static void au_procfs_plm_write_clean(struct file *file)
28380 +{
28381 +       struct au_sbinfo *sbinfo;
28382 +
28383 +       sbinfo = file->private_data;
28384 +       if (sbinfo)
28385 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28386 +}
28387 +
28388 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28389 +{
28390 +       int err;
28391 +       struct super_block *sb;
28392 +       struct au_sbinfo *sbinfo;
28393 +       struct hlist_bl_node *pos;
28394 +
28395 +       err = -EBUSY;
28396 +       if (unlikely(file->private_data))
28397 +               goto out;
28398 +
28399 +       sb = NULL;
28400 +       /* don't use au_sbilist_lock() here */
28401 +       hlist_bl_lock(&au_sbilist);
28402 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28403 +               if (id == sysaufs_si_id(sbinfo)) {
28404 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28405 +                               sb = sbinfo->si_sb;
28406 +                       break;
28407 +               }
28408 +       hlist_bl_unlock(&au_sbilist);
28409 +
28410 +       err = -EINVAL;
28411 +       if (unlikely(!sb))
28412 +               goto out;
28413 +
28414 +       err = au_plink_maint_enter(sb);
28415 +       if (!err)
28416 +               /* keep kobject_get() */
28417 +               file->private_data = sbinfo;
28418 +       else
28419 +               kobject_put(&sbinfo->si_kobj);
28420 +out:
28421 +       return err;
28422 +}
28423 +
28424 +/*
28425 + * Accept a valid "si=xxxx" only.
28426 + * Once it is accepted successfully, accept "clean" too.
28427 + */
28428 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28429 +                                  size_t count, loff_t *ppos)
28430 +{
28431 +       ssize_t err;
28432 +       unsigned long id;
28433 +       /* last newline is allowed */
28434 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28435 +
28436 +       err = -EACCES;
28437 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28438 +               goto out;
28439 +
28440 +       err = -EINVAL;
28441 +       if (unlikely(count > sizeof(buf)))
28442 +               goto out;
28443 +
28444 +       err = copy_from_user(buf, ubuf, count);
28445 +       if (unlikely(err)) {
28446 +               err = -EFAULT;
28447 +               goto out;
28448 +       }
28449 +       buf[count] = 0;
28450 +
28451 +       err = -EINVAL;
28452 +       if (!strcmp("clean", buf)) {
28453 +               au_procfs_plm_write_clean(file);
28454 +               goto out_success;
28455 +       } else if (unlikely(strncmp("si=", buf, 3)))
28456 +               goto out;
28457 +
28458 +       err = kstrtoul(buf + 3, 16, &id);
28459 +       if (unlikely(err))
28460 +               goto out;
28461 +
28462 +       err = au_procfs_plm_write_si(file, id);
28463 +       if (unlikely(err))
28464 +               goto out;
28465 +
28466 +out_success:
28467 +       err = count; /* success */
28468 +out:
28469 +       return err;
28470 +}
28471 +
28472 +static const struct proc_ops au_procfs_plm_op = {
28473 +       .proc_write     = au_procfs_plm_write,
28474 +       .proc_release   = au_procfs_plm_release
28475 +};
28476 +
28477 +/* ---------------------------------------------------------------------- */
28478 +
28479 +static struct proc_dir_entry *au_procfs_dir;
28480 +
28481 +void au_procfs_fin(void)
28482 +{
28483 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28484 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28485 +}
28486 +
28487 +int __init au_procfs_init(void)
28488 +{
28489 +       int err;
28490 +       struct proc_dir_entry *entry;
28491 +
28492 +       err = -ENOMEM;
28493 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28494 +       if (unlikely(!au_procfs_dir))
28495 +               goto out;
28496 +
28497 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28498 +                           au_procfs_dir, &au_procfs_plm_op);
28499 +       if (unlikely(!entry))
28500 +               goto out_dir;
28501 +
28502 +       err = 0;
28503 +       goto out; /* success */
28504 +
28505 +
28506 +out_dir:
28507 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28508 +out:
28509 +       return err;
28510 +}
28511 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28512 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28513 +++ linux/fs/aufs/rdu.c 2021-02-24 13:33:42.747680497 +0100
28514 @@ -0,0 +1,384 @@
28515 +// SPDX-License-Identifier: GPL-2.0
28516 +/*
28517 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28518 + *
28519 + * This program, aufs is free software; you can redistribute it and/or modify
28520 + * it under the terms of the GNU General Public License as published by
28521 + * the Free Software Foundation; either version 2 of the License, or
28522 + * (at your option) any later version.
28523 + *
28524 + * This program is distributed in the hope that it will be useful,
28525 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28526 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28527 + * GNU General Public License for more details.
28528 + *
28529 + * You should have received a copy of the GNU General Public License
28530 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28531 + */
28532 +
28533 +/*
28534 + * readdir in userspace.
28535 + */
28536 +
28537 +#include <linux/compat.h>
28538 +#include <linux/fs_stack.h>
28539 +#include <linux/security.h>
28540 +#include "aufs.h"
28541 +
28542 +/* bits for struct aufs_rdu.flags */
28543 +#define        AuRdu_CALLED    1
28544 +#define        AuRdu_CONT      (1 << 1)
28545 +#define        AuRdu_FULL      (1 << 2)
28546 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28547 +#define au_fset_rdu(flags, name) \
28548 +       do { (flags) |= AuRdu_##name; } while (0)
28549 +#define au_fclr_rdu(flags, name) \
28550 +       do { (flags) &= ~AuRdu_##name; } while (0)
28551 +
28552 +struct au_rdu_arg {
28553 +       struct dir_context              ctx;
28554 +       struct aufs_rdu                 *rdu;
28555 +       union au_rdu_ent_ul             ent;
28556 +       unsigned long                   end;
28557 +
28558 +       struct super_block              *sb;
28559 +       int                             err;
28560 +};
28561 +
28562 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28563 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28564 +{
28565 +       int err, len;
28566 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28567 +       struct aufs_rdu *rdu = arg->rdu;
28568 +       struct au_rdu_ent ent;
28569 +
28570 +       err = 0;
28571 +       arg->err = 0;
28572 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28573 +       len = au_rdu_len(nlen);
28574 +       if (arg->ent.ul + len  < arg->end) {
28575 +               ent.ino = h_ino;
28576 +               ent.bindex = rdu->cookie.bindex;
28577 +               ent.type = d_type;
28578 +               ent.nlen = nlen;
28579 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28580 +                       ent.type = DT_UNKNOWN;
28581 +
28582 +               /* unnecessary to support mmap_sem since this is a dir */
28583 +               err = -EFAULT;
28584 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28585 +                       goto out;
28586 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28587 +                       goto out;
28588 +               /* the terminating NULL */
28589 +               if (__put_user(0, arg->ent.e->name + nlen))
28590 +                       goto out;
28591 +               err = 0;
28592 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28593 +               arg->ent.ul += len;
28594 +               rdu->rent++;
28595 +       } else {
28596 +               err = -EFAULT;
28597 +               au_fset_rdu(rdu->cookie.flags, FULL);
28598 +               rdu->full = 1;
28599 +               rdu->tail = arg->ent;
28600 +       }
28601 +
28602 +out:
28603 +       /* AuTraceErr(err); */
28604 +       return err;
28605 +}
28606 +
28607 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28608 +{
28609 +       int err;
28610 +       loff_t offset;
28611 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28612 +
28613 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28614 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28615 +       err = offset;
28616 +       if (unlikely(offset != cookie->h_pos))
28617 +               goto out;
28618 +
28619 +       err = 0;
28620 +       do {
28621 +               arg->err = 0;
28622 +               au_fclr_rdu(cookie->flags, CALLED);
28623 +               /* smp_mb(); */
28624 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28625 +               if (err >= 0)
28626 +                       err = arg->err;
28627 +       } while (!err
28628 +                && au_ftest_rdu(cookie->flags, CALLED)
28629 +                && !au_ftest_rdu(cookie->flags, FULL));
28630 +       cookie->h_pos = h_file->f_pos;
28631 +
28632 +out:
28633 +       AuTraceErr(err);
28634 +       return err;
28635 +}
28636 +
28637 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28638 +{
28639 +       int err;
28640 +       aufs_bindex_t bbot;
28641 +       struct au_rdu_arg arg = {
28642 +               .ctx = {
28643 +                       .actor = au_rdu_fill
28644 +               }
28645 +       };
28646 +       struct dentry *dentry;
28647 +       struct inode *inode;
28648 +       struct file *h_file;
28649 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28650 +
28651 +       /* VERIFY_WRITE */
28652 +       err = !access_ok(rdu->ent.e, rdu->sz);
28653 +       if (unlikely(err)) {
28654 +               err = -EFAULT;
28655 +               AuTraceErr(err);
28656 +               goto out;
28657 +       }
28658 +       rdu->rent = 0;
28659 +       rdu->tail = rdu->ent;
28660 +       rdu->full = 0;
28661 +       arg.rdu = rdu;
28662 +       arg.ent = rdu->ent;
28663 +       arg.end = arg.ent.ul;
28664 +       arg.end += rdu->sz;
28665 +
28666 +       err = -ENOTDIR;
28667 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28668 +               goto out;
28669 +
28670 +       err = security_file_permission(file, MAY_READ);
28671 +       AuTraceErr(err);
28672 +       if (unlikely(err))
28673 +               goto out;
28674 +
28675 +       dentry = file->f_path.dentry;
28676 +       inode = d_inode(dentry);
28677 +       inode_lock_shared(inode);
28678 +
28679 +       arg.sb = inode->i_sb;
28680 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28681 +       if (unlikely(err))
28682 +               goto out_mtx;
28683 +       err = au_alive_dir(dentry);
28684 +       if (unlikely(err))
28685 +               goto out_si;
28686 +       /* todo: reval? */
28687 +       fi_read_lock(file);
28688 +
28689 +       err = -EAGAIN;
28690 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28691 +                    && cookie->generation != au_figen(file)))
28692 +               goto out_unlock;
28693 +
28694 +       err = 0;
28695 +       if (!rdu->blk) {
28696 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28697 +               if (!rdu->blk)
28698 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28699 +       }
28700 +       bbot = au_fbtop(file);
28701 +       if (cookie->bindex < bbot)
28702 +               cookie->bindex = bbot;
28703 +       bbot = au_fbbot_dir(file);
28704 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28705 +       for (; !err && cookie->bindex <= bbot;
28706 +            cookie->bindex++, cookie->h_pos = 0) {
28707 +               h_file = au_hf_dir(file, cookie->bindex);
28708 +               if (!h_file)
28709 +                       continue;
28710 +
28711 +               au_fclr_rdu(cookie->flags, FULL);
28712 +               err = au_rdu_do(h_file, &arg);
28713 +               AuTraceErr(err);
28714 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28715 +                       break;
28716 +       }
28717 +       AuDbg("rent %llu\n", rdu->rent);
28718 +
28719 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28720 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28721 +               au_fset_rdu(cookie->flags, CONT);
28722 +               cookie->generation = au_figen(file);
28723 +       }
28724 +
28725 +       ii_read_lock_child(inode);
28726 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28727 +       ii_read_unlock(inode);
28728 +
28729 +out_unlock:
28730 +       fi_read_unlock(file);
28731 +out_si:
28732 +       si_read_unlock(arg.sb);
28733 +out_mtx:
28734 +       inode_unlock_shared(inode);
28735 +out:
28736 +       AuTraceErr(err);
28737 +       return err;
28738 +}
28739 +
28740 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28741 +{
28742 +       int err;
28743 +       ino_t ino;
28744 +       unsigned long long nent;
28745 +       union au_rdu_ent_ul *u;
28746 +       struct au_rdu_ent ent;
28747 +       struct super_block *sb;
28748 +
28749 +       err = 0;
28750 +       nent = rdu->nent;
28751 +       u = &rdu->ent;
28752 +       sb = file->f_path.dentry->d_sb;
28753 +       si_read_lock(sb, AuLock_FLUSH);
28754 +       while (nent-- > 0) {
28755 +               /* unnecessary to support mmap_sem since this is a dir */
28756 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28757 +               if (!err)
28758 +                       /* VERIFY_WRITE */
28759 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28760 +               if (unlikely(err)) {
28761 +                       err = -EFAULT;
28762 +                       AuTraceErr(err);
28763 +                       break;
28764 +               }
28765 +
28766 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28767 +               if (!ent.wh)
28768 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28769 +               else
28770 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28771 +                                       &ino);
28772 +               if (unlikely(err)) {
28773 +                       AuTraceErr(err);
28774 +                       break;
28775 +               }
28776 +
28777 +               err = __put_user(ino, &u->e->ino);
28778 +               if (unlikely(err)) {
28779 +                       err = -EFAULT;
28780 +                       AuTraceErr(err);
28781 +                       break;
28782 +               }
28783 +               u->ul += au_rdu_len(ent.nlen);
28784 +       }
28785 +       si_read_unlock(sb);
28786 +
28787 +       return err;
28788 +}
28789 +
28790 +/* ---------------------------------------------------------------------- */
28791 +
28792 +static int au_rdu_verify(struct aufs_rdu *rdu)
28793 +{
28794 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28795 +             "%llu, b%d, 0x%x, g%u}\n",
28796 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28797 +             rdu->blk,
28798 +             rdu->rent, rdu->shwh, rdu->full,
28799 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28800 +             rdu->cookie.generation);
28801 +
28802 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28803 +               return 0;
28804 +
28805 +       AuDbg("%u:%u\n",
28806 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28807 +       return -EINVAL;
28808 +}
28809 +
28810 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28811 +{
28812 +       long err, e;
28813 +       struct aufs_rdu rdu;
28814 +       void __user *p = (void __user *)arg;
28815 +
28816 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28817 +       if (unlikely(err)) {
28818 +               err = -EFAULT;
28819 +               AuTraceErr(err);
28820 +               goto out;
28821 +       }
28822 +       err = au_rdu_verify(&rdu);
28823 +       if (unlikely(err))
28824 +               goto out;
28825 +
28826 +       switch (cmd) {
28827 +       case AUFS_CTL_RDU:
28828 +               err = au_rdu(file, &rdu);
28829 +               if (unlikely(err))
28830 +                       break;
28831 +
28832 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28833 +               if (unlikely(e)) {
28834 +                       err = -EFAULT;
28835 +                       AuTraceErr(err);
28836 +               }
28837 +               break;
28838 +       case AUFS_CTL_RDU_INO:
28839 +               err = au_rdu_ino(file, &rdu);
28840 +               break;
28841 +
28842 +       default:
28843 +               /* err = -ENOTTY; */
28844 +               err = -EINVAL;
28845 +       }
28846 +
28847 +out:
28848 +       AuTraceErr(err);
28849 +       return err;
28850 +}
28851 +
28852 +#ifdef CONFIG_COMPAT
28853 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28854 +{
28855 +       long err, e;
28856 +       struct aufs_rdu rdu;
28857 +       void __user *p = compat_ptr(arg);
28858 +
28859 +       /* todo: get_user()? */
28860 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28861 +       if (unlikely(err)) {
28862 +               err = -EFAULT;
28863 +               AuTraceErr(err);
28864 +               goto out;
28865 +       }
28866 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28867 +       err = au_rdu_verify(&rdu);
28868 +       if (unlikely(err))
28869 +               goto out;
28870 +
28871 +       switch (cmd) {
28872 +       case AUFS_CTL_RDU:
28873 +               err = au_rdu(file, &rdu);
28874 +               if (unlikely(err))
28875 +                       break;
28876 +
28877 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28878 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28879 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28880 +               if (unlikely(e)) {
28881 +                       err = -EFAULT;
28882 +                       AuTraceErr(err);
28883 +               }
28884 +               break;
28885 +       case AUFS_CTL_RDU_INO:
28886 +               err = au_rdu_ino(file, &rdu);
28887 +               break;
28888 +
28889 +       default:
28890 +               /* err = -ENOTTY; */
28891 +               err = -EINVAL;
28892 +       }
28893 +
28894 +out:
28895 +       AuTraceErr(err);
28896 +       return err;
28897 +}
28898 +#endif
28899 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28900 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28901 +++ linux/fs/aufs/rwsem.h       2021-02-24 13:33:42.747680497 +0100
28902 @@ -0,0 +1,85 @@
28903 +/* SPDX-License-Identifier: GPL-2.0 */
28904 +/*
28905 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28906 + *
28907 + * This program, aufs is free software; you can redistribute it and/or modify
28908 + * it under the terms of the GNU General Public License as published by
28909 + * the Free Software Foundation; either version 2 of the License, or
28910 + * (at your option) any later version.
28911 + *
28912 + * This program is distributed in the hope that it will be useful,
28913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28915 + * GNU General Public License for more details.
28916 + *
28917 + * You should have received a copy of the GNU General Public License
28918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28919 + */
28920 +
28921 +/*
28922 + * simple read-write semaphore wrappers
28923 + */
28924 +
28925 +#ifndef __AUFS_RWSEM_H__
28926 +#define __AUFS_RWSEM_H__
28927 +
28928 +#ifdef __KERNEL__
28929 +
28930 +#include "debug.h"
28931 +
28932 +/* in the future, the name 'au_rwsem' will be totally gone */
28933 +#define au_rwsem       rw_semaphore
28934 +
28935 +/* to debug easier, do not make them inlined functions */
28936 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
28937 +
28938 +#ifdef CONFIG_LOCKDEP
28939 +/* rwsem_is_locked() is unusable */
28940 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28941 +                                         && !lockdep_recursing(current) \
28942 +                                         && debug_locks                \
28943 +                                         && !lockdep_is_held_type(rw, 1))
28944 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28945 +                                         && !lockdep_recursing(current) \
28946 +                                         && debug_locks                \
28947 +                                         && !lockdep_is_held_type(rw, 0))
28948 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28949 +                                         && !lockdep_recursing(current) \
28950 +                                         && debug_locks                \
28951 +                                         && !lockdep_is_held(rw))
28952 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
28953 +                                         && !lockdep_recursing(current) \
28954 +                                         && debug_locks                \
28955 +                                         && lockdep_is_held(rw))
28956 +#else
28957 +#define AuRwMustReadLock(rw)   do {} while (0)
28958 +#define AuRwMustWriteLock(rw)  do {} while (0)
28959 +#define AuRwMustAnyLock(rw)    do {} while (0)
28960 +#define AuRwDestroy(rw)                do {} while (0)
28961 +#endif
28962 +
28963 +#define au_rw_init(rw) init_rwsem(rw)
28964 +
28965 +#define au_rw_init_wlock(rw) do {              \
28966 +               au_rw_init(rw);                 \
28967 +               down_write(rw);                 \
28968 +       } while (0)
28969 +
28970 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
28971 +               au_rw_init(rw);                 \
28972 +               down_write_nested(rw, lsc);     \
28973 +       } while (0)
28974 +
28975 +#define au_rw_read_lock(rw)            down_read(rw)
28976 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
28977 +#define au_rw_read_unlock(rw)          up_read(rw)
28978 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
28979 +#define au_rw_write_lock(rw)           down_write(rw)
28980 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28981 +#define au_rw_write_unlock(rw)         up_write(rw)
28982 +/* why is not _nested version defined? */
28983 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
28984 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
28985 +
28986 +#endif /* __KERNEL__ */
28987 +#endif /* __AUFS_RWSEM_H__ */
28988 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28989 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
28990 +++ linux/fs/aufs/sbinfo.c      2021-02-24 13:33:42.747680497 +0100
28991 @@ -0,0 +1,314 @@
28992 +// SPDX-License-Identifier: GPL-2.0
28993 +/*
28994 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28995 + *
28996 + * This program, aufs is free software; you can redistribute it and/or modify
28997 + * it under the terms of the GNU General Public License as published by
28998 + * the Free Software Foundation; either version 2 of the License, or
28999 + * (at your option) any later version.
29000 + *
29001 + * This program is distributed in the hope that it will be useful,
29002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29004 + * GNU General Public License for more details.
29005 + *
29006 + * You should have received a copy of the GNU General Public License
29007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29008 + */
29009 +
29010 +/*
29011 + * superblock private data
29012 + */
29013 +
29014 +#include <linux/iversion.h>
29015 +#include "aufs.h"
29016 +
29017 +/*
29018 + * they are necessary regardless sysfs is disabled.
29019 + */
29020 +void au_si_free(struct kobject *kobj)
29021 +{
29022 +       int i;
29023 +       struct au_sbinfo *sbinfo;
29024 +       char *locked __maybe_unused; /* debug only */
29025 +
29026 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29027 +       for (i = 0; i < AuPlink_NHASH; i++)
29028 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29029 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29030 +
29031 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29032 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29033 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29034 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29035 +
29036 +       dbgaufs_si_fin(sbinfo);
29037 +       au_rw_write_lock(&sbinfo->si_rwsem);
29038 +       au_br_free(sbinfo);
29039 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29040 +
29041 +       au_kfree_try_rcu(sbinfo->si_branch);
29042 +       mutex_destroy(&sbinfo->si_xib_mtx);
29043 +       AuRwDestroy(&sbinfo->si_rwsem);
29044 +
29045 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29046 +       /* si_nfiles is waited too */
29047 +       au_kfree_rcu(sbinfo);
29048 +}
29049 +
29050 +int au_si_alloc(struct super_block *sb)
29051 +{
29052 +       int err, i;
29053 +       struct au_sbinfo *sbinfo;
29054 +
29055 +       err = -ENOMEM;
29056 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29057 +       if (unlikely(!sbinfo))
29058 +               goto out;
29059 +
29060 +       /* will be reallocated separately */
29061 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29062 +       if (unlikely(!sbinfo->si_branch))
29063 +               goto out_sbinfo;
29064 +
29065 +       err = sysaufs_si_init(sbinfo);
29066 +       if (!err) {
29067 +               dbgaufs_si_null(sbinfo);
29068 +               err = dbgaufs_si_init(sbinfo);
29069 +               if (unlikely(err))
29070 +                       kobject_put(&sbinfo->si_kobj);
29071 +       }
29072 +       if (unlikely(err))
29073 +               goto out_br;
29074 +
29075 +       au_nwt_init(&sbinfo->si_nowait);
29076 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29077 +
29078 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29079 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29080 +
29081 +       sbinfo->si_bbot = -1;
29082 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29083 +
29084 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29085 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29086 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29087 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29088 +
29089 +       au_fhsm_init(sbinfo);
29090 +
29091 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29092 +
29093 +       sbinfo->si_xino_jiffy = jiffies;
29094 +       sbinfo->si_xino_expire
29095 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29096 +       mutex_init(&sbinfo->si_xib_mtx);
29097 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29098 +
29099 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29100 +
29101 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29102 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29103 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29104 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29105 +
29106 +       for (i = 0; i < AuPlink_NHASH; i++)
29107 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29108 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29109 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29110 +
29111 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29112 +
29113 +       /* with getattr by default */
29114 +       sbinfo->si_iop_array = aufs_iop;
29115 +
29116 +       /* leave other members for sysaufs and si_mnt. */
29117 +       sbinfo->si_sb = sb;
29118 +       sb->s_fs_info = sbinfo;
29119 +       si_pid_set(sb);
29120 +       return 0; /* success */
29121 +
29122 +out_br:
29123 +       au_kfree_try_rcu(sbinfo->si_branch);
29124 +out_sbinfo:
29125 +       au_kfree_rcu(sbinfo);
29126 +out:
29127 +       return err;
29128 +}
29129 +
29130 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29131 +{
29132 +       int err, sz;
29133 +       struct au_branch **brp;
29134 +
29135 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29136 +
29137 +       err = -ENOMEM;
29138 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29139 +       if (unlikely(!sz))
29140 +               sz = sizeof(*brp);
29141 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29142 +                          may_shrink);
29143 +       if (brp) {
29144 +               sbinfo->si_branch = brp;
29145 +               err = 0;
29146 +       }
29147 +
29148 +       return err;
29149 +}
29150 +
29151 +/* ---------------------------------------------------------------------- */
29152 +
29153 +unsigned int au_sigen_inc(struct super_block *sb)
29154 +{
29155 +       unsigned int gen;
29156 +       struct inode *inode;
29157 +
29158 +       SiMustWriteLock(sb);
29159 +
29160 +       gen = ++au_sbi(sb)->si_generation;
29161 +       au_update_digen(sb->s_root);
29162 +       inode = d_inode(sb->s_root);
29163 +       au_update_iigen(inode, /*half*/0);
29164 +       inode_inc_iversion(inode);
29165 +       return gen;
29166 +}
29167 +
29168 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29169 +{
29170 +       aufs_bindex_t br_id;
29171 +       int i;
29172 +       struct au_sbinfo *sbinfo;
29173 +
29174 +       SiMustWriteLock(sb);
29175 +
29176 +       sbinfo = au_sbi(sb);
29177 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29178 +               br_id = ++sbinfo->si_last_br_id;
29179 +               AuDebugOn(br_id < 0);
29180 +               if (br_id && au_br_index(sb, br_id) < 0)
29181 +                       return br_id;
29182 +       }
29183 +
29184 +       return -1;
29185 +}
29186 +
29187 +/* ---------------------------------------------------------------------- */
29188 +
29189 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29190 +int si_read_lock(struct super_block *sb, int flags)
29191 +{
29192 +       int err;
29193 +
29194 +       err = 0;
29195 +       if (au_ftest_lock(flags, FLUSH))
29196 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29197 +
29198 +       si_noflush_read_lock(sb);
29199 +       err = au_plink_maint(sb, flags);
29200 +       if (unlikely(err))
29201 +               si_read_unlock(sb);
29202 +
29203 +       return err;
29204 +}
29205 +
29206 +int si_write_lock(struct super_block *sb, int flags)
29207 +{
29208 +       int err;
29209 +
29210 +       if (au_ftest_lock(flags, FLUSH))
29211 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29212 +
29213 +       si_noflush_write_lock(sb);
29214 +       err = au_plink_maint(sb, flags);
29215 +       if (unlikely(err))
29216 +               si_write_unlock(sb);
29217 +
29218 +       return err;
29219 +}
29220 +
29221 +/* dentry and super_block lock. call at entry point */
29222 +int aufs_read_lock(struct dentry *dentry, int flags)
29223 +{
29224 +       int err;
29225 +       struct super_block *sb;
29226 +
29227 +       sb = dentry->d_sb;
29228 +       err = si_read_lock(sb, flags);
29229 +       if (unlikely(err))
29230 +               goto out;
29231 +
29232 +       if (au_ftest_lock(flags, DW))
29233 +               di_write_lock_child(dentry);
29234 +       else
29235 +               di_read_lock_child(dentry, flags);
29236 +
29237 +       if (au_ftest_lock(flags, GEN)) {
29238 +               err = au_digen_test(dentry, au_sigen(sb));
29239 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29240 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29241 +               else if (!err)
29242 +                       err = au_dbrange_test(dentry);
29243 +               if (unlikely(err))
29244 +                       aufs_read_unlock(dentry, flags);
29245 +       }
29246 +
29247 +out:
29248 +       return err;
29249 +}
29250 +
29251 +void aufs_read_unlock(struct dentry *dentry, int flags)
29252 +{
29253 +       if (au_ftest_lock(flags, DW))
29254 +               di_write_unlock(dentry);
29255 +       else
29256 +               di_read_unlock(dentry, flags);
29257 +       si_read_unlock(dentry->d_sb);
29258 +}
29259 +
29260 +void aufs_write_lock(struct dentry *dentry)
29261 +{
29262 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29263 +       di_write_lock_child(dentry);
29264 +}
29265 +
29266 +void aufs_write_unlock(struct dentry *dentry)
29267 +{
29268 +       di_write_unlock(dentry);
29269 +       si_write_unlock(dentry->d_sb);
29270 +}
29271 +
29272 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29273 +{
29274 +       int err;
29275 +       unsigned int sigen;
29276 +       struct super_block *sb;
29277 +
29278 +       sb = d1->d_sb;
29279 +       err = si_read_lock(sb, flags);
29280 +       if (unlikely(err))
29281 +               goto out;
29282 +
29283 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29284 +
29285 +       if (au_ftest_lock(flags, GEN)) {
29286 +               sigen = au_sigen(sb);
29287 +               err = au_digen_test(d1, sigen);
29288 +               AuDebugOn(!err && au_dbrange_test(d1));
29289 +               if (!err) {
29290 +                       err = au_digen_test(d2, sigen);
29291 +                       AuDebugOn(!err && au_dbrange_test(d2));
29292 +               }
29293 +               if (unlikely(err))
29294 +                       aufs_read_and_write_unlock2(d1, d2);
29295 +       }
29296 +
29297 +out:
29298 +       return err;
29299 +}
29300 +
29301 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29302 +{
29303 +       di_write_unlock2(d1, d2);
29304 +       si_read_unlock(d1->d_sb);
29305 +}
29306 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29307 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29308 +++ linux/fs/aufs/super.c       2021-02-24 13:33:42.747680497 +0100
29309 @@ -0,0 +1,1047 @@
29310 +// SPDX-License-Identifier: GPL-2.0
29311 +/*
29312 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29313 + *
29314 + * This program, aufs is free software; you can redistribute it and/or modify
29315 + * it under the terms of the GNU General Public License as published by
29316 + * the Free Software Foundation; either version 2 of the License, or
29317 + * (at your option) any later version.
29318 + *
29319 + * This program is distributed in the hope that it will be useful,
29320 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29321 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29322 + * GNU General Public License for more details.
29323 + *
29324 + * You should have received a copy of the GNU General Public License
29325 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29326 + */
29327 +
29328 +/*
29329 + * mount and super_block operations
29330 + */
29331 +
29332 +#include <linux/iversion.h>
29333 +#include <linux/mm.h>
29334 +#include <linux/seq_file.h>
29335 +#include <linux/statfs.h>
29336 +#include <linux/vmalloc.h>
29337 +#include "aufs.h"
29338 +
29339 +/*
29340 + * super_operations
29341 + */
29342 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29343 +{
29344 +       struct au_icntnr *c;
29345 +
29346 +       c = au_cache_alloc_icntnr();
29347 +       if (c) {
29348 +               au_icntnr_init(c);
29349 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29350 +               c->iinfo.ii_hinode = NULL;
29351 +               return &c->vfs_inode;
29352 +       }
29353 +       return NULL;
29354 +}
29355 +
29356 +static void aufs_destroy_inode(struct inode *inode)
29357 +{
29358 +       if (!au_is_bad_inode(inode))
29359 +               au_iinfo_fin(inode);
29360 +}
29361 +
29362 +static void aufs_free_inode(struct inode *inode)
29363 +{
29364 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29365 +}
29366 +
29367 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29368 +{
29369 +       struct inode *inode;
29370 +       int err;
29371 +
29372 +       inode = iget_locked(sb, ino);
29373 +       if (unlikely(!inode)) {
29374 +               inode = ERR_PTR(-ENOMEM);
29375 +               goto out;
29376 +       }
29377 +       if (!(inode->i_state & I_NEW))
29378 +               goto out;
29379 +
29380 +       err = au_xigen_new(inode);
29381 +       if (!err)
29382 +               err = au_iinfo_init(inode);
29383 +       if (!err)
29384 +               inode_inc_iversion(inode);
29385 +       else {
29386 +               iget_failed(inode);
29387 +               inode = ERR_PTR(err);
29388 +       }
29389 +
29390 +out:
29391 +       /* never return NULL */
29392 +       AuDebugOn(!inode);
29393 +       AuTraceErrPtr(inode);
29394 +       return inode;
29395 +}
29396 +
29397 +/* lock free root dinfo */
29398 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29399 +{
29400 +       int err;
29401 +       aufs_bindex_t bindex, bbot;
29402 +       struct path path;
29403 +       struct au_hdentry *hdp;
29404 +       struct au_branch *br;
29405 +       au_br_perm_str_t perm;
29406 +
29407 +       err = 0;
29408 +       bbot = au_sbbot(sb);
29409 +       bindex = 0;
29410 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29411 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29412 +               br = au_sbr(sb, bindex);
29413 +               path.mnt = au_br_mnt(br);
29414 +               path.dentry = hdp->hd_dentry;
29415 +               err = au_seq_path(seq, &path);
29416 +               if (!err) {
29417 +                       au_optstr_br_perm(&perm, br->br_perm);
29418 +                       seq_printf(seq, "=%s", perm.a);
29419 +                       if (bindex != bbot)
29420 +                               seq_putc(seq, ':');
29421 +               }
29422 +       }
29423 +       if (unlikely(err || seq_has_overflowed(seq)))
29424 +               err = -E2BIG;
29425 +
29426 +       return err;
29427 +}
29428 +
29429 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29430 +                      const char *append)
29431 +{
29432 +       char *p;
29433 +
29434 +       p = fmt;
29435 +       while (*pat != ':')
29436 +               *p++ = *pat++;
29437 +       *p++ = *pat++;
29438 +       strcpy(p, append);
29439 +       AuDebugOn(strlen(fmt) >= len);
29440 +}
29441 +
29442 +static void au_show_wbr_create(struct seq_file *m, int v,
29443 +                              struct au_sbinfo *sbinfo)
29444 +{
29445 +       const char *pat;
29446 +       char fmt[32];
29447 +       struct au_wbr_mfs *mfs;
29448 +
29449 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29450 +
29451 +       seq_puts(m, ",create=");
29452 +       pat = au_optstr_wbr_create(v);
29453 +       mfs = &sbinfo->si_wbr_mfs;
29454 +       switch (v) {
29455 +       case AuWbrCreate_TDP:
29456 +       case AuWbrCreate_RR:
29457 +       case AuWbrCreate_MFS:
29458 +       case AuWbrCreate_PMFS:
29459 +               seq_puts(m, pat);
29460 +               break;
29461 +       case AuWbrCreate_MFSRR:
29462 +       case AuWbrCreate_TDMFS:
29463 +       case AuWbrCreate_PMFSRR:
29464 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29465 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29466 +               break;
29467 +       case AuWbrCreate_MFSV:
29468 +       case AuWbrCreate_PMFSV:
29469 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29470 +               seq_printf(m, fmt,
29471 +                          jiffies_to_msecs(mfs->mfs_expire)
29472 +                          / MSEC_PER_SEC);
29473 +               break;
29474 +       case AuWbrCreate_MFSRRV:
29475 +       case AuWbrCreate_TDMFSV:
29476 +       case AuWbrCreate_PMFSRRV:
29477 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29478 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29479 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29480 +               break;
29481 +       default:
29482 +               BUG();
29483 +       }
29484 +}
29485 +
29486 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29487 +{
29488 +#ifdef CONFIG_SYSFS
29489 +       return 0;
29490 +#else
29491 +       int err;
29492 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29493 +       aufs_bindex_t bindex, brid;
29494 +       struct qstr *name;
29495 +       struct file *f;
29496 +       struct dentry *d, *h_root;
29497 +       struct au_branch *br;
29498 +
29499 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29500 +
29501 +       err = 0;
29502 +       f = au_sbi(sb)->si_xib;
29503 +       if (!f)
29504 +               goto out;
29505 +
29506 +       /* stop printing the default xino path on the first writable branch */
29507 +       h_root = NULL;
29508 +       bindex = au_xi_root(sb, f->f_path.dentry);
29509 +       if (bindex >= 0) {
29510 +               br = au_sbr_sb(sb, bindex);
29511 +               h_root = au_br_dentry(br);
29512 +       }
29513 +
29514 +       d = f->f_path.dentry;
29515 +       name = &d->d_name;
29516 +       /* safe ->d_parent because the file is unlinked */
29517 +       if (d->d_parent == h_root
29518 +           && name->len == len
29519 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29520 +               goto out;
29521 +
29522 +       seq_puts(seq, ",xino=");
29523 +       err = au_xino_path(seq, f);
29524 +
29525 +out:
29526 +       return err;
29527 +#endif
29528 +}
29529 +
29530 +/* seq_file will re-call me in case of too long string */
29531 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29532 +{
29533 +       int err;
29534 +       unsigned int mnt_flags, v;
29535 +       struct super_block *sb;
29536 +       struct au_sbinfo *sbinfo;
29537 +
29538 +#define AuBool(name, str) do { \
29539 +       v = au_opt_test(mnt_flags, name); \
29540 +       if (v != au_opt_test(AuOpt_Def, name)) \
29541 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29542 +} while (0)
29543 +
29544 +#define AuStr(name, str) do { \
29545 +       v = mnt_flags & AuOptMask_##name; \
29546 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29547 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29548 +} while (0)
29549 +
29550 +#define AuUInt(name, str, val) do { \
29551 +       if (val != AUFS_##name##_DEF) \
29552 +               seq_printf(m, "," #str "=%u", val); \
29553 +} while (0)
29554 +
29555 +       sb = dentry->d_sb;
29556 +       if (sb->s_flags & SB_POSIXACL)
29557 +               seq_puts(m, ",acl");
29558 +#if 0 /* reserved for future use */
29559 +       if (sb->s_flags & SB_I_VERSION)
29560 +               seq_puts(m, ",i_version");
29561 +#endif
29562 +
29563 +       /* lock free root dinfo */
29564 +       si_noflush_read_lock(sb);
29565 +       sbinfo = au_sbi(sb);
29566 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29567 +
29568 +       mnt_flags = au_mntflags(sb);
29569 +       if (au_opt_test(mnt_flags, XINO)) {
29570 +               err = au_show_xino(m, sb);
29571 +               if (unlikely(err))
29572 +                       goto out;
29573 +       } else
29574 +               seq_puts(m, ",noxino");
29575 +
29576 +       AuBool(TRUNC_XINO, trunc_xino);
29577 +       AuStr(UDBA, udba);
29578 +       AuBool(SHWH, shwh);
29579 +       AuBool(PLINK, plink);
29580 +       AuBool(DIO, dio);
29581 +       AuBool(DIRPERM1, dirperm1);
29582 +
29583 +       v = sbinfo->si_wbr_create;
29584 +       if (v != AuWbrCreate_Def)
29585 +               au_show_wbr_create(m, v, sbinfo);
29586 +
29587 +       v = sbinfo->si_wbr_copyup;
29588 +       if (v != AuWbrCopyup_Def)
29589 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29590 +
29591 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29592 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29593 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29594 +
29595 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29596 +
29597 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29598 +       AuUInt(RDCACHE, rdcache, v);
29599 +
29600 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29601 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29602 +
29603 +       au_fhsm_show(m, sbinfo);
29604 +
29605 +       AuBool(DIRREN, dirren);
29606 +       AuBool(SUM, sum);
29607 +       /* AuBool(SUM_W, wsum); */
29608 +       AuBool(WARN_PERM, warn_perm);
29609 +       AuBool(VERBOSE, verbose);
29610 +
29611 +out:
29612 +       /* be sure to print "br:" last */
29613 +       if (!sysaufs_brs) {
29614 +               seq_puts(m, ",br:");
29615 +               au_show_brs(m, sb);
29616 +       }
29617 +       si_read_unlock(sb);
29618 +       return 0;
29619 +
29620 +#undef AuBool
29621 +#undef AuStr
29622 +#undef AuUInt
29623 +}
29624 +
29625 +/* ---------------------------------------------------------------------- */
29626 +
29627 +/* sum mode which returns the summation for statfs(2) */
29628 +
29629 +static u64 au_add_till_max(u64 a, u64 b)
29630 +{
29631 +       u64 old;
29632 +
29633 +       old = a;
29634 +       a += b;
29635 +       if (old <= a)
29636 +               return a;
29637 +       return ULLONG_MAX;
29638 +}
29639 +
29640 +static u64 au_mul_till_max(u64 a, long mul)
29641 +{
29642 +       u64 old;
29643 +
29644 +       old = a;
29645 +       a *= mul;
29646 +       if (old <= a)
29647 +               return a;
29648 +       return ULLONG_MAX;
29649 +}
29650 +
29651 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29652 +{
29653 +       int err;
29654 +       long bsize, factor;
29655 +       u64 blocks, bfree, bavail, files, ffree;
29656 +       aufs_bindex_t bbot, bindex, i;
29657 +       unsigned char shared;
29658 +       struct path h_path;
29659 +       struct super_block *h_sb;
29660 +
29661 +       err = 0;
29662 +       bsize = LONG_MAX;
29663 +       files = 0;
29664 +       ffree = 0;
29665 +       blocks = 0;
29666 +       bfree = 0;
29667 +       bavail = 0;
29668 +       bbot = au_sbbot(sb);
29669 +       for (bindex = 0; bindex <= bbot; bindex++) {
29670 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29671 +               h_sb = h_path.mnt->mnt_sb;
29672 +               shared = 0;
29673 +               for (i = 0; !shared && i < bindex; i++)
29674 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29675 +               if (shared)
29676 +                       continue;
29677 +
29678 +               /* sb->s_root for NFS is unreliable */
29679 +               h_path.dentry = h_path.mnt->mnt_root;
29680 +               err = vfs_statfs(&h_path, buf);
29681 +               if (unlikely(err))
29682 +                       goto out;
29683 +
29684 +               if (bsize > buf->f_bsize) {
29685 +                       /*
29686 +                        * we will reduce bsize, so we have to expand blocks
29687 +                        * etc. to match them again
29688 +                        */
29689 +                       factor = (bsize / buf->f_bsize);
29690 +                       blocks = au_mul_till_max(blocks, factor);
29691 +                       bfree = au_mul_till_max(bfree, factor);
29692 +                       bavail = au_mul_till_max(bavail, factor);
29693 +                       bsize = buf->f_bsize;
29694 +               }
29695 +
29696 +               factor = (buf->f_bsize / bsize);
29697 +               blocks = au_add_till_max(blocks,
29698 +                               au_mul_till_max(buf->f_blocks, factor));
29699 +               bfree = au_add_till_max(bfree,
29700 +                               au_mul_till_max(buf->f_bfree, factor));
29701 +               bavail = au_add_till_max(bavail,
29702 +                               au_mul_till_max(buf->f_bavail, factor));
29703 +               files = au_add_till_max(files, buf->f_files);
29704 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29705 +       }
29706 +
29707 +       buf->f_bsize = bsize;
29708 +       buf->f_blocks = blocks;
29709 +       buf->f_bfree = bfree;
29710 +       buf->f_bavail = bavail;
29711 +       buf->f_files = files;
29712 +       buf->f_ffree = ffree;
29713 +       buf->f_frsize = 0;
29714 +
29715 +out:
29716 +       return err;
29717 +}
29718 +
29719 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29720 +{
29721 +       int err;
29722 +       struct path h_path;
29723 +       struct super_block *sb;
29724 +
29725 +       /* lock free root dinfo */
29726 +       sb = dentry->d_sb;
29727 +       si_noflush_read_lock(sb);
29728 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29729 +               /* sb->s_root for NFS is unreliable */
29730 +               h_path.mnt = au_sbr_mnt(sb, 0);
29731 +               h_path.dentry = h_path.mnt->mnt_root;
29732 +               err = vfs_statfs(&h_path, buf);
29733 +       } else
29734 +               err = au_statfs_sum(sb, buf);
29735 +       si_read_unlock(sb);
29736 +
29737 +       if (!err) {
29738 +               buf->f_type = AUFS_SUPER_MAGIC;
29739 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29740 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29741 +       }
29742 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29743 +
29744 +       return err;
29745 +}
29746 +
29747 +/* ---------------------------------------------------------------------- */
29748 +
29749 +static int aufs_sync_fs(struct super_block *sb, int wait)
29750 +{
29751 +       int err, e;
29752 +       aufs_bindex_t bbot, bindex;
29753 +       struct au_branch *br;
29754 +       struct super_block *h_sb;
29755 +
29756 +       err = 0;
29757 +       si_noflush_read_lock(sb);
29758 +       bbot = au_sbbot(sb);
29759 +       for (bindex = 0; bindex <= bbot; bindex++) {
29760 +               br = au_sbr(sb, bindex);
29761 +               if (!au_br_writable(br->br_perm))
29762 +                       continue;
29763 +
29764 +               h_sb = au_sbr_sb(sb, bindex);
29765 +               e = vfsub_sync_filesystem(h_sb, wait);
29766 +               if (unlikely(e && !err))
29767 +                       err = e;
29768 +               /* go on even if an error happens */
29769 +       }
29770 +       si_read_unlock(sb);
29771 +
29772 +       return err;
29773 +}
29774 +
29775 +/* ---------------------------------------------------------------------- */
29776 +
29777 +/* final actions when unmounting a file system */
29778 +static void aufs_put_super(struct super_block *sb)
29779 +{
29780 +       struct au_sbinfo *sbinfo;
29781 +
29782 +       sbinfo = au_sbi(sb);
29783 +       if (sbinfo)
29784 +               kobject_put(&sbinfo->si_kobj);
29785 +}
29786 +
29787 +/* ---------------------------------------------------------------------- */
29788 +
29789 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29790 +                    struct super_block *sb, void *arg)
29791 +{
29792 +       void *array;
29793 +       unsigned long long n, sz;
29794 +
29795 +       array = NULL;
29796 +       n = 0;
29797 +       if (!*hint)
29798 +               goto out;
29799 +
29800 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29801 +               array = ERR_PTR(-EMFILE);
29802 +               pr_err("hint %llu\n", *hint);
29803 +               goto out;
29804 +       }
29805 +
29806 +       sz = sizeof(array) * *hint;
29807 +       array = kzalloc(sz, GFP_NOFS);
29808 +       if (unlikely(!array))
29809 +               array = vzalloc(sz);
29810 +       if (unlikely(!array)) {
29811 +               array = ERR_PTR(-ENOMEM);
29812 +               goto out;
29813 +       }
29814 +
29815 +       n = cb(sb, array, *hint, arg);
29816 +       AuDebugOn(n > *hint);
29817 +
29818 +out:
29819 +       *hint = n;
29820 +       return array;
29821 +}
29822 +
29823 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29824 +                                      unsigned long long max __maybe_unused,
29825 +                                      void *arg)
29826 +{
29827 +       unsigned long long n;
29828 +       struct inode **p, *inode;
29829 +       struct list_head *head;
29830 +
29831 +       n = 0;
29832 +       p = a;
29833 +       head = arg;
29834 +       spin_lock(&sb->s_inode_list_lock);
29835 +       list_for_each_entry(inode, head, i_sb_list) {
29836 +               if (!au_is_bad_inode(inode)
29837 +                   && au_ii(inode)->ii_btop >= 0) {
29838 +                       spin_lock(&inode->i_lock);
29839 +                       if (atomic_read(&inode->i_count)) {
29840 +                               au_igrab(inode);
29841 +                               *p++ = inode;
29842 +                               n++;
29843 +                               AuDebugOn(n > max);
29844 +                       }
29845 +                       spin_unlock(&inode->i_lock);
29846 +               }
29847 +       }
29848 +       spin_unlock(&sb->s_inode_list_lock);
29849 +
29850 +       return n;
29851 +}
29852 +
29853 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29854 +{
29855 +       struct au_sbinfo *sbi;
29856 +
29857 +       sbi = au_sbi(sb);
29858 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29859 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29860 +}
29861 +
29862 +void au_iarray_free(struct inode **a, unsigned long long max)
29863 +{
29864 +       unsigned long long ull;
29865 +
29866 +       for (ull = 0; ull < max; ull++)
29867 +               iput(a[ull]);
29868 +       kvfree(a);
29869 +}
29870 +
29871 +/* ---------------------------------------------------------------------- */
29872 +
29873 +/*
29874 + * refresh dentry and inode at remount time.
29875 + */
29876 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29877 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29878 +                     struct dentry *parent)
29879 +{
29880 +       int err;
29881 +
29882 +       di_write_lock_child(dentry);
29883 +       di_read_lock_parent(parent, AuLock_IR);
29884 +       err = au_refresh_dentry(dentry, parent);
29885 +       if (!err && dir_flags)
29886 +               au_hn_reset(d_inode(dentry), dir_flags);
29887 +       di_read_unlock(parent, AuLock_IR);
29888 +       di_write_unlock(dentry);
29889 +
29890 +       return err;
29891 +}
29892 +
29893 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29894 +                          struct au_sbinfo *sbinfo,
29895 +                          const unsigned int dir_flags, unsigned int do_idop)
29896 +{
29897 +       int err;
29898 +       struct dentry *parent;
29899 +
29900 +       err = 0;
29901 +       parent = dget_parent(dentry);
29902 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29903 +               if (d_really_is_positive(dentry)) {
29904 +                       if (!d_is_dir(dentry))
29905 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29906 +                                                parent);
29907 +                       else {
29908 +                               err = au_do_refresh(dentry, dir_flags, parent);
29909 +                               if (unlikely(err))
29910 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29911 +                       }
29912 +               } else
29913 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29914 +               AuDbgDentry(dentry);
29915 +       }
29916 +       dput(parent);
29917 +
29918 +       if (!err) {
29919 +               if (do_idop)
29920 +                       au_refresh_dop(dentry, /*force_reval*/0);
29921 +       } else
29922 +               au_refresh_dop(dentry, /*force_reval*/1);
29923 +
29924 +       AuTraceErr(err);
29925 +       return err;
29926 +}
29927 +
29928 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29929 +{
29930 +       int err, i, j, ndentry, e;
29931 +       unsigned int sigen;
29932 +       struct au_dcsub_pages dpages;
29933 +       struct au_dpage *dpage;
29934 +       struct dentry **dentries, *d;
29935 +       struct au_sbinfo *sbinfo;
29936 +       struct dentry *root = sb->s_root;
29937 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29938 +
29939 +       if (do_idop)
29940 +               au_refresh_dop(root, /*force_reval*/0);
29941 +
29942 +       err = au_dpages_init(&dpages, GFP_NOFS);
29943 +       if (unlikely(err))
29944 +               goto out;
29945 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
29946 +       if (unlikely(err))
29947 +               goto out_dpages;
29948 +
29949 +       sigen = au_sigen(sb);
29950 +       sbinfo = au_sbi(sb);
29951 +       for (i = 0; i < dpages.ndpage; i++) {
29952 +               dpage = dpages.dpages + i;
29953 +               dentries = dpage->dentries;
29954 +               ndentry = dpage->ndentry;
29955 +               for (j = 0; j < ndentry; j++) {
29956 +                       d = dentries[j];
29957 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
29958 +                                           do_idop);
29959 +                       if (unlikely(e && !err))
29960 +                               err = e;
29961 +                       /* go on even err */
29962 +               }
29963 +       }
29964 +
29965 +out_dpages:
29966 +       au_dpages_free(&dpages);
29967 +out:
29968 +       return err;
29969 +}
29970 +
29971 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
29972 +{
29973 +       int err, e;
29974 +       unsigned int sigen;
29975 +       unsigned long long max, ull;
29976 +       struct inode *inode, **array;
29977 +
29978 +       array = au_iarray_alloc(sb, &max);
29979 +       err = PTR_ERR(array);
29980 +       if (IS_ERR(array))
29981 +               goto out;
29982 +
29983 +       err = 0;
29984 +       sigen = au_sigen(sb);
29985 +       for (ull = 0; ull < max; ull++) {
29986 +               inode = array[ull];
29987 +               if (unlikely(!inode))
29988 +                       break;
29989 +
29990 +               e = 0;
29991 +               ii_write_lock_child(inode);
29992 +               if (au_iigen(inode, NULL) != sigen) {
29993 +                       e = au_refresh_hinode_self(inode);
29994 +                       if (unlikely(e)) {
29995 +                               au_refresh_iop(inode, /*force_getattr*/1);
29996 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
29997 +                               if (!err)
29998 +                                       err = e;
29999 +                               /* go on even if err */
30000 +                       }
30001 +               }
30002 +               if (!e && do_idop)
30003 +                       au_refresh_iop(inode, /*force_getattr*/0);
30004 +               ii_write_unlock(inode);
30005 +       }
30006 +
30007 +       au_iarray_free(array, max);
30008 +
30009 +out:
30010 +       return err;
30011 +}
30012 +
30013 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30014 +{
30015 +       int err, e;
30016 +       unsigned int udba;
30017 +       aufs_bindex_t bindex, bbot;
30018 +       struct dentry *root;
30019 +       struct inode *inode;
30020 +       struct au_branch *br;
30021 +       struct au_sbinfo *sbi;
30022 +
30023 +       au_sigen_inc(sb);
30024 +       sbi = au_sbi(sb);
30025 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30026 +
30027 +       root = sb->s_root;
30028 +       DiMustNoWaiters(root);
30029 +       inode = d_inode(root);
30030 +       IiMustNoWaiters(inode);
30031 +
30032 +       udba = au_opt_udba(sb);
30033 +       bbot = au_sbbot(sb);
30034 +       for (bindex = 0; bindex <= bbot; bindex++) {
30035 +               br = au_sbr(sb, bindex);
30036 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30037 +               if (unlikely(err))
30038 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30039 +                               bindex, err);
30040 +               /* go on even if err */
30041 +       }
30042 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30043 +
30044 +       if (do_idop) {
30045 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30046 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30047 +                       sb->s_d_op = &aufs_dop_noreval;
30048 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30049 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30050 +               } else {
30051 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30052 +                       sb->s_d_op = &aufs_dop;
30053 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30054 +                       sbi->si_iop_array = aufs_iop;
30055 +               }
30056 +               pr_info("reset to %ps and %ps\n",
30057 +                       sb->s_d_op, sbi->si_iop_array);
30058 +       }
30059 +
30060 +       di_write_unlock(root);
30061 +       err = au_refresh_d(sb, do_idop);
30062 +       e = au_refresh_i(sb, do_idop);
30063 +       if (unlikely(e && !err))
30064 +               err = e;
30065 +       /* aufs_write_lock() calls ..._child() */
30066 +       di_write_lock_child(root);
30067 +
30068 +       au_cpup_attr_all(inode, /*force*/1);
30069 +
30070 +       if (unlikely(err))
30071 +               AuIOErr("refresh failed, ignored, %d\n", err);
30072 +}
30073 +
30074 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30075 +static int cvt_err(int err)
30076 +{
30077 +       AuTraceErr(err);
30078 +
30079 +       switch (err) {
30080 +       case -ENOENT:
30081 +       case -ENOTDIR:
30082 +       case -EEXIST:
30083 +       case -EIO:
30084 +               err = -EINVAL;
30085 +       }
30086 +       return err;
30087 +}
30088 +
30089 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30090 +{
30091 +       int err, do_dx;
30092 +       unsigned int mntflags;
30093 +       struct au_opts opts = {
30094 +               .opt = NULL
30095 +       };
30096 +       struct dentry *root;
30097 +       struct inode *inode;
30098 +       struct au_sbinfo *sbinfo;
30099 +
30100 +       err = 0;
30101 +       root = sb->s_root;
30102 +       if (!data || !*data) {
30103 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30104 +               if (!err) {
30105 +                       di_write_lock_child(root);
30106 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30107 +                       aufs_write_unlock(root);
30108 +               }
30109 +               goto out;
30110 +       }
30111 +
30112 +       err = -ENOMEM;
30113 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30114 +       if (unlikely(!opts.opt))
30115 +               goto out;
30116 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30117 +       opts.flags = AuOpts_REMOUNT;
30118 +       opts.sb_flags = *flags;
30119 +
30120 +       /* parse it before aufs lock */
30121 +       err = au_opts_parse(sb, data, &opts);
30122 +       if (unlikely(err))
30123 +               goto out_opts;
30124 +
30125 +       sbinfo = au_sbi(sb);
30126 +       inode = d_inode(root);
30127 +       inode_lock(inode);
30128 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30129 +       if (unlikely(err))
30130 +               goto out_mtx;
30131 +       di_write_lock_child(root);
30132 +
30133 +       /* au_opts_remount() may return an error */
30134 +       err = au_opts_remount(sb, &opts);
30135 +       au_opts_free(&opts);
30136 +
30137 +       if (au_ftest_opts(opts.flags, REFRESH))
30138 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30139 +
30140 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30141 +               mntflags = au_mntflags(sb);
30142 +               do_dx = !!au_opt_test(mntflags, DIO);
30143 +               au_dy_arefresh(do_dx);
30144 +       }
30145 +
30146 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30147 +       aufs_write_unlock(root);
30148 +
30149 +out_mtx:
30150 +       inode_unlock(inode);
30151 +out_opts:
30152 +       free_page((unsigned long)opts.opt);
30153 +out:
30154 +       err = cvt_err(err);
30155 +       AuTraceErr(err);
30156 +       return err;
30157 +}
30158 +
30159 +static const struct super_operations aufs_sop = {
30160 +       .alloc_inode    = aufs_alloc_inode,
30161 +       .destroy_inode  = aufs_destroy_inode,
30162 +       .free_inode     = aufs_free_inode,
30163 +       /* always deleting, no clearing */
30164 +       .drop_inode     = generic_delete_inode,
30165 +       .show_options   = aufs_show_options,
30166 +       .statfs         = aufs_statfs,
30167 +       .put_super      = aufs_put_super,
30168 +       .sync_fs        = aufs_sync_fs,
30169 +       .remount_fs     = aufs_remount_fs
30170 +};
30171 +
30172 +/* ---------------------------------------------------------------------- */
30173 +
30174 +static int alloc_root(struct super_block *sb)
30175 +{
30176 +       int err;
30177 +       struct inode *inode;
30178 +       struct dentry *root;
30179 +
30180 +       err = -ENOMEM;
30181 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30182 +       err = PTR_ERR(inode);
30183 +       if (IS_ERR(inode))
30184 +               goto out;
30185 +
30186 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30187 +       inode->i_fop = &aufs_dir_fop;
30188 +       inode->i_mode = S_IFDIR;
30189 +       set_nlink(inode, 2);
30190 +       unlock_new_inode(inode);
30191 +
30192 +       root = d_make_root(inode);
30193 +       if (unlikely(!root))
30194 +               goto out;
30195 +       err = PTR_ERR(root);
30196 +       if (IS_ERR(root))
30197 +               goto out;
30198 +
30199 +       err = au_di_init(root);
30200 +       if (!err) {
30201 +               sb->s_root = root;
30202 +               return 0; /* success */
30203 +       }
30204 +       dput(root);
30205 +
30206 +out:
30207 +       return err;
30208 +}
30209 +
30210 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30211 +                          int silent __maybe_unused)
30212 +{
30213 +       int err;
30214 +       struct au_opts opts = {
30215 +               .opt = NULL
30216 +       };
30217 +       struct au_sbinfo *sbinfo;
30218 +       struct dentry *root;
30219 +       struct inode *inode;
30220 +       char *arg = raw_data;
30221 +
30222 +       if (unlikely(!arg || !*arg)) {
30223 +               err = -EINVAL;
30224 +               pr_err("no arg\n");
30225 +               goto out;
30226 +       }
30227 +
30228 +       err = -ENOMEM;
30229 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30230 +       if (unlikely(!opts.opt))
30231 +               goto out;
30232 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30233 +       opts.sb_flags = sb->s_flags;
30234 +
30235 +       err = au_si_alloc(sb);
30236 +       if (unlikely(err))
30237 +               goto out_opts;
30238 +       sbinfo = au_sbi(sb);
30239 +
30240 +       /* all timestamps always follow the ones on the branch */
30241 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30242 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30243 +       sb->s_op = &aufs_sop;
30244 +       sb->s_d_op = &aufs_dop;
30245 +       sb->s_magic = AUFS_SUPER_MAGIC;
30246 +       sb->s_maxbytes = 0;
30247 +       sb->s_stack_depth = 1;
30248 +       au_export_init(sb);
30249 +       au_xattr_init(sb);
30250 +
30251 +       err = alloc_root(sb);
30252 +       if (unlikely(err)) {
30253 +               si_write_unlock(sb);
30254 +               goto out_info;
30255 +       }
30256 +       root = sb->s_root;
30257 +       inode = d_inode(root);
30258 +
30259 +       /*
30260 +        * actually we can parse options regardless aufs lock here.
30261 +        * but at remount time, parsing must be done before aufs lock.
30262 +        * so we follow the same rule.
30263 +        */
30264 +       ii_write_lock_parent(inode);
30265 +       aufs_write_unlock(root);
30266 +       err = au_opts_parse(sb, arg, &opts);
30267 +       if (unlikely(err))
30268 +               goto out_root;
30269 +
30270 +       /* lock vfs_inode first, then aufs. */
30271 +       inode_lock(inode);
30272 +       aufs_write_lock(root);
30273 +       err = au_opts_mount(sb, &opts);
30274 +       au_opts_free(&opts);
30275 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30276 +               sb->s_d_op = &aufs_dop_noreval;
30277 +               pr_info("%ps\n", sb->s_d_op);
30278 +               au_refresh_dop(root, /*force_reval*/0);
30279 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30280 +               au_refresh_iop(inode, /*force_getattr*/0);
30281 +       }
30282 +       aufs_write_unlock(root);
30283 +       inode_unlock(inode);
30284 +       if (!err)
30285 +               goto out_opts; /* success */
30286 +
30287 +out_root:
30288 +       dput(root);
30289 +       sb->s_root = NULL;
30290 +out_info:
30291 +       kobject_put(&sbinfo->si_kobj);
30292 +       sb->s_fs_info = NULL;
30293 +out_opts:
30294 +       free_page((unsigned long)opts.opt);
30295 +out:
30296 +       AuTraceErr(err);
30297 +       err = cvt_err(err);
30298 +       AuTraceErr(err);
30299 +       return err;
30300 +}
30301 +
30302 +/* ---------------------------------------------------------------------- */
30303 +
30304 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30305 +                                const char *dev_name __maybe_unused,
30306 +                                void *raw_data)
30307 +{
30308 +       struct dentry *root;
30309 +
30310 +       /* all timestamps always follow the ones on the branch */
30311 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30312 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30313 +       if (IS_ERR(root))
30314 +               goto out;
30315 +
30316 +       au_sbilist_add(root->d_sb);
30317 +
30318 +out:
30319 +       return root;
30320 +}
30321 +
30322 +static void aufs_kill_sb(struct super_block *sb)
30323 +{
30324 +       struct au_sbinfo *sbinfo;
30325 +
30326 +       sbinfo = au_sbi(sb);
30327 +       if (sbinfo) {
30328 +               au_sbilist_del(sb);
30329 +               aufs_write_lock(sb->s_root);
30330 +               au_fhsm_fin(sb);
30331 +               if (sbinfo->si_wbr_create_ops->fin)
30332 +                       sbinfo->si_wbr_create_ops->fin(sb);
30333 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30334 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30335 +                       au_remount_refresh(sb, /*do_idop*/0);
30336 +               }
30337 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30338 +                       au_plink_put(sb, /*verbose*/1);
30339 +               au_xino_clr(sb);
30340 +               au_dr_opt_flush(sb);
30341 +               sbinfo->si_sb = NULL;
30342 +               aufs_write_unlock(sb->s_root);
30343 +               au_nwt_flush(&sbinfo->si_nowait);
30344 +       }
30345 +       kill_anon_super(sb);
30346 +}
30347 +
30348 +struct file_system_type aufs_fs_type = {
30349 +       .name           = AUFS_FSTYPE,
30350 +       /* a race between rename and others */
30351 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30352 +       .mount          = aufs_mount,
30353 +       .kill_sb        = aufs_kill_sb,
30354 +       /* no need to __module_get() and module_put(). */
30355 +       .owner          = THIS_MODULE,
30356 +};
30357 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30358 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30359 +++ linux/fs/aufs/super.h       2021-02-24 13:33:42.747680497 +0100
30360 @@ -0,0 +1,587 @@
30361 +/* SPDX-License-Identifier: GPL-2.0 */
30362 +/*
30363 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30364 + *
30365 + * This program, aufs is free software; you can redistribute it and/or modify
30366 + * it under the terms of the GNU General Public License as published by
30367 + * the Free Software Foundation; either version 2 of the License, or
30368 + * (at your option) any later version.
30369 + *
30370 + * This program is distributed in the hope that it will be useful,
30371 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30372 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30373 + * GNU General Public License for more details.
30374 + *
30375 + * You should have received a copy of the GNU General Public License
30376 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30377 + */
30378 +
30379 +/*
30380 + * super_block operations
30381 + */
30382 +
30383 +#ifndef __AUFS_SUPER_H__
30384 +#define __AUFS_SUPER_H__
30385 +
30386 +#ifdef __KERNEL__
30387 +
30388 +#include <linux/fs.h>
30389 +#include <linux/kobject.h>
30390 +#include "hbl.h"
30391 +#include "lcnt.h"
30392 +#include "rwsem.h"
30393 +#include "wkq.h"
30394 +
30395 +/* policies to select one among multiple writable branches */
30396 +struct au_wbr_copyup_operations {
30397 +       int (*copyup)(struct dentry *dentry);
30398 +};
30399 +
30400 +#define AuWbr_DIR      1               /* target is a dir */
30401 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30402 +
30403 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30404 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30405 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30406 +
30407 +struct au_wbr_create_operations {
30408 +       int (*create)(struct dentry *dentry, unsigned int flags);
30409 +       int (*init)(struct super_block *sb);
30410 +       int (*fin)(struct super_block *sb);
30411 +};
30412 +
30413 +struct au_wbr_mfs {
30414 +       struct mutex    mfs_lock; /* protect this structure */
30415 +       unsigned long   mfs_jiffy;
30416 +       unsigned long   mfs_expire;
30417 +       aufs_bindex_t   mfs_bindex;
30418 +
30419 +       unsigned long long      mfsrr_bytes;
30420 +       unsigned long long      mfsrr_watermark;
30421 +};
30422 +
30423 +#define AuPlink_NHASH 100
30424 +static inline int au_plink_hash(ino_t ino)
30425 +{
30426 +       return ino % AuPlink_NHASH;
30427 +}
30428 +
30429 +/* File-based Hierarchical Storage Management */
30430 +struct au_fhsm {
30431 +#ifdef CONFIG_AUFS_FHSM
30432 +       /* allow only one process who can receive the notification */
30433 +       spinlock_t              fhsm_spin;
30434 +       pid_t                   fhsm_pid;
30435 +       wait_queue_head_t       fhsm_wqh;
30436 +       atomic_t                fhsm_readable;
30437 +
30438 +       /* these are protected by si_rwsem */
30439 +       unsigned long           fhsm_expire;
30440 +       aufs_bindex_t           fhsm_bottom;
30441 +#endif
30442 +};
30443 +
30444 +struct au_branch;
30445 +struct au_sbinfo {
30446 +       /* nowait tasks in the system-wide workqueue */
30447 +       struct au_nowait_tasks  si_nowait;
30448 +
30449 +       /*
30450 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30451 +        * rwsem for au_sbinfo is necessary.
30452 +        */
30453 +       struct au_rwsem         si_rwsem;
30454 +
30455 +       /*
30456 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30457 +        * remount.
30458 +        */
30459 +       au_lcnt_t               si_ninodes, si_nfiles;
30460 +
30461 +       /* branch management */
30462 +       unsigned int            si_generation;
30463 +
30464 +       /* see AuSi_ flags */
30465 +       unsigned char           au_si_status;
30466 +
30467 +       aufs_bindex_t           si_bbot;
30468 +
30469 +       /* dirty trick to keep br_id plus */
30470 +       unsigned int            si_last_br_id :
30471 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30472 +       struct au_branch        **si_branch;
30473 +
30474 +       /* policy to select a writable branch */
30475 +       unsigned char           si_wbr_copyup;
30476 +       unsigned char           si_wbr_create;
30477 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30478 +       struct au_wbr_create_operations *si_wbr_create_ops;
30479 +
30480 +       /* round robin */
30481 +       atomic_t                si_wbr_rr_next;
30482 +
30483 +       /* most free space */
30484 +       struct au_wbr_mfs       si_wbr_mfs;
30485 +
30486 +       /* File-based Hierarchical Storage Management */
30487 +       struct au_fhsm          si_fhsm;
30488 +
30489 +       /* mount flags */
30490 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30491 +       unsigned int            si_mntflags;
30492 +
30493 +       /* external inode number (bitmap and translation table) */
30494 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30495 +
30496 +       struct file             *si_xib;
30497 +       struct mutex            si_xib_mtx; /* protect xib members */
30498 +       unsigned long           *si_xib_buf;
30499 +       unsigned long           si_xib_last_pindex;
30500 +       int                     si_xib_next_bit;
30501 +
30502 +       unsigned long           si_xino_jiffy;
30503 +       unsigned long           si_xino_expire;
30504 +       /* reserved for future use */
30505 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30506 +
30507 +#ifdef CONFIG_AUFS_EXPORT
30508 +       /* i_generation */
30509 +       /* todo: make xigen file an array to support many inode numbers */
30510 +       struct file             *si_xigen;
30511 +       atomic_t                si_xigen_next;
30512 +#endif
30513 +
30514 +       /* dirty trick to support atomic_open */
30515 +       struct hlist_bl_head    si_aopen;
30516 +
30517 +       /* vdir parameters */
30518 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30519 +       unsigned int            si_rdblk;       /* deblk size */
30520 +       unsigned int            si_rdhash;      /* hash size */
30521 +
30522 +       /*
30523 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30524 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30525 +        * future fsck.aufs or kernel thread will remove them later.
30526 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30527 +        */
30528 +       unsigned int            si_dirwh;
30529 +
30530 +       /* pseudo_link list */
30531 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30532 +       wait_queue_head_t       si_plink_wq;
30533 +       spinlock_t              si_plink_maint_lock;
30534 +       pid_t                   si_plink_maint_pid;
30535 +
30536 +       /* file list */
30537 +       struct hlist_bl_head    si_files;
30538 +
30539 +       /* with/without getattr, brother of sb->s_d_op */
30540 +       const struct inode_operations *si_iop_array;
30541 +
30542 +       /*
30543 +        * sysfs and lifetime management.
30544 +        * this is not a small structure and it may be a waste of memory in case
30545 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30546 +        * but using sysfs is majority.
30547 +        */
30548 +       struct kobject          si_kobj;
30549 +#ifdef CONFIG_DEBUG_FS
30550 +       struct dentry            *si_dbgaufs;
30551 +       struct dentry            *si_dbgaufs_plink;
30552 +       struct dentry            *si_dbgaufs_xib;
30553 +#ifdef CONFIG_AUFS_EXPORT
30554 +       struct dentry            *si_dbgaufs_xigen;
30555 +#endif
30556 +#endif
30557 +
30558 +#ifdef CONFIG_AUFS_SBILIST
30559 +       struct hlist_bl_node    si_list;
30560 +#endif
30561 +
30562 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30563 +       struct super_block      *si_sb;
30564 +};
30565 +
30566 +/* sbinfo status flags */
30567 +/*
30568 + * set true when refresh_dirs() failed at remount time.
30569 + * then try refreshing dirs at access time again.
30570 + * if it is false, refreshing dirs at access time is unnecessary
30571 + */
30572 +#define AuSi_FAILED_REFRESH_DIR        1
30573 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30574 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30575 +
30576 +#ifndef CONFIG_AUFS_FHSM
30577 +#undef AuSi_FHSM
30578 +#define AuSi_FHSM              0
30579 +#endif
30580 +
30581 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30582 +                                          unsigned int flag)
30583 +{
30584 +       AuRwMustAnyLock(&sbi->si_rwsem);
30585 +       return sbi->au_si_status & flag;
30586 +}
30587 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30588 +#define au_fset_si(sbinfo, name) do { \
30589 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30590 +       (sbinfo)->au_si_status |= AuSi_##name; \
30591 +} while (0)
30592 +#define au_fclr_si(sbinfo, name) do { \
30593 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30594 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30595 +} while (0)
30596 +
30597 +/* ---------------------------------------------------------------------- */
30598 +
30599 +/* policy to select one among writable branches */
30600 +#define AuWbrCopyup(sbinfo, ...) \
30601 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30602 +#define AuWbrCreate(sbinfo, ...) \
30603 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30604 +
30605 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30606 +#define AuLock_DW              1               /* write-lock dentry */
30607 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30608 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30609 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30610 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30611 +                                               /* except RENAME_EXCHANGE */
30612 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30613 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30614 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30615 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30616 +#define au_fset_lock(flags, name) \
30617 +       do { (flags) |= AuLock_##name; } while (0)
30618 +#define au_fclr_lock(flags, name) \
30619 +       do { (flags) &= ~AuLock_##name; } while (0)
30620 +
30621 +/* ---------------------------------------------------------------------- */
30622 +
30623 +/* super.c */
30624 +extern struct file_system_type aufs_fs_type;
30625 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30626 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30627 +                                          unsigned long long max, void *arg);
30628 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30629 +                    struct super_block *sb, void *arg);
30630 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30631 +void au_iarray_free(struct inode **a, unsigned long long max);
30632 +
30633 +/* sbinfo.c */
30634 +void au_si_free(struct kobject *kobj);
30635 +int au_si_alloc(struct super_block *sb);
30636 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30637 +
30638 +unsigned int au_sigen_inc(struct super_block *sb);
30639 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30640 +
30641 +int si_read_lock(struct super_block *sb, int flags);
30642 +int si_write_lock(struct super_block *sb, int flags);
30643 +int aufs_read_lock(struct dentry *dentry, int flags);
30644 +void aufs_read_unlock(struct dentry *dentry, int flags);
30645 +void aufs_write_lock(struct dentry *dentry);
30646 +void aufs_write_unlock(struct dentry *dentry);
30647 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30648 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30649 +
30650 +/* wbr_policy.c */
30651 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30652 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30653 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30654 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30655 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30656 +
30657 +/* mvdown.c */
30658 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30659 +
30660 +#ifdef CONFIG_AUFS_FHSM
30661 +/* fhsm.c */
30662 +
30663 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30664 +{
30665 +       pid_t pid;
30666 +
30667 +       spin_lock(&fhsm->fhsm_spin);
30668 +       pid = fhsm->fhsm_pid;
30669 +       spin_unlock(&fhsm->fhsm_spin);
30670 +
30671 +       return pid;
30672 +}
30673 +
30674 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30675 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30676 +int au_fhsm_fd(struct super_block *sb, int oflags);
30677 +int au_fhsm_br_alloc(struct au_branch *br);
30678 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30679 +void au_fhsm_fin(struct super_block *sb);
30680 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30681 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30682 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30683 +#else
30684 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30685 +          int force)
30686 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30687 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30688 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30689 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30690 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30691 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30692 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30693 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30694 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30695 +#endif
30696 +
30697 +/* ---------------------------------------------------------------------- */
30698 +
30699 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30700 +{
30701 +       return sb->s_fs_info;
30702 +}
30703 +
30704 +/* ---------------------------------------------------------------------- */
30705 +
30706 +#ifdef CONFIG_AUFS_EXPORT
30707 +int au_test_nfsd(void);
30708 +void au_export_init(struct super_block *sb);
30709 +void au_xigen_inc(struct inode *inode);
30710 +int au_xigen_new(struct inode *inode);
30711 +int au_xigen_set(struct super_block *sb, struct path *path);
30712 +void au_xigen_clr(struct super_block *sb);
30713 +
30714 +static inline int au_busy_or_stale(void)
30715 +{
30716 +       if (!au_test_nfsd())
30717 +               return -EBUSY;
30718 +       return -ESTALE;
30719 +}
30720 +#else
30721 +AuStubInt0(au_test_nfsd, void)
30722 +AuStubVoid(au_export_init, struct super_block *sb)
30723 +AuStubVoid(au_xigen_inc, struct inode *inode)
30724 +AuStubInt0(au_xigen_new, struct inode *inode)
30725 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30726 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30727 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30728 +#endif /* CONFIG_AUFS_EXPORT */
30729 +
30730 +/* ---------------------------------------------------------------------- */
30731 +
30732 +#ifdef CONFIG_AUFS_SBILIST
30733 +/* module.c */
30734 +extern struct hlist_bl_head au_sbilist;
30735 +
30736 +static inline void au_sbilist_init(void)
30737 +{
30738 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30739 +}
30740 +
30741 +static inline void au_sbilist_add(struct super_block *sb)
30742 +{
30743 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30744 +}
30745 +
30746 +static inline void au_sbilist_del(struct super_block *sb)
30747 +{
30748 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30749 +}
30750 +
30751 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30752 +static inline void au_sbilist_lock(void)
30753 +{
30754 +       hlist_bl_lock(&au_sbilist);
30755 +}
30756 +
30757 +static inline void au_sbilist_unlock(void)
30758 +{
30759 +       hlist_bl_unlock(&au_sbilist);
30760 +}
30761 +#define AuGFP_SBILIST  GFP_ATOMIC
30762 +#else
30763 +AuStubVoid(au_sbilist_lock, void)
30764 +AuStubVoid(au_sbilist_unlock, void)
30765 +#define AuGFP_SBILIST  GFP_NOFS
30766 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30767 +#else
30768 +AuStubVoid(au_sbilist_init, void)
30769 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30770 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30771 +AuStubVoid(au_sbilist_lock, void)
30772 +AuStubVoid(au_sbilist_unlock, void)
30773 +#define AuGFP_SBILIST  GFP_NOFS
30774 +#endif
30775 +
30776 +/* ---------------------------------------------------------------------- */
30777 +
30778 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30779 +{
30780 +       /*
30781 +        * This function is a dynamic '__init' function actually,
30782 +        * so the tiny check for si_rwsem is unnecessary.
30783 +        */
30784 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30785 +#ifdef CONFIG_DEBUG_FS
30786 +       sbinfo->si_dbgaufs = NULL;
30787 +       sbinfo->si_dbgaufs_plink = NULL;
30788 +       sbinfo->si_dbgaufs_xib = NULL;
30789 +#ifdef CONFIG_AUFS_EXPORT
30790 +       sbinfo->si_dbgaufs_xigen = NULL;
30791 +#endif
30792 +#endif
30793 +}
30794 +
30795 +/* ---------------------------------------------------------------------- */
30796 +
30797 +/* current->atomic_flags */
30798 +/* this value should never corrupt the ones defined in linux/sched.h */
30799 +#define PFA_AUFS       0x10
30800 +
30801 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30802 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30803 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30804 +
30805 +static inline int si_pid_test(struct super_block *sb)
30806 +{
30807 +       return !!task_test_aufs(current);
30808 +}
30809 +
30810 +static inline void si_pid_clr(struct super_block *sb)
30811 +{
30812 +       AuDebugOn(!task_test_aufs(current));
30813 +       task_clear_aufs(current);
30814 +}
30815 +
30816 +static inline void si_pid_set(struct super_block *sb)
30817 +{
30818 +       AuDebugOn(task_test_aufs(current));
30819 +       task_set_aufs(current);
30820 +}
30821 +
30822 +/* ---------------------------------------------------------------------- */
30823 +
30824 +/* lock superblock. mainly for entry point functions */
30825 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30826 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30827 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30828 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30829 +/*
30830 +#define __si_read_trylock_nested(sb) \
30831 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30832 +#define __si_write_trylock_nested(sb) \
30833 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30834 +*/
30835 +
30836 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30837 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30838 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30839 +
30840 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30841 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30842 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30843 +
30844 +static inline void si_noflush_read_lock(struct super_block *sb)
30845 +{
30846 +       __si_read_lock(sb);
30847 +       si_pid_set(sb);
30848 +}
30849 +
30850 +static inline int si_noflush_read_trylock(struct super_block *sb)
30851 +{
30852 +       int locked;
30853 +
30854 +       locked = __si_read_trylock(sb);
30855 +       if (locked)
30856 +               si_pid_set(sb);
30857 +       return locked;
30858 +}
30859 +
30860 +static inline void si_noflush_write_lock(struct super_block *sb)
30861 +{
30862 +       __si_write_lock(sb);
30863 +       si_pid_set(sb);
30864 +}
30865 +
30866 +static inline int si_noflush_write_trylock(struct super_block *sb)
30867 +{
30868 +       int locked;
30869 +
30870 +       locked = __si_write_trylock(sb);
30871 +       if (locked)
30872 +               si_pid_set(sb);
30873 +       return locked;
30874 +}
30875 +
30876 +#if 0 /* reserved */
30877 +static inline int si_read_trylock(struct super_block *sb, int flags)
30878 +{
30879 +       if (au_ftest_lock(flags, FLUSH))
30880 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30881 +       return si_noflush_read_trylock(sb);
30882 +}
30883 +#endif
30884 +
30885 +static inline void si_read_unlock(struct super_block *sb)
30886 +{
30887 +       si_pid_clr(sb);
30888 +       __si_read_unlock(sb);
30889 +}
30890 +
30891 +#if 0 /* reserved */
30892 +static inline int si_write_trylock(struct super_block *sb, int flags)
30893 +{
30894 +       if (au_ftest_lock(flags, FLUSH))
30895 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30896 +       return si_noflush_write_trylock(sb);
30897 +}
30898 +#endif
30899 +
30900 +static inline void si_write_unlock(struct super_block *sb)
30901 +{
30902 +       si_pid_clr(sb);
30903 +       __si_write_unlock(sb);
30904 +}
30905 +
30906 +#if 0 /* reserved */
30907 +static inline void si_downgrade_lock(struct super_block *sb)
30908 +{
30909 +       __si_downgrade_lock(sb);
30910 +}
30911 +#endif
30912 +
30913 +/* ---------------------------------------------------------------------- */
30914 +
30915 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30916 +{
30917 +       SiMustAnyLock(sb);
30918 +       return au_sbi(sb)->si_bbot;
30919 +}
30920 +
30921 +static inline unsigned int au_mntflags(struct super_block *sb)
30922 +{
30923 +       SiMustAnyLock(sb);
30924 +       return au_sbi(sb)->si_mntflags;
30925 +}
30926 +
30927 +static inline unsigned int au_sigen(struct super_block *sb)
30928 +{
30929 +       SiMustAnyLock(sb);
30930 +       return au_sbi(sb)->si_generation;
30931 +}
30932 +
30933 +static inline struct au_branch *au_sbr(struct super_block *sb,
30934 +                                      aufs_bindex_t bindex)
30935 +{
30936 +       SiMustAnyLock(sb);
30937 +       return au_sbi(sb)->si_branch[0 + bindex];
30938 +}
30939 +
30940 +static inline loff_t au_xi_maxent(struct super_block *sb)
30941 +{
30942 +       SiMustAnyLock(sb);
30943 +       return au_sbi(sb)->si_ximaxent;
30944 +}
30945 +
30946 +#endif /* __KERNEL__ */
30947 +#endif /* __AUFS_SUPER_H__ */
30948 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30949 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
30950 +++ linux/fs/aufs/sysaufs.c     2021-02-24 13:33:42.747680497 +0100
30951 @@ -0,0 +1,93 @@
30952 +// SPDX-License-Identifier: GPL-2.0
30953 +/*
30954 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30955 + *
30956 + * This program, aufs is free software; you can redistribute it and/or modify
30957 + * it under the terms of the GNU General Public License as published by
30958 + * the Free Software Foundation; either version 2 of the License, or
30959 + * (at your option) any later version.
30960 + *
30961 + * This program is distributed in the hope that it will be useful,
30962 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30963 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30964 + * GNU General Public License for more details.
30965 + *
30966 + * You should have received a copy of the GNU General Public License
30967 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30968 + */
30969 +
30970 +/*
30971 + * sysfs interface and lifetime management
30972 + * they are necessary regardless sysfs is disabled.
30973 + */
30974 +
30975 +#include <linux/random.h>
30976 +#include "aufs.h"
30977 +
30978 +unsigned long sysaufs_si_mask;
30979 +struct kset *sysaufs_kset;
30980 +
30981 +#define AuSiAttr(_name) { \
30982 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
30983 +       .show   = sysaufs_si_##_name,                           \
30984 +}
30985 +
30986 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30987 +struct attribute *sysaufs_si_attrs[] = {
30988 +       &sysaufs_si_attr_xi_path.attr,
30989 +       NULL,
30990 +};
30991 +
30992 +static const struct sysfs_ops au_sbi_ops = {
30993 +       .show   = sysaufs_si_show
30994 +};
30995 +
30996 +static struct kobj_type au_sbi_ktype = {
30997 +       .release        = au_si_free,
30998 +       .sysfs_ops      = &au_sbi_ops,
30999 +       .default_attrs  = sysaufs_si_attrs
31000 +};
31001 +
31002 +/* ---------------------------------------------------------------------- */
31003 +
31004 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31005 +{
31006 +       int err;
31007 +
31008 +       sbinfo->si_kobj.kset = sysaufs_kset;
31009 +       /* cf. sysaufs_name() */
31010 +       err = kobject_init_and_add
31011 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31012 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31013 +
31014 +       return err;
31015 +}
31016 +
31017 +void sysaufs_fin(void)
31018 +{
31019 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31020 +       kset_unregister(sysaufs_kset);
31021 +}
31022 +
31023 +int __init sysaufs_init(void)
31024 +{
31025 +       int err;
31026 +
31027 +       do {
31028 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31029 +       } while (!sysaufs_si_mask);
31030 +
31031 +       err = -EINVAL;
31032 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31033 +       if (unlikely(!sysaufs_kset))
31034 +               goto out;
31035 +       err = PTR_ERR(sysaufs_kset);
31036 +       if (IS_ERR(sysaufs_kset))
31037 +               goto out;
31038 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31039 +       if (unlikely(err))
31040 +               kset_unregister(sysaufs_kset);
31041 +
31042 +out:
31043 +       return err;
31044 +}
31045 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31046 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31047 +++ linux/fs/aufs/sysaufs.h     2021-02-24 13:33:42.747680497 +0100
31048 @@ -0,0 +1,102 @@
31049 +/* SPDX-License-Identifier: GPL-2.0 */
31050 +/*
31051 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31052 + *
31053 + * This program, aufs is free software; you can redistribute it and/or modify
31054 + * it under the terms of the GNU General Public License as published by
31055 + * the Free Software Foundation; either version 2 of the License, or
31056 + * (at your option) any later version.
31057 + *
31058 + * This program is distributed in the hope that it will be useful,
31059 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31060 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31061 + * GNU General Public License for more details.
31062 + *
31063 + * You should have received a copy of the GNU General Public License
31064 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31065 + */
31066 +
31067 +/*
31068 + * sysfs interface and mount lifetime management
31069 + */
31070 +
31071 +#ifndef __SYSAUFS_H__
31072 +#define __SYSAUFS_H__
31073 +
31074 +#ifdef __KERNEL__
31075 +
31076 +#include <linux/sysfs.h>
31077 +#include "module.h"
31078 +
31079 +struct super_block;
31080 +struct au_sbinfo;
31081 +
31082 +struct sysaufs_si_attr {
31083 +       struct attribute attr;
31084 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31085 +};
31086 +
31087 +/* ---------------------------------------------------------------------- */
31088 +
31089 +/* sysaufs.c */
31090 +extern unsigned long sysaufs_si_mask;
31091 +extern struct kset *sysaufs_kset;
31092 +extern struct attribute *sysaufs_si_attrs[];
31093 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31094 +int __init sysaufs_init(void);
31095 +void sysaufs_fin(void);
31096 +
31097 +/* ---------------------------------------------------------------------- */
31098 +
31099 +/* some people doesn't like to show a pointer in kernel */
31100 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31101 +{
31102 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31103 +}
31104 +
31105 +#define SysaufsSiNamePrefix    "si_"
31106 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31107 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31108 +{
31109 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31110 +                sysaufs_si_id(sbinfo));
31111 +}
31112 +
31113 +struct au_branch;
31114 +#ifdef CONFIG_SYSFS
31115 +/* sysfs.c */
31116 +extern struct attribute_group *sysaufs_attr_group;
31117 +
31118 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31119 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31120 +                        char *buf);
31121 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31122 +#ifdef CONFIG_COMPAT
31123 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31124 +#endif
31125 +
31126 +void sysaufs_br_init(struct au_branch *br);
31127 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31128 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31129 +
31130 +#define sysaufs_brs_init()     do {} while (0)
31131 +
31132 +#else
31133 +#define sysaufs_attr_group     NULL
31134 +
31135 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31136 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31137 +       struct attribute *attr, char *buf)
31138 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31139 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31140 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31141 +
31142 +static inline void sysaufs_brs_init(void)
31143 +{
31144 +       sysaufs_brs = 0;
31145 +}
31146 +
31147 +#endif /* CONFIG_SYSFS */
31148 +
31149 +#endif /* __KERNEL__ */
31150 +#endif /* __SYSAUFS_H__ */
31151 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31152 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31153 +++ linux/fs/aufs/sysfs.c       2021-02-24 13:33:42.747680497 +0100
31154 @@ -0,0 +1,374 @@
31155 +// SPDX-License-Identifier: GPL-2.0
31156 +/*
31157 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31158 + *
31159 + * This program, aufs is free software; you can redistribute it and/or modify
31160 + * it under the terms of the GNU General Public License as published by
31161 + * the Free Software Foundation; either version 2 of the License, or
31162 + * (at your option) any later version.
31163 + *
31164 + * This program is distributed in the hope that it will be useful,
31165 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31166 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31167 + * GNU General Public License for more details.
31168 + *
31169 + * You should have received a copy of the GNU General Public License
31170 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31171 + */
31172 +
31173 +/*
31174 + * sysfs interface
31175 + */
31176 +
31177 +#include <linux/compat.h>
31178 +#include <linux/seq_file.h>
31179 +#include "aufs.h"
31180 +
31181 +#ifdef CONFIG_AUFS_FS_MODULE
31182 +/* this entry violates the "one line per file" policy of sysfs */
31183 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31184 +                          char *buf)
31185 +{
31186 +       ssize_t err;
31187 +       static char *conf =
31188 +/* this file is generated at compiling */
31189 +#include "conf.str"
31190 +               ;
31191 +
31192 +       err = snprintf(buf, PAGE_SIZE, conf);
31193 +       if (unlikely(err >= PAGE_SIZE))
31194 +               err = -EFBIG;
31195 +       return err;
31196 +}
31197 +
31198 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31199 +#endif
31200 +
31201 +static struct attribute *au_attr[] = {
31202 +#ifdef CONFIG_AUFS_FS_MODULE
31203 +       &au_config_attr.attr,
31204 +#endif
31205 +       NULL,   /* need to NULL terminate the list of attributes */
31206 +};
31207 +
31208 +static struct attribute_group sysaufs_attr_group_body = {
31209 +       .attrs = au_attr
31210 +};
31211 +
31212 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31213 +
31214 +/* ---------------------------------------------------------------------- */
31215 +
31216 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31217 +{
31218 +       int err;
31219 +
31220 +       SiMustAnyLock(sb);
31221 +
31222 +       err = 0;
31223 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31224 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31225 +               seq_putc(seq, '\n');
31226 +       }
31227 +       return err;
31228 +}
31229 +
31230 +/*
31231 + * the lifetime of branch is independent from the entry under sysfs.
31232 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31233 + * unlinked.
31234 + */
31235 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31236 +                        aufs_bindex_t bindex, int idx)
31237 +{
31238 +       int err;
31239 +       struct path path;
31240 +       struct dentry *root;
31241 +       struct au_branch *br;
31242 +       au_br_perm_str_t perm;
31243 +
31244 +       AuDbg("b%d\n", bindex);
31245 +
31246 +       err = 0;
31247 +       root = sb->s_root;
31248 +       di_read_lock_parent(root, !AuLock_IR);
31249 +       br = au_sbr(sb, bindex);
31250 +
31251 +       switch (idx) {
31252 +       case AuBrSysfs_BR:
31253 +               path.mnt = au_br_mnt(br);
31254 +               path.dentry = au_h_dptr(root, bindex);
31255 +               err = au_seq_path(seq, &path);
31256 +               if (!err) {
31257 +                       au_optstr_br_perm(&perm, br->br_perm);
31258 +                       seq_printf(seq, "=%s\n", perm.a);
31259 +               }
31260 +               break;
31261 +       case AuBrSysfs_BRID:
31262 +               seq_printf(seq, "%d\n", br->br_id);
31263 +               break;
31264 +       }
31265 +       di_read_unlock(root, !AuLock_IR);
31266 +       if (unlikely(err || seq_has_overflowed(seq)))
31267 +               err = -E2BIG;
31268 +
31269 +       return err;
31270 +}
31271 +
31272 +/* ---------------------------------------------------------------------- */
31273 +
31274 +static struct seq_file *au_seq(char *p, ssize_t len)
31275 +{
31276 +       struct seq_file *seq;
31277 +
31278 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31279 +       if (seq) {
31280 +               /* mutex_init(&seq.lock); */
31281 +               seq->buf = p;
31282 +               seq->size = len;
31283 +               return seq; /* success */
31284 +       }
31285 +
31286 +       seq = ERR_PTR(-ENOMEM);
31287 +       return seq;
31288 +}
31289 +
31290 +#define SysaufsBr_PREFIX       "br"
31291 +#define SysaufsBrid_PREFIX     "brid"
31292 +
31293 +/* todo: file size may exceed PAGE_SIZE */
31294 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31295 +                       char *buf)
31296 +{
31297 +       ssize_t err;
31298 +       int idx;
31299 +       long l;
31300 +       aufs_bindex_t bbot;
31301 +       struct au_sbinfo *sbinfo;
31302 +       struct super_block *sb;
31303 +       struct seq_file *seq;
31304 +       char *name;
31305 +       struct attribute **cattr;
31306 +
31307 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31308 +       sb = sbinfo->si_sb;
31309 +
31310 +       /*
31311 +        * prevent a race condition between sysfs and aufs.
31312 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31313 +        * prohibits maintaining the sysfs entries.
31314 +        * hew we acquire read lock after sysfs_get_active_two().
31315 +        * on the other hand, the remount process may maintain the sysfs/aufs
31316 +        * entries after acquiring write lock.
31317 +        * it can cause a deadlock.
31318 +        * simply we gave up processing read here.
31319 +        */
31320 +       err = -EBUSY;
31321 +       if (unlikely(!si_noflush_read_trylock(sb)))
31322 +               goto out;
31323 +
31324 +       seq = au_seq(buf, PAGE_SIZE);
31325 +       err = PTR_ERR(seq);
31326 +       if (IS_ERR(seq))
31327 +               goto out_unlock;
31328 +
31329 +       name = (void *)attr->name;
31330 +       cattr = sysaufs_si_attrs;
31331 +       while (*cattr) {
31332 +               if (!strcmp(name, (*cattr)->name)) {
31333 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31334 +                               ->show(seq, sb);
31335 +                       goto out_seq;
31336 +               }
31337 +               cattr++;
31338 +       }
31339 +
31340 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31341 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31342 +               idx = AuBrSysfs_BRID;
31343 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31344 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31345 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31346 +               idx = AuBrSysfs_BR;
31347 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31348 +       } else
31349 +                 BUG();
31350 +
31351 +       err = kstrtol(name, 10, &l);
31352 +       if (!err) {
31353 +               bbot = au_sbbot(sb);
31354 +               if (l <= bbot)
31355 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31356 +               else
31357 +                       err = -ENOENT;
31358 +       }
31359 +
31360 +out_seq:
31361 +       if (!err) {
31362 +               err = seq->count;
31363 +               /* sysfs limit */
31364 +               if (unlikely(err == PAGE_SIZE))
31365 +                       err = -EFBIG;
31366 +       }
31367 +       au_kfree_rcu(seq);
31368 +out_unlock:
31369 +       si_read_unlock(sb);
31370 +out:
31371 +       return err;
31372 +}
31373 +
31374 +/* ---------------------------------------------------------------------- */
31375 +
31376 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31377 +{
31378 +       int err;
31379 +       int16_t brid;
31380 +       aufs_bindex_t bindex, bbot;
31381 +       size_t sz;
31382 +       char *buf;
31383 +       struct seq_file *seq;
31384 +       struct au_branch *br;
31385 +
31386 +       si_read_lock(sb, AuLock_FLUSH);
31387 +       bbot = au_sbbot(sb);
31388 +       err = bbot + 1;
31389 +       if (!arg)
31390 +               goto out;
31391 +
31392 +       err = -ENOMEM;
31393 +       buf = (void *)__get_free_page(GFP_NOFS);
31394 +       if (unlikely(!buf))
31395 +               goto out;
31396 +
31397 +       seq = au_seq(buf, PAGE_SIZE);
31398 +       err = PTR_ERR(seq);
31399 +       if (IS_ERR(seq))
31400 +               goto out_buf;
31401 +
31402 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31403 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31404 +               /* VERIFY_WRITE */
31405 +               err = !access_ok(arg, sizeof(*arg));
31406 +               if (unlikely(err))
31407 +                       break;
31408 +
31409 +               br = au_sbr(sb, bindex);
31410 +               brid = br->br_id;
31411 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31412 +               err = __put_user(brid, &arg->id);
31413 +               if (unlikely(err))
31414 +                       break;
31415 +
31416 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31417 +               err = __put_user(br->br_perm, &arg->perm);
31418 +               if (unlikely(err))
31419 +                       break;
31420 +
31421 +               err = au_seq_path(seq, &br->br_path);
31422 +               if (unlikely(err))
31423 +                       break;
31424 +               seq_putc(seq, '\0');
31425 +               if (!seq_has_overflowed(seq)) {
31426 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31427 +                       seq->count = 0;
31428 +                       if (unlikely(err))
31429 +                               break;
31430 +               } else {
31431 +                       err = -E2BIG;
31432 +                       goto out_seq;
31433 +               }
31434 +       }
31435 +       if (unlikely(err))
31436 +               err = -EFAULT;
31437 +
31438 +out_seq:
31439 +       au_kfree_rcu(seq);
31440 +out_buf:
31441 +       free_page((unsigned long)buf);
31442 +out:
31443 +       si_read_unlock(sb);
31444 +       return err;
31445 +}
31446 +
31447 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31448 +{
31449 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31450 +}
31451 +
31452 +#ifdef CONFIG_COMPAT
31453 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31454 +{
31455 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31456 +}
31457 +#endif
31458 +
31459 +/* ---------------------------------------------------------------------- */
31460 +
31461 +void sysaufs_br_init(struct au_branch *br)
31462 +{
31463 +       int i;
31464 +       struct au_brsysfs *br_sysfs;
31465 +       struct attribute *attr;
31466 +
31467 +       br_sysfs = br->br_sysfs;
31468 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31469 +               attr = &br_sysfs->attr;
31470 +               sysfs_attr_init(attr);
31471 +               attr->name = br_sysfs->name;
31472 +               attr->mode = 0444;
31473 +               br_sysfs++;
31474 +       }
31475 +}
31476 +
31477 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31478 +{
31479 +       struct au_branch *br;
31480 +       struct kobject *kobj;
31481 +       struct au_brsysfs *br_sysfs;
31482 +       int i;
31483 +       aufs_bindex_t bbot;
31484 +
31485 +       if (!sysaufs_brs)
31486 +               return;
31487 +
31488 +       kobj = &au_sbi(sb)->si_kobj;
31489 +       bbot = au_sbbot(sb);
31490 +       for (; bindex <= bbot; bindex++) {
31491 +               br = au_sbr(sb, bindex);
31492 +               br_sysfs = br->br_sysfs;
31493 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31494 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31495 +                       br_sysfs++;
31496 +               }
31497 +       }
31498 +}
31499 +
31500 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31501 +{
31502 +       int err, i;
31503 +       aufs_bindex_t bbot;
31504 +       struct kobject *kobj;
31505 +       struct au_branch *br;
31506 +       struct au_brsysfs *br_sysfs;
31507 +
31508 +       if (!sysaufs_brs)
31509 +               return;
31510 +
31511 +       kobj = &au_sbi(sb)->si_kobj;
31512 +       bbot = au_sbbot(sb);
31513 +       for (; bindex <= bbot; bindex++) {
31514 +               br = au_sbr(sb, bindex);
31515 +               br_sysfs = br->br_sysfs;
31516 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31517 +                        SysaufsBr_PREFIX "%d", bindex);
31518 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31519 +                        SysaufsBrid_PREFIX "%d", bindex);
31520 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31521 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31522 +                       if (unlikely(err))
31523 +                               pr_warn("failed %s under sysfs(%d)\n",
31524 +                                       br_sysfs->name, err);
31525 +                       br_sysfs++;
31526 +               }
31527 +       }
31528 +}
31529 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31530 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31531 +++ linux/fs/aufs/sysrq.c       2021-02-24 13:33:42.747680497 +0100
31532 @@ -0,0 +1,149 @@
31533 +// SPDX-License-Identifier: GPL-2.0
31534 +/*
31535 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31536 + *
31537 + * This program, aufs is free software; you can redistribute it and/or modify
31538 + * it under the terms of the GNU General Public License as published by
31539 + * the Free Software Foundation; either version 2 of the License, or
31540 + * (at your option) any later version.
31541 + *
31542 + * This program is distributed in the hope that it will be useful,
31543 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31544 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31545 + * GNU General Public License for more details.
31546 + *
31547 + * You should have received a copy of the GNU General Public License
31548 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31549 + */
31550 +
31551 +/*
31552 + * magic sysrq handler
31553 + */
31554 +
31555 +/* #include <linux/sysrq.h> */
31556 +#include <linux/writeback.h>
31557 +#include "aufs.h"
31558 +
31559 +/* ---------------------------------------------------------------------- */
31560 +
31561 +static void sysrq_sb(struct super_block *sb)
31562 +{
31563 +       char *plevel;
31564 +       struct au_sbinfo *sbinfo;
31565 +       struct file *file;
31566 +       struct hlist_bl_head *files;
31567 +       struct hlist_bl_node *pos;
31568 +       struct au_finfo *finfo;
31569 +       struct inode *i;
31570 +
31571 +       plevel = au_plevel;
31572 +       au_plevel = KERN_WARNING;
31573 +
31574 +       /* since we define pr_fmt, call printk directly */
31575 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31576 +
31577 +       sbinfo = au_sbi(sb);
31578 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31579 +       pr("superblock\n");
31580 +       au_dpri_sb(sb);
31581 +
31582 +#if 0 /* reserved */
31583 +       do {
31584 +               int err, i, j, ndentry;
31585 +               struct au_dcsub_pages dpages;
31586 +               struct au_dpage *dpage;
31587 +
31588 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31589 +               if (unlikely(err))
31590 +                       break;
31591 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31592 +               if (!err)
31593 +                       for (i = 0; i < dpages.ndpage; i++) {
31594 +                               dpage = dpages.dpages + i;
31595 +                               ndentry = dpage->ndentry;
31596 +                               for (j = 0; j < ndentry; j++)
31597 +                                       au_dpri_dentry(dpage->dentries[j]);
31598 +                       }
31599 +               au_dpages_free(&dpages);
31600 +       } while (0);
31601 +#endif
31602 +
31603 +       pr("isolated inode\n");
31604 +       spin_lock(&sb->s_inode_list_lock);
31605 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31606 +               spin_lock(&i->i_lock);
31607 +               if (hlist_empty(&i->i_dentry))
31608 +                       au_dpri_inode(i);
31609 +               spin_unlock(&i->i_lock);
31610 +       }
31611 +       spin_unlock(&sb->s_inode_list_lock);
31612 +
31613 +       pr("files\n");
31614 +       files = &au_sbi(sb)->si_files;
31615 +       hlist_bl_lock(files);
31616 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31617 +               umode_t mode;
31618 +
31619 +               file = finfo->fi_file;
31620 +               mode = file_inode(file)->i_mode;
31621 +               if (!special_file(mode))
31622 +                       au_dpri_file(file);
31623 +       }
31624 +       hlist_bl_unlock(files);
31625 +       pr("done\n");
31626 +
31627 +#undef pr
31628 +       au_plevel = plevel;
31629 +}
31630 +
31631 +/* ---------------------------------------------------------------------- */
31632 +
31633 +/* module parameter */
31634 +static char *aufs_sysrq_key = "a";
31635 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31636 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31637 +
31638 +static void au_sysrq(int key __maybe_unused)
31639 +{
31640 +       struct au_sbinfo *sbinfo;
31641 +       struct hlist_bl_node *pos;
31642 +
31643 +       lockdep_off();
31644 +       au_sbilist_lock();
31645 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31646 +               sysrq_sb(sbinfo->si_sb);
31647 +       au_sbilist_unlock();
31648 +       lockdep_on();
31649 +}
31650 +
31651 +static struct sysrq_key_op au_sysrq_op = {
31652 +       .handler        = au_sysrq,
31653 +       .help_msg       = "Aufs",
31654 +       .action_msg     = "Aufs",
31655 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31656 +};
31657 +
31658 +/* ---------------------------------------------------------------------- */
31659 +
31660 +int __init au_sysrq_init(void)
31661 +{
31662 +       int err;
31663 +       char key;
31664 +
31665 +       err = -1;
31666 +       key = *aufs_sysrq_key;
31667 +       if ('a' <= key && key <= 'z')
31668 +               err = register_sysrq_key(key, &au_sysrq_op);
31669 +       if (unlikely(err))
31670 +               pr_err("err %d, sysrq=%c\n", err, key);
31671 +       return err;
31672 +}
31673 +
31674 +void au_sysrq_fin(void)
31675 +{
31676 +       int err;
31677 +
31678 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31679 +       if (unlikely(err))
31680 +               pr_err("err %d (ignored)\n", err);
31681 +}
31682 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31683 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31684 +++ linux/fs/aufs/vdir.c        2021-02-24 13:33:42.747680497 +0100
31685 @@ -0,0 +1,896 @@
31686 +// SPDX-License-Identifier: GPL-2.0
31687 +/*
31688 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31689 + *
31690 + * This program, aufs is free software; you can redistribute it and/or modify
31691 + * it under the terms of the GNU General Public License as published by
31692 + * the Free Software Foundation; either version 2 of the License, or
31693 + * (at your option) any later version.
31694 + *
31695 + * This program is distributed in the hope that it will be useful,
31696 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31697 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31698 + * GNU General Public License for more details.
31699 + *
31700 + * You should have received a copy of the GNU General Public License
31701 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31702 + */
31703 +
31704 +/*
31705 + * virtual or vertical directory
31706 + */
31707 +
31708 +#include <linux/iversion.h>
31709 +#include "aufs.h"
31710 +
31711 +static unsigned int calc_size(int nlen)
31712 +{
31713 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31714 +}
31715 +
31716 +static int set_deblk_end(union au_vdir_deblk_p *p,
31717 +                        union au_vdir_deblk_p *deblk_end)
31718 +{
31719 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31720 +               p->de->de_str.len = 0;
31721 +               /* smp_mb(); */
31722 +               return 0;
31723 +       }
31724 +       return -1; /* error */
31725 +}
31726 +
31727 +/* returns true or false */
31728 +static int is_deblk_end(union au_vdir_deblk_p *p,
31729 +                       union au_vdir_deblk_p *deblk_end)
31730 +{
31731 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31732 +               return !p->de->de_str.len;
31733 +       return 1;
31734 +}
31735 +
31736 +static unsigned char *last_deblk(struct au_vdir *vdir)
31737 +{
31738 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31739 +}
31740 +
31741 +/* ---------------------------------------------------------------------- */
31742 +
31743 +/* estimate the appropriate size for name hash table */
31744 +unsigned int au_rdhash_est(loff_t sz)
31745 +{
31746 +       unsigned int n;
31747 +
31748 +       n = UINT_MAX;
31749 +       sz >>= 10;
31750 +       if (sz < n)
31751 +               n = sz;
31752 +       if (sz < AUFS_RDHASH_DEF)
31753 +               n = AUFS_RDHASH_DEF;
31754 +       /* pr_info("n %u\n", n); */
31755 +       return n;
31756 +}
31757 +
31758 +/*
31759 + * the allocated memory has to be freed by
31760 + * au_nhash_wh_free() or au_nhash_de_free().
31761 + */
31762 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31763 +{
31764 +       struct hlist_head *head;
31765 +       unsigned int u;
31766 +       size_t sz;
31767 +
31768 +       sz = sizeof(*nhash->nh_head) * num_hash;
31769 +       head = kmalloc(sz, gfp);
31770 +       if (head) {
31771 +               nhash->nh_num = num_hash;
31772 +               nhash->nh_head = head;
31773 +               for (u = 0; u < num_hash; u++)
31774 +                       INIT_HLIST_HEAD(head++);
31775 +               return 0; /* success */
31776 +       }
31777 +
31778 +       return -ENOMEM;
31779 +}
31780 +
31781 +static void nhash_count(struct hlist_head *head)
31782 +{
31783 +#if 0 /* debugging */
31784 +       unsigned long n;
31785 +       struct hlist_node *pos;
31786 +
31787 +       n = 0;
31788 +       hlist_for_each(pos, head)
31789 +               n++;
31790 +       pr_info("%lu\n", n);
31791 +#endif
31792 +}
31793 +
31794 +static void au_nhash_wh_do_free(struct hlist_head *head)
31795 +{
31796 +       struct au_vdir_wh *pos;
31797 +       struct hlist_node *node;
31798 +
31799 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31800 +               au_kfree_rcu(pos);
31801 +}
31802 +
31803 +static void au_nhash_de_do_free(struct hlist_head *head)
31804 +{
31805 +       struct au_vdir_dehstr *pos;
31806 +       struct hlist_node *node;
31807 +
31808 +       hlist_for_each_entry_safe(pos, node, head, hash)
31809 +               au_cache_free_vdir_dehstr(pos);
31810 +}
31811 +
31812 +static void au_nhash_do_free(struct au_nhash *nhash,
31813 +                            void (*free)(struct hlist_head *head))
31814 +{
31815 +       unsigned int n;
31816 +       struct hlist_head *head;
31817 +
31818 +       n = nhash->nh_num;
31819 +       if (!n)
31820 +               return;
31821 +
31822 +       head = nhash->nh_head;
31823 +       while (n-- > 0) {
31824 +               nhash_count(head);
31825 +               free(head++);
31826 +       }
31827 +       au_kfree_try_rcu(nhash->nh_head);
31828 +}
31829 +
31830 +void au_nhash_wh_free(struct au_nhash *whlist)
31831 +{
31832 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31833 +}
31834 +
31835 +static void au_nhash_de_free(struct au_nhash *delist)
31836 +{
31837 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31838 +}
31839 +
31840 +/* ---------------------------------------------------------------------- */
31841 +
31842 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31843 +                           int limit)
31844 +{
31845 +       int num;
31846 +       unsigned int u, n;
31847 +       struct hlist_head *head;
31848 +       struct au_vdir_wh *pos;
31849 +
31850 +       num = 0;
31851 +       n = whlist->nh_num;
31852 +       head = whlist->nh_head;
31853 +       for (u = 0; u < n; u++, head++)
31854 +               hlist_for_each_entry(pos, head, wh_hash)
31855 +                       if (pos->wh_bindex == btgt && ++num > limit)
31856 +                               return 1;
31857 +       return 0;
31858 +}
31859 +
31860 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31861 +                                      unsigned char *name,
31862 +                                      unsigned int len)
31863 +{
31864 +       unsigned int v;
31865 +       /* const unsigned int magic_bit = 12; */
31866 +
31867 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31868 +
31869 +       v = 0;
31870 +       if (len > 8)
31871 +               len = 8;
31872 +       while (len--)
31873 +               v += *name++;
31874 +       /* v = hash_long(v, magic_bit); */
31875 +       v %= nhash->nh_num;
31876 +       return nhash->nh_head + v;
31877 +}
31878 +
31879 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31880 +                             int nlen)
31881 +{
31882 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31883 +}
31884 +
31885 +/* returns found or not */
31886 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31887 +{
31888 +       struct hlist_head *head;
31889 +       struct au_vdir_wh *pos;
31890 +       struct au_vdir_destr *str;
31891 +
31892 +       head = au_name_hash(whlist, name, nlen);
31893 +       hlist_for_each_entry(pos, head, wh_hash) {
31894 +               str = &pos->wh_str;
31895 +               AuDbg("%.*s\n", str->len, str->name);
31896 +               if (au_nhash_test_name(str, name, nlen))
31897 +                       return 1;
31898 +       }
31899 +       return 0;
31900 +}
31901 +
31902 +/* returns found(true) or not */
31903 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31904 +{
31905 +       struct hlist_head *head;
31906 +       struct au_vdir_dehstr *pos;
31907 +       struct au_vdir_destr *str;
31908 +
31909 +       head = au_name_hash(delist, name, nlen);
31910 +       hlist_for_each_entry(pos, head, hash) {
31911 +               str = pos->str;
31912 +               AuDbg("%.*s\n", str->len, str->name);
31913 +               if (au_nhash_test_name(str, name, nlen))
31914 +                       return 1;
31915 +       }
31916 +       return 0;
31917 +}
31918 +
31919 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31920 +                           unsigned char d_type)
31921 +{
31922 +#ifdef CONFIG_AUFS_SHWH
31923 +       wh->wh_ino = ino;
31924 +       wh->wh_type = d_type;
31925 +#endif
31926 +}
31927 +
31928 +/* ---------------------------------------------------------------------- */
31929 +
31930 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31931 +                      unsigned int d_type, aufs_bindex_t bindex,
31932 +                      unsigned char shwh)
31933 +{
31934 +       int err;
31935 +       struct au_vdir_destr *str;
31936 +       struct au_vdir_wh *wh;
31937 +
31938 +       AuDbg("%.*s\n", nlen, name);
31939 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31940 +
31941 +       err = -ENOMEM;
31942 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31943 +       if (unlikely(!wh))
31944 +               goto out;
31945 +
31946 +       err = 0;
31947 +       wh->wh_bindex = bindex;
31948 +       if (shwh)
31949 +               au_shwh_init_wh(wh, ino, d_type);
31950 +       str = &wh->wh_str;
31951 +       str->len = nlen;
31952 +       memcpy(str->name, name, nlen);
31953 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31954 +       /* smp_mb(); */
31955 +
31956 +out:
31957 +       return err;
31958 +}
31959 +
31960 +static int append_deblk(struct au_vdir *vdir)
31961 +{
31962 +       int err;
31963 +       unsigned long ul;
31964 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31965 +       union au_vdir_deblk_p p, deblk_end;
31966 +       unsigned char **o;
31967 +
31968 +       err = -ENOMEM;
31969 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31970 +                       GFP_NOFS, /*may_shrink*/0);
31971 +       if (unlikely(!o))
31972 +               goto out;
31973 +
31974 +       vdir->vd_deblk = o;
31975 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31976 +       if (p.deblk) {
31977 +               ul = vdir->vd_nblk++;
31978 +               vdir->vd_deblk[ul] = p.deblk;
31979 +               vdir->vd_last.ul = ul;
31980 +               vdir->vd_last.p.deblk = p.deblk;
31981 +               deblk_end.deblk = p.deblk + deblk_sz;
31982 +               err = set_deblk_end(&p, &deblk_end);
31983 +       }
31984 +
31985 +out:
31986 +       return err;
31987 +}
31988 +
31989 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31990 +                    unsigned int d_type, struct au_nhash *delist)
31991 +{
31992 +       int err;
31993 +       unsigned int sz;
31994 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31995 +       union au_vdir_deblk_p p, *room, deblk_end;
31996 +       struct au_vdir_dehstr *dehstr;
31997 +
31998 +       p.deblk = last_deblk(vdir);
31999 +       deblk_end.deblk = p.deblk + deblk_sz;
32000 +       room = &vdir->vd_last.p;
32001 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32002 +                 || !is_deblk_end(room, &deblk_end));
32003 +
32004 +       sz = calc_size(nlen);
32005 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32006 +               err = append_deblk(vdir);
32007 +               if (unlikely(err))
32008 +                       goto out;
32009 +
32010 +               p.deblk = last_deblk(vdir);
32011 +               deblk_end.deblk = p.deblk + deblk_sz;
32012 +               /* smp_mb(); */
32013 +               AuDebugOn(room->deblk != p.deblk);
32014 +       }
32015 +
32016 +       err = -ENOMEM;
32017 +       dehstr = au_cache_alloc_vdir_dehstr();
32018 +       if (unlikely(!dehstr))
32019 +               goto out;
32020 +
32021 +       dehstr->str = &room->de->de_str;
32022 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32023 +       room->de->de_ino = ino;
32024 +       room->de->de_type = d_type;
32025 +       room->de->de_str.len = nlen;
32026 +       memcpy(room->de->de_str.name, name, nlen);
32027 +
32028 +       err = 0;
32029 +       room->deblk += sz;
32030 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32031 +               err = append_deblk(vdir);
32032 +       /* smp_mb(); */
32033 +
32034 +out:
32035 +       return err;
32036 +}
32037 +
32038 +/* ---------------------------------------------------------------------- */
32039 +
32040 +void au_vdir_free(struct au_vdir *vdir)
32041 +{
32042 +       unsigned char **deblk;
32043 +
32044 +       deblk = vdir->vd_deblk;
32045 +       while (vdir->vd_nblk--)
32046 +               au_kfree_try_rcu(*deblk++);
32047 +       au_kfree_try_rcu(vdir->vd_deblk);
32048 +       au_cache_free_vdir(vdir);
32049 +}
32050 +
32051 +static struct au_vdir *alloc_vdir(struct file *file)
32052 +{
32053 +       struct au_vdir *vdir;
32054 +       struct super_block *sb;
32055 +       int err;
32056 +
32057 +       sb = file->f_path.dentry->d_sb;
32058 +       SiMustAnyLock(sb);
32059 +
32060 +       err = -ENOMEM;
32061 +       vdir = au_cache_alloc_vdir();
32062 +       if (unlikely(!vdir))
32063 +               goto out;
32064 +
32065 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32066 +       if (unlikely(!vdir->vd_deblk))
32067 +               goto out_free;
32068 +
32069 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32070 +       if (!vdir->vd_deblk_sz) {
32071 +               /* estimate the appropriate size for deblk */
32072 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32073 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32074 +       }
32075 +       vdir->vd_nblk = 0;
32076 +       vdir->vd_version = 0;
32077 +       vdir->vd_jiffy = 0;
32078 +       err = append_deblk(vdir);
32079 +       if (!err)
32080 +               return vdir; /* success */
32081 +
32082 +       au_kfree_try_rcu(vdir->vd_deblk);
32083 +
32084 +out_free:
32085 +       au_cache_free_vdir(vdir);
32086 +out:
32087 +       vdir = ERR_PTR(err);
32088 +       return vdir;
32089 +}
32090 +
32091 +static int reinit_vdir(struct au_vdir *vdir)
32092 +{
32093 +       int err;
32094 +       union au_vdir_deblk_p p, deblk_end;
32095 +
32096 +       while (vdir->vd_nblk > 1) {
32097 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32098 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32099 +               vdir->vd_nblk--;
32100 +       }
32101 +       p.deblk = vdir->vd_deblk[0];
32102 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32103 +       err = set_deblk_end(&p, &deblk_end);
32104 +       /* keep vd_dblk_sz */
32105 +       vdir->vd_last.ul = 0;
32106 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32107 +       vdir->vd_version = 0;
32108 +       vdir->vd_jiffy = 0;
32109 +       /* smp_mb(); */
32110 +       return err;
32111 +}
32112 +
32113 +/* ---------------------------------------------------------------------- */
32114 +
32115 +#define AuFillVdir_CALLED      1
32116 +#define AuFillVdir_WHABLE      (1 << 1)
32117 +#define AuFillVdir_SHWH                (1 << 2)
32118 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32119 +#define au_fset_fillvdir(flags, name) \
32120 +       do { (flags) |= AuFillVdir_##name; } while (0)
32121 +#define au_fclr_fillvdir(flags, name) \
32122 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32123 +
32124 +#ifndef CONFIG_AUFS_SHWH
32125 +#undef AuFillVdir_SHWH
32126 +#define AuFillVdir_SHWH                0
32127 +#endif
32128 +
32129 +struct fillvdir_arg {
32130 +       struct dir_context      ctx;
32131 +       struct file             *file;
32132 +       struct au_vdir          *vdir;
32133 +       struct au_nhash         delist;
32134 +       struct au_nhash         whlist;
32135 +       aufs_bindex_t           bindex;
32136 +       unsigned int            flags;
32137 +       int                     err;
32138 +};
32139 +
32140 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32141 +                   loff_t offset __maybe_unused, u64 h_ino,
32142 +                   unsigned int d_type)
32143 +{
32144 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32145 +       char *name = (void *)__name;
32146 +       struct super_block *sb;
32147 +       ino_t ino;
32148 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32149 +
32150 +       arg->err = 0;
32151 +       sb = arg->file->f_path.dentry->d_sb;
32152 +       au_fset_fillvdir(arg->flags, CALLED);
32153 +       /* smp_mb(); */
32154 +       if (nlen <= AUFS_WH_PFX_LEN
32155 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32156 +               if (test_known(&arg->delist, name, nlen)
32157 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32158 +                       goto out; /* already exists or whiteouted */
32159 +
32160 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32161 +               if (!arg->err) {
32162 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32163 +                               d_type = DT_UNKNOWN;
32164 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32165 +                                            d_type, &arg->delist);
32166 +               }
32167 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32168 +               name += AUFS_WH_PFX_LEN;
32169 +               nlen -= AUFS_WH_PFX_LEN;
32170 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32171 +                       goto out; /* already whiteouted */
32172 +
32173 +               ino = 0; /* just to suppress a warning */
32174 +               if (shwh)
32175 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32176 +                                            &ino);
32177 +               if (!arg->err) {
32178 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32179 +                               d_type = DT_UNKNOWN;
32180 +                       arg->err = au_nhash_append_wh
32181 +                               (&arg->whlist, name, nlen, ino, d_type,
32182 +                                arg->bindex, shwh);
32183 +               }
32184 +       }
32185 +
32186 +out:
32187 +       if (!arg->err)
32188 +               arg->vdir->vd_jiffy = jiffies;
32189 +       /* smp_mb(); */
32190 +       AuTraceErr(arg->err);
32191 +       return arg->err;
32192 +}
32193 +
32194 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32195 +                         struct au_nhash *whlist, struct au_nhash *delist)
32196 +{
32197 +#ifdef CONFIG_AUFS_SHWH
32198 +       int err;
32199 +       unsigned int nh, u;
32200 +       struct hlist_head *head;
32201 +       struct au_vdir_wh *pos;
32202 +       struct hlist_node *n;
32203 +       char *p, *o;
32204 +       struct au_vdir_destr *destr;
32205 +
32206 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32207 +
32208 +       err = -ENOMEM;
32209 +       o = p = (void *)__get_free_page(GFP_NOFS);
32210 +       if (unlikely(!p))
32211 +               goto out;
32212 +
32213 +       err = 0;
32214 +       nh = whlist->nh_num;
32215 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32216 +       p += AUFS_WH_PFX_LEN;
32217 +       for (u = 0; u < nh; u++) {
32218 +               head = whlist->nh_head + u;
32219 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32220 +                       destr = &pos->wh_str;
32221 +                       memcpy(p, destr->name, destr->len);
32222 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32223 +                                       pos->wh_ino, pos->wh_type, delist);
32224 +                       if (unlikely(err))
32225 +                               break;
32226 +               }
32227 +       }
32228 +
32229 +       free_page((unsigned long)o);
32230 +
32231 +out:
32232 +       AuTraceErr(err);
32233 +       return err;
32234 +#else
32235 +       return 0;
32236 +#endif
32237 +}
32238 +
32239 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32240 +{
32241 +       int err;
32242 +       unsigned int rdhash;
32243 +       loff_t offset;
32244 +       aufs_bindex_t bbot, bindex, btop;
32245 +       unsigned char shwh;
32246 +       struct file *hf, *file;
32247 +       struct super_block *sb;
32248 +
32249 +       file = arg->file;
32250 +       sb = file->f_path.dentry->d_sb;
32251 +       SiMustAnyLock(sb);
32252 +
32253 +       rdhash = au_sbi(sb)->si_rdhash;
32254 +       if (!rdhash)
32255 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32256 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32257 +       if (unlikely(err))
32258 +               goto out;
32259 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32260 +       if (unlikely(err))
32261 +               goto out_delist;
32262 +
32263 +       err = 0;
32264 +       arg->flags = 0;
32265 +       shwh = 0;
32266 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32267 +               shwh = 1;
32268 +               au_fset_fillvdir(arg->flags, SHWH);
32269 +       }
32270 +       btop = au_fbtop(file);
32271 +       bbot = au_fbbot_dir(file);
32272 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32273 +               hf = au_hf_dir(file, bindex);
32274 +               if (!hf)
32275 +                       continue;
32276 +
32277 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32278 +               err = offset;
32279 +               if (unlikely(offset))
32280 +                       break;
32281 +
32282 +               arg->bindex = bindex;
32283 +               au_fclr_fillvdir(arg->flags, WHABLE);
32284 +               if (shwh
32285 +                   || (bindex != bbot
32286 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32287 +                       au_fset_fillvdir(arg->flags, WHABLE);
32288 +               do {
32289 +                       arg->err = 0;
32290 +                       au_fclr_fillvdir(arg->flags, CALLED);
32291 +                       /* smp_mb(); */
32292 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32293 +                       if (err >= 0)
32294 +                               err = arg->err;
32295 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32296 +
32297 +               /*
32298 +                * dir_relax() may be good for concurrency, but aufs should not
32299 +                * use it since it will cause a lockdep problem.
32300 +                */
32301 +       }
32302 +
32303 +       if (!err && shwh)
32304 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32305 +
32306 +       au_nhash_wh_free(&arg->whlist);
32307 +
32308 +out_delist:
32309 +       au_nhash_de_free(&arg->delist);
32310 +out:
32311 +       return err;
32312 +}
32313 +
32314 +static int read_vdir(struct file *file, int may_read)
32315 +{
32316 +       int err;
32317 +       unsigned long expire;
32318 +       unsigned char do_read;
32319 +       struct fillvdir_arg arg = {
32320 +               .ctx = {
32321 +                       .actor = fillvdir
32322 +               }
32323 +       };
32324 +       struct inode *inode;
32325 +       struct au_vdir *vdir, *allocated;
32326 +
32327 +       err = 0;
32328 +       inode = file_inode(file);
32329 +       IMustLock(inode);
32330 +       IiMustWriteLock(inode);
32331 +       SiMustAnyLock(inode->i_sb);
32332 +
32333 +       allocated = NULL;
32334 +       do_read = 0;
32335 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32336 +       vdir = au_ivdir(inode);
32337 +       if (!vdir) {
32338 +               do_read = 1;
32339 +               vdir = alloc_vdir(file);
32340 +               err = PTR_ERR(vdir);
32341 +               if (IS_ERR(vdir))
32342 +                       goto out;
32343 +               err = 0;
32344 +               allocated = vdir;
32345 +       } else if (may_read
32346 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32347 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32348 +               do_read = 1;
32349 +               err = reinit_vdir(vdir);
32350 +               if (unlikely(err))
32351 +                       goto out;
32352 +       }
32353 +
32354 +       if (!do_read)
32355 +               return 0; /* success */
32356 +
32357 +       arg.file = file;
32358 +       arg.vdir = vdir;
32359 +       err = au_do_read_vdir(&arg);
32360 +       if (!err) {
32361 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32362 +               vdir->vd_version = inode_query_iversion(inode);
32363 +               vdir->vd_last.ul = 0;
32364 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32365 +               if (allocated)
32366 +                       au_set_ivdir(inode, allocated);
32367 +       } else if (allocated)
32368 +               au_vdir_free(allocated);
32369 +
32370 +out:
32371 +       return err;
32372 +}
32373 +
32374 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32375 +{
32376 +       int err, rerr;
32377 +       unsigned long ul, n;
32378 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32379 +
32380 +       AuDebugOn(tgt->vd_nblk != 1);
32381 +
32382 +       err = -ENOMEM;
32383 +       if (tgt->vd_nblk < src->vd_nblk) {
32384 +               unsigned char **p;
32385 +
32386 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32387 +                               GFP_NOFS, /*may_shrink*/0);
32388 +               if (unlikely(!p))
32389 +                       goto out;
32390 +               tgt->vd_deblk = p;
32391 +       }
32392 +
32393 +       if (tgt->vd_deblk_sz != deblk_sz) {
32394 +               unsigned char *p;
32395 +
32396 +               tgt->vd_deblk_sz = deblk_sz;
32397 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32398 +                               /*may_shrink*/1);
32399 +               if (unlikely(!p))
32400 +                       goto out;
32401 +               tgt->vd_deblk[0] = p;
32402 +       }
32403 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32404 +       tgt->vd_version = src->vd_version;
32405 +       tgt->vd_jiffy = src->vd_jiffy;
32406 +
32407 +       n = src->vd_nblk;
32408 +       for (ul = 1; ul < n; ul++) {
32409 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32410 +                                           GFP_NOFS);
32411 +               if (unlikely(!tgt->vd_deblk[ul]))
32412 +                       goto out;
32413 +               tgt->vd_nblk++;
32414 +       }
32415 +       tgt->vd_nblk = n;
32416 +       tgt->vd_last.ul = tgt->vd_last.ul;
32417 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32418 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32419 +               - src->vd_deblk[src->vd_last.ul];
32420 +       /* smp_mb(); */
32421 +       return 0; /* success */
32422 +
32423 +out:
32424 +       rerr = reinit_vdir(tgt);
32425 +       BUG_ON(rerr);
32426 +       return err;
32427 +}
32428 +
32429 +int au_vdir_init(struct file *file)
32430 +{
32431 +       int err;
32432 +       struct inode *inode;
32433 +       struct au_vdir *vdir_cache, *allocated;
32434 +
32435 +       /* test file->f_pos here instead of ctx->pos */
32436 +       err = read_vdir(file, !file->f_pos);
32437 +       if (unlikely(err))
32438 +               goto out;
32439 +
32440 +       allocated = NULL;
32441 +       vdir_cache = au_fvdir_cache(file);
32442 +       if (!vdir_cache) {
32443 +               vdir_cache = alloc_vdir(file);
32444 +               err = PTR_ERR(vdir_cache);
32445 +               if (IS_ERR(vdir_cache))
32446 +                       goto out;
32447 +               allocated = vdir_cache;
32448 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32449 +               /* test file->f_pos here instead of ctx->pos */
32450 +               err = reinit_vdir(vdir_cache);
32451 +               if (unlikely(err))
32452 +                       goto out;
32453 +       } else
32454 +               return 0; /* success */
32455 +
32456 +       inode = file_inode(file);
32457 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32458 +       if (!err) {
32459 +               file->f_version = inode_query_iversion(inode);
32460 +               if (allocated)
32461 +                       au_set_fvdir_cache(file, allocated);
32462 +       } else if (allocated)
32463 +               au_vdir_free(allocated);
32464 +
32465 +out:
32466 +       return err;
32467 +}
32468 +
32469 +static loff_t calc_offset(struct au_vdir *vdir)
32470 +{
32471 +       loff_t offset;
32472 +       union au_vdir_deblk_p p;
32473 +
32474 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32475 +       offset = vdir->vd_last.p.deblk - p.deblk;
32476 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32477 +       return offset;
32478 +}
32479 +
32480 +/* returns true or false */
32481 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32482 +{
32483 +       int valid;
32484 +       unsigned int deblk_sz;
32485 +       unsigned long ul, n;
32486 +       loff_t offset;
32487 +       union au_vdir_deblk_p p, deblk_end;
32488 +       struct au_vdir *vdir_cache;
32489 +
32490 +       valid = 1;
32491 +       vdir_cache = au_fvdir_cache(file);
32492 +       offset = calc_offset(vdir_cache);
32493 +       AuDbg("offset %lld\n", offset);
32494 +       if (ctx->pos == offset)
32495 +               goto out;
32496 +
32497 +       vdir_cache->vd_last.ul = 0;
32498 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32499 +       if (!ctx->pos)
32500 +               goto out;
32501 +
32502 +       valid = 0;
32503 +       deblk_sz = vdir_cache->vd_deblk_sz;
32504 +       ul = div64_u64(ctx->pos, deblk_sz);
32505 +       AuDbg("ul %lu\n", ul);
32506 +       if (ul >= vdir_cache->vd_nblk)
32507 +               goto out;
32508 +
32509 +       n = vdir_cache->vd_nblk;
32510 +       for (; ul < n; ul++) {
32511 +               p.deblk = vdir_cache->vd_deblk[ul];
32512 +               deblk_end.deblk = p.deblk + deblk_sz;
32513 +               offset = ul;
32514 +               offset *= deblk_sz;
32515 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32516 +                       unsigned int l;
32517 +
32518 +                       l = calc_size(p.de->de_str.len);
32519 +                       offset += l;
32520 +                       p.deblk += l;
32521 +               }
32522 +               if (!is_deblk_end(&p, &deblk_end)) {
32523 +                       valid = 1;
32524 +                       vdir_cache->vd_last.ul = ul;
32525 +                       vdir_cache->vd_last.p = p;
32526 +                       break;
32527 +               }
32528 +       }
32529 +
32530 +out:
32531 +       /* smp_mb(); */
32532 +       if (!valid)
32533 +               AuDbg("valid %d\n", !valid);
32534 +       return valid;
32535 +}
32536 +
32537 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32538 +{
32539 +       unsigned int l, deblk_sz;
32540 +       union au_vdir_deblk_p deblk_end;
32541 +       struct au_vdir *vdir_cache;
32542 +       struct au_vdir_de *de;
32543 +
32544 +       if (!seek_vdir(file, ctx))
32545 +               return 0;
32546 +
32547 +       vdir_cache = au_fvdir_cache(file);
32548 +       deblk_sz = vdir_cache->vd_deblk_sz;
32549 +       while (1) {
32550 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32551 +               deblk_end.deblk += deblk_sz;
32552 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32553 +                       de = vdir_cache->vd_last.p.de;
32554 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32555 +                             de->de_str.len, de->de_str.name, ctx->pos,
32556 +                             (unsigned long)de->de_ino, de->de_type);
32557 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32558 +                                              de->de_str.len, de->de_ino,
32559 +                                              de->de_type))) {
32560 +                               /* todo: ignore the error caused by udba? */
32561 +                               /* return err; */
32562 +                               return 0;
32563 +                       }
32564 +
32565 +                       l = calc_size(de->de_str.len);
32566 +                       vdir_cache->vd_last.p.deblk += l;
32567 +                       ctx->pos += l;
32568 +               }
32569 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32570 +                       vdir_cache->vd_last.ul++;
32571 +                       vdir_cache->vd_last.p.deblk
32572 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32573 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32574 +                       continue;
32575 +               }
32576 +               break;
32577 +       }
32578 +
32579 +       /* smp_mb(); */
32580 +       return 0;
32581 +}
32582 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32583 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32584 +++ linux/fs/aufs/vfsub.c       2021-02-24 13:33:42.747680497 +0100
32585 @@ -0,0 +1,885 @@
32586 +// SPDX-License-Identifier: GPL-2.0
32587 +/*
32588 + * Copyright (C) 2005-2020 Junjiro R. Okajima
32589 + *
32590 + * This program, aufs is free software; you can redistribute it and/or modify
32591 + * it under the terms of the GNU General Public License as published by
32592 + * the Free Software Foundation; either version 2 of the License, or
32593 + * (at your option) any later version.
32594 + *
32595 + * This program is distributed in the hope that it will be useful,
32596 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32597 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32598 + * GNU General Public License for more details.
32599 + *
32600 + * You should have received a copy of the GNU General Public License
32601 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32602 + */
32603 +
32604 +/*
32605 + * sub-routines for VFS
32606 + */
32607 +
32608 +#include <linux/mnt_namespace.h>
32609 +#include <linux/namei.h>
32610 +#include <linux/nsproxy.h>
32611 +#include <linux/security.h>
32612 +#include <linux/splice.h>
32613 +#include "aufs.h"
32614 +
32615 +#ifdef CONFIG_AUFS_BR_FUSE
32616 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32617 +{
32618 +       if (!au_test_fuse(h_sb) || !au_userns)
32619 +               return 0;
32620 +
32621 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32622 +}
32623 +#endif
32624 +
32625 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32626 +{
32627 +       int err;
32628 +
32629 +       lockdep_off();
32630 +       down_read(&h_sb->s_umount);
32631 +       err = __sync_filesystem(h_sb, wait);
32632 +       up_read(&h_sb->s_umount);
32633 +       lockdep_on();
32634 +
32635 +       return err;
32636 +}
32637 +
32638 +/* ---------------------------------------------------------------------- */
32639 +
32640 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32641 +{
32642 +       int err;
32643 +       struct kstat st;
32644 +       struct super_block *h_sb;
32645 +
32646 +       /* for remote fs, leave work for its getattr or d_revalidate */
32647 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32648 +       /* still some fs may acquire i_mutex. we need to skip them */
32649 +       err = 0;
32650 +       if (!did)
32651 +               did = &err;
32652 +       h_sb = h_path->dentry->d_sb;
32653 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32654 +       if (*did)
32655 +               err = vfsub_getattr(h_path, &st);
32656 +
32657 +       return err;
32658 +}
32659 +
32660 +/* ---------------------------------------------------------------------- */
32661 +
32662 +struct file *vfsub_dentry_open(struct path *path, int flags)
32663 +{
32664 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32665 +                          current_cred());
32666 +}
32667 +
32668 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32669 +{
32670 +       struct file *file;
32671 +
32672 +       lockdep_off();
32673 +       file = filp_open(path,
32674 +                        oflags /* | __FMODE_NONOTIFY */,
32675 +                        mode);
32676 +       lockdep_on();
32677 +       if (IS_ERR(file))
32678 +               goto out;
32679 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32680 +
32681 +out:
32682 +       return file;
32683 +}
32684 +
32685 +/*
32686 + * Ideally this function should call VFS:do_last() in order to keep all its
32687 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32688 + * structure such as nameidata. This is a second (or third) best approach.
32689 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32690 + */
32691 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32692 +                     struct vfsub_aopen_args *args)
32693 +{
32694 +       int err;
32695 +       struct au_branch *br = args->br;
32696 +       struct file *file = args->file;
32697 +       /* copied from linux/fs/namei.c:atomic_open() */
32698 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32699 +
32700 +       IMustLock(dir);
32701 +       AuDebugOn(!dir->i_op->atomic_open);
32702 +
32703 +       err = au_br_test_oflag(args->open_flag, br);
32704 +       if (unlikely(err))
32705 +               goto out;
32706 +
32707 +       au_lcnt_inc(&br->br_nfiles);
32708 +       file->f_path.dentry = DENTRY_NOT_SET;
32709 +       file->f_path.mnt = au_br_mnt(br);
32710 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32711 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32712 +                                    args->create_mode);
32713 +       if (unlikely(err < 0)) {
32714 +               au_lcnt_dec(&br->br_nfiles);
32715 +               goto out;
32716 +       }
32717 +
32718 +       /* temporary workaround for nfsv4 branch */
32719 +       if (au_test_nfs(dir->i_sb))
32720 +               nfs_mark_for_revalidate(dir);
32721 +
32722 +       if (file->f_mode & FMODE_CREATED)
32723 +               fsnotify_create(dir, dentry);
32724 +       if (!(file->f_mode & FMODE_OPENED)) {
32725 +               au_lcnt_dec(&br->br_nfiles);
32726 +               goto out;
32727 +       }
32728 +
32729 +       /* todo: call VFS:may_open() here */
32730 +       /* todo: ima_file_check() too? */
32731 +       if (!err && (args->open_flag & __FMODE_EXEC))
32732 +               err = deny_write_access(file);
32733 +       if (!err)
32734 +               fsnotify_open(file);
32735 +       else
32736 +               au_lcnt_dec(&br->br_nfiles);
32737 +       /* note that the file is created and still opened */
32738 +
32739 +out:
32740 +       return err;
32741 +}
32742 +
32743 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32744 +{
32745 +       int err;
32746 +
32747 +       err = kern_path(name, flags, path);
32748 +       if (!err && d_is_positive(path->dentry))
32749 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32750 +       return err;
32751 +}
32752 +
32753 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32754 +                                            struct dentry *parent, int len)
32755 +{
32756 +       struct path path = {
32757 +               .mnt = NULL
32758 +       };
32759 +
32760 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32761 +       if (IS_ERR(path.dentry))
32762 +               goto out;
32763 +       if (d_is_positive(path.dentry))
32764 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32765 +
32766 +out:
32767 +       AuTraceErrPtr(path.dentry);
32768 +       return path.dentry;
32769 +}
32770 +
32771 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32772 +                                   int len)
32773 +{
32774 +       struct path path = {
32775 +               .mnt = NULL
32776 +       };
32777 +
32778 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32779 +       IMustLock(d_inode(parent));
32780 +
32781 +       path.dentry = lookup_one_len(name, parent, len);
32782 +       if (IS_ERR(path.dentry))
32783 +               goto out;
32784 +       if (d_is_positive(path.dentry))
32785 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32786 +
32787 +out:
32788 +       AuTraceErrPtr(path.dentry);
32789 +       return path.dentry;
32790 +}
32791 +
32792 +void vfsub_call_lkup_one(void *args)
32793 +{
32794 +       struct vfsub_lkup_one_args *a = args;
32795 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32796 +}
32797 +
32798 +/* ---------------------------------------------------------------------- */
32799 +
32800 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32801 +                                struct dentry *d2, struct au_hinode *hdir2)
32802 +{
32803 +       struct dentry *d;
32804 +
32805 +       lockdep_off();
32806 +       d = lock_rename(d1, d2);
32807 +       lockdep_on();
32808 +       au_hn_suspend(hdir1);
32809 +       if (hdir1 != hdir2)
32810 +               au_hn_suspend(hdir2);
32811 +
32812 +       return d;
32813 +}
32814 +
32815 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32816 +                        struct dentry *d2, struct au_hinode *hdir2)
32817 +{
32818 +       au_hn_resume(hdir1);
32819 +       if (hdir1 != hdir2)
32820 +               au_hn_resume(hdir2);
32821 +       lockdep_off();
32822 +       unlock_rename(d1, d2);
32823 +       lockdep_on();
32824 +}
32825 +
32826 +/* ---------------------------------------------------------------------- */
32827 +
32828 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32829 +{
32830 +       int err;
32831 +       struct dentry *d;
32832 +
32833 +       IMustLock(dir);
32834 +
32835 +       d = path->dentry;
32836 +       path->dentry = d->d_parent;
32837 +       err = security_path_mknod(path, d, mode, 0);
32838 +       path->dentry = d;
32839 +       if (unlikely(err))
32840 +               goto out;
32841 +
32842 +       lockdep_off();
32843 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32844 +       lockdep_on();
32845 +       if (!err) {
32846 +               struct path tmp = *path;
32847 +               int did;
32848 +
32849 +               vfsub_update_h_iattr(&tmp, &did);
32850 +               if (did) {
32851 +                       tmp.dentry = path->dentry->d_parent;
32852 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32853 +               }
32854 +               /*ignore*/
32855 +       }
32856 +
32857 +out:
32858 +       return err;
32859 +}
32860 +
32861 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32862 +{
32863 +       int err;
32864 +       struct dentry *d;
32865 +
32866 +       IMustLock(dir);
32867 +
32868 +       d = path->dentry;
32869 +       path->dentry = d->d_parent;
32870 +       err = security_path_symlink(path, d, symname);
32871 +       path->dentry = d;
32872 +       if (unlikely(err))
32873 +               goto out;
32874 +
32875 +       lockdep_off();
32876 +       err = vfs_symlink(dir, path->dentry, symname);
32877 +       lockdep_on();
32878 +       if (!err) {
32879 +               struct path tmp = *path;
32880 +               int did;
32881 +
32882 +               vfsub_update_h_iattr(&tmp, &did);
32883 +               if (did) {
32884 +                       tmp.dentry = path->dentry->d_parent;
32885 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32886 +               }
32887 +               /*ignore*/
32888 +       }
32889 +
32890 +out:
32891 +       return err;
32892 +}
32893 +
32894 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32895 +{
32896 +       int err;
32897 +       struct dentry *d;
32898 +
32899 +       IMustLock(dir);
32900 +
32901 +       d = path->dentry;
32902 +       path->dentry = d->d_parent;
32903 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32904 +       path->dentry = d;
32905 +       if (unlikely(err))
32906 +               goto out;
32907 +
32908 +       lockdep_off();
32909 +       err = vfs_mknod(dir, path->dentry, mode, dev);
32910 +       lockdep_on();
32911 +       if (!err) {
32912 +               struct path tmp = *path;
32913 +               int did;
32914 +
32915 +               vfsub_update_h_iattr(&tmp, &did);
32916 +               if (did) {
32917 +                       tmp.dentry = path->dentry->d_parent;
32918 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32919 +               }
32920 +               /*ignore*/
32921 +       }
32922 +
32923 +out:
32924 +       return err;
32925 +}
32926 +
32927 +static int au_test_nlink(struct inode *inode)
32928 +{
32929 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32930 +
32931 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
32932 +           || inode->i_nlink < link_max)
32933 +               return 0;
32934 +       return -EMLINK;
32935 +}
32936 +
32937 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32938 +              struct inode **delegated_inode)
32939 +{
32940 +       int err;
32941 +       struct dentry *d;
32942 +
32943 +       IMustLock(dir);
32944 +
32945 +       err = au_test_nlink(d_inode(src_dentry));
32946 +       if (unlikely(err))
32947 +               return err;
32948 +
32949 +       /* we don't call may_linkat() */
32950 +       d = path->dentry;
32951 +       path->dentry = d->d_parent;
32952 +       err = security_path_link(src_dentry, path, d);
32953 +       path->dentry = d;
32954 +       if (unlikely(err))
32955 +               goto out;
32956 +
32957 +       lockdep_off();
32958 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
32959 +       lockdep_on();
32960 +       if (!err) {
32961 +               struct path tmp = *path;
32962 +               int did;
32963 +
32964 +               /* fuse has different memory inode for the same inumber */
32965 +               vfsub_update_h_iattr(&tmp, &did);
32966 +               if (did) {
32967 +                       tmp.dentry = path->dentry->d_parent;
32968 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32969 +                       tmp.dentry = src_dentry;
32970 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32971 +               }
32972 +               /*ignore*/
32973 +       }
32974 +
32975 +out:
32976 +       return err;
32977 +}
32978 +
32979 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
32980 +                struct inode *dir, struct path *path,
32981 +                struct inode **delegated_inode, unsigned int flags)
32982 +{
32983 +       int err;
32984 +       struct path tmp = {
32985 +               .mnt    = path->mnt
32986 +       };
32987 +       struct dentry *d;
32988 +
32989 +       IMustLock(dir);
32990 +       IMustLock(src_dir);
32991 +
32992 +       d = path->dentry;
32993 +       path->dentry = d->d_parent;
32994 +       tmp.dentry = src_dentry->d_parent;
32995 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
32996 +       path->dentry = d;
32997 +       if (unlikely(err))
32998 +               goto out;
32999 +
33000 +       lockdep_off();
33001 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33002 +                        delegated_inode, flags);
33003 +       lockdep_on();
33004 +       if (!err) {
33005 +               int did;
33006 +
33007 +               tmp.dentry = d->d_parent;
33008 +               vfsub_update_h_iattr(&tmp, &did);
33009 +               if (did) {
33010 +                       tmp.dentry = src_dentry;
33011 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33012 +                       tmp.dentry = src_dentry->d_parent;
33013 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33014 +               }
33015 +               /*ignore*/
33016 +       }
33017 +
33018 +out:
33019 +       return err;
33020 +}
33021 +
33022 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33023 +{
33024 +       int err;
33025 +       struct dentry *d;
33026 +
33027 +       IMustLock(dir);
33028 +
33029 +       d = path->dentry;
33030 +       path->dentry = d->d_parent;
33031 +       err = security_path_mkdir(path, d, mode);
33032 +       path->dentry = d;
33033 +       if (unlikely(err))
33034 +               goto out;
33035 +
33036 +       lockdep_off();
33037 +       err = vfs_mkdir(dir, path->dentry, mode);
33038 +       lockdep_on();
33039 +       if (!err) {
33040 +               struct path tmp = *path;
33041 +               int did;
33042 +
33043 +               vfsub_update_h_iattr(&tmp, &did);
33044 +               if (did) {
33045 +                       tmp.dentry = path->dentry->d_parent;
33046 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33047 +               }
33048 +               /*ignore*/
33049 +       }
33050 +
33051 +out:
33052 +       return err;
33053 +}
33054 +
33055 +int vfsub_rmdir(struct inode *dir, struct path *path)
33056 +{
33057 +       int err;
33058 +       struct dentry *d;
33059 +
33060 +       IMustLock(dir);
33061 +
33062 +       d = path->dentry;
33063 +       path->dentry = d->d_parent;
33064 +       err = security_path_rmdir(path, d);
33065 +       path->dentry = d;
33066 +       if (unlikely(err))
33067 +               goto out;
33068 +
33069 +       lockdep_off();
33070 +       err = vfs_rmdir(dir, path->dentry);
33071 +       lockdep_on();
33072 +       if (!err) {
33073 +               struct path tmp = {
33074 +                       .dentry = path->dentry->d_parent,
33075 +                       .mnt    = path->mnt
33076 +               };
33077 +
33078 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33079 +       }
33080 +
33081 +out:
33082 +       return err;
33083 +}
33084 +
33085 +/* ---------------------------------------------------------------------- */
33086 +
33087 +/* todo: support mmap_sem? */
33088 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33089 +                    loff_t *ppos)
33090 +{
33091 +       ssize_t err;
33092 +
33093 +       lockdep_off();
33094 +       err = vfs_read(file, ubuf, count, ppos);
33095 +       lockdep_on();
33096 +       if (err >= 0)
33097 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33098 +       return err;
33099 +}
33100 +
33101 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33102 +                    loff_t *ppos)
33103 +{
33104 +       ssize_t err;
33105 +
33106 +       lockdep_off();
33107 +       err = kernel_read(file, kbuf, count, ppos);
33108 +       lockdep_on();
33109 +       AuTraceErr(err);
33110 +       if (err >= 0)
33111 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33112 +       return err;
33113 +}
33114 +
33115 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33116 +                     loff_t *ppos)
33117 +{
33118 +       ssize_t err;
33119 +
33120 +       lockdep_off();
33121 +       err = vfs_write(file, ubuf, count, ppos);
33122 +       lockdep_on();
33123 +       if (err >= 0)
33124 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33125 +       return err;
33126 +}
33127 +
33128 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33129 +{
33130 +       ssize_t err;
33131 +
33132 +       lockdep_off();
33133 +       err = kernel_write(file, kbuf, count, ppos);
33134 +       lockdep_on();
33135 +       if (err >= 0)
33136 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33137 +       return err;
33138 +}
33139 +
33140 +int vfsub_flush(struct file *file, fl_owner_t id)
33141 +{
33142 +       int err;
33143 +
33144 +       err = 0;
33145 +       if (file->f_op->flush) {
33146 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33147 +                       err = file->f_op->flush(file, id);
33148 +               else {
33149 +                       lockdep_off();
33150 +                       err = file->f_op->flush(file, id);
33151 +                       lockdep_on();
33152 +               }
33153 +               if (!err)
33154 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33155 +               /*ignore*/
33156 +       }
33157 +       return err;
33158 +}
33159 +
33160 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33161 +{
33162 +       int err;
33163 +
33164 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33165 +
33166 +       lockdep_off();
33167 +       err = iterate_dir(file, ctx);
33168 +       lockdep_on();
33169 +       if (err >= 0)
33170 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33171 +
33172 +       return err;
33173 +}
33174 +
33175 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33176 +                    struct pipe_inode_info *pipe, size_t len,
33177 +                    unsigned int flags)
33178 +{
33179 +       long err;
33180 +
33181 +       lockdep_off();
33182 +       err = do_splice_to(in, ppos, pipe, len, flags);
33183 +       lockdep_on();
33184 +       file_accessed(in);
33185 +       if (err >= 0)
33186 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33187 +       return err;
33188 +}
33189 +
33190 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33191 +                      loff_t *ppos, size_t len, unsigned int flags)
33192 +{
33193 +       long err;
33194 +
33195 +       lockdep_off();
33196 +       err = do_splice_from(pipe, out, ppos, len, flags);
33197 +       lockdep_on();
33198 +       if (err >= 0)
33199 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33200 +       return err;
33201 +}
33202 +
33203 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33204 +{
33205 +       int err;
33206 +
33207 +       /* file can be NULL */
33208 +       lockdep_off();
33209 +       err = vfs_fsync(file, datasync);
33210 +       lockdep_on();
33211 +       if (!err) {
33212 +               if (!path) {
33213 +                       AuDebugOn(!file);
33214 +                       path = &file->f_path;
33215 +               }
33216 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33217 +       }
33218 +       return err;
33219 +}
33220 +
33221 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33222 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33223 +               struct file *h_file)
33224 +{
33225 +       int err;
33226 +       struct inode *h_inode;
33227 +       struct super_block *h_sb;
33228 +
33229 +       if (!h_file) {
33230 +               err = vfsub_truncate(h_path, length);
33231 +               goto out;
33232 +       }
33233 +
33234 +       h_inode = d_inode(h_path->dentry);
33235 +       h_sb = h_inode->i_sb;
33236 +       lockdep_off();
33237 +       sb_start_write(h_sb);
33238 +       lockdep_on();
33239 +       err = locks_verify_truncate(h_inode, h_file, length);
33240 +       if (!err)
33241 +               err = security_path_truncate(h_path);
33242 +       if (!err) {
33243 +               lockdep_off();
33244 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33245 +               lockdep_on();
33246 +       }
33247 +       lockdep_off();
33248 +       sb_end_write(h_sb);
33249 +       lockdep_on();
33250 +
33251 +out:
33252 +       return err;
33253 +}
33254 +
33255 +/* ---------------------------------------------------------------------- */
33256 +
33257 +struct au_vfsub_mkdir_args {
33258 +       int *errp;
33259 +       struct inode *dir;
33260 +       struct path *path;
33261 +       int mode;
33262 +};
33263 +
33264 +static void au_call_vfsub_mkdir(void *args)
33265 +{
33266 +       struct au_vfsub_mkdir_args *a = args;
33267 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33268 +}
33269 +
33270 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33271 +{
33272 +       int err, do_sio, wkq_err;
33273 +
33274 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33275 +       if (!do_sio) {
33276 +               lockdep_off();
33277 +               err = vfsub_mkdir(dir, path, mode);
33278 +               lockdep_on();
33279 +       } else {
33280 +               struct au_vfsub_mkdir_args args = {
33281 +                       .errp   = &err,
33282 +                       .dir    = dir,
33283 +                       .path   = path,
33284 +                       .mode   = mode
33285 +               };
33286 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33287 +               if (unlikely(wkq_err))
33288 +                       err = wkq_err;
33289 +       }
33290 +
33291 +       return err;
33292 +}
33293 +
33294 +struct au_vfsub_rmdir_args {
33295 +       int *errp;
33296 +       struct inode *dir;
33297 +       struct path *path;
33298 +};
33299 +
33300 +static void au_call_vfsub_rmdir(void *args)
33301 +{
33302 +       struct au_vfsub_rmdir_args *a = args;
33303 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33304 +}
33305 +
33306 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33307 +{
33308 +       int err, do_sio, wkq_err;
33309 +
33310 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33311 +       if (!do_sio) {
33312 +               lockdep_off();
33313 +               err = vfsub_rmdir(dir, path);
33314 +               lockdep_on();
33315 +       } else {
33316 +               struct au_vfsub_rmdir_args args = {
33317 +                       .errp   = &err,
33318 +                       .dir    = dir,
33319 +                       .path   = path
33320 +               };
33321 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33322 +               if (unlikely(wkq_err))
33323 +                       err = wkq_err;
33324 +       }
33325 +
33326 +       return err;
33327 +}
33328 +
33329 +/* ---------------------------------------------------------------------- */
33330 +
33331 +struct notify_change_args {
33332 +       int *errp;
33333 +       struct path *path;
33334 +       struct iattr *ia;
33335 +       struct inode **delegated_inode;
33336 +};
33337 +
33338 +static void call_notify_change(void *args)
33339 +{
33340 +       struct notify_change_args *a = args;
33341 +       struct inode *h_inode;
33342 +
33343 +       h_inode = d_inode(a->path->dentry);
33344 +       IMustLock(h_inode);
33345 +
33346 +       *a->errp = -EPERM;
33347 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33348 +               lockdep_off();
33349 +               *a->errp = notify_change(a->path->dentry, a->ia,
33350 +                                        a->delegated_inode);
33351 +               lockdep_on();
33352 +               if (!*a->errp)
33353 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33354 +       }
33355 +       AuTraceErr(*a->errp);
33356 +}
33357 +
33358 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33359 +                       struct inode **delegated_inode)
33360 +{
33361 +       int err;
33362 +       struct notify_change_args args = {
33363 +               .errp                   = &err,
33364 +               .path                   = path,
33365 +               .ia                     = ia,
33366 +               .delegated_inode        = delegated_inode
33367 +       };
33368 +
33369 +       call_notify_change(&args);
33370 +
33371 +       return err;
33372 +}
33373 +
33374 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33375 +                           struct inode **delegated_inode)
33376 +{
33377 +       int err, wkq_err;
33378 +       struct notify_change_args args = {
33379 +               .errp                   = &err,
33380 +               .path                   = path,
33381 +               .ia                     = ia,
33382 +               .delegated_inode        = delegated_inode
33383 +       };
33384 +
33385 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33386 +       if (unlikely(wkq_err))
33387 +               err = wkq_err;
33388 +
33389 +       return err;
33390 +}
33391 +
33392 +/* ---------------------------------------------------------------------- */
33393 +
33394 +struct unlink_args {
33395 +       int *errp;
33396 +       struct inode *dir;
33397 +       struct path *path;
33398 +       struct inode **delegated_inode;
33399 +};
33400 +
33401 +static void call_unlink(void *args)
33402 +{
33403 +       struct unlink_args *a = args;
33404 +       struct dentry *d = a->path->dentry;
33405 +       struct inode *h_inode;
33406 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33407 +                                     && au_dcount(d) == 1);
33408 +
33409 +       IMustLock(a->dir);
33410 +
33411 +       a->path->dentry = d->d_parent;
33412 +       *a->errp = security_path_unlink(a->path, d);
33413 +       a->path->dentry = d;
33414 +       if (unlikely(*a->errp))
33415 +               return;
33416 +
33417 +       if (!stop_sillyrename)
33418 +               dget(d);
33419 +       h_inode = NULL;
33420 +       if (d_is_positive(d)) {
33421 +               h_inode = d_inode(d);
33422 +               ihold(h_inode);
33423 +       }
33424 +
33425 +       lockdep_off();
33426 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33427 +       lockdep_on();
33428 +       if (!*a->errp) {
33429 +               struct path tmp = {
33430 +                       .dentry = d->d_parent,
33431 +                       .mnt    = a->path->mnt
33432 +               };
33433 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33434 +       }
33435 +
33436 +       if (!stop_sillyrename)
33437 +               dput(d);
33438 +       if (h_inode)
33439 +               iput(h_inode);
33440 +
33441 +       AuTraceErr(*a->errp);
33442 +}
33443 +
33444 +/*
33445 + * @dir: must be locked.
33446 + * @dentry: target dentry.
33447 + */
33448 +int vfsub_unlink(struct inode *dir, struct path *path,
33449 +                struct inode **delegated_inode, int force)
33450 +{
33451 +       int err;
33452 +       struct unlink_args args = {
33453 +               .errp                   = &err,
33454 +               .dir                    = dir,
33455 +               .path                   = path,
33456 +               .delegated_inode        = delegated_inode
33457 +       };
33458 +
33459 +       if (!force)
33460 +               call_unlink(&args);
33461 +       else {
33462 +               int wkq_err;
33463 +
33464 +               wkq_err = au_wkq_wait(call_unlink, &args);
33465 +               if (unlikely(wkq_err))
33466 +                       err = wkq_err;
33467 +       }
33468 +
33469 +       return err;
33470 +}
33471 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33472 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33473 +++ linux/fs/aufs/vfsub.h       2021-02-24 13:33:42.747680497 +0100
33474 @@ -0,0 +1,354 @@
33475 +/* SPDX-License-Identifier: GPL-2.0 */
33476 +/*
33477 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33478 + *
33479 + * This program, aufs is free software; you can redistribute it and/or modify
33480 + * it under the terms of the GNU General Public License as published by
33481 + * the Free Software Foundation; either version 2 of the License, or
33482 + * (at your option) any later version.
33483 + *
33484 + * This program is distributed in the hope that it will be useful,
33485 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33486 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33487 + * GNU General Public License for more details.
33488 + *
33489 + * You should have received a copy of the GNU General Public License
33490 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33491 + */
33492 +
33493 +/*
33494 + * sub-routines for VFS
33495 + */
33496 +
33497 +#ifndef __AUFS_VFSUB_H__
33498 +#define __AUFS_VFSUB_H__
33499 +
33500 +#ifdef __KERNEL__
33501 +
33502 +#include <linux/fs.h>
33503 +#include <linux/mount.h>
33504 +#include <linux/posix_acl.h>
33505 +#include <linux/xattr.h>
33506 +#include "debug.h"
33507 +
33508 +/* copied from linux/fs/internal.h */
33509 +/* todo: BAD approach!! */
33510 +extern void __mnt_drop_write(struct vfsmount *);
33511 +extern struct file *alloc_empty_file(int, const struct cred *);
33512 +
33513 +/* ---------------------------------------------------------------------- */
33514 +
33515 +/* lock subclass for lower inode */
33516 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33517 +/* reduce? gave up. */
33518 +enum {
33519 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33520 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33521 +       AuLsc_I_PARENT2,        /* copyup dirs */
33522 +       AuLsc_I_PARENT3,        /* copyup wh */
33523 +       AuLsc_I_CHILD,
33524 +       AuLsc_I_CHILD2,
33525 +       AuLsc_I_End
33526 +};
33527 +
33528 +/* to debug easier, do not make them inlined functions */
33529 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33530 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33531 +
33532 +/* ---------------------------------------------------------------------- */
33533 +
33534 +static inline void vfsub_drop_nlink(struct inode *inode)
33535 +{
33536 +       AuDebugOn(!inode->i_nlink);
33537 +       drop_nlink(inode);
33538 +}
33539 +
33540 +static inline void vfsub_dead_dir(struct inode *inode)
33541 +{
33542 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33543 +       inode->i_flags |= S_DEAD;
33544 +       clear_nlink(inode);
33545 +}
33546 +
33547 +static inline int vfsub_native_ro(struct inode *inode)
33548 +{
33549 +       return sb_rdonly(inode->i_sb)
33550 +               || IS_RDONLY(inode)
33551 +               /* || IS_APPEND(inode) */
33552 +               || IS_IMMUTABLE(inode);
33553 +}
33554 +
33555 +#ifdef CONFIG_AUFS_BR_FUSE
33556 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33557 +#else
33558 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33559 +#endif
33560 +
33561 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33562 +
33563 +/* ---------------------------------------------------------------------- */
33564 +
33565 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33566 +struct file *vfsub_dentry_open(struct path *path, int flags);
33567 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33568 +struct au_branch;
33569 +struct vfsub_aopen_args {
33570 +       struct file             *file;
33571 +       unsigned int            open_flag;
33572 +       umode_t                 create_mode;
33573 +       struct au_branch        *br;
33574 +};
33575 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33576 +                     struct vfsub_aopen_args *args);
33577 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33578 +
33579 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33580 +                                            struct dentry *parent, int len);
33581 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33582 +                                   int len);
33583 +
33584 +struct vfsub_lkup_one_args {
33585 +       struct dentry **errp;
33586 +       struct qstr *name;
33587 +       struct dentry *parent;
33588 +};
33589 +
33590 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33591 +                                           struct dentry *parent)
33592 +{
33593 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33594 +}
33595 +
33596 +void vfsub_call_lkup_one(void *args);
33597 +
33598 +/* ---------------------------------------------------------------------- */
33599 +
33600 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33601 +{
33602 +       int err;
33603 +
33604 +       lockdep_off();
33605 +       err = mnt_want_write(mnt);
33606 +       lockdep_on();
33607 +       return err;
33608 +}
33609 +
33610 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33611 +{
33612 +       lockdep_off();
33613 +       mnt_drop_write(mnt);
33614 +       lockdep_on();
33615 +}
33616 +
33617 +#if 0 /* reserved */
33618 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33619 +{
33620 +       lockdep_off();
33621 +       mnt_drop_write_file(file);
33622 +       lockdep_on();
33623 +}
33624 +#endif
33625 +
33626 +/* ---------------------------------------------------------------------- */
33627 +
33628 +struct au_hinode;
33629 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33630 +                                struct dentry *d2, struct au_hinode *hdir2);
33631 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33632 +                        struct dentry *d2, struct au_hinode *hdir2);
33633 +
33634 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33635 +                bool want_excl);
33636 +int vfsub_symlink(struct inode *dir, struct path *path,
33637 +                 const char *symname);
33638 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33639 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33640 +              struct path *path, struct inode **delegated_inode);
33641 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33642 +                struct inode *hdir, struct path *path,
33643 +                struct inode **delegated_inode, unsigned int flags);
33644 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33645 +int vfsub_rmdir(struct inode *dir, struct path *path);
33646 +
33647 +/* ---------------------------------------------------------------------- */
33648 +
33649 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33650 +                    loff_t *ppos);
33651 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33652 +                       loff_t *ppos);
33653 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33654 +                     loff_t *ppos);
33655 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33656 +                     loff_t *ppos);
33657 +int vfsub_flush(struct file *file, fl_owner_t id);
33658 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33659 +
33660 +static inline loff_t vfsub_f_size_read(struct file *file)
33661 +{
33662 +       return i_size_read(file_inode(file));
33663 +}
33664 +
33665 +static inline unsigned int vfsub_file_flags(struct file *file)
33666 +{
33667 +       unsigned int flags;
33668 +
33669 +       spin_lock(&file->f_lock);
33670 +       flags = file->f_flags;
33671 +       spin_unlock(&file->f_lock);
33672 +
33673 +       return flags;
33674 +}
33675 +
33676 +static inline int vfsub_file_execed(struct file *file)
33677 +{
33678 +       /* todo: direct access f_flags */
33679 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33680 +}
33681 +
33682 +#if 0 /* reserved */
33683 +static inline void vfsub_file_accessed(struct file *h_file)
33684 +{
33685 +       file_accessed(h_file);
33686 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33687 +}
33688 +#endif
33689 +
33690 +#if 0 /* reserved */
33691 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33692 +                                    struct dentry *h_dentry)
33693 +{
33694 +       struct path h_path = {
33695 +               .dentry = h_dentry,
33696 +               .mnt    = h_mnt
33697 +       };
33698 +       touch_atime(&h_path);
33699 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33700 +}
33701 +#endif
33702 +
33703 +static inline int vfsub_update_time(struct inode *h_inode,
33704 +                                   struct timespec64 *ts, int flags)
33705 +{
33706 +       return update_time(h_inode, ts, flags);
33707 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33708 +}
33709 +
33710 +#ifdef CONFIG_FS_POSIX_ACL
33711 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33712 +{
33713 +       int err;
33714 +
33715 +       err = posix_acl_chmod(h_inode, h_mode);
33716 +       if (err == -EOPNOTSUPP)
33717 +               err = 0;
33718 +       return err;
33719 +}
33720 +#else
33721 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33722 +#endif
33723 +
33724 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33725 +                    struct pipe_inode_info *pipe, size_t len,
33726 +                    unsigned int flags);
33727 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33728 +                      loff_t *ppos, size_t len, unsigned int flags);
33729 +
33730 +static inline long vfsub_truncate(struct path *path, loff_t length)
33731 +{
33732 +       long err;
33733 +
33734 +       lockdep_off();
33735 +       err = vfs_truncate(path, length);
33736 +       lockdep_on();
33737 +       return err;
33738 +}
33739 +
33740 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33741 +               struct file *h_file);
33742 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33743 +
33744 +/*
33745 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33746 + * ioctl.
33747 + */
33748 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33749 +                                           loff_t len)
33750 +{
33751 +       loff_t err;
33752 +
33753 +       lockdep_off();
33754 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33755 +       lockdep_on();
33756 +
33757 +       return err;
33758 +}
33759 +
33760 +/* copy_file_range(2) is a systemcall */
33761 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33762 +                                           struct file *dst, loff_t dst_pos,
33763 +                                           size_t len, unsigned int flags)
33764 +{
33765 +       ssize_t ssz;
33766 +
33767 +       lockdep_off();
33768 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33769 +       lockdep_on();
33770 +
33771 +       return ssz;
33772 +}
33773 +
33774 +/* ---------------------------------------------------------------------- */
33775 +
33776 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33777 +{
33778 +       loff_t err;
33779 +
33780 +       lockdep_off();
33781 +       err = vfs_llseek(file, offset, origin);
33782 +       lockdep_on();
33783 +       return err;
33784 +}
33785 +
33786 +/* ---------------------------------------------------------------------- */
33787 +
33788 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33789 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33790 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33791 +                           struct inode **delegated_inode);
33792 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33793 +                       struct inode **delegated_inode);
33794 +int vfsub_unlink(struct inode *dir, struct path *path,
33795 +                struct inode **delegated_inode, int force);
33796 +
33797 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33798 +{
33799 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33800 +}
33801 +
33802 +/* ---------------------------------------------------------------------- */
33803 +
33804 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33805 +                                const void *value, size_t size, int flags)
33806 +{
33807 +       int err;
33808 +
33809 +       lockdep_off();
33810 +       err = vfs_setxattr(dentry, name, value, size, flags);
33811 +       lockdep_on();
33812 +
33813 +       return err;
33814 +}
33815 +
33816 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33817 +{
33818 +       int err;
33819 +
33820 +       lockdep_off();
33821 +       err = vfs_removexattr(dentry, name);
33822 +       lockdep_on();
33823 +
33824 +       return err;
33825 +}
33826 +
33827 +#endif /* __KERNEL__ */
33828 +#endif /* __AUFS_VFSUB_H__ */
33829 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33830 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33831 +++ linux/fs/aufs/wbr_policy.c  2021-02-24 13:33:42.747680497 +0100
33832 @@ -0,0 +1,830 @@
33833 +// SPDX-License-Identifier: GPL-2.0
33834 +/*
33835 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33836 + *
33837 + * This program, aufs is free software; you can redistribute it and/or modify
33838 + * it under the terms of the GNU General Public License as published by
33839 + * the Free Software Foundation; either version 2 of the License, or
33840 + * (at your option) any later version.
33841 + *
33842 + * This program is distributed in the hope that it will be useful,
33843 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33844 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33845 + * GNU General Public License for more details.
33846 + *
33847 + * You should have received a copy of the GNU General Public License
33848 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33849 + */
33850 +
33851 +/*
33852 + * policies for selecting one among multiple writable branches
33853 + */
33854 +
33855 +#include <linux/statfs.h>
33856 +#include "aufs.h"
33857 +
33858 +/* subset of cpup_attr() */
33859 +static noinline_for_stack
33860 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33861 +{
33862 +       int err, sbits;
33863 +       struct iattr ia;
33864 +       struct inode *h_isrc;
33865 +
33866 +       h_isrc = d_inode(h_src);
33867 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33868 +       ia.ia_mode = h_isrc->i_mode;
33869 +       ia.ia_uid = h_isrc->i_uid;
33870 +       ia.ia_gid = h_isrc->i_gid;
33871 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33872 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33873 +       /* no delegation since it is just created */
33874 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33875 +
33876 +       /* is this nfs only? */
33877 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33878 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33879 +               ia.ia_mode = h_isrc->i_mode;
33880 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33881 +       }
33882 +
33883 +       return err;
33884 +}
33885 +
33886 +#define AuCpdown_PARENT_OPQ    1
33887 +#define AuCpdown_WHED          (1 << 1)
33888 +#define AuCpdown_MADE_DIR      (1 << 2)
33889 +#define AuCpdown_DIROPQ                (1 << 3)
33890 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
33891 +#define au_fset_cpdown(flags, name) \
33892 +       do { (flags) |= AuCpdown_##name; } while (0)
33893 +#define au_fclr_cpdown(flags, name) \
33894 +       do { (flags) &= ~AuCpdown_##name; } while (0)
33895 +
33896 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33897 +                            unsigned int *flags)
33898 +{
33899 +       int err;
33900 +       struct dentry *opq_dentry;
33901 +
33902 +       opq_dentry = au_diropq_create(dentry, bdst);
33903 +       err = PTR_ERR(opq_dentry);
33904 +       if (IS_ERR(opq_dentry))
33905 +               goto out;
33906 +       dput(opq_dentry);
33907 +       au_fset_cpdown(*flags, DIROPQ);
33908 +
33909 +out:
33910 +       return err;
33911 +}
33912 +
33913 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33914 +                           struct inode *dir, aufs_bindex_t bdst)
33915 +{
33916 +       int err;
33917 +       struct path h_path;
33918 +       struct au_branch *br;
33919 +
33920 +       br = au_sbr(dentry->d_sb, bdst);
33921 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33922 +       err = PTR_ERR(h_path.dentry);
33923 +       if (IS_ERR(h_path.dentry))
33924 +               goto out;
33925 +
33926 +       err = 0;
33927 +       if (d_is_positive(h_path.dentry)) {
33928 +               h_path.mnt = au_br_mnt(br);
33929 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33930 +                                         dentry);
33931 +       }
33932 +       dput(h_path.dentry);
33933 +
33934 +out:
33935 +       return err;
33936 +}
33937 +
33938 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
33939 +                        struct au_pin *pin,
33940 +                        struct dentry *h_parent, void *arg)
33941 +{
33942 +       int err, rerr;
33943 +       aufs_bindex_t bopq, btop;
33944 +       struct path h_path;
33945 +       struct dentry *parent;
33946 +       struct inode *h_dir, *h_inode, *inode, *dir;
33947 +       unsigned int *flags = arg;
33948 +
33949 +       btop = au_dbtop(dentry);
33950 +       /* dentry is di-locked */
33951 +       parent = dget_parent(dentry);
33952 +       dir = d_inode(parent);
33953 +       h_dir = d_inode(h_parent);
33954 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33955 +       IMustLock(h_dir);
33956 +
33957 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
33958 +       if (unlikely(err < 0))
33959 +               goto out;
33960 +       h_path.dentry = au_h_dptr(dentry, bdst);
33961 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33962 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
33963 +       if (unlikely(err))
33964 +               goto out_put;
33965 +       au_fset_cpdown(*flags, MADE_DIR);
33966 +
33967 +       bopq = au_dbdiropq(dentry);
33968 +       au_fclr_cpdown(*flags, WHED);
33969 +       au_fclr_cpdown(*flags, DIROPQ);
33970 +       if (au_dbwh(dentry) == bdst)
33971 +               au_fset_cpdown(*flags, WHED);
33972 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33973 +               au_fset_cpdown(*flags, PARENT_OPQ);
33974 +       h_inode = d_inode(h_path.dentry);
33975 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
33976 +       if (au_ftest_cpdown(*flags, WHED)) {
33977 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
33978 +               if (unlikely(err)) {
33979 +                       inode_unlock(h_inode);
33980 +                       goto out_dir;
33981 +               }
33982 +       }
33983 +
33984 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
33985 +       inode_unlock(h_inode);
33986 +       if (unlikely(err))
33987 +               goto out_opq;
33988 +
33989 +       if (au_ftest_cpdown(*flags, WHED)) {
33990 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33991 +               if (unlikely(err))
33992 +                       goto out_opq;
33993 +       }
33994 +
33995 +       inode = d_inode(dentry);
33996 +       if (au_ibbot(inode) < bdst)
33997 +               au_set_ibbot(inode, bdst);
33998 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
33999 +                     au_hi_flags(inode, /*isdir*/1));
34000 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34001 +       goto out; /* success */
34002 +
34003 +       /* revert */
34004 +out_opq:
34005 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34006 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34007 +               rerr = au_diropq_remove(dentry, bdst);
34008 +               inode_unlock(h_inode);
34009 +               if (unlikely(rerr)) {
34010 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34011 +                               dentry, bdst, rerr);
34012 +                       err = -EIO;
34013 +                       goto out;
34014 +               }
34015 +       }
34016 +out_dir:
34017 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34018 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34019 +               if (unlikely(rerr)) {
34020 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34021 +                               dentry, bdst, rerr);
34022 +                       err = -EIO;
34023 +               }
34024 +       }
34025 +out_put:
34026 +       au_set_h_dptr(dentry, bdst, NULL);
34027 +       if (au_dbbot(dentry) == bdst)
34028 +               au_update_dbbot(dentry);
34029 +out:
34030 +       dput(parent);
34031 +       return err;
34032 +}
34033 +
34034 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34035 +{
34036 +       int err;
34037 +       unsigned int flags;
34038 +
34039 +       flags = 0;
34040 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34041 +
34042 +       return err;
34043 +}
34044 +
34045 +/* ---------------------------------------------------------------------- */
34046 +
34047 +/* policies for create */
34048 +
34049 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34050 +{
34051 +       int err, i, j, ndentry;
34052 +       aufs_bindex_t bopq;
34053 +       struct au_dcsub_pages dpages;
34054 +       struct au_dpage *dpage;
34055 +       struct dentry **dentries, *parent, *d;
34056 +
34057 +       err = au_dpages_init(&dpages, GFP_NOFS);
34058 +       if (unlikely(err))
34059 +               goto out;
34060 +       parent = dget_parent(dentry);
34061 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34062 +       if (unlikely(err))
34063 +               goto out_free;
34064 +
34065 +       err = bindex;
34066 +       for (i = 0; i < dpages.ndpage; i++) {
34067 +               dpage = dpages.dpages + i;
34068 +               dentries = dpage->dentries;
34069 +               ndentry = dpage->ndentry;
34070 +               for (j = 0; j < ndentry; j++) {
34071 +                       d = dentries[j];
34072 +                       di_read_lock_parent2(d, !AuLock_IR);
34073 +                       bopq = au_dbdiropq(d);
34074 +                       di_read_unlock(d, !AuLock_IR);
34075 +                       if (bopq >= 0 && bopq < err)
34076 +                               err = bopq;
34077 +               }
34078 +       }
34079 +
34080 +out_free:
34081 +       dput(parent);
34082 +       au_dpages_free(&dpages);
34083 +out:
34084 +       return err;
34085 +}
34086 +
34087 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34088 +{
34089 +       for (; bindex >= 0; bindex--)
34090 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34091 +                       return bindex;
34092 +       return -EROFS;
34093 +}
34094 +
34095 +/* top down parent */
34096 +static int au_wbr_create_tdp(struct dentry *dentry,
34097 +                            unsigned int flags __maybe_unused)
34098 +{
34099 +       int err;
34100 +       aufs_bindex_t btop, bindex;
34101 +       struct super_block *sb;
34102 +       struct dentry *parent, *h_parent;
34103 +
34104 +       sb = dentry->d_sb;
34105 +       btop = au_dbtop(dentry);
34106 +       err = btop;
34107 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34108 +               goto out;
34109 +
34110 +       err = -EROFS;
34111 +       parent = dget_parent(dentry);
34112 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34113 +               h_parent = au_h_dptr(parent, bindex);
34114 +               if (!h_parent || d_is_negative(h_parent))
34115 +                       continue;
34116 +
34117 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34118 +                       err = bindex;
34119 +                       break;
34120 +               }
34121 +       }
34122 +       dput(parent);
34123 +
34124 +       /* bottom up here */
34125 +       if (unlikely(err < 0)) {
34126 +               err = au_wbr_bu(sb, btop - 1);
34127 +               if (err >= 0)
34128 +                       err = au_wbr_nonopq(dentry, err);
34129 +       }
34130 +
34131 +out:
34132 +       AuDbg("b%d\n", err);
34133 +       return err;
34134 +}
34135 +
34136 +/* ---------------------------------------------------------------------- */
34137 +
34138 +/* an exception for the policy other than tdp */
34139 +static int au_wbr_create_exp(struct dentry *dentry)
34140 +{
34141 +       int err;
34142 +       aufs_bindex_t bwh, bdiropq;
34143 +       struct dentry *parent;
34144 +
34145 +       err = -1;
34146 +       bwh = au_dbwh(dentry);
34147 +       parent = dget_parent(dentry);
34148 +       bdiropq = au_dbdiropq(parent);
34149 +       if (bwh >= 0) {
34150 +               if (bdiropq >= 0)
34151 +                       err = min(bdiropq, bwh);
34152 +               else
34153 +                       err = bwh;
34154 +               AuDbg("%d\n", err);
34155 +       } else if (bdiropq >= 0) {
34156 +               err = bdiropq;
34157 +               AuDbg("%d\n", err);
34158 +       }
34159 +       dput(parent);
34160 +
34161 +       if (err >= 0)
34162 +               err = au_wbr_nonopq(dentry, err);
34163 +
34164 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34165 +               err = -1;
34166 +
34167 +       AuDbg("%d\n", err);
34168 +       return err;
34169 +}
34170 +
34171 +/* ---------------------------------------------------------------------- */
34172 +
34173 +/* round robin */
34174 +static int au_wbr_create_init_rr(struct super_block *sb)
34175 +{
34176 +       int err;
34177 +
34178 +       err = au_wbr_bu(sb, au_sbbot(sb));
34179 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34180 +       /* smp_mb(); */
34181 +
34182 +       AuDbg("b%d\n", err);
34183 +       return err;
34184 +}
34185 +
34186 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34187 +{
34188 +       int err, nbr;
34189 +       unsigned int u;
34190 +       aufs_bindex_t bindex, bbot;
34191 +       struct super_block *sb;
34192 +       atomic_t *next;
34193 +
34194 +       err = au_wbr_create_exp(dentry);
34195 +       if (err >= 0)
34196 +               goto out;
34197 +
34198 +       sb = dentry->d_sb;
34199 +       next = &au_sbi(sb)->si_wbr_rr_next;
34200 +       bbot = au_sbbot(sb);
34201 +       nbr = bbot + 1;
34202 +       for (bindex = 0; bindex <= bbot; bindex++) {
34203 +               if (!au_ftest_wbr(flags, DIR)) {
34204 +                       err = atomic_dec_return(next) + 1;
34205 +                       /* modulo for 0 is meaningless */
34206 +                       if (unlikely(!err))
34207 +                               err = atomic_dec_return(next) + 1;
34208 +               } else
34209 +                       err = atomic_read(next);
34210 +               AuDbg("%d\n", err);
34211 +               u = err;
34212 +               err = u % nbr;
34213 +               AuDbg("%d\n", err);
34214 +               if (!au_br_rdonly(au_sbr(sb, err)))
34215 +                       break;
34216 +               err = -EROFS;
34217 +       }
34218 +
34219 +       if (err >= 0)
34220 +               err = au_wbr_nonopq(dentry, err);
34221 +
34222 +out:
34223 +       AuDbg("%d\n", err);
34224 +       return err;
34225 +}
34226 +
34227 +/* ---------------------------------------------------------------------- */
34228 +
34229 +/* most free space */
34230 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34231 +{
34232 +       struct super_block *sb;
34233 +       struct au_branch *br;
34234 +       struct au_wbr_mfs *mfs;
34235 +       struct dentry *h_parent;
34236 +       aufs_bindex_t bindex, bbot;
34237 +       int err;
34238 +       unsigned long long b, bavail;
34239 +       struct path h_path;
34240 +       /* reduce the stack usage */
34241 +       struct kstatfs *st;
34242 +
34243 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34244 +       if (unlikely(!st)) {
34245 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34246 +               return;
34247 +       }
34248 +
34249 +       bavail = 0;
34250 +       sb = dentry->d_sb;
34251 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34252 +       MtxMustLock(&mfs->mfs_lock);
34253 +       mfs->mfs_bindex = -EROFS;
34254 +       mfs->mfsrr_bytes = 0;
34255 +       if (!parent) {
34256 +               bindex = 0;
34257 +               bbot = au_sbbot(sb);
34258 +       } else {
34259 +               bindex = au_dbtop(parent);
34260 +               bbot = au_dbtaildir(parent);
34261 +       }
34262 +
34263 +       for (; bindex <= bbot; bindex++) {
34264 +               if (parent) {
34265 +                       h_parent = au_h_dptr(parent, bindex);
34266 +                       if (!h_parent || d_is_negative(h_parent))
34267 +                               continue;
34268 +               }
34269 +               br = au_sbr(sb, bindex);
34270 +               if (au_br_rdonly(br))
34271 +                       continue;
34272 +
34273 +               /* sb->s_root for NFS is unreliable */
34274 +               h_path.mnt = au_br_mnt(br);
34275 +               h_path.dentry = h_path.mnt->mnt_root;
34276 +               err = vfs_statfs(&h_path, st);
34277 +               if (unlikely(err)) {
34278 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34279 +                       continue;
34280 +               }
34281 +
34282 +               /* when the available size is equal, select the lower one */
34283 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34284 +                            || sizeof(b) < sizeof(st->f_bsize));
34285 +               b = st->f_bavail * st->f_bsize;
34286 +               br->br_wbr->wbr_bytes = b;
34287 +               if (b >= bavail) {
34288 +                       bavail = b;
34289 +                       mfs->mfs_bindex = bindex;
34290 +                       mfs->mfs_jiffy = jiffies;
34291 +               }
34292 +       }
34293 +
34294 +       mfs->mfsrr_bytes = bavail;
34295 +       AuDbg("b%d\n", mfs->mfs_bindex);
34296 +       au_kfree_rcu(st);
34297 +}
34298 +
34299 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34300 +{
34301 +       int err;
34302 +       struct dentry *parent;
34303 +       struct super_block *sb;
34304 +       struct au_wbr_mfs *mfs;
34305 +
34306 +       err = au_wbr_create_exp(dentry);
34307 +       if (err >= 0)
34308 +               goto out;
34309 +
34310 +       sb = dentry->d_sb;
34311 +       parent = NULL;
34312 +       if (au_ftest_wbr(flags, PARENT))
34313 +               parent = dget_parent(dentry);
34314 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34315 +       mutex_lock(&mfs->mfs_lock);
34316 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34317 +           || mfs->mfs_bindex < 0
34318 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34319 +               au_mfs(dentry, parent);
34320 +       mutex_unlock(&mfs->mfs_lock);
34321 +       err = mfs->mfs_bindex;
34322 +       dput(parent);
34323 +
34324 +       if (err >= 0)
34325 +               err = au_wbr_nonopq(dentry, err);
34326 +
34327 +out:
34328 +       AuDbg("b%d\n", err);
34329 +       return err;
34330 +}
34331 +
34332 +static int au_wbr_create_init_mfs(struct super_block *sb)
34333 +{
34334 +       struct au_wbr_mfs *mfs;
34335 +
34336 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34337 +       mutex_init(&mfs->mfs_lock);
34338 +       mfs->mfs_jiffy = 0;
34339 +       mfs->mfs_bindex = -EROFS;
34340 +
34341 +       return 0;
34342 +}
34343 +
34344 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34345 +{
34346 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34347 +       return 0;
34348 +}
34349 +
34350 +/* ---------------------------------------------------------------------- */
34351 +
34352 +/* top down regardless parent, and then mfs */
34353 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34354 +                              unsigned int flags __maybe_unused)
34355 +{
34356 +       int err;
34357 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34358 +       unsigned long long watermark;
34359 +       struct super_block *sb;
34360 +       struct au_wbr_mfs *mfs;
34361 +       struct au_branch *br;
34362 +       struct dentry *parent;
34363 +
34364 +       sb = dentry->d_sb;
34365 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34366 +       mutex_lock(&mfs->mfs_lock);
34367 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34368 +           || mfs->mfs_bindex < 0)
34369 +               au_mfs(dentry, /*parent*/NULL);
34370 +       watermark = mfs->mfsrr_watermark;
34371 +       bmfs = mfs->mfs_bindex;
34372 +       mutex_unlock(&mfs->mfs_lock);
34373 +
34374 +       /* another style of au_wbr_create_exp() */
34375 +       bwh = au_dbwh(dentry);
34376 +       parent = dget_parent(dentry);
34377 +       btail = au_dbtaildir(parent);
34378 +       if (bwh >= 0 && bwh < btail)
34379 +               btail = bwh;
34380 +
34381 +       err = au_wbr_nonopq(dentry, btail);
34382 +       if (unlikely(err < 0))
34383 +               goto out;
34384 +       btail = err;
34385 +       bfound = -1;
34386 +       for (bindex = 0; bindex <= btail; bindex++) {
34387 +               br = au_sbr(sb, bindex);
34388 +               if (au_br_rdonly(br))
34389 +                       continue;
34390 +               if (br->br_wbr->wbr_bytes > watermark) {
34391 +                       bfound = bindex;
34392 +                       break;
34393 +               }
34394 +       }
34395 +       err = bfound;
34396 +       if (err < 0)
34397 +               err = bmfs;
34398 +
34399 +out:
34400 +       dput(parent);
34401 +       AuDbg("b%d\n", err);
34402 +       return err;
34403 +}
34404 +
34405 +/* ---------------------------------------------------------------------- */
34406 +
34407 +/* most free space and then round robin */
34408 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34409 +{
34410 +       int err;
34411 +       struct au_wbr_mfs *mfs;
34412 +
34413 +       err = au_wbr_create_mfs(dentry, flags);
34414 +       if (err >= 0) {
34415 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34416 +               mutex_lock(&mfs->mfs_lock);
34417 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34418 +                       err = au_wbr_create_rr(dentry, flags);
34419 +               mutex_unlock(&mfs->mfs_lock);
34420 +       }
34421 +
34422 +       AuDbg("b%d\n", err);
34423 +       return err;
34424 +}
34425 +
34426 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34427 +{
34428 +       int err;
34429 +
34430 +       au_wbr_create_init_mfs(sb); /* ignore */
34431 +       err = au_wbr_create_init_rr(sb);
34432 +
34433 +       return err;
34434 +}
34435 +
34436 +/* ---------------------------------------------------------------------- */
34437 +
34438 +/* top down parent and most free space */
34439 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34440 +{
34441 +       int err, e2;
34442 +       unsigned long long b;
34443 +       aufs_bindex_t bindex, btop, bbot;
34444 +       struct super_block *sb;
34445 +       struct dentry *parent, *h_parent;
34446 +       struct au_branch *br;
34447 +
34448 +       err = au_wbr_create_tdp(dentry, flags);
34449 +       if (unlikely(err < 0))
34450 +               goto out;
34451 +       parent = dget_parent(dentry);
34452 +       btop = au_dbtop(parent);
34453 +       bbot = au_dbtaildir(parent);
34454 +       if (btop == bbot)
34455 +               goto out_parent; /* success */
34456 +
34457 +       e2 = au_wbr_create_mfs(dentry, flags);
34458 +       if (e2 < 0)
34459 +               goto out_parent; /* success */
34460 +
34461 +       /* when the available size is equal, select upper one */
34462 +       sb = dentry->d_sb;
34463 +       br = au_sbr(sb, err);
34464 +       b = br->br_wbr->wbr_bytes;
34465 +       AuDbg("b%d, %llu\n", err, b);
34466 +
34467 +       for (bindex = btop; bindex <= bbot; bindex++) {
34468 +               h_parent = au_h_dptr(parent, bindex);
34469 +               if (!h_parent || d_is_negative(h_parent))
34470 +                       continue;
34471 +
34472 +               br = au_sbr(sb, bindex);
34473 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34474 +                       b = br->br_wbr->wbr_bytes;
34475 +                       err = bindex;
34476 +                       AuDbg("b%d, %llu\n", err, b);
34477 +               }
34478 +       }
34479 +
34480 +       if (err >= 0)
34481 +               err = au_wbr_nonopq(dentry, err);
34482 +
34483 +out_parent:
34484 +       dput(parent);
34485 +out:
34486 +       AuDbg("b%d\n", err);
34487 +       return err;
34488 +}
34489 +
34490 +/* ---------------------------------------------------------------------- */
34491 +
34492 +/*
34493 + * - top down parent
34494 + * - most free space with parent
34495 + * - most free space round-robin regardless parent
34496 + */
34497 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34498 +{
34499 +       int err;
34500 +       unsigned long long watermark;
34501 +       struct super_block *sb;
34502 +       struct au_branch *br;
34503 +       struct au_wbr_mfs *mfs;
34504 +
34505 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34506 +       if (unlikely(err < 0))
34507 +               goto out;
34508 +
34509 +       sb = dentry->d_sb;
34510 +       br = au_sbr(sb, err);
34511 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34512 +       mutex_lock(&mfs->mfs_lock);
34513 +       watermark = mfs->mfsrr_watermark;
34514 +       mutex_unlock(&mfs->mfs_lock);
34515 +       if (br->br_wbr->wbr_bytes < watermark)
34516 +               /* regardless the parent dir */
34517 +               err = au_wbr_create_mfsrr(dentry, flags);
34518 +
34519 +out:
34520 +       AuDbg("b%d\n", err);
34521 +       return err;
34522 +}
34523 +
34524 +/* ---------------------------------------------------------------------- */
34525 +
34526 +/* policies for copyup */
34527 +
34528 +/* top down parent */
34529 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34530 +{
34531 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34532 +}
34533 +
34534 +/* bottom up parent */
34535 +static int au_wbr_copyup_bup(struct dentry *dentry)
34536 +{
34537 +       int err;
34538 +       aufs_bindex_t bindex, btop;
34539 +       struct dentry *parent, *h_parent;
34540 +       struct super_block *sb;
34541 +
34542 +       err = -EROFS;
34543 +       sb = dentry->d_sb;
34544 +       parent = dget_parent(dentry);
34545 +       btop = au_dbtop(parent);
34546 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34547 +               h_parent = au_h_dptr(parent, bindex);
34548 +               if (!h_parent || d_is_negative(h_parent))
34549 +                       continue;
34550 +
34551 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34552 +                       err = bindex;
34553 +                       break;
34554 +               }
34555 +       }
34556 +       dput(parent);
34557 +
34558 +       /* bottom up here */
34559 +       if (unlikely(err < 0))
34560 +               err = au_wbr_bu(sb, btop - 1);
34561 +
34562 +       AuDbg("b%d\n", err);
34563 +       return err;
34564 +}
34565 +
34566 +/* bottom up */
34567 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34568 +{
34569 +       int err;
34570 +
34571 +       err = au_wbr_bu(dentry->d_sb, btop);
34572 +       AuDbg("b%d\n", err);
34573 +       if (err > btop)
34574 +               err = au_wbr_nonopq(dentry, err);
34575 +
34576 +       AuDbg("b%d\n", err);
34577 +       return err;
34578 +}
34579 +
34580 +static int au_wbr_copyup_bu(struct dentry *dentry)
34581 +{
34582 +       int err;
34583 +       aufs_bindex_t btop;
34584 +
34585 +       btop = au_dbtop(dentry);
34586 +       err = au_wbr_do_copyup_bu(dentry, btop);
34587 +       return err;
34588 +}
34589 +
34590 +/* ---------------------------------------------------------------------- */
34591 +
34592 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34593 +       [AuWbrCopyup_TDP] = {
34594 +               .copyup = au_wbr_copyup_tdp
34595 +       },
34596 +       [AuWbrCopyup_BUP] = {
34597 +               .copyup = au_wbr_copyup_bup
34598 +       },
34599 +       [AuWbrCopyup_BU] = {
34600 +               .copyup = au_wbr_copyup_bu
34601 +       }
34602 +};
34603 +
34604 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34605 +       [AuWbrCreate_TDP] = {
34606 +               .create = au_wbr_create_tdp
34607 +       },
34608 +       [AuWbrCreate_RR] = {
34609 +               .create = au_wbr_create_rr,
34610 +               .init   = au_wbr_create_init_rr
34611 +       },
34612 +       [AuWbrCreate_MFS] = {
34613 +               .create = au_wbr_create_mfs,
34614 +               .init   = au_wbr_create_init_mfs,
34615 +               .fin    = au_wbr_create_fin_mfs
34616 +       },
34617 +       [AuWbrCreate_MFSV] = {
34618 +               .create = au_wbr_create_mfs,
34619 +               .init   = au_wbr_create_init_mfs,
34620 +               .fin    = au_wbr_create_fin_mfs
34621 +       },
34622 +       [AuWbrCreate_MFSRR] = {
34623 +               .create = au_wbr_create_mfsrr,
34624 +               .init   = au_wbr_create_init_mfsrr,
34625 +               .fin    = au_wbr_create_fin_mfs
34626 +       },
34627 +       [AuWbrCreate_MFSRRV] = {
34628 +               .create = au_wbr_create_mfsrr,
34629 +               .init   = au_wbr_create_init_mfsrr,
34630 +               .fin    = au_wbr_create_fin_mfs
34631 +       },
34632 +       [AuWbrCreate_TDMFS] = {
34633 +               .create = au_wbr_create_tdmfs,
34634 +               .init   = au_wbr_create_init_mfs,
34635 +               .fin    = au_wbr_create_fin_mfs
34636 +       },
34637 +       [AuWbrCreate_TDMFSV] = {
34638 +               .create = au_wbr_create_tdmfs,
34639 +               .init   = au_wbr_create_init_mfs,
34640 +               .fin    = au_wbr_create_fin_mfs
34641 +       },
34642 +       [AuWbrCreate_PMFS] = {
34643 +               .create = au_wbr_create_pmfs,
34644 +               .init   = au_wbr_create_init_mfs,
34645 +               .fin    = au_wbr_create_fin_mfs
34646 +       },
34647 +       [AuWbrCreate_PMFSV] = {
34648 +               .create = au_wbr_create_pmfs,
34649 +               .init   = au_wbr_create_init_mfs,
34650 +               .fin    = au_wbr_create_fin_mfs
34651 +       },
34652 +       [AuWbrCreate_PMFSRR] = {
34653 +               .create = au_wbr_create_pmfsrr,
34654 +               .init   = au_wbr_create_init_mfsrr,
34655 +               .fin    = au_wbr_create_fin_mfs
34656 +       },
34657 +       [AuWbrCreate_PMFSRRV] = {
34658 +               .create = au_wbr_create_pmfsrr,
34659 +               .init   = au_wbr_create_init_mfsrr,
34660 +               .fin    = au_wbr_create_fin_mfs
34661 +       }
34662 +};
34663 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34664 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34665 +++ linux/fs/aufs/whout.c       2021-02-24 13:33:42.751013936 +0100
34666 @@ -0,0 +1,1062 @@
34667 +// SPDX-License-Identifier: GPL-2.0
34668 +/*
34669 + * Copyright (C) 2005-2020 Junjiro R. Okajima
34670 + *
34671 + * This program, aufs is free software; you can redistribute it and/or modify
34672 + * it under the terms of the GNU General Public License as published by
34673 + * the Free Software Foundation; either version 2 of the License, or
34674 + * (at your option) any later version.
34675 + *
34676 + * This program is distributed in the hope that it will be useful,
34677 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34678 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34679 + * GNU General Public License for more details.
34680 + *
34681 + * You should have received a copy of the GNU General Public License
34682 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34683 + */
34684 +
34685 +/*
34686 + * whiteout for logical deletion and opaque directory
34687 + */
34688 +
34689 +#include "aufs.h"
34690 +
34691 +#define WH_MASK                        0444
34692 +
34693 +/*
34694 + * If a directory contains this file, then it is opaque.  We start with the
34695 + * .wh. flag so that it is blocked by lookup.
34696 + */
34697 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34698 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34699 +
34700 +/*
34701 + * generate whiteout name, which is NOT terminated by NULL.
34702 + * @name: original d_name.name
34703 + * @len: original d_name.len
34704 + * @wh: whiteout qstr
34705 + * returns zero when succeeds, otherwise error.
34706 + * succeeded value as wh->name should be freed by kfree().
34707 + */
34708 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34709 +{
34710 +       char *p;
34711 +
34712 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34713 +               return -ENAMETOOLONG;
34714 +
34715 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34716 +       p = kmalloc(wh->len, GFP_NOFS);
34717 +       wh->name = p;
34718 +       if (p) {
34719 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34720 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34721 +               /* smp_mb(); */
34722 +               return 0;
34723 +       }
34724 +       return -ENOMEM;
34725 +}
34726 +
34727 +/* ---------------------------------------------------------------------- */
34728 +
34729 +/*
34730 + * test if the @wh_name exists under @h_parent.
34731 + * @try_sio specifies the necessary of super-io.
34732 + */
34733 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34734 +{
34735 +       int err;
34736 +       struct dentry *wh_dentry;
34737 +
34738 +       if (!try_sio)
34739 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34740 +       else
34741 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34742 +       err = PTR_ERR(wh_dentry);
34743 +       if (IS_ERR(wh_dentry)) {
34744 +               if (err == -ENAMETOOLONG)
34745 +                       err = 0;
34746 +               goto out;
34747 +       }
34748 +
34749 +       err = 0;
34750 +       if (d_is_negative(wh_dentry))
34751 +               goto out_wh; /* success */
34752 +
34753 +       err = 1;
34754 +       if (d_is_reg(wh_dentry))
34755 +               goto out_wh; /* success */
34756 +
34757 +       err = -EIO;
34758 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34759 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34760 +
34761 +out_wh:
34762 +       dput(wh_dentry);
34763 +out:
34764 +       return err;
34765 +}
34766 +
34767 +/*
34768 + * test if the @h_dentry sets opaque or not.
34769 + */
34770 +int au_diropq_test(struct dentry *h_dentry)
34771 +{
34772 +       int err;
34773 +       struct inode *h_dir;
34774 +
34775 +       h_dir = d_inode(h_dentry);
34776 +       err = au_wh_test(h_dentry, &diropq_name,
34777 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34778 +       return err;
34779 +}
34780 +
34781 +/*
34782 + * returns a negative dentry whose name is unique and temporary.
34783 + */
34784 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34785 +                            struct qstr *prefix)
34786 +{
34787 +       struct dentry *dentry;
34788 +       int i;
34789 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34790 +               *name, *p;
34791 +       /* strict atomic_t is unnecessary here */
34792 +       static unsigned short cnt;
34793 +       struct qstr qs;
34794 +
34795 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34796 +
34797 +       name = defname;
34798 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34799 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34800 +               dentry = ERR_PTR(-ENAMETOOLONG);
34801 +               if (unlikely(qs.len > NAME_MAX))
34802 +                       goto out;
34803 +               dentry = ERR_PTR(-ENOMEM);
34804 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34805 +               if (unlikely(!name))
34806 +                       goto out;
34807 +       }
34808 +
34809 +       /* doubly whiteout-ed */
34810 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34811 +       p = name + AUFS_WH_PFX_LEN * 2;
34812 +       memcpy(p, prefix->name, prefix->len);
34813 +       p += prefix->len;
34814 +       *p++ = '.';
34815 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34816 +
34817 +       qs.name = name;
34818 +       for (i = 0; i < 3; i++) {
34819 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34820 +               dentry = au_sio_lkup_one(&qs, h_parent);
34821 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34822 +                       goto out_name;
34823 +               dput(dentry);
34824 +       }
34825 +       /* pr_warn("could not get random name\n"); */
34826 +       dentry = ERR_PTR(-EEXIST);
34827 +       AuDbg("%.*s\n", AuLNPair(&qs));
34828 +       BUG();
34829 +
34830 +out_name:
34831 +       if (name != defname)
34832 +               au_kfree_try_rcu(name);
34833 +out:
34834 +       AuTraceErrPtr(dentry);
34835 +       return dentry;
34836 +}
34837 +
34838 +/*
34839 + * rename the @h_dentry on @br to the whiteouted temporary name.
34840 + */
34841 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34842 +{
34843 +       int err;
34844 +       struct path h_path = {
34845 +               .mnt = au_br_mnt(br)
34846 +       };
34847 +       struct inode *h_dir, *delegated;
34848 +       struct dentry *h_parent;
34849 +
34850 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34851 +       h_dir = d_inode(h_parent);
34852 +       IMustLock(h_dir);
34853 +
34854 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34855 +       err = PTR_ERR(h_path.dentry);
34856 +       if (IS_ERR(h_path.dentry))
34857 +               goto out;
34858 +
34859 +       /* under the same dir, no need to lock_rename() */
34860 +       delegated = NULL;
34861 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34862 +                          /*flags*/0);
34863 +       AuTraceErr(err);
34864 +       if (unlikely(err == -EWOULDBLOCK)) {
34865 +               pr_warn("cannot retry for NFSv4 delegation"
34866 +                       " for an internal rename\n");
34867 +               iput(delegated);
34868 +       }
34869 +       dput(h_path.dentry);
34870 +
34871 +out:
34872 +       AuTraceErr(err);
34873 +       return err;
34874 +}
34875 +
34876 +/* ---------------------------------------------------------------------- */
34877 +/*
34878 + * functions for removing a whiteout
34879 + */
34880 +
34881 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34882 +{
34883 +       int err, force;
34884 +       struct inode *delegated;
34885 +
34886 +       /*
34887 +        * forces superio when the dir has a sticky bit.
34888 +        * this may be a violation of unix fs semantics.
34889 +        */
34890 +       force = (h_dir->i_mode & S_ISVTX)
34891 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34892 +       delegated = NULL;
34893 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
34894 +       if (unlikely(err == -EWOULDBLOCK)) {
34895 +               pr_warn("cannot retry for NFSv4 delegation"
34896 +                       " for an internal unlink\n");
34897 +               iput(delegated);
34898 +       }
34899 +       return err;
34900 +}
34901 +
34902 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34903 +                       struct dentry *dentry)
34904 +{
34905 +       int err;
34906 +
34907 +       err = do_unlink_wh(h_dir, h_path);
34908 +       if (!err && dentry)
34909 +               au_set_dbwh(dentry, -1);
34910 +
34911 +       return err;
34912 +}
34913 +
34914 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34915 +                         struct au_branch *br)
34916 +{
34917 +       int err;
34918 +       struct path h_path = {
34919 +               .mnt = au_br_mnt(br)
34920 +       };
34921 +
34922 +       err = 0;
34923 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
34924 +       if (IS_ERR(h_path.dentry))
34925 +               err = PTR_ERR(h_path.dentry);
34926 +       else {
34927 +               if (d_is_reg(h_path.dentry))
34928 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
34929 +               dput(h_path.dentry);
34930 +       }
34931 +
34932 +       return err;
34933 +}
34934 +
34935 +/* ---------------------------------------------------------------------- */
34936 +/*
34937 + * initialize/clean whiteout for a branch
34938 + */
34939 +
34940 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34941 +                       const int isdir)
34942 +{
34943 +       int err;
34944 +       struct inode *delegated;
34945 +
34946 +       if (d_is_negative(whpath->dentry))
34947 +               return;
34948 +
34949 +       if (isdir)
34950 +               err = vfsub_rmdir(h_dir, whpath);
34951 +       else {
34952 +               delegated = NULL;
34953 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34954 +               if (unlikely(err == -EWOULDBLOCK)) {
34955 +                       pr_warn("cannot retry for NFSv4 delegation"
34956 +                               " for an internal unlink\n");
34957 +                       iput(delegated);
34958 +               }
34959 +       }
34960 +       if (unlikely(err))
34961 +               pr_warn("failed removing %pd (%d), ignored.\n",
34962 +                       whpath->dentry, err);
34963 +}
34964 +
34965 +static int test_linkable(struct dentry *h_root)
34966 +{
34967 +       struct inode *h_dir = d_inode(h_root);
34968 +
34969 +       if (h_dir->i_op->link)
34970 +               return 0;
34971 +
34972 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34973 +              h_root, au_sbtype(h_root->d_sb));
34974 +       return -ENOSYS; /* the branch doesn't have its ->link() */
34975 +}
34976 +
34977 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34978 +static int au_whdir(struct inode *h_dir, struct path *path)
34979 +{
34980 +       int err;
34981 +
34982 +       err = -EEXIST;
34983 +       if (d_is_negative(path->dentry)) {
34984 +               int mode = 0700;
34985 +
34986 +               if (au_test_nfs(path->dentry->d_sb))
34987 +                       mode |= 0111;
34988 +               err = vfsub_mkdir(h_dir, path, mode);
34989 +       } else if (d_is_dir(path->dentry))
34990 +               err = 0;
34991 +       else
34992 +               pr_err("unknown %pd exists\n", path->dentry);
34993 +
34994 +       return err;
34995 +}
34996 +
34997 +struct au_wh_base {
34998 +       const struct qstr *name;
34999 +       struct dentry *dentry;
35000 +};
35001 +
35002 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35003 +                         struct path *h_path)
35004 +{
35005 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35006 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35007 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35008 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35009 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35010 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35011 +}
35012 +
35013 +/*
35014 + * returns tri-state,
35015 + * minus: error, caller should print the message
35016 + * zero: success
35017 + * plus: error, caller should NOT print the message
35018 + */
35019 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35020 +                               int do_plink, struct au_wh_base base[],
35021 +                               struct path *h_path)
35022 +{
35023 +       int err;
35024 +       struct inode *h_dir;
35025 +
35026 +       h_dir = d_inode(h_root);
35027 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35028 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35029 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35030 +       if (do_plink) {
35031 +               err = test_linkable(h_root);
35032 +               if (unlikely(err)) {
35033 +                       err = 1;
35034 +                       goto out;
35035 +               }
35036 +
35037 +               err = au_whdir(h_dir, h_path);
35038 +               if (unlikely(err))
35039 +                       goto out;
35040 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35041 +       } else
35042 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35043 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35044 +       err = au_whdir(h_dir, h_path);
35045 +       if (unlikely(err))
35046 +               goto out;
35047 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35048 +
35049 +out:
35050 +       return err;
35051 +}
35052 +
35053 +/*
35054 + * for the moment, aufs supports the branch filesystem which does not support
35055 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35056 + * copyup failed. finally, such filesystem will not be used as the writable
35057 + * branch.
35058 + *
35059 + * returns tri-state, see above.
35060 + */
35061 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35062 +                        int do_plink, struct au_wh_base base[],
35063 +                        struct path *h_path)
35064 +{
35065 +       int err;
35066 +       struct inode *h_dir;
35067 +
35068 +       WbrWhMustWriteLock(wbr);
35069 +
35070 +       err = test_linkable(h_root);
35071 +       if (unlikely(err)) {
35072 +               err = 1;
35073 +               goto out;
35074 +       }
35075 +
35076 +       /*
35077 +        * todo: should this create be done in /sbin/mount.aufs helper?
35078 +        */
35079 +       err = -EEXIST;
35080 +       h_dir = d_inode(h_root);
35081 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35082 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35083 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35084 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35085 +               err = 0;
35086 +       else
35087 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35088 +       if (unlikely(err))
35089 +               goto out;
35090 +
35091 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35092 +       if (do_plink) {
35093 +               err = au_whdir(h_dir, h_path);
35094 +               if (unlikely(err))
35095 +                       goto out;
35096 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35097 +       } else
35098 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35099 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35100 +
35101 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35102 +       err = au_whdir(h_dir, h_path);
35103 +       if (unlikely(err))
35104 +               goto out;
35105 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35106 +
35107 +out:
35108 +       return err;
35109 +}
35110 +
35111 +/*
35112 + * initialize the whiteout base file/dir for @br.
35113 + */
35114 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35115 +{
35116 +       int err, i;
35117 +       const unsigned char do_plink
35118 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35119 +       struct inode *h_dir;
35120 +       struct path path = br->br_path;
35121 +       struct dentry *h_root = path.dentry;
35122 +       struct au_wbr *wbr = br->br_wbr;
35123 +       static const struct qstr base_name[] = {
35124 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35125 +                                         sizeof(AUFS_BASE_NAME) - 1),
35126 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35127 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35128 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35129 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35130 +       };
35131 +       struct au_wh_base base[] = {
35132 +               [AuBrWh_BASE] = {
35133 +                       .name   = base_name + AuBrWh_BASE,
35134 +                       .dentry = NULL
35135 +               },
35136 +               [AuBrWh_PLINK] = {
35137 +                       .name   = base_name + AuBrWh_PLINK,
35138 +                       .dentry = NULL
35139 +               },
35140 +               [AuBrWh_ORPH] = {
35141 +                       .name   = base_name + AuBrWh_ORPH,
35142 +                       .dentry = NULL
35143 +               }
35144 +       };
35145 +
35146 +       if (wbr)
35147 +               WbrWhMustWriteLock(wbr);
35148 +
35149 +       for (i = 0; i < AuBrWh_Last; i++) {
35150 +               /* doubly whiteouted */
35151 +               struct dentry *d;
35152 +
35153 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35154 +               err = PTR_ERR(d);
35155 +               if (IS_ERR(d))
35156 +                       goto out;
35157 +
35158 +               base[i].dentry = d;
35159 +               AuDebugOn(wbr
35160 +                         && wbr->wbr_wh[i]
35161 +                         && wbr->wbr_wh[i] != base[i].dentry);
35162 +       }
35163 +
35164 +       if (wbr)
35165 +               for (i = 0; i < AuBrWh_Last; i++) {
35166 +                       dput(wbr->wbr_wh[i]);
35167 +                       wbr->wbr_wh[i] = NULL;
35168 +               }
35169 +
35170 +       err = 0;
35171 +       if (!au_br_writable(br->br_perm)) {
35172 +               h_dir = d_inode(h_root);
35173 +               au_wh_init_ro(h_dir, base, &path);
35174 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35175 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35176 +               if (err > 0)
35177 +                       goto out;
35178 +               else if (err)
35179 +                       goto out_err;
35180 +       } else {
35181 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35182 +               if (err > 0)
35183 +                       goto out;
35184 +               else if (err)
35185 +                       goto out_err;
35186 +       }
35187 +       goto out; /* success */
35188 +
35189 +out_err:
35190 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35191 +              err, h_root, au_sbtype(h_root->d_sb));
35192 +out:
35193 +       for (i = 0; i < AuBrWh_Last; i++)
35194 +               dput(base[i].dentry);
35195 +       return err;
35196 +}
35197 +
35198 +/* ---------------------------------------------------------------------- */
35199 +/*
35200 + * whiteouts are all hard-linked usually.
35201 + * when its link count reaches a ceiling, we create a new whiteout base
35202 + * asynchronously.
35203 + */
35204 +
35205 +struct reinit_br_wh {
35206 +       struct super_block *sb;
35207 +       struct au_branch *br;
35208 +};
35209 +
35210 +static void reinit_br_wh(void *arg)
35211 +{
35212 +       int err;
35213 +       aufs_bindex_t bindex;
35214 +       struct path h_path;
35215 +       struct reinit_br_wh *a = arg;
35216 +       struct au_wbr *wbr;
35217 +       struct inode *dir, *delegated;
35218 +       struct dentry *h_root;
35219 +       struct au_hinode *hdir;
35220 +
35221 +       err = 0;
35222 +       wbr = a->br->br_wbr;
35223 +       /* big aufs lock */
35224 +       si_noflush_write_lock(a->sb);
35225 +       if (!au_br_writable(a->br->br_perm))
35226 +               goto out;
35227 +       bindex = au_br_index(a->sb, a->br->br_id);
35228 +       if (unlikely(bindex < 0))
35229 +               goto out;
35230 +
35231 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35232 +       dir = d_inode(a->sb->s_root);
35233 +       hdir = au_hi(dir, bindex);
35234 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35235 +       AuDebugOn(h_root != au_br_dentry(a->br));
35236 +
35237 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35238 +       wbr_wh_write_lock(wbr);
35239 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35240 +                         h_root, a->br);
35241 +       if (!err) {
35242 +               h_path.dentry = wbr->wbr_whbase;
35243 +               h_path.mnt = au_br_mnt(a->br);
35244 +               delegated = NULL;
35245 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35246 +                                  /*force*/0);
35247 +               if (unlikely(err == -EWOULDBLOCK)) {
35248 +                       pr_warn("cannot retry for NFSv4 delegation"
35249 +                               " for an internal unlink\n");
35250 +                       iput(delegated);
35251 +               }
35252 +       } else {
35253 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35254 +               err = 0;
35255 +       }
35256 +       dput(wbr->wbr_whbase);
35257 +       wbr->wbr_whbase = NULL;
35258 +       if (!err)
35259 +               err = au_wh_init(a->br, a->sb);
35260 +       wbr_wh_write_unlock(wbr);
35261 +       au_hn_inode_unlock(hdir);
35262 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35263 +       if (!err)
35264 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35265 +
35266 +out:
35267 +       if (wbr)
35268 +               atomic_dec(&wbr->wbr_wh_running);
35269 +       au_lcnt_dec(&a->br->br_count);
35270 +       si_write_unlock(a->sb);
35271 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35272 +       au_kfree_rcu(a);
35273 +       if (unlikely(err))
35274 +               AuIOErr("err %d\n", err);
35275 +}
35276 +
35277 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35278 +{
35279 +       int do_dec, wkq_err;
35280 +       struct reinit_br_wh *arg;
35281 +
35282 +       do_dec = 1;
35283 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35284 +               goto out;
35285 +
35286 +       /* ignore ENOMEM */
35287 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35288 +       if (arg) {
35289 +               /*
35290 +                * dec(wh_running), kfree(arg) and dec(br_count)
35291 +                * in reinit function
35292 +                */
35293 +               arg->sb = sb;
35294 +               arg->br = br;
35295 +               au_lcnt_inc(&br->br_count);
35296 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35297 +               if (unlikely(wkq_err)) {
35298 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35299 +                       au_lcnt_dec(&br->br_count);
35300 +                       au_kfree_rcu(arg);
35301 +               }
35302 +               do_dec = 0;
35303 +       }
35304 +
35305 +out:
35306 +       if (do_dec)
35307 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35308 +}
35309 +
35310 +/* ---------------------------------------------------------------------- */
35311 +
35312 +/*
35313 + * create the whiteout @wh.
35314 + */
35315 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35316 +                            struct dentry *wh)
35317 +{
35318 +       int err;
35319 +       struct path h_path = {
35320 +               .dentry = wh
35321 +       };
35322 +       struct au_branch *br;
35323 +       struct au_wbr *wbr;
35324 +       struct dentry *h_parent;
35325 +       struct inode *h_dir, *delegated;
35326 +
35327 +       h_parent = wh->d_parent; /* dir inode is locked */
35328 +       h_dir = d_inode(h_parent);
35329 +       IMustLock(h_dir);
35330 +
35331 +       br = au_sbr(sb, bindex);
35332 +       h_path.mnt = au_br_mnt(br);
35333 +       wbr = br->br_wbr;
35334 +       wbr_wh_read_lock(wbr);
35335 +       if (wbr->wbr_whbase) {
35336 +               delegated = NULL;
35337 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35338 +               if (unlikely(err == -EWOULDBLOCK)) {
35339 +                       pr_warn("cannot retry for NFSv4 delegation"
35340 +                               " for an internal link\n");
35341 +                       iput(delegated);
35342 +               }
35343 +               if (!err || err != -EMLINK)
35344 +                       goto out;
35345 +
35346 +               /* link count full. re-initialize br_whbase. */
35347 +               kick_reinit_br_wh(sb, br);
35348 +       }
35349 +
35350 +       /* return this error in this context */
35351 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35352 +       if (!err)
35353 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35354 +
35355 +out:
35356 +       wbr_wh_read_unlock(wbr);
35357 +       return err;
35358 +}
35359 +
35360 +/* ---------------------------------------------------------------------- */
35361 +
35362 +/*
35363 + * create or remove the diropq.
35364 + */
35365 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35366 +                               unsigned int flags)
35367 +{
35368 +       struct dentry *opq_dentry, *h_dentry;
35369 +       struct super_block *sb;
35370 +       struct au_branch *br;
35371 +       int err;
35372 +
35373 +       sb = dentry->d_sb;
35374 +       br = au_sbr(sb, bindex);
35375 +       h_dentry = au_h_dptr(dentry, bindex);
35376 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35377 +       if (IS_ERR(opq_dentry))
35378 +               goto out;
35379 +
35380 +       if (au_ftest_diropq(flags, CREATE)) {
35381 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35382 +               if (!err) {
35383 +                       au_set_dbdiropq(dentry, bindex);
35384 +                       goto out; /* success */
35385 +               }
35386 +       } else {
35387 +               struct path tmp = {
35388 +                       .dentry = opq_dentry,
35389 +                       .mnt    = au_br_mnt(br)
35390 +               };
35391 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35392 +               if (!err)
35393 +                       au_set_dbdiropq(dentry, -1);
35394 +       }
35395 +       dput(opq_dentry);
35396 +       opq_dentry = ERR_PTR(err);
35397 +
35398 +out:
35399 +       return opq_dentry;
35400 +}
35401 +
35402 +struct do_diropq_args {
35403 +       struct dentry **errp;
35404 +       struct dentry *dentry;
35405 +       aufs_bindex_t bindex;
35406 +       unsigned int flags;
35407 +};
35408 +
35409 +static void call_do_diropq(void *args)
35410 +{
35411 +       struct do_diropq_args *a = args;
35412 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35413 +}
35414 +
35415 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35416 +                            unsigned int flags)
35417 +{
35418 +       struct dentry *diropq, *h_dentry;
35419 +
35420 +       h_dentry = au_h_dptr(dentry, bindex);
35421 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35422 +               diropq = do_diropq(dentry, bindex, flags);
35423 +       else {
35424 +               int wkq_err;
35425 +               struct do_diropq_args args = {
35426 +                       .errp           = &diropq,
35427 +                       .dentry         = dentry,
35428 +                       .bindex         = bindex,
35429 +                       .flags          = flags
35430 +               };
35431 +
35432 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35433 +               if (unlikely(wkq_err))
35434 +                       diropq = ERR_PTR(wkq_err);
35435 +       }
35436 +
35437 +       return diropq;
35438 +}
35439 +
35440 +/* ---------------------------------------------------------------------- */
35441 +
35442 +/*
35443 + * lookup whiteout dentry.
35444 + * @h_parent: lower parent dentry which must exist and be locked
35445 + * @base_name: name of dentry which will be whiteouted
35446 + * returns dentry for whiteout.
35447 + */
35448 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35449 +                         struct au_branch *br)
35450 +{
35451 +       int err;
35452 +       struct qstr wh_name;
35453 +       struct dentry *wh_dentry;
35454 +
35455 +       err = au_wh_name_alloc(&wh_name, base_name);
35456 +       wh_dentry = ERR_PTR(err);
35457 +       if (!err) {
35458 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35459 +               au_kfree_try_rcu(wh_name.name);
35460 +       }
35461 +       return wh_dentry;
35462 +}
35463 +
35464 +/*
35465 + * link/create a whiteout for @dentry on @bindex.
35466 + */
35467 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35468 +                           struct dentry *h_parent)
35469 +{
35470 +       struct dentry *wh_dentry;
35471 +       struct super_block *sb;
35472 +       int err;
35473 +
35474 +       sb = dentry->d_sb;
35475 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35476 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35477 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35478 +               if (!err) {
35479 +                       au_set_dbwh(dentry, bindex);
35480 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35481 +               } else {
35482 +                       dput(wh_dentry);
35483 +                       wh_dentry = ERR_PTR(err);
35484 +               }
35485 +       }
35486 +
35487 +       return wh_dentry;
35488 +}
35489 +
35490 +/* ---------------------------------------------------------------------- */
35491 +
35492 +/* Delete all whiteouts in this directory on branch bindex. */
35493 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35494 +                          aufs_bindex_t bindex, struct au_branch *br)
35495 +{
35496 +       int err;
35497 +       unsigned long ul, n;
35498 +       struct qstr wh_name;
35499 +       char *p;
35500 +       struct hlist_head *head;
35501 +       struct au_vdir_wh *pos;
35502 +       struct au_vdir_destr *str;
35503 +
35504 +       err = -ENOMEM;
35505 +       p = (void *)__get_free_page(GFP_NOFS);
35506 +       wh_name.name = p;
35507 +       if (unlikely(!wh_name.name))
35508 +               goto out;
35509 +
35510 +       err = 0;
35511 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35512 +       p += AUFS_WH_PFX_LEN;
35513 +       n = whlist->nh_num;
35514 +       head = whlist->nh_head;
35515 +       for (ul = 0; !err && ul < n; ul++, head++) {
35516 +               hlist_for_each_entry(pos, head, wh_hash) {
35517 +                       if (pos->wh_bindex != bindex)
35518 +                               continue;
35519 +
35520 +                       str = &pos->wh_str;
35521 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35522 +                               memcpy(p, str->name, str->len);
35523 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35524 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35525 +                               if (!err)
35526 +                                       continue;
35527 +                               break;
35528 +                       }
35529 +                       AuIOErr("whiteout name too long %.*s\n",
35530 +                               str->len, str->name);
35531 +                       err = -EIO;
35532 +                       break;
35533 +               }
35534 +       }
35535 +       free_page((unsigned long)wh_name.name);
35536 +
35537 +out:
35538 +       return err;
35539 +}
35540 +
35541 +struct del_wh_children_args {
35542 +       int *errp;
35543 +       struct dentry *h_dentry;
35544 +       struct au_nhash *whlist;
35545 +       aufs_bindex_t bindex;
35546 +       struct au_branch *br;
35547 +};
35548 +
35549 +static void call_del_wh_children(void *args)
35550 +{
35551 +       struct del_wh_children_args *a = args;
35552 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35553 +}
35554 +
35555 +/* ---------------------------------------------------------------------- */
35556 +
35557 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35558 +{
35559 +       struct au_whtmp_rmdir *whtmp;
35560 +       int err;
35561 +       unsigned int rdhash;
35562 +
35563 +       SiMustAnyLock(sb);
35564 +
35565 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35566 +       if (unlikely(!whtmp)) {
35567 +               whtmp = ERR_PTR(-ENOMEM);
35568 +               goto out;
35569 +       }
35570 +
35571 +       /* no estimation for dir size */
35572 +       rdhash = au_sbi(sb)->si_rdhash;
35573 +       if (!rdhash)
35574 +               rdhash = AUFS_RDHASH_DEF;
35575 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35576 +       if (unlikely(err)) {
35577 +               au_kfree_rcu(whtmp);
35578 +               whtmp = ERR_PTR(err);
35579 +       }
35580 +
35581 +out:
35582 +       return whtmp;
35583 +}
35584 +
35585 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35586 +{
35587 +       if (whtmp->br)
35588 +               au_lcnt_dec(&whtmp->br->br_count);
35589 +       dput(whtmp->wh_dentry);
35590 +       iput(whtmp->dir);
35591 +       au_nhash_wh_free(&whtmp->whlist);
35592 +       au_kfree_rcu(whtmp);
35593 +}
35594 +
35595 +/*
35596 + * rmdir the whiteouted temporary named dir @h_dentry.
35597 + * @whlist: whiteouted children.
35598 + */
35599 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35600 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35601 +{
35602 +       int err;
35603 +       unsigned int h_nlink;
35604 +       struct path h_tmp;
35605 +       struct inode *wh_inode, *h_dir;
35606 +       struct au_branch *br;
35607 +
35608 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35609 +       IMustLock(h_dir);
35610 +
35611 +       br = au_sbr(dir->i_sb, bindex);
35612 +       wh_inode = d_inode(wh_dentry);
35613 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35614 +
35615 +       /*
35616 +        * someone else might change some whiteouts while we were sleeping.
35617 +        * it means this whlist may have an obsoleted entry.
35618 +        */
35619 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35620 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35621 +       else {
35622 +               int wkq_err;
35623 +               struct del_wh_children_args args = {
35624 +                       .errp           = &err,
35625 +                       .h_dentry       = wh_dentry,
35626 +                       .whlist         = whlist,
35627 +                       .bindex         = bindex,
35628 +                       .br             = br
35629 +               };
35630 +
35631 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35632 +               if (unlikely(wkq_err))
35633 +                       err = wkq_err;
35634 +       }
35635 +       inode_unlock(wh_inode);
35636 +
35637 +       if (!err) {
35638 +               h_tmp.dentry = wh_dentry;
35639 +               h_tmp.mnt = au_br_mnt(br);
35640 +               h_nlink = h_dir->i_nlink;
35641 +               err = vfsub_rmdir(h_dir, &h_tmp);
35642 +               /* some fs doesn't change the parent nlink in some cases */
35643 +               h_nlink -= h_dir->i_nlink;
35644 +       }
35645 +
35646 +       if (!err) {
35647 +               if (au_ibtop(dir) == bindex) {
35648 +                       /* todo: dir->i_mutex is necessary */
35649 +                       au_cpup_attr_timesizes(dir);
35650 +                       if (h_nlink)
35651 +                               vfsub_drop_nlink(dir);
35652 +               }
35653 +               return 0; /* success */
35654 +       }
35655 +
35656 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35657 +       return err;
35658 +}
35659 +
35660 +static void call_rmdir_whtmp(void *args)
35661 +{
35662 +       int err;
35663 +       aufs_bindex_t bindex;
35664 +       struct au_whtmp_rmdir *a = args;
35665 +       struct super_block *sb;
35666 +       struct dentry *h_parent;
35667 +       struct inode *h_dir;
35668 +       struct au_hinode *hdir;
35669 +
35670 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35671 +       /* inode_lock(a->dir); */
35672 +       err = -EROFS;
35673 +       sb = a->dir->i_sb;
35674 +       si_read_lock(sb, !AuLock_FLUSH);
35675 +       if (!au_br_writable(a->br->br_perm))
35676 +               goto out;
35677 +       bindex = au_br_index(sb, a->br->br_id);
35678 +       if (unlikely(bindex < 0))
35679 +               goto out;
35680 +
35681 +       err = -EIO;
35682 +       ii_write_lock_parent(a->dir);
35683 +       h_parent = dget_parent(a->wh_dentry);
35684 +       h_dir = d_inode(h_parent);
35685 +       hdir = au_hi(a->dir, bindex);
35686 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35687 +       if (unlikely(err))
35688 +               goto out_mnt;
35689 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35690 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35691 +                         a->br);
35692 +       if (!err)
35693 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35694 +       au_hn_inode_unlock(hdir);
35695 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35696 +
35697 +out_mnt:
35698 +       dput(h_parent);
35699 +       ii_write_unlock(a->dir);
35700 +out:
35701 +       /* inode_unlock(a->dir); */
35702 +       au_whtmp_rmdir_free(a);
35703 +       si_read_unlock(sb);
35704 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35705 +       if (unlikely(err))
35706 +               AuIOErr("err %d\n", err);
35707 +}
35708 +
35709 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35710 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35711 +{
35712 +       int wkq_err;
35713 +       struct super_block *sb;
35714 +
35715 +       IMustLock(dir);
35716 +
35717 +       /* all post-process will be done in do_rmdir_whtmp(). */
35718 +       sb = dir->i_sb;
35719 +       args->dir = au_igrab(dir);
35720 +       args->br = au_sbr(sb, bindex);
35721 +       au_lcnt_inc(&args->br->br_count);
35722 +       args->wh_dentry = dget(wh_dentry);
35723 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35724 +       if (unlikely(wkq_err)) {
35725 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35726 +               au_whtmp_rmdir_free(args);
35727 +       }
35728 +}
35729 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35730 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35731 +++ linux/fs/aufs/whout.h       2021-02-24 13:33:42.751013936 +0100
35732 @@ -0,0 +1,86 @@
35733 +/* SPDX-License-Identifier: GPL-2.0 */
35734 +/*
35735 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35736 + *
35737 + * This program, aufs is free software; you can redistribute it and/or modify
35738 + * it under the terms of the GNU General Public License as published by
35739 + * the Free Software Foundation; either version 2 of the License, or
35740 + * (at your option) any later version.
35741 + *
35742 + * This program is distributed in the hope that it will be useful,
35743 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35744 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35745 + * GNU General Public License for more details.
35746 + *
35747 + * You should have received a copy of the GNU General Public License
35748 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35749 + */
35750 +
35751 +/*
35752 + * whiteout for logical deletion and opaque directory
35753 + */
35754 +
35755 +#ifndef __AUFS_WHOUT_H__
35756 +#define __AUFS_WHOUT_H__
35757 +
35758 +#ifdef __KERNEL__
35759 +
35760 +#include "dir.h"
35761 +
35762 +/* whout.c */
35763 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35764 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35765 +int au_diropq_test(struct dentry *h_dentry);
35766 +struct au_branch;
35767 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35768 +                            struct qstr *prefix);
35769 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35770 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35771 +                       struct dentry *dentry);
35772 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35773 +
35774 +/* diropq flags */
35775 +#define AuDiropq_CREATE        1
35776 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35777 +#define au_fset_diropq(flags, name) \
35778 +       do { (flags) |= AuDiropq_##name; } while (0)
35779 +#define au_fclr_diropq(flags, name) \
35780 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35781 +
35782 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35783 +                            unsigned int flags);
35784 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35785 +                         struct au_branch *br);
35786 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35787 +                           struct dentry *h_parent);
35788 +
35789 +/* real rmdir for the whiteout-ed dir */
35790 +struct au_whtmp_rmdir {
35791 +       struct inode *dir;
35792 +       struct au_branch *br;
35793 +       struct dentry *wh_dentry;
35794 +       struct au_nhash whlist;
35795 +};
35796 +
35797 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35798 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35799 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35800 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
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 +/* ---------------------------------------------------------------------- */
35805 +
35806 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35807 +                                             aufs_bindex_t bindex)
35808 +{
35809 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35810 +}
35811 +
35812 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35813 +{
35814 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35815 +}
35816 +
35817 +#endif /* __KERNEL__ */
35818 +#endif /* __AUFS_WHOUT_H__ */
35819 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35820 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35821 +++ linux/fs/aufs/wkq.c 2021-02-24 13:33:42.751013936 +0100
35822 @@ -0,0 +1,372 @@
35823 +// SPDX-License-Identifier: GPL-2.0
35824 +/*
35825 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35826 + *
35827 + * This program, aufs is free software; you can redistribute it and/or modify
35828 + * it under the terms of the GNU General Public License as published by
35829 + * the Free Software Foundation; either version 2 of the License, or
35830 + * (at your option) any later version.
35831 + *
35832 + * This program is distributed in the hope that it will be useful,
35833 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35834 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35835 + * GNU General Public License for more details.
35836 + *
35837 + * You should have received a copy of the GNU General Public License
35838 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35839 + */
35840 +
35841 +/*
35842 + * workqueue for asynchronous/super-io operations
35843 + * todo: try new credential scheme
35844 + */
35845 +
35846 +#include <linux/module.h>
35847 +#include "aufs.h"
35848 +
35849 +/* internal workqueue named AUFS_WKQ_NAME */
35850 +
35851 +static struct workqueue_struct *au_wkq;
35852 +
35853 +struct au_wkinfo {
35854 +       struct work_struct wk;
35855 +       struct kobject *kobj;
35856 +
35857 +       unsigned int flags; /* see wkq.h */
35858 +
35859 +       au_wkq_func_t func;
35860 +       void *args;
35861 +
35862 +#ifdef CONFIG_LOCKDEP
35863 +       int dont_check;
35864 +       struct held_lock **hlock;
35865 +#endif
35866 +
35867 +       struct completion *comp;
35868 +};
35869 +
35870 +/* ---------------------------------------------------------------------- */
35871 +/*
35872 + * Aufs passes some operations to the workqueue such as the internal copyup.
35873 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35874 + * job run by workqueue depends upon the locks acquired in the other task.
35875 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35876 + * information too. And the job in the workqueue restores the info in order to
35877 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35878 + * correctly and expectedly.
35879 + */
35880 +
35881 +#ifndef CONFIG_LOCKDEP
35882 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35883 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35884 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35885 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35886 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35887 +#else
35888 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35889 +{
35890 +       wkinfo->hlock = NULL;
35891 +       wkinfo->dont_check = 0;
35892 +}
35893 +
35894 +/*
35895 + * 1: matched
35896 + * 0: unmatched
35897 + */
35898 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35899 +{
35900 +       static DEFINE_SPINLOCK(spin);
35901 +       static struct {
35902 +               char *name;
35903 +               struct lock_class_key *key;
35904 +       } a[] = {
35905 +               { .name = "&sbinfo->si_rwsem" },
35906 +               { .name = "&finfo->fi_rwsem" },
35907 +               { .name = "&dinfo->di_rwsem" },
35908 +               { .name = "&iinfo->ii_rwsem" }
35909 +       };
35910 +       static int set;
35911 +       int i;
35912 +
35913 +       /* lockless read from 'set.' see below */
35914 +       if (set == ARRAY_SIZE(a)) {
35915 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35916 +                       if (a[i].key == key)
35917 +                               goto match;
35918 +               goto unmatch;
35919 +       }
35920 +
35921 +       spin_lock(&spin);
35922 +       if (set)
35923 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35924 +                       if (a[i].key == key) {
35925 +                               spin_unlock(&spin);
35926 +                               goto match;
35927 +                       }
35928 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
35929 +               if (a[i].key) {
35930 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
35931 +                               spin_unlock(&spin);
35932 +                               goto match;
35933 +                       } else
35934 +                               continue;
35935 +               }
35936 +               if (strstr(a[i].name, name)) {
35937 +                       /*
35938 +                        * the order of these three lines is important for the
35939 +                        * lockless read above.
35940 +                        */
35941 +                       a[i].key = key;
35942 +                       spin_unlock(&spin);
35943 +                       set++;
35944 +                       /* AuDbg("%d, %s\n", set, name); */
35945 +                       goto match;
35946 +               }
35947 +       }
35948 +       spin_unlock(&spin);
35949 +       goto unmatch;
35950 +
35951 +match:
35952 +       return 1;
35953 +unmatch:
35954 +       return 0;
35955 +}
35956 +
35957 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35958 +{
35959 +       int err, n;
35960 +       struct task_struct *curr;
35961 +       struct held_lock **hl, *held_locks, *p;
35962 +
35963 +       err = 0;
35964 +       curr = current;
35965 +       wkinfo->dont_check = lockdep_recursing(curr);
35966 +       if (wkinfo->dont_check)
35967 +               goto out;
35968 +       n = curr->lockdep_depth;
35969 +       if (!n)
35970 +               goto out;
35971 +
35972 +       err = -ENOMEM;
35973 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35974 +       if (unlikely(!wkinfo->hlock))
35975 +               goto out;
35976 +
35977 +       err = 0;
35978 +#if 0 /* left for debugging */
35979 +       if (0 && au_debug_test())
35980 +               lockdep_print_held_locks(curr);
35981 +#endif
35982 +       held_locks = curr->held_locks;
35983 +       hl = wkinfo->hlock;
35984 +       while (n--) {
35985 +               p = held_locks++;
35986 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35987 +                       *hl++ = p;
35988 +       }
35989 +       *hl = NULL;
35990 +
35991 +out:
35992 +       return err;
35993 +}
35994 +
35995 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35996 +{
35997 +       au_kfree_try_rcu(wkinfo->hlock);
35998 +}
35999 +
36000 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36001 +{
36002 +       struct held_lock *p, **hl = wkinfo->hlock;
36003 +       int subclass;
36004 +
36005 +       if (wkinfo->dont_check)
36006 +               lockdep_off();
36007 +       if (!hl)
36008 +               return;
36009 +       while ((p = *hl++)) { /* assignment */
36010 +               subclass = lockdep_hlock_class(p)->subclass;
36011 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36012 +               if (p->read)
36013 +                       rwsem_acquire_read(p->instance, subclass, 0,
36014 +                                          /*p->acquire_ip*/_RET_IP_);
36015 +               else
36016 +                       rwsem_acquire(p->instance, subclass, 0,
36017 +                                     /*p->acquire_ip*/_RET_IP_);
36018 +       }
36019 +}
36020 +
36021 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36022 +{
36023 +       struct held_lock *p, **hl = wkinfo->hlock;
36024 +
36025 +       if (wkinfo->dont_check)
36026 +               lockdep_on();
36027 +       if (!hl)
36028 +               return;
36029 +       while ((p = *hl++)) /* assignment */
36030 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36031 +}
36032 +#endif
36033 +
36034 +static void wkq_func(struct work_struct *wk)
36035 +{
36036 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36037 +
36038 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36039 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36040 +
36041 +       au_wkq_lockdep_pre(wkinfo);
36042 +       wkinfo->func(wkinfo->args);
36043 +       au_wkq_lockdep_post(wkinfo);
36044 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36045 +               complete(wkinfo->comp);
36046 +       else {
36047 +               kobject_put(wkinfo->kobj);
36048 +               module_put(THIS_MODULE); /* todo: ?? */
36049 +               au_kfree_rcu(wkinfo);
36050 +       }
36051 +}
36052 +
36053 +/*
36054 + * Since struct completion is large, try allocating it dynamically.
36055 + */
36056 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36057 +
36058 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36059 +{
36060 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36061 +       if (*comp) {
36062 +               init_completion(*comp);
36063 +               wkinfo->comp = *comp;
36064 +               return 0;
36065 +       }
36066 +       return -ENOMEM;
36067 +}
36068 +
36069 +static void au_wkq_comp_free(struct completion *comp)
36070 +{
36071 +       au_kfree_rcu(comp);
36072 +}
36073 +
36074 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36075 +{
36076 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36077 +               if (au_wkq_test()) {
36078 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36079 +                               " due to a dead dir by UDBA,"
36080 +                               " or async xino write?\n");
36081 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36082 +               }
36083 +       } else
36084 +               au_dbg_verify_kthread();
36085 +
36086 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36087 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36088 +               queue_work(au_wkq, &wkinfo->wk);
36089 +       } else {
36090 +               INIT_WORK(&wkinfo->wk, wkq_func);
36091 +               schedule_work(&wkinfo->wk);
36092 +       }
36093 +}
36094 +
36095 +/*
36096 + * Be careful. It is easy to make deadlock happen.
36097 + * processA: lock, wkq and wait
36098 + * processB: wkq and wait, lock in wkq
36099 + * --> deadlock
36100 + */
36101 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36102 +{
36103 +       int err;
36104 +       AuWkqCompDeclare(comp);
36105 +       struct au_wkinfo wkinfo = {
36106 +               .flags  = flags,
36107 +               .func   = func,
36108 +               .args   = args
36109 +       };
36110 +
36111 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36112 +       if (unlikely(err))
36113 +               goto out;
36114 +       err = au_wkq_lockdep_alloc(&wkinfo);
36115 +       if (unlikely(err))
36116 +               goto out_comp;
36117 +       if (!err) {
36118 +               au_wkq_run(&wkinfo);
36119 +               /* no timeout, no interrupt */
36120 +               wait_for_completion(wkinfo.comp);
36121 +       }
36122 +       au_wkq_lockdep_free(&wkinfo);
36123 +
36124 +out_comp:
36125 +       au_wkq_comp_free(comp);
36126 +out:
36127 +       destroy_work_on_stack(&wkinfo.wk);
36128 +       return err;
36129 +}
36130 +
36131 +/*
36132 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36133 + * problem in a concurrent umounting.
36134 + */
36135 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36136 +                 unsigned int flags)
36137 +{
36138 +       int err;
36139 +       struct au_wkinfo *wkinfo;
36140 +
36141 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36142 +
36143 +       /*
36144 +        * wkq_func() must free this wkinfo.
36145 +        * it highly depends upon the implementation of workqueue.
36146 +        */
36147 +       err = 0;
36148 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36149 +       if (wkinfo) {
36150 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36151 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36152 +               wkinfo->func = func;
36153 +               wkinfo->args = args;
36154 +               wkinfo->comp = NULL;
36155 +               au_wkq_lockdep_init(wkinfo);
36156 +               kobject_get(wkinfo->kobj);
36157 +               __module_get(THIS_MODULE); /* todo: ?? */
36158 +
36159 +               au_wkq_run(wkinfo);
36160 +       } else {
36161 +               err = -ENOMEM;
36162 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36163 +       }
36164 +
36165 +       return err;
36166 +}
36167 +
36168 +/* ---------------------------------------------------------------------- */
36169 +
36170 +void au_nwt_init(struct au_nowait_tasks *nwt)
36171 +{
36172 +       atomic_set(&nwt->nw_len, 0);
36173 +       /* smp_mb(); */ /* atomic_set */
36174 +       init_waitqueue_head(&nwt->nw_wq);
36175 +}
36176 +
36177 +void au_wkq_fin(void)
36178 +{
36179 +       destroy_workqueue(au_wkq);
36180 +}
36181 +
36182 +int __init au_wkq_init(void)
36183 +{
36184 +       int err;
36185 +
36186 +       err = 0;
36187 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36188 +       if (IS_ERR(au_wkq))
36189 +               err = PTR_ERR(au_wkq);
36190 +       else if (!au_wkq)
36191 +               err = -ENOMEM;
36192 +
36193 +       return err;
36194 +}
36195 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36196 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36197 +++ linux/fs/aufs/wkq.h 2021-02-24 13:33:42.751013936 +0100
36198 @@ -0,0 +1,89 @@
36199 +/* SPDX-License-Identifier: GPL-2.0 */
36200 +/*
36201 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36202 + *
36203 + * This program, aufs is free software; you can redistribute it and/or modify
36204 + * it under the terms of the GNU General Public License as published by
36205 + * the Free Software Foundation; either version 2 of the License, or
36206 + * (at your option) any later version.
36207 + *
36208 + * This program is distributed in the hope that it will be useful,
36209 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36210 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36211 + * GNU General Public License for more details.
36212 + *
36213 + * You should have received a copy of the GNU General Public License
36214 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36215 + */
36216 +
36217 +/*
36218 + * workqueue for asynchronous/super-io operations
36219 + * todo: try new credentials management scheme
36220 + */
36221 +
36222 +#ifndef __AUFS_WKQ_H__
36223 +#define __AUFS_WKQ_H__
36224 +
36225 +#ifdef __KERNEL__
36226 +
36227 +#include <linux/wait.h>
36228 +
36229 +struct super_block;
36230 +
36231 +/* ---------------------------------------------------------------------- */
36232 +
36233 +/*
36234 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36235 + */
36236 +struct au_nowait_tasks {
36237 +       atomic_t                nw_len;
36238 +       wait_queue_head_t       nw_wq;
36239 +};
36240 +
36241 +/* ---------------------------------------------------------------------- */
36242 +
36243 +typedef void (*au_wkq_func_t)(void *args);
36244 +
36245 +/* wkq flags */
36246 +#define AuWkq_WAIT     1
36247 +#define AuWkq_NEST     (1 << 1)
36248 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36249 +#define au_fset_wkq(flags, name) \
36250 +       do { (flags) |= AuWkq_##name; } while (0)
36251 +#define au_fclr_wkq(flags, name) \
36252 +       do { (flags) &= ~AuWkq_##name; } while (0)
36253 +
36254 +/* wkq.c */
36255 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36256 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36257 +                 unsigned int flags);
36258 +void au_nwt_init(struct au_nowait_tasks *nwt);
36259 +int __init au_wkq_init(void);
36260 +void au_wkq_fin(void);
36261 +
36262 +/* ---------------------------------------------------------------------- */
36263 +
36264 +static inline int au_wkq_test(void)
36265 +{
36266 +       return current->flags & PF_WQ_WORKER;
36267 +}
36268 +
36269 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36270 +{
36271 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36272 +}
36273 +
36274 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36275 +{
36276 +       if (atomic_dec_and_test(&nwt->nw_len))
36277 +               wake_up_all(&nwt->nw_wq);
36278 +}
36279 +
36280 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36281 +{
36282 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36283 +       return 0;
36284 +}
36285 +
36286 +#endif /* __KERNEL__ */
36287 +#endif /* __AUFS_WKQ_H__ */
36288 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36289 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36290 +++ linux/fs/aufs/xattr.c       2021-02-24 13:33:42.751013936 +0100
36291 @@ -0,0 +1,356 @@
36292 +// SPDX-License-Identifier: GPL-2.0
36293 +/*
36294 + * Copyright (C) 2014-2020 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 + * handling xattr functions
36312 + */
36313 +
36314 +#include <linux/fs.h>
36315 +#include <linux/posix_acl_xattr.h>
36316 +#include <linux/xattr.h>
36317 +#include "aufs.h"
36318 +
36319 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36320 +{
36321 +       if (!ignore_flags)
36322 +               goto out;
36323 +       switch (err) {
36324 +       case -ENOMEM:
36325 +       case -EDQUOT:
36326 +               goto out;
36327 +       }
36328 +
36329 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36330 +               err = 0;
36331 +               goto out;
36332 +       }
36333 +
36334 +#define cmp(brattr, prefix) do {                                       \
36335 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36336 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36337 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36338 +                               err = 0;                                \
36339 +                       goto out;                                       \
36340 +               }                                                       \
36341 +       } while (0)
36342 +
36343 +       cmp(SEC, SECURITY);
36344 +       cmp(SYS, SYSTEM);
36345 +       cmp(TR, TRUSTED);
36346 +       cmp(USR, USER);
36347 +#undef cmp
36348 +
36349 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36350 +               err = 0;
36351 +
36352 +out:
36353 +       return err;
36354 +}
36355 +
36356 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36357 +
36358 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36359 +                           char *name, char **buf, unsigned int ignore_flags,
36360 +                           unsigned int verbose)
36361 +{
36362 +       int err;
36363 +       ssize_t ssz;
36364 +       struct inode *h_idst;
36365 +
36366 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36367 +       err = ssz;
36368 +       if (unlikely(err <= 0)) {
36369 +               if (err == -ENODATA
36370 +                   || (err == -EOPNOTSUPP
36371 +                       && ((ignore_flags & au_xattr_out_of_list)
36372 +                           || (au_test_nfs_noacl(d_inode(h_src))
36373 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36374 +                                   || !strcmp(name,
36375 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36376 +                           ))
36377 +                       err = 0;
36378 +               if (err && (verbose || au_debug_test()))
36379 +                       pr_err("%s, err %d\n", name, err);
36380 +               goto out;
36381 +       }
36382 +
36383 +       /* unlock it temporary */
36384 +       h_idst = d_inode(h_dst);
36385 +       inode_unlock(h_idst);
36386 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36387 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36388 +       if (unlikely(err)) {
36389 +               if (verbose || au_debug_test())
36390 +                       pr_err("%s, err %d\n", name, err);
36391 +               err = au_xattr_ignore(err, name, ignore_flags);
36392 +       }
36393 +
36394 +out:
36395 +       return err;
36396 +}
36397 +
36398 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36399 +                 unsigned int verbose)
36400 +{
36401 +       int err, unlocked, acl_access, acl_default;
36402 +       ssize_t ssz;
36403 +       struct inode *h_isrc, *h_idst;
36404 +       char *value, *p, *o, *e;
36405 +
36406 +       /* try stopping to update the source inode while we are referencing */
36407 +       /* there should not be the parent-child relationship between them */
36408 +       h_isrc = d_inode(h_src);
36409 +       h_idst = d_inode(h_dst);
36410 +       inode_unlock(h_idst);
36411 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36412 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36413 +       unlocked = 0;
36414 +
36415 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36416 +       ssz = vfs_listxattr(h_src, NULL, 0);
36417 +       err = ssz;
36418 +       if (unlikely(err < 0)) {
36419 +               AuTraceErr(err);
36420 +               if (err == -ENODATA
36421 +                   || err == -EOPNOTSUPP)
36422 +                       err = 0;        /* ignore */
36423 +               goto out;
36424 +       }
36425 +
36426 +       err = 0;
36427 +       p = NULL;
36428 +       o = NULL;
36429 +       if (ssz) {
36430 +               err = -ENOMEM;
36431 +               p = kmalloc(ssz, GFP_NOFS);
36432 +               o = p;
36433 +               if (unlikely(!p))
36434 +                       goto out;
36435 +               err = vfs_listxattr(h_src, p, ssz);
36436 +       }
36437 +       inode_unlock_shared(h_isrc);
36438 +       unlocked = 1;
36439 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36440 +       if (unlikely(err < 0))
36441 +               goto out_free;
36442 +
36443 +       err = 0;
36444 +       e = p + ssz;
36445 +       value = NULL;
36446 +       acl_access = 0;
36447 +       acl_default = 0;
36448 +       while (!err && p < e) {
36449 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36450 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36451 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36452 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36453 +                                       - 1);
36454 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36455 +                                      verbose);
36456 +               p += strlen(p) + 1;
36457 +       }
36458 +       AuTraceErr(err);
36459 +       ignore_flags |= au_xattr_out_of_list;
36460 +       if (!err && !acl_access) {
36461 +               err = au_do_cpup_xattr(h_dst, h_src,
36462 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36463 +                                      ignore_flags, verbose);
36464 +               AuTraceErr(err);
36465 +       }
36466 +       if (!err && !acl_default) {
36467 +               err = au_do_cpup_xattr(h_dst, h_src,
36468 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36469 +                                      ignore_flags, verbose);
36470 +               AuTraceErr(err);
36471 +       }
36472 +
36473 +       au_kfree_try_rcu(value);
36474 +
36475 +out_free:
36476 +       au_kfree_try_rcu(o);
36477 +out:
36478 +       if (!unlocked)
36479 +               inode_unlock_shared(h_isrc);
36480 +       AuTraceErr(err);
36481 +       return err;
36482 +}
36483 +
36484 +/* ---------------------------------------------------------------------- */
36485 +
36486 +static int au_smack_reentering(struct super_block *sb)
36487 +{
36488 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36489 +       /*
36490 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36491 +        * i_op->getxattr(). ouch.
36492 +        */
36493 +       return si_pid_test(sb);
36494 +#else
36495 +       return 0;
36496 +#endif
36497 +}
36498 +
36499 +enum {
36500 +       AU_XATTR_LIST,
36501 +       AU_XATTR_GET
36502 +};
36503 +
36504 +struct au_lgxattr {
36505 +       int type;
36506 +       union {
36507 +               struct {
36508 +                       char    *list;
36509 +                       size_t  size;
36510 +               } list;
36511 +               struct {
36512 +                       const char      *name;
36513 +                       void            *value;
36514 +                       size_t          size;
36515 +               } get;
36516 +       } u;
36517 +};
36518 +
36519 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36520 +                         struct au_lgxattr *arg)
36521 +{
36522 +       ssize_t err;
36523 +       int reenter;
36524 +       struct path h_path;
36525 +       struct super_block *sb;
36526 +
36527 +       sb = dentry->d_sb;
36528 +       reenter = au_smack_reentering(sb);
36529 +       if (!reenter) {
36530 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36531 +               if (unlikely(err))
36532 +                       goto out;
36533 +       }
36534 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36535 +       if (unlikely(err))
36536 +               goto out_si;
36537 +       if (unlikely(!h_path.dentry))
36538 +               /* illegally overlapped or something */
36539 +               goto out_di; /* pretending success */
36540 +
36541 +       /* always topmost entry only */
36542 +       switch (arg->type) {
36543 +       case AU_XATTR_LIST:
36544 +               err = vfs_listxattr(h_path.dentry,
36545 +                                   arg->u.list.list, arg->u.list.size);
36546 +               break;
36547 +       case AU_XATTR_GET:
36548 +               AuDebugOn(d_is_negative(h_path.dentry));
36549 +               err = vfs_getxattr(h_path.dentry,
36550 +                                  arg->u.get.name, arg->u.get.value,
36551 +                                  arg->u.get.size);
36552 +               break;
36553 +       }
36554 +
36555 +out_di:
36556 +       if (!reenter)
36557 +               di_read_unlock(dentry, AuLock_IR);
36558 +out_si:
36559 +       if (!reenter)
36560 +               si_read_unlock(sb);
36561 +out:
36562 +       AuTraceErr(err);
36563 +       return err;
36564 +}
36565 +
36566 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36567 +{
36568 +       struct au_lgxattr arg = {
36569 +               .type = AU_XATTR_LIST,
36570 +               .u.list = {
36571 +                       .list   = list,
36572 +                       .size   = size
36573 +               },
36574 +       };
36575 +
36576 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36577 +}
36578 +
36579 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36580 +                          const char *name, void *value, size_t size)
36581 +{
36582 +       struct au_lgxattr arg = {
36583 +               .type = AU_XATTR_GET,
36584 +               .u.get = {
36585 +                       .name   = name,
36586 +                       .value  = value,
36587 +                       .size   = size
36588 +               },
36589 +       };
36590 +
36591 +       return au_lgxattr(dentry, inode, &arg);
36592 +}
36593 +
36594 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36595 +                      const char *name, const void *value, size_t size,
36596 +                      int flags)
36597 +{
36598 +       struct au_sxattr arg = {
36599 +               .type = AU_XATTR_SET,
36600 +               .u.set = {
36601 +                       .name   = name,
36602 +                       .value  = value,
36603 +                       .size   = size,
36604 +                       .flags  = flags
36605 +               },
36606 +       };
36607 +
36608 +       return au_sxattr(dentry, inode, &arg);
36609 +}
36610 +
36611 +/* ---------------------------------------------------------------------- */
36612 +
36613 +static int au_xattr_get(const struct xattr_handler *handler,
36614 +                       struct dentry *dentry, struct inode *inode,
36615 +                       const char *name, void *buffer, size_t size)
36616 +{
36617 +       return au_getxattr(dentry, inode, name, buffer, size);
36618 +}
36619 +
36620 +static int au_xattr_set(const struct xattr_handler *handler,
36621 +                       struct dentry *dentry, struct inode *inode,
36622 +                       const char *name, const void *value, size_t size,
36623 +                       int flags)
36624 +{
36625 +       return au_setxattr(dentry, inode, name, value, size, flags);
36626 +}
36627 +
36628 +static const struct xattr_handler au_xattr_handler = {
36629 +       .name   = "",
36630 +       .prefix = "",
36631 +       .get    = au_xattr_get,
36632 +       .set    = au_xattr_set
36633 +};
36634 +
36635 +static const struct xattr_handler *au_xattr_handlers[] = {
36636 +#ifdef CONFIG_FS_POSIX_ACL
36637 +       &posix_acl_access_xattr_handler,
36638 +       &posix_acl_default_xattr_handler,
36639 +#endif
36640 +       &au_xattr_handler, /* must be last */
36641 +       NULL
36642 +};
36643 +
36644 +void au_xattr_init(struct super_block *sb)
36645 +{
36646 +       sb->s_xattr = au_xattr_handlers;
36647 +}
36648 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36649 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36650 +++ linux/fs/aufs/xino.c        2021-02-24 13:33:42.751013936 +0100
36651 @@ -0,0 +1,1925 @@
36652 +// SPDX-License-Identifier: GPL-2.0
36653 +/*
36654 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36655 + *
36656 + * This program, aufs is free software; you can redistribute it and/or modify
36657 + * it under the terms of the GNU General Public License as published by
36658 + * the Free Software Foundation; either version 2 of the License, or
36659 + * (at your option) any later version.
36660 + *
36661 + * This program is distributed in the hope that it will be useful,
36662 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36663 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36664 + * GNU General Public License for more details.
36665 + *
36666 + * You should have received a copy of the GNU General Public License
36667 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36668 + */
36669 +
36670 +/*
36671 + * external inode number translation table and bitmap
36672 + *
36673 + * things to consider
36674 + * - the lifetime
36675 + *   + au_xino object
36676 + *   + XINO files (xino, xib, xigen)
36677 + *   + dynamic debugfs entries (xiN)
36678 + *   + static debugfs entries (xib, xigen)
36679 + *   + static sysfs entry (xi_path)
36680 + * - several entry points to handle them.
36681 + *   + mount(2) without xino option (default)
36682 + *   + mount(2) with xino option
36683 + *   + mount(2) with noxino option
36684 + *   + umount(2)
36685 + *   + remount with add/del branches
36686 + *   + remount with xino/noxino options
36687 + */
36688 +
36689 +#include <linux/seq_file.h>
36690 +#include <linux/statfs.h>
36691 +#include "aufs.h"
36692 +
36693 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36694 +                                    aufs_bindex_t bbot,
36695 +                                    struct super_block *h_sb)
36696 +{
36697 +       /* todo: try binary-search if the branches are many */
36698 +       for (; btop <= bbot; btop++)
36699 +               if (h_sb == au_sbr_sb(sb, btop))
36700 +                       return btop;
36701 +       return -1;
36702 +}
36703 +
36704 +/*
36705 + * find another branch who is on the same filesystem of the specified
36706 + * branch{@btgt}. search until @bbot.
36707 + */
36708 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36709 +                                 aufs_bindex_t bbot)
36710 +{
36711 +       aufs_bindex_t bindex;
36712 +       struct super_block *tgt_sb;
36713 +
36714 +       tgt_sb = au_sbr_sb(sb, btgt);
36715 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36716 +       if (bindex < 0)
36717 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36718 +
36719 +       return bindex;
36720 +}
36721 +
36722 +/* ---------------------------------------------------------------------- */
36723 +
36724 +/*
36725 + * stop unnecessary notify events at creating xino files
36726 + */
36727 +
36728 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36729 +{
36730 +       aufs_bindex_t bfound, bindex, bbot;
36731 +       struct dentry *parent;
36732 +       struct au_branch *br;
36733 +
36734 +       bfound = -1;
36735 +       parent = dentry->d_parent; /* safe d_parent access */
36736 +       bbot = au_sbbot(sb);
36737 +       for (bindex = 0; bindex <= bbot; bindex++) {
36738 +               br = au_sbr(sb, bindex);
36739 +               if (au_br_dentry(br) == parent) {
36740 +                       bfound = bindex;
36741 +                       break;
36742 +               }
36743 +       }
36744 +
36745 +       AuDbg("bfound b%d\n", bfound);
36746 +       return bfound;
36747 +}
36748 +
36749 +struct au_xino_lock_dir {
36750 +       struct au_hinode *hdir;
36751 +       struct dentry *parent;
36752 +       struct inode *dir;
36753 +};
36754 +
36755 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36756 +                                         unsigned int lsc)
36757 +{
36758 +       struct dentry *parent;
36759 +       struct inode *dir;
36760 +
36761 +       parent = dget_parent(dentry);
36762 +       dir = d_inode(parent);
36763 +       inode_lock_nested(dir, lsc);
36764 +#if 0 /* it should not happen */
36765 +       spin_lock(&dentry->d_lock);
36766 +       if (unlikely(dentry->d_parent != parent)) {
36767 +               spin_unlock(&dentry->d_lock);
36768 +               inode_unlock(dir);
36769 +               dput(parent);
36770 +               parent = NULL;
36771 +               goto out;
36772 +       }
36773 +       spin_unlock(&dentry->d_lock);
36774 +
36775 +out:
36776 +#endif
36777 +       return parent;
36778 +}
36779 +
36780 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36781 +                            struct au_xino_lock_dir *ldir)
36782 +{
36783 +       aufs_bindex_t bindex;
36784 +
36785 +       ldir->hdir = NULL;
36786 +       bindex = au_xi_root(sb, xipath->dentry);
36787 +       if (bindex >= 0) {
36788 +               /* rw branch root */
36789 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36790 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36791 +       } else {
36792 +               /* other */
36793 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36794 +                                                  AuLsc_I_PARENT);
36795 +               ldir->dir = d_inode(ldir->parent);
36796 +       }
36797 +}
36798 +
36799 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36800 +{
36801 +       if (ldir->hdir)
36802 +               au_hn_inode_unlock(ldir->hdir);
36803 +       else {
36804 +               inode_unlock(ldir->dir);
36805 +               dput(ldir->parent);
36806 +       }
36807 +}
36808 +
36809 +/* ---------------------------------------------------------------------- */
36810 +
36811 +/*
36812 + * create and set a new xino file
36813 + */
36814 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36815 +                           int wbrtop)
36816 +{
36817 +       struct file *file;
36818 +       struct dentry *h_parent, *d;
36819 +       struct inode *h_dir, *inode;
36820 +       int err;
36821 +       static DEFINE_MUTEX(mtx);
36822 +
36823 +       /*
36824 +        * at mount-time, and the xino file is the default path,
36825 +        * hnotify is disabled so we have no notify events to ignore.
36826 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36827 +        */
36828 +       if (!wbrtop)
36829 +               mutex_lock(&mtx);
36830 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36831 +                              /* | __FMODE_NONOTIFY */,
36832 +                              0666);
36833 +       if (IS_ERR(file)) {
36834 +               if (!wbrtop)
36835 +                       mutex_unlock(&mtx);
36836 +               if (!silent)
36837 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36838 +               return file;
36839 +       }
36840 +
36841 +       /* keep file count */
36842 +       err = 0;
36843 +       d = file->f_path.dentry;
36844 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36845 +       if (!wbrtop)
36846 +               mutex_unlock(&mtx);
36847 +       /* mnt_want_write() is unnecessary here */
36848 +       h_dir = d_inode(h_parent);
36849 +       inode = file_inode(file);
36850 +       /* no delegation since it is just created */
36851 +       if (inode->i_nlink)
36852 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36853 +                                  /*force*/0);
36854 +       inode_unlock(h_dir);
36855 +       dput(h_parent);
36856 +       if (unlikely(err)) {
36857 +               if (!silent)
36858 +                       pr_err("unlink %s(%d)\n", fpath, err);
36859 +               goto out;
36860 +       }
36861 +
36862 +       err = -EINVAL;
36863 +       if (unlikely(sb == d->d_sb)) {
36864 +               if (!silent)
36865 +                       pr_err("%s must be outside\n", fpath);
36866 +               goto out;
36867 +       }
36868 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36869 +               if (!silent)
36870 +                       pr_err("xino doesn't support %s(%s)\n",
36871 +                              fpath, au_sbtype(d->d_sb));
36872 +               goto out;
36873 +       }
36874 +       return file; /* success */
36875 +
36876 +out:
36877 +       fput(file);
36878 +       file = ERR_PTR(err);
36879 +       return file;
36880 +}
36881 +
36882 +/*
36883 + * create a new xinofile at the same place/path as @base.
36884 + */
36885 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
36886 +                            struct file *copy_src)
36887 +{
36888 +       struct file *file;
36889 +       struct dentry *dentry, *parent;
36890 +       struct inode *dir, *delegated;
36891 +       struct qstr *name;
36892 +       struct path path;
36893 +       int err, do_unlock;
36894 +       struct au_xino_lock_dir ldir;
36895 +
36896 +       do_unlock = 1;
36897 +       au_xino_lock_dir(sb, base, &ldir);
36898 +       dentry = base->dentry;
36899 +       parent = dentry->d_parent; /* dir inode is locked */
36900 +       dir = d_inode(parent);
36901 +       IMustLock(dir);
36902 +
36903 +       name = &dentry->d_name;
36904 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36905 +       if (IS_ERR(path.dentry)) {
36906 +               file = (void *)path.dentry;
36907 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36908 +               goto out;
36909 +       }
36910 +
36911 +       /* no need to mnt_want_write() since we call dentry_open() later */
36912 +       err = vfs_create(dir, path.dentry, 0666, NULL);
36913 +       if (unlikely(err)) {
36914 +               file = ERR_PTR(err);
36915 +               pr_err("%pd create err %d\n", dentry, err);
36916 +               goto out_dput;
36917 +       }
36918 +
36919 +       path.mnt = base->mnt;
36920 +       file = vfsub_dentry_open(&path,
36921 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36922 +                                /* | __FMODE_NONOTIFY */);
36923 +       if (IS_ERR(file)) {
36924 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36925 +               goto out_dput;
36926 +       }
36927 +
36928 +       delegated = NULL;
36929 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36930 +       au_xino_unlock_dir(&ldir);
36931 +       do_unlock = 0;
36932 +       if (unlikely(err == -EWOULDBLOCK)) {
36933 +               pr_warn("cannot retry for NFSv4 delegation"
36934 +                       " for an internal unlink\n");
36935 +               iput(delegated);
36936 +       }
36937 +       if (unlikely(err)) {
36938 +               pr_err("%pd unlink err %d\n", dentry, err);
36939 +               goto out_fput;
36940 +       }
36941 +
36942 +       if (copy_src) {
36943 +               /* no one can touch copy_src xino */
36944 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36945 +               if (unlikely(err)) {
36946 +                       pr_err("%pd copy err %d\n", dentry, err);
36947 +                       goto out_fput;
36948 +               }
36949 +       }
36950 +       goto out_dput; /* success */
36951 +
36952 +out_fput:
36953 +       fput(file);
36954 +       file = ERR_PTR(err);
36955 +out_dput:
36956 +       dput(path.dentry);
36957 +out:
36958 +       if (do_unlock)
36959 +               au_xino_unlock_dir(&ldir);
36960 +       return file;
36961 +}
36962 +
36963 +struct file *au_xino_file1(struct au_xino *xi)
36964 +{
36965 +       struct file *file;
36966 +       unsigned int u, nfile;
36967 +
36968 +       file = NULL;
36969 +       nfile = xi->xi_nfile;
36970 +       for (u = 0; u < nfile; u++) {
36971 +               file = xi->xi_file[u];
36972 +               if (file)
36973 +                       break;
36974 +       }
36975 +
36976 +       return file;
36977 +}
36978 +
36979 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
36980 +{
36981 +       int err;
36982 +       struct file *f;
36983 +       void *p;
36984 +
36985 +       if (file)
36986 +               get_file(file);
36987 +
36988 +       err = 0;
36989 +       f = NULL;
36990 +       if (idx < xi->xi_nfile) {
36991 +               f = xi->xi_file[idx];
36992 +               if (f)
36993 +                       fput(f);
36994 +       } else {
36995 +               p = au_kzrealloc(xi->xi_file,
36996 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
36997 +                                sizeof(*xi->xi_file) * (idx + 1),
36998 +                                GFP_NOFS, /*may_shrink*/0);
36999 +               if (p) {
37000 +                       MtxMustLock(&xi->xi_mtx);
37001 +                       xi->xi_file = p;
37002 +                       xi->xi_nfile = idx + 1;
37003 +               } else {
37004 +                       err = -ENOMEM;
37005 +                       if (file)
37006 +                               fput(file);
37007 +                       goto out;
37008 +               }
37009 +       }
37010 +       xi->xi_file[idx] = file;
37011 +
37012 +out:
37013 +       return err;
37014 +}
37015 +
37016 +/*
37017 + * if @xinew->xi is not set, then create new xigen file.
37018 + */
37019 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37020 +{
37021 +       struct file *file;
37022 +       int err;
37023 +
37024 +       SiMustAnyLock(sb);
37025 +
37026 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37027 +       if (IS_ERR(file)) {
37028 +               err = PTR_ERR(file);
37029 +               pr_err("%s[%d], err %d\n",
37030 +                      xinew->xi ? "xino" : "xigen",
37031 +                      xinew->idx, err);
37032 +               goto out;
37033 +       }
37034 +
37035 +       if (xinew->xi)
37036 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37037 +       else {
37038 +               BUG();
37039 +               /* todo: make xigen file an array */
37040 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37041 +       }
37042 +       fput(file);
37043 +       if (unlikely(err))
37044 +               file = ERR_PTR(err);
37045 +
37046 +out:
37047 +       return file;
37048 +}
37049 +
37050 +/* ---------------------------------------------------------------------- */
37051 +
37052 +/*
37053 + * truncate xino files
37054 + */
37055 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37056 +                           int idx, struct kstatfs *st)
37057 +{
37058 +       int err;
37059 +       blkcnt_t blocks;
37060 +       struct file *file, *new_xino;
37061 +       struct au_xi_new xinew = {
37062 +               .idx = idx
37063 +       };
37064 +
37065 +       err = 0;
37066 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37067 +       file = au_xino_file(xinew.xi, idx);
37068 +       if (!file)
37069 +               goto out;
37070 +
37071 +       xinew.base = &file->f_path;
37072 +       err = vfs_statfs(xinew.base, st);
37073 +       if (unlikely(err)) {
37074 +               AuErr1("statfs err %d, ignored\n", err);
37075 +               err = 0;
37076 +               goto out;
37077 +       }
37078 +
37079 +       blocks = file_inode(file)->i_blocks;
37080 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37081 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37082 +
37083 +       xinew.copy_src = file;
37084 +       new_xino = au_xi_new(sb, &xinew);
37085 +       if (IS_ERR(new_xino)) {
37086 +               err = PTR_ERR(new_xino);
37087 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37088 +               goto out;
37089 +       }
37090 +
37091 +       err = vfs_statfs(&new_xino->f_path, st);
37092 +       if (!err)
37093 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37094 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37095 +                       st->f_bfree, st->f_blocks);
37096 +       else {
37097 +               AuErr1("statfs err %d, ignored\n", err);
37098 +               err = 0;
37099 +       }
37100 +
37101 +out:
37102 +       return err;
37103 +}
37104 +
37105 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37106 +{
37107 +       int err, i;
37108 +       unsigned long jiffy;
37109 +       aufs_bindex_t bbot;
37110 +       struct kstatfs *st;
37111 +       struct au_branch *br;
37112 +       struct au_xino *xi;
37113 +
37114 +       err = -ENOMEM;
37115 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37116 +       if (unlikely(!st))
37117 +               goto out;
37118 +
37119 +       err = -EINVAL;
37120 +       bbot = au_sbbot(sb);
37121 +       if (unlikely(bindex < 0 || bbot < bindex))
37122 +               goto out_st;
37123 +
37124 +       err = 0;
37125 +       jiffy = jiffies;
37126 +       br = au_sbr(sb, bindex);
37127 +       xi = br->br_xino;
37128 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37129 +               err = au_xino_do_trunc(sb, bindex, i, st);
37130 +       if (!err)
37131 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37132 +
37133 +out_st:
37134 +       au_kfree_rcu(st);
37135 +out:
37136 +       return err;
37137 +}
37138 +
37139 +struct xino_do_trunc_args {
37140 +       struct super_block *sb;
37141 +       struct au_branch *br;
37142 +       int idx;
37143 +};
37144 +
37145 +static void xino_do_trunc(void *_args)
37146 +{
37147 +       struct xino_do_trunc_args *args = _args;
37148 +       struct super_block *sb;
37149 +       struct au_branch *br;
37150 +       struct inode *dir;
37151 +       int err, idx;
37152 +       aufs_bindex_t bindex;
37153 +
37154 +       err = 0;
37155 +       sb = args->sb;
37156 +       dir = d_inode(sb->s_root);
37157 +       br = args->br;
37158 +       idx = args->idx;
37159 +
37160 +       si_noflush_write_lock(sb);
37161 +       ii_read_lock_parent(dir);
37162 +       bindex = au_br_index(sb, br->br_id);
37163 +       err = au_xino_trunc(sb, bindex, idx);
37164 +       ii_read_unlock(dir);
37165 +       if (unlikely(err))
37166 +               pr_warn("err b%d, (%d)\n", bindex, err);
37167 +       atomic_dec(&br->br_xino->xi_truncating);
37168 +       au_lcnt_dec(&br->br_count);
37169 +       si_write_unlock(sb);
37170 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37171 +       au_kfree_rcu(args);
37172 +}
37173 +
37174 +/*
37175 + * returns the index in the xi_file array whose corresponding file is necessary
37176 + * to truncate, or -1 which means no need to truncate.
37177 + */
37178 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37179 +{
37180 +       int err;
37181 +       unsigned int u;
37182 +       struct kstatfs st;
37183 +       struct au_sbinfo *sbinfo;
37184 +       struct au_xino *xi;
37185 +       struct file *file;
37186 +
37187 +       /* todo: si_xino_expire and the ratio should be customizable */
37188 +       sbinfo = au_sbi(sb);
37189 +       if (time_before(jiffies,
37190 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37191 +               return -1;
37192 +
37193 +       /* truncation border */
37194 +       xi = br->br_xino;
37195 +       for (u = 0; u < xi->xi_nfile; u++) {
37196 +               file = au_xino_file(xi, u);
37197 +               if (!file)
37198 +                       continue;
37199 +
37200 +               err = vfs_statfs(&file->f_path, &st);
37201 +               if (unlikely(err)) {
37202 +                       AuErr1("statfs err %d, ignored\n", err);
37203 +                       return -1;
37204 +               }
37205 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37206 +                   >= AUFS_XINO_DEF_TRUNC)
37207 +                       return u;
37208 +       }
37209 +
37210 +       return -1;
37211 +}
37212 +
37213 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37214 +{
37215 +       int idx;
37216 +       struct xino_do_trunc_args *args;
37217 +       int wkq_err;
37218 +
37219 +       idx = xino_trunc_test(sb, br);
37220 +       if (idx < 0)
37221 +               return;
37222 +
37223 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37224 +               goto out;
37225 +
37226 +       /* lock and kfree() will be called in trunc_xino() */
37227 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37228 +       if (unlikely(!args)) {
37229 +               AuErr1("no memory\n");
37230 +               goto out;
37231 +       }
37232 +
37233 +       au_lcnt_inc(&br->br_count);
37234 +       args->sb = sb;
37235 +       args->br = br;
37236 +       args->idx = idx;
37237 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37238 +       if (!wkq_err)
37239 +               return; /* success */
37240 +
37241 +       pr_err("wkq %d\n", wkq_err);
37242 +       au_lcnt_dec(&br->br_count);
37243 +       au_kfree_rcu(args);
37244 +
37245 +out:
37246 +       atomic_dec(&br->br_xino->xi_truncating);
37247 +}
37248 +
37249 +/* ---------------------------------------------------------------------- */
37250 +
37251 +struct au_xi_calc {
37252 +       int idx;
37253 +       loff_t pos;
37254 +};
37255 +
37256 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37257 +                      struct au_xi_calc *calc)
37258 +{
37259 +       loff_t maxent;
37260 +
37261 +       maxent = au_xi_maxent(sb);
37262 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37263 +       calc->pos *= sizeof(ino_t);
37264 +}
37265 +
37266 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37267 +                               struct au_xi_calc *calc)
37268 +{
37269 +       int err;
37270 +       struct file *file;
37271 +       struct au_xino *xi = br->br_xino;
37272 +       struct au_xi_new xinew = {
37273 +               .xi = xi
37274 +       };
37275 +
37276 +       SiMustAnyLock(sb);
37277 +
37278 +       err = 0;
37279 +       if (!xi)
37280 +               goto out;
37281 +
37282 +       mutex_lock(&xi->xi_mtx);
37283 +       file = au_xino_file(xi, calc->idx);
37284 +       if (file)
37285 +               goto out_mtx;
37286 +
37287 +       file = au_xino_file(xi, /*idx*/-1);
37288 +       AuDebugOn(!file);
37289 +       xinew.idx = calc->idx;
37290 +       xinew.base = &file->f_path;
37291 +       /* xinew.copy_src = NULL; */
37292 +       file = au_xi_new(sb, &xinew);
37293 +       if (IS_ERR(file))
37294 +               err = PTR_ERR(file);
37295 +
37296 +out_mtx:
37297 +       mutex_unlock(&xi->xi_mtx);
37298 +out:
37299 +       return err;
37300 +}
37301 +
37302 +struct au_xino_do_new_async_args {
37303 +       struct super_block *sb;
37304 +       struct au_branch *br;
37305 +       struct au_xi_calc calc;
37306 +       ino_t ino;
37307 +};
37308 +
37309 +struct au_xi_writing {
37310 +       struct hlist_bl_node node;
37311 +       ino_t h_ino, ino;
37312 +};
37313 +
37314 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37315 +                           ino_t ino);
37316 +
37317 +static void au_xino_call_do_new_async(void *args)
37318 +{
37319 +       struct au_xino_do_new_async_args *a = args;
37320 +       struct au_branch *br;
37321 +       struct super_block *sb;
37322 +       struct au_sbinfo *sbi;
37323 +       struct inode *root;
37324 +       struct file *file;
37325 +       struct au_xi_writing *del, *p;
37326 +       struct hlist_bl_head *hbl;
37327 +       struct hlist_bl_node *pos;
37328 +       int err;
37329 +
37330 +       br = a->br;
37331 +       sb = a->sb;
37332 +       sbi = au_sbi(sb);
37333 +       si_noflush_read_lock(sb);
37334 +       root = d_inode(sb->s_root);
37335 +       ii_read_lock_child(root);
37336 +       err = au_xino_do_new_async(sb, br, &a->calc);
37337 +       if (unlikely(err)) {
37338 +               AuIOErr("err %d\n", err);
37339 +               goto out;
37340 +       }
37341 +
37342 +       file = au_xino_file(br->br_xino, a->calc.idx);
37343 +       AuDebugOn(!file);
37344 +       err = au_xino_do_write(file, &a->calc, a->ino);
37345 +       if (unlikely(err)) {
37346 +               AuIOErr("err %d\n", err);
37347 +               goto out;
37348 +       }
37349 +
37350 +       del = NULL;
37351 +       hbl = &br->br_xino->xi_writing;
37352 +       hlist_bl_lock(hbl);
37353 +       au_hbl_for_each(pos, hbl) {
37354 +               p = container_of(pos, struct au_xi_writing, node);
37355 +               if (p->ino == a->ino) {
37356 +                       del = p;
37357 +                       hlist_bl_del(&p->node);
37358 +                       break;
37359 +               }
37360 +       }
37361 +       hlist_bl_unlock(hbl);
37362 +       au_kfree_rcu(del);
37363 +
37364 +out:
37365 +       au_lcnt_dec(&br->br_count);
37366 +       ii_read_unlock(root);
37367 +       si_read_unlock(sb);
37368 +       au_nwt_done(&sbi->si_nowait);
37369 +       au_kfree_rcu(a);
37370 +}
37371 +
37372 +/*
37373 + * create a new xino file asynchronously
37374 + */
37375 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37376 +                            struct au_xi_calc *calc, ino_t ino)
37377 +{
37378 +       int err;
37379 +       struct au_xino_do_new_async_args *arg;
37380 +
37381 +       err = -ENOMEM;
37382 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37383 +       if (unlikely(!arg))
37384 +               goto out;
37385 +
37386 +       arg->sb = sb;
37387 +       arg->br = br;
37388 +       arg->calc = *calc;
37389 +       arg->ino = ino;
37390 +       au_lcnt_inc(&br->br_count);
37391 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37392 +       if (unlikely(err)) {
37393 +               pr_err("wkq %d\n", err);
37394 +               au_lcnt_dec(&br->br_count);
37395 +               au_kfree_rcu(arg);
37396 +       }
37397 +
37398 +out:
37399 +       return err;
37400 +}
37401 +
37402 +/*
37403 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37404 + * at the position of @h_ino.
37405 + */
37406 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37407 +                ino_t *ino)
37408 +{
37409 +       int err;
37410 +       ssize_t sz;
37411 +       struct au_xi_calc calc;
37412 +       struct au_sbinfo *sbinfo;
37413 +       struct file *file;
37414 +       struct au_xino *xi;
37415 +       struct hlist_bl_head *hbl;
37416 +       struct hlist_bl_node *pos;
37417 +       struct au_xi_writing *p;
37418 +
37419 +       *ino = 0;
37420 +       if (!au_opt_test(au_mntflags(sb), XINO))
37421 +               return 0; /* no xino */
37422 +
37423 +       err = 0;
37424 +       au_xi_calc(sb, h_ino, &calc);
37425 +       xi = au_sbr(sb, bindex)->br_xino;
37426 +       file = au_xino_file(xi, calc.idx);
37427 +       if (!file) {
37428 +               hbl = &xi->xi_writing;
37429 +               hlist_bl_lock(hbl);
37430 +               au_hbl_for_each(pos, hbl) {
37431 +                       p = container_of(pos, struct au_xi_writing, node);
37432 +                       if (p->h_ino == h_ino) {
37433 +                               AuDbg("hi%llu, i%llu, found\n",
37434 +                                     (u64)p->h_ino, (u64)p->ino);
37435 +                               *ino = p->ino;
37436 +                               break;
37437 +                       }
37438 +               }
37439 +               hlist_bl_unlock(hbl);
37440 +               return 0;
37441 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37442 +               return 0; /* no xino */
37443 +
37444 +       sbinfo = au_sbi(sb);
37445 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37446 +       if (sz == sizeof(*ino))
37447 +               return 0; /* success */
37448 +
37449 +       err = sz;
37450 +       if (unlikely(sz >= 0)) {
37451 +               err = -EIO;
37452 +               AuIOErr("xino read error (%zd)\n", sz);
37453 +       }
37454 +       return err;
37455 +}
37456 +
37457 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37458 +                           ino_t ino)
37459 +{
37460 +       ssize_t sz;
37461 +
37462 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37463 +       if (sz == sizeof(ino))
37464 +               return 0; /* success */
37465 +
37466 +       AuIOErr("write failed (%zd)\n", sz);
37467 +       return -EIO;
37468 +}
37469 +
37470 +/*
37471 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37472 + * at the position of @h_ino.
37473 + * even if @ino is zero, it is written to the xinofile and means no entry.
37474 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37475 + * try truncating it.
37476 + */
37477 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37478 +                 ino_t ino)
37479 +{
37480 +       int err;
37481 +       unsigned int mnt_flags;
37482 +       struct au_xi_calc calc;
37483 +       struct file *file;
37484 +       struct au_branch *br;
37485 +       struct au_xino *xi;
37486 +       struct au_xi_writing *p;
37487 +
37488 +       SiMustAnyLock(sb);
37489 +
37490 +       mnt_flags = au_mntflags(sb);
37491 +       if (!au_opt_test(mnt_flags, XINO))
37492 +               return 0;
37493 +
37494 +       au_xi_calc(sb, h_ino, &calc);
37495 +       br = au_sbr(sb, bindex);
37496 +       xi = br->br_xino;
37497 +       file = au_xino_file(xi, calc.idx);
37498 +       if (!file) {
37499 +               /* store the inum pair into the list */
37500 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37501 +               p->h_ino = h_ino;
37502 +               p->ino = ino;
37503 +               au_hbl_add(&p->node, &xi->xi_writing);
37504 +
37505 +               /* create and write a new xino file asynchronously */
37506 +               err = au_xino_new_async(sb, br, &calc, ino);
37507 +               if (!err)
37508 +                       return 0; /* success */
37509 +               goto out;
37510 +       }
37511 +
37512 +       err = au_xino_do_write(file, &calc, ino);
37513 +       if (!err) {
37514 +               br = au_sbr(sb, bindex);
37515 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37516 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37517 +                       xino_try_trunc(sb, br);
37518 +               return 0; /* success */
37519 +       }
37520 +
37521 +out:
37522 +       AuIOErr("write failed (%d)\n", err);
37523 +       return -EIO;
37524 +}
37525 +
37526 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37527 +                             loff_t *pos);
37528 +
37529 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37530 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37531 +{
37532 +       ssize_t err;
37533 +       int i;
37534 +       const int prevent_endless = 10;
37535 +
37536 +       i = 0;
37537 +       do {
37538 +               err = vfsub_read_k(file, kbuf, size, pos);
37539 +               if (err == -EINTR
37540 +                   && !au_wkq_test()
37541 +                   && fatal_signal_pending(current)) {
37542 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37543 +                       BUG_ON(err == -EINTR);
37544 +               }
37545 +       } while (i++ < prevent_endless
37546 +                && (err == -EAGAIN || err == -EINTR));
37547 +
37548 +#if 0 /* reserved for future use */
37549 +       if (err > 0)
37550 +               fsnotify_access(file->f_path.dentry);
37551 +#endif
37552 +
37553 +       return err;
37554 +}
37555 +
37556 +struct xino_fread_args {
37557 +       ssize_t *errp;
37558 +       struct file *file;
37559 +       void *buf;
37560 +       size_t size;
37561 +       loff_t *pos;
37562 +};
37563 +
37564 +static void call_xino_fread(void *args)
37565 +{
37566 +       struct xino_fread_args *a = args;
37567 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37568 +}
37569 +
37570 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37571 +                             loff_t *pos)
37572 +{
37573 +       ssize_t err;
37574 +       int wkq_err;
37575 +       struct xino_fread_args args = {
37576 +               .errp   = &err,
37577 +               .file   = file,
37578 +               .buf    = buf,
37579 +               .size   = size,
37580 +               .pos    = pos
37581 +       };
37582 +
37583 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37584 +       if (unlikely(wkq_err))
37585 +               err = wkq_err;
37586 +
37587 +       return err;
37588 +}
37589 +
37590 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37591 +                              loff_t *pos);
37592 +
37593 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37594 +                             loff_t *pos)
37595 +{
37596 +       ssize_t err;
37597 +       int i;
37598 +       const int prevent_endless = 10;
37599 +
37600 +       i = 0;
37601 +       do {
37602 +               err = vfsub_write_k(file, kbuf, size, pos);
37603 +               if (err == -EINTR
37604 +                   && !au_wkq_test()
37605 +                   && fatal_signal_pending(current)) {
37606 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37607 +                       BUG_ON(err == -EINTR);
37608 +               }
37609 +       } while (i++ < prevent_endless
37610 +                && (err == -EAGAIN || err == -EINTR));
37611 +
37612 +#if 0 /* reserved for future use */
37613 +       if (err > 0)
37614 +               fsnotify_modify(file->f_path.dentry);
37615 +#endif
37616 +
37617 +       return err;
37618 +}
37619 +
37620 +struct do_xino_fwrite_args {
37621 +       ssize_t *errp;
37622 +       struct file *file;
37623 +       void *buf;
37624 +       size_t size;
37625 +       loff_t *pos;
37626 +};
37627 +
37628 +static void call_do_xino_fwrite(void *args)
37629 +{
37630 +       struct do_xino_fwrite_args *a = args;
37631 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37632 +}
37633 +
37634 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37635 +                              loff_t *pos)
37636 +{
37637 +       ssize_t err;
37638 +       int wkq_err;
37639 +       struct do_xino_fwrite_args args = {
37640 +               .errp   = &err,
37641 +               .file   = file,
37642 +               .buf    = buf,
37643 +               .size   = size,
37644 +               .pos    = pos
37645 +       };
37646 +
37647 +       /*
37648 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37649 +        * users should care about quota and real 'filesystem full.'
37650 +        */
37651 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37652 +       if (unlikely(wkq_err))
37653 +               err = wkq_err;
37654 +
37655 +       return err;
37656 +}
37657 +
37658 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37659 +{
37660 +       ssize_t err;
37661 +
37662 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37663 +               lockdep_off();
37664 +               err = do_xino_fwrite(file, buf, size, pos);
37665 +               lockdep_on();
37666 +       } else {
37667 +               lockdep_off();
37668 +               err = xino_fwrite_wkq(file, buf, size, pos);
37669 +               lockdep_on();
37670 +       }
37671 +
37672 +       return err;
37673 +}
37674 +
37675 +/* ---------------------------------------------------------------------- */
37676 +
37677 +/*
37678 + * inode number bitmap
37679 + */
37680 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37681 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37682 +{
37683 +       ino_t ino;
37684 +
37685 +       AuDebugOn(bit < 0 || page_bits <= bit);
37686 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37687 +       return ino;
37688 +}
37689 +
37690 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37691 +{
37692 +       AuDebugOn(ino < AUFS_FIRST_INO);
37693 +       ino -= AUFS_FIRST_INO;
37694 +       *pindex = ino / page_bits;
37695 +       *bit = ino % page_bits;
37696 +}
37697 +
37698 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37699 +{
37700 +       int err;
37701 +       loff_t pos;
37702 +       ssize_t sz;
37703 +       struct au_sbinfo *sbinfo;
37704 +       struct file *xib;
37705 +       unsigned long *p;
37706 +
37707 +       sbinfo = au_sbi(sb);
37708 +       MtxMustLock(&sbinfo->si_xib_mtx);
37709 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37710 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37711 +
37712 +       if (pindex == sbinfo->si_xib_last_pindex)
37713 +               return 0;
37714 +
37715 +       xib = sbinfo->si_xib;
37716 +       p = sbinfo->si_xib_buf;
37717 +       pos = sbinfo->si_xib_last_pindex;
37718 +       pos *= PAGE_SIZE;
37719 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37720 +       if (unlikely(sz != PAGE_SIZE))
37721 +               goto out;
37722 +
37723 +       pos = pindex;
37724 +       pos *= PAGE_SIZE;
37725 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37726 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37727 +       else {
37728 +               memset(p, 0, PAGE_SIZE);
37729 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37730 +       }
37731 +       if (sz == PAGE_SIZE) {
37732 +               sbinfo->si_xib_last_pindex = pindex;
37733 +               return 0; /* success */
37734 +       }
37735 +
37736 +out:
37737 +       AuIOErr1("write failed (%zd)\n", sz);
37738 +       err = sz;
37739 +       if (sz >= 0)
37740 +               err = -EIO;
37741 +       return err;
37742 +}
37743 +
37744 +static void au_xib_clear_bit(struct inode *inode)
37745 +{
37746 +       int err, bit;
37747 +       unsigned long pindex;
37748 +       struct super_block *sb;
37749 +       struct au_sbinfo *sbinfo;
37750 +
37751 +       AuDebugOn(inode->i_nlink);
37752 +
37753 +       sb = inode->i_sb;
37754 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37755 +       AuDebugOn(page_bits <= bit);
37756 +       sbinfo = au_sbi(sb);
37757 +       mutex_lock(&sbinfo->si_xib_mtx);
37758 +       err = xib_pindex(sb, pindex);
37759 +       if (!err) {
37760 +               clear_bit(bit, sbinfo->si_xib_buf);
37761 +               sbinfo->si_xib_next_bit = bit;
37762 +       }
37763 +       mutex_unlock(&sbinfo->si_xib_mtx);
37764 +}
37765 +
37766 +/* ---------------------------------------------------------------------- */
37767 +
37768 +/*
37769 + * truncate a xino bitmap file
37770 + */
37771 +
37772 +/* todo: slow */
37773 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37774 +{
37775 +       int err, bit;
37776 +       ssize_t sz;
37777 +       unsigned long pindex;
37778 +       loff_t pos, pend;
37779 +       struct au_sbinfo *sbinfo;
37780 +       ino_t *ino;
37781 +       unsigned long *p;
37782 +
37783 +       err = 0;
37784 +       sbinfo = au_sbi(sb);
37785 +       MtxMustLock(&sbinfo->si_xib_mtx);
37786 +       p = sbinfo->si_xib_buf;
37787 +       pend = vfsub_f_size_read(file);
37788 +       pos = 0;
37789 +       while (pos < pend) {
37790 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
37791 +               err = sz;
37792 +               if (unlikely(sz <= 0))
37793 +                       goto out;
37794 +
37795 +               err = 0;
37796 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37797 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37798 +                               continue;
37799 +
37800 +                       xib_calc_bit(*ino, &pindex, &bit);
37801 +                       AuDebugOn(page_bits <= bit);
37802 +                       err = xib_pindex(sb, pindex);
37803 +                       if (!err)
37804 +                               set_bit(bit, p);
37805 +                       else
37806 +                               goto out;
37807 +               }
37808 +       }
37809 +
37810 +out:
37811 +       return err;
37812 +}
37813 +
37814 +static int xib_restore(struct super_block *sb)
37815 +{
37816 +       int err, i;
37817 +       unsigned int nfile;
37818 +       aufs_bindex_t bindex, bbot;
37819 +       void *page;
37820 +       struct au_branch *br;
37821 +       struct au_xino *xi;
37822 +       struct file *file;
37823 +
37824 +       err = -ENOMEM;
37825 +       page = (void *)__get_free_page(GFP_NOFS);
37826 +       if (unlikely(!page))
37827 +               goto out;
37828 +
37829 +       err = 0;
37830 +       bbot = au_sbbot(sb);
37831 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37832 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37833 +                       br = au_sbr(sb, bindex);
37834 +                       xi = br->br_xino;
37835 +                       nfile = xi->xi_nfile;
37836 +                       for (i = 0; i < nfile; i++) {
37837 +                               file = au_xino_file(xi, i);
37838 +                               if (file)
37839 +                                       err = do_xib_restore(sb, file, page);
37840 +                       }
37841 +               } else
37842 +                       AuDbg("skip shared b%d\n", bindex);
37843 +       free_page((unsigned long)page);
37844 +
37845 +out:
37846 +       return err;
37847 +}
37848 +
37849 +int au_xib_trunc(struct super_block *sb)
37850 +{
37851 +       int err;
37852 +       ssize_t sz;
37853 +       loff_t pos;
37854 +       struct au_sbinfo *sbinfo;
37855 +       unsigned long *p;
37856 +       struct file *file;
37857 +
37858 +       SiMustWriteLock(sb);
37859 +
37860 +       err = 0;
37861 +       sbinfo = au_sbi(sb);
37862 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
37863 +               goto out;
37864 +
37865 +       file = sbinfo->si_xib;
37866 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
37867 +               goto out;
37868 +
37869 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37870 +       err = PTR_ERR(file);
37871 +       if (IS_ERR(file))
37872 +               goto out;
37873 +       fput(sbinfo->si_xib);
37874 +       sbinfo->si_xib = file;
37875 +
37876 +       p = sbinfo->si_xib_buf;
37877 +       memset(p, 0, PAGE_SIZE);
37878 +       pos = 0;
37879 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
37880 +       if (unlikely(sz != PAGE_SIZE)) {
37881 +               err = sz;
37882 +               AuIOErr("err %d\n", err);
37883 +               if (sz >= 0)
37884 +                       err = -EIO;
37885 +               goto out;
37886 +       }
37887 +
37888 +       mutex_lock(&sbinfo->si_xib_mtx);
37889 +       /* mnt_want_write() is unnecessary here */
37890 +       err = xib_restore(sb);
37891 +       mutex_unlock(&sbinfo->si_xib_mtx);
37892 +
37893 +out:
37894 +       return err;
37895 +}
37896 +
37897 +/* ---------------------------------------------------------------------- */
37898 +
37899 +struct au_xino *au_xino_alloc(unsigned int nfile)
37900 +{
37901 +       struct au_xino *xi;
37902 +
37903 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
37904 +       if (unlikely(!xi))
37905 +               goto out;
37906 +       xi->xi_nfile = nfile;
37907 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
37908 +       if (unlikely(!xi->xi_file))
37909 +               goto out_free;
37910 +
37911 +       xi->xi_nondir.total = 8; /* initial size */
37912 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37913 +                                     GFP_NOFS);
37914 +       if (unlikely(!xi->xi_nondir.array))
37915 +               goto out_file;
37916 +
37917 +       spin_lock_init(&xi->xi_nondir.spin);
37918 +       init_waitqueue_head(&xi->xi_nondir.wqh);
37919 +       mutex_init(&xi->xi_mtx);
37920 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
37921 +       atomic_set(&xi->xi_truncating, 0);
37922 +       kref_init(&xi->xi_kref);
37923 +       goto out; /* success */
37924 +
37925 +out_file:
37926 +       au_kfree_try_rcu(xi->xi_file);
37927 +out_free:
37928 +       au_kfree_rcu(xi);
37929 +       xi = NULL;
37930 +out:
37931 +       return xi;
37932 +}
37933 +
37934 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
37935 +{
37936 +       int err;
37937 +       struct au_xino *xi;
37938 +
37939 +       err = 0;
37940 +       xi = au_xino_alloc(idx + 1);
37941 +       if (unlikely(!xi)) {
37942 +               err = -ENOMEM;
37943 +               goto out;
37944 +       }
37945 +
37946 +       if (file)
37947 +               get_file(file);
37948 +       xi->xi_file[idx] = file;
37949 +       AuDebugOn(br->br_xino);
37950 +       br->br_xino = xi;
37951 +
37952 +out:
37953 +       return err;
37954 +}
37955 +
37956 +static void au_xino_release(struct kref *kref)
37957 +{
37958 +       struct au_xino *xi;
37959 +       int i;
37960 +       unsigned long ul;
37961 +       struct hlist_bl_head *hbl;
37962 +       struct hlist_bl_node *pos, *n;
37963 +       struct au_xi_writing *p;
37964 +
37965 +       xi = container_of(kref, struct au_xino, xi_kref);
37966 +       for (i = 0; i < xi->xi_nfile; i++)
37967 +               if (xi->xi_file[i])
37968 +                       fput(xi->xi_file[i]);
37969 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37970 +               AuDebugOn(xi->xi_nondir.array[i]);
37971 +       mutex_destroy(&xi->xi_mtx);
37972 +       hbl = &xi->xi_writing;
37973 +       ul = au_hbl_count(hbl);
37974 +       if (unlikely(ul)) {
37975 +               pr_warn("xi_writing %lu\n", ul);
37976 +               hlist_bl_lock(hbl);
37977 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
37978 +                       hlist_bl_del(&p->node);
37979 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
37980 +                       kfree(p);
37981 +               }
37982 +               hlist_bl_unlock(hbl);
37983 +       }
37984 +       au_kfree_try_rcu(xi->xi_file);
37985 +       au_kfree_try_rcu(xi->xi_nondir.array);
37986 +       au_kfree_rcu(xi);
37987 +}
37988 +
37989 +int au_xino_put(struct au_branch *br)
37990 +{
37991 +       int ret;
37992 +       struct au_xino *xi;
37993 +
37994 +       ret = 0;
37995 +       xi = br->br_xino;
37996 +       if (xi) {
37997 +               br->br_xino = NULL;
37998 +               ret = kref_put(&xi->xi_kref, au_xino_release);
37999 +       }
38000 +
38001 +       return ret;
38002 +}
38003 +
38004 +/* ---------------------------------------------------------------------- */
38005 +
38006 +/*
38007 + * xino mount option handlers
38008 + */
38009 +
38010 +/* xino bitmap */
38011 +static void xino_clear_xib(struct super_block *sb)
38012 +{
38013 +       struct au_sbinfo *sbinfo;
38014 +
38015 +       SiMustWriteLock(sb);
38016 +
38017 +       sbinfo = au_sbi(sb);
38018 +       if (sbinfo->si_xib)
38019 +               fput(sbinfo->si_xib);
38020 +       sbinfo->si_xib = NULL;
38021 +       if (sbinfo->si_xib_buf)
38022 +               free_page((unsigned long)sbinfo->si_xib_buf);
38023 +       sbinfo->si_xib_buf = NULL;
38024 +}
38025 +
38026 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38027 +{
38028 +       int err;
38029 +       loff_t pos;
38030 +       struct au_sbinfo *sbinfo;
38031 +       struct file *file;
38032 +       struct super_block *xi_sb;
38033 +
38034 +       SiMustWriteLock(sb);
38035 +
38036 +       sbinfo = au_sbi(sb);
38037 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38038 +       err = PTR_ERR(file);
38039 +       if (IS_ERR(file))
38040 +               goto out;
38041 +       if (sbinfo->si_xib)
38042 +               fput(sbinfo->si_xib);
38043 +       sbinfo->si_xib = file;
38044 +       xi_sb = file_inode(file)->i_sb;
38045 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38046 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38047 +               err = -EIO;
38048 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38049 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38050 +               goto out_unset;
38051 +       }
38052 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38053 +
38054 +       err = -ENOMEM;
38055 +       if (!sbinfo->si_xib_buf)
38056 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38057 +       if (unlikely(!sbinfo->si_xib_buf))
38058 +               goto out_unset;
38059 +
38060 +       sbinfo->si_xib_last_pindex = 0;
38061 +       sbinfo->si_xib_next_bit = 0;
38062 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38063 +               pos = 0;
38064 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38065 +               if (unlikely(err != PAGE_SIZE))
38066 +                       goto out_free;
38067 +       }
38068 +       err = 0;
38069 +       goto out; /* success */
38070 +
38071 +out_free:
38072 +       if (sbinfo->si_xib_buf)
38073 +               free_page((unsigned long)sbinfo->si_xib_buf);
38074 +       sbinfo->si_xib_buf = NULL;
38075 +       if (err >= 0)
38076 +               err = -EIO;
38077 +out_unset:
38078 +       fput(sbinfo->si_xib);
38079 +       sbinfo->si_xib = NULL;
38080 +out:
38081 +       AuTraceErr(err);
38082 +       return err;
38083 +}
38084 +
38085 +/* xino for each branch */
38086 +static void xino_clear_br(struct super_block *sb)
38087 +{
38088 +       aufs_bindex_t bindex, bbot;
38089 +       struct au_branch *br;
38090 +
38091 +       bbot = au_sbbot(sb);
38092 +       for (bindex = 0; bindex <= bbot; bindex++) {
38093 +               br = au_sbr(sb, bindex);
38094 +               AuDebugOn(!br);
38095 +               au_xino_put(br);
38096 +       }
38097 +}
38098 +
38099 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38100 +                                 aufs_bindex_t bshared)
38101 +{
38102 +       struct au_branch *brshared;
38103 +
38104 +       brshared = au_sbr(sb, bshared);
38105 +       AuDebugOn(!brshared->br_xino);
38106 +       AuDebugOn(!brshared->br_xino->xi_file);
38107 +       if (br->br_xino != brshared->br_xino) {
38108 +               au_xino_get(brshared);
38109 +               au_xino_put(br);
38110 +               br->br_xino = brshared->br_xino;
38111 +       }
38112 +}
38113 +
38114 +struct au_xino_do_set_br {
38115 +       struct au_branch *br;
38116 +       ino_t h_ino;
38117 +       aufs_bindex_t bshared;
38118 +};
38119 +
38120 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38121 +                            struct au_xino_do_set_br *args)
38122 +{
38123 +       int err;
38124 +       struct au_xi_calc calc;
38125 +       struct file *file;
38126 +       struct au_branch *br;
38127 +       struct au_xi_new xinew = {
38128 +               .base = path
38129 +       };
38130 +
38131 +       br = args->br;
38132 +       xinew.xi = br->br_xino;
38133 +       au_xi_calc(sb, args->h_ino, &calc);
38134 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38135 +       if (args->bshared >= 0)
38136 +               /* shared xino */
38137 +               au_xino_set_br_shared(sb, br, args->bshared);
38138 +       else if (!xinew.xi) {
38139 +               /* new xino */
38140 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38141 +               if (unlikely(err))
38142 +                       goto out;
38143 +       }
38144 +
38145 +       /* force re-creating */
38146 +       xinew.xi = br->br_xino;
38147 +       xinew.idx = calc.idx;
38148 +       mutex_lock(&xinew.xi->xi_mtx);
38149 +       file = au_xi_new(sb, &xinew);
38150 +       mutex_unlock(&xinew.xi->xi_mtx);
38151 +       err = PTR_ERR(file);
38152 +       if (IS_ERR(file))
38153 +               goto out;
38154 +       AuDebugOn(!file);
38155 +
38156 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38157 +       if (unlikely(err))
38158 +               au_xino_put(br);
38159 +
38160 +out:
38161 +       AuTraceErr(err);
38162 +       return err;
38163 +}
38164 +
38165 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38166 +{
38167 +       int err;
38168 +       aufs_bindex_t bindex, bbot;
38169 +       struct au_xino_do_set_br args;
38170 +       struct inode *inode;
38171 +
38172 +       SiMustWriteLock(sb);
38173 +
38174 +       bbot = au_sbbot(sb);
38175 +       inode = d_inode(sb->s_root);
38176 +       for (bindex = 0; bindex <= bbot; bindex++) {
38177 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38178 +               args.br = au_sbr(sb, bindex);
38179 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38180 +               err = au_xino_do_set_br(sb, path, &args);
38181 +               if (unlikely(err))
38182 +                       break;
38183 +       }
38184 +
38185 +       AuTraceErr(err);
38186 +       return err;
38187 +}
38188 +
38189 +void au_xino_clr(struct super_block *sb)
38190 +{
38191 +       struct au_sbinfo *sbinfo;
38192 +
38193 +       au_xigen_clr(sb);
38194 +       xino_clear_xib(sb);
38195 +       xino_clear_br(sb);
38196 +       dbgaufs_brs_del(sb, 0);
38197 +       sbinfo = au_sbi(sb);
38198 +       /* lvalue, do not call au_mntflags() */
38199 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38200 +}
38201 +
38202 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38203 +{
38204 +       int err, skip;
38205 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38206 +       struct qstr *dname, *cur_name;
38207 +       struct file *cur_xino;
38208 +       struct au_sbinfo *sbinfo;
38209 +       struct path *path, *cur_path;
38210 +
38211 +       SiMustWriteLock(sb);
38212 +
38213 +       err = 0;
38214 +       sbinfo = au_sbi(sb);
38215 +       path = &xiopt->file->f_path;
38216 +       dentry = path->dentry;
38217 +       parent = dget_parent(dentry);
38218 +       if (remount) {
38219 +               skip = 0;
38220 +               cur_xino = sbinfo->si_xib;
38221 +               if (cur_xino) {
38222 +                       cur_path = &cur_xino->f_path;
38223 +                       cur_dentry = cur_path->dentry;
38224 +                       cur_parent = dget_parent(cur_dentry);
38225 +                       cur_name = &cur_dentry->d_name;
38226 +                       dname = &dentry->d_name;
38227 +                       skip = (cur_parent == parent
38228 +                               && au_qstreq(dname, cur_name));
38229 +                       dput(cur_parent);
38230 +               }
38231 +               if (skip)
38232 +                       goto out;
38233 +       }
38234 +
38235 +       au_opt_set(sbinfo->si_mntflags, XINO);
38236 +       err = au_xino_set_xib(sb, path);
38237 +       /* si_x{read,write} are set */
38238 +       if (!err)
38239 +               err = au_xigen_set(sb, path);
38240 +       if (!err)
38241 +               err = au_xino_set_br(sb, path);
38242 +       if (!err) {
38243 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38244 +               goto out; /* success */
38245 +       }
38246 +
38247 +       /* reset all */
38248 +       AuIOErr("failed setting xino(%d).\n", err);
38249 +       au_xino_clr(sb);
38250 +
38251 +out:
38252 +       dput(parent);
38253 +       return err;
38254 +}
38255 +
38256 +/*
38257 + * create a xinofile at the default place/path.
38258 + */
38259 +struct file *au_xino_def(struct super_block *sb)
38260 +{
38261 +       struct file *file;
38262 +       char *page, *p;
38263 +       struct au_branch *br;
38264 +       struct super_block *h_sb;
38265 +       struct path path;
38266 +       aufs_bindex_t bbot, bindex, bwr;
38267 +
38268 +       br = NULL;
38269 +       bbot = au_sbbot(sb);
38270 +       bwr = -1;
38271 +       for (bindex = 0; bindex <= bbot; bindex++) {
38272 +               br = au_sbr(sb, bindex);
38273 +               if (au_br_writable(br->br_perm)
38274 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38275 +                       bwr = bindex;
38276 +                       break;
38277 +               }
38278 +       }
38279 +
38280 +       if (bwr >= 0) {
38281 +               file = ERR_PTR(-ENOMEM);
38282 +               page = (void *)__get_free_page(GFP_NOFS);
38283 +               if (unlikely(!page))
38284 +                       goto out;
38285 +               path.mnt = au_br_mnt(br);
38286 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38287 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38288 +               file = (void *)p;
38289 +               if (!IS_ERR(p)) {
38290 +                       strcat(p, "/" AUFS_XINO_FNAME);
38291 +                       AuDbg("%s\n", p);
38292 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38293 +               }
38294 +               free_page((unsigned long)page);
38295 +       } else {
38296 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38297 +                                     /*wbrtop*/0);
38298 +               if (IS_ERR(file))
38299 +                       goto out;
38300 +               h_sb = file->f_path.dentry->d_sb;
38301 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38302 +                       pr_err("xino doesn't support %s(%s)\n",
38303 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38304 +                       fput(file);
38305 +                       file = ERR_PTR(-EINVAL);
38306 +               }
38307 +       }
38308 +
38309 +out:
38310 +       return file;
38311 +}
38312 +
38313 +/* ---------------------------------------------------------------------- */
38314 +
38315 +/*
38316 + * initialize the xinofile for the specified branch @br
38317 + * at the place/path where @base_file indicates.
38318 + * test whether another branch is on the same filesystem or not,
38319 + * if found then share the xinofile with another branch.
38320 + */
38321 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38322 +                   struct path *base)
38323 +{
38324 +       int err;
38325 +       struct au_xino_do_set_br args = {
38326 +               .h_ino  = h_ino,
38327 +               .br     = br
38328 +       };
38329 +
38330 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38331 +                                      au_br_sb(br));
38332 +       err = au_xino_do_set_br(sb, base, &args);
38333 +       if (unlikely(err))
38334 +               au_xino_put(br);
38335 +
38336 +       return err;
38337 +}
38338 +
38339 +/* ---------------------------------------------------------------------- */
38340 +
38341 +/*
38342 + * get an unused inode number from bitmap
38343 + */
38344 +ino_t au_xino_new_ino(struct super_block *sb)
38345 +{
38346 +       ino_t ino;
38347 +       unsigned long *p, pindex, ul, pend;
38348 +       struct au_sbinfo *sbinfo;
38349 +       struct file *file;
38350 +       int free_bit, err;
38351 +
38352 +       if (!au_opt_test(au_mntflags(sb), XINO))
38353 +               return iunique(sb, AUFS_FIRST_INO);
38354 +
38355 +       sbinfo = au_sbi(sb);
38356 +       mutex_lock(&sbinfo->si_xib_mtx);
38357 +       p = sbinfo->si_xib_buf;
38358 +       free_bit = sbinfo->si_xib_next_bit;
38359 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38360 +               goto out; /* success */
38361 +       free_bit = find_first_zero_bit(p, page_bits);
38362 +       if (free_bit < page_bits)
38363 +               goto out; /* success */
38364 +
38365 +       pindex = sbinfo->si_xib_last_pindex;
38366 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38367 +               err = xib_pindex(sb, ul);
38368 +               if (unlikely(err))
38369 +                       goto out_err;
38370 +               free_bit = find_first_zero_bit(p, page_bits);
38371 +               if (free_bit < page_bits)
38372 +                       goto out; /* success */
38373 +       }
38374 +
38375 +       file = sbinfo->si_xib;
38376 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38377 +       for (ul = pindex + 1; ul <= pend; ul++) {
38378 +               err = xib_pindex(sb, ul);
38379 +               if (unlikely(err))
38380 +                       goto out_err;
38381 +               free_bit = find_first_zero_bit(p, page_bits);
38382 +               if (free_bit < page_bits)
38383 +                       goto out; /* success */
38384 +       }
38385 +       BUG();
38386 +
38387 +out:
38388 +       set_bit(free_bit, p);
38389 +       sbinfo->si_xib_next_bit = free_bit + 1;
38390 +       pindex = sbinfo->si_xib_last_pindex;
38391 +       mutex_unlock(&sbinfo->si_xib_mtx);
38392 +       ino = xib_calc_ino(pindex, free_bit);
38393 +       AuDbg("i%lu\n", (unsigned long)ino);
38394 +       return ino;
38395 +out_err:
38396 +       mutex_unlock(&sbinfo->si_xib_mtx);
38397 +       AuDbg("i0\n");
38398 +       return 0;
38399 +}
38400 +
38401 +/* for s_op->delete_inode() */
38402 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38403 +{
38404 +       int err;
38405 +       unsigned int mnt_flags;
38406 +       aufs_bindex_t bindex, bbot, bi;
38407 +       unsigned char try_trunc;
38408 +       struct au_iinfo *iinfo;
38409 +       struct super_block *sb;
38410 +       struct au_hinode *hi;
38411 +       struct inode *h_inode;
38412 +       struct au_branch *br;
38413 +       struct au_xi_calc calc;
38414 +       struct file *file;
38415 +
38416 +       AuDebugOn(au_is_bad_inode(inode));
38417 +
38418 +       sb = inode->i_sb;
38419 +       mnt_flags = au_mntflags(sb);
38420 +       if (!au_opt_test(mnt_flags, XINO)
38421 +           || inode->i_ino == AUFS_ROOT_INO)
38422 +               return;
38423 +
38424 +       if (unlinked) {
38425 +               au_xigen_inc(inode);
38426 +               au_xib_clear_bit(inode);
38427 +       }
38428 +
38429 +       iinfo = au_ii(inode);
38430 +       bindex = iinfo->ii_btop;
38431 +       if (bindex < 0)
38432 +               return;
38433 +
38434 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38435 +       hi = au_hinode(iinfo, bindex);
38436 +       bbot = iinfo->ii_bbot;
38437 +       for (; bindex <= bbot; bindex++, hi++) {
38438 +               h_inode = hi->hi_inode;
38439 +               if (!h_inode
38440 +                   || (!unlinked && h_inode->i_nlink))
38441 +                       continue;
38442 +
38443 +               /* inode may not be revalidated */
38444 +               bi = au_br_index(sb, hi->hi_id);
38445 +               if (bi < 0)
38446 +                       continue;
38447 +
38448 +               br = au_sbr(sb, bi);
38449 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38450 +               file = au_xino_file(br->br_xino, calc.idx);
38451 +               if (IS_ERR_OR_NULL(file))
38452 +                       continue;
38453 +
38454 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38455 +               if (!err && try_trunc
38456 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38457 +                       xino_try_trunc(sb, br);
38458 +       }
38459 +}
38460 +
38461 +/* ---------------------------------------------------------------------- */
38462 +
38463 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38464 +{
38465 +       int found, total, i;
38466 +
38467 +       found = -1;
38468 +       total = xi->xi_nondir.total;
38469 +       for (i = 0; i < total; i++) {
38470 +               if (xi->xi_nondir.array[i] != h_ino)
38471 +                       continue;
38472 +               found = i;
38473 +               break;
38474 +       }
38475 +
38476 +       return found;
38477 +}
38478 +
38479 +static int au_xinondir_expand(struct au_xino *xi)
38480 +{
38481 +       int err, sz;
38482 +       ino_t *p;
38483 +
38484 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38485 +
38486 +       err = -ENOMEM;
38487 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38488 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38489 +               goto out;
38490 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38491 +                        /*may_shrink*/0);
38492 +       if (p) {
38493 +               xi->xi_nondir.array = p;
38494 +               xi->xi_nondir.total <<= 1;
38495 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38496 +               err = 0;
38497 +       }
38498 +
38499 +out:
38500 +       return err;
38501 +}
38502 +
38503 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38504 +                      ino_t h_ino, int idx)
38505 +{
38506 +       struct au_xino *xi;
38507 +
38508 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38509 +       xi = au_sbr(sb, bindex)->br_xino;
38510 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38511 +
38512 +       spin_lock(&xi->xi_nondir.spin);
38513 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38514 +       xi->xi_nondir.array[idx] = 0;
38515 +       spin_unlock(&xi->xi_nondir.spin);
38516 +       wake_up_all(&xi->xi_nondir.wqh);
38517 +}
38518 +
38519 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38520 +                     int *idx)
38521 +{
38522 +       int err, found, empty;
38523 +       struct au_xino *xi;
38524 +
38525 +       err = 0;
38526 +       *idx = -1;
38527 +       if (!au_opt_test(au_mntflags(sb), XINO))
38528 +               goto out; /* no xino */
38529 +
38530 +       xi = au_sbr(sb, bindex)->br_xino;
38531 +
38532 +again:
38533 +       spin_lock(&xi->xi_nondir.spin);
38534 +       found = au_xinondir_find(xi, h_ino);
38535 +       if (found == -1) {
38536 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38537 +               if (empty == -1) {
38538 +                       empty = xi->xi_nondir.total;
38539 +                       err = au_xinondir_expand(xi);
38540 +                       if (unlikely(err))
38541 +                               goto out_unlock;
38542 +               }
38543 +               xi->xi_nondir.array[empty] = h_ino;
38544 +               *idx = empty;
38545 +       } else {
38546 +               spin_unlock(&xi->xi_nondir.spin);
38547 +               wait_event(xi->xi_nondir.wqh,
38548 +                          xi->xi_nondir.array[found] != h_ino);
38549 +               goto again;
38550 +       }
38551 +
38552 +out_unlock:
38553 +       spin_unlock(&xi->xi_nondir.spin);
38554 +out:
38555 +       return err;
38556 +}
38557 +
38558 +/* ---------------------------------------------------------------------- */
38559 +
38560 +int au_xino_path(struct seq_file *seq, struct file *file)
38561 +{
38562 +       int err;
38563 +
38564 +       err = au_seq_path(seq, &file->f_path);
38565 +       if (unlikely(err))
38566 +               goto out;
38567 +
38568 +#define Deleted "\\040(deleted)"
38569 +       seq->count -= sizeof(Deleted) - 1;
38570 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38571 +                        sizeof(Deleted) - 1));
38572 +#undef Deleted
38573 +
38574 +out:
38575 +       return err;
38576 +}
38577 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38578 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38579 +++ linux/include/uapi/linux/aufs_type.h        2021-02-24 13:33:42.751013936 +0100
38580 @@ -0,0 +1,452 @@
38581 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38582 +/*
38583 + * Copyright (C) 2005-2020 Junjiro R. Okajima
38584 + *
38585 + * This program, aufs is free software; you can redistribute it and/or modify
38586 + * it under the terms of the GNU General Public License as published by
38587 + * the Free Software Foundation; either version 2 of the License, or
38588 + * (at your option) any later version.
38589 + *
38590 + * This program is distributed in the hope that it will be useful,
38591 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38592 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38593 + * GNU General Public License for more details.
38594 + *
38595 + * You should have received a copy of the GNU General Public License
38596 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38597 + */
38598 +
38599 +#ifndef __AUFS_TYPE_H__
38600 +#define __AUFS_TYPE_H__
38601 +
38602 +#define AUFS_NAME      "aufs"
38603 +
38604 +#ifdef __KERNEL__
38605 +/*
38606 + * define it before including all other headers.
38607 + * sched.h may use pr_* macros before defining "current", so define the
38608 + * no-current version first, and re-define later.
38609 + */
38610 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38611 +#include <linux/sched.h>
38612 +#undef pr_fmt
38613 +#define pr_fmt(fmt) \
38614 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38615 +               (int)sizeof(current->comm), current->comm, current->pid
38616 +#include <linux/limits.h>
38617 +#else
38618 +#include <stdint.h>
38619 +#include <sys/types.h>
38620 +#include <limits.h>
38621 +#endif /* __KERNEL__ */
38622 +
38623 +#define AUFS_VERSION   "5.11-20210222"
38624 +
38625 +/* todo? move this to linux-2.6.19/include/magic.h */
38626 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38627 +
38628 +/* ---------------------------------------------------------------------- */
38629 +
38630 +#ifdef __KERNEL__
38631 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38632 +typedef int8_t aufs_bindex_t;
38633 +#define AUFS_BRANCH_MAX 127
38634 +#else
38635 +typedef int16_t aufs_bindex_t;
38636 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38637 +#define AUFS_BRANCH_MAX 511
38638 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38639 +#define AUFS_BRANCH_MAX 1023
38640 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38641 +#define AUFS_BRANCH_MAX 32767
38642 +#endif
38643 +#endif
38644 +
38645 +#ifndef AUFS_BRANCH_MAX
38646 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38647 +#endif
38648 +#endif /* __KERNEL__ */
38649 +
38650 +/* ---------------------------------------------------------------------- */
38651 +
38652 +#define AUFS_FSTYPE            AUFS_NAME
38653 +
38654 +#define AUFS_ROOT_INO          2
38655 +#define AUFS_FIRST_INO         11
38656 +
38657 +#define AUFS_WH_PFX            ".wh."
38658 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38659 +#define AUFS_WH_TMP_LEN                4
38660 +/* a limit for rmdir/rename a dir and copyup */
38661 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38662 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38663 +                               - 1                     /* dot */\
38664 +                               - AUFS_WH_TMP_LEN)      /* hex */
38665 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38666 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38667 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38668 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38669 +#define AUFS_DIRWH_DEF         3
38670 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38671 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38672 +#define AUFS_RDBLK_DEF         512 /* bytes */
38673 +#define AUFS_RDHASH_DEF                32
38674 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38675 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38676 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38677 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38678 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38679 +
38680 +/* pseudo-link maintenace under /proc */
38681 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38682 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38683 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38684 +
38685 +/* dirren, renamed dir */
38686 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38687 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38688 +/* whiteouted doubly */
38689 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38690 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38691 +
38692 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38693 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38694 +
38695 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38696 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38697 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38698 +
38699 +/* doubly whiteouted */
38700 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38701 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38702 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38703 +
38704 +/* branch permissions and attributes */
38705 +#define AUFS_BRPERM_RW         "rw"
38706 +#define AUFS_BRPERM_RO         "ro"
38707 +#define AUFS_BRPERM_RR         "rr"
38708 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38709 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38710 +#define AUFS_BRATTR_FHSM       "fhsm"
38711 +#define AUFS_BRATTR_UNPIN      "unpin"
38712 +#define AUFS_BRATTR_ICEX       "icex"
38713 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38714 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38715 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38716 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38717 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38718 +#define AUFS_BRRATTR_WH                "wh"
38719 +#define AUFS_BRWATTR_NLWH      "nolwh"
38720 +#define AUFS_BRWATTR_MOO       "moo"
38721 +
38722 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38723 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38724 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38725 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38726 +
38727 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38728 +#define AuBrAttr_COO_ALL       (1 << 4)
38729 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38730 +
38731 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38732 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38733 +                                                  branch. meaningless since
38734 +                                                  linux-3.18-rc1 */
38735 +
38736 +/* ignore error in copying XATTR */
38737 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38738 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38739 +#define AuBrAttr_ICEX_TR       (1 << 9)
38740 +#define AuBrAttr_ICEX_USR      (1 << 10)
38741 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38742 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38743 +                                | AuBrAttr_ICEX_SYS    \
38744 +                                | AuBrAttr_ICEX_TR     \
38745 +                                | AuBrAttr_ICEX_USR    \
38746 +                                | AuBrAttr_ICEX_OTH)
38747 +
38748 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38749 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38750 +
38751 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38752 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38753 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38754 +
38755 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38756 +
38757 +/* #warning test userspace */
38758 +#ifdef __KERNEL__
38759 +#ifndef CONFIG_AUFS_FHSM
38760 +#undef AuBrAttr_FHSM
38761 +#define AuBrAttr_FHSM          0
38762 +#endif
38763 +#ifndef CONFIG_AUFS_XATTR
38764 +#undef AuBrAttr_ICEX
38765 +#define AuBrAttr_ICEX          0
38766 +#undef AuBrAttr_ICEX_SEC
38767 +#define AuBrAttr_ICEX_SEC      0
38768 +#undef AuBrAttr_ICEX_SYS
38769 +#define AuBrAttr_ICEX_SYS      0
38770 +#undef AuBrAttr_ICEX_TR
38771 +#define AuBrAttr_ICEX_TR       0
38772 +#undef AuBrAttr_ICEX_USR
38773 +#define AuBrAttr_ICEX_USR      0
38774 +#undef AuBrAttr_ICEX_OTH
38775 +#define AuBrAttr_ICEX_OTH      0
38776 +#endif
38777 +#endif
38778 +
38779 +/* the longest combination */
38780 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38781 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38782 +                              "+" AUFS_BRATTR_COO_REG          \
38783 +                              "+" AUFS_BRATTR_FHSM             \
38784 +                              "+" AUFS_BRATTR_UNPIN            \
38785 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38786 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38787 +                              "+" AUFS_BRATTR_ICEX_USR         \
38788 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38789 +                              "+" AUFS_BRWATTR_NLWH)
38790 +
38791 +typedef struct {
38792 +       char a[AuBrPermStrSz];
38793 +} au_br_perm_str_t;
38794 +
38795 +static inline int au_br_writable(int brperm)
38796 +{
38797 +       return brperm & AuBrPerm_RW;
38798 +}
38799 +
38800 +static inline int au_br_whable(int brperm)
38801 +{
38802 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38803 +}
38804 +
38805 +static inline int au_br_wh_linkable(int brperm)
38806 +{
38807 +       return !(brperm & AuBrWAttr_NoLinkWH);
38808 +}
38809 +
38810 +static inline int au_br_cmoo(int brperm)
38811 +{
38812 +       return brperm & AuBrAttr_CMOO_Mask;
38813 +}
38814 +
38815 +static inline int au_br_fhsm(int brperm)
38816 +{
38817 +       return brperm & AuBrAttr_FHSM;
38818 +}
38819 +
38820 +/* ---------------------------------------------------------------------- */
38821 +
38822 +/* ioctl */
38823 +enum {
38824 +       /* readdir in userspace */
38825 +       AuCtl_RDU,
38826 +       AuCtl_RDU_INO,
38827 +
38828 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38829 +       AuCtl_IBUSY,    /* busy inode */
38830 +       AuCtl_MVDOWN,   /* move-down */
38831 +       AuCtl_BR,       /* info about branches */
38832 +       AuCtl_FHSM_FD   /* connection for fhsm */
38833 +};
38834 +
38835 +/* borrowed from linux/include/linux/kernel.h */
38836 +#ifndef ALIGN
38837 +#ifdef _GNU_SOURCE
38838 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38839 +#else
38840 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
38841 +#endif
38842 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38843 +#endif
38844 +
38845 +/* borrowed from linux/include/linux/compiler-gcc3.h */
38846 +#ifndef __aligned
38847 +#define __aligned(x)                   __attribute__((aligned(x)))
38848 +#endif
38849 +
38850 +#ifdef __KERNEL__
38851 +#ifndef __packed
38852 +#define __packed                       __attribute__((packed))
38853 +#endif
38854 +#endif
38855 +
38856 +struct au_rdu_cookie {
38857 +       uint64_t        h_pos;
38858 +       int16_t         bindex;
38859 +       uint8_t         flags;
38860 +       uint8_t         pad;
38861 +       uint32_t        generation;
38862 +} __aligned(8);
38863 +
38864 +struct au_rdu_ent {
38865 +       uint64_t        ino;
38866 +       int16_t         bindex;
38867 +       uint8_t         type;
38868 +       uint8_t         nlen;
38869 +       uint8_t         wh;
38870 +       char            name[];
38871 +} __aligned(8);
38872 +
38873 +static inline int au_rdu_len(int nlen)
38874 +{
38875 +       /* include the terminating NULL */
38876 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38877 +                    sizeof(uint64_t));
38878 +}
38879 +
38880 +union au_rdu_ent_ul {
38881 +       struct au_rdu_ent __user        *e;
38882 +       uint64_t                        ul;
38883 +};
38884 +
38885 +enum {
38886 +       AufsCtlRduV_SZ,
38887 +       AufsCtlRduV_End
38888 +};
38889 +
38890 +struct aufs_rdu {
38891 +       /* input */
38892 +       union {
38893 +               uint64_t        sz;     /* AuCtl_RDU */
38894 +               uint64_t        nent;   /* AuCtl_RDU_INO */
38895 +       };
38896 +       union au_rdu_ent_ul     ent;
38897 +       uint16_t                verify[AufsCtlRduV_End];
38898 +
38899 +       /* input/output */
38900 +       uint32_t                blk;
38901 +
38902 +       /* output */
38903 +       union au_rdu_ent_ul     tail;
38904 +       /* number of entries which were added in a single call */
38905 +       uint64_t                rent;
38906 +       uint8_t                 full;
38907 +       uint8_t                 shwh;
38908 +
38909 +       struct au_rdu_cookie    cookie;
38910 +} __aligned(8);
38911 +
38912 +/* ---------------------------------------------------------------------- */
38913 +
38914 +/* dirren. the branch is identified by the filename who contains this */
38915 +struct au_drinfo {
38916 +       uint64_t ino;
38917 +       union {
38918 +               uint8_t oldnamelen;
38919 +               uint64_t _padding;
38920 +       };
38921 +       uint8_t oldname[];
38922 +} __aligned(8);
38923 +
38924 +struct au_drinfo_fdata {
38925 +       uint32_t magic;
38926 +       struct au_drinfo drinfo;
38927 +} __aligned(8);
38928 +
38929 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38930 +/* future */
38931 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38932 +
38933 +/* ---------------------------------------------------------------------- */
38934 +
38935 +struct aufs_wbr_fd {
38936 +       uint32_t        oflags;
38937 +       int16_t         brid;
38938 +} __aligned(8);
38939 +
38940 +/* ---------------------------------------------------------------------- */
38941 +
38942 +struct aufs_ibusy {
38943 +       uint64_t        ino, h_ino;
38944 +       int16_t         bindex;
38945 +} __aligned(8);
38946 +
38947 +/* ---------------------------------------------------------------------- */
38948 +
38949 +/* error code for move-down */
38950 +/* the actual message strings are implemented in aufs-util.git */
38951 +enum {
38952 +       EAU_MVDOWN_OPAQUE = 1,
38953 +       EAU_MVDOWN_WHITEOUT,
38954 +       EAU_MVDOWN_UPPER,
38955 +       EAU_MVDOWN_BOTTOM,
38956 +       EAU_MVDOWN_NOUPPER,
38957 +       EAU_MVDOWN_NOLOWERBR,
38958 +       EAU_Last
38959 +};
38960 +
38961 +/* flags for move-down */
38962 +#define AUFS_MVDOWN_DMSG       1
38963 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
38964 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
38965 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
38966 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
38967 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
38968 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
38969 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
38970 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
38971 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
38972 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
38973 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
38974 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
38975 +
38976 +/* index for move-down */
38977 +enum {
38978 +       AUFS_MVDOWN_UPPER,
38979 +       AUFS_MVDOWN_LOWER,
38980 +       AUFS_MVDOWN_NARRAY
38981 +};
38982 +
38983 +/*
38984 + * additional info of move-down
38985 + * number of free blocks and inodes.
38986 + * subset of struct kstatfs, but smaller and always 64bit.
38987 + */
38988 +struct aufs_stfs {
38989 +       uint64_t        f_blocks;
38990 +       uint64_t        f_bavail;
38991 +       uint64_t        f_files;
38992 +       uint64_t        f_ffree;
38993 +};
38994 +
38995 +struct aufs_stbr {
38996 +       int16_t                 brid;   /* optional input */
38997 +       int16_t                 bindex; /* output */
38998 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
38999 +} __aligned(8);
39000 +
39001 +struct aufs_mvdown {
39002 +       uint32_t                flags;                  /* input/output */
39003 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39004 +       int8_t                  au_errno;               /* output */
39005 +} __aligned(8);
39006 +
39007 +/* ---------------------------------------------------------------------- */
39008 +
39009 +union aufs_brinfo {
39010 +       /* PATH_MAX may differ between kernel-space and user-space */
39011 +       char    _spacer[4096];
39012 +       struct {
39013 +               int16_t id;
39014 +               int     perm;
39015 +               char    path[];
39016 +       };
39017 +} __aligned(8);
39018 +
39019 +/* ---------------------------------------------------------------------- */
39020 +
39021 +#define AuCtlType              'A'
39022 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39023 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39024 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39025 +                                    struct aufs_wbr_fd)
39026 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39027 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39028 +                                     struct aufs_mvdown)
39029 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39030 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39031 +
39032 +#endif /* __AUFS_TYPE_H__ */
39033 SPDX-License-Identifier: GPL-2.0
39034 aufs5.11 loopback patch
39035
39036 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39037 index 39d539df0349d..4109efe2f6a3a 100644
39038 --- a/drivers/block/loop.c
39039 +++ b/drivers/block/loop.c
39040 @@ -646,6 +646,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39041                                 lo->use_dio);
39042  }
39043  
39044 +static struct file *loop_real_file(struct file *file)
39045 +{
39046 +       struct file *f = NULL;
39047 +
39048 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39049 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39050 +       return f;
39051 +}
39052 +
39053  static void loop_reread_partitions(struct loop_device *lo,
39054                                    struct block_device *bdev)
39055  {
39056 @@ -701,6 +710,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39057                           unsigned int arg)
39058  {
39059         struct file     *file = NULL, *old_file;
39060 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39061         int             error;
39062         bool            partscan;
39063  
39064 @@ -720,12 +730,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39065         file = fget(arg);
39066         if (!file)
39067                 goto out_err;
39068 +       f = loop_real_file(file);
39069 +       if (f) {
39070 +               virt_file = file;
39071 +               file = f;
39072 +               get_file(file);
39073 +       }
39074  
39075         error = loop_validate_file(file, bdev);
39076         if (error)
39077                 goto out_err;
39078  
39079         old_file = lo->lo_backing_file;
39080 +       old_virt_file = lo->lo_backing_virt_file;
39081  
39082         error = -EINVAL;
39083  
39084 @@ -737,6 +754,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39085         blk_mq_freeze_queue(lo->lo_queue);
39086         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39087         lo->lo_backing_file = file;
39088 +       lo->lo_backing_virt_file = virt_file;
39089         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39090         mapping_set_gfp_mask(file->f_mapping,
39091                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39092 @@ -750,6 +768,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39093          * dependency.
39094          */
39095         fput(old_file);
39096 +       if (old_virt_file)
39097 +               fput(old_virt_file);
39098         if (partscan)
39099                 loop_reread_partitions(lo, bdev);
39100         return 0;
39101 @@ -758,6 +778,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39102         mutex_unlock(&loop_ctl_mutex);
39103         if (file)
39104                 fput(file);
39105 +       if (virt_file)
39106 +               fput(virt_file);
39107         return error;
39108  }
39109  
39110 @@ -1084,7 +1106,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39111                           struct block_device *bdev,
39112                           const struct loop_config *config)
39113  {
39114 -       struct file     *file;
39115 +       struct file     *file, *f, *virt_file = NULL;
39116         struct inode    *inode;
39117         struct address_space *mapping;
39118         int             error;
39119 @@ -1099,6 +1121,12 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39120         file = fget(config->fd);
39121         if (!file)
39122                 goto out;
39123 +       f = loop_real_file(file);
39124 +       if (f) {
39125 +               virt_file = file;
39126 +               file = f;
39127 +               get_file(file);
39128 +       }
39129  
39130         /*
39131          * If we don't hold exclusive handle for the device, upgrade to it
39132 @@ -1153,6 +1181,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39133         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39134         lo->lo_device = bdev;
39135         lo->lo_backing_file = file;
39136 +       lo->lo_backing_virt_file = virt_file;
39137         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39138         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39139  
39140 @@ -1203,6 +1232,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39141                 bd_abort_claiming(bdev, loop_configure);
39142  out_putf:
39143         fput(file);
39144 +       if (virt_file)
39145 +               fput(virt_file);
39146  out:
39147         /* This is safe: open() is still holding a reference. */
39148         module_put(THIS_MODULE);
39149 @@ -1212,6 +1243,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39150  static int __loop_clr_fd(struct loop_device *lo, bool release)
39151  {
39152         struct file *filp = NULL;
39153 +       struct file *virt_filp = lo->lo_backing_virt_file;
39154         gfp_t gfp = lo->old_gfp_mask;
39155         struct block_device *bdev = lo->lo_device;
39156         int err = 0;
39157 @@ -1235,6 +1267,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39158  
39159         spin_lock_irq(&lo->lo_lock);
39160         lo->lo_backing_file = NULL;
39161 +       lo->lo_backing_virt_file = NULL;
39162         spin_unlock_irq(&lo->lo_lock);
39163  
39164         loop_release_xfer(lo);
39165 @@ -1317,6 +1350,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39166          */
39167         if (filp)
39168                 fput(filp);
39169 +       if (virt_filp)
39170 +               fput(virt_filp);
39171         return err;
39172  }
39173  
39174 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39175 index af75a5ee40944..1d847cb194ff6 100644
39176 --- a/drivers/block/loop.h
39177 +++ b/drivers/block/loop.h
39178 @@ -46,7 +46,7 @@ struct loop_device {
39179         int             (*ioctl)(struct loop_device *, int cmd, 
39180                                  unsigned long arg); 
39181  
39182 -       struct file *   lo_backing_file;
39183 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39184         struct block_device *lo_device;
39185         void            *key_data; 
39186  
39187 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39188 index 5ef279a59bb14..6447c37f98756 100644
39189 --- a/fs/aufs/f_op.c
39190 +++ b/fs/aufs/f_op.c
39191 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39192         if (IS_ERR(h_file))
39193                 goto out;
39194  
39195 -       if (au_test_loopback_kthread()) {
39196 +       if (0 && au_test_loopback_kthread()) {
39197                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39198                 if (file->f_mapping != h_file->f_mapping) {
39199                         file->f_mapping = h_file->f_mapping;
39200 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39201 index a8b63acc62045..9d97c3af5686a 100644
39202 --- a/fs/aufs/loop.c
39203 +++ b/fs/aufs/loop.c
39204 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39205                 symbol_put(loop_backing_file);
39206         au_kfree_try_rcu(au_warn_loopback_array);
39207  }
39208 +
39209 +/* ---------------------------------------------------------------------- */
39210 +
39211 +/* support the loopback block device insude aufs */
39212 +
39213 +struct file *aufs_real_loop(struct file *file)
39214 +{
39215 +       struct file *f;
39216 +
39217 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39218 +       fi_read_lock(file);
39219 +       f = au_hf_top(file);
39220 +       fi_read_unlock(file);
39221 +       AuDebugOn(!f);
39222 +       return f;
39223 +}
39224 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39225 index 94f4f80ae33bf..ca1194354aff4 100644
39226 --- a/fs/aufs/loop.h
39227 +++ b/fs/aufs/loop.h
39228 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39229  
39230  int au_loopback_init(void);
39231  void au_loopback_fin(void);
39232 +
39233 +struct file *aufs_real_loop(struct file *file);
39234  #else
39235  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39236  
39237 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39238  
39239  AuStubInt0(au_loopback_init, void)
39240  AuStubVoid(au_loopback_fin, void)
39241 +
39242 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39243  #endif /* BLK_DEV_LOOP */
39244  
39245  #endif /* __KERNEL__ */
39246 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39247 index 589dd01220201..801e0a7faec59 100644
39248 --- a/fs/aufs/super.c
39249 +++ b/fs/aufs/super.c
39250 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39251         .statfs         = aufs_statfs,
39252         .put_super      = aufs_put_super,
39253         .sync_fs        = aufs_sync_fs,
39254 -       .remount_fs     = aufs_remount_fs
39255 +       .remount_fs     = aufs_remount_fs,
39256 +#ifdef CONFIG_AUFS_BDEV_LOOP
39257 +       .real_loop      = aufs_real_loop
39258 +#endif
39259  };
39260  
39261  /* ---------------------------------------------------------------------- */
39262 diff --git a/include/linux/fs.h b/include/linux/fs.h
39263 index 91f3fbe5b57ff..ad1b2e44d538b 100644
39264 --- a/include/linux/fs.h
39265 +++ b/include/linux/fs.h
39266 @@ -1965,6 +1965,10 @@ struct super_operations {
39267                                   struct shrink_control *);
39268         long (*free_cached_objects)(struct super_block *,
39269                                     struct shrink_control *);
39270 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39271 +       /* and aufs */
39272 +       struct file *(*real_loop)(struct file *);
39273 +#endif
39274  };
39275  
39276  /*
This page took 4.85473 seconds and 3 git commands to generate.