]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
- up to 5.5.2
[packages/kernel.git] / kernel-aufs5.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs5.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index 7b623e9fc1b0..9070b9159188 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -264,6 +264,7 @@ source "fs/pstore/Kconfig"
9  source "fs/sysv/Kconfig"
10  source "fs/ufs/Kconfig"
11  source "fs/erofs/Kconfig"
12 +source "fs/aufs/Kconfig"
13  
14  endif # MISC_FILESYSTEMS
15  
16 diff --git a/fs/Makefile b/fs/Makefile
17 index 1148c555c4d3..ed6ff7ddc478 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -133,3 +133,4 @@ obj-$(CONFIG_CEPH_FS)               += ceph/
21  obj-$(CONFIG_PSTORE)           += pstore/
22  obj-$(CONFIG_EFIVAR_FS)                += efivarfs/
23  obj-$(CONFIG_EROFS_FS)         += erofs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs5.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index cf6ccca6e61c..c3b9b0d405fd 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2877,6 +2877,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 +W:     http://aufs.sourceforge.net
41 +T:     git://github.com/sfjro/aufs4-linux.git
42 +S:     Supported
43 +F:     Documentation/filesystems/aufs/
44 +F:     Documentation/ABI/testing/debugfs-aufs
45 +F:     Documentation/ABI/testing/sysfs-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 739b372a5112..872e3b3a7693 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -750,6 +750,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 b280e07e162b..046000653e4d 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1266,7 +1266,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 9bc167562ee8..b0935c67bd1e 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 96d62d97694e..31796c1040d6 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1680,7 +1680,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         int (*update_time)(struct inode *, struct timespec64 *, int);
128  
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index 5e1bf611a9eb..c05ac2488abb 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -776,6 +776,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/read_write.c b/fs/read_write.c
147 index 5bbf587f5bc1..fa9b3994b34c 100644
148 --- a/fs/read_write.c
149 +++ b/fs/read_write.c
150 @@ -498,6 +498,28 @@ static ssize_t __vfs_write(struct file *file, const char __user *p,
151                 return -EINVAL;
152  }
153  
154 +vfs_readf_t vfs_readf(struct file *file)
155 +{
156 +       const struct file_operations *fop = file->f_op;
157 +
158 +       if (fop->read)
159 +               return fop->read;
160 +       if (fop->read_iter)
161 +               return new_sync_read;
162 +       return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
163 +}
164 +
165 +vfs_writef_t vfs_writef(struct file *file)
166 +{
167 +       const struct file_operations *fop = file->f_op;
168 +
169 +       if (fop->write)
170 +               return fop->write;
171 +       if (fop->write_iter)
172 +               return new_sync_write;
173 +       return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
174 +}
175 +
176  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
177  {
178         mm_segment_t old_fs;
179 diff --git a/fs/splice.c b/fs/splice.c
180 index 3009652a41c8..d9bcc4a71a0a 100644
181 --- a/fs/splice.c
182 +++ b/fs/splice.c
183 @@ -849,8 +849,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
184  /*
185   * Attempt to initiate a splice from pipe to file.
186   */
187 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188 -                          loff_t *ppos, size_t len, unsigned int flags)
189 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
190 +                   loff_t *ppos, size_t len, unsigned int flags)
191  {
192         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
193                                 loff_t *, size_t, unsigned int);
194 @@ -866,9 +866,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
195  /*
196   * Attempt to initiate a splice from a file to a pipe.
197   */
198 -static long do_splice_to(struct file *in, loff_t *ppos,
199 -                        struct pipe_inode_info *pipe, size_t len,
200 -                        unsigned int flags)
201 +long do_splice_to(struct file *in, loff_t *ppos,
202 +                 struct pipe_inode_info *pipe, size_t len,
203 +                 unsigned int flags)
204  {
205         ssize_t (*splice_read)(struct file *, loff_t *,
206                                struct pipe_inode_info *, size_t, unsigned int);
207 diff --git a/fs/sync.c b/fs/sync.c
208 index 4d1ff010bc5a..457f4e4a5cc1 100644
209 --- a/fs/sync.c
210 +++ b/fs/sync.c
211 @@ -28,7 +28,7 @@
212   * wait == 1 case since in that case write_inode() functions do
213   * sync_dirty_buffer() and thus effectively write one block at a time.
214   */
215 -static int __sync_filesystem(struct super_block *sb, int wait)
216 +int __sync_filesystem(struct super_block *sb, int wait)
217  {
218         if (wait)
219                 sync_inodes_sb(sb);
220 diff --git a/include/linux/fs.h b/include/linux/fs.h
221 index 98e0349adb52..5fc7807f7c2e 100644
222 --- a/include/linux/fs.h
223 +++ b/include/linux/fs.h
224 @@ -1349,6 +1349,7 @@ extern void fasync_free(struct fasync_struct *);
225  /* can be called from interrupts */
226  extern void kill_fasync(struct fasync_struct **, int, int);
227  
228 +extern int setfl(int fd, struct file *filp, unsigned long arg);
229  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
230  extern int f_setown(struct file *filp, unsigned long arg, int force);
231  extern void f_delown(struct file *filp);
232 @@ -1842,6 +1843,7 @@ struct file_operations {
233         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
234         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
235         int (*check_flags)(int);
236 +       int (*setfl)(struct file *, unsigned long);
237         int (*flock) (struct file *, int, struct file_lock *);
238         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
239         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
240 @@ -1912,6 +1914,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
241                               struct iovec *fast_pointer,
242                               struct iovec **ret_pointer);
243  
244 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
245 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
246 +                               loff_t *);
247 +vfs_readf_t vfs_readf(struct file *file);
248 +vfs_writef_t vfs_writef(struct file *file);
249 +
250  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
251  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
252  extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
253 @@ -2332,6 +2340,7 @@ extern int current_umask(void);
254  extern void ihold(struct inode * inode);
255  extern void iput(struct inode *);
256  extern int generic_update_time(struct inode *, struct timespec64 *, int);
257 +extern int update_time(struct inode *, struct timespec64 *, int);
258  
259  /* /sys/fs */
260  extern struct kobject *fs_kobj;
261 @@ -2620,6 +2629,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
262         return false;
263  }
264  #endif
265 +extern int __sync_filesystem(struct super_block *, int);
266  extern int sync_filesystem(struct super_block *);
267  extern const struct file_operations def_blk_fops;
268  extern const struct file_operations def_chr_fops;
269 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
270 index c50d01ef1414..4f4677020360 100644
271 --- a/include/linux/lockdep.h
272 +++ b/include/linux/lockdep.h
273 @@ -331,6 +331,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
274         return lock->key == key;
275  }
276  
277 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
278 +
279  /*
280   * Acquire a lock.
281   *
282 @@ -472,6 +474,7 @@ struct lockdep_map { };
283  
284  #define lockdep_depth(tsk)     (0)
285  
286 +#define lockdep_is_held(lock)                  (1)
287  #define lockdep_is_held_type(l, r)             (1)
288  
289  #define lockdep_assert_held(l)                 do { (void)(l); } while (0)
290 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
291 index 35942084cd40..24f5fd1a789d 100644
292 --- a/include/linux/mnt_namespace.h
293 +++ b/include/linux/mnt_namespace.h
294 @@ -6,11 +6,14 @@
295  struct mnt_namespace;
296  struct fs_struct;
297  struct user_namespace;
298 +struct vfsmount;
299  
300  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
301                 struct user_namespace *, struct fs_struct *);
302  extern void put_mnt_ns(struct mnt_namespace *ns);
303  
304 +extern int is_current_mnt_ns(struct vfsmount *mnt);
305 +
306  extern const struct file_operations proc_mounts_operations;
307  extern const struct file_operations proc_mountinfo_operations;
308  extern const struct file_operations proc_mountstats_operations;
309 diff --git a/include/linux/splice.h b/include/linux/splice.h
310 index 74b4911ac16d..19789fbea567 100644
311 --- a/include/linux/splice.h
312 +++ b/include/linux/splice.h
313 @@ -87,4 +87,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
314  
315  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
316  extern const struct pipe_buf_operations default_pipe_buf_ops;
317 +
318 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
319 +                          loff_t *ppos, size_t len, unsigned int flags);
320 +extern long do_splice_to(struct file *in, loff_t *ppos,
321 +                        struct pipe_inode_info *pipe, size_t len,
322 +                        unsigned int flags);
323  #endif
324 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
325 index 32406ef0d6a2..e52169c2086b 100644
326 --- a/kernel/locking/lockdep.c
327 +++ b/kernel/locking/lockdep.c
328 @@ -153,7 +153,7 @@ static
329  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
330  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
331  
332 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
333 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
334  {
335         unsigned int class_idx = hlock->class_idx;
336  
337 @@ -174,6 +174,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
338          */
339         return lock_classes + class_idx;
340  }
341 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
342  
343  #ifdef CONFIG_LOCK_STAT
344  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
345 SPDX-License-Identifier: GPL-2.0
346 aufs5.x-rcN mmap patch
347
348 diff --git a/fs/proc/base.c b/fs/proc/base.c
349 index ebea9501afb8..dc7edc5f7267 100644
350 --- a/fs/proc/base.c
351 +++ b/fs/proc/base.c
352 @@ -2037,7 +2037,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
353         rc = -ENOENT;
354         vma = find_exact_vma(mm, vm_start, vm_end);
355         if (vma && vma->vm_file) {
356 -               *path = vma->vm_file->f_path;
357 +               *path = vma_pr_or_file(vma)->f_path;
358                 path_get(path);
359                 rc = 0;
360         }
361 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
362 index 14c2badb8fd9..65afe5287e43 100644
363 --- a/fs/proc/nommu.c
364 +++ b/fs/proc/nommu.c
365 @@ -41,7 +41,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
366         file = region->vm_file;
367  
368         if (file) {
369 -               struct inode *inode = file_inode(region->vm_file);
370 +               struct inode *inode;
371 +
372 +               file = vmr_pr_or_file(region);
373 +               inode = file_inode(file);
374                 dev = inode->i_sb->s_dev;
375                 ino = inode->i_ino;
376         }
377 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
378 index 9442631fd4af..1fa8a5fcdeee 100644
379 --- a/fs/proc/task_mmu.c
380 +++ b/fs/proc/task_mmu.c
381 @@ -309,7 +309,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
382         const char *name = NULL;
383  
384         if (file) {
385 -               struct inode *inode = file_inode(vma->vm_file);
386 +               struct inode *inode;
387 +
388 +               file = vma_pr_or_file(vma);
389 +               inode = file_inode(file);
390                 dev = inode->i_sb->s_dev;
391                 ino = inode->i_ino;
392                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
393 @@ -1819,7 +1822,7 @@ static int show_numa_map(struct seq_file *m, void *v)
394         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
395         struct vm_area_struct *vma = v;
396         struct numa_maps *md = &numa_priv->md;
397 -       struct file *file = vma->vm_file;
398 +       struct file *file = vma_pr_or_file(vma);
399         struct mm_struct *mm = vma->vm_mm;
400         struct mempolicy *pol;
401         char buffer[64];
402 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
403 index 7907e6419e57..d17209cf52bc 100644
404 --- a/fs/proc/task_nommu.c
405 +++ b/fs/proc/task_nommu.c
406 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
407         file = vma->vm_file;
408  
409         if (file) {
410 -               struct inode *inode = file_inode(vma->vm_file);
411 +               struct inode *inode;
412 +
413 +               file = vma_pr_or_file(vma);
414 +               inode = file_inode(file);
415                 dev = inode->i_sb->s_dev;
416                 ino = inode->i_ino;
417                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
418 diff --git a/include/linux/mm.h b/include/linux/mm.h
419 index cfaa8feecfe8..b66c4c536ac5 100644
420 --- a/include/linux/mm.h
421 +++ b/include/linux/mm.h
422 @@ -1495,6 +1495,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
423         unmap_mapping_range(mapping, holebegin, holelen, 0);
424  }
425  
426 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
427 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
428 +                                     int);
429 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
430 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
431 +
432 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
433 +                                                               __LINE__)
434 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
435 +                                                         __LINE__)
436 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
437 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
438 +
439 +#ifndef CONFIG_MMU
440 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
441 +extern void vmr_do_fput(struct vm_region *, const char[], int);
442 +
443 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
444 +                                                         __LINE__)
445 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
446 +#endif /* !CONFIG_MMU */
447 +
448  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
449                 void *buf, int len, unsigned int gup_flags);
450  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
451 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
452 index 270aa8fd2800..33848c2165e2 100644
453 --- a/include/linux/mm_types.h
454 +++ b/include/linux/mm_types.h
455 @@ -267,6 +267,7 @@ struct vm_region {
456         unsigned long   vm_top;         /* region allocated to here */
457         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
458         struct file     *vm_file;       /* the backing file or NULL */
459 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
460  
461         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
462         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
463 @@ -341,6 +342,7 @@ struct vm_area_struct {
464         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
465                                            units */
466         struct file * vm_file;          /* File we map to (can be NULL). */
467 +       struct file *vm_prfile;         /* shadow of vm_file */
468         void * vm_private_data;         /* was vm_pte (shared mem) */
469  
470  #ifdef CONFIG_SWAP
471 diff --git a/kernel/fork.c b/kernel/fork.c
472 index 080809560072..968e4dc2279f 100644
473 --- a/kernel/fork.c
474 +++ b/kernel/fork.c
475 @@ -565,7 +565,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
476                         struct inode *inode = file_inode(file);
477                         struct address_space *mapping = file->f_mapping;
478  
479 -                       get_file(file);
480 +                       vma_get_file(tmp);
481                         if (tmp->vm_flags & VM_DENYWRITE)
482                                 atomic_dec(&inode->i_writecount);
483                         i_mmap_lock_write(mapping);
484 diff --git a/mm/Makefile b/mm/Makefile
485 index 1937cc251883..778c1e399903 100644
486 --- a/mm/Makefile
487 +++ b/mm/Makefile
488 @@ -42,7 +42,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
489                            mm_init.o mmu_context.o percpu.o slab_common.o \
490                            compaction.o vmacache.o \
491                            interval_tree.o list_lru.o workingset.o \
492 -                          debug.o gup.o $(mmu-y)
493 +                          prfile.o debug.o gup.o $(mmu-y)
494  
495  # Give 'page_alloc' its own module-parameter namespace
496  page-alloc-y := page_alloc.o
497 diff --git a/mm/filemap.c b/mm/filemap.c
498 index bf6aa30be58d..32ca075c0c8b 100644
499 --- a/mm/filemap.c
500 +++ b/mm/filemap.c
501 @@ -2675,7 +2675,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
502         vm_fault_t ret = VM_FAULT_LOCKED;
503  
504         sb_start_pagefault(inode->i_sb);
505 -       file_update_time(vmf->vma->vm_file);
506 +       vma_file_update_time(vmf->vma);
507         lock_page(page);
508         if (page->mapping != inode->i_mapping) {
509                 unlock_page(page);
510 diff --git a/mm/mmap.c b/mm/mmap.c
511 index 71e4ffc83bcd..7256e8b985eb 100644
512 --- a/mm/mmap.c
513 +++ b/mm/mmap.c
514 @@ -176,7 +176,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
515         if (vma->vm_ops && vma->vm_ops->close)
516                 vma->vm_ops->close(vma);
517         if (vma->vm_file)
518 -               fput(vma->vm_file);
519 +               vma_fput(vma);
520         mpol_put(vma_policy(vma));
521         vm_area_free(vma);
522         return next;
523 @@ -909,7 +909,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
524         if (remove_next) {
525                 if (file) {
526                         uprobe_munmap(next, next->vm_start, next->vm_end);
527 -                       fput(file);
528 +                       vma_fput(vma);
529                 }
530                 if (next->anon_vma)
531                         anon_vma_merge(vma, next);
532 @@ -1837,8 +1837,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
533         return addr;
534  
535  unmap_and_free_vma:
536 +       vma_fput(vma);
537         vma->vm_file = NULL;
538 -       fput(file);
539  
540         /* Undo any partial mapping done by a device driver. */
541         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
542 @@ -2667,7 +2667,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
543                 goto out_free_mpol;
544  
545         if (new->vm_file)
546 -               get_file(new->vm_file);
547 +               vma_get_file(new);
548  
549         if (new->vm_ops && new->vm_ops->open)
550                 new->vm_ops->open(new);
551 @@ -2686,7 +2686,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
552         if (new->vm_ops && new->vm_ops->close)
553                 new->vm_ops->close(new);
554         if (new->vm_file)
555 -               fput(new->vm_file);
556 +               vma_fput(new);
557         unlink_anon_vmas(new);
558   out_free_mpol:
559         mpol_put(vma_policy(new));
560 @@ -2878,7 +2878,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
561         struct vm_area_struct *vma;
562         unsigned long populate = 0;
563         unsigned long ret = -EINVAL;
564 -       struct file *file;
565 +       struct file *file, *prfile;
566  
567         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
568                      current->comm, current->pid);
569 @@ -2953,10 +2953,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
570                 }
571         }
572  
573 -       file = get_file(vma->vm_file);
574 +       vma_get_file(vma);
575 +       file = vma->vm_file;
576 +       prfile = vma->vm_prfile;
577         ret = do_mmap_pgoff(vma->vm_file, start, size,
578                         prot, flags, pgoff, &populate, NULL);
579 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
580 +               struct vm_area_struct *new_vma;
581 +
582 +               new_vma = find_vma(mm, ret);
583 +               if (!new_vma->vm_prfile)
584 +                       new_vma->vm_prfile = prfile;
585 +               if (new_vma != vma)
586 +                       get_file(prfile);
587 +       }
588 +       /*
589 +        * two fput()s instead of vma_fput(vma),
590 +        * coz vma may not be available anymore.
591 +        */
592         fput(file);
593 +       if (prfile)
594 +               fput(prfile);
595  out:
596         up_write(&mm->mmap_sem);
597         if (populate)
598 @@ -3247,7 +3264,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
599                 if (anon_vma_clone(new_vma, vma))
600                         goto out_free_mempol;
601                 if (new_vma->vm_file)
602 -                       get_file(new_vma->vm_file);
603 +                       vma_get_file(new_vma);
604                 if (new_vma->vm_ops && new_vma->vm_ops->open)
605                         new_vma->vm_ops->open(new_vma);
606                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
607 diff --git a/mm/nommu.c b/mm/nommu.c
608 index bd2b4e5ef144..acdab5253263 100644
609 --- a/mm/nommu.c
610 +++ b/mm/nommu.c
611 @@ -563,7 +563,7 @@ static void __put_nommu_region(struct vm_region *region)
612                 up_write(&nommu_region_sem);
613  
614                 if (region->vm_file)
615 -                       fput(region->vm_file);
616 +                       vmr_fput(region);
617  
618                 /* IO memory and memory shared directly out of the pagecache
619                  * from ramfs/tmpfs mustn't be released here */
620 @@ -695,7 +695,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
621         if (vma->vm_ops && vma->vm_ops->close)
622                 vma->vm_ops->close(vma);
623         if (vma->vm_file)
624 -               fput(vma->vm_file);
625 +               vma_fput(vma);
626         put_nommu_region(vma->vm_region);
627         vm_area_free(vma);
628  }
629 @@ -1218,7 +1218,7 @@ unsigned long do_mmap(struct file *file,
630                                         goto error_just_free;
631                                 }
632                         }
633 -                       fput(region->vm_file);
634 +                       vmr_fput(region);
635                         kmem_cache_free(vm_region_jar, region);
636                         region = pregion;
637                         result = start;
638 @@ -1295,10 +1295,10 @@ unsigned long do_mmap(struct file *file,
639         up_write(&nommu_region_sem);
640  error:
641         if (region->vm_file)
642 -               fput(region->vm_file);
643 +               vmr_fput(region);
644         kmem_cache_free(vm_region_jar, region);
645         if (vma->vm_file)
646 -               fput(vma->vm_file);
647 +               vma_fput(vma);
648         vm_area_free(vma);
649         return ret;
650  
651 diff --git a/mm/prfile.c b/mm/prfile.c
652 new file mode 100644
653 index 000000000000..00d51187c325
654 --- /dev/null
655 +++ b/mm/prfile.c
656 @@ -0,0 +1,86 @@
657 +// SPDX-License-Identifier: GPL-2.0
658 +/*
659 + * Mainly for aufs which mmap(2) different file and wants to print different
660 + * path in /proc/PID/maps.
661 + * Call these functions via macros defined in linux/mm.h.
662 + *
663 + * See Documentation/filesystems/aufs/design/06mmap.txt
664 + *
665 + * Copyright (c) 2014-2020 Junjro R. Okajima
666 + * Copyright (c) 2014 Ian Campbell
667 + */
668 +
669 +#include <linux/mm.h>
670 +#include <linux/file.h>
671 +#include <linux/fs.h>
672 +
673 +/* #define PRFILE_TRACE */
674 +static inline void prfile_trace(struct file *f, struct file *pr,
675 +                             const char func[], int line, const char func2[])
676 +{
677 +#ifdef PRFILE_TRACE
678 +       if (pr)
679 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
680 +#endif
681 +}
682 +
683 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
684 +                            int line)
685 +{
686 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
687 +
688 +       prfile_trace(f, pr, func, line, __func__);
689 +       file_update_time(f);
690 +       if (f && pr)
691 +               file_update_time(pr);
692 +}
693 +
694 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
695 +                              int line)
696 +{
697 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
698 +
699 +       prfile_trace(f, pr, func, line, __func__);
700 +       return (f && pr) ? pr : f;
701 +}
702 +
703 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
704 +{
705 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
706 +
707 +       prfile_trace(f, pr, func, line, __func__);
708 +       get_file(f);
709 +       if (f && pr)
710 +               get_file(pr);
711 +}
712 +
713 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
714 +{
715 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
716 +
717 +       prfile_trace(f, pr, func, line, __func__);
718 +       fput(f);
719 +       if (f && pr)
720 +               fput(pr);
721 +}
722 +
723 +#ifndef CONFIG_MMU
724 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
725 +                              int line)
726 +{
727 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
728 +
729 +       prfile_trace(f, pr, func, line, __func__);
730 +       return (f && pr) ? pr : f;
731 +}
732 +
733 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
734 +{
735 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
736 +
737 +       prfile_trace(f, pr, func, line, __func__);
738 +       fput(f);
739 +       if (f && pr)
740 +               fput(pr);
741 +}
742 +#endif /* !CONFIG_MMU */
743 SPDX-License-Identifier: GPL-2.0
744 aufs5.x-rcN standalone patch
745
746 diff --git a/fs/dcache.c b/fs/dcache.c
747 index 046000653e4d..15aa871d1b45 100644
748 --- a/fs/dcache.c
749 +++ b/fs/dcache.c
750 @@ -1371,6 +1371,7 @@ void d_walk(struct dentry *parent, void *data,
751         seq = 1;
752         goto again;
753  }
754 +EXPORT_SYMBOL_GPL(d_walk);
755  
756  struct check_mount {
757         struct vfsmount *mnt;
758 @@ -2916,6 +2917,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
759  
760         write_sequnlock(&rename_lock);
761  }
762 +EXPORT_SYMBOL_GPL(d_exchange);
763  
764  /**
765   * d_ancestor - search for an ancestor
766 diff --git a/fs/exec.c b/fs/exec.c
767 index 74d88dab98dd..71eb4fcace94 100644
768 --- a/fs/exec.c
769 +++ b/fs/exec.c
770 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
771         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
772                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
773  }
774 +EXPORT_SYMBOL_GPL(path_noexec);
775  
776  #ifdef CONFIG_USELIB
777  /*
778 diff --git a/fs/fcntl.c b/fs/fcntl.c
779 index b0935c67bd1e..07251ba86c31 100644
780 --- a/fs/fcntl.c
781 +++ b/fs/fcntl.c
782 @@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
783   out:
784         return error;
785  }
786 +EXPORT_SYMBOL_GPL(setfl);
787  
788  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
789                       int force)
790 diff --git a/fs/file_table.c b/fs/file_table.c
791 index 30d55c9a1744..34b9bbf4c556 100644
792 --- a/fs/file_table.c
793 +++ b/fs/file_table.c
794 @@ -162,6 +162,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
795         }
796         return ERR_PTR(-ENFILE);
797  }
798 +EXPORT_SYMBOL_GPL(alloc_empty_file);
799  
800  /*
801   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
802 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
803  }
804  
805  EXPORT_SYMBOL(fput);
806 +EXPORT_SYMBOL_GPL(__fput_sync);
807  
808  void __init files_init(void)
809  {
810 diff --git a/fs/inode.c b/fs/inode.c
811 index 31796c1040d6..217169541ac4 100644
812 --- a/fs/inode.c
813 +++ b/fs/inode.c
814 @@ -1689,6 +1689,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
815  
816         return update_time(inode, time, flags);
817  }
818 +EXPORT_SYMBOL_GPL(update_time);
819  
820  /**
821   *     touch_atime     -       update the access time
822 diff --git a/fs/namespace.c b/fs/namespace.c
823 index c05ac2488abb..100b7839c8b0 100644
824 --- a/fs/namespace.c
825 +++ b/fs/namespace.c
826 @@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
827         mnt_dec_writers(real_mount(mnt));
828         preempt_enable();
829  }
830 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
831  
832  /**
833   * mnt_drop_write - give up write access to a mount
834 @@ -781,6 +782,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
835  {
836         return check_mnt(real_mount(mnt));
837  }
838 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
839  
840  /*
841   * vfsmount lock must be held for write
842 @@ -1903,6 +1905,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
843         }
844         return 0;
845  }
846 +EXPORT_SYMBOL_GPL(iterate_mounts);
847  
848  static void lock_mnt_tree(struct mount *mnt)
849  {
850 diff --git a/fs/notify/group.c b/fs/notify/group.c
851 index 133f723aca07..0b9f7f6d8390 100644
852 --- a/fs/notify/group.c
853 +++ b/fs/notify/group.c
854 @@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
855  {
856         refcount_inc(&group->refcnt);
857  }
858 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
859  
860  /*
861   * Drop a reference to a group.  Free it if it's through.
862 diff --git a/fs/open.c b/fs/open.c
863 index b62f5c0923a8..89af4b9c7319 100644
864 --- a/fs/open.c
865 +++ b/fs/open.c
866 @@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
867         inode_unlock(dentry->d_inode);
868         return ret;
869  }
870 +EXPORT_SYMBOL_GPL(do_truncate);
871  
872  long vfs_truncate(const struct path *path, loff_t length)
873  {
874 diff --git a/fs/read_write.c b/fs/read_write.c
875 index fa9b3994b34c..eb0e2c6ebaff 100644
876 --- a/fs/read_write.c
877 +++ b/fs/read_write.c
878 @@ -468,6 +468,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
879  
880         return ret;
881  }
882 +EXPORT_SYMBOL_GPL(vfs_read);
883  
884  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
885  {
886 @@ -508,6 +509,7 @@ vfs_readf_t vfs_readf(struct file *file)
887                 return new_sync_read;
888         return ERR_PTR(-ENOSYS); /* doesn't have ->read(|_iter)() op */
889  }
890 +EXPORT_SYMBOL_GPL(vfs_readf);
891  
892  vfs_writef_t vfs_writef(struct file *file)
893  {
894 @@ -519,6 +521,7 @@ vfs_writef_t vfs_writef(struct file *file)
895                 return new_sync_write;
896         return ERR_PTR(-ENOSYS); /* doesn't have ->write(|_iter)() op */
897  }
898 +EXPORT_SYMBOL_GPL(vfs_writef);
899  
900  ssize_t __kernel_write(struct file *file, const void *buf, size_t count, loff_t *pos)
901  {
902 @@ -588,6 +591,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
903  
904         return ret;
905  }
906 +EXPORT_SYMBOL_GPL(vfs_write);
907  
908  /* file_ppos returns &file->f_pos or NULL if file is stream */
909  static inline loff_t *file_ppos(struct file *file)
910 diff --git a/fs/splice.c b/fs/splice.c
911 index d9bcc4a71a0a..bf8725e99070 100644
912 --- a/fs/splice.c
913 +++ b/fs/splice.c
914 @@ -862,6 +862,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
915  
916         return splice_write(pipe, out, ppos, len, flags);
917  }
918 +EXPORT_SYMBOL_GPL(do_splice_from);
919  
920  /*
921   * Attempt to initiate a splice from a file to a pipe.
922 @@ -891,6 +892,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
923  
924         return splice_read(in, ppos, pipe, len, flags);
925  }
926 +EXPORT_SYMBOL_GPL(do_splice_to);
927  
928  /**
929   * splice_direct_to_actor - splices data directly between two non-pipes
930 diff --git a/fs/sync.c b/fs/sync.c
931 index 457f4e4a5cc1..67c66358f3fe 100644
932 --- a/fs/sync.c
933 +++ b/fs/sync.c
934 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
935                 sb->s_op->sync_fs(sb, wait);
936         return __sync_blockdev(sb->s_bdev, wait);
937  }
938 +EXPORT_SYMBOL_GPL(__sync_filesystem);
939  
940  /*
941   * Write out and wait upon all dirty data associated with this
942 diff --git a/fs/xattr.c b/fs/xattr.c
943 index 90dd78f0eb27..40b01dd1b14a 100644
944 --- a/fs/xattr.c
945 +++ b/fs/xattr.c
946 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
947         *xattr_value = value;
948         return error;
949  }
950 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
951  
952  ssize_t
953  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
954 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
955 index e52169c2086b..d9bc5cbe9078 100644
956 --- a/kernel/locking/lockdep.c
957 +++ b/kernel/locking/lockdep.c
958 @@ -174,6 +174,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
959          */
960         return lock_classes + class_idx;
961  }
962 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
963  #define hlock_class(hlock) lockdep_hlock_class(hlock)
964  
965  #ifdef CONFIG_LOCK_STAT
966 diff --git a/kernel/task_work.c b/kernel/task_work.c
967 index 0fef395662a6..83fb1ecfc33d 100644
968 --- a/kernel/task_work.c
969 +++ b/kernel/task_work.c
970 @@ -116,3 +116,4 @@ void task_work_run(void)
971                 } while (work);
972         }
973  }
974 +EXPORT_SYMBOL_GPL(task_work_run);
975 diff --git a/security/security.c b/security/security.c
976 index cd2d18d2d279..fbf716450399 100644
977 --- a/security/security.c
978 +++ b/security/security.c
979 @@ -1036,6 +1036,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
980                 return 0;
981         return call_int_hook(path_rmdir, 0, dir, dentry);
982  }
983 +EXPORT_SYMBOL_GPL(security_path_rmdir);
984  
985  int security_path_unlink(const struct path *dir, struct dentry *dentry)
986  {
987 @@ -1052,6 +1053,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
988                 return 0;
989         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
990  }
991 +EXPORT_SYMBOL_GPL(security_path_symlink);
992  
993  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
994                        struct dentry *new_dentry)
995 @@ -1060,6 +1062,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
996                 return 0;
997         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
998  }
999 +EXPORT_SYMBOL_GPL(security_path_link);
1000  
1001  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1002                          const struct path *new_dir, struct dentry *new_dentry,
1003 @@ -1087,6 +1090,7 @@ int security_path_truncate(const struct path *path)
1004                 return 0;
1005         return call_int_hook(path_truncate, 0, path);
1006  }
1007 +EXPORT_SYMBOL_GPL(security_path_truncate);
1008  
1009  int security_path_chmod(const struct path *path, umode_t mode)
1010  {
1011 @@ -1094,6 +1098,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1012                 return 0;
1013         return call_int_hook(path_chmod, 0, path, mode);
1014  }
1015 +EXPORT_SYMBOL_GPL(security_path_chmod);
1016  
1017  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1018  {
1019 @@ -1101,6 +1106,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1020                 return 0;
1021         return call_int_hook(path_chown, 0, path, uid, gid);
1022  }
1023 +EXPORT_SYMBOL_GPL(security_path_chown);
1024  
1025  int security_path_chroot(const struct path *path)
1026  {
1027 @@ -1201,6 +1207,7 @@ int security_inode_permission(struct inode *inode, int mask)
1028                 return 0;
1029         return call_int_hook(inode_permission, 0, inode, mask);
1030  }
1031 +EXPORT_SYMBOL_GPL(security_inode_permission);
1032  
1033  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1034  {
1035 @@ -1378,6 +1385,7 @@ int security_file_permission(struct file *file, int mask)
1036  
1037         return fsnotify_perm(file, mask);
1038  }
1039 +EXPORT_SYMBOL_GPL(security_file_permission);
1040  
1041  int security_file_alloc(struct file *file)
1042  {
1043 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1044 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1045 +++ linux/Documentation/ABI/testing/debugfs-aufs        2019-07-11 15:42:14.455570938 +0200
1046 @@ -0,0 +1,55 @@
1047 +What:          /debug/aufs/si_<id>/
1048 +Date:          March 2009
1049 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1050 +Description:
1051 +               Under /debug/aufs, a directory named si_<id> is created
1052 +               per aufs mount, where <id> is a unique id generated
1053 +               internally.
1054 +
1055 +What:          /debug/aufs/si_<id>/plink
1056 +Date:          Apr 2013
1057 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1058 +Description:
1059 +               It has three lines and shows the information about the
1060 +               pseudo-link. The first line is a single number
1061 +               representing a number of buckets. The second line is a
1062 +               number of pseudo-links per buckets (separated by a
1063 +               blank). The last line is a single number representing a
1064 +               total number of psedo-links.
1065 +               When the aufs mount option 'noplink' is specified, it
1066 +               will show "1\n0\n0\n".
1067 +
1068 +What:          /debug/aufs/si_<id>/xib
1069 +Date:          March 2009
1070 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1071 +Description:
1072 +               It shows the consumed blocks by xib (External Inode Number
1073 +               Bitmap), its block size and file size.
1074 +               When the aufs mount option 'noxino' is specified, it
1075 +               will be empty. About XINO files, see the aufs manual.
1076 +
1077 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1078 +Date:          March 2009
1079 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1080 +Description:
1081 +               It shows the consumed blocks by xino (External Inode Number
1082 +               Translation Table), its link count, block size and file
1083 +               size.
1084 +               Due to the file size limit, there may exist multiple
1085 +               xino files per branch.  In this case, "-N" is added to
1086 +               the filename and it corresponds to the index of the
1087 +               internal xino array.  "-0" is omitted.
1088 +               When the aufs mount option 'noxino' is specified, Those
1089 +               entries won't exist.  About XINO files, see the aufs
1090 +               manual.
1091 +
1092 +What:          /debug/aufs/si_<id>/xigen
1093 +Date:          March 2009
1094 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1095 +Description:
1096 +               It shows the consumed blocks by xigen (External Inode
1097 +               Generation Table), its block size and file size.
1098 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1099 +               be created.
1100 +               When the aufs mount option 'noxino' is specified, it
1101 +               will be empty. About XINO files, see the aufs manual.
1102 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1103 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1104 +++ linux/Documentation/ABI/testing/sysfs-aufs  2019-07-11 15:42:14.455570938 +0200
1105 @@ -0,0 +1,31 @@
1106 +What:          /sys/fs/aufs/si_<id>/
1107 +Date:          March 2009
1108 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1109 +Description:
1110 +               Under /sys/fs/aufs, a directory named si_<id> is created
1111 +               per aufs mount, where <id> is a unique id generated
1112 +               internally.
1113 +
1114 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1115 +Date:          March 2009
1116 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1117 +Description:
1118 +               It shows the abolute path of a member directory (which
1119 +               is called branch) in aufs, and its permission.
1120 +
1121 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1122 +Date:          July 2013
1123 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1124 +Description:
1125 +               It shows the id of a member directory (which is called
1126 +               branch) in aufs.
1127 +
1128 +What:          /sys/fs/aufs/si_<id>/xi_path
1129 +Date:          March 2009
1130 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1131 +Description:
1132 +               It shows the abolute path of XINO (External Inode Number
1133 +               Bitmap, Translation Table and Generation Table) file
1134 +               even if it is the default path.
1135 +               When the aufs mount option 'noxino' is specified, it
1136 +               will be empty. About XINO files, see the aufs manual.
1137 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1138 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1139 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2020-01-27 10:57:18.162204582 +0100
1140 @@ -0,0 +1,171 @@
1141 +
1142 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1143 +# 
1144 +# This program is free software; you can redistribute it and/or modify
1145 +# it under the terms of the GNU General Public License as published by
1146 +# the Free Software Foundation; either version 2 of the License, or
1147 +# (at your option) any later version.
1148 +# 
1149 +# This program is distributed in the hope that it will be useful,
1150 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1151 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1152 +# GNU General Public License for more details.
1153 +# 
1154 +# You should have received a copy of the GNU General Public License
1155 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1156 +
1157 +Introduction
1158 +----------------------------------------
1159 +
1160 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1161 +1. abbrev. for "advanced multi-layered unification filesystem".
1162 +2. abbrev. for "another unionfs".
1163 +3. abbrev. for "auf das" in German which means "on the" in English.
1164 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1165 +   But "Filesystem aufs Filesystem" is hard to understand.
1166 +4. abbrev. for "African Urban Fashion Show".
1167 +
1168 +AUFS is a filesystem with features:
1169 +- multi layered stackable unification filesystem, the member directory
1170 +  is called as a branch.
1171 +- branch permission and attribute, 'readonly', 'real-readonly',
1172 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1173 +  combination.
1174 +- internal "file copy-on-write".
1175 +- logical deletion, whiteout.
1176 +- dynamic branch manipulation, adding, deleting and changing permission.
1177 +- allow bypassing aufs, user's direct branch access.
1178 +- external inode number translation table and bitmap which maintains the
1179 +  persistent aufs inode number.
1180 +- seekable directory, including NFS readdir.
1181 +- file mapping, mmap and sharing pages.
1182 +- pseudo-link, hardlink over branches.
1183 +- loopback mounted filesystem as a branch.
1184 +- several policies to select one among multiple writable branches.
1185 +- revert a single systemcall when an error occurs in aufs.
1186 +- and more...
1187 +
1188 +
1189 +Multi Layered Stackable Unification Filesystem
1190 +----------------------------------------------------------------------
1191 +Most people already knows what it is.
1192 +It is a filesystem which unifies several directories and provides a
1193 +merged single directory. When users access a file, the access will be
1194 +passed/re-directed/converted (sorry, I am not sure which English word is
1195 +correct) to the real file on the member filesystem. The member
1196 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1197 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1198 +readonly branch is handled by creating 'whiteout' on the upper writable
1199 +branch.
1200 +
1201 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1202 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1203 +different approaches to implement the merged-view.
1204 +The former tries putting it into VFS, and the latter implements as a
1205 +separate filesystem.
1206 +(If I misunderstand about these implementations, please let me know and
1207 +I shall correct it. Because it is a long time ago when I read their
1208 +source files last time).
1209 +
1210 +UnionMount's approach will be able to small, but may be hard to share
1211 +branches between several UnionMount since the whiteout in it is
1212 +implemented in the inode on branch filesystem and always
1213 +shared. According to Bharata's post, readdir does not seems to be
1214 +finished yet.
1215 +There are several missing features known in this implementations such as
1216 +- for users, the inode number may change silently. eg. copy-up.
1217 +- link(2) may break by copy-up.
1218 +- read(2) may get an obsoleted filedata (fstat(2) too).
1219 +- fcntl(F_SETLK) may be broken by copy-up.
1220 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1221 +  open(O_RDWR).
1222 +
1223 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1224 +merged into mainline. This is another implementation of UnionMount as a
1225 +separated filesystem. All the limitations and known problems which
1226 +UnionMount are equally inherited to "overlay" filesystem.
1227 +
1228 +Unionfs has a longer history. When I started implementing a stackable
1229 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1230 +inode, dentry and file objects and they have an array pointing lower
1231 +same kind objects. After contributing many patches for Unionfs, I
1232 +re-started my project AUFS (Jun 2006).
1233 +
1234 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1235 +implemented my own ideas, approaches and enhancements and it became
1236 +totally different one.
1237 +
1238 +Comparing DM snapshot and fs based implementation
1239 +- the number of bytes to be copied between devices is much smaller.
1240 +- the type of filesystem must be one and only.
1241 +- the fs must be writable, no readonly fs, even for the lower original
1242 +  device. so the compression fs will not be usable. but if we use
1243 +  loopback mount, we may address this issue.
1244 +  for instance,
1245 +       mount /cdrom/squashfs.img /sq
1246 +       losetup /sq/ext2.img
1247 +       losetup /somewhere/cow
1248 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1249 +- it will be difficult (or needs more operations) to extract the
1250 +  difference between the original device and COW.
1251 +- DM snapshot-merge may help a lot when users try merging. in the
1252 +  fs-layer union, users will use rsync(1).
1253 +
1254 +You may want to read my old paper "Filesystems in LiveCD"
1255 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1256 +
1257 +
1258 +Several characters/aspects/persona of aufs
1259 +----------------------------------------------------------------------
1260 +
1261 +Aufs has several characters, aspects or persona.
1262 +1. a filesystem, callee of VFS helper
1263 +2. sub-VFS, caller of VFS helper for branches
1264 +3. a virtual filesystem which maintains persistent inode number
1265 +4. reader/writer of files on branches such like an application
1266 +
1267 +1. Callee of VFS Helper
1268 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1269 +unlink(2) from an application reaches sys_unlink() kernel function and
1270 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1271 +calls filesystem specific unlink operation. Actually aufs implements the
1272 +unlink operation but it behaves like a redirector.
1273 +
1274 +2. Caller of VFS Helper for Branches
1275 +aufs_unlink() passes the unlink request to the branch filesystem as if
1276 +it were called from VFS. So the called unlink operation of the branch
1277 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1278 +every necessary pre/post operation for the branch filesystem.
1279 +- acquire the lock for the parent dir on a branch
1280 +- lookup in a branch
1281 +- revalidate dentry on a branch
1282 +- mnt_want_write() for a branch
1283 +- vfs_unlink() for a branch
1284 +- mnt_drop_write() for a branch
1285 +- release the lock on a branch
1286 +
1287 +3. Persistent Inode Number
1288 +One of the most important issue for a filesystem is to maintain inode
1289 +numbers. This is particularly important to support exporting a
1290 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1291 +backend block device for its own. But some storage is necessary to
1292 +keep and maintain the inode numbers. It may be a large space and may not
1293 +suit to keep in memory. Aufs rents some space from its first writable
1294 +branch filesystem (by default) and creates file(s) on it. These files
1295 +are created by aufs internally and removed soon (currently) keeping
1296 +opened.
1297 +Note: Because these files are removed, they are totally gone after
1298 +      unmounting aufs. It means the inode numbers are not persistent
1299 +      across unmount or reboot. I have a plan to make them really
1300 +      persistent which will be important for aufs on NFS server.
1301 +
1302 +4. Read/Write Files Internally (copy-on-write)
1303 +Because a branch can be readonly, when you write a file on it, aufs will
1304 +"copy-up" it to the upper writable branch internally. And then write the
1305 +originally requested thing to the file. Generally kernel doesn't
1306 +open/read/write file actively. In aufs, even a single write may cause a
1307 +internal "file copy". This behaviour is very similar to cp(1) command.
1308 +
1309 +Some people may think it is better to pass such work to user space
1310 +helper, instead of doing in kernel space. Actually I am still thinking
1311 +about it. But currently I have implemented it in kernel space.
1312 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1313 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1314 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2020-01-27 10:57:18.162204582 +0100
1315 @@ -0,0 +1,258 @@
1316 +
1317 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1318 +# 
1319 +# This program is free software; you can redistribute it and/or modify
1320 +# it under the terms of the GNU General Public License as published by
1321 +# the Free Software Foundation; either version 2 of the License, or
1322 +# (at your option) any later version.
1323 +# 
1324 +# This program is distributed in the hope that it will be useful,
1325 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1326 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1327 +# GNU General Public License for more details.
1328 +# 
1329 +# You should have received a copy of the GNU General Public License
1330 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1331 +
1332 +Basic Aufs Internal Structure
1333 +
1334 +Superblock/Inode/Dentry/File Objects
1335 +----------------------------------------------------------------------
1336 +As like an ordinary filesystem, aufs has its own
1337 +superblock/inode/dentry/file objects. All these objects have a
1338 +dynamically allocated array and store the same kind of pointers to the
1339 +lower filesystem, branch.
1340 +For example, when you build a union with one readwrite branch and one
1341 +readonly, mounted /au, /rw and /ro respectively.
1342 +- /au = /rw + /ro
1343 +- /ro/fileA exists but /rw/fileA
1344 +
1345 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1346 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1347 +- [0] = NULL (because /rw/fileA doesn't exist)
1348 +- [1] = /ro/fileA
1349 +
1350 +This style of an array is essentially same to the aufs
1351 +superblock/inode/dentry/file objects.
1352 +
1353 +Because aufs supports manipulating branches, ie. add/delete/change
1354 +branches dynamically, these objects has its own generation. When
1355 +branches are changed, the generation in aufs superblock is
1356 +incremented. And a generation in other object are compared when it is
1357 +accessed. When a generation in other objects are obsoleted, aufs
1358 +refreshes the internal array.
1359 +
1360 +
1361 +Superblock
1362 +----------------------------------------------------------------------
1363 +Additionally aufs superblock has some data for policies to select one
1364 +among multiple writable branches, XIB files, pseudo-links and kobject.
1365 +See below in detail.
1366 +About the policies which supports copy-down a directory, see
1367 +wbr_policy.txt too.
1368 +
1369 +
1370 +Branch and XINO(External Inode Number Translation Table)
1371 +----------------------------------------------------------------------
1372 +Every branch has its own xino (external inode number translation table)
1373 +file. The xino file is created and unlinked by aufs internally. When two
1374 +members of a union exist on the same filesystem, they share the single
1375 +xino file.
1376 +The struct of a xino file is simple, just a sequence of aufs inode
1377 +numbers which is indexed by the lower inode number.
1378 +In the above sample, assume the inode number of /ro/fileA is i111 and
1379 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1380 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1381 +
1382 +When the inode numbers are not contiguous, the xino file will be sparse
1383 +which has a hole in it and doesn't consume as much disk space as it
1384 +might appear. If your branch filesystem consumes disk space for such
1385 +holes, then you should specify 'xino=' option at mounting aufs.
1386 +
1387 +Aufs has a mount option to free the disk blocks for such holes in XINO
1388 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1389 +meet a problem of disk shortage due to XINO files, then you should try
1390 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1391 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1392 +the holes in XINO files.
1393 +
1394 +Also a writable branch has three kinds of "whiteout bases". All these
1395 +are existed when the branch is joined to aufs, and their names are
1396 +whiteout-ed doubly, so that users will never see their names in aufs
1397 +hierarchy.
1398 +1. a regular file which will be hardlinked to all whiteouts.
1399 +2. a directory to store a pseudo-link.
1400 +3. a directory to store an "orphan"-ed file temporary.
1401 +
1402 +1. Whiteout Base
1403 +   When you remove a file on a readonly branch, aufs handles it as a
1404 +   logical deletion and creates a whiteout on the upper writable branch
1405 +   as a hardlink of this file in order not to consume inode on the
1406 +   writable branch.
1407 +2. Pseudo-link Dir
1408 +   See below, Pseudo-link.
1409 +3. Step-Parent Dir
1410 +   When "fileC" exists on the lower readonly branch only and it is
1411 +   opened and removed with its parent dir, and then user writes
1412 +   something into it, then aufs copies-up fileC to this
1413 +   directory. Because there is no other dir to store fileC. After
1414 +   creating a file under this dir, the file is unlinked.
1415 +
1416 +Because aufs supports manipulating branches, ie. add/delete/change
1417 +dynamically, a branch has its own id. When the branch order changes,
1418 +aufs finds the new index by searching the branch id.
1419 +
1420 +
1421 +Pseudo-link
1422 +----------------------------------------------------------------------
1423 +Assume "fileA" exists on the lower readonly branch only and it is
1424 +hardlinked to "fileB" on the branch. When you write something to fileA,
1425 +aufs copies-up it to the upper writable branch. Additionally aufs
1426 +creates a hardlink under the Pseudo-link Directory of the writable
1427 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1428 +simple list. If fileB is read after unlinking fileA, aufs returns
1429 +filedata from the pseudo-link instead of the lower readonly
1430 +branch. Because the pseudo-link is based upon the inode, to keep the
1431 +inode number by xino (see above) is essentially necessary.
1432 +
1433 +All the hardlinks under the Pseudo-link Directory of the writable branch
1434 +should be restored in a proper location later. Aufs provides a utility
1435 +to do this. The userspace helpers executed at remounting and unmounting
1436 +aufs by default.
1437 +During this utility is running, it puts aufs into the pseudo-link
1438 +maintenance mode. In this mode, only the process which began the
1439 +maintenance mode (and its child processes) is allowed to operate in
1440 +aufs. Some other processes which are not related to the pseudo-link will
1441 +be allowed to run too, but the rest have to return an error or wait
1442 +until the maintenance mode ends. If a process already acquires an inode
1443 +mutex (in VFS), it has to return an error.
1444 +
1445 +
1446 +XIB(external inode number bitmap)
1447 +----------------------------------------------------------------------
1448 +Addition to the xino file per a branch, aufs has an external inode number
1449 +bitmap in a superblock object. It is also an internal file such like a
1450 +xino file.
1451 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1452 +not.
1453 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1454 +
1455 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1456 +reduce the number of consumed disk blocks for these files.
1457 +
1458 +
1459 +Virtual or Vertical Dir, and Readdir in Userspace
1460 +----------------------------------------------------------------------
1461 +In order to support multiple layers (branches), aufs readdir operation
1462 +constructs a virtual dir block on memory. For readdir, aufs calls
1463 +vfs_readdir() internally for each dir on branches, merges their entries
1464 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1465 +object. So the file object has its entry list until it is closed. The
1466 +entry list will be updated when the file position is zero and becomes
1467 +obsoleted. This decision is made in aufs automatically.
1468 +
1469 +The dynamically allocated memory block for the name of entries has a
1470 +unit of 512 bytes (by default) and stores the names contiguously (no
1471 +padding). Another block for each entry is handled by kmem_cache too.
1472 +During building dir blocks, aufs creates hash list and judging whether
1473 +the entry is whiteouted by its upper branch or already listed.
1474 +The merged result is cached in the corresponding inode object and
1475 +maintained by a customizable life-time option.
1476 +
1477 +Some people may call it can be a security hole or invite DoS attack
1478 +since the opened and once readdir-ed dir (file object) holds its entry
1479 +list and becomes a pressure for system memory. But I'd say it is similar
1480 +to files under /proc or /sys. The virtual files in them also holds a
1481 +memory page (generally) while they are opened. When an idea to reduce
1482 +memory for them is introduced, it will be applied to aufs too.
1483 +For those who really hate this situation, I've developed readdir(3)
1484 +library which operates this merging in userspace. You just need to set
1485 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1486 +kernel space for readdir(3).
1487 +
1488 +
1489 +Workqueue
1490 +----------------------------------------------------------------------
1491 +Aufs sometimes requires privilege access to a branch. For instance,
1492 +in copy-up/down operation. When a user process is going to make changes
1493 +to a file which exists in the lower readonly branch only, and the mode
1494 +of one of ancestor directories may not be writable by a user
1495 +process. Here aufs copy-up the file with its ancestors and they may
1496 +require privilege to set its owner/group/mode/etc.
1497 +This is a typical case of a application character of aufs (see
1498 +Introduction).
1499 +
1500 +Aufs uses workqueue synchronously for this case. It creates its own
1501 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1502 +passes the request to call mkdir or write (for example), and wait for
1503 +its completion. This approach solves a problem of a signal handler
1504 +simply.
1505 +If aufs didn't adopt the workqueue and changed the privilege of the
1506 +process, then the process may receive the unexpected SIGXFSZ or other
1507 +signals.
1508 +
1509 +Also aufs uses the system global workqueue ("events" kernel thread) too
1510 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1511 +whiteout base and etc. This is unrelated to a privilege.
1512 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1513 +superblock at the beginning, at the same time waits for the completion
1514 +of all queued asynchronous tasks.
1515 +
1516 +
1517 +Whiteout
1518 +----------------------------------------------------------------------
1519 +The whiteout in aufs is very similar to Unionfs's. That is represented
1520 +by its filename. UnionMount takes an approach of a file mode, but I am
1521 +afraid several utilities (find(1) or something) will have to support it.
1522 +
1523 +Basically the whiteout represents "logical deletion" which stops aufs to
1524 +lookup further, but also it represents "dir is opaque" which also stop
1525 +further lookup.
1526 +
1527 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1528 +In order to make several functions in a single systemcall to be
1529 +revertible, aufs adopts an approach to rename a directory to a temporary
1530 +unique whiteouted name.
1531 +For example, in rename(2) dir where the target dir already existed, aufs
1532 +renames the target dir to a temporary unique whiteouted name before the
1533 +actual rename on a branch, and then handles other actions (make it opaque,
1534 +update the attributes, etc). If an error happens in these actions, aufs
1535 +simply renames the whiteouted name back and returns an error. If all are
1536 +succeeded, aufs registers a function to remove the whiteouted unique
1537 +temporary name completely and asynchronously to the system global
1538 +workqueue.
1539 +
1540 +
1541 +Copy-up
1542 +----------------------------------------------------------------------
1543 +It is a well-known feature or concept.
1544 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1545 +internally and makes change to the new file on the upper writable branch.
1546 +When the trigger systemcall does not update the timestamps of the parent
1547 +dir, aufs reverts it after copy-up.
1548 +
1549 +
1550 +Move-down (aufs3.9 and later)
1551 +----------------------------------------------------------------------
1552 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1553 +the lower readonly branch to the upper writable branch when a user
1554 +changes something about the file.
1555 +"Move-down" is an opposite action of copy-up. Basically this action is
1556 +ran manually instead of automatically and internally.
1557 +For desgin and implementation, aufs has to consider these issues.
1558 +- whiteout for the file may exist on the lower branch.
1559 +- ancestor directories may not exist on the lower branch.
1560 +- diropq for the ancestor directories may exist on the upper branch.
1561 +- free space on the lower branch will reduce.
1562 +- another access to the file may happen during moving-down, including
1563 +  UDBA (see "Revalidate Dentry and UDBA").
1564 +- the file should not be hard-linked nor pseudo-linked. they should be
1565 +  handled by auplink utility later.
1566 +
1567 +Sometimes users want to move-down a file from the upper writable branch
1568 +to the lower readonly or writable branch. For instance,
1569 +- the free space of the upper writable branch is going to run out.
1570 +- create a new intermediate branch between the upper and lower branch.
1571 +- etc.
1572 +
1573 +For this purpose, use "aumvdown" command in aufs-util.git.
1574 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1575 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1576 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2020-01-27 10:57:18.162204582 +0100
1577 @@ -0,0 +1,85 @@
1578 +
1579 +# Copyright (C) 2015-2020 Junjiro R. Okajima
1580 +# 
1581 +# This program is free software; you can redistribute it and/or modify
1582 +# it under the terms of the GNU General Public License as published by
1583 +# the Free Software Foundation; either version 2 of the License, or
1584 +# (at your option) any later version.
1585 +# 
1586 +# This program is distributed in the hope that it will be useful,
1587 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1588 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1589 +# GNU General Public License for more details.
1590 +# 
1591 +# You should have received a copy of the GNU General Public License
1592 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1593 +
1594 +Support for a branch who has its ->atomic_open()
1595 +----------------------------------------------------------------------
1596 +The filesystems who implement its ->atomic_open() are not majority. For
1597 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1598 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1599 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1600 +sure whether all filesystems who have ->atomic_open() behave like this,
1601 +but NFSv4 surely returns the error.
1602 +
1603 +In order to support ->atomic_open() for aufs, there are a few
1604 +approaches.
1605 +
1606 +A. Introduce aufs_atomic_open()
1607 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1608 +     branch fs.
1609 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1610 +   an aufs user Pip Cet's approach
1611 +   - calls aufs_create(), VFS finish_open() and notify_change().
1612 +   - pass fake-mode to finish_open(), and then correct the mode by
1613 +     notify_change().
1614 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1615 +   - no aufs_atomic_open().
1616 +   - aufs_lookup() registers the TID to an aufs internal object.
1617 +   - aufs_create() does nothing when the matching TID is registered, but
1618 +     registers the mode.
1619 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1620 +     TID is registered.
1621 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1622 +   credential
1623 +   - no aufs_atomic_open().
1624 +   - aufs_create() registers the TID to an internal object. this info
1625 +     represents "this process created this file just now."
1626 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1627 +     registered TID and re-try open() with superuser's credential.
1628 +
1629 +Pros and cons for each approach.
1630 +
1631 +A.
1632 +   - straightforward but highly depends upon VFS internal.
1633 +   - the atomic behavaiour is kept.
1634 +   - some of parameters such as nameidata are hard to reproduce for
1635 +     branch fs.
1636 +   - large overhead.
1637 +B.
1638 +   - easy to implement.
1639 +   - the atomic behavaiour is lost.
1640 +C.
1641 +   - the atomic behavaiour is kept.
1642 +   - dirty and tricky.
1643 +   - VFS checks whether the file is created correctly after calling
1644 +     ->create(), which means this approach doesn't work.
1645 +D.
1646 +   - easy to implement.
1647 +   - the atomic behavaiour is lost.
1648 +   - to open a file with superuser's credential and give it to a user
1649 +     process is a bad idea, since the file object keeps the credential
1650 +     in it. It may affect LSM or something. This approach doesn't work
1651 +     either.
1652 +
1653 +The approach A is ideal, but it hard to implement. So here is a
1654 +variation of A, which is to be implemented.
1655 +
1656 +A-1. Introduce aufs_atomic_open()
1657 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1658 +       vfs_create() and finish_open().
1659 +     - the demerit is that the several checks after branch fs
1660 +       ->atomic_open() are lost. in the ordinary case, the checks are
1661 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1662 +       be implemented in aufs, but not all I am afraid.
1663 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1664 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1665 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2020-01-27 10:57:18.165538015 +0100
1666 @@ -0,0 +1,113 @@
1667 +
1668 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1669 +# 
1670 +# This program is free software; you can redistribute it and/or modify
1671 +# it under the terms of the GNU General Public License as published by
1672 +# the Free Software Foundation; either version 2 of the License, or
1673 +# (at your option) any later version.
1674 +# 
1675 +# This program is distributed in the hope that it will be useful,
1676 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1677 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1678 +# GNU General Public License for more details.
1679 +# 
1680 +# You should have received a copy of the GNU General Public License
1681 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1682 +
1683 +Lookup in a Branch
1684 +----------------------------------------------------------------------
1685 +Since aufs has a character of sub-VFS (see Introduction), it operates
1686 +lookup for branches as VFS does. It may be a heavy work. But almost all
1687 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1688 +directly connected to its parent. Digging down the directory hierarchy
1689 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1690 +aufs calls it.
1691 +
1692 +When a branch is a remote filesystem, aufs basically relies upon its
1693 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1694 +them.
1695 +For d_revalidate, aufs implements three levels of revalidate tests. See
1696 +"Revalidate Dentry and UDBA" in detail.
1697 +
1698 +
1699 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1700 +----------------------------------------------------------------------
1701 +Let's try case study.
1702 +- aufs has two branches, upper readwrite and lower readonly.
1703 +  /au = /rw + /ro
1704 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1705 +- user invoked "chmod a+rx /au/dirA"
1706 +- the internal copy-up is activated and "/rw/dirA" is created and its
1707 +  permission bits are set to world readable.
1708 +- then "/au/dirA" becomes world readable?
1709 +
1710 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1711 +or it may be a natively readonly filesystem. If aufs respects the lower
1712 +branch, it should not respond readdir request from other users. But user
1713 +allowed it by chmod. Should really aufs rejects showing the entries
1714 +under /ro/dirA?
1715 +
1716 +To be honest, I don't have a good solution for this case. So aufs
1717 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1718 +users.
1719 +When dirperm1 is specified, aufs checks only the highest one for the
1720 +directory permission, and shows the entries. Otherwise, as usual, checks
1721 +every dir existing on all branches and rejects the request.
1722 +
1723 +As a side effect, dirperm1 option improves the performance of aufs
1724 +because the number of permission check is reduced when the number of
1725 +branch is many.
1726 +
1727 +
1728 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1729 +----------------------------------------------------------------------
1730 +Generally VFS helpers re-validate a dentry as a part of lookup.
1731 +0. digging down the directory hierarchy.
1732 +1. lock the parent dir by its i_mutex.
1733 +2. lookup the final (child) entry.
1734 +3. revalidate it.
1735 +4. call the actual operation (create, unlink, etc.)
1736 +5. unlock the parent dir
1737 +
1738 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1739 +called. Actually aufs implements it and checks the dentry on a branch is
1740 +still valid.
1741 +But it is not enough. Because aufs has to release the lock for the
1742 +parent dir on a branch at the end of ->lookup() (step 2) and
1743 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1744 +held by VFS.
1745 +If the file on a branch is changed directly, eg. bypassing aufs, after
1746 +aufs released the lock, then the subsequent operation may cause
1747 +something unpleasant result.
1748 +
1749 +This situation is a result of VFS architecture, ->lookup() and
1750 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1751 +design from VFS's point of view. It is just not suitable for sub-VFS
1752 +character in aufs.
1753 +
1754 +Aufs supports such case by three level of revalidation which is
1755 +selectable by user.
1756 +1. Simple Revalidate
1757 +   Addition to the native flow in VFS's, confirm the child-parent
1758 +   relationship on the branch just after locking the parent dir on the
1759 +   branch in the "actual operation" (step 4). When this validation
1760 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1761 +   checks the validation of the dentry on branches.
1762 +2. Monitor Changes Internally by Inotify/Fsnotify
1763 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1764 +   the dentry on the branch, and returns EBUSY if it finds different
1765 +   dentry.
1766 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1767 +   during it is in cache. When the event is notified, aufs registers a
1768 +   function to kernel 'events' thread by schedule_work(). And the
1769 +   function sets some special status to the cached aufs dentry and inode
1770 +   private data. If they are not cached, then aufs has nothing to
1771 +   do. When the same file is accessed through aufs (step 0-3) later,
1772 +   aufs will detect the status and refresh all necessary data.
1773 +   In this mode, aufs has to ignore the event which is fired by aufs
1774 +   itself.
1775 +3. No Extra Validation
1776 +   This is the simplest test and doesn't add any additional revalidation
1777 +   test, and skip the revalidation in step 4. It is useful and improves
1778 +   aufs performance when system surely hide the aufs branches from user,
1779 +   by over-mounting something (or another method).
1780 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1781 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1782 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2020-01-27 10:57:18.165538015 +0100
1783 @@ -0,0 +1,74 @@
1784 +
1785 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1786 +# 
1787 +# This program is free software; you can redistribute it and/or modify
1788 +# it under the terms of the GNU General Public License as published by
1789 +# the Free Software Foundation; either version 2 of the License, or
1790 +# (at your option) any later version.
1791 +# 
1792 +# This program is distributed in the hope that it will be useful,
1793 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1794 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1795 +# GNU General Public License for more details.
1796 +# 
1797 +# You should have received a copy of the GNU General Public License
1798 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1799 +
1800 +Branch Manipulation
1801 +
1802 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1803 +and changing its permission/attribute, there are a lot of works to do.
1804 +
1805 +
1806 +Add a Branch
1807 +----------------------------------------------------------------------
1808 +o Confirm the adding dir exists outside of aufs, including loopback
1809 +  mount, and its various attributes.
1810 +o Initialize the xino file and whiteout bases if necessary.
1811 +  See struct.txt.
1812 +
1813 +o Check the owner/group/mode of the directory
1814 +  When the owner/group/mode of the adding directory differs from the
1815 +  existing branch, aufs issues a warning because it may impose a
1816 +  security risk.
1817 +  For example, when a upper writable branch has a world writable empty
1818 +  top directory, a malicious user can create any files on the writable
1819 +  branch directly, like copy-up and modify manually. If something like
1820 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1821 +  writable branch, and the writable branch is world-writable, then a
1822 +  malicious guy may create /etc/passwd on the writable branch directly
1823 +  and the infected file will be valid in aufs.
1824 +  I am afraid it can be a security issue, but aufs can do nothing except
1825 +  producing a warning.
1826 +
1827 +
1828 +Delete a Branch
1829 +----------------------------------------------------------------------
1830 +o Confirm the deleting branch is not busy
1831 +  To be general, there is one merit to adopt "remount" interface to
1832 +  manipulate branches. It is to discard caches. At deleting a branch,
1833 +  aufs checks the still cached (and connected) dentries and inodes. If
1834 +  there are any, then they are all in-use. An inode without its
1835 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1836 +
1837 +  For the cached one, aufs checks whether the same named entry exists on
1838 +  other branches.
1839 +  If the cached one is a directory, because aufs provides a merged view
1840 +  to users, as long as one dir is left on any branch aufs can show the
1841 +  dir to users. In this case, the branch can be removed from aufs.
1842 +  Otherwise aufs rejects deleting the branch.
1843 +
1844 +  If any file on the deleting branch is opened by aufs, then aufs
1845 +  rejects deleting.
1846 +
1847 +
1848 +Modify the Permission of a Branch
1849 +----------------------------------------------------------------------
1850 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1851 +  See struct.txt.
1852 +
1853 +o rw --> ro: Confirm the modifying branch is not busy
1854 +  Aufs rejects the request if any of these conditions are true.
1855 +  - a file on the branch is mmap-ed.
1856 +  - a regular file on the branch is opened for write and there is no
1857 +    same named entry on the upper branch.
1858 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1859 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1860 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2020-01-27 10:57:18.165538015 +0100
1861 @@ -0,0 +1,64 @@
1862 +
1863 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1864 +# 
1865 +# This program is free software; you can redistribute it and/or modify
1866 +# it under the terms of the GNU General Public License as published by
1867 +# the Free Software Foundation; either version 2 of the License, or
1868 +# (at your option) any later version.
1869 +# 
1870 +# This program is distributed in the hope that it will be useful,
1871 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1872 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1873 +# GNU General Public License for more details.
1874 +# 
1875 +# You should have received a copy of the GNU General Public License
1876 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1877 +
1878 +Policies to Select One among Multiple Writable Branches
1879 +----------------------------------------------------------------------
1880 +When the number of writable branch is more than one, aufs has to decide
1881 +the target branch for file creation or copy-up. By default, the highest
1882 +writable branch which has the parent (or ancestor) dir of the target
1883 +file is chosen (top-down-parent policy).
1884 +By user's request, aufs implements some other policies to select the
1885 +writable branch, for file creation several policies, round-robin,
1886 +most-free-space, and other policies. For copy-up, top-down-parent,
1887 +bottom-up-parent, bottom-up and others.
1888 +
1889 +As expected, the round-robin policy selects the branch in circular. When
1890 +you have two writable branches and creates 10 new files, 5 files will be
1891 +created for each branch. mkdir(2) systemcall is an exception. When you
1892 +create 10 new directories, all will be created on the same branch.
1893 +And the most-free-space policy selects the one which has most free
1894 +space among the writable branches. The amount of free space will be
1895 +checked by aufs internally, and users can specify its time interval.
1896 +
1897 +The policies for copy-up is more simple,
1898 +top-down-parent is equivalent to the same named on in create policy,
1899 +bottom-up-parent selects the writable branch where the parent dir
1900 +exists and the nearest upper one from the copyup-source,
1901 +bottom-up selects the nearest upper writable branch from the
1902 +copyup-source, regardless the existence of the parent dir.
1903 +
1904 +There are some rules or exceptions to apply these policies.
1905 +- If there is a readonly branch above the policy-selected branch and
1906 +  the parent dir is marked as opaque (a variation of whiteout), or the
1907 +  target (creating) file is whiteout-ed on the upper readonly branch,
1908 +  then the result of the policy is ignored and the target file will be
1909 +  created on the nearest upper writable branch than the readonly branch.
1910 +- If there is a writable branch above the policy-selected branch and
1911 +  the parent dir is marked as opaque or the target file is whiteouted
1912 +  on the branch, then the result of the policy is ignored and the target
1913 +  file will be created on the highest one among the upper writable
1914 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1915 +  it as usual.
1916 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1917 +  They try selecting the branch where the source exists as possible
1918 +  since copyup a large file will take long time. If it can't be,
1919 +  ie. the branch where the source exists is readonly, then they will
1920 +  follow the copyup policy.
1921 +- There is an exception for rename(2) when the target exists.
1922 +  If the rename target exists, aufs compares the index of the branches
1923 +  where the source and the target exists and selects the higher
1924 +  one. If the selected branch is readonly, then aufs follows the
1925 +  copyup policy.
1926 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1927 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1928 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2019-07-11 15:42:14.458904362 +0200
1929 @@ -0,0 +1,31 @@
1930 +
1931 +// to view this graph, run dot(1) command in GRAPHVIZ.
1932 +
1933 +digraph G {
1934 +node [shape=box];
1935 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1936 +
1937 +node [shape=oval];
1938 +
1939 +aufs_rename -> whinfo [label="store/remove"];
1940 +
1941 +node [shape=oval];
1942 +inode_list [label="h_inum list in branch\ncache"];
1943 +
1944 +node [shape=box];
1945 +whinode [label="h_inum list file"];
1946 +
1947 +node [shape=oval];
1948 +brmgmt [label="br_add/del/mod/umount"];
1949 +
1950 +brmgmt -> inode_list [label="create/remove"];
1951 +brmgmt -> whinode [label="load/store"];
1952 +
1953 +inode_list -> whinode [style=dashed,dir=both];
1954 +
1955 +aufs_rename -> inode_list [label="add/del"];
1956 +
1957 +aufs_lookup -> inode_list [label="search"];
1958 +
1959 +aufs_lookup -> whinfo [label="load/remove"];
1960 +}
1961 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1962 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1963 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2020-01-27 10:57:18.165538015 +0100
1964 @@ -0,0 +1,102 @@
1965 +
1966 +# Copyright (C) 2017-2020 Junjiro R. Okajima
1967 +#
1968 +# This program is free software; you can redistribute it and/or modify
1969 +# it under the terms of the GNU General Public License as published by
1970 +# the Free Software Foundation; either version 2 of the License, or
1971 +# (at your option) any later version.
1972 +#
1973 +# This program is distributed in the hope that it will be useful,
1974 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1975 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1976 +# GNU General Public License for more details.
1977 +#
1978 +# You should have received a copy of the GNU General Public License
1979 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1980 +
1981 +Special handling for renaming a directory (DIRREN)
1982 +----------------------------------------------------------------------
1983 +First, let's assume we have a simple usecase.
1984 +
1985 +- /u = /rw + /ro
1986 +- /rw/dirA exists
1987 +- /ro/dirA and /ro/dirA/file exist too
1988 +- there is no dirB on both branches
1989 +- a user issues rename("dirA", "dirB")
1990 +
1991 +Now, what should aufs behave against this rename(2)?
1992 +There are a few possible cases.
1993 +
1994 +A. returns EROFS.
1995 +   since dirA exists on a readonly branch which cannot be renamed.
1996 +B. returns EXDEV.
1997 +   it is possible to copy-up dirA (only the dir itself), but the child
1998 +   entries ("file" in this case) should not be. it must be a bad
1999 +   approach to copy-up recursively.
2000 +C. returns a success.
2001 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
2002 +   is a violation of aufs' policy.
2003 +D. construct an extra information which indicates that /ro/dirA should
2004 +   be handled as the name of dirB.
2005 +   overlayfs has a similar feature called REDIRECT.
2006 +
2007 +Until now, aufs implements the case B only which returns EXDEV, and
2008 +expects the userspace application behaves like mv(1) which tries
2009 +issueing rename(2) recursively.
2010 +
2011 +A new aufs feature called DIRREN is introduced which implements the case
2012 +D. There are several "extra information" added.
2013 +
2014 +1. detailed info per renamed directory
2015 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
2016 +2. the inode-number list of directories on a branch
2017 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
2018 +
2019 +The filename of "detailed info per directory" represents the lower
2020 +branch, and its format is
2021 +- a type of the branch id
2022 +  one of these.
2023 +  + uuid (not implemented yet)
2024 +  + fsid
2025 +  + dev
2026 +- the inode-number of the branch root dir
2027 +
2028 +And it contains these info in a single regular file.
2029 +- magic number
2030 +- branch's inode-number of the logically renamed dir
2031 +- the name of the before-renamed dir
2032 +
2033 +The "detailed info per directory" file is created in aufs rename(2), and
2034 +loaded in any lookup.
2035 +The info is considered in lookup for the matching case only. Here
2036 +"matching" means that the root of branch (in the info filename) is same
2037 +to the current looking-up branch. After looking-up the before-renamed
2038 +name, the inode-number is compared. And the matched dentry is used.
2039 +
2040 +The "inode-number list of directories" is a regular file which contains
2041 +simply the inode-numbers on the branch. The file is created or updated
2042 +in removing the branch, and loaded in adding the branch. Its lifetime is
2043 +equal to the branch.
2044 +The list is refered in lookup, and when the current target inode is
2045 +found in the list, the aufs tries loading the "detailed info per
2046 +directory" and get the changed and valid name of the dir.
2047 +
2048 +Theoretically these "extra informaiton" may be able to be put into XATTR
2049 +in the dir inode. But aufs doesn't choose this way because
2050 +1. XATTR may not be supported by the branch (or its configuration)
2051 +2. XATTR may have its size limit.
2052 +3. XATTR may be less easy to convert than a regular file, when the
2053 +   format of the info is changed in the future.
2054 +At the same time, I agree that the regular file approach is much slower
2055 +than XATTR approach. So, in the future, aufs may take the XATTR or other
2056 +better approach.
2057 +
2058 +This DIRREN feature is enabled by aufs configuration, and is activated
2059 +by a new mount option.
2060 +
2061 +For the more complicated case, there is a work with UDBA option, which
2062 +is to dected the direct access to the branches (by-passing aufs) and to
2063 +maintain the cashes in aufs. Since a single cached aufs dentry may
2064 +contains two names, before- and after-rename, the name comparision in
2065 +UDBA handler may not work correctly. In this case, the behaviour will be
2066 +equivalen to udba=reval case.
2067 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2068 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2069 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2020-01-27 10:57:18.165538015 +0100
2070 @@ -0,0 +1,120 @@
2071 +
2072 +# Copyright (C) 2011-2020 Junjiro R. Okajima
2073 +# 
2074 +# This program is free software; you can redistribute it and/or modify
2075 +# it under the terms of the GNU General Public License as published by
2076 +# the Free Software Foundation; either version 2 of the License, or
2077 +# (at your option) any later version.
2078 +# 
2079 +# This program is distributed in the hope that it will be useful,
2080 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2081 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2082 +# GNU General Public License for more details.
2083 +# 
2084 +# You should have received a copy of the GNU General Public License
2085 +# along with this program; if not, write to the Free Software
2086 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2087 +
2088 +
2089 +File-based Hierarchical Storage Management (FHSM)
2090 +----------------------------------------------------------------------
2091 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2092 +storage world. Aufs provides this feature as file-based with multiple
2093 +writable branches, based upon the principle of "Colder, the Lower".
2094 +Here the word "colder" means that the less used files, and "lower" means
2095 +that the position in the order of the stacked branches vertically.
2096 +These multiple writable branches are prioritized, ie. the topmost one
2097 +should be the fastest drive and be used heavily.
2098 +
2099 +o Characters in aufs FHSM story
2100 +- aufs itself and a new branch attribute.
2101 +- a new ioctl interface to move-down and to establish a connection with
2102 +  the daemon ("move-down" is a converse of "copy-up").
2103 +- userspace tool and daemon.
2104 +
2105 +The userspace daemon establishes a connection with aufs and waits for
2106 +the notification. The notified information is very similar to struct
2107 +statfs containing the number of consumed blocks and inodes.
2108 +When the consumed blocks/inodes of a branch exceeds the user-specified
2109 +upper watermark, the daemon activates its move-down process until the
2110 +consumed blocks/inodes reaches the user-specified lower watermark.
2111 +
2112 +The actual move-down is done by aufs based upon the request from
2113 +user-space since we need to maintain the inode number and the internal
2114 +pointer arrays in aufs.
2115 +
2116 +Currently aufs FHSM handles the regular files only. Additionally they
2117 +must not be hard-linked nor pseudo-linked.
2118 +
2119 +
2120 +o Cowork of aufs and the user-space daemon
2121 +  During the userspace daemon established the connection, aufs sends a
2122 +  small notification to it whenever aufs writes something into the
2123 +  writable branch. But it may cost high since aufs issues statfs(2)
2124 +  internally. So user can specify a new option to cache the
2125 +  info. Actually the notification is controlled by these factors.
2126 +  + the specified cache time.
2127 +  + classified as "force" by aufs internally.
2128 +  Until the specified time expires, aufs doesn't send the info
2129 +  except the forced cases. When aufs decide forcing, the info is always
2130 +  notified to userspace.
2131 +  For example, the number of free inodes is generally large enough and
2132 +  the shortage of it happens rarely. So aufs doesn't force the
2133 +  notification when creating a new file, directory and others. This is
2134 +  the typical case which aufs doesn't force.
2135 +  When aufs writes the actual filedata and the files consumes any of new
2136 +  blocks, the aufs forces notifying.
2137 +
2138 +
2139 +o Interfaces in aufs
2140 +- New branch attribute.
2141 +  + fhsm
2142 +    Specifies that the branch is managed by FHSM feature. In other word,
2143 +    participant in the FHSM.
2144 +    When nofhsm is set to the branch, it will not be the source/target
2145 +    branch of the move-down operation. This attribute is set
2146 +    independently from coo and moo attributes, and if you want full
2147 +    FHSM, you should specify them as well.
2148 +- New mount option.
2149 +  + fhsm_sec
2150 +    Specifies a second to suppress many less important info to be
2151 +    notified.
2152 +- New ioctl.
2153 +  + AUFS_CTL_FHSM_FD
2154 +    create a new file descriptor which userspace can read the notification
2155 +    (a subset of struct statfs) from aufs.
2156 +- Module parameter 'brs'
2157 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2158 +  be set.
2159 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2160 +  When there are two or more branches with fhsm attributes,
2161 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2162 +  terminates it. As a result of remounting and branch-manipulation, the
2163 +  number of branches with fhsm attribute can be one. In this case,
2164 +  /sbin/mount.aufs will terminate the user-space daemon.
2165 +
2166 +
2167 +Finally the operation is done as these steps in kernel-space.
2168 +- make sure that,
2169 +  + no one else is using the file.
2170 +  + the file is not hard-linked.
2171 +  + the file is not pseudo-linked.
2172 +  + the file is a regular file.
2173 +  + the parent dir is not opaqued.
2174 +- find the target writable branch.
2175 +- make sure the file is not whiteout-ed by the upper (than the target)
2176 +  branch.
2177 +- make the parent dir on the target branch.
2178 +- mutex lock the inode on the branch.
2179 +- unlink the whiteout on the target branch (if exists).
2180 +- lookup and create the whiteout-ed temporary name on the target branch.
2181 +- copy the file as the whiteout-ed temporary name on the target branch.
2182 +- rename the whiteout-ed temporary name to the original name.
2183 +- unlink the file on the source branch.
2184 +- maintain the internal pointer array and the external inode number
2185 +  table (XINO).
2186 +- maintain the timestamps and other attributes of the parent dir and the
2187 +  file.
2188 +
2189 +And of course, in every step, an error may happen. So the operation
2190 +should restore the original file state after an error happens.
2191 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2192 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2193 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2020-01-27 10:57:18.165538015 +0100
2194 @@ -0,0 +1,72 @@
2195 +
2196 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2197 +# 
2198 +# This program is free software; you can redistribute it and/or modify
2199 +# it under the terms of the GNU General Public License as published by
2200 +# the Free Software Foundation; either version 2 of the License, or
2201 +# (at your option) any later version.
2202 +# 
2203 +# This program is distributed in the hope that it will be useful,
2204 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2205 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2206 +# GNU General Public License for more details.
2207 +# 
2208 +# You should have received a copy of the GNU General Public License
2209 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2210 +
2211 +mmap(2) -- File Memory Mapping
2212 +----------------------------------------------------------------------
2213 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2214 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2215 +->mmap().
2216 +This approach is simple and good, but there is one problem.
2217 +Under /proc, several entries show the mmapped files by its path (with
2218 +device and inode number), and the printed path will be the path on the
2219 +branch fs's instead of virtual aufs's.
2220 +This is not a problem in most cases, but some utilities lsof(1) (and its
2221 +user) may expect the path on aufs.
2222 +
2223 +To address this issue, aufs adds a new member called vm_prfile in struct
2224 +vm_area_struct (and struct vm_region). The original vm_file points to
2225 +the file on the branch fs in order to handle everything correctly as
2226 +usual. The new vm_prfile points to a virtual file in aufs, and the
2227 +show-functions in procfs refers to vm_prfile if it is set.
2228 +Also we need to maintain several other places where touching vm_file
2229 +such like
2230 +- fork()/clone() copies vma and the reference count of vm_file is
2231 +  incremented.
2232 +- merging vma maintains the ref count too.
2233 +
2234 +This is not a good approach. It just fakes the printed path. But it
2235 +leaves all behaviour around f_mapping unchanged. This is surely an
2236 +advantage.
2237 +Actually aufs had adopted another complicated approach which calls
2238 +generic_file_mmap() and handles struct vm_operations_struct. In this
2239 +approach, aufs met a hard problem and I could not solve it without
2240 +switching the approach.
2241 +
2242 +There may be one more another approach which is
2243 +- bind-mount the branch-root onto the aufs-root internally
2244 +- grab the new vfsmount (ie. struct mount)
2245 +- lazy-umount the branch-root internally
2246 +- in open(2) the aufs-file, open the branch-file with the hidden
2247 +  vfsmount (instead of the original branch's vfsmount)
2248 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2249 +  but it may be possible from userspace by the mount helper.
2250 +
2251 +Adding the internal hidden vfsmount and using it in opening a file, the
2252 +file path under /proc will be printed correctly. This approach looks
2253 +smarter, but is not possible I am afraid.
2254 +- aufs-root may be bind-mount later. when it happens, another hidden
2255 +  vfsmount will be required.
2256 +- it is hard to get the chance to bind-mount and lazy-umount
2257 +  + in kernel-space, FS can have vfsmount in open(2) via
2258 +    file->f_path, and aufs can know its vfsmount. But several locks are
2259 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2260 +    here, then it may cause a deadlock.
2261 +  + in user-space, bind-mount doesn't invoke the mount helper.
2262 +- since /proc shows dev and ino, aufs has to give vma these info. it
2263 +  means a new member vm_prinode will be necessary. this is essentially
2264 +  equivalent to vm_prfile described above.
2265 +
2266 +I have to give up this "looks-smater" approach.
2267 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2268 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2269 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2020-01-27 10:57:18.165538015 +0100
2270 @@ -0,0 +1,96 @@
2271 +
2272 +# Copyright (C) 2014-2020 Junjiro R. Okajima
2273 +#
2274 +# This program is free software; you can redistribute it and/or modify
2275 +# it under the terms of the GNU General Public License as published by
2276 +# the Free Software Foundation; either version 2 of the License, or
2277 +# (at your option) any later version.
2278 +#
2279 +# This program is distributed in the hope that it will be useful,
2280 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2281 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2282 +# GNU General Public License for more details.
2283 +#
2284 +# You should have received a copy of the GNU General Public License
2285 +# along with this program; if not, write to the Free Software
2286 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2287 +
2288 +
2289 +Listing XATTR/EA and getting the value
2290 +----------------------------------------------------------------------
2291 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2292 +shows the values from the topmost existing file. This behaviour is good
2293 +for the non-dir entries since the bahaviour exactly matches the shown
2294 +information. But for the directories, aufs considers all the same named
2295 +entries on the lower branches. Which means, if one of the lower entry
2296 +rejects readdir call, then aufs returns an error even if the topmost
2297 +entry allows it. This behaviour is necessary to respect the branch fs's
2298 +security, but can make users confused since the user-visible standard
2299 +attributes don't match the behaviour.
2300 +To address this issue, aufs has a mount option called dirperm1 which
2301 +checks the permission for the topmost entry only, and ignores the lower
2302 +entry's permission.
2303 +
2304 +A similar issue can happen around XATTR.
2305 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2306 +always set. Otherwise these very unpleasant situation would happen.
2307 +- listxattr(2) may return the duplicated entries.
2308 +- users may not be able to remove or reset the XATTR forever,
2309 +
2310 +
2311 +XATTR/EA support in the internal (copy,move)-(up,down)
2312 +----------------------------------------------------------------------
2313 +Generally the extended attributes of inode are categorized as these.
2314 +- "security" for LSM and capability.
2315 +- "system" for posix ACL, 'acl' mount option is required for the branch
2316 +  fs generally.
2317 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2318 +- "user" for userspace, 'user_xattr' mount option is required for the
2319 +  branch fs generally.
2320 +
2321 +Moreover there are some other categories. Aufs handles these rather
2322 +unpopular categories as the ordinary ones, ie. there is no special
2323 +condition nor exception.
2324 +
2325 +In copy-up, the support for XATTR on the dst branch may differ from the
2326 +src branch. In this case, the copy-up operation will get an error and
2327 +the original user operation which triggered the copy-up will fail. It
2328 +can happen that even all copy-up will fail.
2329 +When both of src and dst branches support XATTR and if an error occurs
2330 +during copying XATTR, then the copy-up should fail obviously. That is a
2331 +good reason and aufs should return an error to userspace. But when only
2332 +the src branch support that XATTR, aufs should not return an error.
2333 +For example, the src branch supports ACL but the dst branch doesn't
2334 +because the dst branch may natively un-support it or temporary
2335 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2336 +may NOT return an error even if the XATTR is not supported. It is
2337 +totally up to the branch fs.
2338 +
2339 +Anyway when the aufs internal copy-up gets an error from the dst branch
2340 +fs, then aufs tries removing the just copied entry and returns the error
2341 +to the userspace. The worst case of this situation will be all copy-up
2342 +will fail.
2343 +
2344 +For the copy-up operation, there two basic approaches.
2345 +- copy the specified XATTR only (by category above), and return the
2346 +  error unconditionally if it happens.
2347 +- copy all XATTR, and ignore the error on the specified category only.
2348 +
2349 +In order to support XATTR and to implement the correct behaviour, aufs
2350 +chooses the latter approach and introduces some new branch attributes,
2351 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2352 +They correspond to the XATTR namespaces (see above). Additionally, to be
2353 +convenient, "icex" is also provided which means all "icex*" attributes
2354 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2355 +
2356 +The meaning of these attributes is to ignore the error from setting
2357 +XATTR on that branch.
2358 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2359 +error from the dst branch according to the specified attributes.
2360 +
2361 +Some XATTR may have its default value. The default value may come from
2362 +the parent dir or the environment. If the default value is set at the
2363 +file creating-time, it will be overwritten by copy-up.
2364 +Some contradiction may happen I am afraid.
2365 +Do we need another attribute to stop copying XATTR? I am unsure. For
2366 +now, aufs implements the branch attributes to ignore the error.
2367 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2368 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2369 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2020-01-27 10:57:18.165538015 +0100
2370 @@ -0,0 +1,58 @@
2371 +
2372 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2373 +# 
2374 +# This program is free software; you can redistribute it and/or modify
2375 +# it under the terms of the GNU General Public License as published by
2376 +# the Free Software Foundation; either version 2 of the License, or
2377 +# (at your option) any later version.
2378 +# 
2379 +# This program is distributed in the hope that it will be useful,
2380 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2381 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2382 +# GNU General Public License for more details.
2383 +# 
2384 +# You should have received a copy of the GNU General Public License
2385 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2386 +
2387 +Export Aufs via NFS
2388 +----------------------------------------------------------------------
2389 +Here is an approach.
2390 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2391 +  generation.
2392 +- iget_locked(): initialize aufs inode generation for a new inode, and
2393 +  store it in xigen file.
2394 +- destroy_inode(): increment aufs inode generation and store it in xigen
2395 +  file. it is necessary even if it is not unlinked, because any data of
2396 +  inode may be changed by UDBA.
2397 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2398 +  build file handle by
2399 +  + branch id (4 bytes)
2400 +  + superblock generation (4 bytes)
2401 +  + inode number (4 or 8 bytes)
2402 +  + parent dir inode number (4 or 8 bytes)
2403 +  + inode generation (4 bytes))
2404 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2405 +    bytes)
2406 +  + file handle for a branch (by exportfs_encode_fh())
2407 +- fh_to_dentry():
2408 +  + find the index of a branch from its id in handle, and check it is
2409 +    still exist in aufs.
2410 +  + 1st level: get the inode number from handle and search it in cache.
2411 +  + 2nd level: if not found in cache, get the parent inode number from
2412 +    the handle and search it in cache. and then open the found parent
2413 +    dir, find the matching inode number by vfs_readdir() and get its
2414 +    name, and call lookup_one_len() for the target dentry.
2415 +  + 3rd level: if the parent dir is not cached, call
2416 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2417 +    build a pathname of it, convert it a pathname in aufs, call
2418 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2419 +    the 2nd level.
2420 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2421 +    for every branch, but not itself. to get this, (currently) aufs
2422 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2423 +    idea, but I didn't get other approach.
2424 +  + test the generation of the gotten inode.
2425 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2426 +  convert it into ESTALE for NFSD.
2427 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2428 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2429 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2430 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2431 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2020-01-27 10:57:18.165538015 +0100
2432 @@ -0,0 +1,52 @@
2433 +
2434 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2435 +# 
2436 +# This program is free software; you can redistribute it and/or modify
2437 +# it under the terms of the GNU General Public License as published by
2438 +# the Free Software Foundation; either version 2 of the License, or
2439 +# (at your option) any later version.
2440 +# 
2441 +# This program is distributed in the hope that it will be useful,
2442 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2443 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2444 +# GNU General Public License for more details.
2445 +# 
2446 +# You should have received a copy of the GNU General Public License
2447 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2448 +
2449 +Show Whiteout Mode (shwh)
2450 +----------------------------------------------------------------------
2451 +Generally aufs hides the name of whiteouts. But in some cases, to show
2452 +them is very useful for users. For instance, creating a new middle layer
2453 +(branch) by merging existing layers.
2454 +
2455 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2456 +When you have three branches,
2457 +- Bottom: 'system', squashfs (underlying base system), read-only
2458 +- Middle: 'mods', squashfs, read-only
2459 +- Top: 'overlay', ram (tmpfs), read-write
2460 +
2461 +The top layer is loaded at boot time and saved at shutdown, to preserve
2462 +the changes made to the system during the session.
2463 +When larger changes have been made, or smaller changes have accumulated,
2464 +the size of the saved top layer data grows. At this point, it would be
2465 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2466 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2467 +restoring save and load speed.
2468 +
2469 +This merging is simplified by the use of another aufs mount, of just the
2470 +two overlay branches using the 'shwh' option.
2471 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2472 +       aufs /livesys/merge_union
2473 +
2474 +A merged view of these two branches is then available at
2475 +/livesys/merge_union, and the new feature is that the whiteouts are
2476 +visible!
2477 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2478 +writing to all branches. Also the default mode for all branches is 'ro'.
2479 +It is now possible to save the combined contents of the two overlay
2480 +branches to a new squashfs, e.g.:
2481 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2482 +
2483 +This new squashfs archive can be stored on the boot device and the
2484 +initramfs will use it to replace the old one at the next boot.
2485 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2486 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2487 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2020-01-27 10:57:18.165538015 +0100
2488 @@ -0,0 +1,47 @@
2489 +
2490 +# Copyright (C) 2010-2020 Junjiro R. Okajima
2491 +#
2492 +# This program is free software; you can redistribute it and/or modify
2493 +# it under the terms of the GNU General Public License as published by
2494 +# the Free Software Foundation; either version 2 of the License, or
2495 +# (at your option) any later version.
2496 +#
2497 +# This program is distributed in the hope that it will be useful,
2498 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2499 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2500 +# GNU General Public License for more details.
2501 +#
2502 +# You should have received a copy of the GNU General Public License
2503 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2504 +
2505 +Dynamically customizable FS operations
2506 +----------------------------------------------------------------------
2507 +Generally FS operations (struct inode_operations, struct
2508 +address_space_operations, struct file_operations, etc.) are defined as
2509 +"static const", but it never means that FS have only one set of
2510 +operation. Some FS have multiple sets of them. For instance, ext2 has
2511 +three sets, one for XIP, for NOBH, and for normal.
2512 +Since aufs overrides and redirects these operations, sometimes aufs has
2513 +to change its behaviour according to the branch FS type. More importantly
2514 +VFS acts differently if a function (member in the struct) is set or
2515 +not. It means aufs should have several sets of operations and select one
2516 +among them according to the branch FS definition.
2517 +
2518 +In order to solve this problem and not to affect the behaviour of VFS,
2519 +aufs defines these operations dynamically. For instance, aufs defines
2520 +dummy direct_IO function for struct address_space_operations, but it may
2521 +not be set to the address_space_operations actually. When the branch FS
2522 +doesn't have it, aufs doesn't set it to its address_space_operations
2523 +while the function definition itself is still alive. So the behaviour
2524 +itself will not change, and it will return an error when direct_IO is
2525 +not set.
2526 +
2527 +The lifetime of these dynamically generated operation object is
2528 +maintained by aufs branch object. When the branch is removed from aufs,
2529 +the reference counter of the object is decremented. When it reaches
2530 +zero, the dynamically generated operation object will be freed.
2531 +
2532 +This approach is designed to support AIO (io_submit), Direct I/O and
2533 +XIP (DAX) mainly.
2534 +Currently this approach is applied to address_space_operations for
2535 +regular files only.
2536 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2537 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2538 +++ linux/Documentation/filesystems/aufs/README 2020-01-23 09:59:36.748660743 +0100
2539 @@ -0,0 +1,401 @@
2540 +
2541 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2542 +http://aufs.sf.net
2543 +Junjiro R. Okajima
2544 +
2545 +
2546 +0. Introduction
2547 +----------------------------------------
2548 +In the early days, aufs was entirely re-designed and re-implemented
2549 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2550 +improvements and implementations, it became totally different from
2551 +Unionfs while keeping the basic features.
2552 +Later, Unionfs Version 2.x series began taking some of the same
2553 +approaches to aufs1's.
2554 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2555 +University and his team.
2556 +
2557 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2558 +support,
2559 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2560 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2561 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2562 +  or aufs1 from CVS on SourceForge.
2563 +
2564 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2565 +      According to Christoph Hellwig, linux rejects all union-type
2566 +      filesystems but UnionMount.
2567 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2568 +
2569 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2570 +    UnionMount, and he pointed out an issue around a directory mutex
2571 +    lock and aufs addressed it. But it is still unsure whether aufs will
2572 +    be merged (or any other union solution).
2573 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2574 +
2575 +
2576 +1. Features
2577 +----------------------------------------
2578 +- unite several directories into a single virtual filesystem. The member
2579 +  directory is called as a branch.
2580 +- you can specify the permission flags to the branch, which are 'readonly',
2581 +  'readwrite' and 'whiteout-able.'
2582 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2583 +  readonly branch are modifiable logically.
2584 +- dynamic branch manipulation, add, del.
2585 +- etc...
2586 +
2587 +Also there are many enhancements in aufs, such as:
2588 +- test only the highest one for the directory permission (dirperm1)
2589 +- copyup on open (coo=)
2590 +- 'move' policy for copy-up between two writable branches, after
2591 +  checking free space.
2592 +- xattr, acl
2593 +- readdir(3) in userspace.
2594 +- keep inode number by external inode number table
2595 +- keep the timestamps of file/dir in internal copyup operation
2596 +- seekable directory, supporting NFS readdir.
2597 +- whiteout is hardlinked in order to reduce the consumption of inodes
2598 +  on branch
2599 +- do not copyup, nor create a whiteout when it is unnecessary
2600 +- revert a single systemcall when an error occurs in aufs
2601 +- remount interface instead of ioctl
2602 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2603 +- loopback mounted filesystem as a branch
2604 +- kernel thread for removing the dir who has a plenty of whiteouts
2605 +- support copyup sparse file (a file which has a 'hole' in it)
2606 +- default permission flags for branches
2607 +- selectable permission flags for ro branch, whether whiteout can
2608 +  exist or not
2609 +- export via NFS.
2610 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2611 +- support multiple writable branches, some policies to select one
2612 +  among multiple writable branches.
2613 +- a new semantics for link(2) and rename(2) to support multiple
2614 +  writable branches.
2615 +- no glibc changes are required.
2616 +- pseudo hardlink (hardlink over branches)
2617 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2618 +  including NFS or remote filesystem branch.
2619 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2620 +- and more...
2621 +
2622 +Currently these features are dropped temporary from aufs5.
2623 +See design/08plan.txt in detail.
2624 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2625 +  (robr)
2626 +- statistics of aufs thread (/sys/fs/aufs/stat)
2627 +
2628 +Features or just an idea in the future (see also design/*.txt),
2629 +- reorder the branch index without del/re-add.
2630 +- permanent xino files for NFSD
2631 +- an option for refreshing the opened files after add/del branches
2632 +- light version, without branch manipulation. (unnecessary?)
2633 +- copyup in userspace
2634 +- inotify in userspace
2635 +- readv/writev
2636 +
2637 +
2638 +2. Download
2639 +----------------------------------------
2640 +There are three GIT trees for aufs5, aufs5-linux.git,
2641 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2642 +"aufs-util.git."
2643 +While the aufs-util is always necessary, you need either of aufs5-linux
2644 +or aufs5-standalone.
2645 +
2646 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2647 +git://git.kernel.org/.../torvalds/linux.git.
2648 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2649 +build aufs5 as an external kernel module.
2650 +Several extra patches are not included in this tree. Only
2651 +aufs5-standalone tree contains them. They are described in the later
2652 +section "Configuration and Compilation."
2653 +
2654 +On the other hand, the aufs5-standalone tree has only aufs source files
2655 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2656 +But you need to apply all aufs patches manually.
2657 +
2658 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2659 +represents the linux kernel version, "linux-5.x". For instance,
2660 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2661 +"aufs5.x-rcN" branch.
2662 +
2663 +o aufs5-linux tree
2664 +$ git clone --reference /your/linux/git/tree \
2665 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2666 +- if you don't have linux GIT tree, then remove "--reference ..."
2667 +$ cd aufs5-linux.git
2668 +$ git checkout origin/aufs5.0
2669 +
2670 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2671 +leave the patch-work to GIT.
2672 +$ cd /your/linux/git/tree
2673 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2674 +$ git fetch aufs5
2675 +$ git checkout -b my5.0 v5.0
2676 +$ (add your local change...)
2677 +$ git pull aufs5 aufs5.0
2678 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2679 +- you may need to solve some conflicts between your_changes and
2680 +  aufs5.0. in this case, git-rerere is recommended so that you can
2681 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2682 +  later in the future.
2683 +
2684 +o aufs5-standalone tree
2685 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2686 +$ cd aufs5-standalone.git
2687 +$ git checkout origin/aufs5.0
2688 +
2689 +o aufs-util tree
2690 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2691 +- note that the public aufs-util.git is on SourceForge instead of
2692 +  GitHUB.
2693 +$ cd aufs-util.git
2694 +$ git checkout origin/aufs5.0
2695 +
2696 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2697 +The minor version number, 'x' in '5.x', of aufs may not always
2698 +follow the minor version number of the kernel.
2699 +Because changes in the kernel that cause the use of a new
2700 +minor version number do not always require changes to aufs-util.
2701 +
2702 +Since aufs-util has its own minor version number, you may not be
2703 +able to find a GIT branch in aufs-util for your kernel's
2704 +exact minor version number.
2705 +In this case, you should git-checkout the branch for the
2706 +nearest lower number.
2707 +
2708 +For (an unreleased) example:
2709 +If you are using "linux-5.10" and the "aufs5.10" branch
2710 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2711 +or something numerically smaller is the branch for your kernel.
2712 +
2713 +Also you can view all branches by
2714 +       $ git branch -a
2715 +
2716 +
2717 +3. Configuration and Compilation
2718 +----------------------------------------
2719 +Make sure you have git-checkout'ed the correct branch.
2720 +
2721 +For aufs5-linux tree,
2722 +- enable CONFIG_AUFS_FS.
2723 +- set other aufs configurations if necessary.
2724 +
2725 +For aufs5-standalone tree,
2726 +There are several ways to build.
2727 +
2728 +1.
2729 +- apply ./aufs5-kbuild.patch to your kernel source files.
2730 +- apply ./aufs5-base.patch too.
2731 +- apply ./aufs5-mmap.patch too.
2732 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2733 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2734 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2735 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2736 +- enable CONFIG_AUFS_FS, you can select either
2737 +  =m or =y.
2738 +- and build your kernel as usual.
2739 +- install the built kernel.
2740 +- install the header files too by "make headers_install" to the
2741 +  directory where you specify. By default, it is $PWD/usr.
2742 +  "make help" shows a brief note for headers_install.
2743 +- and reboot your system.
2744 +
2745 +2.
2746 +- module only (CONFIG_AUFS_FS=m).
2747 +- apply ./aufs5-base.patch to your kernel source files.
2748 +- apply ./aufs5-mmap.patch too.
2749 +- apply ./aufs5-standalone.patch too.
2750 +- build your kernel, don't forget "make headers_install", and reboot.
2751 +- edit ./config.mk and set other aufs configurations if necessary.
2752 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2753 +  every aufs configurations.
2754 +- build the module by simple "make".
2755 +- you can specify ${KDIR} make variable which points to your kernel
2756 +  source tree.
2757 +- install the files
2758 +  + run "make install" to install the aufs module, or copy the built
2759 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2760 +  + run "make install_headers" (instead of headers_install) to install
2761 +    the modified aufs header file (you can specify DESTDIR which is
2762 +    available in aufs standalone version's Makefile only), or copy
2763 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2764 +    you like manually. By default, the target directory is $PWD/usr.
2765 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2766 +  kernel source tree.
2767 +
2768 +Note: The header file aufs_type.h is necessary to build aufs-util
2769 +      as well as "make headers_install" in the kernel source tree.
2770 +      headers_install is subject to be forgotten, but it is essentially
2771 +      necessary, not only for building aufs-util.
2772 +      You may not meet problems without headers_install in some older
2773 +      version though.
2774 +
2775 +And then,
2776 +- read README in aufs-util, build and install it
2777 +- note that your distribution may contain an obsoleted version of
2778 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2779 +  utilities, make sure that your compiler refers the correct aufs header
2780 +  file which is built by "make headers_install."
2781 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2782 +  then run "make install_ulib" too. And refer to the aufs manual in
2783 +  detail.
2784 +
2785 +There several other patches in aufs5-standalone.git. They are all
2786 +optional. When you meet some problems, they will help you.
2787 +- aufs5-loopback.patch
2788 +  Supports a nested loopback mount in a branch-fs. This patch is
2789 +  unnecessary until aufs produces a message like "you may want to try
2790 +  another patch for loopback file".
2791 +- proc_mounts.patch
2792 +  When there are many mountpoints and many mount(2)/umount(2) are
2793 +  running, then /proc/mounts may not show the all mountpoints.  This
2794 +  patch makes /proc/mounts always show the full mountpoints list.
2795 +  If you don't want to apply this patch and meet such problem, then you
2796 +  need to increase the value of 'ProcMounts_Times' make-variable in
2797 +  aufs-util.git as a second best solution.
2798 +- vfs-ino.patch
2799 +  Modifies a system global kernel internal function get_next_ino() in
2800 +  order to stop assigning 0 for an inode-number. Not directly related to
2801 +  aufs, but recommended generally.
2802 +- tmpfs-idr.patch
2803 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2804 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2805 +  duplication of inode number, which is important for backup tools and
2806 +  other utilities. When you find aufs XINO files for tmpfs branch
2807 +  growing too much, try this patch.
2808 +- lockdep-debug.patch
2809 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2810 +  also a caller of VFS functions for branch filesystems, subclassing of
2811 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2812 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2813 +  need to apply this debug patch to expand several constant values.
2814 +  If you don't know what LOCKDEP is, then you don't have apply this
2815 +  patch.
2816 +
2817 +
2818 +4. Usage
2819 +----------------------------------------
2820 +At first, make sure aufs-util are installed, and please read the aufs
2821 +manual, aufs.5 in aufs-util.git tree.
2822 +$ man -l aufs.5
2823 +
2824 +And then,
2825 +$ mkdir /tmp/rw /tmp/aufs
2826 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2827 +
2828 +Here is another example. The result is equivalent.
2829 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2830 +  Or
2831 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2832 +# mount -o remount,append:${HOME} /tmp/aufs
2833 +
2834 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2835 +you modify a file under /tmp/aufs, the one on your home directory is
2836 +not affected, instead the same named file will be newly created under
2837 +/tmp/rw. And all of your modification to a file will be applied to
2838 +the one under /tmp/rw. This is called the file based Copy on Write
2839 +(COW) method.
2840 +Aufs mount options are described in aufs.5.
2841 +If you run chroot or something and make your aufs as a root directory,
2842 +then you need to customize the shutdown script. See the aufs manual in
2843 +detail.
2844 +
2845 +Additionally, there are some sample usages of aufs which are a
2846 +diskless system with network booting, and LiveCD over NFS.
2847 +See sample dir in CVS tree on SourceForge.
2848 +
2849 +
2850 +5. Contact
2851 +----------------------------------------
2852 +When you have any problems or strange behaviour in aufs, please let me
2853 +know with:
2854 +- /proc/mounts (instead of the output of mount(8))
2855 +- /sys/module/aufs/*
2856 +- /sys/fs/aufs/* (if you have them)
2857 +- /debug/aufs/* (if you have them)
2858 +- linux kernel version
2859 +  if your kernel is not plain, for example modified by distributor,
2860 +  the url where i can download its source is necessary too.
2861 +- aufs version which was printed at loading the module or booting the
2862 +  system, instead of the date you downloaded.
2863 +- configuration (define/undefine CONFIG_AUFS_xxx)
2864 +- kernel configuration or /proc/config.gz (if you have it)
2865 +- LSM (linux security module, if you are using)
2866 +- behaviour which you think to be incorrect
2867 +- actual operation, reproducible one is better
2868 +- mailto: aufs-users at lists.sourceforge.net
2869 +
2870 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2871 +and Feature Requests) on SourceForge. Please join and write to
2872 +aufs-users ML.
2873 +
2874 +
2875 +6. Acknowledgements
2876 +----------------------------------------
2877 +Thanks to everyone who have tried and are using aufs, whoever
2878 +have reported a bug or any feedback.
2879 +
2880 +Especially donators:
2881 +Tomas Matejicek(slax.org) made a donation (much more than once).
2882 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2883 +       scripts) is making "doubling" donations.
2884 +       Unfortunately I cannot list all of the donators, but I really
2885 +       appreciate.
2886 +       It ends Aug 2010, but the ordinary donation URL is still available.
2887 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2888 +Dai Itasaka made a donation (2007/8).
2889 +Chuck Smith made a donation (2008/4, 10 and 12).
2890 +Henk Schoneveld made a donation (2008/9).
2891 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2892 +Francois Dupoux made a donation (2008/11).
2893 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2894 +       aufs2 GIT tree (2009/2).
2895 +William Grant made a donation (2009/3).
2896 +Patrick Lane made a donation (2009/4).
2897 +The Mail Archive (mail-archive.com) made donations (2009/5).
2898 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2899 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2900 +Pavel Pronskiy made a donation (2011/2).
2901 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2902 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2903 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2904 +11).
2905 +Sam Liddicott made a donation (2011/9).
2906 +Era Scarecrow made a donation (2013/4).
2907 +Bor Ratajc made a donation (2013/4).
2908 +Alessandro Gorreta made a donation (2013/4).
2909 +POIRETTE Marc made a donation (2013/4).
2910 +Alessandro Gorreta made a donation (2013/4).
2911 +lauri kasvandik made a donation (2013/5).
2912 +"pemasu from Finland" made a donation (2013/7).
2913 +The Parted Magic Project made a donation (2013/9 and 11).
2914 +Pavel Barta made a donation (2013/10).
2915 +Nikolay Pertsev made a donation (2014/5).
2916 +James B made a donation (2014/7 and 2015/7).
2917 +Stefano Di Biase made a donation (2014/8).
2918 +Daniel Epellei made a donation (2015/1).
2919 +OmegaPhil made a donation (2016/1, 2018/4).
2920 +Tomasz Szewczyk made a donation (2016/4).
2921 +James Burry made a donation (2016/12).
2922 +Carsten Rose made a donation (2018/9).
2923 +Porteus Kiosk made a donation (2018/10).
2924 +
2925 +Thank you very much.
2926 +Donations are always, including future donations, very important and
2927 +helpful for me to keep on developing aufs.
2928 +
2929 +
2930 +7.
2931 +----------------------------------------
2932 +If you are an experienced user, no explanation is needed. Aufs is
2933 +just a linux filesystem.
2934 +
2935 +
2936 +Enjoy!
2937 +
2938 +# Local variables: ;
2939 +# mode: text;
2940 +# End: ;
2941 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2942 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2943 +++ linux/fs/aufs/aufs.h        2020-01-27 10:57:18.165538015 +0100
2944 @@ -0,0 +1,62 @@
2945 +/* SPDX-License-Identifier: GPL-2.0 */
2946 +/*
2947 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2948 + *
2949 + * This program, aufs is free software; you can redistribute it and/or modify
2950 + * it under the terms of the GNU General Public License as published by
2951 + * the Free Software Foundation; either version 2 of the License, or
2952 + * (at your option) any later version.
2953 + *
2954 + * This program is distributed in the hope that it will be useful,
2955 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2956 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2957 + * GNU General Public License for more details.
2958 + *
2959 + * You should have received a copy of the GNU General Public License
2960 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2961 + */
2962 +
2963 +/*
2964 + * all header files
2965 + */
2966 +
2967 +#ifndef __AUFS_H__
2968 +#define __AUFS_H__
2969 +
2970 +#ifdef __KERNEL__
2971 +
2972 +#define AuStub(type, name, body, ...) \
2973 +       static inline type name(__VA_ARGS__) { body; }
2974 +
2975 +#define AuStubVoid(name, ...) \
2976 +       AuStub(void, name, , __VA_ARGS__)
2977 +#define AuStubInt0(name, ...) \
2978 +       AuStub(int, name, return 0, __VA_ARGS__)
2979 +
2980 +#include "debug.h"
2981 +
2982 +#include "branch.h"
2983 +#include "cpup.h"
2984 +#include "dcsub.h"
2985 +#include "dbgaufs.h"
2986 +#include "dentry.h"
2987 +#include "dir.h"
2988 +#include "dirren.h"
2989 +#include "dynop.h"
2990 +#include "file.h"
2991 +#include "fstype.h"
2992 +#include "hbl.h"
2993 +#include "inode.h"
2994 +#include "lcnt.h"
2995 +#include "loop.h"
2996 +#include "module.h"
2997 +#include "opts.h"
2998 +#include "rwsem.h"
2999 +#include "super.h"
3000 +#include "sysaufs.h"
3001 +#include "vfsub.h"
3002 +#include "whout.h"
3003 +#include "wkq.h"
3004 +
3005 +#endif /* __KERNEL__ */
3006 +#endif /* __AUFS_H__ */
3007 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
3008 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
3009 +++ linux/fs/aufs/branch.c      2020-01-27 10:57:18.165538015 +0100
3010 @@ -0,0 +1,1428 @@
3011 +// SPDX-License-Identifier: GPL-2.0
3012 +/*
3013 + * Copyright (C) 2005-2020 Junjiro R. Okajima
3014 + *
3015 + * This program, aufs is free software; you can redistribute it and/or modify
3016 + * it under the terms of the GNU General Public License as published by
3017 + * the Free Software Foundation; either version 2 of the License, or
3018 + * (at your option) any later version.
3019 + *
3020 + * This program is distributed in the hope that it will be useful,
3021 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3022 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3023 + * GNU General Public License for more details.
3024 + *
3025 + * You should have received a copy of the GNU General Public License
3026 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
3027 + */
3028 +
3029 +/*
3030 + * branch management
3031 + */
3032 +
3033 +#include <linux/compat.h>
3034 +#include <linux/statfs.h>
3035 +#include "aufs.h"
3036 +
3037 +/*
3038 + * free a single branch
3039 + */
3040 +static void au_br_do_free(struct au_branch *br)
3041 +{
3042 +       int i;
3043 +       struct au_wbr *wbr;
3044 +       struct au_dykey **key;
3045 +
3046 +       au_hnotify_fin_br(br);
3047 +       /* always, regardless the mount option */
3048 +       au_dr_hino_free(&br->br_dirren);
3049 +       au_xino_put(br);
3050 +
3051 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
3052 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
3053 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
3054 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
3055 +
3056 +       wbr = br->br_wbr;
3057 +       if (wbr) {
3058 +               for (i = 0; i < AuBrWh_Last; i++)
3059 +                       dput(wbr->wbr_wh[i]);
3060 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3061 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3062 +       }
3063 +
3064 +       if (br->br_fhsm) {
3065 +               au_br_fhsm_fin(br->br_fhsm);
3066 +               au_kfree_try_rcu(br->br_fhsm);
3067 +       }
3068 +
3069 +       key = br->br_dykey;
3070 +       for (i = 0; i < AuBrDynOp; i++, key++)
3071 +               if (*key)
3072 +                       au_dy_put(*key);
3073 +               else
3074 +                       break;
3075 +
3076 +       /* recursive lock, s_umount of branch's */
3077 +       /* synchronize_rcu(); */ /* why? */
3078 +       lockdep_off();
3079 +       path_put(&br->br_path);
3080 +       lockdep_on();
3081 +       au_kfree_rcu(wbr);
3082 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3083 +       au_lcnt_wait_for_fin(&br->br_count);
3084 +       /* I don't know why, but percpu_refcount requires this */
3085 +       /* synchronize_rcu(); */
3086 +       au_kfree_rcu(br);
3087 +}
3088 +
3089 +/*
3090 + * frees all branches
3091 + */
3092 +void au_br_free(struct au_sbinfo *sbinfo)
3093 +{
3094 +       aufs_bindex_t bmax;
3095 +       struct au_branch **br;
3096 +
3097 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3098 +
3099 +       bmax = sbinfo->si_bbot + 1;
3100 +       br = sbinfo->si_branch;
3101 +       while (bmax--)
3102 +               au_br_do_free(*br++);
3103 +}
3104 +
3105 +/*
3106 + * find the index of a branch which is specified by @br_id.
3107 + */
3108 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3109 +{
3110 +       aufs_bindex_t bindex, bbot;
3111 +
3112 +       bbot = au_sbbot(sb);
3113 +       for (bindex = 0; bindex <= bbot; bindex++)
3114 +               if (au_sbr_id(sb, bindex) == br_id)
3115 +                       return bindex;
3116 +       return -1;
3117 +}
3118 +
3119 +/* ---------------------------------------------------------------------- */
3120 +
3121 +/*
3122 + * add a branch
3123 + */
3124 +
3125 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3126 +                       struct dentry *h_root)
3127 +{
3128 +       if (unlikely(h_adding == h_root
3129 +                    || au_test_loopback_overlap(sb, h_adding)))
3130 +               return 1;
3131 +       if (h_adding->d_sb != h_root->d_sb)
3132 +               return 0;
3133 +       return au_test_subdir(h_adding, h_root)
3134 +               || au_test_subdir(h_root, h_adding);
3135 +}
3136 +
3137 +/*
3138 + * returns a newly allocated branch. @new_nbranch is a number of branches
3139 + * after adding a branch.
3140 + */
3141 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3142 +                                    int perm)
3143 +{
3144 +       struct au_branch *add_branch;
3145 +       struct dentry *root;
3146 +       struct inode *inode;
3147 +       int err;
3148 +
3149 +       err = -ENOMEM;
3150 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3151 +       if (unlikely(!add_branch))
3152 +               goto out;
3153 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3154 +       if (unlikely(!add_branch->br_xino))
3155 +               goto out_br;
3156 +       err = au_hnotify_init_br(add_branch, perm);
3157 +       if (unlikely(err))
3158 +               goto out_xino;
3159 +
3160 +       if (au_br_writable(perm)) {
3161 +               /* may be freed separately at changing the branch permission */
3162 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3163 +                                            GFP_NOFS);
3164 +               if (unlikely(!add_branch->br_wbr))
3165 +                       goto out_hnotify;
3166 +       }
3167 +
3168 +       if (au_br_fhsm(perm)) {
3169 +               err = au_fhsm_br_alloc(add_branch);
3170 +               if (unlikely(err))
3171 +                       goto out_wbr;
3172 +       }
3173 +
3174 +       root = sb->s_root;
3175 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3176 +       if (!err)
3177 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3178 +       if (!err) {
3179 +               inode = d_inode(root);
3180 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3181 +                                       /*may_shrink*/0);
3182 +       }
3183 +       if (!err)
3184 +               return add_branch; /* success */
3185 +
3186 +out_wbr:
3187 +       au_kfree_rcu(add_branch->br_wbr);
3188 +out_hnotify:
3189 +       au_hnotify_fin_br(add_branch);
3190 +out_xino:
3191 +       au_xino_put(add_branch);
3192 +out_br:
3193 +       au_kfree_rcu(add_branch);
3194 +out:
3195 +       return ERR_PTR(err);
3196 +}
3197 +
3198 +/*
3199 + * test if the branch permission is legal or not.
3200 + */
3201 +static int test_br(struct inode *inode, int brperm, char *path)
3202 +{
3203 +       int err;
3204 +
3205 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3206 +       if (!err)
3207 +               goto out;
3208 +
3209 +       err = -EINVAL;
3210 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3211 +
3212 +out:
3213 +       return err;
3214 +}
3215 +
3216 +/*
3217 + * returns:
3218 + * 0: success, the caller will add it
3219 + * plus: success, it is already unified, the caller should ignore it
3220 + * minus: error
3221 + */
3222 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3223 +{
3224 +       int err;
3225 +       aufs_bindex_t bbot, bindex;
3226 +       struct dentry *root, *h_dentry;
3227 +       struct inode *inode, *h_inode;
3228 +
3229 +       root = sb->s_root;
3230 +       bbot = au_sbbot(sb);
3231 +       if (unlikely(bbot >= 0
3232 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3233 +               err = 1;
3234 +               if (!remount) {
3235 +                       err = -EINVAL;
3236 +                       pr_err("%s duplicated\n", add->pathname);
3237 +               }
3238 +               goto out;
3239 +       }
3240 +
3241 +       err = -ENOSPC; /* -E2BIG; */
3242 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3243 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3244 +               pr_err("number of branches exceeded %s\n", add->pathname);
3245 +               goto out;
3246 +       }
3247 +
3248 +       err = -EDOM;
3249 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3250 +               pr_err("bad index %d\n", add->bindex);
3251 +               goto out;
3252 +       }
3253 +
3254 +       inode = d_inode(add->path.dentry);
3255 +       err = -ENOENT;
3256 +       if (unlikely(!inode->i_nlink)) {
3257 +               pr_err("no existence %s\n", add->pathname);
3258 +               goto out;
3259 +       }
3260 +
3261 +       err = -EINVAL;
3262 +       if (unlikely(inode->i_sb == sb)) {
3263 +               pr_err("%s must be outside\n", add->pathname);
3264 +               goto out;
3265 +       }
3266 +
3267 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3268 +               pr_err("unsupported filesystem, %s (%s)\n",
3269 +                      add->pathname, au_sbtype(inode->i_sb));
3270 +               goto out;
3271 +       }
3272 +
3273 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3274 +               pr_err("already stacked, %s (%s)\n",
3275 +                      add->pathname, au_sbtype(inode->i_sb));
3276 +               goto out;
3277 +       }
3278 +
3279 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3280 +       if (unlikely(err))
3281 +               goto out;
3282 +
3283 +       if (bbot < 0)
3284 +               return 0; /* success */
3285 +
3286 +       err = -EINVAL;
3287 +       for (bindex = 0; bindex <= bbot; bindex++)
3288 +               if (unlikely(test_overlap(sb, add->path.dentry,
3289 +                                         au_h_dptr(root, bindex)))) {
3290 +                       pr_err("%s is overlapped\n", add->pathname);
3291 +                       goto out;
3292 +               }
3293 +
3294 +       err = 0;
3295 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3296 +               h_dentry = au_h_dptr(root, 0);
3297 +               h_inode = d_inode(h_dentry);
3298 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3299 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3300 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3301 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3302 +                               add->pathname,
3303 +                               i_uid_read(inode), i_gid_read(inode),
3304 +                               (inode->i_mode & S_IALLUGO),
3305 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3306 +                               (h_inode->i_mode & S_IALLUGO));
3307 +       }
3308 +
3309 +out:
3310 +       return err;
3311 +}
3312 +
3313 +/*
3314 + * initialize or clean the whiteouts for an adding branch
3315 + */
3316 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3317 +                        int new_perm)
3318 +{
3319 +       int err, old_perm;
3320 +       aufs_bindex_t bindex;
3321 +       struct inode *h_inode;
3322 +       struct au_wbr *wbr;
3323 +       struct au_hinode *hdir;
3324 +       struct dentry *h_dentry;
3325 +
3326 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3327 +       if (unlikely(err))
3328 +               goto out;
3329 +
3330 +       wbr = br->br_wbr;
3331 +       old_perm = br->br_perm;
3332 +       br->br_perm = new_perm;
3333 +       hdir = NULL;
3334 +       h_inode = NULL;
3335 +       bindex = au_br_index(sb, br->br_id);
3336 +       if (0 <= bindex) {
3337 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3338 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3339 +       } else {
3340 +               h_dentry = au_br_dentry(br);
3341 +               h_inode = d_inode(h_dentry);
3342 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3343 +       }
3344 +       if (!wbr)
3345 +               err = au_wh_init(br, sb);
3346 +       else {
3347 +               wbr_wh_write_lock(wbr);
3348 +               err = au_wh_init(br, sb);
3349 +               wbr_wh_write_unlock(wbr);
3350 +       }
3351 +       if (hdir)
3352 +               au_hn_inode_unlock(hdir);
3353 +       else
3354 +               inode_unlock(h_inode);
3355 +       vfsub_mnt_drop_write(au_br_mnt(br));
3356 +       br->br_perm = old_perm;
3357 +
3358 +       if (!err && wbr && !au_br_writable(new_perm)) {
3359 +               au_kfree_rcu(wbr);
3360 +               br->br_wbr = NULL;
3361 +       }
3362 +
3363 +out:
3364 +       return err;
3365 +}
3366 +
3367 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3368 +                      int perm)
3369 +{
3370 +       int err;
3371 +       struct kstatfs kst;
3372 +       struct au_wbr *wbr;
3373 +
3374 +       wbr = br->br_wbr;
3375 +       au_rw_init(&wbr->wbr_wh_rwsem);
3376 +       atomic_set(&wbr->wbr_wh_running, 0);
3377 +
3378 +       /*
3379 +        * a limit for rmdir/rename a dir
3380 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3381 +        */
3382 +       err = vfs_statfs(&br->br_path, &kst);
3383 +       if (unlikely(err))
3384 +               goto out;
3385 +       err = -EINVAL;
3386 +       if (kst.f_namelen >= NAME_MAX)
3387 +               err = au_br_init_wh(sb, br, perm);
3388 +       else
3389 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3390 +                      au_br_dentry(br),
3391 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3392 +
3393 +out:
3394 +       return err;
3395 +}
3396 +
3397 +/* initialize a new branch */
3398 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3399 +                     struct au_opt_add *add)
3400 +{
3401 +       int err;
3402 +       struct au_branch *brbase;
3403 +       struct file *xf;
3404 +       struct inode *h_inode;
3405 +
3406 +       err = 0;
3407 +       br->br_perm = add->perm;
3408 +       br->br_path = add->path; /* set first, path_get() later */
3409 +       spin_lock_init(&br->br_dykey_lock);
3410 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3411 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3412 +       br->br_id = au_new_br_id(sb);
3413 +       AuDebugOn(br->br_id < 0);
3414 +
3415 +       /* always, regardless the given option */
3416 +       err = au_dr_br_init(sb, br, &add->path);
3417 +       if (unlikely(err))
3418 +               goto out_err;
3419 +
3420 +       if (au_br_writable(add->perm)) {
3421 +               err = au_wbr_init(br, sb, add->perm);
3422 +               if (unlikely(err))
3423 +                       goto out_err;
3424 +       }
3425 +
3426 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3427 +               brbase = au_sbr(sb, 0);
3428 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3429 +               AuDebugOn(!xf);
3430 +               h_inode = d_inode(add->path.dentry);
3431 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3432 +               if (unlikely(err)) {
3433 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3434 +                       goto out_err;
3435 +               }
3436 +       }
3437 +
3438 +       sysaufs_br_init(br);
3439 +       path_get(&br->br_path);
3440 +       goto out; /* success */
3441 +
3442 +out_err:
3443 +       memset(&br->br_path, 0, sizeof(br->br_path));
3444 +out:
3445 +       return err;
3446 +}
3447 +
3448 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3449 +                            struct au_branch *br, aufs_bindex_t bbot,
3450 +                            aufs_bindex_t amount)
3451 +{
3452 +       struct au_branch **brp;
3453 +
3454 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3455 +
3456 +       brp = sbinfo->si_branch + bindex;
3457 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3458 +       *brp = br;
3459 +       sbinfo->si_bbot++;
3460 +       if (unlikely(bbot < 0))
3461 +               sbinfo->si_bbot = 0;
3462 +}
3463 +
3464 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3465 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3466 +{
3467 +       struct au_hdentry *hdp;
3468 +
3469 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3470 +
3471 +       hdp = au_hdentry(dinfo, bindex);
3472 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3473 +       au_h_dentry_init(hdp);
3474 +       dinfo->di_bbot++;
3475 +       if (unlikely(bbot < 0))
3476 +               dinfo->di_btop = 0;
3477 +}
3478 +
3479 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3480 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3481 +{
3482 +       struct au_hinode *hip;
3483 +
3484 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3485 +
3486 +       hip = au_hinode(iinfo, bindex);
3487 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3488 +       au_hinode_init(hip);
3489 +       iinfo->ii_bbot++;
3490 +       if (unlikely(bbot < 0))
3491 +               iinfo->ii_btop = 0;
3492 +}
3493 +
3494 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3495 +                        aufs_bindex_t bindex)
3496 +{
3497 +       struct dentry *root, *h_dentry;
3498 +       struct inode *root_inode, *h_inode;
3499 +       aufs_bindex_t bbot, amount;
3500 +
3501 +       root = sb->s_root;
3502 +       root_inode = d_inode(root);
3503 +       bbot = au_sbbot(sb);
3504 +       amount = bbot + 1 - bindex;
3505 +       h_dentry = au_br_dentry(br);
3506 +       au_sbilist_lock();
3507 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3508 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3509 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3510 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3511 +       h_inode = d_inode(h_dentry);
3512 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3513 +       au_sbilist_unlock();
3514 +}
3515 +
3516 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3517 +{
3518 +       int err;
3519 +       aufs_bindex_t bbot, add_bindex;
3520 +       struct dentry *root, *h_dentry;
3521 +       struct inode *root_inode;
3522 +       struct au_branch *add_branch;
3523 +
3524 +       root = sb->s_root;
3525 +       root_inode = d_inode(root);
3526 +       IMustLock(root_inode);
3527 +       IiMustWriteLock(root_inode);
3528 +       err = test_add(sb, add, remount);
3529 +       if (unlikely(err < 0))
3530 +               goto out;
3531 +       if (err) {
3532 +               err = 0;
3533 +               goto out; /* success */
3534 +       }
3535 +
3536 +       bbot = au_sbbot(sb);
3537 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3538 +       err = PTR_ERR(add_branch);
3539 +       if (IS_ERR(add_branch))
3540 +               goto out;
3541 +
3542 +       err = au_br_init(add_branch, sb, add);
3543 +       if (unlikely(err)) {
3544 +               au_br_do_free(add_branch);
3545 +               goto out;
3546 +       }
3547 +
3548 +       add_bindex = add->bindex;
3549 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3550 +       au_br_do_add(sb, add_branch, add_bindex);
3551 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3552 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3553 +
3554 +       h_dentry = add->path.dentry;
3555 +       if (!add_bindex) {
3556 +               au_cpup_attr_all(root_inode, /*force*/1);
3557 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3558 +       } else
3559 +               au_add_nlink(root_inode, d_inode(h_dentry));
3560 +
3561 +out:
3562 +       return err;
3563 +}
3564 +
3565 +/* ---------------------------------------------------------------------- */
3566 +
3567 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3568 +                                      unsigned long long max __maybe_unused,
3569 +                                      void *arg)
3570 +{
3571 +       unsigned long long n;
3572 +       struct file **p, *f;
3573 +       struct hlist_bl_head *files;
3574 +       struct hlist_bl_node *pos;
3575 +       struct au_finfo *finfo;
3576 +
3577 +       n = 0;
3578 +       p = a;
3579 +       files = &au_sbi(sb)->si_files;
3580 +       hlist_bl_lock(files);
3581 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3582 +               f = finfo->fi_file;
3583 +               if (file_count(f)
3584 +                   && !special_file(file_inode(f)->i_mode)) {
3585 +                       get_file(f);
3586 +                       *p++ = f;
3587 +                       n++;
3588 +                       AuDebugOn(n > max);
3589 +               }
3590 +       }
3591 +       hlist_bl_unlock(files);
3592 +
3593 +       return n;
3594 +}
3595 +
3596 +static struct file **au_farray_alloc(struct super_block *sb,
3597 +                                    unsigned long long *max)
3598 +{
3599 +       struct au_sbinfo *sbi;
3600 +
3601 +       sbi = au_sbi(sb);
3602 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3603 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3604 +}
3605 +
3606 +static void au_farray_free(struct file **a, unsigned long long max)
3607 +{
3608 +       unsigned long long ull;
3609 +
3610 +       for (ull = 0; ull < max; ull++)
3611 +               if (a[ull])
3612 +                       fput(a[ull]);
3613 +       kvfree(a);
3614 +}
3615 +
3616 +/* ---------------------------------------------------------------------- */
3617 +
3618 +/*
3619 + * delete a branch
3620 + */
3621 +
3622 +/* to show the line number, do not make it inlined function */
3623 +#define AuVerbose(do_info, fmt, ...) do { \
3624 +       if (do_info) \
3625 +               pr_info(fmt, ##__VA_ARGS__); \
3626 +} while (0)
3627 +
3628 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3629 +                        aufs_bindex_t bbot)
3630 +{
3631 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3632 +}
3633 +
3634 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3635 +                        aufs_bindex_t bbot)
3636 +{
3637 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3638 +}
3639 +
3640 +/*
3641 + * test if the branch is deletable or not.
3642 + */
3643 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3644 +                           unsigned int sigen, const unsigned int verbose)
3645 +{
3646 +       int err, i, j, ndentry;
3647 +       aufs_bindex_t btop, bbot;
3648 +       struct au_dcsub_pages dpages;
3649 +       struct au_dpage *dpage;
3650 +       struct dentry *d;
3651 +
3652 +       err = au_dpages_init(&dpages, GFP_NOFS);
3653 +       if (unlikely(err))
3654 +               goto out;
3655 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3656 +       if (unlikely(err))
3657 +               goto out_dpages;
3658 +
3659 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3660 +               dpage = dpages.dpages + i;
3661 +               ndentry = dpage->ndentry;
3662 +               for (j = 0; !err && j < ndentry; j++) {
3663 +                       d = dpage->dentries[j];
3664 +                       AuDebugOn(au_dcount(d) <= 0);
3665 +                       if (!au_digen_test(d, sigen)) {
3666 +                               di_read_lock_child(d, AuLock_IR);
3667 +                               if (unlikely(au_dbrange_test(d))) {
3668 +                                       di_read_unlock(d, AuLock_IR);
3669 +                                       continue;
3670 +                               }
3671 +                       } else {
3672 +                               di_write_lock_child(d);
3673 +                               if (unlikely(au_dbrange_test(d))) {
3674 +                                       di_write_unlock(d);
3675 +                                       continue;
3676 +                               }
3677 +                               err = au_reval_dpath(d, sigen);
3678 +                               if (!err)
3679 +                                       di_downgrade_lock(d, AuLock_IR);
3680 +                               else {
3681 +                                       di_write_unlock(d);
3682 +                                       break;
3683 +                               }
3684 +                       }
3685 +
3686 +                       /* AuDbgDentry(d); */
3687 +                       btop = au_dbtop(d);
3688 +                       bbot = au_dbbot(d);
3689 +                       if (btop <= bindex
3690 +                           && bindex <= bbot
3691 +                           && au_h_dptr(d, bindex)
3692 +                           && au_test_dbusy(d, btop, bbot)) {
3693 +                               err = -EBUSY;
3694 +                               AuVerbose(verbose, "busy %pd\n", d);
3695 +                               AuDbgDentry(d);
3696 +                       }
3697 +                       di_read_unlock(d, AuLock_IR);
3698 +               }
3699 +       }
3700 +
3701 +out_dpages:
3702 +       au_dpages_free(&dpages);
3703 +out:
3704 +       return err;
3705 +}
3706 +
3707 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3708 +                          unsigned int sigen, const unsigned int verbose)
3709 +{
3710 +       int err;
3711 +       unsigned long long max, ull;
3712 +       struct inode *i, **array;
3713 +       aufs_bindex_t btop, bbot;
3714 +
3715 +       array = au_iarray_alloc(sb, &max);
3716 +       err = PTR_ERR(array);
3717 +       if (IS_ERR(array))
3718 +               goto out;
3719 +
3720 +       err = 0;
3721 +       AuDbg("b%d\n", bindex);
3722 +       for (ull = 0; !err && ull < max; ull++) {
3723 +               i = array[ull];
3724 +               if (unlikely(!i))
3725 +                       break;
3726 +               if (i->i_ino == AUFS_ROOT_INO)
3727 +                       continue;
3728 +
3729 +               /* AuDbgInode(i); */
3730 +               if (au_iigen(i, NULL) == sigen)
3731 +                       ii_read_lock_child(i);
3732 +               else {
3733 +                       ii_write_lock_child(i);
3734 +                       err = au_refresh_hinode_self(i);
3735 +                       au_iigen_dec(i);
3736 +                       if (!err)
3737 +                               ii_downgrade_lock(i);
3738 +                       else {
3739 +                               ii_write_unlock(i);
3740 +                               break;
3741 +                       }
3742 +               }
3743 +
3744 +               btop = au_ibtop(i);
3745 +               bbot = au_ibbot(i);
3746 +               if (btop <= bindex
3747 +                   && bindex <= bbot
3748 +                   && au_h_iptr(i, bindex)
3749 +                   && au_test_ibusy(i, btop, bbot)) {
3750 +                       err = -EBUSY;
3751 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3752 +                       AuDbgInode(i);
3753 +               }
3754 +               ii_read_unlock(i);
3755 +       }
3756 +       au_iarray_free(array, max);
3757 +
3758 +out:
3759 +       return err;
3760 +}
3761 +
3762 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3763 +                             const unsigned int verbose)
3764 +{
3765 +       int err;
3766 +       unsigned int sigen;
3767 +
3768 +       sigen = au_sigen(root->d_sb);
3769 +       DiMustNoWaiters(root);
3770 +       IiMustNoWaiters(d_inode(root));
3771 +       di_write_unlock(root);
3772 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3773 +       if (!err)
3774 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3775 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3776 +
3777 +       return err;
3778 +}
3779 +
3780 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3781 +                        struct file **to_free, int *idx)
3782 +{
3783 +       int err;
3784 +       unsigned char matched, root;
3785 +       aufs_bindex_t bindex, bbot;
3786 +       struct au_fidir *fidir;
3787 +       struct au_hfile *hfile;
3788 +
3789 +       err = 0;
3790 +       root = IS_ROOT(file->f_path.dentry);
3791 +       if (root) {
3792 +               get_file(file);
3793 +               to_free[*idx] = file;
3794 +               (*idx)++;
3795 +               goto out;
3796 +       }
3797 +
3798 +       matched = 0;
3799 +       fidir = au_fi(file)->fi_hdir;
3800 +       AuDebugOn(!fidir);
3801 +       bbot = au_fbbot_dir(file);
3802 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3803 +               hfile = fidir->fd_hfile + bindex;
3804 +               if (!hfile->hf_file)
3805 +                       continue;
3806 +
3807 +               if (hfile->hf_br->br_id == br_id) {
3808 +                       matched = 1;
3809 +                       break;
3810 +               }
3811 +       }
3812 +       if (matched)
3813 +               err = -EBUSY;
3814 +
3815 +out:
3816 +       return err;
3817 +}
3818 +
3819 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3820 +                         struct file **to_free, int opened)
3821 +{
3822 +       int err, idx;
3823 +       unsigned long long ull, max;
3824 +       aufs_bindex_t btop;
3825 +       struct file *file, **array;
3826 +       struct dentry *root;
3827 +       struct au_hfile *hfile;
3828 +
3829 +       array = au_farray_alloc(sb, &max);
3830 +       err = PTR_ERR(array);
3831 +       if (IS_ERR(array))
3832 +               goto out;
3833 +
3834 +       err = 0;
3835 +       idx = 0;
3836 +       root = sb->s_root;
3837 +       di_write_unlock(root);
3838 +       for (ull = 0; ull < max; ull++) {
3839 +               file = array[ull];
3840 +               if (unlikely(!file))
3841 +                       break;
3842 +
3843 +               /* AuDbg("%pD\n", file); */
3844 +               fi_read_lock(file);
3845 +               btop = au_fbtop(file);
3846 +               if (!d_is_dir(file->f_path.dentry)) {
3847 +                       hfile = &au_fi(file)->fi_htop;
3848 +                       if (hfile->hf_br->br_id == br_id)
3849 +                               err = -EBUSY;
3850 +               } else
3851 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3852 +               fi_read_unlock(file);
3853 +               if (unlikely(err))
3854 +                       break;
3855 +       }
3856 +       di_write_lock_child(root);
3857 +       au_farray_free(array, max);
3858 +       AuDebugOn(idx > opened);
3859 +
3860 +out:
3861 +       return err;
3862 +}
3863 +
3864 +static void br_del_file(struct file **to_free, unsigned long long opened,
3865 +                       aufs_bindex_t br_id)
3866 +{
3867 +       unsigned long long ull;
3868 +       aufs_bindex_t bindex, btop, bbot, bfound;
3869 +       struct file *file;
3870 +       struct au_fidir *fidir;
3871 +       struct au_hfile *hfile;
3872 +
3873 +       for (ull = 0; ull < opened; ull++) {
3874 +               file = to_free[ull];
3875 +               if (unlikely(!file))
3876 +                       break;
3877 +
3878 +               /* AuDbg("%pD\n", file); */
3879 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3880 +               bfound = -1;
3881 +               fidir = au_fi(file)->fi_hdir;
3882 +               AuDebugOn(!fidir);
3883 +               fi_write_lock(file);
3884 +               btop = au_fbtop(file);
3885 +               bbot = au_fbbot_dir(file);
3886 +               for (bindex = btop; bindex <= bbot; bindex++) {
3887 +                       hfile = fidir->fd_hfile + bindex;
3888 +                       if (!hfile->hf_file)
3889 +                               continue;
3890 +
3891 +                       if (hfile->hf_br->br_id == br_id) {
3892 +                               bfound = bindex;
3893 +                               break;
3894 +                       }
3895 +               }
3896 +               AuDebugOn(bfound < 0);
3897 +               au_set_h_fptr(file, bfound, NULL);
3898 +               if (bfound == btop) {
3899 +                       for (btop++; btop <= bbot; btop++)
3900 +                               if (au_hf_dir(file, btop)) {
3901 +                                       au_set_fbtop(file, btop);
3902 +                                       break;
3903 +                               }
3904 +               }
3905 +               fi_write_unlock(file);
3906 +       }
3907 +}
3908 +
3909 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3910 +                            const aufs_bindex_t bindex,
3911 +                            const aufs_bindex_t bbot)
3912 +{
3913 +       struct au_branch **brp, **p;
3914 +
3915 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3916 +
3917 +       brp = sbinfo->si_branch + bindex;
3918 +       if (bindex < bbot)
3919 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3920 +       sbinfo->si_branch[0 + bbot] = NULL;
3921 +       sbinfo->si_bbot--;
3922 +
3923 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3924 +                       /*may_shrink*/1);
3925 +       if (p)
3926 +               sbinfo->si_branch = p;
3927 +       /* harmless error */
3928 +}
3929 +
3930 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3931 +                            const aufs_bindex_t bbot)
3932 +{
3933 +       struct au_hdentry *hdp, *p;
3934 +
3935 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3936 +
3937 +       hdp = au_hdentry(dinfo, bindex);
3938 +       if (bindex < bbot)
3939 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3940 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3941 +       dinfo->di_bbot--;
3942 +
3943 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3944 +                       /*may_shrink*/1);
3945 +       if (p)
3946 +               dinfo->di_hdentry = p;
3947 +       /* harmless error */
3948 +}
3949 +
3950 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3951 +                            const aufs_bindex_t bbot)
3952 +{
3953 +       struct au_hinode *hip, *p;
3954 +
3955 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3956 +
3957 +       hip = au_hinode(iinfo, bindex);
3958 +       if (bindex < bbot)
3959 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3960 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3961 +       iinfo->ii_bbot--;
3962 +
3963 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3964 +                       /*may_shrink*/1);
3965 +       if (p)
3966 +               iinfo->ii_hinode = p;
3967 +       /* harmless error */
3968 +}
3969 +
3970 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3971 +                        struct au_branch *br)
3972 +{
3973 +       aufs_bindex_t bbot;
3974 +       struct au_sbinfo *sbinfo;
3975 +       struct dentry *root, *h_root;
3976 +       struct inode *inode, *h_inode;
3977 +       struct au_hinode *hinode;
3978 +
3979 +       SiMustWriteLock(sb);
3980 +
3981 +       root = sb->s_root;
3982 +       inode = d_inode(root);
3983 +       sbinfo = au_sbi(sb);
3984 +       bbot = sbinfo->si_bbot;
3985 +
3986 +       h_root = au_h_dptr(root, bindex);
3987 +       hinode = au_hi(inode, bindex);
3988 +       h_inode = au_igrab(hinode->hi_inode);
3989 +       au_hiput(hinode);
3990 +
3991 +       au_sbilist_lock();
3992 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3993 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3994 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3995 +       au_sbilist_unlock();
3996 +
3997 +       /* ignore an error */
3998 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3999 +
4000 +       dput(h_root);
4001 +       iput(h_inode);
4002 +       au_br_do_free(br);
4003 +}
4004 +
4005 +static unsigned long long empty_cb(struct super_block *sb, void *array,
4006 +                                  unsigned long long max, void *arg)
4007 +{
4008 +       return max;
4009 +}
4010 +
4011 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
4012 +{
4013 +       int err, rerr, i;
4014 +       unsigned long long opened;
4015 +       unsigned int mnt_flags;
4016 +       aufs_bindex_t bindex, bbot, br_id;
4017 +       unsigned char do_wh, verbose;
4018 +       struct au_branch *br;
4019 +       struct au_wbr *wbr;
4020 +       struct dentry *root;
4021 +       struct file **to_free;
4022 +
4023 +       err = 0;
4024 +       opened = 0;
4025 +       to_free = NULL;
4026 +       root = sb->s_root;
4027 +       bindex = au_find_dbindex(root, del->h_path.dentry);
4028 +       if (bindex < 0) {
4029 +               if (remount)
4030 +                       goto out; /* success */
4031 +               err = -ENOENT;
4032 +               pr_err("%s no such branch\n", del->pathname);
4033 +               goto out;
4034 +       }
4035 +       AuDbg("bindex b%d\n", bindex);
4036 +
4037 +       err = -EBUSY;
4038 +       mnt_flags = au_mntflags(sb);
4039 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4040 +       bbot = au_sbbot(sb);
4041 +       if (unlikely(!bbot)) {
4042 +               AuVerbose(verbose, "no more branches left\n");
4043 +               goto out;
4044 +       }
4045 +
4046 +       br = au_sbr(sb, bindex);
4047 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
4048 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
4049 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
4050 +               goto out;
4051 +       }
4052 +
4053 +       br_id = br->br_id;
4054 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
4055 +       if (unlikely(opened)) {
4056 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
4057 +               err = PTR_ERR(to_free);
4058 +               if (IS_ERR(to_free))
4059 +                       goto out;
4060 +
4061 +               err = test_file_busy(sb, br_id, to_free, opened);
4062 +               if (unlikely(err)) {
4063 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4064 +                       goto out;
4065 +               }
4066 +       }
4067 +
4068 +       wbr = br->br_wbr;
4069 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4070 +       if (do_wh) {
4071 +               /* instead of WbrWhMustWriteLock(wbr) */
4072 +               SiMustWriteLock(sb);
4073 +               for (i = 0; i < AuBrWh_Last; i++) {
4074 +                       dput(wbr->wbr_wh[i]);
4075 +                       wbr->wbr_wh[i] = NULL;
4076 +               }
4077 +       }
4078 +
4079 +       err = test_children_busy(root, bindex, verbose);
4080 +       if (unlikely(err)) {
4081 +               if (do_wh)
4082 +                       goto out_wh;
4083 +               goto out;
4084 +       }
4085 +
4086 +       err = 0;
4087 +       if (to_free) {
4088 +               /*
4089 +                * now we confirmed the branch is deletable.
4090 +                * let's free the remaining opened dirs on the branch.
4091 +                */
4092 +               di_write_unlock(root);
4093 +               br_del_file(to_free, opened, br_id);
4094 +               di_write_lock_child(root);
4095 +       }
4096 +
4097 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4098 +       dbgaufs_xino_del(br);           /* remove one */
4099 +       au_br_do_del(sb, bindex, br);
4100 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4101 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4102 +
4103 +       if (!bindex) {
4104 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4105 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4106 +       } else
4107 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4108 +       if (au_opt_test(mnt_flags, PLINK))
4109 +               au_plink_half_refresh(sb, br_id);
4110 +
4111 +       goto out; /* success */
4112 +
4113 +out_wh:
4114 +       /* revert */
4115 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4116 +       if (rerr)
4117 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4118 +                       del->pathname, rerr);
4119 +out:
4120 +       if (to_free)
4121 +               au_farray_free(to_free, opened);
4122 +       return err;
4123 +}
4124 +
4125 +/* ---------------------------------------------------------------------- */
4126 +
4127 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4128 +{
4129 +       int err;
4130 +       aufs_bindex_t btop, bbot;
4131 +       struct aufs_ibusy ibusy;
4132 +       struct inode *inode, *h_inode;
4133 +
4134 +       err = -EPERM;
4135 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4136 +               goto out;
4137 +
4138 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4139 +       if (!err)
4140 +               /* VERIFY_WRITE */
4141 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4142 +       if (unlikely(err)) {
4143 +               err = -EFAULT;
4144 +               AuTraceErr(err);
4145 +               goto out;
4146 +       }
4147 +
4148 +       err = -EINVAL;
4149 +       si_read_lock(sb, AuLock_FLUSH);
4150 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4151 +               goto out_unlock;
4152 +
4153 +       err = 0;
4154 +       ibusy.h_ino = 0; /* invalid */
4155 +       inode = ilookup(sb, ibusy.ino);
4156 +       if (!inode
4157 +           || inode->i_ino == AUFS_ROOT_INO
4158 +           || au_is_bad_inode(inode))
4159 +               goto out_unlock;
4160 +
4161 +       ii_read_lock_child(inode);
4162 +       btop = au_ibtop(inode);
4163 +       bbot = au_ibbot(inode);
4164 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4165 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4166 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4167 +                       ibusy.h_ino = h_inode->i_ino;
4168 +       }
4169 +       ii_read_unlock(inode);
4170 +       iput(inode);
4171 +
4172 +out_unlock:
4173 +       si_read_unlock(sb);
4174 +       if (!err) {
4175 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4176 +               if (unlikely(err)) {
4177 +                       err = -EFAULT;
4178 +                       AuTraceErr(err);
4179 +               }
4180 +       }
4181 +out:
4182 +       return err;
4183 +}
4184 +
4185 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4186 +{
4187 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4188 +}
4189 +
4190 +#ifdef CONFIG_COMPAT
4191 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4192 +{
4193 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4194 +}
4195 +#endif
4196 +
4197 +/* ---------------------------------------------------------------------- */
4198 +
4199 +/*
4200 + * change a branch permission
4201 + */
4202 +
4203 +static void au_warn_ima(void)
4204 +{
4205 +#ifdef CONFIG_IMA
4206 +       /* since it doesn't support mark_files_ro() */
4207 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4208 +#endif
4209 +}
4210 +
4211 +static int do_need_sigen_inc(int a, int b)
4212 +{
4213 +       return au_br_whable(a) && !au_br_whable(b);
4214 +}
4215 +
4216 +static int need_sigen_inc(int old, int new)
4217 +{
4218 +       return do_need_sigen_inc(old, new)
4219 +               || do_need_sigen_inc(new, old);
4220 +}
4221 +
4222 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4223 +{
4224 +       int err, do_warn;
4225 +       unsigned int mnt_flags;
4226 +       unsigned long long ull, max;
4227 +       aufs_bindex_t br_id;
4228 +       unsigned char verbose, writer;
4229 +       struct file *file, *hf, **array;
4230 +       struct au_hfile *hfile;
4231 +       struct inode *h_inode;
4232 +
4233 +       mnt_flags = au_mntflags(sb);
4234 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4235 +
4236 +       array = au_farray_alloc(sb, &max);
4237 +       err = PTR_ERR(array);
4238 +       if (IS_ERR(array))
4239 +               goto out;
4240 +
4241 +       do_warn = 0;
4242 +       br_id = au_sbr_id(sb, bindex);
4243 +       for (ull = 0; ull < max; ull++) {
4244 +               file = array[ull];
4245 +               if (unlikely(!file))
4246 +                       break;
4247 +
4248 +               /* AuDbg("%pD\n", file); */
4249 +               fi_read_lock(file);
4250 +               if (unlikely(au_test_mmapped(file))) {
4251 +                       err = -EBUSY;
4252 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4253 +                       AuDbgFile(file);
4254 +                       FiMustNoWaiters(file);
4255 +                       fi_read_unlock(file);
4256 +                       goto out_array;
4257 +               }
4258 +
4259 +               hfile = &au_fi(file)->fi_htop;
4260 +               hf = hfile->hf_file;
4261 +               if (!d_is_reg(file->f_path.dentry)
4262 +                   || !(file->f_mode & FMODE_WRITE)
4263 +                   || hfile->hf_br->br_id != br_id
4264 +                   || !(hf->f_mode & FMODE_WRITE))
4265 +                       array[ull] = NULL;
4266 +               else {
4267 +                       do_warn = 1;
4268 +                       get_file(file);
4269 +               }
4270 +
4271 +               FiMustNoWaiters(file);
4272 +               fi_read_unlock(file);
4273 +               fput(file);
4274 +       }
4275 +
4276 +       err = 0;
4277 +       if (do_warn)
4278 +               au_warn_ima();
4279 +
4280 +       for (ull = 0; ull < max; ull++) {
4281 +               file = array[ull];
4282 +               if (!file)
4283 +                       continue;
4284 +
4285 +               /* todo: already flushed? */
4286 +               /*
4287 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4288 +                * approach which resets f_mode and calls mnt_drop_write() and
4289 +                * file_release_write() for each file, because the branch
4290 +                * attribute in aufs world is totally different from the native
4291 +                * fs rw/ro mode.
4292 +               */
4293 +               /* fi_read_lock(file); */
4294 +               hfile = &au_fi(file)->fi_htop;
4295 +               hf = hfile->hf_file;
4296 +               /* fi_read_unlock(file); */
4297 +               spin_lock(&hf->f_lock);
4298 +               writer = !!(hf->f_mode & FMODE_WRITER);
4299 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4300 +               spin_unlock(&hf->f_lock);
4301 +               if (writer) {
4302 +                       h_inode = file_inode(hf);
4303 +                       put_write_access(h_inode);
4304 +                       __mnt_drop_write(hf->f_path.mnt);
4305 +                       if ((hf->f_mode & (FMODE_READ | FMODE_WRITE))
4306 +                           == FMODE_READ)
4307 +                               i_readcount_inc(h_inode);
4308 +               }
4309 +       }
4310 +
4311 +out_array:
4312 +       au_farray_free(array, max);
4313 +out:
4314 +       AuTraceErr(err);
4315 +       return err;
4316 +}
4317 +
4318 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4319 +             int *do_refresh)
4320 +{
4321 +       int err, rerr;
4322 +       aufs_bindex_t bindex;
4323 +       struct dentry *root;
4324 +       struct au_branch *br;
4325 +       struct au_br_fhsm *bf;
4326 +
4327 +       root = sb->s_root;
4328 +       bindex = au_find_dbindex(root, mod->h_root);
4329 +       if (bindex < 0) {
4330 +               if (remount)
4331 +                       return 0; /* success */
4332 +               err = -ENOENT;
4333 +               pr_err("%s no such branch\n", mod->path);
4334 +               goto out;
4335 +       }
4336 +       AuDbg("bindex b%d\n", bindex);
4337 +
4338 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4339 +       if (unlikely(err))
4340 +               goto out;
4341 +
4342 +       br = au_sbr(sb, bindex);
4343 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4344 +       if (br->br_perm == mod->perm)
4345 +               return 0; /* success */
4346 +
4347 +       /* pre-allocate for non-fhsm --> fhsm */
4348 +       bf = NULL;
4349 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4350 +               err = au_fhsm_br_alloc(br);
4351 +               if (unlikely(err))
4352 +                       goto out;
4353 +               bf = br->br_fhsm;
4354 +               br->br_fhsm = NULL;
4355 +       }
4356 +
4357 +       if (au_br_writable(br->br_perm)) {
4358 +               /* remove whiteout base */
4359 +               err = au_br_init_wh(sb, br, mod->perm);
4360 +               if (unlikely(err))
4361 +                       goto out_bf;
4362 +
4363 +               if (!au_br_writable(mod->perm)) {
4364 +                       /* rw --> ro, file might be mmapped */
4365 +                       DiMustNoWaiters(root);
4366 +                       IiMustNoWaiters(d_inode(root));
4367 +                       di_write_unlock(root);
4368 +                       err = au_br_mod_files_ro(sb, bindex);
4369 +                       /* aufs_write_lock() calls ..._child() */
4370 +                       di_write_lock_child(root);
4371 +
4372 +                       if (unlikely(err)) {
4373 +                               rerr = -ENOMEM;
4374 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4375 +                                                    GFP_NOFS);
4376 +                               if (br->br_wbr)
4377 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4378 +                               if (unlikely(rerr)) {
4379 +                                       AuIOErr("nested error %d (%d)\n",
4380 +                                               rerr, err);
4381 +                                       br->br_perm = mod->perm;
4382 +                               }
4383 +                       }
4384 +               }
4385 +       } else if (au_br_writable(mod->perm)) {
4386 +               /* ro --> rw */
4387 +               err = -ENOMEM;
4388 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4389 +               if (br->br_wbr) {
4390 +                       err = au_wbr_init(br, sb, mod->perm);
4391 +                       if (unlikely(err)) {
4392 +                               au_kfree_rcu(br->br_wbr);
4393 +                               br->br_wbr = NULL;
4394 +                       }
4395 +               }
4396 +       }
4397 +       if (unlikely(err))
4398 +               goto out_bf;
4399 +
4400 +       if (au_br_fhsm(br->br_perm)) {
4401 +               if (!au_br_fhsm(mod->perm)) {
4402 +                       /* fhsm --> non-fhsm */
4403 +                       au_br_fhsm_fin(br->br_fhsm);
4404 +                       au_kfree_rcu(br->br_fhsm);
4405 +                       br->br_fhsm = NULL;
4406 +               }
4407 +       } else if (au_br_fhsm(mod->perm))
4408 +               /* non-fhsm --> fhsm */
4409 +               br->br_fhsm = bf;
4410 +
4411 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4412 +       br->br_perm = mod->perm;
4413 +       goto out; /* success */
4414 +
4415 +out_bf:
4416 +       au_kfree_try_rcu(bf);
4417 +out:
4418 +       AuTraceErr(err);
4419 +       return err;
4420 +}
4421 +
4422 +/* ---------------------------------------------------------------------- */
4423 +
4424 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4425 +{
4426 +       int err;
4427 +       struct kstatfs kstfs;
4428 +
4429 +       err = vfs_statfs(&br->br_path, &kstfs);
4430 +       if (!err) {
4431 +               stfs->f_blocks = kstfs.f_blocks;
4432 +               stfs->f_bavail = kstfs.f_bavail;
4433 +               stfs->f_files = kstfs.f_files;
4434 +               stfs->f_ffree = kstfs.f_ffree;
4435 +       }
4436 +
4437 +       return err;
4438 +}
4439 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4440 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4441 +++ linux/fs/aufs/branch.h      2020-01-27 10:57:18.165538015 +0100
4442 @@ -0,0 +1,366 @@
4443 +/* SPDX-License-Identifier: GPL-2.0 */
4444 +/*
4445 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4446 + *
4447 + * This program, aufs is free software; you can redistribute it and/or modify
4448 + * it under the terms of the GNU General Public License as published by
4449 + * the Free Software Foundation; either version 2 of the License, or
4450 + * (at your option) any later version.
4451 + *
4452 + * This program is distributed in the hope that it will be useful,
4453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4455 + * GNU General Public License for more details.
4456 + *
4457 + * You should have received a copy of the GNU General Public License
4458 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4459 + */
4460 +
4461 +/*
4462 + * branch filesystems and xino for them
4463 + */
4464 +
4465 +#ifndef __AUFS_BRANCH_H__
4466 +#define __AUFS_BRANCH_H__
4467 +
4468 +#ifdef __KERNEL__
4469 +
4470 +#include <linux/mount.h>
4471 +#include "dirren.h"
4472 +#include "dynop.h"
4473 +#include "lcnt.h"
4474 +#include "rwsem.h"
4475 +#include "super.h"
4476 +
4477 +/* ---------------------------------------------------------------------- */
4478 +
4479 +/* a xino file */
4480 +struct au_xino {
4481 +       struct file             **xi_file;
4482 +       unsigned int            xi_nfile;
4483 +
4484 +       struct {
4485 +               spinlock_t              spin;
4486 +               ino_t                   *array;
4487 +               int                     total;
4488 +               /* reserved for future use */
4489 +               /* unsigned long        *bitmap; */
4490 +               wait_queue_head_t       wqh;
4491 +       } xi_nondir;
4492 +
4493 +       struct mutex            xi_mtx; /* protects xi_file array */
4494 +       struct hlist_bl_head    xi_writing;
4495 +
4496 +       atomic_t                xi_truncating;
4497 +
4498 +       struct kref             xi_kref;
4499 +};
4500 +
4501 +/* File-based Hierarchical Storage Management */
4502 +struct au_br_fhsm {
4503 +#ifdef CONFIG_AUFS_FHSM
4504 +       struct mutex            bf_lock;
4505 +       unsigned long           bf_jiffy;
4506 +       struct aufs_stfs        bf_stfs;
4507 +       int                     bf_readable;
4508 +#endif
4509 +};
4510 +
4511 +/* members for writable branch only */
4512 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4513 +struct au_wbr {
4514 +       struct au_rwsem         wbr_wh_rwsem;
4515 +       struct dentry           *wbr_wh[AuBrWh_Last];
4516 +       atomic_t                wbr_wh_running;
4517 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4518 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4519 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4520 +
4521 +       /* mfs mode */
4522 +       unsigned long long      wbr_bytes;
4523 +};
4524 +
4525 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4526 +#define AuBrDynOp (AuDyLast * 4)
4527 +
4528 +#ifdef CONFIG_AUFS_HFSNOTIFY
4529 +/* support for asynchronous destruction */
4530 +struct au_br_hfsnotify {
4531 +       struct fsnotify_group   *hfsn_group;
4532 +};
4533 +#endif
4534 +
4535 +/* sysfs entries */
4536 +struct au_brsysfs {
4537 +       char                    name[16];
4538 +       struct attribute        attr;
4539 +};
4540 +
4541 +enum {
4542 +       AuBrSysfs_BR,
4543 +       AuBrSysfs_BRID,
4544 +       AuBrSysfs_Last
4545 +};
4546 +
4547 +/* protected by superblock rwsem */
4548 +struct au_branch {
4549 +       struct au_xino          *br_xino;
4550 +
4551 +       aufs_bindex_t           br_id;
4552 +
4553 +       int                     br_perm;
4554 +       struct path             br_path;
4555 +       spinlock_t              br_dykey_lock;
4556 +       struct au_dykey         *br_dykey[AuBrDynOp];
4557 +       au_lcnt_t               br_nfiles;      /* opened files */
4558 +       au_lcnt_t               br_count;       /* in-use for other */
4559 +
4560 +       struct au_wbr           *br_wbr;
4561 +       struct au_br_fhsm       *br_fhsm;
4562 +
4563 +#ifdef CONFIG_AUFS_HFSNOTIFY
4564 +       struct au_br_hfsnotify  *br_hfsn;
4565 +#endif
4566 +
4567 +#ifdef CONFIG_SYSFS
4568 +       /* entries under sysfs per mount-point */
4569 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4570 +#endif
4571 +
4572 +#ifdef CONFIG_DEBUG_FS
4573 +       struct dentry            *br_dbgaufs; /* xino */
4574 +#endif
4575 +
4576 +       struct au_dr_br         br_dirren;
4577 +};
4578 +
4579 +/* ---------------------------------------------------------------------- */
4580 +
4581 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4582 +{
4583 +       return br->br_path.mnt;
4584 +}
4585 +
4586 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4587 +{
4588 +       return br->br_path.dentry;
4589 +}
4590 +
4591 +static inline struct super_block *au_br_sb(struct au_branch *br)
4592 +{
4593 +       return au_br_mnt(br)->mnt_sb;
4594 +}
4595 +
4596 +static inline int au_br_rdonly(struct au_branch *br)
4597 +{
4598 +       return (sb_rdonly(au_br_sb(br))
4599 +               || !au_br_writable(br->br_perm))
4600 +               ? -EROFS : 0;
4601 +}
4602 +
4603 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4604 +{
4605 +#ifdef CONFIG_AUFS_HNOTIFY
4606 +       return !(brperm & AuBrPerm_RR);
4607 +#else
4608 +       return 0;
4609 +#endif
4610 +}
4611 +
4612 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4613 +{
4614 +       int err, exec_flag;
4615 +
4616 +       err = 0;
4617 +       exec_flag = oflag & __FMODE_EXEC;
4618 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4619 +               err = -EACCES;
4620 +
4621 +       return err;
4622 +}
4623 +
4624 +static inline void au_xino_get(struct au_branch *br)
4625 +{
4626 +       struct au_xino *xi;
4627 +
4628 +       xi = br->br_xino;
4629 +       if (xi)
4630 +               kref_get(&xi->xi_kref);
4631 +}
4632 +
4633 +static inline int au_xino_count(struct au_branch *br)
4634 +{
4635 +       int v;
4636 +       struct au_xino *xi;
4637 +
4638 +       v = 0;
4639 +       xi = br->br_xino;
4640 +       if (xi)
4641 +               v = kref_read(&xi->xi_kref);
4642 +
4643 +       return v;
4644 +}
4645 +
4646 +/* ---------------------------------------------------------------------- */
4647 +
4648 +/* branch.c */
4649 +struct au_sbinfo;
4650 +void au_br_free(struct au_sbinfo *sinfo);
4651 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4652 +struct au_opt_add;
4653 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4654 +struct au_opt_del;
4655 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4656 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4657 +#ifdef CONFIG_COMPAT
4658 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4659 +#endif
4660 +struct au_opt_mod;
4661 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4662 +             int *do_refresh);
4663 +struct aufs_stfs;
4664 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4665 +
4666 +/* xino.c */
4667 +static const loff_t au_loff_max = LLONG_MAX;
4668 +
4669 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4670 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4671 +                           int wbrtop);
4672 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4673 +                            struct file *copy_src);
4674 +struct au_xi_new {
4675 +       struct au_xino *xi;     /* switch between xino and xigen */
4676 +       int idx;
4677 +       struct path *base;
4678 +       struct file *copy_src;
4679 +};
4680 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4681 +
4682 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4683 +                ino_t *ino);
4684 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4685 +                 ino_t ino);
4686 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4687 +                  loff_t *pos);
4688 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4689 +                   size_t size, loff_t *pos);
4690 +
4691 +int au_xib_trunc(struct super_block *sb);
4692 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4693 +
4694 +struct au_xino *au_xino_alloc(unsigned int nfile);
4695 +int au_xino_put(struct au_branch *br);
4696 +struct file *au_xino_file1(struct au_xino *xi);
4697 +
4698 +struct au_opt_xino;
4699 +void au_xino_clr(struct super_block *sb);
4700 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4701 +struct file *au_xino_def(struct super_block *sb);
4702 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4703 +                   struct path *base);
4704 +
4705 +ino_t au_xino_new_ino(struct super_block *sb);
4706 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4707 +
4708 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4709 +                      ino_t h_ino, int idx);
4710 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4711 +                     int *idx);
4712 +
4713 +int au_xino_path(struct seq_file *seq, struct file *file);
4714 +
4715 +/* ---------------------------------------------------------------------- */
4716 +
4717 +/* @idx is signed to accept -1 meaning the first file */
4718 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4719 +{
4720 +       struct file *file;
4721 +
4722 +       file = NULL;
4723 +       if (!xi)
4724 +               goto out;
4725 +
4726 +       if (idx >= 0) {
4727 +               if (idx < xi->xi_nfile)
4728 +                       file = xi->xi_file[idx];
4729 +       } else
4730 +               file = au_xino_file1(xi);
4731 +
4732 +out:
4733 +       return file;
4734 +}
4735 +
4736 +/* ---------------------------------------------------------------------- */
4737 +
4738 +/* Superblock to branch */
4739 +static inline
4740 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4741 +{
4742 +       return au_sbr(sb, bindex)->br_id;
4743 +}
4744 +
4745 +static inline
4746 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4747 +{
4748 +       return au_br_mnt(au_sbr(sb, bindex));
4749 +}
4750 +
4751 +static inline
4752 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4753 +{
4754 +       return au_br_sb(au_sbr(sb, bindex));
4755 +}
4756 +
4757 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4758 +{
4759 +       return au_sbr(sb, bindex)->br_perm;
4760 +}
4761 +
4762 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4763 +{
4764 +       return au_br_whable(au_sbr_perm(sb, bindex));
4765 +}
4766 +
4767 +/* ---------------------------------------------------------------------- */
4768 +
4769 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4770 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4771 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4772 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4773 +/*
4774 +#define wbr_wh_read_trylock_nested(wbr) \
4775 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4776 +#define wbr_wh_write_trylock_nested(wbr) \
4777 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4778 +*/
4779 +
4780 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4781 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4782 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4783 +
4784 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4785 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4786 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4787 +
4788 +/* ---------------------------------------------------------------------- */
4789 +
4790 +#ifdef CONFIG_AUFS_FHSM
4791 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4792 +{
4793 +       mutex_init(&brfhsm->bf_lock);
4794 +       brfhsm->bf_jiffy = 0;
4795 +       brfhsm->bf_readable = 0;
4796 +}
4797 +
4798 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4799 +{
4800 +       mutex_destroy(&brfhsm->bf_lock);
4801 +}
4802 +#else
4803 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4804 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4805 +#endif
4806 +
4807 +#endif /* __KERNEL__ */
4808 +#endif /* __AUFS_BRANCH_H__ */
4809 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4810 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4811 +++ linux/fs/aufs/conf.mk       2019-07-11 15:42:14.462237786 +0200
4812 @@ -0,0 +1,40 @@
4813 +# SPDX-License-Identifier: GPL-2.0
4814 +
4815 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4816 +
4817 +define AuConf
4818 +ifdef ${1}
4819 +AuConfStr += ${1}=${${1}}
4820 +endif
4821 +endef
4822 +
4823 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4824 +       SBILIST \
4825 +       HNOTIFY HFSNOTIFY \
4826 +       EXPORT INO_T_64 \
4827 +       XATTR \
4828 +       FHSM \
4829 +       RDU \
4830 +       DIRREN \
4831 +       SHWH \
4832 +       BR_RAMFS \
4833 +       BR_FUSE POLL \
4834 +       BR_HFSPLUS \
4835 +       BDEV_LOOP \
4836 +       DEBUG MAGIC_SYSRQ
4837 +$(foreach i, ${AuConfAll}, \
4838 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4839 +
4840 +AuConfName = ${obj}/conf.str
4841 +${AuConfName}.tmp: FORCE
4842 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4843 +${AuConfName}: ${AuConfName}.tmp
4844 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4845 +       echo '  GEN    ' $@; \
4846 +       cp -p $< $@; \
4847 +       }
4848 +FORCE:
4849 +clean-files += ${AuConfName} ${AuConfName}.tmp
4850 +${obj}/sysfs.o: ${AuConfName}
4851 +
4852 +-include ${srctree}/${src}/conf_priv.mk
4853 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4854 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4855 +++ linux/fs/aufs/cpup.c        2020-01-27 10:57:18.165538015 +0100
4856 @@ -0,0 +1,1458 @@
4857 +// SPDX-License-Identifier: GPL-2.0
4858 +/*
4859 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4860 + *
4861 + * This program, aufs is free software; you can redistribute it and/or modify
4862 + * it under the terms of the GNU General Public License as published by
4863 + * the Free Software Foundation; either version 2 of the License, or
4864 + * (at your option) any later version.
4865 + *
4866 + * This program is distributed in the hope that it will be useful,
4867 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4868 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4869 + * GNU General Public License for more details.
4870 + *
4871 + * You should have received a copy of the GNU General Public License
4872 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4873 + */
4874 +
4875 +/*
4876 + * copy-up functions, see wbr_policy.c for copy-down
4877 + */
4878 +
4879 +#include <linux/fs_stack.h>
4880 +#include <linux/mm.h>
4881 +#include <linux/task_work.h>
4882 +#include "aufs.h"
4883 +
4884 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4885 +{
4886 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4887 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4888 +
4889 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4890 +
4891 +       dst->i_flags |= iflags & ~mask;
4892 +       if (au_test_fs_notime(dst->i_sb))
4893 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4894 +}
4895 +
4896 +void au_cpup_attr_timesizes(struct inode *inode)
4897 +{
4898 +       struct inode *h_inode;
4899 +
4900 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4901 +       fsstack_copy_attr_times(inode, h_inode);
4902 +       fsstack_copy_inode_size(inode, h_inode);
4903 +}
4904 +
4905 +void au_cpup_attr_nlink(struct inode *inode, int force)
4906 +{
4907 +       struct inode *h_inode;
4908 +       struct super_block *sb;
4909 +       aufs_bindex_t bindex, bbot;
4910 +
4911 +       sb = inode->i_sb;
4912 +       bindex = au_ibtop(inode);
4913 +       h_inode = au_h_iptr(inode, bindex);
4914 +       if (!force
4915 +           && !S_ISDIR(h_inode->i_mode)
4916 +           && au_opt_test(au_mntflags(sb), PLINK)
4917 +           && au_plink_test(inode))
4918 +               return;
4919 +
4920 +       /*
4921 +        * 0 can happen in revalidating.
4922 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4923 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4924 +        * case.
4925 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4926 +        *       the incorrect link count.
4927 +        */
4928 +       set_nlink(inode, h_inode->i_nlink);
4929 +
4930 +       /*
4931 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4932 +        * it may includes whplink directory.
4933 +        */
4934 +       if (S_ISDIR(h_inode->i_mode)) {
4935 +               bbot = au_ibbot(inode);
4936 +               for (bindex++; bindex <= bbot; bindex++) {
4937 +                       h_inode = au_h_iptr(inode, bindex);
4938 +                       if (h_inode)
4939 +                               au_add_nlink(inode, h_inode);
4940 +               }
4941 +       }
4942 +}
4943 +
4944 +void au_cpup_attr_changeable(struct inode *inode)
4945 +{
4946 +       struct inode *h_inode;
4947 +
4948 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4949 +       inode->i_mode = h_inode->i_mode;
4950 +       inode->i_uid = h_inode->i_uid;
4951 +       inode->i_gid = h_inode->i_gid;
4952 +       au_cpup_attr_timesizes(inode);
4953 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4954 +}
4955 +
4956 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4957 +{
4958 +       struct au_iinfo *iinfo = au_ii(inode);
4959 +
4960 +       IiMustWriteLock(inode);
4961 +
4962 +       iinfo->ii_higen = h_inode->i_generation;
4963 +       iinfo->ii_hsb1 = h_inode->i_sb;
4964 +}
4965 +
4966 +void au_cpup_attr_all(struct inode *inode, int force)
4967 +{
4968 +       struct inode *h_inode;
4969 +
4970 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4971 +       au_cpup_attr_changeable(inode);
4972 +       if (inode->i_nlink > 0)
4973 +               au_cpup_attr_nlink(inode, force);
4974 +       inode->i_rdev = h_inode->i_rdev;
4975 +       inode->i_blkbits = h_inode->i_blkbits;
4976 +       au_cpup_igen(inode, h_inode);
4977 +}
4978 +
4979 +/* ---------------------------------------------------------------------- */
4980 +
4981 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4982 +
4983 +/* keep the timestamps of the parent dir when cpup */
4984 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4985 +                   struct path *h_path)
4986 +{
4987 +       struct inode *h_inode;
4988 +
4989 +       dt->dt_dentry = dentry;
4990 +       dt->dt_h_path = *h_path;
4991 +       h_inode = d_inode(h_path->dentry);
4992 +       dt->dt_atime = h_inode->i_atime;
4993 +       dt->dt_mtime = h_inode->i_mtime;
4994 +       /* smp_mb(); */
4995 +}
4996 +
4997 +void au_dtime_revert(struct au_dtime *dt)
4998 +{
4999 +       struct iattr attr;
5000 +       int err;
5001 +
5002 +       attr.ia_atime = dt->dt_atime;
5003 +       attr.ia_mtime = dt->dt_mtime;
5004 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
5005 +               | ATTR_ATIME | ATTR_ATIME_SET;
5006 +
5007 +       /* no delegation since this is a directory */
5008 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
5009 +       if (unlikely(err))
5010 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
5011 +}
5012 +
5013 +/* ---------------------------------------------------------------------- */
5014 +
5015 +/* internal use only */
5016 +struct au_cpup_reg_attr {
5017 +       int             valid;
5018 +       struct kstat    st;
5019 +       unsigned int    iflags; /* inode->i_flags */
5020 +};
5021 +
5022 +static noinline_for_stack
5023 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
5024 +              struct au_cpup_reg_attr *h_src_attr)
5025 +{
5026 +       int err, sbits, icex;
5027 +       unsigned int mnt_flags;
5028 +       unsigned char verbose;
5029 +       struct iattr ia;
5030 +       struct path h_path;
5031 +       struct inode *h_isrc, *h_idst;
5032 +       struct kstat *h_st;
5033 +       struct au_branch *br;
5034 +
5035 +       h_path.dentry = au_h_dptr(dst, bindex);
5036 +       h_idst = d_inode(h_path.dentry);
5037 +       br = au_sbr(dst->d_sb, bindex);
5038 +       h_path.mnt = au_br_mnt(br);
5039 +       h_isrc = d_inode(h_src);
5040 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
5041 +               | ATTR_ATIME | ATTR_MTIME
5042 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
5043 +       if (h_src_attr && h_src_attr->valid) {
5044 +               h_st = &h_src_attr->st;
5045 +               ia.ia_uid = h_st->uid;
5046 +               ia.ia_gid = h_st->gid;
5047 +               ia.ia_atime = h_st->atime;
5048 +               ia.ia_mtime = h_st->mtime;
5049 +               if (h_idst->i_mode != h_st->mode
5050 +                   && !S_ISLNK(h_idst->i_mode)) {
5051 +                       ia.ia_valid |= ATTR_MODE;
5052 +                       ia.ia_mode = h_st->mode;
5053 +               }
5054 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
5055 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
5056 +       } else {
5057 +               ia.ia_uid = h_isrc->i_uid;
5058 +               ia.ia_gid = h_isrc->i_gid;
5059 +               ia.ia_atime = h_isrc->i_atime;
5060 +               ia.ia_mtime = h_isrc->i_mtime;
5061 +               if (h_idst->i_mode != h_isrc->i_mode
5062 +                   && !S_ISLNK(h_idst->i_mode)) {
5063 +                       ia.ia_valid |= ATTR_MODE;
5064 +                       ia.ia_mode = h_isrc->i_mode;
5065 +               }
5066 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5067 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5068 +       }
5069 +       /* no delegation since it is just created */
5070 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5071 +
5072 +       /* is this nfs only? */
5073 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5074 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5075 +               ia.ia_mode = h_isrc->i_mode;
5076 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5077 +       }
5078 +
5079 +       icex = br->br_perm & AuBrAttr_ICEX;
5080 +       if (!err) {
5081 +               mnt_flags = au_mntflags(dst->d_sb);
5082 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5083 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5084 +       }
5085 +
5086 +       return err;
5087 +}
5088 +
5089 +/* ---------------------------------------------------------------------- */
5090 +
5091 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5092 +                          char *buf, unsigned long blksize)
5093 +{
5094 +       int err;
5095 +       size_t sz, rbytes, wbytes;
5096 +       unsigned char all_zero;
5097 +       char *p, *zp;
5098 +       struct inode *h_inode;
5099 +       /* reduce stack usage */
5100 +       struct iattr *ia;
5101 +
5102 +       zp = page_address(ZERO_PAGE(0));
5103 +       if (unlikely(!zp))
5104 +               return -ENOMEM; /* possible? */
5105 +
5106 +       err = 0;
5107 +       all_zero = 0;
5108 +       while (len) {
5109 +               AuDbg("len %lld\n", len);
5110 +               sz = blksize;
5111 +               if (len < blksize)
5112 +                       sz = len;
5113 +
5114 +               rbytes = 0;
5115 +               /* todo: signal_pending? */
5116 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5117 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5118 +                       err = rbytes;
5119 +               }
5120 +               if (unlikely(err < 0))
5121 +                       break;
5122 +
5123 +               all_zero = 0;
5124 +               if (len >= rbytes && rbytes == blksize)
5125 +                       all_zero = !memcmp(buf, zp, rbytes);
5126 +               if (!all_zero) {
5127 +                       wbytes = rbytes;
5128 +                       p = buf;
5129 +                       while (wbytes) {
5130 +                               size_t b;
5131 +
5132 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5133 +                               err = b;
5134 +                               /* todo: signal_pending? */
5135 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5136 +                                       continue;
5137 +                               if (unlikely(err < 0))
5138 +                                       break;
5139 +                               wbytes -= b;
5140 +                               p += b;
5141 +                       }
5142 +                       if (unlikely(err < 0))
5143 +                               break;
5144 +               } else {
5145 +                       loff_t res;
5146 +
5147 +                       AuLabel(hole);
5148 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5149 +                       err = res;
5150 +                       if (unlikely(res < 0))
5151 +                               break;
5152 +               }
5153 +               len -= rbytes;
5154 +               err = 0;
5155 +       }
5156 +
5157 +       /* the last block may be a hole */
5158 +       if (!err && all_zero) {
5159 +               AuLabel(last hole);
5160 +
5161 +               err = 1;
5162 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5163 +                       /* nfs requires this step to make last hole */
5164 +                       /* is this only nfs? */
5165 +                       do {
5166 +                               /* todo: signal_pending? */
5167 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5168 +                       } while (err == -EAGAIN || err == -EINTR);
5169 +                       if (err == 1)
5170 +                               dst->f_pos--;
5171 +               }
5172 +
5173 +               if (err == 1) {
5174 +                       ia = (void *)buf;
5175 +                       ia->ia_size = dst->f_pos;
5176 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5177 +                       ia->ia_file = dst;
5178 +                       h_inode = file_inode(dst);
5179 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5180 +                       /* no delegation since it is just created */
5181 +                       err = vfsub_notify_change(&dst->f_path, ia,
5182 +                                                 /*delegated*/NULL);
5183 +                       inode_unlock(h_inode);
5184 +               }
5185 +       }
5186 +
5187 +       return err;
5188 +}
5189 +
5190 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5191 +{
5192 +       int err;
5193 +       unsigned long blksize;
5194 +       unsigned char do_kfree;
5195 +       char *buf;
5196 +       struct super_block *h_sb;
5197 +
5198 +       err = -ENOMEM;
5199 +       h_sb = file_inode(dst)->i_sb;
5200 +       blksize = h_sb->s_blocksize;
5201 +       if (!blksize || PAGE_SIZE < blksize)
5202 +               blksize = PAGE_SIZE;
5203 +       AuDbg("blksize %lu\n", blksize);
5204 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5205 +       if (do_kfree)
5206 +               buf = kmalloc(blksize, GFP_NOFS);
5207 +       else
5208 +               buf = (void *)__get_free_page(GFP_NOFS);
5209 +       if (unlikely(!buf))
5210 +               goto out;
5211 +
5212 +       if (len > (1 << 22))
5213 +               AuDbg("copying a large file %lld\n", (long long)len);
5214 +
5215 +       src->f_pos = 0;
5216 +       dst->f_pos = 0;
5217 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5218 +       if (do_kfree) {
5219 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5220 +               au_kfree_do_rcu(buf);
5221 +       } else
5222 +               free_page((unsigned long)buf);
5223 +
5224 +out:
5225 +       return err;
5226 +}
5227 +
5228 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5229 +{
5230 +       int err;
5231 +       struct super_block *h_src_sb;
5232 +       struct inode *h_src_inode;
5233 +
5234 +       h_src_inode = file_inode(src);
5235 +       h_src_sb = h_src_inode->i_sb;
5236 +
5237 +       /* XFS acquires inode_lock */
5238 +       if (!au_test_xfs(h_src_sb))
5239 +               err = au_copy_file(dst, src, len);
5240 +       else {
5241 +               inode_unlock_shared(h_src_inode);
5242 +               err = au_copy_file(dst, src, len);
5243 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5244 +       }
5245 +
5246 +       return err;
5247 +}
5248 +
5249 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5250 +{
5251 +       int err;
5252 +       loff_t lo;
5253 +       struct super_block *h_src_sb;
5254 +       struct inode *h_src_inode;
5255 +
5256 +       h_src_inode = file_inode(src);
5257 +       h_src_sb = h_src_inode->i_sb;
5258 +       if (h_src_sb != file_inode(dst)->i_sb
5259 +           || !dst->f_op->remap_file_range) {
5260 +               err = au_do_copy(dst, src, len);
5261 +               goto out;
5262 +       }
5263 +
5264 +       if (!au_test_nfs(h_src_sb)) {
5265 +               inode_unlock_shared(h_src_inode);
5266 +               lo = vfsub_clone_file_range(src, dst, len);
5267 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5268 +       } else
5269 +               lo = vfsub_clone_file_range(src, dst, len);
5270 +       if (lo == len) {
5271 +               err = 0;
5272 +               goto out; /* success */
5273 +       } else if (lo >= 0)
5274 +               /* todo: possible? */
5275 +               /* paritially succeeded */
5276 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5277 +       else if (lo != -EOPNOTSUPP) {
5278 +               /* older XFS has a condition in cloning */
5279 +               err = lo;
5280 +               goto out;
5281 +       }
5282 +
5283 +       /* the backend fs on NFS may not support cloning */
5284 +       err = au_do_copy(dst, src, len);
5285 +
5286 +out:
5287 +       AuTraceErr(err);
5288 +       return err;
5289 +}
5290 +
5291 +/*
5292 + * to support a sparse file which is opened with O_APPEND,
5293 + * we need to close the file.
5294 + */
5295 +static int au_cp_regular(struct au_cp_generic *cpg)
5296 +{
5297 +       int err, i;
5298 +       enum { SRC, DST };
5299 +       struct {
5300 +               aufs_bindex_t bindex;
5301 +               unsigned int flags;
5302 +               struct dentry *dentry;
5303 +               int force_wr;
5304 +               struct file *file;
5305 +       } *f, file[] = {
5306 +               {
5307 +                       .bindex = cpg->bsrc,
5308 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5309 +               },
5310 +               {
5311 +                       .bindex = cpg->bdst,
5312 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5313 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5314 +               }
5315 +       };
5316 +       struct au_branch *br;
5317 +       struct super_block *sb, *h_src_sb;
5318 +       struct inode *h_src_inode;
5319 +       struct task_struct *tsk = current;
5320 +
5321 +       /* bsrc branch can be ro/rw. */
5322 +       sb = cpg->dentry->d_sb;
5323 +       f = file;
5324 +       for (i = 0; i < 2; i++, f++) {
5325 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5326 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5327 +                                   /*file*/NULL, f->force_wr);
5328 +               if (IS_ERR(f->file)) {
5329 +                       err = PTR_ERR(f->file);
5330 +                       if (i == SRC)
5331 +                               goto out;
5332 +                       else
5333 +                               goto out_src;
5334 +               }
5335 +       }
5336 +
5337 +       /* try stopping to update while we copyup */
5338 +       h_src_inode = d_inode(file[SRC].dentry);
5339 +       h_src_sb = h_src_inode->i_sb;
5340 +       if (!au_test_nfs(h_src_sb))
5341 +               IMustLock(h_src_inode);
5342 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5343 +
5344 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5345 +       if (tsk->flags & PF_KTHREAD)
5346 +               __fput_sync(file[DST].file);
5347 +       else {
5348 +               /* it happened actually */
5349 +               fput(file[DST].file);
5350 +               /*
5351 +                * too bad.
5352 +                * we have to call both since we don't know which place the file
5353 +                * was added to.
5354 +                */
5355 +               task_work_run();
5356 +               flush_delayed_fput();
5357 +       }
5358 +       br = au_sbr(sb, file[DST].bindex);
5359 +       au_lcnt_dec(&br->br_nfiles);
5360 +
5361 +out_src:
5362 +       fput(file[SRC].file);
5363 +       br = au_sbr(sb, file[SRC].bindex);
5364 +       au_lcnt_dec(&br->br_nfiles);
5365 +out:
5366 +       return err;
5367 +}
5368 +
5369 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5370 +                             struct au_cpup_reg_attr *h_src_attr)
5371 +{
5372 +       int err, rerr;
5373 +       loff_t l;
5374 +       struct path h_path;
5375 +       struct inode *h_src_inode, *h_dst_inode;
5376 +
5377 +       err = 0;
5378 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5379 +       l = i_size_read(h_src_inode);
5380 +       if (cpg->len == -1 || l < cpg->len)
5381 +               cpg->len = l;
5382 +       if (cpg->len) {
5383 +               /* try stopping to update while we are referencing */
5384 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5385 +               au_pin_hdir_unlock(cpg->pin);
5386 +
5387 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5388 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5389 +               h_src_attr->iflags = h_src_inode->i_flags;
5390 +               if (!au_test_nfs(h_src_inode->i_sb))
5391 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5392 +               else {
5393 +                       inode_unlock_shared(h_src_inode);
5394 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5395 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5396 +               }
5397 +               if (unlikely(err)) {
5398 +                       inode_unlock_shared(h_src_inode);
5399 +                       goto out;
5400 +               }
5401 +               h_src_attr->valid = 1;
5402 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5403 +                       err = au_cp_regular(cpg);
5404 +                       inode_unlock_shared(h_src_inode);
5405 +               } else {
5406 +                       inode_unlock_shared(h_src_inode);
5407 +                       err = au_cp_regular(cpg);
5408 +               }
5409 +               rerr = au_pin_hdir_relock(cpg->pin);
5410 +               if (!err && rerr)
5411 +                       err = rerr;
5412 +       }
5413 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5414 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5415 +               h_dst_inode = d_inode(h_path.dentry);
5416 +               spin_lock(&h_dst_inode->i_lock);
5417 +               h_dst_inode->i_state |= I_LINKABLE;
5418 +               spin_unlock(&h_dst_inode->i_lock);
5419 +       }
5420 +
5421 +out:
5422 +       return err;
5423 +}
5424 +
5425 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5426 +                             struct inode *h_dir)
5427 +{
5428 +       int err, symlen;
5429 +       mm_segment_t old_fs;
5430 +       union {
5431 +               char *k;
5432 +               char __user *u;
5433 +       } sym;
5434 +
5435 +       err = -ENOMEM;
5436 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5437 +       if (unlikely(!sym.k))
5438 +               goto out;
5439 +
5440 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5441 +       old_fs = get_fs();
5442 +       set_fs(KERNEL_DS);
5443 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5444 +       err = symlen;
5445 +       set_fs(old_fs);
5446 +
5447 +       if (symlen > 0) {
5448 +               sym.k[symlen] = 0;
5449 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5450 +       }
5451 +       free_page((unsigned long)sym.k);
5452 +
5453 +out:
5454 +       return err;
5455 +}
5456 +
5457 +/*
5458 + * regardless 'acl' option, reset all ACL.
5459 + * All ACL will be copied up later from the original entry on the lower branch.
5460 + */
5461 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5462 +{
5463 +       int err;
5464 +       struct dentry *h_dentry;
5465 +       struct inode *h_inode;
5466 +
5467 +       h_dentry = h_path->dentry;
5468 +       h_inode = d_inode(h_dentry);
5469 +       /* forget_all_cached_acls(h_inode)); */
5470 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5471 +       AuTraceErr(err);
5472 +       if (err == -EOPNOTSUPP)
5473 +               err = 0;
5474 +       if (!err)
5475 +               err = vfsub_acl_chmod(h_inode, mode);
5476 +
5477 +       AuTraceErr(err);
5478 +       return err;
5479 +}
5480 +
5481 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5482 +                         struct inode *h_dir, struct path *h_path)
5483 +{
5484 +       int err;
5485 +       struct inode *dir, *inode;
5486 +
5487 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5488 +       AuTraceErr(err);
5489 +       if (err == -EOPNOTSUPP)
5490 +               err = 0;
5491 +       if (unlikely(err))
5492 +               goto out;
5493 +
5494 +       /*
5495 +        * strange behaviour from the users view,
5496 +        * particularly setattr case
5497 +        */
5498 +       dir = d_inode(dst_parent);
5499 +       if (au_ibtop(dir) == cpg->bdst)
5500 +               au_cpup_attr_nlink(dir, /*force*/1);
5501 +       inode = d_inode(cpg->dentry);
5502 +       au_cpup_attr_nlink(inode, /*force*/1);
5503 +
5504 +out:
5505 +       return err;
5506 +}
5507 +
5508 +static noinline_for_stack
5509 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5510 +              struct au_cpup_reg_attr *h_src_attr)
5511 +{
5512 +       int err;
5513 +       umode_t mode;
5514 +       unsigned int mnt_flags;
5515 +       unsigned char isdir, isreg, force;
5516 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5517 +       struct au_dtime dt;
5518 +       struct path h_path;
5519 +       struct dentry *h_src, *h_dst, *h_parent;
5520 +       struct inode *h_inode, *h_dir;
5521 +       struct super_block *sb;
5522 +
5523 +       /* bsrc branch can be ro/rw. */
5524 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5525 +       h_inode = d_inode(h_src);
5526 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5527 +
5528 +       /* try stopping to be referenced while we are creating */
5529 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5530 +       if (au_ftest_cpup(cpg->flags, RENAME))
5531 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5532 +                                 AUFS_WH_PFX_LEN));
5533 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5534 +       h_dir = d_inode(h_parent);
5535 +       IMustLock(h_dir);
5536 +       AuDebugOn(h_parent != h_dst->d_parent);
5537 +
5538 +       sb = cpg->dentry->d_sb;
5539 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5540 +       if (do_dt) {
5541 +               h_path.dentry = h_parent;
5542 +               au_dtime_store(&dt, dst_parent, &h_path);
5543 +       }
5544 +       h_path.dentry = h_dst;
5545 +
5546 +       isreg = 0;
5547 +       isdir = 0;
5548 +       mode = h_inode->i_mode;
5549 +       switch (mode & S_IFMT) {
5550 +       case S_IFREG:
5551 +               isreg = 1;
5552 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5553 +               if (!err)
5554 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5555 +               break;
5556 +       case S_IFDIR:
5557 +               isdir = 1;
5558 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5559 +               if (!err)
5560 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5561 +               break;
5562 +       case S_IFLNK:
5563 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5564 +               break;
5565 +       case S_IFCHR:
5566 +       case S_IFBLK:
5567 +               AuDebugOn(!capable(CAP_MKNOD));
5568 +               /*FALLTHROUGH*/
5569 +       case S_IFIFO:
5570 +       case S_IFSOCK:
5571 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5572 +               break;
5573 +       default:
5574 +               AuIOErr("Unknown inode type 0%o\n", mode);
5575 +               err = -EIO;
5576 +       }
5577 +       if (!err)
5578 +               err = au_reset_acl(h_dir, &h_path, mode);
5579 +
5580 +       mnt_flags = au_mntflags(sb);
5581 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5582 +           && !isdir
5583 +           && au_opt_test(mnt_flags, XINO)
5584 +           && (h_inode->i_nlink == 1
5585 +               || (h_inode->i_state & I_LINKABLE))
5586 +           /* todo: unnecessary? */
5587 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5588 +           && cpg->bdst < cpg->bsrc
5589 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5590 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5591 +               /* ignore this error */
5592 +
5593 +       if (!err) {
5594 +               force = 0;
5595 +               if (isreg) {
5596 +                       force = !!cpg->len;
5597 +                       if (cpg->len == -1)
5598 +                               force = !!i_size_read(h_inode);
5599 +               }
5600 +               au_fhsm_wrote(sb, cpg->bdst, force);
5601 +       }
5602 +
5603 +       if (do_dt)
5604 +               au_dtime_revert(&dt);
5605 +       return err;
5606 +}
5607 +
5608 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5609 +{
5610 +       int err;
5611 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5612 +       struct inode *h_dir;
5613 +       aufs_bindex_t bdst;
5614 +
5615 +       dentry = cpg->dentry;
5616 +       bdst = cpg->bdst;
5617 +       h_dentry = au_h_dptr(dentry, bdst);
5618 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5619 +               dget(h_dentry);
5620 +               au_set_h_dptr(dentry, bdst, NULL);
5621 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5622 +               if (!err)
5623 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5624 +               au_set_h_dptr(dentry, bdst, h_dentry);
5625 +       } else {
5626 +               err = 0;
5627 +               parent = dget_parent(dentry);
5628 +               h_parent = au_h_dptr(parent, bdst);
5629 +               dput(parent);
5630 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5631 +               if (IS_ERR(h_path->dentry))
5632 +                       err = PTR_ERR(h_path->dentry);
5633 +       }
5634 +       if (unlikely(err))
5635 +               goto out;
5636 +
5637 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5638 +       h_dir = d_inode(h_parent);
5639 +       IMustLock(h_dir);
5640 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5641 +       /* no delegation since it is just created */
5642 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5643 +                          /*flags*/0);
5644 +       dput(h_path->dentry);
5645 +
5646 +out:
5647 +       return err;
5648 +}
5649 +
5650 +/*
5651 + * copyup the @dentry from @bsrc to @bdst.
5652 + * the caller must set the both of lower dentries.
5653 + * @len is for truncating when it is -1 copyup the entire file.
5654 + * in link/rename cases, @dst_parent may be different from the real one.
5655 + * basic->bsrc can be larger than basic->bdst.
5656 + * aufs doesn't touch the credential so
5657 + * security_inode_copy_up{,_xattr}() are unnecessary.
5658 + */
5659 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5660 +{
5661 +       int err, rerr;
5662 +       aufs_bindex_t old_ibtop;
5663 +       unsigned char isdir, plink;
5664 +       struct dentry *h_src, *h_dst, *h_parent;
5665 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5666 +       struct super_block *sb;
5667 +       struct au_branch *br;
5668 +       /* to reduce stack size */
5669 +       struct {
5670 +               struct au_dtime dt;
5671 +               struct path h_path;
5672 +               struct au_cpup_reg_attr h_src_attr;
5673 +       } *a;
5674 +
5675 +       err = -ENOMEM;
5676 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5677 +       if (unlikely(!a))
5678 +               goto out;
5679 +       a->h_src_attr.valid = 0;
5680 +
5681 +       sb = cpg->dentry->d_sb;
5682 +       br = au_sbr(sb, cpg->bdst);
5683 +       a->h_path.mnt = au_br_mnt(br);
5684 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5685 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5686 +       h_dir = d_inode(h_parent);
5687 +       IMustLock(h_dir);
5688 +
5689 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5690 +       inode = d_inode(cpg->dentry);
5691 +
5692 +       if (!dst_parent)
5693 +               dst_parent = dget_parent(cpg->dentry);
5694 +       else
5695 +               dget(dst_parent);
5696 +
5697 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5698 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5699 +       if (dst_inode) {
5700 +               if (unlikely(!plink)) {
5701 +                       err = -EIO;
5702 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5703 +                               "but plink is disabled\n",
5704 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5705 +                       goto out_parent;
5706 +               }
5707 +
5708 +               if (dst_inode->i_nlink) {
5709 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5710 +
5711 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5712 +                       err = PTR_ERR(h_src);
5713 +                       if (IS_ERR(h_src))
5714 +                               goto out_parent;
5715 +                       if (unlikely(d_is_negative(h_src))) {
5716 +                               err = -EIO;
5717 +                               AuIOErr("i%lu exists on b%d "
5718 +                                       "but not pseudo-linked\n",
5719 +                                       inode->i_ino, cpg->bdst);
5720 +                               dput(h_src);
5721 +                               goto out_parent;
5722 +                       }
5723 +
5724 +                       if (do_dt) {
5725 +                               a->h_path.dentry = h_parent;
5726 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5727 +                       }
5728 +
5729 +                       a->h_path.dentry = h_dst;
5730 +                       delegated = NULL;
5731 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5732 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5733 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5734 +                       if (do_dt)
5735 +                               au_dtime_revert(&a->dt);
5736 +                       if (unlikely(err == -EWOULDBLOCK)) {
5737 +                               pr_warn("cannot retry for NFSv4 delegation"
5738 +                                       " for an internal link\n");
5739 +                               iput(delegated);
5740 +                       }
5741 +                       dput(h_src);
5742 +                       goto out_parent;
5743 +               } else
5744 +                       /* todo: cpup_wh_file? */
5745 +                       /* udba work */
5746 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5747 +       }
5748 +
5749 +       isdir = S_ISDIR(inode->i_mode);
5750 +       old_ibtop = au_ibtop(inode);
5751 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5752 +       if (unlikely(err))
5753 +               goto out_rev;
5754 +       dst_inode = d_inode(h_dst);
5755 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5756 +       /* todo: necessary? */
5757 +       /* au_pin_hdir_unlock(cpg->pin); */
5758 +
5759 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5760 +       if (unlikely(err)) {
5761 +               /* todo: necessary? */
5762 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5763 +               inode_unlock(dst_inode);
5764 +               goto out_rev;
5765 +       }
5766 +
5767 +       if (cpg->bdst < old_ibtop) {
5768 +               if (S_ISREG(inode->i_mode)) {
5769 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5770 +                       if (unlikely(err)) {
5771 +                               /* ignore an error */
5772 +                               /* au_pin_hdir_relock(cpg->pin); */
5773 +                               inode_unlock(dst_inode);
5774 +                               goto out_rev;
5775 +                       }
5776 +               }
5777 +               au_set_ibtop(inode, cpg->bdst);
5778 +       } else
5779 +               au_set_ibbot(inode, cpg->bdst);
5780 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5781 +                     au_hi_flags(inode, isdir));
5782 +
5783 +       /* todo: necessary? */
5784 +       /* err = au_pin_hdir_relock(cpg->pin); */
5785 +       inode_unlock(dst_inode);
5786 +       if (unlikely(err))
5787 +               goto out_rev;
5788 +
5789 +       src_inode = d_inode(h_src);
5790 +       if (!isdir
5791 +           && (src_inode->i_nlink > 1
5792 +               || src_inode->i_state & I_LINKABLE)
5793 +           && plink)
5794 +               au_plink_append(inode, cpg->bdst, h_dst);
5795 +
5796 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5797 +               a->h_path.dentry = h_dst;
5798 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5799 +       }
5800 +       if (!err)
5801 +               goto out_parent; /* success */
5802 +
5803 +       /* revert */
5804 +out_rev:
5805 +       a->h_path.dentry = h_parent;
5806 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5807 +       a->h_path.dentry = h_dst;
5808 +       rerr = 0;
5809 +       if (d_is_positive(h_dst)) {
5810 +               if (!isdir) {
5811 +                       /* no delegation since it is just created */
5812 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5813 +                                           /*delegated*/NULL, /*force*/0);
5814 +               } else
5815 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5816 +       }
5817 +       au_dtime_revert(&a->dt);
5818 +       if (rerr) {
5819 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5820 +               err = -EIO;
5821 +       }
5822 +out_parent:
5823 +       dput(dst_parent);
5824 +       au_kfree_rcu(a);
5825 +out:
5826 +       return err;
5827 +}
5828 +
5829 +#if 0 /* reserved */
5830 +struct au_cpup_single_args {
5831 +       int *errp;
5832 +       struct au_cp_generic *cpg;
5833 +       struct dentry *dst_parent;
5834 +};
5835 +
5836 +static void au_call_cpup_single(void *args)
5837 +{
5838 +       struct au_cpup_single_args *a = args;
5839 +
5840 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5841 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5842 +       au_pin_hdir_release(a->cpg->pin);
5843 +}
5844 +#endif
5845 +
5846 +/*
5847 + * prevent SIGXFSZ in copy-up.
5848 + * testing CAP_MKNOD is for generic fs,
5849 + * but CAP_FSETID is for xfs only, currently.
5850 + */
5851 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5852 +{
5853 +       int do_sio;
5854 +       struct super_block *sb;
5855 +       struct inode *h_dir;
5856 +
5857 +       do_sio = 0;
5858 +       sb = au_pinned_parent(pin)->d_sb;
5859 +       if (!au_wkq_test()
5860 +           && (!au_sbi(sb)->si_plink_maint_pid
5861 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5862 +               switch (mode & S_IFMT) {
5863 +               case S_IFREG:
5864 +                       /* no condition about RLIMIT_FSIZE and the file size */
5865 +                       do_sio = 1;
5866 +                       break;
5867 +               case S_IFCHR:
5868 +               case S_IFBLK:
5869 +                       do_sio = !capable(CAP_MKNOD);
5870 +                       break;
5871 +               }
5872 +               if (!do_sio)
5873 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5874 +                                 && !capable(CAP_FSETID));
5875 +               /* this workaround may be removed in the future */
5876 +               if (!do_sio) {
5877 +                       h_dir = au_pinned_h_dir(pin);
5878 +                       do_sio = h_dir->i_mode & S_ISVTX;
5879 +               }
5880 +       }
5881 +
5882 +       return do_sio;
5883 +}
5884 +
5885 +#if 0 /* reserved */
5886 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5887 +{
5888 +       int err, wkq_err;
5889 +       struct dentry *h_dentry;
5890 +
5891 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5892 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5893 +               err = au_cpup_single(cpg, dst_parent);
5894 +       else {
5895 +               struct au_cpup_single_args args = {
5896 +                       .errp           = &err,
5897 +                       .cpg            = cpg,
5898 +                       .dst_parent     = dst_parent
5899 +               };
5900 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5901 +               if (unlikely(wkq_err))
5902 +                       err = wkq_err;
5903 +       }
5904 +
5905 +       return err;
5906 +}
5907 +#endif
5908 +
5909 +/*
5910 + * copyup the @dentry from the first active lower branch to @bdst,
5911 + * using au_cpup_single().
5912 + */
5913 +static int au_cpup_simple(struct au_cp_generic *cpg)
5914 +{
5915 +       int err;
5916 +       unsigned int flags_orig;
5917 +       struct dentry *dentry;
5918 +
5919 +       AuDebugOn(cpg->bsrc < 0);
5920 +
5921 +       dentry = cpg->dentry;
5922 +       DiMustWriteLock(dentry);
5923 +
5924 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5925 +       if (!err) {
5926 +               flags_orig = cpg->flags;
5927 +               au_fset_cpup(cpg->flags, RENAME);
5928 +               err = au_cpup_single(cpg, NULL);
5929 +               cpg->flags = flags_orig;
5930 +               if (!err)
5931 +                       return 0; /* success */
5932 +
5933 +               /* revert */
5934 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5935 +               au_set_dbtop(dentry, cpg->bsrc);
5936 +       }
5937 +
5938 +       return err;
5939 +}
5940 +
5941 +struct au_cpup_simple_args {
5942 +       int *errp;
5943 +       struct au_cp_generic *cpg;
5944 +};
5945 +
5946 +static void au_call_cpup_simple(void *args)
5947 +{
5948 +       struct au_cpup_simple_args *a = args;
5949 +
5950 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5951 +       *a->errp = au_cpup_simple(a->cpg);
5952 +       au_pin_hdir_release(a->cpg->pin);
5953 +}
5954 +
5955 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5956 +{
5957 +       int err, wkq_err;
5958 +       struct dentry *dentry, *parent;
5959 +       struct file *h_file;
5960 +       struct inode *h_dir;
5961 +
5962 +       dentry = cpg->dentry;
5963 +       h_file = NULL;
5964 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5965 +               AuDebugOn(cpg->bsrc < 0);
5966 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5967 +               err = PTR_ERR(h_file);
5968 +               if (IS_ERR(h_file))
5969 +                       goto out;
5970 +       }
5971 +
5972 +       parent = dget_parent(dentry);
5973 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5974 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5975 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5976 +               err = au_cpup_simple(cpg);
5977 +       else {
5978 +               struct au_cpup_simple_args args = {
5979 +                       .errp           = &err,
5980 +                       .cpg            = cpg
5981 +               };
5982 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5983 +               if (unlikely(wkq_err))
5984 +                       err = wkq_err;
5985 +       }
5986 +
5987 +       dput(parent);
5988 +       if (h_file)
5989 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5990 +
5991 +out:
5992 +       return err;
5993 +}
5994 +
5995 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5996 +{
5997 +       aufs_bindex_t bsrc, bbot;
5998 +       struct dentry *dentry, *h_dentry;
5999 +
6000 +       if (cpg->bsrc < 0) {
6001 +               dentry = cpg->dentry;
6002 +               bbot = au_dbbot(dentry);
6003 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
6004 +                       h_dentry = au_h_dptr(dentry, bsrc);
6005 +                       if (h_dentry) {
6006 +                               AuDebugOn(d_is_negative(h_dentry));
6007 +                               break;
6008 +                       }
6009 +               }
6010 +               AuDebugOn(bsrc > bbot);
6011 +               cpg->bsrc = bsrc;
6012 +       }
6013 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
6014 +       return au_do_sio_cpup_simple(cpg);
6015 +}
6016 +
6017 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
6018 +{
6019 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
6020 +       return au_do_sio_cpup_simple(cpg);
6021 +}
6022 +
6023 +/* ---------------------------------------------------------------------- */
6024 +
6025 +/*
6026 + * copyup the deleted file for writing.
6027 + */
6028 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
6029 +                        struct file *file)
6030 +{
6031 +       int err;
6032 +       unsigned int flags_orig;
6033 +       aufs_bindex_t bsrc_orig;
6034 +       struct au_dinfo *dinfo;
6035 +       struct {
6036 +               struct au_hdentry *hd;
6037 +               struct dentry *h_dentry;
6038 +       } hdst, hsrc;
6039 +
6040 +       dinfo = au_di(cpg->dentry);
6041 +       AuRwMustWriteLock(&dinfo->di_rwsem);
6042 +
6043 +       bsrc_orig = cpg->bsrc;
6044 +       cpg->bsrc = dinfo->di_btop;
6045 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
6046 +       hdst.h_dentry = hdst.hd->hd_dentry;
6047 +       hdst.hd->hd_dentry = wh_dentry;
6048 +       dinfo->di_btop = cpg->bdst;
6049 +
6050 +       hsrc.h_dentry = NULL;
6051 +       if (file) {
6052 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
6053 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
6054 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
6055 +       }
6056 +       flags_orig = cpg->flags;
6057 +       cpg->flags = !AuCpup_DTIME;
6058 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
6059 +       cpg->flags = flags_orig;
6060 +       if (file) {
6061 +               if (!err)
6062 +                       err = au_reopen_nondir(file);
6063 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
6064 +       }
6065 +       hdst.hd->hd_dentry = hdst.h_dentry;
6066 +       dinfo->di_btop = cpg->bsrc;
6067 +       cpg->bsrc = bsrc_orig;
6068 +
6069 +       return err;
6070 +}
6071 +
6072 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6073 +{
6074 +       int err;
6075 +       aufs_bindex_t bdst;
6076 +       struct au_dtime dt;
6077 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6078 +       struct au_branch *br;
6079 +       struct path h_path;
6080 +
6081 +       dentry = cpg->dentry;
6082 +       bdst = cpg->bdst;
6083 +       br = au_sbr(dentry->d_sb, bdst);
6084 +       parent = dget_parent(dentry);
6085 +       h_parent = au_h_dptr(parent, bdst);
6086 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6087 +       err = PTR_ERR(wh_dentry);
6088 +       if (IS_ERR(wh_dentry))
6089 +               goto out;
6090 +
6091 +       h_path.dentry = h_parent;
6092 +       h_path.mnt = au_br_mnt(br);
6093 +       au_dtime_store(&dt, parent, &h_path);
6094 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6095 +       if (unlikely(err))
6096 +               goto out_wh;
6097 +
6098 +       dget(wh_dentry);
6099 +       h_path.dentry = wh_dentry;
6100 +       if (!d_is_dir(wh_dentry)) {
6101 +               /* no delegation since it is just created */
6102 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6103 +                                  /*delegated*/NULL, /*force*/0);
6104 +       } else
6105 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6106 +       if (unlikely(err)) {
6107 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6108 +                       wh_dentry, err);
6109 +               err = -EIO;
6110 +       }
6111 +       au_dtime_revert(&dt);
6112 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6113 +
6114 +out_wh:
6115 +       dput(wh_dentry);
6116 +out:
6117 +       dput(parent);
6118 +       return err;
6119 +}
6120 +
6121 +struct au_cpup_wh_args {
6122 +       int *errp;
6123 +       struct au_cp_generic *cpg;
6124 +       struct file *file;
6125 +};
6126 +
6127 +static void au_call_cpup_wh(void *args)
6128 +{
6129 +       struct au_cpup_wh_args *a = args;
6130 +
6131 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6132 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6133 +       au_pin_hdir_release(a->cpg->pin);
6134 +}
6135 +
6136 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6137 +{
6138 +       int err, wkq_err;
6139 +       aufs_bindex_t bdst;
6140 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6141 +       struct inode *dir, *h_dir, *h_tmpdir;
6142 +       struct au_wbr *wbr;
6143 +       struct au_pin wh_pin, *pin_orig;
6144 +
6145 +       dentry = cpg->dentry;
6146 +       bdst = cpg->bdst;
6147 +       parent = dget_parent(dentry);
6148 +       dir = d_inode(parent);
6149 +       h_orph = NULL;
6150 +       h_parent = NULL;
6151 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6152 +       h_tmpdir = h_dir;
6153 +       pin_orig = NULL;
6154 +       if (!h_dir->i_nlink) {
6155 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6156 +               h_orph = wbr->wbr_orph;
6157 +
6158 +               h_parent = dget(au_h_dptr(parent, bdst));
6159 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6160 +               h_tmpdir = d_inode(h_orph);
6161 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6162 +
6163 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6164 +               /* todo: au_h_open_pre()? */
6165 +
6166 +               pin_orig = cpg->pin;
6167 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6168 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6169 +               cpg->pin = &wh_pin;
6170 +       }
6171 +
6172 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6173 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6174 +               err = au_cpup_wh(cpg, file);
6175 +       else {
6176 +               struct au_cpup_wh_args args = {
6177 +                       .errp   = &err,
6178 +                       .cpg    = cpg,
6179 +                       .file   = file
6180 +               };
6181 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6182 +               if (unlikely(wkq_err))
6183 +                       err = wkq_err;
6184 +       }
6185 +
6186 +       if (h_orph) {
6187 +               inode_unlock(h_tmpdir);
6188 +               /* todo: au_h_open_post()? */
6189 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6190 +               au_set_h_dptr(parent, bdst, h_parent);
6191 +               AuDebugOn(!pin_orig);
6192 +               cpg->pin = pin_orig;
6193 +       }
6194 +       iput(h_dir);
6195 +       dput(parent);
6196 +
6197 +       return err;
6198 +}
6199 +
6200 +/* ---------------------------------------------------------------------- */
6201 +
6202 +/*
6203 + * generic routine for both of copy-up and copy-down.
6204 + */
6205 +/* cf. revalidate function in file.c */
6206 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6207 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6208 +                        struct au_pin *pin,
6209 +                        struct dentry *h_parent, void *arg),
6210 +              void *arg)
6211 +{
6212 +       int err;
6213 +       struct au_pin pin;
6214 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6215 +
6216 +       err = 0;
6217 +       parent = dget_parent(dentry);
6218 +       if (IS_ROOT(parent))
6219 +               goto out;
6220 +
6221 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6222 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6223 +
6224 +       /* do not use au_dpage */
6225 +       real_parent = parent;
6226 +       while (1) {
6227 +               dput(parent);
6228 +               parent = dget_parent(dentry);
6229 +               h_parent = au_h_dptr(parent, bdst);
6230 +               if (h_parent)
6231 +                       goto out; /* success */
6232 +
6233 +               /* find top dir which is necessary to cpup */
6234 +               do {
6235 +                       d = parent;
6236 +                       dput(parent);
6237 +                       parent = dget_parent(d);
6238 +                       di_read_lock_parent3(parent, !AuLock_IR);
6239 +                       h_parent = au_h_dptr(parent, bdst);
6240 +                       di_read_unlock(parent, !AuLock_IR);
6241 +               } while (!h_parent);
6242 +
6243 +               if (d != real_parent)
6244 +                       di_write_lock_child3(d);
6245 +
6246 +               /* somebody else might create while we were sleeping */
6247 +               h_dentry = au_h_dptr(d, bdst);
6248 +               if (!h_dentry || d_is_negative(h_dentry)) {
6249 +                       if (h_dentry)
6250 +                               au_update_dbtop(d);
6251 +
6252 +                       au_pin_set_dentry(&pin, d);
6253 +                       err = au_do_pin(&pin);
6254 +                       if (!err) {
6255 +                               err = cp(d, bdst, &pin, h_parent, arg);
6256 +                               au_unpin(&pin);
6257 +                       }
6258 +               }
6259 +
6260 +               if (d != real_parent)
6261 +                       di_write_unlock(d);
6262 +               if (unlikely(err))
6263 +                       break;
6264 +       }
6265 +
6266 +out:
6267 +       dput(parent);
6268 +       return err;
6269 +}
6270 +
6271 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6272 +                      struct au_pin *pin,
6273 +                      struct dentry *h_parent __maybe_unused,
6274 +                      void *arg __maybe_unused)
6275 +{
6276 +       struct au_cp_generic cpg = {
6277 +               .dentry = dentry,
6278 +               .bdst   = bdst,
6279 +               .bsrc   = -1,
6280 +               .len    = 0,
6281 +               .pin    = pin,
6282 +               .flags  = AuCpup_DTIME
6283 +       };
6284 +       return au_sio_cpup_simple(&cpg);
6285 +}
6286 +
6287 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6288 +{
6289 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6290 +}
6291 +
6292 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6293 +{
6294 +       int err;
6295 +       struct dentry *parent;
6296 +       struct inode *dir;
6297 +
6298 +       parent = dget_parent(dentry);
6299 +       dir = d_inode(parent);
6300 +       err = 0;
6301 +       if (au_h_iptr(dir, bdst))
6302 +               goto out;
6303 +
6304 +       di_read_unlock(parent, AuLock_IR);
6305 +       di_write_lock_parent(parent);
6306 +       /* someone else might change our inode while we were sleeping */
6307 +       if (!au_h_iptr(dir, bdst))
6308 +               err = au_cpup_dirs(dentry, bdst);
6309 +       di_downgrade_lock(parent, AuLock_IR);
6310 +
6311 +out:
6312 +       dput(parent);
6313 +       return err;
6314 +}
6315 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6316 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6317 +++ linux/fs/aufs/cpup.h        2020-01-27 10:57:18.168871450 +0100
6318 @@ -0,0 +1,100 @@
6319 +/* SPDX-License-Identifier: GPL-2.0 */
6320 +/*
6321 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6322 + *
6323 + * This program, aufs is free software; you can redistribute it and/or modify
6324 + * it under the terms of the GNU General Public License as published by
6325 + * the Free Software Foundation; either version 2 of the License, or
6326 + * (at your option) any later version.
6327 + *
6328 + * This program is distributed in the hope that it will be useful,
6329 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6330 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6331 + * GNU General Public License for more details.
6332 + *
6333 + * You should have received a copy of the GNU General Public License
6334 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6335 + */
6336 +
6337 +/*
6338 + * copy-up/down functions
6339 + */
6340 +
6341 +#ifndef __AUFS_CPUP_H__
6342 +#define __AUFS_CPUP_H__
6343 +
6344 +#ifdef __KERNEL__
6345 +
6346 +#include <linux/path.h>
6347 +
6348 +struct inode;
6349 +struct file;
6350 +struct au_pin;
6351 +
6352 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6353 +void au_cpup_attr_timesizes(struct inode *inode);
6354 +void au_cpup_attr_nlink(struct inode *inode, int force);
6355 +void au_cpup_attr_changeable(struct inode *inode);
6356 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6357 +void au_cpup_attr_all(struct inode *inode, int force);
6358 +
6359 +/* ---------------------------------------------------------------------- */
6360 +
6361 +struct au_cp_generic {
6362 +       struct dentry   *dentry;
6363 +       aufs_bindex_t   bdst, bsrc;
6364 +       loff_t          len;
6365 +       struct au_pin   *pin;
6366 +       unsigned int    flags;
6367 +};
6368 +
6369 +/* cpup flags */
6370 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6371 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6372 +                                                  for link(2) */
6373 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6374 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6375 +                                                  cpup */
6376 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6377 +                                                  existing entry */
6378 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6379 +                                                  the branch is marked as RO */
6380 +
6381 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6382 +#undef AuCpup_HOPEN
6383 +#define AuCpup_HOPEN           0
6384 +#endif
6385 +
6386 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6387 +#define au_fset_cpup(flags, name) \
6388 +       do { (flags) |= AuCpup_##name; } while (0)
6389 +#define au_fclr_cpup(flags, name) \
6390 +       do { (flags) &= ~AuCpup_##name; } while (0)
6391 +
6392 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6393 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6394 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6395 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6396 +
6397 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6398 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6399 +                        struct au_pin *pin,
6400 +                        struct dentry *h_parent, void *arg),
6401 +              void *arg);
6402 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6403 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6404 +
6405 +/* ---------------------------------------------------------------------- */
6406 +
6407 +/* keep timestamps when copyup */
6408 +struct au_dtime {
6409 +       struct dentry *dt_dentry;
6410 +       struct path dt_h_path;
6411 +       struct timespec64 dt_atime, dt_mtime;
6412 +};
6413 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6414 +                   struct path *h_path);
6415 +void au_dtime_revert(struct au_dtime *dt);
6416 +
6417 +#endif /* __KERNEL__ */
6418 +#endif /* __AUFS_CPUP_H__ */
6419 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6420 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6421 +++ linux/fs/aufs/dbgaufs.c     2020-01-27 10:57:18.168871450 +0100
6422 @@ -0,0 +1,526 @@
6423 +// SPDX-License-Identifier: GPL-2.0
6424 +/*
6425 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6426 + *
6427 + * This program, aufs is free software; you can redistribute it and/or modify
6428 + * it under the terms of the GNU General Public License as published by
6429 + * the Free Software Foundation; either version 2 of the License, or
6430 + * (at your option) any later version.
6431 + *
6432 + * This program is distributed in the hope that it will be useful,
6433 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6434 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6435 + * GNU General Public License for more details.
6436 + *
6437 + * You should have received a copy of the GNU General Public License
6438 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6439 + */
6440 +
6441 +/*
6442 + * debugfs interface
6443 + */
6444 +
6445 +#include <linux/debugfs.h>
6446 +#include "aufs.h"
6447 +
6448 +#ifndef CONFIG_SYSFS
6449 +#error DEBUG_FS depends upon SYSFS
6450 +#endif
6451 +
6452 +static struct dentry *dbgaufs;
6453 +static const mode_t dbgaufs_mode = 0444;
6454 +
6455 +/* 20 is max digits length of ulong 64 */
6456 +struct dbgaufs_arg {
6457 +       int n;
6458 +       char a[20 * 4];
6459 +};
6460 +
6461 +/*
6462 + * common function for all XINO files
6463 + */
6464 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6465 +                             struct file *file)
6466 +{
6467 +       void *p;
6468 +
6469 +       p = file->private_data;
6470 +       if (p) {
6471 +               /* this is struct dbgaufs_arg */
6472 +               AuDebugOn(!au_kfree_sz_test(p));
6473 +               au_kfree_do_rcu(p);
6474 +       }
6475 +       return 0;
6476 +}
6477 +
6478 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6479 +                          int cnt)
6480 +{
6481 +       int err;
6482 +       struct kstat st;
6483 +       struct dbgaufs_arg *p;
6484 +
6485 +       err = -ENOMEM;
6486 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6487 +       if (unlikely(!p))
6488 +               goto out;
6489 +
6490 +       err = 0;
6491 +       p->n = 0;
6492 +       file->private_data = p;
6493 +       if (!xf)
6494 +               goto out;
6495 +
6496 +       err = vfsub_getattr(&xf->f_path, &st);
6497 +       if (!err) {
6498 +               if (do_fcnt)
6499 +                       p->n = snprintf
6500 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6501 +                                cnt, st.blocks, st.blksize,
6502 +                                (long long)st.size);
6503 +               else
6504 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6505 +                                       st.blocks, st.blksize,
6506 +                                       (long long)st.size);
6507 +               AuDebugOn(p->n >= sizeof(p->a));
6508 +       } else {
6509 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6510 +               err = 0;
6511 +       }
6512 +
6513 +out:
6514 +       return err;
6515 +}
6516 +
6517 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6518 +                              size_t count, loff_t *ppos)
6519 +{
6520 +       struct dbgaufs_arg *p;
6521 +
6522 +       p = file->private_data;
6523 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6524 +}
6525 +
6526 +/* ---------------------------------------------------------------------- */
6527 +
6528 +struct dbgaufs_plink_arg {
6529 +       int n;
6530 +       char a[];
6531 +};
6532 +
6533 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6534 +                                struct file *file)
6535 +{
6536 +       free_page((unsigned long)file->private_data);
6537 +       return 0;
6538 +}
6539 +
6540 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6541 +{
6542 +       int err, i, limit;
6543 +       unsigned long n, sum;
6544 +       struct dbgaufs_plink_arg *p;
6545 +       struct au_sbinfo *sbinfo;
6546 +       struct super_block *sb;
6547 +       struct hlist_bl_head *hbl;
6548 +
6549 +       err = -ENOMEM;
6550 +       p = (void *)get_zeroed_page(GFP_NOFS);
6551 +       if (unlikely(!p))
6552 +               goto out;
6553 +
6554 +       err = -EFBIG;
6555 +       sbinfo = inode->i_private;
6556 +       sb = sbinfo->si_sb;
6557 +       si_noflush_read_lock(sb);
6558 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6559 +               limit = PAGE_SIZE - sizeof(p->n);
6560 +
6561 +               /* the number of buckets */
6562 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6563 +               p->n += n;
6564 +               limit -= n;
6565 +
6566 +               sum = 0;
6567 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6568 +                    i++, hbl++) {
6569 +                       n = au_hbl_count(hbl);
6570 +                       sum += n;
6571 +
6572 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6573 +                       p->n += n;
6574 +                       limit -= n;
6575 +                       if (unlikely(limit <= 0))
6576 +                               goto out_free;
6577 +               }
6578 +               p->a[p->n - 1] = '\n';
6579 +
6580 +               /* the sum of plinks */
6581 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6582 +               p->n += n;
6583 +               limit -= n;
6584 +               if (unlikely(limit <= 0))
6585 +                       goto out_free;
6586 +       } else {
6587 +#define str "1\n0\n0\n"
6588 +               p->n = sizeof(str) - 1;
6589 +               strcpy(p->a, str);
6590 +#undef str
6591 +       }
6592 +       si_read_unlock(sb);
6593 +
6594 +       err = 0;
6595 +       file->private_data = p;
6596 +       goto out; /* success */
6597 +
6598 +out_free:
6599 +       free_page((unsigned long)p);
6600 +out:
6601 +       return err;
6602 +}
6603 +
6604 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6605 +                                 size_t count, loff_t *ppos)
6606 +{
6607 +       struct dbgaufs_plink_arg *p;
6608 +
6609 +       p = file->private_data;
6610 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6611 +}
6612 +
6613 +static const struct file_operations dbgaufs_plink_fop = {
6614 +       .owner          = THIS_MODULE,
6615 +       .open           = dbgaufs_plink_open,
6616 +       .release        = dbgaufs_plink_release,
6617 +       .read           = dbgaufs_plink_read
6618 +};
6619 +
6620 +/* ---------------------------------------------------------------------- */
6621 +
6622 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6623 +{
6624 +       int err;
6625 +       struct au_sbinfo *sbinfo;
6626 +       struct super_block *sb;
6627 +
6628 +       sbinfo = inode->i_private;
6629 +       sb = sbinfo->si_sb;
6630 +       si_noflush_read_lock(sb);
6631 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6632 +       si_read_unlock(sb);
6633 +       return err;
6634 +}
6635 +
6636 +static const struct file_operations dbgaufs_xib_fop = {
6637 +       .owner          = THIS_MODULE,
6638 +       .open           = dbgaufs_xib_open,
6639 +       .release        = dbgaufs_xi_release,
6640 +       .read           = dbgaufs_xi_read
6641 +};
6642 +
6643 +/* ---------------------------------------------------------------------- */
6644 +
6645 +#define DbgaufsXi_PREFIX "xi"
6646 +
6647 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6648 +{
6649 +       int err, idx;
6650 +       long l;
6651 +       aufs_bindex_t bindex;
6652 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6653 +       struct au_sbinfo *sbinfo;
6654 +       struct super_block *sb;
6655 +       struct au_xino *xi;
6656 +       struct file *xf;
6657 +       struct qstr *name;
6658 +       struct au_branch *br;
6659 +
6660 +       err = -ENOENT;
6661 +       name = &file->f_path.dentry->d_name;
6662 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6663 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6664 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6665 +               goto out;
6666 +
6667 +       AuDebugOn(name->len >= sizeof(a));
6668 +       memcpy(a, name->name, name->len);
6669 +       a[name->len] = '\0';
6670 +       p = strchr(a, '-');
6671 +       if (p)
6672 +               *p = '\0';
6673 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6674 +       if (unlikely(err))
6675 +               goto out;
6676 +       bindex = l;
6677 +       idx = 0;
6678 +       if (p) {
6679 +               err = kstrtol(p + 1, 10, &l);
6680 +               if (unlikely(err))
6681 +                       goto out;
6682 +               idx = l;
6683 +       }
6684 +
6685 +       err = -ENOENT;
6686 +       sbinfo = inode->i_private;
6687 +       sb = sbinfo->si_sb;
6688 +       si_noflush_read_lock(sb);
6689 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6690 +               goto out_si;
6691 +       br = au_sbr(sb, bindex);
6692 +       xi = br->br_xino;
6693 +       if (unlikely(idx >= xi->xi_nfile))
6694 +               goto out_si;
6695 +       xf = au_xino_file(xi, idx);
6696 +       if (xf)
6697 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6698 +                                     au_xino_count(br));
6699 +
6700 +out_si:
6701 +       si_read_unlock(sb);
6702 +out:
6703 +       AuTraceErr(err);
6704 +       return err;
6705 +}
6706 +
6707 +static const struct file_operations dbgaufs_xino_fop = {
6708 +       .owner          = THIS_MODULE,
6709 +       .open           = dbgaufs_xino_open,
6710 +       .release        = dbgaufs_xi_release,
6711 +       .read           = dbgaufs_xi_read
6712 +};
6713 +
6714 +void dbgaufs_xino_del(struct au_branch *br)
6715 +{
6716 +       struct dentry *dbgaufs;
6717 +
6718 +       dbgaufs = br->br_dbgaufs;
6719 +       if (!dbgaufs)
6720 +               return;
6721 +
6722 +       br->br_dbgaufs = NULL;
6723 +       /* debugfs acquires the parent i_mutex */
6724 +       lockdep_off();
6725 +       debugfs_remove(dbgaufs);
6726 +       lockdep_on();
6727 +}
6728 +
6729 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6730 +{
6731 +       aufs_bindex_t bbot;
6732 +       struct au_branch *br;
6733 +
6734 +       if (!au_sbi(sb)->si_dbgaufs)
6735 +               return;
6736 +
6737 +       bbot = au_sbbot(sb);
6738 +       for (; bindex <= bbot; bindex++) {
6739 +               br = au_sbr(sb, bindex);
6740 +               dbgaufs_xino_del(br);
6741 +       }
6742 +}
6743 +
6744 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6745 +                             unsigned int idx, struct dentry *parent,
6746 +                             struct au_sbinfo *sbinfo)
6747 +{
6748 +       struct au_branch *br;
6749 +       struct dentry *d;
6750 +       /* "xi" bindex(5) "-" idx(2) NULL */
6751 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6752 +
6753 +       if (!idx)
6754 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6755 +       else
6756 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6757 +                        bindex, idx);
6758 +       br = au_sbr(sb, bindex);
6759 +       if (br->br_dbgaufs) {
6760 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6761 +
6762 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6763 +                       /* debugfs acquires the parent i_mutex */
6764 +                       lockdep_off();
6765 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6766 +                                          name);
6767 +                       lockdep_on();
6768 +                       if (unlikely(!d))
6769 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6770 +                                       parent, name);
6771 +               }
6772 +       } else {
6773 +               lockdep_off();
6774 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6775 +                                                    sbinfo, &dbgaufs_xino_fop);
6776 +               lockdep_on();
6777 +               if (unlikely(!br->br_dbgaufs))
6778 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6779 +                               parent, name);
6780 +       }
6781 +}
6782 +
6783 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6784 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6785 +{
6786 +       struct au_branch *br;
6787 +       struct au_xino *xi;
6788 +       unsigned int u;
6789 +
6790 +       br = au_sbr(sb, bindex);
6791 +       xi = br->br_xino;
6792 +       for (u = 0; u < xi->xi_nfile; u++)
6793 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6794 +}
6795 +
6796 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6797 +{
6798 +       struct au_sbinfo *sbinfo;
6799 +       struct dentry *parent;
6800 +       aufs_bindex_t bbot;
6801 +
6802 +       if (!au_opt_test(au_mntflags(sb), XINO))
6803 +               return;
6804 +
6805 +       sbinfo = au_sbi(sb);
6806 +       parent = sbinfo->si_dbgaufs;
6807 +       if (!parent)
6808 +               return;
6809 +
6810 +       bbot = au_sbbot(sb);
6811 +       if (topdown)
6812 +               for (; bindex <= bbot; bindex++)
6813 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6814 +       else
6815 +               for (; bbot >= bindex; bbot--)
6816 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6817 +}
6818 +
6819 +/* ---------------------------------------------------------------------- */
6820 +
6821 +#ifdef CONFIG_AUFS_EXPORT
6822 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6823 +{
6824 +       int err;
6825 +       struct au_sbinfo *sbinfo;
6826 +       struct super_block *sb;
6827 +
6828 +       sbinfo = inode->i_private;
6829 +       sb = sbinfo->si_sb;
6830 +       si_noflush_read_lock(sb);
6831 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6832 +       si_read_unlock(sb);
6833 +       return err;
6834 +}
6835 +
6836 +static const struct file_operations dbgaufs_xigen_fop = {
6837 +       .owner          = THIS_MODULE,
6838 +       .open           = dbgaufs_xigen_open,
6839 +       .release        = dbgaufs_xi_release,
6840 +       .read           = dbgaufs_xi_read
6841 +};
6842 +
6843 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6844 +{
6845 +       int err;
6846 +
6847 +       /*
6848 +        * This function is a dynamic '__init' function actually,
6849 +        * so the tiny check for si_rwsem is unnecessary.
6850 +        */
6851 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6852 +
6853 +       err = -EIO;
6854 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6855 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6856 +                &dbgaufs_xigen_fop);
6857 +       if (sbinfo->si_dbgaufs_xigen)
6858 +               err = 0;
6859 +
6860 +       return err;
6861 +}
6862 +#else
6863 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6864 +{
6865 +       return 0;
6866 +}
6867 +#endif /* CONFIG_AUFS_EXPORT */
6868 +
6869 +/* ---------------------------------------------------------------------- */
6870 +
6871 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6872 +{
6873 +       /*
6874 +        * This function is a dynamic '__fin' function actually,
6875 +        * so the tiny check for si_rwsem is unnecessary.
6876 +        */
6877 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6878 +
6879 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6880 +       sbinfo->si_dbgaufs = NULL;
6881 +}
6882 +
6883 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6884 +{
6885 +       int err;
6886 +       char name[SysaufsSiNameLen];
6887 +
6888 +       /*
6889 +        * This function is a dynamic '__init' function actually,
6890 +        * so the tiny check for si_rwsem is unnecessary.
6891 +        */
6892 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6893 +
6894 +       err = -ENOENT;
6895 +       if (!dbgaufs) {
6896 +               AuErr1("/debug/aufs is uninitialized\n");
6897 +               goto out;
6898 +       }
6899 +
6900 +       err = -EIO;
6901 +       sysaufs_name(sbinfo, name);
6902 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6903 +       if (unlikely(!sbinfo->si_dbgaufs))
6904 +               goto out;
6905 +
6906 +       /* regardless plink/noplink option */
6907 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6908 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6909 +                &dbgaufs_plink_fop);
6910 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6911 +               goto out_dir;
6912 +
6913 +       /* regardless xino/noxino option */
6914 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6915 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6916 +                &dbgaufs_xib_fop);
6917 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6918 +               goto out_dir;
6919 +
6920 +       err = dbgaufs_xigen_init(sbinfo);
6921 +       if (!err)
6922 +               goto out; /* success */
6923 +
6924 +out_dir:
6925 +       dbgaufs_si_fin(sbinfo);
6926 +out:
6927 +       if (unlikely(err))
6928 +               pr_err("debugfs/aufs failed\n");
6929 +       return err;
6930 +}
6931 +
6932 +/* ---------------------------------------------------------------------- */
6933 +
6934 +void dbgaufs_fin(void)
6935 +{
6936 +       debugfs_remove(dbgaufs);
6937 +}
6938 +
6939 +int __init dbgaufs_init(void)
6940 +{
6941 +       int err;
6942 +
6943 +       err = -EIO;
6944 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6945 +       if (dbgaufs)
6946 +               err = 0;
6947 +       return err;
6948 +}
6949 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6950 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6951 +++ linux/fs/aufs/dbgaufs.h     2020-01-27 10:57:18.168871450 +0100
6952 @@ -0,0 +1,53 @@
6953 +/* SPDX-License-Identifier: GPL-2.0 */
6954 +/*
6955 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6956 + *
6957 + * This program, aufs is free software; you can redistribute it and/or modify
6958 + * it under the terms of the GNU General Public License as published by
6959 + * the Free Software Foundation; either version 2 of the License, or
6960 + * (at your option) any later version.
6961 + *
6962 + * This program is distributed in the hope that it will be useful,
6963 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6964 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6965 + * GNU General Public License for more details.
6966 + *
6967 + * You should have received a copy of the GNU General Public License
6968 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6969 + */
6970 +
6971 +/*
6972 + * debugfs interface
6973 + */
6974 +
6975 +#ifndef __DBGAUFS_H__
6976 +#define __DBGAUFS_H__
6977 +
6978 +#ifdef __KERNEL__
6979 +
6980 +struct super_block;
6981 +struct au_sbinfo;
6982 +struct au_branch;
6983 +
6984 +#ifdef CONFIG_DEBUG_FS
6985 +/* dbgaufs.c */
6986 +void dbgaufs_xino_del(struct au_branch *br);
6987 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6988 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6989 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6990 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6991 +void dbgaufs_fin(void);
6992 +int __init dbgaufs_init(void);
6993 +#else
6994 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6995 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6996 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6997 +          int topdown)
6998 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6999 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
7000 +AuStubVoid(dbgaufs_fin, void)
7001 +AuStubInt0(__init dbgaufs_init, void)
7002 +#endif /* CONFIG_DEBUG_FS */
7003 +
7004 +#endif /* __KERNEL__ */
7005 +#endif /* __DBGAUFS_H__ */
7006 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
7007 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
7008 +++ linux/fs/aufs/dcsub.c       2020-01-27 10:57:18.168871450 +0100
7009 @@ -0,0 +1,225 @@
7010 +// SPDX-License-Identifier: GPL-2.0
7011 +/*
7012 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7013 + *
7014 + * This program, aufs is free software; you can redistribute it and/or modify
7015 + * it under the terms of the GNU General Public License as published by
7016 + * the Free Software Foundation; either version 2 of the License, or
7017 + * (at your option) any later version.
7018 + *
7019 + * This program is distributed in the hope that it will be useful,
7020 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7021 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7022 + * GNU General Public License for more details.
7023 + *
7024 + * You should have received a copy of the GNU General Public License
7025 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7026 + */
7027 +
7028 +/*
7029 + * sub-routines for dentry cache
7030 + */
7031 +
7032 +#include "aufs.h"
7033 +
7034 +static void au_dpage_free(struct au_dpage *dpage)
7035 +{
7036 +       int i;
7037 +       struct dentry **p;
7038 +
7039 +       p = dpage->dentries;
7040 +       for (i = 0; i < dpage->ndentry; i++)
7041 +               dput(*p++);
7042 +       free_page((unsigned long)dpage->dentries);
7043 +}
7044 +
7045 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
7046 +{
7047 +       int err;
7048 +       void *p;
7049 +
7050 +       err = -ENOMEM;
7051 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
7052 +       if (unlikely(!dpages->dpages))
7053 +               goto out;
7054 +
7055 +       p = (void *)__get_free_page(gfp);
7056 +       if (unlikely(!p))
7057 +               goto out_dpages;
7058 +
7059 +       dpages->dpages[0].ndentry = 0;
7060 +       dpages->dpages[0].dentries = p;
7061 +       dpages->ndpage = 1;
7062 +       return 0; /* success */
7063 +
7064 +out_dpages:
7065 +       au_kfree_try_rcu(dpages->dpages);
7066 +out:
7067 +       return err;
7068 +}
7069 +
7070 +void au_dpages_free(struct au_dcsub_pages *dpages)
7071 +{
7072 +       int i;
7073 +       struct au_dpage *p;
7074 +
7075 +       p = dpages->dpages;
7076 +       for (i = 0; i < dpages->ndpage; i++)
7077 +               au_dpage_free(p++);
7078 +       au_kfree_try_rcu(dpages->dpages);
7079 +}
7080 +
7081 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7082 +                           struct dentry *dentry, gfp_t gfp)
7083 +{
7084 +       int err, sz;
7085 +       struct au_dpage *dpage;
7086 +       void *p;
7087 +
7088 +       dpage = dpages->dpages + dpages->ndpage - 1;
7089 +       sz = PAGE_SIZE / sizeof(dentry);
7090 +       if (unlikely(dpage->ndentry >= sz)) {
7091 +               AuLabel(new dpage);
7092 +               err = -ENOMEM;
7093 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7094 +               p = au_kzrealloc(dpages->dpages, sz,
7095 +                                sz + sizeof(*dpages->dpages), gfp,
7096 +                                /*may_shrink*/0);
7097 +               if (unlikely(!p))
7098 +                       goto out;
7099 +
7100 +               dpages->dpages = p;
7101 +               dpage = dpages->dpages + dpages->ndpage;
7102 +               p = (void *)__get_free_page(gfp);
7103 +               if (unlikely(!p))
7104 +                       goto out;
7105 +
7106 +               dpage->ndentry = 0;
7107 +               dpage->dentries = p;
7108 +               dpages->ndpage++;
7109 +       }
7110 +
7111 +       AuDebugOn(au_dcount(dentry) <= 0);
7112 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7113 +       return 0; /* success */
7114 +
7115 +out:
7116 +       return err;
7117 +}
7118 +
7119 +/* todo: BAD approach */
7120 +/* copied from linux/fs/dcache.c */
7121 +enum d_walk_ret {
7122 +       D_WALK_CONTINUE,
7123 +       D_WALK_QUIT,
7124 +       D_WALK_NORETRY,
7125 +       D_WALK_SKIP,
7126 +};
7127 +
7128 +extern void d_walk(struct dentry *parent, void *data,
7129 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7130 +
7131 +struct ac_dpages_arg {
7132 +       int err;
7133 +       struct au_dcsub_pages *dpages;
7134 +       struct super_block *sb;
7135 +       au_dpages_test test;
7136 +       void *arg;
7137 +};
7138 +
7139 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7140 +{
7141 +       enum d_walk_ret ret;
7142 +       struct ac_dpages_arg *arg = _arg;
7143 +
7144 +       ret = D_WALK_CONTINUE;
7145 +       if (dentry->d_sb == arg->sb
7146 +           && !IS_ROOT(dentry)
7147 +           && au_dcount(dentry) > 0
7148 +           && au_di(dentry)
7149 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7150 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7151 +               if (unlikely(arg->err))
7152 +                       ret = D_WALK_QUIT;
7153 +       }
7154 +
7155 +       return ret;
7156 +}
7157 +
7158 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7159 +                  au_dpages_test test, void *arg)
7160 +{
7161 +       struct ac_dpages_arg args = {
7162 +               .err    = 0,
7163 +               .dpages = dpages,
7164 +               .sb     = root->d_sb,
7165 +               .test   = test,
7166 +               .arg    = arg
7167 +       };
7168 +
7169 +       d_walk(root, &args, au_call_dpages_append);
7170 +
7171 +       return args.err;
7172 +}
7173 +
7174 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7175 +                      int do_include, au_dpages_test test, void *arg)
7176 +{
7177 +       int err;
7178 +
7179 +       err = 0;
7180 +       write_seqlock(&rename_lock);
7181 +       spin_lock(&dentry->d_lock);
7182 +       if (do_include
7183 +           && au_dcount(dentry) > 0
7184 +           && (!test || test(dentry, arg)))
7185 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7186 +       spin_unlock(&dentry->d_lock);
7187 +       if (unlikely(err))
7188 +               goto out;
7189 +
7190 +       /*
7191 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7192 +        * mount
7193 +        */
7194 +       while (!IS_ROOT(dentry)) {
7195 +               dentry = dentry->d_parent; /* rename_lock is locked */
7196 +               spin_lock(&dentry->d_lock);
7197 +               if (au_dcount(dentry) > 0
7198 +                   && (!test || test(dentry, arg)))
7199 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7200 +               spin_unlock(&dentry->d_lock);
7201 +               if (unlikely(err))
7202 +                       break;
7203 +       }
7204 +
7205 +out:
7206 +       write_sequnlock(&rename_lock);
7207 +       return err;
7208 +}
7209 +
7210 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7211 +{
7212 +       return au_di(dentry) && dentry->d_sb == arg;
7213 +}
7214 +
7215 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7216 +                           struct dentry *dentry, int do_include)
7217 +{
7218 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7219 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7220 +}
7221 +
7222 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7223 +{
7224 +       struct path path[2] = {
7225 +               {
7226 +                       .dentry = d1
7227 +               },
7228 +               {
7229 +                       .dentry = d2
7230 +               }
7231 +       };
7232 +
7233 +       return path_is_under(path + 0, path + 1);
7234 +}
7235 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7236 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7237 +++ linux/fs/aufs/dcsub.h       2020-01-27 10:57:18.168871450 +0100
7238 @@ -0,0 +1,137 @@
7239 +/* SPDX-License-Identifier: GPL-2.0 */
7240 +/*
7241 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7242 + *
7243 + * This program, aufs is free software; you can redistribute it and/or modify
7244 + * it under the terms of the GNU General Public License as published by
7245 + * the Free Software Foundation; either version 2 of the License, or
7246 + * (at your option) any later version.
7247 + *
7248 + * This program is distributed in the hope that it will be useful,
7249 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7250 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7251 + * GNU General Public License for more details.
7252 + *
7253 + * You should have received a copy of the GNU General Public License
7254 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7255 + */
7256 +
7257 +/*
7258 + * sub-routines for dentry cache
7259 + */
7260 +
7261 +#ifndef __AUFS_DCSUB_H__
7262 +#define __AUFS_DCSUB_H__
7263 +
7264 +#ifdef __KERNEL__
7265 +
7266 +#include <linux/dcache.h>
7267 +#include <linux/fs.h>
7268 +
7269 +struct au_dpage {
7270 +       int ndentry;
7271 +       struct dentry **dentries;
7272 +};
7273 +
7274 +struct au_dcsub_pages {
7275 +       int ndpage;
7276 +       struct au_dpage *dpages;
7277 +};
7278 +
7279 +/* ---------------------------------------------------------------------- */
7280 +
7281 +/* dcsub.c */
7282 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7283 +void au_dpages_free(struct au_dcsub_pages *dpages);
7284 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7285 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7286 +                  au_dpages_test test, void *arg);
7287 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7288 +                      int do_include, au_dpages_test test, void *arg);
7289 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7290 +                           struct dentry *dentry, int do_include);
7291 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7292 +
7293 +/* ---------------------------------------------------------------------- */
7294 +
7295 +/*
7296 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7297 + * include/linux/dcache.h. Try them (in the future).
7298 + */
7299 +
7300 +static inline int au_d_hashed_positive(struct dentry *d)
7301 +{
7302 +       int err;
7303 +       struct inode *inode = d_inode(d);
7304 +
7305 +       err = 0;
7306 +       if (unlikely(d_unhashed(d)
7307 +                    || d_is_negative(d)
7308 +                    || !inode->i_nlink))
7309 +               err = -ENOENT;
7310 +       return err;
7311 +}
7312 +
7313 +static inline int au_d_linkable(struct dentry *d)
7314 +{
7315 +       int err;
7316 +       struct inode *inode = d_inode(d);
7317 +
7318 +       err = au_d_hashed_positive(d);
7319 +       if (err
7320 +           && d_is_positive(d)
7321 +           && (inode->i_state & I_LINKABLE))
7322 +               err = 0;
7323 +       return err;
7324 +}
7325 +
7326 +static inline int au_d_alive(struct dentry *d)
7327 +{
7328 +       int err;
7329 +       struct inode *inode;
7330 +
7331 +       err = 0;
7332 +       if (!IS_ROOT(d))
7333 +               err = au_d_hashed_positive(d);
7334 +       else {
7335 +               inode = d_inode(d);
7336 +               if (unlikely(d_unlinked(d)
7337 +                            || d_is_negative(d)
7338 +                            || !inode->i_nlink))
7339 +                       err = -ENOENT;
7340 +       }
7341 +       return err;
7342 +}
7343 +
7344 +static inline int au_alive_dir(struct dentry *d)
7345 +{
7346 +       int err;
7347 +
7348 +       err = au_d_alive(d);
7349 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7350 +               err = -ENOENT;
7351 +       return err;
7352 +}
7353 +
7354 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7355 +{
7356 +       return a->len == b->len
7357 +               && !memcmp(a->name, b->name, a->len);
7358 +}
7359 +
7360 +/*
7361 + * by the commit
7362 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7363 + *                     taking d_lock
7364 + * the type of d_lockref.count became int, but the inlined function d_count()
7365 + * still returns unsigned int.
7366 + * I don't know why. Maybe it is for every d_count() users?
7367 + * Anyway au_dcount() lives on.
7368 + */
7369 +static inline int au_dcount(struct dentry *d)
7370 +{
7371 +       return (int)d_count(d);
7372 +}
7373 +
7374 +#endif /* __KERNEL__ */
7375 +#endif /* __AUFS_DCSUB_H__ */
7376 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7377 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7378 +++ linux/fs/aufs/debug.c       2020-01-27 10:57:18.168871450 +0100
7379 @@ -0,0 +1,441 @@
7380 +// SPDX-License-Identifier: GPL-2.0
7381 +/*
7382 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7383 + *
7384 + * This program, aufs is free software; you can redistribute it and/or modify
7385 + * it under the terms of the GNU General Public License as published by
7386 + * the Free Software Foundation; either version 2 of the License, or
7387 + * (at your option) any later version.
7388 + *
7389 + * This program is distributed in the hope that it will be useful,
7390 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7391 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7392 + * GNU General Public License for more details.
7393 + *
7394 + * You should have received a copy of the GNU General Public License
7395 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7396 + */
7397 +
7398 +/*
7399 + * debug print functions
7400 + */
7401 +
7402 +#include <linux/iversion.h>
7403 +#include "aufs.h"
7404 +
7405 +/* Returns 0, or -errno.  arg is in kp->arg. */
7406 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7407 +{
7408 +       int err, n;
7409 +
7410 +       err = kstrtoint(val, 0, &n);
7411 +       if (!err) {
7412 +               if (n > 0)
7413 +                       au_debug_on();
7414 +               else
7415 +                       au_debug_off();
7416 +       }
7417 +       return err;
7418 +}
7419 +
7420 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7421 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7422 +{
7423 +       atomic_t *a;
7424 +
7425 +       a = kp->arg;
7426 +       return sprintf(buffer, "%d", atomic_read(a));
7427 +}
7428 +
7429 +static struct kernel_param_ops param_ops_atomic_t = {
7430 +       .set = param_atomic_t_set,
7431 +       .get = param_atomic_t_get
7432 +       /* void (*free)(void *arg) */
7433 +};
7434 +
7435 +atomic_t aufs_debug = ATOMIC_INIT(0);
7436 +MODULE_PARM_DESC(debug, "debug print");
7437 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7438 +
7439 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7440 +char *au_plevel = KERN_DEBUG;
7441 +#define dpri(fmt, ...) do {                                    \
7442 +       if ((au_plevel                                          \
7443 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7444 +           || au_debug_test())                                 \
7445 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7446 +} while (0)
7447 +
7448 +/* ---------------------------------------------------------------------- */
7449 +
7450 +void au_dpri_whlist(struct au_nhash *whlist)
7451 +{
7452 +       unsigned long ul, n;
7453 +       struct hlist_head *head;
7454 +       struct au_vdir_wh *pos;
7455 +
7456 +       n = whlist->nh_num;
7457 +       head = whlist->nh_head;
7458 +       for (ul = 0; ul < n; ul++) {
7459 +               hlist_for_each_entry(pos, head, wh_hash)
7460 +                       dpri("b%d, %.*s, %d\n",
7461 +                            pos->wh_bindex,
7462 +                            pos->wh_str.len, pos->wh_str.name,
7463 +                            pos->wh_str.len);
7464 +               head++;
7465 +       }
7466 +}
7467 +
7468 +void au_dpri_vdir(struct au_vdir *vdir)
7469 +{
7470 +       unsigned long ul;
7471 +       union au_vdir_deblk_p p;
7472 +       unsigned char *o;
7473 +
7474 +       if (!vdir || IS_ERR(vdir)) {
7475 +               dpri("err %ld\n", PTR_ERR(vdir));
7476 +               return;
7477 +       }
7478 +
7479 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7480 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7481 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7482 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7483 +               p.deblk = vdir->vd_deblk[ul];
7484 +               o = p.deblk;
7485 +               dpri("[%lu]: %p\n", ul, o);
7486 +       }
7487 +}
7488 +
7489 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7490 +                       struct dentry *wh)
7491 +{
7492 +       char *n = NULL;
7493 +       int l = 0;
7494 +
7495 +       if (!inode || IS_ERR(inode)) {
7496 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7497 +               return -1;
7498 +       }
7499 +
7500 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7501 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7502 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7503 +       if (wh) {
7504 +               n = (void *)wh->d_name.name;
7505 +               l = wh->d_name.len;
7506 +       }
7507 +
7508 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7509 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7510 +            bindex, inode,
7511 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7512 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7513 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7514 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7515 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7516 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7517 +            inode->i_generation,
7518 +            l ? ", wh " : "", l, n);
7519 +       return 0;
7520 +}
7521 +
7522 +void au_dpri_inode(struct inode *inode)
7523 +{
7524 +       struct au_iinfo *iinfo;
7525 +       struct au_hinode *hi;
7526 +       aufs_bindex_t bindex;
7527 +       int err, hn;
7528 +
7529 +       err = do_pri_inode(-1, inode, -1, NULL);
7530 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7531 +               return;
7532 +
7533 +       iinfo = au_ii(inode);
7534 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7535 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7536 +       if (iinfo->ii_btop < 0)
7537 +               return;
7538 +       hn = 0;
7539 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7540 +               hi = au_hinode(iinfo, bindex);
7541 +               hn = !!au_hn(hi);
7542 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7543 +       }
7544 +}
7545 +
7546 +void au_dpri_dalias(struct inode *inode)
7547 +{
7548 +       struct dentry *d;
7549 +
7550 +       spin_lock(&inode->i_lock);
7551 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7552 +               au_dpri_dentry(d);
7553 +       spin_unlock(&inode->i_lock);
7554 +}
7555 +
7556 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7557 +{
7558 +       struct dentry *wh = NULL;
7559 +       int hn;
7560 +       struct inode *inode;
7561 +       struct au_iinfo *iinfo;
7562 +       struct au_hinode *hi;
7563 +
7564 +       if (!dentry || IS_ERR(dentry)) {
7565 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7566 +               return -1;
7567 +       }
7568 +       /* do not call dget_parent() here */
7569 +       /* note: access d_xxx without d_lock */
7570 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7571 +            bindex, dentry, dentry,
7572 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7573 +            au_dcount(dentry), dentry->d_flags,
7574 +            d_unhashed(dentry) ? "un" : "");
7575 +       hn = -1;
7576 +       inode = NULL;
7577 +       if (d_is_positive(dentry))
7578 +               inode = d_inode(dentry);
7579 +       if (inode
7580 +           && au_test_aufs(dentry->d_sb)
7581 +           && bindex >= 0
7582 +           && !au_is_bad_inode(inode)) {
7583 +               iinfo = au_ii(inode);
7584 +               hi = au_hinode(iinfo, bindex);
7585 +               hn = !!au_hn(hi);
7586 +               wh = hi->hi_whdentry;
7587 +       }
7588 +       do_pri_inode(bindex, inode, hn, wh);
7589 +       return 0;
7590 +}
7591 +
7592 +void au_dpri_dentry(struct dentry *dentry)
7593 +{
7594 +       struct au_dinfo *dinfo;
7595 +       aufs_bindex_t bindex;
7596 +       int err;
7597 +
7598 +       err = do_pri_dentry(-1, dentry);
7599 +       if (err || !au_test_aufs(dentry->d_sb))
7600 +               return;
7601 +
7602 +       dinfo = au_di(dentry);
7603 +       if (!dinfo)
7604 +               return;
7605 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7606 +            dinfo->di_btop, dinfo->di_bbot,
7607 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7608 +            dinfo->di_tmpfile);
7609 +       if (dinfo->di_btop < 0)
7610 +               return;
7611 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7612 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7613 +}
7614 +
7615 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7616 +{
7617 +       char a[32];
7618 +
7619 +       if (!file || IS_ERR(file)) {
7620 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7621 +               return -1;
7622 +       }
7623 +       a[0] = 0;
7624 +       if (bindex < 0
7625 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7626 +           && au_test_aufs(file->f_path.dentry->d_sb)
7627 +           && au_fi(file))
7628 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7629 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7630 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7631 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7632 +            file->f_version, file->f_pos, a);
7633 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7634 +               do_pri_dentry(bindex, file->f_path.dentry);
7635 +       return 0;
7636 +}
7637 +
7638 +void au_dpri_file(struct file *file)
7639 +{
7640 +       struct au_finfo *finfo;
7641 +       struct au_fidir *fidir;
7642 +       struct au_hfile *hfile;
7643 +       aufs_bindex_t bindex;
7644 +       int err;
7645 +
7646 +       err = do_pri_file(-1, file);
7647 +       if (err
7648 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7649 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7650 +               return;
7651 +
7652 +       finfo = au_fi(file);
7653 +       if (!finfo)
7654 +               return;
7655 +       if (finfo->fi_btop < 0)
7656 +               return;
7657 +       fidir = finfo->fi_hdir;
7658 +       if (!fidir)
7659 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7660 +       else
7661 +               for (bindex = finfo->fi_btop;
7662 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7663 +                    bindex++) {
7664 +                       hfile = fidir->fd_hfile + bindex;
7665 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7666 +               }
7667 +}
7668 +
7669 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7670 +{
7671 +       struct vfsmount *mnt;
7672 +       struct super_block *sb;
7673 +
7674 +       if (!br || IS_ERR(br))
7675 +               goto out;
7676 +       mnt = au_br_mnt(br);
7677 +       if (!mnt || IS_ERR(mnt))
7678 +               goto out;
7679 +       sb = mnt->mnt_sb;
7680 +       if (!sb || IS_ERR(sb))
7681 +               goto out;
7682 +
7683 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7684 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7685 +            "xino %d\n",
7686 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7687 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7688 +            sb->s_flags, sb->s_count,
7689 +            atomic_read(&sb->s_active),
7690 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7691 +       return 0;
7692 +
7693 +out:
7694 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7695 +       return -1;
7696 +}
7697 +
7698 +void au_dpri_sb(struct super_block *sb)
7699 +{
7700 +       struct au_sbinfo *sbinfo;
7701 +       aufs_bindex_t bindex;
7702 +       int err;
7703 +       /* to reduce stack size */
7704 +       struct {
7705 +               struct vfsmount mnt;
7706 +               struct au_branch fake;
7707 +       } *a;
7708 +
7709 +       /* this function can be called from magic sysrq */
7710 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7711 +       if (unlikely(!a)) {
7712 +               dpri("no memory\n");
7713 +               return;
7714 +       }
7715 +
7716 +       a->mnt.mnt_sb = sb;
7717 +       a->fake.br_path.mnt = &a->mnt;
7718 +       err = do_pri_br(-1, &a->fake);
7719 +       au_kfree_rcu(a);
7720 +       dpri("dev 0x%x\n", sb->s_dev);
7721 +       if (err || !au_test_aufs(sb))
7722 +               return;
7723 +
7724 +       sbinfo = au_sbi(sb);
7725 +       if (!sbinfo)
7726 +               return;
7727 +       dpri("nw %d, gen %u, kobj %d\n",
7728 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7729 +            kref_read(&sbinfo->si_kobj.kref));
7730 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7731 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7732 +}
7733 +
7734 +/* ---------------------------------------------------------------------- */
7735 +
7736 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7737 +{
7738 +       struct inode *h_inode, *inode = d_inode(dentry);
7739 +       struct dentry *h_dentry;
7740 +       aufs_bindex_t bindex, bbot, bi;
7741 +
7742 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7743 +               return;
7744 +
7745 +       bbot = au_dbbot(dentry);
7746 +       bi = au_ibbot(inode);
7747 +       if (bi < bbot)
7748 +               bbot = bi;
7749 +       bindex = au_dbtop(dentry);
7750 +       bi = au_ibtop(inode);
7751 +       if (bi > bindex)
7752 +               bindex = bi;
7753 +
7754 +       for (; bindex <= bbot; bindex++) {
7755 +               h_dentry = au_h_dptr(dentry, bindex);
7756 +               if (!h_dentry)
7757 +                       continue;
7758 +               h_inode = au_h_iptr(inode, bindex);
7759 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7760 +                       au_debug_on();
7761 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7762 +                       AuDbgDentry(dentry);
7763 +                       AuDbgInode(inode);
7764 +                       au_debug_off();
7765 +                       BUG();
7766 +               }
7767 +       }
7768 +}
7769 +
7770 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7771 +{
7772 +       int err, i, j;
7773 +       struct au_dcsub_pages dpages;
7774 +       struct au_dpage *dpage;
7775 +       struct dentry **dentries;
7776 +
7777 +       err = au_dpages_init(&dpages, GFP_NOFS);
7778 +       AuDebugOn(err);
7779 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7780 +       AuDebugOn(err);
7781 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7782 +               dpage = dpages.dpages + i;
7783 +               dentries = dpage->dentries;
7784 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7785 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7786 +       }
7787 +       au_dpages_free(&dpages);
7788 +}
7789 +
7790 +void au_dbg_verify_kthread(void)
7791 +{
7792 +       if (au_wkq_test()) {
7793 +               au_dbg_blocked();
7794 +               /*
7795 +                * It may be recursive, but udba=notify between two aufs mounts,
7796 +                * where a single ro branch is shared, is not a problem.
7797 +                */
7798 +               /* WARN_ON(1); */
7799 +       }
7800 +}
7801 +
7802 +/* ---------------------------------------------------------------------- */
7803 +
7804 +int __init au_debug_init(void)
7805 +{
7806 +       aufs_bindex_t bindex;
7807 +       struct au_vdir_destr destr;
7808 +
7809 +       bindex = -1;
7810 +       AuDebugOn(bindex >= 0);
7811 +
7812 +       destr.len = -1;
7813 +       AuDebugOn(destr.len < NAME_MAX);
7814 +
7815 +#ifdef CONFIG_4KSTACKS
7816 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7817 +#endif
7818 +
7819 +       return 0;
7820 +}
7821 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7822 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7823 +++ linux/fs/aufs/debug.h       2020-01-27 10:57:18.168871450 +0100
7824 @@ -0,0 +1,226 @@
7825 +/* SPDX-License-Identifier: GPL-2.0 */
7826 +/*
7827 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7828 + *
7829 + * This program, aufs is free software; you can redistribute it and/or modify
7830 + * it under the terms of the GNU General Public License as published by
7831 + * the Free Software Foundation; either version 2 of the License, or
7832 + * (at your option) any later version.
7833 + *
7834 + * This program is distributed in the hope that it will be useful,
7835 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7836 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7837 + * GNU General Public License for more details.
7838 + *
7839 + * You should have received a copy of the GNU General Public License
7840 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7841 + */
7842 +
7843 +/*
7844 + * debug print functions
7845 + */
7846 +
7847 +#ifndef __AUFS_DEBUG_H__
7848 +#define __AUFS_DEBUG_H__
7849 +
7850 +#ifdef __KERNEL__
7851 +
7852 +#include <linux/atomic.h>
7853 +#include <linux/module.h>
7854 +#include <linux/kallsyms.h>
7855 +#include <linux/sysrq.h>
7856 +
7857 +#ifdef CONFIG_AUFS_DEBUG
7858 +#define AuDebugOn(a)           BUG_ON(a)
7859 +
7860 +/* module parameter */
7861 +extern atomic_t aufs_debug;
7862 +static inline void au_debug_on(void)
7863 +{
7864 +       atomic_inc(&aufs_debug);
7865 +}
7866 +static inline void au_debug_off(void)
7867 +{
7868 +       atomic_dec_if_positive(&aufs_debug);
7869 +}
7870 +
7871 +static inline int au_debug_test(void)
7872 +{
7873 +       return atomic_read(&aufs_debug) > 0;
7874 +}
7875 +#else
7876 +#define AuDebugOn(a)           do {} while (0)
7877 +AuStubVoid(au_debug_on, void)
7878 +AuStubVoid(au_debug_off, void)
7879 +AuStubInt0(au_debug_test, void)
7880 +#endif /* CONFIG_AUFS_DEBUG */
7881 +
7882 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7883 +
7884 +/* ---------------------------------------------------------------------- */
7885 +
7886 +/* debug print */
7887 +
7888 +#define AuDbg(fmt, ...) do { \
7889 +       if (au_debug_test()) \
7890 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7891 +} while (0)
7892 +#define AuLabel(l)             AuDbg(#l "\n")
7893 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7894 +#define AuWarn1(fmt, ...) do { \
7895 +       static unsigned char _c; \
7896 +       if (!_c++) \
7897 +               pr_warn(fmt, ##__VA_ARGS__); \
7898 +} while (0)
7899 +
7900 +#define AuErr1(fmt, ...) do { \
7901 +       static unsigned char _c; \
7902 +       if (!_c++) \
7903 +               pr_err(fmt, ##__VA_ARGS__); \
7904 +} while (0)
7905 +
7906 +#define AuIOErr1(fmt, ...) do { \
7907 +       static unsigned char _c; \
7908 +       if (!_c++) \
7909 +               AuIOErr(fmt, ##__VA_ARGS__); \
7910 +} while (0)
7911 +
7912 +#define AuUnsupportMsg "This operation is not supported." \
7913 +                       " Please report this application to aufs-users ML."
7914 +#define AuUnsupport(fmt, ...) do { \
7915 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7916 +       dump_stack(); \
7917 +} while (0)
7918 +
7919 +#define AuTraceErr(e) do { \
7920 +       if (unlikely((e) < 0)) \
7921 +               AuDbg("err %d\n", (int)(e)); \
7922 +} while (0)
7923 +
7924 +#define AuTraceErrPtr(p) do { \
7925 +       if (IS_ERR(p)) \
7926 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7927 +} while (0)
7928 +
7929 +/* dirty macros for debug print, use with "%.*s" and caution */
7930 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7931 +
7932 +/* ---------------------------------------------------------------------- */
7933 +
7934 +struct dentry;
7935 +#ifdef CONFIG_AUFS_DEBUG
7936 +extern struct mutex au_dbg_mtx;
7937 +extern char *au_plevel;
7938 +struct au_nhash;
7939 +void au_dpri_whlist(struct au_nhash *whlist);
7940 +struct au_vdir;
7941 +void au_dpri_vdir(struct au_vdir *vdir);
7942 +struct inode;
7943 +void au_dpri_inode(struct inode *inode);
7944 +void au_dpri_dalias(struct inode *inode);
7945 +void au_dpri_dentry(struct dentry *dentry);
7946 +struct file;
7947 +void au_dpri_file(struct file *filp);
7948 +struct super_block;
7949 +void au_dpri_sb(struct super_block *sb);
7950 +
7951 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7952 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7953 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7954 +void au_dbg_verify_kthread(void);
7955 +
7956 +int __init au_debug_init(void);
7957 +
7958 +#define AuDbgWhlist(w) do { \
7959 +       mutex_lock(&au_dbg_mtx); \
7960 +       AuDbg(#w "\n"); \
7961 +       au_dpri_whlist(w); \
7962 +       mutex_unlock(&au_dbg_mtx); \
7963 +} while (0)
7964 +
7965 +#define AuDbgVdir(v) do { \
7966 +       mutex_lock(&au_dbg_mtx); \
7967 +       AuDbg(#v "\n"); \
7968 +       au_dpri_vdir(v); \
7969 +       mutex_unlock(&au_dbg_mtx); \
7970 +} while (0)
7971 +
7972 +#define AuDbgInode(i) do { \
7973 +       mutex_lock(&au_dbg_mtx); \
7974 +       AuDbg(#i "\n"); \
7975 +       au_dpri_inode(i); \
7976 +       mutex_unlock(&au_dbg_mtx); \
7977 +} while (0)
7978 +
7979 +#define AuDbgDAlias(i) do { \
7980 +       mutex_lock(&au_dbg_mtx); \
7981 +       AuDbg(#i "\n"); \
7982 +       au_dpri_dalias(i); \
7983 +       mutex_unlock(&au_dbg_mtx); \
7984 +} while (0)
7985 +
7986 +#define AuDbgDentry(d) do { \
7987 +       mutex_lock(&au_dbg_mtx); \
7988 +       AuDbg(#d "\n"); \
7989 +       au_dpri_dentry(d); \
7990 +       mutex_unlock(&au_dbg_mtx); \
7991 +} while (0)
7992 +
7993 +#define AuDbgFile(f) do { \
7994 +       mutex_lock(&au_dbg_mtx); \
7995 +       AuDbg(#f "\n"); \
7996 +       au_dpri_file(f); \
7997 +       mutex_unlock(&au_dbg_mtx); \
7998 +} while (0)
7999 +
8000 +#define AuDbgSb(sb) do { \
8001 +       mutex_lock(&au_dbg_mtx); \
8002 +       AuDbg(#sb "\n"); \
8003 +       au_dpri_sb(sb); \
8004 +       mutex_unlock(&au_dbg_mtx); \
8005 +} while (0)
8006 +
8007 +#define AuDbgSym(addr) do {                            \
8008 +       char sym[KSYM_SYMBOL_LEN];                      \
8009 +       sprint_symbol(sym, (unsigned long)addr);        \
8010 +       AuDbg("%s\n", sym);                             \
8011 +} while (0)
8012 +#else
8013 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
8014 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
8015 +AuStubVoid(au_dbg_verify_kthread, void)
8016 +AuStubInt0(__init au_debug_init, void)
8017 +
8018 +#define AuDbgWhlist(w)         do {} while (0)
8019 +#define AuDbgVdir(v)           do {} while (0)
8020 +#define AuDbgInode(i)          do {} while (0)
8021 +#define AuDbgDAlias(i)         do {} while (0)
8022 +#define AuDbgDentry(d)         do {} while (0)
8023 +#define AuDbgFile(f)           do {} while (0)
8024 +#define AuDbgSb(sb)            do {} while (0)
8025 +#define AuDbgSym(addr)         do {} while (0)
8026 +#endif /* CONFIG_AUFS_DEBUG */
8027 +
8028 +/* ---------------------------------------------------------------------- */
8029 +
8030 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
8031 +int __init au_sysrq_init(void);
8032 +void au_sysrq_fin(void);
8033 +
8034 +#ifdef CONFIG_HW_CONSOLE
8035 +#define au_dbg_blocked() do { \
8036 +       WARN_ON(1); \
8037 +       handle_sysrq('w'); \
8038 +} while (0)
8039 +#else
8040 +AuStubVoid(au_dbg_blocked, void)
8041 +#endif
8042 +
8043 +#else
8044 +AuStubInt0(__init au_sysrq_init, void)
8045 +AuStubVoid(au_sysrq_fin, void)
8046 +AuStubVoid(au_dbg_blocked, void)
8047 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
8048 +
8049 +#endif /* __KERNEL__ */
8050 +#endif /* __AUFS_DEBUG_H__ */
8051 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
8052 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
8053 +++ linux/fs/aufs/dentry.c      2020-01-27 10:57:18.168871450 +0100
8054 @@ -0,0 +1,1154 @@
8055 +// SPDX-License-Identifier: GPL-2.0
8056 +/*
8057 + * Copyright (C) 2005-2020 Junjiro R. Okajima
8058 + *
8059 + * This program, aufs is free software; you can redistribute it and/or modify
8060 + * it under the terms of the GNU General Public License as published by
8061 + * the Free Software Foundation; either version 2 of the License, or
8062 + * (at your option) any later version.
8063 + *
8064 + * This program is distributed in the hope that it will be useful,
8065 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8066 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8067 + * GNU General Public License for more details.
8068 + *
8069 + * You should have received a copy of the GNU General Public License
8070 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8071 + */
8072 +
8073 +/*
8074 + * lookup and dentry operations
8075 + */
8076 +
8077 +#include <linux/iversion.h>
8078 +#include <linux/namei.h>
8079 +#include "aufs.h"
8080 +
8081 +/*
8082 + * returns positive/negative dentry, NULL or an error.
8083 + * NULL means whiteout-ed or not-found.
8084 + */
8085 +static struct dentry*
8086 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8087 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8088 +{
8089 +       struct dentry *h_dentry;
8090 +       struct inode *h_inode;
8091 +       struct au_branch *br;
8092 +       int wh_found, opq;
8093 +       unsigned char wh_able;
8094 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8095 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8096 +                                                         IGNORE_PERM);
8097 +
8098 +       wh_found = 0;
8099 +       br = au_sbr(dentry->d_sb, bindex);
8100 +       wh_able = !!au_br_whable(br->br_perm);
8101 +       if (wh_able)
8102 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8103 +       h_dentry = ERR_PTR(wh_found);
8104 +       if (!wh_found)
8105 +               goto real_lookup;
8106 +       if (unlikely(wh_found < 0))
8107 +               goto out;
8108 +
8109 +       /* We found a whiteout */
8110 +       /* au_set_dbbot(dentry, bindex); */
8111 +       au_set_dbwh(dentry, bindex);
8112 +       if (!allow_neg)
8113 +               return NULL; /* success */
8114 +
8115 +real_lookup:
8116 +       if (!ignore_perm)
8117 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8118 +       else
8119 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8120 +       if (IS_ERR(h_dentry)) {
8121 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8122 +                   && !allow_neg)
8123 +                       h_dentry = NULL;
8124 +               goto out;
8125 +       }
8126 +
8127 +       h_inode = d_inode(h_dentry);
8128 +       if (d_is_negative(h_dentry)) {
8129 +               if (!allow_neg)
8130 +                       goto out_neg;
8131 +       } else if (wh_found
8132 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8133 +               goto out_neg;
8134 +       else if (au_ftest_lkup(args->flags, DIRREN)
8135 +                /* && h_inode */
8136 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8137 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8138 +                     (unsigned long long)h_inode->i_ino);
8139 +               goto out_neg;
8140 +       }
8141 +
8142 +       if (au_dbbot(dentry) <= bindex)
8143 +               au_set_dbbot(dentry, bindex);
8144 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8145 +               au_set_dbtop(dentry, bindex);
8146 +       au_set_h_dptr(dentry, bindex, h_dentry);
8147 +
8148 +       if (!d_is_dir(h_dentry)
8149 +           || !wh_able
8150 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8151 +               goto out; /* success */
8152 +
8153 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8154 +       opq = au_diropq_test(h_dentry);
8155 +       inode_unlock_shared(h_inode);
8156 +       if (opq > 0)
8157 +               au_set_dbdiropq(dentry, bindex);
8158 +       else if (unlikely(opq < 0)) {
8159 +               au_set_h_dptr(dentry, bindex, NULL);
8160 +               h_dentry = ERR_PTR(opq);
8161 +       }
8162 +       goto out;
8163 +
8164 +out_neg:
8165 +       dput(h_dentry);
8166 +       h_dentry = NULL;
8167 +out:
8168 +       return h_dentry;
8169 +}
8170 +
8171 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8172 +{
8173 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8174 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8175 +               return -EPERM;
8176 +       return 0;
8177 +}
8178 +
8179 +/*
8180 + * returns the number of lower positive dentries,
8181 + * otherwise an error.
8182 + * can be called at unlinking with @type is zero.
8183 + */
8184 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8185 +                  unsigned int flags)
8186 +{
8187 +       int npositive, err;
8188 +       aufs_bindex_t bindex, btail, bdiropq;
8189 +       unsigned char isdir, dirperm1, dirren;
8190 +       struct au_do_lookup_args args = {
8191 +               .flags          = flags,
8192 +               .name           = &dentry->d_name
8193 +       };
8194 +       struct dentry *parent;
8195 +       struct super_block *sb;
8196 +
8197 +       sb = dentry->d_sb;
8198 +       err = au_test_shwh(sb, args.name);
8199 +       if (unlikely(err))
8200 +               goto out;
8201 +
8202 +       err = au_wh_name_alloc(&args.whname, args.name);
8203 +       if (unlikely(err))
8204 +               goto out;
8205 +
8206 +       isdir = !!d_is_dir(dentry);
8207 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8208 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8209 +       if (dirren)
8210 +               au_fset_lkup(args.flags, DIRREN);
8211 +
8212 +       npositive = 0;
8213 +       parent = dget_parent(dentry);
8214 +       btail = au_dbtaildir(parent);
8215 +       for (bindex = btop; bindex <= btail; bindex++) {
8216 +               struct dentry *h_parent, *h_dentry;
8217 +               struct inode *h_inode, *h_dir;
8218 +               struct au_branch *br;
8219 +
8220 +               h_dentry = au_h_dptr(dentry, bindex);
8221 +               if (h_dentry) {
8222 +                       if (d_is_positive(h_dentry))
8223 +                               npositive++;
8224 +                       break;
8225 +               }
8226 +               h_parent = au_h_dptr(parent, bindex);
8227 +               if (!h_parent || !d_is_dir(h_parent))
8228 +                       continue;
8229 +
8230 +               if (dirren) {
8231 +                       /* if the inum matches, then use the prepared name */
8232 +                       err = au_dr_lkup_name(&args, bindex);
8233 +                       if (unlikely(err))
8234 +                               goto out_parent;
8235 +               }
8236 +
8237 +               h_dir = d_inode(h_parent);
8238 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8239 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8240 +               inode_unlock_shared(h_dir);
8241 +               err = PTR_ERR(h_dentry);
8242 +               if (IS_ERR(h_dentry))
8243 +                       goto out_parent;
8244 +               if (h_dentry)
8245 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8246 +               if (dirperm1)
8247 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8248 +
8249 +               if (au_dbwh(dentry) == bindex)
8250 +                       break;
8251 +               if (!h_dentry)
8252 +                       continue;
8253 +               if (d_is_negative(h_dentry))
8254 +                       continue;
8255 +               h_inode = d_inode(h_dentry);
8256 +               npositive++;
8257 +               if (!args.type)
8258 +                       args.type = h_inode->i_mode & S_IFMT;
8259 +               if (args.type != S_IFDIR)
8260 +                       break;
8261 +               else if (isdir) {
8262 +                       /* the type of lower may be different */
8263 +                       bdiropq = au_dbdiropq(dentry);
8264 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8265 +                               break;
8266 +               }
8267 +               br = au_sbr(sb, bindex);
8268 +               if (dirren
8269 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8270 +                                          /*add_ent*/NULL)) {
8271 +                       /* prepare next name to lookup */
8272 +                       err = au_dr_lkup(&args, dentry, bindex);
8273 +                       if (unlikely(err))
8274 +                               goto out_parent;
8275 +               }
8276 +       }
8277 +
8278 +       if (npositive) {
8279 +               AuLabel(positive);
8280 +               au_update_dbtop(dentry);
8281 +       }
8282 +       err = npositive;
8283 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8284 +                    && au_dbtop(dentry) < 0)) {
8285 +               err = -EIO;
8286 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8287 +                       dentry, err);
8288 +       }
8289 +
8290 +out_parent:
8291 +       dput(parent);
8292 +       au_kfree_try_rcu(args.whname.name);
8293 +       if (dirren)
8294 +               au_dr_lkup_fin(&args);
8295 +out:
8296 +       return err;
8297 +}
8298 +
8299 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8300 +{
8301 +       struct dentry *dentry;
8302 +       int wkq_err;
8303 +
8304 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8305 +               dentry = vfsub_lkup_one(name, parent);
8306 +       else {
8307 +               struct vfsub_lkup_one_args args = {
8308 +                       .errp   = &dentry,
8309 +                       .name   = name,
8310 +                       .parent = parent
8311 +               };
8312 +
8313 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8314 +               if (unlikely(wkq_err))
8315 +                       dentry = ERR_PTR(wkq_err);
8316 +       }
8317 +
8318 +       return dentry;
8319 +}
8320 +
8321 +/*
8322 + * lookup @dentry on @bindex which should be negative.
8323 + */
8324 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8325 +{
8326 +       int err;
8327 +       struct dentry *parent, *h_parent, *h_dentry;
8328 +       struct au_branch *br;
8329 +
8330 +       parent = dget_parent(dentry);
8331 +       h_parent = au_h_dptr(parent, bindex);
8332 +       br = au_sbr(dentry->d_sb, bindex);
8333 +       if (wh)
8334 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8335 +       else
8336 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8337 +       err = PTR_ERR(h_dentry);
8338 +       if (IS_ERR(h_dentry))
8339 +               goto out;
8340 +       if (unlikely(d_is_positive(h_dentry))) {
8341 +               err = -EIO;
8342 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8343 +               dput(h_dentry);
8344 +               goto out;
8345 +       }
8346 +
8347 +       err = 0;
8348 +       if (bindex < au_dbtop(dentry))
8349 +               au_set_dbtop(dentry, bindex);
8350 +       if (au_dbbot(dentry) < bindex)
8351 +               au_set_dbbot(dentry, bindex);
8352 +       au_set_h_dptr(dentry, bindex, h_dentry);
8353 +
8354 +out:
8355 +       dput(parent);
8356 +       return err;
8357 +}
8358 +
8359 +/* ---------------------------------------------------------------------- */
8360 +
8361 +/* subset of struct inode */
8362 +struct au_iattr {
8363 +       unsigned long           i_ino;
8364 +       /* unsigned int         i_nlink; */
8365 +       kuid_t                  i_uid;
8366 +       kgid_t                  i_gid;
8367 +       u64                     i_version;
8368 +/*
8369 +       loff_t                  i_size;
8370 +       blkcnt_t                i_blocks;
8371 +*/
8372 +       umode_t                 i_mode;
8373 +};
8374 +
8375 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8376 +{
8377 +       ia->i_ino = h_inode->i_ino;
8378 +       /* ia->i_nlink = h_inode->i_nlink; */
8379 +       ia->i_uid = h_inode->i_uid;
8380 +       ia->i_gid = h_inode->i_gid;
8381 +       ia->i_version = inode_query_iversion(h_inode);
8382 +/*
8383 +       ia->i_size = h_inode->i_size;
8384 +       ia->i_blocks = h_inode->i_blocks;
8385 +*/
8386 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8387 +}
8388 +
8389 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8390 +{
8391 +       return ia->i_ino != h_inode->i_ino
8392 +               /* || ia->i_nlink != h_inode->i_nlink */
8393 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8394 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8395 +               || !inode_eq_iversion(h_inode, ia->i_version)
8396 +/*
8397 +               || ia->i_size != h_inode->i_size
8398 +               || ia->i_blocks != h_inode->i_blocks
8399 +*/
8400 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8401 +}
8402 +
8403 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8404 +                             struct au_branch *br)
8405 +{
8406 +       int err;
8407 +       struct au_iattr ia;
8408 +       struct inode *h_inode;
8409 +       struct dentry *h_d;
8410 +       struct super_block *h_sb;
8411 +
8412 +       err = 0;
8413 +       memset(&ia, -1, sizeof(ia));
8414 +       h_sb = h_dentry->d_sb;
8415 +       h_inode = NULL;
8416 +       if (d_is_positive(h_dentry)) {
8417 +               h_inode = d_inode(h_dentry);
8418 +               au_iattr_save(&ia, h_inode);
8419 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8420 +               /* nfs d_revalidate may return 0 for negative dentry */
8421 +               /* fuse d_revalidate always return 0 for negative dentry */
8422 +               goto out;
8423 +
8424 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8425 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8426 +       err = PTR_ERR(h_d);
8427 +       if (IS_ERR(h_d))
8428 +               goto out;
8429 +
8430 +       err = 0;
8431 +       if (unlikely(h_d != h_dentry
8432 +                    || d_inode(h_d) != h_inode
8433 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8434 +               err = au_busy_or_stale();
8435 +       dput(h_d);
8436 +
8437 +out:
8438 +       AuTraceErr(err);
8439 +       return err;
8440 +}
8441 +
8442 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8443 +               struct dentry *h_parent, struct au_branch *br)
8444 +{
8445 +       int err;
8446 +
8447 +       err = 0;
8448 +       if (udba == AuOpt_UDBA_REVAL
8449 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8450 +               IMustLock(h_dir);
8451 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8452 +       } else if (udba != AuOpt_UDBA_NONE)
8453 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8454 +
8455 +       return err;
8456 +}
8457 +
8458 +/* ---------------------------------------------------------------------- */
8459 +
8460 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8461 +{
8462 +       int err;
8463 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8464 +       struct au_hdentry tmp, *p, *q;
8465 +       struct au_dinfo *dinfo;
8466 +       struct super_block *sb;
8467 +
8468 +       DiMustWriteLock(dentry);
8469 +
8470 +       sb = dentry->d_sb;
8471 +       dinfo = au_di(dentry);
8472 +       bbot = dinfo->di_bbot;
8473 +       bwh = dinfo->di_bwh;
8474 +       bdiropq = dinfo->di_bdiropq;
8475 +       bindex = dinfo->di_btop;
8476 +       p = au_hdentry(dinfo, bindex);
8477 +       for (; bindex <= bbot; bindex++, p++) {
8478 +               if (!p->hd_dentry)
8479 +                       continue;
8480 +
8481 +               new_bindex = au_br_index(sb, p->hd_id);
8482 +               if (new_bindex == bindex)
8483 +                       continue;
8484 +
8485 +               if (dinfo->di_bwh == bindex)
8486 +                       bwh = new_bindex;
8487 +               if (dinfo->di_bdiropq == bindex)
8488 +                       bdiropq = new_bindex;
8489 +               if (new_bindex < 0) {
8490 +                       au_hdput(p);
8491 +                       p->hd_dentry = NULL;
8492 +                       continue;
8493 +               }
8494 +
8495 +               /* swap two lower dentries, and loop again */
8496 +               q = au_hdentry(dinfo, new_bindex);
8497 +               tmp = *q;
8498 +               *q = *p;
8499 +               *p = tmp;
8500 +               if (tmp.hd_dentry) {
8501 +                       bindex--;
8502 +                       p--;
8503 +               }
8504 +       }
8505 +
8506 +       dinfo->di_bwh = -1;
8507 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8508 +               dinfo->di_bwh = bwh;
8509 +
8510 +       dinfo->di_bdiropq = -1;
8511 +       if (bdiropq >= 0
8512 +           && bdiropq <= au_sbbot(sb)
8513 +           && au_sbr_whable(sb, bdiropq))
8514 +               dinfo->di_bdiropq = bdiropq;
8515 +
8516 +       err = -EIO;
8517 +       dinfo->di_btop = -1;
8518 +       dinfo->di_bbot = -1;
8519 +       bbot = au_dbbot(parent);
8520 +       bindex = 0;
8521 +       p = au_hdentry(dinfo, bindex);
8522 +       for (; bindex <= bbot; bindex++, p++)
8523 +               if (p->hd_dentry) {
8524 +                       dinfo->di_btop = bindex;
8525 +                       break;
8526 +               }
8527 +
8528 +       if (dinfo->di_btop >= 0) {
8529 +               bindex = bbot;
8530 +               p = au_hdentry(dinfo, bindex);
8531 +               for (; bindex >= 0; bindex--, p--)
8532 +                       if (p->hd_dentry) {
8533 +                               dinfo->di_bbot = bindex;
8534 +                               err = 0;
8535 +                               break;
8536 +                       }
8537 +       }
8538 +
8539 +       return err;
8540 +}
8541 +
8542 +static void au_do_hide(struct dentry *dentry)
8543 +{
8544 +       struct inode *inode;
8545 +
8546 +       if (d_really_is_positive(dentry)) {
8547 +               inode = d_inode(dentry);
8548 +               if (!d_is_dir(dentry)) {
8549 +                       if (inode->i_nlink && !d_unhashed(dentry))
8550 +                               drop_nlink(inode);
8551 +               } else {
8552 +                       clear_nlink(inode);
8553 +                       /* stop next lookup */
8554 +                       inode->i_flags |= S_DEAD;
8555 +               }
8556 +               smp_mb(); /* necessary? */
8557 +       }
8558 +       d_drop(dentry);
8559 +}
8560 +
8561 +static int au_hide_children(struct dentry *parent)
8562 +{
8563 +       int err, i, j, ndentry;
8564 +       struct au_dcsub_pages dpages;
8565 +       struct au_dpage *dpage;
8566 +       struct dentry *dentry;
8567 +
8568 +       err = au_dpages_init(&dpages, GFP_NOFS);
8569 +       if (unlikely(err))
8570 +               goto out;
8571 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8572 +       if (unlikely(err))
8573 +               goto out_dpages;
8574 +
8575 +       /* in reverse order */
8576 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8577 +               dpage = dpages.dpages + i;
8578 +               ndentry = dpage->ndentry;
8579 +               for (j = ndentry - 1; j >= 0; j--) {
8580 +                       dentry = dpage->dentries[j];
8581 +                       if (dentry != parent)
8582 +                               au_do_hide(dentry);
8583 +               }
8584 +       }
8585 +
8586 +out_dpages:
8587 +       au_dpages_free(&dpages);
8588 +out:
8589 +       return err;
8590 +}
8591 +
8592 +static void au_hide(struct dentry *dentry)
8593 +{
8594 +       int err;
8595 +
8596 +       AuDbgDentry(dentry);
8597 +       if (d_is_dir(dentry)) {
8598 +               /* shrink_dcache_parent(dentry); */
8599 +               err = au_hide_children(dentry);
8600 +               if (unlikely(err))
8601 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8602 +                               dentry, err);
8603 +       }
8604 +       au_do_hide(dentry);
8605 +}
8606 +
8607 +/*
8608 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8609 + *
8610 + * a dirty branch is added
8611 + * - on the top of layers
8612 + * - in the middle of layers
8613 + * - to the bottom of layers
8614 + *
8615 + * on the added branch there exists
8616 + * - a whiteout
8617 + * - a diropq
8618 + * - a same named entry
8619 + *   + exist
8620 + *     * negative --> positive
8621 + *     * positive --> positive
8622 + *      - type is unchanged
8623 + *      - type is changed
8624 + *   + doesn't exist
8625 + *     * negative --> negative
8626 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8627 + * - none
8628 + */
8629 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8630 +                              struct au_dinfo *tmp)
8631 +{
8632 +       int err;
8633 +       aufs_bindex_t bindex, bbot;
8634 +       struct {
8635 +               struct dentry *dentry;
8636 +               struct inode *inode;
8637 +               mode_t mode;
8638 +       } orig_h, tmp_h = {
8639 +               .dentry = NULL
8640 +       };
8641 +       struct au_hdentry *hd;
8642 +       struct inode *inode, *h_inode;
8643 +       struct dentry *h_dentry;
8644 +
8645 +       err = 0;
8646 +       AuDebugOn(dinfo->di_btop < 0);
8647 +       orig_h.mode = 0;
8648 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8649 +       orig_h.inode = NULL;
8650 +       if (d_is_positive(orig_h.dentry)) {
8651 +               orig_h.inode = d_inode(orig_h.dentry);
8652 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8653 +       }
8654 +       if (tmp->di_btop >= 0) {
8655 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8656 +               if (d_is_positive(tmp_h.dentry)) {
8657 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8658 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8659 +               }
8660 +       }
8661 +
8662 +       inode = NULL;
8663 +       if (d_really_is_positive(dentry))
8664 +               inode = d_inode(dentry);
8665 +       if (!orig_h.inode) {
8666 +               AuDbg("negative originally\n");
8667 +               if (inode) {
8668 +                       au_hide(dentry);
8669 +                       goto out;
8670 +               }
8671 +               AuDebugOn(inode);
8672 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8673 +               AuDebugOn(dinfo->di_bdiropq != -1);
8674 +
8675 +               if (!tmp_h.inode) {
8676 +                       AuDbg("negative --> negative\n");
8677 +                       /* should have only one negative lower */
8678 +                       if (tmp->di_btop >= 0
8679 +                           && tmp->di_btop < dinfo->di_btop) {
8680 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8681 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8682 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8683 +                               au_di_cp(dinfo, tmp);
8684 +                               hd = au_hdentry(tmp, tmp->di_btop);
8685 +                               au_set_h_dptr(dentry, tmp->di_btop,
8686 +                                             dget(hd->hd_dentry));
8687 +                       }
8688 +                       au_dbg_verify_dinode(dentry);
8689 +               } else {
8690 +                       AuDbg("negative --> positive\n");
8691 +                       /*
8692 +                        * similar to the behaviour of creating with bypassing
8693 +                        * aufs.
8694 +                        * unhash it in order to force an error in the
8695 +                        * succeeding create operation.
8696 +                        * we should not set S_DEAD here.
8697 +                        */
8698 +                       d_drop(dentry);
8699 +                       /* au_di_swap(tmp, dinfo); */
8700 +                       au_dbg_verify_dinode(dentry);
8701 +               }
8702 +       } else {
8703 +               AuDbg("positive originally\n");
8704 +               /* inode may be NULL */
8705 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8706 +               if (!tmp_h.inode) {
8707 +                       AuDbg("positive --> negative\n");
8708 +                       /* or bypassing aufs */
8709 +                       au_hide(dentry);
8710 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8711 +                               dinfo->di_bwh = tmp->di_bwh;
8712 +                       if (inode)
8713 +                               err = au_refresh_hinode_self(inode);
8714 +                       au_dbg_verify_dinode(dentry);
8715 +               } else if (orig_h.mode == tmp_h.mode) {
8716 +                       AuDbg("positive --> positive, same type\n");
8717 +                       if (!S_ISDIR(orig_h.mode)
8718 +                           && dinfo->di_btop > tmp->di_btop) {
8719 +                               /*
8720 +                                * similar to the behaviour of removing and
8721 +                                * creating.
8722 +                                */
8723 +                               au_hide(dentry);
8724 +                               if (inode)
8725 +                                       err = au_refresh_hinode_self(inode);
8726 +                               au_dbg_verify_dinode(dentry);
8727 +                       } else {
8728 +                               /* fill empty slots */
8729 +                               if (dinfo->di_btop > tmp->di_btop)
8730 +                                       dinfo->di_btop = tmp->di_btop;
8731 +                               if (dinfo->di_bbot < tmp->di_bbot)
8732 +                                       dinfo->di_bbot = tmp->di_bbot;
8733 +                               dinfo->di_bwh = tmp->di_bwh;
8734 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8735 +                               bbot = dinfo->di_bbot;
8736 +                               bindex = tmp->di_btop;
8737 +                               hd = au_hdentry(tmp, bindex);
8738 +                               for (; bindex <= bbot; bindex++, hd++) {
8739 +                                       if (au_h_dptr(dentry, bindex))
8740 +                                               continue;
8741 +                                       h_dentry = hd->hd_dentry;
8742 +                                       if (!h_dentry)
8743 +                                               continue;
8744 +                                       AuDebugOn(d_is_negative(h_dentry));
8745 +                                       h_inode = d_inode(h_dentry);
8746 +                                       AuDebugOn(orig_h.mode
8747 +                                                 != (h_inode->i_mode
8748 +                                                     & S_IFMT));
8749 +                                       au_set_h_dptr(dentry, bindex,
8750 +                                                     dget(h_dentry));
8751 +                               }
8752 +                               if (inode)
8753 +                                       err = au_refresh_hinode(inode, dentry);
8754 +                               au_dbg_verify_dinode(dentry);
8755 +                       }
8756 +               } else {
8757 +                       AuDbg("positive --> positive, different type\n");
8758 +                       /* similar to the behaviour of removing and creating */
8759 +                       au_hide(dentry);
8760 +                       if (inode)
8761 +                               err = au_refresh_hinode_self(inode);
8762 +                       au_dbg_verify_dinode(dentry);
8763 +               }
8764 +       }
8765 +
8766 +out:
8767 +       return err;
8768 +}
8769 +
8770 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8771 +{
8772 +       const struct dentry_operations *dop
8773 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8774 +       static const unsigned int mask
8775 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8776 +
8777 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8778 +
8779 +       if (dentry->d_op == dop)
8780 +               return;
8781 +
8782 +       AuDbg("%pd\n", dentry);
8783 +       spin_lock(&dentry->d_lock);
8784 +       if (dop == &aufs_dop)
8785 +               dentry->d_flags |= mask;
8786 +       else
8787 +               dentry->d_flags &= ~mask;
8788 +       dentry->d_op = dop;
8789 +       spin_unlock(&dentry->d_lock);
8790 +}
8791 +
8792 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8793 +{
8794 +       int err, ebrange, nbr;
8795 +       unsigned int sigen;
8796 +       struct au_dinfo *dinfo, *tmp;
8797 +       struct super_block *sb;
8798 +       struct inode *inode;
8799 +
8800 +       DiMustWriteLock(dentry);
8801 +       AuDebugOn(IS_ROOT(dentry));
8802 +       AuDebugOn(d_really_is_negative(parent));
8803 +
8804 +       sb = dentry->d_sb;
8805 +       sigen = au_sigen(sb);
8806 +       err = au_digen_test(parent, sigen);
8807 +       if (unlikely(err))
8808 +               goto out;
8809 +
8810 +       nbr = au_sbbot(sb) + 1;
8811 +       dinfo = au_di(dentry);
8812 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8813 +       if (unlikely(err))
8814 +               goto out;
8815 +       ebrange = au_dbrange_test(dentry);
8816 +       if (!ebrange)
8817 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8818 +
8819 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8820 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8821 +               if (d_really_is_positive(dentry)) {
8822 +                       inode = d_inode(dentry);
8823 +                       err = au_refresh_hinode_self(inode);
8824 +               }
8825 +               au_dbg_verify_dinode(dentry);
8826 +               if (!err)
8827 +                       goto out_dgen; /* success */
8828 +               goto out;
8829 +       }
8830 +
8831 +       /* temporary dinfo */
8832 +       AuDbgDentry(dentry);
8833 +       err = -ENOMEM;
8834 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8835 +       if (unlikely(!tmp))
8836 +               goto out;
8837 +       au_di_swap(tmp, dinfo);
8838 +       /* returns the number of positive dentries */
8839 +       /*
8840 +        * if current working dir is removed, it returns an error.
8841 +        * but the dentry is legal.
8842 +        */
8843 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8844 +       AuDbgDentry(dentry);
8845 +       au_di_swap(tmp, dinfo);
8846 +       if (err == -ENOENT)
8847 +               err = 0;
8848 +       if (err >= 0) {
8849 +               /* compare/refresh by dinfo */
8850 +               AuDbgDentry(dentry);
8851 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8852 +               au_dbg_verify_dinode(dentry);
8853 +               AuTraceErr(err);
8854 +       }
8855 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8856 +       au_rw_write_unlock(&tmp->di_rwsem);
8857 +       au_di_free(tmp);
8858 +       if (unlikely(err))
8859 +               goto out;
8860 +
8861 +out_dgen:
8862 +       au_update_digen(dentry);
8863 +out:
8864 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8865 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8866 +               AuDbgDentry(dentry);
8867 +       }
8868 +       AuTraceErr(err);
8869 +       return err;
8870 +}
8871 +
8872 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8873 +                          struct dentry *dentry, aufs_bindex_t bindex)
8874 +{
8875 +       int err, valid;
8876 +
8877 +       err = 0;
8878 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8879 +               goto out;
8880 +
8881 +       AuDbg("b%d\n", bindex);
8882 +       /*
8883 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8884 +        * due to whiteout and branch permission.
8885 +        */
8886 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8887 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8888 +       /* it may return tri-state */
8889 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8890 +
8891 +       if (unlikely(valid < 0))
8892 +               err = valid;
8893 +       else if (!valid)
8894 +               err = -EINVAL;
8895 +
8896 +out:
8897 +       AuTraceErr(err);
8898 +       return err;
8899 +}
8900 +
8901 +/* todo: remove this */
8902 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8903 +                         unsigned int flags, int do_udba, int dirren)
8904 +{
8905 +       int err;
8906 +       umode_t mode, h_mode;
8907 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8908 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8909 +       struct inode *h_inode, *h_cached_inode;
8910 +       struct dentry *h_dentry;
8911 +       struct qstr *name, *h_name;
8912 +
8913 +       err = 0;
8914 +       plus = 0;
8915 +       mode = 0;
8916 +       ibs = -1;
8917 +       ibe = -1;
8918 +       unhashed = !!d_unhashed(dentry);
8919 +       is_root = !!IS_ROOT(dentry);
8920 +       name = &dentry->d_name;
8921 +       tmpfile = au_di(dentry)->di_tmpfile;
8922 +
8923 +       /*
8924 +        * Theoretically, REVAL test should be unnecessary in case of
8925 +        * {FS,I}NOTIFY.
8926 +        * But {fs,i}notify doesn't fire some necessary events,
8927 +        *      IN_ATTRIB for atime/nlink/pageio
8928 +        * Let's do REVAL test too.
8929 +        */
8930 +       if (do_udba && inode) {
8931 +               mode = (inode->i_mode & S_IFMT);
8932 +               plus = (inode->i_nlink > 0);
8933 +               ibs = au_ibtop(inode);
8934 +               ibe = au_ibbot(inode);
8935 +       }
8936 +
8937 +       btop = au_dbtop(dentry);
8938 +       btail = btop;
8939 +       if (inode && S_ISDIR(inode->i_mode))
8940 +               btail = au_dbtaildir(dentry);
8941 +       for (bindex = btop; bindex <= btail; bindex++) {
8942 +               h_dentry = au_h_dptr(dentry, bindex);
8943 +               if (!h_dentry)
8944 +                       continue;
8945 +
8946 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8947 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8948 +               spin_lock(&h_dentry->d_lock);
8949 +               h_name = &h_dentry->d_name;
8950 +               if (unlikely(do_udba
8951 +                            && !is_root
8952 +                            && ((!h_nfs
8953 +                                 && (unhashed != !!d_unhashed(h_dentry)
8954 +                                     || (!tmpfile && !dirren
8955 +                                         && !au_qstreq(name, h_name))
8956 +                                         ))
8957 +                                || (h_nfs
8958 +                                    && !(flags & LOOKUP_OPEN)
8959 +                                    && (h_dentry->d_flags
8960 +                                        & DCACHE_NFSFS_RENAMED)))
8961 +                           )) {
8962 +                       int h_unhashed;
8963 +
8964 +                       h_unhashed = d_unhashed(h_dentry);
8965 +                       spin_unlock(&h_dentry->d_lock);
8966 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8967 +                             unhashed, h_unhashed, dentry, h_dentry);
8968 +                       goto err;
8969 +               }
8970 +               spin_unlock(&h_dentry->d_lock);
8971 +
8972 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8973 +               if (unlikely(err))
8974 +                       /* do not goto err, to keep the errno */
8975 +                       break;
8976 +
8977 +               /* todo: plink too? */
8978 +               if (!do_udba)
8979 +                       continue;
8980 +
8981 +               /* UDBA tests */
8982 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8983 +                       goto err;
8984 +
8985 +               h_inode = NULL;
8986 +               if (d_is_positive(h_dentry))
8987 +                       h_inode = d_inode(h_dentry);
8988 +               h_plus = plus;
8989 +               h_mode = mode;
8990 +               h_cached_inode = h_inode;
8991 +               if (h_inode) {
8992 +                       h_mode = (h_inode->i_mode & S_IFMT);
8993 +                       h_plus = (h_inode->i_nlink > 0);
8994 +               }
8995 +               if (inode && ibs <= bindex && bindex <= ibe)
8996 +                       h_cached_inode = au_h_iptr(inode, bindex);
8997 +
8998 +               if (!h_nfs) {
8999 +                       if (unlikely(plus != h_plus && !tmpfile))
9000 +                               goto err;
9001 +               } else {
9002 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
9003 +                                    && !is_root
9004 +                                    && !IS_ROOT(h_dentry)
9005 +                                    && unhashed != d_unhashed(h_dentry)))
9006 +                               goto err;
9007 +               }
9008 +               if (unlikely(mode != h_mode
9009 +                            || h_cached_inode != h_inode))
9010 +                       goto err;
9011 +               continue;
9012 +
9013 +err:
9014 +               err = -EINVAL;
9015 +               break;
9016 +       }
9017 +
9018 +       AuTraceErr(err);
9019 +       return err;
9020 +}
9021 +
9022 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
9023 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
9024 +{
9025 +       int err;
9026 +       struct dentry *parent;
9027 +
9028 +       if (!au_digen_test(dentry, sigen))
9029 +               return 0;
9030 +
9031 +       parent = dget_parent(dentry);
9032 +       di_read_lock_parent(parent, AuLock_IR);
9033 +       AuDebugOn(au_digen_test(parent, sigen));
9034 +       au_dbg_verify_gen(parent, sigen);
9035 +       err = au_refresh_dentry(dentry, parent);
9036 +       di_read_unlock(parent, AuLock_IR);
9037 +       dput(parent);
9038 +       AuTraceErr(err);
9039 +       return err;
9040 +}
9041 +
9042 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
9043 +{
9044 +       int err;
9045 +       struct dentry *d, *parent;
9046 +
9047 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9048 +               return simple_reval_dpath(dentry, sigen);
9049 +
9050 +       /* slow loop, keep it simple and stupid */
9051 +       /* cf: au_cpup_dirs() */
9052 +       err = 0;
9053 +       parent = NULL;
9054 +       while (au_digen_test(dentry, sigen)) {
9055 +               d = dentry;
9056 +               while (1) {
9057 +                       dput(parent);
9058 +                       parent = dget_parent(d);
9059 +                       if (!au_digen_test(parent, sigen))
9060 +                               break;
9061 +                       d = parent;
9062 +               }
9063 +
9064 +               if (d != dentry)
9065 +                       di_write_lock_child2(d);
9066 +
9067 +               /* someone might update our dentry while we were sleeping */
9068 +               if (au_digen_test(d, sigen)) {
9069 +                       /*
9070 +                        * todo: consolidate with simple_reval_dpath(),
9071 +                        * do_refresh() and au_reval_for_attr().
9072 +                        */
9073 +                       di_read_lock_parent(parent, AuLock_IR);
9074 +                       err = au_refresh_dentry(d, parent);
9075 +                       di_read_unlock(parent, AuLock_IR);
9076 +               }
9077 +
9078 +               if (d != dentry)
9079 +                       di_write_unlock(d);
9080 +               dput(parent);
9081 +               if (unlikely(err))
9082 +                       break;
9083 +       }
9084 +
9085 +       return err;
9086 +}
9087 +
9088 +/*
9089 + * if valid returns 1, otherwise 0.
9090 + */
9091 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9092 +{
9093 +       int valid, err;
9094 +       unsigned int sigen;
9095 +       unsigned char do_udba, dirren;
9096 +       struct super_block *sb;
9097 +       struct inode *inode;
9098 +
9099 +       /* todo: support rcu-walk? */
9100 +       if (flags & LOOKUP_RCU)
9101 +               return -ECHILD;
9102 +
9103 +       valid = 0;
9104 +       if (unlikely(!au_di(dentry)))
9105 +               goto out;
9106 +
9107 +       valid = 1;
9108 +       sb = dentry->d_sb;
9109 +       /*
9110 +        * todo: very ugly
9111 +        * i_mutex of parent dir may be held,
9112 +        * but we should not return 'invalid' due to busy.
9113 +        */
9114 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9115 +       if (unlikely(err)) {
9116 +               valid = err;
9117 +               AuTraceErr(err);
9118 +               goto out;
9119 +       }
9120 +       inode = NULL;
9121 +       if (d_really_is_positive(dentry))
9122 +               inode = d_inode(dentry);
9123 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9124 +               err = -EINVAL;
9125 +               AuTraceErr(err);
9126 +               goto out_dgrade;
9127 +       }
9128 +       if (unlikely(au_dbrange_test(dentry))) {
9129 +               err = -EINVAL;
9130 +               AuTraceErr(err);
9131 +               goto out_dgrade;
9132 +       }
9133 +
9134 +       sigen = au_sigen(sb);
9135 +       if (au_digen_test(dentry, sigen)) {
9136 +               AuDebugOn(IS_ROOT(dentry));
9137 +               err = au_reval_dpath(dentry, sigen);
9138 +               if (unlikely(err)) {
9139 +                       AuTraceErr(err);
9140 +                       goto out_dgrade;
9141 +               }
9142 +       }
9143 +       di_downgrade_lock(dentry, AuLock_IR);
9144 +
9145 +       err = -EINVAL;
9146 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9147 +           && inode
9148 +           && !(inode->i_state && I_LINKABLE)
9149 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9150 +               AuTraceErr(err);
9151 +               goto out_inval;
9152 +       }
9153 +
9154 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9155 +       if (do_udba && inode) {
9156 +               aufs_bindex_t btop = au_ibtop(inode);
9157 +               struct inode *h_inode;
9158 +
9159 +               if (btop >= 0) {
9160 +                       h_inode = au_h_iptr(inode, btop);
9161 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9162 +                               AuTraceErr(err);
9163 +                               goto out_inval;
9164 +                       }
9165 +               }
9166 +       }
9167 +
9168 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9169 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9170 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9171 +               err = -EIO;
9172 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9173 +                     dentry, err);
9174 +       }
9175 +       goto out_inval;
9176 +
9177 +out_dgrade:
9178 +       di_downgrade_lock(dentry, AuLock_IR);
9179 +out_inval:
9180 +       aufs_read_unlock(dentry, AuLock_IR);
9181 +       AuTraceErr(err);
9182 +       valid = !err;
9183 +out:
9184 +       if (!valid) {
9185 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9186 +               d_drop(dentry);
9187 +       }
9188 +       return valid;
9189 +}
9190 +
9191 +static void aufs_d_release(struct dentry *dentry)
9192 +{
9193 +       if (au_di(dentry)) {
9194 +               au_di_fin(dentry);
9195 +               au_hn_di_reinit(dentry);
9196 +       }
9197 +}
9198 +
9199 +const struct dentry_operations aufs_dop = {
9200 +       .d_revalidate           = aufs_d_revalidate,
9201 +       .d_weak_revalidate      = aufs_d_revalidate,
9202 +       .d_release              = aufs_d_release
9203 +};
9204 +
9205 +/* aufs_dop without d_revalidate */
9206 +const struct dentry_operations aufs_dop_noreval = {
9207 +       .d_release              = aufs_d_release
9208 +};
9209 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9210 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9211 +++ linux/fs/aufs/dentry.h      2020-01-27 10:57:18.168871450 +0100
9212 @@ -0,0 +1,268 @@
9213 +/* SPDX-License-Identifier: GPL-2.0 */
9214 +/*
9215 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9216 + *
9217 + * This program, aufs is free software; you can redistribute it and/or modify
9218 + * it under the terms of the GNU General Public License as published by
9219 + * the Free Software Foundation; either version 2 of the License, or
9220 + * (at your option) any later version.
9221 + *
9222 + * This program is distributed in the hope that it will be useful,
9223 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9224 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9225 + * GNU General Public License for more details.
9226 + *
9227 + * You should have received a copy of the GNU General Public License
9228 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9229 + */
9230 +
9231 +/*
9232 + * lookup and dentry operations
9233 + */
9234 +
9235 +#ifndef __AUFS_DENTRY_H__
9236 +#define __AUFS_DENTRY_H__
9237 +
9238 +#ifdef __KERNEL__
9239 +
9240 +#include <linux/dcache.h>
9241 +#include "dirren.h"
9242 +#include "rwsem.h"
9243 +
9244 +struct au_hdentry {
9245 +       struct dentry           *hd_dentry;
9246 +       aufs_bindex_t           hd_id;
9247 +};
9248 +
9249 +struct au_dinfo {
9250 +       atomic_t                di_generation;
9251 +
9252 +       struct au_rwsem         di_rwsem;
9253 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9254 +       unsigned char           di_tmpfile; /* to allow the different name */
9255 +       struct au_hdentry       *di_hdentry;
9256 +       struct rcu_head         rcu;
9257 +} ____cacheline_aligned_in_smp;
9258 +
9259 +/* ---------------------------------------------------------------------- */
9260 +
9261 +/* flags for au_lkup_dentry() */
9262 +#define AuLkup_ALLOW_NEG       1
9263 +#define AuLkup_IGNORE_PERM     (1 << 1)
9264 +#define AuLkup_DIRREN          (1 << 2)
9265 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9266 +#define au_fset_lkup(flags, name) \
9267 +       do { (flags) |= AuLkup_##name; } while (0)
9268 +#define au_fclr_lkup(flags, name) \
9269 +       do { (flags) &= ~AuLkup_##name; } while (0)
9270 +
9271 +#ifndef CONFIG_AUFS_DIRREN
9272 +#undef AuLkup_DIRREN
9273 +#define AuLkup_DIRREN 0
9274 +#endif
9275 +
9276 +struct au_do_lookup_args {
9277 +       unsigned int            flags;
9278 +       mode_t                  type;
9279 +       struct qstr             whname, *name;
9280 +       struct au_dr_lookup     dirren;
9281 +};
9282 +
9283 +/* ---------------------------------------------------------------------- */
9284 +
9285 +/* dentry.c */
9286 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9287 +struct au_branch;
9288 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9289 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9290 +               struct dentry *h_parent, struct au_branch *br);
9291 +
9292 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9293 +                  unsigned int flags);
9294 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9295 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9296 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9297 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9298 +
9299 +/* dinfo.c */
9300 +void au_di_init_once(void *_di);
9301 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9302 +void au_di_free(struct au_dinfo *dinfo);
9303 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9304 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9305 +int au_di_init(struct dentry *dentry);
9306 +void au_di_fin(struct dentry *dentry);
9307 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9308 +
9309 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9310 +void di_read_unlock(struct dentry *d, int flags);
9311 +void di_downgrade_lock(struct dentry *d, int flags);
9312 +void di_write_lock(struct dentry *d, unsigned int lsc);
9313 +void di_write_unlock(struct dentry *d);
9314 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9315 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9316 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9317 +
9318 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9319 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9320 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9321 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9322 +
9323 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9324 +                  struct dentry *h_dentry);
9325 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9326 +int au_dbrange_test(struct dentry *dentry);
9327 +void au_update_digen(struct dentry *dentry);
9328 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9329 +void au_update_dbtop(struct dentry *dentry);
9330 +void au_update_dbbot(struct dentry *dentry);
9331 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9332 +
9333 +/* ---------------------------------------------------------------------- */
9334 +
9335 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9336 +{
9337 +       return dentry->d_fsdata;
9338 +}
9339 +
9340 +/* ---------------------------------------------------------------------- */
9341 +
9342 +/* lock subclass for dinfo */
9343 +enum {
9344 +       AuLsc_DI_CHILD,         /* child first */
9345 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9346 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9347 +       AuLsc_DI_PARENT,
9348 +       AuLsc_DI_PARENT2,
9349 +       AuLsc_DI_PARENT3,
9350 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9351 +};
9352 +
9353 +/*
9354 + * di_read_lock_child, di_write_lock_child,
9355 + * di_read_lock_child2, di_write_lock_child2,
9356 + * di_read_lock_child3, di_write_lock_child3,
9357 + * di_read_lock_parent, di_write_lock_parent,
9358 + * di_read_lock_parent2, di_write_lock_parent2,
9359 + * di_read_lock_parent3, di_write_lock_parent3,
9360 + */
9361 +#define AuReadLockFunc(name, lsc) \
9362 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9363 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9364 +
9365 +#define AuWriteLockFunc(name, lsc) \
9366 +static inline void di_write_lock_##name(struct dentry *d) \
9367 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9368 +
9369 +#define AuRWLockFuncs(name, lsc) \
9370 +       AuReadLockFunc(name, lsc) \
9371 +       AuWriteLockFunc(name, lsc)
9372 +
9373 +AuRWLockFuncs(child, CHILD);
9374 +AuRWLockFuncs(child2, CHILD2);
9375 +AuRWLockFuncs(child3, CHILD3);
9376 +AuRWLockFuncs(parent, PARENT);
9377 +AuRWLockFuncs(parent2, PARENT2);
9378 +AuRWLockFuncs(parent3, PARENT3);
9379 +
9380 +#undef AuReadLockFunc
9381 +#undef AuWriteLockFunc
9382 +#undef AuRWLockFuncs
9383 +
9384 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9385 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9386 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9387 +
9388 +/* ---------------------------------------------------------------------- */
9389 +
9390 +/* todo: memory barrier? */
9391 +static inline unsigned int au_digen(struct dentry *d)
9392 +{
9393 +       return atomic_read(&au_di(d)->di_generation);
9394 +}
9395 +
9396 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9397 +{
9398 +       hdentry->hd_dentry = NULL;
9399 +}
9400 +
9401 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9402 +                                           aufs_bindex_t bindex)
9403 +{
9404 +       return di->di_hdentry + bindex;
9405 +}
9406 +
9407 +static inline void au_hdput(struct au_hdentry *hd)
9408 +{
9409 +       if (hd)
9410 +               dput(hd->hd_dentry);
9411 +}
9412 +
9413 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9414 +{
9415 +       DiMustAnyLock(dentry);
9416 +       return au_di(dentry)->di_btop;
9417 +}
9418 +
9419 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9420 +{
9421 +       DiMustAnyLock(dentry);
9422 +       return au_di(dentry)->di_bbot;
9423 +}
9424 +
9425 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9426 +{
9427 +       DiMustAnyLock(dentry);
9428 +       return au_di(dentry)->di_bwh;
9429 +}
9430 +
9431 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9432 +{
9433 +       DiMustAnyLock(dentry);
9434 +       return au_di(dentry)->di_bdiropq;
9435 +}
9436 +
9437 +/* todo: hard/soft set? */
9438 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9439 +{
9440 +       DiMustWriteLock(dentry);
9441 +       au_di(dentry)->di_btop = bindex;
9442 +}
9443 +
9444 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9445 +{
9446 +       DiMustWriteLock(dentry);
9447 +       au_di(dentry)->di_bbot = bindex;
9448 +}
9449 +
9450 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9451 +{
9452 +       DiMustWriteLock(dentry);
9453 +       /* dbwh can be outside of btop - bbot range */
9454 +       au_di(dentry)->di_bwh = bindex;
9455 +}
9456 +
9457 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9458 +{
9459 +       DiMustWriteLock(dentry);
9460 +       au_di(dentry)->di_bdiropq = bindex;
9461 +}
9462 +
9463 +/* ---------------------------------------------------------------------- */
9464 +
9465 +#ifdef CONFIG_AUFS_HNOTIFY
9466 +static inline void au_digen_dec(struct dentry *d)
9467 +{
9468 +       atomic_dec(&au_di(d)->di_generation);
9469 +}
9470 +
9471 +static inline void au_hn_di_reinit(struct dentry *dentry)
9472 +{
9473 +       dentry->d_fsdata = NULL;
9474 +}
9475 +#else
9476 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9477 +#endif /* CONFIG_AUFS_HNOTIFY */
9478 +
9479 +#endif /* __KERNEL__ */
9480 +#endif /* __AUFS_DENTRY_H__ */
9481 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9482 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9483 +++ linux/fs/aufs/dinfo.c       2020-01-27 10:57:18.168871450 +0100
9484 @@ -0,0 +1,554 @@
9485 +// SPDX-License-Identifier: GPL-2.0
9486 +/*
9487 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9488 + *
9489 + * This program, aufs is free software; you can redistribute it and/or modify
9490 + * it under the terms of the GNU General Public License as published by
9491 + * the Free Software Foundation; either version 2 of the License, or
9492 + * (at your option) any later version.
9493 + *
9494 + * This program is distributed in the hope that it will be useful,
9495 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9496 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9497 + * GNU General Public License for more details.
9498 + *
9499 + * You should have received a copy of the GNU General Public License
9500 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9501 + */
9502 +
9503 +/*
9504 + * dentry private data
9505 + */
9506 +
9507 +#include "aufs.h"
9508 +
9509 +void au_di_init_once(void *_dinfo)
9510 +{
9511 +       struct au_dinfo *dinfo = _dinfo;
9512 +
9513 +       au_rw_init(&dinfo->di_rwsem);
9514 +}
9515 +
9516 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9517 +{
9518 +       struct au_dinfo *dinfo;
9519 +       int nbr, i;
9520 +
9521 +       dinfo = au_cache_alloc_dinfo();
9522 +       if (unlikely(!dinfo))
9523 +               goto out;
9524 +
9525 +       nbr = au_sbbot(sb) + 1;
9526 +       if (nbr <= 0)
9527 +               nbr = 1;
9528 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9529 +       if (dinfo->di_hdentry) {
9530 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9531 +               dinfo->di_btop = -1;
9532 +               dinfo->di_bbot = -1;
9533 +               dinfo->di_bwh = -1;
9534 +               dinfo->di_bdiropq = -1;
9535 +               dinfo->di_tmpfile = 0;
9536 +               for (i = 0; i < nbr; i++)
9537 +                       dinfo->di_hdentry[i].hd_id = -1;
9538 +               goto out;
9539 +       }
9540 +
9541 +       au_cache_free_dinfo(dinfo);
9542 +       dinfo = NULL;
9543 +
9544 +out:
9545 +       return dinfo;
9546 +}
9547 +
9548 +void au_di_free(struct au_dinfo *dinfo)
9549 +{
9550 +       struct au_hdentry *p;
9551 +       aufs_bindex_t bbot, bindex;
9552 +
9553 +       /* dentry may not be revalidated */
9554 +       bindex = dinfo->di_btop;
9555 +       if (bindex >= 0) {
9556 +               bbot = dinfo->di_bbot;
9557 +               p = au_hdentry(dinfo, bindex);
9558 +               while (bindex++ <= bbot)
9559 +                       au_hdput(p++);
9560 +       }
9561 +       au_kfree_try_rcu(dinfo->di_hdentry);
9562 +       au_cache_free_dinfo(dinfo);
9563 +}
9564 +
9565 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9566 +{
9567 +       struct au_hdentry *p;
9568 +       aufs_bindex_t bi;
9569 +
9570 +       AuRwMustWriteLock(&a->di_rwsem);
9571 +       AuRwMustWriteLock(&b->di_rwsem);
9572 +
9573 +#define DiSwap(v, name)                                \
9574 +       do {                                    \
9575 +               v = a->di_##name;               \
9576 +               a->di_##name = b->di_##name;    \
9577 +               b->di_##name = v;               \
9578 +       } while (0)
9579 +
9580 +       DiSwap(p, hdentry);
9581 +       DiSwap(bi, btop);
9582 +       DiSwap(bi, bbot);
9583 +       DiSwap(bi, bwh);
9584 +       DiSwap(bi, bdiropq);
9585 +       /* smp_mb(); */
9586 +
9587 +#undef DiSwap
9588 +}
9589 +
9590 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9591 +{
9592 +       AuRwMustWriteLock(&dst->di_rwsem);
9593 +       AuRwMustWriteLock(&src->di_rwsem);
9594 +
9595 +       dst->di_btop = src->di_btop;
9596 +       dst->di_bbot = src->di_bbot;
9597 +       dst->di_bwh = src->di_bwh;
9598 +       dst->di_bdiropq = src->di_bdiropq;
9599 +       /* smp_mb(); */
9600 +}
9601 +
9602 +int au_di_init(struct dentry *dentry)
9603 +{
9604 +       int err;
9605 +       struct super_block *sb;
9606 +       struct au_dinfo *dinfo;
9607 +
9608 +       err = 0;
9609 +       sb = dentry->d_sb;
9610 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9611 +       if (dinfo) {
9612 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9613 +               /* smp_mb(); */ /* atomic_set */
9614 +               dentry->d_fsdata = dinfo;
9615 +       } else
9616 +               err = -ENOMEM;
9617 +
9618 +       return err;
9619 +}
9620 +
9621 +void au_di_fin(struct dentry *dentry)
9622 +{
9623 +       struct au_dinfo *dinfo;
9624 +
9625 +       dinfo = au_di(dentry);
9626 +       AuRwDestroy(&dinfo->di_rwsem);
9627 +       au_di_free(dinfo);
9628 +}
9629 +
9630 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9631 +{
9632 +       int err, sz;
9633 +       struct au_hdentry *hdp;
9634 +
9635 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9636 +
9637 +       err = -ENOMEM;
9638 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9639 +       if (!sz)
9640 +               sz = sizeof(*hdp);
9641 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9642 +                          may_shrink);
9643 +       if (hdp) {
9644 +               dinfo->di_hdentry = hdp;
9645 +               err = 0;
9646 +       }
9647 +
9648 +       return err;
9649 +}
9650 +
9651 +/* ---------------------------------------------------------------------- */
9652 +
9653 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9654 +{
9655 +       switch (lsc) {
9656 +       case AuLsc_DI_CHILD:
9657 +               ii_write_lock_child(inode);
9658 +               break;
9659 +       case AuLsc_DI_CHILD2:
9660 +               ii_write_lock_child2(inode);
9661 +               break;
9662 +       case AuLsc_DI_CHILD3:
9663 +               ii_write_lock_child3(inode);
9664 +               break;
9665 +       case AuLsc_DI_PARENT:
9666 +               ii_write_lock_parent(inode);
9667 +               break;
9668 +       case AuLsc_DI_PARENT2:
9669 +               ii_write_lock_parent2(inode);
9670 +               break;
9671 +       case AuLsc_DI_PARENT3:
9672 +               ii_write_lock_parent3(inode);
9673 +               break;
9674 +       default:
9675 +               BUG();
9676 +       }
9677 +}
9678 +
9679 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9680 +{
9681 +       switch (lsc) {
9682 +       case AuLsc_DI_CHILD:
9683 +               ii_read_lock_child(inode);
9684 +               break;
9685 +       case AuLsc_DI_CHILD2:
9686 +               ii_read_lock_child2(inode);
9687 +               break;
9688 +       case AuLsc_DI_CHILD3:
9689 +               ii_read_lock_child3(inode);
9690 +               break;
9691 +       case AuLsc_DI_PARENT:
9692 +               ii_read_lock_parent(inode);
9693 +               break;
9694 +       case AuLsc_DI_PARENT2:
9695 +               ii_read_lock_parent2(inode);
9696 +               break;
9697 +       case AuLsc_DI_PARENT3:
9698 +               ii_read_lock_parent3(inode);
9699 +               break;
9700 +       default:
9701 +               BUG();
9702 +       }
9703 +}
9704 +
9705 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9706 +{
9707 +       struct inode *inode;
9708 +
9709 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9710 +       if (d_really_is_positive(d)) {
9711 +               inode = d_inode(d);
9712 +               if (au_ftest_lock(flags, IW))
9713 +                       do_ii_write_lock(inode, lsc);
9714 +               else if (au_ftest_lock(flags, IR))
9715 +                       do_ii_read_lock(inode, lsc);
9716 +       }
9717 +}
9718 +
9719 +void di_read_unlock(struct dentry *d, int flags)
9720 +{
9721 +       struct inode *inode;
9722 +
9723 +       if (d_really_is_positive(d)) {
9724 +               inode = d_inode(d);
9725 +               if (au_ftest_lock(flags, IW)) {
9726 +                       au_dbg_verify_dinode(d);
9727 +                       ii_write_unlock(inode);
9728 +               } else if (au_ftest_lock(flags, IR)) {
9729 +                       au_dbg_verify_dinode(d);
9730 +                       ii_read_unlock(inode);
9731 +               }
9732 +       }
9733 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9734 +}
9735 +
9736 +void di_downgrade_lock(struct dentry *d, int flags)
9737 +{
9738 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9739 +               ii_downgrade_lock(d_inode(d));
9740 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9741 +}
9742 +
9743 +void di_write_lock(struct dentry *d, unsigned int lsc)
9744 +{
9745 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9746 +       if (d_really_is_positive(d))
9747 +               do_ii_write_lock(d_inode(d), lsc);
9748 +}
9749 +
9750 +void di_write_unlock(struct dentry *d)
9751 +{
9752 +       au_dbg_verify_dinode(d);
9753 +       if (d_really_is_positive(d))
9754 +               ii_write_unlock(d_inode(d));
9755 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9756 +}
9757 +
9758 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9759 +{
9760 +       AuDebugOn(d1 == d2
9761 +                 || d_inode(d1) == d_inode(d2)
9762 +                 || d1->d_sb != d2->d_sb);
9763 +
9764 +       if ((isdir && au_test_subdir(d1, d2))
9765 +           || d1 < d2) {
9766 +               di_write_lock_child(d1);
9767 +               di_write_lock_child2(d2);
9768 +       } else {
9769 +               di_write_lock_child(d2);
9770 +               di_write_lock_child2(d1);
9771 +       }
9772 +}
9773 +
9774 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9775 +{
9776 +       AuDebugOn(d1 == d2
9777 +                 || d_inode(d1) == d_inode(d2)
9778 +                 || d1->d_sb != d2->d_sb);
9779 +
9780 +       if ((isdir && au_test_subdir(d1, d2))
9781 +           || d1 < d2) {
9782 +               di_write_lock_parent(d1);
9783 +               di_write_lock_parent2(d2);
9784 +       } else {
9785 +               di_write_lock_parent(d2);
9786 +               di_write_lock_parent2(d1);
9787 +       }
9788 +}
9789 +
9790 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9791 +{
9792 +       di_write_unlock(d1);
9793 +       if (d_inode(d1) == d_inode(d2))
9794 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9795 +       else
9796 +               di_write_unlock(d2);
9797 +}
9798 +
9799 +/* ---------------------------------------------------------------------- */
9800 +
9801 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9802 +{
9803 +       struct dentry *d;
9804 +
9805 +       DiMustAnyLock(dentry);
9806 +
9807 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9808 +               return NULL;
9809 +       AuDebugOn(bindex < 0);
9810 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9811 +       AuDebugOn(d && au_dcount(d) <= 0);
9812 +       return d;
9813 +}
9814 +
9815 +/*
9816 + * extended version of au_h_dptr().
9817 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9818 + * error.
9819 + */
9820 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9821 +{
9822 +       struct dentry *h_dentry;
9823 +       struct inode *inode, *h_inode;
9824 +
9825 +       AuDebugOn(d_really_is_negative(dentry));
9826 +
9827 +       h_dentry = NULL;
9828 +       if (au_dbtop(dentry) <= bindex
9829 +           && bindex <= au_dbbot(dentry))
9830 +               h_dentry = au_h_dptr(dentry, bindex);
9831 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9832 +               dget(h_dentry);
9833 +               goto out; /* success */
9834 +       }
9835 +
9836 +       inode = d_inode(dentry);
9837 +       AuDebugOn(bindex < au_ibtop(inode));
9838 +       AuDebugOn(au_ibbot(inode) < bindex);
9839 +       h_inode = au_h_iptr(inode, bindex);
9840 +       h_dentry = d_find_alias(h_inode);
9841 +       if (h_dentry) {
9842 +               if (!IS_ERR(h_dentry)) {
9843 +                       if (!au_d_linkable(h_dentry))
9844 +                               goto out; /* success */
9845 +                       dput(h_dentry);
9846 +               } else
9847 +                       goto out;
9848 +       }
9849 +
9850 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9851 +               h_dentry = au_plink_lkup(inode, bindex);
9852 +               AuDebugOn(!h_dentry);
9853 +               if (!IS_ERR(h_dentry)) {
9854 +                       if (!au_d_hashed_positive(h_dentry))
9855 +                               goto out; /* success */
9856 +                       dput(h_dentry);
9857 +                       h_dentry = NULL;
9858 +               }
9859 +       }
9860 +
9861 +out:
9862 +       AuDbgDentry(h_dentry);
9863 +       return h_dentry;
9864 +}
9865 +
9866 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9867 +{
9868 +       aufs_bindex_t bbot, bwh;
9869 +
9870 +       bbot = au_dbbot(dentry);
9871 +       if (0 <= bbot) {
9872 +               bwh = au_dbwh(dentry);
9873 +               if (!bwh)
9874 +                       return bwh;
9875 +               if (0 < bwh && bwh < bbot)
9876 +                       return bwh - 1;
9877 +       }
9878 +       return bbot;
9879 +}
9880 +
9881 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9882 +{
9883 +       aufs_bindex_t bbot, bopq;
9884 +
9885 +       bbot = au_dbtail(dentry);
9886 +       if (0 <= bbot) {
9887 +               bopq = au_dbdiropq(dentry);
9888 +               if (0 <= bopq && bopq < bbot)
9889 +                       bbot = bopq;
9890 +       }
9891 +       return bbot;
9892 +}
9893 +
9894 +/* ---------------------------------------------------------------------- */
9895 +
9896 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9897 +                  struct dentry *h_dentry)
9898 +{
9899 +       struct au_dinfo *dinfo;
9900 +       struct au_hdentry *hd;
9901 +       struct au_branch *br;
9902 +
9903 +       DiMustWriteLock(dentry);
9904 +
9905 +       dinfo = au_di(dentry);
9906 +       hd = au_hdentry(dinfo, bindex);
9907 +       au_hdput(hd);
9908 +       hd->hd_dentry = h_dentry;
9909 +       if (h_dentry) {
9910 +               br = au_sbr(dentry->d_sb, bindex);
9911 +               hd->hd_id = br->br_id;
9912 +       }
9913 +}
9914 +
9915 +int au_dbrange_test(struct dentry *dentry)
9916 +{
9917 +       int err;
9918 +       aufs_bindex_t btop, bbot;
9919 +
9920 +       err = 0;
9921 +       btop = au_dbtop(dentry);
9922 +       bbot = au_dbbot(dentry);
9923 +       if (btop >= 0)
9924 +               AuDebugOn(bbot < 0 && btop > bbot);
9925 +       else {
9926 +               err = -EIO;
9927 +               AuDebugOn(bbot >= 0);
9928 +       }
9929 +
9930 +       return err;
9931 +}
9932 +
9933 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9934 +{
9935 +       int err;
9936 +
9937 +       err = 0;
9938 +       if (unlikely(au_digen(dentry) != sigen
9939 +                    || au_iigen_test(d_inode(dentry), sigen)))
9940 +               err = -EIO;
9941 +
9942 +       return err;
9943 +}
9944 +
9945 +void au_update_digen(struct dentry *dentry)
9946 +{
9947 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9948 +       /* smp_mb(); */ /* atomic_set */
9949 +}
9950 +
9951 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9952 +{
9953 +       struct au_dinfo *dinfo;
9954 +       struct dentry *h_d;
9955 +       struct au_hdentry *hdp;
9956 +       aufs_bindex_t bindex, bbot;
9957 +
9958 +       DiMustWriteLock(dentry);
9959 +
9960 +       dinfo = au_di(dentry);
9961 +       if (!dinfo || dinfo->di_btop < 0)
9962 +               return;
9963 +
9964 +       if (do_put_zero) {
9965 +               bbot = dinfo->di_bbot;
9966 +               bindex = dinfo->di_btop;
9967 +               hdp = au_hdentry(dinfo, bindex);
9968 +               for (; bindex <= bbot; bindex++, hdp++) {
9969 +                       h_d = hdp->hd_dentry;
9970 +                       if (h_d && d_is_negative(h_d))
9971 +                               au_set_h_dptr(dentry, bindex, NULL);
9972 +               }
9973 +       }
9974 +
9975 +       dinfo->di_btop = 0;
9976 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9977 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9978 +               if (hdp->hd_dentry)
9979 +                       break;
9980 +       if (dinfo->di_btop > dinfo->di_bbot) {
9981 +               dinfo->di_btop = -1;
9982 +               dinfo->di_bbot = -1;
9983 +               return;
9984 +       }
9985 +
9986 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9987 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9988 +               if (hdp->hd_dentry)
9989 +                       break;
9990 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9991 +}
9992 +
9993 +void au_update_dbtop(struct dentry *dentry)
9994 +{
9995 +       aufs_bindex_t bindex, bbot;
9996 +       struct dentry *h_dentry;
9997 +
9998 +       bbot = au_dbbot(dentry);
9999 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
10000 +               h_dentry = au_h_dptr(dentry, bindex);
10001 +               if (!h_dentry)
10002 +                       continue;
10003 +               if (d_is_positive(h_dentry)) {
10004 +                       au_set_dbtop(dentry, bindex);
10005 +                       return;
10006 +               }
10007 +               au_set_h_dptr(dentry, bindex, NULL);
10008 +       }
10009 +}
10010 +
10011 +void au_update_dbbot(struct dentry *dentry)
10012 +{
10013 +       aufs_bindex_t bindex, btop;
10014 +       struct dentry *h_dentry;
10015 +
10016 +       btop = au_dbtop(dentry);
10017 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
10018 +               h_dentry = au_h_dptr(dentry, bindex);
10019 +               if (!h_dentry)
10020 +                       continue;
10021 +               if (d_is_positive(h_dentry)) {
10022 +                       au_set_dbbot(dentry, bindex);
10023 +                       return;
10024 +               }
10025 +               au_set_h_dptr(dentry, bindex, NULL);
10026 +       }
10027 +}
10028 +
10029 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
10030 +{
10031 +       aufs_bindex_t bindex, bbot;
10032 +
10033 +       bbot = au_dbbot(dentry);
10034 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
10035 +               if (au_h_dptr(dentry, bindex) == h_dentry)
10036 +                       return bindex;
10037 +       return -1;
10038 +}
10039 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
10040 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
10041 +++ linux/fs/aufs/dir.c 2020-01-27 10:57:18.168871450 +0100
10042 @@ -0,0 +1,763 @@
10043 +// SPDX-License-Identifier: GPL-2.0
10044 +/*
10045 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10046 + *
10047 + * This program, aufs is free software; you can redistribute it and/or modify
10048 + * it under the terms of the GNU General Public License as published by
10049 + * the Free Software Foundation; either version 2 of the License, or
10050 + * (at your option) any later version.
10051 + *
10052 + * This program is distributed in the hope that it will be useful,
10053 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10054 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10055 + * GNU General Public License for more details.
10056 + *
10057 + * You should have received a copy of the GNU General Public License
10058 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10059 + */
10060 +
10061 +/*
10062 + * directory operations
10063 + */
10064 +
10065 +#include <linux/fs_stack.h>
10066 +#include <linux/iversion.h>
10067 +#include "aufs.h"
10068 +
10069 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10070 +{
10071 +       unsigned int nlink;
10072 +
10073 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10074 +
10075 +       nlink = dir->i_nlink;
10076 +       nlink += h_dir->i_nlink - 2;
10077 +       if (h_dir->i_nlink < 2)
10078 +               nlink += 2;
10079 +       smp_mb(); /* for i_nlink */
10080 +       /* 0 can happen in revaliding */
10081 +       set_nlink(dir, nlink);
10082 +}
10083 +
10084 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10085 +{
10086 +       unsigned int nlink;
10087 +
10088 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10089 +
10090 +       nlink = dir->i_nlink;
10091 +       nlink -= h_dir->i_nlink - 2;
10092 +       if (h_dir->i_nlink < 2)
10093 +               nlink -= 2;
10094 +       smp_mb(); /* for i_nlink */
10095 +       /* nlink == 0 means the branch-fs is broken */
10096 +       set_nlink(dir, nlink);
10097 +}
10098 +
10099 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10100 +{
10101 +       loff_t sz;
10102 +       aufs_bindex_t bindex, bbot;
10103 +       struct file *h_file;
10104 +       struct dentry *h_dentry;
10105 +
10106 +       sz = 0;
10107 +       if (file) {
10108 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10109 +
10110 +               bbot = au_fbbot_dir(file);
10111 +               for (bindex = au_fbtop(file);
10112 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10113 +                    bindex++) {
10114 +                       h_file = au_hf_dir(file, bindex);
10115 +                       if (h_file && file_inode(h_file))
10116 +                               sz += vfsub_f_size_read(h_file);
10117 +               }
10118 +       } else {
10119 +               AuDebugOn(!dentry);
10120 +               AuDebugOn(!d_is_dir(dentry));
10121 +
10122 +               bbot = au_dbtaildir(dentry);
10123 +               for (bindex = au_dbtop(dentry);
10124 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10125 +                    bindex++) {
10126 +                       h_dentry = au_h_dptr(dentry, bindex);
10127 +                       if (h_dentry && d_is_positive(h_dentry))
10128 +                               sz += i_size_read(d_inode(h_dentry));
10129 +               }
10130 +       }
10131 +       if (sz < KMALLOC_MAX_SIZE)
10132 +               sz = roundup_pow_of_two(sz);
10133 +       if (sz > KMALLOC_MAX_SIZE)
10134 +               sz = KMALLOC_MAX_SIZE;
10135 +       else if (sz < NAME_MAX) {
10136 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10137 +               sz = AUFS_RDBLK_DEF;
10138 +       }
10139 +       return sz;
10140 +}
10141 +
10142 +struct au_dir_ts_arg {
10143 +       struct dentry *dentry;
10144 +       aufs_bindex_t brid;
10145 +};
10146 +
10147 +static void au_do_dir_ts(void *arg)
10148 +{
10149 +       struct au_dir_ts_arg *a = arg;
10150 +       struct au_dtime dt;
10151 +       struct path h_path;
10152 +       struct inode *dir, *h_dir;
10153 +       struct super_block *sb;
10154 +       struct au_branch *br;
10155 +       struct au_hinode *hdir;
10156 +       int err;
10157 +       aufs_bindex_t btop, bindex;
10158 +
10159 +       sb = a->dentry->d_sb;
10160 +       if (d_really_is_negative(a->dentry))
10161 +               goto out;
10162 +       /* no dir->i_mutex lock */
10163 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10164 +
10165 +       dir = d_inode(a->dentry);
10166 +       btop = au_ibtop(dir);
10167 +       bindex = au_br_index(sb, a->brid);
10168 +       if (bindex < btop)
10169 +               goto out_unlock;
10170 +
10171 +       br = au_sbr(sb, bindex);
10172 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10173 +       if (!h_path.dentry)
10174 +               goto out_unlock;
10175 +       h_path.mnt = au_br_mnt(br);
10176 +       au_dtime_store(&dt, a->dentry, &h_path);
10177 +
10178 +       br = au_sbr(sb, btop);
10179 +       if (!au_br_writable(br->br_perm))
10180 +               goto out_unlock;
10181 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10182 +       h_path.mnt = au_br_mnt(br);
10183 +       err = vfsub_mnt_want_write(h_path.mnt);
10184 +       if (err)
10185 +               goto out_unlock;
10186 +       hdir = au_hi(dir, btop);
10187 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10188 +       h_dir = au_h_iptr(dir, btop);
10189 +       if (h_dir->i_nlink
10190 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10191 +               dt.dt_h_path = h_path;
10192 +               au_dtime_revert(&dt);
10193 +       }
10194 +       au_hn_inode_unlock(hdir);
10195 +       vfsub_mnt_drop_write(h_path.mnt);
10196 +       au_cpup_attr_timesizes(dir);
10197 +
10198 +out_unlock:
10199 +       aufs_read_unlock(a->dentry, AuLock_DW);
10200 +out:
10201 +       dput(a->dentry);
10202 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10203 +       au_kfree_try_rcu(arg);
10204 +}
10205 +
10206 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10207 +{
10208 +       int perm, wkq_err;
10209 +       aufs_bindex_t btop;
10210 +       struct au_dir_ts_arg *arg;
10211 +       struct dentry *dentry;
10212 +       struct super_block *sb;
10213 +
10214 +       IMustLock(dir);
10215 +
10216 +       dentry = d_find_any_alias(dir);
10217 +       AuDebugOn(!dentry);
10218 +       sb = dentry->d_sb;
10219 +       btop = au_ibtop(dir);
10220 +       if (btop == bindex) {
10221 +               au_cpup_attr_timesizes(dir);
10222 +               goto out;
10223 +       }
10224 +
10225 +       perm = au_sbr_perm(sb, btop);
10226 +       if (!au_br_writable(perm))
10227 +               goto out;
10228 +
10229 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10230 +       if (!arg)
10231 +               goto out;
10232 +
10233 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10234 +       arg->brid = au_sbr_id(sb, bindex);
10235 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10236 +       if (unlikely(wkq_err)) {
10237 +               pr_err("wkq %d\n", wkq_err);
10238 +               dput(dentry);
10239 +               au_kfree_try_rcu(arg);
10240 +       }
10241 +
10242 +out:
10243 +       dput(dentry);
10244 +}
10245 +
10246 +/* ---------------------------------------------------------------------- */
10247 +
10248 +static int reopen_dir(struct file *file)
10249 +{
10250 +       int err;
10251 +       unsigned int flags;
10252 +       aufs_bindex_t bindex, btail, btop;
10253 +       struct dentry *dentry, *h_dentry;
10254 +       struct file *h_file;
10255 +
10256 +       /* open all lower dirs */
10257 +       dentry = file->f_path.dentry;
10258 +       btop = au_dbtop(dentry);
10259 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10260 +               au_set_h_fptr(file, bindex, NULL);
10261 +       au_set_fbtop(file, btop);
10262 +
10263 +       btail = au_dbtaildir(dentry);
10264 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10265 +               au_set_h_fptr(file, bindex, NULL);
10266 +       au_set_fbbot_dir(file, btail);
10267 +
10268 +       flags = vfsub_file_flags(file);
10269 +       for (bindex = btop; bindex <= btail; bindex++) {
10270 +               h_dentry = au_h_dptr(dentry, bindex);
10271 +               if (!h_dentry)
10272 +                       continue;
10273 +               h_file = au_hf_dir(file, bindex);
10274 +               if (h_file)
10275 +                       continue;
10276 +
10277 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10278 +               err = PTR_ERR(h_file);
10279 +               if (IS_ERR(h_file))
10280 +                       goto out; /* close all? */
10281 +               au_set_h_fptr(file, bindex, h_file);
10282 +       }
10283 +       au_update_figen(file);
10284 +       /* todo: necessary? */
10285 +       /* file->f_ra = h_file->f_ra; */
10286 +       err = 0;
10287 +
10288 +out:
10289 +       return err;
10290 +}
10291 +
10292 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10293 +{
10294 +       int err;
10295 +       aufs_bindex_t bindex, btail;
10296 +       struct dentry *dentry, *h_dentry;
10297 +       struct vfsmount *mnt;
10298 +
10299 +       FiMustWriteLock(file);
10300 +       AuDebugOn(h_file);
10301 +
10302 +       err = 0;
10303 +       mnt = file->f_path.mnt;
10304 +       dentry = file->f_path.dentry;
10305 +       file->f_version = inode_query_iversion(d_inode(dentry));
10306 +       bindex = au_dbtop(dentry);
10307 +       au_set_fbtop(file, bindex);
10308 +       btail = au_dbtaildir(dentry);
10309 +       au_set_fbbot_dir(file, btail);
10310 +       for (; !err && bindex <= btail; bindex++) {
10311 +               h_dentry = au_h_dptr(dentry, bindex);
10312 +               if (!h_dentry)
10313 +                       continue;
10314 +
10315 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10316 +               if (unlikely(err))
10317 +                       break;
10318 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10319 +               if (IS_ERR(h_file)) {
10320 +                       err = PTR_ERR(h_file);
10321 +                       break;
10322 +               }
10323 +               au_set_h_fptr(file, bindex, h_file);
10324 +       }
10325 +       au_update_figen(file);
10326 +       /* todo: necessary? */
10327 +       /* file->f_ra = h_file->f_ra; */
10328 +       if (!err)
10329 +               return 0; /* success */
10330 +
10331 +       /* close all */
10332 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10333 +               au_set_h_fptr(file, bindex, NULL);
10334 +       au_set_fbtop(file, -1);
10335 +       au_set_fbbot_dir(file, -1);
10336 +
10337 +       return err;
10338 +}
10339 +
10340 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10341 +                        struct file *file)
10342 +{
10343 +       int err;
10344 +       struct super_block *sb;
10345 +       struct au_fidir *fidir;
10346 +
10347 +       err = -ENOMEM;
10348 +       sb = file->f_path.dentry->d_sb;
10349 +       si_read_lock(sb, AuLock_FLUSH);
10350 +       fidir = au_fidir_alloc(sb);
10351 +       if (fidir) {
10352 +               struct au_do_open_args args = {
10353 +                       .open   = do_open_dir,
10354 +                       .fidir  = fidir
10355 +               };
10356 +               err = au_do_open(file, &args);
10357 +               if (unlikely(err))
10358 +                       au_kfree_rcu(fidir);
10359 +       }
10360 +       si_read_unlock(sb);
10361 +       return err;
10362 +}
10363 +
10364 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10365 +                           struct file *file)
10366 +{
10367 +       struct au_vdir *vdir_cache;
10368 +       struct au_finfo *finfo;
10369 +       struct au_fidir *fidir;
10370 +       struct au_hfile *hf;
10371 +       aufs_bindex_t bindex, bbot;
10372 +
10373 +       finfo = au_fi(file);
10374 +       fidir = finfo->fi_hdir;
10375 +       if (fidir) {
10376 +               au_hbl_del(&finfo->fi_hlist,
10377 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10378 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10379 +               if (vdir_cache)
10380 +                       au_vdir_free(vdir_cache);
10381 +
10382 +               bindex = finfo->fi_btop;
10383 +               if (bindex >= 0) {
10384 +                       hf = fidir->fd_hfile + bindex;
10385 +                       /*
10386 +                        * calls fput() instead of filp_close(),
10387 +                        * since no dnotify or lock for the lower file.
10388 +                        */
10389 +                       bbot = fidir->fd_bbot;
10390 +                       for (; bindex <= bbot; bindex++, hf++)
10391 +                               if (hf->hf_file)
10392 +                                       au_hfput(hf, /*execed*/0);
10393 +               }
10394 +               au_kfree_rcu(fidir);
10395 +               finfo->fi_hdir = NULL;
10396 +       }
10397 +       au_finfo_fin(file);
10398 +       return 0;
10399 +}
10400 +
10401 +/* ---------------------------------------------------------------------- */
10402 +
10403 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10404 +{
10405 +       int err;
10406 +       aufs_bindex_t bindex, bbot;
10407 +       struct file *h_file;
10408 +
10409 +       err = 0;
10410 +       bbot = au_fbbot_dir(file);
10411 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10412 +               h_file = au_hf_dir(file, bindex);
10413 +               if (h_file)
10414 +                       err = vfsub_flush(h_file, id);
10415 +       }
10416 +       return err;
10417 +}
10418 +
10419 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10420 +{
10421 +       return au_do_flush(file, id, au_do_flush_dir);
10422 +}
10423 +
10424 +/* ---------------------------------------------------------------------- */
10425 +
10426 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10427 +{
10428 +       int err;
10429 +       aufs_bindex_t bbot, bindex;
10430 +       struct inode *inode;
10431 +       struct super_block *sb;
10432 +
10433 +       err = 0;
10434 +       sb = dentry->d_sb;
10435 +       inode = d_inode(dentry);
10436 +       IMustLock(inode);
10437 +       bbot = au_dbbot(dentry);
10438 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10439 +               struct path h_path;
10440 +
10441 +               if (au_test_ro(sb, bindex, inode))
10442 +                       continue;
10443 +               h_path.dentry = au_h_dptr(dentry, bindex);
10444 +               if (!h_path.dentry)
10445 +                       continue;
10446 +
10447 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10448 +               err = vfsub_fsync(NULL, &h_path, datasync);
10449 +       }
10450 +
10451 +       return err;
10452 +}
10453 +
10454 +static int au_do_fsync_dir(struct file *file, int datasync)
10455 +{
10456 +       int err;
10457 +       aufs_bindex_t bbot, bindex;
10458 +       struct file *h_file;
10459 +       struct super_block *sb;
10460 +       struct inode *inode;
10461 +
10462 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10463 +       if (unlikely(err))
10464 +               goto out;
10465 +
10466 +       inode = file_inode(file);
10467 +       sb = inode->i_sb;
10468 +       bbot = au_fbbot_dir(file);
10469 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10470 +               h_file = au_hf_dir(file, bindex);
10471 +               if (!h_file || au_test_ro(sb, bindex, inode))
10472 +                       continue;
10473 +
10474 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10475 +       }
10476 +
10477 +out:
10478 +       return err;
10479 +}
10480 +
10481 +/*
10482 + * @file may be NULL
10483 + */
10484 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10485 +                         int datasync)
10486 +{
10487 +       int err;
10488 +       struct dentry *dentry;
10489 +       struct inode *inode;
10490 +       struct super_block *sb;
10491 +
10492 +       err = 0;
10493 +       dentry = file->f_path.dentry;
10494 +       inode = d_inode(dentry);
10495 +       inode_lock(inode);
10496 +       sb = dentry->d_sb;
10497 +       si_noflush_read_lock(sb);
10498 +       if (file)
10499 +               err = au_do_fsync_dir(file, datasync);
10500 +       else {
10501 +               di_write_lock_child(dentry);
10502 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10503 +       }
10504 +       au_cpup_attr_timesizes(inode);
10505 +       di_write_unlock(dentry);
10506 +       if (file)
10507 +               fi_write_unlock(file);
10508 +
10509 +       si_read_unlock(sb);
10510 +       inode_unlock(inode);
10511 +       return err;
10512 +}
10513 +
10514 +/* ---------------------------------------------------------------------- */
10515 +
10516 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10517 +{
10518 +       int err;
10519 +       struct dentry *dentry;
10520 +       struct inode *inode, *h_inode;
10521 +       struct super_block *sb;
10522 +
10523 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10524 +
10525 +       dentry = file->f_path.dentry;
10526 +       inode = d_inode(dentry);
10527 +       IMustLock(inode);
10528 +
10529 +       sb = dentry->d_sb;
10530 +       si_read_lock(sb, AuLock_FLUSH);
10531 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10532 +       if (unlikely(err))
10533 +               goto out;
10534 +       err = au_alive_dir(dentry);
10535 +       if (!err)
10536 +               err = au_vdir_init(file);
10537 +       di_downgrade_lock(dentry, AuLock_IR);
10538 +       if (unlikely(err))
10539 +               goto out_unlock;
10540 +
10541 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10542 +       if (!au_test_nfsd()) {
10543 +               err = au_vdir_fill_de(file, ctx);
10544 +               fsstack_copy_attr_atime(inode, h_inode);
10545 +       } else {
10546 +               /*
10547 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10548 +                * encode_fh() and others.
10549 +                */
10550 +               atomic_inc(&h_inode->i_count);
10551 +               di_read_unlock(dentry, AuLock_IR);
10552 +               si_read_unlock(sb);
10553 +               err = au_vdir_fill_de(file, ctx);
10554 +               fsstack_copy_attr_atime(inode, h_inode);
10555 +               fi_write_unlock(file);
10556 +               iput(h_inode);
10557 +
10558 +               AuTraceErr(err);
10559 +               return err;
10560 +       }
10561 +
10562 +out_unlock:
10563 +       di_read_unlock(dentry, AuLock_IR);
10564 +       fi_write_unlock(file);
10565 +out:
10566 +       si_read_unlock(sb);
10567 +       return err;
10568 +}
10569 +
10570 +/* ---------------------------------------------------------------------- */
10571 +
10572 +#define AuTestEmpty_WHONLY     1
10573 +#define AuTestEmpty_CALLED     (1 << 1)
10574 +#define AuTestEmpty_SHWH       (1 << 2)
10575 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10576 +#define au_fset_testempty(flags, name) \
10577 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10578 +#define au_fclr_testempty(flags, name) \
10579 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10580 +
10581 +#ifndef CONFIG_AUFS_SHWH
10582 +#undef AuTestEmpty_SHWH
10583 +#define AuTestEmpty_SHWH       0
10584 +#endif
10585 +
10586 +struct test_empty_arg {
10587 +       struct dir_context ctx;
10588 +       struct au_nhash *whlist;
10589 +       unsigned int flags;
10590 +       int err;
10591 +       aufs_bindex_t bindex;
10592 +};
10593 +
10594 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10595 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10596 +                        unsigned int d_type)
10597 +{
10598 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10599 +                                                 ctx);
10600 +       char *name = (void *)__name;
10601 +
10602 +       arg->err = 0;
10603 +       au_fset_testempty(arg->flags, CALLED);
10604 +       /* smp_mb(); */
10605 +       if (name[0] == '.'
10606 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10607 +               goto out; /* success */
10608 +
10609 +       if (namelen <= AUFS_WH_PFX_LEN
10610 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10611 +               if (au_ftest_testempty(arg->flags, WHONLY)
10612 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10613 +                       arg->err = -ENOTEMPTY;
10614 +               goto out;
10615 +       }
10616 +
10617 +       name += AUFS_WH_PFX_LEN;
10618 +       namelen -= AUFS_WH_PFX_LEN;
10619 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10620 +               arg->err = au_nhash_append_wh
10621 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10622 +                        au_ftest_testempty(arg->flags, SHWH));
10623 +
10624 +out:
10625 +       /* smp_mb(); */
10626 +       AuTraceErr(arg->err);
10627 +       return arg->err;
10628 +}
10629 +
10630 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10631 +{
10632 +       int err;
10633 +       struct file *h_file;
10634 +       struct au_branch *br;
10635 +
10636 +       h_file = au_h_open(dentry, arg->bindex,
10637 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10638 +                          /*file*/NULL, /*force_wr*/0);
10639 +       err = PTR_ERR(h_file);
10640 +       if (IS_ERR(h_file))
10641 +               goto out;
10642 +
10643 +       err = 0;
10644 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10645 +           && !file_inode(h_file)->i_nlink)
10646 +               goto out_put;
10647 +
10648 +       do {
10649 +               arg->err = 0;
10650 +               au_fclr_testempty(arg->flags, CALLED);
10651 +               /* smp_mb(); */
10652 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10653 +               if (err >= 0)
10654 +                       err = arg->err;
10655 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10656 +
10657 +out_put:
10658 +       fput(h_file);
10659 +       br = au_sbr(dentry->d_sb, arg->bindex);
10660 +       au_lcnt_dec(&br->br_nfiles);
10661 +out:
10662 +       return err;
10663 +}
10664 +
10665 +struct do_test_empty_args {
10666 +       int *errp;
10667 +       struct dentry *dentry;
10668 +       struct test_empty_arg *arg;
10669 +};
10670 +
10671 +static void call_do_test_empty(void *args)
10672 +{
10673 +       struct do_test_empty_args *a = args;
10674 +       *a->errp = do_test_empty(a->dentry, a->arg);
10675 +}
10676 +
10677 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10678 +{
10679 +       int err, wkq_err;
10680 +       struct dentry *h_dentry;
10681 +       struct inode *h_inode;
10682 +
10683 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10684 +       h_inode = d_inode(h_dentry);
10685 +       /* todo: i_mode changes anytime? */
10686 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10687 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10688 +       inode_unlock_shared(h_inode);
10689 +       if (!err)
10690 +               err = do_test_empty(dentry, arg);
10691 +       else {
10692 +               struct do_test_empty_args args = {
10693 +                       .errp   = &err,
10694 +                       .dentry = dentry,
10695 +                       .arg    = arg
10696 +               };
10697 +               unsigned int flags = arg->flags;
10698 +
10699 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10700 +               if (unlikely(wkq_err))
10701 +                       err = wkq_err;
10702 +               arg->flags = flags;
10703 +       }
10704 +
10705 +       return err;
10706 +}
10707 +
10708 +int au_test_empty_lower(struct dentry *dentry)
10709 +{
10710 +       int err;
10711 +       unsigned int rdhash;
10712 +       aufs_bindex_t bindex, btop, btail;
10713 +       struct au_nhash whlist;
10714 +       struct test_empty_arg arg = {
10715 +               .ctx = {
10716 +                       .actor = test_empty_cb
10717 +               }
10718 +       };
10719 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10720 +
10721 +       SiMustAnyLock(dentry->d_sb);
10722 +
10723 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10724 +       if (!rdhash)
10725 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10726 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10727 +       if (unlikely(err))
10728 +               goto out;
10729 +
10730 +       arg.flags = 0;
10731 +       arg.whlist = &whlist;
10732 +       btop = au_dbtop(dentry);
10733 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10734 +               au_fset_testempty(arg.flags, SHWH);
10735 +       test_empty = do_test_empty;
10736 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10737 +               test_empty = sio_test_empty;
10738 +       arg.bindex = btop;
10739 +       err = test_empty(dentry, &arg);
10740 +       if (unlikely(err))
10741 +               goto out_whlist;
10742 +
10743 +       au_fset_testempty(arg.flags, WHONLY);
10744 +       btail = au_dbtaildir(dentry);
10745 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10746 +               struct dentry *h_dentry;
10747 +
10748 +               h_dentry = au_h_dptr(dentry, bindex);
10749 +               if (h_dentry && d_is_positive(h_dentry)) {
10750 +                       arg.bindex = bindex;
10751 +                       err = test_empty(dentry, &arg);
10752 +               }
10753 +       }
10754 +
10755 +out_whlist:
10756 +       au_nhash_wh_free(&whlist);
10757 +out:
10758 +       return err;
10759 +}
10760 +
10761 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10762 +{
10763 +       int err;
10764 +       struct test_empty_arg arg = {
10765 +               .ctx = {
10766 +                       .actor = test_empty_cb
10767 +               }
10768 +       };
10769 +       aufs_bindex_t bindex, btail;
10770 +
10771 +       err = 0;
10772 +       arg.whlist = whlist;
10773 +       arg.flags = AuTestEmpty_WHONLY;
10774 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10775 +               au_fset_testempty(arg.flags, SHWH);
10776 +       btail = au_dbtaildir(dentry);
10777 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10778 +               struct dentry *h_dentry;
10779 +
10780 +               h_dentry = au_h_dptr(dentry, bindex);
10781 +               if (h_dentry && d_is_positive(h_dentry)) {
10782 +                       arg.bindex = bindex;
10783 +                       err = sio_test_empty(dentry, &arg);
10784 +               }
10785 +       }
10786 +
10787 +       return err;
10788 +}
10789 +
10790 +/* ---------------------------------------------------------------------- */
10791 +
10792 +const struct file_operations aufs_dir_fop = {
10793 +       .owner          = THIS_MODULE,
10794 +       .llseek         = default_llseek,
10795 +       .read           = generic_read_dir,
10796 +       .iterate_shared = aufs_iterate_shared,
10797 +       .unlocked_ioctl = aufs_ioctl_dir,
10798 +#ifdef CONFIG_COMPAT
10799 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10800 +#endif
10801 +       .open           = aufs_open_dir,
10802 +       .release        = aufs_release_dir,
10803 +       .flush          = aufs_flush_dir,
10804 +       .fsync          = aufs_fsync_dir
10805 +};
10806 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10807 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10808 +++ linux/fs/aufs/dir.h 2020-01-27 10:57:18.168871450 +0100
10809 @@ -0,0 +1,134 @@
10810 +/* SPDX-License-Identifier: GPL-2.0 */
10811 +/*
10812 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10813 + *
10814 + * This program, aufs is free software; you can redistribute it and/or modify
10815 + * it under the terms of the GNU General Public License as published by
10816 + * the Free Software Foundation; either version 2 of the License, or
10817 + * (at your option) any later version.
10818 + *
10819 + * This program is distributed in the hope that it will be useful,
10820 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10821 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10822 + * GNU General Public License for more details.
10823 + *
10824 + * You should have received a copy of the GNU General Public License
10825 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10826 + */
10827 +
10828 +/*
10829 + * directory operations
10830 + */
10831 +
10832 +#ifndef __AUFS_DIR_H__
10833 +#define __AUFS_DIR_H__
10834 +
10835 +#ifdef __KERNEL__
10836 +
10837 +#include <linux/fs.h>
10838 +
10839 +/* ---------------------------------------------------------------------- */
10840 +
10841 +/* need to be faster and smaller */
10842 +
10843 +struct au_nhash {
10844 +       unsigned int            nh_num;
10845 +       struct hlist_head       *nh_head;
10846 +};
10847 +
10848 +struct au_vdir_destr {
10849 +       unsigned char   len;
10850 +       unsigned char   name[0];
10851 +} __packed;
10852 +
10853 +struct au_vdir_dehstr {
10854 +       struct hlist_node       hash;
10855 +       struct au_vdir_destr    *str;
10856 +       struct rcu_head         rcu;
10857 +} ____cacheline_aligned_in_smp;
10858 +
10859 +struct au_vdir_de {
10860 +       ino_t                   de_ino;
10861 +       unsigned char           de_type;
10862 +       /* caution: packed */
10863 +       struct au_vdir_destr    de_str;
10864 +} __packed;
10865 +
10866 +struct au_vdir_wh {
10867 +       struct hlist_node       wh_hash;
10868 +#ifdef CONFIG_AUFS_SHWH
10869 +       ino_t                   wh_ino;
10870 +       aufs_bindex_t           wh_bindex;
10871 +       unsigned char           wh_type;
10872 +#else
10873 +       aufs_bindex_t           wh_bindex;
10874 +#endif
10875 +       /* caution: packed */
10876 +       struct au_vdir_destr    wh_str;
10877 +} __packed;
10878 +
10879 +union au_vdir_deblk_p {
10880 +       unsigned char           *deblk;
10881 +       struct au_vdir_de       *de;
10882 +};
10883 +
10884 +struct au_vdir {
10885 +       unsigned char   **vd_deblk;
10886 +       unsigned long   vd_nblk;
10887 +       struct {
10888 +               unsigned long           ul;
10889 +               union au_vdir_deblk_p   p;
10890 +       } vd_last;
10891 +
10892 +       u64             vd_version;
10893 +       unsigned int    vd_deblk_sz;
10894 +       unsigned long   vd_jiffy;
10895 +       struct rcu_head rcu;
10896 +} ____cacheline_aligned_in_smp;
10897 +
10898 +/* ---------------------------------------------------------------------- */
10899 +
10900 +/* dir.c */
10901 +extern const struct file_operations aufs_dir_fop;
10902 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10903 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10904 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10905 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10906 +int au_test_empty_lower(struct dentry *dentry);
10907 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10908 +
10909 +/* vdir.c */
10910 +unsigned int au_rdhash_est(loff_t sz);
10911 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10912 +void au_nhash_wh_free(struct au_nhash *whlist);
10913 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10914 +                           int limit);
10915 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10916 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10917 +                      unsigned int d_type, aufs_bindex_t bindex,
10918 +                      unsigned char shwh);
10919 +void au_vdir_free(struct au_vdir *vdir);
10920 +int au_vdir_init(struct file *file);
10921 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10922 +
10923 +/* ioctl.c */
10924 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10925 +
10926 +#ifdef CONFIG_AUFS_RDU
10927 +/* rdu.c */
10928 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10929 +#ifdef CONFIG_COMPAT
10930 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10931 +                        unsigned long arg);
10932 +#endif
10933 +#else
10934 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10935 +       unsigned int cmd, unsigned long arg)
10936 +#ifdef CONFIG_COMPAT
10937 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10938 +       unsigned int cmd, unsigned long arg)
10939 +#endif
10940 +#endif
10941 +
10942 +#endif /* __KERNEL__ */
10943 +#endif /* __AUFS_DIR_H__ */
10944 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10945 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10946 +++ linux/fs/aufs/dirren.c      2020-01-27 10:57:18.168871450 +0100
10947 @@ -0,0 +1,1316 @@
10948 +// SPDX-License-Identifier: GPL-2.0
10949 +/*
10950 + * Copyright (C) 2017-2020 Junjiro R. Okajima
10951 + *
10952 + * This program, aufs is free software; you can redistribute it and/or modify
10953 + * it under the terms of the GNU General Public License as published by
10954 + * the Free Software Foundation; either version 2 of the License, or
10955 + * (at your option) any later version.
10956 + *
10957 + * This program is distributed in the hope that it will be useful,
10958 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10959 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10960 + * GNU General Public License for more details.
10961 + *
10962 + * You should have received a copy of the GNU General Public License
10963 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10964 + */
10965 +
10966 +/*
10967 + * special handling in renaming a directory
10968 + * in order to support looking-up the before-renamed name on the lower readonly
10969 + * branches
10970 + */
10971 +
10972 +#include <linux/byteorder/generic.h>
10973 +#include "aufs.h"
10974 +
10975 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10976 +{
10977 +       int idx;
10978 +
10979 +       idx = au_dr_ihash(ent->dr_h_ino);
10980 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10981 +}
10982 +
10983 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10984 +{
10985 +       int ret, i;
10986 +       struct hlist_bl_head *hbl;
10987 +
10988 +       ret = 1;
10989 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10990 +               hbl = dr->dr_h_ino + i;
10991 +               hlist_bl_lock(hbl);
10992 +               ret &= hlist_bl_empty(hbl);
10993 +               hlist_bl_unlock(hbl);
10994 +       }
10995 +
10996 +       return ret;
10997 +}
10998 +
10999 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
11000 +{
11001 +       struct au_dr_hino *found, *ent;
11002 +       struct hlist_bl_head *hbl;
11003 +       struct hlist_bl_node *pos;
11004 +       int idx;
11005 +
11006 +       found = NULL;
11007 +       idx = au_dr_ihash(ino);
11008 +       hbl = dr->dr_h_ino + idx;
11009 +       hlist_bl_lock(hbl);
11010 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11011 +               if (ent->dr_h_ino == ino) {
11012 +                       found = ent;
11013 +                       break;
11014 +               }
11015 +       hlist_bl_unlock(hbl);
11016 +
11017 +       return found;
11018 +}
11019 +
11020 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
11021 +                       struct au_dr_hino *add_ent)
11022 +{
11023 +       int found, idx;
11024 +       struct hlist_bl_head *hbl;
11025 +       struct hlist_bl_node *pos;
11026 +       struct au_dr_hino *ent;
11027 +
11028 +       found = 0;
11029 +       idx = au_dr_ihash(ino);
11030 +       hbl = dr->dr_h_ino + idx;
11031 +#if 0 /* debug print */
11032 +       {
11033 +               struct hlist_bl_node *tmp;
11034 +
11035 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11036 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
11037 +       }
11038 +#endif
11039 +       hlist_bl_lock(hbl);
11040 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
11041 +               if (ent->dr_h_ino == ino) {
11042 +                       found = 1;
11043 +                       break;
11044 +               }
11045 +       if (!found && add_ent)
11046 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11047 +       hlist_bl_unlock(hbl);
11048 +
11049 +       if (!found && add_ent)
11050 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11051 +
11052 +       return found;
11053 +}
11054 +
11055 +void au_dr_hino_free(struct au_dr_br *dr)
11056 +{
11057 +       int i;
11058 +       struct hlist_bl_head *hbl;
11059 +       struct hlist_bl_node *pos, *tmp;
11060 +       struct au_dr_hino *ent;
11061 +
11062 +       /* SiMustWriteLock(sb); */
11063 +
11064 +       for (i = 0; i < AuDirren_NHASH; i++) {
11065 +               hbl = dr->dr_h_ino + i;
11066 +               /* no spinlock since sbinfo must be write-locked */
11067 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11068 +                       au_kfree_rcu(ent);
11069 +               INIT_HLIST_BL_HEAD(hbl);
11070 +       }
11071 +}
11072 +
11073 +/* returns the number of inodes or an error */
11074 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11075 +                           struct file *hinofile)
11076 +{
11077 +       int err, i;
11078 +       ssize_t ssz;
11079 +       loff_t pos, oldsize;
11080 +       __be64 u64;
11081 +       struct inode *hinoinode;
11082 +       struct hlist_bl_head *hbl;
11083 +       struct hlist_bl_node *n1, *n2;
11084 +       struct au_dr_hino *ent;
11085 +
11086 +       SiMustWriteLock(sb);
11087 +       AuDebugOn(!au_br_writable(br->br_perm));
11088 +
11089 +       hinoinode = file_inode(hinofile);
11090 +       oldsize = i_size_read(hinoinode);
11091 +
11092 +       err = 0;
11093 +       pos = 0;
11094 +       hbl = br->br_dirren.dr_h_ino;
11095 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11096 +               /* no bit-lock since sbinfo must be write-locked */
11097 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11098 +                       AuDbg("hi%llu, %pD2\n",
11099 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11100 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11101 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11102 +                       if (ssz == sizeof(u64))
11103 +                               continue;
11104 +
11105 +                       /* write error */
11106 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11107 +                       err = -ENOSPC;
11108 +                       if (ssz < 0)
11109 +                               err = ssz;
11110 +                       break;
11111 +               }
11112 +       }
11113 +       /* regardless the error */
11114 +       if (pos < oldsize) {
11115 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11116 +               AuTraceErr(err);
11117 +       }
11118 +
11119 +       AuTraceErr(err);
11120 +       return err;
11121 +}
11122 +
11123 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11124 +{
11125 +       int err, hidx;
11126 +       ssize_t ssz;
11127 +       size_t sz, n;
11128 +       loff_t pos;
11129 +       uint64_t u64;
11130 +       struct au_dr_hino *ent;
11131 +       struct inode *hinoinode;
11132 +       struct hlist_bl_head *hbl;
11133 +
11134 +       err = 0;
11135 +       pos = 0;
11136 +       hbl = dr->dr_h_ino;
11137 +       hinoinode = file_inode(hinofile);
11138 +       sz = i_size_read(hinoinode);
11139 +       AuDebugOn(sz % sizeof(u64));
11140 +       n = sz / sizeof(u64);
11141 +       while (n--) {
11142 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11143 +               if (unlikely(ssz != sizeof(u64))) {
11144 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11145 +                       err = -EINVAL;
11146 +                       if (ssz < 0)
11147 +                               err = ssz;
11148 +                       goto out_free;
11149 +               }
11150 +
11151 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11152 +               if (!ent) {
11153 +                       err = -ENOMEM;
11154 +                       AuTraceErr(err);
11155 +                       goto out_free;
11156 +               }
11157 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11158 +               AuDbg("hi%llu, %pD2\n",
11159 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11160 +               hidx = au_dr_ihash(ent->dr_h_ino);
11161 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11162 +       }
11163 +       goto out; /* success */
11164 +
11165 +out_free:
11166 +       au_dr_hino_free(dr);
11167 +out:
11168 +       AuTraceErr(err);
11169 +       return err;
11170 +}
11171 +
11172 +/*
11173 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11174 + * @path is a switch to distinguish load and store.
11175 + */
11176 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11177 +                     struct au_branch *br, const struct path *path)
11178 +{
11179 +       int err, flags;
11180 +       unsigned char load, suspend;
11181 +       struct file *hinofile;
11182 +       struct au_hinode *hdir;
11183 +       struct inode *dir, *delegated;
11184 +       struct path hinopath;
11185 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11186 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11187 +
11188 +       AuDebugOn(bindex < 0 && !br);
11189 +       AuDebugOn(bindex >= 0 && br);
11190 +
11191 +       err = -EINVAL;
11192 +       suspend = !br;
11193 +       if (suspend)
11194 +               br = au_sbr(sb, bindex);
11195 +       load = !!path;
11196 +       if (!load) {
11197 +               path = &br->br_path;
11198 +               AuDebugOn(!au_br_writable(br->br_perm));
11199 +               if (unlikely(!au_br_writable(br->br_perm)))
11200 +                       goto out;
11201 +       }
11202 +
11203 +       hdir = NULL;
11204 +       if (suspend) {
11205 +               dir = d_inode(sb->s_root);
11206 +               hdir = au_hinode(au_ii(dir), bindex);
11207 +               dir = hdir->hi_inode;
11208 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11209 +       } else {
11210 +               dir = d_inode(path->dentry);
11211 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11212 +       }
11213 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11214 +       err = PTR_ERR(hinopath.dentry);
11215 +       if (IS_ERR(hinopath.dentry))
11216 +               goto out_unlock;
11217 +
11218 +       err = 0;
11219 +       flags = O_RDONLY;
11220 +       if (load) {
11221 +               if (d_is_negative(hinopath.dentry))
11222 +                       goto out_dput; /* success */
11223 +       } else {
11224 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11225 +                       if (d_is_positive(hinopath.dentry)) {
11226 +                               delegated = NULL;
11227 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11228 +                                                  /*force*/0);
11229 +                               AuTraceErr(err);
11230 +                               if (unlikely(err))
11231 +                                       pr_err("ignored err %d, %pd2\n",
11232 +                                              err, hinopath.dentry);
11233 +                               if (unlikely(err == -EWOULDBLOCK))
11234 +                                       iput(delegated);
11235 +                               err = 0;
11236 +                       }
11237 +                       goto out_dput;
11238 +               } else if (!d_is_positive(hinopath.dentry)) {
11239 +                       err = vfsub_create(dir, &hinopath, 0600,
11240 +                                          /*want_excl*/false);
11241 +                       AuTraceErr(err);
11242 +                       if (unlikely(err))
11243 +                               goto out_dput;
11244 +               }
11245 +               flags = O_WRONLY;
11246 +       }
11247 +       hinopath.mnt = path->mnt;
11248 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11249 +       if (suspend)
11250 +               au_hn_inode_unlock(hdir);
11251 +       else
11252 +               inode_unlock(dir);
11253 +       dput(hinopath.dentry);
11254 +       AuTraceErrPtr(hinofile);
11255 +       if (IS_ERR(hinofile)) {
11256 +               err = PTR_ERR(hinofile);
11257 +               goto out;
11258 +       }
11259 +
11260 +       if (load)
11261 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11262 +       else
11263 +               err = au_dr_hino_store(sb, br, hinofile);
11264 +       fput(hinofile);
11265 +       goto out;
11266 +
11267 +out_dput:
11268 +       dput(hinopath.dentry);
11269 +out_unlock:
11270 +       if (suspend)
11271 +               au_hn_inode_unlock(hdir);
11272 +       else
11273 +               inode_unlock(dir);
11274 +out:
11275 +       AuTraceErr(err);
11276 +       return err;
11277 +}
11278 +
11279 +/* ---------------------------------------------------------------------- */
11280 +
11281 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11282 +{
11283 +       int err;
11284 +       struct kstatfs kstfs;
11285 +       dev_t dev;
11286 +       struct dentry *dentry;
11287 +       struct super_block *sb;
11288 +
11289 +       err = vfs_statfs((void *)path, &kstfs);
11290 +       AuTraceErr(err);
11291 +       if (unlikely(err))
11292 +               goto out;
11293 +
11294 +       /* todo: support for UUID */
11295 +
11296 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11297 +               brid->type = AuBrid_FSID;
11298 +               brid->fsid = kstfs.f_fsid;
11299 +       } else {
11300 +               dentry = path->dentry;
11301 +               sb = dentry->d_sb;
11302 +               dev = sb->s_dev;
11303 +               if (dev) {
11304 +                       brid->type = AuBrid_DEV;
11305 +                       brid->dev = dev;
11306 +               }
11307 +       }
11308 +
11309 +out:
11310 +       return err;
11311 +}
11312 +
11313 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11314 +                 const struct path *path)
11315 +{
11316 +       int err, i;
11317 +       struct au_dr_br *dr;
11318 +       struct hlist_bl_head *hbl;
11319 +
11320 +       dr = &br->br_dirren;
11321 +       hbl = dr->dr_h_ino;
11322 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11323 +               INIT_HLIST_BL_HEAD(hbl);
11324 +
11325 +       err = au_dr_brid_init(&dr->dr_brid, path);
11326 +       if (unlikely(err))
11327 +               goto out;
11328 +
11329 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11330 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11331 +
11332 +out:
11333 +       AuTraceErr(err);
11334 +       return err;
11335 +}
11336 +
11337 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11338 +{
11339 +       int err;
11340 +
11341 +       err = 0;
11342 +       if (au_br_writable(br->br_perm))
11343 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11344 +       if (!err)
11345 +               au_dr_hino_free(&br->br_dirren);
11346 +
11347 +       return err;
11348 +}
11349 +
11350 +/* ---------------------------------------------------------------------- */
11351 +
11352 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11353 +                      char *buf, size_t sz)
11354 +{
11355 +       int err;
11356 +       unsigned int major, minor;
11357 +       char *p;
11358 +
11359 +       p = buf;
11360 +       err = snprintf(p, sz, "%d_", brid->type);
11361 +       AuDebugOn(err > sz);
11362 +       p += err;
11363 +       sz -= err;
11364 +       switch (brid->type) {
11365 +       case AuBrid_Unset:
11366 +               return -EINVAL;
11367 +       case AuBrid_UUID:
11368 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11369 +               break;
11370 +       case AuBrid_FSID:
11371 +               err = snprintf(p, sz, "%08x-%08x",
11372 +                              brid->fsid.val[0], brid->fsid.val[1]);
11373 +               break;
11374 +       case AuBrid_DEV:
11375 +               major = MAJOR(brid->dev);
11376 +               minor = MINOR(brid->dev);
11377 +               if (major <= 0xff && minor <= 0xff)
11378 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11379 +               else
11380 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11381 +               break;
11382 +       }
11383 +       AuDebugOn(err > sz);
11384 +       p += err;
11385 +       sz -= err;
11386 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11387 +       AuDebugOn(err > sz);
11388 +       p += err;
11389 +       sz -= err;
11390 +
11391 +       return p - buf;
11392 +}
11393 +
11394 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11395 +{
11396 +       int rlen;
11397 +       struct dentry *br_dentry;
11398 +       struct inode *br_inode;
11399 +
11400 +       br_dentry = au_br_dentry(br);
11401 +       br_inode = d_inode(br_dentry);
11402 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11403 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11404 +       AuDebugOn(rlen > len);
11405 +
11406 +       return rlen;
11407 +}
11408 +
11409 +/* ---------------------------------------------------------------------- */
11410 +
11411 +/*
11412 + * from the given @h_dentry, construct drinfo at @*fdata.
11413 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11414 + * @allocated.
11415 + */
11416 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11417 +                              struct dentry *h_dentry,
11418 +                              unsigned char *allocated)
11419 +{
11420 +       int err, v;
11421 +       struct au_drinfo_fdata *f, *p;
11422 +       struct au_drinfo *drinfo;
11423 +       struct inode *h_inode;
11424 +       struct qstr *qname;
11425 +
11426 +       err = 0;
11427 +       f = *fdata;
11428 +       h_inode = d_inode(h_dentry);
11429 +       qname = &h_dentry->d_name;
11430 +       drinfo = &f->drinfo;
11431 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11432 +       drinfo->oldnamelen = qname->len;
11433 +       if (*allocated < sizeof(*f) + qname->len) {
11434 +               v = roundup_pow_of_two(*allocated + qname->len);
11435 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11436 +               if (unlikely(!p)) {
11437 +                       err = -ENOMEM;
11438 +                       AuTraceErr(err);
11439 +                       goto out;
11440 +               }
11441 +               f = p;
11442 +               *fdata = f;
11443 +               *allocated = v;
11444 +               drinfo = &f->drinfo;
11445 +       }
11446 +       memcpy(drinfo->oldname, qname->name, qname->len);
11447 +       AuDbg("i%llu, %.*s\n",
11448 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11449 +             drinfo->oldname);
11450 +
11451 +out:
11452 +       AuTraceErr(err);
11453 +       return err;
11454 +}
11455 +
11456 +/* callers have to free the return value */
11457 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11458 +{
11459 +       struct au_drinfo *ret, *drinfo;
11460 +       struct au_drinfo_fdata fdata;
11461 +       int len;
11462 +       loff_t pos;
11463 +       ssize_t ssz;
11464 +
11465 +       ret = ERR_PTR(-EIO);
11466 +       pos = 0;
11467 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11468 +       if (unlikely(ssz != sizeof(fdata))) {
11469 +               AuIOErr("ssz %zd, %u, %pD2\n",
11470 +                       ssz, (unsigned int)sizeof(fdata), file);
11471 +               goto out;
11472 +       }
11473 +
11474 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11475 +       switch (fdata.magic) {
11476 +       case AUFS_DRINFO_MAGIC_V1:
11477 +               break;
11478 +       default:
11479 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11480 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11481 +               goto out;
11482 +       }
11483 +
11484 +       drinfo = &fdata.drinfo;
11485 +       len = drinfo->oldnamelen;
11486 +       if (!len) {
11487 +               AuIOErr("broken drinfo %pD2\n", file);
11488 +               goto out;
11489 +       }
11490 +
11491 +       ret = NULL;
11492 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11493 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11494 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11495 +                     (unsigned long long)drinfo->ino,
11496 +                     (unsigned long long)h_ino, file);
11497 +               goto out; /* success */
11498 +       }
11499 +
11500 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11501 +       if (unlikely(!ret)) {
11502 +               ret = ERR_PTR(-ENOMEM);
11503 +               AuTraceErrPtr(ret);
11504 +               goto out;
11505 +       }
11506 +
11507 +       *ret = *drinfo;
11508 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11509 +       if (unlikely(ssz != len)) {
11510 +               au_kfree_rcu(ret);
11511 +               ret = ERR_PTR(-EIO);
11512 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11513 +               goto out;
11514 +       }
11515 +
11516 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11517 +
11518 +out:
11519 +       return ret;
11520 +}
11521 +
11522 +/* ---------------------------------------------------------------------- */
11523 +
11524 +/* in order to be revertible */
11525 +struct au_drinfo_rev_elm {
11526 +       int                     created;
11527 +       struct dentry           *info_dentry;
11528 +       struct au_drinfo        *info_last;
11529 +};
11530 +
11531 +struct au_drinfo_rev {
11532 +       unsigned char                   already;
11533 +       aufs_bindex_t                   nelm;
11534 +       struct au_drinfo_rev_elm        elm[0];
11535 +};
11536 +
11537 +/* todo: isn't it too large? */
11538 +struct au_drinfo_store {
11539 +       struct path h_ppath;
11540 +       struct dentry *h_dentry;
11541 +       struct au_drinfo_fdata *fdata;
11542 +       char *infoname;                 /* inside of whname, just after PFX */
11543 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11544 +       aufs_bindex_t btgt, btail;
11545 +       unsigned char no_sio,
11546 +               allocated,              /* current size of *fdata */
11547 +               infonamelen,            /* room size for p */
11548 +               whnamelen,              /* length of the generated name */
11549 +               renameback;             /* renamed back */
11550 +};
11551 +
11552 +/* on rename(2) error, the caller should revert it using @elm */
11553 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11554 +                             struct au_drinfo_rev_elm *elm)
11555 +{
11556 +       int err, len;
11557 +       ssize_t ssz;
11558 +       loff_t pos;
11559 +       struct path infopath = {
11560 +               .mnt = w->h_ppath.mnt
11561 +       };
11562 +       struct inode *h_dir, *h_inode, *delegated;
11563 +       struct file *infofile;
11564 +       struct qstr *qname;
11565 +
11566 +       AuDebugOn(elm
11567 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11568 +
11569 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11570 +                                              w->whnamelen);
11571 +       AuTraceErrPtr(infopath.dentry);
11572 +       if (IS_ERR(infopath.dentry)) {
11573 +               err = PTR_ERR(infopath.dentry);
11574 +               goto out;
11575 +       }
11576 +
11577 +       err = 0;
11578 +       h_dir = d_inode(w->h_ppath.dentry);
11579 +       if (elm && d_is_negative(infopath.dentry)) {
11580 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11581 +               AuTraceErr(err);
11582 +               if (unlikely(err))
11583 +                       goto out_dput;
11584 +               elm->created = 1;
11585 +               elm->info_dentry = dget(infopath.dentry);
11586 +       }
11587 +
11588 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11589 +       AuTraceErrPtr(infofile);
11590 +       if (IS_ERR(infofile)) {
11591 +               err = PTR_ERR(infofile);
11592 +               goto out_dput;
11593 +       }
11594 +
11595 +       h_inode = d_inode(infopath.dentry);
11596 +       if (elm && i_size_read(h_inode)) {
11597 +               h_inode = d_inode(w->h_dentry);
11598 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11599 +               AuTraceErrPtr(elm->info_last);
11600 +               if (IS_ERR(elm->info_last)) {
11601 +                       err = PTR_ERR(elm->info_last);
11602 +                       elm->info_last = NULL;
11603 +                       AuDebugOn(elm->info_dentry);
11604 +                       goto out_fput;
11605 +               }
11606 +       }
11607 +
11608 +       if (elm && w->renameback) {
11609 +               delegated = NULL;
11610 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11611 +               AuTraceErr(err);
11612 +               if (unlikely(err == -EWOULDBLOCK))
11613 +                       iput(delegated);
11614 +               goto out_fput;
11615 +       }
11616 +
11617 +       pos = 0;
11618 +       qname = &w->h_dentry->d_name;
11619 +       len = sizeof(*w->fdata) + qname->len;
11620 +       if (!elm)
11621 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11622 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11623 +       if (ssz == len) {
11624 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11625 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11626 +               goto out_fput; /* success */
11627 +       } else {
11628 +               err = -EIO;
11629 +               if (ssz < 0)
11630 +                       err = ssz;
11631 +               /* the caller should revert it using @elm */
11632 +       }
11633 +
11634 +out_fput:
11635 +       fput(infofile);
11636 +out_dput:
11637 +       dput(infopath.dentry);
11638 +out:
11639 +       AuTraceErr(err);
11640 +       return err;
11641 +}
11642 +
11643 +struct au_call_drinfo_do_store_args {
11644 +       int *errp;
11645 +       struct au_drinfo_store *w;
11646 +       struct au_drinfo_rev_elm *elm;
11647 +};
11648 +
11649 +static void au_call_drinfo_do_store(void *args)
11650 +{
11651 +       struct au_call_drinfo_do_store_args *a = args;
11652 +
11653 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11654 +}
11655 +
11656 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11657 +                              struct au_drinfo_rev_elm *elm)
11658 +{
11659 +       int err, wkq_err;
11660 +
11661 +       if (w->no_sio)
11662 +               err = au_drinfo_do_store(w, elm);
11663 +       else {
11664 +               struct au_call_drinfo_do_store_args a = {
11665 +                       .errp   = &err,
11666 +                       .w      = w,
11667 +                       .elm    = elm
11668 +               };
11669 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11670 +               if (unlikely(wkq_err))
11671 +                       err = wkq_err;
11672 +       }
11673 +       AuTraceErr(err);
11674 +
11675 +       return err;
11676 +}
11677 +
11678 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11679 +                                    aufs_bindex_t btgt)
11680 +{
11681 +       int err;
11682 +
11683 +       memset(w, 0, sizeof(*w));
11684 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11685 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11686 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11687 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11688 +       w->btgt = btgt;
11689 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11690 +
11691 +       err = -ENOMEM;
11692 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11693 +       if (unlikely(!w->fdata)) {
11694 +               AuTraceErr(err);
11695 +               goto out;
11696 +       }
11697 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11698 +       err = 0;
11699 +
11700 +out:
11701 +       return err;
11702 +}
11703 +
11704 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11705 +{
11706 +       au_kfree_rcu(w->fdata);
11707 +}
11708 +
11709 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11710 +                               struct au_drinfo_store *w)
11711 +{
11712 +       struct au_drinfo_rev_elm *elm;
11713 +       struct inode *h_dir, *delegated;
11714 +       int err, nelm;
11715 +       struct path infopath = {
11716 +               .mnt = w->h_ppath.mnt
11717 +       };
11718 +
11719 +       h_dir = d_inode(w->h_ppath.dentry);
11720 +       IMustLock(h_dir);
11721 +
11722 +       err = 0;
11723 +       elm = rev->elm;
11724 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11725 +               AuDebugOn(elm->created && elm->info_last);
11726 +               if (elm->created) {
11727 +                       AuDbg("here\n");
11728 +                       delegated = NULL;
11729 +                       infopath.dentry = elm->info_dentry;
11730 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11731 +                                          !w->no_sio);
11732 +                       AuTraceErr(err);
11733 +                       if (unlikely(err == -EWOULDBLOCK))
11734 +                               iput(delegated);
11735 +                       dput(elm->info_dentry);
11736 +               } else if (elm->info_last) {
11737 +                       AuDbg("here\n");
11738 +                       w->fdata->drinfo = *elm->info_last;
11739 +                       memcpy(w->fdata->drinfo.oldname,
11740 +                              elm->info_last->oldname,
11741 +                              elm->info_last->oldnamelen);
11742 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11743 +                       au_kfree_rcu(elm->info_last);
11744 +               }
11745 +               if (unlikely(err))
11746 +                       AuIOErr("%d, %s\n", err, w->whname);
11747 +               /* go on even if err */
11748 +       }
11749 +}
11750 +
11751 +/* caller has to call au_dr_rename_fin() later */
11752 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11753 +                          struct qstr *dst_name, void *_rev)
11754 +{
11755 +       int err, sz, nelm;
11756 +       aufs_bindex_t bindex, btail;
11757 +       struct au_drinfo_store work;
11758 +       struct au_drinfo_rev *rev, **p;
11759 +       struct au_drinfo_rev_elm *elm;
11760 +       struct super_block *sb;
11761 +       struct au_branch *br;
11762 +       struct au_hinode *hdir;
11763 +
11764 +       err = au_drinfo_store_work_init(&work, btgt);
11765 +       AuTraceErr(err);
11766 +       if (unlikely(err))
11767 +               goto out;
11768 +
11769 +       err = -ENOMEM;
11770 +       btail = au_dbtaildir(dentry);
11771 +       nelm = btail - btgt;
11772 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11773 +       rev = kcalloc(1, sz, GFP_NOFS);
11774 +       if (unlikely(!rev)) {
11775 +               AuTraceErr(err);
11776 +               goto out_args;
11777 +       }
11778 +       rev->nelm = nelm;
11779 +       elm = rev->elm;
11780 +       p = _rev;
11781 +       *p = rev;
11782 +
11783 +       err = 0;
11784 +       sb = dentry->d_sb;
11785 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11786 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11787 +       hdir = au_hi(d_inode(dentry), btgt);
11788 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11789 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11790 +               work.h_dentry = au_h_dptr(dentry, bindex);
11791 +               if (!work.h_dentry)
11792 +                       continue;
11793 +
11794 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11795 +                                         &work.allocated);
11796 +               AuTraceErr(err);
11797 +               if (unlikely(err))
11798 +                       break;
11799 +
11800 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11801 +               br = au_sbr(sb, bindex);
11802 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11803 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11804 +                                                work.infonamelen);
11805 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11806 +                     work.whnamelen, work.whname,
11807 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11808 +                     work.fdata->drinfo.oldnamelen,
11809 +                     work.fdata->drinfo.oldname);
11810 +
11811 +               err = au_drinfo_store_sio(&work, elm);
11812 +               AuTraceErr(err);
11813 +               if (unlikely(err))
11814 +                       break;
11815 +       }
11816 +       if (unlikely(err)) {
11817 +               /* revert all drinfo */
11818 +               au_drinfo_store_rev(rev, &work);
11819 +               au_kfree_try_rcu(rev);
11820 +               *p = NULL;
11821 +       }
11822 +       au_hn_inode_unlock(hdir);
11823 +
11824 +out_args:
11825 +       au_drinfo_store_work_fin(&work);
11826 +out:
11827 +       return err;
11828 +}
11829 +
11830 +/* ---------------------------------------------------------------------- */
11831 +
11832 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11833 +                struct qstr *dst_name, void *_rev)
11834 +{
11835 +       int err, already;
11836 +       ino_t ino;
11837 +       struct super_block *sb;
11838 +       struct au_branch *br;
11839 +       struct au_dr_br *dr;
11840 +       struct dentry *h_dentry;
11841 +       struct inode *h_inode;
11842 +       struct au_dr_hino *ent;
11843 +       struct au_drinfo_rev *rev, **p;
11844 +
11845 +       AuDbg("bindex %d\n", bindex);
11846 +
11847 +       err = -ENOMEM;
11848 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11849 +       if (unlikely(!ent))
11850 +               goto out;
11851 +
11852 +       sb = src->d_sb;
11853 +       br = au_sbr(sb, bindex);
11854 +       dr = &br->br_dirren;
11855 +       h_dentry = au_h_dptr(src, bindex);
11856 +       h_inode = d_inode(h_dentry);
11857 +       ino = h_inode->i_ino;
11858 +       ent->dr_h_ino = ino;
11859 +       already = au_dr_hino_test_add(dr, ino, ent);
11860 +       AuDbg("b%d, hi%llu, already %d\n",
11861 +             bindex, (unsigned long long)ino, already);
11862 +
11863 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11864 +       AuTraceErr(err);
11865 +       if (!err) {
11866 +               p = _rev;
11867 +               rev = *p;
11868 +               rev->already = already;
11869 +               goto out; /* success */
11870 +       }
11871 +
11872 +       /* revert */
11873 +       if (!already)
11874 +               au_dr_hino_del(dr, ent);
11875 +       au_kfree_rcu(ent);
11876 +
11877 +out:
11878 +       AuTraceErr(err);
11879 +       return err;
11880 +}
11881 +
11882 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11883 +{
11884 +       struct au_drinfo_rev *rev;
11885 +       struct au_drinfo_rev_elm *elm;
11886 +       int nelm;
11887 +
11888 +       rev = _rev;
11889 +       elm = rev->elm;
11890 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11891 +               dput(elm->info_dentry);
11892 +               au_kfree_rcu(elm->info_last);
11893 +       }
11894 +       au_kfree_try_rcu(rev);
11895 +}
11896 +
11897 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11898 +{
11899 +       int err;
11900 +       struct au_drinfo_store work;
11901 +       struct au_drinfo_rev *rev = _rev;
11902 +       struct super_block *sb;
11903 +       struct au_branch *br;
11904 +       struct inode *h_inode;
11905 +       struct au_dr_br *dr;
11906 +       struct au_dr_hino *ent;
11907 +
11908 +       err = au_drinfo_store_work_init(&work, btgt);
11909 +       if (unlikely(err))
11910 +               goto out;
11911 +
11912 +       sb = src->d_sb;
11913 +       br = au_sbr(sb, btgt);
11914 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11915 +       work.h_ppath.mnt = au_br_mnt(br);
11916 +       au_drinfo_store_rev(rev, &work);
11917 +       au_drinfo_store_work_fin(&work);
11918 +       if (rev->already)
11919 +               goto out;
11920 +
11921 +       dr = &br->br_dirren;
11922 +       h_inode = d_inode(work.h_ppath.dentry);
11923 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11924 +       BUG_ON(!ent);
11925 +       au_dr_hino_del(dr, ent);
11926 +       au_kfree_rcu(ent);
11927 +
11928 +out:
11929 +       au_kfree_try_rcu(rev);
11930 +       if (unlikely(err))
11931 +               pr_err("failed to remove dirren info\n");
11932 +}
11933 +
11934 +/* ---------------------------------------------------------------------- */
11935 +
11936 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11937 +                                          char *whname, int whnamelen,
11938 +                                          struct dentry **info_dentry)
11939 +{
11940 +       struct au_drinfo *drinfo;
11941 +       struct file *f;
11942 +       struct inode *h_dir;
11943 +       struct path infopath;
11944 +       int unlocked;
11945 +
11946 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11947 +
11948 +       *info_dentry = NULL;
11949 +       drinfo = NULL;
11950 +       unlocked = 0;
11951 +       h_dir = d_inode(h_ppath->dentry);
11952 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11953 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11954 +                                              whnamelen);
11955 +       if (IS_ERR(infopath.dentry)) {
11956 +               drinfo = (void *)infopath.dentry;
11957 +               goto out;
11958 +       }
11959 +
11960 +       if (d_is_negative(infopath.dentry))
11961 +               goto out_dput; /* success */
11962 +
11963 +       infopath.mnt = h_ppath->mnt;
11964 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11965 +       inode_unlock_shared(h_dir);
11966 +       unlocked = 1;
11967 +       if (IS_ERR(f)) {
11968 +               drinfo = (void *)f;
11969 +               goto out_dput;
11970 +       }
11971 +
11972 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11973 +       if (IS_ERR_OR_NULL(drinfo))
11974 +               goto out_fput;
11975 +
11976 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11977 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11978 +
11979 +out_fput:
11980 +       fput(f);
11981 +out_dput:
11982 +       dput(infopath.dentry);
11983 +out:
11984 +       if (!unlocked)
11985 +               inode_unlock_shared(h_dir);
11986 +       AuTraceErrPtr(drinfo);
11987 +       return drinfo;
11988 +}
11989 +
11990 +struct au_drinfo_do_load_args {
11991 +       struct au_drinfo **drinfop;
11992 +       struct path *h_ppath;
11993 +       char *whname;
11994 +       int whnamelen;
11995 +       struct dentry **info_dentry;
11996 +};
11997 +
11998 +static void au_call_drinfo_do_load(void *args)
11999 +{
12000 +       struct au_drinfo_do_load_args *a = args;
12001 +
12002 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
12003 +                                       a->info_dentry);
12004 +}
12005 +
12006 +struct au_drinfo_load {
12007 +       struct path h_ppath;
12008 +       struct qstr *qname;
12009 +       unsigned char no_sio;
12010 +
12011 +       aufs_bindex_t ninfo;
12012 +       struct au_drinfo **drinfo;
12013 +};
12014 +
12015 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
12016 +                         struct au_branch *br)
12017 +{
12018 +       int err, wkq_err, whnamelen, e;
12019 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
12020 +               = AUFS_WH_DR_INFO_PFX;
12021 +       struct au_drinfo *drinfo;
12022 +       struct qstr oldname;
12023 +       struct inode *h_dir, *delegated;
12024 +       struct dentry *info_dentry;
12025 +       struct path infopath;
12026 +
12027 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
12028 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
12029 +                                   sizeof(whname) - whnamelen);
12030 +       if (w->no_sio)
12031 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
12032 +                                          &info_dentry);
12033 +       else {
12034 +               struct au_drinfo_do_load_args args = {
12035 +                       .drinfop        = &drinfo,
12036 +                       .h_ppath        = &w->h_ppath,
12037 +                       .whname         = whname,
12038 +                       .whnamelen      = whnamelen,
12039 +                       .info_dentry    = &info_dentry
12040 +               };
12041 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
12042 +               if (unlikely(wkq_err))
12043 +                       drinfo = ERR_PTR(wkq_err);
12044 +       }
12045 +       err = PTR_ERR(drinfo);
12046 +       if (IS_ERR_OR_NULL(drinfo))
12047 +               goto out;
12048 +
12049 +       err = 0;
12050 +       oldname.len = drinfo->oldnamelen;
12051 +       oldname.name = drinfo->oldname;
12052 +       if (au_qstreq(w->qname, &oldname)) {
12053 +               /* the name is renamed back */
12054 +               au_kfree_rcu(drinfo);
12055 +               drinfo = NULL;
12056 +
12057 +               infopath.dentry = info_dentry;
12058 +               infopath.mnt = w->h_ppath.mnt;
12059 +               h_dir = d_inode(w->h_ppath.dentry);
12060 +               delegated = NULL;
12061 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12062 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12063 +               inode_unlock(h_dir);
12064 +               if (unlikely(e))
12065 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12066 +               if (unlikely(e == -EWOULDBLOCK))
12067 +                       iput(delegated);
12068 +       }
12069 +       au_kfree_rcu(w->drinfo[bindex]);
12070 +       w->drinfo[bindex] = drinfo;
12071 +       dput(info_dentry);
12072 +
12073 +out:
12074 +       AuTraceErr(err);
12075 +       return err;
12076 +}
12077 +
12078 +/* ---------------------------------------------------------------------- */
12079 +
12080 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12081 +{
12082 +       struct au_drinfo **p = drinfo;
12083 +
12084 +       while (n-- > 0)
12085 +               au_kfree_rcu(*drinfo++);
12086 +       au_kfree_try_rcu(p);
12087 +}
12088 +
12089 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12090 +              aufs_bindex_t btgt)
12091 +{
12092 +       int err, ninfo;
12093 +       struct au_drinfo_load w;
12094 +       aufs_bindex_t bindex, bbot;
12095 +       struct au_branch *br;
12096 +       struct inode *h_dir;
12097 +       struct au_dr_hino *ent;
12098 +       struct super_block *sb;
12099 +
12100 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12101 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12102 +             AuLNPair(&lkup->whname), btgt);
12103 +
12104 +       sb = dentry->d_sb;
12105 +       bbot = au_sbbot(sb);
12106 +       w.ninfo = bbot + 1;
12107 +       if (!lkup->dirren.drinfo) {
12108 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12109 +                                             sizeof(*lkup->dirren.drinfo),
12110 +                                             GFP_NOFS);
12111 +               if (unlikely(!lkup->dirren.drinfo)) {
12112 +                       err = -ENOMEM;
12113 +                       goto out;
12114 +               }
12115 +               lkup->dirren.ninfo = w.ninfo;
12116 +       }
12117 +       w.drinfo = lkup->dirren.drinfo;
12118 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12119 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12120 +       AuDebugOn(!w.h_ppath.dentry);
12121 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12122 +       w.qname = &dentry->d_name;
12123 +
12124 +       ninfo = 0;
12125 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12126 +               br = au_sbr(sb, bindex);
12127 +               err = au_drinfo_load(&w, bindex, br);
12128 +               if (unlikely(err))
12129 +                       goto out_free;
12130 +               if (w.drinfo[bindex])
12131 +                       ninfo++;
12132 +       }
12133 +       if (!ninfo) {
12134 +               br = au_sbr(sb, btgt);
12135 +               h_dir = d_inode(w.h_ppath.dentry);
12136 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12137 +               AuDebugOn(!ent);
12138 +               au_dr_hino_del(&br->br_dirren, ent);
12139 +               au_kfree_rcu(ent);
12140 +       }
12141 +       goto out; /* success */
12142 +
12143 +out_free:
12144 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12145 +       lkup->dirren.ninfo = 0;
12146 +       lkup->dirren.drinfo = NULL;
12147 +out:
12148 +       AuTraceErr(err);
12149 +       return err;
12150 +}
12151 +
12152 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12153 +{
12154 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12155 +}
12156 +
12157 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12158 +{
12159 +       int err;
12160 +       struct au_drinfo *drinfo;
12161 +
12162 +       err = 0;
12163 +       if (!lkup->dirren.drinfo)
12164 +               goto out;
12165 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12166 +       drinfo = lkup->dirren.drinfo[btgt];
12167 +       if (!drinfo)
12168 +               goto out;
12169 +
12170 +       au_kfree_try_rcu(lkup->whname.name);
12171 +       lkup->whname.name = NULL;
12172 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12173 +       lkup->dirren.dr_name.name = drinfo->oldname;
12174 +       lkup->name = &lkup->dirren.dr_name;
12175 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12176 +       if (!err)
12177 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12178 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12179 +                     btgt);
12180 +
12181 +out:
12182 +       AuTraceErr(err);
12183 +       return err;
12184 +}
12185 +
12186 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12187 +                    ino_t h_ino)
12188 +{
12189 +       int match;
12190 +       struct au_drinfo *drinfo;
12191 +
12192 +       match = 1;
12193 +       if (!lkup->dirren.drinfo)
12194 +               goto out;
12195 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12196 +       drinfo = lkup->dirren.drinfo[bindex];
12197 +       if (!drinfo)
12198 +               goto out;
12199 +
12200 +       match = (drinfo->ino == h_ino);
12201 +       AuDbg("match %d\n", match);
12202 +
12203 +out:
12204 +       return match;
12205 +}
12206 +
12207 +/* ---------------------------------------------------------------------- */
12208 +
12209 +int au_dr_opt_set(struct super_block *sb)
12210 +{
12211 +       int err;
12212 +       aufs_bindex_t bindex, bbot;
12213 +       struct au_branch *br;
12214 +
12215 +       err = 0;
12216 +       bbot = au_sbbot(sb);
12217 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12218 +               br = au_sbr(sb, bindex);
12219 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12220 +       }
12221 +
12222 +       return err;
12223 +}
12224 +
12225 +int au_dr_opt_flush(struct super_block *sb)
12226 +{
12227 +       int err;
12228 +       aufs_bindex_t bindex, bbot;
12229 +       struct au_branch *br;
12230 +
12231 +       err = 0;
12232 +       bbot = au_sbbot(sb);
12233 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12234 +               br = au_sbr(sb, bindex);
12235 +               if (au_br_writable(br->br_perm))
12236 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12237 +       }
12238 +
12239 +       return err;
12240 +}
12241 +
12242 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12243 +{
12244 +       int err;
12245 +       aufs_bindex_t bindex, bbot;
12246 +       struct au_branch *br;
12247 +
12248 +       err = 0;
12249 +       if (!no_flush) {
12250 +               err = au_dr_opt_flush(sb);
12251 +               if (unlikely(err))
12252 +                       goto out;
12253 +       }
12254 +
12255 +       bbot = au_sbbot(sb);
12256 +       for (bindex = 0; bindex <= bbot; bindex++) {
12257 +               br = au_sbr(sb, bindex);
12258 +               au_dr_hino_free(&br->br_dirren);
12259 +       }
12260 +
12261 +out:
12262 +       return err;
12263 +}
12264 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12265 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12266 +++ linux/fs/aufs/dirren.h      2020-01-27 10:57:18.168871450 +0100
12267 @@ -0,0 +1,140 @@
12268 +/* SPDX-License-Identifier: GPL-2.0 */
12269 +/*
12270 + * Copyright (C) 2017-2020 Junjiro R. Okajima
12271 + *
12272 + * This program, aufs is free software; you can redistribute it and/or modify
12273 + * it under the terms of the GNU General Public License as published by
12274 + * the Free Software Foundation; either version 2 of the License, or
12275 + * (at your option) any later version.
12276 + *
12277 + * This program is distributed in the hope that it will be useful,
12278 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12279 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12280 + * GNU General Public License for more details.
12281 + *
12282 + * You should have received a copy of the GNU General Public License
12283 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12284 + */
12285 +
12286 +/*
12287 + * renamed dir info
12288 + */
12289 +
12290 +#ifndef __AUFS_DIRREN_H__
12291 +#define __AUFS_DIRREN_H__
12292 +
12293 +#ifdef __KERNEL__
12294 +
12295 +#include <linux/dcache.h>
12296 +#include <linux/statfs.h>
12297 +#include <linux/uuid.h>
12298 +#include "hbl.h"
12299 +
12300 +#define AuDirren_NHASH 100
12301 +
12302 +#ifdef CONFIG_AUFS_DIRREN
12303 +enum au_brid_type {
12304 +       AuBrid_Unset,
12305 +       AuBrid_UUID,
12306 +       AuBrid_FSID,
12307 +       AuBrid_DEV
12308 +};
12309 +
12310 +struct au_dr_brid {
12311 +       enum au_brid_type       type;
12312 +       union {
12313 +               uuid_t  uuid;   /* unimplemented yet */
12314 +               fsid_t  fsid;
12315 +               dev_t   dev;
12316 +       };
12317 +};
12318 +
12319 +/* 20 is the max digits length of ulong 64 */
12320 +/* brid-type "_" uuid "_" inum */
12321 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12322 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12323 +
12324 +struct au_dr_hino {
12325 +       struct hlist_bl_node    dr_hnode;
12326 +       ino_t                   dr_h_ino;
12327 +};
12328 +
12329 +struct au_dr_br {
12330 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12331 +       struct au_dr_brid       dr_brid;
12332 +};
12333 +
12334 +struct au_dr_lookup {
12335 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12336 +       struct qstr             dr_name; /* subset of dr_info */
12337 +       aufs_bindex_t           ninfo;
12338 +       struct au_drinfo        **drinfo;
12339 +};
12340 +#else
12341 +struct au_dr_hino;
12342 +/* empty */
12343 +struct au_dr_br { };
12344 +struct au_dr_lookup { };
12345 +#endif
12346 +
12347 +/* ---------------------------------------------------------------------- */
12348 +
12349 +struct au_branch;
12350 +struct au_do_lookup_args;
12351 +struct au_hinode;
12352 +#ifdef CONFIG_AUFS_DIRREN
12353 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12354 +                       struct au_dr_hino *add_ent);
12355 +void au_dr_hino_free(struct au_dr_br *dr);
12356 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12357 +                 const struct path *path);
12358 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12359 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12360 +                struct qstr *dst_name, void *_rev);
12361 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12362 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12363 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12364 +              aufs_bindex_t bindex);
12365 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12366 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12367 +                    ino_t h_ino);
12368 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12369 +int au_dr_opt_set(struct super_block *sb);
12370 +int au_dr_opt_flush(struct super_block *sb);
12371 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12372 +#else
12373 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12374 +          struct au_dr_hino *add_ent);
12375 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12376 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12377 +          const struct path *path);
12378 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12379 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12380 +          struct qstr *dst_name, void *_rev);
12381 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12382 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12383 +          void *rev);
12384 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12385 +          aufs_bindex_t bindex);
12386 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12387 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12388 +          aufs_bindex_t bindex, ino_t h_ino);
12389 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12390 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12391 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12392 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12393 +#endif
12394 +
12395 +/* ---------------------------------------------------------------------- */
12396 +
12397 +#ifdef CONFIG_AUFS_DIRREN
12398 +static inline int au_dr_ihash(ino_t h_ino)
12399 +{
12400 +       return h_ino % AuDirren_NHASH;
12401 +}
12402 +#else
12403 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12404 +#endif
12405 +
12406 +#endif /* __KERNEL__ */
12407 +#endif /* __AUFS_DIRREN_H__ */
12408 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12409 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12410 +++ linux/fs/aufs/dynop.c       2020-01-27 10:57:18.168871450 +0100
12411 @@ -0,0 +1,367 @@
12412 +// SPDX-License-Identifier: GPL-2.0
12413 +/*
12414 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12415 + *
12416 + * This program, aufs is free software; you can redistribute it and/or modify
12417 + * it under the terms of the GNU General Public License as published by
12418 + * the Free Software Foundation; either version 2 of the License, or
12419 + * (at your option) any later version.
12420 + *
12421 + * This program is distributed in the hope that it will be useful,
12422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12424 + * GNU General Public License for more details.
12425 + *
12426 + * You should have received a copy of the GNU General Public License
12427 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12428 + */
12429 +
12430 +/*
12431 + * dynamically customizable operations for regular files
12432 + */
12433 +
12434 +#include "aufs.h"
12435 +
12436 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12437 +
12438 +/*
12439 + * How large will these lists be?
12440 + * Usually just a few elements, 20-30 at most for each, I guess.
12441 + */
12442 +static struct hlist_bl_head dynop[AuDyLast];
12443 +
12444 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12445 +                                    const void *h_op)
12446 +{
12447 +       struct au_dykey *key, *tmp;
12448 +       struct hlist_bl_node *pos;
12449 +
12450 +       key = NULL;
12451 +       hlist_bl_lock(hbl);
12452 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12453 +               if (tmp->dk_op.dy_hop == h_op) {
12454 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12455 +                               key = tmp;
12456 +                       break;
12457 +               }
12458 +       hlist_bl_unlock(hbl);
12459 +
12460 +       return key;
12461 +}
12462 +
12463 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12464 +{
12465 +       struct au_dykey **k, *found;
12466 +       const void *h_op = key->dk_op.dy_hop;
12467 +       int i;
12468 +
12469 +       found = NULL;
12470 +       k = br->br_dykey;
12471 +       for (i = 0; i < AuBrDynOp; i++)
12472 +               if (k[i]) {
12473 +                       if (k[i]->dk_op.dy_hop == h_op) {
12474 +                               found = k[i];
12475 +                               break;
12476 +                       }
12477 +               } else
12478 +                       break;
12479 +       if (!found) {
12480 +               spin_lock(&br->br_dykey_lock);
12481 +               for (; i < AuBrDynOp; i++)
12482 +                       if (k[i]) {
12483 +                               if (k[i]->dk_op.dy_hop == h_op) {
12484 +                                       found = k[i];
12485 +                                       break;
12486 +                               }
12487 +                       } else {
12488 +                               k[i] = key;
12489 +                               break;
12490 +                       }
12491 +               spin_unlock(&br->br_dykey_lock);
12492 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12493 +       }
12494 +
12495 +       return found;
12496 +}
12497 +
12498 +/* kref_get() if @key is already added */
12499 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12500 +{
12501 +       struct au_dykey *tmp, *found;
12502 +       struct hlist_bl_node *pos;
12503 +       const void *h_op = key->dk_op.dy_hop;
12504 +
12505 +       found = NULL;
12506 +       hlist_bl_lock(hbl);
12507 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12508 +               if (tmp->dk_op.dy_hop == h_op) {
12509 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12510 +                               found = tmp;
12511 +                       break;
12512 +               }
12513 +       if (!found)
12514 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12515 +       hlist_bl_unlock(hbl);
12516 +
12517 +       if (!found)
12518 +               DyPrSym(key);
12519 +       return found;
12520 +}
12521 +
12522 +static void dy_free_rcu(struct rcu_head *rcu)
12523 +{
12524 +       struct au_dykey *key;
12525 +
12526 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12527 +       DyPrSym(key);
12528 +       kfree(key);
12529 +}
12530 +
12531 +static void dy_free(struct kref *kref)
12532 +{
12533 +       struct au_dykey *key;
12534 +       struct hlist_bl_head *hbl;
12535 +
12536 +       key = container_of(kref, struct au_dykey, dk_kref);
12537 +       hbl = dynop + key->dk_op.dy_type;
12538 +       au_hbl_del(&key->dk_hnode, hbl);
12539 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12540 +}
12541 +
12542 +void au_dy_put(struct au_dykey *key)
12543 +{
12544 +       kref_put(&key->dk_kref, dy_free);
12545 +}
12546 +
12547 +/* ---------------------------------------------------------------------- */
12548 +
12549 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12550 +
12551 +#ifdef CONFIG_AUFS_DEBUG
12552 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12553 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12554 +#else
12555 +#define DyDbgDeclare(cnt)      do {} while (0)
12556 +#define DyDbgInc(cnt)          do {} while (0)
12557 +#endif
12558 +
12559 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12560 +       DyDbgInc(cnt);                                                  \
12561 +       if (h_op->func) {                                               \
12562 +               if (src.func)                                           \
12563 +                       dst.func = src.func;                            \
12564 +               else                                                    \
12565 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12566 +       }                                                               \
12567 +} while (0)
12568 +
12569 +#define DySetForce(func, dst, src) do {                \
12570 +       AuDebugOn(!src.func);                   \
12571 +       DyDbgInc(cnt);                          \
12572 +       dst.func = src.func;                    \
12573 +} while (0)
12574 +
12575 +#define DySetAop(func) \
12576 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12577 +#define DySetAopForce(func) \
12578 +       DySetForce(func, dyaop->da_op, aufs_aop)
12579 +
12580 +static void dy_aop(struct au_dykey *key, const void *h_op,
12581 +                  struct super_block *h_sb __maybe_unused)
12582 +{
12583 +       struct au_dyaop *dyaop = (void *)key;
12584 +       const struct address_space_operations *h_aop = h_op;
12585 +       DyDbgDeclare(cnt);
12586 +
12587 +       AuDbg("%s\n", au_sbtype(h_sb));
12588 +
12589 +       DySetAop(writepage);
12590 +       DySetAopForce(readpage);        /* force */
12591 +       DySetAop(writepages);
12592 +       DySetAop(set_page_dirty);
12593 +       DySetAop(readpages);
12594 +       DySetAop(write_begin);
12595 +       DySetAop(write_end);
12596 +       DySetAop(bmap);
12597 +       DySetAop(invalidatepage);
12598 +       DySetAop(releasepage);
12599 +       DySetAop(freepage);
12600 +       /* this one will be changed according to an aufs mount option */
12601 +       DySetAop(direct_IO);
12602 +       DySetAop(migratepage);
12603 +       DySetAop(isolate_page);
12604 +       DySetAop(putback_page);
12605 +       DySetAop(launder_page);
12606 +       DySetAop(is_partially_uptodate);
12607 +       DySetAop(is_dirty_writeback);
12608 +       DySetAop(error_remove_page);
12609 +       DySetAop(swap_activate);
12610 +       DySetAop(swap_deactivate);
12611 +
12612 +       DyDbgSize(cnt, *h_aop);
12613 +}
12614 +
12615 +/* ---------------------------------------------------------------------- */
12616 +
12617 +static void dy_bug(struct kref *kref)
12618 +{
12619 +       BUG();
12620 +}
12621 +
12622 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12623 +{
12624 +       struct au_dykey *key, *old;
12625 +       struct hlist_bl_head *hbl;
12626 +       struct op {
12627 +               unsigned int sz;
12628 +               void (*set)(struct au_dykey *key, const void *h_op,
12629 +                           struct super_block *h_sb __maybe_unused);
12630 +       };
12631 +       static const struct op a[] = {
12632 +               [AuDy_AOP] = {
12633 +                       .sz     = sizeof(struct au_dyaop),
12634 +                       .set    = dy_aop
12635 +               }
12636 +       };
12637 +       const struct op *p;
12638 +
12639 +       hbl = dynop + op->dy_type;
12640 +       key = dy_gfind_get(hbl, op->dy_hop);
12641 +       if (key)
12642 +               goto out_add; /* success */
12643 +
12644 +       p = a + op->dy_type;
12645 +       key = kzalloc(p->sz, GFP_NOFS);
12646 +       if (unlikely(!key)) {
12647 +               key = ERR_PTR(-ENOMEM);
12648 +               goto out;
12649 +       }
12650 +
12651 +       key->dk_op.dy_hop = op->dy_hop;
12652 +       kref_init(&key->dk_kref);
12653 +       p->set(key, op->dy_hop, au_br_sb(br));
12654 +       old = dy_gadd(hbl, key);
12655 +       if (old) {
12656 +               au_kfree_rcu(key);
12657 +               key = old;
12658 +       }
12659 +
12660 +out_add:
12661 +       old = dy_bradd(br, key);
12662 +       if (old)
12663 +               /* its ref-count should never be zero here */
12664 +               kref_put(&key->dk_kref, dy_bug);
12665 +out:
12666 +       return key;
12667 +}
12668 +
12669 +/* ---------------------------------------------------------------------- */
12670 +/*
12671 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12672 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12673 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12674 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12675 + * See the aufs manual in detail.
12676 + */
12677 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12678 +{
12679 +       if (!do_dx)
12680 +               dyaop->da_op.direct_IO = NULL;
12681 +       else
12682 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12683 +}
12684 +
12685 +static struct au_dyaop *dy_aget(struct au_branch *br,
12686 +                               const struct address_space_operations *h_aop,
12687 +                               int do_dx)
12688 +{
12689 +       struct au_dyaop *dyaop;
12690 +       struct au_dynop op;
12691 +
12692 +       op.dy_type = AuDy_AOP;
12693 +       op.dy_haop = h_aop;
12694 +       dyaop = (void *)dy_get(&op, br);
12695 +       if (IS_ERR(dyaop))
12696 +               goto out;
12697 +       dy_adx(dyaop, do_dx);
12698 +
12699 +out:
12700 +       return dyaop;
12701 +}
12702 +
12703 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12704 +               struct inode *h_inode)
12705 +{
12706 +       int err, do_dx;
12707 +       struct super_block *sb;
12708 +       struct au_branch *br;
12709 +       struct au_dyaop *dyaop;
12710 +
12711 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12712 +       IiMustWriteLock(inode);
12713 +
12714 +       sb = inode->i_sb;
12715 +       br = au_sbr(sb, bindex);
12716 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12717 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12718 +       err = PTR_ERR(dyaop);
12719 +       if (IS_ERR(dyaop))
12720 +               /* unnecessary to call dy_fput() */
12721 +               goto out;
12722 +
12723 +       err = 0;
12724 +       inode->i_mapping->a_ops = &dyaop->da_op;
12725 +
12726 +out:
12727 +       return err;
12728 +}
12729 +
12730 +/*
12731 + * Is it safe to replace a_ops during the inode/file is in operation?
12732 + * Yes, I hope so.
12733 + */
12734 +int au_dy_irefresh(struct inode *inode)
12735 +{
12736 +       int err;
12737 +       aufs_bindex_t btop;
12738 +       struct inode *h_inode;
12739 +
12740 +       err = 0;
12741 +       if (S_ISREG(inode->i_mode)) {
12742 +               btop = au_ibtop(inode);
12743 +               h_inode = au_h_iptr(inode, btop);
12744 +               err = au_dy_iaop(inode, btop, h_inode);
12745 +       }
12746 +       return err;
12747 +}
12748 +
12749 +void au_dy_arefresh(int do_dx)
12750 +{
12751 +       struct hlist_bl_head *hbl;
12752 +       struct hlist_bl_node *pos;
12753 +       struct au_dykey *key;
12754 +
12755 +       hbl = dynop + AuDy_AOP;
12756 +       hlist_bl_lock(hbl);
12757 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12758 +               dy_adx((void *)key, do_dx);
12759 +       hlist_bl_unlock(hbl);
12760 +}
12761 +
12762 +/* ---------------------------------------------------------------------- */
12763 +
12764 +void __init au_dy_init(void)
12765 +{
12766 +       int i;
12767 +
12768 +       for (i = 0; i < AuDyLast; i++)
12769 +               INIT_HLIST_BL_HEAD(dynop + i);
12770 +}
12771 +
12772 +void au_dy_fin(void)
12773 +{
12774 +       int i;
12775 +
12776 +       for (i = 0; i < AuDyLast; i++)
12777 +               WARN_ON(!hlist_bl_empty(dynop + i));
12778 +}
12779 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12780 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12781 +++ linux/fs/aufs/dynop.h       2020-01-27 10:57:18.168871450 +0100
12782 @@ -0,0 +1,77 @@
12783 +/* SPDX-License-Identifier: GPL-2.0 */
12784 +/*
12785 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12786 + *
12787 + * This program, aufs is free software; you can redistribute it and/or modify
12788 + * it under the terms of the GNU General Public License as published by
12789 + * the Free Software Foundation; either version 2 of the License, or
12790 + * (at your option) any later version.
12791 + *
12792 + * This program is distributed in the hope that it will be useful,
12793 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12794 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12795 + * GNU General Public License for more details.
12796 + *
12797 + * You should have received a copy of the GNU General Public License
12798 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12799 + */
12800 +
12801 +/*
12802 + * dynamically customizable operations (for regular files only)
12803 + */
12804 +
12805 +#ifndef __AUFS_DYNOP_H__
12806 +#define __AUFS_DYNOP_H__
12807 +
12808 +#ifdef __KERNEL__
12809 +
12810 +#include <linux/fs.h>
12811 +#include <linux/kref.h>
12812 +
12813 +enum {AuDy_AOP, AuDyLast};
12814 +
12815 +struct au_dynop {
12816 +       int                                             dy_type;
12817 +       union {
12818 +               const void                              *dy_hop;
12819 +               const struct address_space_operations   *dy_haop;
12820 +       };
12821 +};
12822 +
12823 +struct au_dykey {
12824 +       union {
12825 +               struct hlist_bl_node    dk_hnode;
12826 +               struct rcu_head         dk_rcu;
12827 +       };
12828 +       struct au_dynop         dk_op;
12829 +
12830 +       /*
12831 +        * during I am in the branch local array, kref is gotten. when the
12832 +        * branch is removed, kref is put.
12833 +        */
12834 +       struct kref             dk_kref;
12835 +};
12836 +
12837 +/* stop unioning since their sizes are very different from each other */
12838 +struct au_dyaop {
12839 +       struct au_dykey                 da_key;
12840 +       struct address_space_operations da_op; /* not const */
12841 +};
12842 +/* make sure that 'struct au_dykey *' can be any type */
12843 +static_assert(!offsetof(struct au_dyaop, da_key));
12844 +
12845 +/* ---------------------------------------------------------------------- */
12846 +
12847 +/* dynop.c */
12848 +struct au_branch;
12849 +void au_dy_put(struct au_dykey *key);
12850 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12851 +               struct inode *h_inode);
12852 +int au_dy_irefresh(struct inode *inode);
12853 +void au_dy_arefresh(int do_dio);
12854 +
12855 +void __init au_dy_init(void);
12856 +void au_dy_fin(void);
12857 +
12858 +#endif /* __KERNEL__ */
12859 +#endif /* __AUFS_DYNOP_H__ */
12860 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12861 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12862 +++ linux/fs/aufs/export.c      2020-01-27 10:57:18.168871450 +0100
12863 @@ -0,0 +1,838 @@
12864 +// SPDX-License-Identifier: GPL-2.0
12865 +/*
12866 + * Copyright (C) 2005-2020 Junjiro R. Okajima
12867 + *
12868 + * This program, aufs is free software; you can redistribute it and/or modify
12869 + * it under the terms of the GNU General Public License as published by
12870 + * the Free Software Foundation; either version 2 of the License, or
12871 + * (at your option) any later version.
12872 + *
12873 + * This program is distributed in the hope that it will be useful,
12874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12876 + * GNU General Public License for more details.
12877 + *
12878 + * You should have received a copy of the GNU General Public License
12879 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12880 + */
12881 +
12882 +/*
12883 + * export via nfs
12884 + */
12885 +
12886 +#include <linux/exportfs.h>
12887 +#include <linux/fs_struct.h>
12888 +#include <linux/namei.h>
12889 +#include <linux/nsproxy.h>
12890 +#include <linux/random.h>
12891 +#include <linux/writeback.h>
12892 +#include "aufs.h"
12893 +
12894 +union conv {
12895 +#ifdef CONFIG_AUFS_INO_T_64
12896 +       __u32 a[2];
12897 +#else
12898 +       __u32 a[1];
12899 +#endif
12900 +       ino_t ino;
12901 +};
12902 +
12903 +static ino_t decode_ino(__u32 *a)
12904 +{
12905 +       union conv u;
12906 +
12907 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12908 +       u.a[0] = a[0];
12909 +#ifdef CONFIG_AUFS_INO_T_64
12910 +       u.a[1] = a[1];
12911 +#endif
12912 +       return u.ino;
12913 +}
12914 +
12915 +static void encode_ino(__u32 *a, ino_t ino)
12916 +{
12917 +       union conv u;
12918 +
12919 +       u.ino = ino;
12920 +       a[0] = u.a[0];
12921 +#ifdef CONFIG_AUFS_INO_T_64
12922 +       a[1] = u.a[1];
12923 +#endif
12924 +}
12925 +
12926 +/* NFS file handle */
12927 +enum {
12928 +       Fh_br_id,
12929 +       Fh_sigen,
12930 +#ifdef CONFIG_AUFS_INO_T_64
12931 +       /* support 64bit inode number */
12932 +       Fh_ino1,
12933 +       Fh_ino2,
12934 +       Fh_dir_ino1,
12935 +       Fh_dir_ino2,
12936 +#else
12937 +       Fh_ino1,
12938 +       Fh_dir_ino1,
12939 +#endif
12940 +       Fh_igen,
12941 +       Fh_h_type,
12942 +       Fh_tail,
12943 +
12944 +       Fh_ino = Fh_ino1,
12945 +       Fh_dir_ino = Fh_dir_ino1
12946 +};
12947 +
12948 +static int au_test_anon(struct dentry *dentry)
12949 +{
12950 +       /* note: read d_flags without d_lock */
12951 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12952 +}
12953 +
12954 +int au_test_nfsd(void)
12955 +{
12956 +       int ret;
12957 +       struct task_struct *tsk = current;
12958 +       char comm[sizeof(tsk->comm)];
12959 +
12960 +       ret = 0;
12961 +       if (tsk->flags & PF_KTHREAD) {
12962 +               get_task_comm(comm, tsk);
12963 +               ret = !strcmp(comm, "nfsd");
12964 +       }
12965 +
12966 +       return ret;
12967 +}
12968 +
12969 +/* ---------------------------------------------------------------------- */
12970 +/* inode generation external table */
12971 +
12972 +void au_xigen_inc(struct inode *inode)
12973 +{
12974 +       loff_t pos;
12975 +       ssize_t sz;
12976 +       __u32 igen;
12977 +       struct super_block *sb;
12978 +       struct au_sbinfo *sbinfo;
12979 +
12980 +       sb = inode->i_sb;
12981 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12982 +
12983 +       sbinfo = au_sbi(sb);
12984 +       pos = inode->i_ino;
12985 +       pos *= sizeof(igen);
12986 +       igen = inode->i_generation + 1;
12987 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
12988 +                        sizeof(igen), &pos);
12989 +       if (sz == sizeof(igen))
12990 +               return; /* success */
12991 +
12992 +       if (unlikely(sz >= 0))
12993 +               AuIOErr("xigen error (%zd)\n", sz);
12994 +}
12995 +
12996 +int au_xigen_new(struct inode *inode)
12997 +{
12998 +       int err;
12999 +       loff_t pos;
13000 +       ssize_t sz;
13001 +       struct super_block *sb;
13002 +       struct au_sbinfo *sbinfo;
13003 +       struct file *file;
13004 +
13005 +       err = 0;
13006 +       /* todo: dirty, at mount time */
13007 +       if (inode->i_ino == AUFS_ROOT_INO)
13008 +               goto out;
13009 +       sb = inode->i_sb;
13010 +       SiMustAnyLock(sb);
13011 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13012 +               goto out;
13013 +
13014 +       err = -EFBIG;
13015 +       pos = inode->i_ino;
13016 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
13017 +               AuIOErr1("too large i%lld\n", pos);
13018 +               goto out;
13019 +       }
13020 +       pos *= sizeof(inode->i_generation);
13021 +
13022 +       err = 0;
13023 +       sbinfo = au_sbi(sb);
13024 +       file = sbinfo->si_xigen;
13025 +       BUG_ON(!file);
13026 +
13027 +       if (vfsub_f_size_read(file)
13028 +           < pos + sizeof(inode->i_generation)) {
13029 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
13030 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
13031 +                                sizeof(inode->i_generation), &pos);
13032 +       } else
13033 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
13034 +                               sizeof(inode->i_generation), &pos);
13035 +       if (sz == sizeof(inode->i_generation))
13036 +               goto out; /* success */
13037 +
13038 +       err = sz;
13039 +       if (unlikely(sz >= 0)) {
13040 +               err = -EIO;
13041 +               AuIOErr("xigen error (%zd)\n", sz);
13042 +       }
13043 +
13044 +out:
13045 +       return err;
13046 +}
13047 +
13048 +int au_xigen_set(struct super_block *sb, struct path *path)
13049 +{
13050 +       int err;
13051 +       struct au_sbinfo *sbinfo;
13052 +       struct file *file;
13053 +
13054 +       SiMustWriteLock(sb);
13055 +
13056 +       sbinfo = au_sbi(sb);
13057 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13058 +       err = PTR_ERR(file);
13059 +       if (IS_ERR(file))
13060 +               goto out;
13061 +       err = 0;
13062 +       if (sbinfo->si_xigen)
13063 +               fput(sbinfo->si_xigen);
13064 +       sbinfo->si_xigen = file;
13065 +
13066 +out:
13067 +       AuTraceErr(err);
13068 +       return err;
13069 +}
13070 +
13071 +void au_xigen_clr(struct super_block *sb)
13072 +{
13073 +       struct au_sbinfo *sbinfo;
13074 +
13075 +       SiMustWriteLock(sb);
13076 +
13077 +       sbinfo = au_sbi(sb);
13078 +       if (sbinfo->si_xigen) {
13079 +               fput(sbinfo->si_xigen);
13080 +               sbinfo->si_xigen = NULL;
13081 +       }
13082 +}
13083 +
13084 +/* ---------------------------------------------------------------------- */
13085 +
13086 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13087 +                                   ino_t dir_ino)
13088 +{
13089 +       struct dentry *dentry, *d;
13090 +       struct inode *inode;
13091 +       unsigned int sigen;
13092 +
13093 +       dentry = NULL;
13094 +       inode = ilookup(sb, ino);
13095 +       if (!inode)
13096 +               goto out;
13097 +
13098 +       dentry = ERR_PTR(-ESTALE);
13099 +       sigen = au_sigen(sb);
13100 +       if (unlikely(au_is_bad_inode(inode)
13101 +                    || IS_DEADDIR(inode)
13102 +                    || sigen != au_iigen(inode, NULL)))
13103 +               goto out_iput;
13104 +
13105 +       dentry = NULL;
13106 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13107 +               dentry = d_find_alias(inode);
13108 +       else {
13109 +               spin_lock(&inode->i_lock);
13110 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13111 +                       spin_lock(&d->d_lock);
13112 +                       if (!au_test_anon(d)
13113 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13114 +                               dentry = dget_dlock(d);
13115 +                               spin_unlock(&d->d_lock);
13116 +                               break;
13117 +                       }
13118 +                       spin_unlock(&d->d_lock);
13119 +               }
13120 +               spin_unlock(&inode->i_lock);
13121 +       }
13122 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13123 +               /* need to refresh */
13124 +               dput(dentry);
13125 +               dentry = NULL;
13126 +       }
13127 +
13128 +out_iput:
13129 +       iput(inode);
13130 +out:
13131 +       AuTraceErrPtr(dentry);
13132 +       return dentry;
13133 +}
13134 +
13135 +/* ---------------------------------------------------------------------- */
13136 +
13137 +/* todo: dirty? */
13138 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13139 +
13140 +struct au_compare_mnt_args {
13141 +       /* input */
13142 +       struct super_block *sb;
13143 +
13144 +       /* output */
13145 +       struct vfsmount *mnt;
13146 +};
13147 +
13148 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13149 +{
13150 +       struct au_compare_mnt_args *a = arg;
13151 +
13152 +       if (mnt->mnt_sb != a->sb)
13153 +               return 0;
13154 +       a->mnt = mntget(mnt);
13155 +       return 1;
13156 +}
13157 +
13158 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13159 +{
13160 +       int err;
13161 +       struct path root;
13162 +       struct au_compare_mnt_args args = {
13163 +               .sb = sb
13164 +       };
13165 +
13166 +       get_fs_root(current->fs, &root);
13167 +       rcu_read_lock();
13168 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13169 +       rcu_read_unlock();
13170 +       path_put(&root);
13171 +       AuDebugOn(!err);
13172 +       AuDebugOn(!args.mnt);
13173 +       return args.mnt;
13174 +}
13175 +
13176 +struct au_nfsd_si_lock {
13177 +       unsigned int sigen;
13178 +       aufs_bindex_t bindex, br_id;
13179 +       unsigned char force_lock;
13180 +};
13181 +
13182 +static int si_nfsd_read_lock(struct super_block *sb,
13183 +                            struct au_nfsd_si_lock *nsi_lock)
13184 +{
13185 +       int err;
13186 +       aufs_bindex_t bindex;
13187 +
13188 +       si_read_lock(sb, AuLock_FLUSH);
13189 +
13190 +       /* branch id may be wrapped around */
13191 +       err = 0;
13192 +       bindex = au_br_index(sb, nsi_lock->br_id);
13193 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13194 +               goto out; /* success */
13195 +
13196 +       err = -ESTALE;
13197 +       bindex = -1;
13198 +       if (!nsi_lock->force_lock)
13199 +               si_read_unlock(sb);
13200 +
13201 +out:
13202 +       nsi_lock->bindex = bindex;
13203 +       return err;
13204 +}
13205 +
13206 +struct find_name_by_ino {
13207 +       struct dir_context ctx;
13208 +       int called, found;
13209 +       ino_t ino;
13210 +       char *name;
13211 +       int namelen;
13212 +};
13213 +
13214 +static int
13215 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13216 +                loff_t offset, u64 ino, unsigned int d_type)
13217 +{
13218 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13219 +                                                 ctx);
13220 +
13221 +       a->called++;
13222 +       if (a->ino != ino)
13223 +               return 0;
13224 +
13225 +       memcpy(a->name, name, namelen);
13226 +       a->namelen = namelen;
13227 +       a->found = 1;
13228 +       return 1;
13229 +}
13230 +
13231 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13232 +                                    struct au_nfsd_si_lock *nsi_lock)
13233 +{
13234 +       struct dentry *dentry, *parent;
13235 +       struct file *file;
13236 +       struct inode *dir;
13237 +       struct find_name_by_ino arg = {
13238 +               .ctx = {
13239 +                       .actor = find_name_by_ino
13240 +               }
13241 +       };
13242 +       int err;
13243 +
13244 +       parent = path->dentry;
13245 +       if (nsi_lock)
13246 +               si_read_unlock(parent->d_sb);
13247 +       file = vfsub_dentry_open(path, au_dir_roflags);
13248 +       dentry = (void *)file;
13249 +       if (IS_ERR(file))
13250 +               goto out;
13251 +
13252 +       dentry = ERR_PTR(-ENOMEM);
13253 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13254 +       if (unlikely(!arg.name))
13255 +               goto out_file;
13256 +       arg.ino = ino;
13257 +       arg.found = 0;
13258 +       do {
13259 +               arg.called = 0;
13260 +               /* smp_mb(); */
13261 +               err = vfsub_iterate_dir(file, &arg.ctx);
13262 +       } while (!err && !arg.found && arg.called);
13263 +       dentry = ERR_PTR(err);
13264 +       if (unlikely(err))
13265 +               goto out_name;
13266 +       /* instead of ENOENT */
13267 +       dentry = ERR_PTR(-ESTALE);
13268 +       if (!arg.found)
13269 +               goto out_name;
13270 +
13271 +       /* do not call vfsub_lkup_one() */
13272 +       dir = d_inode(parent);
13273 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13274 +       AuTraceErrPtr(dentry);
13275 +       if (IS_ERR(dentry))
13276 +               goto out_name;
13277 +       AuDebugOn(au_test_anon(dentry));
13278 +       if (unlikely(d_really_is_negative(dentry))) {
13279 +               dput(dentry);
13280 +               dentry = ERR_PTR(-ENOENT);
13281 +       }
13282 +
13283 +out_name:
13284 +       free_page((unsigned long)arg.name);
13285 +out_file:
13286 +       fput(file);
13287 +out:
13288 +       if (unlikely(nsi_lock
13289 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13290 +               if (!IS_ERR(dentry)) {
13291 +                       dput(dentry);
13292 +                       dentry = ERR_PTR(-ESTALE);
13293 +               }
13294 +       AuTraceErrPtr(dentry);
13295 +       return dentry;
13296 +}
13297 +
13298 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13299 +                                       ino_t dir_ino,
13300 +                                       struct au_nfsd_si_lock *nsi_lock)
13301 +{
13302 +       struct dentry *dentry;
13303 +       struct path path;
13304 +
13305 +       if (dir_ino != AUFS_ROOT_INO) {
13306 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13307 +               dentry = path.dentry;
13308 +               if (!path.dentry || IS_ERR(path.dentry))
13309 +                       goto out;
13310 +               AuDebugOn(au_test_anon(path.dentry));
13311 +       } else
13312 +               path.dentry = dget(sb->s_root);
13313 +
13314 +       path.mnt = au_mnt_get(sb);
13315 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13316 +       path_put(&path);
13317 +
13318 +out:
13319 +       AuTraceErrPtr(dentry);
13320 +       return dentry;
13321 +}
13322 +
13323 +/* ---------------------------------------------------------------------- */
13324 +
13325 +static int h_acceptable(void *expv, struct dentry *dentry)
13326 +{
13327 +       return 1;
13328 +}
13329 +
13330 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13331 +                          char *buf, int len, struct super_block *sb)
13332 +{
13333 +       char *p;
13334 +       int n;
13335 +       struct path path;
13336 +
13337 +       p = d_path(h_rootpath, buf, len);
13338 +       if (IS_ERR(p))
13339 +               goto out;
13340 +       n = strlen(p);
13341 +
13342 +       path.mnt = h_rootpath->mnt;
13343 +       path.dentry = h_parent;
13344 +       p = d_path(&path, buf, len);
13345 +       if (IS_ERR(p))
13346 +               goto out;
13347 +       if (n != 1)
13348 +               p += n;
13349 +
13350 +       path.mnt = au_mnt_get(sb);
13351 +       path.dentry = sb->s_root;
13352 +       p = d_path(&path, buf, len - strlen(p));
13353 +       mntput(path.mnt);
13354 +       if (IS_ERR(p))
13355 +               goto out;
13356 +       if (n != 1)
13357 +               p[strlen(p)] = '/';
13358 +
13359 +out:
13360 +       AuTraceErrPtr(p);
13361 +       return p;
13362 +}
13363 +
13364 +static
13365 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13366 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13367 +{
13368 +       struct dentry *dentry, *h_parent, *root;
13369 +       struct super_block *h_sb;
13370 +       char *pathname, *p;
13371 +       struct vfsmount *h_mnt;
13372 +       struct au_branch *br;
13373 +       int err;
13374 +       struct path path;
13375 +
13376 +       br = au_sbr(sb, nsi_lock->bindex);
13377 +       h_mnt = au_br_mnt(br);
13378 +       h_sb = h_mnt->mnt_sb;
13379 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13380 +       lockdep_off();
13381 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13382 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13383 +                                     h_acceptable, /*context*/NULL);
13384 +       lockdep_on();
13385 +       dentry = h_parent;
13386 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13387 +               AuWarn1("%s decode_fh failed, %ld\n",
13388 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13389 +               goto out;
13390 +       }
13391 +       dentry = NULL;
13392 +       if (unlikely(au_test_anon(h_parent))) {
13393 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13394 +                       au_sbtype(h_sb));
13395 +               goto out_h_parent;
13396 +       }
13397 +
13398 +       dentry = ERR_PTR(-ENOMEM);
13399 +       pathname = (void *)__get_free_page(GFP_NOFS);
13400 +       if (unlikely(!pathname))
13401 +               goto out_h_parent;
13402 +
13403 +       root = sb->s_root;
13404 +       path.mnt = h_mnt;
13405 +       di_read_lock_parent(root, !AuLock_IR);
13406 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13407 +       di_read_unlock(root, !AuLock_IR);
13408 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13409 +       dentry = (void *)p;
13410 +       if (IS_ERR(p))
13411 +               goto out_pathname;
13412 +
13413 +       si_read_unlock(sb);
13414 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13415 +       dentry = ERR_PTR(err);
13416 +       if (unlikely(err))
13417 +               goto out_relock;
13418 +
13419 +       dentry = ERR_PTR(-ENOENT);
13420 +       AuDebugOn(au_test_anon(path.dentry));
13421 +       if (unlikely(d_really_is_negative(path.dentry)))
13422 +               goto out_path;
13423 +
13424 +       if (ino != d_inode(path.dentry)->i_ino)
13425 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13426 +       else
13427 +               dentry = dget(path.dentry);
13428 +
13429 +out_path:
13430 +       path_put(&path);
13431 +out_relock:
13432 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13433 +               if (!IS_ERR(dentry)) {
13434 +                       dput(dentry);
13435 +                       dentry = ERR_PTR(-ESTALE);
13436 +               }
13437 +out_pathname:
13438 +       free_page((unsigned long)pathname);
13439 +out_h_parent:
13440 +       dput(h_parent);
13441 +out:
13442 +       AuTraceErrPtr(dentry);
13443 +       return dentry;
13444 +}
13445 +
13446 +/* ---------------------------------------------------------------------- */
13447 +
13448 +static struct dentry *
13449 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13450 +                 int fh_type)
13451 +{
13452 +       struct dentry *dentry;
13453 +       __u32 *fh = fid->raw;
13454 +       struct au_branch *br;
13455 +       ino_t ino, dir_ino;
13456 +       struct au_nfsd_si_lock nsi_lock = {
13457 +               .force_lock     = 0
13458 +       };
13459 +
13460 +       dentry = ERR_PTR(-ESTALE);
13461 +       /* it should never happen, but the file handle is unreliable */
13462 +       if (unlikely(fh_len < Fh_tail))
13463 +               goto out;
13464 +       nsi_lock.sigen = fh[Fh_sigen];
13465 +       nsi_lock.br_id = fh[Fh_br_id];
13466 +
13467 +       /* branch id may be wrapped around */
13468 +       br = NULL;
13469 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13470 +               goto out;
13471 +       nsi_lock.force_lock = 1;
13472 +
13473 +       /* is this inode still cached? */
13474 +       ino = decode_ino(fh + Fh_ino);
13475 +       /* it should never happen */
13476 +       if (unlikely(ino == AUFS_ROOT_INO))
13477 +               goto out_unlock;
13478 +
13479 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13480 +       dentry = decode_by_ino(sb, ino, dir_ino);
13481 +       if (IS_ERR(dentry))
13482 +               goto out_unlock;
13483 +       if (dentry)
13484 +               goto accept;
13485 +
13486 +       /* is the parent dir cached? */
13487 +       br = au_sbr(sb, nsi_lock.bindex);
13488 +       au_lcnt_inc(&br->br_nfiles);
13489 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13490 +       if (IS_ERR(dentry))
13491 +               goto out_unlock;
13492 +       if (dentry)
13493 +               goto accept;
13494 +
13495 +       /* lookup path */
13496 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13497 +       if (IS_ERR(dentry))
13498 +               goto out_unlock;
13499 +       if (unlikely(!dentry))
13500 +               /* todo?: make it ESTALE */
13501 +               goto out_unlock;
13502 +
13503 +accept:
13504 +       if (!au_digen_test(dentry, au_sigen(sb))
13505 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13506 +               goto out_unlock; /* success */
13507 +
13508 +       dput(dentry);
13509 +       dentry = ERR_PTR(-ESTALE);
13510 +out_unlock:
13511 +       if (br)
13512 +               au_lcnt_dec(&br->br_nfiles);
13513 +       si_read_unlock(sb);
13514 +out:
13515 +       AuTraceErrPtr(dentry);
13516 +       return dentry;
13517 +}
13518 +
13519 +#if 0 /* reserved for future use */
13520 +/* support subtreecheck option */
13521 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13522 +                                       int fh_len, int fh_type)
13523 +{
13524 +       struct dentry *parent;
13525 +       __u32 *fh = fid->raw;
13526 +       ino_t dir_ino;
13527 +
13528 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13529 +       parent = decode_by_ino(sb, dir_ino, 0);
13530 +       if (IS_ERR(parent))
13531 +               goto out;
13532 +       if (!parent)
13533 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13534 +                                       dir_ino, fh, fh_len);
13535 +
13536 +out:
13537 +       AuTraceErrPtr(parent);
13538 +       return parent;
13539 +}
13540 +#endif
13541 +
13542 +/* ---------------------------------------------------------------------- */
13543 +
13544 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13545 +                         struct inode *dir)
13546 +{
13547 +       int err;
13548 +       aufs_bindex_t bindex;
13549 +       struct super_block *sb, *h_sb;
13550 +       struct dentry *dentry, *parent, *h_parent;
13551 +       struct inode *h_dir;
13552 +       struct au_branch *br;
13553 +
13554 +       err = -ENOSPC;
13555 +       if (unlikely(*max_len <= Fh_tail)) {
13556 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13557 +               goto out;
13558 +       }
13559 +
13560 +       err = FILEID_ROOT;
13561 +       if (inode->i_ino == AUFS_ROOT_INO) {
13562 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13563 +               goto out;
13564 +       }
13565 +
13566 +       h_parent = NULL;
13567 +       sb = inode->i_sb;
13568 +       err = si_read_lock(sb, AuLock_FLUSH);
13569 +       if (unlikely(err))
13570 +               goto out;
13571 +
13572 +#ifdef CONFIG_AUFS_DEBUG
13573 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13574 +               AuWarn1("NFS-exporting requires xino\n");
13575 +#endif
13576 +       err = -EIO;
13577 +       parent = NULL;
13578 +       ii_read_lock_child(inode);
13579 +       bindex = au_ibtop(inode);
13580 +       if (!dir) {
13581 +               dentry = d_find_any_alias(inode);
13582 +               if (unlikely(!dentry))
13583 +                       goto out_unlock;
13584 +               AuDebugOn(au_test_anon(dentry));
13585 +               parent = dget_parent(dentry);
13586 +               dput(dentry);
13587 +               if (unlikely(!parent))
13588 +                       goto out_unlock;
13589 +               if (d_really_is_positive(parent))
13590 +                       dir = d_inode(parent);
13591 +       }
13592 +
13593 +       ii_read_lock_parent(dir);
13594 +       h_dir = au_h_iptr(dir, bindex);
13595 +       ii_read_unlock(dir);
13596 +       if (unlikely(!h_dir))
13597 +               goto out_parent;
13598 +       h_parent = d_find_any_alias(h_dir);
13599 +       if (unlikely(!h_parent))
13600 +               goto out_hparent;
13601 +
13602 +       err = -EPERM;
13603 +       br = au_sbr(sb, bindex);
13604 +       h_sb = au_br_sb(br);
13605 +       if (unlikely(!h_sb->s_export_op)) {
13606 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13607 +               goto out_hparent;
13608 +       }
13609 +
13610 +       fh[Fh_br_id] = br->br_id;
13611 +       fh[Fh_sigen] = au_sigen(sb);
13612 +       encode_ino(fh + Fh_ino, inode->i_ino);
13613 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13614 +       fh[Fh_igen] = inode->i_generation;
13615 +
13616 +       *max_len -= Fh_tail;
13617 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13618 +                                          max_len,
13619 +                                          /*connectable or subtreecheck*/0);
13620 +       err = fh[Fh_h_type];
13621 +       *max_len += Fh_tail;
13622 +       /* todo: macros? */
13623 +       if (err != FILEID_INVALID)
13624 +               err = 99;
13625 +       else
13626 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13627 +
13628 +out_hparent:
13629 +       dput(h_parent);
13630 +out_parent:
13631 +       dput(parent);
13632 +out_unlock:
13633 +       ii_read_unlock(inode);
13634 +       si_read_unlock(sb);
13635 +out:
13636 +       if (unlikely(err < 0))
13637 +               err = FILEID_INVALID;
13638 +       return err;
13639 +}
13640 +
13641 +/* ---------------------------------------------------------------------- */
13642 +
13643 +static int aufs_commit_metadata(struct inode *inode)
13644 +{
13645 +       int err;
13646 +       aufs_bindex_t bindex;
13647 +       struct super_block *sb;
13648 +       struct inode *h_inode;
13649 +       int (*f)(struct inode *inode);
13650 +
13651 +       sb = inode->i_sb;
13652 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13653 +       ii_write_lock_child(inode);
13654 +       bindex = au_ibtop(inode);
13655 +       AuDebugOn(bindex < 0);
13656 +       h_inode = au_h_iptr(inode, bindex);
13657 +
13658 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13659 +       if (f)
13660 +               err = f(h_inode);
13661 +       else {
13662 +               struct writeback_control wbc = {
13663 +                       .sync_mode      = WB_SYNC_ALL,
13664 +                       .nr_to_write    = 0 /* metadata only */
13665 +               };
13666 +
13667 +               err = sync_inode(h_inode, &wbc);
13668 +       }
13669 +
13670 +       au_cpup_attr_timesizes(inode);
13671 +       ii_write_unlock(inode);
13672 +       si_read_unlock(sb);
13673 +       return err;
13674 +}
13675 +
13676 +/* ---------------------------------------------------------------------- */
13677 +
13678 +static struct export_operations aufs_export_op = {
13679 +       .fh_to_dentry           = aufs_fh_to_dentry,
13680 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13681 +       .encode_fh              = aufs_encode_fh,
13682 +       .commit_metadata        = aufs_commit_metadata
13683 +};
13684 +
13685 +void au_export_init(struct super_block *sb)
13686 +{
13687 +       struct au_sbinfo *sbinfo;
13688 +       __u32 u;
13689 +
13690 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13691 +                        && IS_MODULE(CONFIG_EXPORTFS),
13692 +                        AUFS_NAME ": unsupported configuration "
13693 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13694 +
13695 +       sb->s_export_op = &aufs_export_op;
13696 +       sbinfo = au_sbi(sb);
13697 +       sbinfo->si_xigen = NULL;
13698 +       get_random_bytes(&u, sizeof(u));
13699 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13700 +       atomic_set(&sbinfo->si_xigen_next, u);
13701 +}
13702 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13703 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13704 +++ linux/fs/aufs/fhsm.c        2020-01-27 10:57:18.172204883 +0100
13705 @@ -0,0 +1,427 @@
13706 +// SPDX-License-Identifier: GPL-2.0
13707 +/*
13708 + * Copyright (C) 2011-2020 Junjiro R. Okajima
13709 + *
13710 + * This program, aufs is free software; you can redistribute it and/or modify
13711 + * it under the terms of the GNU General Public License as published by
13712 + * the Free Software Foundation; either version 2 of the License, or
13713 + * (at your option) any later version.
13714 + *
13715 + * This program is distributed in the hope that it will be useful,
13716 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13717 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13718 + * GNU General Public License for more details.
13719 + *
13720 + * You should have received a copy of the GNU General Public License
13721 + * along with this program; if not, write to the Free Software
13722 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13723 + */
13724 +
13725 +/*
13726 + * File-based Hierarchy Storage Management
13727 + */
13728 +
13729 +#include <linux/anon_inodes.h>
13730 +#include <linux/poll.h>
13731 +#include <linux/seq_file.h>
13732 +#include <linux/statfs.h>
13733 +#include "aufs.h"
13734 +
13735 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13736 +{
13737 +       struct au_sbinfo *sbinfo;
13738 +       struct au_fhsm *fhsm;
13739 +
13740 +       SiMustAnyLock(sb);
13741 +
13742 +       sbinfo = au_sbi(sb);
13743 +       fhsm = &sbinfo->si_fhsm;
13744 +       AuDebugOn(!fhsm);
13745 +       return fhsm->fhsm_bottom;
13746 +}
13747 +
13748 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13749 +{
13750 +       struct au_sbinfo *sbinfo;
13751 +       struct au_fhsm *fhsm;
13752 +
13753 +       SiMustWriteLock(sb);
13754 +
13755 +       sbinfo = au_sbi(sb);
13756 +       fhsm = &sbinfo->si_fhsm;
13757 +       AuDebugOn(!fhsm);
13758 +       fhsm->fhsm_bottom = bindex;
13759 +}
13760 +
13761 +/* ---------------------------------------------------------------------- */
13762 +
13763 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13764 +{
13765 +       struct au_br_fhsm *bf;
13766 +
13767 +       bf = br->br_fhsm;
13768 +       MtxMustLock(&bf->bf_lock);
13769 +
13770 +       return !bf->bf_readable
13771 +               || time_after(jiffies,
13772 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13773 +}
13774 +
13775 +/* ---------------------------------------------------------------------- */
13776 +
13777 +static void au_fhsm_notify(struct super_block *sb, int val)
13778 +{
13779 +       struct au_sbinfo *sbinfo;
13780 +       struct au_fhsm *fhsm;
13781 +
13782 +       SiMustAnyLock(sb);
13783 +
13784 +       sbinfo = au_sbi(sb);
13785 +       fhsm = &sbinfo->si_fhsm;
13786 +       if (au_fhsm_pid(fhsm)
13787 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13788 +               atomic_set(&fhsm->fhsm_readable, val);
13789 +               if (val)
13790 +                       wake_up(&fhsm->fhsm_wqh);
13791 +       }
13792 +}
13793 +
13794 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13795 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13796 +{
13797 +       int err;
13798 +       struct au_branch *br;
13799 +       struct au_br_fhsm *bf;
13800 +
13801 +       br = au_sbr(sb, bindex);
13802 +       AuDebugOn(au_br_rdonly(br));
13803 +       bf = br->br_fhsm;
13804 +       AuDebugOn(!bf);
13805 +
13806 +       if (do_lock)
13807 +               mutex_lock(&bf->bf_lock);
13808 +       else
13809 +               MtxMustLock(&bf->bf_lock);
13810 +
13811 +       /* sb->s_root for NFS is unreliable */
13812 +       err = au_br_stfs(br, &bf->bf_stfs);
13813 +       if (unlikely(err)) {
13814 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13815 +               goto out;
13816 +       }
13817 +
13818 +       bf->bf_jiffy = jiffies;
13819 +       bf->bf_readable = 1;
13820 +       if (do_notify)
13821 +               au_fhsm_notify(sb, /*val*/1);
13822 +       if (rstfs)
13823 +               *rstfs = bf->bf_stfs;
13824 +
13825 +out:
13826 +       if (do_lock)
13827 +               mutex_unlock(&bf->bf_lock);
13828 +       au_fhsm_notify(sb, /*val*/1);
13829 +
13830 +       return err;
13831 +}
13832 +
13833 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13834 +{
13835 +       int err;
13836 +       struct au_sbinfo *sbinfo;
13837 +       struct au_fhsm *fhsm;
13838 +       struct au_branch *br;
13839 +       struct au_br_fhsm *bf;
13840 +
13841 +       AuDbg("b%d, force %d\n", bindex, force);
13842 +       SiMustAnyLock(sb);
13843 +
13844 +       sbinfo = au_sbi(sb);
13845 +       fhsm = &sbinfo->si_fhsm;
13846 +       if (!au_ftest_si(sbinfo, FHSM)
13847 +           || fhsm->fhsm_bottom == bindex)
13848 +               return;
13849 +
13850 +       br = au_sbr(sb, bindex);
13851 +       bf = br->br_fhsm;
13852 +       AuDebugOn(!bf);
13853 +       mutex_lock(&bf->bf_lock);
13854 +       if (force
13855 +           || au_fhsm_pid(fhsm)
13856 +           || au_fhsm_test_jiffy(sbinfo, br))
13857 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13858 +                                 /*do_notify*/1);
13859 +       mutex_unlock(&bf->bf_lock);
13860 +}
13861 +
13862 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13863 +{
13864 +       aufs_bindex_t bindex, bbot;
13865 +       struct au_branch *br;
13866 +
13867 +       /* exclude the bottom */
13868 +       bbot = au_fhsm_bottom(sb);
13869 +       for (bindex = 0; bindex < bbot; bindex++) {
13870 +               br = au_sbr(sb, bindex);
13871 +               if (au_br_fhsm(br->br_perm))
13872 +                       au_fhsm_wrote(sb, bindex, force);
13873 +       }
13874 +}
13875 +
13876 +/* ---------------------------------------------------------------------- */
13877 +
13878 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13879 +{
13880 +       __poll_t mask;
13881 +       struct au_sbinfo *sbinfo;
13882 +       struct au_fhsm *fhsm;
13883 +
13884 +       mask = 0;
13885 +       sbinfo = file->private_data;
13886 +       fhsm = &sbinfo->si_fhsm;
13887 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13888 +       if (atomic_read(&fhsm->fhsm_readable))
13889 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13890 +
13891 +       if (!mask)
13892 +               AuDbg("mask 0x%x\n", mask);
13893 +       return mask;
13894 +}
13895 +
13896 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13897 +                             struct aufs_stfs *stfs, __s16 brid)
13898 +{
13899 +       int err;
13900 +
13901 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13902 +       if (!err)
13903 +               err = __put_user(brid, &stbr->brid);
13904 +       if (unlikely(err))
13905 +               err = -EFAULT;
13906 +
13907 +       return err;
13908 +}
13909 +
13910 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13911 +                              struct aufs_stbr __user *stbr, size_t count)
13912 +{
13913 +       ssize_t err;
13914 +       int nstbr;
13915 +       aufs_bindex_t bindex, bbot;
13916 +       struct au_branch *br;
13917 +       struct au_br_fhsm *bf;
13918 +
13919 +       /* except the bottom branch */
13920 +       err = 0;
13921 +       nstbr = 0;
13922 +       bbot = au_fhsm_bottom(sb);
13923 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13924 +               br = au_sbr(sb, bindex);
13925 +               if (!au_br_fhsm(br->br_perm))
13926 +                       continue;
13927 +
13928 +               bf = br->br_fhsm;
13929 +               mutex_lock(&bf->bf_lock);
13930 +               if (bf->bf_readable) {
13931 +                       err = -EFAULT;
13932 +                       if (count >= sizeof(*stbr))
13933 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13934 +                                                         br->br_id);
13935 +                       if (!err) {
13936 +                               bf->bf_readable = 0;
13937 +                               count -= sizeof(*stbr);
13938 +                               nstbr++;
13939 +                       }
13940 +               }
13941 +               mutex_unlock(&bf->bf_lock);
13942 +       }
13943 +       if (!err)
13944 +               err = sizeof(*stbr) * nstbr;
13945 +
13946 +       return err;
13947 +}
13948 +
13949 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13950 +                          loff_t *pos)
13951 +{
13952 +       ssize_t err;
13953 +       int readable;
13954 +       aufs_bindex_t nfhsm, bindex, bbot;
13955 +       struct au_sbinfo *sbinfo;
13956 +       struct au_fhsm *fhsm;
13957 +       struct au_branch *br;
13958 +       struct super_block *sb;
13959 +
13960 +       err = 0;
13961 +       sbinfo = file->private_data;
13962 +       fhsm = &sbinfo->si_fhsm;
13963 +need_data:
13964 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13965 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13966 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13967 +                       err = -EAGAIN;
13968 +               else
13969 +                       err = wait_event_interruptible_locked_irq
13970 +                               (fhsm->fhsm_wqh,
13971 +                                atomic_read(&fhsm->fhsm_readable));
13972 +       }
13973 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13974 +       if (unlikely(err))
13975 +               goto out;
13976 +
13977 +       /* sb may already be dead */
13978 +       au_rw_read_lock(&sbinfo->si_rwsem);
13979 +       readable = atomic_read(&fhsm->fhsm_readable);
13980 +       if (readable > 0) {
13981 +               sb = sbinfo->si_sb;
13982 +               AuDebugOn(!sb);
13983 +               /* exclude the bottom branch */
13984 +               nfhsm = 0;
13985 +               bbot = au_fhsm_bottom(sb);
13986 +               for (bindex = 0; bindex < bbot; bindex++) {
13987 +                       br = au_sbr(sb, bindex);
13988 +                       if (au_br_fhsm(br->br_perm))
13989 +                               nfhsm++;
13990 +               }
13991 +               err = -EMSGSIZE;
13992 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13993 +                       atomic_set(&fhsm->fhsm_readable, 0);
13994 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13995 +                                            count);
13996 +               }
13997 +       }
13998 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13999 +       if (!readable)
14000 +               goto need_data;
14001 +
14002 +out:
14003 +       return err;
14004 +}
14005 +
14006 +static int au_fhsm_release(struct inode *inode, struct file *file)
14007 +{
14008 +       struct au_sbinfo *sbinfo;
14009 +       struct au_fhsm *fhsm;
14010 +
14011 +       /* sb may already be dead */
14012 +       sbinfo = file->private_data;
14013 +       fhsm = &sbinfo->si_fhsm;
14014 +       spin_lock(&fhsm->fhsm_spin);
14015 +       fhsm->fhsm_pid = 0;
14016 +       spin_unlock(&fhsm->fhsm_spin);
14017 +       kobject_put(&sbinfo->si_kobj);
14018 +
14019 +       return 0;
14020 +}
14021 +
14022 +static const struct file_operations au_fhsm_fops = {
14023 +       .owner          = THIS_MODULE,
14024 +       .llseek         = noop_llseek,
14025 +       .read           = au_fhsm_read,
14026 +       .poll           = au_fhsm_poll,
14027 +       .release        = au_fhsm_release
14028 +};
14029 +
14030 +int au_fhsm_fd(struct super_block *sb, int oflags)
14031 +{
14032 +       int err, fd;
14033 +       struct au_sbinfo *sbinfo;
14034 +       struct au_fhsm *fhsm;
14035 +
14036 +       err = -EPERM;
14037 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
14038 +               goto out;
14039 +
14040 +       err = -EINVAL;
14041 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
14042 +               goto out;
14043 +
14044 +       err = 0;
14045 +       sbinfo = au_sbi(sb);
14046 +       fhsm = &sbinfo->si_fhsm;
14047 +       spin_lock(&fhsm->fhsm_spin);
14048 +       if (!fhsm->fhsm_pid)
14049 +               fhsm->fhsm_pid = current->pid;
14050 +       else
14051 +               err = -EBUSY;
14052 +       spin_unlock(&fhsm->fhsm_spin);
14053 +       if (unlikely(err))
14054 +               goto out;
14055 +
14056 +       oflags |= O_RDONLY;
14057 +       /* oflags |= FMODE_NONOTIFY; */
14058 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14059 +       err = fd;
14060 +       if (unlikely(fd < 0))
14061 +               goto out_pid;
14062 +
14063 +       /* succeed regardless 'fhsm' status */
14064 +       kobject_get(&sbinfo->si_kobj);
14065 +       si_noflush_read_lock(sb);
14066 +       if (au_ftest_si(sbinfo, FHSM))
14067 +               au_fhsm_wrote_all(sb, /*force*/0);
14068 +       si_read_unlock(sb);
14069 +       goto out; /* success */
14070 +
14071 +out_pid:
14072 +       spin_lock(&fhsm->fhsm_spin);
14073 +       fhsm->fhsm_pid = 0;
14074 +       spin_unlock(&fhsm->fhsm_spin);
14075 +out:
14076 +       AuTraceErr(err);
14077 +       return err;
14078 +}
14079 +
14080 +/* ---------------------------------------------------------------------- */
14081 +
14082 +int au_fhsm_br_alloc(struct au_branch *br)
14083 +{
14084 +       int err;
14085 +
14086 +       err = 0;
14087 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14088 +       if (br->br_fhsm)
14089 +               au_br_fhsm_init(br->br_fhsm);
14090 +       else
14091 +               err = -ENOMEM;
14092 +
14093 +       return err;
14094 +}
14095 +
14096 +/* ---------------------------------------------------------------------- */
14097 +
14098 +void au_fhsm_fin(struct super_block *sb)
14099 +{
14100 +       au_fhsm_notify(sb, /*val*/-1);
14101 +}
14102 +
14103 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14104 +{
14105 +       struct au_fhsm *fhsm;
14106 +
14107 +       fhsm = &sbinfo->si_fhsm;
14108 +       spin_lock_init(&fhsm->fhsm_spin);
14109 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14110 +       atomic_set(&fhsm->fhsm_readable, 0);
14111 +       fhsm->fhsm_expire
14112 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14113 +       fhsm->fhsm_bottom = -1;
14114 +}
14115 +
14116 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14117 +{
14118 +       sbinfo->si_fhsm.fhsm_expire
14119 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14120 +}
14121 +
14122 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14123 +{
14124 +       unsigned int u;
14125 +
14126 +       if (!au_ftest_si(sbinfo, FHSM))
14127 +               return;
14128 +
14129 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14130 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14131 +               seq_printf(seq, ",fhsm_sec=%u", u);
14132 +}
14133 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14134 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14135 +++ linux/fs/aufs/file.c        2020-01-27 10:57:18.172204883 +0100
14136 @@ -0,0 +1,863 @@
14137 +// SPDX-License-Identifier: GPL-2.0
14138 +/*
14139 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14140 + *
14141 + * This program, aufs is free software; you can redistribute it and/or modify
14142 + * it under the terms of the GNU General Public License as published by
14143 + * the Free Software Foundation; either version 2 of the License, or
14144 + * (at your option) any later version.
14145 + *
14146 + * This program is distributed in the hope that it will be useful,
14147 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14148 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14149 + * GNU General Public License for more details.
14150 + *
14151 + * You should have received a copy of the GNU General Public License
14152 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14153 + */
14154 +
14155 +/*
14156 + * handling file/dir, and address_space operation
14157 + */
14158 +
14159 +#ifdef CONFIG_AUFS_DEBUG
14160 +#include <linux/migrate.h>
14161 +#endif
14162 +#include <linux/pagemap.h>
14163 +#include "aufs.h"
14164 +
14165 +/* drop flags for writing */
14166 +unsigned int au_file_roflags(unsigned int flags)
14167 +{
14168 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14169 +       flags |= O_RDONLY | O_NOATIME;
14170 +       return flags;
14171 +}
14172 +
14173 +/* common functions to regular file and dir */
14174 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14175 +                      struct file *file, int force_wr)
14176 +{
14177 +       struct file *h_file;
14178 +       struct dentry *h_dentry;
14179 +       struct inode *h_inode;
14180 +       struct super_block *sb;
14181 +       struct au_branch *br;
14182 +       struct path h_path;
14183 +       int err;
14184 +
14185 +       /* a race condition can happen between open and unlink/rmdir */
14186 +       h_file = ERR_PTR(-ENOENT);
14187 +       h_dentry = au_h_dptr(dentry, bindex);
14188 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14189 +               goto out;
14190 +       h_inode = d_inode(h_dentry);
14191 +       spin_lock(&h_dentry->d_lock);
14192 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14193 +               /* || !d_inode(dentry)->i_nlink */
14194 +               ;
14195 +       spin_unlock(&h_dentry->d_lock);
14196 +       if (unlikely(err))
14197 +               goto out;
14198 +
14199 +       sb = dentry->d_sb;
14200 +       br = au_sbr(sb, bindex);
14201 +       err = au_br_test_oflag(flags, br);
14202 +       h_file = ERR_PTR(err);
14203 +       if (unlikely(err))
14204 +               goto out;
14205 +
14206 +       /* drop flags for writing */
14207 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14208 +               if (force_wr && !(flags & O_WRONLY))
14209 +                       force_wr = 0;
14210 +               flags = au_file_roflags(flags);
14211 +               if (force_wr) {
14212 +                       h_file = ERR_PTR(-EROFS);
14213 +                       flags = au_file_roflags(flags);
14214 +                       if (unlikely(vfsub_native_ro(h_inode)
14215 +                                    || IS_APPEND(h_inode)))
14216 +                               goto out;
14217 +                       flags &= ~O_ACCMODE;
14218 +                       flags |= O_WRONLY;
14219 +               }
14220 +       }
14221 +       flags &= ~O_CREAT;
14222 +       au_lcnt_inc(&br->br_nfiles);
14223 +       h_path.dentry = h_dentry;
14224 +       h_path.mnt = au_br_mnt(br);
14225 +       h_file = vfsub_dentry_open(&h_path, flags);
14226 +       if (IS_ERR(h_file))
14227 +               goto out_br;
14228 +
14229 +       if (flags & __FMODE_EXEC) {
14230 +               err = deny_write_access(h_file);
14231 +               if (unlikely(err)) {
14232 +                       fput(h_file);
14233 +                       h_file = ERR_PTR(err);
14234 +                       goto out_br;
14235 +               }
14236 +       }
14237 +       fsnotify_open(h_file);
14238 +       goto out; /* success */
14239 +
14240 +out_br:
14241 +       au_lcnt_dec(&br->br_nfiles);
14242 +out:
14243 +       return h_file;
14244 +}
14245 +
14246 +static int au_cmoo(struct dentry *dentry)
14247 +{
14248 +       int err, cmoo, matched;
14249 +       unsigned int udba;
14250 +       struct path h_path;
14251 +       struct au_pin pin;
14252 +       struct au_cp_generic cpg = {
14253 +               .dentry = dentry,
14254 +               .bdst   = -1,
14255 +               .bsrc   = -1,
14256 +               .len    = -1,
14257 +               .pin    = &pin,
14258 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14259 +       };
14260 +       struct inode *delegated;
14261 +       struct super_block *sb;
14262 +       struct au_sbinfo *sbinfo;
14263 +       struct au_fhsm *fhsm;
14264 +       pid_t pid;
14265 +       struct au_branch *br;
14266 +       struct dentry *parent;
14267 +       struct au_hinode *hdir;
14268 +
14269 +       DiMustWriteLock(dentry);
14270 +       IiMustWriteLock(d_inode(dentry));
14271 +
14272 +       err = 0;
14273 +       if (IS_ROOT(dentry))
14274 +               goto out;
14275 +       cpg.bsrc = au_dbtop(dentry);
14276 +       if (!cpg.bsrc)
14277 +               goto out;
14278 +
14279 +       sb = dentry->d_sb;
14280 +       sbinfo = au_sbi(sb);
14281 +       fhsm = &sbinfo->si_fhsm;
14282 +       pid = au_fhsm_pid(fhsm);
14283 +       rcu_read_lock();
14284 +       matched = (pid
14285 +                  && (current->pid == pid
14286 +                      || rcu_dereference(current->real_parent)->pid == pid));
14287 +       rcu_read_unlock();
14288 +       if (matched)
14289 +               goto out;
14290 +
14291 +       br = au_sbr(sb, cpg.bsrc);
14292 +       cmoo = au_br_cmoo(br->br_perm);
14293 +       if (!cmoo)
14294 +               goto out;
14295 +       if (!d_is_reg(dentry))
14296 +               cmoo &= AuBrAttr_COO_ALL;
14297 +       if (!cmoo)
14298 +               goto out;
14299 +
14300 +       parent = dget_parent(dentry);
14301 +       di_write_lock_parent(parent);
14302 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14303 +       cpg.bdst = err;
14304 +       if (unlikely(err < 0)) {
14305 +               err = 0;        /* there is no upper writable branch */
14306 +               goto out_dgrade;
14307 +       }
14308 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14309 +
14310 +       /* do not respect the coo attrib for the target branch */
14311 +       err = au_cpup_dirs(dentry, cpg.bdst);
14312 +       if (unlikely(err))
14313 +               goto out_dgrade;
14314 +
14315 +       di_downgrade_lock(parent, AuLock_IR);
14316 +       udba = au_opt_udba(sb);
14317 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14318 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14319 +       if (unlikely(err))
14320 +               goto out_parent;
14321 +
14322 +       err = au_sio_cpup_simple(&cpg);
14323 +       au_unpin(&pin);
14324 +       if (unlikely(err))
14325 +               goto out_parent;
14326 +       if (!(cmoo & AuBrWAttr_MOO))
14327 +               goto out_parent; /* success */
14328 +
14329 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14330 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14331 +       if (unlikely(err))
14332 +               goto out_parent;
14333 +
14334 +       h_path.mnt = au_br_mnt(br);
14335 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14336 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14337 +       delegated = NULL;
14338 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14339 +       au_unpin(&pin);
14340 +       /* todo: keep h_dentry or not? */
14341 +       if (unlikely(err == -EWOULDBLOCK)) {
14342 +               pr_warn("cannot retry for NFSv4 delegation"
14343 +                       " for an internal unlink\n");
14344 +               iput(delegated);
14345 +       }
14346 +       if (unlikely(err)) {
14347 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14348 +                      dentry, err);
14349 +               err = 0;
14350 +       }
14351 +       goto out_parent; /* success */
14352 +
14353 +out_dgrade:
14354 +       di_downgrade_lock(parent, AuLock_IR);
14355 +out_parent:
14356 +       di_read_unlock(parent, AuLock_IR);
14357 +       dput(parent);
14358 +out:
14359 +       AuTraceErr(err);
14360 +       return err;
14361 +}
14362 +
14363 +int au_do_open(struct file *file, struct au_do_open_args *args)
14364 +{
14365 +       int err, aopen = args->aopen;
14366 +       struct dentry *dentry;
14367 +       struct au_finfo *finfo;
14368 +
14369 +       if (!aopen)
14370 +               err = au_finfo_init(file, args->fidir);
14371 +       else {
14372 +               lockdep_off();
14373 +               err = au_finfo_init(file, args->fidir);
14374 +               lockdep_on();
14375 +       }
14376 +       if (unlikely(err))
14377 +               goto out;
14378 +
14379 +       dentry = file->f_path.dentry;
14380 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14381 +       di_write_lock_child(dentry);
14382 +       err = au_cmoo(dentry);
14383 +       di_downgrade_lock(dentry, AuLock_IR);
14384 +       if (!err) {
14385 +               if (!aopen)
14386 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14387 +               else {
14388 +                       lockdep_off();
14389 +                       err = args->open(file, vfsub_file_flags(file),
14390 +                                        args->h_file);
14391 +                       lockdep_on();
14392 +               }
14393 +       }
14394 +       di_read_unlock(dentry, AuLock_IR);
14395 +
14396 +       finfo = au_fi(file);
14397 +       if (!err) {
14398 +               finfo->fi_file = file;
14399 +               au_hbl_add(&finfo->fi_hlist,
14400 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14401 +       }
14402 +       if (!aopen)
14403 +               fi_write_unlock(file);
14404 +       else {
14405 +               lockdep_off();
14406 +               fi_write_unlock(file);
14407 +               lockdep_on();
14408 +       }
14409 +       if (unlikely(err)) {
14410 +               finfo->fi_hdir = NULL;
14411 +               au_finfo_fin(file);
14412 +       }
14413 +
14414 +out:
14415 +       AuTraceErr(err);
14416 +       return err;
14417 +}
14418 +
14419 +int au_reopen_nondir(struct file *file)
14420 +{
14421 +       int err;
14422 +       aufs_bindex_t btop;
14423 +       struct dentry *dentry;
14424 +       struct au_branch *br;
14425 +       struct file *h_file, *h_file_tmp;
14426 +
14427 +       dentry = file->f_path.dentry;
14428 +       btop = au_dbtop(dentry);
14429 +       br = au_sbr(dentry->d_sb, btop);
14430 +       h_file_tmp = NULL;
14431 +       if (au_fbtop(file) == btop) {
14432 +               h_file = au_hf_top(file);
14433 +               if (file->f_mode == h_file->f_mode)
14434 +                       return 0; /* success */
14435 +               h_file_tmp = h_file;
14436 +               get_file(h_file_tmp);
14437 +               au_lcnt_inc(&br->br_nfiles);
14438 +               au_set_h_fptr(file, btop, NULL);
14439 +       }
14440 +       AuDebugOn(au_fi(file)->fi_hdir);
14441 +       /*
14442 +        * it can happen
14443 +        * file exists on both of rw and ro
14444 +        * open --> dbtop and fbtop are both 0
14445 +        * prepend a branch as rw, "rw" become ro
14446 +        * remove rw/file
14447 +        * delete the top branch, "rw" becomes rw again
14448 +        *      --> dbtop is 1, fbtop is still 0
14449 +        * write --> fbtop is 0 but dbtop is 1
14450 +        */
14451 +       /* AuDebugOn(au_fbtop(file) < btop); */
14452 +
14453 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14454 +                          file, /*force_wr*/0);
14455 +       err = PTR_ERR(h_file);
14456 +       if (IS_ERR(h_file)) {
14457 +               if (h_file_tmp) {
14458 +                       /* revert */
14459 +                       au_set_h_fptr(file, btop, h_file_tmp);
14460 +                       h_file_tmp = NULL;
14461 +               }
14462 +               goto out; /* todo: close all? */
14463 +       }
14464 +
14465 +       err = 0;
14466 +       au_set_fbtop(file, btop);
14467 +       au_set_h_fptr(file, btop, h_file);
14468 +       au_update_figen(file);
14469 +       /* todo: necessary? */
14470 +       /* file->f_ra = h_file->f_ra; */
14471 +
14472 +out:
14473 +       if (h_file_tmp) {
14474 +               fput(h_file_tmp);
14475 +               au_lcnt_dec(&br->br_nfiles);
14476 +       }
14477 +       return err;
14478 +}
14479 +
14480 +/* ---------------------------------------------------------------------- */
14481 +
14482 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14483 +                       struct dentry *hi_wh)
14484 +{
14485 +       int err;
14486 +       aufs_bindex_t btop;
14487 +       struct au_dinfo *dinfo;
14488 +       struct dentry *h_dentry;
14489 +       struct au_hdentry *hdp;
14490 +
14491 +       dinfo = au_di(file->f_path.dentry);
14492 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14493 +
14494 +       btop = dinfo->di_btop;
14495 +       dinfo->di_btop = btgt;
14496 +       hdp = au_hdentry(dinfo, btgt);
14497 +       h_dentry = hdp->hd_dentry;
14498 +       hdp->hd_dentry = hi_wh;
14499 +       err = au_reopen_nondir(file);
14500 +       hdp->hd_dentry = h_dentry;
14501 +       dinfo->di_btop = btop;
14502 +
14503 +       return err;
14504 +}
14505 +
14506 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14507 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14508 +{
14509 +       int err;
14510 +       struct inode *inode, *h_inode;
14511 +       struct dentry *h_dentry, *hi_wh;
14512 +       struct au_cp_generic cpg = {
14513 +               .dentry = file->f_path.dentry,
14514 +               .bdst   = bcpup,
14515 +               .bsrc   = -1,
14516 +               .len    = len,
14517 +               .pin    = pin
14518 +       };
14519 +
14520 +       au_update_dbtop(cpg.dentry);
14521 +       inode = d_inode(cpg.dentry);
14522 +       h_inode = NULL;
14523 +       if (au_dbtop(cpg.dentry) <= bcpup
14524 +           && au_dbbot(cpg.dentry) >= bcpup) {
14525 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14526 +               if (h_dentry && d_is_positive(h_dentry))
14527 +                       h_inode = d_inode(h_dentry);
14528 +       }
14529 +       hi_wh = au_hi_wh(inode, bcpup);
14530 +       if (!hi_wh && !h_inode)
14531 +               err = au_sio_cpup_wh(&cpg, file);
14532 +       else
14533 +               /* already copied-up after unlink */
14534 +               err = au_reopen_wh(file, bcpup, hi_wh);
14535 +
14536 +       if (!err
14537 +           && (inode->i_nlink > 1
14538 +               || (inode->i_state & I_LINKABLE))
14539 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14540 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14541 +
14542 +       return err;
14543 +}
14544 +
14545 +/*
14546 + * prepare the @file for writing.
14547 + */
14548 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14549 +{
14550 +       int err;
14551 +       aufs_bindex_t dbtop;
14552 +       struct dentry *parent;
14553 +       struct inode *inode;
14554 +       struct super_block *sb;
14555 +       struct file *h_file;
14556 +       struct au_cp_generic cpg = {
14557 +               .dentry = file->f_path.dentry,
14558 +               .bdst   = -1,
14559 +               .bsrc   = -1,
14560 +               .len    = len,
14561 +               .pin    = pin,
14562 +               .flags  = AuCpup_DTIME
14563 +       };
14564 +
14565 +       sb = cpg.dentry->d_sb;
14566 +       inode = d_inode(cpg.dentry);
14567 +       cpg.bsrc = au_fbtop(file);
14568 +       err = au_test_ro(sb, cpg.bsrc, inode);
14569 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14570 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14571 +                            /*flags*/0);
14572 +               goto out;
14573 +       }
14574 +
14575 +       /* need to cpup or reopen */
14576 +       parent = dget_parent(cpg.dentry);
14577 +       di_write_lock_parent(parent);
14578 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14579 +       cpg.bdst = err;
14580 +       if (unlikely(err < 0))
14581 +               goto out_dgrade;
14582 +       err = 0;
14583 +
14584 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14585 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14586 +               if (unlikely(err))
14587 +                       goto out_dgrade;
14588 +       }
14589 +
14590 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14591 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14592 +       if (unlikely(err))
14593 +               goto out_dgrade;
14594 +
14595 +       dbtop = au_dbtop(cpg.dentry);
14596 +       if (dbtop <= cpg.bdst)
14597 +               cpg.bsrc = cpg.bdst;
14598 +
14599 +       if (dbtop <= cpg.bdst           /* just reopen */
14600 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14601 +               ) {
14602 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14603 +               if (IS_ERR(h_file))
14604 +                       err = PTR_ERR(h_file);
14605 +               else {
14606 +                       di_downgrade_lock(parent, AuLock_IR);
14607 +                       if (dbtop > cpg.bdst)
14608 +                               err = au_sio_cpup_simple(&cpg);
14609 +                       if (!err)
14610 +                               err = au_reopen_nondir(file);
14611 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14612 +               }
14613 +       } else {                        /* copyup as wh and reopen */
14614 +               /*
14615 +                * since writable hfsplus branch is not supported,
14616 +                * h_open_pre/post() are unnecessary.
14617 +                */
14618 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14619 +               di_downgrade_lock(parent, AuLock_IR);
14620 +       }
14621 +
14622 +       if (!err) {
14623 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14624 +               goto out_dput; /* success */
14625 +       }
14626 +       au_unpin(pin);
14627 +       goto out_unlock;
14628 +
14629 +out_dgrade:
14630 +       di_downgrade_lock(parent, AuLock_IR);
14631 +out_unlock:
14632 +       di_read_unlock(parent, AuLock_IR);
14633 +out_dput:
14634 +       dput(parent);
14635 +out:
14636 +       return err;
14637 +}
14638 +
14639 +/* ---------------------------------------------------------------------- */
14640 +
14641 +int au_do_flush(struct file *file, fl_owner_t id,
14642 +               int (*flush)(struct file *file, fl_owner_t id))
14643 +{
14644 +       int err;
14645 +       struct super_block *sb;
14646 +       struct inode *inode;
14647 +
14648 +       inode = file_inode(file);
14649 +       sb = inode->i_sb;
14650 +       si_noflush_read_lock(sb);
14651 +       fi_read_lock(file);
14652 +       ii_read_lock_child(inode);
14653 +
14654 +       err = flush(file, id);
14655 +       au_cpup_attr_timesizes(inode);
14656 +
14657 +       ii_read_unlock(inode);
14658 +       fi_read_unlock(file);
14659 +       si_read_unlock(sb);
14660 +       return err;
14661 +}
14662 +
14663 +/* ---------------------------------------------------------------------- */
14664 +
14665 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14666 +{
14667 +       int err;
14668 +       struct au_pin pin;
14669 +       struct au_finfo *finfo;
14670 +       struct dentry *parent, *hi_wh;
14671 +       struct inode *inode;
14672 +       struct super_block *sb;
14673 +       struct au_cp_generic cpg = {
14674 +               .dentry = file->f_path.dentry,
14675 +               .bdst   = -1,
14676 +               .bsrc   = -1,
14677 +               .len    = -1,
14678 +               .pin    = &pin,
14679 +               .flags  = AuCpup_DTIME
14680 +       };
14681 +
14682 +       FiMustWriteLock(file);
14683 +
14684 +       err = 0;
14685 +       finfo = au_fi(file);
14686 +       sb = cpg.dentry->d_sb;
14687 +       inode = d_inode(cpg.dentry);
14688 +       cpg.bdst = au_ibtop(inode);
14689 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14690 +               goto out;
14691 +
14692 +       parent = dget_parent(cpg.dentry);
14693 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14694 +               di_read_lock_parent(parent, !AuLock_IR);
14695 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14696 +               cpg.bdst = err;
14697 +               di_read_unlock(parent, !AuLock_IR);
14698 +               if (unlikely(err < 0))
14699 +                       goto out_parent;
14700 +               err = 0;
14701 +       }
14702 +
14703 +       di_read_lock_parent(parent, AuLock_IR);
14704 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14705 +       if (!S_ISDIR(inode->i_mode)
14706 +           && au_opt_test(au_mntflags(sb), PLINK)
14707 +           && au_plink_test(inode)
14708 +           && !d_unhashed(cpg.dentry)
14709 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14710 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14711 +               if (unlikely(err))
14712 +                       goto out_unlock;
14713 +
14714 +               /* always superio. */
14715 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14716 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14717 +               if (!err) {
14718 +                       err = au_sio_cpup_simple(&cpg);
14719 +                       au_unpin(&pin);
14720 +               }
14721 +       } else if (hi_wh) {
14722 +               /* already copied-up after unlink */
14723 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14724 +               *need_reopen = 0;
14725 +       }
14726 +
14727 +out_unlock:
14728 +       di_read_unlock(parent, AuLock_IR);
14729 +out_parent:
14730 +       dput(parent);
14731 +out:
14732 +       return err;
14733 +}
14734 +
14735 +static void au_do_refresh_dir(struct file *file)
14736 +{
14737 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14738 +       struct au_hfile *p, tmp, *q;
14739 +       struct au_finfo *finfo;
14740 +       struct super_block *sb;
14741 +       struct au_fidir *fidir;
14742 +
14743 +       FiMustWriteLock(file);
14744 +
14745 +       sb = file->f_path.dentry->d_sb;
14746 +       finfo = au_fi(file);
14747 +       fidir = finfo->fi_hdir;
14748 +       AuDebugOn(!fidir);
14749 +       p = fidir->fd_hfile + finfo->fi_btop;
14750 +       brid = p->hf_br->br_id;
14751 +       bbot = fidir->fd_bbot;
14752 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14753 +               if (!p->hf_file)
14754 +                       continue;
14755 +
14756 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14757 +               if (new_bindex == bindex)
14758 +                       continue;
14759 +               if (new_bindex < 0) {
14760 +                       au_set_h_fptr(file, bindex, NULL);
14761 +                       continue;
14762 +               }
14763 +
14764 +               /* swap two lower inode, and loop again */
14765 +               q = fidir->fd_hfile + new_bindex;
14766 +               tmp = *q;
14767 +               *q = *p;
14768 +               *p = tmp;
14769 +               if (tmp.hf_file) {
14770 +                       bindex--;
14771 +                       p--;
14772 +               }
14773 +       }
14774 +
14775 +       p = fidir->fd_hfile;
14776 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14777 +               bbot = au_sbbot(sb);
14778 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14779 +                    finfo->fi_btop++, p++)
14780 +                       if (p->hf_file) {
14781 +                               if (file_inode(p->hf_file))
14782 +                                       break;
14783 +                               au_hfput(p, /*execed*/0);
14784 +                       }
14785 +       } else {
14786 +               bbot = au_br_index(sb, brid);
14787 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14788 +                    finfo->fi_btop++, p++)
14789 +                       if (p->hf_file)
14790 +                               au_hfput(p, /*execed*/0);
14791 +               bbot = au_sbbot(sb);
14792 +       }
14793 +
14794 +       p = fidir->fd_hfile + bbot;
14795 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14796 +            fidir->fd_bbot--, p--)
14797 +               if (p->hf_file) {
14798 +                       if (file_inode(p->hf_file))
14799 +                               break;
14800 +                       au_hfput(p, /*execed*/0);
14801 +               }
14802 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14803 +}
14804 +
14805 +/*
14806 + * after branch manipulating, refresh the file.
14807 + */
14808 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14809 +{
14810 +       int err, need_reopen, nbr;
14811 +       aufs_bindex_t bbot, bindex;
14812 +       struct dentry *dentry;
14813 +       struct super_block *sb;
14814 +       struct au_finfo *finfo;
14815 +       struct au_hfile *hfile;
14816 +
14817 +       dentry = file->f_path.dentry;
14818 +       sb = dentry->d_sb;
14819 +       nbr = au_sbbot(sb) + 1;
14820 +       finfo = au_fi(file);
14821 +       if (!finfo->fi_hdir) {
14822 +               hfile = &finfo->fi_htop;
14823 +               AuDebugOn(!hfile->hf_file);
14824 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14825 +               AuDebugOn(bindex < 0);
14826 +               if (bindex != finfo->fi_btop)
14827 +                       au_set_fbtop(file, bindex);
14828 +       } else {
14829 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14830 +               if (unlikely(err))
14831 +                       goto out;
14832 +               au_do_refresh_dir(file);
14833 +       }
14834 +
14835 +       err = 0;
14836 +       need_reopen = 1;
14837 +       if (!au_test_mmapped(file))
14838 +               err = au_file_refresh_by_inode(file, &need_reopen);
14839 +       if (finfo->fi_hdir)
14840 +               /* harmless if err */
14841 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14842 +       if (!err && need_reopen && !d_unlinked(dentry))
14843 +               err = reopen(file);
14844 +       if (!err) {
14845 +               au_update_figen(file);
14846 +               goto out; /* success */
14847 +       }
14848 +
14849 +       /* error, close all lower files */
14850 +       if (finfo->fi_hdir) {
14851 +               bbot = au_fbbot_dir(file);
14852 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14853 +                       au_set_h_fptr(file, bindex, NULL);
14854 +       }
14855 +
14856 +out:
14857 +       return err;
14858 +}
14859 +
14860 +/* common function to regular file and dir */
14861 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14862 +                         int wlock, unsigned int fi_lsc)
14863 +{
14864 +       int err;
14865 +       unsigned int sigen, figen;
14866 +       aufs_bindex_t btop;
14867 +       unsigned char pseudo_link;
14868 +       struct dentry *dentry;
14869 +       struct inode *inode;
14870 +
14871 +       err = 0;
14872 +       dentry = file->f_path.dentry;
14873 +       inode = d_inode(dentry);
14874 +       sigen = au_sigen(dentry->d_sb);
14875 +       fi_write_lock_nested(file, fi_lsc);
14876 +       figen = au_figen(file);
14877 +       if (!fi_lsc)
14878 +               di_write_lock_child(dentry);
14879 +       else
14880 +               di_write_lock_child2(dentry);
14881 +       btop = au_dbtop(dentry);
14882 +       pseudo_link = (btop != au_ibtop(inode));
14883 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14884 +               if (!wlock) {
14885 +                       di_downgrade_lock(dentry, AuLock_IR);
14886 +                       fi_downgrade_lock(file);
14887 +               }
14888 +               goto out; /* success */
14889 +       }
14890 +
14891 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14892 +       if (au_digen_test(dentry, sigen)) {
14893 +               err = au_reval_dpath(dentry, sigen);
14894 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14895 +       }
14896 +
14897 +       if (!err)
14898 +               err = refresh_file(file, reopen);
14899 +       if (!err) {
14900 +               if (!wlock) {
14901 +                       di_downgrade_lock(dentry, AuLock_IR);
14902 +                       fi_downgrade_lock(file);
14903 +               }
14904 +       } else {
14905 +               di_write_unlock(dentry);
14906 +               fi_write_unlock(file);
14907 +       }
14908 +
14909 +out:
14910 +       return err;
14911 +}
14912 +
14913 +/* ---------------------------------------------------------------------- */
14914 +
14915 +/* cf. aufs_nopage() */
14916 +/* for madvise(2) */
14917 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14918 +{
14919 +       unlock_page(page);
14920 +       return 0;
14921 +}
14922 +
14923 +/* it will never be called, but necessary to support O_DIRECT */
14924 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14925 +{ BUG(); return 0; }
14926 +
14927 +/* they will never be called. */
14928 +#ifdef CONFIG_AUFS_DEBUG
14929 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14930 +                           loff_t pos, unsigned len, unsigned flags,
14931 +                           struct page **pagep, void **fsdata)
14932 +{ AuUnsupport(); return 0; }
14933 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14934 +                         loff_t pos, unsigned len, unsigned copied,
14935 +                         struct page *page, void *fsdata)
14936 +{ AuUnsupport(); return 0; }
14937 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14938 +{ AuUnsupport(); return 0; }
14939 +
14940 +static int aufs_set_page_dirty(struct page *page)
14941 +{ AuUnsupport(); return 0; }
14942 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14943 +                               unsigned int length)
14944 +{ AuUnsupport(); }
14945 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14946 +{ AuUnsupport(); return 0; }
14947 +#if 0 /* called by memory compaction regardless file */
14948 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14949 +                           struct page *page, enum migrate_mode mode)
14950 +{ AuUnsupport(); return 0; }
14951 +#endif
14952 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14953 +{ AuUnsupport(); return true; }
14954 +static void aufs_putback_page(struct page *page)
14955 +{ AuUnsupport(); }
14956 +static int aufs_launder_page(struct page *page)
14957 +{ AuUnsupport(); return 0; }
14958 +static int aufs_is_partially_uptodate(struct page *page,
14959 +                                     unsigned long from,
14960 +                                     unsigned long count)
14961 +{ AuUnsupport(); return 0; }
14962 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14963 +                                   bool *writeback)
14964 +{ AuUnsupport(); }
14965 +static int aufs_error_remove_page(struct address_space *mapping,
14966 +                                 struct page *page)
14967 +{ AuUnsupport(); return 0; }
14968 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14969 +                             sector_t *span)
14970 +{ AuUnsupport(); return 0; }
14971 +static void aufs_swap_deactivate(struct file *file)
14972 +{ AuUnsupport(); }
14973 +#endif /* CONFIG_AUFS_DEBUG */
14974 +
14975 +const struct address_space_operations aufs_aop = {
14976 +       .readpage               = aufs_readpage,
14977 +       .direct_IO              = aufs_direct_IO,
14978 +#ifdef CONFIG_AUFS_DEBUG
14979 +       .writepage              = aufs_writepage,
14980 +       /* no writepages, because of writepage */
14981 +       .set_page_dirty         = aufs_set_page_dirty,
14982 +       /* no readpages, because of readpage */
14983 +       .write_begin            = aufs_write_begin,
14984 +       .write_end              = aufs_write_end,
14985 +       /* no bmap, no block device */
14986 +       .invalidatepage         = aufs_invalidatepage,
14987 +       .releasepage            = aufs_releasepage,
14988 +       /* is fallback_migrate_page ok? */
14989 +       /* .migratepage         = aufs_migratepage, */
14990 +       .isolate_page           = aufs_isolate_page,
14991 +       .putback_page           = aufs_putback_page,
14992 +       .launder_page           = aufs_launder_page,
14993 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14994 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14995 +       .error_remove_page      = aufs_error_remove_page,
14996 +       .swap_activate          = aufs_swap_activate,
14997 +       .swap_deactivate        = aufs_swap_deactivate
14998 +#endif /* CONFIG_AUFS_DEBUG */
14999 +};
15000 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
15001 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
15002 +++ linux/fs/aufs/file.h        2020-01-27 10:57:18.172204883 +0100
15003 @@ -0,0 +1,342 @@
15004 +/* SPDX-License-Identifier: GPL-2.0 */
15005 +/*
15006 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15007 + *
15008 + * This program, aufs is free software; you can redistribute it and/or modify
15009 + * it under the terms of the GNU General Public License as published by
15010 + * the Free Software Foundation; either version 2 of the License, or
15011 + * (at your option) any later version.
15012 + *
15013 + * This program is distributed in the hope that it will be useful,
15014 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15015 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15016 + * GNU General Public License for more details.
15017 + *
15018 + * You should have received a copy of the GNU General Public License
15019 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15020 + */
15021 +
15022 +/*
15023 + * file operations
15024 + */
15025 +
15026 +#ifndef __AUFS_FILE_H__
15027 +#define __AUFS_FILE_H__
15028 +
15029 +#ifdef __KERNEL__
15030 +
15031 +#include <linux/file.h>
15032 +#include <linux/fs.h>
15033 +#include <linux/mm_types.h>
15034 +#include <linux/poll.h>
15035 +#include "rwsem.h"
15036 +
15037 +struct au_branch;
15038 +struct au_hfile {
15039 +       struct file             *hf_file;
15040 +       struct au_branch        *hf_br;
15041 +};
15042 +
15043 +struct au_vdir;
15044 +struct au_fidir {
15045 +       aufs_bindex_t           fd_bbot;
15046 +       aufs_bindex_t           fd_nent;
15047 +       struct au_vdir          *fd_vdir_cache;
15048 +       struct au_hfile         fd_hfile[];
15049 +};
15050 +
15051 +static inline int au_fidir_sz(int nent)
15052 +{
15053 +       AuDebugOn(nent < 0);
15054 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15055 +}
15056 +
15057 +struct au_finfo {
15058 +       atomic_t                fi_generation;
15059 +
15060 +       struct au_rwsem         fi_rwsem;
15061 +       aufs_bindex_t           fi_btop;
15062 +
15063 +       /* do not union them */
15064 +       struct {                                /* for non-dir */
15065 +               struct au_hfile                 fi_htop;
15066 +               atomic_t                        fi_mmapped;
15067 +       };
15068 +       struct au_fidir         *fi_hdir;       /* for dir only */
15069 +
15070 +       struct hlist_bl_node    fi_hlist;
15071 +       struct file             *fi_file;       /* very ugly */
15072 +       struct rcu_head         rcu;
15073 +} ____cacheline_aligned_in_smp;
15074 +
15075 +/* ---------------------------------------------------------------------- */
15076 +
15077 +/* file.c */
15078 +extern const struct address_space_operations aufs_aop;
15079 +unsigned int au_file_roflags(unsigned int flags);
15080 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15081 +                      struct file *file, int force_wr);
15082 +struct au_do_open_args {
15083 +       int             aopen;
15084 +       int             (*open)(struct file *file, int flags,
15085 +                               struct file *h_file);
15086 +       struct au_fidir *fidir;
15087 +       struct file     *h_file;
15088 +};
15089 +int au_do_open(struct file *file, struct au_do_open_args *args);
15090 +int au_reopen_nondir(struct file *file);
15091 +struct au_pin;
15092 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15093 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15094 +                         int wlock, unsigned int fi_lsc);
15095 +int au_do_flush(struct file *file, fl_owner_t id,
15096 +               int (*flush)(struct file *file, fl_owner_t id));
15097 +
15098 +/* poll.c */
15099 +#ifdef CONFIG_AUFS_POLL
15100 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15101 +#endif
15102 +
15103 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15104 +/* hfsplus.c */
15105 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15106 +                          int force_wr);
15107 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15108 +                   struct file *h_file);
15109 +#else
15110 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15111 +       aufs_bindex_t bindex, int force_wr)
15112 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15113 +          struct file *h_file);
15114 +#endif
15115 +
15116 +/* f_op.c */
15117 +extern const struct file_operations aufs_file_fop;
15118 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15119 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15120 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15121 +
15122 +/* finfo.c */
15123 +void au_hfput(struct au_hfile *hf, int execed);
15124 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15125 +                  struct file *h_file);
15126 +
15127 +void au_update_figen(struct file *file);
15128 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15129 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15130 +
15131 +void au_fi_init_once(void *_fi);
15132 +void au_finfo_fin(struct file *file);
15133 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15134 +
15135 +/* ioctl.c */
15136 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15137 +#ifdef CONFIG_COMPAT
15138 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15139 +                          unsigned long arg);
15140 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15141 +                             unsigned long arg);
15142 +#endif
15143 +
15144 +/* ---------------------------------------------------------------------- */
15145 +
15146 +static inline struct au_finfo *au_fi(struct file *file)
15147 +{
15148 +       return file->private_data;
15149 +}
15150 +
15151 +/* ---------------------------------------------------------------------- */
15152 +
15153 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15154 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15155 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15156 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15157 +/*
15158 +#define fi_read_trylock_nested(f) \
15159 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15160 +#define fi_write_trylock_nested(f) \
15161 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15162 +*/
15163 +
15164 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15165 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15166 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15167 +
15168 +/* lock subclass for finfo */
15169 +enum {
15170 +       AuLsc_FI_1,
15171 +       AuLsc_FI_2
15172 +};
15173 +
15174 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15175 +{
15176 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15177 +}
15178 +
15179 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15180 +{
15181 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15182 +}
15183 +
15184 +/*
15185 + * fi_read_lock_1, fi_write_lock_1,
15186 + * fi_read_lock_2, fi_write_lock_2
15187 + */
15188 +#define AuReadLockFunc(name) \
15189 +static inline void fi_read_lock_##name(struct file *f) \
15190 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15191 +
15192 +#define AuWriteLockFunc(name) \
15193 +static inline void fi_write_lock_##name(struct file *f) \
15194 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15195 +
15196 +#define AuRWLockFuncs(name) \
15197 +       AuReadLockFunc(name) \
15198 +       AuWriteLockFunc(name)
15199 +
15200 +AuRWLockFuncs(1);
15201 +AuRWLockFuncs(2);
15202 +
15203 +#undef AuReadLockFunc
15204 +#undef AuWriteLockFunc
15205 +#undef AuRWLockFuncs
15206 +
15207 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15208 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15209 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15210 +
15211 +/* ---------------------------------------------------------------------- */
15212 +
15213 +/* todo: hard/soft set? */
15214 +static inline aufs_bindex_t au_fbtop(struct file *file)
15215 +{
15216 +       FiMustAnyLock(file);
15217 +       return au_fi(file)->fi_btop;
15218 +}
15219 +
15220 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15221 +{
15222 +       FiMustAnyLock(file);
15223 +       AuDebugOn(!au_fi(file)->fi_hdir);
15224 +       return au_fi(file)->fi_hdir->fd_bbot;
15225 +}
15226 +
15227 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15228 +{
15229 +       FiMustAnyLock(file);
15230 +       AuDebugOn(!au_fi(file)->fi_hdir);
15231 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15232 +}
15233 +
15234 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15235 +{
15236 +       FiMustWriteLock(file);
15237 +       au_fi(file)->fi_btop = bindex;
15238 +}
15239 +
15240 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15241 +{
15242 +       FiMustWriteLock(file);
15243 +       AuDebugOn(!au_fi(file)->fi_hdir);
15244 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15245 +}
15246 +
15247 +static inline void au_set_fvdir_cache(struct file *file,
15248 +                                     struct au_vdir *vdir_cache)
15249 +{
15250 +       FiMustWriteLock(file);
15251 +       AuDebugOn(!au_fi(file)->fi_hdir);
15252 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15253 +}
15254 +
15255 +static inline struct file *au_hf_top(struct file *file)
15256 +{
15257 +       FiMustAnyLock(file);
15258 +       AuDebugOn(au_fi(file)->fi_hdir);
15259 +       return au_fi(file)->fi_htop.hf_file;
15260 +}
15261 +
15262 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15263 +{
15264 +       FiMustAnyLock(file);
15265 +       AuDebugOn(!au_fi(file)->fi_hdir);
15266 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15267 +}
15268 +
15269 +/* todo: memory barrier? */
15270 +static inline unsigned int au_figen(struct file *f)
15271 +{
15272 +       return atomic_read(&au_fi(f)->fi_generation);
15273 +}
15274 +
15275 +static inline void au_set_mmapped(struct file *f)
15276 +{
15277 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15278 +               return;
15279 +       pr_warn("fi_mmapped wrapped around\n");
15280 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15281 +               ;
15282 +}
15283 +
15284 +static inline void au_unset_mmapped(struct file *f)
15285 +{
15286 +       atomic_dec(&au_fi(f)->fi_mmapped);
15287 +}
15288 +
15289 +static inline int au_test_mmapped(struct file *f)
15290 +{
15291 +       return atomic_read(&au_fi(f)->fi_mmapped);
15292 +}
15293 +
15294 +/* customize vma->vm_file */
15295 +
15296 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15297 +                                      struct file *file)
15298 +{
15299 +       struct file *f;
15300 +
15301 +       f = vma->vm_file;
15302 +       get_file(file);
15303 +       vma->vm_file = file;
15304 +       fput(f);
15305 +}
15306 +
15307 +#ifdef CONFIG_MMU
15308 +#define AuDbgVmRegion(file, vma) do {} while (0)
15309 +
15310 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15311 +                                   struct file *file)
15312 +{
15313 +       au_do_vm_file_reset(vma, file);
15314 +}
15315 +#else
15316 +#define AuDbgVmRegion(file, vma) \
15317 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15318 +
15319 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15320 +                                   struct file *file)
15321 +{
15322 +       struct file *f;
15323 +
15324 +       au_do_vm_file_reset(vma, file);
15325 +       f = vma->vm_region->vm_file;
15326 +       get_file(file);
15327 +       vma->vm_region->vm_file = file;
15328 +       fput(f);
15329 +}
15330 +#endif /* CONFIG_MMU */
15331 +
15332 +/* handle vma->vm_prfile */
15333 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15334 +                                   struct file *file)
15335 +{
15336 +       get_file(file);
15337 +       vma->vm_prfile = file;
15338 +#ifndef CONFIG_MMU
15339 +       get_file(file);
15340 +       vma->vm_region->vm_prfile = file;
15341 +#endif
15342 +}
15343 +
15344 +#endif /* __KERNEL__ */
15345 +#endif /* __AUFS_FILE_H__ */
15346 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15347 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15348 +++ linux/fs/aufs/finfo.c       2020-01-27 10:57:18.172204883 +0100
15349 @@ -0,0 +1,149 @@
15350 +// SPDX-License-Identifier: GPL-2.0
15351 +/*
15352 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15353 + *
15354 + * This program, aufs is free software; you can redistribute it and/or modify
15355 + * it under the terms of the GNU General Public License as published by
15356 + * the Free Software Foundation; either version 2 of the License, or
15357 + * (at your option) any later version.
15358 + *
15359 + * This program is distributed in the hope that it will be useful,
15360 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15361 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15362 + * GNU General Public License for more details.
15363 + *
15364 + * You should have received a copy of the GNU General Public License
15365 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15366 + */
15367 +
15368 +/*
15369 + * file private data
15370 + */
15371 +
15372 +#include "aufs.h"
15373 +
15374 +void au_hfput(struct au_hfile *hf, int execed)
15375 +{
15376 +       if (execed)
15377 +               allow_write_access(hf->hf_file);
15378 +       fput(hf->hf_file);
15379 +       hf->hf_file = NULL;
15380 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15381 +       hf->hf_br = NULL;
15382 +}
15383 +
15384 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15385 +{
15386 +       struct au_finfo *finfo = au_fi(file);
15387 +       struct au_hfile *hf;
15388 +       struct au_fidir *fidir;
15389 +
15390 +       fidir = finfo->fi_hdir;
15391 +       if (!fidir) {
15392 +               AuDebugOn(finfo->fi_btop != bindex);
15393 +               hf = &finfo->fi_htop;
15394 +       } else
15395 +               hf = fidir->fd_hfile + bindex;
15396 +
15397 +       if (hf && hf->hf_file)
15398 +               au_hfput(hf, vfsub_file_execed(file));
15399 +       if (val) {
15400 +               FiMustWriteLock(file);
15401 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15402 +               hf->hf_file = val;
15403 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15404 +       }
15405 +}
15406 +
15407 +void au_update_figen(struct file *file)
15408 +{
15409 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15410 +       /* smp_mb(); */ /* atomic_set */
15411 +}
15412 +
15413 +/* ---------------------------------------------------------------------- */
15414 +
15415 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15416 +{
15417 +       struct au_fidir *fidir;
15418 +       int nbr;
15419 +
15420 +       nbr = au_sbbot(sb) + 1;
15421 +       if (nbr < 2)
15422 +               nbr = 2; /* initial allocate for 2 branches */
15423 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15424 +       if (fidir) {
15425 +               fidir->fd_bbot = -1;
15426 +               fidir->fd_nent = nbr;
15427 +       }
15428 +
15429 +       return fidir;
15430 +}
15431 +
15432 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15433 +{
15434 +       int err;
15435 +       struct au_fidir *fidir, *p;
15436 +
15437 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15438 +       fidir = finfo->fi_hdir;
15439 +       AuDebugOn(!fidir);
15440 +
15441 +       err = -ENOMEM;
15442 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15443 +                        GFP_NOFS, may_shrink);
15444 +       if (p) {
15445 +               p->fd_nent = nbr;
15446 +               finfo->fi_hdir = p;
15447 +               err = 0;
15448 +       }
15449 +
15450 +       return err;
15451 +}
15452 +
15453 +/* ---------------------------------------------------------------------- */
15454 +
15455 +void au_finfo_fin(struct file *file)
15456 +{
15457 +       struct au_finfo *finfo;
15458 +
15459 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15460 +
15461 +       finfo = au_fi(file);
15462 +       AuDebugOn(finfo->fi_hdir);
15463 +       AuRwDestroy(&finfo->fi_rwsem);
15464 +       au_cache_free_finfo(finfo);
15465 +}
15466 +
15467 +void au_fi_init_once(void *_finfo)
15468 +{
15469 +       struct au_finfo *finfo = _finfo;
15470 +
15471 +       au_rw_init(&finfo->fi_rwsem);
15472 +}
15473 +
15474 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15475 +{
15476 +       int err;
15477 +       struct au_finfo *finfo;
15478 +       struct dentry *dentry;
15479 +
15480 +       err = -ENOMEM;
15481 +       dentry = file->f_path.dentry;
15482 +       finfo = au_cache_alloc_finfo();
15483 +       if (unlikely(!finfo))
15484 +               goto out;
15485 +
15486 +       err = 0;
15487 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15488 +       au_rw_write_lock(&finfo->fi_rwsem);
15489 +       finfo->fi_btop = -1;
15490 +       finfo->fi_hdir = fidir;
15491 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15492 +       /* smp_mb(); */ /* atomic_set */
15493 +
15494 +       file->private_data = finfo;
15495 +
15496 +out:
15497 +       return err;
15498 +}
15499 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15500 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15501 +++ linux/fs/aufs/f_op.c        2020-01-27 10:57:18.172204883 +0100
15502 @@ -0,0 +1,819 @@
15503 +// SPDX-License-Identifier: GPL-2.0
15504 +/*
15505 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15506 + *
15507 + * This program, aufs is free software; you can redistribute it and/or modify
15508 + * it under the terms of the GNU General Public License as published by
15509 + * the Free Software Foundation; either version 2 of the License, or
15510 + * (at your option) any later version.
15511 + *
15512 + * This program is distributed in the hope that it will be useful,
15513 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15514 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15515 + * GNU General Public License for more details.
15516 + *
15517 + * You should have received a copy of the GNU General Public License
15518 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15519 + */
15520 +
15521 +/*
15522 + * file and vm operations
15523 + */
15524 +
15525 +#include <linux/aio.h>
15526 +#include <linux/fs_stack.h>
15527 +#include <linux/mman.h>
15528 +#include <linux/security.h>
15529 +#include "aufs.h"
15530 +
15531 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15532 +{
15533 +       int err;
15534 +       aufs_bindex_t bindex;
15535 +       struct dentry *dentry, *h_dentry;
15536 +       struct au_finfo *finfo;
15537 +       struct inode *h_inode;
15538 +
15539 +       FiMustWriteLock(file);
15540 +
15541 +       err = 0;
15542 +       dentry = file->f_path.dentry;
15543 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15544 +       finfo = au_fi(file);
15545 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15546 +       atomic_set(&finfo->fi_mmapped, 0);
15547 +       bindex = au_dbtop(dentry);
15548 +       if (!h_file) {
15549 +               h_dentry = au_h_dptr(dentry, bindex);
15550 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15551 +               if (unlikely(err))
15552 +                       goto out;
15553 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15554 +               if (IS_ERR(h_file)) {
15555 +                       err = PTR_ERR(h_file);
15556 +                       goto out;
15557 +               }
15558 +       } else {
15559 +               h_dentry = h_file->f_path.dentry;
15560 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15561 +               if (unlikely(err))
15562 +                       goto out;
15563 +               /* br ref is already inc-ed */
15564 +       }
15565 +
15566 +       if ((flags & __O_TMPFILE)
15567 +           && !(flags & O_EXCL)) {
15568 +               h_inode = file_inode(h_file);
15569 +               spin_lock(&h_inode->i_lock);
15570 +               h_inode->i_state |= I_LINKABLE;
15571 +               spin_unlock(&h_inode->i_lock);
15572 +       }
15573 +       au_set_fbtop(file, bindex);
15574 +       au_set_h_fptr(file, bindex, h_file);
15575 +       au_update_figen(file);
15576 +       /* todo: necessary? */
15577 +       /* file->f_ra = h_file->f_ra; */
15578 +
15579 +out:
15580 +       return err;
15581 +}
15582 +
15583 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15584 +                           struct file *file)
15585 +{
15586 +       int err;
15587 +       struct super_block *sb;
15588 +       struct au_do_open_args args = {
15589 +               .open   = au_do_open_nondir
15590 +       };
15591 +
15592 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15593 +             file, vfsub_file_flags(file), file->f_mode);
15594 +
15595 +       sb = file->f_path.dentry->d_sb;
15596 +       si_read_lock(sb, AuLock_FLUSH);
15597 +       err = au_do_open(file, &args);
15598 +       si_read_unlock(sb);
15599 +       return err;
15600 +}
15601 +
15602 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15603 +{
15604 +       struct au_finfo *finfo;
15605 +       aufs_bindex_t bindex;
15606 +
15607 +       finfo = au_fi(file);
15608 +       au_hbl_del(&finfo->fi_hlist,
15609 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15610 +       bindex = finfo->fi_btop;
15611 +       if (bindex >= 0)
15612 +               au_set_h_fptr(file, bindex, NULL);
15613 +
15614 +       au_finfo_fin(file);
15615 +       return 0;
15616 +}
15617 +
15618 +/* ---------------------------------------------------------------------- */
15619 +
15620 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15621 +{
15622 +       int err;
15623 +       struct file *h_file;
15624 +
15625 +       err = 0;
15626 +       h_file = au_hf_top(file);
15627 +       if (h_file)
15628 +               err = vfsub_flush(h_file, id);
15629 +       return err;
15630 +}
15631 +
15632 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15633 +{
15634 +       return au_do_flush(file, id, au_do_flush_nondir);
15635 +}
15636 +
15637 +/* ---------------------------------------------------------------------- */
15638 +/*
15639 + * read and write functions acquire [fdi]_rwsem once, but release before
15640 + * mmap_sem. This is because to stop a race condition between mmap(2).
15641 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15642 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15643 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15644 + */
15645 +
15646 +/* Callers should call au_read_post() or fput() in the end */
15647 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15648 +{
15649 +       struct file *h_file;
15650 +       int err;
15651 +
15652 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15653 +       if (!err) {
15654 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15655 +               h_file = au_hf_top(file);
15656 +               get_file(h_file);
15657 +               if (!keep_fi)
15658 +                       fi_read_unlock(file);
15659 +       } else
15660 +               h_file = ERR_PTR(err);
15661 +
15662 +       return h_file;
15663 +}
15664 +
15665 +static void au_read_post(struct inode *inode, struct file *h_file)
15666 +{
15667 +       /* update without lock, I don't think it a problem */
15668 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15669 +       fput(h_file);
15670 +}
15671 +
15672 +struct au_write_pre {
15673 +       /* input */
15674 +       unsigned int lsc;
15675 +
15676 +       /* output */
15677 +       blkcnt_t blks;
15678 +       aufs_bindex_t btop;
15679 +};
15680 +
15681 +/*
15682 + * return with iinfo is write-locked
15683 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15684 + * end
15685 + */
15686 +static struct file *au_write_pre(struct file *file, int do_ready,
15687 +                                struct au_write_pre *wpre)
15688 +{
15689 +       struct file *h_file;
15690 +       struct dentry *dentry;
15691 +       int err;
15692 +       unsigned int lsc;
15693 +       struct au_pin pin;
15694 +
15695 +       lsc = 0;
15696 +       if (wpre)
15697 +               lsc = wpre->lsc;
15698 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15699 +       h_file = ERR_PTR(err);
15700 +       if (unlikely(err))
15701 +               goto out;
15702 +
15703 +       dentry = file->f_path.dentry;
15704 +       if (do_ready) {
15705 +               err = au_ready_to_write(file, -1, &pin);
15706 +               if (unlikely(err)) {
15707 +                       h_file = ERR_PTR(err);
15708 +                       di_write_unlock(dentry);
15709 +                       goto out_fi;
15710 +               }
15711 +       }
15712 +
15713 +       di_downgrade_lock(dentry, /*flags*/0);
15714 +       if (wpre)
15715 +               wpre->btop = au_fbtop(file);
15716 +       h_file = au_hf_top(file);
15717 +       get_file(h_file);
15718 +       if (wpre)
15719 +               wpre->blks = file_inode(h_file)->i_blocks;
15720 +       if (do_ready)
15721 +               au_unpin(&pin);
15722 +       di_read_unlock(dentry, /*flags*/0);
15723 +
15724 +out_fi:
15725 +       fi_write_unlock(file);
15726 +out:
15727 +       return h_file;
15728 +}
15729 +
15730 +static void au_write_post(struct inode *inode, struct file *h_file,
15731 +                         struct au_write_pre *wpre, ssize_t written)
15732 +{
15733 +       struct inode *h_inode;
15734 +
15735 +       au_cpup_attr_timesizes(inode);
15736 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15737 +       h_inode = file_inode(h_file);
15738 +       inode->i_mode = h_inode->i_mode;
15739 +       ii_write_unlock(inode);
15740 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15741 +       if (written > 0)
15742 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15743 +                             /*force*/h_inode->i_blocks > wpre->blks);
15744 +       fput(h_file);
15745 +}
15746 +
15747 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
15748 +                        loff_t *ppos)
15749 +{
15750 +       ssize_t err;
15751 +       struct inode *inode;
15752 +       struct file *h_file;
15753 +       struct super_block *sb;
15754 +
15755 +       inode = file_inode(file);
15756 +       sb = inode->i_sb;
15757 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15758 +
15759 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15760 +       err = PTR_ERR(h_file);
15761 +       if (IS_ERR(h_file))
15762 +               goto out;
15763 +
15764 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
15765 +       err = vfsub_read_u(h_file, buf, count, ppos);
15766 +       /* todo: necessary? */
15767 +       /* file->f_ra = h_file->f_ra; */
15768 +       au_read_post(inode, h_file);
15769 +
15770 +out:
15771 +       si_read_unlock(sb);
15772 +       return err;
15773 +}
15774 +
15775 +/*
15776 + * todo: very ugly
15777 + * it locks both of i_mutex and si_rwsem for read in safe.
15778 + * if the plink maintenance mode continues forever (that is the problem),
15779 + * may loop forever.
15780 + */
15781 +static void au_mtx_and_read_lock(struct inode *inode)
15782 +{
15783 +       int err;
15784 +       struct super_block *sb = inode->i_sb;
15785 +
15786 +       while (1) {
15787 +               inode_lock(inode);
15788 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15789 +               if (!err)
15790 +                       break;
15791 +               inode_unlock(inode);
15792 +               si_read_lock(sb, AuLock_NOPLMW);
15793 +               si_read_unlock(sb);
15794 +       }
15795 +}
15796 +
15797 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
15798 +                         size_t count, loff_t *ppos)
15799 +{
15800 +       ssize_t err;
15801 +       struct au_write_pre wpre;
15802 +       struct inode *inode;
15803 +       struct file *h_file;
15804 +       char __user *buf = (char __user *)ubuf;
15805 +
15806 +       inode = file_inode(file);
15807 +       au_mtx_and_read_lock(inode);
15808 +
15809 +       wpre.lsc = 0;
15810 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15811 +       err = PTR_ERR(h_file);
15812 +       if (IS_ERR(h_file))
15813 +               goto out;
15814 +
15815 +       err = vfsub_write_u(h_file, buf, count, ppos);
15816 +       au_write_post(inode, h_file, &wpre, err);
15817 +
15818 +out:
15819 +       si_read_unlock(inode->i_sb);
15820 +       inode_unlock(inode);
15821 +       return err;
15822 +}
15823 +
15824 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15825 +                         struct iov_iter *iov_iter)
15826 +{
15827 +       ssize_t err;
15828 +       struct file *file;
15829 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15830 +
15831 +       err = security_file_permission(h_file, rw);
15832 +       if (unlikely(err))
15833 +               goto out;
15834 +
15835 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15836 +       iter = NULL;
15837 +       if (rw == MAY_READ)
15838 +               iter = h_file->f_op->read_iter;
15839 +       else if (rw == MAY_WRITE)
15840 +               iter = h_file->f_op->write_iter;
15841 +
15842 +       file = kio->ki_filp;
15843 +       kio->ki_filp = h_file;
15844 +       if (iter) {
15845 +               lockdep_off();
15846 +               err = iter(kio, iov_iter);
15847 +               lockdep_on();
15848 +       } else
15849 +               /* currently there is no such fs */
15850 +               WARN_ON_ONCE(1);
15851 +       kio->ki_filp = file;
15852 +
15853 +out:
15854 +       return err;
15855 +}
15856 +
15857 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15858 +{
15859 +       ssize_t err;
15860 +       struct file *file, *h_file;
15861 +       struct inode *inode;
15862 +       struct super_block *sb;
15863 +
15864 +       file = kio->ki_filp;
15865 +       inode = file_inode(file);
15866 +       sb = inode->i_sb;
15867 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15868 +
15869 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15870 +       err = PTR_ERR(h_file);
15871 +       if (IS_ERR(h_file))
15872 +               goto out;
15873 +
15874 +       if (au_test_loopback_kthread()) {
15875 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15876 +               if (file->f_mapping != h_file->f_mapping) {
15877 +                       file->f_mapping = h_file->f_mapping;
15878 +                       smp_mb(); /* unnecessary? */
15879 +               }
15880 +       }
15881 +       fi_read_unlock(file);
15882 +
15883 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15884 +       /* todo: necessary? */
15885 +       /* file->f_ra = h_file->f_ra; */
15886 +       au_read_post(inode, h_file);
15887 +
15888 +out:
15889 +       si_read_unlock(sb);
15890 +       return err;
15891 +}
15892 +
15893 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15894 +{
15895 +       ssize_t err;
15896 +       struct au_write_pre wpre;
15897 +       struct inode *inode;
15898 +       struct file *file, *h_file;
15899 +
15900 +       file = kio->ki_filp;
15901 +       inode = file_inode(file);
15902 +       au_mtx_and_read_lock(inode);
15903 +
15904 +       wpre.lsc = 0;
15905 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15906 +       err = PTR_ERR(h_file);
15907 +       if (IS_ERR(h_file))
15908 +               goto out;
15909 +
15910 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15911 +       au_write_post(inode, h_file, &wpre, err);
15912 +
15913 +out:
15914 +       si_read_unlock(inode->i_sb);
15915 +       inode_unlock(inode);
15916 +       return err;
15917 +}
15918 +
15919 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15920 +                               struct pipe_inode_info *pipe, size_t len,
15921 +                               unsigned int flags)
15922 +{
15923 +       ssize_t err;
15924 +       struct file *h_file;
15925 +       struct inode *inode;
15926 +       struct super_block *sb;
15927 +
15928 +       inode = file_inode(file);
15929 +       sb = inode->i_sb;
15930 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15931 +
15932 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15933 +       err = PTR_ERR(h_file);
15934 +       if (IS_ERR(h_file))
15935 +               goto out;
15936 +
15937 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15938 +       /* todo: necessary? */
15939 +       /* file->f_ra = h_file->f_ra; */
15940 +       au_read_post(inode, h_file);
15941 +
15942 +out:
15943 +       si_read_unlock(sb);
15944 +       return err;
15945 +}
15946 +
15947 +static ssize_t
15948 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15949 +                 size_t len, unsigned int flags)
15950 +{
15951 +       ssize_t err;
15952 +       struct au_write_pre wpre;
15953 +       struct inode *inode;
15954 +       struct file *h_file;
15955 +
15956 +       inode = file_inode(file);
15957 +       au_mtx_and_read_lock(inode);
15958 +
15959 +       wpre.lsc = 0;
15960 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15961 +       err = PTR_ERR(h_file);
15962 +       if (IS_ERR(h_file))
15963 +               goto out;
15964 +
15965 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15966 +       au_write_post(inode, h_file, &wpre, err);
15967 +
15968 +out:
15969 +       si_read_unlock(inode->i_sb);
15970 +       inode_unlock(inode);
15971 +       return err;
15972 +}
15973 +
15974 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15975 +                          loff_t len)
15976 +{
15977 +       long err;
15978 +       struct au_write_pre wpre;
15979 +       struct inode *inode;
15980 +       struct file *h_file;
15981 +
15982 +       inode = file_inode(file);
15983 +       au_mtx_and_read_lock(inode);
15984 +
15985 +       wpre.lsc = 0;
15986 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15987 +       err = PTR_ERR(h_file);
15988 +       if (IS_ERR(h_file))
15989 +               goto out;
15990 +
15991 +       lockdep_off();
15992 +       err = vfs_fallocate(h_file, mode, offset, len);
15993 +       lockdep_on();
15994 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15995 +
15996 +out:
15997 +       si_read_unlock(inode->i_sb);
15998 +       inode_unlock(inode);
15999 +       return err;
16000 +}
16001 +
16002 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
16003 +                                   struct file *dst, loff_t dst_pos,
16004 +                                   size_t len, unsigned int flags)
16005 +{
16006 +       ssize_t err;
16007 +       struct au_write_pre wpre;
16008 +       enum { SRC, DST };
16009 +       struct {
16010 +               struct inode *inode;
16011 +               struct file *h_file;
16012 +               struct super_block *h_sb;
16013 +       } a[2];
16014 +#define a_src  a[SRC]
16015 +#define a_dst  a[DST]
16016 +
16017 +       err = -EINVAL;
16018 +       a_src.inode = file_inode(src);
16019 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
16020 +               goto out;
16021 +       a_dst.inode = file_inode(dst);
16022 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
16023 +               goto out;
16024 +
16025 +       au_mtx_and_read_lock(a_dst.inode);
16026 +       /*
16027 +        * in order to match the order in di_write_lock2_{child,parent}(),
16028 +        * use f_path.dentry for this comparison.
16029 +        */
16030 +       if (src->f_path.dentry < dst->f_path.dentry) {
16031 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
16032 +               err = PTR_ERR(a_src.h_file);
16033 +               if (IS_ERR(a_src.h_file))
16034 +                       goto out_si;
16035 +
16036 +               wpre.lsc = AuLsc_FI_2;
16037 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16038 +               err = PTR_ERR(a_dst.h_file);
16039 +               if (IS_ERR(a_dst.h_file)) {
16040 +                       au_read_post(a_src.inode, a_src.h_file);
16041 +                       goto out_si;
16042 +               }
16043 +       } else {
16044 +               wpre.lsc = AuLsc_FI_1;
16045 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
16046 +               err = PTR_ERR(a_dst.h_file);
16047 +               if (IS_ERR(a_dst.h_file))
16048 +                       goto out_si;
16049 +
16050 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
16051 +               err = PTR_ERR(a_src.h_file);
16052 +               if (IS_ERR(a_src.h_file)) {
16053 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
16054 +                                     /*written*/0);
16055 +                       goto out_si;
16056 +               }
16057 +       }
16058 +
16059 +       err = -EXDEV;
16060 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
16061 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
16062 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
16063 +               AuDbgFile(src);
16064 +               AuDbgFile(dst);
16065 +               goto out_file;
16066 +       }
16067 +
16068 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
16069 +                                   dst_pos, len, flags);
16070 +
16071 +out_file:
16072 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
16073 +       fi_read_unlock(src);
16074 +       au_read_post(a_src.inode, a_src.h_file);
16075 +out_si:
16076 +       si_read_unlock(a_dst.inode->i_sb);
16077 +       inode_unlock(a_dst.inode);
16078 +out:
16079 +       return err;
16080 +#undef a_src
16081 +#undef a_dst
16082 +}
16083 +
16084 +/* ---------------------------------------------------------------------- */
16085 +
16086 +/*
16087 + * The locking order around current->mmap_sem.
16088 + * - in most and regular cases
16089 + *   file I/O syscall -- aufs_read() or something
16090 + *     -- si_rwsem for read -- mmap_sem
16091 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
16092 + * - in mmap case
16093 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
16094 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
16095 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
16096 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
16097 + * It means that when aufs acquires si_rwsem for write, the process should never
16098 + * acquire mmap_sem.
16099 + *
16100 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16101 + * problem either since any directory is not able to be mmap-ed.
16102 + * The similar scenario is applied to aufs_readlink() too.
16103 + */
16104 +
16105 +#if 0 /* stop calling security_file_mmap() */
16106 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16107 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16108 +
16109 +static unsigned long au_arch_prot_conv(unsigned long flags)
16110 +{
16111 +       /* currently ppc64 only */
16112 +#ifdef CONFIG_PPC64
16113 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16114 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16115 +       return AuConv_VM_PROT(flags, SAO);
16116 +#else
16117 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16118 +       return 0;
16119 +#endif
16120 +}
16121 +
16122 +static unsigned long au_prot_conv(unsigned long flags)
16123 +{
16124 +       return AuConv_VM_PROT(flags, READ)
16125 +               | AuConv_VM_PROT(flags, WRITE)
16126 +               | AuConv_VM_PROT(flags, EXEC)
16127 +               | au_arch_prot_conv(flags);
16128 +}
16129 +
16130 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16131 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16132 +
16133 +static unsigned long au_flag_conv(unsigned long flags)
16134 +{
16135 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16136 +               | AuConv_VM_MAP(flags, DENYWRITE)
16137 +               | AuConv_VM_MAP(flags, LOCKED);
16138 +}
16139 +#endif
16140 +
16141 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16142 +{
16143 +       int err;
16144 +       const unsigned char wlock
16145 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16146 +       struct super_block *sb;
16147 +       struct file *h_file;
16148 +       struct inode *inode;
16149 +
16150 +       AuDbgVmRegion(file, vma);
16151 +
16152 +       inode = file_inode(file);
16153 +       sb = inode->i_sb;
16154 +       lockdep_off();
16155 +       si_read_lock(sb, AuLock_NOPLMW);
16156 +
16157 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16158 +       lockdep_on();
16159 +       err = PTR_ERR(h_file);
16160 +       if (IS_ERR(h_file))
16161 +               goto out;
16162 +
16163 +       err = 0;
16164 +       au_set_mmapped(file);
16165 +       au_vm_file_reset(vma, h_file);
16166 +       /*
16167 +        * we cannot call security_mmap_file() here since it may acquire
16168 +        * mmap_sem or i_mutex.
16169 +        *
16170 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16171 +        *                       au_flag_conv(vma->vm_flags));
16172 +        */
16173 +       if (!err)
16174 +               err = call_mmap(h_file, vma);
16175 +       if (!err) {
16176 +               au_vm_prfile_set(vma, file);
16177 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16178 +               goto out_fput; /* success */
16179 +       }
16180 +       au_unset_mmapped(file);
16181 +       au_vm_file_reset(vma, file);
16182 +
16183 +out_fput:
16184 +       lockdep_off();
16185 +       ii_write_unlock(inode);
16186 +       lockdep_on();
16187 +       fput(h_file);
16188 +out:
16189 +       lockdep_off();
16190 +       si_read_unlock(sb);
16191 +       lockdep_on();
16192 +       AuTraceErr(err);
16193 +       return err;
16194 +}
16195 +
16196 +/* ---------------------------------------------------------------------- */
16197 +
16198 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16199 +                            int datasync)
16200 +{
16201 +       int err;
16202 +       struct au_write_pre wpre;
16203 +       struct inode *inode;
16204 +       struct file *h_file;
16205 +
16206 +       err = 0; /* -EBADF; */ /* posix? */
16207 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16208 +               goto out;
16209 +
16210 +       inode = file_inode(file);
16211 +       au_mtx_and_read_lock(inode);
16212 +
16213 +       wpre.lsc = 0;
16214 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16215 +       err = PTR_ERR(h_file);
16216 +       if (IS_ERR(h_file))
16217 +               goto out_unlock;
16218 +
16219 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16220 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16221 +
16222 +out_unlock:
16223 +       si_read_unlock(inode->i_sb);
16224 +       inode_unlock(inode);
16225 +out:
16226 +       return err;
16227 +}
16228 +
16229 +static int aufs_fasync(int fd, struct file *file, int flag)
16230 +{
16231 +       int err;
16232 +       struct file *h_file;
16233 +       struct super_block *sb;
16234 +
16235 +       sb = file->f_path.dentry->d_sb;
16236 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16237 +
16238 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16239 +       err = PTR_ERR(h_file);
16240 +       if (IS_ERR(h_file))
16241 +               goto out;
16242 +
16243 +       if (h_file->f_op->fasync)
16244 +               err = h_file->f_op->fasync(fd, h_file, flag);
16245 +       fput(h_file); /* instead of au_read_post() */
16246 +
16247 +out:
16248 +       si_read_unlock(sb);
16249 +       return err;
16250 +}
16251 +
16252 +static int aufs_setfl(struct file *file, unsigned long arg)
16253 +{
16254 +       int err;
16255 +       struct file *h_file;
16256 +       struct super_block *sb;
16257 +
16258 +       sb = file->f_path.dentry->d_sb;
16259 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16260 +
16261 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16262 +       err = PTR_ERR(h_file);
16263 +       if (IS_ERR(h_file))
16264 +               goto out;
16265 +
16266 +       /* stop calling h_file->fasync */
16267 +       arg |= vfsub_file_flags(file) & FASYNC;
16268 +       err = setfl(/*unused fd*/-1, h_file, arg);
16269 +       fput(h_file); /* instead of au_read_post() */
16270 +
16271 +out:
16272 +       si_read_unlock(sb);
16273 +       return err;
16274 +}
16275 +
16276 +/* ---------------------------------------------------------------------- */
16277 +
16278 +/* no one supports this operation, currently */
16279 +#if 0 /* reserved for future use */
16280 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16281 +                            size_t len, loff_t *pos, int more)
16282 +{
16283 +}
16284 +#endif
16285 +
16286 +/* ---------------------------------------------------------------------- */
16287 +
16288 +const struct file_operations aufs_file_fop = {
16289 +       .owner          = THIS_MODULE,
16290 +
16291 +       .llseek         = default_llseek,
16292 +
16293 +       .read           = aufs_read,
16294 +       .write          = aufs_write,
16295 +       .read_iter      = aufs_read_iter,
16296 +       .write_iter     = aufs_write_iter,
16297 +
16298 +#ifdef CONFIG_AUFS_POLL
16299 +       .poll           = aufs_poll,
16300 +#endif
16301 +       .unlocked_ioctl = aufs_ioctl_nondir,
16302 +#ifdef CONFIG_COMPAT
16303 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16304 +#endif
16305 +       .mmap           = aufs_mmap,
16306 +       .open           = aufs_open_nondir,
16307 +       .flush          = aufs_flush_nondir,
16308 +       .release        = aufs_release_nondir,
16309 +       .fsync          = aufs_fsync_nondir,
16310 +       .fasync         = aufs_fasync,
16311 +       /* .sendpage    = aufs_sendpage, */
16312 +       .setfl          = aufs_setfl,
16313 +       .splice_write   = aufs_splice_write,
16314 +       .splice_read    = aufs_splice_read,
16315 +#if 0 /* reserved for future use */
16316 +       .aio_splice_write = aufs_aio_splice_write,
16317 +       .aio_splice_read  = aufs_aio_splice_read,
16318 +#endif
16319 +       .fallocate      = aufs_fallocate,
16320 +       .copy_file_range = aufs_copy_file_range
16321 +};
16322 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16323 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16324 +++ linux/fs/aufs/fstype.h      2020-01-27 10:57:18.172204883 +0100
16325 @@ -0,0 +1,401 @@
16326 +/* SPDX-License-Identifier: GPL-2.0 */
16327 +/*
16328 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16329 + *
16330 + * This program, aufs is free software; you can redistribute it and/or modify
16331 + * it under the terms of the GNU General Public License as published by
16332 + * the Free Software Foundation; either version 2 of the License, or
16333 + * (at your option) any later version.
16334 + *
16335 + * This program is distributed in the hope that it will be useful,
16336 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16337 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16338 + * GNU General Public License for more details.
16339 + *
16340 + * You should have received a copy of the GNU General Public License
16341 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16342 + */
16343 +
16344 +/*
16345 + * judging filesystem type
16346 + */
16347 +
16348 +#ifndef __AUFS_FSTYPE_H__
16349 +#define __AUFS_FSTYPE_H__
16350 +
16351 +#ifdef __KERNEL__
16352 +
16353 +#include <linux/fs.h>
16354 +#include <linux/magic.h>
16355 +#include <linux/nfs_fs.h>
16356 +#include <linux/romfs_fs.h>
16357 +
16358 +static inline int au_test_aufs(struct super_block *sb)
16359 +{
16360 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16361 +}
16362 +
16363 +static inline const char *au_sbtype(struct super_block *sb)
16364 +{
16365 +       return sb->s_type->name;
16366 +}
16367 +
16368 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16369 +{
16370 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16371 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16372 +#else
16373 +       return 0;
16374 +#endif
16375 +}
16376 +
16377 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16378 +{
16379 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16380 +       return sb->s_magic == ROMFS_MAGIC;
16381 +#else
16382 +       return 0;
16383 +#endif
16384 +}
16385 +
16386 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16387 +{
16388 +#if IS_ENABLED(CONFIG_CRAMFS)
16389 +       return sb->s_magic == CRAMFS_MAGIC;
16390 +#endif
16391 +       return 0;
16392 +}
16393 +
16394 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16395 +{
16396 +#if IS_ENABLED(CONFIG_NFS_FS)
16397 +       return sb->s_magic == NFS_SUPER_MAGIC;
16398 +#else
16399 +       return 0;
16400 +#endif
16401 +}
16402 +
16403 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16404 +{
16405 +#if IS_ENABLED(CONFIG_FUSE_FS)
16406 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16407 +#else
16408 +       return 0;
16409 +#endif
16410 +}
16411 +
16412 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16413 +{
16414 +#if IS_ENABLED(CONFIG_XFS_FS)
16415 +       return sb->s_magic == XFS_SB_MAGIC;
16416 +#else
16417 +       return 0;
16418 +#endif
16419 +}
16420 +
16421 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16422 +{
16423 +#ifdef CONFIG_TMPFS
16424 +       return sb->s_magic == TMPFS_MAGIC;
16425 +#else
16426 +       return 0;
16427 +#endif
16428 +}
16429 +
16430 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16431 +{
16432 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16433 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16434 +#else
16435 +       return 0;
16436 +#endif
16437 +}
16438 +
16439 +static inline int au_test_ramfs(struct super_block *sb)
16440 +{
16441 +       return sb->s_magic == RAMFS_MAGIC;
16442 +}
16443 +
16444 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16445 +{
16446 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16447 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16448 +#else
16449 +       return 0;
16450 +#endif
16451 +}
16452 +
16453 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16454 +{
16455 +#ifdef CONFIG_PROC_FS
16456 +       return sb->s_magic == PROC_SUPER_MAGIC;
16457 +#else
16458 +       return 0;
16459 +#endif
16460 +}
16461 +
16462 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16463 +{
16464 +#ifdef CONFIG_SYSFS
16465 +       return sb->s_magic == SYSFS_MAGIC;
16466 +#else
16467 +       return 0;
16468 +#endif
16469 +}
16470 +
16471 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16472 +{
16473 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16474 +       return sb->s_magic == CONFIGFS_MAGIC;
16475 +#else
16476 +       return 0;
16477 +#endif
16478 +}
16479 +
16480 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16481 +{
16482 +#if IS_ENABLED(CONFIG_MINIX_FS)
16483 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16484 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16485 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16486 +               || sb->s_magic == MINIX_SUPER_MAGIC
16487 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16488 +#else
16489 +       return 0;
16490 +#endif
16491 +}
16492 +
16493 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16494 +{
16495 +#if IS_ENABLED(CONFIG_FAT_FS)
16496 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16497 +#else
16498 +       return 0;
16499 +#endif
16500 +}
16501 +
16502 +static inline int au_test_msdos(struct super_block *sb)
16503 +{
16504 +       return au_test_fat(sb);
16505 +}
16506 +
16507 +static inline int au_test_vfat(struct super_block *sb)
16508 +{
16509 +       return au_test_fat(sb);
16510 +}
16511 +
16512 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16513 +{
16514 +#ifdef CONFIG_SECURITYFS
16515 +       return sb->s_magic == SECURITYFS_MAGIC;
16516 +#else
16517 +       return 0;
16518 +#endif
16519 +}
16520 +
16521 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16522 +{
16523 +#if IS_ENABLED(CONFIG_SQUASHFS)
16524 +       return sb->s_magic == SQUASHFS_MAGIC;
16525 +#else
16526 +       return 0;
16527 +#endif
16528 +}
16529 +
16530 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16531 +{
16532 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16533 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16534 +#else
16535 +       return 0;
16536 +#endif
16537 +}
16538 +
16539 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16540 +{
16541 +#if IS_ENABLED(CONFIG_XENFS)
16542 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16543 +#else
16544 +       return 0;
16545 +#endif
16546 +}
16547 +
16548 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16549 +{
16550 +#ifdef CONFIG_DEBUG_FS
16551 +       return sb->s_magic == DEBUGFS_MAGIC;
16552 +#else
16553 +       return 0;
16554 +#endif
16555 +}
16556 +
16557 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16558 +{
16559 +#if IS_ENABLED(CONFIG_NILFS)
16560 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16561 +#else
16562 +       return 0;
16563 +#endif
16564 +}
16565 +
16566 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16567 +{
16568 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16569 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16570 +#else
16571 +       return 0;
16572 +#endif
16573 +}
16574 +
16575 +/* ---------------------------------------------------------------------- */
16576 +/*
16577 + * they can't be an aufs branch.
16578 + */
16579 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16580 +{
16581 +       return
16582 +#ifndef CONFIG_AUFS_BR_RAMFS
16583 +               au_test_ramfs(sb) ||
16584 +#endif
16585 +               au_test_procfs(sb)
16586 +               || au_test_sysfs(sb)
16587 +               || au_test_configfs(sb)
16588 +               || au_test_debugfs(sb)
16589 +               || au_test_securityfs(sb)
16590 +               || au_test_xenfs(sb)
16591 +               || au_test_ecryptfs(sb)
16592 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16593 +               || au_test_aufs(sb); /* will be supported in next version */
16594 +}
16595 +
16596 +static inline int au_test_fs_remote(struct super_block *sb)
16597 +{
16598 +       return !au_test_tmpfs(sb)
16599 +#ifdef CONFIG_AUFS_BR_RAMFS
16600 +               && !au_test_ramfs(sb)
16601 +#endif
16602 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16603 +}
16604 +
16605 +/* ---------------------------------------------------------------------- */
16606 +
16607 +/*
16608 + * Note: these functions (below) are created after reading ->getattr() in all
16609 + * filesystems under linux/fs. it means we have to do so in every update...
16610 + */
16611 +
16612 +/*
16613 + * some filesystems require getattr to refresh the inode attributes before
16614 + * referencing.
16615 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16616 + * and leave the work for d_revalidate()
16617 + */
16618 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16619 +{
16620 +       return au_test_nfs(sb)
16621 +               || au_test_fuse(sb)
16622 +               /* || au_test_btrfs(sb) */      /* untested */
16623 +               ;
16624 +}
16625 +
16626 +/*
16627 + * filesystems which don't maintain i_size or i_blocks.
16628 + */
16629 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16630 +{
16631 +       return au_test_xfs(sb)
16632 +               || au_test_btrfs(sb)
16633 +               || au_test_ubifs(sb)
16634 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16635 +               /* || au_test_minix(sb) */      /* untested */
16636 +               ;
16637 +}
16638 +
16639 +/*
16640 + * filesystems which don't store the correct value in some of their inode
16641 + * attributes.
16642 + */
16643 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16644 +{
16645 +       return au_test_fs_bad_iattr_size(sb)
16646 +               || au_test_fat(sb)
16647 +               || au_test_msdos(sb)
16648 +               || au_test_vfat(sb);
16649 +}
16650 +
16651 +/* they don't check i_nlink in link(2) */
16652 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16653 +{
16654 +       return au_test_tmpfs(sb)
16655 +#ifdef CONFIG_AUFS_BR_RAMFS
16656 +               || au_test_ramfs(sb)
16657 +#endif
16658 +               || au_test_ubifs(sb)
16659 +               || au_test_hfsplus(sb);
16660 +}
16661 +
16662 +/*
16663 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16664 + */
16665 +static inline int au_test_fs_notime(struct super_block *sb)
16666 +{
16667 +       return au_test_nfs(sb)
16668 +               || au_test_fuse(sb)
16669 +               || au_test_ubifs(sb)
16670 +               ;
16671 +}
16672 +
16673 +/* temporary support for i#1 in cramfs */
16674 +static inline int au_test_fs_unique_ino(struct inode *inode)
16675 +{
16676 +       if (au_test_cramfs(inode->i_sb))
16677 +               return inode->i_ino != 1;
16678 +       return 1;
16679 +}
16680 +
16681 +/* ---------------------------------------------------------------------- */
16682 +
16683 +/*
16684 + * the filesystem where the xino files placed must support i/o after unlink and
16685 + * maintain i_size and i_blocks.
16686 + */
16687 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16688 +{
16689 +       return au_test_fs_remote(sb)
16690 +               || au_test_fs_bad_iattr_size(sb)
16691 +               /* don't want unnecessary work for xino */
16692 +               || au_test_aufs(sb)
16693 +               || au_test_ecryptfs(sb)
16694 +               || au_test_nilfs(sb);
16695 +}
16696 +
16697 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16698 +{
16699 +       return au_test_tmpfs(sb)
16700 +               || au_test_ramfs(sb);
16701 +}
16702 +
16703 +/*
16704 + * test if the @sb is real-readonly.
16705 + */
16706 +static inline int au_test_fs_rr(struct super_block *sb)
16707 +{
16708 +       return au_test_squashfs(sb)
16709 +               || au_test_iso9660(sb)
16710 +               || au_test_cramfs(sb)
16711 +               || au_test_romfs(sb);
16712 +}
16713 +
16714 +/*
16715 + * test if the @inode is nfs with 'noacl' option
16716 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16717 + */
16718 +static inline int au_test_nfs_noacl(struct inode *inode)
16719 +{
16720 +       return au_test_nfs(inode->i_sb)
16721 +               /* && IS_POSIXACL(inode) */
16722 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16723 +}
16724 +
16725 +#endif /* __KERNEL__ */
16726 +#endif /* __AUFS_FSTYPE_H__ */
16727 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16728 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16729 +++ linux/fs/aufs/hbl.h 2020-01-27 10:57:18.172204883 +0100
16730 @@ -0,0 +1,65 @@
16731 +/* SPDX-License-Identifier: GPL-2.0 */
16732 +/*
16733 + * Copyright (C) 2017-2020 Junjiro R. Okajima
16734 + *
16735 + * This program, aufs is free software; you can redistribute it and/or modify
16736 + * it under the terms of the GNU General Public License as published by
16737 + * the Free Software Foundation; either version 2 of the License, or
16738 + * (at your option) any later version.
16739 + *
16740 + * This program is distributed in the hope that it will be useful,
16741 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16742 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16743 + * GNU General Public License for more details.
16744 + *
16745 + * You should have received a copy of the GNU General Public License
16746 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16747 + */
16748 +
16749 +/*
16750 + * helpers for hlist_bl.h
16751 + */
16752 +
16753 +#ifndef __AUFS_HBL_H__
16754 +#define __AUFS_HBL_H__
16755 +
16756 +#ifdef __KERNEL__
16757 +
16758 +#include <linux/list_bl.h>
16759 +
16760 +static inline void au_hbl_add(struct hlist_bl_node *node,
16761 +                             struct hlist_bl_head *hbl)
16762 +{
16763 +       hlist_bl_lock(hbl);
16764 +       hlist_bl_add_head(node, hbl);
16765 +       hlist_bl_unlock(hbl);
16766 +}
16767 +
16768 +static inline void au_hbl_del(struct hlist_bl_node *node,
16769 +                             struct hlist_bl_head *hbl)
16770 +{
16771 +       hlist_bl_lock(hbl);
16772 +       hlist_bl_del(node);
16773 +       hlist_bl_unlock(hbl);
16774 +}
16775 +
16776 +#define au_hbl_for_each(pos, head)                                     \
16777 +       for (pos = hlist_bl_first(head);                                \
16778 +            pos;                                                       \
16779 +            pos = pos->next)
16780 +
16781 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16782 +{
16783 +       unsigned long cnt;
16784 +       struct hlist_bl_node *pos;
16785 +
16786 +       cnt = 0;
16787 +       hlist_bl_lock(hbl);
16788 +       au_hbl_for_each(pos, hbl)
16789 +               cnt++;
16790 +       hlist_bl_unlock(hbl);
16791 +       return cnt;
16792 +}
16793 +
16794 +#endif /* __KERNEL__ */
16795 +#endif /* __AUFS_HBL_H__ */
16796 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16797 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16798 +++ linux/fs/aufs/hfsnotify.c   2020-01-27 10:57:18.172204883 +0100
16799 @@ -0,0 +1,288 @@
16800 +// SPDX-License-Identifier: GPL-2.0
16801 +/*
16802 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16803 + *
16804 + * This program, aufs is free software; you can redistribute it and/or modify
16805 + * it under the terms of the GNU General Public License as published by
16806 + * the Free Software Foundation; either version 2 of the License, or
16807 + * (at your option) any later version.
16808 + *
16809 + * This program is distributed in the hope that it will be useful,
16810 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16811 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16812 + * GNU General Public License for more details.
16813 + *
16814 + * You should have received a copy of the GNU General Public License
16815 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16816 + */
16817 +
16818 +/*
16819 + * fsnotify for the lower directories
16820 + */
16821 +
16822 +#include "aufs.h"
16823 +
16824 +/* FS_IN_IGNORED is unnecessary */
16825 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16826 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16827 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16828 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16829 +
16830 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16831 +{
16832 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16833 +                                            hn_mark);
16834 +       /* AuDbg("here\n"); */
16835 +       au_cache_free_hnotify(hn);
16836 +       smp_mb__before_atomic(); /* for atomic64_dec */
16837 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16838 +               wake_up(&au_hfsn_wq);
16839 +}
16840 +
16841 +static int au_hfsn_alloc(struct au_hinode *hinode)
16842 +{
16843 +       int err;
16844 +       struct au_hnotify *hn;
16845 +       struct super_block *sb;
16846 +       struct au_branch *br;
16847 +       struct fsnotify_mark *mark;
16848 +       aufs_bindex_t bindex;
16849 +
16850 +       hn = hinode->hi_notify;
16851 +       sb = hn->hn_aufs_inode->i_sb;
16852 +       bindex = au_br_index(sb, hinode->hi_id);
16853 +       br = au_sbr(sb, bindex);
16854 +       AuDebugOn(!br->br_hfsn);
16855 +
16856 +       mark = &hn->hn_mark;
16857 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16858 +       mark->mask = AuHfsnMask;
16859 +       /*
16860 +        * by udba rename or rmdir, aufs assign a new inode to the known
16861 +        * h_inode, so specify 1 to allow dups.
16862 +        */
16863 +       lockdep_off();
16864 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16865 +       lockdep_on();
16866 +
16867 +       return err;
16868 +}
16869 +
16870 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16871 +{
16872 +       struct fsnotify_mark *mark;
16873 +       unsigned long long ull;
16874 +       struct fsnotify_group *group;
16875 +
16876 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16877 +       BUG_ON(!ull);
16878 +
16879 +       mark = &hn->hn_mark;
16880 +       spin_lock(&mark->lock);
16881 +       group = mark->group;
16882 +       fsnotify_get_group(group);
16883 +       spin_unlock(&mark->lock);
16884 +       lockdep_off();
16885 +       fsnotify_destroy_mark(mark, group);
16886 +       fsnotify_put_mark(mark);
16887 +       fsnotify_put_group(group);
16888 +       lockdep_on();
16889 +
16890 +       /* free hn by myself */
16891 +       return 0;
16892 +}
16893 +
16894 +/* ---------------------------------------------------------------------- */
16895 +
16896 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16897 +{
16898 +       struct fsnotify_mark *mark;
16899 +
16900 +       mark = &hinode->hi_notify->hn_mark;
16901 +       spin_lock(&mark->lock);
16902 +       if (do_set) {
16903 +               AuDebugOn(mark->mask & AuHfsnMask);
16904 +               mark->mask |= AuHfsnMask;
16905 +       } else {
16906 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16907 +               mark->mask &= ~AuHfsnMask;
16908 +       }
16909 +       spin_unlock(&mark->lock);
16910 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16911 +}
16912 +
16913 +/* ---------------------------------------------------------------------- */
16914 +
16915 +/* #define AuDbgHnotify */
16916 +#ifdef AuDbgHnotify
16917 +static char *au_hfsn_name(u32 mask)
16918 +{
16919 +#ifdef CONFIG_AUFS_DEBUG
16920 +#define test_ret(flag)                         \
16921 +       do {                                    \
16922 +               if (mask & flag)                \
16923 +                       return #flag;           \
16924 +       } while (0)
16925 +       test_ret(FS_ACCESS);
16926 +       test_ret(FS_MODIFY);
16927 +       test_ret(FS_ATTRIB);
16928 +       test_ret(FS_CLOSE_WRITE);
16929 +       test_ret(FS_CLOSE_NOWRITE);
16930 +       test_ret(FS_OPEN);
16931 +       test_ret(FS_MOVED_FROM);
16932 +       test_ret(FS_MOVED_TO);
16933 +       test_ret(FS_CREATE);
16934 +       test_ret(FS_DELETE);
16935 +       test_ret(FS_DELETE_SELF);
16936 +       test_ret(FS_MOVE_SELF);
16937 +       test_ret(FS_UNMOUNT);
16938 +       test_ret(FS_Q_OVERFLOW);
16939 +       test_ret(FS_IN_IGNORED);
16940 +       test_ret(FS_ISDIR);
16941 +       test_ret(FS_IN_ONESHOT);
16942 +       test_ret(FS_EVENT_ON_CHILD);
16943 +       return "";
16944 +#undef test_ret
16945 +#else
16946 +       return "??";
16947 +#endif
16948 +}
16949 +#endif
16950 +
16951 +/* ---------------------------------------------------------------------- */
16952 +
16953 +static void au_hfsn_free_group(struct fsnotify_group *group)
16954 +{
16955 +       struct au_br_hfsnotify *hfsn = group->private;
16956 +
16957 +       /* AuDbg("here\n"); */
16958 +       au_kfree_try_rcu(hfsn);
16959 +}
16960 +
16961 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16962 +                               struct inode *inode,
16963 +                               u32 mask, const void *data, int data_type,
16964 +                               const struct qstr *file_name, u32 cookie,
16965 +                               struct fsnotify_iter_info *iter_info)
16966 +{
16967 +       int err;
16968 +       struct au_hnotify *hnotify;
16969 +       struct inode *h_dir, *h_inode;
16970 +       struct fsnotify_mark *inode_mark;
16971 +
16972 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16973 +
16974 +       err = 0;
16975 +       /* if FS_UNMOUNT happens, there must be another bug */
16976 +       AuDebugOn(mask & FS_UNMOUNT);
16977 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16978 +               goto out;
16979 +
16980 +       h_dir = inode;
16981 +       h_inode = NULL;
16982 +#ifdef AuDbgHnotify
16983 +       au_debug_on();
16984 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16985 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16986 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16987 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16988 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16989 +               /* WARN_ON(1); */
16990 +       }
16991 +       au_debug_off();
16992 +#endif
16993 +
16994 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16995 +       AuDebugOn(!inode_mark);
16996 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16997 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16998 +
16999 +out:
17000 +       return err;
17001 +}
17002 +
17003 +static struct fsnotify_ops au_hfsn_ops = {
17004 +       .handle_event           = au_hfsn_handle_event,
17005 +       .free_group_priv        = au_hfsn_free_group,
17006 +       .free_mark              = au_hfsn_free_mark
17007 +};
17008 +
17009 +/* ---------------------------------------------------------------------- */
17010 +
17011 +static void au_hfsn_fin_br(struct au_branch *br)
17012 +{
17013 +       struct au_br_hfsnotify *hfsn;
17014 +
17015 +       hfsn = br->br_hfsn;
17016 +       if (hfsn) {
17017 +               lockdep_off();
17018 +               fsnotify_put_group(hfsn->hfsn_group);
17019 +               lockdep_on();
17020 +       }
17021 +}
17022 +
17023 +static int au_hfsn_init_br(struct au_branch *br, int perm)
17024 +{
17025 +       int err;
17026 +       struct fsnotify_group *group;
17027 +       struct au_br_hfsnotify *hfsn;
17028 +
17029 +       err = 0;
17030 +       br->br_hfsn = NULL;
17031 +       if (!au_br_hnotifyable(perm))
17032 +               goto out;
17033 +
17034 +       err = -ENOMEM;
17035 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
17036 +       if (unlikely(!hfsn))
17037 +               goto out;
17038 +
17039 +       err = 0;
17040 +       group = fsnotify_alloc_group(&au_hfsn_ops);
17041 +       if (IS_ERR(group)) {
17042 +               err = PTR_ERR(group);
17043 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
17044 +               goto out_hfsn;
17045 +       }
17046 +
17047 +       group->private = hfsn;
17048 +       hfsn->hfsn_group = group;
17049 +       br->br_hfsn = hfsn;
17050 +       goto out; /* success */
17051 +
17052 +out_hfsn:
17053 +       au_kfree_try_rcu(hfsn);
17054 +out:
17055 +       return err;
17056 +}
17057 +
17058 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
17059 +{
17060 +       int err;
17061 +
17062 +       err = 0;
17063 +       if (!br->br_hfsn)
17064 +               err = au_hfsn_init_br(br, perm);
17065 +
17066 +       return err;
17067 +}
17068 +
17069 +/* ---------------------------------------------------------------------- */
17070 +
17071 +static void au_hfsn_fin(void)
17072 +{
17073 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
17074 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
17075 +}
17076 +
17077 +const struct au_hnotify_op au_hnotify_op = {
17078 +       .ctl            = au_hfsn_ctl,
17079 +       .alloc          = au_hfsn_alloc,
17080 +       .free           = au_hfsn_free,
17081 +
17082 +       .fin            = au_hfsn_fin,
17083 +
17084 +       .reset_br       = au_hfsn_reset_br,
17085 +       .fin_br         = au_hfsn_fin_br,
17086 +       .init_br        = au_hfsn_init_br
17087 +};
17088 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
17089 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
17090 +++ linux/fs/aufs/hfsplus.c     2020-01-27 10:57:18.172204883 +0100
17091 @@ -0,0 +1,60 @@
17092 +// SPDX-License-Identifier: GPL-2.0
17093 +/*
17094 + * Copyright (C) 2010-2020 Junjiro R. Okajima
17095 + *
17096 + * This program, aufs is free software; you can redistribute it and/or modify
17097 + * it under the terms of the GNU General Public License as published by
17098 + * the Free Software Foundation; either version 2 of the License, or
17099 + * (at your option) any later version.
17100 + *
17101 + * This program is distributed in the hope that it will be useful,
17102 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17103 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17104 + * GNU General Public License for more details.
17105 + *
17106 + * You should have received a copy of the GNU General Public License
17107 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17108 + */
17109 +
17110 +/*
17111 + * special support for filesystems which acquires an inode mutex
17112 + * at final closing a file, eg, hfsplus.
17113 + *
17114 + * This trick is very simple and stupid, just to open the file before really
17115 + * necessary open to tell hfsplus that this is not the final closing.
17116 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17117 + * and au_h_open_post() after releasing it.
17118 + */
17119 +
17120 +#include "aufs.h"
17121 +
17122 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17123 +                          int force_wr)
17124 +{
17125 +       struct file *h_file;
17126 +       struct dentry *h_dentry;
17127 +
17128 +       h_dentry = au_h_dptr(dentry, bindex);
17129 +       AuDebugOn(!h_dentry);
17130 +       AuDebugOn(d_is_negative(h_dentry));
17131 +
17132 +       h_file = NULL;
17133 +       if (au_test_hfsplus(h_dentry->d_sb)
17134 +           && d_is_reg(h_dentry))
17135 +               h_file = au_h_open(dentry, bindex,
17136 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17137 +                                  /*file*/NULL, force_wr);
17138 +       return h_file;
17139 +}
17140 +
17141 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17142 +                   struct file *h_file)
17143 +{
17144 +       struct au_branch *br;
17145 +
17146 +       if (h_file) {
17147 +               fput(h_file);
17148 +               br = au_sbr(dentry->d_sb, bindex);
17149 +               au_lcnt_dec(&br->br_nfiles);
17150 +       }
17151 +}
17152 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17153 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17154 +++ linux/fs/aufs/hnotify.c     2020-01-27 10:57:18.172204883 +0100
17155 @@ -0,0 +1,715 @@
17156 +// SPDX-License-Identifier: GPL-2.0
17157 +/*
17158 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17159 + *
17160 + * This program, aufs is free software; you can redistribute it and/or modify
17161 + * it under the terms of the GNU General Public License as published by
17162 + * the Free Software Foundation; either version 2 of the License, or
17163 + * (at your option) any later version.
17164 + *
17165 + * This program is distributed in the hope that it will be useful,
17166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17168 + * GNU General Public License for more details.
17169 + *
17170 + * You should have received a copy of the GNU General Public License
17171 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17172 + */
17173 +
17174 +/*
17175 + * abstraction to notify the direct changes on lower directories
17176 + */
17177 +
17178 +/* #include <linux/iversion.h> */
17179 +#include "aufs.h"
17180 +
17181 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17182 +{
17183 +       int err;
17184 +       struct au_hnotify *hn;
17185 +
17186 +       err = -ENOMEM;
17187 +       hn = au_cache_alloc_hnotify();
17188 +       if (hn) {
17189 +               hn->hn_aufs_inode = inode;
17190 +               hinode->hi_notify = hn;
17191 +               err = au_hnotify_op.alloc(hinode);
17192 +               AuTraceErr(err);
17193 +               if (unlikely(err)) {
17194 +                       hinode->hi_notify = NULL;
17195 +                       au_cache_free_hnotify(hn);
17196 +                       /*
17197 +                        * The upper dir was removed by udba, but the same named
17198 +                        * dir left. In this case, aufs assigns a new inode
17199 +                        * number and set the monitor again.
17200 +                        * For the lower dir, the old monitor is still left.
17201 +                        */
17202 +                       if (err == -EEXIST)
17203 +                               err = 0;
17204 +               }
17205 +       }
17206 +
17207 +       AuTraceErr(err);
17208 +       return err;
17209 +}
17210 +
17211 +void au_hn_free(struct au_hinode *hinode)
17212 +{
17213 +       struct au_hnotify *hn;
17214 +
17215 +       hn = hinode->hi_notify;
17216 +       if (hn) {
17217 +               hinode->hi_notify = NULL;
17218 +               if (au_hnotify_op.free(hinode, hn))
17219 +                       au_cache_free_hnotify(hn);
17220 +       }
17221 +}
17222 +
17223 +/* ---------------------------------------------------------------------- */
17224 +
17225 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17226 +{
17227 +       if (hinode->hi_notify)
17228 +               au_hnotify_op.ctl(hinode, do_set);
17229 +}
17230 +
17231 +void au_hn_reset(struct inode *inode, unsigned int flags)
17232 +{
17233 +       aufs_bindex_t bindex, bbot;
17234 +       struct inode *hi;
17235 +       struct dentry *iwhdentry;
17236 +
17237 +       bbot = au_ibbot(inode);
17238 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17239 +               hi = au_h_iptr(inode, bindex);
17240 +               if (!hi)
17241 +                       continue;
17242 +
17243 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17244 +               iwhdentry = au_hi_wh(inode, bindex);
17245 +               if (iwhdentry)
17246 +                       dget(iwhdentry);
17247 +               au_igrab(hi);
17248 +               au_set_h_iptr(inode, bindex, NULL, 0);
17249 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17250 +                             flags & ~AuHi_XINO);
17251 +               iput(hi);
17252 +               dput(iwhdentry);
17253 +               /* inode_unlock(hi); */
17254 +       }
17255 +}
17256 +
17257 +/* ---------------------------------------------------------------------- */
17258 +
17259 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17260 +{
17261 +       int err;
17262 +       aufs_bindex_t bindex, bbot, bfound, btop;
17263 +       struct inode *h_i;
17264 +
17265 +       err = 0;
17266 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17267 +               pr_warn("branch root dir was changed\n");
17268 +               goto out;
17269 +       }
17270 +
17271 +       bfound = -1;
17272 +       bbot = au_ibbot(inode);
17273 +       btop = au_ibtop(inode);
17274 +#if 0 /* reserved for future use */
17275 +       if (bindex == bbot) {
17276 +               /* keep this ino in rename case */
17277 +               goto out;
17278 +       }
17279 +#endif
17280 +       for (bindex = btop; bindex <= bbot; bindex++)
17281 +               if (au_h_iptr(inode, bindex) == h_inode) {
17282 +                       bfound = bindex;
17283 +                       break;
17284 +               }
17285 +       if (bfound < 0)
17286 +               goto out;
17287 +
17288 +       for (bindex = btop; bindex <= bbot; bindex++) {
17289 +               h_i = au_h_iptr(inode, bindex);
17290 +               if (!h_i)
17291 +                       continue;
17292 +
17293 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17294 +               /* ignore this error */
17295 +               /* bad action? */
17296 +       }
17297 +
17298 +       /* children inode number will be broken */
17299 +
17300 +out:
17301 +       AuTraceErr(err);
17302 +       return err;
17303 +}
17304 +
17305 +static int hn_gen_tree(struct dentry *dentry)
17306 +{
17307 +       int err, i, j, ndentry;
17308 +       struct au_dcsub_pages dpages;
17309 +       struct au_dpage *dpage;
17310 +       struct dentry **dentries;
17311 +
17312 +       err = au_dpages_init(&dpages, GFP_NOFS);
17313 +       if (unlikely(err))
17314 +               goto out;
17315 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17316 +       if (unlikely(err))
17317 +               goto out_dpages;
17318 +
17319 +       for (i = 0; i < dpages.ndpage; i++) {
17320 +               dpage = dpages.dpages + i;
17321 +               dentries = dpage->dentries;
17322 +               ndentry = dpage->ndentry;
17323 +               for (j = 0; j < ndentry; j++) {
17324 +                       struct dentry *d;
17325 +
17326 +                       d = dentries[j];
17327 +                       if (IS_ROOT(d))
17328 +                               continue;
17329 +
17330 +                       au_digen_dec(d);
17331 +                       if (d_really_is_positive(d))
17332 +                               /* todo: reset children xino?
17333 +                                  cached children only? */
17334 +                               au_iigen_dec(d_inode(d));
17335 +               }
17336 +       }
17337 +
17338 +out_dpages:
17339 +       au_dpages_free(&dpages);
17340 +out:
17341 +       return err;
17342 +}
17343 +
17344 +/*
17345 + * return 0 if processed.
17346 + */
17347 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17348 +                          const unsigned int isdir)
17349 +{
17350 +       int err;
17351 +       struct dentry *d;
17352 +       struct qstr *dname;
17353 +
17354 +       err = 1;
17355 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17356 +               pr_warn("branch root dir was changed\n");
17357 +               err = 0;
17358 +               goto out;
17359 +       }
17360 +
17361 +       if (!isdir) {
17362 +               AuDebugOn(!name);
17363 +               au_iigen_dec(inode);
17364 +               spin_lock(&inode->i_lock);
17365 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17366 +                       spin_lock(&d->d_lock);
17367 +                       dname = &d->d_name;
17368 +                       if (dname->len != nlen
17369 +                           && memcmp(dname->name, name, nlen)) {
17370 +                               spin_unlock(&d->d_lock);
17371 +                               continue;
17372 +                       }
17373 +                       err = 0;
17374 +                       au_digen_dec(d);
17375 +                       spin_unlock(&d->d_lock);
17376 +                       break;
17377 +               }
17378 +               spin_unlock(&inode->i_lock);
17379 +       } else {
17380 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17381 +               d = d_find_any_alias(inode);
17382 +               if (!d) {
17383 +                       au_iigen_dec(inode);
17384 +                       goto out;
17385 +               }
17386 +
17387 +               spin_lock(&d->d_lock);
17388 +               dname = &d->d_name;
17389 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17390 +                       spin_unlock(&d->d_lock);
17391 +                       err = hn_gen_tree(d);
17392 +                       spin_lock(&d->d_lock);
17393 +               }
17394 +               spin_unlock(&d->d_lock);
17395 +               dput(d);
17396 +       }
17397 +
17398 +out:
17399 +       AuTraceErr(err);
17400 +       return err;
17401 +}
17402 +
17403 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17404 +{
17405 +       int err;
17406 +
17407 +       if (IS_ROOT(dentry)) {
17408 +               pr_warn("branch root dir was changed\n");
17409 +               return 0;
17410 +       }
17411 +
17412 +       err = 0;
17413 +       if (!isdir) {
17414 +               au_digen_dec(dentry);
17415 +               if (d_really_is_positive(dentry))
17416 +                       au_iigen_dec(d_inode(dentry));
17417 +       } else {
17418 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17419 +               if (d_really_is_positive(dentry))
17420 +                       err = hn_gen_tree(dentry);
17421 +       }
17422 +
17423 +       AuTraceErr(err);
17424 +       return err;
17425 +}
17426 +
17427 +/* ---------------------------------------------------------------------- */
17428 +
17429 +/* hnotify job flags */
17430 +#define AuHnJob_XINO0          1
17431 +#define AuHnJob_GEN            (1 << 1)
17432 +#define AuHnJob_DIRENT         (1 << 2)
17433 +#define AuHnJob_ISDIR          (1 << 3)
17434 +#define AuHnJob_TRYXINO0       (1 << 4)
17435 +#define AuHnJob_MNTPNT         (1 << 5)
17436 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17437 +#define au_fset_hnjob(flags, name) \
17438 +       do { (flags) |= AuHnJob_##name; } while (0)
17439 +#define au_fclr_hnjob(flags, name) \
17440 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17441 +
17442 +enum {
17443 +       AuHn_CHILD,
17444 +       AuHn_PARENT,
17445 +       AuHnLast
17446 +};
17447 +
17448 +struct au_hnotify_args {
17449 +       struct inode *h_dir, *dir, *h_child_inode;
17450 +       u32 mask;
17451 +       unsigned int flags[AuHnLast];
17452 +       unsigned int h_child_nlen;
17453 +       char h_child_name[];
17454 +};
17455 +
17456 +struct hn_job_args {
17457 +       unsigned int flags;
17458 +       struct inode *inode, *h_inode, *dir, *h_dir;
17459 +       struct dentry *dentry;
17460 +       char *h_name;
17461 +       int h_nlen;
17462 +};
17463 +
17464 +static int hn_job(struct hn_job_args *a)
17465 +{
17466 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17467 +       int e;
17468 +
17469 +       /* reset xino */
17470 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17471 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17472 +
17473 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17474 +           && a->inode
17475 +           && a->h_inode) {
17476 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17477 +               if (!a->h_inode->i_nlink
17478 +                   && !(a->h_inode->i_state & I_LINKABLE))
17479 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17480 +               inode_unlock_shared(a->h_inode);
17481 +       }
17482 +
17483 +       /* make the generation obsolete */
17484 +       if (au_ftest_hnjob(a->flags, GEN)) {
17485 +               e = -1;
17486 +               if (a->inode)
17487 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17488 +                                             isdir);
17489 +               if (e && a->dentry)
17490 +                       hn_gen_by_name(a->dentry, isdir);
17491 +               /* ignore this error */
17492 +       }
17493 +
17494 +       /* make dir entries obsolete */
17495 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17496 +               struct au_vdir *vdir;
17497 +
17498 +               vdir = au_ivdir(a->inode);
17499 +               if (vdir)
17500 +                       vdir->vd_jiffy = 0;
17501 +               /* IMustLock(a->inode); */
17502 +               /* inode_inc_iversion(a->inode); */
17503 +       }
17504 +
17505 +       /* can do nothing but warn */
17506 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17507 +           && a->dentry
17508 +           && d_mountpoint(a->dentry))
17509 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17510 +
17511 +       return 0;
17512 +}
17513 +
17514 +/* ---------------------------------------------------------------------- */
17515 +
17516 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17517 +                                          struct inode *dir)
17518 +{
17519 +       struct dentry *dentry, *d, *parent;
17520 +       struct qstr *dname;
17521 +
17522 +       parent = d_find_any_alias(dir);
17523 +       if (!parent)
17524 +               return NULL;
17525 +
17526 +       dentry = NULL;
17527 +       spin_lock(&parent->d_lock);
17528 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17529 +               /* AuDbg("%pd\n", d); */
17530 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17531 +               dname = &d->d_name;
17532 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17533 +                       goto cont_unlock;
17534 +               if (au_di(d))
17535 +                       au_digen_dec(d);
17536 +               else
17537 +                       goto cont_unlock;
17538 +               if (au_dcount(d) > 0) {
17539 +                       dentry = dget_dlock(d);
17540 +                       spin_unlock(&d->d_lock);
17541 +                       break;
17542 +               }
17543 +
17544 +cont_unlock:
17545 +               spin_unlock(&d->d_lock);
17546 +       }
17547 +       spin_unlock(&parent->d_lock);
17548 +       dput(parent);
17549 +
17550 +       if (dentry)
17551 +               di_write_lock_child(dentry);
17552 +
17553 +       return dentry;
17554 +}
17555 +
17556 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17557 +                                        aufs_bindex_t bindex, ino_t h_ino)
17558 +{
17559 +       struct inode *inode;
17560 +       ino_t ino;
17561 +       int err;
17562 +
17563 +       inode = NULL;
17564 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17565 +       if (!err && ino)
17566 +               inode = ilookup(sb, ino);
17567 +       if (!inode)
17568 +               goto out;
17569 +
17570 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17571 +               pr_warn("wrong root branch\n");
17572 +               iput(inode);
17573 +               inode = NULL;
17574 +               goto out;
17575 +       }
17576 +
17577 +       ii_write_lock_child(inode);
17578 +
17579 +out:
17580 +       return inode;
17581 +}
17582 +
17583 +static void au_hn_bh(void *_args)
17584 +{
17585 +       struct au_hnotify_args *a = _args;
17586 +       struct super_block *sb;
17587 +       aufs_bindex_t bindex, bbot, bfound;
17588 +       unsigned char xino, try_iput;
17589 +       int err;
17590 +       struct inode *inode;
17591 +       ino_t h_ino;
17592 +       struct hn_job_args args;
17593 +       struct dentry *dentry;
17594 +       struct au_sbinfo *sbinfo;
17595 +
17596 +       AuDebugOn(!_args);
17597 +       AuDebugOn(!a->h_dir);
17598 +       AuDebugOn(!a->dir);
17599 +       AuDebugOn(!a->mask);
17600 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17601 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17602 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17603 +
17604 +       inode = NULL;
17605 +       dentry = NULL;
17606 +       /*
17607 +        * do not lock a->dir->i_mutex here
17608 +        * because of d_revalidate() may cause a deadlock.
17609 +        */
17610 +       sb = a->dir->i_sb;
17611 +       AuDebugOn(!sb);
17612 +       sbinfo = au_sbi(sb);
17613 +       AuDebugOn(!sbinfo);
17614 +       si_write_lock(sb, AuLock_NOPLMW);
17615 +
17616 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17617 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17618 +               case FS_MOVED_FROM:
17619 +               case FS_MOVED_TO:
17620 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17621 +                               "for the direct rename(2)\n");
17622 +               }
17623 +
17624 +       ii_read_lock_parent(a->dir);
17625 +       bfound = -1;
17626 +       bbot = au_ibbot(a->dir);
17627 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17628 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17629 +                       bfound = bindex;
17630 +                       break;
17631 +               }
17632 +       ii_read_unlock(a->dir);
17633 +       if (unlikely(bfound < 0))
17634 +               goto out;
17635 +
17636 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17637 +       h_ino = 0;
17638 +       if (a->h_child_inode)
17639 +               h_ino = a->h_child_inode->i_ino;
17640 +
17641 +       if (a->h_child_nlen
17642 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17643 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17644 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17645 +                                             a->dir);
17646 +       try_iput = 0;
17647 +       if (dentry && d_really_is_positive(dentry))
17648 +               inode = d_inode(dentry);
17649 +       if (xino && !inode && h_ino
17650 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17651 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17652 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17653 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17654 +               try_iput = 1;
17655 +       }
17656 +
17657 +       args.flags = a->flags[AuHn_CHILD];
17658 +       args.dentry = dentry;
17659 +       args.inode = inode;
17660 +       args.h_inode = a->h_child_inode;
17661 +       args.dir = a->dir;
17662 +       args.h_dir = a->h_dir;
17663 +       args.h_name = a->h_child_name;
17664 +       args.h_nlen = a->h_child_nlen;
17665 +       err = hn_job(&args);
17666 +       if (dentry) {
17667 +               if (au_di(dentry))
17668 +                       di_write_unlock(dentry);
17669 +               dput(dentry);
17670 +       }
17671 +       if (inode && try_iput) {
17672 +               ii_write_unlock(inode);
17673 +               iput(inode);
17674 +       }
17675 +
17676 +       ii_write_lock_parent(a->dir);
17677 +       args.flags = a->flags[AuHn_PARENT];
17678 +       args.dentry = NULL;
17679 +       args.inode = a->dir;
17680 +       args.h_inode = a->h_dir;
17681 +       args.dir = NULL;
17682 +       args.h_dir = NULL;
17683 +       args.h_name = NULL;
17684 +       args.h_nlen = 0;
17685 +       err = hn_job(&args);
17686 +       ii_write_unlock(a->dir);
17687 +
17688 +out:
17689 +       iput(a->h_child_inode);
17690 +       iput(a->h_dir);
17691 +       iput(a->dir);
17692 +       si_write_unlock(sb);
17693 +       au_nwt_done(&sbinfo->si_nowait);
17694 +       au_kfree_rcu(a);
17695 +}
17696 +
17697 +/* ---------------------------------------------------------------------- */
17698 +
17699 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17700 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17701 +{
17702 +       int err, len;
17703 +       unsigned int flags[AuHnLast], f;
17704 +       unsigned char isdir, isroot, wh;
17705 +       struct inode *dir;
17706 +       struct au_hnotify_args *args;
17707 +       char *p, *h_child_name;
17708 +
17709 +       err = 0;
17710 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17711 +       dir = igrab(hnotify->hn_aufs_inode);
17712 +       if (!dir)
17713 +               goto out;
17714 +
17715 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17716 +       wh = 0;
17717 +       h_child_name = (void *)h_child_qstr->name;
17718 +       len = h_child_qstr->len;
17719 +       if (h_child_name) {
17720 +               if (len > AUFS_WH_PFX_LEN
17721 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17722 +                       h_child_name += AUFS_WH_PFX_LEN;
17723 +                       len -= AUFS_WH_PFX_LEN;
17724 +                       wh = 1;
17725 +               }
17726 +       }
17727 +
17728 +       isdir = 0;
17729 +       if (h_child_inode)
17730 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17731 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17732 +       flags[AuHn_CHILD] = 0;
17733 +       if (isdir)
17734 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17735 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17736 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17737 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17738 +       case FS_MOVED_FROM:
17739 +       case FS_MOVED_TO:
17740 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17741 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17742 +               /*FALLTHROUGH*/
17743 +       case FS_CREATE:
17744 +               AuDebugOn(!h_child_name);
17745 +               break;
17746 +
17747 +       case FS_DELETE:
17748 +               /*
17749 +                * aufs never be able to get this child inode.
17750 +                * revalidation should be in d_revalidate()
17751 +                * by checking i_nlink, i_generation or d_unhashed().
17752 +                */
17753 +               AuDebugOn(!h_child_name);
17754 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17755 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17756 +               break;
17757 +
17758 +       default:
17759 +               AuDebugOn(1);
17760 +       }
17761 +
17762 +       if (wh)
17763 +               h_child_inode = NULL;
17764 +
17765 +       err = -ENOMEM;
17766 +       /* iput() and kfree() will be called in au_hnotify() */
17767 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17768 +       if (unlikely(!args)) {
17769 +               AuErr1("no memory\n");
17770 +               iput(dir);
17771 +               goto out;
17772 +       }
17773 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17774 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17775 +       args->mask = mask;
17776 +       args->dir = dir;
17777 +       args->h_dir = igrab(h_dir);
17778 +       if (h_child_inode)
17779 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17780 +       args->h_child_inode = h_child_inode;
17781 +       args->h_child_nlen = len;
17782 +       if (len) {
17783 +               p = (void *)args;
17784 +               p += sizeof(*args);
17785 +               memcpy(p, h_child_name, len);
17786 +               p[len] = 0;
17787 +       }
17788 +
17789 +       /* NFS fires the event for silly-renamed one from kworker */
17790 +       f = 0;
17791 +       if (!dir->i_nlink
17792 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17793 +               f = AuWkq_NEST;
17794 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17795 +       if (unlikely(err)) {
17796 +               pr_err("wkq %d\n", err);
17797 +               iput(args->h_child_inode);
17798 +               iput(args->h_dir);
17799 +               iput(args->dir);
17800 +               au_kfree_rcu(args);
17801 +       }
17802 +
17803 +out:
17804 +       return err;
17805 +}
17806 +
17807 +/* ---------------------------------------------------------------------- */
17808 +
17809 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17810 +{
17811 +       int err;
17812 +
17813 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17814 +
17815 +       err = 0;
17816 +       if (au_hnotify_op.reset_br)
17817 +               err = au_hnotify_op.reset_br(udba, br, perm);
17818 +
17819 +       return err;
17820 +}
17821 +
17822 +int au_hnotify_init_br(struct au_branch *br, int perm)
17823 +{
17824 +       int err;
17825 +
17826 +       err = 0;
17827 +       if (au_hnotify_op.init_br)
17828 +               err = au_hnotify_op.init_br(br, perm);
17829 +
17830 +       return err;
17831 +}
17832 +
17833 +void au_hnotify_fin_br(struct au_branch *br)
17834 +{
17835 +       if (au_hnotify_op.fin_br)
17836 +               au_hnotify_op.fin_br(br);
17837 +}
17838 +
17839 +static void au_hn_destroy_cache(void)
17840 +{
17841 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17842 +       au_cache[AuCache_HNOTIFY] = NULL;
17843 +}
17844 +
17845 +int __init au_hnotify_init(void)
17846 +{
17847 +       int err;
17848 +
17849 +       err = -ENOMEM;
17850 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17851 +       if (au_cache[AuCache_HNOTIFY]) {
17852 +               err = 0;
17853 +               if (au_hnotify_op.init)
17854 +                       err = au_hnotify_op.init();
17855 +               if (unlikely(err))
17856 +                       au_hn_destroy_cache();
17857 +       }
17858 +       AuTraceErr(err);
17859 +       return err;
17860 +}
17861 +
17862 +void au_hnotify_fin(void)
17863 +{
17864 +       if (au_hnotify_op.fin)
17865 +               au_hnotify_op.fin();
17866 +
17867 +       /* cf. au_cache_fin() */
17868 +       if (au_cache[AuCache_HNOTIFY])
17869 +               au_hn_destroy_cache();
17870 +}
17871 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17872 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17873 +++ linux/fs/aufs/iinfo.c       2020-01-27 10:57:18.175538316 +0100
17874 @@ -0,0 +1,286 @@
17875 +// SPDX-License-Identifier: GPL-2.0
17876 +/*
17877 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17878 + *
17879 + * This program, aufs is free software; you can redistribute it and/or modify
17880 + * it under the terms of the GNU General Public License as published by
17881 + * the Free Software Foundation; either version 2 of the License, or
17882 + * (at your option) any later version.
17883 + *
17884 + * This program is distributed in the hope that it will be useful,
17885 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17886 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17887 + * GNU General Public License for more details.
17888 + *
17889 + * You should have received a copy of the GNU General Public License
17890 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17891 + */
17892 +
17893 +/*
17894 + * inode private data
17895 + */
17896 +
17897 +#include "aufs.h"
17898 +
17899 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17900 +{
17901 +       struct inode *h_inode;
17902 +       struct au_hinode *hinode;
17903 +
17904 +       IiMustAnyLock(inode);
17905 +
17906 +       hinode = au_hinode(au_ii(inode), bindex);
17907 +       h_inode = hinode->hi_inode;
17908 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17909 +       return h_inode;
17910 +}
17911 +
17912 +/* todo: hard/soft set? */
17913 +void au_hiput(struct au_hinode *hinode)
17914 +{
17915 +       au_hn_free(hinode);
17916 +       dput(hinode->hi_whdentry);
17917 +       iput(hinode->hi_inode);
17918 +}
17919 +
17920 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17921 +{
17922 +       unsigned int flags;
17923 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17924 +
17925 +       flags = 0;
17926 +       if (au_opt_test(mnt_flags, XINO))
17927 +               au_fset_hi(flags, XINO);
17928 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17929 +               au_fset_hi(flags, HNOTIFY);
17930 +       return flags;
17931 +}
17932 +
17933 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17934 +                  struct inode *h_inode, unsigned int flags)
17935 +{
17936 +       struct au_hinode *hinode;
17937 +       struct inode *hi;
17938 +       struct au_iinfo *iinfo = au_ii(inode);
17939 +
17940 +       IiMustWriteLock(inode);
17941 +
17942 +       hinode = au_hinode(iinfo, bindex);
17943 +       hi = hinode->hi_inode;
17944 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17945 +
17946 +       if (hi)
17947 +               au_hiput(hinode);
17948 +       hinode->hi_inode = h_inode;
17949 +       if (h_inode) {
17950 +               int err;
17951 +               struct super_block *sb = inode->i_sb;
17952 +               struct au_branch *br;
17953 +
17954 +               AuDebugOn(inode->i_mode
17955 +                         && (h_inode->i_mode & S_IFMT)
17956 +                         != (inode->i_mode & S_IFMT));
17957 +               if (bindex == iinfo->ii_btop)
17958 +                       au_cpup_igen(inode, h_inode);
17959 +               br = au_sbr(sb, bindex);
17960 +               hinode->hi_id = br->br_id;
17961 +               if (au_ftest_hi(flags, XINO)) {
17962 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17963 +                                           inode->i_ino);
17964 +                       if (unlikely(err))
17965 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17966 +               }
17967 +
17968 +               if (au_ftest_hi(flags, HNOTIFY)
17969 +                   && au_br_hnotifyable(br->br_perm)) {
17970 +                       err = au_hn_alloc(hinode, inode);
17971 +                       if (unlikely(err))
17972 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17973 +               }
17974 +       }
17975 +}
17976 +
17977 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17978 +                 struct dentry *h_wh)
17979 +{
17980 +       struct au_hinode *hinode;
17981 +
17982 +       IiMustWriteLock(inode);
17983 +
17984 +       hinode = au_hinode(au_ii(inode), bindex);
17985 +       AuDebugOn(hinode->hi_whdentry);
17986 +       hinode->hi_whdentry = h_wh;
17987 +}
17988 +
17989 +void au_update_iigen(struct inode *inode, int half)
17990 +{
17991 +       struct au_iinfo *iinfo;
17992 +       struct au_iigen *iigen;
17993 +       unsigned int sigen;
17994 +
17995 +       sigen = au_sigen(inode->i_sb);
17996 +       iinfo = au_ii(inode);
17997 +       iigen = &iinfo->ii_generation;
17998 +       spin_lock(&iigen->ig_spin);
17999 +       iigen->ig_generation = sigen;
18000 +       if (half)
18001 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
18002 +       else
18003 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
18004 +       spin_unlock(&iigen->ig_spin);
18005 +}
18006 +
18007 +/* it may be called at remount time, too */
18008 +void au_update_ibrange(struct inode *inode, int do_put_zero)
18009 +{
18010 +       struct au_iinfo *iinfo;
18011 +       aufs_bindex_t bindex, bbot;
18012 +
18013 +       AuDebugOn(au_is_bad_inode(inode));
18014 +       IiMustWriteLock(inode);
18015 +
18016 +       iinfo = au_ii(inode);
18017 +       if (do_put_zero && iinfo->ii_btop >= 0) {
18018 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18019 +                    bindex++) {
18020 +                       struct inode *h_i;
18021 +
18022 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
18023 +                       if (h_i
18024 +                           && !h_i->i_nlink
18025 +                           && !(h_i->i_state & I_LINKABLE))
18026 +                               au_set_h_iptr(inode, bindex, NULL, 0);
18027 +               }
18028 +       }
18029 +
18030 +       iinfo->ii_btop = -1;
18031 +       iinfo->ii_bbot = -1;
18032 +       bbot = au_sbbot(inode->i_sb);
18033 +       for (bindex = 0; bindex <= bbot; bindex++)
18034 +               if (au_hinode(iinfo, bindex)->hi_inode) {
18035 +                       iinfo->ii_btop = bindex;
18036 +                       break;
18037 +               }
18038 +       if (iinfo->ii_btop >= 0)
18039 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
18040 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
18041 +                               iinfo->ii_bbot = bindex;
18042 +                               break;
18043 +                       }
18044 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
18045 +}
18046 +
18047 +/* ---------------------------------------------------------------------- */
18048 +
18049 +void au_icntnr_init_once(void *_c)
18050 +{
18051 +       struct au_icntnr *c = _c;
18052 +       struct au_iinfo *iinfo = &c->iinfo;
18053 +
18054 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
18055 +       au_rw_init(&iinfo->ii_rwsem);
18056 +       inode_init_once(&c->vfs_inode);
18057 +}
18058 +
18059 +void au_hinode_init(struct au_hinode *hinode)
18060 +{
18061 +       hinode->hi_inode = NULL;
18062 +       hinode->hi_id = -1;
18063 +       au_hn_init(hinode);
18064 +       hinode->hi_whdentry = NULL;
18065 +}
18066 +
18067 +int au_iinfo_init(struct inode *inode)
18068 +{
18069 +       struct au_iinfo *iinfo;
18070 +       struct super_block *sb;
18071 +       struct au_hinode *hi;
18072 +       int nbr, i;
18073 +
18074 +       sb = inode->i_sb;
18075 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18076 +       nbr = au_sbbot(sb) + 1;
18077 +       if (unlikely(nbr <= 0))
18078 +               nbr = 1;
18079 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
18080 +       if (hi) {
18081 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
18082 +
18083 +               iinfo->ii_hinode = hi;
18084 +               for (i = 0; i < nbr; i++, hi++)
18085 +                       au_hinode_init(hi);
18086 +
18087 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
18088 +               iinfo->ii_btop = -1;
18089 +               iinfo->ii_bbot = -1;
18090 +               iinfo->ii_vdir = NULL;
18091 +               return 0;
18092 +       }
18093 +       return -ENOMEM;
18094 +}
18095 +
18096 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
18097 +{
18098 +       int err, i;
18099 +       struct au_hinode *hip;
18100 +
18101 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18102 +
18103 +       err = -ENOMEM;
18104 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18105 +                         may_shrink);
18106 +       if (hip) {
18107 +               iinfo->ii_hinode = hip;
18108 +               i = iinfo->ii_bbot + 1;
18109 +               hip += i;
18110 +               for (; i < nbr; i++, hip++)
18111 +                       au_hinode_init(hip);
18112 +               err = 0;
18113 +       }
18114 +
18115 +       return err;
18116 +}
18117 +
18118 +void au_iinfo_fin(struct inode *inode)
18119 +{
18120 +       struct au_iinfo *iinfo;
18121 +       struct au_hinode *hi;
18122 +       struct super_block *sb;
18123 +       aufs_bindex_t bindex, bbot;
18124 +       const unsigned char unlinked = !inode->i_nlink;
18125 +
18126 +       AuDebugOn(au_is_bad_inode(inode));
18127 +
18128 +       sb = inode->i_sb;
18129 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18130 +       if (si_pid_test(sb))
18131 +               au_xino_delete_inode(inode, unlinked);
18132 +       else {
18133 +               /*
18134 +                * it is safe to hide the dependency between sbinfo and
18135 +                * sb->s_umount.
18136 +                */
18137 +               lockdep_off();
18138 +               si_noflush_read_lock(sb);
18139 +               au_xino_delete_inode(inode, unlinked);
18140 +               si_read_unlock(sb);
18141 +               lockdep_on();
18142 +       }
18143 +
18144 +       iinfo = au_ii(inode);
18145 +       if (iinfo->ii_vdir)
18146 +               au_vdir_free(iinfo->ii_vdir);
18147 +
18148 +       bindex = iinfo->ii_btop;
18149 +       if (bindex >= 0) {
18150 +               hi = au_hinode(iinfo, bindex);
18151 +               bbot = iinfo->ii_bbot;
18152 +               while (bindex++ <= bbot) {
18153 +                       if (hi->hi_inode)
18154 +                               au_hiput(hi);
18155 +                       hi++;
18156 +               }
18157 +       }
18158 +       au_kfree_rcu(iinfo->ii_hinode);
18159 +       AuRwDestroy(&iinfo->ii_rwsem);
18160 +}
18161 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18162 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18163 +++ linux/fs/aufs/inode.c       2020-01-27 10:57:18.175538316 +0100
18164 @@ -0,0 +1,529 @@
18165 +// SPDX-License-Identifier: GPL-2.0
18166 +/*
18167 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18168 + *
18169 + * This program, aufs is free software; you can redistribute it and/or modify
18170 + * it under the terms of the GNU General Public License as published by
18171 + * the Free Software Foundation; either version 2 of the License, or
18172 + * (at your option) any later version.
18173 + *
18174 + * This program is distributed in the hope that it will be useful,
18175 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18176 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18177 + * GNU General Public License for more details.
18178 + *
18179 + * You should have received a copy of the GNU General Public License
18180 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18181 + */
18182 +
18183 +/*
18184 + * inode functions
18185 + */
18186 +
18187 +#include <linux/iversion.h>
18188 +#include "aufs.h"
18189 +
18190 +struct inode *au_igrab(struct inode *inode)
18191 +{
18192 +       if (inode) {
18193 +               AuDebugOn(!atomic_read(&inode->i_count));
18194 +               ihold(inode);
18195 +       }
18196 +       return inode;
18197 +}
18198 +
18199 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18200 +{
18201 +       au_cpup_attr_all(inode, /*force*/0);
18202 +       au_update_iigen(inode, /*half*/1);
18203 +       if (do_version)
18204 +               inode_inc_iversion(inode);
18205 +}
18206 +
18207 +static int au_ii_refresh(struct inode *inode, int *update)
18208 +{
18209 +       int err, e, nbr;
18210 +       umode_t type;
18211 +       aufs_bindex_t bindex, new_bindex;
18212 +       struct super_block *sb;
18213 +       struct au_iinfo *iinfo;
18214 +       struct au_hinode *p, *q, tmp;
18215 +
18216 +       AuDebugOn(au_is_bad_inode(inode));
18217 +       IiMustWriteLock(inode);
18218 +
18219 +       *update = 0;
18220 +       sb = inode->i_sb;
18221 +       nbr = au_sbbot(sb) + 1;
18222 +       type = inode->i_mode & S_IFMT;
18223 +       iinfo = au_ii(inode);
18224 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18225 +       if (unlikely(err))
18226 +               goto out;
18227 +
18228 +       AuDebugOn(iinfo->ii_btop < 0);
18229 +       p = au_hinode(iinfo, iinfo->ii_btop);
18230 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18231 +            bindex++, p++) {
18232 +               if (!p->hi_inode)
18233 +                       continue;
18234 +
18235 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18236 +               new_bindex = au_br_index(sb, p->hi_id);
18237 +               if (new_bindex == bindex)
18238 +                       continue;
18239 +
18240 +               if (new_bindex < 0) {
18241 +                       *update = 1;
18242 +                       au_hiput(p);
18243 +                       p->hi_inode = NULL;
18244 +                       continue;
18245 +               }
18246 +
18247 +               if (new_bindex < iinfo->ii_btop)
18248 +                       iinfo->ii_btop = new_bindex;
18249 +               if (iinfo->ii_bbot < new_bindex)
18250 +                       iinfo->ii_bbot = new_bindex;
18251 +               /* swap two lower inode, and loop again */
18252 +               q = au_hinode(iinfo, new_bindex);
18253 +               tmp = *q;
18254 +               *q = *p;
18255 +               *p = tmp;
18256 +               if (tmp.hi_inode) {
18257 +                       bindex--;
18258 +                       p--;
18259 +               }
18260 +       }
18261 +       au_update_ibrange(inode, /*do_put_zero*/0);
18262 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18263 +       e = au_dy_irefresh(inode);
18264 +       if (unlikely(e && !err))
18265 +               err = e;
18266 +
18267 +out:
18268 +       AuTraceErr(err);
18269 +       return err;
18270 +}
18271 +
18272 +void au_refresh_iop(struct inode *inode, int force_getattr)
18273 +{
18274 +       int type;
18275 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18276 +       const struct inode_operations *iop
18277 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18278 +
18279 +       if (inode->i_op == iop)
18280 +               return;
18281 +
18282 +       switch (inode->i_mode & S_IFMT) {
18283 +       case S_IFDIR:
18284 +               type = AuIop_DIR;
18285 +               break;
18286 +       case S_IFLNK:
18287 +               type = AuIop_SYMLINK;
18288 +               break;
18289 +       default:
18290 +               type = AuIop_OTHER;
18291 +               break;
18292 +       }
18293 +
18294 +       inode->i_op = iop + type;
18295 +       /* unnecessary smp_wmb() */
18296 +}
18297 +
18298 +int au_refresh_hinode_self(struct inode *inode)
18299 +{
18300 +       int err, update;
18301 +
18302 +       err = au_ii_refresh(inode, &update);
18303 +       if (!err)
18304 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18305 +
18306 +       AuTraceErr(err);
18307 +       return err;
18308 +}
18309 +
18310 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18311 +{
18312 +       int err, e, update;
18313 +       unsigned int flags;
18314 +       umode_t mode;
18315 +       aufs_bindex_t bindex, bbot;
18316 +       unsigned char isdir;
18317 +       struct au_hinode *p;
18318 +       struct au_iinfo *iinfo;
18319 +
18320 +       err = au_ii_refresh(inode, &update);
18321 +       if (unlikely(err))
18322 +               goto out;
18323 +
18324 +       update = 0;
18325 +       iinfo = au_ii(inode);
18326 +       p = au_hinode(iinfo, iinfo->ii_btop);
18327 +       mode = (inode->i_mode & S_IFMT);
18328 +       isdir = S_ISDIR(mode);
18329 +       flags = au_hi_flags(inode, isdir);
18330 +       bbot = au_dbbot(dentry);
18331 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18332 +               struct inode *h_i, *h_inode;
18333 +               struct dentry *h_d;
18334 +
18335 +               h_d = au_h_dptr(dentry, bindex);
18336 +               if (!h_d || d_is_negative(h_d))
18337 +                       continue;
18338 +
18339 +               h_inode = d_inode(h_d);
18340 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18341 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18342 +                       h_i = au_h_iptr(inode, bindex);
18343 +                       if (h_i) {
18344 +                               if (h_i == h_inode)
18345 +                                       continue;
18346 +                               err = -EIO;
18347 +                               break;
18348 +                       }
18349 +               }
18350 +               if (bindex < iinfo->ii_btop)
18351 +                       iinfo->ii_btop = bindex;
18352 +               if (iinfo->ii_bbot < bindex)
18353 +                       iinfo->ii_bbot = bindex;
18354 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18355 +               update = 1;
18356 +       }
18357 +       au_update_ibrange(inode, /*do_put_zero*/0);
18358 +       e = au_dy_irefresh(inode);
18359 +       if (unlikely(e && !err))
18360 +               err = e;
18361 +       if (!err)
18362 +               au_refresh_hinode_attr(inode, update && isdir);
18363 +
18364 +out:
18365 +       AuTraceErr(err);
18366 +       return err;
18367 +}
18368 +
18369 +static int set_inode(struct inode *inode, struct dentry *dentry)
18370 +{
18371 +       int err;
18372 +       unsigned int flags;
18373 +       umode_t mode;
18374 +       aufs_bindex_t bindex, btop, btail;
18375 +       unsigned char isdir;
18376 +       struct dentry *h_dentry;
18377 +       struct inode *h_inode;
18378 +       struct au_iinfo *iinfo;
18379 +       const struct inode_operations *iop;
18380 +
18381 +       IiMustWriteLock(inode);
18382 +
18383 +       err = 0;
18384 +       isdir = 0;
18385 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18386 +       btop = au_dbtop(dentry);
18387 +       h_dentry = au_h_dptr(dentry, btop);
18388 +       h_inode = d_inode(h_dentry);
18389 +       mode = h_inode->i_mode;
18390 +       switch (mode & S_IFMT) {
18391 +       case S_IFREG:
18392 +               btail = au_dbtail(dentry);
18393 +               inode->i_op = iop + AuIop_OTHER;
18394 +               inode->i_fop = &aufs_file_fop;
18395 +               err = au_dy_iaop(inode, btop, h_inode);
18396 +               if (unlikely(err))
18397 +                       goto out;
18398 +               break;
18399 +       case S_IFDIR:
18400 +               isdir = 1;
18401 +               btail = au_dbtaildir(dentry);
18402 +               inode->i_op = iop + AuIop_DIR;
18403 +               inode->i_fop = &aufs_dir_fop;
18404 +               break;
18405 +       case S_IFLNK:
18406 +               btail = au_dbtail(dentry);
18407 +               inode->i_op = iop + AuIop_SYMLINK;
18408 +               break;
18409 +       case S_IFBLK:
18410 +       case S_IFCHR:
18411 +       case S_IFIFO:
18412 +       case S_IFSOCK:
18413 +               btail = au_dbtail(dentry);
18414 +               inode->i_op = iop + AuIop_OTHER;
18415 +               init_special_inode(inode, mode, h_inode->i_rdev);
18416 +               break;
18417 +       default:
18418 +               AuIOErr("Unknown file type 0%o\n", mode);
18419 +               err = -EIO;
18420 +               goto out;
18421 +       }
18422 +
18423 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18424 +       flags = au_hi_flags(inode, isdir);
18425 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18426 +           && au_ftest_hi(flags, HNOTIFY)
18427 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18428 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18429 +               au_fclr_hi(flags, HNOTIFY);
18430 +       iinfo = au_ii(inode);
18431 +       iinfo->ii_btop = btop;
18432 +       iinfo->ii_bbot = btail;
18433 +       for (bindex = btop; bindex <= btail; bindex++) {
18434 +               h_dentry = au_h_dptr(dentry, bindex);
18435 +               if (h_dentry)
18436 +                       au_set_h_iptr(inode, bindex,
18437 +                                     au_igrab(d_inode(h_dentry)), flags);
18438 +       }
18439 +       au_cpup_attr_all(inode, /*force*/1);
18440 +       /*
18441 +        * to force calling aufs_get_acl() every time,
18442 +        * do not call cache_no_acl() for aufs inode.
18443 +        */
18444 +
18445 +out:
18446 +       return err;
18447 +}
18448 +
18449 +/*
18450 + * successful returns with iinfo write_locked
18451 + * minus: errno
18452 + * zero: success, matched
18453 + * plus: no error, but unmatched
18454 + */
18455 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18456 +{
18457 +       int err;
18458 +       unsigned int gen, igflags;
18459 +       aufs_bindex_t bindex, bbot;
18460 +       struct inode *h_inode, *h_dinode;
18461 +       struct dentry *h_dentry;
18462 +
18463 +       /*
18464 +        * before this function, if aufs got any iinfo lock, it must be only
18465 +        * one, the parent dir.
18466 +        * it can happen by UDBA and the obsoleted inode number.
18467 +        */
18468 +       err = -EIO;
18469 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18470 +               goto out;
18471 +
18472 +       err = 1;
18473 +       ii_write_lock_new_child(inode);
18474 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18475 +       h_dinode = d_inode(h_dentry);
18476 +       bbot = au_ibbot(inode);
18477 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18478 +               h_inode = au_h_iptr(inode, bindex);
18479 +               if (!h_inode || h_inode != h_dinode)
18480 +                       continue;
18481 +
18482 +               err = 0;
18483 +               gen = au_iigen(inode, &igflags);
18484 +               if (gen == au_digen(dentry)
18485 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18486 +                       break;
18487 +
18488 +               /* fully refresh inode using dentry */
18489 +               err = au_refresh_hinode(inode, dentry);
18490 +               if (!err)
18491 +                       au_update_iigen(inode, /*half*/0);
18492 +               break;
18493 +       }
18494 +
18495 +       if (unlikely(err))
18496 +               ii_write_unlock(inode);
18497 +out:
18498 +       return err;
18499 +}
18500 +
18501 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18502 +          unsigned int d_type, ino_t *ino)
18503 +{
18504 +       int err, idx;
18505 +       const int isnondir = d_type != DT_DIR;
18506 +
18507 +       /* prevent hardlinked inode number from race condition */
18508 +       if (isnondir) {
18509 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18510 +               if (unlikely(err))
18511 +                       goto out;
18512 +       }
18513 +
18514 +       err = au_xino_read(sb, bindex, h_ino, ino);
18515 +       if (unlikely(err))
18516 +               goto out_xinondir;
18517 +
18518 +       if (!*ino) {
18519 +               err = -EIO;
18520 +               *ino = au_xino_new_ino(sb);
18521 +               if (unlikely(!*ino))
18522 +                       goto out_xinondir;
18523 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18524 +               if (unlikely(err))
18525 +                       goto out_xinondir;
18526 +       }
18527 +
18528 +out_xinondir:
18529 +       if (isnondir && idx >= 0)
18530 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18531 +out:
18532 +       return err;
18533 +}
18534 +
18535 +/* successful returns with iinfo write_locked */
18536 +/* todo: return with unlocked? */
18537 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18538 +{
18539 +       struct inode *inode, *h_inode;
18540 +       struct dentry *h_dentry;
18541 +       struct super_block *sb;
18542 +       ino_t h_ino, ino;
18543 +       int err, idx, hlinked;
18544 +       aufs_bindex_t btop;
18545 +
18546 +       sb = dentry->d_sb;
18547 +       btop = au_dbtop(dentry);
18548 +       h_dentry = au_h_dptr(dentry, btop);
18549 +       h_inode = d_inode(h_dentry);
18550 +       h_ino = h_inode->i_ino;
18551 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18552 +
18553 +new_ino:
18554 +       /*
18555 +        * stop 'race'-ing between hardlinks under different
18556 +        * parents.
18557 +        */
18558 +       if (hlinked) {
18559 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18560 +               inode = ERR_PTR(err);
18561 +               if (unlikely(err))
18562 +                       goto out;
18563 +       }
18564 +
18565 +       err = au_xino_read(sb, btop, h_ino, &ino);
18566 +       inode = ERR_PTR(err);
18567 +       if (unlikely(err))
18568 +               goto out_xinondir;
18569 +
18570 +       if (!ino) {
18571 +               ino = au_xino_new_ino(sb);
18572 +               if (unlikely(!ino)) {
18573 +                       inode = ERR_PTR(-EIO);
18574 +                       goto out_xinondir;
18575 +               }
18576 +       }
18577 +
18578 +       AuDbg("i%lu\n", (unsigned long)ino);
18579 +       inode = au_iget_locked(sb, ino);
18580 +       err = PTR_ERR(inode);
18581 +       if (IS_ERR(inode))
18582 +               goto out_xinondir;
18583 +
18584 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18585 +       if (inode->i_state & I_NEW) {
18586 +               ii_write_lock_new_child(inode);
18587 +               err = set_inode(inode, dentry);
18588 +               if (!err) {
18589 +                       unlock_new_inode(inode);
18590 +                       goto out_xinondir; /* success */
18591 +               }
18592 +
18593 +               /*
18594 +                * iget_failed() calls iput(), but we need to call
18595 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18596 +                * i_count.
18597 +                */
18598 +               atomic_inc(&inode->i_count);
18599 +               iget_failed(inode);
18600 +               ii_write_unlock(inode);
18601 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18602 +               /* ignore this error */
18603 +               goto out_iput;
18604 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18605 +               /*
18606 +                * horrible race condition between lookup, readdir and copyup
18607 +                * (or something).
18608 +                */
18609 +               if (hlinked && idx >= 0)
18610 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18611 +               err = reval_inode(inode, dentry);
18612 +               if (unlikely(err < 0)) {
18613 +                       hlinked = 0;
18614 +                       goto out_iput;
18615 +               }
18616 +               if (!err)
18617 +                       goto out; /* success */
18618 +               else if (hlinked && idx >= 0) {
18619 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18620 +                       if (unlikely(err)) {
18621 +                               iput(inode);
18622 +                               inode = ERR_PTR(err);
18623 +                               goto out;
18624 +                       }
18625 +               }
18626 +       }
18627 +
18628 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18629 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18630 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18631 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18632 +                       (unsigned long)h_ino, (unsigned long)ino);
18633 +       ino = 0;
18634 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18635 +       if (!err) {
18636 +               iput(inode);
18637 +               if (hlinked && idx >= 0)
18638 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18639 +               goto new_ino;
18640 +       }
18641 +
18642 +out_iput:
18643 +       iput(inode);
18644 +       inode = ERR_PTR(err);
18645 +out_xinondir:
18646 +       if (hlinked && idx >= 0)
18647 +               au_xinondir_leave(sb, btop, h_ino, idx);
18648 +out:
18649 +       return inode;
18650 +}
18651 +
18652 +/* ---------------------------------------------------------------------- */
18653 +
18654 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18655 +              struct inode *inode)
18656 +{
18657 +       int err;
18658 +       struct inode *hi;
18659 +
18660 +       err = au_br_rdonly(au_sbr(sb, bindex));
18661 +
18662 +       /* pseudo-link after flushed may happen out of bounds */
18663 +       if (!err
18664 +           && inode
18665 +           && au_ibtop(inode) <= bindex
18666 +           && bindex <= au_ibbot(inode)) {
18667 +               /*
18668 +                * permission check is unnecessary since vfsub routine
18669 +                * will be called later
18670 +                */
18671 +               hi = au_h_iptr(inode, bindex);
18672 +               if (hi)
18673 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18674 +       }
18675 +
18676 +       return err;
18677 +}
18678 +
18679 +int au_test_h_perm(struct inode *h_inode, int mask)
18680 +{
18681 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18682 +               return 0;
18683 +       return inode_permission(h_inode, mask);
18684 +}
18685 +
18686 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18687 +{
18688 +       if (au_test_nfs(h_inode->i_sb)
18689 +           && (mask & MAY_WRITE)
18690 +           && S_ISDIR(h_inode->i_mode))
18691 +               mask |= MAY_READ; /* force permission check */
18692 +       return au_test_h_perm(h_inode, mask);
18693 +}
18694 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18695 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18696 +++ linux/fs/aufs/inode.h       2020-01-27 10:57:18.175538316 +0100
18697 @@ -0,0 +1,698 @@
18698 +/* SPDX-License-Identifier: GPL-2.0 */
18699 +/*
18700 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18701 + *
18702 + * This program, aufs is free software; you can redistribute it and/or modify
18703 + * it under the terms of the GNU General Public License as published by
18704 + * the Free Software Foundation; either version 2 of the License, or
18705 + * (at your option) any later version.
18706 + *
18707 + * This program is distributed in the hope that it will be useful,
18708 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18709 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18710 + * GNU General Public License for more details.
18711 + *
18712 + * You should have received a copy of the GNU General Public License
18713 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18714 + */
18715 +
18716 +/*
18717 + * inode operations
18718 + */
18719 +
18720 +#ifndef __AUFS_INODE_H__
18721 +#define __AUFS_INODE_H__
18722 +
18723 +#ifdef __KERNEL__
18724 +
18725 +#include <linux/fsnotify.h>
18726 +#include "rwsem.h"
18727 +
18728 +struct vfsmount;
18729 +
18730 +struct au_hnotify {
18731 +#ifdef CONFIG_AUFS_HNOTIFY
18732 +#ifdef CONFIG_AUFS_HFSNOTIFY
18733 +       /* never use fsnotify_add_vfsmount_mark() */
18734 +       struct fsnotify_mark            hn_mark;
18735 +#endif
18736 +       struct inode            *hn_aufs_inode; /* no get/put */
18737 +       struct rcu_head         rcu;
18738 +#endif
18739 +} ____cacheline_aligned_in_smp;
18740 +
18741 +struct au_hinode {
18742 +       struct inode            *hi_inode;
18743 +       aufs_bindex_t           hi_id;
18744 +#ifdef CONFIG_AUFS_HNOTIFY
18745 +       struct au_hnotify       *hi_notify;
18746 +#endif
18747 +
18748 +       /* reference to the copied-up whiteout with get/put */
18749 +       struct dentry           *hi_whdentry;
18750 +};
18751 +
18752 +/* ig_flags */
18753 +#define AuIG_HALF_REFRESHED            1
18754 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18755 +#define au_ig_fset(flags, name) \
18756 +       do { (flags) |= AuIG_##name; } while (0)
18757 +#define au_ig_fclr(flags, name) \
18758 +       do { (flags) &= ~AuIG_##name; } while (0)
18759 +
18760 +struct au_iigen {
18761 +       spinlock_t      ig_spin;
18762 +       __u32           ig_generation, ig_flags;
18763 +};
18764 +
18765 +struct au_vdir;
18766 +struct au_iinfo {
18767 +       struct au_iigen         ii_generation;
18768 +       struct super_block      *ii_hsb1;       /* no get/put */
18769 +
18770 +       struct au_rwsem         ii_rwsem;
18771 +       aufs_bindex_t           ii_btop, ii_bbot;
18772 +       __u32                   ii_higen;
18773 +       struct au_hinode        *ii_hinode;
18774 +       struct au_vdir          *ii_vdir;
18775 +};
18776 +
18777 +struct au_icntnr {
18778 +       struct au_iinfo         iinfo;
18779 +       struct inode            vfs_inode;
18780 +       struct hlist_bl_node    plink;
18781 +       struct rcu_head         rcu;
18782 +} ____cacheline_aligned_in_smp;
18783 +
18784 +/* au_pin flags */
18785 +#define AuPin_DI_LOCKED                1
18786 +#define AuPin_MNT_WRITE                (1 << 1)
18787 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18788 +#define au_fset_pin(flags, name) \
18789 +       do { (flags) |= AuPin_##name; } while (0)
18790 +#define au_fclr_pin(flags, name) \
18791 +       do { (flags) &= ~AuPin_##name; } while (0)
18792 +
18793 +struct au_pin {
18794 +       /* input */
18795 +       struct dentry *dentry;
18796 +       unsigned int udba;
18797 +       unsigned char lsc_di, lsc_hi, flags;
18798 +       aufs_bindex_t bindex;
18799 +
18800 +       /* output */
18801 +       struct dentry *parent;
18802 +       struct au_hinode *hdir;
18803 +       struct vfsmount *h_mnt;
18804 +
18805 +       /* temporary unlock/relock for copyup */
18806 +       struct dentry *h_dentry, *h_parent;
18807 +       struct au_branch *br;
18808 +       struct task_struct *task;
18809 +};
18810 +
18811 +void au_pin_hdir_unlock(struct au_pin *p);
18812 +int au_pin_hdir_lock(struct au_pin *p);
18813 +int au_pin_hdir_relock(struct au_pin *p);
18814 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18815 +void au_pin_hdir_release(struct au_pin *p);
18816 +
18817 +/* ---------------------------------------------------------------------- */
18818 +
18819 +static inline struct au_iinfo *au_ii(struct inode *inode)
18820 +{
18821 +       BUG_ON(is_bad_inode(inode));
18822 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18823 +}
18824 +
18825 +/* ---------------------------------------------------------------------- */
18826 +
18827 +/* inode.c */
18828 +struct inode *au_igrab(struct inode *inode);
18829 +void au_refresh_iop(struct inode *inode, int force_getattr);
18830 +int au_refresh_hinode_self(struct inode *inode);
18831 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18832 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18833 +          unsigned int d_type, ino_t *ino);
18834 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18835 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18836 +              struct inode *inode);
18837 +int au_test_h_perm(struct inode *h_inode, int mask);
18838 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18839 +
18840 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18841 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18842 +{
18843 +#ifdef CONFIG_AUFS_SHWH
18844 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18845 +#else
18846 +       return 0;
18847 +#endif
18848 +}
18849 +
18850 +/* i_op.c */
18851 +enum {
18852 +       AuIop_SYMLINK,
18853 +       AuIop_DIR,
18854 +       AuIop_OTHER,
18855 +       AuIop_Last
18856 +};
18857 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18858 +       aufs_iop_nogetattr[AuIop_Last];
18859 +
18860 +/* au_wr_dir flags */
18861 +#define AuWrDir_ADD_ENTRY      1
18862 +#define AuWrDir_ISDIR          (1 << 1)
18863 +#define AuWrDir_TMPFILE                (1 << 2)
18864 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18865 +#define au_fset_wrdir(flags, name) \
18866 +       do { (flags) |= AuWrDir_##name; } while (0)
18867 +#define au_fclr_wrdir(flags, name) \
18868 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18869 +
18870 +struct au_wr_dir_args {
18871 +       aufs_bindex_t force_btgt;
18872 +       unsigned char flags;
18873 +};
18874 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18875 +             struct au_wr_dir_args *args);
18876 +
18877 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18878 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18879 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18880 +                unsigned int udba, unsigned char flags);
18881 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18882 +          unsigned int udba, unsigned char flags) __must_check;
18883 +int au_do_pin(struct au_pin *pin) __must_check;
18884 +void au_unpin(struct au_pin *pin);
18885 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18886 +
18887 +#define AuIcpup_DID_CPUP       1
18888 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18889 +#define au_fset_icpup(flags, name) \
18890 +       do { (flags) |= AuIcpup_##name; } while (0)
18891 +#define au_fclr_icpup(flags, name) \
18892 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18893 +
18894 +struct au_icpup_args {
18895 +       unsigned char flags;
18896 +       unsigned char pin_flags;
18897 +       aufs_bindex_t btgt;
18898 +       unsigned int udba;
18899 +       struct au_pin pin;
18900 +       struct path h_path;
18901 +       struct inode *h_inode;
18902 +};
18903 +
18904 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18905 +                    struct au_icpup_args *a);
18906 +
18907 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
18908 +                     int locked);
18909 +
18910 +/* i_op_add.c */
18911 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18912 +              struct dentry *h_parent, int isdir);
18913 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18914 +              dev_t dev);
18915 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18916 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18917 +               bool want_excl);
18918 +struct vfsub_aopen_args;
18919 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18920 +                      struct vfsub_aopen_args *args);
18921 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18922 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18923 +             struct dentry *dentry);
18924 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18925 +
18926 +/* i_op_del.c */
18927 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18928 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18929 +              struct dentry *h_parent, int isdir);
18930 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18931 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18932 +
18933 +/* i_op_ren.c */
18934 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18935 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18936 +               struct inode *dir, struct dentry *dentry,
18937 +               unsigned int flags);
18938 +
18939 +/* iinfo.c */
18940 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18941 +void au_hiput(struct au_hinode *hinode);
18942 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18943 +                 struct dentry *h_wh);
18944 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18945 +
18946 +/* hinode flags */
18947 +#define AuHi_XINO      1
18948 +#define AuHi_HNOTIFY   (1 << 1)
18949 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18950 +#define au_fset_hi(flags, name) \
18951 +       do { (flags) |= AuHi_##name; } while (0)
18952 +#define au_fclr_hi(flags, name) \
18953 +       do { (flags) &= ~AuHi_##name; } while (0)
18954 +
18955 +#ifndef CONFIG_AUFS_HNOTIFY
18956 +#undef AuHi_HNOTIFY
18957 +#define AuHi_HNOTIFY   0
18958 +#endif
18959 +
18960 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18961 +                  struct inode *h_inode, unsigned int flags);
18962 +
18963 +void au_update_iigen(struct inode *inode, int half);
18964 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18965 +
18966 +void au_icntnr_init_once(void *_c);
18967 +void au_hinode_init(struct au_hinode *hinode);
18968 +int au_iinfo_init(struct inode *inode);
18969 +void au_iinfo_fin(struct inode *inode);
18970 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18971 +
18972 +#ifdef CONFIG_PROC_FS
18973 +/* plink.c */
18974 +int au_plink_maint(struct super_block *sb, int flags);
18975 +struct au_sbinfo;
18976 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18977 +int au_plink_maint_enter(struct super_block *sb);
18978 +#ifdef CONFIG_AUFS_DEBUG
18979 +void au_plink_list(struct super_block *sb);
18980 +#else
18981 +AuStubVoid(au_plink_list, struct super_block *sb)
18982 +#endif
18983 +int au_plink_test(struct inode *inode);
18984 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18985 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18986 +                    struct dentry *h_dentry);
18987 +void au_plink_put(struct super_block *sb, int verbose);
18988 +void au_plink_clean(struct super_block *sb, int verbose);
18989 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18990 +#else
18991 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18992 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18993 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18994 +AuStubVoid(au_plink_list, struct super_block *sb);
18995 +AuStubInt0(au_plink_test, struct inode *inode);
18996 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18997 +       struct inode *inode, aufs_bindex_t bindex);
18998 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18999 +          struct dentry *h_dentry);
19000 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
19001 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
19002 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
19003 +#endif /* CONFIG_PROC_FS */
19004 +
19005 +#ifdef CONFIG_AUFS_XATTR
19006 +/* xattr.c */
19007 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
19008 +                 unsigned int verbose);
19009 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
19010 +void au_xattr_init(struct super_block *sb);
19011 +#else
19012 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
19013 +          int ignore_flags, unsigned int verbose);
19014 +AuStubVoid(au_xattr_init, struct super_block *sb);
19015 +#endif
19016 +
19017 +#ifdef CONFIG_FS_POSIX_ACL
19018 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
19019 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
19020 +#endif
19021 +
19022 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19023 +enum {
19024 +       AU_XATTR_SET,
19025 +       AU_ACL_SET
19026 +};
19027 +
19028 +struct au_sxattr {
19029 +       int type;
19030 +       union {
19031 +               struct {
19032 +                       const char      *name;
19033 +                       const void      *value;
19034 +                       size_t          size;
19035 +                       int             flags;
19036 +               } set;
19037 +               struct {
19038 +                       struct posix_acl *acl;
19039 +                       int             type;
19040 +               } acl_set;
19041 +       } u;
19042 +};
19043 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19044 +                 struct au_sxattr *arg);
19045 +#endif
19046 +
19047 +/* ---------------------------------------------------------------------- */
19048 +
19049 +/* lock subclass for iinfo */
19050 +enum {
19051 +       AuLsc_II_CHILD,         /* child first */
19052 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
19053 +       AuLsc_II_CHILD3,        /* copyup dirs */
19054 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
19055 +       AuLsc_II_PARENT2,
19056 +       AuLsc_II_PARENT3,       /* copyup dirs */
19057 +       AuLsc_II_NEW_CHILD
19058 +};
19059 +
19060 +/*
19061 + * ii_read_lock_child, ii_write_lock_child,
19062 + * ii_read_lock_child2, ii_write_lock_child2,
19063 + * ii_read_lock_child3, ii_write_lock_child3,
19064 + * ii_read_lock_parent, ii_write_lock_parent,
19065 + * ii_read_lock_parent2, ii_write_lock_parent2,
19066 + * ii_read_lock_parent3, ii_write_lock_parent3,
19067 + * ii_read_lock_new_child, ii_write_lock_new_child,
19068 + */
19069 +#define AuReadLockFunc(name, lsc) \
19070 +static inline void ii_read_lock_##name(struct inode *i) \
19071 +{ \
19072 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19073 +}
19074 +
19075 +#define AuWriteLockFunc(name, lsc) \
19076 +static inline void ii_write_lock_##name(struct inode *i) \
19077 +{ \
19078 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
19079 +}
19080 +
19081 +#define AuRWLockFuncs(name, lsc) \
19082 +       AuReadLockFunc(name, lsc) \
19083 +       AuWriteLockFunc(name, lsc)
19084 +
19085 +AuRWLockFuncs(child, CHILD);
19086 +AuRWLockFuncs(child2, CHILD2);
19087 +AuRWLockFuncs(child3, CHILD3);
19088 +AuRWLockFuncs(parent, PARENT);
19089 +AuRWLockFuncs(parent2, PARENT2);
19090 +AuRWLockFuncs(parent3, PARENT3);
19091 +AuRWLockFuncs(new_child, NEW_CHILD);
19092 +
19093 +#undef AuReadLockFunc
19094 +#undef AuWriteLockFunc
19095 +#undef AuRWLockFuncs
19096 +
19097 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19098 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19099 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19100 +
19101 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19102 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19103 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19104 +
19105 +/* ---------------------------------------------------------------------- */
19106 +
19107 +static inline void au_icntnr_init(struct au_icntnr *c)
19108 +{
19109 +#ifdef CONFIG_AUFS_DEBUG
19110 +       c->vfs_inode.i_mode = 0;
19111 +#endif
19112 +}
19113 +
19114 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19115 +{
19116 +       unsigned int gen;
19117 +       struct au_iinfo *iinfo;
19118 +       struct au_iigen *iigen;
19119 +
19120 +       iinfo = au_ii(inode);
19121 +       iigen = &iinfo->ii_generation;
19122 +       spin_lock(&iigen->ig_spin);
19123 +       if (igflags)
19124 +               *igflags = iigen->ig_flags;
19125 +       gen = iigen->ig_generation;
19126 +       spin_unlock(&iigen->ig_spin);
19127 +
19128 +       return gen;
19129 +}
19130 +
19131 +/* tiny test for inode number */
19132 +/* tmpfs generation is too rough */
19133 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19134 +{
19135 +       struct au_iinfo *iinfo;
19136 +
19137 +       iinfo = au_ii(inode);
19138 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19139 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19140 +                && iinfo->ii_higen == h_inode->i_generation);
19141 +}
19142 +
19143 +static inline void au_iigen_dec(struct inode *inode)
19144 +{
19145 +       struct au_iinfo *iinfo;
19146 +       struct au_iigen *iigen;
19147 +
19148 +       iinfo = au_ii(inode);
19149 +       iigen = &iinfo->ii_generation;
19150 +       spin_lock(&iigen->ig_spin);
19151 +       iigen->ig_generation--;
19152 +       spin_unlock(&iigen->ig_spin);
19153 +}
19154 +
19155 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19156 +{
19157 +       int err;
19158 +
19159 +       err = 0;
19160 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19161 +               err = -EIO;
19162 +
19163 +       return err;
19164 +}
19165 +
19166 +/* ---------------------------------------------------------------------- */
19167 +
19168 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19169 +                                         aufs_bindex_t bindex)
19170 +{
19171 +       return iinfo->ii_hinode + bindex;
19172 +}
19173 +
19174 +static inline int au_is_bad_inode(struct inode *inode)
19175 +{
19176 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19177 +}
19178 +
19179 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19180 +                                       aufs_bindex_t bindex)
19181 +{
19182 +       IiMustAnyLock(inode);
19183 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19184 +}
19185 +
19186 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19187 +{
19188 +       IiMustAnyLock(inode);
19189 +       return au_ii(inode)->ii_btop;
19190 +}
19191 +
19192 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19193 +{
19194 +       IiMustAnyLock(inode);
19195 +       return au_ii(inode)->ii_bbot;
19196 +}
19197 +
19198 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19199 +{
19200 +       IiMustAnyLock(inode);
19201 +       return au_ii(inode)->ii_vdir;
19202 +}
19203 +
19204 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19205 +{
19206 +       IiMustAnyLock(inode);
19207 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19208 +}
19209 +
19210 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19211 +{
19212 +       IiMustWriteLock(inode);
19213 +       au_ii(inode)->ii_btop = bindex;
19214 +}
19215 +
19216 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19217 +{
19218 +       IiMustWriteLock(inode);
19219 +       au_ii(inode)->ii_bbot = bindex;
19220 +}
19221 +
19222 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19223 +{
19224 +       IiMustWriteLock(inode);
19225 +       au_ii(inode)->ii_vdir = vdir;
19226 +}
19227 +
19228 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19229 +{
19230 +       IiMustAnyLock(inode);
19231 +       return au_hinode(au_ii(inode), bindex);
19232 +}
19233 +
19234 +/* ---------------------------------------------------------------------- */
19235 +
19236 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19237 +{
19238 +       if (pin)
19239 +               return pin->parent;
19240 +       return NULL;
19241 +}
19242 +
19243 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19244 +{
19245 +       if (pin && pin->hdir)
19246 +               return pin->hdir->hi_inode;
19247 +       return NULL;
19248 +}
19249 +
19250 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19251 +{
19252 +       if (pin)
19253 +               return pin->hdir;
19254 +       return NULL;
19255 +}
19256 +
19257 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19258 +{
19259 +       if (pin)
19260 +               pin->dentry = dentry;
19261 +}
19262 +
19263 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19264 +                                          unsigned char lflag)
19265 +{
19266 +       if (pin) {
19267 +               if (lflag)
19268 +                       au_fset_pin(pin->flags, DI_LOCKED);
19269 +               else
19270 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19271 +       }
19272 +}
19273 +
19274 +#if 0 /* reserved */
19275 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19276 +{
19277 +       if (pin) {
19278 +               dput(pin->parent);
19279 +               pin->parent = dget(parent);
19280 +       }
19281 +}
19282 +#endif
19283 +
19284 +/* ---------------------------------------------------------------------- */
19285 +
19286 +struct au_branch;
19287 +#ifdef CONFIG_AUFS_HNOTIFY
19288 +struct au_hnotify_op {
19289 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19290 +       int (*alloc)(struct au_hinode *hinode);
19291 +
19292 +       /*
19293 +        * if it returns true, the the caller should free hinode->hi_notify,
19294 +        * otherwise ->free() frees it.
19295 +        */
19296 +       int (*free)(struct au_hinode *hinode,
19297 +                   struct au_hnotify *hn) __must_check;
19298 +
19299 +       void (*fin)(void);
19300 +       int (*init)(void);
19301 +
19302 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19303 +       void (*fin_br)(struct au_branch *br);
19304 +       int (*init_br)(struct au_branch *br, int perm);
19305 +};
19306 +
19307 +/* hnotify.c */
19308 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19309 +void au_hn_free(struct au_hinode *hinode);
19310 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19311 +void au_hn_reset(struct inode *inode, unsigned int flags);
19312 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19313 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19314 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19315 +int au_hnotify_init_br(struct au_branch *br, int perm);
19316 +void au_hnotify_fin_br(struct au_branch *br);
19317 +int __init au_hnotify_init(void);
19318 +void au_hnotify_fin(void);
19319 +
19320 +/* hfsnotify.c */
19321 +extern const struct au_hnotify_op au_hnotify_op;
19322 +
19323 +static inline
19324 +void au_hn_init(struct au_hinode *hinode)
19325 +{
19326 +       hinode->hi_notify = NULL;
19327 +}
19328 +
19329 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19330 +{
19331 +       return hinode->hi_notify;
19332 +}
19333 +
19334 +#else
19335 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19336 +       struct au_hinode *hinode __maybe_unused,
19337 +       struct inode *inode __maybe_unused)
19338 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19339 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19340 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19341 +          int do_set __maybe_unused)
19342 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19343 +          unsigned int flags __maybe_unused)
19344 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19345 +          struct au_branch *br __maybe_unused,
19346 +          int perm __maybe_unused)
19347 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19348 +          int perm __maybe_unused)
19349 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19350 +AuStubInt0(__init au_hnotify_init, void)
19351 +AuStubVoid(au_hnotify_fin, void)
19352 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19353 +#endif /* CONFIG_AUFS_HNOTIFY */
19354 +
19355 +static inline void au_hn_suspend(struct au_hinode *hdir)
19356 +{
19357 +       au_hn_ctl(hdir, /*do_set*/0);
19358 +}
19359 +
19360 +static inline void au_hn_resume(struct au_hinode *hdir)
19361 +{
19362 +       au_hn_ctl(hdir, /*do_set*/1);
19363 +}
19364 +
19365 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19366 +{
19367 +       inode_lock(hdir->hi_inode);
19368 +       au_hn_suspend(hdir);
19369 +}
19370 +
19371 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19372 +                                         unsigned int sc __maybe_unused)
19373 +{
19374 +       inode_lock_nested(hdir->hi_inode, sc);
19375 +       au_hn_suspend(hdir);
19376 +}
19377 +
19378 +#if 0 /* unused */
19379 +#include "vfsub.h"
19380 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19381 +                                                 unsigned int sc)
19382 +{
19383 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19384 +       au_hn_suspend(hdir);
19385 +}
19386 +#endif
19387 +
19388 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19389 +{
19390 +       au_hn_resume(hdir);
19391 +       inode_unlock(hdir->hi_inode);
19392 +}
19393 +
19394 +#endif /* __KERNEL__ */
19395 +#endif /* __AUFS_INODE_H__ */
19396 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19397 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19398 +++ linux/fs/aufs/ioctl.c       2020-01-27 10:57:18.175538316 +0100
19399 @@ -0,0 +1,220 @@
19400 +// SPDX-License-Identifier: GPL-2.0
19401 +/*
19402 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19403 + *
19404 + * This program, aufs is free software; you can redistribute it and/or modify
19405 + * it under the terms of the GNU General Public License as published by
19406 + * the Free Software Foundation; either version 2 of the License, or
19407 + * (at your option) any later version.
19408 + *
19409 + * This program is distributed in the hope that it will be useful,
19410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19412 + * GNU General Public License for more details.
19413 + *
19414 + * You should have received a copy of the GNU General Public License
19415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19416 + */
19417 +
19418 +/*
19419 + * ioctl
19420 + * plink-management and readdir in userspace.
19421 + * assist the pathconf(3) wrapper library.
19422 + * move-down
19423 + * File-based Hierarchical Storage Management.
19424 + */
19425 +
19426 +#include <linux/compat.h>
19427 +#include <linux/file.h>
19428 +#include "aufs.h"
19429 +
19430 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19431 +{
19432 +       int err, fd;
19433 +       aufs_bindex_t wbi, bindex, bbot;
19434 +       struct file *h_file;
19435 +       struct super_block *sb;
19436 +       struct dentry *root;
19437 +       struct au_branch *br;
19438 +       struct aufs_wbr_fd wbrfd = {
19439 +               .oflags = au_dir_roflags,
19440 +               .brid   = -1
19441 +       };
19442 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19443 +               | O_NOATIME | O_CLOEXEC;
19444 +
19445 +       AuDebugOn(wbrfd.oflags & ~valid);
19446 +
19447 +       if (arg) {
19448 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19449 +               if (unlikely(err)) {
19450 +                       err = -EFAULT;
19451 +                       goto out;
19452 +               }
19453 +
19454 +               err = -EINVAL;
19455 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19456 +               wbrfd.oflags |= au_dir_roflags;
19457 +               AuDbg("0%o\n", wbrfd.oflags);
19458 +               if (unlikely(wbrfd.oflags & ~valid))
19459 +                       goto out;
19460 +       }
19461 +
19462 +       fd = get_unused_fd_flags(0);
19463 +       err = fd;
19464 +       if (unlikely(fd < 0))
19465 +               goto out;
19466 +
19467 +       h_file = ERR_PTR(-EINVAL);
19468 +       wbi = 0;
19469 +       br = NULL;
19470 +       sb = path->dentry->d_sb;
19471 +       root = sb->s_root;
19472 +       aufs_read_lock(root, AuLock_IR);
19473 +       bbot = au_sbbot(sb);
19474 +       if (wbrfd.brid >= 0) {
19475 +               wbi = au_br_index(sb, wbrfd.brid);
19476 +               if (unlikely(wbi < 0 || wbi > bbot))
19477 +                       goto out_unlock;
19478 +       }
19479 +
19480 +       h_file = ERR_PTR(-ENOENT);
19481 +       br = au_sbr(sb, wbi);
19482 +       if (!au_br_writable(br->br_perm)) {
19483 +               if (arg)
19484 +                       goto out_unlock;
19485 +
19486 +               bindex = wbi + 1;
19487 +               wbi = -1;
19488 +               for (; bindex <= bbot; bindex++) {
19489 +                       br = au_sbr(sb, bindex);
19490 +                       if (au_br_writable(br->br_perm)) {
19491 +                               wbi = bindex;
19492 +                               br = au_sbr(sb, wbi);
19493 +                               break;
19494 +                       }
19495 +               }
19496 +       }
19497 +       AuDbg("wbi %d\n", wbi);
19498 +       if (wbi >= 0)
19499 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19500 +                                  /*force_wr*/0);
19501 +
19502 +out_unlock:
19503 +       aufs_read_unlock(root, AuLock_IR);
19504 +       err = PTR_ERR(h_file);
19505 +       if (IS_ERR(h_file))
19506 +               goto out_fd;
19507 +
19508 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19509 +       fd_install(fd, h_file);
19510 +       err = fd;
19511 +       goto out; /* success */
19512 +
19513 +out_fd:
19514 +       put_unused_fd(fd);
19515 +out:
19516 +       AuTraceErr(err);
19517 +       return err;
19518 +}
19519 +
19520 +/* ---------------------------------------------------------------------- */
19521 +
19522 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19523 +{
19524 +       long err;
19525 +       struct dentry *dentry;
19526 +
19527 +       switch (cmd) {
19528 +       case AUFS_CTL_RDU:
19529 +       case AUFS_CTL_RDU_INO:
19530 +               err = au_rdu_ioctl(file, cmd, arg);
19531 +               break;
19532 +
19533 +       case AUFS_CTL_WBR_FD:
19534 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19535 +               break;
19536 +
19537 +       case AUFS_CTL_IBUSY:
19538 +               err = au_ibusy_ioctl(file, arg);
19539 +               break;
19540 +
19541 +       case AUFS_CTL_BRINFO:
19542 +               err = au_brinfo_ioctl(file, arg);
19543 +               break;
19544 +
19545 +       case AUFS_CTL_FHSM_FD:
19546 +               dentry = file->f_path.dentry;
19547 +               if (IS_ROOT(dentry))
19548 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19549 +               else
19550 +                       err = -ENOTTY;
19551 +               break;
19552 +
19553 +       default:
19554 +               /* do not call the lower */
19555 +               AuDbg("0x%x\n", cmd);
19556 +               err = -ENOTTY;
19557 +       }
19558 +
19559 +       AuTraceErr(err);
19560 +       return err;
19561 +}
19562 +
19563 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19564 +{
19565 +       long err;
19566 +
19567 +       switch (cmd) {
19568 +       case AUFS_CTL_MVDOWN:
19569 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19570 +               break;
19571 +
19572 +       case AUFS_CTL_WBR_FD:
19573 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19574 +               break;
19575 +
19576 +       default:
19577 +               /* do not call the lower */
19578 +               AuDbg("0x%x\n", cmd);
19579 +               err = -ENOTTY;
19580 +       }
19581 +
19582 +       AuTraceErr(err);
19583 +       return err;
19584 +}
19585 +
19586 +#ifdef CONFIG_COMPAT
19587 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19588 +                          unsigned long arg)
19589 +{
19590 +       long err;
19591 +
19592 +       switch (cmd) {
19593 +       case AUFS_CTL_RDU:
19594 +       case AUFS_CTL_RDU_INO:
19595 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19596 +               break;
19597 +
19598 +       case AUFS_CTL_IBUSY:
19599 +               err = au_ibusy_compat_ioctl(file, arg);
19600 +               break;
19601 +
19602 +       case AUFS_CTL_BRINFO:
19603 +               err = au_brinfo_compat_ioctl(file, arg);
19604 +               break;
19605 +
19606 +       default:
19607 +               err = aufs_ioctl_dir(file, cmd, arg);
19608 +       }
19609 +
19610 +       AuTraceErr(err);
19611 +       return err;
19612 +}
19613 +
19614 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19615 +                             unsigned long arg)
19616 +{
19617 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19618 +}
19619 +#endif
19620 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19621 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19622 +++ linux/fs/aufs/i_op_add.c    2020-01-27 10:57:18.172204883 +0100
19623 @@ -0,0 +1,936 @@
19624 +// SPDX-License-Identifier: GPL-2.0
19625 +/*
19626 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19627 + *
19628 + * This program, aufs is free software; you can redistribute it and/or modify
19629 + * it under the terms of the GNU General Public License as published by
19630 + * the Free Software Foundation; either version 2 of the License, or
19631 + * (at your option) any later version.
19632 + *
19633 + * This program is distributed in the hope that it will be useful,
19634 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19635 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19636 + * GNU General Public License for more details.
19637 + *
19638 + * You should have received a copy of the GNU General Public License
19639 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19640 + */
19641 +
19642 +/*
19643 + * inode operations (add entry)
19644 + */
19645 +
19646 +#include <linux/iversion.h>
19647 +#include "aufs.h"
19648 +
19649 +/*
19650 + * final procedure of adding a new entry, except link(2).
19651 + * remove whiteout, instantiate, copyup the parent dir's times and size
19652 + * and update version.
19653 + * if it failed, re-create the removed whiteout.
19654 + */
19655 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19656 +                 struct dentry *wh_dentry, struct dentry *dentry)
19657 +{
19658 +       int err, rerr;
19659 +       aufs_bindex_t bwh;
19660 +       struct path h_path;
19661 +       struct super_block *sb;
19662 +       struct inode *inode, *h_dir;
19663 +       struct dentry *wh;
19664 +
19665 +       bwh = -1;
19666 +       sb = dir->i_sb;
19667 +       if (wh_dentry) {
19668 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19669 +               IMustLock(h_dir);
19670 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19671 +               bwh = au_dbwh(dentry);
19672 +               h_path.dentry = wh_dentry;
19673 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19674 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19675 +                                         dentry);
19676 +               if (unlikely(err))
19677 +                       goto out;
19678 +       }
19679 +
19680 +       inode = au_new_inode(dentry, /*must_new*/1);
19681 +       if (!IS_ERR(inode)) {
19682 +               d_instantiate(dentry, inode);
19683 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19684 +               IMustLock(dir);
19685 +               au_dir_ts(dir, bindex);
19686 +               inode_inc_iversion(dir);
19687 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19688 +               return 0; /* success */
19689 +       }
19690 +
19691 +       err = PTR_ERR(inode);
19692 +       if (!wh_dentry)
19693 +               goto out;
19694 +
19695 +       /* revert */
19696 +       /* dir inode is locked */
19697 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19698 +       rerr = PTR_ERR(wh);
19699 +       if (IS_ERR(wh)) {
19700 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19701 +                       dentry, err, rerr);
19702 +               err = -EIO;
19703 +       } else
19704 +               dput(wh);
19705 +
19706 +out:
19707 +       return err;
19708 +}
19709 +
19710 +static int au_d_may_add(struct dentry *dentry)
19711 +{
19712 +       int err;
19713 +
19714 +       err = 0;
19715 +       if (unlikely(d_unhashed(dentry)))
19716 +               err = -ENOENT;
19717 +       if (unlikely(d_really_is_positive(dentry)))
19718 +               err = -EEXIST;
19719 +       return err;
19720 +}
19721 +
19722 +/*
19723 + * simple tests for the adding inode operations.
19724 + * following the checks in vfs, plus the parent-child relationship.
19725 + */
19726 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19727 +              struct dentry *h_parent, int isdir)
19728 +{
19729 +       int err;
19730 +       umode_t h_mode;
19731 +       struct dentry *h_dentry;
19732 +       struct inode *h_inode;
19733 +
19734 +       err = -ENAMETOOLONG;
19735 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19736 +               goto out;
19737 +
19738 +       h_dentry = au_h_dptr(dentry, bindex);
19739 +       if (d_really_is_negative(dentry)) {
19740 +               err = -EEXIST;
19741 +               if (unlikely(d_is_positive(h_dentry)))
19742 +                       goto out;
19743 +       } else {
19744 +               /* rename(2) case */
19745 +               err = -EIO;
19746 +               if (unlikely(d_is_negative(h_dentry)))
19747 +                       goto out;
19748 +               h_inode = d_inode(h_dentry);
19749 +               if (unlikely(!h_inode->i_nlink))
19750 +                       goto out;
19751 +
19752 +               h_mode = h_inode->i_mode;
19753 +               if (!isdir) {
19754 +                       err = -EISDIR;
19755 +                       if (unlikely(S_ISDIR(h_mode)))
19756 +                               goto out;
19757 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19758 +                       err = -ENOTDIR;
19759 +                       goto out;
19760 +               }
19761 +       }
19762 +
19763 +       err = 0;
19764 +       /* expected parent dir is locked */
19765 +       if (unlikely(h_parent != h_dentry->d_parent))
19766 +               err = -EIO;
19767 +
19768 +out:
19769 +       AuTraceErr(err);
19770 +       return err;
19771 +}
19772 +
19773 +/*
19774 + * initial procedure of adding a new entry.
19775 + * prepare writable branch and the parent dir, lock it,
19776 + * and lookup whiteout for the new entry.
19777 + */
19778 +static struct dentry*
19779 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19780 +                 struct dentry *src_dentry, struct au_pin *pin,
19781 +                 struct au_wr_dir_args *wr_dir_args)
19782 +{
19783 +       struct dentry *wh_dentry, *h_parent;
19784 +       struct super_block *sb;
19785 +       struct au_branch *br;
19786 +       int err;
19787 +       unsigned int udba;
19788 +       aufs_bindex_t bcpup;
19789 +
19790 +       AuDbg("%pd\n", dentry);
19791 +
19792 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19793 +       bcpup = err;
19794 +       wh_dentry = ERR_PTR(err);
19795 +       if (unlikely(err < 0))
19796 +               goto out;
19797 +
19798 +       sb = dentry->d_sb;
19799 +       udba = au_opt_udba(sb);
19800 +       err = au_pin(pin, dentry, bcpup, udba,
19801 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19802 +       wh_dentry = ERR_PTR(err);
19803 +       if (unlikely(err))
19804 +               goto out;
19805 +
19806 +       h_parent = au_pinned_h_parent(pin);
19807 +       if (udba != AuOpt_UDBA_NONE
19808 +           && au_dbtop(dentry) == bcpup)
19809 +               err = au_may_add(dentry, bcpup, h_parent,
19810 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19811 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19812 +               err = -ENAMETOOLONG;
19813 +       wh_dentry = ERR_PTR(err);
19814 +       if (unlikely(err))
19815 +               goto out_unpin;
19816 +
19817 +       br = au_sbr(sb, bcpup);
19818 +       if (dt) {
19819 +               struct path tmp = {
19820 +                       .dentry = h_parent,
19821 +                       .mnt    = au_br_mnt(br)
19822 +               };
19823 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19824 +       }
19825 +
19826 +       wh_dentry = NULL;
19827 +       if (bcpup != au_dbwh(dentry))
19828 +               goto out; /* success */
19829 +
19830 +       /*
19831 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19832 +        * would not be able to removed in the future. So we don't allow such
19833 +        * name here and we don't handle ENAMETOOLONG differently here.
19834 +        */
19835 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19836 +
19837 +out_unpin:
19838 +       if (IS_ERR(wh_dentry))
19839 +               au_unpin(pin);
19840 +out:
19841 +       return wh_dentry;
19842 +}
19843 +
19844 +/* ---------------------------------------------------------------------- */
19845 +
19846 +enum { Mknod, Symlink, Creat };
19847 +struct simple_arg {
19848 +       int type;
19849 +       union {
19850 +               struct {
19851 +                       umode_t                 mode;
19852 +                       bool                    want_excl;
19853 +                       bool                    try_aopen;
19854 +                       struct vfsub_aopen_args *aopen;
19855 +               } c;
19856 +               struct {
19857 +                       const char *symname;
19858 +               } s;
19859 +               struct {
19860 +                       umode_t mode;
19861 +                       dev_t dev;
19862 +               } m;
19863 +       } u;
19864 +};
19865 +
19866 +static int add_simple(struct inode *dir, struct dentry *dentry,
19867 +                     struct simple_arg *arg)
19868 +{
19869 +       int err, rerr;
19870 +       aufs_bindex_t btop;
19871 +       unsigned char created;
19872 +       const unsigned char try_aopen
19873 +               = (arg->type == Creat && arg->u.c.try_aopen);
19874 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19875 +       struct dentry *wh_dentry, *parent;
19876 +       struct inode *h_dir;
19877 +       struct super_block *sb;
19878 +       struct au_branch *br;
19879 +       /* to reduce stack size */
19880 +       struct {
19881 +               struct au_dtime dt;
19882 +               struct au_pin pin;
19883 +               struct path h_path;
19884 +               struct au_wr_dir_args wr_dir_args;
19885 +       } *a;
19886 +
19887 +       AuDbg("%pd\n", dentry);
19888 +       IMustLock(dir);
19889 +
19890 +       err = -ENOMEM;
19891 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19892 +       if (unlikely(!a))
19893 +               goto out;
19894 +       a->wr_dir_args.force_btgt = -1;
19895 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19896 +
19897 +       parent = dentry->d_parent; /* dir inode is locked */
19898 +       if (!try_aopen) {
19899 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19900 +               if (unlikely(err))
19901 +                       goto out_free;
19902 +       }
19903 +       err = au_d_may_add(dentry);
19904 +       if (unlikely(err))
19905 +               goto out_unlock;
19906 +       if (!try_aopen)
19907 +               di_write_lock_parent(parent);
19908 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19909 +                                     &a->pin, &a->wr_dir_args);
19910 +       err = PTR_ERR(wh_dentry);
19911 +       if (IS_ERR(wh_dentry))
19912 +               goto out_parent;
19913 +
19914 +       btop = au_dbtop(dentry);
19915 +       sb = dentry->d_sb;
19916 +       br = au_sbr(sb, btop);
19917 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19918 +       a->h_path.mnt = au_br_mnt(br);
19919 +       h_dir = au_pinned_h_dir(&a->pin);
19920 +       switch (arg->type) {
19921 +       case Creat:
19922 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19923 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19924 +                                          arg->u.c.want_excl);
19925 +                       created = !err;
19926 +                       if (!err && try_aopen)
19927 +                               aopen->file->f_mode |= FMODE_CREATED;
19928 +               } else {
19929 +                       aopen->br = br;
19930 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19931 +                       AuDbg("err %d\n", err);
19932 +                       AuDbgFile(aopen->file);
19933 +                       created = err >= 0
19934 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19935 +               }
19936 +               break;
19937 +       case Symlink:
19938 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19939 +               created = !err;
19940 +               break;
19941 +       case Mknod:
19942 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19943 +                                 arg->u.m.dev);
19944 +               created = !err;
19945 +               break;
19946 +       default:
19947 +               BUG();
19948 +       }
19949 +       if (unlikely(err < 0))
19950 +               goto out_unpin;
19951 +
19952 +       err = epilog(dir, btop, wh_dentry, dentry);
19953 +       if (!err)
19954 +               goto out_unpin; /* success */
19955 +
19956 +       /* revert */
19957 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19958 +               /* no delegation since it is just created */
19959 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19960 +                                   /*force*/0);
19961 +               if (rerr) {
19962 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19963 +                               dentry, err, rerr);
19964 +                       err = -EIO;
19965 +               }
19966 +               au_dtime_revert(&a->dt);
19967 +       }
19968 +       if (try_aopen && h_dir->i_op->atomic_open
19969 +           && (aopen->file->f_mode & FMODE_OPENED))
19970 +               /* aopen->file is still opened */
19971 +               au_lcnt_dec(&aopen->br->br_nfiles);
19972 +
19973 +out_unpin:
19974 +       au_unpin(&a->pin);
19975 +       dput(wh_dentry);
19976 +out_parent:
19977 +       if (!try_aopen)
19978 +               di_write_unlock(parent);
19979 +out_unlock:
19980 +       if (unlikely(err)) {
19981 +               au_update_dbtop(dentry);
19982 +               d_drop(dentry);
19983 +       }
19984 +       if (!try_aopen)
19985 +               aufs_read_unlock(dentry, AuLock_DW);
19986 +out_free:
19987 +       au_kfree_rcu(a);
19988 +out:
19989 +       return err;
19990 +}
19991 +
19992 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19993 +              dev_t dev)
19994 +{
19995 +       struct simple_arg arg = {
19996 +               .type = Mknod,
19997 +               .u.m = {
19998 +                       .mode   = mode,
19999 +                       .dev    = dev
20000 +               }
20001 +       };
20002 +       return add_simple(dir, dentry, &arg);
20003 +}
20004 +
20005 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
20006 +{
20007 +       struct simple_arg arg = {
20008 +               .type = Symlink,
20009 +               .u.s.symname = symname
20010 +       };
20011 +       return add_simple(dir, dentry, &arg);
20012 +}
20013 +
20014 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20015 +               bool want_excl)
20016 +{
20017 +       struct simple_arg arg = {
20018 +               .type = Creat,
20019 +               .u.c = {
20020 +                       .mode           = mode,
20021 +                       .want_excl      = want_excl
20022 +               }
20023 +       };
20024 +       return add_simple(dir, dentry, &arg);
20025 +}
20026 +
20027 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20028 +                      struct vfsub_aopen_args *aopen_args)
20029 +{
20030 +       struct simple_arg arg = {
20031 +               .type = Creat,
20032 +               .u.c = {
20033 +                       .mode           = aopen_args->create_mode,
20034 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
20035 +                       .try_aopen      = true,
20036 +                       .aopen          = aopen_args
20037 +               }
20038 +       };
20039 +       return add_simple(dir, dentry, &arg);
20040 +}
20041 +
20042 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
20043 +{
20044 +       int err;
20045 +       aufs_bindex_t bindex;
20046 +       struct super_block *sb;
20047 +       struct dentry *parent, *h_parent, *h_dentry;
20048 +       struct inode *h_dir, *inode;
20049 +       struct vfsmount *h_mnt;
20050 +       struct au_wr_dir_args wr_dir_args = {
20051 +               .force_btgt     = -1,
20052 +               .flags          = AuWrDir_TMPFILE
20053 +       };
20054 +
20055 +       /* copy-up may happen */
20056 +       inode_lock(dir);
20057 +
20058 +       sb = dir->i_sb;
20059 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20060 +       if (unlikely(err))
20061 +               goto out;
20062 +
20063 +       err = au_di_init(dentry);
20064 +       if (unlikely(err))
20065 +               goto out_si;
20066 +
20067 +       err = -EBUSY;
20068 +       parent = d_find_any_alias(dir);
20069 +       AuDebugOn(!parent);
20070 +       di_write_lock_parent(parent);
20071 +       if (unlikely(d_inode(parent) != dir))
20072 +               goto out_parent;
20073 +
20074 +       err = au_digen_test(parent, au_sigen(sb));
20075 +       if (unlikely(err))
20076 +               goto out_parent;
20077 +
20078 +       bindex = au_dbtop(parent);
20079 +       au_set_dbtop(dentry, bindex);
20080 +       au_set_dbbot(dentry, bindex);
20081 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
20082 +       bindex = err;
20083 +       if (unlikely(err < 0))
20084 +               goto out_parent;
20085 +
20086 +       err = -EOPNOTSUPP;
20087 +       h_dir = au_h_iptr(dir, bindex);
20088 +       if (unlikely(!h_dir->i_op->tmpfile))
20089 +               goto out_parent;
20090 +
20091 +       h_mnt = au_sbr_mnt(sb, bindex);
20092 +       err = vfsub_mnt_want_write(h_mnt);
20093 +       if (unlikely(err))
20094 +               goto out_parent;
20095 +
20096 +       h_parent = au_h_dptr(parent, bindex);
20097 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
20098 +       if (IS_ERR(h_dentry)) {
20099 +               err = PTR_ERR(h_dentry);
20100 +               goto out_mnt;
20101 +       }
20102 +
20103 +       au_set_dbtop(dentry, bindex);
20104 +       au_set_dbbot(dentry, bindex);
20105 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20106 +       inode = au_new_inode(dentry, /*must_new*/1);
20107 +       if (IS_ERR(inode)) {
20108 +               err = PTR_ERR(inode);
20109 +               au_set_h_dptr(dentry, bindex, NULL);
20110 +               au_set_dbtop(dentry, -1);
20111 +               au_set_dbbot(dentry, -1);
20112 +       } else {
20113 +               if (!inode->i_nlink)
20114 +                       set_nlink(inode, 1);
20115 +               d_tmpfile(dentry, inode);
20116 +               au_di(dentry)->di_tmpfile = 1;
20117 +
20118 +               /* update without i_mutex */
20119 +               if (au_ibtop(dir) == au_dbtop(dentry))
20120 +                       au_cpup_attr_timesizes(dir);
20121 +       }
20122 +       dput(h_dentry);
20123 +
20124 +out_mnt:
20125 +       vfsub_mnt_drop_write(h_mnt);
20126 +out_parent:
20127 +       di_write_unlock(parent);
20128 +       dput(parent);
20129 +       di_write_unlock(dentry);
20130 +       if (unlikely(err)) {
20131 +               au_di_fin(dentry);
20132 +               dentry->d_fsdata = NULL;
20133 +       }
20134 +out_si:
20135 +       si_read_unlock(sb);
20136 +out:
20137 +       inode_unlock(dir);
20138 +       return err;
20139 +}
20140 +
20141 +/* ---------------------------------------------------------------------- */
20142 +
20143 +struct au_link_args {
20144 +       aufs_bindex_t bdst, bsrc;
20145 +       struct au_pin pin;
20146 +       struct path h_path;
20147 +       struct dentry *src_parent, *parent;
20148 +};
20149 +
20150 +static int au_cpup_before_link(struct dentry *src_dentry,
20151 +                              struct au_link_args *a)
20152 +{
20153 +       int err;
20154 +       struct dentry *h_src_dentry;
20155 +       struct au_cp_generic cpg = {
20156 +               .dentry = src_dentry,
20157 +               .bdst   = a->bdst,
20158 +               .bsrc   = a->bsrc,
20159 +               .len    = -1,
20160 +               .pin    = &a->pin,
20161 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20162 +       };
20163 +
20164 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20165 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20166 +       if (unlikely(err))
20167 +               goto out;
20168 +
20169 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20170 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20171 +                    au_opt_udba(src_dentry->d_sb),
20172 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20173 +       if (unlikely(err))
20174 +               goto out;
20175 +
20176 +       err = au_sio_cpup_simple(&cpg);
20177 +       au_unpin(&a->pin);
20178 +
20179 +out:
20180 +       di_read_unlock(a->src_parent, AuLock_IR);
20181 +       return err;
20182 +}
20183 +
20184 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20185 +                          struct au_link_args *a)
20186 +{
20187 +       int err;
20188 +       unsigned char plink;
20189 +       aufs_bindex_t bbot;
20190 +       struct dentry *h_src_dentry;
20191 +       struct inode *h_inode, *inode, *delegated;
20192 +       struct super_block *sb;
20193 +       struct file *h_file;
20194 +
20195 +       plink = 0;
20196 +       h_inode = NULL;
20197 +       sb = src_dentry->d_sb;
20198 +       inode = d_inode(src_dentry);
20199 +       if (au_ibtop(inode) <= a->bdst)
20200 +               h_inode = au_h_iptr(inode, a->bdst);
20201 +       if (!h_inode || !h_inode->i_nlink) {
20202 +               /* copyup src_dentry as the name of dentry. */
20203 +               bbot = au_dbbot(dentry);
20204 +               if (bbot < a->bsrc)
20205 +                       au_set_dbbot(dentry, a->bsrc);
20206 +               au_set_h_dptr(dentry, a->bsrc,
20207 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20208 +               dget(a->h_path.dentry);
20209 +               au_set_h_dptr(dentry, a->bdst, NULL);
20210 +               AuDbg("temporary d_inode...\n");
20211 +               spin_lock(&dentry->d_lock);
20212 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20213 +               spin_unlock(&dentry->d_lock);
20214 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20215 +               if (IS_ERR(h_file))
20216 +                       err = PTR_ERR(h_file);
20217 +               else {
20218 +                       struct au_cp_generic cpg = {
20219 +                               .dentry = dentry,
20220 +                               .bdst   = a->bdst,
20221 +                               .bsrc   = -1,
20222 +                               .len    = -1,
20223 +                               .pin    = &a->pin,
20224 +                               .flags  = AuCpup_KEEPLINO
20225 +                       };
20226 +                       err = au_sio_cpup_simple(&cpg);
20227 +                       au_h_open_post(dentry, a->bsrc, h_file);
20228 +                       if (!err) {
20229 +                               dput(a->h_path.dentry);
20230 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20231 +                       } else
20232 +                               au_set_h_dptr(dentry, a->bdst,
20233 +                                             a->h_path.dentry);
20234 +               }
20235 +               spin_lock(&dentry->d_lock);
20236 +               dentry->d_inode = NULL; /* restore */
20237 +               spin_unlock(&dentry->d_lock);
20238 +               AuDbg("temporary d_inode...done\n");
20239 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20240 +               au_set_dbbot(dentry, bbot);
20241 +       } else {
20242 +               /* the inode of src_dentry already exists on a.bdst branch */
20243 +               h_src_dentry = d_find_alias(h_inode);
20244 +               if (!h_src_dentry && au_plink_test(inode)) {
20245 +                       plink = 1;
20246 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20247 +                       err = PTR_ERR(h_src_dentry);
20248 +                       if (IS_ERR(h_src_dentry))
20249 +                               goto out;
20250 +
20251 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20252 +                               dput(h_src_dentry);
20253 +                               h_src_dentry = NULL;
20254 +                       }
20255 +
20256 +               }
20257 +               if (h_src_dentry) {
20258 +                       delegated = NULL;
20259 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20260 +                                        &a->h_path, &delegated);
20261 +                       if (unlikely(err == -EWOULDBLOCK)) {
20262 +                               pr_warn("cannot retry for NFSv4 delegation"
20263 +                                       " for an internal link\n");
20264 +                               iput(delegated);
20265 +                       }
20266 +                       dput(h_src_dentry);
20267 +               } else {
20268 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20269 +                               h_inode->i_ino, a->bdst);
20270 +                       err = -EIO;
20271 +               }
20272 +       }
20273 +
20274 +       if (!err && !plink)
20275 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20276 +
20277 +out:
20278 +       AuTraceErr(err);
20279 +       return err;
20280 +}
20281 +
20282 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20283 +             struct dentry *dentry)
20284 +{
20285 +       int err, rerr;
20286 +       struct au_dtime dt;
20287 +       struct au_link_args *a;
20288 +       struct dentry *wh_dentry, *h_src_dentry;
20289 +       struct inode *inode, *delegated;
20290 +       struct super_block *sb;
20291 +       struct au_wr_dir_args wr_dir_args = {
20292 +               /* .force_btgt  = -1, */
20293 +               .flags          = AuWrDir_ADD_ENTRY
20294 +       };
20295 +
20296 +       IMustLock(dir);
20297 +       inode = d_inode(src_dentry);
20298 +       IMustLock(inode);
20299 +
20300 +       err = -ENOMEM;
20301 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20302 +       if (unlikely(!a))
20303 +               goto out;
20304 +
20305 +       a->parent = dentry->d_parent; /* dir inode is locked */
20306 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20307 +                                       AuLock_NOPLM | AuLock_GEN);
20308 +       if (unlikely(err))
20309 +               goto out_kfree;
20310 +       err = au_d_linkable(src_dentry);
20311 +       if (unlikely(err))
20312 +               goto out_unlock;
20313 +       err = au_d_may_add(dentry);
20314 +       if (unlikely(err))
20315 +               goto out_unlock;
20316 +
20317 +       a->src_parent = dget_parent(src_dentry);
20318 +       wr_dir_args.force_btgt = au_ibtop(inode);
20319 +
20320 +       di_write_lock_parent(a->parent);
20321 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20322 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20323 +                                     &wr_dir_args);
20324 +       err = PTR_ERR(wh_dentry);
20325 +       if (IS_ERR(wh_dentry))
20326 +               goto out_parent;
20327 +
20328 +       err = 0;
20329 +       sb = dentry->d_sb;
20330 +       a->bdst = au_dbtop(dentry);
20331 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20332 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20333 +       a->bsrc = au_ibtop(inode);
20334 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20335 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20336 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20337 +       if (!h_src_dentry) {
20338 +               a->bsrc = au_dbtop(src_dentry);
20339 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20340 +               AuDebugOn(!h_src_dentry);
20341 +       } else if (IS_ERR(h_src_dentry)) {
20342 +               err = PTR_ERR(h_src_dentry);
20343 +               goto out_parent;
20344 +       }
20345 +
20346 +       /*
20347 +        * aufs doesn't touch the credential so
20348 +        * security_dentry_create_files_as() is unnecessary.
20349 +        */
20350 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20351 +               if (a->bdst < a->bsrc
20352 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20353 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20354 +               else {
20355 +                       delegated = NULL;
20356 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20357 +                                        &a->h_path, &delegated);
20358 +                       if (unlikely(err == -EWOULDBLOCK)) {
20359 +                               pr_warn("cannot retry for NFSv4 delegation"
20360 +                                       " for an internal link\n");
20361 +                               iput(delegated);
20362 +                       }
20363 +               }
20364 +               dput(h_src_dentry);
20365 +       } else {
20366 +               /*
20367 +                * copyup src_dentry to the branch we process,
20368 +                * and then link(2) to it.
20369 +                */
20370 +               dput(h_src_dentry);
20371 +               if (a->bdst < a->bsrc
20372 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20373 +                       au_unpin(&a->pin);
20374 +                       di_write_unlock(a->parent);
20375 +                       err = au_cpup_before_link(src_dentry, a);
20376 +                       di_write_lock_parent(a->parent);
20377 +                       if (!err)
20378 +                               err = au_pin(&a->pin, dentry, a->bdst,
20379 +                                            au_opt_udba(sb),
20380 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20381 +                       if (unlikely(err))
20382 +                               goto out_wh;
20383 +               }
20384 +               if (!err) {
20385 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20386 +                       err = -ENOENT;
20387 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20388 +                               delegated = NULL;
20389 +                               err = vfsub_link(h_src_dentry,
20390 +                                                au_pinned_h_dir(&a->pin),
20391 +                                                &a->h_path, &delegated);
20392 +                               if (unlikely(err == -EWOULDBLOCK)) {
20393 +                                       pr_warn("cannot retry"
20394 +                                               " for NFSv4 delegation"
20395 +                                               " for an internal link\n");
20396 +                                       iput(delegated);
20397 +                               }
20398 +                       }
20399 +               }
20400 +       }
20401 +       if (unlikely(err))
20402 +               goto out_unpin;
20403 +
20404 +       if (wh_dentry) {
20405 +               a->h_path.dentry = wh_dentry;
20406 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20407 +                                         dentry);
20408 +               if (unlikely(err))
20409 +                       goto out_revert;
20410 +       }
20411 +
20412 +       au_dir_ts(dir, a->bdst);
20413 +       inode_inc_iversion(dir);
20414 +       inc_nlink(inode);
20415 +       inode->i_ctime = dir->i_ctime;
20416 +       d_instantiate(dentry, au_igrab(inode));
20417 +       if (d_unhashed(a->h_path.dentry))
20418 +               /* some filesystem calls d_drop() */
20419 +               d_drop(dentry);
20420 +       /* some filesystems consume an inode even hardlink */
20421 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20422 +       goto out_unpin; /* success */
20423 +
20424 +out_revert:
20425 +       /* no delegation since it is just created */
20426 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20427 +                           /*delegated*/NULL, /*force*/0);
20428 +       if (unlikely(rerr)) {
20429 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20430 +               err = -EIO;
20431 +       }
20432 +       au_dtime_revert(&dt);
20433 +out_unpin:
20434 +       au_unpin(&a->pin);
20435 +out_wh:
20436 +       dput(wh_dentry);
20437 +out_parent:
20438 +       di_write_unlock(a->parent);
20439 +       dput(a->src_parent);
20440 +out_unlock:
20441 +       if (unlikely(err)) {
20442 +               au_update_dbtop(dentry);
20443 +               d_drop(dentry);
20444 +       }
20445 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20446 +out_kfree:
20447 +       au_kfree_rcu(a);
20448 +out:
20449 +       AuTraceErr(err);
20450 +       return err;
20451 +}
20452 +
20453 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20454 +{
20455 +       int err, rerr;
20456 +       aufs_bindex_t bindex;
20457 +       unsigned char diropq;
20458 +       struct path h_path;
20459 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20460 +       struct inode *h_inode;
20461 +       struct super_block *sb;
20462 +       struct {
20463 +               struct au_pin pin;
20464 +               struct au_dtime dt;
20465 +       } *a; /* reduce the stack usage */
20466 +       struct au_wr_dir_args wr_dir_args = {
20467 +               .force_btgt     = -1,
20468 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20469 +       };
20470 +
20471 +       IMustLock(dir);
20472 +
20473 +       err = -ENOMEM;
20474 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20475 +       if (unlikely(!a))
20476 +               goto out;
20477 +
20478 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20479 +       if (unlikely(err))
20480 +               goto out_free;
20481 +       err = au_d_may_add(dentry);
20482 +       if (unlikely(err))
20483 +               goto out_unlock;
20484 +
20485 +       parent = dentry->d_parent; /* dir inode is locked */
20486 +       di_write_lock_parent(parent);
20487 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20488 +                                     &a->pin, &wr_dir_args);
20489 +       err = PTR_ERR(wh_dentry);
20490 +       if (IS_ERR(wh_dentry))
20491 +               goto out_parent;
20492 +
20493 +       sb = dentry->d_sb;
20494 +       bindex = au_dbtop(dentry);
20495 +       h_path.dentry = au_h_dptr(dentry, bindex);
20496 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20497 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20498 +       if (unlikely(err))
20499 +               goto out_unpin;
20500 +
20501 +       /* make the dir opaque */
20502 +       diropq = 0;
20503 +       h_inode = d_inode(h_path.dentry);
20504 +       if (wh_dentry
20505 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20506 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20507 +               opq_dentry = au_diropq_create(dentry, bindex);
20508 +               inode_unlock(h_inode);
20509 +               err = PTR_ERR(opq_dentry);
20510 +               if (IS_ERR(opq_dentry))
20511 +                       goto out_dir;
20512 +               dput(opq_dentry);
20513 +               diropq = 1;
20514 +       }
20515 +
20516 +       err = epilog(dir, bindex, wh_dentry, dentry);
20517 +       if (!err) {
20518 +               inc_nlink(dir);
20519 +               goto out_unpin; /* success */
20520 +       }
20521 +
20522 +       /* revert */
20523 +       if (diropq) {
20524 +               AuLabel(revert opq);
20525 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20526 +               rerr = au_diropq_remove(dentry, bindex);
20527 +               inode_unlock(h_inode);
20528 +               if (rerr) {
20529 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20530 +                               dentry, err, rerr);
20531 +                       err = -EIO;
20532 +               }
20533 +       }
20534 +
20535 +out_dir:
20536 +       AuLabel(revert dir);
20537 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20538 +       if (rerr) {
20539 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20540 +                       dentry, err, rerr);
20541 +               err = -EIO;
20542 +       }
20543 +       au_dtime_revert(&a->dt);
20544 +out_unpin:
20545 +       au_unpin(&a->pin);
20546 +       dput(wh_dentry);
20547 +out_parent:
20548 +       di_write_unlock(parent);
20549 +out_unlock:
20550 +       if (unlikely(err)) {
20551 +               au_update_dbtop(dentry);
20552 +               d_drop(dentry);
20553 +       }
20554 +       aufs_read_unlock(dentry, AuLock_DW);
20555 +out_free:
20556 +       au_kfree_rcu(a);
20557 +out:
20558 +       return err;
20559 +}
20560 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20561 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20562 +++ linux/fs/aufs/i_op.c        2020-01-27 10:57:18.172204883 +0100
20563 @@ -0,0 +1,1498 @@
20564 +// SPDX-License-Identifier: GPL-2.0
20565 +/*
20566 + * Copyright (C) 2005-2020 Junjiro R. Okajima
20567 + *
20568 + * This program, aufs is free software; you can redistribute it and/or modify
20569 + * it under the terms of the GNU General Public License as published by
20570 + * the Free Software Foundation; either version 2 of the License, or
20571 + * (at your option) any later version.
20572 + *
20573 + * This program is distributed in the hope that it will be useful,
20574 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20575 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20576 + * GNU General Public License for more details.
20577 + *
20578 + * You should have received a copy of the GNU General Public License
20579 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20580 + */
20581 +
20582 +/*
20583 + * inode operations (except add/del/rename)
20584 + */
20585 +
20586 +#include <linux/device_cgroup.h>
20587 +#include <linux/fs_stack.h>
20588 +#include <linux/iversion.h>
20589 +#include <linux/namei.h>
20590 +#include <linux/security.h>
20591 +#include "aufs.h"
20592 +
20593 +static int h_permission(struct inode *h_inode, int mask,
20594 +                       struct path *h_path, int brperm)
20595 +{
20596 +       int err;
20597 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20598 +
20599 +       err = -EPERM;
20600 +       if (write_mask && IS_IMMUTABLE(h_inode))
20601 +               goto out;
20602 +
20603 +       err = -EACCES;
20604 +       if (((mask & MAY_EXEC)
20605 +            && S_ISREG(h_inode->i_mode)
20606 +            && (path_noexec(h_path)
20607 +                || !(h_inode->i_mode & 0111))))
20608 +               goto out;
20609 +
20610 +       /*
20611 +        * - skip the lower fs test in the case of write to ro branch.
20612 +        * - nfs dir permission write check is optimized, but a policy for
20613 +        *   link/rename requires a real check.
20614 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20615 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20616 +        */
20617 +       if ((write_mask && !au_br_writable(brperm))
20618 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20619 +               && write_mask && !(mask & MAY_READ))
20620 +           || !h_inode->i_op->permission) {
20621 +               /* AuLabel(generic_permission); */
20622 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20623 +               err = generic_permission(h_inode, mask);
20624 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20625 +                       err = h_inode->i_op->permission(h_inode, mask);
20626 +               AuTraceErr(err);
20627 +       } else {
20628 +               /* AuLabel(h_inode->permission); */
20629 +               err = h_inode->i_op->permission(h_inode, mask);
20630 +               AuTraceErr(err);
20631 +       }
20632 +
20633 +       if (!err)
20634 +               err = devcgroup_inode_permission(h_inode, mask);
20635 +       if (!err)
20636 +               err = security_inode_permission(h_inode, mask);
20637 +
20638 +out:
20639 +       return err;
20640 +}
20641 +
20642 +static int aufs_permission(struct inode *inode, int mask)
20643 +{
20644 +       int err;
20645 +       aufs_bindex_t bindex, bbot;
20646 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20647 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20648 +       struct inode *h_inode;
20649 +       struct super_block *sb;
20650 +       struct au_branch *br;
20651 +
20652 +       /* todo: support rcu-walk? */
20653 +       if (mask & MAY_NOT_BLOCK)
20654 +               return -ECHILD;
20655 +
20656 +       sb = inode->i_sb;
20657 +       si_read_lock(sb, AuLock_FLUSH);
20658 +       ii_read_lock_child(inode);
20659 +#if 0 /* reserved for future use */
20660 +       /*
20661 +        * This test may be rather 'too much' since the test is essentially done
20662 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20663 +        * generation doesn't match to the superblock's here.  But it isn't a
20664 +        * big deal I suppose.
20665 +        */
20666 +       err = au_iigen_test(inode, au_sigen(sb));
20667 +       if (unlikely(err))
20668 +               goto out;
20669 +#endif
20670 +
20671 +       if (!isdir
20672 +           || write_mask
20673 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20674 +               err = au_busy_or_stale();
20675 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20676 +               if (unlikely(!h_inode
20677 +                            || (h_inode->i_mode & S_IFMT)
20678 +                            != (inode->i_mode & S_IFMT)))
20679 +                       goto out;
20680 +
20681 +               err = 0;
20682 +               bindex = au_ibtop(inode);
20683 +               br = au_sbr(sb, bindex);
20684 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20685 +               if (write_mask
20686 +                   && !err
20687 +                   && !special_file(h_inode->i_mode)) {
20688 +                       /* test whether the upper writable branch exists */
20689 +                       err = -EROFS;
20690 +                       for (; bindex >= 0; bindex--)
20691 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20692 +                                       err = 0;
20693 +                                       break;
20694 +                               }
20695 +               }
20696 +               goto out;
20697 +       }
20698 +
20699 +       /* non-write to dir */
20700 +       err = 0;
20701 +       bbot = au_ibbot(inode);
20702 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20703 +               h_inode = au_h_iptr(inode, bindex);
20704 +               if (h_inode) {
20705 +                       err = au_busy_or_stale();
20706 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20707 +                               break;
20708 +
20709 +                       br = au_sbr(sb, bindex);
20710 +                       err = h_permission(h_inode, mask, &br->br_path,
20711 +                                          br->br_perm);
20712 +               }
20713 +       }
20714 +
20715 +out:
20716 +       ii_read_unlock(inode);
20717 +       si_read_unlock(sb);
20718 +       return err;
20719 +}
20720 +
20721 +/* ---------------------------------------------------------------------- */
20722 +
20723 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20724 +                                 unsigned int flags)
20725 +{
20726 +       struct dentry *ret, *parent;
20727 +       struct inode *inode;
20728 +       struct super_block *sb;
20729 +       int err, npositive;
20730 +
20731 +       IMustLock(dir);
20732 +
20733 +       /* todo: support rcu-walk? */
20734 +       ret = ERR_PTR(-ECHILD);
20735 +       if (flags & LOOKUP_RCU)
20736 +               goto out;
20737 +
20738 +       ret = ERR_PTR(-ENAMETOOLONG);
20739 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20740 +               goto out;
20741 +
20742 +       sb = dir->i_sb;
20743 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20744 +       ret = ERR_PTR(err);
20745 +       if (unlikely(err))
20746 +               goto out;
20747 +
20748 +       err = au_di_init(dentry);
20749 +       ret = ERR_PTR(err);
20750 +       if (unlikely(err))
20751 +               goto out_si;
20752 +
20753 +       inode = NULL;
20754 +       npositive = 0; /* suppress a warning */
20755 +       parent = dentry->d_parent; /* dir inode is locked */
20756 +       di_read_lock_parent(parent, AuLock_IR);
20757 +       err = au_alive_dir(parent);
20758 +       if (!err)
20759 +               err = au_digen_test(parent, au_sigen(sb));
20760 +       if (!err) {
20761 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20762 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20763 +                                          AuLkup_ALLOW_NEG);
20764 +               err = npositive;
20765 +       }
20766 +       di_read_unlock(parent, AuLock_IR);
20767 +       ret = ERR_PTR(err);
20768 +       if (unlikely(err < 0))
20769 +               goto out_unlock;
20770 +
20771 +       if (npositive) {
20772 +               inode = au_new_inode(dentry, /*must_new*/0);
20773 +               if (IS_ERR(inode)) {
20774 +                       ret = (void *)inode;
20775 +                       inode = NULL;
20776 +                       goto out_unlock;
20777 +               }
20778 +       }
20779 +
20780 +       if (inode)
20781 +               atomic_inc(&inode->i_count);
20782 +       ret = d_splice_alias(inode, dentry);
20783 +#if 0 /* reserved for future use */
20784 +       if (unlikely(d_need_lookup(dentry))) {
20785 +               spin_lock(&dentry->d_lock);
20786 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20787 +               spin_unlock(&dentry->d_lock);
20788 +       } else
20789 +#endif
20790 +       if (inode) {
20791 +               if (!IS_ERR(ret)) {
20792 +                       iput(inode);
20793 +                       if (ret && ret != dentry)
20794 +                               ii_write_unlock(inode);
20795 +               } else {
20796 +                       ii_write_unlock(inode);
20797 +                       iput(inode);
20798 +                       inode = NULL;
20799 +               }
20800 +       }
20801 +
20802 +out_unlock:
20803 +       di_write_unlock(dentry);
20804 +out_si:
20805 +       si_read_unlock(sb);
20806 +out:
20807 +       return ret;
20808 +}
20809 +
20810 +/* ---------------------------------------------------------------------- */
20811 +
20812 +/*
20813 + * very dirty and complicated aufs ->atomic_open().
20814 + * aufs_atomic_open()
20815 + * + au_aopen_or_create()
20816 + *   + add_simple()
20817 + *     + vfsub_atomic_open()
20818 + *       + branch fs ->atomic_open()
20819 + *        may call the actual 'open' for h_file
20820 + *       + inc br_nfiles only if opened
20821 + * + au_aopen_no_open() or au_aopen_do_open()
20822 + *
20823 + * au_aopen_do_open()
20824 + * + finish_open()
20825 + *   + au_do_aopen()
20826 + *     + au_do_open() the body of all 'open'
20827 + *       + au_do_open_nondir()
20828 + *        set the passed h_file
20829 + *
20830 + * au_aopen_no_open()
20831 + * + finish_no_open()
20832 + */
20833 +
20834 +struct aopen_node {
20835 +       struct hlist_bl_node hblist;
20836 +       struct file *file, *h_file;
20837 +};
20838 +
20839 +static int au_do_aopen(struct inode *inode, struct file *file)
20840 +{
20841 +       struct hlist_bl_head *aopen;
20842 +       struct hlist_bl_node *pos;
20843 +       struct aopen_node *node;
20844 +       struct au_do_open_args args = {
20845 +               .aopen  = 1,
20846 +               .open   = au_do_open_nondir
20847 +       };
20848 +
20849 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20850 +       hlist_bl_lock(aopen);
20851 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20852 +               if (node->file == file) {
20853 +                       args.h_file = node->h_file;
20854 +                       break;
20855 +               }
20856 +       hlist_bl_unlock(aopen);
20857 +       /* AuDebugOn(!args.h_file); */
20858 +
20859 +       return au_do_open(file, &args);
20860 +}
20861 +
20862 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20863 +                           struct aopen_node *aopen_node)
20864 +{
20865 +       int err;
20866 +       struct hlist_bl_head *aopen;
20867 +
20868 +       AuLabel(here);
20869 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20870 +       au_hbl_add(&aopen_node->hblist, aopen);
20871 +       err = finish_open(file, dentry, au_do_aopen);
20872 +       au_hbl_del(&aopen_node->hblist, aopen);
20873 +       /* AuDbgFile(file); */
20874 +       AuDbg("%pd%s%s\n", dentry,
20875 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20876 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20877 +
20878 +       AuTraceErr(err);
20879 +       return err;
20880 +}
20881 +
20882 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20883 +{
20884 +       int err;
20885 +
20886 +       AuLabel(here);
20887 +       dget(dentry);
20888 +       err = finish_no_open(file, dentry);
20889 +
20890 +       AuTraceErr(err);
20891 +       return err;
20892 +}
20893 +
20894 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20895 +                           struct file *file, unsigned int open_flag,
20896 +                           umode_t create_mode)
20897 +{
20898 +       int err, did_open;
20899 +       unsigned int lkup_flags;
20900 +       aufs_bindex_t bindex;
20901 +       struct super_block *sb;
20902 +       struct dentry *parent, *d;
20903 +       struct vfsub_aopen_args args = {
20904 +               .open_flag      = open_flag,
20905 +               .create_mode    = create_mode
20906 +       };
20907 +       struct aopen_node aopen_node = {
20908 +               .file   = file
20909 +       };
20910 +
20911 +       IMustLock(dir);
20912 +       AuDbg("open_flag 0%o\n", open_flag);
20913 +       AuDbgDentry(dentry);
20914 +
20915 +       err = 0;
20916 +       if (!au_di(dentry)) {
20917 +               lkup_flags = LOOKUP_OPEN;
20918 +               if (open_flag & O_CREAT)
20919 +                       lkup_flags |= LOOKUP_CREATE;
20920 +               d = aufs_lookup(dir, dentry, lkup_flags);
20921 +               if (IS_ERR(d)) {
20922 +                       err = PTR_ERR(d);
20923 +                       AuTraceErr(err);
20924 +                       goto out;
20925 +               } else if (d) {
20926 +                       /*
20927 +                        * obsoleted dentry found.
20928 +                        * another error will be returned later.
20929 +                        */
20930 +                       d_drop(d);
20931 +                       AuDbgDentry(d);
20932 +                       dput(d);
20933 +               }
20934 +               AuDbgDentry(dentry);
20935 +       }
20936 +
20937 +       if (d_is_positive(dentry)
20938 +           || d_unhashed(dentry)
20939 +           || d_unlinked(dentry)
20940 +           || !(open_flag & O_CREAT)) {
20941 +               err = au_aopen_no_open(file, dentry);
20942 +               goto out; /* success */
20943 +       }
20944 +
20945 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20946 +       if (unlikely(err))
20947 +               goto out;
20948 +
20949 +       sb = dentry->d_sb;
20950 +       parent = dentry->d_parent;      /* dir is locked */
20951 +       di_write_lock_parent(parent);
20952 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20953 +       if (unlikely(err < 0))
20954 +               goto out_parent;
20955 +
20956 +       AuDbgDentry(dentry);
20957 +       if (d_is_positive(dentry)) {
20958 +               err = au_aopen_no_open(file, dentry);
20959 +               goto out_parent; /* success */
20960 +       }
20961 +
20962 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20963 +       err = PTR_ERR(args.file);
20964 +       if (IS_ERR(args.file))
20965 +               goto out_parent;
20966 +
20967 +       bindex = au_dbtop(dentry);
20968 +       err = au_aopen_or_create(dir, dentry, &args);
20969 +       AuTraceErr(err);
20970 +       AuDbgFile(args.file);
20971 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20972 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20973 +       if (!did_open) {
20974 +               fput(args.file);
20975 +               args.file = NULL;
20976 +       }
20977 +       di_write_unlock(parent);
20978 +       di_write_unlock(dentry);
20979 +       if (unlikely(err < 0)) {
20980 +               if (args.file)
20981 +                       fput(args.file);
20982 +               goto out_sb;
20983 +       }
20984 +
20985 +       if (!did_open)
20986 +               err = au_aopen_no_open(file, dentry);
20987 +       else {
20988 +               aopen_node.h_file = args.file;
20989 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20990 +       }
20991 +       if (unlikely(err < 0)) {
20992 +               if (args.file)
20993 +                       fput(args.file);
20994 +               if (did_open)
20995 +                       au_lcnt_dec(&args.br->br_nfiles);
20996 +       }
20997 +       goto out_sb; /* success */
20998 +
20999 +out_parent:
21000 +       di_write_unlock(parent);
21001 +       di_write_unlock(dentry);
21002 +out_sb:
21003 +       si_read_unlock(sb);
21004 +out:
21005 +       AuTraceErr(err);
21006 +       AuDbgFile(file);
21007 +       return err;
21008 +}
21009 +
21010 +
21011 +/* ---------------------------------------------------------------------- */
21012 +
21013 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
21014 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
21015 +                         aufs_bindex_t btop)
21016 +{
21017 +       int err;
21018 +       struct dentry *h_parent;
21019 +       struct inode *h_dir;
21020 +
21021 +       if (add_entry)
21022 +               IMustLock(d_inode(parent));
21023 +       else
21024 +               di_write_lock_parent(parent);
21025 +
21026 +       err = 0;
21027 +       if (!au_h_dptr(parent, bcpup)) {
21028 +               if (btop > bcpup)
21029 +                       err = au_cpup_dirs(dentry, bcpup);
21030 +               else if (btop < bcpup)
21031 +                       err = au_cpdown_dirs(dentry, bcpup);
21032 +               else
21033 +                       BUG();
21034 +       }
21035 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
21036 +               h_parent = au_h_dptr(parent, bcpup);
21037 +               h_dir = d_inode(h_parent);
21038 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
21039 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
21040 +               /* todo: no unlock here */
21041 +               inode_unlock_shared(h_dir);
21042 +
21043 +               AuDbg("bcpup %d\n", bcpup);
21044 +               if (!err) {
21045 +                       if (d_really_is_negative(dentry))
21046 +                               au_set_h_dptr(dentry, btop, NULL);
21047 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
21048 +               }
21049 +       }
21050 +
21051 +       if (!add_entry)
21052 +               di_write_unlock(parent);
21053 +       if (!err)
21054 +               err = bcpup; /* success */
21055 +
21056 +       AuTraceErr(err);
21057 +       return err;
21058 +}
21059 +
21060 +/*
21061 + * decide the branch and the parent dir where we will create a new entry.
21062 + * returns new bindex or an error.
21063 + * copyup the parent dir if needed.
21064 + */
21065 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
21066 +             struct au_wr_dir_args *args)
21067 +{
21068 +       int err;
21069 +       unsigned int flags;
21070 +       aufs_bindex_t bcpup, btop, src_btop;
21071 +       const unsigned char add_entry
21072 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
21073 +               | au_ftest_wrdir(args->flags, TMPFILE);
21074 +       struct super_block *sb;
21075 +       struct dentry *parent;
21076 +       struct au_sbinfo *sbinfo;
21077 +
21078 +       sb = dentry->d_sb;
21079 +       sbinfo = au_sbi(sb);
21080 +       parent = dget_parent(dentry);
21081 +       btop = au_dbtop(dentry);
21082 +       bcpup = btop;
21083 +       if (args->force_btgt < 0) {
21084 +               if (src_dentry) {
21085 +                       src_btop = au_dbtop(src_dentry);
21086 +                       if (src_btop < btop)
21087 +                               bcpup = src_btop;
21088 +               } else if (add_entry) {
21089 +                       flags = 0;
21090 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21091 +                               au_fset_wbr(flags, DIR);
21092 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21093 +                       bcpup = err;
21094 +               }
21095 +
21096 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21097 +                       if (add_entry)
21098 +                               err = AuWbrCopyup(sbinfo, dentry);
21099 +                       else {
21100 +                               if (!IS_ROOT(dentry)) {
21101 +                                       di_read_lock_parent(parent, !AuLock_IR);
21102 +                                       err = AuWbrCopyup(sbinfo, dentry);
21103 +                                       di_read_unlock(parent, !AuLock_IR);
21104 +                               } else
21105 +                                       err = AuWbrCopyup(sbinfo, dentry);
21106 +                       }
21107 +                       bcpup = err;
21108 +                       if (unlikely(err < 0))
21109 +                               goto out;
21110 +               }
21111 +       } else {
21112 +               bcpup = args->force_btgt;
21113 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21114 +       }
21115 +
21116 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21117 +       err = bcpup;
21118 +       if (bcpup == btop)
21119 +               goto out; /* success */
21120 +
21121 +       /* copyup the new parent into the branch we process */
21122 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21123 +       if (err >= 0) {
21124 +               if (d_really_is_negative(dentry)) {
21125 +                       au_set_h_dptr(dentry, btop, NULL);
21126 +                       au_set_dbtop(dentry, bcpup);
21127 +                       au_set_dbbot(dentry, bcpup);
21128 +               }
21129 +               AuDebugOn(add_entry
21130 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21131 +                         && !au_h_dptr(dentry, bcpup));
21132 +       }
21133 +
21134 +out:
21135 +       dput(parent);
21136 +       return err;
21137 +}
21138 +
21139 +/* ---------------------------------------------------------------------- */
21140 +
21141 +void au_pin_hdir_unlock(struct au_pin *p)
21142 +{
21143 +       if (p->hdir)
21144 +               au_hn_inode_unlock(p->hdir);
21145 +}
21146 +
21147 +int au_pin_hdir_lock(struct au_pin *p)
21148 +{
21149 +       int err;
21150 +
21151 +       err = 0;
21152 +       if (!p->hdir)
21153 +               goto out;
21154 +
21155 +       /* even if an error happens later, keep this lock */
21156 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21157 +
21158 +       err = -EBUSY;
21159 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21160 +               goto out;
21161 +
21162 +       err = 0;
21163 +       if (p->h_dentry)
21164 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21165 +                                 p->h_parent, p->br);
21166 +
21167 +out:
21168 +       return err;
21169 +}
21170 +
21171 +int au_pin_hdir_relock(struct au_pin *p)
21172 +{
21173 +       int err, i;
21174 +       struct inode *h_i;
21175 +       struct dentry *h_d[] = {
21176 +               p->h_dentry,
21177 +               p->h_parent
21178 +       };
21179 +
21180 +       err = au_pin_hdir_lock(p);
21181 +       if (unlikely(err))
21182 +               goto out;
21183 +
21184 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21185 +               if (!h_d[i])
21186 +                       continue;
21187 +               if (d_is_positive(h_d[i])) {
21188 +                       h_i = d_inode(h_d[i]);
21189 +                       err = !h_i->i_nlink;
21190 +               }
21191 +       }
21192 +
21193 +out:
21194 +       return err;
21195 +}
21196 +
21197 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21198 +{
21199 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21200 +}
21201 +
21202 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21203 +{
21204 +       if (p->hdir) {
21205 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21206 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21207 +               au_pin_hdir_set_owner(p, current);
21208 +       }
21209 +}
21210 +
21211 +void au_pin_hdir_release(struct au_pin *p)
21212 +{
21213 +       if (p->hdir) {
21214 +               au_pin_hdir_set_owner(p, p->task);
21215 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21216 +       }
21217 +}
21218 +
21219 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21220 +{
21221 +       if (pin && pin->parent)
21222 +               return au_h_dptr(pin->parent, pin->bindex);
21223 +       return NULL;
21224 +}
21225 +
21226 +void au_unpin(struct au_pin *p)
21227 +{
21228 +       if (p->hdir)
21229 +               au_pin_hdir_unlock(p);
21230 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21231 +               vfsub_mnt_drop_write(p->h_mnt);
21232 +       if (!p->hdir)
21233 +               return;
21234 +
21235 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21236 +               di_read_unlock(p->parent, AuLock_IR);
21237 +       iput(p->hdir->hi_inode);
21238 +       dput(p->parent);
21239 +       p->parent = NULL;
21240 +       p->hdir = NULL;
21241 +       p->h_mnt = NULL;
21242 +       /* do not clear p->task */
21243 +}
21244 +
21245 +int au_do_pin(struct au_pin *p)
21246 +{
21247 +       int err;
21248 +       struct super_block *sb;
21249 +       struct inode *h_dir;
21250 +
21251 +       err = 0;
21252 +       sb = p->dentry->d_sb;
21253 +       p->br = au_sbr(sb, p->bindex);
21254 +       if (IS_ROOT(p->dentry)) {
21255 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21256 +                       p->h_mnt = au_br_mnt(p->br);
21257 +                       err = vfsub_mnt_want_write(p->h_mnt);
21258 +                       if (unlikely(err)) {
21259 +                               au_fclr_pin(p->flags, MNT_WRITE);
21260 +                               goto out_err;
21261 +                       }
21262 +               }
21263 +               goto out;
21264 +       }
21265 +
21266 +       p->h_dentry = NULL;
21267 +       if (p->bindex <= au_dbbot(p->dentry))
21268 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21269 +
21270 +       p->parent = dget_parent(p->dentry);
21271 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21272 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21273 +
21274 +       h_dir = NULL;
21275 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21276 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21277 +       if (p->hdir)
21278 +               h_dir = p->hdir->hi_inode;
21279 +
21280 +       /*
21281 +        * udba case, or
21282 +        * if DI_LOCKED is not set, then p->parent may be different
21283 +        * and h_parent can be NULL.
21284 +        */
21285 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21286 +               err = -EBUSY;
21287 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21288 +                       di_read_unlock(p->parent, AuLock_IR);
21289 +               dput(p->parent);
21290 +               p->parent = NULL;
21291 +               goto out_err;
21292 +       }
21293 +
21294 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21295 +               p->h_mnt = au_br_mnt(p->br);
21296 +               err = vfsub_mnt_want_write(p->h_mnt);
21297 +               if (unlikely(err)) {
21298 +                       au_fclr_pin(p->flags, MNT_WRITE);
21299 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21300 +                               di_read_unlock(p->parent, AuLock_IR);
21301 +                       dput(p->parent);
21302 +                       p->parent = NULL;
21303 +                       goto out_err;
21304 +               }
21305 +       }
21306 +
21307 +       au_igrab(h_dir);
21308 +       err = au_pin_hdir_lock(p);
21309 +       if (!err)
21310 +               goto out; /* success */
21311 +
21312 +       au_unpin(p);
21313 +
21314 +out_err:
21315 +       pr_err("err %d\n", err);
21316 +       err = au_busy_or_stale();
21317 +out:
21318 +       return err;
21319 +}
21320 +
21321 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21322 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21323 +                unsigned int udba, unsigned char flags)
21324 +{
21325 +       p->dentry = dentry;
21326 +       p->udba = udba;
21327 +       p->lsc_di = lsc_di;
21328 +       p->lsc_hi = lsc_hi;
21329 +       p->flags = flags;
21330 +       p->bindex = bindex;
21331 +
21332 +       p->parent = NULL;
21333 +       p->hdir = NULL;
21334 +       p->h_mnt = NULL;
21335 +
21336 +       p->h_dentry = NULL;
21337 +       p->h_parent = NULL;
21338 +       p->br = NULL;
21339 +       p->task = current;
21340 +}
21341 +
21342 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21343 +          unsigned int udba, unsigned char flags)
21344 +{
21345 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21346 +                   udba, flags);
21347 +       return au_do_pin(pin);
21348 +}
21349 +
21350 +/* ---------------------------------------------------------------------- */
21351 +
21352 +/*
21353 + * ->setattr() and ->getattr() are called in various cases.
21354 + * chmod, stat: dentry is revalidated.
21355 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21356 + *               unhashed.
21357 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21358 + */
21359 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21360 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21361 +{
21362 +       int err;
21363 +       struct dentry *parent;
21364 +
21365 +       err = 0;
21366 +       if (au_digen_test(dentry, sigen)) {
21367 +               parent = dget_parent(dentry);
21368 +               di_read_lock_parent(parent, AuLock_IR);
21369 +               err = au_refresh_dentry(dentry, parent);
21370 +               di_read_unlock(parent, AuLock_IR);
21371 +               dput(parent);
21372 +       }
21373 +
21374 +       AuTraceErr(err);
21375 +       return err;
21376 +}
21377 +
21378 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21379 +                    struct au_icpup_args *a)
21380 +{
21381 +       int err;
21382 +       loff_t sz;
21383 +       aufs_bindex_t btop, ibtop;
21384 +       struct dentry *hi_wh, *parent;
21385 +       struct inode *inode;
21386 +       struct au_wr_dir_args wr_dir_args = {
21387 +               .force_btgt     = -1,
21388 +               .flags          = 0
21389 +       };
21390 +
21391 +       if (d_is_dir(dentry))
21392 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21393 +       /* plink or hi_wh() case */
21394 +       btop = au_dbtop(dentry);
21395 +       inode = d_inode(dentry);
21396 +       ibtop = au_ibtop(inode);
21397 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21398 +               wr_dir_args.force_btgt = ibtop;
21399 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21400 +       if (unlikely(err < 0))
21401 +               goto out;
21402 +       a->btgt = err;
21403 +       if (err != btop)
21404 +               au_fset_icpup(a->flags, DID_CPUP);
21405 +
21406 +       err = 0;
21407 +       a->pin_flags = AuPin_MNT_WRITE;
21408 +       parent = NULL;
21409 +       if (!IS_ROOT(dentry)) {
21410 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21411 +               parent = dget_parent(dentry);
21412 +               di_write_lock_parent(parent);
21413 +       }
21414 +
21415 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21416 +       if (unlikely(err))
21417 +               goto out_parent;
21418 +
21419 +       sz = -1;
21420 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21421 +       a->h_inode = d_inode(a->h_path.dentry);
21422 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21423 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21424 +               if (ia->ia_size < i_size_read(a->h_inode))
21425 +                       sz = ia->ia_size;
21426 +               inode_unlock_shared(a->h_inode);
21427 +       }
21428 +
21429 +       hi_wh = NULL;
21430 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21431 +               hi_wh = au_hi_wh(inode, a->btgt);
21432 +               if (!hi_wh) {
21433 +                       struct au_cp_generic cpg = {
21434 +                               .dentry = dentry,
21435 +                               .bdst   = a->btgt,
21436 +                               .bsrc   = -1,
21437 +                               .len    = sz,
21438 +                               .pin    = &a->pin
21439 +                       };
21440 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21441 +                       if (unlikely(err))
21442 +                               goto out_unlock;
21443 +                       hi_wh = au_hi_wh(inode, a->btgt);
21444 +                       /* todo: revalidate hi_wh? */
21445 +               }
21446 +       }
21447 +
21448 +       if (parent) {
21449 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21450 +               di_downgrade_lock(parent, AuLock_IR);
21451 +               dput(parent);
21452 +               parent = NULL;
21453 +       }
21454 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21455 +               goto out; /* success */
21456 +
21457 +       if (!d_unhashed(dentry)) {
21458 +               struct au_cp_generic cpg = {
21459 +                       .dentry = dentry,
21460 +                       .bdst   = a->btgt,
21461 +                       .bsrc   = btop,
21462 +                       .len    = sz,
21463 +                       .pin    = &a->pin,
21464 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21465 +               };
21466 +               err = au_sio_cpup_simple(&cpg);
21467 +               if (!err)
21468 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21469 +       } else if (!hi_wh)
21470 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21471 +       else
21472 +               a->h_path.dentry = hi_wh; /* do not dget here */
21473 +
21474 +out_unlock:
21475 +       a->h_inode = d_inode(a->h_path.dentry);
21476 +       if (!err)
21477 +               goto out; /* success */
21478 +       au_unpin(&a->pin);
21479 +out_parent:
21480 +       if (parent) {
21481 +               di_write_unlock(parent);
21482 +               dput(parent);
21483 +       }
21484 +out:
21485 +       if (!err)
21486 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21487 +       return err;
21488 +}
21489 +
21490 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21491 +{
21492 +       int err;
21493 +       struct inode *inode, *delegated;
21494 +       struct super_block *sb;
21495 +       struct file *file;
21496 +       struct au_icpup_args *a;
21497 +
21498 +       inode = d_inode(dentry);
21499 +       IMustLock(inode);
21500 +
21501 +       err = setattr_prepare(dentry, ia);
21502 +       if (unlikely(err))
21503 +               goto out;
21504 +
21505 +       err = -ENOMEM;
21506 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21507 +       if (unlikely(!a))
21508 +               goto out;
21509 +
21510 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21511 +               ia->ia_valid &= ~ATTR_MODE;
21512 +
21513 +       file = NULL;
21514 +       sb = dentry->d_sb;
21515 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21516 +       if (unlikely(err))
21517 +               goto out_kfree;
21518 +
21519 +       if (ia->ia_valid & ATTR_FILE) {
21520 +               /* currently ftruncate(2) only */
21521 +               AuDebugOn(!d_is_reg(dentry));
21522 +               file = ia->ia_file;
21523 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21524 +                                           /*fi_lsc*/0);
21525 +               if (unlikely(err))
21526 +                       goto out_si;
21527 +               ia->ia_file = au_hf_top(file);
21528 +               a->udba = AuOpt_UDBA_NONE;
21529 +       } else {
21530 +               /* fchmod() doesn't pass ia_file */
21531 +               a->udba = au_opt_udba(sb);
21532 +               di_write_lock_child(dentry);
21533 +               /* no d_unlinked(), to set UDBA_NONE for root */
21534 +               if (d_unhashed(dentry))
21535 +                       a->udba = AuOpt_UDBA_NONE;
21536 +               if (a->udba != AuOpt_UDBA_NONE) {
21537 +                       AuDebugOn(IS_ROOT(dentry));
21538 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21539 +                       if (unlikely(err))
21540 +                               goto out_dentry;
21541 +               }
21542 +       }
21543 +
21544 +       err = au_pin_and_icpup(dentry, ia, a);
21545 +       if (unlikely(err < 0))
21546 +               goto out_dentry;
21547 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21548 +               ia->ia_file = NULL;
21549 +               ia->ia_valid &= ~ATTR_FILE;
21550 +       }
21551 +
21552 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21553 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21554 +           == (ATTR_MODE | ATTR_CTIME)) {
21555 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21556 +               if (unlikely(err))
21557 +                       goto out_unlock;
21558 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21559 +                  && (ia->ia_valid & ATTR_CTIME)) {
21560 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21561 +               if (unlikely(err))
21562 +                       goto out_unlock;
21563 +       }
21564 +
21565 +       if (ia->ia_valid & ATTR_SIZE) {
21566 +               struct file *f;
21567 +
21568 +               if (ia->ia_size < i_size_read(inode))
21569 +                       /* unmap only */
21570 +                       truncate_setsize(inode, ia->ia_size);
21571 +
21572 +               f = NULL;
21573 +               if (ia->ia_valid & ATTR_FILE)
21574 +                       f = ia->ia_file;
21575 +               inode_unlock(a->h_inode);
21576 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21577 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21578 +       } else {
21579 +               delegated = NULL;
21580 +               while (1) {
21581 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21582 +                       if (delegated) {
21583 +                               err = break_deleg_wait(&delegated);
21584 +                               if (!err)
21585 +                                       continue;
21586 +                       }
21587 +                       break;
21588 +               }
21589 +       }
21590 +       /*
21591 +        * regardless aufs 'acl' option setting.
21592 +        * why don't all acl-aware fs call this func from their ->setattr()?
21593 +        */
21594 +       if (!err && (ia->ia_valid & ATTR_MODE))
21595 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21596 +       if (!err)
21597 +               au_cpup_attr_changeable(inode);
21598 +
21599 +out_unlock:
21600 +       inode_unlock(a->h_inode);
21601 +       au_unpin(&a->pin);
21602 +       if (unlikely(err))
21603 +               au_update_dbtop(dentry);
21604 +out_dentry:
21605 +       di_write_unlock(dentry);
21606 +       if (file) {
21607 +               fi_write_unlock(file);
21608 +               ia->ia_file = file;
21609 +               ia->ia_valid |= ATTR_FILE;
21610 +       }
21611 +out_si:
21612 +       si_read_unlock(sb);
21613 +out_kfree:
21614 +       au_kfree_rcu(a);
21615 +out:
21616 +       AuTraceErr(err);
21617 +       return err;
21618 +}
21619 +
21620 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21621 +static int au_h_path_to_set_attr(struct dentry *dentry,
21622 +                                struct au_icpup_args *a, struct path *h_path)
21623 +{
21624 +       int err;
21625 +       struct super_block *sb;
21626 +
21627 +       sb = dentry->d_sb;
21628 +       a->udba = au_opt_udba(sb);
21629 +       /* no d_unlinked(), to set UDBA_NONE for root */
21630 +       if (d_unhashed(dentry))
21631 +               a->udba = AuOpt_UDBA_NONE;
21632 +       if (a->udba != AuOpt_UDBA_NONE) {
21633 +               AuDebugOn(IS_ROOT(dentry));
21634 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21635 +               if (unlikely(err))
21636 +                       goto out;
21637 +       }
21638 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21639 +       if (unlikely(err < 0))
21640 +               goto out;
21641 +
21642 +       h_path->dentry = a->h_path.dentry;
21643 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21644 +
21645 +out:
21646 +       return err;
21647 +}
21648 +
21649 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21650 +                 struct au_sxattr *arg)
21651 +{
21652 +       int err;
21653 +       struct path h_path;
21654 +       struct super_block *sb;
21655 +       struct au_icpup_args *a;
21656 +       struct inode *h_inode;
21657 +
21658 +       IMustLock(inode);
21659 +
21660 +       err = -ENOMEM;
21661 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21662 +       if (unlikely(!a))
21663 +               goto out;
21664 +
21665 +       sb = dentry->d_sb;
21666 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21667 +       if (unlikely(err))
21668 +               goto out_kfree;
21669 +
21670 +       h_path.dentry = NULL;   /* silence gcc */
21671 +       di_write_lock_child(dentry);
21672 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21673 +       if (unlikely(err))
21674 +               goto out_di;
21675 +
21676 +       inode_unlock(a->h_inode);
21677 +       switch (arg->type) {
21678 +       case AU_XATTR_SET:
21679 +               AuDebugOn(d_is_negative(h_path.dentry));
21680 +               err = vfsub_setxattr(h_path.dentry,
21681 +                                    arg->u.set.name, arg->u.set.value,
21682 +                                    arg->u.set.size, arg->u.set.flags);
21683 +               break;
21684 +       case AU_ACL_SET:
21685 +               err = -EOPNOTSUPP;
21686 +               h_inode = d_inode(h_path.dentry);
21687 +               if (h_inode->i_op->set_acl)
21688 +                       /* this will call posix_acl_update_mode */
21689 +                       err = h_inode->i_op->set_acl(h_inode,
21690 +                                                    arg->u.acl_set.acl,
21691 +                                                    arg->u.acl_set.type);
21692 +               break;
21693 +       }
21694 +       if (!err)
21695 +               au_cpup_attr_timesizes(inode);
21696 +
21697 +       au_unpin(&a->pin);
21698 +       if (unlikely(err))
21699 +               au_update_dbtop(dentry);
21700 +
21701 +out_di:
21702 +       di_write_unlock(dentry);
21703 +       si_read_unlock(sb);
21704 +out_kfree:
21705 +       au_kfree_rcu(a);
21706 +out:
21707 +       AuTraceErr(err);
21708 +       return err;
21709 +}
21710 +#endif
21711 +
21712 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21713 +                            unsigned int nlink)
21714 +{
21715 +       unsigned int n;
21716 +
21717 +       inode->i_mode = st->mode;
21718 +       /* don't i_[ug]id_write() here */
21719 +       inode->i_uid = st->uid;
21720 +       inode->i_gid = st->gid;
21721 +       inode->i_atime = st->atime;
21722 +       inode->i_mtime = st->mtime;
21723 +       inode->i_ctime = st->ctime;
21724 +
21725 +       au_cpup_attr_nlink(inode, /*force*/0);
21726 +       if (S_ISDIR(inode->i_mode)) {
21727 +               n = inode->i_nlink;
21728 +               n -= nlink;
21729 +               n += st->nlink;
21730 +               smp_mb(); /* for i_nlink */
21731 +               /* 0 can happen */
21732 +               set_nlink(inode, n);
21733 +       }
21734 +
21735 +       spin_lock(&inode->i_lock);
21736 +       inode->i_blocks = st->blocks;
21737 +       i_size_write(inode, st->size);
21738 +       spin_unlock(&inode->i_lock);
21739 +}
21740 +
21741 +/*
21742 + * common routine for aufs_getattr() and au_getxattr().
21743 + * returns zero or negative (an error).
21744 + * @dentry will be read-locked in success.
21745 + */
21746 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
21747 +                     int locked)
21748 +{
21749 +       int err;
21750 +       unsigned int mnt_flags, sigen;
21751 +       unsigned char udba_none;
21752 +       aufs_bindex_t bindex;
21753 +       struct super_block *sb, *h_sb;
21754 +       struct inode *inode;
21755 +
21756 +       h_path->mnt = NULL;
21757 +       h_path->dentry = NULL;
21758 +
21759 +       err = 0;
21760 +       sb = dentry->d_sb;
21761 +       mnt_flags = au_mntflags(sb);
21762 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21763 +
21764 +       if (unlikely(locked))
21765 +               goto body; /* skip locking dinfo */
21766 +
21767 +       /* support fstat(2) */
21768 +       if (!d_unlinked(dentry) && !udba_none) {
21769 +               sigen = au_sigen(sb);
21770 +               err = au_digen_test(dentry, sigen);
21771 +               if (!err) {
21772 +                       di_read_lock_child(dentry, AuLock_IR);
21773 +                       err = au_dbrange_test(dentry);
21774 +                       if (unlikely(err)) {
21775 +                               di_read_unlock(dentry, AuLock_IR);
21776 +                               goto out;
21777 +                       }
21778 +               } else {
21779 +                       AuDebugOn(IS_ROOT(dentry));
21780 +                       di_write_lock_child(dentry);
21781 +                       err = au_dbrange_test(dentry);
21782 +                       if (!err)
21783 +                               err = au_reval_for_attr(dentry, sigen);
21784 +                       if (!err)
21785 +                               di_downgrade_lock(dentry, AuLock_IR);
21786 +                       else {
21787 +                               di_write_unlock(dentry);
21788 +                               goto out;
21789 +                       }
21790 +               }
21791 +       } else
21792 +               di_read_lock_child(dentry, AuLock_IR);
21793 +
21794 +body:
21795 +       inode = d_inode(dentry);
21796 +       bindex = au_ibtop(inode);
21797 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21798 +       h_sb = h_path->mnt->mnt_sb;
21799 +       if (!force
21800 +           && !au_test_fs_bad_iattr(h_sb)
21801 +           && udba_none)
21802 +               goto out; /* success */
21803 +
21804 +       if (au_dbtop(dentry) == bindex)
21805 +               h_path->dentry = au_h_dptr(dentry, bindex);
21806 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21807 +               h_path->dentry = au_plink_lkup(inode, bindex);
21808 +               if (IS_ERR(h_path->dentry))
21809 +                       /* pretending success */
21810 +                       h_path->dentry = NULL;
21811 +               else
21812 +                       dput(h_path->dentry);
21813 +       }
21814 +
21815 +out:
21816 +       return err;
21817 +}
21818 +
21819 +static int aufs_getattr(const struct path *path, struct kstat *st,
21820 +                       u32 request, unsigned int query)
21821 +{
21822 +       int err;
21823 +       unsigned char positive;
21824 +       struct path h_path;
21825 +       struct dentry *dentry;
21826 +       struct inode *inode;
21827 +       struct super_block *sb;
21828 +
21829 +       dentry = path->dentry;
21830 +       inode = d_inode(dentry);
21831 +       sb = dentry->d_sb;
21832 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21833 +       if (unlikely(err))
21834 +               goto out;
21835 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
21836 +       if (unlikely(err))
21837 +               goto out_si;
21838 +       if (unlikely(!h_path.dentry))
21839 +               /* illegally overlapped or something */
21840 +               goto out_fill; /* pretending success */
21841 +
21842 +       positive = d_is_positive(h_path.dentry);
21843 +       if (positive)
21844 +               /* no vfsub version */
21845 +               err = vfs_getattr(&h_path, st, request, query);
21846 +       if (!err) {
21847 +               if (positive)
21848 +                       au_refresh_iattr(inode, st,
21849 +                                        d_inode(h_path.dentry)->i_nlink);
21850 +               goto out_fill; /* success */
21851 +       }
21852 +       AuTraceErr(err);
21853 +       goto out_di;
21854 +
21855 +out_fill:
21856 +       generic_fillattr(inode, st);
21857 +out_di:
21858 +       di_read_unlock(dentry, AuLock_IR);
21859 +out_si:
21860 +       si_read_unlock(sb);
21861 +out:
21862 +       AuTraceErr(err);
21863 +       return err;
21864 +}
21865 +
21866 +/* ---------------------------------------------------------------------- */
21867 +
21868 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21869 +                                struct delayed_call *done)
21870 +{
21871 +       const char *ret;
21872 +       struct dentry *h_dentry;
21873 +       struct inode *h_inode;
21874 +       int err;
21875 +       aufs_bindex_t bindex;
21876 +
21877 +       ret = NULL; /* suppress a warning */
21878 +       err = -ECHILD;
21879 +       if (!dentry)
21880 +               goto out;
21881 +
21882 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21883 +       if (unlikely(err))
21884 +               goto out;
21885 +
21886 +       err = au_d_hashed_positive(dentry);
21887 +       if (unlikely(err))
21888 +               goto out_unlock;
21889 +
21890 +       err = -EINVAL;
21891 +       inode = d_inode(dentry);
21892 +       bindex = au_ibtop(inode);
21893 +       h_inode = au_h_iptr(inode, bindex);
21894 +       if (unlikely(!h_inode->i_op->get_link))
21895 +               goto out_unlock;
21896 +
21897 +       err = -EBUSY;
21898 +       h_dentry = NULL;
21899 +       if (au_dbtop(dentry) <= bindex) {
21900 +               h_dentry = au_h_dptr(dentry, bindex);
21901 +               if (h_dentry)
21902 +                       dget(h_dentry);
21903 +       }
21904 +       if (!h_dentry) {
21905 +               h_dentry = d_find_any_alias(h_inode);
21906 +               if (IS_ERR(h_dentry)) {
21907 +                       err = PTR_ERR(h_dentry);
21908 +                       goto out_unlock;
21909 +               }
21910 +       }
21911 +       if (unlikely(!h_dentry))
21912 +               goto out_unlock;
21913 +
21914 +       err = 0;
21915 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21916 +       AuDbgDentry(h_dentry);
21917 +       ret = vfs_get_link(h_dentry, done);
21918 +       dput(h_dentry);
21919 +       if (IS_ERR(ret))
21920 +               err = PTR_ERR(ret);
21921 +
21922 +out_unlock:
21923 +       aufs_read_unlock(dentry, AuLock_IR);
21924 +out:
21925 +       if (unlikely(err))
21926 +               ret = ERR_PTR(err);
21927 +       AuTraceErrPtr(ret);
21928 +       return ret;
21929 +}
21930 +
21931 +/* ---------------------------------------------------------------------- */
21932 +
21933 +static int au_is_special(struct inode *inode)
21934 +{
21935 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21936 +}
21937 +
21938 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21939 +                           int flags)
21940 +{
21941 +       int err;
21942 +       aufs_bindex_t bindex;
21943 +       struct super_block *sb;
21944 +       struct inode *h_inode;
21945 +       struct vfsmount *h_mnt;
21946 +
21947 +       sb = inode->i_sb;
21948 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21949 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21950 +
21951 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21952 +       lockdep_off();
21953 +       si_read_lock(sb, AuLock_FLUSH);
21954 +       ii_write_lock_child(inode);
21955 +
21956 +       err = 0;
21957 +       bindex = au_ibtop(inode);
21958 +       h_inode = au_h_iptr(inode, bindex);
21959 +       if (!au_test_ro(sb, bindex, inode)) {
21960 +               h_mnt = au_sbr_mnt(sb, bindex);
21961 +               err = vfsub_mnt_want_write(h_mnt);
21962 +               if (!err) {
21963 +                       err = vfsub_update_time(h_inode, ts, flags);
21964 +                       vfsub_mnt_drop_write(h_mnt);
21965 +               }
21966 +       } else if (au_is_special(h_inode)) {
21967 +               /*
21968 +                * Never copy-up here.
21969 +                * These special files may already be opened and used for
21970 +                * communicating. If we copied it up, then the communication
21971 +                * would be corrupted.
21972 +                */
21973 +               AuWarn1("timestamps for i%lu are ignored "
21974 +                       "since it is on readonly branch (hi%lu).\n",
21975 +                       inode->i_ino, h_inode->i_ino);
21976 +       } else if (flags & ~S_ATIME) {
21977 +               err = -EIO;
21978 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21979 +               AuDebugOn(1);
21980 +       }
21981 +
21982 +       if (!err)
21983 +               au_cpup_attr_timesizes(inode);
21984 +       ii_write_unlock(inode);
21985 +       si_read_unlock(sb);
21986 +       lockdep_on();
21987 +
21988 +       if (!err && (flags & S_VERSION))
21989 +               inode_inc_iversion(inode);
21990 +
21991 +       return err;
21992 +}
21993 +
21994 +/* ---------------------------------------------------------------------- */
21995 +
21996 +/* no getattr version will be set by module.c:aufs_init() */
21997 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21998 +       aufs_iop[] = {
21999 +       [AuIop_SYMLINK] = {
22000 +               .permission     = aufs_permission,
22001 +#ifdef CONFIG_FS_POSIX_ACL
22002 +               .get_acl        = aufs_get_acl,
22003 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
22004 +#endif
22005 +
22006 +               .setattr        = aufs_setattr,
22007 +               .getattr        = aufs_getattr,
22008 +
22009 +#ifdef CONFIG_AUFS_XATTR
22010 +               .listxattr      = aufs_listxattr,
22011 +#endif
22012 +
22013 +               .get_link       = aufs_get_link,
22014 +
22015 +               /* .update_time = aufs_update_time */
22016 +       },
22017 +       [AuIop_DIR] = {
22018 +               .create         = aufs_create,
22019 +               .lookup         = aufs_lookup,
22020 +               .link           = aufs_link,
22021 +               .unlink         = aufs_unlink,
22022 +               .symlink        = aufs_symlink,
22023 +               .mkdir          = aufs_mkdir,
22024 +               .rmdir          = aufs_rmdir,
22025 +               .mknod          = aufs_mknod,
22026 +               .rename         = aufs_rename,
22027 +
22028 +               .permission     = aufs_permission,
22029 +#ifdef CONFIG_FS_POSIX_ACL
22030 +               .get_acl        = aufs_get_acl,
22031 +               .set_acl        = aufs_set_acl,
22032 +#endif
22033 +
22034 +               .setattr        = aufs_setattr,
22035 +               .getattr        = aufs_getattr,
22036 +
22037 +#ifdef CONFIG_AUFS_XATTR
22038 +               .listxattr      = aufs_listxattr,
22039 +#endif
22040 +
22041 +               .update_time    = aufs_update_time,
22042 +               .atomic_open    = aufs_atomic_open,
22043 +               .tmpfile        = aufs_tmpfile
22044 +       },
22045 +       [AuIop_OTHER] = {
22046 +               .permission     = aufs_permission,
22047 +#ifdef CONFIG_FS_POSIX_ACL
22048 +               .get_acl        = aufs_get_acl,
22049 +               .set_acl        = aufs_set_acl,
22050 +#endif
22051 +
22052 +               .setattr        = aufs_setattr,
22053 +               .getattr        = aufs_getattr,
22054 +
22055 +#ifdef CONFIG_AUFS_XATTR
22056 +               .listxattr      = aufs_listxattr,
22057 +#endif
22058 +
22059 +               .update_time    = aufs_update_time
22060 +       }
22061 +};
22062 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
22063 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
22064 +++ linux/fs/aufs/i_op_del.c    2020-01-27 10:57:18.172204883 +0100
22065 @@ -0,0 +1,513 @@
22066 +// SPDX-License-Identifier: GPL-2.0
22067 +/*
22068 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22069 + *
22070 + * This program, aufs is free software; you can redistribute it and/or modify
22071 + * it under the terms of the GNU General Public License as published by
22072 + * the Free Software Foundation; either version 2 of the License, or
22073 + * (at your option) any later version.
22074 + *
22075 + * This program is distributed in the hope that it will be useful,
22076 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22077 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22078 + * GNU General Public License for more details.
22079 + *
22080 + * You should have received a copy of the GNU General Public License
22081 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22082 + */
22083 +
22084 +/*
22085 + * inode operations (del entry)
22086 + */
22087 +
22088 +#include <linux/iversion.h>
22089 +#include "aufs.h"
22090 +
22091 +/*
22092 + * decide if a new whiteout for @dentry is necessary or not.
22093 + * when it is necessary, prepare the parent dir for the upper branch whose
22094 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22095 + * be done by caller.
22096 + * return value:
22097 + * 0: wh is unnecessary
22098 + * plus: wh is necessary
22099 + * minus: error
22100 + */
22101 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22102 +{
22103 +       int need_wh, err;
22104 +       aufs_bindex_t btop;
22105 +       struct super_block *sb;
22106 +
22107 +       sb = dentry->d_sb;
22108 +       btop = au_dbtop(dentry);
22109 +       if (*bcpup < 0) {
22110 +               *bcpup = btop;
22111 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22112 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22113 +                       *bcpup = err;
22114 +                       if (unlikely(err < 0))
22115 +                               goto out;
22116 +               }
22117 +       } else
22118 +               AuDebugOn(btop < *bcpup
22119 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22120 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22121 +
22122 +       if (*bcpup != btop) {
22123 +               err = au_cpup_dirs(dentry, *bcpup);
22124 +               if (unlikely(err))
22125 +                       goto out;
22126 +               need_wh = 1;
22127 +       } else {
22128 +               struct au_dinfo *dinfo, *tmp;
22129 +
22130 +               need_wh = -ENOMEM;
22131 +               dinfo = au_di(dentry);
22132 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22133 +               if (tmp) {
22134 +                       au_di_cp(tmp, dinfo);
22135 +                       au_di_swap(tmp, dinfo);
22136 +                       /* returns the number of positive dentries */
22137 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22138 +                                                /* AuLkup_IGNORE_PERM */ 0);
22139 +                       au_di_swap(tmp, dinfo);
22140 +                       au_rw_write_unlock(&tmp->di_rwsem);
22141 +                       au_di_free(tmp);
22142 +               }
22143 +       }
22144 +       AuDbg("need_wh %d\n", need_wh);
22145 +       err = need_wh;
22146 +
22147 +out:
22148 +       return err;
22149 +}
22150 +
22151 +/*
22152 + * simple tests for the del-entry operations.
22153 + * following the checks in vfs, plus the parent-child relationship.
22154 + */
22155 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22156 +              struct dentry *h_parent, int isdir)
22157 +{
22158 +       int err;
22159 +       umode_t h_mode;
22160 +       struct dentry *h_dentry, *h_latest;
22161 +       struct inode *h_inode;
22162 +
22163 +       h_dentry = au_h_dptr(dentry, bindex);
22164 +       if (d_really_is_positive(dentry)) {
22165 +               err = -ENOENT;
22166 +               if (unlikely(d_is_negative(h_dentry)))
22167 +                       goto out;
22168 +               h_inode = d_inode(h_dentry);
22169 +               if (unlikely(!h_inode->i_nlink))
22170 +                       goto out;
22171 +
22172 +               h_mode = h_inode->i_mode;
22173 +               if (!isdir) {
22174 +                       err = -EISDIR;
22175 +                       if (unlikely(S_ISDIR(h_mode)))
22176 +                               goto out;
22177 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22178 +                       err = -ENOTDIR;
22179 +                       goto out;
22180 +               }
22181 +       } else {
22182 +               /* rename(2) case */
22183 +               err = -EIO;
22184 +               if (unlikely(d_is_positive(h_dentry)))
22185 +                       goto out;
22186 +       }
22187 +
22188 +       err = -ENOENT;
22189 +       /* expected parent dir is locked */
22190 +       if (unlikely(h_parent != h_dentry->d_parent))
22191 +               goto out;
22192 +       err = 0;
22193 +
22194 +       /*
22195 +        * rmdir a dir may break the consistency on some filesystem.
22196 +        * let's try heavy test.
22197 +        */
22198 +       err = -EACCES;
22199 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22200 +                    && au_test_h_perm(d_inode(h_parent),
22201 +                                      MAY_EXEC | MAY_WRITE)))
22202 +               goto out;
22203 +
22204 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22205 +       err = -EIO;
22206 +       if (IS_ERR(h_latest))
22207 +               goto out;
22208 +       if (h_latest == h_dentry)
22209 +               err = 0;
22210 +       dput(h_latest);
22211 +
22212 +out:
22213 +       return err;
22214 +}
22215 +
22216 +/*
22217 + * decide the branch where we operate for @dentry. the branch index will be set
22218 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22219 + * dir for reverting.
22220 + * when a new whiteout is necessary, create it.
22221 + */
22222 +static struct dentry*
22223 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22224 +                   struct au_dtime *dt, struct au_pin *pin)
22225 +{
22226 +       struct dentry *wh_dentry;
22227 +       struct super_block *sb;
22228 +       struct path h_path;
22229 +       int err, need_wh;
22230 +       unsigned int udba;
22231 +       aufs_bindex_t bcpup;
22232 +
22233 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22234 +       wh_dentry = ERR_PTR(need_wh);
22235 +       if (unlikely(need_wh < 0))
22236 +               goto out;
22237 +
22238 +       sb = dentry->d_sb;
22239 +       udba = au_opt_udba(sb);
22240 +       bcpup = *rbcpup;
22241 +       err = au_pin(pin, dentry, bcpup, udba,
22242 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22243 +       wh_dentry = ERR_PTR(err);
22244 +       if (unlikely(err))
22245 +               goto out;
22246 +
22247 +       h_path.dentry = au_pinned_h_parent(pin);
22248 +       if (udba != AuOpt_UDBA_NONE
22249 +           && au_dbtop(dentry) == bcpup) {
22250 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22251 +               wh_dentry = ERR_PTR(err);
22252 +               if (unlikely(err))
22253 +                       goto out_unpin;
22254 +       }
22255 +
22256 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22257 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22258 +       wh_dentry = NULL;
22259 +       if (!need_wh)
22260 +               goto out; /* success, no need to create whiteout */
22261 +
22262 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22263 +       if (IS_ERR(wh_dentry))
22264 +               goto out_unpin;
22265 +
22266 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22267 +       goto out; /* success */
22268 +
22269 +out_unpin:
22270 +       au_unpin(pin);
22271 +out:
22272 +       return wh_dentry;
22273 +}
22274 +
22275 +/*
22276 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22277 + * in order to be revertible and save time for removing many child whiteouts
22278 + * under the dir.
22279 + * returns 1 when there are too many child whiteout and caller should remove
22280 + * them asynchronously. returns 0 when the number of children is enough small to
22281 + * remove now or the branch fs is a remote fs.
22282 + * otherwise return an error.
22283 + */
22284 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22285 +                          struct au_nhash *whlist, struct inode *dir)
22286 +{
22287 +       int rmdir_later, err, dirwh;
22288 +       struct dentry *h_dentry;
22289 +       struct super_block *sb;
22290 +       struct inode *inode;
22291 +
22292 +       sb = dentry->d_sb;
22293 +       SiMustAnyLock(sb);
22294 +       h_dentry = au_h_dptr(dentry, bindex);
22295 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22296 +       if (unlikely(err))
22297 +               goto out;
22298 +
22299 +       /* stop monitoring */
22300 +       inode = d_inode(dentry);
22301 +       au_hn_free(au_hi(inode, bindex));
22302 +
22303 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22304 +               dirwh = au_sbi(sb)->si_dirwh;
22305 +               rmdir_later = (dirwh <= 1);
22306 +               if (!rmdir_later)
22307 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22308 +                                                             dirwh);
22309 +               if (rmdir_later)
22310 +                       return rmdir_later;
22311 +       }
22312 +
22313 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22314 +       if (unlikely(err)) {
22315 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22316 +                       h_dentry, bindex, err);
22317 +               err = 0;
22318 +       }
22319 +
22320 +out:
22321 +       AuTraceErr(err);
22322 +       return err;
22323 +}
22324 +
22325 +/*
22326 + * final procedure for deleting a entry.
22327 + * maintain dentry and iattr.
22328 + */
22329 +static void epilog(struct inode *dir, struct dentry *dentry,
22330 +                  aufs_bindex_t bindex)
22331 +{
22332 +       struct inode *inode;
22333 +
22334 +       inode = d_inode(dentry);
22335 +       d_drop(dentry);
22336 +       inode->i_ctime = dir->i_ctime;
22337 +
22338 +       au_dir_ts(dir, bindex);
22339 +       inode_inc_iversion(dir);
22340 +}
22341 +
22342 +/*
22343 + * when an error happened, remove the created whiteout and revert everything.
22344 + */
22345 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22346 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22347 +                    struct dentry *dentry, struct au_dtime *dt)
22348 +{
22349 +       int rerr;
22350 +       struct path h_path = {
22351 +               .dentry = wh_dentry,
22352 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22353 +       };
22354 +
22355 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22356 +       if (!rerr) {
22357 +               au_set_dbwh(dentry, bwh);
22358 +               au_dtime_revert(dt);
22359 +               return 0;
22360 +       }
22361 +
22362 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22363 +       return -EIO;
22364 +}
22365 +
22366 +/* ---------------------------------------------------------------------- */
22367 +
22368 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22369 +{
22370 +       int err;
22371 +       aufs_bindex_t bwh, bindex, btop;
22372 +       struct inode *inode, *h_dir, *delegated;
22373 +       struct dentry *parent, *wh_dentry;
22374 +       /* to reduce stack size */
22375 +       struct {
22376 +               struct au_dtime dt;
22377 +               struct au_pin pin;
22378 +               struct path h_path;
22379 +       } *a;
22380 +
22381 +       IMustLock(dir);
22382 +
22383 +       err = -ENOMEM;
22384 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22385 +       if (unlikely(!a))
22386 +               goto out;
22387 +
22388 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22389 +       if (unlikely(err))
22390 +               goto out_free;
22391 +       err = au_d_hashed_positive(dentry);
22392 +       if (unlikely(err))
22393 +               goto out_unlock;
22394 +       inode = d_inode(dentry);
22395 +       IMustLock(inode);
22396 +       err = -EISDIR;
22397 +       if (unlikely(d_is_dir(dentry)))
22398 +               goto out_unlock; /* possible? */
22399 +
22400 +       btop = au_dbtop(dentry);
22401 +       bwh = au_dbwh(dentry);
22402 +       bindex = -1;
22403 +       parent = dentry->d_parent; /* dir inode is locked */
22404 +       di_write_lock_parent(parent);
22405 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22406 +                                       &a->pin);
22407 +       err = PTR_ERR(wh_dentry);
22408 +       if (IS_ERR(wh_dentry))
22409 +               goto out_parent;
22410 +
22411 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22412 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22413 +       dget(a->h_path.dentry);
22414 +       if (bindex == btop) {
22415 +               h_dir = au_pinned_h_dir(&a->pin);
22416 +               delegated = NULL;
22417 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22418 +               if (unlikely(err == -EWOULDBLOCK)) {
22419 +                       pr_warn("cannot retry for NFSv4 delegation"
22420 +                               " for an internal unlink\n");
22421 +                       iput(delegated);
22422 +               }
22423 +       } else {
22424 +               /* dir inode is locked */
22425 +               h_dir = d_inode(wh_dentry->d_parent);
22426 +               IMustLock(h_dir);
22427 +               err = 0;
22428 +       }
22429 +
22430 +       if (!err) {
22431 +               vfsub_drop_nlink(inode);
22432 +               epilog(dir, dentry, bindex);
22433 +
22434 +               /* update target timestamps */
22435 +               if (bindex == btop) {
22436 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22437 +                       /*ignore*/
22438 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22439 +               } else
22440 +                       /* todo: this timestamp may be reverted later */
22441 +                       inode->i_ctime = h_dir->i_ctime;
22442 +               goto out_unpin; /* success */
22443 +       }
22444 +
22445 +       /* revert */
22446 +       if (wh_dentry) {
22447 +               int rerr;
22448 +
22449 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22450 +                                &a->dt);
22451 +               if (rerr)
22452 +                       err = rerr;
22453 +       }
22454 +
22455 +out_unpin:
22456 +       au_unpin(&a->pin);
22457 +       dput(wh_dentry);
22458 +       dput(a->h_path.dentry);
22459 +out_parent:
22460 +       di_write_unlock(parent);
22461 +out_unlock:
22462 +       aufs_read_unlock(dentry, AuLock_DW);
22463 +out_free:
22464 +       au_kfree_rcu(a);
22465 +out:
22466 +       return err;
22467 +}
22468 +
22469 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22470 +{
22471 +       int err, rmdir_later;
22472 +       aufs_bindex_t bwh, bindex, btop;
22473 +       struct inode *inode;
22474 +       struct dentry *parent, *wh_dentry, *h_dentry;
22475 +       struct au_whtmp_rmdir *args;
22476 +       /* to reduce stack size */
22477 +       struct {
22478 +               struct au_dtime dt;
22479 +               struct au_pin pin;
22480 +       } *a;
22481 +
22482 +       IMustLock(dir);
22483 +
22484 +       err = -ENOMEM;
22485 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22486 +       if (unlikely(!a))
22487 +               goto out;
22488 +
22489 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22490 +       if (unlikely(err))
22491 +               goto out_free;
22492 +       err = au_alive_dir(dentry);
22493 +       if (unlikely(err))
22494 +               goto out_unlock;
22495 +       inode = d_inode(dentry);
22496 +       IMustLock(inode);
22497 +       err = -ENOTDIR;
22498 +       if (unlikely(!d_is_dir(dentry)))
22499 +               goto out_unlock; /* possible? */
22500 +
22501 +       err = -ENOMEM;
22502 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22503 +       if (unlikely(!args))
22504 +               goto out_unlock;
22505 +
22506 +       parent = dentry->d_parent; /* dir inode is locked */
22507 +       di_write_lock_parent(parent);
22508 +       err = au_test_empty(dentry, &args->whlist);
22509 +       if (unlikely(err))
22510 +               goto out_parent;
22511 +
22512 +       btop = au_dbtop(dentry);
22513 +       bwh = au_dbwh(dentry);
22514 +       bindex = -1;
22515 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22516 +                                       &a->pin);
22517 +       err = PTR_ERR(wh_dentry);
22518 +       if (IS_ERR(wh_dentry))
22519 +               goto out_parent;
22520 +
22521 +       h_dentry = au_h_dptr(dentry, btop);
22522 +       dget(h_dentry);
22523 +       rmdir_later = 0;
22524 +       if (bindex == btop) {
22525 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22526 +               if (err > 0) {
22527 +                       rmdir_later = err;
22528 +                       err = 0;
22529 +               }
22530 +       } else {
22531 +               /* stop monitoring */
22532 +               au_hn_free(au_hi(inode, btop));
22533 +
22534 +               /* dir inode is locked */
22535 +               IMustLock(d_inode(wh_dentry->d_parent));
22536 +               err = 0;
22537 +       }
22538 +
22539 +       if (!err) {
22540 +               vfsub_dead_dir(inode);
22541 +               au_set_dbdiropq(dentry, -1);
22542 +               epilog(dir, dentry, bindex);
22543 +
22544 +               if (rmdir_later) {
22545 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22546 +                       args = NULL;
22547 +               }
22548 +
22549 +               goto out_unpin; /* success */
22550 +       }
22551 +
22552 +       /* revert */
22553 +       AuLabel(revert);
22554 +       if (wh_dentry) {
22555 +               int rerr;
22556 +
22557 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22558 +                                &a->dt);
22559 +               if (rerr)
22560 +                       err = rerr;
22561 +       }
22562 +
22563 +out_unpin:
22564 +       au_unpin(&a->pin);
22565 +       dput(wh_dentry);
22566 +       dput(h_dentry);
22567 +out_parent:
22568 +       di_write_unlock(parent);
22569 +       if (args)
22570 +               au_whtmp_rmdir_free(args);
22571 +out_unlock:
22572 +       aufs_read_unlock(dentry, AuLock_DW);
22573 +out_free:
22574 +       au_kfree_rcu(a);
22575 +out:
22576 +       AuTraceErr(err);
22577 +       return err;
22578 +}
22579 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22580 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22581 +++ linux/fs/aufs/i_op_ren.c    2020-01-27 10:57:18.172204883 +0100
22582 @@ -0,0 +1,1250 @@
22583 +// SPDX-License-Identifier: GPL-2.0
22584 +/*
22585 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22586 + *
22587 + * This program, aufs is free software; you can redistribute it and/or modify
22588 + * it under the terms of the GNU General Public License as published by
22589 + * the Free Software Foundation; either version 2 of the License, or
22590 + * (at your option) any later version.
22591 + *
22592 + * This program is distributed in the hope that it will be useful,
22593 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22594 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22595 + * GNU General Public License for more details.
22596 + *
22597 + * You should have received a copy of the GNU General Public License
22598 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22599 + */
22600 +
22601 +/*
22602 + * inode operation (rename entry)
22603 + * todo: this is crazy monster
22604 + */
22605 +
22606 +#include <linux/iversion.h>
22607 +#include "aufs.h"
22608 +
22609 +enum { AuSRC, AuDST, AuSrcDst };
22610 +enum { AuPARENT, AuCHILD, AuParentChild };
22611 +
22612 +#define AuRen_ISDIR_SRC                1
22613 +#define AuRen_ISDIR_DST                (1 << 1)
22614 +#define AuRen_ISSAMEDIR                (1 << 2)
22615 +#define AuRen_WHSRC            (1 << 3)
22616 +#define AuRen_WHDST            (1 << 4)
22617 +#define AuRen_MNT_WRITE                (1 << 5)
22618 +#define AuRen_DT_DSTDIR                (1 << 6)
22619 +#define AuRen_DIROPQ_SRC       (1 << 7)
22620 +#define AuRen_DIROPQ_DST       (1 << 8)
22621 +#define AuRen_DIRREN           (1 << 9)
22622 +#define AuRen_DROPPED_SRC      (1 << 10)
22623 +#define AuRen_DROPPED_DST      (1 << 11)
22624 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22625 +#define au_fset_ren(flags, name) \
22626 +       do { (flags) |= AuRen_##name; } while (0)
22627 +#define au_fclr_ren(flags, name) \
22628 +       do { (flags) &= ~AuRen_##name; } while (0)
22629 +
22630 +#ifndef CONFIG_AUFS_DIRREN
22631 +#undef AuRen_DIRREN
22632 +#define AuRen_DIRREN           0
22633 +#endif
22634 +
22635 +struct au_ren_args {
22636 +       struct {
22637 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22638 +                       *wh_dentry;
22639 +               struct inode *dir, *inode;
22640 +               struct au_hinode *hdir, *hinode;
22641 +               struct au_dtime dt[AuParentChild];
22642 +               aufs_bindex_t btop, bdiropq;
22643 +       } sd[AuSrcDst];
22644 +
22645 +#define src_dentry     sd[AuSRC].dentry
22646 +#define src_dir                sd[AuSRC].dir
22647 +#define src_inode      sd[AuSRC].inode
22648 +#define src_h_dentry   sd[AuSRC].h_dentry
22649 +#define src_parent     sd[AuSRC].parent
22650 +#define src_h_parent   sd[AuSRC].h_parent
22651 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22652 +#define src_hdir       sd[AuSRC].hdir
22653 +#define src_hinode     sd[AuSRC].hinode
22654 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22655 +#define src_dt         sd[AuSRC].dt
22656 +#define src_btop       sd[AuSRC].btop
22657 +#define src_bdiropq    sd[AuSRC].bdiropq
22658 +
22659 +#define dst_dentry     sd[AuDST].dentry
22660 +#define dst_dir                sd[AuDST].dir
22661 +#define dst_inode      sd[AuDST].inode
22662 +#define dst_h_dentry   sd[AuDST].h_dentry
22663 +#define dst_parent     sd[AuDST].parent
22664 +#define dst_h_parent   sd[AuDST].h_parent
22665 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22666 +#define dst_hdir       sd[AuDST].hdir
22667 +#define dst_hinode     sd[AuDST].hinode
22668 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22669 +#define dst_dt         sd[AuDST].dt
22670 +#define dst_btop       sd[AuDST].btop
22671 +#define dst_bdiropq    sd[AuDST].bdiropq
22672 +
22673 +       struct dentry *h_trap;
22674 +       struct au_branch *br;
22675 +       struct path h_path;
22676 +       struct au_nhash whlist;
22677 +       aufs_bindex_t btgt, src_bwh;
22678 +
22679 +       struct {
22680 +               unsigned short auren_flags;
22681 +               unsigned char flags;    /* syscall parameter */
22682 +               unsigned char exchange;
22683 +       } __packed;
22684 +
22685 +       struct au_whtmp_rmdir *thargs;
22686 +       struct dentry *h_dst;
22687 +       struct au_hinode *h_root;
22688 +};
22689 +
22690 +/* ---------------------------------------------------------------------- */
22691 +
22692 +/*
22693 + * functions for reverting.
22694 + * when an error happened in a single rename systemcall, we should revert
22695 + * everything as if nothing happened.
22696 + * we don't need to revert the copied-up/down the parent dir since they are
22697 + * harmless.
22698 + */
22699 +
22700 +#define RevertFailure(fmt, ...) do { \
22701 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22702 +               ##__VA_ARGS__, err, rerr); \
22703 +       err = -EIO; \
22704 +} while (0)
22705 +
22706 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22707 +{
22708 +       int rerr;
22709 +       struct dentry *d;
22710 +#define src_or_dst(member) a->sd[idx].member
22711 +
22712 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22713 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22714 +       rerr = au_diropq_remove(d, a->btgt);
22715 +       au_hn_inode_unlock(src_or_dst(hinode));
22716 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22717 +       if (rerr)
22718 +               RevertFailure("remove diropq %pd", d);
22719 +
22720 +#undef src_or_dst_
22721 +}
22722 +
22723 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22724 +{
22725 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22726 +               au_ren_do_rev_diropq(err, a, AuSRC);
22727 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22728 +               au_ren_do_rev_diropq(err, a, AuDST);
22729 +}
22730 +
22731 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22732 +{
22733 +       int rerr;
22734 +       struct inode *delegated;
22735 +
22736 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22737 +                                         a->src_h_parent);
22738 +       rerr = PTR_ERR(a->h_path.dentry);
22739 +       if (IS_ERR(a->h_path.dentry)) {
22740 +               RevertFailure("lkup one %pd", a->src_dentry);
22741 +               return;
22742 +       }
22743 +
22744 +       delegated = NULL;
22745 +       rerr = vfsub_rename(a->dst_h_dir,
22746 +                           au_h_dptr(a->src_dentry, a->btgt),
22747 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22748 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22749 +               pr_warn("cannot retry for NFSv4 delegation"
22750 +                       " for an internal rename\n");
22751 +               iput(delegated);
22752 +       }
22753 +       d_drop(a->h_path.dentry);
22754 +       dput(a->h_path.dentry);
22755 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22756 +       if (rerr)
22757 +               RevertFailure("rename %pd", a->src_dentry);
22758 +}
22759 +
22760 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22761 +{
22762 +       int rerr;
22763 +       struct inode *delegated;
22764 +
22765 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22766 +                                         a->dst_h_parent);
22767 +       rerr = PTR_ERR(a->h_path.dentry);
22768 +       if (IS_ERR(a->h_path.dentry)) {
22769 +               RevertFailure("lkup one %pd", a->dst_dentry);
22770 +               return;
22771 +       }
22772 +       if (d_is_positive(a->h_path.dentry)) {
22773 +               d_drop(a->h_path.dentry);
22774 +               dput(a->h_path.dentry);
22775 +               return;
22776 +       }
22777 +
22778 +       delegated = NULL;
22779 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22780 +                           &delegated, a->flags);
22781 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22782 +               pr_warn("cannot retry for NFSv4 delegation"
22783 +                       " for an internal rename\n");
22784 +               iput(delegated);
22785 +       }
22786 +       d_drop(a->h_path.dentry);
22787 +       dput(a->h_path.dentry);
22788 +       if (!rerr)
22789 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22790 +       else
22791 +               RevertFailure("rename %pd", a->h_dst);
22792 +}
22793 +
22794 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22795 +{
22796 +       int rerr;
22797 +
22798 +       a->h_path.dentry = a->src_wh_dentry;
22799 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22800 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22801 +       if (rerr)
22802 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22803 +}
22804 +#undef RevertFailure
22805 +
22806 +/* ---------------------------------------------------------------------- */
22807 +
22808 +/*
22809 + * when we have to copyup the renaming entry, do it with the rename-target name
22810 + * in order to minimize the cost (the later actual rename is unnecessary).
22811 + * otherwise rename it on the target branch.
22812 + */
22813 +static int au_ren_or_cpup(struct au_ren_args *a)
22814 +{
22815 +       int err;
22816 +       struct dentry *d;
22817 +       struct inode *delegated;
22818 +
22819 +       d = a->src_dentry;
22820 +       if (au_dbtop(d) == a->btgt) {
22821 +               a->h_path.dentry = a->dst_h_dentry;
22822 +               AuDebugOn(au_dbtop(d) != a->btgt);
22823 +               delegated = NULL;
22824 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22825 +                                  a->dst_h_dir, &a->h_path, &delegated,
22826 +                                  a->flags);
22827 +               if (unlikely(err == -EWOULDBLOCK)) {
22828 +                       pr_warn("cannot retry for NFSv4 delegation"
22829 +                               " for an internal rename\n");
22830 +                       iput(delegated);
22831 +               }
22832 +       } else
22833 +               BUG();
22834 +
22835 +       if (!err && a->h_dst)
22836 +               /* it will be set to dinfo later */
22837 +               dget(a->h_dst);
22838 +
22839 +       return err;
22840 +}
22841 +
22842 +/* cf. aufs_rmdir() */
22843 +static int au_ren_del_whtmp(struct au_ren_args *a)
22844 +{
22845 +       int err;
22846 +       struct inode *dir;
22847 +
22848 +       dir = a->dst_dir;
22849 +       SiMustAnyLock(dir->i_sb);
22850 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22851 +                                    au_sbi(dir->i_sb)->si_dirwh)
22852 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22853 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22854 +               if (unlikely(err))
22855 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22856 +                               "ignored.\n", a->h_dst, err);
22857 +       } else {
22858 +               au_nhash_wh_free(&a->thargs->whlist);
22859 +               a->thargs->whlist = a->whlist;
22860 +               a->whlist.nh_num = 0;
22861 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22862 +               dput(a->h_dst);
22863 +               a->thargs = NULL;
22864 +       }
22865 +
22866 +       return 0;
22867 +}
22868 +
22869 +/* make it 'opaque' dir. */
22870 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22871 +{
22872 +       int err;
22873 +       struct dentry *d, *diropq;
22874 +#define src_or_dst(member) a->sd[idx].member
22875 +
22876 +       err = 0;
22877 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22878 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22879 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22880 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22881 +       diropq = au_diropq_create(d, a->btgt);
22882 +       au_hn_inode_unlock(src_or_dst(hinode));
22883 +       if (IS_ERR(diropq))
22884 +               err = PTR_ERR(diropq);
22885 +       else
22886 +               dput(diropq);
22887 +
22888 +#undef src_or_dst_
22889 +       return err;
22890 +}
22891 +
22892 +static int au_ren_diropq(struct au_ren_args *a)
22893 +{
22894 +       int err;
22895 +       unsigned char always;
22896 +       struct dentry *d;
22897 +
22898 +       err = 0;
22899 +       d = a->dst_dentry; /* already renamed on the branch */
22900 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22901 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22902 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22903 +           && a->btgt != au_dbdiropq(a->src_dentry)
22904 +           && (a->dst_wh_dentry
22905 +               || a->btgt <= au_dbdiropq(d)
22906 +               /* hide the lower to keep xino */
22907 +               /* the lowers may not be a dir, but we hide them anyway */
22908 +               || a->btgt < au_dbbot(d)
22909 +               || always)) {
22910 +               AuDbg("here\n");
22911 +               err = au_ren_do_diropq(a, AuSRC);
22912 +               if (unlikely(err))
22913 +                       goto out;
22914 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22915 +       }
22916 +       if (!a->exchange)
22917 +               goto out; /* success */
22918 +
22919 +       d = a->src_dentry; /* already renamed on the branch */
22920 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22921 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22922 +           && (a->btgt < au_dbdiropq(d)
22923 +               || a->btgt < au_dbbot(d)
22924 +               || always)) {
22925 +               AuDbgDentry(a->src_dentry);
22926 +               AuDbgDentry(a->dst_dentry);
22927 +               err = au_ren_do_diropq(a, AuDST);
22928 +               if (unlikely(err))
22929 +                       goto out_rev_src;
22930 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22931 +       }
22932 +       goto out; /* success */
22933 +
22934 +out_rev_src:
22935 +       AuDbg("err %d, reverting src\n", err);
22936 +       au_ren_rev_diropq(err, a);
22937 +out:
22938 +       return err;
22939 +}
22940 +
22941 +static int do_rename(struct au_ren_args *a)
22942 +{
22943 +       int err;
22944 +       struct dentry *d, *h_d;
22945 +
22946 +       if (!a->exchange) {
22947 +               /* prepare workqueue args for asynchronous rmdir */
22948 +               h_d = a->dst_h_dentry;
22949 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22950 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22951 +                   && d_is_positive(h_d)) {
22952 +                       err = -ENOMEM;
22953 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22954 +                                                        GFP_NOFS);
22955 +                       if (unlikely(!a->thargs))
22956 +                               goto out;
22957 +                       a->h_dst = dget(h_d);
22958 +               }
22959 +
22960 +               /* create whiteout for src_dentry */
22961 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22962 +                       a->src_bwh = au_dbwh(a->src_dentry);
22963 +                       AuDebugOn(a->src_bwh >= 0);
22964 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22965 +                                                       a->src_h_parent);
22966 +                       err = PTR_ERR(a->src_wh_dentry);
22967 +                       if (IS_ERR(a->src_wh_dentry))
22968 +                               goto out_thargs;
22969 +               }
22970 +
22971 +               /* lookup whiteout for dentry */
22972 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22973 +                       h_d = au_wh_lkup(a->dst_h_parent,
22974 +                                        &a->dst_dentry->d_name, a->br);
22975 +                       err = PTR_ERR(h_d);
22976 +                       if (IS_ERR(h_d))
22977 +                               goto out_whsrc;
22978 +                       if (d_is_negative(h_d))
22979 +                               dput(h_d);
22980 +                       else
22981 +                               a->dst_wh_dentry = h_d;
22982 +               }
22983 +
22984 +               /* rename dentry to tmpwh */
22985 +               if (a->thargs) {
22986 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22987 +                       if (unlikely(err))
22988 +                               goto out_whdst;
22989 +
22990 +                       d = a->dst_dentry;
22991 +                       au_set_h_dptr(d, a->btgt, NULL);
22992 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22993 +                       if (unlikely(err))
22994 +                               goto out_whtmp;
22995 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22996 +               }
22997 +       }
22998 +
22999 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
23000 +#if 0 /* debugging */
23001 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
23002 +              && d_is_positive(a->dst_h_dentry)
23003 +              && a->src_btop != a->btgt);
23004 +#endif
23005 +
23006 +       /* rename by vfs_rename or cpup */
23007 +       err = au_ren_or_cpup(a);
23008 +       if (unlikely(err))
23009 +               /* leave the copied-up one */
23010 +               goto out_whtmp;
23011 +
23012 +       /* make dir opaque */
23013 +       err = au_ren_diropq(a);
23014 +       if (unlikely(err))
23015 +               goto out_rename;
23016 +
23017 +       /* update target timestamps */
23018 +       if (a->exchange) {
23019 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
23020 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
23021 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23022 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23023 +       }
23024 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
23025 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
23026 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
23027 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23028 +
23029 +       if (!a->exchange) {
23030 +               /* remove whiteout for dentry */
23031 +               if (a->dst_wh_dentry) {
23032 +                       a->h_path.dentry = a->dst_wh_dentry;
23033 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
23034 +                                                 a->dst_dentry);
23035 +                       if (unlikely(err))
23036 +                               goto out_diropq;
23037 +               }
23038 +
23039 +               /* remove whtmp */
23040 +               if (a->thargs)
23041 +                       au_ren_del_whtmp(a); /* ignore this error */
23042 +
23043 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
23044 +       }
23045 +       err = 0;
23046 +       goto out_success;
23047 +
23048 +out_diropq:
23049 +       au_ren_rev_diropq(err, a);
23050 +out_rename:
23051 +       au_ren_rev_rename(err, a);
23052 +       dput(a->h_dst);
23053 +out_whtmp:
23054 +       if (a->thargs)
23055 +               au_ren_rev_whtmp(err, a);
23056 +out_whdst:
23057 +       dput(a->dst_wh_dentry);
23058 +       a->dst_wh_dentry = NULL;
23059 +out_whsrc:
23060 +       if (a->src_wh_dentry)
23061 +               au_ren_rev_whsrc(err, a);
23062 +out_success:
23063 +       dput(a->src_wh_dentry);
23064 +       dput(a->dst_wh_dentry);
23065 +out_thargs:
23066 +       if (a->thargs) {
23067 +               dput(a->h_dst);
23068 +               au_whtmp_rmdir_free(a->thargs);
23069 +               a->thargs = NULL;
23070 +       }
23071 +out:
23072 +       return err;
23073 +}
23074 +
23075 +/* ---------------------------------------------------------------------- */
23076 +
23077 +/*
23078 + * test if @dentry dir can be rename destination or not.
23079 + * success means, it is a logically empty dir.
23080 + */
23081 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23082 +{
23083 +       return au_test_empty(dentry, whlist);
23084 +}
23085 +
23086 +/*
23087 + * test if @a->src_dentry dir can be rename source or not.
23088 + * if it can, return 0.
23089 + * success means,
23090 + * - it is a logically empty dir.
23091 + * - or, it exists on writable branch and has no children including whiteouts
23092 + *   on the lower branch unless DIRREN is on.
23093 + */
23094 +static int may_rename_srcdir(struct au_ren_args *a)
23095 +{
23096 +       int err;
23097 +       unsigned int rdhash;
23098 +       aufs_bindex_t btop, btgt;
23099 +       struct dentry *dentry;
23100 +       struct super_block *sb;
23101 +       struct au_sbinfo *sbinfo;
23102 +
23103 +       dentry = a->src_dentry;
23104 +       sb = dentry->d_sb;
23105 +       sbinfo = au_sbi(sb);
23106 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23107 +               au_fset_ren(a->auren_flags, DIRREN);
23108 +
23109 +       btgt = a->btgt;
23110 +       btop = au_dbtop(dentry);
23111 +       if (btop != btgt) {
23112 +               struct au_nhash whlist;
23113 +
23114 +               SiMustAnyLock(sb);
23115 +               rdhash = sbinfo->si_rdhash;
23116 +               if (!rdhash)
23117 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23118 +                                                          dentry));
23119 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23120 +               if (unlikely(err))
23121 +                       goto out;
23122 +               err = au_test_empty(dentry, &whlist);
23123 +               au_nhash_wh_free(&whlist);
23124 +               goto out;
23125 +       }
23126 +
23127 +       if (btop == au_dbtaildir(dentry))
23128 +               return 0; /* success */
23129 +
23130 +       err = au_test_empty_lower(dentry);
23131 +
23132 +out:
23133 +       if (err == -ENOTEMPTY) {
23134 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23135 +                       err = 0;
23136 +               } else {
23137 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23138 +                               "branches, is not supported\n");
23139 +                       err = -EXDEV;
23140 +               }
23141 +       }
23142 +       return err;
23143 +}
23144 +
23145 +/* side effect: sets whlist and h_dentry */
23146 +static int au_ren_may_dir(struct au_ren_args *a)
23147 +{
23148 +       int err;
23149 +       unsigned int rdhash;
23150 +       struct dentry *d;
23151 +
23152 +       d = a->dst_dentry;
23153 +       SiMustAnyLock(d->d_sb);
23154 +
23155 +       err = 0;
23156 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23157 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23158 +               if (!rdhash)
23159 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23160 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23161 +               if (unlikely(err))
23162 +                       goto out;
23163 +
23164 +               if (!a->exchange) {
23165 +                       au_set_dbtop(d, a->dst_btop);
23166 +                       err = may_rename_dstdir(d, &a->whlist);
23167 +                       au_set_dbtop(d, a->btgt);
23168 +               } else
23169 +                       err = may_rename_srcdir(a);
23170 +       }
23171 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23172 +       if (unlikely(err))
23173 +               goto out;
23174 +
23175 +       d = a->src_dentry;
23176 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23177 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23178 +               err = may_rename_srcdir(a);
23179 +               if (unlikely(err)) {
23180 +                       au_nhash_wh_free(&a->whlist);
23181 +                       a->whlist.nh_num = 0;
23182 +               }
23183 +       }
23184 +out:
23185 +       return err;
23186 +}
23187 +
23188 +/* ---------------------------------------------------------------------- */
23189 +
23190 +/*
23191 + * simple tests for rename.
23192 + * following the checks in vfs, plus the parent-child relationship.
23193 + */
23194 +static int au_may_ren(struct au_ren_args *a)
23195 +{
23196 +       int err, isdir;
23197 +       struct inode *h_inode;
23198 +
23199 +       if (a->src_btop == a->btgt) {
23200 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23201 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23202 +               if (unlikely(err))
23203 +                       goto out;
23204 +               err = -EINVAL;
23205 +               if (unlikely(a->src_h_dentry == a->h_trap))
23206 +                       goto out;
23207 +       }
23208 +
23209 +       err = 0;
23210 +       if (a->dst_btop != a->btgt)
23211 +               goto out;
23212 +
23213 +       err = -ENOTEMPTY;
23214 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23215 +               goto out;
23216 +
23217 +       err = -EIO;
23218 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23219 +       if (d_really_is_negative(a->dst_dentry)) {
23220 +               if (d_is_negative(a->dst_h_dentry))
23221 +                       err = au_may_add(a->dst_dentry, a->btgt,
23222 +                                        a->dst_h_parent, isdir);
23223 +       } else {
23224 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23225 +                       goto out;
23226 +               h_inode = d_inode(a->dst_h_dentry);
23227 +               if (h_inode->i_nlink)
23228 +                       err = au_may_del(a->dst_dentry, a->btgt,
23229 +                                        a->dst_h_parent, isdir);
23230 +       }
23231 +
23232 +out:
23233 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23234 +               err = -EIO;
23235 +       AuTraceErr(err);
23236 +       return err;
23237 +}
23238 +
23239 +/* ---------------------------------------------------------------------- */
23240 +
23241 +/*
23242 + * locking order
23243 + * (VFS)
23244 + * - src_dir and dir by lock_rename()
23245 + * - inode if exists
23246 + * (aufs)
23247 + * - lock all
23248 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23249 + *     + si_read_lock
23250 + *     + di_write_lock2_child()
23251 + *       + di_write_lock_child()
23252 + *        + ii_write_lock_child()
23253 + *       + di_write_lock_child2()
23254 + *        + ii_write_lock_child2()
23255 + *     + src_parent and parent
23256 + *       + di_write_lock_parent()
23257 + *        + ii_write_lock_parent()
23258 + *       + di_write_lock_parent2()
23259 + *        + ii_write_lock_parent2()
23260 + *   + lower src_dir and dir by vfsub_lock_rename()
23261 + *   + verify the every relationships between child and parent. if any
23262 + *     of them failed, unlock all and return -EBUSY.
23263 + */
23264 +static void au_ren_unlock(struct au_ren_args *a)
23265 +{
23266 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23267 +                           a->dst_h_parent, a->dst_hdir);
23268 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23269 +           && a->h_root)
23270 +               au_hn_inode_unlock(a->h_root);
23271 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23272 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23273 +}
23274 +
23275 +static int au_ren_lock(struct au_ren_args *a)
23276 +{
23277 +       int err;
23278 +       unsigned int udba;
23279 +
23280 +       err = 0;
23281 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23282 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23283 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23284 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23285 +
23286 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23287 +       if (unlikely(err))
23288 +               goto out;
23289 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23290 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23291 +               struct dentry *root;
23292 +               struct inode *dir;
23293 +
23294 +               /*
23295 +                * sbinfo is already locked, so this ii_read_lock is
23296 +                * unnecessary. but our debugging feature checks it.
23297 +                */
23298 +               root = a->src_inode->i_sb->s_root;
23299 +               if (root != a->src_parent && root != a->dst_parent) {
23300 +                       dir = d_inode(root);
23301 +                       ii_read_lock_parent3(dir);
23302 +                       a->h_root = au_hi(dir, a->btgt);
23303 +                       ii_read_unlock(dir);
23304 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23305 +               }
23306 +       }
23307 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23308 +                                     a->dst_h_parent, a->dst_hdir);
23309 +       udba = au_opt_udba(a->src_dentry->d_sb);
23310 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23311 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23312 +               err = au_busy_or_stale();
23313 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23314 +               err = au_h_verify(a->src_h_dentry, udba,
23315 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23316 +                                 a->br);
23317 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23318 +               err = au_h_verify(a->dst_h_dentry, udba,
23319 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23320 +                                 a->br);
23321 +       if (!err)
23322 +               goto out; /* success */
23323 +
23324 +       err = au_busy_or_stale();
23325 +       au_ren_unlock(a);
23326 +
23327 +out:
23328 +       return err;
23329 +}
23330 +
23331 +/* ---------------------------------------------------------------------- */
23332 +
23333 +static void au_ren_refresh_dir(struct au_ren_args *a)
23334 +{
23335 +       struct inode *dir;
23336 +
23337 +       dir = a->dst_dir;
23338 +       inode_inc_iversion(dir);
23339 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23340 +               /* is this updating defined in POSIX? */
23341 +               au_cpup_attr_timesizes(a->src_inode);
23342 +               au_cpup_attr_nlink(dir, /*force*/1);
23343 +       }
23344 +       au_dir_ts(dir, a->btgt);
23345 +
23346 +       if (a->exchange) {
23347 +               dir = a->src_dir;
23348 +               inode_inc_iversion(dir);
23349 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23350 +                       /* is this updating defined in POSIX? */
23351 +                       au_cpup_attr_timesizes(a->dst_inode);
23352 +                       au_cpup_attr_nlink(dir, /*force*/1);
23353 +               }
23354 +               au_dir_ts(dir, a->btgt);
23355 +       }
23356 +
23357 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23358 +               return;
23359 +
23360 +       dir = a->src_dir;
23361 +       inode_inc_iversion(dir);
23362 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23363 +               au_cpup_attr_nlink(dir, /*force*/1);
23364 +       au_dir_ts(dir, a->btgt);
23365 +}
23366 +
23367 +static void au_ren_refresh(struct au_ren_args *a)
23368 +{
23369 +       aufs_bindex_t bbot, bindex;
23370 +       struct dentry *d, *h_d;
23371 +       struct inode *i, *h_i;
23372 +       struct super_block *sb;
23373 +
23374 +       d = a->dst_dentry;
23375 +       d_drop(d);
23376 +       if (a->h_dst)
23377 +               /* already dget-ed by au_ren_or_cpup() */
23378 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23379 +
23380 +       i = a->dst_inode;
23381 +       if (i) {
23382 +               if (!a->exchange) {
23383 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23384 +                               vfsub_drop_nlink(i);
23385 +                       else {
23386 +                               vfsub_dead_dir(i);
23387 +                               au_cpup_attr_timesizes(i);
23388 +                       }
23389 +                       au_update_dbrange(d, /*do_put_zero*/1);
23390 +               } else
23391 +                       au_cpup_attr_nlink(i, /*force*/1);
23392 +       } else {
23393 +               bbot = a->btgt;
23394 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23395 +                       au_set_h_dptr(d, bindex, NULL);
23396 +               bbot = au_dbbot(d);
23397 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23398 +                       au_set_h_dptr(d, bindex, NULL);
23399 +               au_update_dbrange(d, /*do_put_zero*/0);
23400 +       }
23401 +
23402 +       if (a->exchange
23403 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23404 +               d_drop(a->src_dentry);
23405 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23406 +                       au_set_dbwh(a->src_dentry, -1);
23407 +               return;
23408 +       }
23409 +
23410 +       d = a->src_dentry;
23411 +       au_set_dbwh(d, -1);
23412 +       bbot = au_dbbot(d);
23413 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23414 +               h_d = au_h_dptr(d, bindex);
23415 +               if (h_d)
23416 +                       au_set_h_dptr(d, bindex, NULL);
23417 +       }
23418 +       au_set_dbbot(d, a->btgt);
23419 +
23420 +       sb = d->d_sb;
23421 +       i = a->src_inode;
23422 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23423 +               return; /* success */
23424 +
23425 +       bbot = au_ibbot(i);
23426 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23427 +               h_i = au_h_iptr(i, bindex);
23428 +               if (h_i) {
23429 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23430 +                       /* ignore this error */
23431 +                       au_set_h_iptr(i, bindex, NULL, 0);
23432 +               }
23433 +       }
23434 +       au_set_ibbot(i, a->btgt);
23435 +}
23436 +
23437 +/* ---------------------------------------------------------------------- */
23438 +
23439 +/* mainly for link(2) and rename(2) */
23440 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23441 +{
23442 +       aufs_bindex_t bdiropq, bwh;
23443 +       struct dentry *parent;
23444 +       struct au_branch *br;
23445 +
23446 +       parent = dentry->d_parent;
23447 +       IMustLock(d_inode(parent)); /* dir is locked */
23448 +
23449 +       bdiropq = au_dbdiropq(parent);
23450 +       bwh = au_dbwh(dentry);
23451 +       br = au_sbr(dentry->d_sb, btgt);
23452 +       if (au_br_rdonly(br)
23453 +           || (0 <= bdiropq && bdiropq < btgt)
23454 +           || (0 <= bwh && bwh < btgt))
23455 +               btgt = -1;
23456 +
23457 +       AuDbg("btgt %d\n", btgt);
23458 +       return btgt;
23459 +}
23460 +
23461 +/* sets src_btop, dst_btop and btgt */
23462 +static int au_ren_wbr(struct au_ren_args *a)
23463 +{
23464 +       int err;
23465 +       struct au_wr_dir_args wr_dir_args = {
23466 +               /* .force_btgt  = -1, */
23467 +               .flags          = AuWrDir_ADD_ENTRY
23468 +       };
23469 +
23470 +       a->src_btop = au_dbtop(a->src_dentry);
23471 +       a->dst_btop = au_dbtop(a->dst_dentry);
23472 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23473 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23474 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23475 +       wr_dir_args.force_btgt = a->src_btop;
23476 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23477 +               wr_dir_args.force_btgt = a->dst_btop;
23478 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23479 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23480 +       a->btgt = err;
23481 +       if (a->exchange)
23482 +               au_update_dbtop(a->dst_dentry);
23483 +
23484 +       return err;
23485 +}
23486 +
23487 +static void au_ren_dt(struct au_ren_args *a)
23488 +{
23489 +       a->h_path.dentry = a->src_h_parent;
23490 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23491 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23492 +               a->h_path.dentry = a->dst_h_parent;
23493 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23494 +       }
23495 +
23496 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23497 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23498 +           && !a->exchange)
23499 +               return;
23500 +
23501 +       a->h_path.dentry = a->src_h_dentry;
23502 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23503 +       if (d_is_positive(a->dst_h_dentry)) {
23504 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23505 +               a->h_path.dentry = a->dst_h_dentry;
23506 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23507 +       }
23508 +}
23509 +
23510 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23511 +{
23512 +       struct dentry *h_d;
23513 +       struct inode *h_inode;
23514 +
23515 +       au_dtime_revert(a->src_dt + AuPARENT);
23516 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23517 +               au_dtime_revert(a->dst_dt + AuPARENT);
23518 +
23519 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23520 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23521 +               h_inode = d_inode(h_d);
23522 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23523 +               au_dtime_revert(a->src_dt + AuCHILD);
23524 +               inode_unlock(h_inode);
23525 +
23526 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23527 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23528 +                       h_inode = d_inode(h_d);
23529 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23530 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23531 +                       inode_unlock(h_inode);
23532 +               }
23533 +       }
23534 +}
23535 +
23536 +/* ---------------------------------------------------------------------- */
23537 +
23538 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23539 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23540 +               unsigned int _flags)
23541 +{
23542 +       int err, lock_flags;
23543 +       void *rev;
23544 +       /* reduce stack space */
23545 +       struct au_ren_args *a;
23546 +       struct au_pin pin;
23547 +
23548 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23549 +       IMustLock(_src_dir);
23550 +       IMustLock(_dst_dir);
23551 +
23552 +       err = -EINVAL;
23553 +       if (unlikely(_flags & RENAME_WHITEOUT))
23554 +               goto out;
23555 +
23556 +       err = -ENOMEM;
23557 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23558 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23559 +       if (unlikely(!a))
23560 +               goto out;
23561 +
23562 +       a->flags = _flags;
23563 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23564 +                    && RENAME_EXCHANGE > U8_MAX);
23565 +       a->exchange = _flags & RENAME_EXCHANGE;
23566 +       a->src_dir = _src_dir;
23567 +       a->src_dentry = _src_dentry;
23568 +       a->src_inode = NULL;
23569 +       if (d_really_is_positive(a->src_dentry))
23570 +               a->src_inode = d_inode(a->src_dentry);
23571 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23572 +       a->dst_dir = _dst_dir;
23573 +       a->dst_dentry = _dst_dentry;
23574 +       a->dst_inode = NULL;
23575 +       if (d_really_is_positive(a->dst_dentry))
23576 +               a->dst_inode = d_inode(a->dst_dentry);
23577 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23578 +       if (a->dst_inode) {
23579 +               /*
23580 +                * if EXCHANGE && src is non-dir && dst is dir,
23581 +                * dst is not locked.
23582 +                */
23583 +               /* IMustLock(a->dst_inode); */
23584 +               au_igrab(a->dst_inode);
23585 +       }
23586 +
23587 +       err = -ENOTDIR;
23588 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23589 +       if (d_is_dir(a->src_dentry)) {
23590 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23591 +               if (unlikely(!a->exchange
23592 +                            && d_really_is_positive(a->dst_dentry)
23593 +                            && !d_is_dir(a->dst_dentry)))
23594 +                       goto out_free;
23595 +               lock_flags |= AuLock_DIRS;
23596 +       }
23597 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23598 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23599 +               if (unlikely(!a->exchange
23600 +                            && d_really_is_positive(a->src_dentry)
23601 +                            && !d_is_dir(a->src_dentry)))
23602 +                       goto out_free;
23603 +               lock_flags |= AuLock_DIRS;
23604 +       }
23605 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23606 +                                       lock_flags);
23607 +       if (unlikely(err))
23608 +               goto out_free;
23609 +
23610 +       err = au_d_hashed_positive(a->src_dentry);
23611 +       if (unlikely(err))
23612 +               goto out_unlock;
23613 +       err = -ENOENT;
23614 +       if (a->dst_inode) {
23615 +               /*
23616 +                * If it is a dir, VFS unhash it before this
23617 +                * function. It means we cannot rely upon d_unhashed().
23618 +                */
23619 +               if (unlikely(!a->dst_inode->i_nlink))
23620 +                       goto out_unlock;
23621 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23622 +                       err = au_d_hashed_positive(a->dst_dentry);
23623 +                       if (unlikely(err && !a->exchange))
23624 +                               goto out_unlock;
23625 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23626 +                       goto out_unlock;
23627 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23628 +               goto out_unlock;
23629 +
23630 +       /*
23631 +        * is it possible?
23632 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23633 +        * there may exist a problem somewhere else.
23634 +        */
23635 +       err = -EINVAL;
23636 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23637 +               goto out_unlock;
23638 +
23639 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23640 +       di_write_lock_parent(a->dst_parent);
23641 +
23642 +       /* which branch we process */
23643 +       err = au_ren_wbr(a);
23644 +       if (unlikely(err < 0))
23645 +               goto out_parent;
23646 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23647 +       a->h_path.mnt = au_br_mnt(a->br);
23648 +
23649 +       /* are they available to be renamed */
23650 +       err = au_ren_may_dir(a);
23651 +       if (unlikely(err))
23652 +               goto out_children;
23653 +
23654 +       /* prepare the writable parent dir on the same branch */
23655 +       if (a->dst_btop == a->btgt) {
23656 +               au_fset_ren(a->auren_flags, WHDST);
23657 +       } else {
23658 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23659 +               if (unlikely(err))
23660 +                       goto out_children;
23661 +       }
23662 +
23663 +       err = 0;
23664 +       if (!a->exchange) {
23665 +               if (a->src_dir != a->dst_dir) {
23666 +                       /*
23667 +                        * this temporary unlock is safe,
23668 +                        * because both dir->i_mutex are locked.
23669 +                        */
23670 +                       di_write_unlock(a->dst_parent);
23671 +                       di_write_lock_parent(a->src_parent);
23672 +                       err = au_wr_dir_need_wh(a->src_dentry,
23673 +                                               au_ftest_ren(a->auren_flags,
23674 +                                                            ISDIR_SRC),
23675 +                                               &a->btgt);
23676 +                       di_write_unlock(a->src_parent);
23677 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23678 +                                             /*isdir*/1);
23679 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23680 +               } else
23681 +                       err = au_wr_dir_need_wh(a->src_dentry,
23682 +                                               au_ftest_ren(a->auren_flags,
23683 +                                                            ISDIR_SRC),
23684 +                                               &a->btgt);
23685 +       }
23686 +       if (unlikely(err < 0))
23687 +               goto out_children;
23688 +       if (err)
23689 +               au_fset_ren(a->auren_flags, WHSRC);
23690 +
23691 +       /* cpup src */
23692 +       if (a->src_btop != a->btgt) {
23693 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23694 +                            au_opt_udba(a->src_dentry->d_sb),
23695 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23696 +               if (!err) {
23697 +                       struct au_cp_generic cpg = {
23698 +                               .dentry = a->src_dentry,
23699 +                               .bdst   = a->btgt,
23700 +                               .bsrc   = a->src_btop,
23701 +                               .len    = -1,
23702 +                               .pin    = &pin,
23703 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23704 +                       };
23705 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23706 +                       err = au_sio_cpup_simple(&cpg);
23707 +                       au_unpin(&pin);
23708 +               }
23709 +               if (unlikely(err))
23710 +                       goto out_children;
23711 +               a->src_btop = a->btgt;
23712 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23713 +               if (!a->exchange)
23714 +                       au_fset_ren(a->auren_flags, WHSRC);
23715 +       }
23716 +
23717 +       /* cpup dst */
23718 +       if (a->exchange && a->dst_inode
23719 +           && a->dst_btop != a->btgt) {
23720 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23721 +                            au_opt_udba(a->dst_dentry->d_sb),
23722 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23723 +               if (!err) {
23724 +                       struct au_cp_generic cpg = {
23725 +                               .dentry = a->dst_dentry,
23726 +                               .bdst   = a->btgt,
23727 +                               .bsrc   = a->dst_btop,
23728 +                               .len    = -1,
23729 +                               .pin    = &pin,
23730 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23731 +                       };
23732 +                       err = au_sio_cpup_simple(&cpg);
23733 +                       au_unpin(&pin);
23734 +               }
23735 +               if (unlikely(err))
23736 +                       goto out_children;
23737 +               a->dst_btop = a->btgt;
23738 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23739 +       }
23740 +
23741 +       /* lock them all */
23742 +       err = au_ren_lock(a);
23743 +       if (unlikely(err))
23744 +               /* leave the copied-up one */
23745 +               goto out_children;
23746 +
23747 +       if (!a->exchange) {
23748 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23749 +                       err = au_may_ren(a);
23750 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23751 +                       err = -ENAMETOOLONG;
23752 +               if (unlikely(err))
23753 +                       goto out_hdir;
23754 +       }
23755 +
23756 +       /* store timestamps to be revertible */
23757 +       au_ren_dt(a);
23758 +
23759 +       /* store dirren info */
23760 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23761 +               err = au_dr_rename(a->src_dentry, a->btgt,
23762 +                                  &a->dst_dentry->d_name, &rev);
23763 +               AuTraceErr(err);
23764 +               if (unlikely(err))
23765 +                       goto out_dt;
23766 +       }
23767 +
23768 +       /* here we go */
23769 +       err = do_rename(a);
23770 +       if (unlikely(err))
23771 +               goto out_dirren;
23772 +
23773 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23774 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23775 +
23776 +       /* update dir attributes */
23777 +       au_ren_refresh_dir(a);
23778 +
23779 +       /* dput/iput all lower dentries */
23780 +       au_ren_refresh(a);
23781 +
23782 +       goto out_hdir; /* success */
23783 +
23784 +out_dirren:
23785 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23786 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23787 +out_dt:
23788 +       au_ren_rev_dt(err, a);
23789 +out_hdir:
23790 +       au_ren_unlock(a);
23791 +out_children:
23792 +       au_nhash_wh_free(&a->whlist);
23793 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23794 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23795 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23796 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23797 +       }
23798 +out_parent:
23799 +       if (!err) {
23800 +               if (d_unhashed(a->src_dentry))
23801 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23802 +               if (d_unhashed(a->dst_dentry))
23803 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23804 +               if (!a->exchange)
23805 +                       d_move(a->src_dentry, a->dst_dentry);
23806 +               else {
23807 +                       d_exchange(a->src_dentry, a->dst_dentry);
23808 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23809 +                               d_drop(a->dst_dentry);
23810 +               }
23811 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23812 +                       d_drop(a->src_dentry);
23813 +       } else {
23814 +               au_update_dbtop(a->dst_dentry);
23815 +               if (!a->dst_inode)
23816 +                       d_drop(a->dst_dentry);
23817 +       }
23818 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23819 +               di_write_unlock(a->dst_parent);
23820 +       else
23821 +               di_write_unlock2(a->src_parent, a->dst_parent);
23822 +out_unlock:
23823 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23824 +out_free:
23825 +       iput(a->dst_inode);
23826 +       if (a->thargs)
23827 +               au_whtmp_rmdir_free(a->thargs);
23828 +       au_kfree_rcu(a);
23829 +out:
23830 +       AuTraceErr(err);
23831 +       return err;
23832 +}
23833 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23834 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23835 +++ linux/fs/aufs/Kconfig       2019-07-11 15:42:14.458904362 +0200
23836 @@ -0,0 +1,199 @@
23837 +# SPDX-License-Identifier: GPL-2.0
23838 +config AUFS_FS
23839 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23840 +       help
23841 +       Aufs is a stackable unification filesystem such as Unionfs,
23842 +       which unifies several directories and provides a merged single
23843 +       directory.
23844 +       In the early days, aufs was entirely re-designed and
23845 +       re-implemented Unionfs Version 1.x series. Introducing many
23846 +       original ideas, approaches and improvements, it becomes totally
23847 +       different from Unionfs while keeping the basic features.
23848 +
23849 +if AUFS_FS
23850 +choice
23851 +       prompt "Maximum number of branches"
23852 +       default AUFS_BRANCH_MAX_127
23853 +       help
23854 +       Specifies the maximum number of branches (or member directories)
23855 +       in a single aufs. The larger value consumes more system
23856 +       resources and has a minor impact to performance.
23857 +config AUFS_BRANCH_MAX_127
23858 +       bool "127"
23859 +       help
23860 +       Specifies the maximum number of branches (or member directories)
23861 +       in a single aufs. The larger value consumes more system
23862 +       resources and has a minor impact to performance.
23863 +config AUFS_BRANCH_MAX_511
23864 +       bool "511"
23865 +       help
23866 +       Specifies the maximum number of branches (or member directories)
23867 +       in a single aufs. The larger value consumes more system
23868 +       resources and has a minor impact to performance.
23869 +config AUFS_BRANCH_MAX_1023
23870 +       bool "1023"
23871 +       help
23872 +       Specifies the maximum number of branches (or member directories)
23873 +       in a single aufs. The larger value consumes more system
23874 +       resources and has a minor impact to performance.
23875 +config AUFS_BRANCH_MAX_32767
23876 +       bool "32767"
23877 +       help
23878 +       Specifies the maximum number of branches (or member directories)
23879 +       in a single aufs. The larger value consumes more system
23880 +       resources and has a minor impact to performance.
23881 +endchoice
23882 +
23883 +config AUFS_SBILIST
23884 +       bool
23885 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23886 +       default y
23887 +       help
23888 +       Automatic configuration for internal use.
23889 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23890 +
23891 +config AUFS_HNOTIFY
23892 +       bool "Detect direct branch access (bypassing aufs)"
23893 +       help
23894 +       If you want to modify files on branches directly, eg. bypassing aufs,
23895 +       and want aufs to detect the changes of them fully, then enable this
23896 +       option and use 'udba=notify' mount option.
23897 +       Currently there is only one available configuration, "fsnotify".
23898 +       It will have a negative impact to the performance.
23899 +       See detail in aufs.5.
23900 +
23901 +choice
23902 +       prompt "method" if AUFS_HNOTIFY
23903 +       default AUFS_HFSNOTIFY
23904 +config AUFS_HFSNOTIFY
23905 +       bool "fsnotify"
23906 +       select FSNOTIFY
23907 +endchoice
23908 +
23909 +config AUFS_EXPORT
23910 +       bool "NFS-exportable aufs"
23911 +       depends on EXPORTFS
23912 +       help
23913 +       If you want to export your mounted aufs via NFS, then enable this
23914 +       option. There are several requirements for this configuration.
23915 +       See detail in aufs.5.
23916 +
23917 +config AUFS_INO_T_64
23918 +       bool
23919 +       depends on AUFS_EXPORT
23920 +       depends on 64BIT && !(ALPHA || S390)
23921 +       default y
23922 +       help
23923 +       Automatic configuration for internal use.
23924 +       /* typedef unsigned long/int __kernel_ino_t */
23925 +       /* alpha and s390x are int */
23926 +
23927 +config AUFS_XATTR
23928 +       bool "support for XATTR/EA (including Security Labels)"
23929 +       help
23930 +       If your branch fs supports XATTR/EA and you want to make them
23931 +       available in aufs too, then enable this opsion and specify the
23932 +       branch attributes for EA.
23933 +       See detail in aufs.5.
23934 +
23935 +config AUFS_FHSM
23936 +       bool "File-based Hierarchical Storage Management"
23937 +       help
23938 +       Hierarchical Storage Management (or HSM) is a well-known feature
23939 +       in the storage world. Aufs provides this feature as file-based.
23940 +       with multiple branches.
23941 +       These multiple branches are prioritized, ie. the topmost one
23942 +       should be the fastest drive and be used heavily.
23943 +
23944 +config AUFS_RDU
23945 +       bool "Readdir in userspace"
23946 +       help
23947 +       Aufs has two methods to provide a merged view for a directory,
23948 +       by a user-space library and by kernel-space natively. The latter
23949 +       is always enabled but sometimes large and slow.
23950 +       If you enable this option, install the library in aufs2-util
23951 +       package, and set some environment variables for your readdir(3),
23952 +       then the work will be handled in user-space which generally
23953 +       shows better performance in most cases.
23954 +       See detail in aufs.5.
23955 +
23956 +config AUFS_DIRREN
23957 +       bool "Workaround for rename(2)-ing a directory"
23958 +       help
23959 +       By default, aufs returns EXDEV error in renameing a dir who has
23960 +       his child on the lower branch, since it is a bad idea to issue
23961 +       rename(2) internally for every lower branch. But user may not
23962 +       accept this behaviour. So here is a workaround to allow such
23963 +       rename(2) and store some extra infromation on the writable
23964 +       branch. Obviously this costs high (and I don't like it).
23965 +       To use this feature, you need to enable this configuration AND
23966 +       to specify the mount option `dirren.'
23967 +       See details in aufs.5 and the design documents.
23968 +
23969 +config AUFS_SHWH
23970 +       bool "Show whiteouts"
23971 +       help
23972 +       If you want to make the whiteouts in aufs visible, then enable
23973 +       this option and specify 'shwh' mount option. Although it may
23974 +       sounds like philosophy or something, but in technically it
23975 +       simply shows the name of whiteout with keeping its behaviour.
23976 +
23977 +config AUFS_BR_RAMFS
23978 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23979 +       help
23980 +       If you want to use ramfs as an aufs branch fs, then enable this
23981 +       option. Generally tmpfs is recommended.
23982 +       Aufs prohibited them to be a branch fs by default, because
23983 +       initramfs becomes unusable after switch_root or something
23984 +       generally. If you sets initramfs as an aufs branch and boot your
23985 +       system by switch_root, you will meet a problem easily since the
23986 +       files in initramfs may be inaccessible.
23987 +       Unless you are going to use ramfs as an aufs branch fs without
23988 +       switch_root or something, leave it N.
23989 +
23990 +config AUFS_BR_FUSE
23991 +       bool "Fuse fs as an aufs branch"
23992 +       depends on FUSE_FS
23993 +       select AUFS_POLL
23994 +       help
23995 +       If you want to use fuse-based userspace filesystem as an aufs
23996 +       branch fs, then enable this option.
23997 +       It implements the internal poll(2) operation which is
23998 +       implemented by fuse only (curretnly).
23999 +
24000 +config AUFS_POLL
24001 +       bool
24002 +       help
24003 +       Automatic configuration for internal use.
24004 +
24005 +config AUFS_BR_HFSPLUS
24006 +       bool "Hfsplus as an aufs branch"
24007 +       depends on HFSPLUS_FS
24008 +       default y
24009 +       help
24010 +       If you want to use hfsplus fs as an aufs branch fs, then enable
24011 +       this option. This option introduces a small overhead at
24012 +       copying-up a file on hfsplus.
24013 +
24014 +config AUFS_BDEV_LOOP
24015 +       bool
24016 +       depends on BLK_DEV_LOOP
24017 +       default y
24018 +       help
24019 +       Automatic configuration for internal use.
24020 +       Convert =[ym] into =y.
24021 +
24022 +config AUFS_DEBUG
24023 +       bool "Debug aufs"
24024 +       help
24025 +       Enable this to compile aufs internal debug code.
24026 +       It will have a negative impact to the performance.
24027 +
24028 +config AUFS_MAGIC_SYSRQ
24029 +       bool
24030 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
24031 +       default y
24032 +       help
24033 +       Automatic configuration for internal use.
24034 +       When aufs supports Magic SysRq, enabled automatically.
24035 +endif
24036 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
24037 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
24038 +++ linux/fs/aufs/lcnt.h        2020-01-27 10:57:18.175538316 +0100
24039 @@ -0,0 +1,186 @@
24040 +/* SPDX-License-Identifier: GPL-2.0 */
24041 +/*
24042 + * Copyright (C) 2018-2020 Junjiro R. Okajima
24043 + *
24044 + * This program, aufs is free software; you can redistribute it and/or modify
24045 + * it under the terms of the GNU General Public License as published by
24046 + * the Free Software Foundation; either version 2 of the License, or
24047 + * (at your option) any later version.
24048 + *
24049 + * This program is distributed in the hope that it will be useful,
24050 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24051 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24052 + * GNU General Public License for more details.
24053 + *
24054 + * You should have received a copy of the GNU General Public License
24055 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24056 + */
24057 +
24058 +/*
24059 + * simple long counter wrapper
24060 + */
24061 +
24062 +#ifndef __AUFS_LCNT_H__
24063 +#define __AUFS_LCNT_H__
24064 +
24065 +#ifdef __KERNEL__
24066 +
24067 +#include "debug.h"
24068 +
24069 +#define AuLCntATOMIC   1
24070 +#define AuLCntPCPUCNT  2
24071 +/*
24072 + * why does percpu_refcount require extra synchronize_rcu()s in
24073 + * au_br_do_free()
24074 + */
24075 +#define AuLCntPCPUREF  3
24076 +
24077 +/* #define AuLCntChosen        AuLCntATOMIC */
24078 +#define AuLCntChosen   AuLCntPCPUCNT
24079 +/* #define AuLCntChosen        AuLCntPCPUREF */
24080 +
24081 +#if AuLCntChosen == AuLCntATOMIC
24082 +#include <linux/atomic.h>
24083 +
24084 +typedef atomic_long_t au_lcnt_t;
24085 +
24086 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24087 +{
24088 +       atomic_long_set(cnt, 0);
24089 +       return 0;
24090 +}
24091 +
24092 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24093 +{
24094 +       /* empty */
24095 +}
24096 +
24097 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24098 +                              int do_sync __maybe_unused)
24099 +{
24100 +       /* empty */
24101 +}
24102 +
24103 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24104 +{
24105 +       atomic_long_inc(cnt);
24106 +}
24107 +
24108 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24109 +{
24110 +       atomic_long_dec(cnt);
24111 +}
24112 +
24113 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24114 +{
24115 +       return atomic_long_read(cnt);
24116 +}
24117 +#endif
24118 +
24119 +#if AuLCntChosen == AuLCntPCPUCNT
24120 +#include <linux/percpu_counter.h>
24121 +
24122 +typedef struct percpu_counter au_lcnt_t;
24123 +
24124 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24125 +{
24126 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24127 +}
24128 +
24129 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24130 +{
24131 +       /* empty */
24132 +}
24133 +
24134 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24135 +{
24136 +       percpu_counter_destroy(cnt);
24137 +}
24138 +
24139 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24140 +{
24141 +       percpu_counter_inc(cnt);
24142 +}
24143 +
24144 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24145 +{
24146 +       percpu_counter_dec(cnt);
24147 +}
24148 +
24149 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24150 +{
24151 +       s64 n;
24152 +
24153 +       n = percpu_counter_sum(cnt);
24154 +       BUG_ON(n < 0);
24155 +       if (LONG_MAX != LLONG_MAX
24156 +           && n > LONG_MAX)
24157 +               AuWarn1("%s\n", "wrap-around");
24158 +
24159 +       return n;
24160 +}
24161 +#endif
24162 +
24163 +#if AuLCntChosen == AuLCntPCPUREF
24164 +#include <linux/percpu-refcount.h>
24165 +
24166 +typedef struct percpu_ref au_lcnt_t;
24167 +
24168 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24169 +{
24170 +       if (!release)
24171 +               release = percpu_ref_exit;
24172 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24173 +}
24174 +
24175 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24176 +{
24177 +       synchronize_rcu();
24178 +}
24179 +
24180 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24181 +{
24182 +       percpu_ref_kill(cnt);
24183 +       if (do_sync)
24184 +               au_lcnt_wait_for_fin(cnt);
24185 +}
24186 +
24187 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24188 +{
24189 +       percpu_ref_get(cnt);
24190 +}
24191 +
24192 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24193 +{
24194 +       percpu_ref_put(cnt);
24195 +}
24196 +
24197 +/*
24198 + * avoid calling this func as possible.
24199 + */
24200 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24201 +{
24202 +       long l;
24203 +
24204 +       percpu_ref_switch_to_atomic_sync(cnt);
24205 +       l = atomic_long_read(&cnt->count);
24206 +       if (do_rev)
24207 +               percpu_ref_switch_to_percpu(cnt);
24208 +
24209 +       /* percpu_ref is initialized by 1 instead of 0 */
24210 +       return l - 1;
24211 +}
24212 +#endif
24213 +
24214 +#ifdef CONFIG_AUFS_DEBUG
24215 +#define AuLCntZero(val) do {                   \
24216 +       long l = val;                           \
24217 +       if (l)                                  \
24218 +               AuDbg("%s = %ld\n", #val, l);   \
24219 +} while (0)
24220 +#else
24221 +#define AuLCntZero(val)                do {} while (0)
24222 +#endif
24223 +
24224 +#endif /* __KERNEL__ */
24225 +#endif /* __AUFS_LCNT_H__ */
24226 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24227 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24228 +++ linux/fs/aufs/loop.c        2020-01-27 10:57:18.175538316 +0100
24229 @@ -0,0 +1,148 @@
24230 +// SPDX-License-Identifier: GPL-2.0
24231 +/*
24232 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24233 + *
24234 + * This program, aufs is free software; you can redistribute it and/or modify
24235 + * it under the terms of the GNU General Public License as published by
24236 + * the Free Software Foundation; either version 2 of the License, or
24237 + * (at your option) any later version.
24238 + *
24239 + * This program is distributed in the hope that it will be useful,
24240 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24241 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24242 + * GNU General Public License for more details.
24243 + *
24244 + * You should have received a copy of the GNU General Public License
24245 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24246 + */
24247 +
24248 +/*
24249 + * support for loopback block device as a branch
24250 + */
24251 +
24252 +#include "aufs.h"
24253 +
24254 +/* added into drivers/block/loop.c */
24255 +static struct file *(*backing_file_func)(struct super_block *sb);
24256 +
24257 +/*
24258 + * test if two lower dentries have overlapping branches.
24259 + */
24260 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24261 +{
24262 +       struct super_block *h_sb;
24263 +       struct file *backing_file;
24264 +
24265 +       if (unlikely(!backing_file_func)) {
24266 +               /* don't load "loop" module here */
24267 +               backing_file_func = symbol_get(loop_backing_file);
24268 +               if (unlikely(!backing_file_func))
24269 +                       /* "loop" module is not loaded */
24270 +                       return 0;
24271 +       }
24272 +
24273 +       h_sb = h_adding->d_sb;
24274 +       backing_file = backing_file_func(h_sb);
24275 +       if (!backing_file)
24276 +               return 0;
24277 +
24278 +       h_adding = backing_file->f_path.dentry;
24279 +       /*
24280 +        * h_adding can be local NFS.
24281 +        * in this case aufs cannot detect the loop.
24282 +        */
24283 +       if (unlikely(h_adding->d_sb == sb))
24284 +               return 1;
24285 +       return !!au_test_subdir(h_adding, sb->s_root);
24286 +}
24287 +
24288 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24289 +int au_test_loopback_kthread(void)
24290 +{
24291 +       int ret;
24292 +       struct task_struct *tsk = current;
24293 +       char c, comm[sizeof(tsk->comm)];
24294 +
24295 +       ret = 0;
24296 +       if (tsk->flags & PF_KTHREAD) {
24297 +               get_task_comm(comm, tsk);
24298 +               c = comm[4];
24299 +               ret = ('0' <= c && c <= '9'
24300 +                      && !strncmp(comm, "loop", 4));
24301 +       }
24302 +
24303 +       return ret;
24304 +}
24305 +
24306 +/* ---------------------------------------------------------------------- */
24307 +
24308 +#define au_warn_loopback_step  16
24309 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24310 +static unsigned long *au_warn_loopback_array;
24311 +
24312 +void au_warn_loopback(struct super_block *h_sb)
24313 +{
24314 +       int i, new_nelem;
24315 +       unsigned long *a, magic;
24316 +       static DEFINE_SPINLOCK(spin);
24317 +
24318 +       magic = h_sb->s_magic;
24319 +       spin_lock(&spin);
24320 +       a = au_warn_loopback_array;
24321 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24322 +               if (a[i] == magic) {
24323 +                       spin_unlock(&spin);
24324 +                       return;
24325 +               }
24326 +
24327 +       /* h_sb is new to us, print it */
24328 +       if (i < au_warn_loopback_nelem) {
24329 +               a[i] = magic;
24330 +               goto pr;
24331 +       }
24332 +
24333 +       /* expand the array */
24334 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24335 +       a = au_kzrealloc(au_warn_loopback_array,
24336 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24337 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24338 +                        /*may_shrink*/0);
24339 +       if (a) {
24340 +               au_warn_loopback_nelem = new_nelem;
24341 +               au_warn_loopback_array = a;
24342 +               a[i] = magic;
24343 +               goto pr;
24344 +       }
24345 +
24346 +       spin_unlock(&spin);
24347 +       AuWarn1("realloc failed, ignored\n");
24348 +       return;
24349 +
24350 +pr:
24351 +       spin_unlock(&spin);
24352 +       pr_warn("you may want to try another patch for loopback file "
24353 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24354 +}
24355 +
24356 +int au_loopback_init(void)
24357 +{
24358 +       int err;
24359 +       struct super_block *sb __maybe_unused;
24360 +
24361 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24362 +
24363 +       err = 0;
24364 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24365 +                                        sizeof(unsigned long), GFP_NOFS);
24366 +       if (unlikely(!au_warn_loopback_array))
24367 +               err = -ENOMEM;
24368 +
24369 +       return err;
24370 +}
24371 +
24372 +void au_loopback_fin(void)
24373 +{
24374 +       if (backing_file_func)
24375 +               symbol_put(loop_backing_file);
24376 +       au_kfree_try_rcu(au_warn_loopback_array);
24377 +}
24378 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24379 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24380 +++ linux/fs/aufs/loop.h        2020-01-27 10:57:18.175538316 +0100
24381 @@ -0,0 +1,55 @@
24382 +/* SPDX-License-Identifier: GPL-2.0 */
24383 +/*
24384 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24385 + *
24386 + * This program, aufs is free software; you can redistribute it and/or modify
24387 + * it under the terms of the GNU General Public License as published by
24388 + * the Free Software Foundation; either version 2 of the License, or
24389 + * (at your option) any later version.
24390 + *
24391 + * This program is distributed in the hope that it will be useful,
24392 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24393 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24394 + * GNU General Public License for more details.
24395 + *
24396 + * You should have received a copy of the GNU General Public License
24397 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24398 + */
24399 +
24400 +/*
24401 + * support for loopback mount as a branch
24402 + */
24403 +
24404 +#ifndef __AUFS_LOOP_H__
24405 +#define __AUFS_LOOP_H__
24406 +
24407 +#ifdef __KERNEL__
24408 +
24409 +struct dentry;
24410 +struct super_block;
24411 +
24412 +#ifdef CONFIG_AUFS_BDEV_LOOP
24413 +/* drivers/block/loop.c */
24414 +struct file *loop_backing_file(struct super_block *sb);
24415 +
24416 +/* loop.c */
24417 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24418 +int au_test_loopback_kthread(void);
24419 +void au_warn_loopback(struct super_block *h_sb);
24420 +
24421 +int au_loopback_init(void);
24422 +void au_loopback_fin(void);
24423 +#else
24424 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24425 +
24426 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24427 +          struct dentry *h_adding)
24428 +AuStubInt0(au_test_loopback_kthread, void)
24429 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24430 +
24431 +AuStubInt0(au_loopback_init, void)
24432 +AuStubVoid(au_loopback_fin, void)
24433 +#endif /* BLK_DEV_LOOP */
24434 +
24435 +#endif /* __KERNEL__ */
24436 +#endif /* __AUFS_LOOP_H__ */
24437 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24438 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24439 +++ linux/fs/aufs/magic.mk      2019-07-11 15:42:14.468904634 +0200
24440 @@ -0,0 +1,31 @@
24441 +# SPDX-License-Identifier: GPL-2.0
24442 +
24443 +# defined in ${srctree}/fs/fuse/inode.c
24444 +# tristate
24445 +ifdef CONFIG_FUSE_FS
24446 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24447 +endif
24448 +
24449 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24450 +# tristate
24451 +ifdef CONFIG_XFS_FS
24452 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24453 +endif
24454 +
24455 +# defined in ${srctree}/fs/configfs/mount.c
24456 +# tristate
24457 +ifdef CONFIG_CONFIGFS_FS
24458 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24459 +endif
24460 +
24461 +# defined in ${srctree}/fs/ubifs/ubifs.h
24462 +# tristate
24463 +ifdef CONFIG_UBIFS_FS
24464 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24465 +endif
24466 +
24467 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24468 +# tristate
24469 +ifdef CONFIG_HFSPLUS_FS
24470 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24471 +endif
24472 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24473 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24474 +++ linux/fs/aufs/Makefile      2019-07-11 15:42:14.462237786 +0200
24475 @@ -0,0 +1,46 @@
24476 +# SPDX-License-Identifier: GPL-2.0
24477 +
24478 +include ${src}/magic.mk
24479 +ifeq (${CONFIG_AUFS_FS},m)
24480 +include ${src}/conf.mk
24481 +endif
24482 +-include ${src}/priv_def.mk
24483 +
24484 +# cf. include/linux/kernel.h
24485 +# enable pr_debug
24486 +ccflags-y += -DDEBUG
24487 +# sparse requires the full pathname
24488 +ifdef M
24489 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24490 +else
24491 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24492 +endif
24493 +
24494 +obj-$(CONFIG_AUFS_FS) += aufs.o
24495 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24496 +       wkq.o vfsub.o dcsub.o \
24497 +       cpup.o whout.o wbr_policy.o \
24498 +       dinfo.o dentry.o \
24499 +       dynop.o \
24500 +       finfo.o file.o f_op.o \
24501 +       dir.o vdir.o \
24502 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24503 +       mvdown.o ioctl.o
24504 +
24505 +# all are boolean
24506 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24507 +aufs-$(CONFIG_SYSFS) += sysfs.o
24508 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24509 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24510 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24511 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24512 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24513 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24514 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24515 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24516 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24517 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24518 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24519 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24520 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24521 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24522 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24523 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24524 +++ linux/fs/aufs/module.c      2020-01-27 10:57:18.175538316 +0100
24525 @@ -0,0 +1,273 @@
24526 +// SPDX-License-Identifier: GPL-2.0
24527 +/*
24528 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24529 + *
24530 + * This program, aufs is free software; you can redistribute it and/or modify
24531 + * it under the terms of the GNU General Public License as published by
24532 + * the Free Software Foundation; either version 2 of the License, or
24533 + * (at your option) any later version.
24534 + *
24535 + * This program is distributed in the hope that it will be useful,
24536 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24537 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24538 + * GNU General Public License for more details.
24539 + *
24540 + * You should have received a copy of the GNU General Public License
24541 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24542 + */
24543 +
24544 +/*
24545 + * module global variables and operations
24546 + */
24547 +
24548 +#include <linux/module.h>
24549 +#include <linux/seq_file.h>
24550 +#include "aufs.h"
24551 +
24552 +/* shrinkable realloc */
24553 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24554 +{
24555 +       size_t sz;
24556 +       int diff;
24557 +
24558 +       sz = 0;
24559 +       diff = -1;
24560 +       if (p) {
24561 +#if 0 /* unused */
24562 +               if (!new_sz) {
24563 +                       au_kfree_rcu(p);
24564 +                       p = NULL;
24565 +                       goto out;
24566 +               }
24567 +#else
24568 +               AuDebugOn(!new_sz);
24569 +#endif
24570 +               sz = ksize(p);
24571 +               diff = au_kmidx_sub(sz, new_sz);
24572 +       }
24573 +       if (sz && !diff)
24574 +               goto out;
24575 +
24576 +       if (sz < new_sz)
24577 +               /* expand or SLOB */
24578 +               p = krealloc(p, new_sz, gfp);
24579 +       else if (new_sz < sz && may_shrink) {
24580 +               /* shrink */
24581 +               void *q;
24582 +
24583 +               q = kmalloc(new_sz, gfp);
24584 +               if (q) {
24585 +                       if (p) {
24586 +                               memcpy(q, p, new_sz);
24587 +                               au_kfree_try_rcu(p);
24588 +                       }
24589 +                       p = q;
24590 +               } else
24591 +                       p = NULL;
24592 +       }
24593 +
24594 +out:
24595 +       return p;
24596 +}
24597 +
24598 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24599 +                  int may_shrink)
24600 +{
24601 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24602 +       if (p && new_sz > nused)
24603 +               memset(p + nused, 0, new_sz - nused);
24604 +       return p;
24605 +}
24606 +
24607 +/* ---------------------------------------------------------------------- */
24608 +/*
24609 + * aufs caches
24610 + */
24611 +struct kmem_cache *au_cache[AuCache_Last];
24612 +
24613 +static void au_cache_fin(void)
24614 +{
24615 +       int i;
24616 +
24617 +       /*
24618 +        * Make sure all delayed rcu free inodes are flushed before we
24619 +        * destroy cache.
24620 +        */
24621 +       rcu_barrier();
24622 +
24623 +       /* excluding AuCache_HNOTIFY */
24624 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24625 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24626 +               kmem_cache_destroy(au_cache[i]);
24627 +               au_cache[i] = NULL;
24628 +       }
24629 +}
24630 +
24631 +static int __init au_cache_init(void)
24632 +{
24633 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24634 +       if (au_cache[AuCache_DINFO])
24635 +               /* SLAB_DESTROY_BY_RCU */
24636 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24637 +                                                      au_icntnr_init_once);
24638 +       if (au_cache[AuCache_ICNTNR])
24639 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24640 +                                                     au_fi_init_once);
24641 +       if (au_cache[AuCache_FINFO])
24642 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24643 +       if (au_cache[AuCache_VDIR])
24644 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24645 +       if (au_cache[AuCache_DEHSTR])
24646 +               return 0;
24647 +
24648 +       au_cache_fin();
24649 +       return -ENOMEM;
24650 +}
24651 +
24652 +/* ---------------------------------------------------------------------- */
24653 +
24654 +int au_dir_roflags;
24655 +
24656 +#ifdef CONFIG_AUFS_SBILIST
24657 +/*
24658 + * iterate_supers_type() doesn't protect us from
24659 + * remounting (branch management)
24660 + */
24661 +struct hlist_bl_head au_sbilist;
24662 +#endif
24663 +
24664 +/*
24665 + * functions for module interface.
24666 + */
24667 +MODULE_LICENSE("GPL");
24668 +/* MODULE_LICENSE("GPL v2"); */
24669 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24670 +MODULE_DESCRIPTION(AUFS_NAME
24671 +       " -- Advanced multi layered unification filesystem");
24672 +MODULE_VERSION(AUFS_VERSION);
24673 +MODULE_ALIAS_FS(AUFS_NAME);
24674 +
24675 +/* this module parameter has no meaning when SYSFS is disabled */
24676 +int sysaufs_brs = 1;
24677 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24678 +module_param_named(brs, sysaufs_brs, int, 0444);
24679 +
24680 +/* this module parameter has no meaning when USER_NS is disabled */
24681 +bool au_userns;
24682 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24683 +module_param_named(allow_userns, au_userns, bool, 0444);
24684 +
24685 +/* ---------------------------------------------------------------------- */
24686 +
24687 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24688 +
24689 +int au_seq_path(struct seq_file *seq, struct path *path)
24690 +{
24691 +       int err;
24692 +
24693 +       err = seq_path(seq, path, au_esc_chars);
24694 +       if (err >= 0)
24695 +               err = 0;
24696 +       else
24697 +               err = -ENOMEM;
24698 +
24699 +       return err;
24700 +}
24701 +
24702 +/* ---------------------------------------------------------------------- */
24703 +
24704 +static int __init aufs_init(void)
24705 +{
24706 +       int err, i;
24707 +       char *p;
24708 +
24709 +       p = au_esc_chars;
24710 +       for (i = 1; i <= ' '; i++)
24711 +               *p++ = i;
24712 +       *p++ = '\\';
24713 +       *p++ = '\x7f';
24714 +       *p = 0;
24715 +
24716 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24717 +
24718 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24719 +       for (i = 0; i < AuIop_Last; i++)
24720 +               aufs_iop_nogetattr[i].getattr = NULL;
24721 +
24722 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24723 +
24724 +       au_sbilist_init();
24725 +       sysaufs_brs_init();
24726 +       au_debug_init();
24727 +       au_dy_init();
24728 +       err = sysaufs_init();
24729 +       if (unlikely(err))
24730 +               goto out;
24731 +       err = dbgaufs_init();
24732 +       if (unlikely(err))
24733 +               goto out_sysaufs;
24734 +       err = au_procfs_init();
24735 +       if (unlikely(err))
24736 +               goto out_dbgaufs;
24737 +       err = au_wkq_init();
24738 +       if (unlikely(err))
24739 +               goto out_procfs;
24740 +       err = au_loopback_init();
24741 +       if (unlikely(err))
24742 +               goto out_wkq;
24743 +       err = au_hnotify_init();
24744 +       if (unlikely(err))
24745 +               goto out_loopback;
24746 +       err = au_sysrq_init();
24747 +       if (unlikely(err))
24748 +               goto out_hin;
24749 +       err = au_cache_init();
24750 +       if (unlikely(err))
24751 +               goto out_sysrq;
24752 +
24753 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24754 +       err = register_filesystem(&aufs_fs_type);
24755 +       if (unlikely(err))
24756 +               goto out_cache;
24757 +
24758 +       /* since we define pr_fmt, call printk directly */
24759 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24760 +       goto out; /* success */
24761 +
24762 +out_cache:
24763 +       au_cache_fin();
24764 +out_sysrq:
24765 +       au_sysrq_fin();
24766 +out_hin:
24767 +       au_hnotify_fin();
24768 +out_loopback:
24769 +       au_loopback_fin();
24770 +out_wkq:
24771 +       au_wkq_fin();
24772 +out_procfs:
24773 +       au_procfs_fin();
24774 +out_dbgaufs:
24775 +       dbgaufs_fin();
24776 +out_sysaufs:
24777 +       sysaufs_fin();
24778 +       au_dy_fin();
24779 +out:
24780 +       return err;
24781 +}
24782 +
24783 +static void __exit aufs_exit(void)
24784 +{
24785 +       unregister_filesystem(&aufs_fs_type);
24786 +       au_cache_fin();
24787 +       au_sysrq_fin();
24788 +       au_hnotify_fin();
24789 +       au_loopback_fin();
24790 +       au_wkq_fin();
24791 +       au_procfs_fin();
24792 +       dbgaufs_fin();
24793 +       sysaufs_fin();
24794 +       au_dy_fin();
24795 +}
24796 +
24797 +module_init(aufs_init);
24798 +module_exit(aufs_exit);
24799 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24800 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24801 +++ linux/fs/aufs/module.h      2020-01-27 10:57:18.175538316 +0100
24802 @@ -0,0 +1,166 @@
24803 +/* SPDX-License-Identifier: GPL-2.0 */
24804 +/*
24805 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24806 + *
24807 + * This program, aufs is free software; you can redistribute it and/or modify
24808 + * it under the terms of the GNU General Public License as published by
24809 + * the Free Software Foundation; either version 2 of the License, or
24810 + * (at your option) any later version.
24811 + *
24812 + * This program is distributed in the hope that it will be useful,
24813 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24814 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24815 + * GNU General Public License for more details.
24816 + *
24817 + * You should have received a copy of the GNU General Public License
24818 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24819 + */
24820 +
24821 +/*
24822 + * module initialization and module-global
24823 + */
24824 +
24825 +#ifndef __AUFS_MODULE_H__
24826 +#define __AUFS_MODULE_H__
24827 +
24828 +#ifdef __KERNEL__
24829 +
24830 +#include <linux/slab.h>
24831 +#include "debug.h"
24832 +#include "dentry.h"
24833 +#include "dir.h"
24834 +#include "file.h"
24835 +#include "inode.h"
24836 +
24837 +struct path;
24838 +struct seq_file;
24839 +
24840 +/* module parameters */
24841 +extern int sysaufs_brs;
24842 +extern bool au_userns;
24843 +
24844 +/* ---------------------------------------------------------------------- */
24845 +
24846 +extern int au_dir_roflags;
24847 +
24848 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24849 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24850 +                  int may_shrink);
24851 +
24852 +/*
24853 + * Comparing the size of the object with sizeof(struct rcu_head)
24854 + * case 1: object is always larger
24855 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24856 + * case 2: object is always smaller
24857 + *     --> au_kfree_small()
24858 + * case 3: object can be any size
24859 + *     --> au_kfree_try_rcu()
24860 + */
24861 +
24862 +static inline void au_kfree_do_rcu(const void *p)
24863 +{
24864 +       struct {
24865 +               struct rcu_head rcu;
24866 +       } *a = (void *)p;
24867 +
24868 +       kfree_rcu(a, rcu);
24869 +}
24870 +
24871 +#define au_kfree_rcu(_p) do {                                          \
24872 +               typeof(_p) p = (_p);                                    \
24873 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24874 +               if (p)                                                  \
24875 +                       au_kfree_do_rcu(p);                             \
24876 +       } while (0)
24877 +
24878 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24879 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24880 +
24881 +static inline void au_kfree_try_rcu(const void *p)
24882 +{
24883 +       if (!p)
24884 +               return;
24885 +       if (au_kfree_sz_test(p))
24886 +               au_kfree_do_rcu(p);
24887 +       else
24888 +               kfree(p);
24889 +}
24890 +
24891 +static inline void au_kfree_small(const void *p)
24892 +{
24893 +       if (!p)
24894 +               return;
24895 +       AuDebugOn(au_kfree_sz_test(p));
24896 +       kfree(p);
24897 +}
24898 +
24899 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24900 +{
24901 +#ifndef CONFIG_SLOB
24902 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24903 +#else
24904 +       return -1; /* SLOB is untested */
24905 +#endif
24906 +}
24907 +
24908 +int au_seq_path(struct seq_file *seq, struct path *path);
24909 +
24910 +#ifdef CONFIG_PROC_FS
24911 +/* procfs.c */
24912 +int __init au_procfs_init(void);
24913 +void au_procfs_fin(void);
24914 +#else
24915 +AuStubInt0(au_procfs_init, void);
24916 +AuStubVoid(au_procfs_fin, void);
24917 +#endif
24918 +
24919 +/* ---------------------------------------------------------------------- */
24920 +
24921 +/* kmem cache */
24922 +enum {
24923 +       AuCache_DINFO,
24924 +       AuCache_ICNTNR,
24925 +       AuCache_FINFO,
24926 +       AuCache_VDIR,
24927 +       AuCache_DEHSTR,
24928 +       AuCache_HNOTIFY, /* must be last */
24929 +       AuCache_Last
24930 +};
24931 +
24932 +extern struct kmem_cache *au_cache[AuCache_Last];
24933 +
24934 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24935 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24936 +#define AuCacheCtor(type, ctor)        \
24937 +       kmem_cache_create(#type, sizeof(struct type), \
24938 +                         __alignof__(struct type), AuCacheFlags, ctor)
24939 +
24940 +#define AuCacheFuncs(name, index)                                      \
24941 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24942 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24943 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24944 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24945 +                                                                       \
24946 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24947 +       { void *p = rcu;                                                \
24948 +               p -= offsetof(struct au_##name, rcu);                   \
24949 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24950 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24951 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24952 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24953 +                                                                       \
24954 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24955 +       { /* au_cache_free_##name##_norcu(p); */                        \
24956 +               au_cache_free_##name##_rcu(p); }
24957 +
24958 +AuCacheFuncs(dinfo, DINFO);
24959 +AuCacheFuncs(icntnr, ICNTNR);
24960 +AuCacheFuncs(finfo, FINFO);
24961 +AuCacheFuncs(vdir, VDIR);
24962 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24963 +#ifdef CONFIG_AUFS_HNOTIFY
24964 +AuCacheFuncs(hnotify, HNOTIFY);
24965 +#endif
24966 +
24967 +#endif /* __KERNEL__ */
24968 +#endif /* __AUFS_MODULE_H__ */
24969 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24970 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24971 +++ linux/fs/aufs/mvdown.c      2020-01-27 10:57:18.175538316 +0100
24972 @@ -0,0 +1,706 @@
24973 +// SPDX-License-Identifier: GPL-2.0
24974 +/*
24975 + * Copyright (C) 2011-2020 Junjiro R. Okajima
24976 + *
24977 + * This program, aufs is free software; you can redistribute it and/or modify
24978 + * it under the terms of the GNU General Public License as published by
24979 + * the Free Software Foundation; either version 2 of the License, or
24980 + * (at your option) any later version.
24981 + *
24982 + * This program is distributed in the hope that it will be useful,
24983 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24984 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24985 + * GNU General Public License for more details.
24986 + *
24987 + * You should have received a copy of the GNU General Public License
24988 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24989 + */
24990 +
24991 +/*
24992 + * move-down, opposite of copy-up
24993 + */
24994 +
24995 +#include "aufs.h"
24996 +
24997 +struct au_mvd_args {
24998 +       struct {
24999 +               struct super_block *h_sb;
25000 +               struct dentry *h_parent;
25001 +               struct au_hinode *hdir;
25002 +               struct inode *h_dir, *h_inode;
25003 +               struct au_pin pin;
25004 +       } info[AUFS_MVDOWN_NARRAY];
25005 +
25006 +       struct aufs_mvdown mvdown;
25007 +       struct dentry *dentry, *parent;
25008 +       struct inode *inode, *dir;
25009 +       struct super_block *sb;
25010 +       aufs_bindex_t bopq, bwh, bfound;
25011 +       unsigned char rename_lock;
25012 +};
25013 +
25014 +#define mvd_errno              mvdown.au_errno
25015 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
25016 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
25017 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
25018 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
25019 +
25020 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
25021 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
25022 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
25023 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
25024 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
25025 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
25026 +
25027 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
25028 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
25029 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
25030 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
25031 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
25032 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
25033 +
25034 +#define AU_MVD_PR(flag, ...) do {                      \
25035 +               if (flag)                               \
25036 +                       pr_err(__VA_ARGS__);            \
25037 +       } while (0)
25038 +
25039 +static int find_lower_writable(struct au_mvd_args *a)
25040 +{
25041 +       struct super_block *sb;
25042 +       aufs_bindex_t bindex, bbot;
25043 +       struct au_branch *br;
25044 +
25045 +       sb = a->sb;
25046 +       bindex = a->mvd_bsrc;
25047 +       bbot = au_sbbot(sb);
25048 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
25049 +               for (bindex++; bindex <= bbot; bindex++) {
25050 +                       br = au_sbr(sb, bindex);
25051 +                       if (au_br_fhsm(br->br_perm)
25052 +                           && !sb_rdonly(au_br_sb(br)))
25053 +                               return bindex;
25054 +               }
25055 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25056 +               for (bindex++; bindex <= bbot; bindex++) {
25057 +                       br = au_sbr(sb, bindex);
25058 +                       if (!au_br_rdonly(br))
25059 +                               return bindex;
25060 +               }
25061 +       else
25062 +               for (bindex++; bindex <= bbot; bindex++) {
25063 +                       br = au_sbr(sb, bindex);
25064 +                       if (!sb_rdonly(au_br_sb(br))) {
25065 +                               if (au_br_rdonly(br))
25066 +                                       a->mvdown.flags
25067 +                                               |= AUFS_MVDOWN_ROLOWER_R;
25068 +                               return bindex;
25069 +                       }
25070 +               }
25071 +
25072 +       return -1;
25073 +}
25074 +
25075 +/* make the parent dir on bdst */
25076 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25077 +{
25078 +       int err;
25079 +
25080 +       err = 0;
25081 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25082 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25083 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25084 +       a->mvd_h_dst_parent = NULL;
25085 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
25086 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25087 +       if (!a->mvd_h_dst_parent) {
25088 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25089 +               if (unlikely(err)) {
25090 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25091 +                       goto out;
25092 +               }
25093 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25094 +       }
25095 +
25096 +out:
25097 +       AuTraceErr(err);
25098 +       return err;
25099 +}
25100 +
25101 +/* lock them all */
25102 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25103 +{
25104 +       int err;
25105 +       struct dentry *h_trap;
25106 +
25107 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25108 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25109 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25110 +                    au_opt_udba(a->sb),
25111 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25112 +       AuTraceErr(err);
25113 +       if (unlikely(err)) {
25114 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25115 +               goto out;
25116 +       }
25117 +
25118 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25119 +               a->rename_lock = 0;
25120 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25121 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25122 +                           au_opt_udba(a->sb),
25123 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25124 +               err = au_do_pin(&a->mvd_pin_src);
25125 +               AuTraceErr(err);
25126 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25127 +               if (unlikely(err)) {
25128 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25129 +                       goto out_dst;
25130 +               }
25131 +               goto out; /* success */
25132 +       }
25133 +
25134 +       a->rename_lock = 1;
25135 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25136 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25137 +                    au_opt_udba(a->sb),
25138 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25139 +       AuTraceErr(err);
25140 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25141 +       if (unlikely(err)) {
25142 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25143 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25144 +               goto out_dst;
25145 +       }
25146 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25147 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25148 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25149 +       if (h_trap) {
25150 +               err = (h_trap != a->mvd_h_src_parent);
25151 +               if (err)
25152 +                       err = (h_trap != a->mvd_h_dst_parent);
25153 +       }
25154 +       BUG_ON(err); /* it should never happen */
25155 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25156 +               err = -EBUSY;
25157 +               AuTraceErr(err);
25158 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25159 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25160 +               au_pin_hdir_lock(&a->mvd_pin_src);
25161 +               au_unpin(&a->mvd_pin_src);
25162 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25163 +               goto out_dst;
25164 +       }
25165 +       goto out; /* success */
25166 +
25167 +out_dst:
25168 +       au_unpin(&a->mvd_pin_dst);
25169 +out:
25170 +       AuTraceErr(err);
25171 +       return err;
25172 +}
25173 +
25174 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25175 +{
25176 +       if (!a->rename_lock)
25177 +               au_unpin(&a->mvd_pin_src);
25178 +       else {
25179 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25180 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25181 +               au_pin_hdir_lock(&a->mvd_pin_src);
25182 +               au_unpin(&a->mvd_pin_src);
25183 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25184 +       }
25185 +       au_unpin(&a->mvd_pin_dst);
25186 +}
25187 +
25188 +/* copy-down the file */
25189 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25190 +{
25191 +       int err;
25192 +       struct au_cp_generic cpg = {
25193 +               .dentry = a->dentry,
25194 +               .bdst   = a->mvd_bdst,
25195 +               .bsrc   = a->mvd_bsrc,
25196 +               .len    = -1,
25197 +               .pin    = &a->mvd_pin_dst,
25198 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25199 +       };
25200 +
25201 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25202 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25203 +               au_fset_cpup(cpg.flags, OVERWRITE);
25204 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25205 +               au_fset_cpup(cpg.flags, RWDST);
25206 +       err = au_sio_cpdown_simple(&cpg);
25207 +       if (unlikely(err))
25208 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25209 +
25210 +       AuTraceErr(err);
25211 +       return err;
25212 +}
25213 +
25214 +/*
25215 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25216 + * were sleeping
25217 + */
25218 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25219 +{
25220 +       int err;
25221 +       struct path h_path;
25222 +       struct au_branch *br;
25223 +       struct inode *delegated;
25224 +
25225 +       br = au_sbr(a->sb, a->mvd_bdst);
25226 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25227 +       err = PTR_ERR(h_path.dentry);
25228 +       if (IS_ERR(h_path.dentry)) {
25229 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25230 +               goto out;
25231 +       }
25232 +
25233 +       err = 0;
25234 +       if (d_is_positive(h_path.dentry)) {
25235 +               h_path.mnt = au_br_mnt(br);
25236 +               delegated = NULL;
25237 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25238 +                                  &delegated, /*force*/0);
25239 +               if (unlikely(err == -EWOULDBLOCK)) {
25240 +                       pr_warn("cannot retry for NFSv4 delegation"
25241 +                               " for an internal unlink\n");
25242 +                       iput(delegated);
25243 +               }
25244 +               if (unlikely(err))
25245 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25246 +       }
25247 +       dput(h_path.dentry);
25248 +
25249 +out:
25250 +       AuTraceErr(err);
25251 +       return err;
25252 +}
25253 +
25254 +/*
25255 + * unlink the topmost h_dentry
25256 + */
25257 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25258 +{
25259 +       int err;
25260 +       struct path h_path;
25261 +       struct inode *delegated;
25262 +
25263 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25264 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25265 +       delegated = NULL;
25266 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25267 +       if (unlikely(err == -EWOULDBLOCK)) {
25268 +               pr_warn("cannot retry for NFSv4 delegation"
25269 +                       " for an internal unlink\n");
25270 +               iput(delegated);
25271 +       }
25272 +       if (unlikely(err))
25273 +               AU_MVD_PR(dmsg, "unlink failed\n");
25274 +
25275 +       AuTraceErr(err);
25276 +       return err;
25277 +}
25278 +
25279 +/* Since mvdown succeeded, we ignore an error of this function */
25280 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25281 +{
25282 +       int err;
25283 +       struct au_branch *br;
25284 +
25285 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25286 +       br = au_sbr(a->sb, a->mvd_bsrc);
25287 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25288 +       if (!err) {
25289 +               br = au_sbr(a->sb, a->mvd_bdst);
25290 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25291 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25292 +       }
25293 +       if (!err)
25294 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25295 +       else
25296 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25297 +}
25298 +
25299 +/*
25300 + * copy-down the file and unlink the bsrc file.
25301 + * - unlink the bdst whout if exist
25302 + * - copy-down the file (with whtmp name and rename)
25303 + * - unlink the bsrc file
25304 + */
25305 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25306 +{
25307 +       int err;
25308 +
25309 +       err = au_do_mkdir(dmsg, a);
25310 +       if (!err)
25311 +               err = au_do_lock(dmsg, a);
25312 +       if (unlikely(err))
25313 +               goto out;
25314 +
25315 +       /*
25316 +        * do not revert the activities we made on bdst since they should be
25317 +        * harmless in aufs.
25318 +        */
25319 +
25320 +       err = au_do_cpdown(dmsg, a);
25321 +       if (!err)
25322 +               err = au_do_unlink_wh(dmsg, a);
25323 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25324 +               err = au_do_unlink(dmsg, a);
25325 +       if (unlikely(err))
25326 +               goto out_unlock;
25327 +
25328 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25329 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25330 +       if (find_lower_writable(a) < 0)
25331 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25332 +
25333 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25334 +               au_do_stfs(dmsg, a);
25335 +
25336 +       /* maintain internal array */
25337 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25338 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25339 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25340 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25341 +               au_set_ibtop(a->inode, a->mvd_bdst);
25342 +       } else {
25343 +               /* hide the lower */
25344 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25345 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25346 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25347 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25348 +       }
25349 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25350 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25351 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25352 +               au_set_ibbot(a->inode, a->mvd_bdst);
25353 +
25354 +out_unlock:
25355 +       au_do_unlock(dmsg, a);
25356 +out:
25357 +       AuTraceErr(err);
25358 +       return err;
25359 +}
25360 +
25361 +/* ---------------------------------------------------------------------- */
25362 +
25363 +/* make sure the file is idle */
25364 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25365 +{
25366 +       int err, plinked;
25367 +
25368 +       err = 0;
25369 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25370 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25371 +           && au_dcount(a->dentry) == 1
25372 +           && atomic_read(&a->inode->i_count) == 1
25373 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25374 +           && (!plinked || !au_plink_test(a->inode))
25375 +           && a->inode->i_nlink == 1)
25376 +               goto out;
25377 +
25378 +       err = -EBUSY;
25379 +       AU_MVD_PR(dmsg,
25380 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25381 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25382 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25383 +                 a->mvd_h_src_inode->i_nlink,
25384 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25385 +
25386 +out:
25387 +       AuTraceErr(err);
25388 +       return err;
25389 +}
25390 +
25391 +/* make sure the parent dir is fine */
25392 +static int au_mvd_args_parent(const unsigned char dmsg,
25393 +                             struct au_mvd_args *a)
25394 +{
25395 +       int err;
25396 +       aufs_bindex_t bindex;
25397 +
25398 +       err = 0;
25399 +       if (unlikely(au_alive_dir(a->parent))) {
25400 +               err = -ENOENT;
25401 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25402 +               goto out;
25403 +       }
25404 +
25405 +       a->bopq = au_dbdiropq(a->parent);
25406 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25407 +       AuDbg("b%d\n", bindex);
25408 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25409 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25410 +               err = -EINVAL;
25411 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25412 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25413 +                         a->bopq, a->mvd_bdst);
25414 +       }
25415 +
25416 +out:
25417 +       AuTraceErr(err);
25418 +       return err;
25419 +}
25420 +
25421 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25422 +                                   struct au_mvd_args *a)
25423 +{
25424 +       int err;
25425 +       struct au_dinfo *dinfo, *tmp;
25426 +
25427 +       /* lookup the next lower positive entry */
25428 +       err = -ENOMEM;
25429 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25430 +       if (unlikely(!tmp))
25431 +               goto out;
25432 +
25433 +       a->bfound = -1;
25434 +       a->bwh = -1;
25435 +       dinfo = au_di(a->dentry);
25436 +       au_di_cp(tmp, dinfo);
25437 +       au_di_swap(tmp, dinfo);
25438 +
25439 +       /* returns the number of positive dentries */
25440 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25441 +                            /* AuLkup_IGNORE_PERM */ 0);
25442 +       if (!err)
25443 +               a->bwh = au_dbwh(a->dentry);
25444 +       else if (err > 0)
25445 +               a->bfound = au_dbtop(a->dentry);
25446 +
25447 +       au_di_swap(tmp, dinfo);
25448 +       au_rw_write_unlock(&tmp->di_rwsem);
25449 +       au_di_free(tmp);
25450 +       if (unlikely(err < 0))
25451 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25452 +
25453 +       /*
25454 +        * here, we have these cases.
25455 +        * bfound == -1
25456 +        *      no positive dentry under bsrc. there are more sub-cases.
25457 +        *      bwh < 0
25458 +        *              there no whiteout, we can safely move-down.
25459 +        *      bwh <= bsrc
25460 +        *              impossible
25461 +        *      bsrc < bwh && bwh < bdst
25462 +        *              there is a whiteout on RO branch. cannot proceed.
25463 +        *      bwh == bdst
25464 +        *              there is a whiteout on the RW target branch. it should
25465 +        *              be removed.
25466 +        *      bdst < bwh
25467 +        *              there is a whiteout somewhere unrelated branch.
25468 +        * -1 < bfound && bfound <= bsrc
25469 +        *      impossible.
25470 +        * bfound < bdst
25471 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25472 +        *      proceed.
25473 +        * bfound == bdst
25474 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25475 +        *      error.
25476 +        * bdst < bfound
25477 +        *      found, after we create the file on bdst, it will be hidden.
25478 +        */
25479 +
25480 +       AuDebugOn(a->bfound == -1
25481 +                 && a->bwh != -1
25482 +                 && a->bwh <= a->mvd_bsrc);
25483 +       AuDebugOn(-1 < a->bfound
25484 +                 && a->bfound <= a->mvd_bsrc);
25485 +
25486 +       err = -EINVAL;
25487 +       if (a->bfound == -1
25488 +           && a->mvd_bsrc < a->bwh
25489 +           && a->bwh != -1
25490 +           && a->bwh < a->mvd_bdst) {
25491 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25492 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25493 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25494 +               goto out;
25495 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25496 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25497 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25498 +                         a->mvd_bdst, a->bfound);
25499 +               goto out;
25500 +       }
25501 +
25502 +       err = 0; /* success */
25503 +
25504 +out:
25505 +       AuTraceErr(err);
25506 +       return err;
25507 +}
25508 +
25509 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25510 +{
25511 +       int err;
25512 +
25513 +       err = 0;
25514 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25515 +           && a->bfound == a->mvd_bdst)
25516 +               err = -EEXIST;
25517 +       AuTraceErr(err);
25518 +       return err;
25519 +}
25520 +
25521 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25522 +{
25523 +       int err;
25524 +       struct au_branch *br;
25525 +
25526 +       err = -EISDIR;
25527 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25528 +               goto out;
25529 +
25530 +       err = -EINVAL;
25531 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25532 +               a->mvd_bsrc = au_ibtop(a->inode);
25533 +       else {
25534 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25535 +               if (unlikely(a->mvd_bsrc < 0
25536 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25537 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25538 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25539 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25540 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25541 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25542 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25543 +                       AU_MVD_PR(dmsg, "no upper\n");
25544 +                       goto out;
25545 +               }
25546 +       }
25547 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25548 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25549 +               AU_MVD_PR(dmsg, "on the bottom\n");
25550 +               goto out;
25551 +       }
25552 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25553 +       br = au_sbr(a->sb, a->mvd_bsrc);
25554 +       err = au_br_rdonly(br);
25555 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25556 +               if (unlikely(err))
25557 +                       goto out;
25558 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25559 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25560 +               if (err)
25561 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25562 +               /* go on */
25563 +       } else
25564 +               goto out;
25565 +
25566 +       err = -EINVAL;
25567 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25568 +               a->mvd_bdst = find_lower_writable(a);
25569 +               if (unlikely(a->mvd_bdst < 0)) {
25570 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25571 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25572 +                       goto out;
25573 +               }
25574 +       } else {
25575 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25576 +               if (unlikely(a->mvd_bdst < 0
25577 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25578 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25579 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25580 +                       goto out;
25581 +               }
25582 +       }
25583 +
25584 +       err = au_mvd_args_busy(dmsg, a);
25585 +       if (!err)
25586 +               err = au_mvd_args_parent(dmsg, a);
25587 +       if (!err)
25588 +               err = au_mvd_args_intermediate(dmsg, a);
25589 +       if (!err)
25590 +               err = au_mvd_args_exist(dmsg, a);
25591 +       if (!err)
25592 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25593 +
25594 +out:
25595 +       AuTraceErr(err);
25596 +       return err;
25597 +}
25598 +
25599 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25600 +{
25601 +       int err, e;
25602 +       unsigned char dmsg;
25603 +       struct au_mvd_args *args;
25604 +       struct inode *inode;
25605 +
25606 +       inode = d_inode(dentry);
25607 +       err = -EPERM;
25608 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25609 +               goto out;
25610 +
25611 +       err = -ENOMEM;
25612 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25613 +       if (unlikely(!args))
25614 +               goto out;
25615 +
25616 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25617 +       if (!err)
25618 +               /* VERIFY_WRITE */
25619 +               err = !access_ok(uarg, sizeof(*uarg));
25620 +       if (unlikely(err)) {
25621 +               err = -EFAULT;
25622 +               AuTraceErr(err);
25623 +               goto out_free;
25624 +       }
25625 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25626 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25627 +       args->mvdown.au_errno = 0;
25628 +       args->dentry = dentry;
25629 +       args->inode = inode;
25630 +       args->sb = dentry->d_sb;
25631 +
25632 +       err = -ENOENT;
25633 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25634 +       args->parent = dget_parent(dentry);
25635 +       args->dir = d_inode(args->parent);
25636 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25637 +       dput(args->parent);
25638 +       if (unlikely(args->parent != dentry->d_parent)) {
25639 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25640 +               goto out_dir;
25641 +       }
25642 +
25643 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25644 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25645 +       if (unlikely(err))
25646 +               goto out_inode;
25647 +
25648 +       di_write_lock_parent(args->parent);
25649 +       err = au_mvd_args(dmsg, args);
25650 +       if (unlikely(err))
25651 +               goto out_parent;
25652 +
25653 +       err = au_do_mvdown(dmsg, args);
25654 +       if (unlikely(err))
25655 +               goto out_parent;
25656 +
25657 +       au_cpup_attr_timesizes(args->dir);
25658 +       au_cpup_attr_timesizes(inode);
25659 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25660 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25661 +       /* au_digen_dec(dentry); */
25662 +
25663 +out_parent:
25664 +       di_write_unlock(args->parent);
25665 +       aufs_read_unlock(dentry, AuLock_DW);
25666 +out_inode:
25667 +       inode_unlock(inode);
25668 +out_dir:
25669 +       inode_unlock(args->dir);
25670 +out_free:
25671 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25672 +       if (unlikely(e))
25673 +               err = -EFAULT;
25674 +       au_kfree_rcu(args);
25675 +out:
25676 +       AuTraceErr(err);
25677 +       return err;
25678 +}
25679 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25680 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25681 +++ linux/fs/aufs/opts.c        2020-01-27 10:57:18.175538316 +0100
25682 @@ -0,0 +1,1880 @@
25683 +// SPDX-License-Identifier: GPL-2.0
25684 +/*
25685 + * Copyright (C) 2005-2020 Junjiro R. Okajima
25686 + *
25687 + * This program, aufs is free software; you can redistribute it and/or modify
25688 + * it under the terms of the GNU General Public License as published by
25689 + * the Free Software Foundation; either version 2 of the License, or
25690 + * (at your option) any later version.
25691 + *
25692 + * This program is distributed in the hope that it will be useful,
25693 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25694 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25695 + * GNU General Public License for more details.
25696 + *
25697 + * You should have received a copy of the GNU General Public License
25698 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25699 + */
25700 +
25701 +/*
25702 + * mount options/flags
25703 + */
25704 +
25705 +#include <linux/namei.h>
25706 +#include <linux/types.h> /* a distribution requires */
25707 +#include <linux/parser.h>
25708 +#include "aufs.h"
25709 +
25710 +/* ---------------------------------------------------------------------- */
25711 +
25712 +enum {
25713 +       Opt_br,
25714 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25715 +       Opt_idel, Opt_imod,
25716 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25717 +       Opt_rdblk_def, Opt_rdhash_def,
25718 +       Opt_xino, Opt_noxino,
25719 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25720 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25721 +       Opt_trunc_xib, Opt_notrunc_xib,
25722 +       Opt_shwh, Opt_noshwh,
25723 +       Opt_plink, Opt_noplink, Opt_list_plink,
25724 +       Opt_udba,
25725 +       Opt_dio, Opt_nodio,
25726 +       Opt_diropq_a, Opt_diropq_w,
25727 +       Opt_warn_perm, Opt_nowarn_perm,
25728 +       Opt_wbr_copyup, Opt_wbr_create,
25729 +       Opt_fhsm_sec,
25730 +       Opt_verbose, Opt_noverbose,
25731 +       Opt_sum, Opt_nosum, Opt_wsum,
25732 +       Opt_dirperm1, Opt_nodirperm1,
25733 +       Opt_dirren, Opt_nodirren,
25734 +       Opt_acl, Opt_noacl,
25735 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25736 +};
25737 +
25738 +static match_table_t options = {
25739 +       {Opt_br, "br=%s"},
25740 +       {Opt_br, "br:%s"},
25741 +
25742 +       {Opt_add, "add=%d:%s"},
25743 +       {Opt_add, "add:%d:%s"},
25744 +       {Opt_add, "ins=%d:%s"},
25745 +       {Opt_add, "ins:%d:%s"},
25746 +       {Opt_append, "append=%s"},
25747 +       {Opt_append, "append:%s"},
25748 +       {Opt_prepend, "prepend=%s"},
25749 +       {Opt_prepend, "prepend:%s"},
25750 +
25751 +       {Opt_del, "del=%s"},
25752 +       {Opt_del, "del:%s"},
25753 +       /* {Opt_idel, "idel:%d"}, */
25754 +       {Opt_mod, "mod=%s"},
25755 +       {Opt_mod, "mod:%s"},
25756 +       /* {Opt_imod, "imod:%d:%s"}, */
25757 +
25758 +       {Opt_dirwh, "dirwh=%d"},
25759 +
25760 +       {Opt_xino, "xino=%s"},
25761 +       {Opt_noxino, "noxino"},
25762 +       {Opt_trunc_xino, "trunc_xino"},
25763 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25764 +       {Opt_notrunc_xino, "notrunc_xino"},
25765 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25766 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25767 +       /* {Opt_zxino, "zxino=%s"}, */
25768 +       {Opt_trunc_xib, "trunc_xib"},
25769 +       {Opt_notrunc_xib, "notrunc_xib"},
25770 +
25771 +#ifdef CONFIG_PROC_FS
25772 +       {Opt_plink, "plink"},
25773 +#else
25774 +       {Opt_ignore_silent, "plink"},
25775 +#endif
25776 +
25777 +       {Opt_noplink, "noplink"},
25778 +
25779 +#ifdef CONFIG_AUFS_DEBUG
25780 +       {Opt_list_plink, "list_plink"},
25781 +#endif
25782 +
25783 +       {Opt_udba, "udba=%s"},
25784 +
25785 +       {Opt_dio, "dio"},
25786 +       {Opt_nodio, "nodio"},
25787 +
25788 +#ifdef CONFIG_AUFS_DIRREN
25789 +       {Opt_dirren, "dirren"},
25790 +       {Opt_nodirren, "nodirren"},
25791 +#else
25792 +       {Opt_ignore, "dirren"},
25793 +       {Opt_ignore_silent, "nodirren"},
25794 +#endif
25795 +
25796 +#ifdef CONFIG_AUFS_FHSM
25797 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25798 +#else
25799 +       {Opt_ignore, "fhsm_sec=%d"},
25800 +#endif
25801 +
25802 +       {Opt_diropq_a, "diropq=always"},
25803 +       {Opt_diropq_a, "diropq=a"},
25804 +       {Opt_diropq_w, "diropq=whiteouted"},
25805 +       {Opt_diropq_w, "diropq=w"},
25806 +
25807 +       {Opt_warn_perm, "warn_perm"},
25808 +       {Opt_nowarn_perm, "nowarn_perm"},
25809 +
25810 +       /* keep them temporary */
25811 +       {Opt_ignore_silent, "nodlgt"},
25812 +       {Opt_ignore, "clean_plink"},
25813 +
25814 +#ifdef CONFIG_AUFS_SHWH
25815 +       {Opt_shwh, "shwh"},
25816 +#endif
25817 +       {Opt_noshwh, "noshwh"},
25818 +
25819 +       {Opt_dirperm1, "dirperm1"},
25820 +       {Opt_nodirperm1, "nodirperm1"},
25821 +
25822 +       {Opt_verbose, "verbose"},
25823 +       {Opt_verbose, "v"},
25824 +       {Opt_noverbose, "noverbose"},
25825 +       {Opt_noverbose, "quiet"},
25826 +       {Opt_noverbose, "q"},
25827 +       {Opt_noverbose, "silent"},
25828 +
25829 +       {Opt_sum, "sum"},
25830 +       {Opt_nosum, "nosum"},
25831 +       {Opt_wsum, "wsum"},
25832 +
25833 +       {Opt_rdcache, "rdcache=%d"},
25834 +       {Opt_rdblk, "rdblk=%d"},
25835 +       {Opt_rdblk_def, "rdblk=def"},
25836 +       {Opt_rdhash, "rdhash=%d"},
25837 +       {Opt_rdhash_def, "rdhash=def"},
25838 +
25839 +       {Opt_wbr_create, "create=%s"},
25840 +       {Opt_wbr_create, "create_policy=%s"},
25841 +       {Opt_wbr_copyup, "cpup=%s"},
25842 +       {Opt_wbr_copyup, "copyup=%s"},
25843 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25844 +
25845 +       /* generic VFS flag */
25846 +#ifdef CONFIG_FS_POSIX_ACL
25847 +       {Opt_acl, "acl"},
25848 +       {Opt_noacl, "noacl"},
25849 +#else
25850 +       {Opt_ignore, "acl"},
25851 +       {Opt_ignore_silent, "noacl"},
25852 +#endif
25853 +
25854 +       /* internal use for the scripts */
25855 +       {Opt_ignore_silent, "si=%s"},
25856 +
25857 +       {Opt_br, "dirs=%s"},
25858 +       {Opt_ignore, "debug=%d"},
25859 +       {Opt_ignore, "delete=whiteout"},
25860 +       {Opt_ignore, "delete=all"},
25861 +       {Opt_ignore, "imap=%s"},
25862 +
25863 +       /* temporary workaround, due to old mount(8)? */
25864 +       {Opt_ignore_silent, "relatime"},
25865 +
25866 +       {Opt_err, NULL}
25867 +};
25868 +
25869 +/* ---------------------------------------------------------------------- */
25870 +
25871 +static const char *au_parser_pattern(int val, match_table_t tbl)
25872 +{
25873 +       struct match_token *p;
25874 +
25875 +       p = tbl;
25876 +       while (p->pattern) {
25877 +               if (p->token == val)
25878 +                       return p->pattern;
25879 +               p++;
25880 +       }
25881 +       BUG();
25882 +       return "??";
25883 +}
25884 +
25885 +static const char *au_optstr(int *val, match_table_t tbl)
25886 +{
25887 +       struct match_token *p;
25888 +       int v;
25889 +
25890 +       v = *val;
25891 +       if (!v)
25892 +               goto out;
25893 +       p = tbl;
25894 +       while (p->pattern) {
25895 +               if (p->token
25896 +                   && (v & p->token) == p->token) {
25897 +                       *val &= ~p->token;
25898 +                       return p->pattern;
25899 +               }
25900 +               p++;
25901 +       }
25902 +
25903 +out:
25904 +       return NULL;
25905 +}
25906 +
25907 +/* ---------------------------------------------------------------------- */
25908 +
25909 +static match_table_t brperm = {
25910 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25911 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25912 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25913 +       {0, NULL}
25914 +};
25915 +
25916 +static match_table_t brattr = {
25917 +       /* general */
25918 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25919 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25920 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25921 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25922 +#ifdef CONFIG_AUFS_FHSM
25923 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25924 +#endif
25925 +#ifdef CONFIG_AUFS_XATTR
25926 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25927 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25928 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25929 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25930 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25931 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25932 +#endif
25933 +
25934 +       /* ro/rr branch */
25935 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25936 +
25937 +       /* rw branch */
25938 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25939 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25940 +
25941 +       {0, NULL}
25942 +};
25943 +
25944 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25945 +{
25946 +       int attr, v;
25947 +       char *p;
25948 +
25949 +       attr = 0;
25950 +       do {
25951 +               p = strchr(str, '+');
25952 +               if (p)
25953 +                       *p = 0;
25954 +               v = match_token(str, table, args);
25955 +               if (v) {
25956 +                       if (v & AuBrAttr_CMOO_Mask)
25957 +                               attr &= ~AuBrAttr_CMOO_Mask;
25958 +                       attr |= v;
25959 +               } else {
25960 +                       if (p)
25961 +                               *p = '+';
25962 +                       pr_warn("ignored branch attribute %s\n", str);
25963 +                       break;
25964 +               }
25965 +               if (p)
25966 +                       str = p + 1;
25967 +       } while (p);
25968 +
25969 +       return attr;
25970 +}
25971 +
25972 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25973 +{
25974 +       int sz;
25975 +       const char *p;
25976 +       char *q;
25977 +
25978 +       q = str->a;
25979 +       *q = 0;
25980 +       p = au_optstr(&perm, brattr);
25981 +       if (p) {
25982 +               sz = strlen(p);
25983 +               memcpy(q, p, sz + 1);
25984 +               q += sz;
25985 +       } else
25986 +               goto out;
25987 +
25988 +       do {
25989 +               p = au_optstr(&perm, brattr);
25990 +               if (p) {
25991 +                       *q++ = '+';
25992 +                       sz = strlen(p);
25993 +                       memcpy(q, p, sz + 1);
25994 +                       q += sz;
25995 +               }
25996 +       } while (p);
25997 +
25998 +out:
25999 +       return q - str->a;
26000 +}
26001 +
26002 +static int noinline_for_stack br_perm_val(char *perm)
26003 +{
26004 +       int val, bad, sz;
26005 +       char *p;
26006 +       substring_t args[MAX_OPT_ARGS];
26007 +       au_br_perm_str_t attr;
26008 +
26009 +       p = strchr(perm, '+');
26010 +       if (p)
26011 +               *p = 0;
26012 +       val = match_token(perm, brperm, args);
26013 +       if (!val) {
26014 +               if (p)
26015 +                       *p = '+';
26016 +               pr_warn("ignored branch permission %s\n", perm);
26017 +               val = AuBrPerm_RO;
26018 +               goto out;
26019 +       }
26020 +       if (!p)
26021 +               goto out;
26022 +
26023 +       val |= br_attr_val(p + 1, brattr, args);
26024 +
26025 +       bad = 0;
26026 +       switch (val & AuBrPerm_Mask) {
26027 +       case AuBrPerm_RO:
26028 +       case AuBrPerm_RR:
26029 +               bad = val & AuBrWAttr_Mask;
26030 +               val &= ~AuBrWAttr_Mask;
26031 +               break;
26032 +       case AuBrPerm_RW:
26033 +               bad = val & AuBrRAttr_Mask;
26034 +               val &= ~AuBrRAttr_Mask;
26035 +               break;
26036 +       }
26037 +
26038 +       /*
26039 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
26040 +        * does not treat it as an error, just warning.
26041 +        * this is a tiny guard for the user operation.
26042 +        */
26043 +       if (val & AuBrAttr_UNPIN) {
26044 +               bad |= AuBrAttr_UNPIN;
26045 +               val &= ~AuBrAttr_UNPIN;
26046 +       }
26047 +
26048 +       if (unlikely(bad)) {
26049 +               sz = au_do_optstr_br_attr(&attr, bad);
26050 +               AuDebugOn(!sz);
26051 +               pr_warn("ignored branch attribute %s\n", attr.a);
26052 +       }
26053 +
26054 +out:
26055 +       return val;
26056 +}
26057 +
26058 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26059 +{
26060 +       au_br_perm_str_t attr;
26061 +       const char *p;
26062 +       char *q;
26063 +       int sz;
26064 +
26065 +       q = str->a;
26066 +       p = au_optstr(&perm, brperm);
26067 +       AuDebugOn(!p || !*p);
26068 +       sz = strlen(p);
26069 +       memcpy(q, p, sz + 1);
26070 +       q += sz;
26071 +
26072 +       sz = au_do_optstr_br_attr(&attr, perm);
26073 +       if (sz) {
26074 +               *q++ = '+';
26075 +               memcpy(q, attr.a, sz + 1);
26076 +       }
26077 +
26078 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
26079 +}
26080 +
26081 +/* ---------------------------------------------------------------------- */
26082 +
26083 +static match_table_t udbalevel = {
26084 +       {AuOpt_UDBA_REVAL, "reval"},
26085 +       {AuOpt_UDBA_NONE, "none"},
26086 +#ifdef CONFIG_AUFS_HNOTIFY
26087 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26088 +#ifdef CONFIG_AUFS_HFSNOTIFY
26089 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26090 +#endif
26091 +#endif
26092 +       {-1, NULL}
26093 +};
26094 +
26095 +static int noinline_for_stack udba_val(char *str)
26096 +{
26097 +       substring_t args[MAX_OPT_ARGS];
26098 +
26099 +       return match_token(str, udbalevel, args);
26100 +}
26101 +
26102 +const char *au_optstr_udba(int udba)
26103 +{
26104 +       return au_parser_pattern(udba, udbalevel);
26105 +}
26106 +
26107 +/* ---------------------------------------------------------------------- */
26108 +
26109 +static match_table_t au_wbr_create_policy = {
26110 +       {AuWbrCreate_TDP, "tdp"},
26111 +       {AuWbrCreate_TDP, "top-down-parent"},
26112 +       {AuWbrCreate_RR, "rr"},
26113 +       {AuWbrCreate_RR, "round-robin"},
26114 +       {AuWbrCreate_MFS, "mfs"},
26115 +       {AuWbrCreate_MFS, "most-free-space"},
26116 +       {AuWbrCreate_MFSV, "mfs:%d"},
26117 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26118 +
26119 +       /* top-down regardless the parent, and then mfs */
26120 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26121 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26122 +
26123 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26124 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26125 +       {AuWbrCreate_PMFS, "pmfs"},
26126 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26127 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26128 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26129 +
26130 +       {-1, NULL}
26131 +};
26132 +
26133 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26134 +                           struct au_opt_wbr_create *create)
26135 +{
26136 +       int err;
26137 +       unsigned long long ull;
26138 +
26139 +       err = 0;
26140 +       if (!match_u64(arg, &ull))
26141 +               create->mfsrr_watermark = ull;
26142 +       else {
26143 +               pr_err("bad integer in %s\n", str);
26144 +               err = -EINVAL;
26145 +       }
26146 +
26147 +       return err;
26148 +}
26149 +
26150 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26151 +                         struct au_opt_wbr_create *create)
26152 +{
26153 +       int n, err;
26154 +
26155 +       err = 0;
26156 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26157 +               create->mfs_second = n;
26158 +       else {
26159 +               pr_err("bad integer in %s\n", str);
26160 +               err = -EINVAL;
26161 +       }
26162 +
26163 +       return err;
26164 +}
26165 +
26166 +static int noinline_for_stack
26167 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26168 +{
26169 +       int err, e;
26170 +       substring_t args[MAX_OPT_ARGS];
26171 +
26172 +       err = match_token(str, au_wbr_create_policy, args);
26173 +       create->wbr_create = err;
26174 +       switch (err) {
26175 +       case AuWbrCreate_MFSRRV:
26176 +       case AuWbrCreate_TDMFSV:
26177 +       case AuWbrCreate_PMFSRRV:
26178 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26179 +               if (!e)
26180 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26181 +               if (unlikely(e))
26182 +                       err = e;
26183 +               break;
26184 +       case AuWbrCreate_MFSRR:
26185 +       case AuWbrCreate_TDMFS:
26186 +       case AuWbrCreate_PMFSRR:
26187 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26188 +               if (unlikely(e)) {
26189 +                       err = e;
26190 +                       break;
26191 +               }
26192 +               /*FALLTHROUGH*/
26193 +       case AuWbrCreate_MFS:
26194 +       case AuWbrCreate_PMFS:
26195 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26196 +               break;
26197 +       case AuWbrCreate_MFSV:
26198 +       case AuWbrCreate_PMFSV:
26199 +               e = au_wbr_mfs_sec(&args[0], str, create);
26200 +               if (unlikely(e))
26201 +                       err = e;
26202 +               break;
26203 +       }
26204 +
26205 +       return err;
26206 +}
26207 +
26208 +const char *au_optstr_wbr_create(int wbr_create)
26209 +{
26210 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26211 +}
26212 +
26213 +static match_table_t au_wbr_copyup_policy = {
26214 +       {AuWbrCopyup_TDP, "tdp"},
26215 +       {AuWbrCopyup_TDP, "top-down-parent"},
26216 +       {AuWbrCopyup_BUP, "bup"},
26217 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26218 +       {AuWbrCopyup_BU, "bu"},
26219 +       {AuWbrCopyup_BU, "bottom-up"},
26220 +       {-1, NULL}
26221 +};
26222 +
26223 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26224 +{
26225 +       substring_t args[MAX_OPT_ARGS];
26226 +
26227 +       return match_token(str, au_wbr_copyup_policy, args);
26228 +}
26229 +
26230 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26231 +{
26232 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26233 +}
26234 +
26235 +/* ---------------------------------------------------------------------- */
26236 +
26237 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26238 +
26239 +static void dump_opts(struct au_opts *opts)
26240 +{
26241 +#ifdef CONFIG_AUFS_DEBUG
26242 +       /* reduce stack space */
26243 +       union {
26244 +               struct au_opt_add *add;
26245 +               struct au_opt_del *del;
26246 +               struct au_opt_mod *mod;
26247 +               struct au_opt_xino *xino;
26248 +               struct au_opt_xino_itrunc *xino_itrunc;
26249 +               struct au_opt_wbr_create *create;
26250 +       } u;
26251 +       struct au_opt *opt;
26252 +
26253 +       opt = opts->opt;
26254 +       while (opt->type != Opt_tail) {
26255 +               switch (opt->type) {
26256 +               case Opt_add:
26257 +                       u.add = &opt->add;
26258 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26259 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26260 +                                 u.add->path.dentry);
26261 +                       break;
26262 +               case Opt_del:
26263 +               case Opt_idel:
26264 +                       u.del = &opt->del;
26265 +                       AuDbg("del {%s, %p}\n",
26266 +                             u.del->pathname, u.del->h_path.dentry);
26267 +                       break;
26268 +               case Opt_mod:
26269 +               case Opt_imod:
26270 +                       u.mod = &opt->mod;
26271 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26272 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26273 +                       break;
26274 +               case Opt_append:
26275 +                       u.add = &opt->add;
26276 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26277 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26278 +                                 u.add->path.dentry);
26279 +                       break;
26280 +               case Opt_prepend:
26281 +                       u.add = &opt->add;
26282 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26283 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26284 +                                 u.add->path.dentry);
26285 +                       break;
26286 +               case Opt_dirwh:
26287 +                       AuDbg("dirwh %d\n", opt->dirwh);
26288 +                       break;
26289 +               case Opt_rdcache:
26290 +                       AuDbg("rdcache %d\n", opt->rdcache);
26291 +                       break;
26292 +               case Opt_rdblk:
26293 +                       AuDbg("rdblk %u\n", opt->rdblk);
26294 +                       break;
26295 +               case Opt_rdblk_def:
26296 +                       AuDbg("rdblk_def\n");
26297 +                       break;
26298 +               case Opt_rdhash:
26299 +                       AuDbg("rdhash %u\n", opt->rdhash);
26300 +                       break;
26301 +               case Opt_rdhash_def:
26302 +                       AuDbg("rdhash_def\n");
26303 +                       break;
26304 +               case Opt_xino:
26305 +                       u.xino = &opt->xino;
26306 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26307 +                       break;
26308 +               case Opt_trunc_xino:
26309 +                       AuLabel(trunc_xino);
26310 +                       break;
26311 +               case Opt_notrunc_xino:
26312 +                       AuLabel(notrunc_xino);
26313 +                       break;
26314 +               case Opt_trunc_xino_path:
26315 +               case Opt_itrunc_xino:
26316 +                       u.xino_itrunc = &opt->xino_itrunc;
26317 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26318 +                       break;
26319 +               case Opt_noxino:
26320 +                       AuLabel(noxino);
26321 +                       break;
26322 +               case Opt_trunc_xib:
26323 +                       AuLabel(trunc_xib);
26324 +                       break;
26325 +               case Opt_notrunc_xib:
26326 +                       AuLabel(notrunc_xib);
26327 +                       break;
26328 +               case Opt_shwh:
26329 +                       AuLabel(shwh);
26330 +                       break;
26331 +               case Opt_noshwh:
26332 +                       AuLabel(noshwh);
26333 +                       break;
26334 +               case Opt_dirperm1:
26335 +                       AuLabel(dirperm1);
26336 +                       break;
26337 +               case Opt_nodirperm1:
26338 +                       AuLabel(nodirperm1);
26339 +                       break;
26340 +               case Opt_plink:
26341 +                       AuLabel(plink);
26342 +                       break;
26343 +               case Opt_noplink:
26344 +                       AuLabel(noplink);
26345 +                       break;
26346 +               case Opt_list_plink:
26347 +                       AuLabel(list_plink);
26348 +                       break;
26349 +               case Opt_udba:
26350 +                       AuDbg("udba %d, %s\n",
26351 +                                 opt->udba, au_optstr_udba(opt->udba));
26352 +                       break;
26353 +               case Opt_dio:
26354 +                       AuLabel(dio);
26355 +                       break;
26356 +               case Opt_nodio:
26357 +                       AuLabel(nodio);
26358 +                       break;
26359 +               case Opt_diropq_a:
26360 +                       AuLabel(diropq_a);
26361 +                       break;
26362 +               case Opt_diropq_w:
26363 +                       AuLabel(diropq_w);
26364 +                       break;
26365 +               case Opt_warn_perm:
26366 +                       AuLabel(warn_perm);
26367 +                       break;
26368 +               case Opt_nowarn_perm:
26369 +                       AuLabel(nowarn_perm);
26370 +                       break;
26371 +               case Opt_verbose:
26372 +                       AuLabel(verbose);
26373 +                       break;
26374 +               case Opt_noverbose:
26375 +                       AuLabel(noverbose);
26376 +                       break;
26377 +               case Opt_sum:
26378 +                       AuLabel(sum);
26379 +                       break;
26380 +               case Opt_nosum:
26381 +                       AuLabel(nosum);
26382 +                       break;
26383 +               case Opt_wsum:
26384 +                       AuLabel(wsum);
26385 +                       break;
26386 +               case Opt_wbr_create:
26387 +                       u.create = &opt->wbr_create;
26388 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26389 +                                 au_optstr_wbr_create(u.create->wbr_create));
26390 +                       switch (u.create->wbr_create) {
26391 +                       case AuWbrCreate_MFSV:
26392 +                       case AuWbrCreate_PMFSV:
26393 +                               AuDbg("%d sec\n", u.create->mfs_second);
26394 +                               break;
26395 +                       case AuWbrCreate_MFSRR:
26396 +                       case AuWbrCreate_TDMFS:
26397 +                               AuDbg("%llu watermark\n",
26398 +                                         u.create->mfsrr_watermark);
26399 +                               break;
26400 +                       case AuWbrCreate_MFSRRV:
26401 +                       case AuWbrCreate_TDMFSV:
26402 +                       case AuWbrCreate_PMFSRRV:
26403 +                               AuDbg("%llu watermark, %d sec\n",
26404 +                                         u.create->mfsrr_watermark,
26405 +                                         u.create->mfs_second);
26406 +                               break;
26407 +                       }
26408 +                       break;
26409 +               case Opt_wbr_copyup:
26410 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26411 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26412 +                       break;
26413 +               case Opt_fhsm_sec:
26414 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26415 +                       break;
26416 +               case Opt_dirren:
26417 +                       AuLabel(dirren);
26418 +                       break;
26419 +               case Opt_nodirren:
26420 +                       AuLabel(nodirren);
26421 +                       break;
26422 +               case Opt_acl:
26423 +                       AuLabel(acl);
26424 +                       break;
26425 +               case Opt_noacl:
26426 +                       AuLabel(noacl);
26427 +                       break;
26428 +               default:
26429 +                       BUG();
26430 +               }
26431 +               opt++;
26432 +       }
26433 +#endif
26434 +}
26435 +
26436 +void au_opts_free(struct au_opts *opts)
26437 +{
26438 +       struct au_opt *opt;
26439 +
26440 +       opt = opts->opt;
26441 +       while (opt->type != Opt_tail) {
26442 +               switch (opt->type) {
26443 +               case Opt_add:
26444 +               case Opt_append:
26445 +               case Opt_prepend:
26446 +                       path_put(&opt->add.path);
26447 +                       break;
26448 +               case Opt_del:
26449 +               case Opt_idel:
26450 +                       path_put(&opt->del.h_path);
26451 +                       break;
26452 +               case Opt_mod:
26453 +               case Opt_imod:
26454 +                       dput(opt->mod.h_root);
26455 +                       break;
26456 +               case Opt_xino:
26457 +                       fput(opt->xino.file);
26458 +                       break;
26459 +               }
26460 +               opt++;
26461 +       }
26462 +}
26463 +
26464 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26465 +                  aufs_bindex_t bindex)
26466 +{
26467 +       int err;
26468 +       struct au_opt_add *add = &opt->add;
26469 +       char *p;
26470 +
26471 +       add->bindex = bindex;
26472 +       add->perm = AuBrPerm_RO;
26473 +       add->pathname = opt_str;
26474 +       p = strchr(opt_str, '=');
26475 +       if (p) {
26476 +               *p++ = 0;
26477 +               if (*p)
26478 +                       add->perm = br_perm_val(p);
26479 +       }
26480 +
26481 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26482 +       if (!err) {
26483 +               if (!p) {
26484 +                       add->perm = AuBrPerm_RO;
26485 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26486 +                               add->perm = AuBrPerm_RR;
26487 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26488 +                               add->perm = AuBrPerm_RW;
26489 +               }
26490 +               opt->type = Opt_add;
26491 +               goto out;
26492 +       }
26493 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26494 +       err = -EINVAL;
26495 +
26496 +out:
26497 +       return err;
26498 +}
26499 +
26500 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26501 +{
26502 +       int err;
26503 +
26504 +       del->pathname = args[0].from;
26505 +       AuDbg("del path %s\n", del->pathname);
26506 +
26507 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26508 +       if (unlikely(err))
26509 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26510 +
26511 +       return err;
26512 +}
26513 +
26514 +#if 0 /* reserved for future use */
26515 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26516 +                             struct au_opt_del *del, substring_t args[])
26517 +{
26518 +       int err;
26519 +       struct dentry *root;
26520 +
26521 +       err = -EINVAL;
26522 +       root = sb->s_root;
26523 +       aufs_read_lock(root, AuLock_FLUSH);
26524 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26525 +               pr_err("out of bounds, %d\n", bindex);
26526 +               goto out;
26527 +       }
26528 +
26529 +       err = 0;
26530 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26531 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26532 +
26533 +out:
26534 +       aufs_read_unlock(root, !AuLock_IR);
26535 +       return err;
26536 +}
26537 +#endif
26538 +
26539 +static int noinline_for_stack
26540 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26541 +{
26542 +       int err;
26543 +       struct path path;
26544 +       char *p;
26545 +
26546 +       err = -EINVAL;
26547 +       mod->path = args[0].from;
26548 +       p = strchr(mod->path, '=');
26549 +       if (unlikely(!p)) {
26550 +               pr_err("no permission %s\n", args[0].from);
26551 +               goto out;
26552 +       }
26553 +
26554 +       *p++ = 0;
26555 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26556 +       if (unlikely(err)) {
26557 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26558 +               goto out;
26559 +       }
26560 +
26561 +       mod->perm = br_perm_val(p);
26562 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26563 +       mod->h_root = dget(path.dentry);
26564 +       path_put(&path);
26565 +
26566 +out:
26567 +       return err;
26568 +}
26569 +
26570 +#if 0 /* reserved for future use */
26571 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26572 +                             struct au_opt_mod *mod, substring_t args[])
26573 +{
26574 +       int err;
26575 +       struct dentry *root;
26576 +
26577 +       err = -EINVAL;
26578 +       root = sb->s_root;
26579 +       aufs_read_lock(root, AuLock_FLUSH);
26580 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26581 +               pr_err("out of bounds, %d\n", bindex);
26582 +               goto out;
26583 +       }
26584 +
26585 +       err = 0;
26586 +       mod->perm = br_perm_val(args[1].from);
26587 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26588 +             mod->path, mod->perm, args[1].from);
26589 +       mod->h_root = dget(au_h_dptr(root, bindex));
26590 +
26591 +out:
26592 +       aufs_read_unlock(root, !AuLock_IR);
26593 +       return err;
26594 +}
26595 +#endif
26596 +
26597 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26598 +                             substring_t args[])
26599 +{
26600 +       int err;
26601 +       struct file *file;
26602 +
26603 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26604 +       err = PTR_ERR(file);
26605 +       if (IS_ERR(file))
26606 +               goto out;
26607 +
26608 +       err = -EINVAL;
26609 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26610 +               fput(file);
26611 +               pr_err("%s must be outside\n", args[0].from);
26612 +               goto out;
26613 +       }
26614 +
26615 +       err = 0;
26616 +       xino->file = file;
26617 +       xino->path = args[0].from;
26618 +
26619 +out:
26620 +       return err;
26621 +}
26622 +
26623 +static int noinline_for_stack
26624 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26625 +                              struct au_opt_xino_itrunc *xino_itrunc,
26626 +                              substring_t args[])
26627 +{
26628 +       int err;
26629 +       aufs_bindex_t bbot, bindex;
26630 +       struct path path;
26631 +       struct dentry *root;
26632 +
26633 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26634 +       if (unlikely(err)) {
26635 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26636 +               goto out;
26637 +       }
26638 +
26639 +       xino_itrunc->bindex = -1;
26640 +       root = sb->s_root;
26641 +       aufs_read_lock(root, AuLock_FLUSH);
26642 +       bbot = au_sbbot(sb);
26643 +       for (bindex = 0; bindex <= bbot; bindex++) {
26644 +               if (au_h_dptr(root, bindex) == path.dentry) {
26645 +                       xino_itrunc->bindex = bindex;
26646 +                       break;
26647 +               }
26648 +       }
26649 +       aufs_read_unlock(root, !AuLock_IR);
26650 +       path_put(&path);
26651 +
26652 +       if (unlikely(xino_itrunc->bindex < 0)) {
26653 +               pr_err("no such branch %s\n", args[0].from);
26654 +               err = -EINVAL;
26655 +       }
26656 +
26657 +out:
26658 +       return err;
26659 +}
26660 +
26661 +/* called without aufs lock */
26662 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26663 +{
26664 +       int err, n, token;
26665 +       aufs_bindex_t bindex;
26666 +       unsigned char skipped;
26667 +       struct dentry *root;
26668 +       struct au_opt *opt, *opt_tail;
26669 +       char *opt_str;
26670 +       /* reduce the stack space */
26671 +       union {
26672 +               struct au_opt_xino_itrunc *xino_itrunc;
26673 +               struct au_opt_wbr_create *create;
26674 +       } u;
26675 +       struct {
26676 +               substring_t args[MAX_OPT_ARGS];
26677 +       } *a;
26678 +
26679 +       err = -ENOMEM;
26680 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26681 +       if (unlikely(!a))
26682 +               goto out;
26683 +
26684 +       root = sb->s_root;
26685 +       err = 0;
26686 +       bindex = 0;
26687 +       opt = opts->opt;
26688 +       opt_tail = opt + opts->max_opt - 1;
26689 +       opt->type = Opt_tail;
26690 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26691 +               err = -EINVAL;
26692 +               skipped = 0;
26693 +               token = match_token(opt_str, options, a->args);
26694 +               switch (token) {
26695 +               case Opt_br:
26696 +                       err = 0;
26697 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26698 +                              && *opt_str) {
26699 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26700 +                                             bindex++);
26701 +                               if (unlikely(!err && ++opt > opt_tail)) {
26702 +                                       err = -E2BIG;
26703 +                                       break;
26704 +                               }
26705 +                               opt->type = Opt_tail;
26706 +                               skipped = 1;
26707 +                       }
26708 +                       break;
26709 +               case Opt_add:
26710 +                       if (unlikely(match_int(&a->args[0], &n))) {
26711 +                               pr_err("bad integer in %s\n", opt_str);
26712 +                               break;
26713 +                       }
26714 +                       bindex = n;
26715 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26716 +                                     bindex);
26717 +                       if (!err)
26718 +                               opt->type = token;
26719 +                       break;
26720 +               case Opt_append:
26721 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26722 +                                     /*dummy bindex*/1);
26723 +                       if (!err)
26724 +                               opt->type = token;
26725 +                       break;
26726 +               case Opt_prepend:
26727 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26728 +                                     /*bindex*/0);
26729 +                       if (!err)
26730 +                               opt->type = token;
26731 +                       break;
26732 +               case Opt_del:
26733 +                       err = au_opts_parse_del(&opt->del, a->args);
26734 +                       if (!err)
26735 +                               opt->type = token;
26736 +                       break;
26737 +#if 0 /* reserved for future use */
26738 +               case Opt_idel:
26739 +                       del->pathname = "(indexed)";
26740 +                       if (unlikely(match_int(&args[0], &n))) {
26741 +                               pr_err("bad integer in %s\n", opt_str);
26742 +                               break;
26743 +                       }
26744 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26745 +                       if (!err)
26746 +                               opt->type = token;
26747 +                       break;
26748 +#endif
26749 +               case Opt_mod:
26750 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26751 +                       if (!err)
26752 +                               opt->type = token;
26753 +                       break;
26754 +#ifdef IMOD /* reserved for future use */
26755 +               case Opt_imod:
26756 +                       u.mod->path = "(indexed)";
26757 +                       if (unlikely(match_int(&a->args[0], &n))) {
26758 +                               pr_err("bad integer in %s\n", opt_str);
26759 +                               break;
26760 +                       }
26761 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26762 +                       if (!err)
26763 +                               opt->type = token;
26764 +                       break;
26765 +#endif
26766 +               case Opt_xino:
26767 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26768 +                       if (!err)
26769 +                               opt->type = token;
26770 +                       break;
26771 +
26772 +               case Opt_trunc_xino_path:
26773 +                       err = au_opts_parse_xino_itrunc_path
26774 +                               (sb, &opt->xino_itrunc, a->args);
26775 +                       if (!err)
26776 +                               opt->type = token;
26777 +                       break;
26778 +
26779 +               case Opt_itrunc_xino:
26780 +                       u.xino_itrunc = &opt->xino_itrunc;
26781 +                       if (unlikely(match_int(&a->args[0], &n))) {
26782 +                               pr_err("bad integer in %s\n", opt_str);
26783 +                               break;
26784 +                       }
26785 +                       u.xino_itrunc->bindex = n;
26786 +                       aufs_read_lock(root, AuLock_FLUSH);
26787 +                       if (n < 0 || au_sbbot(sb) < n) {
26788 +                               pr_err("out of bounds, %d\n", n);
26789 +                               aufs_read_unlock(root, !AuLock_IR);
26790 +                               break;
26791 +                       }
26792 +                       aufs_read_unlock(root, !AuLock_IR);
26793 +                       err = 0;
26794 +                       opt->type = token;
26795 +                       break;
26796 +
26797 +               case Opt_dirwh:
26798 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26799 +                               break;
26800 +                       err = 0;
26801 +                       opt->type = token;
26802 +                       break;
26803 +
26804 +               case Opt_rdcache:
26805 +                       if (unlikely(match_int(&a->args[0], &n))) {
26806 +                               pr_err("bad integer in %s\n", opt_str);
26807 +                               break;
26808 +                       }
26809 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26810 +                               pr_err("rdcache must be smaller than %d\n",
26811 +                                      AUFS_RDCACHE_MAX);
26812 +                               break;
26813 +                       }
26814 +                       opt->rdcache = n;
26815 +                       err = 0;
26816 +                       opt->type = token;
26817 +                       break;
26818 +               case Opt_rdblk:
26819 +                       if (unlikely(match_int(&a->args[0], &n)
26820 +                                    || n < 0
26821 +                                    || n > KMALLOC_MAX_SIZE)) {
26822 +                               pr_err("bad integer in %s\n", opt_str);
26823 +                               break;
26824 +                       }
26825 +                       if (unlikely(n && n < NAME_MAX)) {
26826 +                               pr_err("rdblk must be larger than %d\n",
26827 +                                      NAME_MAX);
26828 +                               break;
26829 +                       }
26830 +                       opt->rdblk = n;
26831 +                       err = 0;
26832 +                       opt->type = token;
26833 +                       break;
26834 +               case Opt_rdhash:
26835 +                       if (unlikely(match_int(&a->args[0], &n)
26836 +                                    || n < 0
26837 +                                    || n * sizeof(struct hlist_head)
26838 +                                    > KMALLOC_MAX_SIZE)) {
26839 +                               pr_err("bad integer in %s\n", opt_str);
26840 +                               break;
26841 +                       }
26842 +                       opt->rdhash = n;
26843 +                       err = 0;
26844 +                       opt->type = token;
26845 +                       break;
26846 +
26847 +               case Opt_trunc_xino:
26848 +               case Opt_notrunc_xino:
26849 +               case Opt_noxino:
26850 +               case Opt_trunc_xib:
26851 +               case Opt_notrunc_xib:
26852 +               case Opt_shwh:
26853 +               case Opt_noshwh:
26854 +               case Opt_dirperm1:
26855 +               case Opt_nodirperm1:
26856 +               case Opt_plink:
26857 +               case Opt_noplink:
26858 +               case Opt_list_plink:
26859 +               case Opt_dio:
26860 +               case Opt_nodio:
26861 +               case Opt_diropq_a:
26862 +               case Opt_diropq_w:
26863 +               case Opt_warn_perm:
26864 +               case Opt_nowarn_perm:
26865 +               case Opt_verbose:
26866 +               case Opt_noverbose:
26867 +               case Opt_sum:
26868 +               case Opt_nosum:
26869 +               case Opt_wsum:
26870 +               case Opt_rdblk_def:
26871 +               case Opt_rdhash_def:
26872 +               case Opt_dirren:
26873 +               case Opt_nodirren:
26874 +               case Opt_acl:
26875 +               case Opt_noacl:
26876 +                       err = 0;
26877 +                       opt->type = token;
26878 +                       break;
26879 +
26880 +               case Opt_udba:
26881 +                       opt->udba = udba_val(a->args[0].from);
26882 +                       if (opt->udba >= 0) {
26883 +                               err = 0;
26884 +                               opt->type = token;
26885 +                       } else
26886 +                               pr_err("wrong value, %s\n", opt_str);
26887 +                       break;
26888 +
26889 +               case Opt_wbr_create:
26890 +                       u.create = &opt->wbr_create;
26891 +                       u.create->wbr_create
26892 +                               = au_wbr_create_val(a->args[0].from, u.create);
26893 +                       if (u.create->wbr_create >= 0) {
26894 +                               err = 0;
26895 +                               opt->type = token;
26896 +                       } else
26897 +                               pr_err("wrong value, %s\n", opt_str);
26898 +                       break;
26899 +               case Opt_wbr_copyup:
26900 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26901 +                       if (opt->wbr_copyup >= 0) {
26902 +                               err = 0;
26903 +                               opt->type = token;
26904 +                       } else
26905 +                               pr_err("wrong value, %s\n", opt_str);
26906 +                       break;
26907 +
26908 +               case Opt_fhsm_sec:
26909 +                       if (unlikely(match_int(&a->args[0], &n)
26910 +                                    || n < 0)) {
26911 +                               pr_err("bad integer in %s\n", opt_str);
26912 +                               break;
26913 +                       }
26914 +                       if (sysaufs_brs) {
26915 +                               opt->fhsm_second = n;
26916 +                               opt->type = token;
26917 +                       } else
26918 +                               pr_warn("ignored %s\n", opt_str);
26919 +                       err = 0;
26920 +                       break;
26921 +
26922 +               case Opt_ignore:
26923 +                       pr_warn("ignored %s\n", opt_str);
26924 +                       /*FALLTHROUGH*/
26925 +               case Opt_ignore_silent:
26926 +                       skipped = 1;
26927 +                       err = 0;
26928 +                       break;
26929 +               case Opt_err:
26930 +                       pr_err("unknown option %s\n", opt_str);
26931 +                       break;
26932 +               }
26933 +
26934 +               if (!err && !skipped) {
26935 +                       if (unlikely(++opt > opt_tail)) {
26936 +                               err = -E2BIG;
26937 +                               opt--;
26938 +                               opt->type = Opt_tail;
26939 +                               break;
26940 +                       }
26941 +                       opt->type = Opt_tail;
26942 +               }
26943 +       }
26944 +
26945 +       au_kfree_rcu(a);
26946 +       dump_opts(opts);
26947 +       if (unlikely(err))
26948 +               au_opts_free(opts);
26949 +
26950 +out:
26951 +       return err;
26952 +}
26953 +
26954 +static int au_opt_wbr_create(struct super_block *sb,
26955 +                            struct au_opt_wbr_create *create)
26956 +{
26957 +       int err;
26958 +       struct au_sbinfo *sbinfo;
26959 +
26960 +       SiMustWriteLock(sb);
26961 +
26962 +       err = 1; /* handled */
26963 +       sbinfo = au_sbi(sb);
26964 +       if (sbinfo->si_wbr_create_ops->fin) {
26965 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26966 +               if (!err)
26967 +                       err = 1;
26968 +       }
26969 +
26970 +       sbinfo->si_wbr_create = create->wbr_create;
26971 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26972 +       switch (create->wbr_create) {
26973 +       case AuWbrCreate_MFSRRV:
26974 +       case AuWbrCreate_MFSRR:
26975 +       case AuWbrCreate_TDMFS:
26976 +       case AuWbrCreate_TDMFSV:
26977 +       case AuWbrCreate_PMFSRR:
26978 +       case AuWbrCreate_PMFSRRV:
26979 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26980 +               /*FALLTHROUGH*/
26981 +       case AuWbrCreate_MFS:
26982 +       case AuWbrCreate_MFSV:
26983 +       case AuWbrCreate_PMFS:
26984 +       case AuWbrCreate_PMFSV:
26985 +               sbinfo->si_wbr_mfs.mfs_expire
26986 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26987 +               break;
26988 +       }
26989 +
26990 +       if (sbinfo->si_wbr_create_ops->init)
26991 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26992 +
26993 +       return err;
26994 +}
26995 +
26996 +/*
26997 + * returns,
26998 + * plus: processed without an error
26999 + * zero: unprocessed
27000 + */
27001 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27002 +                        struct au_opts *opts)
27003 +{
27004 +       int err;
27005 +       struct au_sbinfo *sbinfo;
27006 +
27007 +       SiMustWriteLock(sb);
27008 +
27009 +       err = 1; /* handled */
27010 +       sbinfo = au_sbi(sb);
27011 +       switch (opt->type) {
27012 +       case Opt_udba:
27013 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27014 +               sbinfo->si_mntflags |= opt->udba;
27015 +               opts->given_udba |= opt->udba;
27016 +               break;
27017 +
27018 +       case Opt_plink:
27019 +               au_opt_set(sbinfo->si_mntflags, PLINK);
27020 +               break;
27021 +       case Opt_noplink:
27022 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27023 +                       au_plink_put(sb, /*verbose*/1);
27024 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
27025 +               break;
27026 +       case Opt_list_plink:
27027 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27028 +                       au_plink_list(sb);
27029 +               break;
27030 +
27031 +       case Opt_dio:
27032 +               au_opt_set(sbinfo->si_mntflags, DIO);
27033 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27034 +               break;
27035 +       case Opt_nodio:
27036 +               au_opt_clr(sbinfo->si_mntflags, DIO);
27037 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
27038 +               break;
27039 +
27040 +       case Opt_fhsm_sec:
27041 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27042 +               break;
27043 +
27044 +       case Opt_diropq_a:
27045 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27046 +               break;
27047 +       case Opt_diropq_w:
27048 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27049 +               break;
27050 +
27051 +       case Opt_warn_perm:
27052 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27053 +               break;
27054 +       case Opt_nowarn_perm:
27055 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27056 +               break;
27057 +
27058 +       case Opt_verbose:
27059 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
27060 +               break;
27061 +       case Opt_noverbose:
27062 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27063 +               break;
27064 +
27065 +       case Opt_sum:
27066 +               au_opt_set(sbinfo->si_mntflags, SUM);
27067 +               break;
27068 +       case Opt_wsum:
27069 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27070 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27071 +               break;
27072 +       case Opt_nosum:
27073 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27074 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
27075 +               break;
27076 +
27077 +       case Opt_wbr_create:
27078 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27079 +               break;
27080 +       case Opt_wbr_copyup:
27081 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27082 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27083 +               break;
27084 +
27085 +       case Opt_dirwh:
27086 +               sbinfo->si_dirwh = opt->dirwh;
27087 +               break;
27088 +
27089 +       case Opt_rdcache:
27090 +               sbinfo->si_rdcache
27091 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27092 +               break;
27093 +       case Opt_rdblk:
27094 +               sbinfo->si_rdblk = opt->rdblk;
27095 +               break;
27096 +       case Opt_rdblk_def:
27097 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27098 +               break;
27099 +       case Opt_rdhash:
27100 +               sbinfo->si_rdhash = opt->rdhash;
27101 +               break;
27102 +       case Opt_rdhash_def:
27103 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27104 +               break;
27105 +
27106 +       case Opt_shwh:
27107 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27108 +               break;
27109 +       case Opt_noshwh:
27110 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27111 +               break;
27112 +
27113 +       case Opt_dirperm1:
27114 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27115 +               break;
27116 +       case Opt_nodirperm1:
27117 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27118 +               break;
27119 +
27120 +       case Opt_trunc_xino:
27121 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27122 +               break;
27123 +       case Opt_notrunc_xino:
27124 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27125 +               break;
27126 +
27127 +       case Opt_trunc_xino_path:
27128 +       case Opt_itrunc_xino:
27129 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27130 +                                   /*idx_begin*/0);
27131 +               if (!err)
27132 +                       err = 1;
27133 +               break;
27134 +
27135 +       case Opt_trunc_xib:
27136 +               au_fset_opts(opts->flags, TRUNC_XIB);
27137 +               break;
27138 +       case Opt_notrunc_xib:
27139 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27140 +               break;
27141 +
27142 +       case Opt_dirren:
27143 +               err = 1;
27144 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27145 +                       err = au_dr_opt_set(sb);
27146 +                       if (!err)
27147 +                               err = 1;
27148 +               }
27149 +               if (err == 1)
27150 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27151 +               break;
27152 +       case Opt_nodirren:
27153 +               err = 1;
27154 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27155 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27156 +                                                             DR_FLUSHED));
27157 +                       if (!err)
27158 +                               err = 1;
27159 +               }
27160 +               if (err == 1)
27161 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27162 +               break;
27163 +
27164 +       case Opt_acl:
27165 +               sb->s_flags |= SB_POSIXACL;
27166 +               break;
27167 +       case Opt_noacl:
27168 +               sb->s_flags &= ~SB_POSIXACL;
27169 +               break;
27170 +
27171 +       default:
27172 +               err = 0;
27173 +               break;
27174 +       }
27175 +
27176 +       return err;
27177 +}
27178 +
27179 +/*
27180 + * returns tri-state.
27181 + * plus: processed without an error
27182 + * zero: unprocessed
27183 + * minus: error
27184 + */
27185 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27186 +                    struct au_opts *opts)
27187 +{
27188 +       int err, do_refresh;
27189 +
27190 +       err = 0;
27191 +       switch (opt->type) {
27192 +       case Opt_append:
27193 +               opt->add.bindex = au_sbbot(sb) + 1;
27194 +               if (opt->add.bindex < 0)
27195 +                       opt->add.bindex = 0;
27196 +               goto add;
27197 +               /* Always goto add, not fallthrough */
27198 +       case Opt_prepend:
27199 +               opt->add.bindex = 0;
27200 +               /* fallthrough */
27201 +       add: /* indented label */
27202 +       case Opt_add:
27203 +               err = au_br_add(sb, &opt->add,
27204 +                               au_ftest_opts(opts->flags, REMOUNT));
27205 +               if (!err) {
27206 +                       err = 1;
27207 +                       au_fset_opts(opts->flags, REFRESH);
27208 +               }
27209 +               break;
27210 +
27211 +       case Opt_del:
27212 +       case Opt_idel:
27213 +               err = au_br_del(sb, &opt->del,
27214 +                               au_ftest_opts(opts->flags, REMOUNT));
27215 +               if (!err) {
27216 +                       err = 1;
27217 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27218 +                       au_fset_opts(opts->flags, REFRESH);
27219 +               }
27220 +               break;
27221 +
27222 +       case Opt_mod:
27223 +       case Opt_imod:
27224 +               err = au_br_mod(sb, &opt->mod,
27225 +                               au_ftest_opts(opts->flags, REMOUNT),
27226 +                               &do_refresh);
27227 +               if (!err) {
27228 +                       err = 1;
27229 +                       if (do_refresh)
27230 +                               au_fset_opts(opts->flags, REFRESH);
27231 +               }
27232 +               break;
27233 +       }
27234 +       return err;
27235 +}
27236 +
27237 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27238 +                      struct au_opt_xino **opt_xino,
27239 +                      struct au_opts *opts)
27240 +{
27241 +       int err;
27242 +
27243 +       err = 0;
27244 +       switch (opt->type) {
27245 +       case Opt_xino:
27246 +               err = au_xino_set(sb, &opt->xino,
27247 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27248 +               if (unlikely(err))
27249 +                       break;
27250 +
27251 +               *opt_xino = &opt->xino;
27252 +               break;
27253 +
27254 +       case Opt_noxino:
27255 +               au_xino_clr(sb);
27256 +               *opt_xino = (void *)-1;
27257 +               break;
27258 +       }
27259 +
27260 +       return err;
27261 +}
27262 +
27263 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27264 +                  unsigned int pending)
27265 +{
27266 +       int err, fhsm;
27267 +       aufs_bindex_t bindex, bbot;
27268 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27269 +       struct au_branch *br;
27270 +       struct au_wbr *wbr;
27271 +       struct dentry *root, *dentry;
27272 +       struct inode *dir, *h_dir;
27273 +       struct au_sbinfo *sbinfo;
27274 +       struct au_hinode *hdir;
27275 +
27276 +       SiMustAnyLock(sb);
27277 +
27278 +       sbinfo = au_sbi(sb);
27279 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27280 +
27281 +       if (!(sb_flags & SB_RDONLY)) {
27282 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27283 +                       pr_warn("first branch should be rw\n");
27284 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27285 +                       pr_warn_once("shwh should be used with ro\n");
27286 +       }
27287 +
27288 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27289 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27290 +               pr_warn_once("udba=*notify requires xino\n");
27291 +
27292 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27293 +               pr_warn_once("dirperm1 breaks the protection"
27294 +                            " by the permission bits on the lower branch\n");
27295 +
27296 +       err = 0;
27297 +       fhsm = 0;
27298 +       root = sb->s_root;
27299 +       dir = d_inode(root);
27300 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27301 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27302 +                                     UDBA_NONE);
27303 +       bbot = au_sbbot(sb);
27304 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27305 +               skip = 0;
27306 +               h_dir = au_h_iptr(dir, bindex);
27307 +               br = au_sbr(sb, bindex);
27308 +
27309 +               if ((br->br_perm & AuBrAttr_ICEX)
27310 +                   && !h_dir->i_op->listxattr)
27311 +                       br->br_perm &= ~AuBrAttr_ICEX;
27312 +#if 0 /* untested */
27313 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27314 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27315 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27316 +#endif
27317 +
27318 +               do_free = 0;
27319 +               wbr = br->br_wbr;
27320 +               if (wbr)
27321 +                       wbr_wh_read_lock(wbr);
27322 +
27323 +               if (!au_br_writable(br->br_perm)) {
27324 +                       do_free = !!wbr;
27325 +                       skip = (!wbr
27326 +                               || (!wbr->wbr_whbase
27327 +                                   && !wbr->wbr_plink
27328 +                                   && !wbr->wbr_orph));
27329 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27330 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27331 +                       skip = (!wbr || !wbr->wbr_whbase);
27332 +                       if (skip && wbr) {
27333 +                               if (do_plink)
27334 +                                       skip = !!wbr->wbr_plink;
27335 +                               else
27336 +                                       skip = !wbr->wbr_plink;
27337 +                       }
27338 +               } else {
27339 +                       /* skip = (br->br_whbase && br->br_ohph); */
27340 +                       skip = (wbr && wbr->wbr_whbase);
27341 +                       if (skip) {
27342 +                               if (do_plink)
27343 +                                       skip = !!wbr->wbr_plink;
27344 +                               else
27345 +                                       skip = !wbr->wbr_plink;
27346 +                       }
27347 +               }
27348 +               if (wbr)
27349 +                       wbr_wh_read_unlock(wbr);
27350 +
27351 +               if (can_no_dreval) {
27352 +                       dentry = br->br_path.dentry;
27353 +                       spin_lock(&dentry->d_lock);
27354 +                       if (dentry->d_flags &
27355 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27356 +                               can_no_dreval = 0;
27357 +                       spin_unlock(&dentry->d_lock);
27358 +               }
27359 +
27360 +               if (au_br_fhsm(br->br_perm)) {
27361 +                       fhsm++;
27362 +                       AuDebugOn(!br->br_fhsm);
27363 +               }
27364 +
27365 +               if (skip)
27366 +                       continue;
27367 +
27368 +               hdir = au_hi(dir, bindex);
27369 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27370 +               if (wbr)
27371 +                       wbr_wh_write_lock(wbr);
27372 +               err = au_wh_init(br, sb);
27373 +               if (wbr)
27374 +                       wbr_wh_write_unlock(wbr);
27375 +               au_hn_inode_unlock(hdir);
27376 +
27377 +               if (!err && do_free) {
27378 +                       au_kfree_rcu(wbr);
27379 +                       br->br_wbr = NULL;
27380 +               }
27381 +       }
27382 +
27383 +       if (can_no_dreval)
27384 +               au_fset_si(sbinfo, NO_DREVAL);
27385 +       else
27386 +               au_fclr_si(sbinfo, NO_DREVAL);
27387 +
27388 +       if (fhsm >= 2) {
27389 +               au_fset_si(sbinfo, FHSM);
27390 +               for (bindex = bbot; bindex >= 0; bindex--) {
27391 +                       br = au_sbr(sb, bindex);
27392 +                       if (au_br_fhsm(br->br_perm)) {
27393 +                               au_fhsm_set_bottom(sb, bindex);
27394 +                               break;
27395 +                       }
27396 +               }
27397 +       } else {
27398 +               au_fclr_si(sbinfo, FHSM);
27399 +               au_fhsm_set_bottom(sb, -1);
27400 +       }
27401 +
27402 +       return err;
27403 +}
27404 +
27405 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27406 +{
27407 +       int err;
27408 +       unsigned int tmp;
27409 +       aufs_bindex_t bindex, bbot;
27410 +       struct au_opt *opt;
27411 +       struct au_opt_xino *opt_xino, xino;
27412 +       struct au_sbinfo *sbinfo;
27413 +       struct au_branch *br;
27414 +       struct inode *dir;
27415 +
27416 +       SiMustWriteLock(sb);
27417 +
27418 +       err = 0;
27419 +       opt_xino = NULL;
27420 +       opt = opts->opt;
27421 +       while (err >= 0 && opt->type != Opt_tail)
27422 +               err = au_opt_simple(sb, opt++, opts);
27423 +       if (err > 0)
27424 +               err = 0;
27425 +       else if (unlikely(err < 0))
27426 +               goto out;
27427 +
27428 +       /* disable xino and udba temporary */
27429 +       sbinfo = au_sbi(sb);
27430 +       tmp = sbinfo->si_mntflags;
27431 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27432 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27433 +
27434 +       opt = opts->opt;
27435 +       while (err >= 0 && opt->type != Opt_tail)
27436 +               err = au_opt_br(sb, opt++, opts);
27437 +       if (err > 0)
27438 +               err = 0;
27439 +       else if (unlikely(err < 0))
27440 +               goto out;
27441 +
27442 +       bbot = au_sbbot(sb);
27443 +       if (unlikely(bbot < 0)) {
27444 +               err = -EINVAL;
27445 +               pr_err("no branches\n");
27446 +               goto out;
27447 +       }
27448 +
27449 +       if (au_opt_test(tmp, XINO))
27450 +               au_opt_set(sbinfo->si_mntflags, XINO);
27451 +       opt = opts->opt;
27452 +       while (!err && opt->type != Opt_tail)
27453 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27454 +       if (unlikely(err))
27455 +               goto out;
27456 +
27457 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27458 +       if (unlikely(err))
27459 +               goto out;
27460 +
27461 +       /* restore xino */
27462 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27463 +               xino.file = au_xino_def(sb);
27464 +               err = PTR_ERR(xino.file);
27465 +               if (IS_ERR(xino.file))
27466 +                       goto out;
27467 +
27468 +               err = au_xino_set(sb, &xino, /*remount*/0);
27469 +               fput(xino.file);
27470 +               if (unlikely(err))
27471 +                       goto out;
27472 +       }
27473 +
27474 +       /* restore udba */
27475 +       tmp &= AuOptMask_UDBA;
27476 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27477 +       sbinfo->si_mntflags |= tmp;
27478 +       bbot = au_sbbot(sb);
27479 +       for (bindex = 0; bindex <= bbot; bindex++) {
27480 +               br = au_sbr(sb, bindex);
27481 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27482 +               if (unlikely(err))
27483 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27484 +                               bindex, err);
27485 +               /* go on even if err */
27486 +       }
27487 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27488 +               dir = d_inode(sb->s_root);
27489 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27490 +       }
27491 +
27492 +out:
27493 +       return err;
27494 +}
27495 +
27496 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27497 +{
27498 +       int err, rerr;
27499 +       unsigned char no_dreval;
27500 +       struct inode *dir;
27501 +       struct au_opt_xino *opt_xino;
27502 +       struct au_opt *opt;
27503 +       struct au_sbinfo *sbinfo;
27504 +
27505 +       SiMustWriteLock(sb);
27506 +
27507 +       err = au_dr_opt_flush(sb);
27508 +       if (unlikely(err))
27509 +               goto out;
27510 +       au_fset_opts(opts->flags, DR_FLUSHED);
27511 +
27512 +       dir = d_inode(sb->s_root);
27513 +       sbinfo = au_sbi(sb);
27514 +       opt_xino = NULL;
27515 +       opt = opts->opt;
27516 +       while (err >= 0 && opt->type != Opt_tail) {
27517 +               err = au_opt_simple(sb, opt, opts);
27518 +               if (!err)
27519 +                       err = au_opt_br(sb, opt, opts);
27520 +               if (!err)
27521 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27522 +               opt++;
27523 +       }
27524 +       if (err > 0)
27525 +               err = 0;
27526 +       AuTraceErr(err);
27527 +       /* go on even err */
27528 +
27529 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27530 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27531 +       if (unlikely(rerr && !err))
27532 +               err = rerr;
27533 +
27534 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27535 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27536 +
27537 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27538 +               rerr = au_xib_trunc(sb);
27539 +               if (unlikely(rerr && !err))
27540 +                       err = rerr;
27541 +       }
27542 +
27543 +       /* will be handled by the caller */
27544 +       if (!au_ftest_opts(opts->flags, REFRESH)
27545 +           && (opts->given_udba
27546 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27547 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27548 +                   ))
27549 +               au_fset_opts(opts->flags, REFRESH);
27550 +
27551 +       AuDbg("status 0x%x\n", opts->flags);
27552 +
27553 +out:
27554 +       return err;
27555 +}
27556 +
27557 +/* ---------------------------------------------------------------------- */
27558 +
27559 +unsigned int au_opt_udba(struct super_block *sb)
27560 +{
27561 +       return au_mntflags(sb) & AuOptMask_UDBA;
27562 +}
27563 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27564 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27565 +++ linux/fs/aufs/opts.h        2020-01-27 10:57:18.175538316 +0100
27566 @@ -0,0 +1,225 @@
27567 +/* SPDX-License-Identifier: GPL-2.0 */
27568 +/*
27569 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27570 + *
27571 + * This program, aufs is free software; you can redistribute it and/or modify
27572 + * it under the terms of the GNU General Public License as published by
27573 + * the Free Software Foundation; either version 2 of the License, or
27574 + * (at your option) any later version.
27575 + *
27576 + * This program is distributed in the hope that it will be useful,
27577 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27578 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27579 + * GNU General Public License for more details.
27580 + *
27581 + * You should have received a copy of the GNU General Public License
27582 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27583 + */
27584 +
27585 +/*
27586 + * mount options/flags
27587 + */
27588 +
27589 +#ifndef __AUFS_OPTS_H__
27590 +#define __AUFS_OPTS_H__
27591 +
27592 +#ifdef __KERNEL__
27593 +
27594 +#include <linux/path.h>
27595 +
27596 +struct file;
27597 +
27598 +/* ---------------------------------------------------------------------- */
27599 +
27600 +/* mount flags */
27601 +#define AuOpt_XINO             1               /* external inode number bitmap
27602 +                                                  and translation table */
27603 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27604 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27605 +#define AuOpt_UDBA_REVAL       (1 << 3)
27606 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27607 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27608 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27609 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27610 +                                                  bits */
27611 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27612 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27613 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27614 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27615 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27616 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27617 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27618 +
27619 +#ifndef CONFIG_AUFS_HNOTIFY
27620 +#undef AuOpt_UDBA_HNOTIFY
27621 +#define AuOpt_UDBA_HNOTIFY     0
27622 +#endif
27623 +#ifndef CONFIG_AUFS_DIRREN
27624 +#undef AuOpt_DIRREN
27625 +#define AuOpt_DIRREN           0
27626 +#endif
27627 +#ifndef CONFIG_AUFS_SHWH
27628 +#undef AuOpt_SHWH
27629 +#define AuOpt_SHWH             0
27630 +#endif
27631 +
27632 +#define AuOpt_Def      (AuOpt_XINO \
27633 +                        | AuOpt_UDBA_REVAL \
27634 +                        | AuOpt_PLINK \
27635 +                        /* | AuOpt_DIRPERM1 */ \
27636 +                        | AuOpt_WARN_PERM)
27637 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27638 +                        | AuOpt_UDBA_REVAL \
27639 +                        | AuOpt_UDBA_HNOTIFY)
27640 +
27641 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27642 +#define au_opt_set(flags, name) do { \
27643 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27644 +       ((flags) |= AuOpt_##name); \
27645 +} while (0)
27646 +#define au_opt_set_udba(flags, name) do { \
27647 +       (flags) &= ~AuOptMask_UDBA; \
27648 +       ((flags) |= AuOpt_##name); \
27649 +} while (0)
27650 +#define au_opt_clr(flags, name) do { \
27651 +       ((flags) &= ~AuOpt_##name); \
27652 +} while (0)
27653 +
27654 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27655 +{
27656 +#ifdef CONFIG_PROC_FS
27657 +       return mntflags;
27658 +#else
27659 +       return mntflags & ~AuOpt_PLINK;
27660 +#endif
27661 +}
27662 +
27663 +/* ---------------------------------------------------------------------- */
27664 +
27665 +/* policies to select one among multiple writable branches */
27666 +enum {
27667 +       AuWbrCreate_TDP,        /* top down parent */
27668 +       AuWbrCreate_RR,         /* round robin */
27669 +       AuWbrCreate_MFS,        /* most free space */
27670 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27671 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27672 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27673 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27674 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27675 +       AuWbrCreate_PMFS,       /* parent and mfs */
27676 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27677 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27678 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27679 +
27680 +       AuWbrCreate_Def = AuWbrCreate_TDP
27681 +};
27682 +
27683 +enum {
27684 +       AuWbrCopyup_TDP,        /* top down parent */
27685 +       AuWbrCopyup_BUP,        /* bottom up parent */
27686 +       AuWbrCopyup_BU,         /* bottom up */
27687 +
27688 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27689 +};
27690 +
27691 +/* ---------------------------------------------------------------------- */
27692 +
27693 +struct au_opt_add {
27694 +       aufs_bindex_t   bindex;
27695 +       char            *pathname;
27696 +       int             perm;
27697 +       struct path     path;
27698 +};
27699 +
27700 +struct au_opt_del {
27701 +       char            *pathname;
27702 +       struct path     h_path;
27703 +};
27704 +
27705 +struct au_opt_mod {
27706 +       char            *path;
27707 +       int             perm;
27708 +       struct dentry   *h_root;
27709 +};
27710 +
27711 +struct au_opt_xino {
27712 +       char            *path;
27713 +       struct file     *file;
27714 +};
27715 +
27716 +struct au_opt_xino_itrunc {
27717 +       aufs_bindex_t   bindex;
27718 +};
27719 +
27720 +struct au_opt_wbr_create {
27721 +       int                     wbr_create;
27722 +       int                     mfs_second;
27723 +       unsigned long long      mfsrr_watermark;
27724 +};
27725 +
27726 +struct au_opt {
27727 +       int type;
27728 +       union {
27729 +               struct au_opt_xino      xino;
27730 +               struct au_opt_xino_itrunc xino_itrunc;
27731 +               struct au_opt_add       add;
27732 +               struct au_opt_del       del;
27733 +               struct au_opt_mod       mod;
27734 +               int                     dirwh;
27735 +               int                     rdcache;
27736 +               unsigned int            rdblk;
27737 +               unsigned int            rdhash;
27738 +               int                     udba;
27739 +               struct au_opt_wbr_create wbr_create;
27740 +               int                     wbr_copyup;
27741 +               unsigned int            fhsm_second;
27742 +       };
27743 +};
27744 +
27745 +/* opts flags */
27746 +#define AuOpts_REMOUNT         1
27747 +#define AuOpts_REFRESH         (1 << 1)
27748 +#define AuOpts_TRUNC_XIB       (1 << 2)
27749 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27750 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27751 +#define AuOpts_DR_FLUSHED      (1 << 5)
27752 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27753 +#define au_fset_opts(flags, name) \
27754 +       do { (flags) |= AuOpts_##name; } while (0)
27755 +#define au_fclr_opts(flags, name) \
27756 +       do { (flags) &= ~AuOpts_##name; } while (0)
27757 +
27758 +#ifndef CONFIG_AUFS_DIRREN
27759 +#undef AuOpts_DR_FLUSHED
27760 +#define AuOpts_DR_FLUSHED      0
27761 +#endif
27762 +
27763 +struct au_opts {
27764 +       struct au_opt   *opt;
27765 +       int             max_opt;
27766 +
27767 +       unsigned int    given_udba;
27768 +       unsigned int    flags;
27769 +       unsigned long   sb_flags;
27770 +};
27771 +
27772 +/* ---------------------------------------------------------------------- */
27773 +
27774 +/* opts.c */
27775 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27776 +const char *au_optstr_udba(int udba);
27777 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27778 +const char *au_optstr_wbr_create(int wbr_create);
27779 +
27780 +void au_opts_free(struct au_opts *opts);
27781 +struct super_block;
27782 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27783 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27784 +                  unsigned int pending);
27785 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27786 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27787 +
27788 +unsigned int au_opt_udba(struct super_block *sb);
27789 +
27790 +#endif /* __KERNEL__ */
27791 +#endif /* __AUFS_OPTS_H__ */
27792 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27793 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27794 +++ linux/fs/aufs/plink.c       2020-01-27 10:57:18.175538316 +0100
27795 @@ -0,0 +1,516 @@
27796 +// SPDX-License-Identifier: GPL-2.0
27797 +/*
27798 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27799 + *
27800 + * This program, aufs is free software; you can redistribute it and/or modify
27801 + * it under the terms of the GNU General Public License as published by
27802 + * the Free Software Foundation; either version 2 of the License, or
27803 + * (at your option) any later version.
27804 + *
27805 + * This program is distributed in the hope that it will be useful,
27806 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27807 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27808 + * GNU General Public License for more details.
27809 + *
27810 + * You should have received a copy of the GNU General Public License
27811 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27812 + */
27813 +
27814 +/*
27815 + * pseudo-link
27816 + */
27817 +
27818 +#include "aufs.h"
27819 +
27820 +/*
27821 + * the pseudo-link maintenance mode.
27822 + * during a user process maintains the pseudo-links,
27823 + * prohibit adding a new plink and branch manipulation.
27824 + *
27825 + * Flags
27826 + * NOPLM:
27827 + *     For entry functions which will handle plink, and i_mutex is already held
27828 + *     in VFS.
27829 + *     They cannot wait and should return an error at once.
27830 + *     Callers has to check the error.
27831 + * NOPLMW:
27832 + *     For entry functions which will handle plink, but i_mutex is not held
27833 + *     in VFS.
27834 + *     They can wait the plink maintenance mode to finish.
27835 + *
27836 + * They behave like F_SETLK and F_SETLKW.
27837 + * If the caller never handle plink, then both flags are unnecessary.
27838 + */
27839 +
27840 +int au_plink_maint(struct super_block *sb, int flags)
27841 +{
27842 +       int err;
27843 +       pid_t pid, ppid;
27844 +       struct task_struct *parent, *prev;
27845 +       struct au_sbinfo *sbi;
27846 +
27847 +       SiMustAnyLock(sb);
27848 +
27849 +       err = 0;
27850 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27851 +               goto out;
27852 +
27853 +       sbi = au_sbi(sb);
27854 +       pid = sbi->si_plink_maint_pid;
27855 +       if (!pid || pid == current->pid)
27856 +               goto out;
27857 +
27858 +       /* todo: it highly depends upon /sbin/mount.aufs */
27859 +       prev = NULL;
27860 +       parent = current;
27861 +       ppid = 0;
27862 +       rcu_read_lock();
27863 +       while (1) {
27864 +               parent = rcu_dereference(parent->real_parent);
27865 +               if (parent == prev)
27866 +                       break;
27867 +               ppid = task_pid_vnr(parent);
27868 +               if (pid == ppid) {
27869 +                       rcu_read_unlock();
27870 +                       goto out;
27871 +               }
27872 +               prev = parent;
27873 +       }
27874 +       rcu_read_unlock();
27875 +
27876 +       if (au_ftest_lock(flags, NOPLMW)) {
27877 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27878 +               /* AuDebugOn(!lockdep_depth(current)); */
27879 +               while (sbi->si_plink_maint_pid) {
27880 +                       si_read_unlock(sb);
27881 +                       /* gave up wake_up_bit() */
27882 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27883 +
27884 +                       if (au_ftest_lock(flags, FLUSH))
27885 +                               au_nwt_flush(&sbi->si_nowait);
27886 +                       si_noflush_read_lock(sb);
27887 +               }
27888 +       } else if (au_ftest_lock(flags, NOPLM)) {
27889 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27890 +               err = -EAGAIN;
27891 +       }
27892 +
27893 +out:
27894 +       return err;
27895 +}
27896 +
27897 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27898 +{
27899 +       spin_lock(&sbinfo->si_plink_maint_lock);
27900 +       sbinfo->si_plink_maint_pid = 0;
27901 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27902 +       wake_up_all(&sbinfo->si_plink_wq);
27903 +}
27904 +
27905 +int au_plink_maint_enter(struct super_block *sb)
27906 +{
27907 +       int err;
27908 +       struct au_sbinfo *sbinfo;
27909 +
27910 +       err = 0;
27911 +       sbinfo = au_sbi(sb);
27912 +       /* make sure i am the only one in this fs */
27913 +       si_write_lock(sb, AuLock_FLUSH);
27914 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27915 +               spin_lock(&sbinfo->si_plink_maint_lock);
27916 +               if (!sbinfo->si_plink_maint_pid)
27917 +                       sbinfo->si_plink_maint_pid = current->pid;
27918 +               else
27919 +                       err = -EBUSY;
27920 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27921 +       }
27922 +       si_write_unlock(sb);
27923 +
27924 +       return err;
27925 +}
27926 +
27927 +/* ---------------------------------------------------------------------- */
27928 +
27929 +#ifdef CONFIG_AUFS_DEBUG
27930 +void au_plink_list(struct super_block *sb)
27931 +{
27932 +       int i;
27933 +       struct au_sbinfo *sbinfo;
27934 +       struct hlist_bl_head *hbl;
27935 +       struct hlist_bl_node *pos;
27936 +       struct au_icntnr *icntnr;
27937 +
27938 +       SiMustAnyLock(sb);
27939 +
27940 +       sbinfo = au_sbi(sb);
27941 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27942 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27943 +
27944 +       for (i = 0; i < AuPlink_NHASH; i++) {
27945 +               hbl = sbinfo->si_plink + i;
27946 +               hlist_bl_lock(hbl);
27947 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27948 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27949 +               hlist_bl_unlock(hbl);
27950 +       }
27951 +}
27952 +#endif
27953 +
27954 +/* is the inode pseudo-linked? */
27955 +int au_plink_test(struct inode *inode)
27956 +{
27957 +       int found, i;
27958 +       struct au_sbinfo *sbinfo;
27959 +       struct hlist_bl_head *hbl;
27960 +       struct hlist_bl_node *pos;
27961 +       struct au_icntnr *icntnr;
27962 +
27963 +       sbinfo = au_sbi(inode->i_sb);
27964 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27965 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27966 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27967 +
27968 +       found = 0;
27969 +       i = au_plink_hash(inode->i_ino);
27970 +       hbl =  sbinfo->si_plink + i;
27971 +       hlist_bl_lock(hbl);
27972 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27973 +               if (&icntnr->vfs_inode == inode) {
27974 +                       found = 1;
27975 +                       break;
27976 +               }
27977 +       hlist_bl_unlock(hbl);
27978 +       return found;
27979 +}
27980 +
27981 +/* ---------------------------------------------------------------------- */
27982 +
27983 +/*
27984 + * generate a name for plink.
27985 + * the file will be stored under AUFS_WH_PLINKDIR.
27986 + */
27987 +/* 20 is max digits length of ulong 64 */
27988 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27989 +
27990 +static int plink_name(char *name, int len, struct inode *inode,
27991 +                     aufs_bindex_t bindex)
27992 +{
27993 +       int rlen;
27994 +       struct inode *h_inode;
27995 +
27996 +       h_inode = au_h_iptr(inode, bindex);
27997 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27998 +       return rlen;
27999 +}
28000 +
28001 +struct au_do_plink_lkup_args {
28002 +       struct dentry **errp;
28003 +       struct qstr *tgtname;
28004 +       struct dentry *h_parent;
28005 +       struct au_branch *br;
28006 +};
28007 +
28008 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28009 +                                      struct dentry *h_parent,
28010 +                                      struct au_branch *br)
28011 +{
28012 +       struct dentry *h_dentry;
28013 +       struct inode *h_inode;
28014 +
28015 +       h_inode = d_inode(h_parent);
28016 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28017 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
28018 +       inode_unlock_shared(h_inode);
28019 +       return h_dentry;
28020 +}
28021 +
28022 +static void au_call_do_plink_lkup(void *args)
28023 +{
28024 +       struct au_do_plink_lkup_args *a = args;
28025 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
28026 +}
28027 +
28028 +/* lookup the plink-ed @inode under the branch at @bindex */
28029 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28030 +{
28031 +       struct dentry *h_dentry, *h_parent;
28032 +       struct au_branch *br;
28033 +       int wkq_err;
28034 +       char a[PLINK_NAME_LEN];
28035 +       struct qstr tgtname = QSTR_INIT(a, 0);
28036 +
28037 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28038 +
28039 +       br = au_sbr(inode->i_sb, bindex);
28040 +       h_parent = br->br_wbr->wbr_plink;
28041 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28042 +
28043 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28044 +               struct au_do_plink_lkup_args args = {
28045 +                       .errp           = &h_dentry,
28046 +                       .tgtname        = &tgtname,
28047 +                       .h_parent       = h_parent,
28048 +                       .br             = br
28049 +               };
28050 +
28051 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28052 +               if (unlikely(wkq_err))
28053 +                       h_dentry = ERR_PTR(wkq_err);
28054 +       } else
28055 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
28056 +
28057 +       return h_dentry;
28058 +}
28059 +
28060 +/* create a pseudo-link */
28061 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
28062 +                     struct dentry *h_dentry, struct au_branch *br)
28063 +{
28064 +       int err;
28065 +       struct path h_path = {
28066 +               .mnt = au_br_mnt(br)
28067 +       };
28068 +       struct inode *h_dir, *delegated;
28069 +
28070 +       h_dir = d_inode(h_parent);
28071 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28072 +again:
28073 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
28074 +       err = PTR_ERR(h_path.dentry);
28075 +       if (IS_ERR(h_path.dentry))
28076 +               goto out;
28077 +
28078 +       err = 0;
28079 +       /* wh.plink dir is not monitored */
28080 +       /* todo: is it really safe? */
28081 +       if (d_is_positive(h_path.dentry)
28082 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28083 +               delegated = NULL;
28084 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28085 +               if (unlikely(err == -EWOULDBLOCK)) {
28086 +                       pr_warn("cannot retry for NFSv4 delegation"
28087 +                               " for an internal unlink\n");
28088 +                       iput(delegated);
28089 +               }
28090 +               dput(h_path.dentry);
28091 +               h_path.dentry = NULL;
28092 +               if (!err)
28093 +                       goto again;
28094 +       }
28095 +       if (!err && d_is_negative(h_path.dentry)) {
28096 +               delegated = NULL;
28097 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28098 +               if (unlikely(err == -EWOULDBLOCK)) {
28099 +                       pr_warn("cannot retry for NFSv4 delegation"
28100 +                               " for an internal link\n");
28101 +                       iput(delegated);
28102 +               }
28103 +       }
28104 +       dput(h_path.dentry);
28105 +
28106 +out:
28107 +       inode_unlock(h_dir);
28108 +       return err;
28109 +}
28110 +
28111 +struct do_whplink_args {
28112 +       int *errp;
28113 +       struct qstr *tgt;
28114 +       struct dentry *h_parent;
28115 +       struct dentry *h_dentry;
28116 +       struct au_branch *br;
28117 +};
28118 +
28119 +static void call_do_whplink(void *args)
28120 +{
28121 +       struct do_whplink_args *a = args;
28122 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
28123 +}
28124 +
28125 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28126 +                  aufs_bindex_t bindex, struct au_branch *br)
28127 +{
28128 +       int err, wkq_err;
28129 +       struct au_wbr *wbr;
28130 +       struct dentry *h_parent;
28131 +       char a[PLINK_NAME_LEN];
28132 +       struct qstr tgtname = QSTR_INIT(a, 0);
28133 +
28134 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28135 +       h_parent = wbr->wbr_plink;
28136 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28137 +
28138 +       /* always superio. */
28139 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28140 +               struct do_whplink_args args = {
28141 +                       .errp           = &err,
28142 +                       .tgt            = &tgtname,
28143 +                       .h_parent       = h_parent,
28144 +                       .h_dentry       = h_dentry,
28145 +                       .br             = br
28146 +               };
28147 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28148 +               if (unlikely(wkq_err))
28149 +                       err = wkq_err;
28150 +       } else
28151 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28152 +
28153 +       return err;
28154 +}
28155 +
28156 +/*
28157 + * create a new pseudo-link for @h_dentry on @bindex.
28158 + * the linked inode is held in aufs @inode.
28159 + */
28160 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28161 +                    struct dentry *h_dentry)
28162 +{
28163 +       struct super_block *sb;
28164 +       struct au_sbinfo *sbinfo;
28165 +       struct hlist_bl_head *hbl;
28166 +       struct hlist_bl_node *pos;
28167 +       struct au_icntnr *icntnr;
28168 +       int found, err, cnt, i;
28169 +
28170 +       sb = inode->i_sb;
28171 +       sbinfo = au_sbi(sb);
28172 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28173 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28174 +
28175 +       found = au_plink_test(inode);
28176 +       if (found)
28177 +               return;
28178 +
28179 +       i = au_plink_hash(inode->i_ino);
28180 +       hbl = sbinfo->si_plink + i;
28181 +       au_igrab(inode);
28182 +
28183 +       hlist_bl_lock(hbl);
28184 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28185 +               if (&icntnr->vfs_inode == inode) {
28186 +                       found = 1;
28187 +                       break;
28188 +               }
28189 +       }
28190 +       if (!found) {
28191 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28192 +               hlist_bl_add_head(&icntnr->plink, hbl);
28193 +       }
28194 +       hlist_bl_unlock(hbl);
28195 +       if (!found) {
28196 +               cnt = au_hbl_count(hbl);
28197 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28198 +               if (cnt > AUFS_PLINK_WARN)
28199 +                       AuWarn1(msg ", %d\n", cnt);
28200 +#undef msg
28201 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28202 +               if (unlikely(err)) {
28203 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28204 +                       au_hbl_del(&icntnr->plink, hbl);
28205 +                       iput(&icntnr->vfs_inode);
28206 +               }
28207 +       } else
28208 +               iput(&icntnr->vfs_inode);
28209 +}
28210 +
28211 +/* free all plinks */
28212 +void au_plink_put(struct super_block *sb, int verbose)
28213 +{
28214 +       int i, warned;
28215 +       struct au_sbinfo *sbinfo;
28216 +       struct hlist_bl_head *hbl;
28217 +       struct hlist_bl_node *pos, *tmp;
28218 +       struct au_icntnr *icntnr;
28219 +
28220 +       SiMustWriteLock(sb);
28221 +
28222 +       sbinfo = au_sbi(sb);
28223 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28224 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28225 +
28226 +       /* no spin_lock since sbinfo is write-locked */
28227 +       warned = 0;
28228 +       for (i = 0; i < AuPlink_NHASH; i++) {
28229 +               hbl = sbinfo->si_plink + i;
28230 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28231 +                       pr_warn("pseudo-link is not flushed");
28232 +                       warned = 1;
28233 +               }
28234 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28235 +                       iput(&icntnr->vfs_inode);
28236 +               INIT_HLIST_BL_HEAD(hbl);
28237 +       }
28238 +}
28239 +
28240 +void au_plink_clean(struct super_block *sb, int verbose)
28241 +{
28242 +       struct dentry *root;
28243 +
28244 +       root = sb->s_root;
28245 +       aufs_write_lock(root);
28246 +       if (au_opt_test(au_mntflags(sb), PLINK))
28247 +               au_plink_put(sb, verbose);
28248 +       aufs_write_unlock(root);
28249 +}
28250 +
28251 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28252 +{
28253 +       int do_put;
28254 +       aufs_bindex_t btop, bbot, bindex;
28255 +
28256 +       do_put = 0;
28257 +       btop = au_ibtop(inode);
28258 +       bbot = au_ibbot(inode);
28259 +       if (btop >= 0) {
28260 +               for (bindex = btop; bindex <= bbot; bindex++) {
28261 +                       if (!au_h_iptr(inode, bindex)
28262 +                           || au_ii_br_id(inode, bindex) != br_id)
28263 +                               continue;
28264 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28265 +                       do_put = 1;
28266 +                       break;
28267 +               }
28268 +               if (do_put)
28269 +                       for (bindex = btop; bindex <= bbot; bindex++)
28270 +                               if (au_h_iptr(inode, bindex)) {
28271 +                                       do_put = 0;
28272 +                                       break;
28273 +                               }
28274 +       } else
28275 +               do_put = 1;
28276 +
28277 +       return do_put;
28278 +}
28279 +
28280 +/* free the plinks on a branch specified by @br_id */
28281 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28282 +{
28283 +       struct au_sbinfo *sbinfo;
28284 +       struct hlist_bl_head *hbl;
28285 +       struct hlist_bl_node *pos, *tmp;
28286 +       struct au_icntnr *icntnr;
28287 +       struct inode *inode;
28288 +       int i, do_put;
28289 +
28290 +       SiMustWriteLock(sb);
28291 +
28292 +       sbinfo = au_sbi(sb);
28293 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28294 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28295 +
28296 +       /* no bit_lock since sbinfo is write-locked */
28297 +       for (i = 0; i < AuPlink_NHASH; i++) {
28298 +               hbl = sbinfo->si_plink + i;
28299 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28300 +                       inode = au_igrab(&icntnr->vfs_inode);
28301 +                       ii_write_lock_child(inode);
28302 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28303 +                       if (do_put) {
28304 +                               hlist_bl_del(&icntnr->plink);
28305 +                               iput(inode);
28306 +                       }
28307 +                       ii_write_unlock(inode);
28308 +                       iput(inode);
28309 +               }
28310 +       }
28311 +}
28312 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28313 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28314 +++ linux/fs/aufs/poll.c        2020-01-27 10:57:18.175538316 +0100
28315 @@ -0,0 +1,51 @@
28316 +// SPDX-License-Identifier: GPL-2.0
28317 +/*
28318 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28319 + *
28320 + * This program, aufs is free software; you can redistribute it and/or modify
28321 + * it under the terms of the GNU General Public License as published by
28322 + * the Free Software Foundation; either version 2 of the License, or
28323 + * (at your option) any later version.
28324 + *
28325 + * This program is distributed in the hope that it will be useful,
28326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28328 + * GNU General Public License for more details.
28329 + *
28330 + * You should have received a copy of the GNU General Public License
28331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28332 + */
28333 +
28334 +/*
28335 + * poll operation
28336 + * There is only one filesystem which implements ->poll operation, currently.
28337 + */
28338 +
28339 +#include "aufs.h"
28340 +
28341 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28342 +{
28343 +       __poll_t mask;
28344 +       struct file *h_file;
28345 +       struct super_block *sb;
28346 +
28347 +       /* We should pretend an error happened. */
28348 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28349 +       sb = file->f_path.dentry->d_sb;
28350 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28351 +
28352 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28353 +       if (IS_ERR(h_file)) {
28354 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28355 +               goto out;
28356 +       }
28357 +
28358 +       mask = vfs_poll(h_file, pt);
28359 +       fput(h_file); /* instead of au_read_post() */
28360 +
28361 +out:
28362 +       si_read_unlock(sb);
28363 +       if (mask & EPOLLERR)
28364 +               AuDbg("mask 0x%x\n", mask);
28365 +       return mask;
28366 +}
28367 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28368 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28369 +++ linux/fs/aufs/posix_acl.c   2020-01-27 10:57:18.175538316 +0100
28370 @@ -0,0 +1,105 @@
28371 +// SPDX-License-Identifier: GPL-2.0
28372 +/*
28373 + * Copyright (C) 2014-2020 Junjiro R. Okajima
28374 + *
28375 + * This program, aufs is free software; you can redistribute it and/or modify
28376 + * it under the terms of the GNU General Public License as published by
28377 + * the Free Software Foundation; either version 2 of the License, or
28378 + * (at your option) any later version.
28379 + *
28380 + * This program is distributed in the hope that it will be useful,
28381 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28382 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28383 + * GNU General Public License for more details.
28384 + *
28385 + * You should have received a copy of the GNU General Public License
28386 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28387 + */
28388 +
28389 +/*
28390 + * posix acl operations
28391 + */
28392 +
28393 +#include <linux/fs.h>
28394 +#include "aufs.h"
28395 +
28396 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28397 +{
28398 +       struct posix_acl *acl;
28399 +       int err;
28400 +       aufs_bindex_t bindex;
28401 +       struct inode *h_inode;
28402 +       struct super_block *sb;
28403 +
28404 +       acl = NULL;
28405 +       sb = inode->i_sb;
28406 +       si_read_lock(sb, AuLock_FLUSH);
28407 +       ii_read_lock_child(inode);
28408 +       if (!(sb->s_flags & SB_POSIXACL))
28409 +               goto out;
28410 +
28411 +       bindex = au_ibtop(inode);
28412 +       h_inode = au_h_iptr(inode, bindex);
28413 +       if (unlikely(!h_inode
28414 +                    || ((h_inode->i_mode & S_IFMT)
28415 +                        != (inode->i_mode & S_IFMT)))) {
28416 +               err = au_busy_or_stale();
28417 +               acl = ERR_PTR(err);
28418 +               goto out;
28419 +       }
28420 +
28421 +       /* always topmost only */
28422 +       acl = get_acl(h_inode, type);
28423 +       if (IS_ERR(acl))
28424 +               forget_cached_acl(inode, type);
28425 +       else
28426 +               set_cached_acl(inode, type, acl);
28427 +
28428 +out:
28429 +       ii_read_unlock(inode);
28430 +       si_read_unlock(sb);
28431 +
28432 +       AuTraceErrPtr(acl);
28433 +       return acl;
28434 +}
28435 +
28436 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28437 +{
28438 +       int err;
28439 +       ssize_t ssz;
28440 +       struct dentry *dentry;
28441 +       struct au_sxattr arg = {
28442 +               .type = AU_ACL_SET,
28443 +               .u.acl_set = {
28444 +                       .acl    = acl,
28445 +                       .type   = type
28446 +               },
28447 +       };
28448 +
28449 +       IMustLock(inode);
28450 +
28451 +       if (inode->i_ino == AUFS_ROOT_INO)
28452 +               dentry = dget(inode->i_sb->s_root);
28453 +       else {
28454 +               dentry = d_find_alias(inode);
28455 +               if (!dentry)
28456 +                       dentry = d_find_any_alias(inode);
28457 +               if (!dentry) {
28458 +                       pr_warn("cannot handle this inode, "
28459 +                               "please report to aufs-users ML\n");
28460 +                       err = -ENOENT;
28461 +                       goto out;
28462 +               }
28463 +       }
28464 +
28465 +       ssz = au_sxattr(dentry, inode, &arg);
28466 +       /* forget even it if succeeds since the branch might set differently */
28467 +       forget_cached_acl(inode, type);
28468 +       dput(dentry);
28469 +       err = ssz;
28470 +       if (ssz >= 0)
28471 +               err = 0;
28472 +
28473 +out:
28474 +       return err;
28475 +}
28476 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28477 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28478 +++ linux/fs/aufs/procfs.c      2020-01-27 10:57:18.175538316 +0100
28479 @@ -0,0 +1,171 @@
28480 +// SPDX-License-Identifier: GPL-2.0
28481 +/*
28482 + * Copyright (C) 2010-2020 Junjiro R. Okajima
28483 + *
28484 + * This program, aufs is free software; you can redistribute it and/or modify
28485 + * it under the terms of the GNU General Public License as published by
28486 + * the Free Software Foundation; either version 2 of the License, or
28487 + * (at your option) any later version.
28488 + *
28489 + * This program is distributed in the hope that it will be useful,
28490 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28491 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28492 + * GNU General Public License for more details.
28493 + *
28494 + * You should have received a copy of the GNU General Public License
28495 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28496 + */
28497 +
28498 +/*
28499 + * procfs interfaces
28500 + */
28501 +
28502 +#include <linux/proc_fs.h>
28503 +#include "aufs.h"
28504 +
28505 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28506 +{
28507 +       struct au_sbinfo *sbinfo;
28508 +
28509 +       sbinfo = file->private_data;
28510 +       if (sbinfo) {
28511 +               au_plink_maint_leave(sbinfo);
28512 +               kobject_put(&sbinfo->si_kobj);
28513 +       }
28514 +
28515 +       return 0;
28516 +}
28517 +
28518 +static void au_procfs_plm_write_clean(struct file *file)
28519 +{
28520 +       struct au_sbinfo *sbinfo;
28521 +
28522 +       sbinfo = file->private_data;
28523 +       if (sbinfo)
28524 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28525 +}
28526 +
28527 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28528 +{
28529 +       int err;
28530 +       struct super_block *sb;
28531 +       struct au_sbinfo *sbinfo;
28532 +       struct hlist_bl_node *pos;
28533 +
28534 +       err = -EBUSY;
28535 +       if (unlikely(file->private_data))
28536 +               goto out;
28537 +
28538 +       sb = NULL;
28539 +       /* don't use au_sbilist_lock() here */
28540 +       hlist_bl_lock(&au_sbilist);
28541 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28542 +               if (id == sysaufs_si_id(sbinfo)) {
28543 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28544 +                               sb = sbinfo->si_sb;
28545 +                       break;
28546 +               }
28547 +       hlist_bl_unlock(&au_sbilist);
28548 +
28549 +       err = -EINVAL;
28550 +       if (unlikely(!sb))
28551 +               goto out;
28552 +
28553 +       err = au_plink_maint_enter(sb);
28554 +       if (!err)
28555 +               /* keep kobject_get() */
28556 +               file->private_data = sbinfo;
28557 +       else
28558 +               kobject_put(&sbinfo->si_kobj);
28559 +out:
28560 +       return err;
28561 +}
28562 +
28563 +/*
28564 + * Accept a valid "si=xxxx" only.
28565 + * Once it is accepted successfully, accept "clean" too.
28566 + */
28567 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28568 +                                  size_t count, loff_t *ppos)
28569 +{
28570 +       ssize_t err;
28571 +       unsigned long id;
28572 +       /* last newline is allowed */
28573 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28574 +
28575 +       err = -EACCES;
28576 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28577 +               goto out;
28578 +
28579 +       err = -EINVAL;
28580 +       if (unlikely(count > sizeof(buf)))
28581 +               goto out;
28582 +
28583 +       err = copy_from_user(buf, ubuf, count);
28584 +       if (unlikely(err)) {
28585 +               err = -EFAULT;
28586 +               goto out;
28587 +       }
28588 +       buf[count] = 0;
28589 +
28590 +       err = -EINVAL;
28591 +       if (!strcmp("clean", buf)) {
28592 +               au_procfs_plm_write_clean(file);
28593 +               goto out_success;
28594 +       } else if (unlikely(strncmp("si=", buf, 3)))
28595 +               goto out;
28596 +
28597 +       err = kstrtoul(buf + 3, 16, &id);
28598 +       if (unlikely(err))
28599 +               goto out;
28600 +
28601 +       err = au_procfs_plm_write_si(file, id);
28602 +       if (unlikely(err))
28603 +               goto out;
28604 +
28605 +out_success:
28606 +       err = count; /* success */
28607 +out:
28608 +       return err;
28609 +}
28610 +
28611 +static const struct file_operations au_procfs_plm_fop = {
28612 +       .write          = au_procfs_plm_write,
28613 +       .release        = au_procfs_plm_release,
28614 +       .owner          = THIS_MODULE
28615 +};
28616 +
28617 +/* ---------------------------------------------------------------------- */
28618 +
28619 +static struct proc_dir_entry *au_procfs_dir;
28620 +
28621 +void au_procfs_fin(void)
28622 +{
28623 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28624 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28625 +}
28626 +
28627 +int __init au_procfs_init(void)
28628 +{
28629 +       int err;
28630 +       struct proc_dir_entry *entry;
28631 +
28632 +       err = -ENOMEM;
28633 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28634 +       if (unlikely(!au_procfs_dir))
28635 +               goto out;
28636 +
28637 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28638 +                           au_procfs_dir, &au_procfs_plm_fop);
28639 +       if (unlikely(!entry))
28640 +               goto out_dir;
28641 +
28642 +       err = 0;
28643 +       goto out; /* success */
28644 +
28645 +
28646 +out_dir:
28647 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28648 +out:
28649 +       return err;
28650 +}
28651 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28652 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28653 +++ linux/fs/aufs/rdu.c 2020-01-27 10:57:18.178871751 +0100
28654 @@ -0,0 +1,384 @@
28655 +// SPDX-License-Identifier: GPL-2.0
28656 +/*
28657 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28658 + *
28659 + * This program, aufs is free software; you can redistribute it and/or modify
28660 + * it under the terms of the GNU General Public License as published by
28661 + * the Free Software Foundation; either version 2 of the License, or
28662 + * (at your option) any later version.
28663 + *
28664 + * This program is distributed in the hope that it will be useful,
28665 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28666 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28667 + * GNU General Public License for more details.
28668 + *
28669 + * You should have received a copy of the GNU General Public License
28670 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28671 + */
28672 +
28673 +/*
28674 + * readdir in userspace.
28675 + */
28676 +
28677 +#include <linux/compat.h>
28678 +#include <linux/fs_stack.h>
28679 +#include <linux/security.h>
28680 +#include "aufs.h"
28681 +
28682 +/* bits for struct aufs_rdu.flags */
28683 +#define        AuRdu_CALLED    1
28684 +#define        AuRdu_CONT      (1 << 1)
28685 +#define        AuRdu_FULL      (1 << 2)
28686 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28687 +#define au_fset_rdu(flags, name) \
28688 +       do { (flags) |= AuRdu_##name; } while (0)
28689 +#define au_fclr_rdu(flags, name) \
28690 +       do { (flags) &= ~AuRdu_##name; } while (0)
28691 +
28692 +struct au_rdu_arg {
28693 +       struct dir_context              ctx;
28694 +       struct aufs_rdu                 *rdu;
28695 +       union au_rdu_ent_ul             ent;
28696 +       unsigned long                   end;
28697 +
28698 +       struct super_block              *sb;
28699 +       int                             err;
28700 +};
28701 +
28702 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28703 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28704 +{
28705 +       int err, len;
28706 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28707 +       struct aufs_rdu *rdu = arg->rdu;
28708 +       struct au_rdu_ent ent;
28709 +
28710 +       err = 0;
28711 +       arg->err = 0;
28712 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28713 +       len = au_rdu_len(nlen);
28714 +       if (arg->ent.ul + len  < arg->end) {
28715 +               ent.ino = h_ino;
28716 +               ent.bindex = rdu->cookie.bindex;
28717 +               ent.type = d_type;
28718 +               ent.nlen = nlen;
28719 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28720 +                       ent.type = DT_UNKNOWN;
28721 +
28722 +               /* unnecessary to support mmap_sem since this is a dir */
28723 +               err = -EFAULT;
28724 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28725 +                       goto out;
28726 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28727 +                       goto out;
28728 +               /* the terminating NULL */
28729 +               if (__put_user(0, arg->ent.e->name + nlen))
28730 +                       goto out;
28731 +               err = 0;
28732 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28733 +               arg->ent.ul += len;
28734 +               rdu->rent++;
28735 +       } else {
28736 +               err = -EFAULT;
28737 +               au_fset_rdu(rdu->cookie.flags, FULL);
28738 +               rdu->full = 1;
28739 +               rdu->tail = arg->ent;
28740 +       }
28741 +
28742 +out:
28743 +       /* AuTraceErr(err); */
28744 +       return err;
28745 +}
28746 +
28747 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28748 +{
28749 +       int err;
28750 +       loff_t offset;
28751 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28752 +
28753 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28754 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28755 +       err = offset;
28756 +       if (unlikely(offset != cookie->h_pos))
28757 +               goto out;
28758 +
28759 +       err = 0;
28760 +       do {
28761 +               arg->err = 0;
28762 +               au_fclr_rdu(cookie->flags, CALLED);
28763 +               /* smp_mb(); */
28764 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28765 +               if (err >= 0)
28766 +                       err = arg->err;
28767 +       } while (!err
28768 +                && au_ftest_rdu(cookie->flags, CALLED)
28769 +                && !au_ftest_rdu(cookie->flags, FULL));
28770 +       cookie->h_pos = h_file->f_pos;
28771 +
28772 +out:
28773 +       AuTraceErr(err);
28774 +       return err;
28775 +}
28776 +
28777 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28778 +{
28779 +       int err;
28780 +       aufs_bindex_t bbot;
28781 +       struct au_rdu_arg arg = {
28782 +               .ctx = {
28783 +                       .actor = au_rdu_fill
28784 +               }
28785 +       };
28786 +       struct dentry *dentry;
28787 +       struct inode *inode;
28788 +       struct file *h_file;
28789 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28790 +
28791 +       /* VERIFY_WRITE */
28792 +       err = !access_ok(rdu->ent.e, rdu->sz);
28793 +       if (unlikely(err)) {
28794 +               err = -EFAULT;
28795 +               AuTraceErr(err);
28796 +               goto out;
28797 +       }
28798 +       rdu->rent = 0;
28799 +       rdu->tail = rdu->ent;
28800 +       rdu->full = 0;
28801 +       arg.rdu = rdu;
28802 +       arg.ent = rdu->ent;
28803 +       arg.end = arg.ent.ul;
28804 +       arg.end += rdu->sz;
28805 +
28806 +       err = -ENOTDIR;
28807 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28808 +               goto out;
28809 +
28810 +       err = security_file_permission(file, MAY_READ);
28811 +       AuTraceErr(err);
28812 +       if (unlikely(err))
28813 +               goto out;
28814 +
28815 +       dentry = file->f_path.dentry;
28816 +       inode = d_inode(dentry);
28817 +       inode_lock_shared(inode);
28818 +
28819 +       arg.sb = inode->i_sb;
28820 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28821 +       if (unlikely(err))
28822 +               goto out_mtx;
28823 +       err = au_alive_dir(dentry);
28824 +       if (unlikely(err))
28825 +               goto out_si;
28826 +       /* todo: reval? */
28827 +       fi_read_lock(file);
28828 +
28829 +       err = -EAGAIN;
28830 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28831 +                    && cookie->generation != au_figen(file)))
28832 +               goto out_unlock;
28833 +
28834 +       err = 0;
28835 +       if (!rdu->blk) {
28836 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28837 +               if (!rdu->blk)
28838 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28839 +       }
28840 +       bbot = au_fbtop(file);
28841 +       if (cookie->bindex < bbot)
28842 +               cookie->bindex = bbot;
28843 +       bbot = au_fbbot_dir(file);
28844 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28845 +       for (; !err && cookie->bindex <= bbot;
28846 +            cookie->bindex++, cookie->h_pos = 0) {
28847 +               h_file = au_hf_dir(file, cookie->bindex);
28848 +               if (!h_file)
28849 +                       continue;
28850 +
28851 +               au_fclr_rdu(cookie->flags, FULL);
28852 +               err = au_rdu_do(h_file, &arg);
28853 +               AuTraceErr(err);
28854 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28855 +                       break;
28856 +       }
28857 +       AuDbg("rent %llu\n", rdu->rent);
28858 +
28859 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28860 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28861 +               au_fset_rdu(cookie->flags, CONT);
28862 +               cookie->generation = au_figen(file);
28863 +       }
28864 +
28865 +       ii_read_lock_child(inode);
28866 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28867 +       ii_read_unlock(inode);
28868 +
28869 +out_unlock:
28870 +       fi_read_unlock(file);
28871 +out_si:
28872 +       si_read_unlock(arg.sb);
28873 +out_mtx:
28874 +       inode_unlock_shared(inode);
28875 +out:
28876 +       AuTraceErr(err);
28877 +       return err;
28878 +}
28879 +
28880 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28881 +{
28882 +       int err;
28883 +       ino_t ino;
28884 +       unsigned long long nent;
28885 +       union au_rdu_ent_ul *u;
28886 +       struct au_rdu_ent ent;
28887 +       struct super_block *sb;
28888 +
28889 +       err = 0;
28890 +       nent = rdu->nent;
28891 +       u = &rdu->ent;
28892 +       sb = file->f_path.dentry->d_sb;
28893 +       si_read_lock(sb, AuLock_FLUSH);
28894 +       while (nent-- > 0) {
28895 +               /* unnecessary to support mmap_sem since this is a dir */
28896 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28897 +               if (!err)
28898 +                       /* VERIFY_WRITE */
28899 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28900 +               if (unlikely(err)) {
28901 +                       err = -EFAULT;
28902 +                       AuTraceErr(err);
28903 +                       break;
28904 +               }
28905 +
28906 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28907 +               if (!ent.wh)
28908 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28909 +               else
28910 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28911 +                                       &ino);
28912 +               if (unlikely(err)) {
28913 +                       AuTraceErr(err);
28914 +                       break;
28915 +               }
28916 +
28917 +               err = __put_user(ino, &u->e->ino);
28918 +               if (unlikely(err)) {
28919 +                       err = -EFAULT;
28920 +                       AuTraceErr(err);
28921 +                       break;
28922 +               }
28923 +               u->ul += au_rdu_len(ent.nlen);
28924 +       }
28925 +       si_read_unlock(sb);
28926 +
28927 +       return err;
28928 +}
28929 +
28930 +/* ---------------------------------------------------------------------- */
28931 +
28932 +static int au_rdu_verify(struct aufs_rdu *rdu)
28933 +{
28934 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28935 +             "%llu, b%d, 0x%x, g%u}\n",
28936 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28937 +             rdu->blk,
28938 +             rdu->rent, rdu->shwh, rdu->full,
28939 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28940 +             rdu->cookie.generation);
28941 +
28942 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28943 +               return 0;
28944 +
28945 +       AuDbg("%u:%u\n",
28946 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28947 +       return -EINVAL;
28948 +}
28949 +
28950 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28951 +{
28952 +       long err, e;
28953 +       struct aufs_rdu rdu;
28954 +       void __user *p = (void __user *)arg;
28955 +
28956 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28957 +       if (unlikely(err)) {
28958 +               err = -EFAULT;
28959 +               AuTraceErr(err);
28960 +               goto out;
28961 +       }
28962 +       err = au_rdu_verify(&rdu);
28963 +       if (unlikely(err))
28964 +               goto out;
28965 +
28966 +       switch (cmd) {
28967 +       case AUFS_CTL_RDU:
28968 +               err = au_rdu(file, &rdu);
28969 +               if (unlikely(err))
28970 +                       break;
28971 +
28972 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28973 +               if (unlikely(e)) {
28974 +                       err = -EFAULT;
28975 +                       AuTraceErr(err);
28976 +               }
28977 +               break;
28978 +       case AUFS_CTL_RDU_INO:
28979 +               err = au_rdu_ino(file, &rdu);
28980 +               break;
28981 +
28982 +       default:
28983 +               /* err = -ENOTTY; */
28984 +               err = -EINVAL;
28985 +       }
28986 +
28987 +out:
28988 +       AuTraceErr(err);
28989 +       return err;
28990 +}
28991 +
28992 +#ifdef CONFIG_COMPAT
28993 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28994 +{
28995 +       long err, e;
28996 +       struct aufs_rdu rdu;
28997 +       void __user *p = compat_ptr(arg);
28998 +
28999 +       /* todo: get_user()? */
29000 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29001 +       if (unlikely(err)) {
29002 +               err = -EFAULT;
29003 +               AuTraceErr(err);
29004 +               goto out;
29005 +       }
29006 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29007 +       err = au_rdu_verify(&rdu);
29008 +       if (unlikely(err))
29009 +               goto out;
29010 +
29011 +       switch (cmd) {
29012 +       case AUFS_CTL_RDU:
29013 +               err = au_rdu(file, &rdu);
29014 +               if (unlikely(err))
29015 +                       break;
29016 +
29017 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29018 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29019 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29020 +               if (unlikely(e)) {
29021 +                       err = -EFAULT;
29022 +                       AuTraceErr(err);
29023 +               }
29024 +               break;
29025 +       case AUFS_CTL_RDU_INO:
29026 +               err = au_rdu_ino(file, &rdu);
29027 +               break;
29028 +
29029 +       default:
29030 +               /* err = -ENOTTY; */
29031 +               err = -EINVAL;
29032 +       }
29033 +
29034 +out:
29035 +       AuTraceErr(err);
29036 +       return err;
29037 +}
29038 +#endif
29039 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29040 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29041 +++ linux/fs/aufs/rwsem.h       2020-01-27 10:57:18.178871751 +0100
29042 @@ -0,0 +1,73 @@
29043 +/* SPDX-License-Identifier: GPL-2.0 */
29044 +/*
29045 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29046 + *
29047 + * This program, aufs is free software; you can redistribute it and/or modify
29048 + * it under the terms of the GNU General Public License as published by
29049 + * the Free Software Foundation; either version 2 of the License, or
29050 + * (at your option) any later version.
29051 + *
29052 + * This program is distributed in the hope that it will be useful,
29053 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29054 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29055 + * GNU General Public License for more details.
29056 + *
29057 + * You should have received a copy of the GNU General Public License
29058 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29059 + */
29060 +
29061 +/*
29062 + * simple read-write semaphore wrappers
29063 + */
29064 +
29065 +#ifndef __AUFS_RWSEM_H__
29066 +#define __AUFS_RWSEM_H__
29067 +
29068 +#ifdef __KERNEL__
29069 +
29070 +#include "debug.h"
29071 +
29072 +/* in the future, the name 'au_rwsem' will be totally gone */
29073 +#define au_rwsem       rw_semaphore
29074 +
29075 +/* to debug easier, do not make them inlined functions */
29076 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29077 +/* rwsem_is_locked() is unusable */
29078 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
29079 +                                         && debug_locks \
29080 +                                         && !lockdep_is_held_type(rw, 1))
29081 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
29082 +                                         && debug_locks \
29083 +                                         && !lockdep_is_held_type(rw, 0))
29084 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
29085 +                                         && debug_locks \
29086 +                                         && !lockdep_is_held(rw))
29087 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
29088 +                                         && debug_locks \
29089 +                                         && lockdep_is_held(rw))
29090 +
29091 +#define au_rw_init(rw) init_rwsem(rw)
29092 +
29093 +#define au_rw_init_wlock(rw) do {              \
29094 +               au_rw_init(rw);                 \
29095 +               down_write(rw);                 \
29096 +       } while (0)
29097 +
29098 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29099 +               au_rw_init(rw);                 \
29100 +               down_write_nested(rw, lsc);     \
29101 +       } while (0)
29102 +
29103 +#define au_rw_read_lock(rw)            down_read(rw)
29104 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29105 +#define au_rw_read_unlock(rw)          up_read(rw)
29106 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29107 +#define au_rw_write_lock(rw)           down_write(rw)
29108 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29109 +#define au_rw_write_unlock(rw)         up_write(rw)
29110 +/* why is not _nested version defined? */
29111 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29112 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29113 +
29114 +#endif /* __KERNEL__ */
29115 +#endif /* __AUFS_RWSEM_H__ */
29116 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29117 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29118 +++ linux/fs/aufs/sbinfo.c      2020-01-27 10:57:18.178871751 +0100
29119 @@ -0,0 +1,314 @@
29120 +// SPDX-License-Identifier: GPL-2.0
29121 +/*
29122 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29123 + *
29124 + * This program, aufs is free software; you can redistribute it and/or modify
29125 + * it under the terms of the GNU General Public License as published by
29126 + * the Free Software Foundation; either version 2 of the License, or
29127 + * (at your option) any later version.
29128 + *
29129 + * This program is distributed in the hope that it will be useful,
29130 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29131 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29132 + * GNU General Public License for more details.
29133 + *
29134 + * You should have received a copy of the GNU General Public License
29135 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29136 + */
29137 +
29138 +/*
29139 + * superblock private data
29140 + */
29141 +
29142 +#include <linux/iversion.h>
29143 +#include "aufs.h"
29144 +
29145 +/*
29146 + * they are necessary regardless sysfs is disabled.
29147 + */
29148 +void au_si_free(struct kobject *kobj)
29149 +{
29150 +       int i;
29151 +       struct au_sbinfo *sbinfo;
29152 +       char *locked __maybe_unused; /* debug only */
29153 +
29154 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29155 +       for (i = 0; i < AuPlink_NHASH; i++)
29156 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29157 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29158 +
29159 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29160 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29161 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29162 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29163 +
29164 +       dbgaufs_si_fin(sbinfo);
29165 +       au_rw_write_lock(&sbinfo->si_rwsem);
29166 +       au_br_free(sbinfo);
29167 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29168 +
29169 +       au_kfree_try_rcu(sbinfo->si_branch);
29170 +       mutex_destroy(&sbinfo->si_xib_mtx);
29171 +       AuRwDestroy(&sbinfo->si_rwsem);
29172 +
29173 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29174 +       /* si_nfiles is waited too */
29175 +       au_kfree_rcu(sbinfo);
29176 +}
29177 +
29178 +int au_si_alloc(struct super_block *sb)
29179 +{
29180 +       int err, i;
29181 +       struct au_sbinfo *sbinfo;
29182 +
29183 +       err = -ENOMEM;
29184 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29185 +       if (unlikely(!sbinfo))
29186 +               goto out;
29187 +
29188 +       /* will be reallocated separately */
29189 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29190 +       if (unlikely(!sbinfo->si_branch))
29191 +               goto out_sbinfo;
29192 +
29193 +       err = sysaufs_si_init(sbinfo);
29194 +       if (!err) {
29195 +               dbgaufs_si_null(sbinfo);
29196 +               err = dbgaufs_si_init(sbinfo);
29197 +               if (unlikely(err))
29198 +                       kobject_put(&sbinfo->si_kobj);
29199 +       }
29200 +       if (unlikely(err))
29201 +               goto out_br;
29202 +
29203 +       au_nwt_init(&sbinfo->si_nowait);
29204 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29205 +
29206 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29207 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29208 +
29209 +       sbinfo->si_bbot = -1;
29210 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29211 +
29212 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29213 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29214 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29215 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29216 +
29217 +       au_fhsm_init(sbinfo);
29218 +
29219 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29220 +
29221 +       sbinfo->si_xino_jiffy = jiffies;
29222 +       sbinfo->si_xino_expire
29223 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29224 +       mutex_init(&sbinfo->si_xib_mtx);
29225 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29226 +
29227 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29228 +
29229 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29230 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29231 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29232 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29233 +
29234 +       for (i = 0; i < AuPlink_NHASH; i++)
29235 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29236 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29237 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29238 +
29239 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29240 +
29241 +       /* with getattr by default */
29242 +       sbinfo->si_iop_array = aufs_iop;
29243 +
29244 +       /* leave other members for sysaufs and si_mnt. */
29245 +       sbinfo->si_sb = sb;
29246 +       sb->s_fs_info = sbinfo;
29247 +       si_pid_set(sb);
29248 +       return 0; /* success */
29249 +
29250 +out_br:
29251 +       au_kfree_try_rcu(sbinfo->si_branch);
29252 +out_sbinfo:
29253 +       au_kfree_rcu(sbinfo);
29254 +out:
29255 +       return err;
29256 +}
29257 +
29258 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29259 +{
29260 +       int err, sz;
29261 +       struct au_branch **brp;
29262 +
29263 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29264 +
29265 +       err = -ENOMEM;
29266 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29267 +       if (unlikely(!sz))
29268 +               sz = sizeof(*brp);
29269 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29270 +                          may_shrink);
29271 +       if (brp) {
29272 +               sbinfo->si_branch = brp;
29273 +               err = 0;
29274 +       }
29275 +
29276 +       return err;
29277 +}
29278 +
29279 +/* ---------------------------------------------------------------------- */
29280 +
29281 +unsigned int au_sigen_inc(struct super_block *sb)
29282 +{
29283 +       unsigned int gen;
29284 +       struct inode *inode;
29285 +
29286 +       SiMustWriteLock(sb);
29287 +
29288 +       gen = ++au_sbi(sb)->si_generation;
29289 +       au_update_digen(sb->s_root);
29290 +       inode = d_inode(sb->s_root);
29291 +       au_update_iigen(inode, /*half*/0);
29292 +       inode_inc_iversion(inode);
29293 +       return gen;
29294 +}
29295 +
29296 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29297 +{
29298 +       aufs_bindex_t br_id;
29299 +       int i;
29300 +       struct au_sbinfo *sbinfo;
29301 +
29302 +       SiMustWriteLock(sb);
29303 +
29304 +       sbinfo = au_sbi(sb);
29305 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29306 +               br_id = ++sbinfo->si_last_br_id;
29307 +               AuDebugOn(br_id < 0);
29308 +               if (br_id && au_br_index(sb, br_id) < 0)
29309 +                       return br_id;
29310 +       }
29311 +
29312 +       return -1;
29313 +}
29314 +
29315 +/* ---------------------------------------------------------------------- */
29316 +
29317 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29318 +int si_read_lock(struct super_block *sb, int flags)
29319 +{
29320 +       int err;
29321 +
29322 +       err = 0;
29323 +       if (au_ftest_lock(flags, FLUSH))
29324 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29325 +
29326 +       si_noflush_read_lock(sb);
29327 +       err = au_plink_maint(sb, flags);
29328 +       if (unlikely(err))
29329 +               si_read_unlock(sb);
29330 +
29331 +       return err;
29332 +}
29333 +
29334 +int si_write_lock(struct super_block *sb, int flags)
29335 +{
29336 +       int err;
29337 +
29338 +       if (au_ftest_lock(flags, FLUSH))
29339 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29340 +
29341 +       si_noflush_write_lock(sb);
29342 +       err = au_plink_maint(sb, flags);
29343 +       if (unlikely(err))
29344 +               si_write_unlock(sb);
29345 +
29346 +       return err;
29347 +}
29348 +
29349 +/* dentry and super_block lock. call at entry point */
29350 +int aufs_read_lock(struct dentry *dentry, int flags)
29351 +{
29352 +       int err;
29353 +       struct super_block *sb;
29354 +
29355 +       sb = dentry->d_sb;
29356 +       err = si_read_lock(sb, flags);
29357 +       if (unlikely(err))
29358 +               goto out;
29359 +
29360 +       if (au_ftest_lock(flags, DW))
29361 +               di_write_lock_child(dentry);
29362 +       else
29363 +               di_read_lock_child(dentry, flags);
29364 +
29365 +       if (au_ftest_lock(flags, GEN)) {
29366 +               err = au_digen_test(dentry, au_sigen(sb));
29367 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29368 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29369 +               else if (!err)
29370 +                       err = au_dbrange_test(dentry);
29371 +               if (unlikely(err))
29372 +                       aufs_read_unlock(dentry, flags);
29373 +       }
29374 +
29375 +out:
29376 +       return err;
29377 +}
29378 +
29379 +void aufs_read_unlock(struct dentry *dentry, int flags)
29380 +{
29381 +       if (au_ftest_lock(flags, DW))
29382 +               di_write_unlock(dentry);
29383 +       else
29384 +               di_read_unlock(dentry, flags);
29385 +       si_read_unlock(dentry->d_sb);
29386 +}
29387 +
29388 +void aufs_write_lock(struct dentry *dentry)
29389 +{
29390 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29391 +       di_write_lock_child(dentry);
29392 +}
29393 +
29394 +void aufs_write_unlock(struct dentry *dentry)
29395 +{
29396 +       di_write_unlock(dentry);
29397 +       si_write_unlock(dentry->d_sb);
29398 +}
29399 +
29400 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29401 +{
29402 +       int err;
29403 +       unsigned int sigen;
29404 +       struct super_block *sb;
29405 +
29406 +       sb = d1->d_sb;
29407 +       err = si_read_lock(sb, flags);
29408 +       if (unlikely(err))
29409 +               goto out;
29410 +
29411 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29412 +
29413 +       if (au_ftest_lock(flags, GEN)) {
29414 +               sigen = au_sigen(sb);
29415 +               err = au_digen_test(d1, sigen);
29416 +               AuDebugOn(!err && au_dbrange_test(d1));
29417 +               if (!err) {
29418 +                       err = au_digen_test(d2, sigen);
29419 +                       AuDebugOn(!err && au_dbrange_test(d2));
29420 +               }
29421 +               if (unlikely(err))
29422 +                       aufs_read_and_write_unlock2(d1, d2);
29423 +       }
29424 +
29425 +out:
29426 +       return err;
29427 +}
29428 +
29429 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29430 +{
29431 +       di_write_unlock2(d1, d2);
29432 +       si_read_unlock(d1->d_sb);
29433 +}
29434 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29435 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29436 +++ linux/fs/aufs/super.c       2020-01-27 10:57:18.178871751 +0100
29437 @@ -0,0 +1,1047 @@
29438 +// SPDX-License-Identifier: GPL-2.0
29439 +/*
29440 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29441 + *
29442 + * This program, aufs is free software; you can redistribute it and/or modify
29443 + * it under the terms of the GNU General Public License as published by
29444 + * the Free Software Foundation; either version 2 of the License, or
29445 + * (at your option) any later version.
29446 + *
29447 + * This program is distributed in the hope that it will be useful,
29448 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29449 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29450 + * GNU General Public License for more details.
29451 + *
29452 + * You should have received a copy of the GNU General Public License
29453 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29454 + */
29455 +
29456 +/*
29457 + * mount and super_block operations
29458 + */
29459 +
29460 +#include <linux/iversion.h>
29461 +#include <linux/mm.h>
29462 +#include <linux/seq_file.h>
29463 +#include <linux/statfs.h>
29464 +#include <linux/vmalloc.h>
29465 +#include "aufs.h"
29466 +
29467 +/*
29468 + * super_operations
29469 + */
29470 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29471 +{
29472 +       struct au_icntnr *c;
29473 +
29474 +       c = au_cache_alloc_icntnr();
29475 +       if (c) {
29476 +               au_icntnr_init(c);
29477 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29478 +               c->iinfo.ii_hinode = NULL;
29479 +               return &c->vfs_inode;
29480 +       }
29481 +       return NULL;
29482 +}
29483 +
29484 +static void aufs_destroy_inode(struct inode *inode)
29485 +{
29486 +       if (!au_is_bad_inode(inode))
29487 +               au_iinfo_fin(inode);
29488 +}
29489 +
29490 +static void aufs_free_inode(struct inode *inode)
29491 +{
29492 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29493 +}
29494 +
29495 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29496 +{
29497 +       struct inode *inode;
29498 +       int err;
29499 +
29500 +       inode = iget_locked(sb, ino);
29501 +       if (unlikely(!inode)) {
29502 +               inode = ERR_PTR(-ENOMEM);
29503 +               goto out;
29504 +       }
29505 +       if (!(inode->i_state & I_NEW))
29506 +               goto out;
29507 +
29508 +       err = au_xigen_new(inode);
29509 +       if (!err)
29510 +               err = au_iinfo_init(inode);
29511 +       if (!err)
29512 +               inode_inc_iversion(inode);
29513 +       else {
29514 +               iget_failed(inode);
29515 +               inode = ERR_PTR(err);
29516 +       }
29517 +
29518 +out:
29519 +       /* never return NULL */
29520 +       AuDebugOn(!inode);
29521 +       AuTraceErrPtr(inode);
29522 +       return inode;
29523 +}
29524 +
29525 +/* lock free root dinfo */
29526 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29527 +{
29528 +       int err;
29529 +       aufs_bindex_t bindex, bbot;
29530 +       struct path path;
29531 +       struct au_hdentry *hdp;
29532 +       struct au_branch *br;
29533 +       au_br_perm_str_t perm;
29534 +
29535 +       err = 0;
29536 +       bbot = au_sbbot(sb);
29537 +       bindex = 0;
29538 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29539 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29540 +               br = au_sbr(sb, bindex);
29541 +               path.mnt = au_br_mnt(br);
29542 +               path.dentry = hdp->hd_dentry;
29543 +               err = au_seq_path(seq, &path);
29544 +               if (!err) {
29545 +                       au_optstr_br_perm(&perm, br->br_perm);
29546 +                       seq_printf(seq, "=%s", perm.a);
29547 +                       if (bindex != bbot)
29548 +                               seq_putc(seq, ':');
29549 +               }
29550 +       }
29551 +       if (unlikely(err || seq_has_overflowed(seq)))
29552 +               err = -E2BIG;
29553 +
29554 +       return err;
29555 +}
29556 +
29557 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29558 +                      const char *append)
29559 +{
29560 +       char *p;
29561 +
29562 +       p = fmt;
29563 +       while (*pat != ':')
29564 +               *p++ = *pat++;
29565 +       *p++ = *pat++;
29566 +       strcpy(p, append);
29567 +       AuDebugOn(strlen(fmt) >= len);
29568 +}
29569 +
29570 +static void au_show_wbr_create(struct seq_file *m, int v,
29571 +                              struct au_sbinfo *sbinfo)
29572 +{
29573 +       const char *pat;
29574 +       char fmt[32];
29575 +       struct au_wbr_mfs *mfs;
29576 +
29577 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29578 +
29579 +       seq_puts(m, ",create=");
29580 +       pat = au_optstr_wbr_create(v);
29581 +       mfs = &sbinfo->si_wbr_mfs;
29582 +       switch (v) {
29583 +       case AuWbrCreate_TDP:
29584 +       case AuWbrCreate_RR:
29585 +       case AuWbrCreate_MFS:
29586 +       case AuWbrCreate_PMFS:
29587 +               seq_puts(m, pat);
29588 +               break;
29589 +       case AuWbrCreate_MFSRR:
29590 +       case AuWbrCreate_TDMFS:
29591 +       case AuWbrCreate_PMFSRR:
29592 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29593 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29594 +               break;
29595 +       case AuWbrCreate_MFSV:
29596 +       case AuWbrCreate_PMFSV:
29597 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29598 +               seq_printf(m, fmt,
29599 +                          jiffies_to_msecs(mfs->mfs_expire)
29600 +                          / MSEC_PER_SEC);
29601 +               break;
29602 +       case AuWbrCreate_MFSRRV:
29603 +       case AuWbrCreate_TDMFSV:
29604 +       case AuWbrCreate_PMFSRRV:
29605 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29606 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29607 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29608 +               break;
29609 +       default:
29610 +               BUG();
29611 +       }
29612 +}
29613 +
29614 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29615 +{
29616 +#ifdef CONFIG_SYSFS
29617 +       return 0;
29618 +#else
29619 +       int err;
29620 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29621 +       aufs_bindex_t bindex, brid;
29622 +       struct qstr *name;
29623 +       struct file *f;
29624 +       struct dentry *d, *h_root;
29625 +       struct au_branch *br;
29626 +
29627 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29628 +
29629 +       err = 0;
29630 +       f = au_sbi(sb)->si_xib;
29631 +       if (!f)
29632 +               goto out;
29633 +
29634 +       /* stop printing the default xino path on the first writable branch */
29635 +       h_root = NULL;
29636 +       bindex = au_xi_root(sb, f->f_path.dentry);
29637 +       if (bindex >= 0) {
29638 +               br = au_sbr_sb(sb, bindex);
29639 +               h_root = au_br_dentry(br);
29640 +       }
29641 +
29642 +       d = f->f_path.dentry;
29643 +       name = &d->d_name;
29644 +       /* safe ->d_parent because the file is unlinked */
29645 +       if (d->d_parent == h_root
29646 +           && name->len == len
29647 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29648 +               goto out;
29649 +
29650 +       seq_puts(seq, ",xino=");
29651 +       err = au_xino_path(seq, f);
29652 +
29653 +out:
29654 +       return err;
29655 +#endif
29656 +}
29657 +
29658 +/* seq_file will re-call me in case of too long string */
29659 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29660 +{
29661 +       int err;
29662 +       unsigned int mnt_flags, v;
29663 +       struct super_block *sb;
29664 +       struct au_sbinfo *sbinfo;
29665 +
29666 +#define AuBool(name, str) do { \
29667 +       v = au_opt_test(mnt_flags, name); \
29668 +       if (v != au_opt_test(AuOpt_Def, name)) \
29669 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29670 +} while (0)
29671 +
29672 +#define AuStr(name, str) do { \
29673 +       v = mnt_flags & AuOptMask_##name; \
29674 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29675 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29676 +} while (0)
29677 +
29678 +#define AuUInt(name, str, val) do { \
29679 +       if (val != AUFS_##name##_DEF) \
29680 +               seq_printf(m, "," #str "=%u", val); \
29681 +} while (0)
29682 +
29683 +       sb = dentry->d_sb;
29684 +       if (sb->s_flags & SB_POSIXACL)
29685 +               seq_puts(m, ",acl");
29686 +#if 0 /* reserved for future use */
29687 +       if (sb->s_flags & SB_I_VERSION)
29688 +               seq_puts(m, ",i_version");
29689 +#endif
29690 +
29691 +       /* lock free root dinfo */
29692 +       si_noflush_read_lock(sb);
29693 +       sbinfo = au_sbi(sb);
29694 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29695 +
29696 +       mnt_flags = au_mntflags(sb);
29697 +       if (au_opt_test(mnt_flags, XINO)) {
29698 +               err = au_show_xino(m, sb);
29699 +               if (unlikely(err))
29700 +                       goto out;
29701 +       } else
29702 +               seq_puts(m, ",noxino");
29703 +
29704 +       AuBool(TRUNC_XINO, trunc_xino);
29705 +       AuStr(UDBA, udba);
29706 +       AuBool(SHWH, shwh);
29707 +       AuBool(PLINK, plink);
29708 +       AuBool(DIO, dio);
29709 +       AuBool(DIRPERM1, dirperm1);
29710 +
29711 +       v = sbinfo->si_wbr_create;
29712 +       if (v != AuWbrCreate_Def)
29713 +               au_show_wbr_create(m, v, sbinfo);
29714 +
29715 +       v = sbinfo->si_wbr_copyup;
29716 +       if (v != AuWbrCopyup_Def)
29717 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29718 +
29719 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29720 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29721 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29722 +
29723 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29724 +
29725 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29726 +       AuUInt(RDCACHE, rdcache, v);
29727 +
29728 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29729 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29730 +
29731 +       au_fhsm_show(m, sbinfo);
29732 +
29733 +       AuBool(DIRREN, dirren);
29734 +       AuBool(SUM, sum);
29735 +       /* AuBool(SUM_W, wsum); */
29736 +       AuBool(WARN_PERM, warn_perm);
29737 +       AuBool(VERBOSE, verbose);
29738 +
29739 +out:
29740 +       /* be sure to print "br:" last */
29741 +       if (!sysaufs_brs) {
29742 +               seq_puts(m, ",br:");
29743 +               au_show_brs(m, sb);
29744 +       }
29745 +       si_read_unlock(sb);
29746 +       return 0;
29747 +
29748 +#undef AuBool
29749 +#undef AuStr
29750 +#undef AuUInt
29751 +}
29752 +
29753 +/* ---------------------------------------------------------------------- */
29754 +
29755 +/* sum mode which returns the summation for statfs(2) */
29756 +
29757 +static u64 au_add_till_max(u64 a, u64 b)
29758 +{
29759 +       u64 old;
29760 +
29761 +       old = a;
29762 +       a += b;
29763 +       if (old <= a)
29764 +               return a;
29765 +       return ULLONG_MAX;
29766 +}
29767 +
29768 +static u64 au_mul_till_max(u64 a, long mul)
29769 +{
29770 +       u64 old;
29771 +
29772 +       old = a;
29773 +       a *= mul;
29774 +       if (old <= a)
29775 +               return a;
29776 +       return ULLONG_MAX;
29777 +}
29778 +
29779 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29780 +{
29781 +       int err;
29782 +       long bsize, factor;
29783 +       u64 blocks, bfree, bavail, files, ffree;
29784 +       aufs_bindex_t bbot, bindex, i;
29785 +       unsigned char shared;
29786 +       struct path h_path;
29787 +       struct super_block *h_sb;
29788 +
29789 +       err = 0;
29790 +       bsize = LONG_MAX;
29791 +       files = 0;
29792 +       ffree = 0;
29793 +       blocks = 0;
29794 +       bfree = 0;
29795 +       bavail = 0;
29796 +       bbot = au_sbbot(sb);
29797 +       for (bindex = 0; bindex <= bbot; bindex++) {
29798 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29799 +               h_sb = h_path.mnt->mnt_sb;
29800 +               shared = 0;
29801 +               for (i = 0; !shared && i < bindex; i++)
29802 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29803 +               if (shared)
29804 +                       continue;
29805 +
29806 +               /* sb->s_root for NFS is unreliable */
29807 +               h_path.dentry = h_path.mnt->mnt_root;
29808 +               err = vfs_statfs(&h_path, buf);
29809 +               if (unlikely(err))
29810 +                       goto out;
29811 +
29812 +               if (bsize > buf->f_bsize) {
29813 +                       /*
29814 +                        * we will reduce bsize, so we have to expand blocks
29815 +                        * etc. to match them again
29816 +                        */
29817 +                       factor = (bsize / buf->f_bsize);
29818 +                       blocks = au_mul_till_max(blocks, factor);
29819 +                       bfree = au_mul_till_max(bfree, factor);
29820 +                       bavail = au_mul_till_max(bavail, factor);
29821 +                       bsize = buf->f_bsize;
29822 +               }
29823 +
29824 +               factor = (buf->f_bsize / bsize);
29825 +               blocks = au_add_till_max(blocks,
29826 +                               au_mul_till_max(buf->f_blocks, factor));
29827 +               bfree = au_add_till_max(bfree,
29828 +                               au_mul_till_max(buf->f_bfree, factor));
29829 +               bavail = au_add_till_max(bavail,
29830 +                               au_mul_till_max(buf->f_bavail, factor));
29831 +               files = au_add_till_max(files, buf->f_files);
29832 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29833 +       }
29834 +
29835 +       buf->f_bsize = bsize;
29836 +       buf->f_blocks = blocks;
29837 +       buf->f_bfree = bfree;
29838 +       buf->f_bavail = bavail;
29839 +       buf->f_files = files;
29840 +       buf->f_ffree = ffree;
29841 +       buf->f_frsize = 0;
29842 +
29843 +out:
29844 +       return err;
29845 +}
29846 +
29847 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29848 +{
29849 +       int err;
29850 +       struct path h_path;
29851 +       struct super_block *sb;
29852 +
29853 +       /* lock free root dinfo */
29854 +       sb = dentry->d_sb;
29855 +       si_noflush_read_lock(sb);
29856 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29857 +               /* sb->s_root for NFS is unreliable */
29858 +               h_path.mnt = au_sbr_mnt(sb, 0);
29859 +               h_path.dentry = h_path.mnt->mnt_root;
29860 +               err = vfs_statfs(&h_path, buf);
29861 +       } else
29862 +               err = au_statfs_sum(sb, buf);
29863 +       si_read_unlock(sb);
29864 +
29865 +       if (!err) {
29866 +               buf->f_type = AUFS_SUPER_MAGIC;
29867 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29868 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29869 +       }
29870 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29871 +
29872 +       return err;
29873 +}
29874 +
29875 +/* ---------------------------------------------------------------------- */
29876 +
29877 +static int aufs_sync_fs(struct super_block *sb, int wait)
29878 +{
29879 +       int err, e;
29880 +       aufs_bindex_t bbot, bindex;
29881 +       struct au_branch *br;
29882 +       struct super_block *h_sb;
29883 +
29884 +       err = 0;
29885 +       si_noflush_read_lock(sb);
29886 +       bbot = au_sbbot(sb);
29887 +       for (bindex = 0; bindex <= bbot; bindex++) {
29888 +               br = au_sbr(sb, bindex);
29889 +               if (!au_br_writable(br->br_perm))
29890 +                       continue;
29891 +
29892 +               h_sb = au_sbr_sb(sb, bindex);
29893 +               e = vfsub_sync_filesystem(h_sb, wait);
29894 +               if (unlikely(e && !err))
29895 +                       err = e;
29896 +               /* go on even if an error happens */
29897 +       }
29898 +       si_read_unlock(sb);
29899 +
29900 +       return err;
29901 +}
29902 +
29903 +/* ---------------------------------------------------------------------- */
29904 +
29905 +/* final actions when unmounting a file system */
29906 +static void aufs_put_super(struct super_block *sb)
29907 +{
29908 +       struct au_sbinfo *sbinfo;
29909 +
29910 +       sbinfo = au_sbi(sb);
29911 +       if (sbinfo)
29912 +               kobject_put(&sbinfo->si_kobj);
29913 +}
29914 +
29915 +/* ---------------------------------------------------------------------- */
29916 +
29917 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29918 +                    struct super_block *sb, void *arg)
29919 +{
29920 +       void *array;
29921 +       unsigned long long n, sz;
29922 +
29923 +       array = NULL;
29924 +       n = 0;
29925 +       if (!*hint)
29926 +               goto out;
29927 +
29928 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29929 +               array = ERR_PTR(-EMFILE);
29930 +               pr_err("hint %llu\n", *hint);
29931 +               goto out;
29932 +       }
29933 +
29934 +       sz = sizeof(array) * *hint;
29935 +       array = kzalloc(sz, GFP_NOFS);
29936 +       if (unlikely(!array))
29937 +               array = vzalloc(sz);
29938 +       if (unlikely(!array)) {
29939 +               array = ERR_PTR(-ENOMEM);
29940 +               goto out;
29941 +       }
29942 +
29943 +       n = cb(sb, array, *hint, arg);
29944 +       AuDebugOn(n > *hint);
29945 +
29946 +out:
29947 +       *hint = n;
29948 +       return array;
29949 +}
29950 +
29951 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29952 +                                      unsigned long long max __maybe_unused,
29953 +                                      void *arg)
29954 +{
29955 +       unsigned long long n;
29956 +       struct inode **p, *inode;
29957 +       struct list_head *head;
29958 +
29959 +       n = 0;
29960 +       p = a;
29961 +       head = arg;
29962 +       spin_lock(&sb->s_inode_list_lock);
29963 +       list_for_each_entry(inode, head, i_sb_list) {
29964 +               if (!au_is_bad_inode(inode)
29965 +                   && au_ii(inode)->ii_btop >= 0) {
29966 +                       spin_lock(&inode->i_lock);
29967 +                       if (atomic_read(&inode->i_count)) {
29968 +                               au_igrab(inode);
29969 +                               *p++ = inode;
29970 +                               n++;
29971 +                               AuDebugOn(n > max);
29972 +                       }
29973 +                       spin_unlock(&inode->i_lock);
29974 +               }
29975 +       }
29976 +       spin_unlock(&sb->s_inode_list_lock);
29977 +
29978 +       return n;
29979 +}
29980 +
29981 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29982 +{
29983 +       struct au_sbinfo *sbi;
29984 +
29985 +       sbi = au_sbi(sb);
29986 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29987 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29988 +}
29989 +
29990 +void au_iarray_free(struct inode **a, unsigned long long max)
29991 +{
29992 +       unsigned long long ull;
29993 +
29994 +       for (ull = 0; ull < max; ull++)
29995 +               iput(a[ull]);
29996 +       kvfree(a);
29997 +}
29998 +
29999 +/* ---------------------------------------------------------------------- */
30000 +
30001 +/*
30002 + * refresh dentry and inode at remount time.
30003 + */
30004 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30005 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30006 +                     struct dentry *parent)
30007 +{
30008 +       int err;
30009 +
30010 +       di_write_lock_child(dentry);
30011 +       di_read_lock_parent(parent, AuLock_IR);
30012 +       err = au_refresh_dentry(dentry, parent);
30013 +       if (!err && dir_flags)
30014 +               au_hn_reset(d_inode(dentry), dir_flags);
30015 +       di_read_unlock(parent, AuLock_IR);
30016 +       di_write_unlock(dentry);
30017 +
30018 +       return err;
30019 +}
30020 +
30021 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30022 +                          struct au_sbinfo *sbinfo,
30023 +                          const unsigned int dir_flags, unsigned int do_idop)
30024 +{
30025 +       int err;
30026 +       struct dentry *parent;
30027 +
30028 +       err = 0;
30029 +       parent = dget_parent(dentry);
30030 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30031 +               if (d_really_is_positive(dentry)) {
30032 +                       if (!d_is_dir(dentry))
30033 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30034 +                                                parent);
30035 +                       else {
30036 +                               err = au_do_refresh(dentry, dir_flags, parent);
30037 +                               if (unlikely(err))
30038 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30039 +                       }
30040 +               } else
30041 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30042 +               AuDbgDentry(dentry);
30043 +       }
30044 +       dput(parent);
30045 +
30046 +       if (!err) {
30047 +               if (do_idop)
30048 +                       au_refresh_dop(dentry, /*force_reval*/0);
30049 +       } else
30050 +               au_refresh_dop(dentry, /*force_reval*/1);
30051 +
30052 +       AuTraceErr(err);
30053 +       return err;
30054 +}
30055 +
30056 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30057 +{
30058 +       int err, i, j, ndentry, e;
30059 +       unsigned int sigen;
30060 +       struct au_dcsub_pages dpages;
30061 +       struct au_dpage *dpage;
30062 +       struct dentry **dentries, *d;
30063 +       struct au_sbinfo *sbinfo;
30064 +       struct dentry *root = sb->s_root;
30065 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30066 +
30067 +       if (do_idop)
30068 +               au_refresh_dop(root, /*force_reval*/0);
30069 +
30070 +       err = au_dpages_init(&dpages, GFP_NOFS);
30071 +       if (unlikely(err))
30072 +               goto out;
30073 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30074 +       if (unlikely(err))
30075 +               goto out_dpages;
30076 +
30077 +       sigen = au_sigen(sb);
30078 +       sbinfo = au_sbi(sb);
30079 +       for (i = 0; i < dpages.ndpage; i++) {
30080 +               dpage = dpages.dpages + i;
30081 +               dentries = dpage->dentries;
30082 +               ndentry = dpage->ndentry;
30083 +               for (j = 0; j < ndentry; j++) {
30084 +                       d = dentries[j];
30085 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30086 +                                           do_idop);
30087 +                       if (unlikely(e && !err))
30088 +                               err = e;
30089 +                       /* go on even err */
30090 +               }
30091 +       }
30092 +
30093 +out_dpages:
30094 +       au_dpages_free(&dpages);
30095 +out:
30096 +       return err;
30097 +}
30098 +
30099 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30100 +{
30101 +       int err, e;
30102 +       unsigned int sigen;
30103 +       unsigned long long max, ull;
30104 +       struct inode *inode, **array;
30105 +
30106 +       array = au_iarray_alloc(sb, &max);
30107 +       err = PTR_ERR(array);
30108 +       if (IS_ERR(array))
30109 +               goto out;
30110 +
30111 +       err = 0;
30112 +       sigen = au_sigen(sb);
30113 +       for (ull = 0; ull < max; ull++) {
30114 +               inode = array[ull];
30115 +               if (unlikely(!inode))
30116 +                       break;
30117 +
30118 +               e = 0;
30119 +               ii_write_lock_child(inode);
30120 +               if (au_iigen(inode, NULL) != sigen) {
30121 +                       e = au_refresh_hinode_self(inode);
30122 +                       if (unlikely(e)) {
30123 +                               au_refresh_iop(inode, /*force_getattr*/1);
30124 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30125 +                               if (!err)
30126 +                                       err = e;
30127 +                               /* go on even if err */
30128 +                       }
30129 +               }
30130 +               if (!e && do_idop)
30131 +                       au_refresh_iop(inode, /*force_getattr*/0);
30132 +               ii_write_unlock(inode);
30133 +       }
30134 +
30135 +       au_iarray_free(array, max);
30136 +
30137 +out:
30138 +       return err;
30139 +}
30140 +
30141 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30142 +{
30143 +       int err, e;
30144 +       unsigned int udba;
30145 +       aufs_bindex_t bindex, bbot;
30146 +       struct dentry *root;
30147 +       struct inode *inode;
30148 +       struct au_branch *br;
30149 +       struct au_sbinfo *sbi;
30150 +
30151 +       au_sigen_inc(sb);
30152 +       sbi = au_sbi(sb);
30153 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30154 +
30155 +       root = sb->s_root;
30156 +       DiMustNoWaiters(root);
30157 +       inode = d_inode(root);
30158 +       IiMustNoWaiters(inode);
30159 +
30160 +       udba = au_opt_udba(sb);
30161 +       bbot = au_sbbot(sb);
30162 +       for (bindex = 0; bindex <= bbot; bindex++) {
30163 +               br = au_sbr(sb, bindex);
30164 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30165 +               if (unlikely(err))
30166 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30167 +                               bindex, err);
30168 +               /* go on even if err */
30169 +       }
30170 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30171 +
30172 +       if (do_idop) {
30173 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30174 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30175 +                       sb->s_d_op = &aufs_dop_noreval;
30176 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30177 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30178 +               } else {
30179 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30180 +                       sb->s_d_op = &aufs_dop;
30181 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30182 +                       sbi->si_iop_array = aufs_iop;
30183 +               }
30184 +               pr_info("reset to %ps and %ps\n",
30185 +                       sb->s_d_op, sbi->si_iop_array);
30186 +       }
30187 +
30188 +       di_write_unlock(root);
30189 +       err = au_refresh_d(sb, do_idop);
30190 +       e = au_refresh_i(sb, do_idop);
30191 +       if (unlikely(e && !err))
30192 +               err = e;
30193 +       /* aufs_write_lock() calls ..._child() */
30194 +       di_write_lock_child(root);
30195 +
30196 +       au_cpup_attr_all(inode, /*force*/1);
30197 +
30198 +       if (unlikely(err))
30199 +               AuIOErr("refresh failed, ignored, %d\n", err);
30200 +}
30201 +
30202 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30203 +static int cvt_err(int err)
30204 +{
30205 +       AuTraceErr(err);
30206 +
30207 +       switch (err) {
30208 +       case -ENOENT:
30209 +       case -ENOTDIR:
30210 +       case -EEXIST:
30211 +       case -EIO:
30212 +               err = -EINVAL;
30213 +       }
30214 +       return err;
30215 +}
30216 +
30217 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30218 +{
30219 +       int err, do_dx;
30220 +       unsigned int mntflags;
30221 +       struct au_opts opts = {
30222 +               .opt = NULL
30223 +       };
30224 +       struct dentry *root;
30225 +       struct inode *inode;
30226 +       struct au_sbinfo *sbinfo;
30227 +
30228 +       err = 0;
30229 +       root = sb->s_root;
30230 +       if (!data || !*data) {
30231 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30232 +               if (!err) {
30233 +                       di_write_lock_child(root);
30234 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30235 +                       aufs_write_unlock(root);
30236 +               }
30237 +               goto out;
30238 +       }
30239 +
30240 +       err = -ENOMEM;
30241 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30242 +       if (unlikely(!opts.opt))
30243 +               goto out;
30244 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30245 +       opts.flags = AuOpts_REMOUNT;
30246 +       opts.sb_flags = *flags;
30247 +
30248 +       /* parse it before aufs lock */
30249 +       err = au_opts_parse(sb, data, &opts);
30250 +       if (unlikely(err))
30251 +               goto out_opts;
30252 +
30253 +       sbinfo = au_sbi(sb);
30254 +       inode = d_inode(root);
30255 +       inode_lock(inode);
30256 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30257 +       if (unlikely(err))
30258 +               goto out_mtx;
30259 +       di_write_lock_child(root);
30260 +
30261 +       /* au_opts_remount() may return an error */
30262 +       err = au_opts_remount(sb, &opts);
30263 +       au_opts_free(&opts);
30264 +
30265 +       if (au_ftest_opts(opts.flags, REFRESH))
30266 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30267 +
30268 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30269 +               mntflags = au_mntflags(sb);
30270 +               do_dx = !!au_opt_test(mntflags, DIO);
30271 +               au_dy_arefresh(do_dx);
30272 +       }
30273 +
30274 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30275 +       aufs_write_unlock(root);
30276 +
30277 +out_mtx:
30278 +       inode_unlock(inode);
30279 +out_opts:
30280 +       free_page((unsigned long)opts.opt);
30281 +out:
30282 +       err = cvt_err(err);
30283 +       AuTraceErr(err);
30284 +       return err;
30285 +}
30286 +
30287 +static const struct super_operations aufs_sop = {
30288 +       .alloc_inode    = aufs_alloc_inode,
30289 +       .destroy_inode  = aufs_destroy_inode,
30290 +       .free_inode     = aufs_free_inode,
30291 +       /* always deleting, no clearing */
30292 +       .drop_inode     = generic_delete_inode,
30293 +       .show_options   = aufs_show_options,
30294 +       .statfs         = aufs_statfs,
30295 +       .put_super      = aufs_put_super,
30296 +       .sync_fs        = aufs_sync_fs,
30297 +       .remount_fs     = aufs_remount_fs
30298 +};
30299 +
30300 +/* ---------------------------------------------------------------------- */
30301 +
30302 +static int alloc_root(struct super_block *sb)
30303 +{
30304 +       int err;
30305 +       struct inode *inode;
30306 +       struct dentry *root;
30307 +
30308 +       err = -ENOMEM;
30309 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30310 +       err = PTR_ERR(inode);
30311 +       if (IS_ERR(inode))
30312 +               goto out;
30313 +
30314 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30315 +       inode->i_fop = &aufs_dir_fop;
30316 +       inode->i_mode = S_IFDIR;
30317 +       set_nlink(inode, 2);
30318 +       unlock_new_inode(inode);
30319 +
30320 +       root = d_make_root(inode);
30321 +       if (unlikely(!root))
30322 +               goto out;
30323 +       err = PTR_ERR(root);
30324 +       if (IS_ERR(root))
30325 +               goto out;
30326 +
30327 +       err = au_di_init(root);
30328 +       if (!err) {
30329 +               sb->s_root = root;
30330 +               return 0; /* success */
30331 +       }
30332 +       dput(root);
30333 +
30334 +out:
30335 +       return err;
30336 +}
30337 +
30338 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30339 +                          int silent __maybe_unused)
30340 +{
30341 +       int err;
30342 +       struct au_opts opts = {
30343 +               .opt = NULL
30344 +       };
30345 +       struct au_sbinfo *sbinfo;
30346 +       struct dentry *root;
30347 +       struct inode *inode;
30348 +       char *arg = raw_data;
30349 +
30350 +       if (unlikely(!arg || !*arg)) {
30351 +               err = -EINVAL;
30352 +               pr_err("no arg\n");
30353 +               goto out;
30354 +       }
30355 +
30356 +       err = -ENOMEM;
30357 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30358 +       if (unlikely(!opts.opt))
30359 +               goto out;
30360 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30361 +       opts.sb_flags = sb->s_flags;
30362 +
30363 +       err = au_si_alloc(sb);
30364 +       if (unlikely(err))
30365 +               goto out_opts;
30366 +       sbinfo = au_sbi(sb);
30367 +
30368 +       /* all timestamps always follow the ones on the branch */
30369 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30370 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30371 +       sb->s_op = &aufs_sop;
30372 +       sb->s_d_op = &aufs_dop;
30373 +       sb->s_magic = AUFS_SUPER_MAGIC;
30374 +       sb->s_maxbytes = 0;
30375 +       sb->s_stack_depth = 1;
30376 +       au_export_init(sb);
30377 +       au_xattr_init(sb);
30378 +
30379 +       err = alloc_root(sb);
30380 +       if (unlikely(err)) {
30381 +               si_write_unlock(sb);
30382 +               goto out_info;
30383 +       }
30384 +       root = sb->s_root;
30385 +       inode = d_inode(root);
30386 +
30387 +       /*
30388 +        * actually we can parse options regardless aufs lock here.
30389 +        * but at remount time, parsing must be done before aufs lock.
30390 +        * so we follow the same rule.
30391 +        */
30392 +       ii_write_lock_parent(inode);
30393 +       aufs_write_unlock(root);
30394 +       err = au_opts_parse(sb, arg, &opts);
30395 +       if (unlikely(err))
30396 +               goto out_root;
30397 +
30398 +       /* lock vfs_inode first, then aufs. */
30399 +       inode_lock(inode);
30400 +       aufs_write_lock(root);
30401 +       err = au_opts_mount(sb, &opts);
30402 +       au_opts_free(&opts);
30403 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30404 +               sb->s_d_op = &aufs_dop_noreval;
30405 +               pr_info("%ps\n", sb->s_d_op);
30406 +               au_refresh_dop(root, /*force_reval*/0);
30407 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30408 +               au_refresh_iop(inode, /*force_getattr*/0);
30409 +       }
30410 +       aufs_write_unlock(root);
30411 +       inode_unlock(inode);
30412 +       if (!err)
30413 +               goto out_opts; /* success */
30414 +
30415 +out_root:
30416 +       dput(root);
30417 +       sb->s_root = NULL;
30418 +out_info:
30419 +       kobject_put(&sbinfo->si_kobj);
30420 +       sb->s_fs_info = NULL;
30421 +out_opts:
30422 +       free_page((unsigned long)opts.opt);
30423 +out:
30424 +       AuTraceErr(err);
30425 +       err = cvt_err(err);
30426 +       AuTraceErr(err);
30427 +       return err;
30428 +}
30429 +
30430 +/* ---------------------------------------------------------------------- */
30431 +
30432 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30433 +                                const char *dev_name __maybe_unused,
30434 +                                void *raw_data)
30435 +{
30436 +       struct dentry *root;
30437 +
30438 +       /* all timestamps always follow the ones on the branch */
30439 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30440 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30441 +       if (IS_ERR(root))
30442 +               goto out;
30443 +
30444 +       au_sbilist_add(root->d_sb);
30445 +
30446 +out:
30447 +       return root;
30448 +}
30449 +
30450 +static void aufs_kill_sb(struct super_block *sb)
30451 +{
30452 +       struct au_sbinfo *sbinfo;
30453 +
30454 +       sbinfo = au_sbi(sb);
30455 +       if (sbinfo) {
30456 +               au_sbilist_del(sb);
30457 +               aufs_write_lock(sb->s_root);
30458 +               au_fhsm_fin(sb);
30459 +               if (sbinfo->si_wbr_create_ops->fin)
30460 +                       sbinfo->si_wbr_create_ops->fin(sb);
30461 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30462 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30463 +                       au_remount_refresh(sb, /*do_idop*/0);
30464 +               }
30465 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30466 +                       au_plink_put(sb, /*verbose*/1);
30467 +               au_xino_clr(sb);
30468 +               au_dr_opt_flush(sb);
30469 +               sbinfo->si_sb = NULL;
30470 +               aufs_write_unlock(sb->s_root);
30471 +               au_nwt_flush(&sbinfo->si_nowait);
30472 +       }
30473 +       kill_anon_super(sb);
30474 +}
30475 +
30476 +struct file_system_type aufs_fs_type = {
30477 +       .name           = AUFS_FSTYPE,
30478 +       /* a race between rename and others */
30479 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30480 +       .mount          = aufs_mount,
30481 +       .kill_sb        = aufs_kill_sb,
30482 +       /* no need to __module_get() and module_put(). */
30483 +       .owner          = THIS_MODULE,
30484 +};
30485 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30486 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30487 +++ linux/fs/aufs/super.h       2020-01-27 10:57:18.178871751 +0100
30488 @@ -0,0 +1,589 @@
30489 +/* SPDX-License-Identifier: GPL-2.0 */
30490 +/*
30491 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30492 + *
30493 + * This program, aufs is free software; you can redistribute it and/or modify
30494 + * it under the terms of the GNU General Public License as published by
30495 + * the Free Software Foundation; either version 2 of the License, or
30496 + * (at your option) any later version.
30497 + *
30498 + * This program is distributed in the hope that it will be useful,
30499 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30500 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30501 + * GNU General Public License for more details.
30502 + *
30503 + * You should have received a copy of the GNU General Public License
30504 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30505 + */
30506 +
30507 +/*
30508 + * super_block operations
30509 + */
30510 +
30511 +#ifndef __AUFS_SUPER_H__
30512 +#define __AUFS_SUPER_H__
30513 +
30514 +#ifdef __KERNEL__
30515 +
30516 +#include <linux/fs.h>
30517 +#include <linux/kobject.h>
30518 +#include "hbl.h"
30519 +#include "lcnt.h"
30520 +#include "rwsem.h"
30521 +#include "wkq.h"
30522 +
30523 +/* policies to select one among multiple writable branches */
30524 +struct au_wbr_copyup_operations {
30525 +       int (*copyup)(struct dentry *dentry);
30526 +};
30527 +
30528 +#define AuWbr_DIR      1               /* target is a dir */
30529 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30530 +
30531 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30532 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30533 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30534 +
30535 +struct au_wbr_create_operations {
30536 +       int (*create)(struct dentry *dentry, unsigned int flags);
30537 +       int (*init)(struct super_block *sb);
30538 +       int (*fin)(struct super_block *sb);
30539 +};
30540 +
30541 +struct au_wbr_mfs {
30542 +       struct mutex    mfs_lock; /* protect this structure */
30543 +       unsigned long   mfs_jiffy;
30544 +       unsigned long   mfs_expire;
30545 +       aufs_bindex_t   mfs_bindex;
30546 +
30547 +       unsigned long long      mfsrr_bytes;
30548 +       unsigned long long      mfsrr_watermark;
30549 +};
30550 +
30551 +#define AuPlink_NHASH 100
30552 +static inline int au_plink_hash(ino_t ino)
30553 +{
30554 +       return ino % AuPlink_NHASH;
30555 +}
30556 +
30557 +/* File-based Hierarchical Storage Management */
30558 +struct au_fhsm {
30559 +#ifdef CONFIG_AUFS_FHSM
30560 +       /* allow only one process who can receive the notification */
30561 +       spinlock_t              fhsm_spin;
30562 +       pid_t                   fhsm_pid;
30563 +       wait_queue_head_t       fhsm_wqh;
30564 +       atomic_t                fhsm_readable;
30565 +
30566 +       /* these are protected by si_rwsem */
30567 +       unsigned long           fhsm_expire;
30568 +       aufs_bindex_t           fhsm_bottom;
30569 +#endif
30570 +};
30571 +
30572 +struct au_branch;
30573 +struct au_sbinfo {
30574 +       /* nowait tasks in the system-wide workqueue */
30575 +       struct au_nowait_tasks  si_nowait;
30576 +
30577 +       /*
30578 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30579 +        * rwsem for au_sbinfo is necessary.
30580 +        */
30581 +       struct au_rwsem         si_rwsem;
30582 +
30583 +       /*
30584 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30585 +        * remount.
30586 +        */
30587 +       au_lcnt_t               si_ninodes, si_nfiles;
30588 +
30589 +       /* branch management */
30590 +       unsigned int            si_generation;
30591 +
30592 +       /* see AuSi_ flags */
30593 +       unsigned char           au_si_status;
30594 +
30595 +       aufs_bindex_t           si_bbot;
30596 +
30597 +       /* dirty trick to keep br_id plus */
30598 +       unsigned int            si_last_br_id :
30599 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30600 +       struct au_branch        **si_branch;
30601 +
30602 +       /* policy to select a writable branch */
30603 +       unsigned char           si_wbr_copyup;
30604 +       unsigned char           si_wbr_create;
30605 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30606 +       struct au_wbr_create_operations *si_wbr_create_ops;
30607 +
30608 +       /* round robin */
30609 +       atomic_t                si_wbr_rr_next;
30610 +
30611 +       /* most free space */
30612 +       struct au_wbr_mfs       si_wbr_mfs;
30613 +
30614 +       /* File-based Hierarchical Storage Management */
30615 +       struct au_fhsm          si_fhsm;
30616 +
30617 +       /* mount flags */
30618 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30619 +       unsigned int            si_mntflags;
30620 +
30621 +       /* external inode number (bitmap and translation table) */
30622 +       vfs_readf_t             si_xread;
30623 +       vfs_writef_t            si_xwrite;
30624 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30625 +
30626 +       struct file             *si_xib;
30627 +       struct mutex            si_xib_mtx; /* protect xib members */
30628 +       unsigned long           *si_xib_buf;
30629 +       unsigned long           si_xib_last_pindex;
30630 +       int                     si_xib_next_bit;
30631 +
30632 +       unsigned long           si_xino_jiffy;
30633 +       unsigned long           si_xino_expire;
30634 +       /* reserved for future use */
30635 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30636 +
30637 +#ifdef CONFIG_AUFS_EXPORT
30638 +       /* i_generation */
30639 +       /* todo: make xigen file an array to support many inode numbers */
30640 +       struct file             *si_xigen;
30641 +       atomic_t                si_xigen_next;
30642 +#endif
30643 +
30644 +       /* dirty trick to support atomic_open */
30645 +       struct hlist_bl_head    si_aopen;
30646 +
30647 +       /* vdir parameters */
30648 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30649 +       unsigned int            si_rdblk;       /* deblk size */
30650 +       unsigned int            si_rdhash;      /* hash size */
30651 +
30652 +       /*
30653 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30654 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30655 +        * future fsck.aufs or kernel thread will remove them later.
30656 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30657 +        */
30658 +       unsigned int            si_dirwh;
30659 +
30660 +       /* pseudo_link list */
30661 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30662 +       wait_queue_head_t       si_plink_wq;
30663 +       spinlock_t              si_plink_maint_lock;
30664 +       pid_t                   si_plink_maint_pid;
30665 +
30666 +       /* file list */
30667 +       struct hlist_bl_head    si_files;
30668 +
30669 +       /* with/without getattr, brother of sb->s_d_op */
30670 +       const struct inode_operations *si_iop_array;
30671 +
30672 +       /*
30673 +        * sysfs and lifetime management.
30674 +        * this is not a small structure and it may be a waste of memory in case
30675 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30676 +        * but using sysfs is majority.
30677 +        */
30678 +       struct kobject          si_kobj;
30679 +#ifdef CONFIG_DEBUG_FS
30680 +       struct dentry            *si_dbgaufs;
30681 +       struct dentry            *si_dbgaufs_plink;
30682 +       struct dentry            *si_dbgaufs_xib;
30683 +#ifdef CONFIG_AUFS_EXPORT
30684 +       struct dentry            *si_dbgaufs_xigen;
30685 +#endif
30686 +#endif
30687 +
30688 +#ifdef CONFIG_AUFS_SBILIST
30689 +       struct hlist_bl_node    si_list;
30690 +#endif
30691 +
30692 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30693 +       struct super_block      *si_sb;
30694 +};
30695 +
30696 +/* sbinfo status flags */
30697 +/*
30698 + * set true when refresh_dirs() failed at remount time.
30699 + * then try refreshing dirs at access time again.
30700 + * if it is false, refreshing dirs at access time is unnecessary
30701 + */
30702 +#define AuSi_FAILED_REFRESH_DIR        1
30703 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30704 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30705 +
30706 +#ifndef CONFIG_AUFS_FHSM
30707 +#undef AuSi_FHSM
30708 +#define AuSi_FHSM              0
30709 +#endif
30710 +
30711 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30712 +                                          unsigned int flag)
30713 +{
30714 +       AuRwMustAnyLock(&sbi->si_rwsem);
30715 +       return sbi->au_si_status & flag;
30716 +}
30717 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30718 +#define au_fset_si(sbinfo, name) do { \
30719 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30720 +       (sbinfo)->au_si_status |= AuSi_##name; \
30721 +} while (0)
30722 +#define au_fclr_si(sbinfo, name) do { \
30723 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30724 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30725 +} while (0)
30726 +
30727 +/* ---------------------------------------------------------------------- */
30728 +
30729 +/* policy to select one among writable branches */
30730 +#define AuWbrCopyup(sbinfo, ...) \
30731 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30732 +#define AuWbrCreate(sbinfo, ...) \
30733 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30734 +
30735 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30736 +#define AuLock_DW              1               /* write-lock dentry */
30737 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30738 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30739 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30740 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30741 +                                               /* except RENAME_EXCHANGE */
30742 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30743 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30744 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30745 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30746 +#define au_fset_lock(flags, name) \
30747 +       do { (flags) |= AuLock_##name; } while (0)
30748 +#define au_fclr_lock(flags, name) \
30749 +       do { (flags) &= ~AuLock_##name; } while (0)
30750 +
30751 +/* ---------------------------------------------------------------------- */
30752 +
30753 +/* super.c */
30754 +extern struct file_system_type aufs_fs_type;
30755 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30756 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30757 +                                          unsigned long long max, void *arg);
30758 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30759 +                    struct super_block *sb, void *arg);
30760 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30761 +void au_iarray_free(struct inode **a, unsigned long long max);
30762 +
30763 +/* sbinfo.c */
30764 +void au_si_free(struct kobject *kobj);
30765 +int au_si_alloc(struct super_block *sb);
30766 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30767 +
30768 +unsigned int au_sigen_inc(struct super_block *sb);
30769 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30770 +
30771 +int si_read_lock(struct super_block *sb, int flags);
30772 +int si_write_lock(struct super_block *sb, int flags);
30773 +int aufs_read_lock(struct dentry *dentry, int flags);
30774 +void aufs_read_unlock(struct dentry *dentry, int flags);
30775 +void aufs_write_lock(struct dentry *dentry);
30776 +void aufs_write_unlock(struct dentry *dentry);
30777 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30778 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30779 +
30780 +/* wbr_policy.c */
30781 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30782 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30783 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30784 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30785 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30786 +
30787 +/* mvdown.c */
30788 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30789 +
30790 +#ifdef CONFIG_AUFS_FHSM
30791 +/* fhsm.c */
30792 +
30793 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30794 +{
30795 +       pid_t pid;
30796 +
30797 +       spin_lock(&fhsm->fhsm_spin);
30798 +       pid = fhsm->fhsm_pid;
30799 +       spin_unlock(&fhsm->fhsm_spin);
30800 +
30801 +       return pid;
30802 +}
30803 +
30804 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30805 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30806 +int au_fhsm_fd(struct super_block *sb, int oflags);
30807 +int au_fhsm_br_alloc(struct au_branch *br);
30808 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30809 +void au_fhsm_fin(struct super_block *sb);
30810 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30811 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30812 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30813 +#else
30814 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30815 +          int force)
30816 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30817 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30818 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30819 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30820 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30821 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30822 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30823 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30824 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30825 +#endif
30826 +
30827 +/* ---------------------------------------------------------------------- */
30828 +
30829 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30830 +{
30831 +       return sb->s_fs_info;
30832 +}
30833 +
30834 +/* ---------------------------------------------------------------------- */
30835 +
30836 +#ifdef CONFIG_AUFS_EXPORT
30837 +int au_test_nfsd(void);
30838 +void au_export_init(struct super_block *sb);
30839 +void au_xigen_inc(struct inode *inode);
30840 +int au_xigen_new(struct inode *inode);
30841 +int au_xigen_set(struct super_block *sb, struct path *path);
30842 +void au_xigen_clr(struct super_block *sb);
30843 +
30844 +static inline int au_busy_or_stale(void)
30845 +{
30846 +       if (!au_test_nfsd())
30847 +               return -EBUSY;
30848 +       return -ESTALE;
30849 +}
30850 +#else
30851 +AuStubInt0(au_test_nfsd, void)
30852 +AuStubVoid(au_export_init, struct super_block *sb)
30853 +AuStubVoid(au_xigen_inc, struct inode *inode)
30854 +AuStubInt0(au_xigen_new, struct inode *inode)
30855 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30856 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30857 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30858 +#endif /* CONFIG_AUFS_EXPORT */
30859 +
30860 +/* ---------------------------------------------------------------------- */
30861 +
30862 +#ifdef CONFIG_AUFS_SBILIST
30863 +/* module.c */
30864 +extern struct hlist_bl_head au_sbilist;
30865 +
30866 +static inline void au_sbilist_init(void)
30867 +{
30868 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30869 +}
30870 +
30871 +static inline void au_sbilist_add(struct super_block *sb)
30872 +{
30873 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30874 +}
30875 +
30876 +static inline void au_sbilist_del(struct super_block *sb)
30877 +{
30878 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30879 +}
30880 +
30881 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30882 +static inline void au_sbilist_lock(void)
30883 +{
30884 +       hlist_bl_lock(&au_sbilist);
30885 +}
30886 +
30887 +static inline void au_sbilist_unlock(void)
30888 +{
30889 +       hlist_bl_unlock(&au_sbilist);
30890 +}
30891 +#define AuGFP_SBILIST  GFP_ATOMIC
30892 +#else
30893 +AuStubVoid(au_sbilist_lock, void)
30894 +AuStubVoid(au_sbilist_unlock, void)
30895 +#define AuGFP_SBILIST  GFP_NOFS
30896 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30897 +#else
30898 +AuStubVoid(au_sbilist_init, void)
30899 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30900 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30901 +AuStubVoid(au_sbilist_lock, void)
30902 +AuStubVoid(au_sbilist_unlock, void)
30903 +#define AuGFP_SBILIST  GFP_NOFS
30904 +#endif
30905 +
30906 +/* ---------------------------------------------------------------------- */
30907 +
30908 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30909 +{
30910 +       /*
30911 +        * This function is a dynamic '__init' function actually,
30912 +        * so the tiny check for si_rwsem is unnecessary.
30913 +        */
30914 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30915 +#ifdef CONFIG_DEBUG_FS
30916 +       sbinfo->si_dbgaufs = NULL;
30917 +       sbinfo->si_dbgaufs_plink = NULL;
30918 +       sbinfo->si_dbgaufs_xib = NULL;
30919 +#ifdef CONFIG_AUFS_EXPORT
30920 +       sbinfo->si_dbgaufs_xigen = NULL;
30921 +#endif
30922 +#endif
30923 +}
30924 +
30925 +/* ---------------------------------------------------------------------- */
30926 +
30927 +/* current->atomic_flags */
30928 +/* this value should never corrupt the ones defined in linux/sched.h */
30929 +#define PFA_AUFS       0x10
30930 +
30931 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30932 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30933 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30934 +
30935 +static inline int si_pid_test(struct super_block *sb)
30936 +{
30937 +       return !!task_test_aufs(current);
30938 +}
30939 +
30940 +static inline void si_pid_clr(struct super_block *sb)
30941 +{
30942 +       AuDebugOn(!task_test_aufs(current));
30943 +       task_clear_aufs(current);
30944 +}
30945 +
30946 +static inline void si_pid_set(struct super_block *sb)
30947 +{
30948 +       AuDebugOn(task_test_aufs(current));
30949 +       task_set_aufs(current);
30950 +}
30951 +
30952 +/* ---------------------------------------------------------------------- */
30953 +
30954 +/* lock superblock. mainly for entry point functions */
30955 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30956 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30957 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30958 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30959 +/*
30960 +#define __si_read_trylock_nested(sb) \
30961 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30962 +#define __si_write_trylock_nested(sb) \
30963 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30964 +*/
30965 +
30966 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30967 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30968 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30969 +
30970 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30971 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30972 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30973 +
30974 +static inline void si_noflush_read_lock(struct super_block *sb)
30975 +{
30976 +       __si_read_lock(sb);
30977 +       si_pid_set(sb);
30978 +}
30979 +
30980 +static inline int si_noflush_read_trylock(struct super_block *sb)
30981 +{
30982 +       int locked;
30983 +
30984 +       locked = __si_read_trylock(sb);
30985 +       if (locked)
30986 +               si_pid_set(sb);
30987 +       return locked;
30988 +}
30989 +
30990 +static inline void si_noflush_write_lock(struct super_block *sb)
30991 +{
30992 +       __si_write_lock(sb);
30993 +       si_pid_set(sb);
30994 +}
30995 +
30996 +static inline int si_noflush_write_trylock(struct super_block *sb)
30997 +{
30998 +       int locked;
30999 +
31000 +       locked = __si_write_trylock(sb);
31001 +       if (locked)
31002 +               si_pid_set(sb);
31003 +       return locked;
31004 +}
31005 +
31006 +#if 0 /* reserved */
31007 +static inline int si_read_trylock(struct super_block *sb, int flags)
31008 +{
31009 +       if (au_ftest_lock(flags, FLUSH))
31010 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31011 +       return si_noflush_read_trylock(sb);
31012 +}
31013 +#endif
31014 +
31015 +static inline void si_read_unlock(struct super_block *sb)
31016 +{
31017 +       si_pid_clr(sb);
31018 +       __si_read_unlock(sb);
31019 +}
31020 +
31021 +#if 0 /* reserved */
31022 +static inline int si_write_trylock(struct super_block *sb, int flags)
31023 +{
31024 +       if (au_ftest_lock(flags, FLUSH))
31025 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31026 +       return si_noflush_write_trylock(sb);
31027 +}
31028 +#endif
31029 +
31030 +static inline void si_write_unlock(struct super_block *sb)
31031 +{
31032 +       si_pid_clr(sb);
31033 +       __si_write_unlock(sb);
31034 +}
31035 +
31036 +#if 0 /* reserved */
31037 +static inline void si_downgrade_lock(struct super_block *sb)
31038 +{
31039 +       __si_downgrade_lock(sb);
31040 +}
31041 +#endif
31042 +
31043 +/* ---------------------------------------------------------------------- */
31044 +
31045 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31046 +{
31047 +       SiMustAnyLock(sb);
31048 +       return au_sbi(sb)->si_bbot;
31049 +}
31050 +
31051 +static inline unsigned int au_mntflags(struct super_block *sb)
31052 +{
31053 +       SiMustAnyLock(sb);
31054 +       return au_sbi(sb)->si_mntflags;
31055 +}
31056 +
31057 +static inline unsigned int au_sigen(struct super_block *sb)
31058 +{
31059 +       SiMustAnyLock(sb);
31060 +       return au_sbi(sb)->si_generation;
31061 +}
31062 +
31063 +static inline struct au_branch *au_sbr(struct super_block *sb,
31064 +                                      aufs_bindex_t bindex)
31065 +{
31066 +       SiMustAnyLock(sb);
31067 +       return au_sbi(sb)->si_branch[0 + bindex];
31068 +}
31069 +
31070 +static inline loff_t au_xi_maxent(struct super_block *sb)
31071 +{
31072 +       SiMustAnyLock(sb);
31073 +       return au_sbi(sb)->si_ximaxent;
31074 +}
31075 +
31076 +#endif /* __KERNEL__ */
31077 +#endif /* __AUFS_SUPER_H__ */
31078 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31079 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31080 +++ linux/fs/aufs/sysaufs.c     2020-01-27 10:57:18.178871751 +0100
31081 @@ -0,0 +1,93 @@
31082 +// SPDX-License-Identifier: GPL-2.0
31083 +/*
31084 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31085 + *
31086 + * This program, aufs is free software; you can redistribute it and/or modify
31087 + * it under the terms of the GNU General Public License as published by
31088 + * the Free Software Foundation; either version 2 of the License, or
31089 + * (at your option) any later version.
31090 + *
31091 + * This program is distributed in the hope that it will be useful,
31092 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31093 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31094 + * GNU General Public License for more details.
31095 + *
31096 + * You should have received a copy of the GNU General Public License
31097 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31098 + */
31099 +
31100 +/*
31101 + * sysfs interface and lifetime management
31102 + * they are necessary regardless sysfs is disabled.
31103 + */
31104 +
31105 +#include <linux/random.h>
31106 +#include "aufs.h"
31107 +
31108 +unsigned long sysaufs_si_mask;
31109 +struct kset *sysaufs_kset;
31110 +
31111 +#define AuSiAttr(_name) { \
31112 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31113 +       .show   = sysaufs_si_##_name,                           \
31114 +}
31115 +
31116 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31117 +struct attribute *sysaufs_si_attrs[] = {
31118 +       &sysaufs_si_attr_xi_path.attr,
31119 +       NULL,
31120 +};
31121 +
31122 +static const struct sysfs_ops au_sbi_ops = {
31123 +       .show   = sysaufs_si_show
31124 +};
31125 +
31126 +static struct kobj_type au_sbi_ktype = {
31127 +       .release        = au_si_free,
31128 +       .sysfs_ops      = &au_sbi_ops,
31129 +       .default_attrs  = sysaufs_si_attrs
31130 +};
31131 +
31132 +/* ---------------------------------------------------------------------- */
31133 +
31134 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31135 +{
31136 +       int err;
31137 +
31138 +       sbinfo->si_kobj.kset = sysaufs_kset;
31139 +       /* cf. sysaufs_name() */
31140 +       err = kobject_init_and_add
31141 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31142 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31143 +
31144 +       return err;
31145 +}
31146 +
31147 +void sysaufs_fin(void)
31148 +{
31149 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31150 +       kset_unregister(sysaufs_kset);
31151 +}
31152 +
31153 +int __init sysaufs_init(void)
31154 +{
31155 +       int err;
31156 +
31157 +       do {
31158 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31159 +       } while (!sysaufs_si_mask);
31160 +
31161 +       err = -EINVAL;
31162 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31163 +       if (unlikely(!sysaufs_kset))
31164 +               goto out;
31165 +       err = PTR_ERR(sysaufs_kset);
31166 +       if (IS_ERR(sysaufs_kset))
31167 +               goto out;
31168 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31169 +       if (unlikely(err))
31170 +               kset_unregister(sysaufs_kset);
31171 +
31172 +out:
31173 +       return err;
31174 +}
31175 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31176 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31177 +++ linux/fs/aufs/sysaufs.h     2020-01-27 10:57:18.178871751 +0100
31178 @@ -0,0 +1,102 @@
31179 +/* SPDX-License-Identifier: GPL-2.0 */
31180 +/*
31181 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31182 + *
31183 + * This program, aufs is free software; you can redistribute it and/or modify
31184 + * it under the terms of the GNU General Public License as published by
31185 + * the Free Software Foundation; either version 2 of the License, or
31186 + * (at your option) any later version.
31187 + *
31188 + * This program is distributed in the hope that it will be useful,
31189 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31190 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31191 + * GNU General Public License for more details.
31192 + *
31193 + * You should have received a copy of the GNU General Public License
31194 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31195 + */
31196 +
31197 +/*
31198 + * sysfs interface and mount lifetime management
31199 + */
31200 +
31201 +#ifndef __SYSAUFS_H__
31202 +#define __SYSAUFS_H__
31203 +
31204 +#ifdef __KERNEL__
31205 +
31206 +#include <linux/sysfs.h>
31207 +#include "module.h"
31208 +
31209 +struct super_block;
31210 +struct au_sbinfo;
31211 +
31212 +struct sysaufs_si_attr {
31213 +       struct attribute attr;
31214 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31215 +};
31216 +
31217 +/* ---------------------------------------------------------------------- */
31218 +
31219 +/* sysaufs.c */
31220 +extern unsigned long sysaufs_si_mask;
31221 +extern struct kset *sysaufs_kset;
31222 +extern struct attribute *sysaufs_si_attrs[];
31223 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31224 +int __init sysaufs_init(void);
31225 +void sysaufs_fin(void);
31226 +
31227 +/* ---------------------------------------------------------------------- */
31228 +
31229 +/* some people doesn't like to show a pointer in kernel */
31230 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31231 +{
31232 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31233 +}
31234 +
31235 +#define SysaufsSiNamePrefix    "si_"
31236 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31237 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31238 +{
31239 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31240 +                sysaufs_si_id(sbinfo));
31241 +}
31242 +
31243 +struct au_branch;
31244 +#ifdef CONFIG_SYSFS
31245 +/* sysfs.c */
31246 +extern struct attribute_group *sysaufs_attr_group;
31247 +
31248 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31249 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31250 +                        char *buf);
31251 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31252 +#ifdef CONFIG_COMPAT
31253 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31254 +#endif
31255 +
31256 +void sysaufs_br_init(struct au_branch *br);
31257 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31258 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31259 +
31260 +#define sysaufs_brs_init()     do {} while (0)
31261 +
31262 +#else
31263 +#define sysaufs_attr_group     NULL
31264 +
31265 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31266 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31267 +       struct attribute *attr, char *buf)
31268 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31269 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31270 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31271 +
31272 +static inline void sysaufs_brs_init(void)
31273 +{
31274 +       sysaufs_brs = 0;
31275 +}
31276 +
31277 +#endif /* CONFIG_SYSFS */
31278 +
31279 +#endif /* __KERNEL__ */
31280 +#endif /* __SYSAUFS_H__ */
31281 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31282 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31283 +++ linux/fs/aufs/sysfs.c       2020-01-27 10:57:18.178871751 +0100
31284 @@ -0,0 +1,374 @@
31285 +// SPDX-License-Identifier: GPL-2.0
31286 +/*
31287 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31288 + *
31289 + * This program, aufs is free software; you can redistribute it and/or modify
31290 + * it under the terms of the GNU General Public License as published by
31291 + * the Free Software Foundation; either version 2 of the License, or
31292 + * (at your option) any later version.
31293 + *
31294 + * This program is distributed in the hope that it will be useful,
31295 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31296 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31297 + * GNU General Public License for more details.
31298 + *
31299 + * You should have received a copy of the GNU General Public License
31300 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31301 + */
31302 +
31303 +/*
31304 + * sysfs interface
31305 + */
31306 +
31307 +#include <linux/compat.h>
31308 +#include <linux/seq_file.h>
31309 +#include "aufs.h"
31310 +
31311 +#ifdef CONFIG_AUFS_FS_MODULE
31312 +/* this entry violates the "one line per file" policy of sysfs */
31313 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31314 +                          char *buf)
31315 +{
31316 +       ssize_t err;
31317 +       static char *conf =
31318 +/* this file is generated at compiling */
31319 +#include "conf.str"
31320 +               ;
31321 +
31322 +       err = snprintf(buf, PAGE_SIZE, conf);
31323 +       if (unlikely(err >= PAGE_SIZE))
31324 +               err = -EFBIG;
31325 +       return err;
31326 +}
31327 +
31328 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31329 +#endif
31330 +
31331 +static struct attribute *au_attr[] = {
31332 +#ifdef CONFIG_AUFS_FS_MODULE
31333 +       &au_config_attr.attr,
31334 +#endif
31335 +       NULL,   /* need to NULL terminate the list of attributes */
31336 +};
31337 +
31338 +static struct attribute_group sysaufs_attr_group_body = {
31339 +       .attrs = au_attr
31340 +};
31341 +
31342 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31343 +
31344 +/* ---------------------------------------------------------------------- */
31345 +
31346 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31347 +{
31348 +       int err;
31349 +
31350 +       SiMustAnyLock(sb);
31351 +
31352 +       err = 0;
31353 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31354 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31355 +               seq_putc(seq, '\n');
31356 +       }
31357 +       return err;
31358 +}
31359 +
31360 +/*
31361 + * the lifetime of branch is independent from the entry under sysfs.
31362 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31363 + * unlinked.
31364 + */
31365 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31366 +                        aufs_bindex_t bindex, int idx)
31367 +{
31368 +       int err;
31369 +       struct path path;
31370 +       struct dentry *root;
31371 +       struct au_branch *br;
31372 +       au_br_perm_str_t perm;
31373 +
31374 +       AuDbg("b%d\n", bindex);
31375 +
31376 +       err = 0;
31377 +       root = sb->s_root;
31378 +       di_read_lock_parent(root, !AuLock_IR);
31379 +       br = au_sbr(sb, bindex);
31380 +
31381 +       switch (idx) {
31382 +       case AuBrSysfs_BR:
31383 +               path.mnt = au_br_mnt(br);
31384 +               path.dentry = au_h_dptr(root, bindex);
31385 +               err = au_seq_path(seq, &path);
31386 +               if (!err) {
31387 +                       au_optstr_br_perm(&perm, br->br_perm);
31388 +                       seq_printf(seq, "=%s\n", perm.a);
31389 +               }
31390 +               break;
31391 +       case AuBrSysfs_BRID:
31392 +               seq_printf(seq, "%d\n", br->br_id);
31393 +               break;
31394 +       }
31395 +       di_read_unlock(root, !AuLock_IR);
31396 +       if (unlikely(err || seq_has_overflowed(seq)))
31397 +               err = -E2BIG;
31398 +
31399 +       return err;
31400 +}
31401 +
31402 +/* ---------------------------------------------------------------------- */
31403 +
31404 +static struct seq_file *au_seq(char *p, ssize_t len)
31405 +{
31406 +       struct seq_file *seq;
31407 +
31408 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31409 +       if (seq) {
31410 +               /* mutex_init(&seq.lock); */
31411 +               seq->buf = p;
31412 +               seq->size = len;
31413 +               return seq; /* success */
31414 +       }
31415 +
31416 +       seq = ERR_PTR(-ENOMEM);
31417 +       return seq;
31418 +}
31419 +
31420 +#define SysaufsBr_PREFIX       "br"
31421 +#define SysaufsBrid_PREFIX     "brid"
31422 +
31423 +/* todo: file size may exceed PAGE_SIZE */
31424 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31425 +                       char *buf)
31426 +{
31427 +       ssize_t err;
31428 +       int idx;
31429 +       long l;
31430 +       aufs_bindex_t bbot;
31431 +       struct au_sbinfo *sbinfo;
31432 +       struct super_block *sb;
31433 +       struct seq_file *seq;
31434 +       char *name;
31435 +       struct attribute **cattr;
31436 +
31437 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31438 +       sb = sbinfo->si_sb;
31439 +
31440 +       /*
31441 +        * prevent a race condition between sysfs and aufs.
31442 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31443 +        * prohibits maintaining the sysfs entries.
31444 +        * hew we acquire read lock after sysfs_get_active_two().
31445 +        * on the other hand, the remount process may maintain the sysfs/aufs
31446 +        * entries after acquiring write lock.
31447 +        * it can cause a deadlock.
31448 +        * simply we gave up processing read here.
31449 +        */
31450 +       err = -EBUSY;
31451 +       if (unlikely(!si_noflush_read_trylock(sb)))
31452 +               goto out;
31453 +
31454 +       seq = au_seq(buf, PAGE_SIZE);
31455 +       err = PTR_ERR(seq);
31456 +       if (IS_ERR(seq))
31457 +               goto out_unlock;
31458 +
31459 +       name = (void *)attr->name;
31460 +       cattr = sysaufs_si_attrs;
31461 +       while (*cattr) {
31462 +               if (!strcmp(name, (*cattr)->name)) {
31463 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31464 +                               ->show(seq, sb);
31465 +                       goto out_seq;
31466 +               }
31467 +               cattr++;
31468 +       }
31469 +
31470 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31471 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31472 +               idx = AuBrSysfs_BRID;
31473 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31474 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31475 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31476 +               idx = AuBrSysfs_BR;
31477 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31478 +       } else
31479 +                 BUG();
31480 +
31481 +       err = kstrtol(name, 10, &l);
31482 +       if (!err) {
31483 +               bbot = au_sbbot(sb);
31484 +               if (l <= bbot)
31485 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31486 +               else
31487 +                       err = -ENOENT;
31488 +       }
31489 +
31490 +out_seq:
31491 +       if (!err) {
31492 +               err = seq->count;
31493 +               /* sysfs limit */
31494 +               if (unlikely(err == PAGE_SIZE))
31495 +                       err = -EFBIG;
31496 +       }
31497 +       au_kfree_rcu(seq);
31498 +out_unlock:
31499 +       si_read_unlock(sb);
31500 +out:
31501 +       return err;
31502 +}
31503 +
31504 +/* ---------------------------------------------------------------------- */
31505 +
31506 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31507 +{
31508 +       int err;
31509 +       int16_t brid;
31510 +       aufs_bindex_t bindex, bbot;
31511 +       size_t sz;
31512 +       char *buf;
31513 +       struct seq_file *seq;
31514 +       struct au_branch *br;
31515 +
31516 +       si_read_lock(sb, AuLock_FLUSH);
31517 +       bbot = au_sbbot(sb);
31518 +       err = bbot + 1;
31519 +       if (!arg)
31520 +               goto out;
31521 +
31522 +       err = -ENOMEM;
31523 +       buf = (void *)__get_free_page(GFP_NOFS);
31524 +       if (unlikely(!buf))
31525 +               goto out;
31526 +
31527 +       seq = au_seq(buf, PAGE_SIZE);
31528 +       err = PTR_ERR(seq);
31529 +       if (IS_ERR(seq))
31530 +               goto out_buf;
31531 +
31532 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31533 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31534 +               /* VERIFY_WRITE */
31535 +               err = !access_ok(arg, sizeof(*arg));
31536 +               if (unlikely(err))
31537 +                       break;
31538 +
31539 +               br = au_sbr(sb, bindex);
31540 +               brid = br->br_id;
31541 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31542 +               err = __put_user(brid, &arg->id);
31543 +               if (unlikely(err))
31544 +                       break;
31545 +
31546 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31547 +               err = __put_user(br->br_perm, &arg->perm);
31548 +               if (unlikely(err))
31549 +                       break;
31550 +
31551 +               err = au_seq_path(seq, &br->br_path);
31552 +               if (unlikely(err))
31553 +                       break;
31554 +               seq_putc(seq, '\0');
31555 +               if (!seq_has_overflowed(seq)) {
31556 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31557 +                       seq->count = 0;
31558 +                       if (unlikely(err))
31559 +                               break;
31560 +               } else {
31561 +                       err = -E2BIG;
31562 +                       goto out_seq;
31563 +               }
31564 +       }
31565 +       if (unlikely(err))
31566 +               err = -EFAULT;
31567 +
31568 +out_seq:
31569 +       au_kfree_rcu(seq);
31570 +out_buf:
31571 +       free_page((unsigned long)buf);
31572 +out:
31573 +       si_read_unlock(sb);
31574 +       return err;
31575 +}
31576 +
31577 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31578 +{
31579 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31580 +}
31581 +
31582 +#ifdef CONFIG_COMPAT
31583 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31584 +{
31585 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31586 +}
31587 +#endif
31588 +
31589 +/* ---------------------------------------------------------------------- */
31590 +
31591 +void sysaufs_br_init(struct au_branch *br)
31592 +{
31593 +       int i;
31594 +       struct au_brsysfs *br_sysfs;
31595 +       struct attribute *attr;
31596 +
31597 +       br_sysfs = br->br_sysfs;
31598 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31599 +               attr = &br_sysfs->attr;
31600 +               sysfs_attr_init(attr);
31601 +               attr->name = br_sysfs->name;
31602 +               attr->mode = 0444;
31603 +               br_sysfs++;
31604 +       }
31605 +}
31606 +
31607 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31608 +{
31609 +       struct au_branch *br;
31610 +       struct kobject *kobj;
31611 +       struct au_brsysfs *br_sysfs;
31612 +       int i;
31613 +       aufs_bindex_t bbot;
31614 +
31615 +       if (!sysaufs_brs)
31616 +               return;
31617 +
31618 +       kobj = &au_sbi(sb)->si_kobj;
31619 +       bbot = au_sbbot(sb);
31620 +       for (; bindex <= bbot; bindex++) {
31621 +               br = au_sbr(sb, bindex);
31622 +               br_sysfs = br->br_sysfs;
31623 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31624 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31625 +                       br_sysfs++;
31626 +               }
31627 +       }
31628 +}
31629 +
31630 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31631 +{
31632 +       int err, i;
31633 +       aufs_bindex_t bbot;
31634 +       struct kobject *kobj;
31635 +       struct au_branch *br;
31636 +       struct au_brsysfs *br_sysfs;
31637 +
31638 +       if (!sysaufs_brs)
31639 +               return;
31640 +
31641 +       kobj = &au_sbi(sb)->si_kobj;
31642 +       bbot = au_sbbot(sb);
31643 +       for (; bindex <= bbot; bindex++) {
31644 +               br = au_sbr(sb, bindex);
31645 +               br_sysfs = br->br_sysfs;
31646 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31647 +                        SysaufsBr_PREFIX "%d", bindex);
31648 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31649 +                        SysaufsBrid_PREFIX "%d", bindex);
31650 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31651 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31652 +                       if (unlikely(err))
31653 +                               pr_warn("failed %s under sysfs(%d)\n",
31654 +                                       br_sysfs->name, err);
31655 +                       br_sysfs++;
31656 +               }
31657 +       }
31658 +}
31659 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31660 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31661 +++ linux/fs/aufs/sysrq.c       2020-01-27 10:57:18.178871751 +0100
31662 @@ -0,0 +1,149 @@
31663 +// SPDX-License-Identifier: GPL-2.0
31664 +/*
31665 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31666 + *
31667 + * This program, aufs is free software; you can redistribute it and/or modify
31668 + * it under the terms of the GNU General Public License as published by
31669 + * the Free Software Foundation; either version 2 of the License, or
31670 + * (at your option) any later version.
31671 + *
31672 + * This program is distributed in the hope that it will be useful,
31673 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31674 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31675 + * GNU General Public License for more details.
31676 + *
31677 + * You should have received a copy of the GNU General Public License
31678 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31679 + */
31680 +
31681 +/*
31682 + * magic sysrq handler
31683 + */
31684 +
31685 +/* #include <linux/sysrq.h> */
31686 +#include <linux/writeback.h>
31687 +#include "aufs.h"
31688 +
31689 +/* ---------------------------------------------------------------------- */
31690 +
31691 +static void sysrq_sb(struct super_block *sb)
31692 +{
31693 +       char *plevel;
31694 +       struct au_sbinfo *sbinfo;
31695 +       struct file *file;
31696 +       struct hlist_bl_head *files;
31697 +       struct hlist_bl_node *pos;
31698 +       struct au_finfo *finfo;
31699 +       struct inode *i;
31700 +
31701 +       plevel = au_plevel;
31702 +       au_plevel = KERN_WARNING;
31703 +
31704 +       /* since we define pr_fmt, call printk directly */
31705 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31706 +
31707 +       sbinfo = au_sbi(sb);
31708 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31709 +       pr("superblock\n");
31710 +       au_dpri_sb(sb);
31711 +
31712 +#if 0 /* reserved */
31713 +       do {
31714 +               int err, i, j, ndentry;
31715 +               struct au_dcsub_pages dpages;
31716 +               struct au_dpage *dpage;
31717 +
31718 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31719 +               if (unlikely(err))
31720 +                       break;
31721 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31722 +               if (!err)
31723 +                       for (i = 0; i < dpages.ndpage; i++) {
31724 +                               dpage = dpages.dpages + i;
31725 +                               ndentry = dpage->ndentry;
31726 +                               for (j = 0; j < ndentry; j++)
31727 +                                       au_dpri_dentry(dpage->dentries[j]);
31728 +                       }
31729 +               au_dpages_free(&dpages);
31730 +       } while (0);
31731 +#endif
31732 +
31733 +       pr("isolated inode\n");
31734 +       spin_lock(&sb->s_inode_list_lock);
31735 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31736 +               spin_lock(&i->i_lock);
31737 +               if (hlist_empty(&i->i_dentry))
31738 +                       au_dpri_inode(i);
31739 +               spin_unlock(&i->i_lock);
31740 +       }
31741 +       spin_unlock(&sb->s_inode_list_lock);
31742 +
31743 +       pr("files\n");
31744 +       files = &au_sbi(sb)->si_files;
31745 +       hlist_bl_lock(files);
31746 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31747 +               umode_t mode;
31748 +
31749 +               file = finfo->fi_file;
31750 +               mode = file_inode(file)->i_mode;
31751 +               if (!special_file(mode))
31752 +                       au_dpri_file(file);
31753 +       }
31754 +       hlist_bl_unlock(files);
31755 +       pr("done\n");
31756 +
31757 +#undef pr
31758 +       au_plevel = plevel;
31759 +}
31760 +
31761 +/* ---------------------------------------------------------------------- */
31762 +
31763 +/* module parameter */
31764 +static char *aufs_sysrq_key = "a";
31765 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31766 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31767 +
31768 +static void au_sysrq(int key __maybe_unused)
31769 +{
31770 +       struct au_sbinfo *sbinfo;
31771 +       struct hlist_bl_node *pos;
31772 +
31773 +       lockdep_off();
31774 +       au_sbilist_lock();
31775 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31776 +               sysrq_sb(sbinfo->si_sb);
31777 +       au_sbilist_unlock();
31778 +       lockdep_on();
31779 +}
31780 +
31781 +static struct sysrq_key_op au_sysrq_op = {
31782 +       .handler        = au_sysrq,
31783 +       .help_msg       = "Aufs",
31784 +       .action_msg     = "Aufs",
31785 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31786 +};
31787 +
31788 +/* ---------------------------------------------------------------------- */
31789 +
31790 +int __init au_sysrq_init(void)
31791 +{
31792 +       int err;
31793 +       char key;
31794 +
31795 +       err = -1;
31796 +       key = *aufs_sysrq_key;
31797 +       if ('a' <= key && key <= 'z')
31798 +               err = register_sysrq_key(key, &au_sysrq_op);
31799 +       if (unlikely(err))
31800 +               pr_err("err %d, sysrq=%c\n", err, key);
31801 +       return err;
31802 +}
31803 +
31804 +void au_sysrq_fin(void)
31805 +{
31806 +       int err;
31807 +
31808 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31809 +       if (unlikely(err))
31810 +               pr_err("err %d (ignored)\n", err);
31811 +}
31812 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31813 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31814 +++ linux/fs/aufs/vdir.c        2020-01-27 10:57:18.178871751 +0100
31815 @@ -0,0 +1,896 @@
31816 +// SPDX-License-Identifier: GPL-2.0
31817 +/*
31818 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31819 + *
31820 + * This program, aufs is free software; you can redistribute it and/or modify
31821 + * it under the terms of the GNU General Public License as published by
31822 + * the Free Software Foundation; either version 2 of the License, or
31823 + * (at your option) any later version.
31824 + *
31825 + * This program is distributed in the hope that it will be useful,
31826 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31827 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31828 + * GNU General Public License for more details.
31829 + *
31830 + * You should have received a copy of the GNU General Public License
31831 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31832 + */
31833 +
31834 +/*
31835 + * virtual or vertical directory
31836 + */
31837 +
31838 +#include <linux/iversion.h>
31839 +#include "aufs.h"
31840 +
31841 +static unsigned int calc_size(int nlen)
31842 +{
31843 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31844 +}
31845 +
31846 +static int set_deblk_end(union au_vdir_deblk_p *p,
31847 +                        union au_vdir_deblk_p *deblk_end)
31848 +{
31849 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31850 +               p->de->de_str.len = 0;
31851 +               /* smp_mb(); */
31852 +               return 0;
31853 +       }
31854 +       return -1; /* error */
31855 +}
31856 +
31857 +/* returns true or false */
31858 +static int is_deblk_end(union au_vdir_deblk_p *p,
31859 +                       union au_vdir_deblk_p *deblk_end)
31860 +{
31861 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31862 +               return !p->de->de_str.len;
31863 +       return 1;
31864 +}
31865 +
31866 +static unsigned char *last_deblk(struct au_vdir *vdir)
31867 +{
31868 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31869 +}
31870 +
31871 +/* ---------------------------------------------------------------------- */
31872 +
31873 +/* estimate the appropriate size for name hash table */
31874 +unsigned int au_rdhash_est(loff_t sz)
31875 +{
31876 +       unsigned int n;
31877 +
31878 +       n = UINT_MAX;
31879 +       sz >>= 10;
31880 +       if (sz < n)
31881 +               n = sz;
31882 +       if (sz < AUFS_RDHASH_DEF)
31883 +               n = AUFS_RDHASH_DEF;
31884 +       /* pr_info("n %u\n", n); */
31885 +       return n;
31886 +}
31887 +
31888 +/*
31889 + * the allocated memory has to be freed by
31890 + * au_nhash_wh_free() or au_nhash_de_free().
31891 + */
31892 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31893 +{
31894 +       struct hlist_head *head;
31895 +       unsigned int u;
31896 +       size_t sz;
31897 +
31898 +       sz = sizeof(*nhash->nh_head) * num_hash;
31899 +       head = kmalloc(sz, gfp);
31900 +       if (head) {
31901 +               nhash->nh_num = num_hash;
31902 +               nhash->nh_head = head;
31903 +               for (u = 0; u < num_hash; u++)
31904 +                       INIT_HLIST_HEAD(head++);
31905 +               return 0; /* success */
31906 +       }
31907 +
31908 +       return -ENOMEM;
31909 +}
31910 +
31911 +static void nhash_count(struct hlist_head *head)
31912 +{
31913 +#if 0 /* debugging */
31914 +       unsigned long n;
31915 +       struct hlist_node *pos;
31916 +
31917 +       n = 0;
31918 +       hlist_for_each(pos, head)
31919 +               n++;
31920 +       pr_info("%lu\n", n);
31921 +#endif
31922 +}
31923 +
31924 +static void au_nhash_wh_do_free(struct hlist_head *head)
31925 +{
31926 +       struct au_vdir_wh *pos;
31927 +       struct hlist_node *node;
31928 +
31929 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31930 +               au_kfree_rcu(pos);
31931 +}
31932 +
31933 +static void au_nhash_de_do_free(struct hlist_head *head)
31934 +{
31935 +       struct au_vdir_dehstr *pos;
31936 +       struct hlist_node *node;
31937 +
31938 +       hlist_for_each_entry_safe(pos, node, head, hash)
31939 +               au_cache_free_vdir_dehstr(pos);
31940 +}
31941 +
31942 +static void au_nhash_do_free(struct au_nhash *nhash,
31943 +                            void (*free)(struct hlist_head *head))
31944 +{
31945 +       unsigned int n;
31946 +       struct hlist_head *head;
31947 +
31948 +       n = nhash->nh_num;
31949 +       if (!n)
31950 +               return;
31951 +
31952 +       head = nhash->nh_head;
31953 +       while (n-- > 0) {
31954 +               nhash_count(head);
31955 +               free(head++);
31956 +       }
31957 +       au_kfree_try_rcu(nhash->nh_head);
31958 +}
31959 +
31960 +void au_nhash_wh_free(struct au_nhash *whlist)
31961 +{
31962 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31963 +}
31964 +
31965 +static void au_nhash_de_free(struct au_nhash *delist)
31966 +{
31967 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31968 +}
31969 +
31970 +/* ---------------------------------------------------------------------- */
31971 +
31972 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31973 +                           int limit)
31974 +{
31975 +       int num;
31976 +       unsigned int u, n;
31977 +       struct hlist_head *head;
31978 +       struct au_vdir_wh *pos;
31979 +
31980 +       num = 0;
31981 +       n = whlist->nh_num;
31982 +       head = whlist->nh_head;
31983 +       for (u = 0; u < n; u++, head++)
31984 +               hlist_for_each_entry(pos, head, wh_hash)
31985 +                       if (pos->wh_bindex == btgt && ++num > limit)
31986 +                               return 1;
31987 +       return 0;
31988 +}
31989 +
31990 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31991 +                                      unsigned char *name,
31992 +                                      unsigned int len)
31993 +{
31994 +       unsigned int v;
31995 +       /* const unsigned int magic_bit = 12; */
31996 +
31997 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31998 +
31999 +       v = 0;
32000 +       if (len > 8)
32001 +               len = 8;
32002 +       while (len--)
32003 +               v += *name++;
32004 +       /* v = hash_long(v, magic_bit); */
32005 +       v %= nhash->nh_num;
32006 +       return nhash->nh_head + v;
32007 +}
32008 +
32009 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32010 +                             int nlen)
32011 +{
32012 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32013 +}
32014 +
32015 +/* returns found or not */
32016 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32017 +{
32018 +       struct hlist_head *head;
32019 +       struct au_vdir_wh *pos;
32020 +       struct au_vdir_destr *str;
32021 +
32022 +       head = au_name_hash(whlist, name, nlen);
32023 +       hlist_for_each_entry(pos, head, wh_hash) {
32024 +               str = &pos->wh_str;
32025 +               AuDbg("%.*s\n", str->len, str->name);
32026 +               if (au_nhash_test_name(str, name, nlen))
32027 +                       return 1;
32028 +       }
32029 +       return 0;
32030 +}
32031 +
32032 +/* returns found(true) or not */
32033 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32034 +{
32035 +       struct hlist_head *head;
32036 +       struct au_vdir_dehstr *pos;
32037 +       struct au_vdir_destr *str;
32038 +
32039 +       head = au_name_hash(delist, name, nlen);
32040 +       hlist_for_each_entry(pos, head, hash) {
32041 +               str = pos->str;
32042 +               AuDbg("%.*s\n", str->len, str->name);
32043 +               if (au_nhash_test_name(str, name, nlen))
32044 +                       return 1;
32045 +       }
32046 +       return 0;
32047 +}
32048 +
32049 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32050 +                           unsigned char d_type)
32051 +{
32052 +#ifdef CONFIG_AUFS_SHWH
32053 +       wh->wh_ino = ino;
32054 +       wh->wh_type = d_type;
32055 +#endif
32056 +}
32057 +
32058 +/* ---------------------------------------------------------------------- */
32059 +
32060 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32061 +                      unsigned int d_type, aufs_bindex_t bindex,
32062 +                      unsigned char shwh)
32063 +{
32064 +       int err;
32065 +       struct au_vdir_destr *str;
32066 +       struct au_vdir_wh *wh;
32067 +
32068 +       AuDbg("%.*s\n", nlen, name);
32069 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32070 +
32071 +       err = -ENOMEM;
32072 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32073 +       if (unlikely(!wh))
32074 +               goto out;
32075 +
32076 +       err = 0;
32077 +       wh->wh_bindex = bindex;
32078 +       if (shwh)
32079 +               au_shwh_init_wh(wh, ino, d_type);
32080 +       str = &wh->wh_str;
32081 +       str->len = nlen;
32082 +       memcpy(str->name, name, nlen);
32083 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32084 +       /* smp_mb(); */
32085 +
32086 +out:
32087 +       return err;
32088 +}
32089 +
32090 +static int append_deblk(struct au_vdir *vdir)
32091 +{
32092 +       int err;
32093 +       unsigned long ul;
32094 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32095 +       union au_vdir_deblk_p p, deblk_end;
32096 +       unsigned char **o;
32097 +
32098 +       err = -ENOMEM;
32099 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32100 +                       GFP_NOFS, /*may_shrink*/0);
32101 +       if (unlikely(!o))
32102 +               goto out;
32103 +
32104 +       vdir->vd_deblk = o;
32105 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32106 +       if (p.deblk) {
32107 +               ul = vdir->vd_nblk++;
32108 +               vdir->vd_deblk[ul] = p.deblk;
32109 +               vdir->vd_last.ul = ul;
32110 +               vdir->vd_last.p.deblk = p.deblk;
32111 +               deblk_end.deblk = p.deblk + deblk_sz;
32112 +               err = set_deblk_end(&p, &deblk_end);
32113 +       }
32114 +
32115 +out:
32116 +       return err;
32117 +}
32118 +
32119 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32120 +                    unsigned int d_type, struct au_nhash *delist)
32121 +{
32122 +       int err;
32123 +       unsigned int sz;
32124 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32125 +       union au_vdir_deblk_p p, *room, deblk_end;
32126 +       struct au_vdir_dehstr *dehstr;
32127 +
32128 +       p.deblk = last_deblk(vdir);
32129 +       deblk_end.deblk = p.deblk + deblk_sz;
32130 +       room = &vdir->vd_last.p;
32131 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32132 +                 || !is_deblk_end(room, &deblk_end));
32133 +
32134 +       sz = calc_size(nlen);
32135 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32136 +               err = append_deblk(vdir);
32137 +               if (unlikely(err))
32138 +                       goto out;
32139 +
32140 +               p.deblk = last_deblk(vdir);
32141 +               deblk_end.deblk = p.deblk + deblk_sz;
32142 +               /* smp_mb(); */
32143 +               AuDebugOn(room->deblk != p.deblk);
32144 +       }
32145 +
32146 +       err = -ENOMEM;
32147 +       dehstr = au_cache_alloc_vdir_dehstr();
32148 +       if (unlikely(!dehstr))
32149 +               goto out;
32150 +
32151 +       dehstr->str = &room->de->de_str;
32152 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32153 +       room->de->de_ino = ino;
32154 +       room->de->de_type = d_type;
32155 +       room->de->de_str.len = nlen;
32156 +       memcpy(room->de->de_str.name, name, nlen);
32157 +
32158 +       err = 0;
32159 +       room->deblk += sz;
32160 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32161 +               err = append_deblk(vdir);
32162 +       /* smp_mb(); */
32163 +
32164 +out:
32165 +       return err;
32166 +}
32167 +
32168 +/* ---------------------------------------------------------------------- */
32169 +
32170 +void au_vdir_free(struct au_vdir *vdir)
32171 +{
32172 +       unsigned char **deblk;
32173 +
32174 +       deblk = vdir->vd_deblk;
32175 +       while (vdir->vd_nblk--)
32176 +               au_kfree_try_rcu(*deblk++);
32177 +       au_kfree_try_rcu(vdir->vd_deblk);
32178 +       au_cache_free_vdir(vdir);
32179 +}
32180 +
32181 +static struct au_vdir *alloc_vdir(struct file *file)
32182 +{
32183 +       struct au_vdir *vdir;
32184 +       struct super_block *sb;
32185 +       int err;
32186 +
32187 +       sb = file->f_path.dentry->d_sb;
32188 +       SiMustAnyLock(sb);
32189 +
32190 +       err = -ENOMEM;
32191 +       vdir = au_cache_alloc_vdir();
32192 +       if (unlikely(!vdir))
32193 +               goto out;
32194 +
32195 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32196 +       if (unlikely(!vdir->vd_deblk))
32197 +               goto out_free;
32198 +
32199 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32200 +       if (!vdir->vd_deblk_sz) {
32201 +               /* estimate the appropriate size for deblk */
32202 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32203 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32204 +       }
32205 +       vdir->vd_nblk = 0;
32206 +       vdir->vd_version = 0;
32207 +       vdir->vd_jiffy = 0;
32208 +       err = append_deblk(vdir);
32209 +       if (!err)
32210 +               return vdir; /* success */
32211 +
32212 +       au_kfree_try_rcu(vdir->vd_deblk);
32213 +
32214 +out_free:
32215 +       au_cache_free_vdir(vdir);
32216 +out:
32217 +       vdir = ERR_PTR(err);
32218 +       return vdir;
32219 +}
32220 +
32221 +static int reinit_vdir(struct au_vdir *vdir)
32222 +{
32223 +       int err;
32224 +       union au_vdir_deblk_p p, deblk_end;
32225 +
32226 +       while (vdir->vd_nblk > 1) {
32227 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32228 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32229 +               vdir->vd_nblk--;
32230 +       }
32231 +       p.deblk = vdir->vd_deblk[0];
32232 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32233 +       err = set_deblk_end(&p, &deblk_end);
32234 +       /* keep vd_dblk_sz */
32235 +       vdir->vd_last.ul = 0;
32236 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32237 +       vdir->vd_version = 0;
32238 +       vdir->vd_jiffy = 0;
32239 +       /* smp_mb(); */
32240 +       return err;
32241 +}
32242 +
32243 +/* ---------------------------------------------------------------------- */
32244 +
32245 +#define AuFillVdir_CALLED      1
32246 +#define AuFillVdir_WHABLE      (1 << 1)
32247 +#define AuFillVdir_SHWH                (1 << 2)
32248 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32249 +#define au_fset_fillvdir(flags, name) \
32250 +       do { (flags) |= AuFillVdir_##name; } while (0)
32251 +#define au_fclr_fillvdir(flags, name) \
32252 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32253 +
32254 +#ifndef CONFIG_AUFS_SHWH
32255 +#undef AuFillVdir_SHWH
32256 +#define AuFillVdir_SHWH                0
32257 +#endif
32258 +
32259 +struct fillvdir_arg {
32260 +       struct dir_context      ctx;
32261 +       struct file             *file;
32262 +       struct au_vdir          *vdir;
32263 +       struct au_nhash         delist;
32264 +       struct au_nhash         whlist;
32265 +       aufs_bindex_t           bindex;
32266 +       unsigned int            flags;
32267 +       int                     err;
32268 +};
32269 +
32270 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32271 +                   loff_t offset __maybe_unused, u64 h_ino,
32272 +                   unsigned int d_type)
32273 +{
32274 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32275 +       char *name = (void *)__name;
32276 +       struct super_block *sb;
32277 +       ino_t ino;
32278 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32279 +
32280 +       arg->err = 0;
32281 +       sb = arg->file->f_path.dentry->d_sb;
32282 +       au_fset_fillvdir(arg->flags, CALLED);
32283 +       /* smp_mb(); */
32284 +       if (nlen <= AUFS_WH_PFX_LEN
32285 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32286 +               if (test_known(&arg->delist, name, nlen)
32287 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32288 +                       goto out; /* already exists or whiteouted */
32289 +
32290 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32291 +               if (!arg->err) {
32292 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32293 +                               d_type = DT_UNKNOWN;
32294 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32295 +                                            d_type, &arg->delist);
32296 +               }
32297 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32298 +               name += AUFS_WH_PFX_LEN;
32299 +               nlen -= AUFS_WH_PFX_LEN;
32300 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32301 +                       goto out; /* already whiteouted */
32302 +
32303 +               ino = 0; /* just to suppress a warning */
32304 +               if (shwh)
32305 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32306 +                                            &ino);
32307 +               if (!arg->err) {
32308 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32309 +                               d_type = DT_UNKNOWN;
32310 +                       arg->err = au_nhash_append_wh
32311 +                               (&arg->whlist, name, nlen, ino, d_type,
32312 +                                arg->bindex, shwh);
32313 +               }
32314 +       }
32315 +
32316 +out:
32317 +       if (!arg->err)
32318 +               arg->vdir->vd_jiffy = jiffies;
32319 +       /* smp_mb(); */
32320 +       AuTraceErr(arg->err);
32321 +       return arg->err;
32322 +}
32323 +
32324 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32325 +                         struct au_nhash *whlist, struct au_nhash *delist)
32326 +{
32327 +#ifdef CONFIG_AUFS_SHWH
32328 +       int err;
32329 +       unsigned int nh, u;
32330 +       struct hlist_head *head;
32331 +       struct au_vdir_wh *pos;
32332 +       struct hlist_node *n;
32333 +       char *p, *o;
32334 +       struct au_vdir_destr *destr;
32335 +
32336 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32337 +
32338 +       err = -ENOMEM;
32339 +       o = p = (void *)__get_free_page(GFP_NOFS);
32340 +       if (unlikely(!p))
32341 +               goto out;
32342 +
32343 +       err = 0;
32344 +       nh = whlist->nh_num;
32345 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32346 +       p += AUFS_WH_PFX_LEN;
32347 +       for (u = 0; u < nh; u++) {
32348 +               head = whlist->nh_head + u;
32349 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32350 +                       destr = &pos->wh_str;
32351 +                       memcpy(p, destr->name, destr->len);
32352 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32353 +                                       pos->wh_ino, pos->wh_type, delist);
32354 +                       if (unlikely(err))
32355 +                               break;
32356 +               }
32357 +       }
32358 +
32359 +       free_page((unsigned long)o);
32360 +
32361 +out:
32362 +       AuTraceErr(err);
32363 +       return err;
32364 +#else
32365 +       return 0;
32366 +#endif
32367 +}
32368 +
32369 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32370 +{
32371 +       int err;
32372 +       unsigned int rdhash;
32373 +       loff_t offset;
32374 +       aufs_bindex_t bbot, bindex, btop;
32375 +       unsigned char shwh;
32376 +       struct file *hf, *file;
32377 +       struct super_block *sb;
32378 +
32379 +       file = arg->file;
32380 +       sb = file->f_path.dentry->d_sb;
32381 +       SiMustAnyLock(sb);
32382 +
32383 +       rdhash = au_sbi(sb)->si_rdhash;
32384 +       if (!rdhash)
32385 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32386 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32387 +       if (unlikely(err))
32388 +               goto out;
32389 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32390 +       if (unlikely(err))
32391 +               goto out_delist;
32392 +
32393 +       err = 0;
32394 +       arg->flags = 0;
32395 +       shwh = 0;
32396 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32397 +               shwh = 1;
32398 +               au_fset_fillvdir(arg->flags, SHWH);
32399 +       }
32400 +       btop = au_fbtop(file);
32401 +       bbot = au_fbbot_dir(file);
32402 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32403 +               hf = au_hf_dir(file, bindex);
32404 +               if (!hf)
32405 +                       continue;
32406 +
32407 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32408 +               err = offset;
32409 +               if (unlikely(offset))
32410 +                       break;
32411 +
32412 +               arg->bindex = bindex;
32413 +               au_fclr_fillvdir(arg->flags, WHABLE);
32414 +               if (shwh
32415 +                   || (bindex != bbot
32416 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32417 +                       au_fset_fillvdir(arg->flags, WHABLE);
32418 +               do {
32419 +                       arg->err = 0;
32420 +                       au_fclr_fillvdir(arg->flags, CALLED);
32421 +                       /* smp_mb(); */
32422 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32423 +                       if (err >= 0)
32424 +                               err = arg->err;
32425 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32426 +
32427 +               /*
32428 +                * dir_relax() may be good for concurrency, but aufs should not
32429 +                * use it since it will cause a lockdep problem.
32430 +                */
32431 +       }
32432 +
32433 +       if (!err && shwh)
32434 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32435 +
32436 +       au_nhash_wh_free(&arg->whlist);
32437 +
32438 +out_delist:
32439 +       au_nhash_de_free(&arg->delist);
32440 +out:
32441 +       return err;
32442 +}
32443 +
32444 +static int read_vdir(struct file *file, int may_read)
32445 +{
32446 +       int err;
32447 +       unsigned long expire;
32448 +       unsigned char do_read;
32449 +       struct fillvdir_arg arg = {
32450 +               .ctx = {
32451 +                       .actor = fillvdir
32452 +               }
32453 +       };
32454 +       struct inode *inode;
32455 +       struct au_vdir *vdir, *allocated;
32456 +
32457 +       err = 0;
32458 +       inode = file_inode(file);
32459 +       IMustLock(inode);
32460 +       IiMustWriteLock(inode);
32461 +       SiMustAnyLock(inode->i_sb);
32462 +
32463 +       allocated = NULL;
32464 +       do_read = 0;
32465 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32466 +       vdir = au_ivdir(inode);
32467 +       if (!vdir) {
32468 +               do_read = 1;
32469 +               vdir = alloc_vdir(file);
32470 +               err = PTR_ERR(vdir);
32471 +               if (IS_ERR(vdir))
32472 +                       goto out;
32473 +               err = 0;
32474 +               allocated = vdir;
32475 +       } else if (may_read
32476 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32477 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32478 +               do_read = 1;
32479 +               err = reinit_vdir(vdir);
32480 +               if (unlikely(err))
32481 +                       goto out;
32482 +       }
32483 +
32484 +       if (!do_read)
32485 +               return 0; /* success */
32486 +
32487 +       arg.file = file;
32488 +       arg.vdir = vdir;
32489 +       err = au_do_read_vdir(&arg);
32490 +       if (!err) {
32491 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32492 +               vdir->vd_version = inode_query_iversion(inode);
32493 +               vdir->vd_last.ul = 0;
32494 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32495 +               if (allocated)
32496 +                       au_set_ivdir(inode, allocated);
32497 +       } else if (allocated)
32498 +               au_vdir_free(allocated);
32499 +
32500 +out:
32501 +       return err;
32502 +}
32503 +
32504 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32505 +{
32506 +       int err, rerr;
32507 +       unsigned long ul, n;
32508 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32509 +
32510 +       AuDebugOn(tgt->vd_nblk != 1);
32511 +
32512 +       err = -ENOMEM;
32513 +       if (tgt->vd_nblk < src->vd_nblk) {
32514 +               unsigned char **p;
32515 +
32516 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32517 +                               GFP_NOFS, /*may_shrink*/0);
32518 +               if (unlikely(!p))
32519 +                       goto out;
32520 +               tgt->vd_deblk = p;
32521 +       }
32522 +
32523 +       if (tgt->vd_deblk_sz != deblk_sz) {
32524 +               unsigned char *p;
32525 +
32526 +               tgt->vd_deblk_sz = deblk_sz;
32527 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32528 +                               /*may_shrink*/1);
32529 +               if (unlikely(!p))
32530 +                       goto out;
32531 +               tgt->vd_deblk[0] = p;
32532 +       }
32533 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32534 +       tgt->vd_version = src->vd_version;
32535 +       tgt->vd_jiffy = src->vd_jiffy;
32536 +
32537 +       n = src->vd_nblk;
32538 +       for (ul = 1; ul < n; ul++) {
32539 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32540 +                                           GFP_NOFS);
32541 +               if (unlikely(!tgt->vd_deblk[ul]))
32542 +                       goto out;
32543 +               tgt->vd_nblk++;
32544 +       }
32545 +       tgt->vd_nblk = n;
32546 +       tgt->vd_last.ul = tgt->vd_last.ul;
32547 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32548 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32549 +               - src->vd_deblk[src->vd_last.ul];
32550 +       /* smp_mb(); */
32551 +       return 0; /* success */
32552 +
32553 +out:
32554 +       rerr = reinit_vdir(tgt);
32555 +       BUG_ON(rerr);
32556 +       return err;
32557 +}
32558 +
32559 +int au_vdir_init(struct file *file)
32560 +{
32561 +       int err;
32562 +       struct inode *inode;
32563 +       struct au_vdir *vdir_cache, *allocated;
32564 +
32565 +       /* test file->f_pos here instead of ctx->pos */
32566 +       err = read_vdir(file, !file->f_pos);
32567 +       if (unlikely(err))
32568 +               goto out;
32569 +
32570 +       allocated = NULL;
32571 +       vdir_cache = au_fvdir_cache(file);
32572 +       if (!vdir_cache) {
32573 +               vdir_cache = alloc_vdir(file);
32574 +               err = PTR_ERR(vdir_cache);
32575 +               if (IS_ERR(vdir_cache))
32576 +                       goto out;
32577 +               allocated = vdir_cache;
32578 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32579 +               /* test file->f_pos here instead of ctx->pos */
32580 +               err = reinit_vdir(vdir_cache);
32581 +               if (unlikely(err))
32582 +                       goto out;
32583 +       } else
32584 +               return 0; /* success */
32585 +
32586 +       inode = file_inode(file);
32587 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32588 +       if (!err) {
32589 +               file->f_version = inode_query_iversion(inode);
32590 +               if (allocated)
32591 +                       au_set_fvdir_cache(file, allocated);
32592 +       } else if (allocated)
32593 +               au_vdir_free(allocated);
32594 +
32595 +out:
32596 +       return err;
32597 +}
32598 +
32599 +static loff_t calc_offset(struct au_vdir *vdir)
32600 +{
32601 +       loff_t offset;
32602 +       union au_vdir_deblk_p p;
32603 +
32604 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32605 +       offset = vdir->vd_last.p.deblk - p.deblk;
32606 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32607 +       return offset;
32608 +}
32609 +
32610 +/* returns true or false */
32611 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32612 +{
32613 +       int valid;
32614 +       unsigned int deblk_sz;
32615 +       unsigned long ul, n;
32616 +       loff_t offset;
32617 +       union au_vdir_deblk_p p, deblk_end;
32618 +       struct au_vdir *vdir_cache;
32619 +
32620 +       valid = 1;
32621 +       vdir_cache = au_fvdir_cache(file);
32622 +       offset = calc_offset(vdir_cache);
32623 +       AuDbg("offset %lld\n", offset);
32624 +       if (ctx->pos == offset)
32625 +               goto out;
32626 +
32627 +       vdir_cache->vd_last.ul = 0;
32628 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32629 +       if (!ctx->pos)
32630 +               goto out;
32631 +
32632 +       valid = 0;
32633 +       deblk_sz = vdir_cache->vd_deblk_sz;
32634 +       ul = div64_u64(ctx->pos, deblk_sz);
32635 +       AuDbg("ul %lu\n", ul);
32636 +       if (ul >= vdir_cache->vd_nblk)
32637 +               goto out;
32638 +
32639 +       n = vdir_cache->vd_nblk;
32640 +       for (; ul < n; ul++) {
32641 +               p.deblk = vdir_cache->vd_deblk[ul];
32642 +               deblk_end.deblk = p.deblk + deblk_sz;
32643 +               offset = ul;
32644 +               offset *= deblk_sz;
32645 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32646 +                       unsigned int l;
32647 +
32648 +                       l = calc_size(p.de->de_str.len);
32649 +                       offset += l;
32650 +                       p.deblk += l;
32651 +               }
32652 +               if (!is_deblk_end(&p, &deblk_end)) {
32653 +                       valid = 1;
32654 +                       vdir_cache->vd_last.ul = ul;
32655 +                       vdir_cache->vd_last.p = p;
32656 +                       break;
32657 +               }
32658 +       }
32659 +
32660 +out:
32661 +       /* smp_mb(); */
32662 +       if (!valid)
32663 +               AuDbg("valid %d\n", !valid);
32664 +       return valid;
32665 +}
32666 +
32667 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32668 +{
32669 +       unsigned int l, deblk_sz;
32670 +       union au_vdir_deblk_p deblk_end;
32671 +       struct au_vdir *vdir_cache;
32672 +       struct au_vdir_de *de;
32673 +
32674 +       if (!seek_vdir(file, ctx))
32675 +               return 0;
32676 +
32677 +       vdir_cache = au_fvdir_cache(file);
32678 +       deblk_sz = vdir_cache->vd_deblk_sz;
32679 +       while (1) {
32680 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32681 +               deblk_end.deblk += deblk_sz;
32682 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32683 +                       de = vdir_cache->vd_last.p.de;
32684 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32685 +                             de->de_str.len, de->de_str.name, ctx->pos,
32686 +                             (unsigned long)de->de_ino, de->de_type);
32687 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32688 +                                              de->de_str.len, de->de_ino,
32689 +                                              de->de_type))) {
32690 +                               /* todo: ignore the error caused by udba? */
32691 +                               /* return err; */
32692 +                               return 0;
32693 +                       }
32694 +
32695 +                       l = calc_size(de->de_str.len);
32696 +                       vdir_cache->vd_last.p.deblk += l;
32697 +                       ctx->pos += l;
32698 +               }
32699 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32700 +                       vdir_cache->vd_last.ul++;
32701 +                       vdir_cache->vd_last.p.deblk
32702 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32703 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32704 +                       continue;
32705 +               }
32706 +               break;
32707 +       }
32708 +
32709 +       /* smp_mb(); */
32710 +       return 0;
32711 +}
32712 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32713 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32714 +++ linux/fs/aufs/vfsub.c       2020-01-27 10:57:18.178871751 +0100
32715 @@ -0,0 +1,902 @@
32716 +// SPDX-License-Identifier: GPL-2.0
32717 +/*
32718 + * Copyright (C) 2005-2020 Junjiro R. Okajima
32719 + *
32720 + * This program, aufs is free software; you can redistribute it and/or modify
32721 + * it under the terms of the GNU General Public License as published by
32722 + * the Free Software Foundation; either version 2 of the License, or
32723 + * (at your option) any later version.
32724 + *
32725 + * This program is distributed in the hope that it will be useful,
32726 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32727 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32728 + * GNU General Public License for more details.
32729 + *
32730 + * You should have received a copy of the GNU General Public License
32731 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32732 + */
32733 +
32734 +/*
32735 + * sub-routines for VFS
32736 + */
32737 +
32738 +#include <linux/mnt_namespace.h>
32739 +#include <linux/namei.h>
32740 +#include <linux/nsproxy.h>
32741 +#include <linux/security.h>
32742 +#include <linux/splice.h>
32743 +#include "aufs.h"
32744 +
32745 +#ifdef CONFIG_AUFS_BR_FUSE
32746 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32747 +{
32748 +       if (!au_test_fuse(h_sb) || !au_userns)
32749 +               return 0;
32750 +
32751 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32752 +}
32753 +#endif
32754 +
32755 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32756 +{
32757 +       int err;
32758 +
32759 +       lockdep_off();
32760 +       down_read(&h_sb->s_umount);
32761 +       err = __sync_filesystem(h_sb, wait);
32762 +       up_read(&h_sb->s_umount);
32763 +       lockdep_on();
32764 +
32765 +       return err;
32766 +}
32767 +
32768 +/* ---------------------------------------------------------------------- */
32769 +
32770 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32771 +{
32772 +       int err;
32773 +       struct kstat st;
32774 +       struct super_block *h_sb;
32775 +
32776 +       /* for remote fs, leave work for its getattr or d_revalidate */
32777 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32778 +       /* still some fs may acquire i_mutex. we need to skip them */
32779 +       err = 0;
32780 +       if (!did)
32781 +               did = &err;
32782 +       h_sb = h_path->dentry->d_sb;
32783 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32784 +       if (*did)
32785 +               err = vfsub_getattr(h_path, &st);
32786 +
32787 +       return err;
32788 +}
32789 +
32790 +/* ---------------------------------------------------------------------- */
32791 +
32792 +struct file *vfsub_dentry_open(struct path *path, int flags)
32793 +{
32794 +       struct file *file;
32795 +
32796 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32797 +                          current_cred());
32798 +       if (!IS_ERR_OR_NULL(file)
32799 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
32800 +               i_readcount_inc(d_inode(path->dentry));
32801 +
32802 +       return file;
32803 +}
32804 +
32805 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32806 +{
32807 +       struct file *file;
32808 +
32809 +       lockdep_off();
32810 +       file = filp_open(path,
32811 +                        oflags /* | __FMODE_NONOTIFY */,
32812 +                        mode);
32813 +       lockdep_on();
32814 +       if (IS_ERR(file))
32815 +               goto out;
32816 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32817 +
32818 +out:
32819 +       return file;
32820 +}
32821 +
32822 +/*
32823 + * Ideally this function should call VFS:do_last() in order to keep all its
32824 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32825 + * structure such as nameidata. This is a second (or third) best approach.
32826 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32827 + */
32828 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32829 +                     struct vfsub_aopen_args *args)
32830 +{
32831 +       int err;
32832 +       struct au_branch *br = args->br;
32833 +       struct file *file = args->file;
32834 +       /* copied from linux/fs/namei.c:atomic_open() */
32835 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32836 +
32837 +       IMustLock(dir);
32838 +       AuDebugOn(!dir->i_op->atomic_open);
32839 +
32840 +       err = au_br_test_oflag(args->open_flag, br);
32841 +       if (unlikely(err))
32842 +               goto out;
32843 +
32844 +       au_lcnt_inc(&br->br_nfiles);
32845 +       file->f_path.dentry = DENTRY_NOT_SET;
32846 +       file->f_path.mnt = au_br_mnt(br);
32847 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32848 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32849 +                                    args->create_mode);
32850 +       if (unlikely(err < 0)) {
32851 +               au_lcnt_dec(&br->br_nfiles);
32852 +               goto out;
32853 +       }
32854 +
32855 +       /* temporary workaround for nfsv4 branch */
32856 +       if (au_test_nfs(dir->i_sb))
32857 +               nfs_mark_for_revalidate(dir);
32858 +
32859 +       if (file->f_mode & FMODE_CREATED)
32860 +               fsnotify_create(dir, dentry);
32861 +       if (!(file->f_mode & FMODE_OPENED)) {
32862 +               au_lcnt_dec(&br->br_nfiles);
32863 +               goto out;
32864 +       }
32865 +
32866 +       /* todo: call VFS:may_open() here */
32867 +       /* todo: ima_file_check() too? */
32868 +       if (!err && (args->open_flag & __FMODE_EXEC))
32869 +               err = deny_write_access(file);
32870 +       if (!err)
32871 +               fsnotify_open(file);
32872 +       else
32873 +               au_lcnt_dec(&br->br_nfiles);
32874 +       /* note that the file is created and still opened */
32875 +
32876 +out:
32877 +       return err;
32878 +}
32879 +
32880 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32881 +{
32882 +       int err;
32883 +
32884 +       err = kern_path(name, flags, path);
32885 +       if (!err && d_is_positive(path->dentry))
32886 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32887 +       return err;
32888 +}
32889 +
32890 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32891 +                                            struct dentry *parent, int len)
32892 +{
32893 +       struct path path = {
32894 +               .mnt = NULL
32895 +       };
32896 +
32897 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32898 +       if (IS_ERR(path.dentry))
32899 +               goto out;
32900 +       if (d_is_positive(path.dentry))
32901 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32902 +
32903 +out:
32904 +       AuTraceErrPtr(path.dentry);
32905 +       return path.dentry;
32906 +}
32907 +
32908 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32909 +                                   int len)
32910 +{
32911 +       struct path path = {
32912 +               .mnt = NULL
32913 +       };
32914 +
32915 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32916 +       IMustLock(d_inode(parent));
32917 +
32918 +       path.dentry = lookup_one_len(name, parent, len);
32919 +       if (IS_ERR(path.dentry))
32920 +               goto out;
32921 +       if (d_is_positive(path.dentry))
32922 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32923 +
32924 +out:
32925 +       AuTraceErrPtr(path.dentry);
32926 +       return path.dentry;
32927 +}
32928 +
32929 +void vfsub_call_lkup_one(void *args)
32930 +{
32931 +       struct vfsub_lkup_one_args *a = args;
32932 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32933 +}
32934 +
32935 +/* ---------------------------------------------------------------------- */
32936 +
32937 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32938 +                                struct dentry *d2, struct au_hinode *hdir2)
32939 +{
32940 +       struct dentry *d;
32941 +
32942 +       lockdep_off();
32943 +       d = lock_rename(d1, d2);
32944 +       lockdep_on();
32945 +       au_hn_suspend(hdir1);
32946 +       if (hdir1 != hdir2)
32947 +               au_hn_suspend(hdir2);
32948 +
32949 +       return d;
32950 +}
32951 +
32952 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32953 +                        struct dentry *d2, struct au_hinode *hdir2)
32954 +{
32955 +       au_hn_resume(hdir1);
32956 +       if (hdir1 != hdir2)
32957 +               au_hn_resume(hdir2);
32958 +       lockdep_off();
32959 +       unlock_rename(d1, d2);
32960 +       lockdep_on();
32961 +}
32962 +
32963 +/* ---------------------------------------------------------------------- */
32964 +
32965 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32966 +{
32967 +       int err;
32968 +       struct dentry *d;
32969 +
32970 +       IMustLock(dir);
32971 +
32972 +       d = path->dentry;
32973 +       path->dentry = d->d_parent;
32974 +       err = security_path_mknod(path, d, mode, 0);
32975 +       path->dentry = d;
32976 +       if (unlikely(err))
32977 +               goto out;
32978 +
32979 +       lockdep_off();
32980 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32981 +       lockdep_on();
32982 +       if (!err) {
32983 +               struct path tmp = *path;
32984 +               int did;
32985 +
32986 +               vfsub_update_h_iattr(&tmp, &did);
32987 +               if (did) {
32988 +                       tmp.dentry = path->dentry->d_parent;
32989 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32990 +               }
32991 +               /*ignore*/
32992 +       }
32993 +
32994 +out:
32995 +       return err;
32996 +}
32997 +
32998 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32999 +{
33000 +       int err;
33001 +       struct dentry *d;
33002 +
33003 +       IMustLock(dir);
33004 +
33005 +       d = path->dentry;
33006 +       path->dentry = d->d_parent;
33007 +       err = security_path_symlink(path, d, symname);
33008 +       path->dentry = d;
33009 +       if (unlikely(err))
33010 +               goto out;
33011 +
33012 +       lockdep_off();
33013 +       err = vfs_symlink(dir, path->dentry, symname);
33014 +       lockdep_on();
33015 +       if (!err) {
33016 +               struct path tmp = *path;
33017 +               int did;
33018 +
33019 +               vfsub_update_h_iattr(&tmp, &did);
33020 +               if (did) {
33021 +                       tmp.dentry = path->dentry->d_parent;
33022 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33023 +               }
33024 +               /*ignore*/
33025 +       }
33026 +
33027 +out:
33028 +       return err;
33029 +}
33030 +
33031 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33032 +{
33033 +       int err;
33034 +       struct dentry *d;
33035 +
33036 +       IMustLock(dir);
33037 +
33038 +       d = path->dentry;
33039 +       path->dentry = d->d_parent;
33040 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33041 +       path->dentry = d;
33042 +       if (unlikely(err))
33043 +               goto out;
33044 +
33045 +       lockdep_off();
33046 +       err = vfs_mknod(dir, path->dentry, mode, dev);
33047 +       lockdep_on();
33048 +       if (!err) {
33049 +               struct path tmp = *path;
33050 +               int did;
33051 +
33052 +               vfsub_update_h_iattr(&tmp, &did);
33053 +               if (did) {
33054 +                       tmp.dentry = path->dentry->d_parent;
33055 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33056 +               }
33057 +               /*ignore*/
33058 +       }
33059 +
33060 +out:
33061 +       return err;
33062 +}
33063 +
33064 +static int au_test_nlink(struct inode *inode)
33065 +{
33066 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33067 +
33068 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33069 +           || inode->i_nlink < link_max)
33070 +               return 0;
33071 +       return -EMLINK;
33072 +}
33073 +
33074 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33075 +              struct inode **delegated_inode)
33076 +{
33077 +       int err;
33078 +       struct dentry *d;
33079 +
33080 +       IMustLock(dir);
33081 +
33082 +       err = au_test_nlink(d_inode(src_dentry));
33083 +       if (unlikely(err))
33084 +               return err;
33085 +
33086 +       /* we don't call may_linkat() */
33087 +       d = path->dentry;
33088 +       path->dentry = d->d_parent;
33089 +       err = security_path_link(src_dentry, path, d);
33090 +       path->dentry = d;
33091 +       if (unlikely(err))
33092 +               goto out;
33093 +
33094 +       lockdep_off();
33095 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
33096 +       lockdep_on();
33097 +       if (!err) {
33098 +               struct path tmp = *path;
33099 +               int did;
33100 +
33101 +               /* fuse has different memory inode for the same inumber */
33102 +               vfsub_update_h_iattr(&tmp, &did);
33103 +               if (did) {
33104 +                       tmp.dentry = path->dentry->d_parent;
33105 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33106 +                       tmp.dentry = src_dentry;
33107 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33108 +               }
33109 +               /*ignore*/
33110 +       }
33111 +
33112 +out:
33113 +       return err;
33114 +}
33115 +
33116 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33117 +                struct inode *dir, struct path *path,
33118 +                struct inode **delegated_inode, unsigned int flags)
33119 +{
33120 +       int err;
33121 +       struct path tmp = {
33122 +               .mnt    = path->mnt
33123 +       };
33124 +       struct dentry *d;
33125 +
33126 +       IMustLock(dir);
33127 +       IMustLock(src_dir);
33128 +
33129 +       d = path->dentry;
33130 +       path->dentry = d->d_parent;
33131 +       tmp.dentry = src_dentry->d_parent;
33132 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33133 +       path->dentry = d;
33134 +       if (unlikely(err))
33135 +               goto out;
33136 +
33137 +       lockdep_off();
33138 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33139 +                        delegated_inode, flags);
33140 +       lockdep_on();
33141 +       if (!err) {
33142 +               int did;
33143 +
33144 +               tmp.dentry = d->d_parent;
33145 +               vfsub_update_h_iattr(&tmp, &did);
33146 +               if (did) {
33147 +                       tmp.dentry = src_dentry;
33148 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33149 +                       tmp.dentry = src_dentry->d_parent;
33150 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33151 +               }
33152 +               /*ignore*/
33153 +       }
33154 +
33155 +out:
33156 +       return err;
33157 +}
33158 +
33159 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33160 +{
33161 +       int err;
33162 +       struct dentry *d;
33163 +
33164 +       IMustLock(dir);
33165 +
33166 +       d = path->dentry;
33167 +       path->dentry = d->d_parent;
33168 +       err = security_path_mkdir(path, d, mode);
33169 +       path->dentry = d;
33170 +       if (unlikely(err))
33171 +               goto out;
33172 +
33173 +       lockdep_off();
33174 +       err = vfs_mkdir(dir, path->dentry, mode);
33175 +       lockdep_on();
33176 +       if (!err) {
33177 +               struct path tmp = *path;
33178 +               int did;
33179 +
33180 +               vfsub_update_h_iattr(&tmp, &did);
33181 +               if (did) {
33182 +                       tmp.dentry = path->dentry->d_parent;
33183 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33184 +               }
33185 +               /*ignore*/
33186 +       }
33187 +
33188 +out:
33189 +       return err;
33190 +}
33191 +
33192 +int vfsub_rmdir(struct inode *dir, struct path *path)
33193 +{
33194 +       int err;
33195 +       struct dentry *d;
33196 +
33197 +       IMustLock(dir);
33198 +
33199 +       d = path->dentry;
33200 +       path->dentry = d->d_parent;
33201 +       err = security_path_rmdir(path, d);
33202 +       path->dentry = d;
33203 +       if (unlikely(err))
33204 +               goto out;
33205 +
33206 +       lockdep_off();
33207 +       err = vfs_rmdir(dir, path->dentry);
33208 +       lockdep_on();
33209 +       if (!err) {
33210 +               struct path tmp = {
33211 +                       .dentry = path->dentry->d_parent,
33212 +                       .mnt    = path->mnt
33213 +               };
33214 +
33215 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33216 +       }
33217 +
33218 +out:
33219 +       return err;
33220 +}
33221 +
33222 +/* ---------------------------------------------------------------------- */
33223 +
33224 +/* todo: support mmap_sem? */
33225 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33226 +                    loff_t *ppos)
33227 +{
33228 +       ssize_t err;
33229 +
33230 +       lockdep_off();
33231 +       err = vfs_read(file, ubuf, count, ppos);
33232 +       lockdep_on();
33233 +       if (err >= 0)
33234 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33235 +       return err;
33236 +}
33237 +
33238 +/* todo: kernel_read()? */
33239 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33240 +                    loff_t *ppos)
33241 +{
33242 +       ssize_t err;
33243 +       mm_segment_t oldfs;
33244 +       union {
33245 +               void *k;
33246 +               char __user *u;
33247 +       } buf;
33248 +
33249 +       buf.k = kbuf;
33250 +       oldfs = get_fs();
33251 +       set_fs(KERNEL_DS);
33252 +       err = vfsub_read_u(file, buf.u, count, ppos);
33253 +       set_fs(oldfs);
33254 +       return err;
33255 +}
33256 +
33257 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33258 +                     loff_t *ppos)
33259 +{
33260 +       ssize_t err;
33261 +
33262 +       lockdep_off();
33263 +       err = vfs_write(file, ubuf, count, ppos);
33264 +       lockdep_on();
33265 +       if (err >= 0)
33266 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33267 +       return err;
33268 +}
33269 +
33270 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33271 +{
33272 +       ssize_t err;
33273 +       mm_segment_t oldfs;
33274 +       union {
33275 +               void *k;
33276 +               const char __user *u;
33277 +       } buf;
33278 +
33279 +       buf.k = kbuf;
33280 +       oldfs = get_fs();
33281 +       set_fs(KERNEL_DS);
33282 +       err = vfsub_write_u(file, buf.u, count, ppos);
33283 +       set_fs(oldfs);
33284 +       return err;
33285 +}
33286 +
33287 +int vfsub_flush(struct file *file, fl_owner_t id)
33288 +{
33289 +       int err;
33290 +
33291 +       err = 0;
33292 +       if (file->f_op->flush) {
33293 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33294 +                       err = file->f_op->flush(file, id);
33295 +               else {
33296 +                       lockdep_off();
33297 +                       err = file->f_op->flush(file, id);
33298 +                       lockdep_on();
33299 +               }
33300 +               if (!err)
33301 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33302 +               /*ignore*/
33303 +       }
33304 +       return err;
33305 +}
33306 +
33307 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33308 +{
33309 +       int err;
33310 +
33311 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33312 +
33313 +       lockdep_off();
33314 +       err = iterate_dir(file, ctx);
33315 +       lockdep_on();
33316 +       if (err >= 0)
33317 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33318 +
33319 +       return err;
33320 +}
33321 +
33322 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33323 +                    struct pipe_inode_info *pipe, size_t len,
33324 +                    unsigned int flags)
33325 +{
33326 +       long err;
33327 +
33328 +       lockdep_off();
33329 +       err = do_splice_to(in, ppos, pipe, len, flags);
33330 +       lockdep_on();
33331 +       file_accessed(in);
33332 +       if (err >= 0)
33333 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33334 +       return err;
33335 +}
33336 +
33337 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33338 +                      loff_t *ppos, size_t len, unsigned int flags)
33339 +{
33340 +       long err;
33341 +
33342 +       lockdep_off();
33343 +       err = do_splice_from(pipe, out, ppos, len, flags);
33344 +       lockdep_on();
33345 +       if (err >= 0)
33346 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33347 +       return err;
33348 +}
33349 +
33350 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33351 +{
33352 +       int err;
33353 +
33354 +       /* file can be NULL */
33355 +       lockdep_off();
33356 +       err = vfs_fsync(file, datasync);
33357 +       lockdep_on();
33358 +       if (!err) {
33359 +               if (!path) {
33360 +                       AuDebugOn(!file);
33361 +                       path = &file->f_path;
33362 +               }
33363 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33364 +       }
33365 +       return err;
33366 +}
33367 +
33368 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33369 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33370 +               struct file *h_file)
33371 +{
33372 +       int err;
33373 +       struct inode *h_inode;
33374 +       struct super_block *h_sb;
33375 +
33376 +       if (!h_file) {
33377 +               err = vfsub_truncate(h_path, length);
33378 +               goto out;
33379 +       }
33380 +
33381 +       h_inode = d_inode(h_path->dentry);
33382 +       h_sb = h_inode->i_sb;
33383 +       lockdep_off();
33384 +       sb_start_write(h_sb);
33385 +       lockdep_on();
33386 +       err = locks_verify_truncate(h_inode, h_file, length);
33387 +       if (!err)
33388 +               err = security_path_truncate(h_path);
33389 +       if (!err) {
33390 +               lockdep_off();
33391 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33392 +               lockdep_on();
33393 +       }
33394 +       lockdep_off();
33395 +       sb_end_write(h_sb);
33396 +       lockdep_on();
33397 +
33398 +out:
33399 +       return err;
33400 +}
33401 +
33402 +/* ---------------------------------------------------------------------- */
33403 +
33404 +struct au_vfsub_mkdir_args {
33405 +       int *errp;
33406 +       struct inode *dir;
33407 +       struct path *path;
33408 +       int mode;
33409 +};
33410 +
33411 +static void au_call_vfsub_mkdir(void *args)
33412 +{
33413 +       struct au_vfsub_mkdir_args *a = args;
33414 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33415 +}
33416 +
33417 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33418 +{
33419 +       int err, do_sio, wkq_err;
33420 +
33421 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33422 +       if (!do_sio) {
33423 +               lockdep_off();
33424 +               err = vfsub_mkdir(dir, path, mode);
33425 +               lockdep_on();
33426 +       } else {
33427 +               struct au_vfsub_mkdir_args args = {
33428 +                       .errp   = &err,
33429 +                       .dir    = dir,
33430 +                       .path   = path,
33431 +                       .mode   = mode
33432 +               };
33433 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33434 +               if (unlikely(wkq_err))
33435 +                       err = wkq_err;
33436 +       }
33437 +
33438 +       return err;
33439 +}
33440 +
33441 +struct au_vfsub_rmdir_args {
33442 +       int *errp;
33443 +       struct inode *dir;
33444 +       struct path *path;
33445 +};
33446 +
33447 +static void au_call_vfsub_rmdir(void *args)
33448 +{
33449 +       struct au_vfsub_rmdir_args *a = args;
33450 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33451 +}
33452 +
33453 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33454 +{
33455 +       int err, do_sio, wkq_err;
33456 +
33457 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33458 +       if (!do_sio) {
33459 +               lockdep_off();
33460 +               err = vfsub_rmdir(dir, path);
33461 +               lockdep_on();
33462 +       } else {
33463 +               struct au_vfsub_rmdir_args args = {
33464 +                       .errp   = &err,
33465 +                       .dir    = dir,
33466 +                       .path   = path
33467 +               };
33468 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33469 +               if (unlikely(wkq_err))
33470 +                       err = wkq_err;
33471 +       }
33472 +
33473 +       return err;
33474 +}
33475 +
33476 +/* ---------------------------------------------------------------------- */
33477 +
33478 +struct notify_change_args {
33479 +       int *errp;
33480 +       struct path *path;
33481 +       struct iattr *ia;
33482 +       struct inode **delegated_inode;
33483 +};
33484 +
33485 +static void call_notify_change(void *args)
33486 +{
33487 +       struct notify_change_args *a = args;
33488 +       struct inode *h_inode;
33489 +
33490 +       h_inode = d_inode(a->path->dentry);
33491 +       IMustLock(h_inode);
33492 +
33493 +       *a->errp = -EPERM;
33494 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33495 +               lockdep_off();
33496 +               *a->errp = notify_change(a->path->dentry, a->ia,
33497 +                                        a->delegated_inode);
33498 +               lockdep_on();
33499 +               if (!*a->errp)
33500 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33501 +       }
33502 +       AuTraceErr(*a->errp);
33503 +}
33504 +
33505 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33506 +                       struct inode **delegated_inode)
33507 +{
33508 +       int err;
33509 +       struct notify_change_args args = {
33510 +               .errp                   = &err,
33511 +               .path                   = path,
33512 +               .ia                     = ia,
33513 +               .delegated_inode        = delegated_inode
33514 +       };
33515 +
33516 +       call_notify_change(&args);
33517 +
33518 +       return err;
33519 +}
33520 +
33521 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33522 +                           struct inode **delegated_inode)
33523 +{
33524 +       int err, wkq_err;
33525 +       struct notify_change_args args = {
33526 +               .errp                   = &err,
33527 +               .path                   = path,
33528 +               .ia                     = ia,
33529 +               .delegated_inode        = delegated_inode
33530 +       };
33531 +
33532 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33533 +       if (unlikely(wkq_err))
33534 +               err = wkq_err;
33535 +
33536 +       return err;
33537 +}
33538 +
33539 +/* ---------------------------------------------------------------------- */
33540 +
33541 +struct unlink_args {
33542 +       int *errp;
33543 +       struct inode *dir;
33544 +       struct path *path;
33545 +       struct inode **delegated_inode;
33546 +};
33547 +
33548 +static void call_unlink(void *args)
33549 +{
33550 +       struct unlink_args *a = args;
33551 +       struct dentry *d = a->path->dentry;
33552 +       struct inode *h_inode;
33553 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33554 +                                     && au_dcount(d) == 1);
33555 +
33556 +       IMustLock(a->dir);
33557 +
33558 +       a->path->dentry = d->d_parent;
33559 +       *a->errp = security_path_unlink(a->path, d);
33560 +       a->path->dentry = d;
33561 +       if (unlikely(*a->errp))
33562 +               return;
33563 +
33564 +       if (!stop_sillyrename)
33565 +               dget(d);
33566 +       h_inode = NULL;
33567 +       if (d_is_positive(d)) {
33568 +               h_inode = d_inode(d);
33569 +               ihold(h_inode);
33570 +       }
33571 +
33572 +       lockdep_off();
33573 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33574 +       lockdep_on();
33575 +       if (!*a->errp) {
33576 +               struct path tmp = {
33577 +                       .dentry = d->d_parent,
33578 +                       .mnt    = a->path->mnt
33579 +               };
33580 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33581 +       }
33582 +
33583 +       if (!stop_sillyrename)
33584 +               dput(d);
33585 +       if (h_inode)
33586 +               iput(h_inode);
33587 +
33588 +       AuTraceErr(*a->errp);
33589 +}
33590 +
33591 +/*
33592 + * @dir: must be locked.
33593 + * @dentry: target dentry.
33594 + */
33595 +int vfsub_unlink(struct inode *dir, struct path *path,
33596 +                struct inode **delegated_inode, int force)
33597 +{
33598 +       int err;
33599 +       struct unlink_args args = {
33600 +               .errp                   = &err,
33601 +               .dir                    = dir,
33602 +               .path                   = path,
33603 +               .delegated_inode        = delegated_inode
33604 +       };
33605 +
33606 +       if (!force)
33607 +               call_unlink(&args);
33608 +       else {
33609 +               int wkq_err;
33610 +
33611 +               wkq_err = au_wkq_wait(call_unlink, &args);
33612 +               if (unlikely(wkq_err))
33613 +                       err = wkq_err;
33614 +       }
33615 +
33616 +       return err;
33617 +}
33618 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33619 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33620 +++ linux/fs/aufs/vfsub.h       2020-01-27 10:57:18.178871751 +0100
33621 @@ -0,0 +1,354 @@
33622 +/* SPDX-License-Identifier: GPL-2.0 */
33623 +/*
33624 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33625 + *
33626 + * This program, aufs is free software; you can redistribute it and/or modify
33627 + * it under the terms of the GNU General Public License as published by
33628 + * the Free Software Foundation; either version 2 of the License, or
33629 + * (at your option) any later version.
33630 + *
33631 + * This program is distributed in the hope that it will be useful,
33632 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33633 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33634 + * GNU General Public License for more details.
33635 + *
33636 + * You should have received a copy of the GNU General Public License
33637 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33638 + */
33639 +
33640 +/*
33641 + * sub-routines for VFS
33642 + */
33643 +
33644 +#ifndef __AUFS_VFSUB_H__
33645 +#define __AUFS_VFSUB_H__
33646 +
33647 +#ifdef __KERNEL__
33648 +
33649 +#include <linux/fs.h>
33650 +#include <linux/mount.h>
33651 +#include <linux/posix_acl.h>
33652 +#include <linux/xattr.h>
33653 +#include "debug.h"
33654 +
33655 +/* copied from linux/fs/internal.h */
33656 +/* todo: BAD approach!! */
33657 +extern void __mnt_drop_write(struct vfsmount *);
33658 +extern struct file *alloc_empty_file(int, const struct cred *);
33659 +
33660 +/* ---------------------------------------------------------------------- */
33661 +
33662 +/* lock subclass for lower inode */
33663 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33664 +/* reduce? gave up. */
33665 +enum {
33666 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33667 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33668 +       AuLsc_I_PARENT2,        /* copyup dirs */
33669 +       AuLsc_I_PARENT3,        /* copyup wh */
33670 +       AuLsc_I_CHILD,
33671 +       AuLsc_I_CHILD2,
33672 +       AuLsc_I_End
33673 +};
33674 +
33675 +/* to debug easier, do not make them inlined functions */
33676 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33677 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33678 +
33679 +/* ---------------------------------------------------------------------- */
33680 +
33681 +static inline void vfsub_drop_nlink(struct inode *inode)
33682 +{
33683 +       AuDebugOn(!inode->i_nlink);
33684 +       drop_nlink(inode);
33685 +}
33686 +
33687 +static inline void vfsub_dead_dir(struct inode *inode)
33688 +{
33689 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33690 +       inode->i_flags |= S_DEAD;
33691 +       clear_nlink(inode);
33692 +}
33693 +
33694 +static inline int vfsub_native_ro(struct inode *inode)
33695 +{
33696 +       return sb_rdonly(inode->i_sb)
33697 +               || IS_RDONLY(inode)
33698 +               /* || IS_APPEND(inode) */
33699 +               || IS_IMMUTABLE(inode);
33700 +}
33701 +
33702 +#ifdef CONFIG_AUFS_BR_FUSE
33703 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33704 +#else
33705 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33706 +#endif
33707 +
33708 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33709 +
33710 +/* ---------------------------------------------------------------------- */
33711 +
33712 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33713 +struct file *vfsub_dentry_open(struct path *path, int flags);
33714 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33715 +struct au_branch;
33716 +struct vfsub_aopen_args {
33717 +       struct file             *file;
33718 +       unsigned int            open_flag;
33719 +       umode_t                 create_mode;
33720 +       struct au_branch        *br;
33721 +};
33722 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33723 +                     struct vfsub_aopen_args *args);
33724 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33725 +
33726 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33727 +                                            struct dentry *parent, int len);
33728 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33729 +                                   int len);
33730 +
33731 +struct vfsub_lkup_one_args {
33732 +       struct dentry **errp;
33733 +       struct qstr *name;
33734 +       struct dentry *parent;
33735 +};
33736 +
33737 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33738 +                                           struct dentry *parent)
33739 +{
33740 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33741 +}
33742 +
33743 +void vfsub_call_lkup_one(void *args);
33744 +
33745 +/* ---------------------------------------------------------------------- */
33746 +
33747 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33748 +{
33749 +       int err;
33750 +
33751 +       lockdep_off();
33752 +       err = mnt_want_write(mnt);
33753 +       lockdep_on();
33754 +       return err;
33755 +}
33756 +
33757 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33758 +{
33759 +       lockdep_off();
33760 +       mnt_drop_write(mnt);
33761 +       lockdep_on();
33762 +}
33763 +
33764 +#if 0 /* reserved */
33765 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33766 +{
33767 +       lockdep_off();
33768 +       mnt_drop_write_file(file);
33769 +       lockdep_on();
33770 +}
33771 +#endif
33772 +
33773 +/* ---------------------------------------------------------------------- */
33774 +
33775 +struct au_hinode;
33776 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33777 +                                struct dentry *d2, struct au_hinode *hdir2);
33778 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33779 +                        struct dentry *d2, struct au_hinode *hdir2);
33780 +
33781 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33782 +                bool want_excl);
33783 +int vfsub_symlink(struct inode *dir, struct path *path,
33784 +                 const char *symname);
33785 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33786 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33787 +              struct path *path, struct inode **delegated_inode);
33788 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33789 +                struct inode *hdir, struct path *path,
33790 +                struct inode **delegated_inode, unsigned int flags);
33791 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33792 +int vfsub_rmdir(struct inode *dir, struct path *path);
33793 +
33794 +/* ---------------------------------------------------------------------- */
33795 +
33796 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33797 +                    loff_t *ppos);
33798 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33799 +                       loff_t *ppos);
33800 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33801 +                     loff_t *ppos);
33802 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33803 +                     loff_t *ppos);
33804 +int vfsub_flush(struct file *file, fl_owner_t id);
33805 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33806 +
33807 +static inline loff_t vfsub_f_size_read(struct file *file)
33808 +{
33809 +       return i_size_read(file_inode(file));
33810 +}
33811 +
33812 +static inline unsigned int vfsub_file_flags(struct file *file)
33813 +{
33814 +       unsigned int flags;
33815 +
33816 +       spin_lock(&file->f_lock);
33817 +       flags = file->f_flags;
33818 +       spin_unlock(&file->f_lock);
33819 +
33820 +       return flags;
33821 +}
33822 +
33823 +static inline int vfsub_file_execed(struct file *file)
33824 +{
33825 +       /* todo: direct access f_flags */
33826 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33827 +}
33828 +
33829 +#if 0 /* reserved */
33830 +static inline void vfsub_file_accessed(struct file *h_file)
33831 +{
33832 +       file_accessed(h_file);
33833 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33834 +}
33835 +#endif
33836 +
33837 +#if 0 /* reserved */
33838 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33839 +                                    struct dentry *h_dentry)
33840 +{
33841 +       struct path h_path = {
33842 +               .dentry = h_dentry,
33843 +               .mnt    = h_mnt
33844 +       };
33845 +       touch_atime(&h_path);
33846 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33847 +}
33848 +#endif
33849 +
33850 +static inline int vfsub_update_time(struct inode *h_inode,
33851 +                                   struct timespec64 *ts, int flags)
33852 +{
33853 +       return update_time(h_inode, ts, flags);
33854 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33855 +}
33856 +
33857 +#ifdef CONFIG_FS_POSIX_ACL
33858 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33859 +{
33860 +       int err;
33861 +
33862 +       err = posix_acl_chmod(h_inode, h_mode);
33863 +       if (err == -EOPNOTSUPP)
33864 +               err = 0;
33865 +       return err;
33866 +}
33867 +#else
33868 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33869 +#endif
33870 +
33871 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33872 +                    struct pipe_inode_info *pipe, size_t len,
33873 +                    unsigned int flags);
33874 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33875 +                      loff_t *ppos, size_t len, unsigned int flags);
33876 +
33877 +static inline long vfsub_truncate(struct path *path, loff_t length)
33878 +{
33879 +       long err;
33880 +
33881 +       lockdep_off();
33882 +       err = vfs_truncate(path, length);
33883 +       lockdep_on();
33884 +       return err;
33885 +}
33886 +
33887 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33888 +               struct file *h_file);
33889 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33890 +
33891 +/*
33892 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33893 + * ioctl.
33894 + */
33895 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33896 +                                           loff_t len)
33897 +{
33898 +       loff_t err;
33899 +
33900 +       lockdep_off();
33901 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33902 +       lockdep_on();
33903 +
33904 +       return err;
33905 +}
33906 +
33907 +/* copy_file_range(2) is a systemcall */
33908 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33909 +                                           struct file *dst, loff_t dst_pos,
33910 +                                           size_t len, unsigned int flags)
33911 +{
33912 +       ssize_t ssz;
33913 +
33914 +       lockdep_off();
33915 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33916 +       lockdep_on();
33917 +
33918 +       return ssz;
33919 +}
33920 +
33921 +/* ---------------------------------------------------------------------- */
33922 +
33923 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33924 +{
33925 +       loff_t err;
33926 +
33927 +       lockdep_off();
33928 +       err = vfs_llseek(file, offset, origin);
33929 +       lockdep_on();
33930 +       return err;
33931 +}
33932 +
33933 +/* ---------------------------------------------------------------------- */
33934 +
33935 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33936 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33937 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33938 +                           struct inode **delegated_inode);
33939 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33940 +                       struct inode **delegated_inode);
33941 +int vfsub_unlink(struct inode *dir, struct path *path,
33942 +                struct inode **delegated_inode, int force);
33943 +
33944 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33945 +{
33946 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33947 +}
33948 +
33949 +/* ---------------------------------------------------------------------- */
33950 +
33951 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33952 +                                const void *value, size_t size, int flags)
33953 +{
33954 +       int err;
33955 +
33956 +       lockdep_off();
33957 +       err = vfs_setxattr(dentry, name, value, size, flags);
33958 +       lockdep_on();
33959 +
33960 +       return err;
33961 +}
33962 +
33963 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33964 +{
33965 +       int err;
33966 +
33967 +       lockdep_off();
33968 +       err = vfs_removexattr(dentry, name);
33969 +       lockdep_on();
33970 +
33971 +       return err;
33972 +}
33973 +
33974 +#endif /* __KERNEL__ */
33975 +#endif /* __AUFS_VFSUB_H__ */
33976 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33977 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33978 +++ linux/fs/aufs/wbr_policy.c  2020-01-27 10:57:18.178871751 +0100
33979 @@ -0,0 +1,830 @@
33980 +// SPDX-License-Identifier: GPL-2.0
33981 +/*
33982 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33983 + *
33984 + * This program, aufs is free software; you can redistribute it and/or modify
33985 + * it under the terms of the GNU General Public License as published by
33986 + * the Free Software Foundation; either version 2 of the License, or
33987 + * (at your option) any later version.
33988 + *
33989 + * This program is distributed in the hope that it will be useful,
33990 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33991 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33992 + * GNU General Public License for more details.
33993 + *
33994 + * You should have received a copy of the GNU General Public License
33995 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33996 + */
33997 +
33998 +/*
33999 + * policies for selecting one among multiple writable branches
34000 + */
34001 +
34002 +#include <linux/statfs.h>
34003 +#include "aufs.h"
34004 +
34005 +/* subset of cpup_attr() */
34006 +static noinline_for_stack
34007 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34008 +{
34009 +       int err, sbits;
34010 +       struct iattr ia;
34011 +       struct inode *h_isrc;
34012 +
34013 +       h_isrc = d_inode(h_src);
34014 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34015 +       ia.ia_mode = h_isrc->i_mode;
34016 +       ia.ia_uid = h_isrc->i_uid;
34017 +       ia.ia_gid = h_isrc->i_gid;
34018 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34019 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34020 +       /* no delegation since it is just created */
34021 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34022 +
34023 +       /* is this nfs only? */
34024 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34025 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34026 +               ia.ia_mode = h_isrc->i_mode;
34027 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34028 +       }
34029 +
34030 +       return err;
34031 +}
34032 +
34033 +#define AuCpdown_PARENT_OPQ    1
34034 +#define AuCpdown_WHED          (1 << 1)
34035 +#define AuCpdown_MADE_DIR      (1 << 2)
34036 +#define AuCpdown_DIROPQ                (1 << 3)
34037 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34038 +#define au_fset_cpdown(flags, name) \
34039 +       do { (flags) |= AuCpdown_##name; } while (0)
34040 +#define au_fclr_cpdown(flags, name) \
34041 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34042 +
34043 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34044 +                            unsigned int *flags)
34045 +{
34046 +       int err;
34047 +       struct dentry *opq_dentry;
34048 +
34049 +       opq_dentry = au_diropq_create(dentry, bdst);
34050 +       err = PTR_ERR(opq_dentry);
34051 +       if (IS_ERR(opq_dentry))
34052 +               goto out;
34053 +       dput(opq_dentry);
34054 +       au_fset_cpdown(*flags, DIROPQ);
34055 +
34056 +out:
34057 +       return err;
34058 +}
34059 +
34060 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34061 +                           struct inode *dir, aufs_bindex_t bdst)
34062 +{
34063 +       int err;
34064 +       struct path h_path;
34065 +       struct au_branch *br;
34066 +
34067 +       br = au_sbr(dentry->d_sb, bdst);
34068 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34069 +       err = PTR_ERR(h_path.dentry);
34070 +       if (IS_ERR(h_path.dentry))
34071 +               goto out;
34072 +
34073 +       err = 0;
34074 +       if (d_is_positive(h_path.dentry)) {
34075 +               h_path.mnt = au_br_mnt(br);
34076 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34077 +                                         dentry);
34078 +       }
34079 +       dput(h_path.dentry);
34080 +
34081 +out:
34082 +       return err;
34083 +}
34084 +
34085 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34086 +                        struct au_pin *pin,
34087 +                        struct dentry *h_parent, void *arg)
34088 +{
34089 +       int err, rerr;
34090 +       aufs_bindex_t bopq, btop;
34091 +       struct path h_path;
34092 +       struct dentry *parent;
34093 +       struct inode *h_dir, *h_inode, *inode, *dir;
34094 +       unsigned int *flags = arg;
34095 +
34096 +       btop = au_dbtop(dentry);
34097 +       /* dentry is di-locked */
34098 +       parent = dget_parent(dentry);
34099 +       dir = d_inode(parent);
34100 +       h_dir = d_inode(h_parent);
34101 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34102 +       IMustLock(h_dir);
34103 +
34104 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34105 +       if (unlikely(err < 0))
34106 +               goto out;
34107 +       h_path.dentry = au_h_dptr(dentry, bdst);
34108 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34109 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34110 +       if (unlikely(err))
34111 +               goto out_put;
34112 +       au_fset_cpdown(*flags, MADE_DIR);
34113 +
34114 +       bopq = au_dbdiropq(dentry);
34115 +       au_fclr_cpdown(*flags, WHED);
34116 +       au_fclr_cpdown(*flags, DIROPQ);
34117 +       if (au_dbwh(dentry) == bdst)
34118 +               au_fset_cpdown(*flags, WHED);
34119 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34120 +               au_fset_cpdown(*flags, PARENT_OPQ);
34121 +       h_inode = d_inode(h_path.dentry);
34122 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34123 +       if (au_ftest_cpdown(*flags, WHED)) {
34124 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34125 +               if (unlikely(err)) {
34126 +                       inode_unlock(h_inode);
34127 +                       goto out_dir;
34128 +               }
34129 +       }
34130 +
34131 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34132 +       inode_unlock(h_inode);
34133 +       if (unlikely(err))
34134 +               goto out_opq;
34135 +
34136 +       if (au_ftest_cpdown(*flags, WHED)) {
34137 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34138 +               if (unlikely(err))
34139 +                       goto out_opq;
34140 +       }
34141 +
34142 +       inode = d_inode(dentry);
34143 +       if (au_ibbot(inode) < bdst)
34144 +               au_set_ibbot(inode, bdst);
34145 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34146 +                     au_hi_flags(inode, /*isdir*/1));
34147 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34148 +       goto out; /* success */
34149 +
34150 +       /* revert */
34151 +out_opq:
34152 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34153 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34154 +               rerr = au_diropq_remove(dentry, bdst);
34155 +               inode_unlock(h_inode);
34156 +               if (unlikely(rerr)) {
34157 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34158 +                               dentry, bdst, rerr);
34159 +                       err = -EIO;
34160 +                       goto out;
34161 +               }
34162 +       }
34163 +out_dir:
34164 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34165 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34166 +               if (unlikely(rerr)) {
34167 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34168 +                               dentry, bdst, rerr);
34169 +                       err = -EIO;
34170 +               }
34171 +       }
34172 +out_put:
34173 +       au_set_h_dptr(dentry, bdst, NULL);
34174 +       if (au_dbbot(dentry) == bdst)
34175 +               au_update_dbbot(dentry);
34176 +out:
34177 +       dput(parent);
34178 +       return err;
34179 +}
34180 +
34181 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34182 +{
34183 +       int err;
34184 +       unsigned int flags;
34185 +
34186 +       flags = 0;
34187 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34188 +
34189 +       return err;
34190 +}
34191 +
34192 +/* ---------------------------------------------------------------------- */
34193 +
34194 +/* policies for create */
34195 +
34196 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34197 +{
34198 +       int err, i, j, ndentry;
34199 +       aufs_bindex_t bopq;
34200 +       struct au_dcsub_pages dpages;
34201 +       struct au_dpage *dpage;
34202 +       struct dentry **dentries, *parent, *d;
34203 +
34204 +       err = au_dpages_init(&dpages, GFP_NOFS);
34205 +       if (unlikely(err))
34206 +               goto out;
34207 +       parent = dget_parent(dentry);
34208 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34209 +       if (unlikely(err))
34210 +               goto out_free;
34211 +
34212 +       err = bindex;
34213 +       for (i = 0; i < dpages.ndpage; i++) {
34214 +               dpage = dpages.dpages + i;
34215 +               dentries = dpage->dentries;
34216 +               ndentry = dpage->ndentry;
34217 +               for (j = 0; j < ndentry; j++) {
34218 +                       d = dentries[j];
34219 +                       di_read_lock_parent2(d, !AuLock_IR);
34220 +                       bopq = au_dbdiropq(d);
34221 +                       di_read_unlock(d, !AuLock_IR);
34222 +                       if (bopq >= 0 && bopq < err)
34223 +                               err = bopq;
34224 +               }
34225 +       }
34226 +
34227 +out_free:
34228 +       dput(parent);
34229 +       au_dpages_free(&dpages);
34230 +out:
34231 +       return err;
34232 +}
34233 +
34234 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34235 +{
34236 +       for (; bindex >= 0; bindex--)
34237 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34238 +                       return bindex;
34239 +       return -EROFS;
34240 +}
34241 +
34242 +/* top down parent */
34243 +static int au_wbr_create_tdp(struct dentry *dentry,
34244 +                            unsigned int flags __maybe_unused)
34245 +{
34246 +       int err;
34247 +       aufs_bindex_t btop, bindex;
34248 +       struct super_block *sb;
34249 +       struct dentry *parent, *h_parent;
34250 +
34251 +       sb = dentry->d_sb;
34252 +       btop = au_dbtop(dentry);
34253 +       err = btop;
34254 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34255 +               goto out;
34256 +
34257 +       err = -EROFS;
34258 +       parent = dget_parent(dentry);
34259 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34260 +               h_parent = au_h_dptr(parent, bindex);
34261 +               if (!h_parent || d_is_negative(h_parent))
34262 +                       continue;
34263 +
34264 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34265 +                       err = bindex;
34266 +                       break;
34267 +               }
34268 +       }
34269 +       dput(parent);
34270 +
34271 +       /* bottom up here */
34272 +       if (unlikely(err < 0)) {
34273 +               err = au_wbr_bu(sb, btop - 1);
34274 +               if (err >= 0)
34275 +                       err = au_wbr_nonopq(dentry, err);
34276 +       }
34277 +
34278 +out:
34279 +       AuDbg("b%d\n", err);
34280 +       return err;
34281 +}
34282 +
34283 +/* ---------------------------------------------------------------------- */
34284 +
34285 +/* an exception for the policy other than tdp */
34286 +static int au_wbr_create_exp(struct dentry *dentry)
34287 +{
34288 +       int err;
34289 +       aufs_bindex_t bwh, bdiropq;
34290 +       struct dentry *parent;
34291 +
34292 +       err = -1;
34293 +       bwh = au_dbwh(dentry);
34294 +       parent = dget_parent(dentry);
34295 +       bdiropq = au_dbdiropq(parent);
34296 +       if (bwh >= 0) {
34297 +               if (bdiropq >= 0)
34298 +                       err = min(bdiropq, bwh);
34299 +               else
34300 +                       err = bwh;
34301 +               AuDbg("%d\n", err);
34302 +       } else if (bdiropq >= 0) {
34303 +               err = bdiropq;
34304 +               AuDbg("%d\n", err);
34305 +       }
34306 +       dput(parent);
34307 +
34308 +       if (err >= 0)
34309 +               err = au_wbr_nonopq(dentry, err);
34310 +
34311 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34312 +               err = -1;
34313 +
34314 +       AuDbg("%d\n", err);
34315 +       return err;
34316 +}
34317 +
34318 +/* ---------------------------------------------------------------------- */
34319 +
34320 +/* round robin */
34321 +static int au_wbr_create_init_rr(struct super_block *sb)
34322 +{
34323 +       int err;
34324 +
34325 +       err = au_wbr_bu(sb, au_sbbot(sb));
34326 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34327 +       /* smp_mb(); */
34328 +
34329 +       AuDbg("b%d\n", err);
34330 +       return err;
34331 +}
34332 +
34333 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34334 +{
34335 +       int err, nbr;
34336 +       unsigned int u;
34337 +       aufs_bindex_t bindex, bbot;
34338 +       struct super_block *sb;
34339 +       atomic_t *next;
34340 +
34341 +       err = au_wbr_create_exp(dentry);
34342 +       if (err >= 0)
34343 +               goto out;
34344 +
34345 +       sb = dentry->d_sb;
34346 +       next = &au_sbi(sb)->si_wbr_rr_next;
34347 +       bbot = au_sbbot(sb);
34348 +       nbr = bbot + 1;
34349 +       for (bindex = 0; bindex <= bbot; bindex++) {
34350 +               if (!au_ftest_wbr(flags, DIR)) {
34351 +                       err = atomic_dec_return(next) + 1;
34352 +                       /* modulo for 0 is meaningless */
34353 +                       if (unlikely(!err))
34354 +                               err = atomic_dec_return(next) + 1;
34355 +               } else
34356 +                       err = atomic_read(next);
34357 +               AuDbg("%d\n", err);
34358 +               u = err;
34359 +               err = u % nbr;
34360 +               AuDbg("%d\n", err);
34361 +               if (!au_br_rdonly(au_sbr(sb, err)))
34362 +                       break;
34363 +               err = -EROFS;
34364 +       }
34365 +
34366 +       if (err >= 0)
34367 +               err = au_wbr_nonopq(dentry, err);
34368 +
34369 +out:
34370 +       AuDbg("%d\n", err);
34371 +       return err;
34372 +}
34373 +
34374 +/* ---------------------------------------------------------------------- */
34375 +
34376 +/* most free space */
34377 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34378 +{
34379 +       struct super_block *sb;
34380 +       struct au_branch *br;
34381 +       struct au_wbr_mfs *mfs;
34382 +       struct dentry *h_parent;
34383 +       aufs_bindex_t bindex, bbot;
34384 +       int err;
34385 +       unsigned long long b, bavail;
34386 +       struct path h_path;
34387 +       /* reduce the stack usage */
34388 +       struct kstatfs *st;
34389 +
34390 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34391 +       if (unlikely(!st)) {
34392 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34393 +               return;
34394 +       }
34395 +
34396 +       bavail = 0;
34397 +       sb = dentry->d_sb;
34398 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34399 +       MtxMustLock(&mfs->mfs_lock);
34400 +       mfs->mfs_bindex = -EROFS;
34401 +       mfs->mfsrr_bytes = 0;
34402 +       if (!parent) {
34403 +               bindex = 0;
34404 +               bbot = au_sbbot(sb);
34405 +       } else {
34406 +               bindex = au_dbtop(parent);
34407 +               bbot = au_dbtaildir(parent);
34408 +       }
34409 +
34410 +       for (; bindex <= bbot; bindex++) {
34411 +               if (parent) {
34412 +                       h_parent = au_h_dptr(parent, bindex);
34413 +                       if (!h_parent || d_is_negative(h_parent))
34414 +                               continue;
34415 +               }
34416 +               br = au_sbr(sb, bindex);
34417 +               if (au_br_rdonly(br))
34418 +                       continue;
34419 +
34420 +               /* sb->s_root for NFS is unreliable */
34421 +               h_path.mnt = au_br_mnt(br);
34422 +               h_path.dentry = h_path.mnt->mnt_root;
34423 +               err = vfs_statfs(&h_path, st);
34424 +               if (unlikely(err)) {
34425 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34426 +                       continue;
34427 +               }
34428 +
34429 +               /* when the available size is equal, select the lower one */
34430 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34431 +                            || sizeof(b) < sizeof(st->f_bsize));
34432 +               b = st->f_bavail * st->f_bsize;
34433 +               br->br_wbr->wbr_bytes = b;
34434 +               if (b >= bavail) {
34435 +                       bavail = b;
34436 +                       mfs->mfs_bindex = bindex;
34437 +                       mfs->mfs_jiffy = jiffies;
34438 +               }
34439 +       }
34440 +
34441 +       mfs->mfsrr_bytes = bavail;
34442 +       AuDbg("b%d\n", mfs->mfs_bindex);
34443 +       au_kfree_rcu(st);
34444 +}
34445 +
34446 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34447 +{
34448 +       int err;
34449 +       struct dentry *parent;
34450 +       struct super_block *sb;
34451 +       struct au_wbr_mfs *mfs;
34452 +
34453 +       err = au_wbr_create_exp(dentry);
34454 +       if (err >= 0)
34455 +               goto out;
34456 +
34457 +       sb = dentry->d_sb;
34458 +       parent = NULL;
34459 +       if (au_ftest_wbr(flags, PARENT))
34460 +               parent = dget_parent(dentry);
34461 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34462 +       mutex_lock(&mfs->mfs_lock);
34463 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34464 +           || mfs->mfs_bindex < 0
34465 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34466 +               au_mfs(dentry, parent);
34467 +       mutex_unlock(&mfs->mfs_lock);
34468 +       err = mfs->mfs_bindex;
34469 +       dput(parent);
34470 +
34471 +       if (err >= 0)
34472 +               err = au_wbr_nonopq(dentry, err);
34473 +
34474 +out:
34475 +       AuDbg("b%d\n", err);
34476 +       return err;
34477 +}
34478 +
34479 +static int au_wbr_create_init_mfs(struct super_block *sb)
34480 +{
34481 +       struct au_wbr_mfs *mfs;
34482 +
34483 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34484 +       mutex_init(&mfs->mfs_lock);
34485 +       mfs->mfs_jiffy = 0;
34486 +       mfs->mfs_bindex = -EROFS;
34487 +
34488 +       return 0;
34489 +}
34490 +
34491 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34492 +{
34493 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34494 +       return 0;
34495 +}
34496 +
34497 +/* ---------------------------------------------------------------------- */
34498 +
34499 +/* top down regardless parent, and then mfs */
34500 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34501 +                              unsigned int flags __maybe_unused)
34502 +{
34503 +       int err;
34504 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34505 +       unsigned long long watermark;
34506 +       struct super_block *sb;
34507 +       struct au_wbr_mfs *mfs;
34508 +       struct au_branch *br;
34509 +       struct dentry *parent;
34510 +
34511 +       sb = dentry->d_sb;
34512 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34513 +       mutex_lock(&mfs->mfs_lock);
34514 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34515 +           || mfs->mfs_bindex < 0)
34516 +               au_mfs(dentry, /*parent*/NULL);
34517 +       watermark = mfs->mfsrr_watermark;
34518 +       bmfs = mfs->mfs_bindex;
34519 +       mutex_unlock(&mfs->mfs_lock);
34520 +
34521 +       /* another style of au_wbr_create_exp() */
34522 +       bwh = au_dbwh(dentry);
34523 +       parent = dget_parent(dentry);
34524 +       btail = au_dbtaildir(parent);
34525 +       if (bwh >= 0 && bwh < btail)
34526 +               btail = bwh;
34527 +
34528 +       err = au_wbr_nonopq(dentry, btail);
34529 +       if (unlikely(err < 0))
34530 +               goto out;
34531 +       btail = err;
34532 +       bfound = -1;
34533 +       for (bindex = 0; bindex <= btail; bindex++) {
34534 +               br = au_sbr(sb, bindex);
34535 +               if (au_br_rdonly(br))
34536 +                       continue;
34537 +               if (br->br_wbr->wbr_bytes > watermark) {
34538 +                       bfound = bindex;
34539 +                       break;
34540 +               }
34541 +       }
34542 +       err = bfound;
34543 +       if (err < 0)
34544 +               err = bmfs;
34545 +
34546 +out:
34547 +       dput(parent);
34548 +       AuDbg("b%d\n", err);
34549 +       return err;
34550 +}
34551 +
34552 +/* ---------------------------------------------------------------------- */
34553 +
34554 +/* most free space and then round robin */
34555 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34556 +{
34557 +       int err;
34558 +       struct au_wbr_mfs *mfs;
34559 +
34560 +       err = au_wbr_create_mfs(dentry, flags);
34561 +       if (err >= 0) {
34562 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34563 +               mutex_lock(&mfs->mfs_lock);
34564 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34565 +                       err = au_wbr_create_rr(dentry, flags);
34566 +               mutex_unlock(&mfs->mfs_lock);
34567 +       }
34568 +
34569 +       AuDbg("b%d\n", err);
34570 +       return err;
34571 +}
34572 +
34573 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34574 +{
34575 +       int err;
34576 +
34577 +       au_wbr_create_init_mfs(sb); /* ignore */
34578 +       err = au_wbr_create_init_rr(sb);
34579 +
34580 +       return err;
34581 +}
34582 +
34583 +/* ---------------------------------------------------------------------- */
34584 +
34585 +/* top down parent and most free space */
34586 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34587 +{
34588 +       int err, e2;
34589 +       unsigned long long b;
34590 +       aufs_bindex_t bindex, btop, bbot;
34591 +       struct super_block *sb;
34592 +       struct dentry *parent, *h_parent;
34593 +       struct au_branch *br;
34594 +
34595 +       err = au_wbr_create_tdp(dentry, flags);
34596 +       if (unlikely(err < 0))
34597 +               goto out;
34598 +       parent = dget_parent(dentry);
34599 +       btop = au_dbtop(parent);
34600 +       bbot = au_dbtaildir(parent);
34601 +       if (btop == bbot)
34602 +               goto out_parent; /* success */
34603 +
34604 +       e2 = au_wbr_create_mfs(dentry, flags);
34605 +       if (e2 < 0)
34606 +               goto out_parent; /* success */
34607 +
34608 +       /* when the available size is equal, select upper one */
34609 +       sb = dentry->d_sb;
34610 +       br = au_sbr(sb, err);
34611 +       b = br->br_wbr->wbr_bytes;
34612 +       AuDbg("b%d, %llu\n", err, b);
34613 +
34614 +       for (bindex = btop; bindex <= bbot; bindex++) {
34615 +               h_parent = au_h_dptr(parent, bindex);
34616 +               if (!h_parent || d_is_negative(h_parent))
34617 +                       continue;
34618 +
34619 +               br = au_sbr(sb, bindex);
34620 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34621 +                       b = br->br_wbr->wbr_bytes;
34622 +                       err = bindex;
34623 +                       AuDbg("b%d, %llu\n", err, b);
34624 +               }
34625 +       }
34626 +
34627 +       if (err >= 0)
34628 +               err = au_wbr_nonopq(dentry, err);
34629 +
34630 +out_parent:
34631 +       dput(parent);
34632 +out:
34633 +       AuDbg("b%d\n", err);
34634 +       return err;
34635 +}
34636 +
34637 +/* ---------------------------------------------------------------------- */
34638 +
34639 +/*
34640 + * - top down parent
34641 + * - most free space with parent
34642 + * - most free space round-robin regardless parent
34643 + */
34644 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34645 +{
34646 +       int err;
34647 +       unsigned long long watermark;
34648 +       struct super_block *sb;
34649 +       struct au_branch *br;
34650 +       struct au_wbr_mfs *mfs;
34651 +
34652 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34653 +       if (unlikely(err < 0))
34654 +               goto out;
34655 +
34656 +       sb = dentry->d_sb;
34657 +       br = au_sbr(sb, err);
34658 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34659 +       mutex_lock(&mfs->mfs_lock);
34660 +       watermark = mfs->mfsrr_watermark;
34661 +       mutex_unlock(&mfs->mfs_lock);
34662 +       if (br->br_wbr->wbr_bytes < watermark)
34663 +               /* regardless the parent dir */
34664 +               err = au_wbr_create_mfsrr(dentry, flags);
34665 +
34666 +out:
34667 +       AuDbg("b%d\n", err);
34668 +       return err;
34669 +}
34670 +
34671 +/* ---------------------------------------------------------------------- */
34672 +
34673 +/* policies for copyup */
34674 +
34675 +/* top down parent */
34676 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34677 +{
34678 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34679 +}
34680 +
34681 +/* bottom up parent */
34682 +static int au_wbr_copyup_bup(struct dentry *dentry)
34683 +{
34684 +       int err;
34685 +       aufs_bindex_t bindex, btop;
34686 +       struct dentry *parent, *h_parent;
34687 +       struct super_block *sb;
34688 +
34689 +       err = -EROFS;
34690 +       sb = dentry->d_sb;
34691 +       parent = dget_parent(dentry);
34692 +       btop = au_dbtop(parent);
34693 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34694 +               h_parent = au_h_dptr(parent, bindex);
34695 +               if (!h_parent || d_is_negative(h_parent))
34696 +                       continue;
34697 +
34698 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34699 +                       err = bindex;
34700 +                       break;
34701 +               }
34702 +       }
34703 +       dput(parent);
34704 +
34705 +       /* bottom up here */
34706 +       if (unlikely(err < 0))
34707 +               err = au_wbr_bu(sb, btop - 1);
34708 +
34709 +       AuDbg("b%d\n", err);
34710 +       return err;
34711 +}
34712 +
34713 +/* bottom up */
34714 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34715 +{
34716 +       int err;
34717 +
34718 +       err = au_wbr_bu(dentry->d_sb, btop);
34719 +       AuDbg("b%d\n", err);
34720 +       if (err > btop)
34721 +               err = au_wbr_nonopq(dentry, err);
34722 +
34723 +       AuDbg("b%d\n", err);
34724 +       return err;
34725 +}
34726 +
34727 +static int au_wbr_copyup_bu(struct dentry *dentry)
34728 +{
34729 +       int err;
34730 +       aufs_bindex_t btop;
34731 +
34732 +       btop = au_dbtop(dentry);
34733 +       err = au_wbr_do_copyup_bu(dentry, btop);
34734 +       return err;
34735 +}
34736 +
34737 +/* ---------------------------------------------------------------------- */
34738 +
34739 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34740 +       [AuWbrCopyup_TDP] = {
34741 +               .copyup = au_wbr_copyup_tdp
34742 +       },
34743 +       [AuWbrCopyup_BUP] = {
34744 +               .copyup = au_wbr_copyup_bup
34745 +       },
34746 +       [AuWbrCopyup_BU] = {
34747 +               .copyup = au_wbr_copyup_bu
34748 +       }
34749 +};
34750 +
34751 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34752 +       [AuWbrCreate_TDP] = {
34753 +               .create = au_wbr_create_tdp
34754 +       },
34755 +       [AuWbrCreate_RR] = {
34756 +               .create = au_wbr_create_rr,
34757 +               .init   = au_wbr_create_init_rr
34758 +       },
34759 +       [AuWbrCreate_MFS] = {
34760 +               .create = au_wbr_create_mfs,
34761 +               .init   = au_wbr_create_init_mfs,
34762 +               .fin    = au_wbr_create_fin_mfs
34763 +       },
34764 +       [AuWbrCreate_MFSV] = {
34765 +               .create = au_wbr_create_mfs,
34766 +               .init   = au_wbr_create_init_mfs,
34767 +               .fin    = au_wbr_create_fin_mfs
34768 +       },
34769 +       [AuWbrCreate_MFSRR] = {
34770 +               .create = au_wbr_create_mfsrr,
34771 +               .init   = au_wbr_create_init_mfsrr,
34772 +               .fin    = au_wbr_create_fin_mfs
34773 +       },
34774 +       [AuWbrCreate_MFSRRV] = {
34775 +               .create = au_wbr_create_mfsrr,
34776 +               .init   = au_wbr_create_init_mfsrr,
34777 +               .fin    = au_wbr_create_fin_mfs
34778 +       },
34779 +       [AuWbrCreate_TDMFS] = {
34780 +               .create = au_wbr_create_tdmfs,
34781 +               .init   = au_wbr_create_init_mfs,
34782 +               .fin    = au_wbr_create_fin_mfs
34783 +       },
34784 +       [AuWbrCreate_TDMFSV] = {
34785 +               .create = au_wbr_create_tdmfs,
34786 +               .init   = au_wbr_create_init_mfs,
34787 +               .fin    = au_wbr_create_fin_mfs
34788 +       },
34789 +       [AuWbrCreate_PMFS] = {
34790 +               .create = au_wbr_create_pmfs,
34791 +               .init   = au_wbr_create_init_mfs,
34792 +               .fin    = au_wbr_create_fin_mfs
34793 +       },
34794 +       [AuWbrCreate_PMFSV] = {
34795 +               .create = au_wbr_create_pmfs,
34796 +               .init   = au_wbr_create_init_mfs,
34797 +               .fin    = au_wbr_create_fin_mfs
34798 +       },
34799 +       [AuWbrCreate_PMFSRR] = {
34800 +               .create = au_wbr_create_pmfsrr,
34801 +               .init   = au_wbr_create_init_mfsrr,
34802 +               .fin    = au_wbr_create_fin_mfs
34803 +       },
34804 +       [AuWbrCreate_PMFSRRV] = {
34805 +               .create = au_wbr_create_pmfsrr,
34806 +               .init   = au_wbr_create_init_mfsrr,
34807 +               .fin    = au_wbr_create_fin_mfs
34808 +       }
34809 +};
34810 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34811 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34812 +++ linux/fs/aufs/whout.c       2020-01-27 10:57:18.178871751 +0100
34813 @@ -0,0 +1,1062 @@
34814 +// SPDX-License-Identifier: GPL-2.0
34815 +/*
34816 + * Copyright (C) 2005-2020 Junjiro R. Okajima
34817 + *
34818 + * This program, aufs is free software; you can redistribute it and/or modify
34819 + * it under the terms of the GNU General Public License as published by
34820 + * the Free Software Foundation; either version 2 of the License, or
34821 + * (at your option) any later version.
34822 + *
34823 + * This program is distributed in the hope that it will be useful,
34824 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34825 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34826 + * GNU General Public License for more details.
34827 + *
34828 + * You should have received a copy of the GNU General Public License
34829 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34830 + */
34831 +
34832 +/*
34833 + * whiteout for logical deletion and opaque directory
34834 + */
34835 +
34836 +#include "aufs.h"
34837 +
34838 +#define WH_MASK                        0444
34839 +
34840 +/*
34841 + * If a directory contains this file, then it is opaque.  We start with the
34842 + * .wh. flag so that it is blocked by lookup.
34843 + */
34844 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34845 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34846 +
34847 +/*
34848 + * generate whiteout name, which is NOT terminated by NULL.
34849 + * @name: original d_name.name
34850 + * @len: original d_name.len
34851 + * @wh: whiteout qstr
34852 + * returns zero when succeeds, otherwise error.
34853 + * succeeded value as wh->name should be freed by kfree().
34854 + */
34855 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34856 +{
34857 +       char *p;
34858 +
34859 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34860 +               return -ENAMETOOLONG;
34861 +
34862 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34863 +       p = kmalloc(wh->len, GFP_NOFS);
34864 +       wh->name = p;
34865 +       if (p) {
34866 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34867 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34868 +               /* smp_mb(); */
34869 +               return 0;
34870 +       }
34871 +       return -ENOMEM;
34872 +}
34873 +
34874 +/* ---------------------------------------------------------------------- */
34875 +
34876 +/*
34877 + * test if the @wh_name exists under @h_parent.
34878 + * @try_sio specifies the necessary of super-io.
34879 + */
34880 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34881 +{
34882 +       int err;
34883 +       struct dentry *wh_dentry;
34884 +
34885 +       if (!try_sio)
34886 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34887 +       else
34888 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34889 +       err = PTR_ERR(wh_dentry);
34890 +       if (IS_ERR(wh_dentry)) {
34891 +               if (err == -ENAMETOOLONG)
34892 +                       err = 0;
34893 +               goto out;
34894 +       }
34895 +
34896 +       err = 0;
34897 +       if (d_is_negative(wh_dentry))
34898 +               goto out_wh; /* success */
34899 +
34900 +       err = 1;
34901 +       if (d_is_reg(wh_dentry))
34902 +               goto out_wh; /* success */
34903 +
34904 +       err = -EIO;
34905 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34906 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34907 +
34908 +out_wh:
34909 +       dput(wh_dentry);
34910 +out:
34911 +       return err;
34912 +}
34913 +
34914 +/*
34915 + * test if the @h_dentry sets opaque or not.
34916 + */
34917 +int au_diropq_test(struct dentry *h_dentry)
34918 +{
34919 +       int err;
34920 +       struct inode *h_dir;
34921 +
34922 +       h_dir = d_inode(h_dentry);
34923 +       err = au_wh_test(h_dentry, &diropq_name,
34924 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34925 +       return err;
34926 +}
34927 +
34928 +/*
34929 + * returns a negative dentry whose name is unique and temporary.
34930 + */
34931 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34932 +                            struct qstr *prefix)
34933 +{
34934 +       struct dentry *dentry;
34935 +       int i;
34936 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34937 +               *name, *p;
34938 +       /* strict atomic_t is unnecessary here */
34939 +       static unsigned short cnt;
34940 +       struct qstr qs;
34941 +
34942 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34943 +
34944 +       name = defname;
34945 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34946 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34947 +               dentry = ERR_PTR(-ENAMETOOLONG);
34948 +               if (unlikely(qs.len > NAME_MAX))
34949 +                       goto out;
34950 +               dentry = ERR_PTR(-ENOMEM);
34951 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34952 +               if (unlikely(!name))
34953 +                       goto out;
34954 +       }
34955 +
34956 +       /* doubly whiteout-ed */
34957 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34958 +       p = name + AUFS_WH_PFX_LEN * 2;
34959 +       memcpy(p, prefix->name, prefix->len);
34960 +       p += prefix->len;
34961 +       *p++ = '.';
34962 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34963 +
34964 +       qs.name = name;
34965 +       for (i = 0; i < 3; i++) {
34966 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34967 +               dentry = au_sio_lkup_one(&qs, h_parent);
34968 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34969 +                       goto out_name;
34970 +               dput(dentry);
34971 +       }
34972 +       /* pr_warn("could not get random name\n"); */
34973 +       dentry = ERR_PTR(-EEXIST);
34974 +       AuDbg("%.*s\n", AuLNPair(&qs));
34975 +       BUG();
34976 +
34977 +out_name:
34978 +       if (name != defname)
34979 +               au_kfree_try_rcu(name);
34980 +out:
34981 +       AuTraceErrPtr(dentry);
34982 +       return dentry;
34983 +}
34984 +
34985 +/*
34986 + * rename the @h_dentry on @br to the whiteouted temporary name.
34987 + */
34988 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34989 +{
34990 +       int err;
34991 +       struct path h_path = {
34992 +               .mnt = au_br_mnt(br)
34993 +       };
34994 +       struct inode *h_dir, *delegated;
34995 +       struct dentry *h_parent;
34996 +
34997 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34998 +       h_dir = d_inode(h_parent);
34999 +       IMustLock(h_dir);
35000 +
35001 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35002 +       err = PTR_ERR(h_path.dentry);
35003 +       if (IS_ERR(h_path.dentry))
35004 +               goto out;
35005 +
35006 +       /* under the same dir, no need to lock_rename() */
35007 +       delegated = NULL;
35008 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35009 +                          /*flags*/0);
35010 +       AuTraceErr(err);
35011 +       if (unlikely(err == -EWOULDBLOCK)) {
35012 +               pr_warn("cannot retry for NFSv4 delegation"
35013 +                       " for an internal rename\n");
35014 +               iput(delegated);
35015 +       }
35016 +       dput(h_path.dentry);
35017 +
35018 +out:
35019 +       AuTraceErr(err);
35020 +       return err;
35021 +}
35022 +
35023 +/* ---------------------------------------------------------------------- */
35024 +/*
35025 + * functions for removing a whiteout
35026 + */
35027 +
35028 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35029 +{
35030 +       int err, force;
35031 +       struct inode *delegated;
35032 +
35033 +       /*
35034 +        * forces superio when the dir has a sticky bit.
35035 +        * this may be a violation of unix fs semantics.
35036 +        */
35037 +       force = (h_dir->i_mode & S_ISVTX)
35038 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35039 +       delegated = NULL;
35040 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35041 +       if (unlikely(err == -EWOULDBLOCK)) {
35042 +               pr_warn("cannot retry for NFSv4 delegation"
35043 +                       " for an internal unlink\n");
35044 +               iput(delegated);
35045 +       }
35046 +       return err;
35047 +}
35048 +
35049 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35050 +                       struct dentry *dentry)
35051 +{
35052 +       int err;
35053 +
35054 +       err = do_unlink_wh(h_dir, h_path);
35055 +       if (!err && dentry)
35056 +               au_set_dbwh(dentry, -1);
35057 +
35058 +       return err;
35059 +}
35060 +
35061 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
35062 +                         struct au_branch *br)
35063 +{
35064 +       int err;
35065 +       struct path h_path = {
35066 +               .mnt = au_br_mnt(br)
35067 +       };
35068 +
35069 +       err = 0;
35070 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
35071 +       if (IS_ERR(h_path.dentry))
35072 +               err = PTR_ERR(h_path.dentry);
35073 +       else {
35074 +               if (d_is_reg(h_path.dentry))
35075 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
35076 +               dput(h_path.dentry);
35077 +       }
35078 +
35079 +       return err;
35080 +}
35081 +
35082 +/* ---------------------------------------------------------------------- */
35083 +/*
35084 + * initialize/clean whiteout for a branch
35085 + */
35086 +
35087 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35088 +                       const int isdir)
35089 +{
35090 +       int err;
35091 +       struct inode *delegated;
35092 +
35093 +       if (d_is_negative(whpath->dentry))
35094 +               return;
35095 +
35096 +       if (isdir)
35097 +               err = vfsub_rmdir(h_dir, whpath);
35098 +       else {
35099 +               delegated = NULL;
35100 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35101 +               if (unlikely(err == -EWOULDBLOCK)) {
35102 +                       pr_warn("cannot retry for NFSv4 delegation"
35103 +                               " for an internal unlink\n");
35104 +                       iput(delegated);
35105 +               }
35106 +       }
35107 +       if (unlikely(err))
35108 +               pr_warn("failed removing %pd (%d), ignored.\n",
35109 +                       whpath->dentry, err);
35110 +}
35111 +
35112 +static int test_linkable(struct dentry *h_root)
35113 +{
35114 +       struct inode *h_dir = d_inode(h_root);
35115 +
35116 +       if (h_dir->i_op->link)
35117 +               return 0;
35118 +
35119 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35120 +              h_root, au_sbtype(h_root->d_sb));
35121 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35122 +}
35123 +
35124 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35125 +static int au_whdir(struct inode *h_dir, struct path *path)
35126 +{
35127 +       int err;
35128 +
35129 +       err = -EEXIST;
35130 +       if (d_is_negative(path->dentry)) {
35131 +               int mode = 0700;
35132 +
35133 +               if (au_test_nfs(path->dentry->d_sb))
35134 +                       mode |= 0111;
35135 +               err = vfsub_mkdir(h_dir, path, mode);
35136 +       } else if (d_is_dir(path->dentry))
35137 +               err = 0;
35138 +       else
35139 +               pr_err("unknown %pd exists\n", path->dentry);
35140 +
35141 +       return err;
35142 +}
35143 +
35144 +struct au_wh_base {
35145 +       const struct qstr *name;
35146 +       struct dentry *dentry;
35147 +};
35148 +
35149 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35150 +                         struct path *h_path)
35151 +{
35152 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35153 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35154 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35155 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35156 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35157 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35158 +}
35159 +
35160 +/*
35161 + * returns tri-state,
35162 + * minus: error, caller should print the message
35163 + * zero: success
35164 + * plus: error, caller should NOT print the message
35165 + */
35166 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35167 +                               int do_plink, struct au_wh_base base[],
35168 +                               struct path *h_path)
35169 +{
35170 +       int err;
35171 +       struct inode *h_dir;
35172 +
35173 +       h_dir = d_inode(h_root);
35174 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35175 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35176 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35177 +       if (do_plink) {
35178 +               err = test_linkable(h_root);
35179 +               if (unlikely(err)) {
35180 +                       err = 1;
35181 +                       goto out;
35182 +               }
35183 +
35184 +               err = au_whdir(h_dir, h_path);
35185 +               if (unlikely(err))
35186 +                       goto out;
35187 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35188 +       } else
35189 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35190 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35191 +       err = au_whdir(h_dir, h_path);
35192 +       if (unlikely(err))
35193 +               goto out;
35194 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35195 +
35196 +out:
35197 +       return err;
35198 +}
35199 +
35200 +/*
35201 + * for the moment, aufs supports the branch filesystem which does not support
35202 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35203 + * copyup failed. finally, such filesystem will not be used as the writable
35204 + * branch.
35205 + *
35206 + * returns tri-state, see above.
35207 + */
35208 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35209 +                        int do_plink, struct au_wh_base base[],
35210 +                        struct path *h_path)
35211 +{
35212 +       int err;
35213 +       struct inode *h_dir;
35214 +
35215 +       WbrWhMustWriteLock(wbr);
35216 +
35217 +       err = test_linkable(h_root);
35218 +       if (unlikely(err)) {
35219 +               err = 1;
35220 +               goto out;
35221 +       }
35222 +
35223 +       /*
35224 +        * todo: should this create be done in /sbin/mount.aufs helper?
35225 +        */
35226 +       err = -EEXIST;
35227 +       h_dir = d_inode(h_root);
35228 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35229 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35230 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35231 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35232 +               err = 0;
35233 +       else
35234 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35235 +       if (unlikely(err))
35236 +               goto out;
35237 +
35238 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35239 +       if (do_plink) {
35240 +               err = au_whdir(h_dir, h_path);
35241 +               if (unlikely(err))
35242 +                       goto out;
35243 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35244 +       } else
35245 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35246 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35247 +
35248 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35249 +       err = au_whdir(h_dir, h_path);
35250 +       if (unlikely(err))
35251 +               goto out;
35252 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35253 +
35254 +out:
35255 +       return err;
35256 +}
35257 +
35258 +/*
35259 + * initialize the whiteout base file/dir for @br.
35260 + */
35261 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35262 +{
35263 +       int err, i;
35264 +       const unsigned char do_plink
35265 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35266 +       struct inode *h_dir;
35267 +       struct path path = br->br_path;
35268 +       struct dentry *h_root = path.dentry;
35269 +       struct au_wbr *wbr = br->br_wbr;
35270 +       static const struct qstr base_name[] = {
35271 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35272 +                                         sizeof(AUFS_BASE_NAME) - 1),
35273 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35274 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35275 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35276 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35277 +       };
35278 +       struct au_wh_base base[] = {
35279 +               [AuBrWh_BASE] = {
35280 +                       .name   = base_name + AuBrWh_BASE,
35281 +                       .dentry = NULL
35282 +               },
35283 +               [AuBrWh_PLINK] = {
35284 +                       .name   = base_name + AuBrWh_PLINK,
35285 +                       .dentry = NULL
35286 +               },
35287 +               [AuBrWh_ORPH] = {
35288 +                       .name   = base_name + AuBrWh_ORPH,
35289 +                       .dentry = NULL
35290 +               }
35291 +       };
35292 +
35293 +       if (wbr)
35294 +               WbrWhMustWriteLock(wbr);
35295 +
35296 +       for (i = 0; i < AuBrWh_Last; i++) {
35297 +               /* doubly whiteouted */
35298 +               struct dentry *d;
35299 +
35300 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35301 +               err = PTR_ERR(d);
35302 +               if (IS_ERR(d))
35303 +                       goto out;
35304 +
35305 +               base[i].dentry = d;
35306 +               AuDebugOn(wbr
35307 +                         && wbr->wbr_wh[i]
35308 +                         && wbr->wbr_wh[i] != base[i].dentry);
35309 +       }
35310 +
35311 +       if (wbr)
35312 +               for (i = 0; i < AuBrWh_Last; i++) {
35313 +                       dput(wbr->wbr_wh[i]);
35314 +                       wbr->wbr_wh[i] = NULL;
35315 +               }
35316 +
35317 +       err = 0;
35318 +       if (!au_br_writable(br->br_perm)) {
35319 +               h_dir = d_inode(h_root);
35320 +               au_wh_init_ro(h_dir, base, &path);
35321 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35322 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35323 +               if (err > 0)
35324 +                       goto out;
35325 +               else if (err)
35326 +                       goto out_err;
35327 +       } else {
35328 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35329 +               if (err > 0)
35330 +                       goto out;
35331 +               else if (err)
35332 +                       goto out_err;
35333 +       }
35334 +       goto out; /* success */
35335 +
35336 +out_err:
35337 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35338 +              err, h_root, au_sbtype(h_root->d_sb));
35339 +out:
35340 +       for (i = 0; i < AuBrWh_Last; i++)
35341 +               dput(base[i].dentry);
35342 +       return err;
35343 +}
35344 +
35345 +/* ---------------------------------------------------------------------- */
35346 +/*
35347 + * whiteouts are all hard-linked usually.
35348 + * when its link count reaches a ceiling, we create a new whiteout base
35349 + * asynchronously.
35350 + */
35351 +
35352 +struct reinit_br_wh {
35353 +       struct super_block *sb;
35354 +       struct au_branch *br;
35355 +};
35356 +
35357 +static void reinit_br_wh(void *arg)
35358 +{
35359 +       int err;
35360 +       aufs_bindex_t bindex;
35361 +       struct path h_path;
35362 +       struct reinit_br_wh *a = arg;
35363 +       struct au_wbr *wbr;
35364 +       struct inode *dir, *delegated;
35365 +       struct dentry *h_root;
35366 +       struct au_hinode *hdir;
35367 +
35368 +       err = 0;
35369 +       wbr = a->br->br_wbr;
35370 +       /* big aufs lock */
35371 +       si_noflush_write_lock(a->sb);
35372 +       if (!au_br_writable(a->br->br_perm))
35373 +               goto out;
35374 +       bindex = au_br_index(a->sb, a->br->br_id);
35375 +       if (unlikely(bindex < 0))
35376 +               goto out;
35377 +
35378 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35379 +       dir = d_inode(a->sb->s_root);
35380 +       hdir = au_hi(dir, bindex);
35381 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35382 +       AuDebugOn(h_root != au_br_dentry(a->br));
35383 +
35384 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35385 +       wbr_wh_write_lock(wbr);
35386 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35387 +                         h_root, a->br);
35388 +       if (!err) {
35389 +               h_path.dentry = wbr->wbr_whbase;
35390 +               h_path.mnt = au_br_mnt(a->br);
35391 +               delegated = NULL;
35392 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35393 +                                  /*force*/0);
35394 +               if (unlikely(err == -EWOULDBLOCK)) {
35395 +                       pr_warn("cannot retry for NFSv4 delegation"
35396 +                               " for an internal unlink\n");
35397 +                       iput(delegated);
35398 +               }
35399 +       } else {
35400 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35401 +               err = 0;
35402 +       }
35403 +       dput(wbr->wbr_whbase);
35404 +       wbr->wbr_whbase = NULL;
35405 +       if (!err)
35406 +               err = au_wh_init(a->br, a->sb);
35407 +       wbr_wh_write_unlock(wbr);
35408 +       au_hn_inode_unlock(hdir);
35409 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35410 +       if (!err)
35411 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35412 +
35413 +out:
35414 +       if (wbr)
35415 +               atomic_dec(&wbr->wbr_wh_running);
35416 +       au_lcnt_dec(&a->br->br_count);
35417 +       si_write_unlock(a->sb);
35418 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35419 +       au_kfree_rcu(a);
35420 +       if (unlikely(err))
35421 +               AuIOErr("err %d\n", err);
35422 +}
35423 +
35424 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35425 +{
35426 +       int do_dec, wkq_err;
35427 +       struct reinit_br_wh *arg;
35428 +
35429 +       do_dec = 1;
35430 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35431 +               goto out;
35432 +
35433 +       /* ignore ENOMEM */
35434 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35435 +       if (arg) {
35436 +               /*
35437 +                * dec(wh_running), kfree(arg) and dec(br_count)
35438 +                * in reinit function
35439 +                */
35440 +               arg->sb = sb;
35441 +               arg->br = br;
35442 +               au_lcnt_inc(&br->br_count);
35443 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35444 +               if (unlikely(wkq_err)) {
35445 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35446 +                       au_lcnt_dec(&br->br_count);
35447 +                       au_kfree_rcu(arg);
35448 +               }
35449 +               do_dec = 0;
35450 +       }
35451 +
35452 +out:
35453 +       if (do_dec)
35454 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35455 +}
35456 +
35457 +/* ---------------------------------------------------------------------- */
35458 +
35459 +/*
35460 + * create the whiteout @wh.
35461 + */
35462 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35463 +                            struct dentry *wh)
35464 +{
35465 +       int err;
35466 +       struct path h_path = {
35467 +               .dentry = wh
35468 +       };
35469 +       struct au_branch *br;
35470 +       struct au_wbr *wbr;
35471 +       struct dentry *h_parent;
35472 +       struct inode *h_dir, *delegated;
35473 +
35474 +       h_parent = wh->d_parent; /* dir inode is locked */
35475 +       h_dir = d_inode(h_parent);
35476 +       IMustLock(h_dir);
35477 +
35478 +       br = au_sbr(sb, bindex);
35479 +       h_path.mnt = au_br_mnt(br);
35480 +       wbr = br->br_wbr;
35481 +       wbr_wh_read_lock(wbr);
35482 +       if (wbr->wbr_whbase) {
35483 +               delegated = NULL;
35484 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35485 +               if (unlikely(err == -EWOULDBLOCK)) {
35486 +                       pr_warn("cannot retry for NFSv4 delegation"
35487 +                               " for an internal link\n");
35488 +                       iput(delegated);
35489 +               }
35490 +               if (!err || err != -EMLINK)
35491 +                       goto out;
35492 +
35493 +               /* link count full. re-initialize br_whbase. */
35494 +               kick_reinit_br_wh(sb, br);
35495 +       }
35496 +
35497 +       /* return this error in this context */
35498 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35499 +       if (!err)
35500 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35501 +
35502 +out:
35503 +       wbr_wh_read_unlock(wbr);
35504 +       return err;
35505 +}
35506 +
35507 +/* ---------------------------------------------------------------------- */
35508 +
35509 +/*
35510 + * create or remove the diropq.
35511 + */
35512 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35513 +                               unsigned int flags)
35514 +{
35515 +       struct dentry *opq_dentry, *h_dentry;
35516 +       struct super_block *sb;
35517 +       struct au_branch *br;
35518 +       int err;
35519 +
35520 +       sb = dentry->d_sb;
35521 +       br = au_sbr(sb, bindex);
35522 +       h_dentry = au_h_dptr(dentry, bindex);
35523 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35524 +       if (IS_ERR(opq_dentry))
35525 +               goto out;
35526 +
35527 +       if (au_ftest_diropq(flags, CREATE)) {
35528 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35529 +               if (!err) {
35530 +                       au_set_dbdiropq(dentry, bindex);
35531 +                       goto out; /* success */
35532 +               }
35533 +       } else {
35534 +               struct path tmp = {
35535 +                       .dentry = opq_dentry,
35536 +                       .mnt    = au_br_mnt(br)
35537 +               };
35538 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35539 +               if (!err)
35540 +                       au_set_dbdiropq(dentry, -1);
35541 +       }
35542 +       dput(opq_dentry);
35543 +       opq_dentry = ERR_PTR(err);
35544 +
35545 +out:
35546 +       return opq_dentry;
35547 +}
35548 +
35549 +struct do_diropq_args {
35550 +       struct dentry **errp;
35551 +       struct dentry *dentry;
35552 +       aufs_bindex_t bindex;
35553 +       unsigned int flags;
35554 +};
35555 +
35556 +static void call_do_diropq(void *args)
35557 +{
35558 +       struct do_diropq_args *a = args;
35559 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35560 +}
35561 +
35562 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35563 +                            unsigned int flags)
35564 +{
35565 +       struct dentry *diropq, *h_dentry;
35566 +
35567 +       h_dentry = au_h_dptr(dentry, bindex);
35568 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35569 +               diropq = do_diropq(dentry, bindex, flags);
35570 +       else {
35571 +               int wkq_err;
35572 +               struct do_diropq_args args = {
35573 +                       .errp           = &diropq,
35574 +                       .dentry         = dentry,
35575 +                       .bindex         = bindex,
35576 +                       .flags          = flags
35577 +               };
35578 +
35579 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35580 +               if (unlikely(wkq_err))
35581 +                       diropq = ERR_PTR(wkq_err);
35582 +       }
35583 +
35584 +       return diropq;
35585 +}
35586 +
35587 +/* ---------------------------------------------------------------------- */
35588 +
35589 +/*
35590 + * lookup whiteout dentry.
35591 + * @h_parent: lower parent dentry which must exist and be locked
35592 + * @base_name: name of dentry which will be whiteouted
35593 + * returns dentry for whiteout.
35594 + */
35595 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35596 +                         struct au_branch *br)
35597 +{
35598 +       int err;
35599 +       struct qstr wh_name;
35600 +       struct dentry *wh_dentry;
35601 +
35602 +       err = au_wh_name_alloc(&wh_name, base_name);
35603 +       wh_dentry = ERR_PTR(err);
35604 +       if (!err) {
35605 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35606 +               au_kfree_try_rcu(wh_name.name);
35607 +       }
35608 +       return wh_dentry;
35609 +}
35610 +
35611 +/*
35612 + * link/create a whiteout for @dentry on @bindex.
35613 + */
35614 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35615 +                           struct dentry *h_parent)
35616 +{
35617 +       struct dentry *wh_dentry;
35618 +       struct super_block *sb;
35619 +       int err;
35620 +
35621 +       sb = dentry->d_sb;
35622 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35623 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35624 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35625 +               if (!err) {
35626 +                       au_set_dbwh(dentry, bindex);
35627 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35628 +               } else {
35629 +                       dput(wh_dentry);
35630 +                       wh_dentry = ERR_PTR(err);
35631 +               }
35632 +       }
35633 +
35634 +       return wh_dentry;
35635 +}
35636 +
35637 +/* ---------------------------------------------------------------------- */
35638 +
35639 +/* Delete all whiteouts in this directory on branch bindex. */
35640 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35641 +                          aufs_bindex_t bindex, struct au_branch *br)
35642 +{
35643 +       int err;
35644 +       unsigned long ul, n;
35645 +       struct qstr wh_name;
35646 +       char *p;
35647 +       struct hlist_head *head;
35648 +       struct au_vdir_wh *pos;
35649 +       struct au_vdir_destr *str;
35650 +
35651 +       err = -ENOMEM;
35652 +       p = (void *)__get_free_page(GFP_NOFS);
35653 +       wh_name.name = p;
35654 +       if (unlikely(!wh_name.name))
35655 +               goto out;
35656 +
35657 +       err = 0;
35658 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35659 +       p += AUFS_WH_PFX_LEN;
35660 +       n = whlist->nh_num;
35661 +       head = whlist->nh_head;
35662 +       for (ul = 0; !err && ul < n; ul++, head++) {
35663 +               hlist_for_each_entry(pos, head, wh_hash) {
35664 +                       if (pos->wh_bindex != bindex)
35665 +                               continue;
35666 +
35667 +                       str = &pos->wh_str;
35668 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35669 +                               memcpy(p, str->name, str->len);
35670 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35671 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35672 +                               if (!err)
35673 +                                       continue;
35674 +                               break;
35675 +                       }
35676 +                       AuIOErr("whiteout name too long %.*s\n",
35677 +                               str->len, str->name);
35678 +                       err = -EIO;
35679 +                       break;
35680 +               }
35681 +       }
35682 +       free_page((unsigned long)wh_name.name);
35683 +
35684 +out:
35685 +       return err;
35686 +}
35687 +
35688 +struct del_wh_children_args {
35689 +       int *errp;
35690 +       struct dentry *h_dentry;
35691 +       struct au_nhash *whlist;
35692 +       aufs_bindex_t bindex;
35693 +       struct au_branch *br;
35694 +};
35695 +
35696 +static void call_del_wh_children(void *args)
35697 +{
35698 +       struct del_wh_children_args *a = args;
35699 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35700 +}
35701 +
35702 +/* ---------------------------------------------------------------------- */
35703 +
35704 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35705 +{
35706 +       struct au_whtmp_rmdir *whtmp;
35707 +       int err;
35708 +       unsigned int rdhash;
35709 +
35710 +       SiMustAnyLock(sb);
35711 +
35712 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35713 +       if (unlikely(!whtmp)) {
35714 +               whtmp = ERR_PTR(-ENOMEM);
35715 +               goto out;
35716 +       }
35717 +
35718 +       /* no estimation for dir size */
35719 +       rdhash = au_sbi(sb)->si_rdhash;
35720 +       if (!rdhash)
35721 +               rdhash = AUFS_RDHASH_DEF;
35722 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35723 +       if (unlikely(err)) {
35724 +               au_kfree_rcu(whtmp);
35725 +               whtmp = ERR_PTR(err);
35726 +       }
35727 +
35728 +out:
35729 +       return whtmp;
35730 +}
35731 +
35732 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35733 +{
35734 +       if (whtmp->br)
35735 +               au_lcnt_dec(&whtmp->br->br_count);
35736 +       dput(whtmp->wh_dentry);
35737 +       iput(whtmp->dir);
35738 +       au_nhash_wh_free(&whtmp->whlist);
35739 +       au_kfree_rcu(whtmp);
35740 +}
35741 +
35742 +/*
35743 + * rmdir the whiteouted temporary named dir @h_dentry.
35744 + * @whlist: whiteouted children.
35745 + */
35746 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35747 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35748 +{
35749 +       int err;
35750 +       unsigned int h_nlink;
35751 +       struct path h_tmp;
35752 +       struct inode *wh_inode, *h_dir;
35753 +       struct au_branch *br;
35754 +
35755 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35756 +       IMustLock(h_dir);
35757 +
35758 +       br = au_sbr(dir->i_sb, bindex);
35759 +       wh_inode = d_inode(wh_dentry);
35760 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35761 +
35762 +       /*
35763 +        * someone else might change some whiteouts while we were sleeping.
35764 +        * it means this whlist may have an obsoleted entry.
35765 +        */
35766 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35767 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35768 +       else {
35769 +               int wkq_err;
35770 +               struct del_wh_children_args args = {
35771 +                       .errp           = &err,
35772 +                       .h_dentry       = wh_dentry,
35773 +                       .whlist         = whlist,
35774 +                       .bindex         = bindex,
35775 +                       .br             = br
35776 +               };
35777 +
35778 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35779 +               if (unlikely(wkq_err))
35780 +                       err = wkq_err;
35781 +       }
35782 +       inode_unlock(wh_inode);
35783 +
35784 +       if (!err) {
35785 +               h_tmp.dentry = wh_dentry;
35786 +               h_tmp.mnt = au_br_mnt(br);
35787 +               h_nlink = h_dir->i_nlink;
35788 +               err = vfsub_rmdir(h_dir, &h_tmp);
35789 +               /* some fs doesn't change the parent nlink in some cases */
35790 +               h_nlink -= h_dir->i_nlink;
35791 +       }
35792 +
35793 +       if (!err) {
35794 +               if (au_ibtop(dir) == bindex) {
35795 +                       /* todo: dir->i_mutex is necessary */
35796 +                       au_cpup_attr_timesizes(dir);
35797 +                       if (h_nlink)
35798 +                               vfsub_drop_nlink(dir);
35799 +               }
35800 +               return 0; /* success */
35801 +       }
35802 +
35803 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35804 +       return err;
35805 +}
35806 +
35807 +static void call_rmdir_whtmp(void *args)
35808 +{
35809 +       int err;
35810 +       aufs_bindex_t bindex;
35811 +       struct au_whtmp_rmdir *a = args;
35812 +       struct super_block *sb;
35813 +       struct dentry *h_parent;
35814 +       struct inode *h_dir;
35815 +       struct au_hinode *hdir;
35816 +
35817 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35818 +       /* inode_lock(a->dir); */
35819 +       err = -EROFS;
35820 +       sb = a->dir->i_sb;
35821 +       si_read_lock(sb, !AuLock_FLUSH);
35822 +       if (!au_br_writable(a->br->br_perm))
35823 +               goto out;
35824 +       bindex = au_br_index(sb, a->br->br_id);
35825 +       if (unlikely(bindex < 0))
35826 +               goto out;
35827 +
35828 +       err = -EIO;
35829 +       ii_write_lock_parent(a->dir);
35830 +       h_parent = dget_parent(a->wh_dentry);
35831 +       h_dir = d_inode(h_parent);
35832 +       hdir = au_hi(a->dir, bindex);
35833 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35834 +       if (unlikely(err))
35835 +               goto out_mnt;
35836 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35837 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35838 +                         a->br);
35839 +       if (!err)
35840 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35841 +       au_hn_inode_unlock(hdir);
35842 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35843 +
35844 +out_mnt:
35845 +       dput(h_parent);
35846 +       ii_write_unlock(a->dir);
35847 +out:
35848 +       /* inode_unlock(a->dir); */
35849 +       au_whtmp_rmdir_free(a);
35850 +       si_read_unlock(sb);
35851 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35852 +       if (unlikely(err))
35853 +               AuIOErr("err %d\n", err);
35854 +}
35855 +
35856 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35857 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35858 +{
35859 +       int wkq_err;
35860 +       struct super_block *sb;
35861 +
35862 +       IMustLock(dir);
35863 +
35864 +       /* all post-process will be done in do_rmdir_whtmp(). */
35865 +       sb = dir->i_sb;
35866 +       args->dir = au_igrab(dir);
35867 +       args->br = au_sbr(sb, bindex);
35868 +       au_lcnt_inc(&args->br->br_count);
35869 +       args->wh_dentry = dget(wh_dentry);
35870 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35871 +       if (unlikely(wkq_err)) {
35872 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35873 +               au_whtmp_rmdir_free(args);
35874 +       }
35875 +}
35876 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35877 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35878 +++ linux/fs/aufs/whout.h       2020-01-27 10:57:18.178871751 +0100
35879 @@ -0,0 +1,86 @@
35880 +/* SPDX-License-Identifier: GPL-2.0 */
35881 +/*
35882 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35883 + *
35884 + * This program, aufs is free software; you can redistribute it and/or modify
35885 + * it under the terms of the GNU General Public License as published by
35886 + * the Free Software Foundation; either version 2 of the License, or
35887 + * (at your option) any later version.
35888 + *
35889 + * This program is distributed in the hope that it will be useful,
35890 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35891 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35892 + * GNU General Public License for more details.
35893 + *
35894 + * You should have received a copy of the GNU General Public License
35895 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35896 + */
35897 +
35898 +/*
35899 + * whiteout for logical deletion and opaque directory
35900 + */
35901 +
35902 +#ifndef __AUFS_WHOUT_H__
35903 +#define __AUFS_WHOUT_H__
35904 +
35905 +#ifdef __KERNEL__
35906 +
35907 +#include "dir.h"
35908 +
35909 +/* whout.c */
35910 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35911 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35912 +int au_diropq_test(struct dentry *h_dentry);
35913 +struct au_branch;
35914 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35915 +                            struct qstr *prefix);
35916 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35917 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35918 +                       struct dentry *dentry);
35919 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35920 +
35921 +/* diropq flags */
35922 +#define AuDiropq_CREATE        1
35923 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35924 +#define au_fset_diropq(flags, name) \
35925 +       do { (flags) |= AuDiropq_##name; } while (0)
35926 +#define au_fclr_diropq(flags, name) \
35927 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35928 +
35929 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35930 +                            unsigned int flags);
35931 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35932 +                         struct au_branch *br);
35933 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35934 +                           struct dentry *h_parent);
35935 +
35936 +/* real rmdir for the whiteout-ed dir */
35937 +struct au_whtmp_rmdir {
35938 +       struct inode *dir;
35939 +       struct au_branch *br;
35940 +       struct dentry *wh_dentry;
35941 +       struct au_nhash whlist;
35942 +};
35943 +
35944 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35945 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35946 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35947 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35948 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35949 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35950 +
35951 +/* ---------------------------------------------------------------------- */
35952 +
35953 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35954 +                                             aufs_bindex_t bindex)
35955 +{
35956 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35957 +}
35958 +
35959 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35960 +{
35961 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35962 +}
35963 +
35964 +#endif /* __KERNEL__ */
35965 +#endif /* __AUFS_WHOUT_H__ */
35966 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35967 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35968 +++ linux/fs/aufs/wkq.c 2020-01-27 10:57:18.178871751 +0100
35969 @@ -0,0 +1,372 @@
35970 +// SPDX-License-Identifier: GPL-2.0
35971 +/*
35972 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35973 + *
35974 + * This program, aufs is free software; you can redistribute it and/or modify
35975 + * it under the terms of the GNU General Public License as published by
35976 + * the Free Software Foundation; either version 2 of the License, or
35977 + * (at your option) any later version.
35978 + *
35979 + * This program is distributed in the hope that it will be useful,
35980 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35981 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35982 + * GNU General Public License for more details.
35983 + *
35984 + * You should have received a copy of the GNU General Public License
35985 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35986 + */
35987 +
35988 +/*
35989 + * workqueue for asynchronous/super-io operations
35990 + * todo: try new credential scheme
35991 + */
35992 +
35993 +#include <linux/module.h>
35994 +#include "aufs.h"
35995 +
35996 +/* internal workqueue named AUFS_WKQ_NAME */
35997 +
35998 +static struct workqueue_struct *au_wkq;
35999 +
36000 +struct au_wkinfo {
36001 +       struct work_struct wk;
36002 +       struct kobject *kobj;
36003 +
36004 +       unsigned int flags; /* see wkq.h */
36005 +
36006 +       au_wkq_func_t func;
36007 +       void *args;
36008 +
36009 +#ifdef CONFIG_LOCKDEP
36010 +       int dont_check;
36011 +       struct held_lock **hlock;
36012 +#endif
36013 +
36014 +       struct completion *comp;
36015 +};
36016 +
36017 +/* ---------------------------------------------------------------------- */
36018 +/*
36019 + * Aufs passes some operations to the workqueue such as the internal copyup.
36020 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36021 + * job run by workqueue depends upon the locks acquired in the other task.
36022 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36023 + * information too. And the job in the workqueue restores the info in order to
36024 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36025 + * correctly and expectedly.
36026 + */
36027 +
36028 +#ifndef CONFIG_LOCKDEP
36029 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36030 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36031 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36032 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36033 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36034 +#else
36035 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36036 +{
36037 +       wkinfo->hlock = NULL;
36038 +       wkinfo->dont_check = 0;
36039 +}
36040 +
36041 +/*
36042 + * 1: matched
36043 + * 0: unmatched
36044 + */
36045 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36046 +{
36047 +       static DEFINE_SPINLOCK(spin);
36048 +       static struct {
36049 +               char *name;
36050 +               struct lock_class_key *key;
36051 +       } a[] = {
36052 +               { .name = "&sbinfo->si_rwsem" },
36053 +               { .name = "&finfo->fi_rwsem" },
36054 +               { .name = "&dinfo->di_rwsem" },
36055 +               { .name = "&iinfo->ii_rwsem" }
36056 +       };
36057 +       static int set;
36058 +       int i;
36059 +
36060 +       /* lockless read from 'set.' see below */
36061 +       if (set == ARRAY_SIZE(a)) {
36062 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36063 +                       if (a[i].key == key)
36064 +                               goto match;
36065 +               goto unmatch;
36066 +       }
36067 +
36068 +       spin_lock(&spin);
36069 +       if (set)
36070 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36071 +                       if (a[i].key == key) {
36072 +                               spin_unlock(&spin);
36073 +                               goto match;
36074 +                       }
36075 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36076 +               if (a[i].key) {
36077 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36078 +                               spin_unlock(&spin);
36079 +                               goto match;
36080 +                       } else
36081 +                               continue;
36082 +               }
36083 +               if (strstr(a[i].name, name)) {
36084 +                       /*
36085 +                        * the order of these three lines is important for the
36086 +                        * lockless read above.
36087 +                        */
36088 +                       a[i].key = key;
36089 +                       spin_unlock(&spin);
36090 +                       set++;
36091 +                       /* AuDbg("%d, %s\n", set, name); */
36092 +                       goto match;
36093 +               }
36094 +       }
36095 +       spin_unlock(&spin);
36096 +       goto unmatch;
36097 +
36098 +match:
36099 +       return 1;
36100 +unmatch:
36101 +       return 0;
36102 +}
36103 +
36104 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36105 +{
36106 +       int err, n;
36107 +       struct task_struct *curr;
36108 +       struct held_lock **hl, *held_locks, *p;
36109 +
36110 +       err = 0;
36111 +       curr = current;
36112 +       wkinfo->dont_check = lockdep_recursing(curr);
36113 +       if (wkinfo->dont_check)
36114 +               goto out;
36115 +       n = curr->lockdep_depth;
36116 +       if (!n)
36117 +               goto out;
36118 +
36119 +       err = -ENOMEM;
36120 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36121 +       if (unlikely(!wkinfo->hlock))
36122 +               goto out;
36123 +
36124 +       err = 0;
36125 +#if 0 /* left for debugging */
36126 +       if (0 && au_debug_test())
36127 +               lockdep_print_held_locks(curr);
36128 +#endif
36129 +       held_locks = curr->held_locks;
36130 +       hl = wkinfo->hlock;
36131 +       while (n--) {
36132 +               p = held_locks++;
36133 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36134 +                       *hl++ = p;
36135 +       }
36136 +       *hl = NULL;
36137 +
36138 +out:
36139 +       return err;
36140 +}
36141 +
36142 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36143 +{
36144 +       au_kfree_try_rcu(wkinfo->hlock);
36145 +}
36146 +
36147 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36148 +{
36149 +       struct held_lock *p, **hl = wkinfo->hlock;
36150 +       int subclass;
36151 +
36152 +       if (wkinfo->dont_check)
36153 +               lockdep_off();
36154 +       if (!hl)
36155 +               return;
36156 +       while ((p = *hl++)) { /* assignment */
36157 +               subclass = lockdep_hlock_class(p)->subclass;
36158 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36159 +               if (p->read)
36160 +                       rwsem_acquire_read(p->instance, subclass, 0,
36161 +                                          /*p->acquire_ip*/_RET_IP_);
36162 +               else
36163 +                       rwsem_acquire(p->instance, subclass, 0,
36164 +                                     /*p->acquire_ip*/_RET_IP_);
36165 +       }
36166 +}
36167 +
36168 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36169 +{
36170 +       struct held_lock *p, **hl = wkinfo->hlock;
36171 +
36172 +       if (wkinfo->dont_check)
36173 +               lockdep_on();
36174 +       if (!hl)
36175 +               return;
36176 +       while ((p = *hl++)) /* assignment */
36177 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36178 +}
36179 +#endif
36180 +
36181 +static void wkq_func(struct work_struct *wk)
36182 +{
36183 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36184 +
36185 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36186 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36187 +
36188 +       au_wkq_lockdep_pre(wkinfo);
36189 +       wkinfo->func(wkinfo->args);
36190 +       au_wkq_lockdep_post(wkinfo);
36191 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36192 +               complete(wkinfo->comp);
36193 +       else {
36194 +               kobject_put(wkinfo->kobj);
36195 +               module_put(THIS_MODULE); /* todo: ?? */
36196 +               au_kfree_rcu(wkinfo);
36197 +       }
36198 +}
36199 +
36200 +/*
36201 + * Since struct completion is large, try allocating it dynamically.
36202 + */
36203 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36204 +
36205 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36206 +{
36207 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36208 +       if (*comp) {
36209 +               init_completion(*comp);
36210 +               wkinfo->comp = *comp;
36211 +               return 0;
36212 +       }
36213 +       return -ENOMEM;
36214 +}
36215 +
36216 +static void au_wkq_comp_free(struct completion *comp)
36217 +{
36218 +       au_kfree_rcu(comp);
36219 +}
36220 +
36221 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36222 +{
36223 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36224 +               if (au_wkq_test()) {
36225 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36226 +                               " due to a dead dir by UDBA,"
36227 +                               " or async xino write?\n");
36228 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36229 +               }
36230 +       } else
36231 +               au_dbg_verify_kthread();
36232 +
36233 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36234 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36235 +               queue_work(au_wkq, &wkinfo->wk);
36236 +       } else {
36237 +               INIT_WORK(&wkinfo->wk, wkq_func);
36238 +               schedule_work(&wkinfo->wk);
36239 +       }
36240 +}
36241 +
36242 +/*
36243 + * Be careful. It is easy to make deadlock happen.
36244 + * processA: lock, wkq and wait
36245 + * processB: wkq and wait, lock in wkq
36246 + * --> deadlock
36247 + */
36248 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36249 +{
36250 +       int err;
36251 +       AuWkqCompDeclare(comp);
36252 +       struct au_wkinfo wkinfo = {
36253 +               .flags  = flags,
36254 +               .func   = func,
36255 +               .args   = args
36256 +       };
36257 +
36258 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36259 +       if (unlikely(err))
36260 +               goto out;
36261 +       err = au_wkq_lockdep_alloc(&wkinfo);
36262 +       if (unlikely(err))
36263 +               goto out_comp;
36264 +       if (!err) {
36265 +               au_wkq_run(&wkinfo);
36266 +               /* no timeout, no interrupt */
36267 +               wait_for_completion(wkinfo.comp);
36268 +       }
36269 +       au_wkq_lockdep_free(&wkinfo);
36270 +
36271 +out_comp:
36272 +       au_wkq_comp_free(comp);
36273 +out:
36274 +       destroy_work_on_stack(&wkinfo.wk);
36275 +       return err;
36276 +}
36277 +
36278 +/*
36279 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36280 + * problem in a concurrent umounting.
36281 + */
36282 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36283 +                 unsigned int flags)
36284 +{
36285 +       int err;
36286 +       struct au_wkinfo *wkinfo;
36287 +
36288 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36289 +
36290 +       /*
36291 +        * wkq_func() must free this wkinfo.
36292 +        * it highly depends upon the implementation of workqueue.
36293 +        */
36294 +       err = 0;
36295 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36296 +       if (wkinfo) {
36297 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36298 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36299 +               wkinfo->func = func;
36300 +               wkinfo->args = args;
36301 +               wkinfo->comp = NULL;
36302 +               au_wkq_lockdep_init(wkinfo);
36303 +               kobject_get(wkinfo->kobj);
36304 +               __module_get(THIS_MODULE); /* todo: ?? */
36305 +
36306 +               au_wkq_run(wkinfo);
36307 +       } else {
36308 +               err = -ENOMEM;
36309 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36310 +       }
36311 +
36312 +       return err;
36313 +}
36314 +
36315 +/* ---------------------------------------------------------------------- */
36316 +
36317 +void au_nwt_init(struct au_nowait_tasks *nwt)
36318 +{
36319 +       atomic_set(&nwt->nw_len, 0);
36320 +       /* smp_mb(); */ /* atomic_set */
36321 +       init_waitqueue_head(&nwt->nw_wq);
36322 +}
36323 +
36324 +void au_wkq_fin(void)
36325 +{
36326 +       destroy_workqueue(au_wkq);
36327 +}
36328 +
36329 +int __init au_wkq_init(void)
36330 +{
36331 +       int err;
36332 +
36333 +       err = 0;
36334 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36335 +       if (IS_ERR(au_wkq))
36336 +               err = PTR_ERR(au_wkq);
36337 +       else if (!au_wkq)
36338 +               err = -ENOMEM;
36339 +
36340 +       return err;
36341 +}
36342 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36343 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36344 +++ linux/fs/aufs/wkq.h 2020-01-27 10:57:18.182205184 +0100
36345 @@ -0,0 +1,89 @@
36346 +/* SPDX-License-Identifier: GPL-2.0 */
36347 +/*
36348 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36349 + *
36350 + * This program, aufs is free software; you can redistribute it and/or modify
36351 + * it under the terms of the GNU General Public License as published by
36352 + * the Free Software Foundation; either version 2 of the License, or
36353 + * (at your option) any later version.
36354 + *
36355 + * This program is distributed in the hope that it will be useful,
36356 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36357 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36358 + * GNU General Public License for more details.
36359 + *
36360 + * You should have received a copy of the GNU General Public License
36361 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36362 + */
36363 +
36364 +/*
36365 + * workqueue for asynchronous/super-io operations
36366 + * todo: try new credentials management scheme
36367 + */
36368 +
36369 +#ifndef __AUFS_WKQ_H__
36370 +#define __AUFS_WKQ_H__
36371 +
36372 +#ifdef __KERNEL__
36373 +
36374 +#include <linux/wait.h>
36375 +
36376 +struct super_block;
36377 +
36378 +/* ---------------------------------------------------------------------- */
36379 +
36380 +/*
36381 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36382 + */
36383 +struct au_nowait_tasks {
36384 +       atomic_t                nw_len;
36385 +       wait_queue_head_t       nw_wq;
36386 +};
36387 +
36388 +/* ---------------------------------------------------------------------- */
36389 +
36390 +typedef void (*au_wkq_func_t)(void *args);
36391 +
36392 +/* wkq flags */
36393 +#define AuWkq_WAIT     1
36394 +#define AuWkq_NEST     (1 << 1)
36395 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36396 +#define au_fset_wkq(flags, name) \
36397 +       do { (flags) |= AuWkq_##name; } while (0)
36398 +#define au_fclr_wkq(flags, name) \
36399 +       do { (flags) &= ~AuWkq_##name; } while (0)
36400 +
36401 +/* wkq.c */
36402 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36403 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36404 +                 unsigned int flags);
36405 +void au_nwt_init(struct au_nowait_tasks *nwt);
36406 +int __init au_wkq_init(void);
36407 +void au_wkq_fin(void);
36408 +
36409 +/* ---------------------------------------------------------------------- */
36410 +
36411 +static inline int au_wkq_test(void)
36412 +{
36413 +       return current->flags & PF_WQ_WORKER;
36414 +}
36415 +
36416 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36417 +{
36418 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36419 +}
36420 +
36421 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36422 +{
36423 +       if (atomic_dec_and_test(&nwt->nw_len))
36424 +               wake_up_all(&nwt->nw_wq);
36425 +}
36426 +
36427 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36428 +{
36429 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36430 +       return 0;
36431 +}
36432 +
36433 +#endif /* __KERNEL__ */
36434 +#endif /* __AUFS_WKQ_H__ */
36435 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36436 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36437 +++ linux/fs/aufs/xattr.c       2020-01-27 10:57:18.182205184 +0100
36438 @@ -0,0 +1,356 @@
36439 +// SPDX-License-Identifier: GPL-2.0
36440 +/*
36441 + * Copyright (C) 2014-2020 Junjiro R. Okajima
36442 + *
36443 + * This program, aufs is free software; you can redistribute it and/or modify
36444 + * it under the terms of the GNU General Public License as published by
36445 + * the Free Software Foundation; either version 2 of the License, or
36446 + * (at your option) any later version.
36447 + *
36448 + * This program is distributed in the hope that it will be useful,
36449 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36450 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36451 + * GNU General Public License for more details.
36452 + *
36453 + * You should have received a copy of the GNU General Public License
36454 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36455 + */
36456 +
36457 +/*
36458 + * handling xattr functions
36459 + */
36460 +
36461 +#include <linux/fs.h>
36462 +#include <linux/posix_acl_xattr.h>
36463 +#include <linux/xattr.h>
36464 +#include "aufs.h"
36465 +
36466 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36467 +{
36468 +       if (!ignore_flags)
36469 +               goto out;
36470 +       switch (err) {
36471 +       case -ENOMEM:
36472 +       case -EDQUOT:
36473 +               goto out;
36474 +       }
36475 +
36476 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36477 +               err = 0;
36478 +               goto out;
36479 +       }
36480 +
36481 +#define cmp(brattr, prefix) do {                                       \
36482 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36483 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36484 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36485 +                               err = 0;                                \
36486 +                       goto out;                                       \
36487 +               }                                                       \
36488 +       } while (0)
36489 +
36490 +       cmp(SEC, SECURITY);
36491 +       cmp(SYS, SYSTEM);
36492 +       cmp(TR, TRUSTED);
36493 +       cmp(USR, USER);
36494 +#undef cmp
36495 +
36496 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36497 +               err = 0;
36498 +
36499 +out:
36500 +       return err;
36501 +}
36502 +
36503 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36504 +
36505 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36506 +                           char *name, char **buf, unsigned int ignore_flags,
36507 +                           unsigned int verbose)
36508 +{
36509 +       int err;
36510 +       ssize_t ssz;
36511 +       struct inode *h_idst;
36512 +
36513 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36514 +       err = ssz;
36515 +       if (unlikely(err <= 0)) {
36516 +               if (err == -ENODATA
36517 +                   || (err == -EOPNOTSUPP
36518 +                       && ((ignore_flags & au_xattr_out_of_list)
36519 +                           || (au_test_nfs_noacl(d_inode(h_src))
36520 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36521 +                                   || !strcmp(name,
36522 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36523 +                           ))
36524 +                       err = 0;
36525 +               if (err && (verbose || au_debug_test()))
36526 +                       pr_err("%s, err %d\n", name, err);
36527 +               goto out;
36528 +       }
36529 +
36530 +       /* unlock it temporary */
36531 +       h_idst = d_inode(h_dst);
36532 +       inode_unlock(h_idst);
36533 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36534 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36535 +       if (unlikely(err)) {
36536 +               if (verbose || au_debug_test())
36537 +                       pr_err("%s, err %d\n", name, err);
36538 +               err = au_xattr_ignore(err, name, ignore_flags);
36539 +       }
36540 +
36541 +out:
36542 +       return err;
36543 +}
36544 +
36545 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36546 +                 unsigned int verbose)
36547 +{
36548 +       int err, unlocked, acl_access, acl_default;
36549 +       ssize_t ssz;
36550 +       struct inode *h_isrc, *h_idst;
36551 +       char *value, *p, *o, *e;
36552 +
36553 +       /* try stopping to update the source inode while we are referencing */
36554 +       /* there should not be the parent-child relationship between them */
36555 +       h_isrc = d_inode(h_src);
36556 +       h_idst = d_inode(h_dst);
36557 +       inode_unlock(h_idst);
36558 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36559 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36560 +       unlocked = 0;
36561 +
36562 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36563 +       ssz = vfs_listxattr(h_src, NULL, 0);
36564 +       err = ssz;
36565 +       if (unlikely(err < 0)) {
36566 +               AuTraceErr(err);
36567 +               if (err == -ENODATA
36568 +                   || err == -EOPNOTSUPP)
36569 +                       err = 0;        /* ignore */
36570 +               goto out;
36571 +       }
36572 +
36573 +       err = 0;
36574 +       p = NULL;
36575 +       o = NULL;
36576 +       if (ssz) {
36577 +               err = -ENOMEM;
36578 +               p = kmalloc(ssz, GFP_NOFS);
36579 +               o = p;
36580 +               if (unlikely(!p))
36581 +                       goto out;
36582 +               err = vfs_listxattr(h_src, p, ssz);
36583 +       }
36584 +       inode_unlock_shared(h_isrc);
36585 +       unlocked = 1;
36586 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36587 +       if (unlikely(err < 0))
36588 +               goto out_free;
36589 +
36590 +       err = 0;
36591 +       e = p + ssz;
36592 +       value = NULL;
36593 +       acl_access = 0;
36594 +       acl_default = 0;
36595 +       while (!err && p < e) {
36596 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36597 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36598 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36599 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36600 +                                       - 1);
36601 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36602 +                                      verbose);
36603 +               p += strlen(p) + 1;
36604 +       }
36605 +       AuTraceErr(err);
36606 +       ignore_flags |= au_xattr_out_of_list;
36607 +       if (!err && !acl_access) {
36608 +               err = au_do_cpup_xattr(h_dst, h_src,
36609 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36610 +                                      ignore_flags, verbose);
36611 +               AuTraceErr(err);
36612 +       }
36613 +       if (!err && !acl_default) {
36614 +               err = au_do_cpup_xattr(h_dst, h_src,
36615 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36616 +                                      ignore_flags, verbose);
36617 +               AuTraceErr(err);
36618 +       }
36619 +
36620 +       au_kfree_try_rcu(value);
36621 +
36622 +out_free:
36623 +       au_kfree_try_rcu(o);
36624 +out:
36625 +       if (!unlocked)
36626 +               inode_unlock_shared(h_isrc);
36627 +       AuTraceErr(err);
36628 +       return err;
36629 +}
36630 +
36631 +/* ---------------------------------------------------------------------- */
36632 +
36633 +static int au_smack_reentering(struct super_block *sb)
36634 +{
36635 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
36636 +       /*
36637 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36638 +        * i_op->getxattr(). ouch.
36639 +        */
36640 +       return si_pid_test(sb);
36641 +#else
36642 +       return 0;
36643 +#endif
36644 +}
36645 +
36646 +enum {
36647 +       AU_XATTR_LIST,
36648 +       AU_XATTR_GET
36649 +};
36650 +
36651 +struct au_lgxattr {
36652 +       int type;
36653 +       union {
36654 +               struct {
36655 +                       char    *list;
36656 +                       size_t  size;
36657 +               } list;
36658 +               struct {
36659 +                       const char      *name;
36660 +                       void            *value;
36661 +                       size_t          size;
36662 +               } get;
36663 +       } u;
36664 +};
36665 +
36666 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
36667 +{
36668 +       ssize_t err;
36669 +       int reenter;
36670 +       struct path h_path;
36671 +       struct super_block *sb;
36672 +
36673 +       sb = dentry->d_sb;
36674 +       reenter = au_smack_reentering(sb);
36675 +       if (!reenter) {
36676 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36677 +               if (unlikely(err))
36678 +                       goto out;
36679 +       }
36680 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
36681 +       if (unlikely(err))
36682 +               goto out_si;
36683 +       if (unlikely(!h_path.dentry))
36684 +               /* illegally overlapped or something */
36685 +               goto out_di; /* pretending success */
36686 +
36687 +       /* always topmost entry only */
36688 +       switch (arg->type) {
36689 +       case AU_XATTR_LIST:
36690 +               err = vfs_listxattr(h_path.dentry,
36691 +                                   arg->u.list.list, arg->u.list.size);
36692 +               break;
36693 +       case AU_XATTR_GET:
36694 +               AuDebugOn(d_is_negative(h_path.dentry));
36695 +               err = vfs_getxattr(h_path.dentry,
36696 +                                  arg->u.get.name, arg->u.get.value,
36697 +                                  arg->u.get.size);
36698 +               break;
36699 +       }
36700 +
36701 +out_di:
36702 +       if (!reenter)
36703 +               di_read_unlock(dentry, AuLock_IR);
36704 +out_si:
36705 +       if (!reenter)
36706 +               si_read_unlock(sb);
36707 +out:
36708 +       AuTraceErr(err);
36709 +       return err;
36710 +}
36711 +
36712 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36713 +{
36714 +       struct au_lgxattr arg = {
36715 +               .type = AU_XATTR_LIST,
36716 +               .u.list = {
36717 +                       .list   = list,
36718 +                       .size   = size
36719 +               },
36720 +       };
36721 +
36722 +       return au_lgxattr(dentry, &arg);
36723 +}
36724 +
36725 +static ssize_t au_getxattr(struct dentry *dentry,
36726 +                          struct inode *inode __maybe_unused,
36727 +                          const char *name, void *value, size_t size)
36728 +{
36729 +       struct au_lgxattr arg = {
36730 +               .type = AU_XATTR_GET,
36731 +               .u.get = {
36732 +                       .name   = name,
36733 +                       .value  = value,
36734 +                       .size   = size
36735 +               },
36736 +       };
36737 +
36738 +       return au_lgxattr(dentry, &arg);
36739 +}
36740 +
36741 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36742 +                      const char *name, const void *value, size_t size,
36743 +                      int flags)
36744 +{
36745 +       struct au_sxattr arg = {
36746 +               .type = AU_XATTR_SET,
36747 +               .u.set = {
36748 +                       .name   = name,
36749 +                       .value  = value,
36750 +                       .size   = size,
36751 +                       .flags  = flags
36752 +               },
36753 +       };
36754 +
36755 +       return au_sxattr(dentry, inode, &arg);
36756 +}
36757 +
36758 +/* ---------------------------------------------------------------------- */
36759 +
36760 +static int au_xattr_get(const struct xattr_handler *handler,
36761 +                       struct dentry *dentry, struct inode *inode,
36762 +                       const char *name, void *buffer, size_t size)
36763 +{
36764 +       return au_getxattr(dentry, inode, name, buffer, size);
36765 +}
36766 +
36767 +static int au_xattr_set(const struct xattr_handler *handler,
36768 +                       struct dentry *dentry, struct inode *inode,
36769 +                       const char *name, const void *value, size_t size,
36770 +                       int flags)
36771 +{
36772 +       return au_setxattr(dentry, inode, name, value, size, flags);
36773 +}
36774 +
36775 +static const struct xattr_handler au_xattr_handler = {
36776 +       .name   = "",
36777 +       .prefix = "",
36778 +       .get    = au_xattr_get,
36779 +       .set    = au_xattr_set
36780 +};
36781 +
36782 +static const struct xattr_handler *au_xattr_handlers[] = {
36783 +#ifdef CONFIG_FS_POSIX_ACL
36784 +       &posix_acl_access_xattr_handler,
36785 +       &posix_acl_default_xattr_handler,
36786 +#endif
36787 +       &au_xattr_handler, /* must be last */
36788 +       NULL
36789 +};
36790 +
36791 +void au_xattr_init(struct super_block *sb)
36792 +{
36793 +       sb->s_xattr = au_xattr_handlers;
36794 +}
36795 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36796 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36797 +++ linux/fs/aufs/xino.c        2020-01-27 10:57:18.182205184 +0100
36798 @@ -0,0 +1,1965 @@
36799 +// SPDX-License-Identifier: GPL-2.0
36800 +/*
36801 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36802 + *
36803 + * This program, aufs is free software; you can redistribute it and/or modify
36804 + * it under the terms of the GNU General Public License as published by
36805 + * the Free Software Foundation; either version 2 of the License, or
36806 + * (at your option) any later version.
36807 + *
36808 + * This program is distributed in the hope that it will be useful,
36809 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36810 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36811 + * GNU General Public License for more details.
36812 + *
36813 + * You should have received a copy of the GNU General Public License
36814 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36815 + */
36816 +
36817 +/*
36818 + * external inode number translation table and bitmap
36819 + *
36820 + * things to consider
36821 + * - the lifetime
36822 + *   + au_xino object
36823 + *   + XINO files (xino, xib, xigen)
36824 + *   + dynamic debugfs entries (xiN)
36825 + *   + static debugfs entries (xib, xigen)
36826 + *   + static sysfs entry (xi_path)
36827 + * - several entry points to handle them.
36828 + *   + mount(2) without xino option (default)
36829 + *   + mount(2) with xino option
36830 + *   + mount(2) with noxino option
36831 + *   + umount(2)
36832 + *   + remount with add/del branches
36833 + *   + remount with xino/noxino options
36834 + */
36835 +
36836 +#include <linux/seq_file.h>
36837 +#include <linux/statfs.h>
36838 +#include "aufs.h"
36839 +
36840 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36841 +                                    aufs_bindex_t bbot,
36842 +                                    struct super_block *h_sb)
36843 +{
36844 +       /* todo: try binary-search if the branches are many */
36845 +       for (; btop <= bbot; btop++)
36846 +               if (h_sb == au_sbr_sb(sb, btop))
36847 +                       return btop;
36848 +       return -1;
36849 +}
36850 +
36851 +/*
36852 + * find another branch who is on the same filesystem of the specified
36853 + * branch{@btgt}. search until @bbot.
36854 + */
36855 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36856 +                                 aufs_bindex_t bbot)
36857 +{
36858 +       aufs_bindex_t bindex;
36859 +       struct super_block *tgt_sb;
36860 +
36861 +       tgt_sb = au_sbr_sb(sb, btgt);
36862 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36863 +       if (bindex < 0)
36864 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36865 +
36866 +       return bindex;
36867 +}
36868 +
36869 +/* ---------------------------------------------------------------------- */
36870 +
36871 +/*
36872 + * stop unnecessary notify events at creating xino files
36873 + */
36874 +
36875 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36876 +{
36877 +       aufs_bindex_t bfound, bindex, bbot;
36878 +       struct dentry *parent;
36879 +       struct au_branch *br;
36880 +
36881 +       bfound = -1;
36882 +       parent = dentry->d_parent; /* safe d_parent access */
36883 +       bbot = au_sbbot(sb);
36884 +       for (bindex = 0; bindex <= bbot; bindex++) {
36885 +               br = au_sbr(sb, bindex);
36886 +               if (au_br_dentry(br) == parent) {
36887 +                       bfound = bindex;
36888 +                       break;
36889 +               }
36890 +       }
36891 +
36892 +       AuDbg("bfound b%d\n", bfound);
36893 +       return bfound;
36894 +}
36895 +
36896 +struct au_xino_lock_dir {
36897 +       struct au_hinode *hdir;
36898 +       struct dentry *parent;
36899 +       struct inode *dir;
36900 +};
36901 +
36902 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36903 +                                         unsigned int lsc)
36904 +{
36905 +       struct dentry *parent;
36906 +       struct inode *dir;
36907 +
36908 +       parent = dget_parent(dentry);
36909 +       dir = d_inode(parent);
36910 +       inode_lock_nested(dir, lsc);
36911 +#if 0 /* it should not happen */
36912 +       spin_lock(&dentry->d_lock);
36913 +       if (unlikely(dentry->d_parent != parent)) {
36914 +               spin_unlock(&dentry->d_lock);
36915 +               inode_unlock(dir);
36916 +               dput(parent);
36917 +               parent = NULL;
36918 +               goto out;
36919 +       }
36920 +       spin_unlock(&dentry->d_lock);
36921 +
36922 +out:
36923 +#endif
36924 +       return parent;
36925 +}
36926 +
36927 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36928 +                            struct au_xino_lock_dir *ldir)
36929 +{
36930 +       aufs_bindex_t bindex;
36931 +
36932 +       ldir->hdir = NULL;
36933 +       bindex = au_xi_root(sb, xipath->dentry);
36934 +       if (bindex >= 0) {
36935 +               /* rw branch root */
36936 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36937 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36938 +       } else {
36939 +               /* other */
36940 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36941 +                                                  AuLsc_I_PARENT);
36942 +               ldir->dir = d_inode(ldir->parent);
36943 +       }
36944 +}
36945 +
36946 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36947 +{
36948 +       if (ldir->hdir)
36949 +               au_hn_inode_unlock(ldir->hdir);
36950 +       else {
36951 +               inode_unlock(ldir->dir);
36952 +               dput(ldir->parent);
36953 +       }
36954 +}
36955 +
36956 +/* ---------------------------------------------------------------------- */
36957 +
36958 +/*
36959 + * create and set a new xino file
36960 + */
36961 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36962 +                           int wbrtop)
36963 +{
36964 +       struct file *file;
36965 +       struct dentry *h_parent, *d;
36966 +       struct inode *h_dir, *inode;
36967 +       int err;
36968 +       static DEFINE_MUTEX(mtx);
36969 +
36970 +       /*
36971 +        * at mount-time, and the xino file is the default path,
36972 +        * hnotify is disabled so we have no notify events to ignore.
36973 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36974 +        */
36975 +       if (!wbrtop)
36976 +               mutex_lock(&mtx);
36977 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36978 +                              /* | __FMODE_NONOTIFY */,
36979 +                              0666);
36980 +       if (IS_ERR(file)) {
36981 +               if (!wbrtop)
36982 +                       mutex_unlock(&mtx);
36983 +               if (!silent)
36984 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36985 +               return file;
36986 +       }
36987 +
36988 +       /* keep file count */
36989 +       err = 0;
36990 +       d = file->f_path.dentry;
36991 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36992 +       if (!wbrtop)
36993 +               mutex_unlock(&mtx);
36994 +       /* mnt_want_write() is unnecessary here */
36995 +       h_dir = d_inode(h_parent);
36996 +       inode = file_inode(file);
36997 +       /* no delegation since it is just created */
36998 +       if (inode->i_nlink)
36999 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37000 +                                  /*force*/0);
37001 +       inode_unlock(h_dir);
37002 +       dput(h_parent);
37003 +       if (unlikely(err)) {
37004 +               if (!silent)
37005 +                       pr_err("unlink %s(%d)\n", fpath, err);
37006 +               goto out;
37007 +       }
37008 +
37009 +       err = -EINVAL;
37010 +       if (unlikely(sb == d->d_sb)) {
37011 +               if (!silent)
37012 +                       pr_err("%s must be outside\n", fpath);
37013 +               goto out;
37014 +       }
37015 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37016 +               if (!silent)
37017 +                       pr_err("xino doesn't support %s(%s)\n",
37018 +                              fpath, au_sbtype(d->d_sb));
37019 +               goto out;
37020 +       }
37021 +       return file; /* success */
37022 +
37023 +out:
37024 +       fput(file);
37025 +       file = ERR_PTR(err);
37026 +       return file;
37027 +}
37028 +
37029 +/*
37030 + * create a new xinofile at the same place/path as @base.
37031 + */
37032 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37033 +                            struct file *copy_src)
37034 +{
37035 +       struct file *file;
37036 +       struct dentry *dentry, *parent;
37037 +       struct inode *dir, *delegated;
37038 +       struct qstr *name;
37039 +       struct path path;
37040 +       int err, do_unlock;
37041 +       struct au_xino_lock_dir ldir;
37042 +
37043 +       do_unlock = 1;
37044 +       au_xino_lock_dir(sb, base, &ldir);
37045 +       dentry = base->dentry;
37046 +       parent = dentry->d_parent; /* dir inode is locked */
37047 +       dir = d_inode(parent);
37048 +       IMustLock(dir);
37049 +
37050 +       name = &dentry->d_name;
37051 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
37052 +       if (IS_ERR(path.dentry)) {
37053 +               file = (void *)path.dentry;
37054 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37055 +               goto out;
37056 +       }
37057 +
37058 +       /* no need to mnt_want_write() since we call dentry_open() later */
37059 +       err = vfs_create(dir, path.dentry, 0666, NULL);
37060 +       if (unlikely(err)) {
37061 +               file = ERR_PTR(err);
37062 +               pr_err("%pd create err %d\n", dentry, err);
37063 +               goto out_dput;
37064 +       }
37065 +
37066 +       path.mnt = base->mnt;
37067 +       file = vfsub_dentry_open(&path,
37068 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37069 +                                /* | __FMODE_NONOTIFY */);
37070 +       if (IS_ERR(file)) {
37071 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37072 +               goto out_dput;
37073 +       }
37074 +
37075 +       delegated = NULL;
37076 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37077 +       au_xino_unlock_dir(&ldir);
37078 +       do_unlock = 0;
37079 +       if (unlikely(err == -EWOULDBLOCK)) {
37080 +               pr_warn("cannot retry for NFSv4 delegation"
37081 +                       " for an internal unlink\n");
37082 +               iput(delegated);
37083 +       }
37084 +       if (unlikely(err)) {
37085 +               pr_err("%pd unlink err %d\n", dentry, err);
37086 +               goto out_fput;
37087 +       }
37088 +
37089 +       if (copy_src) {
37090 +               /* no one can touch copy_src xino */
37091 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37092 +               if (unlikely(err)) {
37093 +                       pr_err("%pd copy err %d\n", dentry, err);
37094 +                       goto out_fput;
37095 +               }
37096 +       }
37097 +       goto out_dput; /* success */
37098 +
37099 +out_fput:
37100 +       fput(file);
37101 +       file = ERR_PTR(err);
37102 +out_dput:
37103 +       dput(path.dentry);
37104 +out:
37105 +       if (do_unlock)
37106 +               au_xino_unlock_dir(&ldir);
37107 +       return file;
37108 +}
37109 +
37110 +struct file *au_xino_file1(struct au_xino *xi)
37111 +{
37112 +       struct file *file;
37113 +       unsigned int u, nfile;
37114 +
37115 +       file = NULL;
37116 +       nfile = xi->xi_nfile;
37117 +       for (u = 0; u < nfile; u++) {
37118 +               file = xi->xi_file[u];
37119 +               if (file)
37120 +                       break;
37121 +       }
37122 +
37123 +       return file;
37124 +}
37125 +
37126 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37127 +{
37128 +       int err;
37129 +       struct file *f;
37130 +       void *p;
37131 +
37132 +       if (file)
37133 +               get_file(file);
37134 +
37135 +       err = 0;
37136 +       f = NULL;
37137 +       if (idx < xi->xi_nfile) {
37138 +               f = xi->xi_file[idx];
37139 +               if (f)
37140 +                       fput(f);
37141 +       } else {
37142 +               p = au_kzrealloc(xi->xi_file,
37143 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37144 +                                sizeof(*xi->xi_file) * (idx + 1),
37145 +                                GFP_NOFS, /*may_shrink*/0);
37146 +               if (p) {
37147 +                       MtxMustLock(&xi->xi_mtx);
37148 +                       xi->xi_file = p;
37149 +                       xi->xi_nfile = idx + 1;
37150 +               } else {
37151 +                       err = -ENOMEM;
37152 +                       if (file)
37153 +                               fput(file);
37154 +                       goto out;
37155 +               }
37156 +       }
37157 +       xi->xi_file[idx] = file;
37158 +
37159 +out:
37160 +       return err;
37161 +}
37162 +
37163 +/*
37164 + * if @xinew->xi is not set, then create new xigen file.
37165 + */
37166 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37167 +{
37168 +       struct file *file;
37169 +       int err;
37170 +
37171 +       SiMustAnyLock(sb);
37172 +
37173 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37174 +       if (IS_ERR(file)) {
37175 +               err = PTR_ERR(file);
37176 +               pr_err("%s[%d], err %d\n",
37177 +                      xinew->xi ? "xino" : "xigen",
37178 +                      xinew->idx, err);
37179 +               goto out;
37180 +       }
37181 +
37182 +       if (xinew->xi)
37183 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37184 +       else {
37185 +               BUG();
37186 +               /* todo: make xigen file an array */
37187 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37188 +       }
37189 +       fput(file);
37190 +       if (unlikely(err))
37191 +               file = ERR_PTR(err);
37192 +
37193 +out:
37194 +       return file;
37195 +}
37196 +
37197 +/* ---------------------------------------------------------------------- */
37198 +
37199 +/*
37200 + * truncate xino files
37201 + */
37202 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37203 +                           int idx, struct kstatfs *st)
37204 +{
37205 +       int err;
37206 +       blkcnt_t blocks;
37207 +       struct file *file, *new_xino;
37208 +       struct au_xi_new xinew = {
37209 +               .idx = idx
37210 +       };
37211 +
37212 +       err = 0;
37213 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37214 +       file = au_xino_file(xinew.xi, idx);
37215 +       if (!file)
37216 +               goto out;
37217 +
37218 +       xinew.base = &file->f_path;
37219 +       err = vfs_statfs(xinew.base, st);
37220 +       if (unlikely(err)) {
37221 +               AuErr1("statfs err %d, ignored\n", err);
37222 +               err = 0;
37223 +               goto out;
37224 +       }
37225 +
37226 +       blocks = file_inode(file)->i_blocks;
37227 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37228 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37229 +
37230 +       xinew.copy_src = file;
37231 +       new_xino = au_xi_new(sb, &xinew);
37232 +       if (IS_ERR(new_xino)) {
37233 +               err = PTR_ERR(new_xino);
37234 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37235 +               goto out;
37236 +       }
37237 +
37238 +       err = vfs_statfs(&new_xino->f_path, st);
37239 +       if (!err)
37240 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37241 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37242 +                       st->f_bfree, st->f_blocks);
37243 +       else {
37244 +               AuErr1("statfs err %d, ignored\n", err);
37245 +               err = 0;
37246 +       }
37247 +
37248 +out:
37249 +       return err;
37250 +}
37251 +
37252 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37253 +{
37254 +       int err, i;
37255 +       unsigned long jiffy;
37256 +       aufs_bindex_t bbot;
37257 +       struct kstatfs *st;
37258 +       struct au_branch *br;
37259 +       struct au_xino *xi;
37260 +
37261 +       err = -ENOMEM;
37262 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37263 +       if (unlikely(!st))
37264 +               goto out;
37265 +
37266 +       err = -EINVAL;
37267 +       bbot = au_sbbot(sb);
37268 +       if (unlikely(bindex < 0 || bbot < bindex))
37269 +               goto out_st;
37270 +
37271 +       err = 0;
37272 +       jiffy = jiffies;
37273 +       br = au_sbr(sb, bindex);
37274 +       xi = br->br_xino;
37275 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37276 +               err = au_xino_do_trunc(sb, bindex, i, st);
37277 +       if (!err)
37278 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37279 +
37280 +out_st:
37281 +       au_kfree_rcu(st);
37282 +out:
37283 +       return err;
37284 +}
37285 +
37286 +struct xino_do_trunc_args {
37287 +       struct super_block *sb;
37288 +       struct au_branch *br;
37289 +       int idx;
37290 +};
37291 +
37292 +static void xino_do_trunc(void *_args)
37293 +{
37294 +       struct xino_do_trunc_args *args = _args;
37295 +       struct super_block *sb;
37296 +       struct au_branch *br;
37297 +       struct inode *dir;
37298 +       int err, idx;
37299 +       aufs_bindex_t bindex;
37300 +
37301 +       err = 0;
37302 +       sb = args->sb;
37303 +       dir = d_inode(sb->s_root);
37304 +       br = args->br;
37305 +       idx = args->idx;
37306 +
37307 +       si_noflush_write_lock(sb);
37308 +       ii_read_lock_parent(dir);
37309 +       bindex = au_br_index(sb, br->br_id);
37310 +       err = au_xino_trunc(sb, bindex, idx);
37311 +       ii_read_unlock(dir);
37312 +       if (unlikely(err))
37313 +               pr_warn("err b%d, (%d)\n", bindex, err);
37314 +       atomic_dec(&br->br_xino->xi_truncating);
37315 +       au_lcnt_dec(&br->br_count);
37316 +       si_write_unlock(sb);
37317 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37318 +       au_kfree_rcu(args);
37319 +}
37320 +
37321 +/*
37322 + * returns the index in the xi_file array whose corresponding file is necessary
37323 + * to truncate, or -1 which means no need to truncate.
37324 + */
37325 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37326 +{
37327 +       int err;
37328 +       unsigned int u;
37329 +       struct kstatfs st;
37330 +       struct au_sbinfo *sbinfo;
37331 +       struct au_xino *xi;
37332 +       struct file *file;
37333 +
37334 +       /* todo: si_xino_expire and the ratio should be customizable */
37335 +       sbinfo = au_sbi(sb);
37336 +       if (time_before(jiffies,
37337 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37338 +               return -1;
37339 +
37340 +       /* truncation border */
37341 +       xi = br->br_xino;
37342 +       for (u = 0; u < xi->xi_nfile; u++) {
37343 +               file = au_xino_file(xi, u);
37344 +               if (!file)
37345 +                       continue;
37346 +
37347 +               err = vfs_statfs(&file->f_path, &st);
37348 +               if (unlikely(err)) {
37349 +                       AuErr1("statfs err %d, ignored\n", err);
37350 +                       return -1;
37351 +               }
37352 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37353 +                   >= AUFS_XINO_DEF_TRUNC)
37354 +                       return u;
37355 +       }
37356 +
37357 +       return -1;
37358 +}
37359 +
37360 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37361 +{
37362 +       int idx;
37363 +       struct xino_do_trunc_args *args;
37364 +       int wkq_err;
37365 +
37366 +       idx = xino_trunc_test(sb, br);
37367 +       if (idx < 0)
37368 +               return;
37369 +
37370 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37371 +               goto out;
37372 +
37373 +       /* lock and kfree() will be called in trunc_xino() */
37374 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37375 +       if (unlikely(!args)) {
37376 +               AuErr1("no memory\n");
37377 +               goto out;
37378 +       }
37379 +
37380 +       au_lcnt_inc(&br->br_count);
37381 +       args->sb = sb;
37382 +       args->br = br;
37383 +       args->idx = idx;
37384 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37385 +       if (!wkq_err)
37386 +               return; /* success */
37387 +
37388 +       pr_err("wkq %d\n", wkq_err);
37389 +       au_lcnt_dec(&br->br_count);
37390 +       au_kfree_rcu(args);
37391 +
37392 +out:
37393 +       atomic_dec(&br->br_xino->xi_truncating);
37394 +}
37395 +
37396 +/* ---------------------------------------------------------------------- */
37397 +
37398 +struct au_xi_calc {
37399 +       int idx;
37400 +       loff_t pos;
37401 +};
37402 +
37403 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37404 +                      struct au_xi_calc *calc)
37405 +{
37406 +       loff_t maxent;
37407 +
37408 +       maxent = au_xi_maxent(sb);
37409 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37410 +       calc->pos *= sizeof(ino_t);
37411 +}
37412 +
37413 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37414 +                               struct au_xi_calc *calc)
37415 +{
37416 +       int err;
37417 +       struct file *file;
37418 +       struct au_xino *xi = br->br_xino;
37419 +       struct au_xi_new xinew = {
37420 +               .xi = xi
37421 +       };
37422 +
37423 +       SiMustAnyLock(sb);
37424 +
37425 +       err = 0;
37426 +       if (!xi)
37427 +               goto out;
37428 +
37429 +       mutex_lock(&xi->xi_mtx);
37430 +       file = au_xino_file(xi, calc->idx);
37431 +       if (file)
37432 +               goto out_mtx;
37433 +
37434 +       file = au_xino_file(xi, /*idx*/-1);
37435 +       AuDebugOn(!file);
37436 +       xinew.idx = calc->idx;
37437 +       xinew.base = &file->f_path;
37438 +       /* xinew.copy_src = NULL; */
37439 +       file = au_xi_new(sb, &xinew);
37440 +       if (IS_ERR(file))
37441 +               err = PTR_ERR(file);
37442 +
37443 +out_mtx:
37444 +       mutex_unlock(&xi->xi_mtx);
37445 +out:
37446 +       return err;
37447 +}
37448 +
37449 +struct au_xino_do_new_async_args {
37450 +       struct super_block *sb;
37451 +       struct au_branch *br;
37452 +       struct au_xi_calc calc;
37453 +       ino_t ino;
37454 +};
37455 +
37456 +struct au_xi_writing {
37457 +       struct hlist_bl_node node;
37458 +       ino_t h_ino, ino;
37459 +};
37460 +
37461 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37462 +                           struct au_xi_calc *calc, ino_t ino);
37463 +
37464 +static void au_xino_call_do_new_async(void *args)
37465 +{
37466 +       struct au_xino_do_new_async_args *a = args;
37467 +       struct au_branch *br;
37468 +       struct super_block *sb;
37469 +       struct au_sbinfo *sbi;
37470 +       struct inode *root;
37471 +       struct file *file;
37472 +       struct au_xi_writing *del, *p;
37473 +       struct hlist_bl_head *hbl;
37474 +       struct hlist_bl_node *pos;
37475 +       int err;
37476 +
37477 +       br = a->br;
37478 +       sb = a->sb;
37479 +       sbi = au_sbi(sb);
37480 +       si_noflush_read_lock(sb);
37481 +       root = d_inode(sb->s_root);
37482 +       ii_read_lock_child(root);
37483 +       err = au_xino_do_new_async(sb, br, &a->calc);
37484 +       if (unlikely(err)) {
37485 +               AuIOErr("err %d\n", err);
37486 +               goto out;
37487 +       }
37488 +
37489 +       file = au_xino_file(br->br_xino, a->calc.idx);
37490 +       AuDebugOn(!file);
37491 +       err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
37492 +       if (unlikely(err)) {
37493 +               AuIOErr("err %d\n", err);
37494 +               goto out;
37495 +       }
37496 +
37497 +       del = NULL;
37498 +       hbl = &br->br_xino->xi_writing;
37499 +       hlist_bl_lock(hbl);
37500 +       au_hbl_for_each(pos, hbl) {
37501 +               p = container_of(pos, struct au_xi_writing, node);
37502 +               if (p->ino == a->ino) {
37503 +                       del = p;
37504 +                       hlist_bl_del(&p->node);
37505 +                       break;
37506 +               }
37507 +       }
37508 +       hlist_bl_unlock(hbl);
37509 +       au_kfree_rcu(del);
37510 +
37511 +out:
37512 +       au_lcnt_dec(&br->br_count);
37513 +       ii_read_unlock(root);
37514 +       si_read_unlock(sb);
37515 +       au_nwt_done(&sbi->si_nowait);
37516 +       au_kfree_rcu(a);
37517 +}
37518 +
37519 +/*
37520 + * create a new xino file asynchronously
37521 + */
37522 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37523 +                            struct au_xi_calc *calc, ino_t ino)
37524 +{
37525 +       int err;
37526 +       struct au_xino_do_new_async_args *arg;
37527 +
37528 +       err = -ENOMEM;
37529 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37530 +       if (unlikely(!arg))
37531 +               goto out;
37532 +
37533 +       arg->sb = sb;
37534 +       arg->br = br;
37535 +       arg->calc = *calc;
37536 +       arg->ino = ino;
37537 +       au_lcnt_inc(&br->br_count);
37538 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37539 +       if (unlikely(err)) {
37540 +               pr_err("wkq %d\n", err);
37541 +               au_lcnt_dec(&br->br_count);
37542 +               au_kfree_rcu(arg);
37543 +       }
37544 +
37545 +out:
37546 +       return err;
37547 +}
37548 +
37549 +/*
37550 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37551 + * at the position of @h_ino.
37552 + */
37553 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37554 +                ino_t *ino)
37555 +{
37556 +       int err;
37557 +       ssize_t sz;
37558 +       struct au_xi_calc calc;
37559 +       struct au_sbinfo *sbinfo;
37560 +       struct file *file;
37561 +       struct au_xino *xi;
37562 +       struct hlist_bl_head *hbl;
37563 +       struct hlist_bl_node *pos;
37564 +       struct au_xi_writing *p;
37565 +
37566 +       *ino = 0;
37567 +       if (!au_opt_test(au_mntflags(sb), XINO))
37568 +               return 0; /* no xino */
37569 +
37570 +       err = 0;
37571 +       au_xi_calc(sb, h_ino, &calc);
37572 +       xi = au_sbr(sb, bindex)->br_xino;
37573 +       file = au_xino_file(xi, calc.idx);
37574 +       if (!file) {
37575 +               hbl = &xi->xi_writing;
37576 +               hlist_bl_lock(hbl);
37577 +               au_hbl_for_each(pos, hbl) {
37578 +                       p = container_of(pos, struct au_xi_writing, node);
37579 +                       if (p->h_ino == h_ino) {
37580 +                               AuDbg("hi%llu, i%llu, found\n",
37581 +                                     (u64)p->h_ino, (u64)p->ino);
37582 +                               *ino = p->ino;
37583 +                               break;
37584 +                       }
37585 +               }
37586 +               hlist_bl_unlock(hbl);
37587 +               return 0;
37588 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37589 +               return 0; /* no xino */
37590 +
37591 +       sbinfo = au_sbi(sb);
37592 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
37593 +       if (sz == sizeof(*ino))
37594 +               return 0; /* success */
37595 +
37596 +       err = sz;
37597 +       if (unlikely(sz >= 0)) {
37598 +               err = -EIO;
37599 +               AuIOErr("xino read error (%zd)\n", sz);
37600 +       }
37601 +       return err;
37602 +}
37603 +
37604 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
37605 +                           struct au_xi_calc *calc, ino_t ino)
37606 +{
37607 +       ssize_t sz;
37608 +
37609 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
37610 +       if (sz == sizeof(ino))
37611 +               return 0; /* success */
37612 +
37613 +       AuIOErr("write failed (%zd)\n", sz);
37614 +       return -EIO;
37615 +}
37616 +
37617 +/*
37618 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37619 + * at the position of @h_ino.
37620 + * even if @ino is zero, it is written to the xinofile and means no entry.
37621 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37622 + * try truncating it.
37623 + */
37624 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37625 +                 ino_t ino)
37626 +{
37627 +       int err;
37628 +       unsigned int mnt_flags;
37629 +       struct au_xi_calc calc;
37630 +       struct file *file;
37631 +       struct au_branch *br;
37632 +       struct au_xino *xi;
37633 +       struct au_xi_writing *p;
37634 +
37635 +       SiMustAnyLock(sb);
37636 +
37637 +       mnt_flags = au_mntflags(sb);
37638 +       if (!au_opt_test(mnt_flags, XINO))
37639 +               return 0;
37640 +
37641 +       au_xi_calc(sb, h_ino, &calc);
37642 +       br = au_sbr(sb, bindex);
37643 +       xi = br->br_xino;
37644 +       file = au_xino_file(xi, calc.idx);
37645 +       if (!file) {
37646 +               /* store the inum pair into the list */
37647 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37648 +               p->h_ino = h_ino;
37649 +               p->ino = ino;
37650 +               au_hbl_add(&p->node, &xi->xi_writing);
37651 +
37652 +               /* create and write a new xino file asynchronously */
37653 +               err = au_xino_new_async(sb, br, &calc, ino);
37654 +               if (!err)
37655 +                       return 0; /* success */
37656 +               goto out;
37657 +       }
37658 +
37659 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
37660 +       if (!err) {
37661 +               br = au_sbr(sb, bindex);
37662 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37663 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37664 +                       xino_try_trunc(sb, br);
37665 +               return 0; /* success */
37666 +       }
37667 +
37668 +out:
37669 +       AuIOErr("write failed (%d)\n", err);
37670 +       return -EIO;
37671 +}
37672 +
37673 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37674 +                             size_t size, loff_t *pos);
37675 +
37676 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37677 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
37678 +                  loff_t *pos)
37679 +{
37680 +       ssize_t err;
37681 +       mm_segment_t oldfs;
37682 +       union {
37683 +               void *k;
37684 +               char __user *u;
37685 +       } buf;
37686 +       int i;
37687 +       const int prevent_endless = 10;
37688 +
37689 +       i = 0;
37690 +       buf.k = kbuf;
37691 +       oldfs = get_fs();
37692 +       set_fs(KERNEL_DS);
37693 +       do {
37694 +               err = func(file, buf.u, size, pos);
37695 +               if (err == -EINTR
37696 +                   && !au_wkq_test()
37697 +                   && fatal_signal_pending(current)) {
37698 +                       set_fs(oldfs);
37699 +                       err = xino_fread_wkq(func, file, kbuf, size, pos);
37700 +                       BUG_ON(err == -EINTR);
37701 +                       oldfs = get_fs();
37702 +                       set_fs(KERNEL_DS);
37703 +               }
37704 +       } while (i++ < prevent_endless
37705 +                && (err == -EAGAIN || err == -EINTR));
37706 +       set_fs(oldfs);
37707 +
37708 +#if 0 /* reserved for future use */
37709 +       if (err > 0)
37710 +               fsnotify_access(file->f_path.dentry);
37711 +#endif
37712 +
37713 +       return err;
37714 +}
37715 +
37716 +struct xino_fread_args {
37717 +       ssize_t *errp;
37718 +       vfs_readf_t func;
37719 +       struct file *file;
37720 +       void *buf;
37721 +       size_t size;
37722 +       loff_t *pos;
37723 +};
37724 +
37725 +static void call_xino_fread(void *args)
37726 +{
37727 +       struct xino_fread_args *a = args;
37728 +       *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
37729 +}
37730 +
37731 +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
37732 +                             size_t size, loff_t *pos)
37733 +{
37734 +       ssize_t err;
37735 +       int wkq_err;
37736 +       struct xino_fread_args args = {
37737 +               .errp   = &err,
37738 +               .func   = func,
37739 +               .file   = file,
37740 +               .buf    = buf,
37741 +               .size   = size,
37742 +               .pos    = pos
37743 +       };
37744 +
37745 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37746 +       if (unlikely(wkq_err))
37747 +               err = wkq_err;
37748 +
37749 +       return err;
37750 +}
37751 +
37752 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37753 +                              size_t size, loff_t *pos);
37754 +
37755 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
37756 +                             size_t size, loff_t *pos)
37757 +{
37758 +       ssize_t err;
37759 +       mm_segment_t oldfs;
37760 +       union {
37761 +               void *k;
37762 +               const char __user *u;
37763 +       } buf;
37764 +       int i;
37765 +       const int prevent_endless = 10;
37766 +
37767 +       i = 0;
37768 +       buf.k = kbuf;
37769 +       oldfs = get_fs();
37770 +       set_fs(KERNEL_DS);
37771 +       do {
37772 +               err = func(file, buf.u, size, pos);
37773 +               if (err == -EINTR
37774 +                   && !au_wkq_test()
37775 +                   && fatal_signal_pending(current)) {
37776 +                       set_fs(oldfs);
37777 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
37778 +                       BUG_ON(err == -EINTR);
37779 +                       oldfs = get_fs();
37780 +                       set_fs(KERNEL_DS);
37781 +               }
37782 +       } while (i++ < prevent_endless
37783 +                && (err == -EAGAIN || err == -EINTR));
37784 +       set_fs(oldfs);
37785 +
37786 +#if 0 /* reserved for future use */
37787 +       if (err > 0)
37788 +               fsnotify_modify(file->f_path.dentry);
37789 +#endif
37790 +
37791 +       return err;
37792 +}
37793 +
37794 +struct do_xino_fwrite_args {
37795 +       ssize_t *errp;
37796 +       vfs_writef_t func;
37797 +       struct file *file;
37798 +       void *buf;
37799 +       size_t size;
37800 +       loff_t *pos;
37801 +};
37802 +
37803 +static void call_do_xino_fwrite(void *args)
37804 +{
37805 +       struct do_xino_fwrite_args *a = args;
37806 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
37807 +}
37808 +
37809 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
37810 +                              size_t size, loff_t *pos)
37811 +{
37812 +       ssize_t err;
37813 +       int wkq_err;
37814 +       struct do_xino_fwrite_args args = {
37815 +               .errp   = &err,
37816 +               .func   = func,
37817 +               .file   = file,
37818 +               .buf    = buf,
37819 +               .size   = size,
37820 +               .pos    = pos
37821 +       };
37822 +
37823 +       /*
37824 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37825 +        * users should care about quota and real 'filesystem full.'
37826 +        */
37827 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37828 +       if (unlikely(wkq_err))
37829 +               err = wkq_err;
37830 +
37831 +       return err;
37832 +}
37833 +
37834 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
37835 +                   size_t size, loff_t *pos)
37836 +{
37837 +       ssize_t err;
37838 +
37839 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37840 +               lockdep_off();
37841 +               err = do_xino_fwrite(func, file, buf, size, pos);
37842 +               lockdep_on();
37843 +       } else {
37844 +               lockdep_off();
37845 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
37846 +               lockdep_on();
37847 +       }
37848 +
37849 +       return err;
37850 +}
37851 +
37852 +/* ---------------------------------------------------------------------- */
37853 +
37854 +/*
37855 + * inode number bitmap
37856 + */
37857 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37858 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37859 +{
37860 +       ino_t ino;
37861 +
37862 +       AuDebugOn(bit < 0 || page_bits <= bit);
37863 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37864 +       return ino;
37865 +}
37866 +
37867 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37868 +{
37869 +       AuDebugOn(ino < AUFS_FIRST_INO);
37870 +       ino -= AUFS_FIRST_INO;
37871 +       *pindex = ino / page_bits;
37872 +       *bit = ino % page_bits;
37873 +}
37874 +
37875 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37876 +{
37877 +       int err;
37878 +       loff_t pos;
37879 +       ssize_t sz;
37880 +       struct au_sbinfo *sbinfo;
37881 +       struct file *xib;
37882 +       unsigned long *p;
37883 +
37884 +       sbinfo = au_sbi(sb);
37885 +       MtxMustLock(&sbinfo->si_xib_mtx);
37886 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37887 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37888 +
37889 +       if (pindex == sbinfo->si_xib_last_pindex)
37890 +               return 0;
37891 +
37892 +       xib = sbinfo->si_xib;
37893 +       p = sbinfo->si_xib_buf;
37894 +       pos = sbinfo->si_xib_last_pindex;
37895 +       pos *= PAGE_SIZE;
37896 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37897 +       if (unlikely(sz != PAGE_SIZE))
37898 +               goto out;
37899 +
37900 +       pos = pindex;
37901 +       pos *= PAGE_SIZE;
37902 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37903 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
37904 +       else {
37905 +               memset(p, 0, PAGE_SIZE);
37906 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
37907 +       }
37908 +       if (sz == PAGE_SIZE) {
37909 +               sbinfo->si_xib_last_pindex = pindex;
37910 +               return 0; /* success */
37911 +       }
37912 +
37913 +out:
37914 +       AuIOErr1("write failed (%zd)\n", sz);
37915 +       err = sz;
37916 +       if (sz >= 0)
37917 +               err = -EIO;
37918 +       return err;
37919 +}
37920 +
37921 +static void au_xib_clear_bit(struct inode *inode)
37922 +{
37923 +       int err, bit;
37924 +       unsigned long pindex;
37925 +       struct super_block *sb;
37926 +       struct au_sbinfo *sbinfo;
37927 +
37928 +       AuDebugOn(inode->i_nlink);
37929 +
37930 +       sb = inode->i_sb;
37931 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37932 +       AuDebugOn(page_bits <= bit);
37933 +       sbinfo = au_sbi(sb);
37934 +       mutex_lock(&sbinfo->si_xib_mtx);
37935 +       err = xib_pindex(sb, pindex);
37936 +       if (!err) {
37937 +               clear_bit(bit, sbinfo->si_xib_buf);
37938 +               sbinfo->si_xib_next_bit = bit;
37939 +       }
37940 +       mutex_unlock(&sbinfo->si_xib_mtx);
37941 +}
37942 +
37943 +/* ---------------------------------------------------------------------- */
37944 +
37945 +/*
37946 + * truncate a xino bitmap file
37947 + */
37948 +
37949 +/* todo: slow */
37950 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37951 +{
37952 +       int err, bit;
37953 +       ssize_t sz;
37954 +       unsigned long pindex;
37955 +       loff_t pos, pend;
37956 +       struct au_sbinfo *sbinfo;
37957 +       vfs_readf_t func;
37958 +       ino_t *ino;
37959 +       unsigned long *p;
37960 +
37961 +       err = 0;
37962 +       sbinfo = au_sbi(sb);
37963 +       MtxMustLock(&sbinfo->si_xib_mtx);
37964 +       p = sbinfo->si_xib_buf;
37965 +       func = sbinfo->si_xread;
37966 +       pend = vfsub_f_size_read(file);
37967 +       pos = 0;
37968 +       while (pos < pend) {
37969 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
37970 +               err = sz;
37971 +               if (unlikely(sz <= 0))
37972 +                       goto out;
37973 +
37974 +               err = 0;
37975 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37976 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37977 +                               continue;
37978 +
37979 +                       xib_calc_bit(*ino, &pindex, &bit);
37980 +                       AuDebugOn(page_bits <= bit);
37981 +                       err = xib_pindex(sb, pindex);
37982 +                       if (!err)
37983 +                               set_bit(bit, p);
37984 +                       else
37985 +                               goto out;
37986 +               }
37987 +       }
37988 +
37989 +out:
37990 +       return err;
37991 +}
37992 +
37993 +static int xib_restore(struct super_block *sb)
37994 +{
37995 +       int err, i;
37996 +       unsigned int nfile;
37997 +       aufs_bindex_t bindex, bbot;
37998 +       void *page;
37999 +       struct au_branch *br;
38000 +       struct au_xino *xi;
38001 +       struct file *file;
38002 +
38003 +       err = -ENOMEM;
38004 +       page = (void *)__get_free_page(GFP_NOFS);
38005 +       if (unlikely(!page))
38006 +               goto out;
38007 +
38008 +       err = 0;
38009 +       bbot = au_sbbot(sb);
38010 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38011 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38012 +                       br = au_sbr(sb, bindex);
38013 +                       xi = br->br_xino;
38014 +                       nfile = xi->xi_nfile;
38015 +                       for (i = 0; i < nfile; i++) {
38016 +                               file = au_xino_file(xi, i);
38017 +                               if (file)
38018 +                                       err = do_xib_restore(sb, file, page);
38019 +                       }
38020 +               } else
38021 +                       AuDbg("skip shared b%d\n", bindex);
38022 +       free_page((unsigned long)page);
38023 +
38024 +out:
38025 +       return err;
38026 +}
38027 +
38028 +int au_xib_trunc(struct super_block *sb)
38029 +{
38030 +       int err;
38031 +       ssize_t sz;
38032 +       loff_t pos;
38033 +       struct au_sbinfo *sbinfo;
38034 +       unsigned long *p;
38035 +       struct file *file;
38036 +
38037 +       SiMustWriteLock(sb);
38038 +
38039 +       err = 0;
38040 +       sbinfo = au_sbi(sb);
38041 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38042 +               goto out;
38043 +
38044 +       file = sbinfo->si_xib;
38045 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38046 +               goto out;
38047 +
38048 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38049 +       err = PTR_ERR(file);
38050 +       if (IS_ERR(file))
38051 +               goto out;
38052 +       fput(sbinfo->si_xib);
38053 +       sbinfo->si_xib = file;
38054 +
38055 +       p = sbinfo->si_xib_buf;
38056 +       memset(p, 0, PAGE_SIZE);
38057 +       pos = 0;
38058 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
38059 +       if (unlikely(sz != PAGE_SIZE)) {
38060 +               err = sz;
38061 +               AuIOErr("err %d\n", err);
38062 +               if (sz >= 0)
38063 +                       err = -EIO;
38064 +               goto out;
38065 +       }
38066 +
38067 +       mutex_lock(&sbinfo->si_xib_mtx);
38068 +       /* mnt_want_write() is unnecessary here */
38069 +       err = xib_restore(sb);
38070 +       mutex_unlock(&sbinfo->si_xib_mtx);
38071 +
38072 +out:
38073 +       return err;
38074 +}
38075 +
38076 +/* ---------------------------------------------------------------------- */
38077 +
38078 +struct au_xino *au_xino_alloc(unsigned int nfile)
38079 +{
38080 +       struct au_xino *xi;
38081 +
38082 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38083 +       if (unlikely(!xi))
38084 +               goto out;
38085 +       xi->xi_nfile = nfile;
38086 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38087 +       if (unlikely(!xi->xi_file))
38088 +               goto out_free;
38089 +
38090 +       xi->xi_nondir.total = 8; /* initial size */
38091 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38092 +                                     GFP_NOFS);
38093 +       if (unlikely(!xi->xi_nondir.array))
38094 +               goto out_file;
38095 +
38096 +       spin_lock_init(&xi->xi_nondir.spin);
38097 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38098 +       mutex_init(&xi->xi_mtx);
38099 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38100 +       atomic_set(&xi->xi_truncating, 0);
38101 +       kref_init(&xi->xi_kref);
38102 +       goto out; /* success */
38103 +
38104 +out_file:
38105 +       au_kfree_try_rcu(xi->xi_file);
38106 +out_free:
38107 +       au_kfree_rcu(xi);
38108 +       xi = NULL;
38109 +out:
38110 +       return xi;
38111 +}
38112 +
38113 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38114 +{
38115 +       int err;
38116 +       struct au_xino *xi;
38117 +
38118 +       err = 0;
38119 +       xi = au_xino_alloc(idx + 1);
38120 +       if (unlikely(!xi)) {
38121 +               err = -ENOMEM;
38122 +               goto out;
38123 +       }
38124 +
38125 +       if (file)
38126 +               get_file(file);
38127 +       xi->xi_file[idx] = file;
38128 +       AuDebugOn(br->br_xino);
38129 +       br->br_xino = xi;
38130 +
38131 +out:
38132 +       return err;
38133 +}
38134 +
38135 +static void au_xino_release(struct kref *kref)
38136 +{
38137 +       struct au_xino *xi;
38138 +       int i;
38139 +       unsigned long ul;
38140 +       struct hlist_bl_head *hbl;
38141 +       struct hlist_bl_node *pos, *n;
38142 +       struct au_xi_writing *p;
38143 +
38144 +       xi = container_of(kref, struct au_xino, xi_kref);
38145 +       for (i = 0; i < xi->xi_nfile; i++)
38146 +               if (xi->xi_file[i])
38147 +                       fput(xi->xi_file[i]);
38148 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38149 +               AuDebugOn(xi->xi_nondir.array[i]);
38150 +       mutex_destroy(&xi->xi_mtx);
38151 +       hbl = &xi->xi_writing;
38152 +       ul = au_hbl_count(hbl);
38153 +       if (unlikely(ul)) {
38154 +               pr_warn("xi_writing %lu\n", ul);
38155 +               hlist_bl_lock(hbl);
38156 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38157 +                       hlist_bl_del(&p->node);
38158 +                       au_kfree_rcu(p);
38159 +               }
38160 +               hlist_bl_unlock(hbl);
38161 +       }
38162 +       au_kfree_try_rcu(xi->xi_file);
38163 +       au_kfree_try_rcu(xi->xi_nondir.array);
38164 +       au_kfree_rcu(xi);
38165 +}
38166 +
38167 +int au_xino_put(struct au_branch *br)
38168 +{
38169 +       int ret;
38170 +       struct au_xino *xi;
38171 +
38172 +       ret = 0;
38173 +       xi = br->br_xino;
38174 +       if (xi) {
38175 +               br->br_xino = NULL;
38176 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38177 +       }
38178 +
38179 +       return ret;
38180 +}
38181 +
38182 +/* ---------------------------------------------------------------------- */
38183 +
38184 +/*
38185 + * xino mount option handlers
38186 + */
38187 +
38188 +/* xino bitmap */
38189 +static void xino_clear_xib(struct super_block *sb)
38190 +{
38191 +       struct au_sbinfo *sbinfo;
38192 +
38193 +       SiMustWriteLock(sb);
38194 +
38195 +       sbinfo = au_sbi(sb);
38196 +       /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
38197 +       if (sbinfo->si_xib)
38198 +               fput(sbinfo->si_xib);
38199 +       sbinfo->si_xib = NULL;
38200 +       if (sbinfo->si_xib_buf)
38201 +               free_page((unsigned long)sbinfo->si_xib_buf);
38202 +       sbinfo->si_xib_buf = NULL;
38203 +}
38204 +
38205 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38206 +{
38207 +       int err;
38208 +       loff_t pos;
38209 +       struct au_sbinfo *sbinfo;
38210 +       struct file *file;
38211 +       struct super_block *xi_sb;
38212 +
38213 +       SiMustWriteLock(sb);
38214 +
38215 +       sbinfo = au_sbi(sb);
38216 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38217 +       err = PTR_ERR(file);
38218 +       if (IS_ERR(file))
38219 +               goto out;
38220 +       if (sbinfo->si_xib)
38221 +               fput(sbinfo->si_xib);
38222 +       sbinfo->si_xib = file;
38223 +       sbinfo->si_xread = vfs_readf(file);
38224 +       sbinfo->si_xwrite = vfs_writef(file);
38225 +       xi_sb = file_inode(file)->i_sb;
38226 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38227 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38228 +               err = -EIO;
38229 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38230 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38231 +               goto out_unset;
38232 +       }
38233 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38234 +
38235 +       err = -ENOMEM;
38236 +       if (!sbinfo->si_xib_buf)
38237 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38238 +       if (unlikely(!sbinfo->si_xib_buf))
38239 +               goto out_unset;
38240 +
38241 +       sbinfo->si_xib_last_pindex = 0;
38242 +       sbinfo->si_xib_next_bit = 0;
38243 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38244 +               pos = 0;
38245 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
38246 +                                 PAGE_SIZE, &pos);
38247 +               if (unlikely(err != PAGE_SIZE))
38248 +                       goto out_free;
38249 +       }
38250 +       err = 0;
38251 +       goto out; /* success */
38252 +
38253 +out_free:
38254 +       if (sbinfo->si_xib_buf)
38255 +               free_page((unsigned long)sbinfo->si_xib_buf);
38256 +       sbinfo->si_xib_buf = NULL;
38257 +       if (err >= 0)
38258 +               err = -EIO;
38259 +out_unset:
38260 +       fput(sbinfo->si_xib);
38261 +       sbinfo->si_xib = NULL;
38262 +out:
38263 +       AuTraceErr(err);
38264 +       return err;
38265 +}
38266 +
38267 +/* xino for each branch */
38268 +static void xino_clear_br(struct super_block *sb)
38269 +{
38270 +       aufs_bindex_t bindex, bbot;
38271 +       struct au_branch *br;
38272 +
38273 +       bbot = au_sbbot(sb);
38274 +       for (bindex = 0; bindex <= bbot; bindex++) {
38275 +               br = au_sbr(sb, bindex);
38276 +               AuDebugOn(!br);
38277 +               au_xino_put(br);
38278 +       }
38279 +}
38280 +
38281 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38282 +                                 aufs_bindex_t bshared)
38283 +{
38284 +       struct au_branch *brshared;
38285 +
38286 +       brshared = au_sbr(sb, bshared);
38287 +       AuDebugOn(!brshared->br_xino);
38288 +       AuDebugOn(!brshared->br_xino->xi_file);
38289 +       if (br->br_xino != brshared->br_xino) {
38290 +               au_xino_get(brshared);
38291 +               au_xino_put(br);
38292 +               br->br_xino = brshared->br_xino;
38293 +       }
38294 +}
38295 +
38296 +struct au_xino_do_set_br {
38297 +       vfs_writef_t writef;
38298 +       struct au_branch *br;
38299 +       ino_t h_ino;
38300 +       aufs_bindex_t bshared;
38301 +};
38302 +
38303 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38304 +                            struct au_xino_do_set_br *args)
38305 +{
38306 +       int err;
38307 +       struct au_xi_calc calc;
38308 +       struct file *file;
38309 +       struct au_branch *br;
38310 +       struct au_xi_new xinew = {
38311 +               .base = path
38312 +       };
38313 +
38314 +       br = args->br;
38315 +       xinew.xi = br->br_xino;
38316 +       au_xi_calc(sb, args->h_ino, &calc);
38317 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38318 +       if (args->bshared >= 0)
38319 +               /* shared xino */
38320 +               au_xino_set_br_shared(sb, br, args->bshared);
38321 +       else if (!xinew.xi) {
38322 +               /* new xino */
38323 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38324 +               if (unlikely(err))
38325 +                       goto out;
38326 +       }
38327 +
38328 +       /* force re-creating */
38329 +       xinew.xi = br->br_xino;
38330 +       xinew.idx = calc.idx;
38331 +       mutex_lock(&xinew.xi->xi_mtx);
38332 +       file = au_xi_new(sb, &xinew);
38333 +       mutex_unlock(&xinew.xi->xi_mtx);
38334 +       err = PTR_ERR(file);
38335 +       if (IS_ERR(file))
38336 +               goto out;
38337 +       AuDebugOn(!file);
38338 +
38339 +       err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
38340 +       if (unlikely(err))
38341 +               au_xino_put(br);
38342 +
38343 +out:
38344 +       AuTraceErr(err);
38345 +       return err;
38346 +}
38347 +
38348 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38349 +{
38350 +       int err;
38351 +       aufs_bindex_t bindex, bbot;
38352 +       struct au_xino_do_set_br args;
38353 +       struct inode *inode;
38354 +
38355 +       SiMustWriteLock(sb);
38356 +
38357 +       bbot = au_sbbot(sb);
38358 +       inode = d_inode(sb->s_root);
38359 +       args.writef = au_sbi(sb)->si_xwrite;
38360 +       for (bindex = 0; bindex <= bbot; bindex++) {
38361 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38362 +               args.br = au_sbr(sb, bindex);
38363 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38364 +               err = au_xino_do_set_br(sb, path, &args);
38365 +               if (unlikely(err))
38366 +                       break;
38367 +       }
38368 +
38369 +       AuTraceErr(err);
38370 +       return err;
38371 +}
38372 +
38373 +void au_xino_clr(struct super_block *sb)
38374 +{
38375 +       struct au_sbinfo *sbinfo;
38376 +
38377 +       au_xigen_clr(sb);
38378 +       xino_clear_xib(sb);
38379 +       xino_clear_br(sb);
38380 +       dbgaufs_brs_del(sb, 0);
38381 +       sbinfo = au_sbi(sb);
38382 +       /* lvalue, do not call au_mntflags() */
38383 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38384 +}
38385 +
38386 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38387 +{
38388 +       int err, skip;
38389 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38390 +       struct qstr *dname, *cur_name;
38391 +       struct file *cur_xino;
38392 +       struct au_sbinfo *sbinfo;
38393 +       struct path *path, *cur_path;
38394 +
38395 +       SiMustWriteLock(sb);
38396 +
38397 +       err = 0;
38398 +       sbinfo = au_sbi(sb);
38399 +       path = &xiopt->file->f_path;
38400 +       dentry = path->dentry;
38401 +       parent = dget_parent(dentry);
38402 +       if (remount) {
38403 +               skip = 0;
38404 +               cur_xino = sbinfo->si_xib;
38405 +               if (cur_xino) {
38406 +                       cur_path = &cur_xino->f_path;
38407 +                       cur_dentry = cur_path->dentry;
38408 +                       cur_parent = dget_parent(cur_dentry);
38409 +                       cur_name = &cur_dentry->d_name;
38410 +                       dname = &dentry->d_name;
38411 +                       skip = (cur_parent == parent
38412 +                               && au_qstreq(dname, cur_name));
38413 +                       dput(cur_parent);
38414 +               }
38415 +               if (skip)
38416 +                       goto out;
38417 +       }
38418 +
38419 +       au_opt_set(sbinfo->si_mntflags, XINO);
38420 +       err = au_xino_set_xib(sb, path);
38421 +       /* si_x{read,write} are set */
38422 +       if (!err)
38423 +               err = au_xigen_set(sb, path);
38424 +       if (!err)
38425 +               err = au_xino_set_br(sb, path);
38426 +       if (!err) {
38427 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38428 +               goto out; /* success */
38429 +       }
38430 +
38431 +       /* reset all */
38432 +       AuIOErr("failed setting xino(%d).\n", err);
38433 +       au_xino_clr(sb);
38434 +
38435 +out:
38436 +       dput(parent);
38437 +       return err;
38438 +}
38439 +
38440 +/*
38441 + * create a xinofile at the default place/path.
38442 + */
38443 +struct file *au_xino_def(struct super_block *sb)
38444 +{
38445 +       struct file *file;
38446 +       char *page, *p;
38447 +       struct au_branch *br;
38448 +       struct super_block *h_sb;
38449 +       struct path path;
38450 +       aufs_bindex_t bbot, bindex, bwr;
38451 +
38452 +       br = NULL;
38453 +       bbot = au_sbbot(sb);
38454 +       bwr = -1;
38455 +       for (bindex = 0; bindex <= bbot; bindex++) {
38456 +               br = au_sbr(sb, bindex);
38457 +               if (au_br_writable(br->br_perm)
38458 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38459 +                       bwr = bindex;
38460 +                       break;
38461 +               }
38462 +       }
38463 +
38464 +       if (bwr >= 0) {
38465 +               file = ERR_PTR(-ENOMEM);
38466 +               page = (void *)__get_free_page(GFP_NOFS);
38467 +               if (unlikely(!page))
38468 +                       goto out;
38469 +               path.mnt = au_br_mnt(br);
38470 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38471 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38472 +               file = (void *)p;
38473 +               if (!IS_ERR(p)) {
38474 +                       strcat(p, "/" AUFS_XINO_FNAME);
38475 +                       AuDbg("%s\n", p);
38476 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38477 +               }
38478 +               free_page((unsigned long)page);
38479 +       } else {
38480 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38481 +                                     /*wbrtop*/0);
38482 +               if (IS_ERR(file))
38483 +                       goto out;
38484 +               h_sb = file->f_path.dentry->d_sb;
38485 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38486 +                       pr_err("xino doesn't support %s(%s)\n",
38487 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38488 +                       fput(file);
38489 +                       file = ERR_PTR(-EINVAL);
38490 +               }
38491 +       }
38492 +
38493 +out:
38494 +       return file;
38495 +}
38496 +
38497 +/* ---------------------------------------------------------------------- */
38498 +
38499 +/*
38500 + * initialize the xinofile for the specified branch @br
38501 + * at the place/path where @base_file indicates.
38502 + * test whether another branch is on the same filesystem or not,
38503 + * if found then share the xinofile with another branch.
38504 + */
38505 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38506 +                   struct path *base)
38507 +{
38508 +       int err;
38509 +       struct au_xino_do_set_br args = {
38510 +               .h_ino  = h_ino,
38511 +               .br     = br
38512 +       };
38513 +
38514 +       args.writef = au_sbi(sb)->si_xwrite;
38515 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38516 +                                      au_br_sb(br));
38517 +       err = au_xino_do_set_br(sb, base, &args);
38518 +       if (unlikely(err))
38519 +               au_xino_put(br);
38520 +
38521 +       return err;
38522 +}
38523 +
38524 +/* ---------------------------------------------------------------------- */
38525 +
38526 +/*
38527 + * get an unused inode number from bitmap
38528 + */
38529 +ino_t au_xino_new_ino(struct super_block *sb)
38530 +{
38531 +       ino_t ino;
38532 +       unsigned long *p, pindex, ul, pend;
38533 +       struct au_sbinfo *sbinfo;
38534 +       struct file *file;
38535 +       int free_bit, err;
38536 +
38537 +       if (!au_opt_test(au_mntflags(sb), XINO))
38538 +               return iunique(sb, AUFS_FIRST_INO);
38539 +
38540 +       sbinfo = au_sbi(sb);
38541 +       mutex_lock(&sbinfo->si_xib_mtx);
38542 +       p = sbinfo->si_xib_buf;
38543 +       free_bit = sbinfo->si_xib_next_bit;
38544 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38545 +               goto out; /* success */
38546 +       free_bit = find_first_zero_bit(p, page_bits);
38547 +       if (free_bit < page_bits)
38548 +               goto out; /* success */
38549 +
38550 +       pindex = sbinfo->si_xib_last_pindex;
38551 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38552 +               err = xib_pindex(sb, ul);
38553 +               if (unlikely(err))
38554 +                       goto out_err;
38555 +               free_bit = find_first_zero_bit(p, page_bits);
38556 +               if (free_bit < page_bits)
38557 +                       goto out; /* success */
38558 +       }
38559 +
38560 +       file = sbinfo->si_xib;
38561 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38562 +       for (ul = pindex + 1; ul <= pend; ul++) {
38563 +               err = xib_pindex(sb, ul);
38564 +               if (unlikely(err))
38565 +                       goto out_err;
38566 +               free_bit = find_first_zero_bit(p, page_bits);
38567 +               if (free_bit < page_bits)
38568 +                       goto out; /* success */
38569 +       }
38570 +       BUG();
38571 +
38572 +out:
38573 +       set_bit(free_bit, p);
38574 +       sbinfo->si_xib_next_bit = free_bit + 1;
38575 +       pindex = sbinfo->si_xib_last_pindex;
38576 +       mutex_unlock(&sbinfo->si_xib_mtx);
38577 +       ino = xib_calc_ino(pindex, free_bit);
38578 +       AuDbg("i%lu\n", (unsigned long)ino);
38579 +       return ino;
38580 +out_err:
38581 +       mutex_unlock(&sbinfo->si_xib_mtx);
38582 +       AuDbg("i0\n");
38583 +       return 0;
38584 +}
38585 +
38586 +/* for s_op->delete_inode() */
38587 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38588 +{
38589 +       int err;
38590 +       unsigned int mnt_flags;
38591 +       aufs_bindex_t bindex, bbot, bi;
38592 +       unsigned char try_trunc;
38593 +       struct au_iinfo *iinfo;
38594 +       struct super_block *sb;
38595 +       struct au_hinode *hi;
38596 +       struct inode *h_inode;
38597 +       struct au_branch *br;
38598 +       vfs_writef_t xwrite;
38599 +       struct au_xi_calc calc;
38600 +       struct file *file;
38601 +
38602 +       AuDebugOn(au_is_bad_inode(inode));
38603 +
38604 +       sb = inode->i_sb;
38605 +       mnt_flags = au_mntflags(sb);
38606 +       if (!au_opt_test(mnt_flags, XINO)
38607 +           || inode->i_ino == AUFS_ROOT_INO)
38608 +               return;
38609 +
38610 +       if (unlinked) {
38611 +               au_xigen_inc(inode);
38612 +               au_xib_clear_bit(inode);
38613 +       }
38614 +
38615 +       iinfo = au_ii(inode);
38616 +       bindex = iinfo->ii_btop;
38617 +       if (bindex < 0)
38618 +               return;
38619 +
38620 +       xwrite = au_sbi(sb)->si_xwrite;
38621 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38622 +       hi = au_hinode(iinfo, bindex);
38623 +       bbot = iinfo->ii_bbot;
38624 +       for (; bindex <= bbot; bindex++, hi++) {
38625 +               h_inode = hi->hi_inode;
38626 +               if (!h_inode
38627 +                   || (!unlinked && h_inode->i_nlink))
38628 +                       continue;
38629 +
38630 +               /* inode may not be revalidated */
38631 +               bi = au_br_index(sb, hi->hi_id);
38632 +               if (bi < 0)
38633 +                       continue;
38634 +
38635 +               br = au_sbr(sb, bi);
38636 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38637 +               file = au_xino_file(br->br_xino, calc.idx);
38638 +               if (IS_ERR_OR_NULL(file))
38639 +                       continue;
38640 +
38641 +               err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
38642 +               if (!err && try_trunc
38643 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38644 +                       xino_try_trunc(sb, br);
38645 +       }
38646 +}
38647 +
38648 +/* ---------------------------------------------------------------------- */
38649 +
38650 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38651 +{
38652 +       int found, total, i;
38653 +
38654 +       found = -1;
38655 +       total = xi->xi_nondir.total;
38656 +       for (i = 0; i < total; i++) {
38657 +               if (xi->xi_nondir.array[i] != h_ino)
38658 +                       continue;
38659 +               found = i;
38660 +               break;
38661 +       }
38662 +
38663 +       return found;
38664 +}
38665 +
38666 +static int au_xinondir_expand(struct au_xino *xi)
38667 +{
38668 +       int err, sz;
38669 +       ino_t *p;
38670 +
38671 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38672 +
38673 +       err = -ENOMEM;
38674 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38675 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38676 +               goto out;
38677 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38678 +                        /*may_shrink*/0);
38679 +       if (p) {
38680 +               xi->xi_nondir.array = p;
38681 +               xi->xi_nondir.total <<= 1;
38682 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38683 +               err = 0;
38684 +       }
38685 +
38686 +out:
38687 +       return err;
38688 +}
38689 +
38690 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38691 +                      ino_t h_ino, int idx)
38692 +{
38693 +       struct au_xino *xi;
38694 +
38695 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38696 +       xi = au_sbr(sb, bindex)->br_xino;
38697 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38698 +
38699 +       spin_lock(&xi->xi_nondir.spin);
38700 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38701 +       xi->xi_nondir.array[idx] = 0;
38702 +       spin_unlock(&xi->xi_nondir.spin);
38703 +       wake_up_all(&xi->xi_nondir.wqh);
38704 +}
38705 +
38706 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38707 +                     int *idx)
38708 +{
38709 +       int err, found, empty;
38710 +       struct au_xino *xi;
38711 +
38712 +       err = 0;
38713 +       *idx = -1;
38714 +       if (!au_opt_test(au_mntflags(sb), XINO))
38715 +               goto out; /* no xino */
38716 +
38717 +       xi = au_sbr(sb, bindex)->br_xino;
38718 +
38719 +again:
38720 +       spin_lock(&xi->xi_nondir.spin);
38721 +       found = au_xinondir_find(xi, h_ino);
38722 +       if (found == -1) {
38723 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38724 +               if (empty == -1) {
38725 +                       empty = xi->xi_nondir.total;
38726 +                       err = au_xinondir_expand(xi);
38727 +                       if (unlikely(err))
38728 +                               goto out_unlock;
38729 +               }
38730 +               xi->xi_nondir.array[empty] = h_ino;
38731 +               *idx = empty;
38732 +       } else {
38733 +               spin_unlock(&xi->xi_nondir.spin);
38734 +               wait_event(xi->xi_nondir.wqh,
38735 +                          xi->xi_nondir.array[found] != h_ino);
38736 +               goto again;
38737 +       }
38738 +
38739 +out_unlock:
38740 +       spin_unlock(&xi->xi_nondir.spin);
38741 +out:
38742 +       return err;
38743 +}
38744 +
38745 +/* ---------------------------------------------------------------------- */
38746 +
38747 +int au_xino_path(struct seq_file *seq, struct file *file)
38748 +{
38749 +       int err;
38750 +
38751 +       err = au_seq_path(seq, &file->f_path);
38752 +       if (unlikely(err))
38753 +               goto out;
38754 +
38755 +#define Deleted "\\040(deleted)"
38756 +       seq->count -= sizeof(Deleted) - 1;
38757 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38758 +                        sizeof(Deleted) - 1));
38759 +#undef Deleted
38760 +
38761 +out:
38762 +       return err;
38763 +}
38764 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38765 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38766 +++ linux/include/uapi/linux/aufs_type.h        2020-01-27 10:57:18.182205184 +0100
38767 @@ -0,0 +1,452 @@
38768 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38769 +/*
38770 + * Copyright (C) 2005-2020 Junjiro R. Okajima
38771 + *
38772 + * This program, aufs is free software; you can redistribute it and/or modify
38773 + * it under the terms of the GNU General Public License as published by
38774 + * the Free Software Foundation; either version 2 of the License, or
38775 + * (at your option) any later version.
38776 + *
38777 + * This program is distributed in the hope that it will be useful,
38778 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38779 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38780 + * GNU General Public License for more details.
38781 + *
38782 + * You should have received a copy of the GNU General Public License
38783 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38784 + */
38785 +
38786 +#ifndef __AUFS_TYPE_H__
38787 +#define __AUFS_TYPE_H__
38788 +
38789 +#define AUFS_NAME      "aufs"
38790 +
38791 +#ifdef __KERNEL__
38792 +/*
38793 + * define it before including all other headers.
38794 + * sched.h may use pr_* macros before defining "current", so define the
38795 + * no-current version first, and re-define later.
38796 + */
38797 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38798 +#include <linux/sched.h>
38799 +#undef pr_fmt
38800 +#define pr_fmt(fmt) \
38801 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38802 +               (int)sizeof(current->comm), current->comm, current->pid
38803 +#include <linux/limits.h>
38804 +#else
38805 +#include <stdint.h>
38806 +#include <sys/types.h>
38807 +#include <limits.h>
38808 +#endif /* __KERNEL__ */
38809 +
38810 +#define AUFS_VERSION   "5.x-rcN-20200127"
38811 +
38812 +/* todo? move this to linux-2.6.19/include/magic.h */
38813 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38814 +
38815 +/* ---------------------------------------------------------------------- */
38816 +
38817 +#ifdef __KERNEL__
38818 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38819 +typedef int8_t aufs_bindex_t;
38820 +#define AUFS_BRANCH_MAX 127
38821 +#else
38822 +typedef int16_t aufs_bindex_t;
38823 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38824 +#define AUFS_BRANCH_MAX 511
38825 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38826 +#define AUFS_BRANCH_MAX 1023
38827 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38828 +#define AUFS_BRANCH_MAX 32767
38829 +#endif
38830 +#endif
38831 +
38832 +#ifndef AUFS_BRANCH_MAX
38833 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38834 +#endif
38835 +#endif /* __KERNEL__ */
38836 +
38837 +/* ---------------------------------------------------------------------- */
38838 +
38839 +#define AUFS_FSTYPE            AUFS_NAME
38840 +
38841 +#define AUFS_ROOT_INO          2
38842 +#define AUFS_FIRST_INO         11
38843 +
38844 +#define AUFS_WH_PFX            ".wh."
38845 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38846 +#define AUFS_WH_TMP_LEN                4
38847 +/* a limit for rmdir/rename a dir and copyup */
38848 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38849 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38850 +                               - 1                     /* dot */\
38851 +                               - AUFS_WH_TMP_LEN)      /* hex */
38852 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38853 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38854 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38855 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38856 +#define AUFS_DIRWH_DEF         3
38857 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38858 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38859 +#define AUFS_RDBLK_DEF         512 /* bytes */
38860 +#define AUFS_RDHASH_DEF                32
38861 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38862 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38863 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38864 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38865 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38866 +
38867 +/* pseudo-link maintenace under /proc */
38868 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38869 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38870 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38871 +
38872 +/* dirren, renamed dir */
38873 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38874 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38875 +/* whiteouted doubly */
38876 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38877 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38878 +
38879 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38880 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38881 +
38882 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38883 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38884 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38885 +
38886 +/* doubly whiteouted */
38887 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38888 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38889 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38890 +
38891 +/* branch permissions and attributes */
38892 +#define AUFS_BRPERM_RW         "rw"
38893 +#define AUFS_BRPERM_RO         "ro"
38894 +#define AUFS_BRPERM_RR         "rr"
38895 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38896 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38897 +#define AUFS_BRATTR_FHSM       "fhsm"
38898 +#define AUFS_BRATTR_UNPIN      "unpin"
38899 +#define AUFS_BRATTR_ICEX       "icex"
38900 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38901 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38902 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38903 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38904 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38905 +#define AUFS_BRRATTR_WH                "wh"
38906 +#define AUFS_BRWATTR_NLWH      "nolwh"
38907 +#define AUFS_BRWATTR_MOO       "moo"
38908 +
38909 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38910 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38911 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38912 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38913 +
38914 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38915 +#define AuBrAttr_COO_ALL       (1 << 4)
38916 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38917 +
38918 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38919 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38920 +                                                  branch. meaningless since
38921 +                                                  linux-3.18-rc1 */
38922 +
38923 +/* ignore error in copying XATTR */
38924 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38925 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38926 +#define AuBrAttr_ICEX_TR       (1 << 9)
38927 +#define AuBrAttr_ICEX_USR      (1 << 10)
38928 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38929 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38930 +                                | AuBrAttr_ICEX_SYS    \
38931 +                                | AuBrAttr_ICEX_TR     \
38932 +                                | AuBrAttr_ICEX_USR    \
38933 +                                | AuBrAttr_ICEX_OTH)
38934 +
38935 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38936 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38937 +
38938 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38939 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38940 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38941 +
38942 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38943 +
38944 +/* #warning test userspace */
38945 +#ifdef __KERNEL__
38946 +#ifndef CONFIG_AUFS_FHSM
38947 +#undef AuBrAttr_FHSM
38948 +#define AuBrAttr_FHSM          0
38949 +#endif
38950 +#ifndef CONFIG_AUFS_XATTR
38951 +#undef AuBrAttr_ICEX
38952 +#define AuBrAttr_ICEX          0
38953 +#undef AuBrAttr_ICEX_SEC
38954 +#define AuBrAttr_ICEX_SEC      0
38955 +#undef AuBrAttr_ICEX_SYS
38956 +#define AuBrAttr_ICEX_SYS      0
38957 +#undef AuBrAttr_ICEX_TR
38958 +#define AuBrAttr_ICEX_TR       0
38959 +#undef AuBrAttr_ICEX_USR
38960 +#define AuBrAttr_ICEX_USR      0
38961 +#undef AuBrAttr_ICEX_OTH
38962 +#define AuBrAttr_ICEX_OTH      0
38963 +#endif
38964 +#endif
38965 +
38966 +/* the longest combination */
38967 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38968 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38969 +                              "+" AUFS_BRATTR_COO_REG          \
38970 +                              "+" AUFS_BRATTR_FHSM             \
38971 +                              "+" AUFS_BRATTR_UNPIN            \
38972 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38973 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38974 +                              "+" AUFS_BRATTR_ICEX_USR         \
38975 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38976 +                              "+" AUFS_BRWATTR_NLWH)
38977 +
38978 +typedef struct {
38979 +       char a[AuBrPermStrSz];
38980 +} au_br_perm_str_t;
38981 +
38982 +static inline int au_br_writable(int brperm)
38983 +{
38984 +       return brperm & AuBrPerm_RW;
38985 +}
38986 +
38987 +static inline int au_br_whable(int brperm)
38988 +{
38989 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38990 +}
38991 +
38992 +static inline int au_br_wh_linkable(int brperm)
38993 +{
38994 +       return !(brperm & AuBrWAttr_NoLinkWH);
38995 +}
38996 +
38997 +static inline int au_br_cmoo(int brperm)
38998 +{
38999 +       return brperm & AuBrAttr_CMOO_Mask;
39000 +}
39001 +
39002 +static inline int au_br_fhsm(int brperm)
39003 +{
39004 +       return brperm & AuBrAttr_FHSM;
39005 +}
39006 +
39007 +/* ---------------------------------------------------------------------- */
39008 +
39009 +/* ioctl */
39010 +enum {
39011 +       /* readdir in userspace */
39012 +       AuCtl_RDU,
39013 +       AuCtl_RDU_INO,
39014 +
39015 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39016 +       AuCtl_IBUSY,    /* busy inode */
39017 +       AuCtl_MVDOWN,   /* move-down */
39018 +       AuCtl_BR,       /* info about branches */
39019 +       AuCtl_FHSM_FD   /* connection for fhsm */
39020 +};
39021 +
39022 +/* borrowed from linux/include/linux/kernel.h */
39023 +#ifndef ALIGN
39024 +#ifdef _GNU_SOURCE
39025 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39026 +#else
39027 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39028 +#endif
39029 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39030 +#endif
39031 +
39032 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39033 +#ifndef __aligned
39034 +#define __aligned(x)                   __attribute__((aligned(x)))
39035 +#endif
39036 +
39037 +#ifdef __KERNEL__
39038 +#ifndef __packed
39039 +#define __packed                       __attribute__((packed))
39040 +#endif
39041 +#endif
39042 +
39043 +struct au_rdu_cookie {
39044 +       uint64_t        h_pos;
39045 +       int16_t         bindex;
39046 +       uint8_t         flags;
39047 +       uint8_t         pad;
39048 +       uint32_t        generation;
39049 +} __aligned(8);
39050 +
39051 +struct au_rdu_ent {
39052 +       uint64_t        ino;
39053 +       int16_t         bindex;
39054 +       uint8_t         type;
39055 +       uint8_t         nlen;
39056 +       uint8_t         wh;
39057 +       char            name[0];
39058 +} __aligned(8);
39059 +
39060 +static inline int au_rdu_len(int nlen)
39061 +{
39062 +       /* include the terminating NULL */
39063 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39064 +                    sizeof(uint64_t));
39065 +}
39066 +
39067 +union au_rdu_ent_ul {
39068 +       struct au_rdu_ent __user        *e;
39069 +       uint64_t                        ul;
39070 +};
39071 +
39072 +enum {
39073 +       AufsCtlRduV_SZ,
39074 +       AufsCtlRduV_End
39075 +};
39076 +
39077 +struct aufs_rdu {
39078 +       /* input */
39079 +       union {
39080 +               uint64_t        sz;     /* AuCtl_RDU */
39081 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39082 +       };
39083 +       union au_rdu_ent_ul     ent;
39084 +       uint16_t                verify[AufsCtlRduV_End];
39085 +
39086 +       /* input/output */
39087 +       uint32_t                blk;
39088 +
39089 +       /* output */
39090 +       union au_rdu_ent_ul     tail;
39091 +       /* number of entries which were added in a single call */
39092 +       uint64_t                rent;
39093 +       uint8_t                 full;
39094 +       uint8_t                 shwh;
39095 +
39096 +       struct au_rdu_cookie    cookie;
39097 +} __aligned(8);
39098 +
39099 +/* ---------------------------------------------------------------------- */
39100 +
39101 +/* dirren. the branch is identified by the filename who contains this */
39102 +struct au_drinfo {
39103 +       uint64_t ino;
39104 +       union {
39105 +               uint8_t oldnamelen;
39106 +               uint64_t _padding;
39107 +       };
39108 +       uint8_t oldname[0];
39109 +} __aligned(8);
39110 +
39111 +struct au_drinfo_fdata {
39112 +       uint32_t magic;
39113 +       struct au_drinfo drinfo;
39114 +} __aligned(8);
39115 +
39116 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39117 +/* future */
39118 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39119 +
39120 +/* ---------------------------------------------------------------------- */
39121 +
39122 +struct aufs_wbr_fd {
39123 +       uint32_t        oflags;
39124 +       int16_t         brid;
39125 +} __aligned(8);
39126 +
39127 +/* ---------------------------------------------------------------------- */
39128 +
39129 +struct aufs_ibusy {
39130 +       uint64_t        ino, h_ino;
39131 +       int16_t         bindex;
39132 +} __aligned(8);
39133 +
39134 +/* ---------------------------------------------------------------------- */
39135 +
39136 +/* error code for move-down */
39137 +/* the actual message strings are implemented in aufs-util.git */
39138 +enum {
39139 +       EAU_MVDOWN_OPAQUE = 1,
39140 +       EAU_MVDOWN_WHITEOUT,
39141 +       EAU_MVDOWN_UPPER,
39142 +       EAU_MVDOWN_BOTTOM,
39143 +       EAU_MVDOWN_NOUPPER,
39144 +       EAU_MVDOWN_NOLOWERBR,
39145 +       EAU_Last
39146 +};
39147 +
39148 +/* flags for move-down */
39149 +#define AUFS_MVDOWN_DMSG       1
39150 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39151 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39152 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39153 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39154 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39155 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39156 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39157 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39158 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39159 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39160 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39161 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39162 +
39163 +/* index for move-down */
39164 +enum {
39165 +       AUFS_MVDOWN_UPPER,
39166 +       AUFS_MVDOWN_LOWER,
39167 +       AUFS_MVDOWN_NARRAY
39168 +};
39169 +
39170 +/*
39171 + * additional info of move-down
39172 + * number of free blocks and inodes.
39173 + * subset of struct kstatfs, but smaller and always 64bit.
39174 + */
39175 +struct aufs_stfs {
39176 +       uint64_t        f_blocks;
39177 +       uint64_t        f_bavail;
39178 +       uint64_t        f_files;
39179 +       uint64_t        f_ffree;
39180 +};
39181 +
39182 +struct aufs_stbr {
39183 +       int16_t                 brid;   /* optional input */
39184 +       int16_t                 bindex; /* output */
39185 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39186 +} __aligned(8);
39187 +
39188 +struct aufs_mvdown {
39189 +       uint32_t                flags;                  /* input/output */
39190 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39191 +       int8_t                  au_errno;               /* output */
39192 +} __aligned(8);
39193 +
39194 +/* ---------------------------------------------------------------------- */
39195 +
39196 +union aufs_brinfo {
39197 +       /* PATH_MAX may differ between kernel-space and user-space */
39198 +       char    _spacer[4096];
39199 +       struct {
39200 +               int16_t id;
39201 +               int     perm;
39202 +               char    path[0];
39203 +       };
39204 +} __aligned(8);
39205 +
39206 +/* ---------------------------------------------------------------------- */
39207 +
39208 +#define AuCtlType              'A'
39209 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39210 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39211 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39212 +                                    struct aufs_wbr_fd)
39213 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39214 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39215 +                                     struct aufs_mvdown)
39216 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39217 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39218 +
39219 +#endif /* __AUFS_TYPE_H__ */
39220 SPDX-License-Identifier: GPL-2.0
39221 aufs5.x-rcN loopback patch
39222
39223 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39224 index 872e3b3a7693..768100025129 100644
39225 --- a/drivers/block/loop.c
39226 +++ b/drivers/block/loop.c
39227 @@ -635,6 +635,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39228                         lo->use_dio);
39229  }
39230  
39231 +static struct file *loop_real_file(struct file *file)
39232 +{
39233 +       struct file *f = NULL;
39234 +
39235 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39236 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39237 +       return f;
39238 +}
39239 +
39240  static void loop_reread_partitions(struct loop_device *lo,
39241                                    struct block_device *bdev)
39242  {
39243 @@ -690,6 +699,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39244                           unsigned int arg)
39245  {
39246         struct file     *file = NULL, *old_file;
39247 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39248         int             error;
39249         bool            partscan;
39250  
39251 @@ -709,12 +719,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39252         file = fget(arg);
39253         if (!file)
39254                 goto out_err;
39255 +       f = loop_real_file(file);
39256 +       if (f) {
39257 +               virt_file = file;
39258 +               file = f;
39259 +               get_file(file);
39260 +       }
39261  
39262         error = loop_validate_file(file, bdev);
39263         if (error)
39264                 goto out_err;
39265  
39266         old_file = lo->lo_backing_file;
39267 +       old_virt_file = lo->lo_backing_virt_file;
39268  
39269         error = -EINVAL;
39270  
39271 @@ -726,6 +743,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39272         blk_mq_freeze_queue(lo->lo_queue);
39273         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39274         lo->lo_backing_file = file;
39275 +       lo->lo_backing_virt_file = virt_file;
39276         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39277         mapping_set_gfp_mask(file->f_mapping,
39278                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39279 @@ -739,6 +757,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39280          * dependency.
39281          */
39282         fput(old_file);
39283 +       if (old_virt_file)
39284 +               fput(old_virt_file);
39285         if (partscan)
39286                 loop_reread_partitions(lo, bdev);
39287         return 0;
39288 @@ -747,6 +767,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39289         mutex_unlock(&loop_ctl_mutex);
39290         if (file)
39291                 fput(file);
39292 +       if (virt_file)
39293 +               fput(virt_file);
39294         return error;
39295  }
39296  
39297 @@ -951,7 +973,7 @@ static void loop_update_rotational(struct loop_device *lo)
39298  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39299                        struct block_device *bdev, unsigned int arg)
39300  {
39301 -       struct file     *file;
39302 +       struct file     *file, *f, *virt_file = NULL;
39303         struct inode    *inode;
39304         struct address_space *mapping;
39305         struct block_device *claimed_bdev = NULL;
39306 @@ -967,6 +989,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39307         file = fget(arg);
39308         if (!file)
39309                 goto out;
39310 +       f = loop_real_file(file);
39311 +       if (f) {
39312 +               virt_file = file;
39313 +               file = f;
39314 +               get_file(file);
39315 +       }
39316  
39317         /*
39318          * If we don't hold exclusive handle for the device, upgrade to it
39319 @@ -1015,6 +1043,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39320         lo->lo_device = bdev;
39321         lo->lo_flags = lo_flags;
39322         lo->lo_backing_file = file;
39323 +       lo->lo_backing_virt_file = virt_file;
39324         lo->transfer = NULL;
39325         lo->ioctl = NULL;
39326         lo->lo_sizelimit = 0;
39327 @@ -1068,6 +1097,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39328                 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
39329  out_putf:
39330         fput(file);
39331 +       if (virt_file)
39332 +               fput(virt_file);
39333  out:
39334         /* This is safe: open() is still holding a reference. */
39335         module_put(THIS_MODULE);
39336 @@ -1114,6 +1145,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39337  static int __loop_clr_fd(struct loop_device *lo, bool release)
39338  {
39339         struct file *filp = NULL;
39340 +       struct file *virt_filp = lo->lo_backing_virt_file;
39341         gfp_t gfp = lo->old_gfp_mask;
39342         struct block_device *bdev = lo->lo_device;
39343         int err = 0;
39344 @@ -1137,6 +1169,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39345  
39346         spin_lock_irq(&lo->lo_lock);
39347         lo->lo_backing_file = NULL;
39348 +       lo->lo_backing_virt_file = NULL;
39349         spin_unlock_irq(&lo->lo_lock);
39350  
39351         loop_release_xfer(lo);
39352 @@ -1220,6 +1253,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39353          */
39354         if (filp)
39355                 fput(filp);
39356 +       if (virt_filp)
39357 +               fput(virt_filp);
39358         return err;
39359  }
39360  
39361 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39362 index af75a5ee4094..1d847cb194ff 100644
39363 --- a/drivers/block/loop.h
39364 +++ b/drivers/block/loop.h
39365 @@ -46,7 +46,7 @@ struct loop_device {
39366         int             (*ioctl)(struct loop_device *, int cmd, 
39367                                  unsigned long arg); 
39368  
39369 -       struct file *   lo_backing_file;
39370 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39371         struct block_device *lo_device;
39372         void            *key_data; 
39373  
39374 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39375 index 6fb4a4ed8cc7..ba9a959f2db2 100644
39376 --- a/fs/aufs/f_op.c
39377 +++ b/fs/aufs/f_op.c
39378 @@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39379         if (IS_ERR(h_file))
39380                 goto out;
39381  
39382 -       if (au_test_loopback_kthread()) {
39383 +       if (0 && au_test_loopback_kthread()) {
39384                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39385                 if (file->f_mapping != h_file->f_mapping) {
39386                         file->f_mapping = h_file->f_mapping;
39387 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39388 index a8b63acc6204..9d97c3af5686 100644
39389 --- a/fs/aufs/loop.c
39390 +++ b/fs/aufs/loop.c
39391 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39392                 symbol_put(loop_backing_file);
39393         au_kfree_try_rcu(au_warn_loopback_array);
39394  }
39395 +
39396 +/* ---------------------------------------------------------------------- */
39397 +
39398 +/* support the loopback block device insude aufs */
39399 +
39400 +struct file *aufs_real_loop(struct file *file)
39401 +{
39402 +       struct file *f;
39403 +
39404 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39405 +       fi_read_lock(file);
39406 +       f = au_hf_top(file);
39407 +       fi_read_unlock(file);
39408 +       AuDebugOn(!f);
39409 +       return f;
39410 +}
39411 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39412 index 94f4f80ae33b..ca1194354aff 100644
39413 --- a/fs/aufs/loop.h
39414 +++ b/fs/aufs/loop.h
39415 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39416  
39417  int au_loopback_init(void);
39418  void au_loopback_fin(void);
39419 +
39420 +struct file *aufs_real_loop(struct file *file);
39421  #else
39422  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39423  
39424 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39425  
39426  AuStubInt0(au_loopback_init, void)
39427  AuStubVoid(au_loopback_fin, void)
39428 +
39429 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39430  #endif /* BLK_DEV_LOOP */
39431  
39432  #endif /* __KERNEL__ */
39433 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39434 index 589dd0122020..801e0a7faec5 100644
39435 --- a/fs/aufs/super.c
39436 +++ b/fs/aufs/super.c
39437 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39438         .statfs         = aufs_statfs,
39439         .put_super      = aufs_put_super,
39440         .sync_fs        = aufs_sync_fs,
39441 -       .remount_fs     = aufs_remount_fs
39442 +       .remount_fs     = aufs_remount_fs,
39443 +#ifdef CONFIG_AUFS_BDEV_LOOP
39444 +       .real_loop      = aufs_real_loop
39445 +#endif
39446  };
39447  
39448  /* ---------------------------------------------------------------------- */
39449 diff --git a/include/linux/fs.h b/include/linux/fs.h
39450 index 5fc7807f7c2e..07e24e72706e 100644
39451 --- a/include/linux/fs.h
39452 +++ b/include/linux/fs.h
39453 @@ -1980,6 +1980,10 @@ struct super_operations {
39454                                   struct shrink_control *);
39455         long (*free_cached_objects)(struct super_block *,
39456                                     struct shrink_control *);
39457 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39458 +       /* and aufs */
39459 +       struct file *(*real_loop)(struct file *);
39460 +#endif
39461  };
39462  
39463  /*
This page took 2.997748 seconds and 3 git commands to generate.